Skip to contentSkip to conversation

sched_ext: reject oversized dispatch_max_batch

PATCH Superseded Sechang Lim 3 participants 4 messages Nov 20

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

Fixes f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class")
---
 kernel/sched/ext/ext.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
kernel/sched/ext/ext.c +7 -2
index 10de6bd865e8..93128da6b126 100644
@@ -9197,9 +9197,14 @@ static int bpf_scx_init_member(const struct btf_type *t,
 
 	switch (moff) {
 	case offsetof(struct sched_ext_ops, dispatch_max_batch):
-		if (*(u32 *)(udata + moff) > INT_MAX)
+		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)
Signature · 1 line
2.56.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] sched_ext: unbraced variable declaration after case in bpf_scx_init_member() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] sched_ext: unbraced variable declaration after case in bpf_scx_init_member()
--

commit 5d981e883681165123cef4a6b3bfda42fae9cb16
Author: Sechang Lim <[email protected]>

sched_ext: reject oversized dispatch_max_batch

This commit validates sched_ext_ops.dispatch_max_batch against PCPU_MIN_UNIT_SIZE in bpf_scx_init_member() so oversized per-CPU dispatch buffer allocations are rejected with -E2BIG during struct_ops map updates.

kernel/sched/ext/ext.c static int bpf_scx_init_member(const struct btf_type *t, · line 9201

Quoted 11 lines
kernel/sched/ext/ext.c +2 -1
index a9e36b5bb0386..1f663a0490068 100644
@@ -9197,9 +9197,14 @@ static int bpf_scx_init_member(const struct btf_type *t,
 
 	switch (moff) {
 	case offsetof(struct sched_ext_ops, dispatch_max_batch):
-		if (*(u32 *)(udata + moff) > INT_MAX)
+		u32 batch = *(u32 *)(udata + moff);
+		u32 nr = batch ?: SCX_DSP_DFL_MAX_BATCH;

[Severity: Low]
Should this case body be wrapped in braces like the rescue_bandwidth_ppt and rescue_quantum_us cases below? Under -std=gnu11, declaring a variable immediately after a case label is a C23 extension that fails to build on supported GCC versions prior to GCC 11 (and Sparse) with "a label can only be part of a statement and a declaration is not a statement".

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
reviewer
#2 lore raw
In reply to Sechang Lim

Hi Sechang,

Quoted 23 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.

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.

Fixes f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class")
---
 kernel/sched/ext/ext.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
kernel/sched/ext/ext.c +0 -0
index 10de6bd865e8..93128da6b126 100644

kernel/sched/ext/ext.c static int bpf_scx_init_member(const struct btf_type *t, · line 9201

Quoted 6 lines
kernel/sched/ext/ext.c +2 -1
@@ -9197,9 +9197,14 @@ static int bpf_scx_init_member(const struct btf_type *t,
 
 	switch (moff) {
 	case offsetof(struct sched_ext_ops, dispatch_max_batch):
-		if (*(u32 *)(udata + moff) > INT_MAX)
+		u32 batch = *(u32 *)(udata + moff);
+		u32 nr = batch ?: SCX_DSP_DFL_MAX_BATCH;

The fix makes sense, but with clang:

In file included from kernel/sched/build_policy.c:71:
kernel/sched/ext/ext.c:9214:3: error: label followed by a declaration is a C23 extension [-Werror,-Wc23-extensions]

 9214 |                 u32 batch = *(u32 *)(udata + moff);
      |                 ^

1 error generated.

(also reported by sashiko)

Can you wrap that case in braces, matching the nearby cases?

Thanks,
-Andrea

Quoted 13 lines
kernel/sched/ext/ext.c +5 -1
+
+		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)
Signature · 1 line
2.56.1
#3 lore raw
Quoted 39 lines · Andrea Righi

Hi Sechang,

Quoted 23 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.

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.

Fixes f0e1a0643a59 ("sched_ext: Implement BPF extensible scheduler class")
---
 kernel/sched/ext/ext.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
kernel/sched/ext/ext.c +0 -0
index 10de6bd865e8..93128da6b126 100644

kernel/sched/ext/ext.c static int bpf_scx_init_member(const struct btf_type *t, · line 9201

Quoted 6 lines
kernel/sched/ext/ext.c +2 -1
@@ -9197,9 +9197,14 @@ static int bpf_scx_init_member(const struct btf_type *t,
 
 	switch (moff) {
 	case offsetof(struct sched_ext_ops, dispatch_max_batch):
-		if (*(u32 *)(udata + moff) > INT_MAX)
+		u32 batch = *(u32 *)(udata + moff);
+		u32 nr = batch ?: SCX_DSP_DFL_MAX_BATCH;

The fix makes sense, but with clang:

In file included from kernel/sched/build_policy.c:71:
kernel/sched/ext/ext.c:9214:3: error: label followed by a declaration is a C23 extension [-Werror,-Wc23-extensions]

9214 |                 u32 batch = *(u32 *)(udata + moff);
     |                 ^
Quoted 9 lines

1 error generated.

(also reported by sashiko)

Can you wrap that case in braces, matching the nearby cases?

Thanks,
-Andrea

Thanks for catching that. I'll wrap the case in braces and send v2 shortly.

Bests,
Sechang

Quoted 13 lines
Quoted 13 lines
kernel/sched/ext/ext.c +5 -2
+
+		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)
--

2.56.1