xref: /linux-6.15/include/linux/kcov.h (revision 01c8f980)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
25c9a8750SDmitry Vyukov #ifndef _LINUX_KCOV_H
35c9a8750SDmitry Vyukov #define _LINUX_KCOV_H
45c9a8750SDmitry Vyukov 
5183f47fcSSebastian Andrzej Siewior #include <linux/sched.h>
65c9a8750SDmitry Vyukov #include <uapi/linux/kcov.h>
75c9a8750SDmitry Vyukov 
85c9a8750SDmitry Vyukov struct task_struct;
95c9a8750SDmitry Vyukov 
105c9a8750SDmitry Vyukov #ifdef CONFIG_KCOV
115c9a8750SDmitry Vyukov 
125c9a8750SDmitry Vyukov enum kcov_mode {
135c9a8750SDmitry Vyukov 	/* Coverage collection is not enabled yet. */
145c9a8750SDmitry Vyukov 	KCOV_MODE_DISABLED = 0,
15ded97d2cSVictor Chibotaru 	/* KCOV was initialized, but tracing mode hasn't been chosen yet. */
16ded97d2cSVictor Chibotaru 	KCOV_MODE_INIT = 1,
175c9a8750SDmitry Vyukov 	/*
185c9a8750SDmitry Vyukov 	 * Tracing coverage collection mode.
195c9a8750SDmitry Vyukov 	 * Covered PCs are collected in a per-task buffer.
205c9a8750SDmitry Vyukov 	 */
21ded97d2cSVictor Chibotaru 	KCOV_MODE_TRACE_PC = 2,
22ded97d2cSVictor Chibotaru 	/* Collecting comparison operands mode. */
23ded97d2cSVictor Chibotaru 	KCOV_MODE_TRACE_CMP = 3,
24*01c8f980SAleksandr Nogikh 	/* The process owns a KCOV remote reference. */
25*01c8f980SAleksandr Nogikh 	KCOV_MODE_REMOTE = 4,
265c9a8750SDmitry Vyukov };
275c9a8750SDmitry Vyukov 
280ed557aaSMark Rutland #define KCOV_IN_CTXSW	(1 << 30)
290ed557aaSMark Rutland 
30ded97d2cSVictor Chibotaru void kcov_task_init(struct task_struct *t);
31ded97d2cSVictor Chibotaru void kcov_task_exit(struct task_struct *t);
32ded97d2cSVictor Chibotaru 
330ed557aaSMark Rutland #define kcov_prepare_switch(t)			\
340ed557aaSMark Rutland do {						\
350ed557aaSMark Rutland 	(t)->kcov_mode |= KCOV_IN_CTXSW;	\
360ed557aaSMark Rutland } while (0)
370ed557aaSMark Rutland 
380ed557aaSMark Rutland #define kcov_finish_switch(t)			\
390ed557aaSMark Rutland do {						\
400ed557aaSMark Rutland 	(t)->kcov_mode &= ~KCOV_IN_CTXSW;	\
410ed557aaSMark Rutland } while (0)
420ed557aaSMark Rutland 
43eec028c9SAndrey Konovalov /* See Documentation/dev-tools/kcov.rst for usage details. */
44eec028c9SAndrey Konovalov void kcov_remote_start(u64 handle);
45eec028c9SAndrey Konovalov void kcov_remote_stop(void);
46eec028c9SAndrey Konovalov u64 kcov_common_handle(void);
47eec028c9SAndrey Konovalov 
kcov_remote_start_common(u64 id)48eec028c9SAndrey Konovalov static inline void kcov_remote_start_common(u64 id)
49eec028c9SAndrey Konovalov {
50eec028c9SAndrey Konovalov 	kcov_remote_start(kcov_remote_handle(KCOV_SUBSYSTEM_COMMON, id));
51eec028c9SAndrey Konovalov }
52eec028c9SAndrey Konovalov 
kcov_remote_start_usb(u64 id)53eec028c9SAndrey Konovalov static inline void kcov_remote_start_usb(u64 id)
54eec028c9SAndrey Konovalov {
55eec028c9SAndrey Konovalov 	kcov_remote_start(kcov_remote_handle(KCOV_SUBSYSTEM_USB, id));
56eec028c9SAndrey Konovalov }
57eec028c9SAndrey Konovalov 
58e89eed02SAndrey Konovalov /*
59e89eed02SAndrey Konovalov  * The softirq flavor of kcov_remote_*() functions is introduced as a temporary
60e89eed02SAndrey Konovalov  * workaround for KCOV's lack of nested remote coverage sections support.
61204c2f53SRong Tao  *
62e89eed02SAndrey Konovalov  * Adding support is tracked in https://bugzilla.kernel.org/show_bug.cgi?id=210337.
63e89eed02SAndrey Konovalov  *
64e89eed02SAndrey Konovalov  * kcov_remote_start_usb_softirq():
65e89eed02SAndrey Konovalov  *
66e89eed02SAndrey Konovalov  * 1. Only collects coverage when called in the softirq context. This allows
67e89eed02SAndrey Konovalov  *    avoiding nested remote coverage collection sections in the task context.
68e89eed02SAndrey Konovalov  *    For example, USB/IP calls usb_hcd_giveback_urb() in the task context
69e89eed02SAndrey Konovalov  *    within an existing remote coverage collection section. Thus, KCOV should
70e89eed02SAndrey Konovalov  *    not attempt to start collecting coverage within the coverage collection
71e89eed02SAndrey Konovalov  *    section in __usb_hcd_giveback_urb() in this case.
72e89eed02SAndrey Konovalov  *
73e89eed02SAndrey Konovalov  * 2. Disables interrupts for the duration of the coverage collection section.
74e89eed02SAndrey Konovalov  *    This allows avoiding nested remote coverage collection sections in the
75e89eed02SAndrey Konovalov  *    softirq context (a softirq might occur during the execution of a work in
76e89eed02SAndrey Konovalov  *    the BH workqueue, which runs with in_serving_softirq() > 0).
77e0ddec73SArnd Bergmann  *    For example, usb_giveback_urb_bh() runs in the BH workqueue with
78e0ddec73SArnd Bergmann  *    interrupts enabled, so __usb_hcd_giveback_urb() might be interrupted in
79e0ddec73SArnd Bergmann  *    the middle of its remote coverage collection section, and the interrupt
80e0ddec73SArnd Bergmann  *    handler might invoke __usb_hcd_giveback_urb() again.
81e0ddec73SArnd Bergmann  */
82e0ddec73SArnd Bergmann 
kcov_remote_start_usb_softirq(u64 id)83e0ddec73SArnd Bergmann static inline unsigned long kcov_remote_start_usb_softirq(u64 id)
84e0ddec73SArnd Bergmann {
85e0ddec73SArnd Bergmann 	unsigned long flags = 0;
86e0ddec73SArnd Bergmann 
87e0ddec73SArnd Bergmann 	if (in_serving_softirq()) {
88e0ddec73SArnd Bergmann 		local_irq_save(flags);
89e0ddec73SArnd Bergmann 		kcov_remote_start_usb(id);
90e0ddec73SArnd Bergmann 	}
91e0ddec73SArnd Bergmann 
92e0ddec73SArnd Bergmann 	return flags;
93e0ddec73SArnd Bergmann }
945c9a8750SDmitry Vyukov 
kcov_remote_stop_softirq(unsigned long flags)955c9a8750SDmitry Vyukov static inline void kcov_remote_stop_softirq(unsigned long flags)
965c9a8750SDmitry Vyukov {
975c9a8750SDmitry Vyukov 	if (in_serving_softirq()) {
980ed557aaSMark Rutland 		kcov_remote_stop();
990ed557aaSMark Rutland 		local_irq_restore(flags);
100eec028c9SAndrey Konovalov 	}
101eec028c9SAndrey Konovalov }
102eec028c9SAndrey Konovalov 
103eec028c9SAndrey Konovalov #ifdef CONFIG_64BIT
104eec028c9SAndrey Konovalov typedef unsigned long kcov_u64;
105eec028c9SAndrey Konovalov #else
106eec028c9SAndrey Konovalov typedef unsigned long long kcov_u64;
107eec028c9SAndrey Konovalov #endif
108e89eed02SAndrey Konovalov 
109e89eed02SAndrey Konovalov void __sanitizer_cov_trace_pc(void);
1105c9a8750SDmitry Vyukov void __sanitizer_cov_trace_cmp1(u8 arg1, u8 arg2);
1115c9a8750SDmitry Vyukov void __sanitizer_cov_trace_cmp2(u16 arg1, u16 arg2);
1125c9a8750SDmitry Vyukov void __sanitizer_cov_trace_cmp4(u32 arg1, u32 arg2);
113 void __sanitizer_cov_trace_cmp8(kcov_u64 arg1, kcov_u64 arg2);
114 void __sanitizer_cov_trace_const_cmp1(u8 arg1, u8 arg2);
115 void __sanitizer_cov_trace_const_cmp2(u16 arg1, u16 arg2);
116 void __sanitizer_cov_trace_const_cmp4(u32 arg1, u32 arg2);
117 void __sanitizer_cov_trace_const_cmp8(kcov_u64 arg1, kcov_u64 arg2);
118 void __sanitizer_cov_trace_switch(kcov_u64 val, void *cases);
119 
120 #else
121 
kcov_task_init(struct task_struct * t)122 static inline void kcov_task_init(struct task_struct *t) {}
kcov_task_exit(struct task_struct * t)123 static inline void kcov_task_exit(struct task_struct *t) {}
kcov_prepare_switch(struct task_struct * t)124 static inline void kcov_prepare_switch(struct task_struct *t) {}
kcov_finish_switch(struct task_struct * t)125 static inline void kcov_finish_switch(struct task_struct *t) {}
kcov_remote_start(u64 handle)126 static inline void kcov_remote_start(u64 handle) {}
kcov_remote_stop(void)127 static inline void kcov_remote_stop(void) {}
kcov_common_handle(void)128 static inline u64 kcov_common_handle(void)
129 {
130 	return 0;
131 }
kcov_remote_start_common(u64 id)132 static inline void kcov_remote_start_common(u64 id) {}
kcov_remote_start_usb(u64 id)133 static inline void kcov_remote_start_usb(u64 id) {}
kcov_remote_start_usb_softirq(u64 id)134 static inline unsigned long kcov_remote_start_usb_softirq(u64 id)
135 {
136 	return 0;
137 }
kcov_remote_stop_softirq(unsigned long flags)138 static inline void kcov_remote_stop_softirq(unsigned long flags) {}
139 
140 #endif /* CONFIG_KCOV */
141 #endif /* _LINUX_KCOV_H */
142