Skip to content

Commit 591d2fb

Browse files
committed
genirq: Delegate irq affinity setting to the irq thread
irq_set_thread_affinity() calls set_cpus_allowed_ptr() which might sleep, but irq_set_thread_affinity() is called with desc->lock held and can be called from hard interrupt context as well. The code has another bug as it does not hold a ref on the task struct as required by set_cpus_allowed_ptr(). Just set the IRQTF_AFFINITY bit in action->thread_flags. The next time the thread runs it migrates itself. Solves all of the above problems nicely. Add kerneldoc to irq_set_thread_affinity() while at it. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> LKML-Reference: <new-submission>
1 parent aea1f79 commit 591d2fb

4 files changed

Lines changed: 48 additions & 9 deletions

File tree

‎include/linux/interrupt.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,13 @@
6464
* IRQTF_RUNTHREAD - signals that the interrupt handler thread should run
6565
* IRQTF_DIED - handler thread died
6666
* IRQTF_WARNED - warning "IRQ_WAKE_THREAD w/o thread_fn" has been printed
67+
* IRQTF_AFFINITY - irq thread is requested to adjust affinity
6768
*/
6869
enum {
6970
IRQTF_RUNTHREAD,
7071
IRQTF_DIED,
7172
IRQTF_WARNED,
73+
IRQTF_AFFINITY,
7274
};
7375

7476
typedef irqreturn_t (*irq_handler_t)(int, void *);

‎kernel/irq/internals.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ static inline void unregister_handler_proc(unsigned int irq,
4242

4343
extern int irq_select_affinity_usr(unsigned int irq);
4444

45-
extern void
46-
irq_set_thread_affinity(struct irq_desc *desc, const struct cpumask *cpumask);
45+
extern void irq_set_thread_affinity(struct irq_desc *desc);
4746

4847
/*
4948
* Debugging printout:

‎kernel/irq/manage.c‎

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,22 @@ int irq_can_set_affinity(unsigned int irq)
8080
return 1;
8181
}
8282

83-
void
84-
irq_set_thread_affinity(struct irq_desc *desc, const struct cpumask *cpumask)
83+
/**
84+
* irq_set_thread_affinity - Notify irq threads to adjust affinity
85+
* @desc: irq descriptor which has affitnity changed
86+
*
87+
* We just set IRQTF_AFFINITY and delegate the affinity setting
88+
* to the interrupt thread itself. We can not call
89+
* set_cpus_allowed_ptr() here as we hold desc->lock and this
90+
* code can be called from hard interrupt context.
91+
*/
92+
void irq_set_thread_affinity(struct irq_desc *desc)
8593
{
8694
struct irqaction *action = desc->action;
8795

8896
while (action) {
8997
if (action->thread)
90-
set_cpus_allowed_ptr(action->thread, cpumask);
98+
set_bit(IRQTF_AFFINITY, &action->thread_flags);
9199
action = action->next;
92100
}
93101
}
@@ -112,7 +120,7 @@ int irq_set_affinity(unsigned int irq, const struct cpumask *cpumask)
112120
if (desc->status & IRQ_MOVE_PCNTXT) {
113121
if (!desc->chip->set_affinity(irq, cpumask)) {
114122
cpumask_copy(desc->affinity, cpumask);
115-
irq_set_thread_affinity(desc, cpumask);
123+
irq_set_thread_affinity(desc);
116124
}
117125
}
118126
else {
@@ -122,7 +130,7 @@ int irq_set_affinity(unsigned int irq, const struct cpumask *cpumask)
122130
#else
123131
if (!desc->chip->set_affinity(irq, cpumask)) {
124132
cpumask_copy(desc->affinity, cpumask);
125-
irq_set_thread_affinity(desc, cpumask);
133+
irq_set_thread_affinity(desc);
126134
}
127135
#endif
128136
desc->status |= IRQ_AFFINITY_SET;
@@ -176,7 +184,7 @@ int irq_select_affinity_usr(unsigned int irq)
176184
spin_lock_irqsave(&desc->lock, flags);
177185
ret = setup_affinity(irq, desc);
178186
if (!ret)
179-
irq_set_thread_affinity(desc, desc->affinity);
187+
irq_set_thread_affinity(desc);
180188
spin_unlock_irqrestore(&desc->lock, flags);
181189

182190
return ret;
@@ -443,6 +451,34 @@ static int irq_wait_for_interrupt(struct irqaction *action)
443451
return -1;
444452
}
445453

454+
/*
455+
* Check whether we need to change the affinity of the interrupt thread.
456+
*/
457+
static void
458+
irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
459+
{
460+
cpumask_var_t mask;
461+
462+
if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags))
463+
return;
464+
465+
/*
466+
* In case we are out of memory we set IRQTF_AFFINITY again and
467+
* try again next time
468+
*/
469+
if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
470+
set_bit(IRQTF_AFFINITY, &action->thread_flags);
471+
return;
472+
}
473+
474+
spin_lock_irq(&desc->lock);
475+
cpumask_copy(mask, desc->affinity);
476+
spin_unlock_irq(&desc->lock);
477+
478+
set_cpus_allowed_ptr(current, mask);
479+
free_cpumask_var(mask);
480+
}
481+
446482
/*
447483
* Interrupt handler thread
448484
*/
@@ -458,6 +494,8 @@ static int irq_thread(void *data)
458494

459495
while (!irq_wait_for_interrupt(action)) {
460496

497+
irq_thread_check_affinity(desc, action);
498+
461499
atomic_inc(&desc->threads_active);
462500

463501
spin_lock_irq(&desc->lock);

‎kernel/irq/migration.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void move_masked_irq(int irq)
4545
< nr_cpu_ids))
4646
if (!desc->chip->set_affinity(irq, desc->pending_mask)) {
4747
cpumask_copy(desc->affinity, desc->pending_mask);
48-
irq_set_thread_affinity(desc, desc->pending_mask);
48+
irq_set_thread_affinity(desc);
4949
}
5050

5151
cpumask_clear(desc->pending_mask);

0 commit comments

Comments
 (0)