Introduce MMIO/PIO dispatch file descriptors (ioregionfd)
cover Introduce MMIO/PIO dispatch file descriptors (ioregionfd)
This patchset introduces a KVM dispatch mechanism which can be used for handling MMIO/PIO accesses over file descriptors without returning from ioctl(KVM_RUN). This allows device emulation to run in another task separate from the vCPU task.
This is achieved through KVM vm ioctl for registering MMIO/PIO regions and a wire protocol that KVM uses to communicate with a task handling an MMIO/PIO access.
ioregionfd relies on kmemcg in order to limit the amount of kernel memory that userspace can consume. Can NR_IOBUS_DEVS hardcoded limit be enforced only in case kmemcg is disabled?
Elena Afanasova (2):
KVM: add initial support for KVM_SET_IOREGION
KVM: add initial support for ioregionfd blocking read/write operations
arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/Makefile | 1 + arch/x86/kvm/x86.c | 1 + include/linux/kvm_host.h | 17 ++ include/uapi/linux/kvm.h | 23 +++ virt/kvm/Kconfig | 3 + virt/kvm/eventfd.c | 25 +++ virt/kvm/eventfd.h | 14 ++ virt/kvm/ioregion.c | 390 +++++++++++++++++++++++++++++++++++++++ virt/kvm/ioregion.h | 15 ++ virt/kvm/kvm_main.c | 20 +- 11 files changed, 507 insertions(+), 3 deletions(-) create mode 100644 virt/kvm/eventfd.h create mode 100644 virt/kvm/ioregion.c create mode 100644 virt/kvm/ioregion.h
Signature · 1 line
2.25.1
RFC 1/2 KVM: add initial support for KVM_SET_IOREGION
This vm ioctl adds or removes an ioregionfd MMIO/PIO region. Guest read and write accesses are dispatched through the given ioregionfd instead of returning from ioctl(KVM_RUN). Regions can be deleted by setting fds to -1.
--- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/Makefile | 1 + arch/x86/kvm/x86.c | 1 + include/linux/kvm_host.h | 17 +++ include/uapi/linux/kvm.h | 23 ++++ virt/kvm/Kconfig | 3 + virt/kvm/eventfd.c | 25 +++++ virt/kvm/eventfd.h | 14 +++ virt/kvm/ioregion.c | 233 +++++++++++++++++++++++++++++++++++++++ virt/kvm/ioregion.h | 15 +++ virt/kvm/kvm_main.c | 20 +++- 11 files changed, 350 insertions(+), 3 deletions(-) create mode 100644 virt/kvm/eventfd.h create mode 100644 virt/kvm/ioregion.c create mode 100644 virt/kvm/ioregion.h
arch/x86/kvm/Kconfig +1 -0
@@ -33,6 +33,7 @@ config KVM select HAVE_KVM_IRQ_BYPASS select HAVE_KVM_IRQ_ROUTING select HAVE_KVM_EVENTFD + select KVM_IOREGION select KVM_ASYNC_PF select USER_RETURN_NOTIFIER select KVM_MMIO
arch/x86/kvm/Makefile +1 -0
@@ -12,6 +12,7 @@ KVM := ../../../virt/kvm kvm-y += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o \ $(KVM)/eventfd.o $(KVM)/irqchip.o $(KVM)/vfio.o kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o +kvm-$(CONFIG_KVM_IOREGION) += $(KVM)/ioregion.o kvm-y += x86.o emulate.o i8259.o irq.o lapic.o \ i8254.o ioapic.o irq_comm.o cpuid.o pmu.o mtrr.o \
arch/x86/kvm/x86.c +1 -0
@@ -3739,6 +3739,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_X86_USER_SPACE_MSR :case KVM_CAP_X86_MSR_FILTER :case KVM_CAP_ENFORCE_PV_FEATURE_CPUID : +case KVM_CAP_IOREGIONFD : r =1 ;break ;case KVM_CAP_SYNC_REGS :
include/linux/kvm_host.h +17 -0
@@ -470,6 +470,10 @@ struct kvm { struct mutex resampler_lock; } irqfds;struct list_head ioeventfds; +#endif +#ifdef CONFIG_KVM_IOREGION +struct list_head ioregions_mmio; +struct list_head ioregions_pio;#endif struct kvm_vm_stat stat;struct kvm_arch arch;
@@ -1262,6 +1266,19 @@ static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)#endif /* CONFIG_HAVE_KVM_EVENTFD */ +#ifdef CONFIG_KVM_IOREGION +void kvm_ioregionfd_init (struct kvm *kvm); +int kvm_ioregionfd (struct kvm *kvm,struct kvm_ioregion *args); + +#else + +static inline void kvm_ioregionfd_init (struct kvm *kvm) {} +static inline int kvm_ioregionfd (struct kvm *kvm,struct kvm_ioregion *args) +{ +return -ENOSYS ; +} +#endif +void kvm_arch_irq_routing_update (struct kvm *kvm);static inline void kvm_make_request (int req,struct kvm_vcpu *vcpu)
include/uapi/linux/kvm.h +23 -0
@@ -732,6 +732,27 @@ struct kvm_ioeventfd {__u8 pad[36 ]; }; +enum { + kvm_ioregion_flag_nr_pio, + kvm_ioregion_flag_nr_posted_writes, + kvm_ioregion_flag_nr_max, +}; + +#define KVM_IOREGION_PIO (1 << kvm_ioregion_flag_nr_pio) +#define KVM_IOREGION_POSTED_WRITES (1 << kvm_ioregion_flag_nr_posted_writes) + +#define KVM_IOREGION_VALID_FLAG_MASK ((1 << kvm_ioregion_flag_nr_max) - 1) + +struct kvm_ioregion { +__u64 guest_paddr;/* guest physical address */ +__u64 memory_size;/* bytes */ +__u64 user_data; +__s32 rfd; +__s32 wfd; +__u32 flags; +__u8 pad[28 ]; +}; +#define KVM_X86_DISABLE_EXITS_MWAIT (1 << 0)#define KVM_X86_DISABLE_EXITS_HLT (1 << 1)#define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2)
@@ -1053,6 +1074,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_X86_USER_SPACE_MSR 188#define KVM_CAP_X86_MSR_FILTER 189#define KVM_CAP_ENFORCE_PV_FEATURE_CPUID 190 +#define KVM_CAP_IOREGIONFD 191#ifdef KVM_CAP_IRQ_ROUTING
@@ -1308,6 +1330,7 @@ struct kvm_vfio_spapr_tce { struct kvm_userspace_memory_region)#define KVM_SET_TSS_ADDR _IO(KVMIO, 0x47)#define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO, 0x48, __u64) +#define KVM_SET_IOREGION _IOW(KVMIO, 0x49, struct kvm_ioregion)/* enable ucontrol for s390 */ struct kvm_s390_ucas_mapping {
virt/kvm/Kconfig +3 -0
@@ -17,6 +17,9 @@ config HAVE_KVM_EVENTFD bool select EVENTFD +config KVM_IOREGION + bool + config KVM_MMIO bool
virt/kvm/eventfd.c +25 -0
@@ -27,6 +27,7 @@#include <trace/events/kvm.h> #include <kvm/iodev.h> +#include "ioregion.h" #ifdef CONFIG_HAVE_KVM_IRQFD
@@ -755,6 +756,23 @@ static const struct kvm_io_device_ops ioeventfd_ops = { .destructor = ioeventfd_destructor, }; +#ifdef CONFIG_KVM_IOREGION +/* assumes kvm->slots_lock held */ +bool kvm_eventfd_collides (struct kvm *kvm,int bus_idx, +u64 start,u64 size) +{ +struct _ioeventfd *_p; + +list_for_each_entry (_p, &kvm->ioeventfds, list) +if (_p->bus_idx == bus_idx && +overlap (start, size, _p->addr, + !_p->length ?8 : _p->length)) +return true; + +return false; +} +#endif +/* assumes kvm->slots_lock held */ static bool ioeventfd_check_collision (struct kvm *kvm,struct _ioeventfd *p)
@@ -770,6 +788,13 @@ ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p) _p->datamatch == p->datamatch))))return true; +#ifdef CONFIG_KVM_IOREGION +if (p->bus_idx ==KVM_MMIO_BUS || p->bus_idx ==KVM_PIO_BUS ) +if (kvm_ioregion_collides (kvm, p->bus_idx, p->addr, + !p->length ?8 : p->length)) +return true; +#endif +return false; }
virt/kvm/eventfd.h new file +14 -0
@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_EVENTFD_H__ +#define __KVM_EVENTFD_H__ + +#ifdef CONFIG_KVM_IOREGION +bool kvm_eventfd_collides (struct kvm *kvm,int bus_idx,u64 start,u64 size); +#else +static inline bool +kvm_eventfd_collides (struct kvm *kvm,int bus_idx,u64 start,u64 size) +{ +return false; +} +#endif +#endif
virt/kvm/ioregion.c new file +233 -0
233 lines not on this page · raw message
virt/kvm/ioregion.h new file +15 -0
@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_IOREGION_H__ +#define __KVM_IOREGION_H__ + +#ifdef CONFIG_KVM_IOREGION +inline bool overlap (u64 start1,u64 size1,u64 start2,u64 size2); +bool kvm_ioregion_collides (struct kvm *kvm,int bus_idx,u64 start,u64 size); +#else +static inline bool +kvm_ioregion_collides (struct kvm *kvm,int bus_idx,u64 start,u64 size) +{ +return false; +} +#endif +#endif
virt/kvm/kvm_main.c +17 -3
@@ -747,6 +747,7 @@ static struct kvm *kvm_create_vm(unsigned long type)mmgrab (current->mm); kvm->mm = current->mm;kvm_eventfd_init (kvm); +kvm_ioregionfd_init (kvm);mutex_init (&kvm->lock);mutex_init (&kvm->irq_lock);mutex_init (&kvm->slots_lock);
@@ -3708,6 +3709,16 @@ static long kvm_vm_ioctl(struct file *filp, r =kvm_vm_ioctl_set_memory_region (kvm, &kvm_userspace_mem);break ; } +case KVM_SET_IOREGION : { +struct kvm_ioregion data; + + r = -EFAULT ; +if (copy_from_user (&data, argp,sizeof (data))) + goto out; + + r =kvm_ioregionfd (kvm, &data); +break ; + }case KVM_GET_DIRTY_LOG : {struct kvm_dirty_log log;
@@ -4301,9 +4312,12 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,if (!bus)return -ENOMEM ; -/* exclude ioeventfd which is limited by maximum fd */ -if (bus->dev_count - bus->ioeventfd_count >NR_IOBUS_DEVS -1 ) -return -ENOSPC ; +/* enforce hard limit if kmemcg is disabled and +* exclude ioeventfd which is limited by maximum fd +*/ +if (!memcg_kmem_enabled ()) +if (bus->dev_count - bus->ioeventfd_count >NR_IOBUS_DEVS -1 ) +return -ENOSPC ; new_bus =kmalloc (struct_size (bus, range, bus->dev_count +1 ),GFP_KERNEL_ACCOUNT );
Signature · 1 line
2.25.1
RFC 2/2 KVM: add initial support for ioregionfd blocking read/write operations
In reply to Elena Afanasova--- virt/kvm/ioregion.c | 157 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+)
virt/kvm/ioregion.c +157 -0
@@ -4,6 +4,33 @@#include <kvm/iodev.h> #include "eventfd.h" +/* Wire protocol */ +struct ioregionfd_cmd { +__u32 info; +__u32 padding; +__u64 user_data; +__u64 offset; +__u64 data; +}; + +struct ioregionfd_resp { +__u64 data; +__u8 pad[24 ]; +}; + +#define IOREGIONFD_CMD_READ 0 +#define IOREGIONFD_CMD_WRITE 1 + +#define IOREGIONFD_SIZE_8BIT 0 +#define IOREGIONFD_SIZE_16BIT 1 +#define IOREGIONFD_SIZE_32BIT 2 +#define IOREGIONFD_SIZE_64BIT 3 + +#define IOREGIONFD_SIZE_OFFSET 4 +#define IOREGIONFD_RESP_OFFSET 6 +#define IOREGIONFD_SIZE (x) ((x) << IOREGIONFD_SIZE_OFFSET) +#define IOREGIONFD_RESP (x) ((x) << IOREGIONFD_RESP_OFFSET) +void kvm_ioregionfd_init (struct kvm *kvm) {
@@ -38,10 +65,100 @@ ioregion_release(struct ioregion *p)kfree (p); } +static bool +pack_cmd (struct ioregionfd_cmd *cmd,u64 offset,u64 len,int opt,bool resp, +u64 user_data,const void *val) +{ +u64 size =0 ; + +switch (len) { +case 1 : + size =IOREGIONFD_SIZE_8BIT ; + *((u8 *)&cmd->data) = val ? *(u8 *)val :0 ; +break ; +case 2 : + size =IOREGIONFD_SIZE_16BIT ; + *((u16 *)&cmd->data) = val ? *(u16 *)val :0 ; +break ; +case 4 : + size =IOREGIONFD_SIZE_32BIT ; + *((u32 *)&cmd->data) = val ? *(u32 *)val :0 ; +break ; +case 8 : + size =IOREGIONFD_SIZE_64BIT ; + *((u64 *)&cmd->data) = val ? *(u64 *)val :0 ; +break ; +default : +return false; + } + cmd->user_data = user_data; + cmd->offset = offset; + cmd->info |= opt; + cmd->info |=IOREGIONFD_SIZE (size); + cmd->info |=IOREGIONFD_RESP (resp); + +return true; +} +static int ioregion_read (struct kvm_vcpu *vcpu,struct kvm_io_device *this,gpa_t addr,int len,void *val) { +struct ioregion *p =to_ioregion (this); +struct ioregionfd_cmd *cmd; +struct ioregionfd_resp *resp; +size_t buf_size; +void *buf; +int ret =0 ; + +if ((p->rf->f_flags &O_NONBLOCK ) || (p->wf->f_flags &O_NONBLOCK )) +return -EINVAL ; +if ((addr + len -1 ) > (p->paddr + p->size -1 )) +return -EINVAL ; + + buf_size =max_t (size_t,sizeof (*cmd),sizeof (*resp)); + buf =kzalloc (buf_size,GFP_KERNEL ); +if (!buf) +return -ENOMEM ; + cmd = (struct ioregionfd_cmd *)buf; + resp = (struct ioregionfd_resp *)buf; +if (!pack_cmd (cmd, addr - p->paddr, len,IOREGIONFD_CMD_READ , +1 , p->user_data,NULL )) { +kfree (buf); +return -EOPNOTSUPP ; + } + + ret =kernel_write (p->wf, cmd,sizeof (*cmd),0 ); +if (ret !=sizeof (*cmd)) { +kfree (buf); +return (ret <0 ) ? ret : -EIO ; + } +memset (buf,0 , buf_size); + ret =kernel_read (p->rf, resp,sizeof (*resp),0 ); +if (ret !=sizeof (*resp)) { +kfree (buf); +return (ret <0 ) ? ret : -EIO ; + } + +switch (len) { +case 1 : + *(u8 *)val = (u8 )resp->data; +break ; +case 2 : + *(u16 *)val = (u16 )resp->data; +break ; +case 4 : + *(u32 *)val = (u32 )resp->data; +break ; +case 8 : + *(u64 *)val = (u64 )resp->data; +break ; +default : +break ; + } + +kfree (buf); +return 0 ; }
@@ -49,6 +166,46 @@ static int ioregion_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this, gpa_t addr, int len, const void *val) { + struct ioregion *p = to_ioregion(this); +struct ioregionfd_cmd *cmd; +struct ioregionfd_resp *resp; +size_t buf_size =0 ; +void *buf; +int ret =0 ; + +if ((p->rf->f_flags &O_NONBLOCK ) || (p->wf->f_flags &O_NONBLOCK )) +return -EINVAL ; +if ((addr + len -1 ) > (p->paddr + p->size -1 )) +return -EINVAL ; + + buf_size =max_t (size_t,sizeof (*cmd),sizeof (*resp)); + buf =kzalloc (buf_size,GFP_KERNEL ); +if (!buf) +return -ENOMEM ; + cmd = (struct ioregionfd_cmd *)buf; +if (!pack_cmd (cmd, addr - p->paddr, len,IOREGIONFD_CMD_WRITE , + p->posted_writes ?0 :1 , p->user_data, val)) { +kfree (buf); +return -EOPNOTSUPP ; + } + + ret =kernel_write (p->wf, cmd,sizeof (*cmd),0 ); +if (ret !=sizeof (*cmd)) { +kfree (buf); +return (ret <0 ) ? ret : -EIO ; + } + +if (!p->posted_writes) { +memset (buf,0 , buf_size); + resp = (struct ioregionfd_resp *)buf; + ret =kernel_read (p->rf, resp,sizeof (*resp),0 ); +if (ret !=sizeof (*resp)) { +kfree (buf); +return (ret <0 ) ? ret : -EIO ; + } + } +kfree (buf); +return 0 ; }
Signature · 1 line
2.25.1
Re: [RFC 1/2] KVM: add initial support for KVM_SET_IOREGION
In reply to Elena AfanasovaQuoted 28 lines · Elena Afanasova
This vm ioctl adds or removes an ioregionfd MMIO/PIO region. Guest read and write accesses are dispatched through the given ioregionfd instead of returning from
ioctl(KVM_RUN). Regions can be deleted by setting fds to -1.Signed-off-by Elena Afanasova <[email protected]>--- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/Makefile | 1 + arch/x86/kvm/x86.c | 1 + include/linux/kvm_host.h | 17 +++ include/uapi/linux/kvm.h | 23 ++++ virt/kvm/Kconfig | 3 + virt/kvm/eventfd.c | 25 +++++ virt/kvm/eventfd.h | 14 +++ virt/kvm/ioregion.c | 233 +++++++++++++++++++++++++++++++++++++++ virt/kvm/ioregion.h | 15 +++ virt/kvm/kvm_main.c | 20 +++- 11 files changed, 350 insertions(+), 3 deletions(-) create mode 100644 virt/kvm/eventfd.h create mode 100644 virt/kvm/ioregion.c create mode 100644 virt/kvm/ioregion.harch/x86/kvm/Kconfig +0
-0
arch/x86/kvm/Kconfig config KVM · line 39
Quoted 7 lines
arch/x86/kvm/Kconfig +1
-0@@ -33,6 +33,7 @@ config KVM select HAVE_KVM_IRQ_BYPASS select HAVE_KVM_IRQ_ROUTING select HAVE_KVM_EVENTFD + select KVM_IOREGION select KVM_ASYNC_PF select USER_RETURN_NOTIFIER select KVM_MMIO
TODO non-x86 arch support
Quoted 201 lines
arch/x86/kvm/Makefile +1
-0@@ -12,6 +12,7 @@ KVM := ../../../virt/kvm kvm-y += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o \ $(KVM)/eventfd.o $(KVM)/irqchip.o $(KVM)/vfio.o kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o +kvm-$(CONFIG_KVM_IOREGION) += $(KVM)/ioregion.o kvm-y += x86.o emulate.o i8259.o irq.o lapic.o \ i8254.o ioapic.o irq_comm.o cpuid.o pmu.o mtrr.o \arch/x86/kvm/x86.c +1
-0@@ -3739,6 +3739,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_X86_USER_SPACE_MSR :case KVM_CAP_X86_MSR_FILTER :case KVM_CAP_ENFORCE_PV_FEATURE_CPUID : +case KVM_CAP_IOREGIONFD : r =1 ;break ;case KVM_CAP_SYNC_REGS :include/linux/kvm_host.h +17
-0@@ -470,6 +470,10 @@ struct kvm { struct mutex resampler_lock; } irqfds;struct list_head ioeventfds; +#endif +#ifdef CONFIG_KVM_IOREGION +struct list_head ioregions_mmio; +struct list_head ioregions_pio;#endif struct kvm_vm_stat stat;struct kvm_arch arch;@@ -1262,6 +1266,19 @@ static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)#endif /* CONFIG_HAVE_KVM_EVENTFD */ +#ifdef CONFIG_KVM_IOREGION +void kvm_ioregionfd_init (struct kvm *kvm); +int kvm_ioregionfd (struct kvm *kvm,struct kvm_ioregion *args); + +#else + +static inline void kvm_ioregionfd_init (struct kvm *kvm) {} +static inline int kvm_ioregionfd (struct kvm *kvm,struct kvm_ioregion *args) +{ +return -ENOSYS ; +} +#endif +void kvm_arch_irq_routing_update (struct kvm *kvm);static inline void kvm_make_request (int req,struct kvm_vcpu *vcpu)include/uapi/linux/kvm.h +23
-0@@ -732,6 +732,27 @@ struct kvm_ioeventfd {__u8 pad[36 ]; }; +enum { + kvm_ioregion_flag_nr_pio, + kvm_ioregion_flag_nr_posted_writes, + kvm_ioregion_flag_nr_max, +}; + +#define KVM_IOREGION_PIO (1 << kvm_ioregion_flag_nr_pio) +#define KVM_IOREGION_POSTED_WRITES (1 << kvm_ioregion_flag_nr_posted_writes) + +#define KVM_IOREGION_VALID_FLAG_MASK ((1 << kvm_ioregion_flag_nr_max) - 1) + +struct kvm_ioregion { +__u64 guest_paddr;/* guest physical address */ +__u64 memory_size;/* bytes */ +__u64 user_data; +__s32 rfd; +__s32 wfd; +__u32 flags; +__u8 pad[28 ]; +}; +#define KVM_X86_DISABLE_EXITS_MWAIT (1 << 0)#define KVM_X86_DISABLE_EXITS_HLT (1 << 1)#define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2)@@ -1053,6 +1074,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_X86_USER_SPACE_MSR 188#define KVM_CAP_X86_MSR_FILTER 189#define KVM_CAP_ENFORCE_PV_FEATURE_CPUID 190 +#define KVM_CAP_IOREGIONFD 191#ifdef KVM_CAP_IRQ_ROUTING @@ -1308,6 +1330,7 @@ struct kvm_vfio_spapr_tce { struct kvm_userspace_memory_region)#define KVM_SET_TSS_ADDR _IO(KVMIO, 0x47)#define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO, 0x48, __u64) +#define KVM_SET_IOREGION _IOW(KVMIO, 0x49, struct kvm_ioregion)/* enable ucontrol for s390 */ struct kvm_s390_ucas_mapping {virt/kvm/Kconfig +3
-0@@ -17,6 +17,9 @@ config HAVE_KVM_EVENTFD bool select EVENTFD +config KVM_IOREGION + bool + config KVM_MMIO boolvirt/kvm/eventfd.c +25
-0@@ -27,6 +27,7 @@#include <trace/events/kvm.h> #include <kvm/iodev.h> +#include "ioregion.h" #ifdef CONFIG_HAVE_KVM_IRQFD @@ -755,6 +756,23 @@ static const struct kvm_io_device_ops ioeventfd_ops = { .destructor = ioeventfd_destructor, }; +#ifdef CONFIG_KVM_IOREGION +/* assumes kvm->slots_lock held */ +bool kvm_eventfd_collides (struct kvm *kvm,int bus_idx, +u64 start,u64 size) +{ +struct _ioeventfd *_p; + +list_for_each_entry (_p, &kvm->ioeventfds, list) +if (_p->bus_idx == bus_idx && +overlap (start, size, _p->addr, + !_p->length ?8 : _p->length)) +return true; + +return false; +} +#endif +/* assumes kvm->slots_lock held */ static bool ioeventfd_check_collision (struct kvm *kvm,struct _ioeventfd *p)@@ -770,6 +788,13 @@ ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p) _p->datamatch == p->datamatch))))return true; +#ifdef CONFIG_KVM_IOREGION +if (p->bus_idx ==KVM_MMIO_BUS || p->bus_idx ==KVM_PIO_BUS ) +if (kvm_ioregion_collides (kvm, p->bus_idx, p->addr, + !p->length ?8 : p->length)) +return true; +#endif +return false; }virt/kvm/eventfd.h new file +14
-0@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_EVENTFD_H__ +#define __KVM_EVENTFD_H__ + +#ifdef CONFIG_KVM_IOREGION +bool kvm_eventfd_collides (struct kvm *kvm,int bus_idx,u64 start,u64 size); +#else +static inline bool +kvm_eventfd_collides (struct kvm *kvm,int bus_idx,u64 start,u64 size) +{ +return false; +} +#endif +#endif virt/kvm/ioregion.c new file +4
-0@@ -0,0 +1,233 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <linux/kvm_host.h> +#include <linux/fs.h> +#include <kvm/iodev.h>
virt/kvm/ioregion.c line 14
Quoted 10 lines
virt/kvm/ioregion.c new file +10
-0+#include "eventfd.h" + +void +kvm_ioregionfd_init (struct kvm *kvm) +{ +INIT_LIST_HEAD (&kvm->ioregions_mmio); +INIT_LIST_HEAD (&kvm->ioregions_pio); +} + +struct ioregion {
Please add comments describing the purpose of the fields, locking, etc. For example, the list field is used with kvm->ioregions_mmio/pio. paddr is a guest physical address. size is in bytes. wf is for writing struct ioregion_cmd. rf is for reading struct ioregion_resp.
virt/kvm/ioregion.c line 22
Quoted 8 lines
virt/kvm/ioregion.c new file +8
-0+struct list_head list; +u64 paddr; +u64 size; +struct file *rf; +struct file *wf; +u64 user_data; +struct kvm_io_device dev; +bool posted_writes;
TODO implement posted_writes
Quoted 21 lines
virt/kvm/ioregion.c new file +21
-0+}; + +static inline struct ioregion * +to_ioregion (struct kvm_io_device *dev) +{ + return container_of(dev, struct ioregion, dev); +} + +/* assumes kvm->slots_lock held */ +static void +ioregion_release (struct ioregion *p) +{ +fput (p->rf); +fput (p->wf); +list_del (&p->list); +kfree (p); +} + +static int +ioregion_read (struct kvm_vcpu *vcpu,struct kvm_io_device *this,gpa_t addr, +int len,void *val)
virt/kvm/ioregion.c line 53
Quoted 10 lines
virt/kvm/ioregion.c new file +10
-0+{ +return 0 ; +} + +static int +ioregion_write (struct kvm_vcpu *vcpu,struct kvm_io_device *this,gpa_t addr, +int len,const void *val) +{ +return 0 ; +}
The unimplemented ->read()/->write() should probably return errors.
Quoted 86 lines
virt/kvm/ioregion.c new file +86
-0+ +/* +* This function is called as KVM is completely shutting down. We do not +* need to worry about locking just nuke anything we have as quickly as possible +*/ +static void +ioregion_destructor (struct kvm_io_device *this) +{ +struct ioregion *p =to_ioregion (this); + +ioregion_release (p); +} + +static const struct kvm_io_device_ops ioregion_ops = { + .read = ioregion_read, + .write = ioregion_write, + .destructor = ioregion_destructor, +}; + +static inline struct list_head * +get_ioregion_list (struct kvm *kvm,enum kvm_bus bus_idx) +{ +return (bus_idx ==KVM_MMIO_BUS ) ? + &kvm->ioregions_mmio : &kvm->ioregions_pio; +} + +/* check for not overlapping case and reverse */ +inline bool +overlap (u64 start1,u64 size1,u64 start2,u64 size2) +{ +u64 end1 = start1 + size1 -1 ; +u64 end2 = start2 + size2 -1 ; + +return !(end1 < start2 || start1 >= end2); +} + +/* assumes kvm->slots_lock held */ +bool +kvm_ioregion_collides (struct kvm *kvm,int bus_idx, +u64 start,u64 size) +{ +struct ioregion *_p; +struct list_head *ioregions; + + ioregions =get_ioregion_list (kvm, bus_idx); +list_for_each_entry (_p, ioregions, list) +if (overlap (start, size, _p->paddr, _p->size)) +return true; + +return false; +} + +/* assumes kvm->slots_lock held */ +static bool +ioregion_collision (struct kvm *kvm,struct ioregion *p,enum kvm_bus bus_idx) +{ +if (kvm_ioregion_collides (kvm, bus_idx, p->paddr, p->size) || +kvm_eventfd_collides (kvm, bus_idx, p->paddr, p->size)) +return true; + +return false; +} + +static enum kvm_bus +get_bus_from_flags (__u32 flags) +{ +if (flags &KVM_IOREGION_PIO ) +return KVM_PIO_BUS ; +return KVM_MMIO_BUS ; +} + +int +kvm_set_ioregion (struct kvm *kvm,struct kvm_ioregion *args) +{ +struct ioregion *p; +bool is_posted_writes; +struct file *rfile, *wfile; +enum kvm_bus bus_idx; +int ret =0 ; + +if (!args->memory_size) +return -EINVAL ; +if ((args->guest_paddr + args->memory_size -1 ) < args->guest_paddr) +return -EINVAL ; +if (args->flags & ~KVM_IOREGION_VALID_FLAG_MASK ) +return -EINVAL ;
virt/kvm/ioregion.c line 149
Quoted 10 lines
virt/kvm/ioregion.c new file +10
-0+ + rfile =fget (args->rfd); +if (!rfile) +return -EBADF ; + wfile =fget (args->wfd); +if (!wfile) { +fput (rfile); +return -EBADF ; + } +if ((rfile->f_flags &O_NONBLOCK ) || (wfile->f_flags &O_NONBLOCK )) {
This check prevents most user errors, but the userspace process can still change the file descriptor flags later. Therefore the code needs to be written to fail cleanly on -EAGAIN/-EWOULDBLOCK (no infinite loops or crashes). It's worth noting this in a comment here so others reading the code are aware of this constraint.
Quoted 70 lines
virt/kvm/ioregion.c new file +70
-0+ ret = -EINVAL ; + goto fail; + } + p =kzalloc (sizeof (*p),GFP_KERNEL_ACCOUNT ); +if (!p) { + ret = -ENOMEM ; + goto fail; + } + +INIT_LIST_HEAD (&p->list); + p->paddr = args->guest_paddr; + p->size = args->memory_size; + p->user_data = args->user_data; + p->rf = rfile; + p->wf = wfile; + is_posted_writes = args->flags &KVM_IOREGION_POSTED_WRITES ; + p->posted_writes = is_posted_writes ? true : false; + bus_idx =get_bus_from_flags (args->flags); + +mutex_lock (&kvm->slots_lock); + +if (ioregion_collision (kvm, p, bus_idx)) { + ret = -EEXIST ; + goto unlock_fail; + } +kvm_iodevice_init (&p->dev, &ioregion_ops); + ret =kvm_io_bus_register_dev (kvm, bus_idx, p->paddr, p->size, + &p->dev); +if (ret <0 ) + goto unlock_fail; +list_add_tail (&p->list,get_ioregion_list (kvm, bus_idx)); + +mutex_unlock (&kvm->slots_lock); + +return 0 ; + +unlock_fail: +mutex_unlock (&kvm->slots_lock); +kfree (p); +fail: +fput (rfile); +fput (wfile); + +return ret; +} + +static int +kvm_rm_ioregion (struct kvm *kvm,struct kvm_ioregion *args) +{ +struct ioregion *p, *tmp; +enum kvm_bus bus_idx; +int ret = -ENOENT ; +struct list_head *ioregions; + +if (args->rfd !=-1 || args->wfd !=-1 ) +return -EINVAL ; + + bus_idx =get_bus_from_flags (args->flags); + ioregions =get_ioregion_list (kvm, bus_idx); + +mutex_lock (&kvm->slots_lock); + +list_for_each_entry_safe (p, tmp, ioregions, list) { +if (p->paddr == args->guest_paddr && + p->size == args->memory_size) { +kvm_io_bus_unregister_dev (kvm, bus_idx, &p->dev); +ioregion_release (p); + ret =0 ; +break ; + }
virt/kvm/ioregion.c line 229
Quoted 10 lines
virt/kvm/ioregion.c new file +10
-0+ } + +mutex_unlock (&kvm->slots_lock); + +return ret; +} + +int +kvm_ioregionfd (struct kvm *kvm,struct kvm_ioregion *args) +{
The following check can be done here to make sure the ioctl always returns EINVAL if unsupported flags are passed:
if (args->flags & ~KVM_IOREGION_VALID_FLAG_MASK)
return -EINVAL;
(It's currently missing in kvm_rm_ioregion().)
Quoted 60 lines
virt/kvm/ioregion.c new file +4
-0+if (args->rfd ==-1 || args->wfd ==-1 ) +return kvm_rm_ioregion (kvm, args); +return kvm_set_ioregion (kvm, args); +}virt/kvm/ioregion.h new file +15
-0@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_IOREGION_H__ +#define __KVM_IOREGION_H__ + +#ifdef CONFIG_KVM_IOREGION +inline bool overlap (u64 start1,u64 size1,u64 start2,u64 size2); +bool kvm_ioregion_collides (struct kvm *kvm,int bus_idx,u64 start,u64 size); +#else +static inline bool +kvm_ioregion_collides (struct kvm *kvm,int bus_idx,u64 start,u64 size) +{ +return false; +} +#endif +#endif virt/kvm/kvm_main.c +11
-2@@ -747,6 +747,7 @@ static struct kvm *kvm_create_vm(unsigned long type)mmgrab (current->mm); kvm->mm = current->mm;kvm_eventfd_init (kvm); +kvm_ioregionfd_init (kvm);mutex_init (&kvm->lock);mutex_init (&kvm->irq_lock);mutex_init (&kvm->slots_lock);@@ -3708,6 +3709,16 @@ static long kvm_vm_ioctl(struct file *filp, r =kvm_vm_ioctl_set_memory_region (kvm, &kvm_userspace_mem);break ; } +case KVM_SET_IOREGION : { +struct kvm_ioregion data; + + r = -EFAULT ; +if (copy_from_user (&data, argp,sizeof (data))) + goto out; + + r =kvm_ioregionfd (kvm, &data); +break ; + }case KVM_GET_DIRTY_LOG : {struct kvm_dirty_log log;@@ -4301,9 +4312,12 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,if (!bus)return -ENOMEM ; -/* exclude ioeventfd which is limited by maximum fd */ -if (bus->dev_count - bus->ioeventfd_count >NR_IOBUS_DEVS -1 )
virt/kvm/kvm_main.c int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, · line 4323
Quoted 10 lines
virt/kvm/kvm_main.c +6
-1-return -ENOSPC ; +/* enforce hard limit if kmemcg is disabled and +* exclude ioeventfd which is limited by maximum fd +*/ +if (!memcg_kmem_enabled ()) +if (bus->dev_count - bus->ioeventfd_count >NR_IOBUS_DEVS -1 ) +return -ENOSPC ; new_bus =kmalloc (struct_size (bus, range, bus->dev_count +1 ),GFP_KERNEL_ACCOUNT );
Please move this change to a separate patch.
Re: [RFC 2/2] KVM: add initial support for ioregionfd blocking read/write operations
In reply to Elena AfanasovaQuoted 29 lines · Elena Afanasova
Signed-off-by Elena Afanasova <[email protected]>--- virt/kvm/ioregion.c | 157 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+)virt/kvm/ioregion.c +16
-0@@ -4,6 +4,33 @@#include <kvm/iodev.h> #include "eventfd.h" +/* Wire protocol */ +struct ioregionfd_cmd { +__u32 info; +__u32 padding; +__u64 user_data; +__u64 offset; +__u64 data; +}; + +struct ioregionfd_resp { +__u64 data; +__u8 pad[24 ]; +}; + +#define IOREGIONFD_CMD_READ 0 +#define IOREGIONFD_CMD_WRITE 1
virt/kvm/ioregion.c line 32
Quoted 10 lines
virt/kvm/ioregion.c +10
-0+ +#define IOREGIONFD_SIZE_8BIT 0 +#define IOREGIONFD_SIZE_16BIT 1 +#define IOREGIONFD_SIZE_32BIT 2 +#define IOREGIONFD_SIZE_64BIT 3 + +#define IOREGIONFD_SIZE_OFFSET 4 +#define IOREGIONFD_RESP_OFFSET 6 +#define IOREGIONFD_SIZE (x) ((x) << IOREGIONFD_SIZE_OFFSET) +#define IOREGIONFD_RESP (x) ((x) << IOREGIONFD_RESP_OFFSET)
These belong in the uapi header so userspace also has struct ioregionfd_cmd, struct ioregionfd_resp, etc.
Quoted 24 lines
virt/kvm/ioregion.c +17
-0+void kvm_ioregionfd_init (struct kvm *kvm) {@@ -38,10 +65,100 @@ ioregion_release(struct ioregion *p)kfree (p); } +static bool +pack_cmd (struct ioregionfd_cmd *cmd,u64 offset,u64 len,int opt,bool resp, +u64 user_data,const void *val) +{ +u64 size =0 ; + +switch (len) { +case 1 : + size =IOREGIONFD_SIZE_8BIT ; + *((u8 *)&cmd->data) = val ? *(u8 *)val :0 ; +break ; +case 2 : + size =IOREGIONFD_SIZE_16BIT ; + *((u16 *)&cmd->data) = val ? *(u16 *)val :0 ; +break ; +case 4 :
virt/kvm/ioregion.c ioregion_release(struct ioregion *p) · line 93
Quoted 10 lines
virt/kvm/ioregion.c +9
-0+ size =IOREGIONFD_SIZE_32BIT ; + *((u32 *)&cmd->data) = val ? *(u32 *)val :0 ; +break ; +case 8 : + size =IOREGIONFD_SIZE_64BIT ; + *((u64 *)&cmd->data) = val ? *(u64 *)val :0 ; +break ; +default : +return false;
The assignments and casts can be replaced with a single memcpy after the switch statement. This is also how KVM_EXIT_MMIO and Coalesced MMIO do it:
memcpy(cmd->data, val, len);
However, we need to make sure that cmd has been zeroed so that kernel memory is not accidentally exposed to userspace.
+ }
Quoted 12 lines
virt/kvm/ioregion.c +9
-0+ cmd->user_data = user_data; + cmd->offset = offset; + cmd->info |= opt; + cmd->info |=IOREGIONFD_SIZE (size); + cmd->info |=IOREGIONFD_RESP (resp); + +return true; +} +static int ioregion_read (struct kvm_vcpu *vcpu,struct kvm_io_device *this,gpa_t addr,int len,void *val)
virt/kvm/ioregion.c ioregion_release(struct ioregion *p) · line 115
Quoted 10 lines
virt/kvm/ioregion.c +9
-0{ +struct ioregion *p =to_ioregion (this); +struct ioregionfd_cmd *cmd; +struct ioregionfd_resp *resp; +size_t buf_size; +void *buf; +int ret =0 ; + +if ((p->rf->f_flags &O_NONBLOCK ) || (p->wf->f_flags &O_NONBLOCK )) +return -EINVAL ;
Another CPU could change file descriptor flags while we are running. Therefore it might be simplest to handle kernel_write() and kernel_read() -EAGAIN return values instead of trying to check.
virt/kvm/ioregion.c ioregion_release(struct ioregion *p) · line 124
Quoted 9 lines
virt/kvm/ioregion.c +9
-0+if ((addr + len -1 ) > (p->paddr + p->size -1 )) +return -EINVAL ; + + buf_size =max_t (size_t,sizeof (*cmd),sizeof (*resp)); + buf =kzalloc (buf_size,GFP_KERNEL ); +if (!buf) +return -ENOMEM ; + cmd = (struct ioregionfd_cmd *)buf; + resp = (struct ioregionfd_resp *)buf;
I think they are small enough to declare them on the stack:
union {
struct ioregionfd_cmd cmd;
struct ioregionfd_resp resp;
} buf;
memset(&buf, 0, sizeof(buf));Quoted 24 lines
virt/kvm/ioregion.c +24
-0+if (!pack_cmd (cmd, addr - p->paddr, len,IOREGIONFD_CMD_READ , +1 , p->user_data,NULL )) { +kfree (buf); +return -EOPNOTSUPP ; + } + + ret =kernel_write (p->wf, cmd,sizeof (*cmd),0 ); +if (ret !=sizeof (*cmd)) { +kfree (buf); +return (ret <0 ) ? ret : -EIO ; + } +memset (buf,0 , buf_size); + ret =kernel_read (p->rf, resp,sizeof (*resp),0 ); +if (ret !=sizeof (*resp)) { +kfree (buf); +return (ret <0 ) ? ret : -EIO ; + } + +switch (len) { +case 1 : + *(u8 *)val = (u8 )resp->data; +break ; +case 2 : + *(u16 *)val = (u16 )resp->data;
virt/kvm/ioregion.c ioregion_release(struct ioregion *p) · line 158
Quoted 10 lines
virt/kvm/ioregion.c +10
-0+break ; +case 4 : + *(u32 *)val = (u32 )resp->data; +break ; +case 8 : + *(u64 *)val = (u64 )resp->data; +break ; +default : +break ; + }
This looks inconsistent. cmd->data is treated as a packed u8/u16/u32/864 whereas resp->data is treated as u64?
I was expecting memcpy(val, &resp->data, len) here not the u64 -> u8/u16/u32/u64 conversion.
Quoted 5 lines · Elena Afanasova
This patchset introduces a KVM dispatch mechanism which can be used for handling MMIO/PIO accesses over file descriptors without returning from
ioctl(KVM_RUN). This allows device emulation to run in another task separate from the vCPU task.
Quoted 7 lines
This is achieved through KVM vm ioctl for registering MMIO/PIO regions and a wire protocol that KVM uses to communicate with a task handling an MMIO/PIO access.
ioregionfd relies on kmemcg in order to limit the amount of kernel memory that userspace can consume. Can
NR_IOBUS_DEVShardcoded limit be enforced only in case kmemcg is disabled?
Thanks for sharing this! Can you describe the todos? I noticed some in Patch 1 and highlighted them. In addition:
* Signal handling when the vCPU thread is interrupted in kernel_read()/kernel_write()
Quoted 23 lines
Elena Afanasova (2):
KVM: add initial support forKVM_SET_IOREGION
KVM: add initial support for ioregionfd blocking read/write operationsarch/x86/kvm/Kconfig | 1 + arch/x86/kvm/Makefile | 1 + arch/x86/kvm/x86.c | 1 + include/linux/kvm_host.h | 17 ++ include/uapi/linux/kvm.h | 23 +++ virt/kvm/Kconfig | 3 + virt/kvm/eventfd.c | 25 +++ virt/kvm/eventfd.h | 14 ++ virt/kvm/ioregion.c | 390 +++++++++++++++++++++++++++++++++++++++ virt/kvm/ioregion.h | 15 ++ virt/kvm/kvm_main.c | 20 +- 11 files changed, 507 insertions(+), 3 deletions(-) create mode 100644 virt/kvm/eventfd.h create mode 100644 virt/kvm/ioregion.c create mode 100644 virt/kvm/ioregion.hSignature · 1 line
2.25.1
kernel test robot
build botRe: [RFC 1/2] KVM: add initial support for KVM_SET_IOREGION
In reply to Elena Afanasova[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch]
Hi Elena,
[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on vhost/linux-next]
[cannot apply to kvm/linux-next v5.11-rc1 next-20201223]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Elena-Afanasova/Introduce-MMIO-PIO-dispatch-file-descriptors-ioregionfd/20201229-181412 base: https://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git linux-next config: x86_64-randconfig-a002-20201229 (attached as .config) compiler: gcc-9 (Debian 9.3.0-15) 9.3.0 reproduce (this is a W=1 build): # https://github.com/0day-ci/linux/commit/653378d23b958930c2aab283243088b2684d34b8 git remote add linux-review https://github.com/0day-ci/linux git fetch --no-tags linux-review Elena-Afanasova/Introduce-MMIO-PIO-dispatch-file-descriptors-ioregionfd/20201229-181412 git checkout 653378d23b958930c2aab283243088b2684d34b8 # save the attached .config to linux build tree make W=1 ARCH=x86_64
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
>> arch/x86/kvm/../../../virt/kvm/eventfd.c:761:6: warning: no previous prototype for 'kvm_eventfd_collides' [-Wmissing-prototypes]
761 | bool kvm_eventfd_collides(struct kvm *kvm, int bus_idx,
| ^~~~~~~~~~~~~~~~~~~~
--
>> arch/x86/kvm/../../../virt/kvm/ioregion.c:92:1: warning: no previous prototype for 'kvm_ioregion_collides' [-Wmissing-prototypes]
92 | kvm_ioregion_collides(struct kvm *kvm, int bus_idx,
| ^~~~~~~~~~~~~~~~~~~~~
>> arch/x86/kvm/../../../virt/kvm/ioregion.c:126:1: warning: no previous prototype for 'kvm_set_ioregion' [-Wmissing-prototypes] 126 | kvm_set_ioregion(struct kvm *kvm, struct kvm_ioregion *args)
| ^~~~~~~~~~~~~~~~
vim +/kvm_eventfd_collides +761 arch/x86/kvm/../../../virt/kvm/eventfd.c
758
759 #ifdef CONFIG_KVM_IOREGION
760 /* assumes kvm->slots_lock held */
> 761 bool kvm_eventfd_collides(struct kvm *kvm, int bus_idx,
762 u64 start, u64 size)
763 {
764 struct _ioeventfd *_p;
765
766 list_for_each_entry(_p, &kvm->ioeventfds, list)
767 if (_p->bus_idx == bus_idx &&
768 overlap(start, size, _p->addr,
769 !_p->length ? 8 : _p->length))
770 return true;
771
772 return false;
773 }
774 #endif
775
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Re: [RFC 1/2] KVM: add initial support for KVM_SET_IOREGION
In reply to Stefan HajnocziQuoted 268 lines · Stefan Hajnoczi
Quoted 29 lines · Elena Afanasova
This vm ioctl adds or removes an ioregionfd MMIO/PIO region. Guest read and write accesses are dispatched through the given ioregionfd instead of returning from
ioctl(KVM_RUN). Regions can be deleted by setting fds to -1.Signed-off-by Elena Afanasova <[email protected]>--- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/Makefile | 1 + arch/x86/kvm/x86.c | 1 + include/linux/kvm_host.h | 17 +++ include/uapi/linux/kvm.h | 23 ++++ virt/kvm/Kconfig | 3 + virt/kvm/eventfd.c | 25 +++++ virt/kvm/eventfd.h | 14 +++ virt/kvm/ioregion.c | 233 +++++++++++++++++++++++++++++++++++++++ virt/kvm/ioregion.h | 15 +++ virt/kvm/kvm_main.c | 20 +++- 11 files changed, 350 insertions(+), 3 deletions(-) create mode 100644 virt/kvm/eventfd.h create mode 100644 virt/kvm/ioregion.c create mode 100644 virt/kvm/ioregion.harch/x86/kvm/Kconfig +0
-0arch/x86/kvm/Kconfig config KVM · line 39
Quoted 7 lines
arch/x86/kvm/Kconfig +1
-0@@ -33,6 +33,7 @@ config KVM select HAVE_KVM_IRQ_BYPASS select HAVE_KVM_IRQ_ROUTING select HAVE_KVM_EVENTFD + select KVM_IOREGION select KVM_ASYNC_PF select USER_RETURN_NOTIFIER select KVM_MMIOTODO non-x86 arch support
Quoted 217 lines
arch/x86/kvm/Makefile +0
-0@@ -12,6 +12,7 @@ KVM := ../../../virt/kvm kvm-y += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o \ $(KVM)/eventfd.o $(KVM)/irqchip.o$(KVM)/vfio.o
kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o
+kvm-$(CONFIG_KVM_IOREGION) += $(KVM)/ioregion.okvm-y += x86.o emulate.o i8259.o irq.o
lapic.o \
i8254.o ioapic.o irq_comm.o cpuid.o pmu.o
mtrr.o \arch/x86/kvm/x86.c +1
-0@@ -3739,6 +3739,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_X86_USER_SPACE_MSR :case KVM_CAP_X86_MSR_FILTER :case KVM_CAP_ENFORCE_PV_FEATURE_CPUID : +case KVM_CAP_IOREGIONFD : r =1 ;break ;arch/x86/kvm/x86.c +0
-0case KVM_CAP_SYNC_REGS :include/linux/kvm_host.h +17
-0@@ -470,6 +470,10 @@ struct kvm { struct mutex resampler_lock; } irqfds;struct list_head ioeventfds; +#endif +#ifdef CONFIG_KVM_IOREGION +struct list_head ioregions_mmio; +struct list_head ioregions_pio;#endif struct kvm_vm_stat stat;struct kvm_arch arch;@@ -1262,6 +1266,19 @@ static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)#endif /* CONFIG_HAVE_KVM_EVENTFD */ +#ifdef CONFIG_KVM_IOREGION +void kvm_ioregionfd_init (struct kvm *kvm); +int kvm_ioregionfd (struct kvm *kvm,struct kvm_ioregion *args); + +#else + +static inline void kvm_ioregionfd_init (struct kvm *kvm) {} +static inline int kvm_ioregionfd (struct kvm *kvm,struct kvm_ioregion *args) +{ +return -ENOSYS ; +} +#endif +void kvm_arch_irq_routing_update (struct kvm *kvm);static inline void kvm_make_request(int req, struct
kvm_vcpu
*vcpu)include/uapi/linux/kvm.h +21
-0@@ -732,6 +732,27 @@ struct kvm_ioeventfd {__u8 pad[36 ]; }; +enum { + kvm_ioregion_flag_nr_pio, + kvm_ioregion_flag_nr_posted_writes, + kvm_ioregion_flag_nr_max, +}; + +#define KVM_IOREGION_PIO (1 << kvm_ioregion_flag_nr_pio) +#define KVM_IOREGION_POSTED_WRITES (1 << kvm_ioregion_flag_nr_posted_writes) + +#define KVM_IOREGION_VALID_FLAG_MASK ((1 << kvm_ioregion_flag_nr_max) - 1) + +struct kvm_ioregion { +__u64 guest_paddr;/* guest physical address */ +__u64 memory_size;/* bytes */ +__u64 user_data; +__s32 rfd; +__s32 wfd; +__u32 flags; +__u8 pad[28 ]; +}; +#define KVM_X86_DISABLE_EXITS_MWAIT (1 << 0)#define
KVM_X86_DISABLE_EXITS_HLT(1 << 1)
#defineKVM_X86_DISABLE_EXITS_PAUSE(1 << 2)include/uapi/linux/kvm.h +2
-0@@ -1053,6 +1074,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_X86_USER_SPACE_MSR 188#define KVM_CAP_X86_MSR_FILTER 189#define KVM_CAP_ENFORCE_PV_FEATURE_CPUID 190 +#define KVM_CAP_IOREGIONFD 191#ifdef KVM_CAP_IRQ_ROUTING @@ -1308,6 +1330,7 @@ struct kvm_vfio_spapr_tce { struct kvm_userspace_memory_region)#define KVM_SET_TSS_ADDR _IO(KVMIO, 0x47)#define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO, 0x48, __u64) +#define KVM_SET_IOREGION _IOW(KVMIO, 0x49, struct kvm_ioregion)/* enable ucontrol for s390 */
structkvm_s390_ucas_mapping{virt/kvm/Kconfig +3
-0@@ -17,6 +17,9 @@ config HAVE_KVM_EVENTFD bool select EVENTFD +config KVM_IOREGION + bool + config KVM_MMIO boolvirt/kvm/eventfd.c +18
-0@@ -27,6 +27,7 @@#include <trace/events/kvm.h> #include <kvm/iodev.h> +#include "ioregion.h" #ifdef CONFIG_HAVE_KVM_IRQFD @@ -755,6 +756,23 @@ static const struct kvm_io_device_ops ioeventfd_ops = { .destructor = ioeventfd_destructor, }; +#ifdef CONFIG_KVM_IOREGION +/* assumes kvm->slots_lock held */ +bool kvm_eventfd_collides (struct kvm *kvm,int bus_idx, +u64 start,u64 size) +{ +struct _ioeventfd *_p; + +list_for_each_entry (_p, &kvm->ioeventfds, list) +if (_p->bus_idx == bus_idx && +overlap (start, size, _p->addr, + !_p->length ?8 : _p->length)) +return true; + +return false; +} +#endif +/* assumes kvm->slots_lock held */ static bool
ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p)virt/kvm/eventfd.c +7
-0@@ -770,6 +788,13 @@ ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p) _p->datamatch == p->datamatch)))) return true; +#ifdef CONFIG_KVM_IOREGION +if (p->bus_idx ==KVM_MMIO_BUS || p->bus_idx ==KVM_PIO_BUS ) +if (kvm_ioregion_collides (kvm, p->bus_idx, p->addr, + !p->length ?8 : p->length)) +return true; +#endif +return false; }virt/kvm/eventfd.h new file +6
-0@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_EVENTFD_H__ +#define __KVM_EVENTFD_H__ + +#ifdef CONFIG_KVM_IOREGION +bool kvm_eventfd_collides(struct kvm *kvm, int bus_idx, u64 start,u64 size);
virt/kvm/eventfd.h new file +3
-0+#else +static inline bool +kvm_eventfd_collides(struct kvm *kvm, int bus_idx, u64 start, u64size)
virt/kvm/eventfd.h new file +5
-0+{ +return false; +} +#endif +#endif virt/kvm/ioregion.c new file +4
-0@@ -0,0 +1,233 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <linux/kvm_host.h> +#include <linux/fs.h> +#include <kvm/iodev.h> virt/kvm/ioregion.c line 14
Quoted 10 lines
virt/kvm/ioregion.c new file +10
-0+#include "eventfd.h" + +void +kvm_ioregionfd_init (struct kvm *kvm) +{ +INIT_LIST_HEAD (&kvm->ioregions_mmio); +INIT_LIST_HEAD (&kvm->ioregions_pio); +} + +struct ioregion {
Quoted 8 lines
Please add comments describing the purpose of the fields, locking, etc.
For example, the list field is used with kvm->ioregions_mmio/pio. paddr
is a guest physical address. size is in bytes. wf is for writing structioregion_cmd. rf is for reading structioregion_resp.
Ok
Quoted 38 lines
virt/kvm/ioregion.c line 22
Quoted 8 lines
virt/kvm/ioregion.c new file +8
-0+struct list_head list; +u64 paddr; +u64 size; +struct file *rf; +struct file *wf; +u64 user_data; +struct kvm_io_device dev; +bool posted_writes;TODO implement
posted_writesQuoted 27 lines
virt/kvm/ioregion.c new file +20
-0+}; + +static inline struct ioregion * +to_ioregion (struct kvm_io_device *dev) +{ + return container_of(dev, struct ioregion, dev); +} + +/* assumes kvm->slots_lock held */ +static void +ioregion_release (struct ioregion *p) +{ +fput (p->rf); +fput (p->wf); +list_del (&p->list); +kfree (p); +} + +static int +ioregion_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
gpa_taddr,virt/kvm/ioregion.c new file +6
-0+ int len, void *val) +{ +return 0 ; +} + +static int
Quoted 9 lines
virt/kvm/ioregion.c line 53
Quoted 6 lines
+ioregion_write(struct
kvm_vcpu*vcpu, structkvm_io_device*this,gpa_taddr,virt/kvm/ioregion.c new file +4
-0+ int len, const void *val) +{ +return 0 ; +}The unimplemented ->read()/->write() should probably return errors.
You're right, it would be better
Quoted 102 lines
Quoted 100 lines
virt/kvm/ioregion.c new file +3
-0+ +/* +* This function is called as KVM is completely shutting down. We do not
+ * need to worry about locking just nuke anything we have as quickly as possible
+ */virt/kvm/ioregion.c new file +50
-0+static void +ioregion_destructor (struct kvm_io_device *this) +{ +struct ioregion *p =to_ioregion (this); + +ioregion_release (p); +} + +static const struct kvm_io_device_ops ioregion_ops = { + .read = ioregion_read, + .write = ioregion_write, + .destructor = ioregion_destructor, +}; + +static inline struct list_head * +get_ioregion_list (struct kvm *kvm,enum kvm_bus bus_idx) +{ +return (bus_idx ==KVM_MMIO_BUS ) ? + &kvm->ioregions_mmio : &kvm->ioregions_pio; +} + +/* check for not overlapping case and reverse */ +inline bool +overlap (u64 start1,u64 size1,u64 start2,u64 size2) +{ +u64 end1 = start1 + size1 -1 ; +u64 end2 = start2 + size2 -1 ; + +return !(end1 < start2 || start1 >= end2); +} + +/* assumes kvm->slots_lock held */ +bool +kvm_ioregion_collides (struct kvm *kvm,int bus_idx, +u64 start,u64 size) +{ +struct ioregion *_p; +struct list_head *ioregions; + + ioregions =get_ioregion_list (kvm, bus_idx); +list_for_each_entry (_p, ioregions, list) +if (overlap (start, size, _p->paddr, _p->size)) +return true; + +return false; +} + +/* assumes kvm->slots_lock held */ +static bool +ioregion_collision(struct kvm *kvm, struct ioregion *p, enum
kvm_busbus_idx)virt/kvm/ioregion.c new file +28
-0+{ +if (kvm_ioregion_collides (kvm, bus_idx, p->paddr, p->size) || +kvm_eventfd_collides (kvm, bus_idx, p->paddr, p->size)) +return true; + +return false; +} + +static enum kvm_bus +get_bus_from_flags (__u32 flags) +{ +if (flags &KVM_IOREGION_PIO ) +return KVM_PIO_BUS ; +return KVM_MMIO_BUS ; +} + +int +kvm_set_ioregion(struct kvm *kvm, struct kvm_ioregion *args) +{ + struct ioregion *p; + bool is_posted_writes; + struct file *rfile, *wfile; + enum kvm_bus bus_idx; + int ret = 0; + + if (!args->memory_size) + return -EINVAL; + if ((args->guest_paddr + args->memory_size - 1) < args-Quoted 1 line
guest_paddr)virt/kvm/ioregion.c new file +13
-0+return -EINVAL ; +if (args->flags & ~KVM_IOREGION_VALID_FLAG_MASK ) +return -EINVAL ; + + rfile =fget (args->rfd); +if (!rfile) +return -EBADF ; + wfile =fget (args->wfd); +if (!wfile) { +fput (rfile); +return -EBADF ; + } + if ((rfile->f_flags & O_NONBLOCK) || (wfile->f_flags &Quoted 1 line
O_NONBLOCK)) {
Quoted 9 lines
This check prevents most user errors, but the userspace process can still change the file descriptor flags later. Therefore the code needs
to be written to fail cleanly on -EAGAIN/-EWOULDBLOCK (no infinite loops
or crashes). It's worth noting this in a comment here so others reading
the code are aware of this constraint.
Ok, I'll fix this, thanks!
Quoted 85 lines
Quoted 71 lines
virt/kvm/ioregion.c new file +66
-0+ ret = -EINVAL ; + goto fail; + } + p =kzalloc (sizeof (*p),GFP_KERNEL_ACCOUNT ); +if (!p) { + ret = -ENOMEM ; + goto fail; + } + +INIT_LIST_HEAD (&p->list); + p->paddr = args->guest_paddr; + p->size = args->memory_size; + p->user_data = args->user_data; + p->rf = rfile; + p->wf = wfile; + is_posted_writes = args->flags &KVM_IOREGION_POSTED_WRITES ; + p->posted_writes = is_posted_writes ? true : false; + bus_idx =get_bus_from_flags (args->flags); + +mutex_lock (&kvm->slots_lock); + +if (ioregion_collision (kvm, p, bus_idx)) { + ret = -EEXIST ; + goto unlock_fail; + } +kvm_iodevice_init (&p->dev, &ioregion_ops); + ret =kvm_io_bus_register_dev (kvm, bus_idx, p->paddr, p->size, + &p->dev); +if (ret <0 ) + goto unlock_fail; +list_add_tail (&p->list,get_ioregion_list (kvm, bus_idx)); + +mutex_unlock (&kvm->slots_lock); + +return 0 ; + +unlock_fail: +mutex_unlock (&kvm->slots_lock); +kfree (p); +fail: +fput (rfile); +fput (wfile); + +return ret; +} + +static int +kvm_rm_ioregion (struct kvm *kvm,struct kvm_ioregion *args) +{ +struct ioregion *p, *tmp; +enum kvm_bus bus_idx; +int ret = -ENOENT ; +struct list_head *ioregions; + +if (args->rfd !=-1 || args->wfd !=-1 ) +return -EINVAL ; + + bus_idx =get_bus_from_flags (args->flags); + ioregions =get_ioregion_list (kvm, bus_idx); + +mutex_lock (&kvm->slots_lock); + +list_for_each_entry_safe (p, tmp, ioregions, list) { +if (p->paddr == args->guest_paddr && + p->size == args->memory_size) { + kvm_io_bus_unregister_dev(kvm, bus_idx, &p-Quoted 1 line
dev);
virt/kvm/ioregion.c new file +4
-0+ioregion_release (p); + ret =0 ; +break ; + }virt/kvm/ioregion.c line 229
Quoted 10 lines
virt/kvm/ioregion.c new file +10
-0+ } + +mutex_unlock (&kvm->slots_lock); + +return ret; +} + +int +kvm_ioregionfd (struct kvm *kvm,struct kvm_ioregion *args) +{The following check can be done here to make sure the ioctl always returns EINVAL if unsupported flags are passed:
Quoted 5 lines
if (
args->flags& ~KVM_IOREGION_VALID_FLAG_MASK)
return-EINVAL;(It's currently missing in
kvm_rm_ioregion().)
Ok
Quoted 74 lines
Quoted 74 lines
virt/kvm/ioregion.c new file +4
-0+if (args->rfd ==-1 || args->wfd ==-1 ) +return kvm_rm_ioregion (kvm, args); +return kvm_set_ioregion (kvm, args); +}virt/kvm/ioregion.h new file +7
-0@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_IOREGION_H__ +#define __KVM_IOREGION_H__ + +#ifdef CONFIG_KVM_IOREGION +inline bool overlap (u64 start1,u64 size1,u64 start2,u64 size2); +bool kvm_ioregion_collides(struct kvm *kvm, int bus_idx, u64start, u64 size);
virt/kvm/ioregion.h new file +3
-0+#else +static inline bool +kvm_ioregion_collides(struct kvm *kvm, int bus_idx, u64 start, u64size)
virt/kvm/ioregion.h new file +5
-0+{ +return false; +} +#endif +#endif virt/kvm/kvm_main.c +1
-0@@ -747,6 +747,7 @@ static struct kvm *kvm_create_vm(unsigned long type) mmgrab(current->mm); kvm->mm = current->mm;kvm_eventfd_init (kvm); +kvm_ioregionfd_init (kvm);mutex_init (&kvm->lock);mutex_init (&kvm->irq_lock);virt/kvm/kvm_main.c +16
-3mutex_init (&kvm->slots_lock);@@ -3708,6 +3709,16 @@ static long kvm_vm_ioctl(struct file *filp, r =kvm_vm_ioctl_set_memory_region (kvm, &kvm_userspace_mem);break ; } +case KVM_SET_IOREGION : { +struct kvm_ioregion data; + + r = -EFAULT ; +if (copy_from_user (&data, argp,sizeof (data))) + goto out; + + r =kvm_ioregionfd (kvm, &data); +break ; + }case KVM_GET_DIRTY_LOG : {struct kvm_dirty_log log;@@ -4301,9 +4312,12 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, if (!bus) return -ENOMEM; -/* exclude ioeventfd which is limited by maximum fd */ -if (bus->dev_count - bus->ioeventfd_count >NR_IOBUS_DEVS -1 ) -return -ENOSPC ; +/* enforce hard limit if kmemcg is disabled and +* exclude ioeventfd which is limited by maximum fd +*/ +if (!memcg_kmem_enabled ()) +if (bus->dev_count - bus->ioeventfd_count >NR_IOBUS_DEVS -1 ) +return -ENOSPC ;
Quoted 4 lines
virt/kvm/kvm_main.c int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, · line 4323
Quoted 2 lines
virt/kvm/kvm_main.c +0
-0new_bus =kmalloc (struct_size (bus, range, bus->dev_count +1 ),GFP_KERNEL_ACCOUNT );Please move this change to a separate patch.
Ok
Re: [RFC 2/2] KVM: add initial support for ioregionfd blocking read/write operations
In reply to Stefan HajnocziQuoted 36 lines · Stefan Hajnoczi
Quoted 35 lines · Elena Afanasova
Signed-off-by Elena Afanasova <[email protected]>--- virt/kvm/ioregion.c | 157 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+)virt/kvm/ioregion.c +21
-0@@ -4,6 +4,33 @@#include <kvm/iodev.h> #include "eventfd.h" +/* Wire protocol */ +struct ioregionfd_cmd { +__u32 info; +__u32 padding; +__u64 user_data; +__u64 offset; +__u64 data; +}; + +struct ioregionfd_resp { +__u64 data; +__u8 pad[24 ]; +}; + +#define IOREGIONFD_CMD_READ 0 +#define IOREGIONFD_CMD_WRITE 1 + +#define IOREGIONFD_SIZE_8BIT 0 +#define IOREGIONFD_SIZE_16BIT 1 +#define IOREGIONFD_SIZE_32BIT 2 +#define IOREGIONFD_SIZE_64BIT 3
Quoted 9 lines
virt/kvm/ioregion.c line 32
Quoted 5 lines
virt/kvm/ioregion.c +5
-0+ +#define IOREGIONFD_SIZE_OFFSET 4 +#define IOREGIONFD_RESP_OFFSET 6 +#define IOREGIONFD_SIZE (x) ((x) << IOREGIONFD_SIZE_OFFSET) +#define IOREGIONFD_RESP (x) ((x) << IOREGIONFD_RESP_OFFSET)These belong in the uapi header so userspace also has struct
ioregionfd_cmd, structioregionfd_resp, etc.
I'll move the wire protocol to a separate uapi header
Quoted 42 lines
Quoted 25 lines
virt/kvm/ioregion.c +17
-0+void kvm_ioregionfd_init (struct kvm *kvm) {@@ -38,10 +65,100 @@ ioregion_release(struct ioregion *p)kfree (p); } +static bool +pack_cmd (struct ioregionfd_cmd *cmd,u64 offset,u64 len,int opt,bool resp, +u64 user_data,const void *val) +{ +u64 size =0 ; + +switch (len) { +case 1 : + size =IOREGIONFD_SIZE_8BIT ; + *((u8 *)&cmd->data) = val ? *(u8 *)val :0 ; +break ; +case 2 : + size =IOREGIONFD_SIZE_16BIT ; + *((u16 *)&cmd->data) = val ? *(u16 *)val :0 ; +break ; +case 4 :virt/kvm/ioregion.c ioregion_release(struct ioregion *p) · line 94
Quoted 10 lines
virt/kvm/ioregion.c +9
-0+ size =IOREGIONFD_SIZE_32BIT ; + *((u32 *)&cmd->data) = val ? *(u32 *)val :0 ; +break ; +case 8 : + size =IOREGIONFD_SIZE_64BIT ; + *((u64 *)&cmd->data) = val ? *(u64 *)val :0 ; +break ; +default : +return false;The assignments and casts can be replaced with a single memcpy after the
switch statement. This is also howKVM_EXIT_MMIOand Coalesced MMIO do
it:
Quoted 2 lines
memcpy(cmd->data, val, len);
Thanks for pointing it out
Quoted 24 lines
However, we need to make sure that cmd has been zeroed so that kernel memory is not accidentally exposed to userspace.
+ }
Quoted 20 lines
virt/kvm/ioregion.c +15
-0+ cmd->user_data = user_data; + cmd->offset = offset; + cmd->info |= opt; + cmd->info |=IOREGIONFD_SIZE (size); + cmd->info |=IOREGIONFD_RESP (resp); + +return true; +} +static int ioregion_read (struct kvm_vcpu *vcpu,struct kvm_io_device *this,gpa_t addr,int len,void *val) { +struct ioregion *p =to_ioregion (this); +struct ioregionfd_cmd *cmd; +struct ioregionfd_resp *resp; +size_t buf_size; +void *buf; +int ret =0 ;
Quoted 9 lines
virt/kvm/ioregion.c
Quoted 4 lines
virt/kvm/ioregion.c +2
-0+ + if ((p->rf->f_flags & O_NONBLOCK) || (p->wf->f_flags &
O_NONBLOCK))virt/kvm/ioregion.c +1
-0+return -EINVAL ;Another CPU could change file descriptor flags while we are running. Therefore it might be simplest to handle
kernel_write()andkernel_read()-EAGAINreturn values instead of trying to check.
Ok, I'll fix this
Quoted 12 lines
virt/kvm/ioregion.c ioregion_release(struct ioregion *p) · line 124
Quoted 9 lines
virt/kvm/ioregion.c +9
-0+if ((addr + len -1 ) > (p->paddr + p->size -1 )) +return -EINVAL ; + + buf_size =max_t (size_t,sizeof (*cmd),sizeof (*resp)); + buf =kzalloc (buf_size,GFP_KERNEL ); +if (!buf) +return -ENOMEM ; + cmd = (struct ioregionfd_cmd *)buf; + resp = (struct ioregionfd_resp *)buf;I think they are small enough to declare them on the stack:
Quoted 7 lines
union {
struct ioregionfd_cmd cmd;
struct ioregionfd_resp resp;
} buf;
memset(&buf, 0, sizeof(buf));Ok
Quoted 39 lines
Quoted 24 lines
virt/kvm/ioregion.c +24
-0+if (!pack_cmd (cmd, addr - p->paddr, len,IOREGIONFD_CMD_READ , +1 , p->user_data,NULL )) { +kfree (buf); +return -EOPNOTSUPP ; + } + + ret =kernel_write (p->wf, cmd,sizeof (*cmd),0 ); +if (ret !=sizeof (*cmd)) { +kfree (buf); +return (ret <0 ) ? ret : -EIO ; + } +memset (buf,0 , buf_size); + ret =kernel_read (p->rf, resp,sizeof (*resp),0 ); +if (ret !=sizeof (*resp)) { +kfree (buf); +return (ret <0 ) ? ret : -EIO ; + } + +switch (len) { +case 1 : + *(u8 *)val = (u8 )resp->data; +break ; +case 2 : + *(u16 *)val = (u16 )resp->data;virt/kvm/ioregion.c ioregion_release(struct ioregion *p) · line 158
Quoted 10 lines
virt/kvm/ioregion.c +10
-0+break ; +case 4 : + *(u32 *)val = (u32 )resp->data; +break ; +case 8 : + *(u64 *)val = (u64 )resp->data; +break ; +default : +break ; + }This looks inconsistent.
cmd->datais treated as a packed u8/u16/u32/864
whereasresp->datais treated as u64?
Quoted 2 lines
I was expecting
memcpy(val, &resp->data, len)here not the u64 -> u8/u16/u32/u64 conversion.
I'll fix this, thanks!
Quoted 21 lines · Stefan Hajnoczi
Quoted 14 lines · Elena Afanasova
This patchset introduces a KVM dispatch mechanism which can be used
for handling MMIO/PIO accesses over file descriptors without returning
fromioctl(KVM_RUN). This allows device emulation to run in another task
separate from the vCPU task.This is achieved through KVM vm ioctl for registering MMIO/PIO regions and
a wire protocol that KVM uses to communicate with a task handling an
MMIO/PIO access.Quoted 5 lines
ioregionfd relies on kmemcg in order to limit the amount of kernel memory
that userspace can consume. CanNR_IOBUS_DEVShardcoded limit be enforced
only in case kmemcg is disabled?
Quoted 6 lines
Thanks for sharing this! Can you describe the todos? I noticed some in
Patch 1 and highlighted them. In addition:
* Signal handling when the vCPU thread is interrupted in kernel_read()/kernel_write()
TODOs:
* Signal handling when the vCPU thread is interrupted in kernel_read()/kernel_write()
* Add ioregionfd cmds/replies serialization
* Implement KVM_EXIT_IOREGIONFD_FAILURE
* Add non-x86 arch support
* Add kvm-unittests
Quoted 25 lines
Quoted 25 lines
Elena Afanasova (2):
KVM: add initial support forKVM_SET_IOREGION
KVM: add initial support for ioregionfd blocking read/write
operationsarch/x86/kvm/Kconfig | 1 + arch/x86/kvm/Makefile | 1 + arch/x86/kvm/x86.c | 1 + include/linux/kvm_host.h | 17 ++ include/uapi/linux/kvm.h | 23 +++ virt/kvm/Kconfig | 3 + virt/kvm/eventfd.c | 25 +++ virt/kvm/eventfd.h | 14 ++ virt/kvm/ioregion.c | 390 +++++++++++++++++++++++++++++++++++++++ virt/kvm/ioregion.h | 15 ++ virt/kvm/kvm_main.c | 20 +- 11 files changed, 507 insertions(+), 3 deletions(-) create mode 100644 virt/kvm/eventfd.h create mode 100644 virt/kvm/ioregion.c create mode 100644 virt/kvm/ioregion.hSignature · 1 line
2.25.1
Re: [RFC 1/2] KVM: add initial support for KVM_SET_IOREGION
In reply to Elena AfanasovaQuoted 1 line · Elena Afanasova
This vm ioctl adds or removes an ioregionfd MMIO/PIO region.
How about FAST_MMIO?
Quoted 107 lines
Guest
read and write accesses are dispatched through the given ioregionfd instead of returning fromioctl(KVM_RUN). Regions can be deleted by setting fds to -1.Signed-off-by Elena Afanasova <[email protected]>--- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/Makefile | 1 + arch/x86/kvm/x86.c | 1 + include/linux/kvm_host.h | 17 +++ include/uapi/linux/kvm.h | 23 ++++ virt/kvm/Kconfig | 3 + virt/kvm/eventfd.c | 25 +++++ virt/kvm/eventfd.h | 14 +++ virt/kvm/ioregion.c | 233 +++++++++++++++++++++++++++++++++++++++ virt/kvm/ioregion.h | 15 +++ virt/kvm/kvm_main.c | 20 +++- 11 files changed, 350 insertions(+), 3 deletions(-)create mode 100644
virt/kvm/eventfd.h
create mode 100644virt/kvm/ioregion.c
create mode 100644virt/kvm/ioregion.harch/x86/kvm/Kconfig +1
-0@@ -33,6 +33,7 @@ config KVM select HAVE_KVM_IRQ_BYPASS select HAVE_KVM_IRQ_ROUTING select HAVE_KVM_EVENTFD + select KVM_IOREGION select KVM_ASYNC_PF select USER_RETURN_NOTIFIER select KVM_MMIOarch/x86/kvm/Makefile +1
-0@@ -12,6 +12,7 @@ KVM := ../../../virt/kvm kvm-y += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o \ $(KVM)/eventfd.o $(KVM)/irqchip.o $(KVM)/vfio.o kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o +kvm-$(CONFIG_KVM_IOREGION) += $(KVM)/ioregion.o kvm-y += x86.o emulate.o i8259.o irq.o lapic.o \ i8254.o ioapic.o irq_comm.o cpuid.o pmu.o mtrr.o \arch/x86/kvm/x86.c +1
-0@@ -3739,6 +3739,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_X86_USER_SPACE_MSR :case KVM_CAP_X86_MSR_FILTER :case KVM_CAP_ENFORCE_PV_FEATURE_CPUID : +case KVM_CAP_IOREGIONFD : r =1 ;break ;case KVM_CAP_SYNC_REGS :include/linux/kvm_host.h +17
-0@@ -470,6 +470,10 @@ struct kvm { struct mutex resampler_lock; } irqfds;struct list_head ioeventfds; +#endif +#ifdef CONFIG_KVM_IOREGION +struct list_head ioregions_mmio; +struct list_head ioregions_pio;#endif struct kvm_vm_stat stat;struct kvm_arch arch;@@ -1262,6 +1266,19 @@ static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)#endif /* CONFIG_HAVE_KVM_EVENTFD */ +#ifdef CONFIG_KVM_IOREGION +void kvm_ioregionfd_init (struct kvm *kvm); +int kvm_ioregionfd (struct kvm *kvm,struct kvm_ioregion *args); + +#else + +static inline void kvm_ioregionfd_init (struct kvm *kvm) {} +static inline int kvm_ioregionfd (struct kvm *kvm,struct kvm_ioregion *args) +{ +return -ENOSYS ; +} +#endif +void kvm_arch_irq_routing_update (struct kvm *kvm);static inline void kvm_make_request (int req,struct kvm_vcpu *vcpu)include/uapi/linux/kvm.h +5
-0@@ -732,6 +732,27 @@ struct kvm_ioeventfd {__u8 pad[36 ]; }; +enum { + kvm_ioregion_flag_nr_pio, + kvm_ioregion_flag_nr_posted_writes, + kvm_ioregion_flag_nr_max, +};
include/uapi/linux/kvm.h struct kvm_ioeventfd { · line 749
Quoted 10 lines
include/uapi/linux/kvm.h +10
-0+ +#define KVM_IOREGION_PIO (1 << kvm_ioregion_flag_nr_pio) +#define KVM_IOREGION_POSTED_WRITES (1 << kvm_ioregion_flag_nr_posted_writes) + +#define KVM_IOREGION_VALID_FLAG_MASK ((1 << kvm_ioregion_flag_nr_max) - 1) + +struct kvm_ioregion { +__u64 guest_paddr;/* guest physical address */ +__u64 memory_size;/* bytes */ +__u64 user_data;
What will this field do? Is it a token?
include/uapi/linux/kvm.h struct kvm_ioeventfd { · line 754
Quoted 5 lines
include/uapi/linux/kvm.h +5
-0+__s32 rfd; +__s32 wfd; +__u32 flags; +__u8 pad[28 ]; +};
Is this possible to register the same fd with multiple GPA ranges? If not, do we need to check for fd collision?
Quoted 252 lines
include/uapi/linux/kvm.h +3
-0+#define KVM_X86_DISABLE_EXITS_MWAIT (1 << 0)#define KVM_X86_DISABLE_EXITS_HLT (1 << 1)#define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2)@@ -1053,6 +1074,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_X86_USER_SPACE_MSR 188#define KVM_CAP_X86_MSR_FILTER 189#define KVM_CAP_ENFORCE_PV_FEATURE_CPUID 190 +#define KVM_CAP_IOREGIONFD 191#ifdef KVM_CAP_IRQ_ROUTING @@ -1308,6 +1330,7 @@ struct kvm_vfio_spapr_tce { struct kvm_userspace_memory_region)#define KVM_SET_TSS_ADDR _IO(KVMIO, 0x47)#define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO, 0x48, __u64) +#define KVM_SET_IOREGION _IOW(KVMIO, 0x49, struct kvm_ioregion)/* enable ucontrol for s390 */ struct kvm_s390_ucas_mapping {virt/kvm/Kconfig +3
-0@@ -17,6 +17,9 @@ config HAVE_KVM_EVENTFD bool select EVENTFD +config KVM_IOREGION + bool + config KVM_MMIO boolvirt/kvm/eventfd.c +25
-0@@ -27,6 +27,7 @@#include <trace/events/kvm.h> #include <kvm/iodev.h> +#include "ioregion.h" #ifdef CONFIG_HAVE_KVM_IRQFD @@ -755,6 +756,23 @@ static const struct kvm_io_device_ops ioeventfd_ops = { .destructor = ioeventfd_destructor, }; +#ifdef CONFIG_KVM_IOREGION +/* assumes kvm->slots_lock held */ +bool kvm_eventfd_collides (struct kvm *kvm,int bus_idx, +u64 start,u64 size) +{ +struct _ioeventfd *_p; + +list_for_each_entry (_p, &kvm->ioeventfds, list) +if (_p->bus_idx == bus_idx && +overlap (start, size, _p->addr, + !_p->length ?8 : _p->length)) +return true; + +return false; +} +#endif +/* assumes kvm->slots_lock held */ static bool ioeventfd_check_collision (struct kvm *kvm,struct _ioeventfd *p)@@ -770,6 +788,13 @@ ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p) _p->datamatch == p->datamatch))))return true; +#ifdef CONFIG_KVM_IOREGION +if (p->bus_idx ==KVM_MMIO_BUS || p->bus_idx ==KVM_PIO_BUS ) +if (kvm_ioregion_collides (kvm, p->bus_idx, p->addr, + !p->length ?8 : p->length)) +return true; +#endif +return false; }virt/kvm/eventfd.h new file +14
-0@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_EVENTFD_H__ +#define __KVM_EVENTFD_H__ + +#ifdef CONFIG_KVM_IOREGION +bool kvm_eventfd_collides (struct kvm *kvm,int bus_idx,u64 start,u64 size); +#else +static inline bool +kvm_eventfd_collides (struct kvm *kvm,int bus_idx,u64 start,u64 size) +{ +return false; +} +#endif +#endif virt/kvm/ioregion.c new file +142
-0@@ -0,0 +1,233 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <linux/kvm_host.h> +#include <linux/fs.h> +#include <kvm/iodev.h> +#include "eventfd.h" + +void +kvm_ioregionfd_init (struct kvm *kvm) +{ +INIT_LIST_HEAD (&kvm->ioregions_mmio); +INIT_LIST_HEAD (&kvm->ioregions_pio); +} + +struct ioregion { +struct list_head list; +u64 paddr; +u64 size; +struct file *rf; +struct file *wf; +u64 user_data; +struct kvm_io_device dev; +bool posted_writes; +}; + +static inline struct ioregion * +to_ioregion (struct kvm_io_device *dev) +{ + return container_of(dev, struct ioregion, dev); +} + +/* assumes kvm->slots_lock held */ +static void +ioregion_release (struct ioregion *p) +{ +fput (p->rf); +fput (p->wf); +list_del (&p->list); +kfree (p); +} + +static int +ioregion_read (struct kvm_vcpu *vcpu,struct kvm_io_device *this,gpa_t addr, +int len,void *val) +{ +return 0 ; +} + +static int +ioregion_write (struct kvm_vcpu *vcpu,struct kvm_io_device *this,gpa_t addr, +int len,const void *val) +{ +return 0 ; +} + +/* +* This function is called as KVM is completely shutting down. We do not +* need to worry about locking just nuke anything we have as quickly as possible +*/ +static void +ioregion_destructor (struct kvm_io_device *this) +{ +struct ioregion *p =to_ioregion (this); + +ioregion_release (p); +} + +static const struct kvm_io_device_ops ioregion_ops = { + .read = ioregion_read, + .write = ioregion_write, + .destructor = ioregion_destructor, +}; + +static inline struct list_head * +get_ioregion_list (struct kvm *kvm,enum kvm_bus bus_idx) +{ +return (bus_idx ==KVM_MMIO_BUS ) ? + &kvm->ioregions_mmio : &kvm->ioregions_pio; +} + +/* check for not overlapping case and reverse */ +inline bool +overlap (u64 start1,u64 size1,u64 start2,u64 size2) +{ +u64 end1 = start1 + size1 -1 ; +u64 end2 = start2 + size2 -1 ; + +return !(end1 < start2 || start1 >= end2); +} + +/* assumes kvm->slots_lock held */ +bool +kvm_ioregion_collides (struct kvm *kvm,int bus_idx, +u64 start,u64 size) +{ +struct ioregion *_p; +struct list_head *ioregions; + + ioregions =get_ioregion_list (kvm, bus_idx); +list_for_each_entry (_p, ioregions, list) +if (overlap (start, size, _p->paddr, _p->size)) +return true; + +return false; +} + +/* assumes kvm->slots_lock held */ +static bool +ioregion_collision (struct kvm *kvm,struct ioregion *p,enum kvm_bus bus_idx) +{ +if (kvm_ioregion_collides (kvm, bus_idx, p->paddr, p->size) || +kvm_eventfd_collides (kvm, bus_idx, p->paddr, p->size)) +return true; + +return false; +} + +static enum kvm_bus +get_bus_from_flags (__u32 flags) +{ +if (flags &KVM_IOREGION_PIO ) +return KVM_PIO_BUS ; +return KVM_MMIO_BUS ; +} + +int +kvm_set_ioregion (struct kvm *kvm,struct kvm_ioregion *args) +{ +struct ioregion *p; +bool is_posted_writes; +struct file *rfile, *wfile; +enum kvm_bus bus_idx; +int ret =0 ; + +if (!args->memory_size) +return -EINVAL ; +if ((args->guest_paddr + args->memory_size -1 ) < args->guest_paddr) +return -EINVAL ; +if (args->flags & ~KVM_IOREGION_VALID_FLAG_MASK ) +return -EINVAL ; + + rfile =fget (args->rfd); +if (!rfile)
virt/kvm/ioregion.c line 152
Quoted 10 lines
virt/kvm/ioregion.c new file +10
-0+return -EBADF ; + wfile =fget (args->wfd); +if (!wfile) { +fput (rfile); +return -EBADF ; + } +if ((rfile->f_flags &O_NONBLOCK ) || (wfile->f_flags &O_NONBLOCK )) { + ret = -EINVAL ; + goto fail; + }
Instead of checking nonblocking, can we poll here?
Quoted 17 lines
virt/kvm/ioregion.c new file +17
-0+ p =kzalloc (sizeof (*p),GFP_KERNEL_ACCOUNT ); +if (!p) { + ret = -ENOMEM ; + goto fail; + } + +INIT_LIST_HEAD (&p->list); + p->paddr = args->guest_paddr; + p->size = args->memory_size; + p->user_data = args->user_data; + p->rf = rfile; + p->wf = wfile; + is_posted_writes = args->flags &KVM_IOREGION_POSTED_WRITES ; + p->posted_writes = is_posted_writes ? true : false; + bus_idx =get_bus_from_flags (args->flags); + +mutex_lock (&kvm->slots_lock);
virt/kvm/ioregion.c line 179
Quoted 10 lines
virt/kvm/ioregion.c new file +10
-0+ +if (ioregion_collision (kvm, p, bus_idx)) { + ret = -EEXIST ; + goto unlock_fail; + } +kvm_iodevice_init (&p->dev, &ioregion_ops); + ret =kvm_io_bus_register_dev (kvm, bus_idx, p->paddr, p->size, + &p->dev); +if (ret <0 ) + goto unlock_fail;
We probably need to register to FAST_MMIO when bus_idx is MMIO.
Quoted 16 lines
virt/kvm/ioregion.c new file +16
-0+list_add_tail (&p->list,get_ioregion_list (kvm, bus_idx)); + +mutex_unlock (&kvm->slots_lock); + +return 0 ; + +unlock_fail: +mutex_unlock (&kvm->slots_lock); +kfree (p); +fail: +fput (rfile); +fput (wfile); + +return ret; +} +
virt/kvm/ioregion.c line 205
Quoted 10 lines
virt/kvm/ioregion.c new file +10
-0+static int +kvm_rm_ioregion (struct kvm *kvm,struct kvm_ioregion *args) +{ +struct ioregion *p, *tmp; +enum kvm_bus bus_idx; +int ret = -ENOENT ; +struct list_head *ioregions; + +if (args->rfd !=-1 || args->wfd !=-1 ) +return -EINVAL ;
If we want to use ioregion fd for doorbell, rfd is probably not necessary here.
Thanks
Quoted 94 lines
virt/kvm/ioregion.c new file +28
-0+ + bus_idx =get_bus_from_flags (args->flags); + ioregions =get_ioregion_list (kvm, bus_idx); + +mutex_lock (&kvm->slots_lock); + +list_for_each_entry_safe (p, tmp, ioregions, list) { +if (p->paddr == args->guest_paddr && + p->size == args->memory_size) { +kvm_io_bus_unregister_dev (kvm, bus_idx, &p->dev); +ioregion_release (p); + ret =0 ; +break ; + } + } + +mutex_unlock (&kvm->slots_lock); + +return ret; +} + +int +kvm_ioregionfd (struct kvm *kvm,struct kvm_ioregion *args) +{ +if (args->rfd ==-1 || args->wfd ==-1 ) +return kvm_rm_ioregion (kvm, args); +return kvm_set_ioregion (kvm, args); +}virt/kvm/ioregion.h new file +15
-0@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_IOREGION_H__ +#define __KVM_IOREGION_H__ + +#ifdef CONFIG_KVM_IOREGION +inline bool overlap (u64 start1,u64 size1,u64 start2,u64 size2); +bool kvm_ioregion_collides (struct kvm *kvm,int bus_idx,u64 start,u64 size); +#else +static inline bool +kvm_ioregion_collides (struct kvm *kvm,int bus_idx,u64 start,u64 size) +{ +return false; +} +#endif +#endif virt/kvm/kvm_main.c +17
-3@@ -747,6 +747,7 @@ static struct kvm *kvm_create_vm(unsigned long type)mmgrab (current->mm); kvm->mm = current->mm;kvm_eventfd_init (kvm); +kvm_ioregionfd_init (kvm);mutex_init (&kvm->lock);mutex_init (&kvm->irq_lock);mutex_init (&kvm->slots_lock);@@ -3708,6 +3709,16 @@ static long kvm_vm_ioctl(struct file *filp, r =kvm_vm_ioctl_set_memory_region (kvm, &kvm_userspace_mem);break ; } +case KVM_SET_IOREGION : { +struct kvm_ioregion data; + + r = -EFAULT ; +if (copy_from_user (&data, argp,sizeof (data))) + goto out; + + r =kvm_ioregionfd (kvm, &data); +break ; + }case KVM_GET_DIRTY_LOG : {struct kvm_dirty_log log;@@ -4301,9 +4312,12 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,if (!bus)return -ENOMEM ; -/* exclude ioeventfd which is limited by maximum fd */ -if (bus->dev_count - bus->ioeventfd_count >NR_IOBUS_DEVS -1 ) -return -ENOSPC ; +/* enforce hard limit if kmemcg is disabled and +* exclude ioeventfd which is limited by maximum fd +*/ +if (!memcg_kmem_enabled ()) +if (bus->dev_count - bus->ioeventfd_count >NR_IOBUS_DEVS -1 ) +return -ENOSPC ; new_bus =kmalloc (struct_size (bus, range, bus->dev_count +1 ),GFP_KERNEL_ACCOUNT );
Re: [RFC 2/2] KVM: add initial support for ioregionfd blocking read/write operations
In reply to Elena AfanasovaQuoted 12 lines · Elena Afanasova
Signed-off-by Elena Afanasova<[email protected]>--- virt/kvm/ioregion.c | 157 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+)virt/kvm/ioregion.c +0
-0@@ -4,6 +4,33 @@#include <kvm/iodev.h> #include "eventfd.h"
virt/kvm/ioregion.c line 15
Quoted 10 lines
virt/kvm/ioregion.c +9
-0+/* Wire protocol */ +struct ioregionfd_cmd { +__u32 info; +__u32 padding; +__u64 user_data; +__u64 offset; +__u64 data; +}; +
I wonder do we need a seq in the protocol. It might be useful if we allow a pair of file descriptors to be used for multiple different ranges.
Thanks
Quoted 4 lines
virt/kvm/ioregion.c +4
-0+struct ioregionfd_resp { +__u64 data; +__u8 pad[24 ]; +};
Re: [RFC 1/2] KVM: add initial support for KVM_SET_IOREGION
In reply to Jason WangQuoted 5 lines · Jason Wang
Quoted 1 line · Elena Afanasova
This vm ioctl adds or removes an ioregionfd MMIO/PIO region.
How about
FAST_MMIO?
I’ll add KVM_IOREGION_FAST_MMIO flag support. So this may be suitable for triggers which could use posted writes. The struct ioregionfd_cmd size bits and the data field will be unused in this case.
Quoted 123 lines
Quoted 123 lines
Guest
read and write accesses are dispatched through the given ioregionfd instead of returning fromioctl(KVM_RUN). Regions can be deleted by setting fds to -1.Signed-off-by Elena Afanasova <[email protected]>--- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/Makefile | 1 + arch/x86/kvm/x86.c | 1 + include/linux/kvm_host.h | 17 +++ include/uapi/linux/kvm.h | 23 ++++ virt/kvm/Kconfig | 3 + virt/kvm/eventfd.c | 25 +++++ virt/kvm/eventfd.h | 14 +++ virt/kvm/ioregion.c | 233 +++++++++++++++++++++++++++++++++++++++ virt/kvm/ioregion.h | 15 +++ virt/kvm/kvm_main.c | 20 +++- 11 files changed, 350 insertions(+), 3 deletions(-)create mode 100644
virt/kvm/eventfd.h
create mode 100644virt/kvm/ioregion.c
create mode 100644virt/kvm/ioregion.harch/x86/kvm/Kconfig +1
-0@@ -33,6 +33,7 @@ config KVM select HAVE_KVM_IRQ_BYPASS select HAVE_KVM_IRQ_ROUTING select HAVE_KVM_EVENTFD + select KVM_IOREGION select KVM_ASYNC_PF select USER_RETURN_NOTIFIER select KVM_MMIOarch/x86/kvm/Makefile +0
-0@@ -12,6 +12,7 @@ KVM := ../../../virt/kvm kvm-y += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o \ $(KVM)/eventfd.o $(KVM)/irqchip.o$(KVM)/vfio.o
kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o
+kvm-$(CONFIG_KVM_IOREGION) += $(KVM)/ioregion.okvm-y += x86.o emulate.o i8259.o irq.o
lapic.o \
i8254.o ioapic.o irq_comm.o cpuid.o pmu.o
mtrr.o \arch/x86/kvm/x86.c +1
-0@@ -3739,6 +3739,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_X86_USER_SPACE_MSR :case KVM_CAP_X86_MSR_FILTER :case KVM_CAP_ENFORCE_PV_FEATURE_CPUID : +case KVM_CAP_IOREGIONFD : r =1 ;break ;case
KVM_CAP_SYNC_REGS:include/linux/kvm_host.h +17
-0@@ -470,6 +470,10 @@ struct kvm { struct mutex resampler_lock; } irqfds;struct list_head ioeventfds; +#endif +#ifdef CONFIG_KVM_IOREGION +struct list_head ioregions_mmio; +struct list_head ioregions_pio;#endif struct kvm_vm_stat stat;struct kvm_arch arch;@@ -1262,6 +1266,19 @@ static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)#endif /* CONFIG_HAVE_KVM_EVENTFD */ +#ifdef CONFIG_KVM_IOREGION +void kvm_ioregionfd_init (struct kvm *kvm); +int kvm_ioregionfd (struct kvm *kvm,struct kvm_ioregion *args); + +#else + +static inline void kvm_ioregionfd_init (struct kvm *kvm) {} +static inline int kvm_ioregionfd (struct kvm *kvm,struct kvm_ioregion *args) +{ +return -ENOSYS ; +} +#endif +void kvm_arch_irq_routing_update (struct kvm *kvm);static inline void kvm_make_request(int req, struct
kvm_vcpu
*vcpu)include/uapi/linux/kvm.h +10
-0@@ -732,6 +732,27 @@ struct kvm_ioeventfd {__u8 pad[36 ]; }; +enum { + kvm_ioregion_flag_nr_pio, + kvm_ioregion_flag_nr_posted_writes, + kvm_ioregion_flag_nr_max, +}; + +#define KVM_IOREGION_PIO (1 << kvm_ioregion_flag_nr_pio) +#define KVM_IOREGION_POSTED_WRITES (1 << kvm_ioregion_flag_nr_posted_writes) + +#define KVM_IOREGION_VALID_FLAG_MASK ((1 <<
Quoted 9 lines
include/uapi/linux/kvm.h struct kvm_ioeventfd { · line 749
Quoted 6 lines
include/uapi/linux/kvm.h +5
-0kvm_ioregion_flag_nr_max) - 1) + +struct kvm_ioregion { +__u64 guest_paddr;/* guest physical address */ +__u64 memory_size;/* bytes */ +__u64 user_data;What will this field do? Is it a token?
Yes, it’s an opaque token that can be used by userspace in order to determine which MemoryRegion to dispatch.
Quoted 11 lines
include/uapi/linux/kvm.h struct kvm_ioeventfd { · line 754
Quoted 6 lines
include/uapi/linux/kvm.h +5
-0+__s32 rfd; +__s32 wfd; +__u32 flags; +__u8 pad[28 ]; +};Is this possible to register the same fd with multiple GPA ranges? If
not, do we need to check for fd collision?
Yes, it’s possible to register the same fd with multiple GPA ranges.
Quoted 270 lines
Quoted 270 lines
include/uapi/linux/kvm.h +1
-0+#define KVM_X86_DISABLE_EXITS_MWAIT (1 << 0)#define KVM_X86_DISABLE_EXITS_HLT (1 << 1) #define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2)include/uapi/linux/kvm.h +2
-0@@ -1053,6 +1074,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_X86_USER_SPACE_MSR 188#define KVM_CAP_X86_MSR_FILTER 189#define KVM_CAP_ENFORCE_PV_FEATURE_CPUID 190 +#define KVM_CAP_IOREGIONFD 191#ifdef KVM_CAP_IRQ_ROUTING @@ -1308,6 +1330,7 @@ struct kvm_vfio_spapr_tce { struct kvm_userspace_memory_region)#define KVM_SET_TSS_ADDR _IO(KVMIO, 0x47)#define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO, 0x48, __u64) +#define KVM_SET_IOREGION _IOW(KVMIO, 0x49, struct kvm_ioregion)/* enable ucontrol for s390 */ struct kvm_s390_ucas_mapping {virt/kvm/Kconfig +3
-0@@ -17,6 +17,9 @@ config HAVE_KVM_EVENTFD bool select EVENTFD +config KVM_IOREGION + bool + config KVM_MMIO boolvirt/kvm/eventfd.c +18
-0@@ -27,6 +27,7 @@#include <trace/events/kvm.h> #include <kvm/iodev.h> +#include "ioregion.h" #ifdef CONFIG_HAVE_KVM_IRQFD @@ -755,6 +756,23 @@ static const struct kvm_io_device_ops ioeventfd_ops = { .destructor = ioeventfd_destructor, }; +#ifdef CONFIG_KVM_IOREGION +/* assumes kvm->slots_lock held */ +bool kvm_eventfd_collides (struct kvm *kvm,int bus_idx, +u64 start,u64 size) +{ +struct _ioeventfd *_p; + +list_for_each_entry (_p, &kvm->ioeventfds, list) +if (_p->bus_idx == bus_idx && +overlap (start, size, _p->addr, + !_p->length ?8 : _p->length)) +return true; + +return false; +} +#endif +/* assumes kvm->slots_lock held */ static bool
ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p)virt/kvm/eventfd.c +7
-0@@ -770,6 +788,13 @@ ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p) _p->datamatch == p->datamatch)))) return true; +#ifdef CONFIG_KVM_IOREGION +if (p->bus_idx ==KVM_MMIO_BUS || p->bus_idx ==KVM_PIO_BUS ) +if (kvm_ioregion_collides (kvm, p->bus_idx, p->addr, + !p->length ?8 : p->length)) +return true; +#endif +return false; }virt/kvm/eventfd.h new file +6
-0@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_EVENTFD_H__ +#define __KVM_EVENTFD_H__ + +#ifdef CONFIG_KVM_IOREGION +bool kvm_eventfd_collides(struct kvm *kvm, int bus_idx, u64 start,u64 size);
virt/kvm/eventfd.h new file +3
-0+#else +static inline bool +kvm_eventfd_collides(struct kvm *kvm, int bus_idx, u64 start, u64size)
virt/kvm/eventfd.h new file +5
-0+{ +return false; +} +#endif +#endif virt/kvm/ioregion.c new file +42
-0@@ -0,0 +1,233 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <linux/kvm_host.h> +#include <linux/fs.h> +#include <kvm/iodev.h> +#include "eventfd.h" + +void +kvm_ioregionfd_init (struct kvm *kvm) +{ +INIT_LIST_HEAD (&kvm->ioregions_mmio); +INIT_LIST_HEAD (&kvm->ioregions_pio); +} + +struct ioregion { +struct list_head list; +u64 paddr; +u64 size; +struct file *rf; +struct file *wf; +u64 user_data; +struct kvm_io_device dev; +bool posted_writes; +}; + +static inline struct ioregion * +to_ioregion (struct kvm_io_device *dev) +{ + return container_of(dev, struct ioregion, dev); +} + +/* assumes kvm->slots_lock held */ +static void +ioregion_release (struct ioregion *p) +{ +fput (p->rf); +fput (p->wf); +list_del (&p->list); +kfree (p); +} + +static int +ioregion_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
gpa_taddr,virt/kvm/ioregion.c new file +7
-0+ int len, void *val) +{ +return 0 ; +} + +static int +ioregion_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
gpa_taddr,virt/kvm/ioregion.c new file +7
-0+ int len, const void *val) +{ +return 0 ; +} + +/* +* This function is called as KVM is completely shutting down. We do not
+ * need to worry about locking just nuke anything we have as quickly as possible
+ */virt/kvm/ioregion.c new file +50
-0+static void +ioregion_destructor (struct kvm_io_device *this) +{ +struct ioregion *p =to_ioregion (this); + +ioregion_release (p); +} + +static const struct kvm_io_device_ops ioregion_ops = { + .read = ioregion_read, + .write = ioregion_write, + .destructor = ioregion_destructor, +}; + +static inline struct list_head * +get_ioregion_list (struct kvm *kvm,enum kvm_bus bus_idx) +{ +return (bus_idx ==KVM_MMIO_BUS ) ? + &kvm->ioregions_mmio : &kvm->ioregions_pio; +} + +/* check for not overlapping case and reverse */ +inline bool +overlap (u64 start1,u64 size1,u64 start2,u64 size2) +{ +u64 end1 = start1 + size1 -1 ; +u64 end2 = start2 + size2 -1 ; + +return !(end1 < start2 || start1 >= end2); +} + +/* assumes kvm->slots_lock held */ +bool +kvm_ioregion_collides (struct kvm *kvm,int bus_idx, +u64 start,u64 size) +{ +struct ioregion *_p; +struct list_head *ioregions; + + ioregions =get_ioregion_list (kvm, bus_idx); +list_for_each_entry (_p, ioregions, list) +if (overlap (start, size, _p->paddr, _p->size)) +return true; + +return false; +} + +/* assumes kvm->slots_lock held */ +static bool +ioregion_collision(struct kvm *kvm, struct ioregion *p, enum
kvm_busbus_idx)virt/kvm/ioregion.c new file +28
-0+{ +if (kvm_ioregion_collides (kvm, bus_idx, p->paddr, p->size) || +kvm_eventfd_collides (kvm, bus_idx, p->paddr, p->size)) +return true; + +return false; +} + +static enum kvm_bus +get_bus_from_flags (__u32 flags) +{ +if (flags &KVM_IOREGION_PIO ) +return KVM_PIO_BUS ; +return KVM_MMIO_BUS ; +} + +int +kvm_set_ioregion(struct kvm *kvm, struct kvm_ioregion *args) +{ + struct ioregion *p; + bool is_posted_writes; + struct file *rfile, *wfile; + enum kvm_bus bus_idx; + int ret = 0; + + if (!args->memory_size) + return -EINVAL; + if ((args->guest_paddr + args->memory_size - 1) < args-Quoted 1 line
guest_paddr)virt/kvm/ioregion.c new file +11
-0+return -EINVAL ; +if (args->flags & ~KVM_IOREGION_VALID_FLAG_MASK ) +return -EINVAL ; + + rfile =fget (args->rfd); +if (!rfile) +return -EBADF ; + wfile =fget (args->wfd); +if (!wfile) { +fput (rfile); +return -EBADF ;
Quoted 9 lines
virt/kvm/ioregion.c line 152
Quoted 6 lines
virt/kvm/ioregion.c new file +2
-0+ } + if ((rfile->f_flags & O_NONBLOCK) || (wfile->f_flags &
O_NONBLOCK)) {virt/kvm/ioregion.c new file +3
-0+ ret = -EINVAL ; + goto fail; + }Instead of checking nonblocking, can we poll here?
Yes, it’s possible. It would be necessary in the case of out-of-order requests. But since multiple in-flight messages don’t seem to be a use case I’m not sure if it’s necessary. Typically device register accesses should not take a long time, so making them asynchronous doesn't seem like a practical advantage. Also this might complicate the code and make it slower. What do you think?
Quoted 53 lines
Quoted 18 lines
virt/kvm/ioregion.c new file +17
-0+ p =kzalloc (sizeof (*p),GFP_KERNEL_ACCOUNT ); +if (!p) { + ret = -ENOMEM ; + goto fail; + } + +INIT_LIST_HEAD (&p->list); + p->paddr = args->guest_paddr; + p->size = args->memory_size; + p->user_data = args->user_data; + p->rf = rfile; + p->wf = wfile; + is_posted_writes = args->flags &KVM_IOREGION_POSTED_WRITES ; + p->posted_writes = is_posted_writes ? true : false; + bus_idx =get_bus_from_flags (args->flags); + +mutex_lock (&kvm->slots_lock);virt/kvm/ioregion.c line 179
Quoted 10 lines
virt/kvm/ioregion.c new file +10
-0+ +if (ioregion_collision (kvm, p, bus_idx)) { + ret = -EEXIST ; + goto unlock_fail; + } +kvm_iodevice_init (&p->dev, &ioregion_ops); + ret =kvm_io_bus_register_dev (kvm, bus_idx, p->paddr, p->size, + &p->dev); +if (ret <0 ) + goto unlock_fail;We probably need to register to
FAST_MMIOwhenbus_idxis MMIO.Quoted 21 lines
virt/kvm/ioregion.c new file +21
-0+list_add_tail (&p->list,get_ioregion_list (kvm, bus_idx)); + +mutex_unlock (&kvm->slots_lock); + +return 0 ; + +unlock_fail: +mutex_unlock (&kvm->slots_lock); +kfree (p); +fail: +fput (rfile); +fput (wfile); + +return ret; +} + +static int +kvm_rm_ioregion (struct kvm *kvm,struct kvm_ioregion *args) +{ +struct ioregion *p, *tmp; +enum kvm_bus bus_idx;
Quoted 9 lines
virt/kvm/ioregion.c line 205
Quoted 5 lines
virt/kvm/ioregion.c new file +5
-0+int ret = -ENOENT ; +struct list_head *ioregions; + +if (args->rfd !=-1 || args->wfd !=-1 ) +return -EINVAL ;If we want to use ioregion fd for doorbell, rfd is probably not necessary here.
This condition is simply a requirement that region can be deleted in the case of both fds are set to -1.
Quoted 104 lines
Thanks
Quoted 101 lines
virt/kvm/ioregion.c new file +10
-0+ + bus_idx =get_bus_from_flags (args->flags); + ioregions =get_ioregion_list (kvm, bus_idx); + +mutex_lock (&kvm->slots_lock); + +list_for_each_entry_safe (p, tmp, ioregions, list) { +if (p->paddr == args->guest_paddr && + p->size == args->memory_size) { + kvm_io_bus_unregister_dev(kvm, bus_idx, &p-Quoted 1 line
dev);
virt/kvm/ioregion.c new file +18
-0+ioregion_release (p); + ret =0 ; +break ; + } + } + +mutex_unlock (&kvm->slots_lock); + +return ret; +} + +int +kvm_ioregionfd (struct kvm *kvm,struct kvm_ioregion *args) +{ +if (args->rfd ==-1 || args->wfd ==-1 ) +return kvm_rm_ioregion (kvm, args); +return kvm_set_ioregion (kvm, args); +}virt/kvm/ioregion.h new file +7
-0@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_IOREGION_H__ +#define __KVM_IOREGION_H__ + +#ifdef CONFIG_KVM_IOREGION +inline bool overlap (u64 start1,u64 size1,u64 start2,u64 size2); +bool kvm_ioregion_collides(struct kvm *kvm, int bus_idx, u64start, u64 size);
virt/kvm/ioregion.h new file +3
-0+#else +static inline bool +kvm_ioregion_collides(struct kvm *kvm, int bus_idx, u64 start, u64size)
virt/kvm/ioregion.h new file +5
-0+{ +return false; +} +#endif +#endif virt/kvm/kvm_main.c +1
-0@@ -747,6 +747,7 @@ static struct kvm *kvm_create_vm(unsigned long type) mmgrab(current->mm); kvm->mm = current->mm;kvm_eventfd_init (kvm); +kvm_ioregionfd_init (kvm);mutex_init (&kvm->lock);mutex_init (&kvm->irq_lock);mutex_init(&kvm->slots_lock);virt/kvm/kvm_main.c +16
-3@@ -3708,6 +3709,16 @@ static long kvm_vm_ioctl(struct file *filp, r =kvm_vm_ioctl_set_memory_region (kvm, &kvm_userspace_mem);break ; } +case KVM_SET_IOREGION : { +struct kvm_ioregion data; + + r = -EFAULT ; +if (copy_from_user (&data, argp,sizeof (data))) + goto out; + + r =kvm_ioregionfd (kvm, &data); +break ; + }case KVM_GET_DIRTY_LOG : {struct kvm_dirty_log log;@@ -4301,9 +4312,12 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, if (!bus) return -ENOMEM; -/* exclude ioeventfd which is limited by maximum fd */ -if (bus->dev_count - bus->ioeventfd_count >NR_IOBUS_DEVS -1 ) -return -ENOSPC ; +/* enforce hard limit if kmemcg is disabled and +* exclude ioeventfd which is limited by maximum fd +*/ +if (!memcg_kmem_enabled ()) +if (bus->dev_count - bus->ioeventfd_count >NR_IOBUS_DEVS -1 ) +return -ENOSPC ;
new_bus= kmalloc(struct_size(bus, range,bus->dev_count+ 1),
GFP_KERNEL_ACCOUNT);
Re: [RFC 2/2] KVM: add initial support for ioregionfd blocking read/write operations
In reply to Jason WangQuoted 15 lines · Jason Wang
Quoted 14 lines · Elena Afanasova
Signed-off-by Elena Afanasova<[email protected]>--- virt/kvm/ioregion.c | 157 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+)virt/kvm/ioregion.c +0
-0@@ -4,6 +4,33 @@#include <kvm/iodev.h> #include "eventfd.h"
Quoted 14 lines
virt/kvm/ioregion.c line 15
Quoted 9 lines
virt/kvm/ioregion.c +9
-0+/* Wire protocol */ +struct ioregionfd_cmd { +__u32 info; +__u32 padding; +__u64 user_data; +__u64 offset; +__u64 data; +}; +I wonder do we need a seq in the protocol. It might be useful if we allow a pair of file descriptors to be used for multiple different ranges.
I think it might be helpful in the case of out-of-order requests. In the case of in order requests seq field seems not to be necessary since there will be cmds/replies serialization. I’ll include the synchronization code in a RFC v2 series.
Quoted 7 lines
Thanks
Quoted 4 lines
virt/kvm/ioregion.c +4
-0+struct ioregionfd_resp { +__u64 data; +__u8 pad[24 ]; +};
Re: [RFC 1/2] KVM: add initial support for KVM_SET_IOREGION
In reply to Elena AfanasovaQuoted 8 lines · Elena Afanasova
Quoted 4 lines · Jason Wang
Quoted 1 line · Elena Afanasova
This vm ioctl adds or removes an ioregionfd MMIO/PIO region.
How about
FAST_MMIO?I’ll add
KVM_IOREGION_FAST_MMIOflag support. So this may be suitable for triggers which could use posted writes. The structioregionfd_cmdsize bits and the data field will be unused in this case.
Note that eventfd checks for length and have datamatch support. Do we need to do something similar.
I guess the idea is to have a generic interface to let eventfd work for ioregion as well.
Quoted 131 lines
Quoted 122 lines
Quoted 122 lines
Guest
read and write accesses are dispatched through the given ioregionfd instead of returning fromioctl(KVM_RUN). Regions can be deleted by setting fds to -1.Signed-off-by Elena Afanasova <[email protected]>--- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/Makefile | 1 + arch/x86/kvm/x86.c | 1 + include/linux/kvm_host.h | 17 +++ include/uapi/linux/kvm.h | 23 ++++ virt/kvm/Kconfig | 3 + virt/kvm/eventfd.c | 25 +++++ virt/kvm/eventfd.h | 14 +++ virt/kvm/ioregion.c | 233 +++++++++++++++++++++++++++++++++++++++ virt/kvm/ioregion.h | 15 +++ virt/kvm/kvm_main.c | 20 +++- 11 files changed, 350 insertions(+), 3 deletions(-)create mode 100644
virt/kvm/eventfd.h
create mode 100644virt/kvm/ioregion.c
create mode 100644virt/kvm/ioregion.harch/x86/kvm/Kconfig +1
-0@@ -33,6 +33,7 @@ config KVM select HAVE_KVM_IRQ_BYPASS select HAVE_KVM_IRQ_ROUTING select HAVE_KVM_EVENTFD + select KVM_IOREGION select KVM_ASYNC_PF select USER_RETURN_NOTIFIER select KVM_MMIOarch/x86/kvm/Makefile +0
-0@@ -12,6 +12,7 @@ KVM := ../../../virt/kvm kvm-y += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o \ $(KVM)/eventfd.o $(KVM)/irqchip.o$(KVM)/vfio.o
kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o
+kvm-$(CONFIG_KVM_IOREGION) += $(KVM)/ioregion.okvm-y += x86.o emulate.o i8259.o irq.olapic.o \
i8254.o ioapic.o irq_comm.o cpuid.o pmu.o
mtrr.o \arch/x86/kvm/x86.c +1
-0@@ -3739,6 +3739,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_X86_USER_SPACE_MSR :case KVM_CAP_X86_MSR_FILTER :case KVM_CAP_ENFORCE_PV_FEATURE_CPUID : +case KVM_CAP_IOREGIONFD : r =1 ;break ;case
KVM_CAP_SYNC_REGS:include/linux/kvm_host.h +17
-0@@ -470,6 +470,10 @@ struct kvm { struct mutex resampler_lock; } irqfds;struct list_head ioeventfds; +#endif +#ifdef CONFIG_KVM_IOREGION +struct list_head ioregions_mmio; +struct list_head ioregions_pio;#endif struct kvm_vm_stat stat;struct kvm_arch arch;@@ -1262,6 +1266,19 @@ static inline int kvm_ioeventfd(struct kvm *kvm, struct kvm_ioeventfd *args)#endif /* CONFIG_HAVE_KVM_EVENTFD */ +#ifdef CONFIG_KVM_IOREGION +void kvm_ioregionfd_init (struct kvm *kvm); +int kvm_ioregionfd (struct kvm *kvm,struct kvm_ioregion *args); + +#else + +static inline void kvm_ioregionfd_init (struct kvm *kvm) {} +static inline int kvm_ioregionfd (struct kvm *kvm,struct kvm_ioregion *args) +{ +return -ENOSYS ; +} +#endif +void kvm_arch_irq_routing_update (struct kvm *kvm);static inline void kvm_make_request(int req, struct
kvm_vcpu
*vcpu)include/uapi/linux/kvm.h +9
-0@@ -732,6 +732,27 @@ struct kvm_ioeventfd {__u8 pad[36 ]; }; +enum { + kvm_ioregion_flag_nr_pio, + kvm_ioregion_flag_nr_posted_writes, + kvm_ioregion_flag_nr_max, +}; + +#define KVM_IOREGION_PIO (1 << kvm_ioregion_flag_nr_pio) +#define KVM_IOREGION_POSTED_WRITES (1 << kvm_ioregion_flag_nr_posted_writes) +Quoted 9 lines
include/uapi/linux/kvm.h struct kvm_ioeventfd { · line 749
Quoted 7 lines
include/uapi/linux/kvm.h +6
-0+#define KVM_IOREGION_VALID_FLAG_MASK ((1 << kvm_ioregion_flag_nr_max) - 1) + +struct kvm_ioregion { +__u64 guest_paddr;/* guest physical address */ +__u64 memory_size;/* bytes */ +__u64 user_data;What will this field do? Is it a token?
Quoted 2 lines
Yes, it’s an opaque token that can be used by userspace in order to determine which MemoryRegion to dispatch.
This part I don't understand. Userspace should know the fd number (which I guess should be sufficient?).
Quoted 289 lines
Quoted 10 lines
include/uapi/linux/kvm.h struct kvm_ioeventfd { · line 754
Quoted 6 lines
include/uapi/linux/kvm.h +5
-0+__s32 rfd; +__s32 wfd; +__u32 flags; +__u8 pad[28 ]; +};Is this possible to register the same fd with multiple GPA ranges? If
not, do we need to check for fd collision?Yes, it’s possible to register the same fd with multiple GPA ranges.
Quoted 268 lines
Quoted 268 lines
include/uapi/linux/kvm.h +1
-0+#define KVM_X86_DISABLE_EXITS_MWAIT (1 << 0)#define KVM_X86_DISABLE_EXITS_HLT (1 << 1) #define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2)include/uapi/linux/kvm.h +2
-0@@ -1053,6 +1074,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_X86_USER_SPACE_MSR 188#define KVM_CAP_X86_MSR_FILTER 189#define KVM_CAP_ENFORCE_PV_FEATURE_CPUID 190 +#define KVM_CAP_IOREGIONFD 191#ifdef KVM_CAP_IRQ_ROUTING @@ -1308,6 +1330,7 @@ struct kvm_vfio_spapr_tce { struct kvm_userspace_memory_region)#define KVM_SET_TSS_ADDR _IO(KVMIO, 0x47)#define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO, 0x48, __u64) +#define KVM_SET_IOREGION _IOW(KVMIO, 0x49, struct kvm_ioregion)/* enable ucontrol for s390 */ struct kvm_s390_ucas_mapping {virt/kvm/Kconfig +3
-0@@ -17,6 +17,9 @@ config HAVE_KVM_EVENTFD bool select EVENTFD +config KVM_IOREGION + bool + config KVM_MMIO boolvirt/kvm/eventfd.c +18
-0@@ -27,6 +27,7 @@#include <trace/events/kvm.h> #include <kvm/iodev.h> +#include "ioregion.h" #ifdef CONFIG_HAVE_KVM_IRQFD @@ -755,6 +756,23 @@ static const struct kvm_io_device_ops ioeventfd_ops = { .destructor = ioeventfd_destructor, }; +#ifdef CONFIG_KVM_IOREGION +/* assumes kvm->slots_lock held */ +bool kvm_eventfd_collides (struct kvm *kvm,int bus_idx, +u64 start,u64 size) +{ +struct _ioeventfd *_p; + +list_for_each_entry (_p, &kvm->ioeventfds, list) +if (_p->bus_idx == bus_idx && +overlap (start, size, _p->addr, + !_p->length ?8 : _p->length)) +return true; + +return false; +} +#endif +/* assumes kvm->slots_lock held */ static bool
ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p)virt/kvm/eventfd.c +7
-0@@ -770,6 +788,13 @@ ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p) _p->datamatch == p->datamatch)))) return true; +#ifdef CONFIG_KVM_IOREGION +if (p->bus_idx ==KVM_MMIO_BUS || p->bus_idx ==KVM_PIO_BUS ) +if (kvm_ioregion_collides (kvm, p->bus_idx, p->addr, + !p->length ?8 : p->length)) +return true; +#endif +return false; }virt/kvm/eventfd.h new file +6
-0@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_EVENTFD_H__ +#define __KVM_EVENTFD_H__ + +#ifdef CONFIG_KVM_IOREGION +bool kvm_eventfd_collides(struct kvm *kvm, int bus_idx, u64 start,u64 size);
virt/kvm/eventfd.h new file +3
-0+#else +static inline bool +kvm_eventfd_collides(struct kvm *kvm, int bus_idx, u64 start, u64size)
virt/kvm/eventfd.h new file +5
-0+{ +return false; +} +#endif +#endif virt/kvm/ioregion.c new file +42
-0@@ -0,0 +1,233 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <linux/kvm_host.h> +#include <linux/fs.h> +#include <kvm/iodev.h> +#include "eventfd.h" + +void +kvm_ioregionfd_init (struct kvm *kvm) +{ +INIT_LIST_HEAD (&kvm->ioregions_mmio); +INIT_LIST_HEAD (&kvm->ioregions_pio); +} + +struct ioregion { +struct list_head list; +u64 paddr; +u64 size; +struct file *rf; +struct file *wf; +u64 user_data; +struct kvm_io_device dev; +bool posted_writes; +}; + +static inline struct ioregion * +to_ioregion (struct kvm_io_device *dev) +{ + return container_of(dev, struct ioregion, dev); +} + +/* assumes kvm->slots_lock held */ +static void +ioregion_release (struct ioregion *p) +{ +fput (p->rf); +fput (p->wf); +list_del (&p->list); +kfree (p); +} + +static int +ioregion_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
gpa_taddr,virt/kvm/ioregion.c new file +7
-0+ int len, void *val) +{ +return 0 ; +} + +static int +ioregion_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this,
gpa_taddr,virt/kvm/ioregion.c new file +7
-0+ int len, const void *val) +{ +return 0 ; +} + +/* +* This function is called as KVM is completely shutting down. We do not
+ * need to worry about locking just nuke anything we have as quickly as possible
+ */virt/kvm/ioregion.c new file +50
-0+static void +ioregion_destructor (struct kvm_io_device *this) +{ +struct ioregion *p =to_ioregion (this); + +ioregion_release (p); +} + +static const struct kvm_io_device_ops ioregion_ops = { + .read = ioregion_read, + .write = ioregion_write, + .destructor = ioregion_destructor, +}; + +static inline struct list_head * +get_ioregion_list (struct kvm *kvm,enum kvm_bus bus_idx) +{ +return (bus_idx ==KVM_MMIO_BUS ) ? + &kvm->ioregions_mmio : &kvm->ioregions_pio; +} + +/* check for not overlapping case and reverse */ +inline bool +overlap (u64 start1,u64 size1,u64 start2,u64 size2) +{ +u64 end1 = start1 + size1 -1 ; +u64 end2 = start2 + size2 -1 ; + +return !(end1 < start2 || start1 >= end2); +} + +/* assumes kvm->slots_lock held */ +bool +kvm_ioregion_collides (struct kvm *kvm,int bus_idx, +u64 start,u64 size) +{ +struct ioregion *_p; +struct list_head *ioregions; + + ioregions =get_ioregion_list (kvm, bus_idx); +list_for_each_entry (_p, ioregions, list) +if (overlap (start, size, _p->paddr, _p->size)) +return true; + +return false; +} + +/* assumes kvm->slots_lock held */ +static bool +ioregion_collision(struct kvm *kvm, struct ioregion *p, enum
kvm_busbus_idx)virt/kvm/ioregion.c new file +28
-0+{ +if (kvm_ioregion_collides (kvm, bus_idx, p->paddr, p->size) || +kvm_eventfd_collides (kvm, bus_idx, p->paddr, p->size)) +return true; + +return false; +} + +static enum kvm_bus +get_bus_from_flags (__u32 flags) +{ +if (flags &KVM_IOREGION_PIO ) +return KVM_PIO_BUS ; +return KVM_MMIO_BUS ; +} + +int +kvm_set_ioregion(struct kvm *kvm, struct kvm_ioregion *args) +{ + struct ioregion *p; + bool is_posted_writes; + struct file *rfile, *wfile; + enum kvm_bus bus_idx; + int ret = 0; + + if (!args->memory_size) + return -EINVAL; + if ((args->guest_paddr + args->memory_size - 1) < args-Quoted 1 line
guest_paddr)virt/kvm/ioregion.c new file +10
-0+return -EINVAL ; +if (args->flags & ~KVM_IOREGION_VALID_FLAG_MASK ) +return -EINVAL ; + + rfile =fget (args->rfd); +if (!rfile) +return -EBADF ; + wfile =fget (args->wfd); +if (!wfile) { +fput (rfile);Quoted 9 lines
virt/kvm/ioregion.c line 152
Quoted 7 lines
virt/kvm/ioregion.c new file +3
-0+return -EBADF ; + } + if ((rfile->f_flags & O_NONBLOCK) || (wfile->f_flags &
O_NONBLOCK)) {virt/kvm/ioregion.c new file +3
-0+ ret = -EINVAL ; + goto fail; + }Instead of checking nonblocking, can we poll here?
Quoted 6 lines
Yes, it’s possible. It would be necessary in the case of out-of-order requests. But since multiple in-flight messages don’t seem to be a use case I’m not sure if it’s necessary. Typically device register accesses should not take a long time, so making them asynchronous doesn't seem like a practical advantage. Also this might complicate the code and make it slower. What do you think?
One issue I saw is that, if we register a single fd for e.g two regions. And those two regions were read in parallel from guest. It looks to me we don't have any synchronization in the current code.
Quoted 60 lines
Quoted 51 lines
Quoted 18 lines
virt/kvm/ioregion.c new file +17
-0+ p =kzalloc (sizeof (*p),GFP_KERNEL_ACCOUNT ); +if (!p) { + ret = -ENOMEM ; + goto fail; + } + +INIT_LIST_HEAD (&p->list); + p->paddr = args->guest_paddr; + p->size = args->memory_size; + p->user_data = args->user_data; + p->rf = rfile; + p->wf = wfile; + is_posted_writes = args->flags &KVM_IOREGION_POSTED_WRITES ; + p->posted_writes = is_posted_writes ? true : false; + bus_idx =get_bus_from_flags (args->flags); + +mutex_lock (&kvm->slots_lock);virt/kvm/ioregion.c line 179
Quoted 10 lines
virt/kvm/ioregion.c new file +10
-0+ +if (ioregion_collision (kvm, p, bus_idx)) { + ret = -EEXIST ; + goto unlock_fail; + } +kvm_iodevice_init (&p->dev, &ioregion_ops); + ret =kvm_io_bus_register_dev (kvm, bus_idx, p->paddr, p->size, + &p->dev); +if (ret <0 ) + goto unlock_fail;We probably need to register to
FAST_MMIOwhenbus_idxis MMIO.Quoted 20 lines
virt/kvm/ioregion.c new file +20
-0+list_add_tail (&p->list,get_ioregion_list (kvm, bus_idx)); + +mutex_unlock (&kvm->slots_lock); + +return 0 ; + +unlock_fail: +mutex_unlock (&kvm->slots_lock); +kfree (p); +fail: +fput (rfile); +fput (wfile); + +return ret; +} + +static int +kvm_rm_ioregion (struct kvm *kvm,struct kvm_ioregion *args) +{ +struct ioregion *p, *tmp;Quoted 9 lines
virt/kvm/ioregion.c line 205
Quoted 6 lines
virt/kvm/ioregion.c new file +6
-0+enum kvm_bus bus_idx; +int ret = -ENOENT ; +struct list_head *ioregions; + +if (args->rfd !=-1 || args->wfd !=-1 ) +return -EINVAL ;If we want to use ioregion fd for doorbell, rfd is probably not necessary here.
Quoted 2 lines
This condition is simply a requirement that region can be deleted in the case of both fds are set to -1.
Ok.
Thanks
Quoted 105 lines
Quoted 105 lines
Thanks
Quoted 101 lines
virt/kvm/ioregion.c new file +10
-0+ + bus_idx =get_bus_from_flags (args->flags); + ioregions =get_ioregion_list (kvm, bus_idx); + +mutex_lock (&kvm->slots_lock); + +list_for_each_entry_safe (p, tmp, ioregions, list) { +if (p->paddr == args->guest_paddr && + p->size == args->memory_size) { + kvm_io_bus_unregister_dev(kvm, bus_idx, &p-Quoted 1 line
dev);
virt/kvm/ioregion.c new file +18
-0+ioregion_release (p); + ret =0 ; +break ; + } + } + +mutex_unlock (&kvm->slots_lock); + +return ret; +} + +int +kvm_ioregionfd (struct kvm *kvm,struct kvm_ioregion *args) +{ +if (args->rfd ==-1 || args->wfd ==-1 ) +return kvm_rm_ioregion (kvm, args); +return kvm_set_ioregion (kvm, args); +}virt/kvm/ioregion.h new file +7
-0@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_IOREGION_H__ +#define __KVM_IOREGION_H__ + +#ifdef CONFIG_KVM_IOREGION +inline bool overlap (u64 start1,u64 size1,u64 start2,u64 size2); +bool kvm_ioregion_collides(struct kvm *kvm, int bus_idx, u64start, u64 size);
virt/kvm/ioregion.h new file +3
-0+#else +static inline bool +kvm_ioregion_collides(struct kvm *kvm, int bus_idx, u64 start, u64size)
virt/kvm/ioregion.h new file +5
-0+{ +return false; +} +#endif +#endif virt/kvm/kvm_main.c +1
-0@@ -747,6 +747,7 @@ static struct kvm *kvm_create_vm(unsigned long type) mmgrab(current->mm); kvm->mm = current->mm;kvm_eventfd_init (kvm); +kvm_ioregionfd_init (kvm);mutex_init (&kvm->lock);mutex_init (&kvm->irq_lock);mutex_init(&kvm->slots_lock);virt/kvm/kvm_main.c +16
-3@@ -3708,6 +3709,16 @@ static long kvm_vm_ioctl(struct file *filp, r =kvm_vm_ioctl_set_memory_region (kvm, &kvm_userspace_mem);break ; } +case KVM_SET_IOREGION : { +struct kvm_ioregion data; + + r = -EFAULT ; +if (copy_from_user (&data, argp,sizeof (data))) + goto out; + + r =kvm_ioregionfd (kvm, &data); +break ; + }case KVM_GET_DIRTY_LOG : {struct kvm_dirty_log log;@@ -4301,9 +4312,12 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, if (!bus) return -ENOMEM; -/* exclude ioeventfd which is limited by maximum fd */ -if (bus->dev_count - bus->ioeventfd_count >NR_IOBUS_DEVS -1 ) -return -ENOSPC ; +/* enforce hard limit if kmemcg is disabled and +* exclude ioeventfd which is limited by maximum fd +*/ +if (!memcg_kmem_enabled ()) +if (bus->dev_count - bus->ioeventfd_count >NR_IOBUS_DEVS -1 ) +return -ENOSPC ;new_bus = kmalloc(struct_size(bus, range, bus->dev_count + 1), GFP_KERNEL_ACCOUNT);
Re: [RFC 2/2] KVM: add initial support for ioregionfd blocking read/write operations
In reply to Elena AfanasovaQuoted 29 lines · Elena Afanasova
Quoted 15 lines · Jason Wang
Quoted 14 lines · Elena Afanasova
Signed-off-by Elena Afanasova<[email protected]>--- virt/kvm/ioregion.c | 157 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+)virt/kvm/ioregion.c +0
-0@@ -4,6 +4,33 @@#include <kvm/iodev.h> #include "eventfd.h" Quoted 13 lines
virt/kvm/ioregion.c line 15
Quoted 9 lines
virt/kvm/ioregion.c +9
-0+/* Wire protocol */ +struct ioregionfd_cmd { +__u32 info; +__u32 padding; +__u64 user_data; +__u64 offset; +__u64 data; +}; +I wonder do we need a seq in the protocol. It might be useful if we allow a pair of file descriptors to be used for multiple different ranges.
Quoted 4 lines
I think it might be helpful in the case of out-of-order requests. In the case of in order requests seq field seems not to be necessary since there will be cmds/replies serialization. I’ll include the synchronization code in a RFC v2 series.
See my reply to V1. It might be helpful for the case of using single ioregionfd for multiple ranges.
Thanks
Quoted 8 lines
Quoted 8 lines
Thanks
Quoted 4 lines
virt/kvm/ioregion.c +4
-0+struct ioregionfd_resp { +__u64 data; +__u8 pad[24 ]; +};
Re: [RFC 1/2] KVM: add initial support for KVM_SET_IOREGION
In reply to Jason WangQuoted 12 lines · Jason Wang
Quoted 10 lines · Elena Afanasova
Quoted 4 lines · Jason Wang
Quoted 1 line · Elena Afanasova
This vm ioctl adds or removes an ioregionfd MMIO/PIO region.
How about
FAST_MMIO?I’ll add
KVM_IOREGION_FAST_MMIOflag support. So this may be suitable
for triggers which could use posted writes. The structioregionfd_cmd
size bits and the data field will be unused in this case.
Quoted 4 lines
Note that eventfd checks for length and have datamatch support. Do we
need to do something similar.
Do you think datamatch support is necessary for ioregionfd?
Quoted 4 lines
I guess the idea is to have a generic interface to let eventfd work for
ioregion as well.
It seems that posted writes is the only "fast" case in ioregionfd. So I was thinking about using FAST_MMIO for this case only. Maybe in some cases it will be better to just use ioeventfd. But I'm not sure.
Quoted 141 lines
Quoted 141 lines
Quoted 141 lines
Quoted 129 lines
Guest
read and write accesses are dispatched through the given ioregionfd
instead of returning fromioctl(KVM_RUN). Regions can be deleted by
setting fds to -1.Signed-off-by Elena Afanasova <[email protected]>--- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/Makefile | 1 + arch/x86/kvm/x86.c | 1 + include/linux/kvm_host.h | 17 +++ include/uapi/linux/kvm.h | 23 ++++ virt/kvm/Kconfig | 3 + virt/kvm/eventfd.c | 25 +++++ virt/kvm/eventfd.h | 14 +++ virt/kvm/ioregion.c | 233 +++++++++++++++++++++++++++++++++++++++ virt/kvm/ioregion.h | 15 +++ virt/kvm/kvm_main.c | 20 +++- 11 files changed, 350 insertions(+), 3 deletions(-)create mode 100644
virt/kvm/eventfd.h
create mode 100644virt/kvm/ioregion.c
create mode 100644virt/kvm/ioregion.harch/x86/kvm/Kconfig +1
-0@@ -33,6 +33,7 @@ config KVM select HAVE_KVM_IRQ_BYPASS select HAVE_KVM_IRQ_ROUTING select HAVE_KVM_EVENTFD + select KVM_IOREGION select KVM_ASYNC_PF select USER_RETURN_NOTIFIER select KVM_MMIOarch/x86/kvm/Makefile +0
-0@@ -12,6 +12,7 @@ KVM := ../../../virt/kvm kvm-y += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o \ $(KVM)/eventfd.o$(KVM)/irqchip.o
$(KVM)/vfio.o
kvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o
+kvm-$(CONFIG_KVM_IOREGION) += $(KVM)/ioregion.okvm-y += x86.o emulate.o i8259.o irq.o lapic.o \ i8254.o ioapic.o irq_comm.o cpuid.opmu.o
mtrr.o \arch/x86/kvm/x86.c +0
-0@@ -3739,6 +3739,7 @@ int kvm_vm_ioctl_check_extension(structkvm
*kvm, long ext)
caseKVM_CAP_X86_USER_SPACE_MSR:
caseKVM_CAP_X86_MSR_FILTER:
caseKVM_CAP_ENFORCE_PV_FEATURE_CPUID:arch/x86/kvm/x86.c +1
-0+case KVM_CAP_IOREGIONFD : r =1 ;break ;case KVM_CAP_SYNC_REGS :include/linux/kvm_host.h +4
-0@@ -470,6 +470,10 @@ struct kvm { struct mutex resampler_lock; } irqfds;struct list_head ioeventfds; +#endif +#ifdef CONFIG_KVM_IOREGION +struct list_head ioregions_mmio; +struct list_head ioregions_pio;#endif struct kvm_vm_stat stat;struct kvm_arch arch;@@ -1262,6 +1266,19 @@ static inline int kvm_ioeventfd(structkvm
*kvm, structkvm_ioeventfd*args)#endif /* CONFIG_HAVE_KVM_EVENTFD */include/linux/kvm_host.h +3
-0+#ifdef CONFIG_KVM_IOREGION +void kvm_ioregionfd_init (struct kvm *kvm); +int kvm_ioregionfd(struct kvm *kvm, struct kvm_ioregion*args);
include/linux/kvm_host.h +5
-0+ +#else + +static inline void kvm_ioregionfd_init (struct kvm *kvm) {} +static inline int kvm_ioregionfd(struct kvm *kvm, struct
kvm_ioregion*args)include/linux/kvm_host.h +5
-0+{ +return -ENOSYS ; +} +#endif +void kvm_arch_irq_routing_update (struct kvm *kvm); static inline void kvm_make_request(int req, struct kvm_vcpu*vcpu)
include/uapi/linux/kvm.h +7
-0@@ -732,6 +732,27 @@ struct kvm_ioeventfd {__u8 pad[36 ]; }; +enum { + kvm_ioregion_flag_nr_pio, + kvm_ioregion_flag_nr_posted_writes, + kvm_ioregion_flag_nr_max, +}; + +#define KVM_IOREGION_PIO (1 << kvm_ioregion_flag_nr_pio)include/uapi/linux/kvm.h struct kvm_ioeventfd { · line 751
Quoted 10 lines
include/uapi/linux/kvm.h +8
-0+#define KVM_IOREGION_POSTED_WRITES (1 << kvm_ioregion_flag_nr_posted_writes) + +#define KVM_IOREGION_VALID_FLAG_MASK ((1 << kvm_ioregion_flag_nr_max) - 1) + +struct kvm_ioregion { +__u64 guest_paddr;/* guest physical address */ +__u64 memory_size;/* bytes */ +__u64 user_data;What will this field do? Is it a token?
Quoted 7 lines
Quoted 2 lines
Yes, it’s an opaque token that can be used by userspace in order to determine which MemoryRegion to dispatch.
This part I don't understand. Userspace should know the fd number (which
I guess should be sufficient?).
I think the user_data field can be useful if same fd is registered with multiple GPA ranges.
Quoted 313 lines
Quoted 302 lines
Quoted 11 lines
include/uapi/linux/kvm.h struct kvm_ioeventfd { · line 754
Quoted 6 lines
include/uapi/linux/kvm.h +5
-0+__s32 rfd; +__s32 wfd; +__u32 flags; +__u8 pad[28 ]; +};Is this possible to register the same fd with multiple GPA ranges?
If
not, do we need to check for fd collision?Yes, it’s possible to register the same fd with multiple GPA ranges.
Quoted 279 lines
Quoted 279 lines
include/uapi/linux/kvm.h +1
-0+#define KVM_X86_DISABLE_EXITS_MWAIT (1 << 0)#define KVM_X86_DISABLE_EXITS_HLT (1 << 1) #define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2)include/uapi/linux/kvm.h +2
-0@@ -1053,6 +1074,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_X86_USER_SPACE_MSR 188#define KVM_CAP_X86_MSR_FILTER 189#define KVM_CAP_ENFORCE_PV_FEATURE_CPUID 190 +#define KVM_CAP_IOREGIONFD 191#ifdef KVM_CAP_IRQ_ROUTING @@ -1308,6 +1330,7 @@ struct kvm_vfio_spapr_tce { struct kvm_userspace_memory_region)#define KVM_SET_TSS_ADDR _IO(KVMIO, 0x47)#define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO, 0x48, __u64) +#define KVM_SET_IOREGION _IOW(KVMIO, 0x49, struct kvm_ioregion)/* enable ucontrol for s390 */ struct kvm_s390_ucas_mapping {virt/kvm/Kconfig +3
-0@@ -17,6 +17,9 @@ config HAVE_KVM_EVENTFD bool select EVENTFD +config KVM_IOREGION + bool + config KVM_MMIO boolvirt/kvm/eventfd.c +18
-0@@ -27,6 +27,7 @@#include <trace/events/kvm.h> #include <kvm/iodev.h> +#include "ioregion.h" #ifdef CONFIG_HAVE_KVM_IRQFD @@ -755,6 +756,23 @@ static const struct kvm_io_device_ops ioeventfd_ops = { .destructor = ioeventfd_destructor, }; +#ifdef CONFIG_KVM_IOREGION +/* assumes kvm->slots_lock held */ +bool kvm_eventfd_collides (struct kvm *kvm,int bus_idx, +u64 start,u64 size) +{ +struct _ioeventfd *_p; + +list_for_each_entry (_p, &kvm->ioeventfds, list) +if (_p->bus_idx == bus_idx && +overlap (start, size, _p->addr, + !_p->length ?8 : _p->length)) +return true; + +return false; +} +#endif +/* assumes kvm->slots_lock held */ static boolioeventfd_check_collision(struct kvm *kvm, struct
_ioeventfd
*p)virt/kvm/eventfd.c +4
-0@@ -770,6 +788,13 @@ ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p) _p->datamatch == p->datamatch)))) return true; +#ifdef CONFIG_KVM_IOREGION + if (p->bus_idx == KVM_MMIO_BUS || p->bus_idx == KVM_PIO_BUS) + if (kvm_ioregion_collides(kvm, p->bus_idx, p- >addr, + !p->length ? 8 : p-Quoted 1 line
length))
virt/kvm/eventfd.c +3
-0+return true; +#endif +return false; }virt/kvm/eventfd.h new file +6
-0@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_EVENTFD_H__ +#define __KVM_EVENTFD_H__ + +#ifdef CONFIG_KVM_IOREGION +bool kvm_eventfd_collides(struct kvm *kvm, int bus_idx, u64start,
u64 size);virt/kvm/eventfd.h new file +3
-0+#else +static inline bool +kvm_eventfd_collides(struct kvm *kvm, int bus_idx, u64 start,u64
size)virt/kvm/eventfd.h new file +5
-0+{ +return false; +} +#endif +#endif virt/kvm/ioregion.c new file +42
-0@@ -0,0 +1,233 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <linux/kvm_host.h> +#include <linux/fs.h> +#include <kvm/iodev.h> +#include "eventfd.h" + +void +kvm_ioregionfd_init (struct kvm *kvm) +{ +INIT_LIST_HEAD (&kvm->ioregions_mmio); +INIT_LIST_HEAD (&kvm->ioregions_pio); +} + +struct ioregion { +struct list_head list; +u64 paddr; +u64 size; +struct file *rf; +struct file *wf; +u64 user_data; +struct kvm_io_device dev; +bool posted_writes; +}; + +static inline struct ioregion * +to_ioregion (struct kvm_io_device *dev) +{ + return container_of(dev, struct ioregion, dev); +} + +/* assumes kvm->slots_lock held */ +static void +ioregion_release (struct ioregion *p) +{ +fput (p->rf); +fput (p->wf); +list_del (&p->list); +kfree (p); +} + +static int +ioregion_read(struct kvm_vcpu *vcpu, struct kvm_io_device*this,
gpa_taddr,virt/kvm/ioregion.c new file +7
-0+ int len, void *val) +{ +return 0 ; +} + +static int +ioregion_write(struct kvm_vcpu *vcpu, struct kvm_io_device*this,
gpa_taddr,virt/kvm/ioregion.c new file +7
-0+ int len, const void *val) +{ +return 0 ; +} + +/* +* This function is called as KVM is completely shutting down. We
do not
+ * need to worry about locking just nuke anything we have as quickly as possible
+ */virt/kvm/ioregion.c new file +50
-0+static void +ioregion_destructor (struct kvm_io_device *this) +{ +struct ioregion *p =to_ioregion (this); + +ioregion_release (p); +} + +static const struct kvm_io_device_ops ioregion_ops = { + .read = ioregion_read, + .write = ioregion_write, + .destructor = ioregion_destructor, +}; + +static inline struct list_head * +get_ioregion_list (struct kvm *kvm,enum kvm_bus bus_idx) +{ +return (bus_idx ==KVM_MMIO_BUS ) ? + &kvm->ioregions_mmio : &kvm->ioregions_pio; +} + +/* check for not overlapping case and reverse */ +inline bool +overlap (u64 start1,u64 size1,u64 start2,u64 size2) +{ +u64 end1 = start1 + size1 -1 ; +u64 end2 = start2 + size2 -1 ; + +return !(end1 < start2 || start1 >= end2); +} + +/* assumes kvm->slots_lock held */ +bool +kvm_ioregion_collides (struct kvm *kvm,int bus_idx, +u64 start,u64 size) +{ +struct ioregion *_p; +struct list_head *ioregions; + + ioregions =get_ioregion_list (kvm, bus_idx); +list_for_each_entry (_p, ioregions, list) +if (overlap (start, size, _p->paddr, _p->size)) +return true; + +return false; +} + +/* assumes kvm->slots_lock held */ +static bool +ioregion_collision(struct kvm *kvm, struct ioregion *p, enum
kvm_busbus_idx)virt/kvm/ioregion.c new file +2
-0+{ + if (kvm_ioregion_collides(kvm, bus_idx, p->paddr, p-Quoted 1 line
size) ||
virt/kvm/ioregion.c new file +1
-0+ kvm_eventfd_collides(kvm, bus_idx, p->paddr, p-Quoted 1 line
size))
virt/kvm/ioregion.c new file +25
-0+return true; + +return false; +} + +static enum kvm_bus +get_bus_from_flags (__u32 flags) +{ +if (flags &KVM_IOREGION_PIO ) +return KVM_PIO_BUS ; +return KVM_MMIO_BUS ; +} + +int +kvm_set_ioregion(struct kvm *kvm, struct kvm_ioregion *args) +{ + struct ioregion *p; + bool is_posted_writes; + struct file *rfile, *wfile; + enum kvm_bus bus_idx; + int ret = 0; + + if (!args->memory_size) + return -EINVAL; + if ((args->guest_paddr + args->memory_size - 1) < args-Quoted 1 line
guest_paddr)virt/kvm/ioregion.c new file +10
-0+return -EINVAL ; +if (args->flags & ~KVM_IOREGION_VALID_FLAG_MASK ) +return -EINVAL ; + + rfile =fget (args->rfd); +if (!rfile) +return -EBADF ; + wfile =fget (args->wfd); +if (!wfile) { +fput (rfile);Quoted 9 lines
virt/kvm/ioregion.c line 152
Quoted 7 lines
virt/kvm/ioregion.c new file +3
-0+return -EBADF ; + } + if ((rfile->f_flags & O_NONBLOCK) || (wfile->f_flags &
O_NONBLOCK)) {virt/kvm/ioregion.c new file +3
-0+ ret = -EINVAL ; + goto fail; + }Instead of checking nonblocking, can we poll here?
Quoted 10 lines
Yes, it’s possible. It would be necessary in the case of out-of- order
requests. But since multiple in-flight messages don’t seem to be a use
case I’m not sure if it’s necessary. Typically device register accesses
should not take a long time, so making them asynchronous doesn't seem
like a practical advantage. Also this might complicate the code and make it slower. What do you think?
Quoted 6 lines
One issue I saw is that, if we register a single fd for e.g two regions.
And those two regions were read in parallel from guest. It looks to me
we don't have any synchronization in the current code.
Yes, you are right. That’s why there will be cmds/replies serialization in a v2 series.
Quoted 184 lines
Quoted 63 lines
Quoted 54 lines
Quoted 27 lines
virt/kvm/ioregion.c new file +13
-0+ p =kzalloc (sizeof (*p),GFP_KERNEL_ACCOUNT ); +if (!p) { + ret = -ENOMEM ; + goto fail; + } + +INIT_LIST_HEAD (&p->list); + p->paddr = args->guest_paddr; + p->size = args->memory_size; + p->user_data = args->user_data; + p->rf = rfile; + p->wf = wfile; + is_posted_writes = args->flags &
KVM_IOREGION_POSTED_WRITES;virt/kvm/ioregion.c new file +11
-0+ p->posted_writes = is_posted_writes ? true : false; + bus_idx =get_bus_from_flags (args->flags); + +mutex_lock (&kvm->slots_lock); + +if (ioregion_collision (kvm, p, bus_idx)) { + ret = -EEXIST ; + goto unlock_fail; + } +kvm_iodevice_init (&p->dev, &ioregion_ops); + ret = kvm_io_bus_register_dev(kvm, bus_idx, p->paddr,
p->size,virt/kvm/ioregion.c line 179
Quoted 3 lines
virt/kvm/ioregion.c new file +3
-0+ &p->dev); +if (ret <0 ) + goto unlock_fail;We probably need to register to
FAST_MMIOwhenbus_idxis MMIO.Quoted 21 lines
virt/kvm/ioregion.c new file +1
-0+ list_add_tail(&p->list, get_ioregion_list(kvm,
bus_idx));virt/kvm/ioregion.c new file +19
-0+ +mutex_unlock (&kvm->slots_lock); + +return 0 ; + +unlock_fail: +mutex_unlock (&kvm->slots_lock); +kfree (p); +fail: +fput (rfile); +fput (wfile); + +return ret; +} + +static int +kvm_rm_ioregion (struct kvm *kvm,struct kvm_ioregion *args) +{ +struct ioregion *p, *tmp;Quoted 9 lines
virt/kvm/ioregion.c line 205
Quoted 6 lines
virt/kvm/ioregion.c new file +6
-0+enum kvm_bus bus_idx; +int ret = -ENOENT ; +struct list_head *ioregions; + +if (args->rfd !=-1 || args->wfd !=-1 ) +return -EINVAL ;If we want to use ioregion fd for doorbell, rfd is probably not necessary here.
Quoted 3 lines
This condition is simply a requirement that region can be deleted in
the case of both fds are set to -1.Ok.
Thanks
Quoted 112 lines
Quoted 112 lines
Thanks
Quoted 109 lines
virt/kvm/ioregion.c new file +10
-0+ + bus_idx =get_bus_from_flags (args->flags); + ioregions =get_ioregion_list (kvm, bus_idx); + +mutex_lock (&kvm->slots_lock); + +list_for_each_entry_safe (p, tmp, ioregions, list) { +if (p->paddr == args->guest_paddr && + p->size == args->memory_size) { + kvm_io_bus_unregister_dev(kvm, bus_idx,&p-
Quoted 1 line
dev);
virt/kvm/ioregion.c new file +18
-0+ioregion_release (p); + ret =0 ; +break ; + } + } + +mutex_unlock (&kvm->slots_lock); + +return ret; +} + +int +kvm_ioregionfd (struct kvm *kvm,struct kvm_ioregion *args) +{ +if (args->rfd ==-1 || args->wfd ==-1 ) +return kvm_rm_ioregion (kvm, args); +return kvm_set_ioregion (kvm, args); +}virt/kvm/ioregion.h new file +6
-0@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_IOREGION_H__ +#define __KVM_IOREGION_H__ + +#ifdef CONFIG_KVM_IOREGION +inline bool overlap(u64 start1, u64 size1, u64 start2, u64size2);
+boolkvm_ioregion_collides(struct kvm *kvm, int bus_idx, u64 start, u64 size);virt/kvm/ioregion.h new file +3
-0+#else +static inline bool +kvm_ioregion_collides(struct kvm *kvm, int bus_idx, u64 start,u64
size)virt/kvm/ioregion.h new file +5
-0+{ +return false; +} +#endif +#endif virt/kvm/kvm_main.c +0
-0@@ -747,6 +747,7 @@ static struct kvm *kvm_create_vm(unsignedlong
type)mmgrab(current->mm); kvm->mm = current->mm; kvm_eventfd_init(kvm);virt/kvm/kvm_main.c +11
-0+kvm_ioregionfd_init (kvm);mutex_init (&kvm->lock);mutex_init (&kvm->irq_lock);mutex_init (&kvm->slots_lock);@@ -3708,6 +3709,16 @@ static long kvm_vm_ioctl(struct file *filp, r =kvm_vm_ioctl_set_memory_region (kvm, &kvm_userspace_mem);break ; } +case KVM_SET_IOREGION : { +struct kvm_ioregion data; + + r = -EFAULT ; +if (copy_from_user (&data, argp,sizeof (data))) + goto out; + + r =kvm_ioregionfd (kvm, &data); +break ; + }case KVM_GET_DIRTY_LOG : {struct kvm_dirty_log log;virt/kvm/kvm_main.c +5
-3@@ -4301,9 +4312,12 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, if (!bus) return -ENOMEM; -/* exclude ioeventfd which is limited by maximum fd */ -if (bus->dev_count - bus->ioeventfd_count > NR_IOBUS_DEVS - 1) -return -ENOSPC ; +/* enforce hard limit if kmemcg is disabled and +* exclude ioeventfd which is limited by maximum fd +*/ +if (!memcg_kmem_enabled ()) + if (bus->dev_count - bus->ioeventfd_count >
NR_IOBUS_DEVS- 1)virt/kvm/kvm_main.c +1
-0+return -ENOSPC ; new_bus = kmalloc(struct_size(bus, range, bus-Quoted 1 line
dev_count+ 1),
GFP_KERNEL_ACCOUNT);
Re: [RFC 2/2] KVM: add initial support for ioregionfd blocking read/write operations
In reply to Jason WangQuoted 32 lines · Jason Wang
Quoted 31 lines · Elena Afanasova
Quoted 30 lines · Jason Wang
Quoted 13 lines · Elena Afanasova
Signed-off-by Elena Afanasova<[email protected]>--- virt/kvm/ioregion.c | 157 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+)virt/kvm/ioregion.c +0
-0@@ -4,6 +4,33 @@#include <kvm/iodev.h> #include "eventfd.h" virt/kvm/ioregion.c line 15
Quoted 10 lines
virt/kvm/ioregion.c +9
-0+/* Wire protocol */ +struct ioregionfd_cmd { +__u32 info; +__u32 padding; +__u64 user_data; +__u64 offset; +__u64 data; +}; +I wonder do we need a seq in the protocol. It might be useful if we
allow a pair of file descriptors to be used for multiple different
ranges.
Quoted 9 lines
Quoted 5 lines
I think it might be helpful in the case of out-of-order requests. In the case of in order requests seq field seems not to be necessary
since there will be cmds/replies serialization. I’ll include the synchronization code in a RFC v2 series.See my reply to V1. It might be helpful for the case of using single ioregionfd for multiple ranges.
Ok, thank you!
Quoted 10 lines
Thanks
Quoted 7 lines
Quoted 7 lines
Thanks
Quoted 4 lines
virt/kvm/ioregion.c +4
-0+struct ioregionfd_resp { +__u64 data; +__u8 pad[24 ]; +};
Re: [RFC 1/2] KVM: add initial support for KVM_SET_IOREGION
In reply to Elena AfanasovaQuoted 12 lines · Elena Afanasova
Quoted 11 lines · Jason Wang
Quoted 10 lines · Elena Afanasova
Quoted 4 lines · Jason Wang
Quoted 1 line · Elena Afanasova
This vm ioctl adds or removes an ioregionfd MMIO/PIO region.
How about
FAST_MMIO?I’ll add
KVM_IOREGION_FAST_MMIOflag support. So this may be suitable
for triggers which could use posted writes. The structioregionfd_cmd
size bits and the data field will be unused in this case.
Quoted 5 lines
Quoted 4 lines
Note that eventfd checks for length and have datamatch support. Do we
need to do something similar.Do you think datamatch support is necessary for ioregionfd?
I'm not sure. But if we don't have this support, it probably means we can't use eventfd for ioregionfd.
Quoted 8 lines
Quoted 5 lines
I guess the idea is to have a generic interface to let eventfd work for
ioregion as well.It seems that posted writes is the only "fast" case in ioregionfd. So I was thinking about using
FAST_MMIOfor this case only. Maybe in some cases it will be better to just use ioeventfd. But I'm not sure.
To be a generic infrastructure, it's better to have this, but we can listen from the opinion of others.
Quoted 141 lines
Quoted 141 lines
Quoted 141 lines
Quoted 141 lines
Quoted 129 lines
Guest
read and write accesses are dispatched through the given ioregionfd
instead of returning fromioctl(KVM_RUN). Regions can be deleted by
setting fds to -1.Signed-off-by Elena Afanasova <[email protected]>--- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/Makefile | 1 + arch/x86/kvm/x86.c | 1 + include/linux/kvm_host.h | 17 +++ include/uapi/linux/kvm.h | 23 ++++ virt/kvm/Kconfig | 3 + virt/kvm/eventfd.c | 25 +++++ virt/kvm/eventfd.h | 14 +++ virt/kvm/ioregion.c | 233 +++++++++++++++++++++++++++++++++++++++ virt/kvm/ioregion.h | 15 +++ virt/kvm/kvm_main.c | 20 +++- 11 files changed, 350 insertions(+), 3 deletions(-)create mode 100644
virt/kvm/eventfd.hcreate mode 100644virt/kvm/ioregion.ccreate mode 100644virt/kvm/ioregion.harch/x86/kvm/Kconfig +1
-0@@ -33,6 +33,7 @@ config KVM select HAVE_KVM_IRQ_BYPASS select HAVE_KVM_IRQ_ROUTING select HAVE_KVM_EVENTFD + select KVM_IOREGION select KVM_ASYNC_PF select USER_RETURN_NOTIFIER select KVM_MMIOarch/x86/kvm/Makefile +0
-0@@ -12,6 +12,7 @@ KVM := ../../../virt/kvm kvm-y += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o \ $(KVM)/eventfd.o$(KVM)/irqchip.o
$(KVM)/vfio.okvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o+kvm-$(CONFIG_KVM_IOREGION) += $(KVM)/ioregion.o
kvm-y += x86.o emulate.o i8259.o irq.o lapic.o \ i8254.o ioapic.o irq_comm.o cpuid.opmu.o
mtrr.o \arch/x86/kvm/x86.c +0
-0@@ -3739,6 +3739,7 @@ int kvm_vm_ioctl_check_extension(structkvm
*kvm, long ext)
caseKVM_CAP_X86_USER_SPACE_MSR:
caseKVM_CAP_X86_MSR_FILTER:
caseKVM_CAP_ENFORCE_PV_FEATURE_CPUID:arch/x86/kvm/x86.c +1
-0+case KVM_CAP_IOREGIONFD : r =1 ;break ;case KVM_CAP_SYNC_REGS :include/linux/kvm_host.h +4
-0@@ -470,6 +470,10 @@ struct kvm { struct mutex resampler_lock; } irqfds;struct list_head ioeventfds; +#endif +#ifdef CONFIG_KVM_IOREGION +struct list_head ioregions_mmio; +struct list_head ioregions_pio;#endif struct kvm_vm_stat stat;struct kvm_arch arch;@@ -1262,6 +1266,19 @@ static inline int kvm_ioeventfd(structkvm
*kvm, structkvm_ioeventfd*args)#endif /* CONFIG_HAVE_KVM_EVENTFD */include/linux/kvm_host.h +3
-0+#ifdef CONFIG_KVM_IOREGION +void kvm_ioregionfd_init (struct kvm *kvm); +int kvm_ioregionfd(struct kvm *kvm, struct kvm_ioregion*args);
include/linux/kvm_host.h +5
-0+ +#else + +static inline void kvm_ioregionfd_init (struct kvm *kvm) {} +static inline int kvm_ioregionfd(struct kvm *kvm, struct
kvm_ioregion*args)include/linux/kvm_host.h +5
-0+{ +return -ENOSYS ; +} +#endif +void kvm_arch_irq_routing_update (struct kvm *kvm); static inline void kvm_make_request(int req, struct kvm_vcpu*vcpu)
include/uapi/linux/kvm.h +7
-0@@ -732,6 +732,27 @@ struct kvm_ioeventfd {__u8 pad[36 ]; }; +enum { + kvm_ioregion_flag_nr_pio, + kvm_ioregion_flag_nr_posted_writes, + kvm_ioregion_flag_nr_max, +}; + +#define KVM_IOREGION_PIO (1 << kvm_ioregion_flag_nr_pio)include/uapi/linux/kvm.h struct kvm_ioeventfd { · line 751
Quoted 10 lines
include/uapi/linux/kvm.h +8
-0+#define KVM_IOREGION_POSTED_WRITES (1 << kvm_ioregion_flag_nr_posted_writes) + +#define KVM_IOREGION_VALID_FLAG_MASK ((1 << kvm_ioregion_flag_nr_max) - 1) + +struct kvm_ioregion { +__u64 guest_paddr;/* guest physical address */ +__u64 memory_size;/* bytes */ +__u64 user_data;What will this field do? Is it a token?
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Yes, it’s an opaque token that can be used by userspace in order to determine which MemoryRegion to dispatch.
This part I don't understand. Userspace should know the fd number (which
I guess should be sufficient?).I think the
user_datafield can be useful if same fd is registered with multiple GPA ranges.
Yes, but if I read the code correctly, we encode the address in the protocol. Isn't it sufficient?
Quoted 312 lines
Quoted 312 lines
Quoted 312 lines
Quoted 11 lines
include/uapi/linux/kvm.h struct kvm_ioeventfd { · line 754
Quoted 6 lines
include/uapi/linux/kvm.h +5
-0+__s32 rfd; +__s32 wfd; +__u32 flags; +__u8 pad[28 ]; +};Is this possible to register the same fd with multiple GPA ranges?
If
not, do we need to check for fd collision?Yes, it’s possible to register the same fd with multiple GPA ranges.
Quoted 279 lines
Quoted 279 lines
include/uapi/linux/kvm.h +1
-0+#define KVM_X86_DISABLE_EXITS_MWAIT (1 << 0)#define KVM_X86_DISABLE_EXITS_HLT (1 << 1) #define KVM_X86_DISABLE_EXITS_PAUSE (1 << 2)include/uapi/linux/kvm.h +2
-0@@ -1053,6 +1074,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_X86_USER_SPACE_MSR 188#define KVM_CAP_X86_MSR_FILTER 189#define KVM_CAP_ENFORCE_PV_FEATURE_CPUID 190 +#define KVM_CAP_IOREGIONFD 191#ifdef KVM_CAP_IRQ_ROUTING @@ -1308,6 +1330,7 @@ struct kvm_vfio_spapr_tce { struct kvm_userspace_memory_region)#define KVM_SET_TSS_ADDR _IO(KVMIO, 0x47)#define KVM_SET_IDENTITY_MAP_ADDR _IOW(KVMIO, 0x48, __u64) +#define KVM_SET_IOREGION _IOW(KVMIO, 0x49, struct kvm_ioregion)/* enable ucontrol for s390 */ struct kvm_s390_ucas_mapping {virt/kvm/Kconfig +3
-0@@ -17,6 +17,9 @@ config HAVE_KVM_EVENTFD bool select EVENTFD +config KVM_IOREGION + bool + config KVM_MMIO boolvirt/kvm/eventfd.c +18
-0@@ -27,6 +27,7 @@#include <trace/events/kvm.h> #include <kvm/iodev.h> +#include "ioregion.h" #ifdef CONFIG_HAVE_KVM_IRQFD @@ -755,6 +756,23 @@ static const struct kvm_io_device_ops ioeventfd_ops = { .destructor = ioeventfd_destructor, }; +#ifdef CONFIG_KVM_IOREGION +/* assumes kvm->slots_lock held */ +bool kvm_eventfd_collides (struct kvm *kvm,int bus_idx, +u64 start,u64 size) +{ +struct _ioeventfd *_p; + +list_for_each_entry (_p, &kvm->ioeventfds, list) +if (_p->bus_idx == bus_idx && +overlap (start, size, _p->addr, + !_p->length ?8 : _p->length)) +return true; + +return false; +} +#endif +/* assumes kvm->slots_lock held */ static boolioeventfd_check_collision(struct kvm *kvm, struct
_ioeventfd
*p)virt/kvm/eventfd.c +4
-0@@ -770,6 +788,13 @@ ioeventfd_check_collision(struct kvm *kvm, struct _ioeventfd *p) _p->datamatch == p->datamatch)))) return true; +#ifdef CONFIG_KVM_IOREGION + if (p->bus_idx == KVM_MMIO_BUS || p->bus_idx == KVM_PIO_BUS) + if (kvm_ioregion_collides(kvm, p->bus_idx, p- > addr, + !p->length ? 8 : p-Quoted 1 line
length))
virt/kvm/eventfd.c +3
-0+return true; +#endif +return false; }virt/kvm/eventfd.h new file +6
-0@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_EVENTFD_H__ +#define __KVM_EVENTFD_H__ + +#ifdef CONFIG_KVM_IOREGION +bool kvm_eventfd_collides(struct kvm *kvm, int bus_idx, u64start,
u64 size);virt/kvm/eventfd.h new file +3
-0+#else +static inline bool +kvm_eventfd_collides(struct kvm *kvm, int bus_idx, u64 start,u64
size)virt/kvm/eventfd.h new file +5
-0+{ +return false; +} +#endif +#endif virt/kvm/ioregion.c new file +42
-0@@ -0,0 +1,233 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <linux/kvm_host.h> +#include <linux/fs.h> +#include <kvm/iodev.h> +#include "eventfd.h" + +void +kvm_ioregionfd_init (struct kvm *kvm) +{ +INIT_LIST_HEAD (&kvm->ioregions_mmio); +INIT_LIST_HEAD (&kvm->ioregions_pio); +} + +struct ioregion { +struct list_head list; +u64 paddr; +u64 size; +struct file *rf; +struct file *wf; +u64 user_data; +struct kvm_io_device dev; +bool posted_writes; +}; + +static inline struct ioregion * +to_ioregion (struct kvm_io_device *dev) +{ + return container_of(dev, struct ioregion, dev); +} + +/* assumes kvm->slots_lock held */ +static void +ioregion_release (struct ioregion *p) +{ +fput (p->rf); +fput (p->wf); +list_del (&p->list); +kfree (p); +} + +static int +ioregion_read(struct kvm_vcpu *vcpu, struct kvm_io_device*this,
gpa_taddr,virt/kvm/ioregion.c new file +7
-0+ int len, void *val) +{ +return 0 ; +} + +static int +ioregion_write(struct kvm_vcpu *vcpu, struct kvm_io_device*this,
gpa_taddr,virt/kvm/ioregion.c new file +7
-0+ int len, const void *val) +{ +return 0 ; +} + +/* +* This function is called as KVM is completely shutting down. We
do not
+ * need to worry about locking just nuke anything we have as quickly as possible
+ */virt/kvm/ioregion.c new file +50
-0+static void +ioregion_destructor (struct kvm_io_device *this) +{ +struct ioregion *p =to_ioregion (this); + +ioregion_release (p); +} + +static const struct kvm_io_device_ops ioregion_ops = { + .read = ioregion_read, + .write = ioregion_write, + .destructor = ioregion_destructor, +}; + +static inline struct list_head * +get_ioregion_list (struct kvm *kvm,enum kvm_bus bus_idx) +{ +return (bus_idx ==KVM_MMIO_BUS ) ? + &kvm->ioregions_mmio : &kvm->ioregions_pio; +} + +/* check for not overlapping case and reverse */ +inline bool +overlap (u64 start1,u64 size1,u64 start2,u64 size2) +{ +u64 end1 = start1 + size1 -1 ; +u64 end2 = start2 + size2 -1 ; + +return !(end1 < start2 || start1 >= end2); +} + +/* assumes kvm->slots_lock held */ +bool +kvm_ioregion_collides (struct kvm *kvm,int bus_idx, +u64 start,u64 size) +{ +struct ioregion *_p; +struct list_head *ioregions; + + ioregions =get_ioregion_list (kvm, bus_idx); +list_for_each_entry (_p, ioregions, list) +if (overlap (start, size, _p->paddr, _p->size)) +return true; + +return false; +} + +/* assumes kvm->slots_lock held */ +static bool +ioregion_collision(struct kvm *kvm, struct ioregion *p, enum
kvm_busbus_idx)virt/kvm/ioregion.c new file +2
-0+{ + if (kvm_ioregion_collides(kvm, bus_idx, p->paddr, p-Quoted 1 line
size) ||
virt/kvm/ioregion.c new file +1
-0+ kvm_eventfd_collides(kvm, bus_idx, p->paddr, p-Quoted 1 line
size))
virt/kvm/ioregion.c new file +25
-0+return true; + +return false; +} + +static enum kvm_bus +get_bus_from_flags (__u32 flags) +{ +if (flags &KVM_IOREGION_PIO ) +return KVM_PIO_BUS ; +return KVM_MMIO_BUS ; +} + +int +kvm_set_ioregion(struct kvm *kvm, struct kvm_ioregion *args) +{ + struct ioregion *p; + bool is_posted_writes; + struct file *rfile, *wfile; + enum kvm_bus bus_idx; + int ret = 0; + + if (!args->memory_size) + return -EINVAL; + if ((args->guest_paddr + args->memory_size - 1) < args-Quoted 1 line
guest_paddr)virt/kvm/ioregion.c new file +10
-0+return -EINVAL ; +if (args->flags & ~KVM_IOREGION_VALID_FLAG_MASK ) +return -EINVAL ; + + rfile =fget (args->rfd); +if (!rfile) +return -EBADF ; + wfile =fget (args->wfd); +if (!wfile) { +fput (rfile);Quoted 9 lines
virt/kvm/ioregion.c line 152
Quoted 7 lines
virt/kvm/ioregion.c new file +3
-0+return -EBADF ; + } + if ((rfile->f_flags & O_NONBLOCK) || (wfile->f_flags &
O_NONBLOCK)) {virt/kvm/ioregion.c new file +3
-0+ ret = -EINVAL ; + goto fail; + }Instead of checking nonblocking, can we poll here?
Yes, it’s possible. It would be necessary in the case of out-of- order
requests. But since multiple in-flight messages don’t seem to be a use
case I’m not sure if it’s necessary. Typically device register accesses
should not take a long time, so making them asynchronous doesn't seem
like a practical advantage. Also this might complicate the code and make it slower. What do you think?
Quoted 8 lines
Quoted 6 lines
One issue I saw is that, if we register a single fd for e.g two regions.
And those two regions were read in parallel from guest. It looks to me
we don't have any synchronization in the current code.Yes, you are right. That’s why there will be cmds/replies serialization in a v2 series.
I see.
Thanks
Quoted 183 lines
Quoted 183 lines
Quoted 63 lines
Quoted 54 lines
Quoted 27 lines
virt/kvm/ioregion.c new file +13
-0+ p =kzalloc (sizeof (*p),GFP_KERNEL_ACCOUNT ); +if (!p) { + ret = -ENOMEM ; + goto fail; + } + +INIT_LIST_HEAD (&p->list); + p->paddr = args->guest_paddr; + p->size = args->memory_size; + p->user_data = args->user_data; + p->rf = rfile; + p->wf = wfile; + is_posted_writes = args->flags &
KVM_IOREGION_POSTED_WRITES;virt/kvm/ioregion.c new file +11
-0+ p->posted_writes = is_posted_writes ? true : false; + bus_idx =get_bus_from_flags (args->flags); + +mutex_lock (&kvm->slots_lock); + +if (ioregion_collision (kvm, p, bus_idx)) { + ret = -EEXIST ; + goto unlock_fail; + } +kvm_iodevice_init (&p->dev, &ioregion_ops); + ret = kvm_io_bus_register_dev(kvm, bus_idx, p->paddr,
p->size,virt/kvm/ioregion.c line 179
Quoted 3 lines
virt/kvm/ioregion.c new file +3
-0+ &p->dev); +if (ret <0 ) + goto unlock_fail;We probably need to register to
FAST_MMIOwhenbus_idxis MMIO.Quoted 21 lines
virt/kvm/ioregion.c new file +1
-0+ list_add_tail(&p->list, get_ioregion_list(kvm,
bus_idx));virt/kvm/ioregion.c new file +19
-0+ +mutex_unlock (&kvm->slots_lock); + +return 0 ; + +unlock_fail: +mutex_unlock (&kvm->slots_lock); +kfree (p); +fail: +fput (rfile); +fput (wfile); + +return ret; +} + +static int +kvm_rm_ioregion (struct kvm *kvm,struct kvm_ioregion *args) +{ +struct ioregion *p, *tmp;Quoted 9 lines
virt/kvm/ioregion.c line 205
Quoted 6 lines
virt/kvm/ioregion.c new file +6
-0+enum kvm_bus bus_idx; +int ret = -ENOENT ; +struct list_head *ioregions; + +if (args->rfd !=-1 || args->wfd !=-1 ) +return -EINVAL ;If we want to use ioregion fd for doorbell, rfd is probably not necessary here.
Quoted 3 lines
This condition is simply a requirement that region can be deleted in
the case of both fds are set to -1.Ok.
Thanks
Quoted 112 lines
Quoted 112 lines
Thanks
Quoted 109 lines
virt/kvm/ioregion.c new file +10
-0+ + bus_idx =get_bus_from_flags (args->flags); + ioregions =get_ioregion_list (kvm, bus_idx); + +mutex_lock (&kvm->slots_lock); + +list_for_each_entry_safe (p, tmp, ioregions, list) { +if (p->paddr == args->guest_paddr && + p->size == args->memory_size) { + kvm_io_bus_unregister_dev(kvm, bus_idx,&p-
Quoted 1 line
dev);
virt/kvm/ioregion.c new file +18
-0+ioregion_release (p); + ret =0 ; +break ; + } + } + +mutex_unlock (&kvm->slots_lock); + +return ret; +} + +int +kvm_ioregionfd (struct kvm *kvm,struct kvm_ioregion *args) +{ +if (args->rfd ==-1 || args->wfd ==-1 ) +return kvm_rm_ioregion (kvm, args); +return kvm_set_ioregion (kvm, args); +}virt/kvm/ioregion.h new file +6
-0@@ -0,0 +1,15 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __KVM_IOREGION_H__ +#define __KVM_IOREGION_H__ + +#ifdef CONFIG_KVM_IOREGION +inline bool overlap(u64 start1, u64 size1, u64 start2, u64size2);
+boolkvm_ioregion_collides(struct kvm *kvm, int bus_idx, u64 start, u64 size);virt/kvm/ioregion.h new file +3
-0+#else +static inline bool +kvm_ioregion_collides(struct kvm *kvm, int bus_idx, u64 start,u64
size)virt/kvm/ioregion.h new file +5
-0+{ +return false; +} +#endif +#endif virt/kvm/kvm_main.c +0
-0@@ -747,6 +747,7 @@ static struct kvm *kvm_create_vm(unsignedlong
type)mmgrab(current->mm); kvm->mm = current->mm; kvm_eventfd_init(kvm);virt/kvm/kvm_main.c +11
-0+kvm_ioregionfd_init (kvm);mutex_init (&kvm->lock);mutex_init (&kvm->irq_lock);mutex_init (&kvm->slots_lock);@@ -3708,6 +3709,16 @@ static long kvm_vm_ioctl(struct file *filp, r =kvm_vm_ioctl_set_memory_region (kvm, &kvm_userspace_mem);break ; } +case KVM_SET_IOREGION : { +struct kvm_ioregion data; + + r = -EFAULT ; +if (copy_from_user (&data, argp,sizeof (data))) + goto out; + + r =kvm_ioregionfd (kvm, &data); +break ; + }case KVM_GET_DIRTY_LOG : {struct kvm_dirty_log log;virt/kvm/kvm_main.c +5
-3@@ -4301,9 +4312,12 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, if (!bus) return -ENOMEM; -/* exclude ioeventfd which is limited by maximum fd */ -if (bus->dev_count - bus->ioeventfd_count > NR_IOBUS_DEVS - 1) -return -ENOSPC ; +/* enforce hard limit if kmemcg is disabled and +* exclude ioeventfd which is limited by maximum fd +*/ +if (!memcg_kmem_enabled ()) + if (bus->dev_count - bus->ioeventfd_count >
NR_IOBUS_DEVS- 1)virt/kvm/kvm_main.c +1
-0+return -ENOSPC ; new_bus = kmalloc(struct_size(bus, range, bus-Quoted 1 line
dev_count+ 1),GFP_KERNEL_ACCOUNT);
Re: [RFC 1/2] KVM: add initial support for KVM_SET_IOREGION
Quoted 18 lines · Jason Wang
Quoted 16 lines · Elena Afanasova
Quoted 15 lines · Jason Wang
Quoted 10 lines · Elena Afanasova
Quoted 4 lines · Jason Wang
Quoted 1 line · Elena Afanasova
This vm ioctl adds or removes an ioregionfd MMIO/PIO region.
How about
FAST_MMIO?I’ll add
KVM_IOREGION_FAST_MMIOflag support. So this may be suitable
for triggers which could use posted writes. The structioregionfd_cmd
size bits and the data field will be unused in this case.Note that eventfd checks for length and have datamatch support. Do we
need to do something similar.
Quoted 5 lines
Quoted 1 line
Do you think datamatch support is necessary for ioregionfd?
I'm not sure. But if we don't have this support, it probably means we can't use eventfd for ioregionfd.
This is an interesting question because ioregionfd and ioeventfd have different semantics. While it would be great to support all ioeventfd features in ioregionfd, I'm not sure that is possible. I think ioeventfd will remain useful for devices that only need a doorbell write register.
The differences:
1. ioeventfd has datamatch. This could be implemented in ioregionfd so that a datamatch failure results in the classic ioctl(KVM_RETURN) MMIO/PIO exit reason and the VMM can handle the access.
I'm not sure if this feature is useful though. Most of the time ioregionfd users want to handle all accesses to the region and the VMM may not even know how to handle register accesses because they can only be handled in a dedicated thread or an out-of-process device.
2. Write coalescing. ioeventfd combines writes because an eventfd is a counter. The counter is incremented on each write and the counter is reset to zero by reading the eventfd. This way a slow userspace can read the eventfd just once while a fast guest writes to it many times (similar to interrupt coalescing in physical hardware). ioregionfd cannot really do that, userspace will have to read one struct ioregion_cmd per guest access. Elena and I briefly discussed optimizing this by implementing a custom struct file_operations so the ->read() callback can coalesce multiple writes to the same address, but this makes sense mostly for guest write operations in FAST_MMIO mode, doesn't allow userspace to provide any type of fd (socket, pipe, etc), and increases the complexity.
Keeping in mind that ioeventfd and ioregionfd can be combined, I think the main advantage to supporting all ioeventfd features in ioregionfd is uniformity (offering everything through a single interface).
Supporting ioeventfd features in ioregionfd is possible to an extent but will make ioctl(KVM_SET_IOREGION) more complex and userspace will still have to create multiple fds because ioeventfd-style doorbell write registers have different semantics from regular ioregionfd regions (no posted writes).
My thoughts are that ioregionfd should do what it's good at and ioeventfd should do what it's good at.
Quoted 11 lines
Quoted 7 lines
Quoted 4 lines
I guess the idea is to have a generic interface to let eventfd work for
ioregion as well.It seems that posted writes is the only "fast" case in ioregionfd. So I was thinking about using
FAST_MMIOfor this case only. Maybe in some cases it will be better to just use ioeventfd. But I'm not sure.To be a generic infrastructure, it's better to have this, but we can listen from the opinion of others.
I think we want both FAST_MMIO and regular MMIO options for posted writes:
1. FAST_MMIO - ioregionfd_cmd size and data fields are zero and do not contain information about the nature of the guest access. This is fine for ioeventfd doorbell style registers because we don't need that information.
2. Regular MMIO - ioregionfd_cmd size and data fields contain valid data about the nature of the guest access. This is needed when the device register is more than a simple "kick" doorbell. For example, if the device needs to know the value that the guest wrote.
I suggest defining an additional KVM_SET_IOREGION flag called KVM_IOREGION_FAST_MMIO that can be set together with KVM_IOREGION_POSTED_WRITES.
KVM_IOREGION_PIO cannot be used together with KVM_IOREGION_FAST_MMIO.
In theory KVM_IOREGION_POSTED_WRITES doesn't need to be set with KVM_IOREGION_FAST_MMIO. Userspace would have to send back a struct ioregionfd_resp to acknowledge that the write has been handled.
Read accesses are indistinguishable from write accesses with KVM_IOREGION_FAST_MMIO so it only makes sense to use the flag on write-only regions. If the guest performs a read then userspace will see a write and the destination CPU register will be unchanged (I think this is already the case for FAST_MMIO).
Quoted 141 lines
Quoted 141 lines
Quoted 141 lines
Quoted 135 lines
Quoted 126 lines
Quoted 126 lines
Guest
read and write accesses are dispatched through the given ioregionfd
instead of returning fromioctl(KVM_RUN). Regions can be deleted by
setting fds to -1.Signed-off-by Elena Afanasova <[email protected]>--- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/Makefile | 1 + arch/x86/kvm/x86.c | 1 + include/linux/kvm_host.h | 17 +++ include/uapi/linux/kvm.h | 23 ++++ virt/kvm/Kconfig | 3 + virt/kvm/eventfd.c | 25 +++++ virt/kvm/eventfd.h | 14 +++ virt/kvm/ioregion.c | 233 +++++++++++++++++++++++++++++++++++++++ virt/kvm/ioregion.h | 15 +++ virt/kvm/kvm_main.c | 20 +++- 11 files changed, 350 insertions(+), 3 deletions(-)create mode 100644
virt/kvm/eventfd.h
create mode 100644virt/kvm/ioregion.c
create mode 100644virt/kvm/ioregion.harch/x86/kvm/Kconfig +1
-0@@ -33,6 +33,7 @@ config KVM select HAVE_KVM_IRQ_BYPASS select HAVE_KVM_IRQ_ROUTING select HAVE_KVM_EVENTFD + select KVM_IOREGION select KVM_ASYNC_PF select USER_RETURN_NOTIFIER select KVM_MMIOarch/x86/kvm/Makefile +0
-0@@ -12,6 +12,7 @@ KVM := ../../../virt/kvm kvm-y += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o \ $(KVM)/eventfd.o$(KVM)/irqchip.o
$(KVM)/vfio.okvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o +kvm-$(CONFIG_KVM_IOREGION) += $(KVM)/ioregion.o kvm-y += x86.o emulate.o i8259.o irq.o lapic.o \ i8254.o ioapic.o irq_comm.o cpuid.opmu.o
mtrr.o \arch/x86/kvm/x86.c +0
-0@@ -3739,6 +3739,7 @@ int kvm_vm_ioctl_check_extension(structkvm
*kvm, long ext)
caseKVM_CAP_X86_USER_SPACE_MSR:
caseKVM_CAP_X86_MSR_FILTER:
caseKVM_CAP_ENFORCE_PV_FEATURE_CPUID:arch/x86/kvm/x86.c +1
-0+case KVM_CAP_IOREGIONFD : r =1 ;break ;case KVM_CAP_SYNC_REGS :include/linux/kvm_host.h +4
-0@@ -470,6 +470,10 @@ struct kvm { struct mutex resampler_lock; } irqfds;struct list_head ioeventfds; +#endif +#ifdef CONFIG_KVM_IOREGION +struct list_head ioregions_mmio; +struct list_head ioregions_pio;#endif struct kvm_vm_stat stat;struct kvm_arch arch;@@ -1262,6 +1266,19 @@ static inline int kvm_ioeventfd(structkvm
*kvm, structkvm_ioeventfd*args)#endif /* CONFIG_HAVE_KVM_EVENTFD */include/linux/kvm_host.h +3
-0+#ifdef CONFIG_KVM_IOREGION +void kvm_ioregionfd_init (struct kvm *kvm); +int kvm_ioregionfd(struct kvm *kvm, struct kvm_ioregion*args);
include/linux/kvm_host.h +5
-0+ +#else + +static inline void kvm_ioregionfd_init (struct kvm *kvm) {} +static inline int kvm_ioregionfd(struct kvm *kvm, struct
kvm_ioregion*args)include/linux/kvm_host.h +5
-0+{ +return -ENOSYS ; +} +#endif +void kvm_arch_irq_routing_update (struct kvm *kvm); static inline void kvm_make_request(int req, struct kvm_vcpu*vcpu)
include/uapi/linux/kvm.h +9
-0@@ -732,6 +732,27 @@ struct kvm_ioeventfd {__u8 pad[36 ]; }; +enum { + kvm_ioregion_flag_nr_pio, + kvm_ioregion_flag_nr_posted_writes, + kvm_ioregion_flag_nr_max, +}; + +#define KVM_IOREGION_PIO (1 << kvm_ioregion_flag_nr_pio) +#define KVM_IOREGION_POSTED_WRITES (1 << kvm_ioregion_flag_nr_posted_writes) +Quoted 9 lines
include/uapi/linux/kvm.h struct kvm_ioeventfd { · line 749
Quoted 7 lines
include/uapi/linux/kvm.h +6
-0+#define KVM_IOREGION_VALID_FLAG_MASK ((1 << kvm_ioregion_flag_nr_max) - 1) + +struct kvm_ioregion { +__u64 guest_paddr;/* guest physical address */ +__u64 memory_size;/* bytes */ +__u64 user_data;What will this field do? Is it a token?
Quoted 2 lines
Yes, it’s an opaque token that can be used by userspace in order to determine which MemoryRegion to dispatch.
This part I don't understand. Userspace should know the fd number (which
I guess should be sufficient?).
Quoted 6 lines
Quoted 2 lines
I think the
user_datafield can be useful if same fd is registered with multiple GPA ranges.Yes, but if I read the code correctly, we encode the address in the protocol. Isn't it sufficient?
struct ioregionfd_cmd::offset is a relative address from the start of the ioregion.
The idea is that userspace doesn't need to look up the address. The kernel has already done that and provided an offset that is relative to the start of the ioregion that was registered with KVM_SET_IOREGION.
Userspace uses user_data to determine the device/sub-device/region (e.g. QEMU's MemoryRegion) and passes the offset directly to its ->read()/->write() handler function.
If a userspace program prefers to re-dispatch based on the address then it can set user_data = guest_paddr, but I think most userspace programs will prefer to set user_data to a DeviceState (for simple devices with just one ioregion) or MemoryRegion (for complex devices with multiple ioregions) pointer instead.
Stefan
Re: [RFC 1/2] KVM: add initial support for KVM_SET_IOREGION
Quoted 33 lines · Stefan Hajnoczi
Quoted 13 lines · Jason Wang
Quoted 12 lines · Elena Afanasova
Quoted 11 lines · Jason Wang
Quoted 10 lines · Elena Afanasova
Quoted 4 lines · Jason Wang
Quoted 1 line · Elena Afanasova
This vm ioctl adds or removes an ioregionfd MMIO/PIO region.
How about
FAST_MMIO?I’ll add
KVM_IOREGION_FAST_MMIOflag support. So this may be suitable
for triggers which could use posted writes. The structioregionfd_cmd
size bits and the data field will be unused in this case.Quoted 8 lines
Quoted 5 lines
Quoted 4 lines
Note that eventfd checks for length and have datamatch support. Do we
need to do something similar.Do you think datamatch support is necessary for ioregionfd?
I'm not sure. But if we don't have this support, it probably means we can't use eventfd for ioregionfd.
This is an interesting question because ioregionfd and ioeventfd have different semantics. While it would be great to support all ioeventfd features in ioregionfd, I'm not sure that is possible. I think ioeventfd will remain useful for devices that only need a doorbell write register.
The differences:
1. ioeventfd has datamatch. This could be implemented in ioregionfd so that a datamatch failure results in the classic
ioctl(KVM_RETURN)MMIO/PIO exit reason and the VMM can handle the access.
Quoted 5 lines
I'm not sure if this feature is useful though. Most of the time ioregionfd users want to handle all accesses to the region and the VMM may not even know how to handle register accesses because they can only be handled in a dedicated thread or an out-of-process device.
It's about whether or not the current semantic of ioregion is sufficient for implementing doorbell.
E.g in the case of virtio, the virtqueue index is encoded in the write to the doorbell. And if a single MMIO area is used for all virtqueues, datamatch is probably a must in this case.
Quoted 13 lines
2. Write coalescing. ioeventfd combines writes because an eventfd is a counter. The counter is incremented on each write and the counter is reset to zero by reading the eventfd. This way a slow userspace can read the eventfd just once while a fast guest writes to it many times (similar to interrupt coalescing in physical hardware). ioregionfd cannot really do that, userspace will have to read one struct
ioregion_cmdper guest access. Elena and I briefly discussed optimizing this by implementing a custom structfile_operationsso the ->read() callback can coalesce multiple writes to the same address, but this makes sense mostly for guest write operations inFAST_MMIOmode, doesn't allow userspace to provide any type of fd (socket, pipe, etc), and increases the complexity.
So that's why I suggest to use eBPF instead of a hard-coding a protocol (in the future). We wouldn't have any limitation then.
Quoted 2 lines
Keeping in mind that ioeventfd and ioregionfd can be combined,
That's the motivation of my question :) Use eventfd + ioregreionfd
Quoted 3 lines
I think
the main advantage to supporting all ioeventfd features in ioregionfd is uniformity (offering everything through a single interface).
Yes.
Quoted 9 lines
Supporting ioeventfd features in ioregionfd is possible to an extent but will make
ioctl(KVM_SET_IOREGION)more complex and userspace will still have to create multiple fds because ioeventfd-style doorbell write registers have different semantics from regular ioregionfd regions (no posted writes).My thoughts are that ioregionfd should do what it's good at and ioeventfd should do what it's good at.
Fine with me, and we can leave the rest for the future eBPF extension for ioregionfd.
Quoted 11 lines
Quoted 11 lines
Quoted 8 lines
Quoted 5 lines
I guess the idea is to have a generic interface to let eventfd work for
ioregion as well.It seems that posted writes is the only "fast" case in ioregionfd. So I was thinking about using
FAST_MMIOfor this case only. Maybe in some cases it will be better to just use ioeventfd. But I'm not sure.To be a generic infrastructure, it's better to have this, but we can listen from the opinion of others.
Quoted 7 lines
I think we want both
FAST_MMIOand regular MMIO options for posted writes:1.
FAST_MMIO-ioregionfd_cmdsize and data fields are zero and do not contain information about the nature of the guest access. This is fine for ioeventfd doorbell style registers because we don't need that information.
Is FAST_MMIO always for doorbell? If not, we probably need the size and data.
Quoted 9 lines
2. Regular MMIO -
ioregionfd_cmdsize and data fields contain valid data about the nature of the guest access. This is needed when the device register is more than a simple "kick" doorbell. For example, if the device needs to know the value that the guest wrote.I suggest defining an additional
KVM_SET_IOREGIONflag calledKVM_IOREGION_FAST_MMIOthat can be set together withKVM_IOREGION_POSTED_WRITES.
If we need to expose FAST_MMIO to userspace, we probably need to define its semantics which is probably not easy since it's an architecture optimization.
Quoted 6 lines
KVM_IOREGION_PIOcannot be used together withKVM_IOREGION_FAST_MMIO.In theory
KVM_IOREGION_POSTED_WRITESdoesn't need to be set withKVM_IOREGION_FAST_MMIO. Userspace would have to send back a structioregionfd_respto acknowledge that the write has been handled.
Right, and it also depends on whether or not the hardware support (e.g whether or not it can decode the instructions).
Quoted 6 lines
Read accesses are indistinguishable from write accesses with
KVM_IOREGION_FAST_MMIOso it only makes sense to use the flag on write-only regions. If the guest performs a read then userspace will see a write and the destination CPU register will be unchanged (I think this is already the case forFAST_MMIO).
Yes.
Thanks
Quoted 165 lines
Quoted 142 lines
Quoted 142 lines
Quoted 142 lines
Quoted 136 lines
Quoted 127 lines
Quoted 127 lines
Guest
read and write accesses are dispatched through the given ioregionfd
instead of returning fromioctl(KVM_RUN). Regions can be deleted by
setting fds to -1.Signed-off-by Elena Afanasova <[email protected]>--- arch/x86/kvm/Kconfig | 1 + arch/x86/kvm/Makefile | 1 + arch/x86/kvm/x86.c | 1 + include/linux/kvm_host.h | 17 +++ include/uapi/linux/kvm.h | 23 ++++ virt/kvm/Kconfig | 3 + virt/kvm/eventfd.c | 25 +++++ virt/kvm/eventfd.h | 14 +++ virt/kvm/ioregion.c | 233 +++++++++++++++++++++++++++++++++++++++ virt/kvm/ioregion.h | 15 +++ virt/kvm/kvm_main.c | 20 +++- 11 files changed, 350 insertions(+), 3 deletions(-)create mode 100644
virt/kvm/eventfd.h
create mode 100644virt/kvm/ioregion.c
create mode 100644virt/kvm/ioregion.harch/x86/kvm/Kconfig +1
-0@@ -33,6 +33,7 @@ config KVM select HAVE_KVM_IRQ_BYPASS select HAVE_KVM_IRQ_ROUTING select HAVE_KVM_EVENTFD + select KVM_IOREGION select KVM_ASYNC_PF select USER_RETURN_NOTIFIER select KVM_MMIOarch/x86/kvm/Makefile +0
-0@@ -12,6 +12,7 @@ KVM := ../../../virt/kvm kvm-y += $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o \ $(KVM)/eventfd.o$(KVM)/irqchip.o
$(KVM)/vfio.okvm-$(CONFIG_KVM_ASYNC_PF) += $(KVM)/async_pf.o +kvm-$(CONFIG_KVM_IOREGION) += $(KVM)/ioregion.o kvm-y += x86.o emulate.o i8259.o irq.o lapic.o \ i8254.o ioapic.o irq_comm.o cpuid.opmu.o
mtrr.o \arch/x86/kvm/x86.c +0
-0@@ -3739,6 +3739,7 @@ int kvm_vm_ioctl_check_extension(structkvm
*kvm, long ext)
caseKVM_CAP_X86_USER_SPACE_MSR:
caseKVM_CAP_X86_MSR_FILTER:
caseKVM_CAP_ENFORCE_PV_FEATURE_CPUID:arch/x86/kvm/x86.c +1
-0+case KVM_CAP_IOREGIONFD : r =1 ;break ;case KVM_CAP_SYNC_REGS :include/linux/kvm_host.h +4
-0@@ -470,6 +470,10 @@ struct kvm { struct mutex resampler_lock; } irqfds;struct list_head ioeventfds; +#endif +#ifdef CONFIG_KVM_IOREGION +struct list_head ioregions_mmio; +struct list_head ioregions_pio;#endif struct kvm_vm_stat stat;struct kvm_arch arch;@@ -1262,6 +1266,19 @@ static inline int kvm_ioeventfd(structkvm
*kvm, structkvm_ioeventfd*args)#endif /* CONFIG_HAVE_KVM_EVENTFD */include/linux/kvm_host.h +3
-0+#ifdef CONFIG_KVM_IOREGION +void kvm_ioregionfd_init (struct kvm *kvm); +int kvm_ioregionfd(struct kvm *kvm, struct kvm_ioregion*args);
include/linux/kvm_host.h +5
-0+ +#else + +static inline void kvm_ioregionfd_init (struct kvm *kvm) {} +static inline int kvm_ioregionfd(struct kvm *kvm, struct
kvm_ioregion*args)include/linux/kvm_host.h +5
-0+{ +return -ENOSYS ; +} +#endif +void kvm_arch_irq_routing_update (struct kvm *kvm); static inline void kvm_make_request(int req, struct kvm_vcpu*vcpu)
include/uapi/linux/kvm.h +9
-0@@ -732,6 +732,27 @@ struct kvm_ioeventfd {__u8 pad[36 ]; }; +enum { + kvm_ioregion_flag_nr_pio, + kvm_ioregion_flag_nr_posted_writes, + kvm_ioregion_flag_nr_max, +}; + +#define KVM_IOREGION_PIO (1 << kvm_ioregion_flag_nr_pio) +#define KVM_IOREGION_POSTED_WRITES (1 << kvm_ioregion_flag_nr_posted_writes) +Quoted 9 lines
include/uapi/linux/kvm.h struct kvm_ioeventfd { · line 750
Quoted 7 lines
include/uapi/linux/kvm.h +6
-0+#define KVM_IOREGION_VALID_FLAG_MASK ((1 << kvm_ioregion_flag_nr_max) - 1) + +struct kvm_ioregion { +__u64 guest_paddr;/* guest physical address */ +__u64 memory_size;/* bytes */ +__u64 user_data;What will this field do? Is it a token?
Quoted 2 lines
Yes, it’s an opaque token that can be used by userspace in order to determine which MemoryRegion to dispatch.
This part I don't understand. Userspace should know the fd number (which
I guess should be sufficient?).Quoted 5 lines
Quoted 2 lines
I think the
user_datafield can be useful if same fd is registered with multiple GPA ranges.Yes, but if I read the code correctly, we encode the address in the protocol. Isn't it sufficient?
struct
ioregionfd_cmd::offsetis a relative address from the start of the ioregion.The idea is that userspace doesn't need to look up the address. The kernel has already done that and provided an offset that is relative to the start of the ioregion that was registered with
KVM_SET_IOREGION.Userspace uses
user_datato determine the device/sub-device/region (e.g. QEMU's MemoryRegion) and passes the offset directly to its ->read()/->write() handler function.If a userspace program prefers to re-dispatch based on the address then it can set
user_data=guest_paddr, but I think most userspace programs will prefer to setuser_datato a DeviceState (for simple devices with just one ioregion) or MemoryRegion (for complex devices with multiple ioregions) pointer instead.Stefan
Re: [RFC 1/2] KVM: add initial support for KVM_SET_IOREGION
Quoted 42 lines · Jason Wang
Quoted 33 lines · Stefan Hajnoczi
Quoted 13 lines · Jason Wang
Quoted 12 lines · Elena Afanasova
Quoted 11 lines · Jason Wang
Quoted 10 lines · Elena Afanasova
Quoted 4 lines · Jason Wang
Quoted 1 line · Elena Afanasova
This vm ioctl adds or removes an ioregionfd MMIO/PIO region.
How about
FAST_MMIO?I’ll add
KVM_IOREGION_FAST_MMIOflag support. So this may be suitable
for triggers which could use posted writes. The structioregionfd_cmd
size bits and the data field will be unused in this case.Quoted 8 lines
Quoted 5 lines
Quoted 4 lines
Note that eventfd checks for length and have datamatch support. Do we
need to do something similar.Do you think datamatch support is necessary for ioregionfd?
I'm not sure. But if we don't have this support, it probably means we can't use eventfd for ioregionfd.
This is an interesting question because ioregionfd and ioeventfd have different semantics. While it would be great to support all ioeventfd features in ioregionfd, I'm not sure that is possible. I think ioeventfd will remain useful for devices that only need a doorbell write register.
The differences:
1. ioeventfd has datamatch. This could be implemented in ioregionfd so that a datamatch failure results in the classic
ioctl(KVM_RETURN)MMIO/PIO exit reason and the VMM can handle the access.Quoted 5 lines
I'm not sure if this feature is useful though. Most of the time ioregionfd users want to handle all accesses to the region and the VMM may not even know how to handle register accesses because they can only be handled in a dedicated thread or an out-of-process device.
Quoted 6 lines
It's about whether or not the current semantic of ioregion is sufficient for implementing doorbell.
E.g in the case of virtio, the virtqueue index is encoded in the write to the doorbell. And if a single MMIO area is used for all virtqueues, datamatch is probably a must in this case.
struct ioregionfd_cmd contains not just the register offset, but also the value written by the guest. Therefore datamatch is not necessary.
Datamatch would only be useful as some kind of more complex optimization where different values writtent to the same register dispatch to different fds.
Quoted 13 lines
Quoted 13 lines
Quoted 10 lines
Quoted 7 lines
Quoted 4 lines
I guess the idea is to have a generic interface to let eventfd work for
ioregion as well.It seems that posted writes is the only "fast" case in ioregionfd. So I was thinking about using
FAST_MMIOfor this case only. Maybe in some cases it will be better to just use ioeventfd. But I'm not sure.To be a generic infrastructure, it's better to have this, but we can listen from the opinion of others.
I think we want both
FAST_MMIOand regular MMIO options for posted writes:
Quoted 8 lines
Quoted 4 lines
1.
FAST_MMIO-ioregionfd_cmdsize and data fields are zero and do not contain information about the nature of the guest access. This is fine for ioeventfd doorbell style registers because we don't need that information.Is
FAST_MMIOalways for doorbell? If not, we probably need the size and data.
My understanding is that FAST_MMIO only provides the guest physical address and no additional information. In fact, I'm not even sure if we know whether the access is a read or a write.
So there is extremely limited information to work with and it's basically only useful for doorbell writes.
Quoted 5 lines
Quoted 5 lines
2. Regular MMIO -
ioregionfd_cmdsize and data fields contain valid data about the nature of the guest access. This is needed when the device register is more than a simple "kick" doorbell. For example, if the device needs to know the value that the guest wrote.
Quoted 8 lines
Quoted 3 lines
I suggest defining an additional
KVM_SET_IOREGIONflag calledKVM_IOREGION_FAST_MMIOthat can be set together withKVM_IOREGION_POSTED_WRITES.If we need to expose
FAST_MMIOto userspace, we probably need to define its semantics which is probably not easy since it's an architecture optimization.
Maybe the name KVM_IOREGION_FAST_MMIO name should be changed to something more specific like KVM_IOREGION_OFFSET_ONLY, meaning that only the offset field is valid.
I haven't checked if and how other architectures implement FAST_MMIO, but they will at least be able to provide the offset :).
Quoted 9 lines
Quoted 5 lines
KVM_IOREGION_PIOcannot be used together withKVM_IOREGION_FAST_MMIO.In theory
KVM_IOREGION_POSTED_WRITESdoesn't need to be set withKVM_IOREGION_FAST_MMIO. Userspace would have to send back a structioregionfd_respto acknowledge that the write has been handled.Right, and it also depends on whether or not the hardware support (e.g whether or not it can decode the instructions).
The KVM_IOREGION_FAST_MMIO flag should be documented as an optimization hint. If hardware doesn't support FAST_MMIO then struct ioregionfd_cmd will contain all fields. Userspace will be able to process the cmd either way.
Stefan
Re: [RFC 1/2] KVM: add initial support for KVM_SET_IOREGION
Quoted 46 lines · Stefan Hajnoczi
Quoted 39 lines · Jason Wang
Quoted 32 lines · Stefan Hajnoczi
Quoted 13 lines · Jason Wang
Quoted 12 lines · Elena Afanasova
Quoted 11 lines · Jason Wang
Quoted 10 lines · Elena Afanasova
Quoted 4 lines · Jason Wang
Quoted 1 line · Elena Afanasova
This vm ioctl adds or removes an ioregionfd MMIO/PIO region.
How about
FAST_MMIO?I’ll add
KVM_IOREGION_FAST_MMIOflag support. So this may be suitable
for triggers which could use posted writes. The structioregionfd_cmd
size bits and the data field will be unused in this case.Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Note that eventfd checks for length and have datamatch support. Do we
need to do something similar.Do you think datamatch support is necessary for ioregionfd?
I'm not sure. But if we don't have this support, it probably means we can't use eventfd for ioregionfd.
This is an interesting question because ioregionfd and ioeventfd have different semantics. While it would be great to support all ioeventfd features in ioregionfd, I'm not sure that is possible. I think ioeventfd will remain useful for devices that only need a doorbell write register.
The differences:
1. ioeventfd has datamatch. This could be implemented in ioregionfd so that a datamatch failure results in the classic
ioctl(KVM_RETURN)MMIO/PIO exit reason and the VMM can handle the access.Quoted 5 lines
I'm not sure if this feature is useful though. Most of the time ioregionfd users want to handle all accesses to the region and the VMM may not even know how to handle register accesses because they can only be handled in a dedicated thread or an out-of-process device.
Quoted 6 lines
It's about whether or not the current semantic of ioregion is sufficient for implementing doorbell.
E.g in the case of virtio, the virtqueue index is encoded in the write to the doorbell. And if a single MMIO area is used for all virtqueues, datamatch is probably a must in this case.
Quoted 6 lines
struct
ioregionfd_cmdcontains not just the register offset, but also the value written by the guest. Therefore datamatch is not necessary.Datamatch would only be useful as some kind of more complex optimization where different values writtent to the same register dispatch to different fds.
That's exactly the case of virtio. Consider queue 1,2 shares the MMIO register. We need use datamatch to dispatch the notification to different eventfds.
Quoted 36 lines
Quoted 13 lines
Quoted 13 lines
Quoted 10 lines
Quoted 8 lines
Quoted 5 lines
I guess the idea is to have a generic interface to let eventfd work for
ioregion as well.It seems that posted writes is the only "fast" case in ioregionfd. So I was thinking about using
FAST_MMIOfor this case only. Maybe in some cases it will be better to just use ioeventfd. But I'm not sure.To be a generic infrastructure, it's better to have this, but we can listen from the opinion of others.
I think we want both
FAST_MMIOand regular MMIO options for posted writes:Quoted 7 lines
Quoted 4 lines
1.
FAST_MMIO-ioregionfd_cmdsize and data fields are zero and do not contain information about the nature of the guest access. This is fine for ioeventfd doorbell style registers because we don't need that information.Is
FAST_MMIOalways for doorbell? If not, we probably need the size and data.My understanding is that
FAST_MMIOonly provides the guest physical address and no additional information. In fact, I'm not even sure if we know whether the access is a read or a write.So there is extremely limited information to work with and it's basically only useful for doorbell writes.
Quoted 9 lines
Quoted 8 lines
2. Regular MMIO -
ioregionfd_cmdsize and data fields contain valid data about the nature of the guest access. This is needed when the device register is more than a simple "kick" doorbell. For example, if the device needs to know the value that the guest wrote.I suggest defining an additional
KVM_SET_IOREGIONflag calledKVM_IOREGION_FAST_MMIOthat can be set together withKVM_IOREGION_POSTED_WRITES.
Quoted 6 lines
Quoted 3 lines
If we need to expose
FAST_MMIOto userspace, we probably need to define its semantics which is probably not easy since it's an architecture optimization.Maybe the name
KVM_IOREGION_FAST_MMIOname should be changed to something more specific likeKVM_IOREGION_OFFSET_ONLY, meaning that only the offset field is valid.
Or we can do like what eventfd did, implies FAST_MMIO when memory_size is zero (kvm_assign_ioeventfd()):
if (!args->len && bus_idx == KVM_MMIO_BUS) {
ret = kvm_assign_ioeventfd_idx(kvm, KVM_FAST_MMIO_BUS, args); if (ret < 0)
goto fast_fail;
}
Quoted 10 lines
I haven't checked if and how other architectures implement
FAST_MMIO, but they will at least be able to provide the offset :).Quoted 6 lines
Quoted 5 lines
KVM_IOREGION_PIOcannot be used together withKVM_IOREGION_FAST_MMIO.In theory
KVM_IOREGION_POSTED_WRITESdoesn't need to be set withKVM_IOREGION_FAST_MMIO. Userspace would have to send back a structioregionfd_respto acknowledge that the write has been handled.
Quoted 6 lines
Quoted 2 lines
Right, and it also depends on whether or not the hardware support (e.g whether or not it can decode the instructions).
The
KVM_IOREGION_FAST_MMIOflag should be documented as an optimization hint. If hardware doesn't supportFAST_MMIOthen structioregionfd_cmdwill contain all fields. Userspace will be able to process the cmd either way.
You mean always have a fallback to MMIO for FAST_MMIO? That should be fine but looks less optimal than the implying FAST_MMIO for zero length. I still think introducing "FAST_MMIO" may bring confusion for users ...
Thanks
Quoted 2 lines
Stefan
Re: [RFC 1/2] KVM: add initial support for KVM_SET_IOREGION
Quoted 51 lines · Jason Wang
Quoted 49 lines · Stefan Hajnoczi
Quoted 39 lines · Jason Wang
Quoted 32 lines · Stefan Hajnoczi
Quoted 13 lines · Jason Wang
Quoted 12 lines · Elena Afanasova
Quoted 11 lines · Jason Wang
Quoted 10 lines · Elena Afanasova
Quoted 4 lines · Jason Wang
Quoted 1 line · Elena Afanasova
This vm ioctl adds or removes an ioregionfd MMIO/PIO region.
How about
FAST_MMIO?I’ll add
KVM_IOREGION_FAST_MMIOflag support. So this may be suitable
for triggers which could use posted writes. The structioregionfd_cmd
size bits and the data field will be unused in this case.Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Note that eventfd checks for length and have datamatch support. Do we
need to do something similar.Do you think datamatch support is necessary for ioregionfd?
I'm not sure. But if we don't have this support, it probably means we can't use eventfd for ioregionfd.
This is an interesting question because ioregionfd and ioeventfd have different semantics. While it would be great to support all ioeventfd features in ioregionfd, I'm not sure that is possible. I think ioeventfd will remain useful for devices that only need a doorbell write register.
The differences:
1. ioeventfd has datamatch. This could be implemented in ioregionfd so that a datamatch failure results in the classic
ioctl(KVM_RETURN)MMIO/PIO exit reason and the VMM can handle the access.Quoted 5 lines
I'm not sure if this feature is useful though. Most of the time ioregionfd users want to handle all accesses to the region and the VMM may not even know how to handle register accesses because they can only be handled in a dedicated thread or an out-of-process device.
Quoted 6 lines
It's about whether or not the current semantic of ioregion is sufficient for implementing doorbell.
E.g in the case of virtio, the virtqueue index is encoded in the write to the doorbell. And if a single MMIO area is used for all virtqueues, datamatch is probably a must in this case.
struct
ioregionfd_cmdcontains not just the register offset, but also the value written by the guest. Therefore datamatch is not necessary.
Quoted 8 lines
Quoted 3 lines
Datamatch would only be useful as some kind of more complex optimization where different values writtent to the same register dispatch to different fds.
That's exactly the case of virtio. Consider queue 1,2 shares the MMIO register. We need use datamatch to dispatch the notification to different eventfds.
I can see two options without datamatch:
1. If both virtqueues are handled by the same userspace thread then only 1 fd is needed. ioregionfd sends the value written to the register, so userspace is able to distinguish between the virtqueues.
2. If separate userspace threads process the virtqueues, then set up the virtio-pci capabilities so the virtqueues have separate notification registers:
https://docs.oasis-open.org/virtio/virtio/v1.1/cs01/virtio-v1.1-cs01.html#x1-1150004
With ioeventfd 2 fds are needed in case #1 because the data value written to the register is not communicated to userspace. But ioregionfd does not have this limitation, so I'm not sure whether datamatch is really needed in ioregionfd?
Or is there a use case that I missed?
Quoted 43 lines
Quoted 35 lines
Quoted 12 lines
Quoted 12 lines
Quoted 9 lines
Quoted 7 lines
Quoted 4 lines
I guess the idea is to have a generic interface to let eventfd work for
ioregion as well.It seems that posted writes is the only "fast" case in ioregionfd. So I was thinking about using
FAST_MMIOfor this case only. Maybe in some cases it will be better to just use ioeventfd. But I'm not sure.To be a generic infrastructure, it's better to have this, but we can listen from the opinion of others.
I think we want both
FAST_MMIOand regular MMIO options for posted writes:Quoted 7 lines
Quoted 4 lines
1.
FAST_MMIO-ioregionfd_cmdsize and data fields are zero and do not contain information about the nature of the guest access. This is fine for ioeventfd doorbell style registers because we don't need that information.Is
FAST_MMIOalways for doorbell? If not, we probably need the size and data.My understanding is that
FAST_MMIOonly provides the guest physical address and no additional information. In fact, I'm not even sure if we know whether the access is a read or a write.So there is extremely limited information to work with and it's basically only useful for doorbell writes.
Quoted 9 lines
Quoted 8 lines
2. Regular MMIO -
ioregionfd_cmdsize and data fields contain valid data about the nature of the guest access. This is needed when the device register is more than a simple "kick" doorbell. For example, if the device needs to know the value that the guest wrote.I suggest defining an additional
KVM_SET_IOREGIONflag calledKVM_IOREGION_FAST_MMIOthat can be set together withKVM_IOREGION_POSTED_WRITES.Quoted 6 lines
Quoted 3 lines
If we need to expose
FAST_MMIOto userspace, we probably need to define its semantics which is probably not easy since it's an architecture optimization.Maybe the name
KVM_IOREGION_FAST_MMIOname should be changed to something more specific likeKVM_IOREGION_OFFSET_ONLY, meaning that only the offset field is valid.
Quoted 8 lines
Or we can do like what eventfd did, implies
FAST_MMIOwhenmemory_sizeis zero (kvm_assign_ioeventfd()):if (!args->len &&
bus_idx==KVM_MMIO_BUS) {
ret =kvm_assign_ioeventfd_idx(kvm, KVM_FAST_MMIO_BUS, args); if (ret < 0)
gotofast_fail;
}
Yes!
Quoted 17 lines
Quoted 9 lines
I haven't checked if and how other architectures implement
FAST_MMIO, but they will at least be able to provide the offset :).Quoted 6 lines
Quoted 5 lines
KVM_IOREGION_PIOcannot be used together withKVM_IOREGION_FAST_MMIO.In theory
KVM_IOREGION_POSTED_WRITESdoesn't need to be set withKVM_IOREGION_FAST_MMIO. Userspace would have to send back a structioregionfd_respto acknowledge that the write has been handled.Quoted 6 lines
Quoted 2 lines
Right, and it also depends on whether or not the hardware support (e.g whether or not it can decode the instructions).
The
KVM_IOREGION_FAST_MMIOflag should be documented as an optimization hint. If hardware doesn't supportFAST_MMIOthen structioregionfd_cmdwill contain all fields. Userspace will be able to process the cmd either way.
Quoted 3 lines
You mean always have a fallback to MMIO for
FAST_MMIO? That should be fine but looks less optimal than the implyingFAST_MMIOfor zero length. I still think introducing "FAST_MMIO" may bring confusion for users ...
Regarding the fallback, my understanding is that ioeventfds are always placed on both the MMIO and FAST_MMIO bus when len is zero. That way architectures that don't support FAST_MMIO will still dispatch those ioeventfds. In virt/kvm/eventfd.c:kvm_assign_ioeventfd():
ret = kvm_assign_ioeventfd_idx(kvm, bus_idx, args);
...
if (!args->len && bus_idx == KVM_MMIO_BUS) {
ret = kvm_assign_ioeventfd_idx(kvm, KVM_FAST_MMIO_BUS, args);So ioeventfd is already doing this fallback thing.
Let's follow ioeventfd:
1. len=0 means the size/data fields are not needed. Userspace cannot rely on these fields being valid.
2. There is an automatic fallback to the slow MMIO bus so that slow path accesses are still detected by the ioregion.
The explicit KVM_IOREGION_FAST_MMIO flag I mentioned is not needed.
Stefan
Re: [RFC 1/2] KVM: add initial support for KVM_SET_IOREGION
Quoted 54 lines · Stefan Hajnoczi
Quoted 53 lines · Jason Wang
Quoted 51 lines · Stefan Hajnoczi
Quoted 38 lines · Jason Wang
Quoted 32 lines · Stefan Hajnoczi
Quoted 13 lines · Jason Wang
Quoted 12 lines · Elena Afanasova
Quoted 11 lines · Jason Wang
Quoted 10 lines · Elena Afanasova
Quoted 4 lines · Jason Wang
Quoted 1 line · Elena Afanasova
This vm ioctl adds or removes an ioregionfd MMIO/PIO region.
How about
FAST_MMIO?I’ll add
KVM_IOREGION_FAST_MMIOflag support. So this may be suitable
for triggers which could use posted writes. The structioregionfd_cmd
size bits and the data field will be unused in this case.Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Note that eventfd checks for length and have datamatch support. Do we
need to do something similar.Do you think datamatch support is necessary for ioregionfd?
I'm not sure. But if we don't have this support, it probably means we can't use eventfd for ioregionfd.
This is an interesting question because ioregionfd and ioeventfd have different semantics. While it would be great to support all ioeventfd features in ioregionfd, I'm not sure that is possible. I think ioeventfd will remain useful for devices that only need a doorbell write register.
The differences:
1. ioeventfd has datamatch. This could be implemented in ioregionfd so that a datamatch failure results in the classic
ioctl(KVM_RETURN)MMIO/PIO exit reason and the VMM can handle the access.Quoted 5 lines
I'm not sure if this feature is useful though. Most of the time ioregionfd users want to handle all accesses to the region and the VMM may not even know how to handle register accesses because they can only be handled in a dedicated thread or an out-of-process device.
Quoted 6 lines
It's about whether or not the current semantic of ioregion is sufficient for implementing doorbell.
E.g in the case of virtio, the virtqueue index is encoded in the write to the doorbell. And if a single MMIO area is used for all virtqueues, datamatch is probably a must in this case.
struct
ioregionfd_cmdcontains not just the register offset, but also the value written by the guest. Therefore datamatch is not necessary.Datamatch would only be useful as some kind of more complex optimization where different values writtent to the same register dispatch to different fds.
Quoted 8 lines
Quoted 3 lines
That's exactly the case of virtio. Consider queue 1,2 shares the MMIO register. We need use datamatch to dispatch the notification to different eventfds.
I can see two options without datamatch:
1. If both virtqueues are handled by the same userspace thread then only 1 fd is needed. ioregionfd sends the value written to the register, so userspace is able to distinguish between the virtqueues.
Right.
Quoted 5 lines
2. If separate userspace threads process the virtqueues, then set up the virtio-pci capabilities so the virtqueues have separate notification registers:
https://docs.oasis-open.org/virtio/virtio/v1.1/cs01/virtio-v1.1-cs01.html#x1-1150004
Right. But this works only when PCI transport is used and queue index could be deduced from the register address (separated doorbell).
If we use MMIO or sharing the doorbell registers among all the virtqueues (multiplexer is zero in the above case) , it can't work without datamatch.
Quoted 88 lines
With ioeventfd 2 fds are needed in case #1 because the data value written to the register is not communicated to userspace. But ioregionfd does not have this limitation, so I'm not sure whether datamatch is really needed in ioregionfd?
Or is there a use case that I missed?
Quoted 40 lines
Quoted 33 lines
Quoted 12 lines
Quoted 12 lines
Quoted 9 lines
Quoted 7 lines
Quoted 4 lines
I guess the idea is to have a generic interface to let eventfd work for
ioregion as well.It seems that posted writes is the only "fast" case in ioregionfd. So I was thinking about using
FAST_MMIOfor this case only. Maybe in some cases it will be better to just use ioeventfd. But I'm not sure.To be a generic infrastructure, it's better to have this, but we can listen from the opinion of others.
I think we want both
FAST_MMIOand regular MMIO options for posted writes:Quoted 6 lines
Quoted 4 lines
1.
FAST_MMIO-ioregionfd_cmdsize and data fields are zero and do not contain information about the nature of the guest access. This is fine for ioeventfd doorbell style registers because we don't need that information.Is
FAST_MMIOalways for doorbell? If not, we probably need the size and data.My understanding is that
FAST_MMIOonly provides the guest physical address and no additional information. In fact, I'm not even sure if we know whether the access is a read or a write.So there is extremely limited information to work with and it's basically only useful for doorbell writes.
Quoted 8 lines
Quoted 8 lines
2. Regular MMIO -
ioregionfd_cmdsize and data fields contain valid data about the nature of the guest access. This is needed when the device register is more than a simple "kick" doorbell. For example, if the device needs to know the value that the guest wrote.I suggest defining an additional
KVM_SET_IOREGIONflag calledKVM_IOREGION_FAST_MMIOthat can be set together withKVM_IOREGION_POSTED_WRITES.Quoted 6 lines
Quoted 3 lines
If we need to expose
FAST_MMIOto userspace, we probably need to define its semantics which is probably not easy since it's an architecture optimization.Maybe the name
KVM_IOREGION_FAST_MMIOname should be changed to something more specific likeKVM_IOREGION_OFFSET_ONLY, meaning that only the offset field is valid.Quoted 8 lines
Or we can do like what eventfd did, implies
FAST_MMIOwhenmemory_sizeis zero (kvm_assign_ioeventfd()):if (!args->len &&
bus_idx==KVM_MMIO_BUS) {
ret =kvm_assign_ioeventfd_idx(kvm, KVM_FAST_MMIO_BUS, args); if (ret < 0)
gotofast_fail;
}Yes!
Quoted 10 lines
Quoted 10 lines
I haven't checked if and how other architectures implement
FAST_MMIO, but they will at least be able to provide the offset :).Quoted 7 lines
Quoted 5 lines
KVM_IOREGION_PIOcannot be used together withKVM_IOREGION_FAST_MMIO.In theory
KVM_IOREGION_POSTED_WRITESdoesn't need to be set withKVM_IOREGION_FAST_MMIO. Userspace would have to send back a structioregionfd_respto acknowledge that the write has been handled.Right, and it also depends on whether or not the hardware support (e.g whether or not it can decode the instructions).
Quoted 8 lines
Quoted 4 lines
The
KVM_IOREGION_FAST_MMIOflag should be documented as an optimization hint. If hardware doesn't supportFAST_MMIOthen structioregionfd_cmdwill contain all fields. Userspace will be able to process the cmd either way.You mean always have a fallback to MMIO for
FAST_MMIO? That should be fine but looks less optimal than the implyingFAST_MMIOfor zero length. I still think introducing "FAST_MMIO" may bring confusion for users ...Regarding the fallback, my understanding is that ioeventfds are always placed on both the MMIO and
FAST_MMIObus when len is zero. That way architectures that don't supportFAST_MMIOwill still dispatch those ioeventfds. In virt/kvm/eventfd.c:kvm_assign_ioeventfd():ret = kvm_assign_ioeventfd_idx(kvm, bus_idx, args); ... if (!args->len && bus_idx == KVM_MMIO_BUS) { ret = kvm_assign_ioeventfd_idx(kvm, KVM_FAST_MMIO_BUS, args);So ioeventfd is already doing this fallback thing.
Quoted 7 lines
Let's follow ioeventfd:
1. len=0 means the size/data fields are not needed. Userspace cannot rely on these fields being valid.
2. There is an automatic fallback to the slow MMIO bus so that slow path accesses are still detected by the ioregion.The explicit
KVM_IOREGION_FAST_MMIOflag I mentioned is not needed.
Agreed.
Thanks
Quoted 2 lines
Stefan
Re: [RFC 1/2] KVM: add initial support for KVM_SET_IOREGION
Quoted 18 lines · Jason Wang
Quoted 6 lines · Stefan Hajnoczi
Quoted 5 lines · Jason Wang
Quoted 4 lines · Stefan Hajnoczi
Quoted 3 lines · Jason Wang
Quoted 2 lines · Stefan Hajnoczi
Quoted 1 line · Jason Wang
On 2021/1/5 上午8:02, Elena Afanasova wrote:
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines · Jason Wang
Quoted 3 lines · Elena Afanasova
Quoted 2 lines · Jason Wang
Quoted 1 line · Elena Afanasova
2. If separate userspace threads process the virtqueues, then set up the virtio-pci capabilities so the virtqueues have separate notification registers:
https://docs.oasis-open.org/virtio/virtio/v1.1/cs01/virtio-v1.1-cs01.html#x1-1150004
Quoted 5 lines
Right. But this works only when PCI transport is used and queue index could be deduced from the register address (separated doorbell).
If we use MMIO or sharing the doorbell registers among all the virtqueues (multiplexer is zero in the above case) , it can't work without datamatch.
True. Can you think of an application that needs to dispatch a shared doorbell register to several threads?
If this is a case that real-world applications need then we should tackle it. This is where eBPF would be appropriate. I guess the interface would be something like:
/*
* A custom demultiplexer function that returns the index of the <wfd,
* rfd> pair to use or -1 to produce a KVM_EXIT_IOREGION_FAILURE that
* userspace must handle.
*/
int demux(const struct ioregionfd_cmd *cmd);
Userspace can install an eBPF demux function as well as an array of <wfd, rfd> fd pairs. The demux function gets to look at the cmd in order to decide which fd pair it is sent to.
This is how I think eBPF datamatch could work. It's not as general as in our original discussion where we also talked about custom protocols (instead of struct ioregionfd_cmd/struct ioregionfd_resp).
Stefan
Re: [RFC 1/2] KVM: add initial support for KVM_SET_IOREGION
Quoted 17 lines · Stefan Hajnoczi
Quoted 16 lines · Jason Wang
Quoted 14 lines · Stefan Hajnoczi
Quoted 9 lines · Jason Wang
Quoted 8 lines · Stefan Hajnoczi
Quoted 7 lines · Jason Wang
Quoted 6 lines · Stefan Hajnoczi
Quoted 5 lines · Jason Wang
Quoted 4 lines · Elena Afanasova
Quoted 3 lines · Jason Wang
Quoted 2 lines · Elena Afanasova
Quoted 1 line · Jason Wang
On 2020/12/29 下午6:02, Elena Afanasova wrote:
2. If separate userspace threads process the virtqueues, then set up the virtio-pci capabilities so the virtqueues have separate notification registers:
https://docs.oasis-open.org/virtio/virtio/v1.1/cs01/virtio-v1.1-cs01.html#x1-1150004
Quoted 7 lines
Quoted 5 lines
Right. But this works only when PCI transport is used and queue index could be deduced from the register address (separated doorbell).
If we use MMIO or sharing the doorbell registers among all the virtqueues (multiplexer is zero in the above case) , it can't work without datamatch.
True. Can you think of an application that needs to dispatch a shared doorbell register to several threads?
I think it depends on semantic of doorbell register. I guess one example is the virito-mmio multiqueue device.
Quoted 12 lines
If this is a case that real-world applications need then we should tackle it. This is where eBPF would be appropriate. I guess the interface would be something like:
/*
* A custom demultiplexer function that returns the index of the <wfd,
* rfd> pair to use or -1 to produce aKVM_EXIT_IOREGION_FAILUREthat
* userspace must handle.
*/
intdemux(const struct ioregionfd_cmd *cmd);
Quoted 7 lines
Userspace can install an eBPF demux function as well as an array of <wfd, rfd> fd pairs. The demux function gets to look at the cmd in order to decide which fd pair it is sent to.
This is how I think eBPF datamatch could work. It's not as general as in our original discussion where we also talked about custom protocols (instead of struct ioregionfd_cmd/struct
ioregionfd_resp).
Actually they are not conflict. We can make it a eBPF ioregion, then it's the eBPF program that can decide:
1) whether or not it need to do datamatch
2) how many file descriptors it want to use (store the fd in a map)
3) how will the protocol looks like
But as discussed it could be an add-on on top of the hard logic of ioregion since there could be case that eBPF may not be allowed not not supported. So adding simple datamatch support as a start might be a good choice.
Thanks
Quoted 2 lines
Stefan
Re: [RFC 1/2] KVM: add initial support for KVM_SET_IOREGION
Quoted 22 lines · Jason Wang
Quoted 20 lines · Stefan Hajnoczi
Quoted 19 lines · Jason Wang
Quoted 6 lines · Stefan Hajnoczi
Quoted 5 lines · Jason Wang
Quoted 4 lines · Stefan Hajnoczi
Quoted 3 lines · Jason Wang
Quoted 2 lines · Stefan Hajnoczi
Quoted 1 line · Jason Wang
On 2021/1/5 上午8:02, Elena Afanasova wrote:
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines · Jason Wang
Quoted 3 lines · Elena Afanasova
Quoted 2 lines · Jason Wang
Quoted 1 line · Elena Afanasova
2. If separate userspace threads process the virtqueues, then set up the virtio-pci capabilities so the virtqueues have separate notification registers:
https://docs.oasis-open.org/virtio/virtio/v1.1/cs01/virtio-v1.1-cs01.html#x1-1150004Right. But this works only when PCI transport is used and queue index could be deduced from the register address (separated doorbell).
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
If we use MMIO or sharing the doorbell registers among all the virtqueues (multiplexer is zero in the above case) , it can't work without datamatch.
True. Can you think of an application that needs to dispatch a shared doorbell register to several threads?
I think it depends on semantic of doorbell register. I guess one example is the virito-mmio multiqueue device.
Good point. virtio-mmio really needs datamatch if virtqueues are handled by different threads.
Quoted 23 lines
Quoted 11 lines
If this is a case that real-world applications need then we should tackle it. This is where eBPF would be appropriate. I guess the interface would be something like:
/*
* A custom demultiplexer function that returns the index of the <wfd,
* rfd> pair to use or -1 to produce aKVM_EXIT_IOREGION_FAILUREthat
* userspace must handle.
*/
intdemux(const struct ioregionfd_cmd *cmd);Quoted 7 lines
Userspace can install an eBPF demux function as well as an array of <wfd, rfd> fd pairs. The demux function gets to look at the cmd in order to decide which fd pair it is sent to.
This is how I think eBPF datamatch could work. It's not as general as in our original discussion where we also talked about custom protocols (instead of struct ioregionfd_cmd/struct
ioregionfd_resp).Actually they are not conflict. We can make it a eBPF ioregion, then it's the eBPF program that can decide:
Quoted 7 lines
1) whether or not it need to do datamatch
2) how many file descriptors it want to use (store the fd in a map)
3) how will the protocol looks likeBut as discussed it could be an add-on on top of the hard logic of ioregion since there could be case that eBPF may not be allowed not not supported. So adding simple datamatch support as a start might be a good choice.
Let's go further. Can you share pseudo-code for the eBPF program's function signature (inputs/outputs)?
Stefan
Re: [RFC 1/2] KVM: add initial support for KVM_SET_IOREGION
Quoted 57 lines · Stefan Hajnoczi
Quoted 20 lines · Jason Wang
Quoted 19 lines · Stefan Hajnoczi
Quoted 18 lines · Jason Wang
Quoted 6 lines · Stefan Hajnoczi
Quoted 5 lines · Jason Wang
Quoted 4 lines · Stefan Hajnoczi
Quoted 3 lines · Jason Wang
Quoted 2 lines · Stefan Hajnoczi
Quoted 1 line · Jason Wang
On 2021/1/5 上午8:02, Elena Afanasova wrote:
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines · Jason Wang
Quoted 3 lines · Elena Afanasova
Quoted 2 lines · Jason Wang
On 2020/12/29 下午6:02, Elena Afanasova wrote:
2. If separate userspace threads process the virtqueues, then set up the virtio-pci capabilities so the virtqueues have separate notification registers:
https://docs.oasis-open.org/virtio/virtio/v1.1/cs01/virtio-v1.1-cs01.html#x1-1150004Right. But this works only when PCI transport is used and queue index could be deduced from the register address (separated doorbell).
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
If we use MMIO or sharing the doorbell registers among all the virtqueues (multiplexer is zero in the above case) , it can't work without datamatch.
True. Can you think of an application that needs to dispatch a shared doorbell register to several threads?
I think it depends on semantic of doorbell register. I guess one example is the virito-mmio multiqueue device.
Good point. virtio-mmio really needs datamatch if virtqueues are handled by different threads.
Quoted 26 lines
Quoted 11 lines
If this is a case that real-world applications need then we should tackle it. This is where eBPF would be appropriate. I guess the interface would be something like:
/*
* A custom demultiplexer function that returns the index of the <wfd,
* rfd> pair to use or -1 to produce aKVM_EXIT_IOREGION_FAILUREthat
* userspace must handle.
*/
intdemux(const struct ioregionfd_cmd *cmd);Quoted 7 lines
Userspace can install an eBPF demux function as well as an array of <wfd, rfd> fd pairs. The demux function gets to look at the cmd in order to decide which fd pair it is sent to.
This is how I think eBPF datamatch could work. It's not as general as in our original discussion where we also talked about custom protocols (instead of struct ioregionfd_cmd/struct
ioregionfd_resp).Actually they are not conflict. We can make it a eBPF ioregion, then it's the eBPF program that can decide:
1) whether or not it need to do datamatch
2) how many file descriptors it want to use (store the fd in a map)
3) how will the protocol looks like
Quoted 5 lines
Quoted 3 lines
But as discussed it could be an add-on on top of the hard logic of ioregion since there could be case that eBPF may not be allowed not not supported. So adding simple datamatch support as a start might be a good choice.
Let's go further. Can you share pseudo-code for the eBPF program's function signature (inputs/outputs)?
It could be something like this:
1) The eBPF program context could be defined as ioregion_ctx:
struct ioregion_ctx {
gpa_t addr;
int len;
void *val;
};
2) The eBPF program return value could be, 0 (IOREGION_OK) means that the the program can handle this I/O request, otherwise failure (IOREGION_FAIL)
So for implementing the datamatch, userspace is required to stored the file descriptors for doorbell dispatching in a map (dispatch_map). For virtio style doorbell, we can simply:
- find the fd via bpf map lookup
- build the protocol
- use the eBPF helper to send the command (I don't check but I guess we need invent new eBPF helpers for read and write from a file)
Like:
SEC("datamatch")
int datamatch_prog(struct ioregion_ctx *ctx)
{
int *fd, ret;
struct customized_protocol protocol;
fd = bpf_map_lookup_elem(&ctx->val, &dispatch_map); if (!fd)
return IOREGION_FAIL; build_protocol(ctx, &protocol);
ret = bpf_fd_write(fd, &protocol, sizeof(protocol); if (ret != sizeof(protocol))
return IOREGION_FAIL;
return IOREGION_OK;
}
Thanks
Quoted 2 lines
Stefan