189e33ea7SMauro Carvalho Chehab=========
289e33ea7SMauro Carvalho ChehabLivepatch
389e33ea7SMauro Carvalho Chehab=========
489e33ea7SMauro Carvalho Chehab
589e33ea7SMauro Carvalho ChehabThis document outlines basic information about kernel livepatching.
689e33ea7SMauro Carvalho Chehab
789e33ea7SMauro Carvalho Chehab.. Table of Contents:
889e33ea7SMauro Carvalho Chehab
9*7af6fbddSMark Brown.. contents:: :local:
1089e33ea7SMauro Carvalho Chehab
1189e33ea7SMauro Carvalho Chehab
1289e33ea7SMauro Carvalho Chehab1. Motivation
1389e33ea7SMauro Carvalho Chehab=============
1489e33ea7SMauro Carvalho Chehab
1589e33ea7SMauro Carvalho ChehabThere are many situations where users are reluctant to reboot a system. It may
1689e33ea7SMauro Carvalho Chehabbe because their system is performing complex scientific computations or under
1789e33ea7SMauro Carvalho Chehabheavy load during peak usage. In addition to keeping systems up and running,
1889e33ea7SMauro Carvalho Chehabusers want to also have a stable and secure system. Livepatching gives users
1989e33ea7SMauro Carvalho Chehabboth by allowing for function calls to be redirected; thus, fixing critical
2089e33ea7SMauro Carvalho Chehabfunctions without a system reboot.
2189e33ea7SMauro Carvalho Chehab
2289e33ea7SMauro Carvalho Chehab
2389e33ea7SMauro Carvalho Chehab2. Kprobes, Ftrace, Livepatching
2489e33ea7SMauro Carvalho Chehab================================
2589e33ea7SMauro Carvalho Chehab
2689e33ea7SMauro Carvalho ChehabThere are multiple mechanisms in the Linux kernel that are directly related
2789e33ea7SMauro Carvalho Chehabto redirection of code execution; namely: kernel probes, function tracing,
2889e33ea7SMauro Carvalho Chehaband livepatching:
2989e33ea7SMauro Carvalho Chehab
3089e33ea7SMauro Carvalho Chehab  - The kernel probes are the most generic. The code can be redirected by
3189e33ea7SMauro Carvalho Chehab    putting a breakpoint instruction instead of any instruction.
3289e33ea7SMauro Carvalho Chehab
3389e33ea7SMauro Carvalho Chehab  - The function tracer calls the code from a predefined location that is
3489e33ea7SMauro Carvalho Chehab    close to the function entry point. This location is generated by the
3589e33ea7SMauro Carvalho Chehab    compiler using the '-pg' gcc option.
3689e33ea7SMauro Carvalho Chehab
3789e33ea7SMauro Carvalho Chehab  - Livepatching typically needs to redirect the code at the very beginning
3889e33ea7SMauro Carvalho Chehab    of the function entry before the function parameters or the stack
3989e33ea7SMauro Carvalho Chehab    are in any way modified.
4089e33ea7SMauro Carvalho Chehab
4189e33ea7SMauro Carvalho ChehabAll three approaches need to modify the existing code at runtime. Therefore
4289e33ea7SMauro Carvalho Chehabthey need to be aware of each other and not step over each other's toes.
4389e33ea7SMauro Carvalho ChehabMost of these problems are solved by using the dynamic ftrace framework as
4489e33ea7SMauro Carvalho Chehaba base. A Kprobe is registered as a ftrace handler when the function entry
4589e33ea7SMauro Carvalho Chehabis probed, see CONFIG_KPROBES_ON_FTRACE. Also an alternative function from
4689e33ea7SMauro Carvalho Chehaba live patch is called with the help of a custom ftrace handler. But there are
4789e33ea7SMauro Carvalho Chehabsome limitations, see below.
4889e33ea7SMauro Carvalho Chehab
4989e33ea7SMauro Carvalho Chehab
5089e33ea7SMauro Carvalho Chehab3. Consistency model
5189e33ea7SMauro Carvalho Chehab====================
5289e33ea7SMauro Carvalho Chehab
5389e33ea7SMauro Carvalho ChehabFunctions are there for a reason. They take some input parameters, get or
5489e33ea7SMauro Carvalho Chehabrelease locks, read, process, and even write some data in a defined way,
5589e33ea7SMauro Carvalho Chehabhave return values. In other words, each function has a defined semantic.
5689e33ea7SMauro Carvalho Chehab
5789e33ea7SMauro Carvalho ChehabMany fixes do not change the semantic of the modified functions. For
5889e33ea7SMauro Carvalho Chehabexample, they add a NULL pointer or a boundary check, fix a race by adding
5989e33ea7SMauro Carvalho Chehaba missing memory barrier, or add some locking around a critical section.
6089e33ea7SMauro Carvalho ChehabMost of these changes are self contained and the function presents itself
6189e33ea7SMauro Carvalho Chehabthe same way to the rest of the system. In this case, the functions might
6289e33ea7SMauro Carvalho Chehabbe updated independently one by one.
6389e33ea7SMauro Carvalho Chehab
6489e33ea7SMauro Carvalho ChehabBut there are more complex fixes. For example, a patch might change
6589e33ea7SMauro Carvalho Chehabordering of locking in multiple functions at the same time. Or a patch
6689e33ea7SMauro Carvalho Chehabmight exchange meaning of some temporary structures and update
6789e33ea7SMauro Carvalho Chehaball the relevant functions. In this case, the affected unit
6889e33ea7SMauro Carvalho Chehab(thread, whole kernel) need to start using all new versions of
6989e33ea7SMauro Carvalho Chehabthe functions at the same time. Also the switch must happen only
7089e33ea7SMauro Carvalho Chehabwhen it is safe to do so, e.g. when the affected locks are released
7189e33ea7SMauro Carvalho Chehabor no data are stored in the modified structures at the moment.
7289e33ea7SMauro Carvalho Chehab
7389e33ea7SMauro Carvalho ChehabThe theory about how to apply functions a safe way is rather complex.
7489e33ea7SMauro Carvalho ChehabThe aim is to define a so-called consistency model. It attempts to define
7589e33ea7SMauro Carvalho Chehabconditions when the new implementation could be used so that the system
7689e33ea7SMauro Carvalho Chehabstays consistent.
7789e33ea7SMauro Carvalho Chehab
7889e33ea7SMauro Carvalho ChehabLivepatch has a consistency model which is a hybrid of kGraft and
7989e33ea7SMauro Carvalho Chehabkpatch:  it uses kGraft's per-task consistency and syscall barrier
8089e33ea7SMauro Carvalho Chehabswitching combined with kpatch's stack trace switching.  There are also
8189e33ea7SMauro Carvalho Chehaba number of fallback options which make it quite flexible.
8289e33ea7SMauro Carvalho Chehab
8389e33ea7SMauro Carvalho ChehabPatches are applied on a per-task basis, when the task is deemed safe to
8489e33ea7SMauro Carvalho Chehabswitch over.  When a patch is enabled, livepatch enters into a
8589e33ea7SMauro Carvalho Chehabtransition state where tasks are converging to the patched state.
8689e33ea7SMauro Carvalho ChehabUsually this transition state can complete in a few seconds.  The same
8789e33ea7SMauro Carvalho Chehabsequence occurs when a patch is disabled, except the tasks converge from
8889e33ea7SMauro Carvalho Chehabthe patched state to the unpatched state.
8989e33ea7SMauro Carvalho Chehab
9089e33ea7SMauro Carvalho ChehabAn interrupt handler inherits the patched state of the task it
9189e33ea7SMauro Carvalho Chehabinterrupts.  The same is true for forked tasks: the child inherits the
9289e33ea7SMauro Carvalho Chehabpatched state of the parent.
9389e33ea7SMauro Carvalho Chehab
9489e33ea7SMauro Carvalho ChehabLivepatch uses several complementary approaches to determine when it's
9589e33ea7SMauro Carvalho Chehabsafe to patch tasks:
9689e33ea7SMauro Carvalho Chehab
9789e33ea7SMauro Carvalho Chehab1. The first and most effective approach is stack checking of sleeping
9889e33ea7SMauro Carvalho Chehab   tasks.  If no affected functions are on the stack of a given task,
9989e33ea7SMauro Carvalho Chehab   the task is patched.  In most cases this will patch most or all of
10089e33ea7SMauro Carvalho Chehab   the tasks on the first try.  Otherwise it'll keep trying
10189e33ea7SMauro Carvalho Chehab   periodically.  This option is only available if the architecture has
10289e33ea7SMauro Carvalho Chehab   reliable stacks (HAVE_RELIABLE_STACKTRACE).
10389e33ea7SMauro Carvalho Chehab
10489e33ea7SMauro Carvalho Chehab2. The second approach, if needed, is kernel exit switching.  A
10589e33ea7SMauro Carvalho Chehab   task is switched when it returns to user space from a system call, a
10689e33ea7SMauro Carvalho Chehab   user space IRQ, or a signal.  It's useful in the following cases:
10789e33ea7SMauro Carvalho Chehab
10889e33ea7SMauro Carvalho Chehab   a) Patching I/O-bound user tasks which are sleeping on an affected
10989e33ea7SMauro Carvalho Chehab      function.  In this case you have to send SIGSTOP and SIGCONT to
11089e33ea7SMauro Carvalho Chehab      force it to exit the kernel and be patched.
11189e33ea7SMauro Carvalho Chehab   b) Patching CPU-bound user tasks.  If the task is highly CPU-bound
11289e33ea7SMauro Carvalho Chehab      then it will get patched the next time it gets interrupted by an
11389e33ea7SMauro Carvalho Chehab      IRQ.
11489e33ea7SMauro Carvalho Chehab
11589e33ea7SMauro Carvalho Chehab3. For idle "swapper" tasks, since they don't ever exit the kernel, they
11689e33ea7SMauro Carvalho Chehab   instead have a klp_update_patch_state() call in the idle loop which
11789e33ea7SMauro Carvalho Chehab   allows them to be patched before the CPU enters the idle state.
11889e33ea7SMauro Carvalho Chehab
11989e33ea7SMauro Carvalho Chehab   (Note there's not yet such an approach for kthreads.)
12089e33ea7SMauro Carvalho Chehab
12189e33ea7SMauro Carvalho ChehabArchitectures which don't have HAVE_RELIABLE_STACKTRACE solely rely on
12289e33ea7SMauro Carvalho Chehabthe second approach. It's highly likely that some tasks may still be
12389e33ea7SMauro Carvalho Chehabrunning with an old version of the function, until that function
12489e33ea7SMauro Carvalho Chehabreturns. In this case you would have to signal the tasks. This
12589e33ea7SMauro Carvalho Chehabespecially applies to kthreads. They may not be woken up and would need
12689e33ea7SMauro Carvalho Chehabto be forced. See below for more information.
12789e33ea7SMauro Carvalho Chehab
12889e33ea7SMauro Carvalho ChehabUnless we can come up with another way to patch kthreads, architectures
12989e33ea7SMauro Carvalho Chehabwithout HAVE_RELIABLE_STACKTRACE are not considered fully supported by
13089e33ea7SMauro Carvalho Chehabthe kernel livepatching.
13189e33ea7SMauro Carvalho Chehab
13289e33ea7SMauro Carvalho ChehabThe /sys/kernel/livepatch/<patch>/transition file shows whether a patch
13389e33ea7SMauro Carvalho Chehabis in transition.  Only a single patch can be in transition at a given
13489e33ea7SMauro Carvalho Chehabtime.  A patch can remain in transition indefinitely, if any of the tasks
13589e33ea7SMauro Carvalho Chehabare stuck in the initial patch state.
13689e33ea7SMauro Carvalho Chehab
13789e33ea7SMauro Carvalho ChehabA transition can be reversed and effectively canceled by writing the
13889e33ea7SMauro Carvalho Chehabopposite value to the /sys/kernel/livepatch/<patch>/enabled file while
13989e33ea7SMauro Carvalho Chehabthe transition is in progress.  Then all the tasks will attempt to
14089e33ea7SMauro Carvalho Chehabconverge back to the original patch state.
14189e33ea7SMauro Carvalho Chehab
14289e33ea7SMauro Carvalho ChehabThere's also a /proc/<pid>/patch_state file which can be used to
14389e33ea7SMauro Carvalho Chehabdetermine which tasks are blocking completion of a patching operation.
14489e33ea7SMauro Carvalho ChehabIf a patch is in transition, this file shows 0 to indicate the task is
14589e33ea7SMauro Carvalho Chehabunpatched and 1 to indicate it's patched.  Otherwise, if no patch is in
14689e33ea7SMauro Carvalho Chehabtransition, it shows -1.  Any tasks which are blocking the transition
14789e33ea7SMauro Carvalho Chehabcan be signaled with SIGSTOP and SIGCONT to force them to change their
14889e33ea7SMauro Carvalho Chehabpatched state. This may be harmful to the system though. Sending a fake signal
14989e33ea7SMauro Carvalho Chehabto all remaining blocking tasks is a better alternative. No proper signal is
15089e33ea7SMauro Carvalho Chehabactually delivered (there is no data in signal pending structures). Tasks are
15189e33ea7SMauro Carvalho Chehabinterrupted or woken up, and forced to change their patched state. The fake
15289e33ea7SMauro Carvalho Chehabsignal is automatically sent every 15 seconds.
15389e33ea7SMauro Carvalho Chehab
15489e33ea7SMauro Carvalho ChehabAdministrator can also affect a transition through
15589e33ea7SMauro Carvalho Chehab/sys/kernel/livepatch/<patch>/force attribute. Writing 1 there clears
15689e33ea7SMauro Carvalho ChehabTIF_PATCH_PENDING flag of all tasks and thus forces the tasks to the patched
15789e33ea7SMauro Carvalho Chehabstate. Important note! The force attribute is intended for cases when the
15889e33ea7SMauro Carvalho Chehabtransition gets stuck for a long time because of a blocking task. Administrator
15989e33ea7SMauro Carvalho Chehabis expected to collect all necessary data (namely stack traces of such blocking
16089e33ea7SMauro Carvalho Chehabtasks) and request a clearance from a patch distributor to force the transition.
16189e33ea7SMauro Carvalho ChehabUnauthorized usage may cause harm to the system. It depends on the nature of the
16289e33ea7SMauro Carvalho Chehabpatch, which functions are (un)patched, and which functions the blocking tasks
16389e33ea7SMauro Carvalho Chehabare sleeping in (/proc/<pid>/stack may help here). Removal (rmmod) of patch
16489e33ea7SMauro Carvalho Chehabmodules is permanently disabled when the force feature is used. It cannot be
16589e33ea7SMauro Carvalho Chehabguaranteed there is no task sleeping in such module. It implies unbounded
16689e33ea7SMauro Carvalho Chehabreference count if a patch module is disabled and enabled in a loop.
16789e33ea7SMauro Carvalho Chehab
16889e33ea7SMauro Carvalho ChehabMoreover, the usage of force may also affect future applications of live
16989e33ea7SMauro Carvalho Chehabpatches and cause even more harm to the system. Administrator should first
17089e33ea7SMauro Carvalho Chehabconsider to simply cancel a transition (see above). If force is used, reboot
17189e33ea7SMauro Carvalho Chehabshould be planned and no more live patches applied.
17289e33ea7SMauro Carvalho Chehab
17389e33ea7SMauro Carvalho Chehab3.1 Adding consistency model support to new architectures
17489e33ea7SMauro Carvalho Chehab---------------------------------------------------------
17589e33ea7SMauro Carvalho Chehab
17689e33ea7SMauro Carvalho ChehabFor adding consistency model support to new architectures, there are a
17789e33ea7SMauro Carvalho Chehabfew options:
17889e33ea7SMauro Carvalho Chehab
17989e33ea7SMauro Carvalho Chehab1) Add CONFIG_HAVE_RELIABLE_STACKTRACE.  This means porting objtool, and
18089e33ea7SMauro Carvalho Chehab   for non-DWARF unwinders, also making sure there's a way for the stack
18189e33ea7SMauro Carvalho Chehab   tracing code to detect interrupts on the stack.
18289e33ea7SMauro Carvalho Chehab
18389e33ea7SMauro Carvalho Chehab2) Alternatively, ensure that every kthread has a call to
18489e33ea7SMauro Carvalho Chehab   klp_update_patch_state() in a safe location.  Kthreads are typically
18589e33ea7SMauro Carvalho Chehab   in an infinite loop which does some action repeatedly.  The safe
18689e33ea7SMauro Carvalho Chehab   location to switch the kthread's patch state would be at a designated
18789e33ea7SMauro Carvalho Chehab   point in the loop where there are no locks taken and all data
18889e33ea7SMauro Carvalho Chehab   structures are in a well-defined state.
18989e33ea7SMauro Carvalho Chehab
19089e33ea7SMauro Carvalho Chehab   The location is clear when using workqueues or the kthread worker
19189e33ea7SMauro Carvalho Chehab   API.  These kthreads process independent actions in a generic loop.
19289e33ea7SMauro Carvalho Chehab
19389e33ea7SMauro Carvalho Chehab   It's much more complicated with kthreads which have a custom loop.
19489e33ea7SMauro Carvalho Chehab   There the safe location must be carefully selected on a case-by-case
19589e33ea7SMauro Carvalho Chehab   basis.
19689e33ea7SMauro Carvalho Chehab
19789e33ea7SMauro Carvalho Chehab   In that case, arches without HAVE_RELIABLE_STACKTRACE would still be
19889e33ea7SMauro Carvalho Chehab   able to use the non-stack-checking parts of the consistency model:
19989e33ea7SMauro Carvalho Chehab
20089e33ea7SMauro Carvalho Chehab   a) patching user tasks when they cross the kernel/user space
20189e33ea7SMauro Carvalho Chehab      boundary; and
20289e33ea7SMauro Carvalho Chehab
20389e33ea7SMauro Carvalho Chehab   b) patching kthreads and idle tasks at their designated patch points.
20489e33ea7SMauro Carvalho Chehab
20589e33ea7SMauro Carvalho Chehab   This option isn't as good as option 1 because it requires signaling
20689e33ea7SMauro Carvalho Chehab   user tasks and waking kthreads to patch them.  But it could still be
20789e33ea7SMauro Carvalho Chehab   a good backup option for those architectures which don't have
20889e33ea7SMauro Carvalho Chehab   reliable stack traces yet.
20989e33ea7SMauro Carvalho Chehab
21089e33ea7SMauro Carvalho Chehab
21189e33ea7SMauro Carvalho Chehab4. Livepatch module
21289e33ea7SMauro Carvalho Chehab===================
21389e33ea7SMauro Carvalho Chehab
21489e33ea7SMauro Carvalho ChehabLivepatches are distributed using kernel modules, see
21589e33ea7SMauro Carvalho Chehabsamples/livepatch/livepatch-sample.c.
21689e33ea7SMauro Carvalho Chehab
21789e33ea7SMauro Carvalho ChehabThe module includes a new implementation of functions that we want
21889e33ea7SMauro Carvalho Chehabto replace. In addition, it defines some structures describing the
21989e33ea7SMauro Carvalho Chehabrelation between the original and the new implementation. Then there
22089e33ea7SMauro Carvalho Chehabis code that makes the kernel start using the new code when the livepatch
22189e33ea7SMauro Carvalho Chehabmodule is loaded. Also there is code that cleans up before the
22289e33ea7SMauro Carvalho Chehablivepatch module is removed. All this is explained in more details in
22389e33ea7SMauro Carvalho Chehabthe next sections.
22489e33ea7SMauro Carvalho Chehab
22589e33ea7SMauro Carvalho Chehab
22689e33ea7SMauro Carvalho Chehab4.1. New functions
22789e33ea7SMauro Carvalho Chehab------------------
22889e33ea7SMauro Carvalho Chehab
22989e33ea7SMauro Carvalho ChehabNew versions of functions are typically just copied from the original
23089e33ea7SMauro Carvalho Chehabsources. A good practice is to add a prefix to the names so that they
23189e33ea7SMauro Carvalho Chehabcan be distinguished from the original ones, e.g. in a backtrace. Also
23289e33ea7SMauro Carvalho Chehabthey can be declared as static because they are not called directly
23389e33ea7SMauro Carvalho Chehaband do not need the global visibility.
23489e33ea7SMauro Carvalho Chehab
23589e33ea7SMauro Carvalho ChehabThe patch contains only functions that are really modified. But they
23689e33ea7SMauro Carvalho Chehabmight want to access functions or data from the original source file
23789e33ea7SMauro Carvalho Chehabthat may only be locally accessible. This can be solved by a special
23889e33ea7SMauro Carvalho Chehabrelocation section in the generated livepatch module, see
23989e33ea7SMauro Carvalho ChehabDocumentation/livepatch/module-elf-format.rst for more details.
24089e33ea7SMauro Carvalho Chehab
24189e33ea7SMauro Carvalho Chehab
24289e33ea7SMauro Carvalho Chehab4.2. Metadata
24389e33ea7SMauro Carvalho Chehab-------------
24489e33ea7SMauro Carvalho Chehab
24589e33ea7SMauro Carvalho ChehabThe patch is described by several structures that split the information
24689e33ea7SMauro Carvalho Chehabinto three levels:
24789e33ea7SMauro Carvalho Chehab
24889e33ea7SMauro Carvalho Chehab  - struct klp_func is defined for each patched function. It describes
24989e33ea7SMauro Carvalho Chehab    the relation between the original and the new implementation of a
25089e33ea7SMauro Carvalho Chehab    particular function.
25189e33ea7SMauro Carvalho Chehab
25289e33ea7SMauro Carvalho Chehab    The structure includes the name, as a string, of the original function.
25389e33ea7SMauro Carvalho Chehab    The function address is found via kallsyms at runtime.
25489e33ea7SMauro Carvalho Chehab
25589e33ea7SMauro Carvalho Chehab    Then it includes the address of the new function. It is defined
25689e33ea7SMauro Carvalho Chehab    directly by assigning the function pointer. Note that the new
25789e33ea7SMauro Carvalho Chehab    function is typically defined in the same source file.
25889e33ea7SMauro Carvalho Chehab
25989e33ea7SMauro Carvalho Chehab    As an optional parameter, the symbol position in the kallsyms database can
26089e33ea7SMauro Carvalho Chehab    be used to disambiguate functions of the same name. This is not the
26189e33ea7SMauro Carvalho Chehab    absolute position in the database, but rather the order it has been found
26289e33ea7SMauro Carvalho Chehab    only for a particular object ( vmlinux or a kernel module ). Note that
26389e33ea7SMauro Carvalho Chehab    kallsyms allows for searching symbols according to the object name.
26489e33ea7SMauro Carvalho Chehab
26589e33ea7SMauro Carvalho Chehab  - struct klp_object defines an array of patched functions (struct
26689e33ea7SMauro Carvalho Chehab    klp_func) in the same object. Where the object is either vmlinux
26789e33ea7SMauro Carvalho Chehab    (NULL) or a module name.
26889e33ea7SMauro Carvalho Chehab
26989e33ea7SMauro Carvalho Chehab    The structure helps to group and handle functions for each object
27089e33ea7SMauro Carvalho Chehab    together. Note that patched modules might be loaded later than
27189e33ea7SMauro Carvalho Chehab    the patch itself and the relevant functions might be patched
27289e33ea7SMauro Carvalho Chehab    only when they are available.
27389e33ea7SMauro Carvalho Chehab
27489e33ea7SMauro Carvalho Chehab
27589e33ea7SMauro Carvalho Chehab  - struct klp_patch defines an array of patched objects (struct
27689e33ea7SMauro Carvalho Chehab    klp_object).
27789e33ea7SMauro Carvalho Chehab
27889e33ea7SMauro Carvalho Chehab    This structure handles all patched functions consistently and eventually,
27989e33ea7SMauro Carvalho Chehab    synchronously. The whole patch is applied only when all patched
28089e33ea7SMauro Carvalho Chehab    symbols are found. The only exception are symbols from objects
28189e33ea7SMauro Carvalho Chehab    (kernel modules) that have not been loaded yet.
28289e33ea7SMauro Carvalho Chehab
28389e33ea7SMauro Carvalho Chehab    For more details on how the patch is applied on a per-task basis,
28489e33ea7SMauro Carvalho Chehab    see the "Consistency model" section.
28589e33ea7SMauro Carvalho Chehab
28689e33ea7SMauro Carvalho Chehab
28789e33ea7SMauro Carvalho Chehab5. Livepatch life-cycle
28889e33ea7SMauro Carvalho Chehab=======================
28989e33ea7SMauro Carvalho Chehab
29089e33ea7SMauro Carvalho ChehabLivepatching can be described by five basic operations:
29189e33ea7SMauro Carvalho Chehabloading, enabling, replacing, disabling, removing.
29289e33ea7SMauro Carvalho Chehab
29389e33ea7SMauro Carvalho ChehabWhere the replacing and the disabling operations are mutually
29489e33ea7SMauro Carvalho Chehabexclusive. They have the same result for the given patch but
29589e33ea7SMauro Carvalho Chehabnot for the system.
29689e33ea7SMauro Carvalho Chehab
29789e33ea7SMauro Carvalho Chehab
29889e33ea7SMauro Carvalho Chehab5.1. Loading
29989e33ea7SMauro Carvalho Chehab------------
30089e33ea7SMauro Carvalho Chehab
30189e33ea7SMauro Carvalho ChehabThe only reasonable way is to enable the patch when the livepatch kernel
30289e33ea7SMauro Carvalho Chehabmodule is being loaded. For this, klp_enable_patch() has to be called
30389e33ea7SMauro Carvalho Chehabin the module_init() callback. There are two main reasons:
30489e33ea7SMauro Carvalho Chehab
30589e33ea7SMauro Carvalho ChehabFirst, only the module has an easy access to the related struct klp_patch.
30689e33ea7SMauro Carvalho Chehab
30789e33ea7SMauro Carvalho ChehabSecond, the error code might be used to refuse loading the module when
30889e33ea7SMauro Carvalho Chehabthe patch cannot get enabled.
30989e33ea7SMauro Carvalho Chehab
31089e33ea7SMauro Carvalho Chehab
31189e33ea7SMauro Carvalho Chehab5.2. Enabling
31289e33ea7SMauro Carvalho Chehab-------------
31389e33ea7SMauro Carvalho Chehab
31489e33ea7SMauro Carvalho ChehabThe livepatch gets enabled by calling klp_enable_patch() from
31589e33ea7SMauro Carvalho Chehabthe module_init() callback. The system will start using the new
31689e33ea7SMauro Carvalho Chehabimplementation of the patched functions at this stage.
31789e33ea7SMauro Carvalho Chehab
31889e33ea7SMauro Carvalho ChehabFirst, the addresses of the patched functions are found according to their
31989e33ea7SMauro Carvalho Chehabnames. The special relocations, mentioned in the section "New functions",
32089e33ea7SMauro Carvalho Chehabare applied. The relevant entries are created under
32189e33ea7SMauro Carvalho Chehab/sys/kernel/livepatch/<name>. The patch is rejected when any above
32289e33ea7SMauro Carvalho Chehaboperation fails.
32389e33ea7SMauro Carvalho Chehab
32489e33ea7SMauro Carvalho ChehabSecond, livepatch enters into a transition state where tasks are converging
32589e33ea7SMauro Carvalho Chehabto the patched state. If an original function is patched for the first
32689e33ea7SMauro Carvalho Chehabtime, a function specific struct klp_ops is created and an universal
32789e33ea7SMauro Carvalho Chehabftrace handler is registered\ [#]_. This stage is indicated by a value of '1'
32889e33ea7SMauro Carvalho Chehabin /sys/kernel/livepatch/<name>/transition. For more information about
32989e33ea7SMauro Carvalho Chehabthis process, see the "Consistency model" section.
33089e33ea7SMauro Carvalho Chehab
33189e33ea7SMauro Carvalho ChehabFinally, once all tasks have been patched, the 'transition' value changes
33289e33ea7SMauro Carvalho Chehabto '0'.
33389e33ea7SMauro Carvalho Chehab
33489e33ea7SMauro Carvalho Chehab.. [#]
33589e33ea7SMauro Carvalho Chehab
33689e33ea7SMauro Carvalho Chehab    Note that functions might be patched multiple times. The ftrace handler
33789e33ea7SMauro Carvalho Chehab    is registered only once for a given function. Further patches just add
33889e33ea7SMauro Carvalho Chehab    an entry to the list (see field `func_stack`) of the struct klp_ops.
33989e33ea7SMauro Carvalho Chehab    The right implementation is selected by the ftrace handler, see
34089e33ea7SMauro Carvalho Chehab    the "Consistency model" section.
34189e33ea7SMauro Carvalho Chehab
34289e33ea7SMauro Carvalho Chehab    That said, it is highly recommended to use cumulative livepatches
34389e33ea7SMauro Carvalho Chehab    because they help keeping the consistency of all changes. In this case,
34489e33ea7SMauro Carvalho Chehab    functions might be patched two times only during the transition period.
34589e33ea7SMauro Carvalho Chehab
34689e33ea7SMauro Carvalho Chehab
34789e33ea7SMauro Carvalho Chehab5.3. Replacing
34889e33ea7SMauro Carvalho Chehab--------------
34989e33ea7SMauro Carvalho Chehab
35089e33ea7SMauro Carvalho ChehabAll enabled patches might get replaced by a cumulative patch that
35189e33ea7SMauro Carvalho Chehabhas the .replace flag set.
35289e33ea7SMauro Carvalho Chehab
35389e33ea7SMauro Carvalho ChehabOnce the new patch is enabled and the 'transition' finishes then
35489e33ea7SMauro Carvalho Chehaball the functions (struct klp_func) associated with the replaced
35589e33ea7SMauro Carvalho Chehabpatches are removed from the corresponding struct klp_ops. Also
35689e33ea7SMauro Carvalho Chehabthe ftrace handler is unregistered and the struct klp_ops is
35789e33ea7SMauro Carvalho Chehabfreed when the related function is not modified by the new patch
35889e33ea7SMauro Carvalho Chehaband func_stack list becomes empty.
35989e33ea7SMauro Carvalho Chehab
36089e33ea7SMauro Carvalho ChehabSee Documentation/livepatch/cumulative-patches.rst for more details.
36189e33ea7SMauro Carvalho Chehab
36289e33ea7SMauro Carvalho Chehab
36389e33ea7SMauro Carvalho Chehab5.4. Disabling
36489e33ea7SMauro Carvalho Chehab--------------
36589e33ea7SMauro Carvalho Chehab
36689e33ea7SMauro Carvalho ChehabEnabled patches might get disabled by writing '0' to
36789e33ea7SMauro Carvalho Chehab/sys/kernel/livepatch/<name>/enabled.
36889e33ea7SMauro Carvalho Chehab
36989e33ea7SMauro Carvalho ChehabFirst, livepatch enters into a transition state where tasks are converging
37089e33ea7SMauro Carvalho Chehabto the unpatched state. The system starts using either the code from
37189e33ea7SMauro Carvalho Chehabthe previously enabled patch or even the original one. This stage is
37289e33ea7SMauro Carvalho Chehabindicated by a value of '1' in /sys/kernel/livepatch/<name>/transition.
37389e33ea7SMauro Carvalho ChehabFor more information about this process, see the "Consistency model"
37489e33ea7SMauro Carvalho Chehabsection.
37589e33ea7SMauro Carvalho Chehab
37689e33ea7SMauro Carvalho ChehabSecond, once all tasks have been unpatched, the 'transition' value changes
37789e33ea7SMauro Carvalho Chehabto '0'. All the functions (struct klp_func) associated with the to-be-disabled
37889e33ea7SMauro Carvalho Chehabpatch are removed from the corresponding struct klp_ops. The ftrace handler
37989e33ea7SMauro Carvalho Chehabis unregistered and the struct klp_ops is freed when the func_stack list
38089e33ea7SMauro Carvalho Chehabbecomes empty.
38189e33ea7SMauro Carvalho Chehab
38289e33ea7SMauro Carvalho ChehabThird, the sysfs interface is destroyed.
38389e33ea7SMauro Carvalho Chehab
38489e33ea7SMauro Carvalho Chehab
38589e33ea7SMauro Carvalho Chehab5.5. Removing
38689e33ea7SMauro Carvalho Chehab-------------
38789e33ea7SMauro Carvalho Chehab
38889e33ea7SMauro Carvalho ChehabModule removal is only safe when there are no users of functions provided
38989e33ea7SMauro Carvalho Chehabby the module. This is the reason why the force feature permanently
39089e33ea7SMauro Carvalho Chehabdisables the removal. Only when the system is successfully transitioned
39189e33ea7SMauro Carvalho Chehabto a new patch state (patched/unpatched) without being forced it is
39289e33ea7SMauro Carvalho Chehabguaranteed that no task sleeps or runs in the old code.
39389e33ea7SMauro Carvalho Chehab
39489e33ea7SMauro Carvalho Chehab
39589e33ea7SMauro Carvalho Chehab6. Sysfs
39689e33ea7SMauro Carvalho Chehab========
39789e33ea7SMauro Carvalho Chehab
39889e33ea7SMauro Carvalho ChehabInformation about the registered patches can be found under
39989e33ea7SMauro Carvalho Chehab/sys/kernel/livepatch. The patches could be enabled and disabled
40089e33ea7SMauro Carvalho Chehabby writing there.
40189e33ea7SMauro Carvalho Chehab
40289e33ea7SMauro Carvalho Chehab/sys/kernel/livepatch/<patch>/force attributes allow administrator to affect a
40389e33ea7SMauro Carvalho Chehabpatching operation.
40489e33ea7SMauro Carvalho Chehab
40589e33ea7SMauro Carvalho ChehabSee Documentation/ABI/testing/sysfs-kernel-livepatch for more details.
40689e33ea7SMauro Carvalho Chehab
40789e33ea7SMauro Carvalho Chehab
40889e33ea7SMauro Carvalho Chehab7. Limitations
40989e33ea7SMauro Carvalho Chehab==============
41089e33ea7SMauro Carvalho Chehab
41189e33ea7SMauro Carvalho ChehabThe current Livepatch implementation has several limitations:
41289e33ea7SMauro Carvalho Chehab
41389e33ea7SMauro Carvalho Chehab  - Only functions that can be traced could be patched.
41489e33ea7SMauro Carvalho Chehab
41589e33ea7SMauro Carvalho Chehab    Livepatch is based on the dynamic ftrace. In particular, functions
41689e33ea7SMauro Carvalho Chehab    implementing ftrace or the livepatch ftrace handler could not be
41789e33ea7SMauro Carvalho Chehab    patched. Otherwise, the code would end up in an infinite loop. A
41889e33ea7SMauro Carvalho Chehab    potential mistake is prevented by marking the problematic functions
41989e33ea7SMauro Carvalho Chehab    by "notrace".
42089e33ea7SMauro Carvalho Chehab
42189e33ea7SMauro Carvalho Chehab
42289e33ea7SMauro Carvalho Chehab
42389e33ea7SMauro Carvalho Chehab  - Livepatch works reliably only when the dynamic ftrace is located at
42489e33ea7SMauro Carvalho Chehab    the very beginning of the function.
42589e33ea7SMauro Carvalho Chehab
42689e33ea7SMauro Carvalho Chehab    The function need to be redirected before the stack or the function
42789e33ea7SMauro Carvalho Chehab    parameters are modified in any way. For example, livepatch requires
42889e33ea7SMauro Carvalho Chehab    using -fentry gcc compiler option on x86_64.
42989e33ea7SMauro Carvalho Chehab
43089e33ea7SMauro Carvalho Chehab    One exception is the PPC port. It uses relative addressing and TOC.
43189e33ea7SMauro Carvalho Chehab    Each function has to handle TOC and save LR before it could call
43289e33ea7SMauro Carvalho Chehab    the ftrace handler. This operation has to be reverted on return.
43389e33ea7SMauro Carvalho Chehab    Fortunately, the generic ftrace code has the same problem and all
43489e33ea7SMauro Carvalho Chehab    this is handled on the ftrace level.
43589e33ea7SMauro Carvalho Chehab
43689e33ea7SMauro Carvalho Chehab
43789e33ea7SMauro Carvalho Chehab  - Kretprobes using the ftrace framework conflict with the patched
43889e33ea7SMauro Carvalho Chehab    functions.
43989e33ea7SMauro Carvalho Chehab
44089e33ea7SMauro Carvalho Chehab    Both kretprobes and livepatches use a ftrace handler that modifies
44189e33ea7SMauro Carvalho Chehab    the return address. The first user wins. Either the probe or the patch
44289e33ea7SMauro Carvalho Chehab    is rejected when the handler is already in use by the other.
44389e33ea7SMauro Carvalho Chehab
44489e33ea7SMauro Carvalho Chehab
44589e33ea7SMauro Carvalho Chehab  - Kprobes in the original function are ignored when the code is
44689e33ea7SMauro Carvalho Chehab    redirected to the new implementation.
44789e33ea7SMauro Carvalho Chehab
44889e33ea7SMauro Carvalho Chehab    There is a work in progress to add warnings about this situation.
449