BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Xu Kuohai" <xukuohai@huaweicloud.com>, bpf@vger.kernel.org
Subject: Re: [RFC PATCH 1/3] bpf: Introduce classic BPF filter cache
Date: Fri, 20 Nov 2026 07:31:34 +0000	[thread overview]
Message-ID: <sashiko-outbox-224630@kernel.org> (raw)
In-Reply-To: <adf466cb2cad6c674b7da5efbebfc2c3e3e8800c.1795139109.git.xukuohai@huaweicloud.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] bpf: write to read-only bpf_prog in cbpf_cache_complete()
- [Low] bpf: unsnapshotted bpf_jit_enable read in cbpf_cache_tag()
--

commit 150cca79ed8acc9b4ba7fe54a3257e050436741a
Author: Xu Kuohai <xukuohai@huawei.com>

bpf: Introduce classic BPF filter cache

This commit introduces a hash table cache for prepared classic BPF filters
so identical user-space filter programs share the same prepared bpf_prog.

> diff --git a/net/core/filter.c b/net/core/filter.c
> index 7fa8b81fd1dcb..f8ef6a041e8fd 100644
> --- a/net/core/filter.c
> +++ b/net/core/filter.c
[ ... ]
> @@ -1187,6 +1193,282 @@ static void bpf_release_orig_filter(struct bpf_prog *fp)
>  	}
>  }
>  
[ ... ]
> +/*
> + * calculate tag from the allocation-time JIT and blinding states, so sysctl
> + * changes produce a different tag.
> + */
> +static u32 cbpf_cache_tag(const struct bpf_prog *prog)
> +{
> +	u32 tag = 0;
> +
> +	tag |= (prog->jit_requested << 1);
> +	tag |= (prog->blinding_requested << 2);
> +#ifdef CONFIG_BPF_JIT
> +	tag |= (bpf_jit_enable << 3);
> +#endif
> +
> +	return tag;
> +}
> +
> +static u32 cbpf_cache_bucket(const struct bpf_prog *prog)
> +{
> +	const struct sock_fprog_kern *orig = prog->orig_prog;
> +
> +	return jhash(orig->filter, orig->len * sizeof(struct sock_filter),
> +		     cbpf_cache_tag(prog)) % CBPF_CACHE_SIZE;
> +}
[ ... ]
> +static int cbpf_cache_insert(struct cbpf_cache *cache, struct bpf_prog *prog,
> +			     struct cbpf_cache_entry **pentry)
> +{
> +	struct cbpf_cache_entry *entry, *hit, *cur;
> +	u32 bucket = cbpf_cache_bucket(prog);
> +
> +	entry = kzalloc_obj(*entry, GFP_KERNEL | __GFP_NOWARN);
> +	if (!entry)
> +		return -ENOMEM;
> +
> +	entry->orig = prog->orig_prog;
> +	entry->cache = cache;
> +	entry->tag = cbpf_cache_tag(prog);
> +	entry->bucket = bucket;

[Severity: Low]
Can entry->bucket and entry->tag disagree if bpf_jit_enable changes via
sysctl while cbpf_cache_insert() sleeps in kzalloc_obj()?

Unlike prog->jit_requested and prog->blinding_requested, bpf_jit_enable is
read live from the global variable on each cbpf_cache_tag() call rather
than from an allocation-time snapshot.  If bpf_jit_enable changes between
cbpf_cache_bucket() and cbpf_cache_tag(), the entry is inserted into the
bucket hashed with the old tag while storing the new tag in entry->tag, so
future lookups in either bucket will not match it.

[ ... ]

> +/* publish the prepared result, and wake up waiters */
> +static void cbpf_cache_complete(struct cbpf_cache_entry *entry,
> +				struct bpf_prog *fp)
> +{
> +	fp->orig_prog = entry->orig;
> +	fp->orig_prog->cache_entry = entry;

[Severity: High]
Does assigning fp->orig_prog here fault on read-only memory when the filter
is not JIT-compiled on a !CONFIG_BPF_JIT_ALWAYS_ON kernel?

In bpf_prog_create_from_user(), cbpf_cache_insert() clears prog->orig_prog
before calling bpf_prepare_filter(), and cbpf_cache_complete() restores
fp->orig_prog afterwards.  However, bpf_prepare_filter() ->
bpf_migrate_filter() -> bpf_prog_select_runtime() ->
__bpf_prog_select_runtime() calls bpf_prog_lock_ro(fp), which marks all
pages of struct bpf_prog read-only via set_memory_ro() when !fp->jited and
!CONFIG_BPF_JIT_ALWAYS_ON.  Writing to fp->orig_prog in
cbpf_cache_complete() then triggers a supervisor write page fault.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1795139109.git.xukuohai@huaweicloud.com?part=1

  reply	other threads:[~2026-11-20  7:31 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20  6:48 [RFC PATCH 0/3] Introduce a classic BPF filter cache for seccomp Xu Kuohai
2026-11-20  6:48 ` [RFC PATCH 1/3] bpf: Introduce classic BPF filter cache Xu Kuohai
2026-11-20  7:31   ` sashiko-bot [this message]
2026-11-20  6:48 ` [RFC PATCH 2/3] seccomp: Use the " Xu Kuohai
2026-11-20  6:48 ` [RFC PATCH 3/3] selftests/bpf: Add benchmark for seccomp filter install Xu Kuohai
2026-11-20  7:24   ` 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-224630@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=xukuohai@huaweicloud.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