public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1 0/2] resource: A couple of cleanups
@ 2024-09-25 15:43 Andy Shevchenko
  2024-09-25 15:43 ` [PATCH v1 1/2] resource: Replace open coded resource_intersection() Andy Shevchenko
  2024-09-25 15:43 ` [PATCH v1 2/2] resource: Introduce is_type_match() helper and use it Andy Shevchenko
  0 siblings, 2 replies; 3+ messages in thread
From: Andy Shevchenko @ 2024-09-25 15:43 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andy Shevchenko, Rasmus Villemoes, Andrew Morton, Andy Shevchenko

A couple of ad-hoc cleanups since there was a recent development of
the code in question. No functional changes intended.

Andy Shevchenko (2):
  resource: Replace open coded resource_intersection()
  resource: Introduce is_type_match() helper and use it

 kernel/resource.c | 38 ++++++++++++++++----------------------
 1 file changed, 16 insertions(+), 22 deletions(-)

-- 
2.43.0.rc1.1336.g36b5255a03ac


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v1 1/2] resource: Replace open coded resource_intersection()
  2024-09-25 15:43 [PATCH v1 0/2] resource: A couple of cleanups Andy Shevchenko
@ 2024-09-25 15:43 ` Andy Shevchenko
  2024-09-25 15:43 ` [PATCH v1 2/2] resource: Introduce is_type_match() helper and use it Andy Shevchenko
  1 sibling, 0 replies; 3+ messages in thread
From: Andy Shevchenko @ 2024-09-25 15:43 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andy Shevchenko, Rasmus Villemoes, Andrew Morton, Andy Shevchenko

The __region_intersects() uses open coded resource_intersection().
Replace it with existing API which also make more clear what we
are checking.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 kernel/resource.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/kernel/resource.c b/kernel/resource.c
index b730bd28b422..6880c0e283dd 100644
--- a/kernel/resource.c
+++ b/kernel/resource.c
@@ -539,17 +539,16 @@ static int __region_intersects(struct resource *parent, resource_size_t start,
 			       size_t size, unsigned long flags,
 			       unsigned long desc)
 {
-	resource_size_t ostart, oend;
 	int type = 0; int other = 0;
 	struct resource *p, *dp;
+	struct resource res, o;
 	bool is_type, covered;
-	struct resource res;
 
 	res.start = start;
 	res.end = start + size - 1;
 
 	for (p = parent->child; p ; p = p->sibling) {
-		if (!resource_overlaps(p, &res))
+		if (!resource_intersection(p, &res, &o))
 			continue;
 		is_type = (p->flags & flags) == flags &&
 			(desc == IORES_DESC_NONE || desc == p->desc);
@@ -570,8 +569,6 @@ static int __region_intersects(struct resource *parent, resource_size_t start,
 		 * |-- "System RAM" --||-- "CXL Window 0a" --|
 		 */
 		covered = false;
-		ostart = max(res.start, p->start);
-		oend = min(res.end, p->end);
 		for_each_resource(p, dp, false) {
 			if (!resource_overlaps(dp, &res))
 				continue;
@@ -580,17 +577,17 @@ static int __region_intersects(struct resource *parent, resource_size_t start,
 			if (is_type) {
 				type++;
 				/*
-				 * Range from 'ostart' to 'dp->start'
+				 * Range from 'o.start' to 'dp->start'
 				 * isn't covered by matched resource.
 				 */
-				if (dp->start > ostart)
+				if (dp->start > o.start)
 					break;
-				if (dp->end >= oend) {
+				if (dp->end >= o.end) {
 					covered = true;
 					break;
 				}
 				/* Remove covered range */
-				ostart = max(ostart, dp->end + 1);
+				o.start = max(o.start, dp->end + 1);
 			}
 		}
 		if (!covered)
-- 
2.43.0.rc1.1336.g36b5255a03ac


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH v1 2/2] resource: Introduce is_type_match() helper and use it
  2024-09-25 15:43 [PATCH v1 0/2] resource: A couple of cleanups Andy Shevchenko
  2024-09-25 15:43 ` [PATCH v1 1/2] resource: Replace open coded resource_intersection() Andy Shevchenko
@ 2024-09-25 15:43 ` Andy Shevchenko
  1 sibling, 0 replies; 3+ messages in thread
From: Andy Shevchenko @ 2024-09-25 15:43 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andy Shevchenko, Rasmus Villemoes, Andrew Morton, Andy Shevchenko

There are already a couple of places where we may replace
a few lines of code by calling a helper, which increases
readability while deduplicating the code.

Introduce is_type_match() helper and use it.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 kernel/resource.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)

diff --git a/kernel/resource.c b/kernel/resource.c
index 6880c0e283dd..2d266b5ff881 100644
--- a/kernel/resource.c
+++ b/kernel/resource.c
@@ -297,6 +297,11 @@ int release_resource(struct resource *old)
 
 EXPORT_SYMBOL(release_resource);
 
+static bool is_type_match(struct resource *p, unsigned long flags, unsigned long desc)
+{
+	return (p->flags & flags) == flags && (desc == IORES_DESC_NONE || desc == p->desc);
+}
+
 /**
  * find_next_iomem_res - Finds the lowest iomem resource that covers part of
  *			 [@start..@end].
@@ -339,13 +344,9 @@ static int find_next_iomem_res(resource_size_t start, resource_size_t end,
 		if (p->end < start)
 			continue;
 
-		if ((p->flags & flags) != flags)
-			continue;
-		if ((desc != IORES_DESC_NONE) && (desc != p->desc))
-			continue;
-
 		/* Found a match, break */
-		break;
+		if (is_type_match(p, flags, desc))
+			break;
 	}
 
 	if (p) {
@@ -542,7 +543,7 @@ static int __region_intersects(struct resource *parent, resource_size_t start,
 	int type = 0; int other = 0;
 	struct resource *p, *dp;
 	struct resource res, o;
-	bool is_type, covered;
+	bool covered;
 
 	res.start = start;
 	res.end = start + size - 1;
@@ -550,9 +551,7 @@ static int __region_intersects(struct resource *parent, resource_size_t start,
 	for (p = parent->child; p ; p = p->sibling) {
 		if (!resource_intersection(p, &res, &o))
 			continue;
-		is_type = (p->flags & flags) == flags &&
-			(desc == IORES_DESC_NONE || desc == p->desc);
-		if (is_type) {
+		if (is_type_match(p, flags, desc)) {
 			type++;
 			continue;
 		}
@@ -572,9 +571,7 @@ static int __region_intersects(struct resource *parent, resource_size_t start,
 		for_each_resource(p, dp, false) {
 			if (!resource_overlaps(dp, &res))
 				continue;
-			is_type = (dp->flags & flags) == flags &&
-				(desc == IORES_DESC_NONE || desc == dp->desc);
-			if (is_type) {
+			if (is_type_match(dp, flags, desc)) {
 				type++;
 				/*
 				 * Range from 'o.start' to 'dp->start'
-- 
2.43.0.rc1.1336.g36b5255a03ac


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-09-25 15:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-25 15:43 [PATCH v1 0/2] resource: A couple of cleanups Andy Shevchenko
2024-09-25 15:43 ` [PATCH v1 1/2] resource: Replace open coded resource_intersection() Andy Shevchenko
2024-09-25 15:43 ` [PATCH v1 2/2] resource: Introduce is_type_match() helper and use it Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox