Skip to contentSkip to conversation

genirq: Ensure we locate the passed IRQ in irq_alloc_descs()

PATCH In review Mark Brown 4 participants 12 messages Jun 2, 2011 – Jun 6, 2011 IRQ

lore
Share & tools
lore

Mark Brown

author
#0 lore raw

PATCH 1/1 genirq: Ensure we locate the passed IRQ in irq_alloc_descs()

When irq_alloc_descs() is called with no base IRQ specified then it will search for a range of IRQs starting from a specified base address. In the case where an IRQ is specified it still does this search in order to ensure that none of the requested range is already allocated and it still uses the from parameter to specify the base for the search. This means that in the case where a base is specified but from is zero (which is reasonable as any IRQ number is in the range specified by a zero from) the function will get confused and try to allocate the first suitably sized block of free IRQs it finds.

Instead use a specified IRQ as the base address for the search, and insist that any from that is specified can support that IRQ.

---
 kernel/irq/irqdesc.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
kernel/irq/irqdesc.c +6 -0
index 886e803..bb53d6c 100644
@@ -346,6 +346,12 @@ irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node)
 	if (!cnt)
 		return -EINVAL;
 
+	if (irq >= 0) {
+		if (from > irq)
+			return -EINVAL;
+		from = irq;
+	}
+
 	mutex_lock(&sparse_irq_lock);
 
 	start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,
Signature · 1 line
1.7.5.3
#1 lore raw
Quoted 26 lines · Mark Brown

When irq_alloc_descs() is called with no base IRQ specified then it will search for a range of IRQs starting from a specified base address. In the case where an IRQ is specified it still does this search in order to ensure that none of the requested range is already allocated and it still uses the from parameter to specify the base for the search. This means that in the case where a base is specified but from is zero (which is reasonable as any IRQ number is in the range specified by a zero from) the function will get confused and try to allocate the first suitably sized block of free IRQs it finds.

Instead use a specified IRQ as the base address for the search, and insist that any from that is specified can support that IRQ.

---
kernel/irq/irqdesc.c | 6 ++++++

 1 files changed, 6 insertions(+), 0 deletions(-)
kernel/irq/irqdesc.c +0 -0
index 886e803..bb53d6c 100644
@@ -346,6 +346,12 @@ irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node)
 	if (!cnt)
 		return -EINVAL;

kernel/irq/irqdesc.c irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node) · line 357

Quoted 10 lines
kernel/irq/irqdesc.c +6 -0
 
+	if (irq >= 0) {
+		if (from > irq)
+			return -EINVAL;
+		from = irq;
+	}
+
 	mutex_lock(&sparse_irq_lock);
 
 	start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,

and then right after this the code continues:

        ret = -EEXIST;
        if (irq >=0 && start != irq)
                goto err;

This patch enables exactly the calls I want to forbid ! Why do you need to verify that there are no irqs between from and irq ? What is your use case?

Change your caller to specify the irq twice if you need a specific irq block, or if you only need one then use the helper irq_alloc_desc_at.

If you want to change irq_alloc_descs, please make it return -EINVAL if irq >=0 && from != irq (like I did).

See http://lkml.indiana.edu/hypermail/linux/kernel/1105.3/00739.html
[PATCH RFC 4/4] irq: allow a per-allocation upper limit when allocating irqs

(and yes, I have made the changes based on the feedback but haven't had time to get back to the series).

Thanks,
milton
QUIT

author
#2 lore raw
Quoted 11 lines · Milton Miller
Quoted 1 line · Mark Brown

start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,

and then right after this the code continues:

        ret = -EEXIST;
        if (irq >=0 && start != irq)
                goto err;

This patch enables exactly the calls I want to forbid ! Why do

Which you wish to forbid because...? You've not articulated any motivation for doing this which makes it rather hard to engage here.

Quoted 1 line

you need to verify that there are no irqs between from and irq ?

I don't care if there are IRQs between from and the specified irq, all I care about is that we get back the irq we requested (apart from anything else the function will later error out if the allocated IRQ doesn't match the one that was specified - it seems very clear from both the code and documentation that if an IRQ is specified we're supposed to get it back).

- The specified IRQ is ignored except

Quoted 1 line

What is your use case?

I've requested a base IRQ but the only attention that irq_alloc_descs() is paying to it is that it generates an error rather than allocating something

Quoted 1 line

Change your caller to specify the irq twice if you need a specific irq

This seems like a poor UI for the function, if the user specified an irq_base and there's a suitable range of IRQs available at that base what is the benefit in refusing to allocate there? That's just going to make the system fragile against init ordering and driver disabling.

It's also going to be a bit more cumbersome to use:

	if (pdata->irq_base > 0) {
		irq_base = pdata->irq_base;
		from = pdata->irq_base;
	} else {
		irq_base = -1;
		from = 0;
	}
Quoted 1 line

block, or if you only need one then use the helper irq_alloc_desc_at.

I need about 60 IRQs in the particular driver where I noticed this.

Quoted 7 lines

If you want to change irq_alloc_descs, please make it return -EINVAL if irq >=0 && from != irq (like I did).

See http://lkml.indiana.edu/hypermail/linux/kernel/1105.3/00739.html
[PATCH RFC 4/4] irq: allow a per-allocation upper limit when allocating irqs

(and yes, I have made the changes based on the feedback but haven't

I don't really see the relevance of this patch? You're adding functionality for limiting the maximum IRQ number allocated which seems orthogonal to the issue.

#3 lore raw
Quoted 7 lines · Mark Brown
Quoted 5 lines · Milton Miller
Quoted 1 line · Mark Brown

start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,

and then right after this the code continues:

Quoted 8 lines
Quoted 5 lines
        ret = -EEXIST;
        if (irq >=0 && start != irq)
                goto err;

This patch enables exactly the calls I want to forbid ! Why do

Which you wish to forbid because...? You've not articulated any motivation for doing this which makes it rather hard to engage here.

In 2.6.39 all calls to irq_alloc_descs were from the helpers. Either from irq_alloc_descriptor_at , which says "I need this exact irq", or from irq_alloc_desc, which says "give me any irq".

I treated the arguments to irq_alloc_descs as having grown to accomidate the two uses having a common allocator with the partially redunant encoding. In one case an exact irq was specified (irq >= 0), and one that allocates from anywhere (irq < 0, all callers passed -1).

Maybe you have a new case.

Quoted 10 lines
Quoted 2 lines

you need to verify that there are no irqs between from and irq ?

I don't care if there are IRQs between from and the specified irq, all I care about is that we get back the irq we requested (apart from anything else the function will later error out if the allocated IRQ doesn't match the one that was specified - it seems very clear from both the code and documentation that if an IRQ is specified we're supposed to get it back).

Quoted 7 lines

- The specified IRQ is ignored except

Quoted 1 line

What is your use case?

I've requested a base IRQ but the only attention that irq_alloc_descs() is paying to it is that it generates an error rather than allocating something

Do you need a specific irq or an allocated one?

Or do you have a case where you don't know?

Quoted 7 lines
Quoted 2 lines

Change your caller to specify the irq twice if you need a specific irq

This seems like a poor UI for the function, if the user specified an irq_base and there's a suitable range of IRQs available at that base what is the benefit in refusing to allocate there? That's just going to make the system fragile against init ordering and driver disabling.

I am thinking two use cases: dynamic in a range, and pre specified (often by an intermediate layer).

But your example seems to imply a driver used in different environments:

Quoted 11 lines

It's also going to be a bit more cumbersome to use:

	if (pdata->irq_base > 0) {
		irq_base = pdata->irq_base;
		from = pdata->irq_base;
	} else {
		irq_base = -1;
		from = 0;
	}
Quoted 3 lines
Quoted 1 line

block, or if you only need one then use the helper irq_alloc_desc_at.

I need about 60 IRQs in the particular driver where I noticed this.

Do you need a block of 60? or just 60 somewhere?

How do you know from = 0 is safe?

Quoted 4 lines
Quoted 3 lines

If you want to change irq_alloc_descs, please make it return -EINVAL if irq >=0 && from != irq (like I did).

Quoted 8 lines
Quoted 4 lines

See http://lkml.indiana.edu/hypermail/linux/kernel/1105.3/00739.html
[PATCH RFC 4/4] irq: allow a per-allocation upper limit when allocating irqs

(and yes, I have made the changes based on the feedback but haven't

I don't really see the relevance of this patch? You're adding functionality for limiting the maximum IRQ number allocated which seems orthogonal to the issue.

Its relavant in that irq_alloc_descs_range no longer gets both irq and from; the information is passed to the underling allocator in a different form.

Ie dynamic allocations will be in [x, y), static will be at x.

It may be possible to still pass this information without additional arguments but I haven't had time to think about it yet.

I'm attending a conference and am quite busy but will see if I can spend some break time on this.

milton

author
#4 lore raw
Quoted 6 lines · Milton Miller

I treated the arguments to irq_alloc_descs as having grown to accomidate the two uses having a common allocator with the partially redunant encoding. In one case an exact irq was specified (irq >= 0), and one that allocates from anywhere (irq < 0, all callers passed -1).

Maybe you have a new case.

No, I'm only aware of those two cases. All my change does is make the irq parameter be enough to select between the two - at the minute it's just too weak.

Quoted 3 lines

Do you need a specific irq or an allocated one?

Or do you have a case where you don't know?

I need either a specific IRQ or an allocated one. This is just a very standard driver with an interrupt controller (well, there's a bunch of devices that are going to be doing the same thing - it's far from just one driver), it doesn't care what base it gets but systems can specify a base if they care for the externally visible interrupts (so that they can be supplied to other devices or whatever).

Quoted 3 lines
Quoted 1 line

I need about 60 IRQs in the particular driver where I noticed this.

Do you need a block of 60? or just 60 somewhere?

The driver assumes it's going to get a contiguous range, it'd be a lot of bookkeeping for no gain to have to cope with them being splattered all over the place.

Quoted 1 line

How do you know from = 0 is safe?

If the user cares they can just pick a number for the base; if they're going to pick a number they may as well pick the actual number.

Quoted 6 lines
Quoted 3 lines

I don't really see the relevance of this patch? You're adding functionality for limiting the maximum IRQ number allocated which seems orthogonal to the issue.

Its relavant in that irq_alloc_descs_range no longer gets both irq and from; the information is passed to the underling allocator in a different form.

That's not the goal of the patch, it's just something the patch happens to do as part of the implementation as far as I can see.

Acked-by
#5 lore raw

[Replying to both Mark and Thomas]

Quoted 7 lines · Mark Brown
Quoted 4 lines · Milton Miller

I treated the arguments to irq_alloc_descs as having grown to accomidate the two uses having a common allocator with the partially redunant encoding. In one case an exact irq was specified (irq >= 0), and one that allocates from anywhere (irq < 0, all callers passed -1).

Quoted 5 lines
Quoted 1 line

Maybe you have a new case.

No, I'm only aware of those two cases. All my change does is make the irq parameter be enough to select between the two - at the minute it's just too weak.

Actually I do consider this a new case. You are implementing some kind
of cascade, and your code does not know (or want to care) if it can use any irq or needs a platform selected irq block. The decision of "Is this system fully enabled for dynamic irq selection?" is not in the code but in some externally supplied data.

Quoted 7 lines
Quoted 4 lines

Do you need a specific irq or an allocated one?

Or do you have a case where you don't know?

I need either a specific IRQ or an allocated one. This is just a very standard driver with an interrupt controller

I don't know that I'd call an interrupt controller a standard driver today, but agree we are heading towards that.

Quoted 5 lines
Quoted 1 line

Do you need a block of 60? or just 60 somewhere?

The driver assumes it's going to get a contiguous range, it'd be a lot of bookkeeping for no gain to have to cope with them being splattered all over the place.

The irq domain concept addresses mapping, but a simple fully allocated block will be one of the options. Having 60 irqdesc might be pushing the limit for some platforms but that can be addressed later when irq domains are upstream.

Quoted 4 lines
Quoted 1 line

How do you know from = 0 is safe?

If the user cares they can just pick a number for the base; if they're going to pick a number they may as well pick the actual number.

I'd argue the user is the wrong level to make this decision. However, saying the platform decides is valid, and the excerpt you had earlier implied you were getting the irq argument from platform data, not the user (eg via a module parameter)..

Also, read the last five paragraphs below.

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

I don't really see the relevance of this patch? You're adding functionality for limiting the maximum IRQ number allocated which seems orthogonal to the issue.

Its relavant in that irq_alloc_descs_range no longer gets both irq and from; the information is passed to the underling allocator in a different form.

That's not the goal of the patch, it's just something the patch happens to do as part of the implementation as far as I can see.

Well, eliminating the redundant information was a secondary goal.

Quoted 18 lines · Thomas Gleixner
Quoted 17 lines · Milton Miller
Quoted 15 lines · Mark Brown
Quoted 11 lines · Milton Miller
Quoted 1 line · Mark Brown

start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,

and then right after this the code continues:

        ret = -EEXIST;
        if (irq >=0 && start != irq)
                goto err;

This patch enables exactly the calls I want to forbid ! Why do

Which you wish to forbid because...? You've not articulated any motivation for doing this which makes it rather hard to engage here.

Quoted 6 lines
Quoted 3 lines

In 2.6.39 all calls to irq_alloc_descs were from the helpers. Either from irq_alloc_descriptor_at , which says "I need this exact irq", or from irq_alloc_desc, which says "give me any irq".

That does not prevent a caller with uses irq_alloc_descs() directly with the wrong arguments. So we want to sanity check this.

I wasn't debating a sanity check. I was claiming that the incoming argument combination was not sane and should be outright rejected instead of given new meaning and fixed up.

As I said earlier, I saw two valid use cases: the caller needed a fixed irq, and the caller didn't care about the irq. However, upon the further discussion, it does appear that Mark has a third case:
the driver can handle either; the choice is up to the platform.

With the further discussion, and some time to sleep and reflect on what the patch actually acomplishes and the use case, I withdraw my objection. The patch changes the semantics of the case irq >= 0 and from != irq, but it defines it in a meaningful way that removes the current insane combindation of events required for a successful allocation.

I should point out that several archtectures currently allocate irqs in a architecture layer before calling the irq allocator asking for the exact irq they choose on what they thought was free. Among these
are x86 and powerpc. Not calling the architecture layer will causein
all future allocations by other drivers using the architecture layer to fail..

That means that any driver allocating irqs directly is not generic if it actually allocates irqs unconditionally! Instead it will cause hotplug or even boot failures (if it allocates irqs too early).

I have a series in mind, inspired by Grant's initial proposal, to change the master allocation for powerpc (and export its helpers as irq domains), although I haven't been able to work on it for the last week or so.

An alternative, that can be implemented easily today, is to add the ability to specify from above what the architecture layer manages. (eg depend on SPARSE_IRQ and set from to NR_IRQS).

Looking at my other patch I can hide this in irq_alloc_descs helper as I already have a if ladder with two calls to irq_alloc_descs_range. Presently I don't intend to offer this ambigious feature directly in the _range call, but will support it through the wrapper for the existing functions.

Thanks for the discussion
milton

author
#6 lore raw
Quoted 7 lines · Milton Miller
Quoted 2 lines · Mark Brown

I need either a specific IRQ or an allocated one. This is just a very standard driver with an interrupt controller

I don't know that I'd call an interrupt controller a standard driver today, but agree we are heading towards that.

Pretty much any modern ARM system will have a PMIC with an interrupt controller in it.

Quoted 8 lines
Quoted 3 lines

The driver assumes it's going to get a contiguous range, it'd be a lot of bookkeeping for no gain to have to cope with them being splattered all over the place.

The irq domain concept addresses mapping, but a simple fully allocated block will be one of the options. Having 60 irqdesc might be pushing the limit for some platforms but that can be addressed later when irq domains are upstream.

I'm not really sure what an IRQ domain is (the patch series you pointed at before does rather appear to assume one knows already) but given that we're allocating from within a 32 bit type it seems like we should be using a few orders of magnitude more interrupts in a system than we do presently before this becomes much of an issue.

Quoted 7 lines
Quoted 2 lines

If the user cares they can just pick a number for the base; if they're going to pick a number they may as well pick the actual number.

I'd argue the user is the wrong level to make this decision. However, saying the platform decides is valid, and the excerpt you had earlier implied you were getting the irq argument from platform data, not the user (eg via a module parameter)..

It's platform data. Having interrupt numbers specified as module parameters strikes me as insane - the particular integers we map individual interrupts onto are very much an implementation decision of the kernel.

Quoted 6 lines

I should point out that several archtectures currently allocate irqs in a architecture layer before calling the irq allocator asking for the exact irq they choose on what they thought was free. Among these
are x86 and powerpc. Not calling the architecture layer will causein
all future allocations by other drivers using the architecture layer to fail..

That sounds like we should join the two IRQ allocators up with each other.

#7 lore raw
Quoted 16 lines · Mark Brown
Quoted 7 lines · Milton Miller
Quoted 2 lines · Mark Brown

I need either a specific IRQ or an allocated one.  This is just a very standard driver with an interrupt controller

I don't know that I'd call an interrupt controller a standard driver today, but agree we are heading towards that.

Pretty much any modern ARM system will have a PMIC with an interrupt controller in it.

Quoted 3 lines
Quoted 3 lines

The driver assumes it's going to get a contiguous range, it'd be a lot of bookkeeping for no gain to have to cope with them being splattered all over the place.

Quoted 7 lines
Quoted 4 lines

The irq domain concept addresses mapping, but a simple fully allocated block will be one of the options.  Having 60 irqdesc might be pushing the limit for some platforms but that can be addressed later when irq domains are upstream.

I'm not really sure what an IRQ domain is (the patch series you pointed at before does rather appear to assume one knows already)

(obviously I need to write some documentation). irq_domains is some infrastructure to make managing a range of irq numbers easier, and also to handle the mapping of hw irq numbers to linux irq numbers.

Quoted 4 lines

but given that
we're allocating from within a 32 bit type it seems like we should be using a few orders of magnitude more interrupts in a system than we do presently before this becomes much of an issue.

Also, with sparse irqs, irq numbers are effectively free. It may be valuable to implement deferred allocation of the actual irq_desc structures, but ranges of irq numbers are really cheap for discrete irq controllers (say in the range of 1-256 irq inputs). It may be different for MSI controllers that have programmable hw irq numbers, but in that case the solution is to allocated them one at a time as needed, or allocate a pool that the MSI controller driver can manage on its own.

Quoted 9 lines
Quoted 8 lines
Quoted 3 lines

If the user cares they can just pick a number for the base; if they're going to pick a number they may as well pick the actual number.

I'd argue the user is the wrong level to make this decision.  However, saying the platform decides is valid, and the excerpt you had earlier implied you were getting the irq argument from platform data, not the user (eg via a module parameter)..

Quoted 4 lines

It's platform data.  Having interrupt numbers specified as module parameters strikes me as insane - the particular integers we map individual interrupts onto are very much an implementation decision of the kernel.

More and more irq numbers are becoming internal implementation details of the kernel. We should be moving in the direction of irq numbers have less to no impact on anything in userspace or the bootloader. There probably isn't much to be done about /proc/interrupts, but it definitely sounds insane to specify irq numbers as module parameters. or in the device tree. or anything else not linked directly to the kernel.

Quoted 6 lines
Quoted 6 lines

I should point out that several archtectures currently allocate irqs in a architecture layer before calling the irq allocator asking for the exact irq they choose on what they thought was free.  Among these are x86 and powerpc.   Not calling the architecture layer will causein all future allocations by other drivers using the architecture layer to fail..

Yes they do, but no it should not stay that way. The architectures currently do this because there wasn't common code to handle it sanely. Now that irq_alloc_desc*() does exist, the arch specific allocation of irqs needs to be dropped and the architecture code should rely entirely on irq_alloc_desc*() for managing ranges of irqs.
It does not make sense for drivers needing to allocating irq_descs
(gpio controllers are an excellent example) to call into arch code. irq_alloc_desc*() is more than sufficient. Milton, later in your email you mention changing the way powerpc allocates irqs. Is this what you were talking about?

Quoted 3 lines

That sounds like we should join the two IRQ allocators up with each other.

yes.

author
#8 lore raw
Quoted 8 lines · Grant Likely

On Sat, Jun 4, 2011 at 3:51 AM, Mark Brown

Quoted 2 lines

I'm not really sure what an IRQ domain is (the patch series you pointed at before does rather appear to assume one knows already)

(obviously I need to write some documentation). irq_domains is some infrastructure to make managing a range of irq numbers easier, and also to handle the mapping of hw irq numbers to linux irq numbers.

Hrm, in that case it sounds rather like a lot of interrupt controllers ought to be making themselves IRQ domains?

#9 lore raw
Quoted 6 lines · Mark Brown
Quoted 4 lines · Grant Likely

On Sat, Jun 4, 2011 at 3:51 AM, Mark Brown

Quoted 2 lines

I'm not really sure what an IRQ domain is (the patch series you pointed at before does rather appear to assume one knows already)

Quoted 6 lines
Quoted 3 lines

(obviously I need to write some documentation).  irq_domains is some infrastructure to make managing a range of irq numbers easier, and also to handle the mapping of hw irq numbers to linux irq numbers.

Hrm, in that case it sounds rather like a lot of interrupt controllers ought to be making themselves IRQ domains?

yup, and I'll probably craft patches for RFC to do exactly that when I've got irq_domains sorted out.

g.

#10 lore raw
In reply to Milton Miller
Quoted 12 lines · Milton Miller
Quoted 11 lines · Mark Brown
Quoted 9 lines · Milton Miller
Quoted 1 line · Mark Brown

start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,

and then right after this the code continues:

        ret = -EEXIST;
        if (irq >=0 && start != irq)
                goto err;
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

This patch enables exactly the calls I want to forbid ! Why do

Which you wish to forbid because...? You've not articulated any motivation for doing this which makes it rather hard to engage here.

In 2.6.39 all calls to irq_alloc_descs were from the helpers. Either from irq_alloc_descriptor_at , which says "I need this exact irq", or from irq_alloc_desc, which says "give me any irq".

That does not prevent a caller with uses irq_alloc_descs() directly with the wrong arguments. So we want to sanity check this.

Thanks,

tglx

#11 lore raw
In reply to Mark Brown
Commit-ID:  c5182b8867e189e14a8df5dbfcba1c73f286e061
Gitweb:     http://git.kernel.org/tip/c5182b8867e189e14a8df5dbfcba1c73f286e061
Author:     Mark Brown <[email protected]>
AuthorDate: Thu, 2 Jun 2011 18:55:13 +0100
Committer:  Thomas Gleixner <[email protected]>
CommitDate: Fri, 3 Jun 2011 14:53:16 +0200

genirq: Ensure we locate the passed IRQ in irq_alloc_descs()

When irq_alloc_descs() is called with no base IRQ specified then it will search for a range of IRQs starting from a specified base address. In the case where an IRQ is specified it still does this search in order to ensure that none of the requested range is already allocated and it still uses the from parameter to specify the base for the search. This means that in the case where a base is specified but from is zero (which is reasonable as any IRQ number is in the range specified by a zero from) the function will get confused and try to allocate the first suitably sized block of free IRQs it finds.

Instead use a specified IRQ as the base address for the search, and insist that any from that is specified can support that IRQ.

---
 kernel/irq/irqdesc.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
kernel/irq/irqdesc.c +6 -0
index c7ddc87..4c60a50 100644
@@ -344,6 +344,12 @@ irq_alloc_descs(int irq, unsigned int from, unsigned int cnt, int node)
 	if (!cnt)
 		return -EINVAL;
 
+	if (irq >= 0) {
+		if (from > irq)
+			return -EINVAL;
+		from = irq;
+	}
+
 	mutex_lock(&sparse_irq_lock);
 
 	start = bitmap_find_next_zero_area(allocated_irqs, IRQ_BITMAP_BITS,