Skip to contentSkip to conversation

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

PATCH In review Tejun Heo 1 participant 1 message Apr 24 Scheduler - sched_ext

lore
Share & tools
lore

Tejun Heo

author reviewer
#0 lore raw

PATCH 9/17 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.

---
 tools/sched_ext/include/scx/common.bpf.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
tools/sched_ext/include/scx/common.bpf.h +10 -0
index 18f823d424cc..4bf959a8cd08 100644
@@ -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)		\
+	(sizeof(*(p)) + 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"
 
Signature · 1 line
2.53.0