Commit b3d7fe86 authored by Aleksandr Nogikh's avatar Aleksandr Nogikh Committed by Linus Torvalds

kcov: properly handle subsequent mmap calls

Allocate the kcov buffer during KCOV_MODE_INIT in order to untie mmapping
of a kcov instance and the actual coverage collection process. Modify
kcov_mmap, so that it can be reliably used any number of times once
KCOV_MODE_INIT has succeeded.

These changes to the user-facing interface of the tool only weaken the
preconditions, so all existing user space code should remain compatible
with the new version.

Link: https://lkml.kernel.org/r/20220117153634.150357-3-nogikh@google.comSigned-off-by: default avatarAleksandr Nogikh <nogikh@google.com>
Reviewed-by: default avatarDmitry Vyukov <dvyukov@google.com>
Reviewed-by: default avatarAndrey Konovalov <andreyknvl@gmail.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Marco Elver <elver@google.com>
Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Cc: Taras Madan <tarasmadan@google.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 17581aa1
...@@ -459,37 +459,28 @@ void kcov_task_exit(struct task_struct *t) ...@@ -459,37 +459,28 @@ void kcov_task_exit(struct task_struct *t)
static int kcov_mmap(struct file *filep, struct vm_area_struct *vma) static int kcov_mmap(struct file *filep, struct vm_area_struct *vma)
{ {
int res = 0; int res = 0;
void *area;
struct kcov *kcov = vma->vm_file->private_data; struct kcov *kcov = vma->vm_file->private_data;
unsigned long size, off; unsigned long size, off;
struct page *page; struct page *page;
unsigned long flags; unsigned long flags;
area = vmalloc_user(vma->vm_end - vma->vm_start);
if (!area)
return -ENOMEM;
spin_lock_irqsave(&kcov->lock, flags); spin_lock_irqsave(&kcov->lock, flags);
size = kcov->size * sizeof(unsigned long); size = kcov->size * sizeof(unsigned long);
if (kcov->mode != KCOV_MODE_INIT || vma->vm_pgoff != 0 || if (kcov->area == NULL || vma->vm_pgoff != 0 ||
vma->vm_end - vma->vm_start != size) { vma->vm_end - vma->vm_start != size) {
res = -EINVAL; res = -EINVAL;
goto exit; goto exit;
} }
if (!kcov->area) { spin_unlock_irqrestore(&kcov->lock, flags);
kcov->area = area; vma->vm_flags |= VM_DONTEXPAND;
vma->vm_flags |= VM_DONTEXPAND; for (off = 0; off < size; off += PAGE_SIZE) {
spin_unlock_irqrestore(&kcov->lock, flags); page = vmalloc_to_page(kcov->area + off);
for (off = 0; off < size; off += PAGE_SIZE) { if (vm_insert_page(vma, vma->vm_start + off, page))
page = vmalloc_to_page(kcov->area + off); WARN_ONCE(1, "vm_insert_page() failed");
if (vm_insert_page(vma, vma->vm_start + off, page))
WARN_ONCE(1, "vm_insert_page() failed");
}
return 0;
} }
return 0;
exit: exit:
spin_unlock_irqrestore(&kcov->lock, flags); spin_unlock_irqrestore(&kcov->lock, flags);
vfree(area);
return res; return res;
} }
...@@ -674,6 +665,7 @@ static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) ...@@ -674,6 +665,7 @@ static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
unsigned int remote_num_handles; unsigned int remote_num_handles;
unsigned long remote_arg_size; unsigned long remote_arg_size;
unsigned long size, flags; unsigned long size, flags;
void *area;
kcov = filep->private_data; kcov = filep->private_data;
switch (cmd) { switch (cmd) {
...@@ -683,17 +675,21 @@ static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) ...@@ -683,17 +675,21 @@ static long kcov_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
* Must happen before anything else. * Must happen before anything else.
* *
* First check the size argument - it must be at least 2 * First check the size argument - it must be at least 2
* to hold the current position and one PC. Later we allocate * to hold the current position and one PC.
* size * sizeof(unsigned long) memory, that must not overflow.
*/ */
size = arg; size = arg;
if (size < 2 || size > INT_MAX / sizeof(unsigned long)) if (size < 2 || size > INT_MAX / sizeof(unsigned long))
return -EINVAL; return -EINVAL;
area = vmalloc_user(size * sizeof(unsigned long));
if (area == NULL)
return -ENOMEM;
spin_lock_irqsave(&kcov->lock, flags); spin_lock_irqsave(&kcov->lock, flags);
if (kcov->mode != KCOV_MODE_DISABLED) { if (kcov->mode != KCOV_MODE_DISABLED) {
spin_unlock_irqrestore(&kcov->lock, flags); spin_unlock_irqrestore(&kcov->lock, flags);
vfree(area);
return -EBUSY; return -EBUSY;
} }
kcov->area = area;
kcov->size = size; kcov->size = size;
kcov->mode = KCOV_MODE_INIT; kcov->mode = KCOV_MODE_INIT;
spin_unlock_irqrestore(&kcov->lock, flags); spin_unlock_irqrestore(&kcov->lock, flags);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment