On Mon, Jun 29, 2026 at 01:23:31PM +0100, Lorenzo Stoakes wrote:
> vma_assert_write_locked() and vma_assert_attached() are useful for their
> own purposes, however VMA code absolutely does allow the modification of
> non-write locked VMAs if they are at that point detached (i.e. unreachable
> from anywhere).
> 
> It's therefore useful to be able to assert that a VMA is either
> detached (modification doesn't matter) or write locked (you're explicitly
> locked for modification).

Hmm, I was wondering why detached does not imply write_locked, and then
realized that new VMAs aren't write-locked. Could we do it by default?
Like a simple:

        vma->vm_lock_seq = __vma_raw_mm_seqnum(vma);

might do the trick. I don't see why it wouldn't work? Is there some other
case I am not considering?

> 
> Therefore introduce vma_assert_can_modify() for this purpose.
> 
> While we're here, make vma_is_attached() available generally - if
> !CONFIG_PER_VMA_LOCKS, then there's no sense in which a VMA is
> detached (vma_mark_detached() is a noop), so have this default to true in
> this case.
> 
> Signed-off-by: Lorenzo Stoakes <[email protected]>
> ---
>  include/linux/mmap_lock.h | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h
> index 04b8f61ece5d..d513286d8160 100644
> --- a/include/linux/mmap_lock.h
> +++ b/include/linux/mmap_lock.h
> @@ -506,6 +506,8 @@ static inline __must_check
>  int vma_start_write_killable(struct vm_area_struct *vma) { return 0; }
>  static inline void vma_assert_write_locked(struct vm_area_struct *vma)
>               { mmap_assert_write_locked(vma->vm_mm); }
> +static inline bool vma_is_attached(struct vm_area_struct *vma)
> +             { return true; }
>  static inline void vma_assert_attached(struct vm_area_struct *vma) {}
>  static inline void vma_assert_detached(struct vm_area_struct *vma) {}
>  static inline void vma_mark_attached(struct vm_area_struct *vma) {}
> @@ -530,6 +532,12 @@ static inline void vma_assert_stabilised(struct 
> vm_area_struct *vma)
>  
>  #endif /* CONFIG_PER_VMA_LOCK */
>  
> +static inline void vma_assert_can_modify(struct vm_area_struct *vma)
> +{
> +     if (vma_is_attached(vma))
> +             vma_assert_write_locked(vma);
> +}
> +
>  static inline void mmap_write_lock(struct mm_struct *mm)
>  {
>       __mmap_lock_trace_start_locking(mm, true);
> -- 
> 2.54.0
> 

-- 
Pedro

Reply via email to