1
2
3
4
5
6
7
8
9
10
11
12#include <linux/rculist.h>
13#include <linux/mmu_notifier.h>
14#include <linux/export.h>
15#include <linux/mm.h>
16#include <linux/err.h>
17#include <linux/rcupdate.h>
18#include <linux/sched.h>
19#include <linux/slab.h>
20
21
22
23
24
25
26
27
28
29
30
31
32
33void __mmu_notifier_release(struct mm_struct *mm)
34{
35 struct mmu_notifier *mn;
36 struct hlist_node *n;
37
38
39
40
41
42 rcu_read_lock();
43 hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist)
44
45
46
47
48
49
50
51 if (mn->ops->release)
52 mn->ops->release(mn, mm);
53 rcu_read_unlock();
54
55 spin_lock(&mm->mmu_notifier_mm->lock);
56 while (unlikely(!hlist_empty(&mm->mmu_notifier_mm->list))) {
57 mn = hlist_entry(mm->mmu_notifier_mm->list.first,
58 struct mmu_notifier,
59 hlist);
60
61
62
63
64
65
66 hlist_del_init_rcu(&mn->hlist);
67 }
68 spin_unlock(&mm->mmu_notifier_mm->lock);
69
70
71
72
73
74
75
76
77
78
79 synchronize_rcu();
80}
81
82
83
84
85
86
87int __mmu_notifier_clear_flush_young(struct mm_struct *mm,
88 unsigned long address)
89{
90 struct mmu_notifier *mn;
91 struct hlist_node *n;
92 int young = 0;
93
94 rcu_read_lock();
95 hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
96 if (mn->ops->clear_flush_young)
97 young |= mn->ops->clear_flush_young(mn, mm, address);
98 }
99 rcu_read_unlock();
100
101 return young;
102}
103
104int __mmu_notifier_test_young(struct mm_struct *mm,
105 unsigned long address)
106{
107 struct mmu_notifier *mn;
108 struct hlist_node *n;
109 int young = 0;
110
111 rcu_read_lock();
112 hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
113 if (mn->ops->test_young) {
114 young = mn->ops->test_young(mn, mm, address);
115 if (young)
116 break;
117 }
118 }
119 rcu_read_unlock();
120
121 return young;
122}
123
124void __mmu_notifier_change_pte(struct mm_struct *mm, unsigned long address,
125 pte_t pte)
126{
127 struct mmu_notifier *mn;
128 struct hlist_node *n;
129
130 rcu_read_lock();
131 hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
132 if (mn->ops->change_pte)
133 mn->ops->change_pte(mn, mm, address, pte);
134
135
136
137
138 else if (mn->ops->invalidate_page)
139 mn->ops->invalidate_page(mn, mm, address);
140 }
141 rcu_read_unlock();
142}
143
144void __mmu_notifier_invalidate_page(struct mm_struct *mm,
145 unsigned long address)
146{
147 struct mmu_notifier *mn;
148 struct hlist_node *n;
149
150 rcu_read_lock();
151 hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
152 if (mn->ops->invalidate_page)
153 mn->ops->invalidate_page(mn, mm, address);
154 }
155 rcu_read_unlock();
156}
157
158void __mmu_notifier_invalidate_range_start(struct mm_struct *mm,
159 unsigned long start, unsigned long end)
160{
161 struct mmu_notifier *mn;
162 struct hlist_node *n;
163
164 rcu_read_lock();
165 hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
166 if (mn->ops->invalidate_range_start)
167 mn->ops->invalidate_range_start(mn, mm, start, end);
168 }
169 rcu_read_unlock();
170}
171
172void __mmu_notifier_invalidate_range_end(struct mm_struct *mm,
173 unsigned long start, unsigned long end)
174{
175 struct mmu_notifier *mn;
176 struct hlist_node *n;
177
178 rcu_read_lock();
179 hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
180 if (mn->ops->invalidate_range_end)
181 mn->ops->invalidate_range_end(mn, mm, start, end);
182 }
183 rcu_read_unlock();
184}
185
186static int do_mmu_notifier_register(struct mmu_notifier *mn,
187 struct mm_struct *mm,
188 int take_mmap_sem)
189{
190 struct mmu_notifier_mm *mmu_notifier_mm;
191 int ret;
192
193 BUG_ON(atomic_read(&mm->mm_users) <= 0);
194
195 ret = -ENOMEM;
196 mmu_notifier_mm = kmalloc(sizeof(struct mmu_notifier_mm), GFP_KERNEL);
197 if (unlikely(!mmu_notifier_mm))
198 goto out;
199
200 if (take_mmap_sem)
201 down_write(&mm->mmap_sem);
202 ret = mm_take_all_locks(mm);
203 if (unlikely(ret))
204 goto out_cleanup;
205
206 if (!mm_has_notifiers(mm)) {
207 INIT_HLIST_HEAD(&mmu_notifier_mm->list);
208 spin_lock_init(&mmu_notifier_mm->lock);
209 mm->mmu_notifier_mm = mmu_notifier_mm;
210 mmu_notifier_mm = NULL;
211 }
212 atomic_inc(&mm->mm_count);
213
214
215
216
217
218
219
220
221
222 spin_lock(&mm->mmu_notifier_mm->lock);
223 hlist_add_head(&mn->hlist, &mm->mmu_notifier_mm->list);
224 spin_unlock(&mm->mmu_notifier_mm->lock);
225
226 mm_drop_all_locks(mm);
227out_cleanup:
228 if (take_mmap_sem)
229 up_write(&mm->mmap_sem);
230
231 kfree(mmu_notifier_mm);
232out:
233 BUG_ON(atomic_read(&mm->mm_users) <= 0);
234 return ret;
235}
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250int mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)
251{
252 return do_mmu_notifier_register(mn, mm, 1);
253}
254EXPORT_SYMBOL_GPL(mmu_notifier_register);
255
256
257
258
259
260int __mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)
261{
262 return do_mmu_notifier_register(mn, mm, 0);
263}
264EXPORT_SYMBOL_GPL(__mmu_notifier_register);
265
266
267void __mmu_notifier_mm_destroy(struct mm_struct *mm)
268{
269 BUG_ON(!hlist_empty(&mm->mmu_notifier_mm->list));
270 kfree(mm->mmu_notifier_mm);
271 mm->mmu_notifier_mm = LIST_POISON1;
272}
273
274
275
276
277
278
279
280
281
282
283
284void mmu_notifier_unregister(struct mmu_notifier *mn, struct mm_struct *mm)
285{
286 BUG_ON(atomic_read(&mm->mm_count) <= 0);
287
288 if (!hlist_unhashed(&mn->hlist)) {
289
290
291
292
293 rcu_read_lock();
294
295
296
297
298
299
300 if (mn->ops->release)
301 mn->ops->release(mn, mm);
302 rcu_read_unlock();
303
304 spin_lock(&mm->mmu_notifier_mm->lock);
305 hlist_del_rcu(&mn->hlist);
306 spin_unlock(&mm->mmu_notifier_mm->lock);
307 }
308
309
310
311
312
313 synchronize_rcu();
314
315 BUG_ON(atomic_read(&mm->mm_count) <= 0);
316
317 mmdrop(mm);
318}
319EXPORT_SYMBOL_GPL(mmu_notifier_unregister);
320