From: Claude Code Review Bot <claude-review@example.com>
To: dri-devel-reviews@example.com
Subject: Claude review: accel/amdxdna: Fix user buffer VMA checking and page pinning locking
Date: Sat, 16 May 2026 08:54:22 +1000 [thread overview]
Message-ID: <review-patch1-20260515155423.743134-1-lizhi.hou@amd.com> (raw)
In-Reply-To: <20260515155423.743134-1-lizhi.hou@amd.com>
Patch Review
**Positive aspects:**
1. **TOCTOU fix is correct.** The original code in `readonly_va_entry()` acquired and released `mmap_read_lock` independently:
```c
// OLD: lock/unlock scoped only to the VMA check
mmap_read_lock(mm);
vma = find_vma(mm, va_ent->vaddr);
...
ret = vma->vm_flags & VM_WRITE ? 0 : 1;
mmap_read_unlock(mm);
```
Then `pin_user_pages_fast()` was called later without the lock. The new code correctly lifts the lock to the caller (`amdxdna_get_ubuf`) and holds it across both the readonly determination loop and the page pinning loop:
```c
mmap_read_lock(current->mm);
for (i = 0; i < num_entries; i++) {
if (readonly && readonly_va_entry(&va_ent[i]) != 1)
readonly = false;
}
for (i = 0; i < num_entries; i++) {
...
ret = pin_user_pages(va_ent[i].vaddr, npages,
(readonly ? 0 : FOLL_WRITE) | FOLL_LONGTERM,
&ubuf->pages[start]);
...
}
mmap_read_unlock(current->mm);
```
2. **`pin_user_pages_fast` → `pin_user_pages` switch is correct.** `pin_user_pages()` passes `NULL` for the `locked` parameter to `is_valid_gup_args()`, so `FOLL_UNLOCKABLE` is not set. This means the GUP internals assume mmap_read_lock is already held and won't try to manage it. The caller holds the lock — consistent.
3. **`VM_MAYWRITE` addition is important.** The old check:
```c
ret = vma->vm_flags & VM_WRITE ? 0 : 1;
```
only checked current writability. The new check:
```c
ret = vma->vm_flags & (VM_WRITE | VM_MAYWRITE) ? 0 : 1;
```
prevents the scenario where a user pins pages from a read-only VMA that has `VM_MAYWRITE`, then calls `mprotect()` to gain write access to the DMA-pinned pages. Without this, the read-only pinning could be bypassed after the fact.
4. **Error path restructuring is correct.** Changing `goto destroy_pages` to `break` inside the pinning loop, followed by `mmap_read_unlock()` and then the `if (ret < 0) goto destroy_pages` check, ensures the lock is always released before cleanup. The success case (all pages pinned) has `ret` set to the last `pin_user_pages()` return (positive), so the `ret < 0` check correctly skips the error path.
5. **`ret = 0` initialization** is necessary since `ret` is now checked after the loops, and in the edge case of `num_entries == 0`, the pinning loop body never executes.
**Minor concern:**
- **`readonly_va_entry()` error return silently treated as writable.** When `readonly_va_entry()` returns `-ENOENT` (VMA not found or doesn't cover the range), the check `!= 1` evaluates to true, so `readonly` is set to `false`. This means pages will be pinned with `FOLL_WRITE`. The `pin_user_pages()` call will ultimately fail because the VMA doesn't exist, so there's no security issue — but it would be cleaner to propagate the error early rather than relying on `pin_user_pages` to fail later. This also existed in the original code, so it's not a regression. Consider:
```c
for (i = 0; i < num_entries; i++) {
ret = readonly_va_entry(&va_ent[i]);
if (ret < 0)
break;
if (readonly && ret != 1)
readonly = false;
}
if (ret < 0) {
mmap_read_unlock(current->mm);
goto destroy_pages;
}
```
This is a suggestion for improvement, not a blocker.
**No issues found with:**
- Lock ordering (mmap_read_lock is a reader lock; no nesting conflicts with GUP internals since FOLL_UNLOCKABLE is not set)
- The `static` function `readonly_va_entry()` being called only from the locked region (confirmed: single call site at line 214)
- Consistency between `current->mm` and `ubuf->mm` (set to `current->mm` at line 164)
---
Generated by Claude Code Patch Reviewer
prev parent reply other threads:[~2026-05-15 22:54 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-15 15:54 [PATCH V1] accel/amdxdna: Fix user buffer VMA checking and page pinning locking Lizhi Hou
2026-05-15 22:54 ` Claude review: " Claude Code Review Bot
2026-05-15 22:54 ` Claude Code Review Bot [this message]
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=review-patch1-20260515155423.743134-1-lizhi.hou@amd.com \
--to=claude-review@example.com \
--cc=dri-devel-reviews@example.com \
/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