Skip to content

Commit 64e1f0c

Browse files
halil-pasicheicarst
authored andcommitted
s390/mm: force swiotlb for protected virtualization
On s390, protected virtualization guests have to use bounced I/O buffers. That requires some plumbing. Let us make sure, any device that uses DMA API with direct ops correctly is spared from the problems, that a hypervisor attempting I/O to a non-shared page would bring. Signed-off-by: Halil Pasic <[email protected]> Reviewed-by: Claudio Imbrenda <[email protected]> Reviewed-by: Michael Mueller <[email protected]> Tested-by: Michael Mueller <[email protected]> Signed-off-by: Heiko Carstens <[email protected]>
1 parent 45488c4 commit 64e1f0c

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

arch/s390/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# SPDX-License-Identifier: GPL-2.0
2+
config ARCH_HAS_MEM_ENCRYPT
3+
def_bool y
4+
25
config MMU
36
def_bool y
47

@@ -186,6 +189,7 @@ config S390
186189
select VIRT_CPU_ACCOUNTING
187190
select ARCH_HAS_SCALED_CPUTIME
188191
select HAVE_NMI
192+
select SWIOTLB
189193

190194

191195
config SCHED_OMIT_FRAME_POINTER
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef S390_MEM_ENCRYPT_H__
3+
#define S390_MEM_ENCRYPT_H__
4+
5+
#ifndef __ASSEMBLY__
6+
7+
#define sme_me_mask 0ULL
8+
9+
static inline bool sme_active(void) { return false; }
10+
extern bool sev_active(void);
11+
12+
int set_memory_encrypted(unsigned long addr, int numpages);
13+
int set_memory_decrypted(unsigned long addr, int numpages);
14+
15+
#endif /* __ASSEMBLY__ */
16+
17+
#endif /* S390_MEM_ENCRYPT_H__ */

arch/s390/mm/init.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/mman.h>
1919
#include <linux/mm.h>
2020
#include <linux/swap.h>
21+
#include <linux/swiotlb.h>
2122
#include <linux/smp.h>
2223
#include <linux/init.h>
2324
#include <linux/pagemap.h>
@@ -29,6 +30,7 @@
2930
#include <linux/export.h>
3031
#include <linux/cma.h>
3132
#include <linux/gfp.h>
33+
#include <linux/dma-mapping.h>
3234
#include <asm/processor.h>
3335
#include <linux/uaccess.h>
3436
#include <asm/pgtable.h>
@@ -42,6 +44,8 @@
4244
#include <asm/sclp.h>
4345
#include <asm/set_memory.h>
4446
#include <asm/kasan.h>
47+
#include <asm/dma-mapping.h>
48+
#include <asm/uv.h>
4549

4650
pgd_t swapper_pg_dir[PTRS_PER_PGD] __section(.bss..swapper_pg_dir);
4751

@@ -128,6 +132,47 @@ void mark_rodata_ro(void)
128132
pr_info("Write protected read-only-after-init data: %luk\n", size >> 10);
129133
}
130134

135+
int set_memory_encrypted(unsigned long addr, int numpages)
136+
{
137+
int i;
138+
139+
/* make specified pages unshared, (swiotlb, dma_free) */
140+
for (i = 0; i < numpages; ++i) {
141+
uv_remove_shared(addr);
142+
addr += PAGE_SIZE;
143+
}
144+
return 0;
145+
}
146+
147+
int set_memory_decrypted(unsigned long addr, int numpages)
148+
{
149+
int i;
150+
/* make specified pages shared (swiotlb, dma_alloca) */
151+
for (i = 0; i < numpages; ++i) {
152+
uv_set_shared(addr);
153+
addr += PAGE_SIZE;
154+
}
155+
return 0;
156+
}
157+
158+
/* are we a protected virtualization guest? */
159+
bool sev_active(void)
160+
{
161+
return is_prot_virt_guest();
162+
}
163+
164+
/* protected virtualization */
165+
static void pv_init(void)
166+
{
167+
if (!is_prot_virt_guest())
168+
return;
169+
170+
/* make sure bounce buffers are shared */
171+
swiotlb_init(1);
172+
swiotlb_update_mem_attributes();
173+
swiotlb_force = SWIOTLB_FORCE;
174+
}
175+
131176
void __init mem_init(void)
132177
{
133178
cpumask_set_cpu(0, &init_mm.context.cpu_attach_mask);
@@ -136,6 +181,8 @@ void __init mem_init(void)
136181
set_max_mapnr(max_low_pfn);
137182
high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);
138183

184+
pv_init();
185+
139186
/* Setup guest page hinting */
140187
cmma_init();
141188

0 commit comments

Comments
 (0)