• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef _LINUX_MMAP_LOCK_H
2 #define _LINUX_MMAP_LOCK_H
3 
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>
10 
11 #define MMAP_LOCK_INITIALIZER(name) \
12 	.mmap_lock = __RWSEM_INITIALIZER((name).mmap_lock),
13 
14 DECLARE_TRACEPOINT(mmap_lock_start_locking);
15 DECLARE_TRACEPOINT(mmap_lock_acquire_returned);
16 DECLARE_TRACEPOINT(mmap_lock_released);
17 
18 #ifdef CONFIG_TRACING
19 
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,
22 					   bool success);
23 void __mmap_lock_do_trace_released(struct mm_struct *mm, bool write);
24 
__mmap_lock_trace_start_locking(struct mm_struct * mm,bool write)25 static inline void __mmap_lock_trace_start_locking(struct mm_struct *mm,
26 						   bool write)
27 {
28 	if (tracepoint_enabled(mmap_lock_start_locking))
29 		__mmap_lock_do_trace_start_locking(mm, write);
30 }
31 
__mmap_lock_trace_acquire_returned(struct mm_struct * mm,bool write,bool success)32 static inline void __mmap_lock_trace_acquire_returned(struct mm_struct *mm,
33 						      bool write, bool success)
34 {
35 	if (tracepoint_enabled(mmap_lock_acquire_returned))
36 		__mmap_lock_do_trace_acquire_returned(mm, write, success);
37 }
38 
__mmap_lock_trace_released(struct mm_struct * mm,bool write)39 static inline void __mmap_lock_trace_released(struct mm_struct *mm, bool write)
40 {
41 	if (tracepoint_enabled(mmap_lock_released))
42 		__mmap_lock_do_trace_released(mm, write);
43 }
44 
45 #else /* !CONFIG_TRACING */
46 
__mmap_lock_trace_start_locking(struct mm_struct * mm,bool write)47 static inline void __mmap_lock_trace_start_locking(struct mm_struct *mm,
48 						   bool write)
49 {
50 }
51 
__mmap_lock_trace_acquire_returned(struct mm_struct * mm,bool write,bool success)52 static inline void __mmap_lock_trace_acquire_returned(struct mm_struct *mm,
53 						      bool write, bool success)
54 {
55 }
56 
__mmap_lock_trace_released(struct mm_struct * mm,bool write)57 static inline void __mmap_lock_trace_released(struct mm_struct *mm, bool write)
58 {
59 }
60 
61 #endif /* CONFIG_TRACING */
62 
mmap_assert_locked(const struct mm_struct * mm)63 static inline void mmap_assert_locked(const struct mm_struct *mm)
64 {
65 	rwsem_assert_held(&mm->mmap_lock);
66 }
67 
mmap_assert_write_locked(const struct mm_struct * mm)68 static inline void mmap_assert_write_locked(const struct mm_struct *mm)
69 {
70 	rwsem_assert_held_write(&mm->mmap_lock);
71 }
72 
73 #ifdef CONFIG_PER_VMA_LOCK
74 
mm_lock_seqcount_init(struct mm_struct * mm)75 static inline void mm_lock_seqcount_init(struct mm_struct *mm)
76 {
77 	seqcount_init(&mm->mm_lock_seq);
78 }
79 
mm_lock_seqcount_begin(struct mm_struct * mm)80 static inline void mm_lock_seqcount_begin(struct mm_struct *mm)
81 {
82 	do_raw_write_seqcount_begin(&mm->mm_lock_seq);
83 }
84 
mm_lock_seqcount_end(struct mm_struct * mm)85 static inline void mm_lock_seqcount_end(struct mm_struct *mm)
86 {
87 	ASSERT_EXCLUSIVE_WRITER(mm->mm_lock_seq);
88 	do_raw_write_seqcount_end(&mm->mm_lock_seq);
89 }
90 
mmap_lock_speculate_try_begin(struct mm_struct * mm,unsigned int * seq)91 static inline bool mmap_lock_speculate_try_begin(struct mm_struct *mm, unsigned int *seq)
92 {
93 	/*
94 	 * Since mmap_lock is a sleeping lock, and waiting for it to become
95 	 * unlocked is more or less equivalent with taking it ourselves, don't
96 	 * bother with the speculative path if mmap_lock is already write-locked
97 	 * and take the slow path, which takes the lock.
98 	 */
99 	return raw_seqcount_try_begin(&mm->mm_lock_seq, *seq);
100 }
101 
mmap_lock_speculate_retry(struct mm_struct * mm,unsigned int seq)102 static inline bool mmap_lock_speculate_retry(struct mm_struct *mm, unsigned int seq)
103 {
104 	return read_seqcount_retry(&mm->mm_lock_seq, seq);
105 }
106 
107 /*
108  * Locks next vma pointed by the iterator. Confirms the locked vma has not
109  * been modified and will retry under mmap_lock protection if modification
110  * was detected. Should be called from read RCU section.
111  * Returns either a valid locked VMA, NULL if no more VMAs or -EINTR if the
112  * process was interrupted.
113  */
114 struct vm_area_struct *lock_next_vma(struct mm_struct *mm,
115 				     struct vma_iterator *iter,
116 				     unsigned long address);
117 
118 #else /* CONFIG_PER_VMA_LOCK */
119 
mm_lock_seqcount_init(struct mm_struct * mm)120 static inline void mm_lock_seqcount_init(struct mm_struct *mm) {}
mm_lock_seqcount_begin(struct mm_struct * mm)121 static inline void mm_lock_seqcount_begin(struct mm_struct *mm) {}
mm_lock_seqcount_end(struct mm_struct * mm)122 static inline void mm_lock_seqcount_end(struct mm_struct *mm) {}
123 
mmap_lock_speculate_try_begin(struct mm_struct * mm,unsigned int * seq)124 static inline bool mmap_lock_speculate_try_begin(struct mm_struct *mm, unsigned int *seq)
125 {
126 	return false;
127 }
128 
mmap_lock_speculate_retry(struct mm_struct * mm,unsigned int seq)129 static inline bool mmap_lock_speculate_retry(struct mm_struct *mm, unsigned int seq)
130 {
131 	return true;
132 }
133 
134 #endif /* CONFIG_PER_VMA_LOCK */
135 
mmap_write_lock(struct mm_struct * mm)136 static inline void mmap_write_lock(struct mm_struct *mm)
137 {
138 	__mmap_lock_trace_start_locking(mm, true);
139 	down_write(&mm->mmap_lock);
140 	mm_lock_seqcount_begin(mm);
141 	__mmap_lock_trace_acquire_returned(mm, true, true);
142 }
143 
mmap_write_lock_nested(struct mm_struct * mm,int subclass)144 static inline void mmap_write_lock_nested(struct mm_struct *mm, int subclass)
145 {
146 	__mmap_lock_trace_start_locking(mm, true);
147 	down_write_nested(&mm->mmap_lock, subclass);
148 	mm_lock_seqcount_begin(mm);
149 	__mmap_lock_trace_acquire_returned(mm, true, true);
150 }
151 
mmap_write_lock_killable(struct mm_struct * mm)152 static inline int mmap_write_lock_killable(struct mm_struct *mm)
153 {
154 	int ret;
155 
156 	__mmap_lock_trace_start_locking(mm, true);
157 	ret = down_write_killable(&mm->mmap_lock);
158 	if (!ret)
159 		mm_lock_seqcount_begin(mm);
160 	__mmap_lock_trace_acquire_returned(mm, true, ret == 0);
161 	return ret;
162 }
163 
164 /*
165  * Drop all currently-held per-VMA locks.
166  * This is called from the mmap_lock implementation directly before releasing
167  * a write-locked mmap_lock (or downgrading it to read-locked).
168  * This should normally NOT be called manually from other places.
169  * If you want to call this manually anyway, keep in mind that this will release
170  * *all* VMA write locks, including ones from further up the stack.
171  */
vma_end_write_all(struct mm_struct * mm)172 static inline void vma_end_write_all(struct mm_struct *mm)
173 {
174 	mmap_assert_write_locked(mm);
175 	mm_lock_seqcount_end(mm);
176 }
177 
mmap_write_unlock(struct mm_struct * mm)178 static inline void mmap_write_unlock(struct mm_struct *mm)
179 {
180 	__mmap_lock_trace_released(mm, true);
181 	vma_end_write_all(mm);
182 	up_write(&mm->mmap_lock);
183 }
184 
mmap_write_downgrade(struct mm_struct * mm)185 static inline void mmap_write_downgrade(struct mm_struct *mm)
186 {
187 	__mmap_lock_trace_acquire_returned(mm, false, true);
188 	vma_end_write_all(mm);
189 	downgrade_write(&mm->mmap_lock);
190 }
191 
mmap_read_lock(struct mm_struct * mm)192 static inline void mmap_read_lock(struct mm_struct *mm)
193 {
194 	__mmap_lock_trace_start_locking(mm, false);
195 	down_read(&mm->mmap_lock);
196 	__mmap_lock_trace_acquire_returned(mm, false, true);
197 }
198 
mmap_read_lock_killable(struct mm_struct * mm)199 static inline int mmap_read_lock_killable(struct mm_struct *mm)
200 {
201 	int ret;
202 
203 	__mmap_lock_trace_start_locking(mm, false);
204 	ret = down_read_killable(&mm->mmap_lock);
205 	__mmap_lock_trace_acquire_returned(mm, false, ret == 0);
206 	return ret;
207 }
208 
mmap_read_trylock(struct mm_struct * mm)209 static inline bool mmap_read_trylock(struct mm_struct *mm)
210 {
211 	bool ret;
212 
213 	__mmap_lock_trace_start_locking(mm, false);
214 	ret = down_read_trylock(&mm->mmap_lock) != 0;
215 	__mmap_lock_trace_acquire_returned(mm, false, ret);
216 	return ret;
217 }
218 
mmap_read_unlock(struct mm_struct * mm)219 static inline void mmap_read_unlock(struct mm_struct *mm)
220 {
221 	__mmap_lock_trace_released(mm, false);
222 	up_read(&mm->mmap_lock);
223 }
224 
mmap_read_unlock_non_owner(struct mm_struct * mm)225 static inline void mmap_read_unlock_non_owner(struct mm_struct *mm)
226 {
227 	__mmap_lock_trace_released(mm, false);
228 	up_read_non_owner(&mm->mmap_lock);
229 }
230 
mmap_lock_is_contended(struct mm_struct * mm)231 static inline int mmap_lock_is_contended(struct mm_struct *mm)
232 {
233 	return rwsem_is_contended(&mm->mmap_lock);
234 }
235 
236 #endif /* _LINUX_MMAP_LOCK_H */
237