Re: [PATCH v2 1/5] mm: Make per-VMA locks available universally

From: Alice Ryhl

Date: Tue Jun 16 2026 - 03:33:05 EST


On Wed, Jun 10, 2026 at 04:04:11PM -0700, Dave Hansen wrote:
> --- a/rust/kernel/mm.rs~unconditional-vma-locks 2026-06-10 15:57:54.051368539 -0700
> +++ b/rust/kernel/mm.rs 2026-06-10 15:57:54.078369499 -0700
> @@ -174,7 +174,6 @@ impl MmWithUser {
> /// When per-vma locks are disabled, this always returns `None`.
> #[inline]
> pub fn lock_vma_under_rcu(&self, vma_addr: usize) -> Option<VmaReadGuard<'_>> {
> - #[cfg(CONFIG_PER_VMA_LOCK)]
> {
> // SAFETY: Calling `bindings::lock_vma_under_rcu` is always okay given an mm where
> // `mm_users` is non-zero.
> @@ -188,12 +187,6 @@ impl MmWithUser {
> });
> }
> }
> -
> - // Silence warnings about unused variables.
> - #[cfg(not(CONFIG_PER_VMA_LOCK))]
> - let _ = vma_addr;
> -
> - None

This isn't quite right:

error[E0317]: `if` may be missing an `else` clause
--> rust/kernel/mm.rs:181:13
|
181 | / if !vma.is_null() {
182 | | return Some(VmaReadGuard {
... |
187 | | });
188 | | }
| |_____________^ expected `Option<VmaReadGuard<'_>>`, found `()`
|
= note: expected enum `core::option::Option<mm::VmaReadGuard<'_>>`
found unit type `()`
= note: `if` expressions without `else` evaluate to `()`
= help: consider adding an `else` block that evaluates to the expected type

This error is triggered because you deleted the return 'None' at the end
of the function.

I would like to suggest the following implementation

// SAFETY: Calling `bindings::lock_vma_under_rcu` is always okay given an mm where
// `mm_users` is non-zero.
let vma = unsafe { bindings::lock_vma_under_rcu(self.as_raw(), vma_addr) };
if vma.is_null() {
return None;
}
Some(VmaReadGuard {
// SAFETY: If `lock_vma_under_rcu` returns a non-null ptr, then it points at a valid
// vma. The vma is stable for as long as the vma read lock is held.
vma: unsafe { VmaRef::from_raw(vma) },
_nts: NotThreadSafe,
})

Thanks!
Alice