Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp109959pxb; Wed, 20 Jan 2021 02:28:07 -0800 (PST) X-Google-Smtp-Source: ABdhPJxFwUXpcQzFYDuSNxVs6DdAR/d1JpBc5nCy+5TQnq3ma+Ge1f9rWtYCveYXpmd++bHWClCp X-Received: by 2002:a17:906:a082:: with SMTP id q2mr1473262ejy.483.1611138487439; Wed, 20 Jan 2021 02:28:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611138487; cv=none; d=google.com; s=arc-20160816; b=dgO9PZ0U0hM4+MoawUX58p8aXQk24H9SLpDJlgpnFdbMzRRzbQG+2pqpyZX8iwx49Q WCYSwFcnykaG+hy+Jc3lbAaPzSJbSnDjntm6qRj6wQKUVHeDALpSQPr6FlZHFD/pN7Mp jYRJgshpndRYU+qajuPS3ccDilxugKfOomOUMIqyowd/vQcsB2UKkXHPBf/55zN4O4FG FWnz10sEWD/YBiUN2UkxC0vqWowrB0DfyjMYLSAYdUZlJ2odZjo/yaAytL2TePMd9Iri VXt9c42L00EF+/2OYni+3LOqsEhYdBAqOMFlTn9taKPHMfYSp6aWwX8rvuINVY1NSesF LVFw== 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=jnHw/9ZfOUdDPDIVRz58fMDvzkElIsdRxNOVrSA30u0=; b=MY6SXPR3684MSs0L16vHH7WRR9g4pMiz5YaSq8S4mfLh55S+8mS2v0bhrSh6TwMNIA TtQnBWV8GVDwOtVCChWyIpNLswRca3OlTrtF8eNtsRCBcX+7MhE7THUeYg3evqHk7Fxf BtZo3tws0KIYUHHLcL+oM0ap9HlttF5EFk9cP+KUky0qFAXZv180HdfDr4Z+oU0BCqTR rzUnyrdutwToFvmr4I9wiF+RPJ4oHaHHPLLd+11+UFaXPcJGc2Z32VhEf9KT1KmZlIQO WLzpnWuglF1f4CKPxrBrXr3MLpVWuGgTT5Yhmix7cdfnBhLRO8B28dIEAJ7rypJts0IT nJVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=SGoQ9256; 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 b18si704796edw.51.2021.01.20.02.27.14; Wed, 20 Jan 2021 02:28:07 -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=SGoQ9256; 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 S1727380AbhATKND (ORCPT + 99 others); Wed, 20 Jan 2021 05:13:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729522AbhATJM0 (ORCPT ); Wed, 20 Jan 2021 04:12:26 -0500 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5126EC061757 for ; Wed, 20 Jan 2021 01:11:37 -0800 (PST) Received: by mail-wm1-x329.google.com with SMTP id e15so2151876wme.0 for ; Wed, 20 Jan 2021 01:11:37 -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=jnHw/9ZfOUdDPDIVRz58fMDvzkElIsdRxNOVrSA30u0=; b=SGoQ9256qHzwYxJkdrl57cIN/rchQtnhGfDJQqGJl6DxiHNa08K0KF4sF3C0FAf2A7 YLk9qmb/uL5XHhHwCi5Iajxw6DXC6CyegFsLorVcMW701MtboWCBTXBqCUNuBfIbJ9eE t3oQ+hDZvWjkNetM7HNtZSBdWv6X+pzHEvJwE= 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=jnHw/9ZfOUdDPDIVRz58fMDvzkElIsdRxNOVrSA30u0=; b=rhg8sLHMOV+9lFs/Cfu8tvDBNMzgz7+6OXC5iRB7p8kZLty/jxiJsU0AQB4w926e8P 9+kDnQse6qauaePxvMZnaf5RKCZ25IP2vw/Ai5Xt6vvOGwUehGQ9BGGdZt3PatoZfW0y EAB3uDtSDTmQeaG582uwpFyf2Dm9gYIdqSu/RS+XrGC3/SxfUUVcLmXvNISfMsLiThCu oj7s8UhXoWjusu0vV5wDlaYEEUtrBgF80Xiji47Pbx7qSUkVpUGiCdKbMrVPILxc8wtA eJ3MRekpvs+cYo0Q5FyYaiDle5PgYKkuqkSQJvFDAhw/uBmGw4PwrWH1lBBEVDkoStA8 7MPw== X-Gm-Message-State: AOAM531t70dI5e9rm3FIveE+pdL8x9K13wksjDljbJkIFbhjwYt/+TMS PCx/oxbHaha7BVxp+h3CwCfv1O1i9jexZg== X-Received: by 2002:a05:600c:258:: with SMTP id 24mr3297878wmj.161.1611133895972; Wed, 20 Jan 2021 01:11:35 -0800 (PST) Received: from phenom.ffwll.local ([2a02:168:57f4:0:efd0:b9e5:5ae6:c2fa]) by smtp.gmail.com with ESMTPSA id b64sm2775341wmb.26.2021.01.20.01.11.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Jan 2021 01:11:34 -0800 (PST) Date: Wed, 20 Jan 2021 10:11:33 +0100 From: Daniel Vetter To: Yiwei Zhang Cc: Daniel Vetter , David Airlie , Gerd Hoffmann , dri-devel , "open list:VIRTIO CORE, NET..." , Linux Kernel Mailing List , Android Kernel Team Subject: Re: [PATCH v2] 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 , Android Kernel Team References: <20210118234057.270930-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 Tue, Jan 19, 2021 at 11:08:12AM -0800, Yiwei Zhang wrote: > On Mon, Jan 18, 2021 at 11:03 PM Daniel Vetter wrote: > > > > On Tue, Jan 19, 2021 at 12:41 AM Yiwei Zhang wrote: > > > > > > On the success of virtio_gpu_object_create, add size of newly allocated > > > bo to the tracled total_mem. In drm_gem_object_funcs.free, after the gem > > > bo lost its last refcount, subtract the bo size from the tracked > > > total_mem if the original underlying memory allocation is successful. > > > > > > Signed-off-by: Yiwei Zhang > > > > Isn't this something that ideally we'd for everyone? Also tracepoint > > for showing the total feels like tracepoint abuse, usually we show > > totals somewhere in debugfs or similar, and tracepoint just for what's > > happening (i.e. which object got deleted/created). > > > > What is this for exactly? > > -Daniel > > > > > --- > > > drivers/gpu/drm/virtio/Kconfig | 1 + > > > drivers/gpu/drm/virtio/virtgpu_drv.h | 4 ++++ > > > drivers/gpu/drm/virtio/virtgpu_object.c | 19 +++++++++++++++++++ > > > 3 files changed, 24 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..7c60e7486bc4 100644 > > > --- a/drivers/gpu/drm/virtio/virtgpu_drv.h > > > +++ b/drivers/gpu/drm/virtio/virtgpu_drv.h > > > @@ -249,6 +249,10 @@ struct virtio_gpu_device { > > > spinlock_t resource_export_lock; > > > /* protects map state and host_visible_mm */ > > > spinlock_t host_visible_lock; > > > + > > > +#ifdef CONFIG_TRACE_GPU_MEM > > > + atomic64_t total_mem; > > > +#endif > > > }; > > > > > > struct virtio_gpu_fpriv { > > > diff --git a/drivers/gpu/drm/virtio/virtgpu_object.c b/drivers/gpu/drm/virtio/virtgpu_object.c > > > index d69a5b6da553..1e16226cebbe 100644 > > > --- a/drivers/gpu/drm/virtio/virtgpu_object.c > > > +++ b/drivers/gpu/drm/virtio/virtgpu_object.c > > > @@ -25,12 +25,29 @@ > > > > > > #include > > > #include > > > +#ifdef CONFIG_TRACE_GPU_MEM > > > +#include > > > +#endif > > > > > > #include "virtgpu_drv.h" > > > > > > static int virtio_gpu_virglrenderer_workaround = 1; > > > module_param_named(virglhack, virtio_gpu_virglrenderer_workaround, int, 0400); > > > > > > +#ifdef CONFIG_TRACE_GPU_MEM > > > +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(0, 0, total_mem); > > > +} > > > +#else > > > +static inline void virtio_gpu_trace_total_mem(struct virtio_gpu_device *, s64) > > > +{ > > > +} > > > +#endif > > > + > > > int virtio_gpu_resource_id_get(struct virtio_gpu_device *vgdev, uint32_t *resid) > > > { > > > if (virtio_gpu_virglrenderer_workaround) { > > > @@ -104,6 +121,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 +283,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.284.gd98b1dd5eaa7-goog > > > > > > > > > -- > > Daniel Vetter > > Software Engineer, Intel Corporation > > http://blog.ffwll.ch > > Thanks for your reply! Android Cuttlefish virtual platform is using > the virtio-gpu driver, and we currently are carrying this small patch > at the downstream side. This is essential for us because: > (1) Android has deprecated debugfs on production devices already > (2) Android GPU drivers are not DRM based, and this won't change in a > short term. > > Android relies on this tracepoint + eBPF to make the GPU memory totals > available at runtime on production devices, which has been enforced > already. Not only game developers can have a reliable kernel total GPU > memory to look at, but also Android leverages this to take GPU memory > usage out from the system lost ram. > > I'm not sure whether the other DRM drivers would like to integrate > this tracepoint(maybe upstream drivers will move away from debugfs > later as well?), but at least we hope virtio-gpu can take this. There's already another proposal from Android people for tracking dma-buf (in dma-buf heaps/ion) usage. I think we need something which is overall integrated, otherwise we have a complete mess of partial solutions. Also there's work going on to add cgroups support to gpu drivers (pushed by amd and intel folks, latest rfc have been quite old), so that's another proposal for gpu memory usage tracking. Also for upstream we need something which works with upstream gpu drivers (even if you don't end up using that in shipping products). So that's another reason maybe why a quick hack in the virtio gpu driver isn't the best approach here. I guess a good approach would be if Android at least can get to something unified (gpu driver, virtio-gpu, dma-buf heaps), and then we need to figure out how to mesh that with the cgroups side somehow. Also note that at least on dma-buf we already have some other debug features (for android), so an overall "how does this all fit together" would be good. -Daniel > > Many thanks! > Yiwei -- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch