mmu_notifier.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * linux/mm/mmu_notifier.c
  3. *
  4. * Copyright (C) 2008 Qumranet, Inc.
  5. * Copyright (C) 2008 SGI
  6. * Christoph Lameter <clameter@sgi.com>
  7. *
  8. * This work is licensed under the terms of the GNU GPL, version 2. See
  9. * the COPYING file in the top-level directory.
  10. */
  11. #include <linux/rculist.h>
  12. #include <linux/mmu_notifier.h>
  13. #include <linux/export.h>
  14. #include <linux/mm.h>
  15. #include <linux/err.h>
  16. #include <linux/srcu.h>
  17. #include <linux/rcupdate.h>
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. /* global SRCU for all MMs */
  21. static struct srcu_struct srcu;
  22. /*
  23. * This function allows mmu_notifier::release callback to delay a call to
  24. * a function that will free appropriate resources. The function must be
  25. * quick and must not block.
  26. */
  27. void mmu_notifier_call_srcu(struct rcu_head *rcu,
  28. void (*func)(struct rcu_head *rcu))
  29. {
  30. call_srcu(&srcu, rcu, func);
  31. }
  32. EXPORT_SYMBOL_GPL(mmu_notifier_call_srcu);
  33. void mmu_notifier_synchronize(void)
  34. {
  35. /* Wait for any running method to finish. */
  36. srcu_barrier(&srcu);
  37. }
  38. EXPORT_SYMBOL_GPL(mmu_notifier_synchronize);
  39. /*
  40. * This function can't run concurrently against mmu_notifier_register
  41. * because mm->mm_users > 0 during mmu_notifier_register and exit_mmap
  42. * runs with mm_users == 0. Other tasks may still invoke mmu notifiers
  43. * in parallel despite there being no task using this mm any more,
  44. * through the vmas outside of the exit_mmap context, such as with
  45. * vmtruncate. This serializes against mmu_notifier_unregister with
  46. * the mmu_notifier_mm->lock in addition to SRCU and it serializes
  47. * against the other mmu notifiers with SRCU. struct mmu_notifier_mm
  48. * can't go away from under us as exit_mmap holds an mm_count pin
  49. * itself.
  50. */
  51. void __mmu_notifier_release(struct mm_struct *mm)
  52. {
  53. struct mmu_notifier *mn;
  54. int id;
  55. /*
  56. * SRCU here will block mmu_notifier_unregister until
  57. * ->release returns.
  58. */
  59. id = srcu_read_lock(&srcu);
  60. hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist)
  61. /*
  62. * If ->release runs before mmu_notifier_unregister it must be
  63. * handled, as it's the only way for the driver to flush all
  64. * existing sptes and stop the driver from establishing any more
  65. * sptes before all the pages in the mm are freed.
  66. */
  67. if (mn->ops->release)
  68. mn->ops->release(mn, mm);
  69. spin_lock(&mm->mmu_notifier_mm->lock);
  70. while (unlikely(!hlist_empty(&mm->mmu_notifier_mm->list))) {
  71. mn = hlist_entry(mm->mmu_notifier_mm->list.first,
  72. struct mmu_notifier,
  73. hlist);
  74. /*
  75. * We arrived before mmu_notifier_unregister so
  76. * mmu_notifier_unregister will do nothing other than to wait
  77. * for ->release to finish and for mmu_notifier_unregister to
  78. * return.
  79. */
  80. hlist_del_init_rcu(&mn->hlist);
  81. }
  82. spin_unlock(&mm->mmu_notifier_mm->lock);
  83. srcu_read_unlock(&srcu, id);
  84. /*
  85. * synchronize_srcu here prevents mmu_notifier_release from returning to
  86. * exit_mmap (which would proceed with freeing all pages in the mm)
  87. * until the ->release method returns, if it was invoked by
  88. * mmu_notifier_unregister.
  89. *
  90. * The mmu_notifier_mm can't go away from under us because one mm_count
  91. * is held by exit_mmap.
  92. */
  93. synchronize_srcu(&srcu);
  94. }
  95. /*
  96. * If no young bitflag is supported by the hardware, ->clear_flush_young can
  97. * unmap the address and return 1 or 0 depending if the mapping previously
  98. * existed or not.
  99. */
  100. int __mmu_notifier_clear_flush_young(struct mm_struct *mm,
  101. unsigned long start,
  102. unsigned long end)
  103. {
  104. struct mmu_notifier *mn;
  105. int young = 0, id;
  106. id = srcu_read_lock(&srcu);
  107. hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
  108. if (mn->ops->clear_flush_young)
  109. young |= mn->ops->clear_flush_young(mn, mm, start, end);
  110. }
  111. srcu_read_unlock(&srcu, id);
  112. return young;
  113. }
  114. int __mmu_notifier_clear_young(struct mm_struct *mm,
  115. unsigned long start,
  116. unsigned long end)
  117. {
  118. struct mmu_notifier *mn;
  119. int young = 0, id;
  120. id = srcu_read_lock(&srcu);
  121. hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
  122. if (mn->ops->clear_young)
  123. young |= mn->ops->clear_young(mn, mm, start, end);
  124. }
  125. srcu_read_unlock(&srcu, id);
  126. return young;
  127. }
  128. int __mmu_notifier_test_young(struct mm_struct *mm,
  129. unsigned long address)
  130. {
  131. struct mmu_notifier *mn;
  132. int young = 0, id;
  133. id = srcu_read_lock(&srcu);
  134. hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
  135. if (mn->ops->test_young) {
  136. young = mn->ops->test_young(mn, mm, address);
  137. if (young)
  138. break;
  139. }
  140. }
  141. srcu_read_unlock(&srcu, id);
  142. return young;
  143. }
  144. void __mmu_notifier_change_pte(struct mm_struct *mm, unsigned long address,
  145. pte_t pte)
  146. {
  147. struct mmu_notifier *mn;
  148. int id;
  149. id = srcu_read_lock(&srcu);
  150. hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
  151. if (mn->ops->change_pte)
  152. mn->ops->change_pte(mn, mm, address, pte);
  153. }
  154. srcu_read_unlock(&srcu, id);
  155. }
  156. void __mmu_notifier_invalidate_page(struct mm_struct *mm,
  157. unsigned long address)
  158. {
  159. struct mmu_notifier *mn;
  160. int id;
  161. id = srcu_read_lock(&srcu);
  162. hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
  163. if (mn->ops->invalidate_page)
  164. mn->ops->invalidate_page(mn, mm, address);
  165. }
  166. srcu_read_unlock(&srcu, id);
  167. }
  168. void __mmu_notifier_invalidate_range_start(struct mm_struct *mm,
  169. unsigned long start, unsigned long end)
  170. {
  171. struct mmu_notifier *mn;
  172. int id;
  173. id = srcu_read_lock(&srcu);
  174. hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
  175. if (mn->ops->invalidate_range_start)
  176. mn->ops->invalidate_range_start(mn, mm, start, end);
  177. }
  178. srcu_read_unlock(&srcu, id);
  179. }
  180. EXPORT_SYMBOL_GPL(__mmu_notifier_invalidate_range_start);
  181. void __mmu_notifier_invalidate_range_end(struct mm_struct *mm,
  182. unsigned long start, unsigned long end)
  183. {
  184. struct mmu_notifier *mn;
  185. int id;
  186. id = srcu_read_lock(&srcu);
  187. hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
  188. /*
  189. * Call invalidate_range here too to avoid the need for the
  190. * subsystem of having to register an invalidate_range_end
  191. * call-back when there is invalidate_range already. Usually a
  192. * subsystem registers either invalidate_range_start()/end() or
  193. * invalidate_range(), so this will be no additional overhead
  194. * (besides the pointer check).
  195. */
  196. if (mn->ops->invalidate_range)
  197. mn->ops->invalidate_range(mn, mm, start, end);
  198. if (mn->ops->invalidate_range_end)
  199. mn->ops->invalidate_range_end(mn, mm, start, end);
  200. }
  201. srcu_read_unlock(&srcu, id);
  202. }
  203. EXPORT_SYMBOL_GPL(__mmu_notifier_invalidate_range_end);
  204. void __mmu_notifier_invalidate_range(struct mm_struct *mm,
  205. unsigned long start, unsigned long end)
  206. {
  207. struct mmu_notifier *mn;
  208. int id;
  209. id = srcu_read_lock(&srcu);
  210. hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
  211. if (mn->ops->invalidate_range)
  212. mn->ops->invalidate_range(mn, mm, start, end);
  213. }
  214. srcu_read_unlock(&srcu, id);
  215. }
  216. EXPORT_SYMBOL_GPL(__mmu_notifier_invalidate_range);
  217. static int do_mmu_notifier_register(struct mmu_notifier *mn,
  218. struct mm_struct *mm,
  219. int take_mmap_sem)
  220. {
  221. struct mmu_notifier_mm *mmu_notifier_mm;
  222. int ret;
  223. BUG_ON(atomic_read(&mm->mm_users) <= 0);
  224. /*
  225. * Verify that mmu_notifier_init() already run and the global srcu is
  226. * initialized.
  227. */
  228. BUG_ON(!srcu.per_cpu_ref);
  229. ret = -ENOMEM;
  230. mmu_notifier_mm = kmalloc(sizeof(struct mmu_notifier_mm), GFP_KERNEL);
  231. if (unlikely(!mmu_notifier_mm))
  232. goto out;
  233. if (take_mmap_sem)
  234. down_write(&mm->mmap_sem);
  235. ret = mm_take_all_locks(mm);
  236. if (unlikely(ret))
  237. goto out_clean;
  238. if (!mm_has_notifiers(mm)) {
  239. INIT_HLIST_HEAD(&mmu_notifier_mm->list);
  240. spin_lock_init(&mmu_notifier_mm->lock);
  241. mm->mmu_notifier_mm = mmu_notifier_mm;
  242. mmu_notifier_mm = NULL;
  243. }
  244. atomic_inc(&mm->mm_count);
  245. /*
  246. * Serialize the update against mmu_notifier_unregister. A
  247. * side note: mmu_notifier_release can't run concurrently with
  248. * us because we hold the mm_users pin (either implicitly as
  249. * current->mm or explicitly with get_task_mm() or similar).
  250. * We can't race against any other mmu notifier method either
  251. * thanks to mm_take_all_locks().
  252. */
  253. spin_lock(&mm->mmu_notifier_mm->lock);
  254. hlist_add_head(&mn->hlist, &mm->mmu_notifier_mm->list);
  255. spin_unlock(&mm->mmu_notifier_mm->lock);
  256. mm_drop_all_locks(mm);
  257. out_clean:
  258. if (take_mmap_sem)
  259. up_write(&mm->mmap_sem);
  260. kfree(mmu_notifier_mm);
  261. out:
  262. BUG_ON(atomic_read(&mm->mm_users) <= 0);
  263. return ret;
  264. }
  265. /*
  266. * Must not hold mmap_sem nor any other VM related lock when calling
  267. * this registration function. Must also ensure mm_users can't go down
  268. * to zero while this runs to avoid races with mmu_notifier_release,
  269. * so mm has to be current->mm or the mm should be pinned safely such
  270. * as with get_task_mm(). If the mm is not current->mm, the mm_users
  271. * pin should be released by calling mmput after mmu_notifier_register
  272. * returns. mmu_notifier_unregister must be always called to
  273. * unregister the notifier. mm_count is automatically pinned to allow
  274. * mmu_notifier_unregister to safely run at any time later, before or
  275. * after exit_mmap. ->release will always be called before exit_mmap
  276. * frees the pages.
  277. */
  278. int mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)
  279. {
  280. return do_mmu_notifier_register(mn, mm, 1);
  281. }
  282. EXPORT_SYMBOL_GPL(mmu_notifier_register);
  283. /*
  284. * Same as mmu_notifier_register but here the caller must hold the
  285. * mmap_sem in write mode.
  286. */
  287. int __mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)
  288. {
  289. return do_mmu_notifier_register(mn, mm, 0);
  290. }
  291. EXPORT_SYMBOL_GPL(__mmu_notifier_register);
  292. /* this is called after the last mmu_notifier_unregister() returned */
  293. void __mmu_notifier_mm_destroy(struct mm_struct *mm)
  294. {
  295. BUG_ON(!hlist_empty(&mm->mmu_notifier_mm->list));
  296. kfree(mm->mmu_notifier_mm);
  297. mm->mmu_notifier_mm = LIST_POISON1; /* debug */
  298. }
  299. /*
  300. * This releases the mm_count pin automatically and frees the mm
  301. * structure if it was the last user of it. It serializes against
  302. * running mmu notifiers with SRCU and against mmu_notifier_unregister
  303. * with the unregister lock + SRCU. All sptes must be dropped before
  304. * calling mmu_notifier_unregister. ->release or any other notifier
  305. * method may be invoked concurrently with mmu_notifier_unregister,
  306. * and only after mmu_notifier_unregister returned we're guaranteed
  307. * that ->release or any other method can't run anymore.
  308. */
  309. void mmu_notifier_unregister(struct mmu_notifier *mn, struct mm_struct *mm)
  310. {
  311. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  312. if (!hlist_unhashed(&mn->hlist)) {
  313. /*
  314. * SRCU here will force exit_mmap to wait for ->release to
  315. * finish before freeing the pages.
  316. */
  317. int id;
  318. id = srcu_read_lock(&srcu);
  319. /*
  320. * exit_mmap will block in mmu_notifier_release to guarantee
  321. * that ->release is called before freeing the pages.
  322. */
  323. if (mn->ops->release)
  324. mn->ops->release(mn, mm);
  325. srcu_read_unlock(&srcu, id);
  326. spin_lock(&mm->mmu_notifier_mm->lock);
  327. /*
  328. * Can not use list_del_rcu() since __mmu_notifier_release
  329. * can delete it before we hold the lock.
  330. */
  331. hlist_del_init_rcu(&mn->hlist);
  332. spin_unlock(&mm->mmu_notifier_mm->lock);
  333. }
  334. /*
  335. * Wait for any running method to finish, of course including
  336. * ->release if it was run by mmu_notifier_release instead of us.
  337. */
  338. synchronize_srcu(&srcu);
  339. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  340. mmdrop(mm);
  341. }
  342. EXPORT_SYMBOL_GPL(mmu_notifier_unregister);
  343. /*
  344. * Same as mmu_notifier_unregister but no callback and no srcu synchronization.
  345. */
  346. void mmu_notifier_unregister_no_release(struct mmu_notifier *mn,
  347. struct mm_struct *mm)
  348. {
  349. spin_lock(&mm->mmu_notifier_mm->lock);
  350. /*
  351. * Can not use list_del_rcu() since __mmu_notifier_release
  352. * can delete it before we hold the lock.
  353. */
  354. hlist_del_init_rcu(&mn->hlist);
  355. spin_unlock(&mm->mmu_notifier_mm->lock);
  356. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  357. mmdrop(mm);
  358. }
  359. EXPORT_SYMBOL_GPL(mmu_notifier_unregister_no_release);
  360. static int __init mmu_notifier_init(void)
  361. {
  362. return init_srcu_struct(&srcu);
  363. }
  364. subsys_initcall(mmu_notifier_init);