Skip to contentSkip to conversation

lib/dim: fix 32-bit overflow in dim_calc_stats()

PATCH Applied Shashank Mohan Jain 3 participants 7 messages Sep 27 – Oct 2

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover lib/dim: fix 32-bit overflow in dim_calc_stats()

On 32-bit kernels dim_calc_stats() multiplies the u32 byte, packet and completion counts of a DIM window by USEC_PER_MSEC (1000L) in 32-bit long arithmetic. Once a window carries more than about 4.3 MB, bpms wraps, and net_dim steers interrupt moderation on a meaningless throughput value. At 1 Gbit/s line rate a 64-event window passes that size when there are fewer than about 1,860 DIM events per second, which is common while NAPI keeps the interrupt masked under load. 32-bit users of the library include mtk_eth_soc (MT7621, MT7623), bcmgenet and bcmsysport on 32-bit ARM, xilinx_axienet on Zynq-7000 and MicroBlaze, and virtio_net in 32-bit guests. The overflow goes back to the mlx5e code the library was moved from.

Patch 1 does the multiplications in 64 bits and divides with DIV_ROUND_UP_ULL(); the results on 64-bit are unchanged. Patch 2 adds a KUnit suite for dim_calc_stats() whose large-window cases fail on 32-bit without patch 1. It is part of this series as described under "Co-posting selftests" in maintainer-netdev.rst.

The series is based on net (a7bfaba4823e) and has no dependencies; both patches also apply to mainline (fd179f8a05be) and net-next.

The bug was found and the patches were prepared with Claude Code (Anthropic), model Claude Opus 5.5 (claude-opus-5-5).

Tested:
- KUnit (CONFIG_DIMLIB_KUNIT_TEST=y) on UML i386 (SUBARCH=i386):
without patch 1, 4 of the 7 dim_calc_stats cases fail (many_bytes, bytes_32bit_limit, gigabit, many_packets); with it all pass. On UML x86_64 all cases pass with and without patch 1. Both were run on mainline and on net.
- W=1 builds of lib/dim/ for UML x86_64 and i386 without warnings; dim.o references no libgcc 64-bit division helpers. A native i386 defconfig build (vmlinux and modules, DIMLIB=y) succeeds.
- checkpatch --strict. Its "does MAINTAINERS need updating?" warning on patch 2 does not apply: lib/dim/ is already covered by the DYNAMIC INTERRUPT MODERATION entry.

Not tested: 32-bit ARM or MIPS builds (no cross compiler was available), and no run on a 32-bit NIC. The traffic levels at which the drivers hit the overflow are derived from how they count DIM events, not measured.

Shashank Mohan Jain (2):
lib/dim: fix 32-bit overflow in dim_calc_stats() rates lib/dim: add KUnit test for dim_calc_stats()

 lib/Kconfig.debug   |  15 ++++++
 lib/dim/Makefile    |   2 +
 lib/dim/dim.c       |  10 ++--
 lib/dim/dim_kunit.c | 126 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 150 insertions(+), 3 deletions(-)
 create mode 100644 lib/dim/dim_kunit.c

base-commit: a7bfaba4823e3c165bb2004c74eff7c096672bc7

Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 lib/dim: fix 32-bit overflow in dim_calc_stats() rates

dim_calc_stats() computes the per-millisecond rates as

DIV_ROUND_UP(nbytes * USEC_PER_MSEC, delta_us)

where nbytes is a u32 and USEC_PER_MSEC is 1000L. On 64-bit the product is done in 64-bit long arithmetic, but on 32-bit architectures long is 32 bits wide and the product wraps as soon as a measurement window carries more than 4294967 bytes (about 4.3 MB). The same applies to the packet and completion counts, although those need more than 4.29 million packets or completions per window.

A DIM window spans DIM_NEVENTS (64) events. Drivers count events per interrupt or per NAPI poll, so under sustained load a window can easily carry more than 4.3 MB: 64 full NAPI polls of 64 MTU-sized frames are already 6.2 MB, and drivers such as mtk_eth_soc count one event per interrupt while NAPI keeps polling with the interrupt masked. On 32-bit users of the library (for example mtk_eth_soc on MT7621, bcmgenet and bcmsysport on 32-bit ARM, or virtio_net in a 32-bit guest) bpms then becomes the product modulo 2^32 divided by the window length, and net_dim_stats_compare() makes its BETTER/WORSE decisions on a value that has little to do with the real throughput.

For example, a 1 Gbit/s link at line rate that moves 5 MB in a 40 ms window gives bpms = 125000 on 64-bit but 17626 on 32-bit, and 5 million packets in 2 s gives ppms = 353 instead of 2500.

Widen the products to 64 bits and divide with DIV_ROUND_UP_ULL(). The results are unchanged on 64-bit.

Fixes cb3c7fd4f839 ("net/mlx5e: Support adaptive RX coalescing")
Fixes 4c4dbb4a7363 ("net/mlx5e: Move dynamic interrupt coalescing code to include/linux")
Assisted-by LLM

---
Found by reading lib/dim with an LLM assistant (Claude Code, Claude Opus 5.5), which also drafted the fix, the KUnit test and the changelogs.

Tested: the new KUnit suite (patch 2), on mainline and on net, on UML i386 (fails 4 of 7 dim_calc_stats cases without this patch, passes with it) and UML x86_64 (passes with and without it); W=1 builds of lib/dim/ for UML x86_64 and i386, and a native i386 vmlinux+modules build, with no 64-bit division helper references.
Not tested: 32-bit ARM or MIPS builds (no cross compiler available), and no run on a 32-bit NIC; the traffic levels at which drivers hit the overflow are derived from how they count DIM events, not measured.

 lib/dim/dim.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
lib/dim/dim.c +7 -3
index 97c3d084ebf0..7f3eebae73cb 100644
@@ -69,11 +69,15 @@ bool dim_calc_stats(const struct dim_sample *start,
 	if (!delta_us)
 		return false;
 
-	curr_stats->ppms = DIV_ROUND_UP(npkts * USEC_PER_MSEC, delta_us);
-	curr_stats->bpms = DIV_ROUND_UP(nbytes * USEC_PER_MSEC, delta_us);
+	/* u32 * USEC_PER_MSEC overflows a 32-bit long */
+	curr_stats->ppms = DIV_ROUND_UP_ULL((u64)npkts * USEC_PER_MSEC,
+					    delta_us);
+	curr_stats->bpms = DIV_ROUND_UP_ULL((u64)nbytes * USEC_PER_MSEC,
+					    delta_us);
 	curr_stats->epms = DIV_ROUND_UP(DIM_NEVENTS * USEC_PER_MSEC,
 					delta_us);
-	curr_stats->cpms = DIV_ROUND_UP(ncomps * USEC_PER_MSEC, delta_us);
+	curr_stats->cpms = DIV_ROUND_UP_ULL((u64)ncomps * USEC_PER_MSEC,
+					    delta_us);
 	if (curr_stats->epms != 0)
 		curr_stats->cpe_ratio = DIV_ROUND_DOWN_ULL(
 			curr_stats->cpms * 100, curr_stats->epms);
Signature · 1 line
2.43.0
#5 lore raw

PATCH 2/2 lib/dim: add KUnit test for dim_calc_stats()

In reply to Shashank Mohan Jain

Add a KUnit suite for the DIM library that checks the packet, byte, event and completion rates computed by dim_calc_stats(), including counter wraparound and windows whose byte or packet count times USEC_PER_MSEC does not fit in 32 bits. The latter cases fail on 32-bit architectures without the previous commit.

Assisted-by LLM
---
 lib/Kconfig.debug   |  15 ++++++
 lib/dim/Makefile    |   2 +
 lib/dim/dim_kunit.c | 126 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 143 insertions(+)
 create mode 100644 lib/dim/dim_kunit.c
lib/Kconfig.debug +15 -0
index 134b15a44625..6b24a2c89b84 100644
@@ -2906,6 +2906,21 @@ config LIVEUPDATE_TEST
 
 	  If unsure, say N
 
+config DIMLIB_KUNIT_TEST
+	tristate "KUnit test for the DIM library" if !KUNIT_ALL_TESTS
+	depends on KUNIT && NET
+	select DIMLIB
+	default KUNIT_ALL_TESTS
+	help
+	  This builds the KUnit test for the Dynamic Interrupt Moderation
+	  library (lib/dim/), which checks the rate statistics computed by
+	  dim_calc_stats().
+
+	  For more information on KUnit and unit tests in general please refer
+	  to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+	  If unsure, say N.
+
 config CMDLINE_KUNIT_TEST
 	tristate "KUnit test for cmdline API" if !KUNIT_ALL_TESTS
 	depends on KUNIT
lib/dim/Makefile +2 -0
index 5b9bfaac7ac1..47e1c36230e4 100644
@@ -5,3 +5,5 @@
 obj-$(CONFIG_DIMLIB) += dimlib.o
 
 dimlib-y := dim.o net_dim.o rdma_dim.o
+
+obj-$(CONFIG_DIMLIB_KUNIT_TEST) += dim_kunit.o
lib/dim/dim_kunit.c new file +126 -0

126 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH net 1/2] lib/dim: fix 32-bit overflow in dim_calc_stats() rates

In reply to Shashank Mohan Jain
Quoted 22 lines · Shashank Mohan Jain

dim_calc_stats() computes the per-millisecond rates as

DIV_ROUND_UP(nbytes * USEC_PER_MSEC, delta_us)

where nbytes is a u32 and USEC_PER_MSEC is 1000L. On 64-bit the product is done in 64-bit long arithmetic, but on 32-bit architectures long is 32 bits wide and the product wraps as soon as a measurement window carries more than 4294967 bytes (about 4.3 MB). The same applies to the packet and completion counts, although those need more than 4.29 million packets or completions per window.

A DIM window spans DIM_NEVENTS (64) events. Drivers count events per interrupt or per NAPI poll, so under sustained load a window can easily carry more than 4.3 MB: 64 full NAPI polls of 64 MTU-sized frames are already 6.2 MB, and drivers such as mtk_eth_soc count one event per interrupt while NAPI keeps polling with the interrupt masked. On 32-bit users of the library (for example mtk_eth_soc on MT7621, bcmgenet and bcmsysport on 32-bit ARM, or virtio_net in a 32-bit guest) bpms then becomes the product modulo 2^32 divided by the window length, and net_dim_stats_compare() makes its BETTER/WORSE decisions on a value that has little to do with the real throughput.

Quoted 6 lines

For example, a 1 Gbit/s link at line rate that moves 5 MB in a 40 ms window gives bpms = 125000 on 64-bit but 17626 on 32-bit, and 5 million packets in 2 s gives ppms = 353 instead of 2500.

Widen the products to 64 bits and divide with DIV_ROUND_UP_ULL(). The results are unchanged on 64-bit.

How does this 32-bit overflow differ from the overflow that can also occur on 64-bit systems in nbytes * USEC_PER_MSEC?

Thanks

#3 lore raw

Re: [PATCH net 1/2] lib/dim: fix 32-bit overflow in dim_calc_stats() rates

Quoted 2 lines

How does this 32-bit overflow differ from the overflow that can also occur on 64-bit systems in nbytes * USEC_PER_MSEC?

On 64-bit the multiplication itself cannot overflow. nbytes (like npkts and ncomps) is a u32, and USEC_PER_MSEC is 1000L, so the product is computed in a 64-bit long and is at most (2^32 - 1) * 1000, about 4.3 * 10^12 (42 bits). DIV_ROUND_UP() adds at most delta_us - 1 to that, which also fits. So on 64-bit the product is always exact.

On 32-bit the same product is computed in a 32-bit long and wraps once nbytes exceeds 4294967, i.e. about 4.3 MB in one DIM window. That is reached at normal line rates, which is what the patch fixes. With the u64 cast and DIV_ROUND_UP_ULL() the 32-bit results are the same as on 64-bit.

There are two other limits, but they apply to 32-bit and 64-bit alike and the patch does not change them:

- The counters in struct dim_sample are u32 and BIT_GAP() takes the difference modulo 2^32, so a window that carries 4 GiB or more is already undercounted before the multiplication. With 64 events per window that needs a very long window at very high rates (for example about 86 ms at 400 Gbit/s).

- The rates are stored in int fields of struct dim_stats. bpms is bytes per millisecond, so it only exceeds INT_MAX above 2^31 bytes/ms, about 17 Tbit/s.

I can add a sentence to the changelog saying that the 64-bit product cannot overflow, if you think that helps.

Thanks,
Shashank

Quoted 35 lines · Leon Romanovsky
Quoted 22 lines · Shashank Mohan Jain

dim_calc_stats() computes the per-millisecond rates as

DIV_ROUND_UP(nbytes * USEC_PER_MSEC, delta_us)

where nbytes is a u32 and USEC_PER_MSEC is 1000L. On 64-bit the product is done in 64-bit long arithmetic, but on 32-bit architectures long is 32 bits wide and the product wraps as soon as a measurement window carries more than 4294967 bytes (about 4.3 MB). The same applies to the packet and completion counts, although those need more than 4.29 million packets or completions per window.

A DIM window spans DIM_NEVENTS (64) events. Drivers count events per interrupt or per NAPI poll, so under sustained load a window can easily carry more than 4.3 MB: 64 full NAPI polls of 64 MTU-sized frames are already 6.2 MB, and drivers such as mtk_eth_soc count one event per interrupt while NAPI keeps polling with the interrupt masked. On 32-bit users of the library (for example mtk_eth_soc on MT7621, bcmgenet and bcmsysport on 32-bit ARM, or virtio_net in a 32-bit guest) bpms then becomes the product modulo 2^32 divided by the window length, and net_dim_stats_compare() makes its BETTER/WORSE decisions on a value that has little to do with the real throughput.

Quoted 6 lines

For example, a 1 Gbit/s link at line rate that moves 5 MB in a 40 ms window gives bpms = 125000 on 64-bit but 17626 on 32-bit, and 5 million packets in 2 s gives ppms = 353 instead of 2500.

Widen the products to 64 bits and divide with DIV_ROUND_UP_ULL(). The results are unchanged on 64-bit.

How does this 32-bit overflow differ from the overflow that can also occur on 64-bit systems in nbytes * USEC_PER_MSEC?

Thanks

#4 lore raw

Re: [PATCH net 1/2] lib/dim: fix 32-bit overflow in dim_calc_stats() rates

Quoted 24 lines · shashank Jain
Quoted 2 lines

How does this 32-bit overflow differ from the overflow that can also occur on 64-bit systems in nbytes * USEC_PER_MSEC?

On 64-bit the multiplication itself cannot overflow. nbytes (like npkts and ncomps) is a u32, and USEC_PER_MSEC is 1000L, so the product is computed in a 64-bit long and is at most (2^32 - 1) * 1000, about 4.3 * 10^12 (42 bits). DIV_ROUND_UP() adds at most delta_us - 1 to that, which also fits. So on 64-bit the product is always exact.

On 32-bit the same product is computed in a 32-bit long and wraps once nbytes exceeds 4294967, i.e. about 4.3 MB in one DIM window. That is reached at normal line rates, which is what the patch fixes. With the u64 cast and DIV_ROUND_UP_ULL() the 32-bit results are the same as on 64-bit.

There are two other limits, but they apply to 32-bit and 64-bit alike and the patch does not change them:

- The counters in struct dim_sample are u32 and BIT_GAP() takes the difference modulo 2^32, so a window that carries 4 GiB or more is already undercounted before the multiplication. With 64 events per window that needs a very long window at very high rates (for example about 86 ms at 400 Gbit/s).

Quoted 6 lines

- The rates are stored in int fields of struct dim_stats. bpms is bytes per millisecond, so it only exceeds INT_MAX above 2^31 bytes/ms, about 17 Tbit/s.

I can add a sentence to the changelog saying that the 64-bit product cannot overflow, if you think that helps.

There is no need.

Thanks

Quoted 40 lines

Thanks,
Shashank

Quoted 35 lines · Leon Romanovsky
Quoted 22 lines · Shashank Mohan Jain

dim_calc_stats() computes the per-millisecond rates as

DIV_ROUND_UP(nbytes * USEC_PER_MSEC, delta_us)

where nbytes is a u32 and USEC_PER_MSEC is 1000L. On 64-bit the product is done in 64-bit long arithmetic, but on 32-bit architectures long is 32 bits wide and the product wraps as soon as a measurement window carries more than 4294967 bytes (about 4.3 MB). The same applies to the packet and completion counts, although those need more than 4.29 million packets or completions per window.

A DIM window spans DIM_NEVENTS (64) events. Drivers count events per interrupt or per NAPI poll, so under sustained load a window can easily carry more than 4.3 MB: 64 full NAPI polls of 64 MTU-sized frames are already 6.2 MB, and drivers such as mtk_eth_soc count one event per interrupt while NAPI keeps polling with the interrupt masked. On 32-bit users of the library (for example mtk_eth_soc on MT7621, bcmgenet and bcmsysport on 32-bit ARM, or virtio_net in a 32-bit guest) bpms then becomes the product modulo 2^32 divided by the window length, and net_dim_stats_compare() makes its BETTER/WORSE decisions on a value that has little to do with the real throughput.

Quoted 6 lines

For example, a 1 Gbit/s link at line rate that moves 5 MB in a 40 ms window gives bpms = 125000 on 64-bit but 17626 on 32-bit, and 5 million packets in 2 s gives ppms = 353 instead of 2500.

Widen the products to 64 bits and divide with DIV_ROUND_UP_ULL(). The results are unchanged on 64-bit.

How does this 32-bit overflow differ from the overflow that can also occur on 64-bit systems in nbytes * USEC_PER_MSEC?

Thanks

#6 lore raw
In reply to Shashank Mohan Jain
This series was applied to netdev/net.git (main) by David S. Miller <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by David S. Miller <[email protected]>:

Quoted 12 lines · On Sun, 27 Sep 2026 10:47:41 +0530 you wrote:

On 32-bit kernels dim_calc_stats() multiplies the u32 byte, packet and completion counts of a DIM window by USEC_PER_MSEC (1000L) in 32-bit long arithmetic. Once a window carries more than about 4.3 MB, bpms wraps, and net_dim steers interrupt moderation on a meaningless throughput value. At 1 Gbit/s line rate a 64-event window passes that size when there are fewer than about 1,860 DIM events per second, which is common while NAPI keeps the interrupt masked under load. 32-bit users of the library include mtk_eth_soc (MT7621, MT7623), bcmgenet and bcmsysport on 32-bit ARM, xilinx_axienet on Zynq-7000 and MicroBlaze, and virtio_net in 32-bit guests. The overflow goes back to the mlx5e code the library was moved from.

Quoted 1 line

[...]

Here is the summary with links:
- [net,1/2] lib/dim: fix 32-bit overflow in dim_calc_stats() rates

    https://git.kernel.org/netdev/net/c/0843b2389064
  - [net,2/2] lib/dim: add KUnit test for dim_calc_stats()
    https://git.kernel.org/netdev/net/c/636035157a81

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html