Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp2381827rdb; Thu, 21 Sep 2023 17:56:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEwMMDFmA2OIkmTfE3uSo5jdM++uskQbaNIcX+QN2kcc+aH8kVmBcUgrV/I2YX1Wg3wwTVg X-Received: by 2002:aca:2b0d:0:b0:3a7:1e98:80ad with SMTP id i13-20020aca2b0d000000b003a71e9880admr5996628oik.9.1695344193111; Thu, 21 Sep 2023 17:56:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695344193; cv=none; d=google.com; s=arc-20160816; b=JOVHagWkbitEw/PID5Mx3joYHuei+qjEfUx47tlmkncyFv7W3JNiyi/qvOiA+9u06V E95ptsFSR2ZCP/g1rjd1z+9jakEBQ+DC3gYaPAwDk0/P0r8NvNoW/j9ayaEWa3tL71qD e19sA4VjkK8dikfn8qqeAXleJ8Jx3AEULLNle1Mb44edbJl0bTX5vTxFAtyfzLs1A1yr lmvoJsSjCgEF3Xw01N2VCXb8GEaHG+iIneuphda1oPKyQklSZLDsRzzPlTaaVFlULSpS 2cZ8C6zFz4B7c0AXqZpsfsk9BTvEz65Xl0a9oiLu1Qs34e0asczNXGTWR/XPz84GGoVZ NH/g== 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=oicOndlZvddBaEAuXMdY6/JQwL9LVqvq6yidw9cyIRw=; fh=xLuQTK7fvU0s05qk0Ob2L0O/5t9TRZohowldSTh3Eng=; b=0FpLH9ol3gGpaKW/ufW3Es4Ikw0gQVZflnCeitYFUionb2tTuMJIZslnQU/vj+cqpD bIdpRpWH6sCDu6PgjmlhYgRuKSMadpF6FoToCrXgOZ2qmRoi3CBohyOMmGuSrqY55S6E JJJBfB0/pVMKXCHI6l80vgpWp3Nyh1MjCwPeaDWWzdDoljaNCxYQz2nlE0uJtkUC0+HP tvMqWZ9fpIa1B+br7N0cb30YBw+yqySte8Jo7qMK6Vpf5kAc8XcFB92wjw7Cbf6Ynqr0 JE1vGA6hl7OySOEh5PSRqxeXMdMGPImYFJl1bVhvw0cpYDOEZlHKZj35M5maOF4TRqw6 0pDQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=aBnd+wXp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id fb35-20020a056a002da300b00690c22195c4si2702537pfb.0.2023.09.21.17.56.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Sep 2023 17:56:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=aBnd+wXp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (Postfix) with ESMTP id 178EE82357B9; Thu, 21 Sep 2023 12:06:06 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231226AbjIUTF4 (ORCPT + 99 others); Thu, 21 Sep 2023 15:05:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35090 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231574AbjIUTFD (ORCPT ); Thu, 21 Sep 2023 15:05:03 -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 989AE7BC37 for ; Thu, 21 Sep 2023 10:53:18 -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 AFB24660731B; Thu, 21 Sep 2023 16:27:05 +0100 (BST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1695310026; bh=iSk2ckbYAN/XTNJK8G8uZaUu+uSKKEVpKeGB7KywgnA=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=aBnd+wXpDgF8SoEfVb9ceGdEeRWKkoXvufu6wI7xOlVAF16vMITgMNNfYR7l1PYd7 eAlLnghGgzF/PjlrZrWw7JMKppogajuz6dGOGy23T+rZ9SMRtpWx7JXaFypYNMTJ1f InbpX+uOSvcQoEJH6ZUYLpvHVZaQqSnM13e2grxgKdPQQDQ+XoK9mycxbcgR469F8p jnVcDKhZt8hpTBBGZex+Y2DsnwqhJ3B1SKBgcrjvKEaN9ovPzMCM+ByzKn/rDQ7h60 y1+cTvSVg074LItc3J8CUAmltXRX9QNJh1ckQX7OXEgJPMG31+hoRHLQgDYl3UpJ6/ qpy0v9nuWo3og== Date: Thu, 21 Sep 2023 17:27:02 +0200 From: Boris Brezillon To: Christian =?UTF-8?B?S8O2bmln?= Cc: Danilo Krummrich , airlied@gmail.com, daniel@ffwll.ch, matthew.brost@intel.com, thomas.hellstrom@linux.intel.com, sarah.walker@imgtec.com, donald.robson@imgtec.com, faith.ekstrand@collabora.com, dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH drm-misc-next v4 4/8] drm/gpuvm: add common dma-resv per struct drm_gpuvm Message-ID: <20230921172702.1b9a49a9@collabora.com> In-Reply-To: <72ea51ca-f7b0-2e2a-b276-6c6c7413374b@amd.com> References: <20230920144343.64830-1-dakr@redhat.com> <20230920144343.64830-5-dakr@redhat.com> <7951dc11-6047-6beb-8ef8-98c862e26ec3@amd.com> <964a1bdd-549d-7850-9a8c-8278c4cd32ec@redhat.com> <20230921162510.10903d90@collabora.com> <72ea51ca-f7b0-2e2a-b276-6c6c7413374b@amd.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=UTF-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.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 (groat.vger.email [0.0.0.0]); Thu, 21 Sep 2023 12:06:07 -0700 (PDT) On Thu, 21 Sep 2023 16:34:54 +0200 Christian K=C3=B6nig wrote: > Am 21.09.23 um 16:25 schrieb Boris Brezillon: > > On Thu, 21 Sep 2023 15:34:44 +0200 > > Danilo Krummrich wrote: > > =20 > >> On 9/21/23 09:39, Christian K=C3=B6nig wrote: =20 > >>> Am 20.09.23 um 16:42 schrieb Danilo Krummrich: =20 > >>>> Provide a common dma-resv for GEM objects not being used outside of = this > >>>> GPU-VM. This is used in a subsequent patch to generalize dma-resv, > >>>> external and evicted object handling and GEM validation. > >>>> > >>>> Signed-off-by: Danilo Krummrich > >>>> --- > >>>> =C2=A0 drivers/gpu/drm/drm_gpuvm.c=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 |=C2=A0 9 +++++++-- > >>>> =C2=A0 drivers/gpu/drm/nouveau/nouveau_uvmm.c |=C2=A0 2 +- > >>>> =C2=A0 include/drm/drm_gpuvm.h=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 | 17 +++++++++++++++= +- > >>>> =C2=A0 3 files changed, 24 insertions(+), 4 deletions(-) > >>>> > >>>> diff --git a/drivers/gpu/drm/drm_gpuvm.c b/drivers/gpu/drm/drm_gpuvm= .c > >>>> index bfea4a8a19ec..cbf4b738a16c 100644 > >>>> --- a/drivers/gpu/drm/drm_gpuvm.c > >>>> +++ b/drivers/gpu/drm/drm_gpuvm.c > >>>> @@ -655,6 +655,7 @@ drm_gpuva_range_valid(struct drm_gpuvm *gpuvm, > >>>> =C2=A0 /** > >>>> =C2=A0=C2=A0 * drm_gpuvm_init() - initialize a &drm_gpuvm > >>>> =C2=A0=C2=A0 * @gpuvm: pointer to the &drm_gpuvm to initialize > >>>> + * @drm: the drivers &drm_device > >>>> =C2=A0=C2=A0 * @name: the name of the GPU VA space > >>>> =C2=A0=C2=A0 * @start_offset: the start offset of the GPU VA space > >>>> =C2=A0=C2=A0 * @range: the size of the GPU VA space > >>>> @@ -668,7 +669,7 @@ drm_gpuva_range_valid(struct drm_gpuvm *gpuvm, > >>>> =C2=A0=C2=A0 * &name is expected to be managed by the surrounding d= river structures. > >>>> =C2=A0=C2=A0 */ > >>>> =C2=A0 void > >>>> -drm_gpuvm_init(struct drm_gpuvm *gpuvm, > >>>> +drm_gpuvm_init(struct drm_gpuvm *gpuvm, struct drm_device *drm, > >>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 const char *name, > >>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 u64 start_offset, u64 range, > >>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0 u64 reserve_offset, u64 reserve_range, > >>>> @@ -694,6 +695,8 @@ drm_gpuvm_init(struct drm_gpuvm *gpuvm, > >>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 reserve_range))) > >>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 __drm_gpuva_insert(gpuvm, &gpuvm->kernel_alloc_node); > >>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } > >>>> + > >>>> +=C2=A0=C2=A0=C2=A0 drm_gem_private_object_init(drm, &gpuvm->d_obj, = 0); > >>>> =C2=A0 } > >>>> =C2=A0 EXPORT_SYMBOL_GPL(drm_gpuvm_init); > >>>> @@ -713,7 +716,9 @@ drm_gpuvm_destroy(struct drm_gpuvm *gpuvm) > >>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 __drm_gpuva_= remove(&gpuvm->kernel_alloc_node); > >>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 WARN(!RB_EMPTY_ROOT(&gpuvm->rb.tree.= rb_root), > >>>> -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 "GPUVA tree is not= empty, potentially leaking memory."); > >>>> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 "GPUVA tree is not= empty, potentially leaking memory.\n"); > >>>> + > >>>> +=C2=A0=C2=A0=C2=A0 drm_gem_private_object_fini(&gpuvm->d_obj); > >>>> =C2=A0 } > >>>> =C2=A0 EXPORT_SYMBOL_GPL(drm_gpuvm_destroy); > >>>> diff --git a/drivers/gpu/drm/nouveau/nouveau_uvmm.c b/drivers/gpu/dr= m/nouveau/nouveau_uvmm.c > >>>> index 6c86b64273c3..a80ac8767843 100644 > >>>> --- a/drivers/gpu/drm/nouveau/nouveau_uvmm.c > >>>> +++ b/drivers/gpu/drm/nouveau/nouveau_uvmm.c > >>>> @@ -1836,7 +1836,7 @@ nouveau_uvmm_init(struct nouveau_uvmm *uvmm, s= truct nouveau_cli *cli, > >>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 uvmm->kernel_managed_addr =3D kernel= _managed_addr; > >>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 uvmm->kernel_managed_size =3D kernel= _managed_size; > >>>> -=C2=A0=C2=A0=C2=A0 drm_gpuvm_init(&uvmm->base, cli->name, > >>>> +=C2=A0=C2=A0=C2=A0 drm_gpuvm_init(&uvmm->base, cli->drm->dev, cli->= name, > >>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 NOUVEAU_VA_SPACE_START, > >>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 NOUVEAU_VA_SPACE_END, > >>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 kernel_managed_addr, kernel_managed_size, > >>>> diff --git a/include/drm/drm_gpuvm.h b/include/drm/drm_gpuvm.h > >>>> index 0e802676e0a9..6666c07d7c3e 100644 > >>>> --- a/include/drm/drm_gpuvm.h > >>>> +++ b/include/drm/drm_gpuvm.h > >>>> @@ -240,14 +240,29 @@ struct drm_gpuvm { > >>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 * @ops: &drm_gpuvm_ops providi= ng the split/merge steps to drivers > >>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 */ > >>>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 const struct drm_gpuvm_ops *ops; > >>>> + > >>>> +=C2=A0=C2=A0=C2=A0 /** > >>>> +=C2=A0=C2=A0=C2=A0=C2=A0 * @d_obj: Dummy GEM object; used internall= y to pass the GPU VMs > >>>> +=C2=A0=C2=A0=C2=A0=C2=A0 * dma-resv to &drm_exec. Provides the GPUV= M's &dma-resv. > >>>> +=C2=A0=C2=A0=C2=A0=C2=A0 */ > >>>> +=C2=A0=C2=A0=C2=A0 struct drm_gem_object d_obj; =20 > >>> Yeah, as pointed out in the other mail that won't work like this. =20 > >> Which one? Seems that I missed it. > >> =20 > >>> The GPUVM contains GEM objects and therefore should probably have a r= eference to those objects. > >>> > >>> When those GEM objects now use the dma-resv object embedded inside th= e GPUVM then they also need a reference to the GPUVM to make sure the dma-r= esv object won't be freed before they are freed. =20 > >> My assumption here is that GEM objects being local to a certain VM nev= er out-live the VM. We never share it with anyone, otherwise it would be ex= ternal and hence wouldn't carray the VM's dma-resv. The only references I s= ee are from the VM itself (which is fine) and from userspace. The latter is= n't a problem as long as all GEM handles are closed before the VM is destro= yed on FD close. =20 > > But we don't want to rely on userspace doing the right thing (calling > > GEM_CLOSE before releasing the VM), do we? > > > > BTW, even though my private BOs have a ref to their exclusive VM, I just > > ran into a bug because drm_gem_shmem_free() acquires the resv lock > > (which is questionable, but that's not the topic :-)) and > > I was calling vm_put(bo->exclusive_vm) before drm_gem_shmem_free(), > > leading to a use-after-free when the gem->resv is acquired. This has > > nothing to do with drm_gpuvm, but it proves that this sort of bug is > > likely to happen if we don't pay attention. > > =20 > >> Do I miss something? Do we have use cases where this isn't true? =20 > > The other case I can think of is GEM being v[un]map-ed (kernel > > mapping) after the VM was released. =20 >=20 > I think the file reference and the VM stays around in those cases as=20 > well, but yes I also think we have use cases which won't work. >=20 > > =20 > >>> This is a circle reference dependency. =20 > > FWIW, I solved that by having a vm_destroy() function that kills all the > > mappings in a VM, which in turn releases all the refs the VM had on > > private BOs. Then, it's just a matter of waiting for all private GEMs > > to be destroyed to get the final steps of the VM destruction, which is > > really just about releasing resources (it's called panthor_vm_release() > > in my case) executed when the VM refcount drops to zero. > > =20 > >>> The simplest solution I can see is to let the driver provide the GEM = object to use. Amdgpu uses the root page directory object for this. =20 > >> Sure, we can do that, if we see cases where VM local GEM objects can o= ut-live the VM. =20 > >>> Apart from that I strongly think that we shouldn't let the GPUVM code= create a driver GEM object. We did that in TTM for the ghost objects and i= t turned out to be a bad idea. =20 > > Would that really solve the circular ref issue? I mean, if you're > > taking the root page dir object as your VM resv, you still have to make > > sure it outlives the private GEMs, which means, you either need > > to take a ref on the object, leading to the same circular ref mess, or > > you need to reset private GEMs resvs before destroying this root page > > dir GEM (whose lifecyle is likely the same as your VM object which > > embeds the drm_gpuvm instance). =20 >=20 > Yes it does help, see how amdgpu does it: >=20 > The VM references all BOs, e.g. page tables as well as user BOs. >=20 > The BOs which use the dma-resv of the root page directory also reference= =20 > the root page directorys BO. >=20 > So when the VM drops all references the page tables and user BO are=20 > released first and the root page directory which everybody references las= t. Right, now I see how having a dynamically allocated GEM on which both the VM and private BOs hold a reference solve problem. >=20 > > Making it driver-specific just moves the responsibility back to drivers > > (and also allows re-using an real GEM object instead of a dummy one, > > but I'm not sure we care about saving a few hundreds bytes at that > > point), which is a good way to not take the blame if the driver does > > something wrong, but also doesn't really help people do the right thing= . =20 >=20 > The additional memory usage is irrelevant, but we have very very bad=20 > experience with TTM using dummy objects similar to this here. >=20 > They tend to end up in driver specific functions and then the driver=20 > will try to upcast those dummy to driver specific BOs. In the end you=20 > get really hard to figure out memory corruptions. Hm, I see. Anyway, I guess creating a dummy GEM is simple enough that we can leave it to drivers (for drivers that don't have a real GEM to pass, of course).