Batch register access for live migration optimization
cover Batch register access for live migration optimization
This series adds batch register access support to QEMU's KVM GICv3 register synchronization path, reducing syscall overhead during ARM64 live migration.
Currently, QEMU issues one ioctl per register when saving/restoring VGIC state. On large VM configurations this means tens of thousands of syscalls, where lock acquisition and context switch overhead dominates migration downtime. Thus, we provide a batch register method to allow userspace read/write multiple distributor and redistributor registers in a single call. In this way, we can significantly reduce syscalls and migration downtime.
All batch paths probe kernel capability at runtime and fall back to the original one-by-one path when the kernel does not support batch access, ensuring backward compatibility.
This series depends on the corresponding kernel patches that add KVM_DEV_ARM_VGIC_GRP_BATCH_REGS support.
Test the VM migration time under pressure conditions. The VM specifications for migration are as follows:
- VM use 4-K page;
- the number of VCPU is 160;
- the total memory is 320Gigabit;
- use 'Redis SET-benchmark' to pressurize VM;
Performance results (3-run average, ms):
| Metric | Without patch | With patch | Improvement |
|---------------------|---------------|------------|-------------|
| Migration downtime | 536 | 321 | 40% |
| Source (total) | 344 | 230 | 33% |
| - VGIC put | 158 | 40 | 75% |
| - VGIC get | 120 | 19 | 84% |
| Destination (total) | 192 | 91 | 53% |
| - VGIC put | 132 | 27 | 80% |Yize Wang (4):
Sync KVM ARM VGIC batch register UAPI to QEMU headers hw/intc/arm_gicv3_kvm: Add batch helper structures and functions hw/intc/arm_gicv3_kvm: Refactor kvm_arm_gicv3_put for batch register access hw/intc/arm_gicv3_kvm: Refactor kvm_arm_gicv3_get for batch register access
hw/intc/arm_gicv3_kvm.c | 1087 ++++++++++++++++++++++++++++++++++------- linux-headers/asm-arm64/kvm.h | 9 + 2 files changed, 925 insertions(+), 171 deletions(-)
References:
[1] [PATCH 0/2] Batch register access for live migration optimization
https://lore.kernel.org/kvmarm/[email protected]/T/#t
PATCH 1/4 Sync KVM ARM VGIC batch register UAPI to QEMU headers
Add KVM_DEV_ARM_VGIC_GRP_BATCH_REGS, KVM_DEV_ARM_VGIC_BATCH_MAX, and kvm_dev_arm_vgic_batch_entry struct to arm64 QEMU vendored kernel headers.
--- linux-headers/asm-arm64/kvm.h | 9 +++++++++ 1 file changed, 9 insertions(+)
linux-headers/asm-arm64/kvm.h +9 -0
@@ -405,11 +405,20 @@ enum {#define KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO 7#define KVM_DEV_ARM_VGIC_GRP_ITS_REGS 8#define KVM_DEV_ARM_VGIC_GRP_MAINT_IRQ 9 +#define KVM_DEV_ARM_VGIC_GRP_BATCH_REGS 10#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT 10#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_MASK \ (0x3fffffULL << KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT)#define KVM_DEV_ARM_VGIC_LINE_LEVEL_INTID_MASK 0x3ff#define VGIC_LEVEL_INFO_LINE_LEVEL 0 +#define KVM_DEV_ARM_VGIC_BATCH_MAX 4096 + +struct kvm_dev_arm_vgic_batch_entry { +__u32 group; +__u64 attr; +__u32 val; +__s32 error; +};#define KVM_DEV_ARM_VGIC_CTRL_INIT 0#define KVM_DEV_ARM_ITS_SAVE_TABLES 1
Signature · 1 line
2.43.0
PATCH 2/4 hw/intc/arm_gicv3_kvm: Add batch helper structures and functions
In reply to Yize WangAdd GICv3BatchEntry typedef and three helper functions for batch register access to KVM ARM64 VGICv3:
- kvm_arm_gic_batch_add: Accumulates register operations into a dynamic array
- kvm_arm_gic_batch_commit: Submits the batch array to kernel via a single ioctl
- kvm_arm_gic_batch_supported: Probes kernel support for the batch interface
These helpers will be used by kvm_arm_gicv3_get and kvm_arm_gicv3_put in subsequent patches to enable efficient batch register access.
--- hw/intc/arm_gicv3_kvm.c | 95 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+)
hw/intc/arm_gicv3_kvm.c +95 -0
@@ -118,6 +118,101 @@ static inline void kvm_gic_line_level_access(GICv3State *s, int irq, int cpu, val, write, &error_abort); } +typedef struct { +uint32_t group; +uint64_t attr; +uint32_t *val; +}GICv3BatchEntry ; + +/* Accumulates register operations into a dynamic array. */ +static void kvm_arm_gic_batch_add (GICv3BatchEntry **entries,int *n, +int *capacity,uint32_t group, +uint64_t attr,uint32_t *val) +{ +if (*n >= *capacity) { + *capacity =MAX (*capacity *2 ,256 ); + *entries =g_renew (GICv3BatchEntry, *entries, *capacity); + } + (*entries)[*n].group = group; + (*entries)[*n].attr = attr; +/* Store the pointer, not the value, to avoid copying data now. */ + (*entries)[*n].val = val; + (*n)++; +} + +/* +* Submit the batched GIC register operations to the kernel via a single ioctl. +* +* Workflow: +* 1. Allocate a kernel-compatible structure array +* (kvm_dev_arm_vgic_batch_entry). +* 2. Convert QEMU's internal format to the kernel's expected format. +* - For WRITE: Copy the actual values from QEMU's buffers into the +* kernel struct. +* - For READ: Only set the register IDs; the kernel will fill in +* the values. +* 3. Perform a single ioctl() to send/receive all data at once. +* 4. For READ: Copy the values returned by the kernel back into QEMU's +* buffers. +*/ +static void kvm_arm_gic_batch_commit (int dev_fd,GICv3BatchEntry *entries, +int n,bool write) +{ +struct kvm_dev_arm_vgic_batch_entry *batch; +struct kvm_device_attr attr; +int i, ret; + +if (!n) { +return ; + } + + batch = g_new0(struct kvm_dev_arm_vgic_batch_entry, n); +for (i =0 ; i < n; i++) { + batch[i].group = entries[i].group; + batch[i].attr = entries[i].attr; +if (write) { + batch[i].val = *entries[i].val; + } + } + + attr.group =KVM_DEV_ARM_VGIC_GRP_BATCH_REGS ; + attr.addr = (uint64_t )batch; + attr.attr = n; + +if (write) { + ret =kvm_device_ioctl (dev_fd,KVM_SET_DEVICE_ATTR , &attr); + }else { + ret =kvm_device_ioctl (dev_fd,KVM_GET_DEVICE_ATTR , &attr); + } + +if (ret <0 ) { +error_report ("KVM_%s_DEVICE_ATTR failed on batch regs: %s" , + write ?"SET" :"GET" ,strerror (-ret)); +abort (); + } + +if (!write) { +for (i =0 ; i < n; i++) { + *entries[i].val = batch[i].val; + } + } + +g_free (batch); +} + +/* +* Check if the kernel supports GIC batch register operations. +* Returns: true if the kernel supports the BATCH_REGS group, +* false otherwise. +*/ +static bool kvm_arm_gic_batch_supported (int dev_fd) +{ +struct kvm_device_attr attr = { + .group =KVM_DEV_ARM_VGIC_GRP_BATCH_REGS , + }; +return kvm_device_ioctl (dev_fd,KVM_HAS_DEVICE_ATTR , &attr) ==0 ; +} +/* Loop through each distributor IRQ related register; since bits * corresponding to SPIs and PPIs are RAZ/WI when affinity routing * is enabled, we skip those.
Signature · 1 line
2.43.0
PATCH 3/4 hw/intc/arm_gicv3_kvm: Refactor kvm_arm_gicv3_put for batch register access
In reply to Yize WangRefactor kvm_arm_gicv3_put and add auxiliary functions to implement batch register access.
Auxiliary functions:
-kvm_arm_gic_batch_write_plpis: batch write to LPI -kvm_arm_gic_batch_write_redist: batch write to Redistributor -kvm_arm_gic_batch_write_cpuif: batch write to CPU Interface
--- hw/intc/arm_gicv3_kvm.c | 524 ++++++++++++++++++++++++++++++++-------- 1 file changed, 425 insertions(+), 99 deletions(-)
hw/intc/arm_gicv3_kvm.c +425 -99
@@ -34,6 +34,7 @@#include "migration/misc.h" #include "qom/object.h" #include "target/arm/cpregs.h" +#include "exec/cpu-defs.h" #define TYPE_KVM_ARM_GICV3 "kvm-arm-gicv3"
@@ -409,11 +410,298 @@ static void kvm_arm_gicv3_check(GICv3State *s) } } +/* Write to the LPI base address registers in batches */ +static void kvm_arm_gic_batch_write_plpis (GICv3State *s, +GICv3BatchEntry **entries, +uint64_t redist_typer, +int *batch_n, +int *batch_capacity) +{ +if (!(redist_typer &GICR_TYPER_PLPIS )) { +return ; + } + +/* +* Allocate temporary buffers for the LPI base addresses +* of all CPUs. +*/ +uint32_t *plpis =g_new (uint32_t, s->num_cpu *4 ); + +for (int ncpu =0 ; ncpu < s->num_cpu; ncpu++) { +GICv3CPUState *c = &s->cpu[ncpu]; +int base = ncpu *4 ; + +/* Split the 64-bit base address into two 32-bit values. */ + plpis[base] = (uint32_t )c->gicr_propbaser; + plpis[base +1 ] = (uint32_t )(c->gicr_propbaser >>32 ); + plpis[base +2 ] = (uint32_t )c->gicr_pendbaser; + plpis[base +3 ] = (uint32_t )(c->gicr_pendbaser >>32 ); + +/* Add to batch queueing */ +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_PROPBASER , c->gicr_typer), + &plpis[base]); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_PROPBASER +4 , c->gicr_typer), + &plpis[base +1 ]); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_PENDBASER , c->gicr_typer), + &plpis[base +2 ]); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_PENDBASER +4 , c->gicr_typer), + &plpis[base +3 ]); + } + +/* +* Submit the LPI base address immediately. +* Note: After submission, the caller is responsible for resetting batch_n +* to reuse the entries array. +*/ +kvm_arm_gic_batch_commit (s->dev_fd, *entries, *batch_n, true); +g_free (plpis); +} + +/* Write to the Redistributor common registers in batches */ +static void kvm_arm_gic_batch_write_redist (GICv3State *s, +GICv3BatchEntry **entries, +int *batch_n, +int *batch_capacity) +{ +uint32_t clear_all = ~0 ; +uint32_t *redist_icfgr1 =g_new (uint32_t, s->num_cpu); +uint32_t *redist_priority =g_new (uint32_t, + s->num_cpu * (GIC_INTERNAL /4 )); + +for (int ncpu =0 ; ncpu < s->num_cpu; ncpu++) { +GICv3CPUState *c = &s->cpu[ncpu]; +int pri_base = ncpu * (GIC_INTERNAL /4 ); + + redist_icfgr1[ncpu] =half_shuffle32 (c->edge_trigger >>16 ) <<1 ; + +/* control and status register */ +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_CTLR , c->gicr_typer), + &c->gicr_ctlr); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_STATUSR , c->gicr_typer), + &c->gicr_statusr[GICV3_NS ]); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_WAKER , c->gicr_typer), + &c->gicr_waker); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_IGROUPR0 , c->gicr_typer), + &c->gicr_igroupr0); + +/* Enable register (clear-set mode) */ +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_ICENABLER0 , c->gicr_typer), + &clear_all); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_ISENABLER0 , c->gicr_typer), + &c->gicr_ienabler0); + +/* Configure register */ +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_ICFGR1 , c->gicr_typer), + &redist_icfgr1[ncpu]); + +/* Level information */ +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO , +KVM_VGIC_ATTR (0 , c->gicr_typer) | + (VGIC_LEVEL_INFO_LINE_LEVEL << +KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT ), + &c->level); + +/* Suspend register (Clear-Set mode) */ +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_ICPENDR0 , c->gicr_typer), + &clear_all); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_ISPENDR0 , c->gicr_typer), + &c->gicr_ipendr0); + +/* Activation register (Clear-Set mode) */ +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_ICACTIVER0 , c->gicr_typer), + &clear_all); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_ISACTIVER0 , c->gicr_typer), + &c->gicr_iactiver0); + +/* Priority register (written in packet mode) */ +for (int i =0 ; i <GIC_INTERNAL ; i +=4 ) { + redist_priority[pri_base + i /4 ] = + c->gicr_ipriorityr[i] | + (c->gicr_ipriorityr[i +1 ] <<8 ) | + (c->gicr_ipriorityr[i +2 ] <<16 ) | + (c->gicr_ipriorityr[i +3 ] <<24 ); + +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_IPRIORITYR + i, + c->gicr_typer), + &redist_priority[pri_base + i /4 ]); + } + } + +/* Submit all Redistributor statuses */ +kvm_arm_gic_batch_commit (s->dev_fd, *entries, *batch_n, true); + +g_free (redist_icfgr1); +g_free (redist_priority); +} + +/* Batch Write to CPU Interface */ +static void kvm_arm_gic_batch_write_cpuif (GICv3State *s, +GICv3BatchEntry **entries, +int *batch_n, +int *batch_capacity) +{ +uint32_t *cpuif_sre =g_new0 (uint32_t, s->num_cpu); +uint32_t *cpuif_ctlr =g_new0 (uint32_t, s->num_cpu); +uint32_t *cpuif_igrpen0 =g_new0 (uint32_t, s->num_cpu); +uint32_t *cpuif_igrpen1 =g_new0 (uint32_t, s->num_cpu); +uint32_t *cpuif_pmr =g_new0 (uint32_t, s->num_cpu); +uint32_t *cpuif_bpr0 =g_new0 (uint32_t, s->num_cpu); +uint32_t *cpuif_bpr1 =g_new0 (uint32_t, s->num_cpu); + +for (int ncpu =0 ; ncpu < s->num_cpu; ncpu++) { +GICv3CPUState *c = &s->cpu[ncpu]; +int num_pri_bits; + +/* Basic system registers */ + cpuif_sre[ncpu] = c->icc_sre_el1; +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_SRE_EL1 , c->gicr_typer), + &cpuif_sre[ncpu]); + + cpuif_ctlr[ncpu] = c->icc_ctlr_el1[GICV3_NS ]; +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_CTLR_EL1 , c->gicr_typer), + &cpuif_ctlr[ncpu]); + + cpuif_igrpen0[ncpu] = c->icc_igrpen[GICV3_G0 ]; +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_IGRPEN0_EL1 , c->gicr_typer), + &cpuif_igrpen0[ncpu]); + + cpuif_igrpen1[ncpu] = c->icc_igrpen[GICV3_G1NS ]; +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_IGRPEN1_EL1 , c->gicr_typer), + &cpuif_igrpen1[ncpu]); + + cpuif_pmr[ncpu] = c->icc_pmr_el1; +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_PMR_EL1 , c->gicr_typer), + &cpuif_pmr[ncpu]); + + cpuif_bpr0[ncpu] = c->icc_bpr[GICV3_G0 ]; +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_BPR0_EL1 , c->gicr_typer), + &cpuif_bpr0[ncpu]); + + cpuif_bpr1[ncpu] = c->icc_bpr[GICV3_G1NS ]; +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_BPR1_EL1 , c->gicr_typer), + &cpuif_bpr1[ncpu]); + +/* Calculate the number of priority bits */ + num_pri_bits = ((c->icc_ctlr_el1[GICV3_NS ] & +ICC_CTLR_EL1_PRIBITS_MASK ) >> +ICC_CTLR_EL1_PRIBITS_SHIFT ) +1 ; + +/* Group 0 APR */ +switch (num_pri_bits) { +case 7 : +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_AP0R_EL1 (3 ), c->gicr_typer), + (uint32_t *)&c->icc_apr[GICV3_G0 ][3 ]); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_AP0R_EL1 (2 ), c->gicr_typer), + (uint32_t *)&c->icc_apr[GICV3_G0 ][2 ]); +/* fall through */ +case 6 : +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_AP0R_EL1 (1 ), c->gicr_typer), + (uint32_t *)&c->icc_apr[GICV3_G0 ][1 ]); +/* fall through */ +default : +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_AP0R_EL1 (0 ), c->gicr_typer), + (uint32_t *)&c->icc_apr[GICV3_G0 ][0 ]); + } + +/* Group 1 APR */ +switch (num_pri_bits) { +case 7 : +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_AP1R_EL1 (3 ), c->gicr_typer), + (uint32_t *)&c->icc_apr[GICV3_G1NS ][3 ]); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_AP1R_EL1 (2 ), c->gicr_typer), + (uint32_t *)&c->icc_apr[GICV3_G1NS ][2 ]); +/* fall through */ +case 6 : +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_AP1R_EL1 (1 ), c->gicr_typer), + (uint32_t *)&c->icc_apr[GICV3_G1NS ][1 ]); +/* fall through */ +default : +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_AP1R_EL1 (0 ), c->gicr_typer), + (uint32_t *)&c->icc_apr[GICV3_G1NS ][0 ]); + } + } + +/* Submit all CPU Interface statuses */ +kvm_arm_gic_batch_commit (s->dev_fd, *entries, *batch_n, true); + +g_free (cpuif_sre); +g_free (cpuif_ctlr); +g_free (cpuif_igrpen0); +g_free (cpuif_igrpen1); +g_free (cpuif_pmr); +g_free (cpuif_bpr0); +g_free (cpuif_bpr1); +} +static void kvm_arm_gicv3_put (GICv3State *s) {uint32_t regl, regh, reg;uint64_t reg64, redist_typer;int ncpu, i; +bool batch_supported;int64_t start_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME );kvm_arm_gicv3_check (s);
@@ -425,71 +713,95 @@ static void kvm_arm_gicv3_put(GICv3State *s) reg = s->gicd_ctlr;kvm_gicd_access (s,GICD_CTLR , ®, true); -if (redist_typer &GICR_TYPER_PLPIS ) { -/* -* Restore base addresses before LPIs are potentially enabled by -* GICR_CTLR write -*/ -for (ncpu =0 ; ncpu < s->num_cpu; ncpu++) { -GICv3CPUState *c = &s->cpu[ncpu]; + batch_supported =kvm_arm_gic_batch_supported (s->dev_fd); + +if (batch_supported) { + { +GICv3BatchEntry *entries =g_new (GICv3BatchEntry,256 ); +int batch_n =0 ; +int batch_capacity =256 ; + +/* write LPI */ +kvm_arm_gic_batch_write_plpis (s, &entries, redist_typer, + &batch_n, &batch_capacity); + +/* reset batch_n */ + batch_n =0 ; + +/* write Redistributor status */ +kvm_arm_gic_batch_write_redist (s, &entries, &batch_n, + &batch_capacity); - reg64 = c->gicr_propbaser; - regl = (uint32_t )reg64; -kvm_gicr_access (s,GICR_PROPBASER , ncpu, ®l, true); - regh = (uint32_t )(reg64 >>32 ); -kvm_gicr_access (s,GICR_PROPBASER +4 , ncpu, ®h, true); - - reg64 = c->gicr_pendbaser; - regl = (uint32_t )reg64; -kvm_gicr_access (s,GICR_PENDBASER , ncpu, ®l, true); - regh = (uint32_t )(reg64 >>32 ); -kvm_gicr_access (s,GICR_PENDBASER +4 , ncpu, ®h, true); +g_free (entries); + } + }else { +if (redist_typer &GICR_TYPER_PLPIS ) { +/* +* Restore base addresses before LPIs are potentially enabled by +* GICR_CTLR write +*/ +for (ncpu =0 ; ncpu < s->num_cpu; ncpu++) { +GICv3CPUState *c = &s->cpu[ncpu]; + + reg64 = c->gicr_propbaser; + regl = (uint32_t )reg64; +kvm_gicr_access (s,GICR_PROPBASER , ncpu, ®l, true); + regh = (uint32_t )(reg64 >>32 ); +kvm_gicr_access (s,GICR_PROPBASER +4 , ncpu, ®h, true); + + reg64 = c->gicr_pendbaser; + regl = (uint32_t )reg64; +kvm_gicr_access (s,GICR_PENDBASER , ncpu, ®l, true); + regh = (uint32_t )(reg64 >>32 ); +kvm_gicr_access (s,GICR_PENDBASER +4 , ncpu, ®h, true); + } } - } -/* Redistributor state (one per CPU) */ +/* Redistributor state (one per CPU) */ -for (ncpu =0 ; ncpu < s->num_cpu; ncpu++) { -GICv3CPUState *c = &s->cpu[ncpu]; +for (ncpu =0 ; ncpu < s->num_cpu; ncpu++) { +GICv3CPUState *c = &s->cpu[ncpu]; +uint32_t clear_all = ~0 ; - reg = c->gicr_ctlr; -kvm_gicr_access (s,GICR_CTLR , ncpu, ®, true); + reg = c->gicr_ctlr; +kvm_gicr_access (s,GICR_CTLR , ncpu, ®, true); - reg = c->gicr_statusr[GICV3_NS ]; -kvm_gicr_access (s,GICR_STATUSR , ncpu, ®, true); + reg = c->gicr_statusr[GICV3_NS ]; +kvm_gicr_access (s,GICR_STATUSR , ncpu, ®, true); - reg = c->gicr_waker; -kvm_gicr_access (s,GICR_WAKER , ncpu, ®, true); + reg = c->gicr_waker; +kvm_gicr_access (s,GICR_WAKER , ncpu, ®, true); - reg = c->gicr_igroupr0; -kvm_gicr_access (s,GICR_IGROUPR0 , ncpu, ®, true); + reg = c->gicr_igroupr0; +kvm_gicr_access (s,GICR_IGROUPR0 , ncpu, ®, true); - reg = ~0 ; -kvm_gicr_access (s,GICR_ICENABLER0 , ncpu, ®, true); - reg = c->gicr_ienabler0; -kvm_gicr_access (s,GICR_ISENABLER0 , ncpu, ®, true); + reg = clear_all; +kvm_gicr_access (s,GICR_ICENABLER0 , ncpu, ®, true); + reg = c->gicr_ienabler0; +kvm_gicr_access (s,GICR_ISENABLER0 , ncpu, ®, true); -/* Restore config before pending so we treat level/edge correctly */ - reg =half_shuffle32 (c->edge_trigger >>16 ) <<1 ; -kvm_gicr_access (s,GICR_ICFGR1 , ncpu, ®, true); +/* Restore config before pending so we treat level/edge correctly */ + reg =half_shuffle32 (c->edge_trigger >>16 ) <<1 ; +kvm_gicr_access (s,GICR_ICFGR1 , ncpu, ®, true); - reg = c->level; -kvm_gic_line_level_access (s,0 , ncpu, ®, true); + reg = c->level; +kvm_gic_line_level_access (s,0 , ncpu, ®, true); - reg = c->gicr_ipendr0; -kvm_gicr_access (s,GICR_ISPENDR0 , ncpu, ®, true); + reg = c->gicr_ipendr0; +kvm_gicr_access (s,GICR_ISPENDR0 , ncpu, ®, true); - reg = ~0 ; -kvm_gicr_access (s,GICR_ICACTIVER0 , ncpu, ®, true); - reg = c->gicr_iactiver0; -kvm_gicr_access (s,GICR_ISACTIVER0 , ncpu, ®, true); + reg = clear_all; +kvm_gicr_access (s,GICR_ICACTIVER0 , ncpu, ®, true); + reg = c->gicr_iactiver0; +kvm_gicr_access (s,GICR_ISACTIVER0 , ncpu, ®, true); -for (i =0 ; i <GIC_INTERNAL ; i +=4 ) { - reg = c->gicr_ipriorityr[i] | - (c->gicr_ipriorityr[i +1 ] <<8 ) | - (c->gicr_ipriorityr[i +2 ] <<16 ) | - (c->gicr_ipriorityr[i +3 ] <<24 ); -kvm_gicr_access (s,GICR_IPRIORITYR + i, ncpu, ®, true); +for (i =0 ; i <GIC_INTERNAL ; i +=4 ) { + reg = c->gicr_ipriorityr[i] | + (c->gicr_ipriorityr[i +1 ] <<8 ) | + (c->gicr_ipriorityr[i +2 ] <<16 ) | + (c->gicr_ipriorityr[i +3 ] <<24 ); +kvm_gicr_access (s,GICR_IPRIORITYR + i, ncpu, ®, true); + } } }
@@ -540,57 +852,71 @@ static void kvm_arm_gicv3_put(GICv3State *s)/* s->gicd_ipriority[] -> GICD_IPRIORITYRn */ kvm_dist_put_priority (s,GICD_IPRIORITYR , s->gicd_ipriority); -/* CPU Interface state (one per CPU) */ - -for (ncpu =0 ; ncpu < s->num_cpu; ncpu++) { -GICv3CPUState *c = &s->cpu[ncpu]; -int num_pri_bits; +/* write CPU Interface status */ +if (batch_supported) { + { +GICv3BatchEntry *entries =g_new (GICv3BatchEntry,256 ); +int batch_n =0 ; +int batch_capacity =256 ; -kvm_gicc_access (s,ICC_SRE_EL1 , ncpu, &c->icc_sre_el1, true); -kvm_gicc_access (s,ICC_CTLR_EL1 , ncpu, - &c->icc_ctlr_el1[GICV3_NS ], true); -kvm_gicc_access (s,ICC_IGRPEN0_EL1 , ncpu, - &c->icc_igrpen[GICV3_G0 ], true); -kvm_gicc_access (s,ICC_IGRPEN1_EL1 , ncpu, - &c->icc_igrpen[GICV3_G1NS ], true); -kvm_gicc_access (s,ICC_PMR_EL1 , ncpu, &c->icc_pmr_el1, true); -kvm_gicc_access (s,ICC_BPR0_EL1 , ncpu, &c->icc_bpr[GICV3_G0 ], true); -kvm_gicc_access (s,ICC_BPR1_EL1 , ncpu, &c->icc_bpr[GICV3_G1NS ], true); +kvm_arm_gic_batch_write_cpuif (s, &entries, &batch_n, + &batch_capacity); - num_pri_bits = ((c->icc_ctlr_el1[GICV3_NS ] & -ICC_CTLR_EL1_PRIBITS_MASK ) >> -ICC_CTLR_EL1_PRIBITS_SHIFT ) +1 ; - -switch (num_pri_bits) { -case 7 : - reg64 = c->icc_apr[GICV3_G0 ][3 ]; -kvm_gicc_access (s,ICC_AP0R_EL1 (3 ), ncpu, ®64, true); - reg64 = c->icc_apr[GICV3_G0 ][2 ]; -kvm_gicc_access (s,ICC_AP0R_EL1 (2 ), ncpu, ®64, true); -/* fall through */ -case 6 : - reg64 = c->icc_apr[GICV3_G0 ][1 ]; -kvm_gicc_access (s,ICC_AP0R_EL1 (1 ), ncpu, ®64, true); -/* fall through */ -default : - reg64 = c->icc_apr[GICV3_G0 ][0 ]; -kvm_gicc_access (s,ICC_AP0R_EL1 (0 ), ncpu, ®64, true); +g_free (entries); } - -switch (num_pri_bits) { -case 7 : - reg64 = c->icc_apr[GICV3_G1NS ][3 ]; -kvm_gicc_access (s,ICC_AP1R_EL1 (3 ), ncpu, ®64, true); - reg64 = c->icc_apr[GICV3_G1NS ][2 ]; -kvm_gicc_access (s,ICC_AP1R_EL1 (2 ), ncpu, ®64, true); -/* fall through */ -case 6 : - reg64 = c->icc_apr[GICV3_G1NS ][1 ]; -kvm_gicc_access (s,ICC_AP1R_EL1 (1 ), ncpu, ®64, true); -/* fall through */ -default : - reg64 = c->icc_apr[GICV3_G1NS ][0 ]; -kvm_gicc_access (s,ICC_AP1R_EL1 (0 ), ncpu, ®64, true); + }else { +/* CPU Interface state (one per CPU) */ +for (ncpu =0 ; ncpu < s->num_cpu; ncpu++) { +GICv3CPUState *c = &s->cpu[ncpu]; +int num_pri_bits; + +kvm_gicc_access (s,ICC_SRE_EL1 , ncpu, &c->icc_sre_el1, true); +kvm_gicc_access (s,ICC_CTLR_EL1 , ncpu, + &c->icc_ctlr_el1[GICV3_NS ], true); +kvm_gicc_access (s,ICC_IGRPEN0_EL1 , ncpu, + &c->icc_igrpen[GICV3_G0 ], true); +kvm_gicc_access (s,ICC_IGRPEN1_EL1 , ncpu, + &c->icc_igrpen[GICV3_G1NS ], true); +kvm_gicc_access (s,ICC_PMR_EL1 , ncpu, &c->icc_pmr_el1, true); +kvm_gicc_access (s,ICC_BPR0_EL1 , ncpu, &c->icc_bpr[GICV3_G0 ], true); +kvm_gicc_access (s,ICC_BPR1_EL1 , ncpu, &c->icc_bpr[GICV3_G1NS ], + true); + + num_pri_bits = ((c->icc_ctlr_el1[GICV3_NS ] & +ICC_CTLR_EL1_PRIBITS_MASK ) >> +ICC_CTLR_EL1_PRIBITS_SHIFT ) +1 ; + +switch (num_pri_bits) { +case 7 : + reg64 = c->icc_apr[GICV3_G0 ][3 ]; +kvm_gicc_access (s,ICC_AP0R_EL1 (3 ), ncpu, ®64, true); + reg64 = c->icc_apr[GICV3_G0 ][2 ]; +kvm_gicc_access (s,ICC_AP0R_EL1 (2 ), ncpu, ®64, true); +/* fall through */ +case 6 : + reg64 = c->icc_apr[GICV3_G0 ][1 ]; +kvm_gicc_access (s,ICC_AP0R_EL1 (1 ), ncpu, ®64, true); +/* fall through */ +default : + reg64 = c->icc_apr[GICV3_G0 ][0 ]; +kvm_gicc_access (s,ICC_AP0R_EL1 (0 ), ncpu, ®64, true); + } + +switch (num_pri_bits) { +case 7 : + reg64 = c->icc_apr[GICV3_G1NS ][3 ]; +kvm_gicc_access (s,ICC_AP1R_EL1 (3 ), ncpu, ®64, true); + reg64 = c->icc_apr[GICV3_G1NS ][2 ]; +kvm_gicc_access (s,ICC_AP1R_EL1 (2 ), ncpu, ®64, true); +/* fall through */ +case 6 : + reg64 = c->icc_apr[GICV3_G1NS ][1 ]; +kvm_gicc_access (s,ICC_AP1R_EL1 (1 ), ncpu, ®64, true); +/* fall through */ +default : + reg64 = c->icc_apr[GICV3_G1NS ][0 ]; +kvm_gicc_access (s,ICC_AP1R_EL1 (0 ), ncpu, ®64, true); + } } }int64_t end_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME );
Signature · 1 line
2.43.0
PATCH 4/4 hw/intc/arm_gicv3_kvm: Refactor kvm_arm_gicv3_get for batch register access
In reply to Yize WangReplace the kvm_arm_gicv3_get function with a batched implementation that uses the batch helpers added in Task 4.
The new function:
1. Detects batch support using kvm_arm_gic_batch_supported()
2. When batch is supported:
- Batches redistributor register accesses for all vCPUs and commits after the loop, with post-processing for ICFGR1 transformation and IPRIORITYR byte splitting
- Batches PLPIS propbaser/pendbaser accesses when applicable
- Batches CPU interface basic registers for all enabled vCPUs
- Batches CPU interface APR registers based on num_pri_bits
3. Falls back to existing per-register ioctl calls when batch is not supported
4. Keeps all distributor accesses unchanged
Also add auxiliary functions to implement batch operations:
-kvm_arm_gic_batch_get_redist: read Redistributor common registers
in batches
-kvm_arm_gic_batch_get_plpis: read the LPI base address registers
in batches
-kvm_arm_gic_batch_get_cpuif_basic: read the CPU interface basic registers
in batches
-kvm_arm_gic_batch_get_cpuif_apr: read the CPU Interface APR Registers
in batches
This reduces the number of KVM device attribute ioctls from O(num_cpu * registers_per_cpu) to a small constant number of batch commits.
--- hw/intc/arm_gicv3_kvm.c | 500 +++++++++++++++++++++++++++++++++------- 1 file changed, 412 insertions(+), 88 deletions(-)
hw/intc/arm_gicv3_kvm.c +412 -88
@@ -923,11 +923,293 @@ static void kvm_arm_gicv3_put(GICv3State *s)info_report ("kvm_arm_gicv3_put, time=%ldms" , end_time - start_time); } +/* Read Redistributor common registers in batches */ +static void kvm_arm_gic_batch_get_redist (GICv3State *s, +GICv3BatchEntry **entries, +int *batch_n, +int *batch_capacity) +{ +uint32_t *redist_priority =g_new (uint32_t, + s->num_cpu * (GIC_INTERNAL /4 )); + +for (int ncpu =0 ; ncpu < s->num_cpu; ncpu++) { +GICv3CPUState *c = &s->cpu[ncpu]; +int pri_base = ncpu * (GIC_INTERNAL /4 ); + +/* Redistributor state (one per CPU) */ +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_CTLR , c->gicr_typer), + &c->gicr_ctlr); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_STATUSR , c->gicr_typer), + &c->gicr_statusr[GICV3_NS ]); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_WAKER , c->gicr_typer), + &c->gicr_waker); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_IGROUPR0 , c->gicr_typer), + &c->gicr_igroupr0); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_ISENABLER0 , c->gicr_typer), + &c->gicr_ienabler0); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_ICFGR1 , c->gicr_typer), + &c->edge_trigger); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_LEVEL_INFO , +KVM_VGIC_ATTR (0 , c->gicr_typer) | + (VGIC_LEVEL_INFO_LINE_LEVEL << +KVM_DEV_ARM_VGIC_LINE_LEVEL_INFO_SHIFT ), + &c->level); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_ISPENDR0 , c->gicr_typer), + &c->gicr_ipendr0); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_ISACTIVER0 , c->gicr_typer), + &c->gicr_iactiver0); + +for (int i =0 ; i <GIC_INTERNAL ; i +=4 ) { +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_IPRIORITYR + i, + c->gicr_typer), + &redist_priority[pri_base + i /4 ]); + } + } +kvm_arm_gic_batch_commit (s->dev_fd, *entries, *batch_n, false); + +/* Post-process redistributor results */ +for (int ncpu =0 ; ncpu < s->num_cpu; ncpu++) { +GICv3CPUState *c = &s->cpu[ncpu]; +int pri_base = ncpu * (GIC_INTERNAL /4 ); + + c->edge_trigger =half_unshuffle32 (c->edge_trigger >>1 ) <<16 ; + +for (int i =0 ; i <GIC_INTERNAL ; i +=4 ) { +uint32_t val = redist_priority[pri_base + i /4 ]; + c->gicr_ipriorityr[i] =extract32 (val,0 ,8 ); + c->gicr_ipriorityr[i +1 ] =extract32 (val,8 ,8 ); + c->gicr_ipriorityr[i +2 ] =extract32 (val,16 ,8 ); + c->gicr_ipriorityr[i +3 ] =extract32 (val,24 ,8 ); + } + } + +g_free (redist_priority); +} + +/* Read the LPI base address registers in batches */ +static void kvm_arm_gic_batch_get_plpis (GICv3State *s, +GICv3BatchEntry **entries, +uint64_t redist_typer, +int *batch_n, +int *batch_capacity) +{ +if (!(redist_typer &GICR_TYPER_PLPIS )) { +return ; + } + +uint32_t *plpis =g_new (uint32_t, s->num_cpu *4 ); + +for (int ncpu =0 ; ncpu < s->num_cpu; ncpu++) { +GICv3CPUState *c = &s->cpu[ncpu]; +int base = ncpu *4 ; + +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_PROPBASER , c->gicr_typer), + &plpis[base]); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_PROPBASER +4 , c->gicr_typer), + &plpis[base +1 ]); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_PENDBASER , c->gicr_typer), + &plpis[base +2 ]); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_REDIST_REGS , +KVM_VGIC_ATTR (GICR_PENDBASER +4 , c->gicr_typer), + &plpis[base +3 ]); + } + +kvm_arm_gic_batch_commit (s->dev_fd, *entries, *batch_n, false); + +for (int ncpu =0 ; ncpu < s->num_cpu; ncpu++) { +GICv3CPUState *c = &s->cpu[ncpu]; +int base = ncpu *4 ; + + c->gicr_propbaser = ((uint64_t )plpis[base +1 ] <<32 ) | plpis[base]; + c->gicr_pendbaser = ((uint64_t )plpis[base +3 ] <<32 ) | plpis[base +2 ]; + } + +g_free (plpis); +} + +/* Read the CPU interface basic registers in batches */ +static void kvm_arm_gic_batch_get_cpuif_basic (GICv3State *s, +GICv3BatchEntry **entries, +int *batch_n, +int *batch_capacity) +{ +uint32_t *cpuif_sre =g_new0 (uint32_t, s->num_cpu); +uint32_t *cpuif_ctlr =g_new0 (uint32_t, s->num_cpu); +uint32_t *cpuif_igrpen0 =g_new0 (uint32_t, s->num_cpu); +uint32_t *cpuif_igrpen1 =g_new0 (uint32_t, s->num_cpu); +uint32_t *cpuif_pmr =g_new0 (uint32_t, s->num_cpu); +uint32_t *cpuif_bpr0 =g_new0 (uint32_t, s->num_cpu); +uint32_t *cpuif_bpr1 =g_new0 (uint32_t, s->num_cpu); + +/* CPU Interface - basic registers */ +for (int ncpu =0 ; ncpu < s->num_cpu; ncpu++) { +GICv3CPUState *c = &s->cpu[ncpu]; + + cpuif_sre[ncpu] = c->icc_sre_el1; +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_SRE_EL1 , c->gicr_typer), + &cpuif_sre[ncpu]); + + cpuif_ctlr[ncpu] = c->icc_ctlr_el1[GICV3_NS ]; +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_CTLR_EL1 , c->gicr_typer), + &cpuif_ctlr[ncpu]); + + cpuif_igrpen0[ncpu] = c->icc_igrpen[GICV3_G0 ]; +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_IGRPEN0_EL1 , c->gicr_typer), + &cpuif_igrpen0[ncpu]); + + cpuif_igrpen1[ncpu] = c->icc_igrpen[GICV3_G1NS ]; +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_IGRPEN1_EL1 , c->gicr_typer), + &cpuif_igrpen1[ncpu]); + + cpuif_pmr[ncpu] = c->icc_pmr_el1; +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_PMR_EL1 , c->gicr_typer), + &cpuif_pmr[ncpu]); + + cpuif_bpr0[ncpu] = c->icc_bpr[GICV3_G0 ]; +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_BPR0_EL1 , c->gicr_typer), + &cpuif_bpr0[ncpu]); + + cpuif_bpr1[ncpu] = c->icc_bpr[GICV3_G1NS ]; +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_BPR1_EL1 , c->gicr_typer), + &cpuif_bpr1[ncpu]); + } +kvm_arm_gic_batch_commit (s->dev_fd, *entries, *batch_n, false); + +/* Copy back basic CPU interface registers */ +for (int ncpu =0 ; ncpu < s->num_cpu; ncpu++) { +GICv3CPUState *c = &s->cpu[ncpu]; + + c->icc_sre_el1 = cpuif_sre[ncpu]; + c->icc_ctlr_el1[GICV3_NS ] = cpuif_ctlr[ncpu]; + c->icc_igrpen[GICV3_G0 ] = cpuif_igrpen0[ncpu]; + c->icc_igrpen[GICV3_G1NS ] = cpuif_igrpen1[ncpu]; + c->icc_pmr_el1 = cpuif_pmr[ncpu]; + c->icc_bpr[GICV3_G0 ] = cpuif_bpr0[ncpu]; + c->icc_bpr[GICV3_G1NS ] = cpuif_bpr1[ncpu]; + } + +g_free (cpuif_sre); +g_free (cpuif_ctlr); +g_free (cpuif_igrpen0); +g_free (cpuif_igrpen1); +g_free (cpuif_pmr); +g_free (cpuif_bpr0); +g_free (cpuif_bpr1); +} + +/* Read the CPU Interface APR Registers in Batches */ +static void kvm_arm_gic_batch_get_cpuif_apr (GICv3State *s, +GICv3BatchEntry **entries, +int *batch_n, +int *batch_capacity) +{ +for (int ncpu =0 ; ncpu < s->num_cpu; ncpu++) { +GICv3CPUState *c = &s->cpu[ncpu]; +int num_pri_bits; + + num_pri_bits = ((c->icc_ctlr_el1[GICV3_NS ] & +ICC_CTLR_EL1_PRIBITS_MASK ) >> +ICC_CTLR_EL1_PRIBITS_SHIFT ) +1 ; + +/* Group 0 APR */ +switch (num_pri_bits) { +case 7 : +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_AP0R_EL1 (3 ), c->gicr_typer), + (uint32_t *)&c->icc_apr[GICV3_G0 ][3 ]); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_AP0R_EL1 (2 ), c->gicr_typer), + (uint32_t *)&c->icc_apr[GICV3_G0 ][2 ]); +/* fall through */ +case 6 : +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_AP0R_EL1 (1 ), c->gicr_typer), + (uint32_t *)&c->icc_apr[GICV3_G0 ][1 ]); +/* fall through */ +default : +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_AP0R_EL1 (0 ), c->gicr_typer), + (uint32_t *)&c->icc_apr[GICV3_G0 ][0 ]); + } + +/* Group 1 APR */ +switch (num_pri_bits) { +case 7 : +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_AP1R_EL1 (3 ), c->gicr_typer), + (uint32_t *)&c->icc_apr[GICV3_G1NS ][3 ]); +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_AP1R_EL1 (2 ), c->gicr_typer), + (uint32_t *)&c->icc_apr[GICV3_G1NS ][2 ]); +/* fall through */ +case 6 : +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_AP1R_EL1 (1 ), c->gicr_typer), + (uint32_t *)&c->icc_apr[GICV3_G1NS ][1 ]); +/* fall through */ +default : +kvm_arm_gic_batch_add (entries, batch_n, batch_capacity, +KVM_DEV_ARM_VGIC_GRP_CPU_SYSREGS , +KVM_VGIC_ATTR (ICC_AP1R_EL1 (0 ), c->gicr_typer), + (uint32_t *)&c->icc_apr[GICV3_G1NS ][0 ]); + } + } +kvm_arm_gic_batch_commit (s->dev_fd, *entries, *batch_n, false); +} +static void kvm_arm_gicv3_get (GICv3State *s) {uint32_t regl, regh, reg;uint64_t reg64, redist_typer;int ncpu, i; +bool batch_supported;int64_t start_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME );kvm_arm_gicv3_check (s);
@@ -939,58 +1221,80 @@ static void kvm_arm_gicv3_get(GICv3State *s)kvm_gicd_access (s,GICD_CTLR , ®, false); s->gicd_ctlr = reg; -/* Redistributor state (one per CPU) */ +/* Check whether the kernel supports batch operations */ + batch_supported =kvm_arm_gic_batch_supported (s->dev_fd); -for (ncpu =0 ; ncpu < s->num_cpu; ncpu++) { -GICv3CPUState *c = &s->cpu[ncpu]; +if (batch_supported) { + { +GICv3BatchEntry *entries =g_new (GICv3BatchEntry,256 ); +int batch_n =0 ; +int batch_capacity =256 ; -kvm_gicr_access (s,GICR_CTLR , ncpu, ®, false); - c->gicr_ctlr = reg; - -kvm_gicr_access (s,GICR_STATUSR , ncpu, ®, false); - c->gicr_statusr[GICV3_NS ] = reg; - -kvm_gicr_access (s,GICR_WAKER , ncpu, ®, false); - c->gicr_waker = reg; - -kvm_gicr_access (s,GICR_IGROUPR0 , ncpu, ®, false); - c->gicr_igroupr0 = reg; -kvm_gicr_access (s,GICR_ISENABLER0 , ncpu, ®, false); - c->gicr_ienabler0 = reg; -kvm_gicr_access (s,GICR_ICFGR1 , ncpu, ®, false); - c->edge_trigger =half_unshuffle32 (reg >>1 ) <<16 ; -kvm_gic_line_level_access (s,0 , ncpu, ®, false); - c->level = reg; -kvm_gicr_access (s,GICR_ISPENDR0 , ncpu, ®, false); - c->gicr_ipendr0 = reg; -kvm_gicr_access (s,GICR_ISACTIVER0 , ncpu, ®, false); - c->gicr_iactiver0 = reg; - -for (i =0 ; i <GIC_INTERNAL ; i +=4 ) { -kvm_gicr_access (s,GICR_IPRIORITYR + i, ncpu, ®, false); - c->gicr_ipriorityr[i] =extract32 (reg,0 ,8 ); - c->gicr_ipriorityr[i +1 ] =extract32 (reg,8 ,8 ); - c->gicr_ipriorityr[i +2 ] =extract32 (reg,16 ,8 ); - c->gicr_ipriorityr[i +3 ] =extract32 (reg,24 ,8 ); - } - } +/* Redistributor state (one per CPU) */ +kvm_arm_gic_batch_get_redist (s, &entries, &batch_n, + &batch_capacity); + +/* reset batch_n */ + batch_n =0 ; + +/* PLPIS */ +kvm_arm_gic_batch_get_plpis (s, &entries, redist_typer, + &batch_n, &batch_capacity); -if (redist_typer &GICR_TYPER_PLPIS ) { +g_free (entries); + } + }else { +/* Redistributor state (one per CPU) */ for (ncpu =0 ; ncpu < s->num_cpu; ncpu++) {GICv3CPUState *c = &s->cpu[ncpu]; -kvm_gicr_access (s,GICR_PROPBASER , ncpu, ®l, false); -kvm_gicr_access (s,GICR_PROPBASER +4 , ncpu, ®h, false); - c->gicr_propbaser = ((uint64_t )regh <<32 ) | regl; +kvm_gicr_access (s,GICR_CTLR , ncpu, ®, false); + c->gicr_ctlr = reg; + +kvm_gicr_access (s,GICR_STATUSR , ncpu, ®, false); + c->gicr_statusr[GICV3_NS ] = reg; + +kvm_gicr_access (s,GICR_WAKER , ncpu, ®, false); + c->gicr_waker = reg; + +kvm_gicr_access (s,GICR_IGROUPR0 , ncpu, ®, false); + c->gicr_igroupr0 = reg; +kvm_gicr_access (s,GICR_ISENABLER0 , ncpu, ®, false); + c->gicr_ienabler0 = reg; +kvm_gicr_access (s,GICR_ICFGR1 , ncpu, ®, false); + c->edge_trigger =half_unshuffle32 (reg >>1 ) <<16 ; +kvm_gic_line_level_access (s,0 , ncpu, ®, false); + c->level = reg; +kvm_gicr_access (s,GICR_ISPENDR0 , ncpu, ®, false); + c->gicr_ipendr0 = reg; +kvm_gicr_access (s,GICR_ISACTIVER0 , ncpu, ®, false); + c->gicr_iactiver0 = reg; + +for (i =0 ; i <GIC_INTERNAL ; i +=4 ) { +kvm_gicr_access (s,GICR_IPRIORITYR + i, ncpu, ®, false); + c->gicr_ipriorityr[i] =extract32 (reg,0 ,8 ); + c->gicr_ipriorityr[i +1 ] =extract32 (reg,8 ,8 ); + c->gicr_ipriorityr[i +2 ] =extract32 (reg,16 ,8 ); + c->gicr_ipriorityr[i +3 ] =extract32 (reg,24 ,8 ); + } + } + +if (redist_typer &GICR_TYPER_PLPIS ) { +for (ncpu =0 ; ncpu < s->num_cpu; ncpu++) { +GICv3CPUState *c = &s->cpu[ncpu]; -kvm_gicr_access (s,GICR_PENDBASER , ncpu, ®l, false); -kvm_gicr_access (s,GICR_PENDBASER +4 , ncpu, ®h, false); - c->gicr_pendbaser = ((uint64_t )regh <<32 ) | regl; +kvm_gicr_access (s,GICR_PROPBASER , ncpu, ®l, false); +kvm_gicr_access (s,GICR_PROPBASER +4 , ncpu, ®h, false); + c->gicr_propbaser = ((uint64_t )regh <<32 ) | regl; + +kvm_gicr_access (s,GICR_PENDBASER , ncpu, ®l, false); +kvm_gicr_access (s,GICR_PENDBASER +4 , ncpu, ®h, false); + c->gicr_pendbaser = ((uint64_t )regh <<32 ) | regl; + } } }/* Distributor state (shared between all CPUs */ -kvm_gicd_access (s,GICD_STATUSR , ®, false); s->gicd_statusr[GICV3_NS ] = reg;
@@ -1025,58 +1329,78 @@ static void kvm_arm_gicv3_get(GICv3State *s) s->gicd_irouter[i] = ((uint64_t )regh <<32 ) | regl; } -/***************************************************************** -* CPU Interface(s) State -*/ +/* CPU Interface(s) State */ +if (batch_supported) { + { +GICv3BatchEntry *entries =g_new (GICv3BatchEntry,256 ); +int batch_n =0 ; +int batch_capacity =256 ; -for (ncpu =0 ; ncpu < s->num_cpu; ncpu++) { -GICv3CPUState *c = &s->cpu[ncpu]; -int num_pri_bits; +/* CPU Interface - basic registers */ +kvm_arm_gic_batch_get_cpuif_basic (s, &entries, &batch_n, + &batch_capacity); -kvm_gicc_access (s,ICC_SRE_EL1 , ncpu, &c->icc_sre_el1, false); -kvm_gicc_access (s,ICC_CTLR_EL1 , ncpu, - &c->icc_ctlr_el1[GICV3_NS ], false); -kvm_gicc_access (s,ICC_IGRPEN0_EL1 , ncpu, - &c->icc_igrpen[GICV3_G0 ], false); -kvm_gicc_access (s,ICC_IGRPEN1_EL1 , ncpu, - &c->icc_igrpen[GICV3_G1NS ], false); -kvm_gicc_access (s,ICC_PMR_EL1 , ncpu, &c->icc_pmr_el1, false); -kvm_gicc_access (s,ICC_BPR0_EL1 , ncpu, &c->icc_bpr[GICV3_G0 ], false); -kvm_gicc_access (s,ICC_BPR1_EL1 , ncpu, &c->icc_bpr[GICV3_G1NS ], false); - num_pri_bits = ((c->icc_ctlr_el1[GICV3_NS ] & -ICC_CTLR_EL1_PRIBITS_MASK ) >> -ICC_CTLR_EL1_PRIBITS_SHIFT ) +1 ; +/* reset batch_n */ + batch_n =0 ; -switch (num_pri_bits) { -case 7 : -kvm_gicc_access (s,ICC_AP0R_EL1 (3 ), ncpu, ®64, false); - c->icc_apr[GICV3_G0 ][3 ] = reg64; -kvm_gicc_access (s,ICC_AP0R_EL1 (2 ), ncpu, ®64, false); - c->icc_apr[GICV3_G0 ][2 ] = reg64; -/* fall through */ -case 6 : -kvm_gicc_access (s,ICC_AP0R_EL1 (1 ), ncpu, ®64, false); - c->icc_apr[GICV3_G0 ][1 ] = reg64; -/* fall through */ -default : -kvm_gicc_access (s,ICC_AP0R_EL1 (0 ), ncpu, ®64, false); - c->icc_apr[GICV3_G0 ][0 ] = reg64; +/* CPU Interface - APR registers */ +kvm_arm_gic_batch_get_cpuif_apr (s, &entries, &batch_n, + &batch_capacity); + +g_free (entries); } + }else { +for (ncpu =0 ; ncpu < s->num_cpu; ncpu++) { +GICv3CPUState *c = &s->cpu[ncpu]; +int num_pri_bits; -switch (num_pri_bits) { -case 7 : -kvm_gicc_access (s,ICC_AP1R_EL1 (3 ), ncpu, ®64, false); - c->icc_apr[GICV3_G1NS ][3 ] = reg64; -kvm_gicc_access (s,ICC_AP1R_EL1 (2 ), ncpu, ®64, false); - c->icc_apr[GICV3_G1NS ][2 ] = reg64; -/* fall through */ -case 6 : -kvm_gicc_access (s,ICC_AP1R_EL1 (1 ), ncpu, ®64, false); - c->icc_apr[GICV3_G1NS ][1 ] = reg64; -/* fall through */ -default : -kvm_gicc_access (s,ICC_AP1R_EL1 (0 ), ncpu, ®64, false); - c->icc_apr[GICV3_G1NS ][0 ] = reg64; +kvm_gicc_access (s,ICC_SRE_EL1 , ncpu, &c->icc_sre_el1, false); +kvm_gicc_access (s,ICC_CTLR_EL1 , ncpu, + &c->icc_ctlr_el1[GICV3_NS ], false); +kvm_gicc_access (s,ICC_IGRPEN0_EL1 , ncpu, + &c->icc_igrpen[GICV3_G0 ], false); +kvm_gicc_access (s,ICC_IGRPEN1_EL1 , ncpu, + &c->icc_igrpen[GICV3_G1NS ], false); +kvm_gicc_access (s,ICC_PMR_EL1 , ncpu, &c->icc_pmr_el1, false); +kvm_gicc_access (s,ICC_BPR0_EL1 , ncpu, &c->icc_bpr[GICV3_G0 ], + false); +kvm_gicc_access (s,ICC_BPR1_EL1 , ncpu, &c->icc_bpr[GICV3_G1NS ], + false); + num_pri_bits = ((c->icc_ctlr_el1[GICV3_NS ] & +ICC_CTLR_EL1_PRIBITS_MASK ) >> +ICC_CTLR_EL1_PRIBITS_SHIFT ) +1 ; + +switch (num_pri_bits) { +case 7 : +kvm_gicc_access (s,ICC_AP0R_EL1 (3 ), ncpu, ®64, false); + c->icc_apr[GICV3_G0 ][3 ] = reg64; +kvm_gicc_access (s,ICC_AP0R_EL1 (2 ), ncpu, ®64, false); + c->icc_apr[GICV3_G0 ][2 ] = reg64; +/* fall through */ +case 6 : +kvm_gicc_access (s,ICC_AP0R_EL1 (1 ), ncpu, ®64, false); + c->icc_apr[GICV3_G0 ][1 ] = reg64; +/* fall through */ +default : +kvm_gicc_access (s,ICC_AP0R_EL1 (0 ), ncpu, ®64, false); + c->icc_apr[GICV3_G0 ][0 ] = reg64; + } + +switch (num_pri_bits) { +case 7 : +kvm_gicc_access (s,ICC_AP1R_EL1 (3 ), ncpu, ®64, false); + c->icc_apr[GICV3_G1NS ][3 ] = reg64; +kvm_gicc_access (s,ICC_AP1R_EL1 (2 ), ncpu, ®64, false); + c->icc_apr[GICV3_G1NS ][2 ] = reg64; +/* fall through */ +case 6 : +kvm_gicc_access (s,ICC_AP1R_EL1 (1 ), ncpu, ®64, false); + c->icc_apr[GICV3_G1NS ][1 ] = reg64; +/* fall through */ +default : +kvm_gicc_access (s,ICC_AP1R_EL1 (0 ), ncpu, ®64, false); + c->icc_apr[GICV3_G1NS ][0 ] = reg64; + } } }int64_t end_time =qemu_clock_get_ms (QEMU_CLOCK_REALTIME );
Signature · 1 line
2.43.0
Quoted 37 lines · Yize Wang
This series adds batch register access support to QEMU's KVM GICv3 register synchronization path, reducing syscall overhead during ARM64 live migration.
Currently, QEMU issues one ioctl per register when saving/restoring VGIC state. On large VM configurations this means tens of thousands of syscalls, where lock acquisition and context switch overhead dominates migration downtime. Thus, we provide a batch register method to allow userspace read/write multiple distributor and redistributor registers in a single call. In this way, we can significantly reduce syscalls and migration downtime.
All batch paths probe kernel capability at runtime and fall back to the original one-by-one path when the kernel does not support batch access, ensuring backward compatibility.
This series depends on the corresponding kernel patches that add
KVM_DEV_ARM_VGIC_GRP_BATCH_REGSsupport.Test the VM migration time under pressure conditions. The VM specifications for migration are as follows:
- VM use 4-K page;
- the number of VCPU is 160;
- the total memory is 320Gigabit;
- use 'Redis SET-benchmark' to pressurize VM;Performance results (3-run average, ms):
| Metric | Without patch | With patch | Improvement | |---------------------|---------------|------------|-------------| | Migration downtime | 536 | 321 | 40% | | Source (total) | 344 | 230 | 33% | | - VGIC put | 158 | 40 | 75% | | - VGIC get | 120 | 19 | 84% | | Destination (total) | 192 | 91 | 53% | | - VGIC put | 132 | 27 | 80% |
Quoted 5 lines
Yize Wang (4):
Sync KVM ARM VGIC batch register UAPI to QEMU headers hw/intc/arm_gicv3_kvm: Add batch helper structures and functions hw/intc/arm_gicv3_kvm: Refactorkvm_arm_gicv3_putfor batch register access hw/intc/arm_gicv3_kvm: Refactorkvm_arm_gicv3_getfor batch register access
Just FYI for next time, patchsets like these that are the userspace half of a kernel KVM API change/enhancement that hasn't yet been accepted into the upstream kernel should be marked as "RFC" for the QEMU patchset.
thanks
-- PMM