All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] lib: fix compare_delta parameter order in percpu_counter_tree
@ 2026-03-13 17:54 David Carlier
  2026-03-14 15:30 ` Josh Law
  2026-03-14 21:45 ` Andrew Morton
  0 siblings, 2 replies; 15+ messages in thread
From: David Carlier @ 2026-03-13 17:54 UTC (permalink / raw)
  To: Andrew Morton, Josh Law, Dennis Zhou; +Cc: linux-kernel, David Carlier

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].

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". 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.

Fix by swapping the parameter order in compare_delta() itself, since
all call sites are consistently swapped.

Signed-off-by: David Carlier <devnexen@gmail.com>
---
 lib/percpu_counter_tree.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/percpu_counter_tree.c b/lib/percpu_counter_tree.c
index 5c8fc2dcdc16..beb1144e6450 100644
--- 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)
-- 
2.51.0


^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2026-03-16 14:23 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-13 17:54 [PATCH] lib: fix compare_delta parameter order in percpu_counter_tree David Carlier
2026-03-14 15:30 ` Josh Law
2026-03-14 21:45 ` Andrew Morton
2026-03-15 22:00   ` Mathieu Desnoyers
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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.