genirq fixes for 2.6.31
Linus,
Please pull the latest irq-fixes-for-linus git tree from:
git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip.git irq-fixes-for-linus
Thanks,
tglx
------------------>
Thomas Gleixner (1):
genirq: Prevent race between free_irq() and handle_IRQ_event()
kernel/irq/handle.c | 10 +++++++++- 1 files changed, 9 insertions(+), 1 deletions(-)
kernel/irq/handle.c +9 -1
@@ -403,8 +403,16 @@ irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)*/ if (likely (!test_bit (IRQTF_DIED , &action->thread_flags))) { +struct task_struct *tsk = action->thread; +set_bit (IRQTF_RUNTHREAD , &action->thread_flags); -wake_up_process (action->thread); +/* +* Check tsk as we might race against +* free_irq which sets action->thread +* to NULL +*/ +if (tsk) +wake_up_process (tsk); }/* Fall through to add to randomness */
Quoted 4 lines · Thomas Gleixner
Please pull the latest irq-fixes-for-linus git tree from:
git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip.gitirq-fixes-for-linus
Not without lots more explanations - or at least _fixed_ explanations.
Quoted 16 lines
Thomas Gleixner (1):
genirq: Prevent race betweenfree_irq()andhandle_IRQ_event()kernel/irq/handle.c | 10 +++++++++- 1 files changed, 9 insertions(+), 1 deletions(-)kernel/irq/handle.c +1
-0@@ -403,8 +403,16 @@ irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)*/ if (likely (!test_bit (IRQTF_DIED , &action->thread_flags))) { +struct task_struct *tsk = action->thread;
kernel/irq/handle.c irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action) · line 415
Quoted 10 lines
kernel/irq/handle.c +8
-1+set_bit (IRQTF_RUNTHREAD , &action->thread_flags); -wake_up_process (action->thread); +/* +* Check tsk as we might race against +* free_irq which sets action->thread +* to NULL +*/ +if (tsk) +wake_up_process (tsk);
Seriously, this looks entirely bogus.
The thing is, if "action" has been free'd, then dammit, 'tsk' is gone too. In fact, just look at _free_irq(), and realise how it does the
if (irqthread) {
if (!test_bit(IRQTF_DIED, &action->thread_flags))
kthread_stop(irqthread);
put_task_struct(irqthread);
}_before_ it free's 'action'. So what does that fix?
There is no race that I can see - we're holding "action->lock" while all this happens.
Now, I can see a bug, which is that "action->tsk" may have been set to NULL. But I can't see a race, and I can't see a reason for all the code movement. So quite frankly, I think the comments (both in the code and in the commit message) are just wrong. And the odd "load it first, then do other things" code looks confused.
So why is this not just a
if (action->thread) wake_up_process(action->thread);
with appropriate comments?
Or, alternatively, just move all the "clear action->thread" in free_irq() to after having done the "synchronize_irq()" thing, and then - afaik - you'll not need that test at all, because you're guaranteed that as long as you're in an interrupt handler, the thing shouldn't be cleared.
No?
Linus
Quoted 6 lines · Linus Torvalds
Now, I can see a bug, which is that "
action->tsk" may have been set to NULL. But I can't see a race, and I can't see a reason for all the code movement. So quite frankly, I think the comments (both in the code and in the commit message) are just wrong. And the odd "load it first, then do other things" code looks confused.
Quoted 6 lines
So why is this not just a
if (action->thread) wake_up_process(action->thread);with appropriate comments?
What guarantees that the compiler does not dereference action->thread twice and the action->thread = NULL; operation happens between the check and the wake_up_process() call? I might be paranoid, but ...
Quoted 4 lines
Or, alternatively, just move all the "clear
action->thread" infree_irq()to after having done the "synchronize_irq()" thing, and then - afaik - you'll not need that test at all, because you're guaranteed that as long as you're in an interrupt handler, the thing shouldn't be cleared.
Right, I looked at that as well, but we need to do it different than just calling synchronize_irq(), as we need to keep desc->lock after we established that no interrupt is in progress. Otherwise we can run into the same problem which we have right now. Patch below.
Thanks,
tglx
---
kernel/irq/manage.c +22 -1
@@ -895,7 +907,28 @@ static struct irqaction *__free_irq(unsigned int irq, void *dev_id)if (!desc)return NULL ; -atomic_spin_lock_irqsave (&desc->lock, flags); +while (1 ) { +/* +* Wait until we're out of the critical section. This might +* give the wrong answer due to the lack of memory barriers. +*/ +while (desc->status &IRQ_INPROGRESS ) +cpu_relax (); + +/* +* Check under the lock again. If irq is not in +* progress we keep the lock held until we removed +* action. We do not care about an already running irq +* thread here. We care about it when we stop the thread. +*/ +atomic_spin_lock_irqsave (&desc->lock, flags); + +if (!(desc->status &IRQ_INPROGRESS )) +break ; + +/* Try again */ +atomic_spin_unlock_irqrestore (&desc->lock, flags); + }/* * There can be multiple actions per IRQ descriptor, find the right
Sorry, sent out the wrong patch :(
Quoted 23 lines · Thomas Gleixner
Quoted 6 lines · Linus Torvalds
Now, I can see a bug, which is that "
action->tsk" may have been set to NULL. But I can't see a race, and I can't see a reason for all the code movement. So quite frankly, I think the comments (both in the code and in the commit message) are just wrong. And the odd "load it first, then do other things" code looks confused.Quoted 6 lines
So why is this not just a
if (action->thread) wake_up_process(action->thread);with appropriate comments?
What guarantees that the compiler does not dereference
action->threadtwice and theaction->thread= NULL; operation happens between the check and thewake_up_process()call? I might be paranoid, but ...Quoted 4 lines
Or, alternatively, just move all the "clear
action->thread" infree_irq()to after having done the "synchronize_irq()" thing, and then - afaik - you'll not need that test at all, because you're guaranteed that as long as you're in an interrupt handler, the thing shouldn't be cleared.
Quoted 8 lines
Right, I looked at that as well, but we need to do it different than just calling
synchronize_irq(), as we need to keepdesc->lockafter we established that no interrupt is in progress. Otherwise we can run into the same problem which we have right now. Patch below.Thanks,
tglx
kernel/irq/manage.c +22 -1
@@ -895,7 +907,28 @@ static struct irqaction *__free_irq(unsigned int irq, void *dev_id)if (!desc)return NULL ; -spin_lock_irqsave (&desc->lock, flags); +while (1 ) { +/* +* Wait until we're out of the critical section. This might +* give the wrong answer due to the lack of memory barriers. +*/ +while (desc->status &IRQ_INPROGRESS ) +cpu_relax (); + +/* +* Check under the lock again. If irq is not in +* progress we keep the lock held until we removed +* action. We do not care about an already running irq +* thread here. We care about it when we stop the thread. +*/ +spin_lock_irqsave (&desc->lock, flags); + +if (!(desc->status &IRQ_INPROGRESS )) +break ; + +/* Try again */ +spin_unlock_irqrestore (&desc->lock, flags); + }/* * There can be multiple actions per IRQ descriptor, find the right
Quoted 9 lines · Thomas Gleixner
Quoted 8 lines · Linus Torvalds
Now, I can see a bug, which is that "
action->tsk" may have been set to NULL. But I can't see a race, and I can't see a reason for all the code movement. So quite frankly, I think the comments (both in the code and in the commit message) are just wrong. And the odd "load it first, then do other things" code looks confused.So why is this not just a
Quoted 8 lines
Quoted 4 lines
if (action->thread) wake_up_process(action->thread);with appropriate comments?
What guarantees that the compiler does not dereference
action->threadtwice and theaction->thread= NULL; operation happens between the check and thewake_up_process()call? I might be paranoid, but ...
Aren't we holding the lock here?
And if we are _not_ holding the lock, then it's racy anyway, and the right fix is the other one I suggested:
Quoted 9 lines
Quoted 4 lines
Or, alternatively, just move all the "clear
action->thread" infree_irq()to after having done the "synchronize_irq()" thing, and then - afaik - you'll not need that test at all, because you're guaranteed that as long as you're in an interrupt handler, the thing shouldn't be cleared.Right, I looked at that as well, but we need to do it different than just calling
synchronize_irq(), as we need to keepdesc->lockafter we established that no interrupt is in progress. Otherwise we can run into the same problem which we have right now. Patch below.
But we already _do_ call synchronize_irq().
And no, we'd better not be running into the same problem, becaue dang it, if we do, then 'action' itself is unreliable (since we'll be doing a 'kfree()' in it in free_irq())
IOW, why not just make the patch do something like the appended?
NOTE! This is UNTESTED. And I also - on purpose - removed the "set action->thread to NULL", because we're going to free 'action', so if anything depends on it, it's already buggy.
What am I missing?
Linus
--- kernel/irq/manage.c | 17 ++++++++--------- 1 files changed, 8 insertions(+), 9 deletions(-)
kernel/irq/manage.c +8 -9
@@ -809,9 +809,6 @@ static struct irqaction *__free_irq(unsigned int irq, void *dev_id) desc->chip->disable (irq); } - irqthread = action->thread; - action->thread =NULL ; -spin_unlock_irqrestore (&desc->lock, flags);unregister_handler_proc (irq, action);
@@ -819,12 +816,6 @@ static struct irqaction *__free_irq(unsigned int irq, void *dev_id)/* Make sure it's not being used on another CPU: */ synchronize_irq (irq); -if (irqthread) { -if (!test_bit (IRQTF_DIED , &action->thread_flags)) -kthread_stop (irqthread); -put_task_struct (irqthread); - } -#ifdef CONFIG_DEBUG_SHIRQ /* * It's a shared IRQ -- the driver ought to be prepared for an IRQ
@@ -840,6 +831,14 @@ static struct irqaction *__free_irq(unsigned int irq, void *dev_id) local_irq_restore(flags); }#endif + + irqthread = action->thread; +if (irqthread) { +if (!test_bit (IRQTF_DIED , &action->thread_flags)) +kthread_stop (irqthread); +put_task_struct (irqthread); + } +return action; }
Quoted 6 lines · Linus Torvalds
Quoted 3 lines · Thomas Gleixner
What guarantees that the compiler does not dereference
action->threadtwice and theaction->thread= NULL; operation happens between the check and thewake_up_process()call? I might be paranoid, but ...Aren't we holding the lock here?
No, we don't. The lock is dropped before handle_IRQ_event() is called.
Quoted 13 lines
And if we are _not_ holding the lock, then it's racy anyway, and the right fix is the other one I suggested:
Quoted 9 lines
Quoted 4 lines
Or, alternatively, just move all the "clear
action->thread" infree_irq()to after having done the "synchronize_irq()" thing, and then - afaik - you'll not need that test at all, because you're guaranteed that as long as you're in an interrupt handler, the thing shouldn't be cleared.Right, I looked at that as well, but we need to do it different than just calling
synchronize_irq(), as we need to keepdesc->lockafter we established that no interrupt is in progress. Otherwise we can run into the same problem which we have right now. Patch below.
Quoted 5 lines
But we already _do_ call
synchronize_irq().And no, we'd better not be running into the same problem, becaue dang it, if we do, then 'action' itself is unreliable (since we'll be doing a '
kfree()' in it infree_irq())
action->thread is the thing which became unreliable due to setting it to NULL. Yes, I did not think about the fact that we can remove the action while the interrupt is in progress on another CPU. So setting action->thread to NULL _before_ calling synchronize_irq() is the cause for the oops which has been reported.
Quoted 5 lines
IOW, why not just make the patch do something like the appended?
NOTE! This is UNTESTED. And I also - on purpose - removed the "set
action->threadto NULL", because we're going to free 'action', so if anything depends on it, it's already buggy.
Works fine with my test case.
Quoted 1 line
What am I missing?
Nothing, as far as I can tell.
Acked-by-me.
tglx
Quoted 48 lines
--- kernel/irq/manage.c | 17 ++++++++--------- 1 files changed, 8 insertions(+), 9 deletions(-)kernel/irq/manage.c +8
-9@@ -809,9 +809,6 @@ static struct irqaction *__free_irq(unsigned int irq, void *dev_id) desc->chip->disable (irq); } - irqthread = action->thread; - action->thread =NULL ; -spin_unlock_irqrestore (&desc->lock, flags);unregister_handler_proc (irq, action);@@ -819,12 +816,6 @@ static struct irqaction *__free_irq(unsigned int irq, void *dev_id)/* Make sure it's not being used on another CPU: */ synchronize_irq (irq); -if (irqthread) { -if (!test_bit (IRQTF_DIED , &action->thread_flags)) -kthread_stop (irqthread); -put_task_struct (irqthread); - } -#ifdef CONFIG_DEBUG_SHIRQ /* * It's a shared IRQ -- the driver ought to be prepared for an IRQ @@ -840,6 +831,14 @@ static struct irqaction *__free_irq(unsigned int irq, void *dev_id) local_irq_restore(flags); }#endif + + irqthread = action->thread; +if (irqthread) { +if (!test_bit (IRQTF_DIED , &action->thread_flags)) +kthread_stop (irqthread); +put_task_struct (irqthread); + } +return action; }