Fix races between blkcg code and request queue initialization and cleanup
Subsystems (7)
v3 cover Fix races between blkcg code and request queue initialization and cleanup
Hello Jens,
Recently Joseph Qi identified races between the block cgroup code and request queue initialization and cleanup. This patch series address these races. Please consider these patches for kernel v4.17.
Thanks,
Bart.
Changes between v2 and v3:
- Added a third patch that fixes a race between the blkcg code and queue cleanup.
Changes between v1 and v2:
- Split a single patch into two patches.
- Dropped blk_alloc_queue_node2() and modified all block drivers that call blk_alloc_queue_node().
Bart Van Assche (3):
block: Add a third argument to blk_alloc_queue_node()
block: Fix a race between the cgroup code and request queue
initialization
block: Fix a race between request queue removal and the block cgroup
controller
block/blk-core.c | 60 +++++++++++++++++++++++++++++++++++------- block/blk-mq.c | 2 +- block/blk-sysfs.c | 7 ----- drivers/block/drbd/drbd_main.c | 3 +-- drivers/block/null_blk.c | 3 ++- drivers/block/umem.c | 7 +++-- drivers/ide/ide-probe.c | 2 +- drivers/lightnvm/core.c | 2 +- drivers/md/dm.c | 2 +- drivers/nvdimm/pmem.c | 2 +- drivers/nvme/host/multipath.c | 2 +- drivers/scsi/scsi_lib.c | 2 +- include/linux/blkdev.h | 3 ++- 13 files changed, 65 insertions(+), 32 deletions(-)
Signature · 1 line
2.16.1
PATCH v3 1/3 block: Add a third argument to blk_alloc_queue_node()
This patch does not change any functionality.
--- block/blk-core.c | 7 ++++--- block/blk-mq.c | 2 +- drivers/block/null_blk.c | 3 ++- drivers/ide/ide-probe.c | 2 +- drivers/lightnvm/core.c | 2 +- drivers/md/dm.c | 2 +- drivers/nvdimm/pmem.c | 2 +- drivers/nvme/host/multipath.c | 2 +- drivers/scsi/scsi_lib.c | 2 +- include/linux/blkdev.h | 3 ++- 10 files changed, 15 insertions(+), 12 deletions(-)
block/blk-core.c +4 -3
@@ -810,7 +810,7 @@ void blk_exit_rl(struct request_queue *q, struct request_list *rl)struct request_queue *blk_alloc_queue (gfp_t gfp_mask) { -return blk_alloc_queue_node (gfp_mask,NUMA_NO_NODE ); +return blk_alloc_queue_node (gfp_mask,NUMA_NO_NODE ,NULL ); }EXPORT_SYMBOL (blk_alloc_queue);
@@ -888,7 +888,8 @@ static void blk_rq_timed_out_timer(struct timer_list *t)kblockd_schedule_work (&q->timeout_work); } -struct request_queue *blk_alloc_queue_node (gfp_t gfp_mask,int node_id) +struct request_queue *blk_alloc_queue_node (gfp_t gfp_mask,int node_id, +spinlock_t *lock) {struct request_queue *q;
@@ -1030,7 +1031,7 @@ blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id) {struct request_queue *q; - q =blk_alloc_queue_node (GFP_KERNEL , node_id); + q =blk_alloc_queue_node (GFP_KERNEL , node_id,NULL );if (!q)return NULL ;
block/blk-mq.c +1 -1
@@ -2554,7 +2554,7 @@ struct request_queue *blk_mq_init_queue(struct blk_mq_tag_set *set) {struct request_queue *uninit_q, *q; - uninit_q =blk_alloc_queue_node (GFP_KERNEL , set->numa_node); + uninit_q =blk_alloc_queue_node (GFP_KERNEL , set->numa_node,NULL );if (!uninit_q)return ERR_PTR (-ENOMEM );
drivers/block/null_blk.c +2 -1
@@ -1717,7 +1717,8 @@ static int null_add_dev(struct nullb_device *dev) }null_init_queues (nullb); }else if (dev->queue_mode ==NULL_Q_BIO ) { - nullb->q =blk_alloc_queue_node (GFP_KERNEL , dev->home_node); + nullb->q =blk_alloc_queue_node (GFP_KERNEL , dev->home_node, +NULL );if (!nullb->q) { rv = -ENOMEM ; goto out_cleanup_queues;
drivers/ide/ide-probe.c +1 -1
@@ -766,7 +766,7 @@ static int ide_init_queue(ide_drive_t *drive)* limits and LBA48 we could raise it but as yet * do not. */ - q =blk_alloc_queue_node (GFP_KERNEL ,hwif_to_node (hwif)); + q =blk_alloc_queue_node (GFP_KERNEL ,hwif_to_node (hwif),NULL );if (!q)return 1 ;
drivers/lightnvm/core.c +1 -1
@@ -384,7 +384,7 @@ static int nvm_create_tgt(struct nvm_dev *dev, struct nvm_ioctl_create *create) goto err_dev; } - tqueue =blk_alloc_queue_node (GFP_KERNEL , dev->q->node); + tqueue =blk_alloc_queue_node (GFP_KERNEL , dev->q->node,NULL );if (!tqueue) { ret = -ENOMEM ; goto err_disk;
drivers/md/dm.c +1 -1
@@ -1840,7 +1840,7 @@ static struct mapped_device *alloc_dev(int minor)INIT_LIST_HEAD (&md->table_devices);spin_lock_init (&md->uevent_lock); - md->queue =blk_alloc_queue_node (GFP_KERNEL , numa_node_id); + md->queue =blk_alloc_queue_node (GFP_KERNEL , numa_node_id,NULL );if (!md->queue) goto bad; md->queue->queuedata = md;
drivers/nvdimm/pmem.c +1 -1
@@ -344,7 +344,7 @@ static int pmem_attach_disk(struct device *dev,return -EBUSY ; } - q =blk_alloc_queue_node (GFP_KERNEL ,dev_to_node (dev)); + q =blk_alloc_queue_node (GFP_KERNEL ,dev_to_node (dev),NULL );if (!q)return -ENOMEM ;
drivers/nvme/host/multipath.c +1 -1
@@ -162,7 +162,7 @@ int nvme_mpath_alloc_disk(struct nvme_ctrl *ctrl, struct nvme_ns_head *head)if (!(ctrl->subsys->cmic & (1 <<1 )) || !multipath)return 0 ; - q =blk_alloc_queue_node (GFP_KERNEL ,NUMA_NO_NODE ); + q =blk_alloc_queue_node (GFP_KERNEL ,NUMA_NO_NODE ,NULL );if (!q) goto out; q->queuedata = head;
drivers/scsi/scsi_lib.c +1 -1
@@ -2223,7 +2223,7 @@ struct request_queue *scsi_old_alloc_queue(struct scsi_device *sdev)struct Scsi_Host *shost = sdev->host;struct request_queue *q; - q =blk_alloc_queue_node (GFP_KERNEL ,NUMA_NO_NODE ); + q =blk_alloc_queue_node (GFP_KERNEL ,NUMA_NO_NODE ,NULL );if (!q)return NULL ; q->cmd_size =sizeof (struct scsi_cmnd ) + shost->hostt->cmd_size;
include/linux/blkdev.h +2 -1
@@ -1321,7 +1321,8 @@ extern long nr_blockdev_pages(void);bool __must_check blk_get_queue (struct request_queue *);struct request_queue *blk_alloc_queue (gfp_t ); -struct request_queue *blk_alloc_queue_node (gfp_t ,int ); +struct request_queue *blk_alloc_queue_node (gfp_t gfp_mask,int node_id, +spinlock_t *lock);extern void blk_put_queue (struct request_queue *);extern void blk_set_queue_dying (struct request_queue *);
Signature · 1 line
2.16.1
PATCH v3 2/3 block: Fix a race between the cgroup code and request queue initialization
In reply to Bart Van AsscheInitialize the request queue lock earlier such that the following race can no longer occur:
blk_init_queue_node() blkcg_print_blkgs()
blk_alloc_queue_node (1)
q->queue_lock = &q->__queue_lock (2)
blkcg_init_queue(q) (3)
spin_lock_irq(blkg->q->queue_lock) (4)
q->queue_lock = lock (5)
spin_unlock_irq(blkg->q->queue_lock) (6)(1) allocate an uninitialized queue;
(2) initialize queue_lock to its default internal lock;
(3) initialize blkcg part of request queue, which will create blkg and
then insert it to blkg_list;
(4) traverse blkg_list and find the created blkg, and then take its
queue lock, here it is the default *internal lock*;
(5) *race window*, now queue_lock is overridden with *driver specified
lock*;(6) now unlock *driver specified lock*, not the locked *internal lock*, unlock balance breaks.
The changes in this patch are as follows:
- Move the .queue_lock initialization from blk_init_queue_node() into blk_alloc_queue_node().
- Only override the .queue_lock pointer for legacy queues because it is not useful for blk-mq queues to override this pointer.
- For all all block drivers that initialize .queue_lock explicitly, change the blk_alloc_queue() call in the driver into a blk_alloc_queue_node() call and remove the explicit .queue_lock initialization. Additionally, initialize the spin lock that will be used as queue lock earlier if necessary.
--- block/blk-core.c | 24 ++++++++++++++++-------- drivers/block/drbd/drbd_main.c | 3 +-- drivers/block/umem.c | 7 +++---- 3 files changed, 20 insertions(+), 14 deletions(-)
block/blk-core.c +16 -8
@@ -888,6 +888,19 @@ static void blk_rq_timed_out_timer(struct timer_list *t)kblockd_schedule_work (&q->timeout_work); } +/** +* blk_alloc_queue_node - allocate a request queue +* @gfp_mask: memory allocation flags +* @node_id: NUMA node to allocate memory from +* @lock: For legacy queues, pointer to a spinlock that will be used to e.g. +* serialize calls to the legacy .request_fn() callback. Ignored for +* blk-mq request queues. +* +* Note: pass the queue lock as the third argument to this function instead of +* setting the queue lock pointer explicitly to avoid triggering a sporadic +* crash in the blkcg code. This function namely calls blkcg_init_queue() and +* the queue lock pointer must be set before blkcg_init_queue() is called. +*/ struct request_queue *blk_alloc_queue_node (gfp_t gfp_mask,int node_id,spinlock_t *lock) {
@@ -940,11 +953,8 @@ struct request_queue *blk_alloc_queue_node(gfp_t gfp_mask, int node_id,mutex_init (&q->sysfs_lock);spin_lock_init (&q->__queue_lock); -/* -* By default initialize queue_lock to internal lock and driver can -* override it later if need be. -*/ - q->queue_lock = &q->__queue_lock; +if (!q->mq_ops) + q->queue_lock = lock ? : &q->__queue_lock;/* * A queue starts its life with bypass turned on to avoid
@@ -1031,13 +1041,11 @@ blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id) {struct request_queue *q; - q =blk_alloc_queue_node (GFP_KERNEL , node_id,NULL ); + q =blk_alloc_queue_node (GFP_KERNEL , node_id, lock);if (!q)return NULL ; q->request_fn = rfn; -if (lock) - q->queue_lock = lock;if (blk_init_allocated_queue (q) <0 ) {blk_cleanup_queue (q);return NULL ;
drivers/block/drbd/drbd_main.c +1 -2
@@ -2816,7 +2816,7 @@ enum drbd_ret_code drbd_create_device(struct drbd_config_context *adm_ctx, unsigdrbd_init_set_defaults (device); - q =blk_alloc_queue (GFP_KERNEL ); + q =blk_alloc_queue_node (GFP_KERNEL ,NUMA_NO_NODE , &resource->req_lock);if (!q) goto out_no_q; device->rq_queue = q;
@@ -2848,7 +2848,6 @@ enum drbd_ret_code drbd_create_device(struct drbd_config_context *adm_ctx, unsig/* Setting the max_hw_sectors to an odd value of 8kibyte here This triggers a max_bio_size message upon first attach or connect */ blk_queue_max_hw_sectors (q,DRBD_MAX_BIO_SIZE_SAFE >>8 ); - q->queue_lock = &resource->req_lock; device->md_io.page =alloc_page (GFP_KERNEL );if (!device->md_io.page)
drivers/block/umem.c +3 -4
@@ -888,13 +888,14 @@ static int mm_pci_probe(struct pci_dev *dev, const struct pci_device_id *id) card->Active =-1 ;/* no page is active */ card->bio =NULL ; card->biotail = &card->bio; +spin_lock_init (&card->lock); - card->queue =blk_alloc_queue (GFP_KERNEL ); + card->queue =blk_alloc_queue_node (GFP_KERNEL ,NUMA_NO_NODE , + &card->lock);if (!card->queue) goto failed_alloc;blk_queue_make_request (card->queue, mm_make_request); - card->queue->queue_lock = &card->lock; card->queue->queuedata = card;tasklet_init (&card->tasklet, process_page, (unsigned long )card);
@@ -968,8 +969,6 @@ static int mm_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)dev_printk (KERN_INFO , &card->dev->dev,"Window size %d bytes, IRQ %d\n" , data, dev->irq); -spin_lock_init (&card->lock); -pci_set_drvdata (dev, card);if (pci_write_cmd !=0x0F )/* If not Memory Write & Invalidate */
Signature · 1 line
2.16.1
PATCH v3 3/3 block: Fix a race between request queue removal and the block cgroup controller
In reply to Bart Van AsscheAvoid that the following race can occur:
blk_cleanup_queue() blkcg_print_blkgs()
spin_lock_irq(lock) (1) spin_lock_irq(blkg->q->queue_lock) (2,5)
q->queue_lock = &q->__queue_lock (3)
spin_unlock_irq(lock) (4)
spin_unlock_irq(blkg->q->queue_lock) (6)(1) take driver lock;
(2) busy loop for driver lock;
(3) override driver lock with internal lock;
(4) unlock driver lock;
(5) can take driver lock now;
(6) but unlock internal lock.
This change is safe because only the SCSI core and the NVME core keep a reference on a request queue after having called blk_cleanup_queue(). Neither driver accesses any of the removed data structures between its blk_cleanup_queue() and blk_put_queue() calls.
--- block/blk-core.c | 31 +++++++++++++++++++++++++++++++ block/blk-sysfs.c | 7 ------- 2 files changed, 31 insertions(+), 7 deletions(-)
block/blk-core.c +31 -0
@@ -719,6 +719,37 @@ void blk_cleanup_queue(struct request_queue *q)del_timer_sync (&q->backing_dev_info->laptop_mode_wb_timer);blk_sync_queue (q); +/* +* I/O scheduler exit is only safe after the sysfs scheduler attribute +* has been removed. +*/ +WARN_ON_ONCE (q->kobj.state_in_sysfs); + +/* +* Since the I/O scheduler exit code may access cgroup information, +* perform I/O scheduler exit before disassociating from the block +* cgroup controller. +*/ +if (q->elevator) { +ioc_clear_queue (q); +elevator_exit (q, q->elevator); + q->elevator =NULL ; + } + +/* +* Remove all references to @q from the block cgroup controller before +* restoring @q->queue_lock to avoid that restoring this pointer causes +* e.g. blkcg_print_blkgs() to crash. +*/ +blkcg_exit_queue (q); + +/* +* Since the cgroup code may dereference the @q->backing_dev_info +* pointer, only decrease its reference count after having removed the +* association with the block cgroup controller. +*/ +bdi_put (q->backing_dev_info); +if (q->mq_ops)blk_mq_free_queue (q);percpu_ref_exit (&q->q_usage_counter);
block/blk-sysfs.c +0 -7
@@ -798,13 +798,6 @@ static void __blk_release_queue(struct work_struct *work)if (test_bit (QUEUE_FLAG_POLL_STATS , &q->queue_flags))blk_stat_remove_callback (q, q->poll_cb);blk_stat_free_callback (q->poll_cb); -bdi_put (q->backing_dev_info); -blkcg_exit_queue (q); - -if (q->elevator) { -ioc_clear_queue (q); -elevator_exit (q, q->elevator); - }blk_free_queue_stats (q->stats);
Signature · 1 line
2.16.1
Quoted 3 lines · Bart Van Assche
Recently Joseph Qi identified races between the block cgroup code and request queue initialization and cleanup. This patch series address these races. Please consider these patches for kernel v4.17.
Hello Joseph,
Can you add your Tested-by or Reviewed-by to the patches in this series?
Thanks,
Bart.
Re: [PATCH v3 3/3] block: Fix a race between request queue removal and the block cgroup controller
In reply to Bart Van AsscheHi Bart,
Sorry for the delayed response since I was on holiday.
Quoted 33 lines · Bart Van Assche
Avoid that the following race can occur:
blk_cleanup_queue()blkcg_print_blkgs()
spin_lock_irq(lock)(1)spin_lock_irq(blkg->q->queue_lock)(2,5)q->queue_lock = &q->__queue_lock (3) spin_unlock_irq(lock) (4) spin_unlock_irq(blkg->q->queue_lock) (6)(1) take driver lock;
(2) busy loop for driver lock;
(3) override driver lock with internal lock;
(4) unlock driver lock;
(5) can take driver lock now;
(6) but unlock internal lock.This change is safe because only the SCSI core and the NVME core keep a reference on a request queue after having called
blk_cleanup_queue(). Neither driver accesses any of the removed data structures between itsblk_cleanup_queue()andblk_put_queue()calls.Reported-by Joseph Qi <[email protected]>Signed-off-by Bart Van Assche <[email protected]>Cc Jan Kara <[email protected]>--- block/blk-core.c | 31 +++++++++++++++++++++++++++++++ block/blk-sysfs.c | 7 ------- 2 files changed, 31 insertions(+), 7 deletions(-)block/blk-core.c +0
-0
block/blk-core.c void blk_cleanup_queue(struct request_queue *q) · line 727
Quoted 9 lines
block/blk-core.c +6
-0@@ -719,6 +719,37 @@ void blk_cleanup_queue(struct request_queue *q)del_timer_sync (&q->backing_dev_info->laptop_mode_wb_timer);blk_sync_queue (q); +/* +* I/O scheduler exit is only safe after the sysfs scheduler attribute +* has been removed. +*/ +WARN_ON_ONCE (q->kobj.state_in_sysfs); +
I notice that several devices such as loop and zram will call blk_cleanup_queue before del_gendisk, so it will hit this warning. Is this normal?
Thanks,
Joseph
Quoted 47 lines
block/blk-core.c +25
-0+/* +* Since the I/O scheduler exit code may access cgroup information, +* perform I/O scheduler exit before disassociating from the block +* cgroup controller. +*/ +if (q->elevator) { +ioc_clear_queue (q); +elevator_exit (q, q->elevator); + q->elevator =NULL ; + } + +/* +* Remove all references to @q from the block cgroup controller before +* restoring @q->queue_lock to avoid that restoring this pointer causes +* e.g. blkcg_print_blkgs() to crash. +*/ +blkcg_exit_queue (q); + +/* +* Since the cgroup code may dereference the @q->backing_dev_info +* pointer, only decrease its reference count after having removed the +* association with the block cgroup controller. +*/ +bdi_put (q->backing_dev_info); +if (q->mq_ops)blk_mq_free_queue (q);percpu_ref_exit (&q->q_usage_counter);block/blk-sysfs.c +0
-7@@ -798,13 +798,6 @@ static void __blk_release_queue(struct work_struct *work)if (test_bit (QUEUE_FLAG_POLL_STATS , &q->queue_flags))blk_stat_remove_callback (q, q->poll_cb);blk_stat_free_callback (q->poll_cb); -bdi_put (q->backing_dev_info); -blkcg_exit_queue (q); - -if (q->elevator) { -ioc_clear_queue (q); -elevator_exit (q, q->elevator); - }blk_free_queue_stats (q->stats);
Re: [PATCH v3 3/3] block: Fix a race between request queue removal and the block cgroup controller
Quoted 41 lines · Joseph Qi
Hi Bart,
Sorry for the delayed response since I was on holiday.
Quoted 36 lines · Bart Van Assche
Avoid that the following race can occur:
blk_cleanup_queue()blkcg_print_blkgs()
spin_lock_irq(lock)(1)spin_lock_irq(blkg->q->queue_lock)(2,5)q->queue_lock = &q->__queue_lock (3) spin_unlock_irq(lock) (4) spin_unlock_irq(blkg->q->queue_lock) (6)(1) take driver lock;
(2) busy loop for driver lock;
(3) override driver lock with internal lock;
(4) unlock driver lock;
(5) can take driver lock now;
(6) but unlock internal lock.This change is safe because only the SCSI core and the NVME core keep a reference on a request queue after having called
blk_cleanup_queue(). Neither driver accesses any of the removed data structures between itsblk_cleanup_queue()andblk_put_queue()calls.Reported-by Joseph Qi <[email protected]>Signed-off-by Bart Van Assche <[email protected]>Cc Jan Kara <[email protected]>--- block/blk-core.c | 31 +++++++++++++++++++++++++++++++ block/blk-sysfs.c | 7 ------- 2 files changed, 31 insertions(+), 7 deletions(-)block/blk-core.c +0
-0@@ -719,6 +719,37 @@ void blk_cleanup_queue(struct request_queue *q)del_timer_sync (&q->backing_dev_info->laptop_mode_wb_timer);blk_sync_queue (q);
Quoted 9 lines
block/blk-core.c void blk_cleanup_queue(struct request_queue *q) · line 727
Quoted 6 lines
block/blk-core.c +6
-0+/* +* I/O scheduler exit is only safe after the sysfs scheduler attribute +* has been removed. +*/ +WARN_ON_ONCE (q->kobj.state_in_sysfs); +I notice that several devices such as loop and zram will call
blk_cleanup_queuebeforedel_gendisk, so it will hit this warning. Is this normal?
In theory, it should be a bug to call del_gendisk() after blk_cleanup_queue() since dirty pages can't be flushed to device any more after queue is cleaned up.
Thanks,
Ming
Re: [PATCH v3 3/3] block: Fix a race between request queue removal and the block cgroup controller
In reply to Joseph QiT24gVGh1LCAyMDE4LTAyLTIyIGF0IDEwOjI1ICswODAwLCBKb3NlcGggUWkgd3JvdGU6DQo+IEkg bm90aWNlIHRoYXQgc2V2ZXJhbCBkZXZpY2VzIHN1Y2ggYXMgbG9vcCBhbmQgenJhbSB3aWxsIGNh bGwNCj4gYmxrX2NsZWFudXBfcXVldWUgYmVmb3JlIGRlbF9nZW5kaXNrLCBzbyBpdCB3aWxsIGhp dCB0aGlzIHdhcm5pbmcuIElzDQo+IHRoaXMgbm9ybWFsPw0KDQpIZWxsbyBKb3NlcGgsDQoNClNp bmNlIHRoZSBkaXNrIG9iamVjdCBoYXMgYSByZWZlcmVuY2UgdG8gdGhlIHF1ZXVlIEkgYWdyZWUg d2l0aCBNaW5nIHRoYXQgaXQncw0Kd3JvbmcgdG8gY2FsbCBibGtfY2xlYW51cF9xdWV1ZSgpIGJl Zm9yZSBkZWxfZ2VuZGlzaygpLiBBbnl3YXksIEkgd2lsbCB2ZXJpZnkNCndoZXRoZXIgdGhlcmUg YXJlIGFueSBibG9jayBkcml2ZXJzIGluIHdoaWNoIHRoZXNlIGNhbGxzIGhhdmUgdG8gYmUgc3dh cHBlZC4NCg0KQmFydC4=