ttm_page_alloc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  1. /*
  2. * Copyright (c) Red Hat Inc.
  3. * Permission is hereby granted, free of charge, to any person obtaining a
  4. * copy of this software and associated documentation files (the "Software"),
  5. * to deal in the Software without restriction, including without limitation
  6. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  7. * and/or sell copies of the Software, and to permit persons to whom the
  8. * Software is furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice (including the
  11. * next paragraph) shall be included in all copies or substantial portions
  12. * of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Dave Airlie <airlied@redhat.com>
  23. * Jerome Glisse <jglisse@redhat.com>
  24. * Pauli Nieminen <suokkos@gmail.com>
  25. */
  26. /* simple list based uncached page pool
  27. * - Pool collects resently freed pages for reuse
  28. * - Use page->lru to keep a free list
  29. * - doesn't track currently in use pages
  30. */
  31. #define pr_fmt(fmt) "[TTM] " fmt
  32. #include <linux/list.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/highmem.h>
  35. #include <linux/mm_types.h>
  36. #include <linux/module.h>
  37. #include <linux/mm.h>
  38. #include <linux/seq_file.h> /* for seq_printf */
  39. #include <linux/slab.h>
  40. #include <linux/dma-mapping.h>
  41. #include <linux/atomic.h>
  42. #include <drm/ttm/ttm_bo_driver.h>
  43. #include <drm/ttm/ttm_page_alloc.h>
  44. #if IS_ENABLED(CONFIG_AGP)
  45. #include <asm/agp.h>
  46. #endif
  47. #ifdef CONFIG_X86
  48. #include <asm/set_memory.h>
  49. #endif
  50. #define NUM_PAGES_TO_ALLOC (PAGE_SIZE/sizeof(struct page *))
  51. #define SMALL_ALLOCATION 16
  52. #define FREE_ALL_PAGES (~0U)
  53. /* times are in msecs */
  54. #define PAGE_FREE_INTERVAL 1000
  55. /**
  56. * struct ttm_page_pool - Pool to reuse recently allocated uc/wc pages.
  57. *
  58. * @lock: Protects the shared pool from concurrnet access. Must be used with
  59. * irqsave/irqrestore variants because pool allocator maybe called from
  60. * delayed work.
  61. * @fill_lock: Prevent concurrent calls to fill.
  62. * @list: Pool of free uc/wc pages for fast reuse.
  63. * @gfp_flags: Flags to pass for alloc_page.
  64. * @npages: Number of pages in pool.
  65. */
  66. struct ttm_page_pool {
  67. spinlock_t lock;
  68. bool fill_lock;
  69. struct list_head list;
  70. gfp_t gfp_flags;
  71. unsigned npages;
  72. char *name;
  73. unsigned long nfrees;
  74. unsigned long nrefills;
  75. unsigned int order;
  76. };
  77. /**
  78. * Limits for the pool. They are handled without locks because only place where
  79. * they may change is in sysfs store. They won't have immediate effect anyway
  80. * so forcing serialization to access them is pointless.
  81. */
  82. struct ttm_pool_opts {
  83. unsigned alloc_size;
  84. unsigned max_size;
  85. unsigned small;
  86. };
  87. #define NUM_POOLS 6
  88. /**
  89. * struct ttm_pool_manager - Holds memory pools for fst allocation
  90. *
  91. * Manager is read only object for pool code so it doesn't need locking.
  92. *
  93. * @free_interval: minimum number of jiffies between freeing pages from pool.
  94. * @page_alloc_inited: reference counting for pool allocation.
  95. * @work: Work that is used to shrink the pool. Work is only run when there is
  96. * some pages to free.
  97. * @small_allocation: Limit in number of pages what is small allocation.
  98. *
  99. * @pools: All pool objects in use.
  100. **/
  101. struct ttm_pool_manager {
  102. struct kobject kobj;
  103. struct shrinker mm_shrink;
  104. struct ttm_pool_opts options;
  105. union {
  106. struct ttm_page_pool pools[NUM_POOLS];
  107. struct {
  108. struct ttm_page_pool wc_pool;
  109. struct ttm_page_pool uc_pool;
  110. struct ttm_page_pool wc_pool_dma32;
  111. struct ttm_page_pool uc_pool_dma32;
  112. struct ttm_page_pool wc_pool_huge;
  113. struct ttm_page_pool uc_pool_huge;
  114. } ;
  115. };
  116. };
  117. static struct attribute ttm_page_pool_max = {
  118. .name = "pool_max_size",
  119. .mode = S_IRUGO | S_IWUSR
  120. };
  121. static struct attribute ttm_page_pool_small = {
  122. .name = "pool_small_allocation",
  123. .mode = S_IRUGO | S_IWUSR
  124. };
  125. static struct attribute ttm_page_pool_alloc_size = {
  126. .name = "pool_allocation_size",
  127. .mode = S_IRUGO | S_IWUSR
  128. };
  129. static struct attribute *ttm_pool_attrs[] = {
  130. &ttm_page_pool_max,
  131. &ttm_page_pool_small,
  132. &ttm_page_pool_alloc_size,
  133. NULL
  134. };
  135. static void ttm_pool_kobj_release(struct kobject *kobj)
  136. {
  137. struct ttm_pool_manager *m =
  138. container_of(kobj, struct ttm_pool_manager, kobj);
  139. kfree(m);
  140. }
  141. static ssize_t ttm_pool_store(struct kobject *kobj,
  142. struct attribute *attr, const char *buffer, size_t size)
  143. {
  144. struct ttm_pool_manager *m =
  145. container_of(kobj, struct ttm_pool_manager, kobj);
  146. int chars;
  147. unsigned val;
  148. chars = sscanf(buffer, "%u", &val);
  149. if (chars == 0)
  150. return size;
  151. /* Convert kb to number of pages */
  152. val = val / (PAGE_SIZE >> 10);
  153. if (attr == &ttm_page_pool_max)
  154. m->options.max_size = val;
  155. else if (attr == &ttm_page_pool_small)
  156. m->options.small = val;
  157. else if (attr == &ttm_page_pool_alloc_size) {
  158. if (val > NUM_PAGES_TO_ALLOC*8) {
  159. pr_err("Setting allocation size to %lu is not allowed. Recommended size is %lu\n",
  160. NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 7),
  161. NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
  162. return size;
  163. } else if (val > NUM_PAGES_TO_ALLOC) {
  164. pr_warn("Setting allocation size to larger than %lu is not recommended\n",
  165. NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
  166. }
  167. m->options.alloc_size = val;
  168. }
  169. return size;
  170. }
  171. static ssize_t ttm_pool_show(struct kobject *kobj,
  172. struct attribute *attr, char *buffer)
  173. {
  174. struct ttm_pool_manager *m =
  175. container_of(kobj, struct ttm_pool_manager, kobj);
  176. unsigned val = 0;
  177. if (attr == &ttm_page_pool_max)
  178. val = m->options.max_size;
  179. else if (attr == &ttm_page_pool_small)
  180. val = m->options.small;
  181. else if (attr == &ttm_page_pool_alloc_size)
  182. val = m->options.alloc_size;
  183. val = val * (PAGE_SIZE >> 10);
  184. return snprintf(buffer, PAGE_SIZE, "%u\n", val);
  185. }
  186. static const struct sysfs_ops ttm_pool_sysfs_ops = {
  187. .show = &ttm_pool_show,
  188. .store = &ttm_pool_store,
  189. };
  190. static struct kobj_type ttm_pool_kobj_type = {
  191. .release = &ttm_pool_kobj_release,
  192. .sysfs_ops = &ttm_pool_sysfs_ops,
  193. .default_attrs = ttm_pool_attrs,
  194. };
  195. static struct ttm_pool_manager *_manager;
  196. #ifndef CONFIG_X86
  197. static int set_pages_wb(struct page *page, int numpages)
  198. {
  199. #if IS_ENABLED(CONFIG_AGP)
  200. int i;
  201. for (i = 0; i < numpages; i++)
  202. unmap_page_from_agp(page++);
  203. #endif
  204. return 0;
  205. }
  206. static int set_pages_array_wb(struct page **pages, int addrinarray)
  207. {
  208. #if IS_ENABLED(CONFIG_AGP)
  209. int i;
  210. for (i = 0; i < addrinarray; i++)
  211. unmap_page_from_agp(pages[i]);
  212. #endif
  213. return 0;
  214. }
  215. static int set_pages_array_wc(struct page **pages, int addrinarray)
  216. {
  217. #if IS_ENABLED(CONFIG_AGP)
  218. int i;
  219. for (i = 0; i < addrinarray; i++)
  220. map_page_into_agp(pages[i]);
  221. #endif
  222. return 0;
  223. }
  224. static int set_pages_array_uc(struct page **pages, int addrinarray)
  225. {
  226. #if IS_ENABLED(CONFIG_AGP)
  227. int i;
  228. for (i = 0; i < addrinarray; i++)
  229. map_page_into_agp(pages[i]);
  230. #endif
  231. return 0;
  232. }
  233. #endif
  234. /**
  235. * Select the right pool or requested caching state and ttm flags. */
  236. static struct ttm_page_pool *ttm_get_pool(int flags, bool huge,
  237. enum ttm_caching_state cstate)
  238. {
  239. int pool_index;
  240. if (cstate == tt_cached)
  241. return NULL;
  242. if (cstate == tt_wc)
  243. pool_index = 0x0;
  244. else
  245. pool_index = 0x1;
  246. if (flags & TTM_PAGE_FLAG_DMA32) {
  247. if (huge)
  248. return NULL;
  249. pool_index |= 0x2;
  250. } else if (huge) {
  251. pool_index |= 0x4;
  252. }
  253. return &_manager->pools[pool_index];
  254. }
  255. /* set memory back to wb and free the pages. */
  256. static void ttm_pages_put(struct page *pages[], unsigned npages,
  257. unsigned int order)
  258. {
  259. unsigned int i, pages_nr = (1 << order);
  260. if (order == 0) {
  261. if (set_pages_array_wb(pages, npages))
  262. pr_err("Failed to set %d pages to wb!\n", npages);
  263. }
  264. for (i = 0; i < npages; ++i) {
  265. if (order > 0) {
  266. if (set_pages_wb(pages[i], pages_nr))
  267. pr_err("Failed to set %d pages to wb!\n", pages_nr);
  268. }
  269. __free_pages(pages[i], order);
  270. }
  271. }
  272. static void ttm_pool_update_free_locked(struct ttm_page_pool *pool,
  273. unsigned freed_pages)
  274. {
  275. pool->npages -= freed_pages;
  276. pool->nfrees += freed_pages;
  277. }
  278. /**
  279. * Free pages from pool.
  280. *
  281. * To prevent hogging the ttm_swap process we only free NUM_PAGES_TO_ALLOC
  282. * number of pages in one go.
  283. *
  284. * @pool: to free the pages from
  285. * @free_all: If set to true will free all pages in pool
  286. * @use_static: Safe to use static buffer
  287. **/
  288. static int ttm_page_pool_free(struct ttm_page_pool *pool, unsigned nr_free,
  289. bool use_static)
  290. {
  291. static struct page *static_buf[NUM_PAGES_TO_ALLOC];
  292. unsigned long irq_flags;
  293. struct page *p;
  294. struct page **pages_to_free;
  295. unsigned freed_pages = 0,
  296. npages_to_free = nr_free;
  297. if (NUM_PAGES_TO_ALLOC < nr_free)
  298. npages_to_free = NUM_PAGES_TO_ALLOC;
  299. if (use_static)
  300. pages_to_free = static_buf;
  301. else
  302. pages_to_free = kmalloc(npages_to_free * sizeof(struct page *),
  303. GFP_KERNEL);
  304. if (!pages_to_free) {
  305. pr_debug("Failed to allocate memory for pool free operation\n");
  306. return 0;
  307. }
  308. restart:
  309. spin_lock_irqsave(&pool->lock, irq_flags);
  310. list_for_each_entry_reverse(p, &pool->list, lru) {
  311. if (freed_pages >= npages_to_free)
  312. break;
  313. pages_to_free[freed_pages++] = p;
  314. /* We can only remove NUM_PAGES_TO_ALLOC at a time. */
  315. if (freed_pages >= NUM_PAGES_TO_ALLOC) {
  316. /* remove range of pages from the pool */
  317. __list_del(p->lru.prev, &pool->list);
  318. ttm_pool_update_free_locked(pool, freed_pages);
  319. /**
  320. * Because changing page caching is costly
  321. * we unlock the pool to prevent stalling.
  322. */
  323. spin_unlock_irqrestore(&pool->lock, irq_flags);
  324. ttm_pages_put(pages_to_free, freed_pages, pool->order);
  325. if (likely(nr_free != FREE_ALL_PAGES))
  326. nr_free -= freed_pages;
  327. if (NUM_PAGES_TO_ALLOC >= nr_free)
  328. npages_to_free = nr_free;
  329. else
  330. npages_to_free = NUM_PAGES_TO_ALLOC;
  331. freed_pages = 0;
  332. /* free all so restart the processing */
  333. if (nr_free)
  334. goto restart;
  335. /* Not allowed to fall through or break because
  336. * following context is inside spinlock while we are
  337. * outside here.
  338. */
  339. goto out;
  340. }
  341. }
  342. /* remove range of pages from the pool */
  343. if (freed_pages) {
  344. __list_del(&p->lru, &pool->list);
  345. ttm_pool_update_free_locked(pool, freed_pages);
  346. nr_free -= freed_pages;
  347. }
  348. spin_unlock_irqrestore(&pool->lock, irq_flags);
  349. if (freed_pages)
  350. ttm_pages_put(pages_to_free, freed_pages, pool->order);
  351. out:
  352. if (pages_to_free != static_buf)
  353. kfree(pages_to_free);
  354. return nr_free;
  355. }
  356. /**
  357. * Callback for mm to request pool to reduce number of page held.
  358. *
  359. * XXX: (dchinner) Deadlock warning!
  360. *
  361. * This code is crying out for a shrinker per pool....
  362. */
  363. static unsigned long
  364. ttm_pool_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
  365. {
  366. static DEFINE_MUTEX(lock);
  367. static unsigned start_pool;
  368. unsigned i;
  369. unsigned pool_offset;
  370. struct ttm_page_pool *pool;
  371. int shrink_pages = sc->nr_to_scan;
  372. unsigned long freed = 0;
  373. unsigned int nr_free_pool;
  374. if (!mutex_trylock(&lock))
  375. return SHRINK_STOP;
  376. pool_offset = ++start_pool % NUM_POOLS;
  377. /* select start pool in round robin fashion */
  378. for (i = 0; i < NUM_POOLS; ++i) {
  379. unsigned nr_free = shrink_pages;
  380. unsigned page_nr;
  381. if (shrink_pages == 0)
  382. break;
  383. pool = &_manager->pools[(i + pool_offset)%NUM_POOLS];
  384. page_nr = (1 << pool->order);
  385. /* OK to use static buffer since global mutex is held. */
  386. nr_free_pool = roundup(nr_free, page_nr) >> pool->order;
  387. shrink_pages = ttm_page_pool_free(pool, nr_free_pool, true);
  388. freed += (nr_free_pool - shrink_pages) << pool->order;
  389. if (freed >= sc->nr_to_scan)
  390. break;
  391. shrink_pages <<= pool->order;
  392. }
  393. mutex_unlock(&lock);
  394. return freed;
  395. }
  396. static unsigned long
  397. ttm_pool_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
  398. {
  399. unsigned i;
  400. unsigned long count = 0;
  401. struct ttm_page_pool *pool;
  402. for (i = 0; i < NUM_POOLS; ++i) {
  403. pool = &_manager->pools[i];
  404. count += (pool->npages << pool->order);
  405. }
  406. return count;
  407. }
  408. static int ttm_pool_mm_shrink_init(struct ttm_pool_manager *manager)
  409. {
  410. manager->mm_shrink.count_objects = ttm_pool_shrink_count;
  411. manager->mm_shrink.scan_objects = ttm_pool_shrink_scan;
  412. manager->mm_shrink.seeks = 1;
  413. return register_shrinker(&manager->mm_shrink);
  414. }
  415. static void ttm_pool_mm_shrink_fini(struct ttm_pool_manager *manager)
  416. {
  417. unregister_shrinker(&manager->mm_shrink);
  418. }
  419. static int ttm_set_pages_caching(struct page **pages,
  420. enum ttm_caching_state cstate, unsigned cpages)
  421. {
  422. int r = 0;
  423. /* Set page caching */
  424. switch (cstate) {
  425. case tt_uncached:
  426. r = set_pages_array_uc(pages, cpages);
  427. if (r)
  428. pr_err("Failed to set %d pages to uc!\n", cpages);
  429. break;
  430. case tt_wc:
  431. r = set_pages_array_wc(pages, cpages);
  432. if (r)
  433. pr_err("Failed to set %d pages to wc!\n", cpages);
  434. break;
  435. default:
  436. break;
  437. }
  438. return r;
  439. }
  440. /**
  441. * Free pages the pages that failed to change the caching state. If there is
  442. * any pages that have changed their caching state already put them to the
  443. * pool.
  444. */
  445. static void ttm_handle_caching_state_failure(struct list_head *pages,
  446. int ttm_flags, enum ttm_caching_state cstate,
  447. struct page **failed_pages, unsigned cpages)
  448. {
  449. unsigned i;
  450. /* Failed pages have to be freed */
  451. for (i = 0; i < cpages; ++i) {
  452. list_del(&failed_pages[i]->lru);
  453. __free_page(failed_pages[i]);
  454. }
  455. }
  456. /**
  457. * Allocate new pages with correct caching.
  458. *
  459. * This function is reentrant if caller updates count depending on number of
  460. * pages returned in pages array.
  461. */
  462. static int ttm_alloc_new_pages(struct list_head *pages, gfp_t gfp_flags,
  463. int ttm_flags, enum ttm_caching_state cstate,
  464. unsigned count, unsigned order)
  465. {
  466. struct page **caching_array;
  467. struct page *p;
  468. int r = 0;
  469. unsigned i, j, cpages;
  470. unsigned npages = 1 << order;
  471. unsigned max_cpages = min(count << order, (unsigned)NUM_PAGES_TO_ALLOC);
  472. /* allocate array for page caching change */
  473. caching_array = kmalloc(max_cpages*sizeof(struct page *), GFP_KERNEL);
  474. if (!caching_array) {
  475. pr_debug("Unable to allocate table for new pages\n");
  476. return -ENOMEM;
  477. }
  478. for (i = 0, cpages = 0; i < count; ++i) {
  479. p = alloc_pages(gfp_flags, order);
  480. if (!p) {
  481. pr_debug("Unable to get page %u\n", i);
  482. /* store already allocated pages in the pool after
  483. * setting the caching state */
  484. if (cpages) {
  485. r = ttm_set_pages_caching(caching_array,
  486. cstate, cpages);
  487. if (r)
  488. ttm_handle_caching_state_failure(pages,
  489. ttm_flags, cstate,
  490. caching_array, cpages);
  491. }
  492. r = -ENOMEM;
  493. goto out;
  494. }
  495. list_add(&p->lru, pages);
  496. #ifdef CONFIG_HIGHMEM
  497. /* gfp flags of highmem page should never be dma32 so we
  498. * we should be fine in such case
  499. */
  500. if (PageHighMem(p))
  501. continue;
  502. #endif
  503. for (j = 0; j < npages; ++j) {
  504. caching_array[cpages++] = p++;
  505. if (cpages == max_cpages) {
  506. r = ttm_set_pages_caching(caching_array,
  507. cstate, cpages);
  508. if (r) {
  509. ttm_handle_caching_state_failure(pages,
  510. ttm_flags, cstate,
  511. caching_array, cpages);
  512. goto out;
  513. }
  514. cpages = 0;
  515. }
  516. }
  517. }
  518. if (cpages) {
  519. r = ttm_set_pages_caching(caching_array, cstate, cpages);
  520. if (r)
  521. ttm_handle_caching_state_failure(pages,
  522. ttm_flags, cstate,
  523. caching_array, cpages);
  524. }
  525. out:
  526. kfree(caching_array);
  527. return r;
  528. }
  529. /**
  530. * Fill the given pool if there aren't enough pages and the requested number of
  531. * pages is small.
  532. */
  533. static void ttm_page_pool_fill_locked(struct ttm_page_pool *pool, int ttm_flags,
  534. enum ttm_caching_state cstate,
  535. unsigned count, unsigned long *irq_flags)
  536. {
  537. struct page *p;
  538. int r;
  539. unsigned cpages = 0;
  540. /**
  541. * Only allow one pool fill operation at a time.
  542. * If pool doesn't have enough pages for the allocation new pages are
  543. * allocated from outside of pool.
  544. */
  545. if (pool->fill_lock)
  546. return;
  547. pool->fill_lock = true;
  548. /* If allocation request is small and there are not enough
  549. * pages in a pool we fill the pool up first. */
  550. if (count < _manager->options.small
  551. && count > pool->npages) {
  552. struct list_head new_pages;
  553. unsigned alloc_size = _manager->options.alloc_size;
  554. /**
  555. * Can't change page caching if in irqsave context. We have to
  556. * drop the pool->lock.
  557. */
  558. spin_unlock_irqrestore(&pool->lock, *irq_flags);
  559. INIT_LIST_HEAD(&new_pages);
  560. r = ttm_alloc_new_pages(&new_pages, pool->gfp_flags, ttm_flags,
  561. cstate, alloc_size, 0);
  562. spin_lock_irqsave(&pool->lock, *irq_flags);
  563. if (!r) {
  564. list_splice(&new_pages, &pool->list);
  565. ++pool->nrefills;
  566. pool->npages += alloc_size;
  567. } else {
  568. pr_debug("Failed to fill pool (%p)\n", pool);
  569. /* If we have any pages left put them to the pool. */
  570. list_for_each_entry(p, &new_pages, lru) {
  571. ++cpages;
  572. }
  573. list_splice(&new_pages, &pool->list);
  574. pool->npages += cpages;
  575. }
  576. }
  577. pool->fill_lock = false;
  578. }
  579. /**
  580. * Allocate pages from the pool and put them on the return list.
  581. *
  582. * @return zero for success or negative error code.
  583. */
  584. static int ttm_page_pool_get_pages(struct ttm_page_pool *pool,
  585. struct list_head *pages,
  586. int ttm_flags,
  587. enum ttm_caching_state cstate,
  588. unsigned count, unsigned order)
  589. {
  590. unsigned long irq_flags;
  591. struct list_head *p;
  592. unsigned i;
  593. int r = 0;
  594. spin_lock_irqsave(&pool->lock, irq_flags);
  595. if (!order)
  596. ttm_page_pool_fill_locked(pool, ttm_flags, cstate, count,
  597. &irq_flags);
  598. if (count >= pool->npages) {
  599. /* take all pages from the pool */
  600. list_splice_init(&pool->list, pages);
  601. count -= pool->npages;
  602. pool->npages = 0;
  603. goto out;
  604. }
  605. /* find the last pages to include for requested number of pages. Split
  606. * pool to begin and halve it to reduce search space. */
  607. if (count <= pool->npages/2) {
  608. i = 0;
  609. list_for_each(p, &pool->list) {
  610. if (++i == count)
  611. break;
  612. }
  613. } else {
  614. i = pool->npages + 1;
  615. list_for_each_prev(p, &pool->list) {
  616. if (--i == count)
  617. break;
  618. }
  619. }
  620. /* Cut 'count' number of pages from the pool */
  621. list_cut_position(pages, &pool->list, p);
  622. pool->npages -= count;
  623. count = 0;
  624. out:
  625. spin_unlock_irqrestore(&pool->lock, irq_flags);
  626. /* clear the pages coming from the pool if requested */
  627. if (ttm_flags & TTM_PAGE_FLAG_ZERO_ALLOC) {
  628. struct page *page;
  629. list_for_each_entry(page, pages, lru) {
  630. if (PageHighMem(page))
  631. clear_highpage(page);
  632. else
  633. clear_page(page_address(page));
  634. }
  635. }
  636. /* If pool didn't have enough pages allocate new one. */
  637. if (count) {
  638. gfp_t gfp_flags = pool->gfp_flags;
  639. /* set zero flag for page allocation if required */
  640. if (ttm_flags & TTM_PAGE_FLAG_ZERO_ALLOC)
  641. gfp_flags |= __GFP_ZERO;
  642. if (ttm_flags & TTM_PAGE_FLAG_NO_RETRY)
  643. gfp_flags |= __GFP_RETRY_MAYFAIL;
  644. /* ttm_alloc_new_pages doesn't reference pool so we can run
  645. * multiple requests in parallel.
  646. **/
  647. r = ttm_alloc_new_pages(pages, gfp_flags, ttm_flags, cstate,
  648. count, order);
  649. }
  650. return r;
  651. }
  652. /* Put all pages in pages list to correct pool to wait for reuse */
  653. static void ttm_put_pages(struct page **pages, unsigned npages, int flags,
  654. enum ttm_caching_state cstate)
  655. {
  656. struct ttm_page_pool *pool = ttm_get_pool(flags, false, cstate);
  657. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  658. struct ttm_page_pool *huge = ttm_get_pool(flags, true, cstate);
  659. #endif
  660. unsigned long irq_flags;
  661. unsigned i;
  662. if (pool == NULL) {
  663. /* No pool for this memory type so free the pages */
  664. i = 0;
  665. while (i < npages) {
  666. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  667. struct page *p = pages[i];
  668. #endif
  669. unsigned order = 0, j;
  670. if (!pages[i]) {
  671. ++i;
  672. continue;
  673. }
  674. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  675. if (!(flags & TTM_PAGE_FLAG_DMA32)) {
  676. for (j = 0; j < HPAGE_PMD_NR; ++j)
  677. if (p++ != pages[i + j])
  678. break;
  679. if (j == HPAGE_PMD_NR)
  680. order = HPAGE_PMD_ORDER;
  681. }
  682. #endif
  683. if (page_count(pages[i]) != 1)
  684. pr_err("Erroneous page count. Leaking pages.\n");
  685. __free_pages(pages[i], order);
  686. j = 1 << order;
  687. while (j) {
  688. pages[i++] = NULL;
  689. --j;
  690. }
  691. }
  692. return;
  693. }
  694. i = 0;
  695. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  696. if (huge) {
  697. unsigned max_size, n2free;
  698. spin_lock_irqsave(&huge->lock, irq_flags);
  699. while (i < npages) {
  700. struct page *p = pages[i];
  701. unsigned j;
  702. if (!p)
  703. break;
  704. for (j = 0; j < HPAGE_PMD_NR; ++j)
  705. if (p++ != pages[i + j])
  706. break;
  707. if (j != HPAGE_PMD_NR)
  708. break;
  709. list_add_tail(&pages[i]->lru, &huge->list);
  710. for (j = 0; j < HPAGE_PMD_NR; ++j)
  711. pages[i++] = NULL;
  712. huge->npages++;
  713. }
  714. /* Check that we don't go over the pool limit */
  715. max_size = _manager->options.max_size;
  716. max_size /= HPAGE_PMD_NR;
  717. if (huge->npages > max_size)
  718. n2free = huge->npages - max_size;
  719. else
  720. n2free = 0;
  721. spin_unlock_irqrestore(&huge->lock, irq_flags);
  722. if (n2free)
  723. ttm_page_pool_free(huge, n2free, false);
  724. }
  725. #endif
  726. spin_lock_irqsave(&pool->lock, irq_flags);
  727. while (i < npages) {
  728. if (pages[i]) {
  729. if (page_count(pages[i]) != 1)
  730. pr_err("Erroneous page count. Leaking pages.\n");
  731. list_add_tail(&pages[i]->lru, &pool->list);
  732. pages[i] = NULL;
  733. pool->npages++;
  734. }
  735. ++i;
  736. }
  737. /* Check that we don't go over the pool limit */
  738. npages = 0;
  739. if (pool->npages > _manager->options.max_size) {
  740. npages = pool->npages - _manager->options.max_size;
  741. /* free at least NUM_PAGES_TO_ALLOC number of pages
  742. * to reduce calls to set_memory_wb */
  743. if (npages < NUM_PAGES_TO_ALLOC)
  744. npages = NUM_PAGES_TO_ALLOC;
  745. }
  746. spin_unlock_irqrestore(&pool->lock, irq_flags);
  747. if (npages)
  748. ttm_page_pool_free(pool, npages, false);
  749. }
  750. /*
  751. * On success pages list will hold count number of correctly
  752. * cached pages.
  753. */
  754. static int ttm_get_pages(struct page **pages, unsigned npages, int flags,
  755. enum ttm_caching_state cstate)
  756. {
  757. struct ttm_page_pool *pool = ttm_get_pool(flags, false, cstate);
  758. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  759. struct ttm_page_pool *huge = ttm_get_pool(flags, true, cstate);
  760. #endif
  761. struct list_head plist;
  762. struct page *p = NULL;
  763. unsigned count, first;
  764. int r;
  765. /* No pool for cached pages */
  766. if (pool == NULL) {
  767. gfp_t gfp_flags = GFP_USER;
  768. unsigned i;
  769. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  770. unsigned j;
  771. #endif
  772. /* set zero flag for page allocation if required */
  773. if (flags & TTM_PAGE_FLAG_ZERO_ALLOC)
  774. gfp_flags |= __GFP_ZERO;
  775. if (flags & TTM_PAGE_FLAG_NO_RETRY)
  776. gfp_flags |= __GFP_RETRY_MAYFAIL;
  777. if (flags & TTM_PAGE_FLAG_DMA32)
  778. gfp_flags |= GFP_DMA32;
  779. else
  780. gfp_flags |= GFP_HIGHUSER;
  781. i = 0;
  782. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  783. if (!(gfp_flags & GFP_DMA32)) {
  784. while (npages >= HPAGE_PMD_NR) {
  785. gfp_t huge_flags = gfp_flags;
  786. huge_flags |= GFP_TRANSHUGE;
  787. huge_flags &= ~__GFP_MOVABLE;
  788. huge_flags &= ~__GFP_COMP;
  789. p = alloc_pages(huge_flags, HPAGE_PMD_ORDER);
  790. if (!p)
  791. break;
  792. for (j = 0; j < HPAGE_PMD_NR; ++j)
  793. pages[i++] = p++;
  794. npages -= HPAGE_PMD_NR;
  795. }
  796. }
  797. #endif
  798. first = i;
  799. while (npages) {
  800. p = alloc_page(gfp_flags);
  801. if (!p) {
  802. pr_debug("Unable to allocate page\n");
  803. return -ENOMEM;
  804. }
  805. /* Swap the pages if we detect consecutive order */
  806. if (i > first && pages[i - 1] == p - 1)
  807. swap(p, pages[i - 1]);
  808. pages[i++] = p;
  809. --npages;
  810. }
  811. return 0;
  812. }
  813. count = 0;
  814. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  815. if (huge && npages >= HPAGE_PMD_NR) {
  816. INIT_LIST_HEAD(&plist);
  817. ttm_page_pool_get_pages(huge, &plist, flags, cstate,
  818. npages / HPAGE_PMD_NR,
  819. HPAGE_PMD_ORDER);
  820. list_for_each_entry(p, &plist, lru) {
  821. unsigned j;
  822. for (j = 0; j < HPAGE_PMD_NR; ++j)
  823. pages[count++] = &p[j];
  824. }
  825. }
  826. #endif
  827. INIT_LIST_HEAD(&plist);
  828. r = ttm_page_pool_get_pages(pool, &plist, flags, cstate,
  829. npages - count, 0);
  830. first = count;
  831. list_for_each_entry(p, &plist, lru) {
  832. struct page *tmp = p;
  833. /* Swap the pages if we detect consecutive order */
  834. if (count > first && pages[count - 1] == tmp - 1)
  835. swap(tmp, pages[count - 1]);
  836. pages[count++] = tmp;
  837. }
  838. if (r) {
  839. /* If there is any pages in the list put them back to
  840. * the pool.
  841. */
  842. pr_debug("Failed to allocate extra pages for large request\n");
  843. ttm_put_pages(pages, count, flags, cstate);
  844. return r;
  845. }
  846. return 0;
  847. }
  848. static void ttm_page_pool_init_locked(struct ttm_page_pool *pool, gfp_t flags,
  849. char *name, unsigned int order)
  850. {
  851. spin_lock_init(&pool->lock);
  852. pool->fill_lock = false;
  853. INIT_LIST_HEAD(&pool->list);
  854. pool->npages = pool->nfrees = 0;
  855. pool->gfp_flags = flags;
  856. pool->name = name;
  857. pool->order = order;
  858. }
  859. int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
  860. {
  861. int ret;
  862. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  863. unsigned order = HPAGE_PMD_ORDER;
  864. #else
  865. unsigned order = 0;
  866. #endif
  867. WARN_ON(_manager);
  868. pr_info("Initializing pool allocator\n");
  869. _manager = kzalloc(sizeof(*_manager), GFP_KERNEL);
  870. if (!_manager)
  871. return -ENOMEM;
  872. ttm_page_pool_init_locked(&_manager->wc_pool, GFP_HIGHUSER, "wc", 0);
  873. ttm_page_pool_init_locked(&_manager->uc_pool, GFP_HIGHUSER, "uc", 0);
  874. ttm_page_pool_init_locked(&_manager->wc_pool_dma32,
  875. GFP_USER | GFP_DMA32, "wc dma", 0);
  876. ttm_page_pool_init_locked(&_manager->uc_pool_dma32,
  877. GFP_USER | GFP_DMA32, "uc dma", 0);
  878. ttm_page_pool_init_locked(&_manager->wc_pool_huge,
  879. GFP_TRANSHUGE & ~(__GFP_MOVABLE | __GFP_COMP),
  880. "wc huge", order);
  881. ttm_page_pool_init_locked(&_manager->uc_pool_huge,
  882. GFP_TRANSHUGE & ~(__GFP_MOVABLE | __GFP_COMP)
  883. , "uc huge", order);
  884. _manager->options.max_size = max_pages;
  885. _manager->options.small = SMALL_ALLOCATION;
  886. _manager->options.alloc_size = NUM_PAGES_TO_ALLOC;
  887. ret = kobject_init_and_add(&_manager->kobj, &ttm_pool_kobj_type,
  888. &glob->kobj, "pool");
  889. if (unlikely(ret != 0))
  890. goto error;
  891. ret = ttm_pool_mm_shrink_init(_manager);
  892. if (unlikely(ret != 0))
  893. goto error;
  894. return 0;
  895. error:
  896. kobject_put(&_manager->kobj);
  897. _manager = NULL;
  898. return ret;
  899. }
  900. void ttm_page_alloc_fini(void)
  901. {
  902. int i;
  903. pr_info("Finalizing pool allocator\n");
  904. ttm_pool_mm_shrink_fini(_manager);
  905. /* OK to use static buffer since global mutex is no longer used. */
  906. for (i = 0; i < NUM_POOLS; ++i)
  907. ttm_page_pool_free(&_manager->pools[i], FREE_ALL_PAGES, true);
  908. kobject_put(&_manager->kobj);
  909. _manager = NULL;
  910. }
  911. static void
  912. ttm_pool_unpopulate_helper(struct ttm_tt *ttm, unsigned mem_count_update)
  913. {
  914. struct ttm_mem_global *mem_glob = ttm->bdev->glob->mem_glob;
  915. unsigned i;
  916. if (mem_count_update == 0)
  917. goto put_pages;
  918. for (i = 0; i < mem_count_update; ++i) {
  919. if (!ttm->pages[i])
  920. continue;
  921. ttm_mem_global_free_page(mem_glob, ttm->pages[i], PAGE_SIZE);
  922. }
  923. put_pages:
  924. ttm_put_pages(ttm->pages, ttm->num_pages, ttm->page_flags,
  925. ttm->caching_state);
  926. ttm->state = tt_unpopulated;
  927. }
  928. int ttm_pool_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
  929. {
  930. struct ttm_mem_global *mem_glob = ttm->bdev->glob->mem_glob;
  931. unsigned i;
  932. int ret;
  933. if (ttm->state != tt_unpopulated)
  934. return 0;
  935. if (ttm_check_under_lowerlimit(mem_glob, ttm->num_pages, ctx))
  936. return -ENOMEM;
  937. ret = ttm_get_pages(ttm->pages, ttm->num_pages, ttm->page_flags,
  938. ttm->caching_state);
  939. if (unlikely(ret != 0)) {
  940. ttm_pool_unpopulate_helper(ttm, 0);
  941. return ret;
  942. }
  943. for (i = 0; i < ttm->num_pages; ++i) {
  944. ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
  945. PAGE_SIZE, ctx);
  946. if (unlikely(ret != 0)) {
  947. ttm_pool_unpopulate_helper(ttm, i);
  948. return -ENOMEM;
  949. }
  950. }
  951. if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
  952. ret = ttm_tt_swapin(ttm);
  953. if (unlikely(ret != 0)) {
  954. ttm_pool_unpopulate(ttm);
  955. return ret;
  956. }
  957. }
  958. ttm->state = tt_unbound;
  959. return 0;
  960. }
  961. EXPORT_SYMBOL(ttm_pool_populate);
  962. void ttm_pool_unpopulate(struct ttm_tt *ttm)
  963. {
  964. ttm_pool_unpopulate_helper(ttm, ttm->num_pages);
  965. }
  966. EXPORT_SYMBOL(ttm_pool_unpopulate);
  967. int ttm_populate_and_map_pages(struct device *dev, struct ttm_dma_tt *tt,
  968. struct ttm_operation_ctx *ctx)
  969. {
  970. unsigned i, j;
  971. int r;
  972. r = ttm_pool_populate(&tt->ttm, ctx);
  973. if (r)
  974. return r;
  975. for (i = 0; i < tt->ttm.num_pages; ++i) {
  976. struct page *p = tt->ttm.pages[i];
  977. size_t num_pages = 1;
  978. for (j = i + 1; j < tt->ttm.num_pages; ++j) {
  979. if (++p != tt->ttm.pages[j])
  980. break;
  981. ++num_pages;
  982. }
  983. tt->dma_address[i] = dma_map_page(dev, tt->ttm.pages[i],
  984. 0, num_pages * PAGE_SIZE,
  985. DMA_BIDIRECTIONAL);
  986. if (dma_mapping_error(dev, tt->dma_address[i])) {
  987. while (i--) {
  988. dma_unmap_page(dev, tt->dma_address[i],
  989. PAGE_SIZE, DMA_BIDIRECTIONAL);
  990. tt->dma_address[i] = 0;
  991. }
  992. ttm_pool_unpopulate(&tt->ttm);
  993. return -EFAULT;
  994. }
  995. for (j = 1; j < num_pages; ++j) {
  996. tt->dma_address[i + 1] = tt->dma_address[i] + PAGE_SIZE;
  997. ++i;
  998. }
  999. }
  1000. return 0;
  1001. }
  1002. EXPORT_SYMBOL(ttm_populate_and_map_pages);
  1003. void ttm_unmap_and_unpopulate_pages(struct device *dev, struct ttm_dma_tt *tt)
  1004. {
  1005. unsigned i, j;
  1006. for (i = 0; i < tt->ttm.num_pages;) {
  1007. struct page *p = tt->ttm.pages[i];
  1008. size_t num_pages = 1;
  1009. if (!tt->dma_address[i] || !tt->ttm.pages[i]) {
  1010. ++i;
  1011. continue;
  1012. }
  1013. for (j = i + 1; j < tt->ttm.num_pages; ++j) {
  1014. if (++p != tt->ttm.pages[j])
  1015. break;
  1016. ++num_pages;
  1017. }
  1018. dma_unmap_page(dev, tt->dma_address[i], num_pages * PAGE_SIZE,
  1019. DMA_BIDIRECTIONAL);
  1020. i += num_pages;
  1021. }
  1022. ttm_pool_unpopulate(&tt->ttm);
  1023. }
  1024. EXPORT_SYMBOL(ttm_unmap_and_unpopulate_pages);
  1025. int ttm_page_alloc_debugfs(struct seq_file *m, void *data)
  1026. {
  1027. struct ttm_page_pool *p;
  1028. unsigned i;
  1029. char *h[] = {"pool", "refills", "pages freed", "size"};
  1030. if (!_manager) {
  1031. seq_printf(m, "No pool allocator running.\n");
  1032. return 0;
  1033. }
  1034. seq_printf(m, "%7s %12s %13s %8s\n",
  1035. h[0], h[1], h[2], h[3]);
  1036. for (i = 0; i < NUM_POOLS; ++i) {
  1037. p = &_manager->pools[i];
  1038. seq_printf(m, "%7s %12ld %13ld %8d\n",
  1039. p->name, p->nrefills,
  1040. p->nfrees, p->npages);
  1041. }
  1042. return 0;
  1043. }
  1044. EXPORT_SYMBOL(ttm_page_alloc_debugfs);