bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RESEND PATCH bpf-next v2 0/4] bpf: Introduce global percpu data
@ 2025-02-13 16:19 Leon Hwang
  2025-02-13 16:19 ` [RESEND PATCH bpf-next v2 1/4] " Leon Hwang
                   ` (3 more replies)
  0 siblings, 4 replies; 16+ messages in thread
From: Leon Hwang @ 2025-02-13 16:19 UTC (permalink / raw)
  To: bpf
  Cc: ast, daniel, andrii, yonghong.song, song, eddyz87, qmo, dxu,
	leon.hwang, kernel-patches-bot

Sorry for resending this patch set to add "v2" in subject.

This patch set introduces global percpu data, similar to commit
6316f78306c1 ("Merge branch 'support-global-data'"), to reduce restrictions
in C for BPF programs.

With this enhancement, it becomes possible to define and use global percpu
variables, much like the DEFINE_PER_CPU() macro in the kernel[0].

The idea stems from the bpflbr project[1], which itself was inspired by
retsnoop[2]. During testing of bpflbr on the v6.6 kernel, two LBR
(Last Branch Record) entries were observed related to the
bpf_get_smp_processor_id() helper.

Since commit 1ae6921009e5 ("bpf: inline bpf_get_smp_processor_id() helper"),
the bpf_get_smp_processor_id() helper has been inlined on x86_64, reducing
the overhead and consequently minimizing these two LBR records.

However, the introduction of global percpu data offers a more robust
solution. By leveraging the percpu_array map and percpu instructions,
global percpu data can be implemented intrinsically.

This feature also facilitates sharing percpu information between tail
callers and callees or between freplace callers and callees through a
shared global percpu variable. Previously, this was achieved using a
1-entry percpu_array map, which this patch set aims to improve upon.

Links:
[0] https://github.com/torvalds/linux/blob/fbfd64d25c7af3b8695201ebc85efe90be28c5a3/include/linux/percpu-defs.h#L114
[1] https://github.com/Asphaltt/bpflbr
[2] https://github.com/anakryiko/retsnoop

Changes:
v1 -> v2:
  * Address comments from Andrii:
    * Use LIBBPF_MAP_PERCPU and SEC_PERCPU.
    * Reuse mmaped of libbpf's struct bpf_map for .percpu map data.
    * Set .percpu struct pointer to NULL after loading skeleton.
    * Make sure value size of .percpu map is __aligned(8).
    * Use raw_tp and opts.cpu to test global percpu variables on all CPUs.
  * Address comments from Alexei:
    * Test non-zero offset of global percpu variable.
    * Test case about BPF_PSEUDO_MAP_IDX_VALUE.

rfc -> v1:
  * Address comments from Andrii:
    * Keep one image of global percpu variable for all CPUs.
    * Reject non-ARRAY map in bpf_map_direct_read(), check_reg_const_str(),
      and check_bpf_snprintf_call() in verifier.
    * Split out libbpf changes from kernel-side changes.
    * Use ".percpu" as PERCPU_DATA_SEC.
    * Use enum libbpf_map_type to distinguish BSS, DATA, RODATA and
      PERCPU_DATA.
    * Avoid using errno for checking err from libbpf_num_possible_cpus().
    * Use "map '%s': " prefix for error message.
Leon Hwang (4):
  bpf: Introduce global percpu data
  bpf, libbpf: Support global percpu data
  bpf, bpftool: Generate skeleton for global percpu data
  selftests/bpf: Add cases to test global percpu data

 kernel/bpf/arraymap.c                         |  41 +++-
 kernel/bpf/verifier.c                         |  45 ++++
 tools/bpf/bpftool/gen.c                       |  47 +++-
 tools/lib/bpf/libbpf.c                        | 101 ++++++--
 tools/lib/bpf/libbpf.h                        |   9 +
 tools/lib/bpf/libbpf.map                      |   1 +
 tools/testing/selftests/bpf/Makefile          |   2 +-
 .../bpf/prog_tests/global_data_init.c         | 217 +++++++++++++++++-
 .../bpf/progs/test_global_percpu_data.c       |  20 ++
 9 files changed, 448 insertions(+), 35 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/test_global_percpu_data.c

-- 
2.47.1


^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2025-02-27  2:13 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-02-13 16:19 [RESEND PATCH bpf-next v2 0/4] bpf: Introduce global percpu data Leon Hwang
2025-02-13 16:19 ` [RESEND PATCH bpf-next v2 1/4] " Leon Hwang
2025-02-19  1:47   ` Alexei Starovoitov
2025-02-24  5:25     ` Leon Hwang
2025-02-26  2:19   ` Hou Tao
2025-02-26  4:26     ` Hou Tao
2025-02-26 14:54     ` Leon Hwang
2025-02-26 15:31       ` Alexei Starovoitov
2025-02-26 16:12         ` Leon Hwang
2025-02-27  2:11       ` Hou Tao
2025-02-13 16:19 ` [RESEND PATCH bpf-next v2 2/4] bpf, libbpf: Support " Leon Hwang
2025-02-13 16:19 ` [RESEND PATCH bpf-next v2 3/4] bpf, bpftool: Generate skeleton for " Leon Hwang
2025-02-14  9:49   ` Leon Hwang
2025-02-13 16:19 ` [RESEND PATCH bpf-next v2 4/4] selftests/bpf: Add cases to test " Leon Hwang
2025-02-19  1:54   ` Alexei Starovoitov
2025-02-24  5:40     ` Leon Hwang

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).