Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp1814164rdh; Tue, 26 Sep 2023 04:36:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFiAJEdFqSkzjczNQW6yLQuqW25kNlD6aJq0WtFo6PDTHqg29RPIoiZu6+zr9ynU82N6Wij X-Received: by 2002:a05:6358:724e:b0:142:fd2b:da2e with SMTP id i14-20020a056358724e00b00142fd2bda2emr10810493rwa.31.1695728170045; Tue, 26 Sep 2023 04:36:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695728170; cv=none; d=google.com; s=arc-20160816; b=zkUhQpqYoxraVJdaBlQ1MFSfwmWFiJ+fegLfVAKvpBmDfbiRueqrY8ewv7YXn0RiCW YqIH6Xi9SJAbPlOIIfWN5KL2jG7U6G+UAPn51uik5g9bzb70QcpY56/Tf0oF13yadkQi 4C8UsCb2xYK1xAlYzIaFXBlUwIAvjTdT/APngIIyUltS8Sci2CNYVt/cAH9an1ByFw9D wQobwOO8JF4mlyAoYo+brABHxh+otC33e5oEw+r6+sl92L23KDokrNqYFnSNxzJx4FM5 w56CUwFvOZ80gIB+pqGOTuFjI1Kt4JUr4jXS/lFsILkN8wLE1y0ZFskEFSXHevdccb9y L27g== 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=5HeYSwNPVdbZgitEGI2mWWIcEo4Ivute+wLQPWQxfGA=; fh=uxLNaPoPRUzmkUc1KOgGwCEU/bTSjK8Yed2Up6ovXLY=; b=Q+PUWU7E3gPnCfT6YY6ikPqGCqXuQQe6JI/3PV3jzjBE+pAfg6KqeZMcnfECA2oCMO kkuRxsxi8DDJd9WgmK+XBwn+zTrxY5ZN7feONNzst58dvHQ773skLVyY6ipgU74Tos4t eNmbjKvucOXrwz5PnUahSfxSAAXChwLaeRnVp1TkKzZkyxcuqpxHTyC3FoRNFzNfC4+h QQkbCjM7+WhrpWJcITAQM6RQ44GaBACaZ3O/r0JmMYH7f6Yjf7Xjq22igm+pI1n29lOY QdTFOxhJPCrZBVD/O3cpHDtrXorIwowQQBUSH6u18KVqrYi1p6coDllJyTsIq6pBh8KV YhdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=EI4lEbMs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id i9-20020a63b309000000b0057754fd03a3si12220459pgf.144.2023.09.26.04.36.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 04:36:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=EI4lEbMs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 2E35280AC34C; Tue, 26 Sep 2023 00:35:36 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233446AbjIZHfa (ORCPT + 99 others); Tue, 26 Sep 2023 03:35:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56824 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233345AbjIZHf2 (ORCPT ); Tue, 26 Sep 2023 03:35:28 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [IPv6:2a00:1098:0:82:1000:25:2eeb:e5ab]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 11E93F3 for ; Tue, 26 Sep 2023 00:35:21 -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 9CEAD6607314; Tue, 26 Sep 2023 08:35:19 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1695713720; bh=ab+8nbg2PMrVtNGAFD8Asd2z04FE9VRGE9dAy3ZJWiA=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=EI4lEbMsblThRUsetRRTEVZS6rHCA3vJIXb1uCRxHLX79NVHdT/02VtREn6TgfRDy Fxp7/8tj66psqFO3hANj0kQICCl2dWRebmmjMEhuBRg3LCIkIRK7hf35x3MA+Wltpt zVPYZ4sxgJ5/Bi8EFVCwVXE4VB1lCxqi/2r/7xIb9OG1jkLwsoFR/9l1I5YLa41Jts uoF21hLGsi0ieIcb6BffTw8yFwBi84WlRE1YxG252ayEMNY29+aU0Br2bA3n9SRPJC ht6ras1sy5Q0PMAtFkNlnkD3jmCTA5djdeTl1z74L6CVqOC7cRbZo+WtUH25YWIlr6 6QH8jTeAROGcQ== Date: Tue, 26 Sep 2023 09:35:17 +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 v17 13/18] drm/shmem-helper: Add memory shrinker Message-ID: <20230926093517.11a172ad@collabora.com> In-Reply-To: <454c464e-4534-7ec3-6d38-49b7df83c7be@collabora.com> References: <20230914232721.408581-1-dmitry.osipenko@collabora.com> <20230914232721.408581-14-dmitry.osipenko@collabora.com> <20230915104633.0d5c3932@collabora.com> <454c464e-4534-7ec3-6d38-49b7df83c7be@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=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Tue, 26 Sep 2023 00:35:36 -0700 (PDT) On Tue, 26 Sep 2023 03:30:35 +0300 Dmitry Osipenko wrote: > On 9/15/23 11:46, Boris Brezillon wrote: > > The naming becomes quite confusing, with drm_gem_shmem_unpin_locked() > > and drm_gem_shmem_unpin_pages_locked(). By the look of it, it seems to > > do exactly the opposite of drm_gem_shmem_swapin_locked(), except for > > the missing ->evicted = true, which we can move here anyway, given > > drm_gem_shmem_purge_locked() explicitly set it to false anyway. The > > other thing that's missing is the > > drm_gem_shmem_update_pages_state_locked(), but it can also be moved > > there I think, if the the ->madv update happens before the > > drm_gem_shmem_unpin_pages_locked() call in > > drm_gem_shmem_purge_locked(). > > > > So, how about renaming this function drm_gem_shmem_swapout_locked()? > > The swapout name would be misleading to me because pages aren't moved to > swap, but allowed to be moved. I'll rename it to > drm_gem_shmem_shrinker_unpin_locked(). If you go this way, I would argue that drm_gem_shmem_swapin_locked() is just as incorrect as drm_gem_shmem_swapout_locked(), in that drm_gem_get_pages() might just return pages that were flagged reclaimable but never reclaimed/swapped-out. I do think that having some symmetry in the naming makes more sense than being 100% accurate. > > >> { > >> struct drm_gem_object *obj = &shmem->base; > >> struct drm_device *dev = obj->dev; > >> > >> dma_resv_assert_held(shmem->base.resv); > >> > >> - drm_WARN_ON(obj->dev, !drm_gem_shmem_is_purgeable(shmem)); > >> + if (shmem->evicted) > >> + return; > >> > >> dma_unmap_sgtable(dev->dev, shmem->sgt, DMA_BIDIRECTIONAL, 0); > > Are we sure we'll always have sgt != NULL? IIRC, if the GEM is only > > mmap-ed in userspace, get_sgt() is not necessarily called by the driver > > (needed to map in GPU space), and we have a potential NULL deref here. > > Maybe that changed at some point in the series, and sgt is > > unconditionally populated when get_pages() is called now. > > The sgt is always set in this function because it's part of shrinker and > shrinker doesn't touch GEMs without sgt. Okay, that's questionable. Why would we not want to reclaim BOs that are only mapped in userspace (sgt == NULL && pages_use_count > 0 && pages_pin_count == 0)? I agree that creating such a BO would be pointless (why create a buffer through DRM if it's not passed to the GPU), but that's still something the API allows... > > >> + __drm_gem_shmem_release_pages(shmem); > > Make sure you drop the implicit pages_use_count ref the sgt had, this > > way you can still tie the necessity to drop the pages to sgt != NULL in > > drm_gem_shmem_free(). > > This will require further refcnt re-initialization when pages are > restored if it's dropped to zero. I don't see how this will improve > anything. Sorry to disagree, but I do think it matters to have a clear ownership model, and if I look at the code (drm_gem_shmem_get_pages_sgt_locked()), the sgt clearly owns a reference to the pages it points to.