Skip to content

Commit c74443d

Browse files
Kan LiangPeter Zijlstra
authored andcommitted
perf/x86/uncore: Support per PMU cpumask
The cpumask of some uncore units, e.g., CXL uncore units, may be wrong under some configurations. Perf may access an uncore counter of a non-existent uncore unit. The uncore driver assumes that all uncore units are symmetric among dies. A global cpumask is shared among all uncore PMUs. However, some CXL uncore units may only be available on some dies. A per PMU cpumask is introduced to track the CPU mask of this PMU. The driver searches the unit control RB tree to check whether the PMU is available on a given die, and updates the per PMU cpumask accordingly. Signed-off-by: Kan Liang <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Tested-by: Yunying Sun <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 0007f39 commit c74443d

File tree

4 files changed

+89
-5
lines changed

4 files changed

+89
-5
lines changed

arch/x86/events/intel/uncore.c

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,9 @@ static void uncore_pmu_disable(struct pmu *pmu)
843843
static ssize_t uncore_get_attr_cpumask(struct device *dev,
844844
struct device_attribute *attr, char *buf)
845845
{
846-
return cpumap_print_to_pagebuf(true, buf, &uncore_cpu_mask);
846+
struct intel_uncore_pmu *pmu = container_of(dev_get_drvdata(dev), struct intel_uncore_pmu, pmu);
847+
848+
return cpumap_print_to_pagebuf(true, buf, &pmu->cpu_mask);
847849
}
848850

849851
static DEVICE_ATTR(cpumask, S_IRUGO, uncore_get_attr_cpumask, NULL);
@@ -1453,6 +1455,18 @@ static void uncore_pci_exit(void)
14531455
}
14541456
}
14551457

1458+
static bool uncore_die_has_box(struct intel_uncore_type *type,
1459+
int die, unsigned int pmu_idx)
1460+
{
1461+
if (!type->boxes)
1462+
return true;
1463+
1464+
if (intel_uncore_find_discovery_unit_id(type->boxes, die, pmu_idx) < 0)
1465+
return false;
1466+
1467+
return true;
1468+
}
1469+
14561470
static void uncore_change_type_ctx(struct intel_uncore_type *type, int old_cpu,
14571471
int new_cpu)
14581472
{
@@ -1468,18 +1482,25 @@ static void uncore_change_type_ctx(struct intel_uncore_type *type, int old_cpu,
14681482

14691483
if (old_cpu < 0) {
14701484
WARN_ON_ONCE(box->cpu != -1);
1471-
box->cpu = new_cpu;
1485+
if (uncore_die_has_box(type, die, pmu->pmu_idx)) {
1486+
box->cpu = new_cpu;
1487+
cpumask_set_cpu(new_cpu, &pmu->cpu_mask);
1488+
}
14721489
continue;
14731490
}
14741491

1475-
WARN_ON_ONCE(box->cpu != old_cpu);
1492+
WARN_ON_ONCE(box->cpu != -1 && box->cpu != old_cpu);
14761493
box->cpu = -1;
1494+
cpumask_clear_cpu(old_cpu, &pmu->cpu_mask);
14771495
if (new_cpu < 0)
14781496
continue;
14791497

1498+
if (!uncore_die_has_box(type, die, pmu->pmu_idx))
1499+
continue;
14801500
uncore_pmu_cancel_hrtimer(box);
14811501
perf_pmu_migrate_context(&pmu->pmu, old_cpu, new_cpu);
14821502
box->cpu = new_cpu;
1503+
cpumask_set_cpu(new_cpu, &pmu->cpu_mask);
14831504
}
14841505
}
14851506

@@ -1502,7 +1523,7 @@ static void uncore_box_unref(struct intel_uncore_type **types, int id)
15021523
pmu = type->pmus;
15031524
for (i = 0; i < type->num_boxes; i++, pmu++) {
15041525
box = pmu->boxes[id];
1505-
if (box && atomic_dec_return(&box->refcnt) == 0)
1526+
if (box && box->cpu >= 0 && atomic_dec_return(&box->refcnt) == 0)
15061527
uncore_box_exit(box);
15071528
}
15081529
}
@@ -1592,7 +1613,7 @@ static int uncore_box_ref(struct intel_uncore_type **types,
15921613
pmu = type->pmus;
15931614
for (i = 0; i < type->num_boxes; i++, pmu++) {
15941615
box = pmu->boxes[id];
1595-
if (box && atomic_inc_return(&box->refcnt) == 1)
1616+
if (box && box->cpu >= 0 && atomic_inc_return(&box->refcnt) == 1)
15961617
uncore_box_init(box);
15971618
}
15981619
}

arch/x86/events/intel/uncore.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ struct intel_uncore_type {
8686
const struct attribute_group *attr_groups[4];
8787
const struct attribute_group **attr_update;
8888
struct pmu *pmu; /* for custom pmu ops */
89+
struct rb_root *boxes;
8990
/*
9091
* Uncore PMU would store relevant platform topology configuration here
9192
* to identify which platform component each PMON block of that type is
@@ -125,6 +126,7 @@ struct intel_uncore_pmu {
125126
int func_id;
126127
bool registered;
127128
atomic_t activeboxes;
129+
cpumask_t cpu_mask;
128130
struct intel_uncore_type *type;
129131
struct intel_uncore_box **boxes;
130132
};

arch/x86/events/intel/uncore_discovery.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,64 @@ get_uncore_discovery_type(struct uncore_unit_discovery *unit)
122122
return add_uncore_discovery_type(unit);
123123
}
124124

125+
static inline int pmu_idx_cmp(const void *key, const struct rb_node *b)
126+
{
127+
struct intel_uncore_discovery_unit *unit;
128+
const unsigned int *id = key;
129+
130+
unit = rb_entry(b, struct intel_uncore_discovery_unit, node);
131+
132+
if (unit->pmu_idx > *id)
133+
return -1;
134+
else if (unit->pmu_idx < *id)
135+
return 1;
136+
137+
return 0;
138+
}
139+
140+
static struct intel_uncore_discovery_unit *
141+
intel_uncore_find_discovery_unit(struct rb_root *units, int die,
142+
unsigned int pmu_idx)
143+
{
144+
struct intel_uncore_discovery_unit *unit;
145+
struct rb_node *pos;
146+
147+
if (!units)
148+
return NULL;
149+
150+
pos = rb_find_first(&pmu_idx, units, pmu_idx_cmp);
151+
if (!pos)
152+
return NULL;
153+
unit = rb_entry(pos, struct intel_uncore_discovery_unit, node);
154+
155+
if (die < 0)
156+
return unit;
157+
158+
for (; pos; pos = rb_next(pos)) {
159+
unit = rb_entry(pos, struct intel_uncore_discovery_unit, node);
160+
161+
if (unit->pmu_idx != pmu_idx)
162+
break;
163+
164+
if (unit->die == die)
165+
return unit;
166+
}
167+
168+
return NULL;
169+
}
170+
171+
int intel_uncore_find_discovery_unit_id(struct rb_root *units, int die,
172+
unsigned int pmu_idx)
173+
{
174+
struct intel_uncore_discovery_unit *unit;
175+
176+
unit = intel_uncore_find_discovery_unit(units, die, pmu_idx);
177+
if (unit)
178+
return unit->id;
179+
180+
return -1;
181+
}
182+
125183
static inline bool unit_less(struct rb_node *a, const struct rb_node *b)
126184
{
127185
struct intel_uncore_discovery_unit *a_node, *b_node;

arch/x86/events/intel/uncore_discovery.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,6 @@ u64 intel_generic_uncore_pci_read_counter(struct intel_uncore_box *box,
166166

167167
struct intel_uncore_type **
168168
intel_uncore_generic_init_uncores(enum uncore_access_type type_id, int num_extra);
169+
170+
int intel_uncore_find_discovery_unit_id(struct rb_root *units, int die,
171+
unsigned int pmu_idx);

0 commit comments

Comments
 (0)