[PATCH 03/11] bdi: Mark congested->bdi as internal

2017-03-13 Thread Jan Kara
congested->bdi pointer is used only to be able to remove congested
structure from bdi->cgwb_congested_tree on structure release. Moreover
the pointer can become NULL when we unregister the bdi. Rename the field
to __bdi and add a comment to make it more explicit this is internal
stuff of memcg writeback code and people should not use the field as
such use will be likely race prone.

We do not bother with converting congested->bdi to a proper refcounted
reference. It will be slightly ugly to special-case bdi->wb.congested to
avoid effectively a cyclic reference of bdi to itself and the reference
gets cleared from bdi_unregister() making it impossible to reference
a freed bdi.

Acked-by: Tejun Heo 
Signed-off-by: Jan Kara 
---
 include/linux/backing-dev-defs.h |  4 +++-
 mm/backing-dev.c | 10 +-
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/include/linux/backing-dev-defs.h b/include/linux/backing-dev-defs.h
index ad955817916d..8fb3dcdebc80 100644
--- a/include/linux/backing-dev-defs.h
+++ b/include/linux/backing-dev-defs.h
@@ -54,7 +54,9 @@ struct bdi_writeback_congested {
atomic_t refcnt;/* nr of attached wb's and blkg */
 
 #ifdef CONFIG_CGROUP_WRITEBACK
-   struct backing_dev_info *bdi;   /* the associated bdi */
+   struct backing_dev_info *__bdi; /* the associated bdi, set to NULL
+* on bdi unregistration. For memcg-wb
+* internal use only! */
int blkcg_id;   /* ID of the associated blkcg */
struct rb_node rb_node; /* on bdi->cgwb_congestion_tree */
 #endif
diff --git a/mm/backing-dev.c b/mm/backing-dev.c
index c6f2a37028c2..12408f86783c 100644
--- a/mm/backing-dev.c
+++ b/mm/backing-dev.c
@@ -438,7 +438,7 @@ wb_congested_get_create(struct backing_dev_info *bdi, int 
blkcg_id, gfp_t gfp)
return NULL;
 
atomic_set(_congested->refcnt, 0);
-   new_congested->bdi = bdi;
+   new_congested->__bdi = bdi;
new_congested->blkcg_id = blkcg_id;
goto retry;
 
@@ -466,10 +466,10 @@ void wb_congested_put(struct bdi_writeback_congested 
*congested)
}
 
/* bdi might already have been destroyed leaving @congested unlinked */
-   if (congested->bdi) {
+   if (congested->__bdi) {
rb_erase(>rb_node,
->bdi->cgwb_congested_tree);
-   congested->bdi = NULL;
+>__bdi->cgwb_congested_tree);
+   congested->__bdi = NULL;
}
 
spin_unlock_irqrestore(_lock, flags);
@@ -752,7 +752,7 @@ static void cgwb_bdi_exit(struct backing_dev_info *bdi)
rb_entry(rbn, struct bdi_writeback_congested, rb_node);
 
rb_erase(rbn, >cgwb_congested_tree);
-   congested->bdi = NULL;  /* mark @congested unlinked */
+   congested->__bdi = NULL;/* mark @congested unlinked */
}
spin_unlock_irq(_lock);
 }
-- 
2.10.2



[PATCH 03/11] bdi: Mark congested->bdi as internal

2017-03-06 Thread Jan Kara
congested->bdi pointer is used only to be able to remove congested
structure from bdi->cgwb_congested_tree on structure release. Moreover
the pointer can become NULL when we unregister the bdi. Rename the field
to __bdi and add a comment to make it more explicit this is internal
stuff of memcg writeback code and people should not use the field as
such use will be likely race prone.

We do not bother with converting congested->bdi to a proper refcounted
reference. It will be slightly ugly to special-case bdi->wb.congested to
avoid effectively a cyclic reference of bdi to itself and the reference
gets cleared from bdi_unregister() making it impossible to reference
a freed bdi.

Acked-by: Tejun Heo 
Signed-off-by: Jan Kara 
---
 include/linux/backing-dev-defs.h |  4 +++-
 mm/backing-dev.c | 10 +-
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/include/linux/backing-dev-defs.h b/include/linux/backing-dev-defs.h
index ad955817916d..8fb3dcdebc80 100644
--- a/include/linux/backing-dev-defs.h
+++ b/include/linux/backing-dev-defs.h
@@ -54,7 +54,9 @@ struct bdi_writeback_congested {
atomic_t refcnt;/* nr of attached wb's and blkg */
 
 #ifdef CONFIG_CGROUP_WRITEBACK
-   struct backing_dev_info *bdi;   /* the associated bdi */
+   struct backing_dev_info *__bdi; /* the associated bdi, set to NULL
+* on bdi unregistration. For memcg-wb
+* internal use only! */
int blkcg_id;   /* ID of the associated blkcg */
struct rb_node rb_node; /* on bdi->cgwb_congestion_tree */
 #endif
diff --git a/mm/backing-dev.c b/mm/backing-dev.c
index 6d861d090e9f..fec27da14aea 100644
--- a/mm/backing-dev.c
+++ b/mm/backing-dev.c
@@ -438,7 +438,7 @@ wb_congested_get_create(struct backing_dev_info *bdi, int 
blkcg_id, gfp_t gfp)
return NULL;
 
atomic_set(_congested->refcnt, 0);
-   new_congested->bdi = bdi;
+   new_congested->__bdi = bdi;
new_congested->blkcg_id = blkcg_id;
goto retry;
 
@@ -466,10 +466,10 @@ void wb_congested_put(struct bdi_writeback_congested 
*congested)
}
 
/* bdi might already have been destroyed leaving @congested unlinked */
-   if (congested->bdi) {
+   if (congested->__bdi) {
rb_erase(>rb_node,
->bdi->cgwb_congested_tree);
-   congested->bdi = NULL;
+>__bdi->cgwb_congested_tree);
+   congested->__bdi = NULL;
}
 
spin_unlock_irqrestore(_lock, flags);
@@ -698,7 +698,7 @@ static void cgwb_bdi_destroy(struct backing_dev_info *bdi)
rb_entry(rbn, struct bdi_writeback_congested, rb_node);
 
rb_erase(rbn, >cgwb_congested_tree);
-   congested->bdi = NULL;  /* mark @congested unlinked */
+   congested->__bdi = NULL;/* mark @congested unlinked */
}
 
spin_unlock_irq(_lock);
-- 
2.10.2