Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp907668rdb; Tue, 30 Jan 2024 02:08:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IHPUz2cUeFJroLRutNYA5KVw83zxkwu5X/Lut9gsqceXUAxh6vGIhkPDA3TrdlV+UMonYx4 X-Received: by 2002:a17:906:6bd5:b0:a35:a1dc:8920 with SMTP id t21-20020a1709066bd500b00a35a1dc8920mr4240798ejs.45.1706609292804; Tue, 30 Jan 2024 02:08:12 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706609292; cv=pass; d=google.com; s=arc-20160816; b=uyg17soIj1l+V2KbanEjDLYwSTv3I9QeCyEr8/B2N+6ZDz7esQWJqDwV1qqzsrrYfx HB6hC4oGEEkPh7Uez/tWJzAjVyqPZH49A0GxCnJWPnal2BEQy/7Tm1koXpagigqk3ux1 dKz/Sy5CcpEg7VrWV1uk+qPKBGs6afylG5mM2oGW1z3Q0bVbJhCkYlB3O5TGl52EZEKJ M2cCErbWqXfQurTUM0be4Ogn1JQoskM+zBHLTpQg+LR/R26xlVQIGFCT9IGGZC9vF1H6 0e6LkwABzz76Q4SiJ8IaEdzmWaIe8uzarMdQHBmSZqEgH8t+y1jzkMgCPQjW1xDmRH8N pm0w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:cc:from:references:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=EAswapnMvxatxz2Sor4uFwfWYCA/62D7ZEvR2JZ/3lY=; fh=mUS0I9sd7n8PQb4RDUIMUCm88ey1ZFqYqCJXuio7Evs=; b=OahkCJzaDoqTDUC1EOgbCx3zq7eRLVTXhLe2oc6EAr2/AMpaQytpr3uS4WM+Dhdsnb jiVGH1yi0o68E6R4WhKJT4x7lT1XlmHOEwy8PKzBqJ/9NM9iojLX88wp/15R/YKN9jss /W+6t19Rqz8uet4Yt5j3VbhSEJ9uQRqpc464fuJJ2YhMJxl8gt4ZAR5Nr/PEf97RKVY8 zDdn+70NUNGNWuPa9jOuL/+xdHiXfpoPoAuu6CZSN6HHxFOhlRlntynJwVs97jPUyD2s 9E8PjNiv8x5OXvnB3m7ZjIlX6rCAiQ85KuplFI0dPAEX33nEYAGm8LZP1GF3tt+m+vQn 8pSQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=wlMvbztG; arc=pass (i=1 spf=pass spfdomain=collabora.com dkim=pass dkdomain=collabora.com dmarc=pass fromdomain=collabora.com); spf=pass (google.com: domain of linux-kernel+bounces-44353-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-44353-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id g23-20020a17090613d700b00a35a7bac64csi2047361ejc.995.2024.01.30.02.08.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 02:08:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-44353-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=wlMvbztG; arc=pass (i=1 spf=pass spfdomain=collabora.com dkim=pass dkdomain=collabora.com dmarc=pass fromdomain=collabora.com); spf=pass (google.com: domain of linux-kernel+bounces-44353-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-44353-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 4B1221F2CFEF for ; Tue, 30 Jan 2024 10:07:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 87C68664C9; Tue, 30 Jan 2024 10:05:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="wlMvbztG" 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 200ED664A2 for ; Tue, 30 Jan 2024 10:05:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=46.235.227.194 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706609148; cv=none; b=EKdpiLIWnvMoKm8fxBFrgwf2KuBj2P2ZfqUjoDHqzc1d7lcREVD3+G+FXJMnYmDgIl26HzFFnjW4LA4GXvEu7KxUKD3guxC7y7AcKyqMwZMD7mJ+g1ylt9LNdES1FdzL9vYohjuWpaOsBWk21xIHaFrRIlwm+UnNIoXMGD3E648= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706609148; c=relaxed/simple; bh=D+HHg4Wbbhz/mbS+lLu+znnL2uXDODc+rW4Ur5A3ss0=; h=Message-ID:Date:MIME-Version:Subject:To:References:From:Cc: In-Reply-To:Content-Type; b=qiQMtSIpeiwg8eqnDMn77mk56wtD7fO3zKjmI0BH6k2Dr28IfA19n5fA2lxc3dYkED4YMb+z1hnP74uEprT3hxCQDVJ6i6r/ZheUgWzyiX98cUViwHTKmGyUZUq4oum/d/c2WLBpwcInMfvjRuFTDmguq8dA5t91nkXfaF2DVmM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b=wlMvbztG; arc=none smtp.client-ip=46.235.227.194 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=1706609145; bh=D+HHg4Wbbhz/mbS+lLu+znnL2uXDODc+rW4Ur5A3ss0=; h=Date:Subject:To:References:From:Cc:In-Reply-To:From; b=wlMvbztGA0yA512ZlKIRcB6nX3f7p5ByeRR8ZKN+eBh2AixDxN3QzbFDOPyla6deg glnDFEENKA5QsselWolE9MYhuP1Qj+RwLtmnTxtVNZPdPEXxcr8UvCFI1K3s6Su80t TAiCRT6ybMXGiOKjNIBRDIHxOyXYMpxKq+RQ6j5kZNescOUzIGu45GpLxLh3eO6btZ kagAZmX+p6yCW+om2i8uZL3Z+eUBWY43f1xd81QM5i6EchBV773GHaMhRnoFORPjPn rVAEC5ZsunT5LVdbOkhr0VEo9yGS1SLZtSAwRo4QddYkNZcodEDgOyTKN0FfKYFxmd QGw1T3YvU+kfA== Received: from [100.109.49.129] (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 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 2B04437809D0; Tue, 30 Jan 2024 10:05:44 +0000 (UTC) Message-ID: <7b284de7-f8e9-4b5a-96c5-a4e26193cd90@collabora.com> Date: Tue, 30 Jan 2024 13:05:44 +0300 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v19 09/30] drm/shmem-helper: Add and use lockless drm_gem_shmem_get_pages() Content-Language: en-US To: Boris Brezillon , Daniel Vetter References: <20240105184624.508603-1-dmitry.osipenko@collabora.com> <20240105184624.508603-10-dmitry.osipenko@collabora.com> <20240126111827.70f8726c@collabora.com> From: Dmitry Osipenko Cc: David Airlie , Gerd Hoffmann , Gurchetan Singh , Chia-I Wu , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , =?UTF-8?Q?Christian_K=C3=B6nig?= , Qiang Yu , Steven Price , Emma Anholt , Melissa Wen , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, kernel@collabora.com, virtualization@lists.linux-foundation.org In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 1/30/24 11:34, Daniel Vetter wrote: > On Fri, Jan 26, 2024 at 07:43:29PM +0300, Dmitry Osipenko wrote: >> On 1/26/24 13:18, Boris Brezillon wrote: >>> On Thu, 25 Jan 2024 18:24:04 +0100 >>> Daniel Vetter wrote: >>> >>>> On Fri, Jan 05, 2024 at 09:46:03PM +0300, Dmitry Osipenko wrote: >>>>> Add lockless drm_gem_shmem_get_pages() helper that skips taking reservation >>>>> lock if pages_use_count is non-zero, leveraging from atomicity of the >>>>> refcount_t. Make drm_gem_shmem_mmap() to utilize the new helper. >>>>> >>>>> 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 | 19 +++++++++++++++---- >>>>> 1 file changed, 15 insertions(+), 4 deletions(-) >>>>> >>>>> diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c >>>>> index cacf0f8c42e2..1c032513abf1 100644 >>>>> --- a/drivers/gpu/drm/drm_gem_shmem_helper.c >>>>> +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c >>>>> @@ -226,6 +226,20 @@ void drm_gem_shmem_put_pages_locked(struct drm_gem_shmem_object *shmem) >>>>> } >>>>> EXPORT_SYMBOL_GPL(drm_gem_shmem_put_pages_locked); >>>>> >>>>> +static int drm_gem_shmem_get_pages(struct drm_gem_shmem_object *shmem) >>>>> +{ >>>>> + int ret; >>>> >>>> Just random drive-by comment: a might_lock annotation here might be good, >>>> or people could hit some really interesting bugs that are rather hard to >>>> reproduce ... >>> >>> Actually, being able to acquire a ref in a dma-signalling path on an >>> object we know for sure already has refcount >= 1 (because we previously >>> acquired a ref in a path where dma_resv_lock() was allowed), was the >>> primary reason I suggested moving to this atomic-refcount approach. >>> >>> In the meantime, drm_gpuvm has evolved in a way that allows me to not >>> take the ref in the dma-signalling path (the gpuvm_bo object now holds >>> the ref, and it's acquired/released outside the dma-signalling path). >>> >>> Not saying we shouldn't add this might_lock(), but others might have >>> good reasons to have this function called in a path where locking >>> is not allowed. >> >> For Panthor the might_lock indeed won't be a appropriate, thanks for >> reminding about it. I'll add explanatory comment to the code. > > Hm these kind of tricks feel very dangerous to me. I think it would be > good to split up the two cases into two functions: > > 1. first one does only the atomic_inc and splats if the refcount is zero. > I think something in the name that denotes that we're incrementing a > borrowed pages reference would be good here, so like get_borrowed_pages > (there's not really a naming convention for these in the kernel). > Unfortunately no rust so we can't enforce that you provide the right kind > of borrowed reference at compile time. > > 2. second one has the might_lock. > > This way you force callers to think what they're doing and ideally > document where the borrowed reference is from, and ideally document that > in the code. Otherwise we'll end up with way too much "works in testing, > but is a nice CVE" code :-/ We indeed can have both variants of the borrowed/non-borrowed functions. Thanks again for the suggestions -- Best regards, Dmitry