[RFC PATCH v2 5/6] kcov: add interrupt context guard to kcov_df_write()

Yunseong Kim <[email protected]>
Newsgroups org.kernel.vger.workflows,dev.linux.lists.llvm,org.kernel.vger.linux-doc,org.kernel.vger.linux-kbuild,org.kernel.vger.linux-kernel,org.kernel.vger.rust-for-linux
Message-ID <[email protected]>
The KCOV-Dataflow write path (kcov_df_write) only checks
t->kcov_df_enabled before writing to the shared ring buffer. Unlike
the standard KCOV check_kcov_mode() which rejects interrupt context,
kcov_df_write() has no such protection. This means instrumented code
running in hardirq, softirq, or NMI context that interrupts a task
mid-write can re-enter kcov_df_write(), causing:

 - Data corruption in the ring buffer (interleaved records)
 - Out-of-order sequence counter increments
 - Potential faults from nested pointer dereferences

Add an in_task() check to reject calls from non-task context, matching
the safety model of the standard KCOV tracing path.

Also suppress -Wmissing-prototypes in the eight_args_c test module
Makefile, as the exported test functions intentionally lack a shared
header.

Signed-off-by: Yunseong Kim <[email protected]>
---
 kernel/kcov.c                             | 4 ++++
 tools/kcov-dataflow/eight_args_c/Makefile | 1 +
 2 files changed, 5 insertions(+)

diff --git a/kernel/kcov.c b/kernel/kcov.c
index d3c9c0efe961..373b8034ca5c 100644
--- a/kernel/kcov.c
+++ b/kernel/kcov.c
@@ -409,6 +409,10 @@ kcov_df_write(u64 type_marker, u64 pc, u64 meta, void *ptr,
 	if (!t->kcov_df_enabled)
 		return;
 
+	/* Reject calls from hardirq/softirq/NMI to prevent reentrant corruption. */
+	if (!in_task())
+		return;
+
 	area = (u64 *)t->kcov_df_area;
 	if (!area)
 		return;
diff --git a/tools/kcov-dataflow/eight_args_c/Makefile b/tools/kcov-dataflow/eight_args_c/Makefile
index de35bb541f07..038775b49435 100644
--- a/tools/kcov-dataflow/eight_args_c/Makefile
+++ b/tools/kcov-dataflow/eight_args_c/Makefile
@@ -1,2 +1,3 @@
 obj-m := eight_args_mod.o
 KCOV_DATAFLOW_eight_args_mod.o := y
+ccflags-y += -Wno-missing-prototypes

-- 
2.43.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.