Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp5999930rwd; Wed, 24 May 2023 09:27:43 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7YZ9uFz7DbYPV26fr/L+OvWXv939hBt4r4TZBmBoj3uNZ83QAx6mS94JIvEXILAklxxO0F X-Received: by 2002:a17:902:ec8a:b0:1ae:5212:748b with SMTP id x10-20020a170902ec8a00b001ae5212748bmr19860964plg.49.1684945663437; Wed, 24 May 2023 09:27:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684945663; cv=none; d=google.com; s=arc-20160816; b=LW12nb4mLzHn7PVXBMsZNim1VTimnRfeRxkfFak6mzu2uO/LYYWXJCUPcK5qc7hXcY 4KaPMoPbRUdxXAtcRCjPFPbRyLolmR/nBC1rxa/VLyLGqdtG2o7LoZRTVWclVYS/QzTT 29MV00oJYfStBi7ScIag6Ng+DlLpgMFQMjVg1VGl0K3YW9jO1s8Gwbp2hAPolBHAa1BE Kfkier+onbZl8JfNy34PwyoEASLF1Z/sgphx5Kf4pEMrdBPuSHZrTdj51lW6R4jP+FYZ MVw2YtV4JGeG/eGHBtf9Akrh1wrpfZBFiFw4XoEDCtfc5+DTBtxtCtXfsRh73eGp0puI J9fw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=7/m5y9DMBAGNOMht9FaF2vJN36JyHjRD9qCEwtOdPVI=; b=aZJexoqUdQhAFwX7t1vYLH6cy1eWGulw4mUy34ho0GVnLRRfoCqJv5VIpYYA3y4Adg ZXl/eiM7Lic9rwuBGagRwrvd+thwoqrrsT3cZFiZweyx051UEg8GsIj6NSwlN1ZH9KZL 82uNdvGC0aKxCkndM5Bew3Ypkct0xp3glM7P6Ga8jGUFxErvP/lgpC3aiDz5LsPx1wPA kJquwlezS9QjhMcNOw5BIyPrx767wQce75j3rzusRGe5A8mLOKsO8IgvgIxdSz+my+2Q q1cmyYuW4nxVgisyZW53zbeOYJ17jFKk6Ya/2Khn6zJOy7Q2QXO1MGqibrpxhRPWbyGl yeJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=L8hNGSIC; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l3-20020a170902f68300b001ae488bbbd0si1617909plg.494.2023.05.24.09.27.30; Wed, 24 May 2023 09:27:43 -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=@gmail.com header.s=20221208 header.b=L8hNGSIC; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238359AbjEXQBY (ORCPT + 99 others); Wed, 24 May 2023 12:01:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238267AbjEXQAz (ORCPT ); Wed, 24 May 2023 12:00:55 -0400 Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A709910CF; Wed, 24 May 2023 09:00:15 -0700 (PDT) Received: by mail-pg1-x52a.google.com with SMTP id 41be03b00d2f7-51b0f9d7d70so438097a12.1; Wed, 24 May 2023 09:00:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684944015; x=1687536015; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7/m5y9DMBAGNOMht9FaF2vJN36JyHjRD9qCEwtOdPVI=; b=L8hNGSICE/y/nRGAQSHMJZxKPdKQY4FYCJuUBDFNI+lBh9XwgJXWj1MTK261vpT2/K jtkGpVeK2J8cN8NpfKHIcmaDuURs1mPLgUztOiDhp3ShT4g23biQjG6LVnCoh4Y5ufug 5g4/r35PXOttozl/FpTusNYuR9X/LmpiRX/z4tKuF9Uk2DHKZtJg2MPwo11nr1ErQFOn aCk7xWcaFff7yKrZY9K5IsPYZLoucw2Gd8pGycY8/c/T7fuBdkImH58/PuVAJBNEL6Qg 6eUMjLayFAICkxsxSWnQul10VkazeTO9UIrOzZLqRN83OYlB2OUAjjVwThbgv5ghAi8H q0JA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684944015; x=1687536015; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7/m5y9DMBAGNOMht9FaF2vJN36JyHjRD9qCEwtOdPVI=; b=ZFLXtV35t2reBXiAixDoEyHXk09fazNzvr/xg0NbrzdMf/h2Y6VQLB16xALg4ymgew 2ibiDCtu38pMb3PGwUJwysOGPU/9zkR0vRgMRrHrfCwj8IN5klo364XHkR2xGry4swXx tYz9eD0G3+mRiZ8g5m4mT+6lTW8SSxPygpi0A8pxYPhZI2chxyHphgdyPP5v74IAqWtf i/qENx2P2u/HXP35fVjrPT1id0IAC+F2lSpy46+pU9oB9sa1RMpiuoSetpD5a2XQkLp3 lX4puJ7B6S5i1iYCyb8QvEnmYD90fnA6JUHXQ5EchIR+BQRwWuDt32o7rR0sGuVZiPr5 SVWQ== X-Gm-Message-State: AC+VfDx1jhdiUqG1uPcnLkTF94/Cr6YIt9nh0ZgbVH3n2qyR8Juq78OW VjA+W2igaql9foIhJ+Ql9HtUBa1FDro= X-Received: by 2002:a17:902:f681:b0:1af:e295:ad68 with SMTP id l1-20020a170902f68100b001afe295ad68mr2278321plg.30.1684944014575; Wed, 24 May 2023 09:00:14 -0700 (PDT) Received: from localhost ([2a00:79e1:abd:4a00:61b:48ed:72ab:435b]) by smtp.gmail.com with ESMTPSA id t22-20020a1709028c9600b001afebec96basm264091plo.148.2023.05.24.09.00.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 May 2023 09:00:14 -0700 (PDT) From: Rob Clark To: dri-devel@lists.freedesktop.org Cc: freedreno@lists.freedesktop.org, Daniel Vetter , Tvrtko Ursulin , Boris Brezillon , Christopher Healy , Emil Velikov , =?UTF-8?q?Christian=20K=C3=B6nig?= , Rob Clark , Daniel Vetter , Dave Airlie , David Airlie , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Jonathan Corbet , linux-doc@vger.kernel.org (open list:DOCUMENTATION), linux-kernel@vger.kernel.org (open list) Subject: [PATCH v5 2/7] drm: Add common fdinfo helper Date: Wed, 24 May 2023 08:59:32 -0700 Message-Id: <20230524155956.382440-3-robdclark@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230524155956.382440-1-robdclark@gmail.com> References: <20230524155956.382440-1-robdclark@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 From: Rob Clark Handle a bit of the boiler-plate in a single case, and make it easier to add some core tracked stats. This also ensures consistent behavior across drivers for standardised fields. v2: Update drm-usage-stats.rst, 64b client-id, rename drm_show_fdinfo v3: Rebase on drm-misc-next Reviewed-by: Daniel Vetter Signed-off-by: Rob Clark Acked-by: Dave Airlie --- Documentation/gpu/drm-usage-stats.rst | 10 +++++++- drivers/gpu/drm/drm_file.c | 35 +++++++++++++++++++++++++++ include/drm/drm_drv.h | 7 ++++++ include/drm/drm_file.h | 4 +++ 4 files changed, 55 insertions(+), 1 deletion(-) diff --git a/Documentation/gpu/drm-usage-stats.rst b/Documentation/gpu/drm-usage-stats.rst index 72d069e5dacb..552195fb1ea3 100644 --- a/Documentation/gpu/drm-usage-stats.rst +++ b/Documentation/gpu/drm-usage-stats.rst @@ -119,14 +119,22 @@ value until a monotonic update is seen. - drm-maxfreq-: [Hz|MHz|KHz] Engine identifier string must be the same as the one specified in the drm-engine- tag and shall contain the maximum frequency for the given engine. Taken together with drm-cycles-, this can be used to calculate percentage utilization of the engine, whereas drm-engine- only reflects time active without considering what frequency the engine is operating as a percentage of it's maximum frequency. +Implementation Details +====================== + +Drivers should use drm_show_fdinfo() in their `struct file_operations`, and +implement &drm_driver.show_fdinfo if they wish to provide any stats which +are not provided by drm_show_fdinfo(). But even driver specific stats should +be documented above and where possible, aligned with other drivers. + Driver specific implementations -=============================== +------------------------------- :ref:`i915-usage-stats` diff --git a/drivers/gpu/drm/drm_file.c b/drivers/gpu/drm/drm_file.c index c1018c470047..37b4f76a5191 100644 --- a/drivers/gpu/drm/drm_file.c +++ b/drivers/gpu/drm/drm_file.c @@ -141,28 +141,31 @@ bool drm_dev_needs_global_mutex(struct drm_device *dev) * * This allocates a new DRM file context. It is not linked into any context and * can be used by the caller freely. Note that the context keeps a pointer to * @minor, so it must be freed before @minor is. * * RETURNS: * Pointer to newly allocated context, ERR_PTR on failure. */ struct drm_file *drm_file_alloc(struct drm_minor *minor) { + static atomic64_t ident = ATOMIC_INIT(0); struct drm_device *dev = minor->dev; struct drm_file *file; int ret; file = kzalloc(sizeof(*file), GFP_KERNEL); if (!file) return ERR_PTR(-ENOMEM); + /* Get a unique identifier for fdinfo: */ + file->client_id = atomic64_inc_return(&ident); file->pid = get_pid(task_tgid(current)); file->minor = minor; /* for compatibility root is always authenticated */ file->authenticated = capable(CAP_SYS_ADMIN); INIT_LIST_HEAD(&file->lhead); INIT_LIST_HEAD(&file->fbs); mutex_init(&file->fbs_lock); INIT_LIST_HEAD(&file->blobs); @@ -861,20 +864,52 @@ EXPORT_SYMBOL(drm_send_event_locked); void drm_send_event(struct drm_device *dev, struct drm_pending_event *e) { unsigned long irqflags; spin_lock_irqsave(&dev->event_lock, irqflags); drm_send_event_helper(dev, e, 0); spin_unlock_irqrestore(&dev->event_lock, irqflags); } EXPORT_SYMBOL(drm_send_event); +/** + * drm_show_fdinfo - helper for drm file fops + * @seq_file: output stream + * @f: the device file instance + * + * Helper to implement fdinfo, for userspace to query usage stats, etc, of a + * process using the GPU. See also &drm_driver.show_fdinfo. + * + * For text output format description please see Documentation/gpu/drm-usage-stats.rst + */ +void drm_show_fdinfo(struct seq_file *m, struct file *f) +{ + struct drm_file *file = f->private_data; + struct drm_device *dev = file->minor->dev; + struct drm_printer p = drm_seq_file_printer(m); + + drm_printf(&p, "drm-driver:\t%s\n", dev->driver->name); + drm_printf(&p, "drm-client-id:\t%llu\n", file->client_id); + + if (dev_is_pci(dev->dev)) { + struct pci_dev *pdev = to_pci_dev(dev->dev); + + drm_printf(&p, "drm-pdev:\t%04x:%02x:%02x.%d\n", + pci_domain_nr(pdev->bus), pdev->bus->number, + PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)); + } + + if (dev->driver->show_fdinfo) + dev->driver->show_fdinfo(&p, file); +} +EXPORT_SYMBOL(drm_show_fdinfo); + /** * mock_drm_getfile - Create a new struct file for the drm device * @minor: drm minor to wrap (e.g. #drm_device.primary) * @flags: file creation mode (O_RDWR etc) * * This create a new struct file that wraps a DRM file context around a * DRM minor. This mimicks userspace opening e.g. /dev/dri/card0, but without * invoking userspace. The struct file may be operated on using its f_op * (the drm_device.driver.fops) to mimick userspace operations, or be supplied * to userspace facing functions as an internal/anonymous client. diff --git a/include/drm/drm_drv.h b/include/drm/drm_drv.h index b419c59c4bef..89e2706cac56 100644 --- a/include/drm/drm_drv.h +++ b/include/drm/drm_drv.h @@ -394,20 +394,27 @@ struct drm_driver { * Called by the user via ioctl. * * Returns: * * Zero on success, negative errno on failure. */ int (*dumb_map_offset)(struct drm_file *file_priv, struct drm_device *dev, uint32_t handle, uint64_t *offset); + /** + * @show_fdinfo: + * + * Print device specific fdinfo. See Documentation/gpu/drm-usage-stats.rst. + */ + void (*show_fdinfo)(struct drm_printer *p, struct drm_file *f); + /** @major: driver major number */ int major; /** @minor: driver minor number */ int minor; /** @patchlevel: driver patch level */ int patchlevel; /** @name: driver name */ char *name; /** @desc: driver description */ char *desc; diff --git a/include/drm/drm_file.h b/include/drm/drm_file.h index ecffe24e2b1b..7d9b3c65cbc1 100644 --- a/include/drm/drm_file.h +++ b/include/drm/drm_file.h @@ -251,20 +251,23 @@ struct drm_file { * primary nodes and authentication `. */ struct drm_master *master; /** @master_lookup_lock: Serializes @master. */ spinlock_t master_lookup_lock; /** @pid: Process that opened this file. */ struct pid *pid; + /** @client_id: A unique id for fdinfo */ + u64 client_id; + /** @magic: Authentication magic, see @authenticated. */ drm_magic_t magic; /** * @lhead: * * List of all open files of a DRM device, linked into * &drm_device.filelist. Protected by &drm_device.filelist_mutex. */ struct list_head lhead; @@ -431,14 +434,15 @@ int drm_event_reserve_init(struct drm_device *dev, struct drm_file *file_priv, struct drm_pending_event *p, struct drm_event *e); void drm_event_cancel_free(struct drm_device *dev, struct drm_pending_event *p); void drm_send_event_locked(struct drm_device *dev, struct drm_pending_event *e); void drm_send_event(struct drm_device *dev, struct drm_pending_event *e); void drm_send_event_timestamp_locked(struct drm_device *dev, struct drm_pending_event *e, ktime_t timestamp); +void drm_show_fdinfo(struct seq_file *m, struct file *f); struct file *mock_drm_getfile(struct drm_minor *minor, unsigned int flags); #endif /* _DRM_FILE_H_ */ -- 2.40.1