Skip to contentSkip to conversation

irq: radix_tree_insert can fail

PATCH In review Milton Miller 4 participants 12 messages May 25, 2011 – May 27, 2011 1 message not in the archive IRQ

lore
Share & tools
lore
#5 lore raw

PATCH 2/4 irq: radix_tree_insert can fail

In reply to a message not in the archive

Check the insert, and if it fails cleanup and free all partial work.

Sparse irq was not checking the return code from radix_tree_insert, but it may need to allocate memory and can fail. If it failed, it still claimed success to the caller but the affected irq(s) are unavailable and the reference to the affected descriptors is leaked.

---
I started by tring to change free_desc to take the descriptor pointer and pushing that down, but that soon ran into conflicts between the array and sparse implementations, and/or the old dynamic irq cleanup function that is still used by some architectures. This version is targeted, and also protects against scribbles to irq_data.irq.

kernel/irq/irqdesc.c +12 -5
Index: work.git/kernel/irq/irqdesc.c
===================================================================
@@ -164,10 +164,8 @@ err_desc:
 	return NULL;
 }
 
-static void free_desc(unsigned int irq)
+static void free_a_desc(unsigned int irq, struct irq_desc *desc)
 {
-	struct irq_desc *desc = irq_to_desc(irq);
-
 	unregister_irq_proc(irq, desc);
 
 	mutex_lock(&sparse_irq_lock);
@@ -179,21 +177,30 @@ static void free_desc(unsigned int irq)
 	kfree(desc);
 }
 
+static void free_desc(unsigned int irq)
+{
+	free_a_desc(irq, irq_to_desc(irq));
+}
+
 static int alloc_descs(unsigned int start, unsigned int cnt, int node)
 {
 	struct irq_desc *desc;
-	int i;
+	int i, res;
 
 	for (i = 0; i < cnt; i++) {
 		desc = alloc_desc(start + i, node);
 		if (!desc)
 			goto err;
 		mutex_lock(&sparse_irq_lock);
-		irq_insert_desc(start + i, desc);
+		res = irq_insert_desc(start + i, desc);
 		mutex_unlock(&sparse_irq_lock);
+		if (res)
+			goto err_insert;
 	}
 	return start;
 
+err_insert:
+	free_a_desc(start + i, desc);
 err:
 	for (i--; i >= 0; i--)
 		free_desc(start + i);
#8 lore raw

PATCH 3/4 irq: remove unnecessary __ref on irq_alloc_descs

In reply to a message not in the archive

febcb0c59a (irq: Remove unnecessary bootmem code) removed the slab_is_available() conditonal call to alloc_bootmem* but missed removeing the __ref annotation.

kernel/irq/irqdesc.c +1 -2
Index: work.git/kernel/irq/irqdesc.c
===================================================================
@@ -351,8 +351,7 @@ EXPORT_SYMBOL_GPL(irq_free_descs);
  *
  * Returns the first irq number or error code
  */
-int __ref
-irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node)
+int irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node)
 {
 	int start, ret;
 
#9 lore raw

RFC PATCH 4/4 irq: allow a per-allocation upper limit when allocating irqs

In reply to a message not in the archive

Allow the option to specify an upper limit to the irq numbers for each allocation. The limit is non-inclusive, and 0 means no limit needed by caller.

Some irq chips can support a relative large and arbitrary range, but not infinite. For example, they may use the linux irq number as the msi desciptor data, which for msi is 16 bits.

Since e7bcecb7b1 (genirq: Make nr_irqs runtime expandable), checking NR_IRQS or even nr_irqs is not sufficient to enforce such requirements when sparse irqs are configured.

Based on an irc discussion, make the limit per call instead of an arch callback or global setting.

If the requested count is above the limit, return -ENOMEM as if nr_irqs could not be expanded, assuming the limit was specified at a different layer than the count.

This code does not try to keep the prior semantics of irq_alloc_descs when irq was non-negative but not equal to from. I believe it was an implementation artifact instead of a designed feature that one could specify search starting from 4 and fail if the allocated irq was not exactly 6, confirming that the intervening irqs were reserved.

Thomas,

This is still RFC because I haven't gotten far enough in my series to actually use the patch yet. If you choose not to apply it now, should I submit a patch to enforce irq matches from if its not -1 (or otherwise negative)?

Contrasting to Grant's earlier proposal, this patch:
(1) has a non-inclusive limit, sutiable for showing size of an array etc.
(2) has no redundant parameter for a specific irq to be allocated after inlines
(3) avoids searching areas that will be rejected by calculating the end, which also simpifies the search result check.

milton

include/linux/irq.h +11 -1
Index: work.git/include/linux/irq.h
===================================================================
@@ -546,10 +546,20 @@ static inline struct msi_desc *irq_data_
 	return d->msi_desc;
 }
 
-int irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node);
+int irq_alloc_descs_range(unsigned int from, unsigned int limit, unsigned int cnt, int node);
 void irq_free_descs(unsigned int irq, unsigned int cnt);
 int irq_reserve_irqs(unsigned int from, unsigned int cnt);
 
+int irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node)
+{
+	if (irq < 0)
+		return irq_alloc_descs_range(from, 0, cnt, node);
+	/* fail if specified start at 4 and obtain 6 */
+	if (irq != from)
+		return -EEXIST;
+	return irq_alloc_descs_range(from, from + cnt, cnt, node);
+}
+
 static inline int irq_alloc_desc(int node)
 {
 	return irq_alloc_descs(-1, 0, 1, node);
kernel/irq/irqdesc.c +18 -8
Index: work.git/kernel/irq/irqdesc.c
===================================================================
@@ -343,27 +343,37 @@ void irq_free_descs(unsigned int from, u
 EXPORT_SYMBOL_GPL(irq_free_descs);
 
 /**
- * irq_alloc_descs - allocate and initialize a range of irq descriptors
- * @irq:	Allocate for specific irq number if irq >= 0
+ * irq_alloc_descs_range - allocate and initialize a range of irq descriptors
  * @from:	Start the search from this irq number
+ * @limit:	Unless zero, all irq numbers must be less than this value
  * @cnt:	Number of consecutive irqs to allocate.
  * @node:	Preferred node on which the irq descriptor should be allocated
  *
  * Returns the first irq number or error code
  */
-int irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node)
+int irq_alloc_descs_range(unsigned int from, unsigned int limit,
+		unsigned int cnt, int node)
 {
-	int start, ret;
+	unsigned int start, end;
+	int ret;
 
 	if (!cnt)
 		return -EINVAL;
+	if (limit) {
+		if (cnt > limit)
+			return -ENOMEM;
+		if (from > limit - cnt)
+			return -EINVAL;
+		end = min_t(unsigned int, limit, IRQ_BITMAP_BITS);
+	} else {
+		end = IRQ_BITMAP_BITS;
+	}
 
 	mutex_lock(&sparse_irq_lock);
 
-	start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,
-					   from, cnt, 0);
+	start = bitmap_find_next_zero_area(allocated_irqs, end, from, cnt, 0);
 	ret = -EEXIST;
-	if (irq >=0 && start != irq)
+	if (start >= end)
 		goto err;
 
 	if (start + cnt > nr_irqs) {
@@ -380,7 +390,7 @@ err:
 	mutex_unlock(&sparse_irq_lock);
 	return ret;
 }
-EXPORT_SYMBOL_GPL(irq_alloc_descs);
+EXPORT_SYMBOL_GPL(irq_alloc_descs_range);
 
 /**
  * irq_reserve_irqs - mark irqs allocated
#1 lore raw

PATCH 1/4 sparse irq: protect irq_to_desc against irq_free_descs

In reply to a message not in the archive

The radix-tree code uses call_rcu to delay freeing internal data elements when removing when deleting an entry. We must protect against the elements being freed while we traverse the tree.

While preparing a patch to expand the contexts in which the radix tree optionally used by powerpc for mapping hardware irq numbers to linux numbers would be called, I realized that the radix tree was not locked when radix_tree_lookup was called. I then realized the same issue applies to the generic irq code when sparse irqs are in use.

While the powerpc radix tree was only referenced from one callsite that was irqs_disabled and irq_enter, irq_to_desc is called from many more contexts including threaded irq handlers and other process contexts.

This does not show up in the rcu lockdep because in 2.6.34 commit 2676a58c98 (radix-tree: Disable RCU lockdep checking in radix tree) deemed it too hard to pass the condition of the protecting lock to the library.

---
I expect the relatively infrequent calls to irq_free_descs, combined with most calls to irq_to_desc being irqs_disabled and the fact merged to mainline implemntations of call_rcu requiring a cpu to respond to a hard irq or schedule has hidden this error to date.

kernel/irq/irqdesc.c +7 -1
Index: work.git/kernel/irq/irqdesc.c
===================================================================
@@ -108,7 +108,13 @@ static void irq_insert_desc(unsigned int
 
 struct irq_desc *irq_to_desc(unsigned int irq)
 {
-	return radix_tree_lookup(&irq_desc_tree, irq);
+	struct irq_desc *desc;
+
+	rcu_read_lock();
+	desc = radix_tree_lookup(&irq_desc_tree, irq);
+	rcu_read_unlock();
+
+	return desc
 }
 
 static void delete_irq_desc(unsigned int irq)
#10 lore raw

Re: [PATCH RFC 4/4] irq: allow a per-allocation upper limit when allocating irqs

In reply to Milton Miller
Quoted 11 lines · Milton Miller

Allow the option to specify an upper limit to the irq numbers for each allocation. The limit is non-inclusive, and 0 means no limit needed by caller.

Some irq chips can support a relative large and arbitrary range, but not infinite. For example, they may use the linux irq number as the msi desciptor data, which for msi is 16 bits.

Since e7bcecb7b1 (genirq: Make nr_irqs runtime expandable), checking NR_IRQS or even nr_irqs is not sufficient to enforce such requirements when sparse irqs are configured.

Would be nice to add some more background info to the changelog, like what bad things happen if this change is not provided and what good things would happen if it is provided.

Thanks,

Ingo

#2 lore raw

Re: [PATCH 1/4] sparse irq: protect irq_to_desc against irq_free_descs

In reply to Milton Miller
Quoted 20 lines · Milton Miller

The radix-tree code uses call_rcu to delay freeing internal data elements when removing when deleting an entry. We must protect against the elements being freed while we traverse the tree.

While preparing a patch to expand the contexts in which the radix tree optionally used by powerpc for mapping hardware irq numbers to linux numbers would be called, I realized that the radix tree was not locked when radix_tree_lookup was called. I then realized the same issue applies to the generic irq code when sparse irqs are in use.

While the powerpc radix tree was only referenced from one callsite that was irqs_disabled and irq_enter, irq_to_desc is called from many more contexts including threaded irq handlers and other process contexts.

This does not show up in the rcu lockdep because in 2.6.34 commit 2676a58c98 (radix-tree: Disable RCU lockdep checking in radix tree) deemed it too hard to pass the condition of the protecting lock to the library.

Quoted 7 lines

---
I expect the relatively infrequent calls to irq_free_descs, combined with most calls to irq_to_desc being irqs_disabled and the fact merged to mainline implemntations of call_rcu requiring a cpu to respond to a hard irq or schedule has hidden this error to date.

The reason why nobody ever noticed is that the free happens in the teardown path of PCI devices and at this point nothing accesses that irq anymore.

Quoted 6 lines

Index: work.git/kernel/irq/irqdesc.c
===================================================================

kernel/irq/irqdesc.c +0 -0
@@ -108,7 +108,13 @@ static void irq_insert_desc(unsigned int
 

kernel/irq/irqdesc.c static void irq_insert_desc(unsigned int · line 117

Quoted 10 lines
kernel/irq/irqdesc.c +7 -1
 struct irq_desc *irq_to_desc(unsigned int irq)
 {
-	return radix_tree_lookup(&irq_desc_tree, irq);
+	struct irq_desc *desc;
+
+	rcu_read_lock();
+	desc = radix_tree_lookup(&irq_desc_tree, irq);
+	rcu_read_unlock();
+
+	return desc

That does not really compile :)

And it does not help at all because we unconditionally free the irq descriptor and do not use rcu based kfree. Further you protect only the lookup and not the complete section which uses the descriptor, so it could go away after the rcu_read_unlock() in theory.

Thanks,

tglx

#6 lore raw
In reply to Milton Miller
Quoted 7 lines · Milton Miller

Check the insert, and if it fails cleanup and free all partial work.

Sparse irq was not checking the return code from radix_tree_insert, but it may need to allocate memory and can fail. If it failed, it still claimed success to the caller but the affected irq(s) are unavailable and the reference to the affected descriptors is leaked.

Quoted 7 lines

---
I started by tring to change free_desc to take the descriptor pointer and pushing that down, but that soon ran into conflicts between the array and sparse implementations, and/or the old dynamic irq cleanup function that is still used by some architectures. This version is targeted, and also protects against scribbles to irq_data.irq.

The simpler solution is to move irq_insert_desc() into alloc_desc() and deal with the error case there.

Thanks,

tglx

#11 lore raw

Re: [PATCH RFC 4/4] irq: allow a per-allocation upper limit when allocating irqs

In reply to Milton Miller
Quoted 2 lines · Milton Miller

+int irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node)

You might want this to be inline :)

include/linux/irq.h static inline struct msi_desc *irq_data_ · line 557

Quoted 4 lines
include/linux/irq.h +4 -0
+{
+	if (irq < 0)
+		return irq_alloc_descs_range(from, 0, cnt, node);
+	/* fail if specified start at 4 and obtain 6 */

-ENOPARSE

include/linux/irq.h static inline struct msi_desc *irq_data_ · line 559

Quoted 2 lines
include/linux/irq.h +2 -0
+	if (irq != from)
+		return -EEXIST;

-EINVAL perhaps ?

Quoted 25 lines
include/linux/irq.h +3 -0
+	return irq_alloc_descs_range(from, from + cnt, cnt, node);
+}
+
 static inline int irq_alloc_desc(int node)
 {
 	return irq_alloc_descs(-1, 0, 1, node);

Index: work.git/kernel/irq/irqdesc.c
===================================================================

kernel/irq/irqdesc.c +2 -3
@@ -343,27 +343,37 @@ void irq_free_descs(unsigned int from, u
 EXPORT_SYMBOL_GPL(irq_free_descs);
 
 /**
- * irq_alloc_descs - allocate and initialize a range of irq descriptors
- * @irq:	Allocate for specific irq number if irq >= 0
+ * irq_alloc_descs_range - allocate and initialize a range of irq descriptors
  * @from:	Start the search from this irq number
+ * @limit:	Unless zero, all irq numbers must be less than this value
  * @cnt:	Number of consecutive irqs to allocate.
  * @node:	Preferred node on which the irq descriptor should be allocated
  *
  * Returns the first irq number or error code
  */
-int irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node)

kernel/irq/irqdesc.c void irq_free_descs(unsigned int from, u · line 362

Quoted 10 lines
kernel/irq/irqdesc.c +5 -1
+int irq_alloc_descs_range(unsigned int from, unsigned int limit,
+		unsigned int cnt, int node)
 {
-	int start, ret;
+	unsigned int start, end;
+	int ret;
 
 	if (!cnt)
 		return -EINVAL;
+	if (limit) {

Why 0 ? Just use UINT_MAX (or something like IRQ_ALLOC_ANY) for the limit when you want an unlimited allocation.

Quoted 8 lines
kernel/irq/irqdesc.c +8 -0
+		if (cnt > limit)
+			return -ENOMEM;
+		if (from > limit - cnt)
+			return -EINVAL;
+		end = min_t(unsigned int, limit, IRQ_BITMAP_BITS);
+	} else {
+		end = IRQ_BITMAP_BITS;
+	}

kernel/irq/irqdesc.c void irq_free_descs(unsigned int from, u · line 377

Quoted 10 lines
kernel/irq/irqdesc.c +2 -3
 
 	mutex_lock(&sparse_irq_lock);
 
-	start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,
-					   from, cnt, 0);
+	start = bitmap_find_next_zero_area(allocated_irqs, end, from, cnt, 0);
 	ret = -EEXIST;
-	if (irq >=0 && start != irq)
+	if (start >= end)
 		goto err;

Otherwise I like the approach in general.

Thanks,

tglx

#7 lore raw
In reply to Thomas Gleixner
Quoted 17 lines · Thomas Gleixner
Quoted 7 lines · Milton Miller

Check the insert, and if it fails cleanup and free all partial work.

Sparse irq was not checking the return code from radix_tree_insert, but it may need to allocate memory and can fail. If it failed, it still claimed success to the caller but the affected irq(s) are unavailable and the reference to the affected descriptors is leaked.

Quoted 7 lines

---
I started by tring to change free_desc to take the descriptor pointer and pushing that down, but that soon ran into conflicts between the array and sparse implementations, and/or the old dynamic irq cleanup function that is still used by some architectures. This version is targeted, and also protects against scribbles to irq_data.irq.

Quoted 2 lines

The simpler solution is to move irq_insert_desc() into alloc_desc() and deal with the error case there.

And I see that this one too as written does not compile, due to my faulty config testing. I'll explore your idea tomorrow after some sleep, but it makes sense.

milton

#4 lore raw

Re: [PATCH 1/4] sparse irq: protect irq_to_desc against irq_free_descs

In reply to Milton Miller
Quoted 10 lines · Milton Miller
Quoted 4 lines · Thomas Gleixner

The reason why nobody ever noticed is that the free happens in the teardown path of PCI devices and at this point nothing accesses that irq anymore.

I'm not talking about the irq_desc that is being removed from the tree, instead I'm talking about the internal elements in the radix tree that are used to find the pointers at the bottom of the tree (that in turn point to the irq_desc in the irq tree case).

Oops, did not think about that one :)

-ENOTENOUGHCOFFEE

Thanks,

tglx

#3 lore raw

Re: [PATCH 1/4] sparse irq: protect irq_to_desc against irq_free_descs

In reply to Thomas Gleixner
Quoted 29 lines · Thomas Gleixner
Quoted 20 lines · Milton Miller

The radix-tree code uses call_rcu to delay freeing internal data elements when removing when deleting an entry. We must protect against the elements being freed while we traverse the tree.

While preparing a patch to expand the contexts in which the radix tree optionally used by powerpc for mapping hardware irq numbers to linux numbers would be called, I realized that the radix tree was not locked when radix_tree_lookup was called. I then realized the same issue applies to the generic irq code when sparse irqs are in use.

While the powerpc radix tree was only referenced from one callsite that was irqs_disabled and irq_enter, irq_to_desc is called from many more contexts including threaded irq handlers and other process contexts.

This does not show up in the rcu lockdep because in 2.6.34 commit 2676a58c98 (radix-tree: Disable RCU lockdep checking in radix tree) deemed it too hard to pass the condition of the protecting lock to the library.

Quoted 7 lines

---
I expect the relatively infrequent calls to irq_free_descs, combined with most calls to irq_to_desc being irqs_disabled and the fact merged to mainline implemntations of call_rcu requiring a cpu to respond to a hard irq or schedule has hidden this error to date.

Quoted 3 lines

The reason why nobody ever noticed is that the free happens in the teardown path of PCI devices and at this point nothing accesses that irq anymore.

I'm not talking about the irq_desc that is being removed from the tree, instead I'm talking about the internal elements in the radix tree that are used to find the pointers at the bottom of the tree (that in turn point to the irq_desc in the irq tree case).

Quoted 8 lines
Quoted 8 lines

Index: work.git/kernel/irq/irqdesc.c
===================================================================

kernel/irq/irqdesc.c +0 -0
@@ -108,7 +108,13 @@ static void irq_insert_desc(unsigned int
 
Quoted 12 lines

kernel/irq/irqdesc.c static void irq_insert_desc(unsigned int · line 117

Quoted 10 lines
kernel/irq/irqdesc.c +7 -1
 struct irq_desc *irq_to_desc(unsigned int irq)
 {
-	return radix_tree_lookup(&irq_desc_tree, irq);
+	struct irq_desc *desc;
+
+	rcu_read_lock();
+	desc = radix_tree_lookup(&irq_desc_tree, irq);
+	rcu_read_unlock();
+
+	return desc

That does not really compile :)

Hmm, Ooops I thought I had sparse irq enabled, but I had

CONFIG_HAVE_SPARSE_IRQ=y
# CONFIG_SPARSE_IRQ is not set

and indeed, I am missing a semicolon. Sorry about that.

Quoted 3 lines

And it does not help at all because we unconditionally free the irq descriptor and do not use rcu based kfree. Further you protect only

As I said above, its the actual radix tree leading to the descriptors that are currently freed with call_rcu that I'm tring to cover.

Quoted 2 lines

the lookup and not the complete section which uses the descriptor, so it could go away after the rcu_read_unlock() in theory.

Presently there is no locking in the generic layer between allocating a descriptor and allowing the irq to be used; that seems to be left to the code in the various architetures that call irq_alloc_descs.

In fact the generic layer doesn't check if there are irq actions chained off the irq, let alone if the irq is in progress or the thread is active. While I agree it may be prudent to add such locking, it is a seperate issue beyond traversing the radix tree.

Most of the architecture code I've seen so far doesn't check either. Powerpc calls synchronise_irq, but that leaves windows and doesn't gard against actions being registered. Otherwise it's up to the callers to not request an irq be freed before it is shutdown.

Back to this patch.

Depending on the tree I believe (from my partial reading of the radix tree code) an element being deleted could collapse the path to adjacent entries. The fact that the tree will not always collapse will further reduce the incidence of an error being detected and reported beyond the fact that a lookup has to be delayed across an rcu boundary, which the merged mainline rcu schemes will not do with hard irq disabled.

So one needs (1) the ability to collapse a tree node and (2) an irq descriptor lookup to traverse that node either (3a) not from an irq handler, but an irq thread or other process or bh context, or (3b) with an out-of-tree rcu such as the Concurent RT rcu that Paul mentioned, and (4) the free to result in the memory used by the lookup
traversial to be overwriten. Rare, but probably not impossible.

milton

#12 lore raw

Re: [PATCH RFC 4/4] irq: allow a per-allocation upper limit when allocating irqs

In reply to Milton Miller
Quoted 19 lines · Milton Miller

Allow the option to specify an upper limit to the irq numbers for each allocation. The limit is non-inclusive, and 0 means no limit needed by caller.

Some irq chips can support a relative large and arbitrary range, but not infinite. For example, they may use the linux irq number as the msi desciptor data, which for msi is 16 bits.

Since e7bcecb7b1 (genirq: Make nr_irqs runtime expandable), checking NR_IRQS or even nr_irqs is not sufficient to enforce such requirements when sparse irqs are configured.

Based on an irc discussion, make the limit per call instead of an arch callback or global setting.

If the requested count is above the limit, return -ENOMEM as if nr_irqs could not be expanded, assuming the limit was specified at a different layer than the count.

Quoted 8 lines

This code does not try to keep the prior semantics of irq_alloc_descs when irq was non-negative but not equal to from. I believe it was an implementation artifact instead of a designed feature that one could specify search starting from 4 and fail if the allocated irq was not exactly 6, confirming that the intervening irqs were reserved.

Looks good to me/

g.