Re: [PATCH] ACPI: LPIT: Avoid u64 multiplication overflow in residency counters
From: Rafael J. Wysocki (Intel)
Date: Wed Dec 02 2026 - 14:47:35 EST
On Mon, Nov 30, 2026 at 6:06 AM Moncef Mechri via B4 Relay
<devnull+moncef.mechri.gmail.com@xxxxxxxxxx> wrote:
>
> From: Moncef Mechri <moncef.mechri@xxxxxxxxx>
>
> lpit_read_residency_counter_us() converts a raw residency counter to
> microseconds as div64_u64(counter * 1000000ULL, frequency). The
> multiplication overflows u64 once the counter exceeds U64_MAX / 1000000,
> about 1.8e13 ticks, so a wide counter can wrap in the conversion long
> before the hardware counter rolls over.
>
> On a Dell XPS 13 7390 (i7-10710U), the LPIT CPU residency counter is
> MSR 0x632, 64 bits wide, with a counter frequency of 0, so
> lpit_update_residency() uses tsc_khz (1,599,960 kHz at that point). The
> exported low_power_idle_cpu_residency_us therefore wraps after about
> 11,529 s (3.2 hours) of total residency. On that laptop, MSR 0x632 read
> 36462035739024 while the sysfs file showed 11259838786 us, which is
> exactly ((36462035739024 * 1000000) mod 2^64) / 1599960000. The
> correct value is 22789342070 us. Across a 20,995 s suspend-to-idle the
> sysfs value went up by only 9,464 s, which made the package look as if
> it had been in C10 for less than half of the suspend.
>
> The memory-mapped system residency counter on this laptop is 32 bits
> wide at 9580 Hz, so its conversion cannot overflow there.
>
> Convert whole seconds and the remainder separately. The remainder is
> smaller than the frequency, so mul_u64_u64_div_u64() can scale it
> without overflowing, and its quotient always fits in u64. Applied to
> the full counter instead, that helper would raise #DE on x86-64 when the
> quotient does not fit, which the frequency = 1 fallback in
> lpit_update_residency() could cause. Use the new helper for both the
> FFH and the memory-mapped counters.
>
> The result is now exact whenever it fits in u64. One that does not,
> which takes a frequency below 1 MHz, still wraps modulo 2^64.
>
> Checked in userspace against an exact 128-bit computation, including
> frequencies up to U64_MAX and 2 million pseudorandom inputs. On the
> same laptop, a test module containing the same helper ran it in kernel
> context on boundary inputs and on the live MSR value, and matched the
> expected u64 results, including the wrap when the quotient exceeded
> U64_MAX. The patched file was compile-tested with W=1 against
> Fedora 7.3.6 x86-64 headers; a kernel with this patch has not been
> booted.
>
> Fixes: eeb2d80d502a ("ACPI / LPIT: Add Low Power Idle Table (LPIT) support")
> Assisted-by: Claude:claude-opus-5-5
> Assisted-by: Codex:gpt-6.2-sol
> Signed-off-by: Moncef Mechri <moncef.mechri@xxxxxxxxx>
Applied as 7.5 material, thanks!
> ---
> drivers/acpi/acpi_lpit.c | 20 ++++++++++++++++++--
> 1 file changed, 18 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/acpi/acpi_lpit.c b/drivers/acpi/acpi_lpit.c
> index b8d98b1b4..cab51d8dd 100644
> --- a/drivers/acpi/acpi_lpit.c
> +++ b/drivers/acpi/acpi_lpit.c
> @@ -8,6 +8,7 @@
>
> #include <linux/cpu.h>
> #include <linux/acpi.h>
> +#include <linux/math64.h>
> #include <asm/msr.h>
> #include <asm/tsc.h>
> #include "internal.h"
> @@ -22,6 +23,21 @@ struct lpit_residency_info {
> static struct lpit_residency_info residency_info_mem;
> static struct lpit_residency_info residency_info_ffh;
>
> +/*
> + * Convert a residency counter value to microseconds. Multiplying the full
> + * counter by 1000000 overflows u64 once it exceeds about 1.8e13 ticks
> + * (about 3.2 hours at 1.6 GHz), so convert whole seconds and the
> + * remainder separately. The remainder is below the frequency, so its
> + * scaled quotient is below 1000000 and cannot overflow.
> + */
> +static u64 lpit_residency_us(u64 count, u64 frequency)
> +{
> + u64 rem, sec;
> +
> + sec = div64_u64_rem(count, frequency, &rem);
> + return sec * 1000000ULL + mul_u64_u64_div_u64(rem, 1000000ULL, frequency);
> +}
> +
> static int lpit_read_residency_counter_us(u64 *counter, bool io_mem)
> {
> int err;
> @@ -35,7 +51,7 @@ static int lpit_read_residency_counter_us(u64 *counter, bool io_mem)
> if (error)
> return error;
>
> - *counter = div64_u64(count * 1000000ULL, residency_info_mem.frequency);
> + *counter = lpit_residency_us(count, residency_info_mem.frequency);
> return 0;
> }
>
> @@ -47,7 +63,7 @@ static int lpit_read_residency_counter_us(u64 *counter, bool io_mem)
>
> *counter &= mask;
> *counter >>= residency_info_ffh.gaddr.bit_offset;
> - *counter = div64_u64(*counter * 1000000ULL, residency_info_ffh.frequency);
> + *counter = lpit_residency_us(*counter, residency_info_ffh.frequency);
> return 0;
> }
>
>
> ---
> base-commit: 4f069ab1ec243e6b60dc438772905855c60774e8
> change-id: 20261129-acpi-lpit-residency-overflow-62309378de7f
>
> Best regards,
> --
> Moncef Mechri <moncef.mechri@xxxxxxxxx>
>
>