]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
tools/sched_ext: Add struct_size() helpers to common.bpf.h
authorTejun Heo <tj@kernel.org>
Wed, 29 Apr 2026 18:09:10 +0000 (08:09 -1000)
committerTejun Heo <tj@kernel.org>
Wed, 29 Apr 2026 18:25:07 +0000 (08:25 -1000)
Add flex_array_size(), struct_size() and struct_size_t() to
scx/common.bpf.h so BPF schedulers can size flex-array-containing
structs the same way kernel code does. These are abbreviated forms of
the <linux/overflow.h> macros.

v3: Use offsetof() instead of sizeof() in struct_size() to match kernel
    semantics (no inflation from trailing struct padding). (Sashiko)

Signed-off-by: Tejun Heo <tj@kernel.org>
Reviewed-by: Cheng-Yang Chou <yphbchou0911@gmail.com>
Reviewed-by: Changwoo Min <changwoo@igalia.com>
Reviewed-by: Andrea Righi <arighi@nvidia.com>
tools/sched_ext/include/scx/common.bpf.h

index 18f823d424cc1951bb764d75784b6d3bcb83bc63..087ae4f79c603c9a123a4a4420af037c6f0472af 100644 (file)
@@ -1043,6 +1043,16 @@ static inline u64 scx_clock_irq(u32 cpu)
        return irqt ? BPF_CORE_READ(irqt, total) : 0;
 }
 
+/* Abbreviated forms of <linux/overflow.h>'s struct_size() family. */
+#define flex_array_size(p, member, count)      \
+       ((count) * sizeof(*(p)->member))
+
+#define struct_size(p, member, count)          \
+       (offsetof(typeof(*(p)), member) + flex_array_size(p, member, count))
+
+#define struct_size_t(type, member, count)     \
+       struct_size((type *)NULL, member, count)
+
 #include "compat.bpf.h"
 #include "enums.bpf.h"