mmu_notifier.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 can't run concurrently against mmu_notifier_register
  24. * because mm->mm_users > 0 during mmu_notifier_register and exit_mmap
  25. * runs with mm_users == 0. Other tasks may still invoke mmu notifiers
  26. * in parallel despite there being no task using this mm any more,
  27. * through the vmas outside of the exit_mmap context, such as with
  28. * vmtruncate. This serializes against mmu_notifier_unregister with
  29. * the mmu_notifier_mm->lock in addition to SRCU and it serializes
  30. * against the other mmu notifiers with SRCU. struct mmu_notifier_mm
  31. * can't go away from under us as exit_mmap holds an mm_count pin
  32. * itself.
  33. */
  34. void __mmu_notifier_release(struct mm_struct *mm)
  35. {
  36. struct mmu_notifier *mn;
  37. int id;
  38. /*
  39. * srcu_read_lock() here will block synchronize_srcu() in
  40. * mmu_notifier_unregister() until all registered
  41. * ->release() callouts this function makes have
  42. * returned.
  43. */
  44. id = srcu_read_lock(&srcu);
  45. spin_lock(&mm->mmu_notifier_mm->lock);
  46. while (unlikely(!hlist_empty(&mm->mmu_notifier_mm->list))) {
  47. mn = hlist_entry(mm->mmu_notifier_mm->list.first,
  48. struct mmu_notifier,
  49. hlist);
  50. /*
  51. * Unlink. This will prevent mmu_notifier_unregister()
  52. * from also making the ->release() callout.
  53. */
  54. hlist_del_init_rcu(&mn->hlist);
  55. spin_unlock(&mm->mmu_notifier_mm->lock);
  56. /*
  57. * Clear sptes. (see 'release' description in mmu_notifier.h)
  58. */
  59. if (mn->ops->release)
  60. mn->ops->release(mn, mm);
  61. spin_lock(&mm->mmu_notifier_mm->lock);
  62. }
  63. spin_unlock(&mm->mmu_notifier_mm->lock);
  64. /*
  65. * All callouts to ->release() which we have done are complete.
  66. * Allow synchronize_srcu() in mmu_notifier_unregister() to complete
  67. */
  68. srcu_read_unlock(&srcu, id);
  69. /*
  70. * mmu_notifier_unregister() may have unlinked a notifier and may
  71. * still be calling out to it. Additionally, other notifiers
  72. * may have been active via vmtruncate() et. al. Block here
  73. * to ensure that all notifier callouts for this mm have been
  74. * completed and the sptes are really cleaned up before returning
  75. * to exit_mmap().
  76. */
  77. synchronize_srcu(&srcu);
  78. }
  79. /*
  80. * If no young bitflag is supported by the hardware, ->clear_flush_young can
  81. * unmap the address and return 1 or 0 depending if the mapping previously
  82. * existed or not.
  83. */
  84. int __mmu_notifier_clear_flush_young(struct mm_struct *mm,
  85. unsigned long address)
  86. {
  87. struct mmu_notifier *mn;
  88. struct hlist_node *n;
  89. int young = 0, id;
  90. id = srcu_read_lock(&srcu);
  91. hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
  92. if (mn->ops->clear_flush_young)
  93. young |= mn->ops->clear_flush_young(mn, mm, address);
  94. }
  95. srcu_read_unlock(&srcu, id);
  96. return young;
  97. }
  98. int __mmu_notifier_test_young(struct mm_struct *mm,
  99. unsigned long address)
  100. {
  101. struct mmu_notifier *mn;
  102. struct hlist_node *n;
  103. int young = 0, id;
  104. id = srcu_read_lock(&srcu);
  105. hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
  106. if (mn->ops->test_young) {
  107. young = mn->ops->test_young(mn, mm, address);
  108. if (young)
  109. break;
  110. }
  111. }
  112. srcu_read_unlock(&srcu, id);
  113. return young;
  114. }
  115. void __mmu_notifier_change_pte(struct mm_struct *mm, unsigned long address,
  116. pte_t pte)
  117. {
  118. struct mmu_notifier *mn;
  119. struct hlist_node *n;
  120. int id;
  121. id = srcu_read_lock(&srcu);
  122. hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
  123. if (mn->ops->change_pte)
  124. mn->ops->change_pte(mn, mm, address, pte);
  125. }
  126. srcu_read_unlock(&srcu, id);
  127. }
  128. void __mmu_notifier_invalidate_page(struct mm_struct *mm,
  129. unsigned long address)
  130. {
  131. struct mmu_notifier *mn;
  132. struct hlist_node *n;
  133. int id;
  134. id = srcu_read_lock(&srcu);
  135. hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
  136. if (mn->ops->invalidate_page)
  137. mn->ops->invalidate_page(mn, mm, address);
  138. }
  139. srcu_read_unlock(&srcu, id);
  140. }
  141. void __mmu_notifier_invalidate_range_start(struct mm_struct *mm,
  142. unsigned long start, unsigned long end)
  143. {
  144. struct mmu_notifier *mn;
  145. struct hlist_node *n;
  146. int id;
  147. id = srcu_read_lock(&srcu);
  148. hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
  149. if (mn->ops->invalidate_range_start)
  150. mn->ops->invalidate_range_start(mn, mm, start, end);
  151. }
  152. srcu_read_unlock(&srcu, id);
  153. }
  154. void __mmu_notifier_invalidate_range_end(struct mm_struct *mm,
  155. unsigned long start, unsigned long end)
  156. {
  157. struct mmu_notifier *mn;
  158. struct hlist_node *n;
  159. int id;
  160. id = srcu_read_lock(&srcu);
  161. hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
  162. if (mn->ops->invalidate_range_end)
  163. mn->ops->invalidate_range_end(mn, mm, start, end);
  164. }
  165. srcu_read_unlock(&srcu, id);
  166. }
  167. static int do_mmu_notifier_register(struct mmu_notifier *mn,
  168. struct mm_struct *mm,
  169. int take_mmap_sem)
  170. {
  171. struct mmu_notifier_mm *mmu_notifier_mm;
  172. int ret;
  173. BUG_ON(atomic_read(&mm->mm_users) <= 0);
  174. /*
  175. * Verify that mmu_notifier_init() already run and the global srcu is
  176. * initialized.
  177. */
  178. BUG_ON(!srcu.per_cpu_ref);
  179. ret = -ENOMEM;
  180. mmu_notifier_mm = kmalloc(sizeof(struct mmu_notifier_mm), GFP_KERNEL);
  181. if (unlikely(!mmu_notifier_mm))
  182. goto out;
  183. if (take_mmap_sem)
  184. down_write(&mm->mmap_sem);
  185. ret = mm_take_all_locks(mm);
  186. if (unlikely(ret))
  187. goto out_clean;
  188. if (!mm_has_notifiers(mm)) {
  189. INIT_HLIST_HEAD(&mmu_notifier_mm->list);
  190. spin_lock_init(&mmu_notifier_mm->lock);
  191. mm->mmu_notifier_mm = mmu_notifier_mm;
  192. mmu_notifier_mm = NULL;
  193. }
  194. atomic_inc(&mm->mm_count);
  195. /*
  196. * Serialize the update against mmu_notifier_unregister. A
  197. * side note: mmu_notifier_release can't run concurrently with
  198. * us because we hold the mm_users pin (either implicitly as
  199. * current->mm or explicitly with get_task_mm() or similar).
  200. * We can't race against any other mmu notifier method either
  201. * thanks to mm_take_all_locks().
  202. */
  203. spin_lock(&mm->mmu_notifier_mm->lock);
  204. hlist_add_head(&mn->hlist, &mm->mmu_notifier_mm->list);
  205. spin_unlock(&mm->mmu_notifier_mm->lock);
  206. mm_drop_all_locks(mm);
  207. out_clean:
  208. if (take_mmap_sem)
  209. up_write(&mm->mmap_sem);
  210. kfree(mmu_notifier_mm);
  211. out:
  212. BUG_ON(atomic_read(&mm->mm_users) <= 0);
  213. return ret;
  214. }
  215. /*
  216. * Must not hold mmap_sem nor any other VM related lock when calling
  217. * this registration function. Must also ensure mm_users can't go down
  218. * to zero while this runs to avoid races with mmu_notifier_release,
  219. * so mm has to be current->mm or the mm should be pinned safely such
  220. * as with get_task_mm(). If the mm is not current->mm, the mm_users
  221. * pin should be released by calling mmput after mmu_notifier_register
  222. * returns. mmu_notifier_unregister must be always called to
  223. * unregister the notifier. mm_count is automatically pinned to allow
  224. * mmu_notifier_unregister to safely run at any time later, before or
  225. * after exit_mmap. ->release will always be called before exit_mmap
  226. * frees the pages.
  227. */
  228. int mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)
  229. {
  230. return do_mmu_notifier_register(mn, mm, 1);
  231. }
  232. EXPORT_SYMBOL_GPL(mmu_notifier_register);
  233. /*
  234. * Same as mmu_notifier_register but here the caller must hold the
  235. * mmap_sem in write mode.
  236. */
  237. int __mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)
  238. {
  239. return do_mmu_notifier_register(mn, mm, 0);
  240. }
  241. EXPORT_SYMBOL_GPL(__mmu_notifier_register);
  242. /* this is called after the last mmu_notifier_unregister() returned */
  243. void __mmu_notifier_mm_destroy(struct mm_struct *mm)
  244. {
  245. BUG_ON(!hlist_empty(&mm->mmu_notifier_mm->list));
  246. kfree(mm->mmu_notifier_mm);
  247. mm->mmu_notifier_mm = LIST_POISON1; /* debug */
  248. }
  249. /*
  250. * This releases the mm_count pin automatically and frees the mm
  251. * structure if it was the last user of it. It serializes against
  252. * running mmu notifiers with SRCU and against mmu_notifier_unregister
  253. * with the unregister lock + SRCU. All sptes must be dropped before
  254. * calling mmu_notifier_unregister. ->release or any other notifier
  255. * method may be invoked concurrently with mmu_notifier_unregister,
  256. * and only after mmu_notifier_unregister returned we're guaranteed
  257. * that ->release or any other method can't run anymore.
  258. */
  259. void mmu_notifier_unregister(struct mmu_notifier *mn, struct mm_struct *mm)
  260. {
  261. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  262. spin_lock(&mm->mmu_notifier_mm->lock);
  263. if (!hlist_unhashed(&mn->hlist)) {
  264. int id;
  265. /*
  266. * Ensure we synchronize up with __mmu_notifier_release().
  267. */
  268. id = srcu_read_lock(&srcu);
  269. hlist_del_rcu(&mn->hlist);
  270. spin_unlock(&mm->mmu_notifier_mm->lock);
  271. if (mn->ops->release)
  272. mn->ops->release(mn, mm);
  273. /*
  274. * Allow __mmu_notifier_release() to complete.
  275. */
  276. srcu_read_unlock(&srcu, id);
  277. } else
  278. spin_unlock(&mm->mmu_notifier_mm->lock);
  279. /*
  280. * Wait for any running method to finish, including ->release() if it
  281. * was run by __mmu_notifier_release() instead of us.
  282. */
  283. synchronize_srcu(&srcu);
  284. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  285. mmdrop(mm);
  286. }
  287. EXPORT_SYMBOL_GPL(mmu_notifier_unregister);
  288. static int __init mmu_notifier_init(void)
  289. {
  290. return init_srcu_struct(&srcu);
  291. }
  292. module_init(mmu_notifier_init);