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/vma: update vma_assert_locked() to use lockdep

We can use lockdep to avoid unnecessary work here, otherwise update the
code to logically evaluate all pertinent cases and share code with
vma_assert_write_locked().

Make it clear here that we treat the VMA being detached at this point as a
bug, this was only implicit before.

Additionally, abstract references to vma->vmlock_dep_map by introducing a
macro helper __vma_lockdep_map() which accesses this field if lockdep is
enabled.

Since lock_is_held() is specified as an extern function if lockdep is
disabled, we can simply have __vma_lockdep_map() defined as NULL in this
case, and then use IS_ENABLED(CONFIG_LOCKDEP) to avoid ugly ifdeffery.

[lorenzo.stoakes@oracle.com: add helper macro __vma_lockdep_map(), per Vlastimil]
Link: https://lkml.kernel.org/r/7c4b722e-604b-4b20-8e33-03d2f8d55407@lucifer.local
Link: https://lkml.kernel.org/r/538762f079cc4fa76ff8bf30a8a9525a09961451.1769198904.git.lorenzo.stoakes@oracle.com
Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Reviewed-by: Suren Baghdasaryan <surenb@google.com>
Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
Cc: Boqun Feng <boqun.feng@gmail.com>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Shakeel Butt <shakeel.butt@linux.dev>
Cc: Waiman Long <longman@redhat.com>
Cc: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Lorenzo Stoakes and committed by
Andrew Morton
256c1193 22f7639f

+48 -8
+48 -8
include/linux/mmap_lock.h
··· 78 78 79 79 #ifdef CONFIG_PER_VMA_LOCK 80 80 81 + #ifdef CONFIG_LOCKDEP 82 + #define __vma_lockdep_map(vma) (&vma->vmlock_dep_map) 83 + #else 84 + #define __vma_lockdep_map(vma) NULL 85 + #endif 86 + 81 87 /* 82 88 * VMA locks do not behave like most ordinary locks found in the kernel, so we 83 89 * cannot quite have full lockdep tracking in the way we would ideally prefer. ··· 104 98 * so we utilise lockdep to do so. 105 99 */ 106 100 #define __vma_lockdep_acquire_read(vma) \ 107 - lock_acquire_shared(&vma->vmlock_dep_map, 0, 1, NULL, _RET_IP_) 101 + lock_acquire_shared(__vma_lockdep_map(vma), 0, 1, NULL, _RET_IP_) 108 102 #define __vma_lockdep_release_read(vma) \ 109 - lock_release(&vma->vmlock_dep_map, _RET_IP_) 103 + lock_release(__vma_lockdep_map(vma), _RET_IP_) 110 104 #define __vma_lockdep_acquire_exclusive(vma) \ 111 - lock_acquire_exclusive(&vma->vmlock_dep_map, 0, 0, NULL, _RET_IP_) 105 + lock_acquire_exclusive(__vma_lockdep_map(vma), 0, 0, NULL, _RET_IP_) 112 106 #define __vma_lockdep_release_exclusive(vma) \ 113 - lock_release(&vma->vmlock_dep_map, _RET_IP_) 107 + lock_release(__vma_lockdep_map(vma), _RET_IP_) 114 108 /* Only meaningful if CONFIG_LOCK_STAT is defined. */ 115 109 #define __vma_lockdep_stat_mark_acquired(vma) \ 116 - lock_acquired(&vma->vmlock_dep_map, _RET_IP_) 110 + lock_acquired(__vma_lockdep_map(vma), _RET_IP_) 117 111 118 112 static inline void mm_lock_seqcount_init(struct mm_struct *mm) 119 113 { ··· 152 146 #ifdef CONFIG_DEBUG_LOCK_ALLOC 153 147 static struct lock_class_key lockdep_key; 154 148 155 - lockdep_init_map(&vma->vmlock_dep_map, "vm_lock", &lockdep_key, 0); 149 + lockdep_init_map(__vma_lockdep_map(vma), "vm_lock", &lockdep_key, 0); 156 150 #endif 157 151 if (reset_refcnt) 158 152 refcount_set(&vma->vm_refcnt, 0); ··· 325 319 return __vma_start_write(vma, TASK_KILLABLE); 326 320 } 327 321 322 + /** 323 + * vma_assert_write_locked() - assert that @vma holds a VMA write lock. 324 + * @vma: The VMA to assert. 325 + */ 328 326 static inline void vma_assert_write_locked(struct vm_area_struct *vma) 329 327 { 330 328 VM_WARN_ON_ONCE_VMA(!__is_vma_write_locked(vma), vma); 331 329 } 332 330 331 + /** 332 + * vma_assert_locked() - assert that @vma holds either a VMA read or a VMA write 333 + * lock and is not detached. 334 + * @vma: The VMA to assert. 335 + */ 333 336 static inline void vma_assert_locked(struct vm_area_struct *vma) 334 337 { 338 + unsigned int refcnt; 339 + 340 + if (IS_ENABLED(CONFIG_LOCKDEP)) { 341 + if (!lock_is_held(__vma_lockdep_map(vma))) 342 + vma_assert_write_locked(vma); 343 + return; 344 + } 345 + 335 346 /* 336 347 * See the comment describing the vm_area_struct->vm_refcnt field for 337 348 * details of possible refcnt values. 338 349 */ 339 - VM_WARN_ON_ONCE_VMA(refcount_read(&vma->vm_refcnt) <= 1 && 340 - !__is_vma_write_locked(vma), vma); 350 + refcnt = refcount_read(&vma->vm_refcnt); 351 + 352 + /* 353 + * In this case we're either read-locked, write-locked with temporary 354 + * readers, or in the midst of excluding readers, all of which means 355 + * we're locked. 356 + */ 357 + if (refcnt > 1) 358 + return; 359 + 360 + /* It is a bug for the VMA to be detached here. */ 361 + VM_WARN_ON_ONCE_VMA(!refcnt, vma); 362 + 363 + /* 364 + * OK, the VMA has a reference count of 1 which means it is either 365 + * unlocked and attached or write-locked, so assert that it is 366 + * write-locked. 367 + */ 368 + vma_assert_write_locked(vma); 341 369 } 342 370 343 371 static inline bool vma_is_attached(struct vm_area_struct *vma)