Skip to contentSkip to conversation

Can request_irq be called under spinlock?

DISCUSSION Jeremy Fitzhardinge 4 participants 6 messages Feb 12, 2009 – Feb 13, 2009 IRQ

lore
Share & tools
lore
#0 lore raw

Relatively recently, I've started seeing this report from my code:

BUG: sleeping function called from invalid context at /home/jeremy/git/linux-2.6/mm/slab.c:2982
in_atomic(): 1, irqs_disabled(): 1, pid: 2249, name: xenstored
Pid: 2249, comm: xenstored Not tainted 2.6.29-rc4-tip #22
Call Trace:
[<ffffffff80233e5d>] __might_sleep+0x113/0x115
[<ffffffff802a6f8c>] __kmalloc+0x67/0xe2
[<ffffffff802ebc85>] __proc_create+0x89/0x127
[<ffffffff803f1c5f>] ? bind_evtchn_to_cpu+0x4f/0xa0
[<ffffffff802ec0ad>] proc_mkdir_mode+0x2e/0x57
[<ffffffff802ec0ec>] proc_mkdir+0x16/0x18
[<ffffffff80270d99>] register_irq_proc+0x74/0xcf
[<ffffffff8026f5ab>] __setup_irq+0x19b/0x200
[<ffffffff8026f6e7>] request_irq+0xd7/0x100
[<ffffffff803f7d8e>] ? evtchn_interrupt+0x0/0xc1
[<ffffffff803f7d8e>] ? evtchn_interrupt+0x0/0xc1
[<ffffffff803f2856>] bind_evtchn_to_irqhandler+0x3d/0x5f
[<ffffffff803f7c8b>] evtchn_bind_to_user+0x54/0x72
[<ffffffff803f81cc>] evtchn_ioctl+0x180/0x39c
[<ffffffff802a8592>] ? __dentry_open+0x1a4/0x2a2
[<ffffffff802a9513>] ? nameidata_to_filp+0x46/0x57
[<ffffffff803f02ed>] ? pnpacpi_parse_allocated_resource+0x94/0x9b
[<ffffffff8020e6d9>] ? xen_force_evtchn_callback+0xd/0xf
[<ffffffff802b5f06>] vfs_ioctl+0x2f/0x7c
[<ffffffff802b63fe>] do_vfs_ioctl+0x4ab/0x503
[<ffffffff802aad0e>] ? __fput+0x1a1/0x1ae
[<ffffffff802b649d>] sys_ioctl+0x47/0x6a
[<ffffffff80212522>] system_call_fastpath+0x16/0x1b

because I'm calling request_irq() while holding a spinlock.

request_irq() itself looks like its OK with that (it allocates with GFP_ATOMIC, for example), but __setup_irq -> register_irq_proc -> proc_mkdir ends up doing a GFP_KERNEL allocation, which leads to this message.

I can rearrange this code to not do the call under lock, but it seems like there was an unintentional change in API here.

Thanks,
J

#1 lore raw
Quoted 31 lines · Jeremy Fitzhardinge

Relatively recently, I've started seeing this report from my code:

BUG: sleeping function called from invalid context at
/home/jeremy/git/linux-2.6/mm/slab.c:2982
in_atomic(): 1, irqs_disabled(): 1, pid: 2249, name: xenstored
Pid: 2249, comm: xenstored Not tainted 2.6.29-rc4-tip #22
Call Trace:
[<ffffffff80233e5d>] __might_sleep+0x113/0x115
[<ffffffff802a6f8c>] __kmalloc+0x67/0xe2
[<ffffffff802ebc85>] __proc_create+0x89/0x127
[<ffffffff803f1c5f>] ? bind_evtchn_to_cpu+0x4f/0xa0
[<ffffffff802ec0ad>] proc_mkdir_mode+0x2e/0x57
[<ffffffff802ec0ec>] proc_mkdir+0x16/0x18
[<ffffffff80270d99>] register_irq_proc+0x74/0xcf
[<ffffffff8026f5ab>] __setup_irq+0x19b/0x200
[<ffffffff8026f6e7>] request_irq+0xd7/0x100
[<ffffffff803f7d8e>] ? evtchn_interrupt+0x0/0xc1
[<ffffffff803f7d8e>] ? evtchn_interrupt+0x0/0xc1
[<ffffffff803f2856>] bind_evtchn_to_irqhandler+0x3d/0x5f
[<ffffffff803f7c8b>] evtchn_bind_to_user+0x54/0x72
[<ffffffff803f81cc>] evtchn_ioctl+0x180/0x39c
[<ffffffff802a8592>] ? __dentry_open+0x1a4/0x2a2
[<ffffffff802a9513>] ? nameidata_to_filp+0x46/0x57
[<ffffffff803f02ed>] ? pnpacpi_parse_allocated_resource+0x94/0x9b
[<ffffffff8020e6d9>] ? xen_force_evtchn_callback+0xd/0xf
[<ffffffff802b5f06>] vfs_ioctl+0x2f/0x7c
[<ffffffff802b63fe>] do_vfs_ioctl+0x4ab/0x503
[<ffffffff802aad0e>] ? __fput+0x1a1/0x1ae
[<ffffffff802b649d>] sys_ioctl+0x47/0x6a
[<ffffffff80212522>] system_call_fastpath+0x16/0x1b
Quoted 8 lines

because I'm calling request_irq() while holding a spinlock.

request_irq() itself looks like its OK with that (it allocates with GFP_ATOMIC, for example), but __setup_irq -> register_irq_proc -> proc_mkdir ends up doing a GFP_KERNEL allocation, which leads to this message.

I can rearrange this code to not do the call under lock, but it seems like there was an unintentional change in API here.

I dont think that proc_mkdir conventions have changed recently. According to git blame fs/proc/generic.c:

^1da177e (Linus Torvalds 2005-04-16 15:20:36 -0700 580) ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);

Thanks,

tglx

#2 lore raw
Quoted 6 lines · Thomas Gleixner

I dont think that proc_mkdir conventions have changed recently. According to git blame fs/proc/generic.c:

^1da177e (Linus Torvalds 2005-04-16 15:20:36 -0700 580) ent = kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);

I think its new that request_irq ends up calling proc_mkdir though. But its moot now anyway; I cleaned up that code, and don't call request_irq under spinlock any more.

J

#3 lore raw
Quoted 4 lines · Jeremy Fitzhardinge
Quoted 3 lines · Thomas Gleixner

I dont think that proc_mkdir conventions have changed recently. According to git blame fs/proc/generic.c:

Quoted 8 lines
Quoted 4 lines

^1da177e (Linus Torvalds 2005-04-16 15:20:36 -0700 580) ent =
kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);

I think its new that request_irq ends up calling proc_mkdir though. But its moot now anyway; I cleaned up that code, and don't call request_irq under spinlock any more.

Still, the attached could be of use, no?

Hannes

---
From: Johannes Weiner <[email protected]>
Subject: irq: use GFP_KERNEL for action allocation in request_irq()

request_irq() calls into proc code via __setup_irq() which is not safe in an atomic context, so request_irq() can itself use the more reliable GFP_KERNEL allocation for the action descriptor.

---

kernel/irq/manage.c +1 -1
index 291f036..0f2b3b6 100644
@@ -709,7 +709,7 @@ int request_irq(unsigned int irq, irq_handler_t handler,
 	if (!handler)
 		return -EINVAL;
 
-	action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
+	action = kmalloc(sizeof(struct irqaction), GFP_KERNEL);
 	if (!action)
 		return -ENOMEM;
 
#4 lore raw
Quoted 15 lines · Johannes Weiner

From: Johannes Weiner <[email protected]>
Subject: irq: use GFP_KERNEL for action allocation in request_irq()

request_irq() calls into proc code via __setup_irq() which is not safe in an atomic context, so request_irq() can itself use the more reliable GFP_KERNEL allocation for the action descriptor.

---

kernel/irq/manage.c +0 -0
index 291f036..0f2b3b6 100644

kernel/irq/manage.c int request_irq(unsigned int irq, irq_handler_t handler, · line 714

Quoted 7 lines
kernel/irq/manage.c +1 -1
@@ -709,7 +709,7 @@ int request_irq(unsigned int irq, irq_handler_t handler,
 	if (!handler)
 		return -EINVAL;
 
-	action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
+	action = kmalloc(sizeof(struct irqaction), GFP_KERNEL);
 	if (!action)
 		return -ENOMEM;

Good catch - applied to tip:irq/genirq, thanks Johannes!

Ingo

#5 lore raw
In reply to Jeremy Fitzhardinge
Quoted 4 lines · Jeremy Fitzhardinge
Quoted 3 lines · Thomas Gleixner

I dont think that proc_mkdir conventions have changed recently. According to git blame fs/proc/generic.c:

Quoted 8 lines
Quoted 4 lines

^1da177e (Linus Torvalds 2005-04-16 15:20:36 -0700 580) ent =
kmalloc(sizeof(struct proc_dir_entry) + len + 1, GFP_KERNEL);

I think its new that request_irq ends up calling proc_mkdir though. But its moot now anyway; I cleaned up that code, and don't call request_irq under spinlock any more.

Just checked. The register_irq_proc() and register_handler_proc() calls in request/setup_irq() have the same time stamp in git :)

Thanks,

tglx