|
| 1 | +#include "i915_gpu_work.h" |
| 2 | +#include <linux/pid.h> |
| 3 | +#include <linux/errno.h> |
| 4 | + |
| 5 | +#include "gt/intel_context.h" |
| 6 | +#include "gt/intel_engine.h" |
| 7 | +#include "gem/i915_gem_context.h" |
| 8 | + |
| 9 | +#define CREATE_TRACE_POINTS |
| 10 | +#include "intel_power_gpu_work_period_trace.h" |
| 11 | + |
| 12 | +static s32 get_uid_ctx(struct intel_context *ce) |
| 13 | +{ |
| 14 | + struct i915_gem_context *ctx = NULL; |
| 15 | + struct task_struct *task = NULL; |
| 16 | + const struct cred *cred = NULL; |
| 17 | + s32 ret; |
| 18 | + |
| 19 | + rcu_read_lock(); |
| 20 | + ctx = rcu_dereference(ce->gem_context); |
| 21 | + if (ctx && !kref_get_unless_zero(&ctx->ref)) |
| 22 | + ctx = NULL; |
| 23 | + rcu_read_unlock(); |
| 24 | + |
| 25 | + if (!ctx) |
| 26 | + ret = -EINVAL; |
| 27 | + |
| 28 | + task = get_pid_task(ctx->pid, PIDTYPE_PID); |
| 29 | + cred = get_task_cred(task); |
| 30 | + const unsigned int uid = cred->euid.val; |
| 31 | + ret = (s32)uid; |
| 32 | + |
| 33 | + put_cred(cred); |
| 34 | + put_task_struct(task); |
| 35 | + i915_gem_context_put(ctx); |
| 36 | + return ret; |
| 37 | +} |
| 38 | + |
| 39 | +static void emit_work_period_event(struct i915_engine_work *ew) |
| 40 | +{ |
| 41 | + struct i915_work_stats * const stats = &ew->stats[0]; |
| 42 | + for (int itr = 0; itr < I915_ENGINE_WORK_STATS_COUNT; itr++) { |
| 43 | + struct i915_work_stats *stat = &stats[itr]; |
| 44 | + if (!stat->uid) |
| 45 | + continue; |
| 46 | + |
| 47 | + trace_gpu_work_period(0, stat->uid, |
| 48 | + stat->start_time_ns, stat->end_time_ns, |
| 49 | + stat->total_active_duration_ns); |
| 50 | + |
| 51 | + if (!ew->num_entries--) |
| 52 | + break; |
| 53 | + } |
| 54 | + GEM_BUG_ON(ew->num_entries != 0); |
| 55 | + memset(stats, 0, sizeof(*stats) * |
| 56 | + I915_ENGINE_WORK_STATS_COUNT); |
| 57 | + smp_wmb(); |
| 58 | +} |
| 59 | + |
| 60 | +static void i915_work_period_event_worker(struct work_struct *work) |
| 61 | +{ |
| 62 | + struct i915_engine_work *ew = |
| 63 | + container_of(work, typeof(*ew), event_work); |
| 64 | + spin_lock_bh(&ew->stats_lock); |
| 65 | + emit_work_period_event(ew); |
| 66 | + spin_unlock_bh(&ew->stats_lock); |
| 67 | +} |
| 68 | + |
| 69 | +static inline u32 get_cur_dt(struct intel_context* ce) |
| 70 | +{ |
| 71 | + struct intel_context_stats *stats = &ce->stats; |
| 72 | + s32 dt = READ_ONCE(stats->runtime.dt); |
| 73 | + if (unlikely(dt < 0)) { |
| 74 | + return 0; |
| 75 | + } |
| 76 | + return dt; |
| 77 | +} |
| 78 | + |
| 79 | +static u64 get_active_duration_ns(struct intel_context* ce) |
| 80 | +{ |
| 81 | + u64 dur = get_cur_dt(ce); |
| 82 | + if (ce->ops->flags & COPS_RUNTIME_CYCLES) |
| 83 | + dur *= ce->engine->gt->clock_period_ns; |
| 84 | + return dur; |
| 85 | +} |
| 86 | + |
| 87 | +static inline u32 get_stats_uid(s32 key, struct i915_work_stats *stats) |
| 88 | +{ |
| 89 | + return stats[key].uid; |
| 90 | +} |
| 91 | + |
| 92 | +static s32 handle_collision(s32 key, struct i915_engine_work *ew) |
| 93 | +{ |
| 94 | + struct i915_work_stats * const stats = &ew->stats[0]; |
| 95 | + u32 uid, count = 0; |
| 96 | + |
| 97 | + spin_lock(&ew->stats_lock); |
| 98 | + while (uid = get_stats_uid(key, stats)) { |
| 99 | + if (unlikely(count >= |
| 100 | + I915_ENGINE_WORK_STATS_COUNT)) { |
| 101 | + spin_unlock(&ew->stats_lock); |
| 102 | + return -ENOMEM; |
| 103 | + } |
| 104 | + |
| 105 | + if (key == I915_ENGINE_WORK_STATS_COUNT) |
| 106 | + key = 0; |
| 107 | + key++; |
| 108 | + count++; |
| 109 | + } |
| 110 | + |
| 111 | + spin_unlock(&ew->stats_lock); |
| 112 | + return key; |
| 113 | +} |
| 114 | + |
| 115 | +void i915_gpu_work_process_ctx(struct intel_context *ce, |
| 116 | + struct i915_engine_work *ew) |
| 117 | +{ |
| 118 | + struct i915_work_stats * const stats = &ew->stats[0]; |
| 119 | + struct i915_work_stats *stat; |
| 120 | + s32 key, uid; |
| 121 | + |
| 122 | + uid = get_uid_ctx(ce); |
| 123 | + // TODO: Handle this correctly |
| 124 | + if (uid < 0) |
| 125 | + return; |
| 126 | + |
| 127 | + key = HASH_MAP(uid); |
| 128 | + |
| 129 | + if (get_stats_uid(key, stats) != uid) |
| 130 | + key = handle_collision(key, ew); |
| 131 | + |
| 132 | + if (unlikely(KEY_INVALID(key))) { |
| 133 | + spin_lock(&ew->stats_lock); |
| 134 | + emit_work_period_event(ew); |
| 135 | + spin_unlock(&ew->stats_lock); |
| 136 | + key = 0; |
| 137 | + } |
| 138 | + |
| 139 | + GEM_BUG_ON(KEY_INVALID(key)); |
| 140 | + stat = &stats[key]; |
| 141 | + |
| 142 | + spin_lock(&ew->stats_lock); |
| 143 | + if (!stat->uid) { |
| 144 | + stat->uid = uid; |
| 145 | + stat->start_time_ns = ce->start_time_ns; |
| 146 | + stat->total_active_duration_ns = |
| 147 | + get_active_duration_ns(ce); |
| 148 | + ew->num_entries++; |
| 149 | + goto out; |
| 150 | + } |
| 151 | + GEM_BUG_ON(stat->uid != uid); |
| 152 | + stat->end_time_ns = ktime_get_raw_ns(); |
| 153 | + stat->total_active_duration_ns += |
| 154 | + get_active_duration_ns(ce); |
| 155 | + |
| 156 | +out: |
| 157 | + spin_unlock(&ew->stats_lock); |
| 158 | +} |
| 159 | + |
| 160 | +void i915_gpu_work_stats_init(struct intel_engine_cs *engine) |
| 161 | +{ |
| 162 | + struct i915_engine_work *ew = &engine->gpu_work; |
| 163 | + struct i915_work_stats * const stats = &ew->stats[0]; |
| 164 | + |
| 165 | + ew->enabled = false; |
| 166 | + ew->num_entries = 0; |
| 167 | + memset(stats, 0, sizeof(*stats) * |
| 168 | + I915_ENGINE_WORK_STATS_COUNT); |
| 169 | + |
| 170 | + spin_lock_init(&ew->stats_lock); |
| 171 | + INIT_WORK(&ew->event_work, i915_work_period_event_worker); |
| 172 | +} |
0 commit comments