mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Changwoo Min <changwoo@igalia.com>
To: Alexei Starovoitov <alexei.starovoitov@gmail.com>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Kumar Kartikeya Dwivedi <memxor@gmail.com>,
	Shuah Khan <shuah@kernel.org>,
	Emil Tsalapatis <emil@etsalapatis.com>
Cc: Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	Jiri Olsa <jolsa@kernel.org>,
	Ihor Solodrai <ihor.solodrai@linux.dev>,
	david.dai@linux.dev, Gavin Guo <gavinguo@igalia.com>,
	bpf@vger.kernel.org, sched-ext@lists.linux.dev,
	linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org,
	kernel-dev@igalia.com
Subject: Re: [PATCH bpf-next 2/3] selftests/bpf: libarena: Cache the rbtree's leftmost node and count
Date: Tue, 3 Nov 2026 22:14:15 +0900	[thread overview]
Message-ID: <bcf2e1c2-bc97-4d8e-a690-a1836a371033@igalia.com> (raw)
In-Reply-To: <DMJGWV96Y5SV.33OTYFHFZQ1WT@gmail.com>

Hi Alexei,

Thanks for the review.

On 11/1/26 10:03 AM, Alexei Starovoitov wrote:
> On Tue, Oct 27, 2026 at 11:00 PM Changwoo Min <changwoo@igalia.com> wrote:
>>   struct rbtree {
>> +	/* Writer fields, on a cache line of their own. */
>>   	struct rbnode __arena *root;
>>   	enum rbtree_alloc alloc;
>>   	enum rbtree_insert_mode insert;
>> +
>> +	/*
>> +	 * The leftmost node and the node count on their own cache line.
>> +	 * The writer keeps them current, and readers may read them without
>> +	 * the writer's lock (see rb_first() and friends).
>> +	 */
>> +	struct {
>> +		struct rbnode __arena *first;
>> +		uint64_t nr;
>> +	} __attribute__((aligned(RB_CACHELINE_SIZE)));
>>   };
> 
> That's 16 -> 128 bytes.
> Without perf numbers it's a premature optimization.

You are right. I ran a scheduler-side task queue built on this rbtree
(based on scx_lavd replacing its DSQs) on a 2-socket, 192-CPU EPYC
machine (no hyperthreading) to compare the aligned vs. unaligned
versions. The following are the results of the 10 runs.

                                     aligned        unaligned
   perf bench sched messaging (s)    0.51 +- 0.01   0.50 +- 0.01
   perf bench sched pipe (us/op)     3.18 +- 0.04   3.19 +- 0.04
   schbench full, wakeup p99 (us)    78.3 +- 5.7    79.5 +- 3.6
   schbench full, RPS                44941 +- 124   44954 +- 86
   schbench 2x over, RPS             45146 +- 101   45133 +- 141

There are no statistically meaningful differences so I will drop the
alignment in v2.

> 
>> +	ret = rbnode_remove(rbtree, node);
>> +	if (ret)
>> +		return ret;
>> +
>> +	if (was_first)
>> +		rb_set_first(rbtree, next);
> 
> In an RB_ALLOC tree rbnode_remove() frees the node and then rebalances
> the tree. first points to the freed node all that time:
> 
>    CPU 0, holds the lock            CPU 1, no lock
>    rb_pop()
>      rb_node_remove()
>        rbnode_remove()
>          rb_node_free(node)
>                                     rb_first_key()
>                                       first = rbtree->first
>                                       first->key    <- use after free
>          rebalance
>        rb_set_first(next)
> 
> buddy_free() poisons the node. A prog built with ASAN gets a report from
> rb_first_key().
> 
> rb_erase_cached() moves rb_leftmost before rb_erase().
> Should it be the same here?
> Move rb_next() from patch 3 in front of this patch. Then at the top of
> rb_node_remove():
> 
>    if (rbtree->first == node)
>            rb_set_first(rbtree, rb_next(node));
>    WRITE_ONCE(rbtree->nr, rbtree->nr - 1);
> 
> and no need for the wrapper, was_first and the rename to rbnode_remove().
> 

Sure, I will move rb_next() before this patch and update first and
nr at the top of rb_node_remove() in v2, as you suggested.

Regards,
Changwoo Min

> Patch 1 looks good.
> 
> pw-bot: cr


  reply	other threads:[~2026-11-03 13:14 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-28  6:00 [PATCH bpf-next 0/3] selftests/bpf: libarena: Add ordered-walk and lockless-peek support to the rbtree Changwoo Min
2026-10-28  6:00 ` [PATCH bpf-next 1/3] selftests/bpf: libarena: Order equal rbtree keys FIFO Changwoo Min
2026-11-02  7:51   ` Emil Tsalapatis
2026-11-03 13:14     ` Changwoo Min
2026-10-28  6:00 ` [PATCH bpf-next 2/3] selftests/bpf: libarena: Cache the rbtree's leftmost node and count Changwoo Min
2026-11-01  1:03   ` Alexei Starovoitov
2026-11-03 13:14     ` Changwoo Min [this message]
2026-11-02  8:05   ` Emil Tsalapatis
2026-11-03 13:14     ` Changwoo Min
2026-10-28  6:00 ` [PATCH bpf-next 3/3] selftests/bpf: libarena: Add rb_next() Changwoo Min
2026-11-02  8:07   ` Emil Tsalapatis
2026-11-03 13:15     ` Changwoo Min

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=bcf2e1c2-bc97-4d8e-a690-a1836a371033@igalia.com \
    --to=changwoo@igalia.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=david.dai@linux.dev \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=gavinguo@igalia.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=jolsa@kernel.org \
    --cc=kernel-dev@igalia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=sched-ext@lists.linux.dev \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /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®