Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] exec: annotate lockless tsk->comm update with data_race()
@ 2026-11-24  8:38 Xiange Pi
  2026-11-25  8:50 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Xiange Pi @ 2026-11-24  8:38 UTC (permalink / raw)
  To: Kees Cook, Alexander Viro, Christian Brauner
  Cc: Jan Kara, linux-mm, linux-fsdevel, linux-kernel

KCSAN reports a data race between prctl(PR_SET_NAME) and a lockless
reader of tsk->comm such as the sched_stat_runtime tracepoint:

 BUG: KCSAN: data-race in __set_task_comm / strlen

 read to 0xffff88800d1787d8 of 1 bytes by task 3929 on cpu 1:
  strlen+0x19/0x40
  trace_event_raw_event_sched_stat_runtime+0x5a/0x150
  update_se+0x14f/0x3b0
  ...
 write to 0xffff88800d1787d8 of 10 bytes by task 3417 on cpu 0:
  __set_task_comm+0x78/0x180
  __se_sys_prctl+0xcb3/0x13b0

The race is benign by design: since commit 3a3f61ce5e0b ("exec: Make
sure task->comm is always NUL-terminated") the last byte of comm is
never overwritten with a non-NUL value, so lockless readers see at worst
a mix of old and new name. Annotate the writes in __set_task_comm()
with data_race() to tell KCSAN that the lockless access is intentional.
As nearly all updates of comm go through this function, this covers the
lockless readers without having to annotate each of them.

Reported-by: syzbot+98baf51f5cc36dcbe2f7@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=98baf51f5cc36dcbe2f7
Signed-off-by: Xiange Pi <xiange.pi@intel.com>
---
Test environment:
  - Baseline: torvalds/linux.git (git://git.kernel.org/pub/scm/linux/
    kernel/git/torvalds/linux.git) commit fc1c25014ff8, with and
    without this patch
  - Kernel config: the .config provided on the syzbot bug page
    (CONFIG_KCSAN=y)
  - Method: QEMU guest, running syz-execprog and syz-executor with the
    reproducer in the background, plus a script that enables the
    sched_stat_runtime tracepoint
  - Without the patch: "KCSAN: data-race in __set_task_comm / strlen"
    reproduced in every run
  - With the patch: not reproduced in 10 out of 10 runs

 fs/exec.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/fs/exec.c b/fs/exec.c
index a5269b5e00df..4b03bb2fcb95 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1100,8 +1100,16 @@ void __set_task_comm(struct task_struct *tsk, const char *buf, bool exec)
 	size_t len = strnlen(buf, sizeof(tsk->comm) - 1);
 
 	trace_task_rename(tsk, buf);
-	memcpy(tsk->comm, buf, len);
-	memset(&tsk->comm[len], 0, sizeof(tsk->comm) - len);
+	/*
+	 * tsk->comm is read locklessly by many users (e.g. tracepoints,
+	 * printk). comm[TASK_COMM_LEN - 1] is never written with a non-NUL
+	 * byte, so concurrent readers see a bounded, NUL-terminated string;
+	 * at worst a mix of the old and new name. Mark the update as
+	 * intentionally racy.
+	 */
+	data_race(memcpy(tsk->comm, buf, len));
+	/* Same as above: lockless readers tolerate a partially updated comm. */
+	data_race(memset(&tsk->comm[len], 0, sizeof(tsk->comm) - len));
 	perf_event_comm(tsk, exec);
 }
 
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] exec: annotate lockless tsk->comm update with data_race()
  2026-11-24  8:38 [PATCH] exec: annotate lockless tsk->comm update with data_race() Xiange Pi
@ 2026-11-25  8:50 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-25  8:50 UTC (permalink / raw)
  To: Xiange Pi, Kees Cook, Alexander Viro, Christian Brauner
  Cc: Jan Kara, linux-mm

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] exec: data_race() in __set_task_comm() still triggers KCSAN on
  readers
--

commit a04284363b9bce90bc6367728fc880cc5b2fd84a
Author: Xiange Pi <xiange.pi@intel.com>

exec: annotate lockless tsk->comm update with data_race()

This commit wraps the memcpy() and memset() calls in __set_task_comm()
with data_race() to suppress KCSAN warnings on lockless updates to
tsk->comm.

> diff --git a/fs/exec.c b/fs/exec.c
> index 8d2b14e1d455c..9a47cb27ce5d0 100644
> --- a/fs/exec.c
> +++ b/fs/exec.c
> @@ -1100,8 +1100,16 @@ void __set_task_comm(struct task_struct *tsk, const char *buf, bool exec)
>  	size_t len = strnlen(buf, sizeof(tsk->comm) - 1);
>  
>  	trace_task_rename(tsk, buf);
> -	memcpy(tsk->comm, buf, len);
> -	memset(&tsk->comm[len], 0, sizeof(tsk->comm) - len);
> +	/*
> +	 * tsk->comm is read locklessly by many users (e.g. tracepoints,
> +	 * printk). comm[TASK_COMM_LEN - 1] is never written with a non-NUL
> +	 * byte, so concurrent readers see a bounded, NUL-terminated string;
> +	 * at worst a mix of the old and new name. Mark the update as
> +	 * intentionally racy.
> +	 */
> +	data_race(memcpy(tsk->comm, buf, len));
> +	/* Same as above: lockless readers tolerate a partially updated comm. */
> +	data_race(memset(&tsk->comm[len], 0, sizeof(tsk->comm) - len));

[Severity: Low]
Does annotating only the writer in __set_task_comm() actually suppress
KCSAN reports for unannotated lockless readers of tsk->comm?

data_race() disables KCSAN only on the writer thread via
__kcsan_disable_current().  Unannotated readers such as strlen() in
tracepoints or get_task_comm() still set up read watchpoints on tsk->comm
in kcsan_setup_watchpoint().  When a concurrent __set_task_comm() modifies
a watched byte to a different value, kcsan_found_watchpoint() on the
writer returns early without consuming the watchpoint, and the reader then
reports an unknown-origin data race under the default
CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN=y:

kernel/kcsan/core.c:kcsan_found_watchpoint() {
	if (!kcsan_is_enabled(ctx))
		return;
	...
	/*
	 * If the other thread does not want to ignore the access, and there was
	 * a value change as a result of this thread's operation, we will still
	 * generate a report of unknown origin.
	 *
	 * Use CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN=n to filter.
	 */
}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124083848.1430527-1-xiange.pi@intel.com?part=1


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-25  8:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24  8:38 [PATCH] exec: annotate lockless tsk->comm update with data_race() Xiange Pi
2026-11-25  8:50 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox