Skip to contentSkip to conversation

vITS save/restore

PATCH Superseded Eric Auger 8 participants 132 messages Apr 14, 2017 – May 4, 2017 Page 2 of 2

Subsystems (6)
Documentation Arm (32-bit) KVM arm64 +2 more subsystems

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#112 lore raw

Re: [PATCH v5 19/22] KVM: arm64: vgic-its: ITT save and restore

In reply to Auger Eric
Quoted 266 lines · Auger Eric

Hi Christoffer,

Quoted 263 lines · Christoffer Dall
Quoted 261 lines · Auger Eric

Hi Christoffer,

Quoted 243 lines · Christoffer Dall
Quoted 237 lines · Auger Eric

Hi Christoffer,

Quoted 10 lines · Christoffer Dall
Quoted 5 lines · Eric Auger

Introduce routines to save and restore device ITT and their interrupt table entries (ITE).

The routines will be called on device table save and restore. They will become static in subsequent patches.

Why this bottom-up approach? Couldn't you start by having the patch that restores the device table and define the static functions that return an error there

done
, and then fill them in with subsequent patches

Quoted 94 lines

(liek this one)?

That would have the added benefit of being able to tell how things are designed to be called.

Quoted 89 lines

---
v4 -> v5:
- ITE are now sorted by eventid on the flush
- rename *flush* into *save*
- use macros for shits and masks
- pass ite_esz to vgic_its_save_ite

v3 -> v4:
- lookup_table and compute_next_eventid_offset become static in this patch
- remove static along with vgic_its_flush/restore_itt to avoid compilation warnings
- next field only computed with a shift (mask removed)
- handle the case where the last element has not been found

v2 -> v3:
- add return 0 in vgic_its_restore_ite (was in subsequent patch)

v2: creation

---
 virt/kvm/arm/vgic/vgic-its.c | 128 ++++++++++++++++++++++++++++++++++++++++++-
 virt/kvm/arm/vgic/vgic.h     |   4 ++
 2 files changed, 129 insertions(+), 3 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +30 -3
index 35b2ca1..b02fc3f 100644
@@ -23,6 +23,7 @@
 #include <linux/interrupt.h>
 #include <linux/list.h>
 #include <linux/uaccess.h>
+#include <linux/list_sort.h>
 
 #include <linux/irqchip/arm-gic-v3.h>
 
@@ -1695,7 +1696,7 @@ u32 compute_next_devid_offset(struct list_head *h, struct its_device *dev)
 	return min_t(u32, next_offset, VITS_DTE_MAX_DEVID_OFFSET);
 }
 
-u32 compute_next_eventid_offset(struct list_head *h, struct its_ite *ite)
+static u32 compute_next_eventid_offset(struct list_head *h, struct its_ite *ite)
 {
 	struct list_head *e = &ite->ite_list;
 	struct its_ite *next;
@@ -1737,8 +1738,8 @@ typedef int (*entry_fn_t)(struct vgic_its *its, u32 id, void *entry,
  *
  * Return: < 0 on error, 1 if last element identified, 0 otherwise
  */
-int lookup_table(struct vgic_its *its, gpa_t base, int size, int esz,
-		 int start_id, entry_fn_t fn, void *opaque)
+static int lookup_table(struct vgic_its *its, gpa_t base, int size, int esz,
+			int start_id, entry_fn_t fn, void *opaque)
 {
 	void *entry = kzalloc(esz, GFP_KERNEL);
 	struct kvm *kvm = its->dev->kvm;
@@ -1773,6 +1774,127 @@ int lookup_table(struct vgic_its *its, gpa_t base, int size, int esz,
 }
 
 /**
+ * vgic_its_save_ite - Save an interrupt translation entry at @gpa
+ */
+static int vgic_its_save_ite(struct vgic_its *its, struct its_device *dev,
+			      struct its_ite *ite, gpa_t gpa, int ite_esz)
+{
+	struct kvm *kvm = its->dev->kvm;
+	u32 next_offset;
+	u64 val;
+
+	next_offset = compute_next_eventid_offset(&dev->itt_head, ite);
+	val = ((u64)next_offset << KVM_ITS_ITE_NEXT_SHIFT) |
+	       ((u64)ite->lpi << KVM_ITS_ITE_PINTID_SHIFT) |
+		ite->collection->collection_id;
+	val = cpu_to_le64(val);
+	return kvm_write_guest(kvm, gpa, &val, ite_esz);
+}
+
+/**
+ * vgic_its_restore_ite - restore an interrupt translation entry
+ * @event_id: id used for indexing
+ * @ptr: pointer to the ITE entry
+ * @opaque: pointer to the its_device
+ * @next: id offset to the next entry
+ */
+static int vgic_its_restore_ite(struct vgic_its *its, u32 event_id,
+				void *ptr, void *opaque, u32 *next)
Quoted 8 lines

virt/kvm/arm/vgic/vgic-its.c int lookup_table(struct vgic_its *its, gpa_t base, int size, int esz, · line 1807

Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +5 -0
+{
+	struct its_device *dev = (struct its_device *)opaque;
+	struct its_collection *collection;
+	struct kvm *kvm = its->dev->kvm;
+	u64 val, *p = (u64 *)ptr;

nit: initializations on separate line (and possible do that just above assigning val).

done

Quoted 11 lines
Quoted 11 lines
virt/kvm/arm/vgic/vgic-its.c +10 -0
+	struct vgic_irq *irq;
+	u32 coll_id, lpi_id;
+	struct its_ite *ite;
+	int ret;
+
+	val = *p;
+	*next = 1;
+
+	val = le64_to_cpu(val);
+
Quoted 8 lines

virt/kvm/arm/vgic/vgic-its.c int lookup_table(struct vgic_its *its, gpa_t base, int size, int esz, · line 1822

Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +5 -0
+	coll_id = val & KVM_ITS_ITE_ICID_MASK;
+	lpi_id = (val & KVM_ITS_ITE_PINTID_MASK) >> KVM_ITS_ITE_PINTID_SHIFT;
+
+	if (!lpi_id)
+		return 0;

are all non-zero LPI IDs valid? Don't we have a wrapper that tests if the ID is valid?

no, lpi_id must be >= GIC_MIN_LPI=8192; added that check. ABI Doc says lpi_id==0 is interpreted as invalid. Other values < GIC_MIN_LPI cause an -EINVAL error

Quoted 10 lines

(looks like it's possible to add LPIs with the INTID range of SPIs, SGIs and PPIs here)

virt/kvm/arm/vgic/vgic-its.c int lookup_table(struct vgic_its *its, gpa_t base, int size, int esz, · line 1824

Quoted 2 lines
virt/kvm/arm/vgic/vgic-its.c +2 -0
+
+	*next = val >> KVM_ITS_ITE_NEXT_SHIFT;

Don't we need to validate this somehow since it will presumably be used to forward a pointer somehow by the caller?

checked against max number of eventids supported by the device

Quoted 37 lines
Quoted 37 lines
virt/kvm/arm/vgic/vgic-its.c +36 -0
+
+	collection = find_collection(its, coll_id);
+	if (!collection)
+		return -EINVAL;
+
+	ret = vgic_its_alloc_ite(dev, &ite, collection,
+				  lpi_id, event_id);
+	if (ret)
+		return ret;
+
+	irq = vgic_add_lpi(kvm, lpi_id);
+	if (IS_ERR(irq))
+		return PTR_ERR(irq);
+	ite->irq = irq;
+
+	/* restore the configuration of the LPI */
+	ret = update_lpi_config(kvm, irq, NULL);
+	if (ret)
+		return ret;
+
+	update_affinity_ite(kvm, ite);
+	return 0;
+}
+
+static int vgic_its_ite_cmp(void *priv, struct list_head *a,
+			    struct list_head *b)
+{
+	struct its_ite *itea = container_of(a, struct its_ite, ite_list);
+	struct its_ite *iteb = container_of(b, struct its_ite, ite_list);
+
+	if (itea->event_id < iteb->event_id)
+		return -1;
+	else
+		return 1;
+}
+
Quoted 8 lines

virt/kvm/arm/vgic/vgic-its.c int lookup_table(struct vgic_its *its, gpa_t base, int size, int esz, · line 1866

Quoted 6 lines
virt/kvm/arm/vgic/vgic-its.c +6 -0
+int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
+{
+	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
+	gpa_t base = device->itt_addr;
+	struct its_ite *ite;
+	int ret, ite_esz = abi->ite_esz;

nit: initializations on separate line

OK

Quoted 14 lines
Quoted 14 lines
virt/kvm/arm/vgic/vgic-its.c +13 -0
+
+	list_sort(NULL, &device->itt_head, vgic_its_ite_cmp);
+
+	list_for_each_entry(ite, &device->itt_head, ite_list) {
+		gpa_t gpa = base + ite->event_id * ite_esz;
+
+		ret = vgic_its_save_ite(its, device, ite, gpa, ite_esz);
+		if (ret)
+			return ret;
+	}
+	return 0;
+}
+
Quoted 8 lines

virt/kvm/arm/vgic/vgic-its.c int lookup_table(struct vgic_its *its, gpa_t base, int size, int esz, · line 1885

Quoted 6 lines
virt/kvm/arm/vgic/vgic-its.c +6 -0
+int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
+{
+	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
+	gpa_t base = dev->itt_addr;
+	int ret, ite_esz = abi->ite_esz;
+	size_t max_size = BIT_ULL(dev->nb_eventid_bits) * ite_esz;

nit: initializations on separate line

OK

Quoted 7 lines

virt/kvm/arm/vgic/vgic-its.c int lookup_table(struct vgic_its *its, gpa_t base, int size, int esz, · line 1888

Quoted 4 lines
virt/kvm/arm/vgic/vgic-its.c +3 -0
+
+	ret =  lookup_table(its, base, max_size, ite_esz, 0,
+			    vgic_its_restore_ite, dev);

nit: extra white space

Quoted 10 lines

virt/kvm/arm/vgic/vgic-its.c int lookup_table(struct vgic_its *its, gpa_t base, int size, int esz, · line 1894

Quoted 6 lines
virt/kvm/arm/vgic/vgic-its.c +6 -0
+
+	if (ret < 0)
+		return ret;
+
+	/* if the last element has not been found we are in trouble */
+	return ret ? 0 : -EINVAL;

hmm, these are values potentially created by the guest in guest RAM, right? So do we really abort migration and return an error to userspace in this case?

Quoted 4 lines

So we discussed with Peter/dave we shouldn't abort() in qemu in case of such error. The restore table IOCTL will return an error. Up to qemu to print the error. Destination guest will not be functional though.

Quoted 4 lines

ok, I'm just wondering if userspace can make a qualified decision based on this error code. EINVAL typically means that userspace provided something incorrect, which I suppose in a sense is true, but this should be the only case where we return EINVAL here.

Userspace must be able to

Quoted 4 lines

tell the cases apart where the guest programmed bogus into memory before migration started, in which case we should ignore-and-resume, and where QEMU errornously provide some bogus value where the machine state becomes unreliable and must be powered down.

guest does not feed much besides few registers the ITS table restore depends on. In case we want a more subtle error management at userspace level all the error codes need to be revisited I am afraid. My plan was to be more rough at the beginning and ignore & resume if ITS table restore fails.

Quoted 7 lines
Quoted 4 lines

Do we require that the VM is quiesced the entire time between saving the ITS state to memory and copying all memory over the wire and capturing all register state? If so, then an error to restore would be because of userspace doing something wrong and handling that accordingly is fine.

yes the ITS table save into RAM starts when we have a guarantee that all the VCPUS are stopped (we take all locks).

The important bit is whether or not userspace is allowed to start any VCPUs again before copying over all RAM etc. I suppose not.

Quoted 3 lines

The restore happens before
the VM gets resumed. At least this is the QEMU integration as of today.

Does our ABI mandate this behavior (document it somewhere) ?

Thanks,
-Christoffer

author
#113 lore raw

Re: [PATCH v5 19/22] KVM: arm64: vgic-its: ITT save and restore

Hi,

Quoted 272 lines · Christoffer Dall
Quoted 271 lines · Auger Eric

Hi Christoffer,

Quoted 267 lines · Christoffer Dall
Quoted 255 lines · Auger Eric

Hi Christoffer,

Quoted 243 lines · Christoffer Dall
Quoted 237 lines · Auger Eric

Hi Christoffer,

Quoted 10 lines · Christoffer Dall
Quoted 5 lines · Eric Auger

Introduce routines to save and restore device ITT and their interrupt table entries (ITE).

The routines will be called on device table save and restore. They will become static in subsequent patches.

Why this bottom-up approach? Couldn't you start by having the patch that restores the device table and define the static functions that return an error there

done
, and then fill them in with subsequent patches

Quoted 94 lines

(liek this one)?

That would have the added benefit of being able to tell how things are designed to be called.

Quoted 89 lines

---
v4 -> v5:
- ITE are now sorted by eventid on the flush
- rename *flush* into *save*
- use macros for shits and masks
- pass ite_esz to vgic_its_save_ite

v3 -> v4:
- lookup_table and compute_next_eventid_offset become static in this patch
- remove static along with vgic_its_flush/restore_itt to avoid compilation warnings
- next field only computed with a shift (mask removed)
- handle the case where the last element has not been found

v2 -> v3:
- add return 0 in vgic_its_restore_ite (was in subsequent patch)

v2: creation

---
 virt/kvm/arm/vgic/vgic-its.c | 128 ++++++++++++++++++++++++++++++++++++++++++-
 virt/kvm/arm/vgic/vgic.h     |   4 ++
 2 files changed, 129 insertions(+), 3 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +30 -3
index 35b2ca1..b02fc3f 100644
@@ -23,6 +23,7 @@
 #include <linux/interrupt.h>
 #include <linux/list.h>
 #include <linux/uaccess.h>
+#include <linux/list_sort.h>
 
 #include <linux/irqchip/arm-gic-v3.h>
 
@@ -1695,7 +1696,7 @@ u32 compute_next_devid_offset(struct list_head *h, struct its_device *dev)
 	return min_t(u32, next_offset, VITS_DTE_MAX_DEVID_OFFSET);
 }
 
-u32 compute_next_eventid_offset(struct list_head *h, struct its_ite *ite)
+static u32 compute_next_eventid_offset(struct list_head *h, struct its_ite *ite)
 {
 	struct list_head *e = &ite->ite_list;
 	struct its_ite *next;
@@ -1737,8 +1738,8 @@ typedef int (*entry_fn_t)(struct vgic_its *its, u32 id, void *entry,
  *
  * Return: < 0 on error, 1 if last element identified, 0 otherwise
  */
-int lookup_table(struct vgic_its *its, gpa_t base, int size, int esz,
-		 int start_id, entry_fn_t fn, void *opaque)
+static int lookup_table(struct vgic_its *its, gpa_t base, int size, int esz,
+			int start_id, entry_fn_t fn, void *opaque)
 {
 	void *entry = kzalloc(esz, GFP_KERNEL);
 	struct kvm *kvm = its->dev->kvm;
@@ -1773,6 +1774,127 @@ int lookup_table(struct vgic_its *its, gpa_t base, int size, int esz,
 }
 
 /**
+ * vgic_its_save_ite - Save an interrupt translation entry at @gpa
+ */
+static int vgic_its_save_ite(struct vgic_its *its, struct its_device *dev,
+			      struct its_ite *ite, gpa_t gpa, int ite_esz)
+{
+	struct kvm *kvm = its->dev->kvm;
+	u32 next_offset;
+	u64 val;
+
+	next_offset = compute_next_eventid_offset(&dev->itt_head, ite);
+	val = ((u64)next_offset << KVM_ITS_ITE_NEXT_SHIFT) |
+	       ((u64)ite->lpi << KVM_ITS_ITE_PINTID_SHIFT) |
+		ite->collection->collection_id;
+	val = cpu_to_le64(val);
+	return kvm_write_guest(kvm, gpa, &val, ite_esz);
+}
+
+/**
+ * vgic_its_restore_ite - restore an interrupt translation entry
+ * @event_id: id used for indexing
+ * @ptr: pointer to the ITE entry
+ * @opaque: pointer to the its_device
+ * @next: id offset to the next entry
+ */
+static int vgic_its_restore_ite(struct vgic_its *its, u32 event_id,
+				void *ptr, void *opaque, u32 *next)
Quoted 8 lines

virt/kvm/arm/vgic/vgic-its.c int lookup_table(struct vgic_its *its, gpa_t base, int size, int esz, · line 1807

Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +5 -0
+{
+	struct its_device *dev = (struct its_device *)opaque;
+	struct its_collection *collection;
+	struct kvm *kvm = its->dev->kvm;
+	u64 val, *p = (u64 *)ptr;

nit: initializations on separate line (and possible do that just above assigning val).

done

Quoted 11 lines
Quoted 11 lines
virt/kvm/arm/vgic/vgic-its.c +10 -0
+	struct vgic_irq *irq;
+	u32 coll_id, lpi_id;
+	struct its_ite *ite;
+	int ret;
+
+	val = *p;
+	*next = 1;
+
+	val = le64_to_cpu(val);
+
Quoted 8 lines

virt/kvm/arm/vgic/vgic-its.c int lookup_table(struct vgic_its *its, gpa_t base, int size, int esz, · line 1822

Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +5 -0
+	coll_id = val & KVM_ITS_ITE_ICID_MASK;
+	lpi_id = (val & KVM_ITS_ITE_PINTID_MASK) >> KVM_ITS_ITE_PINTID_SHIFT;
+
+	if (!lpi_id)
+		return 0;

are all non-zero LPI IDs valid? Don't we have a wrapper that tests if the ID is valid?

no, lpi_id must be >= GIC_MIN_LPI=8192; added that check. ABI Doc says lpi_id==0 is interpreted as invalid. Other values < GIC_MIN_LPI cause an -EINVAL error

Quoted 10 lines

(looks like it's possible to add LPIs with the INTID range of SPIs, SGIs and PPIs here)

virt/kvm/arm/vgic/vgic-its.c int lookup_table(struct vgic_its *its, gpa_t base, int size, int esz, · line 1824

Quoted 2 lines
virt/kvm/arm/vgic/vgic-its.c +2 -0
+
+	*next = val >> KVM_ITS_ITE_NEXT_SHIFT;

Don't we need to validate this somehow since it will presumably be used to forward a pointer somehow by the caller?

checked against max number of eventids supported by the device

Quoted 37 lines
Quoted 37 lines
virt/kvm/arm/vgic/vgic-its.c +36 -0
+
+	collection = find_collection(its, coll_id);
+	if (!collection)
+		return -EINVAL;
+
+	ret = vgic_its_alloc_ite(dev, &ite, collection,
+				  lpi_id, event_id);
+	if (ret)
+		return ret;
+
+	irq = vgic_add_lpi(kvm, lpi_id);
+	if (IS_ERR(irq))
+		return PTR_ERR(irq);
+	ite->irq = irq;
+
+	/* restore the configuration of the LPI */
+	ret = update_lpi_config(kvm, irq, NULL);
+	if (ret)
+		return ret;
+
+	update_affinity_ite(kvm, ite);
+	return 0;
+}
+
+static int vgic_its_ite_cmp(void *priv, struct list_head *a,
+			    struct list_head *b)
+{
+	struct its_ite *itea = container_of(a, struct its_ite, ite_list);
+	struct its_ite *iteb = container_of(b, struct its_ite, ite_list);
+
+	if (itea->event_id < iteb->event_id)
+		return -1;
+	else
+		return 1;
+}
+
Quoted 8 lines

virt/kvm/arm/vgic/vgic-its.c int lookup_table(struct vgic_its *its, gpa_t base, int size, int esz, · line 1866

Quoted 6 lines
virt/kvm/arm/vgic/vgic-its.c +6 -0
+int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
+{
+	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
+	gpa_t base = device->itt_addr;
+	struct its_ite *ite;
+	int ret, ite_esz = abi->ite_esz;

nit: initializations on separate line

OK

Quoted 14 lines
Quoted 14 lines
virt/kvm/arm/vgic/vgic-its.c +13 -0
+
+	list_sort(NULL, &device->itt_head, vgic_its_ite_cmp);
+
+	list_for_each_entry(ite, &device->itt_head, ite_list) {
+		gpa_t gpa = base + ite->event_id * ite_esz;
+
+		ret = vgic_its_save_ite(its, device, ite, gpa, ite_esz);
+		if (ret)
+			return ret;
+	}
+	return 0;
+}
+
Quoted 8 lines

virt/kvm/arm/vgic/vgic-its.c int lookup_table(struct vgic_its *its, gpa_t base, int size, int esz, · line 1885

Quoted 6 lines
virt/kvm/arm/vgic/vgic-its.c +6 -0
+int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
+{
+	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
+	gpa_t base = dev->itt_addr;
+	int ret, ite_esz = abi->ite_esz;
+	size_t max_size = BIT_ULL(dev->nb_eventid_bits) * ite_esz;

nit: initializations on separate line

OK

Quoted 7 lines

virt/kvm/arm/vgic/vgic-its.c int lookup_table(struct vgic_its *its, gpa_t base, int size, int esz, · line 1888

Quoted 4 lines
virt/kvm/arm/vgic/vgic-its.c +3 -0
+
+	ret =  lookup_table(its, base, max_size, ite_esz, 0,
+			    vgic_its_restore_ite, dev);

nit: extra white space

Quoted 10 lines

virt/kvm/arm/vgic/vgic-its.c int lookup_table(struct vgic_its *its, gpa_t base, int size, int esz, · line 1894

Quoted 6 lines
virt/kvm/arm/vgic/vgic-its.c +6 -0
+
+	if (ret < 0)
+		return ret;
+
+	/* if the last element has not been found we are in trouble */
+	return ret ? 0 : -EINVAL;

hmm, these are values potentially created by the guest in guest RAM, right? So do we really abort migration and return an error to userspace in this case?

Quoted 4 lines

So we discussed with Peter/dave we shouldn't abort() in qemu in case of such error. The restore table IOCTL will return an error. Up to qemu to print the error. Destination guest will not be functional though.

Quoted 4 lines

ok, I'm just wondering if userspace can make a qualified decision based on this error code. EINVAL typically means that userspace provided something incorrect, which I suppose in a sense is true, but this should be the only case where we return EINVAL here.

Userspace must be able to

Quoted 4 lines

tell the cases apart where the guest programmed bogus into memory before migration started, in which case we should ignore-and-resume, and where QEMU errornously provide some bogus value where the machine state becomes unreliable and must be powered down.

Quoted 6 lines

guest does not feed much besides few registers the ITS table restore depends on. In case we want a more subtle error management at userspace level all the error codes need to be revisited I am afraid. My plan was to be more rough at the beginning and ignore & resume if ITS table restore fails.

Do we require that the VM is quiesced the entire time between saving the ITS state to memory and copying all memory over the wire and capturing all register state? If so, then an error to restore would be because of userspace doing something wrong and handling that accordingly is fine.

Quoted 5 lines
Quoted 2 lines

yes the ITS table save into RAM starts when we have a guarantee that all the VCPUS are stopped (we take all locks).

The important bit is whether or not userspace is allowed to start any VCPUs again before copying over all RAM etc. I suppose not.

no it is not meant to happen.

Quoted 6 lines
Quoted 4 lines

The restore happens before
the VM gets resumed. At least this is the QEMU integration as of today.

Does our ABI mandate this behavior (document it somewhere) ?

I will add this

Thanks

Eric

Quoted 9 lines

Thanks,
-Christoffer

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

author
#114 lore raw

PATCH v5 20/22 KVM: arm64: vgic-its: Device table save/restore

In reply to Eric Auger

This patch saves the device table entries into guest RAM. Both flat table and 2 stage tables are supported. DeviceId indexing is used.

For each device listed in the device table, we also save the translation table using the vgic_its_save/restore_itt routines.

On restore, devices are re-allocated and their ite are re-built.

---
v4 -> v5:
- sort the device list by deviceid on device table save
- use defines for shifts and masks
- use abi->dte_esz
- clatify entry sizes for L1 and L2 tables

v3 -> v4:
- use the new proto for its_alloc_device
- compute_next_devid_offset, vgic_its_flush/restore_itt become static in this patch
- change in the DTE entry format with the introduction of the valid bit and next field width decrease; ittaddr encoded on its full range
- fix handle_l1_entry entry handling
- correct vgic_its_table_restore error handling

v2 -> v3:
- fix itt_addr bitmask in vgic_its_restore_dte
- addition of return 0 in vgic_its_restore_ite moved to the ITE related patch

v1 -> v2:
- use 8 byte format for DTE and ITE
- support 2 stage format
- remove kvm parameter
- ITT flush/restore moved in a separate patch
- use deviceid indexing

---
 virt/kvm/arm/vgic/vgic-its.c | 183 +++++++++++++++++++++++++++++++++++++++++--
 virt/kvm/arm/vgic/vgic.h     |   7 ++
 2 files changed, 185 insertions(+), 5 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +178 -5
index b02fc3f..86dfc6c 100644
@@ -1682,7 +1682,8 @@ int vgic_its_attr_regs_access(struct kvm_device *dev,
 	return ret;
 }
 
-u32 compute_next_devid_offset(struct list_head *h, struct its_device *dev)
+static u32 compute_next_devid_offset(struct list_head *h,
+				     struct its_device *dev)
 {
 	struct list_head *e = &dev->dev_list;
 	struct its_device *next;
@@ -1858,7 +1859,7 @@ static int vgic_its_ite_cmp(void *priv, struct list_head *a,
 		return 1;
 }
 
-int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
+static int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
 {
 	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
 	gpa_t base = device->itt_addr;
@@ -1877,7 +1878,7 @@ int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
 	return 0;
 }
 
-int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
+static int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
 {
 	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
 	gpa_t base = dev->itt_addr;
@@ -1895,12 +1896,161 @@ int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
 }
 
 /**
+ * vgic_its_save_dte - Save a device table entry at a given GPA
+ *
+ * @its: ITS handle
+ * @dev: ITS device
+ * @ptr: GPA
+ */
+static int vgic_its_save_dte(struct vgic_its *its, struct its_device *dev,
+			     gpa_t ptr, int dte_esz)
+{
+	struct kvm *kvm = its->dev->kvm;
+	u64 val, itt_addr_field;
+	int ret;
+	u32 next_offset;
+
+	itt_addr_field = dev->itt_addr >> 8;
+	next_offset = compute_next_devid_offset(&its->device_list, dev);
+	val = (1ULL << KVM_ITS_DTE_VALID_SHIFT |
+	       ((u64)next_offset << KVM_ITS_DTE_NEXT_SHIFT) |
+	       (itt_addr_field << KVM_ITS_DTE_ITTADDR_SHIFT) |
+		(dev->nb_eventid_bits - 1));
+	val = cpu_to_le64(val);
+	ret = kvm_write_guest(kvm, ptr, &val, dte_esz);
+	return ret;
+}
+
+/**
+ * vgic_its_restore_dte - restore a device table entry
+ *
+ * @its: its handle
+ * @id: device id the DTE corresponds to
+ * @ptr: kernel VA where the 8 byte DTE is located
+ * @opaque: unused
+ * @next: offset to the next valid device id
+ *
+ * Return: < 0 on error, 0 otherwise
+ */
+static int vgic_its_restore_dte(struct vgic_its *its, u32 id,
+				void *ptr, void *opaque, u32 *next)
+{
+	struct its_device *dev;
+	gpa_t itt_addr;
+	u8 nb_eventid_bits;
+	u64 entry = *(u64 *)ptr;
+	bool valid;
+	int ret;
+
+	entry = le64_to_cpu(entry);
+
+	valid = entry >> KVM_ITS_DTE_VALID_SHIFT;
+	nb_eventid_bits = (entry & KVM_ITS_DTE_SIZE_MASK) + 1;
+	itt_addr = ((entry & KVM_ITS_DTE_ITTADDR_MASK)
+			>> KVM_ITS_DTE_ITTADDR_SHIFT) << 8;
+	*next = 1;
+
+	if (!valid)
+		return 0;
+
+	/* dte entry is valid */
+	*next = (entry & KVM_ITS_DTE_NEXT_MASK) >> KVM_ITS_DTE_NEXT_SHIFT;
+
+	ret = vgic_its_alloc_device(its, &dev, id,
+				    itt_addr, nb_eventid_bits);
+	if (ret)
+		return ret;
+	ret = vgic_its_restore_itt(its, dev);
+
+	return ret;
+}
+
+static int vgic_its_device_cmp(void *priv, struct list_head *a,
+			       struct list_head *b)
+{
+	struct its_device *deva = container_of(a, struct its_device, dev_list);
+	struct its_device *devb = container_of(b, struct its_device, dev_list);
+
+	if (deva->device_id < devb->device_id)
+		return -1;
+	else
+		return 1;
+}
+
+/**
  * vgic_its_save_device_tables - Save the device table and all ITT
  * into guest RAM
+ *
+ * L1/L2 handling is hidden by vgic_its_check_id() helper which directly
+ * returns the GPA of the device entry
  */
 static int vgic_its_save_device_tables(struct vgic_its *its)
 {
-	return -ENXIO;
+	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
+	struct its_device *dev;
+	int dte_esz = abi->dte_esz;
+	u64 baser;
+
+	baser = its->baser_device_table;
+
+	list_sort(NULL, &its->device_list, vgic_its_device_cmp);
+
+	list_for_each_entry(dev, &its->device_list, dev_list) {
+		int ret;
+		gpa_t eaddr;
+
+		if (!vgic_its_check_id(its, baser,
+				       dev->device_id, &eaddr))
+			return -EINVAL;
+
+		ret = vgic_its_save_itt(its, dev);
+		if (ret)
+			return ret;
+
+		ret = vgic_its_save_dte(its, dev, eaddr, dte_esz);
+		if (ret)
+			return ret;
+	}
+	return 0;
+}
+
+/**
+ * handle_l1_entry - callback used for L1 entries (2 stage case)
+ *
+ * @its: its handle
+ * @id: id
+ * @addr: kernel VA
+ * @opaque: unused
+ * @next_offset: offset to the next L1 entry: 0 if the last element
+ * was found, 1 otherwise
+ */
+static int handle_l1_entry(struct vgic_its *its, u32 id, void *addr,
+			   void *opaque, u32 *next_offset)
+{
+	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
+	int l2_start_id = id * (SZ_64K / GITS_LVL1_ENTRY_SIZE);
+	u64 entry = *(u64 *)addr;
+	int ret, ite_esz = abi->ite_esz;
+	gpa_t gpa;
+
+	entry = le64_to_cpu(entry);
+	*next_offset = 1;
+
+	if (!(entry & BIT_ULL(63)))
+		return 0;
+
+	gpa = entry & GENMASK_ULL(51, 16);
+
+	ret = lookup_table(its, gpa, SZ_64K, ite_esz,
+			   l2_start_id, vgic_its_restore_dte, NULL);
+
+	if (ret == 1) {
+		/* last entry was found in this L2 table */
+		*next_offset = 0;
+		ret = 0;
+	}
+
+	return ret;
 }
 
 /**
@@ -1909,7 +2059,30 @@ static int vgic_its_save_device_tables(struct vgic_its *its)
  */
 static int vgic_its_restore_device_tables(struct vgic_its *its)
 {
-	return -ENXIO;
+	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
+	u64 baser = its->baser_device_table;
+	int l1_esz, ret, l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
+	gpa_t l1_gpa;
+
+	l1_gpa = BASER_ADDRESS(baser);
+	if (!l1_gpa)
+		return 0;
+
+	if (baser & GITS_BASER_INDIRECT) {
+		l1_esz = 8;
+		ret = lookup_table(its, l1_gpa, l1_tbl_size, l1_esz, 0,
+				   handle_l1_entry, NULL);
+	} else {
+		l1_esz = abi->dte_esz;
+		ret = lookup_table(its, l1_gpa, l1_tbl_size, l1_esz, 0,
+				   vgic_its_restore_dte, NULL);
+	}
+
+	if (ret < 0)
+		return ret;
+
+	/* if last element was not found we have an issue here */
+	return ret ? 0 : -EINVAL;
 }
 
 static int vgic_its_save_cte(struct vgic_its *its,
virt/kvm/arm/vgic/vgic.h +7 -0
index ce57fbd..9bc52ef 100644
@@ -85,6 +85,13 @@
 #define KVM_ITS_ITE_PINTID_SHIFT	16
 #define KVM_ITS_ITE_PINTID_MASK		GENMASK_ULL(47, 16)
 #define KVM_ITS_ITE_ICID_MASK		GENMASK_ULL(15, 0)
+#define KVM_ITS_DTE_VALID_SHIFT		63
+#define KVM_ITS_DTE_VALID_MASK		BIT_ULL(63)
+#define KVM_ITS_DTE_NEXT_SHIFT		49
+#define KVM_ITS_DTE_NEXT_MASK		GENMASK_ULL(62, 49)
+#define KVM_ITS_DTE_ITTADDR_SHIFT	5
+#define KVM_ITS_DTE_ITTADDR_MASK	GENMASK_ULL(48, 5)
+#define KVM_ITS_DTE_SIZE_MASK		GENMASK_ULL(4, 0)
 
 static inline bool irq_is_pending(struct vgic_irq *irq)
 {
Signature · 1 line
2.5.5
Tested-by Prakash, Brahmajyosyula on v5 20/22
#115 lore raw

Re: [PATCH v5 20/22] KVM: arm64: vgic-its: Device table save/restore

Quoted 4 lines · Eric Auger

This patch saves the device table entries into guest RAM. Both flat table and 2 stage tables are supported. DeviceId indexing is used.

Quoted 8 lines

For each device listed in the device table, we also save the translation table using the vgic_its_save/restore_itt routines.

On restore, devices are re-allocated and their ite are re-built.

#116 lore raw

Re: [PATCH v5 20/22] KVM: arm64: vgic-its: Device table save/restore

In reply to Eric Auger
Quoted 90 lines · Eric Auger

This patch saves the device table entries into guest RAM. Both flat table and 2 stage tables are supported. DeviceId indexing is used.

For each device listed in the device table, we also save the translation table using the vgic_its_save/restore_itt routines.

On restore, devices are re-allocated and their ite are re-built.

---
v4 -> v5:
- sort the device list by deviceid on device table save
- use defines for shifts and masks
- use abi->dte_esz
- clatify entry sizes for L1 and L2 tables

v3 -> v4:
- use the new proto for its_alloc_device
- compute_next_devid_offset, vgic_its_flush/restore_itt become static in this patch
- change in the DTE entry format with the introduction of the valid bit and next field width decrease; ittaddr encoded on its full range
- fix handle_l1_entry entry handling
- correct vgic_its_table_restore error handling

v2 -> v3:
- fix itt_addr bitmask in vgic_its_restore_dte
- addition of return 0 in vgic_its_restore_ite moved to the ITE related patch

v1 -> v2:
- use 8 byte format for DTE and ITE
- support 2 stage format
- remove kvm parameter
- ITT flush/restore moved in a separate patch
- use deviceid indexing

---
 virt/kvm/arm/vgic/vgic-its.c | 183 +++++++++++++++++++++++++++++++++++++++++--
 virt/kvm/arm/vgic/vgic.h     |   7 ++
 2 files changed, 185 insertions(+), 5 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +12 -3
index b02fc3f..86dfc6c 100644
@@ -1682,7 +1682,8 @@ int vgic_its_attr_regs_access(struct kvm_device *dev,
 	return ret;
 }
 
-u32 compute_next_devid_offset(struct list_head *h, struct its_device *dev)
+static u32 compute_next_devid_offset(struct list_head *h,
+				     struct its_device *dev)
 {
 	struct list_head *e = &dev->dev_list;
 	struct its_device *next;
@@ -1858,7 +1859,7 @@ static int vgic_its_ite_cmp(void *priv, struct list_head *a,
 		return 1;
 }
 
-int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
+static int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
 {
 	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
 	gpa_t base = device->itt_addr;
@@ -1877,7 +1878,7 @@ int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
 	return 0;
 }
 
-int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
+static int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
 {
 	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
 	gpa_t base = dev->itt_addr;
@@ -1895,12 +1896,161 @@ int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
 }
 
 /**
+ * vgic_its_save_dte - Save a device table entry at a given GPA
+ *
+ * @its: ITS handle
+ * @dev: ITS device
+ * @ptr: GPA
+ */
+static int vgic_its_save_dte(struct vgic_its *its, struct its_device *dev,
+			     gpa_t ptr, int dte_esz)

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 1916

Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10 -0
+{
+	struct kvm *kvm = its->dev->kvm;
+	u64 val, itt_addr_field;
+	int ret;
+	u32 next_offset;
+
+	itt_addr_field = dev->itt_addr >> 8;
+	next_offset = compute_next_devid_offset(&its->device_list, dev);
+	val = (1ULL << KVM_ITS_DTE_VALID_SHIFT |
+	       ((u64)next_offset << KVM_ITS_DTE_NEXT_SHIFT) |

I think this implies that the next field in your ABI points to the next offset, regardless of whether or not this is in a a level 2 or lavel 1 table. See more comments on this below (I reviewed this patch from the bottom up).

I have a feeling this wasn't tested with 2 level device tables. Could that be true?

Quoted 96 lines
virt/kvm/arm/vgic/vgic-its.c +90 -1
+	       (itt_addr_field << KVM_ITS_DTE_ITTADDR_SHIFT) |
+		(dev->nb_eventid_bits - 1));
+	val = cpu_to_le64(val);
+	ret = kvm_write_guest(kvm, ptr, &val, dte_esz);
+	return ret;
+}
+
+/**
+ * vgic_its_restore_dte - restore a device table entry
+ *
+ * @its: its handle
+ * @id: device id the DTE corresponds to
+ * @ptr: kernel VA where the 8 byte DTE is located
+ * @opaque: unused
+ * @next: offset to the next valid device id
+ *
+ * Return: < 0 on error, 0 otherwise
+ */
+static int vgic_its_restore_dte(struct vgic_its *its, u32 id,
+				void *ptr, void *opaque, u32 *next)
+{
+	struct its_device *dev;
+	gpa_t itt_addr;
+	u8 nb_eventid_bits;
+	u64 entry = *(u64 *)ptr;
+	bool valid;
+	int ret;
+
+	entry = le64_to_cpu(entry);
+
+	valid = entry >> KVM_ITS_DTE_VALID_SHIFT;
+	nb_eventid_bits = (entry & KVM_ITS_DTE_SIZE_MASK) + 1;
+	itt_addr = ((entry & KVM_ITS_DTE_ITTADDR_MASK)
+			>> KVM_ITS_DTE_ITTADDR_SHIFT) << 8;
+	*next = 1;
+
+	if (!valid)
+		return 0;
+
+	/* dte entry is valid */
+	*next = (entry & KVM_ITS_DTE_NEXT_MASK) >> KVM_ITS_DTE_NEXT_SHIFT;
+
+	ret = vgic_its_alloc_device(its, &dev, id,
+				    itt_addr, nb_eventid_bits);
+	if (ret)
+		return ret;
+	ret = vgic_its_restore_itt(its, dev);
+
+	return ret;
+}
+
+static int vgic_its_device_cmp(void *priv, struct list_head *a,
+			       struct list_head *b)
+{
+	struct its_device *deva = container_of(a, struct its_device, dev_list);
+	struct its_device *devb = container_of(b, struct its_device, dev_list);
+
+	if (deva->device_id < devb->device_id)
+		return -1;
+	else
+		return 1;
+}
+
+/**
  * vgic_its_save_device_tables - Save the device table and all ITT
  * into guest RAM
+ *
+ * L1/L2 handling is hidden by vgic_its_check_id() helper which directly
+ * returns the GPA of the device entry
  */
 static int vgic_its_save_device_tables(struct vgic_its *its)
 {
-	return -ENXIO;
+	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
+	struct its_device *dev;
+	int dte_esz = abi->dte_esz;
+	u64 baser;
+
+	baser = its->baser_device_table;
+
+	list_sort(NULL, &its->device_list, vgic_its_device_cmp);
+
+	list_for_each_entry(dev, &its->device_list, dev_list) {
+		int ret;
+		gpa_t eaddr;
+
+		if (!vgic_its_check_id(its, baser,
+				       dev->device_id, &eaddr))
+			return -EINVAL;
+
+		ret = vgic_its_save_itt(its, dev);
+		if (ret)
+			return ret;
+
+		ret = vgic_its_save_dte(its, dev, eaddr, dte_esz);
+		if (ret)

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2021

Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10 -0
+			return ret;
+	}
+	return 0;
+}
+
+/**
+ * handle_l1_entry - callback used for L1 entries (2 stage case)
+ *
+ * @its: its handle
+ * @id: id

IIUC, this is actually the index of the entry in the L1 table. I think this should be clarified.

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2028

Quoted 7 lines
virt/kvm/arm/vgic/vgic-its.c +7 -0
+ * @addr: kernel VA
+ * @opaque: unused
+ * @next_offset: offset to the next L1 entry: 0 if the last element
+ * was found, 1 otherwise
+ */
+static int handle_l1_entry(struct vgic_its *its, u32 id, void *addr,
+			   void *opaque, u32 *next_offset)

nit: shouldn't this be called handle_l1_device_table_entry ?

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2031

Quoted 3 lines
virt/kvm/arm/vgic/vgic-its.c +3 -0
+{
+	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
+	int l2_start_id = id * (SZ_64K / GITS_LVL1_ENTRY_SIZE);

Hmmm, is this not actually supposed to be (SZ_64K / abi->dte_esz) ?

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2033

Quoted 2 lines
virt/kvm/arm/vgic/vgic-its.c +2 -0
+	u64 entry = *(u64 *)addr;
+	int ret, ite_esz = abi->ite_esz;

Should this be ite_esz or dte_esz?

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2034

Quoted 1 line
virt/kvm/arm/vgic/vgic-its.c +1 -0
+	gpa_t gpa;

nit: put declarations with initialization on separate lines.

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2037

Quoted 3 lines
virt/kvm/arm/vgic/vgic-its.c +3 -0
+
+	entry = le64_to_cpu(entry);
+	*next_offset = 1;

I think you could attach a comment here saying that level 1 tables have to be scanned entirely.

But this also reminds me. Does that mean that the next field in the DTE in your documented ABI format points to the next DTE within that level-2 table, or does it point across to different level-2 tables? I think this needs to be clarified in the ABI unless I'm missing something.

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2042

Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +5 -0
+
+	if (!(entry & BIT_ULL(63)))
+		return 0;
+
+	gpa = entry & GENMASK_ULL(51, 16);

Can you define the bit fields for the level-1 entries as well please?

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2048

Quoted 6 lines
virt/kvm/arm/vgic/vgic-its.c +6 -0
+
+	ret = lookup_table(its, gpa, SZ_64K, ite_esz,
+			   l2_start_id, vgic_its_restore_dte, NULL);
+
+	if (ret == 1) {
+		/* last entry was found in this L2 table */

maybe you should define these return codes for you table scan function, and you wouldn't have to have a separate comment and it would be generally easier to follow the code.

Quoted 9 lines
virt/kvm/arm/vgic/vgic-its.c +5 -0
+		*next_offset = 0;
+		ret = 0;
+	}
+
+	return ret;
 }
 
 /**

virt/kvm/arm/vgic/vgic-its.c static int vgic_its_save_device_tables(struct vgic_its *its) · line 2064

Quoted 7 lines
virt/kvm/arm/vgic/vgic-its.c +3 -1
@@ -1909,7 +2059,30 @@ static int vgic_its_save_device_tables(struct vgic_its *its)
  */
 static int vgic_its_restore_device_tables(struct vgic_its *its)
 {
-	return -ENXIO;
+	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
+	u64 baser = its->baser_device_table;
+	int l1_esz, ret, l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;

nit: put this initialization on a separate line.

virt/kvm/arm/vgic/vgic-its.c static int vgic_its_save_device_tables(struct vgic_its *its) · line 2069

Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +5 -0
+	gpa_t l1_gpa;
+
+	l1_gpa = BASER_ADDRESS(baser);
+	if (!l1_gpa)
+		return 0;

I think you meant to check the valid bit here.

Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +5 -0
+
+	if (baser & GITS_BASER_INDIRECT) {
+		l1_esz = 8;
+		ret = lookup_table(its, l1_gpa, l1_tbl_size, l1_esz, 0,
+				   handle_l1_entry, NULL);

virt/kvm/arm/vgic/vgic-its.c static int vgic_its_save_device_tables(struct vgic_its *its) · line 2084

Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10 -0
+	} else {
+		l1_esz = abi->dte_esz;
+		ret = lookup_table(its, l1_gpa, l1_tbl_size, l1_esz, 0,
+				   vgic_its_restore_dte, NULL);
+	}
+
+	if (ret < 0)
+		return ret;
+
+	/* if last element was not found we have an issue here */

same comment as other patch

Quoted 25 lines
virt/kvm/arm/vgic/vgic-its.c +1 -0
+	return ret ? 0 : -EINVAL;
 }
 
 static int vgic_its_save_cte(struct vgic_its *its,
virt/kvm/arm/vgic/vgic.h +7 -0
index ce57fbd..9bc52ef 100644
@@ -85,6 +85,13 @@
 #define KVM_ITS_ITE_PINTID_SHIFT	16
 #define KVM_ITS_ITE_PINTID_MASK		GENMASK_ULL(47, 16)
 #define KVM_ITS_ITE_ICID_MASK		GENMASK_ULL(15, 0)
+#define KVM_ITS_DTE_VALID_SHIFT		63
+#define KVM_ITS_DTE_VALID_MASK		BIT_ULL(63)
+#define KVM_ITS_DTE_NEXT_SHIFT		49
+#define KVM_ITS_DTE_NEXT_MASK		GENMASK_ULL(62, 49)
+#define KVM_ITS_DTE_ITTADDR_SHIFT	5
+#define KVM_ITS_DTE_ITTADDR_MASK	GENMASK_ULL(48, 5)
+#define KVM_ITS_DTE_SIZE_MASK		GENMASK_ULL(4, 0)
 
 static inline bool irq_is_pending(struct vgic_irq *irq)
 {
Signature · 1 line
2.5.5

Thanks,
-Christoffer

author
#117 lore raw

Re: [PATCH v5 20/22] KVM: arm64: vgic-its: Device table save/restore

Hi Christoffer,

Quoted 98 lines · Christoffer Dall
Quoted 97 lines · Eric Auger

This patch saves the device table entries into guest RAM. Both flat table and 2 stage tables are supported. DeviceId indexing is used.

For each device listed in the device table, we also save the translation table using the vgic_its_save/restore_itt routines.

On restore, devices are re-allocated and their ite are re-built.

---
v4 -> v5:
- sort the device list by deviceid on device table save
- use defines for shifts and masks
- use abi->dte_esz
- clatify entry sizes for L1 and L2 tables

v3 -> v4:
- use the new proto for its_alloc_device
- compute_next_devid_offset, vgic_its_flush/restore_itt become static in this patch
- change in the DTE entry format with the introduction of the valid bit and next field width decrease; ittaddr encoded on its full range
- fix handle_l1_entry entry handling
- correct vgic_its_table_restore error handling

v2 -> v3:
- fix itt_addr bitmask in vgic_its_restore_dte
- addition of return 0 in vgic_its_restore_ite moved to the ITE related patch

v1 -> v2:
- use 8 byte format for DTE and ITE
- support 2 stage format
- remove kvm parameter
- ITT flush/restore moved in a separate patch
- use deviceid indexing

---
 virt/kvm/arm/vgic/vgic-its.c | 183 +++++++++++++++++++++++++++++++++++++++++--
 virt/kvm/arm/vgic/vgic.h     |   7 ++
 2 files changed, 185 insertions(+), 5 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +19 -3
index b02fc3f..86dfc6c 100644
@@ -1682,7 +1682,8 @@ int vgic_its_attr_regs_access(struct kvm_device *dev,
 	return ret;
 }
 
-u32 compute_next_devid_offset(struct list_head *h, struct its_device *dev)
+static u32 compute_next_devid_offset(struct list_head *h,
+				     struct its_device *dev)
 {
 	struct list_head *e = &dev->dev_list;
 	struct its_device *next;
@@ -1858,7 +1859,7 @@ static int vgic_its_ite_cmp(void *priv, struct list_head *a,
 		return 1;
 }
 
-int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
+static int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
 {
 	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
 	gpa_t base = device->itt_addr;
@@ -1877,7 +1878,7 @@ int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
 	return 0;
 }
 
-int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
+static int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
 {
 	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
 	gpa_t base = dev->itt_addr;
@@ -1895,12 +1896,161 @@ int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
 }
 
 /**
+ * vgic_its_save_dte - Save a device table entry at a given GPA
+ *
+ * @its: ITS handle
+ * @dev: ITS device
+ * @ptr: GPA
+ */
+static int vgic_its_save_dte(struct vgic_its *its, struct its_device *dev,
+			     gpa_t ptr, int dte_esz)
+{
+	struct kvm *kvm = its->dev->kvm;
+	u64 val, itt_addr_field;
+	int ret;
+	u32 next_offset;
+
+	itt_addr_field = dev->itt_addr >> 8;
Quoted 8 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 1916

Quoted 3 lines
virt/kvm/arm/vgic/vgic-its.c +3 -0
+	next_offset = compute_next_devid_offset(&its->device_list, dev);
+	val = (1ULL << KVM_ITS_DTE_VALID_SHIFT |
+	       ((u64)next_offset << KVM_ITS_DTE_NEXT_SHIFT) |

I think this implies that the next field in your ABI points to the next offset, regardless of whether or not this is in a a level 2 or lavel 1 table. See more comments on this below (I reviewed this patch from the bottom up).

Not sure I understand your comment.

Doc says:
- next: equals to 0 if this entry is the last one; otherwise it corresponds to the deviceid offset to the next DTE, capped by 2^14 -1.

This is independent on 1 or 2 levels as we sort the devices by deviceid's and compute the delta between those id.

Quoted 3 lines

I have a feeling this wasn't tested with 2 level device tables. Could that be true?

No this was tested with 1 & and 2 levels (I hacked the guest to force 2 levels). 1 test hole I have though is all my dte's currently belong to the same 2d level page, ie. my deviceid are not scattered enough.

Quoted 102 lines
Quoted 102 lines
virt/kvm/arm/vgic/vgic-its.c +95 -1
+	       (itt_addr_field << KVM_ITS_DTE_ITTADDR_SHIFT) |
+		(dev->nb_eventid_bits - 1));
+	val = cpu_to_le64(val);
+	ret = kvm_write_guest(kvm, ptr, &val, dte_esz);
+	return ret;
+}
+
+/**
+ * vgic_its_restore_dte - restore a device table entry
+ *
+ * @its: its handle
+ * @id: device id the DTE corresponds to
+ * @ptr: kernel VA where the 8 byte DTE is located
+ * @opaque: unused
+ * @next: offset to the next valid device id
+ *
+ * Return: < 0 on error, 0 otherwise
+ */
+static int vgic_its_restore_dte(struct vgic_its *its, u32 id,
+				void *ptr, void *opaque, u32 *next)
+{
+	struct its_device *dev;
+	gpa_t itt_addr;
+	u8 nb_eventid_bits;
+	u64 entry = *(u64 *)ptr;
+	bool valid;
+	int ret;
+
+	entry = le64_to_cpu(entry);
+
+	valid = entry >> KVM_ITS_DTE_VALID_SHIFT;
+	nb_eventid_bits = (entry & KVM_ITS_DTE_SIZE_MASK) + 1;
+	itt_addr = ((entry & KVM_ITS_DTE_ITTADDR_MASK)
+			>> KVM_ITS_DTE_ITTADDR_SHIFT) << 8;
+	*next = 1;
+
+	if (!valid)
+		return 0;
+
+	/* dte entry is valid */
+	*next = (entry & KVM_ITS_DTE_NEXT_MASK) >> KVM_ITS_DTE_NEXT_SHIFT;
+
+	ret = vgic_its_alloc_device(its, &dev, id,
+				    itt_addr, nb_eventid_bits);
+	if (ret)
+		return ret;
+	ret = vgic_its_restore_itt(its, dev);
+
+	return ret;
+}
+
+static int vgic_its_device_cmp(void *priv, struct list_head *a,
+			       struct list_head *b)
+{
+	struct its_device *deva = container_of(a, struct its_device, dev_list);
+	struct its_device *devb = container_of(b, struct its_device, dev_list);
+
+	if (deva->device_id < devb->device_id)
+		return -1;
+	else
+		return 1;
+}
+
+/**
  * vgic_its_save_device_tables - Save the device table and all ITT
  * into guest RAM
+ *
+ * L1/L2 handling is hidden by vgic_its_check_id() helper which directly
+ * returns the GPA of the device entry
  */
 static int vgic_its_save_device_tables(struct vgic_its *its)
 {
-	return -ENXIO;
+	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
+	struct its_device *dev;
+	int dte_esz = abi->dte_esz;
+	u64 baser;
+
+	baser = its->baser_device_table;
+
+	list_sort(NULL, &its->device_list, vgic_its_device_cmp);
+
+	list_for_each_entry(dev, &its->device_list, dev_list) {
+		int ret;
+		gpa_t eaddr;
+
+		if (!vgic_its_check_id(its, baser,
+				       dev->device_id, &eaddr))
+			return -EINVAL;
+
+		ret = vgic_its_save_itt(its, dev);
+		if (ret)
+			return ret;
+
+		ret = vgic_its_save_dte(its, dev, eaddr, dte_esz);
+		if (ret)
+			return ret;
+	}
+	return 0;
+}
+
Quoted 8 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2021

Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +5 -0
+/**
+ * handle_l1_entry - callback used for L1 entries (2 stage case)
+ *
+ * @its: its handle
+ * @id: id

IIUC, this is actually the index of the entry in the L1 table. I think this should be clarified.

yep

Quoted 10 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2028

Quoted 8 lines
virt/kvm/arm/vgic/vgic-its.c +7 -0
+ * @addr: kernel VA
+ * @opaque: unused
+ * @next_offset: offset to the next L1 entry: 0 if the last element
+ * was found, 1 otherwise
+ */
+static int handle_l1_entry(struct vgic_its *its, u32 id, void *addr,
+			   void *opaque, u32 *next_offset)

nit: shouldn't this be called handle_l1_device_table_entry ?

renamed into handle_l1_dte

Quoted 6 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2031

Quoted 4 lines
virt/kvm/arm/vgic/vgic-its.c +3 -0
+{
+	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
+	int l2_start_id = id * (SZ_64K / GITS_LVL1_ENTRY_SIZE);

Hmmm, is this not actually supposed to be (SZ_64K / abi->dte_esz) ?

no because 1st level entries have a fixed size of GITS_LVL1_ENTRY_SIZE bytes

Quoted 5 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2033

Quoted 3 lines
virt/kvm/arm/vgic/vgic-its.c +2 -0
+	u64 entry = *(u64 *)addr;
+	int ret, ite_esz = abi->ite_esz;

Should this be ite_esz or dte_esz?

you're correct. dte_esz should be used.

Quoted 4 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2034

Quoted 2 lines
virt/kvm/arm/vgic/vgic-its.c +1 -0
+	gpa_t gpa;

nit: put declarations with initialization on separate lines.

OK

Quoted 7 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2037

Quoted 4 lines
virt/kvm/arm/vgic/vgic-its.c +3 -0
+
+	entry = le64_to_cpu(entry);
+	*next_offset = 1;

I think you could attach a comment here saying that level 1 tables have to be scanned entirely.

added. note we exit as soon as the last element is found when scanning l2 tables.

Quoted 5 lines

But this also reminds me. Does that mean that the next field in the DTE in your documented ABI format points to the next DTE within that level-2 table, or does it point across to different level-2 tables? I think this needs to be clarified in the ABI unless I'm missing something.

see above comment on next_index semantic. In the doc I talk about deviceid offset and not of table index.

Quoted 8 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2042

Quoted 6 lines
virt/kvm/arm/vgic/vgic-its.c +5 -0
+
+	if (!(entry & BIT_ULL(63)))
+		return 0;
+
+	gpa = entry & GENMASK_ULL(51, 16);

Can you define the bit fields for the level-1 entries as well please?

yep

Quoted 11 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2048

Quoted 7 lines
virt/kvm/arm/vgic/vgic-its.c +6 -0
+
+	ret = lookup_table(its, gpa, SZ_64K, ite_esz,
+			   l2_start_id, vgic_its_restore_dte, NULL);
+
+	if (ret == 1) {
+		/* last entry was found in this L2 table */

maybe you should define these return codes for you table scan function, and you wouldn't have to have a separate comment and it would be generally easier to follow the code.

I revisited the error values according to your suggestion and this looks simpler to me now.

Quoted 8 lines
Quoted 8 lines
virt/kvm/arm/vgic/vgic-its.c +5 -0
+		*next_offset = 0;
+		ret = 0;
+	}
+
+	return ret;
 }
 
Quoted 11 lines

virt/kvm/arm/vgic/vgic-its.c static int vgic_its_save_device_tables(struct vgic_its *its) · line 2064

Quoted 9 lines
virt/kvm/arm/vgic/vgic-its.c +3 -1
 /**
@@ -1909,7 +2059,30 @@ static int vgic_its_save_device_tables(struct vgic_its *its)
  */
 static int vgic_its_restore_device_tables(struct vgic_its *its)
 {
-	return -ENXIO;
+	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
+	u64 baser = its->baser_device_table;
+	int l1_esz, ret, l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;

nit: put this initialization on a separate line.

done

Quoted 8 lines

virt/kvm/arm/vgic/vgic-its.c static int vgic_its_save_device_tables(struct vgic_its *its) · line 2069

Quoted 6 lines
virt/kvm/arm/vgic/vgic-its.c +5 -0
+	gpa_t l1_gpa;
+
+	l1_gpa = BASER_ADDRESS(baser);
+	if (!l1_gpa)
+		return 0;

I think you meant to check the valid bit here.

yes

Quoted 19 lines
Quoted 6 lines
virt/kvm/arm/vgic/vgic-its.c +5 -0
+
+	if (baser & GITS_BASER_INDIRECT) {
+		l1_esz = 8;
+		ret = lookup_table(its, l1_gpa, l1_tbl_size, l1_esz, 0,
+				   handle_l1_entry, NULL);

virt/kvm/arm/vgic/vgic-its.c static int vgic_its_save_device_tables(struct vgic_its *its) · line 2084

Quoted 10 lines
virt/kvm/arm/vgic/vgic-its.c +10 -0
+	} else {
+		l1_esz = abi->dte_esz;
+		ret = lookup_table(its, l1_gpa, l1_tbl_size, l1_esz, 0,
+				   vgic_its_restore_dte, NULL);
+	}
+
+	if (ret < 0)
+		return ret;
+
+	/* if last element was not found we have an issue here */

same comment as other patch

Quoted 1 line

virt/kvm/arm/vgic/vgic-its.c static int vgic_its_save_device_tables(struct vgic_its *its) · line 2085

Quoted 1 line
virt/kvm/arm/vgic/vgic-its.c +1 -0
+	return ret ? 0 : -EINVAL;

revisited

Thanks

Eric

Quoted 32 lines
Quoted 24 lines
virt/kvm/arm/vgic/vgic-its.c +0 -0
 }
 
 static int vgic_its_save_cte(struct vgic_its *its,
virt/kvm/arm/vgic/vgic.h +7 -0
index ce57fbd..9bc52ef 100644
@@ -85,6 +85,13 @@
 #define KVM_ITS_ITE_PINTID_SHIFT	16
 #define KVM_ITS_ITE_PINTID_MASK		GENMASK_ULL(47, 16)
 #define KVM_ITS_ITE_ICID_MASK		GENMASK_ULL(15, 0)
+#define KVM_ITS_DTE_VALID_SHIFT		63
+#define KVM_ITS_DTE_VALID_MASK		BIT_ULL(63)
+#define KVM_ITS_DTE_NEXT_SHIFT		49
+#define KVM_ITS_DTE_NEXT_MASK		GENMASK_ULL(62, 49)
+#define KVM_ITS_DTE_ITTADDR_SHIFT	5
+#define KVM_ITS_DTE_ITTADDR_MASK	GENMASK_ULL(48, 5)
+#define KVM_ITS_DTE_SIZE_MASK		GENMASK_ULL(4, 0)
 
 static inline bool irq_is_pending(struct vgic_irq *irq)
 {
Signature · 1 line
2.5.5

Thanks,
-Christoffer

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#118 lore raw

Re: [PATCH v5 20/22] KVM: arm64: vgic-its: Device table save/restore

Quoted 110 lines · Auger Eric

Hi Christoffer,

Quoted 98 lines · Christoffer Dall
Quoted 97 lines · Eric Auger

This patch saves the device table entries into guest RAM. Both flat table and 2 stage tables are supported. DeviceId indexing is used.

For each device listed in the device table, we also save the translation table using the vgic_its_save/restore_itt routines.

On restore, devices are re-allocated and their ite are re-built.

---
v4 -> v5:
- sort the device list by deviceid on device table save
- use defines for shifts and masks
- use abi->dte_esz
- clatify entry sizes for L1 and L2 tables

v3 -> v4:
- use the new proto for its_alloc_device
- compute_next_devid_offset, vgic_its_flush/restore_itt become static in this patch
- change in the DTE entry format with the introduction of the valid bit and next field width decrease; ittaddr encoded on its full range
- fix handle_l1_entry entry handling
- correct vgic_its_table_restore error handling

v2 -> v3:
- fix itt_addr bitmask in vgic_its_restore_dte
- addition of return 0 in vgic_its_restore_ite moved to the ITE related patch

v1 -> v2:
- use 8 byte format for DTE and ITE
- support 2 stage format
- remove kvm parameter
- ITT flush/restore moved in a separate patch
- use deviceid indexing

---
 virt/kvm/arm/vgic/vgic-its.c | 183 +++++++++++++++++++++++++++++++++++++++++--
 virt/kvm/arm/vgic/vgic.h     |   7 ++
 2 files changed, 185 insertions(+), 5 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +19 -3
index b02fc3f..86dfc6c 100644
@@ -1682,7 +1682,8 @@ int vgic_its_attr_regs_access(struct kvm_device *dev,
 	return ret;
 }
 
-u32 compute_next_devid_offset(struct list_head *h, struct its_device *dev)
+static u32 compute_next_devid_offset(struct list_head *h,
+				     struct its_device *dev)
 {
 	struct list_head *e = &dev->dev_list;
 	struct its_device *next;
@@ -1858,7 +1859,7 @@ static int vgic_its_ite_cmp(void *priv, struct list_head *a,
 		return 1;
 }
 
-int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
+static int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
 {
 	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
 	gpa_t base = device->itt_addr;
@@ -1877,7 +1878,7 @@ int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
 	return 0;
 }
 
-int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
+static int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
 {
 	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
 	gpa_t base = dev->itt_addr;
@@ -1895,12 +1896,161 @@ int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
 }
 
 /**
+ * vgic_its_save_dte - Save a device table entry at a given GPA
+ *
+ * @its: ITS handle
+ * @dev: ITS device
+ * @ptr: GPA
+ */
+static int vgic_its_save_dte(struct vgic_its *its, struct its_device *dev,
+			     gpa_t ptr, int dte_esz)
+{
+	struct kvm *kvm = its->dev->kvm;
+	u64 val, itt_addr_field;
+	int ret;
+	u32 next_offset;
+
+	itt_addr_field = dev->itt_addr >> 8;
Quoted 8 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 1916

Quoted 3 lines
virt/kvm/arm/vgic/vgic-its.c +3 -0
+	next_offset = compute_next_devid_offset(&its->device_list, dev);
+	val = (1ULL << KVM_ITS_DTE_VALID_SHIFT |
+	       ((u64)next_offset << KVM_ITS_DTE_NEXT_SHIFT) |

I think this implies that the next field in your ABI points to the next offset, regardless of whether or not this is in a a level 2 or lavel 1 table. See more comments on this below (I reviewed this patch from the bottom up).

Not sure I understand your comment.

Quoted 7 lines

Doc says:
- next: equals to 0 if this entry is the last one; otherwise it corresponds to the deviceid offset to the next DTE, capped by 2^14 -1.

This is independent on 1 or 2 levels as we sort the devices by deviceid's and compute the delta between those id.

see below.

Quoted 127 lines
Quoted 3 lines

I have a feeling this wasn't tested with 2 level device tables. Could that be true?

No this was tested with 1 & and 2 levels (I hacked the guest to force 2 levels). 1 test hole I have though is all my dte's currently belong to the same 2d level page, ie. my deviceid are not scattered enough.

Quoted 102 lines
Quoted 102 lines
virt/kvm/arm/vgic/vgic-its.c +95 -1
+	       (itt_addr_field << KVM_ITS_DTE_ITTADDR_SHIFT) |
+		(dev->nb_eventid_bits - 1));
+	val = cpu_to_le64(val);
+	ret = kvm_write_guest(kvm, ptr, &val, dte_esz);
+	return ret;
+}
+
+/**
+ * vgic_its_restore_dte - restore a device table entry
+ *
+ * @its: its handle
+ * @id: device id the DTE corresponds to
+ * @ptr: kernel VA where the 8 byte DTE is located
+ * @opaque: unused
+ * @next: offset to the next valid device id
+ *
+ * Return: < 0 on error, 0 otherwise
+ */
+static int vgic_its_restore_dte(struct vgic_its *its, u32 id,
+				void *ptr, void *opaque, u32 *next)
+{
+	struct its_device *dev;
+	gpa_t itt_addr;
+	u8 nb_eventid_bits;
+	u64 entry = *(u64 *)ptr;
+	bool valid;
+	int ret;
+
+	entry = le64_to_cpu(entry);
+
+	valid = entry >> KVM_ITS_DTE_VALID_SHIFT;
+	nb_eventid_bits = (entry & KVM_ITS_DTE_SIZE_MASK) + 1;
+	itt_addr = ((entry & KVM_ITS_DTE_ITTADDR_MASK)
+			>> KVM_ITS_DTE_ITTADDR_SHIFT) << 8;
+	*next = 1;
+
+	if (!valid)
+		return 0;
+
+	/* dte entry is valid */
+	*next = (entry & KVM_ITS_DTE_NEXT_MASK) >> KVM_ITS_DTE_NEXT_SHIFT;
+
+	ret = vgic_its_alloc_device(its, &dev, id,
+				    itt_addr, nb_eventid_bits);
+	if (ret)
+		return ret;
+	ret = vgic_its_restore_itt(its, dev);
+
+	return ret;
+}
+
+static int vgic_its_device_cmp(void *priv, struct list_head *a,
+			       struct list_head *b)
+{
+	struct its_device *deva = container_of(a, struct its_device, dev_list);
+	struct its_device *devb = container_of(b, struct its_device, dev_list);
+
+	if (deva->device_id < devb->device_id)
+		return -1;
+	else
+		return 1;
+}
+
+/**
  * vgic_its_save_device_tables - Save the device table and all ITT
  * into guest RAM
+ *
+ * L1/L2 handling is hidden by vgic_its_check_id() helper which directly
+ * returns the GPA of the device entry
  */
 static int vgic_its_save_device_tables(struct vgic_its *its)
 {
-	return -ENXIO;
+	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
+	struct its_device *dev;
+	int dte_esz = abi->dte_esz;
+	u64 baser;
+
+	baser = its->baser_device_table;
+
+	list_sort(NULL, &its->device_list, vgic_its_device_cmp);
+
+	list_for_each_entry(dev, &its->device_list, dev_list) {
+		int ret;
+		gpa_t eaddr;
+
+		if (!vgic_its_check_id(its, baser,
+				       dev->device_id, &eaddr))
+			return -EINVAL;
+
+		ret = vgic_its_save_itt(its, dev);
+		if (ret)
+			return ret;
+
+		ret = vgic_its_save_dte(its, dev, eaddr, dte_esz);
+		if (ret)
+			return ret;
+	}
+	return 0;
+}
+
Quoted 8 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2021

Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +5 -0
+/**
+ * handle_l1_entry - callback used for L1 entries (2 stage case)
+ *
+ * @its: its handle
+ * @id: id

IIUC, this is actually the index of the entry in the L1 table. I think this should be clarified.

yep

Quoted 10 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2028

Quoted 8 lines
virt/kvm/arm/vgic/vgic-its.c +7 -0
+ * @addr: kernel VA
+ * @opaque: unused
+ * @next_offset: offset to the next L1 entry: 0 if the last element
+ * was found, 1 otherwise
+ */
+static int handle_l1_entry(struct vgic_its *its, u32 id, void *addr,
+			   void *opaque, u32 *next_offset)

nit: shouldn't this be called handle_l1_device_table_entry ?

Quoted 8 lines

renamed into handle_l1_dte

Quoted 6 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2031

Quoted 4 lines
virt/kvm/arm/vgic/vgic-its.c +3 -0
+{
+	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
+	int l2_start_id = id * (SZ_64K / GITS_LVL1_ENTRY_SIZE);

Hmmm, is this not actually supposed to be (SZ_64K / abi->dte_esz) ?

no because 1st level entries have a fixed size of GITS_LVL1_ENTRY_SIZE bytes

yes, but the ID you calculate is a result of how many IDs each 64K 2nd level table can hold, which depends on the size of each entry in the 2nd level table, right? Or am I misunderstanding how this works completely.

Quoted 22 lines
Quoted 5 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2033

Quoted 3 lines
virt/kvm/arm/vgic/vgic-its.c +2 -0
+	u64 entry = *(u64 *)addr;
+	int ret, ite_esz = abi->ite_esz;

Should this be ite_esz or dte_esz?

you're correct. dte_esz should be used.

Quoted 4 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2034

Quoted 2 lines
virt/kvm/arm/vgic/vgic-its.c +1 -0
+	gpa_t gpa;

nit: put declarations with initialization on separate lines.

OK

Quoted 7 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2037

Quoted 4 lines
virt/kvm/arm/vgic/vgic-its.c +3 -0
+
+	entry = le64_to_cpu(entry);
+	*next_offset = 1;

I think you could attach a comment here saying that level 1 tables have to be scanned entirely.

added. note we exit as soon as the last element is found when scanning l2 tables.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

But this also reminds me. Does that mean that the next field in the DTE in your documented ABI format points to the next DTE within that level-2 table, or does it point across to different level-2 tables? I think this needs to be clarified in the ABI unless I'm missing something.

see above comment on next_index semantic. In the doc I talk about deviceid offset and not of table index.

ok, I see, I was misled by the definition of lookup_table saying that it returns 1 if the last element is identified, which is only true when you actually find an element that is valid and where the next field is zero. I understood it to mean if it found the last item in the table it was scanning. So it is implied that lookup table can be called in two levels and the return value indicates if the element was the last from the point of view of the highest level, not in the context the last instance was called.

Note that it's further confusing that the handler function has the return value the other way around, where 0 means it's the last element. Perhaps you could make this much more readable by introducing a define for the return values.

Thanks,
-Christoffer

author
#119 lore raw

Re: [PATCH v5 20/22] KVM: arm64: vgic-its: Device table save/restore

Hi Christoffer,

Quoted 255 lines · Christoffer Dall
Quoted 110 lines · Auger Eric

Hi Christoffer,

Quoted 98 lines · Christoffer Dall
Quoted 97 lines · Eric Auger

This patch saves the device table entries into guest RAM. Both flat table and 2 stage tables are supported. DeviceId indexing is used.

For each device listed in the device table, we also save the translation table using the vgic_its_save/restore_itt routines.

On restore, devices are re-allocated and their ite are re-built.

---
v4 -> v5:
- sort the device list by deviceid on device table save
- use defines for shifts and masks
- use abi->dte_esz
- clatify entry sizes for L1 and L2 tables

v3 -> v4:
- use the new proto for its_alloc_device
- compute_next_devid_offset, vgic_its_flush/restore_itt become static in this patch
- change in the DTE entry format with the introduction of the valid bit and next field width decrease; ittaddr encoded on its full range
- fix handle_l1_entry entry handling
- correct vgic_its_table_restore error handling

v2 -> v3:
- fix itt_addr bitmask in vgic_its_restore_dte
- addition of return 0 in vgic_its_restore_ite moved to the ITE related patch

v1 -> v2:
- use 8 byte format for DTE and ITE
- support 2 stage format
- remove kvm parameter
- ITT flush/restore moved in a separate patch
- use deviceid indexing

---
 virt/kvm/arm/vgic/vgic-its.c | 183 +++++++++++++++++++++++++++++++++++++++++--
 virt/kvm/arm/vgic/vgic.h     |   7 ++
 2 files changed, 185 insertions(+), 5 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +19 -3
index b02fc3f..86dfc6c 100644
@@ -1682,7 +1682,8 @@ int vgic_its_attr_regs_access(struct kvm_device *dev,
 	return ret;
 }
 
-u32 compute_next_devid_offset(struct list_head *h, struct its_device *dev)
+static u32 compute_next_devid_offset(struct list_head *h,
+				     struct its_device *dev)
 {
 	struct list_head *e = &dev->dev_list;
 	struct its_device *next;
@@ -1858,7 +1859,7 @@ static int vgic_its_ite_cmp(void *priv, struct list_head *a,
 		return 1;
 }
 
-int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
+static int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
 {
 	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
 	gpa_t base = device->itt_addr;
@@ -1877,7 +1878,7 @@ int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
 	return 0;
 }
 
-int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
+static int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
 {
 	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
 	gpa_t base = dev->itt_addr;
@@ -1895,12 +1896,161 @@ int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
 }
 
 /**
+ * vgic_its_save_dte - Save a device table entry at a given GPA
+ *
+ * @its: ITS handle
+ * @dev: ITS device
+ * @ptr: GPA
+ */
+static int vgic_its_save_dte(struct vgic_its *its, struct its_device *dev,
+			     gpa_t ptr, int dte_esz)
+{
+	struct kvm *kvm = its->dev->kvm;
+	u64 val, itt_addr_field;
+	int ret;
+	u32 next_offset;
+
+	itt_addr_field = dev->itt_addr >> 8;
Quoted 8 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 1916

Quoted 3 lines
virt/kvm/arm/vgic/vgic-its.c +3 -0
+	next_offset = compute_next_devid_offset(&its->device_list, dev);
+	val = (1ULL << KVM_ITS_DTE_VALID_SHIFT |
+	       ((u64)next_offset << KVM_ITS_DTE_NEXT_SHIFT) |

I think this implies that the next field in your ABI points to the next offset, regardless of whether or not this is in a a level 2 or lavel 1 table. See more comments on this below (I reviewed this patch from the bottom up).

Not sure I understand your comment.

Quoted 7 lines

Doc says:
- next: equals to 0 if this entry is the last one; otherwise it corresponds to the deviceid offset to the next DTE, capped by 2^14 -1.

This is independent on 1 or 2 levels as we sort the devices by deviceid's and compute the delta between those id.

see below.

Quoted 134 lines
Quoted 3 lines

I have a feeling this wasn't tested with 2 level device tables. Could that be true?

No this was tested with 1 & and 2 levels (I hacked the guest to force 2 levels). 1 test hole I have though is all my dte's currently belong to the same 2d level page, ie. my deviceid are not scattered enough.

Quoted 102 lines
Quoted 102 lines
virt/kvm/arm/vgic/vgic-its.c +95 -1
+	       (itt_addr_field << KVM_ITS_DTE_ITTADDR_SHIFT) |
+		(dev->nb_eventid_bits - 1));
+	val = cpu_to_le64(val);
+	ret = kvm_write_guest(kvm, ptr, &val, dte_esz);
+	return ret;
+}
+
+/**
+ * vgic_its_restore_dte - restore a device table entry
+ *
+ * @its: its handle
+ * @id: device id the DTE corresponds to
+ * @ptr: kernel VA where the 8 byte DTE is located
+ * @opaque: unused
+ * @next: offset to the next valid device id
+ *
+ * Return: < 0 on error, 0 otherwise
+ */
+static int vgic_its_restore_dte(struct vgic_its *its, u32 id,
+				void *ptr, void *opaque, u32 *next)
+{
+	struct its_device *dev;
+	gpa_t itt_addr;
+	u8 nb_eventid_bits;
+	u64 entry = *(u64 *)ptr;
+	bool valid;
+	int ret;
+
+	entry = le64_to_cpu(entry);
+
+	valid = entry >> KVM_ITS_DTE_VALID_SHIFT;
+	nb_eventid_bits = (entry & KVM_ITS_DTE_SIZE_MASK) + 1;
+	itt_addr = ((entry & KVM_ITS_DTE_ITTADDR_MASK)
+			>> KVM_ITS_DTE_ITTADDR_SHIFT) << 8;
+	*next = 1;
+
+	if (!valid)
+		return 0;
+
+	/* dte entry is valid */
+	*next = (entry & KVM_ITS_DTE_NEXT_MASK) >> KVM_ITS_DTE_NEXT_SHIFT;
+
+	ret = vgic_its_alloc_device(its, &dev, id,
+				    itt_addr, nb_eventid_bits);
+	if (ret)
+		return ret;
+	ret = vgic_its_restore_itt(its, dev);
+
+	return ret;
+}
+
+static int vgic_its_device_cmp(void *priv, struct list_head *a,
+			       struct list_head *b)
+{
+	struct its_device *deva = container_of(a, struct its_device, dev_list);
+	struct its_device *devb = container_of(b, struct its_device, dev_list);
+
+	if (deva->device_id < devb->device_id)
+		return -1;
+	else
+		return 1;
+}
+
+/**
  * vgic_its_save_device_tables - Save the device table and all ITT
  * into guest RAM
+ *
+ * L1/L2 handling is hidden by vgic_its_check_id() helper which directly
+ * returns the GPA of the device entry
  */
 static int vgic_its_save_device_tables(struct vgic_its *its)
 {
-	return -ENXIO;
+	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
+	struct its_device *dev;
+	int dte_esz = abi->dte_esz;
+	u64 baser;
+
+	baser = its->baser_device_table;
+
+	list_sort(NULL, &its->device_list, vgic_its_device_cmp);
+
+	list_for_each_entry(dev, &its->device_list, dev_list) {
+		int ret;
+		gpa_t eaddr;
+
+		if (!vgic_its_check_id(its, baser,
+				       dev->device_id, &eaddr))
+			return -EINVAL;
+
+		ret = vgic_its_save_itt(its, dev);
+		if (ret)
+			return ret;
+
+		ret = vgic_its_save_dte(its, dev, eaddr, dte_esz);
+		if (ret)
+			return ret;
+	}
+	return 0;
+}
+
Quoted 8 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2021

Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +5 -0
+/**
+ * handle_l1_entry - callback used for L1 entries (2 stage case)
+ *
+ * @its: its handle
+ * @id: id

IIUC, this is actually the index of the entry in the L1 table. I think this should be clarified.

yep

Quoted 10 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2028

Quoted 8 lines
virt/kvm/arm/vgic/vgic-its.c +7 -0
+ * @addr: kernel VA
+ * @opaque: unused
+ * @next_offset: offset to the next L1 entry: 0 if the last element
+ * was found, 1 otherwise
+ */
+static int handle_l1_entry(struct vgic_its *its, u32 id, void *addr,
+			   void *opaque, u32 *next_offset)

nit: shouldn't this be called handle_l1_device_table_entry ?

renamed into handle_l1_dte

Quoted 6 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2031

Quoted 4 lines
virt/kvm/arm/vgic/vgic-its.c +3 -0
+{
+	const struct vgic_its_abi *abi = vgic_its_get_abi(its);
+	int l2_start_id = id * (SZ_64K / GITS_LVL1_ENTRY_SIZE);

Hmmm, is this not actually supposed to be (SZ_64K / abi->dte_esz) ?

Quoted 5 lines
Quoted 1 line

no because 1st level entries have a fixed size of GITS_LVL1_ENTRY_SIZE bytes

yes, but the ID you calculate is a result of how many IDs each 64K 2nd level table can hold, which depends on the size of each entry in the 2nd level table, right? Or am I misunderstanding how this works completely.

Hum damn you're fully right. Thank you for insisting. GITS_LVL1_ENTRY_SIZE must be passed instead as l1_esz in lookup_table()

Eric

Quoted 48 lines
Quoted 23 lines
Quoted 6 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2033

Quoted 4 lines
virt/kvm/arm/vgic/vgic-its.c +2 -0
+	u64 entry = *(u64 *)addr;
+	int ret, ite_esz = abi->ite_esz;

Should this be ite_esz or dte_esz?

you're correct. dte_esz should be used.

Quoted 4 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2034

Quoted 2 lines
virt/kvm/arm/vgic/vgic-its.c +1 -0
+	gpa_t gpa;

nit: put declarations with initialization on separate lines.

OK

Quoted 7 lines

virt/kvm/arm/vgic/vgic-its.c int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) · line 2037

Quoted 4 lines
virt/kvm/arm/vgic/vgic-its.c +3 -0
+
+	entry = le64_to_cpu(entry);
+	*next_offset = 1;

I think you could attach a comment here saying that level 1 tables have to be scanned entirely.

added. note we exit as soon as the last element is found when scanning l2 tables.

Quoted 1 line
Quoted 7 lines
Quoted 4 lines

But this also reminds me. Does that mean that the next field in the DTE in your documented ABI format points to the next DTE within that level-2 table, or does it point across to different level-2 tables? I think this needs to be clarified in the ABI unless I'm missing something.

see above comment on next_index semantic. In the doc I talk about deviceid offset and not of table index.

ok, I see, I was misled by the definition of lookup_table saying that it returns 1 if the last element is identified, which is only true when you actually find an element that is valid and where the next field is zero. I understood it to mean if it found the last item in the table it was scanning. So it is implied that lookup table can be called in two levels and the return value indicates if the element was the last from the point of view of the highest level, not in the context the last instance was called.

Note that it's further confusing that the handler function has the return value the other way around, where 0 means it's the last element. Perhaps you could make this much more readable by introducing a define for the return values.

Thanks,
-Christoffer

author
#120 lore raw

PATCH v5 21/22 KVM: arm64: vgic-its: Fix pending table sync

In reply to Eric Auger

In its_sync_lpi_pending_table() we currently ignore the target_vcpu of the LPIs. We sync the pending bit found in the vcpu pending table even if the LPI is not targeting it.

Also in vgic_its_cmd_handle_invall() we are supposed to read the config table data for the LPIs associated to the collection ID. At the moment we refresh all LPI config information.

This patch passes a vpcu to vgic_copy_lpi_list() so that this latter returns a snapshot of the LPIs targeting this CPU and only those.

---
 virt/kvm/arm/vgic/vgic-its.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +12 -12
index 86dfc6c..be848be 100644
@@ -252,13 +252,13 @@ static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq,
 }
 
 /*
- * Create a snapshot of the current LPI list, so that we can enumerate all
- * LPIs without holding any lock.
- * Returns the array length and puts the kmalloc'ed array into intid_ptr.
+ * Create a snapshot of the current LPIs targeting @vcpu, so that we can
+ * enumerate those LPIs without holding any lock.
+ * Returns their number and puts the kmalloc'ed array into intid_ptr.
  */
-static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr)
+static int vgic_copy_lpi_list(struct kvm_vcpu *vcpu, u32 **intid_ptr)
 {
-	struct vgic_dist *dist = &kvm->arch.vgic;
+	struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
 	struct vgic_irq *irq;
 	u32 *intids;
 	int irq_count = dist->lpi_list_count, i = 0;
@@ -277,14 +277,14 @@ static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr)
 	spin_lock(&dist->lpi_list_lock);
 	list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
 		/* We don't need to "get" the IRQ, as we hold the list lock. */
-		intids[i] = irq->intid;
-		if (++i == irq_count)
-			break;
+		if (irq->target_vcpu != vcpu)
+			continue;
+		intids[i++] = irq->intid;
 	}
 	spin_unlock(&dist->lpi_list_lock);
 
 	*intid_ptr = intids;
-	return irq_count;
+	return i;
 }
 
 /*
@@ -333,7 +333,7 @@ static u32 max_lpis_propbaser(u64 propbaser)
 }
 
 /*
- * Scan the whole LPI pending table and sync the pending bit in there
+ * Sync the pending table pending bit of LPIs targeting @vcpu
  * with our own data structures. This relies on the LPI being
  * mapped before.
  */
@@ -346,7 +346,7 @@ static int its_sync_lpi_pending_table(struct kvm_vcpu *vcpu)
 	u32 *intids;
 	int nr_irqs, i;
 
-	nr_irqs = vgic_copy_lpi_list(vcpu->kvm, &intids);
+	nr_irqs = vgic_copy_lpi_list(vcpu, &intids);
 	if (nr_irqs < 0)
 		return nr_irqs;
 
@@ -1027,7 +1027,7 @@ static int vgic_its_cmd_handle_invall(struct kvm *kvm, struct vgic_its *its,
 
 	vcpu = kvm_get_vcpu(kvm, collection->target_addr);
 
-	irq_count = vgic_copy_lpi_list(kvm, &intids);
+	irq_count = vgic_copy_lpi_list(vcpu, &intids);
 	if (irq_count < 0)
 		return irq_count;
 
Signature · 1 line
2.5.5
Tested-by Prakash, Brahmajyosyula on v5 21/22
#121 lore raw

Re: [PATCH v5 21/22] KVM: arm64: vgic-its: Fix pending table sync

Quoted 9 lines · Eric Auger

In its_sync_lpi_pending_table() we currently ignore the target_vcpu of the LPIs. We sync the pending bit found in the vcpu pending table even if the LPI is not targeting it.

Also in vgic_its_cmd_handle_invall() we are supposed to read the config table data for the LPIs associated to the collection ID. At the moment we refresh all LPI config information.

Quoted 5 lines

This patch passes a vpcu to vgic_copy_lpi_list() so that this latter returns a snapshot of the LPIs targeting this CPU and only those.

#122 lore raw

Re: [PATCH v5 21/22] KVM: arm64: vgic-its: Fix pending table sync

In reply to Eric Auger
Quoted 42 lines · Eric Auger

In its_sync_lpi_pending_table() we currently ignore the target_vcpu of the LPIs. We sync the pending bit found in the vcpu pending table even if the LPI is not targeting it.

Also in vgic_its_cmd_handle_invall() we are supposed to read the config table data for the LPIs associated to the collection ID. At the moment we refresh all LPI config information.

This patch passes a vpcu to vgic_copy_lpi_list() so that this latter returns a snapshot of the LPIs targeting this CPU and only those.

---
 virt/kvm/arm/vgic/vgic-its.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +5 -5
index 86dfc6c..be848be 100644
@@ -252,13 +252,13 @@ static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq,
 }
 
 /*
- * Create a snapshot of the current LPI list, so that we can enumerate all
- * LPIs without holding any lock.
- * Returns the array length and puts the kmalloc'ed array into intid_ptr.
+ * Create a snapshot of the current LPIs targeting @vcpu, so that we can
+ * enumerate those LPIs without holding any lock.
+ * Returns their number and puts the kmalloc'ed array into intid_ptr.
  */
-static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr)
+static int vgic_copy_lpi_list(struct kvm_vcpu *vcpu, u32 **intid_ptr)
 {
-	struct vgic_dist *dist = &kvm->arch.vgic;
+	struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
 	struct vgic_irq *irq;
 	u32 *intids;
 	int irq_count = dist->lpi_list_count, i = 0;

virt/kvm/arm/vgic/vgic-its.c static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr) · line 282

Quoted 9 lines
virt/kvm/arm/vgic/vgic-its.c +3 -3
@@ -277,14 +277,14 @@ static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr)
 	spin_lock(&dist->lpi_list_lock);
 	list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
 		/* We don't need to "get" the IRQ, as we hold the list lock. */
-		intids[i] = irq->intid;
-		if (++i == irq_count)
-			break;
+		if (irq->target_vcpu != vcpu)
+			continue;
+		intids[i++] = irq->intid;

were we checking the ++i == irq_count condition for no good reason before since we can just drop it now?

Quoted 39 lines
virt/kvm/arm/vgic/vgic-its.c +4 -4
 	}
 	spin_unlock(&dist->lpi_list_lock);
 
 	*intid_ptr = intids;
-	return irq_count;
+	return i;
 }
 
 /*
@@ -333,7 +333,7 @@ static u32 max_lpis_propbaser(u64 propbaser)
 }
 
 /*
- * Scan the whole LPI pending table and sync the pending bit in there
+ * Sync the pending table pending bit of LPIs targeting @vcpu
  * with our own data structures. This relies on the LPI being
  * mapped before.
  */
@@ -346,7 +346,7 @@ static int its_sync_lpi_pending_table(struct kvm_vcpu *vcpu)
 	u32 *intids;
 	int nr_irqs, i;
 
-	nr_irqs = vgic_copy_lpi_list(vcpu->kvm, &intids);
+	nr_irqs = vgic_copy_lpi_list(vcpu, &intids);
 	if (nr_irqs < 0)
 		return nr_irqs;
 
@@ -1027,7 +1027,7 @@ static int vgic_its_cmd_handle_invall(struct kvm *kvm, struct vgic_its *its,
 
 	vcpu = kvm_get_vcpu(kvm, collection->target_addr);
 
-	irq_count = vgic_copy_lpi_list(kvm, &intids);
+	irq_count = vgic_copy_lpi_list(vcpu, &intids);
 	if (irq_count < 0)
 		return irq_count;
 
Signature · 1 line
2.5.5

Assuming that it's ok to remove the irq_count check above, the rest of this patch looks good to me.

Thanks,
-Christoffer

author
#123 lore raw

Re: [PATCH v5 21/22] KVM: arm64: vgic-its: Fix pending table sync

Hi,

Quoted 47 lines · Christoffer Dall
Quoted 46 lines · Eric Auger

In its_sync_lpi_pending_table() we currently ignore the target_vcpu of the LPIs. We sync the pending bit found in the vcpu pending table even if the LPI is not targeting it.

Also in vgic_its_cmd_handle_invall() we are supposed to read the config table data for the LPIs associated to the collection ID. At the moment we refresh all LPI config information.

This patch passes a vpcu to vgic_copy_lpi_list() so that this latter returns a snapshot of the LPIs targeting this CPU and only those.

---
 virt/kvm/arm/vgic/vgic-its.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +5 -6
index 86dfc6c..be848be 100644
@@ -252,13 +252,13 @@ static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq,
 }
 
 /*
- * Create a snapshot of the current LPI list, so that we can enumerate all
- * LPIs without holding any lock.
- * Returns the array length and puts the kmalloc'ed array into intid_ptr.
+ * Create a snapshot of the current LPIs targeting @vcpu, so that we can
+ * enumerate those LPIs without holding any lock.
+ * Returns their number and puts the kmalloc'ed array into intid_ptr.
  */
-static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr)
+static int vgic_copy_lpi_list(struct kvm_vcpu *vcpu, u32 **intid_ptr)
 {
-	struct vgic_dist *dist = &kvm->arch.vgic;
+	struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
 	struct vgic_irq *irq;
 	u32 *intids;
 	int irq_count = dist->lpi_list_count, i = 0;
@@ -277,14 +277,14 @@ static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr)
 	spin_lock(&dist->lpi_list_lock);
 	list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
 		/* We don't need to "get" the IRQ, as we hold the list lock. */
-		intids[i] = irq->intid;
Quoted 8 lines

virt/kvm/arm/vgic/vgic-its.c static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr) · line 282

Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +3 -2
-		if (++i == irq_count)
-			break;
+		if (irq->target_vcpu != vcpu)
+			continue;
+		intids[i++] = irq->intid;

were we checking the ++i == irq_count condition for no good reason before since we can just drop it now?

I didn't get why we had that check.

Thanks

Eric

Quoted 47 lines
Quoted 40 lines
virt/kvm/arm/vgic/vgic-its.c +4 -4
 	}
 	spin_unlock(&dist->lpi_list_lock);
 
 	*intid_ptr = intids;
-	return irq_count;
+	return i;
 }
 
 /*
@@ -333,7 +333,7 @@ static u32 max_lpis_propbaser(u64 propbaser)
 }
 
 /*
- * Scan the whole LPI pending table and sync the pending bit in there
+ * Sync the pending table pending bit of LPIs targeting @vcpu
  * with our own data structures. This relies on the LPI being
  * mapped before.
  */
@@ -346,7 +346,7 @@ static int its_sync_lpi_pending_table(struct kvm_vcpu *vcpu)
 	u32 *intids;
 	int nr_irqs, i;
 
-	nr_irqs = vgic_copy_lpi_list(vcpu->kvm, &intids);
+	nr_irqs = vgic_copy_lpi_list(vcpu, &intids);
 	if (nr_irqs < 0)
 		return nr_irqs;
 
@@ -1027,7 +1027,7 @@ static int vgic_its_cmd_handle_invall(struct kvm *kvm, struct vgic_its *its,
 
 	vcpu = kvm_get_vcpu(kvm, collection->target_addr);
 
-	irq_count = vgic_copy_lpi_list(kvm, &intids);
+	irq_count = vgic_copy_lpi_list(vcpu, &intids);
 	if (irq_count < 0)
 		return irq_count;
 
Signature · 1 line
2.5.5

Assuming that it's ok to remove the irq_count check above, the rest of this patch looks good to me.

Thanks,
-Christoffer

#124 lore raw

Re: [PATCH v5 21/22] KVM: arm64: vgic-its: Fix pending table sync

Quoted 58 lines · Auger Eric

Hi,

Quoted 47 lines · Christoffer Dall
Quoted 46 lines · Eric Auger

In its_sync_lpi_pending_table() we currently ignore the target_vcpu of the LPIs. We sync the pending bit found in the vcpu pending table even if the LPI is not targeting it.

Also in vgic_its_cmd_handle_invall() we are supposed to read the config table data for the LPIs associated to the collection ID. At the moment we refresh all LPI config information.

This patch passes a vpcu to vgic_copy_lpi_list() so that this latter returns a snapshot of the LPIs targeting this CPU and only those.

---
 virt/kvm/arm/vgic/vgic-its.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
virt/kvm/arm/vgic/vgic-its.c +5 -6
index 86dfc6c..be848be 100644
@@ -252,13 +252,13 @@ static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq,
 }
 
 /*
- * Create a snapshot of the current LPI list, so that we can enumerate all
- * LPIs without holding any lock.
- * Returns the array length and puts the kmalloc'ed array into intid_ptr.
+ * Create a snapshot of the current LPIs targeting @vcpu, so that we can
+ * enumerate those LPIs without holding any lock.
+ * Returns their number and puts the kmalloc'ed array into intid_ptr.
  */
-static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr)
+static int vgic_copy_lpi_list(struct kvm_vcpu *vcpu, u32 **intid_ptr)
 {
-	struct vgic_dist *dist = &kvm->arch.vgic;
+	struct vgic_dist *dist = &vcpu->kvm->arch.vgic;
 	struct vgic_irq *irq;
 	u32 *intids;
 	int irq_count = dist->lpi_list_count, i = 0;
@@ -277,14 +277,14 @@ static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr)
 	spin_lock(&dist->lpi_list_lock);
 	list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
 		/* We don't need to "get" the IRQ, as we hold the list lock. */
-		intids[i] = irq->intid;
Quoted 8 lines

virt/kvm/arm/vgic/vgic-its.c static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr) · line 282

Quoted 5 lines
virt/kvm/arm/vgic/vgic-its.c +3 -2
-		if (++i == irq_count)
-			break;
+		if (irq->target_vcpu != vcpu)
+			continue;
+		intids[i++] = irq->intid;

were we checking the ++i == irq_count condition for no good reason before since we can just drop it now?

Quoted 2 lines

I didn't get why we had that check.

ok, Andre is cc'ed so unless he complains let's just get rid of it.

Thanks,
-Christoffer

author
#125 lore raw

PATCH v5 22/22 KVM: arm64: vgic-v3: KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES

In reply to Eric Auger

This patch adds a new attribute to GICV3 KVM device KVM_DEV_ARM_VGIC_GRP_CTRL group. This Allows the userspace to flush all GICR pending tables into guest RAM. At the moment we do not offer any restore control as the sync is implicit.

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].

---
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(-)
arch/arm/include/uapi/asm/kvm.h +1 -0
index 8e6563c..78fe803 100644
@@ -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
arch/arm64/include/uapi/asm/kvm.h +1 -0
index 1e35115..8a3758a 100644
@@ -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
include/linux/irqchip/arm-gic-v3.h +2 -0
index 0c6798c..9d3932f 100644
@@ -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
virt/kvm/arm/vgic/vgic-its.c +2 -4
index be848be..745c245 100644
@@ -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;
virt/kvm/arm/vgic/vgic-kvm-device.c +20 -0
index 859bfa8..d48743c 100644
@@ -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;
virt/kvm/arm/vgic/vgic-v3.c +54 -0

66 lines not on this page · raw message

virt/kvm/arm/vgic/vgic.h +1 -0
index 9bc52ef..535c2fc 100644
@@ -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);
Signature · 1 line
2.5.5
Tested-by Prakash, Brahmajyosyula on v5 22/22
#126 lore raw

Re: [PATCH v5 22/22] KVM: arm64: vgic-v3: KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES

Quoted 11 lines · Eric Auger

This patch adds a new attribute to GICV3 KVM device KVM_DEV_ARM_VGIC_GRP_CTRL group. This Allows the userspace to flush all GICR pending tables into guest RAM. At the moment we do not offer any restore control as the sync is implicit.

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].

#127 lore raw

Re: [PATCH v5 22/22] KVM: arm64: vgic-v3: KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES

In reply to Eric Auger
Quoted 2 lines · Eric Auger

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/

Quoted 2 lines

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.

Quoted 190 lines

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].

---
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(-)
arch/arm/include/uapi/asm/kvm.h +1 -0
index 8e6563c..78fe803 100644
@@ -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
arch/arm64/include/uapi/asm/kvm.h +1 -0
index 1e35115..8a3758a 100644
@@ -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
include/linux/irqchip/arm-gic-v3.h +2 -0
index 0c6798c..9d3932f 100644
@@ -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
virt/kvm/arm/vgic/vgic-its.c +2 -4
index be848be..745c245 100644
@@ -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;
virt/kvm/arm/vgic/vgic-kvm-device.c +20 -0
index 859bfa8..d48743c 100644
@@ -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;
virt/kvm/arm/vgic/vgic-v3.c +27 -0
index be0f4c3..1f0977f 100644
@@ -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;

virt/kvm/arm/vgic/vgic-v3.c void vgic_v3_enable(struct kvm_vcpu *vcpu) · line 291

Quoted 10 lines
virt/kvm/arm/vgic/vgic-v3.c +10 -0
+		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)' ?

virt/kvm/arm/vgic/vgic-v3.c void vgic_v3_enable(struct kvm_vcpu *vcpu) · line 303

Quoted 12 lines
virt/kvm/arm/vgic/vgic-v3.c +12 -0
+		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.

Thanks,
-Christoffer

Quoted 23 lines
virt/kvm/arm/vgic/vgic-v3.c +5 -0
+
+	return 0;
+}
+
+
 /* check for overlapping regions and for regions crossing the end of memory */
 static bool vgic_v3_check_base(struct kvm *kvm)
 {
virt/kvm/arm/vgic/vgic.h +1 -0
index 9bc52ef..535c2fc 100644
@@ -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);
Signature · 1 line
2.5.5
author
#128 lore raw

Re: [PATCH v5 22/22] KVM: arm64: vgic-v3: KVM_DEV_ARM_VGIC_SAVE_PENDING_TABLES

Hi Christoffer,

Quoted 206 lines · Christoffer Dall
Quoted 2 lines · Eric Auger

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/

Quoted 2 lines

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.

Quoted 194 lines

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].

---
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(-)
arch/arm/include/uapi/asm/kvm.h +1 -0
index 8e6563c..78fe803 100644
@@ -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
arch/arm64/include/uapi/asm/kvm.h +1 -0
index 1e35115..8a3758a 100644
@@ -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
include/linux/irqchip/arm-gic-v3.h +2 -0
index 0c6798c..9d3932f 100644
@@ -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
virt/kvm/arm/vgic/vgic-its.c +2 -4
index be848be..745c245 100644
@@ -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;
virt/kvm/arm/vgic/vgic-kvm-device.c +20 -0
index 859bfa8..d48743c 100644
@@ -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;
virt/kvm/arm/vgic/vgic-v3.c +31 -0
index be0f4c3..1f0977f 100644
@@ -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);
Quoted 8 lines

virt/kvm/arm/vgic/vgic-v3.c void vgic_v3_enable(struct kvm_vcpu *vcpu) · line 291

Quoted 6 lines
virt/kvm/arm/vgic/vgic-v3.c +6 -0
+			if (ret)
+				return ret;
+			last_byte_offset = byte_offset;
+		}
+
+		stored = val & bit_nr;

didn't you mean 'stored = val & (1 << bit_nr)' ?

yes thanks!

Quoted 31 lines

virt/kvm/arm/vgic/vgic-v3.c void vgic_v3_enable(struct kvm_vcpu *vcpu) · line 303

Quoted 13 lines
virt/kvm/arm/vgic/vgic-v3.c +12 -0
+		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;
		}
Quoted 4 lines

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

Quoted 27 lines

Thanks,
-Christoffer

Quoted 23 lines
virt/kvm/arm/vgic/vgic-v3.c +5 -0
+
+	return 0;
+}
+
+
 /* check for overlapping regions and for regions crossing the end of memory */
 static bool vgic_v3_check_base(struct kvm *kvm)
 {
virt/kvm/arm/vgic/vgic.h +1 -0
index 9bc52ef..535c2fc 100644
@@ -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);
Signature · 1 line
2.5.5
#130 lore raw

Hi Prakash,

Quoted 4 lines · Prakash B

Verified on Cavium ThunderX platform
Continousily migrated Guest more than 500 times b/w different hosts. Please feel free to add Tested-by: Prakash, Brahmajyosyula <[email protected]>

I appreciate you looking at these patches and testing them (reviews would have been even more helpful), but in the future you don't need to reply to each individual patch that you've tested it, but can just give your tested-by as a single reply to the cover letter.

Thanks,
-Christoffer

author
#131 lore raw
In reply to Prakash B

Hi Brahmajyosyula,

Quoted 4 lines · Prakash B

Verified on Cavium ThunderX platform
Continousily migrated Guest more than 500 times b/w different hosts. Please feel free to add Tested-by: Prakash, Brahmajyosyula <[email protected]>

I really appreciate. Many thanks!

Best Regards

Eric

* note you are not obliged to bother to send individual T-b on each patch. You can just send T-b on the cover letter and if requested exclude patches or features you could not test.

Quoted 6 lines

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel