Skip to content

Commit 07d2a62

Browse files
npigginmpe
authored andcommitted
powerpc/64s: Avoid cpabort in context switch when possible
The ISA v3.0B copy-paste facility only requires cpabort when switching to a process that has foreign real addresses mapped (direct access to accelerators), to clear a potential copy buffer filled by a previous thread. There is no accelerator driver implemented yet, so cpabort can be removed. It can be be re-added when a driver is implemented. POWER9 DD1 requires the copy buffer to always be cleared on context switch, but if accelerators are not in use, then an unpaired copy from a dummy region is sufficient to clear data out of the copy buffer. This increases context switch performance by about 5% on POWER9. Signed-off-by: Nicholas Piggin <npiggin@gmail.com> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
1 parent 9145eff commit 07d2a62

3 files changed

Lines changed: 30 additions & 14 deletions

File tree

‎arch/powerpc/include/asm/ppc-opcode.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@
189189
/* sorted alphabetically */
190190
#define PPC_INST_BHRBE 0x7c00025c
191191
#define PPC_INST_CLRBHRB 0x7c00035c
192-
#define PPC_INST_COPY 0x7c00060c
193-
#define PPC_INST_COPY_FIRST 0x7c20060c
192+
#define PPC_INST_COPY 0x7c20060c
194193
#define PPC_INST_CP_ABORT 0x7c00068c
195194
#define PPC_INST_DCBA 0x7c0005ec
196195
#define PPC_INST_DCBA_MASK 0xfc0007fe
@@ -223,8 +222,7 @@
223222
#define PPC_INST_MSGSNDP 0x7c00011c
224223
#define PPC_INST_MTTMR 0x7c0003dc
225224
#define PPC_INST_NOP 0x60000000
226-
#define PPC_INST_PASTE 0x7c00070c
227-
#define PPC_INST_PASTE_LAST 0x7c20070d
225+
#define PPC_INST_PASTE 0x7c20070d
228226
#define PPC_INST_POPCNTB 0x7c0000f4
229227
#define PPC_INST_POPCNTB_MASK 0xfc0007fe
230228
#define PPC_INST_POPCNTD 0x7c0003f4
@@ -392,6 +390,8 @@
392390

393391
/* Deal with instructions that older assemblers aren't aware of */
394392
#define PPC_CP_ABORT stringify_in_c(.long PPC_INST_CP_ABORT)
393+
#define PPC_COPY(a, b) stringify_in_c(.long PPC_INST_COPY | \
394+
___PPC_RA(a) | ___PPC_RB(b))
395395
#define PPC_DCBAL(a, b) stringify_in_c(.long PPC_INST_DCBAL | \
396396
__PPC_RA(a) | __PPC_RB(b))
397397
#define PPC_DCBZL(a, b) stringify_in_c(.long PPC_INST_DCBZL | \

‎arch/powerpc/kernel/entry_64.S‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -536,15 +536,6 @@ _GLOBAL(_switch)
536536
* which contains larx/stcx, which will clear any reservation
537537
* of the task being switched.
538538
*/
539-
540-
BEGIN_FTR_SECTION
541-
/*
542-
* A cp_abort (copy paste abort) here ensures that when context switching, a
543-
* copy from one process can't leak into the paste of another.
544-
*/
545-
PPC_CP_ABORT
546-
END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
547-
548539
#ifdef CONFIG_PPC_BOOK3S
549540
/* Cancel all explict user streams as they will have no use after context
550541
* switch and will stop the HW from creating streams itself

‎arch/powerpc/kernel/process.c‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,11 @@ static inline void restore_sprs(struct thread_struct *old_thread,
11371137
#endif
11381138
}
11391139

1140+
#ifdef CONFIG_PPC_BOOK3S_64
1141+
#define CP_SIZE 128
1142+
static const u8 dummy_copy_buffer[CP_SIZE] __attribute__((aligned(CP_SIZE)));
1143+
#endif
1144+
11401145
struct task_struct *__switch_to(struct task_struct *prev,
11411146
struct task_struct *new)
11421147
{
@@ -1226,8 +1231,28 @@ struct task_struct *__switch_to(struct task_struct *prev,
12261231
batch->active = 1;
12271232
}
12281233

1229-
if (current_thread_info()->task->thread.regs)
1234+
if (current_thread_info()->task->thread.regs) {
12301235
restore_math(current_thread_info()->task->thread.regs);
1236+
1237+
/*
1238+
* The copy-paste buffer can only store into foreign real
1239+
* addresses, so unprivileged processes can not see the
1240+
* data or use it in any way unless they have foreign real
1241+
* mappings. We don't have a VAS driver that allocates those
1242+
* yet, so no cpabort is required.
1243+
*/
1244+
if (cpu_has_feature(CPU_FTR_POWER9_DD1)) {
1245+
/*
1246+
* DD1 allows paste into normal system memory, so we
1247+
* do an unpaired copy here to clear the buffer and
1248+
* prevent a covert channel being set up.
1249+
*
1250+
* cpabort is not used because it is quite expensive.
1251+
*/
1252+
asm volatile(PPC_COPY(%0, %1)
1253+
: : "r"(dummy_copy_buffer), "r"(0));
1254+
}
1255+
}
12311256
#endif /* CONFIG_PPC_STD_MMU_64 */
12321257

12331258
return last;

0 commit comments

Comments
 (0)