ttm_page_alloc.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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. }
  392. mutex_unlock(&lock);
  393. return freed;
  394. }
  395. static unsigned long
  396. ttm_pool_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
  397. {
  398. unsigned i;
  399. unsigned long count = 0;
  400. struct ttm_page_pool *pool;
  401. for (i = 0; i < NUM_POOLS; ++i) {
  402. pool = &_manager->pools[i];
  403. count += (pool->npages << pool->order);
  404. }
  405. return count;
  406. }
  407. static void ttm_pool_mm_shrink_init(struct ttm_pool_manager *manager)
  408. {
  409. manager->mm_shrink.count_objects = ttm_pool_shrink_count;
  410. manager->mm_shrink.scan_objects = ttm_pool_shrink_scan;
  411. manager->mm_shrink.seeks = 1;
  412. register_shrinker(&manager->mm_shrink);
  413. }
  414. static void ttm_pool_mm_shrink_fini(struct ttm_pool_manager *manager)
  415. {
  416. unregister_shrinker(&manager->mm_shrink);
  417. }
  418. static int ttm_set_pages_caching(struct page **pages,
  419. enum ttm_caching_state cstate, unsigned cpages)
  420. {
  421. int r = 0;
  422. /* Set page caching */
  423. switch (cstate) {
  424. case tt_uncached:
  425. r = set_pages_array_uc(pages, cpages);
  426. if (r)
  427. pr_err("Failed to set %d pages to uc!\n", cpages);
  428. break;
  429. case tt_wc:
  430. r = set_pages_array_wc(pages, cpages);
  431. if (r)
  432. pr_err("Failed to set %d pages to wc!\n", cpages);
  433. break;
  434. default:
  435. break;
  436. }
  437. return r;
  438. }
  439. /**
  440. * Free pages the pages that failed to change the caching state. If there is
  441. * any pages that have changed their caching state already put them to the
  442. * pool.
  443. */
  444. static void ttm_handle_caching_state_failure(struct list_head *pages,
  445. int ttm_flags, enum ttm_caching_state cstate,
  446. struct page **failed_pages, unsigned cpages)
  447. {
  448. unsigned i;
  449. /* Failed pages have to be freed */
  450. for (i = 0; i < cpages; ++i) {
  451. list_del(&failed_pages[i]->lru);
  452. __free_page(failed_pages[i]);
  453. }
  454. }
  455. /**
  456. * Allocate new pages with correct caching.
  457. *
  458. * This function is reentrant if caller updates count depending on number of
  459. * pages returned in pages array.
  460. */
  461. static int ttm_alloc_new_pages(struct list_head *pages, gfp_t gfp_flags,
  462. int ttm_flags, enum ttm_caching_state cstate,
  463. unsigned count, unsigned order)
  464. {
  465. struct page **caching_array;
  466. struct page *p;
  467. int r = 0;
  468. unsigned i, j, cpages;
  469. unsigned npages = 1 << order;
  470. unsigned max_cpages = min(count, (unsigned)NUM_PAGES_TO_ALLOC);
  471. /* allocate array for page caching change */
  472. caching_array = kmalloc(max_cpages*sizeof(struct page *), GFP_KERNEL);
  473. if (!caching_array) {
  474. pr_debug("Unable to allocate table for new pages\n");
  475. return -ENOMEM;
  476. }
  477. for (i = 0, cpages = 0; i < count; ++i) {
  478. p = alloc_pages(gfp_flags, order);
  479. if (!p) {
  480. pr_debug("Unable to get page %u\n", i);
  481. /* store already allocated pages in the pool after
  482. * setting the caching state */
  483. if (cpages) {
  484. r = ttm_set_pages_caching(caching_array,
  485. cstate, cpages);
  486. if (r)
  487. ttm_handle_caching_state_failure(pages,
  488. ttm_flags, cstate,
  489. caching_array, cpages);
  490. }
  491. r = -ENOMEM;
  492. goto out;
  493. }
  494. list_add(&p->lru, pages);
  495. #ifdef CONFIG_HIGHMEM
  496. /* gfp flags of highmem page should never be dma32 so we
  497. * we should be fine in such case
  498. */
  499. if (PageHighMem(p))
  500. continue;
  501. #endif
  502. for (j = 0; j < npages; ++j) {
  503. caching_array[cpages++] = p++;
  504. if (cpages == max_cpages) {
  505. r = ttm_set_pages_caching(caching_array,
  506. cstate, cpages);
  507. if (r) {
  508. ttm_handle_caching_state_failure(pages,
  509. ttm_flags, cstate,
  510. caching_array, cpages);
  511. goto out;
  512. }
  513. cpages = 0;
  514. }
  515. }
  516. }
  517. if (cpages) {
  518. r = ttm_set_pages_caching(caching_array, cstate, cpages);
  519. if (r)
  520. ttm_handle_caching_state_failure(pages,
  521. ttm_flags, cstate,
  522. caching_array, cpages);
  523. }
  524. out:
  525. kfree(caching_array);
  526. return r;
  527. }
  528. /**
  529. * Fill the given pool if there aren't enough pages and the requested number of
  530. * pages is small.
  531. */
  532. static void ttm_page_pool_fill_locked(struct ttm_page_pool *pool, int ttm_flags,
  533. enum ttm_caching_state cstate,
  534. unsigned count, unsigned long *irq_flags)
  535. {
  536. struct page *p;
  537. int r;
  538. unsigned cpages = 0;
  539. /**
  540. * Only allow one pool fill operation at a time.
  541. * If pool doesn't have enough pages for the allocation new pages are
  542. * allocated from outside of pool.
  543. */
  544. if (pool->fill_lock)
  545. return;
  546. pool->fill_lock = true;
  547. /* If allocation request is small and there are not enough
  548. * pages in a pool we fill the pool up first. */
  549. if (count < _manager->options.small
  550. && count > pool->npages) {
  551. struct list_head new_pages;
  552. unsigned alloc_size = _manager->options.alloc_size;
  553. /**
  554. * Can't change page caching if in irqsave context. We have to
  555. * drop the pool->lock.
  556. */
  557. spin_unlock_irqrestore(&pool->lock, *irq_flags);
  558. INIT_LIST_HEAD(&new_pages);
  559. r = ttm_alloc_new_pages(&new_pages, pool->gfp_flags, ttm_flags,
  560. cstate, alloc_size, 0);
  561. spin_lock_irqsave(&pool->lock, *irq_flags);
  562. if (!r) {
  563. list_splice(&new_pages, &pool->list);
  564. ++pool->nrefills;
  565. pool->npages += alloc_size;
  566. } else {
  567. pr_debug("Failed to fill pool (%p)\n", pool);
  568. /* If we have any pages left put them to the pool. */
  569. list_for_each_entry(p, &new_pages, lru) {
  570. ++cpages;
  571. }
  572. list_splice(&new_pages, &pool->list);
  573. pool->npages += cpages;
  574. }
  575. }
  576. pool->fill_lock = false;
  577. }
  578. /**
  579. * Allocate pages from the pool and put them on the return list.
  580. *
  581. * @return zero for success or negative error code.
  582. */
  583. static int ttm_page_pool_get_pages(struct ttm_page_pool *pool,
  584. struct list_head *pages,
  585. int ttm_flags,
  586. enum ttm_caching_state cstate,
  587. unsigned count, unsigned order)
  588. {
  589. unsigned long irq_flags;
  590. struct list_head *p;
  591. unsigned i;
  592. int r = 0;
  593. spin_lock_irqsave(&pool->lock, irq_flags);
  594. if (!order)
  595. ttm_page_pool_fill_locked(pool, ttm_flags, cstate, count,
  596. &irq_flags);
  597. if (count >= pool->npages) {
  598. /* take all pages from the pool */
  599. list_splice_init(&pool->list, pages);
  600. count -= pool->npages;
  601. pool->npages = 0;
  602. goto out;
  603. }
  604. /* find the last pages to include for requested number of pages. Split
  605. * pool to begin and halve it to reduce search space. */
  606. if (count <= pool->npages/2) {
  607. i = 0;
  608. list_for_each(p, &pool->list) {
  609. if (++i == count)
  610. break;
  611. }
  612. } else {
  613. i = pool->npages + 1;
  614. list_for_each_prev(p, &pool->list) {
  615. if (--i == count)
  616. break;
  617. }
  618. }
  619. /* Cut 'count' number of pages from the pool */
  620. list_cut_position(pages, &pool->list, p);
  621. pool->npages -= count;
  622. count = 0;
  623. out:
  624. spin_unlock_irqrestore(&pool->lock, irq_flags);
  625. /* clear the pages coming from the pool if requested */
  626. if (ttm_flags & TTM_PAGE_FLAG_ZERO_ALLOC) {
  627. struct page *page;
  628. list_for_each_entry(page, pages, lru) {
  629. if (PageHighMem(page))
  630. clear_highpage(page);
  631. else
  632. clear_page(page_address(page));
  633. }
  634. }
  635. /* If pool didn't have enough pages allocate new one. */
  636. if (count) {
  637. gfp_t gfp_flags = pool->gfp_flags;
  638. /* set zero flag for page allocation if required */
  639. if (ttm_flags & TTM_PAGE_FLAG_ZERO_ALLOC)
  640. gfp_flags |= __GFP_ZERO;
  641. /* ttm_alloc_new_pages doesn't reference pool so we can run
  642. * multiple requests in parallel.
  643. **/
  644. r = ttm_alloc_new_pages(pages, gfp_flags, ttm_flags, cstate,
  645. count, order);
  646. }
  647. return r;
  648. }
  649. /* Put all pages in pages list to correct pool to wait for reuse */
  650. static void ttm_put_pages(struct page **pages, unsigned npages, int flags,
  651. enum ttm_caching_state cstate)
  652. {
  653. struct ttm_page_pool *pool = ttm_get_pool(flags, false, cstate);
  654. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  655. struct ttm_page_pool *huge = ttm_get_pool(flags, true, cstate);
  656. #endif
  657. unsigned long irq_flags;
  658. unsigned i;
  659. if (pool == NULL) {
  660. /* No pool for this memory type so free the pages */
  661. i = 0;
  662. while (i < npages) {
  663. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  664. struct page *p = pages[i];
  665. #endif
  666. unsigned order = 0, j;
  667. if (!pages[i]) {
  668. ++i;
  669. continue;
  670. }
  671. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  672. if (!(flags & TTM_PAGE_FLAG_DMA32)) {
  673. for (j = 0; j < HPAGE_PMD_NR; ++j)
  674. if (p++ != pages[i + j])
  675. break;
  676. if (j == HPAGE_PMD_NR)
  677. order = HPAGE_PMD_ORDER;
  678. }
  679. #endif
  680. if (page_count(pages[i]) != 1)
  681. pr_err("Erroneous page count. Leaking pages.\n");
  682. __free_pages(pages[i], order);
  683. j = 1 << order;
  684. while (j) {
  685. pages[i++] = NULL;
  686. --j;
  687. }
  688. }
  689. return;
  690. }
  691. i = 0;
  692. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  693. if (huge) {
  694. unsigned max_size, n2free;
  695. spin_lock_irqsave(&huge->lock, irq_flags);
  696. while (i < npages) {
  697. struct page *p = pages[i];
  698. unsigned j;
  699. if (!p)
  700. break;
  701. for (j = 0; j < HPAGE_PMD_NR; ++j)
  702. if (p++ != pages[i + j])
  703. break;
  704. if (j != HPAGE_PMD_NR)
  705. break;
  706. list_add_tail(&pages[i]->lru, &huge->list);
  707. for (j = 0; j < HPAGE_PMD_NR; ++j)
  708. pages[i++] = NULL;
  709. huge->npages++;
  710. }
  711. /* Check that we don't go over the pool limit */
  712. max_size = _manager->options.max_size;
  713. max_size /= HPAGE_PMD_NR;
  714. if (huge->npages > max_size)
  715. n2free = huge->npages - max_size;
  716. else
  717. n2free = 0;
  718. spin_unlock_irqrestore(&huge->lock, irq_flags);
  719. if (n2free)
  720. ttm_page_pool_free(huge, n2free, false);
  721. }
  722. #endif
  723. spin_lock_irqsave(&pool->lock, irq_flags);
  724. while (i < npages) {
  725. if (pages[i]) {
  726. if (page_count(pages[i]) != 1)
  727. pr_err("Erroneous page count. Leaking pages.\n");
  728. list_add_tail(&pages[i]->lru, &pool->list);
  729. pages[i] = NULL;
  730. pool->npages++;
  731. }
  732. ++i;
  733. }
  734. /* Check that we don't go over the pool limit */
  735. npages = 0;
  736. if (pool->npages > _manager->options.max_size) {
  737. npages = pool->npages - _manager->options.max_size;
  738. /* free at least NUM_PAGES_TO_ALLOC number of pages
  739. * to reduce calls to set_memory_wb */
  740. if (npages < NUM_PAGES_TO_ALLOC)
  741. npages = NUM_PAGES_TO_ALLOC;
  742. }
  743. spin_unlock_irqrestore(&pool->lock, irq_flags);
  744. if (npages)
  745. ttm_page_pool_free(pool, npages, false);
  746. }
  747. /*
  748. * On success pages list will hold count number of correctly
  749. * cached pages.
  750. */
  751. static int ttm_get_pages(struct page **pages, unsigned npages, int flags,
  752. enum ttm_caching_state cstate)
  753. {
  754. struct ttm_page_pool *pool = ttm_get_pool(flags, false, cstate);
  755. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  756. struct ttm_page_pool *huge = ttm_get_pool(flags, true, cstate);
  757. #endif
  758. struct list_head plist;
  759. struct page *p = NULL;
  760. unsigned count, first;
  761. int r;
  762. /* No pool for cached pages */
  763. if (pool == NULL) {
  764. gfp_t gfp_flags = GFP_USER;
  765. unsigned i;
  766. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  767. unsigned j;
  768. #endif
  769. /* set zero flag for page allocation if required */
  770. if (flags & TTM_PAGE_FLAG_ZERO_ALLOC)
  771. gfp_flags |= __GFP_ZERO;
  772. if (flags & TTM_PAGE_FLAG_DMA32)
  773. gfp_flags |= GFP_DMA32;
  774. else
  775. gfp_flags |= GFP_HIGHUSER;
  776. i = 0;
  777. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  778. if (!(gfp_flags & GFP_DMA32)) {
  779. while (npages >= HPAGE_PMD_NR) {
  780. gfp_t huge_flags = gfp_flags;
  781. huge_flags |= GFP_TRANSHUGE;
  782. huge_flags &= ~__GFP_MOVABLE;
  783. huge_flags &= ~__GFP_COMP;
  784. p = alloc_pages(huge_flags, HPAGE_PMD_ORDER);
  785. if (!p)
  786. break;
  787. for (j = 0; j < HPAGE_PMD_NR; ++j)
  788. pages[i++] = p++;
  789. npages -= HPAGE_PMD_NR;
  790. }
  791. }
  792. #endif
  793. first = i;
  794. while (npages) {
  795. p = alloc_page(gfp_flags);
  796. if (!p) {
  797. pr_debug("Unable to allocate page\n");
  798. return -ENOMEM;
  799. }
  800. /* Swap the pages if we detect consecutive order */
  801. if (i > first && pages[i - 1] == p - 1)
  802. swap(p, pages[i - 1]);
  803. pages[i++] = p;
  804. --npages;
  805. }
  806. return 0;
  807. }
  808. count = 0;
  809. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  810. if (huge && npages >= HPAGE_PMD_NR) {
  811. INIT_LIST_HEAD(&plist);
  812. ttm_page_pool_get_pages(huge, &plist, flags, cstate,
  813. npages / HPAGE_PMD_NR,
  814. HPAGE_PMD_ORDER);
  815. list_for_each_entry(p, &plist, lru) {
  816. unsigned j;
  817. for (j = 0; j < HPAGE_PMD_NR; ++j)
  818. pages[count++] = &p[j];
  819. }
  820. }
  821. #endif
  822. INIT_LIST_HEAD(&plist);
  823. r = ttm_page_pool_get_pages(pool, &plist, flags, cstate,
  824. npages - count, 0);
  825. first = count;
  826. list_for_each_entry(p, &plist, lru) {
  827. struct page *tmp = p;
  828. /* Swap the pages if we detect consecutive order */
  829. if (count > first && pages[count - 1] == tmp - 1)
  830. swap(tmp, pages[count - 1]);
  831. pages[count++] = tmp;
  832. }
  833. if (r) {
  834. /* If there is any pages in the list put them back to
  835. * the pool.
  836. */
  837. pr_debug("Failed to allocate extra pages for large request\n");
  838. ttm_put_pages(pages, count, flags, cstate);
  839. return r;
  840. }
  841. return 0;
  842. }
  843. static void ttm_page_pool_init_locked(struct ttm_page_pool *pool, gfp_t flags,
  844. char *name, unsigned int order)
  845. {
  846. spin_lock_init(&pool->lock);
  847. pool->fill_lock = false;
  848. INIT_LIST_HEAD(&pool->list);
  849. pool->npages = pool->nfrees = 0;
  850. pool->gfp_flags = flags;
  851. pool->name = name;
  852. pool->order = order;
  853. }
  854. int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
  855. {
  856. int ret;
  857. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  858. unsigned order = HPAGE_PMD_ORDER;
  859. #else
  860. unsigned order = 0;
  861. #endif
  862. WARN_ON(_manager);
  863. pr_info("Initializing pool allocator\n");
  864. _manager = kzalloc(sizeof(*_manager), GFP_KERNEL);
  865. ttm_page_pool_init_locked(&_manager->wc_pool, GFP_HIGHUSER, "wc", 0);
  866. ttm_page_pool_init_locked(&_manager->uc_pool, GFP_HIGHUSER, "uc", 0);
  867. ttm_page_pool_init_locked(&_manager->wc_pool_dma32,
  868. GFP_USER | GFP_DMA32, "wc dma", 0);
  869. ttm_page_pool_init_locked(&_manager->uc_pool_dma32,
  870. GFP_USER | GFP_DMA32, "uc dma", 0);
  871. ttm_page_pool_init_locked(&_manager->wc_pool_huge,
  872. GFP_TRANSHUGE & ~(__GFP_MOVABLE | __GFP_COMP),
  873. "wc huge", order);
  874. ttm_page_pool_init_locked(&_manager->uc_pool_huge,
  875. GFP_TRANSHUGE & ~(__GFP_MOVABLE | __GFP_COMP)
  876. , "uc huge", order);
  877. _manager->options.max_size = max_pages;
  878. _manager->options.small = SMALL_ALLOCATION;
  879. _manager->options.alloc_size = NUM_PAGES_TO_ALLOC;
  880. ret = kobject_init_and_add(&_manager->kobj, &ttm_pool_kobj_type,
  881. &glob->kobj, "pool");
  882. if (unlikely(ret != 0)) {
  883. kobject_put(&_manager->kobj);
  884. _manager = NULL;
  885. return ret;
  886. }
  887. ttm_pool_mm_shrink_init(_manager);
  888. return 0;
  889. }
  890. void ttm_page_alloc_fini(void)
  891. {
  892. int i;
  893. pr_info("Finalizing pool allocator\n");
  894. ttm_pool_mm_shrink_fini(_manager);
  895. /* OK to use static buffer since global mutex is no longer used. */
  896. for (i = 0; i < NUM_POOLS; ++i)
  897. ttm_page_pool_free(&_manager->pools[i], FREE_ALL_PAGES, true);
  898. kobject_put(&_manager->kobj);
  899. _manager = NULL;
  900. }
  901. int ttm_pool_populate(struct ttm_tt *ttm)
  902. {
  903. struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
  904. unsigned i;
  905. int ret;
  906. if (ttm->state != tt_unpopulated)
  907. return 0;
  908. ret = ttm_get_pages(ttm->pages, ttm->num_pages, ttm->page_flags,
  909. ttm->caching_state);
  910. if (unlikely(ret != 0)) {
  911. ttm_pool_unpopulate(ttm);
  912. return ret;
  913. }
  914. for (i = 0; i < ttm->num_pages; ++i) {
  915. ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
  916. PAGE_SIZE);
  917. if (unlikely(ret != 0)) {
  918. ttm_pool_unpopulate(ttm);
  919. return -ENOMEM;
  920. }
  921. }
  922. if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
  923. ret = ttm_tt_swapin(ttm);
  924. if (unlikely(ret != 0)) {
  925. ttm_pool_unpopulate(ttm);
  926. return ret;
  927. }
  928. }
  929. ttm->state = tt_unbound;
  930. return 0;
  931. }
  932. EXPORT_SYMBOL(ttm_pool_populate);
  933. void ttm_pool_unpopulate(struct ttm_tt *ttm)
  934. {
  935. unsigned i;
  936. for (i = 0; i < ttm->num_pages; ++i) {
  937. if (!ttm->pages[i])
  938. continue;
  939. ttm_mem_global_free_page(ttm->glob->mem_glob, ttm->pages[i],
  940. PAGE_SIZE);
  941. }
  942. ttm_put_pages(ttm->pages, ttm->num_pages, ttm->page_flags,
  943. ttm->caching_state);
  944. ttm->state = tt_unpopulated;
  945. }
  946. EXPORT_SYMBOL(ttm_pool_unpopulate);
  947. int ttm_populate_and_map_pages(struct device *dev, struct ttm_dma_tt *tt)
  948. {
  949. unsigned i, j;
  950. int r;
  951. r = ttm_pool_populate(&tt->ttm);
  952. if (r)
  953. return r;
  954. for (i = 0; i < tt->ttm.num_pages; ++i) {
  955. struct page *p = tt->ttm.pages[i];
  956. size_t num_pages = 1;
  957. for (j = i + 1; j < tt->ttm.num_pages; ++j) {
  958. if (++p != tt->ttm.pages[j])
  959. break;
  960. ++num_pages;
  961. }
  962. tt->dma_address[i] = dma_map_page(dev, tt->ttm.pages[i],
  963. 0, num_pages * PAGE_SIZE,
  964. DMA_BIDIRECTIONAL);
  965. if (dma_mapping_error(dev, tt->dma_address[i])) {
  966. while (i--) {
  967. dma_unmap_page(dev, tt->dma_address[i],
  968. PAGE_SIZE, DMA_BIDIRECTIONAL);
  969. tt->dma_address[i] = 0;
  970. }
  971. ttm_pool_unpopulate(&tt->ttm);
  972. return -EFAULT;
  973. }
  974. for (j = 1; j < num_pages; ++j) {
  975. tt->dma_address[i + 1] = tt->dma_address[i] + PAGE_SIZE;
  976. ++i;
  977. }
  978. }
  979. return 0;
  980. }
  981. EXPORT_SYMBOL(ttm_populate_and_map_pages);
  982. void ttm_unmap_and_unpopulate_pages(struct device *dev, struct ttm_dma_tt *tt)
  983. {
  984. unsigned i, j;
  985. for (i = 0; i < tt->ttm.num_pages;) {
  986. struct page *p = tt->ttm.pages[i];
  987. size_t num_pages = 1;
  988. if (!tt->dma_address[i] || !tt->ttm.pages[i]) {
  989. ++i;
  990. continue;
  991. }
  992. for (j = i + 1; j < tt->ttm.num_pages; ++j) {
  993. if (++p != tt->ttm.pages[j])
  994. break;
  995. ++num_pages;
  996. }
  997. dma_unmap_page(dev, tt->dma_address[i], num_pages * PAGE_SIZE,
  998. DMA_BIDIRECTIONAL);
  999. i += num_pages;
  1000. }
  1001. ttm_pool_unpopulate(&tt->ttm);
  1002. }
  1003. EXPORT_SYMBOL(ttm_unmap_and_unpopulate_pages);
  1004. int ttm_page_alloc_debugfs(struct seq_file *m, void *data)
  1005. {
  1006. struct ttm_page_pool *p;
  1007. unsigned i;
  1008. char *h[] = {"pool", "refills", "pages freed", "size"};
  1009. if (!_manager) {
  1010. seq_printf(m, "No pool allocator running.\n");
  1011. return 0;
  1012. }
  1013. seq_printf(m, "%7s %12s %13s %8s\n",
  1014. h[0], h[1], h[2], h[3]);
  1015. for (i = 0; i < NUM_POOLS; ++i) {
  1016. p = &_manager->pools[i];
  1017. seq_printf(m, "%7s %12ld %13ld %8d\n",
  1018. p->name, p->nrefills,
  1019. p->nfrees, p->npages);
  1020. }
  1021. return 0;
  1022. }
  1023. EXPORT_SYMBOL(ttm_page_alloc_debugfs);