Netdev List
 help / color / mirror / Atom feed
From: Mahe Tardy <mahe.tardy@gmail.com>
To: Jiayuan Chen <jiayuan.chen@linux.dev>
Cc: bpf@vger.kernel.org, andrew+netdev@lunn.ch, andrii@kernel.org,
	ast@kernel.org, daniel@iogearbox.net, davem@davemloft.net,
	eddyz87@gmail.com, edumazet@google.com, john.fastabend@gmail.com,
	kuba@kernel.org, liamwisehart@meta.com, martin.lau@linux.dev,
	pabeni@redhat.com, song@kernel.org, netdev@vger.kernel.org,
	sdf.kernel@gmail.com, ameryhung@gmail.com, kuniyu@google.com,
	memxor@gmail.com
Subject: Re: [PATCH bpf-next v4 3/5] selftests/bpf: Add ksock kfunc test
Date: Fri, 7 Aug 2026 12:11:07 +0200	[thread overview]
Message-ID: <anWvO7ZQh1eEHZie@gmail.com> (raw)
In-Reply-To: <c51bef73-17b0-430d-b473-7744141d6dab@linux.dev>

On Fri, Aug 07, 2026 at 05:12:21PM +0800, Jiayuan Chen wrote:
> 
> On 8/7/26 2:22 AM, Mahe Tardy wrote:
> > Add
> [...]
> > +
> > +SEC("lsm.s/socket_bind")
> > +int BPF_PROG(ksock_socket_bind, struct socket *sock, struct sockaddr *address,
> > +	     int addrlen, int ret)
> > +{
> > +	struct __ksock_ctx_value *v;
> > +	struct bpf_ksock *ks, *tmp;
> > +	u32 pid = bpf_get_current_pid_tgid() >> 32;
> > +
> > +	if (ret || pid != target_pid)
> > +		return ret;
> > +
> > +	v = ksock_ctx_value_lookup();
> > +	if (!v) {
> > +		send_ret = -ENOENT;
> > +		return ret;
> > +	}
> > +
> > +	ks = NULL;
> > +	bpf_rcu_read_lock();
> > +	tmp = v->ctx;
> > +	if (tmp)
> > +		ks = bpf_ksock_acquire(tmp);
> > +	bpf_rcu_read_unlock();
> > +
> > +	if (!ks) {
> > +		send_ret = -ENOENT;
> > +		return ret;
> > +	}
> > +
> 
> 
> Since bpf_ksock_send() is sleepable, every consumer of the kptr is
> necessarily a sleepable program and thus has to open-code the same
> bpf_rcu_read_lock()/acquire/unlock sequence before each send.
> 
> Given ksock_common.h already wraps the insert side, how about also
> providing the get side, e.g.:
> 
> static inline struct bpf_ksock *ksock_ctx_get(void)
> {
>     struct __ksock_ctx_value *v = ksock_ctx_value_lookup();
>     struct bpf_ksock *ks = NULL, *tmp;
> 
>     if (!v)
>             return NULL;
>     bpf_rcu_read_lock();
>     tmp = v->ctx;
>     if (tmp)
>             ks = bpf_ksock_acquire(tmp);
>     bpf_rcu_read_unlock();
>     return ks;
> }
> 
> These selftests will be the reference everyone copies from, so
> encapsulating the RCU dance here also documents the correct usage.
> 

Yeah good idea, I'll just keep a slightly difference dance using
bpf_kptr_xchg() in the ksock_lsm_verifier.c program because I want the
verifier to bump against bpf_ksock_send() restriction explicitely and
not bpf_ksock_acquire().


  reply	other threads:[~2026-08-07 10:11 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-06 18:22 [PATCH bpf-next v4 0/5] Introduce bpf_ksock Mahe Tardy
2026-08-06 18:22 ` [PATCH bpf-next v4 1/5] net: Add connect_socket() helper Mahe Tardy
2026-08-06 20:09   ` Song Liu
2026-08-07  1:53   ` Jiayuan Chen
2026-08-07  9:51     ` Mahe Tardy
2026-08-06 18:22 ` [PATCH bpf-next v4 2/5] bpf: Add ksock kfuncs Mahe Tardy
2026-08-07  3:08   ` Jiayuan Chen
2026-08-07  9:56     ` Mahe Tardy
2026-08-06 18:22 ` [PATCH bpf-next v4 3/5] selftests/bpf: Add ksock kfunc test Mahe Tardy
2026-08-07  9:12   ` Jiayuan Chen
2026-08-07 10:11     ` Mahe Tardy [this message]
2026-08-06 18:22 ` [PATCH bpf-next v4 4/5] selftests/bpf: Test forbidden bpf_ksock_send() LSM attach Mahe Tardy
2026-08-06 18:22 ` [PATCH bpf-next v4 5/5] selftests/bpf: Add ksock test for async callback guard Mahe Tardy

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=anWvO7ZQh1eEHZie@gmail.com \
    --to=mahe.tardy@gmail.com \
    --cc=ameryhung@gmail.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=eddyz87@gmail.com \
    --cc=edumazet@google.com \
    --cc=jiayuan.chen@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=liamwisehart@meta.com \
    --cc=martin.lau@linux.dev \
    --cc=memxor@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sdf.kernel@gmail.com \
    --cc=song@kernel.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