Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm: prepare lock_vma_under_rcu() for vma reuse possibility

Once we make vma cache SLAB_TYPESAFE_BY_RCU, it will be possible for a vma
to be reused and attached to another mm after lock_vma_under_rcu() locks
the vma. lock_vma_under_rcu() should ensure that vma_start_read() is
using the original mm and after locking the vma it should ensure that
vma->vm_mm has not changed from under us.

Link: https://lkml.kernel.org/r/20250213224655.1680278-17-surenb@google.com
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
Tested-by: Shivank Garg <shivankg@amd.com>
Link: https://lkml.kernel.org/r/5e19ec93-8307-47c2-bb13-3ddf7150624e@amd.com
Cc: Christian Brauner <brauner@kernel.org>
Cc: David Hildenbrand <david@redhat.com>
Cc: David Howells <dhowells@redhat.com>
Cc: Davidlohr Bueso <dave@stgolabs.net>
Cc: Hugh Dickins <hughd@google.com>
Cc: Jann Horn <jannh@google.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Klara Modin <klarasmodin@gmail.com>
Cc: Liam R. Howlett <Liam.Howlett@Oracle.com>
Cc: Lokesh Gidra <lokeshgidra@google.com>
Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Mateusz Guzik <mjguzik@gmail.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Mel Gorman <mgorman@techsingularity.net>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Minchan Kim <minchan@google.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Pasha Tatashin <pasha.tatashin@soleen.com>
Cc: "Paul E . McKenney" <paulmck@kernel.org>
Cc: Peter Xu <peterx@redhat.com>
Cc: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: Shakeel Butt <shakeel.butt@linux.dev>
Cc: Sourav Panda <souravpanda@google.com>
Cc: Wei Yang <richard.weiyang@gmail.com>
Cc: Will Deacon <will@kernel.org>
Cc: Heiko Carstens <hca@linux.ibm.com>
Cc: Stephen Rothwell <sfr@canb.auug.org.au>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Suren Baghdasaryan and committed by
Andrew Morton
e49510bf e218d9fe

+12 -7
+8 -4
include/linux/mm.h
··· 739 739 * Try to read-lock a vma. The function is allowed to occasionally yield false 740 740 * locked result to avoid performance overhead, in which case we fall back to 741 741 * using mmap_lock. The function should never yield false unlocked result. 742 + * False locked result is possible if mm_lock_seq overflows or if vma gets 743 + * reused and attached to a different mm before we lock it. 742 744 * Returns the vma on success, NULL on failure to lock and EAGAIN if vma got 743 745 * detached. 744 746 */ 745 - static inline struct vm_area_struct *vma_start_read(struct vm_area_struct *vma) 747 + static inline struct vm_area_struct *vma_start_read(struct mm_struct *mm, 748 + struct vm_area_struct *vma) 746 749 { 747 750 int oldcnt; 748 751 ··· 756 753 * we don't rely on for anything - the mm_lock_seq read against which we 757 754 * need ordering is below. 758 755 */ 759 - if (READ_ONCE(vma->vm_lock_seq) == READ_ONCE(vma->vm_mm->mm_lock_seq.sequence)) 756 + if (READ_ONCE(vma->vm_lock_seq) == READ_ONCE(mm->mm_lock_seq.sequence)) 760 757 return NULL; 761 758 762 759 /* ··· 783 780 * after it has been unlocked. 784 781 * This pairs with RELEASE semantics in vma_end_write_all(). 785 782 */ 786 - if (unlikely(vma->vm_lock_seq == raw_read_seqcount(&vma->vm_mm->mm_lock_seq))) { 783 + if (unlikely(vma->vm_lock_seq == raw_read_seqcount(&mm->mm_lock_seq))) { 787 784 vma_refcount_put(vma); 788 785 return NULL; 789 786 } ··· 917 914 #else /* CONFIG_PER_VMA_LOCK */ 918 915 919 916 static inline void vma_lock_init(struct vm_area_struct *vma, bool reset_refcnt) {} 920 - static inline struct vm_area_struct *vma_start_read(struct vm_area_struct *vma) 917 + static inline struct vm_area_struct *vma_start_read(struct mm_struct *mm, 918 + struct vm_area_struct *vma) 921 919 { return NULL; } 922 920 static inline void vma_end_read(struct vm_area_struct *vma) {} 923 921 static inline void vma_start_write(struct vm_area_struct *vma) {}
+4 -3
mm/memory.c
··· 6452 6452 if (!vma) 6453 6453 goto inval; 6454 6454 6455 - vma = vma_start_read(vma); 6455 + vma = vma_start_read(mm, vma); 6456 6456 if (IS_ERR_OR_NULL(vma)) { 6457 6457 /* Check if the VMA got isolated after we found it */ 6458 6458 if (PTR_ERR(vma) == -EAGAIN) { ··· 6471 6471 * fields are accessible for RCU readers. 6472 6472 */ 6473 6473 6474 - /* Check since vm_start/vm_end might change before we lock the VMA */ 6475 - if (unlikely(address < vma->vm_start || address >= vma->vm_end)) 6474 + /* Check if the vma we locked is the right one. */ 6475 + if (unlikely(vma->vm_mm != mm || 6476 + address < vma->vm_start || address >= vma->vm_end)) 6476 6477 goto inval_end_read; 6477 6478 6478 6479 rcu_read_unlock();