Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <andreyknvl@google.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: Alan Stern <stern@rowland.harvard.edu>
Cc: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Jason Wang <jasowang@redhat.com>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: David Windsor <dwindsor@gmail.com>
Cc: Elena Reshetova <elena.reshetova@intel.com>
Cc: Anders Roxell <anders.roxell@linaro.org>
Cc: Alexander Potapenko <glider@google.com>
Cc: Marco Elver <elver@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Andrey Konovalov and committed by
Linus Torvalds
eec028c9 6d13de14

+700 -35
+129
Documentation/dev-tools/kcov.rst
··· 34 34 35 35 Coverage collection 36 36 ------------------- 37 + 37 38 The following program demonstrates coverage collection from within a test 38 39 program using kcov: 39 40 ··· 129 128 130 129 Comparison operands collection 131 130 ------------------------------ 131 + 132 132 Comparison operands collection is similar to coverage collection: 133 133 134 134 .. code-block:: c ··· 204 202 205 203 Note that the kcov modes (coverage collection or comparison operands) are 206 204 mutually exclusive. 205 + 206 + Remote coverage collection 207 + -------------------------- 208 + 209 + With KCOV_ENABLE coverage is collected only for syscalls that are issued 210 + from the current process. With KCOV_REMOTE_ENABLE it's possible to collect 211 + coverage for arbitrary parts of the kernel code, provided that those parts 212 + are annotated with kcov_remote_start()/kcov_remote_stop(). 213 + 214 + This allows to collect coverage from two types of kernel background 215 + threads: the global ones, that are spawned during kernel boot in a limited 216 + number of instances (e.g. one USB hub_event() worker thread is spawned per 217 + USB HCD); and the local ones, that are spawned when a user interacts with 218 + some kernel interface (e.g. vhost workers). 219 + 220 + To enable collecting coverage from a global background thread, a unique 221 + global handle must be assigned and passed to the corresponding 222 + kcov_remote_start() call. Then a userspace process can pass a list of such 223 + handles to the KCOV_REMOTE_ENABLE ioctl in the handles array field of the 224 + kcov_remote_arg struct. This will attach the used kcov device to the code 225 + sections, that are referenced by those handles. 226 + 227 + Since there might be many local background threads spawned from different 228 + userspace processes, we can't use a single global handle per annotation. 229 + Instead, the userspace process passes a non-zero handle through the 230 + common_handle field of the kcov_remote_arg struct. This common handle gets 231 + saved to the kcov_handle field in the current task_struct and needs to be 232 + passed to the newly spawned threads via custom annotations. Those threads 233 + should in turn be annotated with kcov_remote_start()/kcov_remote_stop(). 234 + 235 + Internally kcov stores handles as u64 integers. The top byte of a handle 236 + is used to denote the id of a subsystem that this handle belongs to, and 237 + the lower 4 bytes are used to denote the id of a thread instance within 238 + that subsystem. A reserved value 0 is used as a subsystem id for common 239 + handles as they don't belong to a particular subsystem. The bytes 4-7 are 240 + currently reserved and must be zero. In the future the number of bytes 241 + used for the subsystem or handle ids might be increased. 242 + 243 + When a particular userspace proccess collects coverage by via a common 244 + handle, kcov will collect coverage for each code section that is annotated 245 + to use the common handle obtained as kcov_handle from the current 246 + task_struct. However non common handles allow to collect coverage 247 + selectively from different subsystems. 248 + 249 + .. code-block:: c 250 + 251 + struct kcov_remote_arg { 252 + unsigned trace_mode; 253 + unsigned area_size; 254 + unsigned num_handles; 255 + uint64_t common_handle; 256 + uint64_t handles[0]; 257 + }; 258 + 259 + #define KCOV_INIT_TRACE _IOR('c', 1, unsigned long) 260 + #define KCOV_DISABLE _IO('c', 101) 261 + #define KCOV_REMOTE_ENABLE _IOW('c', 102, struct kcov_remote_arg) 262 + 263 + #define COVER_SIZE (64 << 10) 264 + 265 + #define KCOV_TRACE_PC 0 266 + 267 + #define KCOV_SUBSYSTEM_COMMON (0x00ull << 56) 268 + #define KCOV_SUBSYSTEM_USB (0x01ull << 56) 269 + 270 + #define KCOV_SUBSYSTEM_MASK (0xffull << 56) 271 + #define KCOV_INSTANCE_MASK (0xffffffffull) 272 + 273 + static inline __u64 kcov_remote_handle(__u64 subsys, __u64 inst) 274 + { 275 + if (subsys & ~KCOV_SUBSYSTEM_MASK || inst & ~KCOV_INSTANCE_MASK) 276 + return 0; 277 + return subsys | inst; 278 + } 279 + 280 + #define KCOV_COMMON_ID 0x42 281 + #define KCOV_USB_BUS_NUM 1 282 + 283 + int main(int argc, char **argv) 284 + { 285 + int fd; 286 + unsigned long *cover, n, i; 287 + struct kcov_remote_arg *arg; 288 + 289 + fd = open("/sys/kernel/debug/kcov", O_RDWR); 290 + if (fd == -1) 291 + perror("open"), exit(1); 292 + if (ioctl(fd, KCOV_INIT_TRACE, COVER_SIZE)) 293 + perror("ioctl"), exit(1); 294 + cover = (unsigned long*)mmap(NULL, COVER_SIZE * sizeof(unsigned long), 295 + PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 296 + if ((void*)cover == MAP_FAILED) 297 + perror("mmap"), exit(1); 298 + 299 + /* Enable coverage collection via common handle and from USB bus #1. */ 300 + arg = calloc(1, sizeof(*arg) + sizeof(uint64_t)); 301 + if (!arg) 302 + perror("calloc"), exit(1); 303 + arg->trace_mode = KCOV_TRACE_PC; 304 + arg->area_size = COVER_SIZE; 305 + arg->num_handles = 1; 306 + arg->common_handle = kcov_remote_handle(KCOV_SUBSYSTEM_COMMON, 307 + KCOV_COMMON_ID); 308 + arg->handles[0] = kcov_remote_handle(KCOV_SUBSYSTEM_USB, 309 + KCOV_USB_BUS_NUM); 310 + if (ioctl(fd, KCOV_REMOTE_ENABLE, arg)) 311 + perror("ioctl"), free(arg), exit(1); 312 + free(arg); 313 + 314 + /* 315 + * Here the user needs to trigger execution of a kernel code section 316 + * that is either annotated with the common handle, or to trigger some 317 + * activity on USB bus #1. 318 + */ 319 + sleep(2); 320 + 321 + n = __atomic_load_n(&cover[0], __ATOMIC_RELAXED); 322 + for (i = 0; i < n; i++) 323 + printf("0x%lx\n", cover[i + 1]); 324 + if (ioctl(fd, KCOV_DISABLE, 0)) 325 + perror("ioctl"), exit(1); 326 + if (munmap(cover, COVER_SIZE * sizeof(unsigned long))) 327 + perror("munmap"), exit(1); 328 + if (close(fd)) 329 + perror("close"), exit(1); 330 + return 0; 331 + }
+23
include/linux/kcov.h
··· 37 37 (t)->kcov_mode &= ~KCOV_IN_CTXSW; \ 38 38 } while (0) 39 39 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 + 40 55 #else 41 56 42 57 static inline void kcov_task_init(struct task_struct *t) {} 43 58 static inline void kcov_task_exit(struct task_struct *t) {} 44 59 static inline void kcov_prepare_switch(struct task_struct *t) {} 45 60 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) {} 46 69 47 70 #endif /* CONFIG_KCOV */ 48 71 #endif /* _LINUX_KCOV_H */
+8
include/linux/sched.h
··· 1210 1210 #endif /* CONFIG_TRACING */ 1211 1211 1212 1212 #ifdef CONFIG_KCOV 1213 + /* See kernel/kcov.c for more details. */ 1214 + 1213 1215 /* Coverage collection mode enabled for this task (0 if disabled): */ 1214 1216 unsigned int kcov_mode; 1215 1217 ··· 1223 1221 1224 1222 /* KCOV descriptor wired with this task or NULL: */ 1225 1223 struct kcov *kcov; 1224 + 1225 + /* KCOV common handle for remote coverage collection: */ 1226 + u64 kcov_handle; 1227 + 1228 + /* KCOV sequence number: */ 1229 + int kcov_sequence; 1226 1230 #endif 1227 1231 1228 1232 #ifdef CONFIG_MEMCG
+28
include/uapi/linux/kcov.h
··· 4 4 5 5 #include <linux/types.h> 6 6 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 + 7 21 #define KCOV_INIT_TRACE _IOR('c', 1, unsigned long) 8 22 #define KCOV_ENABLE _IO('c', 100) 9 23 #define KCOV_DISABLE _IO('c', 101) 24 + #define KCOV_REMOTE_ENABLE _IOW('c', 102, struct kcov_remote_arg) 10 25 11 26 enum { 12 27 /* ··· 46 31 #define KCOV_CMP_CONST (1 << 0) 47 32 #define KCOV_CMP_SIZE(n) ((n) << 1) 48 33 #define KCOV_CMP_MASK KCOV_CMP_SIZE(3) 34 + 35 + #define KCOV_SUBSYSTEM_COMMON (0x00ull << 56) 36 + #define KCOV_SUBSYSTEM_USB (0x01ull << 56) 37 + 38 + #define KCOV_SUBSYSTEM_MASK (0xffull << 56) 39 + #define KCOV_INSTANCE_MASK (0xffffffffull) 40 + 41 + static inline __u64 kcov_remote_handle(__u64 subsys, __u64 inst) 42 + { 43 + if (subsys & ~KCOV_SUBSYSTEM_MASK || inst & ~KCOV_INSTANCE_MASK) 44 + return 0; 45 + return subsys | inst; 46 + } 49 47 50 48 #endif /* _LINUX_KCOV_IOCTLS_H */
+512 -35
kernel/kcov.c
··· 9 9 #include <linux/types.h> 10 10 #include <linux/file.h> 11 11 #include <linux/fs.h> 12 + #include <linux/hashtable.h> 12 13 #include <linux/init.h> 13 14 #include <linux/mm.h> 14 15 #include <linux/preempt.h> ··· 22 21 #include <linux/uaccess.h> 23 22 #include <linux/kcov.h> 24 23 #include <linux/refcount.h> 24 + #include <linux/log2.h> 25 25 #include <asm/setup.h> 26 + 27 + #define kcov_debug(fmt, ...) pr_debug("%s: " fmt, __func__, ##__VA_ARGS__) 26 28 27 29 /* Number of 64-bit words written per one comparison: */ 28 30 #define KCOV_WORDS_PER_CMP 4 ··· 48 44 * Reference counter. We keep one for: 49 45 * - opened file descriptor 50 46 * - task with enabled coverage (we can't unwire it from another task) 47 + * - each code section for remote coverage collection 51 48 */ 52 49 refcount_t refcount; 53 50 /* The lock protects mode, size, area and t. */ 54 51 spinlock_t lock; 55 52 enum kcov_mode mode; 56 - /* Size of arena (in long's for KCOV_MODE_TRACE). */ 57 - unsigned size; 53 + /* Size of arena (in long's). */ 54 + unsigned int size; 58 55 /* Coverage buffer shared with user space. */ 59 56 void *area; 60 57 /* Task for which we collect coverage, or NULL. */ 61 58 struct task_struct *t; 59 + /* Collecting coverage from remote (background) threads. */ 60 + bool remote; 61 + /* Size of remote area (in long's). */ 62 + unsigned int remote_size; 63 + /* 64 + * Sequence is incremented each time kcov is reenabled, used by 65 + * kcov_remote_stop(), see the comment there. 66 + */ 67 + int sequence; 62 68 }; 69 + 70 + struct kcov_remote_area { 71 + struct list_head list; 72 + unsigned int size; 73 + }; 74 + 75 + struct kcov_remote { 76 + u64 handle; 77 + struct kcov *kcov; 78 + struct hlist_node hnode; 79 + }; 80 + 81 + static DEFINE_SPINLOCK(kcov_remote_lock); 82 + static DEFINE_HASHTABLE(kcov_remote_map, 4); 83 + static struct list_head kcov_remote_areas = LIST_HEAD_INIT(kcov_remote_areas); 84 + 85 + /* Must be called with kcov_remote_lock locked. */ 86 + static struct kcov_remote *kcov_remote_find(u64 handle) 87 + { 88 + struct kcov_remote *remote; 89 + 90 + hash_for_each_possible(kcov_remote_map, remote, hnode, handle) { 91 + if (remote->handle == handle) 92 + return remote; 93 + } 94 + return NULL; 95 + } 96 + 97 + static struct kcov_remote *kcov_remote_add(struct kcov *kcov, u64 handle) 98 + { 99 + struct kcov_remote *remote; 100 + 101 + if (kcov_remote_find(handle)) 102 + return ERR_PTR(-EEXIST); 103 + remote = kmalloc(sizeof(*remote), GFP_ATOMIC); 104 + if (!remote) 105 + return ERR_PTR(-ENOMEM); 106 + remote->handle = handle; 107 + remote->kcov = kcov; 108 + hash_add(kcov_remote_map, &remote->hnode, handle); 109 + return remote; 110 + } 111 + 112 + /* Must be called with kcov_remote_lock locked. */ 113 + static struct kcov_remote_area *kcov_remote_area_get(unsigned int size) 114 + { 115 + struct kcov_remote_area *area; 116 + struct list_head *pos; 117 + 118 + kcov_debug("size = %u\n", size); 119 + list_for_each(pos, &kcov_remote_areas) { 120 + area = list_entry(pos, struct kcov_remote_area, list); 121 + if (area->size == size) { 122 + list_del(&area->list); 123 + kcov_debug("rv = %px\n", area); 124 + return area; 125 + } 126 + } 127 + kcov_debug("rv = NULL\n"); 128 + return NULL; 129 + } 130 + 131 + /* Must be called with kcov_remote_lock locked. */ 132 + static void kcov_remote_area_put(struct kcov_remote_area *area, 133 + unsigned int size) 134 + { 135 + kcov_debug("area = %px, size = %u\n", area, size); 136 + INIT_LIST_HEAD(&area->list); 137 + area->size = size; 138 + list_add(&area->list, &kcov_remote_areas); 139 + } 63 140 64 141 static notrace bool check_kcov_mode(enum kcov_mode needed_mode, struct task_struct *t) 65 142 { ··· 158 73 * in_interrupt() returns false (e.g. preempt_schedule_irq()). 159 74 * READ_ONCE()/barrier() effectively provides load-acquire wrt 160 75 * interrupts, there are paired barrier()/WRITE_ONCE() in 161 - * kcov_ioctl_locked(). 76 + * kcov_start(). 162 77 */ 163 78 barrier(); 164 79 return mode == needed_mode; ··· 312 227 EXPORT_SYMBOL(__sanitizer_cov_trace_switch); 313 228 #endif /* ifdef CONFIG_KCOV_ENABLE_COMPARISONS */ 314 229 230 + static void kcov_start(struct task_struct *t, unsigned int size, 231 + void *area, enum kcov_mode mode, int sequence) 232 + { 233 + kcov_debug("t = %px, size = %u, area = %px\n", t, size, area); 234 + /* Cache in task struct for performance. */ 235 + t->kcov_size = size; 236 + t->kcov_area = area; 237 + /* See comment in check_kcov_mode(). */ 238 + barrier(); 239 + WRITE_ONCE(t->kcov_mode, mode); 240 + t->kcov_sequence = sequence; 241 + } 242 + 243 + static void kcov_stop(struct task_struct *t) 244 + { 245 + WRITE_ONCE(t->kcov_mode, KCOV_MODE_DISABLED); 246 + barrier(); 247 + t->kcov_size = 0; 248 + t->kcov_area = NULL; 249 + } 250 + 251 + static void kcov_task_reset(struct task_struct *t) 252 + { 253 + kcov_stop(t); 254 + t->kcov = NULL; 255 + t->kcov_sequence = 0; 256 + t->kcov_handle = 0; 257 + } 258 + 259 + void kcov_task_init(struct task_struct *t) 260 + { 261 + kcov_task_reset(t); 262 + t->kcov_handle = current->kcov_handle; 263 + } 264 + 265 + static void kcov_reset(struct kcov *kcov) 266 + { 267 + kcov->t = NULL; 268 + kcov->mode = KCOV_MODE_INIT; 269 + kcov->remote = false; 270 + kcov->remote_size = 0; 271 + kcov->sequence++; 272 + } 273 + 274 + static void kcov_remote_reset(struct kcov *kcov) 275 + { 276 + int bkt; 277 + struct kcov_remote *remote; 278 + struct hlist_node *tmp; 279 + 280 + spin_lock(&kcov_remote_lock); 281 + hash_for_each_safe(kcov_remote_map, bkt, tmp, remote, hnode) { 282 + if (remote->kcov != kcov) 283 + continue; 284 + kcov_debug("removing handle %llx\n", remote->handle); 285 + hash_del(&remote->hnode); 286 + kfree(remote); 287 + } 288 + /* Do reset before unlock to prevent races with kcov_remote_start(). */ 289 + kcov_reset(kcov); 290 + spin_unlock(&kcov_remote_lock); 291 + } 292 + 293 + static void kcov_disable(struct task_struct *t, struct kcov *kcov) 294 + { 295 + kcov_task_reset(t); 296 + if (kcov->remote) 297 + kcov_remote_reset(kcov); 298 + else 299 + kcov_reset(kcov); 300 + } 301 + 315 302 static void kcov_get(struct kcov *kcov) 316 303 { 317 304 refcount_inc(&kcov->refcount); ··· 392 235 static void kcov_put(struct kcov *kcov) 393 236 { 394 237 if (refcount_dec_and_test(&kcov->refcount)) { 238 + kcov_remote_reset(kcov); 395 239 vfree(kcov->area); 396 240 kfree(kcov); 397 241 } 398 - } 399 - 400 - void kcov_task_init(struct task_struct *t) 401 - { 402 - WRITE_ONCE(t->kcov_mode, KCOV_MODE_DISABLED); 403 - barrier(); 404 - t->kcov_size = 0; 405 - t->kcov_area = NULL; 406 - t->kcov = NULL; 407 242 } 408 243 409 244 void kcov_task_exit(struct task_struct *t) ··· 405 256 kcov = t->kcov; 406 257 if (kcov == NULL) 407 258 return; 259 + 408 260 spin_lock(&kcov->lock); 261 + kcov_debug("t = %px, kcov->t = %px\n", t, kcov->t); 262 + /* 263 + * For KCOV_ENABLE devices we want to make sure that t->kcov->t == t, 264 + * which comes down to: 265 + * WARN_ON(!kcov->remote && kcov->t != t); 266 + * 267 + * For KCOV_REMOTE_ENABLE devices, the exiting task is either: 268 + * 2. A remote task between kcov_remote_start() and kcov_remote_stop(). 269 + * In this case we should print a warning right away, since a task 270 + * shouldn't be exiting when it's in a kcov coverage collection 271 + * section. Here t points to the task that is collecting remote 272 + * coverage, and t->kcov->t points to the thread that created the 273 + * kcov device. Which means that to detect this case we need to 274 + * check that t != t->kcov->t, and this gives us the following: 275 + * WARN_ON(kcov->remote && kcov->t != t); 276 + * 277 + * 2. The task that created kcov exiting without calling KCOV_DISABLE, 278 + * and then again we can make sure that t->kcov->t == t: 279 + * WARN_ON(kcov->remote && kcov->t != t); 280 + * 281 + * By combining all three checks into one we get: 282 + */ 409 283 if (WARN_ON(kcov->t != t)) { 410 284 spin_unlock(&kcov->lock); 411 285 return; 412 286 } 413 287 /* Just to not leave dangling references behind. */ 414 - kcov_task_init(t); 415 - kcov->t = NULL; 416 - kcov->mode = KCOV_MODE_INIT; 288 + kcov_disable(t, kcov); 417 289 spin_unlock(&kcov->lock); 418 290 kcov_put(kcov); 419 291 } ··· 483 313 if (!kcov) 484 314 return -ENOMEM; 485 315 kcov->mode = KCOV_MODE_DISABLED; 316 + kcov->sequence = 1; 486 317 refcount_set(&kcov->refcount, 1); 487 318 spin_lock_init(&kcov->lock); 488 319 filep->private_data = kcov; ··· 494 323 { 495 324 kcov_put(filep->private_data); 496 325 return 0; 326 + } 327 + 328 + static int kcov_get_mode(unsigned long arg) 329 + { 330 + if (arg == KCOV_TRACE_PC) 331 + return KCOV_MODE_TRACE_PC; 332 + else if (arg == KCOV_TRACE_CMP) 333 + #ifdef CONFIG_KCOV_ENABLE_COMPARISONS 334 + return KCOV_MODE_TRACE_CMP; 335 + #else 336 + return -ENOTSUPP; 337 + #endif 338 + else 339 + return -EINVAL; 497 340 } 498 341 499 342 /* ··· 525 340 READ_ONCE(area[offset]); 526 341 } 527 342 343 + static inline bool kcov_check_handle(u64 handle, bool common_valid, 344 + bool uncommon_valid, bool zero_valid) 345 + { 346 + if (handle & ~(KCOV_SUBSYSTEM_MASK | KCOV_INSTANCE_MASK)) 347 + return false; 348 + switch (handle & KCOV_SUBSYSTEM_MASK) { 349 + case KCOV_SUBSYSTEM_COMMON: 350 + return (handle & KCOV_INSTANCE_MASK) ? 351 + common_valid : zero_valid; 352 + case KCOV_SUBSYSTEM_USB: 353 + return uncommon_valid; 354 + default: 355 + return false; 356 + } 357 + return false; 358 + } 359 + 528 360 static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd, 529 361 unsigned long arg) 530 362 { 531 363 struct task_struct *t; 532 364 unsigned long size, unused; 365 + int mode, i; 366 + struct kcov_remote_arg *remote_arg; 367 + struct kcov_remote *remote; 533 368 534 369 switch (cmd) { 535 370 case KCOV_INIT_TRACE: 371 + kcov_debug("KCOV_INIT_TRACE\n"); 536 372 /* 537 373 * Enable kcov in trace mode and setup buffer size. 538 374 * Must happen before anything else. ··· 572 366 kcov->mode = KCOV_MODE_INIT; 573 367 return 0; 574 368 case KCOV_ENABLE: 369 + kcov_debug("KCOV_ENABLE\n"); 575 370 /* 576 371 * Enable coverage for the current task. 577 372 * At this point user must have been enabled trace mode, ··· 585 378 t = current; 586 379 if (kcov->t != NULL || t->kcov != NULL) 587 380 return -EBUSY; 588 - if (arg == KCOV_TRACE_PC) 589 - kcov->mode = KCOV_MODE_TRACE_PC; 590 - else if (arg == KCOV_TRACE_CMP) 591 - #ifdef CONFIG_KCOV_ENABLE_COMPARISONS 592 - kcov->mode = KCOV_MODE_TRACE_CMP; 593 - #else 594 - return -ENOTSUPP; 595 - #endif 596 - else 597 - return -EINVAL; 381 + mode = kcov_get_mode(arg); 382 + if (mode < 0) 383 + return mode; 598 384 kcov_fault_in_area(kcov); 599 - /* Cache in task struct for performance. */ 600 - t->kcov_size = kcov->size; 601 - t->kcov_area = kcov->area; 602 - /* See comment in check_kcov_mode(). */ 603 - barrier(); 604 - WRITE_ONCE(t->kcov_mode, kcov->mode); 385 + kcov->mode = mode; 386 + kcov_start(t, kcov->size, kcov->area, kcov->mode, 387 + kcov->sequence); 605 388 t->kcov = kcov; 606 389 kcov->t = t; 607 - /* This is put either in kcov_task_exit() or in KCOV_DISABLE. */ 390 + /* Put either in kcov_task_exit() or in KCOV_DISABLE. */ 608 391 kcov_get(kcov); 609 392 return 0; 610 393 case KCOV_DISABLE: 394 + kcov_debug("KCOV_DISABLE\n"); 611 395 /* Disable coverage for the current task. */ 612 396 unused = arg; 613 397 if (unused != 0 || current->kcov != kcov) ··· 606 408 t = current; 607 409 if (WARN_ON(kcov->t != t)) 608 410 return -EINVAL; 609 - kcov_task_init(t); 610 - kcov->t = NULL; 611 - kcov->mode = KCOV_MODE_INIT; 411 + kcov_disable(t, kcov); 612 412 kcov_put(kcov); 413 + return 0; 414 + case KCOV_REMOTE_ENABLE: 415 + kcov_debug("KCOV_REMOTE_ENABLE\n"); 416 + if (kcov->mode != KCOV_MODE_INIT || !kcov->area) 417 + return -EINVAL; 418 + t = current; 419 + if (kcov->t != NULL || t->kcov != NULL) 420 + return -EBUSY; 421 + remote_arg = (struct kcov_remote_arg *)arg; 422 + mode = kcov_get_mode(remote_arg->trace_mode); 423 + if (mode < 0) 424 + return mode; 425 + if (remote_arg->area_size > LONG_MAX / sizeof(unsigned long)) 426 + return -EINVAL; 427 + kcov->mode = mode; 428 + t->kcov = kcov; 429 + kcov->t = t; 430 + kcov->remote = true; 431 + kcov->remote_size = remote_arg->area_size; 432 + spin_lock(&kcov_remote_lock); 433 + for (i = 0; i < remote_arg->num_handles; i++) { 434 + kcov_debug("handle %llx\n", remote_arg->handles[i]); 435 + if (!kcov_check_handle(remote_arg->handles[i], 436 + false, true, false)) { 437 + spin_unlock(&kcov_remote_lock); 438 + kcov_disable(t, kcov); 439 + return -EINVAL; 440 + } 441 + remote = kcov_remote_add(kcov, remote_arg->handles[i]); 442 + if (IS_ERR(remote)) { 443 + spin_unlock(&kcov_remote_lock); 444 + kcov_disable(t, kcov); 445 + return PTR_ERR(remote); 446 + } 447 + } 448 + if (remote_arg->common_handle) { 449 + kcov_debug("common handle %llx\n", 450 + remote_arg->common_handle); 451 + if (!kcov_check_handle(remote_arg->common_handle, 452 + true, false, false)) { 453 + spin_unlock(&kcov_remote_lock); 454 + kcov_disable(t, kcov); 455 + return -EINVAL; 456 + } 457 + remote = kcov_remote_add(kcov, 458 + remote_arg->common_handle); 459 + if (IS_ERR(remote)) { 460 + spin_unlock(&kcov_remote_lock); 461 + kcov_disable(t, kcov); 462 + return PTR_ERR(remote); 463 + } 464 + t->kcov_handle = remote_arg->common_handle; 465 + } 466 + spin_unlock(&kcov_remote_lock); 467 + /* Put either in kcov_task_exit() or in KCOV_DISABLE. */ 468 + kcov_get(kcov); 613 469 return 0; 614 470 default: 615 471 return -ENOTTY; ··· 674 422 { 675 423 struct kcov *kcov; 676 424 int res; 425 + struct kcov_remote_arg *remote_arg = NULL; 426 + unsigned int remote_num_handles; 427 + unsigned long remote_arg_size; 428 + 429 + if (cmd == KCOV_REMOTE_ENABLE) { 430 + if (get_user(remote_num_handles, (unsigned __user *)(arg + 431 + offsetof(struct kcov_remote_arg, num_handles)))) 432 + return -EFAULT; 433 + if (remote_num_handles > KCOV_REMOTE_MAX_HANDLES) 434 + return -EINVAL; 435 + remote_arg_size = struct_size(remote_arg, handles, 436 + remote_num_handles); 437 + remote_arg = memdup_user((void __user *)arg, remote_arg_size); 438 + if (IS_ERR(remote_arg)) 439 + return PTR_ERR(remote_arg); 440 + if (remote_arg->num_handles != remote_num_handles) { 441 + kfree(remote_arg); 442 + return -EINVAL; 443 + } 444 + arg = (unsigned long)remote_arg; 445 + } 677 446 678 447 kcov = filep->private_data; 679 448 spin_lock(&kcov->lock); 680 449 res = kcov_ioctl_locked(kcov, cmd, arg); 681 450 spin_unlock(&kcov->lock); 451 + 452 + kfree(remote_arg); 453 + 682 454 return res; 683 455 } 684 456 ··· 713 437 .mmap = kcov_mmap, 714 438 .release = kcov_close, 715 439 }; 440 + 441 + /* 442 + * kcov_remote_start() and kcov_remote_stop() can be used to annotate a section 443 + * of code in a kernel background thread to allow kcov to be used to collect 444 + * coverage from that part of code. 445 + * 446 + * The handle argument of kcov_remote_start() identifies a code section that is 447 + * used for coverage collection. A userspace process passes this handle to 448 + * KCOV_REMOTE_ENABLE ioctl to make the used kcov device start collecting 449 + * coverage for the code section identified by this handle. 450 + * 451 + * The usage of these annotations in the kernel code is different depending on 452 + * the type of the kernel thread whose code is being annotated. 453 + * 454 + * For global kernel threads that are spawned in a limited number of instances 455 + * (e.g. one USB hub_event() worker thread is spawned per USB HCD), each 456 + * instance must be assigned a unique 4-byte instance id. The instance id is 457 + * then combined with a 1-byte subsystem id to get a handle via 458 + * kcov_remote_handle(subsystem_id, instance_id). 459 + * 460 + * For local kernel threads that are spawned from system calls handler when a 461 + * user interacts with some kernel interface (e.g. vhost workers), a handle is 462 + * passed from a userspace process as the common_handle field of the 463 + * kcov_remote_arg struct (note, that the user must generate a handle by using 464 + * kcov_remote_handle() with KCOV_SUBSYSTEM_COMMON as the subsystem id and an 465 + * arbitrary 4-byte non-zero number as the instance id). This common handle 466 + * then gets saved into the task_struct of the process that issued the 467 + * KCOV_REMOTE_ENABLE ioctl. When this proccess issues system calls that spawn 468 + * kernel threads, the common handle must be retrived via kcov_common_handle() 469 + * and passed to the spawned threads via custom annotations. Those kernel 470 + * threads must in turn be annotated with kcov_remote_start(common_handle) and 471 + * kcov_remote_stop(). All of the threads that are spawned by the same process 472 + * obtain the same handle, hence the name "common". 473 + * 474 + * See Documentation/dev-tools/kcov.rst for more details. 475 + * 476 + * Internally, this function looks up the kcov device associated with the 477 + * provided handle, allocates an area for coverage collection, and saves the 478 + * pointers to kcov and area into the current task_struct to allow coverage to 479 + * be collected via __sanitizer_cov_trace_pc() 480 + * In turns kcov_remote_stop() clears those pointers from task_struct to stop 481 + * collecting coverage and copies all collected coverage into the kcov area. 482 + */ 483 + void kcov_remote_start(u64 handle) 484 + { 485 + struct kcov_remote *remote; 486 + void *area; 487 + struct task_struct *t; 488 + unsigned int size; 489 + enum kcov_mode mode; 490 + int sequence; 491 + 492 + if (WARN_ON(!kcov_check_handle(handle, true, true, true))) 493 + return; 494 + if (WARN_ON(!in_task())) 495 + return; 496 + t = current; 497 + /* 498 + * Check that kcov_remote_start is not called twice 499 + * nor called by user tasks (with enabled kcov). 500 + */ 501 + if (WARN_ON(t->kcov)) 502 + return; 503 + 504 + kcov_debug("handle = %llx\n", handle); 505 + 506 + spin_lock(&kcov_remote_lock); 507 + remote = kcov_remote_find(handle); 508 + if (!remote) { 509 + kcov_debug("no remote found"); 510 + spin_unlock(&kcov_remote_lock); 511 + return; 512 + } 513 + /* Put in kcov_remote_stop(). */ 514 + kcov_get(remote->kcov); 515 + t->kcov = remote->kcov; 516 + /* 517 + * Read kcov fields before unlock to prevent races with 518 + * KCOV_DISABLE / kcov_remote_reset(). 519 + */ 520 + size = remote->kcov->remote_size; 521 + mode = remote->kcov->mode; 522 + sequence = remote->kcov->sequence; 523 + area = kcov_remote_area_get(size); 524 + spin_unlock(&kcov_remote_lock); 525 + 526 + if (!area) { 527 + area = vmalloc(size * sizeof(unsigned long)); 528 + if (!area) { 529 + t->kcov = NULL; 530 + kcov_put(remote->kcov); 531 + return; 532 + } 533 + } 534 + /* Reset coverage size. */ 535 + *(u64 *)area = 0; 536 + 537 + kcov_debug("area = %px, size = %u", area, size); 538 + 539 + kcov_start(t, size, area, mode, sequence); 540 + 541 + } 542 + EXPORT_SYMBOL(kcov_remote_start); 543 + 544 + static void kcov_move_area(enum kcov_mode mode, void *dst_area, 545 + unsigned int dst_area_size, void *src_area) 546 + { 547 + u64 word_size = sizeof(unsigned long); 548 + u64 count_size, entry_size_log; 549 + u64 dst_len, src_len; 550 + void *dst_entries, *src_entries; 551 + u64 dst_occupied, dst_free, bytes_to_move, entries_moved; 552 + 553 + kcov_debug("%px %u <= %px %lu\n", 554 + dst_area, dst_area_size, src_area, *(unsigned long *)src_area); 555 + 556 + switch (mode) { 557 + case KCOV_MODE_TRACE_PC: 558 + dst_len = READ_ONCE(*(unsigned long *)dst_area); 559 + src_len = *(unsigned long *)src_area; 560 + count_size = sizeof(unsigned long); 561 + entry_size_log = __ilog2_u64(sizeof(unsigned long)); 562 + break; 563 + case KCOV_MODE_TRACE_CMP: 564 + dst_len = READ_ONCE(*(u64 *)dst_area); 565 + src_len = *(u64 *)src_area; 566 + count_size = sizeof(u64); 567 + BUILD_BUG_ON(!is_power_of_2(KCOV_WORDS_PER_CMP)); 568 + entry_size_log = __ilog2_u64(sizeof(u64) * KCOV_WORDS_PER_CMP); 569 + break; 570 + default: 571 + WARN_ON(1); 572 + return; 573 + } 574 + 575 + /* As arm can't divide u64 integers use log of entry size. */ 576 + if (dst_len > ((dst_area_size * word_size - count_size) >> 577 + entry_size_log)) 578 + return; 579 + dst_occupied = count_size + (dst_len << entry_size_log); 580 + dst_free = dst_area_size * word_size - dst_occupied; 581 + bytes_to_move = min(dst_free, src_len << entry_size_log); 582 + dst_entries = dst_area + dst_occupied; 583 + src_entries = src_area + count_size; 584 + memcpy(dst_entries, src_entries, bytes_to_move); 585 + entries_moved = bytes_to_move >> entry_size_log; 586 + 587 + switch (mode) { 588 + case KCOV_MODE_TRACE_PC: 589 + WRITE_ONCE(*(unsigned long *)dst_area, dst_len + entries_moved); 590 + break; 591 + case KCOV_MODE_TRACE_CMP: 592 + WRITE_ONCE(*(u64 *)dst_area, dst_len + entries_moved); 593 + break; 594 + default: 595 + break; 596 + } 597 + } 598 + 599 + /* See the comment before kcov_remote_start() for usage details. */ 600 + void kcov_remote_stop(void) 601 + { 602 + struct task_struct *t = current; 603 + struct kcov *kcov = t->kcov; 604 + void *area = t->kcov_area; 605 + unsigned int size = t->kcov_size; 606 + int sequence = t->kcov_sequence; 607 + 608 + if (!kcov) { 609 + kcov_debug("no kcov found\n"); 610 + return; 611 + } 612 + 613 + kcov_stop(t); 614 + t->kcov = NULL; 615 + 616 + spin_lock(&kcov->lock); 617 + /* 618 + * KCOV_DISABLE could have been called between kcov_remote_start() 619 + * and kcov_remote_stop(), hence the check. 620 + */ 621 + kcov_debug("move if: %d == %d && %d\n", 622 + sequence, kcov->sequence, (int)kcov->remote); 623 + if (sequence == kcov->sequence && kcov->remote) 624 + kcov_move_area(kcov->mode, kcov->area, kcov->size, area); 625 + spin_unlock(&kcov->lock); 626 + 627 + spin_lock(&kcov_remote_lock); 628 + kcov_remote_area_put(area, size); 629 + spin_unlock(&kcov_remote_lock); 630 + 631 + kcov_put(kcov); 632 + } 633 + EXPORT_SYMBOL(kcov_remote_stop); 634 + 635 + /* See the comment before kcov_remote_start() for usage details. */ 636 + u64 kcov_common_handle(void) 637 + { 638 + return current->kcov_handle; 639 + } 640 + EXPORT_SYMBOL(kcov_common_handle); 716 641 717 642 static int __init kcov_init(void) 718 643 {