mmu_notifier.c 11 KB

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