public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Andrea Righi <arighi@nvidia.com>
To: zhidao su <soolaugust@gmail.com>
Cc: tj@kernel.org, void@manifault.com, changwoo@igalia.com,
	sched-ext@lists.linux.dev, linux-kernel@vger.kernel.org,
	suzhidao@xiaomi.com, Emil Tsalapatis <emil@etsalapatis.com>
Subject: Re: [PATCH] tools/sched_ext: Improve BPF verifier arena detection workaround
Date: Fri, 6 Feb 2026 08:03:37 +0100	[thread overview]
Message-ID: <aYWSSVDwOYEruRb2@gpd4> (raw)
In-Reply-To: <20260206041808.170926-1-suzhidao@xiaomi.com>

Hi,

On Fri, Feb 06, 2026 at 12:18:08PM +0800, zhidao su wrote:
> Replace bpf_printk() with volatile access in scx_sdt scheduler's
> BPF verifier workaround to eliminate console output while maintaining
> the required LD.IMM instruction generation for arena detection.
> 
> This addresses the side effect issue of the previous hack while
> preserving the essential functionality needed by the BPF verifier.
> 
> Signed-off-by: zhidao su <suzhidao@xiaomi.com>

Adding Emil in cc.

> ---
>  tools/sched_ext/scx_sdt.bpf.c | 18 +++++++++---------
>  1 file changed, 9 insertions(+), 9 deletions(-)
> 
> diff --git a/tools/sched_ext/scx_sdt.bpf.c b/tools/sched_ext/scx_sdt.bpf.c
> index 31b09958e8d5..13d3060c99ff 100644
> --- a/tools/sched_ext/scx_sdt.bpf.c
> +++ b/tools/sched_ext/scx_sdt.bpf.c
> @@ -64,14 +64,10 @@ DEFINE_SDT_STAT(select_busy_cpu);
>  static __u64 zero = 0;
>  
>  /*
> - * XXX Hack to get the verifier to find the arena for sdt_exit_task.
> - * As of 6.12-rc5, The verifier associates arenas with programs by
> - * checking LD.IMM instruction operands for an arena and populating
> - * the program state with the first instance it finds. This requires
> - * accessing our global arena variable, but scx methods do not necessarily
> - * do so while still using pointers from that arena. Insert a bpf_printk
> - * statement that triggers at most once to generate an LD.IMM instruction
> - * to access the arena and help the verifier.
> + * Workaround to help BPF verifier track arena usage.
> + * The verifier needs to see an explicit reference to the arena variable
> + * to properly track arena memory usage. This generates the required
> + * LD.IMM instruction without producing unnecessary output.
>   */
>  static volatile bool scx_arena_verify_once;
>  
> @@ -80,7 +76,11 @@ __hidden void scx_arena_subprog_init(void)
>  	if (scx_arena_verify_once)
>  		return;
>  
> -	bpf_printk("%s: arena pointer %p", __func__, &arena);
> +	/*
> +	 * Use volatile access to generate LD.IMM instruction without
> +	 * producing console output like bpf_printk does.
> +	 */
> +	(void)*(volatile void **)&arena;

Makes sense to me.

If we want to be extra picky we can do something like this:

  volatile void *arena_ref = &arena;
  (void)arena_ref;

In this way we take the address of the arena map descriptor and store it
in a volatile void * variable, so the compiler can't optimize away and we
never read the content of the map descriptor, we only use its address. So,
we're not reinterpreting the bytes of the struct as another type (no type
punning, no strict-aliasing violation). Even if it's probably just a
theoretical thing.

>  	scx_arena_verify_once = true;
>  }
>  
> -- 
> 2.43.0
> 

Thanks,
-Andrea

  reply	other threads:[~2026-02-06  7:03 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-06  4:18 [PATCH] tools/sched_ext: Improve BPF verifier arena detection workaround zhidao su
2026-02-06  7:03 ` Andrea Righi [this message]
2026-02-06 16:03   ` Emil Tsalapatis
2026-02-12  8:00     ` [PATCH v2] " zhidao su
  -- strict thread matches above, loose matches on Subject: below --
2026-02-03  3:19 [PATCH] " zhidao su
2026-02-03  3:54 ` Emil Tsalapatis
2026-02-03  6:44   ` zhidao su
2026-02-03  6:57     ` zhidao su

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=aYWSSVDwOYEruRb2@gpd4 \
    --to=arighi@nvidia.com \
    --cc=changwoo@igalia.com \
    --cc=emil@etsalapatis.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sched-ext@lists.linux.dev \
    --cc=soolaugust@gmail.com \
    --cc=suzhidao@xiaomi.com \
    --cc=tj@kernel.org \
    --cc=void@manifault.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