mmu_notifier.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/mm/mmu_notifier.c
  4. *
  5. * Copyright (C) 2008 Qumranet, Inc.
  6. * Copyright (C) 2008 SGI
  7. * Christoph Lameter <cl@linux.com>
  8. */
  9. #include <linux/rculist.h>
  10. #include <linux/mmu_notifier.h>
  11. #include <linux/export.h>
  12. #include <linux/mm.h>
  13. #include <linux/err.h>
  14. #include <linux/interval_tree.h>
  15. #include <linux/srcu.h>
  16. #include <linux/rcupdate.h>
  17. #include <linux/sched.h>
  18. #include <linux/sched/mm.h>
  19. #include <linux/slab.h>
  20. #include "vma.h"
  21. /* global SRCU for all MMs */
  22. DEFINE_STATIC_SRCU(srcu);
  23. #ifdef CONFIG_LOCKDEP
  24. struct lockdep_map __mmu_notifier_invalidate_range_start_map = {
  25. .name = "mmu_notifier_invalidate_range_start"
  26. };
  27. #endif
  28. /*
  29. * The mmu_notifier_subscriptions structure is allocated and installed in
  30. * mm->notifier_subscriptions inside the mm_take_all_locks() protected
  31. * critical section and it's released only when mm_count reaches zero
  32. * in mmdrop().
  33. */
  34. struct mmu_notifier_subscriptions {
  35. /* all mmu notifiers registered in this mm are queued in this list */
  36. struct hlist_head list;
  37. bool has_itree;
  38. /* to serialize the list modifications and hlist_unhashed */
  39. spinlock_t lock;
  40. unsigned long invalidate_seq;
  41. unsigned long active_invalidate_ranges;
  42. struct rb_root_cached itree;
  43. wait_queue_head_t wq;
  44. struct hlist_head deferred_list;
  45. };
  46. /*
  47. * This is a collision-retry read-side/write-side 'lock', a lot like a
  48. * seqcount, however this allows multiple write-sides to hold it at
  49. * once. Conceptually the write side is protecting the values of the PTEs in
  50. * this mm, such that PTES cannot be read into SPTEs (shadow PTEs) while any
  51. * writer exists.
  52. *
  53. * Note that the core mm creates nested invalidate_range_start()/end() regions
  54. * within the same thread, and runs invalidate_range_start()/end() in parallel
  55. * on multiple CPUs. This is designed to not reduce concurrency or block
  56. * progress on the mm side.
  57. *
  58. * As a secondary function, holding the full write side also serves to prevent
  59. * writers for the itree, this is an optimization to avoid extra locking
  60. * during invalidate_range_start/end notifiers.
  61. *
  62. * The write side has two states, fully excluded:
  63. * - mm->active_invalidate_ranges != 0
  64. * - subscriptions->invalidate_seq & 1 == True (odd)
  65. * - some range on the mm_struct is being invalidated
  66. * - the itree is not allowed to change
  67. *
  68. * And partially excluded:
  69. * - mm->active_invalidate_ranges != 0
  70. * - subscriptions->invalidate_seq & 1 == False (even)
  71. * - some range on the mm_struct is being invalidated
  72. * - the itree is allowed to change
  73. *
  74. * Operations on notifier_subscriptions->invalidate_seq (under spinlock):
  75. * seq |= 1 # Begin writing
  76. * seq++ # Release the writing state
  77. * seq & 1 # True if a writer exists
  78. *
  79. * The later state avoids some expensive work on inv_end in the common case of
  80. * no mmu_interval_notifier monitoring the VA.
  81. */
  82. static bool
  83. mn_itree_is_invalidating(struct mmu_notifier_subscriptions *subscriptions)
  84. {
  85. lockdep_assert_held(&subscriptions->lock);
  86. return subscriptions->invalidate_seq & 1;
  87. }
  88. static struct mmu_interval_notifier *
  89. mn_itree_inv_start_range(struct mmu_notifier_subscriptions *subscriptions,
  90. const struct mmu_notifier_range *range,
  91. unsigned long *seq)
  92. {
  93. struct interval_tree_node *node;
  94. struct mmu_interval_notifier *res = NULL;
  95. spin_lock(&subscriptions->lock);
  96. subscriptions->active_invalidate_ranges++;
  97. node = interval_tree_iter_first(&subscriptions->itree, range->start,
  98. range->end - 1);
  99. if (node) {
  100. subscriptions->invalidate_seq |= 1;
  101. res = container_of(node, struct mmu_interval_notifier,
  102. interval_tree);
  103. }
  104. *seq = subscriptions->invalidate_seq;
  105. spin_unlock(&subscriptions->lock);
  106. return res;
  107. }
  108. static struct mmu_interval_notifier *
  109. mn_itree_inv_next(struct mmu_interval_notifier *interval_sub,
  110. const struct mmu_notifier_range *range)
  111. {
  112. struct interval_tree_node *node;
  113. node = interval_tree_iter_next(&interval_sub->interval_tree,
  114. range->start, range->end - 1);
  115. if (!node)
  116. return NULL;
  117. return container_of(node, struct mmu_interval_notifier, interval_tree);
  118. }
  119. static void mn_itree_inv_end(struct mmu_notifier_subscriptions *subscriptions)
  120. {
  121. struct mmu_interval_notifier *interval_sub;
  122. struct hlist_node *next;
  123. spin_lock(&subscriptions->lock);
  124. if (--subscriptions->active_invalidate_ranges ||
  125. !mn_itree_is_invalidating(subscriptions)) {
  126. spin_unlock(&subscriptions->lock);
  127. return;
  128. }
  129. /* Make invalidate_seq even */
  130. subscriptions->invalidate_seq++;
  131. /*
  132. * The inv_end incorporates a deferred mechanism like rtnl_unlock().
  133. * Adds and removes are queued until the final inv_end happens then
  134. * they are progressed. This arrangement for tree updates is used to
  135. * avoid using a blocking lock during invalidate_range_start.
  136. */
  137. hlist_for_each_entry_safe(interval_sub, next,
  138. &subscriptions->deferred_list,
  139. deferred_item) {
  140. if (RB_EMPTY_NODE(&interval_sub->interval_tree.rb))
  141. interval_tree_insert(&interval_sub->interval_tree,
  142. &subscriptions->itree);
  143. else
  144. interval_tree_remove(&interval_sub->interval_tree,
  145. &subscriptions->itree);
  146. hlist_del(&interval_sub->deferred_item);
  147. }
  148. spin_unlock(&subscriptions->lock);
  149. wake_up_all(&subscriptions->wq);
  150. }
  151. /**
  152. * mmu_interval_read_begin - Begin a read side critical section against a VA
  153. * range
  154. * @interval_sub: The interval subscription
  155. *
  156. * mmu_iterval_read_begin()/mmu_iterval_read_retry() implement a
  157. * collision-retry scheme similar to seqcount for the VA range under
  158. * subscription. If the mm invokes invalidation during the critical section
  159. * then mmu_interval_read_retry() will return true.
  160. *
  161. * This is useful to obtain shadow PTEs where teardown or setup of the SPTEs
  162. * require a blocking context. The critical region formed by this can sleep,
  163. * and the required 'user_lock' can also be a sleeping lock.
  164. *
  165. * The caller is required to provide a 'user_lock' to serialize both teardown
  166. * and setup.
  167. *
  168. * The return value should be passed to mmu_interval_read_retry().
  169. */
  170. unsigned long
  171. mmu_interval_read_begin(struct mmu_interval_notifier *interval_sub)
  172. {
  173. struct mmu_notifier_subscriptions *subscriptions =
  174. interval_sub->mm->notifier_subscriptions;
  175. unsigned long seq;
  176. bool is_invalidating;
  177. /*
  178. * If the subscription has a different seq value under the user_lock
  179. * than we started with then it has collided.
  180. *
  181. * If the subscription currently has the same seq value as the
  182. * subscriptions seq, then it is currently between
  183. * invalidate_start/end and is colliding.
  184. *
  185. * The locking looks broadly like this:
  186. * mn_itree_inv_start(): mmu_interval_read_begin():
  187. * spin_lock
  188. * seq = READ_ONCE(interval_sub->invalidate_seq);
  189. * seq == subs->invalidate_seq
  190. * spin_unlock
  191. * spin_lock
  192. * seq = ++subscriptions->invalidate_seq
  193. * spin_unlock
  194. * op->invalidate():
  195. * user_lock
  196. * mmu_interval_set_seq()
  197. * interval_sub->invalidate_seq = seq
  198. * user_unlock
  199. *
  200. * [Required: mmu_interval_read_retry() == true]
  201. *
  202. * mn_itree_inv_end():
  203. * spin_lock
  204. * seq = ++subscriptions->invalidate_seq
  205. * spin_unlock
  206. *
  207. * user_lock
  208. * mmu_interval_read_retry():
  209. * interval_sub->invalidate_seq != seq
  210. * user_unlock
  211. *
  212. * Barriers are not needed here as any races here are closed by an
  213. * eventual mmu_interval_read_retry(), which provides a barrier via the
  214. * user_lock.
  215. */
  216. spin_lock(&subscriptions->lock);
  217. /* Pairs with the WRITE_ONCE in mmu_interval_set_seq() */
  218. seq = READ_ONCE(interval_sub->invalidate_seq);
  219. is_invalidating = seq == subscriptions->invalidate_seq;
  220. spin_unlock(&subscriptions->lock);
  221. /*
  222. * interval_sub->invalidate_seq must always be set to an odd value via
  223. * mmu_interval_set_seq() using the provided cur_seq from
  224. * mn_itree_inv_start_range(). This ensures that if seq does wrap we
  225. * will always clear the below sleep in some reasonable time as
  226. * subscriptions->invalidate_seq is even in the idle state.
  227. */
  228. lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
  229. lock_map_release(&__mmu_notifier_invalidate_range_start_map);
  230. if (is_invalidating)
  231. wait_event(subscriptions->wq,
  232. READ_ONCE(subscriptions->invalidate_seq) != seq);
  233. /*
  234. * Notice that mmu_interval_read_retry() can already be true at this
  235. * point, avoiding loops here allows the caller to provide a global
  236. * time bound.
  237. */
  238. return seq;
  239. }
  240. EXPORT_SYMBOL_GPL(mmu_interval_read_begin);
  241. static void mn_itree_release(struct mmu_notifier_subscriptions *subscriptions,
  242. struct mm_struct *mm)
  243. {
  244. struct mmu_notifier_range range = {
  245. .flags = MMU_NOTIFIER_RANGE_BLOCKABLE,
  246. .event = MMU_NOTIFY_RELEASE,
  247. .mm = mm,
  248. .start = 0,
  249. .end = ULONG_MAX,
  250. };
  251. struct mmu_interval_notifier *interval_sub;
  252. unsigned long cur_seq;
  253. bool ret;
  254. for (interval_sub =
  255. mn_itree_inv_start_range(subscriptions, &range, &cur_seq);
  256. interval_sub;
  257. interval_sub = mn_itree_inv_next(interval_sub, &range)) {
  258. ret = interval_sub->ops->invalidate(interval_sub, &range,
  259. cur_seq);
  260. WARN_ON(!ret);
  261. }
  262. mn_itree_inv_end(subscriptions);
  263. }
  264. /*
  265. * This function can't run concurrently against mmu_notifier_register
  266. * because mm->mm_users > 0 during mmu_notifier_register and exit_mmap
  267. * runs with mm_users == 0. Other tasks may still invoke mmu notifiers
  268. * in parallel despite there being no task using this mm any more,
  269. * through the vmas outside of the exit_mmap context, such as with
  270. * vmtruncate. This serializes against mmu_notifier_unregister with
  271. * the notifier_subscriptions->lock in addition to SRCU and it serializes
  272. * against the other mmu notifiers with SRCU. struct mmu_notifier_subscriptions
  273. * can't go away from under us as exit_mmap holds an mm_count pin
  274. * itself.
  275. */
  276. static void mn_hlist_release(struct mmu_notifier_subscriptions *subscriptions,
  277. struct mm_struct *mm)
  278. {
  279. struct mmu_notifier *subscription;
  280. int id;
  281. /*
  282. * SRCU here will block mmu_notifier_unregister until
  283. * ->release returns.
  284. */
  285. id = srcu_read_lock(&srcu);
  286. hlist_for_each_entry_rcu(subscription, &subscriptions->list, hlist,
  287. srcu_read_lock_held(&srcu))
  288. /*
  289. * If ->release runs before mmu_notifier_unregister it must be
  290. * handled, as it's the only way for the driver to flush all
  291. * existing sptes and stop the driver from establishing any more
  292. * sptes before all the pages in the mm are freed.
  293. */
  294. if (subscription->ops->release)
  295. subscription->ops->release(subscription, mm);
  296. spin_lock(&subscriptions->lock);
  297. while (unlikely(!hlist_empty(&subscriptions->list))) {
  298. subscription = hlist_entry(subscriptions->list.first,
  299. struct mmu_notifier, hlist);
  300. /*
  301. * We arrived before mmu_notifier_unregister so
  302. * mmu_notifier_unregister will do nothing other than to wait
  303. * for ->release to finish and for mmu_notifier_unregister to
  304. * return.
  305. */
  306. hlist_del_init_rcu(&subscription->hlist);
  307. }
  308. spin_unlock(&subscriptions->lock);
  309. srcu_read_unlock(&srcu, id);
  310. /*
  311. * synchronize_srcu here prevents mmu_notifier_release from returning to
  312. * exit_mmap (which would proceed with freeing all pages in the mm)
  313. * until the ->release method returns, if it was invoked by
  314. * mmu_notifier_unregister.
  315. *
  316. * The notifier_subscriptions can't go away from under us because
  317. * one mm_count is held by exit_mmap.
  318. */
  319. synchronize_srcu(&srcu);
  320. }
  321. void __mmu_notifier_release(struct mm_struct *mm)
  322. {
  323. struct mmu_notifier_subscriptions *subscriptions =
  324. mm->notifier_subscriptions;
  325. if (subscriptions->has_itree)
  326. mn_itree_release(subscriptions, mm);
  327. if (!hlist_empty(&subscriptions->list))
  328. mn_hlist_release(subscriptions, mm);
  329. }
  330. /*
  331. * If no young bitflag is supported by the hardware, ->clear_flush_young can
  332. * unmap the address and return 1 or 0 depending if the mapping previously
  333. * existed or not.
  334. */
  335. int __mmu_notifier_clear_flush_young(struct mm_struct *mm,
  336. unsigned long start,
  337. unsigned long end)
  338. {
  339. struct mmu_notifier *subscription;
  340. int young = 0, id;
  341. id = srcu_read_lock(&srcu);
  342. hlist_for_each_entry_rcu(subscription,
  343. &mm->notifier_subscriptions->list, hlist,
  344. srcu_read_lock_held(&srcu)) {
  345. if (subscription->ops->clear_flush_young)
  346. young |= subscription->ops->clear_flush_young(
  347. subscription, mm, start, end);
  348. }
  349. srcu_read_unlock(&srcu, id);
  350. return young;
  351. }
  352. int __mmu_notifier_clear_young(struct mm_struct *mm,
  353. unsigned long start,
  354. unsigned long end)
  355. {
  356. struct mmu_notifier *subscription;
  357. int young = 0, id;
  358. id = srcu_read_lock(&srcu);
  359. hlist_for_each_entry_rcu(subscription,
  360. &mm->notifier_subscriptions->list, hlist,
  361. srcu_read_lock_held(&srcu)) {
  362. if (subscription->ops->clear_young)
  363. young |= subscription->ops->clear_young(subscription,
  364. mm, start, end);
  365. }
  366. srcu_read_unlock(&srcu, id);
  367. return young;
  368. }
  369. int __mmu_notifier_test_young(struct mm_struct *mm,
  370. unsigned long address)
  371. {
  372. struct mmu_notifier *subscription;
  373. int young = 0, id;
  374. id = srcu_read_lock(&srcu);
  375. hlist_for_each_entry_rcu(subscription,
  376. &mm->notifier_subscriptions->list, hlist,
  377. srcu_read_lock_held(&srcu)) {
  378. if (subscription->ops->test_young) {
  379. young = subscription->ops->test_young(subscription, mm,
  380. address);
  381. if (young)
  382. break;
  383. }
  384. }
  385. srcu_read_unlock(&srcu, id);
  386. return young;
  387. }
  388. static int mn_itree_invalidate(struct mmu_notifier_subscriptions *subscriptions,
  389. const struct mmu_notifier_range *range)
  390. {
  391. struct mmu_interval_notifier *interval_sub;
  392. unsigned long cur_seq;
  393. for (interval_sub =
  394. mn_itree_inv_start_range(subscriptions, range, &cur_seq);
  395. interval_sub;
  396. interval_sub = mn_itree_inv_next(interval_sub, range)) {
  397. bool ret;
  398. ret = interval_sub->ops->invalidate(interval_sub, range,
  399. cur_seq);
  400. if (!ret) {
  401. if (WARN_ON(mmu_notifier_range_blockable(range)))
  402. continue;
  403. goto out_would_block;
  404. }
  405. }
  406. return 0;
  407. out_would_block:
  408. /*
  409. * On -EAGAIN the non-blocking caller is not allowed to call
  410. * invalidate_range_end()
  411. */
  412. mn_itree_inv_end(subscriptions);
  413. return -EAGAIN;
  414. }
  415. static int mn_hlist_invalidate_range_start(
  416. struct mmu_notifier_subscriptions *subscriptions,
  417. struct mmu_notifier_range *range)
  418. {
  419. struct mmu_notifier *subscription;
  420. int ret = 0;
  421. int id;
  422. id = srcu_read_lock(&srcu);
  423. hlist_for_each_entry_rcu(subscription, &subscriptions->list, hlist,
  424. srcu_read_lock_held(&srcu)) {
  425. const struct mmu_notifier_ops *ops = subscription->ops;
  426. if (ops->invalidate_range_start) {
  427. int _ret;
  428. if (!mmu_notifier_range_blockable(range))
  429. non_block_start();
  430. _ret = ops->invalidate_range_start(subscription, range);
  431. if (!mmu_notifier_range_blockable(range))
  432. non_block_end();
  433. if (_ret) {
  434. pr_info("%pS callback failed with %d in %sblockable context.\n",
  435. ops->invalidate_range_start, _ret,
  436. !mmu_notifier_range_blockable(range) ?
  437. "non-" :
  438. "");
  439. WARN_ON(mmu_notifier_range_blockable(range) ||
  440. _ret != -EAGAIN);
  441. /*
  442. * We call all the notifiers on any EAGAIN,
  443. * there is no way for a notifier to know if
  444. * its start method failed, thus a start that
  445. * does EAGAIN can't also do end.
  446. */
  447. WARN_ON(ops->invalidate_range_end);
  448. ret = _ret;
  449. }
  450. }
  451. }
  452. if (ret) {
  453. /*
  454. * Must be non-blocking to get here. If there are multiple
  455. * notifiers and one or more failed start, any that succeeded
  456. * start are expecting their end to be called. Do so now.
  457. */
  458. hlist_for_each_entry_rcu(subscription, &subscriptions->list,
  459. hlist, srcu_read_lock_held(&srcu)) {
  460. if (!subscription->ops->invalidate_range_end)
  461. continue;
  462. subscription->ops->invalidate_range_end(subscription,
  463. range);
  464. }
  465. }
  466. srcu_read_unlock(&srcu, id);
  467. return ret;
  468. }
  469. int __mmu_notifier_invalidate_range_start(struct mmu_notifier_range *range)
  470. {
  471. struct mmu_notifier_subscriptions *subscriptions =
  472. range->mm->notifier_subscriptions;
  473. int ret;
  474. if (subscriptions->has_itree) {
  475. ret = mn_itree_invalidate(subscriptions, range);
  476. if (ret)
  477. return ret;
  478. }
  479. if (!hlist_empty(&subscriptions->list))
  480. return mn_hlist_invalidate_range_start(subscriptions, range);
  481. return 0;
  482. }
  483. static void
  484. mn_hlist_invalidate_end(struct mmu_notifier_subscriptions *subscriptions,
  485. struct mmu_notifier_range *range)
  486. {
  487. struct mmu_notifier *subscription;
  488. int id;
  489. id = srcu_read_lock(&srcu);
  490. hlist_for_each_entry_rcu(subscription, &subscriptions->list, hlist,
  491. srcu_read_lock_held(&srcu)) {
  492. if (subscription->ops->invalidate_range_end) {
  493. if (!mmu_notifier_range_blockable(range))
  494. non_block_start();
  495. subscription->ops->invalidate_range_end(subscription,
  496. range);
  497. if (!mmu_notifier_range_blockable(range))
  498. non_block_end();
  499. }
  500. }
  501. srcu_read_unlock(&srcu, id);
  502. }
  503. void __mmu_notifier_invalidate_range_end(struct mmu_notifier_range *range)
  504. {
  505. struct mmu_notifier_subscriptions *subscriptions =
  506. range->mm->notifier_subscriptions;
  507. lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
  508. if (subscriptions->has_itree)
  509. mn_itree_inv_end(subscriptions);
  510. if (!hlist_empty(&subscriptions->list))
  511. mn_hlist_invalidate_end(subscriptions, range);
  512. lock_map_release(&__mmu_notifier_invalidate_range_start_map);
  513. }
  514. void __mmu_notifier_arch_invalidate_secondary_tlbs(struct mm_struct *mm,
  515. unsigned long start, unsigned long end)
  516. {
  517. struct mmu_notifier *subscription;
  518. int id;
  519. id = srcu_read_lock(&srcu);
  520. hlist_for_each_entry_rcu(subscription,
  521. &mm->notifier_subscriptions->list, hlist,
  522. srcu_read_lock_held(&srcu)) {
  523. if (subscription->ops->arch_invalidate_secondary_tlbs)
  524. subscription->ops->arch_invalidate_secondary_tlbs(
  525. subscription, mm,
  526. start, end);
  527. }
  528. srcu_read_unlock(&srcu, id);
  529. }
  530. /*
  531. * Same as mmu_notifier_register but here the caller must hold the mmap_lock in
  532. * write mode. A NULL mn signals the notifier is being registered for itree
  533. * mode.
  534. */
  535. int __mmu_notifier_register(struct mmu_notifier *subscription,
  536. struct mm_struct *mm)
  537. {
  538. struct mmu_notifier_subscriptions *subscriptions = NULL;
  539. int ret;
  540. mmap_assert_write_locked(mm);
  541. BUG_ON(atomic_read(&mm->mm_users) <= 0);
  542. /*
  543. * Subsystems should only register for invalidate_secondary_tlbs() or
  544. * invalidate_range_start()/end() callbacks, not both.
  545. */
  546. if (WARN_ON_ONCE(subscription &&
  547. (subscription->ops->arch_invalidate_secondary_tlbs &&
  548. (subscription->ops->invalidate_range_start ||
  549. subscription->ops->invalidate_range_end))))
  550. return -EINVAL;
  551. if (!mm->notifier_subscriptions) {
  552. /*
  553. * kmalloc cannot be called under mm_take_all_locks(), but we
  554. * know that mm->notifier_subscriptions can't change while we
  555. * hold the write side of the mmap_lock.
  556. */
  557. subscriptions = kzalloc(
  558. sizeof(struct mmu_notifier_subscriptions), GFP_KERNEL);
  559. if (!subscriptions)
  560. return -ENOMEM;
  561. INIT_HLIST_HEAD(&subscriptions->list);
  562. spin_lock_init(&subscriptions->lock);
  563. subscriptions->invalidate_seq = 2;
  564. subscriptions->itree = RB_ROOT_CACHED;
  565. init_waitqueue_head(&subscriptions->wq);
  566. INIT_HLIST_HEAD(&subscriptions->deferred_list);
  567. }
  568. ret = mm_take_all_locks(mm);
  569. if (unlikely(ret))
  570. goto out_clean;
  571. /*
  572. * Serialize the update against mmu_notifier_unregister. A
  573. * side note: mmu_notifier_release can't run concurrently with
  574. * us because we hold the mm_users pin (either implicitly as
  575. * current->mm or explicitly with get_task_mm() or similar).
  576. * We can't race against any other mmu notifier method either
  577. * thanks to mm_take_all_locks().
  578. *
  579. * release semantics on the initialization of the
  580. * mmu_notifier_subscriptions's contents are provided for unlocked
  581. * readers. acquire can only be used while holding the mmgrab or
  582. * mmget, and is safe because once created the
  583. * mmu_notifier_subscriptions is not freed until the mm is destroyed.
  584. * As above, users holding the mmap_lock or one of the
  585. * mm_take_all_locks() do not need to use acquire semantics.
  586. */
  587. if (subscriptions)
  588. smp_store_release(&mm->notifier_subscriptions, subscriptions);
  589. if (subscription) {
  590. /* Pairs with the mmdrop in mmu_notifier_unregister_* */
  591. mmgrab(mm);
  592. subscription->mm = mm;
  593. subscription->users = 1;
  594. spin_lock(&mm->notifier_subscriptions->lock);
  595. hlist_add_head_rcu(&subscription->hlist,
  596. &mm->notifier_subscriptions->list);
  597. spin_unlock(&mm->notifier_subscriptions->lock);
  598. } else
  599. mm->notifier_subscriptions->has_itree = true;
  600. mm_drop_all_locks(mm);
  601. BUG_ON(atomic_read(&mm->mm_users) <= 0);
  602. return 0;
  603. out_clean:
  604. kfree(subscriptions);
  605. return ret;
  606. }
  607. EXPORT_SYMBOL_GPL(__mmu_notifier_register);
  608. /**
  609. * mmu_notifier_register - Register a notifier on a mm
  610. * @subscription: The notifier to attach
  611. * @mm: The mm to attach the notifier to
  612. *
  613. * Must not hold mmap_lock nor any other VM related lock when calling
  614. * this registration function. Must also ensure mm_users can't go down
  615. * to zero while this runs to avoid races with mmu_notifier_release,
  616. * so mm has to be current->mm or the mm should be pinned safely such
  617. * as with get_task_mm(). If the mm is not current->mm, the mm_users
  618. * pin should be released by calling mmput after mmu_notifier_register
  619. * returns.
  620. *
  621. * mmu_notifier_unregister() or mmu_notifier_put() must be always called to
  622. * unregister the notifier.
  623. *
  624. * While the caller has a mmu_notifier get the subscription->mm pointer will remain
  625. * valid, and can be converted to an active mm pointer via mmget_not_zero().
  626. */
  627. int mmu_notifier_register(struct mmu_notifier *subscription,
  628. struct mm_struct *mm)
  629. {
  630. int ret;
  631. mmap_write_lock(mm);
  632. ret = __mmu_notifier_register(subscription, mm);
  633. mmap_write_unlock(mm);
  634. return ret;
  635. }
  636. EXPORT_SYMBOL_GPL(mmu_notifier_register);
  637. static struct mmu_notifier *
  638. find_get_mmu_notifier(struct mm_struct *mm, const struct mmu_notifier_ops *ops)
  639. {
  640. struct mmu_notifier *subscription;
  641. spin_lock(&mm->notifier_subscriptions->lock);
  642. hlist_for_each_entry_rcu(subscription,
  643. &mm->notifier_subscriptions->list, hlist,
  644. lockdep_is_held(&mm->notifier_subscriptions->lock)) {
  645. if (subscription->ops != ops)
  646. continue;
  647. if (likely(subscription->users != UINT_MAX))
  648. subscription->users++;
  649. else
  650. subscription = ERR_PTR(-EOVERFLOW);
  651. spin_unlock(&mm->notifier_subscriptions->lock);
  652. return subscription;
  653. }
  654. spin_unlock(&mm->notifier_subscriptions->lock);
  655. return NULL;
  656. }
  657. /**
  658. * mmu_notifier_get_locked - Return the single struct mmu_notifier for
  659. * the mm & ops
  660. * @ops: The operations struct being subscribe with
  661. * @mm : The mm to attach notifiers too
  662. *
  663. * This function either allocates a new mmu_notifier via
  664. * ops->alloc_notifier(), or returns an already existing notifier on the
  665. * list. The value of the ops pointer is used to determine when two notifiers
  666. * are the same.
  667. *
  668. * Each call to mmu_notifier_get() must be paired with a call to
  669. * mmu_notifier_put(). The caller must hold the write side of mm->mmap_lock.
  670. *
  671. * While the caller has a mmu_notifier get the mm pointer will remain valid,
  672. * and can be converted to an active mm pointer via mmget_not_zero().
  673. */
  674. struct mmu_notifier *mmu_notifier_get_locked(const struct mmu_notifier_ops *ops,
  675. struct mm_struct *mm)
  676. {
  677. struct mmu_notifier *subscription;
  678. int ret;
  679. mmap_assert_write_locked(mm);
  680. if (mm->notifier_subscriptions) {
  681. subscription = find_get_mmu_notifier(mm, ops);
  682. if (subscription)
  683. return subscription;
  684. }
  685. subscription = ops->alloc_notifier(mm);
  686. if (IS_ERR(subscription))
  687. return subscription;
  688. subscription->ops = ops;
  689. ret = __mmu_notifier_register(subscription, mm);
  690. if (ret)
  691. goto out_free;
  692. return subscription;
  693. out_free:
  694. subscription->ops->free_notifier(subscription);
  695. return ERR_PTR(ret);
  696. }
  697. EXPORT_SYMBOL_GPL(mmu_notifier_get_locked);
  698. /* this is called after the last mmu_notifier_unregister() returned */
  699. void __mmu_notifier_subscriptions_destroy(struct mm_struct *mm)
  700. {
  701. BUG_ON(!hlist_empty(&mm->notifier_subscriptions->list));
  702. kfree(mm->notifier_subscriptions);
  703. mm->notifier_subscriptions = LIST_POISON1; /* debug */
  704. }
  705. /*
  706. * This releases the mm_count pin automatically and frees the mm
  707. * structure if it was the last user of it. It serializes against
  708. * running mmu notifiers with SRCU and against mmu_notifier_unregister
  709. * with the unregister lock + SRCU. All sptes must be dropped before
  710. * calling mmu_notifier_unregister. ->release or any other notifier
  711. * method may be invoked concurrently with mmu_notifier_unregister,
  712. * and only after mmu_notifier_unregister returned we're guaranteed
  713. * that ->release or any other method can't run anymore.
  714. */
  715. void mmu_notifier_unregister(struct mmu_notifier *subscription,
  716. struct mm_struct *mm)
  717. {
  718. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  719. if (!hlist_unhashed(&subscription->hlist)) {
  720. /*
  721. * SRCU here will force exit_mmap to wait for ->release to
  722. * finish before freeing the pages.
  723. */
  724. int id;
  725. id = srcu_read_lock(&srcu);
  726. /*
  727. * exit_mmap will block in mmu_notifier_release to guarantee
  728. * that ->release is called before freeing the pages.
  729. */
  730. if (subscription->ops->release)
  731. subscription->ops->release(subscription, mm);
  732. srcu_read_unlock(&srcu, id);
  733. spin_lock(&mm->notifier_subscriptions->lock);
  734. /*
  735. * Can not use list_del_rcu() since __mmu_notifier_release
  736. * can delete it before we hold the lock.
  737. */
  738. hlist_del_init_rcu(&subscription->hlist);
  739. spin_unlock(&mm->notifier_subscriptions->lock);
  740. }
  741. /*
  742. * Wait for any running method to finish, of course including
  743. * ->release if it was run by mmu_notifier_release instead of us.
  744. */
  745. synchronize_srcu(&srcu);
  746. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  747. mmdrop(mm);
  748. }
  749. EXPORT_SYMBOL_GPL(mmu_notifier_unregister);
  750. static void mmu_notifier_free_rcu(struct rcu_head *rcu)
  751. {
  752. struct mmu_notifier *subscription =
  753. container_of(rcu, struct mmu_notifier, rcu);
  754. struct mm_struct *mm = subscription->mm;
  755. subscription->ops->free_notifier(subscription);
  756. /* Pairs with the get in __mmu_notifier_register() */
  757. mmdrop(mm);
  758. }
  759. /**
  760. * mmu_notifier_put - Release the reference on the notifier
  761. * @subscription: The notifier to act on
  762. *
  763. * This function must be paired with each mmu_notifier_get(), it releases the
  764. * reference obtained by the get. If this is the last reference then process
  765. * to free the notifier will be run asynchronously.
  766. *
  767. * Unlike mmu_notifier_unregister() the get/put flow only calls ops->release
  768. * when the mm_struct is destroyed. Instead free_notifier is always called to
  769. * release any resources held by the user.
  770. *
  771. * As ops->release is not guaranteed to be called, the user must ensure that
  772. * all sptes are dropped, and no new sptes can be established before
  773. * mmu_notifier_put() is called.
  774. *
  775. * This function can be called from the ops->release callback, however the
  776. * caller must still ensure it is called pairwise with mmu_notifier_get().
  777. *
  778. * Modules calling this function must call mmu_notifier_synchronize() in
  779. * their __exit functions to ensure the async work is completed.
  780. */
  781. void mmu_notifier_put(struct mmu_notifier *subscription)
  782. {
  783. struct mm_struct *mm = subscription->mm;
  784. spin_lock(&mm->notifier_subscriptions->lock);
  785. if (WARN_ON(!subscription->users) || --subscription->users)
  786. goto out_unlock;
  787. hlist_del_init_rcu(&subscription->hlist);
  788. spin_unlock(&mm->notifier_subscriptions->lock);
  789. call_srcu(&srcu, &subscription->rcu, mmu_notifier_free_rcu);
  790. return;
  791. out_unlock:
  792. spin_unlock(&mm->notifier_subscriptions->lock);
  793. }
  794. EXPORT_SYMBOL_GPL(mmu_notifier_put);
  795. static int __mmu_interval_notifier_insert(
  796. struct mmu_interval_notifier *interval_sub, struct mm_struct *mm,
  797. struct mmu_notifier_subscriptions *subscriptions, unsigned long start,
  798. unsigned long length, const struct mmu_interval_notifier_ops *ops)
  799. {
  800. interval_sub->mm = mm;
  801. interval_sub->ops = ops;
  802. RB_CLEAR_NODE(&interval_sub->interval_tree.rb);
  803. interval_sub->interval_tree.start = start;
  804. /*
  805. * Note that the representation of the intervals in the interval tree
  806. * considers the ending point as contained in the interval.
  807. */
  808. if (length == 0 ||
  809. check_add_overflow(start, length - 1,
  810. &interval_sub->interval_tree.last))
  811. return -EOVERFLOW;
  812. /* Must call with a mmget() held */
  813. if (WARN_ON(atomic_read(&mm->mm_users) <= 0))
  814. return -EINVAL;
  815. /* pairs with mmdrop in mmu_interval_notifier_remove() */
  816. mmgrab(mm);
  817. /*
  818. * If some invalidate_range_start/end region is going on in parallel
  819. * we don't know what VA ranges are affected, so we must assume this
  820. * new range is included.
  821. *
  822. * If the itree is invalidating then we are not allowed to change
  823. * it. Retrying until invalidation is done is tricky due to the
  824. * possibility for live lock, instead defer the add to
  825. * mn_itree_inv_end() so this algorithm is deterministic.
  826. *
  827. * In all cases the value for the interval_sub->invalidate_seq should be
  828. * odd, see mmu_interval_read_begin()
  829. */
  830. spin_lock(&subscriptions->lock);
  831. if (subscriptions->active_invalidate_ranges) {
  832. if (mn_itree_is_invalidating(subscriptions))
  833. hlist_add_head(&interval_sub->deferred_item,
  834. &subscriptions->deferred_list);
  835. else {
  836. subscriptions->invalidate_seq |= 1;
  837. interval_tree_insert(&interval_sub->interval_tree,
  838. &subscriptions->itree);
  839. }
  840. interval_sub->invalidate_seq = subscriptions->invalidate_seq;
  841. } else {
  842. WARN_ON(mn_itree_is_invalidating(subscriptions));
  843. /*
  844. * The starting seq for a subscription not under invalidation
  845. * should be odd, not equal to the current invalidate_seq and
  846. * invalidate_seq should not 'wrap' to the new seq any time
  847. * soon.
  848. */
  849. interval_sub->invalidate_seq =
  850. subscriptions->invalidate_seq - 1;
  851. interval_tree_insert(&interval_sub->interval_tree,
  852. &subscriptions->itree);
  853. }
  854. spin_unlock(&subscriptions->lock);
  855. return 0;
  856. }
  857. /**
  858. * mmu_interval_notifier_insert - Insert an interval notifier
  859. * @interval_sub: Interval subscription to register
  860. * @start: Starting virtual address to monitor
  861. * @length: Length of the range to monitor
  862. * @mm: mm_struct to attach to
  863. * @ops: Interval notifier operations to be called on matching events
  864. *
  865. * This function subscribes the interval notifier for notifications from the
  866. * mm. Upon return the ops related to mmu_interval_notifier will be called
  867. * whenever an event that intersects with the given range occurs.
  868. *
  869. * Upon return the range_notifier may not be present in the interval tree yet.
  870. * The caller must use the normal interval notifier read flow via
  871. * mmu_interval_read_begin() to establish SPTEs for this range.
  872. */
  873. int mmu_interval_notifier_insert(struct mmu_interval_notifier *interval_sub,
  874. struct mm_struct *mm, unsigned long start,
  875. unsigned long length,
  876. const struct mmu_interval_notifier_ops *ops)
  877. {
  878. struct mmu_notifier_subscriptions *subscriptions;
  879. int ret;
  880. might_lock(&mm->mmap_lock);
  881. subscriptions = smp_load_acquire(&mm->notifier_subscriptions);
  882. if (!subscriptions || !subscriptions->has_itree) {
  883. ret = mmu_notifier_register(NULL, mm);
  884. if (ret)
  885. return ret;
  886. subscriptions = mm->notifier_subscriptions;
  887. }
  888. return __mmu_interval_notifier_insert(interval_sub, mm, subscriptions,
  889. start, length, ops);
  890. }
  891. EXPORT_SYMBOL_GPL(mmu_interval_notifier_insert);
  892. int mmu_interval_notifier_insert_locked(
  893. struct mmu_interval_notifier *interval_sub, struct mm_struct *mm,
  894. unsigned long start, unsigned long length,
  895. const struct mmu_interval_notifier_ops *ops)
  896. {
  897. struct mmu_notifier_subscriptions *subscriptions =
  898. mm->notifier_subscriptions;
  899. int ret;
  900. mmap_assert_write_locked(mm);
  901. if (!subscriptions || !subscriptions->has_itree) {
  902. ret = __mmu_notifier_register(NULL, mm);
  903. if (ret)
  904. return ret;
  905. subscriptions = mm->notifier_subscriptions;
  906. }
  907. return __mmu_interval_notifier_insert(interval_sub, mm, subscriptions,
  908. start, length, ops);
  909. }
  910. EXPORT_SYMBOL_GPL(mmu_interval_notifier_insert_locked);
  911. static bool
  912. mmu_interval_seq_released(struct mmu_notifier_subscriptions *subscriptions,
  913. unsigned long seq)
  914. {
  915. bool ret;
  916. spin_lock(&subscriptions->lock);
  917. ret = subscriptions->invalidate_seq != seq;
  918. spin_unlock(&subscriptions->lock);
  919. return ret;
  920. }
  921. /**
  922. * mmu_interval_notifier_remove - Remove a interval notifier
  923. * @interval_sub: Interval subscription to unregister
  924. *
  925. * This function must be paired with mmu_interval_notifier_insert(). It cannot
  926. * be called from any ops callback.
  927. *
  928. * Once this returns ops callbacks are no longer running on other CPUs and
  929. * will not be called in future.
  930. */
  931. void mmu_interval_notifier_remove(struct mmu_interval_notifier *interval_sub)
  932. {
  933. struct mm_struct *mm = interval_sub->mm;
  934. struct mmu_notifier_subscriptions *subscriptions =
  935. mm->notifier_subscriptions;
  936. unsigned long seq = 0;
  937. might_sleep();
  938. spin_lock(&subscriptions->lock);
  939. if (mn_itree_is_invalidating(subscriptions)) {
  940. /*
  941. * remove is being called after insert put this on the
  942. * deferred list, but before the deferred list was processed.
  943. */
  944. if (RB_EMPTY_NODE(&interval_sub->interval_tree.rb)) {
  945. hlist_del(&interval_sub->deferred_item);
  946. } else {
  947. hlist_add_head(&interval_sub->deferred_item,
  948. &subscriptions->deferred_list);
  949. seq = subscriptions->invalidate_seq;
  950. }
  951. } else {
  952. WARN_ON(RB_EMPTY_NODE(&interval_sub->interval_tree.rb));
  953. interval_tree_remove(&interval_sub->interval_tree,
  954. &subscriptions->itree);
  955. }
  956. spin_unlock(&subscriptions->lock);
  957. /*
  958. * The possible sleep on progress in the invalidation requires the
  959. * caller not hold any locks held by invalidation callbacks.
  960. */
  961. lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
  962. lock_map_release(&__mmu_notifier_invalidate_range_start_map);
  963. if (seq)
  964. wait_event(subscriptions->wq,
  965. mmu_interval_seq_released(subscriptions, seq));
  966. /* pairs with mmgrab in mmu_interval_notifier_insert() */
  967. mmdrop(mm);
  968. }
  969. EXPORT_SYMBOL_GPL(mmu_interval_notifier_remove);
  970. /**
  971. * mmu_notifier_synchronize - Ensure all mmu_notifiers are freed
  972. *
  973. * This function ensures that all outstanding async SRU work from
  974. * mmu_notifier_put() is completed. After it returns any mmu_notifier_ops
  975. * associated with an unused mmu_notifier will no longer be called.
  976. *
  977. * Before using the caller must ensure that all of its mmu_notifiers have been
  978. * fully released via mmu_notifier_put().
  979. *
  980. * Modules using the mmu_notifier_put() API should call this in their __exit
  981. * function to avoid module unloading races.
  982. */
  983. void mmu_notifier_synchronize(void)
  984. {
  985. synchronize_srcu(&srcu);
  986. }
  987. EXPORT_SYMBOL_GPL(mmu_notifier_synchronize);