Skip to contentSkip to conversation

block: Fixes for bdi handling

PATCH Superseded Jan Kara 7 participants 15 messages Mar 8, 2017 – Mar 9, 2017

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jan Kara

author
#0 lore raw

cover block: Fixes for bdi handling

Hi!

patches in this series fix the most urgent bugs that were introduced by commit 165a5e22fafb "block: Move bdi_unregister() to del_gendisk()" and by 0dba1314d4f8 "scsi, block: fix duplicate bdi name registration crashes". In fact before these commits we had a different set of problems in the code but they were less visible :).

I'm still waiting for test confirmation from Omar and Arthur Marsh who reported issues but I'm not able to hit any problem anymore in my testing. I think it would be nice to get the patches to rc2 so to speed up things I'm posting the patches now so that review can happen in parallel with the testing.

Other BDI handling fixes I have in my queue can wait a bit more since they are either theoretical or long-standing issues. So I'll repost them once these four are sorted out.

Honza

author
#1 lore raw

PATCH 1/4 block: Allow bdi re-registration

SCSI can call device_add_disk() several times for one request queue when a device in unbound and bound, creating new gendisk each time. This will lead to bdi being repeatedly registered and unregistered. This was not a big problem until commit 165a5e22fafb "block: Move bdi_unregister() to del_gendisk()" since bdi was only registered repeatedly (bdi_register() handles repeated calls fine, only we ended up leaking reference to gendisk due to overwriting bdi->owner) but unregistered only in blk_cleanup_queue() which didn't get called repeatedly. After 165a5e22fafb we were doing correct bdi_register() - bdi_unregister() cycles however bdi_unregister() is not prepared for it. So make sure bdi_unregister() cleans up bdi in such a way that it is prepared for a possible following bdi_register() call.

An easy way to provoke this behavior is to enable CONFIG_DEBUG_TEST_DRIVER_REMOVE and use scsi_debug driver to create a scsi disk which immediately hangs without this fix.

Fixes 165a5e22fafb127ecb5914e12e8c32a1f0d3f820
---
 mm/backing-dev.c | 7 +++++++
 1 file changed, 7 insertions(+)
mm/backing-dev.c +7 -0
index 6d861d090e9f..6ac932210f56 100644
@@ -710,6 +710,11 @@ static void cgwb_bdi_destroy(struct backing_dev_info *bdi)
 	 */
 	atomic_dec(&bdi->usage_cnt);
 	wait_event(cgwb_release_wait, !atomic_read(&bdi->usage_cnt));
+	/*
+	 * Grab back our reference so that we hold it when @bdi gets
+	 * re-registered.
+	 */
+	atomic_inc(&bdi->usage_cnt);
 }
 
 /**
@@ -857,6 +862,8 @@ int bdi_register_owner(struct backing_dev_info *bdi, struct device *owner)
 			MINOR(owner->devt));
 	if (rc)
 		return rc;
+	/* Leaking owner reference... */
+	WARN_ON(bdi->owner);
 	bdi->owner = owner;
 	get_device(owner);
 	return 0;
Signature · 1 line
2.10.2
#2 lore raw

Re: [PATCH 1/4] block: Allow bdi re-registration

Hello,

mm/backing-dev.c static void cgwb_bdi_destroy(struct backing_dev_info *bdi) · line 717

Quoted 9 lines · Jan Kara
mm/backing-dev.c +5 -0
@@ -710,6 +710,11 @@ static void cgwb_bdi_destroy(struct backing_dev_info *bdi)
 	 */
 	atomic_dec(&bdi->usage_cnt);
 	wait_event(cgwb_release_wait, !atomic_read(&bdi->usage_cnt));
+	/*
+	 * Grab back our reference so that we hold it when @bdi gets
+	 * re-registered.
+	 */
+	atomic_inc(&bdi->usage_cnt);

So, this is more re-initializing the ref to the initial state so that it can be re-used, right? Maybe ATOMIC_INIT() is a better choice here just to clarify what's going on?

Thanks.

Signature · 1 line
tejun
maintainer
#3 lore raw

Re: [PATCH 1/4] block: Allow bdi re-registration

Quoted 19 lines · Tejun Heo

Hello,

mm/backing-dev.c static void cgwb_bdi_destroy(struct backing_dev_info *bdi) · line 717

Quoted 12 lines · Jan Kara
(quoted diff) +2 -1
@@ -710,6 +710,11 @@ static void cgwb_bdi_destroy(struct
 backing_dev_info *bdi)
 	 */
 	atomic_dec(&bdi->usage_cnt);
 	wait_event(cgwb_release_wait, !atomic_read(&bdi
->usage_cnt));
+	/*
+	 * Grab back our reference so that we hold it when @bdi

gets

mm/backing-dev.c +3 -0
+	 * re-registered.
+	 */
+	atomic_inc(&bdi->usage_cnt);

So, this is more re-initializing the ref to the initial state so that it can be re-used, right? Maybe ATOMIC_INIT() is a better choice here just to clarify what's going on?

Seconded. Eventually this is going to get converted to a refcount_t and it will dump a spurious warning on the 0->1 transition. We can avoid that by making this a proper initialization.

James

author
#4 lore raw

Re: [PATCH 1/4] block: Allow bdi re-registration

In reply to Tejun Heo
Quoted 16 lines · Tejun Heo

Hello,

mm/backing-dev.c static void cgwb_bdi_destroy(struct backing_dev_info *bdi) · line 717

Quoted 9 lines · Jan Kara
mm/backing-dev.c +5 -0
@@ -710,6 +710,11 @@ static void cgwb_bdi_destroy(struct backing_dev_info *bdi)
 	 */
 	atomic_dec(&bdi->usage_cnt);
 	wait_event(cgwb_release_wait, !atomic_read(&bdi->usage_cnt));
+	/*
+	 * Grab back our reference so that we hold it when @bdi gets
+	 * re-registered.
+	 */
+	atomic_inc(&bdi->usage_cnt);

So, this is more re-initializing the ref to the initial state so that it can be re-used, right? Maybe ATOMIC_INIT() is a better choice here just to clarify what's going on?

OK, I was somewhat undecided between these two option but you and James are probably right that re-init is clearer.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author
#5 lore raw

PATCH 2/4 bdi: Fix use-after-free in wb_congested_put()

In reply to Jan Kara

bdi_writeback_congested structures get created for each blkcg and bdi regardless whether bdi is registered or not. When they are created in unregistered bdi and the request queue (and thus bdi) is then destroyed while blkg still holds reference to bdi_writeback_congested structure, this structure will be referencing freed bdi and last wb_congested_put() will try to remove the structure from already freed bdi.

With commit 165a5e22fafb "block: Move bdi_unregister() to del_gendisk()", SCSI started to destroy bdis without calling bdi_unregister() first (previously it was calling bdi_unregister() even for unregistered bdis) and thus the code detaching bdi_writeback_congested in cgwb_bdi_destroy() was not triggered and we started hitting this use-after-free bug. It is enough to boot a KVM instance with virtio-scsi device to trigger this behavior.

Fix the problem by detaching bdi_writeback_congested structures in bdi_exit() instead of bdi_unregister(). This is also more logical as they can get attached to bdi regardless whether it ever got registered or not.

Fixes 165a5e22fafb127ecb5914e12e8c32a1f0d3f820
---
 mm/backing-dev.c | 36 +++++++++++++++++++++---------------
 1 file changed, 21 insertions(+), 15 deletions(-)
mm/backing-dev.c +21 -15
index 6ac932210f56..c6f2a37028c2 100644
@@ -683,30 +683,18 @@ static int cgwb_bdi_init(struct backing_dev_info *bdi)
 static void cgwb_bdi_destroy(struct backing_dev_info *bdi)
 {
 	struct radix_tree_iter iter;
-	struct rb_node *rbn;
 	void **slot;
 
 	WARN_ON(test_bit(WB_registered, &bdi->wb.state));
 
 	spin_lock_irq(&cgwb_lock);
-
 	radix_tree_for_each_slot(slot, &bdi->cgwb_tree, &iter, 0)
 		cgwb_kill(*slot);
-
-	while ((rbn = rb_first(&bdi->cgwb_congested_tree))) {
-		struct bdi_writeback_congested *congested =
-			rb_entry(rbn, struct bdi_writeback_congested, rb_node);
-
-		rb_erase(rbn, &bdi->cgwb_congested_tree);
-		congested->bdi = NULL;	/* mark @congested unlinked */
-	}
-
 	spin_unlock_irq(&cgwb_lock);
 
 	/*
-	 * All cgwb's and their congested states must be shutdown and
-	 * released before returning.  Drain the usage counter to wait for
-	 * all cgwb's and cgwb_congested's ever created on @bdi.
+	 * All cgwb's must be shutdown and released before returning.  Drain
+	 * the usage counter to wait for all cgwb's ever created on @bdi.
 	 */
 	atomic_dec(&bdi->usage_cnt);
 	wait_event(cgwb_release_wait, !atomic_read(&bdi->usage_cnt));
@@ -754,6 +742,21 @@ void wb_blkcg_offline(struct blkcg *blkcg)
 	spin_unlock_irq(&cgwb_lock);
 }
 
+static void cgwb_bdi_exit(struct backing_dev_info *bdi)
+{
+	struct rb_node *rbn;
+
+	spin_lock_irq(&cgwb_lock);
+	while ((rbn = rb_first(&bdi->cgwb_congested_tree))) {
+		struct bdi_writeback_congested *congested =
+			rb_entry(rbn, struct bdi_writeback_congested, rb_node);
+
+		rb_erase(rbn, &bdi->cgwb_congested_tree);
+		congested->bdi = NULL;	/* mark @congested unlinked */
+	}
+	spin_unlock_irq(&cgwb_lock);
+}
+
 #else	/* CONFIG_CGROUP_WRITEBACK */
 
 static int cgwb_bdi_init(struct backing_dev_info *bdi)
@@ -774,7 +777,9 @@ static int cgwb_bdi_init(struct backing_dev_info *bdi)
 	return 0;
 }
 
-static void cgwb_bdi_destroy(struct backing_dev_info *bdi)
+static void cgwb_bdi_destroy(struct backing_dev_info *bdi) { }
+
+static void cgwb_bdi_exit(struct backing_dev_info *bdi)
 {
 	wb_congested_put(bdi->wb_congested);
 }
@@ -905,6 +910,7 @@ static void bdi_exit(struct backing_dev_info *bdi)
 {
 	WARN_ON_ONCE(bdi->dev);
 	wb_exit(&bdi->wb);
+	cgwb_bdi_exit(bdi);
 }
 
 static void release_bdi(struct kref *ref)
Signature · 1 line
2.10.2
Acked-by on v1 2/4
#6 lore raw

Re: [PATCH 2/4] bdi: Fix use-after-free in wb_congested_put()

Quoted 15 lines · Jan Kara

bdi_writeback_congested structures get created for each blkcg and bdi regardless whether bdi is registered or not. When they are created in unregistered bdi and the request queue (and thus bdi) is then destroyed while blkg still holds reference to bdi_writeback_congested structure, this structure will be referencing freed bdi and last wb_congested_put() will try to remove the structure from already freed bdi.

With commit 165a5e22fafb "block: Move bdi_unregister() to del_gendisk()", SCSI started to destroy bdis without calling bdi_unregister() first (previously it was calling bdi_unregister() even for unregistered bdis) and thus the code detaching bdi_writeback_congested in cgwb_bdi_destroy() was not triggered and we started hitting this use-after-free bug. It is enough to boot a KVM instance with virtio-scsi device to trigger this behavior.

Quoted 7 lines

Fix the problem by detaching bdi_writeback_congested structures in bdi_exit() instead of bdi_unregister(). This is also more logical as they can get attached to bdi regardless whether it ever got registered or not.

Fixes 165a5e22fafb127ecb5914e12e8c32a1f0d3f820

Thanks.

Signature · 1 line
tejun
author
#7 lore raw

PATCH 3/4 block: Make del_gendisk() safer for disks without queues

In reply to Jan Kara

Commit 165a5e22fafb "block: Move bdi_unregister() to del_gendisk()" added disk->queue dereference to del_gendisk(). Although del_gendisk() is not supposed to be called without disk->queue valid and blk_unregister_queue() warns in that case, this change will make it oops instead. Return to the old more robust behavior of just warning when del_gendisk() gets called for gendisk with disk->queue being NULL.

---
 block/genhd.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
block/genhd.c +10 -6
index b26a5ea115d0..94f323842b52 100644
@@ -681,12 +681,16 @@ void del_gendisk(struct gendisk *disk)
 	disk->flags &= ~GENHD_FL_UP;
 
 	sysfs_remove_link(&disk_to_dev(disk)->kobj, "bdi");
-	/*
-	 * Unregister bdi before releasing device numbers (as they can get
-	 * reused and we'd get clashes in sysfs).
-	 */
-	bdi_unregister(disk->queue->backing_dev_info);
-	blk_unregister_queue(disk);
+	if (disk->queue) {
+		/*
+		 * Unregister bdi before releasing device numbers (as they can
+		 * get reused and we'd get clashes in sysfs).
+		 */
+		bdi_unregister(disk->queue->backing_dev_info);
+		blk_unregister_queue(disk);
+	} else {
+		WARN_ON(1);
+	}
 	blk_unregister_region(disk_devt(disk), disk->minors);
 
 	part_stat_set_all(&disk->part0, 0);
Signature · 1 line
2.10.2
Acked-by on v1 3/4
#8 lore raw

Re: [PATCH 3/4] block: Make del_gendisk() safer for disks without queues

Quoted 9 lines · Jan Kara

Commit 165a5e22fafb "block: Move bdi_unregister() to del_gendisk()" added disk->queue dereference to del_gendisk(). Although del_gendisk() is not supposed to be called without disk->queue valid and blk_unregister_queue() warns in that case, this change will make it oops instead. Return to the old more robust behavior of just warning when del_gendisk() gets called for gendisk with disk->queue being NULL.

Thanks.

Signature · 1 line
tejun
author
#9 lore raw

PATCH 4/4 Revert "scsi, block: fix duplicate bdi name registration crashes"

In reply to Jan Kara

This reverts commit 0dba1314d4f81115dce711292ec7981d17231064. It causes leaking of device numbers for SCSI when SCSI registers multiple gendisks for one request_queue in succession. It can be easily reproduced using Omar's script [1] on kernel with CONFIG_DEBUG_TEST_DRIVER_REMOVE. Furthermore the protection provided by this commit is not needed anymore as the problem it was fixing got also fixed by commit 165a5e22fafb "block: Move bdi_unregister() to del_gendisk()".

[1]: http://marc.info/?l=linux-block&m=148554717109098&w=2

---
 block/blk-core.c       |  2 --
 block/genhd.c          | 21 ---------------------
 drivers/scsi/sd.c      | 41 ++++++++---------------------------------
 include/linux/blkdev.h |  1 -
 include/linux/genhd.h  |  8 --------
 5 files changed, 8 insertions(+), 65 deletions(-)
block/blk-core.c +0 -2
index 1086dac8724c..a76895c9776d 100644
@@ -578,8 +578,6 @@ void blk_cleanup_queue(struct request_queue *q)
 		q->queue_lock = &q->__queue_lock;
 	spin_unlock_irq(lock);
 
-	put_disk_devt(q->disk_devt);
-
 	/* @q is and will stay empty, shutdown and put */
 	blk_put_queue(q);
 }
block/genhd.c +0 -21
index 94f323842b52..a9c516a8b37d 100644
@@ -572,20 +572,6 @@ static void register_disk(struct device *parent, struct gendisk *disk)
 	disk_part_iter_exit(&piter);
 }
 
-void put_disk_devt(struct disk_devt *disk_devt)
-{
-	if (disk_devt && atomic_dec_and_test(&disk_devt->count))
-		disk_devt->release(disk_devt);
-}
-EXPORT_SYMBOL(put_disk_devt);
-
-void get_disk_devt(struct disk_devt *disk_devt)
-{
-	if (disk_devt)
-		atomic_inc(&disk_devt->count);
-}
-EXPORT_SYMBOL(get_disk_devt);
-
 /**
  * device_add_disk - add partitioning information to kernel list
  * @parent: parent device for the disk
@@ -626,13 +612,6 @@ void device_add_disk(struct device *parent, struct gendisk *disk)
 
 	disk_alloc_events(disk);
 
-	/*
-	 * Take a reference on the devt and assign it to queue since it
-	 * must not be reallocated while the bdi is registered
-	 */
-	disk->queue->disk_devt = disk->disk_devt;
-	get_disk_devt(disk->disk_devt);
-
 	/* Register BDI before referencing it from bdev */
 	bdi = disk->queue->backing_dev_info;
 	bdi_register_owner(bdi, disk_to_dev(disk));
drivers/scsi/sd.c +8 -33

81 lines not on this page · raw message

include/linux/blkdev.h +0 -1
index 796016e63c1d..5a7da607ca04 100644
@@ -435,7 +435,6 @@ struct request_queue {
 	struct delayed_work	delay_work;
 
 	struct backing_dev_info	*backing_dev_info;
-	struct disk_devt	*disk_devt;
 
 	/*
 	 * The queue owner gets to use this for whatever they like.
include/linux/genhd.h +0 -8
index a999d281a2f1..76f39754e7b0 100644
@@ -167,13 +167,6 @@ struct blk_integrity {
 };
 
 #endif	/* CONFIG_BLK_DEV_INTEGRITY */
-struct disk_devt {
-	atomic_t count;
-	void (*release)(struct disk_devt *disk_devt);
-};
-
-void put_disk_devt(struct disk_devt *disk_devt);
-void get_disk_devt(struct disk_devt *disk_devt);
 
 struct gendisk {
 	/* major, first_minor and minors are input parameters only,
@@ -183,7 +176,6 @@ struct gendisk {
 	int first_minor;
 	int minors;                     /* maximum number of minors, =1 for
                                          * disks that can't be partitioned. */
-	struct disk_devt *disk_devt;
 
 	char disk_name[DISK_NAME_LEN];	/* name of major driver */
 	char *(*devnode)(struct gendisk *gd, umode_t *mode);
Signature · 1 line
2.10.2
Acked-by on v1 4/4
#10 lore raw

Re: [PATCH 4/4] Revert "scsi, block: fix duplicate bdi name registration crashes"

Quoted 11 lines · Jan Kara

This reverts commit 0dba1314d4f81115dce711292ec7981d17231064. It causes leaking of device numbers for SCSI when SCSI registers multiple gendisks for one request_queue in succession. It can be easily reproduced using Omar's script [1] on kernel with CONFIG_DEBUG_TEST_DRIVER_REMOVE. Furthermore the protection provided by this commit is not needed anymore as the problem it was fixing got also fixed by commit 165a5e22fafb "block: Move bdi_unregister() to del_gendisk()".

[1]: http://marc.info/?l=linux-block&m=148554717109098&w=2

#11 lore raw
In reply to Jan Kara
Quoted 13 lines · Jan Kara

Hi!

patches in this series fix the most urgent bugs that were introduced by commit 165a5e22fafb "block: Move bdi_unregister() to del_gendisk()" and by 0dba1314d4f8 "scsi, block: fix duplicate bdi name registration crashes". In fact before these commits we had a different set of problems in the code but they were less visible :).

I'm still waiting for test confirmation from Omar and Arthur Marsh who reported issues but I'm not able to hit any problem anymore in my testing. I think it would be nice to get the patches to rc2 so to speed up things I'm posting the patches now so that review can happen in parallel with the testing.

Quoted 5 lines

Other BDI handling fixes I have in my queue can wait a bit more since they are either theoretical or long-standing issues. So I'll repost them once these four are sorted out.

Honza

Applying patches 1 and 2 indeed fixed my virtio-scsi problem. I'll apply the whole series and test that my sd/sr test cases still work.

Tested-by
#12 lore raw
Quoted 14 lines · Omar Sandoval
Quoted 13 lines · Jan Kara

Hi!

patches in this series fix the most urgent bugs that were introduced by commit 165a5e22fafb "block: Move bdi_unregister() to del_gendisk()" and by 0dba1314d4f8 "scsi, block: fix duplicate bdi name registration crashes". In fact before these commits we had a different set of problems in the code but they were less visible :).

I'm still waiting for test confirmation from Omar and Arthur Marsh who reported issues but I'm not able to hit any problem anymore in my testing. I think it would be nice to get the patches to rc2 so to speed up things I'm posting the patches now so that review can happen in parallel with the testing.

Quoted 8 lines
Quoted 5 lines

Other BDI handling fixes I have in my queue can wait a bit more since they are either theoretical or long-standing issues. So I'll repost them once these four are sorted out.

Honza

Applying patches 1 and 2 indeed fixed my virtio-scsi problem. I'll apply the whole series and test that my sd/sr test cases still work.

Yup, everything is working here. For the series:

maintainer
#13 lore raw
In reply to Jan Kara
Quoted 8 lines · Jan Kara

Hi!

patches in this series fix the most urgent bugs that were introduced by commit 165a5e22fafb "block: Move bdi_unregister() to del_gendisk()" and by 0dba1314d4f8 "scsi, block: fix duplicate bdi name registration crashes". In fact before these commits we had a different set of problems in the code but they were less visible :).

Quoted 8 lines

I'm still waiting for test confirmation from Omar and Arthur Marsh who reported issues but I'm not able to hit any problem anymore in my testing. I think it would be nice to get the patches to rc2 so to speed up things I'm posting the patches now so that review can happen in parallel with the testing.

Other BDI handling fixes I have in my queue can wait a bit more since they are either theoretical or long-standing issues. So I'll repost them once these four are sorted out.

Agree, we'll need to get this sorted for -rc2, thanks a lot for looking into it. I'll queue these up for testing as well, and hopefully ship them off tomorrow (or Friday, at the latest).

Signature · 1 line
Jens Axboe
#14 lore raw
In reply to Jan Kara

Jan Kara wrote on 09/03/17 03:18:

Quoted 13 lines

Hi!

patches in this series fix the most urgent bugs that were introduced by commit 165a5e22fafb "block: Move bdi_unregister() to del_gendisk()" and by 0dba1314d4f8 "scsi, block: fix duplicate bdi name registration crashes". In fact before these commits we had a different set of problems in the code but they were less visible :).

I'm still waiting for test confirmation from Omar and Arthur Marsh who reported issues but I'm not able to hit any problem anymore in my testing. I think it would be nice to get the patches to rc2 so to speed up things I'm posting the patches now so that review can happen in parallel with the testing.

Quoted 6 lines

Other BDI handling fixes I have in my queue can wait a bit more since they are either theoretical or long-standing issues. So I'll repost them once these four are sorted out.

Honza

Sorry for the delay in replying, I had to leave the kernel with all 4 patches applied rebuilding while I was at work and just booted it.

I've only done a kexec reboot so far but there were no problems - no errors in dmesg, all disks were recognised and all attempted mounts worked.

Thanks very much for the quick fix!

Arthur.