* [PATCH] sched_ext: reject oversized dispatch_max_batch
@ 2026-11-20 5:49 Sechang Lim
2026-11-20 6:26 ` Andrea Righi
0 siblings, 1 reply; 3+ messages in thread
From: Sechang Lim @ 2026-11-20 5:49 UTC (permalink / raw)
To: sched-ext
Cc: tj, void, arighi, changwoo, mingo, peterz, juri.lelli,
vincent.guittot, dietmar.eggemann, rostedt, bsegall, mgorman,
vschneid, kprateek.nayak, haoluo, brho, joshdon, linux-kernel,
bpf
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")
Signed-off-by: Sechang Lim <rhkrqnwk98@gmail.com>
---
kernel/sched/ext/ext.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
index 10de6bd865e8..93128da6b126 100644
--- a/kernel/sched/ext/ext.c
+++ b/kernel/sched/ext/ext.c
@@ -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)
--
2.56.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] sched_ext: reject oversized dispatch_max_batch
2026-11-20 5:49 [PATCH] sched_ext: reject oversized dispatch_max_batch Sechang Lim
@ 2026-11-20 6:26 ` Andrea Righi
2026-11-20 6:44 ` Sechang Lim
0 siblings, 1 reply; 3+ messages in thread
From: Andrea Righi @ 2026-11-20 6:26 UTC (permalink / raw)
To: Sechang Lim
Cc: sched-ext, tj, void, changwoo, mingo, peterz, juri.lelli,
vincent.guittot, dietmar.eggemann, rostedt, bsegall, mgorman,
vschneid, kprateek.nayak, haoluo, brho, joshdon, linux-kernel,
bpf
Hi Sechang,
On Fri, Nov 20, 2026 at 05:49:08AM +0000, Sechang Lim wrote:
> 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")
> Signed-off-by: Sechang Lim <rhkrqnwk98@gmail.com>
> ---
> kernel/sched/ext/ext.c | 9 +++++++--
> 1 file changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
> index 10de6bd865e8..93128da6b126 100644
> --- a/kernel/sched/ext/ext.c
> +++ b/kernel/sched/ext/ext.c
> @@ -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
> +
> + 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
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] sched_ext: reject oversized dispatch_max_batch
2026-11-20 6:26 ` Andrea Righi
@ 2026-11-20 6:44 ` Sechang Lim
0 siblings, 0 replies; 3+ messages in thread
From: Sechang Lim @ 2026-11-20 6:44 UTC (permalink / raw)
To: Andrea Righi
Cc: sched-ext, tj, void, changwoo, mingo, peterz, juri.lelli,
vincent.guittot, dietmar.eggemann, rostedt, bsegall, mgorman,
vschneid, kprateek.nayak, haoluo, brho, joshdon, linux-kernel,
bpf
On Fri, Nov 20, 2026 at 07:26:53AM +0100, Andrea Righi wrote:
>Hi Sechang,
>
>On Fri, Nov 20, 2026 at 05:49:08AM +0000, Sechang Lim wrote:
>> 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")
>> Signed-off-by: Sechang Lim <rhkrqnwk98@gmail.com>
>> ---
>> kernel/sched/ext/ext.c | 9 +++++++--
>> 1 file changed, 7 insertions(+), 2 deletions(-)
>>
>> diff --git a/kernel/sched/ext/ext.c b/kernel/sched/ext/ext.c
>> index 10de6bd865e8..93128da6b126 100644
>> --- a/kernel/sched/ext/ext.c
>> +++ b/kernel/sched/ext/ext.c
>> @@ -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
>
Thanks for catching that. I'll wrap the case in braces and send v2
shortly.
Bests,
Sechang
>> +
>> + 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
>>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-20 6:44 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20 5:49 [PATCH] sched_ext: reject oversized dispatch_max_batch Sechang Lim
2026-11-20 6:26 ` Andrea Righi
2026-11-20 6:44 ` Sechang Lim
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®