mmu_notifier.c 9.8 KB

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