Skip to contentSkip to conversation

s390/pai: Add support for Processor Activity Instrumentation

PATCH Applied Thomas Richter 5 participants 10 messages May 4, 2022 – May 9, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover s390/pai: Add support for Processor Activity Instrumentation

This patch series adds support for Processor Activity Instrumentation (PAI) for cryptography instructions. A new Performance Measurement Unit (PMU) is introduced. It reads memory mapped counter values incremented when a CPU executes certain instructions.
To differentiate between user space and kernel space counter increments, the memory mapping is changed on system call entry/exit and IRQ entry/exit. Patch 1 prepares that transition, patch 2 introduces the new PMU and kernel vs user space memory mappings.

Sven Schnelle (1):
entry: Rename arch_check_user_regs() to arch_enter_from_user_mode()

Thomas Richter (1):
s390/pai: Add support for cryptography counters

 arch/s390/include/asm/ctl_reg.h      |   4 +-
 arch/s390/include/asm/entry-common.h |  14 +-
 arch/s390/include/asm/lowcore.h      |   5 +-
 arch/s390/include/asm/nmi.h          |   2 +-
 arch/s390/include/asm/pai.h          |  74 +++
 arch/s390/kernel/Makefile            |   1 +
 arch/s390/kernel/entry.S             |   1 +
 arch/s390/kernel/nmi.c               |   6 +-
 arch/s390/kernel/perf_pai_crypto.c   | 688 +++++++++++++++++++++++++++
 arch/x86/include/asm/entry-common.h  |   4 +-
 include/linux/entry-common.h         |   8 +-
 kernel/entry/common.c                |   2 +-
 12 files changed, 793 insertions(+), 16 deletions(-)
 create mode 100644 arch/s390/include/asm/pai.h
 create mode 100644 arch/s390/kernel/perf_pai_crypto.c
Signature · 1 line
2.32.0
#1 lore raw

PATCH 1/2 entry: Rename arch_check_user_regs() to arch_enter_from_user_mode()

From: Sven Schnelle <[email protected]>

arch_check_user_regs() is used at the moment to verify that struct pt_regs contains valid values when entering the kernel from userspace. s390 needs a place in the generic entry code to modify a cpu data structure when switching from userspace to kernel mode. As arch_check_user_regs() is exactly this, rename it to arch_enter_from_user_mode().

Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Andy Lutomirski <[email protected]>
---
 arch/s390/include/asm/entry-common.h | 4 ++--
 arch/x86/include/asm/entry-common.h  | 4 ++--
 include/linux/entry-common.h         | 8 ++++----
 kernel/entry/common.c                | 2 +-
 4 files changed, 9 insertions(+), 9 deletions(-)
arch/s390/include/asm/entry-common.h +2 -2
index 2f0a1cacdf85..99d654ccd3db 100644
@@ -15,12 +15,12 @@
 void do_per_trap(struct pt_regs *regs);
 
 #ifdef CONFIG_DEBUG_ENTRY
-static __always_inline void arch_check_user_regs(struct pt_regs *regs)
+static __always_inline void arch_enter_from_user_mode(struct pt_regs *regs)
 {
 	debug_user_asce(0);
 }
 
-#define arch_check_user_regs arch_check_user_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,
arch/x86/include/asm/entry-common.h +2 -2
index 43184640b579..674ed46d3ced 100644
@@ -10,7 +10,7 @@
 #include <asm/fpu/api.h>
 
 /* Check that the stack and regs on entry from user mode are sane. */
-static __always_inline void arch_check_user_regs(struct pt_regs *regs)
+static __always_inline void arch_enter_from_user_mode(struct pt_regs *regs)
 {
 	if (IS_ENABLED(CONFIG_DEBUG_ENTRY)) {
 		/*
@@ -42,7 +42,7 @@ static __always_inline void arch_check_user_regs(struct pt_regs *regs)
 		WARN_ON_ONCE(regs != task_pt_regs(current));
 	}
 }
-#define arch_check_user_regs arch_check_user_regs
+#define arch_enter_from_user_mode arch_enter_from_user_mode
 
 static inline void arch_exit_to_user_mode_prepare(struct pt_regs *regs,
 						  unsigned long ti_work)
include/linux/entry-common.h +4 -4
index ab78bd4c2eb0..c92ac75d6556 100644
@@ -63,7 +63,7 @@
 	 ARCH_EXIT_TO_USER_MODE_WORK)
 
 /**
- * arch_check_user_regs - Architecture specific sanity check for user mode regs
+ * arch_enter_from_user_mode - Architecture specific sanity check for user mode regs
  * @regs:	Pointer to currents pt_regs
  *
  * Defaults to an empty implementation. Can be replaced by architecture
@@ -73,10 +73,10 @@
  * section. Use __always_inline so the compiler cannot push it out of line
  * and make it instrumentable.
  */
-static __always_inline void arch_check_user_regs(struct pt_regs *regs);
+static __always_inline void arch_enter_from_user_mode(struct pt_regs *regs);
 
-#ifndef arch_check_user_regs
-static __always_inline void arch_check_user_regs(struct pt_regs *regs) {}
+#ifndef arch_enter_from_user_mode
+static __always_inline void arch_enter_from_user_mode(struct pt_regs *regs) {}
 #endif
 
 /**
kernel/entry/common.c +1 -1
index 93c3b86e781c..9e63923c5a0f 100644
@@ -17,7 +17,7 @@
 /* See comment for enter_from_user_mode() in entry-common.h */
 static __always_inline void __enter_from_user_mode(struct pt_regs *regs)
 {
-	arch_check_user_regs(regs);
+	arch_enter_from_user_mode(regs);
 	lockdep_hardirqs_off(CALLER_ADDR0);
 
 	CT_WARN_ON(ct_state() != CONTEXT_USER);
Signature · 1 line
2.32.0
maintainer Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] entry: Rename arch_check_user_regs() to arch_enter_from_user_mode()

Quoted 8 lines · Thomas Richter

From: Sven Schnelle <[email protected]>

arch_check_user_regs() is used at the moment to verify that struct pt_regs contains valid values when entering the kernel from userspace. s390 needs a place in the generic entry code to modify a cpu data structure when switching from userspace to kernel mode. As arch_check_user_regs() is exactly this, rename it to arch_enter_from_user_mode().

Quoted 5 lines
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Andy Lutomirski <[email protected]>

---

With the note that NMI doesn't (necessarily) call this..

reviewer
#3 lore raw

Re: [PATCH 1/2] entry: Rename arch_check_user_regs() to arch_enter_from_user_mode()

Quoted 15 lines · Peter Zijlstra
Quoted 8 lines · Thomas Richter

From: Sven Schnelle <[email protected]>

arch_check_user_regs() is used at the moment to verify that struct pt_regs contains valid values when entering the kernel from userspace. s390 needs a place in the generic entry code to modify a cpu data structure when switching from userspace to kernel mode. As arch_check_user_regs() is exactly this, rename it to arch_enter_from_user_mode().

Quoted 5 lines
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Andy Lutomirski <[email protected]>

---

Quoted 3 lines

With the note that NMI doesn't (necessarily) call this..

How about:

"When entering the kernel from userspace, arch_check_user_regs() is
used to verify that struct pt_regs contains valid values. Note that the NMI codepath doesn't call this function. s390 needs a place in the generic entry code to modify a cpu data structure when switching from userspace to kernel mode. As arch_check_user_regs() is exactly this,

 rename it to arch_enter_from_user_mode()."
maintainer
#4 lore raw

Re: [PATCH 1/2] entry: Rename arch_check_user_regs() to arch_enter_from_user_mode()

Quoted 21 lines · Sven Schnelle
Quoted 15 lines · Peter Zijlstra
Quoted 8 lines · Thomas Richter

From: Sven Schnelle <[email protected]>

arch_check_user_regs() is used at the moment to verify that struct pt_regs contains valid values when entering the kernel from userspace. s390 needs a place in the generic entry code to modify a cpu data structure when switching from userspace to kernel mode. As arch_check_user_regs() is exactly this, rename it to arch_enter_from_user_mode().

Quoted 5 lines
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Andy Lutomirski <[email protected]>

---

Quoted 3 lines

With the note that NMI doesn't (necessarily) call this..

Quoted 8 lines

How about:

"When entering the kernel from userspace, arch_check_user_regs() is
used to verify that struct pt_regs contains valid values. Note that the NMI codepath doesn't call this function. s390 needs a place in the generic entry code to modify a cpu data structure when switching from userspace to kernel mode. As arch_check_user_regs() is exactly this,

 rename it to arch_enter_from_user_mode()."

Sure, thanks!

maintainer Applied
#5 lore raw

Re: [PATCH 1/2] entry: Rename arch_check_user_regs() to arch_enter_from_user_mode()

Quoted 24 lines · Peter Zijlstra
Quoted 23 lines · Sven Schnelle
Quoted 15 lines · Peter Zijlstra
Quoted 8 lines · Thomas Richter

From: Sven Schnelle <[email protected]>

arch_check_user_regs() is used at the moment to verify that struct pt_regs contains valid values when entering the kernel from userspace. s390 needs a place in the generic entry code to modify a cpu data structure when switching from userspace to kernel mode. As arch_check_user_regs() is exactly this, rename it to arch_enter_from_user_mode().

Quoted 5 lines
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Andy Lutomirski <[email protected]>

---

Quoted 3 lines

With the note that NMI doesn't (necessarily) call this..

How about:

Quoted 8 lines
Quoted 6 lines

"When entering the kernel from userspace, arch_check_user_regs() is
used to verify that struct pt_regs contains valid values. Note that the NMI codepath doesn't call this function. s390 needs a place in the generic entry code to modify a cpu data structure when switching from userspace to kernel mode. As arch_check_user_regs() is exactly this,

 rename it to arch_enter_from_user_mode()."

Sure, thanks!

Merged into commit message, and applied both patches to s390 tree.

Thanks!

Reviewed-by on v1 1/2
#6 lore raw

Re: [PATCH 1/2] entry: Rename arch_check_user_regs() to arch_enter_from_user_mode()

In reply to Thomas Richter
Quoted 8 lines · Thomas Richter

From: Sven Schnelle <[email protected]>

arch_check_user_regs() is used at the moment to verify that struct pt_regs contains valid values when entering the kernel from userspace. s390 needs a place in the generic entry code to modify a cpu data structure when switching from userspace to kernel mode. As arch_check_user_regs() is exactly this, rename it to arch_enter_from_user_mode().

Quoted 4 lines
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Andy Lutomirski <[email protected]>
maintainer
#7 lore raw

Re: [PATCH 1/2] entry: Rename arch_check_user_regs() to arch_enter_from_user_mode()

Quoted 9 lines · Thomas Gleixner
Quoted 8 lines · Thomas Richter

From: Sven Schnelle <[email protected]>

arch_check_user_regs() is used at the moment to verify that struct pt_regs contains valid values when entering the kernel from userspace. s390 needs a place in the generic entry code to modify a cpu data structure when switching from userspace to kernel mode. As arch_check_user_regs() is exactly this, rename it to arch_enter_from_user_mode().

Quoted 6 lines
Quoted 4 lines
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Andy Lutomirski <[email protected]>

Is it ok for you if this gets routed via the s390 tree?

#8 lore raw

Re: [PATCH 1/2] entry: Rename arch_check_user_regs() to arch_enter_from_user_mode()

Quoted 10 lines · Heiko Carstens
Quoted 9 lines · Thomas Gleixner
Quoted 8 lines · Thomas Richter

From: Sven Schnelle <[email protected]>

arch_check_user_regs() is used at the moment to verify that struct pt_regs contains valid values when entering the kernel from userspace. s390 needs a place in the generic entry code to modify a cpu data structure when switching from userspace to kernel mode. As arch_check_user_regs() is exactly this, rename it to arch_enter_from_user_mode().

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Cc Thomas Gleixner <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Andy Lutomirski <[email protected]>

Is it ok for you if this gets routed via the s390 tree?

Sure.

#9 lore raw

PATCH 2/2 s390/pai: Add support for cryptography counters

In reply to Thomas Richter

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.

---
 arch/s390/include/asm/ctl_reg.h      |   4 +-
 arch/s390/include/asm/entry-common.h |  10 +-
 arch/s390/include/asm/lowcore.h      |   5 +-
 arch/s390/include/asm/nmi.h          |   2 +-
 arch/s390/include/asm/pai.h          |  74 +++
 arch/s390/kernel/Makefile            |   1 +
 arch/s390/kernel/entry.S             |   1 +
 arch/s390/kernel/nmi.c               |   6 +-
 arch/s390/kernel/perf_pai_crypto.c   | 688 +++++++++++++++++++++++++++
 9 files changed, 784 insertions(+), 7 deletions(-)
 create mode 100644 arch/s390/include/asm/pai.h
 create mode 100644 arch/s390/kernel/perf_pai_crypto.c
arch/s390/include/asm/ctl_reg.h +3 -1
index 82388da3f95f..267a8f88e143 100644
@@ -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;
arch/s390/include/asm/entry-common.h +7 -3
index 99d654ccd3db..000de2b1e67a 100644
@@ -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)
 {
-	debug_user_asce(0);
+	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
arch/s390/include/asm/lowcore.h +4 -1
index 56002aeacabf..26fe5e535728 100644
@@ -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 */
arch/s390/include/asm/nmi.h +1 -1
index 292083083830..af1cd3a6f406 100644
@@ -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);
 
arch/s390/include/asm/pai.h new file +74 -0

74 lines not on this page · raw message

arch/s390/kernel/Makefile +1 -0
index c8d1b6aa823e..5851041bb214 100644
@@ -72,6 +72,7 @@ obj-$(CONFIG_IMA_SECURE_AND_OR_TRUSTED_BOOT)	+= ima_arch.o
 obj-$(CONFIG_PERF_EVENTS)	+= perf_event.o perf_cpum_cf_common.o
 obj-$(CONFIG_PERF_EVENTS)	+= perf_cpum_cf.o perf_cpum_sf.o
 obj-$(CONFIG_PERF_EVENTS)	+= perf_cpum_cf_events.o perf_regs.o
+obj-$(CONFIG_PERF_EVENTS)	+= perf_pai_crypto.o
 
 obj-$(CONFIG_TRACEPOINTS)	+= trace.o
 obj-$(findstring y, $(CONFIG_PROTECTED_VIRTUALIZATION_GUEST) $(CONFIG_PGSTE))	+= uv.o
arch/s390/kernel/entry.S +1 -0
index 59b69c8ab5e1..e3d1e01a37ea 100644
@@ -602,6 +602,7 @@ ENTRY(mcck_int_handler)
 	mvc	STACK_FRAME_OVERHEAD(__PT_SIZE,%r1),0(%r11)
 	xc	__SF_BACKCHAIN(8,%r1),__SF_BACKCHAIN(%r1)
 	la	%r11,STACK_FRAME_OVERHEAD(%r1)
+	lgr	%r2,%r11
 	lgr	%r15,%r1
 	brasl	%r14,s390_handle_mcck
 .Lmcck_return:
arch/s390/kernel/nmi.c +5 -1
index fc60e29b8690..53ed3884fe64 100644
@@ -29,6 +29,8 @@
 #include <asm/switch_to.h>
 #include <asm/ctl_reg.h>
 #include <asm/asm-offsets.h>
+#include <asm/pai.h>
+
 #include <linux/kvm_host.h>
 
 struct mcck_struct {
@@ -169,10 +171,12 @@ void __s390_handle_mcck(void)
 	}
 }
 
-void noinstr s390_handle_mcck(void)
+void noinstr s390_handle_mcck(struct pt_regs *regs)
 {
 	trace_hardirqs_off();
+	pai_kernel_enter(regs);
 	__s390_handle_mcck();
+	pai_kernel_exit(regs);
 	trace_hardirqs_on();
 }
 /*
arch/s390/kernel/perf_pai_crypto.c new file +688 -0

688 lines not on this page · raw message

Signature · 1 line
2.32.0