mmu_notifier.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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_page(struct mm_struct *mm,
  158. unsigned long address)
  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_page)
  165. mn->ops->invalidate_page(mn, mm, address);
  166. }
  167. srcu_read_unlock(&srcu, id);
  168. }
  169. void __mmu_notifier_invalidate_range_start(struct mm_struct *mm,
  170. unsigned long start, unsigned long end)
  171. {
  172. struct mmu_notifier *mn;
  173. int id;
  174. id = srcu_read_lock(&srcu);
  175. hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
  176. if (mn->ops->invalidate_range_start)
  177. mn->ops->invalidate_range_start(mn, mm, start, end);
  178. }
  179. srcu_read_unlock(&srcu, id);
  180. }
  181. EXPORT_SYMBOL_GPL(__mmu_notifier_invalidate_range_start);
  182. void __mmu_notifier_invalidate_range_end(struct mm_struct *mm,
  183. unsigned long start, unsigned long end)
  184. {
  185. struct mmu_notifier *mn;
  186. int id;
  187. id = srcu_read_lock(&srcu);
  188. hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
  189. /*
  190. * Call invalidate_range here too to avoid the need for the
  191. * subsystem of having to register an invalidate_range_end
  192. * call-back when there is invalidate_range already. Usually a
  193. * subsystem registers either invalidate_range_start()/end() or
  194. * invalidate_range(), so this will be no additional overhead
  195. * (besides the pointer check).
  196. */
  197. if (mn->ops->invalidate_range)
  198. mn->ops->invalidate_range(mn, mm, start, end);
  199. if (mn->ops->invalidate_range_end)
  200. mn->ops->invalidate_range_end(mn, mm, start, end);
  201. }
  202. srcu_read_unlock(&srcu, id);
  203. }
  204. EXPORT_SYMBOL_GPL(__mmu_notifier_invalidate_range_end);
  205. void __mmu_notifier_invalidate_range(struct mm_struct *mm,
  206. unsigned long start, unsigned long end)
  207. {
  208. struct mmu_notifier *mn;
  209. int id;
  210. id = srcu_read_lock(&srcu);
  211. hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
  212. if (mn->ops->invalidate_range)
  213. mn->ops->invalidate_range(mn, mm, start, end);
  214. }
  215. srcu_read_unlock(&srcu, id);
  216. }
  217. EXPORT_SYMBOL_GPL(__mmu_notifier_invalidate_range);
  218. static int do_mmu_notifier_register(struct mmu_notifier *mn,
  219. struct mm_struct *mm,
  220. int take_mmap_sem)
  221. {
  222. struct mmu_notifier_mm *mmu_notifier_mm;
  223. int ret;
  224. BUG_ON(atomic_read(&mm->mm_users) <= 0);
  225. ret = -ENOMEM;
  226. mmu_notifier_mm = kmalloc(sizeof(struct mmu_notifier_mm), GFP_KERNEL);
  227. if (unlikely(!mmu_notifier_mm))
  228. goto out;
  229. if (take_mmap_sem)
  230. down_write(&mm->mmap_sem);
  231. ret = mm_take_all_locks(mm);
  232. if (unlikely(ret))
  233. goto out_clean;
  234. if (!mm_has_notifiers(mm)) {
  235. INIT_HLIST_HEAD(&mmu_notifier_mm->list);
  236. spin_lock_init(&mmu_notifier_mm->lock);
  237. mm->mmu_notifier_mm = mmu_notifier_mm;
  238. mmu_notifier_mm = NULL;
  239. }
  240. mmgrab(mm);
  241. /*
  242. * Serialize the update against mmu_notifier_unregister. A
  243. * side note: mmu_notifier_release can't run concurrently with
  244. * us because we hold the mm_users pin (either implicitly as
  245. * current->mm or explicitly with get_task_mm() or similar).
  246. * We can't race against any other mmu notifier method either
  247. * thanks to mm_take_all_locks().
  248. */
  249. spin_lock(&mm->mmu_notifier_mm->lock);
  250. hlist_add_head(&mn->hlist, &mm->mmu_notifier_mm->list);
  251. spin_unlock(&mm->mmu_notifier_mm->lock);
  252. mm_drop_all_locks(mm);
  253. out_clean:
  254. if (take_mmap_sem)
  255. up_write(&mm->mmap_sem);
  256. kfree(mmu_notifier_mm);
  257. out:
  258. BUG_ON(atomic_read(&mm->mm_users) <= 0);
  259. return ret;
  260. }
  261. /*
  262. * Must not hold mmap_sem nor any other VM related lock when calling
  263. * this registration function. Must also ensure mm_users can't go down
  264. * to zero while this runs to avoid races with mmu_notifier_release,
  265. * so mm has to be current->mm or the mm should be pinned safely such
  266. * as with get_task_mm(). If the mm is not current->mm, the mm_users
  267. * pin should be released by calling mmput after mmu_notifier_register
  268. * returns. mmu_notifier_unregister must be always called to
  269. * unregister the notifier. mm_count is automatically pinned to allow
  270. * mmu_notifier_unregister to safely run at any time later, before or
  271. * after exit_mmap. ->release will always be called before exit_mmap
  272. * frees the pages.
  273. */
  274. int mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)
  275. {
  276. return do_mmu_notifier_register(mn, mm, 1);
  277. }
  278. EXPORT_SYMBOL_GPL(mmu_notifier_register);
  279. /*
  280. * Same as mmu_notifier_register but here the caller must hold the
  281. * mmap_sem in write mode.
  282. */
  283. int __mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)
  284. {
  285. return do_mmu_notifier_register(mn, mm, 0);
  286. }
  287. EXPORT_SYMBOL_GPL(__mmu_notifier_register);
  288. /* this is called after the last mmu_notifier_unregister() returned */
  289. void __mmu_notifier_mm_destroy(struct mm_struct *mm)
  290. {
  291. BUG_ON(!hlist_empty(&mm->mmu_notifier_mm->list));
  292. kfree(mm->mmu_notifier_mm);
  293. mm->mmu_notifier_mm = LIST_POISON1; /* debug */
  294. }
  295. /*
  296. * This releases the mm_count pin automatically and frees the mm
  297. * structure if it was the last user of it. It serializes against
  298. * running mmu notifiers with SRCU and against mmu_notifier_unregister
  299. * with the unregister lock + SRCU. All sptes must be dropped before
  300. * calling mmu_notifier_unregister. ->release or any other notifier
  301. * method may be invoked concurrently with mmu_notifier_unregister,
  302. * and only after mmu_notifier_unregister returned we're guaranteed
  303. * that ->release or any other method can't run anymore.
  304. */
  305. void mmu_notifier_unregister(struct mmu_notifier *mn, struct mm_struct *mm)
  306. {
  307. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  308. if (!hlist_unhashed(&mn->hlist)) {
  309. /*
  310. * SRCU here will force exit_mmap to wait for ->release to
  311. * finish before freeing the pages.
  312. */
  313. int id;
  314. id = srcu_read_lock(&srcu);
  315. /*
  316. * exit_mmap will block in mmu_notifier_release to guarantee
  317. * that ->release is called before freeing the pages.
  318. */
  319. if (mn->ops->release)
  320. mn->ops->release(mn, mm);
  321. srcu_read_unlock(&srcu, id);
  322. spin_lock(&mm->mmu_notifier_mm->lock);
  323. /*
  324. * Can not use list_del_rcu() since __mmu_notifier_release
  325. * can delete it before we hold the lock.
  326. */
  327. hlist_del_init_rcu(&mn->hlist);
  328. spin_unlock(&mm->mmu_notifier_mm->lock);
  329. }
  330. /*
  331. * Wait for any running method to finish, of course including
  332. * ->release if it was run by mmu_notifier_release instead of us.
  333. */
  334. synchronize_srcu(&srcu);
  335. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  336. mmdrop(mm);
  337. }
  338. EXPORT_SYMBOL_GPL(mmu_notifier_unregister);
  339. /*
  340. * Same as mmu_notifier_unregister but no callback and no srcu synchronization.
  341. */
  342. void mmu_notifier_unregister_no_release(struct mmu_notifier *mn,
  343. struct mm_struct *mm)
  344. {
  345. spin_lock(&mm->mmu_notifier_mm->lock);
  346. /*
  347. * Can not use list_del_rcu() since __mmu_notifier_release
  348. * can delete it before we hold the lock.
  349. */
  350. hlist_del_init_rcu(&mn->hlist);
  351. spin_unlock(&mm->mmu_notifier_mm->lock);
  352. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  353. mmdrop(mm);
  354. }
  355. EXPORT_SYMBOL_GPL(mmu_notifier_unregister_no_release);