From: Leon Hwang <leon.hwang@linux.dev>
To: Andrii Nakryiko <andrii.nakryiko@gmail.com>
Cc: bpf@vger.kernel.org, ast@kernel.org, andrii@kernel.org,
daniel@iogearbox.net
Subject: Re: [RFC PATCH bpf-next 2/3] bpf, libbpf: Support BPF_F_CPU for percpu_array map
Date: Thu, 3 Jul 2025 01:32:45 +0800 [thread overview]
Message-ID: <f69609ad-d277-46fd-933d-d8838353ff01@linux.dev> (raw)
In-Reply-To: <CAEf4BzbsN3E467efA3Wu1TMwW+J=6ZMgtF7H490_waec32Grgg@mail.gmail.com>
On 2025/7/3 01:30, Andrii Nakryiko wrote:
> On Wed, Jul 2, 2025 at 10:28 AM Leon Hwang <leon.hwang@linux.dev> wrote:
>>
>>
>>
>> On 2025/7/2 04:22, Andrii Nakryiko wrote:
>>> On Tue, Jun 24, 2025 at 9:55 AM Leon Hwang <leon.hwang@linux.dev> wrote:
>>>>
>>>> This patch adds libbpf support for the BPF_F_CPU flag in percpu_array maps,
>>>> introducing the following APIs:
>>>>
>>>> 1. bpf_map_update_elem_opts(): update with struct bpf_map_update_elem_opts
>>>> 2. bpf_map_lookup_elem_opts(): lookup with struct bpf_map_lookup_elem_opts
>>>> 3. bpf_map__update_elem_opts(): high-level wrapper with input validation
>>>> 4. bpf_map__lookup_elem_opts(): high-level wrapper with input validation
>>>>
>>>> Behavior:
>>>>
>>>> * If opts->cpu == 0xFFFFFFFF, the update is applied to all CPUs.
>>>> * Otherwise, it applies only to the specified CPU.
>>>> * Lookup APIs retrieve values from the target CPU when BPF_F_CPU is used.
>>>>
>>>> Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
>>>> ---
>>>> tools/lib/bpf/bpf.c | 37 +++++++++++++++++++++++
>>>> tools/lib/bpf/bpf.h | 35 +++++++++++++++++++++-
>>>> tools/lib/bpf/libbpf.c | 56 +++++++++++++++++++++++++++++++++++
>>>> tools/lib/bpf/libbpf.h | 45 ++++++++++++++++++++++++++++
>>>> tools/lib/bpf/libbpf.map | 4 +++
>>>> tools/lib/bpf/libbpf_common.h | 12 ++++++++
>>>> 6 files changed, 188 insertions(+), 1 deletion(-)
>>>>
>
> [...]
>
>>>> };
>>>> -#define bpf_map_batch_opts__last_field flags
>>>> +#define bpf_map_batch_opts__last_field cpu
>>>>
>>>>
>>>> /**
>>>> @@ -286,6 +315,10 @@ LIBBPF_API int bpf_map_lookup_and_delete_batch(int fd, void *in_batch,
>>>> * Update spin_lock-ed map elements. This must be
>>>> * specified if the map value contains a spinlock.
>>>> *
>>>> + * **BPF_F_CPU**
>>>> + * As for percpu map, update value on all CPUs if **opts->cpu** is
>>>> + * 0xFFFFFFFF, or on specified CPU otherwise.
>>>> + *
>>>> * @param fd BPF map file descriptor
>>>> * @param keys pointer to an array of *count* keys
>>>> * @param values pointer to an array of *count* values
>>>> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
>>>> index 6445165a24f2..30400bdc20d9 100644
>>>> --- a/tools/lib/bpf/libbpf.c
>>>> +++ b/tools/lib/bpf/libbpf.c
>>>> @@ -10636,6 +10636,34 @@ int bpf_map__lookup_elem(const struct bpf_map *map,
>>>> return bpf_map_lookup_elem_flags(map->fd, key, value, flags);
>>>> }
>>>>
>>>> +int bpf_map__lookup_elem_opts(const struct bpf_map *map, const void *key,
>>>> + size_t key_sz, void *value, size_t value_sz,
>>>> + const struct bpf_map_lookup_elem_opts *opts)
>>>> +{
>>>> + int nr_cpus = libbpf_num_possible_cpus();
>>>> + __u32 cpu = OPTS_GET(opts, cpu, nr_cpus);
>>>> + __u64 flags = OPTS_GET(opts, flags, 0);
>>>> + int err;
>>>> +
>>>> + if (flags & BPF_F_CPU) {
>>>> + if (map->def.type != BPF_MAP_TYPE_PERCPU_ARRAY)
>>>> + return -EINVAL;
>>>> + if (cpu >= nr_cpus)
>>>> + return -E2BIG;
>>>> + if (map->def.value_size != value_sz) {
>>>> + pr_warn("map '%s': unexpected value size %zu provided, expected %u\n",
>>>> + map->name, value_sz, map->def.value_size);
>>>> + return -EINVAL;
>>>> + }
>>>
>>> shouldn't this go into validate_map_op?..
>>>
>>
>> It should.
>>
>> However, to avoid making validate_map_op really complicated, I'd like to
>> add validate_map_cpu_op to wrap checking cpu and validate_map_op.
>
> validate_map_op is meant to handle all the different conditions, let's
> keep all that in one function
>
Got it.
Thanks,
Leon
next prev parent reply other threads:[~2025-07-02 17:32 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-24 16:53 [RFC PATCH bpf-next 0/3] bpf: Introduce BPF_F_CPU flag for percpu_array map Leon Hwang
2025-06-24 16:53 ` [RFC PATCH bpf-next 1/3] " Leon Hwang
2025-07-01 20:22 ` Andrii Nakryiko
2025-07-02 17:01 ` Leon Hwang
2025-07-02 17:13 ` Andrii Nakryiko
2025-06-24 16:53 ` [RFC PATCH bpf-next 2/3] bpf, libbpf: Support BPF_F_CPU " Leon Hwang
2025-07-01 20:22 ` Andrii Nakryiko
2025-07-02 17:28 ` Leon Hwang
2025-07-02 17:30 ` Andrii Nakryiko
2025-07-02 17:32 ` Leon Hwang [this message]
2025-06-24 16:53 ` [RFC PATCH bpf-next 3/3] selftests/bpf: Add case to test BPF_F_CPU Leon Hwang
2025-07-01 20:22 ` Andrii Nakryiko
2025-07-02 17:29 ` Leon Hwang
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=f69609ad-d277-46fd-933d-d8838353ff01@linux.dev \
--to=leon.hwang@linux.dev \
--cc=andrii.nakryiko@gmail.com \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.