Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp909200rdb; Tue, 30 Jan 2024 02:11:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IGVR3XSmUWaoTu9aM6aI1PfdqGsEERHl4ubQPo5YdaeycJqoPjqzi0ooakoP7BDWu8yguTL X-Received: by 2002:a05:622a:1113:b0:42a:a6ff:82e5 with SMTP id e19-20020a05622a111300b0042aa6ff82e5mr3404017qty.34.1706609509204; Tue, 30 Jan 2024 02:11:49 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706609509; cv=pass; d=google.com; s=arc-20160816; b=jG0fXCFE8ql9C0fNICrn//AAGWvKfn7+WTR3wTN16Ezl1jH+PWFQz+Wol8jyxvPUJn 9PNhzzFtCvmaVeXel2b/94lzpUF8p+jbV0VlYINRJuL01ETWRg4Bq6yCth5Ai2a7BO02 yGZF1SOYjPvjnaE9wFeZ5xVAXDTk/RHfwZKhHdN1DMxLC+HjpKLhgSU+N3A7M8lS16pG V6ykI8NGHp3qk3Rl9zgPOXNch2ru+CWWbiSd7cza6lJchBaye6vRXUcnNcowXvmY/QVQ Z9T/xOXGz6oJwoYx3bPBY24/RC86G3kGjIVgs4JuPBKdBwt9Rue7AcWJLCbu3hBhtNPO zROA== ARC-Message-Signature: i=2; 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:organization:references :in-reply-to:message-id:subject:cc:to:from:date:dkim-signature; bh=yNdJCTQip+J9rS6N3WHJsqHYub18KBJIo/QQK/Uv7xA=; fh=YRbtOGJj27IcZTpfa2kmsNwRQgyCW2hxMiEIrmCqiJY=; b=t1DjClMziJbYAcy2+B7IVJTOmk24+ihaXPsmvVHwgjT5SNufuOPl0SWor+8BTgdLS4 XpdcR3cD0yQd2qApHZon3hnakLZ8qCXI4y6/aT/A8TX4sj7kopVOqXBX6SeE3vo8qgWx coay7zHuXktK0VSJJ7QuVDJQ8zikJtoU0/8oRCxMCjNODmnqGVEGG3uHzyAgxQmfoOY7 alOjAmu8pof7Kd4XOtY4zsPBNF2Eu1C3bSqa8QG5fWaUw6hNEtWjHZmhMiHar9r+Vi1F Wk0NVaslJjY14qKQT/1KBD05T7SMzZlw1va3rEKlsIE8nMB3aUgNeikn+OQ99zCsgVpz 4s7Q== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=Bwt9Vxx7; 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-44358-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-44358-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id e5-20020a05622a110500b0042ac5d58b73si698878qty.389.2024.01.30.02.11.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 02:11:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-44358-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=Bwt9Vxx7; 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-44358-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-44358-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id F1F0E1C25D4A for ; Tue, 30 Jan 2024 10:10:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D45C860884; Tue, 30 Jan 2024 10:10:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="Bwt9Vxx7" 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 618BE605B4 for ; Tue, 30 Jan 2024 10:10:38 +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=1706609440; cv=none; b=NQPbIFIXFzKJtfOPTrREsF17FE+MySqut90E1qxNgjqVegQeAI4oA64Vl5frj14LcnqT/tOhMIYyIuI4tbml9089pQHxcyNktqes02NaLXouh6nPKu/s9kt1S7pcgOAOCkbAWdRfT3NHkI7DhakFbi0nT8TvyA6rKrdZQHjUouA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706609440; c=relaxed/simple; bh=pvTVYZQvoWE9TYRSfVKQhlX6lvtPW1b9pCFx1Xl/zbk=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=OtmsNn351yqPxOttVyJx+evAgEaVu/eqibAeE+O77gYLLqVaOBMKPve/hws9uPcc1ucNFpl8R3Ca1Px9Zd1R6W1C79oeqk9DVBxWGQuFub/q2A6bgK3fexFzWsedAL4YeCRUuNUxLmw3s+Dqx7VAYmsyzdep8sga/T6UD2wj7xs= 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=Bwt9Vxx7; 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=1706609436; bh=pvTVYZQvoWE9TYRSfVKQhlX6lvtPW1b9pCFx1Xl/zbk=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=Bwt9Vxx7XAVrOtbWrBb7G+/IYQ+vog5Ze2jE+wOBgZQqlI16QYW4m05otZWBw0KP4 Ttk7ggDeV4kKwQo5xwNFYvHFY/3i6ODDbOIHxDzOi8W2QiQmK1q9k+iApJq8PHTvac paw++6waKYNiQP3WNaq4FUI8O5zXgRcDvaBsLSH/hbPV4QLmTNLK8Mu0kim5lw1ogQ 5iSnDI2LlwRH6mzYyj6nAUfwWwgtAsgO9EOmtyUnFRUqThSQR/uPEqfYh6BCtbgxGn JfUXMxjE+RcNb2GlDKzpPHOpQK+fA3NnZx7GfJE/yPO2xRdzDq1hx5PqnHWTAaZ9HQ /DExCDuRUIJIA== Received: from localhost (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: bbrezillon) by madrid.collaboradmins.com (Postfix) with ESMTPSA id 4F68D3782079; Tue, 30 Jan 2024 10:10:35 +0000 (UTC) Date: Tue, 30 Jan 2024 11:10:33 +0100 From: Boris Brezillon To: Daniel Vetter Cc: Dmitry Osipenko , David Airlie , Gerd Hoffmann , Gurchetan Singh , Chia-I Wu , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Christian =?UTF-8?B?S8O2bmln?= , 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 Subject: Re: [PATCH v19 09/30] drm/shmem-helper: Add and use lockless drm_gem_shmem_get_pages() Message-ID: <20240130111033.5c01054b@collabora.com> In-Reply-To: References: <20240105184624.508603-1-dmitry.osipenko@collabora.com> <20240105184624.508603-10-dmitry.osipenko@collabora.com> <20240126111827.70f8726c@collabora.com> Organization: Collabora X-Mailer: Claws Mail 4.2.0 (GTK 3.24.38; x86_64-redhat-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Tue, 30 Jan 2024 09:34:29 +0100 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. Yeah, I also considered adding a dedicated function for that use case at some point, instead of abusing get_pages(). Given I no longer need it, we can probably add this might_lock() and defer the addition of this get_borrowed_pages() helper until someone actually needs it. > > 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 :-/ Totally agree with you on that point.