Skip to contentSkip to conversation

selftests/bpf: Remove duplicate copies of the arena spinlock qnodes

PATCH Superseded Changwoo Min 4 participants 6 messages Aug 3 – Aug 17

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Changwoo Min

author reviewer
#0 lore raw

PATCH 1/1 selftests/bpf: Remove duplicate copies of the arena spinlock qnodes

bpf_arena_spin_lock.h defines its 64KB qnodes array in the header, so every translation unit including it emits a copy. __weak makes them all resolve to one instance, but bpftool gen object merges only the symbols and concatenates each input's .addr_space.1 bytes, leaving the surplus copies unreferenced in the linked object.

libarena links ten such units, so nine copies were dead weight (bytes):

object before after
-----------------------------------------------------
.addr_space.1 in libarena.bpf.o 676200 86376
libarena.skel.h 2100123 892371
libarena_asan.skel.h 2641124 1466477

Declare qnodes in the header and let each program define it once:
libarena in src/common.bpf.c, and the arena_spin_lock test beside the lock it guards.

Tested with test_progs -t arena_spin_lock and -t libarena.

---
 .../selftests/bpf/libarena/include/bpf_arena_spin_lock.h   | 7 +------
 tools/testing/selftests/bpf/libarena/src/common.bpf.c      | 7 +++++++
 tools/testing/selftests/bpf/progs/arena_spin_lock.c        | 7 +++++++
 3 files changed, 15 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/libarena/include/bpf_arena_spin_lock.h +1 -6
index ae6b72d15bb6..71d9db610263 100644
@@ -103,12 +103,7 @@ struct arena_qnode {
 #define _Q_LOCKED_VAL		(1U << _Q_LOCKED_OFFSET)
 #define _Q_PENDING_VAL		(1U << _Q_PENDING_OFFSET)
 
-/*
- * The qnodes are marked __weak so we can define them in the header
- * while still ensuring all compilation units use the same struct
- * instance.
- */
-struct arena_qnode __weak __arena __hidden qnodes[_Q_MAX_CPUS][_Q_MAX_NODES];
+extern struct arena_qnode __arena __hidden qnodes[_Q_MAX_CPUS][_Q_MAX_NODES];
 
 static inline u32 encode_tail(int cpu, int idx)
 {
tools/testing/selftests/bpf/libarena/src/common.bpf.c +7 -0
index 50be57213dfb..06481f2a2892 100644
@@ -8,6 +8,13 @@ const volatile u32 zero = 0;
 
 struct buddy __arena buddy;
 
+/*
+ * Storage for the queue nodes declared by bpf_arena_spin_lock.h. Each program
+ * linking the arena spinlock provides exactly one definition, so that the array
+ * is emitted once rather than once per translation unit.
+ */
+struct arena_qnode __arena __hidden qnodes[_Q_MAX_CPUS][_Q_MAX_NODES];
+
 int arena_fls(__u64 word)
 {
 	if (!word)
tools/testing/selftests/bpf/progs/arena_spin_lock.c +7 -0
index cf7cda79c16c..92e75ec3844c 100644
@@ -23,6 +23,13 @@ int cs_count;
 #if defined(ENABLE_ATOMICS_TESTS) && defined(__BPF_FEATURE_ADDR_SPACE_CAST)
 arena_spinlock_t __arena lock;
 int test_skip = 1;
+
+/*
+ * Storage for the queue nodes declared by bpf_arena_spin_lock.h. Each program
+ * linking the arena spinlock provides exactly one definition; libarena's lives
+ * in libarena/src/common.bpf.c.
+ */
+struct arena_qnode __arena __hidden qnodes[_Q_MAX_CPUS][_Q_MAX_NODES];
 #else
 int test_skip = 2;
 #endif
Signature · 1 line
2.55.0
#1 lore raw
Quoted 14 lines · Changwoo Min

bpf_arena_spin_lock.h defines its 64KB qnodes array in the header, so every translation unit including it emits a copy. __weak makes them all resolve to one instance, but bpftool gen object merges only the symbols and concatenates each input's .addr_space.1 bytes, leaving the surplus copies unreferenced in the linked object.

libarena links ten such units, so nine copies were dead weight (bytes):

object before after
-----------------------------------------------------
.addr_space.1 in libarena.bpf.o 676200 86376
libarena.skel.h 2100123 892371
libarena_asan.skel.h 2641124 1466477

Quoted 8 lines

Declare qnodes in the header and let each program define it once:
libarena in src/common.bpf.c, and the arena_spin_lock test beside the lock it guards.

Tested with test_progs -t arena_spin_lock and -t libarena.

---

This seems ok to me. I don't have better ideas. Emil, any thoughts?

Quoted 59 lines
 .../selftests/bpf/libarena/include/bpf_arena_spin_lock.h   | 7 +------
 tools/testing/selftests/bpf/libarena/src/common.bpf.c      | 7 +++++++
 tools/testing/selftests/bpf/progs/arena_spin_lock.c        | 7 +++++++
 3 files changed, 15 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/libarena/include/bpf_arena_spin_lock.h +1 -6
index ae6b72d15bb6..71d9db610263 100644
@@ -103,12 +103,7 @@ struct arena_qnode {
 #define _Q_LOCKED_VAL		(1U << _Q_LOCKED_OFFSET)
 #define _Q_PENDING_VAL		(1U << _Q_PENDING_OFFSET)
 
-/*
- * The qnodes are marked __weak so we can define them in the header
- * while still ensuring all compilation units use the same struct
- * instance.
- */
-struct arena_qnode __weak __arena __hidden qnodes[_Q_MAX_CPUS][_Q_MAX_NODES];
+extern struct arena_qnode __arena __hidden qnodes[_Q_MAX_CPUS][_Q_MAX_NODES];
 
 static inline u32 encode_tail(int cpu, int idx)
 {
tools/testing/selftests/bpf/libarena/src/common.bpf.c +7 -0
index 50be57213dfb..06481f2a2892 100644
@@ -8,6 +8,13 @@ const volatile u32 zero = 0;
 
 struct buddy __arena buddy;
 
+/*
+ * Storage for the queue nodes declared by bpf_arena_spin_lock.h. Each program
+ * linking the arena spinlock provides exactly one definition, so that the array
+ * is emitted once rather than once per translation unit.
+ */
+struct arena_qnode __arena __hidden qnodes[_Q_MAX_CPUS][_Q_MAX_NODES];
+
 int arena_fls(__u64 word)
 {
 	if (!word)
tools/testing/selftests/bpf/progs/arena_spin_lock.c +7 -0
index cf7cda79c16c..92e75ec3844c 100644
@@ -23,6 +23,13 @@ int cs_count;
 #if defined(ENABLE_ATOMICS_TESTS) && defined(__BPF_FEATURE_ADDR_SPACE_CAST)
 arena_spinlock_t __arena lock;
 int test_skip = 1;
+
+/*
+ * Storage for the queue nodes declared by bpf_arena_spin_lock.h. Each program
+ * linking the arena spinlock provides exactly one definition; libarena's lives
+ * in libarena/src/common.bpf.c.
+ */
+struct arena_qnode __arena __hidden qnodes[_Q_MAX_CPUS][_Q_MAX_NODES];
 #else
 int test_skip = 2;
 #endif
maintainer
#2 lore raw
In reply to Changwoo Min
Quoted 14 lines · Changwoo Min

bpf_arena_spin_lock.h defines its 64KB qnodes array in the header, so every translation unit including it emits a copy. __weak makes them all resolve to one instance, but bpftool gen object merges only the symbols and concatenates each input's .addr_space.1 bytes, leaving the surplus copies unreferenced in the linked object.

libarena links ten such units, so nine copies were dead weight (bytes):

   object                             before       after
   -----------------------------------------------------
   .addr_space.1 in libarena.bpf.o    676200       86376
   libarena.skel.h                   2100123      892371
   libarena_asan.skel.h              2641124     1466477
Quoted 7 lines

Declare qnodes in the header and let each program define it once:
libarena in src/common.bpf.c, and the arena_spin_lock test beside the lock it guards.

Tested with test_progs -t arena_spin_lock and -t libarena.

This doesn't apply cleanly, could you respin against latest bpf-next?

Thanks,
Daniel

author reviewer
#3 lore raw

Hi Daniel,

Quoted 15 lines · Daniel Borkmann
Quoted 14 lines · Changwoo Min

bpf_arena_spin_lock.h defines its 64KB qnodes array in the header, so every translation unit including it emits a copy. __weak makes them all resolve to one instance, but bpftool gen object merges only the symbols and concatenates each input's .addr_space.1 bytes, leaving the surplus copies unreferenced in the linked object.

libarena links ten such units, so nine copies were dead weight (bytes):

   object                             before       after
   -----------------------------------------------------
   .addr_space.1 in libarena.bpf.o    676200       86376 libarena.skel.h                   2100123      892371 libarena_asan.skel.h              2641124     1466477

Quoted 8 lines
Quoted 7 lines

Declare qnodes in the header and let each program define it once:
libarena in src/common.bpf.c, and the arena_spin_lock test beside the lock it guards.

Tested with test_progs -t arena_spin_lock and -t libarena.

This doesn't apply cleanly, could you respin against latest bpf-next?

Sure. I will send the v2 rebased on the HEAD of the the bpf-next tree soon.

Regards.
Changwoo Min

Quoted 4 lines

Thanks,
Daniel

Reviewed-by
#4 lore raw
Quoted 27 lines · Changwoo Min
Quoted 2 lines

Hi Daniel,

Quoted 22 lines · Daniel Borkmann
Quoted 21 lines · Changwoo Min

bpf_arena_spin_lock.h defines its 64KB qnodes array in the header, so every translation unit including it emits a copy. __weak makes them all resolve to one instance, but bpftool gen object merges only the symbols and concatenates each input's .addr_space.1 bytes, leaving the surplus copies unreferenced in the linked object.

libarena links ten such units, so nine copies were dead weight (bytes):

   object                             before       after
   -----------------------------------------------------
   .addr_space.1 in libarena.bpf.o    676200       86376
   libarena.skel.h                   2100123      892371
   libarena_asan.skel.h              2641124     1466477

Declare qnodes in the header and let each program define it once:
libarena in src/common.bpf.c, and the arena_spin_lock test beside the lock it guards.

Tested with test_progs -t arena_spin_lock and -t libarena.

Hi Changwoo,

Sorry for the late reply, for the next version feel free to add:

Quoted 13 lines
Quoted 1 line

This doesn't apply cleanly, could you respin against latest bpf-next?

Sure. I will send the v2 rebased on the HEAD of the the bpf-next tree soon.

Regards.
Changwoo Min

Quoted 4 lines

Thanks,
Daniel

#5 lore raw
Quoted 27 lines · Emil Tsalapatis
Quoted 26 lines · Changwoo Min
Quoted 2 lines

Hi Daniel,

Quoted 21 lines · Daniel Borkmann
Quoted 20 lines · Changwoo Min

bpf_arena_spin_lock.h defines its 64KB qnodes array in the header, so every translation unit including it emits a copy. __weak makes them all resolve to one instance, but bpftool gen object merges only the symbols and concatenates each input's .addr_space.1 bytes, leaving the surplus copies unreferenced in the linked object.

libarena links ten such units, so nine copies were dead weight (bytes):

   object                             before       after
   -----------------------------------------------------
   .addr_space.1 in libarena.bpf.o    676200       86376
   libarena.skel.h                   2100123      892371
   libarena_asan.skel.h              2641124     1466477

Declare qnodes in the header and let each program define it once:
libarena in src/common.bpf.c, and the arena_spin_lock test beside the lock it guards.

Tested with test_progs -t arena_spin_lock and -t libarena.

Quoted 8 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

Hi Changwoo,

Sorry for the late reply, for the next version feel free to add:

Added the tag to v2 and applied, thanks.

Quoted 13 lines
Quoted 13 lines
Quoted 1 line

This doesn't apply cleanly, could you respin against latest bpf-next?

Sure. I will send the v2 rebased on the HEAD of the the bpf-next tree soon.

Regards.
Changwoo Min

Quoted 4 lines

Thanks,
Daniel