Commit d160dc6f authored by Vinay Belgaumkar's avatar Vinay Belgaumkar Committed by Lucas De Marchi
Browse files

drm/xe: Add locks in gtidle code



The update of the residency values needs to be protected by a lock to
avoid multiple entrypoints, for example when multiple userspace clients
read the sysfs file. Other in-kernel clients are going to be added to
sample these values, making the problem worse. Protect those updates
with a raw_spinlock so it can be called by future integration with perf
pmu.

Suggested-by: default avatarLucas De Marchi <lucas.demarchi@intel.com>
Cc: Rodrigo Vivi <rodrigo.vivi@intel.com>
Cc: Lucas De Marchi <lucas.demarchi@intel.com>
Signed-off-by: default avatarVinay Belgaumkar <vinay.belgaumkar@intel.com>
Reviewed-by: default avatarLucas De Marchi <lucas.demarchi@intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20250110173308.2412232-2-lucas.demarchi@intel.com


Signed-off-by: default avatarLucas De Marchi <lucas.demarchi@intel.com>
parent c26f22da
Loading
Loading
Loading
Loading
+20 −3
Original line number Diff line number Diff line
@@ -69,6 +69,8 @@ static u64 get_residency_ms(struct xe_gt_idle *gtidle, u64 cur_residency)
{
	u64 delta, overflow_residency, prev_residency;

	lockdep_assert_held(&gtidle->lock);

	overflow_residency = BIT_ULL(32);

	/*
@@ -275,8 +277,21 @@ static ssize_t idle_status_show(struct device *dev,

	return sysfs_emit(buff, "%s\n", gt_idle_state_to_string(state));
}
static DEVICE_ATTR_RO(idle_status);

u64 xe_gt_idle_residency_msec(struct xe_gt_idle *gtidle)
{
	struct xe_guc_pc *pc = gtidle_to_pc(gtidle);
	u64 residency;
	unsigned long flags;

	raw_spin_lock_irqsave(&gtidle->lock, flags);
	residency = get_residency_ms(gtidle, gtidle->idle_residency(pc));
	raw_spin_unlock_irqrestore(&gtidle->lock, flags);

	return residency;
}

static DEVICE_ATTR_RO(idle_status);
static ssize_t idle_residency_ms_show(struct device *dev,
				      struct device_attribute *attr, char *buff)
{
@@ -285,10 +300,10 @@ static ssize_t idle_residency_ms_show(struct device *dev,
	u64 residency;

	xe_pm_runtime_get(pc_to_xe(pc));
	residency = gtidle->idle_residency(pc);
	residency = xe_gt_idle_residency_msec(gtidle);
	xe_pm_runtime_put(pc_to_xe(pc));

	return sysfs_emit(buff, "%llu\n", get_residency_ms(gtidle, residency));
	return sysfs_emit(buff, "%llu\n", residency);
}
static DEVICE_ATTR_RO(idle_residency_ms);

@@ -331,6 +346,8 @@ int xe_gt_idle_init(struct xe_gt_idle *gtidle)
	if (!kobj)
		return -ENOMEM;

	raw_spin_lock_init(&gtidle->lock);

	if (xe_gt_is_media_type(gt)) {
		snprintf(gtidle->name, sizeof(gtidle->name), "gt%d-mc", gt->info.id);
		gtidle->idle_residency = xe_guc_pc_mc6_residency;
+1 −0
Original line number Diff line number Diff line
@@ -17,5 +17,6 @@ void xe_gt_idle_disable_c6(struct xe_gt *gt);
void xe_gt_idle_enable_pg(struct xe_gt *gt);
void xe_gt_idle_disable_pg(struct xe_gt *gt);
int xe_gt_idle_pg_print(struct xe_gt *gt, struct drm_printer *p);
u64 xe_gt_idle_residency_msec(struct xe_gt_idle *gtidle);

#endif /* _XE_GT_IDLE_H_ */
+3 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
#ifndef _XE_GT_IDLE_SYSFS_TYPES_H_
#define _XE_GT_IDLE_SYSFS_TYPES_H_

#include <linux/spinlock.h>
#include <linux/types.h>

struct xe_guc_pc;
@@ -31,6 +32,8 @@ struct xe_gt_idle {
	u64 cur_residency;
	/** @prev_residency: previous residency counter */
	u64 prev_residency;
	/** @lock: Lock protecting idle residency counters */
	raw_spinlock_t lock;
	/** @idle_status: get the current idle state */
	enum xe_gt_idle_state (*idle_status)(struct xe_guc_pc *pc);
	/** @idle_residency: get idle residency counter */