LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Problem using spin_lock_irq() (https://www.linuxquestions.org/questions/programming-9/problem-using-spin_lock_irq-136/)

spachiap 10-05-2000 05:23 AM

Hi,

I am working on porting code to linux. The original code has
asm("cli") & asm("sti") inline assembly to disable
interrupts on a single processor system.

In linux, I found their equivalents to be
spin_lock_irq()/spin_unlock_irq(). The following program always gives a crash.
----------------------------------------------------------

#include <asm/spinlock.h>
#include <linux/smp_lock.h>
#include <asm/system.h>
#include <linux/linkage.h>

spinlock_t lock = SPIN_LOCK_UNLOCKED;
main()
{
func();

}

asmlinkage
int func()
{
unsigned long flags;

spin_lock_irqsave(&lock, flags);
/* critcal section */
spin_unlock_irqrestore(&lock, flags);
}

Please let me know why the program always crashes.
linux:20> ./a.out
Segmentation fault (core dumped)

Thanks,
Suresh


rjlee 11-23-2004 06:41 AM

spin_lock_irq is a kernel-space macro, and cannot be used from inside a user-space program (as far as I know).

A user-space program cannot disable hardware interrupts, as to do so would prevent any other programs or drivers from receiving any generated interrupts. Spinlocks do not context-shift, so they're only really useful on multi-processor systems when blocking for short periods of time; otherwise, you want to use a semaphore.

Also, you should probably be using spin_lock_irqsave rather than spin_lock_irq to save the interrupt state (see http://www.ussg.iu.edu/hypermail/lin...06.2/0310.html)


All times are GMT -5. The time now is 11:48 AM.