# Watching Linux live patch transitions with eBPF

Kernel live patching lets a server take a security fix without a reboot. A live patch is a kernel module that carries new versions of the functions a fix changes, and the kernel redirects calls from the old functions to the new ones. Ubuntu, Red Hat Enterprise Linux, SUSE and other distributions ship their fixes this way.

Loading the module is only the start. Between the moment a patch is enabled and the moment it is fully in place, the kernel runs a *transition*, and that transition can be fast, slow or stuck. Most monitoring sees none of it. In this post we walk through what a transition is, what holds one up, and how we watch it with klp-watch, an open-source tool we wrote for the job.

## What a transition is

The kernel cannot swap a function while a task might be in the middle of running it. If a task were halfway through the old version and returned into the new one, it could mix two versions of the same code path. So the kernel moves tasks to the new code one at a time, and only when it is safe for each task.

Every thread carries its own patch state, visible in `/proc/<pid>/task/<tid>/patch_state`. A thread switches when it is not inside any function the patch changes, which the kernel checks from the thread's stack, or when it returns to user space. Once every thread has switched, the transition is complete and the patch reports `transition` 0 in `/sys/kernel/livepatch/<patch>/`.

The kernel functions behind this have plain names. `klp_enable_patch` starts the work, `klp_init_transition` and `klp_start_transition` set it up, `klp_try_complete_transition` checks whether every task has switched, and `klp_complete_transition` finishes. If some tasks have not switched yet, the kernel tries again about once a second.

## What holds a transition up

A task that sleeps inside one of the patched functions cannot switch until it wakes up and leaves that function. A kernel worker thread that waits in a loop is the classic case. While it sleeps there, the transition stays open, the patch reports `enabled` 1 and `transition` 1, and only part of the system runs the new code.

Most transitions finish in milliseconds or seconds, so nobody notices. A transition that stays open for minutes is worth knowing about, and so is the name of the thread that holds it.

## Watching it

klp-watch attaches eBPF kprobes to the kernel functions above and reads sysfs and the per-thread patch state. In its trace mode it prints one JSON line per event. Here is the kernel's own `livepatch-sample` being enabled on Ubuntu 24.04 (6.8.0-147-generic). We left out the probe lines.

```json
{"event":"klp","function":"klp_enable_patch","pid":6339,"comm":"insmod"}
{"event":"klp","function":"klp_init_transition","pid":6339,"comm":"insmod","target":"patched"}
{"event":"sysfs","patch":"livepatch_sample","present":true,"enabled":"0","transition":"1"}
{"event":"klp","function":"klp_start_transition","pid":6339,"comm":"insmod"}
{"event":"klp","function":"klp_try_complete_transition","pid":6339,"comm":"insmod"}
{"event":"klp","function":"klp_complete_transition","pid":6339,"comm":"insmod"}
{"event":"sysfs","patch":"livepatch_sample","present":true,"enabled":"1","transition":"0"}
{"event":"klp","function":"klp_enable_patch","pid":6339,"comm":"insmod","ret":0}
```

(Timestamps are trimmed here; every line carries the kernel's monotonic time and the wall time.) Every task switched on the first attempt, so `insmod` completed the transition itself, 262 ms after it started.

To see a transition held up, we use two modules from the kernel's own live patch selftests. `test_klp_callbacks_busy` keeps a kernel worker sleeping inside a function, and `test_klp_callbacks_demo` patches that function. Once the transition has run for two seconds, klp-watch reads every thread's patch state and reports the ones still on the old code, with their kernel stacks:

```json
{"event":"blocked","patch":"test_klp_callbacks_demo","target":"patched","transition_ms":2999,
 "tasks":[{"pid":4777,"comm":"kworker/0:0+events","patch_state":0,"wchan":"msleep",
 "stack":["msleep+0x2d/0x50","busymod_work_func+0x35/0x70 [test_klp_callbacks_busy]","process_one_work+0x184/0x3a0", "..."]}]}
```

The stack says exactly why the transition is waiting. The worker sleeps in `msleep`, called from `busymod_work_func`, which is the function being patched. When the worker is released, klp-watch prints an empty list and the transition completes.

## From events to alerts

A trace helps while you are looking. On a fleet we want the machine to tell us. klp-watch has two more modes for that.

`klp-watch status` reads sysfs once, prints a report and exits with the codes monitoring systems expect, 0 for OK, 1 for WARNING, 2 for CRITICAL and 3 for UNKNOWN. It needs no root. A transition that runs longer than a limit (ten minutes by default) is critical, and a live patch module that stays loaded after its patch was disabled or replaced is a warning, because its fixes are no longer in effect. Each finding comes with a short hint on what to do:

```text
LIVEPATCH CRITICAL - test_klp_callbacks_demo has been patching for 6 s (limit 5 s); some tasks have not switched
  what to do: Run `klp-watch --stacks` to see which tasks hold it up and where they sleep; the kernel keeps retrying.
  Forcing the transition skips the consistency check and is a last resort.
```

`klp-watch agent` runs as a service. It traces with eBPF, checks status every few seconds, and raises an alert once when a condition starts or ends. The conditions are a stalled transition, a forced one, a patch that failed to enable, and a module left without its patch. Alerts go to the systemd journal at the right priority, to a JSON log for tools such as Wazuh, to Prometheus metrics, and, if you want, to Slack, Microsoft Teams, a generic webhook or mail. A normal transition is logged but not notified, because every patch goes through one.

Both modes work without eBPF. `status` never uses it, and the agent falls back to reading sysfs and /proc where eBPF is not allowed. eBPF adds what leaves no trace there, a patch that failed to enable and a forced transition, and the exact time of each step without polling.

The repository has Prometheus alert rules with unit tests, a Grafana dashboard, Wazuh rules and systemd units.

## How we test it

Every claim above comes from scripts in the repository that load the kernel's sample and selftest live patches in a throwaway virtual machine and fail unless the output is what it should be. For v0.2 we ran them on Linux 5.15 (Ubuntu 22.04), 6.8 (Ubuntu 24.04), 6.12 (Debian 13) and 7.0 (Ubuntu 26.04).

klp-watch only observes. It never changes a patch. It sees every patch that uses the kernel's live patch interface, which covers modules built with kpatch, klp-build and klp-ccp. Oracle Ksplice works differently and is not seen.

## Try it

The code, the test scripts and the results for each kernel are at [github.com/kazuru-chidumbwe/klp-watch](https://github.com/kazuru-chidumbwe/klp-watch). Version 0.2.0 is the release described here. We would like to hear how it behaves on your kernels and what you would want it to report next.
