Skip to content

Commit 251e90e

Browse files
committed
xen: don't require virtio with grants for non-PV guests
Commit fa1f574 ("xen/virtio: Enable restricted memory access using Xen grant mappings") introduced a new requirement for using virtio devices: the backend now needs to support the VIRTIO_F_ACCESS_PLATFORM feature. This is an undue requirement for non-PV guests, as those can be operated with existing backends without any problem, as long as those backends are running in dom0. Per default allow virtio devices without grant support for non-PV guests. On Arm require VIRTIO_F_ACCESS_PLATFORM for devices having been listed in the device tree to use grants. Add a new config item to always force use of grants for virtio. Fixes: fa1f574 ("xen/virtio: Enable restricted memory access using Xen grant mappings") Reported-by: Viresh Kumar <viresh.kumar@linaro.org> Signed-off-by: Juergen Gross <jgross@suse.com> Reviewed-by: Oleksandr Tyshchenko <oleksandr_tyshchenko@epam.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-4-jgross@suse.com Signed-off-by: Juergen Gross <jgross@suse.com>
1 parent a870544 commit 251e90e

File tree

7 files changed

+38
-11
lines changed

7 files changed

+38
-11
lines changed

arch/arm/xen/enlighten.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <linux/timekeeping.h>
3535
#include <linux/timekeeper_internal.h>
3636
#include <linux/acpi.h>
37+
#include <linux/virtio_anchor.h>
3738

3839
#include <linux/mm.h>
3940

@@ -443,7 +444,8 @@ static int __init xen_guest_init(void)
443444
if (!xen_domain())
444445
return 0;
445446

446-
xen_set_restricted_virtio_memory_access();
447+
if (IS_ENABLED(CONFIG_XEN_VIRTIO))
448+
virtio_set_mem_acc_cb(xen_virtio_mem_acc);
447449

448450
if (!acpi_disabled)
449451
xen_acpi_guest_init();

arch/x86/xen/enlighten_hvm.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <linux/cpu.h>
55
#include <linux/kexec.h>
66
#include <linux/memblock.h>
7+
#include <linux/virtio_anchor.h>
78

89
#include <xen/features.h>
910
#include <xen/events.h>
@@ -195,7 +196,8 @@ static void __init xen_hvm_guest_init(void)
195196
if (xen_pv_domain())
196197
return;
197198

198-
xen_set_restricted_virtio_memory_access();
199+
if (IS_ENABLED(CONFIG_XEN_VIRTIO_FORCE_GRANT))
200+
virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc);
199201

200202
init_hvm_pv_info();
201203

arch/x86/xen/enlighten_pv.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <linux/gfp.h>
3232
#include <linux/edd.h>
3333
#include <linux/reboot.h>
34+
#include <linux/virtio_anchor.h>
3435

3536
#include <xen/xen.h>
3637
#include <xen/events.h>
@@ -109,7 +110,9 @@ static DEFINE_PER_CPU(struct tls_descs, shadow_tls_desc);
109110

110111
static void __init xen_pv_init_platform(void)
111112
{
112-
xen_set_restricted_virtio_memory_access();
113+
/* PV guests can't operate virtio devices without grants. */
114+
if (IS_ENABLED(CONFIG_XEN_VIRTIO))
115+
virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc);
113116

114117
populate_extra_pte(fix_to_virt(FIX_PARAVIRT_BOOTMAP));
115118

drivers/xen/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,4 +355,13 @@ config XEN_VIRTIO
355355

356356
If in doubt, say n.
357357

358+
config XEN_VIRTIO_FORCE_GRANT
359+
bool "Require Xen virtio support to use grants"
360+
depends on XEN_VIRTIO
361+
help
362+
Require virtio for Xen guests to use grant mappings.
363+
This will avoid the need to give the backend the right to map all
364+
of the guest memory. This will need support on the backend side
365+
(e.g. qemu or kernel, depending on the virtio device types used).
366+
358367
endmenu

drivers/xen/grant-dma-ops.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <linux/of.h>
1313
#include <linux/pfn.h>
1414
#include <linux/xarray.h>
15+
#include <linux/virtio_anchor.h>
16+
#include <linux/virtio.h>
1517
#include <xen/xen.h>
1618
#include <xen/xen-ops.h>
1719
#include <xen/grant_table.h>
@@ -287,6 +289,14 @@ bool xen_is_grant_dma_device(struct device *dev)
287289
return has_iommu;
288290
}
289291

292+
bool xen_virtio_mem_acc(struct virtio_device *dev)
293+
{
294+
if (IS_ENABLED(CONFIG_XEN_VIRTIO_FORCE_GRANT))
295+
return true;
296+
297+
return xen_is_grant_dma_device(dev->dev.parent);
298+
}
299+
290300
void xen_grant_setup_dma_ops(struct device *dev)
291301
{
292302
struct xen_grant_dma_data *data;

include/xen/xen-ops.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <linux/percpu.h>
66
#include <linux/notifier.h>
77
#include <linux/efi.h>
8+
#include <linux/virtio_anchor.h>
89
#include <xen/features.h>
910
#include <asm/xen/interface.h>
1011
#include <xen/interface/vcpu.h>
@@ -217,6 +218,7 @@ static inline void xen_preemptible_hcall_end(void) { }
217218
#ifdef CONFIG_XEN_GRANT_DMA_OPS
218219
void xen_grant_setup_dma_ops(struct device *dev);
219220
bool xen_is_grant_dma_device(struct device *dev);
221+
bool xen_virtio_mem_acc(struct virtio_device *dev);
220222
#else
221223
static inline void xen_grant_setup_dma_ops(struct device *dev)
222224
{
@@ -225,6 +227,13 @@ static inline bool xen_is_grant_dma_device(struct device *dev)
225227
{
226228
return false;
227229
}
230+
231+
struct virtio_device;
232+
233+
static inline bool xen_virtio_mem_acc(struct virtio_device *dev)
234+
{
235+
return false;
236+
}
228237
#endif /* CONFIG_XEN_GRANT_DMA_OPS */
229238

230239
#endif /* INCLUDE_XEN_OPS_H */

include/xen/xen.h

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

55-
#include <linux/virtio_anchor.h>
56-
57-
static inline void xen_set_restricted_virtio_memory_access(void)
58-
{
59-
if (IS_ENABLED(CONFIG_XEN_VIRTIO) && xen_domain())
60-
virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc);
61-
}
62-
6355
#ifdef CONFIG_XEN_UNPOPULATED_ALLOC
6456
int xen_alloc_unpopulated_pages(unsigned int nr_pages, struct page **pages);
6557
void xen_free_unpopulated_pages(unsigned int nr_pages, struct page **pages);

0 commit comments

Comments
 (0)