1 #define pr_fmt(fmt) "kcov: " fmt
3 #define DISABLE_BRANCH_PROFILING
4 #include <linux/compiler.h>
5 #include <linux/types.h>
6 #include <linux/file.h>
9 #include <linux/printk.h>
10 #include <linux/sched.h>
11 #include <linux/slab.h>
12 #include <linux/spinlock.h>
13 #include <linux/vmalloc.h>
14 #include <linux/debugfs.h>
15 #include <linux/uaccess.h>
16 #include <linux/kcov.h>
19 * kcov descriptor (one per opened debugfs file).
20 * State transitions of the descriptor:
21 * - initial state after open()
22 * - then there must be a single ioctl(KCOV_INIT_TRACE) call
23 * - then, mmap() call (several calls are allowed but not useful)
24 * - then, repeated enable/disable for a task (only one task a time allowed)
28 * Reference counter. We keep one for:
29 * - opened file descriptor
30 * - task with enabled coverage (we can't unwire it from another task)
33 /* The lock protects mode, size, area and t. */
36 /* Size of arena (in long's for KCOV_MODE_TRACE). */
38 /* Coverage buffer shared with user space. */
40 /* Task for which we collect coverage, or NULL. */
41 struct task_struct
*t
;
45 * Entry point from instrumented code.
46 * This is called once per basic-block/edge.
48 void notrace
__sanitizer_cov_trace_pc(void)
50 struct task_struct
*t
;
55 * We are interested in code coverage as a function of a syscall inputs,
56 * so we ignore code executed in interrupts.
57 * The checks for whether we are in an interrupt are open-coded, because
58 * 1. We can't use in_interrupt() here, since it also returns true
59 * when we are inside local_bh_disable() section.
60 * 2. We don't want to use (in_irq() | in_serving_softirq() | in_nmi()),
61 * since that leads to slower generated code (three separate tests,
62 * one for each of the flags).
64 if (!t
|| (preempt_count() & (HARDIRQ_MASK
| SOFTIRQ_OFFSET
67 mode
= READ_ONCE(t
->kcov_mode
);
68 if (mode
== KCOV_MODE_TRACE
) {
73 * There is some code that runs in interrupts but for which
74 * in_interrupt() returns false (e.g. preempt_schedule_irq()).
75 * READ_ONCE()/barrier() effectively provides load-acquire wrt
76 * interrupts, there are paired barrier()/WRITE_ONCE() in
77 * kcov_ioctl_locked().
81 /* The first word is number of subsequent PCs. */
82 pos
= READ_ONCE(area
[0]) + 1;
83 if (likely(pos
< t
->kcov_size
)) {
85 WRITE_ONCE(area
[0], pos
);
89 EXPORT_SYMBOL(__sanitizer_cov_trace_pc
);
91 static void kcov_get(struct kcov
*kcov
)
93 atomic_inc(&kcov
->refcount
);
96 static void kcov_put(struct kcov
*kcov
)
98 if (atomic_dec_and_test(&kcov
->refcount
)) {
104 void kcov_task_init(struct task_struct
*t
)
106 WRITE_ONCE(t
->kcov_mode
, KCOV_MODE_DISABLED
);
113 void kcov_task_exit(struct task_struct
*t
)
120 spin_lock(&kcov
->lock
);
121 if (WARN_ON(kcov
->t
!= t
)) {
122 spin_unlock(&kcov
->lock
);
125 /* Just to not leave dangling references behind. */
128 spin_unlock(&kcov
->lock
);
132 static int kcov_mmap(struct file
*filep
, struct vm_area_struct
*vma
)
136 struct kcov
*kcov
= vma
->vm_file
->private_data
;
137 unsigned long size
, off
;
140 area
= vmalloc_user(vma
->vm_end
- vma
->vm_start
);
144 spin_lock(&kcov
->lock
);
145 size
= kcov
->size
* sizeof(unsigned long);
146 if (kcov
->mode
== KCOV_MODE_DISABLED
|| vma
->vm_pgoff
!= 0 ||
147 vma
->vm_end
- vma
->vm_start
!= size
) {
153 vma
->vm_flags
|= VM_DONTEXPAND
;
154 spin_unlock(&kcov
->lock
);
155 for (off
= 0; off
< size
; off
+= PAGE_SIZE
) {
156 page
= vmalloc_to_page(kcov
->area
+ off
);
157 if (vm_insert_page(vma
, vma
->vm_start
+ off
, page
))
158 WARN_ONCE(1, "vm_insert_page() failed");
163 spin_unlock(&kcov
->lock
);
168 static int kcov_open(struct inode
*inode
, struct file
*filep
)
172 kcov
= kzalloc(sizeof(*kcov
), GFP_KERNEL
);
175 atomic_set(&kcov
->refcount
, 1);
176 spin_lock_init(&kcov
->lock
);
177 filep
->private_data
= kcov
;
178 return nonseekable_open(inode
, filep
);
181 static int kcov_close(struct inode
*inode
, struct file
*filep
)
183 kcov_put(filep
->private_data
);
187 static int kcov_ioctl_locked(struct kcov
*kcov
, unsigned int cmd
,
190 struct task_struct
*t
;
191 unsigned long size
, unused
;
194 case KCOV_INIT_TRACE
:
196 * Enable kcov in trace mode and setup buffer size.
197 * Must happen before anything else.
199 if (kcov
->mode
!= KCOV_MODE_DISABLED
)
202 * Size must be at least 2 to hold current position and one PC.
203 * Later we allocate size * sizeof(unsigned long) memory,
204 * that must not overflow.
207 if (size
< 2 || size
> INT_MAX
/ sizeof(unsigned long))
210 kcov
->mode
= KCOV_MODE_TRACE
;
214 * Enable coverage for the current task.
215 * At this point user must have been enabled trace mode,
216 * and mmapped the file. Coverage collection is disabled only
217 * at task exit or voluntary by KCOV_DISABLE. After that it can
218 * be enabled for another task.
221 if (unused
!= 0 || kcov
->mode
== KCOV_MODE_DISABLED
||
225 if (kcov
->t
!= NULL
|| t
->kcov
!= NULL
)
227 /* Cache in task struct for performance. */
228 t
->kcov_size
= kcov
->size
;
229 t
->kcov_area
= kcov
->area
;
230 /* See comment in __sanitizer_cov_trace_pc(). */
232 WRITE_ONCE(t
->kcov_mode
, kcov
->mode
);
235 /* This is put either in kcov_task_exit() or in KCOV_DISABLE. */
239 /* Disable coverage for the current task. */
241 if (unused
!= 0 || current
->kcov
!= kcov
)
244 if (WARN_ON(kcov
->t
!= t
))
255 static long kcov_ioctl(struct file
*filep
, unsigned int cmd
, unsigned long arg
)
260 kcov
= filep
->private_data
;
261 spin_lock(&kcov
->lock
);
262 res
= kcov_ioctl_locked(kcov
, cmd
, arg
);
263 spin_unlock(&kcov
->lock
);
267 static const struct file_operations kcov_fops
= {
269 .unlocked_ioctl
= kcov_ioctl
,
271 .release
= kcov_close
,
274 static int __init
kcov_init(void)
277 * The kcov debugfs file won't ever get removed and thus,
278 * there is no need to protect it against removal races. The
279 * use of debugfs_create_file_unsafe() is actually safe here.
281 if (!debugfs_create_file_unsafe("kcov", 0600, NULL
, NULL
, &kcov_fops
)) {
282 pr_err("failed to create kcov in debugfs\n");
288 device_initcall(kcov_init
);