mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: David CARLIER <devnexen@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	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 20:41:15 -0400	[thread overview]
Message-ID: <b10b607b-8852-4b5d-86fe-c0515a6e7bae@efficios.com> (raw)
In-Reply-To: <CA+XhMqzrcsiDi_Uu7M9jL0Hst2dopNSpmMUiwmzV1j_yyUXDEA@mail.gmail.com>

On 2026-03-15 20:05, David CARLIER wrote:
> Ok what about this revised version ?
> 
> static void hpcc_test_compare_value_boundaries(struct kunit *test)
>    {
>          struct percpu_counter_tree pct;
>          struct percpu_counter_tree_level_item *counter_items;
>          unsigned long under = 0, over = 0;
>          int ret;
> 
>          counter_items = kzalloc(percpu_counter_tree_items_size(), GFP_KERNEL);
>          KUNIT_ASSERT_PTR_NE(test, counter_items, NULL);
>          ret = percpu_counter_tree_init(&pct, counter_items, 32, GFP_KERNEL);
>          KUNIT_ASSERT_EQ(test, ret, 0);
> 
>          percpu_counter_tree_set(&pct, 0);
>          percpu_counter_tree_approximate_accuracy_range(&pct, &under, &over);
> 
>          /*
>           * With approx_sum = precise_sum = 0, from the accuracy invariant:
>           *   approx_sum - over <= precise_sum <= approx_sum + under
>           * Positive deltas use 'under' as tolerance, negative use 'over'.
>           */
> 
>          /* At boundary: indeterminate */
>          KUNIT_EXPECT_EQ(test, 0,
>                  percpu_counter_tree_approximate_compare_value(&pct,
> (long)under));
>          KUNIT_EXPECT_EQ(test, 0,
>                  percpu_counter_tree_approximate_compare_value(&pct,
> -(long)over));
> 
>          /* Beyond boundary: definitive */
>          KUNIT_EXPECT_EQ(test, 1,
>                  percpu_counter_tree_approximate_compare_value(&pct,
>                          (long)(under + 1)));
>          KUNIT_EXPECT_EQ(test, -1,
>                  percpu_counter_tree_approximate_compare_value(&pct,
>                          -(long)(over + 1)));
> 
>          /*
>           * When ranges are asymmetric, test values in the gap between the
>           * smaller and larger range to catch swapped accuracy parameters.
>           * Determine which range is larger at runtime to avoid assuming a
>           * specific relationship between under and over.
>           */
>          if (under != over) {
>                  unsigned long a = max(under, over);
>                  unsigned long b = min(under, over);

Looking at the resulting code, I wonder if the a/b variables are
useful after all.

> 
>                  /*
>                   * b + 1 is beyond the smaller range but within the larger.
>                   * The side with the larger tolerance must return indeterminate,
>                   * the side with the smaller tolerance must return definitive.
>                   */
>                  if (a == under) {

This can become if (under > over) {

>                          /* Positive side has larger tolerance */
>                          KUNIT_EXPECT_EQ(test, 0,
>                                  percpu_counter_tree_approximate_compare_value(
>                                          &pct, (long)(b + 1)));

"b" becomes "under"

>                          KUNIT_EXPECT_EQ(test, -1,
>                                  percpu_counter_tree_approximate_compare_value(
>                                          &pct, -(long)(b + 1)));

same.

>                  } else {
>                          /* Negative side has larger tolerance */
>                          KUNIT_EXPECT_EQ(test, 1,
>                                  percpu_counter_tree_approximate_compare_value(
>                                          &pct, (long)(b + 1)));

"b" becomes "over".

>                          KUNIT_EXPECT_EQ(test, 0,
>                                  percpu_counter_tree_approximate_compare_value(
>                                          &pct, -(long)(b + 1)));

same.

Thanks,

Mathieu

>                  }
>          }
> 
>          percpu_counter_tree_destroy(&pct);
>          kfree(counter_items);
>    }
-- 
Mathieu Desnoyers
EfficiOS Inc.
https://www.efficios.com

  reply	other threads:[~2026-03-16  0:41 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
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 [this message]
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=b10b607b-8852-4b5d-86fe-c0515a6e7bae@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®