Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp1879654rdb; Tue, 3 Oct 2023 04:09:41 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHcWk0fKePbaUK52a+7vO96VS9exss18lu9DGC66OrTftewuHwCctQ/ZGejQuQGjdRFLltw X-Received: by 2002:a05:6808:18a:b0:3a9:d5df:2bc with SMTP id w10-20020a056808018a00b003a9d5df02bcmr14400403oic.32.1696331381199; Tue, 03 Oct 2023 04:09:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696331381; cv=none; d=google.com; s=arc-20160816; b=cAGn58C0ZQCApVMcc7XH8+x54jKY29fvbBmVut0l5ZV1AL8WYHFsyI28vtxwsFFZPI Ne7aYHfPftS4SVjD83mmbEaO2Gi6tqKSq1XIW3EolCNuBL/SE6g6a3hwU7nNvzYUNv18 NGVoKJSJkPpR6dhVTvQEmqCDSEcGYzeu7OhoqFDkpyUKMOEZC5Qnpft93X3IwJctInJR HfWQaFQzDxxiSIFtYUifEGDVelCIwKJO2seubGROBtvWDROuyWtFxrTFlOXuM8axX0vx fYkEMC38SBSkHPZ/Niv06OlohJwwUEYZpXiPNGi2yFmDL/n+Ug752i1URf/ksimisxg8 Mpxg== 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=InU26qn0jfC+g2c0znNoT4/wIwXteT1VghAo5zLhWb8=; fh=uxLNaPoPRUzmkUc1KOgGwCEU/bTSjK8Yed2Up6ovXLY=; b=PL5OT+yAO05VvB4KjgM+sqXIs8QELmLk/4/Yi6Ja8lKFYOmEVutgwmdvflXEENxdy/ 83MUTazTCMYTeA+mQ7m2X8K0qvcJHaQqhYWf5w8jfd7K6AQ8PHzj4iSpJwUaZbGcbvKx S84/TlWpPK4Q7AjQDxfq5lT8Jn3GB34GZlD34VGULIBqC92VEAtM6FS8uut0Ym05GHd9 ffN/mSo9zoNoxKmu3yltG+0SPSwud4Z16UaRZ5D01ELlXEBgpfJiHZSFLqzEMG228nYS 1Cqnlvjzp6XPvUnVJfyq6p5najoIheDjdJwJMD2lMzb25I/D5pift0X9OqNkI/cDJO7J btwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=TjJcT+Ri; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id v4-20020a17090a634400b00277517b42dasi1170304pjs.35.2023.10.03.04.09.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Oct 2023 04:09:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=TjJcT+Ri; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 snail.vger.email (Postfix) with ESMTP id 6F0A7816692D; Tue, 3 Oct 2023 04:09:40 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231801AbjJCLJk (ORCPT + 99 others); Tue, 3 Oct 2023 07:09:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38516 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231800AbjJCLJj (ORCPT ); Tue, 3 Oct 2023 07:09:39 -0400 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9E3159E for ; Tue, 3 Oct 2023 04:09:15 -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 145A0660731C; Tue, 3 Oct 2023 12:09:13 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1696331353; bh=qhpiVcKFT7dJL847ee3DXa0YUpydpW7zhFpxmr/dhvY=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=TjJcT+Rib1JV/7Xma8o6sD4s2oscHZ/L7vAOn13i19Xww2TebVBO5xr/eSOaCQE0d cKhVXSSQbb12hS1xji9dnKkyJFdreuXaN3s/yC22Ft3f3vLp3UYYL+Bb6B3ABtS9jL ecJo9hwxguDpLbAYOCUYZ25V+Li8dPsiiJnv1LknEOVPIFKCB6TTLytzn3oAh1PQ75 vUf0guwuMClW/ZvrRXVSxITULBAzVBwJ/EQiwEycLKiMlGVczdQM8ko75IN94CWQS1 lWMU+oCTZ9sP/iPhnqhuSHG+LtIwcCb/ecEgw7zoadfQ60/FwjJTN8JMffFlqFf99v 07+WbsgjAEdkA== Date: Tue, 3 Oct 2023 13:09:09 +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: <20231003130909.3b470a43@collabora.com> In-Reply-To: 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> <20230926093517.11a172ad@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,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 03 Oct 2023 04:09:40 -0700 (PDT) On Mon, 2 Oct 2023 22:28:13 +0300 Dmitry Osipenko wrote: > On 9/26/23 10:35, Boris Brezillon 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. > > That function is internal to drm-shmem and is used for both eviction and > purging. Having "swap-out" invoked by the purging also doesn't sound good. The part that discards the GEM in the shmem file is outside this function (shmem_truncate_range()), so all this function does in practice is flag the pages as evictable (or rather, clear the unevictable flag), so they can be reclaimed. The swapout suggesting was mostly based on the fact it does exactly the opposite of swapin(). > > Given that the function in question mainly "unmaps" the pages, what > about drm_gem_shmem_shkinker_unmap_pages_locked()? Unmap tends to refer to a VM related operation (removing a mapping in the CPU or GPU VM), so it's confusing too IMHO. What we do here is return pages to the shmem file logic, so they can be reclaimed. Given the drm_gem function doing that is called drm_gem_put_pages(), maybe rename it drm_gem_shmem_shrinker_put_pages_locked(), and rename drm_gem_shmem_swapin_locked() into drm_gem_shmem_shrinker_get_pages_locked(), to be consistent. > > >>>> { > >>>> 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... > > This is a pre-existing behaviour. There is no driver that uses pages > without sgt, hence there is nobody to test such code paths. > > Maybe will worth to explicitly prohibit usage of get_pages() without > having sgt for clarity. Nope, I don't think we should. Panthor is dissociating the BO creation for the GPU VM map operation, meaning we only get to ask for an sgt when the BO is first mapped in GPU space. In the meantime, the shrinker logic might decide to evict an object that has been already CPU-mapped (using mmap()). > But this should be separate to this patchset, IMO. FYI, I'm not being picky just for fun, I do intend to use the shmem-shrinker in panthor at some point, and I think it's important to get it right from the beginning, even if all your existing users don't care. I mean, I would understand if what I was asking was requiring heavy changes to the existing logic, but, unless I'm wrong, I don't think it does.