libselinux: improve restorecon progress locking
PATCH v2 1/1 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.
Also reset the scanned file count at startup in case the calling process calls selinux_restorecon(3) multiple times during its lifetime.
---
v2: use GCC builtin __atomic_add_fetch() instead of C11 atomics for musl support
--- libselinux/src/selinux_restorecon.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-)
libselinux/src/selinux_restorecon.c +13 -9
@@ -39,8 +39,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 ;
@@ -694,19 +692,23 @@ static int restorecon_sb(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) {
@@ -1118,6 +1120,8 @@ static int selinux_restorecon_common(const char *pathname_orig,int fts_flags, 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.51.0
Quoted 11 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.
Also reset the scanned file count at startup in case the calling process calls
selinux_restorecon(3)multiple times during its lifetime.Suggested-by CopilotSigned-off-by Christian Göttsche <[email protected]>
Kindly ping.
Quoted 63 lines
---
v2: use GCC builtin__atomic_add_fetch()instead of C11 atomics for musl support--- libselinux/src/selinux_restorecon.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-)libselinux/src/selinux_restorecon.c +13
-10@@ -39,8 +39,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 ;@@ -694,19 +692,23 @@ static int restorecon_sb(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) {@@ -1118,6 +1120,8 @@ static int selinux_restorecon_common(const char *pathname_orig,int fts_flags, error;struct dir_hash_node *current =NULL ; + fc_count =0 ; +if (state.flags.verbose && state.flags.progress) state.flags.verbose = false; --2.51.0
Stephen Smalley
maintainerQuoted 9 lines · Christian Göttsche
Quoted 6 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.
Quoted 7 lines
Quoted 5 lines
Also reset the scanned file count at startup in case the calling process calls
selinux_restorecon(3)multiple times during its lifetime.Suggested-by CopilotSigned-off-by Christian Göttsche <[email protected]>Kindly ping.
Can you split into two separate patches, one for the progress locking improvement and one for resetting the count?
Quoted 65 lines
Quoted 64 lines
---
v2: use GCC builtin__atomic_add_fetch()instead of C11 atomics for musl support--- libselinux/src/selinux_restorecon.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-)libselinux/src/selinux_restorecon.c +13
-10@@ -39,8 +39,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 ;@@ -694,19 +692,23 @@ static int restorecon_sb(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) {@@ -1118,6 +1120,8 @@ static int selinux_restorecon_common(const char *pathname_orig,int fts_flags, error;struct dir_hash_node *current =NULL ; + fc_count =0 ; +if (state.flags.verbose && state.flags.progress) state.flags.verbose = false; --2.51.0