Skip to contentSkip to conversation

KVM: arm64: vgic-its: clear dte when mapd unmaps a device

PATCH In review Kunkun Jiang 3 participants 6 messages Jun 13, 2024 – Jun 20, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: arm64: vgic-its: clear dte when mapd unmaps a device

vgic_its_save_device_tables will traverse its->device_list to save dte for each device. vgic_its_restore_device_tables will traverse each entry of device table and check if it is valid. Restore if valid.

But when mapd unmaps a devices, we do not invalidate the corresponding dte. In the scenario of continuous save and restore, there may be a situation where a device's dte is not saved but is restored. This is unreasonable and may cause restore to fail. This patch clears the corresponding dte when mapd unmaps a device.

---
 arch/arm64/kvm/vgic/vgic-its.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
arch/arm64/kvm/vgic/vgic-its.c +11 -2
index 40bb43f20bf3..dd5fba1e8ba3 100644
@@ -1140,8 +1140,12 @@ static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,
 	u8 num_eventid_bits = its_cmd_get_size(its_cmd);
 	gpa_t itt_addr = its_cmd_get_ittaddr(its_cmd);
 	struct its_device *device;
+	gpa_t gpa;
+	const struct vgic_its_abi *abi;
+	int dte_esz;
+	u64 val;
 
-	if (!vgic_its_check_id(its, its->baser_device_table, device_id, NULL))
+	if (!vgic_its_check_id(its, its->baser_device_table, device_id, &gpa))
 		return E_ITS_MAPD_DEVICE_OOR;
 
 	if (valid && num_eventid_bits > VITS_TYPER_IDBITS)
@@ -1161,8 +1165,13 @@ static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,
 	 * The spec does not say whether unmapping a not-mapped device
 	 * is an error, so we are done in any case.
 	 */
-	if (!valid)
+	if (!valid) {
+		abi = vgic_its_get_abi(its);
+		dte_esz = abi->dte_esz;
+		val = cpu_to_le64(0);
+		vgic_write_guest_lock(kvm, gpa, &val, dte_esz);
 		return 0;
+	}
 
 	device = vgic_its_alloc_device(its, device_id, itt_addr,
 				       num_eventid_bits);
Signature · 1 line
2.33.0
maintainer
#1 lore raw
Quoted 6 lines · Kunkun Jiang

vgic_its_save_device_tables will traverse its->device_list to save dte for each device. vgic_its_restore_device_tables will traverse each entry of device table and check if it is valid. Restore if valid.

Quoted 8 lines

But when mapd unmaps a devices, we do not invalidate the corresponding dte. In the scenario of continuous save and restore, there may be a situation where a device's dte is not saved but is restored. This is unreasonable and may cause restore to fail. This patch clears the corresponding dte when mapd unmaps a device.

^^^^^^^^^^^^^ This isn't a valid tag!

Quoted 1 line

Something is wrong. Either Shusen Li is the author and you are merely posting it, in which case there should be a 'From' line in the commit message, or you are co-authors and there should be a 'Co-developed-by' right after Shusen Li's SoB.

Please read Documentation/process/submitting-patches.rst for the details.

Quoted 9 lines
---
 arch/arm64/kvm/vgic/vgic-its.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
arch/arm64/kvm/vgic/vgic-its.c +0 -0
index 40bb43f20bf3..dd5fba1e8ba3 100644

arch/arm64/kvm/vgic/vgic-its.c static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its, · line 1146

Quoted 7 lines
arch/arm64/kvm/vgic/vgic-its.c +4 -0
@@ -1140,8 +1140,12 @@ static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,
 	u8 num_eventid_bits = its_cmd_get_size(its_cmd);
 	gpa_t itt_addr = its_cmd_get_ittaddr(its_cmd);
 	struct its_device *device;
+	gpa_t gpa;
+	const struct vgic_its_abi *abi;
+	int dte_esz;
+	u64 val;

Aside from 'gpa', all the extra variables should be moved into the !valid block. And it would be nicer if this variable was called something more descriptive.

Quoted 7 lines
arch/arm64/kvm/vgic/vgic-its.c +1 -1
-	if (!vgic_its_check_id(its, its->baser_device_table, device_id, NULL))
+	if (!vgic_its_check_id(its, its->baser_device_table, device_id, &gpa))
 		return E_ITS_MAPD_DEVICE_OOR;
 
 	if (valid && num_eventid_bits > VITS_TYPER_IDBITS)

arch/arm64/kvm/vgic/vgic-its.c static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its, · line 1172

Quoted 9 lines
arch/arm64/kvm/vgic/vgic-its.c +5 -1
@@ -1161,8 +1165,13 @@ static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,
 	 * The spec does not say whether unmapping a not-mapped device
 	 * is an error, so we are done in any case.
 	 */
-	if (!valid)
+	if (!valid) {
+		abi = vgic_its_get_abi(its);
+		dte_esz = abi->dte_esz;
+		val = cpu_to_le64(0);
+		vgic_write_guest_lock(kvm, gpa, &val, dte_esz);

This is just a bit wrong:

- you store 0 in a u64 after having (pointlessly) converted it to le64

- you write 'dte_esz' bytes into guest memory, but you don't have any check whether this is *larger* than the u64 you have passed

Similar issue exists in all the vgic_its_save_*() functions, calling for a generic fix, as we're just lucky to never have needed a new ABI so far. At the very least a check that we deal with data that is exactly 8 bytes.

Also, is the device table the only one that is subject to this 'reload' problem? How about the Collection table?

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#2 lore raw

Hi,

Quoted 74 lines · Marc Zyngier
Quoted 6 lines · Kunkun Jiang

vgic_its_save_device_tables will traverse its->device_list to save dte for each device. vgic_its_restore_device_tables will traverse each entry of device table and check if it is valid. Restore if valid.

Quoted 8 lines

But when mapd unmaps a devices, we do not invalidate the corresponding dte. In the scenario of continuous save and restore, there may be a situation where a device's dte is not saved but is restored. This is unreasonable and may cause restore to fail. This patch clears the corresponding dte when mapd unmaps a device.

^^^^^^^^^^^^^ This isn't a valid tag!

Quoted 1 line

Something is wrong. Either Shusen Li is the author and you are merely posting it, in which case there should be a 'From' line in the commit message, or you are co-authors and there should be a 'Co-developed-by' right after Shusen Li's SoB.

Please read Documentation/process/submitting-patches.rst for the details.

Quoted 9 lines
---
 arch/arm64/kvm/vgic/vgic-its.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
arch/arm64/kvm/vgic/vgic-its.c +0 -0
index 40bb43f20bf3..dd5fba1e8ba3 100644

arch/arm64/kvm/vgic/vgic-its.c static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its, · line 1146

Quoted 7 lines
arch/arm64/kvm/vgic/vgic-its.c +4 -0
@@ -1140,8 +1140,12 @@ static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,
 	u8 num_eventid_bits = its_cmd_get_size(its_cmd);
 	gpa_t itt_addr = its_cmd_get_ittaddr(its_cmd);
 	struct its_device *device;
+	gpa_t gpa;
+	const struct vgic_its_abi *abi;
+	int dte_esz;
+	u64 val;

Aside from 'gpa', all the extra variables should be moved into the !valid block. And it would be nicer if this variable was called something more descriptive.

Quoted 7 lines
arch/arm64/kvm/vgic/vgic-its.c +1 -1
-	if (!vgic_its_check_id(its, its->baser_device_table, device_id, NULL))
+	if (!vgic_its_check_id(its, its->baser_device_table, device_id, &gpa))
 		return E_ITS_MAPD_DEVICE_OOR;
 
 	if (valid && num_eventid_bits > VITS_TYPER_IDBITS)

arch/arm64/kvm/vgic/vgic-its.c static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its, · line 1172

Quoted 9 lines
arch/arm64/kvm/vgic/vgic-its.c +5 -1
@@ -1161,8 +1165,13 @@ static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,
 	 * The spec does not say whether unmapping a not-mapped device
 	 * is an error, so we are done in any case.
 	 */
-	if (!valid)
+	if (!valid) {
+		abi = vgic_its_get_abi(its);
+		dte_esz = abi->dte_esz;
+		val = cpu_to_le64(0);
+		vgic_write_guest_lock(kvm, gpa, &val, dte_esz);

This is just a bit wrong:

- you store 0 in a u64 after having (pointlessly) converted it to le64

- you write 'dte_esz' bytes into guest memory, but you don't have any check whether this is *larger* than the u64 you have passed

Quoted 7 lines

Similar issue exists in all the vgic_its_save_*() functions, calling for a generic fix, as we're just lucky to never have needed a new ABI so far. At the very least a check that we deal with data that is exactly 8 bytes.

Also, is the device table the only one that is subject to this 'reload' problem? How about the Collection table?

The collection table is not indexed by the collection ID and when saving we end up writing an invalid dummy entry to close the recording. So I don't think we have the same problem there.

For ITT I think we have the same issue on restore because the restore scans the table until it gets a first valid entry and then chain on next item. If we have stale entries this looks like the same issue.

Thanks

Eric

Quoted 5 lines

Thanks,

	M.
#3 lore raw

Hi Eric,

Quoted 5 lines · Eric Auger
Quoted 2 lines

Also, is the device table the only one that is subject to this 'reload' problem? How about the Collection table?

The collection table is not indexed by the collection ID and when saving we end up writing an invalid dummy entry to close the recording. So I don't think we have the same problem there.

I'm a little confused here. In the current Linux, there is no implementation of mapc unmaps a cte. But should this scenario be considered in vgic-its?

Quoted 3 lines

For ITT I think we have the same issue on restore because the restore scans the table until it gets a first valid entry and then chain on next item. If we have stale entries this looks like the same issue.

Thanks for your reply. I added a patch to fix it.
https://lore.kernel.org/kvmarm/[email protected]/

Thanks,
Kunkun Jiang

maintainer
#4 lore raw
Quoted 4 lines · Kunkun Jiang

Hi Eric,

On 2024/6/19 1:18, Eric Auger wrote:

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Also, is the device table the only one that is subject to this 'reload' problem? How about the Collection table?

The collection table is not indexed by the collection ID and when saving we end up writing an invalid dummy entry to close the recording. So I don't think we have the same problem there.

I'm a little confused here. In the current Linux, there is no implementation of mapc unmaps a cte. But should this scenario be considered in vgic-its?

We implement the "HW", not what Linux does.

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
#5 lore raw
In reply to Marc Zyngier

Hi Marc,

Thank you very much for reviewing.

Quoted 17 lines · Marc Zyngier
Quoted 5 lines · Kunkun Jiang

vgic_its_save_device_tables will traverse its->device_list to save dte for each device. vgic_its_restore_device_tables will traverse each entry of device table and check if it is valid. Restore if valid.

Quoted 8 lines

But when mapd unmaps a devices, we do not invalidate the corresponding dte. In the scenario of continuous save and restore, there may be a situation where a device's dte is not saved but is restored. This is unreasonable and may cause restore to fail. This patch clears the corresponding dte when mapd unmaps a device.

^^^^^^^^^^^^^ This isn't a valid tag!

Quoted 8 lines
Quoted 1 line

Something is wrong. Either Shusen Li is the author and you are merely posting it, in which case there should be a 'From' line in the commit message, or you are co-authors and there should be a 'Co-developed-by' right after Shusen Li's SoB.

Please read Documentation/process/submitting-patches.rst for the details.

The latest patch has been modified. See link below.

Quoted 38 lines
Quoted 9 lines
---
  arch/arm64/kvm/vgic/vgic-its.c | 13 +++++++++++--
  1 file changed, 11 insertions(+), 2 deletions(-)
arch/arm64/kvm/vgic/vgic-its.c +0 -0
index 40bb43f20bf3..dd5fba1e8ba3 100644

arch/arm64/kvm/vgic/vgic-its.c static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its, · line 1146

Quoted 7 lines
arch/arm64/kvm/vgic/vgic-its.c +4 -0
@@ -1140,8 +1140,12 @@ static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,
  	u8 num_eventid_bits = its_cmd_get_size(its_cmd);
  	gpa_t itt_addr = its_cmd_get_ittaddr(its_cmd);
  	struct its_device *device;
+	gpa_t gpa;
+	const struct vgic_its_abi *abi;
+	int dte_esz;
+	u64 val;

Aside from 'gpa', all the extra variables should be moved into the !valid block. And it would be nicer if this variable was called something more descriptive.

Quoted 7 lines
arch/arm64/kvm/vgic/vgic-its.c +1 -1
-	if (!vgic_its_check_id(its, its->baser_device_table, device_id, NULL))
+	if (!vgic_its_check_id(its, its->baser_device_table, device_id, &gpa))
  		return E_ITS_MAPD_DEVICE_OOR;
 
  	if (valid && num_eventid_bits > VITS_TYPER_IDBITS)

arch/arm64/kvm/vgic/vgic-its.c static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its, · line 1172

Quoted 9 lines
arch/arm64/kvm/vgic/vgic-its.c +5 -1
@@ -1161,8 +1165,13 @@ static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,
  	 * The spec does not say whether unmapping a not-mapped device
  	 * is an error, so we are done in any case.
  	 */
-	if (!valid)
+	if (!valid) {
+		abi = vgic_its_get_abi(its);
+		dte_esz = abi->dte_esz;
+		val = cpu_to_le64(0);
+		vgic_write_guest_lock(kvm, gpa, &val, dte_esz);

This is just a bit wrong:

Quoted 4 lines

- you store 0 in a u64 after having (pointlessly) converted it to le64

- you write 'dte_esz' bytes into guest memory, but you don't have any check whether this is *larger* than the u64 you have passed

The latest patch has been modified. See link below.

Quoted 4 lines

Similar issue exists in all the vgic_its_save_*() functions, calling for a generic fix, as we're just lucky to never have needed a new ABI so far. At the very least a check that we deal with data that is exactly 8 bytes.

A patch has been added to check it.

Thanks,
Kunkun Jiang