Re: [PATCH bpf-next v4 3/5] selftests/bpf: Add ksock kfunc test

Jiayuan Chen <[email protected]>
Newsgroups org.kernel.vger.bpf,org.kernel.vger.netdev
Message-ID <[email protected]>
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.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.