FAILED: patch "[PATCH] kcov: fix data corruption and race conditions on PREEMPT_RT" failed to apply to 5.15-stable tree
Subsystems (4)
[email protected]
maintainerThe patch below does not apply to the 5.15-stable tree.
If someone wants it applied there, or to any other stable or longterm tree, then please email the backport, including the original git commit id to <[email protected]>.
To reproduce the conflict and resubmit, you may use the following commands:
git fetch https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/ linux-5.15.y
git checkout FETCH_HEAD
git cherry-pick -x 2eed77fdcb0cc48e8eccb2bcd4b7f2c6d650e84c
# <resolve conflicts, build, test, etc.>
git commit -s
git send-email --to '<[email protected]>' --in-reply-to '2026082427-contour-certify-9d3c@gregkh' --subject-prefix 'PATCH 5.15.y' 'HEAD^..'
Possible dependencies:
thanks,
greg k-h
------------------ original commit in Linus's tree ------------------
From 2eed77fdcb0cc48e8eccb2bcd4b7f2c6d650e84c Mon Sep 17 00:00:00 2001
From: Tetsuo Handa <[email protected]>
Date: Thu, 16 Jul 2026 08:01:29 +0900
Subject: [PATCH] kcov: fix data corruption and race conditions on PREEMPT_RT
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.
It is likely that GFP_KERNEL allocation by vmalloc_node() in kcov_init() has already called panic() before returning NULL, for there will be no OOM-killable userspace processes when __init function of built-in module runs. But this patch also fixes crashing the kernel when vmalloc_node() in kcov_init() returned NULL, for kcov_init() left per-CPU irq_area == NULL but kcov_remote_start() depends on per-CPU irq_area != NULL, resulting in
(1) doing vmalloc() in kcov_remote_start() despite !in_task() context
(2) out-of-array-bounds access if (1) succeeded but
kcov->remote_size < CONFIG_KCOV_IRQ_AREA_SIZE
(3) always leak memory allocated by (1), eventually killing all OOM-killable userspace processes
problems.
include/linux/sched.h +8 -0
@@ -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
@@ -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
Sasha Levin
maintainerPATCH 1/2 kcov: replace local_irq_save() with a local_lock_t
From: Sebastian Andrzej Siewior <[email protected]>
[ Upstream commit d5d2c51f1e5f56ed01d2c773974630c007e5e5f5 ]
The kcov code mixes local_irq_save() and spin_lock() in kcov_remote_{start|end}(). This creates a warning on PREEMPT_RT because local_irq_save() disables interrupts and spin_lock_t is turned into a sleeping lock which can not be acquired in a section with disabled interrupts.
The kcov_remote_lock is used to synchronize the access to the hash-list kcov_remote_map. The local_irq_save() block protects access to the per-CPU data kcov_percpu_data.
There is no compelling reason to change the lock type to raw_spin_lock_t to make it work with local_irq_save(). Changing it would require to move memory allocation (in kcov_remote_add()) and deallocation outside of the locked section.
Adding an unlimited amount of entries to the hashlist will increase the IRQ-off time during lookup. It could be argued that this is debug code and the latency does not matter. There is however no need to do so and it would allow to use this facility in an RT enabled build.
Using a local_lock_t instead of local_irq_save() has the befit of adding a protection scope within the source which makes it obvious what is protected. On a !PREEMPT_RT && !LOCKDEP build the local_lock_irqsave() maps directly to local_irq_save() so there is overhead at runtime.
Replace the local_irq_save() section with a local_lock_t.
Stable-dep-of: 2eed77fdcb0c ("kcov: fix data corruption and race conditions on PREEMPT_RT")
--- kernel/kcov.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-)
kernel/kcov.c +18 -14
@@ -88,6 +88,7 @@ static struct list_head kcov_remote_areas = LIST_HEAD_INIT(kcov_remote_areas);struct kcov_percpu_data {void *irq_area; +local_lock_t lock;unsigned int saved_mode;unsigned int saved_size;
@@ -96,7 +97,9 @@ struct kcov_percpu_data {int saved_sequence; }; -static DEFINE_PER_CPU(struct kcov_percpu_data, kcov_percpu_data); +static DEFINE_PER_CPU(struct kcov_percpu_data, kcov_percpu_data) = { + .lock =INIT_LOCAL_LOCK (lock), +};/* Must be called with kcov_remote_lock locked. */ static struct kcov_remote *kcov_remote_find (u64 handle)
@@ -834,7 +837,7 @@ void kcov_remote_start(u64 handle)if (!in_task () && !in_softirq_really ())return ; -local_irq_save (flags); +local_lock_irqsave (&kcov_percpu_data.lock, flags);/* * Check that kcov_remote_start() is not called twice in background
@@ -842,7 +845,7 @@ void kcov_remote_start(u64 handle)*/ mode =READ_ONCE (t->kcov_mode);if (WARN_ON (in_task () &&kcov_mode_enabled (mode))) { -local_irq_restore (flags); +local_unlock_irqrestore (&kcov_percpu_data.lock, flags);return ; }/*
@@ -851,14 +854,15 @@ void kcov_remote_start(u64 handle)* happened while collecting coverage from a background thread. */ if (WARN_ON (in_serving_softirq () && t->kcov_softirq)) { -local_irq_restore (flags); +local_unlock_irqrestore (&kcov_percpu_data.lock, flags);return ; }spin_lock (&kcov_remote_lock); remote =kcov_remote_find (handle);if (!remote) { -spin_unlock_irqrestore (&kcov_remote_lock, flags); +spin_unlock (&kcov_remote_lock); +local_unlock_irqrestore (&kcov_percpu_data.lock, flags);return ; } kcov_debug("handle = %llx, context: %s\n", handle,
@@ -879,19 +883,19 @@ void kcov_remote_start(u64 handle) size =CONFIG_KCOV_IRQ_AREA_SIZE ; area =this_cpu_ptr (&kcov_percpu_data)->irq_area; } -spin_unlock_irqrestore (&kcov_remote_lock, flags); +spin_unlock (&kcov_remote_lock);/* Can only happen when in_task(). */ if (!area) { +local_unlock_irqrestore (&kcov_percpu_data.lock, flags); area =vmalloc (size *sizeof (unsigned long ));if (!area) {kcov_put (kcov);return ; } +local_lock_irqsave (&kcov_percpu_data.lock, flags); } -local_irq_save (flags); -/* Reset coverage size. */ *(u64 *)area =0 ;
@@ -901,7 +905,7 @@ void kcov_remote_start(u64 handle) }kcov_start (t, kcov, size, area, mode, sequence); -local_irq_restore (flags); +local_unlock_irqrestore (&kcov_percpu_data.lock, flags); }EXPORT_SYMBOL (kcov_remote_start);
@@ -975,12 +979,12 @@ void kcov_remote_stop(void)if (!in_task () && !in_softirq_really ())return ; -local_irq_save (flags); +local_lock_irqsave (&kcov_percpu_data.lock, flags); mode =READ_ONCE (t->kcov_mode);barrier ();if (!kcov_mode_enabled (mode)) { -local_irq_restore (flags); +local_unlock_irqrestore (&kcov_percpu_data.lock, flags);return ; }/*
@@ -988,12 +992,12 @@ void kcov_remote_stop(void)* actually found the remote handle and started collecting coverage. */ if (in_serving_softirq () && !t->kcov_softirq) { -local_irq_restore (flags); +local_unlock_irqrestore (&kcov_percpu_data.lock, flags);return ; }/* Make sure that kcov_softirq is only set when in softirq. */ if (WARN_ON (!in_serving_softirq () && t->kcov_softirq)) { -local_irq_restore (flags); +local_unlock_irqrestore (&kcov_percpu_data.lock, flags);return ; }
@@ -1023,7 +1027,7 @@ void kcov_remote_stop(void)spin_unlock (&kcov_remote_lock); } -local_irq_restore (flags); +local_unlock_irqrestore (&kcov_percpu_data.lock, flags);/* Get in kcov_remote_start(). */ kcov_put (kcov);
Signature · 1 line
2.53.0
Sasha Levin
maintainerPATCH 2/2 kcov: fix data corruption and race conditions on PREEMPT_RT
From: Tetsuo Handa <[email protected]>
[ Upstream commit 2eed77fdcb0cc48e8eccb2bcd4b7f2c6d650e84c ]
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.
It is likely that GFP_KERNEL allocation by vmalloc_node() in kcov_init() has already called panic() before returning NULL, for there will be no OOM-killable userspace processes when __init function of built-in module runs. But this patch also fixes crashing the kernel when vmalloc_node() in kcov_init() returned NULL, for kcov_init() left per-CPU irq_area == NULL but kcov_remote_start() depends on per-CPU irq_area != NULL, resulting in
(1) doing vmalloc() in kcov_remote_start() despite !in_task() context
(2) out-of-array-bounds access if (1) succeeded but
kcov->remote_size < CONFIG_KCOV_IRQ_AREA_SIZE
(3) always leak memory allocated by (1), eventually killing all OOM-killable userspace processes
problems.
--- 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
@@ -1398,6 +1398,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
kernel/kcov.c +47 -43
169 lines not on this page · raw message
lib/Kconfig.debug +3 -2
@@ -2032,10 +2032,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. menuconfig RUNTIME_TESTING_MENU
Signature · 1 line
2.53.0