1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Performance event support for s390x - CPU-measurement Counter Facility 4 * 5 * Copyright IBM Corp. 2012, 2023 6 * Author(s): Hendrik Brueckner <brueckner@linux.ibm.com> 7 * Thomas Richter <tmricht@linux.ibm.com> 8 */ 9 #define pr_fmt(fmt) "cpum_cf: " fmt 10 11 #include <linux/kernel.h> 12 #include <linux/kernel_stat.h> 13 #include <linux/percpu.h> 14 #include <linux/notifier.h> 15 #include <linux/init.h> 16 #include <linux/miscdevice.h> 17 #include <linux/perf_event.h> 18 #include <linux/nospec.h> 19 20 #include <asm/cpu_mf.h> 21 #include <asm/hwctrset.h> 22 #include <asm/debug.h> 23 24 /* Perf PMU definitions for the counter facility */ 25 #define PERF_CPUM_CF_MAX_CTR 0xffffUL /* Max ctr for ECCTR */ 26 #define PERF_EVENT_CPUM_CF_DIAG 0xBC000UL /* Event: Counter sets */ 27 28 enum cpumf_ctr_set { 29 CPUMF_CTR_SET_BASIC = 0, /* Basic Counter Set */ 30 CPUMF_CTR_SET_USER = 1, /* Problem-State Counter Set */ 31 CPUMF_CTR_SET_CRYPTO = 2, /* Crypto-Activity Counter Set */ 32 CPUMF_CTR_SET_EXT = 3, /* Extended Counter Set */ 33 CPUMF_CTR_SET_MT_DIAG = 4, /* MT-diagnostic Counter Set */ 34 35 /* Maximum number of counter sets */ 36 CPUMF_CTR_SET_MAX, 37 }; 38 39 #define CPUMF_LCCTL_ENABLE_SHIFT 16 40 #define CPUMF_LCCTL_ACTCTL_SHIFT 0 41 42 static inline void ctr_set_enable(u64 *state, u64 ctrsets) 43 { 44 *state |= ctrsets << CPUMF_LCCTL_ENABLE_SHIFT; 45 } 46 47 static inline void ctr_set_disable(u64 *state, u64 ctrsets) 48 { 49 *state &= ~(ctrsets << CPUMF_LCCTL_ENABLE_SHIFT); 50 } 51 52 static inline void ctr_set_start(u64 *state, u64 ctrsets) 53 { 54 *state |= ctrsets << CPUMF_LCCTL_ACTCTL_SHIFT; 55 } 56 57 static inline void ctr_set_stop(u64 *state, u64 ctrsets) 58 { 59 *state &= ~(ctrsets << CPUMF_LCCTL_ACTCTL_SHIFT); 60 } 61 62 static inline int ctr_stcctm(enum cpumf_ctr_set set, u64 range, u64 *dest) 63 { 64 switch (set) { 65 case CPUMF_CTR_SET_BASIC: 66 return stcctm(BASIC, range, dest); 67 case CPUMF_CTR_SET_USER: 68 return stcctm(PROBLEM_STATE, range, dest); 69 case CPUMF_CTR_SET_CRYPTO: 70 return stcctm(CRYPTO_ACTIVITY, range, dest); 71 case CPUMF_CTR_SET_EXT: 72 return stcctm(EXTENDED, range, dest); 73 case CPUMF_CTR_SET_MT_DIAG: 74 return stcctm(MT_DIAG_CLEARING, range, dest); 75 case CPUMF_CTR_SET_MAX: 76 return 3; 77 } 78 return 3; 79 } 80 81 struct cpu_cf_events { 82 refcount_t refcnt; /* Reference count */ 83 atomic_t ctr_set[CPUMF_CTR_SET_MAX]; 84 u64 state; /* For perf_event_open SVC */ 85 u64 dev_state; /* For /dev/hwctr */ 86 unsigned int flags; 87 size_t used; /* Bytes used in data */ 88 size_t usedss; /* Bytes used in start/stop */ 89 unsigned char start[PAGE_SIZE]; /* Counter set at event add */ 90 unsigned char stop[PAGE_SIZE]; /* Counter set at event delete */ 91 unsigned char data[PAGE_SIZE]; /* Counter set at /dev/hwctr */ 92 unsigned int sets; /* # Counter set saved in memory */ 93 }; 94 95 static unsigned int cfdiag_cpu_speed; /* CPU speed for CF_DIAG trailer */ 96 static debug_info_t *cf_dbg; 97 98 /* 99 * The CPU Measurement query counter information instruction contains 100 * information which varies per machine generation, but is constant and 101 * does not change when running on a particular machine, such as counter 102 * first and second version number. This is needed to determine the size 103 * of counter sets. Extract this information at device driver initialization. 104 */ 105 static struct cpumf_ctr_info cpumf_ctr_info; 106 107 struct cpu_cf_ptr { 108 struct cpu_cf_events *cpucf; 109 }; 110 111 static struct cpu_cf_root { /* Anchor to per CPU data */ 112 refcount_t refcnt; /* Overall active events */ 113 unsigned int tskctx; /* Users tracking all CPUs (cpu == -1) */ 114 struct cpu_cf_ptr __percpu *cfptr; 115 } cpu_cf_root; 116 117 /* 118 * Serialize event initialization and event removal. Both are called from 119 * user space in task context with perf_event_open() and close() 120 * system calls. 121 * 122 * This mutex serializes the allocation and removal of the per CPU counter 123 * data via cpum_cf_alloc_cpu() and cpum_cf_free_cpu(). They are called with 124 * this mutex held at event initialization via cpumf_pmu_event_init(), at 125 * event removal via call back function hw_perf_event_destroy() when the 126 * event is deleted, and from the CPU hotplug prepare/dead callbacks. The 127 * mutex enforces correct bookkeeping of pointer and reference counts 128 * anchored by struct cpu_cf_root and protects the access to 129 * cpu_cf_root::refcnt, cpu_cf_root::tskctx and the per CPU pointers 130 * stored in cpu_cf_root::cfptr. 131 */ 132 static DEFINE_MUTEX(pmc_reserve_mutex); 133 134 /* 135 * Get pointer to per-cpu structure. 136 * 137 * Function get_cpu_cfhw() is called from 138 * - cfset_copy_all(): This function is protected by cpus_read_lock(), so 139 * CPU hot plug remove can not happen. Event removal requires a close() 140 * first. 141 * 142 * Function this_cpu_cfhw() is called from perf common code functions: 143 * - pmu_{en|dis}able(), pmu_{add|del}()and pmu_{start|stop}(): 144 * All functions execute with interrupts disabled on that particular CPU. 145 * - cfset_ioctl_{on|off}, cfset_cpu_read(): see comment cfset_copy_all(). 146 * 147 * Therefore it is safe to access the CPU specific pointer to the event. 148 */ 149 static struct cpu_cf_events *get_cpu_cfhw(int cpu) 150 { 151 struct cpu_cf_ptr __percpu *p = cpu_cf_root.cfptr; 152 153 if (p) { 154 struct cpu_cf_ptr *q = per_cpu_ptr(p, cpu); 155 156 return q->cpucf; 157 } 158 return NULL; 159 } 160 161 static struct cpu_cf_events *this_cpu_cfhw(void) 162 { 163 return get_cpu_cfhw(smp_processor_id()); 164 } 165 166 /* Disable counter sets on dedicated CPU */ 167 static void cpum_cf_reset_cpu(void *flags) 168 { 169 lcctl(0); 170 } 171 172 /* Free per CPU data when the last event is removed. */ 173 static void cpum_cf_free_root(unsigned int num) 174 { 175 struct cpu_cf_ptr __percpu *p = cpu_cf_root.cfptr; 176 177 if (!refcount_sub_and_test(num, &cpu_cf_root.refcnt)) 178 return; 179 cpu_cf_root.cfptr = NULL; 180 free_percpu(p); 181 irq_subclass_unregister(IRQ_SUBCLASS_MEASUREMENT_ALERT); 182 on_each_cpu(cpum_cf_reset_cpu, NULL, 1); 183 debug_sprintf_event(cf_dbg, 4, "%s root.refcnt %u cfptr %d\n", 184 __func__, refcount_read(&cpu_cf_root.refcnt), 185 !cpu_cf_root.cfptr); 186 } 187 188 /* 189 * On initialization of first event also allocate per CPU data dynamically. 190 * Start with an array of pointers, the array size is the maximum number of 191 * CPUs possible, which might be larger than the number of CPUs currently 192 * online. 193 */ 194 static int cpum_cf_alloc_root(unsigned int num) 195 { 196 int rc = 0; 197 198 if (refcount_add_not_zero(num, &cpu_cf_root.refcnt)) 199 return rc; 200 201 /* The memory is already zeroed. */ 202 cpu_cf_root.cfptr = alloc_percpu(struct cpu_cf_ptr); 203 if (cpu_cf_root.cfptr) { 204 refcount_set(&cpu_cf_root.refcnt, num); 205 on_each_cpu(cpum_cf_reset_cpu, NULL, 1); 206 irq_subclass_register(IRQ_SUBCLASS_MEASUREMENT_ALERT); 207 } else { 208 rc = -ENOMEM; 209 } 210 211 return rc; 212 } 213 214 /* 215 * Remove num references to the CPU counter data structure of a PMU. 216 * Called with pmc_reserve_mutex held. 217 */ 218 static void cpum_cf_free_cpu(int cpu, unsigned int num) 219 { 220 struct cpu_cf_events *cpuhw; 221 struct cpu_cf_ptr *p; 222 223 lockdep_assert_held(&pmc_reserve_mutex); 224 /* 225 * When invoked via CPU hotplug handler, there might be no events 226 * installed or that particular CPU might not have an 227 * event installed. This anchor pointer can be NULL! 228 */ 229 if (!cpu_cf_root.cfptr) 230 return; 231 p = per_cpu_ptr(cpu_cf_root.cfptr, cpu); 232 cpuhw = p->cpucf; 233 /* 234 * Might be zero when called from CPU hotplug handler and no event 235 * installed on that CPU, but on different CPUs. 236 */ 237 if (!cpuhw) 238 return; 239 240 if (refcount_sub_and_test(num, &cpuhw->refcnt)) { 241 p->cpucf = NULL; 242 kfree(cpuhw); 243 } 244 cpum_cf_free_root(num); 245 } 246 247 /* 248 * Add num references to the CPU counter data structure of a PMU and 249 * allocate it when necessary. Called with pmc_reserve_mutex held. 250 */ 251 static int cpum_cf_alloc_cpu(int cpu, unsigned int num) 252 { 253 struct cpu_cf_events *cpuhw; 254 struct cpu_cf_ptr *p; 255 int rc; 256 257 lockdep_assert_held(&pmc_reserve_mutex); 258 rc = cpum_cf_alloc_root(num); 259 if (rc) 260 return rc; 261 p = per_cpu_ptr(cpu_cf_root.cfptr, cpu); 262 cpuhw = p->cpucf; 263 264 if (!cpuhw) { 265 cpuhw = kzalloc_obj(*cpuhw); 266 if (cpuhw) { 267 p->cpucf = cpuhw; 268 refcount_set(&cpuhw->refcnt, num); 269 } else { 270 rc = -ENOMEM; 271 } 272 } else { 273 refcount_add(num, &cpuhw->refcnt); 274 } 275 if (rc) { 276 /* 277 * Error in allocation of event, decrement anchor. Since 278 * cpu_cf_event in not created, its destroy() function is not 279 * invoked. Adjust the reference counter for the anchor. 280 */ 281 cpum_cf_free_root(num); 282 } 283 return rc; 284 } 285 286 /* 287 * Create/delete per CPU data structures for /dev/hwctr interface and events 288 * created by perf_event_open(). 289 * If cpu is -1, track task on all available CPUs. This requires 290 * allocation of hardware data structures for all CPUs. This setup handles 291 * perf_event_open() with task context and /dev/hwctr interface. 292 * If cpu is non-zero install event on this CPU only. This setup handles 293 * perf_event_open() with CPU context. 294 * Users with cpu == -1 are counted in cpu_cf_root::tskctx. The CPU hotplug 295 * prepare and dead callbacks use this count to install and remove the per 296 * CPU counter data on a new or dying CPU. 297 */ 298 static int cpum_cf_alloc_cpuslocked(int cpu) 299 { 300 cpumask_var_t mask; 301 int rc; 302 303 lockdep_assert_cpus_held(); 304 if (cpu == -1) { 305 if (!zalloc_cpumask_var(&mask, GFP_KERNEL)) 306 return -ENOMEM; 307 mutex_lock(&pmc_reserve_mutex); 308 for_each_online_cpu(cpu) { 309 rc = cpum_cf_alloc_cpu(cpu, 1); 310 if (rc) { 311 for_each_cpu(cpu, mask) 312 cpum_cf_free_cpu(cpu, 1); 313 break; 314 } 315 cpumask_set_cpu(cpu, mask); 316 } 317 if (!rc) 318 cpu_cf_root.tskctx++; 319 mutex_unlock(&pmc_reserve_mutex); 320 free_cpumask_var(mask); 321 } else { 322 mutex_lock(&pmc_reserve_mutex); 323 rc = cpum_cf_alloc_cpu(cpu, 1); 324 mutex_unlock(&pmc_reserve_mutex); 325 } 326 return rc; 327 } 328 329 static int cpum_cf_alloc(int cpu) 330 { 331 int rc; 332 333 cpus_read_lock(); 334 rc = cpum_cf_alloc_cpuslocked(cpu); 335 cpus_read_unlock(); 336 return rc; 337 } 338 339 static void cpum_cf_free_cpuslocked(int cpu) 340 { 341 lockdep_assert_cpus_held(); 342 mutex_lock(&pmc_reserve_mutex); 343 if (cpu == -1) { 344 cpu_cf_root.tskctx--; 345 for_each_online_cpu(cpu) 346 cpum_cf_free_cpu(cpu, 1); 347 } else { 348 cpum_cf_free_cpu(cpu, 1); 349 } 350 mutex_unlock(&pmc_reserve_mutex); 351 } 352 353 static void cpum_cf_free(int cpu) 354 { 355 cpus_read_lock(); 356 cpum_cf_free_cpuslocked(cpu); 357 cpus_read_unlock(); 358 } 359 360 #define CF_DIAG_CTRSET_DEF 0xfeef /* Counter set header mark */ 361 /* interval in seconds */ 362 363 /* Counter sets are stored as data stream in a page sized memory buffer and 364 * exported to user space via raw data attached to the event sample data. 365 * Each counter set starts with an eight byte header consisting of: 366 * - a two byte eye catcher (0xfeef) 367 * - a one byte counter set number 368 * - a two byte counter set size (indicates the number of counters in this set) 369 * - a three byte reserved value (must be zero) to make the header the same 370 * size as a counter value. 371 * All counter values are eight byte in size. 372 * 373 * All counter sets are followed by a 64 byte trailer. 374 * The trailer consists of a: 375 * - flag field indicating valid fields when corresponding bit set 376 * - the counter facility first and second version number 377 * - the CPU speed if nonzero 378 * - the time stamp the counter sets have been collected 379 * - the time of day (TOD) base value 380 * - the machine type. 381 * 382 * The counter sets are saved when the process is prepared to be executed on a 383 * CPU and saved again when the process is going to be removed from a CPU. 384 * The difference of both counter sets are calculated and stored in the event 385 * sample data area. 386 */ 387 struct cf_ctrset_entry { /* CPU-M CF counter set entry (8 byte) */ 388 unsigned int def:16; /* 0-15 Data Entry Format */ 389 unsigned int set:16; /* 16-31 Counter set identifier */ 390 unsigned int ctr:16; /* 32-47 Number of stored counters */ 391 unsigned int res1:16; /* 48-63 Reserved */ 392 }; 393 394 struct cf_trailer_entry { /* CPU-M CF_DIAG trailer (64 byte) */ 395 /* 0 - 7 */ 396 union { 397 struct { 398 unsigned int clock_base:1; /* TOD clock base set */ 399 unsigned int speed:1; /* CPU speed set */ 400 /* Measurement alerts */ 401 unsigned int mtda:1; /* Loss of MT ctr. data alert */ 402 unsigned int caca:1; /* Counter auth. change alert */ 403 unsigned int lcda:1; /* Loss of counter data alert */ 404 }; 405 unsigned long flags; /* 0-63 All indicators */ 406 }; 407 /* 8 - 15 */ 408 unsigned int cfvn:16; /* 64-79 Ctr First Version */ 409 unsigned int csvn:16; /* 80-95 Ctr Second Version */ 410 unsigned int cpu_speed:32; /* 96-127 CPU speed */ 411 /* 16 - 23 */ 412 unsigned long timestamp; /* 128-191 Timestamp (TOD) */ 413 /* 24 - 55 */ 414 union { 415 struct { 416 unsigned long progusage1; 417 unsigned long progusage2; 418 unsigned long progusage3; 419 unsigned long tod_base; 420 }; 421 unsigned long progusage[4]; 422 }; 423 /* 56 - 63 */ 424 unsigned int mach_type:16; /* Machine type */ 425 unsigned int res1:16; /* Reserved */ 426 unsigned int res2:32; /* Reserved */ 427 }; 428 429 /* Create the trailer data at the end of a page. */ 430 static void cfdiag_trailer(struct cf_trailer_entry *te) 431 { 432 struct cpuid cpuid; 433 434 te->cfvn = cpumf_ctr_info.cfvn; /* Counter version numbers */ 435 te->csvn = cpumf_ctr_info.csvn; 436 437 get_cpu_id(&cpuid); /* Machine type */ 438 te->mach_type = cpuid.machine; 439 te->cpu_speed = cfdiag_cpu_speed; 440 if (te->cpu_speed) 441 te->speed = 1; 442 te->clock_base = 1; /* Save clock base */ 443 te->tod_base = tod_clock_base.tod; 444 te->timestamp = get_tod_clock_fast(); 445 } 446 447 /* 448 * The number of counters per counter set varies between machine generations, 449 * but is constant when running on a particular machine generation. 450 * Determine each counter set size at device driver initialization and 451 * retrieve it later. 452 */ 453 static size_t cpumf_ctr_setsizes[CPUMF_CTR_SET_MAX]; 454 static void cpum_cf_make_setsize(enum cpumf_ctr_set ctrset) 455 { 456 size_t ctrset_size = 0; 457 458 switch (ctrset) { 459 case CPUMF_CTR_SET_BASIC: 460 if (cpumf_ctr_info.cfvn >= 1) 461 ctrset_size = 6; 462 break; 463 case CPUMF_CTR_SET_USER: 464 if (cpumf_ctr_info.cfvn == 1) 465 ctrset_size = 6; 466 else if (cpumf_ctr_info.cfvn >= 3) 467 ctrset_size = 2; 468 break; 469 case CPUMF_CTR_SET_CRYPTO: 470 if (cpumf_ctr_info.csvn >= 1 && cpumf_ctr_info.csvn <= 5) 471 ctrset_size = 16; 472 else if (cpumf_ctr_info.csvn >= 6) 473 ctrset_size = 20; 474 break; 475 case CPUMF_CTR_SET_EXT: 476 if (cpumf_ctr_info.csvn == 1) 477 ctrset_size = 32; 478 else if (cpumf_ctr_info.csvn == 2) 479 ctrset_size = 48; 480 else if (cpumf_ctr_info.csvn >= 3 && cpumf_ctr_info.csvn <= 5) 481 ctrset_size = 128; 482 else if (cpumf_ctr_info.csvn >= 6 && cpumf_ctr_info.csvn <= 8) 483 ctrset_size = 160; 484 break; 485 case CPUMF_CTR_SET_MT_DIAG: 486 if (cpumf_ctr_info.csvn > 3) 487 ctrset_size = 48; 488 break; 489 case CPUMF_CTR_SET_MAX: 490 break; 491 } 492 cpumf_ctr_setsizes[ctrset] = ctrset_size; 493 } 494 495 /* 496 * Return the maximum possible counter set size (in number of 8 byte counters) 497 * depending on type and model number. 498 */ 499 static size_t cpum_cf_read_setsize(enum cpumf_ctr_set ctrset) 500 { 501 return cpumf_ctr_setsizes[ctrset]; 502 } 503 504 /* Read a counter set. The counter set number determines the counter set and 505 * the CPUM-CF first and second version number determine the number of 506 * available counters in each counter set. 507 * Each counter set starts with header containing the counter set number and 508 * the number of eight byte counters. 509 * 510 * The functions returns the number of bytes occupied by this counter set 511 * including the header. 512 * If there is no counter in the counter set, this counter set is useless and 513 * zero is returned on this case. 514 * 515 * Note that the counter sets may not be enabled or active and the stcctm 516 * instruction might return error 3. Depending on error_ok value this is ok, 517 * for example when called from cpumf_pmu_start() call back function. 518 */ 519 static size_t cfdiag_getctrset(struct cf_ctrset_entry *ctrdata, int ctrset, 520 size_t room, bool error_ok) 521 { 522 size_t ctrset_size, need = 0; 523 int rc = 3; /* Assume write failure */ 524 525 ctrdata->def = CF_DIAG_CTRSET_DEF; 526 ctrdata->set = ctrset; 527 ctrdata->res1 = 0; 528 ctrset_size = cpum_cf_read_setsize(ctrset); 529 530 if (ctrset_size) { /* Save data */ 531 need = ctrset_size * sizeof(u64) + sizeof(*ctrdata); 532 if (need <= room) { 533 rc = ctr_stcctm(ctrset, ctrset_size, 534 (u64 *)(ctrdata + 1)); 535 } 536 if (rc != 3 || error_ok) 537 ctrdata->ctr = ctrset_size; 538 else 539 need = 0; 540 } 541 542 return need; 543 } 544 545 static const u64 cpumf_ctr_ctl[CPUMF_CTR_SET_MAX] = { 546 [CPUMF_CTR_SET_BASIC] = 0x02, 547 [CPUMF_CTR_SET_USER] = 0x04, 548 [CPUMF_CTR_SET_CRYPTO] = 0x08, 549 [CPUMF_CTR_SET_EXT] = 0x01, 550 [CPUMF_CTR_SET_MT_DIAG] = 0x20, 551 }; 552 553 /* Read out all counter sets and save them in the provided data buffer. 554 * The last 64 byte host an artificial trailer entry. 555 */ 556 static size_t cfdiag_getctr(void *data, size_t sz, unsigned long auth, 557 bool error_ok) 558 { 559 struct cf_trailer_entry *trailer; 560 size_t offset = 0, done; 561 int i; 562 563 memset(data, 0, sz); 564 sz -= sizeof(*trailer); /* Always room for trailer */ 565 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) { 566 struct cf_ctrset_entry *ctrdata = data + offset; 567 568 if (!(auth & cpumf_ctr_ctl[i])) 569 continue; /* Counter set not authorized */ 570 571 done = cfdiag_getctrset(ctrdata, i, sz - offset, error_ok); 572 offset += done; 573 } 574 trailer = data + offset; 575 cfdiag_trailer(trailer); 576 return offset + sizeof(*trailer); 577 } 578 579 /* Calculate the difference for each counter in a counter set. */ 580 static void cfdiag_diffctrset(u64 *pstart, u64 *pstop, int counters) 581 { 582 for (; --counters >= 0; ++pstart, ++pstop) 583 if (*pstop >= *pstart) 584 *pstop -= *pstart; 585 else 586 *pstop = *pstart - *pstop + 1; 587 } 588 589 /* Scan the counter sets and calculate the difference of each counter 590 * in each set. The result is the increment of each counter during the 591 * period the counter set has been activated. 592 * 593 * Return true on success. 594 */ 595 static int cfdiag_diffctr(struct cpu_cf_events *cpuhw, unsigned long auth) 596 { 597 struct cf_trailer_entry *trailer_start, *trailer_stop; 598 struct cf_ctrset_entry *ctrstart, *ctrstop; 599 size_t offset = 0; 600 int i; 601 602 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) { 603 ctrstart = (struct cf_ctrset_entry *)(cpuhw->start + offset); 604 ctrstop = (struct cf_ctrset_entry *)(cpuhw->stop + offset); 605 606 /* Counter set not authorized */ 607 if (!(auth & cpumf_ctr_ctl[i])) 608 continue; 609 /* Counter set size zero was not saved */ 610 if (!cpum_cf_read_setsize(i)) 611 continue; 612 613 if (memcmp(ctrstop, ctrstart, sizeof(*ctrstop))) { 614 pr_err_once("cpum_cf_diag counter set compare error " 615 "in set %i\n", ctrstart->set); 616 return 0; 617 } 618 if (ctrstart->def == CF_DIAG_CTRSET_DEF) { 619 cfdiag_diffctrset((u64 *)(ctrstart + 1), 620 (u64 *)(ctrstop + 1), ctrstart->ctr); 621 offset += ctrstart->ctr * sizeof(u64) + 622 sizeof(*ctrstart); 623 } 624 } 625 626 /* Save time_stamp from start of event in stop's trailer */ 627 trailer_start = (struct cf_trailer_entry *)(cpuhw->start + offset); 628 trailer_stop = (struct cf_trailer_entry *)(cpuhw->stop + offset); 629 trailer_stop->progusage[0] = trailer_start->timestamp; 630 631 return 1; 632 } 633 634 static enum cpumf_ctr_set get_counter_set(u64 event) 635 { 636 int set = CPUMF_CTR_SET_MAX; 637 638 if (event < 32) 639 set = CPUMF_CTR_SET_BASIC; 640 else if (event < 64) 641 set = CPUMF_CTR_SET_USER; 642 else if (event < 128) 643 set = CPUMF_CTR_SET_CRYPTO; 644 else if (event < 288) 645 set = CPUMF_CTR_SET_EXT; 646 else if (event >= 448 && event < 496) 647 set = CPUMF_CTR_SET_MT_DIAG; 648 649 return set; 650 } 651 652 static int validate_ctr_version(const u64 config, enum cpumf_ctr_set set) 653 { 654 u16 mtdiag_ctl; 655 int err = 0; 656 657 /* check required version for counter sets */ 658 switch (set) { 659 case CPUMF_CTR_SET_BASIC: 660 case CPUMF_CTR_SET_USER: 661 if (cpumf_ctr_info.cfvn < 1) 662 err = -EOPNOTSUPP; 663 break; 664 case CPUMF_CTR_SET_CRYPTO: 665 if ((cpumf_ctr_info.csvn >= 1 && cpumf_ctr_info.csvn <= 5 && 666 config > 79) || (cpumf_ctr_info.csvn >= 6 && config > 83)) 667 err = -EOPNOTSUPP; 668 break; 669 case CPUMF_CTR_SET_EXT: 670 if (cpumf_ctr_info.csvn < 1) 671 err = -EOPNOTSUPP; 672 if ((cpumf_ctr_info.csvn == 1 && config > 159) || 673 (cpumf_ctr_info.csvn == 2 && config > 175) || 674 (cpumf_ctr_info.csvn >= 3 && cpumf_ctr_info.csvn <= 5 && 675 config > 255) || 676 (cpumf_ctr_info.csvn >= 6 && config > 287)) 677 err = -EOPNOTSUPP; 678 break; 679 case CPUMF_CTR_SET_MT_DIAG: 680 if (cpumf_ctr_info.csvn <= 3) 681 err = -EOPNOTSUPP; 682 /* 683 * MT-diagnostic counters are read-only. The counter set 684 * is automatically enabled and activated on all CPUs with 685 * multithreading (SMT). Deactivation of multithreading 686 * also disables the counter set. State changes are ignored 687 * by lcctl(). Because Linux controls SMT enablement through 688 * a kernel parameter only, the counter set is either disabled 689 * or enabled and active. 690 * 691 * Thus, the counters can only be used if SMT is on and the 692 * counter set is enabled and active. 693 */ 694 mtdiag_ctl = cpumf_ctr_ctl[CPUMF_CTR_SET_MT_DIAG]; 695 if (!((cpumf_ctr_info.auth_ctl & mtdiag_ctl) && 696 (cpumf_ctr_info.enable_ctl & mtdiag_ctl) && 697 (cpumf_ctr_info.act_ctl & mtdiag_ctl))) 698 err = -EOPNOTSUPP; 699 break; 700 case CPUMF_CTR_SET_MAX: 701 err = -EOPNOTSUPP; 702 } 703 704 return err; 705 } 706 707 /* 708 * Change the CPUMF state to active. 709 * Enable and activate the CPU-counter sets according 710 * to the per-cpu control state. 711 */ 712 static void cpumf_pmu_enable(struct pmu *pmu) 713 { 714 struct cpu_cf_events *cpuhw = this_cpu_cfhw(); 715 int err; 716 717 if (!cpuhw || (cpuhw->flags & PMU_F_ENABLED)) 718 return; 719 720 err = lcctl(cpuhw->state | cpuhw->dev_state); 721 if (err) 722 pr_err("Enabling the performance measuring unit failed with rc=%x\n", err); 723 else 724 cpuhw->flags |= PMU_F_ENABLED; 725 } 726 727 /* 728 * Change the CPUMF state to inactive. 729 * Disable and enable (inactive) the CPU-counter sets according 730 * to the per-cpu control state. 731 */ 732 static void cpumf_pmu_disable(struct pmu *pmu) 733 { 734 struct cpu_cf_events *cpuhw = this_cpu_cfhw(); 735 u64 inactive; 736 int err; 737 738 if (!cpuhw || !(cpuhw->flags & PMU_F_ENABLED)) 739 return; 740 741 inactive = cpuhw->state & ~((1 << CPUMF_LCCTL_ENABLE_SHIFT) - 1); 742 inactive |= cpuhw->dev_state; 743 err = lcctl(inactive); 744 if (err) 745 pr_err("Disabling the performance measuring unit failed with rc=%x\n", err); 746 else 747 cpuhw->flags &= ~PMU_F_ENABLED; 748 } 749 750 /* Release the PMU if event is the last perf event */ 751 static void hw_perf_event_destroy(struct perf_event *event) 752 { 753 cpum_cf_free(event->cpu); 754 } 755 756 /* CPUMF <-> perf event mappings for kernel+userspace (basic set) */ 757 static const int cpumf_generic_events_basic[] = { 758 [PERF_COUNT_HW_CPU_CYCLES] = 0, 759 [PERF_COUNT_HW_INSTRUCTIONS] = 1, 760 [PERF_COUNT_HW_CACHE_REFERENCES] = -1, 761 [PERF_COUNT_HW_CACHE_MISSES] = -1, 762 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = -1, 763 [PERF_COUNT_HW_BRANCH_MISSES] = -1, 764 [PERF_COUNT_HW_BUS_CYCLES] = -1, 765 }; 766 /* CPUMF <-> perf event mappings for userspace (problem-state set) */ 767 static const int cpumf_generic_events_user[] = { 768 [PERF_COUNT_HW_CPU_CYCLES] = 32, 769 [PERF_COUNT_HW_INSTRUCTIONS] = 33, 770 [PERF_COUNT_HW_CACHE_REFERENCES] = -1, 771 [PERF_COUNT_HW_CACHE_MISSES] = -1, 772 [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = -1, 773 [PERF_COUNT_HW_BRANCH_MISSES] = -1, 774 [PERF_COUNT_HW_BUS_CYCLES] = -1, 775 }; 776 777 static int is_userspace_event(u64 ev) 778 { 779 return cpumf_generic_events_user[PERF_COUNT_HW_CPU_CYCLES] == ev || 780 cpumf_generic_events_user[PERF_COUNT_HW_INSTRUCTIONS] == ev; 781 } 782 783 static int __hw_perf_event_init(struct perf_event *event, unsigned int type) 784 { 785 struct perf_event_attr *attr = &event->attr; 786 struct hw_perf_event *hwc = &event->hw; 787 enum cpumf_ctr_set set; 788 u64 ev; 789 790 switch (type) { 791 case PERF_TYPE_RAW: 792 /* Raw events are used to access counters directly, 793 * hence do not permit excludes */ 794 if (attr->exclude_kernel || attr->exclude_user || 795 attr->exclude_hv) 796 return -EOPNOTSUPP; 797 ev = attr->config; 798 break; 799 800 case PERF_TYPE_HARDWARE: 801 ev = attr->config; 802 if (!attr->exclude_user && attr->exclude_kernel) { 803 /* 804 * Count user space (problem-state) only 805 * Handle events 32 and 33 as 0:u and 1:u 806 */ 807 if (!is_userspace_event(ev)) { 808 if (ev >= ARRAY_SIZE(cpumf_generic_events_user)) 809 return -EOPNOTSUPP; 810 ev = array_index_nospec(ev, ARRAY_SIZE(cpumf_generic_events_user)); 811 ev = cpumf_generic_events_user[ev]; 812 } 813 } else if (!attr->exclude_kernel && attr->exclude_user) { 814 /* No support for kernel space counters only */ 815 return -EOPNOTSUPP; 816 } else { 817 /* Count user and kernel space, incl. events 32 + 33 */ 818 if (!is_userspace_event(ev)) { 819 if (ev >= ARRAY_SIZE(cpumf_generic_events_basic)) 820 return -EOPNOTSUPP; 821 ev = array_index_nospec(ev, ARRAY_SIZE(cpumf_generic_events_basic)); 822 ev = cpumf_generic_events_basic[ev]; 823 } 824 } 825 break; 826 827 default: 828 return -ENOENT; 829 } 830 831 if (ev == -1) 832 return -ENOENT; 833 834 if (ev > PERF_CPUM_CF_MAX_CTR) 835 return -ENOENT; 836 837 /* Obtain the counter set to which the specified counter belongs */ 838 set = get_counter_set(ev); 839 switch (set) { 840 case CPUMF_CTR_SET_BASIC: 841 case CPUMF_CTR_SET_USER: 842 case CPUMF_CTR_SET_CRYPTO: 843 case CPUMF_CTR_SET_EXT: 844 case CPUMF_CTR_SET_MT_DIAG: 845 /* 846 * Use the hardware perf event structure to store the 847 * counter number in the 'config' member and the counter 848 * set number in the 'config_base' as bit mask. 849 * It is later used to enable/disable the counter(s). 850 */ 851 hwc->config = ev; 852 hwc->config_base = cpumf_ctr_ctl[set]; 853 break; 854 case CPUMF_CTR_SET_MAX: 855 /* The counter could not be associated to a counter set */ 856 return -EINVAL; 857 } 858 859 /* Initialize for using the CPU-measurement counter facility */ 860 if (cpum_cf_alloc(event->cpu)) 861 return -ENOMEM; 862 event->destroy = hw_perf_event_destroy; 863 864 /* 865 * Finally, validate version and authorization of the counter set. 866 * If the particular CPU counter set is not authorized, 867 * return with -ENOENT in order to fall back to other 868 * PMUs that might suffice the event request. 869 */ 870 if (!(hwc->config_base & cpumf_ctr_info.auth_ctl)) 871 return -ENOENT; 872 return validate_ctr_version(hwc->config, set); 873 } 874 875 /* Events CPU_CYCLES and INSTRUCTIONS can be submitted with two different 876 * attribute::type values: 877 * - PERF_TYPE_HARDWARE: 878 * - pmu->type: 879 * Handle both type of invocations identical. They address the same hardware. 880 * The result is different when event modifiers exclude_kernel and/or 881 * exclude_user are also set. 882 */ 883 static int cpumf_pmu_event_type(struct perf_event *event) 884 { 885 u64 ev = event->attr.config; 886 887 if (cpumf_generic_events_basic[PERF_COUNT_HW_CPU_CYCLES] == ev || 888 cpumf_generic_events_basic[PERF_COUNT_HW_INSTRUCTIONS] == ev || 889 cpumf_generic_events_user[PERF_COUNT_HW_CPU_CYCLES] == ev || 890 cpumf_generic_events_user[PERF_COUNT_HW_INSTRUCTIONS] == ev) 891 return PERF_TYPE_HARDWARE; 892 return PERF_TYPE_RAW; 893 } 894 895 static int cpumf_pmu_event_init(struct perf_event *event) 896 { 897 unsigned int type = event->attr.type; 898 int err = -ENOENT; 899 900 if (is_sampling_event(event)) /* No sampling support */ 901 return err; 902 if (type == PERF_TYPE_HARDWARE || type == PERF_TYPE_RAW) 903 err = __hw_perf_event_init(event, type); 904 else if (event->pmu->type == type) 905 /* Registered as unknown PMU */ 906 err = __hw_perf_event_init(event, cpumf_pmu_event_type(event)); 907 908 return err; 909 } 910 911 static int hw_perf_event_reset(struct perf_event *event) 912 { 913 u64 prev, new; 914 int err; 915 916 prev = local64_read(&event->hw.prev_count); 917 do { 918 err = ecctr(event->hw.config, &new); 919 if (err) { 920 if (err != 3) 921 break; 922 /* The counter is not (yet) available. This 923 * might happen if the counter set to which 924 * this counter belongs is in the disabled 925 * state. 926 */ 927 new = 0; 928 } 929 } while (!local64_try_cmpxchg(&event->hw.prev_count, &prev, new)); 930 931 return err; 932 } 933 934 static void hw_perf_event_update(struct perf_event *event) 935 { 936 u64 prev, new, delta; 937 int err; 938 939 prev = local64_read(&event->hw.prev_count); 940 do { 941 err = ecctr(event->hw.config, &new); 942 if (err) 943 return; 944 } while (!local64_try_cmpxchg(&event->hw.prev_count, &prev, new)); 945 946 delta = (prev <= new) ? new - prev 947 : (-1ULL - prev) + new + 1; /* overflow */ 948 local64_add(delta, &event->count); 949 } 950 951 static void cpumf_pmu_read(struct perf_event *event) 952 { 953 if (event->hw.state & PERF_HES_STOPPED) 954 return; 955 956 hw_perf_event_update(event); 957 } 958 959 static void cpumf_pmu_start(struct perf_event *event, int flags) 960 { 961 struct cpu_cf_events *cpuhw = this_cpu_cfhw(); 962 struct hw_perf_event *hwc = &event->hw; 963 int i; 964 965 if (!(hwc->state & PERF_HES_STOPPED)) 966 return; 967 968 hwc->state = 0; 969 970 /* (Re-)enable and activate the counter set */ 971 ctr_set_enable(&cpuhw->state, hwc->config_base); 972 ctr_set_start(&cpuhw->state, hwc->config_base); 973 974 /* The counter set to which this counter belongs can be already active. 975 * Because all counters in a set are active, the event->hw.prev_count 976 * needs to be synchronized. At this point, the counter set can be in 977 * the inactive or disabled state. 978 */ 979 if (hwc->config == PERF_EVENT_CPUM_CF_DIAG) { 980 cpuhw->usedss = cfdiag_getctr(cpuhw->start, 981 sizeof(cpuhw->start), 982 hwc->config_base, true); 983 } else { 984 hw_perf_event_reset(event); 985 } 986 987 /* Increment refcount for counter sets */ 988 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) 989 if ((hwc->config_base & cpumf_ctr_ctl[i])) 990 atomic_inc(&cpuhw->ctr_set[i]); 991 } 992 993 /* Create perf event sample with the counter sets as raw data. The sample 994 * is then pushed to the event subsystem and the function checks for 995 * possible event overflows. If an event overflow occurs, the PMU is 996 * stopped. 997 * 998 * Return non-zero if an event overflow occurred. 999 */ 1000 static int cfdiag_push_sample(struct perf_event *event, 1001 struct cpu_cf_events *cpuhw) 1002 { 1003 struct perf_sample_data data; 1004 struct perf_raw_record raw; 1005 struct pt_regs regs; 1006 int overflow; 1007 1008 /* Setup perf sample */ 1009 perf_sample_data_init(&data, 0, event->hw.last_period); 1010 memset(®s, 0, sizeof(regs)); 1011 memset(&raw, 0, sizeof(raw)); 1012 1013 if (event->attr.sample_type & PERF_SAMPLE_CPU) 1014 data.cpu_entry.cpu = event->cpu; 1015 if (event->attr.sample_type & PERF_SAMPLE_RAW) { 1016 raw.frag.size = cpuhw->usedss; 1017 raw.frag.data = cpuhw->stop; 1018 perf_sample_save_raw_data(&data, event, &raw); 1019 } 1020 1021 overflow = perf_event_overflow(event, &data, ®s); 1022 1023 perf_event_update_userpage(event); 1024 return overflow; 1025 } 1026 1027 static void cpumf_pmu_stop(struct perf_event *event, int flags) 1028 { 1029 struct cpu_cf_events *cpuhw = this_cpu_cfhw(); 1030 struct hw_perf_event *hwc = &event->hw; 1031 int i; 1032 1033 if (!(hwc->state & PERF_HES_STOPPED)) { 1034 /* Decrement reference count for this counter set and if this 1035 * is the last used counter in the set, clear activation 1036 * control and set the counter set state to inactive. 1037 */ 1038 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) { 1039 if (!(hwc->config_base & cpumf_ctr_ctl[i])) 1040 continue; 1041 if (!atomic_dec_return(&cpuhw->ctr_set[i])) 1042 ctr_set_stop(&cpuhw->state, cpumf_ctr_ctl[i]); 1043 } 1044 hwc->state |= PERF_HES_STOPPED; 1045 } 1046 1047 if ((flags & PERF_EF_UPDATE) && !(hwc->state & PERF_HES_UPTODATE)) { 1048 if (hwc->config == PERF_EVENT_CPUM_CF_DIAG) { 1049 local64_inc(&event->count); 1050 cpuhw->usedss = cfdiag_getctr(cpuhw->stop, 1051 sizeof(cpuhw->stop), 1052 event->hw.config_base, 1053 false); 1054 if (cfdiag_diffctr(cpuhw, event->hw.config_base)) 1055 cfdiag_push_sample(event, cpuhw); 1056 } else { 1057 hw_perf_event_update(event); 1058 } 1059 hwc->state |= PERF_HES_UPTODATE; 1060 } 1061 } 1062 1063 static int cpumf_pmu_add(struct perf_event *event, int flags) 1064 { 1065 struct cpu_cf_events *cpuhw = this_cpu_cfhw(); 1066 1067 ctr_set_enable(&cpuhw->state, event->hw.config_base); 1068 event->hw.state = PERF_HES_UPTODATE | PERF_HES_STOPPED; 1069 1070 if (flags & PERF_EF_START) 1071 cpumf_pmu_start(event, PERF_EF_RELOAD); 1072 1073 return 0; 1074 } 1075 1076 static void cpumf_pmu_del(struct perf_event *event, int flags) 1077 { 1078 struct cpu_cf_events *cpuhw = this_cpu_cfhw(); 1079 int i; 1080 1081 cpumf_pmu_stop(event, PERF_EF_UPDATE); 1082 1083 /* Check if any counter in the counter set is still used. If not used, 1084 * change the counter set to the disabled state. This also clears the 1085 * content of all counters in the set. 1086 * 1087 * When a new perf event has been added but not yet started, this can 1088 * clear enable control and resets all counters in a set. Therefore, 1089 * cpumf_pmu_start() always has to re-enable a counter set. 1090 */ 1091 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) 1092 if (!atomic_read(&cpuhw->ctr_set[i])) 1093 ctr_set_disable(&cpuhw->state, cpumf_ctr_ctl[i]); 1094 } 1095 1096 /* Performance monitoring unit for s390x */ 1097 static struct pmu cpumf_pmu = { 1098 .task_ctx_nr = perf_sw_context, 1099 .capabilities = PERF_PMU_CAP_NO_INTERRUPT, 1100 .pmu_enable = cpumf_pmu_enable, 1101 .pmu_disable = cpumf_pmu_disable, 1102 .event_init = cpumf_pmu_event_init, 1103 .add = cpumf_pmu_add, 1104 .del = cpumf_pmu_del, 1105 .start = cpumf_pmu_start, 1106 .stop = cpumf_pmu_stop, 1107 .read = cpumf_pmu_read, 1108 }; 1109 1110 static struct cfset_session { /* CPUs and counter set bit mask */ 1111 struct list_head head; /* Head of list of active processes */ 1112 } cfset_session = { 1113 .head = LIST_HEAD_INIT(cfset_session.head) 1114 }; 1115 1116 static refcount_t cfset_opencnt = REFCOUNT_INIT(0); /* Access count */ 1117 /* 1118 * Synchronize access to device /dev/hwc. This mutex protects against 1119 * concurrent access to functions cfset_open() and cfset_release(). 1120 * Same for CPU hotplug add and remove events triggering 1121 * cpum_cf_online_cpu() and cpum_cf_offline_cpu(). 1122 * It also serializes concurrent device ioctl access from multiple 1123 * processes accessing /dev/hwc. 1124 * 1125 * The mutex protects concurrent access to the /dev/hwctr session management 1126 * struct cfset_session and reference counting variable cfset_opencnt. 1127 */ 1128 static DEFINE_MUTEX(cfset_ctrset_mutex); 1129 1130 /* 1131 * CPU hotplug handling: 1132 * 1133 * cpum_cf_prepare_cpu() and cpum_cf_dead_cpu() run while the new or dying 1134 * CPU is offline. They create and remove the per CPU counter data for all 1135 * users tracking every CPU (cpu == -1), that is perf_event_open() events 1136 * with task context and /dev/hwctr device sessions. Each such user holds 1137 * one reference to the per CPU counter data of each CPU. Therefore install 1138 * and remove one reference per user, tracked in cpu_cf_root::tskctx. This 1139 * guarantees the per CPU counter data exists before the new CPU executes 1140 * its first task and is removed only after the dying CPU is gone. 1141 * 1142 * cpum_cf_online_cpu() and cpum_cf_offline_cpu() run while the new or 1143 * dying CPU is online. They handle only the counter set state of open 1144 * /dev/hwctr device sessions on that CPU. For perf_event_open() events 1145 * nothing is done: 1146 * - CPU add: Nothing is done since a file descriptor can not be created 1147 * and returned to the user. 1148 * - CPU delete: Handled by common code via pmu_disable(), pmu_stop() and 1149 * pmu_delete(). During task exit processing of grouped perf events 1150 * triggered by CPU hotplug processing, pmu_disable() is called as part 1151 * of perf context removal process. The event itself is removed when the 1152 * event file descriptor is closed. 1153 */ 1154 static int cpum_cf_prepare_cpu(unsigned int cpu) 1155 { 1156 int rc = 0; 1157 1158 mutex_lock(&pmc_reserve_mutex); 1159 if (cpu_cf_root.tskctx) 1160 rc = cpum_cf_alloc_cpu(cpu, cpu_cf_root.tskctx); 1161 mutex_unlock(&pmc_reserve_mutex); 1162 return rc; 1163 } 1164 1165 static int cpum_cf_dead_cpu(unsigned int cpu) 1166 { 1167 mutex_lock(&pmc_reserve_mutex); 1168 if (cpu_cf_root.tskctx) 1169 cpum_cf_free_cpu(cpu, cpu_cf_root.tskctx); 1170 mutex_unlock(&pmc_reserve_mutex); 1171 return 0; 1172 } 1173 1174 static int cfset_online_cpu(unsigned int cpu); 1175 1176 static int cpum_cf_online_cpu(unsigned int cpu) 1177 { 1178 mutex_lock(&cfset_ctrset_mutex); 1179 if (refcount_read(&cfset_opencnt)) 1180 cfset_online_cpu(cpu); 1181 mutex_unlock(&cfset_ctrset_mutex); 1182 return 0; 1183 } 1184 1185 static int cfset_offline_cpu(unsigned int cpu); 1186 1187 static int cpum_cf_offline_cpu(unsigned int cpu) 1188 { 1189 mutex_lock(&cfset_ctrset_mutex); 1190 if (refcount_read(&cfset_opencnt)) 1191 cfset_offline_cpu(cpu); 1192 mutex_unlock(&cfset_ctrset_mutex); 1193 return 0; 1194 } 1195 1196 /* Return true if store counter set multiple instruction is available */ 1197 static inline int stccm_avail(void) 1198 { 1199 return test_facility(142); 1200 } 1201 1202 /* CPU-measurement alerts for the counter facility */ 1203 static void cpumf_measurement_alert(struct ext_code ext_code, 1204 unsigned int alert, unsigned long unused) 1205 { 1206 struct cpu_cf_events *cpuhw; 1207 1208 if (!(alert & CPU_MF_INT_CF_MASK)) 1209 return; 1210 1211 inc_irq_stat(IRQEXT_CMC); 1212 1213 /* 1214 * Measurement alerts are shared and might happen when the PMU 1215 * is not reserved. Ignore these alerts in this case. 1216 */ 1217 cpuhw = this_cpu_cfhw(); 1218 if (!cpuhw) 1219 return; 1220 1221 /* counter authorization change alert */ 1222 if (alert & CPU_MF_INT_CF_CACA) 1223 qctri(&cpumf_ctr_info); 1224 1225 /* loss of counter data alert */ 1226 if (alert & CPU_MF_INT_CF_LCDA) 1227 pr_err("CPU[%i] Counter data was lost\n", smp_processor_id()); 1228 1229 /* loss of MT counter data alert */ 1230 if (alert & CPU_MF_INT_CF_MTDA) 1231 pr_warn("CPU[%i] MT counter data was lost\n", 1232 smp_processor_id()); 1233 } 1234 1235 static int cfset_init(void); 1236 static int __init cpumf_pmu_init(void) 1237 { 1238 int state, rc; 1239 1240 /* Extract counter measurement facility information */ 1241 if (!cpum_cf_avail() || qctri(&cpumf_ctr_info)) 1242 return -ENODEV; 1243 1244 /* Determine and store counter set sizes for later reference */ 1245 for (rc = CPUMF_CTR_SET_BASIC; rc < CPUMF_CTR_SET_MAX; ++rc) 1246 cpum_cf_make_setsize(rc); 1247 1248 /* 1249 * Clear bit 15 of cr0 to unauthorize problem-state to 1250 * extract measurement counters 1251 */ 1252 system_ctl_clear_bit(0, CR0_CPUMF_EXTRACTION_AUTH_BIT); 1253 1254 /* register handler for measurement-alert interruptions */ 1255 rc = register_external_irq(EXT_IRQ_MEASURE_ALERT, 1256 cpumf_measurement_alert); 1257 if (rc) { 1258 pr_err("Registering for CPU-measurement alerts failed with rc=%i\n", rc); 1259 return rc; 1260 } 1261 1262 /* Setup s390dbf facility */ 1263 cf_dbg = debug_register("cpum_cf", 2, 1, 128); 1264 if (!cf_dbg) { 1265 pr_err("Registration of s390dbf(cpum_cf) failed\n"); 1266 rc = -ENOMEM; 1267 goto out1; 1268 } 1269 debug_register_view(cf_dbg, &debug_sprintf_view); 1270 1271 cpumf_pmu.attr_groups = cpumf_cf_event_group(); 1272 rc = perf_pmu_register(&cpumf_pmu, "cpum_cf", -1); 1273 if (rc) { 1274 pr_err("Registering the cpum_cf PMU failed with rc=%i\n", rc); 1275 goto out2; 1276 } else if (stccm_avail()) { /* Setup counter set device */ 1277 cfset_init(); 1278 } 1279 1280 rc = cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, 1281 "perf/s390/cf:prepare", 1282 cpum_cf_prepare_cpu, cpum_cf_dead_cpu); 1283 if (rc < 0) 1284 goto out3; 1285 state = rc; 1286 1287 rc = cpuhp_setup_state(CPUHP_AP_PERF_S390_CF_ONLINE, 1288 "perf/s390/cf:online", 1289 cpum_cf_online_cpu, cpum_cf_offline_cpu); 1290 if (rc < 0) 1291 goto out4; 1292 return 0; 1293 1294 out4: 1295 cpuhp_remove_state(state); 1296 out3: 1297 perf_pmu_unregister(&cpumf_pmu); 1298 out2: 1299 debug_unregister_view(cf_dbg, &debug_sprintf_view); 1300 debug_unregister(cf_dbg); 1301 out1: 1302 unregister_external_irq(EXT_IRQ_MEASURE_ALERT, cpumf_measurement_alert); 1303 return rc; 1304 } 1305 1306 /* Support for the CPU Measurement Facility counter set extraction using 1307 * device /dev/hwctr. This allows user space programs to extract complete 1308 * counter set via normal file operations. 1309 */ 1310 1311 struct cfset_call_on_cpu_parm { /* Parm struct for smp_call_on_cpu */ 1312 unsigned int sets; /* Counter set bit mask */ 1313 atomic_t cpus_ack; /* # CPUs successfully executed func */ 1314 }; 1315 1316 struct cfset_request { /* CPUs and counter set bit mask */ 1317 unsigned long ctrset; /* Bit mask of counter set to read */ 1318 cpumask_t mask; /* CPU mask to read from */ 1319 struct list_head node; /* Chain to cfset_session.head */ 1320 }; 1321 1322 static void cfset_session_init(void) 1323 { 1324 INIT_LIST_HEAD(&cfset_session.head); 1325 } 1326 1327 /* Remove current request from global bookkeeping. Maintain a counter set bit 1328 * mask on a per CPU basis. 1329 * Done in process context under mutex protection. 1330 */ 1331 static void cfset_session_del(struct cfset_request *p) 1332 { 1333 list_del(&p->node); 1334 } 1335 1336 /* Add current request to global bookkeeping. Maintain a counter set bit mask 1337 * on a per CPU basis. 1338 * Done in process context under mutex protection. 1339 */ 1340 static void cfset_session_add(struct cfset_request *p) 1341 { 1342 list_add(&p->node, &cfset_session.head); 1343 } 1344 1345 /* The /dev/hwctr device access uses PMU_F_IN_USE to mark the device access 1346 * path is currently used. 1347 * The cpu_cf_events::dev_state is used to denote counter sets in use by this 1348 * interface. It is always or'ed in. If this interface is not active, its 1349 * value is zero and no additional counter sets will be included. 1350 * 1351 * The cpu_cf_events::state is used by the perf_event_open SVC and remains 1352 * unchanged. 1353 * 1354 * perf_pmu_enable() and perf_pmu_enable() and its call backs 1355 * cpumf_pmu_enable() and cpumf_pmu_disable() are called by the 1356 * performance measurement subsystem to enable per process 1357 * CPU Measurement counter facility. 1358 * The XXX_enable() and XXX_disable functions are used to turn off 1359 * x86 performance monitoring interrupt (PMI) during scheduling. 1360 * s390 uses these calls to temporarily stop and resume the active CPU 1361 * counters sets during scheduling. 1362 * 1363 * We do allow concurrent access of perf_event_open() SVC and /dev/hwctr 1364 * device access. The perf_event_open() SVC interface makes a lot of effort 1365 * to only run the counters while the calling process is actively scheduled 1366 * to run. 1367 * When /dev/hwctr interface is also used at the same time, the counter sets 1368 * will keep running, even when the process is scheduled off a CPU. 1369 * However this is not a problem and does not lead to wrong counter values 1370 * for the perf_event_open() SVC. The current counter value will be recorded 1371 * during schedule-in. At schedule-out time the current counter value is 1372 * extracted again and the delta is calculated and added to the event. 1373 */ 1374 /* Stop all counter sets via ioctl interface */ 1375 static void cfset_ioctl_off(void *parm) 1376 { 1377 struct cpu_cf_events *cpuhw = this_cpu_cfhw(); 1378 struct cfset_call_on_cpu_parm *p = parm; 1379 int rc; 1380 1381 /* Check if any counter set used by /dev/hwctr */ 1382 for (rc = CPUMF_CTR_SET_BASIC; rc < CPUMF_CTR_SET_MAX; ++rc) 1383 if ((p->sets & cpumf_ctr_ctl[rc])) { 1384 if (!atomic_dec_return(&cpuhw->ctr_set[rc])) { 1385 ctr_set_disable(&cpuhw->dev_state, 1386 cpumf_ctr_ctl[rc]); 1387 ctr_set_stop(&cpuhw->dev_state, 1388 cpumf_ctr_ctl[rc]); 1389 } 1390 } 1391 /* Keep perf_event_open counter sets */ 1392 rc = lcctl(cpuhw->dev_state | cpuhw->state); 1393 if (rc) 1394 pr_err("Counter set stop %#llx of /dev/%s failed rc=%i\n", 1395 cpuhw->state, S390_HWCTR_DEVICE, rc); 1396 if (!cpuhw->dev_state) 1397 cpuhw->flags &= ~PMU_F_IN_USE; 1398 } 1399 1400 /* Start counter sets on particular CPU */ 1401 static void cfset_ioctl_on(void *parm) 1402 { 1403 struct cpu_cf_events *cpuhw = this_cpu_cfhw(); 1404 struct cfset_call_on_cpu_parm *p = parm; 1405 int rc; 1406 1407 cpuhw->flags |= PMU_F_IN_USE; 1408 ctr_set_enable(&cpuhw->dev_state, p->sets); 1409 ctr_set_start(&cpuhw->dev_state, p->sets); 1410 for (rc = CPUMF_CTR_SET_BASIC; rc < CPUMF_CTR_SET_MAX; ++rc) 1411 if ((p->sets & cpumf_ctr_ctl[rc])) 1412 atomic_inc(&cpuhw->ctr_set[rc]); 1413 rc = lcctl(cpuhw->dev_state | cpuhw->state); /* Start counter sets */ 1414 if (!rc) 1415 atomic_inc(&p->cpus_ack); 1416 else 1417 pr_err("Counter set start %#llx of /dev/%s failed rc=%i\n", 1418 cpuhw->dev_state | cpuhw->state, S390_HWCTR_DEVICE, rc); 1419 } 1420 1421 static void cfset_release_cpu(void *p) 1422 { 1423 struct cpu_cf_events *cpuhw = this_cpu_cfhw(); 1424 int rc; 1425 1426 cpuhw->dev_state = 0; 1427 rc = lcctl(cpuhw->state); /* Keep perf_event_open counter sets */ 1428 if (rc) 1429 pr_err("Counter set release %#llx of /dev/%s failed rc=%i\n", 1430 cpuhw->state, S390_HWCTR_DEVICE, rc); 1431 } 1432 1433 /* This modifies the process CPU mask to adopt it to the currently online 1434 * CPUs. Offline CPUs can not be addresses. This call terminates the access 1435 * and is usually followed by close() or a new iotcl(..., START, ...) which 1436 * creates a new request structure. 1437 */ 1438 static void cfset_all_stop(struct cfset_request *req) 1439 { 1440 struct cfset_call_on_cpu_parm p = { 1441 .sets = req->ctrset, 1442 }; 1443 1444 cpumask_and(&req->mask, &req->mask, cpu_online_mask); 1445 on_each_cpu_mask(&req->mask, cfset_ioctl_off, &p, 1); 1446 } 1447 1448 /* Release function is also called when application gets terminated without 1449 * doing a proper ioctl(..., S390_HWCTR_STOP, ...) command. 1450 */ 1451 static int cfset_release(struct inode *inode, struct file *file) 1452 { 1453 cpus_read_lock(); 1454 mutex_lock(&cfset_ctrset_mutex); 1455 /* Open followed by close/exit has no private_data */ 1456 if (file->private_data) { 1457 cfset_all_stop(file->private_data); 1458 cfset_session_del(file->private_data); 1459 kfree(file->private_data); 1460 file->private_data = NULL; 1461 } 1462 if (refcount_dec_and_test(&cfset_opencnt)) { /* Last close */ 1463 on_each_cpu(cfset_release_cpu, NULL, 1); 1464 cpum_cf_free_cpuslocked(-1); 1465 } 1466 mutex_unlock(&cfset_ctrset_mutex); 1467 cpus_read_unlock(); 1468 return 0; 1469 } 1470 1471 /* 1472 * Open via /dev/hwctr device. Allocate all per CPU resources on the first 1473 * open of the device. The last close releases all per CPU resources. 1474 * Parallel perf_event_open system calls also use per CPU resources. 1475 * These invocations are handled via reference counting on the per CPU data 1476 * structures. 1477 */ 1478 static int cfset_open(struct inode *inode, struct file *file) 1479 { 1480 int rc = 0; 1481 1482 if (!perfmon_capable()) 1483 return -EPERM; 1484 file->private_data = NULL; 1485 1486 cpus_read_lock(); 1487 mutex_lock(&cfset_ctrset_mutex); 1488 if (!refcount_inc_not_zero(&cfset_opencnt)) { /* First open */ 1489 rc = cpum_cf_alloc_cpuslocked(-1); 1490 if (!rc) { 1491 cfset_session_init(); 1492 refcount_set(&cfset_opencnt, 1); 1493 } 1494 } 1495 mutex_unlock(&cfset_ctrset_mutex); 1496 cpus_read_unlock(); 1497 1498 /* nonseekable_open() never fails */ 1499 return rc ?: nonseekable_open(inode, file); 1500 } 1501 1502 static int cfset_all_start(struct cfset_request *req) 1503 { 1504 struct cfset_call_on_cpu_parm p = { 1505 .sets = req->ctrset, 1506 .cpus_ack = ATOMIC_INIT(0), 1507 }; 1508 cpumask_var_t mask; 1509 int rc = 0; 1510 1511 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) 1512 return -ENOMEM; 1513 cpumask_and(mask, &req->mask, cpu_online_mask); 1514 on_each_cpu_mask(mask, cfset_ioctl_on, &p, 1); 1515 if (atomic_read(&p.cpus_ack) != cpumask_weight(mask)) { 1516 on_each_cpu_mask(mask, cfset_ioctl_off, &p, 1); 1517 rc = -EIO; 1518 } 1519 free_cpumask_var(mask); 1520 return rc; 1521 } 1522 1523 /* Return the maximum required space for all possible CPUs in case one 1524 * CPU will be onlined during the START, READ, STOP cycles. 1525 * To find out the size of the counter sets, any one CPU will do. They 1526 * all have the same counter sets. 1527 */ 1528 static size_t cfset_needspace(unsigned int sets) 1529 { 1530 size_t bytes = 0; 1531 int i; 1532 1533 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) { 1534 if (!(sets & cpumf_ctr_ctl[i])) 1535 continue; 1536 bytes += cpum_cf_read_setsize(i) * sizeof(u64) + 1537 sizeof(((struct s390_ctrset_setdata *)0)->set) + 1538 sizeof(((struct s390_ctrset_setdata *)0)->no_cnts); 1539 } 1540 bytes = sizeof(((struct s390_ctrset_read *)0)->no_cpus) + nr_cpu_ids * 1541 (bytes + sizeof(((struct s390_ctrset_cpudata *)0)->cpu_nr) + 1542 sizeof(((struct s390_ctrset_cpudata *)0)->no_sets)); 1543 return bytes; 1544 } 1545 1546 static int cfset_all_copy(unsigned long arg, cpumask_t *mask) 1547 { 1548 struct s390_ctrset_read __user *ctrset_read; 1549 unsigned int cpu, cpus, rc = 0; 1550 void __user *uptr; 1551 1552 ctrset_read = (struct s390_ctrset_read __user *)arg; 1553 uptr = ctrset_read->data; 1554 for_each_cpu(cpu, mask) { 1555 struct cpu_cf_events *cpuhw = get_cpu_cfhw(cpu); 1556 struct s390_ctrset_cpudata __user *ctrset_cpudata; 1557 1558 ctrset_cpudata = uptr; 1559 rc = put_user(cpu, &ctrset_cpudata->cpu_nr); 1560 rc |= put_user(cpuhw->sets, &ctrset_cpudata->no_sets); 1561 rc |= copy_to_user(ctrset_cpudata->data, cpuhw->data, 1562 cpuhw->used); 1563 if (rc) { 1564 rc = -EFAULT; 1565 goto out; 1566 } 1567 uptr += sizeof(struct s390_ctrset_cpudata) + cpuhw->used; 1568 } 1569 cpus = cpumask_weight(mask); 1570 if (put_user(cpus, &ctrset_read->no_cpus)) 1571 rc = -EFAULT; 1572 out: 1573 return rc; 1574 } 1575 1576 static size_t cfset_cpuset_read(struct s390_ctrset_setdata *p, int ctrset, 1577 int ctrset_size, size_t room) 1578 { 1579 size_t need = 0; 1580 int rc = -1; 1581 1582 need = sizeof(*p) + sizeof(u64) * ctrset_size; 1583 if (need <= room) { 1584 p->set = cpumf_ctr_ctl[ctrset]; 1585 p->no_cnts = ctrset_size; 1586 rc = ctr_stcctm(ctrset, ctrset_size, (u64 *)p->cv); 1587 if (rc == 3) /* Nothing stored */ 1588 need = 0; 1589 } 1590 return need; 1591 } 1592 1593 /* Read all counter sets. */ 1594 static void cfset_cpu_read(void *parm) 1595 { 1596 struct cpu_cf_events *cpuhw = this_cpu_cfhw(); 1597 struct cfset_call_on_cpu_parm *p = parm; 1598 int set, set_size; 1599 size_t space; 1600 1601 /* No data saved yet */ 1602 cpuhw->used = 0; 1603 cpuhw->sets = 0; 1604 memset(cpuhw->data, 0, sizeof(cpuhw->data)); 1605 1606 /* Scan the counter sets */ 1607 for (set = CPUMF_CTR_SET_BASIC; set < CPUMF_CTR_SET_MAX; ++set) { 1608 struct s390_ctrset_setdata *sp = (void *)cpuhw->data + 1609 cpuhw->used; 1610 1611 if (!(p->sets & cpumf_ctr_ctl[set])) 1612 continue; /* Counter set not in list */ 1613 set_size = cpum_cf_read_setsize(set); 1614 space = sizeof(cpuhw->data) - cpuhw->used; 1615 space = cfset_cpuset_read(sp, set, set_size, space); 1616 if (space) { 1617 cpuhw->used += space; 1618 cpuhw->sets += 1; 1619 } 1620 } 1621 } 1622 1623 static int cfset_all_read(unsigned long arg, struct cfset_request *req) 1624 { 1625 struct cfset_call_on_cpu_parm p; 1626 cpumask_var_t mask; 1627 int rc; 1628 1629 if (!alloc_cpumask_var(&mask, GFP_KERNEL)) 1630 return -ENOMEM; 1631 1632 p.sets = req->ctrset; 1633 cpumask_and(mask, &req->mask, cpu_online_mask); 1634 on_each_cpu_mask(mask, cfset_cpu_read, &p, 1); 1635 rc = cfset_all_copy(arg, mask); 1636 free_cpumask_var(mask); 1637 return rc; 1638 } 1639 1640 static long cfset_ioctl_read(unsigned long arg, struct cfset_request *req) 1641 { 1642 int ret = -ENODATA; 1643 1644 if (req && req->ctrset) 1645 ret = cfset_all_read(arg, req); 1646 return ret; 1647 } 1648 1649 static long cfset_ioctl_stop(struct file *file) 1650 { 1651 struct cfset_request *req = file->private_data; 1652 int ret = -ENXIO; 1653 1654 if (req) { 1655 cfset_all_stop(req); 1656 cfset_session_del(req); 1657 kfree(req); 1658 file->private_data = NULL; 1659 ret = 0; 1660 } 1661 return ret; 1662 } 1663 1664 static long cfset_ioctl_start(unsigned long arg, struct file *file) 1665 { 1666 struct s390_ctrset_start __user *ustart; 1667 struct s390_ctrset_start start; 1668 struct cfset_request *preq; 1669 void __user *umask; 1670 unsigned int len; 1671 int ret = 0; 1672 size_t need; 1673 1674 if (file->private_data) 1675 return -EBUSY; 1676 ustart = (struct s390_ctrset_start __user *)arg; 1677 if (copy_from_user(&start, ustart, sizeof(start))) 1678 return -EFAULT; 1679 if (start.version != S390_HWCTR_START_VERSION) 1680 return -EINVAL; 1681 if (start.counter_sets & ~(cpumf_ctr_ctl[CPUMF_CTR_SET_BASIC] | 1682 cpumf_ctr_ctl[CPUMF_CTR_SET_USER] | 1683 cpumf_ctr_ctl[CPUMF_CTR_SET_CRYPTO] | 1684 cpumf_ctr_ctl[CPUMF_CTR_SET_EXT] | 1685 cpumf_ctr_ctl[CPUMF_CTR_SET_MT_DIAG])) 1686 return -EINVAL; /* Invalid counter set */ 1687 if (!start.counter_sets) 1688 return -EINVAL; /* No counter set at all? */ 1689 1690 preq = kzalloc_obj(*preq); 1691 if (!preq) 1692 return -ENOMEM; 1693 cpumask_clear(&preq->mask); 1694 len = min_t(u64, start.cpumask_len, cpumask_size()); 1695 umask = (void __user *)start.cpumask; 1696 if (copy_from_user(&preq->mask, umask, len)) { 1697 kfree(preq); 1698 return -EFAULT; 1699 } 1700 if (cpumask_empty(&preq->mask)) { 1701 kfree(preq); 1702 return -EINVAL; 1703 } 1704 need = cfset_needspace(start.counter_sets); 1705 if (put_user(need, &ustart->data_bytes)) { 1706 kfree(preq); 1707 return -EFAULT; 1708 } 1709 preq->ctrset = start.counter_sets; 1710 ret = cfset_all_start(preq); 1711 if (!ret) { 1712 cfset_session_add(preq); 1713 file->private_data = preq; 1714 } else { 1715 kfree(preq); 1716 } 1717 return ret; 1718 } 1719 1720 /* Entry point to the /dev/hwctr device interface. 1721 * The ioctl system call supports three subcommands: 1722 * S390_HWCTR_START: Start the specified counter sets on a CPU list. The 1723 * counter set keeps running until explicitly stopped. Returns the number 1724 * of bytes needed to store the counter values. If another S390_HWCTR_START 1725 * ioctl subcommand is called without a previous S390_HWCTR_STOP stop 1726 * command on the same file descriptor, -EBUSY is returned. 1727 * S390_HWCTR_READ: Read the counter set values from specified CPU list given 1728 * with the S390_HWCTR_START command. 1729 * S390_HWCTR_STOP: Stops the counter sets on the CPU list given with the 1730 * previous S390_HWCTR_START subcommand. 1731 */ 1732 static long cfset_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 1733 { 1734 int ret; 1735 1736 cpus_read_lock(); 1737 mutex_lock(&cfset_ctrset_mutex); 1738 switch (cmd) { 1739 case S390_HWCTR_START: 1740 ret = cfset_ioctl_start(arg, file); 1741 break; 1742 case S390_HWCTR_STOP: 1743 ret = cfset_ioctl_stop(file); 1744 break; 1745 case S390_HWCTR_READ: 1746 ret = cfset_ioctl_read(arg, file->private_data); 1747 break; 1748 default: 1749 ret = -ENOTTY; 1750 break; 1751 } 1752 mutex_unlock(&cfset_ctrset_mutex); 1753 cpus_read_unlock(); 1754 return ret; 1755 } 1756 1757 static const struct file_operations cfset_fops = { 1758 .owner = THIS_MODULE, 1759 .open = cfset_open, 1760 .release = cfset_release, 1761 .unlocked_ioctl = cfset_ioctl, 1762 }; 1763 1764 static struct miscdevice cfset_dev = { 1765 .name = S390_HWCTR_DEVICE, 1766 .minor = MISC_DYNAMIC_MINOR, 1767 .fops = &cfset_fops, 1768 .mode = 0666, 1769 }; 1770 1771 /* Hotplug add of a CPU. Scan through all active processes and add 1772 * that CPU to the list of CPUs supplied with ioctl(..., START, ...). 1773 */ 1774 static int cfset_online_cpu(unsigned int cpu) 1775 { 1776 struct cfset_call_on_cpu_parm p; 1777 struct cfset_request *rp; 1778 1779 if (!list_empty(&cfset_session.head)) { 1780 list_for_each_entry(rp, &cfset_session.head, node) { 1781 p.sets = rp->ctrset; 1782 cfset_ioctl_on(&p); 1783 cpumask_set_cpu(cpu, &rp->mask); 1784 } 1785 } 1786 return 0; 1787 } 1788 1789 /* Hotplug remove of a CPU. Scan through all active processes and clear 1790 * that CPU from the list of CPUs supplied with ioctl(..., START, ...). 1791 * Adjust reference counts. 1792 */ 1793 static int cfset_offline_cpu(unsigned int cpu) 1794 { 1795 struct cfset_call_on_cpu_parm p; 1796 struct cfset_request *rp; 1797 1798 if (!list_empty(&cfset_session.head)) { 1799 list_for_each_entry(rp, &cfset_session.head, node) { 1800 p.sets = rp->ctrset; 1801 cfset_ioctl_off(&p); 1802 cpumask_clear_cpu(cpu, &rp->mask); 1803 } 1804 } 1805 return 0; 1806 } 1807 1808 static void cfdiag_read(struct perf_event *event) 1809 { 1810 } 1811 1812 static int get_authctrsets(void) 1813 { 1814 unsigned long auth = 0; 1815 enum cpumf_ctr_set i; 1816 1817 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) { 1818 if (cpumf_ctr_info.auth_ctl & cpumf_ctr_ctl[i]) 1819 auth |= cpumf_ctr_ctl[i]; 1820 } 1821 return auth; 1822 } 1823 1824 /* Setup the event. Test for authorized counter sets and only include counter 1825 * sets which are authorized at the time of the setup. Including unauthorized 1826 * counter sets result in specification exception (and panic). 1827 */ 1828 static int cfdiag_event_init2(struct perf_event *event) 1829 { 1830 struct perf_event_attr *attr = &event->attr; 1831 int err = 0; 1832 1833 /* Set sample_period to indicate sampling */ 1834 event->hw.config = attr->config; 1835 event->hw.sample_period = attr->sample_period; 1836 local64_set(&event->hw.period_left, event->hw.sample_period); 1837 local64_set(&event->count, 0); 1838 event->hw.last_period = event->hw.sample_period; 1839 1840 /* Add all authorized counter sets to config_base. The 1841 * the hardware init function is either called per-cpu or just once 1842 * for all CPUS (event->cpu == -1). This depends on the whether 1843 * counting is started for all CPUs or on a per workload base where 1844 * the perf event moves from one CPU to another CPU. 1845 * Checking the authorization on any CPU is fine as the hardware 1846 * applies the same authorization settings to all CPUs. 1847 */ 1848 event->hw.config_base = get_authctrsets(); 1849 1850 /* No authorized counter sets, nothing to count/sample */ 1851 if (!event->hw.config_base) 1852 err = -EINVAL; 1853 1854 return err; 1855 } 1856 1857 static int cfdiag_event_init(struct perf_event *event) 1858 { 1859 struct perf_event_attr *attr = &event->attr; 1860 int err = -ENOENT; 1861 1862 if (event->attr.config != PERF_EVENT_CPUM_CF_DIAG || 1863 event->attr.type != event->pmu->type) 1864 goto out; 1865 1866 /* Raw events are used to access counters directly, 1867 * hence do not permit excludes. 1868 * This event is useless without PERF_SAMPLE_RAW to return counter set 1869 * values as raw data. 1870 */ 1871 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv || 1872 !(attr->sample_type & (PERF_SAMPLE_CPU | PERF_SAMPLE_RAW))) { 1873 err = -EOPNOTSUPP; 1874 goto out; 1875 } 1876 1877 /* Initialize for using the CPU-measurement counter facility */ 1878 if (cpum_cf_alloc(event->cpu)) 1879 return -ENOMEM; 1880 event->destroy = hw_perf_event_destroy; 1881 1882 err = cfdiag_event_init2(event); 1883 out: 1884 return err; 1885 } 1886 1887 /* Create cf_diag/events/CF_DIAG event sysfs file. This counter is used 1888 * to collect the complete counter sets for a scheduled process. Target 1889 * are complete counter sets attached as raw data to the artificial event. 1890 * This results in complete counter sets available when a process is 1891 * scheduled. Contains the delta of every counter while the process was 1892 * running. 1893 */ 1894 CPUMF_EVENT_ATTR(CF_DIAG, CF_DIAG, PERF_EVENT_CPUM_CF_DIAG); 1895 1896 static struct attribute *cfdiag_events_attr[] = { 1897 CPUMF_EVENT_PTR(CF_DIAG, CF_DIAG), 1898 NULL, 1899 }; 1900 1901 PMU_FORMAT_ATTR(event, "config:0-63"); 1902 1903 static struct attribute *cfdiag_format_attr[] = { 1904 &format_attr_event.attr, 1905 NULL, 1906 }; 1907 1908 static struct attribute_group cfdiag_events_group = { 1909 .name = "events", 1910 .attrs = cfdiag_events_attr, 1911 }; 1912 static struct attribute_group cfdiag_format_group = { 1913 .name = "format", 1914 .attrs = cfdiag_format_attr, 1915 }; 1916 static const struct attribute_group *cfdiag_attr_groups[] = { 1917 &cfdiag_events_group, 1918 &cfdiag_format_group, 1919 NULL, 1920 }; 1921 1922 /* Performance monitoring unit for event CF_DIAG. Since this event 1923 * is also started and stopped via the perf_event_open() system call, use 1924 * the same event enable/disable call back functions. They do not 1925 * have a pointer to the perf_event structure as first parameter. 1926 * 1927 * The functions XXX_add, XXX_del, XXX_start and XXX_stop are also common. 1928 * Reuse them and distinguish the event (always first parameter) via 1929 * 'config' member. 1930 */ 1931 static struct pmu cf_diag = { 1932 .task_ctx_nr = perf_sw_context, 1933 .event_init = cfdiag_event_init, 1934 .pmu_enable = cpumf_pmu_enable, 1935 .pmu_disable = cpumf_pmu_disable, 1936 .add = cpumf_pmu_add, 1937 .del = cpumf_pmu_del, 1938 .start = cpumf_pmu_start, 1939 .stop = cpumf_pmu_stop, 1940 .read = cfdiag_read, 1941 1942 .attr_groups = cfdiag_attr_groups 1943 }; 1944 1945 /* Calculate memory needed to store all counter sets together with header and 1946 * trailer data. This is independent of the counter set authorization which 1947 * can vary depending on the configuration. 1948 */ 1949 static size_t cfdiag_maxsize(struct cpumf_ctr_info *info) 1950 { 1951 size_t max_size = sizeof(struct cf_trailer_entry); 1952 enum cpumf_ctr_set i; 1953 1954 for (i = CPUMF_CTR_SET_BASIC; i < CPUMF_CTR_SET_MAX; ++i) { 1955 size_t size = cpum_cf_read_setsize(i); 1956 1957 if (size) 1958 max_size += size * sizeof(u64) + 1959 sizeof(struct cf_ctrset_entry); 1960 } 1961 return max_size; 1962 } 1963 1964 /* Get the CPU speed, try sampling facility first and CPU attributes second. */ 1965 static void cfdiag_get_cpu_speed(void) 1966 { 1967 unsigned long mhz; 1968 1969 if (cpum_sf_avail()) { /* Sampling facility first */ 1970 struct hws_qsi_info_block si; 1971 1972 memset(&si, 0, sizeof(si)); 1973 if (!qsi(&si)) { 1974 cfdiag_cpu_speed = si.cpu_speed; 1975 return; 1976 } 1977 } 1978 1979 /* Fallback: CPU speed extract static part. Used in case 1980 * CPU Measurement Sampling Facility is turned off. 1981 */ 1982 mhz = __ecag(ECAG_CPU_ATTRIBUTE, 0); 1983 if (mhz != -1UL) 1984 cfdiag_cpu_speed = mhz & 0xffffffff; 1985 } 1986 1987 static int cfset_init(void) 1988 { 1989 size_t need; 1990 int rc; 1991 1992 cfdiag_get_cpu_speed(); 1993 /* Make sure the counter set data fits into predefined buffer. */ 1994 need = cfdiag_maxsize(&cpumf_ctr_info); 1995 if (need > sizeof(((struct cpu_cf_events *)0)->start)) { 1996 pr_err("Insufficient memory for PMU(cpum_cf_diag) need=%zu\n", 1997 need); 1998 return -ENOMEM; 1999 } 2000 2001 rc = misc_register(&cfset_dev); 2002 if (rc) { 2003 pr_err("Registration of /dev/%s failed rc=%i\n", 2004 cfset_dev.name, rc); 2005 goto out; 2006 } 2007 2008 rc = perf_pmu_register(&cf_diag, "cpum_cf_diag", -1); 2009 if (rc) { 2010 misc_deregister(&cfset_dev); 2011 pr_err("Registration of PMU(cpum_cf_diag) failed with rc=%i\n", 2012 rc); 2013 } 2014 out: 2015 return rc; 2016 } 2017 2018 device_initcall(cpumf_pmu_init); 2019