Request_irq和setup_irq的区别(页 1) - 卡巴斯基UNIX/LINUX产品 - 卡巴一族 卡巴斯基官方论坛-卡巴斯基(中国)官方论坛-Kaspersky - Powered by Discuz! Archiver

来源:百度文库 编辑:神马文学网 时间:2024/10/02 16:48:47
Request_irq和setup_irq的区别
Linux 内核提供了两个注册中断处理函数的接口:setup_irq和request_irq。这两个函数都定义在kernel/irq/manage.c里。
/*
* Internal function to register an irqaction - typically used to
* allocate special interrupts that are part of the architecture.
*/
int setup_irq(unsigned int irq, struct irqaction *new);
/*
*    request_irq - allocate an interrupt line
*    This call allocates interrupt resources and enables the
*    interrupt line and IRQ handling.
*/
int request_irq(unsigned int irq,
irqreturn_t (*handler)(int, void *, struct pt_regs *),
unsigned long irqflags, const char *devname, void *dev_id)
这两个函数有什么样的区别呢?
先看看setup_irq
Setup_irq通常用在系统时钟(GP Timer)驱动里,注册系统时钟驱动的中断处理函数。
下面举个列子, 如s3c2410 timer驱动:
/* arch/arm/mach-s3c2410/time.c */
static struct irqaction s3c2410_timer_irq = {
.name          = "S3C2410 Timer Tick",
.flags            = IRQF_DISABLED | IRQF_TIMER,
.handler       = s3c2410_timer_interrupt,
};
static void __init s3c2410_timer_init (void)
{
s3c2410_timer_setup();
setup_irq(IRQ_TIMER4, &s3c2410_timer_irq);
}
struct sys_timer s3c24xx_timer = {
.init        = s3c2410_timer_init,
.offset           = s3c2410_gettimeoffset,
.resume        = s3c2410_timer_setup
};
struct sys_timer s3c24xx_timer = {
.init        = s3c2410_timer_init,
.offset           = s3c2410_gettimeoffset,
.resume        = s3c2410_timer_setup
};
可以看到,setup_irq的使用流程很简单。首先定义s3c2410timer驱动的irqaction结构体,该结构体用于描述timer中断的基本属性包括中断名、类别以及该中断handler等。然后通过setup_irq函数将timer的irqaction注册进内核。其中,IRQ_TIMER4为s3c2410 timer的中断号。
再看看request_irq
request_irq源码如下:
/* kernel/irq/manage.c */
int request_irq(unsigned int irq,
irqreturn_t (*handler)(int, void *, struct pt_regs *),
unsigned long irqflags, const char *devname, void *dev_id)
{
struct irqaction *action;
int retval;
#ifdef CONFIG_LOCKDEP
/*
* Lockdep wants atomic interrupt handlers:
*/
irqflags |= SA_INTERRUPT;
#endif
/*
* Sanity-check: shared interrupts must pass in a real dev-ID,
* otherwise we'll have trouble later trying to figure out
* which interrupt is which (messes up the interrupt freeing
* logic etc).
*/
if ((irqflags & IRQF_SHARED) && !dev_id)   /* 使用共享中断但没有提供非NULL的dev_id则返回错误 */
return -EINVAL;
if (irq >= NR_IRQS)            /* 中断号超出最大值 */
return -EINVAL;
if (irq_desc[irq].status & IRQ_NOREQUEST) /* 该中断号已被使用并且未共享 */
return -EINVAL;
if (!handler)
return -EINVAL;
action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);     /* 动态创建一个irqaction */
if (!action)
return -ENOMEM;
/* 下面几行是根据request_irq 传进来的参数对irqaction结构体赋值 */
action->handler = handler;
action->flags = irqflags;
cpus_clear(action->mask);
action->name = devname;
action->next = NULL;
action->dev_id = dev_id;
select_smp_affinity(irq);
retval = setup_irq(irq, action);      /* 调用setup_irq注册该中断的irqaction结构体 */
if (retval)
kfree(action);
return retval;
}
由上可以看出,request_irq的大致流程为先对申请的中断线进行安全检测,然后根据request_irq传进来的参数,动态创建该中断对应的irqaction结构体,最后通过setup_irq函数将该irqaction注册进内核适当的位置。
这两个函数的使用流程搞清楚了,那么两者之间的联系也就清楚了:
1) Request_irq的注册过程包含setup_irq,最终是调用setup_irq。
2) Request_irq比setup_irq多一套错误检测机制,即kmalloc前面3行if语句。
而Setup_irq通常是直接注册irqaction,并没针对相应中断线进行错误检测,如该irq线是否已经被占用等。因此setup_irq通常只用在特定的中断线上,如Systemtimer。除系统时钟驱动外,大部份驱动还是通过request_irq注册中断。
这里有个小问题:
既然Request_irq实际上就是包含了setup_irq的注册过程,那系统时钟驱动(GP Timer Driver)中断可以用request_irq来注册吗?
做个小试验, 将s3c2410 timer驱动的setup_irq那行去掉,改为用request_irq注册。
修改后代码如下:
static void __init s3c2410_timer_init (void)
{
s3c2410_timer_setup();
//setup_irq(IRQ_TIMER4, &s3c2410_timer_irq);
request_irq(IRQ_TIMER4, s3c2410_timer_interrupt,
IRQF_DISABLED | IRQF_TIMER, "S3C2410 Timer Tick", NULL);
}
编译运行。
结果:内核挂掉
为什么呢?很明显,系统时钟驱动中断不能用request_irq注册,大致搜了一下源码也发现,看到其他平台相关的时钟驱动中断部分都是用的setup_irq注册的。
我们来分析一下原因。
看看request_irq和setup_irq 还有哪些细节不一样?
仔细观察后注意到request_irq内有这么一行代码:
action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
作用为动态创建一个irqaction。
Kmalloc实际上也是使用的slab机制进行分配的。源码如下:
/* include/linux/slab.h */
static inline void *kmalloc(size_t size, gfp_t flags)
{
if (__builtin_constant_p(size)) {
int i = 0;
#define CACHE(x) \
if (size <= x) \
goto found; \
else \
i++;
#include "kmalloc_sizes.h"
#undef CACHE
{
extern void __you_cannot_kmalloc_that_much(void);
__you_cannot_kmalloc_that_much();
}
found:
return kmem_cache_alloc((flags & GFP_DMA) ?
malloc_sizes[i].cs_dmacachep :
malloc_sizes[i].cs_cachep, flags);
}
return __kmalloc(size, flags);
}
使用slab机制分配内存必须先对slab进行初始化,包括mem_init和kmem_cache_init。
看看kernel的初始化流程:
/* init/main.c */
asmlinkage void __init start_kernel(void)
{
……
time_init();
……
vfs_caches_init_early();
cpuset_init_early();
mem_init();  ß------ initializes the memory data structures
kmem_cache_init(); ß---- set up the general caches
……
}
Time_init函数在mem_init和kmem_cache_init之前被调用,而time_init会调用体系结构相关部分系统时钟驱动的初始化函数。拿s3c2410的例子来说,time_init最终会调用s3c2410_timer_init函数,进行s3c2410时钟驱动的初始化和注册中断处理函数。
具体过程如下:
time_init函数定义在arch/arm/kernel/time.c内:
void __init time_init(void)
{
#ifndef CONFIG_GENERIC_TIME
if (system_timer->offset == NULL)
system_timer->offset = dummy_gettimeoffset;
#endif
system_timer->init();  ß-这行实际执行的就是s3c2410_timer_init
#ifdef CONFIG_NO_IDLE_HZ
if (system_timer->dyn_tick)
system_timer->dyn_tick->lock = SPIN_LOCK_UNLOCKED;
#endif
}
system_timer在setup_arch(arch/arm/kernel/setup.c)内通过map_desc机制被初始化为s3c24xx_timer.如上面s3c2410时钟驱动代码所示,s3c24xx_timer的init成员即指向s3c2410_timer_init函数。
现在我们搞清楚了,我们大概的估计是系统时钟驱动(GP TimerDriver)的中断处理函数不能用request_irq注册是因为request_irq内会调用kmalloc动态分配内存创建timer的irqaction结构体。而kmalloc也是使用的slab内存分配机制,使用kmalloc前必须先对kernel的slab以及memdatastructure进行初始化。而这部分初始化工作是在系统时钟驱动初始化之后才进行的,所以造成kmalloc失败,从而造成系统时钟驱动的中断未注册成功,进而内核挂掉。
Request_irq和setup_irq的区别(页 1) - 卡巴斯基UNIX/LINUX产品 - 卡巴一族 卡巴斯基官方论坛-卡巴斯基(中国)官方论坛-Kaspersky - Powered by Discuz! Archiver ubuntu alternate版和desktop版区别 - 卡巴斯基UNIX/LINUX产品 - 卡巴一族 卡巴斯基官方论坛-卡巴斯基(中国)官方论坛-Kaspersky - Powered by Discuz! 黑客攻防:网页挂马攻防全接触 - 电脑求助 - 卡巴一族 卡巴一族-卡巴官方论坛-卡巴斯基... 刚申请的NOD PW+(页 1) - powered by Discuz! Archiver PDA发烧友论坛 Pocket PC,Palm,Symbian,Linux,PDA论坛,PDA软件,掌上电脑,智能手机,数码 - Windows Mobile Pocket PC软件 - X50V的SD卡升级ROM!(页 1) - powered by Discuz! Archiver 中国周易研究会总站 易冒 - Powered by Discuz!NT Archiver 有没有360doc的扩展?(页 1) - 极速版(Chrome) - 扩展和主题 - 世界之窗论坛 - Powered by Discuz! Archiver 虚拟现实软件介绍汇总(页 1) - 虚拟现实软件 - 虚拟现实中国社区 探索虚拟现实的新发展 ... 自由|开源 ... - Powered by Discuz! Archiver 在福珑上做CLFS-1.0.0-x86的问题 - 开发者乐园 - 龙芯论坛 - 龙芯官方论坛 - Powered by Discuz! 脚气病的预防与治疗(各种小偏方)(页 1) - 【船员热点】 - 启航船员培训论坛 - Powered by Discuz! Archiver 脚气病的预防与治疗(各种小偏方)(页 1) - 【船员热点】 - 启航船员培训论坛 - Powered by Discuz! Archiver - 我的电脑 ━ 软硬兼施 - 精品软件下载 - [分享][04.20][软件更新][风情整理2](页 1) - powered by Discuz! Archiver 用RHEL5.0自带的bind配置DNS服务(页 1) - DNS服务器 - ChinaUnix.net - Powered by Discuz! Archiver 英國配樂大師Michael Nyman(页 1) - 音乐空间 - 环球电影论坛 - powered by Discuz! Archiver 掘墓小子 - 旧坛(页 1) - powered by Discuz! Archiver 拔丝地瓜,南瓜(页 1) - 美食健康区 - 阳光论坛 - powered by Discuz! Archiver [强烈推荐]cFosSpeed V2.1.3 Build 1085 Beta(页 1) - powered by Discuz! Archiver 育儿,亲子论坛,早期教育 - 妈咪厨房 - 蒸鸡蛋四忌(页 1) - powered by Discuz! Archiver PCShow IT社区 - 病毒与网络安全 - 关于svchost.exe(页 1) - powered by Discuz! Archiver 虚拟现实软件介绍汇总(页 2) - 虚拟现实软件 - 虚拟现实中国社区 探索虚拟现实的新发展 ... 自由|开源 ... - Powered by Discuz! Archiver 中国财务论坛 - 焦点关注 - 中国金融总资产总量已近60万亿 全球份额逼近5%(页 1) - powered by Discuz! Archiver 【网络安全】使用咖啡杀软的一些心得——用咖啡杀软来打造安全防御体系(提供规则包)(页 1) - powered by Discuz! Archiver Qbaobao亲子乐园 育儿,亲子论坛,早期教育 - 妈咪厨房 - 蒸鸡蛋四忌(页 1) - powered by Discuz! Archiver EZB 刻录家园 - EasyBoot 专题 - EasyBoot 4.0 新特性之一 -- 使用物理软驱(页 1) - powered by Discuz! Archiver