setfiles/restorecon: support parallel relabeling

Use the newly introduced selinux_restorecon_parallel(3) in
setfiles/restorecon and a -T option to both to allow enabling parallel
relabeling. The default behavior without specifying the -T option is to
use 1 thread; parallel relabeling must be requested explicitly by
passing -T 0 (which will use as many threads as there are available CPU
cores) or -T <N>, which will use <N> threads.

=== Benchmarks ===
As measured on a 32-core cloud VM with Fedora 34. Not a fully
representative environment, but still the scaling is quite good.

WITHOUT PATCHES:
$ time restorecon -rn /usr

real    0m21.689s
user    0m21.070s
sys     0m0.494s

WITH PATCHES:
$ time restorecon -rn /usr

real    0m23.940s
user    0m23.127s
sys     0m0.653s
$ time restorecon -rn -T 2 /usr

real    0m13.145s
user    0m25.306s
sys     0m0.695s
$ time restorecon -rn -T 4 /usr

real    0m7.559s
user    0m28.470s
sys     0m1.099s
$ time restorecon -rn -T 8 /usr

real    0m5.186s
user    0m37.450s
sys     0m2.094s
$ time restorecon -rn -T 16 /usr

real    0m3.831s
user    0m51.220s
sys     0m4.895s
$ time restorecon -rn -T 32 /usr

real    0m2.650s
user    1m5.136s
sys     0m6.614s

Note that the benchmarks were performed in read-only mode (-n), so the
labels were only read and looked up in the database, not written. When
fixing labels on a heavily mislabeled system, the scaling would likely
be event better, since a larger % of work could be done in parallel.

Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
This commit is contained in:
Ondrej Mosnacek 2021-10-26 13:52:39 +02:00 committed by Petr Lautrbach
parent 02f302fc5b
commit 93902fc834
6 changed files with 41 additions and 16 deletions

View file

@ -6,7 +6,7 @@ MANDIR = $(PREFIX)/share/man
AUDITH ?= $(shell test -f /usr/include/libaudit.h && echo y) AUDITH ?= $(shell test -f /usr/include/libaudit.h && echo y)
CFLAGS ?= -g -Werror -Wall -W CFLAGS ?= -g -Werror -Wall -W
override LDLIBS += -lselinux -lsepol override LDLIBS += -lselinux -lsepol -lpthread
ifeq ($(AUDITH), y) ifeq ($(AUDITH), y)
override CFLAGS += -DUSE_AUDIT override CFLAGS += -DUSE_AUDIT

View file

@ -72,7 +72,7 @@ void restore_finish(void)
} }
} }
int process_glob(char *name, struct restore_opts *opts) int process_glob(char *name, struct restore_opts *opts, size_t nthreads)
{ {
glob_t globbuf; glob_t globbuf;
size_t i = 0; size_t i = 0;
@ -91,8 +91,9 @@ int process_glob(char *name, struct restore_opts *opts)
continue; continue;
if (len > 0 && strcmp(&globbuf.gl_pathv[i][len], "/..") == 0) if (len > 0 && strcmp(&globbuf.gl_pathv[i][len], "/..") == 0)
continue; continue;
rc = selinux_restorecon(globbuf.gl_pathv[i], rc = selinux_restorecon_parallel(globbuf.gl_pathv[i],
opts->restorecon_flags); opts->restorecon_flags,
nthreads);
if (rc < 0) if (rc < 0)
errors = rc; errors = rc;
} }

View file

@ -49,7 +49,7 @@ struct restore_opts {
void restore_init(struct restore_opts *opts); void restore_init(struct restore_opts *opts);
void restore_finish(void); void restore_finish(void);
void add_exclude(const char *directory); void add_exclude(const char *directory);
int process_glob(char *name, struct restore_opts *opts); int process_glob(char *name, struct restore_opts *opts, size_t nthreads);
extern char **exclude_list; extern char **exclude_list;
#endif #endif

View file

@ -33,6 +33,8 @@ restorecon \- restore file(s) default SELinux security contexts.
.RB [ \-W ] .RB [ \-W ]
.RB [ \-I | \-D ] .RB [ \-I | \-D ]
.RB [ \-x ] .RB [ \-x ]
.RB [ \-T
.IR nthreads ]
.SH "DESCRIPTION" .SH "DESCRIPTION"
This manual page describes the This manual page describes the
@ -160,6 +162,13 @@ prevent
.B restorecon .B restorecon
from crossing file system boundaries. from crossing file system boundaries.
.TP .TP
.BI \-T \ nthreads
use up to
.I nthreads
threads. Specify 0 to create as many threads as there are available
CPU cores; 1 to use only a single thread (default); or any positive
number to use the given number of threads (if possible).
.TP
.SH "ARGUMENTS" .SH "ARGUMENTS"
.IR pathname \ ... .IR pathname \ ...
The pathname for the file(s) to be relabeled. The pathname for the file(s) to be relabeled.

View file

@ -19,6 +19,8 @@ setfiles \- set SELinux file security contexts.
.RB [ \-W ] .RB [ \-W ]
.RB [ \-F ] .RB [ \-F ]
.RB [ \-I | \-D ] .RB [ \-I | \-D ]
.RB [ \-T
.IR nthreads ]
.I spec_file .I spec_file
.IR pathname \ ... .IR pathname \ ...
@ -161,6 +163,13 @@ quote marks or backslashes. The
option of GNU option of GNU
.B find .B find
produces input suitable for this mode. produces input suitable for this mode.
.TP
.BI \-T \ nthreads
use up to
.I nthreads
threads. Specify 0 to create as many threads as there are available
CPU cores; 1 to use only a single thread (default); or any positive
number to use the given number of threads (if possible).
.SH "ARGUMENTS" .SH "ARGUMENTS"
.TP .TP

View file

@ -1,4 +1,5 @@
#include "restore.h" #include "restore.h"
#include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio_ext.h> #include <stdio_ext.h>
@ -34,14 +35,14 @@ static __attribute__((__noreturn__)) void usage(const char *const name)
{ {
if (iamrestorecon) { if (iamrestorecon) {
fprintf(stderr, fprintf(stderr,
"usage: %s [-iIDFmnprRv0x] [-e excludedir] pathname...\n" "usage: %s [-iIDFmnprRv0xT] [-e excludedir] pathname...\n"
"usage: %s [-iIDFmnprRv0x] [-e excludedir] -f filename\n", "usage: %s [-iIDFmnprRv0xT] [-e excludedir] -f filename\n",
name, name); name, name);
} else { } else {
fprintf(stderr, fprintf(stderr,
"usage: %s [-diIDlmnpqvEFW] [-e excludedir] [-r alt_root_path] [-c policyfile] spec_file pathname...\n" "usage: %s [-diIDlmnpqvEFWT] [-e excludedir] [-r alt_root_path] [-c policyfile] spec_file pathname...\n"
"usage: %s [-diIDlmnpqvEFW] [-e excludedir] [-r alt_root_path] [-c policyfile] spec_file -f filename\n" "usage: %s [-diIDlmnpqvEFWT] [-e excludedir] [-r alt_root_path] [-c policyfile] spec_file -f filename\n"
"usage: %s -s [-diIDlmnpqvFW] spec_file\n", "usage: %s -s [-diIDlmnpqvFWT] spec_file\n",
name, name, name); name, name, name);
} }
exit(-1); exit(-1);
@ -144,12 +145,12 @@ int main(int argc, char **argv)
int opt, i = 0; int opt, i = 0;
const char *input_filename = NULL; const char *input_filename = NULL;
int use_input_file = 0; int use_input_file = 0;
char *buf = NULL; char *buf = NULL, *endptr;
size_t buf_len; size_t buf_len, nthreads = 1;
const char *base; const char *base;
int errors = 0; int errors = 0;
const char *ropts = "e:f:hiIDlmno:pqrsvFRW0x"; const char *ropts = "e:f:hiIDlmno:pqrsvFRW0xT:";
const char *sopts = "c:de:f:hiIDlmno:pqr:svEFR:W0"; const char *sopts = "c:de:f:hiIDlmno:pqr:svEFR:W0T:";
const char *opts; const char *opts;
union selinux_callback cb; union selinux_callback cb;
@ -370,6 +371,11 @@ int main(int argc, char **argv)
usage(argv[0]); usage(argv[0]);
} }
break; break;
case 'T':
nthreads = strtoull(optarg, &endptr, 10);
if (*optarg == '\0' || *endptr != '\0')
usage(argv[0]);
break;
case 'h': case 'h':
case '?': case '?':
usage(argv[0]); usage(argv[0]);
@ -448,13 +454,13 @@ int main(int argc, char **argv)
buf[len - 1] = 0; buf[len - 1] = 0;
if (!strcmp(buf, "/")) if (!strcmp(buf, "/"))
r_opts.mass_relabel = SELINUX_RESTORECON_MASS_RELABEL; r_opts.mass_relabel = SELINUX_RESTORECON_MASS_RELABEL;
errors |= process_glob(buf, &r_opts) < 0; errors |= process_glob(buf, &r_opts, nthreads) < 0;
} }
if (strcmp(input_filename, "-") != 0) if (strcmp(input_filename, "-") != 0)
fclose(f); fclose(f);
} else { } else {
for (i = optind; i < argc; i++) for (i = optind; i < argc; i++)
errors |= process_glob(argv[i], &r_opts) < 0; errors |= process_glob(argv[i], &r_opts, nthreads) < 0;
} }
maybe_audit_mass_relabel(r_opts.mass_relabel, errors); maybe_audit_mass_relabel(r_opts.mass_relabel, errors);