Blame SOURCES/0019-setfiles-restorecon-support-parallel-relabeling.patch

139c80
From fba88f42bf8490a23fa6dcd33de2ccd59170009b Mon Sep 17 00:00:00 2001
139c80
From: Ondrej Mosnacek <omosnace@redhat.com>
139c80
Date: Tue, 26 Oct 2021 13:52:39 +0200
139c80
Subject: [PATCH] setfiles/restorecon: support parallel relabeling
139c80
139c80
Use the newly introduced selinux_restorecon_parallel(3) in
139c80
setfiles/restorecon and a -T option to both to allow enabling parallel
139c80
relabeling. The default behavior without specifying the -T option is to
139c80
use 1 thread; parallel relabeling must be requested explicitly by
139c80
passing -T 0 (which will use as many threads as there are available CPU
139c80
cores) or -T <N>, which will use <N> threads.
139c80
139c80
=== Benchmarks ===
139c80
As measured on a 32-core cloud VM with Fedora 34. Not a fully
139c80
representative environment, but still the scaling is quite good.
139c80
139c80
WITHOUT PATCHES:
139c80
$ time restorecon -rn /usr
139c80
139c80
real    0m21.689s
139c80
user    0m21.070s
139c80
sys     0m0.494s
139c80
139c80
WITH PATCHES:
139c80
$ time restorecon -rn /usr
139c80
139c80
real    0m23.940s
139c80
user    0m23.127s
139c80
sys     0m0.653s
139c80
$ time restorecon -rn -T 2 /usr
139c80
139c80
real    0m13.145s
139c80
user    0m25.306s
139c80
sys     0m0.695s
139c80
$ time restorecon -rn -T 4 /usr
139c80
139c80
real    0m7.559s
139c80
user    0m28.470s
139c80
sys     0m1.099s
139c80
$ time restorecon -rn -T 8 /usr
139c80
139c80
real    0m5.186s
139c80
user    0m37.450s
139c80
sys     0m2.094s
139c80
$ time restorecon -rn -T 16 /usr
139c80
139c80
real    0m3.831s
139c80
user    0m51.220s
139c80
sys     0m4.895s
139c80
$ time restorecon -rn -T 32 /usr
139c80
139c80
real    0m2.650s
139c80
user    1m5.136s
139c80
sys     0m6.614s
139c80
139c80
Note that the benchmarks were performed in read-only mode (-n), so the
139c80
labels were only read and looked up in the database, not written. When
139c80
fixing labels on a heavily mislabeled system, the scaling would likely
139c80
be event better, since a larger % of work could be done in parallel.
139c80
139c80
Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
139c80
---
139c80
 policycoreutils/setfiles/Makefile     |  2 +-
139c80
 policycoreutils/setfiles/restore.c    |  7 ++++---
139c80
 policycoreutils/setfiles/restore.h    |  2 +-
139c80
 policycoreutils/setfiles/restorecon.8 |  9 +++++++++
139c80
 policycoreutils/setfiles/setfiles.8   |  9 +++++++++
139c80
 policycoreutils/setfiles/setfiles.c   | 28 ++++++++++++++++-----------
139c80
 6 files changed, 41 insertions(+), 16 deletions(-)
139c80
139c80
diff --git a/policycoreutils/setfiles/Makefile b/policycoreutils/setfiles/Makefile
139c80
index 63d818509791..d7670a8ff54b 100644
139c80
--- a/policycoreutils/setfiles/Makefile
139c80
+++ b/policycoreutils/setfiles/Makefile
139c80
@@ -6,7 +6,7 @@ MANDIR = $(PREFIX)/share/man
139c80
 AUDITH ?= $(shell test -f /usr/include/libaudit.h && echo y)
139c80
 
139c80
 CFLAGS ?= -g -Werror -Wall -W
139c80
-override LDLIBS += -lselinux -lsepol
139c80
+override LDLIBS += -lselinux -lsepol -lpthread
139c80
 
139c80
 ifeq ($(AUDITH), y)
139c80
 	override CFLAGS += -DUSE_AUDIT
139c80
diff --git a/policycoreutils/setfiles/restore.c b/policycoreutils/setfiles/restore.c
139c80
index 9d688c609f79..74d48bb3752d 100644
139c80
--- a/policycoreutils/setfiles/restore.c
139c80
+++ b/policycoreutils/setfiles/restore.c
139c80
@@ -72,7 +72,7 @@ void restore_finish(void)
139c80
 	}
139c80
 }
139c80
 
139c80
-int process_glob(char *name, struct restore_opts *opts)
139c80
+int process_glob(char *name, struct restore_opts *opts, size_t nthreads)
139c80
 {
139c80
 	glob_t globbuf;
139c80
 	size_t i = 0;
139c80
@@ -91,8 +91,9 @@ int process_glob(char *name, struct restore_opts *opts)
139c80
 			continue;
139c80
 		if (len > 0 && strcmp(&globbuf.gl_pathv[i][len], "/..") == 0)
139c80
 			continue;
139c80
-		rc = selinux_restorecon(globbuf.gl_pathv[i],
139c80
-					opts->restorecon_flags);
139c80
+		rc = selinux_restorecon_parallel(globbuf.gl_pathv[i],
139c80
+						 opts->restorecon_flags,
139c80
+						 nthreads);
139c80
 		if (rc < 0)
139c80
 			errors = rc;
139c80
 	}
139c80
diff --git a/policycoreutils/setfiles/restore.h b/policycoreutils/setfiles/restore.h
139c80
index ac6ad6809f4f..bb35a1db9e34 100644
139c80
--- a/policycoreutils/setfiles/restore.h
139c80
+++ b/policycoreutils/setfiles/restore.h
139c80
@@ -49,7 +49,7 @@ struct restore_opts {
139c80
 void restore_init(struct restore_opts *opts);
139c80
 void restore_finish(void);
139c80
 void add_exclude(const char *directory);
139c80
-int process_glob(char *name, struct restore_opts *opts);
139c80
+int process_glob(char *name, struct restore_opts *opts, size_t nthreads);
139c80
 extern char **exclude_list;
139c80
 
139c80
 #endif
139c80
diff --git a/policycoreutils/setfiles/restorecon.8 b/policycoreutils/setfiles/restorecon.8
139c80
index a8900f02b3f3..dbd55ce7c512 100644
139c80
--- a/policycoreutils/setfiles/restorecon.8
139c80
+++ b/policycoreutils/setfiles/restorecon.8
139c80
@@ -33,6 +33,8 @@ restorecon \- restore file(s) default SELinux security contexts.
139c80
 .RB [ \-W ]
139c80
 .RB [ \-I | \-D ]
139c80
 .RB [ \-x ]
139c80
+.RB [ \-T
139c80
+.IR nthreads ]
139c80
 
139c80
 .SH "DESCRIPTION"
139c80
 This manual page describes the
139c80
@@ -160,6 +162,13 @@ prevent
139c80
 .B restorecon
139c80
 from crossing file system boundaries.
139c80
 .TP
139c80
+.BI \-T \ nthreads
139c80
+use up to
139c80
+.I nthreads
139c80
+threads.  Specify 0 to create as many threads as there are available
139c80
+CPU cores; 1 to use only a single thread (default); or any positive
139c80
+number to use the given number of threads (if possible).
139c80
+.TP
139c80
 .SH "ARGUMENTS"
139c80
 .IR pathname \ ...
139c80
 The pathname for the file(s) to be relabeled.
139c80
diff --git a/policycoreutils/setfiles/setfiles.8 b/policycoreutils/setfiles/setfiles.8
139c80
index 0692121f2f4d..8ef9f602e843 100644
139c80
--- a/policycoreutils/setfiles/setfiles.8
139c80
+++ b/policycoreutils/setfiles/setfiles.8
139c80
@@ -19,6 +19,8 @@ setfiles \- set SELinux file security contexts.
139c80
 .RB [ \-W ]
139c80
 .RB [ \-F ]
139c80
 .RB [ \-I | \-D ]
139c80
+.RB [ \-T
139c80
+.IR nthreads ]
139c80
 .I spec_file
139c80
 .IR pathname \ ...
139c80
 
139c80
@@ -161,6 +163,13 @@ quote marks or backslashes.  The
139c80
 option of GNU
139c80
 .B find
139c80
 produces input suitable for this mode.
139c80
+.TP
139c80
+.BI \-T \ nthreads
139c80
+use up to
139c80
+.I nthreads
139c80
+threads.  Specify 0 to create as many threads as there are available
139c80
+CPU cores; 1 to use only a single thread (default); or any positive
139c80
+number to use the given number of threads (if possible).
139c80
 
139c80
 .SH "ARGUMENTS"
139c80
 .TP
139c80
diff --git a/policycoreutils/setfiles/setfiles.c b/policycoreutils/setfiles/setfiles.c
139c80
index f018d161aa9e..2313a21fa0f3 100644
139c80
--- a/policycoreutils/setfiles/setfiles.c
139c80
+++ b/policycoreutils/setfiles/setfiles.c
139c80
@@ -1,4 +1,5 @@
139c80
 #include "restore.h"
139c80
+#include <stdlib.h>
139c80
 #include <unistd.h>
139c80
 #include <fcntl.h>
139c80
 #include <stdio_ext.h>
139c80
@@ -34,14 +35,14 @@ static __attribute__((__noreturn__)) void usage(const char *const name)
139c80
 {
139c80
 	if (iamrestorecon) {
139c80
 		fprintf(stderr,
139c80
-			"usage:  %s [-iIDFmnprRv0x] [-e excludedir] pathname...\n"
139c80
-			"usage:  %s [-iIDFmnprRv0x] [-e excludedir] -f filename\n",
139c80
+			"usage:  %s [-iIDFmnprRv0xT] [-e excludedir] pathname...\n"
139c80
+			"usage:  %s [-iIDFmnprRv0xT] [-e excludedir] -f filename\n",
139c80
 			name, name);
139c80
 	} else {
139c80
 		fprintf(stderr,
139c80
-			"usage:  %s [-diIDlmnpqvEFW] [-e excludedir] [-r alt_root_path] [-c policyfile] spec_file pathname...\n"
139c80
-			"usage:  %s [-diIDlmnpqvEFW] [-e excludedir] [-r alt_root_path] [-c policyfile] spec_file -f filename\n"
139c80
-			"usage:  %s -s [-diIDlmnpqvFW] spec_file\n",
139c80
+			"usage:  %s [-diIDlmnpqvEFWT] [-e excludedir] [-r alt_root_path] [-c policyfile] spec_file pathname...\n"
139c80
+			"usage:  %s [-diIDlmnpqvEFWT] [-e excludedir] [-r alt_root_path] [-c policyfile] spec_file -f filename\n"
139c80
+			"usage:  %s -s [-diIDlmnpqvFWT] spec_file\n",
139c80
 			name, name, name);
139c80
 	}
139c80
 	exit(-1);
139c80
@@ -144,12 +145,12 @@ int main(int argc, char **argv)
139c80
 	int opt, i = 0;
139c80
 	const char *input_filename = NULL;
139c80
 	int use_input_file = 0;
139c80
-	char *buf = NULL;
139c80
-	size_t buf_len;
139c80
+	char *buf = NULL, *endptr;
139c80
+	size_t buf_len, nthreads = 1;
139c80
 	const char *base;
139c80
 	int errors = 0;
139c80
-	const char *ropts = "e:f:hiIDlmno:pqrsvFRW0x";
139c80
-	const char *sopts = "c:de:f:hiIDlmno:pqr:svEFR:W0";
139c80
+	const char *ropts = "e:f:hiIDlmno:pqrsvFRW0xT:";
139c80
+	const char *sopts = "c:de:f:hiIDlmno:pqr:svEFR:W0T:";
139c80
 	const char *opts;
139c80
 	union selinux_callback cb;
139c80
 
139c80
@@ -370,6 +371,11 @@ int main(int argc, char **argv)
139c80
 				usage(argv[0]);
139c80
                         }
139c80
                         break;
139c80
+		case 'T':
139c80
+			nthreads = strtoull(optarg, &endptr, 10);
139c80
+			if (*optarg == '\0' || *endptr != '\0')
139c80
+				usage(argv[0]);
139c80
+			break;
139c80
 		case 'h':
139c80
 		case '?':
139c80
 			usage(argv[0]);
139c80
@@ -448,13 +454,13 @@ int main(int argc, char **argv)
139c80
 			buf[len - 1] = 0;
139c80
 			if (!strcmp(buf, "/"))
139c80
 				r_opts.mass_relabel = SELINUX_RESTORECON_MASS_RELABEL;
139c80
-			errors |= process_glob(buf, &r_opts) < 0;
139c80
+			errors |= process_glob(buf, &r_opts, nthreads) < 0;
139c80
 		}
139c80
 		if (strcmp(input_filename, "-") != 0)
139c80
 			fclose(f);
139c80
 	} else {
139c80
 		for (i = optind; i < argc; i++)
139c80
-			errors |= process_glob(argv[i], &r_opts) < 0;
139c80
+			errors |= process_glob(argv[i], &r_opts, nthreads) < 0;
139c80
 	}
139c80
 
139c80
 	maybe_audit_mass_relabel(r_opts.mass_relabel, errors);
139c80
-- 
139c80
2.33.1
139c80