mmu_notifier.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. int __mmu_notifier_test_young(struct mm_struct *mm,
  96. unsigned long address)
  97. {
  98. struct mmu_notifier *mn;
  99. struct hlist_node *n;
  100. int young = 0;
  101. rcu_read_lock();
  102. hlist_for_each_entry_rcu(mn, n, &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. rcu_read_unlock();
  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. 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->change_pte)
  120. mn->ops->change_pte(mn, mm, address, pte);
  121. /*
  122. * Some drivers don't have change_pte,
  123. * so we must call invalidate_page in that case.
  124. */
  125. else if (mn->ops->invalidate_page)
  126. mn->ops->invalidate_page(mn, mm, address);
  127. }
  128. rcu_read_unlock();
  129. }
  130. void __mmu_notifier_invalidate_page(struct mm_struct *mm,
  131. unsigned long address)
  132. {
  133. struct mmu_notifier *mn;
  134. struct hlist_node *n;
  135. rcu_read_lock();
  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. rcu_read_unlock();
  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. rcu_read_lock();
  148. hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
  149. if (mn->ops->invalidate_range_start)
  150. mn->ops->invalidate_range_start(mn, mm, start, end);
  151. }
  152. rcu_read_unlock();
  153. }
  154. void __mmu_notifier_invalidate_range_end(struct mm_struct *mm,
  155. unsigned long start, unsigned long end)
  156. {
  157. struct mmu_notifier *mn;
  158. struct hlist_node *n;
  159. rcu_read_lock();
  160. hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list, hlist) {
  161. if (mn->ops->invalidate_range_end)
  162. mn->ops->invalidate_range_end(mn, mm, start, end);
  163. }
  164. rcu_read_unlock();
  165. }
  166. static int do_mmu_notifier_register(struct mmu_notifier *mn,
  167. struct mm_struct *mm,
  168. int take_mmap_sem)
  169. {
  170. struct mmu_notifier_mm *mmu_notifier_mm;
  171. int ret;
  172. BUG_ON(atomic_read(&mm->mm_users) <= 0);
  173. ret = -ENOMEM;
  174. mmu_notifier_mm = kmalloc(sizeof(struct mmu_notifier_mm), GFP_KERNEL);
  175. if (unlikely(!mmu_notifier_mm))
  176. goto out;
  177. if (take_mmap_sem)
  178. down_write(&mm->mmap_sem);
  179. ret = mm_take_all_locks(mm);
  180. if (unlikely(ret))
  181. goto out_cleanup;
  182. if (!mm_has_notifiers(mm)) {
  183. INIT_HLIST_HEAD(&mmu_notifier_mm->list);
  184. spin_lock_init(&mmu_notifier_mm->lock);
  185. mm->mmu_notifier_mm = mmu_notifier_mm;
  186. mmu_notifier_mm = NULL;
  187. }
  188. atomic_inc(&mm->mm_count);
  189. /*
  190. * Serialize the update against mmu_notifier_unregister. A
  191. * side note: mmu_notifier_release can't run concurrently with
  192. * us because we hold the mm_users pin (either implicitly as
  193. * current->mm or explicitly with get_task_mm() or similar).
  194. * We can't race against any other mmu notifier method either
  195. * thanks to mm_take_all_locks().
  196. */
  197. spin_lock(&mm->mmu_notifier_mm->lock);
  198. hlist_add_head(&mn->hlist, &mm->mmu_notifier_mm->list);
  199. spin_unlock(&mm->mmu_notifier_mm->lock);
  200. mm_drop_all_locks(mm);
  201. out_cleanup:
  202. if (take_mmap_sem)
  203. up_write(&mm->mmap_sem);
  204. /* kfree() does nothing if mmu_notifier_mm is NULL */
  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 RCU and against mmu_notifier_unregister
  248. * with the unregister lock + RCU. 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. spin_lock(&mm->mmu_notifier_mm->lock);
  258. if (!hlist_unhashed(&mn->hlist)) {
  259. hlist_del_rcu(&mn->hlist);
  260. /*
  261. * RCU here will force exit_mmap to wait ->release to finish
  262. * before freeing the pages.
  263. */
  264. rcu_read_lock();
  265. spin_unlock(&mm->mmu_notifier_mm->lock);
  266. /*
  267. * exit_mmap will block in mmu_notifier_release to
  268. * guarantee ->release is called before freeing the
  269. * pages.
  270. */
  271. if (mn->ops->release)
  272. mn->ops->release(mn, mm);
  273. rcu_read_unlock();
  274. } else
  275. spin_unlock(&mm->mmu_notifier_mm->lock);
  276. /*
  277. * Wait any running method to finish, of course including
  278. * ->release if it was run by mmu_notifier_relase instead of us.
  279. */
  280. synchronize_rcu();
  281. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  282. mmdrop(mm);
  283. }
  284. EXPORT_SYMBOL_GPL(mmu_notifier_unregister);