Skip to content

Commit a603002

Browse files
committed
virtio: replace restricted mem access flag with callback
Instead of having a global flag to require restricted memory access for all virtio devices, introduce a callback which can select that requirement on a per-device basis. For convenience add a common function returning always true, which can be used for use cases like SEV. Per default use a callback always returning false. As the callback needs to be set in early init code already, add a virtio anchor which is builtin in case virtio is enabled. Signed-off-by: Juergen Gross <jgross@suse.com> Tested-by: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.com> # Arm64 guest using Xen Reviewed-by: Stefano Stabellini <sstabellini@kernel.org> Link: https://lore.kernel.org/r/20220622063838.8854-2-jgross@suse.com Signed-off-by: Juergen Gross <jgross@suse.com>
1 parent 8441dac commit a603002

File tree

9 files changed

+51
-13
lines changed

9 files changed

+51
-13
lines changed

arch/s390/mm/init.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include <linux/cma.h>
3232
#include <linux/gfp.h>
3333
#include <linux/dma-direct.h>
34-
#include <linux/platform-feature.h>
3534
#include <asm/processor.h>
3635
#include <linux/uaccess.h>
3736
#include <asm/pgalloc.h>
@@ -48,6 +47,7 @@
4847
#include <asm/kasan.h>
4948
#include <asm/dma-mapping.h>
5049
#include <asm/uv.h>
50+
#include <linux/virtio_anchor.h>
5151
#include <linux/virtio_config.h>
5252

5353
pgd_t swapper_pg_dir[PTRS_PER_PGD] __section(".bss..swapper_pg_dir");
@@ -175,7 +175,7 @@ static void pv_init(void)
175175
if (!is_prot_virt_guest())
176176
return;
177177

178-
platform_set(PLATFORM_VIRTIO_RESTRICTED_MEM_ACCESS);
178+
virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc);
179179

180180
/* make sure bounce buffers are shared */
181181
swiotlb_init(true, SWIOTLB_FORCE | SWIOTLB_VERBOSE);

arch/x86/mm/mem_encrypt_amd.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
#include <linux/bitops.h>
2121
#include <linux/dma-mapping.h>
2222
#include <linux/virtio_config.h>
23+
#include <linux/virtio_anchor.h>
2324
#include <linux/cc_platform.h>
24-
#include <linux/platform-feature.h>
2525

2626
#include <asm/tlbflush.h>
2727
#include <asm/fixmap.h>
@@ -245,7 +245,7 @@ void __init sev_setup_arch(void)
245245
swiotlb_adjust_size(size);
246246

247247
/* Set restricted memory access for virtio. */
248-
platform_set(PLATFORM_VIRTIO_RESTRICTED_MEM_ACCESS);
248+
virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc);
249249
}
250250

251251
static unsigned long pg_level_to_pfn(int level, pte_t *kpte, pgprot_t *ret_prot)

drivers/virtio/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# SPDX-License-Identifier: GPL-2.0-only
2+
config VIRTIO_ANCHOR
3+
bool
4+
25
config VIRTIO
36
tristate
7+
select VIRTIO_ANCHOR
48
help
59
This option is selected by any driver which implements the virtio
610
bus, such as CONFIG_VIRTIO_PCI, CONFIG_VIRTIO_MMIO, CONFIG_RPMSG

drivers/virtio/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-License-Identifier: GPL-2.0
22
obj-$(CONFIG_VIRTIO) += virtio.o virtio_ring.o
3+
obj-$(CONFIG_VIRTIO_ANCHOR) += virtio_anchor.o
34
obj-$(CONFIG_VIRTIO_PCI_LIB) += virtio_pci_modern_dev.o
45
obj-$(CONFIG_VIRTIO_PCI_LIB_LEGACY) += virtio_pci_legacy_dev.o
56
obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o

drivers/virtio/virtio.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
#include <linux/virtio.h>
33
#include <linux/spinlock.h>
44
#include <linux/virtio_config.h>
5+
#include <linux/virtio_anchor.h>
56
#include <linux/module.h>
67
#include <linux/idr.h>
78
#include <linux/of.h>
8-
#include <linux/platform-feature.h>
99
#include <uapi/linux/virtio_ids.h>
1010

1111
/* Unique numbering for virtio devices. */
@@ -174,7 +174,7 @@ static int virtio_features_ok(struct virtio_device *dev)
174174

175175
might_sleep();
176176

177-
if (platform_has(PLATFORM_VIRTIO_RESTRICTED_MEM_ACCESS)) {
177+
if (virtio_check_mem_acc_cb(dev)) {
178178
if (!virtio_has_feature(dev, VIRTIO_F_VERSION_1)) {
179179
dev_warn(&dev->dev,
180180
"device must provide VIRTIO_F_VERSION_1\n");

drivers/virtio/virtio_anchor.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
#include <linux/virtio.h>
3+
#include <linux/virtio_anchor.h>
4+
5+
bool virtio_require_restricted_mem_acc(struct virtio_device *dev)
6+
{
7+
return true;
8+
}
9+
EXPORT_SYMBOL_GPL(virtio_require_restricted_mem_acc);
10+
11+
static bool virtio_no_restricted_mem_acc(struct virtio_device *dev)
12+
{
13+
return false;
14+
}
15+
16+
bool (*virtio_check_mem_acc_cb)(struct virtio_device *dev) =
17+
virtio_no_restricted_mem_acc;
18+
EXPORT_SYMBOL_GPL(virtio_check_mem_acc_cb);

include/linux/platform-feature.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66
#include <asm/platform-feature.h>
77

88
/* The platform features are starting with the architecture specific ones. */
9-
10-
/* Used to enable platform specific DMA handling for virtio devices. */
11-
#define PLATFORM_VIRTIO_RESTRICTED_MEM_ACCESS (0 + PLATFORM_ARCH_FEAT_N)
12-
13-
#define PLATFORM_FEAT_N (1 + PLATFORM_ARCH_FEAT_N)
9+
#define PLATFORM_FEAT_N (0 + PLATFORM_ARCH_FEAT_N)
1410

1511
void platform_set(unsigned int feature);
1612
void platform_clear(unsigned int feature);

include/linux/virtio_anchor.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef _LINUX_VIRTIO_ANCHOR_H
3+
#define _LINUX_VIRTIO_ANCHOR_H
4+
5+
#ifdef CONFIG_VIRTIO_ANCHOR
6+
struct virtio_device;
7+
8+
bool virtio_require_restricted_mem_acc(struct virtio_device *dev);
9+
extern bool (*virtio_check_mem_acc_cb)(struct virtio_device *dev);
10+
11+
static inline void virtio_set_mem_acc_cb(bool (*func)(struct virtio_device *))
12+
{
13+
virtio_check_mem_acc_cb = func;
14+
}
15+
#else
16+
#define virtio_set_mem_acc_cb(func) do { } while (0)
17+
#endif
18+
19+
#endif /* _LINUX_VIRTIO_ANCHOR_H */

include/xen/xen.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ bool xen_biovec_phys_mergeable(const struct bio_vec *vec1,
5252
extern u64 xen_saved_max_mem_size;
5353
#endif
5454

55-
#include <linux/platform-feature.h>
55+
#include <linux/virtio_anchor.h>
5656

5757
static inline void xen_set_restricted_virtio_memory_access(void)
5858
{
5959
if (IS_ENABLED(CONFIG_XEN_VIRTIO) && xen_domain())
60-
platform_set(PLATFORM_VIRTIO_RESTRICTED_MEM_ACCESS);
60+
virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc);
6161
}
6262

6363
#ifdef CONFIG_XEN_UNPOPULATED_ALLOC

0 commit comments

Comments
 (0)