mmu_notifier.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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@gentwo.org>
  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_obj(struct mmu_notifier_subscriptions);
  558. if (!subscriptions)
  559. return -ENOMEM;
  560. INIT_HLIST_HEAD(&subscriptions->list);
  561. spin_lock_init(&subscriptions->lock);
  562. subscriptions->invalidate_seq = 2;
  563. subscriptions->itree = RB_ROOT_CACHED;
  564. init_waitqueue_head(&subscriptions->wq);
  565. INIT_HLIST_HEAD(&subscriptions->deferred_list);
  566. }
  567. ret = mm_take_all_locks(mm);
  568. if (unlikely(ret))
  569. goto out_clean;
  570. /*
  571. * Serialize the update against mmu_notifier_unregister. A
  572. * side note: mmu_notifier_release can't run concurrently with
  573. * us because we hold the mm_users pin (either implicitly as
  574. * current->mm or explicitly with get_task_mm() or similar).
  575. * We can't race against any other mmu notifier method either
  576. * thanks to mm_take_all_locks().
  577. *
  578. * release semantics on the initialization of the
  579. * mmu_notifier_subscriptions's contents are provided for unlocked
  580. * readers. acquire can only be used while holding the mmgrab or
  581. * mmget, and is safe because once created the
  582. * mmu_notifier_subscriptions is not freed until the mm is destroyed.
  583. * As above, users holding the mmap_lock or one of the
  584. * mm_take_all_locks() do not need to use acquire semantics.
  585. */
  586. if (subscriptions)
  587. smp_store_release(&mm->notifier_subscriptions, subscriptions);
  588. if (subscription) {
  589. /* Pairs with the mmdrop in mmu_notifier_unregister_* */
  590. mmgrab(mm);
  591. subscription->mm = mm;
  592. subscription->users = 1;
  593. spin_lock(&mm->notifier_subscriptions->lock);
  594. hlist_add_head_rcu(&subscription->hlist,
  595. &mm->notifier_subscriptions->list);
  596. spin_unlock(&mm->notifier_subscriptions->lock);
  597. } else
  598. mm->notifier_subscriptions->has_itree = true;
  599. mm_drop_all_locks(mm);
  600. BUG_ON(atomic_read(&mm->mm_users) <= 0);
  601. return 0;
  602. out_clean:
  603. kfree(subscriptions);
  604. return ret;
  605. }
  606. EXPORT_SYMBOL_GPL(__mmu_notifier_register);
  607. /**
  608. * mmu_notifier_register - Register a notifier on a mm
  609. * @subscription: The notifier to attach
  610. * @mm: The mm to attach the notifier to
  611. *
  612. * Must not hold mmap_lock nor any other VM related lock when calling
  613. * this registration function. Must also ensure mm_users can't go down
  614. * to zero while this runs to avoid races with mmu_notifier_release,
  615. * so mm has to be current->mm or the mm should be pinned safely such
  616. * as with get_task_mm(). If the mm is not current->mm, the mm_users
  617. * pin should be released by calling mmput after mmu_notifier_register
  618. * returns.
  619. *
  620. * mmu_notifier_unregister() or mmu_notifier_put() must be always called to
  621. * unregister the notifier.
  622. *
  623. * While the caller has a mmu_notifier get the subscription->mm pointer will remain
  624. * valid, and can be converted to an active mm pointer via mmget_not_zero().
  625. */
  626. int mmu_notifier_register(struct mmu_notifier *subscription,
  627. struct mm_struct *mm)
  628. {
  629. int ret;
  630. mmap_write_lock(mm);
  631. ret = __mmu_notifier_register(subscription, mm);
  632. mmap_write_unlock(mm);
  633. return ret;
  634. }
  635. EXPORT_SYMBOL_GPL(mmu_notifier_register);
  636. static struct mmu_notifier *
  637. find_get_mmu_notifier(struct mm_struct *mm, const struct mmu_notifier_ops *ops)
  638. {
  639. struct mmu_notifier *subscription;
  640. spin_lock(&mm->notifier_subscriptions->lock);
  641. hlist_for_each_entry_rcu(subscription,
  642. &mm->notifier_subscriptions->list, hlist,
  643. lockdep_is_held(&mm->notifier_subscriptions->lock)) {
  644. if (subscription->ops != ops)
  645. continue;
  646. if (likely(subscription->users != UINT_MAX))
  647. subscription->users++;
  648. else
  649. subscription = ERR_PTR(-EOVERFLOW);
  650. spin_unlock(&mm->notifier_subscriptions->lock);
  651. return subscription;
  652. }
  653. spin_unlock(&mm->notifier_subscriptions->lock);
  654. return NULL;
  655. }
  656. /**
  657. * mmu_notifier_get_locked - Return the single struct mmu_notifier for
  658. * the mm & ops
  659. * @ops: The operations struct being subscribe with
  660. * @mm : The mm to attach notifiers too
  661. *
  662. * This function either allocates a new mmu_notifier via
  663. * ops->alloc_notifier(), or returns an already existing notifier on the
  664. * list. The value of the ops pointer is used to determine when two notifiers
  665. * are the same.
  666. *
  667. * Each call to mmu_notifier_get() must be paired with a call to
  668. * mmu_notifier_put(). The caller must hold the write side of mm->mmap_lock.
  669. *
  670. * While the caller has a mmu_notifier get the mm pointer will remain valid,
  671. * and can be converted to an active mm pointer via mmget_not_zero().
  672. */
  673. struct mmu_notifier *mmu_notifier_get_locked(const struct mmu_notifier_ops *ops,
  674. struct mm_struct *mm)
  675. {
  676. struct mmu_notifier *subscription;
  677. int ret;
  678. mmap_assert_write_locked(mm);
  679. if (mm->notifier_subscriptions) {
  680. subscription = find_get_mmu_notifier(mm, ops);
  681. if (subscription)
  682. return subscription;
  683. }
  684. subscription = ops->alloc_notifier(mm);
  685. if (IS_ERR(subscription))
  686. return subscription;
  687. subscription->ops = ops;
  688. ret = __mmu_notifier_register(subscription, mm);
  689. if (ret)
  690. goto out_free;
  691. return subscription;
  692. out_free:
  693. subscription->ops->free_notifier(subscription);
  694. return ERR_PTR(ret);
  695. }
  696. EXPORT_SYMBOL_GPL(mmu_notifier_get_locked);
  697. /* this is called after the last mmu_notifier_unregister() returned */
  698. void __mmu_notifier_subscriptions_destroy(struct mm_struct *mm)
  699. {
  700. BUG_ON(!hlist_empty(&mm->notifier_subscriptions->list));
  701. kfree(mm->notifier_subscriptions);
  702. mm->notifier_subscriptions = LIST_POISON1; /* debug */
  703. }
  704. /*
  705. * This releases the mm_count pin automatically and frees the mm
  706. * structure if it was the last user of it. It serializes against
  707. * running mmu notifiers with SRCU and against mmu_notifier_unregister
  708. * with the unregister lock + SRCU. All sptes must be dropped before
  709. * calling mmu_notifier_unregister. ->release or any other notifier
  710. * method may be invoked concurrently with mmu_notifier_unregister,
  711. * and only after mmu_notifier_unregister returned we're guaranteed
  712. * that ->release or any other method can't run anymore.
  713. */
  714. void mmu_notifier_unregister(struct mmu_notifier *subscription,
  715. struct mm_struct *mm)
  716. {
  717. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  718. if (!hlist_unhashed(&subscription->hlist)) {
  719. /*
  720. * SRCU here will force exit_mmap to wait for ->release to
  721. * finish before freeing the pages.
  722. */
  723. int id;
  724. id = srcu_read_lock(&srcu);
  725. /*
  726. * exit_mmap will block in mmu_notifier_release to guarantee
  727. * that ->release is called before freeing the pages.
  728. */
  729. if (subscription->ops->release)
  730. subscription->ops->release(subscription, mm);
  731. srcu_read_unlock(&srcu, id);
  732. spin_lock(&mm->notifier_subscriptions->lock);
  733. /*
  734. * Can not use list_del_rcu() since __mmu_notifier_release
  735. * can delete it before we hold the lock.
  736. */
  737. hlist_del_init_rcu(&subscription->hlist);
  738. spin_unlock(&mm->notifier_subscriptions->lock);
  739. }
  740. /*
  741. * Wait for any running method to finish, of course including
  742. * ->release if it was run by mmu_notifier_release instead of us.
  743. */
  744. synchronize_srcu(&srcu);
  745. BUG_ON(atomic_read(&mm->mm_count) <= 0);
  746. mmdrop(mm);
  747. }
  748. EXPORT_SYMBOL_GPL(mmu_notifier_unregister);
  749. static void mmu_notifier_free_rcu(struct rcu_head *rcu)
  750. {
  751. struct mmu_notifier *subscription =
  752. container_of(rcu, struct mmu_notifier, rcu);
  753. struct mm_struct *mm = subscription->mm;
  754. subscription->ops->free_notifier(subscription);
  755. /* Pairs with the get in __mmu_notifier_register() */
  756. mmdrop(mm);
  757. }
  758. /**
  759. * mmu_notifier_put - Release the reference on the notifier
  760. * @subscription: The notifier to act on
  761. *
  762. * This function must be paired with each mmu_notifier_get(), it releases the
  763. * reference obtained by the get. If this is the last reference then process
  764. * to free the notifier will be run asynchronously.
  765. *
  766. * Unlike mmu_notifier_unregister() the get/put flow only calls ops->release
  767. * when the mm_struct is destroyed. Instead free_notifier is always called to
  768. * release any resources held by the user.
  769. *
  770. * As ops->release is not guaranteed to be called, the user must ensure that
  771. * all sptes are dropped, and no new sptes can be established before
  772. * mmu_notifier_put() is called.
  773. *
  774. * This function can be called from the ops->release callback, however the
  775. * caller must still ensure it is called pairwise with mmu_notifier_get().
  776. *
  777. * Modules calling this function must call mmu_notifier_synchronize() in
  778. * their __exit functions to ensure the async work is completed.
  779. */
  780. void mmu_notifier_put(struct mmu_notifier *subscription)
  781. {
  782. struct mm_struct *mm = subscription->mm;
  783. spin_lock(&mm->notifier_subscriptions->lock);
  784. if (WARN_ON(!subscription->users) || --subscription->users)
  785. goto out_unlock;
  786. hlist_del_init_rcu(&subscription->hlist);
  787. spin_unlock(&mm->notifier_subscriptions->lock);
  788. call_srcu(&srcu, &subscription->rcu, mmu_notifier_free_rcu);
  789. return;
  790. out_unlock:
  791. spin_unlock(&mm->notifier_subscriptions->lock);
  792. }
  793. EXPORT_SYMBOL_GPL(mmu_notifier_put);
  794. static int __mmu_interval_notifier_insert(
  795. struct mmu_interval_notifier *interval_sub, struct mm_struct *mm,
  796. struct mmu_notifier_subscriptions *subscriptions, unsigned long start,
  797. unsigned long length, const struct mmu_interval_notifier_ops *ops)
  798. {
  799. interval_sub->mm = mm;
  800. interval_sub->ops = ops;
  801. RB_CLEAR_NODE(&interval_sub->interval_tree.rb);
  802. interval_sub->interval_tree.start = start;
  803. /*
  804. * Note that the representation of the intervals in the interval tree
  805. * considers the ending point as contained in the interval.
  806. */
  807. if (length == 0 ||
  808. check_add_overflow(start, length - 1,
  809. &interval_sub->interval_tree.last))
  810. return -EOVERFLOW;
  811. /* Must call with a mmget() held */
  812. if (WARN_ON(atomic_read(&mm->mm_users) <= 0))
  813. return -EINVAL;
  814. /* pairs with mmdrop in mmu_interval_notifier_remove() */
  815. mmgrab(mm);
  816. /*
  817. * If some invalidate_range_start/end region is going on in parallel
  818. * we don't know what VA ranges are affected, so we must assume this
  819. * new range is included.
  820. *
  821. * If the itree is invalidating then we are not allowed to change
  822. * it. Retrying until invalidation is done is tricky due to the
  823. * possibility for live lock, instead defer the add to
  824. * mn_itree_inv_end() so this algorithm is deterministic.
  825. *
  826. * In all cases the value for the interval_sub->invalidate_seq should be
  827. * odd, see mmu_interval_read_begin()
  828. */
  829. spin_lock(&subscriptions->lock);
  830. if (subscriptions->active_invalidate_ranges) {
  831. if (mn_itree_is_invalidating(subscriptions))
  832. hlist_add_head(&interval_sub->deferred_item,
  833. &subscriptions->deferred_list);
  834. else {
  835. subscriptions->invalidate_seq |= 1;
  836. interval_tree_insert(&interval_sub->interval_tree,
  837. &subscriptions->itree);
  838. }
  839. interval_sub->invalidate_seq = subscriptions->invalidate_seq;
  840. } else {
  841. WARN_ON(mn_itree_is_invalidating(subscriptions));
  842. /*
  843. * The starting seq for a subscription not under invalidation
  844. * should be odd, not equal to the current invalidate_seq and
  845. * invalidate_seq should not 'wrap' to the new seq any time
  846. * soon.
  847. */
  848. interval_sub->invalidate_seq =
  849. subscriptions->invalidate_seq - 1;
  850. interval_tree_insert(&interval_sub->interval_tree,
  851. &subscriptions->itree);
  852. }
  853. spin_unlock(&subscriptions->lock);
  854. return 0;
  855. }
  856. /**
  857. * mmu_interval_notifier_insert - Insert an interval notifier
  858. * @interval_sub: Interval subscription to register
  859. * @start: Starting virtual address to monitor
  860. * @length: Length of the range to monitor
  861. * @mm: mm_struct to attach to
  862. * @ops: Interval notifier operations to be called on matching events
  863. *
  864. * This function subscribes the interval notifier for notifications from the
  865. * mm. Upon return the ops related to mmu_interval_notifier will be called
  866. * whenever an event that intersects with the given range occurs.
  867. *
  868. * Upon return the range_notifier may not be present in the interval tree yet.
  869. * The caller must use the normal interval notifier read flow via
  870. * mmu_interval_read_begin() to establish SPTEs for this range.
  871. */
  872. int mmu_interval_notifier_insert(struct mmu_interval_notifier *interval_sub,
  873. struct mm_struct *mm, unsigned long start,
  874. unsigned long length,
  875. const struct mmu_interval_notifier_ops *ops)
  876. {
  877. struct mmu_notifier_subscriptions *subscriptions;
  878. int ret;
  879. might_lock(&mm->mmap_lock);
  880. subscriptions = smp_load_acquire(&mm->notifier_subscriptions);
  881. if (!subscriptions || !subscriptions->has_itree) {
  882. ret = mmu_notifier_register(NULL, mm);
  883. if (ret)
  884. return ret;
  885. subscriptions = mm->notifier_subscriptions;
  886. }
  887. return __mmu_interval_notifier_insert(interval_sub, mm, subscriptions,
  888. start, length, ops);
  889. }
  890. EXPORT_SYMBOL_GPL(mmu_interval_notifier_insert);
  891. int mmu_interval_notifier_insert_locked(
  892. struct mmu_interval_notifier *interval_sub, struct mm_struct *mm,
  893. unsigned long start, unsigned long length,
  894. const struct mmu_interval_notifier_ops *ops)
  895. {
  896. struct mmu_notifier_subscriptions *subscriptions =
  897. mm->notifier_subscriptions;
  898. int ret;
  899. mmap_assert_write_locked(mm);
  900. if (!subscriptions || !subscriptions->has_itree) {
  901. ret = __mmu_notifier_register(NULL, mm);
  902. if (ret)
  903. return ret;
  904. subscriptions = mm->notifier_subscriptions;
  905. }
  906. return __mmu_interval_notifier_insert(interval_sub, mm, subscriptions,
  907. start, length, ops);
  908. }
  909. EXPORT_SYMBOL_GPL(mmu_interval_notifier_insert_locked);
  910. static bool
  911. mmu_interval_seq_released(struct mmu_notifier_subscriptions *subscriptions,
  912. unsigned long seq)
  913. {
  914. bool ret;
  915. spin_lock(&subscriptions->lock);
  916. ret = subscriptions->invalidate_seq != seq;
  917. spin_unlock(&subscriptions->lock);
  918. return ret;
  919. }
  920. /**
  921. * mmu_interval_notifier_remove - Remove a interval notifier
  922. * @interval_sub: Interval subscription to unregister
  923. *
  924. * This function must be paired with mmu_interval_notifier_insert(). It cannot
  925. * be called from any ops callback.
  926. *
  927. * Once this returns ops callbacks are no longer running on other CPUs and
  928. * will not be called in future.
  929. */
  930. void mmu_interval_notifier_remove(struct mmu_interval_notifier *interval_sub)
  931. {
  932. struct mm_struct *mm = interval_sub->mm;
  933. struct mmu_notifier_subscriptions *subscriptions =
  934. mm->notifier_subscriptions;
  935. unsigned long seq = 0;
  936. might_sleep();
  937. spin_lock(&subscriptions->lock);
  938. if (mn_itree_is_invalidating(subscriptions)) {
  939. /*
  940. * remove is being called after insert put this on the
  941. * deferred list, but before the deferred list was processed.
  942. */
  943. if (RB_EMPTY_NODE(&interval_sub->interval_tree.rb)) {
  944. hlist_del(&interval_sub->deferred_item);
  945. } else {
  946. hlist_add_head(&interval_sub->deferred_item,
  947. &subscriptions->deferred_list);
  948. seq = subscriptions->invalidate_seq;
  949. }
  950. } else {
  951. WARN_ON(RB_EMPTY_NODE(&interval_sub->interval_tree.rb));
  952. interval_tree_remove(&interval_sub->interval_tree,
  953. &subscriptions->itree);
  954. }
  955. spin_unlock(&subscriptions->lock);
  956. /*
  957. * The possible sleep on progress in the invalidation requires the
  958. * caller not hold any locks held by invalidation callbacks.
  959. */
  960. lock_map_acquire(&__mmu_notifier_invalidate_range_start_map);
  961. lock_map_release(&__mmu_notifier_invalidate_range_start_map);
  962. if (seq)
  963. wait_event(subscriptions->wq,
  964. mmu_interval_seq_released(subscriptions, seq));
  965. /* pairs with mmgrab in mmu_interval_notifier_insert() */
  966. mmdrop(mm);
  967. }
  968. EXPORT_SYMBOL_GPL(mmu_interval_notifier_remove);
  969. /**
  970. * mmu_notifier_synchronize - Ensure all mmu_notifiers are freed
  971. *
  972. * This function ensures that all outstanding async SRU work from
  973. * mmu_notifier_put() is completed. After it returns any mmu_notifier_ops
  974. * associated with an unused mmu_notifier will no longer be called.
  975. *
  976. * Before using the caller must ensure that all of its mmu_notifiers have been
  977. * fully released via mmu_notifier_put().
  978. *
  979. * Modules using the mmu_notifier_put() API should call this in their __exit
  980. * function to avoid module unloading races.
  981. */
  982. void mmu_notifier_synchronize(void)
  983. {
  984. synchronize_srcu(&srcu);
  985. }
  986. EXPORT_SYMBOL_GPL(mmu_notifier_synchronize);