1 // SPDX-License-Identifier: GPL-2.0
3 * Performance event support for the System z CPU-measurement Sampling Facility
5 * Copyright IBM Corp. 2013
6 * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
8 #define KMSG_COMPONENT "cpum_sf"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11 #include <linux/kernel.h>
12 #include <linux/kernel_stat.h>
13 #include <linux/perf_event.h>
14 #include <linux/percpu.h>
15 #include <linux/pid.h>
16 #include <linux/notifier.h>
17 #include <linux/export.h>
18 #include <linux/slab.h>
20 #include <linux/moduleparam.h>
21 #include <asm/cpu_mf.h>
23 #include <asm/debug.h>
24 #include <asm/timex.h>
26 /* Minimum number of sample-data-block-tables:
27 * At least one table is required for the sampling buffer structure.
28 * A single table contains up to 511 pointers to sample-data-blocks.
30 #define CPUM_SF_MIN_SDBT 1
32 /* Number of sample-data-blocks per sample-data-block-table (SDBT):
33 * A table contains SDB pointers (8 bytes) and one table-link entry
34 * that points to the origin of the next SDBT.
36 #define CPUM_SF_SDB_PER_TABLE ((PAGE_SIZE - 8) / 8)
38 /* Maximum page offset for an SDBT table-link entry:
39 * If this page offset is reached, a table-link entry to the next SDBT
42 #define CPUM_SF_SDBT_TL_OFFSET (CPUM_SF_SDB_PER_TABLE * 8)
43 static inline int require_table_link(const void *sdbt
)
45 return ((unsigned long) sdbt
& ~PAGE_MASK
) == CPUM_SF_SDBT_TL_OFFSET
;
48 /* Minimum and maximum sampling buffer sizes:
50 * This number represents the maximum size of the sampling buffer taking
51 * the number of sample-data-block-tables into account. Note that these
52 * numbers apply to the basic-sampling function only.
53 * The maximum number of SDBs is increased by CPUM_SF_SDB_DIAG_FACTOR if
54 * the diagnostic-sampling function is active.
56 * Sampling buffer size Buffer characteristics
57 * ---------------------------------------------------
58 * 64KB == 16 pages (4KB per page)
59 * 1 page for SDB-tables
62 * 32MB == 8192 pages (4KB per page)
63 * 16 pages for SDB-tables
66 static unsigned long __read_mostly CPUM_SF_MIN_SDB
= 15;
67 static unsigned long __read_mostly CPUM_SF_MAX_SDB
= 8176;
68 static unsigned long __read_mostly CPUM_SF_SDB_DIAG_FACTOR
= 1;
71 unsigned long *sdbt
; /* Sample-data-block-table origin */
72 /* buffer characteristics (required for buffer increments) */
73 unsigned long num_sdb
; /* Number of sample-data-blocks */
74 unsigned long num_sdbt
; /* Number of sample-data-block-tables */
75 unsigned long *tail
; /* last sample-data-block-table */
80 unsigned long head
; /* index of SDB of buffer head */
81 unsigned long alert_mark
; /* index of SDB of alert request position */
82 unsigned long empty_mark
; /* mark of SDB not marked full */
83 unsigned long *sdb_index
; /* SDB address for fast lookup */
84 unsigned long *sdbt_index
; /* SDBT address for fast lookup */
88 /* CPU-measurement sampling information block */
89 struct hws_qsi_info_block qsi
;
90 /* CPU-measurement sampling control block */
91 struct hws_lsctl_request_block lsctl
;
92 struct sf_buffer sfb
; /* Sampling buffer */
93 unsigned int flags
; /* Status flags */
94 struct perf_event
*event
; /* Scheduled perf event */
95 struct perf_output_handle handle
; /* AUX buffer output handle */
97 static DEFINE_PER_CPU(struct cpu_hw_sf
, cpu_hw_sf
);
100 static debug_info_t
*sfdbg
;
103 * sf_disable() - Switch off sampling facility
105 static int sf_disable(void)
107 struct hws_lsctl_request_block sreq
;
109 memset(&sreq
, 0, sizeof(sreq
));
114 * sf_buffer_available() - Check for an allocated sampling buffer
116 static int sf_buffer_available(struct cpu_hw_sf
*cpuhw
)
118 return !!cpuhw
->sfb
.sdbt
;
122 * deallocate sampling facility buffer
124 static void free_sampling_buffer(struct sf_buffer
*sfb
)
126 unsigned long *sdbt
, *curr
;
134 /* Free the SDBT after all SDBs are processed... */
139 /* Process table-link entries */
140 if (is_link_entry(curr
)) {
141 curr
= get_next_sdbt(curr
);
143 free_page((unsigned long) sdbt
);
145 /* If the origin is reached, sampling buffer is freed */
146 if (curr
== sfb
->sdbt
)
151 /* Process SDB pointer */
159 debug_sprintf_event(sfdbg
, 5,
160 "free_sampling_buffer: freed sdbt=%p\n", sfb
->sdbt
);
161 memset(sfb
, 0, sizeof(*sfb
));
164 static int alloc_sample_data_block(unsigned long *sdbt
, gfp_t gfp_flags
)
166 unsigned long sdb
, *trailer
;
168 /* Allocate and initialize sample-data-block */
169 sdb
= get_zeroed_page(gfp_flags
);
172 trailer
= trailer_entry_ptr(sdb
);
173 *trailer
= SDB_TE_ALERT_REQ_MASK
;
175 /* Link SDB into the sample-data-block-table */
182 * realloc_sampling_buffer() - extend sampler memory
184 * Allocates new sample-data-blocks and adds them to the specified sampling
187 * Important: This modifies the sampling buffer and must be called when the
188 * sampling facility is disabled.
190 * Returns zero on success, non-zero otherwise.
192 static int realloc_sampling_buffer(struct sf_buffer
*sfb
,
193 unsigned long num_sdb
, gfp_t gfp_flags
)
196 unsigned long *new, *tail
;
198 if (!sfb
->sdbt
|| !sfb
->tail
)
201 if (!is_link_entry(sfb
->tail
))
204 /* Append to the existing sampling buffer, overwriting the table-link
206 * The tail variables always points to the "tail" (last and table-link)
207 * entry in an SDB-table.
211 /* Do a sanity check whether the table-link entry points to
212 * the sampling buffer origin.
214 if (sfb
->sdbt
!= get_next_sdbt(tail
)) {
215 debug_sprintf_event(sfdbg
, 3, "realloc_sampling_buffer: "
216 "sampling buffer is not linked: origin=%p"
218 (void *) sfb
->sdbt
, (void *) tail
);
222 /* Allocate remaining SDBs */
224 for (i
= 0; i
< num_sdb
; i
++) {
225 /* Allocate a new SDB-table if it is full. */
226 if (require_table_link(tail
)) {
227 new = (unsigned long *) get_zeroed_page(gfp_flags
);
233 /* Link current page to tail of chain */
234 *tail
= (unsigned long)(void *) new + 1;
238 /* Allocate a new sample-data-block.
239 * If there is not enough memory, stop the realloc process
240 * and simply use what was allocated. If this is a temporary
241 * issue, a new realloc call (if required) might succeed.
243 rc
= alloc_sample_data_block(tail
, gfp_flags
);
250 /* Link sampling buffer to its origin */
251 *tail
= (unsigned long) sfb
->sdbt
+ 1;
254 debug_sprintf_event(sfdbg
, 4, "realloc_sampling_buffer: new buffer"
255 " settings: sdbt=%lu sdb=%lu\n",
256 sfb
->num_sdbt
, sfb
->num_sdb
);
261 * allocate_sampling_buffer() - allocate sampler memory
263 * Allocates and initializes a sampling buffer structure using the
264 * specified number of sample-data-blocks (SDB). For each allocation,
265 * a 4K page is used. The number of sample-data-block-tables (SDBT)
266 * are calculated from SDBs.
267 * Also set the ALERT_REQ mask in each SDBs trailer.
269 * Returns zero on success, non-zero otherwise.
271 static int alloc_sampling_buffer(struct sf_buffer
*sfb
, unsigned long num_sdb
)
278 /* Allocate the sample-data-block-table origin */
279 sfb
->sdbt
= (unsigned long *) get_zeroed_page(GFP_KERNEL
);
285 /* Link the table origin to point to itself to prepare for
286 * realloc_sampling_buffer() invocation.
288 sfb
->tail
= sfb
->sdbt
;
289 *sfb
->tail
= (unsigned long)(void *) sfb
->sdbt
+ 1;
291 /* Allocate requested number of sample-data-blocks */
292 rc
= realloc_sampling_buffer(sfb
, num_sdb
, GFP_KERNEL
);
294 free_sampling_buffer(sfb
);
295 debug_sprintf_event(sfdbg
, 4, "alloc_sampling_buffer: "
296 "realloc_sampling_buffer failed with rc=%i\n", rc
);
298 debug_sprintf_event(sfdbg
, 4,
299 "alloc_sampling_buffer: tear=%p dear=%p\n",
300 sfb
->sdbt
, (void *) *sfb
->sdbt
);
304 static void sfb_set_limits(unsigned long min
, unsigned long max
)
306 struct hws_qsi_info_block si
;
308 CPUM_SF_MIN_SDB
= min
;
309 CPUM_SF_MAX_SDB
= max
;
311 memset(&si
, 0, sizeof(si
));
313 CPUM_SF_SDB_DIAG_FACTOR
= DIV_ROUND_UP(si
.dsdes
, si
.bsdes
);
316 static unsigned long sfb_max_limit(struct hw_perf_event
*hwc
)
318 return SAMPL_DIAG_MODE(hwc
) ? CPUM_SF_MAX_SDB
* CPUM_SF_SDB_DIAG_FACTOR
322 static unsigned long sfb_pending_allocs(struct sf_buffer
*sfb
,
323 struct hw_perf_event
*hwc
)
326 return SFB_ALLOC_REG(hwc
);
327 if (SFB_ALLOC_REG(hwc
) > sfb
->num_sdb
)
328 return SFB_ALLOC_REG(hwc
) - sfb
->num_sdb
;
332 static int sfb_has_pending_allocs(struct sf_buffer
*sfb
,
333 struct hw_perf_event
*hwc
)
335 return sfb_pending_allocs(sfb
, hwc
) > 0;
338 static void sfb_account_allocs(unsigned long num
, struct hw_perf_event
*hwc
)
340 /* Limit the number of SDBs to not exceed the maximum */
341 num
= min_t(unsigned long, num
, sfb_max_limit(hwc
) - SFB_ALLOC_REG(hwc
));
343 SFB_ALLOC_REG(hwc
) += num
;
346 static void sfb_init_allocs(unsigned long num
, struct hw_perf_event
*hwc
)
348 SFB_ALLOC_REG(hwc
) = 0;
349 sfb_account_allocs(num
, hwc
);
352 static void deallocate_buffers(struct cpu_hw_sf
*cpuhw
)
355 free_sampling_buffer(&cpuhw
->sfb
);
358 static int allocate_buffers(struct cpu_hw_sf
*cpuhw
, struct hw_perf_event
*hwc
)
360 unsigned long n_sdb
, freq
, factor
;
363 /* Calculate sampling buffers using 4K pages
365 * 1. Determine the sample data size which depends on the used
366 * sampling functions, for example, basic-sampling or
367 * basic-sampling with diagnostic-sampling.
369 * 2. Use the sampling frequency as input. The sampling buffer is
370 * designed for almost one second. This can be adjusted through
371 * the "factor" variable.
372 * In any case, alloc_sampling_buffer() sets the Alert Request
373 * Control indicator to trigger a measurement-alert to harvest
374 * sample-data-blocks (sdb).
376 * 3. Compute the number of sample-data-blocks and ensure a minimum
377 * of CPUM_SF_MIN_SDB. Also ensure the upper limit does not
378 * exceed a "calculated" maximum. The symbolic maximum is
379 * designed for basic-sampling only and needs to be increased if
380 * diagnostic-sampling is active.
381 * See also the remarks for these symbolic constants.
383 * 4. Compute the number of sample-data-block-tables (SDBT) and
384 * ensure a minimum of CPUM_SF_MIN_SDBT (one table can manage up
387 sample_size
= sizeof(struct hws_basic_entry
);
388 freq
= sample_rate_to_freq(&cpuhw
->qsi
, SAMPL_RATE(hwc
));
390 n_sdb
= DIV_ROUND_UP(freq
, factor
* ((PAGE_SIZE
-64) / sample_size
));
391 if (n_sdb
< CPUM_SF_MIN_SDB
)
392 n_sdb
= CPUM_SF_MIN_SDB
;
394 /* If there is already a sampling buffer allocated, it is very likely
395 * that the sampling facility is enabled too. If the event to be
396 * initialized requires a greater sampling buffer, the allocation must
397 * be postponed. Changing the sampling buffer requires the sampling
398 * facility to be in the disabled state. So, account the number of
399 * required SDBs and let cpumsf_pmu_enable() resize the buffer just
400 * before the event is started.
402 sfb_init_allocs(n_sdb
, hwc
);
403 if (sf_buffer_available(cpuhw
))
406 debug_sprintf_event(sfdbg
, 3,
407 "allocate_buffers: rate=%lu f=%lu sdb=%lu/%lu"
408 " sample_size=%lu cpuhw=%p\n",
409 SAMPL_RATE(hwc
), freq
, n_sdb
, sfb_max_limit(hwc
),
412 return alloc_sampling_buffer(&cpuhw
->sfb
,
413 sfb_pending_allocs(&cpuhw
->sfb
, hwc
));
416 static unsigned long min_percent(unsigned int percent
, unsigned long base
,
419 return min_t(unsigned long, min
, DIV_ROUND_UP(percent
* base
, 100));
422 static unsigned long compute_sfb_extent(unsigned long ratio
, unsigned long base
)
424 /* Use a percentage-based approach to extend the sampling facility
425 * buffer. Accept up to 5% sample data loss.
426 * Vary the extents between 1% to 5% of the current number of
427 * sample-data-blocks.
432 return min_percent(1, base
, 1);
434 return min_percent(1, base
, 1);
436 return min_percent(2, base
, 2);
438 return min_percent(3, base
, 3);
440 return min_percent(4, base
, 4);
442 return min_percent(5, base
, 8);
445 static void sfb_account_overflows(struct cpu_hw_sf
*cpuhw
,
446 struct hw_perf_event
*hwc
)
448 unsigned long ratio
, num
;
450 if (!OVERFLOW_REG(hwc
))
453 /* The sample_overflow contains the average number of sample data
454 * that has been lost because sample-data-blocks were full.
456 * Calculate the total number of sample data entries that has been
457 * discarded. Then calculate the ratio of lost samples to total samples
458 * per second in percent.
460 ratio
= DIV_ROUND_UP(100 * OVERFLOW_REG(hwc
) * cpuhw
->sfb
.num_sdb
,
461 sample_rate_to_freq(&cpuhw
->qsi
, SAMPL_RATE(hwc
)));
463 /* Compute number of sample-data-blocks */
464 num
= compute_sfb_extent(ratio
, cpuhw
->sfb
.num_sdb
);
466 sfb_account_allocs(num
, hwc
);
468 debug_sprintf_event(sfdbg
, 5, "sfb: overflow: overflow=%llu ratio=%lu"
469 " num=%lu\n", OVERFLOW_REG(hwc
), ratio
, num
);
470 OVERFLOW_REG(hwc
) = 0;
473 /* extend_sampling_buffer() - Extend sampling buffer
474 * @sfb: Sampling buffer structure (for local CPU)
475 * @hwc: Perf event hardware structure
477 * Use this function to extend the sampling buffer based on the overflow counter
478 * and postponed allocation extents stored in the specified Perf event hardware.
480 * Important: This function disables the sampling facility in order to safely
481 * change the sampling buffer structure. Do not call this function
482 * when the PMU is active.
484 static void extend_sampling_buffer(struct sf_buffer
*sfb
,
485 struct hw_perf_event
*hwc
)
487 unsigned long num
, num_old
;
490 num
= sfb_pending_allocs(sfb
, hwc
);
493 num_old
= sfb
->num_sdb
;
495 /* Disable the sampling facility to reset any states and also
496 * clear pending measurement alerts.
500 /* Extend the sampling buffer.
501 * This memory allocation typically happens in an atomic context when
502 * called by perf. Because this is a reallocation, it is fine if the
503 * new SDB-request cannot be satisfied immediately.
505 rc
= realloc_sampling_buffer(sfb
, num
, GFP_ATOMIC
);
507 debug_sprintf_event(sfdbg
, 5, "sfb: extend: realloc "
508 "failed with rc=%i\n", rc
);
510 if (sfb_has_pending_allocs(sfb
, hwc
))
511 debug_sprintf_event(sfdbg
, 5, "sfb: extend: "
512 "req=%lu alloc=%lu remaining=%lu\n",
513 num
, sfb
->num_sdb
- num_old
,
514 sfb_pending_allocs(sfb
, hwc
));
518 /* Number of perf events counting hardware events */
519 static atomic_t num_events
;
520 /* Used to avoid races in calling reserve/release_cpumf_hardware */
521 static DEFINE_MUTEX(pmc_reserve_mutex
);
524 #define PMC_RELEASE 1
525 #define PMC_FAILURE 2
526 static void setup_pmc_cpu(void *flags
)
529 struct cpu_hw_sf
*cpusf
= this_cpu_ptr(&cpu_hw_sf
);
532 switch (*((int *) flags
)) {
534 memset(cpusf
, 0, sizeof(*cpusf
));
535 err
= qsi(&cpusf
->qsi
);
538 cpusf
->flags
|= PMU_F_RESERVED
;
541 pr_err("Switching off the sampling facility failed "
542 "with rc=%i\n", err
);
543 debug_sprintf_event(sfdbg
, 5,
544 "setup_pmc_cpu: initialized: cpuhw=%p\n", cpusf
);
547 cpusf
->flags
&= ~PMU_F_RESERVED
;
550 pr_err("Switching off the sampling facility failed "
551 "with rc=%i\n", err
);
553 deallocate_buffers(cpusf
);
554 debug_sprintf_event(sfdbg
, 5,
555 "setup_pmc_cpu: released: cpuhw=%p\n", cpusf
);
559 *((int *) flags
) |= PMC_FAILURE
;
562 static void release_pmc_hardware(void)
564 int flags
= PMC_RELEASE
;
566 irq_subclass_unregister(IRQ_SUBCLASS_MEASUREMENT_ALERT
);
567 on_each_cpu(setup_pmc_cpu
, &flags
, 1);
570 static int reserve_pmc_hardware(void)
572 int flags
= PMC_INIT
;
574 on_each_cpu(setup_pmc_cpu
, &flags
, 1);
575 if (flags
& PMC_FAILURE
) {
576 release_pmc_hardware();
579 irq_subclass_register(IRQ_SUBCLASS_MEASUREMENT_ALERT
);
584 static void hw_perf_event_destroy(struct perf_event
*event
)
586 /* Release PMC if this is the last perf event */
587 if (!atomic_add_unless(&num_events
, -1, 1)) {
588 mutex_lock(&pmc_reserve_mutex
);
589 if (atomic_dec_return(&num_events
) == 0)
590 release_pmc_hardware();
591 mutex_unlock(&pmc_reserve_mutex
);
595 static void hw_init_period(struct hw_perf_event
*hwc
, u64 period
)
597 hwc
->sample_period
= period
;
598 hwc
->last_period
= hwc
->sample_period
;
599 local64_set(&hwc
->period_left
, hwc
->sample_period
);
602 static void hw_reset_registers(struct hw_perf_event
*hwc
,
603 unsigned long *sdbt_origin
)
605 /* (Re)set to first sample-data-block-table */
606 TEAR_REG(hwc
) = (unsigned long) sdbt_origin
;
609 static unsigned long hw_limit_rate(const struct hws_qsi_info_block
*si
,
612 return clamp_t(unsigned long, rate
,
613 si
->min_sampl_rate
, si
->max_sampl_rate
);
616 static u32
cpumsf_pid_type(struct perf_event
*event
,
617 u32 pid
, enum pid_type type
)
619 struct task_struct
*tsk
;
625 tsk
= find_task_by_pid_ns(pid
, &init_pid_ns
);
629 * Only top level events contain the pid namespace in which
633 event
= event
->parent
;
634 pid
= __task_pid_nr_ns(tsk
, type
, event
->ns
);
636 * See also 1d953111b648
637 * "perf/core: Don't report zero PIDs for exiting tasks".
639 if (!pid
&& !pid_alive(tsk
))
646 static void cpumsf_output_event_pid(struct perf_event
*event
,
647 struct perf_sample_data
*data
,
648 struct pt_regs
*regs
)
651 struct perf_event_header header
;
652 struct perf_output_handle handle
;
655 * Obtain the PID from the basic-sampling data entry and
656 * correct the data->tid_entry.pid value.
658 pid
= data
->tid_entry
.pid
;
660 /* Protect callchain buffers, tasks */
663 perf_prepare_sample(&header
, data
, event
, regs
);
664 if (perf_output_begin(&handle
, event
, header
.size
))
667 /* Update the process ID (see also kernel/events/core.c) */
668 data
->tid_entry
.pid
= cpumsf_pid_type(event
, pid
, __PIDTYPE_TGID
);
669 data
->tid_entry
.tid
= cpumsf_pid_type(event
, pid
, PIDTYPE_PID
);
671 perf_output_sample(&handle
, &header
, data
, event
);
672 perf_output_end(&handle
);
677 static int __hw_perf_event_init(struct perf_event
*event
)
679 struct cpu_hw_sf
*cpuhw
;
680 struct hws_qsi_info_block si
;
681 struct perf_event_attr
*attr
= &event
->attr
;
682 struct hw_perf_event
*hwc
= &event
->hw
;
686 /* Reserve CPU-measurement sampling facility */
688 if (!atomic_inc_not_zero(&num_events
)) {
689 mutex_lock(&pmc_reserve_mutex
);
690 if (atomic_read(&num_events
) == 0 && reserve_pmc_hardware())
693 atomic_inc(&num_events
);
694 mutex_unlock(&pmc_reserve_mutex
);
696 event
->destroy
= hw_perf_event_destroy
;
701 /* Access per-CPU sampling information (query sampling info) */
703 * The event->cpu value can be -1 to count on every CPU, for example,
704 * when attaching to a task. If this is specified, use the query
705 * sampling info from the current CPU, otherwise use event->cpu to
706 * retrieve the per-CPU information.
707 * Later, cpuhw indicates whether to allocate sampling buffers for a
708 * particular CPU (cpuhw!=NULL) or each online CPU (cpuw==NULL).
710 memset(&si
, 0, sizeof(si
));
712 if (event
->cpu
== -1)
715 /* Event is pinned to a particular CPU, retrieve the per-CPU
716 * sampling structure for accessing the CPU-specific QSI.
718 cpuhw
= &per_cpu(cpu_hw_sf
, event
->cpu
);
722 /* Check sampling facility authorization and, if not authorized,
723 * fall back to other PMUs. It is safe to check any CPU because
724 * the authorization is identical for all configured CPUs.
731 /* Always enable basic sampling */
732 SAMPL_FLAGS(hwc
) = PERF_CPUM_SF_BASIC_MODE
;
734 /* Check if diagnostic sampling is requested. Deny if the required
735 * sampling authorization is missing.
737 if (attr
->config
== PERF_EVENT_CPUM_SF_DIAG
) {
742 SAMPL_FLAGS(hwc
) |= PERF_CPUM_SF_DIAG_MODE
;
745 /* Check and set other sampling flags */
746 if (attr
->config1
& PERF_CPUM_SF_FULL_BLOCKS
)
747 SAMPL_FLAGS(hwc
) |= PERF_CPUM_SF_FULL_BLOCKS
;
749 /* The sampling information (si) contains information about the
750 * min/max sampling intervals and the CPU speed. So calculate the
751 * correct sampling interval and avoid the whole period adjust
756 rate
= freq_to_sample_rate(&si
, attr
->sample_freq
);
757 rate
= hw_limit_rate(&si
, rate
);
759 attr
->sample_period
= rate
;
761 /* The min/max sampling rates specifies the valid range
762 * of sample periods. If the specified sample period is
763 * out of range, limit the period to the range boundary.
765 rate
= hw_limit_rate(&si
, hwc
->sample_period
);
767 /* The perf core maintains a maximum sample rate that is
768 * configurable through the sysctl interface. Ensure the
769 * sampling rate does not exceed this value. This also helps
770 * to avoid throttling when pushing samples with
771 * perf_event_overflow().
773 if (sample_rate_to_freq(&si
, rate
) >
774 sysctl_perf_event_sample_rate
) {
776 debug_sprintf_event(sfdbg
, 1, "Sampling rate exceeds maximum perf sample rate\n");
780 SAMPL_RATE(hwc
) = rate
;
781 hw_init_period(hwc
, SAMPL_RATE(hwc
));
783 /* Initialize sample data overflow accounting */
784 hwc
->extra_reg
.reg
= REG_OVERFLOW
;
785 OVERFLOW_REG(hwc
) = 0;
787 /* Use AUX buffer. No need to allocate it by ourself */
788 if (attr
->config
== PERF_EVENT_CPUM_SF_DIAG
)
791 /* Allocate the per-CPU sampling buffer using the CPU information
792 * from the event. If the event is not pinned to a particular
793 * CPU (event->cpu == -1; or cpuhw == NULL), allocate sampling
794 * buffers for each online CPU.
797 /* Event is pinned to a particular CPU */
798 err
= allocate_buffers(cpuhw
, hwc
);
800 /* Event is not pinned, allocate sampling buffer on
803 for_each_online_cpu(cpu
) {
804 cpuhw
= &per_cpu(cpu_hw_sf
, cpu
);
805 err
= allocate_buffers(cpuhw
, hwc
);
811 /* If PID/TID sampling is active, replace the default overflow
812 * handler to extract and resolve the PIDs from the basic-sampling
815 if (event
->attr
.sample_type
& PERF_SAMPLE_TID
)
816 if (is_default_overflow_handler(event
))
817 event
->overflow_handler
= cpumsf_output_event_pid
;
822 static int cpumsf_pmu_event_init(struct perf_event
*event
)
826 /* No support for taken branch sampling */
827 if (has_branch_stack(event
))
830 switch (event
->attr
.type
) {
832 if ((event
->attr
.config
!= PERF_EVENT_CPUM_SF
) &&
833 (event
->attr
.config
!= PERF_EVENT_CPUM_SF_DIAG
))
836 case PERF_TYPE_HARDWARE
:
837 /* Support sampling of CPU cycles in addition to the
838 * counter facility. However, the counter facility
839 * is more precise and, hence, restrict this PMU to
840 * sampling events only.
842 if (event
->attr
.config
!= PERF_COUNT_HW_CPU_CYCLES
)
844 if (!is_sampling_event(event
))
851 /* Check online status of the CPU to which the event is pinned */
852 if (event
->cpu
>= 0 && !cpu_online(event
->cpu
))
855 /* Force reset of idle/hv excludes regardless of what the
858 if (event
->attr
.exclude_hv
)
859 event
->attr
.exclude_hv
= 0;
860 if (event
->attr
.exclude_idle
)
861 event
->attr
.exclude_idle
= 0;
863 err
= __hw_perf_event_init(event
);
866 event
->destroy(event
);
870 static void cpumsf_pmu_enable(struct pmu
*pmu
)
872 struct cpu_hw_sf
*cpuhw
= this_cpu_ptr(&cpu_hw_sf
);
873 struct hw_perf_event
*hwc
;
876 if (cpuhw
->flags
& PMU_F_ENABLED
)
879 if (cpuhw
->flags
& PMU_F_ERR_MASK
)
882 /* Check whether to extent the sampling buffer.
884 * Two conditions trigger an increase of the sampling buffer for a
886 * 1. Postponed buffer allocations from the event initialization.
887 * 2. Sampling overflows that contribute to pending allocations.
889 * Note that the extend_sampling_buffer() function disables the sampling
890 * facility, but it can be fully re-enabled using sampling controls that
891 * have been saved in cpumsf_pmu_disable().
894 hwc
= &cpuhw
->event
->hw
;
895 if (!(SAMPL_DIAG_MODE(hwc
))) {
897 * Account number of overflow-designated
900 sfb_account_overflows(cpuhw
, hwc
);
901 if (sfb_has_pending_allocs(&cpuhw
->sfb
, hwc
))
902 extend_sampling_buffer(&cpuhw
->sfb
, hwc
);
906 /* (Re)enable the PMU and sampling facility */
907 cpuhw
->flags
|= PMU_F_ENABLED
;
910 err
= lsctl(&cpuhw
->lsctl
);
912 cpuhw
->flags
&= ~PMU_F_ENABLED
;
913 pr_err("Loading sampling controls failed: op=%i err=%i\n",
918 /* Load current program parameter */
919 lpp(&S390_lowcore
.lpp
);
921 debug_sprintf_event(sfdbg
, 6, "pmu_enable: es=%i cs=%i ed=%i cd=%i "
922 "tear=%p dear=%p\n", cpuhw
->lsctl
.es
, cpuhw
->lsctl
.cs
,
923 cpuhw
->lsctl
.ed
, cpuhw
->lsctl
.cd
,
924 (void *) cpuhw
->lsctl
.tear
, (void *) cpuhw
->lsctl
.dear
);
927 static void cpumsf_pmu_disable(struct pmu
*pmu
)
929 struct cpu_hw_sf
*cpuhw
= this_cpu_ptr(&cpu_hw_sf
);
930 struct hws_lsctl_request_block inactive
;
931 struct hws_qsi_info_block si
;
934 if (!(cpuhw
->flags
& PMU_F_ENABLED
))
937 if (cpuhw
->flags
& PMU_F_ERR_MASK
)
940 /* Switch off sampling activation control */
941 inactive
= cpuhw
->lsctl
;
945 err
= lsctl(&inactive
);
947 pr_err("Loading sampling controls failed: op=%i err=%i\n",
952 /* Save state of TEAR and DEAR register contents */
954 /* TEAR/DEAR values are valid only if the sampling facility is
955 * enabled. Note that cpumsf_pmu_disable() might be called even
956 * for a disabled sampling facility because cpumsf_pmu_enable()
957 * controls the enable/disable state.
960 cpuhw
->lsctl
.tear
= si
.tear
;
961 cpuhw
->lsctl
.dear
= si
.dear
;
964 debug_sprintf_event(sfdbg
, 3, "cpumsf_pmu_disable: "
965 "qsi() failed with err=%i\n", err
);
967 cpuhw
->flags
&= ~PMU_F_ENABLED
;
970 /* perf_exclude_event() - Filter event
971 * @event: The perf event
972 * @regs: pt_regs structure
973 * @sde_regs: Sample-data-entry (sde) regs structure
975 * Filter perf events according to their exclude specification.
977 * Return non-zero if the event shall be excluded.
979 static int perf_exclude_event(struct perf_event
*event
, struct pt_regs
*regs
,
980 struct perf_sf_sde_regs
*sde_regs
)
982 if (event
->attr
.exclude_user
&& user_mode(regs
))
984 if (event
->attr
.exclude_kernel
&& !user_mode(regs
))
986 if (event
->attr
.exclude_guest
&& sde_regs
->in_guest
)
988 if (event
->attr
.exclude_host
&& !sde_regs
->in_guest
)
993 /* perf_push_sample() - Push samples to perf
994 * @event: The perf event
995 * @sample: Hardware sample data
997 * Use the hardware sample data to create perf event sample. The sample
998 * is the pushed to the event subsystem and the function checks for
999 * possible event overflows. If an event overflow occurs, the PMU is
1002 * Return non-zero if an event overflow occurred.
1004 static int perf_push_sample(struct perf_event
*event
,
1005 struct hws_basic_entry
*basic
)
1008 struct pt_regs regs
;
1009 struct perf_sf_sde_regs
*sde_regs
;
1010 struct perf_sample_data data
;
1012 /* Setup perf sample */
1013 perf_sample_data_init(&data
, 0, event
->hw
.last_period
);
1015 /* Setup pt_regs to look like an CPU-measurement external interrupt
1016 * using the Program Request Alert code. The regs.int_parm_long
1017 * field which is unused contains additional sample-data-entry related
1020 memset(®s
, 0, sizeof(regs
));
1021 regs
.int_code
= 0x1407;
1022 regs
.int_parm
= CPU_MF_INT_SF_PRA
;
1023 sde_regs
= (struct perf_sf_sde_regs
*) ®s
.int_parm_long
;
1025 psw_bits(regs
.psw
).ia
= basic
->ia
;
1026 psw_bits(regs
.psw
).dat
= basic
->T
;
1027 psw_bits(regs
.psw
).wait
= basic
->W
;
1028 psw_bits(regs
.psw
).pstate
= basic
->P
;
1029 psw_bits(regs
.psw
).as
= basic
->AS
;
1032 * Use the hardware provided configuration level to decide if the
1033 * sample belongs to a guest or host. If that is not available,
1034 * fall back to the following heuristics:
1035 * A non-zero guest program parameter always indicates a guest
1036 * sample. Some early samples or samples from guests without
1037 * lpp usage would be misaccounted to the host. We use the asn
1038 * value as an addon heuristic to detect most of these guest samples.
1039 * If the value differs from 0xffff (the host value), we assume to
1042 switch (basic
->CL
) {
1043 case 1: /* logical partition */
1044 sde_regs
->in_guest
= 0;
1046 case 2: /* virtual machine */
1047 sde_regs
->in_guest
= 1;
1049 default: /* old machine, use heuristics */
1050 if (basic
->gpp
|| basic
->prim_asn
!= 0xffff)
1051 sde_regs
->in_guest
= 1;
1056 * Store the PID value from the sample-data-entry to be
1057 * processed and resolved by cpumsf_output_event_pid().
1059 data
.tid_entry
.pid
= basic
->hpp
& LPP_PID_MASK
;
1062 if (perf_exclude_event(event
, ®s
, sde_regs
))
1064 if (perf_event_overflow(event
, &data
, ®s
)) {
1066 event
->pmu
->stop(event
, 0);
1068 perf_event_update_userpage(event
);
1073 static void perf_event_count_update(struct perf_event
*event
, u64 count
)
1075 local64_add(count
, &event
->count
);
1078 static void debug_sample_entry(struct hws_basic_entry
*sample
,
1079 struct hws_trailer_entry
*te
)
1081 debug_sprintf_event(sfdbg
, 4, "hw_collect_samples: Found unknown "
1082 "sampling data entry: te->f=%i basic.def=%04x (%p)\n",
1083 te
->f
, sample
->def
, sample
);
1086 /* hw_collect_samples() - Walk through a sample-data-block and collect samples
1087 * @event: The perf event
1088 * @sdbt: Sample-data-block table
1089 * @overflow: Event overflow counter
1091 * Walks through a sample-data-block and collects sampling data entries that are
1092 * then pushed to the perf event subsystem. Depending on the sampling function,
1093 * there can be either basic-sampling or combined-sampling data entries. A
1094 * combined-sampling data entry consists of a basic- and a diagnostic-sampling
1095 * data entry. The sampling function is determined by the flags in the perf
1096 * event hardware structure. The function always works with a combined-sampling
1097 * data entry but ignores the the diagnostic portion if it is not available.
1099 * Note that the implementation focuses on basic-sampling data entries and, if
1100 * such an entry is not valid, the entire combined-sampling data entry is
1103 * The overflow variables counts the number of samples that has been discarded
1104 * due to a perf event overflow.
1106 static void hw_collect_samples(struct perf_event
*event
, unsigned long *sdbt
,
1107 unsigned long long *overflow
)
1109 struct hws_trailer_entry
*te
;
1110 struct hws_basic_entry
*sample
;
1112 te
= (struct hws_trailer_entry
*) trailer_entry_ptr(*sdbt
);
1113 sample
= (struct hws_basic_entry
*) *sdbt
;
1114 while ((unsigned long *) sample
< (unsigned long *) te
) {
1115 /* Check for an empty sample */
1119 /* Update perf event period */
1120 perf_event_count_update(event
, SAMPL_RATE(&event
->hw
));
1122 /* Check whether sample is valid */
1123 if (sample
->def
== 0x0001) {
1124 /* If an event overflow occurred, the PMU is stopped to
1125 * throttle event delivery. Remaining sample data is
1129 /* Check whether sample is consistent */
1130 if (sample
->I
== 0 && sample
->W
== 0) {
1131 /* Deliver sample data to perf */
1132 *overflow
= perf_push_sample(event
,
1136 /* Count discarded samples */
1139 debug_sample_entry(sample
, te
);
1140 /* Sample slot is not yet written or other record.
1142 * This condition can occur if the buffer was reused
1143 * from a combined basic- and diagnostic-sampling.
1144 * If only basic-sampling is then active, entries are
1145 * written into the larger diagnostic entries.
1146 * This is typically the case for sample-data-blocks
1147 * that are not full. Stop processing if the first
1148 * invalid format was detected.
1154 /* Reset sample slot and advance to next sample */
1160 /* hw_perf_event_update() - Process sampling buffer
1161 * @event: The perf event
1162 * @flush_all: Flag to also flush partially filled sample-data-blocks
1164 * Processes the sampling buffer and create perf event samples.
1165 * The sampling buffer position are retrieved and saved in the TEAR_REG
1166 * register of the specified perf event.
1168 * Only full sample-data-blocks are processed. Specify the flash_all flag
1169 * to also walk through partially filled sample-data-blocks. It is ignored
1170 * if PERF_CPUM_SF_FULL_BLOCKS is set. The PERF_CPUM_SF_FULL_BLOCKS flag
1171 * enforces the processing of full sample-data-blocks only (trailer entries
1172 * with the block-full-indicator bit set).
1174 static void hw_perf_event_update(struct perf_event
*event
, int flush_all
)
1176 struct hw_perf_event
*hwc
= &event
->hw
;
1177 struct hws_trailer_entry
*te
;
1178 unsigned long *sdbt
;
1179 unsigned long long event_overflow
, sampl_overflow
, num_sdb
, te_flags
;
1183 * AUX buffer is used when in diagnostic sampling mode.
1184 * No perf events/samples are created.
1186 if (SAMPL_DIAG_MODE(&event
->hw
))
1189 if (flush_all
&& SDB_FULL_BLOCKS(hwc
))
1192 sdbt
= (unsigned long *) TEAR_REG(hwc
);
1193 done
= event_overflow
= sampl_overflow
= num_sdb
= 0;
1195 /* Get the trailer entry of the sample-data-block */
1196 te
= (struct hws_trailer_entry
*) trailer_entry_ptr(*sdbt
);
1198 /* Leave loop if no more work to do (block full indicator) */
1205 /* Check the sample overflow count */
1207 /* Account sample overflows and, if a particular limit
1208 * is reached, extend the sampling buffer.
1209 * For details, see sfb_account_overflows().
1211 sampl_overflow
+= te
->overflow
;
1213 /* Timestamps are valid for full sample-data-blocks only */
1214 debug_sprintf_event(sfdbg
, 6, "hw_perf_event_update: sdbt=%p "
1215 "overflow=%llu timestamp=0x%llx\n",
1217 (te
->f
) ? trailer_timestamp(te
) : 0ULL);
1219 /* Collect all samples from a single sample-data-block and
1220 * flag if an (perf) event overflow happened. If so, the PMU
1221 * is stopped and remaining samples will be discarded.
1223 hw_collect_samples(event
, sdbt
, &event_overflow
);
1226 /* Reset trailer (using compare-double-and-swap) */
1228 te_flags
= te
->flags
& ~SDB_TE_BUFFER_FULL_MASK
;
1229 te_flags
|= SDB_TE_ALERT_REQ_MASK
;
1230 } while (!cmpxchg_double(&te
->flags
, &te
->overflow
,
1231 te
->flags
, te
->overflow
,
1234 /* Advance to next sample-data-block */
1236 if (is_link_entry(sdbt
))
1237 sdbt
= get_next_sdbt(sdbt
);
1239 /* Update event hardware registers */
1240 TEAR_REG(hwc
) = (unsigned long) sdbt
;
1242 /* Stop processing sample-data if all samples of the current
1243 * sample-data-block were flushed even if it was not full.
1245 if (flush_all
&& done
)
1248 /* If an event overflow happened, discard samples by
1249 * processing any remaining sample-data-blocks.
1255 /* Account sample overflows in the event hardware structure */
1257 OVERFLOW_REG(hwc
) = DIV_ROUND_UP(OVERFLOW_REG(hwc
) +
1258 sampl_overflow
, 1 + num_sdb
);
1259 if (sampl_overflow
|| event_overflow
)
1260 debug_sprintf_event(sfdbg
, 4, "hw_perf_event_update: "
1261 "overflow stats: sample=%llu event=%llu\n",
1262 sampl_overflow
, event_overflow
);
1265 #define AUX_SDB_INDEX(aux, i) ((i) % aux->sfb.num_sdb)
1266 #define AUX_SDB_NUM(aux, start, end) (end >= start ? end - start + 1 : 0)
1267 #define AUX_SDB_NUM_ALERT(aux) AUX_SDB_NUM(aux, aux->head, aux->alert_mark)
1268 #define AUX_SDB_NUM_EMPTY(aux) AUX_SDB_NUM(aux, aux->head, aux->empty_mark)
1271 * Get trailer entry by index of SDB.
1273 static struct hws_trailer_entry
*aux_sdb_trailer(struct aux_buffer
*aux
,
1274 unsigned long index
)
1278 index
= AUX_SDB_INDEX(aux
, index
);
1279 sdb
= aux
->sdb_index
[index
];
1280 return (struct hws_trailer_entry
*)trailer_entry_ptr(sdb
);
1284 * Finish sampling on the cpu. Called by cpumsf_pmu_del() with pmu
1285 * disabled. Collect the full SDBs in AUX buffer which have not reached
1286 * the point of alert indicator. And ignore the SDBs which are not
1289 * 1. Scan SDBs to see how much data is there and consume them.
1290 * 2. Remove alert indicator in the buffer.
1292 static void aux_output_end(struct perf_output_handle
*handle
)
1294 unsigned long i
, range_scan
, idx
;
1295 struct aux_buffer
*aux
;
1296 struct hws_trailer_entry
*te
;
1298 aux
= perf_get_aux(handle
);
1302 range_scan
= AUX_SDB_NUM_ALERT(aux
);
1303 for (i
= 0, idx
= aux
->head
; i
< range_scan
; i
++, idx
++) {
1304 te
= aux_sdb_trailer(aux
, idx
);
1305 if (!(te
->flags
& SDB_TE_BUFFER_FULL_MASK
))
1308 /* i is num of SDBs which are full */
1309 perf_aux_output_end(handle
, i
<< PAGE_SHIFT
);
1311 /* Remove alert indicators in the buffer */
1312 te
= aux_sdb_trailer(aux
, aux
->alert_mark
);
1313 te
->flags
&= ~SDB_TE_ALERT_REQ_MASK
;
1315 debug_sprintf_event(sfdbg
, 6, "aux_output_end: collect %lx SDBs\n", i
);
1319 * Start sampling on the CPU. Called by cpumsf_pmu_add() when an event
1320 * is first added to the CPU or rescheduled again to the CPU. It is called
1321 * with pmu disabled.
1323 * 1. Reset the trailer of SDBs to get ready for new data.
1324 * 2. Tell the hardware where to put the data by reset the SDBs buffer
1327 static int aux_output_begin(struct perf_output_handle
*handle
,
1328 struct aux_buffer
*aux
,
1329 struct cpu_hw_sf
*cpuhw
)
1331 unsigned long range
;
1332 unsigned long i
, range_scan
, idx
;
1333 unsigned long head
, base
, offset
;
1334 struct hws_trailer_entry
*te
;
1336 if (WARN_ON_ONCE(handle
->head
& ~PAGE_MASK
))
1339 aux
->head
= handle
->head
>> PAGE_SHIFT
;
1340 range
= (handle
->size
+ 1) >> PAGE_SHIFT
;
1345 * SDBs between aux->head and aux->empty_mark are already ready
1346 * for new data. range_scan is num of SDBs not within them.
1348 if (range
> AUX_SDB_NUM_EMPTY(aux
)) {
1349 range_scan
= range
- AUX_SDB_NUM_EMPTY(aux
);
1350 idx
= aux
->empty_mark
+ 1;
1351 for (i
= 0; i
< range_scan
; i
++, idx
++) {
1352 te
= aux_sdb_trailer(aux
, idx
);
1353 te
->flags
= te
->flags
& ~SDB_TE_BUFFER_FULL_MASK
;
1354 te
->flags
= te
->flags
& ~SDB_TE_ALERT_REQ_MASK
;
1357 /* Save the position of empty SDBs */
1358 aux
->empty_mark
= aux
->head
+ range
- 1;
1361 /* Set alert indicator */
1362 aux
->alert_mark
= aux
->head
+ range
/2 - 1;
1363 te
= aux_sdb_trailer(aux
, aux
->alert_mark
);
1364 te
->flags
= te
->flags
| SDB_TE_ALERT_REQ_MASK
;
1366 /* Reset hardware buffer head */
1367 head
= AUX_SDB_INDEX(aux
, aux
->head
);
1368 base
= aux
->sdbt_index
[head
/ CPUM_SF_SDB_PER_TABLE
];
1369 offset
= head
% CPUM_SF_SDB_PER_TABLE
;
1370 cpuhw
->lsctl
.tear
= base
+ offset
* sizeof(unsigned long);
1371 cpuhw
->lsctl
.dear
= aux
->sdb_index
[head
];
1373 debug_sprintf_event(sfdbg
, 6, "aux_output_begin: "
1374 "head->alert_mark->empty_mark (num_alert, range)"
1375 "[%lx -> %lx -> %lx] (%lx, %lx) "
1376 "tear index %lx, tear %lx dear %lx\n",
1377 aux
->head
, aux
->alert_mark
, aux
->empty_mark
,
1378 AUX_SDB_NUM_ALERT(aux
), range
,
1379 head
/ CPUM_SF_SDB_PER_TABLE
,
1387 * Set alert indicator on SDB at index @alert_index while sampler is running.
1389 * Return true if successfully.
1390 * Return false if full indicator is already set by hardware sampler.
1392 static bool aux_set_alert(struct aux_buffer
*aux
, unsigned long alert_index
,
1393 unsigned long long *overflow
)
1395 unsigned long long orig_overflow
, orig_flags
, new_flags
;
1396 struct hws_trailer_entry
*te
;
1398 te
= aux_sdb_trailer(aux
, alert_index
);
1400 orig_flags
= te
->flags
;
1401 orig_overflow
= te
->overflow
;
1402 *overflow
= orig_overflow
;
1403 if (orig_flags
& SDB_TE_BUFFER_FULL_MASK
) {
1405 * SDB is already set by hardware.
1406 * Abort and try to set somewhere
1411 new_flags
= orig_flags
| SDB_TE_ALERT_REQ_MASK
;
1412 } while (!cmpxchg_double(&te
->flags
, &te
->overflow
,
1413 orig_flags
, orig_overflow
,
1419 * aux_reset_buffer() - Scan and setup SDBs for new samples
1420 * @aux: The AUX buffer to set
1421 * @range: The range of SDBs to scan started from aux->head
1422 * @overflow: Set to overflow count
1424 * Set alert indicator on the SDB at index of aux->alert_mark. If this SDB is
1425 * marked as empty, check if it is already set full by the hardware sampler.
1426 * If yes, that means new data is already there before we can set an alert
1427 * indicator. Caller should try to set alert indicator to some position behind.
1429 * Scan the SDBs in AUX buffer from behind aux->empty_mark. They are used
1430 * previously and have already been consumed by user space. Reset these SDBs
1431 * (clear full indicator and alert indicator) for new data.
1432 * If aux->alert_mark fall in this area, just set it. Overflow count is
1433 * recorded while scanning.
1435 * SDBs between aux->head and aux->empty_mark are already reset at last time.
1436 * and ready for new samples. So scanning on this area could be skipped.
1438 * Return true if alert indicator is set successfully and false if not.
1440 static bool aux_reset_buffer(struct aux_buffer
*aux
, unsigned long range
,
1441 unsigned long long *overflow
)
1443 unsigned long long orig_overflow
, orig_flags
, new_flags
;
1444 unsigned long i
, range_scan
, idx
;
1445 struct hws_trailer_entry
*te
;
1447 if (range
<= AUX_SDB_NUM_EMPTY(aux
))
1449 * No need to scan. All SDBs in range are marked as empty.
1450 * Just set alert indicator. Should check race with hardware
1453 return aux_set_alert(aux
, aux
->alert_mark
, overflow
);
1455 if (aux
->alert_mark
<= aux
->empty_mark
)
1457 * Set alert indicator on empty SDB. Should check race
1458 * with hardware sampler.
1460 if (!aux_set_alert(aux
, aux
->alert_mark
, overflow
))
1464 * Scan the SDBs to clear full and alert indicator used previously.
1465 * Start scanning from one SDB behind empty_mark. If the new alert
1466 * indicator fall into this range, set it.
1468 range_scan
= range
- AUX_SDB_NUM_EMPTY(aux
);
1469 idx
= aux
->empty_mark
+ 1;
1470 for (i
= 0; i
< range_scan
; i
++, idx
++) {
1471 te
= aux_sdb_trailer(aux
, idx
);
1473 orig_flags
= te
->flags
;
1474 orig_overflow
= te
->overflow
;
1475 new_flags
= orig_flags
& ~SDB_TE_BUFFER_FULL_MASK
;
1476 if (idx
== aux
->alert_mark
)
1477 new_flags
|= SDB_TE_ALERT_REQ_MASK
;
1479 new_flags
&= ~SDB_TE_ALERT_REQ_MASK
;
1480 } while (!cmpxchg_double(&te
->flags
, &te
->overflow
,
1481 orig_flags
, orig_overflow
,
1483 *overflow
+= orig_overflow
;
1486 /* Update empty_mark to new position */
1487 aux
->empty_mark
= aux
->head
+ range
- 1;
1493 * Measurement alert handler for diagnostic mode sampling.
1495 static void hw_collect_aux(struct cpu_hw_sf
*cpuhw
)
1497 struct aux_buffer
*aux
;
1499 unsigned long range
= 0, size
;
1500 unsigned long long overflow
= 0;
1501 struct perf_output_handle
*handle
= &cpuhw
->handle
;
1502 unsigned long num_sdb
;
1504 aux
= perf_get_aux(handle
);
1505 if (WARN_ON_ONCE(!aux
))
1508 /* Inform user space new data arrived */
1509 size
= AUX_SDB_NUM_ALERT(aux
) << PAGE_SHIFT
;
1510 perf_aux_output_end(handle
, size
);
1511 num_sdb
= aux
->sfb
.num_sdb
;
1514 /* Get an output handle */
1515 aux
= perf_aux_output_begin(handle
, cpuhw
->event
);
1516 if (handle
->size
== 0) {
1517 pr_err("The AUX buffer with %lu pages for the "
1518 "diagnostic-sampling mode is full\n",
1520 debug_sprintf_event(sfdbg
, 1, "AUX buffer used up\n");
1523 if (WARN_ON_ONCE(!aux
))
1526 /* Update head and alert_mark to new position */
1527 aux
->head
= handle
->head
>> PAGE_SHIFT
;
1528 range
= (handle
->size
+ 1) >> PAGE_SHIFT
;
1530 aux
->alert_mark
= aux
->head
;
1532 aux
->alert_mark
= aux
->head
+ range
/2 - 1;
1534 if (aux_reset_buffer(aux
, range
, &overflow
)) {
1539 size
= range
<< PAGE_SHIFT
;
1540 perf_aux_output_end(&cpuhw
->handle
, size
);
1541 pr_err("Sample data caused the AUX buffer with %lu "
1542 "pages to overflow\n", num_sdb
);
1543 debug_sprintf_event(sfdbg
, 1, "head %lx range %lx "
1545 aux
->head
, range
, overflow
);
1547 size
= AUX_SDB_NUM_ALERT(aux
) << PAGE_SHIFT
;
1548 perf_aux_output_end(&cpuhw
->handle
, size
);
1549 debug_sprintf_event(sfdbg
, 6, "head %lx alert %lx "
1550 "already full, try another\n",
1551 aux
->head
, aux
->alert_mark
);
1556 debug_sprintf_event(sfdbg
, 6, "aux_reset_buffer: "
1557 "[%lx -> %lx -> %lx] (%lx, %lx)\n",
1558 aux
->head
, aux
->alert_mark
, aux
->empty_mark
,
1559 AUX_SDB_NUM_ALERT(aux
), range
);
1563 * Callback when freeing AUX buffers.
1565 static void aux_buffer_free(void *data
)
1567 struct aux_buffer
*aux
= data
;
1568 unsigned long i
, num_sdbt
;
1573 /* Free SDBT. SDB is freed by the caller */
1574 num_sdbt
= aux
->sfb
.num_sdbt
;
1575 for (i
= 0; i
< num_sdbt
; i
++)
1576 free_page(aux
->sdbt_index
[i
]);
1578 kfree(aux
->sdbt_index
);
1579 kfree(aux
->sdb_index
);
1582 debug_sprintf_event(sfdbg
, 4, "aux_buffer_free: free "
1583 "%lu SDBTs\n", num_sdbt
);
1587 * aux_buffer_setup() - Setup AUX buffer for diagnostic mode sampling
1588 * @cpu: On which to allocate, -1 means current
1589 * @pages: Array of pointers to buffer pages passed from perf core
1590 * @nr_pages: Total pages
1591 * @snapshot: Flag for snapshot mode
1593 * This is the callback when setup an event using AUX buffer. Perf tool can
1594 * trigger this by an additional mmap() call on the event. Unlike the buffer
1595 * for basic samples, AUX buffer belongs to the event. It is scheduled with
1596 * the task among online cpus when it is a per-thread event.
1598 * Return the private AUX buffer structure if success or NULL if fails.
1600 static void *aux_buffer_setup(int cpu
, void **pages
, int nr_pages
,
1603 struct sf_buffer
*sfb
;
1604 struct aux_buffer
*aux
;
1605 unsigned long *new, *tail
;
1608 if (!nr_pages
|| !pages
)
1611 if (nr_pages
> CPUM_SF_MAX_SDB
* CPUM_SF_SDB_DIAG_FACTOR
) {
1612 pr_err("AUX buffer size (%i pages) is larger than the "
1613 "maximum sampling buffer limit\n",
1616 } else if (nr_pages
< CPUM_SF_MIN_SDB
* CPUM_SF_SDB_DIAG_FACTOR
) {
1617 pr_err("AUX buffer size (%i pages) is less than the "
1618 "minimum sampling buffer limit\n",
1623 /* Allocate aux_buffer struct for the event */
1624 aux
= kmalloc(sizeof(struct aux_buffer
), GFP_KERNEL
);
1629 /* Allocate sdbt_index for fast reference */
1630 n_sdbt
= (nr_pages
+ CPUM_SF_SDB_PER_TABLE
- 1) / CPUM_SF_SDB_PER_TABLE
;
1631 aux
->sdbt_index
= kmalloc_array(n_sdbt
, sizeof(void *), GFP_KERNEL
);
1632 if (!aux
->sdbt_index
)
1635 /* Allocate sdb_index for fast reference */
1636 aux
->sdb_index
= kmalloc_array(nr_pages
, sizeof(void *), GFP_KERNEL
);
1637 if (!aux
->sdb_index
)
1640 /* Allocate the first SDBT */
1642 sfb
->sdbt
= (unsigned long *) get_zeroed_page(GFP_KERNEL
);
1645 aux
->sdbt_index
[sfb
->num_sdbt
++] = (unsigned long)sfb
->sdbt
;
1646 tail
= sfb
->tail
= sfb
->sdbt
;
1649 * Link the provided pages of AUX buffer to SDBT.
1650 * Allocate SDBT if needed.
1652 for (i
= 0; i
< nr_pages
; i
++, tail
++) {
1653 if (require_table_link(tail
)) {
1654 new = (unsigned long *) get_zeroed_page(GFP_KERNEL
);
1657 aux
->sdbt_index
[sfb
->num_sdbt
++] = (unsigned long)new;
1658 /* Link current page to tail of chain */
1659 *tail
= (unsigned long)(void *) new + 1;
1662 /* Tail is the entry in a SDBT */
1663 *tail
= (unsigned long)pages
[i
];
1664 aux
->sdb_index
[i
] = (unsigned long)pages
[i
];
1666 sfb
->num_sdb
= nr_pages
;
1668 /* Link the last entry in the SDBT to the first SDBT */
1669 *tail
= (unsigned long) sfb
->sdbt
+ 1;
1673 * Initial all SDBs are zeroed. Mark it as empty.
1674 * So there is no need to clear the full indicator
1675 * when this event is first added.
1677 aux
->empty_mark
= sfb
->num_sdb
- 1;
1679 debug_sprintf_event(sfdbg
, 4, "aux_buffer_setup: setup %lu SDBTs"
1681 sfb
->num_sdbt
, sfb
->num_sdb
);
1686 /* SDBs (AUX buffer pages) are freed by caller */
1687 for (i
= 0; i
< sfb
->num_sdbt
; i
++)
1688 free_page(aux
->sdbt_index
[i
]);
1689 kfree(aux
->sdb_index
);
1691 kfree(aux
->sdbt_index
);
1698 static void cpumsf_pmu_read(struct perf_event
*event
)
1700 /* Nothing to do ... updates are interrupt-driven */
1703 /* Activate sampling control.
1704 * Next call of pmu_enable() starts sampling.
1706 static void cpumsf_pmu_start(struct perf_event
*event
, int flags
)
1708 struct cpu_hw_sf
*cpuhw
= this_cpu_ptr(&cpu_hw_sf
);
1710 if (WARN_ON_ONCE(!(event
->hw
.state
& PERF_HES_STOPPED
)))
1713 if (flags
& PERF_EF_RELOAD
)
1714 WARN_ON_ONCE(!(event
->hw
.state
& PERF_HES_UPTODATE
));
1716 perf_pmu_disable(event
->pmu
);
1717 event
->hw
.state
= 0;
1718 cpuhw
->lsctl
.cs
= 1;
1719 if (SAMPL_DIAG_MODE(&event
->hw
))
1720 cpuhw
->lsctl
.cd
= 1;
1721 perf_pmu_enable(event
->pmu
);
1724 /* Deactivate sampling control.
1725 * Next call of pmu_enable() stops sampling.
1727 static void cpumsf_pmu_stop(struct perf_event
*event
, int flags
)
1729 struct cpu_hw_sf
*cpuhw
= this_cpu_ptr(&cpu_hw_sf
);
1731 if (event
->hw
.state
& PERF_HES_STOPPED
)
1734 perf_pmu_disable(event
->pmu
);
1735 cpuhw
->lsctl
.cs
= 0;
1736 cpuhw
->lsctl
.cd
= 0;
1737 event
->hw
.state
|= PERF_HES_STOPPED
;
1739 if ((flags
& PERF_EF_UPDATE
) && !(event
->hw
.state
& PERF_HES_UPTODATE
)) {
1740 hw_perf_event_update(event
, 1);
1741 event
->hw
.state
|= PERF_HES_UPTODATE
;
1743 perf_pmu_enable(event
->pmu
);
1746 static int cpumsf_pmu_add(struct perf_event
*event
, int flags
)
1748 struct cpu_hw_sf
*cpuhw
= this_cpu_ptr(&cpu_hw_sf
);
1749 struct aux_buffer
*aux
;
1752 if (cpuhw
->flags
& PMU_F_IN_USE
)
1755 if (!SAMPL_DIAG_MODE(&event
->hw
) && !cpuhw
->sfb
.sdbt
)
1759 perf_pmu_disable(event
->pmu
);
1761 event
->hw
.state
= PERF_HES_UPTODATE
| PERF_HES_STOPPED
;
1763 /* Set up sampling controls. Always program the sampling register
1764 * using the SDB-table start. Reset TEAR_REG event hardware register
1765 * that is used by hw_perf_event_update() to store the sampling buffer
1766 * position after samples have been flushed.
1770 cpuhw
->lsctl
.interval
= SAMPL_RATE(&event
->hw
);
1771 if (!SAMPL_DIAG_MODE(&event
->hw
)) {
1772 cpuhw
->lsctl
.tear
= (unsigned long) cpuhw
->sfb
.sdbt
;
1773 cpuhw
->lsctl
.dear
= *(unsigned long *) cpuhw
->sfb
.sdbt
;
1774 hw_reset_registers(&event
->hw
, cpuhw
->sfb
.sdbt
);
1777 /* Ensure sampling functions are in the disabled state. If disabled,
1778 * switch on sampling enable control. */
1779 if (WARN_ON_ONCE(cpuhw
->lsctl
.es
== 1 || cpuhw
->lsctl
.ed
== 1)) {
1783 if (SAMPL_DIAG_MODE(&event
->hw
)) {
1784 aux
= perf_aux_output_begin(&cpuhw
->handle
, event
);
1789 err
= aux_output_begin(&cpuhw
->handle
, aux
, cpuhw
);
1792 cpuhw
->lsctl
.ed
= 1;
1794 cpuhw
->lsctl
.es
= 1;
1796 /* Set in_use flag and store event */
1797 cpuhw
->event
= event
;
1798 cpuhw
->flags
|= PMU_F_IN_USE
;
1800 if (flags
& PERF_EF_START
)
1801 cpumsf_pmu_start(event
, PERF_EF_RELOAD
);
1803 perf_event_update_userpage(event
);
1804 perf_pmu_enable(event
->pmu
);
1808 static void cpumsf_pmu_del(struct perf_event
*event
, int flags
)
1810 struct cpu_hw_sf
*cpuhw
= this_cpu_ptr(&cpu_hw_sf
);
1812 perf_pmu_disable(event
->pmu
);
1813 cpumsf_pmu_stop(event
, PERF_EF_UPDATE
);
1815 cpuhw
->lsctl
.es
= 0;
1816 cpuhw
->lsctl
.ed
= 0;
1817 cpuhw
->flags
&= ~PMU_F_IN_USE
;
1818 cpuhw
->event
= NULL
;
1820 if (SAMPL_DIAG_MODE(&event
->hw
))
1821 aux_output_end(&cpuhw
->handle
);
1822 perf_event_update_userpage(event
);
1823 perf_pmu_enable(event
->pmu
);
1826 CPUMF_EVENT_ATTR(SF
, SF_CYCLES_BASIC
, PERF_EVENT_CPUM_SF
);
1827 CPUMF_EVENT_ATTR(SF
, SF_CYCLES_BASIC_DIAG
, PERF_EVENT_CPUM_SF_DIAG
);
1829 static struct attribute
*cpumsf_pmu_events_attr
[] = {
1830 CPUMF_EVENT_PTR(SF
, SF_CYCLES_BASIC
),
1835 PMU_FORMAT_ATTR(event
, "config:0-63");
1837 static struct attribute
*cpumsf_pmu_format_attr
[] = {
1838 &format_attr_event
.attr
,
1842 static struct attribute_group cpumsf_pmu_events_group
= {
1844 .attrs
= cpumsf_pmu_events_attr
,
1846 static struct attribute_group cpumsf_pmu_format_group
= {
1848 .attrs
= cpumsf_pmu_format_attr
,
1850 static const struct attribute_group
*cpumsf_pmu_attr_groups
[] = {
1851 &cpumsf_pmu_events_group
,
1852 &cpumsf_pmu_format_group
,
1856 static struct pmu cpumf_sampling
= {
1857 .pmu_enable
= cpumsf_pmu_enable
,
1858 .pmu_disable
= cpumsf_pmu_disable
,
1860 .event_init
= cpumsf_pmu_event_init
,
1861 .add
= cpumsf_pmu_add
,
1862 .del
= cpumsf_pmu_del
,
1864 .start
= cpumsf_pmu_start
,
1865 .stop
= cpumsf_pmu_stop
,
1866 .read
= cpumsf_pmu_read
,
1868 .attr_groups
= cpumsf_pmu_attr_groups
,
1870 .setup_aux
= aux_buffer_setup
,
1871 .free_aux
= aux_buffer_free
,
1874 static void cpumf_measurement_alert(struct ext_code ext_code
,
1875 unsigned int alert
, unsigned long unused
)
1877 struct cpu_hw_sf
*cpuhw
;
1879 if (!(alert
& CPU_MF_INT_SF_MASK
))
1881 inc_irq_stat(IRQEXT_CMS
);
1882 cpuhw
= this_cpu_ptr(&cpu_hw_sf
);
1884 /* Measurement alerts are shared and might happen when the PMU
1885 * is not reserved. Ignore these alerts in this case. */
1886 if (!(cpuhw
->flags
& PMU_F_RESERVED
))
1889 /* The processing below must take care of multiple alert events that
1890 * might be indicated concurrently. */
1892 /* Program alert request */
1893 if (alert
& CPU_MF_INT_SF_PRA
) {
1894 if (cpuhw
->flags
& PMU_F_IN_USE
)
1895 if (SAMPL_DIAG_MODE(&cpuhw
->event
->hw
))
1896 hw_collect_aux(cpuhw
);
1898 hw_perf_event_update(cpuhw
->event
, 0);
1900 WARN_ON_ONCE(!(cpuhw
->flags
& PMU_F_IN_USE
));
1903 /* Report measurement alerts only for non-PRA codes */
1904 if (alert
!= CPU_MF_INT_SF_PRA
)
1905 debug_sprintf_event(sfdbg
, 6, "measurement alert: 0x%x\n", alert
);
1907 /* Sampling authorization change request */
1908 if (alert
& CPU_MF_INT_SF_SACA
)
1911 /* Loss of sample data due to high-priority machine activities */
1912 if (alert
& CPU_MF_INT_SF_LSDA
) {
1913 pr_err("Sample data was lost\n");
1914 cpuhw
->flags
|= PMU_F_ERR_LSDA
;
1918 /* Invalid sampling buffer entry */
1919 if (alert
& (CPU_MF_INT_SF_IAE
|CPU_MF_INT_SF_ISE
)) {
1920 pr_err("A sampling buffer entry is incorrect (alert=0x%x)\n",
1922 cpuhw
->flags
|= PMU_F_ERR_IBE
;
1926 static int cpusf_pmu_setup(unsigned int cpu
, int flags
)
1928 /* Ignore the notification if no events are scheduled on the PMU.
1929 * This might be racy...
1931 if (!atomic_read(&num_events
))
1934 local_irq_disable();
1935 setup_pmc_cpu(&flags
);
1940 static int s390_pmu_sf_online_cpu(unsigned int cpu
)
1942 return cpusf_pmu_setup(cpu
, PMC_INIT
);
1945 static int s390_pmu_sf_offline_cpu(unsigned int cpu
)
1947 return cpusf_pmu_setup(cpu
, PMC_RELEASE
);
1950 static int param_get_sfb_size(char *buffer
, const struct kernel_param
*kp
)
1952 if (!cpum_sf_avail())
1954 return sprintf(buffer
, "%lu,%lu", CPUM_SF_MIN_SDB
, CPUM_SF_MAX_SDB
);
1957 static int param_set_sfb_size(const char *val
, const struct kernel_param
*kp
)
1960 unsigned long min
, max
;
1962 if (!cpum_sf_avail())
1964 if (!val
|| !strlen(val
))
1967 /* Valid parameter values: "min,max" or "max" */
1968 min
= CPUM_SF_MIN_SDB
;
1969 max
= CPUM_SF_MAX_SDB
;
1970 if (strchr(val
, ','))
1971 rc
= (sscanf(val
, "%lu,%lu", &min
, &max
) == 2) ? 0 : -EINVAL
;
1973 rc
= kstrtoul(val
, 10, &max
);
1975 if (min
< 2 || min
>= max
|| max
> get_num_physpages())
1980 sfb_set_limits(min
, max
);
1981 pr_info("The sampling buffer limits have changed to: "
1982 "min=%lu max=%lu (diag=x%lu)\n",
1983 CPUM_SF_MIN_SDB
, CPUM_SF_MAX_SDB
, CPUM_SF_SDB_DIAG_FACTOR
);
1987 #define param_check_sfb_size(name, p) __param_check(name, p, void)
1988 static const struct kernel_param_ops param_ops_sfb_size
= {
1989 .set
= param_set_sfb_size
,
1990 .get
= param_get_sfb_size
,
1993 #define RS_INIT_FAILURE_QSI 0x0001
1994 #define RS_INIT_FAILURE_BSDES 0x0002
1995 #define RS_INIT_FAILURE_ALRT 0x0003
1996 #define RS_INIT_FAILURE_PERF 0x0004
1997 static void __init
pr_cpumsf_err(unsigned int reason
)
1999 pr_err("Sampling facility support for perf is not available: "
2000 "reason=%04x\n", reason
);
2003 static int __init
init_cpum_sampling_pmu(void)
2005 struct hws_qsi_info_block si
;
2008 if (!cpum_sf_avail())
2011 memset(&si
, 0, sizeof(si
));
2013 pr_cpumsf_err(RS_INIT_FAILURE_QSI
);
2017 if (!si
.as
&& !si
.ad
)
2020 if (si
.bsdes
!= sizeof(struct hws_basic_entry
)) {
2021 pr_cpumsf_err(RS_INIT_FAILURE_BSDES
);
2026 sfb_set_limits(CPUM_SF_MIN_SDB
, CPUM_SF_MAX_SDB
);
2027 cpumsf_pmu_events_attr
[1] =
2028 CPUMF_EVENT_PTR(SF
, SF_CYCLES_BASIC_DIAG
);
2031 sfdbg
= debug_register(KMSG_COMPONENT
, 2, 1, 80);
2033 pr_err("Registering for s390dbf failed\n");
2034 debug_register_view(sfdbg
, &debug_sprintf_view
);
2036 err
= register_external_irq(EXT_IRQ_MEASURE_ALERT
,
2037 cpumf_measurement_alert
);
2039 pr_cpumsf_err(RS_INIT_FAILURE_ALRT
);
2043 err
= perf_pmu_register(&cpumf_sampling
, "cpum_sf", PERF_TYPE_RAW
);
2045 pr_cpumsf_err(RS_INIT_FAILURE_PERF
);
2046 unregister_external_irq(EXT_IRQ_MEASURE_ALERT
,
2047 cpumf_measurement_alert
);
2051 cpuhp_setup_state(CPUHP_AP_PERF_S390_SF_ONLINE
, "perf/s390/sf:online",
2052 s390_pmu_sf_online_cpu
, s390_pmu_sf_offline_cpu
);
2056 arch_initcall(init_cpum_sampling_pmu
);
2057 core_param(cpum_sfb_size
, CPUM_SF_MAX_SDB
, sfb_size
, 0640);