From nobody Tue Feb 10 10:04:46 2026 Received: from sender4-pp-f112.zoho.com (sender4-pp-f112.zoho.com [136.143.188.112]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BECD61BDCF for ; Sat, 22 Mar 2025 21:30:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=136.143.188.112 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742679005; cv=pass; b=Jw8RszpgdhpeJ3tpTAh2XLu0yMjIFYi6NHfGawAFrk8oSq0T3nGztuUZcmoOQspYGm9OHd/+nDJSZAsIXxqfzTXB/RLDv8iVZdgscDxd/sFHaeO5rt0O/A3URgcYM2RVptvfIVgwvLfiwOQ+rEtXDhqw3kbezy+/6BC+zgI1HsE= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742679005; c=relaxed/simple; bh=5sL5JhRTwA5zkil10rVngMTpdoFtwEQs5zgqi4b6ndk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HtxCO13c6hxByY1hbCobX0l+kO/Aa4ynlxFOXtd8Sc+N/o5KAI4LBvkBTTp+ef9SNFrkp7nfbD1MDjlytlJyQC+uAIPTYPPb7SgRqbYe/j85O8ePy1ZKHggGI8hksx5CdOwNCAckMKVY+jd6m1JjDvsPOHt8okPMS5FzlgtfY4E= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (1024-bit key) header.d=collabora.com header.i=dmitry.osipenko@collabora.com header.b=cKpXo8eB; arc=pass smtp.client-ip=136.143.188.112 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=collabora.com header.i=dmitry.osipenko@collabora.com header.b="cKpXo8eB" ARC-Seal: i=1; a=rsa-sha256; t=1742678986; cv=none; d=zohomail.com; s=zohoarc; b=WaJyEL9xZeU5zR4pgHILJlFQFZmUTtzc4hg6lx/PXbWT4I9jrv7pM40vpdVr+pZgLAeUb/nB62lQLYFm06BYCzmulIxWoEsITQ9BXeRLHrmjN62d9KhroTI6K5WGtANV8rjdxJLd3gSyarEUprS8jJvzOIXmIRtGjoT+sEyC0pU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1742678986; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:References:Subject:Subject:To:To:Message-Id:Reply-To; bh=jI2A6McB5nUab55GDN4/MJXKSOUMSiNkiu8ByBqPMPM=; b=eGtiThTAXaS4Rt47A4xIkV/+OVQYNyNC1cPlUQXjV5rsweVi4unRJrNTs4M6jHhTVawtNVJZyxHAVudC4kFFs4ENk29Imx6SqzfeY8byrhCY2yG1X34Y40jLqVl3C1w+tnMV1HvdUc+7oz1EWKC/ufjvo+iEm2IQeAWFhJVYMW8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=dmitry.osipenko@collabora.com; dmarc=pass header.from= DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1742678986; s=zohomail; d=collabora.com; i=dmitry.osipenko@collabora.com; h=From:From:To:To:Cc:Cc:Subject:Subject:Date:Date:Message-ID:In-Reply-To:References:MIME-Version:Content-Transfer-Encoding:Message-Id:Reply-To; bh=jI2A6McB5nUab55GDN4/MJXKSOUMSiNkiu8ByBqPMPM=; b=cKpXo8eBxoYzi7gEo4QhSot4SporbIAkd8H4e2AB5UtPtPnHLRFt11XciaDf12DI qdT0AZl9NSF1oM+KQutMmO/4V/DptjOAsYd9Nbk8r6SpucaDhT41BO7ylAIVRm8K7Li I7181BxmAsVXSBo/4VeWRjFbKunvKd2E/7znELSU= Received: by mx.zohomail.com with SMTPS id 1742678984353357.2761177607125; Sat, 22 Mar 2025 14:29:44 -0700 (PDT) From: Dmitry Osipenko To: David Airlie , Simona Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , =?UTF-8?q?Christian=20K=C3=B6nig?= , Gerd Hoffmann , Qiang Yu , Steven Price , Boris Brezillon , Frank Binns , Matt Coster Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, kernel@collabora.com Subject: [PATCH v20 10/10] drm/shmem-helper: Use refcount_t for vmap_use_count Date: Sun, 23 Mar 2025 00:26:08 +0300 Message-ID: <20250322212608.40511-11-dmitry.osipenko@collabora.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250322212608.40511-1-dmitry.osipenko@collabora.com> References: <20250322212608.40511-1-dmitry.osipenko@collabora.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMailClient: External Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Boris Brezillon Suggested-by: Boris Brezillon Signed-off-by: Dmitry Osipenko --- drivers/gpu/drm/drm_gem_shmem_helper.c | 28 ++++++++++------------ drivers/gpu/drm/tests/drm_gem_shmem_test.c | 6 ++--- include/drm/drm_gem_shmem_helper.h | 2 +- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_g= em_shmem_helper.c index 84a196bbe44f..2d924d547a51 100644 --- a/drivers/gpu/drm/drm_gem_shmem_helper.c +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c @@ -165,7 +165,7 @@ void drm_gem_shmem_free(struct drm_gem_shmem_object *sh= mem) } else { dma_resv_lock(shmem->base.resv, NULL); =20 - drm_WARN_ON(obj->dev, shmem->vmap_use_count); + drm_WARN_ON(obj->dev, refcount_read(&shmem->vmap_use_count)); =20 if (shmem->sgt) { dma_unmap_sgtable(obj->dev->dev, shmem->sgt, @@ -355,23 +355,25 @@ int drm_gem_shmem_vmap_locked(struct drm_gem_shmem_ob= ject *shmem, =20 dma_resv_assert_held(shmem->base.resv); =20 - if (shmem->vmap_use_count++ > 0) { + if (refcount_inc_not_zero(&shmem->vmap_use_count)) { iosys_map_set_vaddr(map, shmem->vaddr); return 0; } =20 ret =3D drm_gem_shmem_pin_locked(shmem); if (ret) - goto err_zero_use; + return ret; =20 if (shmem->map_wc) prot =3D pgprot_writecombine(prot); shmem->vaddr =3D vmap(shmem->pages, obj->size >> PAGE_SHIFT, VM_MAP, prot); - if (!shmem->vaddr) + if (!shmem->vaddr) { ret =3D -ENOMEM; - else + } else { iosys_map_set_vaddr(map, shmem->vaddr); + refcount_set(&shmem->vmap_use_count, 1); + } } =20 if (ret) { @@ -384,8 +386,6 @@ int drm_gem_shmem_vmap_locked(struct drm_gem_shmem_obje= ct *shmem, err_put_pages: if (!drm_gem_is_imported(obj)) drm_gem_shmem_unpin_locked(shmem); -err_zero_use: - shmem->vmap_use_count =3D 0; =20 return ret; } @@ -413,14 +413,10 @@ void drm_gem_shmem_vunmap_locked(struct drm_gem_shmem= _object *shmem, } else { dma_resv_assert_held(shmem->base.resv); =20 - if (drm_WARN_ON_ONCE(obj->dev, !shmem->vmap_use_count)) - return; - - if (--shmem->vmap_use_count > 0) - return; - - vunmap(shmem->vaddr); - drm_gem_shmem_unpin_locked(shmem); + if (refcount_dec_and_test(&shmem->vmap_use_count)) { + vunmap(shmem->vaddr); + drm_gem_shmem_unpin_locked(shmem); + } } =20 shmem->vaddr =3D NULL; @@ -672,7 +668,7 @@ void drm_gem_shmem_print_info(const struct drm_gem_shme= m_object *shmem, =20 drm_printf_indent(p, indent, "pages_pin_count=3D%u\n", refcount_read(&shm= em->pages_pin_count)); drm_printf_indent(p, indent, "pages_use_count=3D%u\n", refcount_read(&shm= em->pages_use_count)); - drm_printf_indent(p, indent, "vmap_use_count=3D%u\n", shmem->vmap_use_cou= nt); + drm_printf_indent(p, indent, "vmap_use_count=3D%u\n", refcount_read(&shme= m->vmap_use_count)); drm_printf_indent(p, indent, "vaddr=3D%p\n", shmem->vaddr); } EXPORT_SYMBOL_GPL(drm_gem_shmem_print_info); diff --git a/drivers/gpu/drm/tests/drm_gem_shmem_test.c b/drivers/gpu/drm/t= ests/drm_gem_shmem_test.c index 1459cdb0c413..81cadaecdd4f 100644 --- a/drivers/gpu/drm/tests/drm_gem_shmem_test.c +++ b/drivers/gpu/drm/tests/drm_gem_shmem_test.c @@ -168,7 +168,7 @@ static void drm_gem_shmem_test_vmap(struct kunit *test) shmem =3D drm_gem_shmem_create(drm_dev, TEST_SIZE); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, shmem); KUNIT_EXPECT_NULL(test, shmem->vaddr); - KUNIT_EXPECT_EQ(test, shmem->vmap_use_count, 0); + KUNIT_EXPECT_EQ(test, refcount_read(&shmem->vmap_use_count), 0); =20 ret =3D kunit_add_action_or_reset(test, drm_gem_shmem_free_wrapper, shmem= ); KUNIT_ASSERT_EQ(test, ret, 0); @@ -177,7 +177,7 @@ static void drm_gem_shmem_test_vmap(struct kunit *test) KUNIT_ASSERT_EQ(test, ret, 0); KUNIT_ASSERT_NOT_NULL(test, shmem->vaddr); KUNIT_ASSERT_FALSE(test, iosys_map_is_null(&map)); - KUNIT_EXPECT_EQ(test, shmem->vmap_use_count, 1); + KUNIT_EXPECT_EQ(test, refcount_read(&shmem->vmap_use_count), 1); =20 iosys_map_memset(&map, 0, TEST_BYTE, TEST_SIZE); for (i =3D 0; i < TEST_SIZE; i++) @@ -185,7 +185,7 @@ static void drm_gem_shmem_test_vmap(struct kunit *test) =20 drm_gem_shmem_vunmap_locked(shmem, &map); KUNIT_EXPECT_NULL(test, shmem->vaddr); - KUNIT_EXPECT_EQ(test, shmem->vmap_use_count, 0); + KUNIT_EXPECT_EQ(test, refcount_read(&shmem->vmap_use_count), 0); } =20 /* diff --git a/include/drm/drm_gem_shmem_helper.h b/include/drm/drm_gem_shmem= _helper.h index 8b9bba87ae63..b4f993da3cae 100644 --- a/include/drm/drm_gem_shmem_helper.h +++ b/include/drm/drm_gem_shmem_helper.h @@ -82,7 +82,7 @@ struct drm_gem_shmem_object { * Reference count on the virtual address. * The address are un-mapped when the count reaches zero. */ - unsigned int vmap_use_count; + refcount_t vmap_use_count; =20 /** * @pages_mark_dirty_on_put: --=20 2.49.0