Skip to content
Snippets Groups Projects
manage.c 43.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • Linus Torvalds's avatar
    Linus Torvalds committed
    /*
     * linux/kernel/irq/manage.c
     *
    
     * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
     * Copyright (C) 2005-2006 Thomas Gleixner
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     *
     * This file contains driver APIs to the irq subsystem.
     */
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    #include <linux/irq.h>
    
    #include <linux/kthread.h>
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    #include <linux/module.h>
    #include <linux/random.h>
    #include <linux/interrupt.h>
    
    #include <linux/sched.h>
    
    #include <linux/task_work.h>
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    #include "internals.h"
    
    
    #ifdef CONFIG_IRQ_FORCED_THREADING
    __read_mostly bool force_irqthreads;
    
    static int __init setup_forced_irqthreads(char *arg)
    {
    	force_irqthreads = true;
    	return 0;
    }
    early_param("threadirqs", setup_forced_irqthreads);
    #endif
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /**
     *	synchronize_irq - wait for pending IRQ handlers (on other CPUs)
    
     *	@irq: interrupt number to wait for
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     *
     *	This function waits for any pending IRQ handlers for this interrupt
     *	to complete before returning. If you use this function while
     *	holding a resource the IRQ handler may need you will deadlock.
     *
     *	This function may be called - with care - from IRQ context.
     */
    void synchronize_irq(unsigned int irq)
    {
    
    	struct irq_desc *desc = irq_to_desc(irq);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	do {
    		unsigned long flags;
    
    		/*
    		 * Wait until we're out of the critical section.  This might
    		 * give the wrong answer due to the lack of memory barriers.
    		 */
    
    		while (irqd_irq_inprogress(&desc->irq_data))
    
    			cpu_relax();
    
    		/* Ok, that indicated we're done: double-check carefully. */
    
    		raw_spin_lock_irqsave(&desc->lock, flags);
    
    		inprogress = irqd_irq_inprogress(&desc->irq_data);
    
    		raw_spin_unlock_irqrestore(&desc->lock, flags);
    
    
    		/* Oops, that failed? */
    
    
    	/*
    	 * We made sure that no hardirq handler is running. Now verify
    	 * that no threaded handlers are active.
    	 */
    	wait_event(desc->wait_for_threads, !atomic_read(&desc->threads_active));
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    EXPORT_SYMBOL(synchronize_irq);
    
    
    #ifdef CONFIG_SMP
    cpumask_var_t irq_default_affinity;
    
    
    /**
     *	irq_can_set_affinity - Check if the affinity of a given irq can be set
     *	@irq:		Interrupt to check
     *
     */
    int irq_can_set_affinity(unsigned int irq)
    {
    
    	struct irq_desc *desc = irq_to_desc(irq);
    
    	if (!desc || !irqd_can_balance(&desc->irq_data) ||
    	    !desc->irq_data.chip || !desc->irq_data.chip->irq_set_affinity)
    
    /**
     *	irq_set_thread_affinity - Notify irq threads to adjust affinity
     *	@desc:		irq descriptor which has affitnity changed
     *
     *	We just set IRQTF_AFFINITY and delegate the affinity setting
     *	to the interrupt thread itself. We can not call
     *	set_cpus_allowed_ptr() here as we hold desc->lock and this
     *	code can be called from hard interrupt context.
     */
    void irq_set_thread_affinity(struct irq_desc *desc)
    
    {
    	struct irqaction *action = desc->action;
    
    	while (action) {
    		if (action->thread)
    
    			set_bit(IRQTF_AFFINITY, &action->thread_flags);
    
    #ifdef CONFIG_GENERIC_PENDING_IRQ
    
    static inline bool irq_can_move_pcntxt(struct irq_data *data)
    
    	return irqd_can_move_in_process_context(data);
    
    static inline bool irq_move_pending(struct irq_data *data)
    
    	return irqd_is_setaffinity_pending(data);
    
    }
    static inline void
    irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask)
    {
    	cpumask_copy(desc->pending_mask, mask);
    }
    static inline void
    irq_get_pending(struct cpumask *mask, struct irq_desc *desc)
    {
    	cpumask_copy(mask, desc->pending_mask);
    }
    #else
    
    static inline bool irq_can_move_pcntxt(struct irq_data *data) { return true; }
    
    static inline bool irq_move_pending(struct irq_data *data) { return false; }
    
    static inline void
    irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask) { }
    static inline void
    irq_get_pending(struct cpumask *mask, struct irq_desc *desc) { }
    #endif
    
    
    int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask,
    			bool force)
    {
    	struct irq_desc *desc = irq_data_to_desc(data);
    	struct irq_chip *chip = irq_data_get_irq_chip(data);
    	int ret;
    
    	ret = chip->irq_set_affinity(data, mask, false);
    	switch (ret) {
    	case IRQ_SET_MASK_OK:
    		cpumask_copy(data->affinity, mask);
    	case IRQ_SET_MASK_OK_NOCOPY:
    		irq_set_thread_affinity(desc);
    		ret = 0;
    	}
    
    	return ret;
    }
    
    
    int __irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask)
    
    	struct irq_chip *chip = irq_data_get_irq_chip(data);
    	struct irq_desc *desc = irq_data_to_desc(data);
    
    	if (!chip || !chip->irq_set_affinity)
    
    	if (irq_can_move_pcntxt(data)) {
    
    		ret = irq_do_set_affinity(data, mask, false);
    
    		irq_copy_pending(desc, mask);
    
    	if (desc->affinity_notify) {
    		kref_get(&desc->affinity_notify->kref);
    		schedule_work(&desc->affinity_notify->work);
    	}
    
    	irqd_set(data, IRQD_AFFINITY_SET);
    
    	return ret;
    }
    
    /**
     *	irq_set_affinity - Set the irq affinity of a given irq
     *	@irq:		Interrupt to set affinity
    
     *	@mask:		cpumask
    
     *
     */
    int irq_set_affinity(unsigned int irq, const struct cpumask *mask)
    {
    	struct irq_desc *desc = irq_to_desc(irq);
    	unsigned long flags;
    	int ret;
    
    	if (!desc)
    		return -EINVAL;
    
    	raw_spin_lock_irqsave(&desc->lock, flags);
    	ret =  __irq_set_affinity_locked(irq_desc_get_irq_data(desc), mask);
    
    	raw_spin_unlock_irqrestore(&desc->lock, flags);
    
    int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
    {
    	unsigned long flags;
    
    	struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
    
    
    	if (!desc)
    		return -EINVAL;
    	desc->affinity_hint = m;
    
    	irq_put_desc_unlock(desc, flags);
    
    	return 0;
    }
    EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
    
    
    static void irq_affinity_notify(struct work_struct *work)
    {
    	struct irq_affinity_notify *notify =
    		container_of(work, struct irq_affinity_notify, work);
    	struct irq_desc *desc = irq_to_desc(notify->irq);
    	cpumask_var_t cpumask;
    	unsigned long flags;
    
    
    	if (!desc || !alloc_cpumask_var(&cpumask, GFP_KERNEL))
    
    		goto out;
    
    	raw_spin_lock_irqsave(&desc->lock, flags);
    
    	if (irq_move_pending(&desc->irq_data))
    
    		irq_get_pending(cpumask, desc);
    
    		cpumask_copy(cpumask, desc->irq_data.affinity);
    
    	raw_spin_unlock_irqrestore(&desc->lock, flags);
    
    	notify->notify(notify, cpumask);
    
    	free_cpumask_var(cpumask);
    out:
    	kref_put(&notify->kref, notify->release);
    }
    
    /**
     *	irq_set_affinity_notifier - control notification of IRQ affinity changes
     *	@irq:		Interrupt for which to enable/disable notification
     *	@notify:	Context for notification, or %NULL to disable
     *			notification.  Function pointers must be initialised;
     *			the other fields will be initialised by this function.
     *
     *	Must be called in process context.  Notification may only be enabled
     *	after the IRQ is allocated and must be disabled before the IRQ is
     *	freed using free_irq().
     */
    int
    irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
    {
    	struct irq_desc *desc = irq_to_desc(irq);
    	struct irq_affinity_notify *old_notify;
    	unsigned long flags;
    
    	/* The release function is promised process context */
    	might_sleep();
    
    	if (!desc)
    		return -EINVAL;
    
    	/* Complete initialisation of *notify */
    	if (notify) {
    		notify->irq = irq;
    		kref_init(&notify->kref);
    		INIT_WORK(&notify->work, irq_affinity_notify);
    	}
    
    	raw_spin_lock_irqsave(&desc->lock, flags);
    	old_notify = desc->affinity_notify;
    	desc->affinity_notify = notify;
    	raw_spin_unlock_irqrestore(&desc->lock, flags);
    
    	if (old_notify)
    		kref_put(&old_notify->kref, old_notify->release);
    
    	return 0;
    }
    EXPORT_SYMBOL_GPL(irq_set_affinity_notifier);
    
    
    #ifndef CONFIG_AUTO_IRQ_AFFINITY
    /*
     * Generic version of the affinity autoselector.
     */
    
    static int
    setup_affinity(unsigned int irq, struct irq_desc *desc, struct cpumask *mask)
    
    	struct cpumask *set = irq_default_affinity;
    
    	int node = desc->irq_data.node;
    
    	/* Excludes PER_CPU and NO_BALANCE interrupts */
    
    	if (!irq_can_set_affinity(irq))
    		return 0;
    
    
    	/*
    	 * Preserve an userspace affinity setup, but make sure that
    	 * one of the targets is online.
    	 */
    
    	if (irqd_has_set(&desc->irq_data, IRQD_AFFINITY_SET)) {
    
    		if (cpumask_intersects(desc->irq_data.affinity,
    				       cpu_online_mask))
    			set = desc->irq_data.affinity;
    
    			irqd_clear(&desc->irq_data, IRQD_AFFINITY_SET);
    
    	cpumask_and(mask, cpu_online_mask, set);
    
    	if (node != NUMA_NO_NODE) {
    		const struct cpumask *nodemask = cpumask_of_node(node);
    
    		/* make sure at least one of the cpus in nodemask is online */
    		if (cpumask_intersects(mask, nodemask))
    			cpumask_and(mask, mask, nodemask);
    	}
    
    	irq_do_set_affinity(&desc->irq_data, mask, false);
    
    static inline int
    setup_affinity(unsigned int irq, struct irq_desc *d, struct cpumask *mask)
    
    /*
     * Called when affinity is set via /proc/irq
     */
    
    int irq_select_affinity_usr(unsigned int irq, struct cpumask *mask)
    
    {
    	struct irq_desc *desc = irq_to_desc(irq);
    	unsigned long flags;
    	int ret;
    
    
    	raw_spin_lock_irqsave(&desc->lock, flags);
    
    	ret = setup_affinity(irq, desc, mask);
    
    	raw_spin_unlock_irqrestore(&desc->lock, flags);
    
    static inline int
    setup_affinity(unsigned int irq, struct irq_desc *desc, struct cpumask *mask)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    #endif
    
    
    void __disable_irq(struct irq_desc *desc, unsigned int irq, bool suspend)
    {
    	if (suspend) {
    
    		if (!desc->action || (desc->action->flags & IRQF_NO_SUSPEND))
    
    		desc->istate |= IRQS_SUSPENDED;
    
    	if (!desc->depth++)
    
    		irq_disable(desc);
    
    static int __disable_irq_nosync(unsigned int irq)
    {
    	unsigned long flags;
    
    	struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
    
    
    	if (!desc)
    		return -EINVAL;
    	__disable_irq(desc, irq, false);
    	irq_put_desc_busunlock(desc, flags);
    	return 0;
    }
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /**
     *	disable_irq_nosync - disable an irq without waiting
     *	@irq: Interrupt to disable
     *
     *	Disable the selected interrupt line.  Disables and Enables are
     *	nested.
     *	Unlike disable_irq(), this function does not ensure existing
     *	instances of the IRQ handler have completed before returning.
     *
     *	This function may be called from IRQ context.
     */
    void disable_irq_nosync(unsigned int irq)
    {
    
    	__disable_irq_nosync(irq);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    EXPORT_SYMBOL(disable_irq_nosync);
    
    /**
     *	disable_irq - disable an irq and wait for completion
     *	@irq: Interrupt to disable
     *
     *	Disable the selected interrupt line.  Enables and Disables are
     *	nested.
     *	This function waits for any pending IRQ handlers for this interrupt
     *	to complete before returning. If you use this function while
     *	holding a resource the IRQ handler may need you will deadlock.
     *
     *	This function may be called - with care - from IRQ context.
     */
    void disable_irq(unsigned int irq)
    {
    
    	if (!__disable_irq_nosync(irq))
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		synchronize_irq(irq);
    }
    EXPORT_SYMBOL(disable_irq);
    
    
    void __enable_irq(struct irq_desc *desc, unsigned int irq, bool resume)
    
    	if (resume) {
    
    		if (!(desc->istate & IRQS_SUSPENDED)) {
    
    			if (!desc->action)
    				return;
    			if (!(desc->action->flags & IRQF_FORCE_RESUME))
    				return;
    			/* Pretend that it got disabled ! */
    			desc->depth++;
    		}
    
    		desc->istate &= ~IRQS_SUSPENDED;
    
    		WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n", irq);
    
    		if (desc->istate & IRQS_SUSPENDED)
    
    		irq_settings_set_noprobe(desc);
    
    		irq_enable(desc);
    
    		check_irq_resend(desc, irq);
    		/* fall-through */
    	}
    	default:
    		desc->depth--;
    	}
    }
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /**
     *	enable_irq - enable handling of an irq
     *	@irq: Interrupt to enable
     *
     *	Undoes the effect of one call to disable_irq().  If this
     *	matches the last disable, processing of interrupts on this
     *	IRQ line is re-enabled.
     *
    
     *	This function may be called from IRQ context only when
    
     *	desc->irq_data.chip->bus_lock and desc->chip->bus_sync_unlock are NULL !
    
    Linus Torvalds's avatar
    Linus Torvalds committed
     */
    void enable_irq(unsigned int irq)
    {
    	unsigned long flags;
    
    	struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (WARN(!desc->irq_data.chip,
    		 KERN_ERR "enable_irq before setup/request_irq: irq %u\n", irq))
    
    out:
    	irq_put_desc_busunlock(desc, flags);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    }
    EXPORT_SYMBOL(enable_irq);
    
    
    static int set_irq_wake_real(unsigned int irq, unsigned int on)
    
    	struct irq_desc *desc = irq_to_desc(irq);
    
    	if (irq_desc_get_chip(desc)->flags &  IRQCHIP_SKIP_SET_WAKE)
    		return 0;
    
    
    	if (desc->irq_data.chip->irq_set_wake)
    		ret = desc->irq_data.chip->irq_set_wake(&desc->irq_data, on);
    
     *	irq_set_irq_wake - control irq power management wakeup
    
     *	@irq:	interrupt to control
     *	@on:	enable/disable power management wakeup
     *
    
     *	Enable/disable power management wakeup mode, which is
     *	disabled by default.  Enables and disables must match,
     *	just as they match for non-wakeup mode support.
     *
     *	Wakeup mode lets this IRQ wake the system from sleep
     *	states like "suspend to RAM".
    
    int irq_set_irq_wake(unsigned int irq, unsigned int on)
    
    	struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
    
    	/* wakeup-capable irqs can be shared between drivers that
    	 * don't need to have the same sleep mode behaviors.
    	 */
    	if (on) {
    
    		if (desc->wake_depth++ == 0) {
    			ret = set_irq_wake_real(irq, on);
    			if (ret)
    				desc->wake_depth = 0;
    			else
    
    				irqd_set(&desc->irq_data, IRQD_WAKEUP_STATE);
    
    	} else {
    		if (desc->wake_depth == 0) {
    
    			WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
    
    		} else if (--desc->wake_depth == 0) {
    			ret = set_irq_wake_real(irq, on);
    			if (ret)
    				desc->wake_depth = 1;
    			else
    
    				irqd_clear(&desc->irq_data, IRQD_WAKEUP_STATE);
    
    	irq_put_desc_busunlock(desc, flags);
    
    EXPORT_SYMBOL(irq_set_irq_wake);
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /*
     * Internal function that tells the architecture code whether a
     * particular irq has been exclusively allocated or is available
     * for driver use.
     */
    int can_request_irq(unsigned int irq, unsigned long irqflags)
    {
    
    	struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
    
    	int canrequest = 0;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (irq_settings_can_request(desc)) {
    		if (desc->action)
    			if (irqflags & desc->action->flags & IRQF_SHARED)
    				canrequest =1;
    	}
    	irq_put_desc_unlock(desc, flags);
    	return canrequest;
    
    int __irq_set_trigger(struct irq_desc *desc, unsigned int irq,
    
    	struct irq_chip *chip = desc->irq_data.chip;
    
    	int ret, unmask = 0;
    
    	if (!chip || !chip->irq_set_type) {
    
    		/*
    		 * IRQF_TRIGGER_* but the PIC does not support multiple
    		 * flow-types?
    		 */
    
    		pr_debug("No set_type function for IRQ %d (%s)\n", irq,
    
    			 chip ? (chip->name ? : "unknown") : "unknown");
    
    	flags &= IRQ_TYPE_SENSE_MASK;
    
    
    	if (chip->flags & IRQCHIP_SET_TYPE_MASKED) {
    
    		if (!irqd_irq_masked(&desc->irq_data))
    
    			mask_irq(desc);
    
    		if (!irqd_irq_disabled(&desc->irq_data))
    
    	/* caller masked out all except trigger mode flags */
    
    	ret = chip->irq_set_type(&desc->irq_data, flags);
    
    	switch (ret) {
    	case IRQ_SET_MASK_OK:
    		irqd_clear(&desc->irq_data, IRQD_TRIGGER_MASK);
    		irqd_set(&desc->irq_data, flags);
    
    	case IRQ_SET_MASK_OK_NOCOPY:
    		flags = irqd_get_trigger_type(&desc->irq_data);
    		irq_settings_set_trigger_mask(desc, flags);
    		irqd_clear(&desc->irq_data, IRQD_LEVEL);
    		irq_settings_clr_level(desc);
    		if (flags & IRQ_TYPE_LEVEL_MASK) {
    			irq_settings_set_level(desc);
    			irqd_set(&desc->irq_data, IRQD_LEVEL);
    		}
    
    		pr_err("Setting trigger mode %lu for irq %u failed (%pF)\n",
    
    		       flags, irq, chip->irq_set_type);
    
    	if (unmask)
    		unmask_irq(desc);
    
    #ifdef CONFIG_HARDIRQS_SW_RESEND
    int irq_set_parent(int irq, int parent_irq)
    {
    	unsigned long flags;
    	struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
    
    	if (!desc)
    		return -EINVAL;
    
    	desc->parent_irq = parent_irq;
    
    	irq_put_desc_unlock(desc, flags);
    	return 0;
    }
    #endif
    
    
    /*
     * Default primary interrupt handler for threaded interrupts. Is
     * assigned as primary handler when request_threaded_irq is called
     * with handler == NULL. Useful for oneshot interrupts.
     */
    static irqreturn_t irq_default_primary_handler(int irq, void *dev_id)
    {
    	return IRQ_WAKE_THREAD;
    }
    
    
    /*
     * Primary handler for nested threaded interrupts. Should never be
     * called.
     */
    static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id)
    {
    	WARN(1, "Primary handler called for nested irq %d\n", irq);
    	return IRQ_NONE;
    }
    
    
    static int irq_wait_for_interrupt(struct irqaction *action)
    {
    
    	set_current_state(TASK_INTERRUPTIBLE);
    
    
    	while (!kthread_should_stop()) {
    
    
    		if (test_and_clear_bit(IRQTF_RUNTHREAD,
    				       &action->thread_flags)) {
    
    			__set_current_state(TASK_RUNNING);
    			return 0;
    
    		set_current_state(TASK_INTERRUPTIBLE);
    
    	__set_current_state(TASK_RUNNING);
    
    /*
     * Oneshot interrupts keep the irq line masked until the threaded
     * handler finished. unmask if the interrupt has not been disabled and
     * is marked MASKED.
     */
    
    static void irq_finalize_oneshot(struct irq_desc *desc,
    
    	if (!(desc->istate & IRQS_ONESHOT))
    		return;
    
    	raw_spin_lock_irq(&desc->lock);
    
    
    	/*
    	 * Implausible though it may be we need to protect us against
    	 * the following scenario:
    	 *
    	 * The thread is faster done than the hard interrupt handler
    	 * on the other CPU. If we unmask the irq line then the
    	 * interrupt can come in again and masks the line, leaves due
    
    	 * to IRQS_INPROGRESS and the irq line is masked forever.
    
    	 *
    	 * This also serializes the state of shared oneshot handlers
    	 * versus "desc->threads_onehsot |= action->thread_mask;" in
    	 * irq_wake_thread(). See the comment there which explains the
    	 * serialization.
    
    	if (unlikely(irqd_irq_inprogress(&desc->irq_data))) {
    
    		raw_spin_unlock_irq(&desc->lock);
    
    	/*
    	 * Now check again, whether the thread should run. Otherwise
    	 * we would clear the threads_oneshot bit of this thread which
    	 * was just set.
    	 */
    
    	if (test_bit(IRQTF_RUNTHREAD, &action->thread_flags))
    
    		goto out_unlock;
    
    	desc->threads_oneshot &= ~action->thread_mask;
    
    
    	if (!desc->threads_oneshot && !irqd_irq_disabled(&desc->irq_data) &&
    	    irqd_irq_masked(&desc->irq_data))
    		unmask_irq(desc);
    
    
    	raw_spin_unlock_irq(&desc->lock);
    
     * Check whether we need to chasnge the affinity of the interrupt thread.
    
     */
    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;
    
    	/*
    	 * In case we are out of memory we set IRQTF_AFFINITY again and
    	 * try again next time
    	 */
    	if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
    		set_bit(IRQTF_AFFINITY, &action->thread_flags);
    		return;
    	}
    
    
    	raw_spin_lock_irq(&desc->lock);
    
    	/*
    	 * 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);
    
    	if (valid)
    		set_cpus_allowed_ptr(current, mask);
    
    #else
    static inline void
    irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { }
    #endif
    
    /*
     * Interrupts which are not explicitely requested as threaded
     * interrupts rely on the implicit bh/preempt disable of the hard irq
     * context. So we need to disable bh here to avoid deadlocks and other
     * side effects.
     */
    
    irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action)
    {
    
    	local_bh_disable();
    
    	ret = action->thread_fn(action->irq, action->dev_id);
    
    	irq_finalize_oneshot(desc, action);
    
    	local_bh_enable();
    
    }
    
    /*
     * Interrupts explicitely requested as threaded interupts want to be
     * preemtible - many of them need to sleep and wait for slow busses to
     * complete.
     */
    
    static irqreturn_t irq_thread_fn(struct irq_desc *desc,
    		struct irqaction *action)
    
    	irqreturn_t ret;
    
    	ret = action->thread_fn(action->irq, action->dev_id);
    
    	irq_finalize_oneshot(desc, action);
    
    static void wake_threads_waitq(struct irq_desc *desc)
    {
    	if (atomic_dec_and_test(&desc->threads_active) &&
    	    waitqueue_active(&desc->wait_for_threads))
    		wake_up(&desc->wait_for_threads);
    }
    
    
    static void irq_thread_dtor(struct callback_head *unused)
    
    {
    	struct task_struct *tsk = current;
    	struct irq_desc *desc;
    	struct irqaction *action;
    
    	if (WARN_ON_ONCE(!(current->flags & PF_EXITING)))
    		return;
    
    	action = kthread_data(tsk);
    
    
    	pr_err("exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
    
    Alan Cox's avatar
    Alan Cox committed
    	       tsk->comm, tsk->pid, action->irq);
    
    
    
    	desc = irq_to_desc(action->irq);
    	/*
    	 * If IRQTF_RUNTHREAD is set, we need to decrement
    	 * desc->threads_active and wake possible waiters.
    	 */
    	if (test_and_clear_bit(IRQTF_RUNTHREAD, &action->thread_flags))
    		wake_threads_waitq(desc);
    
    	/* Prevent a stale desc->threads_oneshot */
    	irq_finalize_oneshot(desc, action);
    }
    
    
    /*
     * Interrupt handler thread
     */
    static int irq_thread(void *data)
    {
    
    	struct callback_head on_exit_work;
    
    	static const struct sched_param param = {
    
    		.sched_priority = MAX_USER_RT_PRIO/2,
    	};
    
    	struct irqaction *action = data;
    	struct irq_desc *desc = irq_to_desc(action->irq);
    
    	irqreturn_t (*handler_fn)(struct irq_desc *desc,
    			struct irqaction *action);
    
    	if (force_irqthreads && test_bit(IRQTF_FORCED_THREAD,
    
    					&action->thread_flags))
    		handler_fn = irq_forced_thread_fn;
    	else
    		handler_fn = irq_thread_fn;
    
    
    	sched_setscheduler(current, SCHED_FIFO, &param);
    
    Al Viro's avatar
    Al Viro committed
    	init_task_work(&on_exit_work, irq_thread_dtor);
    
    	task_work_add(current, &on_exit_work, false);
    
    	irq_thread_check_affinity(desc, action);
    
    
    	while (!irq_wait_for_interrupt(action)) {
    
    		irqreturn_t action_ret;
    
    		irq_thread_check_affinity(desc, action);
    
    
    		action_ret = handler_fn(desc, action);
    		if (!noirqdebug)
    			note_interrupt(action->irq, desc, action_ret);
    
    		wake_threads_waitq(desc);
    
    	/*
    	 * This is the regular exit path. __free_irq() is stopping the
    	 * thread via kthread_stop() after calling
    	 * synchronize_irq(). So neither IRQTF_RUNTHREAD nor the
    
    	 * oneshot mask bit can be set. We cannot verify that as we
    	 * cannot touch the oneshot mask at this point anymore as
    	 * __setup_irq() might have given out currents thread_mask
    	 * again.
    
    	task_work_cancel(current, irq_thread_dtor);
    
    static void irq_setup_forced_threading(struct irqaction *new)
    {
    	if (!force_irqthreads)
    		return;
    	if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT))
    		return;
    
    	new->flags |= IRQF_ONESHOT;
    
    	if (!new->thread_fn) {
    		set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
    		new->thread_fn = new->handler;
    		new->handler = irq_default_primary_handler;
    	}
    }
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    /*
     * Internal function to register an irqaction - typically used to
     * allocate special interrupts that are part of the architecture.
     */
    
    Ingo Molnar's avatar
    Ingo Molnar committed
    __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    {
    
    	struct irqaction *old, **old_ptr;
    
    	unsigned long flags, thread_mask = 0;
    
    	int ret, nested, shared = 0;
    	cpumask_var_t mask;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	if (desc->irq_data.chip == &no_irq_chip)
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    		return -ENOSYS;
    
    	if (!try_module_get(desc->owner))
    		return -ENODEV;
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    
    
    	 * Check whether the interrupt nests into another interrupt
    	 * thread.
    	 */
    
    	nested = irq_settings_is_nested_thread(desc);
    
    		if (!new->thread_fn) {
    			ret = -EINVAL;
    			goto out_mput;
    		}
    
    		/*
    		 * Replace the primary handler which was provided from
    		 * the driver for non nested interrupt handling by the
    		 * dummy function which warns when called.
    		 */
    		new->handler = irq_nested_primary_handler;
    
    		if (irq_settings_can_thread(desc))
    			irq_setup_forced_threading(new);
    
    	 * Create a handler thread when a thread function is supplied
    	 * and the interrupt does not nest into another interrupt
    	 * thread.
    
    	if (new->thread_fn && !nested) {
    
    		struct task_struct *t;
    
    		t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
    				   new->name);
    
    		if (IS_ERR(t)) {
    			ret = PTR_ERR(t);
    			goto out_mput;
    		}
    
    		/*
    		 * We keep the reference to the task struct even if
    		 * the thread dies to avoid that the interrupt code
    		 * references an already freed task_struct.
    		 */
    		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)) {
    		ret = -ENOMEM;
    		goto out_thread;
    	}
    
    
    	/*
    	 * Drivers are often written to work w/o knowledge about the
    	 * underlying irq chip implementation, so a request for a
    	 * threaded irq without a primary hard irq context handler
    	 * requires the ONESHOT flag to be set. Some irq chips like
    	 * MSI based interrupts are per se one shot safe. Check the
    	 * chip flags, so we can avoid the unmask dance at the end of
    	 * the threaded handler for those.
    	 */
    	if (desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)
    		new->flags &= ~IRQF_ONESHOT;
    
    
    Linus Torvalds's avatar
    Linus Torvalds committed
    	/*
    	 * The following block of code has to be executed atomically
    	 */
    
    	raw_spin_lock_irqsave(&desc->lock, flags);
    
    	old_ptr = &desc->action;
    	old = *old_ptr;