Received: by 2002:a05:6358:701b:b0:131:369:b2a3 with SMTP id 27csp4282661rwo; Tue, 25 Jul 2023 03:51:05 -0700 (PDT) X-Google-Smtp-Source: APBJJlEZErFH+Iig5ULVjm8qBmRejBEVX43AbQibPvtFDmv2kApH/5JqcogkcAKm/5qTB0Z/c4rZ X-Received: by 2002:a17:907:7714:b0:99b:574f:d201 with SMTP id kw20-20020a170907771400b0099b574fd201mr13104121ejc.40.1690282264934; Tue, 25 Jul 2023 03:51:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690282264; cv=none; d=google.com; s=arc-20160816; b=O+u/iFJPXBJDdbECUGdE+oL6XELPiGQ0Fo41Btv83OmkzgDfceWlnWP8LsklK/uEqi pwZyzYFKZYEcj5+f13WY8cNqa8rzEtA3BBpt28jG1xzLAtJ5NX67c9f5il3mjWxEedYD uoHK2d8sK0G+SAwBGPY3k1F8JHP6Ly4sQ2w/3yMWpmfxH/tciMdGRzYxh8xx7AiFmN6Y XI0JZW6FNNIHOya2sF2C1VIc6U65gqoELcJzwAuO0K9HrhqEFZ4X5gumT0iyX4CMP2p7 4sZG0y34f6DSR3N93ioxkFqr9LldnF3DXqyroHHNUVKt+j3cTlrPxJGrey5ozINM7aEP DbRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :organization:references:in-reply-to:message-id:subject:cc:to:from :date:dkim-signature; bh=g3i0gWwaeVQWrkGGeBoO8hPQzWRvTvrj8KpzzlvkEJE=; fh=+PArvOrmmh0iVc/5XGads6zY/5UXlaaY8lrP6PsZ1cY=; b=cg5yQOGPGM4PmPImtnckcPgP2kHC/aAQChGPwlCU1n400Z7/dMUrGdAG8HgXQOXXdH 8lq/TgfQEX5YKazsRB5R8oxR2XSLGtBN6CPlgpRHDMlEHYfPGSYnxybZkP2x2aOvGn99 Fdc+cQMVk0JeNbzWxDFne55PjNay+HiClOJIlFM/rt1OeUBhlCz4mNGHzUophHET9nJm bl7x0PKOrRrLL3B47BbVk0cw8gVLLmr6W93JOzhXztlMDqO9YM56p7FbHHvfehqcRZ7v H/tR7ch0FKkhv4Y7AXRV6TBs/TBwgNilDGN5X2TigdB4y17x5wPirzqc7v8uVre7eJNZ VdHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=SqAm7vrv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d27-20020a170906175b00b00991df467bafsi8193698eje.617.2023.07.25.03.50.40; Tue, 25 Jul 2023 03:51:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=SqAm7vrv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232245AbjGYIcl (ORCPT + 99 others); Tue, 25 Jul 2023 04:32:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53522 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229930AbjGYIck (ORCPT ); Tue, 25 Jul 2023 04:32:40 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA1D3A9 for ; Tue, 25 Jul 2023 01:32:38 -0700 (PDT) Received: from localhost (unknown [IPv6:2a01:e0a:2c:6930:5cf4:84a1:2763:fe0d]) (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 madras.collabora.co.uk (Postfix) with ESMTPSA id A006D6607108; Tue, 25 Jul 2023 09:32:36 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1690273957; bh=fYsygSwR3iu7mxcj3ju5lzqiPYT3fINyEw+FYMQafkA=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=SqAm7vrvN+PEUv3NXhBV9qAufLRUwnMHxPkR/zPcTMYTi0rDj+BGxjPkT24pvyvNq 86yrtnkmqodrtGKOry1iRuEckcwqkOT++jVk/FWwLbV3AD7oC4Owqe4J/A/dWrj5YT Uo7UiSA1pZd0uimOcLxrL8v5KoOyuXyvsEAA0S0AbXO/ayh5C1ZRl8kcqItBSeovvq wsAeWSoQJEB9RI1l1eKt4Z0DppvttCI7hqV6DKTCdKfLKQ09WVzQHC3sSPxqGoKBhF 8PjVIWuwgHqY8o9TYeTfK7SFFhGj4KvpTFgK6LvtLmffobWFFfEQ7IaYkJi22+ta9D 32iOWIcFyfODA== Date: Tue, 25 Jul 2023 10:32:34 +0200 From: Boris Brezillon To: Dmitry Osipenko Cc: David Airlie , Gerd Hoffmann , Gurchetan Singh , Chia-I Wu , Daniel Vetter , 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 v14 02/12] drm/shmem-helper: Add pages_pin_count field Message-ID: <20230725103234.0c8923f1@collabora.com> In-Reply-To: <20230725092709.51356f39@collabora.com> References: <20230722234746.205949-1-dmitry.osipenko@collabora.com> <20230722234746.205949-3-dmitry.osipenko@collabora.com> <20230725092709.51356f39@collabora.com> Organization: Collabora X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; x86_64-redhat-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 25 Jul 2023 09:27:09 +0200 Boris Brezillon wrote: > On Sun, 23 Jul 2023 02:47:36 +0300 > Dmitry Osipenko wrote: > > > And new pages_pin_count field to struct drm_gem_shmem_object that will > > determine whether pages are evictable by memory shrinker. The pages will > > be evictable only when pages_pin_count=0. This patch prepares code for > > addition of the memory shrinker that will utilize the new field. > > > > Signed-off-by: Dmitry Osipenko > > --- > > drivers/gpu/drm/drm_gem_shmem_helper.c | 9 +++++++++ > > include/drm/drm_gem_shmem_helper.h | 9 +++++++++ > > 2 files changed, 18 insertions(+) > > > > diff --git a/drivers/gpu/drm/drm_gem_shmem_helper.c b/drivers/gpu/drm/drm_gem_shmem_helper.c > > index 267153853e2c..42ba201dda50 100644 > > --- a/drivers/gpu/drm/drm_gem_shmem_helper.c > > +++ b/drivers/gpu/drm/drm_gem_shmem_helper.c > > @@ -274,15 +274,24 @@ static int drm_gem_shmem_pin_locked(struct drm_gem_shmem_object *shmem) > > dma_resv_assert_held(shmem->base.resv); > > > > ret = drm_gem_shmem_get_pages(shmem); > > + if (!ret) > > + shmem->pages_pin_count++; > > > > return ret; > > } > > > > static void drm_gem_shmem_unpin_locked(struct drm_gem_shmem_object *shmem) > > { > > + struct drm_gem_object *obj = &shmem->base; > > + > > dma_resv_assert_held(shmem->base.resv); > > > > + if (drm_WARN_ON_ONCE(obj->dev, !shmem->pages_pin_count)) > > + return; > > + > > drm_gem_shmem_put_pages(shmem); > > + > > + shmem->pages_pin_count--; > > } > > > > /** > > diff --git a/include/drm/drm_gem_shmem_helper.h b/include/drm/drm_gem_shmem_helper.h > > index bf0c31aa8fbe..7111f5743006 100644 > > --- a/include/drm/drm_gem_shmem_helper.h > > +++ b/include/drm/drm_gem_shmem_helper.h > > @@ -39,6 +39,15 @@ struct drm_gem_shmem_object { > > */ > > unsigned int pages_use_count; > > > > + /** > > + * @pages_pin_count: > > + * > > + * Reference count on the pinned pages table. > > + * The pages allowed to be evicted by memory shrinker > > + * only when the count is zero. > > + */ > > + unsigned int pages_pin_count; > > Can we make it an atomic_t, so we can avoid taking the lock when the > GEM has already been pinned. That's something I need to be able to grab > a pin-ref in a path where the GEM resv lock is already held[1]. We could > of course expose the locked version, My bad, that's actually not true. The problem is not that I call drm_gem_shmem_pin() with the resv lock already held, but that I call drm_gem_shmem_pin() in a dma-signaling path where I'm not allowed to take a resv lock. I know for sure pin_count > 0, because all GEM objects mapped to a VM have their memory pinned right now, and this should stand until we decide to add support for live-GEM eviction, at which point we'll probably have a way to detect when a GEM is evicted, and avoid calling drm_gem_shmem_pin() on it. TLDR; I can't trade the atomic_t for a drm_gem_shmem_pin_locked(), because that wouldn't solve my problem. The other solution would be to add an atomic_t at the driver-GEM level, and only call drm_gem_shmem_[un]pin() on 0 <-> 1 transitions, but I thought using an atomic at the GEM-shmem level, to avoid locking when we can, would be beneficial to the rest of the eco-system. Let me know if that's not an option, and I'll go back to the driver-specific atomic_t. > but in my case, I want to enforce > the fact the GEM has been pinned before the drm_gem_shmem_pin() call in > the section protected by the resv lock, so catching a "refcount 0 -> 1" > situation would be useful. Beside, using an atomic to avoid the > lock/unlock dance when refcount > 1 might be beneficial to everyone. > > [1]https://gitlab.freedesktop.org/bbrezillon/linux/-/commit/4420fa0d5768ebdc35b34d58d4ae5fad9fbb93f9 > > > + > > /** > > * @madv: State for madvise > > * >