Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp915501pxb; Fri, 22 Jan 2021 02:26:54 -0800 (PST) X-Google-Smtp-Source: ABdhPJxVbEBfhANoiYEtmUMBxXMTSPmnddBxxyFwLaq1FTCp9LM+pLBOMQ+trMbj3oxX49xyIo0d X-Received: by 2002:a17:906:25db:: with SMTP id n27mr2523440ejb.552.1611311214174; Fri, 22 Jan 2021 02:26:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611311214; cv=none; d=google.com; s=arc-20160816; b=eNFw62sdW9liWXJ6i9NgUDGmPTYNcd/9cv+M6vkhY1cnuF4+mLPHY9/TMZVByrp3GL zlI5nx+7vB/PDICRUMLjld7+gO1v+fTh2EAa4s1rWiN74sIlWHk5nBSm4gQHBPCcjbKS +SqxYSxa03E4LIQMrQnzkIFl/HkcDZsgCOtmiD8wCh7JrTD9UtevZ2Y3YXigPYx77wge m19bYSzCrTD2wrsyz7e3aH6bhkSK2GEJPGt04bHd/4Y9Pi6mm1ejE1cgeHDmCTO3l87r xiJE38M0MLb2eLvb8RiHobluk17HglYlYW43jNCx9k6ZNXVi9xEc1cAF72pzNrBmYhSs HkbA== 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-disposition:mime-version :references:mail-followup-to:message-id:subject:cc:to:from:date :dkim-signature; bh=pxR73uYax2G5yfjba411z30jS6rXBdckdXhkr2akbsA=; b=kfeEAarH0WZwLxAaDso1GWa5Rg8HusIW3IRkrtq3HHz8ikruWBrmLvM0R7aML28+vM E7CYWMTMsXagd3h6gsR4Qi88+VsdGzmIDtkD/60knEnzE33REy4uaehTUSlTtgDY/mBZ SYmru59hXBdbdHO4PytJ6vVixYRSgc600jeIJ1vm1DJ7lKxsUV4dCyJOQbYSVT1pnUCd nRCq8gHV6HZOF+8R2fQCmKMojPNd2ucYuqr8qFZgDsS+A1RiaGucFqDBYk5JoB+L+//o t0nEnDTOLIbqWiBtr0gMwGK+IRX3XZzWBWEY7RDox3oiuixz2aHCthk6PMTrjcSPLfIX py9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=X5WwYFfs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s13si3190682eds.57.2021.01.22.02.26.30; Fri, 22 Jan 2021 02:26:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=X5WwYFfs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727944AbhAVKXo (ORCPT + 99 others); Fri, 22 Jan 2021 05:23:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727814AbhAVKDp (ORCPT ); Fri, 22 Jan 2021 05:03:45 -0500 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06A82C061786 for ; Fri, 22 Jan 2021 02:03:05 -0800 (PST) Received: by mail-wm1-x32e.google.com with SMTP id m2so3751465wmm.1 for ; Fri, 22 Jan 2021 02:03:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=date:from:to:cc:subject:message-id:mail-followup-to:references :mime-version:content-disposition:in-reply-to; bh=pxR73uYax2G5yfjba411z30jS6rXBdckdXhkr2akbsA=; b=X5WwYFfsYNJfDQh92DGtNFimXbpUN+GxqfLE1NYzEe3/wu5PgFoLIvm7+ki/7buNkl kZ7RdFYAlQPqXf/58zvq7XnDgQ1lbSBY6MD18BNTiXhDewgngp7ByIGO1awg/WVpMYos IfC9x4aac9zTPv6TJxwkXPhaYSyoZUf4qrM0k= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id :mail-followup-to:references:mime-version:content-disposition :in-reply-to; bh=pxR73uYax2G5yfjba411z30jS6rXBdckdXhkr2akbsA=; b=QIWFMT+DH+CSz9+4NrUZQFTsCsanOdmhhodHBg/PYiRIqsYc16yu8VvCkMNyBtFFHA Ki/x00Fg+1WXRlJ750jJ6zPnnix5THXFgLcRzzQjb2DqH0g1Ya/cNOo3PxerCfkojfBn bHmbhqB+sRCnC9ucjz6B0He846fBTMHiF3IqN1F/j5DMnSLCJtmijmfY/MnndmjcN3gr hA+oYT14iHbsiYJ+4JBCfHiFu8iAQxiYH1iN5AT5Z0XqtkFfH4bjNa4c7oRErpKogkVa V/VOOWi/6rDhtKIyBGGU86e+tPfIveVur2IJFCTkTzPAYVXRsuIg2jlYldbr9YhY+U01 1j7Q== X-Gm-Message-State: AOAM533H38rPEt09Ft4b4bd6AM88Hrjum8NfLKWE6q6pPoZVsRO2CI9e y/okaUYSu4mi9haCtJpNtWzrBXS3q40e2B4o X-Received: by 2002:a05:600c:215:: with SMTP id 21mr3149956wmi.54.1611309783694; Fri, 22 Jan 2021 02:03:03 -0800 (PST) Received: from phenom.ffwll.local ([2a02:168:57f4:0:efd0:b9e5:5ae6:c2fa]) by smtp.gmail.com with ESMTPSA id o14sm10491607wmc.28.2021.01.22.02.03.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Jan 2021 02:03:03 -0800 (PST) Date: Fri, 22 Jan 2021 11:03:01 +0100 From: Daniel Vetter To: Yiwei Zhang Cc: David Airlie , Gerd Hoffmann , Daniel Vetter , dri-devel , "open list:VIRTIO CORE, NET..." , Linux Kernel Mailing List , kernel-team Subject: Re: [PATCH v4] drm/virtio: Track total GPU memory for virtio driver Message-ID: Mail-Followup-To: Yiwei Zhang , David Airlie , Gerd Hoffmann , dri-devel , "open list:VIRTIO CORE, NET..." , Linux Kernel Mailing List , kernel-team References: <20210122053159.1720274-1-zzyiwei@android.com> <20210122054011.1722954-1-zzyiwei@android.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Operating-System: Linux phenom 5.7.0-1-amd64 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jan 21, 2021 at 11:58:22PM -0800, Yiwei Zhang wrote: > On Thu, Jan 21, 2021 at 9:40 PM Yiwei Zhang wrote: > > > > On the success of virtio_gpu_object_create, add size of newly allocated > > bo to the tracked total_mem. In drm_gem_object_funcs.free, after the gem > > bo loses its last refcount, subtract the bo size from the tracked > > total_mem if the original underlying memory allocation is successful. > > > > It's more accurate to do this in device driver layer to best match when > > the underlying resource gets allocated and destroyed during tracing. > > > > Signed-off-by: Yiwei Zhang > > --- > > drivers/gpu/drm/virtio/Kconfig | 1 + > > drivers/gpu/drm/virtio/virtgpu_drv.h | 2 ++ > > drivers/gpu/drm/virtio/virtgpu_object.c | 11 +++++++++++ > > 3 files changed, 14 insertions(+) > > > > diff --git a/drivers/gpu/drm/virtio/Kconfig b/drivers/gpu/drm/virtio/Kconfig > > index b925b8b1da16..e103b7e883b1 100644 > > --- a/drivers/gpu/drm/virtio/Kconfig > > +++ b/drivers/gpu/drm/virtio/Kconfig > > @@ -5,6 +5,7 @@ config DRM_VIRTIO_GPU > > select DRM_KMS_HELPER > > select DRM_GEM_SHMEM_HELPER > > select VIRTIO_DMA_SHARED_BUFFER > > + select TRACE_GPU_MEM > > help > > This is the virtual GPU driver for virtio. It can be used with > > QEMU based VMMs (like KVM or Xen). > > diff --git a/drivers/gpu/drm/virtio/virtgpu_drv.h b/drivers/gpu/drm/virtio/virtgpu_drv.h > > index 6a232553c99b..c5622f9b591f 100644 > > --- a/drivers/gpu/drm/virtio/virtgpu_drv.h > > +++ b/drivers/gpu/drm/virtio/virtgpu_drv.h > > @@ -249,6 +249,8 @@ struct virtio_gpu_device { > > spinlock_t resource_export_lock; > > /* protects map state and host_visible_mm */ > > spinlock_t host_visible_lock; > > + > > + atomic64_t total_mem; > > }; > > > > struct virtio_gpu_fpriv { > > diff --git a/drivers/gpu/drm/virtio/virtgpu_object.c b/drivers/gpu/drm/virtio/virtgpu_object.c > > index d69a5b6da553..e2251fc41509 100644 > > --- a/drivers/gpu/drm/virtio/virtgpu_object.c > > +++ b/drivers/gpu/drm/virtio/virtgpu_object.c > > @@ -25,12 +25,21 @@ > > > > #include > > #include > > +#include > > > > #include "virtgpu_drv.h" > > > > static int virtio_gpu_virglrenderer_workaround = 1; > > module_param_named(virglhack, virtio_gpu_virglrenderer_workaround, int, 0400); > > > > +static inline void virtio_gpu_trace_total_mem(struct virtio_gpu_device *vgdev, > > + s64 delta) > > +{ > > + u64 total_mem = atomic64_add_return(delta, &vgdev->total_mem); > > + > > + trace_gpu_mem_total(vgdev->ddev->primary->index, 0, total_mem); > > +} > > + > > int virtio_gpu_resource_id_get(struct virtio_gpu_device *vgdev, uint32_t *resid) > > { > > if (virtio_gpu_virglrenderer_workaround) { > > @@ -104,6 +113,7 @@ static void virtio_gpu_free_object(struct drm_gem_object *obj) > > struct virtio_gpu_device *vgdev = bo->base.base.dev->dev_private; > > > > if (bo->created) { > > + virtio_gpu_trace_total_mem(vgdev, -(obj->size)); > > virtio_gpu_cmd_unref_resource(vgdev, bo); > > virtio_gpu_notify(vgdev); > > /* completion handler calls virtio_gpu_cleanup_object() */ > > @@ -265,6 +275,7 @@ int virtio_gpu_object_create(struct virtio_gpu_device *vgdev, > > virtio_gpu_object_attach(vgdev, bo, ents, nents); > > } > > > > + virtio_gpu_trace_total_mem(vgdev, shmem_obj->base.size); > > *bo_ptr = bo; > > return 0; > > > > -- > > 2.30.0.280.ga3ce27912f-goog > > > > Re Gerd and Daniel: > > I'm not sure why we want to couple this patch too much with the > dma-bufs tracking. The tracepoint added here itself is pretty useful > for tracking gem bo total usage in virtio gpu upon tracing. The > original purpose for integrating this tracepoint in all Android gpu > kernel drivers is to just track total gpu memory usage and serve the > accurate data to game developers in a much easier way. It's something > they can rely on for robust testing and regression monitoring. > > The only overlap with the dma-buf side is when we export a bo via > prime to a dma-buf. But still, the total here is already useful for > this particular device. Using which approach to account for the > overlap wouldn't block this small integration from my understanding. > > Besides, there's no plan for adding per-process gem total tracking in > virtio-gpu at this moment. This patch should be light enough to carry > without worrying about tech debt I believe. The tracepoint is clearly more generic than just what you implement here, to support the full use cases on Android's closed stacks. And it is uapi. Tech debt isn't measured in lines of code, but in how expensive it's going to be to fix up the mess in the future. uapi is expensive no matter how few lines are used to implement it. So yeah this needs to be properly thought out, properly implemented (not just on the virtual demo stack but something that looks like actual production stack), with open drivers, proper alignment with other efforts like tracking memory with cgroups, and the interactions with dma-buf tracking resolved, and igt testcases (this is meant to be generic after all), and at least solid proposals for rolling this out across the drm drivers, and ... In other words, new uapi needs to be done right. -Daniel -- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch