irq: Honor /proc/irq core affinity when an IRQ thread is created
RFC PATCH 1/1 irq: Honor /proc/irq core affinity when an IRQ thread is created
Hi everyone,
I would like to get some feedback from the RT community before submitting to the mainline.
Currently, by writing to /proc/irq/../smp_affinity file, core affinity of already running IRQ threads can be modified. However, after writing to the /proc file, an IRQ thread that gets created later does not inherit the affinity specified in the file. This happens because the irq action is registered in irq description only after setup_affinity function is called. This patch addresses this problem by calling setup_affinity function after the irq action is updated in the irq description structure. It looks like the existing behavior (without this patch) is inconsistent because in the case of multiple action handlers for an irq, the IRQ thread associated with the new action handler that is being registered does not inherit the core affinity from the /proc file, however, the IRQ threads of the existing action handlers do inherit the affinity.
kernel/irq/manage.c +0 -0
100644
kernel/irq/manage.c +0 -0
@@ -1083,9 +1083,6 @@ __setup_irq(unsigned int irq, struct irq_desc
*desc, struct irqaction *new)
if (new->flags & IRQF_NO_SOFTIRQ_CALL)
irq_settings_set_no_softirq_call(desc);- /* Set default affinity mask once everything is setup */
- setup_affinity(irq, desc, mask);
Quoted 5 lines · Sankara Muthukrishnan
Hi everyone,
I would like to get some feedback from the RT community before submitting to the mainline.
Quoted 13 lines
Currently, by writing to
/proc/irq/../smp_affinityfile, core affinity of already running IRQ threads can be modified. However, after writing to the /proc file, an IRQ thread that gets created later does not inherit the affinity specified in the file. This happens because the irq action is registered in irq description only aftersetup_affinityfunction is called. This patch addresses this problem by callingsetup_affinityfunction after the irq action is updated in the irq description structure. It looks like the existing behavior (without this patch) is inconsistent because in the case of multiple action handlers for an irq, the IRQ thread associated with the new action handler that is being registered does not inherit the core affinity from the /proc file, however, the IRQ threads of the existing action handlers do inherit the affinity.
I understand the problem, but the proposed patch is not going to solve it. setup_affinity() is just called in the !shared case, which is the first action which is set up. So that works for the first irq, but not for later ones.
Does the following patch fix the problem for both shared and non shared interrupts ?
Thanks,
tglx
---
kernel/irq/manage.c +1 -0
@@ -936,6 +936,7 @@ __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)*/ get_task_struct (t); new->thread = t; +set_bit (IRQTF_AFFINITY , &new->thread_flags); }if (!alloc_cpumask_var (&mask,GFP_KERNEL )) {
Quoted 22 lines · Thomas Gleixner
Quoted 5 lines · Sankara Muthukrishnan
Hi everyone,
I would like to get some feedback from the RT community before submitting to the mainline.
Quoted 13 lines
Currently, by writing to
/proc/irq/../smp_affinityfile, core affinity of already running IRQ threads can be modified. However, after writing to the /proc file, an IRQ thread that gets created later does not inherit the affinity specified in the file. This happens because the irq action is registered in irq description only aftersetup_affinityfunction is called. This patch addresses this problem by callingsetup_affinityfunction after the irq action is updated in the irq description structure. It looks like the existing behavior (without this patch) is inconsistent because in the case of multiple action handlers for an irq, the IRQ thread associated with the new action handler that is being registered does not inherit the core affinity from the /proc file, however, the IRQ threads of the existing action handlers do inherit the affinity.
Quoted 7 lines
I understand the problem, but the proposed patch is not going to solve it.
setup_affinity()is just called in the !shared case, which is the first action which is set up. So that works for the first irq, but not for later ones.Does the following patch fix the problem for both shared and non shared interrupts ?
Thanks Thomas. Your suggestion works for non-shared. I do not have devices sharing interrupts on my board to quickly verify. However, I hacked a kernel module to simulate shared irq's and the kernel source (to set the affinity before waiting for interrupts in the irq thread) and verified that it works for "shared interrupts" also.
However, I was wondering whether we are relaxing some checks that setup_affinity does (like checking for flags such as IRQD_NO_BALANCING) by setting the flag IRQTF_AFFINITY after creating the kernel thread directly as you suggested. May be, it does not have practical implications. But, I was wondering whether it is a good idea to go through all the checks as the checks are done when someone writes to /proc/irq/.../smp_affinity file for consistency. Is the following patch a bad idea (basically call setup_affinity for both shared and non-shared interrupts)? This also works in my testing. I am curious why "setup_affinity" was called only under "if (!shared)" case in the released kernel.
kernel/irq/manage.c +0 -0
100644
kernel/irq/manage.c +0 -0
@@ -1083,9 +1083,6 @@ __setup_irq(unsigned int irq, struct irq_desc
*desc, struct irqaction *new)
if (new->flags & IRQF_NO_SOFTIRQ_CALL)
irq_settings_set_no_softirq_call(desc);- /* Set default affinity mask once everything is setup */
- setup_affinity(irq, desc, mask);
-
} else if (new->flags & IRQF_TRIGGER_MASK) {
unsigned int nmsk = new->flags & IRQF_TRIGGER_MASK;
unsigned int omsk = irq_settings_get_trigger_mask(desc);
@@ -1099,6 +1096,17 @@ __setup_irq(unsigned int irq, struct irq_desc
*desc, struct irqaction *new)
new->irq = irq;
*old_ptr = new;+ /* Set default affinity mask once everything is setup.
+ setup_affinity(irq, desc, mask);
/* Reset broken irq detection when installing new handler */
desc->irq_count = 0;
desc->irqs_unhandled = 0;Quoted 17 lines
Thanks,
tglx
---kernel/irq/manage.c +1
-0@@ -936,6 +936,7 @@ __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)*/ get_task_struct (t); new->thread = t; +set_bit (IRQTF_AFFINITY , &new->thread_flags); }if (!alloc_cpumask_var (&mask,GFP_KERNEL )) {
Quoted 10 lines · Sankara Muthukrishnan
Quoted 2 lines · Thomas Gleixner
Does the following patch fix the problem for both shared and non shared interrupts ?
Thanks Thomas. Your suggestion works for non-shared. I do not have devices sharing interrupts on my board to quickly verify. However, I hacked a kernel module to simulate shared irq's and the kernel source (to set the affinity before waiting for interrupts in the irq thread) and verified that it works for "shared interrupts" also.
Quoted 11 lines
However, I was wondering whether we are relaxing some checks that
setup_affinitydoes (like checking for flags such asIRQD_NO_BALANCING) by setting the flagIRQTF_AFFINITYafter creating the kernel thread directly as you suggested. May be, it does not have practical implications. But, I was wondering whether it is a good idea to go through all the checks as the checks are done when someone writes to/proc/irq/.../smp_affinityfile for consistency. Is the following patch a bad idea (basically callsetup_affinityfor both shared and non-shared interrupts)? This also works in my testing. I am curious why "setup_affinity" was called only under "if (!shared)" case in the released kernel.
The reason is that it is sufficient to do that for the first interrupt which gets set up. If another interrupt handler gets registered in the shared case then its pointless to call setup_affinity() as everything is already in place. The only thing which does not follow is the shared thread.
So now for the IRQTF_AFFINITY flag. We really want to set that unconditionally so all irq threads even if the NO_BALANCING flag is set move to the cpu(s) on which the interrupt is assigned to. NO_BALANCING is just making sure that the user space irq balancer cannot move around the interrupt, but it still has an affinity assigned.
Thanks,
tglx
[tip:irq/core] genirq: Always force thread affinity
In reply to Thomas GleixnerCommit-ID: 04aa530ec04f61875b99c12721162e2964e3318c Gitweb: http://git.kernel.org/tip/04aa530ec04f61875b99c12721162e2964e3318c Author: Thomas Gleixner <[email protected]> AuthorDate: Sat, 3 Nov 2012 11:52:09 +0100 Committer: Thomas Gleixner <[email protected]> CommitDate: Mon, 12 Nov 2012 20:07:18 +0100
genirq: Always force thread affinity
Sankara reported that the genirq core code fails to adjust the affinity of an interrupt thread in several cases:
1) On request/setup_irq() the call to setup_affinity() happens before the new action is registered, so the new thread is not notified.
2) For secondary shared interrupts nothing notifies the new thread to change its affinity.
3) Interrupts which have the IRQ_NO_BALANCE flag set are not moving the thread either.
Fix this by setting the thread affinity flag right on thread creation time. This ensures that under all circumstances the thread moves to the right place. Requires a check in irq_thread_check_affinity for an existing affinity mask (CONFIG_CPU_MASK_OFFSTACK=y)
--- kernel/irq/manage.c | 23 +++++++++++++++++++++-- 1 files changed, 21 insertions(+), 2 deletions(-)
kernel/irq/manage.c +21 -2
@@ -732,6 +732,7 @@ static void irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { cpumask_var_t mask; +bool valid = true;if (!test_and_clear_bit (IRQTF_AFFINITY , &action->thread_flags))return ;
@@ -746,10 +747,18 @@ irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) }raw_spin_lock_irq (&desc->lock); -cpumask_copy (mask, desc->irq_data.affinity); +/* +* This code is triggered unconditionally. Check the affinity +* mask pointer. For CPU_MASK_OFFSTACK=n this is optimized out. +*/ +if (desc->irq_data.affinity) +cpumask_copy (mask, desc->irq_data.affinity); +else + valid = false;raw_spin_unlock_irq (&desc->lock); -set_cpus_allowed_ptr (current, mask); +if (valid) +set_cpus_allowed_ptr (current, mask);free_cpumask_var (mask); }#else
@@ -954,6 +963,16 @@ __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)*/ get_task_struct (t); new->thread = t; +/* +* Tell the thread to set its affinity. This is +* important for shared interrupt handlers as we do +* not invoke setup_affinity() for the secondary +* handlers as everything is already set up. Even for +* interrupts marked with IRQF_NO_BALANCE this is +* correct as we want the thread to move to the cpu(s) +* on which the requesting code placed the interrupt. +*/ +set_bit (IRQTF_AFFINITY , &new->thread_flags); }if (!alloc_cpumask_var (&mask,GFP_KERNEL )) {