Skip to contentSkip to conversation

bdi: make sure congestion states are clear on free

PATCH In review Tejun Heo 4 participants 6 messages Feb 2, 2018 – May 30, 2018

Subsystems (5)
lore
Share & tools
lore

Tejun Heo

author
#0 lore raw

PATCH 1/2 bdi: make sure congestion states are clear on free

FUSE has a bug where it fails to clear congestion states if a connection gets aborted while congested, which can leave nr_wb_congested[] stuck until reboot causing wait_iff_congested() to wait spuriously.

While the bdi owner, FUSE, is primarily responsible for clearing congestion states before destroying bdi_writebacks, bdi layer can ensure that congestion states are not leaked beyond bdi_writeback lifecycle.

---
 include/linux/backing-dev.h |   14 +++++++++++++-
 mm/backing-dev.c            |    2 +-
 2 files changed, 14 insertions(+), 2 deletions(-)
include/linux/backing-dev.h +13 -1
@@ -220,6 +220,18 @@ static inline int bdi_sched_wait(void *w
 	return 0;
 }
 
+static inline void __wb_congested_free(struct bdi_writeback_congested *congested)
+{
+	/*
+	 * Make sure congestion states are cleared before freeing to avoid
+	 * nr_wb_congested() corruption which can lead to misbehaving
+	 * wait_iff_congested().
+	 */
+	clear_wb_congested(congested, BLK_RW_SYNC);
+	clear_wb_congested(congested, BLK_RW_ASYNC);
+	kfree(congested);
+}
+
 #ifdef CONFIG_CGROUP_WRITEBACK
 
 struct bdi_writeback_congested *
@@ -409,7 +421,7 @@ wb_congested_get_create(struct backing_d
 static inline void wb_congested_put(struct bdi_writeback_congested *congested)
 {
 	if (atomic_dec_and_test(&congested->refcnt))
-		kfree(congested);
+		__wb_congested_free(congested);
 }
 
 static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
mm/backing-dev.c +1 -1
@@ -509,7 +509,7 @@ void wb_congested_put(struct bdi_writeba
 	}
 
 	spin_unlock_irqrestore(&cgwb_lock, flags);
-	kfree(congested);
+	__wb_congested_free(congested);
 }
 
 static void cgwb_release_workfn(struct work_struct *work)
author
#1 lore raw

PATCH 2/2 FUSE: fix congested state leak on aborted connections

If a connection gets aborted while congested, FUSE can leave nr_wb_congested[] stuck until reboot causing wait_iff_congested() to wait spuriously which can lead to severe performance degradation.

The leak is caused by gating congestion state clearing with fc->connected test in request_end(). This was added way back in 2009 by 26c3679101db ("fuse: destroy bdi on umount"). While the commit description doesn't explain why the test was added, it most likely was to avoid dereferencing bdi after it got destroyed.

Since then, bdi lifetime rules have changed many times and now we're always guaranteed to have access to the bdi while the superblock is alive (fc->sb).

Drop fc->connected conditional to avoid leaking congestion states.

---
 fs/fuse/dev.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/fuse/dev.c +1 -2
@@ -381,8 +381,7 @@ static void request_end(struct fuse_conn
 		if (!fc->blocked && waitqueue_active(&fc->blocked_waitq))
 			wake_up(&fc->blocked_waitq);
 
-		if (fc->num_background == fc->congestion_threshold &&
-		    fc->connected && fc->sb) {
+		if (fc->num_background == fc->congestion_threshold && fc->sb) {
 			clear_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC);
 			clear_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC);
 		}
reviewer Acked-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] FUSE: fix congested state leak on aborted connections

Quoted 14 lines · Tejun Heo

If a connection gets aborted while congested, FUSE can leave nr_wb_congested[] stuck until reboot causing wait_iff_congested() to wait spuriously which can lead to severe performance degradation.

The leak is caused by gating congestion state clearing with fc->connected test in request_end(). This was added way back in 2009 by 26c3679101db ("fuse: destroy bdi on umount"). While the commit description doesn't explain why the test was added, it most likely was to avoid dereferencing bdi after it got destroyed.

Since then, bdi lifetime rules have changed many times and now we're always guaranteed to have access to the bdi while the superblock is alive (fc->sb).

Quoted 8 lines

Drop fc->connected conditional to avoid leaking congestion states.

Yeah, this should be fine AFAICT but my knowledge of FUSE is very cursory. Anyway:

Honza

Quoted 16 lines
---
 fs/fuse/dev.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/fuse/dev.c +1 -2
@@ -381,8 +381,7 @@ static void request_end(struct fuse_conn
 		if (!fc->blocked && waitqueue_active(&fc->blocked_waitq))
 			wake_up(&fc->blocked_waitq);
 
-		if (fc->num_background == fc->congestion_threshold &&
-		    fc->connected && fc->sb) {
+		if (fc->num_background == fc->congestion_threshold && fc->sb) {
 			clear_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC);
 			clear_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC);
 		}
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#3 lore raw

Re: [PATCH 2/2] FUSE: fix congested state leak on aborted connections

Quoted 24 lines · Jan Kara
Quoted 14 lines · Tejun Heo

If a connection gets aborted while congested, FUSE can leave nr_wb_congested[] stuck until reboot causing wait_iff_congested() to wait spuriously which can lead to severe performance degradation.

The leak is caused by gating congestion state clearing with fc->connected test in request_end(). This was added way back in 2009 by 26c3679101db ("fuse: destroy bdi on umount"). While the commit description doesn't explain why the test was added, it most likely was to avoid dereferencing bdi after it got destroyed.

Since then, bdi lifetime rules have changed many times and now we're always guaranteed to have access to the bdi while the superblock is alive (fc->sb).

Quoted 8 lines

Drop fc->connected conditional to avoid leaking congestion states.

Quoted 4 lines

Yeah, this should be fine AFAICT but my knowledge of FUSE is very cursory. Anyway:

Can't say I fully understand how the global "is any bdi congested" state is used in direct reclaim, but the patch is an obvious improvement, so applied.

Thanks,
Miklos

Acked-by on v1 1/2
#4 lore raw
In reply to Tejun Heo
Quoted 10 lines · Tejun Heo

FUSE has a bug where it fails to clear congestion states if a connection gets aborted while congested, which can leave nr_wb_congested[] stuck until reboot causing wait_iff_congested() to wait spuriously.

While the bdi owner, FUSE, is primarily responsible for clearing congestion states before destroying bdi_writebacks, bdi layer can ensure that congestion states are not leaked beyond bdi_writeback lifecycle.

Quoted 5 lines
reviewer Reviewed-by on v1 1/2
#5 lore raw
In reply to Tejun Heo
Quoted 10 lines · Tejun Heo

FUSE has a bug where it fails to clear congestion states if a connection gets aborted while congested, which can leave nr_wb_congested[] stuck until reboot causing wait_iff_congested() to wait spuriously.

While the bdi owner, FUSE, is primarily responsible for clearing congestion states before destroying bdi_writebacks, bdi layer can ensure that congestion states are not leaked beyond bdi_writeback lifecycle.

Quoted 5 lines

Looks good. You can add:

Honza

Quoted 46 lines
---
 include/linux/backing-dev.h |   14 +++++++++++++-
 mm/backing-dev.c            |    2 +-
 2 files changed, 14 insertions(+), 2 deletions(-)
include/linux/backing-dev.h +13 -1
@@ -220,6 +220,18 @@ static inline int bdi_sched_wait(void *w
 	return 0;
 }
 
+static inline void __wb_congested_free(struct bdi_writeback_congested *congested)
+{
+	/*
+	 * Make sure congestion states are cleared before freeing to avoid
+	 * nr_wb_congested() corruption which can lead to misbehaving
+	 * wait_iff_congested().
+	 */
+	clear_wb_congested(congested, BLK_RW_SYNC);
+	clear_wb_congested(congested, BLK_RW_ASYNC);
+	kfree(congested);
+}
+
 #ifdef CONFIG_CGROUP_WRITEBACK
 
 struct bdi_writeback_congested *
@@ -409,7 +421,7 @@ wb_congested_get_create(struct backing_d
 static inline void wb_congested_put(struct bdi_writeback_congested *congested)
 {
 	if (atomic_dec_and_test(&congested->refcnt))
-		kfree(congested);
+		__wb_congested_free(congested);
 }
 
 static inline struct bdi_writeback *wb_find_current(struct backing_dev_info *bdi)
mm/backing-dev.c +1 -1
@@ -509,7 +509,7 @@ void wb_congested_put(struct bdi_writeba
 	}
 
 	spin_unlock_irqrestore(&cgwb_lock, flags);
-	kfree(congested);
+	__wb_congested_free(congested);
 }
 
 static void cgwb_release_workfn(struct work_struct *work)
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR