From: Vineet Agarwal <agarwal.vineet2006@gmail.com>
To: mamin506@gmail.com, lizhi.hou@amd.com, ogabbay@kernel.org
Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org,
Vineet Agarwal <agarwal.vineet2006@gmail.com>
Subject: [PATCH] drm/amdxdna: fix pinned_vm accounting and rlimit rollback
Date: Sat, 2 May 2026 08:47:46 +0530 [thread overview]
Message-ID: <20260502031746.621606-1-agarwal.vineet2006@gmail.com> (raw)
amdxdna_get_ubuf() incorrectly accounted mm->pinned_vm using the requested
number of pages instead of the actual number of pages successfully pinned
by pin_user_pages_fast().
Since pin_user_pages_fast() can return partial success, this led to incorrect
mm pinned page tracking and potential imbalance between pinned and unpinned
memory state.
Fix this by:
- tracking the actual number of successfully pinned pages (pinned_total)
- updating mm->pinned_vm only after successful pinning completes
- adding proper rollback on rlimit failure path to maintain symmetry
This ensures mm pinned_vm always reflects actual pinned memory usage and
keeps accounting consistent with other kernel subsystems such as RDMA,
vdpa, and iommufd.
Signed-off-by: Vineet Agarwal <agarwal.vineet2006@gmail.com>
---
drivers/accel/amdxdna/amdxdna_ubuf.c | 19 +++++++++++--------
1 file changed, 11 insertions(+), 8 deletions(-)
diff --git a/drivers/accel/amdxdna/amdxdna_ubuf.c b/drivers/accel/amdxdna/amdxdna_ubuf.c
index fb999aa25318..ad609846ea30 100644
--- a/drivers/accel/amdxdna/amdxdna_ubuf.c
+++ b/drivers/accel/amdxdna/amdxdna_ubuf.c
@@ -132,7 +132,7 @@ struct dma_buf *amdxdna_get_ubuf(struct drm_device *dev,
unsigned long lock_limit, new_pinned;
struct amdxdna_drm_va_entry *va_ent;
struct amdxdna_ubuf_priv *ubuf;
- u32 npages, start = 0;
+ u32 npages, start = 0, pinned_total = 0;
struct dma_buf *dbuf;
int i, ret;
DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
@@ -176,13 +176,6 @@ struct dma_buf *amdxdna_get_ubuf(struct drm_device *dev,
ubuf->nr_pages = exp_info.size >> PAGE_SHIFT;
lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
- new_pinned = atomic64_add_return(ubuf->nr_pages, &ubuf->mm->pinned_vm);
- if (new_pinned > lock_limit && !capable(CAP_IPC_LOCK)) {
- XDNA_DBG(xdna, "New pin %ld, limit %ld, cap %d",
- new_pinned, lock_limit, capable(CAP_IPC_LOCK));
- ret = -ENOMEM;
- goto sub_pin_cnt;
- }
ubuf->pages = kvmalloc_objs(*ubuf->pages, ubuf->nr_pages);
if (!ubuf->pages) {
@@ -203,6 +196,16 @@ struct dma_buf *amdxdna_get_ubuf(struct drm_device *dev,
}
start += ret;
+ pinned_total += ret;
+ }
+
+ new_pinned = atomic64_add_return(pinned_total,
+ &ubuf->mm->pinned_vm);
+
+ if (new_pinned > lock_limit && !capable(CAP_IPC_LOCK)) {
+ atomic64_sub(pinned_total, &ubuf->mm->pinned_vm);
+ ret = -ENOMEM;
+ goto destroy_pages;
}
exp_info.ops = &amdxdna_ubuf_dmabuf_ops;
--
2.54.0
next reply other threads:[~2026-05-02 3:18 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-02 3:17 Vineet Agarwal [this message]
2026-05-03 4:49 ` [PATCH] drm/amdxdna: fix pinned_vm accounting and error handling in user buffer pinning Vineet Agarwal
2026-05-03 5:50 ` [PATCH v3] " Vineet Agarwal
2026-05-04 16:05 ` Lizhi Hou
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260502031746.621606-1-agarwal.vineet2006@gmail.com \
--to=agarwal.vineet2006@gmail.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lizhi.hou@amd.com \
--cc=mamin506@gmail.com \
--cc=ogabbay@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox