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, jolsa@kernel.org, yonghong.song@linux.dev,
song@kernel.org, eddyz87@gmail.com, dxu@dxuuu.xyz,
deso@posteo.net, kernel-patches-bot@fb.com
Subject: Re: [PATCH bpf-next v8 5/7] bpf: Add BPF_F_CPU and BPF_F_ALL_CPUS flags support for percpu_cgroup_storage maps
Date: Sun, 28 Sep 2025 23:06:34 +0800 [thread overview]
Message-ID: <b3eb97bb-ba9e-4f1c-96e6-8fab12efab2d@linux.dev> (raw)
In-Reply-To: <CAEf4Bzacd768RGKyujM7TTWa-JeNnZntJbJoZr2FetCR4X-soQ@mail.gmail.com>
On 2025/9/28 10:42, Andrii Nakryiko wrote:
> On Thu, Sep 25, 2025 at 8:38 AM Leon Hwang <leon.hwang@linux.dev> wrote:
>>
>> Introduce BPF_F_ALL_CPUS flag support for percpu_cgroup_storage maps to
>> allow updating values for all CPUs with a single value for update_elem
>> API.
>>
>> Introduce BPF_F_CPU flag support for percpu_cgroup_storage maps to
>> allow:
>>
>> * update value for specified CPU for update_elem API.
>> * lookup value for specified CPU for lookup_elem API.
>>
>> The BPF_F_CPU flag is passed via map_flags along with embedded cpu info.
>>
>> Signed-off-by: Leon Hwang <leon.hwang@linux.dev>
>> ---
>> include/linux/bpf-cgroup.h | 4 ++--
>> include/linux/bpf.h | 1 +
>> kernel/bpf/local_storage.c | 22 +++++++++++++++++++---
>> kernel/bpf/syscall.c | 2 +-
>> 4 files changed, 23 insertions(+), 6 deletions(-)
>>
>
> [...]
>
>> int bpf_percpu_cgroup_storage_copy(struct bpf_map *_map, void *key,
>> - void *value)
>> + void *value, u64 map_flags)
>> {
>> struct bpf_cgroup_storage_map *map = map_to_storage(_map);
>> struct bpf_cgroup_storage *storage;
>> @@ -199,11 +199,17 @@ int bpf_percpu_cgroup_storage_copy(struct bpf_map *_map, void *key,
>> * will not leak any kernel data
>> */
>> size = round_up(_map->value_size, 8);
>
> um... same issue with rounding up value_size when BPF_F_CPU is set, no?
>
>> + if (map_flags & BPF_F_CPU) {
>> + cpu = map_flags >> 32;
>> + bpf_long_memcpy(value, per_cpu_ptr(storage->percpu_buf, cpu), size);
>> + goto unlock;
>> + }
>> for_each_possible_cpu(cpu) {
>> bpf_long_memcpy(value + off,
>> per_cpu_ptr(storage->percpu_buf, cpu), size);
>> off += size;
>> }
>> +unlock:
>> rcu_read_unlock();
>> return 0;
>> }
>> @@ -216,7 +222,7 @@ int bpf_percpu_cgroup_storage_update(struct bpf_map *_map, void *key,
>> int cpu, off = 0;
>> u32 size;
>>
>> - if (map_flags != BPF_ANY && map_flags != BPF_EXIST)
>> + if ((u32)map_flags & ~(BPF_ANY | BPF_EXIST | BPF_F_CPU | BPF_F_ALL_CPUS))
>> return -EINVAL;
>>
>> rcu_read_lock();
>> @@ -233,11 +239,21 @@ int bpf_percpu_cgroup_storage_update(struct bpf_map *_map, void *key,
>> * so no kernel data leaks possible
>> */
>> size = round_up(_map->value_size, 8);
>> + if (map_flags & BPF_F_CPU) {
>> + cpu = map_flags >> 32;
>> + bpf_long_memcpy(per_cpu_ptr(storage->percpu_buf, cpu), value, size);
>
> ditto
>
>> + goto unlock;
>> + }
>> for_each_possible_cpu(cpu) {
>> bpf_long_memcpy(per_cpu_ptr(storage->percpu_buf, cpu),
>> value + off, size);
>> - off += size;
>> + /* same user-provided value is used if BPF_F_ALL_CPUS is
>> + * specified, otherwise value is an array of per-CPU values.
>> + */
>> + if (!(map_flags & BPF_F_ALL_CPUS))
>> + off += size;
>
> btw, given we'll need another revision to fix up all those round_up()
> issues, what do you think about make this offset logic completely
> stateless (and, in my opinion, more obvious):
>
> for_each_possible_cpu(cpu) {
> p = (map_flags & BPF_F_ALL_CPUS) ? value : value + size * cpu;
> memcpy(per_cpu_ptr(storage->percpu_buf, cpu), p, size);
> }
>
> seems more straightforward to me
lgtm.
But I think the correct memcpy() should look like this:
memcpy(per_cpu_ptr(storage->percpu_buf, cpu), p,
(map_flags & BPF_F_ALL_CPUS) ? _map->value_size : size);
because 'size' is 8-byte aligned and can’t be used directly when
'map_flags & BPF_F_ALL_CPUS' is set.
So the more accurate version would be:
for_each_possible_cpu(cpu) {
p = (map_flags & BPF_F_ALL_CPUS) ? value : value + size * cpu;
s = (map_flags & BPF_F_ALL_CPUS) ? _map->value_size : size;
memcpy(per_cpu_ptr(storage->percpu_buf, cpu), p, s);
}
Isn’t this the correct approach?
Thanks,
Leon
[...]
next prev parent reply other threads:[~2025-09-28 15:06 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-25 15:37 [PATCH bpf-next v8 0/7] bpf: Introduce BPF_F_CPU and BPF_F_ALL_CPUS flags for percpu maps Leon Hwang
2025-09-25 15:37 ` [PATCH bpf-next v8 1/7] bpf: Introduce internal bpf_map_check_op_flags helper function Leon Hwang
2025-09-25 15:37 ` [PATCH bpf-next v8 2/7] bpf: Introduce BPF_F_CPU and BPF_F_ALL_CPUS flags Leon Hwang
2025-09-28 2:42 ` Andrii Nakryiko
2025-09-25 15:37 ` [PATCH bpf-next v8 3/7] bpf: Add BPF_F_CPU and BPF_F_ALL_CPUS flags support for percpu_array maps Leon Hwang
2025-09-25 15:37 ` [PATCH bpf-next v8 4/7] bpf: Add BPF_F_CPU and BPF_F_ALL_CPUS flags support for percpu_hash and lru_percpu_hash maps Leon Hwang
2025-09-25 15:37 ` [PATCH bpf-next v8 5/7] bpf: Add BPF_F_CPU and BPF_F_ALL_CPUS flags support for percpu_cgroup_storage maps Leon Hwang
2025-09-28 2:42 ` Andrii Nakryiko
2025-09-28 15:06 ` Leon Hwang [this message]
2025-09-29 16:17 ` Andrii Nakryiko
2025-09-25 15:37 ` [PATCH bpf-next v8 6/7] libbpf: Add BPF_F_CPU and BPF_F_ALL_CPUS flags support for percpu maps Leon Hwang
2025-09-28 2:42 ` Andrii Nakryiko
2025-09-25 15:37 ` [PATCH bpf-next v8 7/7] selftests/bpf: Add cases to test BPF_F_CPU and BPF_F_ALL_CPUS flags 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=b3eb97bb-ba9e-4f1c-96e6-8fab12efab2d@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 \
--cc=deso@posteo.net \
--cc=dxu@dxuuu.xyz \
--cc=eddyz87@gmail.com \
--cc=jolsa@kernel.org \
--cc=kernel-patches-bot@fb.com \
--cc=song@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