libselinux: reset scanned file count at selinux_restorecon(3) entry
PATCH v2 2/2 libselinux: reset scanned file count at selinux_restorecon(3) entry
From: Christian Göttsche <[email protected]>
Reset the scanned file count at startup in case the calling process calls selinux_restorecon(3) multiple times during its lifetime.
--- libselinux/src/selinux_restorecon.c | 2 ++ 1 file changed, 2 insertions(+)
libselinux/src/selinux_restorecon.c +2 -0
@@ -1446,6 +1446,8 @@ static int selinux_restorecon_common(const char *pathname_orig,int error;struct dir_hash_node *current =NULL ; + fc_count =0 ; +if (state.flags.verbose && state.flags.progress) state.flags.verbose = false;
Signature · 1 line
2.53.0
PATCH v2 1/2 libselinux: improve restorecon progress locking
From: Christian Göttsche <[email protected]>
Use an atomic counter to avoid acquiring the synchronizing mutex for each file. Only acquire when actually writing to stdout.
---
v2: extract fc_count reset into separate commit
--- libselinux/src/selinux_restorecon.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)
libselinux/src/selinux_restorecon.c +11 -9
@@ -40,8 +40,6 @@#include "label_file.h" #include "sha1.h" -#define STAR_COUNT 1024 -static struct selabel_handle *fc_sehandle =NULL ;static bool selabel_no_digest;static char *rootpath =NULL ;
@@ -734,19 +732,23 @@ static int restorecon_sb(int fd, const char *pathname, const struct stat *sb, }if (flags->progress) { -__pthread_mutex_lock (&progress_mutex); - fc_count++; -if (fc_count %STAR_COUNT ==0 ) { +const unsigned STAR_COUNT =1024 ; +uint64_t fc_count_local; + + fc_count_local =__atomic_add_fetch (&fc_count,1 , __ATOMIC_RELAXED); + +if (fc_count_local %STAR_COUNT ==0 ) { +__pthread_mutex_lock (&progress_mutex);if (flags->mass_relabel && efile_count >0 ) { -float pc = (fc_count < efile_count) ? (100.0 * - fc_count / efile_count) :100 ; +float pc = (fc_count_local < efile_count) ? (100.0 * + fc_count_local / efile_count) :100 ;fprintf (stdout,"\r%-.1f%%" , (double )pc); }else { -fprintf (stdout,"\r%" PRIu64"k" , fc_count /STAR_COUNT ); +fprintf (stdout,"\r%" PRIu64"k" , fc_count_local /STAR_COUNT ); }fflush (stdout); +__pthread_mutex_unlock (&progress_mutex); } -__pthread_mutex_unlock (&progress_mutex); }if (flags->add_assoc) {
Signature · 1 line
2.53.0
Stephen Smalley
maintainerRe: [PATCH v2 1/2] libselinux: improve restorecon progress locking
Quoted 31 lines · Christian Göttsche
From: Christian Göttsche <[email protected]>
Use an atomic counter to avoid acquiring the synchronizing mutex for each file. Only acquire when actually writing to stdout.
Suggested-by CopilotSigned-off-by Christian Göttsche <[email protected]>---
v2: extractfc_countreset into separate commit--- libselinux/src/selinux_restorecon.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)libselinux/src/selinux_restorecon.c +0
-2@@ -40,8 +40,6 @@#include "label_file.h" #include "sha1.h" -#define STAR_COUNT 1024 -static struct selabel_handle *fc_sehandle =NULL ;static bool selabel_no_digest;static char *rootpath =NULL ;@@ -734,19 +732,23 @@ static int restorecon_sb(int fd, const char *pathname, const struct stat *sb, }if (flags->progress) {
libselinux/src/selinux_restorecon.c static int restorecon_sb(int fd, const char *pathname, const struct stat *sb, · line 741
Quoted 10 lines
libselinux/src/selinux_restorecon.c +7
-3-__pthread_mutex_lock (&progress_mutex); - fc_count++; -if (fc_count %STAR_COUNT ==0 ) { +const unsigned STAR_COUNT =1024 ; +uint64_t fc_count_local; + + fc_count_local =__atomic_add_fetch (&fc_count,1 , __ATOMIC_RELAXED); + +if (fc_count_local %STAR_COUNT ==0 ) { +__pthread_mutex_lock (&progress_mutex);
Do we actually need to take a mutex at all here?
Quoted 21 lines
libselinux/src/selinux_restorecon.c +4
-5if (flags->mass_relabel && efile_count >0 ) { -float pc = (fc_count < efile_count) ? (100.0 * - fc_count / efile_count) :100 ; +float pc = (fc_count_local < efile_count) ? (100.0 * + fc_count_local / efile_count) :100 ;fprintf (stdout,"\r%-.1f%%" , (double )pc); }else { -fprintf (stdout,"\r%" PRIu64"k" , fc_count /STAR_COUNT ); +fprintf (stdout,"\r%" PRIu64"k" , fc_count_local /STAR_COUNT ); }fflush (stdout); +__pthread_mutex_unlock (&progress_mutex); } -__pthread_mutex_unlock (&progress_mutex); }if (flags->add_assoc) { --2.53.0
Re: [PATCH v2 1/2] libselinux: improve restorecon progress locking
Quoted 33 lines · Stephen Smalley
Quoted 30 lines · Christian Göttsche
From: Christian Göttsche <[email protected]>
Use an atomic counter to avoid acquiring the synchronizing mutex for each file. Only acquire when actually writing to stdout.
Suggested-by CopilotSigned-off-by Christian Göttsche <[email protected]>---
v2: extractfc_countreset into separate commit--- libselinux/src/selinux_restorecon.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)libselinux/src/selinux_restorecon.c +0
-2@@ -40,8 +40,6 @@#include "label_file.h" #include "sha1.h" -#define STAR_COUNT 1024 -static struct selabel_handle *fc_sehandle =NULL ;static bool selabel_no_digest;static char *rootpath =NULL ;@@ -734,19 +732,23 @@ static int restorecon_sb(int fd, const char *pathname, const struct stat *sb, }
Quoted 13 lines
libselinux/src/selinux_restorecon.c static int restorecon_sb(int fd, const char *pathname, const struct stat *sb, · line 741
Quoted 11 lines
libselinux/src/selinux_restorecon.c +7
-3if (flags->progress) { -__pthread_mutex_lock (&progress_mutex); - fc_count++; -if (fc_count %STAR_COUNT ==0 ) { +const unsigned STAR_COUNT =1024 ; +uint64_t fc_count_local; + + fc_count_local =__atomic_add_fetch (&fc_count,1 , __ATOMIC_RELAXED); + +if (fc_count_local %STAR_COUNT ==0 ) { +__pthread_mutex_lock (&progress_mutex);Do we actually need to take a mutex at all here?
writing to stdout concurrently can interleave output, which might break the line reset via \r
Quoted 22 lines
Quoted 22 lines
libselinux/src/selinux_restorecon.c +4
-5if (flags->mass_relabel && efile_count >0 ) { -float pc = (fc_count < efile_count) ? (100.0 * - fc_count / efile_count) :100 ; +float pc = (fc_count_local < efile_count) ? (100.0 * + fc_count_local / efile_count) :100 ;fprintf (stdout,"\r%-.1f%%" , (double )pc); }else { -fprintf (stdout,"\r%" PRIu64"k" , fc_count /STAR_COUNT ); +fprintf (stdout,"\r%" PRIu64"k" , fc_count_local /STAR_COUNT ); }fflush (stdout); +__pthread_mutex_unlock (&progress_mutex); } -__pthread_mutex_unlock (&progress_mutex); }if (flags->add_assoc) { --2.53.0
Re: [PATCH v2 1/2] libselinux: improve restorecon progress locking
In reply to Christian GöttscheQuoted 8 lines · Christian Göttsche
From: Christian Göttsche <[email protected]>
Use an atomic counter to avoid acquiring the synchronizing mutex for each file. Only acquire when actually writing to stdout.
Suggested-by CopilotSigned-off-by Christian Göttsche <[email protected]>
Quoted 54 lines
---
v2: extractfc_countreset into separate commit--- libselinux/src/selinux_restorecon.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-)libselinux/src/selinux_restorecon.c +11
-10@@ -40,8 +40,6 @@#include "label_file.h" #include "sha1.h" -#define STAR_COUNT 1024 -static struct selabel_handle *fc_sehandle =NULL ;static bool selabel_no_digest;static char *rootpath =NULL ;@@ -734,19 +732,23 @@ static int restorecon_sb(int fd, const char *pathname, const struct stat *sb, }if (flags->progress) { -__pthread_mutex_lock (&progress_mutex); - fc_count++; -if (fc_count %STAR_COUNT ==0 ) { +const unsigned STAR_COUNT =1024 ; +uint64_t fc_count_local; + + fc_count_local =__atomic_add_fetch (&fc_count,1 , __ATOMIC_RELAXED); + +if (fc_count_local %STAR_COUNT ==0 ) { +__pthread_mutex_lock (&progress_mutex);if (flags->mass_relabel && efile_count >0 ) { -float pc = (fc_count < efile_count) ? (100.0 * - fc_count / efile_count) :100 ; +float pc = (fc_count_local < efile_count) ? (100.0 * + fc_count_local / efile_count) :100 ;fprintf (stdout,"\r%-.1f%%" , (double )pc); }else { -fprintf (stdout,"\r%" PRIu64"k" , fc_count /STAR_COUNT ); +fprintf (stdout,"\r%" PRIu64"k" , fc_count_local /STAR_COUNT ); }fflush (stdout); +__pthread_mutex_unlock (&progress_mutex); } -__pthread_mutex_unlock (&progress_mutex); }if (flags->add_assoc) { --2.53.0
Stephen Smalley
maintainerRe: [PATCH v2 1/2] libselinux: improve restorecon progress locking
Quoted 6 lines · Stephen Smalley
Quoted 3 lines · Christian Göttsche
From: Christian Göttsche <[email protected]>
Quoted 7 lines
Quoted 5 lines
Use an atomic counter to avoid acquiring the synchronizing mutex for each file. Only acquire when actually writing to stdout.
Suggested-by CopilotSigned-off-by Christian Göttsche <[email protected]>Acked-by Stephen Smalley <[email protected]>
Merged.
Stephen Smalley
maintainerQuoted 16 lines · Christian Göttsche
From: Christian Göttsche <[email protected]>
Reset the scanned file count at startup in case the calling process calls
selinux_restorecon(3)multiple times during its lifetime.Signed-off-by Christian Göttsche <[email protected]>--- libselinux/src/selinux_restorecon.c | 2 ++ 1 file changed, 2 insertions(+)libselinux/src/selinux_restorecon.c +0
-0
libselinux/src/selinux_restorecon.c static int selinux_restorecon_common(const char *pathname_orig, · line 1450
Quoted 5 lines
libselinux/src/selinux_restorecon.c +2
-0@@ -1446,6 +1446,8 @@ static int selinux_restorecon_common(const char *pathname_orig,int error;struct dir_hash_node *current =NULL ; + fc_count =0 ; +
Should we use an atomic operation for clearing fc_count?
Quoted 7 lines
libselinux/src/selinux_restorecon.c +0
-1if (state.flags.verbose && state.flags.progress) state.flags.verbose = false; --2.53.0
Quoted 22 lines · Stephen Smalley
Quoted 19 lines · Christian Göttsche
From: Christian Göttsche <[email protected]>
Reset the scanned file count at startup in case the calling process calls
selinux_restorecon(3)multiple times during its lifetime.Signed-off-by Christian Göttsche <[email protected]>--- libselinux/src/selinux_restorecon.c | 2 ++ 1 file changed, 2 insertions(+)libselinux/src/selinux_restorecon.c +0
-0@@ -1446,6 +1446,8 @@ static int selinux_restorecon_common(const char *pathname_orig,int error;struct dir_hash_node *current =NULL ;
Quoted 4 lines
libselinux/src/selinux_restorecon.c static int selinux_restorecon_common(const char *pathname_orig, · line 1450
Quoted 2 lines
libselinux/src/selinux_restorecon.c +2
-0+ fc_count =0 ; +Should we use an atomic operation for clearing
fc_count?
I think calling selinux_restorecon(3) concurrently from multiple threads is not fully thread-safe yet, for example the assignments to skipped_errors and relabeled_files. selinux_restorecon_xattr(3) is completely unsafe due the unsynchronized usage of dir_xattr_list and dir_xattr_last. At least for selinux_restorecon(3) I think it might be not too complicated to support concurrent usage, then the reset should be atomic, yes.
Quoted 8 lines
Quoted 8 lines
libselinux/src/selinux_restorecon.c +0
-1if (state.flags.verbose && state.flags.progress) state.flags.verbose = false; --2.53.0
Quoted 7 lines · Christian Göttsche
From: Christian Göttsche <[email protected]>
Reset the scanned file count at startup in case the calling process calls
selinux_restorecon(3)multiple times during its lifetime.Signed-off-by Christian Göttsche <[email protected]>
Quoted 21 lines
--- libselinux/src/selinux_restorecon.c | 2 ++ 1 file changed, 2 insertions(+)libselinux/src/selinux_restorecon.c +2
-1@@ -1446,6 +1446,8 @@ static int selinux_restorecon_common(const char *pathname_orig,int error;struct dir_hash_node *current =NULL ; + fc_count =0 ; +if (state.flags.verbose && state.flags.progress) state.flags.verbose = false; --2.53.0
Stephen Smalley
maintainerQuoted 4 lines · Stephen Smalley
Quoted 1 line · Christian Göttsche
Quoted 8 lines
Quoted 6 lines
From: Christian Göttsche <[email protected]>
Reset the scanned file count at startup in case the calling process calls
selinux_restorecon(3)multiple times during its lifetime.Signed-off-by Christian Göttsche <[email protected]>Acked-by Stephen Smalley <[email protected]>
Merged.
Quoted 22 lines
Quoted 22 lines
--- libselinux/src/selinux_restorecon.c | 2 ++ 1 file changed, 2 insertions(+)libselinux/src/selinux_restorecon.c +2
-1@@ -1446,6 +1446,8 @@ static int selinux_restorecon_common(const char *pathname_orig,int error;struct dir_hash_node *current =NULL ; + fc_count =0 ; +if (state.flags.verbose && state.flags.progress) state.flags.verbose = false; --2.53.0