mmu_notifier.c 9.5 KB

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