mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] blk-cgroup: avoid 32-bit overflow in root io.stat byte accounting
@ 2026-07-14  6:31 Tao Cui
  2026-07-16 12:54 ` Tang Yizhou
  2026-07-16 17:17 ` Bart Van Assche
  0 siblings, 2 replies; 3+ messages in thread
From: Tao Cui @ 2026-07-14  6:31 UTC (permalink / raw)
  To: axboe; +Cc: tj, josef, boris, cgroups, linux-block, linux-kernel, Tao Cui

From: Tao Cui <cuitao@kylinos.cn>

blkcg_fill_root_iostats() converts per-CPU sector counts to bytes with

    tmp.bytes[BLKG_IOSTAT_READ] += cpu_dkstats->sectors[STAT_READ] << 9;

but disk_stats.sectors is `unsigned long`, and the shift is carried out in
that type before the result is promoted to the u64 accumulator.  On 32-bit
kernels (unsigned long is 32 bits) this wraps, so once a per-CPU counter
reaches 2**23 sectors (~4 GiB) the computed byte count is wrong, corrupting
the root cgroup's io.stat.

Every other sector->byte conversion in the tree casts to a wide type first
((loff_t)sectors << SECTOR_SHIFT in bdev.c, (u64)max_sectors << SECTOR_SHIFT
in blk-settings.c); do the same here.

Fixes: ef45fe470e1e5 ("blk-cgroup: show global disk stats in root cgroup io.stat")
Signed-off-by: Tao Cui <cuitao@kylinos.cn>
---
 block/blk-cgroup.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c
index d2a1f5903f24..a778aa9d2bb9 100644
--- a/block/blk-cgroup.c
+++ b/block/blk-cgroup.c
@@ -1112,11 +1112,11 @@ static void blkcg_fill_root_iostats(void)
 				cpu_dkstats->ios[STAT_DISCARD];
 			// convert sectors to bytes
 			tmp.bytes[BLKG_IOSTAT_READ] +=
-				cpu_dkstats->sectors[STAT_READ] << 9;
+				(u64)cpu_dkstats->sectors[STAT_READ] << SECTOR_SHIFT;
 			tmp.bytes[BLKG_IOSTAT_WRITE] +=
-				cpu_dkstats->sectors[STAT_WRITE] << 9;
+				(u64)cpu_dkstats->sectors[STAT_WRITE] << SECTOR_SHIFT;
 			tmp.bytes[BLKG_IOSTAT_DISCARD] +=
-				cpu_dkstats->sectors[STAT_DISCARD] << 9;
+				(u64)cpu_dkstats->sectors[STAT_DISCARD] << SECTOR_SHIFT;
 		}
 
 		flags = u64_stats_update_begin_irqsave(&blkg->iostat.sync);
-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] blk-cgroup: avoid 32-bit overflow in root io.stat byte accounting
  2026-07-14  6:31 [PATCH] blk-cgroup: avoid 32-bit overflow in root io.stat byte accounting Tao Cui
@ 2026-07-16 12:54 ` Tang Yizhou
  2026-07-16 17:17 ` Bart Van Assche
  1 sibling, 0 replies; 3+ messages in thread
From: Tang Yizhou @ 2026-07-16 12:54 UTC (permalink / raw)
  To: Tao Cui, axboe
  Cc: tj, josef, boris, cgroups, linux-block, linux-kernel, Tao Cui

On 14/7/26 2:31 pm, Tao Cui wrote:
> From: Tao Cui <cuitao@kylinos.cn>
> 
> blkcg_fill_root_iostats() converts per-CPU sector counts to bytes with
> 
>     tmp.bytes[BLKG_IOSTAT_READ] += cpu_dkstats->sectors[STAT_READ] << 9;
> 
> but disk_stats.sectors is `unsigned long`, and the shift is carried out in
> that type before the result is promoted to the u64 accumulator.  On 32-bit
> kernels (unsigned long is 32 bits) this wraps, so once a per-CPU counter
> reaches 2**23 sectors (~4 GiB) the computed byte count is wrong, corrupting
> the root cgroup's io.stat.
> 
> Every other sector->byte conversion in the tree casts to a wide type first
> ((loff_t)sectors << SECTOR_SHIFT in bdev.c, (u64)max_sectors << SECTOR_SHIFT
> in blk-settings.c); do the same here.
> 
> Fixes: ef45fe470e1e5 ("blk-cgroup: show global disk stats in root cgroup io.stat")
> Signed-off-by: Tao Cui <cuitao@kylinos.cn>
> ---
>  block/blk-cgroup.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c
> index d2a1f5903f24..a778aa9d2bb9 100644
> --- a/block/blk-cgroup.c
> +++ b/block/blk-cgroup.c
> @@ -1112,11 +1112,11 @@ static void blkcg_fill_root_iostats(void)
>  				cpu_dkstats->ios[STAT_DISCARD];
>  			// convert sectors to bytes
>  			tmp.bytes[BLKG_IOSTAT_READ] +=
> -				cpu_dkstats->sectors[STAT_READ] << 9;
> +				(u64)cpu_dkstats->sectors[STAT_READ] << SECTOR_SHIFT;
>  			tmp.bytes[BLKG_IOSTAT_WRITE] +=
> -				cpu_dkstats->sectors[STAT_WRITE] << 9;
> +				(u64)cpu_dkstats->sectors[STAT_WRITE] << SECTOR_SHIFT;
>  			tmp.bytes[BLKG_IOSTAT_DISCARD] +=
> -				cpu_dkstats->sectors[STAT_DISCARD] << 9;
> +				(u64)cpu_dkstats->sectors[STAT_DISCARD] << SECTOR_SHIFT;
>  		}
>  
>  		flags = u64_stats_update_begin_irqsave(&blkg->iostat.sync);

Reviewed-by: Tang Yizhou <yizhou.tang@shopee.com>

-- 
Best Regards,
Yi


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] blk-cgroup: avoid 32-bit overflow in root io.stat byte accounting
  2026-07-14  6:31 [PATCH] blk-cgroup: avoid 32-bit overflow in root io.stat byte accounting Tao Cui
  2026-07-16 12:54 ` Tang Yizhou
@ 2026-07-16 17:17 ` Bart Van Assche
  1 sibling, 0 replies; 3+ messages in thread
From: Bart Van Assche @ 2026-07-16 17:17 UTC (permalink / raw)
  To: Tao Cui, axboe
  Cc: tj, josef, boris, cgroups, linux-block, linux-kernel, Tao Cui

On 7/13/26 11:31 PM, Tao Cui wrote:
> Every other sector->byte conversion in the tree casts to a wide type first
> ((loff_t)sectors << SECTOR_SHIFT in bdev.c, (u64)max_sectors << SECTOR_SHIFT
> in blk-settings.c); do the same here.

Reviewed-by: Bart Van Assche <bvanassche@acm.org>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-07-16 17:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-14  6:31 [PATCH] blk-cgroup: avoid 32-bit overflow in root io.stat byte accounting Tao Cui
2026-07-16 12:54 ` Tang Yizhou
2026-07-16 17:17 ` Bart Van Assche

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox