mmu_notifier.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. int __mmu_notifier_invalidate_range_start(struct mm_struct *mm,
  158. unsigned long start, unsigned long end,
  159. bool blockable)
  160. {
  161. struct mmu_notifier *mn;
  162. int ret = 0;
  163. int id;
  164. id = srcu_read_lock(&srcu);
  165. hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
  166. if (mn->ops->invalidate_range_start) {
  167. int _ret = mn->ops->invalidate_range_start(mn, mm, start, end, blockable);
  168. if (_ret) {
  169. pr_info("%pS callback failed with %d in %sblockable context.\n",
  170. mn->ops->invalidate_range_start, _ret,
  171. !blockable ? "non-" : "");
  172. ret = _ret;
  173. }
  174. }
  175. }
  176. srcu_read_unlock(&srcu, id);
  177. return ret;
  178. }
  179. EXPORT_SYMBOL_GPL(__mmu_notifier_invalidate_range_start);
  180. void __mmu_notifier_invalidate_range_end(struct mm_struct *mm,
  181. unsigned long start,
  182. unsigned long end,
  183. bool only_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. * We skip call to invalidate_range() if we know it is safe ie
  198. * call site use mmu_notifier_invalidate_range_only_end() which
  199. * is safe to do when we know that a call to invalidate_range()
  200. * already happen under page table lock.
  201. */
  202. if (!only_end && mn->ops->invalidate_range)
  203. mn->ops->invalidate_range(mn, mm, start, end);
  204. if (mn->ops->invalidate_range_end)
  205. mn->ops->invalidate_range_end(mn, mm, start, end);
  206. }
  207. srcu_read_unlock(&srcu, id);
  208. }
  209. EXPORT_SYMBOL_GPL(__mmu_notifier_invalidate_range_end);
  210. void __mmu_notifier_invalidate_range(struct mm_struct *mm,
  211. unsigned long start, unsigned long end)
  212. {
  213. struct mmu_notifier *mn;
  214. int id;
  215. id = srcu_read_lock(&srcu);
  216. hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
  217. if (mn->ops->invalidate_range)
  218. mn->ops->invalidate_range(mn, mm, start, end);
  219. }
  220. srcu_read_unlock(&srcu, id);
  221. }
  222. EXPORT_SYMBOL_GPL(__mmu_notifier_invalidate_range);
  223. /*
  224. * Must be called while holding mm->mmap_sem for either read or write.
  225. * The result is guaranteed to be valid until mm->mmap_sem is dropped.
  226. */
  227. bool mm_has_blockable_invalidate_notifiers(struct mm_struct *mm)
  228. {
  229. struct mmu_notifier *mn;
  230. int id;
  231. bool ret = false;
  232. WARN_ON_ONCE(!rwsem_is_locked(&mm->mmap_sem));
  233. if (!mm_has_notifiers(mm))
  234. return ret;
  235. id = srcu_read_lock(&srcu);
  236. hlist_for_each_entry_rcu(mn, &mm->mmu_notifier_mm->list, hlist) {
  237. if (!mn->ops->invalidate_range &&
  238. !mn->ops->invalidate_range_start &&
  239. !mn->ops->invalidate_range_end)
  240. continue;
  241. if (!(mn->ops->flags & MMU_INVALIDATE_DOES_NOT_BLOCK)) {
  242. ret = true;
  243. break;
  244. }
  245. }
  246. srcu_read_unlock(&srcu, id);
  247. return ret;
  248. }
  249. static int do_mmu_notifier_register(struct mmu_notifier *mn,
  250. struct mm_struct *mm,
  251. int take_mmap_sem)
  252. {
  253. struct mmu_notifier_mm *mmu_notifier_mm;
  254. int ret;
  255. BUG_ON(atomic_read(&mm->mm_users) <= 0);
  256. ret = -ENOMEM;
  257. mmu_notifier_mm = kmalloc(sizeof(struct mmu_notifier_mm), GFP_KERNEL);
  258. if (unlikely(!mmu_notifier_mm))
  259. goto out;
  260. if (take_mmap_sem)
  261. down_write(&mm->mmap_sem);
  262. ret = mm_take_all_locks(mm);
  263. if (unlikely(ret))
  264. goto out_clean;
  265. if (!mm_has_notifiers(mm)) {
  266. INIT_HLIST_HEAD(&mmu_notifier_mm->list);
  267. spin_lock_init(&mmu_notifier_mm->lock);
  268. mm->mmu_notifier_mm = mmu_notifier_mm;
  269. mmu_notifier_mm = NULL;
  270. }
  271. mmgrab(mm);
  272. /*
  273. * Serialize the update against mmu_notifier_unregister. A
  274. * side note: mmu_notifier_release can't run concurrently with
  275. * us because we hold the mm_users pin (either implicitly as
  276. * current->mm or explicitly with get_task_mm() or similar).
  277. * We can't race against any other mmu notifier method either
  278. * thanks to mm_take_all_locks().
  279. */
  280. spin_lock(&mm->mmu_notifier_mm->lock);
  281. hlist_add_head_rcu(&mn->hlist, &mm->mmu_notifier_mm->list);
  282. spin_unlock(&mm->mmu_notifier_mm->lock);
  283. mm_drop_all_locks(mm);
  284. out_clean:
  285. if (take_mmap_sem)
  286. up_write(&mm->mmap_sem);
  287. kfree(mmu_notifier_mm);
  288. out:
  289. BUG_ON(atomic_read(&mm->mm_users) <= 0);
  290. return ret;
  291. }
  292. /*
  293. * Must not hold mmap_sem nor any other VM related lock when calling
  294. * this registration function. Must also ensure mm_users can't go down
  295. * to zero while this runs to avoid races with mmu_notifier_release,
  296. * so mm has to be current->mm or the mm should be pinned safely such
  297. * as with get_task_mm(). If the mm is not current->mm, the mm_users
  298. * pin should be released by calling mmput after mmu_notifier_register
  299. * returns. mmu_notifier_unregister must be always called to
  300. * unregister the notifier. mm_count is automatically pinned to allow
  301. * mmu_notifier_unregister to safely run at any time later, before or
  302. * after exit_mmap. ->release will always be called before exit_mmap
  303. * frees the pages.
  304. */
  305. int mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)
  306. {
  307. return do_mmu_notifier_register(mn, mm, 1);
  308. }
  309. EXPORT_SYMBOL_GPL(mmu_notifier_register);
  310. /*
  311. * Same as mmu_notifier_register but here the caller must hold the
  312. * mmap_sem in write mode.
  313. */
  314. int __mmu_notifier_register(struct mmu_notifier *mn, struct mm_struct *mm)
  315. {
  316. return do_mmu_notifier_register(mn, mm, 0);
  317. }
  318. EXPORT_SYMBOL_GPL(__mmu_notifier_register);
  319. /* this is called after the last mmu_notifier_unregister() returned */
  320. void __mmu_notifier_mm_destroy(struct mm_struct *mm)
  321. {
  322. BUG_ON(!hlist_empty(&mm->mmu_notifier_mm->list));
  323. kfree(mm->mmu_notifier_mm);
  324. mm->mmu_notifier_mm = LIST_POISON1; /* debug */
  325. }
  326. /*
  327. * This releases the mm_count pin automatically and frees the mm
  328. * structure if it was the last user of it. It serializes against
  329. * running mmu notifiers with SRCU and against mmu_notifier_unregister
  330. * with the unregister lock + SRCU. All sptes must be dropped before
  331. * calling mmu_notifier_unregister. ->release or any other notifier
  332. * method may be invoked concurrently with mmu_notifier_unregister,
  333. * and only after mmu_notifier_unregister returned we're guaranteed
  334. * that ->release or any other method can't run anymore.
  335. */
  336. void mmu_notifier_unregister(struct mmu_notifier *mn, struct mm_struct *mm)
  337. {
  338. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  339. if (!hlist_unhashed(&mn->hlist)) {
  340. /*
  341. * SRCU here will force exit_mmap to wait for ->release to
  342. * finish before freeing the pages.
  343. */
  344. int id;
  345. id = srcu_read_lock(&srcu);
  346. /*
  347. * exit_mmap will block in mmu_notifier_release to guarantee
  348. * that ->release is called before freeing the pages.
  349. */
  350. if (mn->ops->release)
  351. mn->ops->release(mn, mm);
  352. srcu_read_unlock(&srcu, id);
  353. spin_lock(&mm->mmu_notifier_mm->lock);
  354. /*
  355. * Can not use list_del_rcu() since __mmu_notifier_release
  356. * can delete it before we hold the lock.
  357. */
  358. hlist_del_init_rcu(&mn->hlist);
  359. spin_unlock(&mm->mmu_notifier_mm->lock);
  360. }
  361. /*
  362. * Wait for any running method to finish, of course including
  363. * ->release if it was run by mmu_notifier_release instead of us.
  364. */
  365. synchronize_srcu(&srcu);
  366. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  367. mmdrop(mm);
  368. }
  369. EXPORT_SYMBOL_GPL(mmu_notifier_unregister);
  370. /*
  371. * Same as mmu_notifier_unregister but no callback and no srcu synchronization.
  372. */
  373. void mmu_notifier_unregister_no_release(struct mmu_notifier *mn,
  374. struct mm_struct *mm)
  375. {
  376. spin_lock(&mm->mmu_notifier_mm->lock);
  377. /*
  378. * Can not use list_del_rcu() since __mmu_notifier_release
  379. * can delete it before we hold the lock.
  380. */
  381. hlist_del_init_rcu(&mn->hlist);
  382. spin_unlock(&mm->mmu_notifier_mm->lock);
  383. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  384. mmdrop(mm);
  385. }
  386. EXPORT_SYMBOL_GPL(mmu_notifier_unregister_no_release);