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/shmem-helper: Use refcount_t for vmap_use_count

Use refcount_t helper for vmap_use_count to make refcounting consistent
with pages_use_count and pages_pin_count that use refcount_t. This also
makes vmapping to benefit from the refcount_t's overflow checks.

Acked-by: Maxime Ripard <mripard@kernel.org>
Reviewed-by: Boris Brezillon <boris.brezillon@collabora.com>
Suggested-by: Boris Brezillon <boris.brezillon@collabora.com>
Acked-by: Thomas Zimmermann <tzimmermann@suse.d>
Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20250322212608.40511-11-dmitry.osipenko@collabora.com

+16 -20
+12 -16
drivers/gpu/drm/drm_gem_shmem_helper.c
··· 165 165 } else { 166 166 dma_resv_lock(shmem->base.resv, NULL); 167 167 168 - drm_WARN_ON(obj->dev, shmem->vmap_use_count); 168 + drm_WARN_ON(obj->dev, refcount_read(&shmem->vmap_use_count)); 169 169 170 170 if (shmem->sgt) { 171 171 dma_unmap_sgtable(obj->dev->dev, shmem->sgt, ··· 355 355 356 356 dma_resv_assert_held(shmem->base.resv); 357 357 358 - if (shmem->vmap_use_count++ > 0) { 358 + if (refcount_inc_not_zero(&shmem->vmap_use_count)) { 359 359 iosys_map_set_vaddr(map, shmem->vaddr); 360 360 return 0; 361 361 } 362 362 363 363 ret = drm_gem_shmem_pin_locked(shmem); 364 364 if (ret) 365 - goto err_zero_use; 365 + return ret; 366 366 367 367 if (shmem->map_wc) 368 368 prot = pgprot_writecombine(prot); 369 369 shmem->vaddr = vmap(shmem->pages, obj->size >> PAGE_SHIFT, 370 370 VM_MAP, prot); 371 - if (!shmem->vaddr) 371 + if (!shmem->vaddr) { 372 372 ret = -ENOMEM; 373 - else 373 + } else { 374 374 iosys_map_set_vaddr(map, shmem->vaddr); 375 + refcount_set(&shmem->vmap_use_count, 1); 376 + } 375 377 } 376 378 377 379 if (ret) { ··· 386 384 err_put_pages: 387 385 if (!drm_gem_is_imported(obj)) 388 386 drm_gem_shmem_unpin_locked(shmem); 389 - err_zero_use: 390 - shmem->vmap_use_count = 0; 391 387 392 388 return ret; 393 389 } ··· 413 413 } else { 414 414 dma_resv_assert_held(shmem->base.resv); 415 415 416 - if (drm_WARN_ON_ONCE(obj->dev, !shmem->vmap_use_count)) 417 - return; 418 - 419 - if (--shmem->vmap_use_count > 0) 420 - return; 421 - 422 - vunmap(shmem->vaddr); 423 - drm_gem_shmem_unpin_locked(shmem); 416 + if (refcount_dec_and_test(&shmem->vmap_use_count)) { 417 + vunmap(shmem->vaddr); 418 + drm_gem_shmem_unpin_locked(shmem); 419 + } 424 420 } 425 421 426 422 shmem->vaddr = NULL; ··· 668 672 669 673 drm_printf_indent(p, indent, "pages_pin_count=%u\n", refcount_read(&shmem->pages_pin_count)); 670 674 drm_printf_indent(p, indent, "pages_use_count=%u\n", refcount_read(&shmem->pages_use_count)); 671 - drm_printf_indent(p, indent, "vmap_use_count=%u\n", shmem->vmap_use_count); 675 + drm_printf_indent(p, indent, "vmap_use_count=%u\n", refcount_read(&shmem->vmap_use_count)); 672 676 drm_printf_indent(p, indent, "vaddr=%p\n", shmem->vaddr); 673 677 } 674 678 EXPORT_SYMBOL_GPL(drm_gem_shmem_print_info);
+3 -3
drivers/gpu/drm/tests/drm_gem_shmem_test.c
··· 168 168 shmem = drm_gem_shmem_create(drm_dev, TEST_SIZE); 169 169 KUNIT_ASSERT_NOT_ERR_OR_NULL(test, shmem); 170 170 KUNIT_EXPECT_NULL(test, shmem->vaddr); 171 - KUNIT_EXPECT_EQ(test, shmem->vmap_use_count, 0); 171 + KUNIT_EXPECT_EQ(test, refcount_read(&shmem->vmap_use_count), 0); 172 172 173 173 ret = kunit_add_action_or_reset(test, drm_gem_shmem_free_wrapper, shmem); 174 174 KUNIT_ASSERT_EQ(test, ret, 0); ··· 177 177 KUNIT_ASSERT_EQ(test, ret, 0); 178 178 KUNIT_ASSERT_NOT_NULL(test, shmem->vaddr); 179 179 KUNIT_ASSERT_FALSE(test, iosys_map_is_null(&map)); 180 - KUNIT_EXPECT_EQ(test, shmem->vmap_use_count, 1); 180 + KUNIT_EXPECT_EQ(test, refcount_read(&shmem->vmap_use_count), 1); 181 181 182 182 iosys_map_memset(&map, 0, TEST_BYTE, TEST_SIZE); 183 183 for (i = 0; i < TEST_SIZE; i++) ··· 185 185 186 186 drm_gem_shmem_vunmap_locked(shmem, &map); 187 187 KUNIT_EXPECT_NULL(test, shmem->vaddr); 188 - KUNIT_EXPECT_EQ(test, shmem->vmap_use_count, 0); 188 + KUNIT_EXPECT_EQ(test, refcount_read(&shmem->vmap_use_count), 0); 189 189 } 190 190 191 191 /*
+1 -1
include/drm/drm_gem_shmem_helper.h
··· 82 82 * Reference count on the virtual address. 83 83 * The address are un-mapped when the count reaches zero. 84 84 */ 85 - unsigned int vmap_use_count; 85 + refcount_t vmap_use_count; 86 86 87 87 /** 88 88 * @pages_mark_dirty_on_put: