accel/amdxdna: Fix leak when pinning ubuf pages
Lizhi Hou
maintainerPATCH 1/1 accel/amdxdna: Fix leak when pinning ubuf pages
From: Max Zhen <[email protected]>
When pin_user_pages_fast() returns fewer pages than requested, the pages that were successfully pinned are not released, leading to a leak.
Fix this by unpinning any partially pinned pages before returning failure.
--- drivers/accel/amdxdna/amdxdna_ubuf.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/accel/amdxdna/amdxdna_ubuf.c +8 -4
@@ -196,13 +196,17 @@ struct dma_buf *amdxdna_get_ubuf(struct drm_device *dev, ret =pin_user_pages_fast (va_ent[i].vaddr, npages,FOLL_WRITE |FOLL_LONGTERM , &ubuf->pages[start]); -if (ret <0 || ret != npages) { - ret = -ENOMEM ; +if (ret >=0 ) { + start += ret; +if (ret != npages) { +XDNA_ERR (xdna,"Partially pinned pages %d/%u" , ret, npages); + ret = -ENOMEM ; + goto destroy_pages; + } + }else {XDNA_ERR (xdna,"Failed to pin pages ret %d" , ret); goto destroy_pages; } - - start += ret; } exp_info.ops = &amdxdna_ubuf_dmabuf_ops;
Signature · 1 line
2.34.1
Quoted 10 lines · Lizhi Hou
From: Max Zhen <[email protected]>
When
pin_user_pages_fast()returns fewer pages than requested, the pages that were successfully pinned are not released, leading to a leak.Fix this by unpinning any partially pinned pages before returning failure.
Fixes bd72d4acda10 ("accel/amdxdna: Support user space allocated buffer")Signed-off-by Max Zhen <[email protected]>Signed-off-by Lizhi Hou <[email protected]>
Quoted 30 lines
--- drivers/accel/amdxdna/amdxdna_ubuf.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)drivers/accel/amdxdna/amdxdna_ubuf.c +8
-4@@ -196,13 +196,17 @@ struct dma_buf *amdxdna_get_ubuf(struct drm_device *dev, ret =pin_user_pages_fast (va_ent[i].vaddr, npages,FOLL_WRITE |FOLL_LONGTERM , &ubuf->pages[start]); -if (ret <0 || ret != npages) { - ret = -ENOMEM ; +if (ret >=0 ) { + start += ret; +if (ret != npages) { +XDNA_ERR (xdna,"Partially pinned pages %d/%u" , ret, npages); + ret = -ENOMEM ; + goto destroy_pages; + } + }else {XDNA_ERR (xdna,"Failed to pin pages ret %d" , ret); goto destroy_pages; } - - start += ret; } exp_info.ops = &amdxdna_ubuf_dmabuf_ops;
Applied to drm-misc-next
Quoted 46 lines · Mario Limonciello
Quoted 10 lines · Lizhi Hou
From: Max Zhen <[email protected]>
When
pin_user_pages_fast()returns fewer pages than requested, the pages that were successfully pinned are not released, leading to a leak.Fix this by unpinning any partially pinned pages before returning failure.
Fixes bd72d4acda10 ("accel/amdxdna: Support user space allocated buffer")Signed-off-by Max Zhen <[email protected]>Signed-off-by Lizhi Hou <[email protected]>Reviewed-by Mario Limonciello (AMD) <[email protected]>Quoted 31 lines
---
drivers/accel/amdxdna/amdxdna_ubuf.c| 12 ++++++++----1 file changed, 8 insertions(+), 4 deletions(-)drivers/accel/amdxdna/amdxdna_ubuf.c +8
-4@@ -196,13 +196,17 @@ struct dma_buf *amdxdna_get_ubuf(structdrm_device *dev, ret = pin_user_pages_fast(va_ent[i].vaddr, npages, FOLL_WRITE | FOLL_LONGTERM, &ubuf->pages[start]); - if (ret < 0 || ret != npages) { - ret = -ENOMEM; + if (ret >= 0) { + start += ret; + if (ret != npages) { + XDNA_ERR(xdna, "Partially pinned pages %d/%u", ret, npages); + ret = -ENOMEM; + goto destroy_pages; + } + } else { XDNA_ERR(xdna, "Failed to pin pages ret %d", ret); goto destroy_pages; } - - start += ret; }exp_info.ops =
&amdxdna_ubuf_dmabuf_ops;