eBPF-based security platform
kernel-obs: document the preemption model of the test VM's kernel
Daniel Borkmann4efd6885fa8a
diff --git a/AGENTS.md b/AGENTS.mdindex 01a829de..e3c7c1e7 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -403,9 +403,10 @@ one usually still compiles and passes tests.and update a per-CPU counter there with an atomic. Elsewhere, such as acounter only one `kprobe` program updates, the plain form is right. Aupatch whose correctness depends on the model says in its `README.md`- which one it is written for, and its code is checked against that one.- The test kernel is built with `PREEMPT_LAZY` alone, so the VM suite- shows the preemption cases only for `syscall` and sleepable programs.+ which one it is written for, as kernel-obs does for the test VM's+ kernel, and its code is checked against that one. The test kernel is+ built with `PREEMPT_LAZY` alone, so the VM suite shows the preemption+ cases only for `syscall` and sleepable programs.The `bpf_` rules in `testing/coccinelle/` refuse these patterns withinone function. No rule refuses per-CPU scratch, as whether it is adiff --git a/Documentation/upatches/write/memory-consistency/hazards.rst b/Documentation/upatches/write/memory-consistency/hazards.rstindex 5f2c2fc5..dcfa4d22 100644--- a/Documentation/upatches/write/memory-consistency/hazards.rst+++ b/Documentation/upatches/write/memory-consistency/hazards.rst@@ -88,7 +88,8 @@ a hazard where it applies and not elsewhere: a busy flag or an atomicthat no interleaving needs costs time on every run and hides thereasoning behind the code that does need one. An upatch whosecorrectness depends on the preemption model says in its ``README.md``-which model it is written for.+which model it is written for, as ``kernel-obs`` does for the test VM's+kernel... list-table:::header-rows: 1diff --git a/upatches/kernel-obs/README.md b/upatches/kernel-obs/README.mdindex 74ee43de..e654df35 100644--- a/upatches/kernel-obs/README.md+++ b/upatches/kernel-obs/README.md@@ -150,6 +150,37 @@ Reading a table drains it, exactly as the map-export timer does; add`--peek` to look at the interval being collected without taking itfrom the exporter.+## Preemption model++`kernel-obs` is written for the preemption model of the kernel the test+VM runs: `CONFIG_PREEMPT_LAZY` alone, without `CONFIG_PREEMPT_DYNAMIC`+and so without preemptible RCU (`CONFIG_PREEMPT_RCU`). The VM suite is+where the package is loaded and its tables are checked, in CI and in a+local `make tests`, so its concurrency is reviewed against the kernel+that suite runs rather than against one no test exercises.++On that kernel the `lsm` and `cgroup_skb` programs run with preemption+disabled, so no other task's run starts on a CPU before one ends; a+softirq can still run an ingress program on top of an egress one, and+the sleepable `lsm.s` programs can sleep. The DNS parser builds a query+in a per-CPU scratch buffer without a guard: only egress runs use it,+and those come from the task that sends the query, so on that kernel+none starts before another ends. Runs on different CPUs share the arena+tables on every kernel, so the tables take and count their entries with+atomics, publish a buffer with a release store, and an export expects+late writes into the buffer it drains.++A kernel with preemptible RCU, which `CONFIG_PREEMPT`,+`CONFIG_PREEMPT_RT` and `CONFIG_PREEMPT_DYNAMIC` bring, is outside that+model. `PREEMPT_DYNAMIC` is the kernel's default and what distribution+kernels use, and since 7.0 it boots only in its `full` or `lazy` mode;+before, distribution kernels mostly booted it as `voluntary`, which does+not preempt a running program. On such a kernel another task's egress+run can preempt a DNS query parse on the same CPU and overwrite the+scratch, so that a DNS record carries the name of another query. The+record is wrong, nothing more: `kernel-obs` observes and decides+nothing.+## LimitsDNS over TCP, TLS and HTTPS is not parsed; the resolvers are on thediff --git a/upatches/kernel-obs/obs_dns.bpf.c b/upatches/kernel-obs/obs_dns.bpf.cindex 889b510c..951f7999 100644--- a/upatches/kernel-obs/obs_dns.bpf.c+++ b/upatches/kernel-obs/obs_dns.bpf.c@@ -546,6 +546,11 @@ static __always_inline bool dns_query_ident(struct __sk_buff *skb,return obs_ident_checked(ident, obs_now());}+/*+ * The scratch is per CPU and unguarded: kernel-obs is written for a kernel+ * without preemptible RCU, where no egress run starts on a CPU before+ * another one there ends.+ */static __always_inline void dns_parse_query_payload(struct __sk_buff *skb,__u32 dns_off,struct dns_request_key *req_key)