xref: /linux/include/linux/mmap_lock.h (revision ee1afc5539b6fe03569a5855b6041b13343b0993)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MMAP_LOCK_H
3 #define _LINUX_MMAP_LOCK_H
4 
5 /* Avoid a dependency loop by declaring here. */
6 extern int rcuwait_wake_up(struct rcuwait *w);
7 
8 #include <linux/lockdep.h>
9 #include <linux/mm_types.h>
10 #include <linux/mmdebug.h>
11 #include <linux/rwsem.h>
12 #include <linux/tracepoint-defs.h>
13 #include <linux/types.h>
14 #include <linux/cleanup.h>
15 #include <linux/sched/mm.h>
16 
17 #define MMAP_LOCK_INITIALIZER(name) \
18 	.mmap_lock = __RWSEM_INITIALIZER((name).mmap_lock),
19 
20 /* Tie a VMA iterator to mm write lock state at iterator initialisation. */
21 struct vma_rcu_iter {
22 	struct mm_struct *mm;
23 	struct vma_iterator *vmi;
24 	unsigned int mm_wr_seq;
25 };
26 
27 DECLARE_TRACEPOINT(mmap_lock_start_locking);
28 DECLARE_TRACEPOINT(mmap_lock_acquire_returned);
29 DECLARE_TRACEPOINT(mmap_lock_released);
30 
31 #ifdef CONFIG_TRACING
32 
33 void __mmap_lock_do_trace_start_locking(struct mm_struct *mm, bool write);
34 void __mmap_lock_do_trace_acquire_returned(struct mm_struct *mm, bool write,
35 					   bool success);
36 void __mmap_lock_do_trace_released(struct mm_struct *mm, bool write);
37 
38 static inline void __mmap_lock_trace_start_locking(struct mm_struct *mm,
39 						   bool write)
40 {
41 	if (tracepoint_enabled(mmap_lock_start_locking))
42 		__mmap_lock_do_trace_start_locking(mm, write);
43 }
44 
45 static inline void __mmap_lock_trace_acquire_returned(struct mm_struct *mm,
46 						      bool write, bool success)
47 {
48 	if (tracepoint_enabled(mmap_lock_acquire_returned))
49 		__mmap_lock_do_trace_acquire_returned(mm, write, success);
50 }
51 
52 static inline void __mmap_lock_trace_released(struct mm_struct *mm, bool write)
53 {
54 	if (tracepoint_enabled(mmap_lock_released))
55 		__mmap_lock_do_trace_released(mm, write);
56 }
57 
58 #else /* !CONFIG_TRACING */
59 
60 static inline void __mmap_lock_trace_start_locking(struct mm_struct *mm,
61 						   bool write)
62 {
63 }
64 
65 static inline void __mmap_lock_trace_acquire_returned(struct mm_struct *mm,
66 						      bool write, bool success)
67 {
68 }
69 
70 static inline void __mmap_lock_trace_released(struct mm_struct *mm, bool write)
71 {
72 }
73 
74 #endif /* CONFIG_TRACING */
75 
76 static inline void mmap_assert_locked(const struct mm_struct *mm)
77 {
78 	rwsem_assert_held(&mm->mmap_lock);
79 }
80 
81 static inline void mmap_assert_write_locked(const struct mm_struct *mm)
82 {
83 	rwsem_assert_held_write(&mm->mmap_lock);
84 }
85 
86 #ifdef CONFIG_LOCKDEP
87 #define __vma_lockdep_map(vma) (&vma->vmlock_dep_map)
88 #else
89 #define __vma_lockdep_map(vma) NULL
90 #endif
91 
92 /*
93  * VMA locks do not behave like most ordinary locks found in the kernel, so we
94  * cannot quite have full lockdep tracking in the way we would ideally prefer.
95  *
96  * Read locks act as shared locks which exclude an exclusive lock being
97  * taken. We therefore mark these accordingly on read lock acquire/release.
98  *
99  * Write locks are acquired exclusively per-VMA, but released in a shared
100  * fashion, that is upon vma_end_write_all(), we update the mmap's seqcount such
101  * that write lock is released.
102  *
103  * We therefore cannot track write locks per-VMA, nor do we try. Mitigating this
104  * is the fact that, of course, we do lockdep-track the mmap lock rwsem which
105  * must be held when taking a VMA write lock.
106  *
107  * We do, however, want to indicate that during either acquisition of a VMA
108  * write lock or detachment of a VMA that we require the lock held be exclusive,
109  * so we utilise lockdep to do so.
110  */
111 #define __vma_lockdep_acquire_read(vma) \
112 	lock_acquire_shared(__vma_lockdep_map(vma), 0, 1, NULL, _RET_IP_)
113 #define __vma_lockdep_release_read(vma) \
114 	lock_release(__vma_lockdep_map(vma), _RET_IP_)
115 #define __vma_lockdep_acquire_exclusive(vma) \
116 	lock_acquire_exclusive(__vma_lockdep_map(vma), 0, 0, NULL, _RET_IP_)
117 #define __vma_lockdep_release_exclusive(vma) \
118 	lock_release(__vma_lockdep_map(vma), _RET_IP_)
119 /* Only meaningful if CONFIG_LOCK_STAT is defined. */
120 #define __vma_lockdep_stat_mark_acquired(vma) \
121 	lock_acquired(__vma_lockdep_map(vma), _RET_IP_)
122 
123 static inline void mm_lock_seqcount_init(struct mm_struct *mm)
124 {
125 	seqcount_init(&mm->mm_lock_seq);
126 }
127 
128 static inline void mm_lock_seqcount_begin(struct mm_struct *mm)
129 {
130 	do_raw_write_seqcount_begin(&mm->mm_lock_seq);
131 }
132 
133 static inline void mm_lock_seqcount_end(struct mm_struct *mm)
134 {
135 	ASSERT_EXCLUSIVE_WRITER(mm->mm_lock_seq);
136 	do_raw_write_seqcount_end(&mm->mm_lock_seq);
137 }
138 
139 static inline bool mmap_lock_speculate_try_begin(struct mm_struct *mm, unsigned int *seq)
140 {
141 	/*
142 	 * Since mmap_lock is a sleeping lock, and waiting for it to become
143 	 * unlocked is more or less equivalent with taking it ourselves, don't
144 	 * bother with the speculative path if mmap_lock is already write-locked
145 	 * and take the slow path, which takes the lock.
146 	 */
147 	return raw_seqcount_try_begin(&mm->mm_lock_seq, *seq);
148 }
149 
150 static inline bool mmap_lock_speculate_retry(struct mm_struct *mm, unsigned int seq)
151 {
152 	return read_seqcount_retry(&mm->mm_lock_seq, seq);
153 }
154 
155 static inline void vma_lock_init(struct vm_area_struct *vma, bool reset_refcnt)
156 {
157 #ifdef CONFIG_DEBUG_LOCK_ALLOC
158 	static struct lock_class_key lockdep_key;
159 
160 	lockdep_init_map(__vma_lockdep_map(vma), "vm_lock", &lockdep_key, 0);
161 #endif
162 	if (reset_refcnt)
163 		refcount_set(&vma->vm_refcnt, 0);
164 	vma->vm_lock_seq = 0;
165 }
166 
167 /*
168  * This function determines whether the input VMA reference count describes a
169  * VMA which has excluded all VMA read locks.
170  *
171  * In the case of a detached VMA, we may incorrectly indicate that readers are
172  * excluded when one remains, because in that scenario we target a refcount of
173  * VM_REFCNT_EXCLUDE_READERS_FLAG, rather than the attached target of
174  * VM_REFCNT_EXCLUDE_READERS_FLAG + 1.
175  *
176  * However, the race window for that is very small so it is unlikely.
177  *
178  * Returns: true if readers are excluded, false otherwise.
179  */
180 static inline bool __vma_are_readers_excluded(int refcnt)
181 {
182 	/*
183 	 * See the comment describing the vm_area_struct->vm_refcnt field for
184 	 * details of possible refcnt values.
185 	 */
186 	return (refcnt & VM_REFCNT_EXCLUDE_READERS_FLAG) &&
187 		refcnt <= VM_REFCNT_EXCLUDE_READERS_FLAG + 1;
188 }
189 
190 /*
191  * Actually decrement the VMA reference count.
192  *
193  * The function returns the reference count as it was immediately after the
194  * decrement took place. If it returns zero, the VMA is now detached.
195  */
196 static inline __must_check unsigned int
197 __vma_refcount_put_return(struct vm_area_struct *vma)
198 {
199 	int oldcnt;
200 
201 	if (__refcount_dec_and_test(&vma->vm_refcnt, &oldcnt))
202 		return 0;
203 
204 	return oldcnt - 1;
205 }
206 
207 /**
208  * vma_refcount_put() - Drop reference count in VMA vm_refcnt field due to a
209  * read-lock being dropped.
210  * @vma: The VMA whose reference count we wish to decrement.
211  *
212  * If we were the last reader, wake up threads waiting to obtain an exclusive
213  * lock.
214  */
215 static inline void vma_refcount_put(struct vm_area_struct *vma)
216 {
217 	/* Use a copy of vm_mm in case vma is freed after we drop vm_refcnt. */
218 	struct mm_struct *mm = vma->vm_mm;
219 	int newcnt;
220 
221 	__vma_lockdep_release_read(vma);
222 	newcnt = __vma_refcount_put_return(vma);
223 
224 	/*
225 	 * __vma_start_exclude_readers() may be sleeping waiting for readers to
226 	 * drop their reference count, so wake it up if we were the last reader
227 	 * blocking it from being acquired.
228 	 *
229 	 * We may be raced by other readers temporarily incrementing the
230 	 * reference count, though the race window is very small, this might
231 	 * cause spurious wakeups.
232 	 */
233 	if (newcnt && __vma_are_readers_excluded(newcnt))
234 		rcuwait_wake_up(&mm->vma_writer_wait);
235 }
236 
237 /*
238  * Use only while holding mmap read lock which guarantees that vma lock is not
239  * contended (nobody can concurrently write-lock the vma). vma_start_read() need
240  * not be used in such cases, as the mmap lock already excludes VMA writers so
241  * its sequence number checks are redundant.
242  * This functionality is used to obtain vma read lock and drop the mmap read lock.
243  *
244  * VMA can't be detached while we are holding mmap lock, therefore in practice this
245  * function can fail only when there are so many readers that vm_refcnt overflows.
246  * The failure case is very unlikely and is already annotated as such internally.
247  */
248 static inline bool vma_start_read_locked_nested(struct vm_area_struct *vma, int subclass)
249 {
250 	int oldcnt;
251 
252 	mmap_assert_locked(vma->vm_mm);
253 	if (unlikely(!__refcount_inc_not_zero_limited_acquire(&vma->vm_refcnt, &oldcnt,
254 							      VM_REFCNT_LIMIT)))
255 		return false;
256 
257 	__vma_lockdep_acquire_read(vma);
258 	return true;
259 }
260 
261 /*
262  * Use only while holding mmap read lock which guarantees that vma lock is not
263  * contended (nobody can concurrently write-lock the vma). vma_start_read() need
264  * not be used in such cases, as the mmap lock already excludes VMA writers so
265  * its sequence number checks are redundant.
266  * This functionality is used to obtain vma read lock and drop the mmap read lock.
267  *
268  * VMA can't be detached while we are holding mmap lock, therefore in practice this
269  * function can fail only when there are so many readers that vm_refcnt overflows.
270  * The failure case is very unlikely and is already annotated as such internally.
271  */
272 static inline bool vma_start_read_locked(struct vm_area_struct *vma)
273 {
274 	return vma_start_read_locked_nested(vma, 0);
275 }
276 
277 struct vm_area_struct *vma_start_read_unlocked(struct mm_struct *mm,
278 					       unsigned long address);
279 
280 static inline void vma_end_read(struct vm_area_struct *vma)
281 {
282 	vma_refcount_put(vma);
283 }
284 
285 static inline unsigned int __vma_raw_mm_seqnum(const struct vm_area_struct *vma)
286 {
287 	const struct mm_struct *mm = vma->vm_mm;
288 
289 	/* We must hold an exclusive write lock for this access to be valid. */
290 	mmap_assert_write_locked(vma->vm_mm);
291 	return mm->mm_lock_seq.sequence;
292 }
293 
294 /*
295  * Determine whether a VMA is write-locked. Must be invoked ONLY if the mmap
296  * write lock is held.
297  *
298  * Returns true if write-locked, otherwise false.
299  */
300 static inline bool __is_vma_write_locked(const struct vm_area_struct *vma)
301 {
302 	/*
303 	 * current task is holding mmap_write_lock, both vma->vm_lock_seq and
304 	 * mm->mm_lock_seq can't be concurrently modified.
305 	 */
306 	return vma->vm_lock_seq == __vma_raw_mm_seqnum(vma);
307 }
308 
309 int __vma_start_write(struct vm_area_struct *vma, int state);
310 
311 /*
312  * Begin writing to a VMA.
313  * Exclude concurrent readers under the per-VMA lock until the currently
314  * write-locked mmap_lock is dropped or downgraded.
315  */
316 static inline void vma_start_write(struct vm_area_struct *vma)
317 {
318 	if (!IS_ENABLED(CONFIG_MMU))
319 		return;
320 
321 	if (__is_vma_write_locked(vma))
322 		return;
323 
324 	__vma_start_write(vma, TASK_UNINTERRUPTIBLE);
325 }
326 
327 /**
328  * vma_start_write_killable - Begin writing to a VMA.
329  * @vma: The VMA we are going to modify.
330  *
331  * Exclude concurrent readers under the per-VMA lock until the currently
332  * write-locked mmap_lock is dropped or downgraded.
333  *
334  * Context: May sleep while waiting for readers to drop the vma read lock.
335  * Caller must already hold the mmap_lock for write.
336  *
337  * Return: 0 for a successful acquisition.  -EINTR if a fatal signal was
338  * received.
339  */
340 static inline __must_check
341 int vma_start_write_killable(struct vm_area_struct *vma)
342 {
343 	if (!IS_ENABLED(CONFIG_MMU))
344 		return 0;
345 
346 	if (__is_vma_write_locked(vma))
347 		return 0;
348 
349 	return __vma_start_write(vma, TASK_KILLABLE);
350 }
351 
352 /**
353  * vma_assert_write_locked() - assert that @vma holds a VMA write lock.
354  * @vma: The VMA to assert.
355  */
356 static inline void vma_assert_write_locked(const struct vm_area_struct *vma)
357 {
358 	if (!IS_ENABLED(CONFIG_MMU)) {
359 		mmap_assert_write_locked(vma->vm_mm);
360 		return;
361 	}
362 
363 	VM_WARN_ON_ONCE_VMA(!__is_vma_write_locked(vma), vma);
364 }
365 
366 /**
367  * vma_assert_locked() - assert that @vma holds either a VMA read or a VMA write
368  * lock and is not detached.
369  * @vma: The VMA to assert.
370  */
371 static inline void vma_assert_locked(const struct vm_area_struct *vma)
372 {
373 	unsigned int refcnt;
374 
375 	if (!IS_ENABLED(CONFIG_MMU)) {
376 		mmap_assert_locked(vma->vm_mm);
377 		return;
378 	}
379 
380 	if (IS_ENABLED(CONFIG_LOCKDEP)) {
381 		if (!lock_is_held(__vma_lockdep_map(vma)))
382 			vma_assert_write_locked(vma);
383 		return;
384 	}
385 
386 	/*
387 	 * See the comment describing the vm_area_struct->vm_refcnt field for
388 	 * details of possible refcnt values.
389 	 */
390 	refcnt = refcount_read(&vma->vm_refcnt);
391 
392 	/*
393 	 * In this case we're either read-locked, write-locked with temporary
394 	 * readers, or in the midst of excluding readers, all of which means
395 	 * we're locked.
396 	 */
397 	if (refcnt > 1)
398 		return;
399 
400 	/* It is a bug for the VMA to be detached here. */
401 	VM_WARN_ON_ONCE_VMA(!refcnt, vma);
402 
403 	/*
404 	 * OK, the VMA has a reference count of 1 which means it is either
405 	 * unlocked and attached or write-locked, so assert that it is
406 	 * write-locked.
407 	 */
408 	vma_assert_write_locked(vma);
409 }
410 
411 /**
412  * vma_assert_stabilised() - assert that this VMA cannot be changed from
413  * underneath us either by having a VMA or mmap lock held.
414  * @vma: The VMA whose stability we wish to assess.
415  *
416  * If lockdep is enabled we can precisely ensure stability via either an mmap
417  * lock owned by us or a specific VMA lock.
418  *
419  * With lockdep disabled we may sometimes race with other threads acquiring the
420  * mmap read lock simultaneous with our VMA read lock.
421  */
422 static inline void vma_assert_stabilised(const struct vm_area_struct *vma)
423 {
424 	/*
425 	 * If another thread owns an mmap lock, it may go away at any time, and
426 	 * thus is no guarantee of stability.
427 	 *
428 	 * If lockdep is enabled we can accurately determine if an mmap lock is
429 	 * held and owned by us. Otherwise we must approximate.
430 	 *
431 	 * It doesn't necessarily mean we are not stabilised however, as we may
432 	 * hold a VMA read lock (not a write lock as this would require an owned
433 	 * mmap lock).
434 	 *
435 	 * If (assuming lockdep is not enabled) we were to assert a VMA read
436 	 * lock first we may also run into issues, as other threads can hold VMA
437 	 * read locks simlutaneous to us.
438 	 *
439 	 * Therefore if lockdep is not enabled we risk a false negative (i.e. no
440 	 * assert fired). If accurate checking is required, enable lockdep.
441 	 */
442 	if (IS_ENABLED(CONFIG_LOCKDEP)) {
443 		if (lockdep_is_held(&vma->vm_mm->mmap_lock))
444 			return;
445 	} else {
446 		if (rwsem_is_locked(&vma->vm_mm->mmap_lock))
447 			return;
448 	}
449 
450 	/*
451 	 * We're not stabilised by the mmap lock, so assert that we're
452 	 * stabilised by a VMA lock.
453 	 */
454 	vma_assert_locked(vma);
455 }
456 
457 static inline bool vma_is_attached(const struct vm_area_struct *vma)
458 {
459 	return refcount_read(&vma->vm_refcnt);
460 }
461 
462 /*
463  * WARNING: to avoid racing with vma_mark_attached()/vma_mark_detached(), these
464  * assertions should be made either under mmap_write_lock or when the object
465  * has been isolated under mmap_write_lock, ensuring no competing writers.
466  */
467 static inline void vma_assert_attached(struct vm_area_struct *vma)
468 {
469 	if (!IS_ENABLED(CONFIG_MMU))
470 		return;
471 
472 	WARN_ON_ONCE(!vma_is_attached(vma));
473 }
474 
475 static inline void vma_assert_detached(struct vm_area_struct *vma)
476 {
477 	WARN_ON_ONCE(vma_is_attached(vma));
478 }
479 
480 static inline void vma_mark_attached(struct vm_area_struct *vma)
481 {
482 	if (!IS_ENABLED(CONFIG_MMU))
483 		return;
484 
485 	vma_assert_write_locked(vma);
486 	vma_assert_detached(vma);
487 	refcount_set_release(&vma->vm_refcnt, 1);
488 }
489 
490 void __vma_exclude_readers_for_detach(struct vm_area_struct *vma);
491 
492 static inline void vma_mark_detached(struct vm_area_struct *vma)
493 {
494 	if (!IS_ENABLED(CONFIG_MMU))
495 		return;
496 
497 	vma_assert_write_locked(vma);
498 	vma_assert_attached(vma);
499 
500 	/*
501 	 * The VMA still being attached (refcnt > 0) - is unlikely, because the
502 	 * vma has been already write-locked and readers can increment vm_refcnt
503 	 * only temporarily before they check vm_lock_seq, realize the vma is
504 	 * locked and drop back the vm_refcnt. That is a narrow window for
505 	 * observing a raised vm_refcnt.
506 	 *
507 	 * See the comment describing the vm_area_struct->vm_refcnt field for
508 	 * details of possible refcnt values.
509 	 */
510 	if (likely(!__vma_refcount_put_return(vma)))
511 		return;
512 
513 	__vma_exclude_readers_for_detach(vma);
514 }
515 
516 struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm,
517 					  unsigned long address);
518 
519 static inline void vma_rcu_iter_init(struct vma_rcu_iter *rvmi,
520 		struct mm_struct *mm, struct vma_iterator *vmi,
521 		unsigned long addr)
522 {
523 	vma_iter_init(vmi, mm, addr);
524 	rvmi->mm = mm;
525 	rvmi->vmi = vmi;
526 	rvmi->mm_wr_seq = UINT_MAX; /* Start odd so changed by default. */
527 }
528 
529 /*
530  * Locks next vma pointed by the iterator. Confirms the locked vma has not
531  * been modified and will retry under mmap_lock protection if modification
532  * was detected. Should be called from read RCU section.
533  * Returns either a valid locked VMA, NULL if no more VMAs or -EINTR if the
534  * process was interrupted.
535  */
536 struct vm_area_struct *lock_next_vma(struct vma_rcu_iter *rvmi,
537 				     unsigned long from_addr);
538 
539 static inline void vma_assert_can_modify(struct vm_area_struct *vma)
540 {
541 	if (vma_is_attached(vma))
542 		vma_assert_write_locked(vma);
543 }
544 
545 static inline void mmap_write_lock(struct mm_struct *mm)
546 {
547 	__mmap_lock_trace_start_locking(mm, true);
548 	down_write(&mm->mmap_lock);
549 	mm_lock_seqcount_begin(mm);
550 	__mmap_lock_trace_acquire_returned(mm, true, true);
551 }
552 
553 static inline void mmap_write_lock_nested(struct mm_struct *mm, int subclass)
554 {
555 	__mmap_lock_trace_start_locking(mm, true);
556 	down_write_nested(&mm->mmap_lock, subclass);
557 	mm_lock_seqcount_begin(mm);
558 	__mmap_lock_trace_acquire_returned(mm, true, true);
559 }
560 
561 static inline int __must_check mmap_write_lock_killable(struct mm_struct *mm)
562 {
563 	int ret;
564 
565 	__mmap_lock_trace_start_locking(mm, true);
566 	ret = down_write_killable(&mm->mmap_lock);
567 	if (!ret)
568 		mm_lock_seqcount_begin(mm);
569 	__mmap_lock_trace_acquire_returned(mm, true, ret == 0);
570 	return ret;
571 }
572 
573 void __mm_reset_vma_seqcounts(struct mm_struct *mm);
574 
575 /*
576  * Drop all currently-held per-VMA locks.
577  * This is called from the mmap_lock implementation directly before releasing
578  * a write-locked mmap_lock (or downgrading it to read-locked).
579  * This should normally NOT be called manually from other places.
580  * If you want to call this manually anyway, keep in mind that this will release
581  * *all* VMA write locks, including ones from further up the stack.
582  */
583 static inline void vma_end_write_all(struct mm_struct *mm)
584 {
585 	mmap_assert_write_locked(mm);
586 	mm_lock_seqcount_end(mm);
587 
588 	/*
589 	 * VMA seqcounts are not updated on unlock, so once the mm seqcount has
590 	 * wrapped, any VMA still carrying a sequence number from before the
591 	 * wrap would satisfy __is_vma_write_locked() when that number comes
592 	 * round again, and readers would not be excluded. Reset every VMA's
593 	 * seqcount so that no stale value survives the wrap.
594 	 */
595 	if (unlikely(!mm->mm_lock_seq.sequence))
596 		__mm_reset_vma_seqcounts(mm);
597 }
598 
599 static inline void mmap_write_unlock(struct mm_struct *mm)
600 {
601 	__mmap_lock_trace_released(mm, true);
602 	vma_end_write_all(mm);
603 	up_write(&mm->mmap_lock);
604 }
605 
606 static inline void mmap_write_downgrade(struct mm_struct *mm)
607 {
608 	__mmap_lock_trace_acquire_returned(mm, false, true);
609 	vma_end_write_all(mm);
610 	downgrade_write(&mm->mmap_lock);
611 }
612 
613 static inline void mmap_read_lock(struct mm_struct *mm)
614 {
615 	__mmap_lock_trace_start_locking(mm, false);
616 	down_read(&mm->mmap_lock);
617 	__mmap_lock_trace_acquire_returned(mm, false, true);
618 }
619 
620 static inline int __must_check mmap_read_lock_killable(struct mm_struct *mm)
621 {
622 	int ret;
623 
624 	__mmap_lock_trace_start_locking(mm, false);
625 	ret = down_read_killable(&mm->mmap_lock);
626 	__mmap_lock_trace_acquire_returned(mm, false, ret == 0);
627 	return ret;
628 }
629 
630 static inline bool __must_check mmap_read_trylock(struct mm_struct *mm)
631 {
632 	bool ret;
633 
634 	__mmap_lock_trace_start_locking(mm, false);
635 	ret = down_read_trylock(&mm->mmap_lock) != 0;
636 	__mmap_lock_trace_acquire_returned(mm, false, ret);
637 	return ret;
638 }
639 
640 static inline void mmap_read_unlock(struct mm_struct *mm)
641 {
642 	__mmap_lock_trace_released(mm, false);
643 	up_read(&mm->mmap_lock);
644 }
645 
646 DEFINE_GUARD(mmap_read_lock, struct mm_struct *,
647 	     mmap_read_lock(_T), mmap_read_unlock(_T))
648 DEFINE_GUARD_COND(mmap_read_lock, _try, mmap_read_trylock(_T))
649 DEFINE_GUARD(mmap_write_lock, struct mm_struct *,
650 	     mmap_write_lock(_T), mmap_write_unlock(_T))
651 
652 static inline void mmap_read_unlock_non_owner(struct mm_struct *mm)
653 {
654 	__mmap_lock_trace_released(mm, false);
655 	up_read_non_owner(&mm->mmap_lock);
656 }
657 
658 static inline int mmap_lock_is_contended(struct mm_struct *mm)
659 {
660 	return rwsem_is_contended(&mm->mmap_lock);
661 }
662 
663 #endif /* _LINUX_MMAP_LOCK_H */
664