From: Jani Nikula <jani.nikula@linux.intel.com>
To: Krzysztof Niemiec <krzysztof.niemiec@intel.com>,
intel-gfx@lists.freedesktop.org
Cc: dri-devel@lists.freedesktop.org,
Andi Shyti <andi.shyti@linux.intel.com>,
Janusz Krzysztofik <janusz.krzysztofik@linux.intel.com>,
Krzysztof Karas <krzysztof.karas@intel.com>,
Sebastian Brzezinka <sebastian.brzezinka@intel.com>,
Chris Wilson <chris.p.wilson@linux.intel.com>,
Joonas Lahtinen <joonas.lahtinen@intel.com>,
Krzysztof Niemiec <krzysztof.niemiec@intel.com>
Subject: Re: [RFC 1/2] drm/i915: Expose local memory information via sysfs
Date: Mon, 19 May 2025 18:57:20 +0300 [thread overview]
Message-ID: <87wmactwyn.fsf@intel.com> (raw)
In-Reply-To: <20250519153418.44543-2-krzysztof.niemiec@intel.com>
On Mon, 19 May 2025, Krzysztof Niemiec <krzysztof.niemiec@intel.com> wrote:
> Introduce sysfs entries regarding basic local memory information
> (unallocated memory and total memory, for both the entire region and the
> CPU visible part). This simplifies how external tools might read this
> information, which at the point of writing this patch is only accessible
> via the i915_query() ioctl.
>
> This change exposes that information to users without CAP_PERFMON.
>
> This change was requested in [1] by a developer of one such external
> tool, with the sysfs idea surfacing in a corresponding request for xe [2].
>
> [1] https://gitlab.freedesktop.org/drm/i915/kernel/-/issues/14153
> [2] https://gitlab.freedesktop.org/drm/xe/kernel/-/issues/4861
>
> Signed-off-by: Krzysztof Niemiec <krzysztof.niemiec@intel.com>
> ---
> drivers/gpu/drm/i915/i915_sysfs.c | 6 ++
> drivers/gpu/drm/i915/intel_memory_region.c | 106 +++++++++++++++++++++
> drivers/gpu/drm/i915/intel_memory_region.h | 3 +
> 3 files changed, 115 insertions(+)
>
> diff --git a/drivers/gpu/drm/i915/i915_sysfs.c b/drivers/gpu/drm/i915/i915_sysfs.c
> index f936e8f1f129..048d6da2f6db 100644
> --- a/drivers/gpu/drm/i915/i915_sysfs.c
> +++ b/drivers/gpu/drm/i915/i915_sysfs.c
> @@ -35,6 +35,8 @@
> #include "gt/intel_rps.h"
> #include "gt/sysfs_engines.h"
>
> +#include "intel_memory_region.h"
> +
> #include "i915_drv.h"
> #include "i915_sysfs.h"
>
> @@ -182,6 +184,8 @@ void i915_setup_sysfs(struct drm_i915_private *dev_priv)
>
> i915_gpu_error_sysfs_setup(dev_priv);
>
> + intel_memory_region_setup_sysfs(dev_priv);
> +
> intel_engines_add_sysfs(dev_priv);
> }
>
> @@ -189,6 +193,8 @@ void i915_teardown_sysfs(struct drm_i915_private *dev_priv)
> {
> struct device *kdev = dev_priv->drm.primary->kdev;
>
> + intel_memory_region_teardown_sysfs();
> +
> i915_gpu_error_sysfs_teardown(dev_priv);
>
> device_remove_bin_file(kdev, &dpf_attrs_1);
> diff --git a/drivers/gpu/drm/i915/intel_memory_region.c b/drivers/gpu/drm/i915/intel_memory_region.c
> index 59bd603e6deb..9558e300209b 100644
> --- a/drivers/gpu/drm/i915/intel_memory_region.c
> +++ b/drivers/gpu/drm/i915/intel_memory_region.c
> @@ -3,14 +3,19 @@
> * Copyright © 2019 Intel Corporation
> */
>
> +#include <linux/kobject.h>
> #include <linux/prandom.h>
> +#include <linux/sysfs.h>
>
> #include <uapi/drm/i915_drm.h>
>
> #include "intel_memory_region.h"
> #include "i915_drv.h"
> +#include "i915_sysfs.h"
> #include "i915_ttm_buddy_manager.h"
>
> +static struct kobject *memory_info_dir;
This can't be per module.
BR,
Jani.
> +
> static const struct {
> u16 class;
> u16 instance;
> @@ -423,6 +428,107 @@ void intel_memory_regions_driver_release(struct drm_i915_private *i915)
> }
> }
>
> +static ssize_t
> +vram_total_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
> +{
> + struct device *dev = kobj_to_dev(kobj->parent);
> + struct intel_memory_region *mr;
> +
> + mr = intel_memory_region_by_type(kdev_minor_to_i915(dev), INTEL_MEMORY_LOCAL);
> +
> + return sysfs_emit(buf, "%llu\n", mr->total);
> +}
> +
> +static const struct kobj_attribute vram_total_attr =
> +__ATTR(vram_total, 0444, vram_total_show, NULL);
> +
> +static ssize_t
> +vram_avail_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
> +{
> + struct device *dev = kobj_to_dev(kobj->parent);
> + struct intel_memory_region *mr;
> + u64 unallocated_size;
> + u64 dummy;
> +
> + mr = intel_memory_region_by_type(kdev_minor_to_i915(dev), INTEL_MEMORY_LOCAL);
> + intel_memory_region_avail(mr, &unallocated_size, &dummy);
> +
> + return sysfs_emit(buf, "%llu\n", unallocated_size);
> +}
> +
> +static const struct kobj_attribute vram_avail_attr =
> +__ATTR(vram_available, 0444, vram_avail_show, NULL);
> +
> +
> +static ssize_t
> +vram_total_visible_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
> +{
> + struct device *dev = kobj_to_dev(kobj->parent);
> + struct intel_memory_region *mr;
> +
> + mr = intel_memory_region_by_type(kdev_minor_to_i915(dev), INTEL_MEMORY_LOCAL);
> +
> + return sysfs_emit(buf, "%llu\n", resource_size(&mr->io));
> +}
> +
> +static const struct kobj_attribute vram_total_visible_attr =
> +__ATTR(vram_total_cpu_visible, 0444, vram_total_visible_show, NULL);
> +
> +static ssize_t
> +vram_avail_visible_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
> +{
> + struct device *dev = kobj_to_dev(kobj->parent);
> + struct intel_memory_region *mr;
> + u64 unallocated_cpu_visible_size;
> + u64 dummy;
> +
> + mr = intel_memory_region_by_type(kdev_minor_to_i915(dev), INTEL_MEMORY_LOCAL);
> + intel_memory_region_avail(mr, &dummy, &unallocated_cpu_visible_size);
> +
> + return sysfs_emit(buf, "%llu\n", unallocated_cpu_visible_size);
> +}
> +
> +static const struct kobj_attribute vram_avail_visible_attr =
> +__ATTR(vram_available_cpu_visible, 0444, vram_avail_visible_show, NULL);
> +
> +int intel_memory_region_setup_sysfs(struct drm_i915_private *i915)
> +{
> + static const struct attribute *const files[] = {
> + &vram_total_attr.attr,
> + &vram_avail_attr.attr,
> + &vram_total_visible_attr.attr,
> + &vram_avail_visible_attr.attr,
> + NULL
> + };
> + struct device *kdev = i915->drm.primary->kdev;
> + int err;
> +
> + /* Skip this function completely if the system does not support lmem */
> + if(!intel_memory_region_by_type(i915, INTEL_MEMORY_LOCAL))
> + return 0;
> +
> + memory_info_dir = kobject_create_and_add("memory_info", &kdev->kobj);
> + if (!memory_info_dir) {
> + drm_warn(&i915->drm, "Failed to create memory_info sysfs directory\n");
> + return -EAGAIN;
> + }
> +
> + err = sysfs_create_files(memory_info_dir, files);
> + if (err) {
> + drm_warn(&i915->drm, "Failed to create memory info sysfs files: %d\n", err);
> + kobject_put(memory_info_dir);
> + return err;
> + }
> +
> + return 0;
> +}
> +
> +int intel_memory_region_teardown_sysfs(void)
> +{
> + kobject_put(memory_info_dir);
> + return 0;
> +}
> +
> #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
> #include "selftests/intel_memory_region.c"
> #include "selftests/mock_region.c"
> diff --git a/drivers/gpu/drm/i915/intel_memory_region.h b/drivers/gpu/drm/i915/intel_memory_region.h
> index b3b75be9ced5..9838eca9344c 100644
> --- a/drivers/gpu/drm/i915/intel_memory_region.h
> +++ b/drivers/gpu/drm/i915/intel_memory_region.h
> @@ -132,4 +132,7 @@ struct intel_memory_region *
> i915_gem_shmem_setup(struct drm_i915_private *i915,
> u16 type, u16 instance);
>
> +int intel_memory_region_setup_sysfs(struct drm_i915_private *i915);
> +int intel_memory_region_teardown_sysfs(void);
> +
> #endif
--
Jani Nikula, Intel
next prev parent reply other threads:[~2025-05-19 15:57 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-19 15:34 [RFC 0/2] Introduce a sysfs interface for lmem information Krzysztof Niemiec
2025-05-19 15:34 ` [RFC 1/2] drm/i915: Expose local memory information via sysfs Krzysztof Niemiec
2025-05-19 15:57 ` Jani Nikula [this message]
2025-05-19 18:08 ` kernel test robot
2025-05-20 14:25 ` Krzysztof Karas
2025-05-19 15:34 ` [RFC 2/2] drm/i915: Add protections to sysfs local memory information Krzysztof Niemiec
2025-05-20 14:39 ` Krzysztof Karas
2025-05-19 16:43 ` ✗ Fi.CI.CHECKPATCH: warning for Introduce a sysfs interface for lmem information Patchwork
2025-05-19 16:43 ` ✗ Fi.CI.SPARSE: " Patchwork
2025-05-19 17:05 ` ✗ i915.CI.BAT: failure " Patchwork
2025-05-20 14:26 ` [RFC 0/2] " Krzysztof Karas
2025-05-20 15:01 ` Joonas Lahtinen
2025-05-20 17:46 ` Andi Shyti
2025-05-21 17:01 ` Krzysztof Niemiec
2025-05-21 8:06 ` Tvrtko Ursulin
2025-05-21 17:10 ` Krzysztof Niemiec
2025-05-22 4:31 ` Tvrtko Ursulin
2025-05-21 16:56 ` Krzysztof Niemiec
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87wmactwyn.fsf@intel.com \
--to=jani.nikula@linux.intel.com \
--cc=andi.shyti@linux.intel.com \
--cc=chris.p.wilson@linux.intel.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=intel-gfx@lists.freedesktop.org \
--cc=janusz.krzysztofik@linux.intel.com \
--cc=joonas.lahtinen@intel.com \
--cc=krzysztof.karas@intel.com \
--cc=krzysztof.niemiec@intel.com \
--cc=sebastian.brzezinka@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.