On Thu, May 02, 2024 at 03:30:32PM +0200, David Hildenbrand wrote:
> On 30.04.24 13:13, Vincent Donnefort wrote:
> > In preparation for allowing the user-space to map a ring-buffer, add
> > a set of mapping functions:
> > 
> >    ring_buffer_{map,unmap}()
> > 
> > And controls on the ring-buffer:
> > 
> >    ring_buffer_map_get_reader()  /* swap reader and head */
> > 
> > Mapping the ring-buffer also involves:
> > 
> >    A unique ID for each subbuf of the ring-buffer, currently they are
> >    only identified through their in-kernel VA.
> > 
> >    A meta-page, where are stored ring-buffer statistics and a
> >    description for the current reader
> > 
> > The linear mapping exposes the meta-page, and each subbuf of the
> > ring-buffer, ordered following their unique ID, assigned during the
> > first mapping.
> > 
> > Once mapped, no subbuf can get in or out of the ring-buffer: the buffer
> > size will remain unmodified and the splice enabling functions will in
> > reality simply memcpy the data instead of swapping subbufs.
> > 
> > CC: <linux...@kvack.org>
> > Signed-off-by: Vincent Donnefort <vdonnef...@google.com>
> > 
> > diff --git a/include/linux/ring_buffer.h b/include/linux/ring_buffer.h
> > index dc5ae4e96aee..96d2140b471e 100644
> > --- a/include/linux/ring_buffer.h
> > +++ b/include/linux/ring_buffer.h
> 
> [...]
> 
> > +/*
> > + *   +--------------+  pgoff == 0
> > + *   |   meta page  |
> > + *   +--------------+  pgoff == 1
> > + *   | subbuffer 0  |
> > + *   |              |
> > + *   +--------------+  pgoff == (1 + (1 << subbuf_order))
> > + *   | subbuffer 1  |
> > + *   |              |
> > + *         ...
> > + */
> > +#ifdef CONFIG_MMU
> > +static int __rb_map_vma(struct ring_buffer_per_cpu *cpu_buffer,
> > +                   struct vm_area_struct *vma)
> > +{
> > +   unsigned long nr_subbufs, nr_pages, vma_pages, pgoff = vma->vm_pgoff;
> > +   unsigned int subbuf_pages, subbuf_order;
> > +   struct page **pages;
> > +   int p = 0, s = 0;
> > +   int err;
> > +
> > +   /* Refuse MP_PRIVATE or writable mappings */
> > +   if (vma->vm_flags & VM_WRITE || vma->vm_flags & VM_EXEC ||
> > +       !(vma->vm_flags & VM_MAYSHARE))
> > +           return -EPERM;
> > +
> > +   /*
> > +    * Make sure the mapping cannot become writable later. Also tell the VM
> > +    * to not touch these pages (VM_DONTCOPY | VM_DONTEXPAND). Finally,
> > +    * prevent migration, GUP and dump (VM_IO).
> > +    */
> > +   vm_flags_mod(vma, VM_DONTCOPY | VM_DONTEXPAND | VM_IO, VM_MAYWRITE);
> > +
> > +   lockdep_assert_held(&cpu_buffer->mapping_lock);
> > +
> > +   subbuf_order = cpu_buffer->buffer->subbuf_order;
> > +   subbuf_pages = 1 << subbuf_order;
> > +
> > +   nr_subbufs = cpu_buffer->nr_pages + 1; /* + reader-subbuf */
> > +   nr_pages = ((nr_subbufs) << subbuf_order) - pgoff + 1; /* + meta-page */
> > +
> > +   vma_pages = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
> > +   if (!vma_pages || vma_pages > nr_pages)
> > +           return -EINVAL;
> > +
> > +   nr_pages = vma_pages;
> > +
> > +   pages = kcalloc(nr_pages, sizeof(*pages), GFP_KERNEL);
> > +   if (!pages)
> > +           return -ENOMEM;
> > +
> > +   if (!pgoff) {
> > +           pages[p++] = virt_to_page(cpu_buffer->meta_page);
> > +
> > +           /*
> > +            * TODO: Align sub-buffers on their size, once
> > +            * vm_insert_pages() supports the zero-page.
> > +            */
> > +   } else {
> > +           /* Skip the meta-page */
> > +           pgoff--;
> > +
> > +           if (pgoff % subbuf_pages) {
> > +                   err = -EINVAL;
> > +                   goto out;
> > +           }
> > +
> > +           s += pgoff / subbuf_pages;
> > +   }
> > +
> > +   while (s < nr_subbufs && p < nr_pages) {
> > +           struct page *page = virt_to_page(cpu_buffer->subbuf_ids[s]);
> > +           int off = 0;
> > +
> > +           for (; off < (1 << (subbuf_order)); off++, page++) {
> > +                   if (p >= nr_pages)
> > +                           break;
> > +
> > +                   pages[p++] = page;
> > +           }
> > +           s++;
> > +   }
> > +
> > +   err = vm_insert_pages(vma, vma->vm_start, pages, &nr_pages);
> 
> Nit: I did not immediately understand if we could end here with p < nr_pages
> (IOW, pages[] not completely filled).
> 
> One source of confusion is the "s < nr_subbufs" check in the while loop: why
> is "p < nr_pages" insufficient?

Hum, indeed, the "s < nr_subbufs" check is superfluous, nr_pages, is already
capped by the number of subbufs, there's no way we can overflow subbuf_ids[].

> 
> 
> For the MM bits:
> 
> Acked-by: David Hildenbrand <da...@redhat.com>

Thanks a lot for having a look at the series, very much appreciated!

> 
> 
> 
> -- 
> Cheers,
> 
> David / dhildenb
> 

Reply via email to