* [PATCH v6 1/7] udmabuf: pre-fault when first page fault
2024-09-09 9:18 [PATCH v6 0/7] udmabuf bug fix and some improvements Huan Yang
@ 2024-09-09 9:18 ` Huan Yang
2024-09-09 9:18 ` [PATCH v6 2/7] udmabuf: change folios array from kmalloc to kvmalloc Huan Yang
` (5 subsequent siblings)
6 siblings, 0 replies; 14+ messages in thread
From: Huan Yang @ 2024-09-09 9:18 UTC (permalink / raw)
To: vivek.kasireddy, Sumit Semwal, Christian König,
Gerd Hoffmann, linux-media, dri-devel, linaro-mm-sig,
linux-kernel
Cc: opensource.kernel, Huan Yang
The current udmabuf mmap only fills the physical memory to the
corresponding virtual address when the user actually accesses the
virtual address.
However, the current udmabuf has already obtained and pinned the folio
upon completion of the creation.This means that the physical memory has
already been acquired, rather than being accessed dynamically.
As a result, the page fault has lost its purpose as a demanding
page. Due to the fact that page fault requires trapping into kernel mode
and filling in when accessing the corresponding virtual address in mmap,
when creating a large size udmabuf, this represents a considerable
overhead.
This patch fill the pfn into page table, and then pre-fault each pfn
into vma, when first access.
Notice, if anything wrong , we do not return an error during this
pre-fault step. However, an error will be returned if the failure occurs
when the addr is truly accessed
Suggested-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
Signed-off-by: Huan Yang <link@vivo.com>
Acked-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
---
drivers/dma-buf/udmabuf.c | 33 +++++++++++++++++++++++++++++++--
1 file changed, 31 insertions(+), 2 deletions(-)
diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
index 047c3cd2ceff..2170d975cc76 100644
--- a/drivers/dma-buf/udmabuf.c
+++ b/drivers/dma-buf/udmabuf.c
@@ -43,7 +43,8 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
struct vm_area_struct *vma = vmf->vma;
struct udmabuf *ubuf = vma->vm_private_data;
pgoff_t pgoff = vmf->pgoff;
- unsigned long pfn;
+ unsigned long addr, pfn;
+ vm_fault_t ret;
if (pgoff >= ubuf->pagecount)
return VM_FAULT_SIGBUS;
@@ -51,7 +52,35 @@ static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
pfn = folio_pfn(ubuf->folios[pgoff]);
pfn += ubuf->offsets[pgoff] >> PAGE_SHIFT;
- return vmf_insert_pfn(vma, vmf->address, pfn);
+ ret = vmf_insert_pfn(vma, vmf->address, pfn);
+ if (ret & VM_FAULT_ERROR)
+ return ret;
+
+ /* pre fault */
+ pgoff = vma->vm_pgoff;
+ addr = vma->vm_start;
+
+ for (; addr < vma->vm_end; pgoff++, addr += PAGE_SIZE) {
+ if (addr == vmf->address)
+ continue;
+
+ if (WARN_ON(pgoff >= ubuf->pagecount))
+ break;
+
+ pfn = folio_pfn(ubuf->folios[pgoff]);
+ pfn += ubuf->offsets[pgoff] >> PAGE_SHIFT;
+
+ /**
+ * If the below vmf_insert_pfn() fails, we do not return an
+ * error here during this pre-fault step. However, an error
+ * will be returned if the failure occurs when the addr is
+ * truly accessed.
+ */
+ if (vmf_insert_pfn(vma, addr, pfn) & VM_FAULT_ERROR)
+ break;
+ }
+
+ return ret;
}
static const struct vm_operations_struct udmabuf_vm_ops = {
--
2.45.2
^ permalink raw reply [flat|nested] 14+ messages in thread* [PATCH v6 2/7] udmabuf: change folios array from kmalloc to kvmalloc
2024-09-09 9:18 [PATCH v6 0/7] udmabuf bug fix and some improvements Huan Yang
2024-09-09 9:18 ` [PATCH v6 1/7] udmabuf: pre-fault when first page fault Huan Yang
@ 2024-09-09 9:18 ` Huan Yang
2024-09-09 9:18 ` [PATCH v6 3/7] udmabuf: fix vmap_udmabuf error page set Huan Yang
` (4 subsequent siblings)
6 siblings, 0 replies; 14+ messages in thread
From: Huan Yang @ 2024-09-09 9:18 UTC (permalink / raw)
To: vivek.kasireddy, Sumit Semwal, Christian König,
Gerd Hoffmann, linux-media, dri-devel, linaro-mm-sig,
linux-kernel
Cc: opensource.kernel, Huan Yang
When PAGE_SIZE 4096, MAX_PAGE_ORDER 10, 64bit machine,
page_alloc only support 4MB.
If above this, trigger this warn and return NULL.
udmabuf can change size limit, if change it to 3072(3GB), and then alloc
3GB udmabuf, will fail create.
[ 4080.876581] ------------[ cut here ]------------
[ 4080.876843] WARNING: CPU: 3 PID: 2015 at mm/page_alloc.c:4556 __alloc_pages+0x2c8/0x350
[ 4080.878839] RIP: 0010:__alloc_pages+0x2c8/0x350
[ 4080.879470] Call Trace:
[ 4080.879473] <TASK>
[ 4080.879473] ? __alloc_pages+0x2c8/0x350
[ 4080.879475] ? __warn.cold+0x8e/0xe8
[ 4080.880647] ? __alloc_pages+0x2c8/0x350
[ 4080.880909] ? report_bug+0xff/0x140
[ 4080.881175] ? handle_bug+0x3c/0x80
[ 4080.881556] ? exc_invalid_op+0x17/0x70
[ 4080.881559] ? asm_exc_invalid_op+0x1a/0x20
[ 4080.882077] ? udmabuf_create+0x131/0x400
Because MAX_PAGE_ORDER, kmalloc can max alloc 4096 * (1 << 10), 4MB
memory, each array entry is pointer(8byte), so can save 524288 pages(2GB).
Further more, costly order(order 3) may not be guaranteed that it can be
applied for, due to fragmentation.
This patch change udmabuf array use kvmalloc_array, this can fallback
alloc into vmalloc, which can guarantee allocation for any size and does
not affect the performance of kmalloc allocations.
Signed-off-by: Huan Yang <link@vivo.com>
Acked-by: Christian König <christian.koenig@amd.com>
Acked-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
---
drivers/dma-buf/udmabuf.c | 26 +++++++++++++-------------
1 file changed, 13 insertions(+), 13 deletions(-)
diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
index 2170d975cc76..ba9dbc7caf71 100644
--- a/drivers/dma-buf/udmabuf.c
+++ b/drivers/dma-buf/udmabuf.c
@@ -109,7 +109,7 @@ static int vmap_udmabuf(struct dma_buf *buf, struct iosys_map *map)
dma_resv_assert_held(buf->resv);
- pages = kmalloc_array(ubuf->pagecount, sizeof(*pages), GFP_KERNEL);
+ pages = kvmalloc_array(ubuf->pagecount, sizeof(*pages), GFP_KERNEL);
if (!pages)
return -ENOMEM;
@@ -117,7 +117,7 @@ static int vmap_udmabuf(struct dma_buf *buf, struct iosys_map *map)
pages[pg] = &ubuf->folios[pg]->page;
vaddr = vm_map_ram(pages, ubuf->pagecount, -1);
- kfree(pages);
+ kvfree(pages);
if (!vaddr)
return -EINVAL;
@@ -225,8 +225,8 @@ static void release_udmabuf(struct dma_buf *buf)
put_sg_table(dev, ubuf->sg, DMA_BIDIRECTIONAL);
unpin_all_folios(&ubuf->unpin_list);
- kfree(ubuf->offsets);
- kfree(ubuf->folios);
+ kvfree(ubuf->offsets);
+ kvfree(ubuf->folios);
kfree(ubuf);
}
@@ -351,14 +351,14 @@ static long udmabuf_create(struct miscdevice *device,
if (!ubuf->pagecount)
goto err;
- ubuf->folios = kmalloc_array(ubuf->pagecount, sizeof(*ubuf->folios),
- GFP_KERNEL);
+ ubuf->folios = kvmalloc_array(ubuf->pagecount, sizeof(*ubuf->folios),
+ GFP_KERNEL);
if (!ubuf->folios) {
ret = -ENOMEM;
goto err;
}
- ubuf->offsets = kcalloc(ubuf->pagecount, sizeof(*ubuf->offsets),
- GFP_KERNEL);
+ ubuf->offsets = kvcalloc(ubuf->pagecount, sizeof(*ubuf->offsets),
+ GFP_KERNEL);
if (!ubuf->offsets) {
ret = -ENOMEM;
goto err;
@@ -372,7 +372,7 @@ static long udmabuf_create(struct miscdevice *device,
goto err;
pgcnt = list[i].size >> PAGE_SHIFT;
- folios = kmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL);
+ folios = kvmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL);
if (!folios) {
ret = -ENOMEM;
goto err;
@@ -382,7 +382,7 @@ static long udmabuf_create(struct miscdevice *device,
ret = memfd_pin_folios(memfd, list[i].offset, end,
folios, pgcnt, &pgoff);
if (ret <= 0) {
- kfree(folios);
+ kvfree(folios);
if (!ret)
ret = -EINVAL;
goto err;
@@ -411,7 +411,7 @@ static long udmabuf_create(struct miscdevice *device,
}
}
- kfree(folios);
+ kvfree(folios);
fput(memfd);
memfd = NULL;
}
@@ -427,8 +427,8 @@ static long udmabuf_create(struct miscdevice *device,
if (memfd)
fput(memfd);
unpin_all_folios(&ubuf->unpin_list);
- kfree(ubuf->offsets);
- kfree(ubuf->folios);
+ kvfree(ubuf->offsets);
+ kvfree(ubuf->folios);
kfree(ubuf);
return ret;
}
--
2.45.2
^ permalink raw reply [flat|nested] 14+ messages in thread* [PATCH v6 3/7] udmabuf: fix vmap_udmabuf error page set
2024-09-09 9:18 [PATCH v6 0/7] udmabuf bug fix and some improvements Huan Yang
2024-09-09 9:18 ` [PATCH v6 1/7] udmabuf: pre-fault when first page fault Huan Yang
2024-09-09 9:18 ` [PATCH v6 2/7] udmabuf: change folios array from kmalloc to kvmalloc Huan Yang
@ 2024-09-09 9:18 ` Huan Yang
2024-09-12 22:37 ` Kasireddy, Vivek
2024-09-09 9:18 ` [PATCH v6 4/7] udmabuf: udmabuf_create pin folio codestyle cleanup Huan Yang
` (3 subsequent siblings)
6 siblings, 1 reply; 14+ messages in thread
From: Huan Yang @ 2024-09-09 9:18 UTC (permalink / raw)
To: vivek.kasireddy, Sumit Semwal, Christian König,
Gerd Hoffmann, linux-media, dri-devel, linaro-mm-sig,
linux-kernel
Cc: opensource.kernel, Huan Yang
Currently vmap_udmabuf set page's array by each folio.
But, ubuf->folios is only contain's the folio's head page.
That mean we repeatedly mapped the folio head page to the vmalloc area.
Due to udmabuf can use hugetlb, if HVO enabled, tail page may not exist,
so, we can't use page array to map, instead, use pfn array.
By this, we removed page usage in udmabuf totally.
Suggested-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
Signed-off-by: Huan Yang <link@vivo.com>
Acked-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
---
drivers/dma-buf/Kconfig | 1 +
drivers/dma-buf/udmabuf.c | 22 +++++++++++++++-------
2 files changed, 16 insertions(+), 7 deletions(-)
diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig
index b46eb8a552d7..fee04fdb0822 100644
--- a/drivers/dma-buf/Kconfig
+++ b/drivers/dma-buf/Kconfig
@@ -36,6 +36,7 @@ config UDMABUF
depends on DMA_SHARED_BUFFER
depends on MEMFD_CREATE || COMPILE_TEST
depends on MMU
+ select VMAP_PFN
help
A driver to let userspace turn memfd regions into dma-bufs.
Qemu can use this to create host dmabufs for guest framebuffers.
diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
index ba9dbc7caf71..aa182a9dcdfa 100644
--- a/drivers/dma-buf/udmabuf.c
+++ b/drivers/dma-buf/udmabuf.c
@@ -103,21 +103,29 @@ static int mmap_udmabuf(struct dma_buf *buf, struct vm_area_struct *vma)
static int vmap_udmabuf(struct dma_buf *buf, struct iosys_map *map)
{
struct udmabuf *ubuf = buf->priv;
- struct page **pages;
+ unsigned long *pfns;
void *vaddr;
pgoff_t pg;
dma_resv_assert_held(buf->resv);
- pages = kvmalloc_array(ubuf->pagecount, sizeof(*pages), GFP_KERNEL);
- if (!pages)
+ /**
+ * HVO may free tail pages, so just use pfn to map each folio
+ * into vmalloc area.
+ */
+ pfns = kvmalloc_array(ubuf->pagecount, sizeof(*pfns), GFP_KERNEL);
+ if (!pfns)
return -ENOMEM;
- for (pg = 0; pg < ubuf->pagecount; pg++)
- pages[pg] = &ubuf->folios[pg]->page;
+ for (pg = 0; pg < ubuf->pagecount; pg++) {
+ unsigned long pfn = folio_pfn(ubuf->folios[pg]);
- vaddr = vm_map_ram(pages, ubuf->pagecount, -1);
- kvfree(pages);
+ pfn += ubuf->offsets[pg] >> PAGE_SHIFT;
+ pfns[pg] = pfn;
+ }
+
+ vaddr = vmap_pfn(pfns, ubuf->pagecount, PAGE_KERNEL);
+ kvfree(pfns);
if (!vaddr)
return -EINVAL;
--
2.45.2
^ permalink raw reply [flat|nested] 14+ messages in thread* RE: [PATCH v6 3/7] udmabuf: fix vmap_udmabuf error page set
2024-09-09 9:18 ` [PATCH v6 3/7] udmabuf: fix vmap_udmabuf error page set Huan Yang
@ 2024-09-12 22:37 ` Kasireddy, Vivek
2024-09-13 6:31 ` Huan Yang
0 siblings, 1 reply; 14+ messages in thread
From: Kasireddy, Vivek @ 2024-09-12 22:37 UTC (permalink / raw)
To: Huan Yang, Sumit Semwal, Christian König, Gerd Hoffmann,
linux-media, dri-devel, linaro-mm-sig, linux-kernel
Cc: opensource.kernel
Hi Huan,
> Subject: [PATCH v6 3/7] udmabuf: fix vmap_udmabuf error page set
>
> Currently vmap_udmabuf set page's array by each folio.
> But, ubuf->folios is only contain's the folio's head page.
>
> That mean we repeatedly mapped the folio head page to the vmalloc area.
>
> Due to udmabuf can use hugetlb, if HVO enabled, tail page may not exist,
> so, we can't use page array to map, instead, use pfn array.
>
> By this, we removed page usage in udmabuf totally.
>
I think this would probably need a Fixes tag:
Fixes: 5e72b2b41a21 ("udmabuf: convert udmabuf driver to use folios")
Thanks,
Vivek
> Suggested-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
> Signed-off-by: Huan Yang <link@vivo.com>
> Acked-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
> ---
> drivers/dma-buf/Kconfig | 1 +
> drivers/dma-buf/udmabuf.c | 22 +++++++++++++++-------
> 2 files changed, 16 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig
> index b46eb8a552d7..fee04fdb0822 100644
> --- a/drivers/dma-buf/Kconfig
> +++ b/drivers/dma-buf/Kconfig
> @@ -36,6 +36,7 @@ config UDMABUF
> depends on DMA_SHARED_BUFFER
> depends on MEMFD_CREATE || COMPILE_TEST
> depends on MMU
> + select VMAP_PFN
> help
> A driver to let userspace turn memfd regions into dma-bufs.
> Qemu can use this to create host dmabufs for guest framebuffers.
> diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
> index ba9dbc7caf71..aa182a9dcdfa 100644
> --- a/drivers/dma-buf/udmabuf.c
> +++ b/drivers/dma-buf/udmabuf.c
> @@ -103,21 +103,29 @@ static int mmap_udmabuf(struct dma_buf *buf,
> struct vm_area_struct *vma)
> static int vmap_udmabuf(struct dma_buf *buf, struct iosys_map *map)
> {
> struct udmabuf *ubuf = buf->priv;
> - struct page **pages;
> + unsigned long *pfns;
> void *vaddr;
> pgoff_t pg;
>
> dma_resv_assert_held(buf->resv);
>
> - pages = kvmalloc_array(ubuf->pagecount, sizeof(*pages),
> GFP_KERNEL);
> - if (!pages)
> + /**
> + * HVO may free tail pages, so just use pfn to map each folio
> + * into vmalloc area.
> + */
> + pfns = kvmalloc_array(ubuf->pagecount, sizeof(*pfns), GFP_KERNEL);
> + if (!pfns)
> return -ENOMEM;
>
> - for (pg = 0; pg < ubuf->pagecount; pg++)
> - pages[pg] = &ubuf->folios[pg]->page;
> + for (pg = 0; pg < ubuf->pagecount; pg++) {
> + unsigned long pfn = folio_pfn(ubuf->folios[pg]);
>
> - vaddr = vm_map_ram(pages, ubuf->pagecount, -1);
> - kvfree(pages);
> + pfn += ubuf->offsets[pg] >> PAGE_SHIFT;
> + pfns[pg] = pfn;
> + }
> +
> + vaddr = vmap_pfn(pfns, ubuf->pagecount, PAGE_KERNEL);
> + kvfree(pfns);
> if (!vaddr)
> return -EINVAL;
>
> --
> 2.45.2
^ permalink raw reply [flat|nested] 14+ messages in thread* Re: [PATCH v6 3/7] udmabuf: fix vmap_udmabuf error page set
2024-09-12 22:37 ` Kasireddy, Vivek
@ 2024-09-13 6:31 ` Huan Yang
0 siblings, 0 replies; 14+ messages in thread
From: Huan Yang @ 2024-09-13 6:31 UTC (permalink / raw)
To: Kasireddy, Vivek, Sumit Semwal, Christian König,
Gerd Hoffmann, linux-media, dri-devel, linaro-mm-sig,
linux-kernel
Cc: opensource.kernel
在 2024/9/13 6:37, Kasireddy, Vivek 写道:
> Hi Huan,
>
>> Subject: [PATCH v6 3/7] udmabuf: fix vmap_udmabuf error page set
>>
>> Currently vmap_udmabuf set page's array by each folio.
>> But, ubuf->folios is only contain's the folio's head page.
>>
>> That mean we repeatedly mapped the folio head page to the vmalloc area.
>>
>> Due to udmabuf can use hugetlb, if HVO enabled, tail page may not exist,
>> so, we can't use page array to map, instead, use pfn array.
>>
>> By this, we removed page usage in udmabuf totally.
>>
> I think this would probably need a Fixes tag:
> Fixes: 5e72b2b41a21 ("udmabuf: convert udmabuf driver to use folios")
OK, I'll update it
>
> Thanks,
> Vivek
>
>> Suggested-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
>> Signed-off-by: Huan Yang <link@vivo.com>
>> Acked-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
>> ---
>> drivers/dma-buf/Kconfig | 1 +
>> drivers/dma-buf/udmabuf.c | 22 +++++++++++++++-------
>> 2 files changed, 16 insertions(+), 7 deletions(-)
>>
>> diff --git a/drivers/dma-buf/Kconfig b/drivers/dma-buf/Kconfig
>> index b46eb8a552d7..fee04fdb0822 100644
>> --- a/drivers/dma-buf/Kconfig
>> +++ b/drivers/dma-buf/Kconfig
>> @@ -36,6 +36,7 @@ config UDMABUF
>> depends on DMA_SHARED_BUFFER
>> depends on MEMFD_CREATE || COMPILE_TEST
>> depends on MMU
>> + select VMAP_PFN
>> help
>> A driver to let userspace turn memfd regions into dma-bufs.
>> Qemu can use this to create host dmabufs for guest framebuffers.
>> diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
>> index ba9dbc7caf71..aa182a9dcdfa 100644
>> --- a/drivers/dma-buf/udmabuf.c
>> +++ b/drivers/dma-buf/udmabuf.c
>> @@ -103,21 +103,29 @@ static int mmap_udmabuf(struct dma_buf *buf,
>> struct vm_area_struct *vma)
>> static int vmap_udmabuf(struct dma_buf *buf, struct iosys_map *map)
>> {
>> struct udmabuf *ubuf = buf->priv;
>> - struct page **pages;
>> + unsigned long *pfns;
>> void *vaddr;
>> pgoff_t pg;
>>
>> dma_resv_assert_held(buf->resv);
>>
>> - pages = kvmalloc_array(ubuf->pagecount, sizeof(*pages),
>> GFP_KERNEL);
>> - if (!pages)
>> + /**
>> + * HVO may free tail pages, so just use pfn to map each folio
>> + * into vmalloc area.
>> + */
>> + pfns = kvmalloc_array(ubuf->pagecount, sizeof(*pfns), GFP_KERNEL);
>> + if (!pfns)
>> return -ENOMEM;
>>
>> - for (pg = 0; pg < ubuf->pagecount; pg++)
>> - pages[pg] = &ubuf->folios[pg]->page;
>> + for (pg = 0; pg < ubuf->pagecount; pg++) {
>> + unsigned long pfn = folio_pfn(ubuf->folios[pg]);
>>
>> - vaddr = vm_map_ram(pages, ubuf->pagecount, -1);
>> - kvfree(pages);
>> + pfn += ubuf->offsets[pg] >> PAGE_SHIFT;
>> + pfns[pg] = pfn;
>> + }
>> +
>> + vaddr = vmap_pfn(pfns, ubuf->pagecount, PAGE_KERNEL);
>> + kvfree(pfns);
>> if (!vaddr)
>> return -EINVAL;
>>
>> --
>> 2.45.2
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v6 4/7] udmabuf: udmabuf_create pin folio codestyle cleanup
2024-09-09 9:18 [PATCH v6 0/7] udmabuf bug fix and some improvements Huan Yang
` (2 preceding siblings ...)
2024-09-09 9:18 ` [PATCH v6 3/7] udmabuf: fix vmap_udmabuf error page set Huan Yang
@ 2024-09-09 9:18 ` Huan Yang
2024-09-12 22:43 ` Kasireddy, Vivek
2024-09-09 9:18 ` [PATCH v6 5/7] udmabuf: introduce udmabuf init and deinit helper Huan Yang
` (2 subsequent siblings)
6 siblings, 1 reply; 14+ messages in thread
From: Huan Yang @ 2024-09-09 9:18 UTC (permalink / raw)
To: vivek.kasireddy, Sumit Semwal, Christian König,
Gerd Hoffmann, linux-media, dri-devel, linaro-mm-sig,
linux-kernel
Cc: opensource.kernel, Huan Yang
This patch aims to simplify the pinning of folio during the udmabuf
creation. No functional changes.
This patch moves the memfd pin folio to udmabuf_pin_folios and modifies
the original loop condition, using the pinned folio as the external
loop condition, and sets the offset and folio during the traversal process.
By this, more readable.
Suggested-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
Signed-off-by: Huan Yang <link@vivo.com>
---
drivers/dma-buf/udmabuf.c | 134 +++++++++++++++++++++-----------------
1 file changed, 76 insertions(+), 58 deletions(-)
diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
index aa182a9dcdfa..fe1466f7d55a 100644
--- a/drivers/dma-buf/udmabuf.c
+++ b/drivers/dma-buf/udmabuf.c
@@ -328,17 +328,68 @@ static int export_udmabuf(struct udmabuf *ubuf,
return dma_buf_fd(buf, flags);
}
+static long udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd,
+ loff_t start, loff_t size)
+{
+ pgoff_t pgoff, pgcnt, upgcnt = ubuf->pagecount;
+ struct folio **folios = NULL;
+ u32 cur_folio, cur_pgcnt;
+ long nr_folios;
+ long ret = 0;
+ loff_t end;
+
+ pgcnt = size >> PAGE_SHIFT;
+ folios = kvmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL);
+ if (!folios)
+ return -ENOMEM;
+
+ end = start + (pgcnt << PAGE_SHIFT) - 1;
+ nr_folios = memfd_pin_folios(memfd, start, end, folios, pgcnt, &pgoff);
+ if (nr_folios <= 0) {
+ ret = nr_folios ? nr_folios : -EINVAL;
+ goto end;
+ }
+
+ cur_pgcnt = 0;
+ for (cur_folio = 0; cur_folio < nr_folios; ++cur_folio) {
+ pgoff_t subpgoff = pgoff;
+ size_t fsize = folio_size(folios[cur_folio]);
+
+ ret = add_to_unpin_list(&ubuf->unpin_list, folios[cur_folio]);
+ if (ret < 0)
+ goto end;
+
+ for (; subpgoff < fsize; subpgoff += PAGE_SIZE) {
+ ubuf->folios[upgcnt] = folios[cur_folio];
+ ubuf->offsets[upgcnt] = subpgoff;
+ ++upgcnt;
+
+ if (++cur_pgcnt >= pgcnt)
+ goto end;
+ }
+
+ /**
+ * In a given range, only the first subpage of the first folio
+ * has an offset, that is returned by memfd_pin_folios().
+ * The first subpages of other folios (in the range) have an
+ * offset of 0.
+ */
+ pgoff = 0;
+ }
+end:
+ ubuf->pagecount = upgcnt;
+ kvfree(folios);
+ return ret;
+}
+
static long udmabuf_create(struct miscdevice *device,
struct udmabuf_create_list *head,
struct udmabuf_create_item *list)
{
- pgoff_t pgoff, pgcnt, pglimit, pgbuf = 0;
- long nr_folios, ret = -EINVAL;
- struct file *memfd = NULL;
- struct folio **folios;
+ pgoff_t pgcnt = 0, pglimit;
struct udmabuf *ubuf;
- u32 i, j, k, flags;
- loff_t end;
+ long ret = -EINVAL;
+ u32 i, flags;
ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL);
if (!ubuf)
@@ -347,81 +398,50 @@ static long udmabuf_create(struct miscdevice *device,
INIT_LIST_HEAD(&ubuf->unpin_list);
pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT;
for (i = 0; i < head->count; i++) {
- if (!IS_ALIGNED(list[i].offset, PAGE_SIZE))
+ if (!PAGE_ALIGNED(list[i].offset))
goto err;
- if (!IS_ALIGNED(list[i].size, PAGE_SIZE))
+ if (!PAGE_ALIGNED(list[i].size))
goto err;
- ubuf->pagecount += list[i].size >> PAGE_SHIFT;
- if (ubuf->pagecount > pglimit)
+
+ pgcnt += list[i].size >> PAGE_SHIFT;
+ if (pgcnt > pglimit)
goto err;
}
- if (!ubuf->pagecount)
+ if (!pgcnt)
goto err;
- ubuf->folios = kvmalloc_array(ubuf->pagecount, sizeof(*ubuf->folios),
- GFP_KERNEL);
+ ubuf->folios = kvmalloc_array(pgcnt, sizeof(*ubuf->folios), GFP_KERNEL);
if (!ubuf->folios) {
ret = -ENOMEM;
goto err;
}
- ubuf->offsets = kvcalloc(ubuf->pagecount, sizeof(*ubuf->offsets),
- GFP_KERNEL);
+
+ ubuf->offsets = kvcalloc(pgcnt, sizeof(*ubuf->offsets), GFP_KERNEL);
if (!ubuf->offsets) {
ret = -ENOMEM;
goto err;
}
- pgbuf = 0;
for (i = 0; i < head->count; i++) {
- memfd = fget(list[i].memfd);
- ret = check_memfd_seals(memfd);
- if (ret < 0)
- goto err;
+ struct file *memfd = fget(list[i].memfd);
- pgcnt = list[i].size >> PAGE_SHIFT;
- folios = kvmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL);
- if (!folios) {
- ret = -ENOMEM;
+ if (!memfd) {
+ ret = -EBADFD;
goto err;
}
- end = list[i].offset + (pgcnt << PAGE_SHIFT) - 1;
- ret = memfd_pin_folios(memfd, list[i].offset, end,
- folios, pgcnt, &pgoff);
- if (ret <= 0) {
- kvfree(folios);
- if (!ret)
- ret = -EINVAL;
+ ret = check_memfd_seals(memfd);
+ if (ret < 0) {
+ fput(memfd);
goto err;
}
- nr_folios = ret;
- pgoff >>= PAGE_SHIFT;
- for (j = 0, k = 0; j < pgcnt; j++) {
- ubuf->folios[pgbuf] = folios[k];
- ubuf->offsets[pgbuf] = pgoff << PAGE_SHIFT;
-
- if (j == 0 || ubuf->folios[pgbuf-1] != folios[k]) {
- ret = add_to_unpin_list(&ubuf->unpin_list,
- folios[k]);
- if (ret < 0) {
- kfree(folios);
- goto err;
- }
- }
-
- pgbuf++;
- if (++pgoff == folio_nr_pages(folios[k])) {
- pgoff = 0;
- if (++k == nr_folios)
- break;
- }
- }
-
- kvfree(folios);
+ ret = udmabuf_pin_folios(ubuf, memfd, list[i].offset,
+ list[i].size);
fput(memfd);
- memfd = NULL;
+ if (ret)
+ goto err;
}
flags = head->flags & UDMABUF_FLAGS_CLOEXEC ? O_CLOEXEC : 0;
@@ -432,8 +452,6 @@ static long udmabuf_create(struct miscdevice *device,
return ret;
err:
- if (memfd)
- fput(memfd);
unpin_all_folios(&ubuf->unpin_list);
kvfree(ubuf->offsets);
kvfree(ubuf->folios);
--
2.45.2
^ permalink raw reply [flat|nested] 14+ messages in thread* RE: [PATCH v6 4/7] udmabuf: udmabuf_create pin folio codestyle cleanup
2024-09-09 9:18 ` [PATCH v6 4/7] udmabuf: udmabuf_create pin folio codestyle cleanup Huan Yang
@ 2024-09-12 22:43 ` Kasireddy, Vivek
2024-09-13 6:33 ` Huan Yang
0 siblings, 1 reply; 14+ messages in thread
From: Kasireddy, Vivek @ 2024-09-12 22:43 UTC (permalink / raw)
To: Huan Yang, Sumit Semwal, Christian König, Gerd Hoffmann,
linux-media, dri-devel, linaro-mm-sig, linux-kernel
Cc: opensource.kernel
Hi Huan,
> Subject: [PATCH v6 4/7] udmabuf: udmabuf_create pin folio codestyle
> cleanup
>
> This patch aims to simplify the pinning of folio during the udmabuf
> creation. No functional changes.
>
> This patch moves the memfd pin folio to udmabuf_pin_folios and modifies
> the original loop condition, using the pinned folio as the external
> loop condition, and sets the offset and folio during the traversal process.
>
> By this, more readable.
>
> Suggested-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
> Signed-off-by: Huan Yang <link@vivo.com>
> ---
> drivers/dma-buf/udmabuf.c | 134 +++++++++++++++++++++-----------------
> 1 file changed, 76 insertions(+), 58 deletions(-)
>
> diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
> index aa182a9dcdfa..fe1466f7d55a 100644
> --- a/drivers/dma-buf/udmabuf.c
> +++ b/drivers/dma-buf/udmabuf.c
> @@ -328,17 +328,68 @@ static int export_udmabuf(struct udmabuf *ubuf,
> return dma_buf_fd(buf, flags);
> }
>
> +static long udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd,
> + loff_t start, loff_t size)
> +{
> + pgoff_t pgoff, pgcnt, upgcnt = ubuf->pagecount;
> + struct folio **folios = NULL;
> + u32 cur_folio, cur_pgcnt;
> + long nr_folios;
> + long ret = 0;
> + loff_t end;
> +
> + pgcnt = size >> PAGE_SHIFT;
> + folios = kvmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL);
> + if (!folios)
> + return -ENOMEM;
> +
> + end = start + (pgcnt << PAGE_SHIFT) - 1;
> + nr_folios = memfd_pin_folios(memfd, start, end, folios, pgcnt,
> &pgoff);
> + if (nr_folios <= 0) {
> + ret = nr_folios ? nr_folios : -EINVAL;
> + goto end;
> + }
> +
> + cur_pgcnt = 0;
> + for (cur_folio = 0; cur_folio < nr_folios; ++cur_folio) {
> + pgoff_t subpgoff = pgoff;
> + size_t fsize = folio_size(folios[cur_folio]);
> +
> + ret = add_to_unpin_list(&ubuf->unpin_list, folios[cur_folio]);
> + if (ret < 0)
> + goto end;
> +
> + for (; subpgoff < fsize; subpgoff += PAGE_SIZE) {
> + ubuf->folios[upgcnt] = folios[cur_folio];
> + ubuf->offsets[upgcnt] = subpgoff;
> + ++upgcnt;
> +
> + if (++cur_pgcnt >= pgcnt)
> + goto end;
> + }
> +
> + /**
> + * In a given range, only the first subpage of the first folio
> + * has an offset, that is returned by memfd_pin_folios().
> + * The first subpages of other folios (in the range) have an
> + * offset of 0.
> + */
> + pgoff = 0;
> + }
> +end:
> + ubuf->pagecount = upgcnt;
> + kvfree(folios);
> + return ret;
> +}
> +
> static long udmabuf_create(struct miscdevice *device,
> struct udmabuf_create_list *head,
> struct udmabuf_create_item *list)
> {
> - pgoff_t pgoff, pgcnt, pglimit, pgbuf = 0;
> - long nr_folios, ret = -EINVAL;
> - struct file *memfd = NULL;
> - struct folio **folios;
> + pgoff_t pgcnt = 0, pglimit;
> struct udmabuf *ubuf;
> - u32 i, j, k, flags;
> - loff_t end;
> + long ret = -EINVAL;
> + u32 i, flags;
>
> ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL);
> if (!ubuf)
> @@ -347,81 +398,50 @@ static long udmabuf_create(struct miscdevice
> *device,
> INIT_LIST_HEAD(&ubuf->unpin_list);
> pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT;
> for (i = 0; i < head->count; i++) {
> - if (!IS_ALIGNED(list[i].offset, PAGE_SIZE))
> + if (!PAGE_ALIGNED(list[i].offset))
> goto err;
> - if (!IS_ALIGNED(list[i].size, PAGE_SIZE))
> + if (!PAGE_ALIGNED(list[i].size))
> goto err;
> - ubuf->pagecount += list[i].size >> PAGE_SHIFT;
> - if (ubuf->pagecount > pglimit)
> +
> + pgcnt += list[i].size >> PAGE_SHIFT;
> + if (pgcnt > pglimit)
> goto err;
> }
>
> - if (!ubuf->pagecount)
> + if (!pgcnt)
> goto err;
>
> - ubuf->folios = kvmalloc_array(ubuf->pagecount, sizeof(*ubuf-
> >folios),
> - GFP_KERNEL);
> + ubuf->folios = kvmalloc_array(pgcnt, sizeof(*ubuf->folios),
> GFP_KERNEL);
> if (!ubuf->folios) {
> ret = -ENOMEM;
> goto err;
> }
> - ubuf->offsets = kvcalloc(ubuf->pagecount, sizeof(*ubuf->offsets),
> - GFP_KERNEL);
> +
> + ubuf->offsets = kvcalloc(pgcnt, sizeof(*ubuf->offsets), GFP_KERNEL);
> if (!ubuf->offsets) {
> ret = -ENOMEM;
> goto err;
> }
>
> - pgbuf = 0;
> for (i = 0; i < head->count; i++) {
> - memfd = fget(list[i].memfd);
> - ret = check_memfd_seals(memfd);
> - if (ret < 0)
> - goto err;
> + struct file *memfd = fget(list[i].memfd);
>
> - pgcnt = list[i].size >> PAGE_SHIFT;
> - folios = kvmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL);
> - if (!folios) {
> - ret = -ENOMEM;
> + if (!memfd) {
> + ret = -EBADFD;
Nit: with the above change added, please remove the redundant if (!memfd)
check from check_memfd_seals().
Acked-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
> goto err;
> }
>
> - end = list[i].offset + (pgcnt << PAGE_SHIFT) - 1;
> - ret = memfd_pin_folios(memfd, list[i].offset, end,
> - folios, pgcnt, &pgoff);
> - if (ret <= 0) {
> - kvfree(folios);
> - if (!ret)
> - ret = -EINVAL;
> + ret = check_memfd_seals(memfd);
> + if (ret < 0) {
> + fput(memfd);
> goto err;
> }
>
> - nr_folios = ret;
> - pgoff >>= PAGE_SHIFT;
> - for (j = 0, k = 0; j < pgcnt; j++) {
> - ubuf->folios[pgbuf] = folios[k];
> - ubuf->offsets[pgbuf] = pgoff << PAGE_SHIFT;
> -
> - if (j == 0 || ubuf->folios[pgbuf-1] != folios[k]) {
> - ret = add_to_unpin_list(&ubuf->unpin_list,
> - folios[k]);
> - if (ret < 0) {
> - kfree(folios);
> - goto err;
> - }
> - }
> -
> - pgbuf++;
> - if (++pgoff == folio_nr_pages(folios[k])) {
> - pgoff = 0;
> - if (++k == nr_folios)
> - break;
> - }
> - }
> -
> - kvfree(folios);
> + ret = udmabuf_pin_folios(ubuf, memfd, list[i].offset,
> + list[i].size);
> fput(memfd);
> - memfd = NULL;
> + if (ret)
> + goto err;
> }
>
> flags = head->flags & UDMABUF_FLAGS_CLOEXEC ? O_CLOEXEC : 0;
> @@ -432,8 +452,6 @@ static long udmabuf_create(struct miscdevice
> *device,
> return ret;
>
> err:
> - if (memfd)
> - fput(memfd);
> unpin_all_folios(&ubuf->unpin_list);
> kvfree(ubuf->offsets);
> kvfree(ubuf->folios);
> --
> 2.45.2
^ permalink raw reply [flat|nested] 14+ messages in thread* Re: [PATCH v6 4/7] udmabuf: udmabuf_create pin folio codestyle cleanup
2024-09-12 22:43 ` Kasireddy, Vivek
@ 2024-09-13 6:33 ` Huan Yang
0 siblings, 0 replies; 14+ messages in thread
From: Huan Yang @ 2024-09-13 6:33 UTC (permalink / raw)
To: Kasireddy, Vivek, Sumit Semwal, Christian König,
Gerd Hoffmann, linux-media, dri-devel, linaro-mm-sig,
linux-kernel
Cc: opensource.kernel
在 2024/9/13 6:43, Kasireddy, Vivek 写道:
> Hi Huan,
>
>> Subject: [PATCH v6 4/7] udmabuf: udmabuf_create pin folio codestyle
>> cleanup
>>
>> This patch aims to simplify the pinning of folio during the udmabuf
>> creation. No functional changes.
>>
>> This patch moves the memfd pin folio to udmabuf_pin_folios and modifies
>> the original loop condition, using the pinned folio as the external
>> loop condition, and sets the offset and folio during the traversal process.
>>
>> By this, more readable.
>>
>> Suggested-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
>> Signed-off-by: Huan Yang <link@vivo.com>
>> ---
>> drivers/dma-buf/udmabuf.c | 134 +++++++++++++++++++++-----------------
>> 1 file changed, 76 insertions(+), 58 deletions(-)
>>
>> diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
>> index aa182a9dcdfa..fe1466f7d55a 100644
>> --- a/drivers/dma-buf/udmabuf.c
>> +++ b/drivers/dma-buf/udmabuf.c
>> @@ -328,17 +328,68 @@ static int export_udmabuf(struct udmabuf *ubuf,
>> return dma_buf_fd(buf, flags);
>> }
>>
>> +static long udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd,
>> + loff_t start, loff_t size)
>> +{
>> + pgoff_t pgoff, pgcnt, upgcnt = ubuf->pagecount;
>> + struct folio **folios = NULL;
>> + u32 cur_folio, cur_pgcnt;
>> + long nr_folios;
>> + long ret = 0;
>> + loff_t end;
>> +
>> + pgcnt = size >> PAGE_SHIFT;
>> + folios = kvmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL);
>> + if (!folios)
>> + return -ENOMEM;
>> +
>> + end = start + (pgcnt << PAGE_SHIFT) - 1;
>> + nr_folios = memfd_pin_folios(memfd, start, end, folios, pgcnt,
>> &pgoff);
>> + if (nr_folios <= 0) {
>> + ret = nr_folios ? nr_folios : -EINVAL;
>> + goto end;
>> + }
>> +
>> + cur_pgcnt = 0;
>> + for (cur_folio = 0; cur_folio < nr_folios; ++cur_folio) {
>> + pgoff_t subpgoff = pgoff;
>> + size_t fsize = folio_size(folios[cur_folio]);
>> +
>> + ret = add_to_unpin_list(&ubuf->unpin_list, folios[cur_folio]);
>> + if (ret < 0)
>> + goto end;
>> +
>> + for (; subpgoff < fsize; subpgoff += PAGE_SIZE) {
>> + ubuf->folios[upgcnt] = folios[cur_folio];
>> + ubuf->offsets[upgcnt] = subpgoff;
>> + ++upgcnt;
>> +
>> + if (++cur_pgcnt >= pgcnt)
>> + goto end;
>> + }
>> +
>> + /**
>> + * In a given range, only the first subpage of the first folio
>> + * has an offset, that is returned by memfd_pin_folios().
>> + * The first subpages of other folios (in the range) have an
>> + * offset of 0.
>> + */
>> + pgoff = 0;
>> + }
>> +end:
>> + ubuf->pagecount = upgcnt;
>> + kvfree(folios);
>> + return ret;
>> +}
>> +
>> static long udmabuf_create(struct miscdevice *device,
>> struct udmabuf_create_list *head,
>> struct udmabuf_create_item *list)
>> {
>> - pgoff_t pgoff, pgcnt, pglimit, pgbuf = 0;
>> - long nr_folios, ret = -EINVAL;
>> - struct file *memfd = NULL;
>> - struct folio **folios;
>> + pgoff_t pgcnt = 0, pglimit;
>> struct udmabuf *ubuf;
>> - u32 i, j, k, flags;
>> - loff_t end;
>> + long ret = -EINVAL;
>> + u32 i, flags;
>>
>> ubuf = kzalloc(sizeof(*ubuf), GFP_KERNEL);
>> if (!ubuf)
>> @@ -347,81 +398,50 @@ static long udmabuf_create(struct miscdevice
>> *device,
>> INIT_LIST_HEAD(&ubuf->unpin_list);
>> pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT;
>> for (i = 0; i < head->count; i++) {
>> - if (!IS_ALIGNED(list[i].offset, PAGE_SIZE))
>> + if (!PAGE_ALIGNED(list[i].offset))
>> goto err;
>> - if (!IS_ALIGNED(list[i].size, PAGE_SIZE))
>> + if (!PAGE_ALIGNED(list[i].size))
>> goto err;
>> - ubuf->pagecount += list[i].size >> PAGE_SHIFT;
>> - if (ubuf->pagecount > pglimit)
>> +
>> + pgcnt += list[i].size >> PAGE_SHIFT;
>> + if (pgcnt > pglimit)
>> goto err;
>> }
>>
>> - if (!ubuf->pagecount)
>> + if (!pgcnt)
>> goto err;
>>
>> - ubuf->folios = kvmalloc_array(ubuf->pagecount, sizeof(*ubuf-
>>> folios),
>> - GFP_KERNEL);
>> + ubuf->folios = kvmalloc_array(pgcnt, sizeof(*ubuf->folios),
>> GFP_KERNEL);
>> if (!ubuf->folios) {
>> ret = -ENOMEM;
>> goto err;
>> }
>> - ubuf->offsets = kvcalloc(ubuf->pagecount, sizeof(*ubuf->offsets),
>> - GFP_KERNEL);
>> +
>> + ubuf->offsets = kvcalloc(pgcnt, sizeof(*ubuf->offsets), GFP_KERNEL);
>> if (!ubuf->offsets) {
>> ret = -ENOMEM;
>> goto err;
>> }
>>
>> - pgbuf = 0;
>> for (i = 0; i < head->count; i++) {
>> - memfd = fget(list[i].memfd);
>> - ret = check_memfd_seals(memfd);
>> - if (ret < 0)
>> - goto err;
>> + struct file *memfd = fget(list[i].memfd);
>>
>> - pgcnt = list[i].size >> PAGE_SHIFT;
>> - folios = kvmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL);
>> - if (!folios) {
>> - ret = -ENOMEM;
>> + if (!memfd) {
>> + ret = -EBADFD;
> Nit: with the above change added, please remove the redundant if (!memfd)
> check from check_memfd_seals().
Yes, right, I'll do it.
Thank you.
>
> Acked-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
>
>> goto err;
>> }
>>
>> - end = list[i].offset + (pgcnt << PAGE_SHIFT) - 1;
>> - ret = memfd_pin_folios(memfd, list[i].offset, end,
>> - folios, pgcnt, &pgoff);
>> - if (ret <= 0) {
>> - kvfree(folios);
>> - if (!ret)
>> - ret = -EINVAL;
>> + ret = check_memfd_seals(memfd);
>> + if (ret < 0) {
>> + fput(memfd);
>> goto err;
>> }
>>
>> - nr_folios = ret;
>> - pgoff >>= PAGE_SHIFT;
>> - for (j = 0, k = 0; j < pgcnt; j++) {
>> - ubuf->folios[pgbuf] = folios[k];
>> - ubuf->offsets[pgbuf] = pgoff << PAGE_SHIFT;
>> -
>> - if (j == 0 || ubuf->folios[pgbuf-1] != folios[k]) {
>> - ret = add_to_unpin_list(&ubuf->unpin_list,
>> - folios[k]);
>> - if (ret < 0) {
>> - kfree(folios);
>> - goto err;
>> - }
>> - }
>> -
>> - pgbuf++;
>> - if (++pgoff == folio_nr_pages(folios[k])) {
>> - pgoff = 0;
>> - if (++k == nr_folios)
>> - break;
>> - }
>> - }
>> -
>> - kvfree(folios);
>> + ret = udmabuf_pin_folios(ubuf, memfd, list[i].offset,
>> + list[i].size);
>> fput(memfd);
>> - memfd = NULL;
>> + if (ret)
>> + goto err;
>> }
>>
>> flags = head->flags & UDMABUF_FLAGS_CLOEXEC ? O_CLOEXEC : 0;
>> @@ -432,8 +452,6 @@ static long udmabuf_create(struct miscdevice
>> *device,
>> return ret;
>>
>> err:
>> - if (memfd)
>> - fput(memfd);
>> unpin_all_folios(&ubuf->unpin_list);
>> kvfree(ubuf->offsets);
>> kvfree(ubuf->folios);
>> --
>> 2.45.2
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v6 5/7] udmabuf: introduce udmabuf init and deinit helper
2024-09-09 9:18 [PATCH v6 0/7] udmabuf bug fix and some improvements Huan Yang
` (3 preceding siblings ...)
2024-09-09 9:18 ` [PATCH v6 4/7] udmabuf: udmabuf_create pin folio codestyle cleanup Huan Yang
@ 2024-09-09 9:18 ` Huan Yang
2024-09-09 9:18 ` [PATCH v6 6/7] udmabuf: remove udmabuf_folio Huan Yang
2024-09-09 9:18 ` [PATCH v6 7/7] udmabuf: reuse folio array when pin folios Huan Yang
6 siblings, 0 replies; 14+ messages in thread
From: Huan Yang @ 2024-09-09 9:18 UTC (permalink / raw)
To: vivek.kasireddy, Sumit Semwal, Christian König,
Gerd Hoffmann, linux-media, dri-devel, linaro-mm-sig,
linux-kernel
Cc: opensource.kernel, Huan Yang
After udmabuf is allocated, its resources need to be initialized,
including various array structures. The current array structure has
already been greatly expanded.
Also, before udmabuf needs to be kfree, the occupied resources need to
be released.
This part is repetitive and maybe overlooked.
This patch give a helper function when init and deinit, by this,
reduce duplicate code.
Signed-off-by: Huan Yang <link@vivo.com>
Acked-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
---
drivers/dma-buf/udmabuf.c | 52 +++++++++++++++++++++++----------------
1 file changed, 31 insertions(+), 21 deletions(-)
diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
index fe1466f7d55a..c2eda960aaaa 100644
--- a/drivers/dma-buf/udmabuf.c
+++ b/drivers/dma-buf/udmabuf.c
@@ -224,6 +224,28 @@ static int add_to_unpin_list(struct list_head *unpin_list,
return 0;
}
+static __always_inline int init_udmabuf(struct udmabuf *ubuf, pgoff_t pgcnt)
+{
+ INIT_LIST_HEAD(&ubuf->unpin_list);
+
+ ubuf->folios = kvmalloc_array(pgcnt, sizeof(*ubuf->folios), GFP_KERNEL);
+ if (!ubuf->folios)
+ return -ENOMEM;
+
+ ubuf->offsets = kvcalloc(pgcnt, sizeof(*ubuf->offsets), GFP_KERNEL);
+ if (!ubuf->offsets)
+ return -ENOMEM;
+
+ return 0;
+}
+
+static __always_inline void deinit_udmabuf(struct udmabuf *ubuf)
+{
+ unpin_all_folios(&ubuf->unpin_list);
+ kvfree(ubuf->offsets);
+ kvfree(ubuf->folios);
+}
+
static void release_udmabuf(struct dma_buf *buf)
{
struct udmabuf *ubuf = buf->priv;
@@ -232,9 +254,7 @@ static void release_udmabuf(struct dma_buf *buf)
if (ubuf->sg)
put_sg_table(dev, ubuf->sg, DMA_BIDIRECTIONAL);
- unpin_all_folios(&ubuf->unpin_list);
- kvfree(ubuf->offsets);
- kvfree(ubuf->folios);
+ deinit_udmabuf(ubuf);
kfree(ubuf);
}
@@ -395,33 +415,24 @@ static long udmabuf_create(struct miscdevice *device,
if (!ubuf)
return -ENOMEM;
- INIT_LIST_HEAD(&ubuf->unpin_list);
pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT;
for (i = 0; i < head->count; i++) {
if (!PAGE_ALIGNED(list[i].offset))
- goto err;
+ goto err_noinit;
if (!PAGE_ALIGNED(list[i].size))
- goto err;
+ goto err_noinit;
pgcnt += list[i].size >> PAGE_SHIFT;
if (pgcnt > pglimit)
- goto err;
+ goto err_noinit;
}
if (!pgcnt)
- goto err;
+ goto err_noinit;
- ubuf->folios = kvmalloc_array(pgcnt, sizeof(*ubuf->folios), GFP_KERNEL);
- if (!ubuf->folios) {
- ret = -ENOMEM;
+ ret = init_udmabuf(ubuf, pgcnt);
+ if (ret)
goto err;
- }
-
- ubuf->offsets = kvcalloc(pgcnt, sizeof(*ubuf->offsets), GFP_KERNEL);
- if (!ubuf->offsets) {
- ret = -ENOMEM;
- goto err;
- }
for (i = 0; i < head->count; i++) {
struct file *memfd = fget(list[i].memfd);
@@ -452,9 +463,8 @@ static long udmabuf_create(struct miscdevice *device,
return ret;
err:
- unpin_all_folios(&ubuf->unpin_list);
- kvfree(ubuf->offsets);
- kvfree(ubuf->folios);
+ deinit_udmabuf(ubuf);
+err_noinit:
kfree(ubuf);
return ret;
}
--
2.45.2
^ permalink raw reply [flat|nested] 14+ messages in thread* [PATCH v6 6/7] udmabuf: remove udmabuf_folio
2024-09-09 9:18 [PATCH v6 0/7] udmabuf bug fix and some improvements Huan Yang
` (4 preceding siblings ...)
2024-09-09 9:18 ` [PATCH v6 5/7] udmabuf: introduce udmabuf init and deinit helper Huan Yang
@ 2024-09-09 9:18 ` Huan Yang
2024-09-12 22:50 ` Kasireddy, Vivek
2024-09-09 9:18 ` [PATCH v6 7/7] udmabuf: reuse folio array when pin folios Huan Yang
6 siblings, 1 reply; 14+ messages in thread
From: Huan Yang @ 2024-09-09 9:18 UTC (permalink / raw)
To: vivek.kasireddy, Sumit Semwal, Christian König,
Gerd Hoffmann, linux-media, dri-devel, linaro-mm-sig,
linux-kernel
Cc: opensource.kernel, Huan Yang
Currently, udmabuf handles folio by creating an unpin list to record
each folio obtained from the list and unpinning them when released. To
maintain this approach, many data structures have been established.
However, maintaining this type of data structure requires a significant
amount of memory and traversing the list is a substantial overhead,
which is not friendly to the CPU cache.
Considering that during creation, we arranged the folio array in the
order of pin and set the offset according to pgcnt.
We actually don't need to use unpin_list to unpin during release.
Instead, we can iterate through the folios array during release and
unpin any folio that is different from the ones previously accessed.
By this, not only saves the overhead of the udmabuf_folio data structure
but also makes array access more cache-friendly.
Signed-off-by: Huan Yang <link@vivo.com>
Acked-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
---
drivers/dma-buf/udmabuf.c | 65 +++++++++++++++++----------------------
1 file changed, 29 insertions(+), 36 deletions(-)
diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
index c2eda960aaaa..0e405a589ca2 100644
--- a/drivers/dma-buf/udmabuf.c
+++ b/drivers/dma-buf/udmabuf.c
@@ -27,15 +27,21 @@ MODULE_PARM_DESC(size_limit_mb, "Max size of a dmabuf, in megabytes. Default is
struct udmabuf {
pgoff_t pagecount;
struct folio **folios;
+
+ /**
+ * Unlike folios, pinned_folios is only used for unpin.
+ * So, nr_pinned is not the same to pagecount, the pinned_folios
+ * only set each folio which already pinned when udmabuf_create.
+ * Note that, since a folio may be pinned multiple times, each folio
+ * can be added to pinned_folios multiple times, depending on how many
+ * times the folio has been pinned when create.
+ */
+ pgoff_t nr_pinned;
+ struct folio **pinned_folios;
+
struct sg_table *sg;
struct miscdevice *device;
pgoff_t *offsets;
- struct list_head unpin_list;
-};
-
-struct udmabuf_folio {
- struct folio *folio;
- struct list_head list;
};
static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
@@ -196,38 +202,18 @@ static void unmap_udmabuf(struct dma_buf_attachment *at,
return put_sg_table(at->dev, sg, direction);
}
-static void unpin_all_folios(struct list_head *unpin_list)
+static void unpin_all_folios(struct udmabuf *ubuf)
{
- struct udmabuf_folio *ubuf_folio;
+ pgoff_t i;
- while (!list_empty(unpin_list)) {
- ubuf_folio = list_first_entry(unpin_list,
- struct udmabuf_folio, list);
- unpin_folio(ubuf_folio->folio);
+ for (i = 0; i < ubuf->nr_pinned; ++i)
+ unpin_folio(ubuf->pinned_folios[i]);
- list_del(&ubuf_folio->list);
- kfree(ubuf_folio);
- }
-}
-
-static int add_to_unpin_list(struct list_head *unpin_list,
- struct folio *folio)
-{
- struct udmabuf_folio *ubuf_folio;
-
- ubuf_folio = kzalloc(sizeof(*ubuf_folio), GFP_KERNEL);
- if (!ubuf_folio)
- return -ENOMEM;
-
- ubuf_folio->folio = folio;
- list_add_tail(&ubuf_folio->list, unpin_list);
- return 0;
+ kvfree(ubuf->pinned_folios);
}
static __always_inline int init_udmabuf(struct udmabuf *ubuf, pgoff_t pgcnt)
{
- INIT_LIST_HEAD(&ubuf->unpin_list);
-
ubuf->folios = kvmalloc_array(pgcnt, sizeof(*ubuf->folios), GFP_KERNEL);
if (!ubuf->folios)
return -ENOMEM;
@@ -236,12 +222,18 @@ static __always_inline int init_udmabuf(struct udmabuf *ubuf, pgoff_t pgcnt)
if (!ubuf->offsets)
return -ENOMEM;
+ ubuf->pinned_folios = kvmalloc_array(pgcnt,
+ sizeof(*ubuf->pinned_folios),
+ GFP_KERNEL);
+ if (!ubuf->pinned_folios)
+ return -ENOMEM;
+
return 0;
}
static __always_inline void deinit_udmabuf(struct udmabuf *ubuf)
{
- unpin_all_folios(&ubuf->unpin_list);
+ unpin_all_folios(ubuf);
kvfree(ubuf->offsets);
kvfree(ubuf->folios);
}
@@ -351,9 +343,11 @@ static int export_udmabuf(struct udmabuf *ubuf,
static long udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd,
loff_t start, loff_t size)
{
- pgoff_t pgoff, pgcnt, upgcnt = ubuf->pagecount;
+ pgoff_t nr_pinned = ubuf->nr_pinned;
+ pgoff_t upgcnt = ubuf->pagecount;
struct folio **folios = NULL;
u32 cur_folio, cur_pgcnt;
+ pgoff_t pgoff, pgcnt;
long nr_folios;
long ret = 0;
loff_t end;
@@ -375,9 +369,7 @@ static long udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd,
pgoff_t subpgoff = pgoff;
size_t fsize = folio_size(folios[cur_folio]);
- ret = add_to_unpin_list(&ubuf->unpin_list, folios[cur_folio]);
- if (ret < 0)
- goto end;
+ ubuf->pinned_folios[nr_pinned++] = folios[cur_folio];
for (; subpgoff < fsize; subpgoff += PAGE_SIZE) {
ubuf->folios[upgcnt] = folios[cur_folio];
@@ -398,6 +390,7 @@ static long udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd,
}
end:
ubuf->pagecount = upgcnt;
+ ubuf->nr_pinned = nr_pinned;
kvfree(folios);
return ret;
}
--
2.45.2
^ permalink raw reply [flat|nested] 14+ messages in thread* RE: [PATCH v6 6/7] udmabuf: remove udmabuf_folio
2024-09-09 9:18 ` [PATCH v6 6/7] udmabuf: remove udmabuf_folio Huan Yang
@ 2024-09-12 22:50 ` Kasireddy, Vivek
0 siblings, 0 replies; 14+ messages in thread
From: Kasireddy, Vivek @ 2024-09-12 22:50 UTC (permalink / raw)
To: Huan Yang, Sumit Semwal, Christian König, Gerd Hoffmann,
linux-media, dri-devel, linaro-mm-sig, linux-kernel
Cc: opensource.kernel
Hi Huan,
> Subject: [PATCH v6 6/7] udmabuf: remove udmabuf_folio
>
> Currently, udmabuf handles folio by creating an unpin list to record
> each folio obtained from the list and unpinning them when released. To
> maintain this approach, many data structures have been established.
>
> However, maintaining this type of data structure requires a significant
> amount of memory and traversing the list is a substantial overhead,
> which is not friendly to the CPU cache.
>
> Considering that during creation, we arranged the folio array in the
> order of pin and set the offset according to pgcnt.
>
> We actually don't need to use unpin_list to unpin during release.
> Instead, we can iterate through the folios array during release and
> unpin any folio that is different from the ones previously accessed.
Sorry, I didn't notice this earlier but the last few lines here do not
reflect how unpin works in this latest patch version. Please update the
commit message to describe how it currently works.
Thanks,
Vivek
>
> By this, not only saves the overhead of the udmabuf_folio data structure
> but also makes array access more cache-friendly.
>
> Signed-off-by: Huan Yang <link@vivo.com>
> Acked-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
> ---
> drivers/dma-buf/udmabuf.c | 65 +++++++++++++++++----------------------
> 1 file changed, 29 insertions(+), 36 deletions(-)
>
> diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
> index c2eda960aaaa..0e405a589ca2 100644
> --- a/drivers/dma-buf/udmabuf.c
> +++ b/drivers/dma-buf/udmabuf.c
> @@ -27,15 +27,21 @@ MODULE_PARM_DESC(size_limit_mb, "Max size of a
> dmabuf, in megabytes. Default is
> struct udmabuf {
> pgoff_t pagecount;
> struct folio **folios;
> +
> + /**
> + * Unlike folios, pinned_folios is only used for unpin.
> + * So, nr_pinned is not the same to pagecount, the pinned_folios
> + * only set each folio which already pinned when udmabuf_create.
> + * Note that, since a folio may be pinned multiple times, each folio
> + * can be added to pinned_folios multiple times, depending on how
> many
> + * times the folio has been pinned when create.
> + */
> + pgoff_t nr_pinned;
> + struct folio **pinned_folios;
> +
> struct sg_table *sg;
> struct miscdevice *device;
> pgoff_t *offsets;
> - struct list_head unpin_list;
> -};
> -
> -struct udmabuf_folio {
> - struct folio *folio;
> - struct list_head list;
> };
>
> static vm_fault_t udmabuf_vm_fault(struct vm_fault *vmf)
> @@ -196,38 +202,18 @@ static void unmap_udmabuf(struct
> dma_buf_attachment *at,
> return put_sg_table(at->dev, sg, direction);
> }
>
> -static void unpin_all_folios(struct list_head *unpin_list)
> +static void unpin_all_folios(struct udmabuf *ubuf)
> {
> - struct udmabuf_folio *ubuf_folio;
> + pgoff_t i;
>
> - while (!list_empty(unpin_list)) {
> - ubuf_folio = list_first_entry(unpin_list,
> - struct udmabuf_folio, list);
> - unpin_folio(ubuf_folio->folio);
> + for (i = 0; i < ubuf->nr_pinned; ++i)
> + unpin_folio(ubuf->pinned_folios[i]);
>
> - list_del(&ubuf_folio->list);
> - kfree(ubuf_folio);
> - }
> -}
> -
> -static int add_to_unpin_list(struct list_head *unpin_list,
> - struct folio *folio)
> -{
> - struct udmabuf_folio *ubuf_folio;
> -
> - ubuf_folio = kzalloc(sizeof(*ubuf_folio), GFP_KERNEL);
> - if (!ubuf_folio)
> - return -ENOMEM;
> -
> - ubuf_folio->folio = folio;
> - list_add_tail(&ubuf_folio->list, unpin_list);
> - return 0;
> + kvfree(ubuf->pinned_folios);
> }
>
> static __always_inline int init_udmabuf(struct udmabuf *ubuf, pgoff_t
> pgcnt)
> {
> - INIT_LIST_HEAD(&ubuf->unpin_list);
> -
> ubuf->folios = kvmalloc_array(pgcnt, sizeof(*ubuf->folios),
> GFP_KERNEL);
> if (!ubuf->folios)
> return -ENOMEM;
> @@ -236,12 +222,18 @@ static __always_inline int init_udmabuf(struct
> udmabuf *ubuf, pgoff_t pgcnt)
> if (!ubuf->offsets)
> return -ENOMEM;
>
> + ubuf->pinned_folios = kvmalloc_array(pgcnt,
> + sizeof(*ubuf->pinned_folios),
> + GFP_KERNEL);
> + if (!ubuf->pinned_folios)
> + return -ENOMEM;
> +
> return 0;
> }
>
> static __always_inline void deinit_udmabuf(struct udmabuf *ubuf)
> {
> - unpin_all_folios(&ubuf->unpin_list);
> + unpin_all_folios(ubuf);
> kvfree(ubuf->offsets);
> kvfree(ubuf->folios);
> }
> @@ -351,9 +343,11 @@ static int export_udmabuf(struct udmabuf *ubuf,
> static long udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd,
> loff_t start, loff_t size)
> {
> - pgoff_t pgoff, pgcnt, upgcnt = ubuf->pagecount;
> + pgoff_t nr_pinned = ubuf->nr_pinned;
> + pgoff_t upgcnt = ubuf->pagecount;
> struct folio **folios = NULL;
> u32 cur_folio, cur_pgcnt;
> + pgoff_t pgoff, pgcnt;
> long nr_folios;
> long ret = 0;
> loff_t end;
> @@ -375,9 +369,7 @@ static long udmabuf_pin_folios(struct udmabuf
> *ubuf, struct file *memfd,
> pgoff_t subpgoff = pgoff;
> size_t fsize = folio_size(folios[cur_folio]);
>
> - ret = add_to_unpin_list(&ubuf->unpin_list, folios[cur_folio]);
> - if (ret < 0)
> - goto end;
> + ubuf->pinned_folios[nr_pinned++] = folios[cur_folio];
>
> for (; subpgoff < fsize; subpgoff += PAGE_SIZE) {
> ubuf->folios[upgcnt] = folios[cur_folio];
> @@ -398,6 +390,7 @@ static long udmabuf_pin_folios(struct udmabuf
> *ubuf, struct file *memfd,
> }
> end:
> ubuf->pagecount = upgcnt;
> + ubuf->nr_pinned = nr_pinned;
> kvfree(folios);
> return ret;
> }
> --
> 2.45.2
^ permalink raw reply [flat|nested] 14+ messages in thread
* [PATCH v6 7/7] udmabuf: reuse folio array when pin folios
2024-09-09 9:18 [PATCH v6 0/7] udmabuf bug fix and some improvements Huan Yang
` (5 preceding siblings ...)
2024-09-09 9:18 ` [PATCH v6 6/7] udmabuf: remove udmabuf_folio Huan Yang
@ 2024-09-09 9:18 ` Huan Yang
2024-09-12 23:09 ` Kasireddy, Vivek
6 siblings, 1 reply; 14+ messages in thread
From: Huan Yang @ 2024-09-09 9:18 UTC (permalink / raw)
To: vivek.kasireddy, Sumit Semwal, Christian König,
Gerd Hoffmann, linux-media, dri-devel, linaro-mm-sig,
linux-kernel
Cc: opensource.kernel, Huan Yang
When invoke memfd_pin_folios, we need offer an array to save each folio
which we pinned.
The currently way is dynamic alloc an array, get folios, save into
udmabuf and then free.
If the size is tiny(alloc from slab) is ok due to slab can cache it.
Or, just PCP order can cover, also ok.
But if size is huge, need fallback into vmalloc, then, not well, due to
each page need alloc, and map into vmalloc area. Too heavy.
Now that we need to iter each udmabuf item, then pin it's range folios,
we can reuse the maximum size range's folios array.
Signed-off-by: Huan Yang <link@vivo.com>
Acked-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
---
drivers/dma-buf/udmabuf.c | 36 +++++++++++++++++++++---------------
1 file changed, 21 insertions(+), 15 deletions(-)
diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
index 0e405a589ca2..9fc5d22d54ae 100644
--- a/drivers/dma-buf/udmabuf.c
+++ b/drivers/dma-buf/udmabuf.c
@@ -341,28 +341,20 @@ static int export_udmabuf(struct udmabuf *ubuf,
}
static long udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd,
- loff_t start, loff_t size)
+ loff_t start, loff_t size, struct folio **folios)
{
pgoff_t nr_pinned = ubuf->nr_pinned;
pgoff_t upgcnt = ubuf->pagecount;
- struct folio **folios = NULL;
u32 cur_folio, cur_pgcnt;
pgoff_t pgoff, pgcnt;
long nr_folios;
- long ret = 0;
loff_t end;
pgcnt = size >> PAGE_SHIFT;
- folios = kvmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL);
- if (!folios)
- return -ENOMEM;
-
end = start + (pgcnt << PAGE_SHIFT) - 1;
nr_folios = memfd_pin_folios(memfd, start, end, folios, pgcnt, &pgoff);
- if (nr_folios <= 0) {
- ret = nr_folios ? nr_folios : -EINVAL;
- goto end;
- }
+ if (nr_folios <= 0)
+ return nr_folios ? nr_folios : -EINVAL;
cur_pgcnt = 0;
for (cur_folio = 0; cur_folio < nr_folios; ++cur_folio) {
@@ -391,14 +383,15 @@ static long udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd,
end:
ubuf->pagecount = upgcnt;
ubuf->nr_pinned = nr_pinned;
- kvfree(folios);
- return ret;
+ return 0;
}
static long udmabuf_create(struct miscdevice *device,
struct udmabuf_create_list *head,
struct udmabuf_create_item *list)
{
+ unsigned long max_nr_folios = 0;
+ struct folio **folios = NULL;
pgoff_t pgcnt = 0, pglimit;
struct udmabuf *ubuf;
long ret = -EINVAL;
@@ -410,14 +403,19 @@ static long udmabuf_create(struct miscdevice *device,
pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT;
for (i = 0; i < head->count; i++) {
+ pgoff_t subpgcnt;
+
if (!PAGE_ALIGNED(list[i].offset))
goto err_noinit;
if (!PAGE_ALIGNED(list[i].size))
goto err_noinit;
- pgcnt += list[i].size >> PAGE_SHIFT;
+ subpgcnt = list[i].size >> PAGE_SHIFT;
+ pgcnt += subpgcnt;
if (pgcnt > pglimit)
goto err_noinit;
+
+ max_nr_folios = max_t(unsigned long, subpgcnt, max_nr_folios);
}
if (!pgcnt)
@@ -427,6 +425,12 @@ static long udmabuf_create(struct miscdevice *device,
if (ret)
goto err;
+ folios = kvmalloc_array(max_nr_folios, sizeof(*folios), GFP_KERNEL);
+ if (!folios) {
+ ret = -ENOMEM;
+ goto err;
+ }
+
for (i = 0; i < head->count; i++) {
struct file *memfd = fget(list[i].memfd);
@@ -442,7 +446,7 @@ static long udmabuf_create(struct miscdevice *device,
}
ret = udmabuf_pin_folios(ubuf, memfd, list[i].offset,
- list[i].size);
+ list[i].size, folios);
fput(memfd);
if (ret)
goto err;
@@ -453,12 +457,14 @@ static long udmabuf_create(struct miscdevice *device,
if (ret < 0)
goto err;
+ kvfree(folios);
return ret;
err:
deinit_udmabuf(ubuf);
err_noinit:
kfree(ubuf);
+ kvfree(folios);
return ret;
}
--
2.45.2
^ permalink raw reply [flat|nested] 14+ messages in thread* RE: [PATCH v6 7/7] udmabuf: reuse folio array when pin folios
2024-09-09 9:18 ` [PATCH v6 7/7] udmabuf: reuse folio array when pin folios Huan Yang
@ 2024-09-12 23:09 ` Kasireddy, Vivek
0 siblings, 0 replies; 14+ messages in thread
From: Kasireddy, Vivek @ 2024-09-12 23:09 UTC (permalink / raw)
To: Huan Yang, Sumit Semwal, Christian König, Gerd Hoffmann,
linux-media, dri-devel, linaro-mm-sig, linux-kernel
Cc: opensource.kernel
Hi Huan,
> Subject: [PATCH v6 7/7] udmabuf: reuse folio array when pin folios
>
> When invoke memfd_pin_folios, we need offer an array to save each folio
> which we pinned.
>
> The currently way is dynamic alloc an array, get folios, save into
*current
> udmabuf and then free.
>
> If the size is tiny(alloc from slab) is ok due to slab can cache it.
> Or, just PCP order can cover, also ok.
A casual reader may not know what is PCP. Please mention what it
stands for and how it is relevant here.
>
> But if size is huge, need fallback into vmalloc, then, not well, due to
> each page need alloc, and map into vmalloc area. Too heavy.
I think it would be better if the above two lines are rewritten to more clearly
describe why or how fallback to vmalloc adversely affects performance.
Thanks,
Vivek
>
> Now that we need to iter each udmabuf item, then pin it's range folios,
> we can reuse the maximum size range's folios array.
>
> Signed-off-by: Huan Yang <link@vivo.com>
> Acked-by: Vivek Kasireddy <vivek.kasireddy@intel.com>
> ---
> drivers/dma-buf/udmabuf.c | 36 +++++++++++++++++++++---------------
> 1 file changed, 21 insertions(+), 15 deletions(-)
>
> diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c
> index 0e405a589ca2..9fc5d22d54ae 100644
> --- a/drivers/dma-buf/udmabuf.c
> +++ b/drivers/dma-buf/udmabuf.c
> @@ -341,28 +341,20 @@ static int export_udmabuf(struct udmabuf *ubuf,
> }
>
> static long udmabuf_pin_folios(struct udmabuf *ubuf, struct file *memfd,
> - loff_t start, loff_t size)
> + loff_t start, loff_t size, struct folio **folios)
> {
> pgoff_t nr_pinned = ubuf->nr_pinned;
> pgoff_t upgcnt = ubuf->pagecount;
> - struct folio **folios = NULL;
> u32 cur_folio, cur_pgcnt;
> pgoff_t pgoff, pgcnt;
> long nr_folios;
> - long ret = 0;
> loff_t end;
>
> pgcnt = size >> PAGE_SHIFT;
> - folios = kvmalloc_array(pgcnt, sizeof(*folios), GFP_KERNEL);
> - if (!folios)
> - return -ENOMEM;
> -
> end = start + (pgcnt << PAGE_SHIFT) - 1;
> nr_folios = memfd_pin_folios(memfd, start, end, folios, pgcnt,
> &pgoff);
> - if (nr_folios <= 0) {
> - ret = nr_folios ? nr_folios : -EINVAL;
> - goto end;
> - }
> + if (nr_folios <= 0)
> + return nr_folios ? nr_folios : -EINVAL;
>
> cur_pgcnt = 0;
> for (cur_folio = 0; cur_folio < nr_folios; ++cur_folio) {
> @@ -391,14 +383,15 @@ static long udmabuf_pin_folios(struct udmabuf
> *ubuf, struct file *memfd,
> end:
> ubuf->pagecount = upgcnt;
> ubuf->nr_pinned = nr_pinned;
> - kvfree(folios);
> - return ret;
> + return 0;
> }
>
> static long udmabuf_create(struct miscdevice *device,
> struct udmabuf_create_list *head,
> struct udmabuf_create_item *list)
> {
> + unsigned long max_nr_folios = 0;
> + struct folio **folios = NULL;
> pgoff_t pgcnt = 0, pglimit;
> struct udmabuf *ubuf;
> long ret = -EINVAL;
> @@ -410,14 +403,19 @@ static long udmabuf_create(struct miscdevice
> *device,
>
> pglimit = (size_limit_mb * 1024 * 1024) >> PAGE_SHIFT;
> for (i = 0; i < head->count; i++) {
> + pgoff_t subpgcnt;
> +
> if (!PAGE_ALIGNED(list[i].offset))
> goto err_noinit;
> if (!PAGE_ALIGNED(list[i].size))
> goto err_noinit;
>
> - pgcnt += list[i].size >> PAGE_SHIFT;
> + subpgcnt = list[i].size >> PAGE_SHIFT;
> + pgcnt += subpgcnt;
> if (pgcnt > pglimit)
> goto err_noinit;
> +
> + max_nr_folios = max_t(unsigned long, subpgcnt,
> max_nr_folios);
> }
>
> if (!pgcnt)
> @@ -427,6 +425,12 @@ static long udmabuf_create(struct miscdevice
> *device,
> if (ret)
> goto err;
>
> + folios = kvmalloc_array(max_nr_folios, sizeof(*folios), GFP_KERNEL);
> + if (!folios) {
> + ret = -ENOMEM;
> + goto err;
> + }
> +
> for (i = 0; i < head->count; i++) {
> struct file *memfd = fget(list[i].memfd);
>
> @@ -442,7 +446,7 @@ static long udmabuf_create(struct miscdevice
> *device,
> }
>
> ret = udmabuf_pin_folios(ubuf, memfd, list[i].offset,
> - list[i].size);
> + list[i].size, folios);
> fput(memfd);
> if (ret)
> goto err;
> @@ -453,12 +457,14 @@ static long udmabuf_create(struct miscdevice
> *device,
> if (ret < 0)
> goto err;
>
> + kvfree(folios);
> return ret;
>
> err:
> deinit_udmabuf(ubuf);
> err_noinit:
> kfree(ubuf);
> + kvfree(folios);
> return ret;
> }
>
> --
> 2.45.2
^ permalink raw reply [flat|nested] 14+ messages in thread