From patchwork Tue Apr 17 12:27:32 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tvrtko Ursulin X-Patchwork-Id: 10344907 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id F3C18601D7 for ; Tue, 17 Apr 2018 12:28:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E9861286EC for ; Tue, 17 Apr 2018 12:28:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DE17028A8F; Tue, 17 Apr 2018 12:28:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id DCB6D286EC for ; Tue, 17 Apr 2018 12:28:23 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 4C1766E472; Tue, 17 Apr 2018 12:28:20 +0000 (UTC) X-Original-To: Intel-gfx@lists.freedesktop.org Delivered-To: Intel-gfx@lists.freedesktop.org Received: from mail-wr0-x244.google.com (mail-wr0-x244.google.com [IPv6:2a00:1450:400c:c0c::244]) by gabe.freedesktop.org (Postfix) with ESMTPS id 39E5E6E160 for ; Tue, 17 Apr 2018 12:28:12 +0000 (UTC) Received: by mail-wr0-x244.google.com with SMTP id v60so31110521wrc.7 for ; Tue, 17 Apr 2018 05:28:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ursulin-net.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=IPh7VL4qaFEkhoFdXGXZAHMwPNPVbkD63t2+MyBcEz8=; b=08D7NdA5H0HgngfUSxC7tP2lxy9CtJc1qF0yTaLFfknTiLHNSrymSvSPwumPnwnE6M xePQ8TvFQt3O2RkWWD3cKDaiKhO2lhg0wTx42e48TWgpsZNspHhb0PtSVpZLgiuYgnAl bksS4xiMvl/02VK7cyjB+/sNBHggqnBH2IuVETsfYukkWIQaiGzB3yC1SQchZOoq8MXJ MhwBzyxX0w+sP4/s3cbwYMV4EIf/MWcOxaFyld/i7b53E51J2EYJXS3a69RVsxPObrmL 5TUxOEnjNxchqcxrPZ8FH1Ef9M0pMc0nHOe6EyW2ntXdgfronBvmRgOQ7xrOq2UnqfaG NRNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=IPh7VL4qaFEkhoFdXGXZAHMwPNPVbkD63t2+MyBcEz8=; b=AWmrXudt0cCc1REfk3aOII1k/DS+QRSE5Njj/wFKjSL9iqGI2QI+GpfiZuWq83hw2Q eXVSPpIJzvW2bHY1HGCwnPxjlLw3l+GXN9PWRO6T2LRyvxchfD5N0FvvI+yMXN24yJHG IRu3nDAzMP7LfJy32A/RL9Jkr4of+VS14K2L08kmjWSXDKVVViVKrDH20Vcle6joEjk2 F2YcEM4rvIQlQSRZB1tjYBSejZoVoIQL5sh9eMOhv+b51NKtdlk+sD38KQlgov62iEcr aRduCjz30TL6pz1W1fxrmOskQl7GLSGyQ0aGPke+xOwQlsxImwktFIKkj2E0eM8LM8gI OwWQ== X-Gm-Message-State: ALQs6tDR7fZLsaH0aEq62QakAKHE1khyYcARdeKfHgtOY0N2DxSW0T2F inut+4pHI5E+u7X0aMQEyjZayA== X-Google-Smtp-Source: AIpwx4/P/oKklG0mGvg8XiAq34IvvcUrZvQUEh9SBq8a843bWCznWhIB+p9peNGTGSH8ro/0np14Vw== X-Received: by 10.223.129.17 with SMTP id 17mr1536997wrm.109.1523968090776; Tue, 17 Apr 2018 05:28:10 -0700 (PDT) Received: from localhost.localdomain ([95.146.151.144]) by smtp.gmail.com with ESMTPSA id f22sm9226027wmc.3.2018.04.17.05.28.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 17 Apr 2018 05:28:10 -0700 (PDT) From: Tvrtko Ursulin X-Google-Original-From: Tvrtko Ursulin To: igt-dev@lists.freedesktop.org Date: Tue, 17 Apr 2018 13:27:32 +0100 Message-Id: <20180417122736.11603-4-tvrtko.ursulin@linux.intel.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20180417122736.11603-1-tvrtko.ursulin@linux.intel.com> References: <20180417122736.11603-1-tvrtko.ursulin@linux.intel.com> Subject: [Intel-gfx] [RFC 3/7] drm/i915: Expose list of clients in sysfs X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Intel-gfx@lists.freedesktop.org MIME-Version: 1.0 Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" X-Virus-Scanned: ClamAV using ClamSMTP From: Tvrtko Ursulin Expose a list of clients with open file handles in sysfs. This will be a basis for a top-like utility showing per-client and per- engine GPU load. Currently we only expose each client's pid and name under opaque numbered directories in /sys/class/drm/card0/clients/. For instance: /sys/class/drm/card0/clients/3/name: Xorg /sys/class/drm/card0/clients/3/pid: 5664 v2: Chris Wilson: * Enclose new members into dedicated structs. * Protect against failed sysfs registration. Signed-off-by: Tvrtko Ursulin --- drivers/gpu/drm/i915/i915_drv.h | 19 +++++++ drivers/gpu/drm/i915/i915_gem.c | 117 +++++++++++++++++++++++++++++++++++--- drivers/gpu/drm/i915/i915_sysfs.c | 8 +++ 3 files changed, 137 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h index 649c0f2f3bae..ff2ad9719b1e 100644 --- a/drivers/gpu/drm/i915/i915_drv.h +++ b/drivers/gpu/drm/i915/i915_drv.h @@ -347,6 +347,20 @@ struct drm_i915_file_private { */ #define I915_MAX_CLIENT_CONTEXT_BANS 3 atomic_t context_bans; + + struct i915_drm_client { + unsigned int id; + + pid_t pid; + char *name; + + struct kobject *root; + + struct { + struct device_attribute pid; + struct device_attribute name; + } attr; + } client; }; /* Interface history: @@ -2111,6 +2125,11 @@ struct drm_i915_private { struct i915_pmu pmu; + struct i915_drm_clients { + struct kobject *root; + atomic_t serial; + } clients; + /* * NOTE: This is the dri1/ums dungeon, don't add stuff here. Your patch * will be rejected. Instead look for a better place. diff --git a/drivers/gpu/drm/i915/i915_gem.c b/drivers/gpu/drm/i915/i915_gem.c index 062a9c743607..421e6c9e95f4 100644 --- a/drivers/gpu/drm/i915/i915_gem.c +++ b/drivers/gpu/drm/i915/i915_gem.c @@ -5708,6 +5708,94 @@ int i915_gem_freeze_late(struct drm_i915_private *dev_priv) return 0; } +static ssize_t +show_client_name(struct device *kdev, struct device_attribute *attr, char *buf) +{ + struct drm_i915_file_private *file_priv = + container_of(attr, struct drm_i915_file_private, + client.attr.name); + + return snprintf(buf, PAGE_SIZE, "%s", file_priv->client.name); +} + +static ssize_t +show_client_pid(struct device *kdev, struct device_attribute *attr, char *buf) +{ + struct drm_i915_file_private *file_priv = + container_of(attr, struct drm_i915_file_private, + client.attr.pid); + + return snprintf(buf, PAGE_SIZE, "%u", file_priv->client.pid); +} + +static int +i915_gem_add_client(struct drm_i915_private *i915, + struct drm_i915_file_private *file_priv, + struct task_struct *task, + unsigned int serial) +{ + int ret = -ENOMEM; + struct device_attribute *attr; + char id[32]; + + if (!i915->clients.root) + goto err_name; + + file_priv->client.name = kstrdup(task->comm, GFP_KERNEL); + if (!file_priv->client.name) + goto err_name; + + snprintf(id, sizeof(id), "%u", serial); + file_priv->client.root = kobject_create_and_add(id, + i915->clients.root); + if (!file_priv->client.root) + goto err_client; + + attr = &file_priv->client.attr.name; + attr->attr.name = "name"; + attr->attr.mode = 0444; + attr->show = show_client_name; + + ret = sysfs_create_file(file_priv->client.root, + (struct attribute *)attr); + if (ret) + goto err_attr_name; + + attr = &file_priv->client.attr.pid; + attr->attr.name = "pid"; + attr->attr.mode = 0444; + attr->show = show_client_pid; + + ret = sysfs_create_file(file_priv->client.root, + (struct attribute *)attr); + if (ret) + goto err_attr_pid; + + file_priv->client.pid = pid_nr(get_task_pid(task, PIDTYPE_PID)); + + return 0; + +err_attr_pid: + sysfs_remove_file(file_priv->client.root, + (struct attribute *)&file_priv->client.attr.name); +err_attr_name: + kobject_put(file_priv->client.root); +err_client: + kfree(file_priv->client.name); +err_name: + return ret; +} + +static void i915_gem_remove_client(struct drm_i915_file_private *file_priv) +{ + sysfs_remove_file(file_priv->client.root, + (struct attribute *)&file_priv->client.attr.pid); + sysfs_remove_file(file_priv->client.root, + (struct attribute *)&file_priv->client.attr.name); + kobject_put(file_priv->client.root); + kfree(file_priv->client.name); +} + void i915_gem_release(struct drm_device *dev, struct drm_file *file) { struct drm_i915_file_private *file_priv = file->driver_priv; @@ -5721,32 +5809,47 @@ void i915_gem_release(struct drm_device *dev, struct drm_file *file) list_for_each_entry(request, &file_priv->mm.request_list, client_link) request->file_priv = NULL; spin_unlock(&file_priv->mm.lock); + + i915_gem_remove_client(file_priv); } int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file) { + int ret = -ENOMEM; struct drm_i915_file_private *file_priv; - int ret; DRM_DEBUG("\n"); file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL); if (!file_priv) - return -ENOMEM; + goto err_alloc; + + file_priv->client.id = atomic_inc_return(&i915->clients.serial); + ret = i915_gem_add_client(i915, file_priv, current, + file_priv->client.id); + if (ret) + goto err_client; file->driver_priv = file_priv; + ret = i915_gem_context_open(i915, file); + if (ret) + goto err_context; + file_priv->dev_priv = i915; file_priv->file = file; + file_priv->bsd_engine = -1; spin_lock_init(&file_priv->mm.lock); INIT_LIST_HEAD(&file_priv->mm.request_list); - file_priv->bsd_engine = -1; - - ret = i915_gem_context_open(i915, file); - if (ret) - kfree(file_priv); + return 0; +err_context: + i915_gem_remove_client(file_priv); +err_client: + atomic_dec(&i915->clients.serial); + kfree(file_priv); +err_alloc: return ret; } diff --git a/drivers/gpu/drm/i915/i915_sysfs.c b/drivers/gpu/drm/i915/i915_sysfs.c index e5e6f6bb2b05..d809259456ef 100644 --- a/drivers/gpu/drm/i915/i915_sysfs.c +++ b/drivers/gpu/drm/i915/i915_sysfs.c @@ -581,6 +581,11 @@ void i915_setup_sysfs(struct drm_i915_private *dev_priv) struct device *kdev = dev_priv->drm.primary->kdev; int ret; + dev_priv->clients.root = + kobject_create_and_add("clients", &kdev->kobj); + if (!dev_priv->clients.root) + DRM_ERROR("Per-client sysfs setup failed\n"); + #ifdef CONFIG_PM if (HAS_RC6(dev_priv)) { ret = sysfs_merge_group(&kdev->kobj, @@ -641,4 +646,7 @@ void i915_teardown_sysfs(struct drm_i915_private *dev_priv) sysfs_unmerge_group(&kdev->kobj, &rc6_attr_group); sysfs_unmerge_group(&kdev->kobj, &rc6p_attr_group); #endif + + if (dev_priv->clients.root) + kobject_put(dev_priv->clients.root); }