mmu_notifier.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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_change_pte(struct mm_struct *mm, unsigned long address,
  95. pte_t pte)
  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->change_pte)
  102. mn->ops->change_pte(mn, mm, address, pte);
  103. /*
  104. * Some drivers don't have change_pte,
  105. * so we must call invalidate_page in that case.
  106. */
  107. else if (mn->ops->invalidate_page)
  108. mn->ops->invalidate_page(mn, mm, address);
  109. }
  110. rcu_read_unlock();
  111. }
  112. void __mmu_notifier_invalidate_page(struct mm_struct *mm,
  113. unsigned long address)
  114. {
  115. struct mmu_notifier *mn;
  116. struct hlist_node *n;
  117. rcu_read_lock();
  118. hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
  119. if (mn->ops->invalidate_page)
  120. mn->ops->invalidate_page(mn, mm, address);
  121. }
  122. rcu_read_unlock();
  123. }
  124. void __mmu_notifier_invalidate_range_start(struct mm_struct *mm,
  125. unsigned long start, unsigned long end)
  126. {
  127. struct mmu_notifier *mn;
  128. struct hlist_node *n;
  129. rcu_read_lock();
  130. hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
  131. if (mn->ops->invalidate_range_start)
  132. mn->ops->invalidate_range_start(mn, mm, start, end);
  133. }
  134. rcu_read_unlock();
  135. }
  136. void __mmu_notifier_invalidate_range_end(struct mm_struct *mm,
  137. unsigned long start, unsigned long end)
  138. {
  139. struct mmu_notifier *mn;
  140. struct hlist_node *n;
  141. rcu_read_lock();
  142. hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
  143. if (mn->ops->invalidate_range_end)
  144. mn->ops->invalidate_range_end(mn, mm, start, end);
  145. }
  146. rcu_read_unlock();
  147. }
  148. static int do_mmu_notifier_register(struct mmu_notifier *mn,
  149. struct mm_struct *mm,
  150. int take_mmap_sem)
  151. {
  152. struct mmu_notifier_mm *mmu_notifier_mm;
  153. int ret;
  154. BUG_ON(atomic_read(&mm->mm_users) <= 0);
  155. ret = -ENOMEM;
  156. mmu_notifier_mm = kmalloc(sizeof(struct mmu_notifier_mm), GFP_KERNEL);
  157. if (unlikely(!mmu_notifier_mm))
  158. goto out;
  159. if (take_mmap_sem)
  160. down_write(&mm->mmap_sem);
  161. ret = mm_take_all_locks(mm);
  162. if (unlikely(ret))
  163. goto out_cleanup;
  164. if (!mm_has_notifiers(mm)) {
  165. INIT_HLIST_HEAD(&mmu_notifier_mm->list);
  166. spin_lock_init(&mmu_notifier_mm->lock);
  167. mm->mmu_notifier_mm = mmu_notifier_mm;
  168. mmu_notifier_mm = NULL;
  169. }
  170. atomic_inc(&mm->mm_count);
  171. /*
  172. * Serialize the update against mmu_notifier_unregister. A
  173. * side note: mmu_notifier_release can't run concurrently with
  174. * us because we hold the mm_users pin (either implicitly as
  175. * current->mm or explicitly with get_task_mm() or similar).
  176. * We can't race against any other mmu notifier method either
  177. * thanks to mm_take_all_locks().
  178. */
  179. spin_lock(&mm->mmu_notifier_mm->lock);
  180. hlist_add_head(&mn->hlist, &mm->mmu_notifier_mm->list);
  181. spin_unlock(&mm->mmu_notifier_mm->lock);
  182. mm_drop_all_locks(mm);
  183. out_cleanup:
  184. if (take_mmap_sem)
  185. up_write(&mm->mmap_sem);
  186. /* kfree() does nothing if mmu_notifier_mm is NULL */
  187. kfree(mmu_notifier_mm);
  188. out:
  189. BUG_ON(atomic_read(&mm->mm_users) <= 0);
  190. return ret;
  191. }
  192. /*
  193. * Must not hold mmap_sem nor any other VM related lock when calling
  194. * this registration function. Must also ensure mm_users can't go down
  195. * to zero while this runs to avoid races with mmu_notifier_release,
  196. * so mm has to be current->mm or the mm should be pinned safely such
  197. * as with get_task_mm(). If the mm is not current->mm, the mm_users
  198. * pin should be released by calling mmput after mmu_notifier_register
  199. * returns. mmu_notifier_unregister must be always called to
  200. * unregister the notifier. mm_count is automatically pinned to allow
  201. * mmu_notifier_unregister to safely run at any time later, before or
  202. * after exit_mmap. ->release will always be called before exit_mmap
  203. * frees the pages.
  204. */
  205. int mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)
  206. {
  207. return do_mmu_notifier_register(mn, mm, 1);
  208. }
  209. EXPORT_SYMBOL_GPL(mmu_notifier_register);
  210. /*
  211. * Same as mmu_notifier_register but here the caller must hold the
  212. * mmap_sem in write mode.
  213. */
  214. int __mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)
  215. {
  216. return do_mmu_notifier_register(mn, mm, 0);
  217. }
  218. EXPORT_SYMBOL_GPL(__mmu_notifier_register);
  219. /* this is called after the last mmu_notifier_unregister() returned */
  220. void __mmu_notifier_mm_destroy(struct mm_struct *mm)
  221. {
  222. BUG_ON(!hlist_empty(&mm->mmu_notifier_mm->list));
  223. kfree(mm->mmu_notifier_mm);
  224. mm->mmu_notifier_mm = LIST_POISON1; /* debug */
  225. }
  226. /*
  227. * This releases the mm_count pin automatically and frees the mm
  228. * structure if it was the last user of it. It serializes against
  229. * running mmu notifiers with RCU and against mmu_notifier_unregister
  230. * with the unregister lock + RCU. All sptes must be dropped before
  231. * calling mmu_notifier_unregister. ->release or any other notifier
  232. * method may be invoked concurrently with mmu_notifier_unregister,
  233. * and only after mmu_notifier_unregister returned we're guaranteed
  234. * that ->release or any other method can't run anymore.
  235. */
  236. void mmu_notifier_unregister(struct mmu_notifier *mn, struct mm_struct *mm)
  237. {
  238. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  239. spin_lock(&mm->mmu_notifier_mm->lock);
  240. if (!hlist_unhashed(&mn->hlist)) {
  241. hlist_del_rcu(&mn->hlist);
  242. /*
  243. * RCU here will force exit_mmap to wait ->release to finish
  244. * before freeing the pages.
  245. */
  246. rcu_read_lock();
  247. spin_unlock(&mm->mmu_notifier_mm->lock);
  248. /*
  249. * exit_mmap will block in mmu_notifier_release to
  250. * guarantee ->release is called before freeing the
  251. * pages.
  252. */
  253. if (mn->ops->release)
  254. mn->ops->release(mn, mm);
  255. rcu_read_unlock();
  256. } else
  257. spin_unlock(&mm->mmu_notifier_mm->lock);
  258. /*
  259. * Wait any running method to finish, of course including
  260. * ->release if it was run by mmu_notifier_relase instead of us.
  261. */
  262. synchronize_rcu();
  263. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  264. mmdrop(mm);
  265. }
  266. EXPORT_SYMBOL_GPL(mmu_notifier_unregister);