Skip to contentSkip to conversation

sched_ext: reject oversized dispatch_max_batch

PATCH In review Sechang Lim 2 participants 2 messages Nov 20

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 sched_ext: reject oversized dispatch_max_batch

bpf_scx_init_member() accepts dispatch_max_batch values up to INT_MAX. The value determines the size of the per-CPU dispatch buffer, but __alloc_percpu() rejects allocations larger than PCPU_MIN_UNIT_SIZE. With BPF_F_LINK, an oversized value passes the struct_ops map update and triggers an illegal-size warning during link creation before the allocation fails with -ENOMEM.

Calculate the buffer size using the effective batch count, including the default used when dispatch_max_batch is zero. Reject values above the per-CPU allocation limit with -E2BIG during the map update, before the allocator is called. This also covers registration without a link.

Fixes f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class")

---
v2:
- add braces (Andrea Righi)

v1:
- https://lore.kernel.org/sched-ext/[email protected]/T/#u

 kernel/sched/ext/ext.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
kernel/sched/ext/ext.c +9 -3
index 10de6bd865e8..56d75d179822 100644
@@ -9196,11 +9196,17 @@ static int bpf_scx_init_member(const struct btf_type *t,
 	int ret;
 
 	switch (moff) {
-	case offsetof(struct sched_ext_ops, dispatch_max_batch):
-		if (*(u32 *)(udata + moff) > INT_MAX)
+	case offsetof(struct sched_ext_ops, dispatch_max_batch): {
+		u32 batch = *(u32 *)(udata + moff);
+		u32 nr = batch ?: SCX_DSP_DFL_MAX_BATCH;
+
+		if (struct_size_t(struct scx_sched_pcpu, dsp_ctx.buf, nr) >
+				PCPU_MIN_UNIT_SIZE)
 			return -E2BIG;
-		ops->dispatch_max_batch = *(u32 *)(udata + moff);
+
+		ops->dispatch_max_batch = batch;
 		return 1;
+	}
 	case offsetof(struct sched_ext_ops, flags):
 		if (*(u64 *)(udata + moff) & ~SCX_OPS_ALL_FLAGS)
 			return -EINVAL;
Signature · 1 line
2.56.1
reviewer Reviewed-by on v2 1/1
#1 lore raw
Quoted 7 lines · Sechang Lim

bpf_scx_init_member() accepts dispatch_max_batch values up to INT_MAX. The value determines the size of the per-CPU dispatch buffer, but __alloc_percpu() rejects allocations larger than PCPU_MIN_UNIT_SIZE. With BPF_F_LINK, an oversized value passes the struct_ops map update and triggers an illegal-size warning during link creation before the allocation fails with -ENOMEM.

Quoted 7 lines

Calculate the buffer size using the effective batch count, including the default used when dispatch_max_batch is zero. Reject values above the per-CPU allocation limit with -E2BIG during the map update, before the allocator is called. This also covers registration without a link.

Fixes f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class")

Thanks,
-Andrea

Quoted 39 lines

---
v2:
- add braces (Andrea Righi)

v1:
- https://lore.kernel.org/sched-ext/[email protected]/T/#u

 kernel/sched/ext/ext.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
kernel/sched/ext/ext.c +9 -3
index 10de6bd865e8..56d75d179822 100644
@@ -9196,11 +9196,17 @@ static int bpf_scx_init_member(const struct btf_type *t,
 	int ret;
 
 	switch (moff) {
-	case offsetof(struct sched_ext_ops, dispatch_max_batch):
-		if (*(u32 *)(udata + moff) > INT_MAX)
+	case offsetof(struct sched_ext_ops, dispatch_max_batch): {
+		u32 batch = *(u32 *)(udata + moff);
+		u32 nr = batch ?: SCX_DSP_DFL_MAX_BATCH;
+
+		if (struct_size_t(struct scx_sched_pcpu, dsp_ctx.buf, nr) >
+				PCPU_MIN_UNIT_SIZE)
 			return -E2BIG;
-		ops->dispatch_max_batch = *(u32 *)(udata + moff);
+
+		ops->dispatch_max_batch = batch;
 		return 1;
+	}
 	case offsetof(struct sched_ext_ops, flags):
 		if (*(u64 *)(udata + moff) & ~SCX_OPS_ALL_FLAGS)
 			return -EINVAL;
Signature · 1 line
2.56.1