[PATCH v2] ACPI: scan: Avoid registering platform devices with resource overlaps

Rafael J. Wysocki posted 1 patch 1 month, 3 weeks ago
drivers/acpi/acpi_platform.c |   49 +++++++++++++++++++++++++++++++++++++++++--
1 file changed, 47 insertions(+), 2 deletions(-)
[PATCH v2] ACPI: scan: Avoid registering platform devices with resource overlaps
Posted by Rafael J. Wysocki 1 month, 3 weeks ago
From: "Rafael J. Wysocki" <rafael.j.wysocki@intel.com>

If acpi_dev_get_resources() returns overlapping I/O or memory resources,
the subsequent registration of a platform device will fail with -EBUSY
due to a resource conflict.  This is reported to happen on Acer Aspire
ES1-572 [1].

Avoid that by adjusting resources returned by acpi_dev_get_resources()
to eliminate partial overlaps between them.

This has not been regarded as necessary before because putting
overlapping resources into the _CRS of one device is really pointless,
but now that the issue has been reported to actually happen in the
field, it needs to be done.

Fixes: ab06eb920401 ("ACPI: scan: Register platform devices for fixed event buttons")
Reported-by: Julien <julien82453@gmail.com>
Closes: https://lore.kernel.org/linux-integrity/CAJOGg3z6LJPDsdPNBxajgy8_wQxfhYBRxe4EiurZf3kPU5A5Bw@mail.gmail.com/ [1]
Cc: All applicable <stable@vger.kernel.org>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---

v1 -> v2:
   * Add the expanded resource instead of and not in addition to the other
     overlapping one (Sashiko)

---
 drivers/acpi/acpi_platform.c |   49 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 47 insertions(+), 2 deletions(-)

--- a/drivers/acpi/acpi_platform.c
+++ b/drivers/acpi/acpi_platform.c
@@ -71,6 +71,47 @@ static struct notifier_block acpi_platfo
 	.notifier_call = acpi_platform_device_remove_notify,
 };
 
+static unsigned int acpi_platform_adjust_resources(struct acpi_device *adev,
+						   struct resource *new_res,
+						   struct resource *resources,
+						   unsigned int count)
+{
+	unsigned int i;
+
+	if (!(new_res->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
+		return count;
+
+	for (i = 0; i < count; ) {
+		struct resource *res = &resources[i];
+
+		if (resource_type(new_res) != resource_type(res) ||
+		    !resource_overlaps(new_res, res)) {
+			i++;
+			continue;
+		}
+
+		dev_info(&adev->dev, "Adjusting %pR for %pR\n", new_res, res);
+		/*
+		 * Extend the new resource to include the one that has been processed
+		 * already.
+		 */
+		if (res->start < new_res->start)
+			new_res->start = res->start;
+
+		if (res->end > new_res->end)
+			new_res->end = res->end;
+
+		/*
+		 * Eliminate the previously processed resource that has
+		 * triggered the adjustment because it is not necessary any
+		 * more.
+		 */
+		memmove(res, res + 1, (--count - i) * sizeof(*res));
+	}
+
+	return count;
+}
+
 static void acpi_platform_fill_resource(struct acpi_device *adev,
 	const struct resource *src, struct resource *dest)
 {
@@ -151,10 +192,14 @@ struct platform_device *acpi_create_plat
 				return ERR_PTR(-ENOMEM);
 			}
 			count = 0;
-			list_for_each_entry(rentry, &resource_list, node)
+			list_for_each_entry(rentry, &resource_list, node) {
+				count = acpi_platform_adjust_resources(adev,
+								       rentry->res,
+								       resources,
+								       count);
 				acpi_platform_fill_resource(adev, rentry->res,
 							    &resources[count++]);
-
+			}
 			acpi_dev_free_resource_list(&resource_list);
 		}
 	}
Re: [PATCH v2] ACPI: scan: Avoid registering platform devices with resource overlaps
Posted by Andy Shevchenko 1 month, 3 weeks ago
On Thu, Aug 06, 2026 at 09:28:03PM +0200, Rafael J. Wysocki wrote:

> If acpi_dev_get_resources() returns overlapping I/O or memory resources,
> the subsequent registration of a platform device will fail with -EBUSY
> due to a resource conflict.  This is reported to happen on Acer Aspire
> ES1-572 [1].
> 
> Avoid that by adjusting resources returned by acpi_dev_get_resources()
> to eliminate partial overlaps between them.
> 
> This has not been regarded as necessary before because putting
> overlapping resources into the _CRS of one device is really pointless,
> but now that the issue has been reported to actually happen in the
> field, it needs to be done.

...

> +static unsigned int acpi_platform_adjust_resources(struct acpi_device *adev,
> +						   struct resource *new_res,
> +						   struct resource *resources,
> +						   unsigned int count)
> +{
> +	unsigned int i;
> +
> +	if (!(new_res->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
> +		return count;
> +
> +	for (i = 0; i < count; ) {
> +		struct resource *res = &resources[i];
> +
> +		if (resource_type(new_res) != resource_type(res) ||
> +		    !resource_overlaps(new_res, res)) {
> +			i++;
> +			continue;
> +		}
> +
> +		dev_info(&adev->dev, "Adjusting %pR for %pR\n", new_res, res);
> +		/*
> +		 * Extend the new resource to include the one that has been processed
> +		 * already.
> +		 */
> +		if (res->start < new_res->start)
> +			new_res->start = res->start;
> +
> +		if (res->end > new_res->end)
> +			new_res->end = res->end;

Can we use resource_union() instead?

> +		/*
> +		 * Eliminate the previously processed resource that has
> +		 * triggered the adjustment because it is not necessary any
> +		 * more.
> +		 */
> +		memmove(res, res + 1, (--count - i) * sizeof(*res));
> +	}
> +
> +	return count;
> +}

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH v2] ACPI: scan: Avoid registering platform devices with resource overlaps
Posted by Rafael J. Wysocki (Intel) 1 month, 3 weeks ago
On Thu, Aug 6, 2026 at 11:23 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
>
> On Thu, Aug 06, 2026 at 09:28:03PM +0200, Rafael J. Wysocki wrote:
>
> > If acpi_dev_get_resources() returns overlapping I/O or memory resources,
> > the subsequent registration of a platform device will fail with -EBUSY
> > due to a resource conflict.  This is reported to happen on Acer Aspire
> > ES1-572 [1].
> >
> > Avoid that by adjusting resources returned by acpi_dev_get_resources()
> > to eliminate partial overlaps between them.
> >
> > This has not been regarded as necessary before because putting
> > overlapping resources into the _CRS of one device is really pointless,
> > but now that the issue has been reported to actually happen in the
> > field, it needs to be done.
>
> ...
>
> > +static unsigned int acpi_platform_adjust_resources(struct acpi_device *adev,
> > +                                                struct resource *new_res,
> > +                                                struct resource *resources,
> > +                                                unsigned int count)
> > +{
> > +     unsigned int i;
> > +
> > +     if (!(new_res->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
> > +             return count;
> > +
> > +     for (i = 0; i < count; ) {
> > +             struct resource *res = &resources[i];
> > +
> > +             if (resource_type(new_res) != resource_type(res) ||
> > +                 !resource_overlaps(new_res, res)) {
> > +                     i++;
> > +                     continue;
> > +             }
> > +
> > +             dev_info(&adev->dev, "Adjusting %pR for %pR\n", new_res, res);
> > +             /*
> > +              * Extend the new resource to include the one that has been processed
> > +              * already.
> > +              */
> > +             if (res->start < new_res->start)
> > +                     new_res->start = res->start;
> > +
> > +             if (res->end > new_res->end)
> > +                     new_res->end = res->end;
>
> Can we use resource_union() instead?

Not really.

> > +             /*
> > +              * Eliminate the previously processed resource that has
> > +              * triggered the adjustment because it is not necessary any
> > +              * more.
> > +              */
> > +             memmove(res, res + 1, (--count - i) * sizeof(*res));
> > +     }
> > +
> > +     return count;
> > +}
>
> --
Re: [PATCH v2] ACPI: scan: Avoid registering platform devices with resource overlaps
Posted by Rafael J. Wysocki (Intel) 1 month, 3 weeks ago
On Fri, Aug 7, 2026 at 11:55 AM Rafael J. Wysocki (Intel)
<rafael@kernel.org> wrote:
>
> On Thu, Aug 6, 2026 at 11:23 PM Andy Shevchenko
> <andriy.shevchenko@linux.intel.com> wrote:
> >
> > On Thu, Aug 06, 2026 at 09:28:03PM +0200, Rafael J. Wysocki wrote:
> >
> > > If acpi_dev_get_resources() returns overlapping I/O or memory resources,
> > > the subsequent registration of a platform device will fail with -EBUSY
> > > due to a resource conflict.  This is reported to happen on Acer Aspire
> > > ES1-572 [1].
> > >
> > > Avoid that by adjusting resources returned by acpi_dev_get_resources()
> > > to eliminate partial overlaps between them.
> > >
> > > This has not been regarded as necessary before because putting
> > > overlapping resources into the _CRS of one device is really pointless,
> > > but now that the issue has been reported to actually happen in the
> > > field, it needs to be done.
> >
> > ...
> >
> > > +static unsigned int acpi_platform_adjust_resources(struct acpi_device *adev,
> > > +                                                struct resource *new_res,
> > > +                                                struct resource *resources,
> > > +                                                unsigned int count)
> > > +{
> > > +     unsigned int i;
> > > +
> > > +     if (!(new_res->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
> > > +             return count;
> > > +
> > > +     for (i = 0; i < count; ) {
> > > +             struct resource *res = &resources[i];
> > > +
> > > +             if (resource_type(new_res) != resource_type(res) ||
> > > +                 !resource_overlaps(new_res, res)) {
> > > +                     i++;
> > > +                     continue;
> > > +             }
> > > +
> > > +             dev_info(&adev->dev, "Adjusting %pR for %pR\n", new_res, res);
> > > +             /*
> > > +              * Extend the new resource to include the one that has been processed
> > > +              * already.
> > > +              */
> > > +             if (res->start < new_res->start)
> > > +                     new_res->start = res->start;
> > > +
> > > +             if (res->end > new_res->end)
> > > +                     new_res->end = res->end;
> >
> > Can we use resource_union() instead?
>
> Not really.

Or maybe.

Let me see if this works.