From: Mahe Tardy <mahe.tardy@gmail.com>
To: Kuniyuki Iwashima <kuniyu@google.com>
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, memxor@gmail.com,
jiayuan.chen@linux.dev
Subject: Re: [PATCH bpf-next v5 2/5] bpf: Add ksock kfuncs
Date: Wed, 12 Aug 2026 12:54:56 +0200 [thread overview]
Message-ID: <anxRAIXU5lLOIo7O@gmail.com> (raw)
In-Reply-To: <CAAVpQUDeiDOQa6i03juWO64vyz0TnFH6mqB_vNo0ZeqA0-otxw@mail.gmail.com>
On Mon, Aug 10, 2026 at 09:24:38PM -0700, Kuniyuki Iwashima wrote:
> On Fri, Aug 7, 2026 at 10:15 AM Mahe Tardy <mahe.tardy@gmail.com> wrote:
> >
> > Add BPF kfuncs that allow BPF LSM programs to create and use sockets for
> > sending data. This provides a mechanism for BPF programs to emit
> > telemetry. For this first patch set, it's restricted to SOCK_DGRAM
> > socket types with IPPROTO_UDP protocol but could be easily extended to
> > SOCK_STREAM and IPPROTO_TCP in the future.
> >
> > The API consists of five kfuncs:
> >
> > bpf_ksock_create() - Create a socket (sleepable)
> > bpf_ksock_connect() - Connect socket to remote address (sleepable)
> > bpf_ksock_send() - Send data through the socket (sleepable)
> > bpf_ksock_acquire() - Acquire a reference to a socket context
> > bpf_ksock_release() - Release a reference (cleanup via
> > queue_rcu_work since sock_release sleeps)
> >
[...]
> > +
> > +/**
> > + * struct bpf_ksock - refcounted BPF kernel socket context
> > + * @sock: The underlying kernel socket.
> > + * @usage: Reference counter.
> > + * @rwork: RCU work for deferred cleanup (sock_release may sleep).
> > + */
> > +struct bpf_ksock {
> > + struct socket *sock;
> > + refcount_t usage;
> > + struct rcu_work rwork;
> > +};
> > +
> > +static void ksock_release_work_fn(struct work_struct *work)
> > +{
> > + struct bpf_ksock *ks =
> > + container_of(to_rcu_work(work), struct bpf_ksock, rwork);
>
> nit: if you need respin:
>
> struct bpf_ksock *ks;
>
> ks = container_of(to_rcu_work(work), struct bpf_ksock, rwork);
sure!
>
> > +
> > + sock_release(ks->sock);
> > + kfree(ks);
> > +}
> > +
[...]
> > +__bpf_kfunc struct bpf_ksock *
> > +bpf_ksock_create(const struct bpf_ksock_create_opts *opts, u32 opts__sz,
> > + int *err__uninit)
> > +{
> > + struct bpf_ksock_create_opts opts_copy;
> > + struct bpf_ksock *ks;
> > + int err;
> > +
> > + /*
> > + * sock_create() derives the network namespace, credentials, and cgroup
> > + * from current. Kernel threads, including BPF workqueue callbacks, do
> > + * not carry the context of the task that invoked the BPF program.
> > + */
> > + if (!bpf_ksock_has_user_task_context()) {
> > + err = -EOPNOTSUPP;
> > + goto err_out;
> > + }
> > +
> > + if (!opts || opts__sz != sizeof(struct bpf_ksock_create_opts)) {
> > + err = -EINVAL;
> > + goto err_out;
> > + }
> > +
> > + opts_copy = (struct bpf_ksock_create_opts){
> > + .family = READ_ONCE(opts->family),
>
> I'm not sure if we really want to care about such an arch, but
> since you mentioned unaligned access in bpf_ksock_connect(),
> is READ_OCNE() safe here ?
To my understanding, there's no issue regarding alignement here since
these four fields are __u8. The READ_ONCE is only used so that the
compiler does not do smart things like moving load or reloading a field
between time of check and time of use.
>
>
> > + .type = READ_ONCE(opts->type),
> > + .protocol = READ_ONCE(opts->protocol),
> > + .reserved = READ_ONCE(opts->reserved),
> > + };
> > +
[...]
next prev parent reply other threads:[~2026-08-12 10:55 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-07 17:15 [PATCH bpf-next v5 0/5] Introduce bpf_ksock Mahe Tardy
2026-08-07 17:15 ` [PATCH bpf-next v5 1/5] net: Add connect_socket() helper Mahe Tardy
2026-08-07 18:11 ` bot+bpf-ci
2026-08-12 10:24 ` Mahe Tardy
2026-08-08 12:46 ` Jiayuan Chen
2026-08-10 18:33 ` Stanislav Fomichev
2026-08-11 4:01 ` Kuniyuki Iwashima
2026-08-12 10:25 ` Mahe Tardy
2026-08-07 17:15 ` [PATCH bpf-next v5 2/5] bpf: Add ksock kfuncs Mahe Tardy
2026-08-08 12:46 ` Jiayuan Chen
2026-08-10 18:35 ` Stanislav Fomichev
2026-08-11 4:24 ` Kuniyuki Iwashima
2026-08-12 10:54 ` Mahe Tardy [this message]
2026-08-07 17:15 ` [PATCH bpf-next v5 3/5] selftests/bpf: Add ksock kfunc test Mahe Tardy
2026-08-08 12:47 ` Jiayuan Chen
2026-08-10 18:35 ` Stanislav Fomichev
2026-08-07 17:15 ` [PATCH bpf-next v5 4/5] selftests/bpf: Test forbidden bpf_ksock_send() LSM attach Mahe Tardy
2026-08-07 18:41 ` bot+bpf-ci
2026-08-10 18:35 ` Stanislav Fomichev
2026-08-07 17:15 ` [PATCH bpf-next v5 5/5] selftests/bpf: Add ksock test for async callback guard Mahe Tardy
2026-08-10 18:35 ` Stanislav Fomichev
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=anxRAIXU5lLOIo7O@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