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

xen: don't require virtio with grants for non-PV guests

Commit fa1f57421e0b ("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: fa1f57421e0b ("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>

+38 -11
+3 -1
arch/arm/xen/enlighten.c
··· 34 34 #include <linux/timekeeping.h> 35 35 #include <linux/timekeeper_internal.h> 36 36 #include <linux/acpi.h> 37 + #include <linux/virtio_anchor.h> 37 38 38 39 #include <linux/mm.h> 39 40 ··· 444 443 if (!xen_domain()) 445 444 return 0; 446 445 447 - xen_set_restricted_virtio_memory_access(); 446 + if (IS_ENABLED(CONFIG_XEN_VIRTIO)) 447 + virtio_set_mem_acc_cb(xen_virtio_mem_acc); 448 448 449 449 if (!acpi_disabled) 450 450 xen_acpi_guest_init();
+3 -1
arch/x86/xen/enlighten_hvm.c
··· 4 4 #include <linux/cpu.h> 5 5 #include <linux/kexec.h> 6 6 #include <linux/memblock.h> 7 + #include <linux/virtio_anchor.h> 7 8 8 9 #include <xen/features.h> 9 10 #include <xen/events.h> ··· 196 195 if (xen_pv_domain()) 197 196 return; 198 197 199 - xen_set_restricted_virtio_memory_access(); 198 + if (IS_ENABLED(CONFIG_XEN_VIRTIO_FORCE_GRANT)) 199 + virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc); 200 200 201 201 init_hvm_pv_info(); 202 202
+4 -1
arch/x86/xen/enlighten_pv.c
··· 31 31 #include <linux/gfp.h> 32 32 #include <linux/edd.h> 33 33 #include <linux/reboot.h> 34 + #include <linux/virtio_anchor.h> 34 35 35 36 #include <xen/xen.h> 36 37 #include <xen/events.h> ··· 110 109 111 110 static void __init xen_pv_init_platform(void) 112 111 { 113 - xen_set_restricted_virtio_memory_access(); 112 + /* PV guests can't operate virtio devices without grants. */ 113 + if (IS_ENABLED(CONFIG_XEN_VIRTIO)) 114 + virtio_set_mem_acc_cb(virtio_require_restricted_mem_acc); 114 115 115 116 populate_extra_pte(fix_to_virt(FIX_PARAVIRT_BOOTMAP)); 116 117
+9
drivers/xen/Kconfig
··· 355 355 356 356 If in doubt, say n. 357 357 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 + 358 367 endmenu
+10
drivers/xen/grant-dma-ops.c
··· 12 12 #include <linux/of.h> 13 13 #include <linux/pfn.h> 14 14 #include <linux/xarray.h> 15 + #include <linux/virtio_anchor.h> 16 + #include <linux/virtio.h> 15 17 #include <xen/xen.h> 16 18 #include <xen/xen-ops.h> 17 19 #include <xen/grant_table.h> ··· 287 285 of_node_put(iommu_np); 288 286 289 287 return has_iommu; 288 + } 289 + 290 + bool xen_virtio_mem_acc(struct virtio_device *dev) 291 + { 292 + if (IS_ENABLED(CONFIG_XEN_VIRTIO_FORCE_GRANT)) 293 + return true; 294 + 295 + return xen_is_grant_dma_device(dev->dev.parent); 290 296 } 291 297 292 298 void xen_grant_setup_dma_ops(struct device *dev)
+9
include/xen/xen-ops.h
··· 5 5 #include <linux/percpu.h> 6 6 #include <linux/notifier.h> 7 7 #include <linux/efi.h> 8 + #include <linux/virtio_anchor.h> 8 9 #include <xen/features.h> 9 10 #include <asm/xen/interface.h> 10 11 #include <xen/interface/vcpu.h> ··· 218 217 #ifdef CONFIG_XEN_GRANT_DMA_OPS 219 218 void xen_grant_setup_dma_ops(struct device *dev); 220 219 bool xen_is_grant_dma_device(struct device *dev); 220 + bool xen_virtio_mem_acc(struct virtio_device *dev); 221 221 #else 222 222 static inline void xen_grant_setup_dma_ops(struct device *dev) 223 223 { 224 224 } 225 225 static inline bool xen_is_grant_dma_device(struct device *dev) 226 + { 227 + return false; 228 + } 229 + 230 + struct virtio_device; 231 + 232 + static inline bool xen_virtio_mem_acc(struct virtio_device *dev) 226 233 { 227 234 return false; 228 235 }
-8
include/xen/xen.h
··· 52 52 extern u64 xen_saved_max_mem_size; 53 53 #endif 54 54 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 - 63 55 #ifdef CONFIG_XEN_UNPOPULATED_ALLOC 64 56 int xen_alloc_unpopulated_pages(unsigned int nr_pages, struct page **pages); 65 57 void xen_free_unpopulated_pages(unsigned int nr_pages, struct page **pages);