Skip to contentSkip to conversation

resource: introduce union(), intersection() API

PATCH Superseded Andy Shevchenko 1 participant 9 messages Nov 3, 2020

Subsystems (3)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v5 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 v5:
- added test cases (Greg)

Changelog v4:
- added Rb tag (Rafael)
- Cc'ed to LKML and Greg (Rafael)

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

Cc Kuppuswamy Sathyanarayanan <[email protected]>
Cc Bjorn Helgaas <[email protected]>

Andy Shevchenko (7):
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
resource: Add test cases for new resource API
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/Makefile              |   1 +
 kernel/resource.c            |  10 +--
 kernel/resource_kunit.c      | 150 +++++++++++++++++++++++++++++++++++
 lib/Kconfig.debug            |  11 +++
 7 files changed, 196 insertions(+), 20 deletions(-)
 create mode 100644 kernel/resource_kunit.c
Signature · 1 line
2.28.0
#1 lore raw

PATCH v5 1/7 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 v5 2/7 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 v5 3/7 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 v5 4/7 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 v5 5/7 resource: Add test cases for new resource API

In reply to Andy Shevchenko

Add test cases for newly added resource APIs.

---
 kernel/Makefile         |   1 +
 kernel/resource_kunit.c | 150 ++++++++++++++++++++++++++++++++++++++++
 lib/Kconfig.debug       |  11 +++
 3 files changed, 162 insertions(+)
 create mode 100644 kernel/resource_kunit.c
kernel/Makefile +1 -0
index af601b9bda0e..aac15aeb9d69 100644
@@ -123,6 +123,7 @@ obj-$(CONFIG_HAS_IOMEM) += iomem.o
 obj-$(CONFIG_RSEQ) += rseq.o
 obj-$(CONFIG_WATCH_QUEUE) += watch_queue.o
 
+obj-$(CONFIG_RESOURCE_KUNIT_TEST) += resource_kunit.o
 obj-$(CONFIG_SYSCTL_KUNIT_TEST) += sysctl-test.o
 
 CFLAGS_stackleak.o += $(DISABLE_STACKLEAK_PLUGIN)
kernel/resource_kunit.c new file +150 -0

150 lines not on this page · raw message

lib/Kconfig.debug +11 -0
index 8596989423bf..663e7238a56e 100644
@@ -2232,6 +2232,17 @@ config BITFIELD_KUNIT
 
 	  If unsure, say N.
 
+config RESOURCE_KUNIT_TEST
+	tristate "KUnit test for resource API"
+	depends on KUNIT
+	help
+	  This builds the resource API unit test.
+	  Tests the logic of API provided by resource.c and ioport.h.
+	  For more information on KUnit and unit tests in general please refer
+	  to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+	  If unsure, say N.
+
 config SYSCTL_KUNIT_TEST
 	tristate "KUnit test for sysctl" if !KUNIT_ALL_TESTS
 	depends on KUNIT
Signature · 1 line
2.28.0
#6 lore raw

PATCH v5 6/7 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
#7 lore raw

PATCH v5 7/7 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
#8 lore raw
In reply to Andy Shevchenko
Quoted 8 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 v5:
- added test cases (Greg)

Sorry, just realized I forgot to add collected tags. Fixed in v6. Sorry for a bit of spam.

Signature · 2 lines
With Best Regards,
Andy Shevchenko