iova.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright © 2006-2009, Intel Corporation.
  4. *
  5. * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
  6. */
  7. #include <linux/iova.h>
  8. #include <linux/kmemleak.h>
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/smp.h>
  12. #include <linux/bitops.h>
  13. #include <linux/cpu.h>
  14. #include <linux/workqueue.h>
  15. /* The anchor node sits above the top of the usable address space */
  16. #define IOVA_ANCHOR ~0UL
  17. #define IOVA_RANGE_CACHE_MAX_SIZE 6 /* log of max cached IOVA range size (in pages) */
  18. static bool iova_rcache_insert(struct iova_domain *iovad,
  19. unsigned long pfn,
  20. unsigned long size);
  21. static unsigned long iova_rcache_get(struct iova_domain *iovad,
  22. unsigned long size,
  23. unsigned long limit_pfn);
  24. static void free_iova_rcaches(struct iova_domain *iovad);
  25. static void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad);
  26. static void free_global_cached_iovas(struct iova_domain *iovad);
  27. static struct iova *to_iova(struct rb_node *node)
  28. {
  29. return rb_entry(node, struct iova, node);
  30. }
  31. void
  32. init_iova_domain(struct iova_domain *iovad, unsigned long granule,
  33. unsigned long start_pfn)
  34. {
  35. /*
  36. * IOVA granularity will normally be equal to the smallest
  37. * supported IOMMU page size; both *must* be capable of
  38. * representing individual CPU pages exactly.
  39. */
  40. BUG_ON((granule > PAGE_SIZE) || !is_power_of_2(granule));
  41. spin_lock_init(&iovad->iova_rbtree_lock);
  42. iovad->rbroot = RB_ROOT;
  43. iovad->cached_node = &iovad->anchor.node;
  44. iovad->cached32_node = &iovad->anchor.node;
  45. iovad->granule = granule;
  46. iovad->start_pfn = start_pfn;
  47. iovad->dma_32bit_pfn = 1UL << (32 - iova_shift(iovad));
  48. iovad->max32_alloc_size = iovad->dma_32bit_pfn;
  49. iovad->anchor.pfn_lo = iovad->anchor.pfn_hi = IOVA_ANCHOR;
  50. rb_link_node(&iovad->anchor.node, NULL, &iovad->rbroot.rb_node);
  51. rb_insert_color(&iovad->anchor.node, &iovad->rbroot);
  52. }
  53. EXPORT_SYMBOL_GPL(init_iova_domain);
  54. static struct rb_node *
  55. __get_cached_rbnode(struct iova_domain *iovad, unsigned long limit_pfn)
  56. {
  57. if (limit_pfn <= iovad->dma_32bit_pfn)
  58. return iovad->cached32_node;
  59. return iovad->cached_node;
  60. }
  61. static void
  62. __cached_rbnode_insert_update(struct iova_domain *iovad, struct iova *new)
  63. {
  64. if (new->pfn_hi < iovad->dma_32bit_pfn)
  65. iovad->cached32_node = &new->node;
  66. else
  67. iovad->cached_node = &new->node;
  68. }
  69. static void
  70. __cached_rbnode_delete_update(struct iova_domain *iovad, struct iova *free)
  71. {
  72. struct iova *cached_iova;
  73. cached_iova = to_iova(iovad->cached32_node);
  74. if (free == cached_iova ||
  75. (free->pfn_hi < iovad->dma_32bit_pfn &&
  76. free->pfn_lo >= cached_iova->pfn_lo))
  77. iovad->cached32_node = rb_next(&free->node);
  78. if (free->pfn_lo < iovad->dma_32bit_pfn)
  79. iovad->max32_alloc_size = iovad->dma_32bit_pfn;
  80. cached_iova = to_iova(iovad->cached_node);
  81. if (free->pfn_lo >= cached_iova->pfn_lo)
  82. iovad->cached_node = rb_next(&free->node);
  83. }
  84. static struct rb_node *iova_find_limit(struct iova_domain *iovad, unsigned long limit_pfn)
  85. {
  86. struct rb_node *node, *next;
  87. /*
  88. * Ideally what we'd like to judge here is whether limit_pfn is close
  89. * enough to the highest-allocated IOVA that starting the allocation
  90. * walk from the anchor node will be quicker than this initial work to
  91. * find an exact starting point (especially if that ends up being the
  92. * anchor node anyway). This is an incredibly crude approximation which
  93. * only really helps the most likely case, but is at least trivially easy.
  94. */
  95. if (limit_pfn > iovad->dma_32bit_pfn)
  96. return &iovad->anchor.node;
  97. node = iovad->rbroot.rb_node;
  98. while (to_iova(node)->pfn_hi < limit_pfn)
  99. node = node->rb_right;
  100. search_left:
  101. while (node->rb_left && to_iova(node->rb_left)->pfn_lo >= limit_pfn)
  102. node = node->rb_left;
  103. if (!node->rb_left)
  104. return node;
  105. next = node->rb_left;
  106. while (next->rb_right) {
  107. next = next->rb_right;
  108. if (to_iova(next)->pfn_lo >= limit_pfn) {
  109. node = next;
  110. goto search_left;
  111. }
  112. }
  113. return node;
  114. }
  115. /* Insert the iova into domain rbtree by holding writer lock */
  116. static void
  117. iova_insert_rbtree(struct rb_root *root, struct iova *iova,
  118. struct rb_node *start)
  119. {
  120. struct rb_node **new, *parent = NULL;
  121. new = (start) ? &start : &(root->rb_node);
  122. /* Figure out where to put new node */
  123. while (*new) {
  124. struct iova *this = to_iova(*new);
  125. parent = *new;
  126. if (iova->pfn_lo < this->pfn_lo)
  127. new = &((*new)->rb_left);
  128. else if (iova->pfn_lo > this->pfn_lo)
  129. new = &((*new)->rb_right);
  130. else {
  131. WARN_ON(1); /* this should not happen */
  132. return;
  133. }
  134. }
  135. /* Add new node and rebalance tree. */
  136. rb_link_node(&iova->node, parent, new);
  137. rb_insert_color(&iova->node, root);
  138. }
  139. static int __alloc_and_insert_iova_range(struct iova_domain *iovad,
  140. unsigned long size, unsigned long limit_pfn,
  141. struct iova *new, bool size_aligned)
  142. {
  143. struct rb_node *curr, *prev;
  144. struct iova *curr_iova;
  145. unsigned long flags;
  146. unsigned long new_pfn, retry_pfn;
  147. unsigned long align_mask = ~0UL;
  148. unsigned long high_pfn = limit_pfn, low_pfn = iovad->start_pfn;
  149. if (size_aligned)
  150. align_mask <<= fls_long(size - 1);
  151. /* Walk the tree backwards */
  152. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  153. if (limit_pfn <= iovad->dma_32bit_pfn &&
  154. size >= iovad->max32_alloc_size)
  155. goto iova32_full;
  156. curr = __get_cached_rbnode(iovad, limit_pfn);
  157. curr_iova = to_iova(curr);
  158. retry_pfn = curr_iova->pfn_hi;
  159. retry:
  160. do {
  161. high_pfn = min(high_pfn, curr_iova->pfn_lo);
  162. new_pfn = (high_pfn - size) & align_mask;
  163. prev = curr;
  164. curr = rb_prev(curr);
  165. curr_iova = to_iova(curr);
  166. } while (curr && new_pfn <= curr_iova->pfn_hi && new_pfn >= low_pfn);
  167. if (high_pfn < size || new_pfn < low_pfn) {
  168. if (low_pfn == iovad->start_pfn && retry_pfn < limit_pfn) {
  169. high_pfn = limit_pfn;
  170. low_pfn = retry_pfn + 1;
  171. curr = iova_find_limit(iovad, limit_pfn);
  172. curr_iova = to_iova(curr);
  173. goto retry;
  174. }
  175. iovad->max32_alloc_size = size;
  176. goto iova32_full;
  177. }
  178. /* pfn_lo will point to size aligned address if size_aligned is set */
  179. new->pfn_lo = new_pfn;
  180. new->pfn_hi = new->pfn_lo + size - 1;
  181. /* If we have 'prev', it's a valid place to start the insertion. */
  182. iova_insert_rbtree(&iovad->rbroot, new, prev);
  183. __cached_rbnode_insert_update(iovad, new);
  184. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  185. return 0;
  186. iova32_full:
  187. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  188. return -ENOMEM;
  189. }
  190. static struct kmem_cache *iova_cache;
  191. static unsigned int iova_cache_users;
  192. static DEFINE_MUTEX(iova_cache_mutex);
  193. static struct iova *alloc_iova_mem(void)
  194. {
  195. return kmem_cache_zalloc(iova_cache, GFP_ATOMIC | __GFP_NOWARN);
  196. }
  197. static void free_iova_mem(struct iova *iova)
  198. {
  199. if (iova->pfn_lo != IOVA_ANCHOR)
  200. kmem_cache_free(iova_cache, iova);
  201. }
  202. /**
  203. * alloc_iova - allocates an iova
  204. * @iovad: - iova domain in question
  205. * @size: - size of page frames to allocate
  206. * @limit_pfn: - max limit address
  207. * @size_aligned: - set if size_aligned address range is required
  208. * This function allocates an iova in the range iovad->start_pfn to limit_pfn,
  209. * searching top-down from limit_pfn to iovad->start_pfn. If the size_aligned
  210. * flag is set then the allocated address iova->pfn_lo will be naturally
  211. * aligned on roundup_power_of_two(size).
  212. */
  213. struct iova *
  214. alloc_iova(struct iova_domain *iovad, unsigned long size,
  215. unsigned long limit_pfn,
  216. bool size_aligned)
  217. {
  218. struct iova *new_iova;
  219. int ret;
  220. new_iova = alloc_iova_mem();
  221. if (!new_iova)
  222. return NULL;
  223. ret = __alloc_and_insert_iova_range(iovad, size, limit_pfn + 1,
  224. new_iova, size_aligned);
  225. if (ret) {
  226. free_iova_mem(new_iova);
  227. return NULL;
  228. }
  229. return new_iova;
  230. }
  231. EXPORT_SYMBOL_GPL(alloc_iova);
  232. static struct iova *
  233. private_find_iova(struct iova_domain *iovad, unsigned long pfn)
  234. {
  235. struct rb_node *node = iovad->rbroot.rb_node;
  236. assert_spin_locked(&iovad->iova_rbtree_lock);
  237. while (node) {
  238. struct iova *iova = to_iova(node);
  239. if (pfn < iova->pfn_lo)
  240. node = node->rb_left;
  241. else if (pfn > iova->pfn_hi)
  242. node = node->rb_right;
  243. else
  244. return iova; /* pfn falls within iova's range */
  245. }
  246. return NULL;
  247. }
  248. static void remove_iova(struct iova_domain *iovad, struct iova *iova)
  249. {
  250. assert_spin_locked(&iovad->iova_rbtree_lock);
  251. __cached_rbnode_delete_update(iovad, iova);
  252. rb_erase(&iova->node, &iovad->rbroot);
  253. }
  254. /**
  255. * find_iova - finds an iova for a given pfn
  256. * @iovad: - iova domain in question.
  257. * @pfn: - page frame number
  258. * This function finds and returns an iova belonging to the
  259. * given domain which matches the given pfn.
  260. */
  261. struct iova *find_iova(struct iova_domain *iovad, unsigned long pfn)
  262. {
  263. unsigned long flags;
  264. struct iova *iova;
  265. /* Take the lock so that no other thread is manipulating the rbtree */
  266. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  267. iova = private_find_iova(iovad, pfn);
  268. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  269. return iova;
  270. }
  271. EXPORT_SYMBOL_GPL(find_iova);
  272. /**
  273. * __free_iova - frees the given iova
  274. * @iovad: iova domain in question.
  275. * @iova: iova in question.
  276. * Frees the given iova belonging to the giving domain
  277. */
  278. void
  279. __free_iova(struct iova_domain *iovad, struct iova *iova)
  280. {
  281. unsigned long flags;
  282. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  283. remove_iova(iovad, iova);
  284. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  285. free_iova_mem(iova);
  286. }
  287. EXPORT_SYMBOL_GPL(__free_iova);
  288. /**
  289. * free_iova - finds and frees the iova for a given pfn
  290. * @iovad: - iova domain in question.
  291. * @pfn: - pfn that is allocated previously
  292. * This functions finds an iova for a given pfn and then
  293. * frees the iova from that domain.
  294. */
  295. void
  296. free_iova(struct iova_domain *iovad, unsigned long pfn)
  297. {
  298. unsigned long flags;
  299. struct iova *iova;
  300. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  301. iova = private_find_iova(iovad, pfn);
  302. if (!iova) {
  303. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  304. return;
  305. }
  306. remove_iova(iovad, iova);
  307. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  308. free_iova_mem(iova);
  309. }
  310. EXPORT_SYMBOL_GPL(free_iova);
  311. /**
  312. * alloc_iova_fast - allocates an iova from rcache
  313. * @iovad: - iova domain in question
  314. * @size: - size of page frames to allocate
  315. * @limit_pfn: - max limit address
  316. * @flush_rcache: - set to flush rcache on regular allocation failure
  317. * This function tries to satisfy an iova allocation from the rcache,
  318. * and falls back to regular allocation on failure. If regular allocation
  319. * fails too and the flush_rcache flag is set then the rcache will be flushed.
  320. */
  321. unsigned long
  322. alloc_iova_fast(struct iova_domain *iovad, unsigned long size,
  323. unsigned long limit_pfn, bool flush_rcache)
  324. {
  325. unsigned long iova_pfn;
  326. struct iova *new_iova;
  327. /*
  328. * Freeing non-power-of-two-sized allocations back into the IOVA caches
  329. * will come back to bite us badly, so we have to waste a bit of space
  330. * rounding up anything cacheable to make sure that can't happen. The
  331. * order of the unadjusted size will still match upon freeing.
  332. */
  333. if (size < (1 << (IOVA_RANGE_CACHE_MAX_SIZE - 1)))
  334. size = roundup_pow_of_two(size);
  335. iova_pfn = iova_rcache_get(iovad, size, limit_pfn + 1);
  336. if (iova_pfn)
  337. return iova_pfn;
  338. retry:
  339. new_iova = alloc_iova(iovad, size, limit_pfn, true);
  340. if (!new_iova) {
  341. unsigned int cpu;
  342. if (!flush_rcache)
  343. return 0;
  344. /* Try replenishing IOVAs by flushing rcache. */
  345. flush_rcache = false;
  346. for_each_online_cpu(cpu)
  347. free_cpu_cached_iovas(cpu, iovad);
  348. free_global_cached_iovas(iovad);
  349. goto retry;
  350. }
  351. return new_iova->pfn_lo;
  352. }
  353. EXPORT_SYMBOL_GPL(alloc_iova_fast);
  354. /**
  355. * free_iova_fast - free iova pfn range into rcache
  356. * @iovad: - iova domain in question.
  357. * @pfn: - pfn that is allocated previously
  358. * @size: - # of pages in range
  359. * This functions frees an iova range by trying to put it into the rcache,
  360. * falling back to regular iova deallocation via free_iova() if this fails.
  361. */
  362. void
  363. free_iova_fast(struct iova_domain *iovad, unsigned long pfn, unsigned long size)
  364. {
  365. if (iova_rcache_insert(iovad, pfn, size))
  366. return;
  367. free_iova(iovad, pfn);
  368. }
  369. EXPORT_SYMBOL_GPL(free_iova_fast);
  370. static void iova_domain_free_rcaches(struct iova_domain *iovad)
  371. {
  372. cpuhp_state_remove_instance_nocalls(CPUHP_IOMMU_IOVA_DEAD,
  373. &iovad->cpuhp_dead);
  374. free_iova_rcaches(iovad);
  375. }
  376. /**
  377. * put_iova_domain - destroys the iova domain
  378. * @iovad: - iova domain in question.
  379. * All the iova's in that domain are destroyed.
  380. */
  381. void put_iova_domain(struct iova_domain *iovad)
  382. {
  383. struct iova *iova, *tmp;
  384. if (iovad->rcaches)
  385. iova_domain_free_rcaches(iovad);
  386. rbtree_postorder_for_each_entry_safe(iova, tmp, &iovad->rbroot, node)
  387. free_iova_mem(iova);
  388. }
  389. EXPORT_SYMBOL_GPL(put_iova_domain);
  390. static int
  391. __is_range_overlap(struct rb_node *node,
  392. unsigned long pfn_lo, unsigned long pfn_hi)
  393. {
  394. struct iova *iova = to_iova(node);
  395. if ((pfn_lo <= iova->pfn_hi) && (pfn_hi >= iova->pfn_lo))
  396. return 1;
  397. return 0;
  398. }
  399. static inline struct iova *
  400. alloc_and_init_iova(unsigned long pfn_lo, unsigned long pfn_hi)
  401. {
  402. struct iova *iova;
  403. iova = alloc_iova_mem();
  404. if (iova) {
  405. iova->pfn_lo = pfn_lo;
  406. iova->pfn_hi = pfn_hi;
  407. }
  408. return iova;
  409. }
  410. static struct iova *
  411. __insert_new_range(struct iova_domain *iovad,
  412. unsigned long pfn_lo, unsigned long pfn_hi)
  413. {
  414. struct iova *iova;
  415. iova = alloc_and_init_iova(pfn_lo, pfn_hi);
  416. if (iova)
  417. iova_insert_rbtree(&iovad->rbroot, iova, NULL);
  418. return iova;
  419. }
  420. static void
  421. __adjust_overlap_range(struct iova *iova,
  422. unsigned long *pfn_lo, unsigned long *pfn_hi)
  423. {
  424. if (*pfn_lo < iova->pfn_lo)
  425. iova->pfn_lo = *pfn_lo;
  426. if (*pfn_hi > iova->pfn_hi)
  427. *pfn_lo = iova->pfn_hi + 1;
  428. }
  429. /**
  430. * reserve_iova - reserves an iova in the given range
  431. * @iovad: - iova domain pointer
  432. * @pfn_lo: - lower page frame address
  433. * @pfn_hi:- higher pfn address
  434. * This function allocates reserves the address range from pfn_lo to pfn_hi so
  435. * that this address is not dished out as part of alloc_iova.
  436. */
  437. struct iova *
  438. reserve_iova(struct iova_domain *iovad,
  439. unsigned long pfn_lo, unsigned long pfn_hi)
  440. {
  441. struct rb_node *node;
  442. unsigned long flags;
  443. struct iova *iova;
  444. unsigned int overlap = 0;
  445. /* Don't allow nonsensical pfns */
  446. if (WARN_ON((pfn_hi | pfn_lo) > (ULLONG_MAX >> iova_shift(iovad))))
  447. return NULL;
  448. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  449. for (node = rb_first(&iovad->rbroot); node; node = rb_next(node)) {
  450. if (__is_range_overlap(node, pfn_lo, pfn_hi)) {
  451. iova = to_iova(node);
  452. __adjust_overlap_range(iova, &pfn_lo, &pfn_hi);
  453. if ((pfn_lo >= iova->pfn_lo) &&
  454. (pfn_hi <= iova->pfn_hi))
  455. goto finish;
  456. overlap = 1;
  457. } else if (overlap)
  458. break;
  459. }
  460. /* We are here either because this is the first reserver node
  461. * or need to insert remaining non overlap addr range
  462. */
  463. iova = __insert_new_range(iovad, pfn_lo, pfn_hi);
  464. finish:
  465. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  466. return iova;
  467. }
  468. EXPORT_SYMBOL_GPL(reserve_iova);
  469. /*
  470. * Magazine caches for IOVA ranges. For an introduction to magazines,
  471. * see the USENIX 2001 paper "Magazines and Vmem: Extending the Slab
  472. * Allocator to Many CPUs and Arbitrary Resources" by Bonwick and Adams.
  473. * For simplicity, we use a static magazine size and don't implement the
  474. * dynamic size tuning described in the paper.
  475. */
  476. /*
  477. * As kmalloc's buffer size is fixed to power of 2, 127 is chosen to
  478. * assure size of 'iova_magazine' to be 1024 bytes, so that no memory
  479. * will be wasted. Since only full magazines are inserted into the depot,
  480. * we don't need to waste PFN capacity on a separate list head either.
  481. */
  482. #define IOVA_MAG_SIZE 127
  483. #define IOVA_DEPOT_DELAY msecs_to_jiffies(100)
  484. struct iova_magazine {
  485. union {
  486. unsigned long size;
  487. struct iova_magazine *next;
  488. };
  489. unsigned long pfns[IOVA_MAG_SIZE];
  490. };
  491. static_assert(!(sizeof(struct iova_magazine) & (sizeof(struct iova_magazine) - 1)));
  492. struct iova_cpu_rcache {
  493. spinlock_t lock;
  494. struct iova_magazine *loaded;
  495. struct iova_magazine *prev;
  496. };
  497. struct iova_rcache {
  498. spinlock_t lock;
  499. unsigned int depot_size;
  500. struct iova_magazine *depot;
  501. struct iova_cpu_rcache __percpu *cpu_rcaches;
  502. struct iova_domain *iovad;
  503. struct delayed_work work;
  504. };
  505. static struct kmem_cache *iova_magazine_cache;
  506. unsigned long iova_rcache_range(void)
  507. {
  508. return PAGE_SIZE << (IOVA_RANGE_CACHE_MAX_SIZE - 1);
  509. }
  510. static struct iova_magazine *iova_magazine_alloc(gfp_t flags)
  511. {
  512. struct iova_magazine *mag;
  513. mag = kmem_cache_alloc(iova_magazine_cache, flags);
  514. if (mag)
  515. mag->size = 0;
  516. return mag;
  517. }
  518. static void iova_magazine_free(struct iova_magazine *mag)
  519. {
  520. kmem_cache_free(iova_magazine_cache, mag);
  521. }
  522. static void
  523. iova_magazine_free_pfns(struct iova_magazine *mag, struct iova_domain *iovad)
  524. {
  525. unsigned long flags;
  526. int i;
  527. spin_lock_irqsave(&iovad->iova_rbtree_lock, flags);
  528. for (i = 0 ; i < mag->size; ++i) {
  529. struct iova *iova = private_find_iova(iovad, mag->pfns[i]);
  530. if (WARN_ON(!iova))
  531. continue;
  532. remove_iova(iovad, iova);
  533. free_iova_mem(iova);
  534. }
  535. spin_unlock_irqrestore(&iovad->iova_rbtree_lock, flags);
  536. mag->size = 0;
  537. }
  538. static bool iova_magazine_full(struct iova_magazine *mag)
  539. {
  540. return mag->size == IOVA_MAG_SIZE;
  541. }
  542. static bool iova_magazine_empty(struct iova_magazine *mag)
  543. {
  544. return mag->size == 0;
  545. }
  546. static unsigned long iova_magazine_pop(struct iova_magazine *mag,
  547. unsigned long limit_pfn)
  548. {
  549. int i;
  550. unsigned long pfn;
  551. /* Only fall back to the rbtree if we have no suitable pfns at all */
  552. for (i = mag->size - 1; mag->pfns[i] > limit_pfn; i--)
  553. if (i == 0)
  554. return 0;
  555. /* Swap it to pop it */
  556. pfn = mag->pfns[i];
  557. mag->pfns[i] = mag->pfns[--mag->size];
  558. return pfn;
  559. }
  560. static void iova_magazine_push(struct iova_magazine *mag, unsigned long pfn)
  561. {
  562. mag->pfns[mag->size++] = pfn;
  563. }
  564. static struct iova_magazine *iova_depot_pop(struct iova_rcache *rcache)
  565. {
  566. struct iova_magazine *mag = rcache->depot;
  567. /*
  568. * As the mag->next pointer is moved to rcache->depot and reset via
  569. * the mag->size assignment, mark it as a transient false positive.
  570. */
  571. kmemleak_transient_leak(mag->next);
  572. rcache->depot = mag->next;
  573. mag->size = IOVA_MAG_SIZE;
  574. rcache->depot_size--;
  575. return mag;
  576. }
  577. static void iova_depot_push(struct iova_rcache *rcache, struct iova_magazine *mag)
  578. {
  579. mag->next = rcache->depot;
  580. rcache->depot = mag;
  581. rcache->depot_size++;
  582. }
  583. static void iova_depot_work_func(struct work_struct *work)
  584. {
  585. struct iova_rcache *rcache = container_of(work, typeof(*rcache), work.work);
  586. struct iova_magazine *mag = NULL;
  587. unsigned long flags;
  588. spin_lock_irqsave(&rcache->lock, flags);
  589. if (rcache->depot_size > num_online_cpus())
  590. mag = iova_depot_pop(rcache);
  591. spin_unlock_irqrestore(&rcache->lock, flags);
  592. if (mag) {
  593. iova_magazine_free_pfns(mag, rcache->iovad);
  594. iova_magazine_free(mag);
  595. schedule_delayed_work(&rcache->work, IOVA_DEPOT_DELAY);
  596. }
  597. }
  598. int iova_domain_init_rcaches(struct iova_domain *iovad)
  599. {
  600. unsigned int cpu;
  601. int i, ret;
  602. iovad->rcaches = kzalloc_objs(struct iova_rcache,
  603. IOVA_RANGE_CACHE_MAX_SIZE);
  604. if (!iovad->rcaches)
  605. return -ENOMEM;
  606. for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
  607. struct iova_cpu_rcache *cpu_rcache;
  608. struct iova_rcache *rcache;
  609. rcache = &iovad->rcaches[i];
  610. spin_lock_init(&rcache->lock);
  611. rcache->iovad = iovad;
  612. INIT_DELAYED_WORK(&rcache->work, iova_depot_work_func);
  613. rcache->cpu_rcaches = __alloc_percpu(sizeof(*cpu_rcache),
  614. cache_line_size());
  615. if (!rcache->cpu_rcaches) {
  616. ret = -ENOMEM;
  617. goto out_err;
  618. }
  619. for_each_possible_cpu(cpu) {
  620. cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
  621. spin_lock_init(&cpu_rcache->lock);
  622. cpu_rcache->loaded = iova_magazine_alloc(GFP_KERNEL);
  623. cpu_rcache->prev = iova_magazine_alloc(GFP_KERNEL);
  624. if (!cpu_rcache->loaded || !cpu_rcache->prev) {
  625. ret = -ENOMEM;
  626. goto out_err;
  627. }
  628. }
  629. }
  630. ret = cpuhp_state_add_instance_nocalls(CPUHP_IOMMU_IOVA_DEAD,
  631. &iovad->cpuhp_dead);
  632. if (ret)
  633. goto out_err;
  634. return 0;
  635. out_err:
  636. free_iova_rcaches(iovad);
  637. return ret;
  638. }
  639. EXPORT_SYMBOL_GPL(iova_domain_init_rcaches);
  640. /*
  641. * Try inserting IOVA range starting with 'iova_pfn' into 'rcache', and
  642. * return true on success. Can fail if rcache is full and we can't free
  643. * space, and free_iova() (our only caller) will then return the IOVA
  644. * range to the rbtree instead.
  645. */
  646. static bool __iova_rcache_insert(struct iova_domain *iovad,
  647. struct iova_rcache *rcache,
  648. unsigned long iova_pfn)
  649. {
  650. struct iova_cpu_rcache *cpu_rcache;
  651. bool can_insert = false;
  652. unsigned long flags;
  653. cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
  654. spin_lock_irqsave(&cpu_rcache->lock, flags);
  655. if (!iova_magazine_full(cpu_rcache->loaded)) {
  656. can_insert = true;
  657. } else if (!iova_magazine_full(cpu_rcache->prev)) {
  658. swap(cpu_rcache->prev, cpu_rcache->loaded);
  659. can_insert = true;
  660. } else {
  661. struct iova_magazine *new_mag = iova_magazine_alloc(GFP_ATOMIC);
  662. if (new_mag) {
  663. spin_lock(&rcache->lock);
  664. iova_depot_push(rcache, cpu_rcache->loaded);
  665. spin_unlock(&rcache->lock);
  666. schedule_delayed_work(&rcache->work, IOVA_DEPOT_DELAY);
  667. cpu_rcache->loaded = new_mag;
  668. can_insert = true;
  669. }
  670. }
  671. if (can_insert)
  672. iova_magazine_push(cpu_rcache->loaded, iova_pfn);
  673. spin_unlock_irqrestore(&cpu_rcache->lock, flags);
  674. return can_insert;
  675. }
  676. static bool iova_rcache_insert(struct iova_domain *iovad, unsigned long pfn,
  677. unsigned long size)
  678. {
  679. unsigned int log_size = order_base_2(size);
  680. if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
  681. return false;
  682. return __iova_rcache_insert(iovad, &iovad->rcaches[log_size], pfn);
  683. }
  684. /*
  685. * Caller wants to allocate a new IOVA range from 'rcache'. If we can
  686. * satisfy the request, return a matching non-NULL range and remove
  687. * it from the 'rcache'.
  688. */
  689. static unsigned long __iova_rcache_get(struct iova_rcache *rcache,
  690. unsigned long limit_pfn)
  691. {
  692. struct iova_cpu_rcache *cpu_rcache;
  693. unsigned long iova_pfn = 0;
  694. bool has_pfn = false;
  695. unsigned long flags;
  696. cpu_rcache = raw_cpu_ptr(rcache->cpu_rcaches);
  697. spin_lock_irqsave(&cpu_rcache->lock, flags);
  698. if (!iova_magazine_empty(cpu_rcache->loaded)) {
  699. has_pfn = true;
  700. } else if (!iova_magazine_empty(cpu_rcache->prev)) {
  701. swap(cpu_rcache->prev, cpu_rcache->loaded);
  702. has_pfn = true;
  703. } else {
  704. spin_lock(&rcache->lock);
  705. if (rcache->depot) {
  706. iova_magazine_free(cpu_rcache->loaded);
  707. cpu_rcache->loaded = iova_depot_pop(rcache);
  708. has_pfn = true;
  709. }
  710. spin_unlock(&rcache->lock);
  711. }
  712. if (has_pfn)
  713. iova_pfn = iova_magazine_pop(cpu_rcache->loaded, limit_pfn);
  714. spin_unlock_irqrestore(&cpu_rcache->lock, flags);
  715. return iova_pfn;
  716. }
  717. /*
  718. * Try to satisfy IOVA allocation range from rcache. Fail if requested
  719. * size is too big or the DMA limit we are given isn't satisfied by the
  720. * top element in the magazine.
  721. */
  722. static unsigned long iova_rcache_get(struct iova_domain *iovad,
  723. unsigned long size,
  724. unsigned long limit_pfn)
  725. {
  726. unsigned int log_size = order_base_2(size);
  727. if (log_size >= IOVA_RANGE_CACHE_MAX_SIZE)
  728. return 0;
  729. return __iova_rcache_get(&iovad->rcaches[log_size], limit_pfn - size);
  730. }
  731. /*
  732. * free rcache data structures.
  733. */
  734. static void free_iova_rcaches(struct iova_domain *iovad)
  735. {
  736. struct iova_rcache *rcache;
  737. struct iova_cpu_rcache *cpu_rcache;
  738. unsigned int cpu;
  739. for (int i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
  740. rcache = &iovad->rcaches[i];
  741. if (!rcache->cpu_rcaches)
  742. break;
  743. for_each_possible_cpu(cpu) {
  744. cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
  745. iova_magazine_free(cpu_rcache->loaded);
  746. iova_magazine_free(cpu_rcache->prev);
  747. }
  748. free_percpu(rcache->cpu_rcaches);
  749. cancel_delayed_work_sync(&rcache->work);
  750. while (rcache->depot)
  751. iova_magazine_free(iova_depot_pop(rcache));
  752. }
  753. kfree(iovad->rcaches);
  754. iovad->rcaches = NULL;
  755. }
  756. /*
  757. * free all the IOVA ranges cached by a cpu (used when cpu is unplugged)
  758. */
  759. static void free_cpu_cached_iovas(unsigned int cpu, struct iova_domain *iovad)
  760. {
  761. struct iova_cpu_rcache *cpu_rcache;
  762. struct iova_rcache *rcache;
  763. unsigned long flags;
  764. int i;
  765. for (i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
  766. rcache = &iovad->rcaches[i];
  767. cpu_rcache = per_cpu_ptr(rcache->cpu_rcaches, cpu);
  768. spin_lock_irqsave(&cpu_rcache->lock, flags);
  769. iova_magazine_free_pfns(cpu_rcache->loaded, iovad);
  770. iova_magazine_free_pfns(cpu_rcache->prev, iovad);
  771. spin_unlock_irqrestore(&cpu_rcache->lock, flags);
  772. }
  773. }
  774. /*
  775. * free all the IOVA ranges of global cache
  776. */
  777. static void free_global_cached_iovas(struct iova_domain *iovad)
  778. {
  779. struct iova_rcache *rcache;
  780. unsigned long flags;
  781. for (int i = 0; i < IOVA_RANGE_CACHE_MAX_SIZE; ++i) {
  782. rcache = &iovad->rcaches[i];
  783. spin_lock_irqsave(&rcache->lock, flags);
  784. while (rcache->depot) {
  785. struct iova_magazine *mag = iova_depot_pop(rcache);
  786. iova_magazine_free_pfns(mag, iovad);
  787. iova_magazine_free(mag);
  788. }
  789. spin_unlock_irqrestore(&rcache->lock, flags);
  790. }
  791. }
  792. static int iova_cpuhp_dead(unsigned int cpu, struct hlist_node *node)
  793. {
  794. struct iova_domain *iovad;
  795. iovad = hlist_entry_safe(node, struct iova_domain, cpuhp_dead);
  796. free_cpu_cached_iovas(cpu, iovad);
  797. return 0;
  798. }
  799. int iova_cache_get(void)
  800. {
  801. int err = -ENOMEM;
  802. mutex_lock(&iova_cache_mutex);
  803. if (!iova_cache_users) {
  804. iova_cache = kmem_cache_create("iommu_iova", sizeof(struct iova), 0,
  805. SLAB_HWCACHE_ALIGN, NULL);
  806. if (!iova_cache)
  807. goto out_err;
  808. iova_magazine_cache = kmem_cache_create("iommu_iova_magazine",
  809. sizeof(struct iova_magazine),
  810. 0, SLAB_HWCACHE_ALIGN, NULL);
  811. if (!iova_magazine_cache)
  812. goto out_err;
  813. err = cpuhp_setup_state_multi(CPUHP_IOMMU_IOVA_DEAD, "iommu/iova:dead",
  814. NULL, iova_cpuhp_dead);
  815. if (err) {
  816. pr_err("IOVA: Couldn't register cpuhp handler: %pe\n", ERR_PTR(err));
  817. goto out_err;
  818. }
  819. }
  820. iova_cache_users++;
  821. mutex_unlock(&iova_cache_mutex);
  822. return 0;
  823. out_err:
  824. kmem_cache_destroy(iova_cache);
  825. kmem_cache_destroy(iova_magazine_cache);
  826. mutex_unlock(&iova_cache_mutex);
  827. return err;
  828. }
  829. EXPORT_SYMBOL_GPL(iova_cache_get);
  830. void iova_cache_put(void)
  831. {
  832. mutex_lock(&iova_cache_mutex);
  833. if (WARN_ON(!iova_cache_users)) {
  834. mutex_unlock(&iova_cache_mutex);
  835. return;
  836. }
  837. iova_cache_users--;
  838. if (!iova_cache_users) {
  839. cpuhp_remove_multi_state(CPUHP_IOMMU_IOVA_DEAD);
  840. kmem_cache_destroy(iova_cache);
  841. kmem_cache_destroy(iova_magazine_cache);
  842. }
  843. mutex_unlock(&iova_cache_mutex);
  844. }
  845. EXPORT_SYMBOL_GPL(iova_cache_put);
  846. MODULE_AUTHOR("Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>");
  847. MODULE_DESCRIPTION("IOMMU I/O Virtual Address management");
  848. MODULE_LICENSE("GPL");