Linux block layer
 help / color / mirror / Atom feed
* [PATCH] block: use READ_ONCE() for the PF_EXITING check in set_task_ioprio()
@ 2026-11-28 14:06 Nikolai Dokutovich
  0 siblings, 0 replies; only message in thread
From: Nikolai Dokutovich @ 2026-11-28 14:06 UTC (permalink / raw)
  To: Jens Axboe; +Cc: linux-block, linux-kernel, Nikolai Dokutovich

set_task_ioprio() can be called on a running kernel thread: ext4 calls
it on the journal thread from ext4_fill_super().  kjournald2() records
journal->j_task and wakes the mount path first, and only then calls
memalloc_nofs_save(), which does

	current->flags |= PF_MEMALLOC_NOFS

without any lock, while set_task_ioprio() reads task->flags under
task_lock(task):

  data-race in kjournald2 / set_task_ioprio

  read to ... of 4 bytes by task 103 on cpu 0:
   set_task_ioprio+0x17d/0x210
   ext4_fill_super+0x4954/0x5600
   get_tree_bdev_flags+0x218/0x320
   ...
  write to ... of 4 bytes by task 251 on cpu 1:
   kjournald2+0xbc/0x3e0
   kthread+0x1db/0x220

task_lock() does not order task->flags against the owner updating its
own flags; here it protects the io_context handoff against
exit_io_context(), which runs after PF_EXITING is set, and PF_EXITING
itself is set under sighand->siglock.  Mark the check as the snapshot
it is.  This adds no synchronization.

The racing write sets PF_MEMALLOC_NOFS, which this check does not read,
and flags is a naturally aligned unsigned int, so the snapshot cannot
tear and the observed race does not change the outcome; the check only
decides whether to allocate an io_context for a task that could be
exiting.

Signed-off-by: Nikolai Dokutovich <ndokutovich@gmail.com>
---
 block/blk-ioc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/block/blk-ioc.c b/block/blk-ioc.c
index d15918d7fabb..a3662e709bd9 100644
--- a/block/blk-ioc.c
+++ b/block/blk-ioc.c
@@ -270,7 +270,7 @@ int set_task_ioprio(struct task_struct *task, int ioprio)
 			return -ENOMEM;
 
 		task_lock(task);
-		if (task->flags & PF_EXITING) {
+		if (READ_ONCE(task->flags) & PF_EXITING) {
 			kmem_cache_free(iocontext_cachep, ioc);
 			goto out;
 		}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-28 14:06 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28 14:06 [PATCH] block: use READ_ONCE() for the PF_EXITING check in set_task_ioprio() Nikolai Dokutovich

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