Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp2924139lqz; Wed, 3 Apr 2024 12:28:57 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWfX/Itz11NpoOVJAxfLP01zYSUyeCtew7bjgNHL6GJ3wQSZanVAFxR3QvKjM5s6PxU9f4uFoUNKzVhSCTUy5wVnGh3WyhJzPnLG22s3Q== X-Google-Smtp-Source: AGHT+IHq5z3jufio6G0QQyVMkhnO+/go79TC5yWhz0yUmg0LTHOyPA0DgJcWO6HVowH9FiuUdso3 X-Received: by 2002:a05:6a20:c91b:b0:1a7:33f6:bb5f with SMTP id gx27-20020a056a20c91b00b001a733f6bb5fmr245889pzb.47.1712172537086; Wed, 03 Apr 2024 12:28:57 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712172537; cv=pass; d=google.com; s=arc-20160816; b=TFGaypvythGmWKOkQhrqdj/3kr9ESeciLbN5n7+dC3MzJjfaH0tX9/o76RT/qG1nuS 4hUU55K2SS96uSC3yzU03HPEtPna5RkLCKPI5x/v5SJqeDAY7J5eoFS/JKutWH43aOTs uZSH9n3DgGNGyph8XLan29t2liVszzaFkeRzpkg0+KVK+w9vOEr5EQxQEVudXoVmxod3 sMV/eqGv/w1b3FeIqSqMkpkKtrAFuigvBnhkY/psPuwnvFOUh5IGKOuXrUk9ZpL5wMId S5CJJ8dmu9Asj/rVWW+aYwKt0Rj7lnUceDQmOa1ATgTih86XiWIrgzhX826N5fAYZkoF jRLQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=masCQ0rjrphQCJObz1vlh6FxybPu7PLrrJNv4xbkQDg=; fh=ObHaSuo3UwaSPqmE4DEoQYkrp5OBSY6qbJx0NvEWykk=; b=WDyuUEQs9rKIWtBukkYuWbKTa0uHMYNUJJlmQbUU7h/oRLtXs130J/QXLGQAeznUe9 Inig3GbjF2EuoeC3xZfvdVNVK6o+Q8uwa6sX0+hHFbT7GuZGVG9TLRzRfrAr41knTFVV 6E1yoh+8so6HKPCiUlpPaWkkLFA73/72SpbghIKBfkb7KEL2x9b2+DHgJNQvCwvtGV1w SicGd5NcAtD6OKhFWdzgep/vJ/9MG8HLRs4X08AR6BpDBrUVErhPCjLD8S8azthp3P/a QqobyWMsJNw7zu21IbjgKV03gVDnggzeHiiOV2CY25oYVJYtfLkI45czRLDXvGMzRMoe nSoA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=AmTBmM3B; arc=pass (i=1 spf=pass spfdomain=collabora.com dkim=pass dkdomain=collabora.com dmarc=pass fromdomain=collabora.com); spf=pass (google.com: domain of linux-kernel+bounces-130447-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-130447-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id y15-20020a056a00180f00b006eac9744617si14363871pfa.127.2024.04.03.12.28.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 12:28:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-130447-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=AmTBmM3B; arc=pass (i=1 spf=pass spfdomain=collabora.com dkim=pass dkdomain=collabora.com dmarc=pass fromdomain=collabora.com); spf=pass (google.com: domain of linux-kernel+bounces-130447-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-130447-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 6BC1AB31CAD for ; Wed, 3 Apr 2024 18:31:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3510C482DA; Wed, 3 Apr 2024 18:30:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="AmTBmM3B" Received: from madrid.collaboradmins.com (madrid.collaboradmins.com [46.235.227.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4E11B53A7 for ; Wed, 3 Apr 2024 18:30:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=46.235.227.194 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712169037; cv=none; b=DdDb5vKW8ODCjesClXb7/n5J8Mfzahc42ujXyqnoyzA8MReR5rlOdBOenDtwT90lyIDzCJLRdszyx9v3e0yqdRCbaw7w6n6efF1E1oOORGhZSGHYIcFJK/YBG7e3tQAkE44X17OjqIamGSZLJl6wvDxu539RvcIsEhU0SwMrVHI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712169037; c=relaxed/simple; bh=X3gawQizTTZ2izaFT0MnlQyxFRjnWmFQBSZh9Jvcdpw=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=tuKElQ79Ot2ndciKXANUdpTIQTRwQWkFbLoCUCFe5/UY1GR9u2s6h6hq3H35kcWAQrnQPhmxT7nCtv/aY+LHAjV6PviWM2cbPxgOxhfU1lC0mcHaRi36nbNPFo4be2InXvHkk0dU3jJb21iVaYcOeqz34PNZ/17PIqboX/xnh6I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b=AmTBmM3B; arc=none smtp.client-ip=46.235.227.194 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1712169033; bh=X3gawQizTTZ2izaFT0MnlQyxFRjnWmFQBSZh9Jvcdpw=; h=From:To:Cc:Subject:Date:From; b=AmTBmM3Bjo9n+afuIZfqGHOst3XC2oURKpvaNsUIq+gCXkNBiBQrx7K2jSL6vdVeS mpTA8LNdX705fFs9Nek8/IvpOVhesw9n9sAWtO9DjCWkcRJj1YfqMVtKLzPSaNNQJQ kS5YPp/vbg+dd01t8P2RSK2w2QRTcXiHyMS/SJezDmcqenIkX14SKxnK4PM29OZDCl wrtBM8bNJeV2a7ev9wFY/Xwob2+7jQWDbWHgbmKtzeAya49/8IwZNpPX8dZDY+tMDj 2kmSQbillZk1vbXiMjh5GC2KxlCJqxwpZ9TmBm2KHKDqWedBVRjHMRab3+7ZhJzZMv QxuXGkbciCslA== Received: from localhost.localdomain (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: alarumbe) by madrid.collaboradmins.com (Postfix) with ESMTPSA id 62EFA37813D7; Wed, 3 Apr 2024 18:30:32 +0000 (UTC) From: =?UTF-8?q?Adri=C3=A1n=20Larumbe?= To: Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Daniel Vetter , Hans de Goede Cc: kernel@collabora.com, =?UTF-8?q?Adri=C3=A1n=20Larumbe?= , Boris Brezillon , Tvrtko Ursulin , Christopher Healy , dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org Subject: [PATCH] drm/sysfs: Add drm class-wide attribute to get active device clients Date: Wed, 3 Apr 2024 19:29:39 +0100 Message-ID: <20240403182951.724488-1-adrian.larumbe@collabora.com> X-Mailer: git-send-email 2.44.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Up to this day, all fdinfo-based GPU profilers must traverse the entire /proc directory structure to find open DRM clients with fdinfo file descriptors. This is inefficient and time-consuming. This patch adds a new device class attribute that will install a sysfs file per DRM device, which can be queried by profilers to get a list of PIDs for their open clients. This file isn't human-readable, and it's meant to be queried only by GPU profilers like gputop and nvtop. Cc: Boris Brezillon Cc: Tvrtko Ursulin Cc: Christopher Healy Signed-off-by: Adrián Larumbe --- drivers/gpu/drm/drm_internal.h | 2 +- drivers/gpu/drm/drm_privacy_screen.c | 2 +- drivers/gpu/drm/drm_sysfs.c | 89 ++++++++++++++++++++++------ 3 files changed, 74 insertions(+), 19 deletions(-) diff --git a/drivers/gpu/drm/drm_internal.h b/drivers/gpu/drm/drm_internal.h index 2215baef9a3e..9a399b03d11c 100644 --- a/drivers/gpu/drm/drm_internal.h +++ b/drivers/gpu/drm/drm_internal.h @@ -145,7 +145,7 @@ bool drm_master_internal_acquire(struct drm_device *dev); void drm_master_internal_release(struct drm_device *dev); /* drm_sysfs.c */ -extern struct class *drm_class; +extern struct class drm_class; int drm_sysfs_init(void); void drm_sysfs_destroy(void); diff --git a/drivers/gpu/drm/drm_privacy_screen.c b/drivers/gpu/drm/drm_privacy_screen.c index 6cc39e30781f..2fbd24ba5818 100644 --- a/drivers/gpu/drm/drm_privacy_screen.c +++ b/drivers/gpu/drm/drm_privacy_screen.c @@ -401,7 +401,7 @@ struct drm_privacy_screen *drm_privacy_screen_register( mutex_init(&priv->lock); BLOCKING_INIT_NOTIFIER_HEAD(&priv->notifier_head); - priv->dev.class = drm_class; + priv->dev.class = &drm_class; priv->dev.type = &drm_privacy_screen_type; priv->dev.parent = parent; priv->dev.release = drm_privacy_screen_device_release; diff --git a/drivers/gpu/drm/drm_sysfs.c b/drivers/gpu/drm/drm_sysfs.c index a953f69a34b6..56ca9e22c720 100644 --- a/drivers/gpu/drm/drm_sysfs.c +++ b/drivers/gpu/drm/drm_sysfs.c @@ -58,8 +58,6 @@ static struct device_type drm_sysfs_device_connector = { .name = "drm_connector", }; -struct class *drm_class; - #ifdef CONFIG_ACPI static bool drm_connector_acpi_bus_match(struct device *dev) { @@ -128,6 +126,62 @@ static const struct component_ops typec_connector_ops = { static CLASS_ATTR_STRING(version, S_IRUGO, "drm 1.1.0 20060810"); +static ssize_t clients_show(struct device *cd, struct device_attribute *attr, char *buf) +{ + struct drm_minor *minor = cd->driver_data; + struct drm_device *ddev = minor->dev; + struct drm_file *priv; + ssize_t offset = 0; + void *pid_buf; + + if (minor->type != DRM_MINOR_RENDER) + return 0; + + pid_buf = kvmalloc(PAGE_SIZE, GFP_KERNEL); + if (!pid_buf) + return 0; + + mutex_lock(&ddev->filelist_mutex); + list_for_each_entry_reverse(priv, &ddev->filelist, lhead) { + struct pid *pid; + + if (drm_WARN_ON(ddev, (PAGE_SIZE - offset) < sizeof(pid_t))) + break; + + rcu_read_lock(); + pid = rcu_dereference(priv->pid); + (*(pid_t *)(pid_buf + offset)) = pid_vnr(pid); + rcu_read_unlock(); + + offset += sizeof(pid_t); + } + mutex_unlock(&ddev->filelist_mutex); + + if (offset < PAGE_SIZE) + (*(pid_t *)(pid_buf + offset)) = 0; + + memcpy(buf, pid_buf, offset); + + kvfree(pid_buf); + + return offset; + +} +static DEVICE_ATTR_RO(clients); + +static struct attribute *drm_device_attrs[] = { + &dev_attr_clients.attr, + NULL, +}; +ATTRIBUTE_GROUPS(drm_device); + +struct class drm_class = { + .name = "drm", + .dev_groups = drm_device_groups, +}; + +static bool drm_class_initialised; + /** * drm_sysfs_init - initialize sysfs helpers * @@ -142,18 +196,19 @@ int drm_sysfs_init(void) { int err; - drm_class = class_create("drm"); - if (IS_ERR(drm_class)) - return PTR_ERR(drm_class); + err = class_register(&drm_class); + if (err) + return err; - err = class_create_file(drm_class, &class_attr_version.attr); + err = class_create_file(&drm_class, &class_attr_version.attr); if (err) { - class_destroy(drm_class); - drm_class = NULL; + class_destroy(&drm_class); return err; } - drm_class->devnode = drm_devnode; + drm_class.devnode = drm_devnode; + + drm_class_initialised = true; drm_sysfs_acpi_register(); return 0; @@ -166,12 +221,12 @@ int drm_sysfs_init(void) */ void drm_sysfs_destroy(void) { - if (IS_ERR_OR_NULL(drm_class)) + if (!drm_class_initialised) return; drm_sysfs_acpi_unregister(); - class_remove_file(drm_class, &class_attr_version.attr); - class_destroy(drm_class); - drm_class = NULL; + class_remove_file(&drm_class, &class_attr_version.attr); + class_destroy(&drm_class); + drm_class_initialised = false; } static void drm_sysfs_release(struct device *dev) @@ -372,7 +427,7 @@ int drm_sysfs_connector_add(struct drm_connector *connector) return -ENOMEM; device_initialize(kdev); - kdev->class = drm_class; + kdev->class = &drm_class; kdev->type = &drm_sysfs_device_connector; kdev->parent = dev->primary->kdev; kdev->groups = connector_dev_groups; @@ -550,7 +605,7 @@ struct device *drm_sysfs_minor_alloc(struct drm_minor *minor) minor_str = "card%d"; kdev->devt = MKDEV(DRM_MAJOR, minor->index); - kdev->class = drm_class; + kdev->class = &drm_class; kdev->type = &drm_sysfs_device_minor; } @@ -579,10 +634,10 @@ struct device *drm_sysfs_minor_alloc(struct drm_minor *minor) */ int drm_class_device_register(struct device *dev) { - if (!drm_class || IS_ERR(drm_class)) + if (!drm_class_initialised) return -ENOENT; - dev->class = drm_class; + dev->class = &drm_class; return device_register(dev); } EXPORT_SYMBOL_GPL(drm_class_device_register); base-commit: 45c734fdd43db14444025910b4c59dd2b8be714a -- 2.44.0