Received: by 2002:a05:7412:98c1:b0:fa:551:50a7 with SMTP id kc1csp319441rdb; Fri, 5 Jan 2024 10:50:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IHj26Ouzj+Ha3iyWVA2NtyVpvRX+/3YmTL2C/WyFn5W0HI95C98+smUqi9C9jUaPrZ80t8Q X-Received: by 2002:a05:6a21:8188:b0:199:1c2:4760 with SMTP id pd8-20020a056a21818800b0019901c24760mr2198472pzb.108.1704480605636; Fri, 05 Jan 2024 10:50:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704480605; cv=none; d=google.com; s=arc-20160816; b=JFs0SmCDpA7puAUb9kd0Kmzv47C/WVtGpPJWx7l/Rr1nrsDp/p9geIoP47/NZRHjNc SOexxqmslfZxEjTOTjB/z+I5zjnnbcyqAV0sPDwhpViEC07mip6fzwgKIWXiO5072fqs EvvYCIFRb7IKjdz/6Xul3JRLIqjxp572U7x2zyxCzo2k6EsadAYPkVVlmJJ8cBQgxRfH BMHNAqQwtaYVxBpwkuajjcvwOenwYkzkmcF7XNxIN2Q8M6j1HvjxwHT42H1gLr2EE4Jl 12kJdnvYXCQpzpzDyQeu2N4OV4pMdrFna24B47DrJamUrwxdYfApMtwWIhdaBtbPMoa+ BrTA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=ldn6U+QkjiO92IE1pxFf2IiYz/CqWGXQ7W6zh9pZv7c=; fh=5cel2jD5h+yPMXVwxbomyVhwojHUqATy6nFjd4aOh4o=; b=xk+bZ3yriZFvrSUEaiVJu2FDeU3igSF+8Cp4OUIA8HW+jB4zzVxlchJ7Ty7cqUIgfj a6TFA7aVAADX0tV7SOmnRmTCahoa/5sAa8R5XawT2LoDJbiddqXvso03fayJtzL4ZS20 2bK5/zbitlYE5Fx1/Ssbv5yFj7I1sq9JefBVOXM6bJzkNE5OIRL2t6Gzzzg6Mw+njNxh AmTK7hUwzX0l7pZLr5g/+bRkFwUa1q0ldJgTSG0Y+nX2bHwpXckElPRWgljTII8u/LBe H9gd8c71yBpGpQPdpgujPkZ3mCTo/HDz1Ob31Ksc/u0exMPK70vhUJXt86H8+IZGRxsd BJ0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=gn2LuCh9; spf=pass (google.com: domain of linux-kernel+bounces-18249-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-18249-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id g8-20020a63e608000000b005cdfc01dcc9si1669567pgh.245.2024.01.05.10.50.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Jan 2024 10:50:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-18249-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=gn2LuCh9; spf=pass (google.com: domain of linux-kernel+bounces-18249-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-18249-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id A1994281EA3 for ; Fri, 5 Jan 2024 18:49:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0AE5B381AE; Fri, 5 Jan 2024 18:47:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="gn2LuCh9" X-Original-To: linux-kernel@vger.kernel.org Received: from madrid.collaboradmins.com (madrid.collaboradmins.com [46.235.227.194]) (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 D5B1C381A9 for ; Fri, 5 Jan 2024 18:47:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1704480422; bh=v5+cngbX/04V7Dxe+pQvc9AS8RouTUe4vyh9L/4x71M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gn2LuCh99bU9HukS0JPvUM/SdGxVuLu3KG4B7p//6T/6Xd51HI/0cFW4FI1SXu+zG lBKM/u481FWqOztXhOl27xbk+N3ZMDtw0dgrgfVk3eeWglaD4CSVKW3WtLc9eelfs3 sTU+w7wdIU9BKVVQxivG7C5YwJraSKen5F9pZ+ZwVdlmKnjwoCsSCNdFAPl21Fm/WL E+wkHc6aUCc++UroSIvJ5SqjMBoYOeYwhV6efG1J+SKH6pERamCmE9J1nVcXLCZAyD t17fH9FSdicgXK5TPH8udebWxykN4ir8jp8+cNuKNFv55NNzvsMDVkKQnhfijKjfL2 oQfDirfL6w82A== Received: from workpc.. (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: dmitry.osipenko) by madrid.collaboradmins.com (Postfix) with ESMTPSA id C5BBA3782049; Fri, 5 Jan 2024 18:47:00 +0000 (UTC) From: Dmitry Osipenko To: David Airlie , Gerd Hoffmann , Gurchetan Singh , Chia-I Wu , Daniel Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , =?UTF-8?q?Christian=20K=C3=B6nig?= , Qiang Yu , Steven Price , Boris Brezillon , Emma Anholt , Melissa Wen Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, kernel@collabora.com, virtualization@lists.linux-foundation.org Subject: [PATCH v19 11/30] drm/shmem-helper: Use refcount_t for vmap_use_count Date: Fri, 5 Jan 2024 21:46:05 +0300 Message-ID: <20240105184624.508603-12-dmitry.osipenko@collabora.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240105184624.508603-1-dmitry.osipenko@collabora.com> References: <20240105184624.508603-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: 8bit 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 +++++++++++--------------- include/drm/drm_gem_shmem_helper.h | 2 +- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c index 9c89183f81b7..3403700780c3 100644 --- a/drivers/gpu/drm/drm_gem_shmem_helper.c +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c @@ -144,7 +144,7 @@ void drm_gem_shmem_free(struct drm_gem_shmem_object *shmem) } else { dma_resv_lock(shmem->base.resv, NULL); - drm_WARN_ON(obj->dev, shmem->vmap_use_count); + drm_WARN_ON(obj->dev, refcount_read(&shmem->vmap_use_count)); if (shmem->sgt) { dma_unmap_sgtable(obj->dev->dev, shmem->sgt, @@ -344,23 +344,25 @@ int drm_gem_shmem_vmap_locked(struct drm_gem_shmem_object *shmem, dma_resv_assert_held(shmem->base.resv); - if (shmem->vmap_use_count++ > 0) { + if (refcount_inc_not_zero(&shmem->vmap_use_count)) { iosys_map_set_vaddr(map, shmem->vaddr); return 0; } ret = drm_gem_shmem_pin_locked(shmem); if (ret) - goto err_zero_use; + return ret; if (shmem->map_wc) prot = pgprot_writecombine(prot); shmem->vaddr = vmap(shmem->pages, obj->size >> PAGE_SHIFT, VM_MAP, prot); - if (!shmem->vaddr) + if (!shmem->vaddr) { ret = -ENOMEM; - else + } else { iosys_map_set_vaddr(map, shmem->vaddr); + refcount_set(&shmem->vmap_use_count, 1); + } } if (ret) { @@ -373,8 +375,6 @@ int drm_gem_shmem_vmap_locked(struct drm_gem_shmem_object *shmem, err_put_pages: if (!obj->import_attach) drm_gem_shmem_unpin_locked(shmem); -err_zero_use: - shmem->vmap_use_count = 0; return ret; } @@ -402,14 +402,10 @@ void drm_gem_shmem_vunmap_locked(struct drm_gem_shmem_object *shmem, } else { dma_resv_assert_held(shmem->base.resv); - 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); + } } shmem->vaddr = NULL; @@ -655,7 +651,7 @@ void drm_gem_shmem_print_info(const struct drm_gem_shmem_object *shmem, drm_printf_indent(p, indent, "pages_pin_count=%u\n", refcount_read(&shmem->pages_pin_count)); drm_printf_indent(p, indent, "pages_use_count=%u\n", refcount_read(&shmem->pages_use_count)); - drm_printf_indent(p, indent, "vmap_use_count=%u\n", shmem->vmap_use_count); + drm_printf_indent(p, indent, "vmap_use_count=%u\n", refcount_read(&shmem->vmap_use_count)); drm_printf_indent(p, indent, "vaddr=%p\n", shmem->vaddr); } EXPORT_SYMBOL_GPL(drm_gem_shmem_print_info); diff --git a/include/drm/drm_gem_shmem_helper.h b/include/drm/drm_gem_shmem_helper.h index 80623b897803..18020f653d7e 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; /** * @pages_mark_dirty_on_put: -- 2.43.0