Skip to contentSkip to conversation

ublk: hold RCU read lock in ublk_try_buf_match()

PATCH Applied Paolo Bertinetti 3 participants 4 messages Sep 22 – Oct 15

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ublk: hold RCU read lock in ublk_try_buf_match()

syzbot reports a lockdep splat when I/O is dispatched to a ublk device with UBLK_F_SHMEM_ZC enabled:

WARNING: suspicious RCU usage
lib/maple_tree.c:720 suspicious rcu_dereference_check() usage!
...

  Call Trace:
   mas_root lib/maple_tree.c:720 [inline]
   mas_start+0x3cc/0x640 lib/maple_tree.c:1250
   mas_state_walk lib/maple_tree.c:3006 [inline]
   mas_walk+0x1db/0x490 lib/maple_tree.c:4458
   ublk_try_buf_match drivers/block/ublk_drv.c:5639 [inline]
   ublk_init_iod+0x5d2/0x1120 drivers/block/ublk_drv.c:521
   ublk_dispatch_req+0x97/0x9c0 drivers/block/ublk_drv.c:1776
   tctx_task_work_run+0x73d/0xa80 io_uring/tw.c:96

ublk_try_buf_match() calls mas_walk() on the buffer tree without holding the RCU read lock or the tree lock. Writers are already excluded because ublk_lock_buf_tree() freezes the queue, so there is no real race, but maple tree readers must still hold one of the two.

Take the RCU read lock around the walk.

Fixes 4d4a512a1f87 ("ublk: add PFN-based buffer matching in I/O path")
Reported-by [email protected]
Assisted-by LLM

---
syzbot tested this patch [1]: the RCU splat is gone, but the reproducer then hits the already open "WARNING in io_wq_put_and_exit (2)" [2], which syzbot has been reporting since May on kernels without this change.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://syzkaller.appspot.com/bug?extid=b0d54b9e81de55179e47

 drivers/block/ublk_drv.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/block/ublk_drv.c +2 -0
index 66eb55e71..7a64745e1 100644
@@ -5628,6 +5628,8 @@ static bool ublk_try_buf_match(struct ublk_device *ub,
 	unsigned long expected_offset = 0;
 	bool first = true;
 
+	guard(rcu)();
+
 	rq_for_each_bvec(bv, rq, iter) {
 		unsigned long pfn = page_to_pfn(bv.bv_page);
 		unsigned long end_pfn = pfn +

base-commit: 93f51579e7df248780214094418f205253383cc5

Signature · 1 line
2.55.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 45 lines · Paolo Bertinetti

syzbot reports a lockdep splat when I/O is dispatched to a ublk device with UBLK_F_SHMEM_ZC enabled:

WARNING: suspicious RCU usage
lib/maple_tree.c:720 suspicious rcu_dereference_check() usage!
...

  Call Trace:
   mas_root lib/maple_tree.c:720 [inline]
   mas_start+0x3cc/0x640 lib/maple_tree.c:1250
   mas_state_walk lib/maple_tree.c:3006 [inline]
   mas_walk+0x1db/0x490 lib/maple_tree.c:4458
   ublk_try_buf_match drivers/block/ublk_drv.c:5639 [inline]
   ublk_init_iod+0x5d2/0x1120 drivers/block/ublk_drv.c:521
   ublk_dispatch_req+0x97/0x9c0 drivers/block/ublk_drv.c:1776
   tctx_task_work_run+0x73d/0xa80 io_uring/tw.c:96

ublk_try_buf_match() calls mas_walk() on the buffer tree without holding the RCU read lock or the tree lock. Writers are already excluded because ublk_lock_buf_tree() freezes the queue, so there is no real race, but maple tree readers must still hold one of the two.

Take the RCU read lock around the walk.

Fixes 4d4a512a1f87 ("ublk: add PFN-based buffer matching in I/O path")
Reported-by [email protected]
Assisted-by LLM

---
syzbot tested this patch [1]: the RCU splat is gone, but the reproducer then hits the already open "WARNING in io_wq_put_and_exit (2)" [2], which syzbot has been reporting since May on kernels without this change.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://syzkaller.appspot.com/bug?extid=b0d54b9e81de55179e47

 drivers/block/ublk_drv.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/block/ublk_drv.c +0 -0
index 66eb55e71..7a64745e1 100644

drivers/block/ublk_drv.c static bool ublk_try_buf_match(struct ublk_device *ub, · line 5632

Quoted 5 lines
drivers/block/ublk_drv.c +2 -0
@@ -5628,6 +5628,8 @@ static bool ublk_try_buf_match(struct ublk_device *ub,
        unsigned long expected_offset = 0;
        bool first = true;
 
+       guard(rcu)();
+

Thanks
Ming Lei

#2 lore raw

Hi Jens,

Gentle ping on this one, Ming has reviewed it. Could you pick it up?

Thanks,
Paolo

maintainer Applied
#3 lore raw
In reply to Paolo Bertinetti
Quoted 16 lines · Paolo Bertinetti

syzbot reports a lockdep splat when I/O is dispatched to a ublk device with UBLK_F_SHMEM_ZC enabled:

WARNING: suspicious RCU usage
lib/maple_tree.c:720 suspicious rcu_dereference_check() usage!
...

  Call Trace:
   mas_root lib/maple_tree.c:720 [inline]
   mas_start+0x3cc/0x640 lib/maple_tree.c:1250
   mas_state_walk lib/maple_tree.c:3006 [inline]
   mas_walk+0x1db/0x490 lib/maple_tree.c:4458
   ublk_try_buf_match drivers/block/ublk_drv.c:5639 [inline]
   ublk_init_iod+0x5d2/0x1120 drivers/block/ublk_drv.c:521
   ublk_dispatch_req+0x97/0x9c0 drivers/block/ublk_drv.c:1776
   tctx_task_work_run+0x73d/0xa80 io_uring/tw.c:96
Quoted 1 line

[...]

Applied, thanks!

[1/1] ublk: hold RCU read lock in ublk_try_buf_match()

      commit: aaf49023041bc1f887a028a164d3bac714b65508

Best regards,

Signature · 1 line
Jens Axboe