Skip to content

Commit 26a11de

Browse files
committed
drm/i915/pmu: Fix enable count array size and bounds checking
Enable count array is supposed to have one counter for each possible engine sampler. As such, array sizing and bounds checking is not correct and would blow up the asserts if more samplers were added. No ill-effect in the current code base but lets fix it for correctness. At the same time tidy the assert for readability and robustness. v2: * One check per assert. (Chris Wilson) Signed-off-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com> Fixes: b46a33e ("drm/i915/pmu: Expose a PMU interface for perf queries") Reviewed-by: Chris Wilson <chris@chris-wilson.co.uk> Link: https://patchwork.freedesktop.org/patch/msgid/20190205130353.21105-1-tvrtko.ursulin@linux.intel.com
1 parent bf002c1 commit 26a11de

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

drivers/gpu/drm/i915/i915_pmu.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,8 @@ static void i915_pmu_enable(struct perf_event *event)
599599
* Update the bitmask of enabled events and increment
600600
* the event reference counter.
601601
*/
602-
GEM_BUG_ON(bit >= I915_PMU_MASK_BITS);
602+
BUILD_BUG_ON(ARRAY_SIZE(i915->pmu.enable_count) != I915_PMU_MASK_BITS);
603+
GEM_BUG_ON(bit >= ARRAY_SIZE(i915->pmu.enable_count));
603604
GEM_BUG_ON(i915->pmu.enable_count[bit] == ~0);
604605
i915->pmu.enable |= BIT_ULL(bit);
605606
i915->pmu.enable_count[bit]++;
@@ -620,11 +621,16 @@ static void i915_pmu_enable(struct perf_event *event)
620621
engine = intel_engine_lookup_user(i915,
621622
engine_event_class(event),
622623
engine_event_instance(event));
623-
GEM_BUG_ON(!engine);
624-
engine->pmu.enable |= BIT(sample);
625624

626-
GEM_BUG_ON(sample >= I915_PMU_SAMPLE_BITS);
625+
BUILD_BUG_ON(ARRAY_SIZE(engine->pmu.enable_count) !=
626+
I915_ENGINE_SAMPLE_COUNT);
627+
BUILD_BUG_ON(ARRAY_SIZE(engine->pmu.sample) !=
628+
I915_ENGINE_SAMPLE_COUNT);
629+
GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.enable_count));
630+
GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.sample));
627631
GEM_BUG_ON(engine->pmu.enable_count[sample] == ~0);
632+
633+
engine->pmu.enable |= BIT(sample);
628634
engine->pmu.enable_count[sample]++;
629635
}
630636

@@ -654,9 +660,11 @@ static void i915_pmu_disable(struct perf_event *event)
654660
engine = intel_engine_lookup_user(i915,
655661
engine_event_class(event),
656662
engine_event_instance(event));
657-
GEM_BUG_ON(!engine);
658-
GEM_BUG_ON(sample >= I915_PMU_SAMPLE_BITS);
663+
664+
GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.enable_count));
665+
GEM_BUG_ON(sample >= ARRAY_SIZE(engine->pmu.sample));
659666
GEM_BUG_ON(engine->pmu.enable_count[sample] == 0);
667+
660668
/*
661669
* Decrement the reference count and clear the enabled
662670
* bitmask when the last listener on an event goes away.
@@ -665,7 +673,7 @@ static void i915_pmu_disable(struct perf_event *event)
665673
engine->pmu.enable &= ~BIT(sample);
666674
}
667675

668-
GEM_BUG_ON(bit >= I915_PMU_MASK_BITS);
676+
GEM_BUG_ON(bit >= ARRAY_SIZE(i915->pmu.enable_count));
669677
GEM_BUG_ON(i915->pmu.enable_count[bit] == 0);
670678
/*
671679
* Decrement the reference count and clear the enabled

drivers/gpu/drm/i915/i915_pmu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ enum {
3131
((1 << I915_PMU_SAMPLE_BITS) + \
3232
(I915_PMU_LAST + 1 - __I915_PMU_OTHER(0)))
3333

34+
#define I915_ENGINE_SAMPLE_COUNT (I915_SAMPLE_SEMA + 1)
35+
3436
struct i915_pmu_sample {
3537
u64 cur;
3638
};

drivers/gpu/drm/i915/intel_ringbuffer.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -403,16 +403,17 @@ struct intel_engine_cs {
403403
/**
404404
* @enable_count: Reference count for the enabled samplers.
405405
*
406-
* Index number corresponds to the bit number from @enable.
406+
* Index number corresponds to @enum drm_i915_pmu_engine_sample.
407407
*/
408-
unsigned int enable_count[I915_PMU_SAMPLE_BITS];
408+
unsigned int enable_count[I915_ENGINE_SAMPLE_COUNT];
409409
/**
410410
* @sample: Counter values for sampling events.
411411
*
412412
* Our internal timer stores the current counters in this field.
413+
*
414+
* Index number corresponds to @enum drm_i915_pmu_engine_sample.
413415
*/
414-
#define I915_ENGINE_SAMPLE_MAX (I915_SAMPLE_SEMA + 1)
415-
struct i915_pmu_sample sample[I915_ENGINE_SAMPLE_MAX];
416+
struct i915_pmu_sample sample[I915_ENGINE_SAMPLE_COUNT];
416417
} pmu;
417418

418419
/*

0 commit comments

Comments
 (0)