mmu_notifier.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. EXPORT_SYMBOL_GPL(__mmu_notifier_invalidate_range_start);
  155. void __mmu_notifier_invalidate_range_end(struct mm_struct *mm,
  156. unsigned long start, unsigned long end)
  157. {
  158. struct mmu_notifier *mn;
  159. struct hlist_node *n;
  160. int id;
  161. id = srcu_read_lock(&srcu);
  162. hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
  163. if (mn->ops->invalidate_range_end)
  164. mn->ops->invalidate_range_end(mn, mm, start, end);
  165. }
  166. srcu_read_unlock(&srcu, id);
  167. }
  168. EXPORT_SYMBOL_GPL(__mmu_notifier_invalidate_range_end);
  169. static int do_mmu_notifier_register(struct mmu_notifier *mn,
  170. struct mm_struct *mm,
  171. int take_mmap_sem)
  172. {
  173. struct mmu_notifier_mm *mmu_notifier_mm;
  174. int ret;
  175. BUG_ON(atomic_read(&mm->mm_users) <= 0);
  176. /*
  177. * Verify that mmu_notifier_init() already run and the global srcu is
  178. * initialized.
  179. */
  180. BUG_ON(!srcu.per_cpu_ref);
  181. ret = -ENOMEM;
  182. mmu_notifier_mm = kmalloc(sizeof(struct mmu_notifier_mm), GFP_KERNEL);
  183. if (unlikely(!mmu_notifier_mm))
  184. goto out;
  185. if (take_mmap_sem)
  186. down_write(&mm->mmap_sem);
  187. ret = mm_take_all_locks(mm);
  188. if (unlikely(ret))
  189. goto out_clean;
  190. if (!mm_has_notifiers(mm)) {
  191. INIT_HLIST_HEAD(&mmu_notifier_mm->list);
  192. spin_lock_init(&mmu_notifier_mm->lock);
  193. mm->mmu_notifier_mm = mmu_notifier_mm;
  194. mmu_notifier_mm = NULL;
  195. }
  196. atomic_inc(&mm->mm_count);
  197. /*
  198. * Serialize the update against mmu_notifier_unregister. A
  199. * side note: mmu_notifier_release can't run concurrently with
  200. * us because we hold the mm_users pin (either implicitly as
  201. * current->mm or explicitly with get_task_mm() or similar).
  202. * We can't race against any other mmu notifier method either
  203. * thanks to mm_take_all_locks().
  204. */
  205. spin_lock(&mm->mmu_notifier_mm->lock);
  206. hlist_add_head(&mn->hlist, &mm->mmu_notifier_mm->list);
  207. spin_unlock(&mm->mmu_notifier_mm->lock);
  208. mm_drop_all_locks(mm);
  209. out_clean:
  210. if (take_mmap_sem)
  211. up_write(&mm->mmap_sem);
  212. kfree(mmu_notifier_mm);
  213. out:
  214. BUG_ON(atomic_read(&mm->mm_users) <= 0);
  215. return ret;
  216. }
  217. /*
  218. * Must not hold mmap_sem nor any other VM related lock when calling
  219. * this registration function. Must also ensure mm_users can't go down
  220. * to zero while this runs to avoid races with mmu_notifier_release,
  221. * so mm has to be current->mm or the mm should be pinned safely such
  222. * as with get_task_mm(). If the mm is not current->mm, the mm_users
  223. * pin should be released by calling mmput after mmu_notifier_register
  224. * returns. mmu_notifier_unregister must be always called to
  225. * unregister the notifier. mm_count is automatically pinned to allow
  226. * mmu_notifier_unregister to safely run at any time later, before or
  227. * after exit_mmap. ->release will always be called before exit_mmap
  228. * frees the pages.
  229. */
  230. int mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)
  231. {
  232. return do_mmu_notifier_register(mn, mm, 1);
  233. }
  234. EXPORT_SYMBOL_GPL(mmu_notifier_register);
  235. /*
  236. * Same as mmu_notifier_register but here the caller must hold the
  237. * mmap_sem in write mode.
  238. */
  239. int __mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)
  240. {
  241. return do_mmu_notifier_register(mn, mm, 0);
  242. }
  243. EXPORT_SYMBOL_GPL(__mmu_notifier_register);
  244. /* this is called after the last mmu_notifier_unregister() returned */
  245. void __mmu_notifier_mm_destroy(struct mm_struct *mm)
  246. {
  247. BUG_ON(!hlist_empty(&mm->mmu_notifier_mm->list));
  248. kfree(mm->mmu_notifier_mm);
  249. mm->mmu_notifier_mm = LIST_POISON1; /* debug */
  250. }
  251. /*
  252. * This releases the mm_count pin automatically and frees the mm
  253. * structure if it was the last user of it. It serializes against
  254. * running mmu notifiers with SRCU and against mmu_notifier_unregister
  255. * with the unregister lock + SRCU. All sptes must be dropped before
  256. * calling mmu_notifier_unregister. ->release or any other notifier
  257. * method may be invoked concurrently with mmu_notifier_unregister,
  258. * and only after mmu_notifier_unregister returned we're guaranteed
  259. * that ->release or any other method can't run anymore.
  260. */
  261. void mmu_notifier_unregister(struct mmu_notifier *mn, struct mm_struct *mm)
  262. {
  263. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  264. spin_lock(&mm->mmu_notifier_mm->lock);
  265. if (!hlist_unhashed(&mn->hlist)) {
  266. int id;
  267. /*
  268. * Ensure we synchronize up with __mmu_notifier_release().
  269. */
  270. id = srcu_read_lock(&srcu);
  271. hlist_del_rcu(&mn->hlist);
  272. spin_unlock(&mm->mmu_notifier_mm->lock);
  273. if (mn->ops->release)
  274. mn->ops->release(mn, mm);
  275. /*
  276. * Allow __mmu_notifier_release() to complete.
  277. */
  278. srcu_read_unlock(&srcu, id);
  279. } else
  280. spin_unlock(&mm->mmu_notifier_mm->lock);
  281. /*
  282. * Wait for any running method to finish, including ->release() if it
  283. * was run by __mmu_notifier_release() instead of us.
  284. */
  285. synchronize_srcu(&srcu);
  286. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  287. mmdrop(mm);
  288. }
  289. EXPORT_SYMBOL_GPL(mmu_notifier_unregister);
  290. static int __init mmu_notifier_init(void)
  291. {
  292. return init_srcu_struct(&srcu);
  293. }
  294. module_init(mmu_notifier_init);