On Wed, 10 Sep 2025 21:22:11 +0100 Lorenzo Stoakes <[email protected]> wrote:
> We can use the mmap insert pages functionality provided for use in > mmap_prepare to insert the kcov pages as required. > > This does necessitate an allocation, but since it's in the mmap path this > doesn't seem egregious. The allocation/freeing of the pages array is > handled automatically by vma_desc_set_mixedmap_pages() and the mapping > logic. > > Signed-off-by: Lorenzo Stoakes <[email protected]> > --- > kernel/kcov.c | 42 ++++++++++++++++++++++++++---------------- > 1 file changed, 26 insertions(+), 16 deletions(-) > > diff --git a/kernel/kcov.c b/kernel/kcov.c > index 1d85597057e1..2bcf403e5f6f 100644 > --- a/kernel/kcov.c > +++ b/kernel/kcov.c > @@ -484,31 +484,41 @@ void kcov_task_exit(struct task_struct *t) > kcov_put(kcov); > } > > -static int kcov_mmap(struct file *filep, struct vm_area_struct *vma) > +static int kcov_mmap_error(int err) > +{ > + pr_warn_once("kcov: vm_insert_page() failed\n"); > + return err; > +} > + > +static int kcov_mmap_prepare(struct vm_area_desc *desc) > { > int res = 0; > - struct kcov *kcov = vma->vm_file->private_data; > - unsigned long size, off; > - struct page *page; > + struct kcov *kcov = desc->file->private_data; > + unsigned long size, nr_pages, i; > + struct page **pages; > unsigned long flags; > > spin_lock_irqsave(&kcov->lock, flags); > size = kcov->size * sizeof(unsigned long); > - if (kcov->area == NULL || vma->vm_pgoff != 0 || > - vma->vm_end - vma->vm_start != size) { > + if (kcov->area == NULL || desc->pgoff != 0 || > + vma_desc_size(desc) != size) { > res = -EINVAL; > goto exit; > } > spin_unlock_irqrestore(&kcov->lock, flags); > - vm_flags_set(vma, VM_DONTEXPAND); > - for (off = 0; off < size; off += PAGE_SIZE) { > - page = vmalloc_to_page(kcov->area + off); > - res = vm_insert_page(vma, vma->vm_start + off, page); > - if (res) { > - pr_warn_once("kcov: vm_insert_page() failed\n"); > - return res; > - } > - } > + > + desc->vm_flags |= VM_DONTEXPAND; > + nr_pages = size >> PAGE_SHIFT; > + > + pages = mmap_action_mixedmap_pages(&desc->action, desc->start, > + nr_pages); Hi Lorenzo, Not sure if it belongs here before the EINVAL tests, but it looks like kcov->size doesn't have any page alignment. I think size could be 4000 bytes other unaligned values, so nr_pages should round up. -chris
