1 // SPDX-License-Identifier: GPL-2.0 OR MIT 2 /* 3 * Copyright 2020 Advanced Micro Devices, Inc. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in 13 * all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 21 * OTHER DEALINGS IN THE SOFTWARE. 22 * 23 * Authors: Christian König 24 */ 25 26 /* Pooling of allocated pages is necessary because changing the caching 27 * attributes on x86 of the linear mapping requires a costly cross CPU TLB 28 * invalidate for those addresses. 29 * 30 * Additional to that allocations from the DMA coherent API are pooled as well 31 * cause they are rather slow compared to alloc_pages+map. 32 */ 33 34 #include <linux/export.h> 35 #include <linux/module.h> 36 #include <linux/dma-mapping.h> 37 #include <linux/debugfs.h> 38 #include <linux/highmem.h> 39 #include <linux/sched/mm.h> 40 41 #ifdef CONFIG_X86 42 #include <asm/set_memory.h> 43 #endif 44 45 #include <drm/ttm/ttm_backup.h> 46 #include <drm/ttm/ttm_pool.h> 47 #include <drm/ttm/ttm_tt.h> 48 #include <drm/ttm/ttm_bo.h> 49 50 #include "ttm_module.h" 51 #include "ttm_pool_internal.h" 52 53 #ifdef CONFIG_FAULT_INJECTION 54 #include <linux/fault-inject.h> 55 static DECLARE_FAULT_ATTR(backup_fault_inject); 56 #else 57 #define should_fail(...) false 58 #endif 59 60 /** 61 * struct ttm_pool_dma - Helper object for coherent DMA mappings 62 * 63 * @addr: original DMA address returned for the mapping 64 * @vaddr: original vaddr return for the mapping and order in the lower bits 65 */ 66 struct ttm_pool_dma { 67 dma_addr_t addr; 68 unsigned long vaddr; 69 }; 70 71 /** 72 * struct ttm_pool_alloc_state - Current state of the tt page allocation process 73 * @pages: Pointer to the next tt page pointer to populate. 74 * @caching_divide: Pointer to the first page pointer whose page has a staged but 75 * not committed caching transition from write-back to @tt_caching. 76 * @dma_addr: Pointer to the next tt dma_address entry to populate if any. 77 * @remaining_pages: Remaining pages to populate. 78 * @tt_caching: The requested cpu-caching for the pages allocated. 79 */ 80 struct ttm_pool_alloc_state { 81 struct page **pages; 82 struct page **caching_divide; 83 dma_addr_t *dma_addr; 84 pgoff_t remaining_pages; 85 enum ttm_caching tt_caching; 86 }; 87 88 /** 89 * struct ttm_pool_tt_restore - State representing restore from backup 90 * @pool: The pool used for page allocation while restoring. 91 * @snapshot_alloc: A snapshot of the most recent struct ttm_pool_alloc_state. 92 * @alloced_page: Pointer to the page most recently allocated from a pool or system. 93 * @first_dma: The dma address corresponding to @alloced_page if dma_mapping 94 * is requested. 95 * @alloced_pages: The number of allocated pages present in the struct ttm_tt 96 * page vector from this restore session. 97 * @restored_pages: The number of 4K pages restored for @alloced_page (which 98 * is typically a multi-order page). 99 * @page_caching: The struct ttm_tt requested caching 100 * @order: The order of @alloced_page. 101 * 102 * Recovery from backup might fail when we've recovered less than the 103 * full ttm_tt. In order not to loose any data (yet), keep information 104 * around that allows us to restart a failed ttm backup recovery. 105 */ 106 struct ttm_pool_tt_restore { 107 struct ttm_pool *pool; 108 struct ttm_pool_alloc_state snapshot_alloc; 109 struct page *alloced_page; 110 dma_addr_t first_dma; 111 pgoff_t alloced_pages; 112 pgoff_t restored_pages; 113 enum ttm_caching page_caching; 114 unsigned int order; 115 }; 116 117 static unsigned long page_pool_size; 118 119 MODULE_PARM_DESC(page_pool_size, "Number of pages in the WC/UC/DMA pool per NUMA node"); 120 module_param(page_pool_size, ulong, 0644); 121 122 static unsigned long pool_node_limit[MAX_NUMNODES]; 123 static atomic_long_t allocated_pages[MAX_NUMNODES]; 124 125 static struct ttm_pool_type global_write_combined[NR_PAGE_ORDERS]; 126 static struct ttm_pool_type global_uncached[NR_PAGE_ORDERS]; 127 128 static struct ttm_pool_type global_dma32_write_combined[NR_PAGE_ORDERS]; 129 static struct ttm_pool_type global_dma32_uncached[NR_PAGE_ORDERS]; 130 131 static spinlock_t shrinker_lock; 132 static struct list_head shrinker_list; 133 static struct shrinker *mm_shrinker; 134 static DECLARE_RWSEM(pool_shrink_rwsem); 135 136 static int ttm_pool_nid(struct ttm_pool *pool) 137 { 138 int nid = NUMA_NO_NODE; 139 if (pool) 140 nid = pool->nid; 141 if (nid == NUMA_NO_NODE) 142 nid = numa_node_id(); 143 return nid; 144 } 145 146 /* Allocate pages of size 1 << order with the given gfp_flags */ 147 static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags, 148 unsigned int order) 149 { 150 const unsigned int beneficial_order = ttm_pool_beneficial_order(pool); 151 unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS; 152 struct ttm_pool_dma *dma; 153 struct page *p; 154 void *vaddr; 155 156 /* Don't set the __GFP_COMP flag for higher order allocations. 157 * Mapping pages directly into an userspace process and calling 158 * put_page() on a TTM allocated page is illegal. 159 */ 160 if (order) 161 gfp_flags |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN | 162 __GFP_THISNODE; 163 164 /* 165 * Do not add latency to the allocation path for allocations orders 166 * device tolds us do not bring them additional performance gains. 167 */ 168 if (beneficial_order && order > beneficial_order) 169 gfp_flags &= ~__GFP_DIRECT_RECLAIM; 170 171 if (!ttm_pool_uses_dma_alloc(pool)) { 172 p = alloc_pages_node(pool->nid, gfp_flags, order); 173 if (p) { 174 p->private = order; 175 mod_lruvec_page_state(p, NR_GPU_ACTIVE, 1 << order); 176 } 177 return p; 178 } 179 180 dma = kmalloc_obj(*dma); 181 if (!dma) 182 return NULL; 183 184 if (order) 185 attr |= DMA_ATTR_NO_WARN; 186 187 vaddr = dma_alloc_attrs(pool->dev, (1ULL << order) * PAGE_SIZE, 188 &dma->addr, gfp_flags, attr); 189 if (!vaddr) 190 goto error_free; 191 192 /* TODO: This is an illegal abuse of the DMA API, but we need to rework 193 * TTM page fault handling and extend the DMA API to clean this up. 194 */ 195 if (is_vmalloc_addr(vaddr)) 196 p = vmalloc_to_page(vaddr); 197 else 198 p = virt_to_page(vaddr); 199 200 dma->vaddr = (unsigned long)vaddr | order; 201 p->private = (unsigned long)dma; 202 return p; 203 204 error_free: 205 kfree(dma); 206 return NULL; 207 } 208 209 /* Reset the caching and pages of size 1 << order */ 210 static void ttm_pool_free_page(struct ttm_pool *pool, enum ttm_caching caching, 211 unsigned int order, struct page *p, bool reclaim) 212 { 213 unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS; 214 struct ttm_pool_dma *dma; 215 void *vaddr; 216 217 #ifdef CONFIG_X86 218 /* We don't care that set_pages_wb is inefficient here. This is only 219 * used when we have to shrink and CPU overhead is irrelevant then. 220 */ 221 if (caching != ttm_cached && !PageHighMem(p)) 222 set_pages_wb(p, 1 << order); 223 #endif 224 225 if (!pool || !ttm_pool_uses_dma_alloc(pool)) { 226 mod_lruvec_page_state(p, reclaim ? NR_GPU_RECLAIM : NR_GPU_ACTIVE, 227 -(1 << order)); 228 __free_pages(p, order); 229 return; 230 } 231 232 if (order) 233 attr |= DMA_ATTR_NO_WARN; 234 235 dma = (void *)p->private; 236 vaddr = (void *)(dma->vaddr & PAGE_MASK); 237 dma_free_attrs(pool->dev, (1UL << order) * PAGE_SIZE, vaddr, dma->addr, 238 attr); 239 kfree(dma); 240 } 241 242 /* Apply any cpu-caching deferred during page allocation */ 243 static int ttm_pool_apply_caching(struct ttm_pool_alloc_state *alloc) 244 { 245 #ifdef CONFIG_X86 246 unsigned int num_pages = alloc->pages - alloc->caching_divide; 247 248 if (!num_pages) 249 return 0; 250 251 switch (alloc->tt_caching) { 252 case ttm_cached: 253 break; 254 case ttm_write_combined: 255 return set_pages_array_wc(alloc->caching_divide, num_pages); 256 case ttm_uncached: 257 return set_pages_array_uc(alloc->caching_divide, num_pages); 258 } 259 #endif 260 alloc->caching_divide = alloc->pages; 261 return 0; 262 } 263 264 /* DMA Map pages of 1 << order size and return the resulting dma_address. */ 265 static int ttm_pool_map(struct ttm_pool *pool, unsigned int order, 266 struct page *p, dma_addr_t *dma_addr) 267 { 268 dma_addr_t addr; 269 270 if (ttm_pool_uses_dma_alloc(pool)) { 271 struct ttm_pool_dma *dma = (void *)p->private; 272 273 addr = dma->addr; 274 } else { 275 size_t size = (1ULL << order) * PAGE_SIZE; 276 277 addr = dma_map_page(pool->dev, p, 0, size, DMA_BIDIRECTIONAL); 278 if (dma_mapping_error(pool->dev, addr)) 279 return -EFAULT; 280 } 281 282 *dma_addr = addr; 283 284 return 0; 285 } 286 287 /* Unmap pages of 1 << order size */ 288 static void ttm_pool_unmap(struct ttm_pool *pool, dma_addr_t dma_addr, 289 unsigned int num_pages) 290 { 291 /* Unmapped while freeing the page */ 292 if (ttm_pool_uses_dma_alloc(pool)) 293 return; 294 295 dma_unmap_page(pool->dev, dma_addr, (long)num_pages << PAGE_SHIFT, 296 DMA_BIDIRECTIONAL); 297 } 298 299 /* Give pages into a specific pool_type */ 300 static void ttm_pool_type_give(struct ttm_pool_type *pt, struct page *p) 301 { 302 unsigned int i, num_pages = 1 << pt->order; 303 int nid = page_to_nid(p); 304 305 for (i = 0; i < num_pages; ++i) { 306 if (PageHighMem(p)) 307 clear_highpage(p + i); 308 else 309 clear_page(page_address(p + i)); 310 } 311 312 INIT_LIST_HEAD(&p->lru); 313 rcu_read_lock(); 314 list_lru_add(&pt->pages, &p->lru, nid, NULL); 315 rcu_read_unlock(); 316 317 atomic_long_add(num_pages, &allocated_pages[nid]); 318 mod_lruvec_page_state(p, NR_GPU_ACTIVE, -num_pages); 319 mod_lruvec_page_state(p, NR_GPU_RECLAIM, num_pages); 320 } 321 322 static enum lru_status take_one_from_lru(struct list_head *item, 323 struct list_lru_one *list, 324 void *cb_arg) 325 { 326 struct page **out_page = cb_arg; 327 struct page *p = container_of(item, struct page, lru); 328 list_lru_isolate(list, item); 329 330 *out_page = p; 331 return LRU_REMOVED; 332 } 333 334 /* Take pages from a specific pool_type, return NULL when nothing available */ 335 static struct page *ttm_pool_type_take(struct ttm_pool_type *pt, int nid) 336 { 337 int ret; 338 struct page *p = NULL; 339 unsigned long nr_to_walk = 1; 340 341 ret = list_lru_walk_node(&pt->pages, nid, take_one_from_lru, (void *)&p, &nr_to_walk); 342 if (ret == 1 && p) { 343 atomic_long_sub(1 << pt->order, &allocated_pages[nid]); 344 mod_lruvec_page_state(p, NR_GPU_ACTIVE, (1 << pt->order)); 345 mod_lruvec_page_state(p, NR_GPU_RECLAIM, -(1 << pt->order)); 346 } 347 return p; 348 } 349 350 /* Initialize and add a pool type to the global shrinker list */ 351 static void ttm_pool_type_init(struct ttm_pool_type *pt, struct ttm_pool *pool, 352 enum ttm_caching caching, unsigned int order) 353 { 354 pt->pool = pool; 355 pt->caching = caching; 356 pt->order = order; 357 list_lru_init(&pt->pages); 358 359 spin_lock(&shrinker_lock); 360 list_add_tail(&pt->shrinker_list, &shrinker_list); 361 spin_unlock(&shrinker_lock); 362 } 363 364 static enum lru_status pool_move_to_dispose_list(struct list_head *item, 365 struct list_lru_one *list, 366 void *cb_arg) 367 { 368 struct list_head *dispose = cb_arg; 369 370 list_lru_isolate_move(list, item, dispose); 371 372 return LRU_REMOVED; 373 } 374 375 static void ttm_pool_dispose_list(struct ttm_pool_type *pt, 376 struct list_head *dispose) 377 { 378 while (!list_empty(dispose)) { 379 struct page *p; 380 p = list_first_entry(dispose, struct page, lru); 381 list_del_init(&p->lru); 382 atomic_long_sub(1 << pt->order, &allocated_pages[page_to_nid(p)]); 383 ttm_pool_free_page(pt->pool, pt->caching, pt->order, p, true); 384 } 385 } 386 387 /* Remove a pool_type from the global shrinker list and free all pages */ 388 static void ttm_pool_type_fini(struct ttm_pool_type *pt) 389 { 390 LIST_HEAD(dispose); 391 392 spin_lock(&shrinker_lock); 393 list_del(&pt->shrinker_list); 394 spin_unlock(&shrinker_lock); 395 396 list_lru_walk(&pt->pages, pool_move_to_dispose_list, &dispose, LONG_MAX); 397 ttm_pool_dispose_list(pt, &dispose); 398 } 399 400 /* Return the pool_type to use for the given caching and order */ 401 static struct ttm_pool_type *ttm_pool_select_type(struct ttm_pool *pool, 402 enum ttm_caching caching, 403 unsigned int order) 404 { 405 if (ttm_pool_uses_dma_alloc(pool)) 406 return &pool->caching[caching].orders[order]; 407 408 #ifdef CONFIG_X86 409 switch (caching) { 410 case ttm_write_combined: 411 if (ttm_pool_uses_dma32(pool)) 412 return &global_dma32_write_combined[order]; 413 414 return &global_write_combined[order]; 415 case ttm_uncached: 416 if (ttm_pool_uses_dma32(pool)) 417 return &global_dma32_uncached[order]; 418 419 return &global_uncached[order]; 420 default: 421 break; 422 } 423 #endif 424 425 return NULL; 426 } 427 428 /* Free pages using the per-node shrinker list */ 429 static unsigned int ttm_pool_shrink(int nid, unsigned long num_to_free) 430 { 431 LIST_HEAD(dispose); 432 struct ttm_pool_type *pt; 433 unsigned int num_pages; 434 435 down_read(&pool_shrink_rwsem); 436 spin_lock(&shrinker_lock); 437 pt = list_first_entry(&shrinker_list, typeof(*pt), shrinker_list); 438 list_move_tail(&pt->shrinker_list, &shrinker_list); 439 spin_unlock(&shrinker_lock); 440 441 num_pages = list_lru_walk_node(&pt->pages, nid, pool_move_to_dispose_list, &dispose, &num_to_free); 442 num_pages *= 1 << pt->order; 443 444 ttm_pool_dispose_list(pt, &dispose); 445 up_read(&pool_shrink_rwsem); 446 447 return num_pages; 448 } 449 450 /* Return the allocation order based for a page */ 451 static unsigned int ttm_pool_page_order(struct ttm_pool *pool, struct page *p) 452 { 453 if (ttm_pool_uses_dma_alloc(pool)) { 454 struct ttm_pool_dma *dma = (void *)p->private; 455 456 return dma->vaddr & ~PAGE_MASK; 457 } 458 459 return p->private; 460 } 461 462 /* 463 * Split larger pages so that we can free each PAGE_SIZE page as soon 464 * as it has been backed up, in order to avoid memory pressure during 465 * reclaim. 466 */ 467 static void ttm_pool_split_for_swap(struct ttm_pool *pool, struct page *p) 468 { 469 unsigned int order = ttm_pool_page_order(pool, p); 470 pgoff_t nr; 471 472 if (!order) 473 return; 474 475 split_page(p, order); 476 nr = 1UL << order; 477 while (nr--) 478 (p++)->private = 0; 479 } 480 481 /** 482 * DOC: Partial backup and restoration of a struct ttm_tt. 483 * 484 * Swapout using ttm_backup_backup_page() and swapin using 485 * ttm_backup_copy_page() may fail. 486 * The former most likely due to lack of swap-space or memory, the latter due 487 * to lack of memory or because of signal interruption during waits. 488 * 489 * Backup failure is easily handled by using a ttm_tt pages vector that holds 490 * both backup handles and page pointers. This has to be taken into account when 491 * restoring such a ttm_tt from backup, and when freeing it while backed up. 492 * When restoring, for simplicity, new pages are actually allocated from the 493 * pool and the contents of any old pages are copied in and then the old pages 494 * are released. 495 * 496 * For restoration failures, the struct ttm_pool_tt_restore holds sufficient state 497 * to be able to resume an interrupted restore, and that structure is freed once 498 * the restoration is complete. If the struct ttm_tt is destroyed while there 499 * is a valid struct ttm_pool_tt_restore attached, that is also properly taken 500 * care of. 501 */ 502 503 /* Is restore ongoing for the currently allocated page? */ 504 static bool ttm_pool_restore_valid(const struct ttm_pool_tt_restore *restore) 505 { 506 return restore && restore->restored_pages < (1 << restore->order); 507 } 508 509 /* DMA unmap and free a multi-order page, either to the relevant pool or to system. */ 510 static pgoff_t ttm_pool_unmap_and_free(struct ttm_pool *pool, struct page *page, 511 const dma_addr_t *dma_addr, enum ttm_caching caching) 512 { 513 struct ttm_pool_type *pt = NULL; 514 unsigned int order; 515 pgoff_t nr; 516 517 if (pool) { 518 order = ttm_pool_page_order(pool, page); 519 nr = (1UL << order); 520 if (dma_addr) 521 ttm_pool_unmap(pool, *dma_addr, nr); 522 523 pt = ttm_pool_select_type(pool, caching, order); 524 } else { 525 order = page->private; 526 nr = (1UL << order); 527 } 528 529 if (pt) 530 ttm_pool_type_give(pt, page); 531 else 532 ttm_pool_free_page(pool, caching, order, page, false); 533 534 return nr; 535 } 536 537 /* Populate the page-array using the most recent allocated multi-order page. */ 538 static void ttm_pool_allocated_page_commit(struct page *allocated, 539 dma_addr_t first_dma, 540 struct ttm_pool_alloc_state *alloc, 541 pgoff_t nr) 542 { 543 pgoff_t i; 544 545 for (i = 0; i < nr; ++i) 546 *alloc->pages++ = allocated++; 547 548 alloc->remaining_pages -= nr; 549 550 if (!alloc->dma_addr) 551 return; 552 553 for (i = 0; i < nr; ++i) { 554 *alloc->dma_addr++ = first_dma; 555 first_dma += PAGE_SIZE; 556 } 557 } 558 559 /* 560 * When restoring, restore backed-up content to the newly allocated page and 561 * if successful, populate the page-table and dma-address arrays. 562 */ 563 static int ttm_pool_restore_commit(struct ttm_pool_tt_restore *restore, 564 struct file *backup, 565 const struct ttm_operation_ctx *ctx, 566 struct ttm_pool_alloc_state *alloc) 567 568 { 569 pgoff_t i, nr = 1UL << restore->order; 570 struct page **first_page = alloc->pages; 571 struct page *p; 572 int ret = 0; 573 574 for (i = restore->restored_pages; i < nr; ++i) { 575 p = first_page[i]; 576 if (ttm_backup_page_ptr_is_handle(p)) { 577 unsigned long handle = ttm_backup_page_ptr_to_handle(p); 578 gfp_t additional_gfp = ctx->gfp_retry_mayfail ? 579 __GFP_RETRY_MAYFAIL | __GFP_NOWARN : 0; 580 581 if (IS_ENABLED(CONFIG_FAULT_INJECTION) && ctx->interruptible && 582 should_fail(&backup_fault_inject, 1)) { 583 ret = -EINTR; 584 break; 585 } 586 587 if (handle == 0) { 588 restore->restored_pages++; 589 continue; 590 } 591 592 ret = ttm_backup_copy_page(backup, restore->alloced_page + i, 593 handle, ctx->interruptible, 594 additional_gfp); 595 if (ret) 596 break; 597 598 ttm_backup_drop(backup, handle); 599 } else if (p) { 600 /* 601 * We could probably avoid splitting the old page 602 * using clever logic, but ATM we don't care, as 603 * we prioritize releasing memory ASAP. Note that 604 * here, the old retained page is always write-back 605 * cached. 606 */ 607 ttm_pool_split_for_swap(restore->pool, p); 608 copy_highpage(restore->alloced_page + i, p); 609 __free_pages(p, 0); 610 } 611 612 restore->restored_pages++; 613 first_page[i] = ttm_backup_handle_to_page_ptr(0); 614 } 615 616 if (ret) { 617 if (!restore->restored_pages) { 618 dma_addr_t *dma_addr = alloc->dma_addr ? &restore->first_dma : NULL; 619 620 ttm_pool_unmap_and_free(restore->pool, restore->alloced_page, 621 dma_addr, restore->page_caching); 622 restore->restored_pages = nr; 623 } 624 return ret; 625 } 626 627 ttm_pool_allocated_page_commit(restore->alloced_page, restore->first_dma, 628 alloc, nr); 629 if (restore->page_caching == alloc->tt_caching || PageHighMem(restore->alloced_page)) 630 alloc->caching_divide = alloc->pages; 631 restore->snapshot_alloc = *alloc; 632 restore->alloced_pages += nr; 633 634 return 0; 635 } 636 637 /* If restoring, save information needed for ttm_pool_restore_commit(). */ 638 static void 639 ttm_pool_page_allocated_restore(struct ttm_pool *pool, unsigned int order, 640 struct page *p, 641 enum ttm_caching page_caching, 642 dma_addr_t first_dma, 643 struct ttm_pool_tt_restore *restore, 644 const struct ttm_pool_alloc_state *alloc) 645 { 646 restore->pool = pool; 647 restore->order = order; 648 restore->restored_pages = 0; 649 restore->page_caching = page_caching; 650 restore->first_dma = first_dma; 651 restore->alloced_page = p; 652 restore->snapshot_alloc = *alloc; 653 } 654 655 /* 656 * Called when we got a page, either from a pool or newly allocated. 657 * if needed, dma map the page and populate the dma address array. 658 * Populate the page address array. 659 * If the caching is consistent, update any deferred caching. Otherwise 660 * stage this page for an upcoming deferred caching update. 661 */ 662 static int ttm_pool_page_allocated(struct ttm_pool *pool, unsigned int order, 663 struct page *p, enum ttm_caching page_caching, 664 struct ttm_pool_alloc_state *alloc, 665 struct ttm_pool_tt_restore *restore) 666 { 667 bool caching_consistent; 668 dma_addr_t first_dma; 669 int r = 0; 670 671 caching_consistent = (page_caching == alloc->tt_caching) || PageHighMem(p); 672 673 if (caching_consistent) { 674 r = ttm_pool_apply_caching(alloc); 675 if (r) 676 return r; 677 } 678 679 if (alloc->dma_addr) { 680 r = ttm_pool_map(pool, order, p, &first_dma); 681 if (r) 682 return r; 683 } 684 685 if (restore) { 686 ttm_pool_page_allocated_restore(pool, order, p, page_caching, 687 first_dma, restore, alloc); 688 } else { 689 ttm_pool_allocated_page_commit(p, first_dma, alloc, 1UL << order); 690 691 if (caching_consistent) 692 alloc->caching_divide = alloc->pages; 693 } 694 695 return 0; 696 } 697 698 /** 699 * ttm_pool_free_range() - Free a range of TTM pages 700 * @pool: The pool used for allocating. 701 * @tt: The struct ttm_tt holding the page pointers. 702 * @caching: The page caching mode used by the range. 703 * @start_page: index for first page to free. 704 * @end_page: index for last page to free + 1. 705 * 706 * During allocation the ttm_tt page-vector may be populated with ranges of 707 * pages with different attributes if allocation hit an error without being 708 * able to completely fulfill the allocation. This function can be used 709 * to free these individual ranges. 710 */ 711 static void ttm_pool_free_range(struct ttm_pool *pool, struct ttm_tt *tt, 712 enum ttm_caching caching, 713 pgoff_t start_page, pgoff_t end_page) 714 { 715 struct page **pages = &tt->pages[start_page]; 716 struct file *backup = tt->backup; 717 pgoff_t i, nr; 718 719 for (i = start_page; i < end_page; i += nr, pages += nr) { 720 struct page *p = *pages; 721 722 nr = 1; 723 if (ttm_backup_page_ptr_is_handle(p)) { 724 unsigned long handle = ttm_backup_page_ptr_to_handle(p); 725 726 if (handle != 0) 727 ttm_backup_drop(backup, handle); 728 } else if (p) { 729 dma_addr_t *dma_addr = tt->dma_address ? 730 tt->dma_address + i : NULL; 731 732 nr = ttm_pool_unmap_and_free(pool, p, dma_addr, caching); 733 } 734 } 735 } 736 737 static void ttm_pool_alloc_state_init(const struct ttm_tt *tt, 738 struct ttm_pool_alloc_state *alloc) 739 { 740 alloc->pages = tt->pages; 741 alloc->caching_divide = tt->pages; 742 alloc->dma_addr = tt->dma_address; 743 alloc->remaining_pages = tt->num_pages; 744 alloc->tt_caching = tt->caching; 745 } 746 747 /* 748 * Find a suitable allocation order based on highest desired order 749 * and number of remaining pages 750 */ 751 static unsigned int ttm_pool_alloc_find_order(unsigned int highest, 752 const struct ttm_pool_alloc_state *alloc) 753 { 754 return min_t(unsigned int, highest, __fls(alloc->remaining_pages)); 755 } 756 757 static int __ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt, 758 const struct ttm_operation_ctx *ctx, 759 struct ttm_pool_alloc_state *alloc, 760 struct ttm_pool_tt_restore *restore) 761 { 762 enum ttm_caching page_caching; 763 gfp_t gfp_flags = GFP_USER; 764 pgoff_t caching_divide; 765 unsigned int order; 766 bool allow_pools; 767 struct page *p; 768 int r; 769 770 WARN_ON(!alloc->remaining_pages || ttm_tt_is_populated(tt)); 771 WARN_ON(alloc->dma_addr && !pool->dev); 772 773 if (tt->page_flags & TTM_TT_FLAG_ZERO_ALLOC) 774 gfp_flags |= __GFP_ZERO; 775 776 if (ctx->gfp_retry_mayfail) 777 gfp_flags |= __GFP_RETRY_MAYFAIL | __GFP_NOWARN; 778 779 if (ttm_pool_uses_dma32(pool)) 780 gfp_flags |= GFP_DMA32; 781 else 782 gfp_flags |= GFP_HIGHUSER; 783 784 page_caching = tt->caching; 785 allow_pools = true; 786 for (order = ttm_pool_alloc_find_order(MAX_PAGE_ORDER, alloc); 787 alloc->remaining_pages; 788 order = ttm_pool_alloc_find_order(order, alloc)) { 789 struct ttm_pool_type *pt; 790 791 /* First, try to allocate a page from a pool if one exists. */ 792 p = NULL; 793 pt = ttm_pool_select_type(pool, page_caching, order); 794 if (pt && allow_pools) 795 p = ttm_pool_type_take(pt, ttm_pool_nid(pool)); 796 797 /* 798 * If that fails or previously failed, allocate from system. 799 * Note that this also disallows additional pool allocations using 800 * write-back cached pools of the same order. Consider removing 801 * that behaviour. 802 */ 803 if (!p) { 804 page_caching = ttm_cached; 805 allow_pools = false; 806 p = ttm_pool_alloc_page(pool, gfp_flags, order); 807 } 808 /* If that fails, lower the order if possible and retry. */ 809 if (!p) { 810 if (order) { 811 --order; 812 page_caching = tt->caching; 813 allow_pools = true; 814 continue; 815 } 816 r = -ENOMEM; 817 goto error_free_all; 818 } 819 r = ttm_pool_page_allocated(pool, order, p, page_caching, alloc, 820 restore); 821 if (r) 822 goto error_free_page; 823 824 if (ttm_pool_restore_valid(restore)) { 825 r = ttm_pool_restore_commit(restore, tt->backup, ctx, alloc); 826 if (r) 827 goto error_free_all; 828 } 829 } 830 831 r = ttm_pool_apply_caching(alloc); 832 if (r) 833 goto error_free_all; 834 835 kfree(tt->restore); 836 tt->restore = NULL; 837 838 return 0; 839 840 error_free_page: 841 ttm_pool_free_page(pool, page_caching, order, p, false); 842 843 error_free_all: 844 if (tt->restore) 845 return r; 846 847 caching_divide = alloc->caching_divide - tt->pages; 848 ttm_pool_free_range(pool, tt, tt->caching, 0, caching_divide); 849 ttm_pool_free_range(pool, tt, ttm_cached, caching_divide, 850 tt->num_pages - alloc->remaining_pages); 851 852 return r; 853 } 854 855 /** 856 * ttm_pool_alloc - Fill a ttm_tt object 857 * 858 * @pool: ttm_pool to use 859 * @tt: ttm_tt object to fill 860 * @ctx: operation context 861 * 862 * Fill the ttm_tt object with pages and also make sure to DMA map them when 863 * necessary. 864 * 865 * Returns: 0 on successe, negative error code otherwise. 866 */ 867 int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt, 868 struct ttm_operation_ctx *ctx) 869 { 870 struct ttm_pool_alloc_state alloc; 871 872 if (WARN_ON(ttm_tt_is_backed_up(tt))) 873 return -EINVAL; 874 875 ttm_pool_alloc_state_init(tt, &alloc); 876 877 return __ttm_pool_alloc(pool, tt, ctx, &alloc, NULL); 878 } 879 EXPORT_SYMBOL(ttm_pool_alloc); 880 881 /** 882 * ttm_pool_restore_and_alloc - Fill a ttm_tt, restoring previously backed-up 883 * content. 884 * 885 * @pool: ttm_pool to use 886 * @tt: ttm_tt object to fill 887 * @ctx: operation context 888 * 889 * Fill the ttm_tt object with pages and also make sure to DMA map them when 890 * necessary. Read in backed-up content. 891 * 892 * Returns: 0 on successe, negative error code otherwise. 893 */ 894 int ttm_pool_restore_and_alloc(struct ttm_pool *pool, struct ttm_tt *tt, 895 const struct ttm_operation_ctx *ctx) 896 { 897 struct ttm_pool_tt_restore *restore = tt->restore; 898 struct ttm_pool_alloc_state alloc; 899 900 if (WARN_ON(!ttm_tt_is_backed_up(tt))) 901 return -EINVAL; 902 903 if (!restore) { 904 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; 905 906 ttm_pool_alloc_state_init(tt, &alloc); 907 if (ctx->gfp_retry_mayfail) 908 gfp |= __GFP_RETRY_MAYFAIL; 909 910 restore = kzalloc_obj(*restore, gfp); 911 if (!restore) 912 return -ENOMEM; 913 914 restore->snapshot_alloc = alloc; 915 restore->pool = pool; 916 restore->restored_pages = 1; 917 918 tt->restore = restore; 919 } else { 920 alloc = restore->snapshot_alloc; 921 if (ttm_pool_restore_valid(restore)) { 922 int ret = ttm_pool_restore_commit(restore, tt->backup, 923 ctx, &alloc); 924 925 if (ret) 926 return ret; 927 } 928 if (!alloc.remaining_pages) 929 return 0; 930 } 931 932 return __ttm_pool_alloc(pool, tt, ctx, &alloc, restore); 933 } 934 935 /** 936 * ttm_pool_free - Free the backing pages from a ttm_tt object 937 * 938 * @pool: Pool to give pages back to. 939 * @tt: ttm_tt object to unpopulate 940 * 941 * Give the packing pages back to a pool or free them 942 */ 943 void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt) 944 { 945 int nid = ttm_pool_nid(pool); 946 947 ttm_pool_free_range(pool, tt, tt->caching, 0, tt->num_pages); 948 949 while (atomic_long_read(&allocated_pages[nid]) > pool_node_limit[nid]) { 950 unsigned long diff = atomic_long_read(&allocated_pages[nid]) - pool_node_limit[nid]; 951 ttm_pool_shrink(nid, diff); 952 } 953 } 954 EXPORT_SYMBOL(ttm_pool_free); 955 956 /** 957 * ttm_pool_drop_backed_up() - Release content of a swapped-out struct ttm_tt 958 * @tt: The struct ttm_tt. 959 * 960 * Release handles with associated content or any remaining pages of 961 * a backed-up struct ttm_tt. 962 */ 963 void ttm_pool_drop_backed_up(struct ttm_tt *tt) 964 { 965 struct ttm_pool_tt_restore *restore; 966 pgoff_t start_page = 0; 967 968 WARN_ON(!ttm_tt_is_backed_up(tt)); 969 970 restore = tt->restore; 971 972 /* 973 * Unmap and free any uncommitted restore page. 974 * any tt page-array backup entries already read back has 975 * been cleared already 976 */ 977 if (ttm_pool_restore_valid(restore)) { 978 dma_addr_t *dma_addr = tt->dma_address ? &restore->first_dma : NULL; 979 980 ttm_pool_unmap_and_free(restore->pool, restore->alloced_page, 981 dma_addr, restore->page_caching); 982 restore->restored_pages = 1UL << restore->order; 983 } 984 985 /* 986 * If a restore is ongoing, part of the tt pages may have a 987 * caching different than writeback. 988 */ 989 if (restore) { 990 pgoff_t mid = restore->snapshot_alloc.caching_divide - tt->pages; 991 992 start_page = restore->alloced_pages; 993 WARN_ON(mid > start_page); 994 /* Pages that might be dma-mapped and non-cached */ 995 ttm_pool_free_range(restore->pool, tt, tt->caching, 996 0, mid); 997 /* Pages that might be dma-mapped but cached */ 998 ttm_pool_free_range(restore->pool, tt, ttm_cached, 999 mid, restore->alloced_pages); 1000 kfree(restore); 1001 tt->restore = NULL; 1002 } 1003 1004 ttm_pool_free_range(NULL, tt, ttm_cached, start_page, tt->num_pages); 1005 } 1006 1007 /** 1008 * ttm_pool_backup() - Back up or purge a struct ttm_tt 1009 * @pool: The pool used when allocating the struct ttm_tt. 1010 * @tt: The struct ttm_tt. 1011 * @flags: Flags to govern the backup behaviour. 1012 * 1013 * Back up or purge a struct ttm_tt. If @purge is true, then 1014 * all pages will be freed directly to the system rather than to the pool 1015 * they were allocated from, making the function behave similarly to 1016 * ttm_pool_free(). If @purge is false the pages will be backed up instead, 1017 * exchanged for handles. 1018 * A subsequent call to ttm_pool_restore_and_alloc() will then read back the content and 1019 * a subsequent call to ttm_pool_drop_backed_up() will drop it. 1020 * If backup of a page fails for whatever reason, @ttm will still be 1021 * partially backed up, retaining those pages for which backup fails. 1022 * In that case, this function can be retried, possibly after freeing up 1023 * memory resources. 1024 * 1025 * Return: Number of pages actually backed up or freed, or negative 1026 * error code on error. 1027 */ 1028 long ttm_pool_backup(struct ttm_pool *pool, struct ttm_tt *tt, 1029 const struct ttm_backup_flags *flags) 1030 { 1031 struct file *backup = tt->backup; 1032 struct page *page; 1033 unsigned long handle; 1034 gfp_t alloc_gfp; 1035 gfp_t gfp; 1036 int ret = 0; 1037 pgoff_t shrunken = 0; 1038 pgoff_t i, num_pages; 1039 1040 if (WARN_ON(ttm_tt_is_backed_up(tt))) 1041 return -EINVAL; 1042 1043 if ((!ttm_backup_bytes_avail() && !flags->purge) || 1044 ttm_pool_uses_dma_alloc(pool) || ttm_tt_is_backed_up(tt)) 1045 return -EBUSY; 1046 1047 #ifdef CONFIG_X86 1048 /* Anything returned to the system needs to be cached. */ 1049 if (tt->caching != ttm_cached) 1050 set_pages_array_wb(tt->pages, tt->num_pages); 1051 #endif 1052 1053 if (tt->dma_address || flags->purge) { 1054 for (i = 0; i < tt->num_pages; i += num_pages) { 1055 unsigned int order; 1056 1057 page = tt->pages[i]; 1058 if (unlikely(!page)) { 1059 num_pages = 1; 1060 continue; 1061 } 1062 1063 order = ttm_pool_page_order(pool, page); 1064 num_pages = 1UL << order; 1065 if (tt->dma_address) 1066 ttm_pool_unmap(pool, tt->dma_address[i], 1067 num_pages); 1068 if (flags->purge) { 1069 shrunken += num_pages; 1070 page->private = 0; 1071 __free_pages(page, order); 1072 memset(tt->pages + i, 0, 1073 num_pages * sizeof(*tt->pages)); 1074 } 1075 } 1076 } 1077 1078 if (flags->purge) 1079 return shrunken; 1080 1081 if (ttm_pool_uses_dma32(pool)) 1082 gfp = GFP_DMA32; 1083 else 1084 gfp = GFP_HIGHUSER; 1085 1086 alloc_gfp = GFP_KERNEL | __GFP_HIGH | __GFP_NOWARN | __GFP_RETRY_MAYFAIL; 1087 1088 num_pages = tt->num_pages; 1089 1090 /* Pretend doing fault injection by shrinking only half of the pages. */ 1091 if (IS_ENABLED(CONFIG_FAULT_INJECTION) && should_fail(&backup_fault_inject, 1)) 1092 num_pages = DIV_ROUND_UP(num_pages, 2); 1093 1094 for (i = 0; i < num_pages; ++i) { 1095 s64 shandle; 1096 1097 page = tt->pages[i]; 1098 if (unlikely(!page)) 1099 continue; 1100 1101 ttm_pool_split_for_swap(pool, page); 1102 1103 shandle = ttm_backup_backup_page(backup, page, flags->writeback, i, 1104 gfp, alloc_gfp); 1105 if (shandle < 0) { 1106 /* We allow partially shrunken tts */ 1107 ret = shandle; 1108 break; 1109 } 1110 handle = shandle; 1111 tt->pages[i] = ttm_backup_handle_to_page_ptr(handle); 1112 put_page(page); 1113 shrunken++; 1114 } 1115 1116 return shrunken ? shrunken : ret; 1117 } 1118 1119 /** 1120 * ttm_pool_init - Initialize a pool 1121 * 1122 * @pool: the pool to initialize 1123 * @dev: device for DMA allocations and mappings 1124 * @nid: NUMA node to use for allocations 1125 * @alloc_flags: TTM_ALLOCATION_POOL_* flags 1126 * 1127 * Initialize the pool and its pool types. 1128 */ 1129 void ttm_pool_init(struct ttm_pool *pool, struct device *dev, 1130 int nid, unsigned int alloc_flags) 1131 { 1132 unsigned int i, j; 1133 1134 WARN_ON(!dev && ttm_pool_uses_dma_alloc(pool)); 1135 1136 pool->dev = dev; 1137 pool->nid = nid; 1138 pool->alloc_flags = alloc_flags; 1139 1140 for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i) { 1141 for (j = 0; j < NR_PAGE_ORDERS; ++j) { 1142 struct ttm_pool_type *pt; 1143 1144 /* Initialize only pool types which are actually used */ 1145 pt = ttm_pool_select_type(pool, i, j); 1146 if (pt != &pool->caching[i].orders[j]) 1147 continue; 1148 1149 ttm_pool_type_init(pt, pool, i, j); 1150 } 1151 } 1152 } 1153 EXPORT_SYMBOL(ttm_pool_init); 1154 1155 /** 1156 * ttm_pool_synchronize_shrinkers - Wait for all running shrinkers to complete. 1157 * 1158 * This is useful to guarantee that all shrinker invocations have seen an 1159 * update, before freeing memory, similar to rcu. 1160 */ 1161 static void ttm_pool_synchronize_shrinkers(void) 1162 { 1163 down_write(&pool_shrink_rwsem); 1164 up_write(&pool_shrink_rwsem); 1165 } 1166 1167 /** 1168 * ttm_pool_fini - Cleanup a pool 1169 * 1170 * @pool: the pool to clean up 1171 * 1172 * Free all pages in the pool and unregister the types from the global 1173 * shrinker. 1174 */ 1175 void ttm_pool_fini(struct ttm_pool *pool) 1176 { 1177 unsigned int i, j; 1178 1179 for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i) { 1180 for (j = 0; j < NR_PAGE_ORDERS; ++j) { 1181 struct ttm_pool_type *pt; 1182 1183 pt = ttm_pool_select_type(pool, i, j); 1184 if (pt != &pool->caching[i].orders[j]) 1185 continue; 1186 1187 ttm_pool_type_fini(pt); 1188 } 1189 } 1190 1191 /* We removed the pool types from the LRU, but we need to also make sure 1192 * that no shrinker is concurrently freeing pages from the pool. 1193 */ 1194 ttm_pool_synchronize_shrinkers(); 1195 } 1196 EXPORT_SYMBOL(ttm_pool_fini); 1197 1198 /* Free average pool number of pages. */ 1199 #define TTM_SHRINKER_BATCH ((1 << (MAX_PAGE_ORDER / 2)) * NR_PAGE_ORDERS) 1200 1201 static unsigned long ttm_pool_shrinker_scan(struct shrinker *shrink, 1202 struct shrink_control *sc) 1203 { 1204 unsigned long num_freed = 0; 1205 1206 do 1207 num_freed += ttm_pool_shrink(sc->nid, sc->nr_to_scan); 1208 while (num_freed < sc->nr_to_scan && 1209 atomic_long_read(&allocated_pages[sc->nid])); 1210 1211 sc->nr_scanned = num_freed; 1212 1213 return num_freed ?: SHRINK_STOP; 1214 } 1215 1216 /* Return the number of pages available or SHRINK_EMPTY if we have none */ 1217 static unsigned long ttm_pool_shrinker_count(struct shrinker *shrink, 1218 struct shrink_control *sc) 1219 { 1220 unsigned long num_pages = atomic_long_read(&allocated_pages[sc->nid]); 1221 1222 return num_pages ? num_pages : SHRINK_EMPTY; 1223 } 1224 1225 #ifdef CONFIG_DEBUG_FS 1226 /* Count the number of pages available in a pool_type */ 1227 static unsigned int ttm_pool_type_count(struct ttm_pool_type *pt) 1228 { 1229 return list_lru_count(&pt->pages); 1230 } 1231 1232 /* Print a nice header for the order */ 1233 static void ttm_pool_debugfs_header(struct seq_file *m) 1234 { 1235 unsigned int i; 1236 1237 seq_puts(m, "\t "); 1238 for (i = 0; i < NR_PAGE_ORDERS; ++i) 1239 seq_printf(m, " ---%2u---", i); 1240 seq_puts(m, "\n"); 1241 } 1242 1243 /* Dump information about the different pool types */ 1244 static void ttm_pool_debugfs_orders(struct ttm_pool_type *pt, 1245 struct seq_file *m) 1246 { 1247 unsigned int i; 1248 1249 for (i = 0; i < NR_PAGE_ORDERS; ++i) 1250 seq_printf(m, " %8u", ttm_pool_type_count(&pt[i])); 1251 seq_puts(m, "\n"); 1252 } 1253 1254 /* Dump the total amount of allocated pages */ 1255 static void ttm_pool_debugfs_footer(struct seq_file *m) 1256 { 1257 int nid; 1258 1259 for_each_node(nid) { 1260 seq_printf(m, "\ntotal node%d\t: %8lu of %8lu\n", nid, 1261 atomic_long_read(&allocated_pages[nid]), pool_node_limit[nid]); 1262 } 1263 } 1264 1265 /* Dump the information for the global pools */ 1266 static int ttm_pool_debugfs_globals_show(struct seq_file *m, void *data) 1267 { 1268 ttm_pool_debugfs_header(m); 1269 1270 spin_lock(&shrinker_lock); 1271 seq_puts(m, "wc\t:"); 1272 ttm_pool_debugfs_orders(global_write_combined, m); 1273 seq_puts(m, "uc\t:"); 1274 ttm_pool_debugfs_orders(global_uncached, m); 1275 seq_puts(m, "wc 32\t:"); 1276 ttm_pool_debugfs_orders(global_dma32_write_combined, m); 1277 seq_puts(m, "uc 32\t:"); 1278 ttm_pool_debugfs_orders(global_dma32_uncached, m); 1279 spin_unlock(&shrinker_lock); 1280 1281 ttm_pool_debugfs_footer(m); 1282 1283 return 0; 1284 } 1285 DEFINE_SHOW_ATTRIBUTE(ttm_pool_debugfs_globals); 1286 1287 /** 1288 * ttm_pool_debugfs - Debugfs dump function for a pool 1289 * 1290 * @pool: the pool to dump the information for 1291 * @m: seq_file to dump to 1292 * 1293 * Make a debugfs dump with the per pool and global information. 1294 */ 1295 int ttm_pool_debugfs(struct ttm_pool *pool, struct seq_file *m) 1296 { 1297 unsigned int i; 1298 1299 if (!ttm_pool_uses_dma_alloc(pool)) { 1300 seq_puts(m, "unused\n"); 1301 return 0; 1302 } 1303 1304 ttm_pool_debugfs_header(m); 1305 1306 spin_lock(&shrinker_lock); 1307 for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i) { 1308 if (!ttm_pool_select_type(pool, i, 0)) 1309 continue; 1310 seq_puts(m, "DMA "); 1311 switch (i) { 1312 case ttm_cached: 1313 seq_puts(m, "\t:"); 1314 break; 1315 case ttm_write_combined: 1316 seq_puts(m, "wc\t:"); 1317 break; 1318 case ttm_uncached: 1319 seq_puts(m, "uc\t:"); 1320 break; 1321 } 1322 ttm_pool_debugfs_orders(pool->caching[i].orders, m); 1323 } 1324 spin_unlock(&shrinker_lock); 1325 1326 ttm_pool_debugfs_footer(m); 1327 return 0; 1328 } 1329 EXPORT_SYMBOL(ttm_pool_debugfs); 1330 1331 /* Test the shrinker functions and dump the result */ 1332 static int ttm_pool_debugfs_shrink_show(struct seq_file *m, void *data) 1333 { 1334 struct shrink_control sc = { 1335 .gfp_mask = GFP_NOFS, 1336 .nr_to_scan = TTM_SHRINKER_BATCH, 1337 }; 1338 unsigned long count; 1339 int nid; 1340 1341 fs_reclaim_acquire(GFP_KERNEL); 1342 for_each_node(nid) { 1343 sc.nid = nid; 1344 count = ttm_pool_shrinker_count(mm_shrinker, &sc); 1345 seq_printf(m, "%d: %lu/%lu\n", nid, count, 1346 ttm_pool_shrinker_scan(mm_shrinker, &sc)); 1347 } 1348 fs_reclaim_release(GFP_KERNEL); 1349 1350 return 0; 1351 } 1352 DEFINE_SHOW_ATTRIBUTE(ttm_pool_debugfs_shrink); 1353 1354 #endif 1355 1356 static inline u64 ttm_get_node_memory_size(int nid) 1357 { 1358 /* 1359 * This is directly using si_meminfo_node implementation as the 1360 * function is not exported. 1361 */ 1362 int zone_type; 1363 u64 managed_pages = 0; 1364 1365 pg_data_t *pgdat = NODE_DATA(nid); 1366 1367 for (zone_type = 0; zone_type < MAX_NR_ZONES; zone_type++) 1368 managed_pages += 1369 zone_managed_pages(&pgdat->node_zones[zone_type]); 1370 return managed_pages * PAGE_SIZE; 1371 } 1372 1373 /** 1374 * ttm_pool_mgr_init - Initialize globals 1375 * 1376 * @num_pages: default number of pages 1377 * 1378 * Initialize the global locks and lists for the MM shrinker. 1379 */ 1380 int ttm_pool_mgr_init(unsigned long num_pages) 1381 { 1382 unsigned int i; 1383 1384 int nid; 1385 for_each_node(nid) { 1386 if (!page_pool_size) { 1387 u64 node_size = ttm_get_node_memory_size(nid); 1388 pool_node_limit[nid] = (node_size >> PAGE_SHIFT) / 2; 1389 } else { 1390 pool_node_limit[nid] = page_pool_size; 1391 } 1392 } 1393 1394 spin_lock_init(&shrinker_lock); 1395 INIT_LIST_HEAD(&shrinker_list); 1396 1397 for (i = 0; i < NR_PAGE_ORDERS; ++i) { 1398 ttm_pool_type_init(&global_write_combined[i], NULL, 1399 ttm_write_combined, i); 1400 ttm_pool_type_init(&global_uncached[i], NULL, ttm_uncached, i); 1401 1402 ttm_pool_type_init(&global_dma32_write_combined[i], NULL, 1403 ttm_write_combined, i); 1404 ttm_pool_type_init(&global_dma32_uncached[i], NULL, 1405 ttm_uncached, i); 1406 } 1407 1408 #ifdef CONFIG_DEBUG_FS 1409 debugfs_create_file("page_pool", 0444, ttm_debugfs_root, NULL, 1410 &ttm_pool_debugfs_globals_fops); 1411 debugfs_create_file("page_pool_shrink", 0400, ttm_debugfs_root, NULL, 1412 &ttm_pool_debugfs_shrink_fops); 1413 #ifdef CONFIG_FAULT_INJECTION 1414 fault_create_debugfs_attr("backup_fault_inject", ttm_debugfs_root, 1415 &backup_fault_inject); 1416 #endif 1417 #endif 1418 1419 mm_shrinker = shrinker_alloc(SHRINKER_NUMA_AWARE, "drm-ttm_pool"); 1420 if (!mm_shrinker) 1421 return -ENOMEM; 1422 1423 mm_shrinker->count_objects = ttm_pool_shrinker_count; 1424 mm_shrinker->scan_objects = ttm_pool_shrinker_scan; 1425 mm_shrinker->batch = TTM_SHRINKER_BATCH; 1426 mm_shrinker->seeks = 1; 1427 1428 shrinker_register(mm_shrinker); 1429 1430 return 0; 1431 } 1432 1433 /** 1434 * ttm_pool_mgr_fini - Finalize globals 1435 * 1436 * Cleanup the global pools and unregister the MM shrinker. 1437 */ 1438 void ttm_pool_mgr_fini(void) 1439 { 1440 unsigned int i; 1441 1442 for (i = 0; i < NR_PAGE_ORDERS; ++i) { 1443 ttm_pool_type_fini(&global_write_combined[i]); 1444 ttm_pool_type_fini(&global_uncached[i]); 1445 1446 ttm_pool_type_fini(&global_dma32_write_combined[i]); 1447 ttm_pool_type_fini(&global_dma32_uncached[i]); 1448 } 1449 1450 shrinker_free(mm_shrinker); 1451 WARN_ON(!list_empty(&shrinker_list)); 1452 } 1453