1 /* 2 * Copyright © 2008 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * 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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 * 23 * Authors: 24 * Eric Anholt <eric@anholt.net> 25 * 26 */ 27 28 #include <linux/dma-buf.h> 29 #include <linux/export.h> 30 #include <linux/file.h> 31 #include <linux/fs.h> 32 #include <linux/iosys-map.h> 33 #include <linux/mem_encrypt.h> 34 #include <linux/mm.h> 35 #include <linux/mman.h> 36 #include <linux/module.h> 37 #include <linux/pagemap.h> 38 #include <linux/pagevec.h> 39 #include <linux/shmem_fs.h> 40 #include <linux/slab.h> 41 #include <linux/string_helpers.h> 42 #include <linux/types.h> 43 #include <linux/uaccess.h> 44 45 #include <drm/drm.h> 46 #include <drm/drm_device.h> 47 #include <drm/drm_drv.h> 48 #include <drm/drm_file.h> 49 #include <drm/drm_gem.h> 50 #include <drm/drm_managed.h> 51 #include <drm/drm_print.h> 52 #include <drm/drm_vma_manager.h> 53 54 #include "drm_internal.h" 55 56 /** @file drm_gem.c 57 * 58 * This file provides some of the base ioctls and library routines for 59 * the graphics memory manager implemented by each device driver. 60 * 61 * Because various devices have different requirements in terms of 62 * synchronization and migration strategies, implementing that is left up to 63 * the driver, and all that the general API provides should be generic -- 64 * allocating objects, reading/writing data with the cpu, freeing objects. 65 * Even there, platform-dependent optimizations for reading/writing data with 66 * the CPU mean we'll likely hook those out to driver-specific calls. However, 67 * the DRI2 implementation wants to have at least allocate/mmap be generic. 68 * 69 * The goal was to have swap-backed object allocation managed through 70 * struct file. However, file descriptors as handles to a struct file have 71 * two major failings: 72 * - Process limits prevent more than 1024 or so being used at a time by 73 * default. 74 * - Inability to allocate high fds will aggravate the X Server's select() 75 * handling, and likely that of many GL client applications as well. 76 * 77 * This led to a plan of using our own integer IDs (called handles, following 78 * DRM terminology) to mimic fds, and implement the fd syscalls we need as 79 * ioctls. The objects themselves will still include the struct file so 80 * that we can transition to fds if the required kernel infrastructure shows 81 * up at a later date, and as our interface with shmfs for memory allocation. 82 */ 83 84 static void 85 drm_gem_init_release(struct drm_device *dev, void *ptr) 86 { 87 drm_vma_offset_manager_destroy(dev->vma_offset_manager); 88 } 89 90 /** 91 * drm_gem_init - Initialize the GEM device fields 92 * @dev: drm_devic structure to initialize 93 */ 94 int 95 drm_gem_init(struct drm_device *dev) 96 { 97 struct drm_vma_offset_manager *vma_offset_manager; 98 99 mutex_init(&dev->object_name_lock); 100 idr_init_base(&dev->object_name_idr, 1); 101 102 vma_offset_manager = drmm_kzalloc(dev, sizeof(*vma_offset_manager), 103 GFP_KERNEL); 104 if (!vma_offset_manager) { 105 DRM_ERROR("out of memory\n"); 106 return -ENOMEM; 107 } 108 109 dev->vma_offset_manager = vma_offset_manager; 110 drm_vma_offset_manager_init(vma_offset_manager, 111 DRM_FILE_PAGE_OFFSET_START, 112 DRM_FILE_PAGE_OFFSET_SIZE); 113 114 return drmm_add_action(dev, drm_gem_init_release, NULL); 115 } 116 117 /** 118 * drm_gem_object_init_with_mnt - initialize an allocated shmem-backed GEM 119 * object in a given shmfs mountpoint 120 * 121 * @dev: drm_device the object should be initialized for 122 * @obj: drm_gem_object to initialize 123 * @size: object size 124 * @gemfs: tmpfs mount where the GEM object will be created. If NULL, use 125 * the usual tmpfs mountpoint (`shm_mnt`). 126 * 127 * Initialize an already allocated GEM object of the specified size with 128 * shmfs backing store. 129 */ 130 int drm_gem_object_init_with_mnt(struct drm_device *dev, 131 struct drm_gem_object *obj, size_t size, 132 struct vfsmount *gemfs) 133 { 134 struct file *filp; 135 136 drm_gem_private_object_init(dev, obj, size); 137 138 if (gemfs) 139 filp = shmem_file_setup_with_mnt(gemfs, "drm mm object", size, 140 VM_NORESERVE); 141 else 142 filp = shmem_file_setup("drm mm object", size, VM_NORESERVE); 143 144 if (IS_ERR(filp)) 145 return PTR_ERR(filp); 146 147 obj->filp = filp; 148 149 return 0; 150 } 151 EXPORT_SYMBOL(drm_gem_object_init_with_mnt); 152 153 /** 154 * drm_gem_object_init - initialize an allocated shmem-backed GEM object 155 * @dev: drm_device the object should be initialized for 156 * @obj: drm_gem_object to initialize 157 * @size: object size 158 * 159 * Initialize an already allocated GEM object of the specified size with 160 * shmfs backing store. 161 */ 162 int drm_gem_object_init(struct drm_device *dev, struct drm_gem_object *obj, 163 size_t size) 164 { 165 return drm_gem_object_init_with_mnt(dev, obj, size, NULL); 166 } 167 EXPORT_SYMBOL(drm_gem_object_init); 168 169 /** 170 * drm_gem_private_object_init - initialize an allocated private GEM object 171 * @dev: drm_device the object should be initialized for 172 * @obj: drm_gem_object to initialize 173 * @size: object size 174 * 175 * Initialize an already allocated GEM object of the specified size with 176 * no GEM provided backing store. Instead the caller is responsible for 177 * backing the object and handling it. 178 */ 179 void drm_gem_private_object_init(struct drm_device *dev, 180 struct drm_gem_object *obj, size_t size) 181 { 182 BUG_ON((size & (PAGE_SIZE - 1)) != 0); 183 184 obj->dev = dev; 185 obj->filp = NULL; 186 187 kref_init(&obj->refcount); 188 obj->handle_count = 0; 189 obj->size = size; 190 dma_resv_init(&obj->_resv); 191 if (!obj->resv) 192 obj->resv = &obj->_resv; 193 194 if (drm_core_check_feature(dev, DRIVER_GEM_GPUVA)) 195 drm_gem_gpuva_init(obj); 196 197 drm_vma_node_reset(&obj->vma_node); 198 INIT_LIST_HEAD(&obj->lru_node); 199 } 200 EXPORT_SYMBOL(drm_gem_private_object_init); 201 202 /** 203 * drm_gem_private_object_fini - Finalize a failed drm_gem_object 204 * @obj: drm_gem_object 205 * 206 * Uninitialize an already allocated GEM object when it initialized failed 207 */ 208 void drm_gem_private_object_fini(struct drm_gem_object *obj) 209 { 210 WARN_ON(obj->dma_buf); 211 212 dma_resv_fini(&obj->_resv); 213 } 214 EXPORT_SYMBOL(drm_gem_private_object_fini); 215 216 static void drm_gem_object_handle_get(struct drm_gem_object *obj) 217 { 218 struct drm_device *dev = obj->dev; 219 220 drm_WARN_ON(dev, !mutex_is_locked(&dev->object_name_lock)); 221 222 if (obj->handle_count++ == 0) 223 drm_gem_object_get(obj); 224 } 225 226 /** 227 * drm_gem_object_handle_get_if_exists_unlocked - acquire reference on user-space handle, if any 228 * @obj: GEM object 229 * 230 * Acquires a reference on the GEM buffer object's handle. Required to keep 231 * the GEM object alive. Call drm_gem_object_handle_put_if_exists_unlocked() 232 * to release the reference. Does nothing if the buffer object has no handle. 233 * 234 * Returns: 235 * True if a handle exists, or false otherwise 236 */ 237 bool drm_gem_object_handle_get_if_exists_unlocked(struct drm_gem_object *obj) 238 { 239 struct drm_device *dev = obj->dev; 240 241 guard(mutex)(&dev->object_name_lock); 242 243 /* 244 * First ref taken during GEM object creation, if any. Some 245 * drivers set up internal framebuffers with GEM objects that 246 * do not have a GEM handle. Hence, this counter can be zero. 247 */ 248 if (!obj->handle_count) 249 return false; 250 251 drm_gem_object_handle_get(obj); 252 253 return true; 254 } 255 256 /** 257 * drm_gem_object_handle_free - release resources bound to userspace handles 258 * @obj: GEM object to clean up. 259 * 260 * Called after the last handle to the object has been closed 261 * 262 * Removes any name for the object. Note that this must be 263 * called before drm_gem_object_free or we'll be touching 264 * freed memory 265 */ 266 static void drm_gem_object_handle_free(struct drm_gem_object *obj) 267 { 268 struct drm_device *dev = obj->dev; 269 270 /* Remove any name for this object */ 271 if (obj->name) { 272 idr_remove(&dev->object_name_idr, obj->name); 273 obj->name = 0; 274 } 275 } 276 277 static void drm_gem_object_exported_dma_buf_free(struct drm_gem_object *obj) 278 { 279 /* Unbreak the reference cycle if we have an exported dma_buf. */ 280 if (obj->dma_buf) { 281 dma_buf_put(obj->dma_buf); 282 obj->dma_buf = NULL; 283 } 284 } 285 286 /** 287 * drm_gem_object_handle_put_unlocked - releases reference on user-space handle 288 * @obj: GEM object 289 * 290 * Releases a reference on the GEM buffer object's handle. Possibly releases 291 * the GEM buffer object and associated dma-buf objects. 292 */ 293 void drm_gem_object_handle_put_unlocked(struct drm_gem_object *obj) 294 { 295 struct drm_device *dev = obj->dev; 296 bool final = false; 297 298 if (drm_WARN_ON(dev, READ_ONCE(obj->handle_count) == 0)) 299 return; 300 301 /* 302 * Must bump handle count first as this may be the last 303 * ref, in which case the object would disappear before 304 * we checked for a name. 305 */ 306 307 mutex_lock(&dev->object_name_lock); 308 if (--obj->handle_count == 0) { 309 drm_gem_object_handle_free(obj); 310 drm_gem_object_exported_dma_buf_free(obj); 311 final = true; 312 } 313 mutex_unlock(&dev->object_name_lock); 314 315 if (final) 316 drm_gem_object_put(obj); 317 } 318 319 /* 320 * Called at device or object close to release the file's 321 * handle references on objects. 322 */ 323 static int 324 drm_gem_object_release_handle(int id, void *ptr, void *data) 325 { 326 struct drm_file *file_priv = data; 327 struct drm_gem_object *obj = ptr; 328 329 if (drm_WARN_ON(obj->dev, !data)) 330 return 0; 331 332 if (obj->funcs->close) 333 obj->funcs->close(obj, file_priv); 334 335 drm_prime_remove_buf_handle(&file_priv->prime, id); 336 drm_vma_node_revoke(&obj->vma_node, file_priv); 337 338 drm_gem_object_handle_put_unlocked(obj); 339 340 return 0; 341 } 342 343 /** 344 * drm_gem_handle_delete - deletes the given file-private handle 345 * @filp: drm file-private structure to use for the handle look up 346 * @handle: userspace handle to delete 347 * 348 * Removes the GEM handle from the @filp lookup table which has been added with 349 * drm_gem_handle_create(). If this is the last handle also cleans up linked 350 * resources like GEM names. 351 */ 352 int 353 drm_gem_handle_delete(struct drm_file *filp, u32 handle) 354 { 355 struct drm_gem_object *obj; 356 357 spin_lock(&filp->table_lock); 358 359 /* Check if we currently have a reference on the object */ 360 obj = idr_replace(&filp->object_idr, NULL, handle); 361 spin_unlock(&filp->table_lock); 362 if (IS_ERR_OR_NULL(obj)) 363 return -EINVAL; 364 365 /* Release driver's reference and decrement refcount. */ 366 drm_gem_object_release_handle(handle, obj, filp); 367 368 /* And finally make the handle available for future allocations. */ 369 spin_lock(&filp->table_lock); 370 idr_remove(&filp->object_idr, handle); 371 spin_unlock(&filp->table_lock); 372 373 return 0; 374 } 375 EXPORT_SYMBOL(drm_gem_handle_delete); 376 377 /** 378 * drm_gem_dumb_map_offset - return the fake mmap offset for a gem object 379 * @file: drm file-private structure containing the gem object 380 * @dev: corresponding drm_device 381 * @handle: gem object handle 382 * @offset: return location for the fake mmap offset 383 * 384 * This implements the &drm_driver.dumb_map_offset kms driver callback for 385 * drivers which use gem to manage their backing storage. 386 * 387 * Returns: 388 * 0 on success or a negative error code on failure. 389 */ 390 int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev, 391 u32 handle, u64 *offset) 392 { 393 struct drm_gem_object *obj; 394 int ret; 395 396 obj = drm_gem_object_lookup(file, handle); 397 if (!obj) 398 return -ENOENT; 399 400 /* Don't allow imported objects to be mapped */ 401 if (drm_gem_is_imported(obj)) { 402 ret = -EINVAL; 403 goto out; 404 } 405 406 ret = drm_gem_create_mmap_offset(obj); 407 if (ret) 408 goto out; 409 410 *offset = drm_vma_node_offset_addr(&obj->vma_node); 411 out: 412 drm_gem_object_put(obj); 413 414 return ret; 415 } 416 EXPORT_SYMBOL_GPL(drm_gem_dumb_map_offset); 417 418 /** 419 * drm_gem_handle_create_tail - internal functions to create a handle 420 * @file_priv: drm file-private structure to register the handle for 421 * @obj: object to register 422 * @handlep: pointer to return the created handle to the caller 423 * 424 * This expects the &drm_device.object_name_lock to be held already and will 425 * drop it before returning. Used to avoid races in establishing new handles 426 * when importing an object from either an flink name or a dma-buf. 427 * 428 * Handles must be release again through drm_gem_handle_delete(). This is done 429 * when userspace closes @file_priv for all attached handles, or through the 430 * GEM_CLOSE ioctl for individual handles. 431 */ 432 int 433 drm_gem_handle_create_tail(struct drm_file *file_priv, 434 struct drm_gem_object *obj, 435 u32 *handlep) 436 { 437 struct drm_device *dev = obj->dev; 438 u32 handle; 439 int ret; 440 441 WARN_ON(!mutex_is_locked(&dev->object_name_lock)); 442 443 drm_gem_object_handle_get(obj); 444 445 /* 446 * Get the user-visible handle using idr. Preload and perform 447 * allocation under our spinlock. 448 */ 449 idr_preload(GFP_KERNEL); 450 spin_lock(&file_priv->table_lock); 451 452 ret = idr_alloc(&file_priv->object_idr, NULL, 1, 0, GFP_NOWAIT); 453 454 spin_unlock(&file_priv->table_lock); 455 idr_preload_end(); 456 457 mutex_unlock(&dev->object_name_lock); 458 if (ret < 0) 459 goto err_unref; 460 461 handle = ret; 462 463 ret = drm_vma_node_allow(&obj->vma_node, file_priv); 464 if (ret) 465 goto err_remove; 466 467 if (obj->funcs->open) { 468 ret = obj->funcs->open(obj, file_priv); 469 if (ret) 470 goto err_revoke; 471 } 472 473 /* mirrors drm_gem_handle_delete to avoid races */ 474 spin_lock(&file_priv->table_lock); 475 obj = idr_replace(&file_priv->object_idr, obj, handle); 476 WARN_ON(obj != NULL); 477 spin_unlock(&file_priv->table_lock); 478 *handlep = handle; 479 return 0; 480 481 err_revoke: 482 drm_vma_node_revoke(&obj->vma_node, file_priv); 483 err_remove: 484 spin_lock(&file_priv->table_lock); 485 idr_remove(&file_priv->object_idr, handle); 486 spin_unlock(&file_priv->table_lock); 487 err_unref: 488 drm_gem_object_handle_put_unlocked(obj); 489 return ret; 490 } 491 492 /** 493 * drm_gem_handle_create - create a gem handle for an object 494 * @file_priv: drm file-private structure to register the handle for 495 * @obj: object to register 496 * @handlep: pointer to return the created handle to the caller 497 * 498 * Create a handle for this object. This adds a handle reference to the object, 499 * which includes a regular reference count. Callers will likely want to 500 * dereference the object afterwards. 501 * 502 * Since this publishes @obj to userspace it must be fully set up by this point, 503 * drivers must call this last in their buffer object creation callbacks. 504 */ 505 int drm_gem_handle_create(struct drm_file *file_priv, 506 struct drm_gem_object *obj, 507 u32 *handlep) 508 { 509 mutex_lock(&obj->dev->object_name_lock); 510 511 return drm_gem_handle_create_tail(file_priv, obj, handlep); 512 } 513 EXPORT_SYMBOL(drm_gem_handle_create); 514 515 516 /** 517 * drm_gem_free_mmap_offset - release a fake mmap offset for an object 518 * @obj: obj in question 519 * 520 * This routine frees fake offsets allocated by drm_gem_create_mmap_offset(). 521 * 522 * Note that drm_gem_object_release() already calls this function, so drivers 523 * don't have to take care of releasing the mmap offset themselves when freeing 524 * the GEM object. 525 */ 526 void 527 drm_gem_free_mmap_offset(struct drm_gem_object *obj) 528 { 529 struct drm_device *dev = obj->dev; 530 531 drm_vma_offset_remove(dev->vma_offset_manager, &obj->vma_node); 532 } 533 EXPORT_SYMBOL(drm_gem_free_mmap_offset); 534 535 /** 536 * drm_gem_create_mmap_offset_size - create a fake mmap offset for an object 537 * @obj: obj in question 538 * @size: the virtual size 539 * 540 * GEM memory mapping works by handing back to userspace a fake mmap offset 541 * it can use in a subsequent mmap(2) call. The DRM core code then looks 542 * up the object based on the offset and sets up the various memory mapping 543 * structures. 544 * 545 * This routine allocates and attaches a fake offset for @obj, in cases where 546 * the virtual size differs from the physical size (ie. &drm_gem_object.size). 547 * Otherwise just use drm_gem_create_mmap_offset(). 548 * 549 * This function is idempotent and handles an already allocated mmap offset 550 * transparently. Drivers do not need to check for this case. 551 */ 552 int 553 drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size) 554 { 555 struct drm_device *dev = obj->dev; 556 557 return drm_vma_offset_add(dev->vma_offset_manager, &obj->vma_node, 558 size / PAGE_SIZE); 559 } 560 EXPORT_SYMBOL(drm_gem_create_mmap_offset_size); 561 562 /** 563 * drm_gem_create_mmap_offset - create a fake mmap offset for an object 564 * @obj: obj in question 565 * 566 * GEM memory mapping works by handing back to userspace a fake mmap offset 567 * it can use in a subsequent mmap(2) call. The DRM core code then looks 568 * up the object based on the offset and sets up the various memory mapping 569 * structures. 570 * 571 * This routine allocates and attaches a fake offset for @obj. 572 * 573 * Drivers can call drm_gem_free_mmap_offset() before freeing @obj to release 574 * the fake offset again. 575 */ 576 int drm_gem_create_mmap_offset(struct drm_gem_object *obj) 577 { 578 return drm_gem_create_mmap_offset_size(obj, obj->size); 579 } 580 EXPORT_SYMBOL(drm_gem_create_mmap_offset); 581 582 /* 583 * Move folios to appropriate lru and release the folios, decrementing the 584 * ref count of those folios. 585 */ 586 static void drm_gem_check_release_batch(struct folio_batch *fbatch) 587 { 588 check_move_unevictable_folios(fbatch); 589 __folio_batch_release(fbatch); 590 cond_resched(); 591 } 592 593 /** 594 * drm_gem_get_pages - helper to allocate backing pages for a GEM object 595 * from shmem 596 * @obj: obj in question 597 * 598 * This reads the page-array of the shmem-backing storage of the given gem 599 * object. An array of pages is returned. If a page is not allocated or 600 * swapped-out, this will allocate/swap-in the required pages. Note that the 601 * whole object is covered by the page-array and pinned in memory. 602 * 603 * Use drm_gem_put_pages() to release the array and unpin all pages. 604 * 605 * This uses the GFP-mask set on the shmem-mapping (see mapping_set_gfp_mask()). 606 * If you require other GFP-masks, you have to do those allocations yourself. 607 * 608 * Note that you are not allowed to change gfp-zones during runtime. That is, 609 * shmem_read_mapping_page_gfp() must be called with the same gfp_zone(gfp) as 610 * set during initialization. If you have special zone constraints, set them 611 * after drm_gem_object_init() via mapping_set_gfp_mask(). shmem-core takes care 612 * to keep pages in the required zone during swap-in. 613 * 614 * This function is only valid on objects initialized with 615 * drm_gem_object_init(), but not for those initialized with 616 * drm_gem_private_object_init() only. 617 */ 618 struct page **drm_gem_get_pages(struct drm_gem_object *obj) 619 { 620 struct address_space *mapping; 621 struct page **pages; 622 struct folio *folio; 623 struct folio_batch fbatch; 624 long i, j, npages; 625 626 if (WARN_ON(!obj->filp)) 627 return ERR_PTR(-EINVAL); 628 629 /* This is the shared memory object that backs the GEM resource */ 630 mapping = obj->filp->f_mapping; 631 632 /* We already BUG_ON() for non-page-aligned sizes in 633 * drm_gem_object_init(), so we should never hit this unless 634 * driver author is doing something really wrong: 635 */ 636 WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0); 637 638 npages = obj->size >> PAGE_SHIFT; 639 640 pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL); 641 if (pages == NULL) 642 return ERR_PTR(-ENOMEM); 643 644 mapping_set_unevictable(mapping); 645 646 i = 0; 647 while (i < npages) { 648 long nr; 649 folio = shmem_read_folio_gfp(mapping, i, 650 mapping_gfp_mask(mapping)); 651 if (IS_ERR(folio)) 652 goto fail; 653 nr = min(npages - i, folio_nr_pages(folio)); 654 for (j = 0; j < nr; j++, i++) 655 pages[i] = folio_file_page(folio, i); 656 657 /* Make sure shmem keeps __GFP_DMA32 allocated pages in the 658 * correct region during swapin. Note that this requires 659 * __GFP_DMA32 to be set in mapping_gfp_mask(inode->i_mapping) 660 * so shmem can relocate pages during swapin if required. 661 */ 662 BUG_ON(mapping_gfp_constraint(mapping, __GFP_DMA32) && 663 (folio_pfn(folio) >= 0x00100000UL)); 664 } 665 666 return pages; 667 668 fail: 669 mapping_clear_unevictable(mapping); 670 folio_batch_init(&fbatch); 671 j = 0; 672 while (j < i) { 673 struct folio *f = page_folio(pages[j]); 674 if (!folio_batch_add(&fbatch, f)) 675 drm_gem_check_release_batch(&fbatch); 676 j += folio_nr_pages(f); 677 } 678 if (fbatch.nr) 679 drm_gem_check_release_batch(&fbatch); 680 681 kvfree(pages); 682 return ERR_CAST(folio); 683 } 684 EXPORT_SYMBOL(drm_gem_get_pages); 685 686 /** 687 * drm_gem_put_pages - helper to free backing pages for a GEM object 688 * @obj: obj in question 689 * @pages: pages to free 690 * @dirty: if true, pages will be marked as dirty 691 * @accessed: if true, the pages will be marked as accessed 692 */ 693 void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages, 694 bool dirty, bool accessed) 695 { 696 int i, npages; 697 struct address_space *mapping; 698 struct folio_batch fbatch; 699 700 mapping = file_inode(obj->filp)->i_mapping; 701 mapping_clear_unevictable(mapping); 702 703 /* We already BUG_ON() for non-page-aligned sizes in 704 * drm_gem_object_init(), so we should never hit this unless 705 * driver author is doing something really wrong: 706 */ 707 WARN_ON((obj->size & (PAGE_SIZE - 1)) != 0); 708 709 npages = obj->size >> PAGE_SHIFT; 710 711 folio_batch_init(&fbatch); 712 for (i = 0; i < npages; i++) { 713 struct folio *folio; 714 715 if (!pages[i]) 716 continue; 717 folio = page_folio(pages[i]); 718 719 if (dirty) 720 folio_mark_dirty(folio); 721 722 if (accessed) 723 folio_mark_accessed(folio); 724 725 /* Undo the reference we took when populating the table */ 726 if (!folio_batch_add(&fbatch, folio)) 727 drm_gem_check_release_batch(&fbatch); 728 i += folio_nr_pages(folio) - 1; 729 } 730 if (folio_batch_count(&fbatch)) 731 drm_gem_check_release_batch(&fbatch); 732 733 kvfree(pages); 734 } 735 EXPORT_SYMBOL(drm_gem_put_pages); 736 737 static int objects_lookup(struct drm_file *filp, u32 *handle, int count, 738 struct drm_gem_object **objs) 739 { 740 int i, ret = 0; 741 struct drm_gem_object *obj; 742 743 spin_lock(&filp->table_lock); 744 745 for (i = 0; i < count; i++) { 746 /* Check if we currently have a reference on the object */ 747 obj = idr_find(&filp->object_idr, handle[i]); 748 if (!obj) { 749 ret = -ENOENT; 750 break; 751 } 752 drm_gem_object_get(obj); 753 objs[i] = obj; 754 } 755 spin_unlock(&filp->table_lock); 756 757 return ret; 758 } 759 760 /** 761 * drm_gem_objects_lookup - look up GEM objects from an array of handles 762 * @filp: DRM file private date 763 * @bo_handles: user pointer to array of userspace handle 764 * @count: size of handle array 765 * @objs_out: returned pointer to array of drm_gem_object pointers 766 * 767 * Takes an array of userspace handles and returns a newly allocated array of 768 * GEM objects. 769 * 770 * For a single handle lookup, use drm_gem_object_lookup(). 771 * 772 * Returns: 773 * @objs filled in with GEM object pointers. Returned GEM objects need to be 774 * released with drm_gem_object_put(). -ENOENT is returned on a lookup 775 * failure. 0 is returned on success. 776 * 777 */ 778 int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles, 779 int count, struct drm_gem_object ***objs_out) 780 { 781 int ret; 782 u32 *handles; 783 struct drm_gem_object **objs; 784 785 if (!count) 786 return 0; 787 788 objs = kvmalloc_array(count, sizeof(struct drm_gem_object *), 789 GFP_KERNEL | __GFP_ZERO); 790 if (!objs) 791 return -ENOMEM; 792 793 *objs_out = objs; 794 795 handles = kvmalloc_array(count, sizeof(u32), GFP_KERNEL); 796 if (!handles) { 797 ret = -ENOMEM; 798 goto out; 799 } 800 801 if (copy_from_user(handles, bo_handles, count * sizeof(u32))) { 802 ret = -EFAULT; 803 DRM_DEBUG("Failed to copy in GEM handles\n"); 804 goto out; 805 } 806 807 ret = objects_lookup(filp, handles, count, objs); 808 out: 809 kvfree(handles); 810 return ret; 811 812 } 813 EXPORT_SYMBOL(drm_gem_objects_lookup); 814 815 /** 816 * drm_gem_object_lookup - look up a GEM object from its handle 817 * @filp: DRM file private date 818 * @handle: userspace handle 819 * 820 * If looking up an array of handles, use drm_gem_objects_lookup(). 821 * 822 * Returns: 823 * A reference to the object named by the handle if such exists on @filp, NULL 824 * otherwise. 825 */ 826 struct drm_gem_object * 827 drm_gem_object_lookup(struct drm_file *filp, u32 handle) 828 { 829 struct drm_gem_object *obj = NULL; 830 831 objects_lookup(filp, &handle, 1, &obj); 832 return obj; 833 } 834 EXPORT_SYMBOL(drm_gem_object_lookup); 835 836 /** 837 * drm_gem_dma_resv_wait - Wait on GEM object's reservation's objects 838 * shared and/or exclusive fences. 839 * @filep: DRM file private date 840 * @handle: userspace handle 841 * @wait_all: if true, wait on all fences, else wait on just exclusive fence 842 * @timeout: timeout value in jiffies or zero to return immediately 843 * 844 * Returns: 845 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or 846 * greater than 0 on success. 847 */ 848 long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle, 849 bool wait_all, unsigned long timeout) 850 { 851 long ret; 852 struct drm_gem_object *obj; 853 854 obj = drm_gem_object_lookup(filep, handle); 855 if (!obj) { 856 DRM_DEBUG("Failed to look up GEM BO %d\n", handle); 857 return -EINVAL; 858 } 859 860 ret = dma_resv_wait_timeout(obj->resv, dma_resv_usage_rw(wait_all), 861 true, timeout); 862 if (ret == 0) 863 ret = -ETIME; 864 else if (ret > 0) 865 ret = 0; 866 867 drm_gem_object_put(obj); 868 869 return ret; 870 } 871 EXPORT_SYMBOL(drm_gem_dma_resv_wait); 872 873 /** 874 * drm_gem_close_ioctl - implementation of the GEM_CLOSE ioctl 875 * @dev: drm_device 876 * @data: ioctl data 877 * @file_priv: drm file-private structure 878 * 879 * Releases the handle to an mm object. 880 */ 881 int 882 drm_gem_close_ioctl(struct drm_device *dev, void *data, 883 struct drm_file *file_priv) 884 { 885 struct drm_gem_close *args = data; 886 int ret; 887 888 if (!drm_core_check_feature(dev, DRIVER_GEM)) 889 return -EOPNOTSUPP; 890 891 ret = drm_gem_handle_delete(file_priv, args->handle); 892 893 return ret; 894 } 895 896 /** 897 * drm_gem_flink_ioctl - implementation of the GEM_FLINK ioctl 898 * @dev: drm_device 899 * @data: ioctl data 900 * @file_priv: drm file-private structure 901 * 902 * Create a global name for an object, returning the name. 903 * 904 * Note that the name does not hold a reference; when the object 905 * is freed, the name goes away. 906 */ 907 int 908 drm_gem_flink_ioctl(struct drm_device *dev, void *data, 909 struct drm_file *file_priv) 910 { 911 struct drm_gem_flink *args = data; 912 struct drm_gem_object *obj; 913 int ret; 914 915 if (!drm_core_check_feature(dev, DRIVER_GEM)) 916 return -EOPNOTSUPP; 917 918 obj = drm_gem_object_lookup(file_priv, args->handle); 919 if (obj == NULL) 920 return -ENOENT; 921 922 mutex_lock(&dev->object_name_lock); 923 /* prevent races with concurrent gem_close. */ 924 if (obj->handle_count == 0) { 925 ret = -ENOENT; 926 goto err; 927 } 928 929 if (!obj->name) { 930 ret = idr_alloc(&dev->object_name_idr, obj, 1, 0, GFP_KERNEL); 931 if (ret < 0) 932 goto err; 933 934 obj->name = ret; 935 } 936 937 args->name = (uint64_t) obj->name; 938 ret = 0; 939 940 err: 941 mutex_unlock(&dev->object_name_lock); 942 drm_gem_object_put(obj); 943 return ret; 944 } 945 946 /** 947 * drm_gem_open_ioctl - implementation of the GEM_OPEN ioctl 948 * @dev: drm_device 949 * @data: ioctl data 950 * @file_priv: drm file-private structure 951 * 952 * Open an object using the global name, returning a handle and the size. 953 * 954 * This handle (of course) holds a reference to the object, so the object 955 * will not go away until the handle is deleted. 956 */ 957 int 958 drm_gem_open_ioctl(struct drm_device *dev, void *data, 959 struct drm_file *file_priv) 960 { 961 struct drm_gem_open *args = data; 962 struct drm_gem_object *obj; 963 int ret; 964 u32 handle; 965 966 if (!drm_core_check_feature(dev, DRIVER_GEM)) 967 return -EOPNOTSUPP; 968 969 mutex_lock(&dev->object_name_lock); 970 obj = idr_find(&dev->object_name_idr, (int) args->name); 971 if (obj) { 972 drm_gem_object_get(obj); 973 } else { 974 mutex_unlock(&dev->object_name_lock); 975 return -ENOENT; 976 } 977 978 /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */ 979 ret = drm_gem_handle_create_tail(file_priv, obj, &handle); 980 if (ret) 981 goto err; 982 983 args->handle = handle; 984 args->size = obj->size; 985 986 err: 987 drm_gem_object_put(obj); 988 return ret; 989 } 990 991 /** 992 * drm_gem_open - initializes GEM file-private structures at devnode open time 993 * @dev: drm_device which is being opened by userspace 994 * @file_private: drm file-private structure to set up 995 * 996 * Called at device open time, sets up the structure for handling refcounting 997 * of mm objects. 998 */ 999 void 1000 drm_gem_open(struct drm_device *dev, struct drm_file *file_private) 1001 { 1002 idr_init_base(&file_private->object_idr, 1); 1003 spin_lock_init(&file_private->table_lock); 1004 } 1005 1006 /** 1007 * drm_gem_release - release file-private GEM resources 1008 * @dev: drm_device which is being closed by userspace 1009 * @file_private: drm file-private structure to clean up 1010 * 1011 * Called at close time when the filp is going away. 1012 * 1013 * Releases any remaining references on objects by this filp. 1014 */ 1015 void 1016 drm_gem_release(struct drm_device *dev, struct drm_file *file_private) 1017 { 1018 idr_for_each(&file_private->object_idr, 1019 &drm_gem_object_release_handle, file_private); 1020 idr_destroy(&file_private->object_idr); 1021 } 1022 1023 /** 1024 * drm_gem_object_release - release GEM buffer object resources 1025 * @obj: GEM buffer object 1026 * 1027 * This releases any structures and resources used by @obj and is the inverse of 1028 * drm_gem_object_init(). 1029 */ 1030 void 1031 drm_gem_object_release(struct drm_gem_object *obj) 1032 { 1033 if (obj->filp) 1034 fput(obj->filp); 1035 1036 drm_gem_private_object_fini(obj); 1037 1038 drm_gem_free_mmap_offset(obj); 1039 drm_gem_lru_remove(obj); 1040 } 1041 EXPORT_SYMBOL(drm_gem_object_release); 1042 1043 /** 1044 * drm_gem_object_free - free a GEM object 1045 * @kref: kref of the object to free 1046 * 1047 * Called after the last reference to the object has been lost. 1048 * 1049 * Frees the object 1050 */ 1051 void 1052 drm_gem_object_free(struct kref *kref) 1053 { 1054 struct drm_gem_object *obj = 1055 container_of(kref, struct drm_gem_object, refcount); 1056 1057 if (WARN_ON(!obj->funcs->free)) 1058 return; 1059 1060 obj->funcs->free(obj); 1061 } 1062 EXPORT_SYMBOL(drm_gem_object_free); 1063 1064 /** 1065 * drm_gem_vm_open - vma->ops->open implementation for GEM 1066 * @vma: VM area structure 1067 * 1068 * This function implements the #vm_operations_struct open() callback for GEM 1069 * drivers. This must be used together with drm_gem_vm_close(). 1070 */ 1071 void drm_gem_vm_open(struct vm_area_struct *vma) 1072 { 1073 struct drm_gem_object *obj = vma->vm_private_data; 1074 1075 drm_gem_object_get(obj); 1076 } 1077 EXPORT_SYMBOL(drm_gem_vm_open); 1078 1079 /** 1080 * drm_gem_vm_close - vma->ops->close implementation for GEM 1081 * @vma: VM area structure 1082 * 1083 * This function implements the #vm_operations_struct close() callback for GEM 1084 * drivers. This must be used together with drm_gem_vm_open(). 1085 */ 1086 void drm_gem_vm_close(struct vm_area_struct *vma) 1087 { 1088 struct drm_gem_object *obj = vma->vm_private_data; 1089 1090 drm_gem_object_put(obj); 1091 } 1092 EXPORT_SYMBOL(drm_gem_vm_close); 1093 1094 /** 1095 * drm_gem_mmap_obj - memory map a GEM object 1096 * @obj: the GEM object to map 1097 * @obj_size: the object size to be mapped, in bytes 1098 * @vma: VMA for the area to be mapped 1099 * 1100 * Set up the VMA to prepare mapping of the GEM object using the GEM object's 1101 * vm_ops. Depending on their requirements, GEM objects can either 1102 * provide a fault handler in their vm_ops (in which case any accesses to 1103 * the object will be trapped, to perform migration, GTT binding, surface 1104 * register allocation, or performance monitoring), or mmap the buffer memory 1105 * synchronously after calling drm_gem_mmap_obj. 1106 * 1107 * This function is mainly intended to implement the DMABUF mmap operation, when 1108 * the GEM object is not looked up based on its fake offset. To implement the 1109 * DRM mmap operation, drivers should use the drm_gem_mmap() function. 1110 * 1111 * drm_gem_mmap_obj() assumes the user is granted access to the buffer while 1112 * drm_gem_mmap() prevents unprivileged users from mapping random objects. So 1113 * callers must verify access restrictions before calling this helper. 1114 * 1115 * Return 0 or success or -EINVAL if the object size is smaller than the VMA 1116 * size, or if no vm_ops are provided. 1117 */ 1118 int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size, 1119 struct vm_area_struct *vma) 1120 { 1121 int ret; 1122 1123 /* Check for valid size. */ 1124 if (obj_size < vma->vm_end - vma->vm_start) 1125 return -EINVAL; 1126 1127 /* Take a ref for this mapping of the object, so that the fault 1128 * handler can dereference the mmap offset's pointer to the object. 1129 * This reference is cleaned up by the corresponding vm_close 1130 * (which should happen whether the vma was created by this call, or 1131 * by a vm_open due to mremap or partial unmap or whatever). 1132 */ 1133 drm_gem_object_get(obj); 1134 1135 vma->vm_private_data = obj; 1136 vma->vm_ops = obj->funcs->vm_ops; 1137 1138 if (obj->funcs->mmap) { 1139 ret = obj->funcs->mmap(obj, vma); 1140 if (ret) 1141 goto err_drm_gem_object_put; 1142 WARN_ON(!(vma->vm_flags & VM_DONTEXPAND)); 1143 } else { 1144 if (!vma->vm_ops) { 1145 ret = -EINVAL; 1146 goto err_drm_gem_object_put; 1147 } 1148 1149 vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP); 1150 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); 1151 vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot); 1152 } 1153 1154 return 0; 1155 1156 err_drm_gem_object_put: 1157 drm_gem_object_put(obj); 1158 return ret; 1159 } 1160 EXPORT_SYMBOL(drm_gem_mmap_obj); 1161 1162 /** 1163 * drm_gem_mmap - memory map routine for GEM objects 1164 * @filp: DRM file pointer 1165 * @vma: VMA for the area to be mapped 1166 * 1167 * If a driver supports GEM object mapping, mmap calls on the DRM file 1168 * descriptor will end up here. 1169 * 1170 * Look up the GEM object based on the offset passed in (vma->vm_pgoff will 1171 * contain the fake offset we created when the GTT map ioctl was called on 1172 * the object) and map it with a call to drm_gem_mmap_obj(). 1173 * 1174 * If the caller is not granted access to the buffer object, the mmap will fail 1175 * with EACCES. Please see the vma manager for more information. 1176 */ 1177 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma) 1178 { 1179 struct drm_file *priv = filp->private_data; 1180 struct drm_device *dev = priv->minor->dev; 1181 struct drm_gem_object *obj = NULL; 1182 struct drm_vma_offset_node *node; 1183 int ret; 1184 1185 if (drm_dev_is_unplugged(dev)) 1186 return -ENODEV; 1187 1188 drm_vma_offset_lock_lookup(dev->vma_offset_manager); 1189 node = drm_vma_offset_exact_lookup_locked(dev->vma_offset_manager, 1190 vma->vm_pgoff, 1191 vma_pages(vma)); 1192 if (likely(node)) { 1193 obj = container_of(node, struct drm_gem_object, vma_node); 1194 /* 1195 * When the object is being freed, after it hits 0-refcnt it 1196 * proceeds to tear down the object. In the process it will 1197 * attempt to remove the VMA offset and so acquire this 1198 * mgr->vm_lock. Therefore if we find an object with a 0-refcnt 1199 * that matches our range, we know it is in the process of being 1200 * destroyed and will be freed as soon as we release the lock - 1201 * so we have to check for the 0-refcnted object and treat it as 1202 * invalid. 1203 */ 1204 if (!kref_get_unless_zero(&obj->refcount)) 1205 obj = NULL; 1206 } 1207 drm_vma_offset_unlock_lookup(dev->vma_offset_manager); 1208 1209 if (!obj) 1210 return -EINVAL; 1211 1212 if (!drm_vma_node_is_allowed(node, priv)) { 1213 drm_gem_object_put(obj); 1214 return -EACCES; 1215 } 1216 1217 ret = drm_gem_mmap_obj(obj, drm_vma_node_size(node) << PAGE_SHIFT, 1218 vma); 1219 1220 drm_gem_object_put(obj); 1221 1222 return ret; 1223 } 1224 EXPORT_SYMBOL(drm_gem_mmap); 1225 1226 void drm_gem_print_info(struct drm_printer *p, unsigned int indent, 1227 const struct drm_gem_object *obj) 1228 { 1229 drm_printf_indent(p, indent, "name=%d\n", obj->name); 1230 drm_printf_indent(p, indent, "refcount=%u\n", 1231 kref_read(&obj->refcount)); 1232 drm_printf_indent(p, indent, "start=%08lx\n", 1233 drm_vma_node_start(&obj->vma_node)); 1234 drm_printf_indent(p, indent, "size=%zu\n", obj->size); 1235 drm_printf_indent(p, indent, "imported=%s\n", 1236 str_yes_no(drm_gem_is_imported(obj))); 1237 1238 if (obj->funcs->print_info) 1239 obj->funcs->print_info(p, indent, obj); 1240 } 1241 1242 int drm_gem_vmap_locked(struct drm_gem_object *obj, struct iosys_map *map) 1243 { 1244 int ret; 1245 1246 dma_resv_assert_held(obj->resv); 1247 1248 if (!obj->funcs->vmap) 1249 return -EOPNOTSUPP; 1250 1251 ret = obj->funcs->vmap(obj, map); 1252 if (ret) 1253 return ret; 1254 else if (iosys_map_is_null(map)) 1255 return -ENOMEM; 1256 1257 return 0; 1258 } 1259 EXPORT_SYMBOL(drm_gem_vmap_locked); 1260 1261 void drm_gem_vunmap_locked(struct drm_gem_object *obj, struct iosys_map *map) 1262 { 1263 dma_resv_assert_held(obj->resv); 1264 1265 if (iosys_map_is_null(map)) 1266 return; 1267 1268 if (obj->funcs->vunmap) 1269 obj->funcs->vunmap(obj, map); 1270 1271 /* Always set the mapping to NULL. Callers may rely on this. */ 1272 iosys_map_clear(map); 1273 } 1274 EXPORT_SYMBOL(drm_gem_vunmap_locked); 1275 1276 void drm_gem_lock(struct drm_gem_object *obj) 1277 { 1278 dma_resv_lock(obj->resv, NULL); 1279 } 1280 EXPORT_SYMBOL(drm_gem_lock); 1281 1282 void drm_gem_unlock(struct drm_gem_object *obj) 1283 { 1284 dma_resv_unlock(obj->resv); 1285 } 1286 EXPORT_SYMBOL(drm_gem_unlock); 1287 1288 int drm_gem_vmap(struct drm_gem_object *obj, struct iosys_map *map) 1289 { 1290 int ret; 1291 1292 dma_resv_lock(obj->resv, NULL); 1293 ret = drm_gem_vmap_locked(obj, map); 1294 dma_resv_unlock(obj->resv); 1295 1296 return ret; 1297 } 1298 EXPORT_SYMBOL(drm_gem_vmap); 1299 1300 void drm_gem_vunmap(struct drm_gem_object *obj, struct iosys_map *map) 1301 { 1302 dma_resv_lock(obj->resv, NULL); 1303 drm_gem_vunmap_locked(obj, map); 1304 dma_resv_unlock(obj->resv); 1305 } 1306 EXPORT_SYMBOL(drm_gem_vunmap); 1307 1308 /** 1309 * drm_gem_lock_reservations - Sets up the ww context and acquires 1310 * the lock on an array of GEM objects. 1311 * 1312 * Once you've locked your reservations, you'll want to set up space 1313 * for your shared fences (if applicable), submit your job, then 1314 * drm_gem_unlock_reservations(). 1315 * 1316 * @objs: drm_gem_objects to lock 1317 * @count: Number of objects in @objs 1318 * @acquire_ctx: struct ww_acquire_ctx that will be initialized as 1319 * part of tracking this set of locked reservations. 1320 */ 1321 int 1322 drm_gem_lock_reservations(struct drm_gem_object **objs, int count, 1323 struct ww_acquire_ctx *acquire_ctx) 1324 { 1325 int contended = -1; 1326 int i, ret; 1327 1328 ww_acquire_init(acquire_ctx, &reservation_ww_class); 1329 1330 retry: 1331 if (contended != -1) { 1332 struct drm_gem_object *obj = objs[contended]; 1333 1334 ret = dma_resv_lock_slow_interruptible(obj->resv, 1335 acquire_ctx); 1336 if (ret) { 1337 ww_acquire_fini(acquire_ctx); 1338 return ret; 1339 } 1340 } 1341 1342 for (i = 0; i < count; i++) { 1343 if (i == contended) 1344 continue; 1345 1346 ret = dma_resv_lock_interruptible(objs[i]->resv, 1347 acquire_ctx); 1348 if (ret) { 1349 int j; 1350 1351 for (j = 0; j < i; j++) 1352 dma_resv_unlock(objs[j]->resv); 1353 1354 if (contended != -1 && contended >= i) 1355 dma_resv_unlock(objs[contended]->resv); 1356 1357 if (ret == -EDEADLK) { 1358 contended = i; 1359 goto retry; 1360 } 1361 1362 ww_acquire_fini(acquire_ctx); 1363 return ret; 1364 } 1365 } 1366 1367 ww_acquire_done(acquire_ctx); 1368 1369 return 0; 1370 } 1371 EXPORT_SYMBOL(drm_gem_lock_reservations); 1372 1373 void 1374 drm_gem_unlock_reservations(struct drm_gem_object **objs, int count, 1375 struct ww_acquire_ctx *acquire_ctx) 1376 { 1377 int i; 1378 1379 for (i = 0; i < count; i++) 1380 dma_resv_unlock(objs[i]->resv); 1381 1382 ww_acquire_fini(acquire_ctx); 1383 } 1384 EXPORT_SYMBOL(drm_gem_unlock_reservations); 1385 1386 /** 1387 * drm_gem_lru_init - initialize a LRU 1388 * 1389 * @lru: The LRU to initialize 1390 * @lock: The lock protecting the LRU 1391 */ 1392 void 1393 drm_gem_lru_init(struct drm_gem_lru *lru, struct mutex *lock) 1394 { 1395 lru->lock = lock; 1396 lru->count = 0; 1397 INIT_LIST_HEAD(&lru->list); 1398 } 1399 EXPORT_SYMBOL(drm_gem_lru_init); 1400 1401 static void 1402 drm_gem_lru_remove_locked(struct drm_gem_object *obj) 1403 { 1404 obj->lru->count -= obj->size >> PAGE_SHIFT; 1405 WARN_ON(obj->lru->count < 0); 1406 list_del(&obj->lru_node); 1407 obj->lru = NULL; 1408 } 1409 1410 /** 1411 * drm_gem_lru_remove - remove object from whatever LRU it is in 1412 * 1413 * If the object is currently in any LRU, remove it. 1414 * 1415 * @obj: The GEM object to remove from current LRU 1416 */ 1417 void 1418 drm_gem_lru_remove(struct drm_gem_object *obj) 1419 { 1420 struct drm_gem_lru *lru = obj->lru; 1421 1422 if (!lru) 1423 return; 1424 1425 mutex_lock(lru->lock); 1426 drm_gem_lru_remove_locked(obj); 1427 mutex_unlock(lru->lock); 1428 } 1429 EXPORT_SYMBOL(drm_gem_lru_remove); 1430 1431 /** 1432 * drm_gem_lru_move_tail_locked - move the object to the tail of the LRU 1433 * 1434 * Like &drm_gem_lru_move_tail but lru lock must be held 1435 * 1436 * @lru: The LRU to move the object into. 1437 * @obj: The GEM object to move into this LRU 1438 */ 1439 void 1440 drm_gem_lru_move_tail_locked(struct drm_gem_lru *lru, struct drm_gem_object *obj) 1441 { 1442 lockdep_assert_held_once(lru->lock); 1443 1444 if (obj->lru) 1445 drm_gem_lru_remove_locked(obj); 1446 1447 lru->count += obj->size >> PAGE_SHIFT; 1448 list_add_tail(&obj->lru_node, &lru->list); 1449 obj->lru = lru; 1450 } 1451 EXPORT_SYMBOL(drm_gem_lru_move_tail_locked); 1452 1453 /** 1454 * drm_gem_lru_move_tail - move the object to the tail of the LRU 1455 * 1456 * If the object is already in this LRU it will be moved to the 1457 * tail. Otherwise it will be removed from whichever other LRU 1458 * it is in (if any) and moved into this LRU. 1459 * 1460 * @lru: The LRU to move the object into. 1461 * @obj: The GEM object to move into this LRU 1462 */ 1463 void 1464 drm_gem_lru_move_tail(struct drm_gem_lru *lru, struct drm_gem_object *obj) 1465 { 1466 mutex_lock(lru->lock); 1467 drm_gem_lru_move_tail_locked(lru, obj); 1468 mutex_unlock(lru->lock); 1469 } 1470 EXPORT_SYMBOL(drm_gem_lru_move_tail); 1471 1472 /** 1473 * drm_gem_lru_scan - helper to implement shrinker.scan_objects 1474 * 1475 * If the shrink callback succeeds, it is expected that the driver 1476 * move the object out of this LRU. 1477 * 1478 * If the LRU possibly contain active buffers, it is the responsibility 1479 * of the shrink callback to check for this (ie. dma_resv_test_signaled()) 1480 * or if necessary block until the buffer becomes idle. 1481 * 1482 * @lru: The LRU to scan 1483 * @nr_to_scan: The number of pages to try to reclaim 1484 * @remaining: The number of pages left to reclaim, should be initialized by caller 1485 * @shrink: Callback to try to shrink/reclaim the object. 1486 * @ticket: Optional ww_acquire_ctx context to use for locking 1487 */ 1488 unsigned long 1489 drm_gem_lru_scan(struct drm_gem_lru *lru, 1490 unsigned int nr_to_scan, 1491 unsigned long *remaining, 1492 bool (*shrink)(struct drm_gem_object *obj, struct ww_acquire_ctx *ticket), 1493 struct ww_acquire_ctx *ticket) 1494 { 1495 struct drm_gem_lru still_in_lru; 1496 struct drm_gem_object *obj; 1497 unsigned freed = 0; 1498 1499 drm_gem_lru_init(&still_in_lru, lru->lock); 1500 1501 mutex_lock(lru->lock); 1502 1503 while (freed < nr_to_scan) { 1504 obj = list_first_entry_or_null(&lru->list, typeof(*obj), lru_node); 1505 1506 if (!obj) 1507 break; 1508 1509 drm_gem_lru_move_tail_locked(&still_in_lru, obj); 1510 1511 /* 1512 * If it's in the process of being freed, gem_object->free() 1513 * may be blocked on lock waiting to remove it. So just 1514 * skip it. 1515 */ 1516 if (!kref_get_unless_zero(&obj->refcount)) 1517 continue; 1518 1519 /* 1520 * Now that we own a reference, we can drop the lock for the 1521 * rest of the loop body, to reduce contention with other 1522 * code paths that need the LRU lock 1523 */ 1524 mutex_unlock(lru->lock); 1525 1526 if (ticket) 1527 ww_acquire_init(ticket, &reservation_ww_class); 1528 1529 /* 1530 * Note that this still needs to be trylock, since we can 1531 * hit shrinker in response to trying to get backing pages 1532 * for this obj (ie. while it's lock is already held) 1533 */ 1534 if (!ww_mutex_trylock(&obj->resv->lock, ticket)) { 1535 *remaining += obj->size >> PAGE_SHIFT; 1536 goto tail; 1537 } 1538 1539 if (shrink(obj, ticket)) { 1540 freed += obj->size >> PAGE_SHIFT; 1541 1542 /* 1543 * If we succeeded in releasing the object's backing 1544 * pages, we expect the driver to have moved the object 1545 * out of this LRU 1546 */ 1547 WARN_ON(obj->lru == &still_in_lru); 1548 WARN_ON(obj->lru == lru); 1549 } 1550 1551 dma_resv_unlock(obj->resv); 1552 1553 if (ticket) 1554 ww_acquire_fini(ticket); 1555 1556 tail: 1557 drm_gem_object_put(obj); 1558 mutex_lock(lru->lock); 1559 } 1560 1561 /* 1562 * Move objects we've skipped over out of the temporary still_in_lru 1563 * back into this LRU 1564 */ 1565 list_for_each_entry (obj, &still_in_lru.list, lru_node) 1566 obj->lru = lru; 1567 list_splice_tail(&still_in_lru.list, &lru->list); 1568 lru->count += still_in_lru.count; 1569 1570 mutex_unlock(lru->lock); 1571 1572 return freed; 1573 } 1574 EXPORT_SYMBOL(drm_gem_lru_scan); 1575 1576 /** 1577 * drm_gem_evict_locked - helper to evict backing pages for a GEM object 1578 * @obj: obj in question 1579 */ 1580 int drm_gem_evict_locked(struct drm_gem_object *obj) 1581 { 1582 dma_resv_assert_held(obj->resv); 1583 1584 if (!dma_resv_test_signaled(obj->resv, DMA_RESV_USAGE_READ)) 1585 return -EBUSY; 1586 1587 if (obj->funcs->evict) 1588 return obj->funcs->evict(obj); 1589 1590 return 0; 1591 } 1592 EXPORT_SYMBOL(drm_gem_evict_locked); 1593