Skip to contentSkip to conversation

avoiding the same resource to be inserted

PATCH In review Huang Shijie 3 participants 7 messages Aug 16, 2010 – Aug 25, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 avoiding the same resource to be inserted

If the same resource is inserted to the resource tree
(maybe not on purpose), a dead loop will be created. In this situation,
The kernel does not report any warning or error :(

The command below will show a endless print.
#cat /proc/iomem

So, adding the check for the same resource is needed for the stability
and reliability of the kernel.

---
 kernel/resource.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
kernel/resource.c +1 -1
index 7b36976..60daab4 100644
@@ -451,7 +451,7 @@ static struct resource * __insert_resource(struct resource *parent, struct resou
 		if (!first)
 			return first;
 
-		if (first == parent)
+		if (first == parent || first == new)
 			return first;
 
 		if ((first->start > new->start) || (first->end < new->end))
Signature · 1 line
1.6.6.1
#1 lore raw
Quoted 6 lines · On Mon, 16 Aug 2010 23:55:38 +0800 Huang Shijie <[email protected]> wrote:

If the same resource is inserted to the resource tree
(maybe not on purpose), a dead loop will be created. In this situation,
The kernel does not report any warning or error :(

The command below will show a endless print.
#cat /proc/iomem

OK, we shouldn't do that.

Quoted 13 lines

So, adding the check for the same resource is needed for the stability
and reliability of the kernel.

---
 kernel/resource.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
kernel/resource.c +0 -0
index 7b36976..60daab4 100644

kernel/resource.c static struct resource * __insert_resource(struct resource *parent, struct resou · line 455

Quoted 6 lines
kernel/resource.c +1 -1
@@ -451,7 +451,7 @@ static struct resource * __insert_resource(struct resource *parent, struct resou
 		if (!first)
 			return first;
 
-		if (first == parent)
+		if (first == parent || first == new)
 			return first;

However, inserting the same thing twice _is_ a bug, and we shouldn't silently accept it like this. We should tell the programmer!

But we can recover from the situation so let's not kill the box. How does this look?

kernel/resource.c renamed from kernel/resource.c~kernel-resourcec-handle-reinsertion-of-an-already-inserted-resource +2 -0
@@ -453,6 +453,8 @@ static struct resource * __insert_resour
 
 		if (first == parent)
 			return first;
+		if (WARN_ON(first == new))	/* duplicated insertion */
+			return first;
 
 		if ((first->start > new->start) || (first->end < new->end))
 			break;

_

#2 lore raw
Quoted 28 lines · Andrew Morton
Quoted 6 lines · On Mon, 16 Aug 2010 23:55:38 +0800 Huang Shijie <[email protected]> wrote:

  If the same resource is inserted to the resource tree (maybe not on purpose), a dead loop will be created. In this situation, The kernel does not report any warning or error       :(

  The command below will show a endless print.
  #cat /proc/iomem

OK, we shouldn't do that.

Quoted 16 lines

  So, adding the check for the same resource is needed for the stability and reliability of the kernel.

---
 kernel/resource.c |    2 +-

 1 files changed, 1 insertions(+), 1 deletions(-)
kernel/resource.c +0 -0
index 7b36976..60daab4 100644
@@ -451,7 +451,7 @@ static struct resource * __insert_resource(struct resource *parent, struct resou
               if (!first)
                       return first;
 
Quoted 6 lines

kernel/resource.c static struct resource * __insert_resource(struct resource *parent, struct resou · line 455

Quoted 3 lines
kernel/resource.c +1 -1
-             if (first == parent)
+             if (first == parent || first == new)
                       return first;

However, inserting the same thing twice _is_ a bug, and we shouldn't silently accept it like this.  We should tell the programmer!

Indeed, this is a bug caused by the driver programmer.I had spent nearly two day to find it in my colleague's code.

kernel/resource.c static struct resource * __insert_resour · line 458

Quoted 13 lines

But we can recover from the situation so let's not kill the box.  How does this look?

kernel/resource.c renamed from kernel/resource.c~kernel-resourcec-handle-reinsertion-of-an-already-inserted-resource +2 -0
@@ -453,6 +453,8 @@ static struct resource * __insert_resour
 
                if (first == parent)
                        return first;
+               if (WARN_ON(first == new))      /* duplicated insertion */
+                       return first;
 

Yes. This one is better.
Many drivers do not check the returned value of insert_resouce().

Quoted 5 lines
kernel/resource.c renamed from kernel/resource.c~kernel-resourcec-handle-reinsertion-of-an-already-inserted-resource +0 -0
                if ((first->start > new->start) || (first->end < new->end))
                        break;

_

#3 lore raw
Quoted 37 lines · Huang Shijie
Quoted 28 lines · Andrew Morton
Quoted 6 lines · On Mon, 16 Aug 2010 23:55:38 +0800 Huang Shijie <[email protected]> wrote:

If the same resource is inserted to the resource tree
(maybe not on purpose), a dead loop will be created. In this situation,
The kernel does not report any warning or error :(

The command below will show a endless print.
#cat /proc/iomem

OK, we shouldn't do that.

Quoted 16 lines

So, adding the check for the same resource is needed for the stability
and reliability of the kernel.

---
 kernel/resource.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
kernel/resource.c +0 -0
index 7b36976..60daab4 100644
@@ -451,7 +451,7 @@ static struct resource * __insert_resource(struct resource *parent, struct resou
              if (!first)
                      return first;
 
Quoted 6 lines

kernel/resource.c static struct resource * __insert_resource(struct resource *parent, struct resou · line 455

Quoted 3 lines
kernel/resource.c +1 -1
-             if (first == parent)
+             if (first == parent || first == new)
                      return first;

However, inserting the same thing twice _is_ a bug, and we shouldn't silently accept it like this. We should tell the programmer!

Quoted 2 lines

Indeed, this is a bug caused by the driver programmer.I had spent nearly two day to find it in my colleague's code.

insert_resource() is a hack to deal with the fact that we don't discover resources in the logical order, so sometimes we have to go back and add a resource after we've already added "children" of the new resource.

In my opinion, the ugliness of using insert_resource() should be confined to core architecture code, and drivers shouldn't use it at all.

That said, I think it's a good idea to add the WARN_ON().

In fact, I wonder if we should also "WARN_ON(first == parent)".

Bjorn

#4 lore raw
In reply to Andrew Morton
Quoted 41 lines · Andrew Morton
Quoted 6 lines · On Mon, 16 Aug 2010 23:55:38 +0800 Huang Shijie <[email protected]> wrote:

  If the same resource is inserted to the resource tree (maybe not on purpose), a dead loop will be created. In this situation, The kernel does not report any warning or error       :(

  The command below will show a endless print.
  #cat /proc/iomem

OK, we shouldn't do that.

Quoted 13 lines

  So, adding the check for the same resource is needed for the stability and reliability of the kernel.

---
 kernel/resource.c |    2 +-

 1 files changed, 1 insertions(+), 1 deletions(-)
kernel/resource.c +0 -0
index 7b36976..60daab4 100644

kernel/resource.c static struct resource * __insert_resource(struct resource *parent, struct resou · line 455

Quoted 6 lines
kernel/resource.c +1 -1
@@ -451,7 +451,7 @@ static struct resource * __insert_resource(struct resource *parent, struct resou
               if (!first)
                       return first;
 
-             if (first == parent)
+             if (first == parent || first == new)
                       return first;

However, inserting the same thing twice _is_ a bug, and we shouldn't silently accept it like this.  We should tell the programmer!

But we can recover from the situation so let's not kill the box.  How does this look?

kernel/resource.c static struct resource * __insert_resour · line 458

Quoted 6 lines
kernel/resource.c renamed from kernel/resource.c~kernel-resourcec-handle-reinsertion-of-an-already-inserted-resource +2 -0
@@ -453,6 +453,8 @@ static struct resource * __insert_resour
 
                if (first == parent)
                        return first;
+               if (WARN_ON(first == new))      /* duplicated insertion */
+                       return first;
 

I think WARN_ON() is not power enough. If the CONFIG_BUG is not defined, the warning message may not be printed. So I think using a directly printk() here is better.

What about this ?

kernel/resource.c +0 -0
@@ -453,6 +453,11 @@ static struct resource * __insert_resource(struct

resource *parent, struct resou

if (first == parent)
return first;
+ if (first == new) {
+ printk(KERN_ERR "You should not insert the"
+ "same resource twice!\n");
+ return first;
+ }

if ((first->start > new->start) || (first->end < new->end))
break;

Quoted 5 lines
kernel/resource.c renamed from kernel/resource.c~kernel-resourcec-handle-reinsertion-of-an-already-inserted-resource +0 -0
                if ((first->start > new->start) || (first->end < new->end))
                        break;

_

#5 lore raw
Quoted 6 lines · On Wed, 25 Aug 2010 13:03:55 +0800 Huang Shijie <[email protected]> wrote:

On Wed, Aug 25, 2010 at 7:46 AM, Andrew Morton
<[email protected]> wrote:

I think WARN_ON() is not power enough. If the CONFIG_BUG is not defined, the warning message may not be printed. So I think using a directly printk() here is better.

No, WARN_ON() is good. It gives us a backtrace and identifies the buggy code. Only maniacs turn off CONFIG_BUG.

#6 lore raw
Quoted 5 lines · Andrew Morton
Quoted 3 lines · On Wed, 25 Aug 2010 13:03:55 +0800 Huang Shijie <[email protected]> wrote:

On Wed, Aug 25, 2010 at 7:46 AM, Andrew Morton
<[email protected]> wrote:

Quoted 7 lines
Quoted 3 lines

I think WARN_ON() is not power enough. If the CONFIG_BUG is not defined, the warning message may not be printed. So I think using a directly printk() here is better.

No, WARN_ON() is good.  It gives us a backtrace and identifies the buggy code.  Only maniacs turn off CONFIG_BUG.

got it. Thanks a lot.