Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested spin_lock_irqsave

If the code is as below

void test(void)
{
  spin_lock_irqsave(&lock1, flag);
  ...
  func1();
  ...
  spin_unlock_irqrestore(&lock1, flag);
}

void func1(void)
{
  spin_lock_irqsave(&lock2, flag);
  ...
  spin_unlock_irqrestore(&lock2, flag);
}

Will there be any issue with the code? when the spin_unlock_irqrestore is called in func1, will the interrupt be enabled already? What I want to achieve is test() routine can be executed without any interruption by scheduler or interrupts. Thanks a lot

like image 240
user1697356 Avatar asked Sep 25 '12 13:09

user1697356


People also ask

What is Spin_lock_irqsave?

spin_lock_irqsave() / spin_unlock_irqrestore() disable all interrupts on local CPU, lock, unlock, restore interrupts to how it was before. need to use this version if the lock is something that an interrupt handler may try to acquire. no need to worry about interrupts on other CPUs – spin lock will work normally.

Which API is used to lock semaphore in kernel space?

All semaphore API is located in the include/linux/semaphore. h header file. We may see that the semaphore mechanism is represented by the following structure: struct semaphore { raw_spinlock_t lock; unsigned int count; struct list_head wait_list; };


1 Answers

As far as I've found in the documentation, and I haven't completely exhausted my search, the flag will save the state of the bits that set the different flags and then turn off interrupts, then restore it on end. If interrupts have been turned off by the first call test and then you do another call, I would assume (and nothing indicates otherwise that I've found) that it would leave the interrupts turned off, store the flags and restore them inside func() and then restore them back to the state flag has in test.

Interrupts should only be re-enabled after your test function.

I would say your only catch is that you cannot use the same flag variable in both functions, else you'll overwrite the first one in your inner call and then reset it, and if any flags changed in between your calls, you may reset the outer one to the wrong state.

like image 59
Tony The Lion Avatar answered Oct 22 '22 07:10

Tony The Lion