[PATCH 0/2] hw/acpi/aml-build: Fix free ranges calculation in crs_replace_with_free_ranges()

Anton Kuchin posted 2 patches 2 weeks, 2 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261107-users-antonkuchin-to-send-fix-crs-replace-with-free-ranges-v1-0-0cede42456c4@nebius.com
Maintainers: "Michael S. Tsirkin" <mst@redhat.com>, Igor Mammedov <imammedo@redhat.com>, Ani Sinha <anisinha@redhat.com>
hw/acpi/aml-build.c | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)
[PATCH 0/2] hw/acpi/aml-build: Fix free ranges calculation in crs_replace_with_free_ranges()
Posted by Anton Kuchin 2 weeks, 2 days ago
The assumption that all used ranges lie within the provided window
bounds resulted in generation of free ranges beyond the limits and
incorrect address assignment in guest. As used regions are collected
without any knowledge about which limits will be applied later, some
non-standard topologies with multiple expanders and devices break free
ranges calculation. Skipping such used regions fixes the problem.

While at this fix potential uint64 wrap around if range->limit is
equal to UINT64_MAX that can add extra free range at the beginning
of the address space. Not sure this can really happen with some
combination of virtual topology and firmware version, but this
check doesn't look expensive and it's better to handle it now than
spend hours debugging this strange case later.

Signed-off-by: Anton Kuchin <antonkuchin@nebius.com>
---
Anton Kuchin (2):
      hw/acpi/aml-build: Ignore used ranges outside the requested window
      hw/acpi/aml-build: Avoid overflow when advancing the free_base cursor

 hw/acpi/aml-build.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
---
base-commit: ddb42c53b4d91d12511f49ee4428d9594eb5eb49
change-id: 20261106-users-antonkuchin-to-send-fix-crs-replace-with-free-ranges-761219313073

Best regards,
--  
Anton Kuchin <antonkuchin@nebius.com>
Re: [PATCH 0/2] hw/acpi/aml-build: Fix free ranges calculation in crs_replace_with_free_ranges()
Posted by Michael S. Tsirkin 5 days, 16 hours ago
On Sat, Nov 07, 2026 at 12:59:47AM +0100, Anton Kuchin wrote:
> The assumption that all used ranges lie within the provided window
> bounds resulted in generation of free ranges beyond the limits and
> incorrect address assignment in guest. As used regions are collected
> without any knowledge about which limits will be applied later, some
> non-standard topologies with multiple expanders and devices break free
> ranges calculation. Skipping such used regions fixes the problem.

Could you be more specific please? Was an issue observed?
In which configurations? With which guests?


> While at this fix potential uint64 wrap around if range->limit is
> equal to UINT64_MAX that can add extra free range at the beginning
> of the address space. Not sure this can really happen with some
> combination of virtual topology and firmware version, but this
> check doesn't look expensive and it's better to handle it now than
> spend hours debugging this strange case later.

maybe but it should be documented better.


> Signed-off-by: Anton Kuchin <antonkuchin@nebius.com>
> ---
> Anton Kuchin (2):
>       hw/acpi/aml-build: Ignore used ranges outside the requested window
>       hw/acpi/aml-build: Avoid overflow when advancing the free_base cursor
> 
>  hw/acpi/aml-build.c | 18 +++++++++++++++++-
>  1 file changed, 17 insertions(+), 1 deletion(-)
> ---
> base-commit: ddb42c53b4d91d12511f49ee4428d9594eb5eb49
> change-id: 20261106-users-antonkuchin-to-send-fix-crs-replace-with-free-ranges-761219313073
> 
> Best regards,
> --  
> Anton Kuchin <antonkuchin@nebius.com>
Re: [PATCH 0/2] hw/acpi/aml-build: Fix free ranges calculation in crs_replace_with_free_ranges()
Posted by Anton Kuchin 3 days, 3 hours ago
On 17/11/2026 13:21, Michael S. Tsirkin wrote:
> On Sat, Nov 07, 2026 at 12:59:47AM +0100, Anton Kuchin wrote:
>> The assumption that all used ranges lie within the provided window
>> bounds resulted in generation of free ranges beyond the limits and
>> incorrect address assignment in guest. As used regions are collected
>> without any knowledge about which limits will be applied later, some
>> non-standard topologies with multiple expanders and devices break free
>> ranges calculation. Skipping such used regions fixes the problem.
> 
 > Was an issue observed?
 >

Yes, the issue was observed. Found it at the bottom of the rabbit hole 
while figuring out why guest panic detection wasn't working in some VM 
configurations.

 >
 > With which guests?
 >

Reproduced with regular ubuntu guest on 6.11 kernel and I also tested 
with 6.17 when it was the latest stable, didn't retest since then, 
though I believe that problem is not in the guest but in qemu ACPI 
generation logic.

 >
 > In which configurations?
 >

Unfortunately I didn't preserve the exact commandline, only guest log 
snippets and troubleshooting notes. It was GB300 VM with with 8 vSMMUs, 
9 PCI expanders, 21 vfio-pci devices plugged into them, plus some 
auxiliary PCI devices. I can try to reproduce the commandline if it 
turns out to be necessary but it will take time.

 >
 > Could you be more specific please?
 >

I discovered it on arm64 so the example will be for arm64, but x86_64 is 
affected too (and probably other platforms that use this code to build 
ACPI tables).

Arm virt machine has VIRT_PCIE_MMIO range [0x10000000-0x3efeffff] for 
PCIe MMIO, that gets assigned to GPEX config mmio32 base and ends up 
being used in acpi_dsdt_add_gpex() as limit to get free ranges by 
crs_replace_with_free_ranges().

Reserved ranges are collected by build_crs() which doesn't know about 
the windows and just sorts all regions into 3 buckets: IO, MMIO32 and 
MMIO64. In my case there was a reserved region [0x00100000-0x001fffff] 
way below mmio32 window that ended up in crs_range_set.mem_ranges bucket 
for MMIO32, I didn't dig what exactly reserved it.

Before the patch the first iteration of the loop starts with free_base = 
0x10000000, used->base = 0x00100000, so the range is not added but at 
the end of iteration free_base is set to used->limit+1
and suddenly jumps back from 0x10000000 to 0x00200000.

Then the second iteration takes next reserved region that belongs to 
mmio32 window [0x11400000-0x...], it passes the check free_base < 
used->base and the range [0x00200000-0x113fffff] is added as free mmio32 
region instead of [0x10000000-0x113fffff], with the 
[0x00200000-0x0fffffff] portion not backed by anything.

Guest kernel trusts ACPI and marks the region as available resource

[    1.194101] pci_bus 0000:00: resource 4 [mem 0x00200000-0x113fffff 
window]

Then unlucky device gets BAR assigned to it

[    1.810094] pci 0000:00:0b.0: BAR 0 [mem 0x00200000-0x0020000f]: assigned

... and doesn't work. In my case this happened to pvpanic-pci, but there 
is nothing specific about it and any pci device can fall into this trap.


After the patch the cursed region is gone and same qemu commandline 
resulted in correct assignment and working device

[    1.831232] pci 0000:00:0b.0: BAR 0 [mem 0x10001000-0x1000100f]: assigned



Similar thing can happen with reserved region above the limits, I've 
never saw it, just analyzed what can happen if the function receives 
some unexpected inputs.

Let's take same [0x10000000-0x3efeffff] limits for PCIe MMIO and 
reserved region above it [0x50000000-0x50000fff].

Without the patch such region is processed, and with free_base = 
0x10000000, used->base = 0x50000000 code

         if (free_base < used->base) {
             crs_range_insert(free_ranges, free_base, used->base - 1);
         }

adds region [0x10000000-0x4fffffff] instead of [0x10000000-0x3efeffff].



These cases can be handled by using MIN(used->base - 1, end) and 
MAX(used->limit, free_base) instead. Skipping obviously irrelevant 
regions just looks cleaner in my opinion.

> 
> 
>> While at this fix potential uint64 wrap around if range->limit is
>> equal to UINT64_MAX that can add extra free range at the beginning
>> of the address space. Not sure this can really happen with some
>> combination of virtual topology and firmware version, but this
>> check doesn't look expensive and it's better to handle it now than
>> spend hours debugging this strange case later.
> 
> maybe but it should be documented better.
> 

You are right, it should.

This one I didn't reproduce. But if we ever encounter the reserved 
region with limit equal to UINT64_MAX the lime

         free_base = used->limit + 1;

wraps free_base to become 0.

Then the code that handles the remaining free chunk after the loop

     if (free_base < end) {
         crs_range_insert(free_ranges, free_base, end);
     }

will insert invalid range [0x0-end].

I was thinking about adding check free_base != 0 at the tail insertion 
instead of checking inside the loop, but it will break the "valid" case 
when window starts at 0 and the ranges list is empty.

To be honest I find this extremely unlikely, that's one of the reasons 
why this part is a separate patch that can be dropped. There must be 
something really unusual that reserves a range ending at the limit of 
the address space, but with the variety of architectures, devices and 
firmwares it's hard to be 100% sure.

> 
>> Signed-off-by: Anton Kuchin <antonkuchin@nebius.com>
>> ---
>> Anton Kuchin (2):
>>        hw/acpi/aml-build: Ignore used ranges outside the requested window
>>        hw/acpi/aml-build: Avoid overflow when advancing the free_base cursor
>>
>>   hw/acpi/aml-build.c | 18 +++++++++++++++++-
>>   1 file changed, 17 insertions(+), 1 deletion(-)
>> ---
>> base-commit: ddb42c53b4d91d12511f49ee4428d9594eb5eb49
>> change-id: 20261106-users-antonkuchin-to-send-fix-crs-replace-with-free-ranges-761219313073
>>
>> Best regards,
>> --
>> Anton Kuchin <antonkuchin@nebius.com>
>