Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Mathieu Desnoyers" <mathieu.desnoyers@efficios.com>,
	"David Hildenbrand" <david@kernel.org>
Cc: "Paul E. McKenney" <paulmck@kernel.org>,
	"Steven Rostedt" <rostedt@goodmis.org>,
	"Masami Hiramatsu" <mhiramat@kernel.org>,
	"Dennis Zhou" <dennis@kernel.org>, "Tejun Heo" <tj@kernel.org>,
	"Christoph Lameter" <cl@gentwo.org>,
	"Martin Liu" <liumartin@google.com>,
	"David Rientjes" <rientjes@google.com>,
	christian.koenig@amd.com, "Shakeel Butt" <shakeel.butt@linux.dev>,
	"SeongJae Park" <sj@kernel.org>, "Michal Hocko" <mhocko@suse.com>,
	"Johannes Weiner" <hannes@cmpxchg.org>,
	"Sweet Tea Dorminy" <sweettea-kernel@dorminy.me>,
	"Lorenzo Stoakes" <ljs@kernel.org>,
	"Liam R. Howlett" <liam@infradead.org>,
	"Mike Rapoport" <rppt@kernel.org>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"Vlastimil Babka" <vbabka@kernel.org>,
	"Christian Brauner" <brauner@kernel.org>,
	"Wei Yang" <richard.weiyang@gmail.com>,
	"Miaohe Lin" <linmiaohe@huawei.com>,
	"Al Viro" <viro@zeniv.linux.org.uk>,
	"Yu Zhao" <yuzhao@google.com>,
	"Roman Gushchin" <roman.gushchin@linux.dev>,
	"Mateusz Guzik" <mjguzik@gmail.com>,
	"Matthew Wilcox" <willy@infradead.org>,
	"Baolin Wang" <baolin.wang@linux.alibaba.com>,
	"Aboorva Devarajan" <aboorvad@linux.ibm.com>,
	"David Carlier" <devnexen@gmail.com>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	linux-mm@kvack.org, "Jonathan Corbet" <corbet@lwn.net>,
	"Shuah Khan" <skhan@linuxfoundation.org>,
	"Randy Dunlap" <rdunlap@infradead.org>,
	"Ingo Molnar" <mingo@redhat.com>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Juri Lelli" <juri.lelli@redhat.com>,
	"Vincent Guittot" <vincent.guittot@linaro.org>,
	"Dietmar Eggemann" <dietmar.eggemann@arm.com>,
	"Ben Segall" <bsegall@google.com>, "Mel Gorman" <mgorman@suse.de>,
	"Valentin Schneider" <vschneid@redhat.com>,
	"K Prateek Nayak" <kprateek.nayak@amd.com>,
	"Kees Cook" <kees@kernel.org>,
	linux-trace-kernel@vger.kernel.org, "Jan Kara" <jack@suse.cz>,
	"Oleg Nesterov" <oleg@redhat.com>
Subject: Re: [RFC PATCH v24 06/10] lib: percpu_counter_tree: add lazy tree activation
Date: Fri, 06 Nov 2026 20:37:36 +0000	[thread overview]
Message-ID: <sashiko-outbox-202244@kernel.org> (raw)
In-Reply-To: <20261105203639.1459524-7-mathieu.desnoyers@efficios.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] lib: percpu_counter_tree_kunit: missing release barrier on sh.go
--

commit badb1aea7bc81016992df61a32943f786fc44dcf
Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>

lib: percpu_counter_tree: add lazy tree activation

This commit introduces lazy tree initialization and activation for
percpu_counter_tree so counters can remain exact without allocating per-CPU
or tree memory until activated. It also adds KUnit and userspace tests for
lazy counter updates and concurrent activation.

> diff --git a/lib/tests/percpu_counter_tree_kunit.c b/lib/tests/percpu_counter_tree_kunit.c
> index 5774278ea1403..156bb63de3466 100644
> --- a/lib/tests/percpu_counter_tree_kunit.c
> +++ b/lib/tests/percpu_counter_tree_kunit.c
> @@ -796,6 +798,220 @@ static void hpcc_test_saturated_compare(struct kunit *test)

[ ... ]

> +static void lazy_race_fn(struct work_struct *work)
> +{
> +	struct lazy_race_work *w = container_of(work, struct lazy_race_work, work);
> +	struct lazy_race_shared *sh = w->sh;
> +	long sum = 0;
> +	int i;
> +
> +	for (i = 0; i < LAZY_RACE_NR_INC; i++) {
> +		long inc = (long)(get_random_u32() % 65) - 32;
> +
> +		if (i == LAZY_RACE_NR_FIRST) {
> +			/* Fully ordered: the first updates are visible before the pause. */
> +			atomic_long_add_return(sum, &sh->first_sum);
> +			atomic_inc_return(&sh->paused);
> +			while (!READ_ONCE(sh->go))
> +				cond_resched();
> +			if (!w->owner) {
> +				mutex_lock(&sh->activate_lock);
> +				if (percpu_counter_tree_activate_many(sh->c, 1, GFP_KERNEL) ||
> +				    !percpu_counter_tree_active(sh->c))
> +					atomic_inc(&sh->activate_errors);
> +				mutex_unlock(&sh->activate_lock);
> +			}
> +		}
> +		if (w->owner)
> +			percpu_counter_tree_add_owner(sh->c, inc);
> +		else
> +			percpu_counter_tree_add(sh->c, inc);
> +		sum += inc;
> +		if (!(i & 1023))
> +			cond_resched();
> +	}
> +	atomic_long_add(sum, &sh->expect);
> +	kfree(w);
> +	complete_work();
> +}
> +
> +static void hpcc_test_lazy_activation_race(struct kunit *test)
> +{
> +	struct percpu_counter_tree pct;
> +	int round;
> +
> +	if (num_online_cpus() < 2)
> +		kunit_skip(test, "needs at least 2 online CPUs");
> +	for (round = 0; round < 4; round++) {
> +		struct lazy_race_shared sh = { .c = &pct };
> +		unsigned long timeout = jiffies + 10 * HZ;
> +		int cpu, nr_workers = 0;
> +
> +		KUNIT_ASSERT_EQ(test, 0, percpu_counter_tree_init_many_lazy(&pct, 1, 32));
> +		mutex_init(&sh.activate_lock);
> +		init_kthreads();
> +		cpus_read_lock();
> +		for_each_online_cpu(cpu) {
> +			struct lazy_race_work *w;
> +
> +			if (nr_workers > LAZY_RACE_MAX_REMOTE)
> +				break;
> +			w = kzalloc_obj(*w);
> +			KUNIT_EXPECT_NOT_NULL(test, w);
> +			if (!w)
> +				break;
> +			INIT_WORK(&w->work, lazy_race_fn);
> +			w->sh = &sh;
> +			/* The first worker is the owner, the others update remotely. */
> +			w->owner = !nr_workers;
> +			atomic_inc(&kernel_threads_to_run);
> +			queue_work_on(cpu, hpcc_test_wq, &w->work);
> +			nr_workers++;
> +		}
> +		cpus_read_unlock();
> +		while (atomic_read(&sh.paused) < nr_workers && time_before(jiffies, timeout))
> +			usleep_range(100, 200);
> +		smp_mb();	/* Read the counter after the first updates of the workers. */
> +		/* Every worker, the owner included, has updates left for after the activation. */
> +		KUNIT_EXPECT_EQ(test, atomic_read(&sh.paused), nr_workers);
> +		/* Before activation, the owner and atomic updates are exact. */
> +		check_lazy_counter(test, &pct, atomic_long_read(&sh.first_sum));
> +		WRITE_ONCE(sh.go, true);

[Severity: Low]
In hpcc_test_lazy_activation_race(), can a weakly ordered CPU reorder the
relaxed WRITE_ONCE(sh.go, true) before the loads of pct.bias and
pct.atomic_bias inside check_lazy_counter()?

Before activation, pct.level0 is NULL, so
percpu_counter_tree_precise_sum_unbiased() returns early after
smp_load_acquire(&counter->level0) and atomic_long_read(&counter->atomic_bias)
without issuing a barrier after reading atomic_bias and bias.

Once sh.go becomes true in lazy_race_fn(), the owner worker immediately calls
percpu_counter_tree_add_owner(sh->c, inc) without acquiring sh->activate_lock,
and remote workers proceed to activate and update sh->c.

Should sh.go be set with smp_store_release(&sh.go, true) (or preceded by
smp_mb()) so the pre-activation reads in check_lazy_counter() complete before
the paused workers resume modifying pct?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261105203639.1459524-1-mathieu.desnoyers@efficios.com?part=6


  reply	other threads:[~2026-11-06 20:37 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-05 20:36 [RFC PATCH v24 00/10] Hierarchical Percpu Counters for RSS Mathieu Desnoyers
2026-11-05 20:36 ` [RFC PATCH v24 01/10] lib: introduce hierarchical per-cpu counters Mathieu Desnoyers
2026-11-05 20:36 ` [RFC PATCH v24 02/10] lib: test " Mathieu Desnoyers
2026-11-05 20:36 ` [RFC PATCH v24 03/10] mm: use hierarchical per-cpu counters for the RSS counters Mathieu Desnoyers
2026-11-05 20:36 ` [RFC PATCH v24 04/10] tools/testing/hpcc: add userspace test of hierarchical per-CPU counters Mathieu Desnoyers
2026-11-05 20:36 ` [RFC PATCH v24 05/10] mm: report the approximate RSS in the rss_stat tracepoint Mathieu Desnoyers
2026-11-05 20:36 ` [RFC PATCH v24 06/10] lib: percpu_counter_tree: add lazy tree activation Mathieu Desnoyers
2026-11-06 20:37   ` sashiko-bot [this message]
2026-11-05 20:36 ` [RFC PATCH v24 07/10] mm: keep the RSS counters of single-threaded processes exact Mathieu Desnoyers
2026-11-05 20:36 ` [RFC PATCH v24 08/10] lib: percpu_counter_tree: add the arity boot parameter Mathieu Desnoyers
2026-11-05 20:36 ` [RFC PATCH v24 09/10] mm: add the rss_stat_batch " Mathieu Desnoyers
2026-11-05 20:36 ` [RFC PATCH v24 10/10] lib: add a benchmark of hierarchical per-cpu counters Mathieu Desnoyers
2026-11-06 20:37   ` sashiko-bot

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=sashiko-outbox-202244@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=aboorvad@linux.ibm.com \
    --cc=akpm@linux-foundation.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=brauner@kernel.org \
    --cc=bsegall@google.com \
    --cc=christian.koenig@amd.com \
    --cc=cl@gentwo.org \
    --cc=corbet@lwn.net \
    --cc=david@kernel.org \
    --cc=dennis@kernel.org \
    --cc=devnexen@gmail.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=hannes@cmpxchg.org \
    --cc=jack@suse.cz \
    --cc=juri.lelli@redhat.com \
    --cc=kees@kernel.org \
    --cc=kprateek.nayak@amd.com \
    --cc=liam@infradead.org \
    --cc=linmiaohe@huawei.com \
    --cc=linux-mm@kvack.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=liumartin@google.com \
    --cc=ljs@kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mgorman@suse.de \
    --cc=mhiramat@kernel.org \
    --cc=mhocko@suse.com \
    --cc=mingo@redhat.com \
    --cc=mjguzik@gmail.com \
    --cc=oleg@redhat.com \
    --cc=paulmck@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rdunlap@infradead.org \
    --cc=richard.weiyang@gmail.com \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=rostedt@goodmis.org \
    --cc=rppt@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=shakeel.butt@linux.dev \
    --cc=sj@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=surenb@google.com \
    --cc=sweettea-kernel@dorminy.me \
    --cc=tj@kernel.org \
    --cc=vbabka@kernel.org \
    --cc=vincent.guittot@linaro.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=vschneid@redhat.com \
    --cc=willy@infradead.org \
    --cc=yuzhao@google.com \
    /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