Re: [PATCH] kernel/dma: dma_common_find_pages returns pages for requested address

From: Dima Stepanov
Date: Wed Jan 29 2025 - 03:13:31 EST


On Tue, Jan 28, 2025 at 7:53 PM Robin Murphy <robin.murphy@xxxxxxx> wrote:
>
> On 27/01/2025 5:47 pm, Dima Stepanov wrote:
> > On Mon, Jan 27, 2025 at 12:50 PM Robin Murphy <robin.murphy@xxxxxxx> wrote:
> >>
> >> No, that's a bug in the caller of dma_mmap_attrs(). As the kerneldoc
> >> says, cpu_addr/dma_addr/size must still represent the whole buffer as
> >> returned by the allocator - any offset for the mapping itself relative
> >> to the start of the buffer is expressed in vma->pgoff.
> >>
> >> Thanks,
> >> Robin.
> >
> > I see, thanks for clarification Robin. I was confused, because depending
> > on the backend it will work or not work. But i believe that in this case it is
> > undefined behavior. That is unfortunate to me, since the idea behind was:
> > - The memory allocated once because of device/firmware
> > - Different users could request a part of this memory from the kernel and
> > mmap it
> >
> > And i didn't want to expose this offset information to the user. Wanted to
> > rely on the kernel to mmap the proper part of the buffer.
>
> In principle I don't see why you shouldn't be able to create multiple
> files representing different parts of one large buffer - seems like the
> ops for said files would just need to be a bit cleverer, and remain
> aware of the whole buffer as well as the range of their own part within
> it. Then they can adjust vma->vm_pgoff accordingly before calling
> dma_mmap_*() (and possibly perform their own stricter bounds checking as
> well.)
>

This is a good point, i think you are right. Indeed in this case i could go with
the vm_pgoff approach internally without exposing any information to the user.
Thanks for figuring this out!

Thanks, Dima