[PATCH 6/8] mm/mmap_lock: add vma_is_attached() helper

Lorenzo Stoakes posted 8 patches 1 month, 3 weeks ago
There is a newer version of this series
[PATCH 6/8] mm/mmap_lock: add vma_is_attached() helper
Posted by Lorenzo Stoakes 1 month, 3 weeks ago
This makes it easy to explicitly check for VMA detachment, which is useful
for things like asserts.

Note that we intentionally do not allow this function to be available
should CONFIG_PER_VMA_LOCK be set - this is because vma_assert_attached()
and vma_assert_detached() are no-ops if !CONFIG_PER_VMA_LOCK, so there is
no correct state for vma_is_attached() to be in if this configuration
option is not specified.

Therefore users elsewhere must invoke this function only after checking for
CONFIG_PER_VMA_LOCK.

We rework the assert functions to utilise this.

Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
---
 include/linux/mmap_lock.h | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h
index d53f72dba7fe..b50416fbba20 100644
--- a/include/linux/mmap_lock.h
+++ b/include/linux/mmap_lock.h
@@ -251,6 +251,11 @@ static inline void vma_assert_locked(struct vm_area_struct *vma)
 		      !__is_vma_write_locked(vma, &mm_lock_seq), vma);
 }

+static inline bool vma_is_attached(struct vm_area_struct *vma)
+{
+	return refcount_read(&vma->vm_refcnt);
+}
+
 /*
  * WARNING: to avoid racing with vma_mark_attached()/vma_mark_detached(), these
  * assertions should be made either under mmap_write_lock or when the object
@@ -258,12 +263,12 @@ static inline void vma_assert_locked(struct vm_area_struct *vma)
  */
 static inline void vma_assert_attached(struct vm_area_struct *vma)
 {
-	WARN_ON_ONCE(!refcount_read(&vma->vm_refcnt));
+	WARN_ON_ONCE(!vma_is_attached(vma));
 }

 static inline void vma_assert_detached(struct vm_area_struct *vma)
 {
-	WARN_ON_ONCE(refcount_read(&vma->vm_refcnt));
+	WARN_ON_ONCE(vma_is_attached(vma));
 }

 static inline void vma_mark_attached(struct vm_area_struct *vma)
--
2.52.0
Re: [PATCH 6/8] mm/mmap_lock: add vma_is_attached() helper
Posted by Suren Baghdasaryan 1 month, 1 week ago
On Wed, Dec 17, 2025 at 4:27 AM Lorenzo Stoakes
<lorenzo.stoakes@oracle.com> wrote:
>
> This makes it easy to explicitly check for VMA detachment, which is useful
> for things like asserts.
>
> Note that we intentionally do not allow this function to be available
> should CONFIG_PER_VMA_LOCK be set - this is because vma_assert_attached()
> and vma_assert_detached() are no-ops if !CONFIG_PER_VMA_LOCK, so there is
> no correct state for vma_is_attached() to be in if this configuration
> option is not specified.
>
> Therefore users elsewhere must invoke this function only after checking for
> CONFIG_PER_VMA_LOCK.
>
> We rework the assert functions to utilise this.

Thank you! This nicely documents vm_refcnt attached state. Another
step in this direction is adding:

static inline bool vma_is_read_locked(struct vm_area_struct *vma)
{
        return refcount_read(&vma->vm_refcnt) > 1;
}

and changing vma_assert_locked() to use it.
But I can do that in a separate patch, so LGTM.

>
> Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>

Reviewed-by: Suren Baghdasaryan <surenb@google.com>

> ---
>  include/linux/mmap_lock.h | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h
> index d53f72dba7fe..b50416fbba20 100644
> --- a/include/linux/mmap_lock.h
> +++ b/include/linux/mmap_lock.h
> @@ -251,6 +251,11 @@ static inline void vma_assert_locked(struct vm_area_struct *vma)
>                       !__is_vma_write_locked(vma, &mm_lock_seq), vma);
>  }
>
> +static inline bool vma_is_attached(struct vm_area_struct *vma)
> +{
> +       return refcount_read(&vma->vm_refcnt);
> +}
> +
>  /*
>   * WARNING: to avoid racing with vma_mark_attached()/vma_mark_detached(), these
>   * assertions should be made either under mmap_write_lock or when the object
> @@ -258,12 +263,12 @@ static inline void vma_assert_locked(struct vm_area_struct *vma)
>   */
>  static inline void vma_assert_attached(struct vm_area_struct *vma)
>  {
> -       WARN_ON_ONCE(!refcount_read(&vma->vm_refcnt));
> +       WARN_ON_ONCE(!vma_is_attached(vma));
>  }
>
>  static inline void vma_assert_detached(struct vm_area_struct *vma)
>  {
> -       WARN_ON_ONCE(refcount_read(&vma->vm_refcnt));
> +       WARN_ON_ONCE(vma_is_attached(vma));
>  }
>
>  static inline void vma_mark_attached(struct vm_area_struct *vma)
> --
> 2.52.0
Re: [PATCH 6/8] mm/mmap_lock: add vma_is_attached() helper
Posted by Lorenzo Stoakes 1 month ago
On Tue, Dec 30, 2025 at 11:50:34AM -0800, Suren Baghdasaryan wrote:
> On Wed, Dec 17, 2025 at 4:27 AM Lorenzo Stoakes
> <lorenzo.stoakes@oracle.com> wrote:
> >
> > This makes it easy to explicitly check for VMA detachment, which is useful
> > for things like asserts.
> >
> > Note that we intentionally do not allow this function to be available
> > should CONFIG_PER_VMA_LOCK be set - this is because vma_assert_attached()
> > and vma_assert_detached() are no-ops if !CONFIG_PER_VMA_LOCK, so there is
> > no correct state for vma_is_attached() to be in if this configuration
> > option is not specified.
> >
> > Therefore users elsewhere must invoke this function only after checking for
> > CONFIG_PER_VMA_LOCK.
> >
> > We rework the assert functions to utilise this.
>
> Thank you! This nicely documents vm_refcnt attached state. Another

You're welcome! :)

> step in this direction is adding:
>
> static inline bool vma_is_read_locked(struct vm_area_struct *vma)
> {
>         return refcount_read(&vma->vm_refcnt) > 1;
> }
>
> and changing vma_assert_locked() to use it.
> But I can do that in a separate patch, so LGTM.

Right, yeah makes sense separately I think as this change was to allow us
to use this for an assert :)

>
> >
> > Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
>
> Reviewed-by: Suren Baghdasaryan <surenb@google.com>

Thanks!

>
> > ---
> >  include/linux/mmap_lock.h | 9 +++++++--
> >  1 file changed, 7 insertions(+), 2 deletions(-)
> >
> > diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h
> > index d53f72dba7fe..b50416fbba20 100644
> > --- a/include/linux/mmap_lock.h
> > +++ b/include/linux/mmap_lock.h
> > @@ -251,6 +251,11 @@ static inline void vma_assert_locked(struct vm_area_struct *vma)
> >                       !__is_vma_write_locked(vma, &mm_lock_seq), vma);
> >  }
> >
> > +static inline bool vma_is_attached(struct vm_area_struct *vma)
> > +{
> > +       return refcount_read(&vma->vm_refcnt);
> > +}
> > +
> >  /*
> >   * WARNING: to avoid racing with vma_mark_attached()/vma_mark_detached(), these
> >   * assertions should be made either under mmap_write_lock or when the object
> > @@ -258,12 +263,12 @@ static inline void vma_assert_locked(struct vm_area_struct *vma)
> >   */
> >  static inline void vma_assert_attached(struct vm_area_struct *vma)
> >  {
> > -       WARN_ON_ONCE(!refcount_read(&vma->vm_refcnt));
> > +       WARN_ON_ONCE(!vma_is_attached(vma));
> >  }
> >
> >  static inline void vma_assert_detached(struct vm_area_struct *vma)
> >  {
> > -       WARN_ON_ONCE(refcount_read(&vma->vm_refcnt));
> > +       WARN_ON_ONCE(vma_is_attached(vma));
> >  }
> >
> >  static inline void vma_mark_attached(struct vm_area_struct *vma)
> > --
> > 2.52.0