BPF List
 help / color / mirror / Atom feed
From: John Fastabend <john.fastabend@gmail.com>
To: Yonghong Song <yonghong.song@linux.dev>,
	 John Fastabend <john.fastabend@gmail.com>,
	 bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	 Andrii Nakryiko <andrii@kernel.org>,
	 Daniel Borkmann <daniel@iogearbox.net>,
	 Jakub Sitnicki <jakub@cloudflare.com>,
	 kernel-team@fb.com,  Martin KaFai Lau <martin.lau@kernel.org>
Subject: Re: [PATCH bpf-next v4 1/5] bpf: Add bpf_link support for sk_msg and sk_skb progs
Date: Fri, 05 Apr 2024 09:23:40 -0700	[thread overview]
Message-ID: <6610258cb977b_5a9cb20838@john.notmuch> (raw)
In-Reply-To: <c9db2abb-2391-415b-ba71-db80dc42cc54@linux.dev>

Yonghong Song wrote:
> 
> On 4/5/24 8:19 AM, John Fastabend wrote:
> > Yonghong Song wrote:
> >> Add bpf_link support for sk_msg and sk_skb programs. We have an
> >> internal request to support bpf_link for sk_msg programs so user
> >> space can have a uniform handling with bpf_link based libbpf
> >> APIs. Using bpf_link based libbpf API also has a benefit which
> >> makes system robust by decoupling prog life cycle and
> >> attachment life cycle.
> >>
> >> Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
> >> ---
> >>   include/linux/bpf.h            |   6 +
> >>   include/linux/skmsg.h          |   4 +
> >>   include/uapi/linux/bpf.h       |   5 +
> >>   kernel/bpf/syscall.c           |   4 +
> >>   net/core/sock_map.c            | 268 ++++++++++++++++++++++++++++++++-
> >>   tools/include/uapi/linux/bpf.h |   5 +
> >>   6 files changed, 284 insertions(+), 8 deletions(-)
> >>
> > LGTM one question below.
> >
> >> +/* Handle the following two cases:
> >> + * case 1: link != NULL, prog != NULL, old != NULL
> >> + * case 2: link != NULL, prog != NULL, old == NULL
> >> + */
> >> +static int sock_map_link_update_prog(struct bpf_link *link,
> >> +				     struct bpf_prog *prog,
> >> +				     struct bpf_prog *old)
> >> +{
> >> +	const struct sockmap_link *sockmap_link = container_of(link, struct sockmap_link, link);
> >> +	struct bpf_prog **pprog;
> >> +	struct bpf_link **plink;
> >> +	int ret = 0;
> >> +
> >> +	mutex_lock(&sockmap_mutex);
> >> +
> >> +	/* If old prog is not NULL, ensure old prog is the same as link->prog. */
> >> +	if (old && link->prog != old) {
> >> +		ret = -EINVAL;
> >> +		goto out;
> >> +	}
> >> +	/* Ensure link->prog has the same type/attach_type as the new prog. */
> >> +	if (link->prog->type != prog->type ||
> >> +	    link->prog->expected_attach_type != prog->expected_attach_type) {
> >> +		ret = -EINVAL;
> >> +		goto out;
> >> +	}
> >> +
> >> +	ret = sock_map_prog_lookup(sockmap_link->map, &pprog,
> >> +				   sockmap_link->attach_type);
> >> +	if (ret)
> >> +		goto out;
> >> +
> >> +	/* Ensure the same link between the one in map and the passed-in. */
> >> +	ret = sock_map_link_lookup(sockmap_link->map, &plink, link, false,
> >> +				   sockmap_link->attach_type);
> >> +	if (ret)
> >> +		goto out;
> >> +
> >> +	if (old) {
> >> +		ret = psock_replace_prog(pprog, prog, old);
> >> +		goto out;
> >> +	}
> >> +
> >> +	psock_set_prog(pprog, prog);
> >> +
> >> +out:
> >> +	if (!ret) {
> >> +		bpf_prog_inc(prog);
> >> +		old = xchg(&link->prog, prog);
> >> +		bpf_prog_put(old);
> > Need to check old? I don't think we can clal bpf_prog_put on null?
> >
> >    if (old)
> >       bpf_prog_put(old)
> 
> The 'old' here represents the *old* link->prog program and
> link->prog should not be NULL at this point.

Ah ok. Maybe instead of using the input old var make it
explicit?

    if (!ret) {
       struct bpf_prog *old_link;

       bpf_prog_inc(prog);
       old_link = xchg(&link->prog, prog);
       bpf_prog_put(old)
    }

Is a bit more obious to me at least. Up to you I have a slight preference
for the explicit more verbose above.

Otherwise for the series.

Reviewed-by: John Fastabend <john.fastabend@gmail.com>

  reply	other threads:[~2024-04-05 16:23 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-04  2:53 [PATCH bpf-next v4 0/5] bpf: Add bpf_link support for sk_msg and sk_skb progs Yonghong Song
2024-04-04  2:53 ` [PATCH bpf-next v4 1/5] " Yonghong Song
2024-04-05 15:19   ` John Fastabend
2024-04-05 15:53     ` Yonghong Song
2024-04-05 16:23       ` John Fastabend [this message]
2024-04-05 16:51         ` Yonghong Song
2024-04-05 19:43           ` John Fastabend
2024-04-05 20:05             ` Yonghong Song
2024-04-05 20:12   ` Andrii Nakryiko
2024-04-06  5:21     ` Yonghong Song
2024-04-04  2:53 ` [PATCH bpf-next v4 2/5] libbpf: Add bpf_link support for BPF_PROG_TYPE_SOCKMAP Yonghong Song
2024-04-05 15:20   ` John Fastabend
2024-04-05 20:14   ` Andrii Nakryiko
2024-04-06  5:19     ` Yonghong Song
2024-04-04  2:53 ` [PATCH bpf-next v4 3/5] bpftool: Add link dump support for BPF_LINK_TYPE_SOCKMAP Yonghong Song
2024-04-05 15:20   ` John Fastabend
2024-04-04  2:53 ` [PATCH bpf-next v4 4/5] selftests/bpf: Refactor out helper functions for a few tests Yonghong Song
2024-04-04  2:53 ` [PATCH bpf-next v4 5/5] selftests/bpf: Add some tests with new bpf_program__attach_sockmap() APIs Yonghong Song

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=6610258cb977b_5a9cb20838@john.notmuch \
    --to=john.fastabend@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=jakub@cloudflare.com \
    --cc=kernel-team@fb.com \
    --cc=martin.lau@kernel.org \
    --cc=yonghong.song@linux.dev \
    /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