mmu_notifier.c 10 KB

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