* [PATCH 0/2] hw/acpi/aml-build: Fix free ranges calculation in crs_replace_with_free_ranges()
@ 2026-11-06 23:59 Anton Kuchin
2026-11-06 23:59 ` [PATCH 1/2] hw/acpi/aml-build: Ignore used ranges outside the requested window Anton Kuchin
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Anton Kuchin @ 2026-11-06 23:59 UTC (permalink / raw)
To: qemu-devel
Cc: Michael S. Tsirkin, Igor Mammedov, Ani Sinha, Anton Kuchin,
NB-CoreTeam
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>
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH 1/2] hw/acpi/aml-build: Ignore used ranges outside the requested window 2026-11-06 23:59 [PATCH 0/2] hw/acpi/aml-build: Fix free ranges calculation in crs_replace_with_free_ranges() Anton Kuchin @ 2026-11-06 23:59 ` Anton Kuchin 2026-11-17 12:19 ` Michael S. Tsirkin 2026-11-17 12:28 ` Michael S. Tsirkin 2026-11-06 23:59 ` [PATCH 2/2] hw/acpi/aml-build: Avoid overflow when advancing the free_base cursor Anton Kuchin 2026-11-17 12:21 ` [PATCH 0/2] hw/acpi/aml-build: Fix free ranges calculation in crs_replace_with_free_ranges() Michael S. Tsirkin 2 siblings, 2 replies; 7+ messages in thread From: Anton Kuchin @ 2026-11-06 23:59 UTC (permalink / raw) To: qemu-devel Cc: Michael S. Tsirkin, Igor Mammedov, Ani Sinha, Anton Kuchin, NB-CoreTeam build_crs() collects used resources without any information about valid address windows. These resources can therefore lie outside the interval passed to crs_replace_with_free_ranges(). crs_replace_with_free_ranges() assumed all used ranges lie within the interval. Ranges outside it could therefore produce free ranges extending below or above its bounds. As a result, the guest kernel may assign a device an invalid address range. Skip ranges with limit below the interval start or with base above the interval's end. Signed-off-by: Anton Kuchin <antonkuchin@nebius.com> --- hw/acpi/aml-build.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index 990abc64cd8b..90837b513e69 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -124,7 +124,7 @@ static gint crs_range_compare(gconstpointer a, gconstpointer b) } /* - * crs_replace_with_free_ranges - given the 'used' ranges within [start - end] + * crs_replace_with_free_ranges - given the 'used' ranges and [start - end] * interval, computes the 'free' ranges from the same interval. * Example: If the input array is { [a1 - a2],[b1 - b2] }, the function * will return { [base - a1], [a2 - b1], [b2 - limit] }. @@ -140,6 +140,16 @@ void crs_replace_with_free_ranges(GPtrArray *ranges, for (i = 0; i < ranges->len; i++) { CrsRangeEntry *used = g_ptr_array_index(ranges, i); + /* skip ranges below the window */ + if (used->limit < free_base) { + continue; + } + + /* skip ranges above the window */ + if (used->base > end) { + break; + } + if (free_base < used->base) { crs_range_insert(free_ranges, free_base, used->base - 1); } -- 2.43.0 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] hw/acpi/aml-build: Ignore used ranges outside the requested window 2026-11-06 23:59 ` [PATCH 1/2] hw/acpi/aml-build: Ignore used ranges outside the requested window Anton Kuchin @ 2026-11-17 12:19 ` Michael S. Tsirkin 2026-11-17 12:28 ` Michael S. Tsirkin 1 sibling, 0 replies; 7+ messages in thread From: Michael S. Tsirkin @ 2026-11-17 12:19 UTC (permalink / raw) To: Anton Kuchin; +Cc: qemu-devel, Igor Mammedov, Ani Sinha, NB-CoreTeam On Sat, Nov 07, 2026 at 12:59:48AM +0100, Anton Kuchin wrote: > build_crs() collects used resources without any information about > valid address windows. These resources can therefore lie outside > the interval passed to crs_replace_with_free_ranges(). > > crs_replace_with_free_ranges() assumed all used ranges lie within > the interval. you mean assumes? > Ranges outside it could therefore produce free ranges > extending below or above its bounds. could in what sense? does it produce them? in what configuratoons? > As a result, the guest kernel > may assign a device an invalid address range. And does it? what guest? > > Skip ranges with limit below the interval start or with base above > the interval's end. and then what happens? > > Signed-off-by: Anton Kuchin <antonkuchin@nebius.com> > --- > hw/acpi/aml-build.c | 12 +++++++++++- > 1 file changed, 11 insertions(+), 1 deletion(-) > > diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c > index 990abc64cd8b..90837b513e69 100644 > --- a/hw/acpi/aml-build.c > +++ b/hw/acpi/aml-build.c > @@ -124,7 +124,7 @@ static gint crs_range_compare(gconstpointer a, gconstpointer b) > } > > /* > - * crs_replace_with_free_ranges - given the 'used' ranges within [start - end] > + * crs_replace_with_free_ranges - given the 'used' ranges and [start - end] > * interval, computes the 'free' ranges from the same interval. > * Example: If the input array is { [a1 - a2],[b1 - b2] }, the function > * will return { [base - a1], [a2 - b1], [b2 - limit] }. > @@ -140,6 +140,16 @@ void crs_replace_with_free_ranges(GPtrArray *ranges, > for (i = 0; i < ranges->len; i++) { > CrsRangeEntry *used = g_ptr_array_index(ranges, i); > > + /* skip ranges below the window */ > + if (used->limit < free_base) { > + continue; > + } > + > + /* skip ranges above the window */ > + if (used->base > end) { > + break; > + } > + > if (free_base < used->base) { > crs_range_insert(free_ranges, free_base, used->base - 1); > } > > -- > 2.43.0 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] hw/acpi/aml-build: Ignore used ranges outside the requested window 2026-11-06 23:59 ` [PATCH 1/2] hw/acpi/aml-build: Ignore used ranges outside the requested window Anton Kuchin 2026-11-17 12:19 ` Michael S. Tsirkin @ 2026-11-17 12:28 ` Michael S. Tsirkin 1 sibling, 0 replies; 7+ messages in thread From: Michael S. Tsirkin @ 2026-11-17 12:28 UTC (permalink / raw) To: Anton Kuchin; +Cc: qemu-devel, Igor Mammedov, Ani Sinha, NB-CoreTeam On Sat, Nov 07, 2026 at 12:59:48AM +0100, Anton Kuchin wrote: > build_crs() collects used resources without any information about > valid address windows. These resources can therefore lie outside > the interval passed to crs_replace_with_free_ranges(). > > crs_replace_with_free_ranges() assumed all used ranges lie within > the interval. Ranges outside it could therefore produce free ranges > extending below or above its bounds. As a result, the guest kernel > may assign a device an invalid address range. > > Skip ranges with limit below the interval start or with base above > the interval's end. > > Signed-off-by: Anton Kuchin <antonkuchin@nebius.com> Given it's a bugfix, pls figure out when was the bug introduced and include a Fixes: tag. > --- > hw/acpi/aml-build.c | 12 +++++++++++- > 1 file changed, 11 insertions(+), 1 deletion(-) > > diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c > index 990abc64cd8b..90837b513e69 100644 > --- a/hw/acpi/aml-build.c > +++ b/hw/acpi/aml-build.c > @@ -124,7 +124,7 @@ static gint crs_range_compare(gconstpointer a, gconstpointer b) > } > > /* > - * crs_replace_with_free_ranges - given the 'used' ranges within [start - end] > + * crs_replace_with_free_ranges - given the 'used' ranges and [start - end] > * interval, computes the 'free' ranges from the same interval. > * Example: If the input array is { [a1 - a2],[b1 - b2] }, the function > * will return { [base - a1], [a2 - b1], [b2 - limit] }. > @@ -140,6 +140,16 @@ void crs_replace_with_free_ranges(GPtrArray *ranges, > for (i = 0; i < ranges->len; i++) { > CrsRangeEntry *used = g_ptr_array_index(ranges, i); > > + /* skip ranges below the window */ > + if (used->limit < free_base) { > + continue; > + } > + > + /* skip ranges above the window */ > + if (used->base > end) { > + break; > + } > + > if (free_base < used->base) { > crs_range_insert(free_ranges, free_base, used->base - 1); > } > > -- > 2.43.0 ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2/2] hw/acpi/aml-build: Avoid overflow when advancing the free_base cursor 2026-11-06 23:59 [PATCH 0/2] hw/acpi/aml-build: Fix free ranges calculation in crs_replace_with_free_ranges() Anton Kuchin 2026-11-06 23:59 ` [PATCH 1/2] hw/acpi/aml-build: Ignore used ranges outside the requested window Anton Kuchin @ 2026-11-06 23:59 ` Anton Kuchin 2026-11-17 12:20 ` Michael S. Tsirkin 2026-11-17 12:21 ` [PATCH 0/2] hw/acpi/aml-build: Fix free ranges calculation in crs_replace_with_free_ranges() Michael S. Tsirkin 2 siblings, 1 reply; 7+ messages in thread From: Anton Kuchin @ 2026-11-06 23:59 UTC (permalink / raw) To: qemu-devel Cc: Michael S. Tsirkin, Igor Mammedov, Ani Sinha, Anton Kuchin, NB-CoreTeam crs_replace_with_free_ranges() advances its cursor past each used range by adding one to the range's limit. If the limit is UINT64_MAX, the cursor wraps to zero, potentially producing free ranges outside the requested window or overlapping used resources. Adjust free_base and stop processing when a used range reaches or exceeds the interval's upper bound to avoid cursor wrapping and final free range insertion. Signed-off-by: Anton Kuchin <antonkuchin@nebius.com> --- hw/acpi/aml-build.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c index 90837b513e69..b71d82b5e8a9 100644 --- a/hw/acpi/aml-build.c +++ b/hw/acpi/aml-build.c @@ -154,6 +154,12 @@ void crs_replace_with_free_ranges(GPtrArray *ranges, crs_range_insert(free_ranges, free_base, used->base - 1); } + /* avoid free_base overflow when used->limit equals UINT64_MAX */ + if (used->limit >= end) { + free_base = end; + break; + } + free_base = used->limit + 1; } -- 2.43.0 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 2/2] hw/acpi/aml-build: Avoid overflow when advancing the free_base cursor 2026-11-06 23:59 ` [PATCH 2/2] hw/acpi/aml-build: Avoid overflow when advancing the free_base cursor Anton Kuchin @ 2026-11-17 12:20 ` Michael S. Tsirkin 0 siblings, 0 replies; 7+ messages in thread From: Michael S. Tsirkin @ 2026-11-17 12:20 UTC (permalink / raw) To: Anton Kuchin; +Cc: qemu-devel, Igor Mammedov, Ani Sinha, NB-CoreTeam On Sat, Nov 07, 2026 at 12:59:49AM +0100, Anton Kuchin wrote: > crs_replace_with_free_ranges() advances its cursor past each used > range by adding one to the range's limit. If the limit is > UINT64_MAX, when is it UINT64_MAX? > the cursor wraps to zero, potentially producing free > ranges outside the requested window or overlapping used resources. > > Adjust free_base and stop processing when a used range reaches or > exceeds the interval's upper bound to avoid cursor wrapping and > final free range insertion. was this tested? how? > > Signed-off-by: Anton Kuchin <antonkuchin@nebius.com> > --- > hw/acpi/aml-build.c | 6 ++++++ > 1 file changed, 6 insertions(+) > > diff --git a/hw/acpi/aml-build.c b/hw/acpi/aml-build.c > index 90837b513e69..b71d82b5e8a9 100644 > --- a/hw/acpi/aml-build.c > +++ b/hw/acpi/aml-build.c > @@ -154,6 +154,12 @@ void crs_replace_with_free_ranges(GPtrArray *ranges, > crs_range_insert(free_ranges, free_base, used->base - 1); > } > > + /* avoid free_base overflow when used->limit equals UINT64_MAX */ > + if (used->limit >= end) { > + free_base = end; > + break; > + } > + > free_base = used->limit + 1; > } > > > -- > 2.43.0 ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 0/2] hw/acpi/aml-build: Fix free ranges calculation in crs_replace_with_free_ranges() 2026-11-06 23:59 [PATCH 0/2] hw/acpi/aml-build: Fix free ranges calculation in crs_replace_with_free_ranges() Anton Kuchin 2026-11-06 23:59 ` [PATCH 1/2] hw/acpi/aml-build: Ignore used ranges outside the requested window Anton Kuchin 2026-11-06 23:59 ` [PATCH 2/2] hw/acpi/aml-build: Avoid overflow when advancing the free_base cursor Anton Kuchin @ 2026-11-17 12:21 ` Michael S. Tsirkin 2 siblings, 0 replies; 7+ messages in thread From: Michael S. Tsirkin @ 2026-11-17 12:21 UTC (permalink / raw) To: Anton Kuchin; +Cc: qemu-devel, Igor Mammedov, Ani Sinha, NB-CoreTeam 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> ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-17 12:29 UTC | newest] Thread overview: 7+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-06 23:59 [PATCH 0/2] hw/acpi/aml-build: Fix free ranges calculation in crs_replace_with_free_ranges() Anton Kuchin 2026-11-06 23:59 ` [PATCH 1/2] hw/acpi/aml-build: Ignore used ranges outside the requested window Anton Kuchin 2026-11-17 12:19 ` Michael S. Tsirkin 2026-11-17 12:28 ` Michael S. Tsirkin 2026-11-06 23:59 ` [PATCH 2/2] hw/acpi/aml-build: Avoid overflow when advancing the free_base cursor Anton Kuchin 2026-11-17 12:20 ` Michael S. Tsirkin 2026-11-17 12:21 ` [PATCH 0/2] hw/acpi/aml-build: Fix free ranges calculation in crs_replace_with_free_ranges() Michael S. Tsirkin
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.