mmu_notifier.c 8.8 KB

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