Skip to content

Commit eec028c

Browse files
xairytorvalds
authored andcommitted
kcov: remote coverage support
Patch series " kcov: collect coverage from usb and vhost", v3. This patchset extends kcov to allow collecting coverage from backgound kernel threads. This extension requires custom annotations for each of the places where coverage collection is desired. This patchset implements this for hub events in the USB subsystem and for vhost workers. See the first patch description for details about the kcov extension. The other two patches apply this kcov extension to USB and vhost. Examples of other subsystems that might potentially benefit from this when custom annotations are added (the list is based on process_one_work() callers for bugs recently reported by syzbot): 1. fs: writeback wb_workfn() worker, 2. net: addrconf_dad_work()/addrconf_verify_work() workers, 3. net: neigh_periodic_work() worker, 4. net/p9: p9_write_work()/p9_read_work() workers, 5. block: blk_mq_run_work_fn() worker. These patches have been used to enable coverage-guided USB fuzzing with syzkaller for the last few years, see the details here: https://github.com/google/syzkaller/blob/master/docs/linux/external_fuzzing_usb.md This patchset has been pushed to the public Linux kernel Gerrit instance: https://linux-review.googlesource.com/c/linux/kernel/git/torvalds/linux/+/1524 This patch (of 3): Add background thread coverage collection ability to kcov. With KCOV_ENABLE coverage is collected only for syscalls that are issued from the current process. With KCOV_REMOTE_ENABLE it's possible to collect coverage for arbitrary parts of the kernel code, provided that those parts are annotated with kcov_remote_start()/kcov_remote_stop(). This allows to collect coverage from two types of kernel background threads: the global ones, that are spawned during kernel boot in a limited number of instances (e.g. one USB hub_event() worker thread is spawned per USB HCD); and the local ones, that are spawned when a user interacts with some kernel interface (e.g. vhost workers). To enable collecting coverage from a global background thread, a unique global handle must be assigned and passed to the corresponding kcov_remote_start() call. Then a userspace process can pass a list of such handles to the KCOV_REMOTE_ENABLE ioctl in the handles array field of the kcov_remote_arg struct. This will attach the used kcov device to the code sections, that are referenced by those handles. Since there might be many local background threads spawned from different userspace processes, we can't use a single global handle per annotation. Instead, the userspace process passes a non-zero handle through the common_handle field of the kcov_remote_arg struct. This common handle gets saved to the kcov_handle field in the current task_struct and needs to be passed to the newly spawned threads via custom annotations. Those threads should in turn be annotated with kcov_remote_start()/kcov_remote_stop(). Internally kcov stores handles as u64 integers. The top byte of a handle is used to denote the id of a subsystem that this handle belongs to, and the lower 4 bytes are used to denote the id of a thread instance within that subsystem. A reserved value 0 is used as a subsystem id for common handles as they don't belong to a particular subsystem. The bytes 4-7 are currently reserved and must be zero. In the future the number of bytes used for the subsystem or handle ids might be increased. When a particular userspace process collects coverage by via a common handle, kcov will collect coverage for each code section that is annotated to use the common handle obtained as kcov_handle from the current task_struct. However non common handles allow to collect coverage selectively from different subsystems. Link: http://lkml.kernel.org/r/e90e315426a384207edbec1d6aa89e43008e4caf.1572366574.git.andreyknvl@google.com Signed-off-by: Andrey Konovalov <[email protected]> Cc: Dmitry Vyukov <[email protected]> Cc: Greg Kroah-Hartman <[email protected]> Cc: Alan Stern <[email protected]> Cc: "Michael S. Tsirkin" <[email protected]> Cc: Jason Wang <[email protected]> Cc: Arnd Bergmann <[email protected]> Cc: Steven Rostedt <[email protected]> Cc: David Windsor <[email protected]> Cc: Elena Reshetova <[email protected]> Cc: Anders Roxell <[email protected]> Cc: Alexander Potapenko <[email protected]> Cc: Marco Elver <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 6d13de1 commit eec028c

File tree

5 files changed

+700
-35
lines changed

5 files changed

+700
-35
lines changed

Documentation/dev-tools/kcov.rst

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Profiling data will only become accessible once debugfs has been mounted::
3434

3535
Coverage collection
3636
-------------------
37+
3738
The following program demonstrates coverage collection from within a test
3839
program using kcov:
3940

@@ -128,6 +129,7 @@ only need to enable coverage (disable happens automatically on thread end).
128129

129130
Comparison operands collection
130131
------------------------------
132+
131133
Comparison operands collection is similar to coverage collection:
132134

133135
.. code-block:: c
@@ -202,3 +204,130 @@ Comparison operands collection is similar to coverage collection:
202204
203205
Note that the kcov modes (coverage collection or comparison operands) are
204206
mutually exclusive.
207+
208+
Remote coverage collection
209+
--------------------------
210+
211+
With KCOV_ENABLE coverage is collected only for syscalls that are issued
212+
from the current process. With KCOV_REMOTE_ENABLE it's possible to collect
213+
coverage for arbitrary parts of the kernel code, provided that those parts
214+
are annotated with kcov_remote_start()/kcov_remote_stop().
215+
216+
This allows to collect coverage from two types of kernel background
217+
threads: the global ones, that are spawned during kernel boot in a limited
218+
number of instances (e.g. one USB hub_event() worker thread is spawned per
219+
USB HCD); and the local ones, that are spawned when a user interacts with
220+
some kernel interface (e.g. vhost workers).
221+
222+
To enable collecting coverage from a global background thread, a unique
223+
global handle must be assigned and passed to the corresponding
224+
kcov_remote_start() call. Then a userspace process can pass a list of such
225+
handles to the KCOV_REMOTE_ENABLE ioctl in the handles array field of the
226+
kcov_remote_arg struct. This will attach the used kcov device to the code
227+
sections, that are referenced by those handles.
228+
229+
Since there might be many local background threads spawned from different
230+
userspace processes, we can't use a single global handle per annotation.
231+
Instead, the userspace process passes a non-zero handle through the
232+
common_handle field of the kcov_remote_arg struct. This common handle gets
233+
saved to the kcov_handle field in the current task_struct and needs to be
234+
passed to the newly spawned threads via custom annotations. Those threads
235+
should in turn be annotated with kcov_remote_start()/kcov_remote_stop().
236+
237+
Internally kcov stores handles as u64 integers. The top byte of a handle
238+
is used to denote the id of a subsystem that this handle belongs to, and
239+
the lower 4 bytes are used to denote the id of a thread instance within
240+
that subsystem. A reserved value 0 is used as a subsystem id for common
241+
handles as they don't belong to a particular subsystem. The bytes 4-7 are
242+
currently reserved and must be zero. In the future the number of bytes
243+
used for the subsystem or handle ids might be increased.
244+
245+
When a particular userspace proccess collects coverage by via a common
246+
handle, kcov will collect coverage for each code section that is annotated
247+
to use the common handle obtained as kcov_handle from the current
248+
task_struct. However non common handles allow to collect coverage
249+
selectively from different subsystems.
250+
251+
.. code-block:: c
252+
253+
struct kcov_remote_arg {
254+
unsigned trace_mode;
255+
unsigned area_size;
256+
unsigned num_handles;
257+
uint64_t common_handle;
258+
uint64_t handles[0];
259+
};
260+
261+
#define KCOV_INIT_TRACE _IOR('c', 1, unsigned long)
262+
#define KCOV_DISABLE _IO('c', 101)
263+
#define KCOV_REMOTE_ENABLE _IOW('c', 102, struct kcov_remote_arg)
264+
265+
#define COVER_SIZE (64 << 10)
266+
267+
#define KCOV_TRACE_PC 0
268+
269+
#define KCOV_SUBSYSTEM_COMMON (0x00ull << 56)
270+
#define KCOV_SUBSYSTEM_USB (0x01ull << 56)
271+
272+
#define KCOV_SUBSYSTEM_MASK (0xffull << 56)
273+
#define KCOV_INSTANCE_MASK (0xffffffffull)
274+
275+
static inline __u64 kcov_remote_handle(__u64 subsys, __u64 inst)
276+
{
277+
if (subsys & ~KCOV_SUBSYSTEM_MASK || inst & ~KCOV_INSTANCE_MASK)
278+
return 0;
279+
return subsys | inst;
280+
}
281+
282+
#define KCOV_COMMON_ID 0x42
283+
#define KCOV_USB_BUS_NUM 1
284+
285+
int main(int argc, char **argv)
286+
{
287+
int fd;
288+
unsigned long *cover, n, i;
289+
struct kcov_remote_arg *arg;
290+
291+
fd = open("/sys/kernel/debug/kcov", O_RDWR);
292+
if (fd == -1)
293+
perror("open"), exit(1);
294+
if (ioctl(fd, KCOV_INIT_TRACE, COVER_SIZE))
295+
perror("ioctl"), exit(1);
296+
cover = (unsigned long*)mmap(NULL, COVER_SIZE * sizeof(unsigned long),
297+
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
298+
if ((void*)cover == MAP_FAILED)
299+
perror("mmap"), exit(1);
300+
301+
/* Enable coverage collection via common handle and from USB bus #1. */
302+
arg = calloc(1, sizeof(*arg) + sizeof(uint64_t));
303+
if (!arg)
304+
perror("calloc"), exit(1);
305+
arg->trace_mode = KCOV_TRACE_PC;
306+
arg->area_size = COVER_SIZE;
307+
arg->num_handles = 1;
308+
arg->common_handle = kcov_remote_handle(KCOV_SUBSYSTEM_COMMON,
309+
KCOV_COMMON_ID);
310+
arg->handles[0] = kcov_remote_handle(KCOV_SUBSYSTEM_USB,
311+
KCOV_USB_BUS_NUM);
312+
if (ioctl(fd, KCOV_REMOTE_ENABLE, arg))
313+
perror("ioctl"), free(arg), exit(1);
314+
free(arg);
315+
316+
/*
317+
* Here the user needs to trigger execution of a kernel code section
318+
* that is either annotated with the common handle, or to trigger some
319+
* activity on USB bus #1.
320+
*/
321+
sleep(2);
322+
323+
n = __atomic_load_n(&cover[0], __ATOMIC_RELAXED);
324+
for (i = 0; i < n; i++)
325+
printf("0x%lx\n", cover[i + 1]);
326+
if (ioctl(fd, KCOV_DISABLE, 0))
327+
perror("ioctl"), exit(1);
328+
if (munmap(cover, COVER_SIZE * sizeof(unsigned long)))
329+
perror("munmap"), exit(1);
330+
if (close(fd))
331+
perror("close"), exit(1);
332+
return 0;
333+
}

include/linux/kcov.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,35 @@ do { \
3737
(t)->kcov_mode &= ~KCOV_IN_CTXSW; \
3838
} while (0)
3939

40+
/* See Documentation/dev-tools/kcov.rst for usage details. */
41+
void kcov_remote_start(u64 handle);
42+
void kcov_remote_stop(void);
43+
u64 kcov_common_handle(void);
44+
45+
static inline void kcov_remote_start_common(u64 id)
46+
{
47+
kcov_remote_start(kcov_remote_handle(KCOV_SUBSYSTEM_COMMON, id));
48+
}
49+
50+
static inline void kcov_remote_start_usb(u64 id)
51+
{
52+
kcov_remote_start(kcov_remote_handle(KCOV_SUBSYSTEM_USB, id));
53+
}
54+
4055
#else
4156

4257
static inline void kcov_task_init(struct task_struct *t) {}
4358
static inline void kcov_task_exit(struct task_struct *t) {}
4459
static inline void kcov_prepare_switch(struct task_struct *t) {}
4560
static inline void kcov_finish_switch(struct task_struct *t) {}
61+
static inline void kcov_remote_start(u64 handle) {}
62+
static inline void kcov_remote_stop(void) {}
63+
static inline u64 kcov_common_handle(void)
64+
{
65+
return 0;
66+
}
67+
static inline void kcov_remote_start_common(u64 id) {}
68+
static inline void kcov_remote_start_usb(u64 id) {}
4669

4770
#endif /* CONFIG_KCOV */
4871
#endif /* _LINUX_KCOV_H */

include/linux/sched.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,8 @@ struct task_struct {
12101210
#endif /* CONFIG_TRACING */
12111211

12121212
#ifdef CONFIG_KCOV
1213+
/* See kernel/kcov.c for more details. */
1214+
12131215
/* Coverage collection mode enabled for this task (0 if disabled): */
12141216
unsigned int kcov_mode;
12151217

@@ -1221,6 +1223,12 @@ struct task_struct {
12211223

12221224
/* KCOV descriptor wired with this task or NULL: */
12231225
struct kcov *kcov;
1226+
1227+
/* KCOV common handle for remote coverage collection: */
1228+
u64 kcov_handle;
1229+
1230+
/* KCOV sequence number: */
1231+
int kcov_sequence;
12241232
#endif
12251233

12261234
#ifdef CONFIG_MEMCG

include/uapi/linux/kcov.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,24 @@
44

55
#include <linux/types.h>
66

7+
/*
8+
* Argument for KCOV_REMOTE_ENABLE ioctl, see Documentation/dev-tools/kcov.rst
9+
* and the comment before kcov_remote_start() for usage details.
10+
*/
11+
struct kcov_remote_arg {
12+
unsigned int trace_mode; /* KCOV_TRACE_PC or KCOV_TRACE_CMP */
13+
unsigned int area_size; /* Length of coverage buffer in words */
14+
unsigned int num_handles; /* Size of handles array */
15+
__u64 common_handle;
16+
__u64 handles[0];
17+
};
18+
19+
#define KCOV_REMOTE_MAX_HANDLES 0x100
20+
721
#define KCOV_INIT_TRACE _IOR('c', 1, unsigned long)
822
#define KCOV_ENABLE _IO('c', 100)
923
#define KCOV_DISABLE _IO('c', 101)
24+
#define KCOV_REMOTE_ENABLE _IOW('c', 102, struct kcov_remote_arg)
1025

1126
enum {
1227
/*
@@ -32,4 +47,17 @@ enum {
3247
#define KCOV_CMP_SIZE(n) ((n) << 1)
3348
#define KCOV_CMP_MASK KCOV_CMP_SIZE(3)
3449

50+
#define KCOV_SUBSYSTEM_COMMON (0x00ull << 56)
51+
#define KCOV_SUBSYSTEM_USB (0x01ull << 56)
52+
53+
#define KCOV_SUBSYSTEM_MASK (0xffull << 56)
54+
#define KCOV_INSTANCE_MASK (0xffffffffull)
55+
56+
static inline __u64 kcov_remote_handle(__u64 subsys, __u64 inst)
57+
{
58+
if (subsys & ~KCOV_SUBSYSTEM_MASK || inst & ~KCOV_INSTANCE_MASK)
59+
return 0;
60+
return subsys | inst;
61+
}
62+
3563
#endif /* _LINUX_KCOV_IOCTLS_H */

0 commit comments

Comments
 (0)