From: Alexander Potapenko <glider@google.com>
To: glider@google.com
Cc: quic_jiangenj@quicinc.com, linux-kernel@vger.kernel.org,
kasan-dev@googlegroups.com, Aleksandr Nogikh <nogikh@google.com>,
Andrey Konovalov <andreyknvl@gmail.com>,
Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
Dmitry Vyukov <dvyukov@google.com>,
Ingo Molnar <mingo@redhat.com>,
Josh Poimboeuf <jpoimboe@kernel.org>,
Marco Elver <elver@google.com>,
Peter Zijlstra <peterz@infradead.org>,
Thomas Gleixner <tglx@linutronix.de>
Subject: [PATCH 4/7] kcov: add `trace` and `trace_size` to `struct kcov_state`
Date: Wed, 16 Apr 2025 10:54:42 +0200 [thread overview]
Message-ID: <20250416085446.480069-5-glider@google.com> (raw)
In-Reply-To: <20250416085446.480069-1-glider@google.com>
Keep kcov_state.area as the pointer to the memory buffer used by
kcov and shared with the userspace. Store the pointer to the trace
(part of the buffer holding sequential events) separately, as we will
be splitting that buffer in multiple parts.
No functional change so far.
Signed-off-by: Alexander Potapenko <glider@google.com>
---
include/linux/kcov-state.h | 9 ++++++-
kernel/kcov.c | 54 ++++++++++++++++++++++----------------
2 files changed, 39 insertions(+), 24 deletions(-)
diff --git a/include/linux/kcov-state.h b/include/linux/kcov-state.h
index 4c4688d01c616..6e576173fd442 100644
--- a/include/linux/kcov-state.h
+++ b/include/linux/kcov-state.h
@@ -15,9 +15,16 @@ struct kcov_state {
struct {
/* Size of the area (in long's). */
unsigned int size;
+ /*
+ * Pointer to user-provided memory used by kcov. This memory may
+ * contain multiple buffers.
+ */
+ void *area;
+ /* Size of the trace (in long's). */
+ unsigned int trace_size;
/* Buffer for coverage collection, shared with the userspace. */
- void *area;
+ unsigned long *trace;
/*
* KCOV sequence number: incremented each time kcov is
diff --git a/kernel/kcov.c b/kernel/kcov.c
index b97f429d17436..7b726fd761c1b 100644
--- a/kernel/kcov.c
+++ b/kernel/kcov.c
@@ -193,11 +193,11 @@ static notrace unsigned long canonicalize_ip(unsigned long ip)
return ip;
}
-static void sanitizer_cov_write_subsequent(unsigned long *area, int size,
+static void sanitizer_cov_write_subsequent(unsigned long *trace, int size,
unsigned long ip)
{
/* The first 64-bit word is the number of subsequent PCs. */
- unsigned long pos = READ_ONCE(area[0]) + 1;
+ unsigned long pos = READ_ONCE(trace[0]) + 1;
if (likely(pos < size)) {
/*
@@ -207,9 +207,9 @@ static void sanitizer_cov_write_subsequent(unsigned long *area, int size,
* overitten by the recursive __sanitizer_cov_trace_pc().
* Update pos before writing pc to avoid such interleaving.
*/
- WRITE_ONCE(area[0], pos);
+ WRITE_ONCE(trace[0], pos);
barrier();
- area[pos] = ip;
+ trace[pos] = ip;
}
}
@@ -223,8 +223,8 @@ void notrace __sanitizer_cov_trace_pc(void)
if (!check_kcov_mode(KCOV_MODE_TRACE_PC, current))
return;
- sanitizer_cov_write_subsequent(current->kcov_state.s.area,
- current->kcov_state.s.size,
+ sanitizer_cov_write_subsequent(current->kcov_state.s.trace,
+ current->kcov_state.s.trace_size,
canonicalize_ip(_RET_IP_));
}
EXPORT_SYMBOL(__sanitizer_cov_trace_pc);
@@ -234,8 +234,8 @@ void notrace __sanitizer_cov_trace_pc_guard(u32 *guard)
if (!check_kcov_mode(KCOV_MODE_TRACE_PC, current))
return;
- sanitizer_cov_write_subsequent(current->kcov_state.s.area,
- current->kcov_state.s.size,
+ sanitizer_cov_write_subsequent(current->kcov_state.s.trace,
+ current->kcov_state.s.trace_size,
canonicalize_ip(_RET_IP_));
}
EXPORT_SYMBOL(__sanitizer_cov_trace_pc_guard);
@@ -250,9 +250,9 @@ EXPORT_SYMBOL(__sanitizer_cov_trace_pc_guard_init);
#ifdef CONFIG_KCOV_ENABLE_COMPARISONS
static void notrace write_comp_data(u64 type, u64 arg1, u64 arg2, u64 ip)
{
- struct task_struct *t;
- u64 *area;
u64 count, start_index, end_pos, max_pos;
+ struct task_struct *t;
+ u64 *trace;
t = current;
if (!check_kcov_mode(KCOV_MODE_TRACE_CMP, t))
@@ -264,22 +264,22 @@ static void notrace write_comp_data(u64 type, u64 arg1, u64 arg2, u64 ip)
* We write all comparison arguments and types as u64.
* The buffer was allocated for t->kcov_state.size unsigned longs.
*/
- area = (u64 *)t->kcov_state.s.area;
+ trace = (u64 *)t->kcov_state.s.trace;
max_pos = t->kcov_state.s.size * sizeof(unsigned long);
- count = READ_ONCE(area[0]);
+ count = READ_ONCE(trace[0]);
/* Every record is KCOV_WORDS_PER_CMP 64-bit words. */
start_index = 1 + count * KCOV_WORDS_PER_CMP;
end_pos = (start_index + KCOV_WORDS_PER_CMP) * sizeof(u64);
if (likely(end_pos <= max_pos)) {
/* See comment in sanitizer_cov_write_subsequent(). */
- WRITE_ONCE(area[0], count + 1);
+ WRITE_ONCE(trace[0], count + 1);
barrier();
- area[start_index] = type;
- area[start_index + 1] = arg1;
- area[start_index + 2] = arg2;
- area[start_index + 3] = ip;
+ trace[start_index] = type;
+ trace[start_index + 1] = arg1;
+ trace[start_index + 2] = arg2;
+ trace[start_index + 3] = ip;
}
}
@@ -380,11 +380,13 @@ static void kcov_start(struct task_struct *t, struct kcov *kcov,
static void kcov_stop(struct task_struct *t)
{
+ int saved_sequence = t->kcov_state.s.sequence;
+
WRITE_ONCE(t->kcov_state.mode, KCOV_MODE_DISABLED);
barrier();
t->kcov = NULL;
- t->kcov_state.s.size = 0;
- t->kcov_state.s.area = NULL;
+ t->kcov_state.s = (typeof(t->kcov_state.s)){ 0 };
+ t->kcov_state.s.sequence = saved_sequence;
}
static void kcov_task_reset(struct task_struct *t)
@@ -733,6 +735,8 @@ static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
}
kcov->state.s.area = area;
kcov->state.s.size = size;
+ kcov->state.s.trace = area;
+ kcov->state.s.trace_size = size;
kcov->state.mode = KCOV_MODE_INIT;
spin_unlock_irqrestore(&kcov->lock, flags);
return 0;
@@ -924,10 +928,12 @@ void kcov_remote_start(u64 handle)
local_lock_irqsave(&kcov_percpu_data.lock, flags);
}
- /* Reset coverage size. */
- *(u64 *)area = 0;
state.s.area = area;
state.s.size = size;
+ state.s.trace = area;
+ state.s.trace_size = size;
+ /* Reset coverage size. */
+ state.s.trace[0] = 0;
if (in_serving_softirq()) {
kcov_remote_softirq_start(t);
@@ -1000,8 +1006,8 @@ void kcov_remote_stop(void)
struct task_struct *t = current;
struct kcov *kcov;
unsigned int mode;
- void *area;
- unsigned int size;
+ void *area, *trace;
+ unsigned int size, trace_size;
int sequence;
unsigned long flags;
@@ -1033,6 +1039,8 @@ void kcov_remote_stop(void)
kcov = t->kcov;
area = t->kcov_state.s.area;
size = t->kcov_state.s.size;
+ trace = t->kcov_state.s.trace;
+ trace_size = t->kcov_state.s.trace_size;
sequence = t->kcov_state.s.sequence;
kcov_stop(t);
--
2.49.0.604.gff1f9ca942-goog
next prev parent reply other threads:[~2025-04-16 8:55 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-16 8:54 [PATCH 0/7] RFC: coverage deduplication for KCOV Alexander Potapenko
2025-04-16 8:54 ` [PATCH 1/7] kcov: apply clang-format to kcov code Alexander Potapenko
2025-04-17 16:47 ` Marco Elver
2025-06-18 14:23 ` Alexander Potapenko
2025-04-16 8:54 ` [PATCH 2/7] kcov: factor out struct kcov_state Alexander Potapenko
2025-04-17 16:42 ` Marco Elver
2025-06-18 17:41 ` Alexander Potapenko
2025-06-25 16:36 ` Alexander Potapenko
2025-04-16 8:54 ` [PATCH 3/7] kcov: x86: introduce CONFIG_KCOV_ENABLE_GUARDS Alexander Potapenko
2025-04-17 19:43 ` Marco Elver
2025-04-22 5:27 ` Joey Jiao
2025-04-24 13:58 ` Alexander Potapenko
2025-04-24 14:03 ` Marco Elver
2025-04-16 8:54 ` Alexander Potapenko [this message]
2025-04-16 8:54 ` [PATCH 5/7] kcov: add ioctl(KCOV_UNIQUE_ENABLE) Alexander Potapenko
2025-04-22 9:29 ` Marco Elver
2025-04-23 13:08 ` Alexander Potapenko
2025-04-23 13:16 ` Alexander Potapenko
2025-04-23 13:30 ` Alexander Potapenko
2025-04-30 2:21 ` Joey Jiao
2025-04-30 6:32 ` Alexander Potapenko
2025-04-16 8:54 ` [PATCH 6/7] x86: objtool: add support for R_X86_64_REX_GOTPCRELX Alexander Potapenko
2025-04-16 14:21 ` Uros Bizjak
2025-04-17 15:37 ` Alexander Potapenko
2025-04-17 15:49 ` Ard Biesheuvel
2025-04-16 8:54 ` [PATCH 7/7] mm/kasan: define __asan_before_dynamic_init, __asan_after_dynamic_init Alexander Potapenko
2025-04-22 6:46 ` Marco Elver
2025-04-22 8:02 ` Alexander Potapenko
2025-04-16 10:52 ` [PATCH 0/7] RFC: coverage deduplication for KCOV Dmitry Vyukov
2025-04-17 4:04 ` Joey Jiao
2025-04-17 12:43 ` Alexander Potapenko
2025-04-17 8:13 ` Alexander Potapenko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250416085446.480069-5-glider@google.com \
--to=glider@google.com \
--cc=andreyknvl@gmail.com \
--cc=bp@alien8.de \
--cc=dave.hansen@linux.intel.com \
--cc=dvyukov@google.com \
--cc=elver@google.com \
--cc=jpoimboe@kernel.org \
--cc=kasan-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=nogikh@google.com \
--cc=peterz@infradead.org \
--cc=quic_jiangenj@quicinc.com \
--cc=tglx@linutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®