Skip to contentSkip to conversation

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

PATCH Superseded Tao Cui 3 participants 7 messages Jul 14 – Jul 16

Subsystems (3)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Tao Cui

author
#0 lore raw

PATCH 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.

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.

---
 block/blk-throttle.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
block/blk-throttle.c +8 -2
index ffc3b70065d4..3f3c1374f4b2 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);
@@ -1386,7 +1391,8 @@ static ssize_t tg_set_conf(struct kernfs_open_file *of,
 	if (is_u64)
 		*(u64 *)((void *)tg + of_cft(of)->private) = v;
 	else
-		*(unsigned int *)((void *)tg + of_cft(of)->private) = v;
+		*(unsigned int *)((void *)tg + of_cft(of)->private) =
+			min_t(u64, v, UINT_MAX);
 
 	tg_conf_updated(tg, false);
 	ret = 0;
Signature · 1 line
2.43.0
#1 lore raw
Quoted 52 lines · On Tue, 14 Jul 2026 18:35:52 +0800 Tao Cui <[email protected]> wrote:

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.

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.

---
 block/blk-throttle.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
block/blk-throttle.c +6 -1
index ffc3b70065d4..3f3c1374f4b2 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);

block/blk-throttle.c static ssize_t tg_set_conf(struct kernfs_open_file *of, · line 1395

Quoted 6 lines
block/blk-throttle.c +2 -1
@@ -1386,7 +1391,8 @@ static ssize_t tg_set_conf(struct kernfs_open_file *of,
 	if (is_u64)
 		*(u64 *)((void *)tg + of_cft(of)->private) = v;
 	else
-		*(unsigned int *)((void *)tg + of_cft(of)->private) = v;
+		*(unsigned int *)((void *)tg + of_cft(of)->private) =
+			min_t(u64, v, UINT_MAX);

The LHS casts look horrid - there has to be a nicer way to do that.

And you don't need min_t() a plain min() will be fine.

David

Quoted 3 lines
block/blk-throttle.c +0 -0
 	tg_conf_updated(tg, false);
 	ret = 0;
author
#2 lore raw
Quoted 64 lines · David Laight
Quoted 52 lines · On Tue, 14 Jul 2026 18:35:52 +0800 Tao Cui <[email protected]> wrote:

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.

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.

---
 block/blk-throttle.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
block/blk-throttle.c +6 -1
index ffc3b70065d4..3f3c1374f4b2 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);

block/blk-throttle.c static ssize_t tg_set_conf(struct kernfs_open_file *of, · line 1395

Quoted 6 lines
block/blk-throttle.c +2 -1
@@ -1386,7 +1391,8 @@ static ssize_t tg_set_conf(struct kernfs_open_file *of,
 	if (is_u64)
 		*(u64 *)((void *)tg + of_cft(of)->private) = v;
 	else
-		*(unsigned int *)((void *)tg + of_cft(of)->private) = v;
+		*(unsigned int *)((void *)tg + of_cft(of)->private) =
+			min_t(u64, v, UINT_MAX);

The LHS casts look horrid - there has to be a nicer way to do that.

Quoted 2 lines

And you don't need min_t() a plain min() will be fine.

Hi David,

Both done in v2 — introduced a void *field local so the writes read *(u64 *)field / *(unsigned int *)field, and switched to min(v, (u64)UINT_MAX).

Thanks,
Tao

Quoted 8 lines

David

Quoted 3 lines
block/blk-throttle.c +0 -0
 	tg_conf_updated(tg, false);
 	ret = 0;
#3 lore raw
Quoted 1 line · On Wed, 15 Jul 2026 20:44:19 +0800 Tao Cui <[email protected]> wrote:

在 2026/7/14 19:37, David Laight 写道:

...

Quoted 11 lines
Quoted 11 lines

block/blk-throttle.c static ssize_t tg_set_conf(struct kernfs_open_file *of, · line 1395

Quoted 8 lines
block/blk-throttle.c +2 -1
 	tg_update_slice(tg, rw);
@@ -1386,7 +1391,8 @@ static ssize_t tg_set_conf(struct kernfs_open_file *of,
 	if (is_u64)
 		*(u64 *)((void *)tg + of_cft(of)->private) = v;
 	else
-		*(unsigned int *)((void *)tg + of_cft(of)->private) = v;
+		*(unsigned int *)((void *)tg + of_cft(of)->private) =
+			min_t(u64, v, UINT_MAX);  

The LHS casts look horrid - there has to be a nicer way to do that.

Quoted 7 lines
Quoted 2 lines

And you don't need min_t() a plain min() will be fine.

Hi David,

Both done in v2 — introduced a void *field local so the writes read *(u64 *)field / *(unsigned int *)field, and switched to min(v, (u64)UINT_MAX).

You don't need the cast either.

David

Quoted 12 lines

Thanks,
Tao

Quoted 8 lines

David

Quoted 3 lines
block/blk-throttle.c +0 -0
 	tg_conf_updated(tg, false);
 	ret = 0;  
author
#4 lore raw
Quoted 18 lines · 于 2026年7月15日 GMT+08:00 23:01:09,David Laight <[email protected]> 写道:
Quoted 1 line · On Wed, 15 Jul 2026 20:44:19 +0800 Tao Cui <[email protected]> wrote:

在 2026/7/14 19:37, David Laight 写道:

...

Quoted 13 lines
Quoted 13 lines

block/blk-throttle.c static ssize_t tg_set_conf(struct kernfs_open_file *of, · line 1395

Quoted 8 lines
block/blk-throttle.c +2 -1
 	tg_update_slice(tg, rw);
@@ -1386,7 +1391,8 @@ static ssize_t tg_set_conf(struct kernfs_open_file *of,
 	if (is_u64)
 		*(u64 *)((void *)tg + of_cft(of)->private) = v;
 	else
-		*(unsigned int *)((void *)tg + of_cft(of)->private) = v;
+		*(unsigned int *)((void *)tg + of_cft(of)->private) =
+			min_t(u64, v, UINT_MAX);  

The LHS casts look horrid - there has to be a nicer way to do that.

And you don't need min_t() a plain min() will be fine.

Quoted 7 lines
Quoted 5 lines

Hi David,

Both done in v2 — introduced a void *field local so the writes read *(u64 *)field / *(unsigned int *)field, and switched to min(v, (u64)UINT_MAX).

You don't need the cast either.

Noted, this will be done in the next version.

Thanks,
Tao

Quoted 16 lines

David

Quoted 12 lines

Thanks,
Tao

Quoted 8 lines

David

Quoted 3 lines
block/blk-throttle.c +0 -0
 	tg_conf_updated(tg, false);
 	ret = 0;  
#5 lore raw
In reply to Tao Cui
Quoted 20 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.

Fix it in two places:

Quoted 8 lines

* 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.

Does this need a "Fixes:" tag.

Quoted 33 lines
---
  block/blk-throttle.c | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
block/blk-throttle.c +8 -2
index ffc3b70065d4..3f3c1374f4b2 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);
@@ -1386,7 +1391,8 @@ static ssize_t tg_set_conf(struct kernfs_open_file *of,
  	if (is_u64)
  		*(u64 *)((void *)tg + of_cft(of)->private) = v;
  	else
-		*(unsigned int *)((void *)tg + of_cft(of)->private) = v;
+		*(unsigned int *)((void *)tg + of_cft(of)->private) =
+			min_t(u64, v, UINT_MAX);
 
  	tg_conf_updated(tg, false);
  	ret = 0;
author
#6 lore raw
Quoted 27 lines · 于 2026年7月16日 GMT+08:00 00:13:06,Haris Iqbal <[email protected]> 写道:
Quoted 24 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.

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 7 lines
Quoted 4 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.

Does this need a "Fixes:" tag.

Noted, I will address this in the next version.

Thanks,
Tao

Quoted 31 lines
Quoted 30 lines
---
  block/blk-throttle.c | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
block/blk-throttle.c +8 -1
index ffc3b70065d4..3f3c1374f4b2 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);
@@ -1386,7 +1391,8 @@ static ssize_t tg_set_conf(struct kernfs_open_file *of,
  	if (is_u64)
  		*(u64 *)((void *)tg + of_cft(of)->private) = v;
  	else
-		*(unsigned int *)((void *)tg + of_cft(of)->private) = v;
+		*(unsigned int *)((void *)tg + of_cft(of)->private) =
+			min_t(u64, v, UINT_MAX);
    	tg_conf_updated(tg, false);
  	ret = 0;