linux/arch/arm64/kvm/vgic
Eric Ren c000a26071 KVM: arm64: vgic: Fix exit condition in scan_its_table()
With some PCIe topologies, restoring a guest fails while
parsing the ITS device tables.

Reproducer hints:
1. Create ARM virt VM with pxb-pcie bus which adds
   extra host bridges, with qemu command like:

```
  -device pxb-pcie,bus_nr=8,id=pci.x,numa_node=0,bus=pcie.0 \
  -device pcie-root-port,..,bus=pci.x \
  ...
  -device pxb-pcie,bus_nr=37,id=pci.y,numa_node=1,bus=pcie.0 \
  -device pcie-root-port,..,bus=pci.y \
  ...

```
2. Ensure the guest uses 2-level device table
3. Perform VM migration which calls save/restore device tables

In that setup, we get a big "offset" between 2 device_ids,
which makes unsigned "len" round up a big positive number,
causing the scan loop to continue with a bad GPA. For example:

1. L1 table has 2 entries;
2. and we are now scanning at L2 table entry index 2075 (pointed
   to by L1 first entry)
3. if next device id is 9472, we will get a big offset: 7397;
4. with unsigned 'len', 'len -= offset * esz', len will underflow to a
   positive number, mistakenly into next iteration with a bad GPA;
   (It should break out of the current L2 table scanning, and jump
   into the next L1 table entry)
5. that bad GPA fails the guest read.

Fix it by stopping the L2 table scan when the next device id is
outside of the current table, allowing the scan to continue from
the next L1 table entry.

Thanks to Eric Auger for the fix suggestion.

Fixes: 920a7a8fa9 ("KVM: arm64: vgic-its: Add infrastructure for tableookup")
Suggested-by: Eric Auger <eric.auger@redhat.com>
Signed-off-by: Eric Ren <renzhengeek@gmail.com>
[maz: commit message tidy-up]
Signed-off-by: Marc Zyngier <maz@kernel.org>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/d9c3a564af9e2c5bf63f48a7dcbf08cd593c5c0b.1665802985.git.renzhengeek@gmail.com
2022-10-15 12:10:54 +01:00
..
trace.h KVM: arm64: Move virt/kvm/arm to arch/arm64 2020-05-16 15:03:59 +01:00
vgic-debug.c KVM: arm64: vgic: Remove unnecessary type castings 2022-04-06 10:42:55 +01:00
vgic-init.c KVM/arm64 updates for 5.19 2022-05-25 05:09:23 -04:00
vgic-irqfd.c KVM: arm64: vgic: Add memcg accounting to vgic allocations 2021-10-17 11:25:55 +01:00
vgic-its.c KVM: arm64: vgic: Fix exit condition in scan_its_table() 2022-10-15 12:10:54 +01:00
vgic-kvm-device.c KVM: arm64: vgic: Tidy-up calls to vgic_{get,set}_common_attr() 2022-07-17 11:55:33 +01:00
vgic-mmio-v2.c KVM: arm64: Don't read a HW interrupt pending state in user context 2022-06-07 16:28:19 +01:00
vgic-mmio-v3.c KVM: arm64: vgic-v3: Use u32 to manage the line level from userspace 2022-07-17 11:55:33 +01:00
vgic-mmio.c KVM: arm64: vgic-v3: Use u32 to manage the line level from userspace 2022-07-17 11:55:33 +01:00
vgic-mmio.h KVM: arm64: vgic-v3: Use u32 to manage the line level from userspace 2022-07-17 11:55:33 +01:00
vgic-v2.c Merge branch kvm-arm64/vgic-fixes-5.17 into kvmarm-master/next 2021-12-16 12:54:12 +00:00
vgic-v3.c KVM: arm64: vgic-v3: List M1 Pro/Max as requiring the SEIS workaround 2022-05-15 11:18:50 +01:00
vgic-v4.c KVM: Use 'unsigned long' as kvm_for_each_vcpu()'s index 2021-12-08 04:24:15 -05:00
vgic.c KVM: arm64: fix typos in comments 2022-03-18 14:04:15 +00:00
vgic.h KVM: arm64: vgic-v3: Use u32 to manage the line level from userspace 2022-07-17 11:55:33 +01:00