From: Kui-Feng Lee <sinquersw@gmail.com>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>,
Kui-Feng Lee <thinker.li@gmail.com>
Cc: bpf@vger.kernel.org, ast@kernel.org, martin.lau@linux.dev,
song@kernel.org, kernel-team@meta.com, andrii@kernel.org,
kuifeng@meta.com
Subject: Re: [PATCH bpf-next 1/6] bpf: add a pointer of the attached link to bpf_struct_ops_map.
Date: Wed, 1 May 2024 15:15:47 -0700 [thread overview]
Message-ID: <42d93c08-1f43-49f4-81d9-076f4e708b97@gmail.com> (raw)
In-Reply-To: <CAEf4Bza3YmsxD7yrK2+TJx=EWyobmgps5ySLmzU7QVQHhUigpQ@mail.gmail.com>
On 5/1/24 10:01, Andrii Nakryiko wrote:
> On Mon, Apr 29, 2024 at 2:36 PM Kui-Feng Lee <thinker.li@gmail.com> wrote:
>>
>> To facilitate the upcoming unregistring struct_ops objects from the systems
>> consuming objects, a pointer of the attached link is added to allow for
>> accessing the attached link of a bpf_struct_ops_map directly from the map
>> itself.
>>
>> Previously, a st_map could be attached to multiple links. This patch now
>> enforces only one link attached at most.
>
> I'd like to avoid this restriction, in principle. We don't enforce
> that BPF program should be attached through a single BPF link, so I
> don't think we should allow that for maps. Worst case you can keep a
> list of attached links.
Agree!
>
>>
>> Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
>> ---
>> kernel/bpf/bpf_struct_ops.c | 47 ++++++++++++++++++++++++++++++++++---
>> 1 file changed, 44 insertions(+), 3 deletions(-)
>>
>> diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c
>> index 86c7884abaf8..072e3416c987 100644
>> --- a/kernel/bpf/bpf_struct_ops.c
>> +++ b/kernel/bpf/bpf_struct_ops.c
>> @@ -20,6 +20,8 @@ struct bpf_struct_ops_value {
>>
>> #define MAX_TRAMP_IMAGE_PAGES 8
>>
>> +struct bpf_struct_ops_link;
>> +
>> struct bpf_struct_ops_map {
>> struct bpf_map map;
>> struct rcu_head rcu;
>> @@ -39,6 +41,8 @@ struct bpf_struct_ops_map {
>> void *image_pages[MAX_TRAMP_IMAGE_PAGES];
>> /* The owner moduler's btf. */
>> struct btf *btf;
>> + /* The link is attached by this map. */
>> + struct bpf_struct_ops_link __rcu *attached;
>> /* uvalue->data stores the kernel struct
>> * (e.g. tcp_congestion_ops) that is more useful
>> * to userspace than the kvalue. For example,
>> @@ -1048,6 +1052,22 @@ static bool bpf_struct_ops_valid_to_reg(struct bpf_map *map)
>> smp_load_acquire(&st_map->kvalue.common.state) == BPF_STRUCT_OPS_STATE_READY;
>> }
>>
>> +/* Set the attached link of a map.
>> + *
>> + * Return the current value of the st_map->attached.
>> + */
>> +static inline struct bpf_struct_ops_link *map_attached(struct bpf_struct_ops_map *st_map,
>> + struct bpf_struct_ops_link *st_link)
>> +{
>> + return unrcu_pointer(cmpxchg(&st_map->attached, NULL, st_link));
>> +}
>> +
>> +/* Reset the attached link of a map */
>> +static inline void map_attached_null(struct bpf_struct_ops_map *st_map)
>> +{
>> + rcu_assign_pointer(st_map->attached, NULL);
>> +}
>> +
>> static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link)
>> {
>> struct bpf_struct_ops_link *st_link;
>> @@ -1061,6 +1081,7 @@ static void bpf_struct_ops_map_link_dealloc(struct bpf_link *link)
>> * bpf_struct_ops_link_create() fails to register.
>> */
>> st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data);
>> + map_attached_null(st_map);
>> bpf_map_put(&st_map->map);
>> }
>> kfree(st_link);
>> @@ -1125,9 +1146,21 @@ static int bpf_struct_ops_map_link_update(struct bpf_link *link, struct bpf_map
>> goto err_out;
>> }
>>
>> + if (likely(st_map != old_st_map) && map_attached(st_map, st_link)) {
>> + /* The map is already in use */
>> + err = -EBUSY;
>> + goto err_out;
>> + }
>> +
>> err = st_map->st_ops_desc->st_ops->update(st_map->kvalue.data, old_st_map->kvalue.data);
>> - if (err)
>> + if (err) {
>> + if (st_map != old_st_map)
>> + map_attached_null(st_map);
>> goto err_out;
>> + }
>> +
>> + if (likely(st_map != old_st_map))
>> + map_attached_null(old_st_map);
>>
>> bpf_map_inc(new_map);
>> rcu_assign_pointer(st_link->map, new_map);
>> @@ -1172,20 +1205,28 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)
>> }
>> bpf_link_init(&link->link, BPF_LINK_TYPE_STRUCT_OPS, &bpf_struct_ops_map_lops, NULL);
>>
>> + if (map_attached(st_map, link)) {
>> + err = -EBUSY;
>> + goto err_out;
>> + }
>> +
>> err = bpf_link_prime(&link->link, &link_primer);
>> if (err)
>> - goto err_out;
>> + goto err_out_attached;
>>
>> err = st_map->st_ops_desc->st_ops->reg(st_map->kvalue.data);
>> if (err) {
>> bpf_link_cleanup(&link_primer);
>> + /* The link has been free by bpf_link_cleanup() */
>> link = NULL;
>> - goto err_out;
>> + goto err_out_attached;
>> }
>> RCU_INIT_POINTER(link->map, map);
>>
>> return bpf_link_settle(&link_primer);
>>
>> +err_out_attached:
>> + map_attached_null(st_map);
>> err_out:
>> bpf_map_put(map);
>> kfree(link);
>> --
>> 2.34.1
>>
next prev parent reply other threads:[~2024-05-01 22:15 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-29 21:36 [PATCH bpf-next 0/6] Notify user space when a struct_ops object is detached/unregisterd Kui-Feng Lee
2024-04-29 21:36 ` [PATCH bpf-next 1/6] bpf: add a pointer of the attached link to bpf_struct_ops_map Kui-Feng Lee
2024-05-01 17:01 ` Andrii Nakryiko
2024-05-01 22:15 ` Kui-Feng Lee [this message]
2024-04-29 21:36 ` [PATCH bpf-next 2/6] bpf: export bpf_link_inc_not_zero() Kui-Feng Lee
2024-04-29 21:36 ` [PATCH bpf-next 3/6] bpf: provide a function to unregister struct_ops objects from consumers Kui-Feng Lee
2024-05-01 18:48 ` Martin KaFai Lau
2024-05-01 22:15 ` Kui-Feng Lee
2024-05-01 23:06 ` Martin KaFai Lau
2024-05-02 17:56 ` Martin KaFai Lau
2024-05-02 18:29 ` Martin KaFai Lau
2024-05-03 0:41 ` Kui-Feng Lee
2024-05-03 16:19 ` Alexei Starovoitov
2024-05-03 18:09 ` Kui-Feng Lee
2024-05-03 17:17 ` Martin KaFai Lau
2024-04-29 21:36 ` [PATCH bpf-next 4/6] bpf: detach a bpf_struct_ops_map from a link Kui-Feng Lee
2024-04-29 21:36 ` [PATCH bpf-next 5/6] bpf: support epoll from bpf struct_ops links Kui-Feng Lee
2024-05-01 17:03 ` Andrii Nakryiko
2024-05-01 22:16 ` Kui-Feng Lee
2024-04-29 21:36 ` [PATCH bpf-next 6/6] selftests/bpf: test detaching " Kui-Feng Lee
2024-05-01 17:05 ` Andrii Nakryiko
2024-05-01 22:17 ` Kui-Feng Lee
2024-05-02 18:15 ` Martin KaFai Lau
2024-05-03 18:34 ` Kui-Feng Lee
2024-05-03 19:15 ` Martin KaFai Lau
2024-05-03 21:34 ` Kui-Feng Lee
2024-05-03 21:59 ` Martin KaFai Lau
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=42d93c08-1f43-49f4-81d9-076f4e708b97@gmail.com \
--to=sinquersw@gmail.com \
--cc=andrii.nakryiko@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=kernel-team@meta.com \
--cc=kuifeng@meta.com \
--cc=martin.lau@linux.dev \
--cc=song@kernel.org \
--cc=thinker.li@gmail.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;
as well as URLs for NNTP newsgroup(s).