blk-mq: fix race related with device deletion/reset/switching sched
Subsystems (2)
v3 cover blk-mq: fix race related with device deletion/reset/switching sched
Hi,
The 1st patch fixes one kernel oops triggered by IOs vs. deleting SCSI device, and this issue can be triggered easily on scsi_debug.
The other 3 patch fixes recent Yi Zhang's reports about his NVMe stress tests, most of them are related with switching io sched, NVMe reset or updating nr_hw_queues.
V3:
- fix 'blk-mq: support concurrent blk_mq_quiesce_queue' on SCSI, which calls quiesce two times, and split blk_quiesce_queue into two parts and fix this issue
- remove previous patch 6, and Sagi provides one better patch to fix that, see '[PATCH v2] nvme-pci: allocate device queues storage space at probe'
- remove patch 2 of 'blk-mq: support concurrent blk_mq_quiesce_queue()' which need a bit much more work, especially we need to cleanup NVMe's usage on quiesce & unquiesce first, so delay it for V4.17.
V2:
- address stale queue mapping in blk_mq_update_queue_map(), instead of PCI transport, since such issue exists on other transport too, as suggested by Christoph
- avoid to introduce nvme_admin_queue_rq() since the nvme queue can be got from hctx->driver_data, which is reliable
Thanks,
Ming
Ming Lei (4):
blk-mq: quiesce queue before freeing queue
blk-mq: quiesce queue during switching io sched and updating
nr_requests
blk-mq: avoid to map CPU into stale hw queue
blk-mq: fix race between updating nr_hw_queues and switching io sched
block/blk-core.c | 9 +++++++++ block/blk-mq.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++--- block/elevator.c | 2 ++ 3 files changed, 61 insertions(+), 3 deletions(-)
Signature · 1 line
2.9.5
PATCH v3 1/4 blk-mq: quiesce queue before freeing queue
After queue is frozen, dispatch still may happen, for example:
1) requests are submitted from several contexts
2) requests from all these contexts are inserted to queue, but may dispatch to LLD in one of these paths, but other paths sill need to move on even all these requests are completed(that means blk_mq_freeze_queue_wait() returns at that time)
3) dispatch after queue freezing still moves on and causes use-after-free, because request queue is freed
This patch quiesces queue after it is frozen, and makes sure all in-progress dispatch are completed.
This patch fixes the following kernel crash when running heavy IOs vs. deleting device:
[ 36.719251] BUG: unable to handle kernel NULL pointer dereference at 0000000000000008 [ 36.720318] IP: kyber_has_work+0x14/0x40 [ 36.720847] PGD 254bf5067 P4D 254bf5067 PUD 255e6a067 PMD 0 [ 36.721584] Oops: 0000 [#1] PREEMPT SMP [ 36.722105] Dumping ftrace buffer: [ 36.722570] (ftrace buffer empty) [ 36.723057] Modules linked in: scsi_debug ebtable_filter ebtables ip6table_filter ip6_tables tcm_loop iscsi_target_mod target_core_file target_core_iblock target_core_pscsi target_core_mod xt_CHECKSUM iptable_mangle ipt_MASQUERADE nf_nat_masquerade_ipv4 iptable_nat nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat_ipv4 nf_nat nf_conntrack libcrc32c bridge stp llc fuse iptable_filter ip_tables sd_mod sg btrfs xor zstd_decompress zstd_compress xxhash raid6_pq mptsas mptscsih bcache crc32c_intel ahci mptbase libahci serio_raw scsi_transport_sas nvme libata shpchp lpc_ich virtio_scsi nvme_core binfmt_misc dm_mod iscsi_tcp libiscsi_tcp libiscsi scsi_transport_iscsi null_blk configs [ 36.733438] CPU: 2 PID: 2374 Comm: fio Not tainted 4.15.0-rc2.blk_mq_quiesce+ #714 [ 36.735143] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.9.3-1.fc25 04/01/2014 [ 36.736688] RIP: 0010:kyber_has_work+0x14/0x40 [ 36.737515] RSP: 0018:ffffc9000209bca0 EFLAGS: 00010202 [ 36.738431] RAX: 0000000000000008 RBX: ffff88025578bfc8 RCX: ffff880257bf4ed0 [ 36.739581] RDX: 0000000000000038 RSI: ffffffff81a98c6d RDI: ffff88025578bfc8 [ 36.740730] RBP: ffff880253cebfc8 R08: ffffc9000209bda0 R09: ffff8802554f3480 [ 36.741885] R10: ffffc9000209be60 R11: ffff880263f72538 R12: ffff88025573e9e8 [ 36.743036] R13: ffff88025578bfd0 R14: 0000000000000001 R15: 0000000000000000 [ 36.744189] FS: 00007f9b9bee67c0(0000) GS:ffff88027fc80000(0000) knlGS:0000000000000000 [ 36.746617] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 36.748483] CR2: 0000000000000008 CR3: 0000000254bf4001 CR4: 00000000003606e0 [ 36.750164] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [ 36.751455] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 [ 36.752796] Call Trace: [ 36.753992] blk_mq_do_dispatch_sched+0x7f/0xe0 [ 36.755110] blk_mq_sched_dispatch_requests+0x119/0x190 [ 36.756179] __blk_mq_run_hw_queue+0x83/0x90 [ 36.757144] __blk_mq_delay_run_hw_queue+0xaf/0x110 [ 36.758046] blk_mq_run_hw_queue+0x24/0x70 [ 36.758845] blk_mq_flush_plug_list+0x1e7/0x270 [ 36.759676] blk_flush_plug_list+0xd6/0x240 [ 36.760463] blk_finish_plug+0x27/0x40 [ 36.761195] do_io_submit+0x19b/0x780 [ 36.761921] ? entry_SYSCALL_64_fastpath+0x1a/0x7d [ 36.762788] entry_SYSCALL_64_fastpath+0x1a/0x7d [ 36.763639] RIP: 0033:0x7f9b9699f697 [ 36.764352] RSP: 002b:00007ffc10f991b8 EFLAGS: 00000206 ORIG_RAX: 00000000000000d1 [ 36.765773] RAX: ffffffffffffffda RBX: 00000000008f6f00 RCX: 00007f9b9699f697 [ 36.766965] RDX: 0000000000a5e6c0 RSI: 0000000000000001 RDI: 00007f9b8462a000 [ 36.768377] RBP: 0000000000000000 R08: 0000000000000001 R09: 00000000008f6420 [ 36.769649] R10: 00007f9b846e5000 R11: 0000000000000206 R12: 00007f9b795d6a70 [ 36.770807] R13: 00007f9b795e4140 R14: 00007f9b795e3fe0 R15: 0000000100000000 [ 36.771955] Code: 83 c7 10 e9 3f 68 d1 ff 0f 1f 44 00 00 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 8b 97 b0 00 00 00 48 8d 42 08 48 83 c2 38 <48> 3b 00 74 06 b8 01 00 00 00 c3 48 3b 40 08 75 f4 48 83 c0 10 [ 36.775004] RIP: kyber_has_work+0x14/0x40 RSP: ffffc9000209bca0 [ 36.776012] CR2: 0000000000000008 [ 36.776690] ---[ end trace 4045cbce364ff2a4 ]--- [ 36.777527] Kernel panic - not syncing: Fatal exception [ 36.778526] Dumping ftrace buffer: [ 36.779313] (ftrace buffer empty) [ 36.780081] Kernel Offset: disabled [ 36.780877] ---[ end Kernel panic - not syncing: Fatal exception
--- block/blk-core.c | 9 +++++++++ 1 file changed, 9 insertions(+)
block/blk-core.c +9 -0
@@ -694,6 +694,15 @@ void blk_cleanup_queue(struct request_queue *q)queue_flag_set (QUEUE_FLAG_DEAD , q);spin_unlock_irq (lock); +/* +* make sure all in-progress dispatch are completed because +* blk_freeze_queue() can only complete all requests, and +* dispatch may still be in-progress since we dispatch requests +* from more than one contexts +*/ +if (q->mq_ops) +blk_mq_quiesce_queue (q); +/* for synchronous bio-based driver finish in-flight integrity i/o */ blk_flush_integrity ();
Signature · 1 line
2.9.5
PATCH v3 2/4 blk-mq: quiesce queue during switching io sched and updating nr_requests
In reply to Ming LeiDispatch may still be in-progress after queue is frozen, so we have to quiesce queue before switching IO scheduler and updating nr_requests.
Also when switching io schedulers, blk_mq_run_hw_queue() may still be called somewhere(such as from nvme_reset_work()), and io scheduler's per-hctx data may not be setup yet, so cause oops even inside blk_mq_hctx_has_pending(), such as it can be run just between:
ret = e->ops.mq.init_sched(q, e);
AND
ret = e->ops.mq.init_hctx(hctx, i)inside blk_mq_init_sched().
This reverts commit 7a148c2fcff8330(block: don't call blk_mq_quiesce_queue() after queue is frozen) basically, and makes sure blk_mq_hctx_has_pending won't be called if queue is quiesced.
--- block/blk-mq.c | 27 ++++++++++++++++++++++++++- block/elevator.c | 2 ++ 2 files changed, 28 insertions(+), 1 deletion(-)
block/blk-mq.c +26 -1
@@ -1285,7 +1285,30 @@ EXPORT_SYMBOL(blk_mq_delay_run_hw_queue);bool blk_mq_run_hw_queue (struct blk_mq_hw_ctx *hctx,bool async) { -if (blk_mq_hctx_has_pending (hctx)) { +int srcu_idx; +bool need_run; + +/* +* When queue is quiesced, we may be switching io scheduler, or +* updating nr_hw_queues, or other things, and we can't run queue +* any more, even __blk_mq_hctx_has_pending() can't be called safely. +* +* And queue will be rerun in blk_mq_unquiesce_queue() if it is +* quiesced. +*/ +if (!(hctx->flags &BLK_MQ_F_BLOCKING )) { +rcu_read_lock (); + need_run = !blk_queue_quiesced (hctx->queue) && +blk_mq_hctx_has_pending (hctx); +rcu_read_unlock (); + }else { + srcu_idx =srcu_read_lock (hctx->queue_rq_srcu); + need_run = !blk_queue_quiesced (hctx->queue) && +blk_mq_hctx_has_pending (hctx); +srcu_read_unlock (hctx->queue_rq_srcu, srcu_idx); + } + +if (need_run) {__blk_mq_delay_run_hw_queue (hctx, async,0 );return true; }
@@ -2710,6 +2733,7 @@ int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)return -EINVAL ;blk_mq_freeze_queue (q); +blk_mq_quiesce_queue (q); ret =0 ;queue_for_each_hw_ctx (q, hctx, i) {
@@ -2733,6 +2757,7 @@ int blk_mq_update_nr_requests(struct request_queue *q, unsigned int nr)if (!ret) q->nr_requests = nr; +blk_mq_unquiesce_queue (q);blk_mq_unfreeze_queue (q);return ret;
block/elevator.c +2 -0
@@ -968,6 +968,7 @@ static int elevator_switch_mq(struct request_queue *q,int ret;blk_mq_freeze_queue (q); +blk_mq_quiesce_queue (q);if (q->elevator) {if (q->elevator->registered)
@@ -994,6 +995,7 @@ static int elevator_switch_mq(struct request_queue *q,blk_add_trace_msg (q,"elv switch: none" ); out: +blk_mq_unquiesce_queue (q);blk_mq_unfreeze_queue (q);return ret; }
Signature · 1 line
2.9.5
PATCH v3 3/4 blk-mq: avoid to map CPU into stale hw queue
In reply to Ming Leiblk_mq_pci_map_queues() may not map one CPU into any hw queue, but its previous map isn't cleared yet, and may point to one stale hw queue index.
This patch fixes the following issue by clearing the mapping table before setting it up in blk_mq_pci_map_queues().
This patches fixes this following issue reported by Zhang Yi:
[ 101.202734] BUG: unable to handle kernel NULL pointer dereference at 0000000094d3013f [ 101.211487] IP: blk_mq_map_swqueue+0xbc/0x200 [ 101.216346] PGD 0 P4D 0 [ 101.219171] Oops: 0000 [#1] SMP [ 101.222674] Modules linked in: sunrpc ipmi_ssif vfat fat intel_rapl sb_edac x86_pkg_temp_thermal intel_powerclamp coretemp kvm_intel kvm irqbypass crct10dif_pclmul crc32_pclmul ghash_clmulni_intel intel_cstate intel_uncore mxm_wmi intel_rapl_perf iTCO_wdt ipmi_si ipmi_devintf pcspkr iTCO_vendor_support sg dcdbas ipmi_msghandler wmi mei_me lpc_ich shpchp mei acpi_power_meter dm_multipath ip_tables xfs libcrc32c sd_mod mgag200 i2c_algo_bit drm_kms_helper syscopyarea sysfillrect sysimgblt fb_sys_fops ttm drm ahci libahci crc32c_intel libata tg3 nvme nvme_core megaraid_sas ptp i2c_core pps_core dm_mirror dm_region_hash dm_log dm_mod [ 101.284881] CPU: 0 PID: 504 Comm: kworker/u25:5 Not tainted 4.15.0-rc2 #1 [ 101.292455] Hardware name: Dell Inc. PowerEdge R730xd/072T6D, BIOS 2.5.5 08/16/2017 [ 101.301001] Workqueue: nvme-wq nvme_reset_work [nvme] [ 101.306636] task: 00000000f2c53190 task.stack: 000000002da874f9 [ 101.313241] RIP: 0010:blk_mq_map_swqueue+0xbc/0x200 [ 101.318681] RSP: 0018:ffffc9000234fd70 EFLAGS: 00010282 [ 101.324511] RAX: ffff88047ffc9480 RBX: ffff88047e130850 RCX: 0000000000000000 [ 101.332471] RDX: ffffe8ffffd40580 RSI: ffff88047e509b40 RDI: ffff88046f37a008 [ 101.340432] RBP: 000000000000000b R08: ffff88046f37a008 R09: 0000000011f94280 [ 101.348392] R10: ffff88047ffd4d00 R11: 0000000000000000 R12: ffff88046f37a008 [ 101.356353] R13: ffff88047e130f38 R14: 000000000000000b R15: ffff88046f37a558 [ 101.364314] FS: 0000000000000000(0000) GS:ffff880277c00000(0000) knlGS:0000000000000000 [ 101.373342] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 101.379753] CR2: 0000000000000098 CR3: 000000047f409004 CR4: 00000000001606f0 [ 101.387714] Call Trace: [ 101.390445] blk_mq_update_nr_hw_queues+0xbf/0x130 [ 101.395791] nvme_reset_work+0x6f4/0xc06 [nvme] [ 101.400848] ? pick_next_task_fair+0x290/0x5f0 [ 101.405807] ? __switch_to+0x1f5/0x430 [ 101.409988] ? put_prev_entity+0x2f/0xd0 [ 101.414365] process_one_work+0x141/0x340 [ 101.418836] worker_thread+0x47/0x3e0 [ 101.422921] kthread+0xf5/0x130 [ 101.426424] ? rescuer_thread+0x380/0x380 [ 101.430896] ? kthread_associate_blkcg+0x90/0x90 [ 101.436048] ret_from_fork+0x1f/0x30 [ 101.440034] Code: 48 83 3c ca 00 0f 84 2b 01 00 00 48 63 cd 48 8b 93 10 01 00 00 8b 0c 88 48 8b 83 20 01 00 00 4a 03 14 f5 60 04 af 81 48 8b 0c c8 <48> 8b 81 98 00 00 00 f0 4c 0f ab 30 8b 81 f8 00 00 00 89 42 44 [ 101.461116] RIP: blk_mq_map_swqueue+0xbc/0x200 RSP: ffffc9000234fd70 [ 101.468205] CR2: 0000000000000098 [ 101.471907] ---[ end trace 5fe710f98228a3ca ]--- [ 101.482489] Kernel panic - not syncing: Fatal exception [ 101.488505] Kernel Offset: disabled [ 101.497752] ---[ end Kernel panic - not syncing: Fatal exception
--- block/blk-mq.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-)
block/blk-mq.c +20 -2
@@ -2622,9 +2622,27 @@ static int blk_mq_alloc_rq_maps(struct blk_mq_tag_set *set)static int blk_mq_update_queue_map (struct blk_mq_tag_set *set) { -if (set->ops->map_queues) +if (set->ops->map_queues) { +int cpu; +/* +* transport .map_queues is usually done in the following +* way: +* +* for (queue = 0; queue < set->nr_hw_queues; queue++) { +* mask = get_cpu_mask(queue) +* for_each_cpu(cpu, mask) +* set->mq_map[cpu] = queue; +* } +* +* When we need to remap, the table has to be cleared for +* killing stale mapping since one CPU may not be mapped +* to any hw queue. +*/ +for_each_possible_cpu (cpu) + set->mq_map[cpu] =0 ; +return set->ops->map_queues (set); -else + }else return blk_mq_map_queues (set); }
Signature · 1 line
2.9.5
PATCH v3 4/4 blk-mq: fix race between updating nr_hw_queues and switching io sched
In reply to Ming LeiIn both elevator_switch_mq() and blk_mq_update_nr_hw_queues(), sched tags can be allocated, and q->nr_hw_queue is used, and race is inevitable, for
example: blk_mq_init_sched() may trigger use-after-free on hctx, which is freed in blk_mq_realloc_hw_ctxs() when nr_hw_queues is decreased.
This patch fixes the race be holding q->sysfs_lock.
--- block/blk-mq.c | 4 ++++ 1 file changed, 4 insertions(+)
block/blk-mq.c +4 -0
@@ -2407,6 +2407,9 @@ static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set,struct blk_mq_hw_ctx **hctxs = q->queue_hw_ctx;blk_mq_sysfs_unregister (q); + +/* protect against switching io scheduler */ +mutex_lock (&q->sysfs_lock);for (i =0 ; i < set->nr_hw_queues; i++) {int node;
@@ -2451,6 +2454,7 @@ static void blk_mq_realloc_hw_ctxs(struct blk_mq_tag_set *set, } } q->nr_hw_queues = i; +mutex_unlock (&q->sysfs_lock);blk_mq_sysfs_register (q); }
Signature · 1 line
2.9.5
Jens Axboe
maintainerQuoted 8 lines · Ming Lei
Hi,
The 1st patch fixes one kernel oops triggered by IOs vs. deleting SCSI device, and this issue can be triggered easily on
scsi_debug.The other 3 patch fixes recent Yi Zhang's reports about his NVMe stress tests, most of them are related with switching io sched, NVMe reset or updating
nr_hw_queues.
Applied for 4.16, thanks.
Signature · 1 line
Jens Axboe