linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: eric.auger@redhat.com (Auger Eric)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH v5 22/22] KVM: arm64: vgic-v3: KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES
Date: Thu, 4 May 2017 00:22:45 +0200	[thread overview]
Message-ID: <a236dc61-2454-835d-985e-db154661042c@redhat.com> (raw)
In-Reply-To: <20170430213219.GE1499@lvm>

Hi Christoffer,

On 30/04/2017 23:32, Christoffer Dall wrote:
> On Fri, Apr 14, 2017 at 12:15:34PM +0200, Eric Auger wrote:
>> This patch adds a new attribute to GICV3 KVM device
>> KVM_DEV_ARM_VGIC_GRP_CTRL group. This Allows the userspace to
> 
> nit: allows (lowercase)
> nit: s/the userspace/userspace/
> 
>> flush all GICR pending tables into guest RAM. At the moment
>> we do not offer any restore control as the sync is implicit.
> 
> I would probably remove the last sentence here.
> 
>>
>> As we need the PENDBASER_ADDRESS() in vgic-v3, let's move its
>> definition in the irqchip header. We restore the full length
>> of the field, ie [51:16]. Same for PROPBASER_ADDRESS with full
>> field length of [51:12].
>>
>> Signed-off-by: Eric Auger <eric.auger@redhat.com>
>>
>> ---
>> v4 -> v5:
>> - move pending table save code/ctrl into VGICv3 instead of ITS
>> - remove useless gpa_t cast
>> - use the same optimization as in its_sync_lpi_pending_table
>>
>> v3 -> v4:
>> - remove the wrong comment about locking
>> - pass kvm struct instead of its handle
>> - add comment about restore method
>> - remove GITR_PENDABASER.PTZ check
>> - continue if target_vcpu == NULL
>> - new locking strategy
>>
>> v1 -> v2:
>> - do not care about the 1st KB which should be zeroed according to
>>   the spec.
>> ---
>>  arch/arm/include/uapi/asm/kvm.h     |  1 +
>>  arch/arm64/include/uapi/asm/kvm.h   |  1 +
>>  include/linux/irqchip/arm-gic-v3.h  |  2 ++
>>  virt/kvm/arm/vgic/vgic-its.c        |  6 ++---
>>  virt/kvm/arm/vgic/vgic-kvm-device.c | 20 ++++++++++++++
>>  virt/kvm/arm/vgic/vgic-v3.c         | 54 +++++++++++++++++++++++++++++++++++++
>>  virt/kvm/arm/vgic/vgic.h            |  1 +
>>  7 files changed, 81 insertions(+), 4 deletions(-)
>>
>> diff --git a/arch/arm/include/uapi/asm/kvm.h b/arch/arm/include/uapi/asm/kvm.h
>> index 8e6563c..78fe803 100644
>> --- a/arch/arm/include/uapi/asm/kvm.h
>> +++ b/arch/arm/include/uapi/asm/kvm.h
>> @@ -202,6 +202,7 @@ struct kvm_arch_memory_slot {
>>  #define   KVM_DEV_ARM_VGIC_CTRL_INIT		0
>>  #define   KVM_DEV_ARM_ITS_SAVE_TABLES		1
>>  #define   KVM_DEV_ARM_ITS_RESTORE_TABLES	2
>> +#define   KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES	3
>>  
>>  /* KVM_IRQ_LINE irq field index values */
>>  #define KVM_ARM_IRQ_TYPE_SHIFT		24
>> diff --git a/arch/arm64/include/uapi/asm/kvm.h b/arch/arm64/include/uapi/asm/kvm.h
>> index 1e35115..8a3758a 100644
>> --- a/arch/arm64/include/uapi/asm/kvm.h
>> +++ b/arch/arm64/include/uapi/asm/kvm.h
>> @@ -222,6 +222,7 @@ struct kvm_arch_memory_slot {
>>  #define   KVM_DEV_ARM_VGIC_CTRL_INIT		0
>>  #define   KVM_DEV_ARM_ITS_SAVE_TABLES           1
>>  #define   KVM_DEV_ARM_ITS_RESTORE_TABLES        2
>> +#define   KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES	3
>>  
>>  /* Device Control API on vcpu fd */
>>  #define KVM_ARM_VCPU_PMU_V3_CTRL	0
>> diff --git a/include/linux/irqchip/arm-gic-v3.h b/include/linux/irqchip/arm-gic-v3.h
>> index 0c6798c..9d3932f 100644
>> --- a/include/linux/irqchip/arm-gic-v3.h
>> +++ b/include/linux/irqchip/arm-gic-v3.h
>> @@ -158,6 +158,7 @@
>>  #define GICR_PROPBASER_RaWaWb	GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWaWb)
>>  
>>  #define GICR_PROPBASER_IDBITS_MASK			(0x1f)
>> +#define GICR_PROPBASER_ADDRESS(x)    ((x) & GENMASK_ULL(51, 12))
>>  
>>  #define GICR_PENDBASER_SHAREABILITY_SHIFT		(10)
>>  #define GICR_PENDBASER_INNER_CACHEABILITY_SHIFT		(7)
>> @@ -183,6 +184,7 @@
>>  #define GICR_PENDBASER_RaWaWb	GIC_BASER_CACHEABILITY(GICR_PENDBASER, INNER, RaWaWb)
>>  
>>  #define GICR_PENDBASER_PTZ				BIT_ULL(62)
>> +#define GICR_PENDBASER_ADDRESS(x)    ((x) & GENMASK_ULL(51, 16))
>>  
>>  /*
>>   * Re-Distributor registers, offsets from SGI_base
>> diff --git a/virt/kvm/arm/vgic/vgic-its.c b/virt/kvm/arm/vgic/vgic-its.c
>> index be848be..745c245 100644
>> --- a/virt/kvm/arm/vgic/vgic-its.c
>> +++ b/virt/kvm/arm/vgic/vgic-its.c
>> @@ -189,8 +189,6 @@ static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,
>>   */
>>  #define BASER_ADDRESS(x)	((x) & GENMASK_ULL(47, 16))
>>  #define CBASER_ADDRESS(x)	((x) & GENMASK_ULL(47, 12))
>> -#define PENDBASER_ADDRESS(x)	((x) & GENMASK_ULL(47, 16))
>> -#define PROPBASER_ADDRESS(x)	((x) & GENMASK_ULL(47, 12))
>>  
>>  #define GIC_LPI_OFFSET 8192
>>  
>> @@ -227,7 +225,7 @@ static struct its_collection *find_collection(struct vgic_its *its, int coll_id)
>>  static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq,
>>  			     struct kvm_vcpu *filter_vcpu)
>>  {
>> -	u64 propbase = PROPBASER_ADDRESS(kvm->arch.vgic.propbaser);
>> +	u64 propbase = GICR_PROPBASER_ADDRESS(kvm->arch.vgic.propbaser);
>>  	u8 prop;
>>  	int ret;
>>  
>> @@ -339,7 +337,7 @@ static u32 max_lpis_propbaser(u64 propbaser)
>>   */
>>  static int its_sync_lpi_pending_table(struct kvm_vcpu *vcpu)
>>  {
>> -	gpa_t pendbase = PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser);
>> +	gpa_t pendbase = GICR_PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser);
>>  	struct vgic_irq *irq;
>>  	int last_byte_offset = -1;
>>  	int ret = 0;
>> diff --git a/virt/kvm/arm/vgic/vgic-kvm-device.c b/virt/kvm/arm/vgic/vgic-kvm-device.c
>> index 859bfa8..d48743c 100644
>> --- a/virt/kvm/arm/vgic/vgic-kvm-device.c
>> +++ b/virt/kvm/arm/vgic/vgic-kvm-device.c
>> @@ -580,6 +580,24 @@ static int vgic_v3_set_attr(struct kvm_device *dev,
>>  		reg = tmp32;
>>  		return vgic_v3_attr_regs_access(dev, attr, &reg, true);
>>  	}
>> +	case KVM_DEV_ARM_VGIC_GRP_CTRL: {
>> +		int ret;
>> +
>> +		switch (attr->attr) {
>> +		case KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES:
>> +			mutex_lock(&dev->kvm->lock);
>> +
>> +			if (!lock_all_vcpus(dev->kvm)) {
>> +				mutex_unlock(&dev->kvm->lock);
>> +				return -EBUSY;
>> +			}
>> +			ret = vgic_v3_save_pending_tables(dev->kvm);
>> +			unlock_all_vcpus(dev->kvm);
>> +			mutex_unlock(&dev->kvm->lock);
>> +			return ret;
>> +		}
>> +		break;
>> +	}
>>  	}
>>  	return -ENXIO;
>>  }
>> @@ -658,6 +676,8 @@ static int vgic_v3_has_attr(struct kvm_device *dev,
>>  		switch (attr->attr) {
>>  		case KVM_DEV_ARM_VGIC_CTRL_INIT:
>>  			return 0;
>> +		case KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES:
>> +			return 0;
>>  		}
>>  	}
>>  	return -ENXIO;
>> diff --git a/virt/kvm/arm/vgic/vgic-v3.c b/virt/kvm/arm/vgic/vgic-v3.c
>> index be0f4c3..1f0977f 100644
>> --- a/virt/kvm/arm/vgic/vgic-v3.c
>> +++ b/virt/kvm/arm/vgic/vgic-v3.c
>> @@ -15,6 +15,7 @@
>>  #include <linux/irqchip/arm-gic-v3.h>
>>  #include <linux/kvm.h>
>>  #include <linux/kvm_host.h>
>> +//#include <linux/bitops.h>
>>  #include <kvm/arm_vgic.h>
>>  #include <asm/kvm_mmu.h>
>>  #include <asm/kvm_asm.h>
>> @@ -252,6 +253,59 @@ void vgic_v3_enable(struct kvm_vcpu *vcpu)
>>  	vgic_v3->vgic_hcr = ICH_HCR_EN;
>>  }
>>  
>> +/**
>> + * vgic_its_save_pending_tables - Save the pending tables into guest RAM
>> + * kvm lock and all vcpu lock must be held
>> + */
>> +int vgic_v3_save_pending_tables(struct kvm *kvm)
>> +{
>> +	struct vgic_dist *dist = &kvm->arch.vgic;
>> +	int last_byte_offset = -1;
>> +	struct vgic_irq *irq;
>> +	int ret;
>> +
>> +	list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
>> +		int byte_offset, bit_nr;
>> +		struct kvm_vcpu *vcpu;
>> +		gpa_t pendbase, ptr;
>> +		bool stored;
>> +		u8 val;
>> +
>> +		vcpu = irq->target_vcpu;
>> +		if (!vcpu)
>> +			continue;
>> +
>> +		pendbase = GICR_PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser);
>> +
>> +		byte_offset = irq->intid / BITS_PER_BYTE;
>> +		bit_nr = irq->intid % BITS_PER_BYTE;
>> +		ptr = pendbase + byte_offset;
>> +
>> +		if (byte_offset != last_byte_offset) {
>> +			ret = kvm_read_guest(kvm, ptr, &val, 1);
>> +			if (ret)
>> +				return ret;
>> +			last_byte_offset = byte_offset;
>> +		}
>> +
>> +		stored = val & bit_nr;
> 
> didn't you mean 'stored = val & (1 << bit_nr)' ?
yes thanks!
> 
>> +		if (stored == irq->pending_latch)
>> +			continue;
>> +
>> +		if (irq->pending_latch)
>> +			val |= 1 << bit_nr;
>> +		else
>> +			val &= ~(1 << bit_nr);
>> +
>> +		ret = kvm_write_guest(kvm, ptr, &val, 1);
>> +		if (ret)
>> +			return ret;
>> +	}
> 
> This loop could probably be written a bit more efficiently and
> simplicity by reading the memory one word at a time (and remembering to
> do le64_to_cpu) and then doing something like:
> 
> 		if (irq->pending_latch)
> 			old = __test_and_set_bit(bit_nr, &val);
> 		else
> 			old = __test_and_clear_bit(bit_nr, &val);
> 
> 		if (old != val) {
> 			tmp = cpu_to_le64(val);
> 			ret = kvm_write_guest(kvm, ptr, &tmp,
> 					      sizeof(unsigned long));
> 			if (ret)
> 				retur ret;
> 		}
> 
> Further, you could also detect when the word_offset changes and write
> back the entire word with all its changes then, but you'd also have to
> check at the end of the loop then.  Not sure that's worth the
> optimization or easier to read than what you heave.  It's up to you.
I eventually chose to leave the code as it is today. One of the
rationale is it is pretty similar to the its_sync_lpi_pending_table
function in vgic-its.c.

Thanks

Eric
> 
> Thanks,
> -Christoffer
> 
>> +
>> +	return 0;
>> +}
>> +
>> +
>>  /* check for overlapping regions and for regions crossing the end of memory */
>>  static bool vgic_v3_check_base(struct kvm *kvm)
>>  {
>> diff --git a/virt/kvm/arm/vgic/vgic.h b/virt/kvm/arm/vgic/vgic.h
>> index 9bc52ef..535c2fc 100644
>> --- a/virt/kvm/arm/vgic/vgic.h
>> +++ b/virt/kvm/arm/vgic/vgic.h
>> @@ -177,6 +177,7 @@ void vgic_v3_get_vmcr(struct kvm_vcpu *vcpu, struct vgic_vmcr *vmcr);
>>  void vgic_v3_enable(struct kvm_vcpu *vcpu);
>>  int vgic_v3_probe(const struct gic_kvm_info *info);
>>  int vgic_v3_map_resources(struct kvm *kvm);
>> +int vgic_v3_save_pending_tables(struct kvm *kvm);
>>  int vgic_register_redist_iodevs(struct kvm *kvm, gpa_t dist_base_address);
>>  
>>  int vgic_register_its_iodevs(struct kvm *kvm);
>> -- 
>> 2.5.5
>>

  reply	other threads:[~2017-05-03 22:22 UTC|newest]

Thread overview: 132+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-14 10:15 [PATCH v5 00/22] vITS save/restore Eric Auger
2017-04-14 10:15 ` [PATCH v5 01/22] KVM: arm/arm64: Add ITS save/restore API documentation Eric Auger
2017-04-25 10:38   ` Peter Maydell
2017-04-26 12:31   ` Christoffer Dall
2017-04-26 15:48     ` Auger Eric
2017-04-27  8:57       ` Christoffer Dall
2017-04-27  9:33         ` Auger Eric
2017-04-27 11:02           ` Christoffer Dall
2017-04-27 12:51             ` Auger Eric
2017-04-27 14:45               ` Christoffer Dall
2017-04-27 15:29                 ` Auger Eric
2017-04-27 16:23                   ` Marc Zyngier
2017-04-27 17:14                     ` Auger Eric
2017-04-27 17:27                       ` Christoffer Dall
2017-04-27 16:38                   ` Christoffer Dall
2017-04-27 17:27                     ` Auger Eric
2017-04-27 17:54                       ` Christoffer Dall
2017-04-27 19:27                         ` Auger Eric
2017-05-04  7:00                 ` Auger Eric
2017-05-04  7:40                   ` Marc Zyngier
2017-05-04  7:54                     ` Auger Eric
2017-05-04  7:46                   ` Christoffer Dall
2017-04-14 10:15 ` [PATCH v5 02/22] KVM: arm/arm64: Add GICV3 pending table save " Eric Auger
2017-04-25 10:43   ` Peter Maydell
2017-04-26  8:26     ` Auger Eric
2017-04-26  8:44       ` Peter Maydell
2017-04-26  8:48         ` Dr. David Alan Gilbert
2017-04-26  9:57           ` Auger Eric
2017-04-26 13:00             ` Christoffer Dall
2017-04-26 13:01               ` Peter Maydell
2017-04-26 13:14                 ` Christoffer Dall
2017-04-26 13:26                   ` Peter Maydell
2017-04-26 14:47                     ` Auger Eric
2017-04-14 10:15 ` [PATCH v5 03/22] KVM: arm/arm64: vgic-its: rename itte into ite Eric Auger
2017-04-26 11:21   ` Prakash B
2017-04-27  9:05   ` Christoffer Dall
2017-04-27  9:20     ` Andre Przywara
2017-04-27  9:40       ` Auger Eric
2017-04-27 11:09         ` Christoffer Dall
2017-04-14 10:15 ` [PATCH v5 04/22] arm/arm64: vgic: turn vgic_find_mmio_region into public Eric Auger
2017-04-26 11:22   ` Prakash B
2017-04-27  9:07   ` Christoffer Dall
2017-04-14 10:15 ` [PATCH v5 05/22] KVM: arm64: vgic-its: KVM_DEV_ARM_VGIC_GRP_ITS_REGS group Eric Auger
2017-04-26 11:23   ` Prakash B
2017-04-27  9:12   ` Christoffer Dall
2017-04-14 10:15 ` [PATCH v5 06/22] KVM: arm/arm64: vgic: expose (un)lock_all_vcpus Eric Auger
2017-04-26 11:23   ` Prakash B
2017-04-27  9:18   ` Christoffer Dall
2017-04-14 10:15 ` [PATCH v5 07/22] KVM: arm64: vgic-its: Implement vgic_its_has_attr_regs and attr_regs_access Eric Auger
2017-04-26 11:24   ` Prakash B
2017-04-27 11:00   ` Christoffer Dall
2017-04-27 12:22     ` Auger Eric
2017-04-14 10:15 ` [PATCH v5 08/22] KVM: arm64: vgic-its: Implement vgic_mmio_uaccess_write_its_creadr Eric Auger
2017-04-26 11:24   ` Prakash B
2017-04-27 11:27   ` Christoffer Dall
2017-04-27 12:53     ` Auger Eric
2017-04-14 10:15 ` [PATCH v5 09/22] KVM: arm64: vgic-its: Introduce migration ABI infrastructure Eric Auger
2017-04-26 11:27   ` Prakash B
2017-04-27 13:14   ` Christoffer Dall
2017-04-14 10:15 ` [PATCH v5 10/22] KVM: arm64: vgic-its: Implement vgic_mmio_uaccess_write_its_iidr Eric Auger
2017-04-26 11:27   ` Prakash B
2017-04-27 14:57   ` Christoffer Dall
2017-04-14 10:15 ` [PATCH v5 11/22] KVM: arm64: vgic-its: Interpret MAPD Size field and check related errors Eric Auger
2017-04-26 11:28   ` Prakash B
2017-04-27 16:25   ` Christoffer Dall
2017-04-27 17:15     ` Auger Eric
2017-04-27 17:28       ` Christoffer Dall
2017-04-14 10:15 ` [PATCH v5 12/22] KVM: arm64: vgic-its: Interpret MAPD ITT_addr field Eric Auger
2017-04-26 11:29   ` Prakash B
2017-04-27 16:43   ` Christoffer Dall
2017-04-27 17:44     ` Auger Eric
2017-04-27 18:09       ` Christoffer Dall
2017-04-27 19:18         ` Auger Eric
2017-04-14 10:15 ` [PATCH v5 13/22] KVM: arm64: vgic-its: Check the device id matches TYPER DEVBITS range Eric Auger
2017-04-26 11:29   ` Prakash B
2017-04-27 16:48   ` Christoffer Dall
2017-04-27 17:24     ` Auger Eric
2017-04-14 10:15 ` [PATCH v5 14/22] KVM: arm64: vgic-its: KVM_DEV_ARM_ITS_SAVE/RESTORE_TABLES Eric Auger
2017-04-26 11:31   ` Prakash B
2017-04-27 17:24   ` Christoffer Dall
2017-04-14 10:15 ` [PATCH v5 15/22] KVM: arm64: vgic-its: vgic_its_alloc_ite/device Eric Auger
2017-04-26 11:31   ` Prakash B
2017-04-27 17:31   ` Christoffer Dall
2017-04-14 10:15 ` [PATCH v5 16/22] KVM: arm64: vgic-its: Add infrastructure for table lookup Eric Auger
2017-04-26 11:32   ` Prakash B
2017-04-27 18:06   ` Christoffer Dall
2017-04-27 19:24     ` Auger Eric
2017-04-28  9:47       ` Christoffer Dall
2017-04-30 19:33   ` Christoffer Dall
2017-05-03 13:40     ` Auger Eric
2017-05-03 14:38       ` Christoffer Dall
2017-04-30 19:35   ` Christoffer Dall
2017-05-03  6:53     ` Auger Eric
2017-05-03  8:01       ` Christoffer Dall
2017-05-03 10:22         ` Auger Eric
2017-04-30 20:13   ` Christoffer Dall
2017-04-14 10:15 ` [PATCH v5 17/22] KVM: arm64: vgic-its: Collection table save/restore Eric Auger
2017-04-26 11:33   ` Prakash B
2017-04-28 10:44   ` Christoffer Dall
2017-04-28 11:05     ` Auger Eric
2017-04-28 17:42       ` Christoffer Dall
2017-04-14 10:15 ` [PATCH v5 18/22] KVM: arm64: vgic-its: vgic_its_check_id returns the entry's GPA Eric Auger
2017-04-26 11:33   ` Prakash B
2017-05-02  8:29   ` Christoffer Dall
2017-04-14 10:15 ` [PATCH v5 19/22] KVM: arm64: vgic-its: ITT save and restore Eric Auger
2017-04-26 11:34   ` Prakash B
2017-04-30 20:14   ` Christoffer Dall
2017-05-03 16:08     ` Auger Eric
2017-05-03 16:37       ` Christoffer Dall
2017-05-03 21:55         ` Auger Eric
2017-05-04  7:31           ` Christoffer Dall
2017-05-04  7:40             ` Auger Eric
2017-05-04  8:23               ` Christoffer Dall
2017-05-04  8:44                 ` Auger Eric
2017-04-14 10:15 ` [PATCH v5 20/22] KVM: arm64: vgic-its: Device table save/restore Eric Auger
2017-04-26 11:34   ` Prakash B
2017-04-30 20:55   ` Christoffer Dall
2017-05-03 14:07     ` Auger Eric
2017-05-03 15:29       ` Christoffer Dall
2017-05-03 21:38         ` Auger Eric
2017-04-14 10:15 ` [PATCH v5 21/22] KVM: arm64: vgic-its: Fix pending table sync Eric Auger
2017-04-26 11:35   ` Prakash B
2017-04-30 21:10   ` Christoffer Dall
2017-05-03 22:20     ` Auger Eric
2017-05-04  7:32       ` Christoffer Dall
2017-04-14 10:15 ` [PATCH v5 22/22] KVM: arm64: vgic-v3: KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES Eric Auger
2017-04-26 11:35   ` Prakash B
2017-04-30 21:32   ` Christoffer Dall
2017-05-03 22:22     ` Auger Eric [this message]
2017-04-26 11:38 ` [PATCH v5 00/22] vITS save/restore Prakash B
2017-04-26 13:02   ` Christoffer Dall
2017-04-27  6:55   ` Auger Eric

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a236dc61-2454-835d-985e-db154661042c@redhat.com \
    --to=eric.auger@redhat.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).