sphinx.addnodesdocument)}( rawsourcechildren]( translations LanguagesNode)}(hhh](h pending_xref)}(hhh]docutils.nodesTextChinese (Simplified)}parenthsba attributes}(ids]classes]names]dupnames]backrefs] refdomainstdreftypedoc reftarget'/translations/zh_CN/livepatch/livepatchmodnameN classnameN refexplicitutagnamehhh ubh)}(hhh]hChinese (Traditional)}hh2sbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget'/translations/zh_TW/livepatch/livepatchmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hItalian}hhFsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget'/translations/it_IT/livepatch/livepatchmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hJapanese}hhZsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget'/translations/ja_JP/livepatch/livepatchmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hKorean}hhnsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget'/translations/ko_KR/livepatch/livepatchmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hSpanish}hhsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget'/translations/sp_SP/livepatch/livepatchmodnameN classnameN refexplicituh1hhh ubeh}(h]h ]h"]h$]h&]current_languageEnglishuh1h hh _documenthsourceNlineNubhsection)}(hhh](htitle)}(h Livepatchh]h Livepatch}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhA/var/lib/git/docbuild/linux/Documentation/livepatch/livepatch.rsthKubh paragraph)}(hCThis document outlines basic information about kernel livepatching.h]hCThis document outlines basic information about kernel livepatching.}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhhhubhcomment)}(hTable of Contents:h]hTable of Contents:}hhsbah}(h]h ]h"]h$]h&] xml:spacepreserveuh1hhhhhhhhKubhtopic)}(hhh]h bullet_list)}(hhh](h list_item)}(hhh]h)}(hhh]h reference)}(hhh]h 1. Motivation}(hhhhhNhNubah}(h]id3ah ]h"]h$]h&]refid motivationuh1hhhubah}(h]h ]h"]h$]h&]uh1hhhubah}(h]h ]h"]h$]h&]uh1hhhubh)}(hhh]h)}(hhh]h)}(hhh]h 2. Kprobes, Ftrace, Livepatching}(hjhhhNhNubah}(h]id4ah ]h"]h$]h&]refidkprobes-ftrace-livepatchinguh1hhj ubah}(h]h ]h"]h$]h&]uh1hhj ubah}(h]h ]h"]h$]h&]uh1hhhubh)}(hhh](h)}(hhh]h)}(hhh]h3. Consistency model}(hj1hhhNhNubah}(h]id5ah ]h"]h$]h&]refidconsistency-modeluh1hhj.ubah}(h]h ]h"]h$]h&]uh1hhj+ubh)}(hhh]h)}(hhh]h)}(hhh]h)}(hhh]h93.1 Adding consistency model support to new architectures}(hjPhhhNhNubah}(h]id6ah ]h"]h$]h&]refid5adding-consistency-model-support-to-new-architecturesuh1hhjMubah}(h]h ]h"]h$]h&]uh1hhjJubah}(h]h ]h"]h$]h&]uh1hhjGubah}(h]h ]h"]h$]h&]uh1hhj+ubeh}(h]h ]h"]h$]h&]uh1hhhubh)}(hhh](h)}(hhh]h)}(hhh]h4. Livepatch module}(hj~hhhNhNubah}(h]id7ah ]h"]h$]h&]refidlivepatch-moduleuh1hhj{ubah}(h]h ]h"]h$]h&]uh1hhjxubh)}(hhh](h)}(hhh]h)}(hhh]h)}(hhh]h4.1. New functions}(hjhhhNhNubah}(h]id8ah ]h"]h$]h&]refid new-functionsuh1hhjubah}(h]h ]h"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]uh1hhjubh)}(hhh]h)}(hhh]h)}(hhh]h 4.2. Metadata}(hjhhhNhNubah}(h]id9ah ]h"]h$]h&]refidmetadatauh1hhjubah}(h]h ]h"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]uh1hhjubeh}(h]h ]h"]h$]h&]uh1hhjxubeh}(h]h ]h"]h$]h&]uh1hhhubh)}(hhh](h)}(hhh]h)}(hhh]h5. Livepatch life-cycle}(hjhhhNhNubah}(h]id10ah ]h"]h$]h&]refidlivepatch-life-cycleuh1hhjubah}(h]h ]h"]h$]h&]uh1hhjubh)}(hhh](h)}(hhh]h)}(hhh]h)}(hhh]h 5.1. Loading}(hj hhhNhNubah}(h]id11ah ]h"]h$]h&]refidloadinguh1hhj ubah}(h]h ]h"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]uh1hhjubh)}(hhh]h)}(hhh]h)}(hhh]h 5.2. Enabling}(hj.hhhNhNubah}(h]id12ah ]h"]h$]h&]refidenablinguh1hhj+ubah}(h]h ]h"]h$]h&]uh1hhj(ubah}(h]h ]h"]h$]h&]uh1hhjubh)}(hhh]h)}(hhh]h)}(hhh]h5.3. Replacing}(hjPhhhNhNubah}(h]id13ah ]h"]h$]h&]refid replacinguh1hhjMubah}(h]h ]h"]h$]h&]uh1hhjJubah}(h]h ]h"]h$]h&]uh1hhjubh)}(hhh]h)}(hhh]h)}(hhh]h5.4. Disabling}(hjrhhhNhNubah}(h]id14ah ]h"]h$]h&]refid disablinguh1hhjoubah}(h]h ]h"]h$]h&]uh1hhjlubah}(h]h ]h"]h$]h&]uh1hhjubh)}(hhh]h)}(hhh]h)}(hhh]h 5.5. Removing}(hjhhhNhNubah}(h]id15ah ]h"]h$]h&]refidremovinguh1hhjubah}(h]h ]h"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]uh1hhjubeh}(h]h ]h"]h$]h&]uh1hhjubeh}(h]h ]h"]h$]h&]uh1hhhubh)}(hhh]h)}(hhh]h)}(hhh]h6. Sysfs}(hjhhhNhNubah}(h]id16ah ]h"]h$]h&]refidsysfsuh1hhjubah}(h]h ]h"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]uh1hhhubh)}(hhh]h)}(hhh]h)}(hhh]h7. Limitations}(hjhhhNhNubah}(h]id17ah ]h"]h$]h&]refid limitationsuh1hhjubah}(h]h ]h"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]uh1hhhubeh}(h]h ]h"]h$]h&]uh1hhhhhhNhNubah}(h]contentsah ](contentslocaleh"]contentsah$]h&]uh1hhhhK hhhhubh)}(hhh](h)}(h 1. Motivationh]h 1. Motivation}(hjhhhNhNubah}(h]h ]h"]h$]h&]refidhuh1hhjhhhhhK ubh)}(hXThere are many situations where users are reluctant to reboot a system. It may be because their system is performing complex scientific computations or under heavy load during peak usage. In addition to keeping systems up and running, users want to also have a stable and secure system. Livepatching gives users both by allowing for function calls to be redirected; thus, fixing critical functions without a system reboot.h]hXThere are many situations where users are reluctant to reboot a system. It may be because their system is performing complex scientific computations or under heavy load during peak usage. In addition to keeping systems up and running, users want to also have a stable and secure system. Livepatching gives users both by allowing for function calls to be redirected; thus, fixing critical functions without a system reboot.}(hj"hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubeh}(h]hah ]h"] 1. motivationah$]h&]uh1hhhhhhhhK ubh)}(hhh](h)}(h 2. Kprobes, Ftrace, Livepatchingh]h 2. Kprobes, Ftrace, Livepatching}(hj:hhhNhNubah}(h]h ]h"]h$]h&]j!juh1hhj7hhhhhKubh)}(hThere are multiple mechanisms in the Linux kernel that are directly related to redirection of code execution; namely: kernel probes, function tracing, and livepatching:h]hThere are multiple mechanisms in the Linux kernel that are directly related to redirection of code execution; namely: kernel probes, function tracing, and livepatching:}(hjHhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj7hhubh block_quote)}(hX- The kernel probes are the most generic. The code can be redirected by putting a breakpoint instruction instead of any instruction. - The function tracer calls the code from a predefined location that is close to the function entry point. This location is generated by the compiler using the '-pg' gcc option. - Livepatching typically needs to redirect the code at the very beginning of the function entry before the function parameters or the stack are in any way modified. h]h)}(hhh](h)}(hThe kernel probes are the most generic. The code can be redirected by putting a breakpoint instruction instead of any instruction. h]h)}(hThe kernel probes are the most generic. The code can be redirected by putting a breakpoint instruction instead of any instruction.h]hThe kernel probes are the most generic. The code can be redirected by putting a breakpoint instruction instead of any instruction.}(hjchhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj_ubah}(h]h ]h"]h$]h&]uh1hhj\ubh)}(hThe function tracer calls the code from a predefined location that is close to the function entry point. This location is generated by the compiler using the '-pg' gcc option. h]h)}(hThe function tracer calls the code from a predefined location that is close to the function entry point. This location is generated by the compiler using the '-pg' gcc option.h]hThe function tracer calls the code from a predefined location that is close to the function entry point. This location is generated by the compiler using the ‘-pg’ gcc option.}(hj{hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK!hjwubah}(h]h ]h"]h$]h&]uh1hhj\ubh)}(hLivepatching typically needs to redirect the code at the very beginning of the function entry before the function parameters or the stack are in any way modified. h]h)}(hLivepatching typically needs to redirect the code at the very beginning of the function entry before the function parameters or the stack are in any way modified.h]hLivepatching typically needs to redirect the code at the very beginning of the function entry before the function parameters or the stack are in any way modified.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK%hjubah}(h]h ]h"]h$]h&]uh1hhj\ubeh}(h]h ]h"]h$]h&]bullet-uh1hhhhKhjXubah}(h]h ]h"]h$]h&]uh1jVhhhKhj7hhubh)}(hXAll three approaches need to modify the existing code at runtime. Therefore they need to be aware of each other and not step over each other's toes. Most of these problems are solved by using the dynamic ftrace framework as a base. A Kprobe is registered as a ftrace handler when the function entry is probed, see CONFIG_KPROBES_ON_FTRACE. Also an alternative function from a live patch is called with the help of a custom ftrace handler. But there are some limitations, see below.h]hXAll three approaches need to modify the existing code at runtime. Therefore they need to be aware of each other and not step over each other’s toes. Most of these problems are solved by using the dynamic ftrace framework as a base. A Kprobe is registered as a ftrace handler when the function entry is probed, see CONFIG_KPROBES_ON_FTRACE. Also an alternative function from a live patch is called with the help of a custom ftrace handler. But there are some limitations, see below.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK)hj7hhubeh}(h]jah ]h"] 2. kprobes, ftrace, livepatchingah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h3. Consistency modelh]h3. Consistency model}(hjhhhNhNubah}(h]h ]h"]h$]h&]j!j:uh1hhjhhhhhK3ubh)}(hFunctions are there for a reason. They take some input parameters, acquire or release locks, read, process, and even write some data in a defined way, have return values. In other words, each function has a defined semantic.h]hFunctions are there for a reason. They take some input parameters, acquire or release locks, read, process, and even write some data in a defined way, have return values. In other words, each function has a defined semantic.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK5hjhhubh)}(hXMany fixes do not change the semantic of the modified functions. For example, they add a NULL pointer or a boundary check, fix a race by adding a missing memory barrier, or add some locking around a critical section. Most of these changes are self contained and the function presents itself the same way to the rest of the system. In this case, the functions might be updated independently one by one.h]hXMany fixes do not change the semantic of the modified functions. For example, they add a NULL pointer or a boundary check, fix a race by adding a missing memory barrier, or add some locking around a critical section. Most of these changes are self contained and the function presents itself the same way to the rest of the system. In this case, the functions might be updated independently one by one.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK9hjhhubh)}(hX But there are more complex fixes. For example, a patch might change ordering of locking in multiple functions at the same time. Or a patch might exchange meaning of some temporary structures and update all the relevant functions. In this case, the affected unit (thread, whole kernel) need to start using all new versions of the functions at the same time. Also the switch must happen only when it is safe to do so, e.g. when the affected locks are released or no data are stored in the modified structures at the moment.h]hX But there are more complex fixes. For example, a patch might change ordering of locking in multiple functions at the same time. Or a patch might exchange meaning of some temporary structures and update all the relevant functions. In this case, the affected unit (thread, whole kernel) need to start using all new versions of the functions at the same time. Also the switch must happen only when it is safe to do so, e.g. when the affected locks are released or no data are stored in the modified structures at the moment.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK@hjhhubh)}(hThe theory about how to apply functions a safe way is rather complex. The aim is to define a so-called consistency model. It attempts to define conditions when the new implementation could be used so that the system stays consistent.h]hThe theory about how to apply functions a safe way is rather complex. The aim is to define a so-called consistency model. It attempts to define conditions when the new implementation could be used so that the system stays consistent.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKIhjhhubh)}(hXLivepatch has a consistency model which is a hybrid of kGraft and kpatch: it uses kGraft's per-task consistency and syscall barrier switching combined with kpatch's stack trace switching. There are also a number of fallback options which make it quite flexible.h]hX Livepatch has a consistency model which is a hybrid of kGraft and kpatch: it uses kGraft’s per-task consistency and syscall barrier switching combined with kpatch’s stack trace switching. There are also a number of fallback options which make it quite flexible.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKNhjhhubh)}(hXPatches are applied on a per-task basis, when the task is deemed safe to switch over. When a patch is enabled, livepatch enters into a transition state where tasks are converging to the patched state. Usually this transition state can complete in a few seconds. The same sequence occurs when a patch is disabled, except the tasks converge from the patched state to the unpatched state.h]hXPatches are applied on a per-task basis, when the task is deemed safe to switch over. When a patch is enabled, livepatch enters into a transition state where tasks are converging to the patched state. Usually this transition state can complete in a few seconds. The same sequence occurs when a patch is disabled, except the tasks converge from the patched state to the unpatched state.}(hj!hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKShjhhubh)}(hAn interrupt handler inherits the patched state of the task it interrupts. The same is true for forked tasks: the child inherits the patched state of the parent.h]hAn interrupt handler inherits the patched state of the task it interrupts. The same is true for forked tasks: the child inherits the patched state of the parent.}(hj/hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKZhjhhubh)}(h[Livepatch uses several complementary approaches to determine when it's safe to patch tasks:h]h]Livepatch uses several complementary approaches to determine when it’s safe to patch tasks:}(hj=hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK^hjhhubhenumerated_list)}(hhh](h)}(hXtThe first and most effective approach is stack checking of sleeping tasks. If no affected functions are on the stack of a given task, the task is patched. In most cases this will patch most or all of the tasks on the first try. Otherwise it'll keep trying periodically. This option is only available if the architecture has reliable stacks (HAVE_RELIABLE_STACKTRACE). h]h)}(hXsThe first and most effective approach is stack checking of sleeping tasks. If no affected functions are on the stack of a given task, the task is patched. In most cases this will patch most or all of the tasks on the first try. Otherwise it'll keep trying periodically. This option is only available if the architecture has reliable stacks (HAVE_RELIABLE_STACKTRACE).h]hXuThe first and most effective approach is stack checking of sleeping tasks. If no affected functions are on the stack of a given task, the task is patched. In most cases this will patch most or all of the tasks on the first try. Otherwise it’ll keep trying periodically. This option is only available if the architecture has reliable stacks (HAVE_RELIABLE_STACKTRACE).}(hjThhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKahjPubah}(h]h ]h"]h$]h&]uh1hhjMhhhhhNubh)}(hX The second approach, if needed, is kernel exit switching. A task is switched when it returns to user space from a system call, a user space IRQ, or a signal. It's useful in the following cases: a) Patching I/O-bound user tasks which are sleeping on an affected function. In this case you have to send SIGSTOP and SIGCONT to force it to exit the kernel and be patched. b) Patching CPU-bound user tasks. If the task is highly CPU-bound then it will get patched the next time it gets interrupted by an IRQ. h](h)}(hThe second approach, if needed, is kernel exit switching. A task is switched when it returns to user space from a system call, a user space IRQ, or a signal. It's useful in the following cases:h]hThe second approach, if needed, is kernel exit switching. A task is switched when it returns to user space from a system call, a user space IRQ, or a signal. It’s useful in the following cases:}(hjlhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhjhubjL)}(hhh](h)}(hPatching I/O-bound user tasks which are sleeping on an affected function. In this case you have to send SIGSTOP and SIGCONT to force it to exit the kernel and be patched.h]h)}(hPatching I/O-bound user tasks which are sleeping on an affected function. In this case you have to send SIGSTOP and SIGCONT to force it to exit the kernel and be patched.h]hPatching I/O-bound user tasks which are sleeping on an affected function. In this case you have to send SIGSTOP and SIGCONT to force it to exit the kernel and be patched.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKlhj}ubah}(h]h ]h"]h$]h&]uh1hhjzubh)}(hPatching CPU-bound user tasks. If the task is highly CPU-bound then it will get patched the next time it gets interrupted by an IRQ. h]h)}(hPatching CPU-bound user tasks. If the task is highly CPU-bound then it will get patched the next time it gets interrupted by an IRQ.h]hPatching CPU-bound user tasks. If the task is highly CPU-bound then it will get patched the next time it gets interrupted by an IRQ.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKohjubah}(h]h ]h"]h$]h&]uh1hhjzubeh}(h]h ]h"]h$]h&]enumtype loweralphaprefixhsuffix)uh1jKhjhubeh}(h]h ]h"]h$]h&]uh1hhjMhhhNhNubh)}(hXFor idle "swapper" tasks, since they don't ever exit the kernel, they instead have a klp_update_patch_state() call in the idle loop which allows them to be patched before the CPU enters the idle state. (Note there's not yet such an approach for kthreads.) h](h)}(hFor idle "swapper" tasks, since they don't ever exit the kernel, they instead have a klp_update_patch_state() call in the idle loop which allows them to be patched before the CPU enters the idle state.h]hFor idle “swapper” tasks, since they don’t ever exit the kernel, they instead have a klp_update_patch_state() call in the idle loop which allows them to be patched before the CPU enters the idle state.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKshjubh)}(h5(Note there's not yet such an approach for kthreads.)h]h7(Note there’s not yet such an approach for kthreads.)}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKwhjubeh}(h]h ]h"]h$]h&]uh1hhjMhhhhhNubeh}(h]h ]h"]h$]h&]jarabicjhj.uh1jKhjhhhhhKaubh)}(hXArchitectures which don't have HAVE_RELIABLE_STACKTRACE solely rely on the second approach. It's highly likely that some tasks may still be running with an old version of the function, until that function returns. In this case you would have to signal the tasks. This especially applies to kthreads. They may not be woken up and would need to be forced. See below for more information.h]hXArchitectures which don’t have HAVE_RELIABLE_STACKTRACE solely rely on the second approach. It’s highly likely that some tasks may still be running with an old version of the function, until that function returns. In this case you would have to signal the tasks. This especially applies to kthreads. They may not be woken up and would need to be forced. See below for more information.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKyhjhhubh)}(hUnless we can come up with another way to patch kthreads, architectures without HAVE_RELIABLE_STACKTRACE are not considered fully supported by the kernel livepatching.h]hUnless we can come up with another way to patch kthreads, architectures without HAVE_RELIABLE_STACKTRACE are not considered fully supported by the kernel livepatching.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hThe /sys/kernel/livepatch//transition file shows whether a patch is in transition. Only a single patch can be in transition at a given time. A patch can remain in transition indefinitely, if any of the tasks are stuck in the initial patch state.h]hThe /sys/kernel/livepatch//transition file shows whether a patch is in transition. Only a single patch can be in transition at a given time. A patch can remain in transition indefinitely, if any of the tasks are stuck in the initial patch state.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hA transition can be reversed and effectively canceled by writing the opposite value to the /sys/kernel/livepatch//enabled file while the transition is in progress. Then all the tasks will attempt to converge back to the original patch state.h]hA transition can be reversed and effectively canceled by writing the opposite value to the /sys/kernel/livepatch//enabled file while the transition is in progress. Then all the tasks will attempt to converge back to the original patch state.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hXThere's also a /proc//patch_state file which can be used to determine which tasks are blocking completion of a patching operation. If a patch is in transition, this file shows 0 to indicate the task is unpatched and 1 to indicate it's patched. Otherwise, if no patch is in transition, it shows -1. Any tasks which are blocking the transition can be signaled with SIGSTOP and SIGCONT to force them to change their patched state. This may be harmful to the system though. Sending a fake signal to all remaining blocking tasks is a better alternative. No proper signal is actually delivered (there is no data in signal pending structures). Tasks are interrupted or woken up, and forced to change their patched state. The fake signal is automatically sent every 15 seconds.h]hX There’s also a /proc//patch_state file which can be used to determine which tasks are blocking completion of a patching operation. If a patch is in transition, this file shows 0 to indicate the task is unpatched and 1 to indicate it’s patched. Otherwise, if no patch is in transition, it shows -1. Any tasks which are blocking the transition can be signaled with SIGSTOP and SIGCONT to force them to change their patched state. This may be harmful to the system though. Sending a fake signal to all remaining blocking tasks is a better alternative. No proper signal is actually delivered (there is no data in signal pending structures). Tasks are interrupted or woken up, and forced to change their patched state. The fake signal is automatically sent every 15 seconds.}(hj$hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hXAdministrator can also affect a transition through /sys/kernel/livepatch//force attribute. Writing 1 there clears TIF_PATCH_PENDING flag of all tasks and thus forces the tasks to the patched state. Important note! The force attribute is intended for cases when the transition gets stuck for a long time because of a blocking task. Administrator is expected to collect all necessary data (namely stack traces of such blocking tasks) and request a clearance from a patch distributor to force the transition. Unauthorized usage may cause harm to the system. It depends on the nature of the patch, which functions are (un)patched, and which functions the blocking tasks are sleeping in (/proc//stack may help here). Removal (rmmod) of patch modules is permanently disabled when the force feature is used. It cannot be guaranteed there is no task sleeping in such module. It implies unbounded reference count if a patch module is disabled and enabled in a loop.h]hXAdministrator can also affect a transition through /sys/kernel/livepatch//force attribute. Writing 1 there clears TIF_PATCH_PENDING flag of all tasks and thus forces the tasks to the patched state. Important note! The force attribute is intended for cases when the transition gets stuck for a long time because of a blocking task. Administrator is expected to collect all necessary data (namely stack traces of such blocking tasks) and request a clearance from a patch distributor to force the transition. Unauthorized usage may cause harm to the system. It depends on the nature of the patch, which functions are (un)patched, and which functions the blocking tasks are sleeping in (/proc//stack may help here). Removal (rmmod) of patch modules is permanently disabled when the force feature is used. It cannot be guaranteed there is no task sleeping in such module. It implies unbounded reference count if a patch module is disabled and enabled in a loop.}(hj2hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hXMoreover, the usage of force may also affect future applications of live patches and cause even more harm to the system. Administrator should first consider to simply cancel a transition (see above). If force is used, reboot should be planned and no more live patches applied.h]hXMoreover, the usage of force may also affect future applications of live patches and cause even more harm to the system. Administrator should first consider to simply cancel a transition (see above). If force is used, reboot should be planned and no more live patches applied.}(hj@hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hhh](h)}(h93.1 Adding consistency model support to new architecturesh]h93.1 Adding consistency model support to new architectures}(hjQhhhNhNubah}(h]h ]h"]h$]h&]j!jYuh1hhjNhhhhhKubh)}(hSFor adding consistency model support to new architectures, there are a few options:h]hSFor adding consistency model support to new architectures, there are a few options:}(hj_hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjNhhubjL)}(hhh](h)}(hAdd CONFIG_HAVE_RELIABLE_STACKTRACE. This means porting objtool, and for non-DWARF unwinders, also making sure there's a way for the stack tracing code to detect interrupts on the stack. h]h)}(hAdd CONFIG_HAVE_RELIABLE_STACKTRACE. This means porting objtool, and for non-DWARF unwinders, also making sure there's a way for the stack tracing code to detect interrupts on the stack.h]hAdd CONFIG_HAVE_RELIABLE_STACKTRACE. This means porting objtool, and for non-DWARF unwinders, also making sure there’s a way for the stack tracing code to detect interrupts on the stack.}(hjthhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjpubah}(h]h ]h"]h$]h&]uh1hhjmhhhhhNubh)}(hXAlternatively, ensure that every kthread has a call to klp_update_patch_state() in a safe location. Kthreads are typically in an infinite loop which does some action repeatedly. The safe location to switch the kthread's patch state would be at a designated point in the loop where there are no locks taken and all data structures are in a well-defined state. The location is clear when using workqueues or the kthread worker API. These kthreads process independent actions in a generic loop. It's much more complicated with kthreads which have a custom loop. There the safe location must be carefully selected on a case-by-case basis. In that case, arches without HAVE_RELIABLE_STACKTRACE would still be able to use the non-stack-checking parts of the consistency model: a) patching user tasks when they cross the kernel/user space boundary; and b) patching kthreads and idle tasks at their designated patch points. This option isn't as good as option 1 because it requires signaling user tasks and waking kthreads to patch them. But it could still be a good backup option for those architectures which don't have reliable stack traces yet. h](h)}(hXhAlternatively, ensure that every kthread has a call to klp_update_patch_state() in a safe location. Kthreads are typically in an infinite loop which does some action repeatedly. The safe location to switch the kthread's patch state would be at a designated point in the loop where there are no locks taken and all data structures are in a well-defined state.h]hXjAlternatively, ensure that every kthread has a call to klp_update_patch_state() in a safe location. Kthreads are typically in an infinite loop which does some action repeatedly. The safe location to switch the kthread’s patch state would be at a designated point in the loop where there are no locks taken and all data structures are in a well-defined state.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubh)}(hThe location is clear when using workqueues or the kthread worker API. These kthreads process independent actions in a generic loop.h]hThe location is clear when using workqueues or the kthread worker API. These kthreads process independent actions in a generic loop.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubh)}(hIt's much more complicated with kthreads which have a custom loop. There the safe location must be carefully selected on a case-by-case basis.h]hIt’s much more complicated with kthreads which have a custom loop. There the safe location must be carefully selected on a case-by-case basis.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubh)}(hIn that case, arches without HAVE_RELIABLE_STACKTRACE would still be able to use the non-stack-checking parts of the consistency model:h]hIn that case, arches without HAVE_RELIABLE_STACKTRACE would still be able to use the non-stack-checking parts of the consistency model:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubjL)}(hhh](h)}(hHpatching user tasks when they cross the kernel/user space boundary; and h]h)}(hGpatching user tasks when they cross the kernel/user space boundary; andh]hGpatching user tasks when they cross the kernel/user space boundary; and}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1hhjubh)}(hCpatching kthreads and idle tasks at their designated patch points. h]h)}(hBpatching kthreads and idle tasks at their designated patch points.h]hBpatching kthreads and idle tasks at their designated patch points.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1hhjubeh}(h]h ]h"]h$]h&]jjjhjjuh1jKhjubh)}(hThis option isn't as good as option 1 because it requires signaling user tasks and waking kthreads to patch them. But it could still be a good backup option for those architectures which don't have reliable stack traces yet.h]hThis option isn’t as good as option 1 because it requires signaling user tasks and waking kthreads to patch them. But it could still be a good backup option for those architectures which don’t have reliable stack traces yet.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubeh}(h]h ]h"]h$]h&]uh1hhjmhhhhhNubeh}(h]h ]h"]h$]h&]jjjhjjuh1jKhjNhhhhhKubeh}(h]j_ah ]h"]93.1 adding consistency model support to new architecturesah$]h&]uh1hhjhhhhhKubeh}(h]j@ah ]h"]3. consistency modelah$]h&]uh1hhhhhhhhK3ubh)}(hhh](h)}(h4. Livepatch moduleh]h4. Livepatch module}(hj(hhhNhNubah}(h]h ]h"]h$]h&]j!juh1hhj%hhhhhKubh)}(h[Livepatches are distributed using kernel modules, see samples/livepatch/livepatch-sample.c.h]h[Livepatches are distributed using kernel modules, see samples/livepatch/livepatch-sample.c.}(hj6hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj%hhubh)}(hXThe module includes a new implementation of functions that we want to replace. In addition, it defines some structures describing the relation between the original and the new implementation. Then there is code that makes the kernel start using the new code when the livepatch module is loaded. Also there is code that cleans up before the livepatch module is removed. All this is explained in more details in the next sections.h]hXThe module includes a new implementation of functions that we want to replace. In addition, it defines some structures describing the relation between the original and the new implementation. Then there is code that makes the kernel start using the new code when the livepatch module is loaded. Also there is code that cleans up before the livepatch module is removed. All this is explained in more details in the next sections.}(hjDhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj%hhubh)}(hhh](h)}(h4.1. New functionsh]h4.1. New functions}(hjUhhhNhNubah}(h]h ]h"]h$]h&]j!juh1hhjRhhhhhKubh)}(hX=New versions of functions are typically just copied from the original sources. A good practice is to add a prefix to the names so that they can be distinguished from the original ones, e.g. in a backtrace. Also they can be declared as static because they are not called directly and do not need the global visibility.h]hX=New versions of functions are typically just copied from the original sources. A good practice is to add a prefix to the names so that they can be distinguished from the original ones, e.g. in a backtrace. Also they can be declared as static because they are not called directly and do not need the global visibility.}(hjchhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjRhhubh)}(hXHThe patch contains only functions that are really modified. But they might want to access functions or data from the original source file that may only be locally accessible. This can be solved by a special relocation section in the generated livepatch module, see Documentation/livepatch/module-elf-format.rst for more details.h]hXHThe patch contains only functions that are really modified. But they might want to access functions or data from the original source file that may only be locally accessible. This can be solved by a special relocation section in the generated livepatch module, see Documentation/livepatch/module-elf-format.rst for more details.}(hjqhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjRhhubeh}(h]jah ]h"]4.1. new functionsah$]h&]uh1hhj%hhhhhKubh)}(hhh](h)}(h 4.2. Metadatah]h 4.2. Metadata}(hjhhhNhNubah}(h]h ]h"]h$]h&]j!juh1hhjhhhhhKubh)}(hZThe patch is described by several structures that split the information into three levels:h]hZThe patch is described by several structures that split the information into three levels:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubjW)}(hX- struct klp_func is defined for each patched function. It describes the relation between the original and the new implementation of a particular function. The structure includes the name, as a string, of the original function. The function address is found via kallsyms at runtime. Then it includes the address of the new function. It is defined directly by assigning the function pointer. Note that the new function is typically defined in the same source file. As an optional parameter, the symbol position in the kallsyms database can be used to disambiguate functions of the same name. This is not the absolute position in the database, but rather the order it has been found only for a particular object ( vmlinux or a kernel module ). Note that kallsyms allows for searching symbols according to the object name. - struct klp_object defines an array of patched functions (struct klp_func) in the same object. Where the object is either vmlinux (NULL) or a module name. The structure helps to group and handle functions for each object together. Note that patched modules might be loaded later than the patch itself and the relevant functions might be patched only when they are available. - struct klp_patch defines an array of patched objects (struct klp_object). This structure handles all patched functions consistently and eventually, synchronously. The whole patch is applied only when all patched symbols are found. The only exception are symbols from objects (kernel modules) that have not been loaded yet. For more details on how the patch is applied on a per-task basis, see the "Consistency model" section. h]h)}(hhh](h)}(hX5struct klp_func is defined for each patched function. It describes the relation between the original and the new implementation of a particular function. The structure includes the name, as a string, of the original function. The function address is found via kallsyms at runtime. Then it includes the address of the new function. It is defined directly by assigning the function pointer. Note that the new function is typically defined in the same source file. As an optional parameter, the symbol position in the kallsyms database can be used to disambiguate functions of the same name. This is not the absolute position in the database, but rather the order it has been found only for a particular object ( vmlinux or a kernel module ). Note that kallsyms allows for searching symbols according to the object name. h](h)}(hstruct klp_func is defined for each patched function. It describes the relation between the original and the new implementation of a particular function.h]hstruct klp_func is defined for each patched function. It describes the relation between the original and the new implementation of a particular function.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubh)}(h~The structure includes the name, as a string, of the original function. The function address is found via kallsyms at runtime.h]h~The structure includes the name, as a string, of the original function. The function address is found via kallsyms at runtime.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubh)}(hThen it includes the address of the new function. It is defined directly by assigning the function pointer. Note that the new function is typically defined in the same source file.h]hThen it includes the address of the new function. It is defined directly by assigning the function pointer. Note that the new function is typically defined in the same source file.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubh)}(hXcAs an optional parameter, the symbol position in the kallsyms database can be used to disambiguate functions of the same name. This is not the absolute position in the database, but rather the order it has been found only for a particular object ( vmlinux or a kernel module ). Note that kallsyms allows for searching symbols according to the object name.h]hXcAs an optional parameter, the symbol position in the kallsyms database can be used to disambiguate functions of the same name. This is not the absolute position in the database, but rather the order it has been found only for a particular object ( vmlinux or a kernel module ). Note that kallsyms allows for searching symbols according to the object name.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubeh}(h]h ]h"]h$]h&]uh1hhjubh)}(hXxstruct klp_object defines an array of patched functions (struct klp_func) in the same object. Where the object is either vmlinux (NULL) or a module name. The structure helps to group and handle functions for each object together. Note that patched modules might be loaded later than the patch itself and the relevant functions might be patched only when they are available. h](h)}(hstruct klp_object defines an array of patched functions (struct klp_func) in the same object. Where the object is either vmlinux (NULL) or a module name.h]hstruct klp_object defines an array of patched functions (struct klp_func) in the same object. Where the object is either vmlinux (NULL) or a module name.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjubh)}(hThe structure helps to group and handle functions for each object together. Note that patched modules might be loaded later than the patch itself and the relevant functions might be patched only when they are available.h]hThe structure helps to group and handle functions for each object together. Note that patched modules might be loaded later than the patch itself and the relevant functions might be patched only when they are available.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjubeh}(h]h ]h"]h$]h&]uh1hhjubh)}(hXstruct klp_patch defines an array of patched objects (struct klp_object). This structure handles all patched functions consistently and eventually, synchronously. The whole patch is applied only when all patched symbols are found. The only exception are symbols from objects (kernel modules) that have not been loaded yet. For more details on how the patch is applied on a per-task basis, see the "Consistency model" section. h](h)}(hIstruct klp_patch defines an array of patched objects (struct klp_object).h]hIstruct klp_patch defines an array of patched objects (struct klp_object).}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubh)}(hThis structure handles all patched functions consistently and eventually, synchronously. The whole patch is applied only when all patched symbols are found. The only exception are symbols from objects (kernel modules) that have not been loaded yet.h]hThis structure handles all patched functions consistently and eventually, synchronously. The whole patch is applied only when all patched symbols are found. The only exception are symbols from objects (kernel modules) that have not been loaded yet.}(hj&hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubh)}(hfFor more details on how the patch is applied on a per-task basis, see the "Consistency model" section.h]hjFor more details on how the patch is applied on a per-task basis, see the “Consistency model” section.}(hj4hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubeh}(h]h ]h"]h$]h&]uh1hhjubeh}(h]h ]h"]h$]h&]jjuh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jVhhhKhjhhubeh}(h]jah ]h"] 4.2. metadataah$]h&]uh1hhj%hhhhhKubeh}(h]jah ]h"]4. livepatch moduleah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h5. Livepatch life-cycleh]h5. Livepatch life-cycle}(hjehhhNhNubah}(h]h ]h"]h$]h&]j!juh1hhjbhhhhhM ubh)}(hjLivepatching can be described by five basic operations: loading, enabling, replacing, disabling, removing.h]hjLivepatching can be described by five basic operations: loading, enabling, replacing, disabling, removing.}(hjshhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM"hjbhhubh)}(hWhere the replacing and the disabling operations are mutually exclusive. They have the same result for the given patch but not for the system.h]hWhere the replacing and the disabling operations are mutually exclusive. They have the same result for the given patch but not for the system.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM%hjbhhubh)}(hhh](h)}(h 5.1. Loadingh]h 5.1. Loading}(hjhhhNhNubah}(h]h ]h"]h$]h&]j!juh1hhjhhhhhM+ubh)}(hThe only reasonable way is to enable the patch when the livepatch kernel module is being loaded. For this, klp_enable_patch() has to be called in the module_init() callback. There are two main reasons:h]hThe only reasonable way is to enable the patch when the livepatch kernel module is being loaded. For this, klp_enable_patch() has to be called in the module_init() callback. There are two main reasons:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM-hjhhubh)}(hJFirst, only the module has an easy access to the related struct klp_patch.h]hJFirst, only the module has an easy access to the related struct klp_patch.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM1hjhhubh)}(hdSecond, the error code might be used to refuse loading the module when the patch cannot get enabled.h]hdSecond, the error code might be used to refuse loading the module when the patch cannot get enabled.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM3hjhhubeh}(h]jah ]h"] 5.1. loadingah$]h&]uh1hhjbhhhhhM+ubh)}(hhh](h)}(h 5.2. Enablingh]h 5.2. Enabling}(hjhhhNhNubah}(h]h ]h"]h$]h&]j!j7uh1hhjhhhhhM8ubh)}(hThe livepatch gets enabled by calling klp_enable_patch() from the module_init() callback. The system will start using the new implementation of the patched functions at this stage.h]hThe livepatch gets enabled by calling klp_enable_patch() from the module_init() callback. The system will start using the new implementation of the patched functions at this stage.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM:hjhhubh)}(hXFirst, the addresses of the patched functions are found according to their names. The special relocations, mentioned in the section "New functions", are applied. The relevant entries are created under /sys/kernel/livepatch/. The patch is rejected when any above operation fails.h]hX First, the addresses of the patched functions are found according to their names. The special relocations, mentioned in the section “New functions”, are applied. The relevant entries are created under /sys/kernel/livepatch/. The patch is rejected when any above operation fails.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM>hjhhubh)}(hXSecond, livepatch enters into a transition state where tasks are converging to the patched state. If an original function is patched for the first time, a function specific struct klp_ops is created and an universal ftrace handler is registered\ [#]_. This stage is indicated by a value of '1' in /sys/kernel/livepatch//transition. For more information about this process, see the "Consistency model" section.h](hSecond, livepatch enters into a transition state where tasks are converging to the patched state. If an original function is patched for the first time, a function specific struct klp_ops is created and an universal ftrace handler is registered }(hjhhhNhNubhfootnote_reference)}(h[#]_h]h1}(hjhhhNhNubah}(h]id1ah ]h"]h$]h&]autoKj!id2docnamelivepatch/livepatchuh1jhjresolvedKubh. This stage is indicated by a value of ‘1’ in /sys/kernel/livepatch//transition. For more information about this process, see the “Consistency model” section.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMDhjhhubh)}(hQFinally, once all tasks have been patched, the 'transition' value changes to '0'.h]hYFinally, once all tasks have been patched, the ‘transition’ value changes to ‘0’.}(hj&hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMKhjhhubhfootnote)}(hX Note that functions might be patched multiple times. The ftrace handler is registered only once for a given function. Further patches just add an entry to the list (see field `func_stack`) of the struct klp_ops. The right implementation is selected by the ftrace handler, see the "Consistency model" section. That said, it is highly recommended to use cumulative livepatches because they help keeping the consistency of all changes. In this case, functions might be patched two times only during the transition period. h](hlabel)}(hhh]h1}(hj<hhhNhNubah}(h]h ]h"]h$]h&]uh1j:hj6hhhNhNubh)}(hX4Note that functions might be patched multiple times. The ftrace handler is registered only once for a given function. Further patches just add an entry to the list (see field `func_stack`) of the struct klp_ops. The right implementation is selected by the ftrace handler, see the "Consistency model" section.h](hNote that functions might be patched multiple times. The ftrace handler is registered only once for a given function. Further patches just add an entry to the list (see field }(hjIhhhNhNubhtitle_reference)}(h `func_stack`h]h func_stack}(hjShhhNhNubah}(h]h ]h"]h$]h&]uh1jQhjIubh}) of the struct klp_ops. The right implementation is selected by the ftrace handler, see the “Consistency model” section.}(hjIhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMPhj6ubh)}(hThat said, it is highly recommended to use cumulative livepatches because they help keeping the consistency of all changes. In this case, functions might be patched two times only during the transition period.h]hThat said, it is highly recommended to use cumulative livepatches because they help keeping the consistency of all changes. In this case, functions might be patched two times only during the transition period.}(hjkhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMVhj6ubeh}(h]jah ]h"]1ah$]h&]jajKjjuh1j4hhhMNhjhhubeh}(h]j=ah ]h"] 5.2. enablingah$]h&]uh1hhjbhhhhhM8ubh)}(hhh](h)}(h5.3. Replacingh]h5.3. Replacing}(hjhhhNhNubah}(h]h ]h"]h$]h&]j!jYuh1hhjhhhhhM\ubh)}(h\All enabled patches might get replaced by a cumulative patch that has the .replace flag set.h]h\All enabled patches might get replaced by a cumulative patch that has the .replace flag set.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM^hjhhubh)}(hXbOnce the new patch is enabled and the 'transition' finishes then all the functions (struct klp_func) associated with the replaced patches are removed from the corresponding struct klp_ops. Also the ftrace handler is unregistered and the struct klp_ops is freed when the related function is not modified by the new patch and func_stack list becomes empty.h]hXfOnce the new patch is enabled and the ‘transition’ finishes then all the functions (struct klp_func) associated with the replaced patches are removed from the corresponding struct klp_ops. Also the ftrace handler is unregistered and the struct klp_ops is freed when the related function is not modified by the new patch and func_stack list becomes empty.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMahjhhubh)}(hDSee Documentation/livepatch/cumulative-patches.rst for more details.h]hDSee Documentation/livepatch/cumulative-patches.rst for more details.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhhjhhubeh}(h]j_ah ]h"]5.3. replacingah$]h&]uh1hhjbhhhhhM\ubh)}(hhh](h)}(h5.4. Disablingh]h5.4. Disabling}(hjhhhNhNubah}(h]h ]h"]h$]h&]j!j{uh1hhjhhhhhMlubh)}(hZEnabled patches might get disabled by writing '0' to /sys/kernel/livepatch//enabled.h]h^Enabled patches might get disabled by writing ‘0’ to /sys/kernel/livepatch//enabled.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMnhjhhubh)}(hXjFirst, livepatch enters into a transition state where tasks are converging to the unpatched state. The system starts using either the code from the previously enabled patch or even the original one. This stage is indicated by a value of '1' in /sys/kernel/livepatch//transition. For more information about this process, see the "Consistency model" section.h]hXrFirst, livepatch enters into a transition state where tasks are converging to the unpatched state. The system starts using either the code from the previously enabled patch or even the original one. This stage is indicated by a value of ‘1’ in /sys/kernel/livepatch//transition. For more information about this process, see the “Consistency model” section.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMqhjhhubh)}(hX=Second, once all tasks have been unpatched, the 'transition' value changes to '0'. All the functions (struct klp_func) associated with the to-be-disabled patch are removed from the corresponding struct klp_ops. The ftrace handler is unregistered and the struct klp_ops is freed when the func_stack list becomes empty.h]hXESecond, once all tasks have been unpatched, the ‘transition’ value changes to ‘0’. All the functions (struct klp_func) associated with the to-be-disabled patch are removed from the corresponding struct klp_ops. The ftrace handler is unregistered and the struct klp_ops is freed when the func_stack list becomes empty.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMxhjhhubh)}(h(Third, the sysfs interface is destroyed.h]h(Third, the sysfs interface is destroyed.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM~hjhhubeh}(h]jah ]h"]5.4. disablingah$]h&]uh1hhjbhhhhhMlubh)}(hhh](h)}(h 5.5. Removingh]h 5.5. Removing}(hj hhhNhNubah}(h]h ]h"]h$]h&]j!juh1hhj hhhhhMubh)}(hXQModule removal is only safe when there are no users of functions provided by the module. This is the reason why the force feature permanently disables the removal. Only when the system is successfully transitioned to a new patch state (patched/unpatched) without being forced it is guaranteed that no task sleeps or runs in the old code.h]hXQModule removal is only safe when there are no users of functions provided by the module. This is the reason why the force feature permanently disables the removal. Only when the system is successfully transitioned to a new patch state (patched/unpatched) without being forced it is guaranteed that no task sleeps or runs in the old code.}(hj* hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubeh}(h]jah ]h"] 5.5. removingah$]h&]uh1hhjbhhhhhMubeh}(h]jah ]h"]5. livepatch life-cycleah$]h&]uh1hhhhhhhhM ubh)}(hhh](h)}(h6. Sysfsh]h6. Sysfs}(hjI hhhNhNubah}(h]h ]h"]h$]h&]j!juh1hhjF hhhhhMubh)}(hInformation about the registered patches can be found under /sys/kernel/livepatch. The patches could be enabled and disabled by writing there.h]hInformation about the registered patches can be found under /sys/kernel/livepatch. The patches could be enabled and disabled by writing there.}(hjW hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjF hhubh)}(hb/sys/kernel/livepatch//force attributes allow administrator to affect a patching operation.h]hb/sys/kernel/livepatch//force attributes allow administrator to affect a patching operation.}(hje hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjF hhubh)}(hFSee Documentation/ABI/testing/sysfs-kernel-livepatch for more details.h]hFSee Documentation/ABI/testing/sysfs-kernel-livepatch for more details.}(hjs hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjF hhubeh}(h]jah ]h"]6. sysfsah$]h&]uh1hhhhhhhhMubh)}(hhh](h)}(h7. Limitationsh]h7. Limitations}(hj hhhNhNubah}(h]h ]h"]h$]h&]j!juh1hhj hhhhhMubh)}(h=The current Livepatch implementation has several limitations:h]h=The current Livepatch implementation has several limitations:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubjW)}(hX- Only functions that can be traced could be patched. Livepatch is based on the dynamic ftrace. In particular, functions implementing ftrace or the livepatch ftrace handler could not be patched. Otherwise, the code would end up in an infinite loop. A potential mistake is prevented by marking the problematic functions by "notrace". - Livepatch works reliably only when the dynamic ftrace is located at the very beginning of the function. The function need to be redirected before the stack or the function parameters are modified in any way. For example, livepatch requires using -fentry gcc compiler option on x86_64. One exception is the PPC port. It uses relative addressing and TOC. Each function has to handle TOC and save LR before it could call the ftrace handler. This operation has to be reverted on return. Fortunately, the generic ftrace code has the same problem and all this is handled on the ftrace level. - Kretprobes using the ftrace framework conflict with the patched functions. Both kretprobes and livepatches use a ftrace handler that modifies the return address. The first user wins. Either the probe or the patch is rejected when the handler is already in use by the other. - Kprobes in the original function are ignored when the code is redirected to the new implementation. There is a work in progress to add warnings about this situation.h]h)}(hhh](h)}(hXNOnly functions that can be traced could be patched. Livepatch is based on the dynamic ftrace. In particular, functions implementing ftrace or the livepatch ftrace handler could not be patched. Otherwise, the code would end up in an infinite loop. A potential mistake is prevented by marking the problematic functions by "notrace". h](h)}(h3Only functions that can be traced could be patched.h]h3Only functions that can be traced could be patched.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ubh)}(hXLivepatch is based on the dynamic ftrace. In particular, functions implementing ftrace or the livepatch ftrace handler could not be patched. Otherwise, the code would end up in an infinite loop. A potential mistake is prevented by marking the problematic functions by "notrace".h]hXLivepatch is based on the dynamic ftrace. In particular, functions implementing ftrace or the livepatch ftrace handler could not be patched. Otherwise, the code would end up in an infinite loop. A potential mistake is prevented by marking the problematic functions by “notrace”.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ubeh}(h]h ]h"]h$]h&]uh1hhj ubh)}(hXMLivepatch works reliably only when the dynamic ftrace is located at the very beginning of the function. The function need to be redirected before the stack or the function parameters are modified in any way. For example, livepatch requires using -fentry gcc compiler option on x86_64. One exception is the PPC port. It uses relative addressing and TOC. Each function has to handle TOC and save LR before it could call the ftrace handler. This operation has to be reverted on return. Fortunately, the generic ftrace code has the same problem and all this is handled on the ftrace level. h](h)}(hgLivepatch works reliably only when the dynamic ftrace is located at the very beginning of the function.h]hgLivepatch works reliably only when the dynamic ftrace is located at the very beginning of the function.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ubh)}(hThe function need to be redirected before the stack or the function parameters are modified in any way. For example, livepatch requires using -fentry gcc compiler option on x86_64.h]hThe function need to be redirected before the stack or the function parameters are modified in any way. For example, livepatch requires using -fentry gcc compiler option on x86_64.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ubh)}(hX,One exception is the PPC port. It uses relative addressing and TOC. Each function has to handle TOC and save LR before it could call the ftrace handler. This operation has to be reverted on return. Fortunately, the generic ftrace code has the same problem and all this is handled on the ftrace level.h]hX,One exception is the PPC port. It uses relative addressing and TOC. Each function has to handle TOC and save LR before it could call the ftrace handler. This operation has to be reverted on return. Fortunately, the generic ftrace code has the same problem and all this is handled on the ftrace level.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ubeh}(h]h ]h"]h$]h&]uh1hhj ubh)}(hXKretprobes using the ftrace framework conflict with the patched functions. Both kretprobes and livepatches use a ftrace handler that modifies the return address. The first user wins. Either the probe or the patch is rejected when the handler is already in use by the other. h](h)}(hJKretprobes using the ftrace framework conflict with the patched functions.h]hJKretprobes using the ftrace framework conflict with the patched functions.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ub*h)}(hBoth kretprobes and livepatches use a ftrace handler that modifies the return address. The first user wins. Either the probe or the patch is rejected when the handler is already in use by the other.h]hBoth kretprobes and livepatches use a ftrace handler that modifies the return address. The first user wins. Either the probe or the patch is rejected when the handler is already in use by the other.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ubeh}(h]h ]h"]h$]h&]uh1hhj ubh)}(hKprobes in the original function are ignored when the code is redirected to the new implementation. There is a work in progress to add warnings about this situation.h](h)}(hcKprobes in the original function are ignored when the code is redirected to the new implementation.h]hcKprobes in the original function are ignored when the code is redirected to the new implementation.}(hj2 hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj. ubh)}(hAThere is a work in progress to add warnings about this situation.h]hAThere is a work in progress to add warnings about this situation.}(hj@ hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj. ubeh}(h]h ]h"]h$]h&]uh1hhj ubeh}(h]h ]h"]h$]h&]jjuh1hhhhMhj ubah}(h]h ]h"]h$]h&]uh1jVhhhMhj hhubeh}(h]jah ]h"]7. limitationsah$]h&]uh1hhhhhhhhMubeh}(h] livepatchah ]h"] livepatchah$]h&]uh1hhhhhhhhKubeh}(h]h ]h"]h$]h&]sourcehuh1hcurrent_sourceN current_lineNsettingsdocutils.frontendValues)}(hN generatorN datestampN source_linkN source_urlN toc_backlinksentryfootnote_backlinksK sectnum_xformKstrip_commentsNstrip_elements_with_classesN strip_classesN report_levelK halt_levelKexit_status_levelKdebugNwarning_streamN tracebackinput_encoding utf-8-siginput_encoding_error_handlerstrictoutput_encodingutf-8output_encoding_error_handlerj error_encodingutf-8error_encoding_error_handlerbackslashreplace language_codeenrecord_dependenciesNconfigN id_prefixhauto_id_prefixid dump_settingsNdump_internalsNdump_transformsNdump_pseudo_xmlNexpose_internalsNstrict_visitorN_disable_configN_sourceh _destinationN _config_files]7/var/lib/git/docbuild/linux/Documentation/docutils.confafile_insertion_enabled raw_enabledKline_length_limitM'pep_referencesN pep_base_urlhttps://peps.python.org/pep_file_url_templatepep-%04drfc_referencesN rfc_base_url&https://datatracker.ietf.org/doc/html/ tab_widthKtrim_footnote_reference_spacesyntax_highlightlong smart_quotessmartquotes_locales]character_level_inline_markupdoctitle_xform docinfo_xformKsectsubtitle_xform image_loadinglinkembed_stylesheetcloak_email_addressessection_self_linkenvNubreporterNindirect_targets]substitution_defs}substitution_names}refnames}refids}j]jasnameids}(jl ji j jj4hjjj"j@jj_j_jjjjXjjC jjjjj=jj_j jj< jj jjd jj}ju nametypes}(jl j j4jj"jj_jjXjC jjjj j< j jd j}uh}(ji hjhhjjj7j@jj_jNjj%jjRjjjjbjjj=jjjjj6j_jjjjj jjF jj hhjjj:j1jYjPjj~jjjjjjjj j7j.jYjPj{jrjjjjjju footnote_refs} citation_refs} autofootnotes]j6aautofootnote_refs]jasymbol_footnotes]symbol_footnote_refs] footnotes] citations]autofootnote_startKsymbol_footnote_startK id_counter collectionsCounter}j KsRparse_messages]transform_messages] transformerN include_log] decorationNhhub.