1 #ifndef _LINUX_MMAP_LOCK_H
2 #define _LINUX_MMAP_LOCK_H
4 #include <linux/lockdep.h>
5 #include <linux/mm_types.h>
6 #include <linux/mmdebug.h>
7 #include <linux/rwsem.h>
8 #include <linux/tracepoint-defs.h>
9 #include <linux/types.h>
11 #define MMAP_LOCK_INITIALIZER(name) \
12 .mmap_lock = __RWSEM_INITIALIZER((name).mmap_lock),
14 DECLARE_TRACEPOINT(mmap_lock_start_locking);
15 DECLARE_TRACEPOINT(mmap_lock_acquire_returned);
16 DECLARE_TRACEPOINT(mmap_lock_released);
20 void __mmap_lock_do_trace_start_locking(struct mm_struct *mm, bool write);
21 void __mmap_lock_do_trace_acquire_returned(struct mm_struct *mm, bool write,
23 void __mmap_lock_do_trace_released(struct mm_struct *mm, bool write);
25 static inline void __mmap_lock_trace_start_locking(struct mm_struct *mm,
28 if (tracepoint_enabled(mmap_lock_start_locking))
29 __mmap_lock_do_trace_start_locking(mm, write);
32 static inline void __mmap_lock_trace_acquire_returned(struct mm_struct *mm,
33 bool write, bool success)
35 if (tracepoint_enabled(mmap_lock_acquire_returned))
36 __mmap_lock_do_trace_acquire_returned(mm, write, success);
39 static inline void __mmap_lock_trace_released(struct mm_struct *mm, bool write)
41 if (tracepoint_enabled(mmap_lock_released))
42 __mmap_lock_do_trace_released(mm, write);
45 #else /* !CONFIG_TRACING */
47 static inline void __mmap_lock_trace_start_locking(struct mm_struct *mm,
52 static inline void __mmap_lock_trace_acquire_returned(struct mm_struct *mm,
53 bool write, bool success)
57 static inline void __mmap_lock_trace_released(struct mm_struct *mm, bool write)
61 #endif /* CONFIG_TRACING */
63 static inline void mmap_init_lock(struct mm_struct *mm)
65 init_rwsem(&mm->mmap_lock);
68 static inline void mmap_write_lock(struct mm_struct *mm)
70 __mmap_lock_trace_start_locking(mm, true);
71 down_write(&mm->mmap_lock);
72 __mmap_lock_trace_acquire_returned(mm, true, true);
75 static inline void mmap_write_lock_nested(struct mm_struct *mm, int subclass)
77 __mmap_lock_trace_start_locking(mm, true);
78 down_write_nested(&mm->mmap_lock, subclass);
79 __mmap_lock_trace_acquire_returned(mm, true, true);
82 static inline int mmap_write_lock_killable(struct mm_struct *mm)
86 __mmap_lock_trace_start_locking(mm, true);
87 ret = down_write_killable(&mm->mmap_lock);
88 __mmap_lock_trace_acquire_returned(mm, true, ret == 0);
92 static inline bool mmap_write_trylock(struct mm_struct *mm)
96 __mmap_lock_trace_start_locking(mm, true);
97 ret = down_write_trylock(&mm->mmap_lock) != 0;
98 __mmap_lock_trace_acquire_returned(mm, true, ret);
102 static inline void mmap_write_unlock(struct mm_struct *mm)
104 up_write(&mm->mmap_lock);
105 __mmap_lock_trace_released(mm, true);
108 static inline void mmap_write_downgrade(struct mm_struct *mm)
110 downgrade_write(&mm->mmap_lock);
111 __mmap_lock_trace_acquire_returned(mm, false, true);
114 static inline void mmap_read_lock(struct mm_struct *mm)
116 __mmap_lock_trace_start_locking(mm, false);
117 down_read(&mm->mmap_lock);
118 __mmap_lock_trace_acquire_returned(mm, false, true);
121 static inline int mmap_read_lock_killable(struct mm_struct *mm)
125 __mmap_lock_trace_start_locking(mm, false);
126 ret = down_read_killable(&mm->mmap_lock);
127 __mmap_lock_trace_acquire_returned(mm, false, ret == 0);
131 static inline bool mmap_read_trylock(struct mm_struct *mm)
135 __mmap_lock_trace_start_locking(mm, false);
136 ret = down_read_trylock(&mm->mmap_lock) != 0;
137 __mmap_lock_trace_acquire_returned(mm, false, ret);
141 static inline void mmap_read_unlock(struct mm_struct *mm)
143 up_read(&mm->mmap_lock);
144 __mmap_lock_trace_released(mm, false);
147 static inline bool mmap_read_trylock_non_owner(struct mm_struct *mm)
149 if (mmap_read_trylock(mm)) {
150 rwsem_release(&mm->mmap_lock.dep_map, _RET_IP_);
156 static inline void mmap_read_unlock_non_owner(struct mm_struct *mm)
158 up_read_non_owner(&mm->mmap_lock);
159 __mmap_lock_trace_released(mm, false);
162 static inline void mmap_assert_locked(struct mm_struct *mm)
164 lockdep_assert_held(&mm->mmap_lock);
165 VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_lock), mm);
168 static inline void mmap_assert_write_locked(struct mm_struct *mm)
170 lockdep_assert_held_write(&mm->mmap_lock);
171 VM_BUG_ON_MM(!rwsem_is_locked(&mm->mmap_lock), mm);
174 static inline int mmap_lock_is_contended(struct mm_struct *mm)
176 return rwsem_is_contended(&mm->mmap_lock);
179 #endif /* _LINUX_MMAP_LOCK_H */