Skip to contentSkip to conversation

kcov: fix data corruption and race conditions on PREEMPT_RT by moving saved remote state to task_struct (v7)

PATCH New Alexander Potapenko 2 participants 2 messages Jul 20

Subsystems (3)
lore
Share & tools
lore

Alexander Potapenko

author reviewer
#0 lore raw

PATCH v7 1/1 kcov: fix data corruption and race conditions on PREEMPT_RT by moving saved remote state to task_struct (v7)

syzbot is reporting KCOV state corruption on PREEMPT_RT kernels, for the temporary storage used for saving/restoring remote KCOV state is currently allocated as the per-CPU area.

On PREEMPT_RT kernels, softirq handlers run as preemptible task threads (e.g., ksoftirqd). If a softirq context preempts a task running a remote KCOV session, it safely saves the task's state into the per-CPU area. However, if that softirq thread is subsequently preempted by a higher-priority softirq thread on the same CPU, the second softirq will overwrite the same per-CPU area, permanently destroying the original task's KCOV state.

Fix this data corruption by moving the temporary storage from the per-CPU area to the per-thread area. Since each softirq thread now owns its own task context, nested softirq preemption no longer causes data overwrites.

Note that while the temporary storage is now on a per-thread basis, the per-CPU kcov_percpu_data.lock must be retained, for we need to ensure that kcov_remote_start() and kcov_remote_stop() operate atomically without racing against asynchronous interrupts that manipulate the current task's KCOV state.

Fixes 5ff3b30ab57d ("kcov: collect coverage from interrupts")
---
 include/linux/sched.h |  8 ++++
 kernel/kcov.c         | 90 ++++++++++++++++++++++---------------------
 lib/Kconfig.debug     |  5 ++-
 3 files changed, 58 insertions(+), 45 deletions(-)
include/linux/sched.h +8 -0
index 373bcc0598d1..7a53c15cecb5 100644
@@ -1543,6 +1543,14 @@ struct task_struct {
 
 	/* Collect coverage from softirq context: */
 	unsigned int			kcov_softirq;
+
+	/* Temporary storage for preempting remote coverage collection: */
+	unsigned int			kcov_saved_mode;
+	unsigned int			kcov_saved_size;
+	void				*kcov_saved_area;
+	struct kcov			*kcov_saved_kcov;
+	int				kcov_saved_sequence;
+
 #endif
 
 #ifdef CONFIG_MEMCG_V1
kernel/kcov.c +47 -43

169 lines not on this page · raw message

lib/Kconfig.debug +3 -2
index 1244dcac2294..12786379bf1d 100644
@@ -2247,10 +2247,11 @@ config KCOV_INSTRUMENT_ALL
 config KCOV_IRQ_AREA_SIZE
 	hex "Size of interrupt coverage collection area in words"
 	depends on KCOV
+	range 0x80 0x1000000
 	default 0x40000
 	help
-	  KCOV uses preallocated per-cpu areas to collect coverage from
-	  soft interrupts. This specifies the size of those areas in the
+	  KCOV uses preallocated areas to collect coverage from soft
+	  interrupts. This specifies the size of those areas in the
 	  number of unsigned long words.
 
 config KCOV_SELFTEST
Signature · 1 line
2.55.0.229.g6434b31f56-goog
syzbot Tested-by [email protected] on v7 1/1
#1 lore raw
syzbot ci has tested the following series

syzbot ci has tested the following series

[v7] kcov: fix data corruption and race conditions on PREEMPT_RT by moving saved remote state to task_struct (v7)
https://lore.kernel.org/all/[email protected]
* [PATCH v7] kcov: fix data corruption and race conditions on PREEMPT_RT by moving saved remote state to task_struct (v7)

and found no issues.

Full report is available here:
https://ci.syzbot.org/series/33930a89-bb2a-4c5a-b6c5-d45829d5c94c

***

If these findings have caused you to resend the series or submit a separate fix, please add the following tag to your commit message:

---
This report is generated by a bot. It may contain errors. syzbot ci engineers can be reached at [email protected].