powerpc: Add support for userspace P9 copy paste
PATCH 1/1 powerpc: Add support for userspace P9 copy paste
The copy paste facility introduced in POWER9 provides an optimised mechanism for a userspace application to copy a cacheline. This is provided by a pair of instructions, copy and paste, while a third, cp_abort (copy paste abort), provides a clean up of the state in case of a failure.
The copy instruction will read a 128 byte cacheline and store it in an internal buffer. The subsequent paste instruction will store this internal buffer to memory and set a CR field if the paste succeeds.
Since the state of the copy paste buffer is internal (and not architecturally visible), in the unlikely event of a context switch, the state cannot be stored and the paste should therefore fail.
The cp_abort instruction exists to fail and clean up any such interrupted copy paste sequence and is to be called by the kernel as part of the context switch. Doing so prevents data from a preceding copy in one process leaking into the paste of another.
This code enables use of the cp_abort instruction if a supported processor is detected.
NOTE: this is for userspace only, not in kernel, and does not deal with KVM guests.
Patch created with much assistance from Michael Neuling <[email protected]>
---
Note: A follow-up patch is expected soon with a working self-test.
arch/powerpc/include/asm/ppc-opcode.h | 2 ++arch/powerpc/kernel/entry_64.S | 9 +++++++++
2 files changed, 11 insertions(+)
arch/powerpc/include/asm/ppc-opcode.h +0 -0
@@ -131,6 +131,7 @@
/* sorted alphabetically */
#define PPC_INST_BHRBE 0x7c00025c
#define PPC_INST_CLRBHRB 0x7c00035c
+#define PPC_INST_CP_ABORT 0x7c00068c
#define PPC_INST_DCBA 0x7c0005ec
#define PPC_INST_DCBA_MASK 0xfc0007fe
#define PPC_INST_DCBAL 0x7c2005ec
@@ -285,6 +286,7 @@
#endif
/* Deal with instructions that older assemblers aren't aware of */
+#define PPC_CP_ABORT stringify_in_c(.long PPC_INST_CP_ABORT)
#define PPC_DCBAL(a, b) stringify_in_c(.long PPC_INST_DCBAL | \ __PPC_RA(a) | __PPC_RB(b))
#define PPC_DCBZL(a, b) stringify_in_c(.long PPC_INST_DCBZL | \
arch/powerpc/kernel/entry_64.S +0 -0
@@ -36,6 +36,7 @@
#include <asm/hw_irq.h>
#include <asm/context_tracking.h>
#include <asm/tm.h>
+#include <asm/ppc-opcode.h>
/*
* System calls.
@@ -508,6 +509,14 @@ BEGIN_FTR_SECTION
ldarx r6,0,r1
END_FTR_SECTION_IFSET(CPU_FTR_STCX_CHECKS_ADDRESS)
+BEGIN_FTR_SECTION
+/*
+ * A cp_abort (copy paste abort) here ensures that when context switching, a
+ * copy from one process can't leak into the paste of another.
+ */
+ PPC_CP_ABORT
+END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
+
#ifdef CONFIG_PPC_BOOK3S
/* Cancel all explict user streams as they will have no use after context
* switch and will stop the HW from creating streams itself
Signature · 8 lines
2.5.5 -- _ °v° /(_)\ ^ ^
Quoted 19 lines · On Tue, 26 Apr 2016 10:28:50 +1000 Chris Smart <[email protected]> wrote:
The copy paste facility introduced in POWER9 provides an optimised mechanism for a userspace application to copy a cacheline. This is provided by a pair of instructions, copy and paste, while a third,
cp_abort(copy paste abort), provides a clean up of the state in case of a failure.The copy instruction will read a 128 byte cacheline and store it in an internal buffer. The subsequent paste instruction will store this internal buffer to memory and set a CR field if the paste succeeds.
Since the state of the copy paste buffer is internal (and not architecturally visible), in the unlikely event of a context switch, the state cannot be stored and the paste should therefore fail.
The
cp_abortinstruction exists to fail and clean up any such interrupted copy paste sequence and is to be called by the kernel as part of the context switch. Doing so prevents data from a preceding copy in one process leaking into the paste of another.
Quoted 9 lines
This code enables use of the
cp_abortinstruction if a supported processor is detected.NOTE: this is for userspace only, not in kernel, and does not deal with KVM guests.
Patch created with much assistance from Michael Neuling <[email protected]>
Hi Chris,
Patch looks good. Looks like you've put 8 spaces (instead of a tab) on the PPC_CP_ABORT line.
Apart from that,
Quoted 56 lines
Signed-off-by Chris Smart <[email protected]>---
Note: A follow-up patch is expected soon with a working self-test.
arch/powerpc/include/asm/ppc-opcode.h | 2 ++ arch/powerpc/kernel/entry_64.S | 9 +++++++++ 2 files changed, 11 insertions(+)arch/powerpc/include/asm/ppc-opcode.h +2
-0@@ -131,6 +131,7 @@/* sorted alphabetically */ #define PPC_INST_BHRBE 0x7c00025c#define PPC_INST_CLRBHRB 0x7c00035c +#define PPC_INST_CP_ABORT 0x7c00068c#define PPC_INST_DCBA 0x7c0005ec#define PPC_INST_DCBA_MASK 0xfc0007fe#define PPC_INST_DCBAL 0x7c2005ec@@ -285,6 +286,7 @@#endif /* Deal with instructions that older assemblers aren't aware of */ +#define PPC_CP_ABORT stringify_in_c(.long PPC_INST_CP_ABORT)#define PPC_DCBAL (a, b) stringify_in_c(.long PPC_INST_DCBAL | \ __PPC_RA(a) | __PPC_RB(b))#define PPC_DCBZL (a, b) stringify_in_c(.long PPC_INST_DCBZL | \arch/powerpc/kernel/entry_64.S +9
-0@@ -36,6 +36,7 @@ #include <asm/hw_irq.h> #include <asm/context_tracking.h> #include <asm/tm.h> +#include <asm/ppc-opcode.h> /* * System calls.@@ -508,6 +509,14 @@ BEGIN_FTR_SECTION ldarx r6,0,r1 END_FTR_SECTION_IFSET(CPU_FTR_STCX_CHECKS_ADDRESS) +BEGIN_FTR_SECTION +/* + * A cp_abort (copy paste abort) here ensures that when context switching, a + * copy from one process can't leak into the paste of another. + */ + PPC_CP_ABORT +END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300) + #ifdef CONFIG_PPC_BOOK3S /* Cancel all explict user streams as they will have no use after context * switch and will stop the HW from creating streams itself
Quoted 5 lines · Cyril Bur
Hi Chris,
Patch looks good. Looks like you've put 8 spaces (instead of a tab) on the
PPC_CP_ABORTline.
Argh. I'm not sure how that happened, thanks.
Quoted 3 lines
Apart from that,
Reviewed-by Cyril Bur <[email protected]>
Cheers,
-c
Review BEGIN_FTR_SECTION · line 517
Quoted 12 lines
/*
* System calls.(quoted diff) +6
-0@@ -508,6 +509,14 @@ BEGIN_FTR_SECTION ldarx r6,0,r1 END_FTR_SECTION_IFSET(CPU_FTR_STCX_CHECKS_ADDRESS) +BEGIN_FTR_SECTION +/* + * A cp_abort (copy paste abort) here ensures that when context switching, a + * copy from one process can't leak into the paste of another. + */ + PPC_CP_ABORT
I think the alignment issue has been called out, but it is not clear from the changelog that we do this during syscall_exit/syscalls.
And also, do we need to care about preemptions, etc by the scheduler?
Quoted 5 lines
(quoted diff) +2
-0+END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300) + #ifdef CONFIG_PPC_BOOK3S /* Cancel all explict user streams as they will have no use after context * switch and will stop the HW from creating streams itself
Quoted 9 lines · Balbir Singh
Quoted 9 lines
=C2=A0
/*
=C2=A0* System calls.(quoted diff) +0
-0@@ -508,6 +509,14 @@ BEGIN_FTR_SECTION=C2=A0=C2=A0=C2=A0=C2=A0ldarx=C2=A0=C2=A0=C2=A0=C2=A0r6,0,r1
END_FTR_SECTION_IFSET(CPU_FTR_STCX_CHECKS_ADDRESS)=C2=A0(quoted diff) +2
-0+BEGIN_FTR_SECTION +/*
Quoted 8 lines
Quoted 5 lines
+ * A
cp_abort(copy paste abort) here ensures that when context switching, a
+ * copy from one process can't leak into the paste of another.
+ */ +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0PPC_CP_ABORTI think the alignment issue has been called out, but it is not clear from the changelog that
we do this during syscall_exit/syscalls.
I don't think we need this during syscall entry exit until we use this in the kernel.
Quoted 1 line
And also, do we need to care about preemptions, etc by the scheduler?
This handles the userspace preemption case.
kernel support is not handled here, so kernel preemption is TBD.
Mikey
Quoted 6 lines
=C2=A0
Quoted 5 lines
=C2=A0
(quoted diff) +2
-0+END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300) +#ifdef
CONFIG_PPC_BOOK3S
/* Cancel all explict user streams as they will have no use after conte=
xt
Quoted 5 lines
Quoted 1 line
=C2=A0* switch and will stop the HW from creating streams itself
_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev
RnJvbTogQ2hyaXMgU21hcnQNCj4gU2VudDogMjYgQXByaWwgMjAxNiAwMToyOQ0KPiBUaGUgY29w eSBwYXN0ZSBmYWNpbGl0eSBpbnRyb2R1Y2VkIGluIFBPV0VSOSBwcm92aWRlcyBhbi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BpcyB0byBiZSBjYWxsZWQgYnkgdGhlIGtlcm5lbCBh cw0KPiBwYXJ0IG9mIHRoZSBjb250ZXh0IHN3aXRjaC4gRG9pbmcgc28gcHJldmVudHMgZGF0YSBm cm9tIGEgcHJlY2VkaW5nIGNvcHkNCj4gaW4gb25lIHByb2Nlc3MgbGVha2luZyBpbnRvIHRoZSBw YXN0ZSBvZiBhbm90aGVyLg0KPiANCj4gVGhpcyBjb2RlIGVuYWJsZXMgdXNlIG9mIHRoZSBjcF9h Ym9ydCBpbnN0cnVjdGlvbiBpZiBhIHN1cHBvcnRlZA0KPiBwcm9jZXNzb3IgaXMgZGV0ZWN0ZWQu DQoNCkluIHRoYXQgY2FzZSB3aGF0IGFjdHVhbGx5IGNvbXBsZXRlcyB0aGUgY29weT8NCkkgdGhp bmsgeW91J2QgbmVlZCB0byBiZSBpbnNpZGUgYSAncmVzdGFydGFibGUgYXRvbWljIHNlcXVlbmNl Jw0KaW4gd2hpY2ggY2FzZSB0aGUgY3BfYWJvcnQgbmVlZCBvbmx5IGJlIGRvbmUgd2hlbiB0aGUv YSBSQVMNCmJsb2NrIGlzIGRldGVjdGVkLg0KDQpPciBoYXZlIEkgbWlzc2VkIHNvbWV0aGluZz8/ DQoNCglEYXZpZA0KDQo=
Quoted 24 lines · David Laight
From: Chris Smart
Quoted 15 lines
Sent: 26 April 2016 01:29
The copy paste facility introduced in POWER9 provides an optimised mechanism for a userspace application to copy a cacheline. This is provided by a pair of instructions, copy and paste, while a third,cp_abort(copy paste abort), provides a clean up of the state in case of a failure.The copy instruction will read a 128 byte cacheline and store it in an internal buffer. The subsequent paste instruction will store this internal buffer to memory and set a CR field if the paste succeeds.
Since the state of the copy paste buffer is internal (and not architecturally visible), in the unlikely event of a context switch, the state cannot be stored and the paste should therefore fail.
Quoted 7 lines
The
cp_abortinstruction exists to fail and clean up any such interrupted copy paste sequence and is to be called by the kernel as part of the context switch. Doing so prevents data from a preceding copy in one process leaking into the paste of another.This code enables use of the
cp_abortinstruction if a supported processor is detected.
Quoted 6 lines
In that case what actually completes the copy?
I think you'd need to be inside a 'restartable atomic sequence' in which case thecp_abortneed only be done when the/a RAS block is detected.Or have I missed something??
It's up to the userspace process that's doing the copy paste to check the CR field after the paste to see whether it has succeeded. If it has not succeeded, then the process can choose to re-do the copy and paste.
This is very similar to a lwarx stwcx where the user checks the CR after the stwcx to verify whether it has succeeded.
Hope that makes sense.
-c
RnJvbTogQ2hyaXMgU21hcnQNCj4gU2VudDogMjggQXByaWwgMjAxNiAwMDo1Mg0KLi4uDQo+ID5J biB0aGF0IGNhc2Ugd2hhdCBhY3R1YWxseSBjb21wbGV0ZXMgdGhlIGNvcHk/DQo+ID5JIHRoaW5r IHlvdSdkIG5lZWQgdG8gYmUgaW5zaWRlIGEgJ3Jlc3RhcnRhYmxlIGF0b21pYyBzZXF1ZW5jZScN Cj4gPmluIHdoaWNoIGNhc2UgdGhlIGNwX2Fib3J0IG5lZWQgb25seSBiZSBkb25lIHdoZW4gdGhl L2EgUkFTDQo+ID5ibG9jayBpcyBkZXRlY3RlZC4NCj4gPg0KPiA+T3IgaGF2ZSBJIG1pc3NlZCBz b21ldGhpbmc/Pw0KPiANCj4gSXQncyB1cCB0byB0aGUgdXNlcnNwYWNlIHByb2Nlc3MgdGhhdCdz IGRvaW5nIHRoZSBjb3B5IHBhc3RlIHRvIGNoZWNrDQo+IHRoZSBDUiBmaWVsZCBhZnRlciB0aGUg cGFzdGUgdG8gc2VlIHdoZXRoZXIgaXQgaGFzIHN1Y2NlZWRlZC4gSWYgaXQgaGFzDQo+IG5vdCBz dWNjZWVkZWQsIHRoZW4gdGhlIHByb2Nlc3MgY2FuIGNob29zZSB0byByZS1kbyB0aGUgY29weSBh bmQgcGFzdGUuDQo+DQo+IEhvcGUgdGhhdCBtYWtlcyBzZW5zZS4NCg0KWWVzLCB0aGFua3MuDQoN CglEYXZpZA0KDQo=
Quoted 5 lines · Chris Smart
The copy paste facility introduced in POWER9 provides an optimised mechanism for a userspace application to copy a cacheline. This is provided by a pair of instructions, copy and paste, while a third,
cp_abort(copy paste abort), provides a clean up of the state in case of a failure.
...
Quoted 2 lines
Signed-off-by Chris Smart <[email protected]>Reviewed-by Cyril Bur <[email protected]>
Applied to powerpc next, thanks.
https://git.kernel.org/powerpc/c/8a649045e75a4b9091ea9d041f
cheers