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: mark vma as detached until it's added into vma tree

Current implementation does not set detached flag when a VMA is first
allocated. This does not represent the real state of the VMA, which is
detached until it is added into mm's VMA tree. Fix this by marking new
VMAs as detached and resetting detached flag only after VMA is added into
a tree.

Introduce vma_mark_attached() to make the API more readable and to
simplify possible future cleanup when vma->vm_mm might be used to indicate
detached vma and vma_mark_attached() will need an additional mm parameter.

Link: https://lkml.kernel.org/r/20250213224655.1680278-4-surenb@google.com
Signed-off-by: Suren Baghdasaryan <surenb@google.com>
Reviewed-by: Shakeel Butt <shakeel.butt@linux.dev>
Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
Reviewed-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
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: Lokesh Gidra <lokeshgidra@google.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: 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
8ef95d8f 7b6218ae

+42 -16
+20 -7
include/linux/mm.h
··· 821 821 vma_assert_write_locked(vma); 822 822 } 823 823 824 - static inline void vma_mark_detached(struct vm_area_struct *vma, bool detached) 824 + static inline void vma_mark_attached(struct vm_area_struct *vma) 825 + { 826 + vma->detached = false; 827 + } 828 + 829 + static inline void vma_mark_detached(struct vm_area_struct *vma) 825 830 { 826 831 /* When detaching vma should be write-locked */ 827 - if (detached) 828 - vma_assert_write_locked(vma); 829 - vma->detached = detached; 832 + vma_assert_write_locked(vma); 833 + vma->detached = true; 834 + } 835 + 836 + static inline bool is_vma_detached(struct vm_area_struct *vma) 837 + { 838 + return vma->detached; 830 839 } 831 840 832 841 static inline void release_fault_lock(struct vm_fault *vmf) ··· 866 857 static inline void vma_start_write(struct vm_area_struct *vma) {} 867 858 static inline void vma_assert_write_locked(struct vm_area_struct *vma) 868 859 { mmap_assert_write_locked(vma->vm_mm); } 869 - static inline void vma_mark_detached(struct vm_area_struct *vma, 870 - bool detached) {} 860 + static inline void vma_mark_attached(struct vm_area_struct *vma) {} 861 + static inline void vma_mark_detached(struct vm_area_struct *vma) {} 871 862 872 863 static inline struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, 873 864 unsigned long address) ··· 900 891 vma->vm_mm = mm; 901 892 vma->vm_ops = &vma_dummy_vm_ops; 902 893 INIT_LIST_HEAD(&vma->anon_vma_chain); 903 - vma_mark_detached(vma, false); 894 + #ifdef CONFIG_PER_VMA_LOCK 895 + /* vma is not locked, can't use vma_mark_detached() */ 896 + vma->detached = true; 897 + #endif 904 898 vma_numab_state_init(vma); 905 899 vma_lock_init(vma); 906 900 } ··· 1098 1086 if (unlikely(mas_is_err(&vmi->mas))) 1099 1087 return -ENOMEM; 1100 1088 1089 + vma_mark_attached(vma); 1101 1090 return 0; 1102 1091 } 1103 1092
+4
kernel/fork.c
··· 465 465 data_race(memcpy(new, orig, sizeof(*new))); 466 466 vma_lock_init(new); 467 467 INIT_LIST_HEAD(&new->anon_vma_chain); 468 + #ifdef CONFIG_PER_VMA_LOCK 469 + /* vma is not locked, can't use vma_mark_detached() */ 470 + new->detached = true; 471 + #endif 468 472 vma_numab_state_init(new); 469 473 dup_anon_vma_name(orig, new); 470 474
+1 -1
mm/memory.c
··· 6374 6374 goto inval; 6375 6375 6376 6376 /* Check if the VMA got isolated after we found it */ 6377 - if (vma->detached) { 6377 + if (is_vma_detached(vma)) { 6378 6378 vma_end_read(vma); 6379 6379 count_vm_vma_lock_event(VMA_LOCK_MISS); 6380 6380 /* The area was replaced with another one */
+3 -3
mm/vma.c
··· 341 341 342 342 if (vp->remove) { 343 343 again: 344 - vma_mark_detached(vp->remove, true); 344 + vma_mark_detached(vp->remove); 345 345 if (vp->file) { 346 346 uprobe_munmap(vp->remove, vp->remove->vm_start, 347 347 vp->remove->vm_end); ··· 1238 1238 1239 1239 mas_set(mas_detach, 0); 1240 1240 mas_for_each(mas_detach, vma, ULONG_MAX) 1241 - vma_mark_detached(vma, false); 1241 + vma_mark_attached(vma); 1242 1242 1243 1243 __mt_destroy(mas_detach->tree); 1244 1244 } ··· 1313 1313 if (error) 1314 1314 goto munmap_gather_failed; 1315 1315 1316 - vma_mark_detached(next, true); 1316 + vma_mark_detached(next); 1317 1317 nrpages = vma_pages(next); 1318 1318 1319 1319 vms->nr_pages += nrpages;
+2
mm/vma.h
··· 205 205 if (unlikely(mas_is_err(&vmi->mas))) 206 206 return -ENOMEM; 207 207 208 + vma_mark_attached(vma); 208 209 return 0; 209 210 } 210 211 ··· 438 437 439 438 __mas_set_range(&vmi->mas, vma->vm_start, vma->vm_end - 1); 440 439 mas_store_prealloc(&vmi->mas, vma); 440 + vma_mark_attached(vma); 441 441 } 442 442 443 443 static inline unsigned long vma_iter_addr(struct vma_iterator *vmi)
+12 -5
tools/testing/vma/vma_internal.h
··· 471 471 } 472 472 473 473 static inline void vma_assert_write_locked(struct vm_area_struct *); 474 - static inline void vma_mark_detached(struct vm_area_struct *vma, bool detached) 474 + static inline void vma_mark_attached(struct vm_area_struct *vma) 475 + { 476 + vma->detached = false; 477 + } 478 + 479 + static inline void vma_mark_detached(struct vm_area_struct *vma) 475 480 { 476 481 /* When detaching vma should be write-locked */ 477 - if (detached) 478 - vma_assert_write_locked(vma); 479 - vma->detached = detached; 482 + vma_assert_write_locked(vma); 483 + vma->detached = true; 480 484 } 481 485 482 486 extern const struct vm_operations_struct vma_dummy_vm_ops; ··· 493 489 vma->vm_mm = mm; 494 490 vma->vm_ops = &vma_dummy_vm_ops; 495 491 INIT_LIST_HEAD(&vma->anon_vma_chain); 496 - vma_mark_detached(vma, false); 492 + /* vma is not locked, can't use vma_mark_detached() */ 493 + vma->detached = true; 497 494 vma_lock_init(vma); 498 495 } 499 496 ··· 520 515 memcpy(new, orig, sizeof(*new)); 521 516 vma_lock_init(new); 522 517 INIT_LIST_HEAD(&new->anon_vma_chain); 518 + /* vma is not locked, can't use vma_mark_detached() */ 519 + new->detached = true; 523 520 524 521 return new; 525 522 }