Skip to content

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

Shashank Mohan Jain lkml netdev 1 version Applied

Status from this message: This series was applied to netdev/net.git (main)

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest2 patches 3 repliesApplied 2/2

Patches in v1 All 2 applied

#Subject and filesSizeRepliesApplied
1/2lib/dim: fix 32-bit overflow in dim_calc_stats() rateslib/dim/dim.c+7 -33 repliesApplied
2/2lib/dim: add KUnit test for dim_calc_stats()lib/Kconfig.debug lib/dim/Makefile lib/dim/dim_kunit.c+143 -00 repliesApplied

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH net 0/2] 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