[PATCH] drm/xe/bo: add GPU memory trace points
Tvrtko Ursulin
tursulin at ursulin.net
Wed May 21 07:49:21 UTC 2025
On 20/05/2025 18:16, Juston Li wrote:
> Add tracepoints behind CONFIG_DRM_XE_GPU_MEM_TRACEPOINTS for tracking
> global and per-process GPU memory usage.
>
> These are required by VSR on Android 12+ for reporting GPU driver memory
> allocations.
+ Maintainers
For reference, this is equivalent to 204226de27c3 ("drm/msm: Add GPU
memory traces") and uses the same shared tracepoints and format.
Two small details below, with which fixed:
Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin at igalia.com>
> Signed-off-by: Juston Li <justonli at chromium.org>
> ---
> drivers/gpu/drm/xe/Kconfig.debug | 12 +++++++
> drivers/gpu/drm/xe/xe_bo.c | 47 ++++++++++++++++++++++++++++
> drivers/gpu/drm/xe/xe_device_types.h | 15 +++++++++
> 3 files changed, 74 insertions(+)
>
> diff --git a/drivers/gpu/drm/xe/Kconfig.debug b/drivers/gpu/drm/xe/Kconfig.debug
> index 01735c6ece8ba..e2ecc6df954ce 100644
> --- a/drivers/gpu/drm/xe/Kconfig.debug
> +++ b/drivers/gpu/drm/xe/Kconfig.debug
> @@ -111,3 +111,15 @@ config DRM_XE_USERPTR_INVAL_INJECT
>
> Recommended for driver developers only.
> If in doubt, say "N".
> +
> +config CONFIG_DRM_XE_GPU_MEM_TRACEPOINTS
> + bool "Enable Xe GPU memory usage tracepoints"
> + default n
> + select TRACE_GPU_MEM
> + help
> + Choose this option to enable tracepoints for tracking
> + and per-process GPU memory usage.
Missing word:
..*global* and per-process..
> + Intended for performance profiling and required for
> + Android.
> +
> + If in doubt, say "N".
> diff --git a/drivers/gpu/drm/xe/xe_bo.c b/drivers/gpu/drm/xe/xe_bo.c
> index d99d91fe8aa98..f137d6ec5357d 100644
> --- a/drivers/gpu/drm/xe/xe_bo.c
> +++ b/drivers/gpu/drm/xe/xe_bo.c
> @@ -19,6 +19,8 @@
>
> #include <kunit/static_stub.h>
>
> +#include <trace/events/gpu_mem.h>
> +
> #include "xe_device.h"
> #include "xe_dma_buf.h"
> #include "xe_drm_client.h"
> @@ -420,6 +422,35 @@ static void xe_ttm_tt_account_subtract(struct ttm_tt *tt)
> xe_shrinker_mod_pages(xe_tt->xe->mem.shrinker, -(long)tt->num_pages, 0);
> }
>
> +#if IS_ENABLED(CONFIG_DRM_XE_GPU_MEM_TRACEPOINTS)
> +static void update_global_total_pages(struct ttm_device *ttm_dev, long num_pages)
> +{
> + struct xe_device *xe = ttm_to_xe_device(ttm_dev);
> + uint64_t global_total_pages =
> + atomic64_add_return(num_pages, &xe->global_total_pages);
Checkpatch reminds kernel should use u64 type. Here and one more just below.
Regards,
Tvrtko
> +
> + trace_gpu_mem_total(0, 0, global_total_pages << PAGE_SHIFT);
> +}
> +
> +static void update_process_mem(struct drm_file *file, ssize_t size)
> +{
> + struct xe_file *xef = to_xe_file(file);
> + uint64_t process_mem = atomic64_add_return(size, &xef->process_mem);
> +
> + rcu_read_lock(); /* Locks file->pid! */
> + trace_gpu_mem_total(0, pid_nr(rcu_dereference(file->pid)), process_mem);
> + rcu_read_unlock();
> +}
> +#else
> +static inline void update_global_total_pages(struct ttm_device *ttm_dev, long num_pages)
> +{
> +}
> +
> +static inline void update_process_mem(struct drm_file *file, ssize_t size)
> +{
> +}
> +#endif
> +
> static struct ttm_tt *xe_ttm_tt_create(struct ttm_buffer_object *ttm_bo,
> u32 page_flags)
> {
> @@ -528,6 +559,7 @@ static int xe_ttm_tt_populate(struct ttm_device *ttm_dev, struct ttm_tt *tt,
>
> xe_tt->purgeable = false;
> xe_ttm_tt_account_add(tt);
> + update_global_total_pages(ttm_dev, tt->num_pages);
>
> return 0;
> }
> @@ -542,6 +574,7 @@ static void xe_ttm_tt_unpopulate(struct ttm_device *ttm_dev, struct ttm_tt *tt)
>
> ttm_pool_free(&ttm_dev->pool, tt);
> xe_ttm_tt_account_subtract(tt);
> + update_global_total_pages(ttm_dev, -(long)tt->num_pages);
> }
>
> static void xe_ttm_tt_destroy(struct ttm_device *ttm_dev, struct ttm_tt *tt)
> @@ -1648,6 +1681,15 @@ static void xe_gem_object_free(struct drm_gem_object *obj)
> ttm_bo_put(container_of(obj, struct ttm_buffer_object, base));
> }
>
> +#if IS_ENABLED(CONFIG_DRM_XE_GPU_MEM_TRACEPOINTS)
> +static int xe_gem_object_open(struct drm_gem_object *obj,
> + struct drm_file *file_priv)
> +{
> + update_process_mem(file_priv, obj->size);
> + return 0;
> +}
> +#endif
> +
> static void xe_gem_object_close(struct drm_gem_object *obj,
> struct drm_file *file_priv)
> {
> @@ -1660,6 +1702,8 @@ static void xe_gem_object_close(struct drm_gem_object *obj,
> ttm_bo_set_bulk_move(&bo->ttm, NULL);
> xe_bo_unlock(bo);
> }
> +
> + update_process_mem(file_priv, -obj->size);
> }
>
> static vm_fault_t xe_gem_fault(struct vm_fault *vmf)
> @@ -1757,6 +1801,9 @@ static const struct vm_operations_struct xe_gem_vm_ops = {
>
> static const struct drm_gem_object_funcs xe_gem_object_funcs = {
> .free = xe_gem_object_free,
> +#if IS_ENABLED(CONFIG_DRM_XE_GPU_MEM_TRACEPOINTS)
> + .open = xe_gem_object_open,
> +#endif
> .close = xe_gem_object_close,
> .mmap = drm_gem_ttm_mmap,
> .export = xe_gem_prime_export,
> diff --git a/drivers/gpu/drm/xe/xe_device_types.h b/drivers/gpu/drm/xe/xe_device_types.h
> index f81be293b260e..5fb65f1c64cd6 100644
> --- a/drivers/gpu/drm/xe/xe_device_types.h
> +++ b/drivers/gpu/drm/xe/xe_device_types.h
> @@ -639,6 +639,13 @@ struct xe_device {
> unsigned int fsb_freq, mem_freq, is_ddr3;
> };
> #endif
> +
> +#if IS_ENABLED(CONFIG_DRM_XE_GPU_MEM_TRACEPOINTS)
> + /**
> + * @global_mem: global GPU pages usage tracked for gpu_mem tracepoints
> + */
> + atomic64_t global_total_pages;
> +#endif
> };
>
> /**
> @@ -700,6 +707,14 @@ struct xe_file {
>
> /** @refcount: ref count of this xe file */
> struct kref refcount;
> +
> +#if IS_ENABLED(CONFIG_DRM_XE_GPU_MEM_TRACEPOINTS)
> + /**
> + * @process_mem: per-process GPU memory usage tracked for gpu_mem
> + * tracepoints
> + */
> + atomic64_t process_mem;
> +#endif
> };
>
> #endif
More information about the Intel-xe
mailing list