Skip to contentSkip to conversation

blk-throttle: fix null pointer dereference while throttling writeback IOs

PATCH In review xuejiufei 3 participants 5 messages Oct 10, 2017 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

xuejiufei

author
#1 lore raw

PATCH 1/1 blk-throttle: fix null pointer dereference while throttling writeback IOs

In reply to a message not in the archive

From: Jiufei Xue <[email protected]>

A null pointer dereference can occur when blkcg is removed manually with writeback IOs inflight. This is caused by the following case:

Writeback kworker submit the bio and set bio->bi_cg_private to tg in blk_throtl_assoc_bio.
Then we remove the block cgroup manually, the blkg and tg would be freed if there is no request inflight.
When the submitted bio come back, blk_throtl_bio_endio() fetch the tg which was already freed.

Fix this by increasing the refcount of blkg in funcion blk_throtl_assoc_bio() so that the blkg will not be freed until the bio_endio called.

---
 block/blk-throttle.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
block/blk-throttle.c +10 -2
index 17816a0..d80c3f0 100644
@@ -2112,8 +2112,12 @@ static inline void throtl_update_latency_buckets(struct throtl_data *td)
 static void blk_throtl_assoc_bio(struct throtl_grp *tg, struct bio *bio)
 {
 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
-	if (bio->bi_css)
+	if (bio->bi_css) {
+		if (bio->bi_cg_private)
+			blkg_put(tg_to_blkg(bio->bi_cg_private));
 		bio->bi_cg_private = tg;
+		blkg_get(tg_to_blkg(tg));
+	}
 	blk_stat_set_issue(&bio->bi_issue_stat, bio_sectors(bio));
 #endif
 }
@@ -2283,8 +2287,10 @@ void blk_throtl_bio_endio(struct bio *bio)
 
 	start_time = blk_stat_time(&bio->bi_issue_stat) >> 10;
 	finish_time = __blk_stat_time(finish_time_ns) >> 10;
-	if (!start_time || finish_time <= start_time)
+	if (!start_time || finish_time <= start_time) {
+		blkg_put(tg_to_blkg(tg));
 		return;
+	}
 
 	lat = finish_time - start_time;
 	/* this is only for bio based driver */
@@ -2314,6 +2320,8 @@ void blk_throtl_bio_endio(struct bio *bio)
 		tg->bio_cnt /= 2;
 		tg->bad_bio_cnt /= 2;
 	}
+
+	blkg_put(tg_to_blkg(tg));
 }
 #endif
 
Signature · 1 line
1.9.4
Reviewed-by on v1 1/1
#2 lore raw
Quoted 52 lines · xuejiufei

From: Jiufei Xue <[email protected]>

A null pointer dereference can occur when blkcg is removed manually with writeback IOs inflight. This is caused by the following case:

Writeback kworker submit the bio and set bio->bi_cg_private to tg in blk_throtl_assoc_bio.
Then we remove the block cgroup manually, the blkg and tg would be freed if there is no request inflight.
When the submitted bio come back, blk_throtl_bio_endio() fetch the tg which was already freed.

Fix this by increasing the refcount of blkg in funcion blk_throtl_assoc_bio() so that the blkg will not be freed until the bio_endio called.

---
 block/blk-throttle.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
block/blk-throttle.c +8 -2
index 17816a0..d80c3f0 100644
@@ -2112,8 +2112,12 @@ static inline void throtl_update_latency_buckets(struct throtl_data *td)
 static void blk_throtl_assoc_bio(struct throtl_grp *tg, struct bio *bio)
 {
 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
-	if (bio->bi_css)
+	if (bio->bi_css) {
+		if (bio->bi_cg_private)
+			blkg_put(tg_to_blkg(bio->bi_cg_private));
 		bio->bi_cg_private = tg;
+		blkg_get(tg_to_blkg(tg));
+	}
 	blk_stat_set_issue(&bio->bi_issue_stat, bio_sectors(bio));
 #endif
 }
@@ -2283,8 +2287,10 @@ void blk_throtl_bio_endio(struct bio *bio)
 
 	start_time = blk_stat_time(&bio->bi_issue_stat) >> 10;
 	finish_time = __blk_stat_time(finish_time_ns) >> 10;
-	if (!start_time || finish_time <= start_time)
+	if (!start_time || finish_time <= start_time) {
+		blkg_put(tg_to_blkg(tg));
 		return;
+	}
 
 	lat = finish_time - start_time;
 	/* this is only for bio based driver */

block/blk-throttle.c void blk_throtl_bio_endio(struct bio *bio) · line 2326

Quoted 7 lines
block/blk-throttle.c +2 -0
@@ -2314,6 +2320,8 @@ void blk_throtl_bio_endio(struct bio *bio)
 		tg->bio_cnt /= 2;
 		tg->bad_bio_cnt /= 2;
 	}
+
+	blkg_put(tg_to_blkg(tg));
 }
 #endif
maintainer
#3 lore raw
Quoted 50 lines · Shaohua Li
Quoted 49 lines · xuejiufei

From: Jiufei Xue <[email protected]>

A null pointer dereference can occur when blkcg is removed manually with writeback IOs inflight. This is caused by the following case:

Writeback kworker submit the bio and set bio->bi_cg_private to tg in blk_throtl_assoc_bio.
Then we remove the block cgroup manually, the blkg and tg would be freed if there is no request inflight.
When the submitted bio come back, blk_throtl_bio_endio() fetch the tg which was already freed.

Fix this by increasing the refcount of blkg in funcion blk_throtl_assoc_bio() so that the blkg will not be freed until the bio_endio called.

---
 block/blk-throttle.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
block/blk-throttle.c +8 -2
index 17816a0..d80c3f0 100644
@@ -2112,8 +2112,12 @@ static inline void throtl_update_latency_buckets(struct throtl_data *td)
 static void blk_throtl_assoc_bio(struct throtl_grp *tg, struct bio *bio)
 {
 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
-	if (bio->bi_css)
+	if (bio->bi_css) {
+		if (bio->bi_cg_private)
+			blkg_put(tg_to_blkg(bio->bi_cg_private));
 		bio->bi_cg_private = tg;
+		blkg_get(tg_to_blkg(tg));
+	}
 	blk_stat_set_issue(&bio->bi_issue_stat, bio_sectors(bio));
 #endif
 }
@@ -2283,8 +2287,10 @@ void blk_throtl_bio_endio(struct bio *bio)
 
 	start_time = blk_stat_time(&bio->bi_issue_stat) >> 10;
 	finish_time = __blk_stat_time(finish_time_ns) >> 10;
-	if (!start_time || finish_time <= start_time)
+	if (!start_time || finish_time <= start_time) {
+		blkg_put(tg_to_blkg(tg));
 		return;
+	}
 
Quoted 12 lines

block/blk-throttle.c void blk_throtl_bio_endio(struct bio *bio) · line 2326

Quoted 10 lines
block/blk-throttle.c +2 -0
 	lat = finish_time - start_time;
 	/* this is only for bio based driver */
@@ -2314,6 +2320,8 @@ void blk_throtl_bio_endio(struct bio *bio)
 		tg->bio_cnt /= 2;
 		tg->bad_bio_cnt /= 2;
 	}
+
+	blkg_put(tg_to_blkg(tg));
 }
 #endif

I was going to queue this up for 4.15, but was wondering if there's a strong reason to include it for 4.14 instead?

Signature · 1 line
Jens Axboe
#4 lore raw
Quoted 64 lines · Jens Axboe
Quoted 50 lines · Shaohua Li
Quoted 49 lines · xuejiufei

From: Jiufei Xue <[email protected]>

A null pointer dereference can occur when blkcg is removed manually with writeback IOs inflight. This is caused by the following case:

Writeback kworker submit the bio and set bio->bi_cg_private to tg in blk_throtl_assoc_bio.
Then we remove the block cgroup manually, the blkg and tg would be freed if there is no request inflight.
When the submitted bio come back, blk_throtl_bio_endio() fetch the tg which was already freed.

Fix this by increasing the refcount of blkg in funcion blk_throtl_assoc_bio() so that the blkg will not be freed until the bio_endio called.

---
 block/blk-throttle.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
block/blk-throttle.c +8 -2
index 17816a0..d80c3f0 100644
@@ -2112,8 +2112,12 @@ static inline void throtl_update_latency_buckets(struct throtl_data *td)
 static void blk_throtl_assoc_bio(struct throtl_grp *tg, struct bio *bio)
 {
 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
-	if (bio->bi_css)
+	if (bio->bi_css) {
+		if (bio->bi_cg_private)
+			blkg_put(tg_to_blkg(bio->bi_cg_private));
 		bio->bi_cg_private = tg;
+		blkg_get(tg_to_blkg(tg));
+	}
 	blk_stat_set_issue(&bio->bi_issue_stat, bio_sectors(bio));
 #endif
 }
@@ -2283,8 +2287,10 @@ void blk_throtl_bio_endio(struct bio *bio)
 
 	start_time = blk_stat_time(&bio->bi_issue_stat) >> 10;
 	finish_time = __blk_stat_time(finish_time_ns) >> 10;
-	if (!start_time || finish_time <= start_time)
+	if (!start_time || finish_time <= start_time) {
+		blkg_put(tg_to_blkg(tg));
 		return;
+	}
 
Quoted 12 lines

block/blk-throttle.c void blk_throtl_bio_endio(struct bio *bio) · line 2326

Quoted 10 lines
block/blk-throttle.c +2 -0
 	lat = finish_time - start_time;
 	/* this is only for bio based driver */
@@ -2314,6 +2320,8 @@ void blk_throtl_bio_endio(struct bio *bio)
 		tg->bio_cnt /= 2;
 		tg->bad_bio_cnt /= 2;
 	}
+
+	blkg_put(tg_to_blkg(tg));
 }
 #endif
Quoted 2 lines

I was going to queue this up for 4.15, but was wondering if there's a strong reason to include it for 4.14 instead?

Either is ok to me, this isn't easy to trigger, but on the other hand it's quite safe.

maintainer
#5 lore raw
Quoted 65 lines · Shaohua Li
Quoted 64 lines · Jens Axboe
Quoted 62 lines · Shaohua Li
Quoted 52 lines · xuejiufei

From: Jiufei Xue <[email protected]>

A null pointer dereference can occur when blkcg is removed manually with writeback IOs inflight. This is caused by the following case:

Writeback kworker submit the bio and set bio->bi_cg_private to tg in blk_throtl_assoc_bio.
Then we remove the block cgroup manually, the blkg and tg would be freed if there is no request inflight.
When the submitted bio come back, blk_throtl_bio_endio() fetch the tg which was already freed.

Fix this by increasing the refcount of blkg in funcion blk_throtl_assoc_bio() so that the blkg will not be freed until the bio_endio called.

---
 block/blk-throttle.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
block/blk-throttle.c +8 -2
index 17816a0..d80c3f0 100644
@@ -2112,8 +2112,12 @@ static inline void throtl_update_latency_buckets(struct throtl_data *td)
 static void blk_throtl_assoc_bio(struct throtl_grp *tg, struct bio *bio)
 {
 #ifdef CONFIG_BLK_DEV_THROTTLING_LOW
-	if (bio->bi_css)
+	if (bio->bi_css) {
+		if (bio->bi_cg_private)
+			blkg_put(tg_to_blkg(bio->bi_cg_private));
 		bio->bi_cg_private = tg;
+		blkg_get(tg_to_blkg(tg));
+	}
 	blk_stat_set_issue(&bio->bi_issue_stat, bio_sectors(bio));
 #endif
 }
@@ -2283,8 +2287,10 @@ void blk_throtl_bio_endio(struct bio *bio)
 
 	start_time = blk_stat_time(&bio->bi_issue_stat) >> 10;
 	finish_time = __blk_stat_time(finish_time_ns) >> 10;
-	if (!start_time || finish_time <= start_time)
+	if (!start_time || finish_time <= start_time) {
+		blkg_put(tg_to_blkg(tg));
 		return;
+	}
 
 	lat = finish_time - start_time;
 	/* this is only for bio based driver */

block/blk-throttle.c void blk_throtl_bio_endio(struct bio *bio) · line 2326

Quoted 7 lines
block/blk-throttle.c +2 -0
@@ -2314,6 +2320,8 @@ void blk_throtl_bio_endio(struct bio *bio)
 		tg->bio_cnt /= 2;
 		tg->bad_bio_cnt /= 2;
 	}
+
+	blkg_put(tg_to_blkg(tg));
 }
 #endif
Quoted 5 lines
Quoted 2 lines

I was going to queue this up for 4.15, but was wondering if there's a strong reason to include it for 4.14 instead?

Either is ok to me, this isn't easy to trigger, but on the other hand it's quite safe.

OK, let's just stick with 4.15 then.

Signature · 1 line
Jens Axboe