Skip to contentSkip to conversation

kernel, resource: use resource_overlaps() to simplify region_intersects()

PATCH Superseded Wei Yang 3 participants 5 messages Jan 21, 2019 – Mar 5, 2019

lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH 1/1 kernel, resource: use resource_overlaps() to simplify region_intersects()

The three checks in region_intersects() is to see whether two resources overlap. This means it could be simplified with one resource_overlaps().

Also fix two typo in related function.

---
 kernel/iomem.c    |  4 ++--
 kernel/resource.c | 11 +++++------
 2 files changed, 7 insertions(+), 8 deletions(-)
kernel/iomem.c +2 -2
index f7525e14ebc6..93c264444510 100644
@@ -55,7 +55,7 @@ static void *try_ram_remap(resource_size_t offset, size_t size,
  *
  * MEMREMAP_WB - matches the default mapping for System RAM on
  * the architecture.  This is usually a read-allocate write-back cache.
- * Morever, if MEMREMAP_WB is specified and the requested remap region is RAM
+ * Moreover, if MEMREMAP_WB is specified and the requested remap region is RAM
  * memremap() will bypass establishing a new mapping and instead return
  * a pointer into the direct map.
  *
@@ -86,7 +86,7 @@ void *memremap(resource_size_t offset, size_t size, unsigned long flags)
 	/* Try all mapping types requested until one returns non-NULL */
 	if (flags & MEMREMAP_WB) {
 		/*
-		 * MEMREMAP_WB is special in that it can be satisifed
+		 * MEMREMAP_WB is special in that it can be satisfied
 		 * from the direct map.  Some archs depend on the
 		 * capability of memremap() to autodetect cases where
 		 * the requested range is potentially in System RAM.
kernel/resource.c +5 -6
index b0fbf685c77a..34dfb94305bb 100644
@@ -521,21 +521,20 @@ EXPORT_SYMBOL_GPL(page_is_ram);
 int region_intersects(resource_size_t start, size_t size, unsigned long flags,
 		      unsigned long desc)
 {
-	resource_size_t end = start + size - 1;
+	struct resource res;
 	int type = 0; int other = 0;
 	struct resource *p;
 
+	res.start = start;
+	res.end = start + size - 1;
+
 	read_lock(&resource_lock);
 	for (p = iomem_resource.child; p ; p = p->sibling) {
 		bool is_type = (((p->flags & flags) == flags) &&
 				((desc == IORES_DESC_NONE) ||
 				 (desc == p->desc)));
 
-		if (start >= p->start && start <= p->end)
-			is_type ? type++ : other++;
-		if (end >= p->start && end <= p->end)
-			is_type ? type++ : other++;
-		if (p->start >= start && p->end <= end)
+		if (resource_overlaps(p, &res))
 			is_type ? type++ : other++;
 	}
 	read_unlock(&resource_lock);
Signature · 1 line
2.19.1
author
#1 lore raw
Quoted 6 lines · Wei Yang

The three checks in region_intersects() is to see whether two resources overlap. This means it could be simplified with one resource_overlaps().

Also fix two typo in related function.

Hello~

Would someone like to take a look?

Quoted 60 lines

---
kernel/iomem.c | 4 ++--
kernel/resource.c | 11 +++++------

2 files changed, 7 insertions(+), 8 deletions(-)
kernel/iomem.c +2 -2
index f7525e14ebc6..93c264444510 100644
@@ -55,7 +55,7 @@ static void *try_ram_remap(resource_size_t offset, size_t size,
 *
 * MEMREMAP_WB - matches the default mapping for System RAM on
 * the architecture.  This is usually a read-allocate write-back cache.
- * Morever, if MEMREMAP_WB is specified and the requested remap region is RAM
+ * Moreover, if MEMREMAP_WB is specified and the requested remap region is RAM
 * memremap() will bypass establishing a new mapping and instead return
 * a pointer into the direct map.
 *
@@ -86,7 +86,7 @@ void *memremap(resource_size_t offset, size_t size, unsigned long flags)
 	/* Try all mapping types requested until one returns non-NULL */
 	if (flags & MEMREMAP_WB) {
 		/*
-		 * MEMREMAP_WB is special in that it can be satisifed
+		 * MEMREMAP_WB is special in that it can be satisfied
 		 * from the direct map.  Some archs depend on the
 		 * capability of memremap() to autodetect cases where
 		 * the requested range is potentially in System RAM.
kernel/resource.c +5 -6
index b0fbf685c77a..34dfb94305bb 100644
@@ -521,21 +521,20 @@ EXPORT_SYMBOL_GPL(page_is_ram);
 int region_intersects(resource_size_t start, size_t size, unsigned long flags,
 		      unsigned long desc)
 {
-	resource_size_t end = start + size - 1;
+	struct resource res;
 	int type = 0; int other = 0;
 	struct resource *p;
 
+	res.start = start;
+	res.end = start + size - 1;
+
 	read_lock(&resource_lock);
 	for (p = iomem_resource.child; p ; p = p->sibling) {
 		bool is_type = (((p->flags & flags) == flags) &&
 				((desc == IORES_DESC_NONE) ||
 				 (desc == p->desc)));
 
-		if (start >= p->start && start <= p->end)
-			is_type ? type++ : other++;
-		if (end >= p->start && end <= p->end)
-			is_type ? type++ : other++;
-		if (p->start >= start && p->end <= end)
+		if (resource_overlaps(p, &res))
 			is_type ? type++ : other++;
 	}
 	read_unlock(&resource_lock);
Signature · 1 line
2.19.1
Signature · 2 lines
Wei Yang
Help you, Help me
Reviewed-by
#2 lore raw
Quoted 11 lines · Wei Yang
Quoted 6 lines · Wei Yang

The three checks in region_intersects() is to see whether two resources overlap. This means it could be simplified with one resource_overlaps().

Also fix two typo in related function.

Hello~

Would someone like to take a look?

No semantic change and it looks good to me.

You may use scripts/get_maintainer.pl to cc more maintainers.

Quoted 62 lines
Quoted 61 lines

---
kernel/iomem.c | 4 ++--
kernel/resource.c | 11 +++++------

2 files changed, 7 insertions(+), 8 deletions(-)
kernel/iomem.c +2 -2
index f7525e14ebc6..93c264444510 100644
@@ -55,7 +55,7 @@ static void *try_ram_remap(resource_size_t offset, size_t size,
  *
  * MEMREMAP_WB - matches the default mapping for System RAM on
  * the architecture.  This is usually a read-allocate write-back cache.
- * Morever, if MEMREMAP_WB is specified and the requested remap region is RAM
+ * Moreover, if MEMREMAP_WB is specified and the requested remap region is RAM
  * memremap() will bypass establishing a new mapping and instead return
  * a pointer into the direct map.
  *
@@ -86,7 +86,7 @@ void *memremap(resource_size_t offset, size_t size, unsigned long flags)
 	/* Try all mapping types requested until one returns non-NULL */
 	if (flags & MEMREMAP_WB) {
 		/*
-		 * MEMREMAP_WB is special in that it can be satisifed
+		 * MEMREMAP_WB is special in that it can be satisfied
 		 * from the direct map.  Some archs depend on the
 		 * capability of memremap() to autodetect cases where
 		 * the requested range is potentially in System RAM.
kernel/resource.c +5 -6
index b0fbf685c77a..34dfb94305bb 100644
@@ -521,21 +521,20 @@ EXPORT_SYMBOL_GPL(page_is_ram);
 int region_intersects(resource_size_t start, size_t size, unsigned long flags,
 		      unsigned long desc)
 {
-	resource_size_t end = start + size - 1;
+	struct resource res;
 	int type = 0; int other = 0;
 	struct resource *p;
 
+	res.start = start;
+	res.end = start + size - 1;
+
 	read_lock(&resource_lock);
 	for (p = iomem_resource.child; p ; p = p->sibling) {
 		bool is_type = (((p->flags & flags) == flags) &&
 				((desc == IORES_DESC_NONE) ||
 				 (desc == p->desc)));
 
-		if (start >= p->start && start <= p->end)
-			is_type ? type++ : other++;
-		if (end >= p->start && end <= p->end)
-			is_type ? type++ : other++;
-		if (p->start >= start && p->end <= end)
+		if (resource_overlaps(p, &res))
 			is_type ? type++ : other++;
 	}
 	read_unlock(&resource_lock);
Signature · 1 line
2.19.1
author
#3 lore raw
Quoted 11 lines · Like Xu
Quoted 10 lines · Wei Yang
Quoted 6 lines · Wei Yang

The three checks in region_intersects() is to see whether two resources overlap. This means it could be simplified with one resource_overlaps().

Also fix two typo in related function.

Hello~

Quoted 8 lines
Quoted 1 line

Would someone like to take a look?

No semantic change and it looks good to me.

You may use scripts/get_maintainer.pl to cc more maintainers.

Yep, thanks for your suggestion :-)

Quoted 62 lines
Quoted 62 lines
Quoted 61 lines

---
kernel/iomem.c | 4 ++--
kernel/resource.c | 11 +++++------

2 files changed, 7 insertions(+), 8 deletions(-)
kernel/iomem.c +2 -2
index f7525e14ebc6..93c264444510 100644
@@ -55,7 +55,7 @@ static void *try_ram_remap(resource_size_t offset, size_t size,
  *
  * MEMREMAP_WB - matches the default mapping for System RAM on
  * the architecture.  This is usually a read-allocate write-back cache.
- * Morever, if MEMREMAP_WB is specified and the requested remap region is RAM
+ * Moreover, if MEMREMAP_WB is specified and the requested remap region is RAM
  * memremap() will bypass establishing a new mapping and instead return
  * a pointer into the direct map.
  *
@@ -86,7 +86,7 @@ void *memremap(resource_size_t offset, size_t size, unsigned long flags)
 	/* Try all mapping types requested until one returns non-NULL */
 	if (flags & MEMREMAP_WB) {
 		/*
-		 * MEMREMAP_WB is special in that it can be satisifed
+		 * MEMREMAP_WB is special in that it can be satisfied
 		 * from the direct map.  Some archs depend on the
 		 * capability of memremap() to autodetect cases where
 		 * the requested range is potentially in System RAM.
kernel/resource.c +5 -6
index b0fbf685c77a..34dfb94305bb 100644
@@ -521,21 +521,20 @@ EXPORT_SYMBOL_GPL(page_is_ram);
 int region_intersects(resource_size_t start, size_t size, unsigned long flags,
 		      unsigned long desc)
 {
-	resource_size_t end = start + size - 1;
+	struct resource res;
 	int type = 0; int other = 0;
 	struct resource *p;
 
+	res.start = start;
+	res.end = start + size - 1;
+
 	read_lock(&resource_lock);
 	for (p = iomem_resource.child; p ; p = p->sibling) {
 		bool is_type = (((p->flags & flags) == flags) &&
 				((desc == IORES_DESC_NONE) ||
 				 (desc == p->desc)));
 
-		if (start >= p->start && start <= p->end)
-			is_type ? type++ : other++;
-		if (end >= p->start && end <= p->end)
-			is_type ? type++ : other++;
-		if (p->start >= start && p->end <= end)
+		if (resource_overlaps(p, &res))
 			is_type ? type++ : other++;
 	}
 	read_unlock(&resource_lock);
Signature · 1 line
2.19.1
Signature · 2 lines
Wei Yang
Help you, Help me
Reviewed-by on v1 1/1
#4 lore raw
In reply to Wei Yang

Looks good to me.

Quoted 66 lines · Wei Yang

The three checks in region_intersects() is to see whether two resources overlap. This means it could be simplified with one resource_overlaps().

Also fix two typo in related function.

---
 kernel/iomem.c    |  4 ++--
 kernel/resource.c | 11 +++++------
 2 files changed, 7 insertions(+), 8 deletions(-)
kernel/iomem.c +2 -2
index f7525e14ebc6..93c264444510 100644
@@ -55,7 +55,7 @@ static void *try_ram_remap(resource_size_t offset, size_t size,
  *
  * MEMREMAP_WB - matches the default mapping for System RAM on
  * the architecture.  This is usually a read-allocate write-back cache.
- * Morever, if MEMREMAP_WB is specified and the requested remap region is RAM
+ * Moreover, if MEMREMAP_WB is specified and the requested remap region is RAM
  * memremap() will bypass establishing a new mapping and instead return
  * a pointer into the direct map.
  *
@@ -86,7 +86,7 @@ void *memremap(resource_size_t offset, size_t size, unsigned long flags)
 	/* Try all mapping types requested until one returns non-NULL */
 	if (flags & MEMREMAP_WB) {
 		/*
-		 * MEMREMAP_WB is special in that it can be satisifed
+		 * MEMREMAP_WB is special in that it can be satisfied
 		 * from the direct map.  Some archs depend on the
 		 * capability of memremap() to autodetect cases where
 		 * the requested range is potentially in System RAM.
kernel/resource.c +5 -6
index b0fbf685c77a..34dfb94305bb 100644
@@ -521,21 +521,20 @@ EXPORT_SYMBOL_GPL(page_is_ram);
 int region_intersects(resource_size_t start, size_t size, unsigned long flags,
 		      unsigned long desc)
 {
-	resource_size_t end = start + size - 1;
+	struct resource res;
 	int type = 0; int other = 0;
 	struct resource *p;
 
+	res.start = start;
+	res.end = start + size - 1;
+
 	read_lock(&resource_lock);
 	for (p = iomem_resource.child; p ; p = p->sibling) {
 		bool is_type = (((p->flags & flags) == flags) &&
 				((desc == IORES_DESC_NONE) ||
 				 (desc == p->desc)));
 
-		if (start >= p->start && start <= p->end)
-			is_type ? type++ : other++;
-		if (end >= p->start && end <= p->end)
-			is_type ? type++ : other++;
-		if (p->start >= start && p->end <= end)
+		if (resource_overlaps(p, &res))
 			is_type ? type++ : other++;
 	}
 	read_unlock(&resource_lock);
Signature · 1 line
2.19.1