2018-12-24 13:16:12

by Souptick Joarder

[permalink] [raw]
Subject: [PATCH v5 0/9] Use vm_insert_range

v1 -> v2:
Address review comment on mm/memory.c. Add EXPORT_SYMBOL
for vm_insert_range and corrected the documentation part
for this API.

In drivers/gpu/drm/xen/xen_drm_front_gem.c, replace err
with ret as suggested.

In drivers/iommu/dma-iommu.c, handle the scenario of partial
mmap() of large buffer by passing *pages + vma->vm_pgoff* to
vm_insert_range().

v2 -> v3:
Declaration of vm_insert_range() moved to include/linux/mm.h

v3 -> v4:
Address review comments.

In mm/memory.c. Added error check.

In arch/arm/mm/dma-mapping.c, remove part of error check as the
similar is checked inside vm_insert_range.

In rockchip/rockchip_drm_gem.c, vma->vm_pgoff is respected as
this might be passed as non zero value considering partial
mapping of large buffer.

In iommu/dma-iommu.c, count is modifed as (count - vma->vm_pgoff)
to handle partial mapping scenario in v2.

v4 -> v5:
Address review comment on [2/9] and [4/9]

In arch/arm/mm/dma-mapping.c, added the error check which was removed
in v4, as without those error check we might end up overrun the page
array.

In rockchip/rockchip_drm_gem.c, added error check which was removed in
v1, as without this it might overrun page array. Adjusted page_count
parameter before passing it to vm_insert_range().

Souptick Joarder (9):
mm: Introduce new vm_insert_range API
arch/arm/mm/dma-mapping.c: Convert to use vm_insert_range
drivers/firewire/core-iso.c: Convert to use vm_insert_range
drm/rockchip/rockchip_drm_gem.c: Convert to use vm_insert_range
drm/xen/xen_drm_front_gem.c: Convert to use vm_insert_range
iommu/dma-iommu.c: Convert to use vm_insert_range
videobuf2/videobuf2-dma-sg.c: Convert to use vm_insert_range
xen/gntdev.c: Convert to use vm_insert_range
xen/privcmd-buf.c: Convert to use vm_insert_range

arch/arm/mm/dma-mapping.c | 18 ++++------
drivers/firewire/core-iso.c | 15 ++-------
drivers/gpu/drm/rockchip/rockchip_drm_gem.c | 14 ++------
drivers/gpu/drm/xen/xen_drm_front_gem.c | 20 ++++-------
drivers/iommu/dma-iommu.c | 13 ++-----
drivers/media/common/videobuf2/videobuf2-dma-sg.c | 23 ++++---------
drivers/xen/gntdev.c | 11 +++---
drivers/xen/privcmd-buf.c | 8 ++---
include/linux/mm.h | 2 ++
mm/memory.c | 41 +++++++++++++++++++++++
mm/nommu.c | 7 ++++
11 files changed, 83 insertions(+), 89 deletions(-)

--
1.9.1



2018-12-24 15:23:33

by Russell King (Oracle)

[permalink] [raw]
Subject: Re: [PATCH v5 0/9] Use vm_insert_range

Having discussed with Matthew offlist, I think we've come to the
following conclusion - there's a number of drivers that buggily
ignore vm_pgoff.

So, what I proposed is:

static int __vm_insert_range(struct vm_struct *vma, struct page *pages,
size_t num, unsigned long offset)
{
unsigned long count = vma_pages(vma);
unsigned long uaddr = vma->vm_start;
int ret;

/* Fail if the user requested offset is beyond the end of the object */
if (offset > num)
return -ENXIO;

/* Fail if the user requested size exceeds available object size */
if (count > num - offset)
return -ENXIO;

/* Never exceed the number of pages that the user requested */
for (i = 0; i < count; i++) {
ret = vm_insert_page(vma, uaddr, pages[offset + i]);
if (ret < 0)
return ret;
uaddr += PAGE_SIZE;
}

return 0;
}

/*
* Maps an object consisting of `num' `pages', catering for the user's
* requested vm_pgoff
*/
int vm_insert_range(struct vm_struct *vma, struct page *pages, size_t num)
{
return __vm_insert_range(vma, pages, num, vma->vm_pgoff);
}

/*
* Maps a set of pages, always starting at page[0]
*/
int vm_insert_range_buggy(struct vm_struct *vma, struct page *pages, size_t num)
{
return __vm_insert_range(vma, pages, num, 0);
}

With this, drivers such as iommu/dma-iommu.c can be converted thusly:

int iommu_dma_mmap(struct page **pages, size_t size, struct vm_area_struct *vma+)
{
- unsigned long uaddr = vma->vm_start;
- unsigned int i, count = PAGE_ALIGN(size) >> PAGE_SHIFT;
- int ret = -ENXIO;
-
- for (i = vma->vm_pgoff; i < count && uaddr < vma->vm_end; i++) {
- ret = vm_insert_page(vma, uaddr, pages[i]);
- if (ret)
- break;
- uaddr += PAGE_SIZE;
- }
- return ret;
+ return vm_insert_range(vma, pages, PAGE_ALIGN(size) >> PAGE_SHIFT);
}

and drivers such as firewire/core-iso.c:

int fw_iso_buffer_map_vma(struct fw_iso_buffer *buffer,
struct vm_area_struct *vma)
{
- unsigned long uaddr;
- int i, err;
-
- uaddr = vma->vm_start;
- for (i = 0; i < buffer->page_count; i++) {
- err = vm_insert_page(vma, uaddr, buffer->pages[i]);
- if (err)
- return err;
-
- uaddr += PAGE_SIZE;
- }
-
- return 0;
+ return vm_insert_range_buggy(vma, buffer->pages, buffer->page_count);
}

and this gives us something to grep for to find these buggy drivers.

Now, this may not look exactly equivalent, but if you look at
fw_device_op_mmap(), buffer->page_count is basically vma_pages(vma)
at this point, which means this should be equivalent.

We _could_ then at a later date "fix" these drivers to behave according
to the normal vm_pgoff offsetting simply by removing the _buggy suffix
on the function name... and if that causes regressions, it gives us an
easy way to revert (as long as vm_insert_range_buggy() remains
available.)

In the case of firewire/core-iso.c, it currently ignores the mmap offset
entirely, so making the above suggested change would be tantamount to
causing it to return -ENXIO for any non-zero mmap offset.

IMHO, this approach is way simpler, and easier to get it correct at
each call site, rather than the current approach which seems to be
error-prone.

--
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line in suburbia: sync at 12.1Mbps down 622kbps up
According to speedtest.net: 11.9Mbps down 500kbps up

2018-12-26 13:41:31

by Souptick Joarder

[permalink] [raw]
Subject: Re: [PATCH v5 0/9] Use vm_insert_range

On Mon, Dec 24, 2018 at 8:51 PM Russell King - ARM Linux
<[email protected]> wrote:
>
> Having discussed with Matthew offlist, I think we've come to the
> following conclusion - there's a number of drivers that buggily
> ignore vm_pgoff.
>
> So, what I proposed is:
>
> static int __vm_insert_range(struct vm_struct *vma, struct page *pages,
> size_t num, unsigned long offset)
> {
> unsigned long count = vma_pages(vma);
> unsigned long uaddr = vma->vm_start;
> int ret;
>
> /* Fail if the user requested offset is beyond the end of the object */
> if (offset > num)
> return -ENXIO;
>
> /* Fail if the user requested size exceeds available object size */
> if (count > num - offset)
> return -ENXIO;
>
> /* Never exceed the number of pages that the user requested */
> for (i = 0; i < count; i++) {
> ret = vm_insert_page(vma, uaddr, pages[offset + i]);
> if (ret < 0)
> return ret;
> uaddr += PAGE_SIZE;
> }
>
> return 0;
> }
>
> /*
> * Maps an object consisting of `num' `pages', catering for the user's
> * requested vm_pgoff
> */
> int vm_insert_range(struct vm_struct *vma, struct page *pages, size_t num)
> {
> return __vm_insert_range(vma, pages, num, vma->vm_pgoff);
> }
>
> /*
> * Maps a set of pages, always starting at page[0]
> */
> int vm_insert_range_buggy(struct vm_struct *vma, struct page *pages, size_t num)
> {
> return __vm_insert_range(vma, pages, num, 0);
> }
>
> With this, drivers such as iommu/dma-iommu.c can be converted thusly:
>
> int iommu_dma_mmap(struct page **pages, size_t size, struct vm_area_struct *vma+)
> {
> - unsigned long uaddr = vma->vm_start;
> - unsigned int i, count = PAGE_ALIGN(size) >> PAGE_SHIFT;
> - int ret = -ENXIO;
> -
> - for (i = vma->vm_pgoff; i < count && uaddr < vma->vm_end; i++) {
> - ret = vm_insert_page(vma, uaddr, pages[i]);
> - if (ret)
> - break;
> - uaddr += PAGE_SIZE;
> - }
> - return ret;
> + return vm_insert_range(vma, pages, PAGE_ALIGN(size) >> PAGE_SHIFT);
> }
>
> and drivers such as firewire/core-iso.c:
>
> int fw_iso_buffer_map_vma(struct fw_iso_buffer *buffer,
> struct vm_area_struct *vma)
> {
> - unsigned long uaddr;
> - int i, err;
> -
> - uaddr = vma->vm_start;
> - for (i = 0; i < buffer->page_count; i++) {
> - err = vm_insert_page(vma, uaddr, buffer->pages[i]);
> - if (err)
> - return err;
> -
> - uaddr += PAGE_SIZE;
> - }
> -
> - return 0;
> + return vm_insert_range_buggy(vma, buffer->pages, buffer->page_count);
> }
>
> and this gives us something to grep for to find these buggy drivers.
>
> Now, this may not look exactly equivalent, but if you look at
> fw_device_op_mmap(), buffer->page_count is basically vma_pages(vma)
> at this point, which means this should be equivalent.
>
> We _could_ then at a later date "fix" these drivers to behave according
> to the normal vm_pgoff offsetting simply by removing the _buggy suffix
> on the function name... and if that causes regressions, it gives us an
> easy way to revert (as long as vm_insert_range_buggy() remains
> available.)
>
> In the case of firewire/core-iso.c, it currently ignores the mmap offset
> entirely, so making the above suggested change would be tantamount to
> causing it to return -ENXIO for any non-zero mmap offset.
>
> IMHO, this approach is way simpler, and easier to get it correct at
> each call site, rather than the current approach which seems to be
> error-prone.

Thanks Russell.
I will drop this patch series and rework on it as suggested.