KVM: arm64: vgic-its: Some fixes about vgic-its
v2 cover KVM: arm64: vgic-its: Some fixes about vgic-its
There are three fixes about vgic-its:
Patch 1: A data length check is added in vgic_its_save_* Patch 2-3: Fix two bugs in the scenario of continuous save and restore
History:
v1 -> v2:
- Replaced BUG_ON() with KVM_BUG_ON()
Kunkun Jiang (3):
KVM: arm64: vgic-its: Add a data length check in vgic_its_save_*
KVM: arm64: vgic-its: Clear dte when mapd unmaps a device
KVM: arm64: vgic-its: Clear ite when discard frees an ite
arch/arm64/kvm/vgic/vgic-its.c | 44 ++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-)
Signature · 1 line
2.27.0
PATCH v2 1/3 KVM: arm64: vgic-its: Add a data length check in vgic_its_save_*
In all the vgic_its_save_*() functinos, it does not check whether the data length is 8 bytes before calling vgic_write_guest_lock. This patch add the check. To prevent the kernel from being blown up when the fault occurs, KVM_BUG_ON() is used. And the other BUG_ON()'s are replaced together.
--- arch/arm64/kvm/vgic/vgic-its.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)
arch/arm64/kvm/vgic/vgic-its.c +19 -2
@@ -2094,6 +2094,10 @@ static int vgic_its_save_ite(struct vgic_its *its, struct its_device *dev, ((u64 )ite->irq->intid <<KVM_ITS_ITE_PINTID_SHIFT ) | ite->collection->collection_id; val =cpu_to_le64 (val); + +if (KVM_BUG_ON (ite_esz !=sizeof (val), kvm)) +return -EINVAL ; +return vgic_write_guest_lock (kvm, gpa, &val, ite_esz); }
@@ -2246,6 +2250,10 @@ static int vgic_its_save_dte(struct vgic_its *its, struct its_device *dev, (itt_addr_field <<KVM_ITS_DTE_ITTADDR_SHIFT ) | (dev->num_eventid_bits - 1)); val =cpu_to_le64 (val); + +if (KVM_BUG_ON (dte_esz !=sizeof (val), kvm)) +return -EINVAL ; +return vgic_write_guest_lock (kvm, ptr, &val, dte_esz); }
@@ -2427,12 +2435,17 @@ static int vgic_its_save_cte(struct vgic_its *its,struct its_collection *collection, gpa_t gpa, int esz) { + struct kvm *kvm = its->dev->kvm;u64 val; val = (1ULL <<KVM_ITS_CTE_VALID_SHIFT | ((u64 )collection->target_addr <<KVM_ITS_CTE_RDBASE_SHIFT ) | collection->collection_id); val =cpu_to_le64 (val); + +if (KVM_BUG_ON (esz !=sizeof (val), kvm)) +return -EINVAL ; +return vgic_write_guest_lock (its->dev->kvm, gpa, &val, esz); }
@@ -2449,7 +2462,9 @@ static int vgic_its_restore_cte(struct vgic_its *its, gpa_t gpa, int esz)u64 val;int ret; -BUG_ON (esz >sizeof (val)); +if (KVM_BUG_ON (esz !=sizeof (val), kvm)) +return -EINVAL ; + ret =kvm_read_guest_lock (kvm, gpa, &val, esz);if (ret)return ret;
@@ -2513,7 +2528,9 @@ static int vgic_its_save_collection_table(struct vgic_its *its)* with valid bit unset */ val =0 ; -BUG_ON (cte_esz >sizeof (val)); +if (KVM_BUG_ON (cte_esz !=sizeof (val), its->dev->kvm)) +return -EINVAL ; + ret =vgic_write_guest_lock (its->dev->kvm, gpa, &val, cte_esz);return ret; }
Signature · 1 line
2.27.0
Re: [PATCH v2 1/3] KVM: arm64: vgic-its: Add a data length check in vgic_its_save_*
Hi Kunkun,
Quoted 2 lines · Kunkun Jiang
In all the vgic_its_save_*() functinos, it does not check whether
typo: functions
Quoted 2 lines
the data length is 8 bytes before calling
vgic_write_guest_lock. This patch add the check. To prevent the kernel from being blown up
typo: adds
Quoted 75 lines
when the fault occurs,
KVM_BUG_ON()is used. And the other BUG_ON()'s are replaced together.Signed-off-by Kunkun Jiang <[email protected]>--- arch/arm64/kvm/vgic/vgic-its.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)arch/arm64/kvm/vgic/vgic-its.c +19
-3@@ -2094,6 +2094,10 @@ static int vgic_its_save_ite(struct vgic_its *its, struct its_device *dev, ((u64 )ite->irq->intid <<KVM_ITS_ITE_PINTID_SHIFT ) | ite->collection->collection_id; val =cpu_to_le64 (val); + +if (KVM_BUG_ON (ite_esz !=sizeof (val), kvm)) +return -EINVAL ; +return vgic_write_guest_lock (kvm, gpa, &val, ite_esz); }@@ -2246,6 +2250,10 @@ static int vgic_its_save_dte(struct vgic_its *its, struct its_device *dev, (itt_addr_field <<KVM_ITS_DTE_ITTADDR_SHIFT ) | (dev->num_eventid_bits - 1)); val =cpu_to_le64 (val); + +if (KVM_BUG_ON (dte_esz !=sizeof (val), kvm)) +return -EINVAL ; +return vgic_write_guest_lock (kvm, ptr, &val, dte_esz); }@@ -2427,12 +2435,17 @@ static int vgic_its_save_cte(struct vgic_its *its,struct its_collection *collection, gpa_t gpa, int esz) { + struct kvm *kvm = its->dev->kvm;u64 val; val = (1ULL <<KVM_ITS_CTE_VALID_SHIFT | ((u64 )collection->target_addr <<KVM_ITS_CTE_RDBASE_SHIFT ) | collection->collection_id); val =cpu_to_le64 (val); + +if (KVM_BUG_ON (esz !=sizeof (val), kvm)) +return -EINVAL ; +return vgic_write_guest_lock (its->dev->kvm, gpa, &val, esz); }@@ -2449,7 +2462,9 @@ static int vgic_its_restore_cte(struct vgic_its *its, gpa_t gpa, int esz)u64 val;int ret; -BUG_ON (esz >sizeof (val)); +if (KVM_BUG_ON (esz !=sizeof (val), kvm)) +return -EINVAL ; + ret =kvm_read_guest_lock (kvm, gpa, &val, esz);if (ret)return ret;@@ -2513,7 +2528,9 @@ static int vgic_its_save_collection_table(struct vgic_its *its)* with valid bit unset */ val =0 ; -BUG_ON (cte_esz >sizeof (val)); +if (KVM_BUG_ON (cte_esz !=sizeof (val), its->dev->kvm)) +return -EINVAL ; + ret =vgic_write_guest_lock (its->dev->kvm, gpa, &val, cte_esz);return ret; } --
Quoted 3 lines
2.27.0
Jing
PATCH v2 2/3 KVM: arm64: vgic-its: Clear dte when mapd unmaps a device
In reply to Kunkun Jiangvgic_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 | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
arch/arm64/kvm/vgic/vgic-its.c +12 -2
@@ -1140,8 +1140,9 @@ 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; -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 +1162,17 @@ 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) { +struct kvm *kvm = its->dev->kvm; +int dte_esz =vgic_its_get_abi (its)->dte_esz; +u64 val =0 ; + +if (KVM_BUG_ON (dte_esz !=sizeof (val), kvm)) +return -EINVAL ; + +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.27.0
Zenghui Yu
reviewerRe: [PATCH v2 2/3] KVM: arm64: vgic-its: Clear dte when mapd unmaps a device
Quoted 12 lines · Kunkun Jiang
vgic_its_save_device_tableswill traverseits->device_listto save dte for each device.vgic_its_restore_device_tableswill 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.
Quoted 2 lines
Co-developed-by Shusen Li <[email protected]>Singed-off-by Shusen Li <[email protected]>
^^^^^^
Please do fix the tag. checkpatch.pl should have complained about it already if you run it.
Zenghui
Re: [PATCH v2 2/3] KVM: arm64: vgic-its: Clear dte when mapd unmaps a device
In reply to Kunkun JiangHi Kunkun,
Quoted 7 lines · Kunkun Jiang
vgic_its_save_device_tableswill traverseits->device_listto save dte for each device.vgic_its_restore_device_tableswill 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
type: devices -> device
Quoted 8 lines
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.
Co-developed-by Shusen Li <[email protected]>Singed-off-by Shusen Li <[email protected]>
typo: Signed
Quoted 40 lines
Signed-off-by Kunkun Jiang <[email protected]>--- arch/arm64/kvm/vgic/vgic-its.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)arch/arm64/kvm/vgic/vgic-its.c +12
-3@@ -1140,8 +1140,9 @@ 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; -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 +1162,17 @@ 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) { +struct kvm *kvm = its->dev->kvm; +int dte_esz =vgic_its_get_abi (its)->dte_esz; +u64 val =0 ; + +if (KVM_BUG_ON (dte_esz !=sizeof (val), kvm)) +return -EINVAL ; + +vgic_write_guest_lock (kvm, gpa, &val, dte_esz);return 0 ; + } device =vgic_its_alloc_device (its, device_id, itt_addr, num_eventid_bits); --
Quoted 3 lines
2.27.0
Jing
PATCH v2 3/3 KVM: arm64: vgic-its: Clear ite when discard frees an ite
In reply to Kunkun JiangWhen discard frees an ite, we do not invalidate the corresponding ite. In the scenario of continuous save and restore, there may be a situation where an ite is not saved but is restored. This is unreasonable and may cause restore to fail. This patch clears the corresponding ite when discard frees a ite.
--- arch/arm64/kvm/vgic/vgic-its.c | 9 +++++++++ 1 file changed, 9 insertions(+)
arch/arm64/kvm/vgic/vgic-its.c +9 -0
@@ -782,6 +782,10 @@ static int vgic_its_cmd_handle_discard(struct kvm *kvm, struct vgic_its *its, ite =find_ite (its, device_id, event_id);if (ite &&its_is_collection_mapped (ite->collection)) { +struct its_device *device =find_its_device (its, device_id); +int ite_esz =vgic_its_get_abi (its)->ite_esz; +gpa_t gpa = device->itt_addr + ite->event_id * ite_esz; +u64 val =0 ;/* * Though the spec talks about removing the pending state, we * don't bother here since we clear the ITTE anyway and the
@@ -790,6 +794,11 @@ static int vgic_its_cmd_handle_discard(struct kvm *kvm, struct vgic_its *its,vgic_its_invalidate_cache (its);its_free_ite (kvm, ite); + +if (KVM_BUG_ON (ite_esz !=sizeof (val), kvm)) +return -EINVAL ; + +vgic_write_guest_lock (kvm, gpa, &val, ite_esz);return 0 ; }
Signature · 1 line
2.27.0
Re: [PATCH v2 3/3] KVM: arm64: vgic-its: Clear ite when discard frees an ite
Hi Kunkun,
Quoted 7 lines · Kunkun Jiang
When discard frees an ite, we do not invalidate the corresponding ite. In the scenario of continuous save and restore, there may be a situation where an ite is not saved but is restored. This is unreasonable and may cause restore to fail. This patch clears the corresponding ite when discard frees a ite.
nit: ite -> ITE
Quoted 34 lines
Signed-off-by Kunkun Jiang <[email protected]>--- arch/arm64/kvm/vgic/vgic-its.c | 9 +++++++++ 1 file changed, 9 insertions(+)arch/arm64/kvm/vgic/vgic-its.c +9
-1@@ -782,6 +782,10 @@ static int vgic_its_cmd_handle_discard(struct kvm *kvm, struct vgic_its *its, ite =find_ite (its, device_id, event_id);if (ite &&its_is_collection_mapped (ite->collection)) { +struct its_device *device =find_its_device (its, device_id); +int ite_esz =vgic_its_get_abi (its)->ite_esz; +gpa_t gpa = device->itt_addr + ite->event_id * ite_esz; +u64 val =0 ;/* * Though the spec talks about removing the pending state, we * don't bother here since we clear the ITTE anyway and the @@ -790,6 +794,11 @@ static int vgic_its_cmd_handle_discard(struct kvm *kvm, struct vgic_its *its,vgic_its_invalidate_cache (its);its_free_ite (kvm, ite); + +if (KVM_BUG_ON (ite_esz !=sizeof (val), kvm)) +return -EINVAL ; + +vgic_write_guest_lock (kvm, gpa, &val, ite_esz);return 0 ; } --
Quoted 3 lines
2.27.0
Jing
Hi Kunkun,
Do you have a plan to respin this patch series?
We encountered an issue during the VM suspend/resume test that can be resolved by this patch series.
During VM suspending, the ITS table saving is called multiple times. Concurrently, device table and ITT table entries are mapped/unmapped due to devices' suspend/resume. This leaves some free entries dirty (valid bit equals 1).
The table restore starts from the first entry to find a valid one. There is a chance that the entry found is a free one, but with the valid bit as 1.
Jing
Quoted 21 lines · Kunkun Jiang
There are three fixes about vgic-its:
Patch 1: A data length check is added in vgic_its_save_* Patch 2-3: Fix two bugs in the scenario of continuous save and restoreHistory:
v1 -> v2:
- ReplacedBUG_ON()withKVM_BUG_ON()Kunkun Jiang (3):
KVM: arm64: vgic-its: Add a data length check in vgic_its_save_*
KVM: arm64: vgic-its: Clear dte when mapd unmaps a device
KVM: arm64: vgic-its: Clear ite when discard frees an itearch/arm64/kvm/vgic/vgic-its.c | 44 ++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-)--
2.27.0