public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: David Howells <dhowells@redhat.com>
To: chengming.zhou@linux.dev
Cc: dhowells@redhat.com, axboe@kernel.dk, kch@nvidia.com,
	damien.lemoal@opensource.wdc.com, bvanassche@acm.org,
	nj.shetty@samsung.com, kbusch@kernel.org,
	zhouchengming@bytedance.com, akinobu.mita@gmail.com,
	shinichiro.kawasaki@wdc.com, linux-block@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] null_blk: fix poll request timeout handling
Date: Tue, 15 Aug 2023 09:19:35 +0100	[thread overview]
Message-ID: <23383.1692087575@warthog.procyon.org.uk> (raw)
In-Reply-To: <20230815060443.660263-1-chengming.zhou@linux.dev>

chengming.zhou@linux.dev wrote:

> From: Chengming Zhou <zhouchengming@bytedance.com>
> 
> When doing io_uring benchmark on /dev/nullb0, it's easy to crash the
> kernel if poll requests timeout triggered, as reported by David. [1]
> 
> BUG: kernel NULL pointer dereference, address: 0000000000000008
> Workqueue: kblockd blk_mq_timeout_work
> RIP: 0010:null_timeout_rq+0x4e/0x91
> Call Trace:
>  ? __die_body+0x1a/0x5c
>  ? page_fault_oops+0x6f/0x9c
>  ? kernelmode_fixup_or_oops+0xc6/0xd6
>  ? __bad_area_nosemaphore+0x44/0x1eb
>  ? exc_page_fault+0xe2/0xf4
>  ? asm_exc_page_fault+0x22/0x30
>  ? null_timeout_rq+0x4e/0x91
>  blk_mq_handle_expired+0x31/0x4b
>  bt_iter+0x68/0x84
>  ? bt_tags_iter+0x81/0x81
>  __sbitmap_for_each_set.constprop.0+0xb0/0xf2
>  ? __blk_mq_complete_request_remote+0xf/0xf
>  bt_for_each+0x46/0x64
>  ? __blk_mq_complete_request_remote+0xf/0xf
>  ? percpu_ref_get_many+0xc/0x2a
>  blk_mq_queue_tag_busy_iter+0x14d/0x18e
>  blk_mq_timeout_work+0x95/0x127
>  process_one_work+0x185/0x263
>  worker_thread+0x1b5/0x227
>  ? rescuer_thread+0x287/0x287
>  kthread+0xfa/0x102
>  ? kthread_complete_and_exit+0x1b/0x1b
>  ret_from_fork+0x22/0x30
> 
> This is indeed a race problem between null_timeout_rq() and null_poll().
> 
> null_poll()				null_timeout_rq()
>   spin_lock(&nq->poll_lock)
>   list_splice_init(&nq->poll_list, &list)
>   spin_unlock(&nq->poll_lock)
> 
>   while (!list_empty(&list))
>     req = list_first_entry()
>     list_del_init()
>     ...
>     blk_mq_add_to_batch()
>     // req->rq_next = NULL
> 					spin_lock(&nq->poll_lock)
> 
> 					// rq->queuelist->next == NULL
> 					list_del_init(&rq->queuelist)
> 
> 					spin_unlock(&nq->poll_lock)
> 
> What's worse is that we don't call blk_mq_complete_request_remote()
> before blk_mq_add_to_batch(), so these completed requests have wrong
> rq->state == MQ_RQ_IN_FLIGHT. We can easily check this using bpftrace:
> 
> ```
> bpftrace -e 'kretfunc:null_blk:null_poll {
>   $iob=(struct io_comp_batch *)args->iob;
>   @[$iob->req_list->state]=count();
> }'
> 
> @[1]: 51708
> ```
> 
> Fix these problems by setting requests state to MQ_RQ_COMPLETE under
> nq->poll_lock protection, in which null_timeout_rq() can safely detect
> this race and early return.
> 
> [1] https://lore.kernel.org/all/3893581.1691785261@warthog.procyon.org.uk/
> 
> Fixes: 0a593fbbc245 ("null_blk: poll queue support")
> Reported-by: David Howells <dhowells@redhat.com>
> Signed-off-by: Chengming Zhou <zhouchengming@bytedance.com>

Okay, the oops no longer seems to happen, so on that basis:

Tested-by: David Howells <dhowells@redhat.com>


However, running:

	./fio/t/io_uring -n4 /dev/nullb0

and then interrupting it with ctrl-C after a while dumps a whole load of
messages into the dmesg log (excerpt attached).  It seems wrong that the user
should be able to generate a dump like this just by interrupting - but I guess
as it's null_blk it probably doesn't matter.

David
---
null_blk: rq 00000000bb2d3264 timed out
timeout error, dev nullb0, sector 328372624 op 0x0:(READ) flags 0xe00000 phys_seg 1 prio class 2
null_blk: rq 00000000abcc1075 timed out
timeout error, dev nullb0, sector 378610072 op 0x0:(READ) flags 0xe00000 phys_seg 1 prio class 2
null_blk: rq 00000000d4bdc71f timed out
timeout error, dev nullb0, sector 185005312 op 0x0:(READ) flags 0xe00000 phys_seg 1 prio class 2
null_blk: rq 00000000f4ffddee timed out
timeout error, dev nullb0, sector 206118608 op 0x0:(READ) flags 0xe00000 phys_seg 1 prio class 2
null_blk: rq 000000001e68b709 timed out
timeout error, dev nullb0, sector 310381160 op 0x0:(READ) flags 0xe00000 phys_seg 1 prio class 2
null_blk: rq 00000000bfeafe97 timed out
timeout error, dev nullb0, sector 52036480 op 0x0:(READ) flags 0xe00000 phys_seg 1 prio class 2
null_blk: rq 00000000aa67d21c timed out
timeout error, dev nullb0, sector 22746448 op 0x0:(READ) flags 0xe00000 phys_seg 1 prio class 2
null_blk: rq 00000000faec1291 timed out
timeout error, dev nullb0, sector 391201440 op 0x0:(READ) flags 0xe00000 phys_seg 1 prio class 2
null_blk: rq 00000000c634428c timed out
timeout error, dev nullb0, sector 237216136 op 0x0:(READ) flags 0xe00000 phys_seg 1 prio class 2
null_blk: rq 0000000077f91a5d timed out
timeout error, dev nullb0, sector 453778912 op 0x0:(READ) flags 0xe00000 phys_seg 1 prio class 2
null_blk: rq 000000003076467c timed out
null_blk: rq 000000009c172678 timed out
null_blk: rq 000000002df50b48 timed out
null_blk: rq 00000000e4c66900 timed out
null_blk: rq 0000000082606e31 timed out
null_blk: rq 00000000fe21ffdc timed out
null_blk: rq 000000005e5c5173 timed out
null_blk: rq 00000000b0a0d20c timed out
null_blk: rq 000000008c729e47 timed out
null_blk: rq 00000000970f75a0 timed out
null_blk: rq 000000002ad3c45a timed out


  reply	other threads:[~2023-08-15  8:21 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-15  6:04 [PATCH] null_blk: fix poll request timeout handling chengming.zhou
2023-08-15  8:19 ` David Howells [this message]
2023-08-16  9:15   ` Chengming Zhou
2023-08-15  8:31 ` Ming Lei
2023-08-15  9:45   ` Chengming Zhou
2023-08-15 12:18     ` Ming Lei
2023-08-17  9:02     ` Ming Lei

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=23383.1692087575@warthog.procyon.org.uk \
    --to=dhowells@redhat.com \
    --cc=akinobu.mita@gmail.com \
    --cc=axboe@kernel.dk \
    --cc=bvanassche@acm.org \
    --cc=chengming.zhou@linux.dev \
    --cc=damien.lemoal@opensource.wdc.com \
    --cc=kbusch@kernel.org \
    --cc=kch@nvidia.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nj.shetty@samsung.com \
    --cc=shinichiro.kawasaki@wdc.com \
    --cc=zhouchengming@bytedance.com \
    /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