1. 02 Jul, 2024 1 commit
  2. 01 Jul, 2024 1 commit
  3. 27 Jun, 2024 3 commits
  4. 25 Jun, 2024 1 commit
    • Tejun Heo's avatar
      sched_ext: Drop tools_clean target from the top-level Makefile · eb4a3b62
      Tejun Heo authored
      2a52ca7c ("sched_ext: Add scx_simple and scx_example_qmap example
      schedulers") added the tools_clean target which is triggered by mrproper.
      The tools_clean target triggers the sched_ext_clean target in tools/. This
      unfortunately makes mrproper fail when no BTF enabled kernel image is found:
      
        Makefile:83: *** Cannot find a vmlinux for VMLINUX_BTF at any of "  ../../vmlinux /sys/kernel/btf/vmlinux/boot/vmlinux-4.15.0-136-generic".  Stop.
        Makefile:192: recipe for target 'sched_ext_clean' failed
        make[2]: *** [sched_ext_clean] Error 2
        Makefile:1361: recipe for target 'sched_ext' failed
        make[1]: *** [sched_ext] Error 2
        Makefile:240: recipe for target '__sub-make' failed
        make: *** [__sub-make] Error 2
      
      Clean targets shouldn't fail like this but also it's really odd for mrproper
      to single out and trigger the sched_ext_clean target when no other clean
      targets under tools/ are triggered.
      
      Fix builds by dropping the tools_clean target from the top-level Makefile.
      The offending Makefile line is shared across BPF targets under tools/. Let's
      revisit them later.
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Reported-by: default avatarJon Hunter <jonathanh@nvidia.com>
      Link: http://lkml.kernel.org/r/ac065f1f-8754-4626-95db-2c9fcf02567b@nvidia.com
      Fixes: 2a52ca7c ("sched_ext: Add scx_simple and scx_example_qmap example schedulers")
      Cc: David Vernet <void@manifault.com>
      eb4a3b62
  5. 23 Jun, 2024 1 commit
    • David Vernet's avatar
      sched_ext: Make scx_bpf_cpuperf_set() @cpu arg signed · 8a6c6b4b
      David Vernet authored
      The scx_bpf_cpuperf_set() kfunc allows a BPF program to set the relative
      performance target of a specified CPU. Commit d86adb4f ("sched_ext: Add
      cpuperf support") defined the @cpu argument to be unsigned. Let's update it
      to be signed to match the norm for the rest of ext.c and the kernel.
      
      Note that the kfunc declaration of scx_bpf_cpuperf_set() in the
      common.bpf.h header in tools/sched_ext already listed the cpu as signed, so
      this also fixes the build for tools/sched_ext and the sched_ext selftests
      due to kfunc declarations now being emitted in vmlinux.h based on BTF (thus
      causing the compiler to error due to observing conflicting types).
      
      Fixes: d86adb4f ("sched_ext: Add cpuperf support")
      Signed-off-by: default avatarDavid Vernet <void@manifault.com>
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      8a6c6b4b
  6. 21 Jun, 2024 3 commits
    • Tejun Heo's avatar
      sched_ext: Add cpuperf support · d86adb4f
      Tejun Heo authored
      sched_ext currently does not integrate with schedutil. When schedutil is the
      governor, frequencies are left unregulated and usually get stuck close to
      the highest performance level from running RT tasks.
      
      Add CPU performance monitoring and scaling support by integrating into
      schedutil. The following kfuncs are added:
      
      - scx_bpf_cpuperf_cap(): Query the relative performance capacity of
        different CPUs in the system.
      
      - scx_bpf_cpuperf_cur(): Query the current performance level of a CPU
        relative to its max performance.
      
      - scx_bpf_cpuperf_set(): Set the current target performance level of a CPU.
      
      This gives direct control over CPU performance setting to the BPF scheduler.
      The only changes on the schedutil side are accounting for the utilization
      factor from sched_ext and disabling frequency holding heuristics as it may
      not apply well to sched_ext schedulers which may have a lot weaker
      connection between tasks and their current / last CPU.
      
      With cpuperf support added, there is no reason to block uclamp. Enable while
      at it.
      
      A toy implementation of cpuperf is added to scx_qmap as a demonstration of
      the feature.
      
      v2: Ignore cpu_util_cfs_boost() when scx_switched_all() in sugov_get_util()
          to avoid factoring in stale util metric. (Christian)
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      Cc: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
      Cc: Viresh Kumar <viresh.kumar@linaro.org>
      Cc: Christian Loehle <christian.loehle@arm.com>
      d86adb4f
    • Tejun Heo's avatar
      cpufreq_schedutil: Refactor sugov_cpu_is_busy() · 8988cad8
      Tejun Heo authored
      sugov_cpu_is_busy() is used to avoid decreasing performance level while the
      CPU is busy and called by sugov_update_single_freq() and
      sugov_update_single_perf(). Both callers repeat the same pattern to first
      test for uclamp and then the business. Let's refactor so that the tests
      aren't repeated.
      
      The new helper is named sugov_hold_freq() and tests both the uclamp
      exception and CPU business. No functional changes. This will make adding
      more exception conditions easier.
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      Reviewed-by: default avatarChristian Loehle <christian.loehle@arm.com>
      Acked-by: default avatarRafael J. Wysocki <rafael.j.wysocki@intel.com>
      Cc: Viresh Kumar <viresh.kumar@linaro.org>
      8988cad8
    • Tejun Heo's avatar
      sched, sched_ext: Replace scx_next_task_picked() with sched_class->switch_class() · b999e365
      Tejun Heo authored
      scx_next_task_picked() is used by sched_ext to notify the BPF scheduler when
      a CPU is taken away by a task dispatched from a higher priority sched_class
      so that the BPF scheduler can, e.g., punt the task[s] which was running or
      were waiting for the CPU to other CPUs.
      
      Replace the sched_ext specific hook scx_next_task_picked() with a new
      sched_class operation switch_class().
      
      The changes are straightforward and the code looks better afterwards.
      However, when !CONFIG_SCHED_CLASS_EXT, this ends up adding an unused hook
      which is unlikely to be useful to other sched_classes. For further
      discussion on this subject, please refer to the following:
      
        http://lkml.kernel.org/r/CAHk-=wjFPLqo7AXu8maAGEGnOy6reUg-F4zzFhVB0Kyu22h7pw@mail.gmail.comSigned-off-by: default avatarTejun Heo <tj@kernel.org>
      Suggested-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      Cc: Ingo Molnar <mingo@redhat.com>
      Cc: Peter Zijlstra <peterz@infradead.org>
      Cc: Thomas Gleixner <tglx@linutronix.de>
      b999e365
  7. 18 Jun, 2024 30 commits
    • David Vernet's avatar
      sched_ext: Add selftests · a5db7817
      David Vernet authored
      Add basic selftests.
      Signed-off-by: default avatarDavid Vernet <dvernet@meta.com>
      Acked-by: default avatarTejun Heo <tj@kernel.org>
      a5db7817
    • Tejun Heo's avatar
      sched_ext: Documentation: scheduler: Document extensible scheduler class · fa48e8d2
      Tejun Heo authored
      Add Documentation/scheduler/sched-ext.rst which gives a high-level overview
      and pointers to the examples.
      
      v6: - Add paragraph explaining debug dump.
      
      v5: - Updated to reflect /sys/kernel interface change. Kconfig options
            added.
      
      v4: - README improved, reformatted in markdown and renamed to README.md.
      
      v3: - Added tools/sched_ext/README.
      
          - Dropped _example prefix from scheduler names.
      
      v2: - Apply minor edits suggested by Bagas. Caveats section dropped as all
            of them are addressed.
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      Acked-by: default avatarJosh Don <joshdon@google.com>
      Acked-by: default avatarHao Luo <haoluo@google.com>
      Acked-by: default avatarBarret Rhoden <brho@google.com>
      Cc: Bagas Sanjaya <bagasdotme@gmail.com>
      fa48e8d2
    • Tejun Heo's avatar
      sched_ext: Add vtime-ordered priority queue to dispatch_q's · 06e51be3
      Tejun Heo authored
      Currently, a dsq is always a FIFO. A task which is dispatched earlier gets
      consumed or executed earlier. While this is sufficient when dsq's are used
      for simple staging areas for tasks which are ready to execute, it'd make
      dsq's a lot more useful if they can implement custom ordering.
      
      This patch adds a vtime-ordered priority queue to dsq's. When the BPF
      scheduler dispatches a task with the new scx_bpf_dispatch_vtime() helper, it
      can specify the vtime tha the task should be inserted at and the task is
      inserted into the priority queue in the dsq which is ordered according to
      time_before64() comparison of the vtime values.
      
      A DSQ can either be a FIFO or priority queue and automatically switches
      between the two depending on whether scx_bpf_dispatch() or
      scx_bpf_dispatch_vtime() is used. Using the wrong variant while the DSQ
      already has the other type queued is not allowed and triggers an ops error.
      Built-in DSQs must always be FIFOs.
      
      This makes it very easy for the BPF schedulers to implement proper vtime
      based scheduling within each dsq very easy and efficient at a negligible
      cost in terms of code complexity and overhead.
      
      scx_simple and scx_example_flatcg are updated to default to weighted
      vtime scheduling (the latter within each cgroup). FIFO scheduling can be
      selected with -f option.
      
      v4: - As allowing mixing priority queue and FIFO on the same DSQ sometimes
            led to unexpected starvations, DSQs now error out if both modes are
            used at the same time and the built-in DSQs are no longer allowed to
            be priority queues.
      
          - Explicit type struct scx_dsq_node added to contain fields needed to be
            linked on DSQs. This will be used to implement stateful iterator.
      
          - Tasks are now always linked on dsq->list whether the DSQ is in FIFO or
            PRIQ mode. This confines PRIQ related complexities to the enqueue and
            dequeue paths. Other paths only need to look at dsq->list. This will
            also ease implementing BPF iterator.
      
          - Print p->scx.dsq_flags in debug dump.
      
      v3: - SCX_TASK_DSQ_ON_PRIQ flag is moved from p->scx.flags into its own
            p->scx.dsq_flags. The flag is protected with the dsq lock unlike other
            flags in p->scx.flags. This led to flag corruption in some cases.
      
          - Add comments explaining the interaction between using consumption of
            p->scx.slice to determine vtime progress and yielding.
      
      v2: - p->scx.dsq_vtime was not initialized on load or across cgroup
            migrations leading to some tasks being stalled for extended period of
            time depending on how saturated the machine is. Fixed.
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      06e51be3
    • Tejun Heo's avatar
      sched_ext: Implement core-sched support · 7b0888b7
      Tejun Heo authored
      The core-sched support is composed of the following parts:
      
      - task_struct->scx.core_sched_at is added. This is a timestamp which can be
        used to order tasks. Depending on whether the BPF scheduler implements
        custom ordering, it tracks either global FIFO ordering of all tasks or
        local-DSQ ordering within the dispatched tasks on a CPU.
      
      - prio_less() is updated to call scx_prio_less() when comparing SCX tasks.
        scx_prio_less() calls ops.core_sched_before() if available or uses the
        core_sched_at timestamp. For global FIFO ordering, the BPF scheduler
        doesn't need to do anything. Otherwise, it should implement
        ops.core_sched_before() which reflects the ordering.
      
      - When core-sched is enabled, balance_scx() balances all SMT siblings so
        that they all have tasks dispatched if necessary before pick_task_scx() is
        called. pick_task_scx() picks between the current task and the first
        dispatched task on the local DSQ based on availability and the
        core_sched_at timestamps. Note that FIFO ordering is expected among the
        already dispatched tasks whether running or on the local DSQ, so this path
        always compares core_sched_at instead of calling into
        ops.core_sched_before().
      
      qmap_core_sched_before() is added to scx_qmap. It scales the
      distances from the heads of the queues to compare the tasks across different
      priority queues and seems to behave as expected.
      
      v3: Fixed build error when !CONFIG_SCHED_SMT reported by Andrea Righi.
      
      v2: Sched core added the const qualifiers to prio_less task arguments.
          Explicitly drop them for ops.core_sched_before() task arguments. BPF
          enforces access control through the verifier, so the qualifier isn't
          actually operative and only gets in the way when interacting with
          various helpers.
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      Reviewed-by: default avatarJosh Don <joshdon@google.com>
      Cc: Andrea Righi <andrea.righi@canonical.com>
      7b0888b7
    • Tejun Heo's avatar
      sched_ext: Bypass BPF scheduler while PM events are in progress · 0fd55582
      Tejun Heo authored
      PM operations freeze userspace. Some BPF schedulers have active userspace
      component and may misbehave as expected across PM events. While the system
      is frozen, nothing too interesting is happening in terms of scheduling and
      we can get by just fine with the fallback FIFO behavior. Let's make things
      easier by always bypassing the BPF scheduler while PM events are in
      progress.
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      0fd55582
    • Tejun Heo's avatar
      sched_ext: Implement sched_ext_ops.cpu_online/offline() · 60c27fb5
      Tejun Heo authored
      Add ops.cpu_online/offline() which are invoked when CPUs come online and
      offline respectively. As the enqueue path already automatically bypasses
      tasks to the local dsq on a deactivated CPU, BPF schedulers are guaranteed
      to see tasks only on CPUs which are between online() and offline().
      
      If the BPF scheduler doesn't implement ops.cpu_online/offline(), the
      scheduler is automatically exited with SCX_ECODE_RESTART |
      SCX_ECODE_RSN_HOTPLUG. Userspace can implement CPU hotpplug support
      trivially by simply reinitializing and reloading the scheduler.
      
      scx_qmap is updated to print out online CPUs on hotplug events. Other
      schedulers are updated to restart based on ecode.
      
      v3: - The previous implementation added @reason to
            sched_class.rq_on/offline() to distinguish between CPU hotplug events
            and topology updates. This was buggy and fragile as the methods are
            skipped if the current state equals the target state. Instead, add
            scx_rq_[de]activate() which are directly called from
            sched_cpu_de/activate(). This also allows ops.cpu_on/offline() to
            sleep which can be useful.
      
          - ops.dispatch() could be called on a CPU that the BPF scheduler was
            told to be offline. The dispatch patch is updated to bypass in such
            cases.
      
      v2: - To accommodate lock ordering change between scx_cgroup_rwsem and
            cpus_read_lock(), CPU hotplug operations are put into its own SCX_OPI
            block and enabled eariler during scx_ope_enable() so that
            cpus_read_lock() can be dropped before acquiring scx_cgroup_rwsem.
      
          - Auto exit with ECODE added.
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      Acked-by: default avatarJosh Don <joshdon@google.com>
      Acked-by: default avatarHao Luo <haoluo@google.com>
      Acked-by: default avatarBarret Rhoden <brho@google.com>
      60c27fb5
    • David Vernet's avatar
      sched_ext: Implement sched_ext_ops.cpu_acquire/release() · 245254f7
      David Vernet authored
      Scheduler classes are strictly ordered and when a higher priority class has
      tasks to run, the lower priority ones lose access to the CPU. Being able to
      monitor and act on these events are necessary for use cases includling
      strict core-scheduling and latency management.
      
      This patch adds two operations ops.cpu_acquire() and .cpu_release(). The
      former is invoked when a CPU becomes available to the BPF scheduler and the
      opposite for the latter. This patch also implements
      scx_bpf_reenqueue_local() which can be called from .cpu_release() to trigger
      requeueing of all tasks in the local dsq of the CPU so that the tasks can be
      reassigned to other available CPUs.
      
      scx_pair is updated to use .cpu_acquire/release() along with
      %SCX_KICK_WAIT to make the pair scheduling guarantee strict even when a CPU
      is preempted by a higher priority scheduler class.
      
      scx_qmap is updated to use .cpu_acquire/release() to empty the local
      dsq of a preempted CPU. A similar approach can be adopted by BPF schedulers
      that want to have a tight control over latency.
      
      v4: Use the new SCX_KICK_IDLE to wake up a CPU after re-enqueueing.
      
      v3: Drop the const qualifier from scx_cpu_release_args.task. BPF enforces
          access control through the verifier, so the qualifier isn't actually
          operative and only gets in the way when interacting with various
          helpers.
      
      v2: Add p->scx.kf_mask annotation to allow calling scx_bpf_reenqueue_local()
          from ops.cpu_release() nested inside ops.init() and other sleepable
          operations.
      Signed-off-by: default avatarDavid Vernet <dvernet@meta.com>
      Reviewed-by: default avatarTejun Heo <tj@kernel.org>
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Acked-by: default avatarJosh Don <joshdon@google.com>
      Acked-by: default avatarHao Luo <haoluo@google.com>
      Acked-by: default avatarBarret Rhoden <brho@google.com>
      245254f7
    • David Vernet's avatar
      sched_ext: Implement SCX_KICK_WAIT · 90e55164
      David Vernet authored
      If set when calling scx_bpf_kick_cpu(), the invoking CPU will busy wait for
      the kicked cpu to enter the scheduler. See the following for example usage:
      
        https://github.com/sched-ext/scx/blob/main/scheds/c/scx_pair.bpf.c
      
      v2: - Updated to fit the updated kick_cpus_irq_workfn() implementation.
      
          - Include SCX_KICK_WAIT related information in debug dump.
      Signed-off-by: default avatarDavid Vernet <dvernet@meta.com>
      Reviewed-by: default avatarTejun Heo <tj@kernel.org>
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Acked-by: default avatarJosh Don <joshdon@google.com>
      Acked-by: default avatarHao Luo <haoluo@google.com>
      Acked-by: default avatarBarret Rhoden <brho@google.com>
      90e55164
    • Tejun Heo's avatar
      sched_ext: Track tasks that are subjects of the in-flight SCX operation · 36454023
      Tejun Heo authored
      When some SCX operations are in flight, it is known that the subject task's
      rq lock is held throughout which makes it safe to access certain fields of
      the task - e.g. its current task_group. We want to add SCX kfunc helpers
      that can make use of this guarantee - e.g. to help determining the currently
      associated CPU cgroup from the task's current task_group.
      
      As it'd be dangerous call such a helper on a task which isn't rq lock
      protected, the helper should be able to verify the input task and reject
      accordingly. This patch adds sched_ext_entity.kf_tasks[] that track the
      tasks which are currently being operated on by a terminal SCX operation. The
      new SCX_CALL_OP_[2]TASK[_RET]() can be used when invoking SCX operations
      which take tasks as arguments and the scx_kf_allowed_on_arg_tasks() can be
      used by kfunc helpers to verify the input task status.
      
      Note that as sched_ext_entity.kf_tasks[] can't handle nesting, the tracking
      is currently only limited to terminal SCX operations. If needed in the
      future, this restriction can be removed by moving the tracking to the task
      side with a couple per-task counters.
      
      v2: Updated to reflect the addition of SCX_KF_SELECT_CPU.
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      36454023
    • Tejun Heo's avatar
      sched_ext: Implement tickless support · 22a92020
      Tejun Heo authored
      Allow BPF schedulers to indicate tickless operation by setting p->scx.slice
      to SCX_SLICE_INF. A CPU whose current task has infinte slice goes into
      tickless operation.
      
      scx_central is updated to use tickless operations for all tasks and
      instead use a BPF timer to expire slices. This also uses the SCX_ENQ_PREEMPT
      and task state tracking added by the previous patches.
      
      Currently, there is no way to pin the timer on the central CPU, so it may
      end up on one of the worker CPUs; however, outside of that, the worker CPUs
      can go tickless both while running sched_ext tasks and idling.
      
      With schbench running, scx_central shows:
      
        root@test ~# grep ^LOC /proc/interrupts; sleep 10; grep ^LOC /proc/interrupts
        LOC:     142024        656        664        449   Local timer interrupts
        LOC:     161663        663        665        449   Local timer interrupts
      
      Without it:
      
        root@test ~ [SIGINT]# grep ^LOC /proc/interrupts; sleep 10; grep ^LOC /proc/interrupts
        LOC:     188778       3142       3793       3993   Local timer interrupts
        LOC:     198993       5314       6323       6438   Local timer interrupts
      
      While scx_central itself is too barebone to be useful as a
      production scheduler, a more featureful central scheduler can be built using
      the same approach. Google's experience shows that such an approach can have
      significant benefits for certain applications such as VM hosting.
      
      v4: Allow operation even if BPF_F_TIMER_CPU_PIN is not available.
      
      v3: Pin the central scheduler's timer on the central_cpu using
          BPF_F_TIMER_CPU_PIN.
      
      v2: Convert to BPF inline iterators.
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      Acked-by: default avatarJosh Don <joshdon@google.com>
      Acked-by: default avatarHao Luo <haoluo@google.com>
      Acked-by: default avatarBarret Rhoden <brho@google.com>
      22a92020
    • Tejun Heo's avatar
      sched_ext: Add task state tracking operations · 1c29f854
      Tejun Heo authored
      Being able to track the task runnable and running state transitions are
      useful for a variety of purposes including latency tracking and load factor
      calculation.
      
      Currently, BPF schedulers don't have a good way of tracking these
      transitions. Becoming runnable can be determined from ops.enqueue() but
      becoming quiescent can only be inferred from the lack of subsequent enqueue.
      Also, as the local dsq can have multiple tasks and some events are handled
      in the sched_ext core, it's difficult to determine when a given task starts
      and stops executing.
      
      This patch adds sched_ext_ops.runnable(), .running(), .stopping() and
      .quiescent() operations to track the task runnable and running state
      transitions. They're mostly self explanatory; however, we want to ensure
      that running <-> stopping transitions are always contained within runnable
      <-> quiescent transitions which is a bit different from how the scheduler
      core behaves. This adds a bit of complication. See the comment in
      dequeue_task_scx().
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      Acked-by: default avatarJosh Don <joshdon@google.com>
      Acked-by: default avatarHao Luo <haoluo@google.com>
      Acked-by: default avatarBarret Rhoden <brho@google.com>
      1c29f854
    • Tejun Heo's avatar
      sched_ext: Make watchdog handle ops.dispatch() looping stall · 0922f54f
      Tejun Heo authored
      The dispatch path retries if the local DSQ is still empty after
      ops.dispatch() either dispatched or consumed a task. This is both out of
      necessity and for convenience. It has to retry because the dispatch path
      might lose the tasks to dequeue while the rq lock is released while trying
      to migrate tasks across CPUs, and the retry mechanism makes ops.dispatch()
      implementation easier as it only needs to make some forward progress each
      iteration.
      
      However, this makes it possible for ops.dispatch() to stall CPUs by
      repeatedly dispatching ineligible tasks. If all CPUs are stalled that way,
      the watchdog or sysrq handler can't run and the system can't be saved. Let's
      address the issue by breaking out of the dispatch loop after 32 iterations.
      
      It is unlikely but not impossible for ops.dispatch() to legitimately go over
      the iteration limit. We want to come back to the dispatch path in such cases
      as not doing so risks stalling the CPU by idling with runnable tasks
      pending. As the previous task is still current in balance_scx(),
      resched_curr() doesn't do anything - it will just get cleared. Let's instead
      use scx_kick_bpf() which will trigger reschedule after switching to the next
      task which will likely be the idle task.
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      0922f54f
    • Tejun Heo's avatar
      sched_ext: Add a central scheduler which makes all scheduling decisions on one CPU · 037df2a3
      Tejun Heo authored
      This patch adds a new example scheduler, scx_central, which demonstrates
      central scheduling where one CPU is responsible for making all scheduling
      decisions in the system using scx_bpf_kick_cpu(). The central CPU makes
      scheduling decisions for all CPUs in the system, queues tasks on the
      appropriate local dsq's and preempts the worker CPUs. The worker CPUs in
      turn preempt the central CPU when it needs tasks to run.
      
      Currently, every CPU depends on its own tick to expire the current task. A
      follow-up patch implementing tickless support for sched_ext will allow the
      worker CPUs to go full tickless so that they can run completely undisturbed.
      
      v3: - Kumar fixed a bug where the dispatch path could overflow the dispatch
            buffer if too many are dispatched to the fallback DSQ.
      
          - Use the new SCX_KICK_IDLE to wake up non-central CPUs.
      
          - Dropped '-p' option.
      
      v2: - Use RESIZABLE_ARRAY() instead of fixed MAX_CPUS and use SCX_BUG[_ON]()
            to simplify error handling.
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      Acked-by: default avatarJosh Don <joshdon@google.com>
      Acked-by: default avatarHao Luo <haoluo@google.com>
      Acked-by: default avatarBarret Rhoden <brho@google.com>
      Cc: Kumar Kartikeya Dwivedi <memxor@gmail.com>
      Cc: Julia Lawall <julia.lawall@inria.fr>
      037df2a3
    • Tejun Heo's avatar
      sched_ext: Implement scx_bpf_kick_cpu() and task preemption support · 81aae789
      Tejun Heo authored
      It's often useful to wake up and/or trigger reschedule on other CPUs. This
      patch adds scx_bpf_kick_cpu() kfunc helper that BPF scheduler can call to
      kick the target CPU into the scheduling path.
      
      As a sched_ext task relinquishes its CPU only after its slice is depleted,
      this patch also adds SCX_KICK_PREEMPT and SCX_ENQ_PREEMPT which clears the
      slice of the target CPU's current task to guarantee that sched_ext's
      scheduling path runs on the CPU.
      
      If SCX_KICK_IDLE is specified, the target CPU is kicked iff the CPU is idle
      to guarantee that the target CPU will go through at least one full sched_ext
      scheduling cycle after the kicking. This can be used to wake up idle CPUs
      without incurring unnecessary overhead if it isn't currently idle.
      
      As a demonstration of how backward compatibility can be supported using BPF
      CO-RE, tools/sched_ext/include/scx/compat.bpf.h is added. It provides
      __COMPAT_scx_bpf_kick_cpu_IDLE() which uses SCX_KICK_IDLE if available or
      becomes a regular kicking otherwise. This allows schedulers to use the new
      SCX_KICK_IDLE while maintaining support for older kernels. The plan is to
      temporarily use compat helpers to ease API updates and drop them after a few
      kernel releases.
      
      v5: - SCX_KICK_IDLE added. Note that this also adds a compat mechanism for
            schedulers so that they can support kernels without SCX_KICK_IDLE.
            This is useful as a demonstration of how new feature flags can be
            added in a backward compatible way.
      
          - kick_cpus_irq_workfn() reimplemented so that it touches the pending
            cpumasks only as necessary to reduce kicking overhead on machines with
            a lot of CPUs.
      
          - tools/sched_ext/include/scx/compat.bpf.h added.
      
      v4: - Move example scheduler to its own patch.
      
      v3: - Make scx_example_central switch all tasks by default.
      
          - Convert to BPF inline iterators.
      
      v2: - Julia Lawall reported that scx_example_central can overflow the
            dispatch buffer and malfunction. As scheduling for other CPUs can't be
            handled by the automatic retry mechanism, fix by implementing an
            explicit overflow and retry handling.
      
          - Updated to use generic BPF cpumask helpers.
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      Acked-by: default avatarJosh Don <joshdon@google.com>
      Acked-by: default avatarHao Luo <haoluo@google.com>
      Acked-by: default avatarBarret Rhoden <brho@google.com>
      81aae789
    • Tejun Heo's avatar
      tools/sched_ext: Add scx_show_state.py · 1c3ae1cb
      Tejun Heo authored
      There are states which are interesting but don't quite fit the interface
      exposed under /sys/kernel/sched_ext. Add tools/scx_show_state.py to show
      them.
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      1c3ae1cb
    • Tejun Heo's avatar
      sched_ext: Print debug dump after an error exit · 07814a94
      Tejun Heo authored
      If a BPF scheduler triggers an error, the scheduler is aborted and the
      system is reverted to the built-in scheduler. In the process, a lot of
      information which may be useful for figuring out what happened can be lost.
      
      This patch adds debug dump which captures information which may be useful
      for debugging including runqueue and runnable thread states at the time of
      failure. The following shows a debug dump after triggering the watchdog:
      
        root@test ~# os/work/tools/sched_ext/build/bin/scx_qmap -t 100
        stats  : enq=1 dsp=0 delta=1 deq=0
        stats  : enq=90 dsp=90 delta=0 deq=0
        stats  : enq=156 dsp=156 delta=0 deq=0
        stats  : enq=218 dsp=218 delta=0 deq=0
        stats  : enq=255 dsp=255 delta=0 deq=0
        stats  : enq=271 dsp=271 delta=0 deq=0
        stats  : enq=284 dsp=284 delta=0 deq=0
        stats  : enq=293 dsp=293 delta=0 deq=0
      
        DEBUG DUMP
        ================================================================================
      
        kworker/u32:12[320] triggered exit kind 1026:
          runnable task stall (stress[1530] failed to run for 6.841s)
      
        Backtrace:
          scx_watchdog_workfn+0x136/0x1c0
          process_scheduled_works+0x2b5/0x600
          worker_thread+0x269/0x360
          kthread+0xeb/0x110
          ret_from_fork+0x36/0x40
          ret_from_fork_asm+0x1a/0x30
      
        QMAP FIFO[0]:
        QMAP FIFO[1]:
        QMAP FIFO[2]: 1436
        QMAP FIFO[3]:
        QMAP FIFO[4]:
      
        CPU states
        ----------
      
        CPU 0   : nr_run=1 ops_qseq=244
      	    curr=swapper/0[0] class=idle_sched_class
      
          QMAP: dsp_idx=1 dsp_cnt=0
      
          R stress[1530] -6841ms
      	scx_state/flags=3/0x1 ops_state/qseq=2/20
      	sticky/holding_cpu=-1/-1 dsq_id=(n/a)
      	cpus=ff
      
            QMAP: force_local=0
      
            asm_sysvec_apic_timer_interrupt+0x16/0x20
      
        CPU 2   : nr_run=2 ops_qseq=142
      	    curr=swapper/2[0] class=idle_sched_class
      
          QMAP: dsp_idx=1 dsp_cnt=0
      
          R sshd[1703] -5905ms
      	scx_state/flags=3/0x9 ops_state/qseq=2/88
      	sticky/holding_cpu=-1/-1 dsq_id=(n/a)
      	cpus=ff
      
            QMAP: force_local=1
      
            __x64_sys_ppoll+0xf6/0x120
            do_syscall_64+0x7b/0x150
            entry_SYSCALL_64_after_hwframe+0x76/0x7e
      
          R fish[1539] -4141ms
      	scx_state/flags=3/0x9 ops_state/qseq=2/124
      	sticky/holding_cpu=-1/-1 dsq_id=(n/a)
      	cpus=ff
      
            QMAP: force_local=1
      
            futex_wait+0x60/0xe0
            do_futex+0x109/0x180
            __x64_sys_futex+0x117/0x190
            do_syscall_64+0x7b/0x150
            entry_SYSCALL_64_after_hwframe+0x76/0x7e
      
        CPU 3   : nr_run=2 ops_qseq=162
      	    curr=kworker/u32:12[320] class=ext_sched_class
      
          QMAP: dsp_idx=1 dsp_cnt=0
      
         *R kworker/u32:12[320] +0ms
      	scx_state/flags=3/0xd ops_state/qseq=0/0
      	sticky/holding_cpu=-1/-1 dsq_id=(n/a)
      	cpus=ff
      
            QMAP: force_local=0
      
            scx_dump_state+0x613/0x6f0
            scx_ops_error_irq_workfn+0x1f/0x40
            irq_work_run_list+0x82/0xd0
            irq_work_run+0x14/0x30
            __sysvec_irq_work+0x40/0x140
            sysvec_irq_work+0x60/0x70
            asm_sysvec_irq_work+0x16/0x20
            scx_watchdog_workfn+0x15f/0x1c0
            process_scheduled_works+0x2b5/0x600
            worker_thread+0x269/0x360
            kthread+0xeb/0x110
            ret_from_fork+0x36/0x40
            ret_from_fork_asm+0x1a/0x30
      
          R kworker/3:2[1436] +0ms
      	scx_state/flags=3/0x9 ops_state/qseq=2/160
      	sticky/holding_cpu=-1/-1 dsq_id=(n/a)
      	cpus=08
      
            QMAP: force_local=0
      
            kthread+0xeb/0x110
            ret_from_fork+0x36/0x40
            ret_from_fork_asm+0x1a/0x30
      
        CPU 7   : nr_run=0 ops_qseq=76
      	    curr=swapper/7[0] class=idle_sched_class
      
      
        ================================================================================
      
        EXIT: runnable task stall (stress[1530] failed to run for 6.841s)
      
      It shows that CPU 3 was running the watchdog when it triggered the error
      condition and the scx_qmap thread has been queued on CPU 0 for over 5
      seconds but failed to run. It also prints out scx_qmap specific information
      - e.g. which tasks are queued on each FIFO and so on using the dump_*() ops.
      This dump has proved pretty useful for developing and debugging BPF
      schedulers.
      
      Debug dump is generated automatically when the BPF scheduler exits due to an
      error. The debug buffer used in such cases is determined by
      sched_ext_ops.exit_dump_len and defaults to 32k. If the debug dump overruns
      the available buffer, the output is truncated and marked accordingly.
      
      Debug dump output can also be read through the sched_ext_dump tracepoint.
      When read through the tracepoint, there is no length limit.
      
      SysRq-D can be used to trigger debug dump at any time while a BPF scheduler
      is loaded. This is non-destructive - the scheduler keeps running afterwards.
      The output can be read through the sched_ext_dump tracepoint.
      
      v2: - The size of exit debug dump buffer can now be customized using
            sched_ext_ops.exit_dump_len.
      
          - sched_ext_ops.dump*() added to enable dumping of BPF scheduler
            specific information.
      
          - Tracpoint output and SysRq-D triggering added.
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      07814a94
    • David Vernet's avatar
      sched_ext: Print sched_ext info when dumping stack · 1538e339
      David Vernet authored
      It would be useful to see what the sched_ext scheduler state is, and what
      scheduler is running, when we're dumping a task's stack. This patch
      therefore adds a new print_scx_info() function that's called in the same
      context as print_worker_info() and print_stop_info(). An example dump
      follows.
      
        BUG: kernel NULL pointer dereference, address: 0000000000000999
        #PF: supervisor write access in kernel mode
        #PF: error_code(0x0002) - not-present page
        PGD 0 P4D 0
        Oops: 0002 [#1] PREEMPT SMP
        CPU: 13 PID: 2047 Comm: insmod Tainted: G           O       6.6.0-work-10323-gb58d4cae8e99-dirty #34
        Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS unknown 2/2/2022
        Sched_ext: qmap (enabled+all), task: runnable_at=-17ms
        RIP: 0010:init_module+0x9/0x1000 [test_module]
        ...
      
      v3: - scx_ops_enable_state_str[] definition moved to an earlier patch as
            it's now used by core implementation.
      
          - Convert jiffy delta to msecs using jiffies_to_msecs() instead of
            multiplying by (HZ / MSEC_PER_SEC). The conversion is implemented in
            jiffies_delta_msecs().
      
      v2: - We are now using scx_ops_enable_state_str[] outside
            CONFIG_SCHED_DEBUG. Move it outside of CONFIG_SCHED_DEBUG and to the
            top. This was reported by Changwoo and Andrea.
      Signed-off-by: default avatarDavid Vernet <void@manifault.com>
      Reported-by: default avatarChangwoo Min <changwoo@igalia.com>
      Reported-by: default avatarAndrea Righi <andrea.righi@canonical.com>
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      1538e339
    • Tejun Heo's avatar
      sched_ext: Allow BPF schedulers to disallow specific tasks from joining SCHED_EXT · 7bb6f081
      Tejun Heo authored
      BPF schedulers might not want to schedule certain tasks - e.g. kernel
      threads. This patch adds p->scx.disallow which can be set by BPF schedulers
      in such cases. The field can be changed anytime and setting it in
      ops.prep_enable() guarantees that the task can never be scheduled by
      sched_ext.
      
      scx_qmap is updated with the -d option to disallow a specific PID:
      
        # echo $$
        1092
        # grep -E '(policy)|(ext\.enabled)' /proc/self/sched
        policy                                       :                    0
        ext.enabled                                  :                    0
        # ./set-scx 1092
        # grep -E '(policy)|(ext\.enabled)' /proc/self/sched
        policy                                       :                    7
        ext.enabled                                  :                    0
      
      Run "scx_qmap -p -d 1092" in another terminal.
      
        # cat /sys/kernel/sched_ext/nr_rejected
        1
        # grep -E '(policy)|(ext\.enabled)' /proc/self/sched
        policy                                       :                    0
        ext.enabled                                  :                    0
        # ./set-scx 1092
        setparam failed for 1092 (Permission denied)
      
      - v4: Refreshed on top of tip:sched/core.
      
      - v3: Update description to reflect /sys/kernel/sched_ext interface change.
      
      - v2: Use atomic_long_t instead of atomic64_t for scx_kick_cpus_pnt_seqs to
            accommodate 32bit archs.
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Suggested-by: default avatarBarret Rhoden <brho@google.com>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      Acked-by: default avatarJosh Don <joshdon@google.com>
      Acked-by: default avatarHao Luo <haoluo@google.com>
      Acked-by: default avatarBarret Rhoden <brho@google.com>
      7bb6f081
    • David Vernet's avatar
      sched_ext: Implement runnable task stall watchdog · 8a010b81
      David Vernet authored
      The most common and critical way that a BPF scheduler can misbehave is by
      failing to run runnable tasks for too long. This patch implements a
      watchdog.
      
      * All tasks record when they become runnable.
      
      * A watchdog work periodically scans all runnable tasks. If any task has
        stayed runnable for too long, the BPF scheduler is aborted.
      
      * scheduler_tick() monitors whether the watchdog itself is stuck. If so, the
        BPF scheduler is aborted.
      
      Because the watchdog only scans the tasks which are currently runnable and
      usually very infrequently, the overhead should be negligible.
      scx_qmap is updated so that it can be told to stall user and/or
      kernel tasks.
      
      A detected task stall looks like the following:
      
       sched_ext: BPF scheduler "qmap" errored, disabling
       sched_ext: runnable task stall (dbus-daemon[953] failed to run for 6.478s)
          scx_check_timeout_workfn+0x10e/0x1b0
          process_one_work+0x287/0x560
          worker_thread+0x234/0x420
          kthread+0xe9/0x100
          ret_from_fork+0x1f/0x30
      
      A detected watchdog stall:
      
       sched_ext: BPF scheduler "qmap" errored, disabling
       sched_ext: runnable task stall (watchdog failed to check in for 5.001s)
          scheduler_tick+0x2eb/0x340
          update_process_times+0x7a/0x90
          tick_sched_timer+0xd8/0x130
          __hrtimer_run_queues+0x178/0x3b0
          hrtimer_interrupt+0xfc/0x390
          __sysvec_apic_timer_interrupt+0xb7/0x2b0
          sysvec_apic_timer_interrupt+0x90/0xb0
          asm_sysvec_apic_timer_interrupt+0x1b/0x20
          default_idle+0x14/0x20
          arch_cpu_idle+0xf/0x20
          default_idle_call+0x50/0x90
          do_idle+0xe8/0x240
          cpu_startup_entry+0x1d/0x20
          kernel_init+0x0/0x190
          start_kernel+0x0/0x392
          start_kernel+0x324/0x392
          x86_64_start_reservations+0x2a/0x2c
          x86_64_start_kernel+0x104/0x109
          secondary_startup_64_no_verify+0xce/0xdb
      
      Note that this patch exposes scx_ops_error[_type]() in kernel/sched/ext.h to
      inline scx_notify_sched_tick().
      
      v4: - While disabling, cancel_delayed_work_sync(&scx_watchdog_work) was
            being called before forward progress was guaranteed and thus could
            lead to system lockup. Relocated.
      
          - While enabling, it was comparing msecs against jiffies without
            conversion leading to spurious load failures on lower HZ kernels.
            Fixed.
      
          - runnable list management is now used by core bypass logic and moved to
            the patch implementing sched_ext core.
      
      v3: - bpf_scx_init_member() was incorrectly comparing ops->timeout_ms
            against SCX_WATCHDOG_MAX_TIMEOUT which is in jiffies without
            conversion leading to spurious load failures in lower HZ kernels.
            Fixed.
      
      v2: - Julia Lawall noticed that the watchdog code was mixing msecs and
            jiffies. Fix by using jiffies for everything.
      Signed-off-by: default avatarDavid Vernet <dvernet@meta.com>
      Reviewed-by: default avatarTejun Heo <tj@kernel.org>
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Acked-by: default avatarJosh Don <joshdon@google.com>
      Acked-by: default avatarHao Luo <haoluo@google.com>
      Acked-by: default avatarBarret Rhoden <brho@google.com>
      Cc: Julia Lawall <julia.lawall@inria.fr>
      8a010b81
    • Tejun Heo's avatar
      sched_ext: Add sysrq-S which disables the BPF scheduler · 79e10440
      Tejun Heo authored
      This enables the admin to abort the BPF scheduler and revert to CFS anytime.
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      Acked-by: default avatarJosh Don <joshdon@google.com>
      Acked-by: default avatarHao Luo <haoluo@google.com>
      Acked-by: default avatarBarret Rhoden <brho@google.com>
      79e10440
    • Tejun Heo's avatar
      sched_ext: Add scx_simple and scx_example_qmap example schedulers · 2a52ca7c
      Tejun Heo authored
      Add two simple example BPF schedulers - simple and qmap.
      
      * simple: In terms of scheduling, it behaves identical to not having any
        operation implemented at all. The two operations it implements are only to
        improve visibility and exit handling. On certain homogeneous
        configurations, this actually can perform pretty well.
      
      * qmap: A fixed five level priority scheduler to demonstrate queueing PIDs
        on BPF maps for scheduling. While not very practical, this is useful as a
        simple example and will be used to demonstrate different features.
      
      v7: - Compat helpers stripped out in prepartion of upstreaming as the
            upstreamed patchset will be the baselinfe. Utility macros that can be
            used to implement compat features are kept.
      
          - Explicitly disable map autoattach on struct_ops to avoid trying to
            attach twice while maintaining compatbility with older libbpf.
      
      v6: - Common header files reorganized and cleaned up. Compat helpers are
            added to demonstrate how schedulers can maintain backward
            compatibility with older kernels while making use of newly added
            features.
      
          - simple_select_cpu() added to keep track of the number of local
            dispatches. This is needed because the default ops.select_cpu()
            implementation is updated to dispatch directly and won't call
            ops.enqueue().
      
          - Updated to reflect the sched_ext API changes. Switching all tasks is
            the default behavior now and scx_qmap supports partial switching when
            `-p` is specified.
      
          - tools/sched_ext/Kconfig dropped. This will be included in the doc
            instead.
      
      v5: - Improve Makefile. Build artifects are now collected into a separate
            dir which change be changed. Install and help targets are added and
            clean actually cleans everything.
      
          - MEMBER_VPTR() improved to improve access to structs. ARRAY_ELEM_PTR()
            and RESIZEABLE_ARRAY() are added to support resizable arrays in .bss.
      
          - Add scx_common.h which provides common utilities to user code such as
            SCX_BUG[_ON]() and RESIZE_ARRAY().
      
          - Use SCX_BUG[_ON]() to simplify error handling.
      
      v4: - Dropped _example prefix from scheduler names.
      
      v3: - Rename scx_example_dummy to scx_example_simple and restructure a bit
            to ease later additions. Comment updates.
      
          - Added declarations for BPF inline iterators. In the future, hopefully,
            these will be consolidated into a generic BPF header so that they
            don't need to be replicated here.
      
      v2: - Updated with the generic BPF cpumask helpers.
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      Acked-by: default avatarJosh Don <joshdon@google.com>
      Acked-by: default avatarHao Luo <haoluo@google.com>
      Acked-by: default avatarBarret Rhoden <brho@google.com>
      2a52ca7c
    • Tejun Heo's avatar
      sched_ext: Implement BPF extensible scheduler class · f0e1a064
      Tejun Heo authored
      Implement a new scheduler class sched_ext (SCX), which allows scheduling
      policies to be implemented as BPF programs to achieve the following:
      
      1. Ease of experimentation and exploration: Enabling rapid iteration of new
         scheduling policies.
      
      2. Customization: Building application-specific schedulers which implement
         policies that are not applicable to general-purpose schedulers.
      
      3. Rapid scheduler deployments: Non-disruptive swap outs of scheduling
         policies in production environments.
      
      sched_ext leverages BPF’s struct_ops feature to define a structure which
      exports function callbacks and flags to BPF programs that wish to implement
      scheduling policies. The struct_ops structure exported by sched_ext is
      struct sched_ext_ops, and is conceptually similar to struct sched_class. The
      role of sched_ext is to map the complex sched_class callbacks to the more
      simple and ergonomic struct sched_ext_ops callbacks.
      
      For more detailed discussion on the motivations and overview, please refer
      to the cover letter.
      
      Later patches will also add several example schedulers and documentation.
      
      This patch implements the minimum core framework to enable implementation of
      BPF schedulers. Subsequent patches will gradually add functionalities
      including safety guarantee mechanisms, nohz and cgroup support.
      
      include/linux/sched/ext.h defines struct sched_ext_ops. With the comment on
      top, each operation should be self-explanatory. The followings are worth
      noting:
      
      - Both "sched_ext" and its shorthand "scx" are used. If the identifier
        already has "sched" in it, "ext" is used; otherwise, "scx".
      
      - In sched_ext_ops, only .name is mandatory. Every operation is optional and
        if omitted a simple but functional default behavior is provided.
      
      - A new policy constant SCHED_EXT is added and a task can select sched_ext
        by invoking sched_setscheduler(2) with the new policy constant. However,
        if the BPF scheduler is not loaded, SCHED_EXT is the same as SCHED_NORMAL
        and the task is scheduled by CFS. When the BPF scheduler is loaded, all
        tasks which have the SCHED_EXT policy are switched to sched_ext.
      
      - To bridge the workflow imbalance between the scheduler core and
        sched_ext_ops callbacks, sched_ext uses simple FIFOs called dispatch
        queues (dsq's). By default, there is one global dsq (SCX_DSQ_GLOBAL), and
        one local per-CPU dsq (SCX_DSQ_LOCAL). SCX_DSQ_GLOBAL is provided for
        convenience and need not be used by a scheduler that doesn't require it.
        SCX_DSQ_LOCAL is the per-CPU FIFO that sched_ext pulls from when putting
        the next task on the CPU. The BPF scheduler can manage an arbitrary number
        of dsq's using scx_bpf_create_dsq() and scx_bpf_destroy_dsq().
      
      - sched_ext guarantees system integrity no matter what the BPF scheduler
        does. To enable this, each task's ownership is tracked through
        p->scx.ops_state and all tasks are put on scx_tasks list. The disable path
        can always recover and revert all tasks back to CFS. See p->scx.ops_state
        and scx_tasks.
      
      - A task is not tied to its rq while enqueued. This decouples CPU selection
        from queueing and allows sharing a scheduling queue across an arbitrary
        subset of CPUs. This adds some complexities as a task may need to be
        bounced between rq's right before it starts executing. See
        dispatch_to_local_dsq() and move_task_to_local_dsq().
      
      - One complication that arises from the above weak association between task
        and rq is that synchronizing with dequeue() gets complicated as dequeue()
        may happen anytime while the task is enqueued and the dispatch path might
        need to release the rq lock to transfer the task. Solving this requires a
        bit of complexity. See the logic around p->scx.sticky_cpu and
        p->scx.ops_qseq.
      
      - Both enable and disable paths are a bit complicated. The enable path
        switches all tasks without blocking to avoid issues which can arise from
        partially switched states (e.g. the switching task itself being starved).
        The disable path can't trust the BPF scheduler at all, so it also has to
        guarantee forward progress without blocking. See scx_ops_enable() and
        scx_ops_disable_workfn().
      
      - When sched_ext is disabled, static_branches are used to shut down the
        entry points from hot paths.
      
      v7: - scx_ops_bypass() was incorrectly and unnecessarily trying to grab
            scx_ops_enable_mutex which can lead to deadlocks in the disable path.
            Fixed.
      
          - Fixed TASK_DEAD handling bug in scx_ops_enable() path which could lead
            to use-after-free.
      
          - Consolidated per-cpu variable usages and other cleanups.
      
      v6: - SCX_NR_ONLINE_OPS replaced with SCX_OPI_*_BEGIN/END so that multiple
            groups can be expressed. Later CPU hotplug operations are put into
            their own group.
      
          - SCX_OPS_DISABLING state is replaced with the new bypass mechanism
            which allows temporarily putting the system into simple FIFO
            scheduling mode bypassing the BPF scheduler. In addition to the shut
            down path, this will also be used to isolate the BPF scheduler across
            PM events. Enabling and disabling the bypass mode requires iterating
            all runnable tasks. rq->scx.runnable_list addition is moved from the
            later watchdog patch.
      
          - ops.prep_enable() is replaced with ops.init_task() and
            ops.enable/disable() are now called whenever the task enters and
            leaves sched_ext instead of when the task becomes schedulable on
            sched_ext and stops being so. A new operation - ops.exit_task() - is
            called when the task stops being schedulable on sched_ext.
      
          - scx_bpf_dispatch() can now be called from ops.select_cpu() too. This
            removes the need for communicating local dispatch decision made by
            ops.select_cpu() to ops.enqueue() via per-task storage.
            SCX_KF_SELECT_CPU is added to support the change.
      
          - SCX_TASK_ENQ_LOCAL which told the BPF scheudler that
            scx_select_cpu_dfl() wants the task to be dispatched to the local DSQ
            was removed. Instead, scx_bpf_select_cpu_dfl() now dispatches directly
            if it finds a suitable idle CPU. If such behavior is not desired,
            users can use scx_bpf_select_cpu_dfl() which returns the verdict in a
            bool out param.
      
          - scx_select_cpu_dfl() was mishandling WAKE_SYNC and could end up
            queueing many tasks on a local DSQ which makes tasks to execute in
            order while other CPUs stay idle which made some hackbench numbers
            really bad. Fixed.
      
          - The current state of sched_ext can now be monitored through files
            under /sys/sched_ext instead of /sys/kernel/debug/sched/ext. This is
            to enable monitoring on kernels which don't enable debugfs.
      
          - sched_ext wasn't telling BPF that ops.dispatch()'s @prev argument may
            be NULL and a BPF scheduler which derefs the pointer without checking
            could crash the kernel. Tell BPF. This is currently a bit ugly. A
            better way to annotate this is expected in the future.
      
          - scx_exit_info updated to carry pointers to message buffers instead of
            embedding them directly. This decouples buffer sizes from API so that
            they can be changed without breaking compatibility.
      
          - exit_code added to scx_exit_info. This is used to indicate different
            exit conditions on non-error exits and will be used to handle e.g. CPU
            hotplugs.
      
          - The patch "sched_ext: Allow BPF schedulers to switch all eligible
            tasks into sched_ext" is folded in and the interface is changed so
            that partial switching is indicated with a new ops flag
            %SCX_OPS_SWITCH_PARTIAL. This makes scx_bpf_switch_all() unnecessasry
            and in turn SCX_KF_INIT. ops.init() is now called with
            SCX_KF_SLEEPABLE.
      
          - Code reorganized so that only the parts necessary to integrate with
            the rest of the kernel are in the header files.
      
          - Changes to reflect the BPF and other kernel changes including the
            addition of bpf_sched_ext_ops.cfi_stubs.
      
      v5: - To accommodate 32bit configs, p->scx.ops_state is now atomic_long_t
            instead of atomic64_t and scx_dsp_buf_ent.qseq which uses
            load_acquire/store_release is now unsigned long instead of u64.
      
          - Fix the bug where bpf_scx_btf_struct_access() was allowing write
            access to arbitrary fields.
      
          - Distinguish kfuncs which can be called from any sched_ext ops and from
            anywhere. e.g. scx_bpf_pick_idle_cpu() can now be called only from
            sched_ext ops.
      
          - Rename "type" to "kind" in scx_exit_info to make it easier to use on
            languages in which "type" is a reserved keyword.
      
          - Since cff9b233 ("kernel/sched: Modify initial boot task idle
            setup"), PF_IDLE is not set on idle tasks which haven't been online
            yet which made scx_task_iter_next_filtered() include those idle tasks
            in iterations leading to oopses. Update scx_task_iter_next_filtered()
            to directly test p->sched_class against idle_sched_class instead of
            using is_idle_task() which tests PF_IDLE.
      
          - Other updates to match upstream changes such as adding const to
            set_cpumask() param and renaming check_preempt_curr() to
            wakeup_preempt().
      
      v4: - SCHED_CHANGE_BLOCK replaced with the previous
            sched_deq_and_put_task()/sched_enq_and_set_tsak() pair. This is
            because upstream is adaopting a different generic cleanup mechanism.
            Once that lands, the code will be adapted accordingly.
      
          - task_on_scx() used to test whether a task should be switched into SCX,
            which is confusing. Renamed to task_should_scx(). task_on_scx() now
            tests whether a task is currently on SCX.
      
          - scx_has_idle_cpus is barely used anymore and replaced with direct
            check on the idle cpumask.
      
          - SCX_PICK_IDLE_CORE added and scx_pick_idle_cpu() improved to prefer
            fully idle cores.
      
          - ops.enable() now sees up-to-date p->scx.weight value.
      
          - ttwu_queue path is disabled for tasks on SCX to avoid confusing BPF
            schedulers expecting ->select_cpu() call.
      
          - Use cpu_smt_mask() instead of topology_sibling_cpumask() like the rest
            of the scheduler.
      
      v3: - ops.set_weight() added to allow BPF schedulers to track weight changes
            without polling p->scx.weight.
      
          - move_task_to_local_dsq() was losing SCX-specific enq_flags when
            enqueueing the task on the target dsq because it goes through
            activate_task() which loses the upper 32bit of the flags. Carry the
            flags through rq->scx.extra_enq_flags.
      
          - scx_bpf_dispatch(), scx_bpf_pick_idle_cpu(), scx_bpf_task_running()
            and scx_bpf_task_cpu() now use the new KF_RCU instead of
            KF_TRUSTED_ARGS to make it easier for BPF schedulers to call them.
      
          - The kfunc helper access control mechanism implemented through
            sched_ext_entity.kf_mask is improved. Now SCX_CALL_OP*() is always
            used when invoking scx_ops operations.
      
      v2: - balance_scx_on_up() is dropped. Instead, on UP, balance_scx() is
            called from put_prev_taks_scx() and pick_next_task_scx() as necessary.
            To determine whether balance_scx() should be called from
            put_prev_task_scx(), SCX_TASK_DEQD_FOR_SLEEP flag is added. See the
            comment in put_prev_task_scx() for details.
      
          - sched_deq_and_put_task() / sched_enq_and_set_task() sequences replaced
            with SCHED_CHANGE_BLOCK().
      
          - Unused all_dsqs list removed. This was a left-over from previous
            iterations.
      
          - p->scx.kf_mask is added to track and enforce which kfunc helpers are
            allowed. Also, init/exit sequences are updated to make some kfuncs
            always safe to call regardless of the current BPF scheduler state.
            Combined, this should make all the kfuncs safe.
      
          - BPF now supports sleepable struct_ops operations. Hacky workaround
            removed and operations and kfunc helpers are tagged appropriately.
      
          - BPF now supports bitmask / cpumask helpers. scx_bpf_get_idle_cpumask()
            and friends are added so that BPF schedulers can use the idle masks
            with the generic helpers. This replaces the hacky kfunc helpers added
            by a separate patch in V1.
      
          - CONFIG_SCHED_CLASS_EXT can no longer be enabled if SCHED_CORE is
            enabled. This restriction will be removed by a later patch which adds
            core-sched support.
      
          - Add MAINTAINERS entries and other misc changes.
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Co-authored-by: default avatarDavid Vernet <dvernet@meta.com>
      Acked-by: default avatarJosh Don <joshdon@google.com>
      Acked-by: default avatarHao Luo <haoluo@google.com>
      Acked-by: default avatarBarret Rhoden <brho@google.com>
      Cc: Andrea Righi <andrea.righi@canonical.com>
      f0e1a064
    • Tejun Heo's avatar
      sched_ext: Add boilerplate for extensible scheduler class · a7a9fc54
      Tejun Heo authored
      This adds dummy implementations of sched_ext interfaces which interact with
      the scheduler core and hook them in the correct places. As they're all
      dummies, this doesn't cause any behavior changes. This is split out to help
      reviewing.
      
      v2: balance_scx_on_up() dropped. This will be handled in sched_ext proper.
      Signed-off-by: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      Acked-by: default avatarJosh Don <joshdon@google.com>
      Acked-by: default avatarHao Luo <haoluo@google.com>
      Acked-by: default avatarBarret Rhoden <brho@google.com>
      a7a9fc54
    • Tejun Heo's avatar
      sched: Add normal_policy() · 2c8d046d
      Tejun Heo authored
      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: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      Acked-by: default avatarJosh Don <joshdon@google.com>
      Acked-by: default avatarHao Luo <haoluo@google.com>
      Acked-by: default avatarBarret Rhoden <brho@google.com>
      2c8d046d
    • Tejun Heo's avatar
      sched: Factor out update_other_load_avgs() from __update_blocked_others() · 96fd6c65
      Tejun Heo authored
      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: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      96fd6c65
    • Tejun Heo's avatar
      sched: Factor out cgroup weight conversion functions · 4f9c7ca8
      Tejun Heo authored
      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: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      Acked-by: default avatarJosh Don <joshdon@google.com>
      Acked-by: default avatarHao Luo <haoluo@google.com>
      Acked-by: default avatarBarret Rhoden <brho@google.com>
      4f9c7ca8
    • Tejun Heo's avatar
      sched: Add sched_class->switching_to() and expose check_class_changing/changed() · d8c7bc2e
      Tejun Heo authored
      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: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      Acked-by: default avatarJosh Don <joshdon@google.com>
      Acked-by: default avatarHao Luo <haoluo@google.com>
      Acked-by: default avatarBarret Rhoden <brho@google.com>
      d8c7bc2e
    • Tejun Heo's avatar
      sched: Add sched_class->reweight_task() · e83edbf8
      Tejun Heo authored
      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: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      Acked-by: default avatarJosh Don <joshdon@google.com>
      Acked-by: default avatarHao Luo <haoluo@google.com>
      Acked-by: default avatarBarret Rhoden <brho@google.com>
      e83edbf8
    • Tejun Heo's avatar
      sched: Allow sched_cgroup_fork() to fail and introduce sched_cancel_fork() · 304b3f2b
      Tejun Heo authored
      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: default avatarTejun Heo <tj@kernel.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      Acked-by: default avatarJosh Don <joshdon@google.com>
      Acked-by: default avatarHao Luo <haoluo@google.com>
      Acked-by: default avatarBarret Rhoden <brho@google.com>
      304b3f2b
    • Tejun Heo's avatar
      sched: Restructure sched_class order sanity checks in sched_init() · df268382
      Tejun Heo authored
      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: default avatarTejun Heo <tj@kernel.org>
      Suggested-by: default avatarPeter Zijlstra <peterz@infradead.org>
      Reviewed-by: default avatarDavid Vernet <dvernet@meta.com>
      df268382