mmu_notifier.c 11 KB

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