Skip to contentSkip to conversation

blk-throttle: fix divide-by-zero on legacy iops limit of 0

PATCH Superseded Tao Cui 2 participants 3 messages Jul 21 – Jul 22

Subsystems (3)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Tao Cui

author
#0 lore raw

PATCH v3 1/1 blk-throttle: fix divide-by-zero on legacy iops limit of 0

From: Tao Cui <[email protected]>

Writing a multiple of 2^32 (e.g. 4294967296) to a legacy cgroup v1 throttle iops file (blkio.throttle.{read,write}_iops_device) silently truncates to 0: tg_set_conf() stores the sscanf-parsed u64 value into an unsigned int field with no clamping. The cgroup v2 path, tg_set_limit(), already clamps the same kind of value with min_t(u64, val, UINT_MAX), but the legacy path never did. Note that the "!v -> U64_MAX" mapping only catches an explicit zero and does not catch a value that truncates to zero.

With iops stored as 0, tg_update_has_rules() sets has_rules_iops[] and the next IO reaches tg_within_iops_limit(), which computes

    jiffy_wait = max(jiffy_wait, HZ / iops_limit + 1);

triggering a divide-by-zero oops. The unclamped write in tg_set_conf() is long-standing, but it only became a crash once the HZ / iops_limit divide was added.

Fix it in two places:

* tg_set_conf(): clamp the value to UINT_MAX, consistent with tg_set_limit(). This closes the truncation root cause (and the general silent truncation for any value above UINT_MAX).

* tg_dispatch_iops_time(): treat iops_limit == 0 as unlimited so the divide in tg_within_iops_limit() is never reached, defending against any future path that could produce a zero limit.

Fixes 1beabab88ece ("blk-throttle: fix lower control under super low iops limit")

---
Changes in v3:
- Drop the (u64) cast on UINT_MAX: the kernel's type-checked min() accepts two unsigned types of different width (both >= 4 bytes), so min(v, UINT_MAX) compiles clean. (David Laight)
- Add a Fixes: tag pointing at the commit that introduced the HZ / iops_limit divide, which is also where the oops became reachable.

Changes in v2:
- Use a "void *field" local for the config write so the assignment reads *(u64 *)field / *(unsigned int *)field instead of the (type *)((void *)tg + of_cft(of)->private) casts.
- Use min(v, UINT_MAX) instead of min_t(u64, v, UINT_MAX).

---
 block/blk-throttle.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
block/blk-throttle.c +10 -3
index ffc3b70065d4..e894852c3142 100644
@@ -883,7 +883,12 @@ static unsigned long tg_dispatch_iops_time(struct throtl_grp *tg, struct bio *bi
 	u32 iops_limit = tg_iops_limit(tg, rw);
 	unsigned long iops_wait;
 
-	if (iops_limit == UINT_MAX || tg->flags & THROTL_TG_CANCELING)
+	/*
+	 * iops_limit == 0 is not a valid limit. Treat it as unlimited so we
+	 * never reach the HZ / iops_limit divide in tg_within_iops_limit().
+	 */
+	if (iops_limit == UINT_MAX || iops_limit == 0 ||
+	    tg->flags & THROTL_TG_CANCELING)
 		return 0;
 
 	tg_update_slice(tg, rw);
@@ -1383,10 +1388,12 @@ static ssize_t tg_set_conf(struct kernfs_open_file *of,
 	tg = blkg_to_tg(ctx.blkg);
 	tg_update_carryover(tg);
 
+	void *field = (void *)tg + of_cft(of)->private;
+
 	if (is_u64)
-		*(u64 *)((void *)tg + of_cft(of)->private) = v;
+		*(u64 *)field = v;
 	else
-		*(unsigned int *)((void *)tg + of_cft(of)->private) = v;
+		*(unsigned int *)field = min(v, UINT_MAX);
 
 	tg_conf_updated(tg, false);
 	ret = 0;
Signature · 1 line
2.43.0
#1 lore raw

Hi,

Quoted 26 lines · Tao Cui

From: Tao Cui <[email protected]>

Writing a multiple of 2^32 (e.g. 4294967296) to a legacy cgroup v1 throttle iops file (blkio.throttle.{read,write}_iops_device) silently truncates to 0: tg_set_conf() stores the sscanf-parsed u64 value into an unsigned int field with no clamping. The cgroup v2 path, tg_set_limit(), already clamps the same kind of value with min_t(u64, val, UINT_MAX), but the legacy path never did. Note that the "!v -> U64_MAX" mapping only catches an explicit zero and does not catch a value that truncates to zero.

With iops stored as 0, tg_update_has_rules() sets has_rules_iops[] and the next IO reaches tg_within_iops_limit(), which computes

     jiffy_wait = max(jiffy_wait, HZ / iops_limit + 1);

triggering a divide-by-zero oops. The unclamped write in tg_set_conf() is long-standing, but it only became a crash once the HZ / iops_limit divide was added.

Fix it in two places:

* tg_set_conf(): clamp the value to UINT_MAX, consistent with tg_set_limit(). This closes the truncation root cause (and the general silent truncation for any value above UINT_MAX).

Quoted 5 lines

* tg_dispatch_iops_time(): treat iops_limit == 0 as unlimited so the divide in tg_within_iops_limit() is never reached, defending against any future path that could produce a zero limit.

Fixes 1beabab88ece ("blk-throttle: fix lower control under super low iops limit")

I think the root cause is that iops_limit set to 0, and what's the behavior of throttle with iops=0? Since calculate_io_allowed() will always return 0, it's likely that no IO can be issued.

Quoted 25 lines

---
Changes in v3:
- Drop the (u64) cast on UINT_MAX: the kernel's type-checked min() accepts two unsigned types of different width (both >= 4 bytes), so min(v, UINT_MAX) compiles clean. (David Laight)
- Add a Fixes: tag pointing at the commit that introduced the HZ / iops_limit divide, which is also where the oops became reachable.

Changes in v2:
- Use a "void *field" local for the config write so the assignment reads *(u64 *)field / *(unsigned int *)field instead of the (type *)((void *)tg + of_cft(of)->private) casts.
- Use min(v, UINT_MAX) instead of min_t(u64, v, UINT_MAX).

---
  block/blk-throttle.c | 13 ++++++++++---
  1 file changed, 10 insertions(+), 3 deletions(-)
block/blk-throttle.c +0 -0
index ffc3b70065d4..e894852c3142 100644
@@ -883,7 +883,12 @@ static unsigned long tg_dispatch_iops_time(struct throtl_grp *tg, struct bio *bi
  	u32 iops_limit = tg_iops_limit(tg, rw);

block/blk-throttle.c static unsigned long tg_dispatch_iops_time(struct throtl_grp *tg, struct bio *bi · line 892

Quoted 10 lines
block/blk-throttle.c +6 -1
  	unsigned long iops_wait;
 
-	if (iops_limit == UINT_MAX || tg->flags & THROTL_TG_CANCELING)
+	/*
+	 * iops_limit == 0 is not a valid limit. Treat it as unlimited so we
+	 * never reach the HZ / iops_limit divide in tg_within_iops_limit().
+	 */
+	if (iops_limit == UINT_MAX || iops_limit == 0 ||
+	    tg->flags & THROTL_TG_CANCELING)
  		return 0;

Is the above still needed with the tg_set_conf() changes? 0 is not a valid limit, and it should not exist in the first place, so I don't like the above new checking.

Quoted 17 lines
block/blk-throttle.c +4 -2
  	tg_update_slice(tg, rw);
@@ -1383,10 +1388,12 @@ static ssize_t tg_set_conf(struct kernfs_open_file *of,
  	tg = blkg_to_tg(ctx.blkg);
  	tg_update_carryover(tg);
 
+	void *field = (void *)tg + of_cft(of)->private;
+
  	if (is_u64)
-		*(u64 *)((void *)tg + of_cft(of)->private) = v;
+		*(u64 *)field = v;
  	else
-		*(unsigned int *)((void *)tg + of_cft(of)->private) = v;
+		*(unsigned int *)field = min(v, UINT_MAX);
 
  	tg_conf_updated(tg, false);
  	ret = 0;
Signature · 2 lines
Thanks,
Kuai
author
#2 lore raw
Quoted 35 lines · yu kuai

Hi,

Quoted 31 lines · Tao Cui

From: Tao Cui <[email protected]>

Writing a multiple of 2^32 (e.g. 4294967296) to a legacy cgroup v1 throttle iops file (blkio.throttle.{read,write}_iops_device) silently truncates to 0: tg_set_conf() stores the sscanf-parsed u64 value into an unsigned int field with no clamping. The cgroup v2 path, tg_set_limit(), already clamps the same kind of value with min_t(u64, val, UINT_MAX), but the legacy path never did. Note that the "!v -> U64_MAX" mapping only catches an explicit zero and does not catch a value that truncates to zero.

With iops stored as 0, tg_update_has_rules() sets has_rules_iops[] and the next IO reaches tg_within_iops_limit(), which computes

     jiffy_wait = max(jiffy_wait, HZ / iops_limit + 1);

triggering a divide-by-zero oops. The unclamped write in tg_set_conf() is long-standing, but it only became a crash once the HZ / iops_limit divide was added.

Fix it in two places:

* tg_set_conf(): clamp the value to UINT_MAX, consistent with tg_set_limit(). This closes the truncation root cause (and the general silent truncation for any value above UINT_MAX).

* tg_dispatch_iops_time(): treat iops_limit == 0 as unlimited so the divide in tg_within_iops_limit() is never reached, defending against any future path that could produce a zero limit.

Fixes 1beabab88ece ("blk-throttle: fix lower control under super low iops limit")

Hi Kuai,

Thanks for the review, and you're right on both points.

Quoted 3 lines

I think the root cause is that iops_limit set to 0, and what's the behavior of throttle with iops=0? Since calculate_io_allowed() will always return 0, it's likely that no IO can be issued.

On the root cause: yes, iops=0 is the real root cause and it predates 1beabab88ece. With iops_limit = 0, calculate_io_allowed() returns 0 (0 * jiffy_elapsed), so no IO can be issued at all; 1beabab88ece only turned that pre-existing iops=0 hang into a divide-by-zero via the new HZ / iops_limit. Attributing it to 1beabab88ece was therefore wrong -- my apologies. I'll drop the Fixes: tag in v4.

Quoted 28 lines
Quoted 28 lines

---
Changes in v3:
- Drop the (u64) cast on UINT_MAX: the kernel's type-checked min() accepts two unsigned types of different width (both >= 4 bytes), so min(v, UINT_MAX) compiles clean. (David Laight)
- Add a Fixes: tag pointing at the commit that introduced the HZ / iops_limit divide, which is also where the oops became reachable.

Changes in v2:
- Use a "void *field" local for the config write so the assignment reads *(u64 *)field / *(unsigned int *)field instead of the (type *)((void *)tg + of_cft(of)->private) casts.
- Use min(v, UINT_MAX) instead of min_t(u64, v, UINT_MAX).

---
  block/blk-throttle.c | 13 ++++++++++---
  1 file changed, 10 insertions(+), 3 deletions(-)
block/blk-throttle.c +0 -0
index ffc3b70065d4..e894852c3142 100644
@@ -883,7 +883,12 @@ static unsigned long tg_dispatch_iops_time(struct throtl_grp *tg, struct bio *bi
  	u32 iops_limit = tg_iops_limit(tg, rw);
  	unsigned long iops_wait;
 
Quoted 11 lines

block/blk-throttle.c static unsigned long tg_dispatch_iops_time(struct throtl_grp *tg, struct bio *bi · line 892

Quoted 8 lines
block/blk-throttle.c +6 -1
-	if (iops_limit == UINT_MAX || tg->flags & THROTL_TG_CANCELING)
+	/*
+	 * iops_limit == 0 is not a valid limit. Treat it as unlimited so we
+	 * never reach the HZ / iops_limit divide in tg_within_iops_limit().
+	 */
+	if (iops_limit == UINT_MAX || iops_limit == 0 ||
+	    tg->flags & THROTL_TG_CANCELING)
  		return 0;

Is the above still needed with the tg_set_conf() changes? 0 is not a valid limit, and it should not exist in the first place, so I don't like the above new checking.

On the iops_limit == 0 check: agreed, dropping it. With the tg_set_conf() clamp in place, 0 can never be stored as a limit (explicit 0 maps to U64_MAX, anything that would truncate to 0 is clamped to UINT_MAX, and the init value is UINT_MAX), so the runtime check only guards an unreachable state. v4 keeps only the tg_set_conf() clamp.

Will send v4 shortly.

Thanks,
Tao

Quoted 19 lines
Quoted 18 lines
block/blk-throttle.c +4 -2
  	tg_update_slice(tg, rw);
@@ -1383,10 +1388,12 @@ static ssize_t tg_set_conf(struct kernfs_open_file *of,
  	tg = blkg_to_tg(ctx.blkg);
  	tg_update_carryover(tg);
 
+	void *field = (void *)tg + of_cft(of)->private;
+
  	if (is_u64)
-		*(u64 *)((void *)tg + of_cft(of)->private) = v;
+		*(u64 *)field = v;
  	else
-		*(unsigned int *)((void *)tg + of_cft(of)->private) = v;
+		*(unsigned int *)field = min(v, UINT_MAX);
 
  	tg_conf_updated(tg, false);
  	ret = 0;