mmu_notifier.c 9.4 KB

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