Skip to contentSkip to conversation

simplify bdi unregistation

PATCH In review Christoph Hellwig 2 participants 13 messages Oct 21, 2021 – Oct 27, 2021

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

Hi Andrew,

this series simplifies the BDI code to get tid of the magic auto-unregister feature that hid a recent block layer refcounting bug.

Diffstat:

 drivers/mtd/mtdcore.c |    1 +
 fs/super.c            |    3 +++
 include/linux/fs.h    |    1 +
 mm/backing-dev.c      |   17 +++++++----------
 4 files changed, 12 insertions(+), 10 deletions(-)
#1 lore raw

PATCH 1/5 mm: export bdi_unregister

To wind down the magic auto-unregister semantics we'll need to push this into modular code.

---
 mm/backing-dev.c | 1 +
 1 file changed, 1 insertion(+)
mm/backing-dev.c +1 -0
index 4a9d4e27d0d9b..8a46a0a4b72fa 100644
@@ -958,6 +958,7 @@ void bdi_unregister(struct backing_dev_info *bdi)
 		bdi->owner = NULL;
 	}
 }
+EXPORT_SYMBOL(bdi_unregister);
 
 static void release_bdi(struct kref *ref)
 {
Signature · 1 line
2.30.2
reviewer Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] mm: export bdi_unregister

Quoted 4 lines · Christoph Hellwig

To wind down the magic auto-unregister semantics we'll need to push this into modular code.

Looks good. Feel free to add:

Honza

Quoted 19 lines
---
 mm/backing-dev.c | 1 +
 1 file changed, 1 insertion(+)
mm/backing-dev.c +1 -0
index 4a9d4e27d0d9b..8a46a0a4b72fa 100644
@@ -958,6 +958,7 @@ void bdi_unregister(struct backing_dev_info *bdi)
 		bdi->owner = NULL;
 	}
 }
+EXPORT_SYMBOL(bdi_unregister);
 
 static void release_bdi(struct kref *ref)
 {
Signature · 1 line
2.30.2
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#3 lore raw

PATCH 2/5 mtd: call bdi_unregister explicitly

In reply to Christoph Hellwig

Call bdi_unregister explicitly instead of relying on the automatic unregistration.

---
 drivers/mtd/mtdcore.c | 1 +
 1 file changed, 1 insertion(+)
drivers/mtd/mtdcore.c +1 -0
index c8fd7f758938b..c904e23c82379 100644
@@ -2409,6 +2409,7 @@ static void __exit cleanup_mtd(void)
 	if (proc_mtd)
 		remove_proc_entry("mtd", NULL);
 	class_unregister(&mtd_class);
+	bdi_unregister(mtd_bdi);
 	bdi_put(mtd_bdi);
 	idr_destroy(&mtd_idr);
 }
Signature · 1 line
2.30.2
reviewer Reviewed-by on v1 2/5
#4 lore raw

Re: [PATCH 2/5] mtd: call bdi_unregister explicitly

Quoted 4 lines · Christoph Hellwig

Call bdi_unregister explicitly instead of relying on the automatic unregistration.

Looks good. Feel free to add:

Honza

Quoted 19 lines
---
 drivers/mtd/mtdcore.c | 1 +
 1 file changed, 1 insertion(+)
drivers/mtd/mtdcore.c +1 -0
index c8fd7f758938b..c904e23c82379 100644
@@ -2409,6 +2409,7 @@ static void __exit cleanup_mtd(void)
 	if (proc_mtd)
 		remove_proc_entry("mtd", NULL);
 	class_unregister(&mtd_class);
+	bdi_unregister(mtd_bdi);
 	bdi_put(mtd_bdi);
 	idr_destroy(&mtd_idr);
 }
Signature · 1 line
2.30.2
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#5 lore raw

PATCH 3/5 fs: explicitly unregister per-superblock BDIs

In reply to Christoph Hellwig

Add a new SB_I_ flag to mark superblocks that have an ephemeral bdi associated with them, an unregister it when the superblock is shut down.

---
 fs/super.c         | 3 +++
 include/linux/fs.h | 1 +
 2 files changed, 4 insertions(+)
fs/super.c +3 -0
index bcef3a6f4c4b5..3bfc0f8fbd5bc 100644
@@ -476,6 +476,8 @@ void generic_shutdown_super(struct super_block *sb)
 	spin_unlock(&sb_lock);
 	up_write(&sb->s_umount);
 	if (sb->s_bdi != &noop_backing_dev_info) {
+		if (sb->s_iflags & SB_I_PERSB_BDI)
+			bdi_unregister(sb->s_bdi);
 		bdi_put(sb->s_bdi);
 		sb->s_bdi = &noop_backing_dev_info;
 	}
@@ -1562,6 +1564,7 @@ int super_setup_bdi_name(struct super_block *sb, char *fmt, ...)
 	}
 	WARN_ON(sb->s_bdi != &noop_backing_dev_info);
 	sb->s_bdi = bdi;
+	sb->s_iflags |= SB_I_PERSB_BDI;
 
 	return 0;
 }
include/linux/fs.h +1 -0
index e7a633353fd20..226de651f52e6 100644
@@ -1443,6 +1443,7 @@ extern int send_sigurg(struct fown_struct *fown);
 #define SB_I_UNTRUSTED_MOUNTER		0x00000040
 
 #define SB_I_SKIP_SYNC	0x00000100	/* Skip superblock at global sync */
+#define SB_I_PERSB_BDI	0x00000200	/* has a per-sb bdi */
 
 /* Possible states of 'frozen' field */
 enum {
Signature · 1 line
2.30.2
reviewer Reviewed-by on v1 3/5
#6 lore raw

Re: [PATCH 3/5] fs: explicitly unregister per-superblock BDIs

Quoted 4 lines · Christoph Hellwig

Add a new SB_I_ flag to mark superblocks that have an ephemeral bdi associated with them, an unregister it when the superblock is shut down.

Looks good. Feel free to add:

Honza

Quoted 41 lines
---
 fs/super.c         | 3 +++
 include/linux/fs.h | 1 +
 2 files changed, 4 insertions(+)
fs/super.c +3 -0
index bcef3a6f4c4b5..3bfc0f8fbd5bc 100644
@@ -476,6 +476,8 @@ void generic_shutdown_super(struct super_block *sb)
 	spin_unlock(&sb_lock);
 	up_write(&sb->s_umount);
 	if (sb->s_bdi != &noop_backing_dev_info) {
+		if (sb->s_iflags & SB_I_PERSB_BDI)
+			bdi_unregister(sb->s_bdi);
 		bdi_put(sb->s_bdi);
 		sb->s_bdi = &noop_backing_dev_info;
 	}
@@ -1562,6 +1564,7 @@ int super_setup_bdi_name(struct super_block *sb, char *fmt, ...)
 	}
 	WARN_ON(sb->s_bdi != &noop_backing_dev_info);
 	sb->s_bdi = bdi;
+	sb->s_iflags |= SB_I_PERSB_BDI;
 
 	return 0;
 }
include/linux/fs.h +1 -0
index e7a633353fd20..226de651f52e6 100644
@@ -1443,6 +1443,7 @@ extern int send_sigurg(struct fown_struct *fown);
 #define SB_I_UNTRUSTED_MOUNTER		0x00000040
 
 #define SB_I_SKIP_SYNC	0x00000100	/* Skip superblock at global sync */
+#define SB_I_PERSB_BDI	0x00000200	/* has a per-sb bdi */
 
 /* Possible states of 'frozen' field */
 enum {
Signature · 1 line
2.30.2
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#7 lore raw

PATCH 4/5 mm: don't automatically unregister bdis

In reply to Christoph Hellwig

All BDI users now unregister explicitly.

---
 mm/backing-dev.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/backing-dev.c +1 -2
index 8a46a0a4b72fa..768e9ae489f66 100644
@@ -965,8 +965,7 @@ static void release_bdi(struct kref *ref)
 	struct backing_dev_info *bdi =
 			container_of(ref, struct backing_dev_info, refcnt);
 
-	if (test_bit(WB_registered, &bdi->wb.state))
-		bdi_unregister(bdi);
+	WARN_ON_ONCE(test_bit(WB_registered, &bdi->wb.state));
 	WARN_ON_ONCE(bdi->dev);
 	wb_exit(&bdi->wb);
 	kfree(bdi);
Signature · 1 line
2.30.2
reviewer Reviewed-by on v1 4/5
#8 lore raw

Re: [PATCH 4/5] mm: don't automatically unregister bdis

Quoted 3 lines · Christoph Hellwig

All BDI users now unregister explicitly.

Looks good. Feel free to add:

Honza

Quoted 21 lines
---
 mm/backing-dev.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
mm/backing-dev.c +1 -2
index 8a46a0a4b72fa..768e9ae489f66 100644
@@ -965,8 +965,7 @@ static void release_bdi(struct kref *ref)
 	struct backing_dev_info *bdi =
 			container_of(ref, struct backing_dev_info, refcnt);
 
-	if (test_bit(WB_registered, &bdi->wb.state))
-		bdi_unregister(bdi);
+	WARN_ON_ONCE(test_bit(WB_registered, &bdi->wb.state));
 	WARN_ON_ONCE(bdi->dev);
 	wb_exit(&bdi->wb);
 	kfree(bdi);
Signature · 1 line
2.30.2
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#9 lore raw

PATCH 5/5 mm: simplify bdi refcounting

In reply to Christoph Hellwig

Move grabbing and releasing the bdi refcount out of the common wb_init/wb_exit helpers into code that is only used for the non-default memcg driven bdi_writeback structures.

---
 mm/backing-dev.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
mm/backing-dev.c +5 -8
index 768e9ae489f66..5ccb250898083 100644
@@ -291,8 +291,6 @@ static int wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi,
 
 	memset(wb, 0, sizeof(*wb));
 
-	if (wb != &bdi->wb)
-		bdi_get(bdi);
 	wb->bdi = bdi;
 	wb->last_old_flush = jiffies;
 	INIT_LIST_HEAD(&wb->b_dirty);
@@ -316,7 +314,7 @@ static int wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi,
 
 	err = fprop_local_init_percpu(&wb->completions, gfp);
 	if (err)
-		goto out_put_bdi;
+		return err;
 
 	for (i = 0; i < NR_WB_STAT_ITEMS; i++) {
 		err = percpu_counter_init(&wb->stat[i], 0, gfp);
@@ -330,9 +328,6 @@ static int wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi,
 	while (i--)
 		percpu_counter_destroy(&wb->stat[i]);
 	fprop_local_destroy_percpu(&wb->completions);
-out_put_bdi:
-	if (wb != &bdi->wb)
-		bdi_put(bdi);
 	return err;
 }
 
@@ -373,8 +368,6 @@ static void wb_exit(struct bdi_writeback *wb)
 		percpu_counter_destroy(&wb->stat[i]);
 
 	fprop_local_destroy_percpu(&wb->completions);
-	if (wb != &wb->bdi->wb)
-		bdi_put(wb->bdi);
 }
 
 #ifdef CONFIG_CGROUP_WRITEBACK
@@ -397,6 +390,7 @@ static void cgwb_release_workfn(struct work_struct *work)
 	struct bdi_writeback *wb = container_of(work, struct bdi_writeback,
 						release_work);
 	struct blkcg *blkcg = css_to_blkcg(wb->blkcg_css);
+	struct backing_dev_info *bdi = wb->bdi;
 
 	mutex_lock(&wb->bdi->cgwb_release_mutex);
 	wb_shutdown(wb);
@@ -416,6 +410,7 @@ static void cgwb_release_workfn(struct work_struct *work)
 
 	percpu_ref_exit(&wb->refcnt);
 	wb_exit(wb);
+	bdi_put(bdi);
 	WARN_ON_ONCE(!list_empty(&wb->b_attached));
 	kfree_rcu(wb, rcu);
 }
@@ -497,6 +492,7 @@ static int cgwb_create(struct backing_dev_info *bdi,
 	INIT_LIST_HEAD(&wb->b_attached);
 	INIT_WORK(&wb->release_work, cgwb_release_workfn);
 	set_bit(WB_registered, &wb->state);
+	bdi_get(bdi);
 
 	/*
 	 * The root wb determines the registered state of the whole bdi and
@@ -528,6 +524,7 @@ static int cgwb_create(struct backing_dev_info *bdi,
 	goto out_put;
 
 err_fprop_exit:
+	bdi_put(bdi);
 	fprop_local_destroy_percpu(&wb->memcg_completions);
 err_ref_exit:
 	percpu_ref_exit(&wb->refcnt);
Signature · 1 line
2.30.2
reviewer Reviewed-by on v1 5/5
#10 lore raw

Re: [PATCH 5/5] mm: simplify bdi refcounting

Quoted 5 lines · Christoph Hellwig

Move grabbing and releasing the bdi refcount out of the common wb_init/wb_exit helpers into code that is only used for the non-default memcg driven bdi_writeback structures.

Can we perhaps add a comment to struct bdi_writeback definition (or maybe wb_init()?) mentioning that it holds a reference to 'bdi' if it is bdi_writeback struct for a cgroup? I don't see it mentioned anywhere and now that you've changed the code, it isn't that obvious from the code either... Otherwise the patch looks good so feel free to add:

Honza

Quoted 80 lines
---
 mm/backing-dev.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
mm/backing-dev.c +5 -8
index 768e9ae489f66..5ccb250898083 100644
@@ -291,8 +291,6 @@ static int wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi,
 
 	memset(wb, 0, sizeof(*wb));
 
-	if (wb != &bdi->wb)
-		bdi_get(bdi);
 	wb->bdi = bdi;
 	wb->last_old_flush = jiffies;
 	INIT_LIST_HEAD(&wb->b_dirty);
@@ -316,7 +314,7 @@ static int wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi,
 
 	err = fprop_local_init_percpu(&wb->completions, gfp);
 	if (err)
-		goto out_put_bdi;
+		return err;
 
 	for (i = 0; i < NR_WB_STAT_ITEMS; i++) {
 		err = percpu_counter_init(&wb->stat[i], 0, gfp);
@@ -330,9 +328,6 @@ static int wb_init(struct bdi_writeback *wb, struct backing_dev_info *bdi,
 	while (i--)
 		percpu_counter_destroy(&wb->stat[i]);
 	fprop_local_destroy_percpu(&wb->completions);
-out_put_bdi:
-	if (wb != &bdi->wb)
-		bdi_put(bdi);
 	return err;
 }
 
@@ -373,8 +368,6 @@ static void wb_exit(struct bdi_writeback *wb)
 		percpu_counter_destroy(&wb->stat[i]);
 
 	fprop_local_destroy_percpu(&wb->completions);
-	if (wb != &wb->bdi->wb)
-		bdi_put(wb->bdi);
 }
 
 #ifdef CONFIG_CGROUP_WRITEBACK
@@ -397,6 +390,7 @@ static void cgwb_release_workfn(struct work_struct *work)
 	struct bdi_writeback *wb = container_of(work, struct bdi_writeback,
 						release_work);
 	struct blkcg *blkcg = css_to_blkcg(wb->blkcg_css);
+	struct backing_dev_info *bdi = wb->bdi;
 
 	mutex_lock(&wb->bdi->cgwb_release_mutex);
 	wb_shutdown(wb);
@@ -416,6 +410,7 @@ static void cgwb_release_workfn(struct work_struct *work)
 
 	percpu_ref_exit(&wb->refcnt);
 	wb_exit(wb);
+	bdi_put(bdi);
 	WARN_ON_ONCE(!list_empty(&wb->b_attached));
 	kfree_rcu(wb, rcu);
 }
@@ -497,6 +492,7 @@ static int cgwb_create(struct backing_dev_info *bdi,
 	INIT_LIST_HEAD(&wb->b_attached);
 	INIT_WORK(&wb->release_work, cgwb_release_workfn);
 	set_bit(WB_registered, &wb->state);
+	bdi_get(bdi);
 
 	/*
 	 * The root wb determines the registered state of the whole bdi and
@@ -528,6 +524,7 @@ static int cgwb_create(struct backing_dev_info *bdi,
 	goto out_put;
 
 err_fprop_exit:
+	bdi_put(bdi);
 	fprop_local_destroy_percpu(&wb->memcg_completions);
 err_ref_exit:
 	percpu_ref_exit(&wb->refcnt);
Signature · 1 line
2.30.2
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#11 lore raw

Re: [PATCH 5/5] mm: simplify bdi refcounting

Quoted 5 lines · Jan Kara
Quoted 4 lines · Christoph Hellwig

Move grabbing and releasing the bdi refcount out of the common wb_init/wb_exit helpers into code that is only used for the non-default memcg driven bdi_writeback structures.

Quoted 7 lines
Quoted 1 line

Can we perhaps add a comment to struct bdi_writeback definition (or maybe wb_init()?) mentioning that it holds a reference to 'bdi' if it is bdi_writeback struct for a cgroup? I don't see it mentioned anywhere and now that you've changed the code, it isn't that obvious from the code either... Otherwise the patch looks good so feel free to add:

Like this?

include/linux/backing-dev-defs.h +3 -0
index 33207004cfded..a3d7dd1cc30a1 100644
@@ -103,6 +103,9 @@ struct wb_completion {
  * change as blkcg is disabled and enabled higher up in the hierarchy, a wb
  * is tested for blkcg after lookup and removed from index on mismatch so
  * that a new wb for the combination can be created.
+ *
+ * Each bdi_writeback that is no embedded into the backing_dev_info must hold
+ * a reference to the parent backing_dev_info.  See cgwb_create() for details.
  */
 struct bdi_writeback {
 	struct backing_dev_info *bdi;	/* our parent bdi */
reviewer
#12 lore raw

Re: [PATCH 5/5] mm: simplify bdi refcounting

Quoted 21 lines · Christoph Hellwig
Quoted 5 lines · Jan Kara
Quoted 4 lines · Christoph Hellwig

Move grabbing and releasing the bdi refcount out of the common wb_init/wb_exit helpers into code that is only used for the non-default memcg driven bdi_writeback structures.

Quoted 7 lines
Quoted 1 line

Can we perhaps add a comment to struct bdi_writeback definition (or maybe wb_init()?) mentioning that it holds a reference to 'bdi' if it is bdi_writeback struct for a cgroup? I don't see it mentioned anywhere and now that you've changed the code, it isn't that obvious from the code either... Otherwise the patch looks good so feel free to add:

Like this?

include/linux/backing-dev-defs.h +0 -0
index 33207004cfded..a3d7dd1cc30a1 100644

include/linux/backing-dev-defs.h struct wb_completion { · line 107

Quoted 5 lines
include/linux/backing-dev-defs.h +2 -0
@@ -103,6 +103,9 @@ struct wb_completion {
  * change as blkcg is disabled and enabled higher up in the hierarchy, a wb
  * is tested for blkcg after lookup and removed from index on mismatch so
  * that a new wb for the combination can be created.
+ *
+ * Each bdi_writeback that is no embedded into the backing_dev_info must hold
				 ^^^ not

include/linux/backing-dev-defs.h struct wb_completion { · line 109

Quoted 2 lines
include/linux/backing-dev-defs.h +1 -0
+ * a reference to the parent backing_dev_info.  See cgwb_create() for details.
  */

Otherwise looks nice. Thanks!

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR