xref: /linux/Documentation/scheduler/sched-preemption.rst (revision 14c897740388216acd59477a382de327d5e27ba4)
1*cd548f83SQuchaosheng.. SPDX-License-Identifier: GPL-2.0
2*cd548f83SQuchaosheng
380c2913dSQuchaosheng=====================
480c2913dSQuchaoshengScheduler preemption
580c2913dSQuchaosheng=====================
680c2913dSQuchaosheng
780c2913dSQuchaoshengThe kernel can be built to run kernel code either uninterruptibly, or with
880c2913dSQuchaoshengvarying degrees of preemptibility.  These are the *preemption models*.
980c2913dSQuchaoshengThey apply to kernel code only: a task running in user space is always
1080c2913dSQuchaoshengpreempted by the scheduler, whichever model is selected.
1180c2913dSQuchaosheng
1280c2913dSQuchaoshengWhen CONFIG_PREEMPT_DYNAMIC is enabled the preemption model can additionally
1380c2913dSQuchaoshengbe selected at boot time with the ``preempt=`` command line parameter, without
1480c2913dSQuchaoshengrebuilding the kernel.  See
1580c2913dSQuchaoshengDocumentation/admin-guide/kernel-parameters.txt for the parameter itself.
1680c2913dSQuchaoshengThe active model can also be read and changed through
1780c2913dSQuchaosheng/sys/kernel/debug/sched/preempt, which exists for debugging purposes and may
1880c2913dSQuchaoshengchange.
1980c2913dSQuchaosheng
2080c2913dSQuchaoshengThe models selectable at runtime are:
2180c2913dSQuchaosheng
2280c2913dSQuchaosheng	=========	====================================================
2380c2913dSQuchaosheng	none		No preemption of kernel code other than at explicit
2480c2913dSQuchaosheng			``cond_resched()`` / blocking points.
2580c2913dSQuchaosheng	voluntary	As ``none``, plus ``might_sleep()`` sites.
2680c2913dSQuchaosheng	full		Any section that is not explicitly preempt disabled
2780c2913dSQuchaosheng			may be preempted at any time.
2880c2913dSQuchaosheng	lazy		As ``full``, except that the scheduling request is
2980c2913dSQuchaosheng			delayed until the return to user space or the next
3080c2913dSQuchaosheng			tick, whichever comes first.  This delay does not
3180c2913dSQuchaosheng			apply to real-time tasks.
3280c2913dSQuchaosheng	=========	====================================================
3380c2913dSQuchaosheng
3480c2913dSQuchaoshengNot every model is available on every kernel.  An architecture that provides
3580c2913dSQuchaoshenglazy-preempt support offers only ``full`` and ``lazy``, and ``none`` and
3680c2913dSQuchaosheng``voluntary`` are rejected there.  A PREEMPT_RT kernel always offers ``full``,
3780c2913dSQuchaoshengand ``lazy`` as well if the architecture provides it.
3880c2913dSQuchaosheng
3980c2913dSQuchaoshengThe model that is actually active is reported in the boot log::
4080c2913dSQuchaosheng
4180c2913dSQuchaosheng	Dynamic Preempt: full
4280c2913dSQuchaosheng
4380c2913dSQuchaoshengHonouring a scheduling request
4480c2913dSQuchaosheng==============================
4580c2913dSQuchaosheng
4680c2913dSQuchaoshengA wakeup makes a task runnable, and the scheduler then decides whether it
4780c2913dSQuchaoshengshould run.  If there is an idle CPU which is suitable then the task is moved
4880c2913dSQuchaoshengthere.  Otherwise the scheduler has to decide which task to preempt, and the
4980c2913dSQuchaoshengtask that has to leave the CPU gets a reschedule flag.  In the tracing output
5080c2913dSQuchaoshengthis is the ``need-resched`` column, ``N`` for TIF_NEED_RESCHED and ``l`` for
5180c2913dSQuchaoshengTIF_NEED_RESCHED_LAZY.
5280c2913dSQuchaosheng
5380c2913dSQuchaoshengThe kernel then has to honour that request.  A task executing in user space
5480c2913dSQuchaoshengcan always be preempted; a task executing in kernel space can only be
5580c2913dSQuchaoshengpreempted where it is safe to do so, and how that is decided is what
5680c2913dSQuchaoshengseparates the models:
5780c2913dSQuchaosheng
5880c2913dSQuchaosheng``none``
5980c2913dSQuchaosheng	Code paths with long loops contain explicit ``cond_resched()`` calls,
6080c2913dSQuchaosheng	which perform the scheduling.
6180c2913dSQuchaosheng
6280c2913dSQuchaosheng``voluntary``
6380c2913dSQuchaosheng	As ``none``, and functions which are known to be able to block gain
6480c2913dSQuchaosheng	a ``cond_resched()``-style scheduling point as well, through
6580c2913dSQuchaosheng	``might_sleep()``.
6680c2913dSQuchaosheng
6780c2913dSQuchaosheng``full``
6880c2913dSQuchaosheng	Explicit preemption points are no longer involved.  The kernel tracks
6980c2913dSQuchaosheng	whether it may be preempted and schedules once the request can be
7080c2913dSQuchaosheng	honoured.
7180c2913dSQuchaosheng
7280c2913dSQuchaosheng``lazy``
7380c2913dSQuchaosheng	As ``full``, with one difference: the request the fair scheduler sets
7480c2913dSQuchaosheng	is TIF_NEED_RESCHED_LAZY, which is not honoured immediately even where
7580c2913dSQuchaosheng	it could be.  It is delayed until the task returns to user space, so
7680c2913dSQuchaosheng	that the in-kernel work runs to completion and every lock has been
7780c2913dSQuchaosheng	dropped before the CPU is given up.  If the task has not scheduled on
7880c2913dSQuchaosheng	its own by then, the request is turned into a full TIF_NEED_RESCHED on
7980c2913dSQuchaosheng	the next HZ tick; this is visible as ``B`` in the ``need-resched``
8080c2913dSQuchaosheng	column.
8180c2913dSQuchaosheng
8280c2913dSQuchaoshengReal-time tasks always use TIF_NEED_RESCHED, so the delay above does not
8380c2913dSQuchaoshengapply to them.  ``lazy`` is therefore not a replacement for PREEMPT_RT: it
8480c2913dSQuchaoshengkeeps most of the responsiveness of ``full`` for SCHED_NORMAL tasks while
8580c2913dSQuchaoshengletting them run to completion, which avoids leaving a lock behind that
8680c2913dSQuchaoshenganother task will immediately ask for, and avoids pushing cache-hot data out
8780c2913dSQuchaoshengthat would have to be pulled back in.
88