Skip to contentSkip to conversation

resource: introduce union(), intersection() API

PATCH Superseded Andy Shevchenko 2 participants 8 messages Oct 30, 2020 – Nov 2, 2020

Subsystems (2)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover resource: introduce union(), intersection() API

Some users may want to use resource library to manage their own resources, besides existing users that open code union() and intersection() implementations.

Provide a generic API for wider use.

Changelog v3:
- rebased on top of v5.10-rc1
- dropped upstreamed dependencies
- added Rb tag to the last patch (Mika)

Andy Shevchenko (6):
resource: Simplify region_intersects() by reducing conditionals
resource: Group resource_overlaps() with other inline helpers
resource: Introduce resource_union() for overlapping resources
resource: Introduce resource_intersection() for overlapping resources PCI/ACPI: Replace open coded variant of resource_union()
ACPI: watchdog: Replace open coded variant of resource_union()

 drivers/acpi/acpi_watchdog.c |  6 +-----
 drivers/acpi/pci_root.c      |  4 +---
 include/linux/ioport.h       | 34 +++++++++++++++++++++++++++-------
 kernel/resource.c            | 10 +++++-----
 4 files changed, 34 insertions(+), 20 deletions(-)
Signature · 1 line
2.28.0
#1 lore raw

PATCH v3 1/6 resource: Simplify region_intersects() by reducing conditionals

Now we have for 'other' and 'type' variables

other type return

  0	  0	REGION_DISJOINT
  0	  x	REGION_INTERSECTS
  x	  0	REGION_DISJOINT
  x	  x	REGION_MIXED

Obviously it's easier to check 'type' for 0 first instead of currently checked 'other'.

---
 kernel/resource.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/resource.c +5 -5
index 3ae2f56cc79d..82df80417489 100644
@@ -557,13 +557,13 @@ int region_intersects(resource_size_t start, size_t size, unsigned long flags,
 	}
 	read_unlock(&resource_lock);
 
-	if (other == 0)
-		return type ? REGION_INTERSECTS : REGION_DISJOINT;
+	if (type == 0)
+		return REGION_DISJOINT;
 
-	if (type)
-		return REGION_MIXED;
+	if (other == 0)
+		return REGION_INTERSECTS;
 
-	return REGION_DISJOINT;
+	return REGION_MIXED;
 }
 EXPORT_SYMBOL_GPL(region_intersects);
 
Signature · 1 line
2.28.0
#2 lore raw

PATCH v3 2/6 resource: Group resource_overlaps() with other inline helpers

In reply to Andy Shevchenko

For better maintenance group resource_overlaps() with other inline helpers. While at it, drop extra parentheses.

---
 include/linux/ioport.h | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
include/linux/ioport.h +5 -6
index 5135d4b86cd6..df4581107536 100644
@@ -229,6 +229,11 @@ static inline bool resource_contains(struct resource *r1, struct resource *r2)
 	return r1->start <= r2->start && r1->end >= r2->end;
 }
 
+/* True if any part of r1 overlaps r2 */
+static inline bool resource_overlaps(struct resource *r1, struct resource *r2)
+{
+       return r1->start <= r2->end && r1->end >= r2->start;
+}
 
 /* Convenience shorthand with allocation */
 #define request_region(start,n,name)		__request_region(&ioport_resource, (start), (n), (name), 0)
@@ -296,12 +301,6 @@ extern int
 walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start, u64 end,
 		    void *arg, int (*func)(struct resource *, void *));
 
-/* True if any part of r1 overlaps r2 */
-static inline bool resource_overlaps(struct resource *r1, struct resource *r2)
-{
-       return (r1->start <= r2->end && r1->end >= r2->start);
-}
-
 struct resource *devm_request_free_mem_region(struct device *dev,
 		struct resource *base, unsigned long size);
 struct resource *request_free_mem_region(struct resource *base,
Signature · 1 line
2.28.0
#3 lore raw

PATCH v3 3/6 resource: Introduce resource_union() for overlapping resources

In reply to Andy Shevchenko

Some already present users may utilize resource_union() helper. Provide it for them and for wider use in the future.

Cc Mika Westerberg <[email protected]>
Cc Kuppuswamy Sathyanarayanan <[email protected]>
Cc Bjorn Helgaas <[email protected]>
---
 include/linux/ioport.h | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
include/linux/ioport.h +12 -1
index df4581107536..40320eb5bc0e 100644
@@ -10,9 +10,10 @@
 #define _LINUX_IOPORT_H
 
 #ifndef __ASSEMBLY__
+#include <linux/bits.h>
 #include <linux/compiler.h>
+#include <linux/minmax.h>
 #include <linux/types.h>
-#include <linux/bits.h>
 /*
  * Resources are tree-like, allowing
  * nesting etc..
@@ -235,6 +236,16 @@ static inline bool resource_overlaps(struct resource *r1, struct resource *r2)
        return r1->start <= r2->end && r1->end >= r2->start;
 }
 
+static inline bool
+resource_union(struct resource *r1, struct resource *r2, struct resource *r)
+{
+	if (!resource_overlaps(r1, r2))
+		return false;
+	r->start = min(r1->start, r2->start);
+	r->end = max(r1->end, r2->end);
+	return true;
+}
+
 /* Convenience shorthand with allocation */
 #define request_region(start,n,name)		__request_region(&ioport_resource, (start), (n), (name), 0)
 #define request_muxed_region(start,n,name)	__request_region(&ioport_resource, (start), (n), (name), IORESOURCE_MUXED)
Signature · 1 line
2.28.0
#4 lore raw

PATCH v3 4/6 resource: Introduce resource_intersection() for overlapping resources

In reply to Andy Shevchenko

There will be at least one user that can utilize new helper. Provide the helper for future user and for wider use.

---
 include/linux/ioport.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
include/linux/ioport.h +10 -0
index 40320eb5bc0e..ece1a8db309c 100644
@@ -236,6 +236,16 @@ static inline bool resource_overlaps(struct resource *r1, struct resource *r2)
        return r1->start <= r2->end && r1->end >= r2->start;
 }
 
+static inline bool
+resource_intersection(struct resource *r1, struct resource *r2, struct resource *r)
+{
+	if (!resource_overlaps(r1, r2))
+		return false;
+	r->start = max(r1->start, r2->start);
+	r->end = min(r1->end, r2->end);
+	return true;
+}
+
 static inline bool
 resource_union(struct resource *r1, struct resource *r2, struct resource *r)
 {
Signature · 1 line
2.28.0
#5 lore raw

PATCH v3 5/6 PCI/ACPI: Replace open coded variant of resource_union()

In reply to Andy Shevchenko

Since we have resource_union() helper, let's utilize it here.

---
 drivers/acpi/pci_root.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/acpi/pci_root.c +1 -3
index c12b5fb3e8fb..0bf072cef6cf 100644
@@ -722,9 +722,7 @@ static void acpi_pci_root_validate_resources(struct device *dev,
 			 * our resources no longer match the ACPI _CRS, but
 			 * the kernel resource tree doesn't allow overlaps.
 			 */
-			if (resource_overlaps(res1, res2)) {
-				res2->start = min(res1->start, res2->start);
-				res2->end = max(res1->end, res2->end);
+			if (resource_union(res1, res2, res2)) {
 				dev_info(dev, "host bridge window expanded to %pR; %pR ignored\n",
 					 res2, res1);
 				free = true;
Signature · 1 line
2.28.0
#6 lore raw

PATCH v3 6/6 ACPI: watchdog: Replace open coded variant of resource_union()

In reply to Andy Shevchenko

Since we have resource_union() helper, let's utilize it here.

---
 drivers/acpi/acpi_watchdog.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/acpi/acpi_watchdog.c +1 -5
index 5c1e9ea43123..ca28183f4d13 100644
@@ -151,11 +151,7 @@ void __init acpi_watchdog_init(void)
 		found = false;
 		resource_list_for_each_entry(rentry, &resource_list) {
 			if (rentry->res->flags == res.flags &&
-			    resource_overlaps(rentry->res, &res)) {
-				if (res.start < rentry->res->start)
-					rentry->res->start = res.start;
-				if (res.end > rentry->res->end)
-					rentry->res->end = res.end;
+			    resource_union(rentry->res, &res, rentry->res)) {
 				found = true;
 				break;
 			}
Signature · 1 line
2.28.0
maintainer Reviewed-by on the v3 series
#7 lore raw
In reply to Andy Shevchenko
Quoted 12 lines · Andy Shevchenko

Some users may want to use resource library to manage their own resources, besides existing users that open code union() and intersection() implementations.

Provide a generic API for wider use.

Changelog v3:
- rebased on top of v5.10-rc1
- dropped upstreamed dependencies
- added Rb tag to the last patch (Mika)

Quoted 5 lines

Andy Shevchenko (6):
resource: Simplify region_intersects() by reducing conditionals
resource: Group resource_overlaps() with other inline helpers
resource: Introduce resource_union() for overlapping resources
resource: Introduce resource_intersection() for overlapping resources

Feel free to add

to the four patches above (and I've already ACKed the remaining two).

If you want me to pick up the series, it would be good to resend it with CCs to linux-kernel and Greg KH.

Thanks!

Quoted 10 lines

PCI/ACPI: Replace open coded variant of resource_union()
ACPI: watchdog: Replace open coded variant of resource_union()

 drivers/acpi/acpi_watchdog.c |  6 +-----
 drivers/acpi/pci_root.c      |  4 +---
 include/linux/ioport.h       | 34 +++++++++++++++++++++++++++-------
 kernel/resource.c            | 10 +++++-----
 4 files changed, 34 insertions(+), 20 deletions(-)

--