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: allow vma_start_read_locked/vma_start_read_locked_nested to fail

With upcoming replacement of vm_lock with vm_refcnt, we need to handle a
possibility of vma_start_read_locked/vma_start_read_locked_nested failing
due to refcount overflow. Prepare for such possibility by changing these
APIs and adjusting their users.

Link: https://lkml.kernel.org/r/20250213224655.1680278-8-surenb@google.com
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Cc: Lokesh Gidra <lokeshgidra@google.com>
Tested-by: Shivank Garg <shivankg@amd.com>
Link: https://lkml.kernel.org/r/5e19ec93-8307-47c2-bb13-3ddf7150624e@amd.com
Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
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: 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
7440adb4 2c2bd11c

+27 -9
+4 -2
include/linux/mm.h
··· 747 747 * not be used in such cases because it might fail due to mm_lock_seq overflow. 748 748 * This functionality is used to obtain vma read lock and drop the mmap read lock. 749 749 */ 750 - static inline void vma_start_read_locked_nested(struct vm_area_struct *vma, int subclass) 750 + static inline bool vma_start_read_locked_nested(struct vm_area_struct *vma, int subclass) 751 751 { 752 752 mmap_assert_locked(vma->vm_mm); 753 753 down_read_nested(&vma->vm_lock.lock, subclass); 754 + return true; 754 755 } 755 756 756 757 /* ··· 760 759 * not be used in such cases because it might fail due to mm_lock_seq overflow. 761 760 * This functionality is used to obtain vma read lock and drop the mmap read lock. 762 761 */ 763 - static inline void vma_start_read_locked(struct vm_area_struct *vma) 762 + static inline bool vma_start_read_locked(struct vm_area_struct *vma) 764 763 { 765 764 mmap_assert_locked(vma->vm_mm); 766 765 down_read(&vma->vm_lock.lock); 766 + return true; 767 767 } 768 768 769 769 static inline void vma_end_read(struct vm_area_struct *vma)
+23 -7
mm/userfaultfd.c
··· 85 85 86 86 mmap_read_lock(mm); 87 87 vma = find_vma_and_prepare_anon(mm, address); 88 - if (!IS_ERR(vma)) 89 - vma_start_read_locked(vma); 88 + if (!IS_ERR(vma)) { 89 + bool locked = vma_start_read_locked(vma); 90 + 91 + if (!locked) 92 + vma = ERR_PTR(-EAGAIN); 93 + } 90 94 91 95 mmap_read_unlock(mm); 92 96 return vma; ··· 1559 1555 1560 1556 mmap_read_lock(mm); 1561 1557 err = find_vmas_mm_locked(mm, dst_start, src_start, dst_vmap, src_vmap); 1562 - if (!err) { 1563 - vma_start_read_locked(*dst_vmap); 1564 - if (*dst_vmap != *src_vmap) 1565 - vma_start_read_locked_nested(*src_vmap, 1566 - SINGLE_DEPTH_NESTING); 1558 + if (err) 1559 + goto out; 1560 + 1561 + if (!vma_start_read_locked(*dst_vmap)) { 1562 + err = -EAGAIN; 1563 + goto out; 1567 1564 } 1565 + 1566 + /* Nothing further to do if both vmas are locked. */ 1567 + if (*dst_vmap == *src_vmap) 1568 + goto out; 1569 + 1570 + if (!vma_start_read_locked_nested(*src_vmap, SINGLE_DEPTH_NESTING)) { 1571 + /* Undo dst_vmap locking if src_vmap failed to lock */ 1572 + vma_end_read(*dst_vmap); 1573 + err = -EAGAIN; 1574 + } 1575 + out: 1568 1576 mmap_read_unlock(mm); 1569 1577 return err; 1570 1578 }