KVM: PPC: Book3S HV: add XIVE native exploitation mode
Subsystems (4)
v4 cover KVM: PPC: Book3S HV: add XIVE native exploitation mode
Hello,
On the POWER9 processor, the XIVE interrupt controller can control interrupt sources using MMIOs to trigger events, to EOI or to turn off the sources. Priority management and interrupt acknowledgment is also controlled by MMIO in the CPU presenter sub-engine.
PowerNV/baremetal Linux runs natively under XIVE but sPAPR guests need special support from the hypervisor to do the same. This is called the XIVE native exploitation mode and today, it can be activated under the PowerPC Hypervisor, pHyp. However, Linux/KVM lacks XIVE native support and still offers the old interrupt mode interface using a KVM device implementing the XICS hcalls over XIVE.
The following series is proposal to add the same support under KVM.
A new KVM device is introduced for the XIVE native exploitation mode. It reuses most of the XICS-over-XIVE glue implementation structures which are internal to KVM but has a completely different interface. A set of KVM device ioctls provide support for the hypervisor calls, all handled in QEMU, to configure the sources and the event queues. From there, all interrupt control is transferred to the guest which can use MMIOs.
These MMIO regions (ESB and TIMA) are exposed to guests in QEMU, similarly to VFIO, and the associated VMAs are populated dynamically with the appropriate pages using a fault handler. These are now implemented using mmap()s of the KVM device fd.
Migration has its own specific needs regarding memory. The patchset provides a specific control to quiesce XIVE before capturing the memory. The save and restore of the internal state is based on the same ioctls used for the hcalls.
On a POWER9 sPAPR machine, the Client Architecture Support (CAS) negotiation process determines whether the guest operates with a interrupt controller using the XICS legacy model, as found on POWER8, or in XIVE exploitation mode. Which means that the KVM interrupt device should be created at run-time, after the machine has started. This requires extra support from KVM to destroy KVM devices. It is introduced at the end of the patchset as it still requires some attention and a XIVE-only VM would not need.
This is based on 5.1-rc1 and should be a candidate for 5.2 now. The OPAL patches have not yet been merged.
GitHub trees available here :
QEMU sPAPR:
https://github.com/legoater/qemu/commits/xive-next
Linux/KVM:
https://github.com/legoater/linux/commits/xive-5.1
OPAL:
https://github.com/legoater/skiboot/commits/xive
Thanks,
C.
Caveats :
- We should introduce a set of definitions common to XIVE and XICS
- The XICS-over-XIVE device file book3s_xive.c could be renamed to book3s_xics_on_xive.c or book3s_xics_p9.c
- The XICS-over-XIVE device has locking issues in the setup.
Changes since v3:
- removed a couple of useless includes
- fix the test ont the initial setting of the EQ toggle bit : 0 -> 1
- renamed qsize to qshift
- renamed qpage to qaddr
- checked host page size
- limited flags to KVM_XIVE_EQ_ALWAYS_NOTIFY to fit sPAPR specs
- Fixed xive_timaval description in documentation
Changes since v2:
- removed extra OPAL call definitions
- removed ->q_order setting. Only useful in the XICS-on-XIVE KVM device which allocates the EQs on behalf of the guest.
- returned -ENXIO when VP base is invalid
- made use of the xive_vp() macro to compute VP identifiers
- reworked locking in kvmppc_xive_native_connect_vcpu() to fix races
- stop advertising KVM_CAP_PPC_IRQ_XIVE as support is not fully available yet
- fixed comment on XIVE IRQ number space
- removed usage of the __x_* macros
- fixed locking on source block
- fixed comments on the KVM device attribute definitions
- handled MASKED EAS configuration
- fixed check on supported EQ size to restrict to 64K pages
- checked kvm_eq.flags that need to be zero
- removed the OPAL call when EQ qtoggle bit and index are zero.
- reduced the size of kvmppc_one_reg timaval attribute to two u64s
- stopped returning of the OS CAM line value
Changes since v1:
- Better documentation (was missing)
- Nested support. XIVE not advertised on non PowerNV platforms. This is a good way to test the fallback on QEMU emulated devices.
- ESB and TIMA special mapping done using the KVM device fd
- All hcalls moved to QEMU. Dropped the patch moving the hcall flags.
- Reworked of the KVM device ioctl controls to support hcalls and migration needs to capture/save states
- Merged the control syncing XIVE and marking the EQ page dirty
- Fixed passthrough support using the KVM device file address_space to clear the ESB pages from the mapping
- Misc enhancements and fixes
Cédric Le Goater (17):
powerpc/xive: add OPAL extensions for the XIVE native exploitation
support
KVM: PPC: Book3S HV: add a new KVM device for the XIVE native
exploitation mode
KVM: PPC: Book3S HV: XIVE: introduce a new capability
KVM_CAP_PPC_IRQ_XIVE
KVM: PPC: Book3S HV: XIVE: add a control to initialize a source
KVM: PPC: Book3S HV: XIVE: add a control to configure a source
KVM: PPC: Book3S HV: XIVE: add controls for the EQ configuration
KVM: PPC: Book3S HV: XIVE: add a global reset control
KVM: PPC: Book3S HV: XIVE: add a control to sync the sources
KVM: PPC: Book3S HV: XIVE: add a control to dirty the XIVE EQ pages
KVM: PPC: Book3S HV: XIVE: add get/set accessors for the VP XIVE state
KVM: introduce a 'mmap' method for KVM devices
KVM: PPC: Book3S HV: XIVE: add a TIMA mapping
KVM: PPC: Book3S HV: XIVE: add a mapping for the source ESB pages
KVM: PPC: Book3S HV: XIVE: add passthrough support
KVM: PPC: Book3S HV: XIVE: activate XIVE exploitation mode
KVM: introduce a KVM_DESTROY_DEVICE ioctl
KVM: PPC: Book3S HV: XIVE: clear the vCPU interrupt presenters
arch/powerpc/include/asm/kvm_host.h | 2 + arch/powerpc/include/asm/kvm_ppc.h | 32 + arch/powerpc/include/asm/opal-api.h | 7 +- arch/powerpc/include/asm/opal.h | 7 + arch/powerpc/include/asm/xive.h | 17 + arch/powerpc/include/uapi/asm/kvm.h | 46 + arch/powerpc/kvm/book3s_xive.h | 36 + include/linux/kvm_host.h | 1 + include/uapi/linux/kvm.h | 10 + arch/powerpc/kvm/book3s.c | 31 +- arch/powerpc/kvm/book3s_xics.c | 19 + arch/powerpc/kvm/book3s_xive.c | 170 ++- arch/powerpc/kvm/book3s_xive_native.c | 1205 ++++++++++++++++++++ arch/powerpc/kvm/powerpc.c | 37 + arch/powerpc/platforms/powernv/opal-call.c | 3 + arch/powerpc/sysdev/xive/native.c | 110 ++ virt/kvm/kvm_main.c | 52 + Documentation/virtual/kvm/api.txt | 29 + Documentation/virtual/kvm/devices/xive.txt | 197 ++++ arch/powerpc/kvm/Makefile | 2 +- 20 files changed, 1953 insertions(+), 60 deletions(-) create mode 100644 arch/powerpc/kvm/book3s_xive_native.c create mode 100644 Documentation/virtual/kvm/devices/xive.txt
Signature · 1 line
2.20.1
PATCH v4 1/17 powerpc/xive: add OPAL extensions for the XIVE native exploitation support
The support for XIVE native exploitation mode in Linux/KVM needs a couple more OPAL calls to get and set the state of the XIVE internal structures being used by a sPAPR guest.
---
Changes since v3:
- rebased on 5.1-rc1
Changes since v2:
- remove extra OPAL call definitions
arch/powerpc/include/asm/opal-api.h | 7 +- arch/powerpc/include/asm/opal.h | 7 ++ arch/powerpc/include/asm/xive.h | 14 +++ arch/powerpc/platforms/powernv/opal-call.c | 3 + arch/powerpc/sysdev/xive/native.c | 99 ++++++++++++++++++++++ 5 files changed, 127 insertions(+), 3 deletions(-)
arch/powerpc/include/asm/opal-api.h +4 -3
@@ -186,8 +186,8 @@#define OPAL_XIVE_FREE_IRQ 140#define OPAL_XIVE_SYNC 141#define OPAL_XIVE_DUMP 142 -#define OPAL_XIVE_RESERVED3 143 -#define OPAL_XIVE_RESERVED4 144 +#define OPAL_XIVE_GET_QUEUE_STATE 143 +#define OPAL_XIVE_SET_QUEUE_STATE 144#define OPAL_SIGNAL_SYSTEM_RESET 145#define OPAL_NPU_INIT_CONTEXT 146#define OPAL_NPU_DESTROY_CONTEXT 147
@@ -210,7 +210,8 @@#define OPAL_PCI_GET_PBCQ_TUNNEL_BAR 164#define OPAL_PCI_SET_PBCQ_TUNNEL_BAR 165#define OPAL_NX_COPROC_INIT 167 -#define OPAL_LAST 167 +#define OPAL_XIVE_GET_VP_STATE 170 +#define OPAL_LAST 170#define QUIESCE_HOLD 1/* Spin all calls at entry */ #define QUIESCE_REJECT 2/* Fail all calls with OPAL_BUSY */
arch/powerpc/include/asm/opal.h +7 -0
@@ -279,6 +279,13 @@ int64_t opal_xive_allocate_irq(uint32_t chip_id);int64_t opal_xive_free_irq (uint32_t girq);int64_t opal_xive_sync (uint32_t type,uint32_t id);int64_t opal_xive_dump (uint32_t type,uint32_t id); +int64_t opal_xive_get_queue_state (uint64_t vp,uint32_t prio, +__be32 *out_qtoggle, +__be32 *out_qindex); +int64_t opal_xive_set_queue_state (uint64_t vp,uint32_t prio, +uint32_t qtoggle, +uint32_t qindex); +int64_t opal_xive_get_vp_state (uint64_t vp,__be64 *out_w01);int64_t opal_pci_set_p2p (uint64_t phb_init,uint64_t phb_target,uint64_t desc,uint16_t pe_number);
arch/powerpc/include/asm/xive.h +14 -0
@@ -109,12 +109,26 @@ extern int xive_native_configure_queue(u32 vp_id, struct xive_q *q, u8 prio,extern void xive_native_disable_queue (u32 vp_id,struct xive_q *q,u8 prio);extern void xive_native_sync_source (u32 hw_irq); +extern void xive_native_sync_queue (u32 hw_irq);extern bool is_xive_irq (struct irq_chip *chip);extern int xive_native_enable_vp (u32 vp_id,bool single_escalation);extern int xive_native_disable_vp (u32 vp_id);extern int xive_native_get_vp_info (u32 vp_id,u32 *out_cam_id,u32 *out_chip_id);extern bool xive_native_has_single_escalation (void ); +extern int xive_native_get_queue_info (u32 vp_id,uint32_t prio, +u64 *out_qpage, +u64 *out_qsize, +u64 *out_qeoi_page, +u32 *out_escalate_irq, +u64 *out_qflags); + +extern int xive_native_get_queue_state (u32 vp_id,uint32_t prio,u32 *qtoggle, +u32 *qindex); +extern int xive_native_set_queue_state (u32 vp_id,uint32_t prio,u32 qtoggle, +u32 qindex); +extern int xive_native_get_vp_state (u32 vp_id,u64 *out_state); +#else static inline bool xive_enabled (void ) {return false; }
arch/powerpc/platforms/powernv/opal-call.c +3 -0
@@ -260,6 +260,9 @@ OPAL_CALL(opal_xive_get_vp_info, OPAL_XIVE_GET_VP_INFO);OPAL_CALL (opal_xive_set_vp_info,OPAL_XIVE_SET_VP_INFO );OPAL_CALL (opal_xive_sync,OPAL_XIVE_SYNC );OPAL_CALL (opal_xive_dump,OPAL_XIVE_DUMP ); +OPAL_CALL (opal_xive_get_queue_state,OPAL_XIVE_GET_QUEUE_STATE ); +OPAL_CALL (opal_xive_set_queue_state,OPAL_XIVE_SET_QUEUE_STATE ); +OPAL_CALL (opal_xive_get_vp_state,OPAL_XIVE_GET_VP_STATE );OPAL_CALL (opal_signal_system_reset,OPAL_SIGNAL_SYSTEM_RESET );OPAL_CALL (opal_npu_init_context,OPAL_NPU_INIT_CONTEXT );OPAL_CALL (opal_npu_destroy_context,OPAL_NPU_DESTROY_CONTEXT );
arch/powerpc/sysdev/xive/native.c +99 -0
108 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v4 2/17 KVM: PPC: Book3S HV: add a new KVM device for the XIVE native exploitation mode
In reply to Cédric Le GoaterThis is the basic framework for the new KVM device supporting the XIVE native exploitation mode. The user interface exposes a new KVM device to be created by QEMU, only available when running on a L0 hypervisor. Support for nested guests is not available yet.
The XIVE device reuses the device structure of the XICS-on-XIVE device as they have a lot in common. That could possibly change in the future if the need arise.
---
Changes since v3:
- removed a couple of useless includes
Changes since v2:
- removed ->q_order setting. Only useful in the XICS-on-XIVE KVM device which allocates the EQs on behalf of the guest.
- returned -ENXIO when VP base is invalid
arch/powerpc/include/asm/kvm_host.h | 1 + arch/powerpc/include/asm/kvm_ppc.h | 8 + arch/powerpc/include/uapi/asm/kvm.h | 3 + include/uapi/linux/kvm.h | 2 + arch/powerpc/kvm/book3s.c | 7 +- arch/powerpc/kvm/book3s_xive_native.c | 179 +++++++++++++++++++++ Documentation/virtual/kvm/devices/xive.txt | 19 +++ arch/powerpc/kvm/Makefile | 2 +- 8 files changed, 219 insertions(+), 2 deletions(-) create mode 100644 arch/powerpc/kvm/book3s_xive_native.c create mode 100644 Documentation/virtual/kvm/devices/xive.txt
arch/powerpc/include/asm/kvm_host.h +1 -0
@@ -222,6 +222,7 @@ extern struct kvm_device_ops kvm_xics_ops;struct kvmppc_xive ;struct kvmppc_xive_vcpu ;extern struct kvm_device_ops kvm_xive_ops; +extern struct kvm_device_ops kvm_xive_native_ops;struct kvmppc_passthru_irqmap ;
arch/powerpc/include/asm/kvm_ppc.h +8 -0
@@ -594,6 +594,10 @@ extern int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval);extern int kvmppc_xive_set_irq (struct kvm *kvm,int irq_source_id,u32 irq,int level,bool line_status);extern void kvmppc_xive_push_vcpu (struct kvm_vcpu *vcpu); + +extern void kvmppc_xive_native_init_module (void ); +extern void kvmppc_xive_native_exit_module (void ); +#else static inline int kvmppc_xive_set_xive (struct kvm *kvm,u32 irq,u32 server,u32 priority) {return -1 ; }
@@ -617,6 +621,10 @@ static inline int kvmppc_xive_set_icp(struct kvm_vcpu *vcpu, u64 icpval) { returstatic inline int kvmppc_xive_set_irq (struct kvm *kvm,int irq_source_id,u32 irq,int level,bool line_status) {return -ENODEV ; }static inline void kvmppc_xive_push_vcpu (struct kvm_vcpu *vcpu) { } + +static inline void kvmppc_xive_native_init_module (void ) { } +static inline void kvmppc_xive_native_exit_module (void ) { } +#endif /* CONFIG_KVM_XIVE */ #if defined(CONFIG_PPC_POWERNV ) && defined(CONFIG_KVM_BOOK3S_64_HANDLER )
arch/powerpc/include/uapi/asm/kvm.h +3 -0
@@ -677,4 +677,7 @@ struct kvm_ppc_cpu_char {#define KVM_XICS_PRESENTED (1ULL << 43)#define KVM_XICS_QUEUED (1ULL << 44) +/* POWER9 XIVE Native Interrupt Controller */ +#define KVM_DEV_XIVE_GRP_CTRL 1 +#endif /* __LINUX_KVM_POWERPC_H */
include/uapi/linux/kvm.h +2 -0
@@ -1211,6 +1211,8 @@ enum kvm_device_type {#define KVM_DEV_TYPE_ARM_VGIC_V3 KVM_DEV_TYPE_ARM_VGIC_V3 KVM_DEV_TYPE_ARM_VGIC_ITS,#define KVM_DEV_TYPE_ARM_VGIC_ITS KVM_DEV_TYPE_ARM_VGIC_ITS + KVM_DEV_TYPE_XIVE, +#define KVM_DEV_TYPE_XIVE KVM_DEV_TYPE_XIVE KVM_DEV_TYPE_MAX, };
arch/powerpc/kvm/book3s.c +6 -1
@@ -1050,6 +1050,9 @@ static int kvmppc_book3s_init(void)if (xics_on_xive ()) {kvmppc_xive_init_module ();kvm_register_device_ops (&kvm_xive_ops,KVM_DEV_TYPE_XICS ); +kvmppc_xive_native_init_module (); +kvm_register_device_ops (&kvm_xive_native_ops, +KVM_DEV_TYPE_XIVE ); } else#endif kvm_register_device_ops (&kvm_xics_ops,KVM_DEV_TYPE_XICS );
@@ -1060,8 +1063,10 @@ static int kvmppc_book3s_init(void)static void kvmppc_book3s_exit (void ) {#ifdef CONFIG_KVM_XICS -if (xics_on_xive ()) +if (xics_on_xive ()) {kvmppc_xive_exit_module (); +kvmppc_xive_native_exit_module (); + }#endif #ifdef CONFIG_KVM_BOOK3S_32_HANDLER kvmppc_book3s_exit_pr ();
arch/powerpc/kvm/book3s_xive_native.c new file +179 -0
179 lines not on this page · raw message
Documentation/virtual/kvm/devices/xive.txt new file +19 -0
@@ -0,0 +1,19 @@ +POWER9 eXternal Interrupt Virtualization Engine (XIVE Gen1) +========================================================== + +Device types supported: + KVM_DEV_TYPE_XIVE POWER9 XIVE Interrupt Controller generation 1 + +This device acts as a VM interrupt controller. It provides the KVM +interface to configure the interrupt sources of a VM in the underlying +POWER9 XIVE interrupt controller. + +Only one XIVE instance may be instantiated. A guest XIVE device +requires a POWER9 host and the guest OS should have support for the +XIVE native exploitation interrupt mode. If not, it should run using +the legacy interrupt mode, referred as XICS (POWER7/8). + +* Groups: + + 1. KVM_DEV_XIVE_GRP_CTRL + Provides global controls on the device
arch/powerpc/kvm/Makefile +1 -1
@@ -94,7 +94,7 @@ endif kvm-book3s_64-objs-$(CONFIG_KVM_XICS) += \ book3s_xics.o -kvm-book3s_64-objs-$(CONFIG_KVM_XIVE) += book3s_xive.o +kvm-book3s_64-objs-$(CONFIG_KVM_XIVE) += book3s_xive.o book3s_xive_native.o kvm-book3s_64-objs-$(CONFIG_SPAPR_TCE_IOMMU) += book3s_64_vio.o kvm-book3s_64-module-objs := \
Signature · 1 line
2.20.1
PATCH v4 3/17 KVM: PPC: Book3S HV: XIVE: introduce a new capability KVM_CAP_PPC_IRQ_XIVE
In reply to Cédric Le GoaterThe user interface exposes a new capability KVM_CAP_PPC_IRQ_XIVE to let QEMU connect the vCPU presenters to the XIVE KVM device if required. The capability is not advertised for now as the full support for the XIVE native exploitation mode is not yet available. When this is case, the capability will be advertised on PowerNV Hypervisors only. Nested guests (pseries KVM Hypervisor) are not supported.
Internally, the interface to the new KVM device is protected with a new interrupt mode: KVMPPC_IRQ_XIVE.
---
Changes since v2:
- made use of the xive_vp() macro to compute VP identifiers
- reworked locking in kvmppc_xive_native_connect_vcpu() to fix races
- stop advertising KVM_CAP_PPC_IRQ_XIVE as support is not fully available yet
arch/powerpc/include/asm/kvm_host.h | 1 + arch/powerpc/include/asm/kvm_ppc.h | 13 +++ arch/powerpc/kvm/book3s_xive.h | 11 ++ include/uapi/linux/kvm.h | 1 + arch/powerpc/kvm/book3s_xive.c | 88 ++++++++------- arch/powerpc/kvm/book3s_xive_native.c | 150 ++++++++++++++++++++++++++ arch/powerpc/kvm/powerpc.c | 36 +++++++ Documentation/virtual/kvm/api.txt | 9 ++ 8 files changed, 268 insertions(+), 41 deletions(-)
arch/powerpc/include/asm/kvm_host.h +1 -0
@@ -450,6 +450,7 @@ struct kvmppc_passthru_irqmap {#define KVMPPC_IRQ_DEFAULT 0#define KVMPPC_IRQ_MPIC 1#define KVMPPC_IRQ_XICS 2/* Includes a XIVE option */ +#define KVMPPC_IRQ_XIVE 3/* XIVE native exploitation mode */ #define MMIO_HPTE_CACHE_SIZE 4
arch/powerpc/include/asm/kvm_ppc.h +13 -0
@@ -595,6 +595,14 @@ extern int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level, bool line_status);extern void kvmppc_xive_push_vcpu (struct kvm_vcpu *vcpu); +static inline int kvmppc_xive_enabled (struct kvm_vcpu *vcpu) +{ +return vcpu->arch.irq_type ==KVMPPC_IRQ_XIVE ; +} + +extern int kvmppc_xive_native_connect_vcpu (struct kvm_device *dev, +struct kvm_vcpu *vcpu,u32 cpu); +extern void kvmppc_xive_native_cleanup_vcpu (struct kvm_vcpu *vcpu);extern void kvmppc_xive_native_init_module (void );extern void kvmppc_xive_native_exit_module (void );
@@ -622,6 +630,11 @@ static inline int kvmppc_xive_set_irq(struct kvm *kvm, int irq_source_id, u32 ir int level, bool line_status) { return -ENODEV; }static inline void kvmppc_xive_push_vcpu (struct kvm_vcpu *vcpu) { } +static inline int kvmppc_xive_enabled (struct kvm_vcpu *vcpu) + {return 0 ; } +static inline int kvmppc_xive_native_connect_vcpu (struct kvm_device *dev, +struct kvm_vcpu *vcpu,u32 cpu) {return -EBUSY ; } +static inline void kvmppc_xive_native_cleanup_vcpu (struct kvm_vcpu *vcpu) { }static inline void kvmppc_xive_native_init_module (void ) { }static inline void kvmppc_xive_native_exit_module (void ) { }
arch/powerpc/kvm/book3s_xive.h +11 -0
@@ -198,6 +198,11 @@ static inline struct kvmppc_xive_src_block *kvmppc_xive_find_source(struct kvmppreturn xive->src_blocks[bid]; } +static inline u32 kvmppc_xive_vp (struct kvmppc_xive *xive,u32 server) +{ +return xive->vp_base +kvmppc_pack_vcpu_id (xive->kvm, server); +} +/* * Mapping between guest priorities and host priorities * is as follow.
@@ -248,5 +253,11 @@ extern int (*__xive_vm_h_ipi)(struct kvm_vcpu *vcpu, unsigned long server,extern int (*__xive_vm_h_cppr)(struct kvm_vcpu *vcpu,unsigned long cppr);extern int (*__xive_vm_h_eoi)(struct kvm_vcpu *vcpu,unsigned long xirr); +/* +* Common Xive routines for XICS-over-XIVE and XIVE native +*/ +void kvmppc_xive_disable_vcpu_interrupts (struct kvm_vcpu *vcpu); +int kvmppc_xive_debug_show_queues (struct seq_file *m,struct kvm_vcpu *vcpu); +#endif /* CONFIG_KVM_XICS */ #endif /* _KVM_PPC_BOOK3S_XICS_H */
include/uapi/linux/kvm.h +1 -0
@@ -988,6 +988,7 @@ struct kvm_ppc_resize_hpt {#define KVM_CAP_ARM_VM_IPA_SIZE 165#define KVM_CAP_MANUAL_DIRTY_LOG_PROTECT 166#define KVM_CAP_HYPERV_CPUID 167 +#define KVM_CAP_PPC_IRQ_XIVE 168#ifdef KVM_CAP_IRQ_ROUTING
arch/powerpc/kvm/book3s_xive.c +47 -41
155 lines not on this page · raw message
arch/powerpc/kvm/book3s_xive_native.c +150 -0
166 lines not on this page · raw message
arch/powerpc/kvm/powerpc.c +36 -0
@@ -570,6 +570,15 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_PPC_GET_CPU_CHAR : r =1 ;break ; +#ifdef CONFIG_KVM_XIVE +case KVM_CAP_PPC_IRQ_XIVE : +/* +* Return false until all the XIVE infrastructure is +* in place including support for migration. +*/ + r =0 ; +break ; +#endif case KVM_CAP_PPC_ALLOC_HTAB : r = hv_enabled;
@@ -753,6 +762,9 @@ void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)else kvmppc_xics_free_icp (vcpu);break ; +case KVMPPC_IRQ_XIVE : +kvmppc_xive_native_cleanup_vcpu (vcpu); +break ; }kvmppc_core_vcpu_free (vcpu);
@@ -1941,6 +1953,30 @@ static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, break; }#endif /* CONFIG_KVM_XICS */ +#ifdef CONFIG_KVM_XIVE +case KVM_CAP_PPC_IRQ_XIVE : { +struct fd f; +struct kvm_device *dev; + + r = -EBADF ; + f =fdget (cap->args[0 ]); +if (!f.file) +break ; + + r = -ENXIO ; +if (!xive_enabled ()) +break ; + + r = -EPERM ; + dev =kvm_device_from_filp (f.file); +if (dev) + r =kvmppc_xive_native_connect_vcpu (dev, vcpu, + cap->args[1 ]); + +fdput (f); +break ; + } +#endif /* CONFIG_KVM_XIVE */ #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE case KVM_CAP_PPC_FWNMI : r = -EINVAL ;
Documentation/virtual/kvm/api.txt +9 -0
@@ -4475,6 +4475,15 @@ struct kvm_sync_regs { struct kvm_vcpu_events events; }; +6.75 KVM_CAP_PPC_IRQ_XIVE + +Architectures: ppc +Target: vcpu +Parameters: args[0] is the XIVE device fd + args[1] is the XIVE CPU number (server ID) for this vcpu + +This capability connects the vcpu to an in-kernel XIVE device. + 7. Capabilities that can be enabled on VMs ------------------------------------------
Signature · 1 line
2.20.1
PATCH v4 4/17 KVM: PPC: Book3S HV: XIVE: add a control to initialize a source
In reply to Cédric Le GoaterThe XIVE KVM device maintains a list of interrupt sources for the VM which are allocated in the pool of generic interrupts (IPIs) of the main XIVE IC controller. These are used for the CPU IPIs as well as for virtual device interrupts. The IRQ number space is defined by QEMU.
The XIVE device reuses the source structures of the XICS-on-XIVE device for the source blocks (2-level tree) and for the source interrupts. Under XIVE native, the source interrupt caches mostly configuration information and is less used than under the XICS-on-XIVE device in which hcalls are still necessary at run-time.
When a source is initialized in KVM, an IPI interrupt source is simply allocated at the OPAL level and then MASKED. KVM only needs to know about its type: LSI or MSI.
---
Changes since v2:
- extra documentation in commit log
- fixed comments on XIVE IRQ number space
- removed usage of the __x_* macros
- fixed locking on source block
arch/powerpc/include/uapi/asm/kvm.h | 5 + arch/powerpc/kvm/book3s_xive.h | 10 ++ arch/powerpc/kvm/book3s_xive.c | 8 +- arch/powerpc/kvm/book3s_xive_native.c | 106 +++++++++++++++++++++ Documentation/virtual/kvm/devices/xive.txt | 15 +++ 5 files changed, 140 insertions(+), 4 deletions(-)
arch/powerpc/include/uapi/asm/kvm.h +5 -0
@@ -679,5 +679,10 @@ struct kvm_ppc_cpu_char {/* POWER9 XIVE Native Interrupt Controller */ #define KVM_DEV_XIVE_GRP_CTRL 1 +#define KVM_DEV_XIVE_GRP_SOURCE 2/* 64-bit source identifier */ + +/* Layout of 64-bit XIVE source attribute values */ +#define KVM_XIVE_LEVEL_SENSITIVE (1ULL << 0) +#define KVM_XIVE_LEVEL_ASSERTED (1ULL << 1)#endif /* __LINUX_KVM_POWERPC_H */
arch/powerpc/kvm/book3s_xive.h +10 -0
@@ -12,6 +12,13 @@#ifdef CONFIG_KVM_XICS #include "book3s_xics.h" +/* +* The XIVE Interrupt source numbers are within the range 0 to +* KVMPPC_XICS_NR_IRQS. +*/ +#define KVMPPC_XIVE_FIRST_IRQ 0 +#define KVMPPC_XIVE_NR_IRQS KVMPPC_XICS_NR_IRQS +/* * State for one guest irq source. *
@@ -258,6 +265,9 @@ extern int (*__xive_vm_h_eoi)(struct kvm_vcpu *vcpu, unsigned long xirr);*/ void kvmppc_xive_disable_vcpu_interrupts (struct kvm_vcpu *vcpu);int kvmppc_xive_debug_show_queues (struct seq_file *m,struct kvm_vcpu *vcpu); +struct kvmppc_xive_src_block *kvmppc_xive_create_src_block ( +struct kvmppc_xive *xive,int irq); +void kvmppc_xive_free_sources (struct kvmppc_xive_src_block *sb);#endif /* CONFIG_KVM_XICS */ #endif /* _KVM_PPC_BOOK3S_XICS_H */
arch/powerpc/kvm/book3s_xive.c +4 -4
@@ -1480,8 +1480,8 @@ static int xive_get_source(struct kvmppc_xive *xive, long irq, u64 addr)return 0 ; } -static struct kvmppc_xive_src_block *xive_create_src_block (struct kvmppc_xive *xive, -int irq) +struct kvmppc_xive_src_block *kvmppc_xive_create_src_block ( +struct kvmppc_xive *xive,int irq) {struct kvm *kvm = xive->kvm;struct kvmppc_xive_src_block *sb;
@@ -1560,7 +1560,7 @@ static int xive_set_source(struct kvmppc_xive *xive, long irq, u64 addr) sb =kvmppc_xive_find_source (xive, irq, &idx);if (!sb) {pr_devel ("No source, creating source block...\n" ); - sb =xive_create_src_block (xive, irq); + sb =kvmppc_xive_create_src_block (xive, irq);if (!sb) {pr_devel ("Failed to create block...\n" );return -ENOMEM ;
@@ -1784,7 +1784,7 @@ static void kvmppc_xive_cleanup_irq(u32 hw_num, struct xive_irq_data *xd)xive_cleanup_irq_data (xd); } -static void kvmppc_xive_free_sources (struct kvmppc_xive_src_block *sb) +void kvmppc_xive_free_sources (struct kvmppc_xive_src_block *sb) {int i;
arch/powerpc/kvm/book3s_xive_native.c +106 -0
142 lines not on this page · raw message
Documentation/virtual/kvm/devices/xive.txt +15 -0
@@ -17,3 +17,18 @@ the legacy interrupt mode, referred as XICS (POWER7/8). 1. KVM_DEV_XIVE_GRP_CTRL Provides global controls on the device + + 2. KVM_DEV_XIVE_GRP_SOURCE (write only) + Initializes a new source in the XIVE device and mask it. + Attributes: + Interrupt source number (64-bit) + The kvm_device_attr.addr points to a __u64 value: + bits: | 63 .... 2 | 1 | 0 + values: | unused | level | type + - type: 0:MSI 1:LSI + - level: assertion level in case of an LSI. + Errors: + -E2BIG: Interrupt source number is out of range + -ENOMEM: Could not create a new source block + -EFAULT: Invalid user pointer for attr->addr. + -ENXIO: Could not allocate underlying HW interrupt
Signature · 1 line
2.20.1
PATCH v4 5/17 KVM: PPC: Book3S HV: XIVE: add a control to configure a source
In reply to Cédric Le GoaterThis control will be used by the H_INT_SET_SOURCE_CONFIG hcall from QEMU to configure the target of a source and also to restore the configuration of a source when migrating the VM.
The XIVE source interrupt structure is extended with the value of the Effective Interrupt Source Number. The EISN is the interrupt number pushed in the event queue that the guest OS will use to dispatch events internally. Caching the EISN value in KVM eases the test when checking if a reconfiguration is indeed needed.
---
Changes since v2:
- fixed comments on the KVM device attribute definitions
- handled MASKED EAS configuration
- fixed locking on source block
arch/powerpc/include/uapi/asm/kvm.h | 11 +++ arch/powerpc/kvm/book3s_xive.h | 4 + arch/powerpc/kvm/book3s_xive.c | 5 +- arch/powerpc/kvm/book3s_xive_native.c | 97 ++++++++++++++++++++++ Documentation/virtual/kvm/devices/xive.txt | 21 +++++ 5 files changed, 136 insertions(+), 2 deletions(-)
arch/powerpc/include/uapi/asm/kvm.h +11 -0
@@ -680,9 +680,20 @@ struct kvm_ppc_cpu_char {/* POWER9 XIVE Native Interrupt Controller */ #define KVM_DEV_XIVE_GRP_CTRL 1#define KVM_DEV_XIVE_GRP_SOURCE 2/* 64-bit source identifier */ +#define KVM_DEV_XIVE_GRP_SOURCE_CONFIG 3/* 64-bit source identifier */ /* Layout of 64-bit XIVE source attribute values */ #define KVM_XIVE_LEVEL_SENSITIVE (1ULL << 0)#define KVM_XIVE_LEVEL_ASSERTED (1ULL << 1) +/* Layout of 64-bit XIVE source configuration attribute values */ +#define KVM_XIVE_SOURCE_PRIORITY_SHIFT 0 +#define KVM_XIVE_SOURCE_PRIORITY_MASK 0x7 +#define KVM_XIVE_SOURCE_SERVER_SHIFT 3 +#define KVM_XIVE_SOURCE_SERVER_MASK 0xfffffff8ULL +#define KVM_XIVE_SOURCE_MASKED_SHIFT 32 +#define KVM_XIVE_SOURCE_MASKED_MASK 0x100000000ULL +#define KVM_XIVE_SOURCE_EISN_SHIFT 33 +#define KVM_XIVE_SOURCE_EISN_MASK 0xfffffffe00000000ULL +#endif /* __LINUX_KVM_POWERPC_H */
arch/powerpc/kvm/book3s_xive.h +4 -0
@@ -61,6 +61,9 @@ struct kvmppc_xive_irq_state {bool saved_p;bool saved_q;u8 saved_scan_prio; + +/* Xive native */ +u32 eisn;/* Guest Effective IRQ number */ };/* Select the "right" interrupt (IPI vs. passthrough) */
@@ -268,6 +271,7 @@ int kvmppc_xive_debug_show_queues(struct seq_file *m, struct kvm_vcpu *vcpu);struct kvmppc_xive_src_block *kvmppc_xive_create_src_block (struct kvmppc_xive *xive,int irq);void kvmppc_xive_free_sources (struct kvmppc_xive_src_block *sb); +int kvmppc_xive_select_target (struct kvm *kvm,u32 *server,u8 prio);#endif /* CONFIG_KVM_XICS */ #endif /* _KVM_PPC_BOOK3S_XICS_H */
arch/powerpc/kvm/book3s_xive.c +3 -2
@@ -342,7 +342,7 @@ static int xive_try_pick_queue(struct kvm_vcpu *vcpu, u8 prio)return atomic_add_unless (&q->count,1 , max) ?0 : -EBUSY ; } -static int xive_select_target (struct kvm *kvm,u32 *server,u8 prio) +int kvmppc_xive_select_target (struct kvm *kvm,u32 *server,u8 prio) {struct kvm_vcpu *vcpu;int i, rc;
@@ -530,7 +530,7 @@ static int xive_target_interrupt(struct kvm *kvm,* priority. The count for that new target will have * already been incremented. */ - rc =xive_select_target (kvm, &server, prio); + rc =kvmppc_xive_select_target (kvm, &server, prio);/* * We failed to find a target ? Not much we can do
@@ -1504,6 +1504,7 @@ struct kvmppc_xive_src_block *kvmppc_xive_create_src_block(for (i =0 ; i <KVMPPC_XICS_IRQ_PER_ICS ; i++) { sb->irq_state[i].number = (bid <<KVMPPC_XICS_ICS_SHIFT ) | i; + sb->irq_state[i].eisn =0 ; sb->irq_state[i].guest_priority =MASKED ; sb->irq_state[i].saved_priority =MASKED ; sb->irq_state[i].act_priority =MASKED ;
arch/powerpc/kvm/book3s_xive_native.c +97 -0
115 lines not on this page · raw message
Documentation/virtual/kvm/devices/xive.txt +21 -0
@@ -32,3 +32,24 @@ the legacy interrupt mode, referred as XICS (POWER7/8). -ENOMEM: Could not create a new source block -EFAULT: Invalid user pointer for attr->addr. -ENXIO: Could not allocate underlying HW interrupt + + 3. KVM_DEV_XIVE_GRP_SOURCE_CONFIG (write only) + Configures source targeting + Attributes: + Interrupt source number (64-bit) + The kvm_device_attr.addr points to a __u64 value: + bits: | 63 .... 33 | 32 | 31 .. 3 | 2 .. 0 + values: | eisn | mask | server | priority + - priority: 0-7 interrupt priority level + - server: CPU number chosen to handle the interrupt + - mask: mask flag (unused) + - eisn: Effective Interrupt Source Number + Errors: + -ENOENT: Unknown source number + -EINVAL: Not initialized source number + -EINVAL: Invalid priority + -EINVAL: Invalid CPU number. + -EFAULT: Invalid user pointer for attr->addr. + -ENXIO: CPU event queues not configured or configuration of the + underlying HW interrupt failed + -EBUSY: No CPU available to serve interrupt
Signature · 1 line
2.20.1
PATCH v4 6/17 KVM: PPC: Book3S HV: XIVE: add controls for the EQ configuration
In reply to Cédric Le GoaterThese controls will be used by the H_INT_SET_QUEUE_CONFIG and H_INT_GET_QUEUE_CONFIG hcalls from QEMU to configure the underlying Event Queue in the XIVE IC. They will also be used to restore the configuration of the XIVE EQs and to capture the internal run-time state of the EQs. Both 'get' and 'set' rely on an OPAL call to access the EQ toggle bit and EQ index which are updated by the XIVE IC when event notifications are enqueued in the EQ.
The value of the guest physical address of the event queue is saved in the XIVE internal xive_q structure for later use. That is when migration needs to mark the EQ pages dirty to capture a consistent memory state of the VM.
To be noted that H_INT_SET_QUEUE_CONFIG does not require the extra OPAL call setting the EQ toggle bit and EQ index to configure the EQ, but restoring the EQ state will.
---
Changes since v3 :
- fix the test ont the initial setting of the EQ toggle bit : 0 -> 1
- renamed qsize to qshift
- renamed qpage to qaddr
- checked host page size
- limited flags to KVM_XIVE_EQ_ALWAYS_NOTIFY to fit sPAPR specs
Changes since v2 :
- fixed comments on the KVM device attribute definitions
- fixed check on supported EQ size to restrict to 64K pages
- checked kvm_eq.flags that need to be zero
- removed the OPAL call when EQ qtoggle bit and index are zero.
arch/powerpc/include/asm/xive.h | 2 + arch/powerpc/include/uapi/asm/kvm.h | 19 ++ arch/powerpc/kvm/book3s_xive.h | 2 + arch/powerpc/kvm/book3s_xive.c | 15 +- arch/powerpc/kvm/book3s_xive_native.c | 242 +++++++++++++++++++++ Documentation/virtual/kvm/devices/xive.txt | 34 +++ 6 files changed, 308 insertions(+), 6 deletions(-)
arch/powerpc/include/asm/xive.h +2 -0
@@ -73,6 +73,8 @@ struct xive_q {u32 esc_irq;atomic_t count;atomic_t pending_count; +u64 guest_qaddr; +u32 guest_qshift; };/* Global enable flags for the XIVE support */
arch/powerpc/include/uapi/asm/kvm.h +19 -0
@@ -681,6 +681,7 @@ struct kvm_ppc_cpu_char {#define KVM_DEV_XIVE_GRP_CTRL 1#define KVM_DEV_XIVE_GRP_SOURCE 2/* 64-bit source identifier */ #define KVM_DEV_XIVE_GRP_SOURCE_CONFIG 3/* 64-bit source identifier */ +#define KVM_DEV_XIVE_GRP_EQ_CONFIG 4/* 64-bit EQ identifier */ /* Layout of 64-bit XIVE source attribute values */ #define KVM_XIVE_LEVEL_SENSITIVE (1ULL << 0)
@@ -696,4 +697,22 @@ struct kvm_ppc_cpu_char {#define KVM_XIVE_SOURCE_EISN_SHIFT 33#define KVM_XIVE_SOURCE_EISN_MASK 0xfffffffe00000000ULL +/* Layout of 64-bit EQ identifier */ +#define KVM_XIVE_EQ_PRIORITY_SHIFT 0 +#define KVM_XIVE_EQ_PRIORITY_MASK 0x7 +#define KVM_XIVE_EQ_SERVER_SHIFT 3 +#define KVM_XIVE_EQ_SERVER_MASK 0xfffffff8ULL + +/* Layout of EQ configuration values (64 bytes) */ +struct kvm_ppc_xive_eq { +__u32 flags; +__u32 qshift; +__u64 qaddr; +__u32 qtoggle; +__u32 qindex; +__u8 pad[40 ]; +}; + +#define KVM_XIVE_EQ_ALWAYS_NOTIFY 0x00000001 +#endif /* __LINUX_KVM_POWERPC_H */
arch/powerpc/kvm/book3s_xive.h +2 -0
@@ -272,6 +272,8 @@ struct kvmppc_xive_src_block *kvmppc_xive_create_src_block( struct kvmppc_xive *xive, int irq);void kvmppc_xive_free_sources (struct kvmppc_xive_src_block *sb);int kvmppc_xive_select_target (struct kvm *kvm,u32 *server,u8 prio); +int kvmppc_xive_attach_escalation (struct kvm_vcpu *vcpu,u8 prio, +bool single_escalation);#endif /* CONFIG_KVM_XICS */ #endif /* _KVM_PPC_BOOK3S_XICS_H */
arch/powerpc/kvm/book3s_xive.c +9 -6
@@ -166,7 +166,8 @@ static irqreturn_t xive_esc_irq(int irq, void *data)return IRQ_HANDLED ; } -static int xive_attach_escalation (struct kvm_vcpu *vcpu,u8 prio) +int kvmppc_xive_attach_escalation (struct kvm_vcpu *vcpu,u8 prio, +bool single_escalation) {struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;struct xive_q *q = &xc->queues[prio];
@@ -185,7 +186,7 @@ static int xive_attach_escalation(struct kvm_vcpu *vcpu, u8 prio)return -EIO ; } -if (xc->xive->single_escalation) +if (single_escalation) name =kasprintf (GFP_KERNEL ,"kvm-%d-%d" , vcpu->kvm->arch.lpid, xc->server_num);else
@@ -217,7 +218,7 @@ static int xive_attach_escalation(struct kvm_vcpu *vcpu, u8 prio)* interrupt, thus leaving it effectively masked after * it fires once. */ -if (xc->xive->single_escalation) { +if (single_escalation) {struct irq_data *d =irq_get_irq_data (xc->esc_virq[prio]);struct xive_irq_data *xd =irq_data_get_irq_handler_data (d);
@@ -291,7 +292,8 @@ static int xive_check_provisioning(struct kvm *kvm, u8 prio)continue ; rc =xive_provision_queue (vcpu, prio);if (rc ==0 && !xive->single_escalation) -xive_attach_escalation (vcpu, prio); +kvmppc_xive_attach_escalation (vcpu, prio, + xive->single_escalation);if (rc)return rc; }
@@ -1214,7 +1216,8 @@ int kvmppc_xive_connect_vcpu(struct kvm_device *dev,if (xive->qmap & (1 << i)) { r =xive_provision_queue (vcpu, i);if (r ==0 && !xive->single_escalation) -xive_attach_escalation (vcpu, i); +kvmppc_xive_attach_escalation ( + vcpu, i, xive->single_escalation);if (r) goto bail; }else {
@@ -1229,7 +1232,7 @@ int kvmppc_xive_connect_vcpu(struct kvm_device *dev, }/* If not done above, attach priority 0 escalation */ - r =xive_attach_escalation (vcpu,0 ); + r =kvmppc_xive_attach_escalation (vcpu,0 , xive->single_escalation);if (r) goto bail;
arch/powerpc/kvm/book3s_xive_native.c +242 -0
266 lines not on this page · raw message
Documentation/virtual/kvm/devices/xive.txt +34 -0
@@ -53,3 +53,37 @@ the legacy interrupt mode, referred as XICS (POWER7/8). -ENXIO: CPU event queues not configured or configuration of the underlying HW interrupt failed -EBUSY: No CPU available to serve interrupt + + 4. KVM_DEV_XIVE_GRP_EQ_CONFIG (read-write) + Configures an event queue of a CPU + Attributes: + EQ descriptor identifier (64-bit) + The EQ descriptor identifier is a tuple (server, priority) : + bits: | 63 .... 32 | 31 .. 3 | 2 .. 0 + values: | unused | server | priority + The kvm_device_attr.addr points to : + struct kvm_ppc_xive_eq { + __u32 flags; + __u32 qshift; + __u64 qaddr; + __u32 qtoggle; + __u32 qindex; + __u8 pad[40]; + }; + - flags: queue flags + KVM_XIVE_EQ_ALWAYS_NOTIFY + forces notification without using the coalescing mechanism + provided by the XIVE END ESBs. + - qshift: queue size (power of 2) + - qaddr: real address of queue + - qtoggle: current queue toggle bit + - qindex: current queue index + - pad: reserved for future use + Errors: + -ENOENT: Invalid CPU number + -EINVAL: Invalid priority + -EINVAL: Invalid flags + -EINVAL: Invalid queue size + -EINVAL: Invalid queue address + -EFAULT: Invalid user pointer for attr->addr. + -EIO: Configuration of the underlying HW failed
Signature · 1 line
2.20.1
Re: [PATCH v4 06/17] KVM: PPC: Book3S HV: XIVE: add controls for the EQ configuration
Quoted 178 lines · Cédric Le Goater
These controls will be used by the
H_INT_SET_QUEUE_CONFIGandH_INT_GET_QUEUE_CONFIGhcalls from QEMU to configure the underlying Event Queue in the XIVE IC. They will also be used to restore the configuration of the XIVE EQs and to capture the internal run-time state of the EQs. Both 'get' and 'set' rely on an OPAL call to access the EQ toggle bit and EQ index which are updated by the XIVE IC when event notifications are enqueued in the EQ.The value of the guest physical address of the event queue is saved in the XIVE internal
xive_qstructure for later use. That is when migration needs to mark the EQ pages dirty to capture a consistent memory state of the VM.To be noted that
H_INT_SET_QUEUE_CONFIGdoes not require the extra OPAL call setting the EQ toggle bit and EQ index to configure the EQ, but restoring the EQ state will.Signed-off-by Cédric Le Goater <[email protected]>---
Changes since v3 :
- fix the test ont the initial setting of the EQ toggle bit : 0 -> 1
- renamed qsize to qshift
- renamed qpage to qaddr
- checked host page size
- limited flags toKVM_XIVE_EQ_ALWAYS_NOTIFYto fit sPAPR specsChanges since v2 :
- fixed comments on the KVM device attribute definitions
- fixed check on supported EQ size to restrict to 64K pages
- checked kvm_eq.flags that need to be zero
- removed the OPAL call when EQ qtoggle bit and index are zero.arch/powerpc/include/asm/xive.h | 2 + arch/powerpc/include/uapi/asm/kvm.h | 19 ++ arch/powerpc/kvm/book3s_xive.h | 2 + arch/powerpc/kvm/book3s_xive.c | 15 +- arch/powerpc/kvm/book3s_xive_native.c | 242 +++++++++++++++++++++ Documentation/virtual/kvm/devices/xive.txt | 34 +++ 6 files changed, 308 insertions(+), 6 deletions(-)arch/powerpc/include/asm/xive.h +2
-0@@ -73,6 +73,8 @@ struct xive_q {u32 esc_irq;atomic_t count;atomic_t pending_count; +u64 guest_qaddr; +u32 guest_qshift; };/* Global enable flags for the XIVE support */ arch/powerpc/include/uapi/asm/kvm.h +19
-0@@ -681,6 +681,7 @@ struct kvm_ppc_cpu_char {#define KVM_DEV_XIVE_GRP_CTRL 1#define KVM_DEV_XIVE_GRP_SOURCE 2/* 64-bit source identifier */ #define KVM_DEV_XIVE_GRP_SOURCE_CONFIG 3/* 64-bit source identifier */ +#define KVM_DEV_XIVE_GRP_EQ_CONFIG 4/* 64-bit EQ identifier */ /* Layout of 64-bit XIVE source attribute values */ #define KVM_XIVE_LEVEL_SENSITIVE (1ULL << 0)@@ -696,4 +697,22 @@ struct kvm_ppc_cpu_char {#define KVM_XIVE_SOURCE_EISN_SHIFT 33#define KVM_XIVE_SOURCE_EISN_MASK 0xfffffffe00000000ULL +/* Layout of 64-bit EQ identifier */ +#define KVM_XIVE_EQ_PRIORITY_SHIFT 0 +#define KVM_XIVE_EQ_PRIORITY_MASK 0x7 +#define KVM_XIVE_EQ_SERVER_SHIFT 3 +#define KVM_XIVE_EQ_SERVER_MASK 0xfffffff8ULL + +/* Layout of EQ configuration values (64 bytes) */ +struct kvm_ppc_xive_eq { +__u32 flags; +__u32 qshift; +__u64 qaddr; +__u32 qtoggle; +__u32 qindex; +__u8 pad[40 ]; +}; + +#define KVM_XIVE_EQ_ALWAYS_NOTIFY 0x00000001 +#endif /* __LINUX_KVM_POWERPC_H */ arch/powerpc/kvm/book3s_xive.h +2
-0@@ -272,6 +272,8 @@ struct kvmppc_xive_src_block *kvmppc_xive_create_src_block( struct kvmppc_xive *xive, int irq);void kvmppc_xive_free_sources (struct kvmppc_xive_src_block *sb);int kvmppc_xive_select_target (struct kvm *kvm,u32 *server,u8 prio); +int kvmppc_xive_attach_escalation (struct kvm_vcpu *vcpu,u8 prio, +bool single_escalation);#endif /* CONFIG_KVM_XICS */ #endif /* _KVM_PPC_BOOK3S_XICS_H */ arch/powerpc/kvm/book3s_xive.c +9
-6@@ -166,7 +166,8 @@ static irqreturn_t xive_esc_irq(int irq, void *data)return IRQ_HANDLED ; } -static int xive_attach_escalation (struct kvm_vcpu *vcpu,u8 prio) +int kvmppc_xive_attach_escalation (struct kvm_vcpu *vcpu,u8 prio, +bool single_escalation) {struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;struct xive_q *q = &xc->queues[prio];@@ -185,7 +186,7 @@ static int xive_attach_escalation(struct kvm_vcpu *vcpu, u8 prio)return -EIO ; } -if (xc->xive->single_escalation) +if (single_escalation) name =kasprintf (GFP_KERNEL ,"kvm-%d-%d" , vcpu->kvm->arch.lpid, xc->server_num);else @@ -217,7 +218,7 @@ static int xive_attach_escalation(struct kvm_vcpu *vcpu, u8 prio)* interrupt, thus leaving it effectively masked after * it fires once. */ -if (xc->xive->single_escalation) { +if (single_escalation) {struct irq_data *d =irq_get_irq_data (xc->esc_virq[prio]);struct xive_irq_data *xd =irq_data_get_irq_handler_data (d);@@ -291,7 +292,8 @@ static int xive_check_provisioning(struct kvm *kvm, u8 prio)continue ; rc =xive_provision_queue (vcpu, prio);if (rc ==0 && !xive->single_escalation) -xive_attach_escalation (vcpu, prio); +kvmppc_xive_attach_escalation (vcpu, prio, + xive->single_escalation);if (rc)return rc; }@@ -1214,7 +1216,8 @@ int kvmppc_xive_connect_vcpu(struct kvm_device *dev,if (xive->qmap & (1 << i)) { r =xive_provision_queue (vcpu, i);if (r ==0 && !xive->single_escalation) -xive_attach_escalation (vcpu, i); +kvmppc_xive_attach_escalation ( + vcpu, i, xive->single_escalation);if (r) goto bail; }else {@@ -1229,7 +1232,7 @@ int kvmppc_xive_connect_vcpu(struct kvm_device *dev, }/* If not done above, attach priority 0 escalation */ - r =xive_attach_escalation (vcpu,0 ); + r =kvmppc_xive_attach_escalation (vcpu,0 , xive->single_escalation);if (r) goto bail;arch/powerpc/kvm/book3s_xive_native.c +5
-0@@ -335,6 +335,236 @@ static int kvmppc_xive_native_set_source_config(struct kvmppc_xive *xive, priority, masked, eisn); } +static int xive_native_validate_queue_size (u32 qshift) +{ +/* +* We only support 64K pages for the moment. This is also +* advertised in the DT property "ibm,xive-eq-sizes"
arch/powerpc/kvm/book3s_xive_native.c static int kvmppc_xive_native_set_source_config(struct kvmppc_xive *xive, · line 352
Quoted 10 lines
arch/powerpc/kvm/book3s_xive_native.c +10
-0+*/ +switch (qshift) { +case 0 :/* EQ reset */ +case 16 : +return 0 ; +case 12 : +case 21 : +case 24 : +default : +return -EINVAL ;
Now that you're doing a proper test against the guest page size, it would be very easy to allow this to support things other than a 64kiB queue. That can be a followup change, though.
Quoted 49 lines
arch/powerpc/kvm/book3s_xive_native.c +49
-0+ } +} + +static int kvmppc_xive_native_set_queue_config (struct kvmppc_xive *xive, +long eq_idx,u64 addr) +{ +struct kvm *kvm = xive->kvm; +struct kvm_vcpu *vcpu; +struct kvmppc_xive_vcpu *xc; +void __user *ubufp = (void __user *) addr; +u32 server; +u8 priority; +struct kvm_ppc_xive_eq kvm_eq; +int rc; +__be32 *qaddr =0 ; +struct page *page; +struct xive_q *q; +gfn_t gfn; +unsigned long page_size; + +/* +* Demangle priority/server tuple from the EQ identifier +*/ + priority = (eq_idx &KVM_XIVE_EQ_PRIORITY_MASK ) >> +KVM_XIVE_EQ_PRIORITY_SHIFT ; + server = (eq_idx &KVM_XIVE_EQ_SERVER_MASK ) >> +KVM_XIVE_EQ_SERVER_SHIFT ; + +if (copy_from_user (&kvm_eq, ubufp,sizeof (kvm_eq))) +return -EFAULT ; + + vcpu =kvmppc_xive_find_server (kvm, server); +if (!vcpu) { +pr_err ("Can't find server %d\n" , server); +return -ENOENT ; + } + xc = vcpu->arch.xive_vcpu; + +if (priority !=xive_prio_from_guest (priority)) { +pr_err ("Trying to restore invalid queue %d for VCPU %d\n" , + priority, server); +return -EINVAL ; + } + q = &xc->queues[priority]; + +pr_devel ("%s VCPU %d priority %d fl:%x shift:%d addr:%llx g:%d idx:%d\n" , + __func__, server, priority, kvm_eq.flags, + kvm_eq.qshift, kvm_eq.qaddr, kvm_eq.qtoggle, kvm_eq.qindex); +
arch/powerpc/kvm/book3s_xive_native.c static int kvmppc_xive_native_set_source_config(struct kvmppc_xive *xive, · line 411
Quoted 10 lines
arch/powerpc/kvm/book3s_xive_native.c +10
-0+/* +* sPAPR specifies a "Unconditional Notify (n) flag" for the +* H_INT_SET_QUEUE_CONFIG hcall which forces notification +* without using the coalescing mechanisms provided by the +* XIVE END ESBs. +*/ +if (kvm_eq.flags & ~KVM_XIVE_EQ_ALWAYS_NOTIFY ) { +pr_err ("invalid flags %d\n" , kvm_eq.flags); +return -EINVAL ; + }
So this test prevents setting any (as yet undefined) flags other than EQ_ALWAYS_NOTIFY, which is good. However, AFAICT you never actually branch based on whether the EQ_ALWAYS_NOTIFY flag is set - you just assume that it is. You should either actually pass this flag through to the OPAL call, or have another test here which rejects the ioctl() if the flag is *not* set.
Quoted 30 lines
arch/powerpc/kvm/book3s_xive_native.c +30
-0+ + rc =xive_native_validate_queue_size (kvm_eq.qshift); +if (rc) { +pr_err ("invalid queue size %d\n" , kvm_eq.qshift); +return rc; + } + +/* reset queue and disable queueing */ +if (!kvm_eq.qshift) { + q->guest_qaddr =0 ; + q->guest_qshift =0 ; + + rc =xive_native_configure_queue (xc->vp_id, q, priority, +NULL ,0 , true); +if (rc) { +pr_err ("Failed to reset queue %d for VCPU %d: %d\n" , + priority, xc->server_num, rc); +return rc; + } + +if (q->qpage) { +put_page (virt_to_page (q->qpage)); + q->qpage =NULL ; + } + +return 0 ; + } + + gfn =gpa_to_gfn (kvm_eq.qaddr); + page =gfn_to_page (kvm, gfn);
arch/powerpc/kvm/book3s_xive_native.c static int kvmppc_xive_native_set_source_config(struct kvmppc_xive *xive, · line 451
Quoted 10 lines
arch/powerpc/kvm/book3s_xive_native.c +10
-0+if (is_error_page (page)) { +pr_warn ("Couldn't get guest page for %llx!\n" , kvm_eq.qaddr); +return -EINVAL ; + } + + page_size =kvm_host_page_size (kvm, gfn); +if (1ull << kvm_eq.qshift > page_size) { +pr_warn ("Incompatible host page size %lx!\n" , page_size); +return -EINVAL ; + }
Sorry.. I should have thought of this earlier. If qaddr isn't aligned to a (page_size) boundary, this test isn't sufficient - you need to make sure the whole thing fits within the host page, even if it's offset.
Alternatively, you could check that qaddr is naturally aligned for the requested qshift. Possibly that's necessary for the hardware anyway.
Quoted 194 lines
arch/powerpc/kvm/book3s_xive_native.c +128
-0+ + qaddr =page_to_virt (page) + (kvm_eq.qaddr & ~PAGE_MASK ); + +/* +* Backup the queue page guest address to the mark EQ page +* dirty for migration. +*/ + q->guest_qaddr = kvm_eq.qaddr; + q->guest_qshift = kvm_eq.qshift; + +/* +* Unconditional Notification is forced by default at the +* OPAL level because the use of END ESBs is not supported by +* Linux. +*/ + rc =xive_native_configure_queue (xc->vp_id, q, priority, + (__be32 *) qaddr, kvm_eq.qshift, true); +if (rc) { +pr_err ("Failed to configure queue %d for VCPU %d: %d\n" , + priority, xc->server_num, rc); +put_page (page); +return rc; + } + +/* +* Only restore the queue state when needed. When doing the +* H_INT_SET_SOURCE_CONFIG hcall, it should not. +*/ +if (kvm_eq.qtoggle !=1 || kvm_eq.qindex !=0 ) { + rc =xive_native_set_queue_state (xc->vp_id, priority, + kvm_eq.qtoggle, + kvm_eq.qindex); +if (rc) + goto error; + } + + rc =kvmppc_xive_attach_escalation (vcpu, priority, + xive->single_escalation); +error: +if (rc) +kvmppc_xive_native_cleanup_queue (vcpu, priority); +return rc; +} + +static int kvmppc_xive_native_get_queue_config (struct kvmppc_xive *xive, +long eq_idx,u64 addr) +{ +struct kvm *kvm = xive->kvm; +struct kvm_vcpu *vcpu; +struct kvmppc_xive_vcpu *xc; +struct xive_q *q; +void __user *ubufp = (u64 __user *) addr; +u32 server; +u8 priority; +struct kvm_ppc_xive_eq kvm_eq; +u64 qaddr; +u64 qshift; +u64 qeoi_page; +u32 escalate_irq; +u64 qflags; +int rc; + +/* +* Demangle priority/server tuple from the EQ identifier +*/ + priority = (eq_idx &KVM_XIVE_EQ_PRIORITY_MASK ) >> +KVM_XIVE_EQ_PRIORITY_SHIFT ; + server = (eq_idx &KVM_XIVE_EQ_SERVER_MASK ) >> +KVM_XIVE_EQ_SERVER_SHIFT ; + + vcpu =kvmppc_xive_find_server (kvm, server); +if (!vcpu) { +pr_err ("Can't find server %d\n" , server); +return -ENOENT ; + } + xc = vcpu->arch.xive_vcpu; + +if (priority !=xive_prio_from_guest (priority)) { +pr_err ("invalid priority for queue %d for VCPU %d\n" , + priority, server); +return -EINVAL ; + } + q = &xc->queues[priority]; + +memset (&kvm_eq,0 ,sizeof (kvm_eq)); + +if (!q->qpage) +return 0 ; + + rc =xive_native_get_queue_info (xc->vp_id, priority, &qaddr, &qshift, + &qeoi_page, &escalate_irq, &qflags); +if (rc) +return rc; + + kvm_eq.flags =0 ; +if (qflags &OPAL_XIVE_EQ_ALWAYS_NOTIFY ) + kvm_eq.flags |=KVM_XIVE_EQ_ALWAYS_NOTIFY ; + + kvm_eq.qshift = q->guest_qshift; + kvm_eq.qaddr = q->guest_qaddr; + + rc =xive_native_get_queue_state (xc->vp_id, priority, &kvm_eq.qtoggle, + &kvm_eq.qindex); +if (rc) +return rc; + +pr_devel ("%s VCPU %d priority %d fl:%x shift:%d addr:%llx g:%d idx:%d\n" , + __func__, server, priority, kvm_eq.flags, + kvm_eq.qshift, kvm_eq.qaddr, kvm_eq.qtoggle, kvm_eq.qindex); + +if (copy_to_user (ubufp, &kvm_eq,sizeof (kvm_eq))) +return -EFAULT ; + +return 0 ; +} +static int kvmppc_xive_native_set_attr (struct kvm_device *dev,struct kvm_device_attr *attr) {@@ -349,6 +579,9 @@ static int kvmppc_xive_native_set_attr(struct kvm_device *dev,case KVM_DEV_XIVE_GRP_SOURCE_CONFIG :return kvmppc_xive_native_set_source_config (xive, attr->attr, attr->addr); +case KVM_DEV_XIVE_GRP_EQ_CONFIG : +return kvmppc_xive_native_set_queue_config (xive, attr->attr, + attr->addr); }return -ENXIO ; }@@ -356,6 +589,13 @@ static int kvmppc_xive_native_set_attr(struct kvm_device *dev,static int kvmppc_xive_native_get_attr (struct kvm_device *dev,struct kvm_device_attr *attr) { +struct kvmppc_xive *xive = dev->private; + +switch (attr->group) { +case KVM_DEV_XIVE_GRP_EQ_CONFIG : +return kvmppc_xive_native_get_queue_config (xive, attr->attr, + attr->addr); + }return -ENXIO ; }@@ -371,6 +611,8 @@ static int kvmppc_xive_native_has_attr(struct kvm_device *dev, attr->attr < KVMPPC_XIVE_NR_IRQS)return 0 ;break ; +case KVM_DEV_XIVE_GRP_EQ_CONFIG : +return 0 ; }return -ENXIO ; }Documentation/virtual/kvm/devices/xive.txt +34
-0@@ -53,3 +53,37 @@ the legacy interrupt mode, referred as XICS (POWER7/8). -ENXIO: CPU event queues not configured or configuration of the underlying HW interrupt failed -EBUSY: No CPU available to serve interrupt + + 4. KVM_DEV_XIVE_GRP_EQ_CONFIG (read-write) + Configures an event queue of a CPU + Attributes: + EQ descriptor identifier (64-bit) + The EQ descriptor identifier is a tuple (server, priority) : + bits: | 63 .... 32 | 31 .. 3 | 2 .. 0 + values: | unused | server | priority + The kvm_device_attr.addr points to : + struct kvm_ppc_xive_eq { + __u32 flags; + __u32 qshift; + __u64 qaddr; + __u32 qtoggle; + __u32 qindex; + __u8 pad[40]; + }; + - flags: queue flags + KVM_XIVE_EQ_ALWAYS_NOTIFY + forces notification without using the coalescing mechanism + provided by the XIVE END ESBs. + - qshift: queue size (power of 2) + - qaddr: real address of queue + - qtoggle: current queue toggle bit + - qindex: current queue index + - pad: reserved for future use + Errors: + -ENOENT: Invalid CPU number + -EINVAL: Invalid priority + -EINVAL: Invalid flags + -EINVAL: Invalid queue size + -EINVAL: Invalid queue address + -EFAULT: Invalid user pointer for attr->addr. + -EIO: Configuration of the underlying HW failed
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [PATCH v4 06/17] KVM: PPC: Book3S HV: XIVE: add controls for the EQ configuration
Quoted 185 lines · David Gibson
Quoted 184 lines · Cédric Le Goater
These controls will be used by the
H_INT_SET_QUEUE_CONFIGandH_INT_GET_QUEUE_CONFIGhcalls from QEMU to configure the underlying Event Queue in the XIVE IC. They will also be used to restore the configuration of the XIVE EQs and to capture the internal run-time state of the EQs. Both 'get' and 'set' rely on an OPAL call to access the EQ toggle bit and EQ index which are updated by the XIVE IC when event notifications are enqueued in the EQ.The value of the guest physical address of the event queue is saved in the XIVE internal
xive_qstructure for later use. That is when migration needs to mark the EQ pages dirty to capture a consistent memory state of the VM.To be noted that
H_INT_SET_QUEUE_CONFIGdoes not require the extra OPAL call setting the EQ toggle bit and EQ index to configure the EQ, but restoring the EQ state will.Signed-off-by Cédric Le Goater <[email protected]>---
Changes since v3 :
- fix the test ont the initial setting of the EQ toggle bit : 0 -> 1
- renamed qsize to qshift
- renamed qpage to qaddr
- checked host page size
- limited flags toKVM_XIVE_EQ_ALWAYS_NOTIFYto fit sPAPR specsChanges since v2 :
- fixed comments on the KVM device attribute definitions
- fixed check on supported EQ size to restrict to 64K pages
- checked kvm_eq.flags that need to be zero
- removed the OPAL call when EQ qtoggle bit and index are zero.arch/powerpc/include/asm/xive.h | 2 + arch/powerpc/include/uapi/asm/kvm.h | 19 ++ arch/powerpc/kvm/book3s_xive.h | 2 + arch/powerpc/kvm/book3s_xive.c | 15 +- arch/powerpc/kvm/book3s_xive_native.c | 242 +++++++++++++++++++++ Documentation/virtual/kvm/devices/xive.txt | 34 +++ 6 files changed, 308 insertions(+), 6 deletions(-)arch/powerpc/include/asm/xive.h +2
-0@@ -73,6 +73,8 @@ struct xive_q {u32 esc_irq;atomic_t count;atomic_t pending_count; +u64 guest_qaddr; +u32 guest_qshift; };/* Global enable flags for the XIVE support */ arch/powerpc/include/uapi/asm/kvm.h +19
-0@@ -681,6 +681,7 @@ struct kvm_ppc_cpu_char {#define KVM_DEV_XIVE_GRP_CTRL 1#define KVM_DEV_XIVE_GRP_SOURCE 2/* 64-bit source identifier */ #define KVM_DEV_XIVE_GRP_SOURCE_CONFIG 3/* 64-bit source identifier */ +#define KVM_DEV_XIVE_GRP_EQ_CONFIG 4/* 64-bit EQ identifier */ /* Layout of 64-bit XIVE source attribute values */ #define KVM_XIVE_LEVEL_SENSITIVE (1ULL << 0)@@ -696,4 +697,22 @@ struct kvm_ppc_cpu_char {#define KVM_XIVE_SOURCE_EISN_SHIFT 33#define KVM_XIVE_SOURCE_EISN_MASK 0xfffffffe00000000ULL +/* Layout of 64-bit EQ identifier */ +#define KVM_XIVE_EQ_PRIORITY_SHIFT 0 +#define KVM_XIVE_EQ_PRIORITY_MASK 0x7 +#define KVM_XIVE_EQ_SERVER_SHIFT 3 +#define KVM_XIVE_EQ_SERVER_MASK 0xfffffff8ULL + +/* Layout of EQ configuration values (64 bytes) */ +struct kvm_ppc_xive_eq { +__u32 flags; +__u32 qshift; +__u64 qaddr; +__u32 qtoggle; +__u32 qindex; +__u8 pad[40 ]; +}; + +#define KVM_XIVE_EQ_ALWAYS_NOTIFY 0x00000001 +#endif /* __LINUX_KVM_POWERPC_H */ arch/powerpc/kvm/book3s_xive.h +2
-0@@ -272,6 +272,8 @@ struct kvmppc_xive_src_block *kvmppc_xive_create_src_block( struct kvmppc_xive *xive, int irq);void kvmppc_xive_free_sources (struct kvmppc_xive_src_block *sb);int kvmppc_xive_select_target (struct kvm *kvm,u32 *server,u8 prio); +int kvmppc_xive_attach_escalation (struct kvm_vcpu *vcpu,u8 prio, +bool single_escalation);#endif /* CONFIG_KVM_XICS */ #endif /* _KVM_PPC_BOOK3S_XICS_H */ arch/powerpc/kvm/book3s_xive.c +9
-6@@ -166,7 +166,8 @@ static irqreturn_t xive_esc_irq(int irq, void *data)return IRQ_HANDLED ; } -static int xive_attach_escalation (struct kvm_vcpu *vcpu,u8 prio) +int kvmppc_xive_attach_escalation (struct kvm_vcpu *vcpu,u8 prio, +bool single_escalation) {struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu;struct xive_q *q = &xc->queues[prio];@@ -185,7 +186,7 @@ static int xive_attach_escalation(struct kvm_vcpu *vcpu, u8 prio)return -EIO ; } -if (xc->xive->single_escalation) +if (single_escalation) name =kasprintf (GFP_KERNEL ,"kvm-%d-%d" , vcpu->kvm->arch.lpid, xc->server_num);else @@ -217,7 +218,7 @@ static int xive_attach_escalation(struct kvm_vcpu *vcpu, u8 prio)* interrupt, thus leaving it effectively masked after * it fires once. */ -if (xc->xive->single_escalation) { +if (single_escalation) {struct irq_data *d =irq_get_irq_data (xc->esc_virq[prio]);struct xive_irq_data *xd =irq_data_get_irq_handler_data (d);@@ -291,7 +292,8 @@ static int xive_check_provisioning(struct kvm *kvm, u8 prio)continue ; rc =xive_provision_queue (vcpu, prio);if (rc ==0 && !xive->single_escalation) -xive_attach_escalation (vcpu, prio); +kvmppc_xive_attach_escalation (vcpu, prio, + xive->single_escalation);if (rc)return rc; }@@ -1214,7 +1216,8 @@ int kvmppc_xive_connect_vcpu(struct kvm_device *dev,if (xive->qmap & (1 << i)) { r =xive_provision_queue (vcpu, i);if (r ==0 && !xive->single_escalation) -xive_attach_escalation (vcpu, i); +kvmppc_xive_attach_escalation ( + vcpu, i, xive->single_escalation);if (r) goto bail; }else {@@ -1229,7 +1232,7 @@ int kvmppc_xive_connect_vcpu(struct kvm_device *dev, }/* If not done above, attach priority 0 escalation */ - r =xive_attach_escalation (vcpu,0 ); + r =kvmppc_xive_attach_escalation (vcpu,0 , xive->single_escalation);if (r) goto bail;arch/powerpc/kvm/book3s_xive_native.c +11
-0@@ -335,6 +335,236 @@ static int kvmppc_xive_native_set_source_config(struct kvmppc_xive *xive, priority, masked, eisn); } +static int xive_native_validate_queue_size (u32 qshift) +{ +/* +* We only support 64K pages for the moment. This is also +* advertised in the DT property "ibm,xive-eq-sizes" +*/ +switch (qshift) { +case 0 :/* EQ reset */ +case 16 : +return 0 ; +case 12 :
Quoted 8 lines
arch/powerpc/kvm/book3s_xive_native.c static int kvmppc_xive_native_set_source_config(struct kvmppc_xive *xive, · line 352
Quoted 4 lines
arch/powerpc/kvm/book3s_xive_native.c +4
-0+case 21 : +case 24 : +default : +return -EINVAL ;Now that you're doing a proper test against the guest page size, it would be very easy to allow this to support things other than a 64kiB queue. That can be a followup change, though.
Supporting larger page sizes could be interesting for AIX or Linux. Talking of which, we should start with QEMU.
Quoted 57 lines
Quoted 57 lines
arch/powerpc/kvm/book3s_xive_native.c +56
-0+ } +} + +static int kvmppc_xive_native_set_queue_config (struct kvmppc_xive *xive, +long eq_idx,u64 addr) +{ +struct kvm *kvm = xive->kvm; +struct kvm_vcpu *vcpu; +struct kvmppc_xive_vcpu *xc; +void __user *ubufp = (void __user *) addr; +u32 server; +u8 priority; +struct kvm_ppc_xive_eq kvm_eq; +int rc; +__be32 *qaddr =0 ; +struct page *page; +struct xive_q *q; +gfn_t gfn; +unsigned long page_size; + +/* +* Demangle priority/server tuple from the EQ identifier +*/ + priority = (eq_idx &KVM_XIVE_EQ_PRIORITY_MASK ) >> +KVM_XIVE_EQ_PRIORITY_SHIFT ; + server = (eq_idx &KVM_XIVE_EQ_SERVER_MASK ) >> +KVM_XIVE_EQ_SERVER_SHIFT ; + +if (copy_from_user (&kvm_eq, ubufp,sizeof (kvm_eq))) +return -EFAULT ; + + vcpu =kvmppc_xive_find_server (kvm, server); +if (!vcpu) { +pr_err ("Can't find server %d\n" , server); +return -ENOENT ; + } + xc = vcpu->arch.xive_vcpu; + +if (priority !=xive_prio_from_guest (priority)) { +pr_err ("Trying to restore invalid queue %d for VCPU %d\n" , + priority, server); +return -EINVAL ; + } + q = &xc->queues[priority]; + +pr_devel ("%s VCPU %d priority %d fl:%x shift:%d addr:%llx g:%d idx:%d\n" , + __func__, server, priority, kvm_eq.flags, + kvm_eq.qshift, kvm_eq.qaddr, kvm_eq.qtoggle, kvm_eq.qindex); + +/* +* sPAPR specifies a "Unconditional Notify (n) flag" for the +* H_INT_SET_QUEUE_CONFIG hcall which forces notification +* without using the coalescing mechanisms provided by the +* XIVE END ESBs. +*/ +if (kvm_eq.flags & ~KVM_XIVE_EQ_ALWAYS_NOTIFY ) {
Quoted 8 lines
arch/powerpc/kvm/book3s_xive_native.c static int kvmppc_xive_native_set_source_config(struct kvmppc_xive *xive, · line 411
Quoted 3 lines
arch/powerpc/kvm/book3s_xive_native.c +3
-0+pr_err ("invalid flags %d\n" , kvm_eq.flags); +return -EINVAL ; + }So this test prevents setting any (as yet undefined) flags other than
EQ_ALWAYS_NOTIFY, which is good. However, AFAICT you never actually branch based on whether theEQ_ALWAYS_NOTIFYflag is set - you just assume that it is.
Yes because it is enforced by xive_native_configure_queue().
Quoted 3 lines
You should either actually pass this flag through
to the OPAL call, or have another test here which rejects theioctl()if the flag is *not* set.
Indeed. Linux/PowerNV and KVM require this flag to be set, I should error out if this is not the case. It is safer.
Quoted 46 lines
Quoted 30 lines
arch/powerpc/kvm/book3s_xive_native.c +30
-0+ + rc =xive_native_validate_queue_size (kvm_eq.qshift); +if (rc) { +pr_err ("invalid queue size %d\n" , kvm_eq.qshift); +return rc; + } + +/* reset queue and disable queueing */ +if (!kvm_eq.qshift) { + q->guest_qaddr =0 ; + q->guest_qshift =0 ; + + rc =xive_native_configure_queue (xc->vp_id, q, priority, +NULL ,0 , true); +if (rc) { +pr_err ("Failed to reset queue %d for VCPU %d: %d\n" , + priority, xc->server_num, rc); +return rc; + } + +if (q->qpage) { +put_page (virt_to_page (q->qpage)); + q->qpage =NULL ; + } + +return 0 ; + } + + gfn =gpa_to_gfn (kvm_eq.qaddr); + page =gfn_to_page (kvm, gfn);arch/powerpc/kvm/book3s_xive_native.c static int kvmppc_xive_native_set_source_config(struct kvmppc_xive *xive, · line 451
Quoted 10 lines
arch/powerpc/kvm/book3s_xive_native.c +10
-0+if (is_error_page (page)) { +pr_warn ("Couldn't get guest page for %llx!\n" , kvm_eq.qaddr); +return -EINVAL ; + } + + page_size =kvm_host_page_size (kvm, gfn); +if (1ull << kvm_eq.qshift > page_size) { +pr_warn ("Incompatible host page size %lx!\n" , page_size); +return -EINVAL ; + }Sorry.. I should have thought of this earlier. If qaddr isn't aligned to a (
page_size) boundary, this test isn't sufficient - you need to make sure the whole thing fits within the host page, even if it's offset.
Quoted 2 lines
Alternatively, you could check that qaddr is naturally aligned for the requested qshift. Possibly that's necessary for the hardware anyway.
Indeed again, it should be "naturally aligned". I will add an extra test for this. QEMU will need it also btw.
Thanks,
C.
Quoted 195 lines
Quoted 194 lines
arch/powerpc/kvm/book3s_xive_native.c +128
-0+ + qaddr =page_to_virt (page) + (kvm_eq.qaddr & ~PAGE_MASK ); + +/* +* Backup the queue page guest address to the mark EQ page +* dirty for migration. +*/ + q->guest_qaddr = kvm_eq.qaddr; + q->guest_qshift = kvm_eq.qshift; + +/* +* Unconditional Notification is forced by default at the +* OPAL level because the use of END ESBs is not supported by +* Linux. +*/ + rc =xive_native_configure_queue (xc->vp_id, q, priority, + (__be32 *) qaddr, kvm_eq.qshift, true); +if (rc) { +pr_err ("Failed to configure queue %d for VCPU %d: %d\n" , + priority, xc->server_num, rc); +put_page (page); +return rc; + } + +/* +* Only restore the queue state when needed. When doing the +* H_INT_SET_SOURCE_CONFIG hcall, it should not. +*/ +if (kvm_eq.qtoggle !=1 || kvm_eq.qindex !=0 ) { + rc =xive_native_set_queue_state (xc->vp_id, priority, + kvm_eq.qtoggle, + kvm_eq.qindex); +if (rc) + goto error; + } + + rc =kvmppc_xive_attach_escalation (vcpu, priority, + xive->single_escalation); +error: +if (rc) +kvmppc_xive_native_cleanup_queue (vcpu, priority); +return rc; +} + +static int kvmppc_xive_native_get_queue_config (struct kvmppc_xive *xive, +long eq_idx,u64 addr) +{ +struct kvm *kvm = xive->kvm; +struct kvm_vcpu *vcpu; +struct kvmppc_xive_vcpu *xc; +struct xive_q *q; +void __user *ubufp = (u64 __user *) addr; +u32 server; +u8 priority; +struct kvm_ppc_xive_eq kvm_eq; +u64 qaddr; +u64 qshift; +u64 qeoi_page; +u32 escalate_irq; +u64 qflags; +int rc; + +/* +* Demangle priority/server tuple from the EQ identifier +*/ + priority = (eq_idx &KVM_XIVE_EQ_PRIORITY_MASK ) >> +KVM_XIVE_EQ_PRIORITY_SHIFT ; + server = (eq_idx &KVM_XIVE_EQ_SERVER_MASK ) >> +KVM_XIVE_EQ_SERVER_SHIFT ; + + vcpu =kvmppc_xive_find_server (kvm, server); +if (!vcpu) { +pr_err ("Can't find server %d\n" , server); +return -ENOENT ; + } + xc = vcpu->arch.xive_vcpu; + +if (priority !=xive_prio_from_guest (priority)) { +pr_err ("invalid priority for queue %d for VCPU %d\n" , + priority, server); +return -EINVAL ; + } + q = &xc->queues[priority]; + +memset (&kvm_eq,0 ,sizeof (kvm_eq)); + +if (!q->qpage) +return 0 ; + + rc =xive_native_get_queue_info (xc->vp_id, priority, &qaddr, &qshift, + &qeoi_page, &escalate_irq, &qflags); +if (rc) +return rc; + + kvm_eq.flags =0 ; +if (qflags &OPAL_XIVE_EQ_ALWAYS_NOTIFY ) + kvm_eq.flags |=KVM_XIVE_EQ_ALWAYS_NOTIFY ; + + kvm_eq.qshift = q->guest_qshift; + kvm_eq.qaddr = q->guest_qaddr; + + rc =xive_native_get_queue_state (xc->vp_id, priority, &kvm_eq.qtoggle, + &kvm_eq.qindex); +if (rc) +return rc; + +pr_devel ("%s VCPU %d priority %d fl:%x shift:%d addr:%llx g:%d idx:%d\n" , + __func__, server, priority, kvm_eq.flags, + kvm_eq.qshift, kvm_eq.qaddr, kvm_eq.qtoggle, kvm_eq.qindex); + +if (copy_to_user (ubufp, &kvm_eq,sizeof (kvm_eq))) +return -EFAULT ; + +return 0 ; +} +static int kvmppc_xive_native_set_attr (struct kvm_device *dev,struct kvm_device_attr *attr) {@@ -349,6 +579,9 @@ static int kvmppc_xive_native_set_attr(struct kvm_device *dev,case KVM_DEV_XIVE_GRP_SOURCE_CONFIG :return kvmppc_xive_native_set_source_config (xive, attr->attr, attr->addr); +case KVM_DEV_XIVE_GRP_EQ_CONFIG : +return kvmppc_xive_native_set_queue_config (xive, attr->attr, + attr->addr); }return -ENXIO ; }@@ -356,6 +589,13 @@ static int kvmppc_xive_native_set_attr(struct kvm_device *dev,static int kvmppc_xive_native_get_attr (struct kvm_device *dev,struct kvm_device_attr *attr) { +struct kvmppc_xive *xive = dev->private; + +switch (attr->group) { +case KVM_DEV_XIVE_GRP_EQ_CONFIG : +return kvmppc_xive_native_get_queue_config (xive, attr->attr, + attr->addr); + }return -ENXIO ; }@@ -371,6 +611,8 @@ static int kvmppc_xive_native_has_attr(struct kvm_device *dev, attr->attr < KVMPPC_XIVE_NR_IRQS)return 0 ;break ; +case KVM_DEV_XIVE_GRP_EQ_CONFIG : +return 0 ; }return -ENXIO ; }Documentation/virtual/kvm/devices/xive.txt +34
-0@@ -53,3 +53,37 @@ the legacy interrupt mode, referred as XICS (POWER7/8). -ENXIO: CPU event queues not configured or configuration of the underlying HW interrupt failed -EBUSY: No CPU available to serve interrupt + + 4. KVM_DEV_XIVE_GRP_EQ_CONFIG (read-write) + Configures an event queue of a CPU + Attributes: + EQ descriptor identifier (64-bit) + The EQ descriptor identifier is a tuple (server, priority) : + bits: | 63 .... 32 | 31 .. 3 | 2 .. 0 + values: | unused | server | priority + The kvm_device_attr.addr points to : + struct kvm_ppc_xive_eq { + __u32 flags; + __u32 qshift; + __u64 qaddr; + __u32 qtoggle; + __u32 qindex; + __u8 pad[40]; + }; + - flags: queue flags + KVM_XIVE_EQ_ALWAYS_NOTIFY + forces notification without using the coalescing mechanism + provided by the XIVE END ESBs. + - qshift: queue size (power of 2) + - qaddr: real address of queue + - qtoggle: current queue toggle bit + - qindex: current queue index + - pad: reserved for future use + Errors: + -ENOENT: Invalid CPU number + -EINVAL: Invalid priority + -EINVAL: Invalid flags + -EINVAL: Invalid queue size + -EINVAL: Invalid queue address + -EFAULT: Invalid user pointer for attr->addr. + -EIO: Configuration of the underlying HW failed
PATCH v4 7/17 KVM: PPC: Book3S HV: XIVE: add a global reset control
In reply to Cédric Le GoaterThis control is to be used by the H_INT_RESET hcall from QEMU. Its purpose is to clear all configuration of the sources and EQs. This is necessary in case of a kexec (for a kdump kernel for instance) to make sure that no remaining configuration is left from the previous boot setup so that the new kernel can start safely from a clean state.
The queue 7 is ignored when the XIVE device is configured to run in single escalation mode. Prio 7 is used by escalations.
The XIVE VP is kept enabled as the vCPU is still active and connected to the XIVE device.
---
Changes since v2 :
- fixed locking on source block
arch/powerpc/include/uapi/asm/kvm.h | 1 + arch/powerpc/kvm/book3s_xive_native.c | 85 ++++++++++++++++++++++ Documentation/virtual/kvm/devices/xive.txt | 5 ++ 3 files changed, 91 insertions(+)
arch/powerpc/include/uapi/asm/kvm.h +1 -0
@@ -679,6 +679,7 @@ struct kvm_ppc_cpu_char {/* POWER9 XIVE Native Interrupt Controller */ #define KVM_DEV_XIVE_GRP_CTRL 1 +#define KVM_DEV_XIVE_RESET 1#define KVM_DEV_XIVE_GRP_SOURCE 2/* 64-bit source identifier */ #define KVM_DEV_XIVE_GRP_SOURCE_CONFIG 3/* 64-bit source identifier */ #define KVM_DEV_XIVE_GRP_EQ_CONFIG 4/* 64-bit EQ identifier */
arch/powerpc/kvm/book3s_xive_native.c +85 -0
103 lines not on this page · raw message
Documentation/virtual/kvm/devices/xive.txt +5 -0
@@ -17,6 +17,11 @@ the legacy interrupt mode, referred as XICS (POWER7/8). 1. KVM_DEV_XIVE_GRP_CTRL Provides global controls on the device + Attributes: + 1.1 KVM_DEV_XIVE_RESET (write only) + Resets the interrupt controller configuration for sources and event + queues. To be used by kexec and kdump. + Errors: none 2. KVM_DEV_XIVE_GRP_SOURCE (write only) Initializes a new source in the XIVE device and mask it.
Signature · 1 line
2.20.1
PATCH v4 8/17 KVM: PPC: Book3S HV: XIVE: add a control to sync the sources
In reply to Cédric Le GoaterThis control will be used by the H_INT_SYNC hcall from QEMU to flush event notifications on the XIVE IC owning the source.
---
Changes since v2 :
- fixed locking on source block
arch/powerpc/include/uapi/asm/kvm.h | 1 + arch/powerpc/kvm/book3s_xive_native.c | 36 ++++++++++++++++++++++ Documentation/virtual/kvm/devices/xive.txt | 8 +++++ 3 files changed, 45 insertions(+)
arch/powerpc/include/uapi/asm/kvm.h +1 -0
@@ -683,6 +683,7 @@ struct kvm_ppc_cpu_char {#define KVM_DEV_XIVE_GRP_SOURCE 2/* 64-bit source identifier */ #define KVM_DEV_XIVE_GRP_SOURCE_CONFIG 3/* 64-bit source identifier */ #define KVM_DEV_XIVE_GRP_EQ_CONFIG 4/* 64-bit EQ identifier */ +#define KVM_DEV_XIVE_GRP_SOURCE_SYNC 5/* 64-bit source identifier */ /* Layout of 64-bit XIVE source attribute values */ #define KVM_XIVE_LEVEL_SENSITIVE (1ULL << 0)
arch/powerpc/kvm/book3s_xive_native.c +36 -0
@@ -335,6 +335,38 @@ static int kvmppc_xive_native_set_source_config(struct kvmppc_xive *xive, priority, masked, eisn); } +static int kvmppc_xive_native_sync_source (struct kvmppc_xive *xive, +long irq,u64 addr) +{ +struct kvmppc_xive_src_block *sb; +struct kvmppc_xive_irq_state *state; +struct xive_irq_data *xd; +u32 hw_num; +u16 src; +int rc =0 ; + +pr_devel ("%s irq=0x%lx" , __func__, irq); + + sb =kvmppc_xive_find_source (xive, irq, &src); +if (!sb) +return -ENOENT ; + + state = &sb->irq_state[src]; + + rc = -EINVAL ; + +arch_spin_lock (&sb->lock); + +if (state->valid) { +kvmppc_xive_select_irq (state, &hw_num, &xd); +xive_native_sync_source (hw_num); + rc =0 ; + } + +arch_spin_unlock (&sb->lock); +return rc; +} +static int xive_native_validate_queue_size (u32 qshift) {/*
@@ -663,6 +695,9 @@ static int kvmppc_xive_native_set_attr(struct kvm_device *dev,case KVM_DEV_XIVE_GRP_EQ_CONFIG :return kvmppc_xive_native_set_queue_config (xive, attr->attr, attr->addr); +case KVM_DEV_XIVE_GRP_SOURCE_SYNC : +return kvmppc_xive_native_sync_source (xive, attr->attr, + attr->addr); }return -ENXIO ; }
@@ -692,6 +727,7 @@ static int kvmppc_xive_native_has_attr(struct kvm_device *dev,break ;case KVM_DEV_XIVE_GRP_SOURCE :case KVM_DEV_XIVE_GRP_SOURCE_CONFIG : +case KVM_DEV_XIVE_GRP_SOURCE_SYNC :if (attr->attr >=KVMPPC_XIVE_FIRST_IRQ && attr->attr <KVMPPC_XIVE_NR_IRQS )return 0 ;
Documentation/virtual/kvm/devices/xive.txt +8 -0
@@ -92,3 +92,11 @@ the legacy interrupt mode, referred as XICS (POWER7/8). -EINVAL: Invalid queue address -EFAULT: Invalid user pointer for attr->addr. -EIO: Configuration of the underlying HW failed + + 5. KVM_DEV_XIVE_GRP_SOURCE_SYNC (write only) + Synchronize the source to flush event notifications + Attributes: + Interrupt source number (64-bit) + Errors: + -ENOENT: Unknown source number + -EINVAL: Not initialized source number
Signature · 1 line
2.20.1
PATCH v4 9/17 KVM: PPC: Book3S HV: XIVE: add a control to dirty the XIVE EQ pages
In reply to Cédric Le GoaterWhen migration of a VM is initiated, a first copy of the RAM is transferred to the destination before the VM is stopped, but there is no guarantee that the EQ pages in which the event notifications are queued have not been modified.
To make sure migration will capture a consistent memory state, the XIVE device should perform a XIVE quiesce sequence to stop the flow of event notifications and stabilize the EQs. This is the purpose of the KVM_DEV_XIVE_EQ_SYNC control which will also marks the EQ pages dirty to force their transfer.
---
Changes since v2 :
- Extra comments
- fixed locking on source block
arch/powerpc/include/uapi/asm/kvm.h | 1 + arch/powerpc/kvm/book3s_xive_native.c | 85 ++++++++++++++++++++++ Documentation/virtual/kvm/devices/xive.txt | 29 ++++++++ 3 files changed, 115 insertions(+)
arch/powerpc/include/uapi/asm/kvm.h +1 -0
@@ -680,6 +680,7 @@ struct kvm_ppc_cpu_char {/* POWER9 XIVE Native Interrupt Controller */ #define KVM_DEV_XIVE_GRP_CTRL 1#define KVM_DEV_XIVE_RESET 1 +#define KVM_DEV_XIVE_EQ_SYNC 2#define KVM_DEV_XIVE_GRP_SOURCE 2/* 64-bit source identifier */ #define KVM_DEV_XIVE_GRP_SOURCE_CONFIG 3/* 64-bit source identifier */ #define KVM_DEV_XIVE_GRP_EQ_CONFIG 4/* 64-bit EQ identifier */
arch/powerpc/kvm/book3s_xive_native.c +85 -0
103 lines not on this page · raw message
Documentation/virtual/kvm/devices/xive.txt +29 -0
@@ -23,6 +23,12 @@ the legacy interrupt mode, referred as XICS (POWER7/8). queues. To be used by kexec and kdump. Errors: none + 1.2 KVM_DEV_XIVE_EQ_SYNC (write only) + Sync all the sources and queues and mark the EQ pages dirty. This + to make sure that a consistent memory state is captured when + migrating the VM. + Errors: none + 2. KVM_DEV_XIVE_GRP_SOURCE (write only) Initializes a new source in the XIVE device and mask it. Attributes:
@@ -100,3 +106,26 @@ the legacy interrupt mode, referred as XICS (POWER7/8). Errors: -ENOENT: Unknown source number -EINVAL: Not initialized source number + +* Migration: + + Saving the state of a VM using the XIVE native exploitation mode + should follow a specific sequence. When the VM is stopped : + + 1. Mask all sources (PQ=01) to stop the flow of events. + + 2. Sync the XIVE device with the KVM control KVM_DEV_XIVE_EQ_SYNC to + flush any in-flight event notification and to stabilize the EQs. At + this stage, the EQ pages are marked dirty to make sure they are + transferred in the migration sequence. + + 3. Capture the state of the source targeting, the EQs configuration + and the state of thread interrupt context registers. + + Restore is similar : + + 1. Restore the EQ configuration. As targeting depends on it. + 2. Restore targeting + 3. Restore the thread interrupt contexts + 4. Restore the source states + 5. Let the vCPU run
Signature · 1 line
2.20.1
PATCH v4 10/17 KVM: PPC: Book3S HV: XIVE: add get/set accessors for the VP XIVE state
In reply to Cédric Le GoaterThe state of the thread interrupt management registers needs to be collected for migration. These registers are cached under the 'xive_saved_state.w01' field of the VCPU when the VPCU context is pulled from the HW thread. An OPAL call retrieves the backup of the IPB register in the underlying XIVE NVT structure and merges it in the KVM state.
---
Changes since v3 :
- Fixed xive_timaval description in documentation
Changes since v2 :
- reduced the size of kvmppc_one_reg timaval attribute to two u64s
- stopped returning of the OS CAM line value
arch/powerpc/include/asm/kvm_ppc.h | 11 ++++ arch/powerpc/include/uapi/asm/kvm.h | 2 + arch/powerpc/kvm/book3s.c | 24 +++++++ arch/powerpc/kvm/book3s_xive_native.c | 76 ++++++++++++++++++++++ Documentation/virtual/kvm/devices/xive.txt | 17 +++++ 5 files changed, 130 insertions(+)
arch/powerpc/include/asm/kvm_ppc.h +11 -0
@@ -273,6 +273,7 @@ union kvmppc_one_reg {u64 addr;u64 length; } vpaval; +u64 xive_timaval[2 ]; };struct kvmppc_ops {
@@ -605,6 +606,10 @@ extern int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,extern void kvmppc_xive_native_cleanup_vcpu (struct kvm_vcpu *vcpu);extern void kvmppc_xive_native_init_module (void );extern void kvmppc_xive_native_exit_module (void ); +extern int kvmppc_xive_native_get_vp (struct kvm_vcpu *vcpu, +union kvmppc_one_reg *val); +extern int kvmppc_xive_native_set_vp (struct kvm_vcpu *vcpu, +union kvmppc_one_reg *val);#else static inline int kvmppc_xive_set_xive(struct kvm *kvm, u32 irq, u32 server,
@@ -637,6 +642,12 @@ static inline int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,static inline void kvmppc_xive_native_cleanup_vcpu (struct kvm_vcpu *vcpu) { }static inline void kvmppc_xive_native_init_module (void ) { }static inline void kvmppc_xive_native_exit_module (void ) { } +static inline int kvmppc_xive_native_get_vp (struct kvm_vcpu *vcpu, +union kvmppc_one_reg *val) +{return 0 ; } +static inline int kvmppc_xive_native_set_vp (struct kvm_vcpu *vcpu, +union kvmppc_one_reg *val) +{return -ENOENT ; }#endif /* CONFIG_KVM_XIVE */
arch/powerpc/include/uapi/asm/kvm.h +2 -0
@@ -482,6 +482,8 @@ struct kvm_ppc_cpu_char {#define KVM_REG_PPC_ICP_PPRI_SHIFT 16/* pending irq priority */ #define KVM_REG_PPC_ICP_PPRI_MASK 0xff +#define KVM_REG_PPC_VP_STATE (KVM_REG_PPC | KVM_REG_SIZE_U128 | 0x8d) +/* Device control API: PPC-specific devices */ #define KVM_DEV_MPIC_GRP_MISC 1#define KVM_DEV_MPIC_BASE_ADDR 0/* 64-bit */
arch/powerpc/kvm/book3s.c +24 -0
@@ -651,6 +651,18 @@ int kvmppc_get_one_reg(struct kvm_vcpu *vcpu, u64 id, *val =get_reg_val (id,kvmppc_xics_get_icp (vcpu));break ;#endif /* CONFIG_KVM_XICS */ +#ifdef CONFIG_KVM_XIVE +case KVM_REG_PPC_VP_STATE : +if (!vcpu->arch.xive_vcpu) { + r = -ENXIO ; +break ; + } +if (xive_enabled ()) + r =kvmppc_xive_native_get_vp (vcpu, val); +else + r = -ENXIO ; +break ; +#endif /* CONFIG_KVM_XIVE */ case KVM_REG_PPC_FSCR : *val =get_reg_val (id, vcpu->arch.fscr);break ;
@@ -724,6 +736,18 @@ int kvmppc_set_one_reg(struct kvm_vcpu *vcpu, u64 id, r =kvmppc_xics_set_icp (vcpu,set_reg_val (id, *val));break ;#endif /* CONFIG_KVM_XICS */ +#ifdef CONFIG_KVM_XIVE +case KVM_REG_PPC_VP_STATE : +if (!vcpu->arch.xive_vcpu) { + r = -ENXIO ; +break ; + } +if (xive_enabled ()) + r =kvmppc_xive_native_set_vp (vcpu, val); +else + r = -ENXIO ; +break ; +#endif /* CONFIG_KVM_XIVE */ case KVM_REG_PPC_FSCR : vcpu->arch.fscr =set_reg_val (id, *val);break ;
arch/powerpc/kvm/book3s_xive_native.c +76 -0
82 lines not on this page · raw message
Documentation/virtual/kvm/devices/xive.txt +17 -0
@@ -107,6 +107,23 @@ the legacy interrupt mode, referred as XICS (POWER7/8). -ENOENT: Unknown source number -EINVAL: Not initialized source number +* VCPU state + + The XIVE IC maintains VP interrupt state in an internal structure + called the NVT. When a VP is not dispatched on a HW processor + thread, this structure can be updated by HW if the VP is the target + of an event notification. + + It is important for migration to capture the cached IPB from the NVT + as it synthesizes the priorities of the pending interrupts. We + capture a bit more to report debug information. + + KVM_REG_PPC_VP_STATE (2 * 64bits) + bits: | 63 .... 32 | 31 .... 0 | + values: | TIMA word0 | TIMA word1 | + bits: | 127 .......... 64 | + values: | unused | + * Migration: Saving the state of a VM using the XIVE native exploitation mode
Signature · 1 line
2.20.1
Re: [PATCH v4 10/17] KVM: PPC: Book3S HV: XIVE: add get/set accessors for the VP XIVE state
Quoted 6 lines · Cédric Le Goater
The state of the thread interrupt management registers needs to be collected for migration. These registers are cached under the 'xive_saved_state.w01' field of the VCPU when the VPCU context is pulled from the HW thread. An OPAL call retrieves the backup of the IPB register in the underlying XIVE NVT structure and merges it in the KVM state.
Since you're adding a new one_reg identifier value, you need to update the list in Documentation/virtual/kvm/api.txt.
Paul.
Re: [PATCH v4 10/17] KVM: PPC: Book3S HV: XIVE: add get/set accessors for the VP XIVE state
Quoted 10 lines · Paul Mackerras
Quoted 6 lines · Cédric Le Goater
The state of the thread interrupt management registers needs to be collected for migration. These registers are cached under the 'xive_saved_state.w01' field of the VCPU when the VPCU context is pulled from the HW thread. An OPAL call retrieves the backup of the IPB register in the underlying XIVE NVT structure and merges it in the KVM state.
Since you're adding a new
one_regidentifier value, you need to update the list inDocumentation/virtual/kvm/api.txt.
yes. I missed that file.
I will keep a description of the VP state register layout in the device documentation file :
Documentation/virtual/kvm/devices/xive.txt
I don't think it belongs to kvm/api.txt file.
Thanks,
C.
PATCH v4 11/17 KVM: introduce a 'mmap' method for KVM devices
In reply to Cédric Le GoaterSome KVM devices will want to handle special mappings related to the underlying HW. For instance, the XIVE interrupt controller of the POWER9 processor has MMIO pages for thread interrupt management and for interrupt source control that need to be exposed to the guest when the OS has the required support.
--- include/linux/kvm_host.h | 1 + virt/kvm/kvm_main.c | 11 +++++++++++ 2 files changed, 12 insertions(+)
include/linux/kvm_host.h +1 -0
@@ -1245,6 +1245,7 @@ struct kvm_device_ops {int (*has_attr)(struct kvm_device *dev,struct kvm_device_attr *attr);long (*ioctl)(struct kvm_device *dev,unsigned int ioctl,unsigned long arg); +int (*mmap)(struct kvm_device *dev,struct vm_area_struct *vma); };void kvm_device_get (struct kvm_device *dev);
virt/kvm/kvm_main.c +11 -0
@@ -2884,6 +2884,16 @@ static long kvm_vcpu_compat_ioctl(struct file *filp, }#endif +static int kvm_device_mmap (struct file *filp,struct vm_area_struct *vma) +{ +struct kvm_device *dev = filp->private_data; + +if (dev->ops->mmap) +return dev->ops->mmap (dev, vma); + +return -ENODEV ; +} + static int kvm_device_ioctl_attr(struct kvm_device *dev, int (*accessor)(struct kvm_device *dev, struct kvm_device_attr *attr),
@@ -2933,6 +2943,7 @@ static const struct file_operations kvm_device_fops = { .unlocked_ioctl = kvm_device_ioctl, .release = kvm_device_release,KVM_COMPAT (kvm_device_ioctl), + .mmap = kvm_device_mmap, };struct kvm_device *kvm_device_from_filp (struct file *filp)
Signature · 1 line
2.20.1
PATCH v4 12/17 KVM: PPC: Book3S HV: XIVE: add a TIMA mapping
In reply to Cédric Le GoaterEach thread has an associated Thread Interrupt Management context composed of a set of registers. These registers let the thread handle priority management and interrupt acknowledgment. The most important are :
- Interrupt Pending Buffer (IPB)
- Current Processor Priority (CPPR)
- Notification Source Register (NSR)
They are exposed to software in four different pages each proposing a view with a different privilege. The first page is for the physical thread context and the second for the hypervisor. Only the third (operating system) and the fourth (user level) are exposed the guest.
A custom VM fault handler will populate the VMA with the appropriate pages, which should only be the OS page for now.
--- arch/powerpc/include/asm/xive.h | 1 + arch/powerpc/include/uapi/asm/kvm.h | 2 ++ arch/powerpc/kvm/book3s_xive_native.c | 39 ++++++++++++++++++++++ arch/powerpc/sysdev/xive/native.c | 11 ++++++ Documentation/virtual/kvm/devices/xive.txt | 23 +++++++++++++ 5 files changed, 76 insertions(+)
arch/powerpc/include/asm/xive.h +1 -0
@@ -23,6 +23,7 @@* same offset regardless of where the code is executing */ extern void __iomem *xive_tima; +extern unsigned long xive_tima_os;/* * Offset in the TM area of our current execution level (provided by
arch/powerpc/include/uapi/asm/kvm.h +2 -0
@@ -720,4 +720,6 @@ struct kvm_ppc_xive_eq {#define KVM_XIVE_EQ_ALWAYS_NOTIFY 0x00000001 +#define KVM_XIVE_TIMA_PAGE_OFFSET 0 +#endif /* __LINUX_KVM_POWERPC_H */
arch/powerpc/kvm/book3s_xive_native.c +39 -0
@@ -165,6 +165,44 @@ int kvmppc_xive_native_connect_vcpu(struct kvm_device *dev,return rc; } +static vm_fault_t xive_native_tima_fault (struct vm_fault *vmf) +{ +struct vm_area_struct *vma = vmf->vma; + +switch (vmf->pgoff - vma->vm_pgoff) { +case 0 :/* HW - forbid access */ +case 1 :/* HV - forbid access */ +return VM_FAULT_SIGBUS ; +case 2 :/* OS */ +vmf_insert_pfn (vma, vmf->address, xive_tima_os >>PAGE_SHIFT ); +return VM_FAULT_NOPAGE ; +case 3 :/* USER - TODO */ +default : +return VM_FAULT_SIGBUS ; + } +} + +static const struct vm_operations_struct xive_native_tima_vmops = { + .fault = xive_native_tima_fault, +}; + +static int kvmppc_xive_native_mmap (struct kvm_device *dev, +struct vm_area_struct *vma) +{ +/* We only allow mappings at fixed offset for now */ +if (vma->vm_pgoff ==KVM_XIVE_TIMA_PAGE_OFFSET ) { +if (vma_pages (vma) >4 ) +return -EINVAL ; + vma->vm_ops = &xive_native_tima_vmops; + }else { +return -EINVAL ; + } + + vma->vm_flags |=VM_IO |VM_PFNMAP ; + vma->vm_page_prot =pgprot_noncached_wc (vma->vm_page_prot); +return 0 ; +} +static int kvmppc_xive_native_set_source (struct kvmppc_xive *xive,long irq,u64 addr) {
@@ -1043,6 +1081,7 @@ struct kvm_device_ops kvm_xive_native_ops = { .set_attr = kvmppc_xive_native_set_attr, .get_attr = kvmppc_xive_native_get_attr, .has_attr = kvmppc_xive_native_has_attr, + .mmap = kvmppc_xive_native_mmap, };void kvmppc_xive_native_init_module (void )
arch/powerpc/sysdev/xive/native.c +11 -0
@@ -521,6 +521,9 @@ u32 xive_native_default_eq_shift(void) }EXPORT_SYMBOL_GPL (xive_native_default_eq_shift); +unsigned long xive_tima_os; +EXPORT_SYMBOL_GPL (xive_tima_os); +bool __init xive_native_init (void ) {struct device_node *np;
@@ -573,6 +576,14 @@ bool __init xive_native_init(void)for_each_possible_cpu (cpu)kvmppc_set_xive_tima (cpu, r.start, tima); +/* Resource 2 is OS window */ +if (of_address_to_resource (np,2 , &r)) { +pr_err ("Failed to get thread mgmnt area resource\n" ); +return false; + } + + xive_tima_os = r.start; +/* Grab size of provisionning pages */ xive_parse_provisioning (np);
Documentation/virtual/kvm/devices/xive.txt +23 -0
@@ -13,6 +13,29 @@ requires a POWER9 host and the guest OS should have support for the XIVE native exploitation interrupt mode. If not, it should run using the legacy interrupt mode, referred as XICS (POWER7/8). +* Device Mappings + + The KVM device exposes different MMIO ranges of the XIVE HW which + are required for interrupt management. These are exposed to the + guest in VMAs populated with a custom VM fault handler. + + 1. Thread Interrupt Management Area (TIMA) + + Each thread has an associated Thread Interrupt Management context + composed of a set of registers. These registers let the thread + handle priority management and interrupt acknowledgment. The most + important are : + + - Interrupt Pending Buffer (IPB) + - Current Processor Priority (CPPR) + - Notification Source Register (NSR) + + They are exposed to software in four different pages each proposing + a view with a different privilege. The first page is for the + physical thread context and the second for the hypervisor. Only the + third (operating system) and the fourth (user level) are exposed the + guest. + * Groups: 1. KVM_DEV_XIVE_GRP_CTRL
Signature · 1 line
2.20.1
PATCH v4 13/17 KVM: PPC: Book3S HV: XIVE: add a mapping for the source ESB pages
In reply to Cédric Le GoaterEach source is associated with an Event State Buffer (ESB) with a even/odd pair of pages which provides commands to manage the source:
to trigger, to EOI, to turn off the source for instance.
The custom VM fault handler will deduce the guest IRQ number from the offset of the fault, and the ESB page of the associated XIVE interrupt will be inserted into the VMA using the internal structure caching information on the interrupts.
--- arch/powerpc/include/uapi/asm/kvm.h | 1 + arch/powerpc/kvm/book3s_xive_native.c | 57 ++++++++++++++++++++++ Documentation/virtual/kvm/devices/xive.txt | 7 +++ 3 files changed, 65 insertions(+)
arch/powerpc/include/uapi/asm/kvm.h +1 -0
@@ -721,5 +721,6 @@ struct kvm_ppc_xive_eq {#define KVM_XIVE_EQ_ALWAYS_NOTIFY 0x00000001#define KVM_XIVE_TIMA_PAGE_OFFSET 0 +#define KVM_XIVE_ESB_PAGE_OFFSET 4#endif /* __LINUX_KVM_POWERPC_H */
arch/powerpc/kvm/book3s_xive_native.c +57 -0
69 lines not on this page · raw message
Documentation/virtual/kvm/devices/xive.txt +7 -0
@@ -36,6 +36,13 @@ the legacy interrupt mode, referred as XICS (POWER7/8). third (operating system) and the fourth (user level) are exposed the guest. + 2. Event State Buffer (ESB) + + Each source is associated with an Event State Buffer (ESB) with + either a pair of even/odd pair of pages which provides commands to + manage the source: to trigger, to EOI, to turn off the source for + instance. + * Groups: 1. KVM_DEV_XIVE_GRP_CTRL
Signature · 1 line
2.20.1
PATCH v4 14/17 KVM: PPC: Book3S HV: XIVE: add passthrough support
In reply to Cédric Le GoaterThe KVM XICS-over-XIVE device and the proposed KVM XIVE native device implement an IRQ space for the guest using the generic IPI interrupts of the XIVE IC controller. These interrupts are allocated at the OPAL level and "mapped" into the guest IRQ number space in the range 0-0x1FFF. Interrupt management is performed in the XIVE way: using loads and stores on the addresses of the XIVE IPI interrupt ESB pages.
Both KVM devices share the same internal structure caching information on the interrupts, among which the xive_irq_data struct containing the addresses of the IPI ESB pages and an extra one in case of pass-through. The later contains the addresses of the ESB pages of the underlying HW controller interrupts, PHB4 in all cases for now.
A guest, when running in the XICS legacy interrupt mode, lets the KVM XICS-over-XIVE device "handle" interrupt management, that is to perform the loads and stores on the addresses of the ESB pages of the guest interrupts. However, when running in XIVE native exploitation mode, the KVM XIVE native device exposes the interrupt ESB pages to the guest and lets the guest perform directly the loads and stores.
The VMA exposing the ESB pages make use of a custom VM fault handler which role is to populate the VMA with appropriate pages. When a fault occurs, the guest IRQ number is deduced from the offset, and the ESB pages of associated XIVE IPI interrupt are inserted in the VMA (using the internal structure caching information on the interrupts).
Supporting device passthrough in the guest running in XIVE native exploitation mode adds some extra refinements because the ESB pages of a different HW controller (PHB4) need to be exposed to the guest along with the initial IPI ESB pages of the XIVE IC controller. But the overall mechanic is the same.
When the device HW irqs are mapped into or unmapped from the guest IRQ number space, the passthru_irq helpers, kvmppc_xive_set_mapped() and kvmppc_xive_clr_mapped(), are called to record or clear the passthrough interrupt information and to perform the switch.
The approach taken by this patch is to clear the ESB pages of the guest IRQ number being mapped and let the VM fault handler repopulate. The handler will insert the ESB page corresponding to the HW interrupt of the device being passed-through or the initial IPI ESB page if the device is being removed.
---
Changes since v2 :
- extra comment in documentation
arch/powerpc/kvm/book3s_xive.h | 9 +++++ arch/powerpc/kvm/book3s_xive.c | 15 ++++++++ arch/powerpc/kvm/book3s_xive_native.c | 41 ++++++++++++++++++++++ Documentation/virtual/kvm/devices/xive.txt | 19 ++++++++++ 4 files changed, 84 insertions(+)
arch/powerpc/kvm/book3s_xive.h +9 -0
@@ -94,6 +94,11 @@ struct kvmppc_xive_src_block {struct kvmppc_xive_irq_state irq_state[KVMPPC_XICS_IRQ_PER_ICS ]; }; +struct kvmppc_xive ; + +struct kvmppc_xive_ops { +int (*reset_mapped)(struct kvm *kvm,unsigned long guest_irq); +};struct kvmppc_xive {struct kvm *kvm;
@@ -132,6 +137,10 @@ struct kvmppc_xive {/* Flags */ u8 single_escalation; + +struct kvmppc_xive_ops *ops; +struct address_space *mapping; +struct mutex mapping_lock; };#define KVMPPC_XIVE_Q_COUNT 8
arch/powerpc/kvm/book3s_xive.c +15 -0
@@ -937,6 +937,13 @@ int kvmppc_xive_set_mapped(struct kvm *kvm, unsigned long guest_irq,/* Turn the IPI hard off */ xive_vm_esb_load (&state->ipi_data,XIVE_ESB_SET_PQ_01 ); +/* +* Reset ESB guest mapping. Needed when ESB pages are exposed +* to the guest in XIVE native mode +*/ +if (xive->ops && xive->ops->reset_mapped) + xive->ops->reset_mapped (kvm, guest_irq); +/* Grab info about irq */ state->pt_number = hw_irq; state->pt_data =irq_data_get_irq_handler_data (host_data);
@@ -1022,6 +1029,14 @@ int kvmppc_xive_clr_mapped(struct kvm *kvm, unsigned long guest_irq, state->pt_number =0 ; state->pt_data =NULL ; +/* +* Reset ESB guest mapping. Needed when ESB pages are exposed +* to the guest in XIVE native mode +*/ +if (xive->ops && xive->ops->reset_mapped) { + xive->ops->reset_mapped (kvm, guest_irq); + } +/* Reconfigure the IPI */ xive_native_configure_irq(state->ipi_number, kvmppc_xive_vp(xive, state->act_server),
arch/powerpc/kvm/book3s_xive_native.c +41 -0
77 lines not on this page · raw message
Documentation/virtual/kvm/devices/xive.txt +19 -0
@@ -43,6 +43,25 @@ the legacy interrupt mode, referred as XICS (POWER7/8). manage the source: to trigger, to EOI, to turn off the source for instance. + 3. Device pass-through + + When a device is passed-through into the guest, the source + interrupts are from a different HW controller (PHB4) and the ESB + pages exposed to the guest should accommadate this change. + + The passthru_irq helpers, kvmppc_xive_set_mapped() and + kvmppc_xive_clr_mapped() are called when the device HW irqs are + mapped into or unmapped from the guest IRQ number space. The KVM + device extends these helpers to clear the ESB pages of the guest IRQ + number being mapped and then lets the VM fault handler repopulate. + The handler will insert the ESB page corresponding to the HW + interrupt of the device being passed-through or the initial IPI ESB + page if the device has being removed. + + The ESB remapping is fully transparent to the guest and the OS + device driver. All handling is done within VFIO and the above + helpers in KVM-PPC. + * Groups: 1. KVM_DEV_XIVE_GRP_CTRL
Signature · 1 line
2.20.1
PATCH v4 15/17 KVM: PPC: Book3S HV: XIVE: activate XIVE exploitation mode
In reply to Cédric Le GoaterFull support for the XIVE native exploitation mode is now available, advertise the capability KVM_CAP_PPC_IRQ_XIVE for guests running on PowerNV KVM Hypervisors only. Support for nested guests (pseries KVM Hypervisor) is not yet available. XIVE should also have been activated which is default setting on POWER9 systems running a recent Linux kernel.
--- arch/powerpc/kvm/powerpc.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
arch/powerpc/kvm/powerpc.c +4 -3
@@ -573,10 +573,11 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)#ifdef CONFIG_KVM_XIVE case KVM_CAP_PPC_IRQ_XIVE :/* -* Return false until all the XIVE infrastructure is -* in place including support for migration. +* We need XIVE to be enabled on the platform (implies +* a POWER9 processor) and the PowerNV platform, as +* nested is not yet supported. */ - r =0 ; + r =xive_enabled () && !!cpu_has_feature (CPU_FTR_HVMODE );break ;#endif
Signature · 1 line
2.20.1
PATCH v4 16/17 KVM: introduce a KVM_DESTROY_DEVICE ioctl
In reply to Cédric Le GoaterThe 'destroy' method is currently used to destroy all devices when the VM is destroyed after the vCPUs have been freed.
This new KVM ioctl exposes the same KVM device method. It acts as a software reset of the VM to 'destroy' selected devices when necessary and perform the required cleanups on the vCPUs. Called with the kvm->lock.
The 'destroy' method could be improved by returning an error code.
---
Changes since v3 :
- Removed temporary TODO comment in kvm_ioctl_destroy_device() regarding kvm_put_kvm()
Changes since v2 :
- checked that device is owned by VM
include/uapi/linux/kvm.h | 7 ++++++ virt/kvm/kvm_main.c | 41 +++++++++++++++++++++++++++++++ Documentation/virtual/kvm/api.txt | 20 +++++++++++++++ 3 files changed, 68 insertions(+)
include/uapi/linux/kvm.h +7 -0
@@ -1183,6 +1183,11 @@ struct kvm_create_device {__u32 flags;/* in: KVM_CREATE_DEVICE_xxx */ }; +struct kvm_destroy_device { +__u32 fd;/* in: device handle */ +__u32 flags;/* in: unused */ +}; +struct kvm_device_attr {__u32 flags;/* no flags currently defined */ __u32 group;/* device-defined */
@@ -1331,6 +1336,8 @@ struct kvm_s390_ucas_mapping {#define KVM_GET_DEVICE_ATTR _IOW(KVMIO, 0xe2, struct kvm_device_attr)#define KVM_HAS_DEVICE_ATTR _IOW(KVMIO, 0xe3, struct kvm_device_attr) +#define KVM_DESTROY_DEVICE _IOWR(KVMIO, 0xf0, struct kvm_destroy_device) +/* * ioctls for vcpu fds */
virt/kvm/kvm_main.c +41 -0
53 lines not on this page · raw message
Documentation/virtual/kvm/api.txt +20 -0
@@ -3874,6 +3874,26 @@ number of valid entries in the 'entries' array, which is then filled. 'index' and 'flags' fields in 'struct kvm_cpuid_entry2' are currently reserved, userspace should not expect to get any particular value there. +4.119 KVM_DESTROY_DEVICE + +Capability: KVM_CAP_DEVICE_CTRL +Type: vm ioctl +Parameters: struct kvm_destroy_device (in) +Returns: 0 on success, -1 on error +Errors: + ENODEV: The device type is unknown or unsupported + EPERM: The device does not belong to the VM + + Other error conditions may be defined by individual device types or + have their standard meanings. + +Destroys an emulated device in the kernel. + +struct kvm_destroy_device { + __u32 fd; /* in: device handle */ + __u32 flags; /* unused */ +}; + 5. The kvm_run structure ------------------------
Signature · 1 line
2.20.1
Re: [PATCH v4 16/17] KVM: introduce a KVM_DESTROY_DEVICE ioctl
Quoted 9 lines · Cédric Le Goater
The 'destroy' method is currently used to destroy all devices when the VM is destroyed after the vCPUs have been freed.
This new KVM ioctl exposes the same KVM device method. It acts as a software reset of the VM to 'destroy' selected devices when necessary and perform the required cleanups on the vCPUs. Called with the
kvm->lock.The 'destroy' method could be improved by returning an error code.
I am working on a new approach for the interrupt device switching that I will send as a replacement of patch 16 and 17. This is for discussion and a v5 will be needed at the end.
C.
Quoted 135 lines
Cc Paolo Bonzini <[email protected]>Signed-off-by Cédric Le Goater <[email protected]>Reviewed-by David Gibson <[email protected]>---
Changes since v3 :
- Removed temporary TODO comment in
kvm_ioctl_destroy_device()regardingkvm_put_kvm()Changes since v2 :
- checked that device is owned by VM
include/uapi/linux/kvm.h | 7 ++++++ virt/kvm/kvm_main.c | 41 +++++++++++++++++++++++++++++++ Documentation/virtual/kvm/api.txt | 20 +++++++++++++++ 3 files changed, 68 insertions(+)include/uapi/linux/kvm.h +7
-0@@ -1183,6 +1183,11 @@ struct kvm_create_device {__u32 flags;/* in: KVM_CREATE_DEVICE_xxx */ }; +struct kvm_destroy_device { +__u32 fd;/* in: device handle */ +__u32 flags;/* in: unused */ +}; +struct kvm_device_attr {__u32 flags;/* no flags currently defined */ __u32 group;/* device-defined */ @@ -1331,6 +1336,8 @@ struct kvm_s390_ucas_mapping {#define KVM_GET_DEVICE_ATTR _IOW(KVMIO, 0xe2, struct kvm_device_attr)#define KVM_HAS_DEVICE_ATTR _IOW(KVMIO, 0xe3, struct kvm_device_attr) +#define KVM_DESTROY_DEVICE _IOWR(KVMIO, 0xf0, struct kvm_destroy_device) +/* * ioctls for vcpu fds */ virt/kvm/kvm_main.c +41
-0@@ -3032,6 +3032,33 @@ static int kvm_ioctl_create_device(struct kvm *kvm,return 0 ; } +static int kvm_ioctl_destroy_device (struct kvm *kvm, +struct kvm_destroy_device *dd) +{ +struct fd f; +struct kvm_device *dev; + + f =fdget (dd->fd); +if (!f.file) +return -EBADF ; + + dev =kvm_device_from_filp (f.file); +fdput (f); + +if (!dev) +return -ENODEV ; + +if (dev->kvm != kvm) +return -EPERM ; + +mutex_lock (&kvm->lock); +list_del (&dev->vm_node); + dev->ops->destroy (dev); +mutex_unlock (&kvm->lock); + +return 0 ; +} +static long kvm_vm_ioctl_check_extension_generic (struct kvm *kvm,long arg) {switch (arg) {@@ -3276,6 +3303,20 @@ static long kvm_vm_ioctl(struct file *filp, r =0 ;break ; } +case KVM_DESTROY_DEVICE : { +struct kvm_destroy_device dd; + + r = -EFAULT ; +if (copy_from_user (&dd, argp,sizeof (dd))) + goto out; + + r =kvm_ioctl_destroy_device (kvm, &dd); +if (r) + goto out; + + r =0 ; +break ; + }case KVM_CHECK_EXTENSION : r =kvm_vm_ioctl_check_extension_generic (kvm, arg);break ;Documentation/virtual/kvm/api.txt +20
-0@@ -3874,6 +3874,26 @@ number of valid entries in the 'entries' array, which is then filled. 'index' and 'flags' fields in 'struct kvm_cpuid_entry2' are currently reserved, userspace should not expect to get any particular value there. +4.119 KVM_DESTROY_DEVICE + +Capability: KVM_CAP_DEVICE_CTRL +Type: vm ioctl +Parameters: struct kvm_destroy_device (in) +Returns: 0 on success, -1 on error +Errors: + ENODEV: The device type is unknown or unsupported + EPERM: The device does not belong to the VM + + Other error conditions may be defined by individual device types or + have their standard meanings. + +Destroys an emulated device in the kernel. + +struct kvm_destroy_device { + __u32 fd; /* in: device handle */ + __u32 flags; /* unused */ +}; + 5. The kvm_run structure ------------------------
RFC PATCH v4 16/17 KVM: PPC: Book3S HV: XIVE: introduce a xive_devices array under the VM
In reply to Cédric Le GoaterOn P9 sPAPR guests, the interrupt mode (XICS legacy or XIVE native) is determine at CAS time and the chosen mode is activated after a machine reset. To be able to switch from one mode to another, subsequent patches will introduce the capability to destroy the KVM device without destroying the VM.
This is not considered as a safe operation as the vCPUs are still running and could be referencing the KVM device through their presenters.
To protect the system from any breakage, the kvmppc_xive objects representing both KVM devices are now stored in an array under the VM. Allocation is performed on first usage and memory is freed only when the VM exits.
--- arch/powerpc/include/asm/kvm_host.h | 1 + arch/powerpc/kvm/book3s_xive.h | 1 + arch/powerpc/kvm/book3s_xive.c | 23 +++++++++++++++++++++-- arch/powerpc/kvm/book3s_xive_native.c | 9 +++++++-- arch/powerpc/kvm/powerpc.c | 6 ++++++ 5 files changed, 36 insertions(+), 4 deletions(-)
arch/powerpc/include/asm/kvm_host.h +1 -0
@@ -314,6 +314,7 @@ struct kvm_arch {#ifdef CONFIG_KVM_XICS struct kvmppc_xics *xics;struct kvmppc_xive *xive; +struct kvmppc_xive *xive_devices[2 ];struct kvmppc_passthru_irqmap *pimap;#endif struct kvmppc_ops *kvm_ops;
arch/powerpc/kvm/book3s_xive.h +1 -0
@@ -283,6 +283,7 @@ void kvmppc_xive_free_sources(struct kvmppc_xive_src_block *sb);int kvmppc_xive_select_target (struct kvm *kvm,u32 *server,u8 prio);int kvmppc_xive_attach_escalation (struct kvm_vcpu *vcpu,u8 prio,bool single_escalation); +struct kvmppc_xive *kvmppc_xive_get_device (struct kvm *kvm,u32 type);#endif /* CONFIG_KVM_XICS */ #endif /* _KVM_PPC_BOOK3S_XICS_H */
arch/powerpc/kvm/book3s_xive.c +21 -2
@@ -1846,11 +1846,30 @@ static void kvmppc_xive_free(struct kvm_device *dev)if (xive->vp_base !=XIVE_INVALID_VP )xive_native_free_vp_block (xive->vp_base); +/* +* A reference of the kvmppc_xive pointer is now kept under +* the xive_devices[] array of the machine for reuse. It is +* freed when the VM is destroyed. +*/ -kfree (xive);kfree (dev); } +struct kvmppc_xive *kvmppc_xive_get_device (struct kvm *kvm,u32 type) +{ +struct kvmppc_xive *xive; +bool xive_native_index = type =KVM_DEV_TYPE_XIVE ; + + xive = kvm->arch.xive_devices[xive_native_index]; + +if (!xive) { + xive =kzalloc (sizeof (*xive),GFP_KERNEL ); + kvm->arch.xive_devices[xive_native_index] = xive; + } + +return xive; +} +static int kvmppc_xive_create (struct kvm_device *dev,u32 type) {struct kvmppc_xive *xive;
@@ -1859,7 +1878,7 @@ static int kvmppc_xive_create(struct kvm_device *dev, u32 type)pr_devel ("Creating xive for partition\n" ); - xive =kzalloc (sizeof (*xive),GFP_KERNEL ); + xive =kvmppc_xive_get_device (kvm, type);if (!xive)return -ENOMEM ;
arch/powerpc/kvm/book3s_xive_native.c +7 -2
@@ -987,7 +987,12 @@ static void kvmppc_xive_native_free(struct kvm_device *dev)if (xive->vp_base !=XIVE_INVALID_VP )xive_native_free_vp_block (xive->vp_base); -kfree (xive); +/* +* A reference of the kvmppc_xive pointer is now kept under +* the xive_devices[] array of the machine for reuse. It is +* freed when the VM is destroyed. +*/ +kfree (dev); }
@@ -1002,7 +1007,7 @@ static int kvmppc_xive_native_create(struct kvm_device *dev, u32 type)if (kvm->arch.xive)return -EEXIST ; - xive =kzalloc (sizeof (*xive),GFP_KERNEL ); + xive =kvmppc_xive_get_device (kvm, type);if (!xive)return -ENOMEM ;
arch/powerpc/kvm/powerpc.c +6 -0
@@ -501,6 +501,12 @@ void kvm_arch_destroy_vm(struct kvm *kvm)mutex_unlock (&kvm->lock); +for (i =0 ; i <ARRAY_SIZE (kvm->arch.xive_devices); i++) { +struct kvmppc_xive *xive = kvm->arch.xive_devices[i]; +if (xive) +kfree (xive); + } +/* drop the module reference */ module_put (kvm->arch.kvm_ops->owner); }
Signature · 1 line
2.20.1
RFC PATCH v4 17/17 KVM: PPC: Book3S HV: XIVE: introduce a 'release' device operation
When the VM boots, the CAS negotiation process determines which interrupt mode to use and invokes a machine reset. At that time, any links to the previous KVM interrupt device should be 'destroyed' before the new chosen one is created.
To perform the necessary cleanups in KVM, we extend the KVM device interface with a new 'release' operation which is called when the file descriptor of the device is closed.
Such operations are defined for the XICS-on-XIVE and the XIVE native KVM devices. They clear the vCPU interrupt presenters that could be attached and then destroy the device.
--- include/linux/kvm_host.h | 1 + arch/powerpc/kvm/book3s_xive.c | 50 +++++++++++++++++++++++++-- arch/powerpc/kvm/book3s_xive_native.c | 23 ++++++++++++ virt/kvm/kvm_main.c | 13 +++++++ 4 files changed, 85 insertions(+), 2 deletions(-)
include/linux/kvm_host.h +1 -0
@@ -1246,6 +1246,7 @@ struct kvm_device_ops {long (*ioctl)(struct kvm_device *dev,unsigned int ioctl,unsigned long arg);int (*mmap)(struct kvm_device *dev,struct vm_area_struct *vma); +void (*release)(struct kvm_device *dev); };void kvm_device_get (struct kvm_device *dev);
arch/powerpc/kvm/book3s_xive.c +48 -2
84 lines not on this page · raw message
arch/powerpc/kvm/book3s_xive_native.c +23 -0
@@ -996,6 +996,28 @@ static void kvmppc_xive_native_free(struct kvm_device *dev)kfree (dev); } +static void kvmppc_xive_native_release (struct kvm_device *dev) +{ +struct kvmppc_xive *xive = dev->private; +struct kvm *kvm = xive->kvm; +struct kvm_vcpu *vcpu; +int i; + +pr_devel ("Releasing xive native device\n" ); + +/* +* When releasing the KVM device fd, the vCPUs can still be +* running and we should clean up the vCPU interrupt +* presenters first. +*/ +if (atomic_read (&kvm->online_vcpus) !=0 ) { +kvm_for_each_vcpu (i, vcpu, kvm) +kvmppc_xive_native_cleanup_vcpu (vcpu); + } + +kvmppc_xive_native_free (dev); +} +static int kvmppc_xive_native_create (struct kvm_device *dev,u32 type) {struct kvmppc_xive *xive;
@@ -1187,6 +1209,7 @@ struct kvm_device_ops kvm_xive_native_ops = { .name = "kvm-xive-native", .create = kvmppc_xive_native_create, .init = kvmppc_xive_native_init, + .release = kvmppc_xive_native_release, .destroy = kvmppc_xive_native_free, .set_attr = kvmppc_xive_native_set_attr, .get_attr = kvmppc_xive_native_get_attr,
virt/kvm/kvm_main.c +13 -0
@@ -2938,6 +2938,19 @@ static int kvm_device_release(struct inode *inode, struct file *filp)struct kvm_device *dev = filp->private_data;struct kvm *kvm = dev->kvm; +if (!dev) +return -ENODEV ; + +if (dev->kvm != kvm) +return -EPERM ; + +if (dev->ops->release) { +mutex_lock (&kvm->lock); +list_del (&dev->vm_node); + dev->ops->release (dev); +mutex_unlock (&kvm->lock); + } +kvm_put_kvm (kvm);return 0 ; }
Signature · 1 line
2.20.1
Re: [RFC PATCH v4 17/17] KVM: PPC: Book3S HV: XIVE: introduce a 'release' device operation
Quoted 87 lines · Cédric Le Goater
When the VM boots, the CAS negotiation process determines which interrupt mode to use and invokes a machine reset. At that time, any links to the previous KVM interrupt device should be 'destroyed' before the new chosen one is created.
To perform the necessary cleanups in KVM, we extend the KVM device interface with a new 'release' operation which is called when the file descriptor of the device is closed.
Such operations are defined for the XICS-on-XIVE and the XIVE native KVM devices. They clear the vCPU interrupt presenters that could be attached and then destroy the device.
Signed-off-by Cédric Le Goater <[email protected]>--- include/linux/kvm_host.h | 1 + arch/powerpc/kvm/book3s_xive.c | 50 +++++++++++++++++++++++++-- arch/powerpc/kvm/book3s_xive_native.c | 23 ++++++++++++ virt/kvm/kvm_main.c | 13 +++++++ 4 files changed, 85 insertions(+), 2 deletions(-)include/linux/kvm_host.h +1
-0@@ -1246,6 +1246,7 @@ struct kvm_device_ops {long (*ioctl)(struct kvm_device *dev,unsigned int ioctl,unsigned long arg);int (*mmap)(struct kvm_device *dev,struct vm_area_struct *vma); +void (*release)(struct kvm_device *dev); };void kvm_device_get (struct kvm_device *dev);arch/powerpc/kvm/book3s_xive.c +19
-2@@ -1100,11 +1100,19 @@ void kvmppc_xive_disable_vcpu_interrupts(struct kvm_vcpu *vcpu)void kvmppc_xive_cleanup_vcpu (struct kvm_vcpu *vcpu) {struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu; -struct kvmppc_xive *xive = xc->xive; +struct kvmppc_xive *xive;int i; +if (!kvmppc_xics_enabled (vcpu)) +return ; + +if (!xc) +return ; +pr_devel ("cleanup_vcpu(cpu=%d)\n" , xc->server_num); + xive = xc->xive; +/* Ensure no interrupt is still routed to that VP */ xc->valid = false;kvmppc_xive_disable_vcpu_interrupts (vcpu);@@ -1141,6 +1149,10 @@ void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu) }/* Free the VP */ kfree (xc); + +/* Cleanup the vcpu */ + vcpu->arch.irq_type =KVMPPC_IRQ_DEFAULT ; + vcpu->arch.xive_vcpu =NULL ; } int kvmppc_xive_connect_vcpu(struct kvm_device *dev,@@ -1158,7 +1170,7 @@ int kvmppc_xive_connect_vcpu(struct kvm_device *dev, }if (xive->kvm != vcpu->kvm)return -EPERM ; -if (vcpu->arch.irq_type) +if (vcpu->arch.irq_type !=KVMPPC_IRQ_DEFAULT )return -EBUSY ;if (kvmppc_xive_find_server (vcpu->kvm, cpu)) {pr_devel ("Duplicate !\n" );@@ -1855,6 +1867,39 @@ static void kvmppc_xive_free(struct kvm_device *dev)kfree (dev); } +static void kvmppc_xive_release (struct kvm_device *dev) +{ +struct kvmppc_xive *xive = dev->private; +struct kvm *kvm = xive->kvm; +struct kvm_vcpu *vcpu;
arch/powerpc/kvm/book3s_xive.c static void kvmppc_xive_free(struct kvm_device *dev) · line 1884
Quoted 10 lines
arch/powerpc/kvm/book3s_xive.c +10
-0+int i; + +pr_devel ("Releasing xive device\n" ); + +/* +* When releasing the KVM device fd, the vCPUs can still be +* running and we should clean up the vCPU interrupt +* presenters first. +*/ +if (atomic_read (&kvm->online_vcpus) !=0 ) {
What prevents online_vcpus from becoming non-zero after this test, but before the kvmppc_xive_free()?
Is the test actually necessary? The operations below should be safe even if there are no online cpus, yes?
arch/powerpc/kvm/book3s_xive.c static void kvmppc_xive_free(struct kvm_device *dev) · line 1895
Quoted 11 lines
arch/powerpc/kvm/book3s_xive.c +11
-0+/* +* call kick_all_cpus_sync() to ensure that all CPUs +* have executed any pending interrupts +*/ +if (is_kvmppc_hv_enabled (kvm)) +kick_all_cpus_sync (); + +/* +* TODO: There is still a race window with the early +* checks in kvmppc_native_connect_vcpu() +*/
That's... not reassuring. What are the consequences of that race, and what do you plan to do about it?
Quoted 31 lines
arch/powerpc/kvm/book3s_xive.c +8
-0+kvm_for_each_vcpu (i, vcpu, kvm) +kvmppc_xive_cleanup_vcpu (vcpu); + } + +kvmppc_xive_free (dev); +} +struct kvmppc_xive *kvmppc_xive_get_device (struct kvm *kvm,u32 type) {struct kvmppc_xive *xive;@@ -2043,6 +2088,7 @@ struct kvm_device_ops kvm_xive_ops = { .name = "kvm-xive", .create = kvmppc_xive_create, .init = kvmppc_xive_init, + .release = kvmppc_xive_release, .destroy = kvmppc_xive_free, .set_attr = xive_set_attr, .get_attr = xive_get_attr,arch/powerpc/kvm/book3s_xive_native.c +5
-0@@ -996,6 +996,28 @@ static void kvmppc_xive_native_free(struct kvm_device *dev)kfree (dev); } +static void kvmppc_xive_native_release (struct kvm_device *dev) +{ +struct kvmppc_xive *xive = dev->private; +struct kvm *kvm = xive->kvm; +struct kvm_vcpu *vcpu;
arch/powerpc/kvm/book3s_xive_native.c static void kvmppc_xive_native_free(struct kvm_device *dev) · line 1013
Quoted 10 lines
arch/powerpc/kvm/book3s_xive_native.c +10
-0+int i; + +pr_devel ("Releasing xive native device\n" ); + +/* +* When releasing the KVM device fd, the vCPUs can still be +* running and we should clean up the vCPU interrupt +* presenters first. +*/ +if (atomic_read (&kvm->online_vcpus) !=0 ) {
Likewise here.
Quoted 29 lines
arch/powerpc/kvm/book3s_xive_native.c +8
-0+kvm_for_each_vcpu (i, vcpu, kvm) +kvmppc_xive_native_cleanup_vcpu (vcpu); + } + +kvmppc_xive_native_free (dev); +} +static int kvmppc_xive_native_create (struct kvm_device *dev,u32 type) {struct kvmppc_xive *xive;@@ -1187,6 +1209,7 @@ struct kvm_device_ops kvm_xive_native_ops = { .name = "kvm-xive-native", .create = kvmppc_xive_native_create, .init = kvmppc_xive_native_init, + .release = kvmppc_xive_native_release, .destroy = kvmppc_xive_native_free, .set_attr = kvmppc_xive_native_set_attr, .get_attr = kvmppc_xive_native_get_attr,virt/kvm/kvm_main.c +3
-0@@ -2938,6 +2938,19 @@ static int kvm_device_release(struct inode *inode, struct file *filp)struct kvm_device *dev = filp->private_data;struct kvm *kvm = dev->kvm; +if (!dev) +return -ENODEV ; +
virt/kvm/kvm_main.c static int kvm_device_release(struct inode *inode, struct file *filp) · line 2953
Quoted 10 lines
virt/kvm/kvm_main.c +10
-0+if (dev->kvm != kvm) +return -EPERM ; + +if (dev->ops->release) { +mutex_lock (&kvm->lock); +list_del (&dev->vm_node); + dev->ops->release (dev); +mutex_unlock (&kvm->lock); + } +
Wasn't there a big comment that explained that release replaced destroy somewhere?
Quoted 3 lines
virt/kvm/kvm_main.c +0
-0kvm_put_kvm (kvm);return 0 ; }
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [RFC PATCH v4 17/17] KVM: PPC: Book3S HV: XIVE: introduce a 'release' device operation
Quoted 102 lines · David Gibson
Quoted 87 lines · Cédric Le Goater
When the VM boots, the CAS negotiation process determines which interrupt mode to use and invokes a machine reset. At that time, any links to the previous KVM interrupt device should be 'destroyed' before the new chosen one is created.
To perform the necessary cleanups in KVM, we extend the KVM device interface with a new 'release' operation which is called when the file descriptor of the device is closed.
Such operations are defined for the XICS-on-XIVE and the XIVE native KVM devices. They clear the vCPU interrupt presenters that could be attached and then destroy the device.
Signed-off-by Cédric Le Goater <[email protected]>--- include/linux/kvm_host.h | 1 + arch/powerpc/kvm/book3s_xive.c | 50 +++++++++++++++++++++++++-- arch/powerpc/kvm/book3s_xive_native.c | 23 ++++++++++++ virt/kvm/kvm_main.c | 13 +++++++ 4 files changed, 85 insertions(+), 2 deletions(-)include/linux/kvm_host.h +1
-0@@ -1246,6 +1246,7 @@ struct kvm_device_ops {long (*ioctl)(struct kvm_device *dev,unsigned int ioctl,unsigned long arg);int (*mmap)(struct kvm_device *dev,struct vm_area_struct *vma); +void (*release)(struct kvm_device *dev); };void kvm_device_get (struct kvm_device *dev);arch/powerpc/kvm/book3s_xive.c +19
-2@@ -1100,11 +1100,19 @@ void kvmppc_xive_disable_vcpu_interrupts(struct kvm_vcpu *vcpu)void kvmppc_xive_cleanup_vcpu (struct kvm_vcpu *vcpu) {struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu; -struct kvmppc_xive *xive = xc->xive; +struct kvmppc_xive *xive;int i; +if (!kvmppc_xics_enabled (vcpu)) +return ; + +if (!xc) +return ; +pr_devel ("cleanup_vcpu(cpu=%d)\n" , xc->server_num); + xive = xc->xive; +/* Ensure no interrupt is still routed to that VP */ xc->valid = false;kvmppc_xive_disable_vcpu_interrupts (vcpu);@@ -1141,6 +1149,10 @@ void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu) }/* Free the VP */ kfree (xc); + +/* Cleanup the vcpu */ + vcpu->arch.irq_type =KVMPPC_IRQ_DEFAULT ; + vcpu->arch.xive_vcpu =NULL ; } int kvmppc_xive_connect_vcpu(struct kvm_device *dev,@@ -1158,7 +1170,7 @@ int kvmppc_xive_connect_vcpu(struct kvm_device *dev, }if (xive->kvm != vcpu->kvm)return -EPERM ; -if (vcpu->arch.irq_type) +if (vcpu->arch.irq_type !=KVMPPC_IRQ_DEFAULT )return -EBUSY ;if (kvmppc_xive_find_server (vcpu->kvm, cpu)) {pr_devel ("Duplicate !\n" );@@ -1855,6 +1867,39 @@ static void kvmppc_xive_free(struct kvm_device *dev)kfree (dev); } +static void kvmppc_xive_release (struct kvm_device *dev) +{ +struct kvmppc_xive *xive = dev->private; +struct kvm *kvm = xive->kvm; +struct kvm_vcpu *vcpu;arch/powerpc/kvm/book3s_xive.c static void kvmppc_xive_free(struct kvm_device *dev) · line 1884
Quoted 10 lines
arch/powerpc/kvm/book3s_xive.c +10
-0+int i; + +pr_devel ("Releasing xive device\n" ); + +/* +* When releasing the KVM device fd, the vCPUs can still be +* running and we should clean up the vCPU interrupt +* presenters first. +*/ +if (atomic_read (&kvm->online_vcpus) !=0 ) {What prevents
online_vcpusfrom becoming non-zero after this test, but before thekvmppc_xive_free()?
Quoted 2 lines
Is the test actually necessary? The operations below should be safe even if there are no online cpus, yes?
Right... Similarly, the kick_all_cpus_sync() without anything having been done before it that we want the other vcpus to notice made me wonder what the point of it was. In other places where it is used we have done something such as set kvm->arch.mmu_ready to 0 first.
Quoted 6 lines
arch/powerpc/kvm/book3s_xive.c static void kvmppc_xive_free(struct kvm_device *dev) · line 1890
Quoted 6 lines
arch/powerpc/kvm/book3s_xive.c +6
-0+/* +* call kick_all_cpus_sync() to ensure that all CPUs +* have executed any pending interrupts +*/ +if (is_kvmppc_hv_enabled (kvm)) +kick_all_cpus_sync ();
Paul.
Re: [RFC PATCH v4 17/17] KVM: PPC: Book3S HV: XIVE: introduce a 'release' device operation
Quoted 103 lines · Paul Mackerras
Quoted 102 lines · David Gibson
Quoted 87 lines · Cédric Le Goater
When the VM boots, the CAS negotiation process determines which interrupt mode to use and invokes a machine reset. At that time, any links to the previous KVM interrupt device should be 'destroyed' before the new chosen one is created.
To perform the necessary cleanups in KVM, we extend the KVM device interface with a new 'release' operation which is called when the file descriptor of the device is closed.
Such operations are defined for the XICS-on-XIVE and the XIVE native KVM devices. They clear the vCPU interrupt presenters that could be attached and then destroy the device.
Signed-off-by Cédric Le Goater <[email protected]>--- include/linux/kvm_host.h | 1 + arch/powerpc/kvm/book3s_xive.c | 50 +++++++++++++++++++++++++-- arch/powerpc/kvm/book3s_xive_native.c | 23 ++++++++++++ virt/kvm/kvm_main.c | 13 +++++++ 4 files changed, 85 insertions(+), 2 deletions(-)include/linux/kvm_host.h +1
-0@@ -1246,6 +1246,7 @@ struct kvm_device_ops {long (*ioctl)(struct kvm_device *dev,unsigned int ioctl,unsigned long arg);int (*mmap)(struct kvm_device *dev,struct vm_area_struct *vma); +void (*release)(struct kvm_device *dev); };void kvm_device_get (struct kvm_device *dev);arch/powerpc/kvm/book3s_xive.c +19
-2@@ -1100,11 +1100,19 @@ void kvmppc_xive_disable_vcpu_interrupts(struct kvm_vcpu *vcpu)void kvmppc_xive_cleanup_vcpu (struct kvm_vcpu *vcpu) {struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu; -struct kvmppc_xive *xive = xc->xive; +struct kvmppc_xive *xive;int i; +if (!kvmppc_xics_enabled (vcpu)) +return ; + +if (!xc) +return ; +pr_devel ("cleanup_vcpu(cpu=%d)\n" , xc->server_num); + xive = xc->xive; +/* Ensure no interrupt is still routed to that VP */ xc->valid = false;kvmppc_xive_disable_vcpu_interrupts (vcpu);@@ -1141,6 +1149,10 @@ void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu) }/* Free the VP */ kfree (xc); + +/* Cleanup the vcpu */ + vcpu->arch.irq_type =KVMPPC_IRQ_DEFAULT ; + vcpu->arch.xive_vcpu =NULL ; } int kvmppc_xive_connect_vcpu(struct kvm_device *dev,@@ -1158,7 +1170,7 @@ int kvmppc_xive_connect_vcpu(struct kvm_device *dev, }if (xive->kvm != vcpu->kvm)return -EPERM ; -if (vcpu->arch.irq_type) +if (vcpu->arch.irq_type !=KVMPPC_IRQ_DEFAULT )return -EBUSY ;if (kvmppc_xive_find_server (vcpu->kvm, cpu)) {pr_devel ("Duplicate !\n" );@@ -1855,6 +1867,39 @@ static void kvmppc_xive_free(struct kvm_device *dev)kfree (dev); } +static void kvmppc_xive_release (struct kvm_device *dev) +{ +struct kvmppc_xive *xive = dev->private; +struct kvm *kvm = xive->kvm; +struct kvm_vcpu *vcpu;arch/powerpc/kvm/book3s_xive.c static void kvmppc_xive_free(struct kvm_device *dev) · line 1884
Quoted 10 lines
arch/powerpc/kvm/book3s_xive.c +10
-0+int i; + +pr_devel ("Releasing xive device\n" ); + +/* +* When releasing the KVM device fd, the vCPUs can still be +* running and we should clean up the vCPU interrupt +* presenters first. +*/ +if (atomic_read (&kvm->online_vcpus) !=0 ) {What prevents
online_vcpusfrom becoming non-zero after this test, but before thekvmppc_xive_free()?
Quoted 7 lines
Quoted 2 lines
Is the test actually necessary? The operations below should be safe even if there are no online cpus, yes?
Right... Similarly, the
kick_all_cpus_sync()without anything having been done before it that we want the other vcpus to notice made me wonder what the point of it was. In other places where it is used we have done something such as setkvm->arch.mmu_readyto 0 first.
This part is more dubious. It comes from my understanding of the routine kvm_arch_destroy_vm() that makes sure all IPIs have been handled before clearing the VCPUs structures. commit e17769eb8c89 is a bit cryptic and looks like an optimization that the release operation can ignore ?
Thanks,
C.
Quoted 9 lines
Quoted 6 lines
arch/powerpc/kvm/book3s_xive.c static void kvmppc_xive_free(struct kvm_device *dev) · line 1890
Quoted 6 lines
arch/powerpc/kvm/book3s_xive.c +6
-0+/* +* call kick_all_cpus_sync() to ensure that all CPUs +* have executed any pending interrupts +*/ +if (is_kvmppc_hv_enabled (kvm)) +kick_all_cpus_sync ();Paul.
Re: [RFC PATCH v4 17/17] KVM: PPC: Book3S HV: XIVE: introduce a 'release' device operation
In reply to David GibsonQuoted 93 lines · David Gibson
Quoted 92 lines · Cédric Le Goater
When the VM boots, the CAS negotiation process determines which interrupt mode to use and invokes a machine reset. At that time, any links to the previous KVM interrupt device should be 'destroyed' before the new chosen one is created.
To perform the necessary cleanups in KVM, we extend the KVM device interface with a new 'release' operation which is called when the file descriptor of the device is closed.
Such operations are defined for the XICS-on-XIVE and the XIVE native KVM devices. They clear the vCPU interrupt presenters that could be attached and then destroy the device.
Signed-off-by Cédric Le Goater <[email protected]>--- include/linux/kvm_host.h | 1 + arch/powerpc/kvm/book3s_xive.c | 50 +++++++++++++++++++++++++-- arch/powerpc/kvm/book3s_xive_native.c | 23 ++++++++++++ virt/kvm/kvm_main.c | 13 +++++++ 4 files changed, 85 insertions(+), 2 deletions(-)include/linux/kvm_host.h +1
-0@@ -1246,6 +1246,7 @@ struct kvm_device_ops {long (*ioctl)(struct kvm_device *dev,unsigned int ioctl,unsigned long arg);int (*mmap)(struct kvm_device *dev,struct vm_area_struct *vma); +void (*release)(struct kvm_device *dev); };void kvm_device_get (struct kvm_device *dev);arch/powerpc/kvm/book3s_xive.c +24
-2@@ -1100,11 +1100,19 @@ void kvmppc_xive_disable_vcpu_interrupts(struct kvm_vcpu *vcpu)void kvmppc_xive_cleanup_vcpu (struct kvm_vcpu *vcpu) {struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu; -struct kvmppc_xive *xive = xc->xive; +struct kvmppc_xive *xive;int i; +if (!kvmppc_xics_enabled (vcpu)) +return ; + +if (!xc) +return ; +pr_devel ("cleanup_vcpu(cpu=%d)\n" , xc->server_num); + xive = xc->xive; +/* Ensure no interrupt is still routed to that VP */ xc->valid = false;kvmppc_xive_disable_vcpu_interrupts (vcpu);@@ -1141,6 +1149,10 @@ void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu) }/* Free the VP */ kfree (xc); + +/* Cleanup the vcpu */ + vcpu->arch.irq_type =KVMPPC_IRQ_DEFAULT ; + vcpu->arch.xive_vcpu =NULL ; } int kvmppc_xive_connect_vcpu(struct kvm_device *dev,@@ -1158,7 +1170,7 @@ int kvmppc_xive_connect_vcpu(struct kvm_device *dev, }if (xive->kvm != vcpu->kvm)return -EPERM ; -if (vcpu->arch.irq_type) +if (vcpu->arch.irq_type !=KVMPPC_IRQ_DEFAULT )return -EBUSY ;if (kvmppc_xive_find_server (vcpu->kvm, cpu)) {pr_devel ("Duplicate !\n" );@@ -1855,6 +1867,39 @@ static void kvmppc_xive_free(struct kvm_device *dev)kfree (dev); } +static void kvmppc_xive_release (struct kvm_device *dev) +{ +struct kvmppc_xive *xive = dev->private; +struct kvm *kvm = xive->kvm; +struct kvm_vcpu *vcpu; +int i; + +pr_devel ("Releasing xive device\n" ); + +/*
Quoted 8 lines
arch/powerpc/kvm/book3s_xive.c static void kvmppc_xive_free(struct kvm_device *dev) · line 1884
Quoted 5 lines
arch/powerpc/kvm/book3s_xive.c +5
-0+* When releasing the KVM device fd, the vCPUs can still be +* running and we should clean up the vCPU interrupt +* presenters first. +*/ +if (atomic_read (&kvm->online_vcpus) !=0 ) {What prevents
online_vcpusfrom becoming non-zero after this test, but before thekvmppc_xive_free()?
I am not sure what you mean. kvmppc_xive_free() is gone with this patch. It has been replaced by kvmppc_xive_release().
Quoted 2 lines
Is the test actually necessary? The operations below should be safe even if there are no online cpus, yes?
ah, yes. kvm_for_each_vcpu() should be safe to use anyhow.
Quoted 11 lines
arch/powerpc/kvm/book3s_xive.c static void kvmppc_xive_free(struct kvm_device *dev) · line 1895
Quoted 9 lines
arch/powerpc/kvm/book3s_xive.c +9
-0+/* +* call kick_all_cpus_sync() to ensure that all CPUs +* have executed any pending interrupts +*/ +if (is_kvmppc_hv_enabled (kvm)) + kick_all_cpus_sync();>> + /* +* TODO: There is still a race window with the early +* checks in kvmppc_native_connect_vcpu() +*/ That's... not reassuring. What are the consequences of that race,
a bogus ->xive pointer under the XIVE vCPU
Quoted 1 line
and what do you plan to do about it?
I don't think this is true any more with the release operation which will be called by the last user of the device file.
Anyhow, xc->xive does not seem very useful (just like xc->valid) We should try to use only vcpu->kvm->arch.xive instead.
I will propose some preliminary cleanups before introducing the new release operation.
Quoted 70 lines
Quoted 31 lines
arch/powerpc/kvm/book3s_xive.c +8
-0+kvm_for_each_vcpu (i, vcpu, kvm) +kvmppc_xive_cleanup_vcpu (vcpu); + } + +kvmppc_xive_free (dev); +} +struct kvmppc_xive *kvmppc_xive_get_device (struct kvm *kvm,u32 type) {struct kvmppc_xive *xive;@@ -2043,6 +2088,7 @@ struct kvm_device_ops kvm_xive_ops = { .name = "kvm-xive", .create = kvmppc_xive_create, .init = kvmppc_xive_init, + .release = kvmppc_xive_release, .destroy = kvmppc_xive_free, .set_attr = xive_set_attr, .get_attr = xive_get_attr,arch/powerpc/kvm/book3s_xive_native.c +5
-0@@ -996,6 +996,28 @@ static void kvmppc_xive_native_free(struct kvm_device *dev)kfree (dev); } +static void kvmppc_xive_native_release (struct kvm_device *dev) +{ +struct kvmppc_xive *xive = dev->private; +struct kvm *kvm = xive->kvm; +struct kvm_vcpu *vcpu;arch/powerpc/kvm/book3s_xive_native.c static void kvmppc_xive_native_free(struct kvm_device *dev) · line 1013
Quoted 10 lines
arch/powerpc/kvm/book3s_xive_native.c +10
-0+int i; + +pr_devel ("Releasing xive native device\n" ); + +/* +* When releasing the KVM device fd, the vCPUs can still be +* running and we should clean up the vCPU interrupt +* presenters first. +*/ +if (atomic_read (&kvm->online_vcpus) !=0 ) {Likewise here.
Quoted 26 lines
arch/powerpc/kvm/book3s_xive_native.c +8
-0+kvm_for_each_vcpu (i, vcpu, kvm) +kvmppc_xive_native_cleanup_vcpu (vcpu); + } + +kvmppc_xive_native_free (dev); +} +static int kvmppc_xive_native_create (struct kvm_device *dev,u32 type) {struct kvmppc_xive *xive;@@ -1187,6 +1209,7 @@ struct kvm_device_ops kvm_xive_native_ops = { .name = "kvm-xive-native", .create = kvmppc_xive_native_create, .init = kvmppc_xive_native_init, + .release = kvmppc_xive_native_release, .destroy = kvmppc_xive_native_free, .set_attr = kvmppc_xive_native_set_attr, .get_attr = kvmppc_xive_native_get_attr,virt/kvm/kvm_main.c +0
-0@@ -2938,6 +2938,19 @@ static int kvm_device_release(struct inode *inode, struct file *filp)struct kvm_device *dev = filp->private_data;struct kvm *kvm = dev->kvm;
Quoted 16 lines
virt/kvm/kvm_main.c static int kvm_device_release(struct inode *inode, struct file *filp) · line 2953
Quoted 13 lines
virt/kvm/kvm_main.c +13
-0+if (!dev) +return -ENODEV ; + +if (dev->kvm != kvm) +return -EPERM ; + +if (dev->ops->release) { +mutex_lock (&kvm->lock); +list_del (&dev->vm_node); + dev->ops->release (dev); +mutex_unlock (&kvm->lock); + } +Wasn't there a big comment that explained that release replaced destroy somewhere?
Yes. I did add a comment in the "V5 errata" series.
I should be sending a v6 this week, to clarify all these attempts to solve the device switching.
Thanks,
C.
Quoted 5 lines
Quoted 4 lines
virt/kvm/kvm_main.c +0
-0kvm_put_kvm (kvm);return 0 ; }
Re: [RFC PATCH v4 17/17] KVM: PPC: Book3S HV: XIVE: introduce a 'release' device operation
Quoted 123 lines · Cédric Le Goater
Quoted 93 lines · David Gibson
Quoted 92 lines · Cédric Le Goater
When the VM boots, the CAS negotiation process determines which interrupt mode to use and invokes a machine reset. At that time, any links to the previous KVM interrupt device should be 'destroyed' before the new chosen one is created.
To perform the necessary cleanups in KVM, we extend the KVM device interface with a new 'release' operation which is called when the file descriptor of the device is closed.
Such operations are defined for the XICS-on-XIVE and the XIVE native KVM devices. They clear the vCPU interrupt presenters that could be attached and then destroy the device.
Signed-off-by Cédric Le Goater <[email protected]>--- include/linux/kvm_host.h | 1 + arch/powerpc/kvm/book3s_xive.c | 50 +++++++++++++++++++++++++-- arch/powerpc/kvm/book3s_xive_native.c | 23 ++++++++++++ virt/kvm/kvm_main.c | 13 +++++++ 4 files changed, 85 insertions(+), 2 deletions(-)include/linux/kvm_host.h +1
-0@@ -1246,6 +1246,7 @@ struct kvm_device_ops {long (*ioctl)(struct kvm_device *dev,unsigned int ioctl,unsigned long arg);int (*mmap)(struct kvm_device *dev,struct vm_area_struct *vma); +void (*release)(struct kvm_device *dev); };void kvm_device_get (struct kvm_device *dev);arch/powerpc/kvm/book3s_xive.c +24
-2@@ -1100,11 +1100,19 @@ void kvmppc_xive_disable_vcpu_interrupts(struct kvm_vcpu *vcpu)void kvmppc_xive_cleanup_vcpu (struct kvm_vcpu *vcpu) {struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu; -struct kvmppc_xive *xive = xc->xive; +struct kvmppc_xive *xive;int i; +if (!kvmppc_xics_enabled (vcpu)) +return ; + +if (!xc) +return ; +pr_devel ("cleanup_vcpu(cpu=%d)\n" , xc->server_num); + xive = xc->xive; +/* Ensure no interrupt is still routed to that VP */ xc->valid = false;kvmppc_xive_disable_vcpu_interrupts (vcpu);@@ -1141,6 +1149,10 @@ void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu) }/* Free the VP */ kfree (xc); + +/* Cleanup the vcpu */ + vcpu->arch.irq_type =KVMPPC_IRQ_DEFAULT ; + vcpu->arch.xive_vcpu =NULL ; } int kvmppc_xive_connect_vcpu(struct kvm_device *dev,@@ -1158,7 +1170,7 @@ int kvmppc_xive_connect_vcpu(struct kvm_device *dev, }if (xive->kvm != vcpu->kvm)return -EPERM ; -if (vcpu->arch.irq_type) +if (vcpu->arch.irq_type !=KVMPPC_IRQ_DEFAULT )return -EBUSY ;if (kvmppc_xive_find_server (vcpu->kvm, cpu)) {pr_devel ("Duplicate !\n" );@@ -1855,6 +1867,39 @@ static void kvmppc_xive_free(struct kvm_device *dev)kfree (dev); } +static void kvmppc_xive_release (struct kvm_device *dev) +{ +struct kvmppc_xive *xive = dev->private; +struct kvm *kvm = xive->kvm; +struct kvm_vcpu *vcpu; +int i; + +pr_devel ("Releasing xive device\n" ); + +/* Quoted 8 lines
arch/powerpc/kvm/book3s_xive.c static void kvmppc_xive_free(struct kvm_device *dev) · line 1884
Quoted 5 lines
arch/powerpc/kvm/book3s_xive.c +5
-0+* When releasing the KVM device fd, the vCPUs can still be +* running and we should clean up the vCPU interrupt +* presenters first. +*/ +if (atomic_read (&kvm->online_vcpus) !=0 ) {What prevents
online_vcpusfrom becoming non-zero after this test, but before thekvmppc_xive_free()?I am not sure what you mean.
kvmppc_xive_free()is gone with this patch. It has been replaced bykvmppc_xive_release().Quoted 2 lines
Is the test actually necessary? The operations below should be safe even if there are no online cpus, yes?
ah, yes.
kvm_for_each_vcpu()should be safe to use anyhow.Quoted 11 lines
arch/powerpc/kvm/book3s_xive.c static void kvmppc_xive_free(struct kvm_device *dev) · line 1895
Quoted 9 lines
arch/powerpc/kvm/book3s_xive.c +9
-0+/* +* call kick_all_cpus_sync() to ensure that all CPUs +* have executed any pending interrupts +*/ +if (is_kvmppc_hv_enabled (kvm)) + kick_all_cpus_sync();>> + /* +* TODO: There is still a race window with the early +* checks in kvmppc_native_connect_vcpu() +*/ That's... not reassuring. What are the consequences of that race,
Quoted 6 lines
a bogus ->xive pointer under the XIVE vCPU
Quoted 1 line
and what do you plan to do about it?
I don't think this is true any more with the release operation which will be called by the last user of the device file.
Ok, so the comment needs updating.
Quoted 108 lines
Anyhow,
xc->xivedoes not seem very useful (just likexc->valid) We should try to use onlyvcpu->kvm->arch.xiveinstead.I will propose some preliminary cleanups before introducing the new release operation.
Quoted 70 lines
Quoted 31 lines
arch/powerpc/kvm/book3s_xive.c +8
-0+kvm_for_each_vcpu (i, vcpu, kvm) +kvmppc_xive_cleanup_vcpu (vcpu); + } + +kvmppc_xive_free (dev); +} +struct kvmppc_xive *kvmppc_xive_get_device (struct kvm *kvm,u32 type) {struct kvmppc_xive *xive;@@ -2043,6 +2088,7 @@ struct kvm_device_ops kvm_xive_ops = { .name = "kvm-xive", .create = kvmppc_xive_create, .init = kvmppc_xive_init, + .release = kvmppc_xive_release, .destroy = kvmppc_xive_free, .set_attr = xive_set_attr, .get_attr = xive_get_attr,arch/powerpc/kvm/book3s_xive_native.c +5
-0@@ -996,6 +996,28 @@ static void kvmppc_xive_native_free(struct kvm_device *dev)kfree (dev); } +static void kvmppc_xive_native_release (struct kvm_device *dev) +{ +struct kvmppc_xive *xive = dev->private; +struct kvm *kvm = xive->kvm; +struct kvm_vcpu *vcpu;arch/powerpc/kvm/book3s_xive_native.c static void kvmppc_xive_native_free(struct kvm_device *dev) · line 1013
Quoted 10 lines
arch/powerpc/kvm/book3s_xive_native.c +10
-0+int i; + +pr_devel ("Releasing xive native device\n" ); + +/* +* When releasing the KVM device fd, the vCPUs can still be +* running and we should clean up the vCPU interrupt +* presenters first. +*/ +if (atomic_read (&kvm->online_vcpus) !=0 ) {Likewise here.
Quoted 26 lines
arch/powerpc/kvm/book3s_xive_native.c +8
-0+kvm_for_each_vcpu (i, vcpu, kvm) +kvmppc_xive_native_cleanup_vcpu (vcpu); + } + +kvmppc_xive_native_free (dev); +} +static int kvmppc_xive_native_create (struct kvm_device *dev,u32 type) {struct kvmppc_xive *xive;@@ -1187,6 +1209,7 @@ struct kvm_device_ops kvm_xive_native_ops = { .name = "kvm-xive-native", .create = kvmppc_xive_native_create, .init = kvmppc_xive_native_init, + .release = kvmppc_xive_native_release, .destroy = kvmppc_xive_native_free, .set_attr = kvmppc_xive_native_set_attr, .get_attr = kvmppc_xive_native_get_attr,virt/kvm/kvm_main.c +0
-0@@ -2938,6 +2938,19 @@ static int kvm_device_release(struct inode *inode, struct file *filp)struct kvm_device *dev = filp->private_data;struct kvm *kvm = dev->kvm;Quoted 16 lines
virt/kvm/kvm_main.c static int kvm_device_release(struct inode *inode, struct file *filp) · line 2953
Quoted 13 lines
virt/kvm/kvm_main.c +13
-0+if (!dev) +return -ENODEV ; + +if (dev->kvm != kvm) +return -EPERM ; + +if (dev->ops->release) { +mutex_lock (&kvm->lock); +list_del (&dev->vm_node); + dev->ops->release (dev); +mutex_unlock (&kvm->lock); + } +Wasn't there a big comment that explained that release replaced destroy somewhere?
Yes. I did add a comment in the "V5 errata" series.
I should be sending a v6 this week, to clarify all these attempts to solve the device switching.
Thanks,
C.
Quoted 5 lines
Quoted 4 lines
virt/kvm/kvm_main.c +0
-0kvm_put_kvm (kvm);return 0 ; }
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
Re: [RFC PATCH v4.1 16/17] KVM: PPC: Book3S HV: XIVE: introduce a xive_devices array under the VM
In reply to Cédric Le GoaterQuoted 10 lines · Cédric Le Goater
On P9 sPAPR guests, the interrupt mode (XICS legacy or XIVE native) is determine at CAS time and the chosen mode is activated after a machine reset. To be able to switch from one mode to another, subsequent patches will introduce the capability to destroy the KVM device without destroying the VM.
This is not considered as a safe operation as the vCPUs are still running and could be referencing the KVM device through their presenters.
Quoted 6 lines
To protect the system from any breakage, the
kvmppc_xiveobjects representing both KVM devices are now stored in an array under the VM. Allocation is performed on first usage and memory is freed only when the VM exits.Signed-off-by Cédric Le Goater <[email protected]>
Although...
Quoted 13 lines
--- arch/powerpc/include/asm/kvm_host.h | 1 + arch/powerpc/kvm/book3s_xive.h | 1 + arch/powerpc/kvm/book3s_xive.c | 23 +++++++++++++++++++++-- arch/powerpc/kvm/book3s_xive_native.c | 9 +++++++-- arch/powerpc/kvm/powerpc.c | 6 ++++++ 5 files changed, 36 insertions(+), 4 deletions(-)arch/powerpc/include/asm/kvm_host.h +0
-0
arch/powerpc/include/asm/kvm_host.h struct kvm_arch { · line 317
Quoted 4 lines
arch/powerpc/include/asm/kvm_host.h +1
-0@@ -314,6 +314,7 @@ struct kvm_arch {#ifdef CONFIG_KVM_XICS struct kvmppc_xics *xics;struct kvmppc_xive *xive; +struct kvmppc_xive *xive_devices[2 ];
... as noted on the previous revision, I feel this use of a bool-indexed array instead of separate fields makes things more confusing than necessary, for a negligible reduction in code size.
Quoted 104 lines
arch/powerpc/include/asm/kvm_host.h +0
-0struct kvmppc_passthru_irqmap *pimap;#endif struct kvmppc_ops *kvm_ops;arch/powerpc/kvm/book3s_xive.h +1
-0@@ -283,6 +283,7 @@ void kvmppc_xive_free_sources(struct kvmppc_xive_src_block *sb);int kvmppc_xive_select_target (struct kvm *kvm,u32 *server,u8 prio);int kvmppc_xive_attach_escalation (struct kvm_vcpu *vcpu,u8 prio,bool single_escalation); +struct kvmppc_xive *kvmppc_xive_get_device (struct kvm *kvm,u32 type);#endif /* CONFIG_KVM_XICS */ #endif /* _KVM_PPC_BOOK3S_XICS_H */ arch/powerpc/kvm/book3s_xive.c +21
-2@@ -1846,11 +1846,30 @@ static void kvmppc_xive_free(struct kvm_device *dev)if (xive->vp_base !=XIVE_INVALID_VP )xive_native_free_vp_block (xive->vp_base); +/* +* A reference of the kvmppc_xive pointer is now kept under +* the xive_devices[] array of the machine for reuse. It is +* freed when the VM is destroyed. +*/ -kfree (xive);kfree (dev); } +struct kvmppc_xive *kvmppc_xive_get_device (struct kvm *kvm,u32 type) +{ +struct kvmppc_xive *xive; +bool xive_native_index = type ==KVM_DEV_TYPE_XIVE ; + + xive = kvm->arch.xive_devices[xive_native_index]; + +if (!xive) { + xive =kzalloc (sizeof (*xive),GFP_KERNEL ); + kvm->arch.xive_devices[xive_native_index] = xive; + } + +return xive; +} +static int kvmppc_xive_create (struct kvm_device *dev,u32 type) {struct kvmppc_xive *xive;@@ -1859,7 +1878,7 @@ static int kvmppc_xive_create(struct kvm_device *dev, u32 type)pr_devel ("Creating xive for partition\n" ); - xive =kzalloc (sizeof (*xive),GFP_KERNEL ); + xive =kvmppc_xive_get_device (kvm, type);if (!xive)return -ENOMEM ;arch/powerpc/kvm/book3s_xive_native.c +7
-2@@ -987,7 +987,12 @@ static void kvmppc_xive_native_free(struct kvm_device *dev)if (xive->vp_base !=XIVE_INVALID_VP )xive_native_free_vp_block (xive->vp_base); -kfree (xive); +/* +* A reference of the kvmppc_xive pointer is now kept under +* the xive_devices[] array of the machine for reuse. It is +* freed when the VM is destroyed. +*/ +kfree (dev); }@@ -1002,7 +1007,7 @@ static int kvmppc_xive_native_create(struct kvm_device *dev, u32 type)if (kvm->arch.xive)return -EEXIST ; - xive =kzalloc (sizeof (*xive),GFP_KERNEL ); + xive =kvmppc_xive_get_device (kvm, type);if (!xive)return -ENOMEM ;arch/powerpc/kvm/powerpc.c +6
-0@@ -501,6 +501,12 @@ void kvm_arch_destroy_vm(struct kvm *kvm)mutex_unlock (&kvm->lock); +for (i =0 ; i <ARRAY_SIZE (kvm->arch.xive_devices); i++) { +struct kvmppc_xive *xive = kvm->arch.xive_devices[i]; +if (xive) +kfree (xive); + } +/* drop the module reference */ module_put (kvm->arch.kvm_ops->owner); }
Signature · 4 lines
David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson
PATCH v4 17/17 KVM: PPC: Book3S HV: XIVE: clear the vCPU interrupt presenters
In reply to Cédric Le GoaterWhen the VM boots, the CAS negotiation process determines which interrupt mode to use and invokes a machine reset. At that time, the previous KVM interrupt device is 'destroyed' before the chosen one is created. Upon destruction, the vCPU interrupt presenters using the KVM device should be cleared first, the machine will reconnect them later to the new device after it is created.
---
Changes since v2 :
- removed comments on possible race in kvmppc_native_connect_vcpu() for the XIVE KVM device. This is still an issue in the XICS-over-XIVE device.
arch/powerpc/kvm/book3s_xics.c | 19 +++++++++++++ arch/powerpc/kvm/book3s_xive.c | 39 +++++++++++++++++++++++++-- arch/powerpc/kvm/book3s_xive_native.c | 12 +++++++++ 3 files changed, 68 insertions(+), 2 deletions(-)
arch/powerpc/kvm/book3s_xics.c +19 -0
@@ -1342,6 +1342,25 @@ static void kvmppc_xics_free(struct kvm_device *dev)struct kvmppc_xics *xics = dev->private;int i;struct kvm *kvm = xics->kvm; +struct kvm_vcpu *vcpu; + +/* +* When destroying the VM, the vCPUs are destroyed first and +* the vCPU list should be empty. If this is not the case, +* then we are simply destroying the device and we should +* clean up the vCPU interrupt presenters first. +*/ +if (atomic_read (&kvm->online_vcpus) !=0 ) { +/* +* call kick_all_cpus_sync() to ensure that all CPUs +* have executed any pending interrupts +*/ +if (is_kvmppc_hv_enabled (kvm)) +kick_all_cpus_sync (); + +kvm_for_each_vcpu (i, vcpu, kvm) +kvmppc_xics_free_icp (vcpu); + }debugfs_remove (xics->dentry);
arch/powerpc/kvm/book3s_xive.c +37 -2
@@ -1100,11 +1100,19 @@ void kvmppc_xive_disable_vcpu_interrupts(struct kvm_vcpu *vcpu)void kvmppc_xive_cleanup_vcpu (struct kvm_vcpu *vcpu) {struct kvmppc_xive_vcpu *xc = vcpu->arch.xive_vcpu; -struct kvmppc_xive *xive = xc->xive; +struct kvmppc_xive *xive;int i; +if (!kvmppc_xics_enabled (vcpu)) +return ; + +if (!xc) +return ; +pr_devel ("cleanup_vcpu(cpu=%d)\n" , xc->server_num); + xive = xc->xive; +/* Ensure no interrupt is still routed to that VP */ xc->valid = false;kvmppc_xive_disable_vcpu_interrupts (vcpu);
@@ -1141,6 +1149,10 @@ void kvmppc_xive_cleanup_vcpu(struct kvm_vcpu *vcpu) }/* Free the VP */ kfree (xc); + +/* Cleanup the vcpu */ + vcpu->arch.irq_type =KVMPPC_IRQ_DEFAULT ; + vcpu->arch.xive_vcpu =NULL ; } int kvmppc_xive_connect_vcpu(struct kvm_device *dev,
@@ -1158,7 +1170,7 @@ int kvmppc_xive_connect_vcpu(struct kvm_device *dev, }if (xive->kvm != vcpu->kvm)return -EPERM ; -if (vcpu->arch.irq_type) +if (vcpu->arch.irq_type !=KVMPPC_IRQ_DEFAULT )return -EBUSY ;if (kvmppc_xive_find_server (vcpu->kvm, cpu)) {pr_devel ("Duplicate !\n" );
@@ -1828,8 +1840,31 @@ static void kvmppc_xive_free(struct kvm_device *dev) {struct kvmppc_xive *xive = dev->private;struct kvm *kvm = xive->kvm; +struct kvm_vcpu *vcpu;int i; +/* +* When destroying the VM, the vCPUs are destroyed first and +* the vCPU list should be empty. If this is not the case, +* then we are simply destroying the device and we should +* clean up the vCPU interrupt presenters first. +*/ +if (atomic_read (&kvm->online_vcpus) !=0 ) { +/* +* call kick_all_cpus_sync() to ensure that all CPUs +* have executed any pending interrupts +*/ +if (is_kvmppc_hv_enabled (kvm)) +kick_all_cpus_sync (); + +/* +* TODO: There is still a race window with the early +* checks in kvmppc_native_connect_vcpu() +*/ +kvm_for_each_vcpu (i, vcpu, kvm) +kvmppc_xive_cleanup_vcpu (vcpu); + } +debugfs_remove (xive->dentry);if (kvm)
arch/powerpc/kvm/book3s_xive_native.c +12 -0
@@ -961,8 +961,20 @@ static void kvmppc_xive_native_free(struct kvm_device *dev) {struct kvmppc_xive *xive = dev->private;struct kvm *kvm = xive->kvm; +struct kvm_vcpu *vcpu;int i; +/* +* When destroying the VM, the vCPUs are destroyed first and +* the vCPU list should be empty. If this is not the case, +* then we are simply destroying the device and we should +* clean up the vCPU interrupt presenters first. +*/ +if (atomic_read (&kvm->online_vcpus) !=0 ) { +kvm_for_each_vcpu (i, vcpu, kvm) +kvmppc_xive_native_cleanup_vcpu (vcpu); + } +debugfs_remove (xive->dentry);pr_devel ("Destroying xive native device\n" );
Signature · 1 line
2.20.1