mmu_notifier.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 start,
  102. unsigned long end)
  103. {
  104. struct mmu_notifier *mn;
  105. int young = 0, id;
  106. id = srcu_read_lock(&srcu);
  107. hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
  108. if (mn->ops->clear_flush_young)
  109. young |= mn->ops->clear_flush_young(mn, mm, start, end);
  110. }
  111. srcu_read_unlock(&srcu, id);
  112. return young;
  113. }
  114. int __mmu_notifier_test_young(struct mm_struct *mm,
  115. unsigned long address)
  116. {
  117. struct mmu_notifier *mn;
  118. int young = 0, id;
  119. id = srcu_read_lock(&srcu);
  120. hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
  121. if (mn->ops->test_young) {
  122. young = mn->ops->test_young(mn, mm, address);
  123. if (young)
  124. break;
  125. }
  126. }
  127. srcu_read_unlock(&srcu, id);
  128. return young;
  129. }
  130. void __mmu_notifier_change_pte(struct mm_struct *mm, unsigned long address,
  131. pte_t pte)
  132. {
  133. struct mmu_notifier *mn;
  134. int id;
  135. id = srcu_read_lock(&srcu);
  136. hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
  137. if (mn->ops->change_pte)
  138. mn->ops->change_pte(mn, mm, address, pte);
  139. }
  140. srcu_read_unlock(&srcu, id);
  141. }
  142. void __mmu_notifier_invalidate_page(struct mm_struct *mm,
  143. unsigned long address)
  144. {
  145. struct mmu_notifier *mn;
  146. int id;
  147. id = srcu_read_lock(&srcu);
  148. hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
  149. if (mn->ops->invalidate_page)
  150. mn->ops->invalidate_page(mn, mm, address);
  151. }
  152. srcu_read_unlock(&srcu, id);
  153. }
  154. void __mmu_notifier_invalidate_range_start(struct mm_struct *mm,
  155. unsigned long start, unsigned long end)
  156. {
  157. struct mmu_notifier *mn;
  158. int id;
  159. id = srcu_read_lock(&srcu);
  160. hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
  161. if (mn->ops->invalidate_range_start)
  162. mn->ops->invalidate_range_start(mn, mm, start, end);
  163. }
  164. srcu_read_unlock(&srcu, id);
  165. }
  166. EXPORT_SYMBOL_GPL(__mmu_notifier_invalidate_range_start);
  167. void __mmu_notifier_invalidate_range_end(struct mm_struct *mm,
  168. unsigned long start, unsigned long end)
  169. {
  170. struct mmu_notifier *mn;
  171. int id;
  172. id = srcu_read_lock(&srcu);
  173. hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
  174. if (mn->ops->invalidate_range_end)
  175. mn->ops->invalidate_range_end(mn, mm, start, end);
  176. }
  177. srcu_read_unlock(&srcu, id);
  178. }
  179. EXPORT_SYMBOL_GPL(__mmu_notifier_invalidate_range_end);
  180. static int do_mmu_notifier_register(struct mmu_notifier *mn,
  181. struct mm_struct *mm,
  182. int take_mmap_sem)
  183. {
  184. struct mmu_notifier_mm *mmu_notifier_mm;
  185. int ret;
  186. BUG_ON(atomic_read(&mm->mm_users) <= 0);
  187. /*
  188. * Verify that mmu_notifier_init() already run and the global srcu is
  189. * initialized.
  190. */
  191. BUG_ON(!srcu.per_cpu_ref);
  192. ret = -ENOMEM;
  193. mmu_notifier_mm = kmalloc(sizeof(struct mmu_notifier_mm), GFP_KERNEL);
  194. if (unlikely(!mmu_notifier_mm))
  195. goto out;
  196. if (take_mmap_sem)
  197. down_write(&mm->mmap_sem);
  198. ret = mm_take_all_locks(mm);
  199. if (unlikely(ret))
  200. goto out_clean;
  201. if (!mm_has_notifiers(mm)) {
  202. INIT_HLIST_HEAD(&mmu_notifier_mm->list);
  203. spin_lock_init(&mmu_notifier_mm->lock);
  204. mm->mmu_notifier_mm = mmu_notifier_mm;
  205. mmu_notifier_mm = NULL;
  206. }
  207. atomic_inc(&mm->mm_count);
  208. /*
  209. * Serialize the update against mmu_notifier_unregister. A
  210. * side note: mmu_notifier_release can't run concurrently with
  211. * us because we hold the mm_users pin (either implicitly as
  212. * current->mm or explicitly with get_task_mm() or similar).
  213. * We can't race against any other mmu notifier method either
  214. * thanks to mm_take_all_locks().
  215. */
  216. spin_lock(&mm->mmu_notifier_mm->lock);
  217. hlist_add_head(&mn->hlist, &mm->mmu_notifier_mm->list);
  218. spin_unlock(&mm->mmu_notifier_mm->lock);
  219. mm_drop_all_locks(mm);
  220. out_clean:
  221. if (take_mmap_sem)
  222. up_write(&mm->mmap_sem);
  223. kfree(mmu_notifier_mm);
  224. out:
  225. BUG_ON(atomic_read(&mm->mm_users) <= 0);
  226. return ret;
  227. }
  228. /*
  229. * Must not hold mmap_sem nor any other VM related lock when calling
  230. * this registration function. Must also ensure mm_users can't go down
  231. * to zero while this runs to avoid races with mmu_notifier_release,
  232. * so mm has to be current->mm or the mm should be pinned safely such
  233. * as with get_task_mm(). If the mm is not current->mm, the mm_users
  234. * pin should be released by calling mmput after mmu_notifier_register
  235. * returns. mmu_notifier_unregister must be always called to
  236. * unregister the notifier. mm_count is automatically pinned to allow
  237. * mmu_notifier_unregister to safely run at any time later, before or
  238. * after exit_mmap. ->release will always be called before exit_mmap
  239. * frees the pages.
  240. */
  241. int mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)
  242. {
  243. return do_mmu_notifier_register(mn, mm, 1);
  244. }
  245. EXPORT_SYMBOL_GPL(mmu_notifier_register);
  246. /*
  247. * Same as mmu_notifier_register but here the caller must hold the
  248. * mmap_sem in write mode.
  249. */
  250. int __mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)
  251. {
  252. return do_mmu_notifier_register(mn, mm, 0);
  253. }
  254. EXPORT_SYMBOL_GPL(__mmu_notifier_register);
  255. /* this is called after the last mmu_notifier_unregister() returned */
  256. void __mmu_notifier_mm_destroy(struct mm_struct *mm)
  257. {
  258. BUG_ON(!hlist_empty(&mm->mmu_notifier_mm->list));
  259. kfree(mm->mmu_notifier_mm);
  260. mm->mmu_notifier_mm = LIST_POISON1; /* debug */
  261. }
  262. /*
  263. * This releases the mm_count pin automatically and frees the mm
  264. * structure if it was the last user of it. It serializes against
  265. * running mmu notifiers with SRCU and against mmu_notifier_unregister
  266. * with the unregister lock + SRCU. All sptes must be dropped before
  267. * calling mmu_notifier_unregister. ->release or any other notifier
  268. * method may be invoked concurrently with mmu_notifier_unregister,
  269. * and only after mmu_notifier_unregister returned we're guaranteed
  270. * that ->release or any other method can't run anymore.
  271. */
  272. void mmu_notifier_unregister(struct mmu_notifier *mn, struct mm_struct *mm)
  273. {
  274. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  275. if (!hlist_unhashed(&mn->hlist)) {
  276. /*
  277. * SRCU here will force exit_mmap to wait for ->release to
  278. * finish before freeing the pages.
  279. */
  280. int id;
  281. id = srcu_read_lock(&srcu);
  282. /*
  283. * exit_mmap will block in mmu_notifier_release to guarantee
  284. * that ->release is called before freeing the pages.
  285. */
  286. if (mn->ops->release)
  287. mn->ops->release(mn, mm);
  288. srcu_read_unlock(&srcu, id);
  289. spin_lock(&mm->mmu_notifier_mm->lock);
  290. /*
  291. * Can not use list_del_rcu() since __mmu_notifier_release
  292. * can delete it before we hold the lock.
  293. */
  294. hlist_del_init_rcu(&mn->hlist);
  295. spin_unlock(&mm->mmu_notifier_mm->lock);
  296. }
  297. /*
  298. * Wait for any running method to finish, of course including
  299. * ->release if it was run by mmu_notifier_release instead of us.
  300. */
  301. synchronize_srcu(&srcu);
  302. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  303. mmdrop(mm);
  304. }
  305. EXPORT_SYMBOL_GPL(mmu_notifier_unregister);
  306. /*
  307. * Same as mmu_notifier_unregister but no callback and no srcu synchronization.
  308. */
  309. void mmu_notifier_unregister_no_release(struct mmu_notifier *mn,
  310. struct mm_struct *mm)
  311. {
  312. spin_lock(&mm->mmu_notifier_mm->lock);
  313. /*
  314. * Can not use list_del_rcu() since __mmu_notifier_release
  315. * can delete it before we hold the lock.
  316. */
  317. hlist_del_init_rcu(&mn->hlist);
  318. spin_unlock(&mm->mmu_notifier_mm->lock);
  319. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  320. mmdrop(mm);
  321. }
  322. EXPORT_SYMBOL_GPL(mmu_notifier_unregister_no_release);
  323. static int __init mmu_notifier_init(void)
  324. {
  325. return init_srcu_struct(&srcu);
  326. }
  327. subsys_initcall(mmu_notifier_init);