Skip to content

[Deepin-Kernel-SIG] [linux 6.6.y] [Upstream] sched update for sched_ext - #2219

Merged
opsiff merged 8 commits into
deepin-community:linux-6.6.yfrom
opsiff:linux-6.6.y-2026-10-22
Oct 22, 2026
Merged

opsiff merged 8 commits into
deepin-community:linux-6.6.yfrom
opsiff:linux-6.6.y-2026-10-22

Conversation

@opsiff

@opsiff opsiff commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Aligned with: https://gitee.com/anolis/cloud-kernel/pulls/4865/commits

Summary by Sourcery

Update scheduler and cgroup interfaces to support sched_ext scheduling classes and safer task lifecycle handling.

Bug Fixes:

  • Make cgroup task iteration safe across interrupt contexts by preserving interrupt state while locking.
  • Allow scheduler-specific fork cancellation and return errors from cgroup-aware scheduler fork setup.

Enhancements:

  • Extend scheduler-class interfaces for task reweighting and class-transition handling, while centralizing load-average updates and cgroup weight conversions to support sched_ext integration.

kchuyizhou and others added 8 commits October 22, 2026 10:02
mainline inclusion
from mainline-v6.7-rc1
category: feature

This patch makes some preparations for using css_task_iter_*() in BPF
Program.

1. Flags CSS_TASK_ITER_* are #define-s and it's not easy for bpf prog to
use them. Convert them to enum so bpf prog can take them from vmlinux.h.

2. In the next patch we will add css_task_iter_*() in common kfuncs which
is not safe. Since css_task_iter_*() does spin_unlock_irq() which might
screw up irq flags depending on the context where bpf prog is running.
So we should use irqsave/irqrestore here and the switching is harmless.

Suggested-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Chuyi Zhou <zhouchuyi@bytedance.com>
Acked-by: Tejun Heo <tj@kernel.org>
Link: https://lore.kernel.org/r/20231018061746.111364-2-zhouchuyi@bytedance.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
(cherry picked from commit 6da8830)
Signed-off-by: LFRon <ronforever@outlook.com>
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion
from mainline-v6.12-rc1
category: feature

Currently, sched_init() checks that the sched_class'es are in the expected
order by testing each adjacency which is a bit brittle and makes it
cumbersome to add optional sched_class'es. Instead, let's verify whether
they're in the expected order using sched_class_above() which is what
matters.

Signed-off-by: Tejun Heo <tj@kernel.org>
Suggested-by: Peter Zijlstra <peterz@infradead.org>
Reviewed-by: David Vernet <dvernet@meta.com>
(cherry picked from commit df26838)
Signed-off-by: LFRon <ronforever@outlook.com>
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
…ork()

mainline inclusion
from mainline-v6.12-rc1
category: feature

A new BPF extensible sched_class will need more control over the forking
process. It wants to be able to fail from sched_cgroup_fork() after the new
task's sched_task_group is initialized so that the loaded BPF program can
prepare the task with its cgroup association is established and reject fork
if e.g. allocation fails.

Allow sched_cgroup_fork() to fail by making it return int instead of void
and adding sched_cancel_fork() to undo sched_fork() in the error path.

sched_cgroup_fork() doesn't fail yet and this patch shouldn't cause any
behavior changes.

v2: Patch description updated to detail the expected use.

Signed-off-by: Tejun Heo <tj@kernel.org>
Reviewed-by: David Vernet <dvernet@meta.com>
Acked-by: Josh Don <joshdon@google.com>
Acked-by: Hao Luo <haoluo@google.com>
Acked-by: Barret Rhoden <brho@google.com>
(cherry picked from commit 304b3f2)
Signed-off-by: LFRon <ronforever@outlook.com>
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion
from mainline-v6.12-rc1
category: feature

Currently, during a task weight change, sched core directly calls
reweight_task() defined in fair.c if @p is on CFS. Let's make it a proper
sched_class operation instead. CFS's reweight_task() is renamed to
reweight_task_fair() and now called through sched_class.

While it turns a direct call into an indirect one, set_load_weight() isn't
called from a hot path and this change shouldn't cause any noticeable
difference. This will be used to implement reweight_task for a new BPF
extensible sched_class so that it can keep its cached task weight
up-to-date.

Signed-off-by: Tejun Heo <tj@kernel.org>
Reviewed-by: David Vernet <dvernet@meta.com>
Acked-by: Josh Don <joshdon@google.com>
Acked-by: Hao Luo <haoluo@google.com>
Acked-by: Barret Rhoden <brho@google.com>
Conflicts:
	kernel/sched/core.c
	kernel/sched/fair.c
	kernel/sched/sched.h
(cherry picked from commit e83edbf)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
…g/changed()

mainline inclusion
from mainline-v6.12-rc1
category: feature

When a task switches to a new sched_class, the prev and new classes are
notified through ->switched_from() and ->switched_to(), respectively, after
the switching is done.

A new BPF extensible sched_class will have callbacks that allow the BPF
scheduler to keep track of relevant task states (like priority and cpumask).
Those callbacks aren't called while a task is on a different sched_class.
When a task comes back, we wanna tell the BPF progs the up-to-date state
before the task gets enqueued, so we need a hook which is called before the
switching is committed.

This patch adds ->switching_to() which is called during sched_class switch
through check_class_changing() before the task is restored. Also, this patch
exposes check_class_changing/changed() in kernel/sched/sched.h. They will be
used by the new BPF extensible sched_class to implement implicit sched_class
switching which is used e.g. when falling back to CFS when the BPF scheduler
fails or unloads.

This is a prep patch and doesn't cause any behavior changes. The new
operation and exposed functions aren't used yet.

v3: Refreshed on top of tip:sched/core.

v2: Improve patch description w/ details on planned use.

Signed-off-by: Tejun Heo <tj@kernel.org>
Reviewed-by: David Vernet <dvernet@meta.com>
Acked-by: Josh Don <joshdon@google.com>
Acked-by: Hao Luo <haoluo@google.com>
Acked-by: Barret Rhoden <brho@google.com>
Conflicts:
	kernel/sched/core.c
(cherry picked from commit d8c7bc2)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion
from mainline-v6.12-rc1
category: feature

Factor out sched_weight_from/to_cgroup() which convert between scheduler
shares and cgroup weight. No functional change. The factored out functions
will be used by a new BPF extensible sched_class so that the weights can be
exposed to the BPF programs in a way which is consistent cgroup weights and
easier to interpret.

The weight conversions will be used regardless of cgroup usage. It's just
borrowing the cgroup weight range as it's more intuitive.
CGROUP_WEIGHT_MIN/DFL/MAX constants are moved outside CONFIG_CGROUPS so that
the conversion helpers can always be defined.

v2: The helpers are now defined regardless of COFNIG_CGROUPS.

Signed-off-by: Tejun Heo <tj@kernel.org>
Reviewed-by: David Vernet <dvernet@meta.com>
Acked-by: Josh Don <joshdon@google.com>
Acked-by: Hao Luo <haoluo@google.com>
Acked-by: Barret Rhoden <brho@google.com>
(cherry picked from commit 4f9c7ca)
Signed-off-by: LFRon <ronforever@outlook.com>
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
…rs()

mainline inclusion
from mainline-v6.12-rc1
category: feature

RT, DL, thermal and irq load and utilization metrics need to be decayed and
updated periodically and before consumption to keep the numbers reasonable.
This is currently done from __update_blocked_others() as a part of the fair
class load balance path. Let's factor it out to update_other_load_avgs().
Pure refactor. No functional changes.

This will be used by the new BPF extensible scheduling class to ensure that
the above metrics are properly maintained.

v2: Refreshed on top of tip:sched/core.

Signed-off-by: Tejun Heo <tj@kernel.org>
Reviewed-by: David Vernet <dvernet@meta.com>
Conflicts:
	kernel/sched/fair.c
(cherry picked from commit 96fd6c6)
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
A new BPF extensible sched_class will need to dynamically change how a task
picks its sched_class. For example, if the loaded BPF scheduler progs fail,
the tasks will be forced back on CFS even if the task's policy is set to the
new sched_class. To support such mapping, add normal_policy() which wraps
testing for %SCHED_NORMAL. This doesn't cause any behavior changes.

v2: Update the description with more details on the expected use.

Signed-off-by: Tejun Heo <tj@kernel.org>
Reviewed-by: David Vernet <dvernet@meta.com>
Acked-by: Josh Don <joshdon@google.com>
Acked-by: Hao Luo <haoluo@google.com>
Acked-by: Barret Rhoden <brho@google.com>
(cherry picked from commit 2c8d046)
Conflicts:
	kernel/sched/fair.c
Signed-off-by: LFRon <ronforever@outlook.com>
Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
@sourcery-ai

sourcery-ai Bot commented Oct 22, 2026

Copy link
Copy Markdown

Sorry @opsiff, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 5 hours and 51 minutes by commenting @sourcery-ai review. Upgrade to get a review now.

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please ask for approval from opsiff. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sourcery-ai

sourcery-ai Bot commented Oct 22, 2026

Copy link
Copy Markdown

Reviewer's Guide

Updates the 6.6 scheduler and cgroup interfaces for sched_ext by adding scheduling-class lifecycle hooks, making fork setup cancellable, hardening cgroup iteration locking, and refactoring load-average and cgroup-weight handling into shared scheduler helpers.

Sequence diagram for cancellable scheduler fork setup

sequenceDiagram
    participant Fork as copy_process
    participant Scheduler as sched_cgroup_fork
    participant Cgroup as cgroup_cancel_fork
    participant Cancel as sched_cancel_fork

    Fork->>Scheduler: sched_cgroup_fork(p, args)
    alt scheduler fork setup fails
        Scheduler-->>Fork: return error
        Fork->>Cgroup: cgroup_cancel_fork(p, args)
    else later fork failure
        Scheduler-->>Fork: return 0
        Fork->>Cgroup: cgroup_cancel_fork(p, args)
    end
    Fork->>Cancel: sched_cancel_fork(p)
Loading

Flow diagram for scheduler class transition handling

flowchart TD
    A[Scheduler changes task class] --> B[check_class_changing]
    B --> C{Previous class differs?}
    C -- No --> D[Continue scheduling update]
    C -- Yes --> E{New class has switching_to?}
    E -- No --> D
    E -- Yes --> F[Call switching_to]
    F --> D
Loading

File-Level Changes

Change Details Files
Adds scheduler extension hooks and fork cancellation handling so scheduling classes can participate in task setup, class transitions, and task reweighting.
  • Make sched_cgroup_fork failure-aware and add sched_cancel_fork cleanup.
  • Invoke a class hook when a task changes scheduling class.
  • Move task reweighting behind sched_class->reweight_task and register the fair-class implementation.
  • Relax scheduler class ordering checks from contiguous layout to relative ordering.
include/linux/sched/task.h
kernel/fork.c
kernel/sched/core.c
kernel/sched/fair.c
kernel/sched/sched.h
kernel/sched/syscalls.c
Hardens cgroup task iteration against interrupt-context locking issues.
  • Replace irq-disabling spin-lock operations with irqsave/irqrestore around iterator state transitions.
  • Represent iterator flags as an enum and scope their definitions under CONFIG_CGROUPS.
include/linux/cgroup.h
kernel/cgroup/cgroup.c
Centralizes scheduler load and cgroup weight conversions while separating non-fair load-average updates.
  • Add shared conversion helpers for cgroup weights and use them for CPU weight reads and writes.
  • Extract RT, deadline, thermal, and IRQ load-average updates into update_other_load_avgs().
  • Add normal_policy() and use it to restrict fair wakeup preemption to normal tasks.
kernel/sched/core.c
kernel/sched/fair.c
kernel/sched/sched.h
kernel/sched/syscalls.c

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new scheduler-class callbacks bypass existing reserved slots and break the preserved binary layout on KABI-enabled builds.

1 open issue
What changed in this PR

This PR prepares the scheduler and cgroup interfaces for sched_ext integration, adding lifecycle hooks and shared scheduler helpers.

Changes:

  • Add scheduler-class reweighting and transition callbacks, plus fork cancellation and error-handling paths.
  • Centralize non-fair load-average updates and cgroup weight conversions.
  • Preserve interrupt state during cgroup task iteration.
File Description
kernel/​sched/​syscalls.c Add shared load-average updates and invoke the class-transition hook.
kernel/​sched/​sched.h Define scheduler callbacks, policy helpers, and weight conversions.
kernel/​sched/​fair.c Register fair-class reweighting and use shared helpers.
kernel/​sched/​core.c Wire scheduler hooks, fork interfaces, and weight helpers.
kernel/​fork.c Handle scheduler fork errors and cancellation cleanup.
kernel/​cgroup/​cgroup.c Preserve interrupt state around iterator locks.
include/​linux/​sched/​task.h Declare updated scheduler fork interfaces.
include/​linux/​cgroup.h Expose weight constants unconditionally and convert iterator flags to an enum.

Review effort: Balanced

Comment thread kernel/sched/sched.h
Comment on lines +2394 to +2395
void (*reweight_task)(struct rq *this_rq, struct task_struct *task,
const struct load_weight *lw);
@opsiff
opsiff merged commit 30f9374 into deepin-community:linux-6.6.y Oct 22, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants