From: Martin KaFai Lau <martin.lau@linux.dev>
To: Kui-Feng Lee <sinquersw@gmail.com>, Kui-Feng Lee <thinker.li@gmail.com>
Cc: kuifeng@meta.com, bpf@vger.kernel.org, ast@kernel.org,
song@kernel.org, kernel-team@meta.com, andrii@kernel.org
Subject: Re: [PATCH bpf-next v3 2/7] bpf: enable detaching links of struct_ops objects.
Date: Tue, 21 May 2024 11:09:31 -0700 [thread overview]
Message-ID: <ebd98bb8-a3c5-4c03-8812-2f64a36dd737@linux.dev> (raw)
In-Reply-To: <58dcc859-45c6-4493-8760-61e469ba2e69@gmail.com>
On 5/21/24 12:30 AM, Kui-Feng Lee wrote:
>
>
> On 5/20/24 18:22, Martin KaFai Lau wrote:
>> On 5/9/24 5:29 PM, Kui-Feng Lee wrote:
>>> +static int bpf_struct_ops_map_link_detach(struct bpf_link *link)
>>> +{
>>> + struct bpf_struct_ops_link *st_link = container_of(link, struct
>>> bpf_struct_ops_link, link);
>>> + struct bpf_struct_ops_map *st_map;
>>> + struct bpf_map *map;
>>> +
>>> + mutex_lock(&update_mutex);
>>> +
>>> + map = rcu_dereference_protected(st_link->map,
>>> lockdep_is_held(&update_mutex));
>>> + if (!map) {
>>> + mutex_unlock(&update_mutex);
>>> + return -EINVAL;
>>> + }
>>> + st_map = container_of(map, struct bpf_struct_ops_map, map);
>>> +
>>> + st_map->st_ops_desc->st_ops->unreg(&st_map->kvalue.data, link);
>>> +
>>> + rcu_assign_pointer(st_link->map, NULL);
>>> + /* Pair with bpf_map_get() in bpf_struct_ops_link_create() or
>>> + * bpf_map_inc() in bpf_struct_ops_map_link_update().
>>> + */
>>> + bpf_map_put(&st_map->map);
>>> +
>>> + mutex_unlock(&update_mutex);
>>> +
>>> + return 0;
>>> +}
>>> +
>>> static const struct bpf_link_ops bpf_struct_ops_map_lops = {
>>> .dealloc = bpf_struct_ops_map_link_dealloc,
>>> + .detach = bpf_struct_ops_map_link_detach,
>>> .show_fdinfo = bpf_struct_ops_map_link_show_fdinfo,
>>> .fill_link_info = bpf_struct_ops_map_link_fill_link_info,
>>> .update_map = bpf_struct_ops_map_link_update,
>>> @@ -1176,13 +1208,19 @@ int bpf_struct_ops_link_create(union bpf_attr *attr)
>>> if (err)
>>> goto err_out;
>>> + /* Init link->map before calling reg() in case being detached
>>> + * immediately.
>>> + */
>>
>> It is not obvious in the patch how this (immediate detach by subsystem after
>> reg) may work without race, so I think it is easier to ask.
>>
>> [ I put back the err_out context at the end ]
>>
>>> + RCU_INIT_POINTER(link->map, map);
>>> +
>>> err = st_map->st_ops_desc->st_ops->reg(st_map->kvalue.data, &link->link);
>>> if (err) {
>>> + RCU_INIT_POINTER(link->map, NULL);
>>
>> In the bpf_struct_ops_map_link_detach() above, the update to link->map is
>> protected by the update_mutex. Could you explain how the link->map update to
>> NULL is safe here without holding the update_mutex?
>
> If err is not zero, it means the subsystem rejects the pair of the
> object and the link passing in. So, it has no reasonable to call
> bpf_struct_ops_map_link_detach() for this link.
>
> Does it make sense to you?
>
>>
>>> bpf_link_cleanup(&link_primer);
>>> + /* The link has been free by bpf_link_cleanup() */
>>> link = NULL;
>>> goto err_out;
>>> }
>
> At this point, we don't change the content of the link anymore except
> changing link->fd in bpf_link_settle(). So, it should be safe to call
> bpf_struct_ops_map_link_detach() from the subsystem.
>
> Should I explain it in a comment if you think it makes sense to you?
It makes sense and will be useful to get this details in the comment.
>
>>> - RCU_INIT_POINTER(link->map, map);
>>> return bpf_link_settle(&link_primer);
>>>
>>
>> err_out:
>> bpf_map_put(map);
>> kfree(link);
>> return err;
>> }
>>
>>
next prev parent reply other threads:[~2024-05-21 18:09 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-10 0:29 [PATCH bpf-next v3 0/7] Notify user space when a struct_ops object is detached/unregistered Kui-Feng Lee
2024-05-10 0:29 ` [PATCH bpf-next v3 1/7] bpf: pass bpf_struct_ops_link to callbacks in bpf_struct_ops Kui-Feng Lee
2024-05-10 0:29 ` [PATCH bpf-next v3 2/7] bpf: enable detaching links of struct_ops objects Kui-Feng Lee
2024-05-21 1:22 ` Martin KaFai Lau
2024-05-21 7:30 ` Kui-Feng Lee
2024-05-21 18:09 ` Martin KaFai Lau [this message]
2024-05-10 0:29 ` [PATCH bpf-next v3 3/7] bpf: support epoll from bpf struct_ops links Kui-Feng Lee
2024-05-21 1:26 ` Martin KaFai Lau
2024-05-21 7:31 ` Kui-Feng Lee
2024-05-10 0:29 ` [PATCH bpf-next v3 4/7] bpf: export bpf_link_inc_not_zero Kui-Feng Lee
2024-05-10 0:29 ` [PATCH bpf-next v3 5/7] selftests/bpf: test struct_ops with epoll Kui-Feng Lee
2024-05-10 0:29 ` [PATCH bpf-next v3 6/7] selftests/bpf: detach a struct_ops link from the subsystem managing it Kui-Feng Lee
2024-05-21 22:56 ` Amery Hung
2024-05-22 0:31 ` Kui-Feng Lee
2024-05-22 17:33 ` Amery Hung
2024-05-10 0:29 ` [PATCH bpf-next v3 7/7] selftests/bpf: make sure bpf_testmod handling racing link destroying well Kui-Feng Lee
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=ebd98bb8-a3c5-4c03-8812-2f64a36dd737@linux.dev \
--to=martin.lau@linux.dev \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=kernel-team@meta.com \
--cc=kuifeng@meta.com \
--cc=sinquersw@gmail.com \
--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