Skip to contentSkip to conversation

irq: clear kstat_irqs

PATCH In review Yinghai Lu 3 participants 13 messages Feb 6, 2009 – Feb 9, 2009 IRQ

lore
Share & tools
lore

Yinghai Lu

author
#0 lore raw

PATCH 1/1 irq: clear kstat_irqs

Impact: get correct kstat_irqs for msi etc

need to call clear_kstat_irqs(), so when reuse that irq_desc, could get correct kstat in /proc/interrupts

also could make /proc/interrupts don't have none-<NULL> entries

don't need to worry about arch that doesn't support genirq, because they will not call dynamic_irq_cleanup().

---
 kernel/irq/chip.c      |    1 +
 kernel/irq/handle.c    |   11 +++++++++++
 kernel/irq/internals.h |    1 +
 3 files changed, 13 insertions(+)
kernel/irq/chip.c +1 -0
Index: linux-2.6/kernel/irq/chip.c
===================================================================
@@ -81,6 +81,7 @@ void dynamic_irq_cleanup(unsigned int ir
 	desc->handle_irq = handle_bad_irq;
 	desc->chip = &no_irq_chip;
 	desc->name = NULL;
+	clear_kstat_irqs(desc);
 	spin_unlock_irqrestore(&desc->lock, flags);
 }
 
kernel/irq/handle.c +11 -0
Index: linux-2.6/kernel/irq/handle.c
===================================================================
@@ -270,6 +270,17 @@ struct irq_desc *irq_to_desc_alloc_cpu(u
 }
 #endif /* !CONFIG_SPARSE_IRQ */
 
+void clear_kstat_irqs(struct irq_desc *desc)
+{
+	unsigned long bytes;
+	char *ptr;
+
+	ptr = (char *)desc->kstat_irqs;
+	/* Compute how many bytes we need to clear */
+	bytes = nr_cpu_ids * sizeof(unsigned int);
+	memset(ptr, 0, bytes);
+}
+
 /*
  * What should we do if we get a hw irq event on an illegal vector?
  * Each architecture has to answer this themself.
kernel/irq/internals.h +1 -0
Index: linux-2.6/kernel/irq/internals.h
===================================================================
@@ -15,6 +15,7 @@ extern int __irq_set_trigger(struct irq_
 
 extern struct lock_class_key irq_desc_lock_class;
 extern void init_kstat_irqs(struct irq_desc *desc, int cpu, int nr);
+extern void clear_kstat_irqs(struct irq_desc *desc);
 extern spinlock_t sparse_irq_lock;
 
 #ifdef CONFIG_SPARSE_IRQ
#1 lore raw

kernel/irq/handle.c struct irq_desc *irq_to_desc_alloc_cpu(u · line 275

Quoted 3 lines · On Fri, 06 Feb 2009 13:50:47 -0800 Yinghai Lu <[email protected]> wrote:
kernel/irq/handle.c +3 -0
+void clear_kstat_irqs(struct irq_desc *desc)
+{
+	unsigned long bytes;

size_t would be more formally correct.

kernel/irq/handle.c struct irq_desc *irq_to_desc_alloc_cpu(u · line 280

Quoted 5 lines
kernel/irq/handle.c +5 -0
+	char *ptr;
+
+	ptr = (char *)desc->kstat_irqs;
+	/* Compute how many bytes we need to clear */
+	bytes = nr_cpu_ids * sizeof(unsigned int);

This is fragile. For example, if someone changes ->kstat_irqs to long then this code will secretly and subtly break.

If it used sizeof(*(desc->kstat_irqs)) then everything would magically continue to work.

kernel/irq/handle.c struct irq_desc *irq_to_desc_alloc_cpu(u · line 282

Quoted 2 lines
kernel/irq/handle.c +2 -0
+	memset(ptr, 0, bytes);
+}

The whole function could be written as

	memset(desc->kstat_irqs, 0, nr_cpu_ids * sizeof(*(desc->kstat_irqs)));
author
#2 lore raw

PATCH 1/1 irq: clear kstat_irqs v2

Impact: get correct kstat_irqs for msi/msi-x etc

need to call clear_kstat_irqs(), so when reuse that irq_desc, could get correct kstat in /proc/interrupts

also could make /proc/interrupts don't have none-<NULL> entries

don't need to worry about arch that doesn't support genirq, because they will not call dynamic_irq_cleanup().

v2: simplify and make clear_kstat_irqs more rebust accord to Andrew

---
 kernel/irq/chip.c      |    1 +
 kernel/irq/handle.c    |    5 +++++
 kernel/irq/internals.h |    1 +
 3 files changed, 7 insertions(+)
kernel/irq/chip.c +1 -0
Index: linux-2.6/kernel/irq/chip.c
===================================================================
@@ -81,6 +81,7 @@ void dynamic_irq_cleanup(unsigned int ir
 	desc->handle_irq = handle_bad_irq;
 	desc->chip = &no_irq_chip;
 	desc->name = NULL;
+	clear_kstat_irqs(desc);
 	spin_unlock_irqrestore(&desc->lock, flags);
 }
 
kernel/irq/handle.c +5 -0
Index: linux-2.6/kernel/irq/handle.c
===================================================================
@@ -270,6 +270,11 @@ struct irq_desc *irq_to_desc_alloc_cpu(u
 }
 #endif /* !CONFIG_SPARSE_IRQ */
 
+void clear_kstat_irqs(struct irq_desc *desc)
+{
+	memset(desc->kstat_irqs, 0, nr_cpu_ids * sizeof(*(desc->kstat_irqs)));
+}
+
 /*
  * What should we do if we get a hw irq event on an illegal vector?
  * Each architecture has to answer this themself.
kernel/irq/internals.h +1 -0
Index: linux-2.6/kernel/irq/internals.h
===================================================================
@@ -15,6 +15,7 @@ extern int __irq_set_trigger(struct irq_
 
 extern struct lock_class_key irq_desc_lock_class;
 extern void init_kstat_irqs(struct irq_desc *desc, int cpu, int nr);
+extern void clear_kstat_irqs(struct irq_desc *desc);
 extern spinlock_t sparse_irq_lock;
 
 #ifdef CONFIG_SPARSE_IRQ
author
#3 lore raw

PATCH 1/1 irq: optimize init_kstat_irqs/init_copy_kstat_irqs

Impact: even later type of kstat_irqs is changed

simplify and make init_kstat_irqs etc more type prof according to Andrew

---
 kernel/irq/handle.c       |   20 +++++++++++---------
 kernel/irq/numa_migrate.c |   11 +++--------
 2 files changed, 14 insertions(+), 17 deletions(-)
kernel/irq/handle.c +11 -9
Index: linux-2.6/kernel/irq/handle.c
===================================================================
@@ -82,19 +82,21 @@ static struct irq_desc irq_desc_init = {
 
 void init_kstat_irqs(struct irq_desc *desc, int cpu, int nr)
 {
-	unsigned long bytes;
-	char *ptr;
 	int node;
-
-	/* Compute how many bytes we need per irq and allocate them */
-	bytes = nr * sizeof(unsigned int);
+	void *ptr;
 
 	node = cpu_to_node(cpu);
-	ptr = kzalloc_node(bytes, GFP_ATOMIC, node);
-	printk(KERN_DEBUG "  alloc kstat_irqs on cpu %d node %d\n", cpu, node);
+	ptr = kzalloc_node(nr * sizeof(*desc->kstat_irqs), GFP_ATOMIC, node);
 
-	if (ptr)
-		desc->kstat_irqs = (unsigned int *)ptr;
+	/*
+	 * don't overwite if can not get new one
+	 * init_copy_kstat_irqs() could still use old one
+	 */
+	if (ptr) {
+		printk(KERN_DEBUG "  alloc kstat_irqs on cpu %d node %d\n",
+			 cpu, node);
+		desc->kstat_irqs = ptr;
+	}
 }
 
 static void init_one_irq_desc(int irq, struct irq_desc *desc, int cpu)
kernel/irq/numa_migrate.c +3 -8
Index: linux-2.6/kernel/irq/numa_migrate.c
===================================================================
@@ -17,16 +17,11 @@ static void init_copy_kstat_irqs(struct
 				 struct irq_desc *desc,
 				 int cpu, int nr)
 {
-	unsigned long bytes;
-
 	init_kstat_irqs(desc, cpu, nr);
 
-	if (desc->kstat_irqs != old_desc->kstat_irqs) {
-		/* Compute how many bytes we need per irq and allocate them */
-		bytes = nr * sizeof(unsigned int);
-
-		memcpy(desc->kstat_irqs, old_desc->kstat_irqs, bytes);
-	}
+	if (desc->kstat_irqs != old_desc->kstat_irqs)
+		memcpy(desc->kstat_irqs, old_desc->kstat_irqs,
+			 nr * sizeof(*desc->kstat_irqs));
 }
 
 static void free_kstat_irqs(struct irq_desc *old_desc, struct irq_desc *desc)
#4 lore raw

Re: [PATCH] irq: optimize init_kstat_irqs/init_copy_kstat_irqs

Quoted 34 lines · On Fri, 06 Feb 2009 23:49:41 -0800 Yinghai Lu <[email protected]> wrote:

Impact: even later type of kstat_irqs is changed

simplify and make init_kstat_irqs etc more type prof according to Andrew

---
 kernel/irq/handle.c       |   20 +++++++++++---------
 kernel/irq/numa_migrate.c |   11 +++--------
 2 files changed, 14 insertions(+), 17 deletions(-)

Index: linux-2.6/kernel/irq/handle.c
===================================================================

kernel/irq/handle.c +2 -8
@@ -82,19 +82,21 @@ static struct irq_desc irq_desc_init = {
 
 void init_kstat_irqs(struct irq_desc *desc, int cpu, int nr)
 {
-	unsigned long bytes;
-	char *ptr;
 	int node;
-
-	/* Compute how many bytes we need per irq and allocate them */
-	bytes = nr * sizeof(unsigned int);
+	void *ptr;
 
 	node = cpu_to_node(cpu);
-	ptr = kzalloc_node(bytes, GFP_ATOMIC, node);
-	printk(KERN_DEBUG "  alloc kstat_irqs on cpu %d node %d\n", cpu, node);
+	ptr = kzalloc_node(nr * sizeof(*desc->kstat_irqs), GFP_ATOMIC, node);
 
-	if (ptr)

kernel/irq/handle.c static struct irq_desc irq_desc_init = { · line 99

Quoted 10 lines
kernel/irq/handle.c +9 -1
-		desc->kstat_irqs = (unsigned int *)ptr;
+	/*
+	 * don't overwite if can not get new one
+	 * init_copy_kstat_irqs() could still use old one
+	 */
+	if (ptr) {
+		printk(KERN_DEBUG "  alloc kstat_irqs on cpu %d node %d\n",
+			 cpu, node);
+		desc->kstat_irqs = ptr;
+	}

else
init_one_irq_desc() goes BUG.

kernel/irq/handle.c static struct irq_desc irq_desc_init = { · line 100

Quoted 1 line
kernel/irq/handle.c +0 -0
 }

Sorry, but it's just not acceptable for irq_to_desc_alloc_cpu() and init_one_irq_desc() to go BUG if a GFP_ATOMIC allocation attempt failed.

If this code is only called on kernel boot then OK, that's acceptable. But if that is the case then all this code should be marked __init/__initdata. And ack_apic_edge() sure doesn't look like a boot-time-only function.

This is basic stuff which even a cursory review should have picked up.

Also, please go through all this code and convert WARN_ON(1) into WARN(), and convert BUG_ON(1) into BUG().

author
#5 lore raw

Re: [PATCH] irq: optimize init_kstat_irqs/init_copy_kstat_irqs

Quoted 52 lines · Andrew Morton
Quoted 34 lines · On Fri, 06 Feb 2009 23:49:41 -0800 Yinghai Lu <[email protected]> wrote:

Impact: even later type of kstat_irqs is changed

simplify and make init_kstat_irqs etc more type prof according to Andrew

---
 kernel/irq/handle.c       |   20 +++++++++++---------
 kernel/irq/numa_migrate.c |   11 +++--------
 2 files changed, 14 insertions(+), 17 deletions(-)

Index: linux-2.6/kernel/irq/handle.c
===================================================================

kernel/irq/handle.c +2 -8
@@ -82,19 +82,21 @@ static struct irq_desc irq_desc_init = {
 
 void init_kstat_irqs(struct irq_desc *desc, int cpu, int nr)
 {
-     unsigned long bytes;
-     char *ptr;
      int node;
-
-     /* Compute how many bytes we need per irq and allocate them */
-     bytes = nr * sizeof(unsigned int);
+     void *ptr;
 
      node = cpu_to_node(cpu);
-     ptr = kzalloc_node(bytes, GFP_ATOMIC, node);
-     printk(KERN_DEBUG "  alloc kstat_irqs on cpu %d node %d\n", cpu, node);
+     ptr = kzalloc_node(nr * sizeof(*desc->kstat_irqs), GFP_ATOMIC, node);
 
-     if (ptr)

kernel/irq/handle.c static struct irq_desc irq_desc_init = { · line 99

Quoted 10 lines
kernel/irq/handle.c +9 -1
-             desc->kstat_irqs = (unsigned int *)ptr;
+     /*
+      * don't overwite if can not get new one
+      * init_copy_kstat_irqs() could still use old one
+      */
+     if (ptr) {
+             printk(KERN_DEBUG "  alloc kstat_irqs on cpu %d node %d\n",
+                      cpu, node);
+             desc->kstat_irqs = ptr;
+     }

else
init_one_irq_desc() goes BUG.

kernel/irq/handle.c static struct irq_desc irq_desc_init = { · line 100

Quoted 1 line
kernel/irq/handle.c +0 -0
 }
Quoted 8 lines

Sorry, but it's just not acceptable for irq_to_desc_alloc_cpu() and init_one_irq_desc() to go BUG if a GFP_ATOMIC allocation attempt failed.

If this code is only called on kernel boot then OK, that's acceptable. But if that is the case then all this code should be marked __init/__initdata. And ack_apic_edge() sure doesn't look like a boot-time-only function.

add kzalloc_node_safe()?

Quoted 5 lines

This is basic stuff which even a cursory review should have picked up.

Also, please go through all this code and convert WARN_ON(1) into WARN(), and convert BUG_ON(1) into BUG().

sure, will do it.

YH

#6 lore raw

Re: [PATCH] irq: optimize init_kstat_irqs/init_copy_kstat_irqs

Quoted 2 lines · On Sat, 7 Feb 2009 01:01:03 -0800 Yinghai Lu <[email protected]> wrote:

add kzalloc_node_safe()?

I cannot find that function.

#7 lore raw

Re: [PATCH] irq: optimize init_kstat_irqs/init_copy_kstat_irqs

Quoted 6 lines · Andrew Morton
Quoted 2 lines · On Sat, 7 Feb 2009 01:01:03 -0800 Yinghai Lu <[email protected]> wrote:

add kzalloc_node_safe()?

I cannot find that function.

His suggestion is to provide that allocator variant.

Ingo

#8 lore raw

Re: [PATCH] irq: optimize init_kstat_irqs/init_copy_kstat_irqs

Quoted 12 lines · On Mon, 9 Feb 2009 09:11:24 +0100 Ingo Molnar <[email protected]> wrote:
Quoted 6 lines · Andrew Morton
Quoted 2 lines · On Sat, 7 Feb 2009 01:01:03 -0800 Yinghai Lu <[email protected]> wrote:

add kzalloc_node_safe()?

I cannot find that function.

His suggestion is to provide that allocator variant.

Oh.

It isn't possible to write a kzalloc_node_safe(GFP_ATOMIC). Or at least, we've never worked out a way.

Maybe I'm confused again.

#9 lore raw

Re: [PATCH] irq: optimize init_kstat_irqs/init_copy_kstat_irqs

Quoted 15 lines · Andrew Morton
Quoted 12 lines · On Mon, 9 Feb 2009 09:11:24 +0100 Ingo Molnar <[email protected]> wrote:
Quoted 6 lines · Andrew Morton
Quoted 2 lines · On Sat, 7 Feb 2009 01:01:03 -0800 Yinghai Lu <[email protected]> wrote:

add kzalloc_node_safe()?

I cannot find that function.

His suggestion is to provide that allocator variant.

Quoted 6 lines

Oh.

It isn't possible to write a kzalloc_node_safe(GFP_ATOMIC). Or at least, we've never worked out a way.

Maybe I'm confused again.

Indeed - duh - more morning tea needed.

Yinghai, why are those allocations GFP_ATOMIC to begin with? These:

earth4:~/tip> grep GFP_ATOMIC kernel/irq/*.c
kernel/irq/handle.c: ptr = kzalloc_node(nr * sizeof(*desc->kstat_irqs), GFP_ATOMIC, node);
kernel/irq/handle.c: desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);
kernel/irq/manage.c: action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);

Should all be GFP_KERNEL. Wherever they are within a spinlocked section the code should be restructured. All descriptor data structures should be preallocated at __setup_irq() time. If we ever need to allocate dynamically later on, in the middle of some difficult codepath that's a structure bug in the code.

and this one:

kernel/irq/numa_migrate.c: desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);

should fail the migration silently if GFP_ATOMIC returns NULL.

Ingo

#10 lore raw

Re: [PATCH] irq: optimize init_kstat_irqs/init_copy_kstat_irqs

Quoted 34 lines · On Mon, 9 Feb 2009 09:37:39 +0100 Ingo Molnar <[email protected]> wrote:
Quoted 15 lines · Andrew Morton
Quoted 12 lines · On Mon, 9 Feb 2009 09:11:24 +0100 Ingo Molnar <[email protected]> wrote:
Quoted 6 lines · Andrew Morton
Quoted 2 lines · On Sat, 7 Feb 2009 01:01:03 -0800 Yinghai Lu <[email protected]> wrote:

add kzalloc_node_safe()?

I cannot find that function.

His suggestion is to provide that allocator variant.

Quoted 6 lines

Oh.

It isn't possible to write a kzalloc_node_safe(GFP_ATOMIC). Or at least, we've never worked out a way.

Maybe I'm confused again.

Indeed - duh - more morning tea needed.

Yinghai, why are those allocations GFP_ATOMIC to begin with? These:

earth4:~/tip> grep GFP_ATOMIC kernel/irq/*.c
kernel/irq/handle.c: ptr = kzalloc_node(nr * sizeof(*desc->kstat_irqs), GFP_ATOMIC, node);
kernel/irq/handle.c: desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);
kernel/irq/manage.c: action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);

Quoted 4 lines

Should all be GFP_KERNEL. Wherever they are within a spinlocked section the code should be restructured. All descriptor data structures should be preallocated at __setup_irq() time. If we ever need to allocate dynamically later on, in the middle of some difficult codepath that's a structure bug in the code.

yup, something along those lines.

Quoted 5 lines

and this one:

kernel/irq/numa_migrate.c: desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);

should fail the migration silently if GFP_ATOMIC returns NULL.

Silent failure sounds bad?

The allocation attempt will spew a page-allocation-failure backtrace anyway, so people will still get alarmed.

You might instead choose to suppress that warning with __GFP_NOWARN and instead add a more meaningful warning at the calling codesite.

author
#12 lore raw

Re: [PATCH] irq: optimize init_kstat_irqs/init_copy_kstat_irqs

In reply to Ingo Molnar
Quoted 28 lines · Ingo Molnar
Quoted 8 lines · Andrew Morton
Quoted 6 lines · On Mon, 9 Feb 2009 09:11:24 +0100 Ingo Molnar <[email protected]> wrote:
Quoted 4 lines · Andrew Morton
Quoted 1 line · On Sat, 7 Feb 2009 01:01:03 -0800 Yinghai Lu <[email protected]> wrote:

add kzalloc_node_safe()?

I cannot find that function.

Quoted 8 lines
Quoted 2 lines

His suggestion is to provide that allocator variant.

Oh.

It isn't possible to write a kzalloc_node_safe(GFP_ATOMIC). Or at least, we've never worked out a way.

Maybe I'm confused again.

Indeed - duh - more morning tea needed.

Yinghai, why are those allocations GFP_ATOMIC to begin with? These:

earth4:~/tip> grep GFP_ATOMIC kernel/irq/*.c
kernel/irq/handle.c: ptr = kzalloc_node(nr * sizeof(*desc->kstat_irqs), GFP_ATOMIC, node);
kernel/irq/handle.c: desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);
kernel/irq/manage.c: action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);

Quoted 4 lines

Should all be GFP_KERNEL. Wherever they are within a spinlocked section the code should be restructured. All descriptor data structures should be preallocated at __setup_irq() time. If we ever need to allocate dynamically later on, in the middle of some difficult codepath that's a structure bug in the code.

ok.

Quoted 6 lines

and this one:

kernel/irq/numa_migrate.c: desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);

should fail the migration silently if GFP_ATOMIC returns NULL.

it will reuse the old one.

YH

#11 lore raw

Re: [PATCH] irq: optimize init_kstat_irqs/init_copy_kstat_irqs

In reply to Andrew Morton
Quoted 49 lines · Andrew Morton
Quoted 34 lines · On Mon, 9 Feb 2009 09:37:39 +0100 Ingo Molnar <[email protected]> wrote:
Quoted 15 lines · Andrew Morton
Quoted 12 lines · On Mon, 9 Feb 2009 09:11:24 +0100 Ingo Molnar <[email protected]> wrote:
Quoted 6 lines · Andrew Morton
Quoted 2 lines · On Sat, 7 Feb 2009 01:01:03 -0800 Yinghai Lu <[email protected]> wrote:

add kzalloc_node_safe()?

I cannot find that function.

His suggestion is to provide that allocator variant.

Quoted 6 lines

Oh.

It isn't possible to write a kzalloc_node_safe(GFP_ATOMIC). Or at least, we've never worked out a way.

Maybe I'm confused again.

Indeed - duh - more morning tea needed.

Yinghai, why are those allocations GFP_ATOMIC to begin with? These:

earth4:~/tip> grep GFP_ATOMIC kernel/irq/*.c
kernel/irq/handle.c: ptr = kzalloc_node(nr * sizeof(*desc->kstat_irqs), GFP_ATOMIC, node);
kernel/irq/handle.c: desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);
kernel/irq/manage.c: action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);

Quoted 4 lines

Should all be GFP_KERNEL. Wherever they are within a spinlocked section the code should be restructured. All descriptor data structures should be preallocated at __setup_irq() time. If we ever need to allocate dynamically later on, in the middle of some difficult codepath that's a structure bug in the code.

yup, something along those lines.

Quoted 5 lines

and this one:

kernel/irq/numa_migrate.c: desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);

should fail the migration silently if GFP_ATOMIC returns NULL.

Quoted 7 lines

Silent failure sounds bad?

The allocation attempt will spew a page-allocation-failure backtrace anyway, so people will still get alarmed.

You might instead choose to suppress that warning with __GFP_NOWARN and instead add a more meaningful warning at the calling codesite.

Well this is a NUMA performance optimization that gets re-tried anyway, so spewing anything because we are so low on RAM that we cannot fulfill GFP_ATOMIC wont help things. Silence is better there i think. At most a WARN_ONCE().

Ingo