linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexander Potapenko <glider@google.com>
To: glider@google.com
Cc: quic_jiangenj@quicinc.com, linux-kernel@vger.kernel.org,
	 kasan-dev@googlegroups.com, Dmitry Vyukov <dvyukov@google.com>,
	 Aleksandr Nogikh <nogikh@google.com>,
	Andrey Konovalov <andreyknvl@gmail.com>,
	Borislav Petkov <bp@alien8.de>,
	 Dave Hansen <dave.hansen@linux.intel.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 v3 02/10] kcov: elaborate on using the shared buffer
Date: Mon, 28 Jul 2025 17:25:40 +0200	[thread overview]
Message-ID: <20250728152548.3969143-3-glider@google.com> (raw)
In-Reply-To: <20250728152548.3969143-1-glider@google.com>

Add a paragraph about the shared buffer usage to kcov.rst.

Signed-off-by: Alexander Potapenko <glider@google.com>
Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
---
v3:
 - add Reviewed-by: Dmitry Vyukov

Change-Id: Ia47ef7c3fcc74789fe57a6e1d93e29a42dbc0a97
---
 Documentation/dev-tools/kcov.rst | 55 ++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)

diff --git a/Documentation/dev-tools/kcov.rst b/Documentation/dev-tools/kcov.rst
index 6611434e2dd24..abf3ad2e784e8 100644
--- a/Documentation/dev-tools/kcov.rst
+++ b/Documentation/dev-tools/kcov.rst
@@ -137,6 +137,61 @@ mmaps coverage buffer, and then forks child processes in a loop. The child
 processes only need to enable coverage (it gets disabled automatically when
 a thread exits).
 
+Shared buffer for coverage collection
+-------------------------------------
+KCOV employs a shared memory buffer as a central mechanism for efficient and
+direct transfer of code coverage information between the kernel and userspace
+applications.
+
+Calling ``ioctl(fd, KCOV_INIT_TRACE, size)`` initializes coverage collection for
+the current thread associated with the file descriptor ``fd``. The buffer
+allocated will hold ``size`` unsigned long values, as interpreted by the kernel.
+Notably, even in a 32-bit userspace program on a 64-bit kernel, each entry will
+occupy 64 bits.
+
+Following initialization, the actual shared memory buffer is created using::
+
+    mmap(NULL, size * sizeof(unsigned long), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)
+
+The size of this memory mapping, calculated as ``size * sizeof(unsigned long)``,
+must be a multiple of ``PAGE_SIZE``.
+
+This buffer is then shared between the kernel and the userspace. The first
+element of the buffer contains the number of PCs stored in it.
+Both the userspace and the kernel may write to the shared buffer, so to avoid
+race conditions each userspace thread should only update its own buffer.
+
+Normally the shared buffer is used as follows::
+
+              Userspace                                         Kernel
+    -----------------------------------------+-------------------------------------------
+    ioctl(fd, KCOV_INIT_TRACE, size)         |
+                                             |    Initialize coverage for current thread
+    mmap(..., MAP_SHARED, fd, 0)             |
+                                             |    Allocate the buffer, initialize it
+                                             |    with zeroes
+    ioctl(fd, KCOV_ENABLE, KCOV_TRACE_PC)    |
+                                             |    Enable PC collection for current thread
+                                             |    starting at buffer[1] (KCOV_ENABLE will
+                                             |    already write some coverage)
+    Atomically write 0 to buffer[0] to       |
+    reset the coverage                       |
+                                             |
+    Execute some syscall(s)                  |
+                                             |    Write new coverage starting at
+                                             |    buffer[1]
+    Atomically read buffer[0] to get the     |
+    total coverage size at this point in     |
+    time                                     |
+                                             |
+    ioctl(fd, KCOV_DISABLE, 0)               |
+                                             |    Write some more coverage for ioctl(),
+                                             |    then disable PC collection for current
+                                             |    thread
+    Safely read and process the coverage     |
+    up to the buffer[0] value saved above    |
+
+
 Comparison operands collection
 ------------------------------
 
-- 
2.50.1.470.g6ba607880d-goog


  parent reply	other threads:[~2025-07-28 15:26 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-28 15:25 [PATCH v3 00/10] Coverage deduplication for KCOV Alexander Potapenko
2025-07-28 15:25 ` [PATCH v3 01/10] x86: kcov: disable instrumentation of arch/x86/kernel/tsc.c Alexander Potapenko
2025-07-28 15:25 ` Alexander Potapenko [this message]
2025-07-28 15:25 ` [PATCH v3 03/10] kcov: factor out struct kcov_state Alexander Potapenko
2025-07-29 11:09   ` Dmitry Vyukov
2025-07-28 15:25 ` [PATCH v3 04/10] mm/kasan: define __asan_before_dynamic_init, __asan_after_dynamic_init Alexander Potapenko
2025-07-29 11:43   ` kernel test robot
2025-07-31 11:44     ` Alexander Potapenko
2025-07-28 15:25 ` [PATCH v3 05/10] kcov: x86: introduce CONFIG_KCOV_UNIQUE Alexander Potapenko
2025-07-29 11:11   ` Dmitry Vyukov
2025-07-28 15:25 ` [PATCH v3 06/10] kcov: add trace and trace_size to struct kcov_state Alexander Potapenko
2025-07-29 11:11   ` Dmitry Vyukov
2025-07-28 15:25 ` [PATCH v3 07/10] kcov: add ioctl(KCOV_UNIQUE_ENABLE) Alexander Potapenko
2025-07-29 11:14   ` Dmitry Vyukov
2025-07-28 15:25 ` [PATCH v3 08/10] kcov: add ioctl(KCOV_RESET_TRACE) Alexander Potapenko
2025-07-29 11:17   ` Dmitry Vyukov
2025-08-06  9:47     ` Alexander Potapenko
2025-08-06  9:59       ` Dmitry Vyukov
2025-07-28 15:25 ` [PATCH v3 09/10] kcov: selftests: add kcov_test Alexander Potapenko
2025-07-29 11:20   ` Dmitry Vyukov
2025-07-31  8:02     ` Alexander Potapenko
2025-07-28 15:25 ` [PATCH v3 10/10] kcov: use enum kcov_mode in kcov_mode_enabled() Alexander Potapenko
2025-07-29 11:20   ` Dmitry Vyukov

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=20250728152548.3969143-3-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;
as well as URLs for NNTP newsgroup(s).