Re: [PATCH v3 1/2] mm/page_counter: avoid integer overflow in effective_protection()
From: David Laight
Date: Sun Sep 06 2026 - 06:16:35 EST
On Sun, 6 Sep 2026 09:11:24 +0800
Ridong Chen <ridong.chen@xxxxxxxxx> wrote:
> On 9/4/2026 4:37 PM, David Laight wrote:
> > On Thu, 3 Sep 2026 11:19:51 +0800
> > Ridong Chen <ridong.chen@xxxxxxxxx> wrote:
> >
> >> From: Ridong Chen <chenridong@xxxxxxxxxx>
> >>
> >> effective_protection() scales a parent's protection by a ratio of page
> >> counts, e.g. for recursive protection:
> >>
> >> (parent_effective - siblings_protected) * (usage - protected)
> >> / (parent_usage - siblings_protected)
> >>
> >> The multiply is done at unsigned long width before dividing. On systems
> >> with >= 16TB RAM the product can exceed 2^64 and wrap, giving a bogus
> >> protection value and silently breaking memory.min/low enforcement.
> >>
> >> Use mul_u64_u64_div_u64() to multiply in a 128-bit intermediate. Because
> >> usage and parent_usage are not read atomically (a child is charged
> >> before its parent), usage - protected can briefly exceed the divisor,
> >> making the quotient overflow 64 bits and trap (#DE on x86). Cap it so
> >> the ratio stays <= 1.
> >>
> >> Reported by the sashiko review tool [1].
> >>
> >> [1] https://sashiko.dev/#/patchset/20260826133054.88529-1-ridong.chen@xxxxxxxxx?part=1
> >>
> >> Fixes: bc50bcc6e00b ("mm: memcontrol: clean up and document effective low/min calculations")
> >> Fixes: 8a931f801340 ("mm: memcontrol: recursive memory.low protection")
> >> Cc: stable@xxxxxxxxxxxxxxx
> >> Assisted-by: Claude:claude-opus-4-8
> >> Reviewed-by: Barry Song <baohua@xxxxxxxxxx>
> >> Signed-off-by: Ridong Chen <chenridong@xxxxxxxxxx>
> >> ---
> >> mm/page_counter.c | 19 +++++++++++++------
> >> 1 file changed, 13 insertions(+), 6 deletions(-)
> >>
> >> diff --git a/mm/page_counter.c b/mm/page_counter.c
> >> index 661e0f2a5127..e8bd512069c5 100644
> >> --- a/mm/page_counter.c
> >> +++ b/mm/page_counter.c
> >> @@ -8,6 +8,7 @@
> >> #include <linux/page_counter.h>
> >> #include <linux/atomic.h>
> >> #include <linux/kernel.h>
> >> +#include <linux/math64.h>
> >> #include <linux/string.h>
> >> #include <linux/sched.h>
> >> #include <linux/bug.h>
> >> @@ -356,7 +357,8 @@ static unsigned long effective_protection(unsigned long usage,
> >> * otherwise get a smaller chunk than what they claimed.
> >> */
> >> if (siblings_protected > parent_effective)
> >> - return protected * parent_effective / siblings_protected;
> >> + return mul_u64_u64_div_u64(protected, parent_effective,
> >> + siblings_protected);
> >
> > On 32bit it is only necessary to use a 64bit intermediary.
> > mul_u64_u64_div_u64() will drop back to the (probably faster) 64 by 64
> > divide (and then maybe to a 64 by 32 one).
> > But there is a lot of extra code before that happens.
> >
>
> Hi David,
>
> Thank you for your review.
>
> Do you want me to split this for 32-bit, or is keeping the single
> mul_u64_u64_div_u64() call for readability fine with you?
Maybe I'll look at wrapping mul_u64_add_u64_div_u64() with something that
checks statically_true(((a) | (b) | (c) | (d)) < 1ull << 32).
Probably neater than the conditional here.
>
> >>
> >> /*
> >> * Ok, utilized protection of all children is within what the
> >> @@ -397,13 +399,18 @@ static unsigned long effective_protection(unsigned long usage,
> >> if (parent_effective > siblings_protected &&
> >> parent_usage > siblings_protected &&
> >> usage > protected) {
> >> - unsigned long unclaimed;
> >> + unsigned long unclaimed = parent_effective - siblings_protected;
> >> + unsigned long unprotected = usage - protected;
> >> + unsigned long parent_unprotected = parent_usage - siblings_protected;
> >>
> >> - unclaimed = parent_effective - siblings_protected;
> >> - unclaimed *= usage - protected;
> >> - unclaimed /= parent_usage - siblings_protected;
> >> + /*
> >> + * The usages aren't read atomically, so a child can transiently
> >> + * appear to use more than its parent, making the ratio exceed 1
> >> + * and the quotient overflow 64 bits (#DE on x86). Cap it.
> >> + */
> >> + unprotected = min(unprotected, parent_unprotected);
> >>
> >> - ep += unclaimed;
> >> + ep += mul_u64_u64_div_u64(unclaimed, unprotected, parent_unprotected);
> >
> > If the ratio is forced to 1 there is no point doing the scaling.
> > So maybe:
> > if (likely(parent_unprotected > unprotected))
> > unclaimed = mul_u64_u64_div_u64(unclaimed, unprotected,
> > parent_unprotected);
> > ep += unclaimed;
> > OTOH if the min() generates a cmov rather than a conditional branch
> > then you don't get a statically mispredicted branch in the normal case
> > (which is very likely with the empty 'else' branch).
> >
>
> I'd rather keep it as-is and avoid the extra branch. Does that work for you?
I nearly came to that conclusion while writing the comment!
Actually the same might go for the other three tests - generating a 0 or 1
from a conditional move ought to be better than the possibly mispredicted
very unlikely branch.
(I am right in thinking that they should never fail?)
I also suspect the #UD is the least of the problems.
You don't want the scaling to produce a bigger number at all.
David
>
> >
> >> }
> >>
> >> return ep;
> >
>