Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp119707pxb; Wed, 20 Jan 2021 02:47:29 -0800 (PST) X-Google-Smtp-Source: ABdhPJyijyICHPF6jPEZMQ+IYh8uCsliqDxGp9Y/JYVwGmSB9wJ/qwYIKDK8/Vj4bAiGMBxB69QB X-Received: by 2002:a17:907:a077:: with SMTP id ia23mr5471300ejc.434.1611139648918; Wed, 20 Jan 2021 02:47:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611139648; cv=none; d=google.com; s=arc-20160816; b=O6SW2QexIWO3inbQySwrzgSpVaK+s/Fpul5m4KElQsAVmSMzjV1Pqfrx0QX51EqqN6 YUeRuSUmP8JqaJktH6CUw134QmUtTKyWukiFuuDgmytVaWp/zPir1vBjhe/I8vxGRMTl qwjyMHTkWW9KTGHGESkhHiHnHdv1TwKNbd4g6p4HpU99yNHC5kEsJoGVAzVND0PDKoFw GeXvzSLBRUZhGqyYM00B4yWjic61CXZxiuls5Fzb6Par9XWRduqWJdoPpVokJ0j3XVwX +qKEfzQKfhjZWC5YqcNj7SOt8nU31suaHnqzEcjednrKfOm9HgL8ccE30M/hXVSk7oTX UcQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=IPCL/E40PTZ+d1dIo1i9GsY75jeKId+u+Yf9cEQriqo=; b=MA2j5E/vQmhHI1Vry+p+lTecjqpUP7DrdZMdTv0E4I3gthj/slaj8nW3zksDAc1IBn HVE6GkPbKkqgWp1C/nyv3DAWpcVnLvEpnliqOq9q94Qt50QRXC86zLJKHbnEyG3C4VDx gVn9oFrdZptItv/OQpTmNYh9FPKoCJIZOOv9hURTStfn76WEJ2I9IdzhghAYgXQ6BL9K 0J/eq2Pdj4dD0VEdn8ePstMK3LjwTkWTYnkoek3rhJOQr2YL9c5GmGmcoZCneC1kjDqY U/fyiw9NH3z3vqcavbw4es9jwW3GVIowiG2BQuYB6ywKPweOEKTedAyLqWfmqjVFlYhJ k/ZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@android.com header.s=20161025 header.b=ELKMR37q; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=android.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t12si719984edc.228.2021.01.20.02.47.04; Wed, 20 Jan 2021 02:47:28 -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=@android.com header.s=20161025 header.b=ELKMR37q; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=android.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732291AbhATKSK (ORCPT + 99 others); Wed, 20 Jan 2021 05:18:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731689AbhATJv4 (ORCPT ); Wed, 20 Jan 2021 04:51:56 -0500 Received: from mail-qk1-x734.google.com (mail-qk1-x734.google.com [IPv6:2607:f8b0:4864:20::734]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B3C9CC061575 for ; Wed, 20 Jan 2021 01:51:15 -0800 (PST) Received: by mail-qk1-x734.google.com with SMTP id 19so1350332qkh.3 for ; Wed, 20 Jan 2021 01:51:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=android.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=IPCL/E40PTZ+d1dIo1i9GsY75jeKId+u+Yf9cEQriqo=; b=ELKMR37qP4HjvYt3xdnU5n2c1UcX3KMLjCYXObgPZLj6ESWnb4ZgkMphLzc222r/Co dUcGbCenZaXjkTgAShIzEIJTYa+FOpq03y/3UB8UHIxi50sRcK9KT5L6Ike/kep7gF2s BBQzrrUZAWFf97RIV1H+tLKcz1LHOb+Jo9e2bqGLvObegDoUyuyiRzS6mbnqQxtSw1Ea H24/GRAfYn/6n1b4OKlJ6gvBnuSeILcz95gWMYF2xYqLe7Q6GAd2FROuH5R2T87gZAG9 zTTrkPQLLdnBuTtr0c4irhMi51XV+x/GToNg+DlGwWzak0bNdApaGa7bL2QyXl9l92Gm aWQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=IPCL/E40PTZ+d1dIo1i9GsY75jeKId+u+Yf9cEQriqo=; b=hOD7NBcFa6njQmzjZTVHk3FaRH+9rJLiDdV5OV0IBOg8nBgH6m60i5EGa2xehu4Qgx JjvoyMBC9A/gPZYMBdlLgBjthYyoPj6ATTCAJZskRt6X0IjLklndP0zNK2IinDO0q8c0 iuuWhjs1HvPpFHvizER/p8yt1pOjXoLeRPtrL1Qb8pnqF3qS1hTog8snNNGOVoAftStO qISSC2bh5E3mJ32C0kozGsvLsDAVPdiSn88nIe9cJP+E3DmUWQAOBPUmfKEIPWgaYsgO 11tKhTlaXHBFtuGeZKapyJ619tiVhgMmnIuWv4C3r9sl5jNvwiE24Y7Ip+CUqRjZKBxN d+7g== X-Gm-Message-State: AOAM532kLokYgjDxLsWrueOx/7vac4EJD03DBm2H9XgYPiwQfUrTyaoT PCVy1vrX4Ur9O5emJ5W/MxtHrymY1YG9U3kmDE/XX8fojNdQOA== X-Received: by 2002:a37:a241:: with SMTP id l62mr8291059qke.482.1611136274602; Wed, 20 Jan 2021 01:51:14 -0800 (PST) MIME-Version: 1.0 References: <20210118234057.270930-1-zzyiwei@android.com> In-Reply-To: From: =?UTF-8?B?WWl3ZWkgWmhhbmfigI4=?= Date: Wed, 20 Jan 2021 01:51:03 -0800 Message-ID: Subject: Re: [PATCH v2] drm/virtio: Track total GPU memory for virtio driver To: Yiwei Zhang , David Airlie , Gerd Hoffmann , dri-devel , "open list:VIRTIO CORE, NET..." , Linux Kernel Mailing List , Android Kernel Team Cc: Daniel Vetter Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jan 20, 2021 at 1:11 AM Daniel Vetter wrote: > > 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 The entire story is to better explain Android system memory usage. They fit together so that the dma-bufs overlap can be removed. Android GPU vendors have integrated this tracepoint to track gpu memory usage total(mapped into the gpu address space), which consists of below: (1) directly allocated via physical page allocator (2) imported external memory backed by dma-bufs (3) allocated exportable memory backed by dma-bufs Our Android kernel team is leading the other side of effort to help remove the dma-bufs overlap(those mapped into a gpu device) as a joint effort, so that we can accurately explain the memory usage of the entire Android system. For virtio-gpu, since that's used by our reference platform Cuttlefish(Cloud Android), we have to integrate the same tracepoint as well to enforce the use of this tracepoint and the eBPF stuff built on top to support runtime query of gpu memory on production devices. For virtio-gpu at this moment, we only want to track GEM allocations since PRIME import is currently not supported/used in Cuttlefish. That's all we are doing in this small patch. Best, Yiwei