Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

powerpc/pseries/svm: Force SWIOTLB for secure guests

SWIOTLB checks range of incoming CPU addresses to be bounced and sees if
the device can access it through its DMA window without requiring bouncing.
In such cases it just chooses to skip bouncing. But for cases like secure
guests on powerpc platform all addresses need to be bounced into the shared
pool of memory because the host cannot access it otherwise. Hence the need
to do the bouncing is not related to device's DMA window and use of bounce
buffers is forced by setting swiotlb_force.

Also, connect the shared memory conversion functions into the
ARCH_HAS_MEM_ENCRYPT hooks and call swiotlb_update_mem_attributes() to
convert SWIOTLB's memory pool to shared memory.

Signed-off-by: Anshuman Khandual <khandual@linux.vnet.ibm.com>
[ bauerman: Use ARCH_HAS_MEM_ENCRYPT hooks to share swiotlb memory pool. ]
Signed-off-by: Thiago Jung Bauermann <bauerman@linux.ibm.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20190820021326.6884-15-bauerman@linux.ibm.com

authored by

Anshuman Khandual and committed by
Michael Ellerman
2efbc58f edea902c

+74
+26
arch/powerpc/include/asm/mem_encrypt.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0+ */ 2 + /* 3 + * SVM helper functions 4 + * 5 + * Copyright 2018 IBM Corporation 6 + */ 7 + 8 + #ifndef _ASM_POWERPC_MEM_ENCRYPT_H 9 + #define _ASM_POWERPC_MEM_ENCRYPT_H 10 + 11 + #include <asm/svm.h> 12 + 13 + static inline bool mem_encrypt_active(void) 14 + { 15 + return is_secure_guest(); 16 + } 17 + 18 + static inline bool force_dma_unencrypted(struct device *dev) 19 + { 20 + return is_secure_guest(); 21 + } 22 + 23 + int set_memory_encrypted(unsigned long addr, int numpages); 24 + int set_memory_decrypted(unsigned long addr, int numpages); 25 + 26 + #endif /* _ASM_POWERPC_MEM_ENCRYPT_H */
+3
arch/powerpc/platforms/pseries/Kconfig
··· 149 149 config PPC_SVM 150 150 bool "Secure virtual machine (SVM) support for POWER" 151 151 depends on PPC_PSERIES 152 + select SWIOTLB 153 + select ARCH_HAS_MEM_ENCRYPT 154 + select ARCH_HAS_FORCE_DMA_UNENCRYPTED 152 155 help 153 156 There are certain POWER platforms which support secure guests using 154 157 the Protected Execution Facility, with the help of an Ultravisor
+45
arch/powerpc/platforms/pseries/svm.c
··· 7 7 */ 8 8 9 9 #include <linux/mm.h> 10 + #include <asm/machdep.h> 11 + #include <asm/svm.h> 12 + #include <asm/swiotlb.h> 10 13 #include <asm/ultravisor.h> 14 + 15 + static int __init init_svm(void) 16 + { 17 + if (!is_secure_guest()) 18 + return 0; 19 + 20 + /* Don't release the SWIOTLB buffer. */ 21 + ppc_swiotlb_enable = 1; 22 + 23 + /* 24 + * Since the guest memory is inaccessible to the host, devices always 25 + * need to use the SWIOTLB buffer for DMA even if dma_capable() says 26 + * otherwise. 27 + */ 28 + swiotlb_force = SWIOTLB_FORCE; 29 + 30 + /* Share the SWIOTLB buffer with the host. */ 31 + swiotlb_update_mem_attributes(); 32 + 33 + return 0; 34 + } 35 + machine_early_initcall(pseries, init_svm); 36 + 37 + int set_memory_encrypted(unsigned long addr, int numpages) 38 + { 39 + if (!PAGE_ALIGNED(addr)) 40 + return -EINVAL; 41 + 42 + uv_unshare_page(PHYS_PFN(__pa(addr)), numpages); 43 + 44 + return 0; 45 + } 46 + 47 + int set_memory_decrypted(unsigned long addr, int numpages) 48 + { 49 + if (!PAGE_ALIGNED(addr)) 50 + return -EINVAL; 51 + 52 + uv_share_page(PHYS_PFN(__pa(addr)), numpages); 53 + 54 + return 0; 55 + } 11 56 12 57 /* There's one dispatch log per CPU. */ 13 58 #define NR_DTL_PAGE (DISPATCH_LOG_BYTES * CONFIG_NR_CPUS / PAGE_SIZE)