Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp5650304rwl; Tue, 11 Apr 2023 08:12:57 -0700 (PDT) X-Google-Smtp-Source: AKy350bx2AY8NDuIk95VM/pgKLYnV4GvLfiiRtunFuA3JRlA9s+5QPXJqy34IdCvkrYpe3LL5yj0 X-Received: by 2002:a17:906:2f8a:b0:933:1134:be1e with SMTP id w10-20020a1709062f8a00b009331134be1emr9849291eji.53.1681225977548; Tue, 11 Apr 2023 08:12:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681225977; cv=none; d=google.com; s=arc-20160816; b=jMGCF2G5B8HDTsXlXz4ZJqvvXNgcAe/HuLo9fqHV3guFrnAHAvzQNBEvAD+4eBS/aO JRbLzjQH5UXyEXeYg+yBJxmdngVn/ymBCJ5TKAOB25IGuix2ppIEWBtEW3h6fpWR+FD3 Le+uY5uAhqjqBbsxKt6zT7sphS6lwd/D87tsPde2bNI3JHjlU6AnCqBJEge/lBGY+b+/ k9Jik2Ydtsbc98OLFzcEP1uytloxuSq4oHcYUr8nkjrVx8duV/l2TzGRgVa2rr423BD5 3Z/YGW2a1h/6WMCQrhIH6GMKH4IIi6kRBg91EiCnjjzW9/do+wfvQtCYsiv9gz/uVhlM ilxQ== 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:mail-followup-to :message-id:subject:cc:to:from:date:dkim-signature; bh=xkBc8y85Toy8UTcXfQtWXbFA44zHzGMNAbB/qPFb8v0=; b=MstPqPzmzTgv1vsY2OpGfEyqSDTXbwZtV+EOiyvIb6/It1m6KZ9J6wHt/akxe582sL jGiuQ42py3fmzKV/ZdH41HSCao01evrbtyezKbOHexuiuZfdUzowhTlL1kkfsYsfMONH IhWpgIwHWj+qUwuFG1oAzMXo1IAUrCvvV+NWdA7g5/pQwrNuEeCGBj/VuZiTdAUrazuD iPdX3TJWMI0W1i8gMI+74GeuAo/PVoFx58GZWefYJYipXlCJBjqcFxvfymMFQlHJYf5w jPwo/83MYii3bFhl57j6yjIJfly/8kV3lMwd4ILVEgHWhCMv3FPr4Ksw0eWqwunNrp4y NEXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=HudeJ0Hf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gb4-20020a170907960400b00948aae5e3d2si13478075ejc.122.2023.04.11.08.12.29; Tue, 11 Apr 2023 08:12:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=HudeJ0Hf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230490AbjDKPLm (ORCPT + 99 others); Tue, 11 Apr 2023 11:11:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40710 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230368AbjDKPL2 (ORCPT ); Tue, 11 Apr 2023 11:11:28 -0400 Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A85C45FCA for ; Tue, 11 Apr 2023 08:11:02 -0700 (PDT) Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-3f05f61adbeso1596075e9.0 for ; Tue, 11 Apr 2023 08:11:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; t=1681225861; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:mail-followup-to:message-id:subject:cc:to :from:date:from:to:cc:subject:date:message-id:reply-to; bh=xkBc8y85Toy8UTcXfQtWXbFA44zHzGMNAbB/qPFb8v0=; b=HudeJ0HfHwO6pVrBeBRsojqT8vHIe9KBuB5ATmGTODShfva0z04Os9nVeGorEN/ige peI5SidBvXShYK+9Au/tW2hkiGsZo9/9j2P8Z5Eh94DyFtIl4RKockTBdfh8h6xdyqG2 Alm+HV8cVj5oSC775K3aKn2VX9ix8xJEuEtp8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681225861; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:mail-followup-to:message-id:subject:cc:to :from:date:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=xkBc8y85Toy8UTcXfQtWXbFA44zHzGMNAbB/qPFb8v0=; b=1S3kc5nfnYB/8RBRssgyhBV9BHUnB7K5VfM8qtJzrWYyABEY7LnaFhNoCjZBbX+vLD d1y3XL/Oi1jSmDP6Acmqa60zGJbMkEB/l/WS568OmHQ1o9r5bmUAT/FlyS9fN5Yx3NSA WWMqBvUVeUZdbgMDzMtRwizdmj/lJeObrdlTu4SOAHGoe0C+c5e6yveG68izV6OoKA94 GfC9nIv3fJECRVbRl0AuBaFuoqKYCvp0sx4aEMMjRBsitY0I8yz6h+OMKOwInqkdhGtN YxtBIt52OcENFwwC8482S4rBfYaKR9CqZHJ4/8WD+xdun+EuUPcTq04GRx8noRpCi1qT ZLjA== X-Gm-Message-State: AAQBX9d64ekf19cjuk+YTyslP1wYfufvHaqczUicDbPcrlwt5ABZxreL WzIOEvyDkDiQ5qspBu2rMmQGag== X-Received: by 2002:a05:600c:1d1d:b0:3f0:34c4:e76d with SMTP id l29-20020a05600c1d1d00b003f034c4e76dmr8176835wms.0.1681225861099; Tue, 11 Apr 2023 08:11:01 -0700 (PDT) Received: from phenom.ffwll.local (212-51-149-33.fiber7.init7.net. [212.51.149.33]) by smtp.gmail.com with ESMTPSA id o11-20020a05600c4fcb00b003ebff290a52sm21197394wmq.28.2023.04.11.08.10.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Apr 2023 08:11:00 -0700 (PDT) Date: Tue, 11 Apr 2023 17:10:58 +0200 From: Daniel Vetter To: Rob Clark Cc: dri-devel@lists.freedesktop.org, linux-arm-msm@vger.kernel.org, freedreno@lists.freedesktop.org, Boris Brezillon , Tvrtko Ursulin , Christopher Healy , Emil Velikov , Rob Clark , David Airlie , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Jonathan Corbet , "open list:DOCUMENTATION" , open list , Daniel Vetter Subject: Re: [PATCH v2 1/2] drm: Add fdinfo memory stats Message-ID: Mail-Followup-To: Rob Clark , dri-devel@lists.freedesktop.org, linux-arm-msm@vger.kernel.org, freedreno@lists.freedesktop.org, Boris Brezillon , Tvrtko Ursulin , Christopher Healy , Emil Velikov , Rob Clark , David Airlie , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Jonathan Corbet , "open list:DOCUMENTATION" , open list References: <20230410210608.1873968-1-robdclark@gmail.com> <20230410210608.1873968-2-robdclark@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Operating-System: Linux phenom 6.1.0-7-amd64 X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Apr 11, 2023 at 08:02:09AM -0700, Rob Clark wrote: > On Tue, Apr 11, 2023 at 3:43 AM Daniel Vetter wrote: > > > > On Mon, Apr 10, 2023 at 02:06:06PM -0700, Rob Clark wrote: > > > From: Rob Clark > > > > > > Add a helper to dump memory stats to fdinfo. For the things the drm > > > core isn't aware of, use a callback. > > > > > > v2: Fix typos, change size units to match docs, use div_u64 > > > > > > Signed-off-by: Rob Clark > > > Reviewed-by: Emil Velikov > > > > Uh can't we wire this up by default? Having this as a per-driver opt-in > > sounds like we'll get maximally fragmented drm fd_info, and since that's > > uapi I don't think that's any good at all. > > That is the reason for the centralized documentation of the props (and > why for this one I added a helper, rather than continuing the current > pattern of everyone rolling their own).. Yeah, but we all know how consistent specs are without either a common implementation or a test suite (or better, both) :-) It's imo good to kick of new things, but anything that multiple drivers could/should implement or once we have multiple drivers implementing something should be common code. I'm doing the same pushing for the fd_info around ctx, at least all drivers using drm/sched should mostly just get this stuff instead of tons of driver glue that then blows up in interesting ways because people discover new ways to get lifetime rules wrong ... > We _could_ (and I had contemplated) doing this all in core if (a) we > move madv to drm_gem_object, and (b) track > drm_gem_get_pages()/drm_gem_put_pages(). I guess neither is totally > unreasonable, pretty much all the non-ttm/non-cma GEM drivers have > some form of madvise ioctl and use > drm_gem_get_pages()/drm_gem_put_pages().. The active part shouldn't need anything new, you should be able to compute that by looking at dma_resv (which is still ok under the spinlock, we still have the lockless stuff to check status afaik). The other bits are a bit more work, and I guess you could sort that out for now by making the status callback optional. Long term pushing purgeable as a concept one level up might make sense, but that's maybe a bit too much refactoring for this. I think just the minimal to get at least infra in place and the fully driver-agnostic stuff computed for every gem driver would be great already. And it makes it much easier for the next fd_info thing to become fully generic, which hopefully motivates people to do that. -Daniel > > BR, > -R > > > I think it's time we have > > - drm_fd_info > > - rolled out to all drivers in their fops > > - with feature checks as appropriate > > - push the driver-specific things into a drm_driver callback > > > > And I guess start peopling giving a hard time for making things needless > > driver-specifict ... there's really no reason at all this is not > > consistent across drivers. > > -Daniel > > > > > --- > > > Documentation/gpu/drm-usage-stats.rst | 21 +++++++ > > > drivers/gpu/drm/drm_file.c | 79 +++++++++++++++++++++++++++ > > > include/drm/drm_file.h | 10 ++++ > > > 3 files changed, 110 insertions(+) > > > > > > diff --git a/Documentation/gpu/drm-usage-stats.rst b/Documentation/gpu/drm-usage-stats.rst > > > index b46327356e80..b5e7802532ed 100644 > > > --- a/Documentation/gpu/drm-usage-stats.rst > > > +++ b/Documentation/gpu/drm-usage-stats.rst > > > @@ -105,6 +105,27 @@ object belong to this client, in the respective memory region. > > > Default unit shall be bytes with optional unit specifiers of 'KiB' or 'MiB' > > > indicating kibi- or mebi-bytes. > > > > > > +- drm-shared-memory: [KiB|MiB] > > > + > > > +The total size of buffers that are shared with another file (ie. have more > > > +than a single handle). > > > + > > > +- drm-private-memory: [KiB|MiB] > > > + > > > +The total size of buffers that are not shared with another file. > > > + > > > +- drm-resident-memory: [KiB|MiB] > > > + > > > +The total size of buffers that are resident in system memory. > > > + > > > +- drm-purgeable-memory: [KiB|MiB] > > > + > > > +The total size of buffers that are purgeable. > > > + > > > +- drm-active-memory: [KiB|MiB] > > > + > > > +The total size of buffers that are active on one or more rings. > > > + > > > - drm-cycles- > > > > > > Engine identifier string must be the same as the one specified in the > > > diff --git a/drivers/gpu/drm/drm_file.c b/drivers/gpu/drm/drm_file.c > > > index a51ff8cee049..085b01842a87 100644 > > > --- a/drivers/gpu/drm/drm_file.c > > > +++ b/drivers/gpu/drm/drm_file.c > > > @@ -42,6 +42,7 @@ > > > #include > > > #include > > > #include > > > +#include > > > #include > > > > > > #include "drm_crtc_internal.h" > > > @@ -868,6 +869,84 @@ void drm_send_event(struct drm_device *dev, struct drm_pending_event *e) > > > } > > > EXPORT_SYMBOL(drm_send_event); > > > > > > +static void print_size(struct drm_printer *p, const char *stat, size_t sz) > > > +{ > > > + const char *units[] = {"", " KiB", " MiB"}; > > > + unsigned u; > > > + > > > + for (u = 0; u < ARRAY_SIZE(units) - 1; u++) { > > > + if (sz < SZ_1K) > > > + break; > > > + sz = div_u64(sz, SZ_1K); > > > + } > > > + > > > + drm_printf(p, "%s:\t%zu%s\n", stat, sz, units[u]); > > > +} > > > + > > > +/** > > > + * drm_print_memory_stats - Helper to print standard fdinfo memory stats > > > + * @file: the DRM file > > > + * @p: the printer to print output to > > > + * @status: callback to get driver tracked object status > > > + * > > > + * Helper to iterate over GEM objects with a handle allocated in the specified > > > + * file. The optional status callback can return additional object state which > > > + * determines which stats the object is counted against. The callback is called > > > + * under table_lock. Racing against object status change is "harmless", and the > > > + * callback can expect to not race against object destruction. > > > + */ > > > +void drm_print_memory_stats(struct drm_file *file, struct drm_printer *p, > > > + enum drm_gem_object_status (*status)(struct drm_gem_object *)) > > > +{ > > > + struct drm_gem_object *obj; > > > + struct { > > > + size_t shared; > > > + size_t private; > > > + size_t resident; > > > + size_t purgeable; > > > + size_t active; > > > + } size = {0}; > > > + int id; > > > + > > > + spin_lock(&file->table_lock); > > > + idr_for_each_entry (&file->object_idr, obj, id) { > > > + enum drm_gem_object_status s = 0; > > > + > > > + if (status) > > > + s = status(obj); > > > + > > > + if (obj->handle_count > 1) { > > > + size.shared += obj->size; > > > + } else { > > > + size.private += obj->size; > > > + } > > > + > > > + if (s & DRM_GEM_OBJECT_RESIDENT) { > > > + size.resident += obj->size; > > > + s &= ~DRM_GEM_OBJECT_PURGEABLE; > > > + } > > > + > > > + if (s & DRM_GEM_OBJECT_ACTIVE) { > > > + size.active += obj->size; > > > + s &= ~DRM_GEM_OBJECT_PURGEABLE; > > > + } > > > + > > > + if (s & DRM_GEM_OBJECT_PURGEABLE) > > > + size.purgeable += obj->size; > > > + } > > > + spin_unlock(&file->table_lock); > > > + > > > + print_size(p, "drm-shared-memory", size.shared); > > > + print_size(p, "drm-private-memory", size.private); > > > + > > > + if (status) { > > > + print_size(p, "drm-resident-memory", size.resident); > > > + print_size(p, "drm-purgeable-memory", size.purgeable); > > > + print_size(p, "drm-active-memory", size.active); > > > + } > > > +} > > > +EXPORT_SYMBOL(drm_print_memory_stats); > > > + > > > /** > > > * mock_drm_getfile - Create a new struct file for the drm device > > > * @minor: drm minor to wrap (e.g. #drm_device.primary) > > > diff --git a/include/drm/drm_file.h b/include/drm/drm_file.h > > > index 0d1f853092ab..7bd8a1374f39 100644 > > > --- a/include/drm/drm_file.h > > > +++ b/include/drm/drm_file.h > > > @@ -41,6 +41,7 @@ > > > struct dma_fence; > > > struct drm_file; > > > struct drm_device; > > > +struct drm_printer; > > > struct device; > > > struct file; > > > > > > @@ -438,6 +439,15 @@ void drm_send_event_timestamp_locked(struct drm_device *dev, > > > struct drm_pending_event *e, > > > ktime_t timestamp); > > > > > > +enum drm_gem_object_status { > > > + DRM_GEM_OBJECT_RESIDENT = BIT(0), > > > + DRM_GEM_OBJECT_PURGEABLE = BIT(1), > > > + DRM_GEM_OBJECT_ACTIVE = BIT(2), > > > +}; > > > + > > > +void drm_print_memory_stats(struct drm_file *file, struct drm_printer *p, > > > + enum drm_gem_object_status (*status)(struct drm_gem_object *)); > > > + > > > struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags); > > > > > > #endif /* _DRM_FILE_H_ */ > > > -- > > > 2.39.2 > > > > > > > -- > > Daniel Vetter > > Software Engineer, Intel Corporation > > http://blog.ffwll.ch -- Daniel Vetter Software Engineer, Intel Corporation http://blog.ffwll.ch