diff mbox series

[5/5] mm: simplify bdi refcounting

Message ID 20211021124441.668816-6-hch@lst.de (mailing list archive)
State New
Headers show
Series [1/5] mm: export bdi_unregister | expand

Commit Message

Christoph Hellwig Oct. 21, 2021, 12:44 p.m. UTC
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.

Signed-off-by: Christoph Hellwig <hch@lst.de>
---
 mm/backing-dev.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

Comments

Jan Kara Oct. 22, 2021, 9:02 a.m. UTC | #1
On Thu 21-10-21 14:44:41, Christoph Hellwig wrote:
> 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.
> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>

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:

Reviewed-by: Jan Kara <jack@suse.cz>

								Honza


> ---
>  mm/backing-dev.c | 13 +++++--------
>  1 file changed, 5 insertions(+), 8 deletions(-)
> 
> diff --git a/mm/backing-dev.c b/mm/backing-dev.c
> index 768e9ae489f66..5ccb250898083 100644
> --- a/mm/backing-dev.c
> +++ b/mm/backing-dev.c
> @@ -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);
> -- 
> 2.30.2
>
Christoph Hellwig Oct. 27, 2021, 7:42 a.m. UTC | #2
On Fri, Oct 22, 2021 at 11:02:03AM +0200, Jan Kara wrote:
> On Thu 21-10-21 14:44:41, Christoph Hellwig wrote:
> > 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.
> > 
> > Signed-off-by: Christoph Hellwig <hch@lst.de>
> 
> 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?

diff --git a/include/linux/backing-dev-defs.h b/include/linux/backing-dev-defs.h
index 33207004cfded..a3d7dd1cc30a1 100644
--- a/include/linux/backing-dev-defs.h
+++ b/include/linux/backing-dev-defs.h
@@ -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 */
Jan Kara Oct. 27, 2021, 9:47 a.m. UTC | #3
On Wed 27-10-21 09:42:07, Christoph Hellwig wrote:
> On Fri, Oct 22, 2021 at 11:02:03AM +0200, Jan Kara wrote:
> > On Thu 21-10-21 14:44:41, Christoph Hellwig wrote:
> > > 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.
> > > 
> > > Signed-off-by: Christoph Hellwig <hch@lst.de>
> > 
> > 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?
> 
> diff --git a/include/linux/backing-dev-defs.h b/include/linux/backing-dev-defs.h
> index 33207004cfded..a3d7dd1cc30a1 100644
> --- a/include/linux/backing-dev-defs.h
> +++ b/include/linux/backing-dev-defs.h
> @@ -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

> + * a reference to the parent backing_dev_info.  See cgwb_create() for details.
>   */

Otherwise looks nice. Thanks!

								Honza
diff mbox series

Patch

diff --git a/mm/backing-dev.c b/mm/backing-dev.c
index 768e9ae489f66..5ccb250898083 100644
--- a/mm/backing-dev.c
+++ b/mm/backing-dev.c
@@ -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);