__get_task_ioprio() reads task->io_context and ioc->ioprio without task_lock() when called on current, e.g. from init_sync_kiocb() on every write. set_task_ioprio() can update both for another task at any time, including the first publication of a new io_context. KCSAN reports it: BUG: KCSAN: data-race in __kernel_write_iter / set_task_ioprio value changed: 0x0000000000000000 -> 0xffff888109813730 A stale or torn value is harmless, a task simply gets the old or new priority, but the compiler may tear or refetch the accesses. The io_context itself needs no extra protection: only the owning task drops it, in exit_io_context(), so it cannot be freed under a lockless reader running as current. Read the pointer and ->ioprio with READ_ONCE() and write ->ioprio with WRITE_ONCE(). Publish a new io_context with smp_store_release() so that its initialization is visible before the pointer. The reader only dereferences the pointer it loaded, so the address dependency of READ_ONCE() is enough to pair with the release, as with rcu_assign_pointer() and rcu_dereference(); an acquire is not needed. Reported-by: syzbot+feeb9b5352b9ff89618a@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=feeb9b5352b9ff89618a Signed-off-by: Nguyen Ngoc Thang --- v4: - Retitle: "annotate" was too vague (Damien). - Explain in the commit message and in a comment why smp_store_release() pairs with READ_ONCE() and not smp_load_acquire(): the reader only uses the loaded pointer, so the address dependency orders the access, as in rcu_assign_pointer()/rcu_dereference(). No code change. v3: https://lore.kernel.org/r/20261020125233.9820-1-ngocthang2710.1999@gmail.com Tested with a KCSAN kernel in QEMU: 26 data-race reports per run before, 0 in two runs after (v1 code, later versions differ only in comments). block/blk-ioc.c | 9 +++++++-- include/linux/ioprio.h | 11 +++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/block/blk-ioc.c b/block/blk-ioc.c index d15918d7fabb..fac62f2858ec 100644 --- a/block/blk-ioc.c +++ b/block/blk-ioc.c @@ -277,9 +277,14 @@ int set_task_ioprio(struct task_struct *task, int ioprio) if (task->io_context) kmem_cache_free(iocontext_cachep, ioc); else - task->io_context = ioc; + /* + * Make the ioc initialization visible before the + * pointer. Readers use READ_ONCE(), whose address + * dependency is enough, like rcu_dereference(). + */ + smp_store_release(&task->io_context, ioc); } - task->io_context->ioprio = ioprio; + WRITE_ONCE(task->io_context->ioprio, ioprio); out: task_unlock(task); return 0; diff --git a/include/linux/ioprio.h b/include/linux/ioprio.h index 5210e8371238..be999cbe7c91 100644 --- a/include/linux/ioprio.h +++ b/include/linux/ioprio.h @@ -56,9 +56,16 @@ static inline int task_nice_ioclass(struct task_struct *task) */ static inline int __get_task_ioprio(struct task_struct *p) { - struct io_context *ioc = p->io_context; + struct io_context *ioc; int prio; + /* + * Only the current task itself drops its io_context, in + * exit_io_context(), so a non-NULL ioc is always valid. However, + * another task may change ->io_context or ioc->ioprio, so make sure + * to get the latest value with READ_ONCE(). + */ + ioc = READ_ONCE(p->io_context); if (!ioc) return IOPRIO_PRIO_VALUE(task_nice_ioclass(p), task_nice_ioprio(p)); @@ -66,7 +73,7 @@ static inline int __get_task_ioprio(struct task_struct *p) if (p != current) lockdep_assert_held(&p->alloc_lock); - prio = ioc->ioprio; + prio = READ_ONCE(ioc->ioprio); if (IOPRIO_PRIO_CLASS(prio) == IOPRIO_CLASS_NONE) prio = IOPRIO_PRIO_VALUE(task_nice_ioclass(p), task_nice_ioprio(p)); -- 2.43.0