mmu_notifier.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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/module.h>
  14. #include <linux/mm.h>
  15. #include <linux/err.h>
  16. #include <linux/rcupdate.h>
  17. #include <linux/sched.h>
  18. /*
  19. * This function can't run concurrently against mmu_notifier_register
  20. * because mm->mm_users > 0 during mmu_notifier_register and exit_mmap
  21. * runs with mm_users == 0. Other tasks may still invoke mmu notifiers
  22. * in parallel despite there being no task using this mm any more,
  23. * through the vmas outside of the exit_mmap context, such as with
  24. * vmtruncate. This serializes against mmu_notifier_unregister with
  25. * the mmu_notifier_mm->lock in addition to RCU and it serializes
  26. * against the other mmu notifiers with RCU. struct mmu_notifier_mm
  27. * can't go away from under us as exit_mmap holds an mm_count pin
  28. * itself.
  29. */
  30. void __mmu_notifier_release(struct mm_struct *mm)
  31. {
  32. struct mmu_notifier *mn;
  33. spin_lock(&mm->mmu_notifier_mm->lock);
  34. while (unlikely(!hlist_empty(&mm->mmu_notifier_mm->list))) {
  35. mn = hlist_entry(mm->mmu_notifier_mm->list.first,
  36. struct mmu_notifier,
  37. hlist);
  38. /*
  39. * We arrived before mmu_notifier_unregister so
  40. * mmu_notifier_unregister will do nothing other than
  41. * to wait ->release to finish and
  42. * mmu_notifier_unregister to return.
  43. */
  44. hlist_del_init_rcu(&mn->hlist);
  45. /*
  46. * RCU here will block mmu_notifier_unregister until
  47. * ->release returns.
  48. */
  49. rcu_read_lock();
  50. spin_unlock(&mm->mmu_notifier_mm->lock);
  51. /*
  52. * if ->release runs before mmu_notifier_unregister it
  53. * must be handled as it's the only way for the driver
  54. * to flush all existing sptes and stop the driver
  55. * from establishing any more sptes before all the
  56. * pages in the mm are freed.
  57. */
  58. if (mn->ops->release)
  59. mn->ops->release(mn, mm);
  60. rcu_read_unlock();
  61. spin_lock(&mm->mmu_notifier_mm->lock);
  62. }
  63. spin_unlock(&mm->mmu_notifier_mm->lock);
  64. /*
  65. * synchronize_rcu here prevents mmu_notifier_release to
  66. * return to exit_mmap (which would proceed freeing all pages
  67. * in the mm) until the ->release method returns, if it was
  68. * invoked by mmu_notifier_unregister.
  69. *
  70. * The mmu_notifier_mm can't go away from under us because one
  71. * mm_count is hold by exit_mmap.
  72. */
  73. synchronize_rcu();
  74. }
  75. /*
  76. * If no young bitflag is supported by the hardware, ->clear_flush_young can
  77. * unmap the address and return 1 or 0 depending if the mapping previously
  78. * existed or not.
  79. */
  80. int __mmu_notifier_clear_flush_young(struct mm_struct *mm,
  81. unsigned long address)
  82. {
  83. struct mmu_notifier *mn;
  84. struct hlist_node *n;
  85. int young = 0;
  86. rcu_read_lock();
  87. hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
  88. if (mn->ops->clear_flush_young)
  89. young |= mn->ops->clear_flush_young(mn, mm, address);
  90. }
  91. rcu_read_unlock();
  92. return young;
  93. }
  94. void __mmu_notifier_invalidate_page(struct mm_struct *mm,
  95. unsigned long address)
  96. {
  97. struct mmu_notifier *mn;
  98. struct hlist_node *n;
  99. rcu_read_lock();
  100. hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
  101. if (mn->ops->invalidate_page)
  102. mn->ops->invalidate_page(mn, mm, address);
  103. }
  104. rcu_read_unlock();
  105. }
  106. void __mmu_notifier_invalidate_range_start(struct mm_struct *mm,
  107. unsigned long start, unsigned long end)
  108. {
  109. struct mmu_notifier *mn;
  110. struct hlist_node *n;
  111. rcu_read_lock();
  112. hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
  113. if (mn->ops->invalidate_range_start)
  114. mn->ops->invalidate_range_start(mn, mm, start, end);
  115. }
  116. rcu_read_unlock();
  117. }
  118. void __mmu_notifier_invalidate_range_end(struct mm_struct *mm,
  119. unsigned long start, unsigned long end)
  120. {
  121. struct mmu_notifier *mn;
  122. struct hlist_node *n;
  123. rcu_read_lock();
  124. hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
  125. if (mn->ops->invalidate_range_end)
  126. mn->ops->invalidate_range_end(mn, mm, start, end);
  127. }
  128. rcu_read_unlock();
  129. }
  130. static int do_mmu_notifier_register(struct mmu_notifier *mn,
  131. struct mm_struct *mm,
  132. int take_mmap_sem)
  133. {
  134. struct mmu_notifier_mm *mmu_notifier_mm;
  135. int ret;
  136. BUG_ON(atomic_read(&mm->mm_users) <= 0);
  137. ret = -ENOMEM;
  138. mmu_notifier_mm = kmalloc(sizeof(struct mmu_notifier_mm), GFP_KERNEL);
  139. if (unlikely(!mmu_notifier_mm))
  140. goto out;
  141. if (take_mmap_sem)
  142. down_write(&mm->mmap_sem);
  143. ret = mm_take_all_locks(mm);
  144. if (unlikely(ret))
  145. goto out_cleanup;
  146. if (!mm_has_notifiers(mm)) {
  147. INIT_HLIST_HEAD(&mmu_notifier_mm->list);
  148. spin_lock_init(&mmu_notifier_mm->lock);
  149. mm->mmu_notifier_mm = mmu_notifier_mm;
  150. mmu_notifier_mm = NULL;
  151. }
  152. atomic_inc(&mm->mm_count);
  153. /*
  154. * Serialize the update against mmu_notifier_unregister. A
  155. * side note: mmu_notifier_release can't run concurrently with
  156. * us because we hold the mm_users pin (either implicitly as
  157. * current->mm or explicitly with get_task_mm() or similar).
  158. * We can't race against any other mmu notifier method either
  159. * thanks to mm_take_all_locks().
  160. */
  161. spin_lock(&mm->mmu_notifier_mm->lock);
  162. hlist_add_head(&mn->hlist, &mm->mmu_notifier_mm->list);
  163. spin_unlock(&mm->mmu_notifier_mm->lock);
  164. mm_drop_all_locks(mm);
  165. out_cleanup:
  166. if (take_mmap_sem)
  167. up_write(&mm->mmap_sem);
  168. /* kfree() does nothing if mmu_notifier_mm is NULL */
  169. kfree(mmu_notifier_mm);
  170. out:
  171. BUG_ON(atomic_read(&mm->mm_users) <= 0);
  172. return ret;
  173. }
  174. /*
  175. * Must not hold mmap_sem nor any other VM related lock when calling
  176. * this registration function. Must also ensure mm_users can't go down
  177. * to zero while this runs to avoid races with mmu_notifier_release,
  178. * so mm has to be current->mm or the mm should be pinned safely such
  179. * as with get_task_mm(). If the mm is not current->mm, the mm_users
  180. * pin should be released by calling mmput after mmu_notifier_register
  181. * returns. mmu_notifier_unregister must be always called to
  182. * unregister the notifier. mm_count is automatically pinned to allow
  183. * mmu_notifier_unregister to safely run at any time later, before or
  184. * after exit_mmap. ->release will always be called before exit_mmap
  185. * frees the pages.
  186. */
  187. int mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)
  188. {
  189. return do_mmu_notifier_register(mn, mm, 1);
  190. }
  191. EXPORT_SYMBOL_GPL(mmu_notifier_register);
  192. /*
  193. * Same as mmu_notifier_register but here the caller must hold the
  194. * mmap_sem in write mode.
  195. */
  196. int __mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)
  197. {
  198. return do_mmu_notifier_register(mn, mm, 0);
  199. }
  200. EXPORT_SYMBOL_GPL(__mmu_notifier_register);
  201. /* this is called after the last mmu_notifier_unregister() returned */
  202. void __mmu_notifier_mm_destroy(struct mm_struct *mm)
  203. {
  204. BUG_ON(!hlist_empty(&mm->mmu_notifier_mm->list));
  205. kfree(mm->mmu_notifier_mm);
  206. mm->mmu_notifier_mm = LIST_POISON1; /* debug */
  207. }
  208. /*
  209. * This releases the mm_count pin automatically and frees the mm
  210. * structure if it was the last user of it. It serializes against
  211. * running mmu notifiers with RCU and against mmu_notifier_unregister
  212. * with the unregister lock + RCU. All sptes must be dropped before
  213. * calling mmu_notifier_unregister. ->release or any other notifier
  214. * method may be invoked concurrently with mmu_notifier_unregister,
  215. * and only after mmu_notifier_unregister returned we're guaranteed
  216. * that ->release or any other method can't run anymore.
  217. */
  218. void mmu_notifier_unregister(struct mmu_notifier *mn, struct mm_struct *mm)
  219. {
  220. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  221. spin_lock(&mm->mmu_notifier_mm->lock);
  222. if (!hlist_unhashed(&mn->hlist)) {
  223. hlist_del_rcu(&mn->hlist);
  224. /*
  225. * RCU here will force exit_mmap to wait ->release to finish
  226. * before freeing the pages.
  227. */
  228. rcu_read_lock();
  229. spin_unlock(&mm->mmu_notifier_mm->lock);
  230. /*
  231. * exit_mmap will block in mmu_notifier_release to
  232. * guarantee ->release is called before freeing the
  233. * pages.
  234. */
  235. if (mn->ops->release)
  236. mn->ops->release(mn, mm);
  237. rcu_read_unlock();
  238. } else
  239. spin_unlock(&mm->mmu_notifier_mm->lock);
  240. /*
  241. * Wait any running method to finish, of course including
  242. * ->release if it was run by mmu_notifier_relase instead of us.
  243. */
  244. synchronize_rcu();
  245. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  246. mmdrop(mm);
  247. }
  248. EXPORT_SYMBOL_GPL(mmu_notifier_unregister);