public inbox for linux-erofs@ozlabs.org
 help / color / mirror / Atom feed
From: Utkal Singh <singhutkal015@gmail.com>
To: linux-erofs@lists.ozlabs.org
Cc: hsiangkao@linux.alibaba.com, chao@kernel.org, singhutkal015@gmail.com
Subject: [PATCH] erofs-utils: lib: fix data race on __erofs_is_progressmsg
Date: Sun, 29 Mar 2026 13:23:06 +0000	[thread overview]
Message-ID: <20260329132306.15353-1-singhutkal015@gmail.com> (raw)

erofs_msg() and erofs_update_progressinfo() both access the static
variable __erofs_is_progressmsg without any synchronization. Since
mkfs already runs multiple worker threads via erofs_alloc_workqueue()
(e.g. in z_erofs_mt_compress()), and those threads call erofs_err()
and erofs_info() which invoke erofs_msg(), this is a real data race
on the current codebase.

Fix this by protecting all accesses to __erofs_is_progressmsg with a
static pthread_mutex_t. The mutex is statically initialized so no
init/destroy changes are needed. Also restructure the
erofs_update_progressinfo() I/O path to eliminate the early return
inside the lock, ensuring pthread_mutex_unlock() is always reached.

Signed-off-by: Utkal Singh <singhutkal015@gmail.com>
---
 lib/config.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/lib/config.c b/lib/config.c
index ab7eb01..7f58159 100644
--- a/lib/config.c
+++ b/lib/config.c
@@ -100,6 +100,7 @@ int erofs_selabel_open(const char *file_contexts)
 #endif
 
 static bool __erofs_is_progressmsg;
+static pthread_mutex_t erofs_msg_lock = PTHREAD_MUTEX_INITIALIZER;
 
 char *erofs_trim_for_progressinfo(const char *str, int placeholder)
 {
@@ -141,6 +142,7 @@ void erofs_msg(int dbglv, const char *fmt, ...)
 	va_list ap;
 	FILE *f = dbglv >= EROFS_ERR ? stderr : stdout;
 
+	pthread_mutex_lock(&erofs_msg_lock);
 	if (__erofs_is_progressmsg) {
 		fputc('\n', stdout);
 		__erofs_is_progressmsg = false;
@@ -148,6 +150,7 @@ void erofs_msg(int dbglv, const char *fmt, ...)
 	va_start(ap, fmt);
 	vfprintf(f, fmt, ap);
 	va_end(ap);
+	pthread_mutex_unlock(&erofs_msg_lock);
 }
 
 void erofs_update_progressinfo(const char *fmt, ...)
@@ -162,14 +165,16 @@ void erofs_update_progressinfo(const char *fmt, ...)
 	vsprintf(msg, fmt, ap);
 	va_end(ap);
 
+	pthread_mutex_lock(&erofs_msg_lock);
 	if (erofs_stdout_tty) {
 		printf("\r\033[K%s", msg);
 		__erofs_is_progressmsg = true;
 		fflush(stdout);
-		return;
+	} else {
+		fputs(msg, stdout);
+		fputc('\n', stdout);
 	}
-	fputs(msg, stdout);
-	fputc('\n', stdout);
+	pthread_mutex_unlock(&erofs_msg_lock);
 }
 
 unsigned int erofs_get_available_processors(void)
-- 
2.43.0



                 reply	other threads:[~2026-03-29 13:23 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260329132306.15353-1-singhutkal015@gmail.com \
    --to=singhutkal015@gmail.com \
    --cc=chao@kernel.org \
    --cc=hsiangkao@linux.alibaba.com \
    --cc=linux-erofs@lists.ozlabs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox