From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: Andrew Morton <akpm@linux-foundation.org>,
David Carlier <devnexen@gmail.com>
Cc: Josh Law <objecting@objecting.org>,
Dennis Zhou <dennis@kernel.org>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] lib: fix compare_delta parameter order in percpu_counter_tree
Date: Sun, 15 Mar 2026 18:00:05 -0400 [thread overview]
Message-ID: <b1b482f8-16dc-4c6a-bd47-5cc8599c0db9@efficios.com> (raw)
In-Reply-To: <20260314144508.ba3504b4338eb1ae84017028@linux-foundation.org>
On 2026-03-14 17:45, Andrew Morton wrote:
> On Fri, 13 Mar 2026 17:54:24 +0000 David Carlier <devnexen@gmail.com> wrote:
>
Thanks Andrew for CCing me on this.
>> The compare_delta() helper takes (delta, accuracy_neg, accuracy_pos),
>> but every call site passes (delta, accuracy_pos, accuracy_neg) — the
>> last two arguments are consistently swapped.
>>
>> The documented invariant (include/linux/percpu_counter_tree.h) is:
>>
>> (precise_sum - under) <= approx_sum <= (precise_sum + over)
>>
>> Which means precise_sum is in [approx_sum - over, approx_sum + under].
Yes, as also documented in the comment above
percpu_counter_tree_approximate_min_max_range().
>>
>> For a positive delta (v - approx_sum >= 0), accuracy_pos must be
>> "under" (the maximum amount precise_sum can exceed approx_sum).
>> For a negative delta, accuracy_neg must be "over".
Right, I mistakenly swapped the logic there. I did not consider that we
have "approximated value" as input and wish to compare precise ranges.
>> Since under > over
>> always (batch_size * M vs (batch_size - 1) * M), swapping them causes
>> false definitive results: the functions return 1 ("v > counter") when
>> the correct answer is 0 (indeterminate).
>>
>> This affects all comparison functions:
>> - percpu_counter_tree_approximate_compare_value()
>> - percpu_counter_tree_approximate_compare()
>> - percpu_counter_tree_precise_compare_value()
>> - percpu_counter_tree_precise_compare()
>>
>> The precise variants are also affected because their approximate
>> fast-path can short-circuit with a wrong result, skipping the precise
>> sum computation.
Yes.
>>
>> Fix by swapping the parameter order in compare_delta() itself, since
>> all call sites are consistently swapped.
That seems like a simple fix for that problem indeed. Did you run the
kunit tests on the fixed code ? Any thought on how to extend the kunit
test to cover this ?
>
> This affects mm-unstable's "lib: introduce hierarchical per-cpu
> counters", so let's cc Mathieu.
Thanks!
Reviewed-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
>
>> --- a/lib/percpu_counter_tree.c
>> +++ b/lib/percpu_counter_tree.c
>> @@ -458,7 +458,7 @@ long percpu_counter_tree_precise_sum(struct percpu_counter_tree *counter)
>> EXPORT_SYMBOL_GPL(percpu_counter_tree_precise_sum);
>>
>> static
>> -int compare_delta(long delta, unsigned long accuracy_neg, unsigned long accuracy_pos)
>> +int compare_delta(long delta, unsigned long accuracy_pos, unsigned long accuracy_neg)
>> {
>> if (delta >= 0) {
>> if (delta <= accuracy_pos)
>
--
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com
next prev parent reply other threads:[~2026-03-15 22:00 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-13 17:54 David Carlier
2026-03-14 15:30 ` Josh Law
2026-03-14 21:45 ` Andrew Morton
2026-03-15 22:00 ` Mathieu Desnoyers [this message]
2026-03-15 22:47 ` David CARLIER
2026-03-15 23:16 ` Mathieu Desnoyers
2026-03-16 0:05 ` David CARLIER
2026-03-16 0:41 ` Mathieu Desnoyers
2026-03-16 4:28 ` David CARLIER
2026-03-16 13:06 ` Mathieu Desnoyers
2026-03-16 13:41 ` David CARLIER
2026-03-16 13:53 ` Mathieu Desnoyers
2026-03-16 14:15 ` David CARLIER
2026-03-16 14:15 ` David CARLIER
2026-03-16 14:23 ` Mathieu Desnoyers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b1b482f8-16dc-4c6a-bd47-5cc8599c0db9@efficios.com \
--to=mathieu.desnoyers@efficios.com \
--cc=akpm@linux-foundation.org \
--cc=dennis@kernel.org \
--cc=devnexen@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=objecting@objecting.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®