Commit 39d62336 authored by Thomas Richter's avatar Thomas Richter Committed by Heiko Carstens
Browse files

s390/pai: add support for cryptography counters



PMU device driver perf_pai_crypto supports Processor Activity
Instrumentation (PAI), available with IBM z16:
- maps a full page to lowcore address 0x1500.
- uses CR0 bit 13 to turn PAI crypto counting on and off.
- creates a sample with raw data on each context switch out when
  at context switch some mapped counters have a value of nonzero.
This device driver only supports CPU wide context, no task context
is allowed.

Support for counting:
- one or more counters can be specified using
  perf stat -e pai_crypto/xxx/
  where xxx stands for the counter event name. Multiple invocation
  of this command is possible. The counter names are listed in
  /sys/devices/pai_crypto/events directory.
- one special counters can be specified using
  perf stat -e pai_crypto/CRYPTO_ALL/
  which returns the sum of all incremented crypto counters.
- one event pai_crypto/CRYPTO_ALL/ is reserved for sampling.
  No multiple invocations are possible. The event collects data at
  context switch out and saves them in the ring buffer.

Add qpaci assembly instruction to query supported memory mapped crypto
counters. It returns the number of counters (no holes allowed in that
range).

The PAI crypto counter events are system wide and can not be executed
in parallel. Therefore some restrictions documented in function
paicrypt_busy apply.
In particular event CRYPTO_ALL for sampling must run exclusive.
Only counting events can run in parallel.

PAI crypto counter events can not be created when a CPU hot plug
add is processed. This means a CPU hot plug add does not get
the necessary PAI event to record PAI cryptography counter increments
on the newly added CPU. CPU hot plug remove removes the event and
terminates the counting of PAI counters immediately.

Co-developed-by: default avatarSven Schnelle <svens@linux.ibm.com>
Signed-off-by: default avatarSven Schnelle <svens@linux.ibm.com>
Reviewed-by: default avatarJuergen Christ <jchrist@linux.ibm.com>
Signed-off-by: default avatarThomas Richter <tmricht@linux.ibm.com>
Link: https://lore.kernel.org/r/20220504062351.2954280-3-tmricht@linux.ibm.com


Signed-off-by: default avatarHeiko Carstens <hca@linux.ibm.com>
parent 6d97af48
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -93,7 +93,9 @@ union ctlreg0 {
		unsigned long tcx  : 1;	/* Transactional-Execution control */
		unsigned long pifo : 1;	/* Transactional-Execution Program-
					   Interruption-Filtering Override */
		unsigned long	   : 22;
		unsigned long	   : 3;
		unsigned long ccc  : 1; /* Cryptography counter control */
		unsigned long	   : 18;
		unsigned long	   : 3;
		unsigned long lap  : 1; /* Low-address-protection control */
		unsigned long	   : 4;
+7 −3
Original line number Diff line number Diff line
@@ -9,19 +9,21 @@
#include <linux/uaccess.h>
#include <asm/timex.h>
#include <asm/fpu/api.h>
#include <asm/pai.h>

#define ARCH_EXIT_TO_USER_MODE_WORK (_TIF_GUARDED_STORAGE | _TIF_PER_TRAP)

void do_per_trap(struct pt_regs *regs);

#ifdef CONFIG_DEBUG_ENTRY
static __always_inline void arch_enter_from_user_mode(struct pt_regs *regs)
{
	if (IS_ENABLED(CONFIG_DEBUG_ENTRY))
		debug_user_asce(0);

	pai_kernel_enter(regs);
}

#define arch_enter_from_user_mode arch_enter_from_user_mode
#endif /* CONFIG_DEBUG_ENTRY */

static __always_inline void arch_exit_to_user_mode_work(struct pt_regs *regs,
							unsigned long ti_work)
@@ -44,6 +46,8 @@ static __always_inline void arch_exit_to_user_mode(void)

	if (IS_ENABLED(CONFIG_DEBUG_ENTRY))
		debug_user_asce(1);

	pai_kernel_exit(current_pt_regs());
}

#define arch_exit_to_user_mode arch_exit_to_user_mode
+4 −1
Original line number Diff line number Diff line
@@ -200,7 +200,10 @@ struct lowcore {
	__u64	last_break_save_area;		/* 0x1338 */
	__u32	access_regs_save_area[16];	/* 0x1340 */
	__u64	cregs_save_area[16];		/* 0x1380 */
	__u8	pad_0x1400[0x1800-0x1400];	/* 0x1400 */
	__u8	pad_0x1400[0x1500-0x1400];	/* 0x1400 */
	/* Cryptography-counter designation */
	__u64	ccd;				/* 0x1500 */
	__u8	pad_0x1508[0x1800-0x1508];	/* 0x1508 */

	/* Transaction abort diagnostic block */
	struct pgm_tdb pgm_tdb;			/* 0x1800 */
+1 −1
Original line number Diff line number Diff line
@@ -101,7 +101,7 @@ void nmi_alloc_mcesa_early(u64 *mcesad);
int nmi_alloc_mcesa(u64 *mcesad);
void nmi_free_mcesa(u64 *mcesad);

void s390_handle_mcck(void);
void s390_handle_mcck(struct pt_regs *regs);
void __s390_handle_mcck(void);
int s390_do_machine_check(struct pt_regs *regs);

+74 −0
Original line number Diff line number Diff line
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Processor Activity Instrumentation support for cryptography counters
 *
 *  Copyright IBM Corp. 2022
 *  Author(s): Thomas Richter <tmricht@linux.ibm.com>
 */
#ifndef _ASM_S390_PAI_H
#define _ASM_S390_PAI_H

#include <linux/jump_label.h>
#include <asm/lowcore.h>
#include <asm/ptrace.h>

struct qpaci_info_block {
	u64 header;
	struct {
		u64 : 8;
		u64 num_cc : 8;	/* # of supported crypto counters */
		u64 : 48;
	};
};

static inline int qpaci(struct qpaci_info_block *info)
{
	/* Size of info (in double words minus one) */
	size_t size = sizeof(*info) / sizeof(u64) - 1;
	int cc;

	asm volatile(
		"	lgr	0,%[size]\n"
		"	.insn	s,0xb28f0000,%[info]\n"
		"	lgr	%[size],0\n"
		"	ipm	%[cc]\n"
		"	srl	%[cc],28\n"
		: [cc] "=d" (cc), [info] "=Q" (*info), [size] "+&d" (size)
		:
		: "0", "cc", "memory");
	return cc ? (size + 1) * sizeof(u64) : 0;
}

#define PAI_CRYPTO_BASE			0x1000	/* First event number */
#define PAI_CRYPTO_MAXCTR		256	/* Max # of event counters */
#define PAI_CRYPTO_KERNEL_OFFSET	2048

DECLARE_STATIC_KEY_FALSE(pai_key);

static __always_inline void pai_kernel_enter(struct pt_regs *regs)
{
	if (!IS_ENABLED(CONFIG_PERF_EVENTS))
		return;
	if (!static_branch_unlikely(&pai_key))
		return;
	if (!S390_lowcore.ccd)
		return;
	if (!user_mode(regs))
		return;
	WRITE_ONCE(S390_lowcore.ccd, S390_lowcore.ccd | PAI_CRYPTO_KERNEL_OFFSET);
}

static __always_inline void pai_kernel_exit(struct pt_regs *regs)
{
	if (!IS_ENABLED(CONFIG_PERF_EVENTS))
		return;
	if (!static_branch_unlikely(&pai_key))
		return;
	if (!S390_lowcore.ccd)
		return;
	if (!user_mode(regs))
		return;
	WRITE_ONCE(S390_lowcore.ccd, S390_lowcore.ccd & ~PAI_CRYPTO_KERNEL_OFFSET);
}

#endif
Loading