Skip to content

Commit cd7eab4

Browse files
Ben HutchingsKAGA-KOKO
authored andcommitted
genirq: Add IRQ affinity notifiers
When initiating I/O on a multiqueue and multi-IRQ device, we may want to select a queue for which the response will be handled on the same or a nearby CPU. This requires a reverse-map of IRQ affinity. Add a notification mechanism to support this. This is based closely on work by Thomas Gleixner <tglx@linutronix.de>. Signed-off-by: Ben Hutchings <bhutchings@solarflare.com> Cc: linux-net-drivers@solarflare.com Cc: Tom Herbert <therbert@google.com> Cc: David Miller <davem@davemloft.net> LKML-Reference: <1295470904.11126.84.camel@bwh-desktop> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
1 parent 1bae4ce commit cd7eab4

3 files changed

Lines changed: 117 additions & 1 deletion

File tree

‎include/linux/interrupt.h‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <linux/smp.h>
1515
#include <linux/percpu.h>
1616
#include <linux/hrtimer.h>
17+
#include <linux/kref.h>
18+
#include <linux/workqueue.h>
1719

1820
#include <asm/atomic.h>
1921
#include <asm/ptrace.h>
@@ -240,6 +242,35 @@ extern int irq_can_set_affinity(unsigned int irq);
240242
extern int irq_select_affinity(unsigned int irq);
241243

242244
extern int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m);
245+
246+
/**
247+
* struct irq_affinity_notify - context for notification of IRQ affinity changes
248+
* @irq: Interrupt to which notification applies
249+
* @kref: Reference count, for internal use
250+
* @work: Work item, for internal use
251+
* @notify: Function to be called on change. This will be
252+
* called in process context.
253+
* @release: Function to be called on release. This will be
254+
* called in process context. Once registered, the
255+
* structure must only be freed when this function is
256+
* called or later.
257+
*/
258+
struct irq_affinity_notify {
259+
unsigned int irq;
260+
struct kref kref;
261+
struct work_struct work;
262+
void (*notify)(struct irq_affinity_notify *, const cpumask_t *mask);
263+
void (*release)(struct kref *ref);
264+
};
265+
266+
extern int
267+
irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify);
268+
269+
static inline void irq_run_affinity_notifiers(void)
270+
{
271+
flush_scheduled_work();
272+
}
273+
243274
#else /* CONFIG_SMP */
244275

245276
static inline int irq_set_affinity(unsigned int irq, const struct cpumask *m)
@@ -255,7 +286,7 @@ static inline int irq_can_set_affinity(unsigned int irq)
255286
static inline int irq_select_affinity(unsigned int irq) { return 0; }
256287

257288
static inline int irq_set_affinity_hint(unsigned int irq,
258-
const struct cpumask *m)
289+
const struct cpumask *m)
259290
{
260291
return -EINVAL;
261292
}

‎include/linux/irqdesc.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* For now it's included from <linux/irq.h>
99
*/
1010

11+
struct irq_affinity_notify;
1112
struct proc_dir_entry;
1213
struct timer_rand_state;
1314
/**
@@ -24,6 +25,7 @@ struct timer_rand_state;
2425
* @last_unhandled: aging timer for unhandled count
2526
* @irqs_unhandled: stats field for spurious unhandled interrupts
2627
* @lock: locking for SMP
28+
* @affinity_notify: context for notification of affinity changes
2729
* @pending_mask: pending rebalanced interrupts
2830
* @threads_active: number of irqaction threads currently running
2931
* @wait_for_threads: wait queue for sync_irq to wait for threaded handlers
@@ -70,6 +72,7 @@ struct irq_desc {
7072
raw_spinlock_t lock;
7173
#ifdef CONFIG_SMP
7274
const struct cpumask *affinity_hint;
75+
struct irq_affinity_notify *affinity_notify;
7376
#ifdef CONFIG_GENERIC_PENDING_IRQ
7477
cpumask_var_t pending_mask;
7578
#endif

‎kernel/irq/manage.c‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ int irq_set_affinity(unsigned int irq, const struct cpumask *cpumask)
134134
irq_set_thread_affinity(desc);
135135
}
136136
#endif
137+
if (desc->affinity_notify) {
138+
kref_get(&desc->affinity_notify->kref);
139+
schedule_work(&desc->affinity_notify->work);
140+
}
137141
desc->status |= IRQ_AFFINITY_SET;
138142
raw_spin_unlock_irqrestore(&desc->lock, flags);
139143
return 0;
@@ -155,6 +159,79 @@ int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
155159
}
156160
EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
157161

162+
static void irq_affinity_notify(struct work_struct *work)
163+
{
164+
struct irq_affinity_notify *notify =
165+
container_of(work, struct irq_affinity_notify, work);
166+
struct irq_desc *desc = irq_to_desc(notify->irq);
167+
cpumask_var_t cpumask;
168+
unsigned long flags;
169+
170+
if (!desc)
171+
goto out;
172+
173+
if (!alloc_cpumask_var(&cpumask, GFP_KERNEL))
174+
goto out;
175+
176+
raw_spin_lock_irqsave(&desc->lock, flags);
177+
#ifdef CONFIG_GENERIC_PENDING_IRQ
178+
if (desc->status & IRQ_MOVE_PENDING)
179+
cpumask_copy(cpumask, desc->pending_mask);
180+
else
181+
#endif
182+
cpumask_copy(cpumask, desc->affinity);
183+
raw_spin_unlock_irqrestore(&desc->lock, flags);
184+
185+
notify->notify(notify, cpumask);
186+
187+
free_cpumask_var(cpumask);
188+
out:
189+
kref_put(&notify->kref, notify->release);
190+
}
191+
192+
/**
193+
* irq_set_affinity_notifier - control notification of IRQ affinity changes
194+
* @irq: Interrupt for which to enable/disable notification
195+
* @notify: Context for notification, or %NULL to disable
196+
* notification. Function pointers must be initialised;
197+
* the other fields will be initialised by this function.
198+
*
199+
* Must be called in process context. Notification may only be enabled
200+
* after the IRQ is allocated and must be disabled before the IRQ is
201+
* freed using free_irq().
202+
*/
203+
int
204+
irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
205+
{
206+
struct irq_desc *desc = irq_to_desc(irq);
207+
struct irq_affinity_notify *old_notify;
208+
unsigned long flags;
209+
210+
/* The release function is promised process context */
211+
might_sleep();
212+
213+
if (!desc)
214+
return -EINVAL;
215+
216+
/* Complete initialisation of *notify */
217+
if (notify) {
218+
notify->irq = irq;
219+
kref_init(&notify->kref);
220+
INIT_WORK(&notify->work, irq_affinity_notify);
221+
}
222+
223+
raw_spin_lock_irqsave(&desc->lock, flags);
224+
old_notify = desc->affinity_notify;
225+
desc->affinity_notify = notify;
226+
raw_spin_unlock_irqrestore(&desc->lock, flags);
227+
228+
if (old_notify)
229+
kref_put(&old_notify->kref, old_notify->release);
230+
231+
return 0;
232+
}
233+
EXPORT_SYMBOL_GPL(irq_set_affinity_notifier);
234+
158235
#ifndef CONFIG_AUTO_IRQ_AFFINITY
159236
/*
160237
* Generic version of the affinity autoselector.
@@ -1004,6 +1081,11 @@ void free_irq(unsigned int irq, void *dev_id)
10041081
if (!desc)
10051082
return;
10061083

1084+
#ifdef CONFIG_SMP
1085+
if (WARN_ON(desc->affinity_notify))
1086+
desc->affinity_notify = NULL;
1087+
#endif
1088+
10071089
chip_bus_lock(desc);
10081090
kfree(__free_irq(irq, dev_id));
10091091
chip_bus_sync_unlock(desc);

0 commit comments

Comments
 (0)