Skip to content

Commit 4c08d4b

Browse files
kjain101djbw
authored andcommitted
powerpc/papr_scm: Add perf interface support
Performance monitoring support for papr-scm nvdimm devices via perf interface is added which includes addition of pmu functions like add/del/read/event_init for nvdimm_pmu struture. A new parameter 'priv' in added to the pdev_archdata structure to save nvdimm_pmu device pointer, to handle the unregistering of pmu device. papr_scm_pmu_register function populates the nvdimm_pmu structure with name, capabilities, cpumask along with event handling functions. Finally the populated nvdimm_pmu structure is passed to register the pmu device. Event handling functions internally uses hcall to get events and counter data. Result in power9 machine with 2 nvdimm device: Ex: List all event by perf list command:# perf list nmem nmem0/cache_rh_cnt/ [Kernel PMU event] nmem0/cache_wh_cnt/ [Kernel PMU event] nmem0/cri_res_util/ [Kernel PMU event] nmem0/ctl_res_cnt/ [Kernel PMU event] nmem0/ctl_res_tm/ [Kernel PMU event] nmem0/fast_w_cnt/ [Kernel PMU event] nmem0/host_l_cnt/ [Kernel PMU event] nmem0/host_l_dur/ [Kernel PMU event] nmem0/host_s_cnt/ [Kernel PMU event] nmem0/host_s_dur/ [Kernel PMU event] nmem0/med_r_cnt/ [Kernel PMU event] nmem0/med_r_dur/ [Kernel PMU event] nmem0/med_w_cnt/ [Kernel PMU event] nmem0/med_w_dur/ [Kernel PMU event] nmem0/mem_life/ [Kernel PMU event] nmem0/poweron_secs/ [Kernel PMU event] ... nmem1/mem_life/ [Kernel PMU event] nmem1/poweron_secs/ [Kernel PMU event] Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org> Tested-by: Nageswara R Sastry <rnsastry@linux.ibm.com> Signed-off-by: Kajol Jain <kjain@linux.ibm.com> [Add numa_map_to_online_node function call to get online node id] Reported-by: Nageswara R Sastry <rnsastry@linux.ibm.com> Reviewed-by: Madhavan Srinivasan <maddy@in.ibm.com> Link: https://lore.kernel.org/r/20220225143024.47947-4-kjain@linux.ibm.com Signed-off-by: Dan Williams <dan.j.williams@intel.com>
1 parent 0fab1ba commit 4c08d4b

2 files changed

Lines changed: 230 additions & 0 deletions

File tree

arch/powerpc/include/asm/device.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ struct dev_archdata {
4848

4949
struct pdev_archdata {
5050
u64 dma_mask;
51+
/*
52+
* Pointer to nvdimm_pmu structure, to handle the unregistering
53+
* of pmu device
54+
*/
55+
void *priv;
5156
};
5257

5358
#endif /* _ASM_POWERPC_DEVICE_H */

arch/powerpc/platforms/pseries/papr_scm.c

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <asm/papr_pdsm.h>
2020
#include <asm/mce.h>
2121
#include <asm/unaligned.h>
22+
#include <linux/perf_event.h>
2223

2324
#define BIND_ANY_ADDR (~0ul)
2425

@@ -68,6 +69,8 @@
6869
#define PAPR_SCM_PERF_STATS_EYECATCHER __stringify(SCMSTATS)
6970
#define PAPR_SCM_PERF_STATS_VERSION 0x1
7071

72+
#define to_nvdimm_pmu(_pmu) container_of(_pmu, struct nvdimm_pmu, pmu)
73+
7174
/* Struct holding a single performance metric */
7275
struct papr_scm_perf_stat {
7376
u8 stat_id[8];
@@ -120,6 +123,9 @@ struct papr_scm_priv {
120123

121124
/* length of the stat buffer as expected by phyp */
122125
size_t stat_buffer_len;
126+
127+
/* array to have event_code and stat_id mappings */
128+
char **nvdimm_events_map;
123129
};
124130

125131
static int papr_scm_pmem_flush(struct nd_region *nd_region,
@@ -340,6 +346,218 @@ static ssize_t drc_pmem_query_stats(struct papr_scm_priv *p,
340346
return 0;
341347
}
342348

349+
static int papr_scm_pmu_get_value(struct perf_event *event, struct device *dev, u64 *count)
350+
{
351+
struct papr_scm_perf_stat *stat;
352+
struct papr_scm_perf_stats *stats;
353+
struct papr_scm_priv *p = (struct papr_scm_priv *)dev->driver_data;
354+
int rc, size;
355+
356+
/* Allocate request buffer enough to hold single performance stat */
357+
size = sizeof(struct papr_scm_perf_stats) +
358+
sizeof(struct papr_scm_perf_stat);
359+
360+
if (!p || !p->nvdimm_events_map)
361+
return -EINVAL;
362+
363+
stats = kzalloc(size, GFP_KERNEL);
364+
if (!stats)
365+
return -ENOMEM;
366+
367+
stat = &stats->scm_statistic[0];
368+
memcpy(&stat->stat_id,
369+
p->nvdimm_events_map[event->attr.config],
370+
sizeof(stat->stat_id));
371+
stat->stat_val = 0;
372+
373+
rc = drc_pmem_query_stats(p, stats, 1);
374+
if (rc < 0) {
375+
kfree(stats);
376+
return rc;
377+
}
378+
379+
*count = be64_to_cpu(stat->stat_val);
380+
kfree(stats);
381+
return 0;
382+
}
383+
384+
static int papr_scm_pmu_event_init(struct perf_event *event)
385+
{
386+
struct nvdimm_pmu *nd_pmu = to_nvdimm_pmu(event->pmu);
387+
struct papr_scm_priv *p;
388+
389+
if (!nd_pmu)
390+
return -EINVAL;
391+
392+
/* test the event attr type for PMU enumeration */
393+
if (event->attr.type != event->pmu->type)
394+
return -ENOENT;
395+
396+
/* it does not support event sampling mode */
397+
if (is_sampling_event(event))
398+
return -EOPNOTSUPP;
399+
400+
/* no branch sampling */
401+
if (has_branch_stack(event))
402+
return -EOPNOTSUPP;
403+
404+
p = (struct papr_scm_priv *)nd_pmu->dev->driver_data;
405+
if (!p)
406+
return -EINVAL;
407+
408+
/* Invalid eventcode */
409+
if (event->attr.config == 0 || event->attr.config > 16)
410+
return -EINVAL;
411+
412+
return 0;
413+
}
414+
415+
static int papr_scm_pmu_add(struct perf_event *event, int flags)
416+
{
417+
u64 count;
418+
int rc;
419+
struct nvdimm_pmu *nd_pmu = to_nvdimm_pmu(event->pmu);
420+
421+
if (!nd_pmu)
422+
return -EINVAL;
423+
424+
if (flags & PERF_EF_START) {
425+
rc = papr_scm_pmu_get_value(event, nd_pmu->dev, &count);
426+
if (rc)
427+
return rc;
428+
429+
local64_set(&event->hw.prev_count, count);
430+
}
431+
432+
return 0;
433+
}
434+
435+
static void papr_scm_pmu_read(struct perf_event *event)
436+
{
437+
u64 prev, now;
438+
int rc;
439+
struct nvdimm_pmu *nd_pmu = to_nvdimm_pmu(event->pmu);
440+
441+
if (!nd_pmu)
442+
return;
443+
444+
rc = papr_scm_pmu_get_value(event, nd_pmu->dev, &now);
445+
if (rc)
446+
return;
447+
448+
prev = local64_xchg(&event->hw.prev_count, now);
449+
local64_add(now - prev, &event->count);
450+
}
451+
452+
static void papr_scm_pmu_del(struct perf_event *event, int flags)
453+
{
454+
papr_scm_pmu_read(event);
455+
}
456+
457+
static int papr_scm_pmu_check_events(struct papr_scm_priv *p, struct nvdimm_pmu *nd_pmu)
458+
{
459+
struct papr_scm_perf_stat *stat;
460+
struct papr_scm_perf_stats *stats;
461+
char *statid;
462+
int index, rc, count;
463+
u32 available_events;
464+
465+
if (!p->stat_buffer_len)
466+
return -ENOENT;
467+
468+
available_events = (p->stat_buffer_len - sizeof(struct papr_scm_perf_stats))
469+
/ sizeof(struct papr_scm_perf_stat);
470+
471+
/* Allocate the buffer for phyp where stats are written */
472+
stats = kzalloc(p->stat_buffer_len, GFP_KERNEL);
473+
if (!stats) {
474+
rc = -ENOMEM;
475+
return rc;
476+
}
477+
478+
/* Allocate memory to nvdimm_event_map */
479+
p->nvdimm_events_map = kcalloc(available_events, sizeof(char *), GFP_KERNEL);
480+
if (!p->nvdimm_events_map) {
481+
rc = -ENOMEM;
482+
goto out_stats;
483+
}
484+
485+
/* Called to get list of events supported */
486+
rc = drc_pmem_query_stats(p, stats, 0);
487+
if (rc)
488+
goto out_nvdimm_events_map;
489+
490+
for (index = 0, stat = stats->scm_statistic, count = 0;
491+
index < available_events; index++, ++stat) {
492+
statid = kzalloc(strlen(stat->stat_id) + 1, GFP_KERNEL);
493+
if (!statid) {
494+
rc = -ENOMEM;
495+
goto out_nvdimm_events_map;
496+
}
497+
498+
strcpy(statid, stat->stat_id);
499+
p->nvdimm_events_map[count] = statid;
500+
count++;
501+
}
502+
p->nvdimm_events_map[count] = NULL;
503+
kfree(stats);
504+
return 0;
505+
506+
out_nvdimm_events_map:
507+
kfree(p->nvdimm_events_map);
508+
out_stats:
509+
kfree(stats);
510+
return rc;
511+
}
512+
513+
static void papr_scm_pmu_register(struct papr_scm_priv *p)
514+
{
515+
struct nvdimm_pmu *nd_pmu;
516+
int rc, nodeid;
517+
518+
nd_pmu = kzalloc(sizeof(*nd_pmu), GFP_KERNEL);
519+
if (!nd_pmu) {
520+
rc = -ENOMEM;
521+
goto pmu_err_print;
522+
}
523+
524+
rc = papr_scm_pmu_check_events(p, nd_pmu);
525+
if (rc)
526+
goto pmu_check_events_err;
527+
528+
nd_pmu->pmu.task_ctx_nr = perf_invalid_context;
529+
nd_pmu->pmu.name = nvdimm_name(p->nvdimm);
530+
nd_pmu->pmu.event_init = papr_scm_pmu_event_init;
531+
nd_pmu->pmu.read = papr_scm_pmu_read;
532+
nd_pmu->pmu.add = papr_scm_pmu_add;
533+
nd_pmu->pmu.del = papr_scm_pmu_del;
534+
535+
nd_pmu->pmu.capabilities = PERF_PMU_CAP_NO_INTERRUPT |
536+
PERF_PMU_CAP_NO_EXCLUDE;
537+
538+
/*updating the cpumask variable */
539+
nodeid = numa_map_to_online_node(dev_to_node(&p->pdev->dev));
540+
nd_pmu->arch_cpumask = *cpumask_of_node(nodeid);
541+
542+
rc = register_nvdimm_pmu(nd_pmu, p->pdev);
543+
if (rc)
544+
goto pmu_register_err;
545+
546+
/*
547+
* Set archdata.priv value to nvdimm_pmu structure, to handle the
548+
* unregistering of pmu device.
549+
*/
550+
p->pdev->archdata.priv = nd_pmu;
551+
return;
552+
553+
pmu_register_err:
554+
kfree(p->nvdimm_events_map);
555+
pmu_check_events_err:
556+
kfree(nd_pmu);
557+
pmu_err_print:
558+
dev_info(&p->pdev->dev, "nvdimm pmu didn't register rc=%d\n", rc);
559+
}
560+
343561
/*
344562
* Issue hcall to retrieve dimm health info and populate papr_scm_priv with the
345563
* health information.
@@ -1236,6 +1454,7 @@ static int papr_scm_probe(struct platform_device *pdev)
12361454
goto err2;
12371455

12381456
platform_set_drvdata(pdev, p);
1457+
papr_scm_pmu_register(p);
12391458

12401459
return 0;
12411460

@@ -1254,6 +1473,12 @@ static int papr_scm_remove(struct platform_device *pdev)
12541473

12551474
nvdimm_bus_unregister(p->bus);
12561475
drc_pmem_unbind(p);
1476+
1477+
if (pdev->archdata.priv)
1478+
unregister_nvdimm_pmu(pdev->archdata.priv);
1479+
1480+
pdev->archdata.priv = NULL;
1481+
kfree(p->nvdimm_events_map);
12571482
kfree(p->bus_desc.provider_name);
12581483
kfree(p);
12591484

0 commit comments

Comments
 (0)