Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp470464rdf; Fri, 3 Nov 2023 06:16:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGxv3ItySp+X+fHnMxZGlx4rmId62vb5PAg/B2/U4D8D02nrbAOwusCfMYPZ8hGyu3EmYCs X-Received: by 2002:a17:90b:947:b0:27e:3ae3:eae0 with SMTP id dw7-20020a17090b094700b0027e3ae3eae0mr18108648pjb.16.1699017378651; Fri, 03 Nov 2023 06:16:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1699017378; cv=none; d=google.com; s=arc-20160816; b=UfIDFHHsw8lYKptyqx4gjm+UzdsfAEdgDy6qI2vXBfDZ5h2zewpAq9pUHNl77BDUCm TO9Y5HtczKMHbkMjG0LtqGh2N43jjHSOLId3lIwM3m9xfBlPNrQAIYjC8tQHcxXfeD0V gR0eFbDGvJVn/6TPha1npNgPpm0Xd6i251V2x9eNVUUO6OuGydIFfw38hBpyBg4LHdQT eAEuzEYVpXrnq80lGW1R9Y528a6QsvWYxan8N41ZgJzM4kEldQjf+OxIS0hreLQlQuOJ 2a26ct9HJ2ThelSMgsGTnLjkHxv9Kc/Ea/WuAtYiF9ZByVDrX3UThE+NEwd7wias6cPv f/fA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=JeaDcEiv+OJg0rs4GLDtEP9L7bFfkuejpkBRQMtkogU=; fh=M3hO8d4vR/GUN1iTCfaCUR7wzEIJGrF96wAlutuiZPU=; b=jE+6yr0soC5siSfnIXOEIzuOOI0ea9dYh7YeMLb/REV8EtU7lUlAF65nipeieoQyJQ 1NnHE1LeVyf6a48eJx+JBa/1Ryxt/GbtWtNTq2cCkzRkqQfKn6hRL95UosqSRbXKx3cK C252Dad2iHgpv473T558M3vomg/pGW+FY11kKuvGqARnShH9Q1mfnOYnl/BuhxHQfjB/ 1SIek6rkRX+O+qrvMXZxxrIeGPHKlSOffIDVVGwwW8V0V7bgfBx5DvRy+Ayzv73/gD5d 1bd4pZ63qEOFKvoPzWbvgGrJJZHI5DiK+itHIOx3mewL4AF00jSSPNi4kGuquhkycFEU ZDdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=RV6Y2NS9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id x18-20020a17090abc9200b0027748734bb9si1676653pjr.148.2023.11.03.06.16.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 06:16:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=RV6Y2NS9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 2E06780569AB; Fri, 3 Nov 2023 06:15:31 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377636AbjKCNPH (ORCPT + 99 others); Fri, 3 Nov 2023 09:15:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1376554AbjKCNPF (ORCPT ); Fri, 3 Nov 2023 09:15:05 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A9511D54 for ; Fri, 3 Nov 2023 06:14:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1699017246; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JeaDcEiv+OJg0rs4GLDtEP9L7bFfkuejpkBRQMtkogU=; b=RV6Y2NS9Zew2F7oDI2WGqAf6pXsBfWgAZF8FEmCg4O6gTFpSIN8pyBTm/ur2Ag+go2bvSo 53iyGgivQe/j0CDq29QXP6lbRbEiRlXn/kQRQEYxV69rgotPS0fMCWdUXE7Jp7oUbV1lpv f17F7AoMPlpn5CPDpRseloM3ja0BXro= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-660-namQgnXzP46eHX0wco1Jfg-1; Fri, 03 Nov 2023 09:14:05 -0400 X-MC-Unique: namQgnXzP46eHX0wco1Jfg-1 Received: by mail-ed1-f72.google.com with SMTP id 4fb4d7f45d1cf-54061ad6600so1767384a12.3 for ; Fri, 03 Nov 2023 06:14:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699017244; x=1699622044; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JeaDcEiv+OJg0rs4GLDtEP9L7bFfkuejpkBRQMtkogU=; b=uQfzjZ5mdQ1yhbLOVdjTySAWxuyn2cx98Ifuxw7j5RwKRbrFts2y83VXTQEYG/VrDd dY76ur3jHkIYxDD8eGbGZr5eabUxnhdEcFSZUaKOmTHBCoIS5ej7v0UMhsh8GtjUCERA hW/xCRIhDW1x90i4AOq6Sugz3avnDTFBtILnU6DiADE8Aq+L9VYU8n36Ui73y8l5QeR3 g7mcYiPyIQrtO+Fyy7tbnoYXy1as6o91u6gReBR8kJywr71hIJvY9p7wAbxX+ZZuujIY QnrZTWg0f7A5bSVjw10i5u8lWdsgLwmdaQNwyL/lGzUOza+p0Pdb8YH0O5B4tWadK+cg Lafg== X-Gm-Message-State: AOJu0YwPGFUzNvurTRWwPOJkCL5VRkJ1f5Z75Nwp9ppt2izWMQYWu4DQ G+0xd5VlV0P3/vRBNUIcqqlsknTZLoxUs2J9JoYG0r0/grON4MbrVvxP4jH5dpvHK/agtOLzUAh capczGdczuRVLCVQlfDOJagqt X-Received: by 2002:a50:aad8:0:b0:543:6444:ef2a with SMTP id r24-20020a50aad8000000b005436444ef2amr7882703edc.32.1699017244398; Fri, 03 Nov 2023 06:14:04 -0700 (PDT) X-Received: by 2002:a50:aad8:0:b0:543:6444:ef2a with SMTP id r24-20020a50aad8000000b005436444ef2amr7882683edc.32.1699017244056; Fri, 03 Nov 2023 06:14:04 -0700 (PDT) Received: from pollux ([2a02:810d:4b3f:de9c:abf:b8ff:feee:998b]) by smtp.gmail.com with ESMTPSA id h15-20020a056402094f00b0053116e45317sm944419edz.44.2023.11.03.06.14.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 06:14:03 -0700 (PDT) Date: Fri, 3 Nov 2023 14:14:01 +0100 From: Danilo Krummrich To: Christian =?iso-8859-1?Q?K=F6nig?= Cc: airlied@gmail.com, daniel@ffwll.ch, matthew.brost@intel.com, thomas.hellstrom@linux.intel.com, sarah.walker@imgtec.com, donald.robson@imgtec.com, boris.brezillon@collabora.com, faith@gfxstrand.net, dri-devel@lists.freedesktop.org, nouveau@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH drm-misc-next v8 09/12] drm/gpuvm: reference count drm_gpuvm structures Message-ID: References: <20231101233113.8059-1-dakr@redhat.com> <20231101233113.8059-10-dakr@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Spam-Status: No, score=-1.3 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.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 (lipwig.vger.email [0.0.0.0]); Fri, 03 Nov 2023 06:15:31 -0700 (PDT) On Fri, Nov 03, 2023 at 08:18:35AM +0100, Christian K?nig wrote: > Am 02.11.23 um 00:31 schrieb Danilo Krummrich: > > Implement reference counting for struct drm_gpuvm. > > From the design point of view what is that good for? It was discussed in this thread [1]. Essentially, the idea is to make sure that vm_bo->vm is always valid without the driver having the need to take extra care. It also ensures that GPUVM can't be freed with mappings still held. > > Background is that the most common use case I see is that this object is > embedded into something else and a reference count is then not really a good > idea. Do you have a specific use-case in mind where this would interfere? > > Thanks, > Christian. [1] https://lore.kernel.org/dri-devel/6fa058a4-20d3-44b9-af58-755cfb375d75@redhat.com/ > > > > > Signed-off-by: Danilo Krummrich > > --- > > drivers/gpu/drm/drm_gpuvm.c | 44 +++++++++++++++++++------- > > drivers/gpu/drm/nouveau/nouveau_uvmm.c | 20 +++++++++--- > > include/drm/drm_gpuvm.h | 31 +++++++++++++++++- > > 3 files changed, 78 insertions(+), 17 deletions(-) > > > > diff --git a/drivers/gpu/drm/drm_gpuvm.c b/drivers/gpu/drm/drm_gpuvm.c > > index 53e2c406fb04..6a88eafc5229 100644 > > --- a/drivers/gpu/drm/drm_gpuvm.c > > +++ b/drivers/gpu/drm/drm_gpuvm.c > > @@ -746,6 +746,8 @@ drm_gpuvm_init(struct drm_gpuvm *gpuvm, const char *name, > > gpuvm->rb.tree = RB_ROOT_CACHED; > > INIT_LIST_HEAD(&gpuvm->rb.list); > > + kref_init(&gpuvm->kref); > > + > > gpuvm->name = name ? name : "unknown"; > > gpuvm->flags = flags; > > gpuvm->ops = ops; > > @@ -770,15 +772,8 @@ drm_gpuvm_init(struct drm_gpuvm *gpuvm, const char *name, > > } > > EXPORT_SYMBOL_GPL(drm_gpuvm_init); > > -/** > > - * drm_gpuvm_destroy() - cleanup a &drm_gpuvm > > - * @gpuvm: pointer to the &drm_gpuvm to clean up > > - * > > - * Note that it is a bug to call this function on a manager that still > > - * holds GPU VA mappings. > > - */ > > -void > > -drm_gpuvm_destroy(struct drm_gpuvm *gpuvm) > > +static void > > +drm_gpuvm_fini(struct drm_gpuvm *gpuvm) > > { > > gpuvm->name = NULL; > > @@ -790,7 +785,33 @@ drm_gpuvm_destroy(struct drm_gpuvm *gpuvm) > > drm_gem_object_put(gpuvm->r_obj); > > } > > -EXPORT_SYMBOL_GPL(drm_gpuvm_destroy); > > + > > +static void > > +drm_gpuvm_free(struct kref *kref) > > +{ > > + struct drm_gpuvm *gpuvm = container_of(kref, struct drm_gpuvm, kref); > > + > > + if (drm_WARN_ON(gpuvm->drm, !gpuvm->ops->vm_free)) > > + return; > > + > > + drm_gpuvm_fini(gpuvm); > > + > > + gpuvm->ops->vm_free(gpuvm); > > +} > > + > > +/** > > + * drm_gpuvm_bo_put() - drop a struct drm_gpuvm reference > > + * @gpuvm: the &drm_gpuvm to release the reference of > > + * > > + * This releases a reference to @gpuvm. > > + */ > > +void > > +drm_gpuvm_put(struct drm_gpuvm *gpuvm) > > +{ > > + if (gpuvm) > > + kref_put(&gpuvm->kref, drm_gpuvm_free); > > +} > > +EXPORT_SYMBOL_GPL(drm_gpuvm_put); > > static int > > __drm_gpuva_insert(struct drm_gpuvm *gpuvm, > > @@ -843,7 +864,7 @@ drm_gpuva_insert(struct drm_gpuvm *gpuvm, > > if (unlikely(!drm_gpuvm_range_valid(gpuvm, addr, range))) > > return -EINVAL; > > - return __drm_gpuva_insert(gpuvm, va); > > + return __drm_gpuva_insert(drm_gpuvm_get(gpuvm), va); > > } > > EXPORT_SYMBOL_GPL(drm_gpuva_insert); > > @@ -876,6 +897,7 @@ drm_gpuva_remove(struct drm_gpuva *va) > > } > > __drm_gpuva_remove(va); > > + drm_gpuvm_put(va->vm); > > } > > EXPORT_SYMBOL_GPL(drm_gpuva_remove); > > diff --git a/drivers/gpu/drm/nouveau/nouveau_uvmm.c b/drivers/gpu/drm/nouveau/nouveau_uvmm.c > > index 54be12c1272f..cb2f06565c46 100644 > > --- a/drivers/gpu/drm/nouveau/nouveau_uvmm.c > > +++ b/drivers/gpu/drm/nouveau/nouveau_uvmm.c > > @@ -1780,6 +1780,18 @@ nouveau_uvmm_bo_unmap_all(struct nouveau_bo *nvbo) > > } > > } > > +static void > > +nouveau_uvmm_free(struct drm_gpuvm *gpuvm) > > +{ > > + struct nouveau_uvmm *uvmm = uvmm_from_gpuvm(gpuvm); > > + > > + kfree(uvmm); > > +} > > + > > +static const struct drm_gpuvm_ops gpuvm_ops = { > > + .vm_free = nouveau_uvmm_free, > > +}; > > + > > int > > nouveau_uvmm_ioctl_vm_init(struct drm_device *dev, > > void *data, > > @@ -1830,7 +1842,7 @@ nouveau_uvmm_ioctl_vm_init(struct drm_device *dev, > > NOUVEAU_VA_SPACE_END, > > init->kernel_managed_addr, > > init->kernel_managed_size, > > - NULL); > > + &gpuvm_ops); > > /* GPUVM takes care from here on. */ > > drm_gem_object_put(r_obj); > > @@ -1849,8 +1861,7 @@ nouveau_uvmm_ioctl_vm_init(struct drm_device *dev, > > return 0; > > out_gpuvm_fini: > > - drm_gpuvm_destroy(&uvmm->base); > > - kfree(uvmm); > > + drm_gpuvm_put(&uvmm->base); > > out_unlock: > > mutex_unlock(&cli->mutex); > > return ret; > > @@ -1902,7 +1913,6 @@ nouveau_uvmm_fini(struct nouveau_uvmm *uvmm) > > mutex_lock(&cli->mutex); > > nouveau_vmm_fini(&uvmm->vmm); > > - drm_gpuvm_destroy(&uvmm->base); > > - kfree(uvmm); > > + drm_gpuvm_put(&uvmm->base); > > mutex_unlock(&cli->mutex); > > } > > diff --git a/include/drm/drm_gpuvm.h b/include/drm/drm_gpuvm.h > > index 0c2e24155a93..4e6e1fd3485a 100644 > > --- a/include/drm/drm_gpuvm.h > > +++ b/include/drm/drm_gpuvm.h > > @@ -247,6 +247,11 @@ struct drm_gpuvm { > > struct list_head list; > > } rb; > > + /** > > + * @kref: reference count of this object > > + */ > > + struct kref kref; > > + > > /** > > * @kernel_alloc_node: > > * > > @@ -273,7 +278,23 @@ void drm_gpuvm_init(struct drm_gpuvm *gpuvm, const char *name, > > u64 start_offset, u64 range, > > u64 reserve_offset, u64 reserve_range, > > const struct drm_gpuvm_ops *ops); > > -void drm_gpuvm_destroy(struct drm_gpuvm *gpuvm); > > + > > +/** > > + * drm_gpuvm_get() - acquire a struct drm_gpuvm reference > > + * @gpuvm: the &drm_gpuvm to acquire the reference of > > + * > > + * This function acquires an additional reference to @gpuvm. It is illegal to > > + * call this without already holding a reference. No locks required. > > + */ > > +static inline struct drm_gpuvm * > > +drm_gpuvm_get(struct drm_gpuvm *gpuvm) > > +{ > > + kref_get(&gpuvm->kref); > > + > > + return gpuvm; > > +} > > + > > +void drm_gpuvm_put(struct drm_gpuvm *gpuvm); > > bool drm_gpuvm_range_valid(struct drm_gpuvm *gpuvm, u64 addr, u64 range); > > bool drm_gpuvm_interval_empty(struct drm_gpuvm *gpuvm, u64 addr, u64 range); > > @@ -673,6 +694,14 @@ static inline void drm_gpuva_init_from_op(struct drm_gpuva *va, > > * operations to drivers. > > */ > > struct drm_gpuvm_ops { > > + /** > > + * @vm_free: called when the last reference of a struct drm_gpuvm is > > + * dropped > > + * > > + * This callback is mandatory. > > + */ > > + void (*vm_free)(struct drm_gpuvm *gpuvm); > > + > > /** > > * @op_alloc: called when the &drm_gpuvm allocates > > * a struct drm_gpuva_op >