Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

drm/ttm: Tidy usage of local variables a little bit

At the moment the TTM code has a few places which exibit sub-optimal
patterns regarding local variable usage:

* Having a local with some object cached but not always using it.
* Having a local for a single use object member access.
* Failed opportunities to use a local to cache a pointer.

Lets tidy this a little bit and apply some more consistency.

It is mostly for consistency and redability but I have also checked that
there are not negative code generation effects. In fact there are more
positives:

add/remove: 0/0 grow/shrink: 3/9 up/down: 12/-175 (-163)
Function old new delta
ttm_pool_restore_and_alloc 415 423 +8
ttm_bo_vunmap 147 149 +2
ttm_bo_evict 521 523 +2
ttm_bo_vm_fault_reserved 972 970 -2
ttm_bo_vm_dummy_page 155 152 -3
ttm_bo_vm_fault 203 196 -7
ttm_bo_populate 158 150 -8
ttm_bo_move_memcpy 600 592 -8
ttm_bo_kmap 667 644 -23
ttm_bo_shrink 333 305 -28
ttm_bo_release 750 720 -30
ttm_bo_swapout_cb 691 625 -66
Total: Before=42717, After=42554, chg -0.38%

Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@igalia.com>
Reviewed-by: Thadeu Lima de Souza Cascardo <cascardo@igalia.com>
Link: https://lore.kernel.org/r/20250919131530.91247-5-tvrtko.ursulin@igalia.com
Acked-by: Christian König <christian.koenig@amd.com>
Signed-off-by: Tvrtko Ursulin <tursulin@ursulin.net>
[tursulin: fixup conflict in ttm_bo_move_pipeline_evict]

authored by

Tvrtko Ursulin and committed by
Tvrtko Ursulin
c06da4b3 802620f5

+77 -76
+32 -30
drivers/gpu/drm/ttm/ttm_bo.c
··· 268 268 30 * HZ); 269 269 } 270 270 271 - if (bo->bdev->funcs->release_notify) 272 - bo->bdev->funcs->release_notify(bo); 271 + if (bdev->funcs->release_notify) 272 + bdev->funcs->release_notify(bo); 273 273 274 274 drm_vma_offset_remove(bdev->vma_manager, &bo->base.vma_node); 275 275 ttm_mem_io_free(bdev, bo->resource); ··· 283 283 ttm_bo_flush_all_fences(bo); 284 284 bo->deleted = true; 285 285 286 - spin_lock(&bo->bdev->lru_lock); 286 + spin_lock(&bdev->lru_lock); 287 287 288 288 /* 289 289 * Make pinned bos immediately available to ··· 299 299 } 300 300 301 301 kref_init(&bo->kref); 302 - spin_unlock(&bo->bdev->lru_lock); 302 + spin_unlock(&bdev->lru_lock); 303 303 304 304 INIT_WORK(&bo->delayed_delete, ttm_bo_delayed_delete); 305 305 ··· 359 359 static int ttm_bo_evict(struct ttm_buffer_object *bo, 360 360 struct ttm_operation_ctx *ctx) 361 361 { 362 - struct ttm_device *bdev = bo->bdev; 363 362 struct ttm_resource *evict_mem; 364 363 struct ttm_placement placement; 365 364 struct ttm_place hop; ··· 369 370 dma_resv_assert_held(bo->base.resv); 370 371 371 372 placement.num_placement = 0; 372 - bdev->funcs->evict_flags(bo, &placement); 373 + bo->bdev->funcs->evict_flags(bo, &placement); 373 374 374 375 if (!placement.num_placement) { 375 376 ret = ttm_bo_wait_ctx(bo, ctx); ··· 422 423 const struct ttm_place *place) 423 424 { 424 425 struct ttm_resource *res = bo->resource; 425 - struct ttm_device *bdev = bo->bdev; 426 426 427 427 dma_resv_assert_held(bo->base.resv); 428 - if (bo->resource->mem_type == TTM_PL_SYSTEM) 428 + 429 + if (res->mem_type == TTM_PL_SYSTEM) 429 430 return true; 430 431 431 432 /* Don't evict this BO if it's outside of the 432 433 * requested placement range 433 434 */ 434 - return ttm_resource_intersects(bdev, res, place, bo->base.size); 435 + return ttm_resource_intersects(bo->bdev, res, place, bo->base.size); 435 436 } 436 437 EXPORT_SYMBOL(ttm_bo_eviction_valuable); 437 438 ··· 1107 1108 static s64 1108 1109 ttm_bo_swapout_cb(struct ttm_lru_walk *walk, struct ttm_buffer_object *bo) 1109 1110 { 1110 - struct ttm_place place = {.mem_type = bo->resource->mem_type}; 1111 + struct ttm_resource *res = bo->resource; 1112 + struct ttm_place place = { .mem_type = res->mem_type }; 1111 1113 struct ttm_bo_swapout_walk *swapout_walk = 1112 1114 container_of(walk, typeof(*swapout_walk), walk); 1113 1115 struct ttm_operation_ctx *ctx = walk->arg.ctx; 1116 + struct ttm_device *bdev = bo->bdev; 1117 + struct ttm_tt *tt = bo->ttm; 1114 1118 s64 ret; 1115 1119 1116 1120 /* ··· 1122 1120 * The driver may use the fact that we're moving from SYSTEM 1123 1121 * as an indication that we're about to swap out. 1124 1122 */ 1125 - if (bo->pin_count || !bo->bdev->funcs->eviction_valuable(bo, &place)) { 1123 + if (bo->pin_count || !bdev->funcs->eviction_valuable(bo, &place)) { 1126 1124 ret = -EBUSY; 1127 1125 goto out; 1128 1126 } 1129 1127 1130 - if (!bo->ttm || !ttm_tt_is_populated(bo->ttm) || 1131 - bo->ttm->page_flags & TTM_TT_FLAG_EXTERNAL || 1132 - bo->ttm->page_flags & TTM_TT_FLAG_SWAPPED) { 1128 + if (!tt || !ttm_tt_is_populated(tt) || 1129 + tt->page_flags & (TTM_TT_FLAG_EXTERNAL | TTM_TT_FLAG_SWAPPED)) { 1133 1130 ret = -EBUSY; 1134 1131 goto out; 1135 1132 } 1136 1133 1137 1134 if (bo->deleted) { 1138 - pgoff_t num_pages = bo->ttm->num_pages; 1135 + pgoff_t num_pages = tt->num_pages; 1139 1136 1140 1137 ret = ttm_bo_wait_ctx(bo, ctx); 1141 1138 if (ret) ··· 1148 1147 /* 1149 1148 * Move to system cached 1150 1149 */ 1151 - if (bo->resource->mem_type != TTM_PL_SYSTEM) { 1150 + if (res->mem_type != TTM_PL_SYSTEM) { 1152 1151 struct ttm_resource *evict_mem; 1153 1152 struct ttm_place hop; 1154 1153 ··· 1175 1174 goto out; 1176 1175 1177 1176 ttm_bo_unmap_virtual(bo); 1178 - if (bo->bdev->funcs->swap_notify) 1179 - bo->bdev->funcs->swap_notify(bo); 1177 + if (bdev->funcs->swap_notify) 1178 + bdev->funcs->swap_notify(bo); 1180 1179 1181 - if (ttm_tt_is_populated(bo->ttm)) { 1182 - spin_lock(&bo->bdev->lru_lock); 1183 - ttm_resource_del_bulk_move(bo->resource, bo); 1184 - spin_unlock(&bo->bdev->lru_lock); 1180 + if (ttm_tt_is_populated(tt)) { 1181 + spin_lock(&bdev->lru_lock); 1182 + ttm_resource_del_bulk_move(res, bo); 1183 + spin_unlock(&bdev->lru_lock); 1185 1184 1186 - ret = ttm_tt_swapout(bo->bdev, bo->ttm, swapout_walk->gfp_flags); 1185 + ret = ttm_tt_swapout(bdev, tt, swapout_walk->gfp_flags); 1187 1186 1188 - spin_lock(&bo->bdev->lru_lock); 1187 + spin_lock(&bdev->lru_lock); 1189 1188 if (ret) 1190 - ttm_resource_add_bulk_move(bo->resource, bo); 1191 - ttm_resource_move_to_lru_tail(bo->resource); 1192 - spin_unlock(&bo->bdev->lru_lock); 1189 + ttm_resource_add_bulk_move(res, bo); 1190 + ttm_resource_move_to_lru_tail(res); 1191 + spin_unlock(&bdev->lru_lock); 1193 1192 } 1194 1193 1195 1194 out: ··· 1262 1261 int ttm_bo_populate(struct ttm_buffer_object *bo, 1263 1262 struct ttm_operation_ctx *ctx) 1264 1263 { 1264 + struct ttm_device *bdev = bo->bdev; 1265 1265 struct ttm_tt *tt = bo->ttm; 1266 1266 bool swapped; 1267 1267 int ret; ··· 1273 1271 return 0; 1274 1272 1275 1273 swapped = ttm_tt_is_swapped(tt); 1276 - ret = ttm_tt_populate(bo->bdev, tt, ctx); 1274 + ret = ttm_tt_populate(bdev, tt, ctx); 1277 1275 if (ret) 1278 1276 return ret; 1279 1277 1280 1278 if (swapped && !ttm_tt_is_swapped(tt) && !bo->pin_count && 1281 1279 bo->resource) { 1282 - spin_lock(&bo->bdev->lru_lock); 1280 + spin_lock(&bdev->lru_lock); 1283 1281 ttm_resource_add_bulk_move(bo->resource, bo); 1284 1282 ttm_resource_move_to_lru_tail(bo->resource); 1285 - spin_unlock(&bo->bdev->lru_lock); 1283 + spin_unlock(&bdev->lru_lock); 1286 1284 } 1287 1285 1288 1286 return 0;
+23 -24
drivers/gpu/drm/ttm/ttm_bo_util.c
··· 174 174 175 175 dst_iter = ttm_kmap_iter_linear_io_init(&_dst_iter.io, bdev, dst_mem); 176 176 if (PTR_ERR(dst_iter) == -EINVAL && dst_man->use_tt) 177 - dst_iter = ttm_kmap_iter_tt_init(&_dst_iter.tt, bo->ttm); 177 + dst_iter = ttm_kmap_iter_tt_init(&_dst_iter.tt, ttm); 178 178 if (IS_ERR(dst_iter)) 179 179 return PTR_ERR(dst_iter); 180 180 181 181 src_iter = ttm_kmap_iter_linear_io_init(&_src_iter.io, bdev, src_mem); 182 182 if (PTR_ERR(src_iter) == -EINVAL && src_man->use_tt) 183 - src_iter = ttm_kmap_iter_tt_init(&_src_iter.tt, bo->ttm); 183 + src_iter = ttm_kmap_iter_tt_init(&_src_iter.tt, ttm); 184 184 if (IS_ERR(src_iter)) { 185 185 ret = PTR_ERR(src_iter); 186 186 goto out_src_iter; ··· 318 318 { 319 319 struct ttm_resource *mem = bo->resource; 320 320 321 - if (bo->resource->bus.addr) { 321 + if (mem->bus.addr) { 322 322 map->bo_kmap_type = ttm_bo_map_premapped; 323 - map->virtual = ((u8 *)bo->resource->bus.addr) + offset; 323 + map->virtual = ((u8 *)mem->bus.addr) + offset; 324 324 } else { 325 - resource_size_t res = bo->resource->bus.offset + offset; 325 + resource_size_t res = mem->bus.offset + offset; 326 326 327 327 map->bo_kmap_type = ttm_bo_map_iomap; 328 328 if (mem->bus.caching == ttm_write_combined) ··· 346 346 struct ttm_operation_ctx ctx = { }; 347 347 struct ttm_tt *ttm = bo->ttm; 348 348 struct ttm_resource_manager *man = 349 - ttm_manager_type(bo->bdev, bo->resource->mem_type); 349 + ttm_manager_type(bo->bdev, mem->mem_type); 350 350 pgprot_t prot; 351 351 int ret; 352 352 ··· 425 425 unsigned long start_page, unsigned long num_pages, 426 426 struct ttm_bo_kmap_obj *map) 427 427 { 428 + struct ttm_resource *res = bo->resource; 428 429 unsigned long offset, size; 429 430 int ret; 430 431 431 432 map->virtual = NULL; 432 433 map->bo = bo; 433 - if (num_pages > PFN_UP(bo->resource->size)) 434 + if (num_pages > PFN_UP(res->size)) 434 435 return -EINVAL; 435 - if ((start_page + num_pages) > PFN_UP(bo->resource->size)) 436 + if ((start_page + num_pages) > PFN_UP(res->size)) 436 437 return -EINVAL; 437 438 438 - ret = ttm_mem_io_reserve(bo->bdev, bo->resource); 439 + ret = ttm_mem_io_reserve(bo->bdev, res); 439 440 if (ret) 440 441 return ret; 441 - if (!bo->resource->bus.is_iomem) { 442 + if (!res->bus.is_iomem) { 442 443 return ttm_bo_kmap_ttm(bo, start_page, num_pages, map); 443 444 } else { 444 445 offset = start_page << PAGE_SHIFT; ··· 576 575 iounmap(map->vaddr_iomem); 577 576 iosys_map_clear(map); 578 577 579 - ttm_mem_io_free(bo->bdev, bo->resource); 578 + ttm_mem_io_free(bo->bdev, mem); 580 579 } 581 580 EXPORT_SYMBOL(ttm_bo_vunmap); 582 581 ··· 639 638 static void ttm_bo_move_pipeline_evict(struct ttm_buffer_object *bo, 640 639 struct dma_fence *fence) 641 640 { 642 - struct ttm_device *bdev = bo->bdev; 643 641 struct ttm_resource_manager *from; 644 642 struct dma_fence *tmp; 645 643 int i; 646 644 647 - from = ttm_manager_type(bdev, bo->resource->mem_type); 645 + from = ttm_manager_type(bo->bdev, bo->resource->mem_type); 648 646 649 647 /** 650 648 * BO doesn't have a TTM we need to bind/unbind. Just remember ··· 737 737 void ttm_bo_move_sync_cleanup(struct ttm_buffer_object *bo, 738 738 struct ttm_resource *new_mem) 739 739 { 740 - struct ttm_device *bdev = bo->bdev; 741 - struct ttm_resource_manager *man = ttm_manager_type(bdev, new_mem->mem_type); 740 + struct ttm_resource_manager *man = 741 + ttm_manager_type(bo->bdev, new_mem->mem_type); 742 742 int ret; 743 743 744 744 ret = ttm_bo_wait_free_node(bo, man->use_tt); ··· 842 842 struct ttm_buffer_object *bo) 843 843 { 844 844 struct ttm_lru_walk_arg *arg = curs->arg; 845 - struct dma_resv *resv = bo->base.resv; 846 845 int ret; 847 846 848 847 if (arg->ctx->interruptible) 849 - ret = dma_resv_lock_interruptible(resv, arg->ticket); 848 + ret = dma_resv_lock_interruptible(bo->base.resv, arg->ticket); 850 849 else 851 - ret = dma_resv_lock(resv, arg->ticket); 850 + ret = dma_resv_lock(bo->base.resv, arg->ticket); 852 851 853 852 if (!ret) { 854 853 curs->needs_unlock = true; ··· 1091 1092 .num_placement = 1, 1092 1093 .placement = &sys_placement_flags, 1093 1094 }; 1094 - struct ttm_tt *tt = bo->ttm; 1095 + struct ttm_device *bdev = bo->bdev; 1095 1096 long lret; 1096 1097 1097 1098 dma_resv_assert_held(bo->base.resv); ··· 1113 1114 return lret; 1114 1115 1115 1116 if (bo->bulk_move) { 1116 - spin_lock(&bo->bdev->lru_lock); 1117 + spin_lock(&bdev->lru_lock); 1117 1118 ttm_resource_del_bulk_move(bo->resource, bo); 1118 - spin_unlock(&bo->bdev->lru_lock); 1119 + spin_unlock(&bdev->lru_lock); 1119 1120 } 1120 1121 1121 - lret = ttm_tt_backup(bo->bdev, tt, (struct ttm_backup_flags) 1122 + lret = ttm_tt_backup(bdev, bo->ttm, (struct ttm_backup_flags) 1122 1123 {.purge = flags.purge, 1123 1124 .writeback = flags.writeback}); 1124 1125 1125 1126 if (lret <= 0 && bo->bulk_move) { 1126 - spin_lock(&bo->bdev->lru_lock); 1127 + spin_lock(&bdev->lru_lock); 1127 1128 ttm_resource_add_bulk_move(bo->resource, bo); 1128 - spin_unlock(&bo->bdev->lru_lock); 1129 + spin_unlock(&bdev->lru_lock); 1129 1130 } 1130 1131 1131 1132 if (lret < 0 && lret != -EINTR)
+5 -7
drivers/gpu/drm/ttm/ttm_bo_vm.c
··· 186 186 { 187 187 struct vm_area_struct *vma = vmf->vma; 188 188 struct ttm_buffer_object *bo = vma->vm_private_data; 189 - struct ttm_device *bdev = bo->bdev; 190 189 unsigned long page_offset; 191 190 unsigned long page_last; 192 191 unsigned long pfn; ··· 204 205 if (unlikely(ret != 0)) 205 206 return ret; 206 207 207 - err = ttm_mem_io_reserve(bdev, bo->resource); 208 + err = ttm_mem_io_reserve(bo->bdev, bo->resource); 208 209 if (unlikely(err != 0)) 209 210 return VM_FAULT_SIGBUS; 210 211 ··· 292 293 { 293 294 struct vm_area_struct *vma = vmf->vma; 294 295 struct ttm_buffer_object *bo = vma->vm_private_data; 295 - struct drm_device *ddev = bo->base.dev; 296 296 vm_fault_t ret = VM_FAULT_NOPAGE; 297 297 unsigned long address; 298 298 unsigned long pfn; ··· 303 305 return VM_FAULT_OOM; 304 306 305 307 /* Set the page to be freed using drmm release action */ 306 - if (drmm_add_action_or_reset(ddev, ttm_bo_release_dummy_page, page)) 308 + if (drmm_add_action_or_reset(bo->base.dev, ttm_bo_release_dummy_page, 309 + page)) 307 310 return VM_FAULT_OOM; 308 311 309 312 pfn = page_to_pfn(page); ··· 321 322 vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf) 322 323 { 323 324 struct vm_area_struct *vma = vmf->vma; 324 - pgprot_t prot; 325 325 struct ttm_buffer_object *bo = vma->vm_private_data; 326 - struct drm_device *ddev = bo->base.dev; 327 326 vm_fault_t ret; 327 + pgprot_t prot; 328 328 int idx; 329 329 330 330 ret = ttm_bo_vm_reserve(bo, vmf); ··· 331 333 return ret; 332 334 333 335 prot = vma->vm_page_prot; 334 - if (drm_dev_enter(ddev, &idx)) { 336 + if (drm_dev_enter(bo->base.dev, &idx)) { 335 337 ret = ttm_bo_vm_fault_reserved(vmf, prot, TTM_BO_VM_NUM_PREFAULT); 336 338 drm_dev_exit(idx); 337 339 } else {
+14 -12
drivers/gpu/drm/ttm/ttm_pool.c
··· 845 845 int ttm_pool_restore_and_alloc(struct ttm_pool *pool, struct ttm_tt *tt, 846 846 const struct ttm_operation_ctx *ctx) 847 847 { 848 + struct ttm_pool_tt_restore *restore = tt->restore; 848 849 struct ttm_pool_alloc_state alloc; 849 850 850 851 if (WARN_ON(!ttm_tt_is_backed_up(tt))) 851 852 return -EINVAL; 852 853 853 - if (!tt->restore) { 854 + if (!restore) { 854 855 gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; 855 856 856 857 ttm_pool_alloc_state_init(tt, &alloc); 857 858 if (ctx->gfp_retry_mayfail) 858 859 gfp |= __GFP_RETRY_MAYFAIL; 859 860 860 - tt->restore = kzalloc(sizeof(*tt->restore), gfp); 861 - if (!tt->restore) 861 + restore = kzalloc(sizeof(*restore), gfp); 862 + if (!restore) 862 863 return -ENOMEM; 863 864 864 - tt->restore->snapshot_alloc = alloc; 865 - tt->restore->pool = pool; 866 - tt->restore->restored_pages = 1; 867 - } else { 868 - struct ttm_pool_tt_restore *restore = tt->restore; 869 - int ret; 865 + restore->snapshot_alloc = alloc; 866 + restore->pool = pool; 867 + restore->restored_pages = 1; 870 868 869 + tt->restore = restore; 870 + } else { 871 871 alloc = restore->snapshot_alloc; 872 - if (ttm_pool_restore_valid(tt->restore)) { 873 - ret = ttm_pool_restore_commit(restore, tt->backup, ctx, &alloc); 872 + if (ttm_pool_restore_valid(restore)) { 873 + int ret = ttm_pool_restore_commit(restore, tt->backup, 874 + ctx, &alloc); 875 + 874 876 if (ret) 875 877 return ret; 876 878 } ··· 880 878 return 0; 881 879 } 882 880 883 - return __ttm_pool_alloc(pool, tt, ctx, &alloc, tt->restore); 881 + return __ttm_pool_alloc(pool, tt, ctx, &alloc, restore); 884 882 } 885 883 886 884 /**
+3 -3
drivers/gpu/drm/ttm/ttm_resource.c
··· 622 622 struct ttm_lru_item *next_lru) 623 623 { 624 624 struct ttm_resource *next = ttm_lru_item_to_res(next_lru); 625 - struct ttm_lru_bulk_move *bulk = NULL; 626 - struct ttm_buffer_object *bo = next->bo; 625 + struct ttm_lru_bulk_move *bulk; 627 626 628 627 lockdep_assert_held(&cursor->man->bdev->lru_lock); 629 - bulk = bo->bulk_move; 628 + 629 + bulk = next->bo->bulk_move; 630 630 631 631 if (cursor->bulk != bulk) { 632 632 if (bulk) {