中断处理函数中不用disable_irq而用disable_irq_nosync原因

来源:互联网 发布:js 日期选择插件 中文 编辑:程序博客网 时间:2024/06/06 01:52

今天在写触摸屏驱动时在中断处理函数中使用disable_irq关中断发现在进入中断处理后内核就挂掉了,于是研究了一下才发现disable_irq关闭中断并等待中断处理完后返回, 而disable_irq_nosync立即返回. 在中断处理程序中应该使用disable_irq_nosync来关闭中断


先看一下disable_irq_nosync,内核代码中是这样解释的:

/**
 *    disable_irq_nosync - disable an irq without waiting
 *    @irq: Interrupt to disable
 *
 *    Disable the selected interrupt line. Disablesand Enables are
 *    nested.
 *    Unlike disable_irq(),this function doesnot 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)
{
    struct irq_desc *desc = irq_to_desc(irq);
    unsigned long flags;

    if (!desc)
        return;

    chip_bus_lock(irq, desc);
    spin_lock_irqsave(&desc->lock, flags);
    __disable_irq(desc, irq, false);
    spin_unlock_irqrestore(&desc->lock, flags);
    chip_bus_sync_unlock(irq, desc);
}

关闭中断后程序返回, 如果在中断处理程序中, 那么会继续将中断处理程序执行完.


/**
 * 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(unsignedint irq)
{
        struct irq_desc *desc = irq_desc + irq;
        if (irq>= NR_IRQS)
                return;
        disable_irq_nosync(irq);
        if (desc->action)
                synchronize_irq(irq);
}

关闭中断并等待中断处理完后返回.从代码中可以看到, disable_irq先是调用了disable_irq_nosync, 然后检测desc->action是否为1. 在中断处理程序中, action是置1的, 所以进入synchronize_irq函数中.


/**
 * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
 * @irq: interrupt number to wait for
 *
 * 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(unsignedint irq)
{
 struct irq_desc *desc= irq_to_desc(irq);
 unsigned int status;
 if (!desc)
  return;
 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 (desc->status& IRQ_INPROGRESS)
   cpu_relax();
  /* Ok, that indicated we're done: double-check carefully. */
  spin_lock_irqsave(&desc->lock, flags);
  status = desc->status;
  spin_unlock_irqrestore(&desc->lock, flags);
  /* Oops, that failed? */
 } while (status & IRQ_INPROGRESS);
 /*
  * 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));
}


注释中说明该函数是在等待中断处理程序的结束, 这也是disable_irq与disable_irq_nosync不同的主要所在. 但是在中断处理函数中调用会发生什么情况呢? 进入中断处理函数前IRQ_INPROGRESS会被__setup_irq设置, 所以程序会一直陷在while循环中, 而此时内核以经被独占, 这就导致系统死掉.
 
总结:
由于在disable_irq中会调用synchronize_irq函数等待中断返回, 所以在中断处理程序中不能使用disable_irq, 否则会导致cpu被synchronize_irq独占而发生系统崩溃.

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 高铁晚点没赶上下班车怎么办 上班期间单位没给交养老保险怎么办 一键启动车钥匙遥控失灵怎么办 已经订购的火车票误了火车怎么办 飞机去程取消分开买的返程怎么办 订完学生票发现使用次数过了怎么办 坐火车买学生票没带学生证怎么办 买了学生票学生证磁条没了怎么办 买了动车学生票没带学生证怎么办 身份证没磁了怎么办能买火车票 格力空调保修单丢了怎么办 格力空调保修期内坏了怎么办 国际联程航班第一程延误怎么办 大麦网演唱会的票售罄了怎么办 国内转机航班第一班延误了怎么办 联程机票下一程航班被取消怎么办 联程机票第一班航班取消怎么办 联程机票第二段航班被取消怎么办 联程航班第一程延误行李怎么办 联程航班未赶上第二程行李怎么办 高铁晚点赶不上下一趟动车怎么办? 动车晚点赶不上下一趟车怎么办 动车晚点导致没赶上下一趟怎么办 浙大三位一体选考分数报错了怎么办 报到证上时间到期了还没报到怎么办 不停的打嗝已经超过24小时怎么办 面对不给下属做主的领导怎么办 法院说退款受伤人去不了证明怎么办 e栈快递柜没收到短信怎么办 京东快递放门卫丢了怎么办 丰巢快递柜收不到取件码怎么办 丰巢快递柜手机号码填错了怎么办 e栈手机号换了取不出来怎么办 买高铁票起点一样终点不一样怎么办 定火车票把名字写错一个字怎么办 火车票坐完车票丢了报销怎么办 客户交给我的钱被骗了怎么办 要证明网络来源的可靠性该怎么办 魅族手机高德地图信号弱怎么办 t恤的印花粘粘的怎么办 从包图网下载的模板素材丢失怎么办