1 #define pr_fmt(fmt) "kcov: " fmt
3 #include <linux/compiler.h>
4 #include <linux/types.h>
5 #include <linux/file.h>
8 #include <linux/printk.h>
9 #include <linux/slab.h>
10 #include <linux/spinlock.h>
11 #include <linux/vmalloc.h>
12 #include <linux/debugfs.h>
13 #include <linux/uaccess.h>
14 #include <linux/kcov.h>
17 * kcov descriptor (one per opened debugfs file).
18 * State transitions of the descriptor:
19 * - initial state after open()
20 * - then there must be a single ioctl(KCOV_INIT_TRACE) call
21 * - then, mmap() call (several calls are allowed but not useful)
22 * - then, repeated enable/disable for a task (only one task a time allowed)
26 * Reference counter. We keep one for:
27 * - opened file descriptor
28 * - task with enabled coverage (we can't unwire it from another task)
31 /* The lock protects mode, size, area and t. */
34 /* Size of arena (in long's for KCOV_MODE_TRACE). */
36 /* Coverage buffer shared with user space. */
38 /* Task for which we collect coverage, or NULL. */
39 struct task_struct
*t
;
43 * Entry point from instrumented code.
44 * This is called once per basic-block/edge.
46 void __sanitizer_cov_trace_pc(void)
48 struct task_struct
*t
;
53 * We are interested in code coverage as a function of a syscall inputs,
54 * so we ignore code executed in interrupts.
56 if (!t
|| in_interrupt())
58 mode
= READ_ONCE(t
->kcov_mode
);
59 if (mode
== KCOV_MODE_TRACE
) {
64 * There is some code that runs in interrupts but for which
65 * in_interrupt() returns false (e.g. preempt_schedule_irq()).
66 * READ_ONCE()/barrier() effectively provides load-acquire wrt
67 * interrupts, there are paired barrier()/WRITE_ONCE() in
68 * kcov_ioctl_locked().
72 /* The first word is number of subsequent PCs. */
73 pos
= READ_ONCE(area
[0]) + 1;
74 if (likely(pos
< t
->kcov_size
)) {
76 WRITE_ONCE(area
[0], pos
);
80 EXPORT_SYMBOL(__sanitizer_cov_trace_pc
);
82 static void kcov_get(struct kcov
*kcov
)
84 atomic_inc(&kcov
->refcount
);
87 static void kcov_put(struct kcov
*kcov
)
89 if (atomic_dec_and_test(&kcov
->refcount
)) {
95 void kcov_task_init(struct task_struct
*t
)
97 t
->kcov_mode
= KCOV_MODE_DISABLED
;
103 void kcov_task_exit(struct task_struct
*t
)
110 spin_lock(&kcov
->lock
);
111 if (WARN_ON(kcov
->t
!= t
)) {
112 spin_unlock(&kcov
->lock
);
115 /* Just to not leave dangling references behind. */
118 spin_unlock(&kcov
->lock
);
122 static int kcov_mmap(struct file
*filep
, struct vm_area_struct
*vma
)
126 struct kcov
*kcov
= vma
->vm_file
->private_data
;
127 unsigned long size
, off
;
130 area
= vmalloc_user(vma
->vm_end
- vma
->vm_start
);
134 spin_lock(&kcov
->lock
);
135 size
= kcov
->size
* sizeof(unsigned long);
136 if (kcov
->mode
== KCOV_MODE_DISABLED
|| vma
->vm_pgoff
!= 0 ||
137 vma
->vm_end
- vma
->vm_start
!= size
) {
143 vma
->vm_flags
|= VM_DONTEXPAND
;
144 spin_unlock(&kcov
->lock
);
145 for (off
= 0; off
< size
; off
+= PAGE_SIZE
) {
146 page
= vmalloc_to_page(kcov
->area
+ off
);
147 if (vm_insert_page(vma
, vma
->vm_start
+ off
, page
))
148 WARN_ONCE(1, "vm_insert_page() failed");
153 spin_unlock(&kcov
->lock
);
158 static int kcov_open(struct inode
*inode
, struct file
*filep
)
162 kcov
= kzalloc(sizeof(*kcov
), GFP_KERNEL
);
165 atomic_set(&kcov
->refcount
, 1);
166 spin_lock_init(&kcov
->lock
);
167 filep
->private_data
= kcov
;
168 return nonseekable_open(inode
, filep
);
171 static int kcov_close(struct inode
*inode
, struct file
*filep
)
173 kcov_put(filep
->private_data
);
177 static int kcov_ioctl_locked(struct kcov
*kcov
, unsigned int cmd
,
180 struct task_struct
*t
;
181 unsigned long size
, unused
;
184 case KCOV_INIT_TRACE
:
186 * Enable kcov in trace mode and setup buffer size.
187 * Must happen before anything else.
189 if (kcov
->mode
!= KCOV_MODE_DISABLED
)
192 * Size must be at least 2 to hold current position and one PC.
193 * Later we allocate size * sizeof(unsigned long) memory,
194 * that must not overflow.
197 if (size
< 2 || size
> INT_MAX
/ sizeof(unsigned long))
200 kcov
->mode
= KCOV_MODE_TRACE
;
204 * Enable coverage for the current task.
205 * At this point user must have been enabled trace mode,
206 * and mmapped the file. Coverage collection is disabled only
207 * at task exit or voluntary by KCOV_DISABLE. After that it can
208 * be enabled for another task.
211 if (unused
!= 0 || kcov
->mode
== KCOV_MODE_DISABLED
||
217 /* Cache in task struct for performance. */
218 t
->kcov_size
= kcov
->size
;
219 t
->kcov_area
= kcov
->area
;
220 /* See comment in __sanitizer_cov_trace_pc(). */
222 WRITE_ONCE(t
->kcov_mode
, kcov
->mode
);
225 /* This is put either in kcov_task_exit() or in KCOV_DISABLE. */
229 /* Disable coverage for the current task. */
231 if (unused
!= 0 || current
->kcov
!= kcov
)
234 if (WARN_ON(kcov
->t
!= t
))
245 static long kcov_ioctl(struct file
*filep
, unsigned int cmd
, unsigned long arg
)
250 kcov
= filep
->private_data
;
251 spin_lock(&kcov
->lock
);
252 res
= kcov_ioctl_locked(kcov
, cmd
, arg
);
253 spin_unlock(&kcov
->lock
);
257 static const struct file_operations kcov_fops
= {
259 .unlocked_ioctl
= kcov_ioctl
,
261 .release
= kcov_close
,
264 static int __init
kcov_init(void)
266 if (!debugfs_create_file("kcov", 0600, NULL
, NULL
, &kcov_fops
)) {
267 pr_err("failed to create kcov in debugfs\n");
273 device_initcall(kcov_init
);