tools/sched_ext: Add struct_size() helpers to common.bpf.h

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>
This commit is contained in:
Tejun Heo 2026-04-29 08:09:10 -10:00
parent df7b5ae038
commit 32a54807c9

View 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"