Linux block layer
 help / color / mirror / Atom feed
From: Nikolai Dokutovich <ndokutovich@gmail.com>
To: Jens Axboe <axboe@kernel.dk>
Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org,
	Nikolai Dokutovich <ndokutovich@gmail.com>
Subject: [PATCH] block: use READ_ONCE() for the PF_EXITING check in set_task_ioprio()
Date: Sat, 28 Nov 2026 17:06:42 +0300	[thread overview]
Message-ID: <20261128140642.57-1-ndokutovich@gmail.com> (raw)

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


                 reply	other threads:[~2026-11-28 14:06 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=20261128140642.57-1-ndokutovich@gmail.com \
    --to=ndokutovich@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.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