Forums » Software Development »
problem with spinlocks in linux kernel module
Added by stephan berner almost 11 years ago
Hi,
I tried to run a trivial test driver pasted in below on a PC using Redhat linux and on the ARM core of the OMAP-138 using Angstrom linux. The function "spinlock_test" trys to obtain a spinlock it has already by calling itself two times. On the PC, it causes a deadlock as expected. On the ARM, it just runs fine, means I can load and unload the module without problems. That means, on the ARM the spinlocks work recursively which contradicts everything I read about linux spinlocks. Seems to be a kernel configuration or architecture specific problem ? Any idea ? Kernel config on the ARM attached.
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
spinlock_t lock;
void spinlock_test(void)
{
static int cnt = 0;
if(cnt < 3)
{
cnt++;
printk("////////////////// spinlock_test:%d\n", cnt);
spin_lock(&lock);
spinlock_test();
}
else
{
printk("////////////////// spinlock_test finished:\n");
spin_unlock(&lock);
spin_unlock(&lock);
spin_unlock(&lock);
cnt = 0;
}
}
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
spin_lock_init(&lock);
spinlock_test();
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
Replies (1)
RE: problem with spinlocks in linux kernel module - Added by Michael Williamson almost 11 years ago
Steven,
I think the reason why you see the difference is that your PC is likely a multi-core (multi-processor), on single core systems spin_lock and spin_unlock really don't do much for you because that is the only possible thread running (and I expect the kernel build system may collapse spin locks down to basically enabling/disabling IRQs, which won't cause a deadlock), as spinlocks are intend to disallow context switches. You should probably use a semaphore.
See this link:
http://stackoverflow.com/questions/5869825/when-should-one-use-a-spinlock-instead-of-mutex
And check out the include/linux/spinlock* kernel code with understanding the CONFIG_SMP is not enabled....
-Mike