Question

In a Linux Kernel Module I am trying to change code that says:

down(&semaphore1);

down(&semaphore2);

   critical code here!

up(&semaphore2);

up(&semaphore1);

To use down_interruptible();

if(down_interruptible(&semaphore1))
   return -ERESTARTSYS;

if(down_interruptible(&semaphore2))
   return -ERESTARTSYS;

   critical code here!

up(&semaphore2);

up(&semaphore1);

Is this the correct way to switch from the "depricated" down to down_interruptible?

I don't understand what return -ERESTARTSYS; does, but to me it seems that it makes my kernel module exit and allow the kernel to do some other stuff until my kernel module is awaken again, is that it?

Was it helpful?

Solution

All fine, except one: you must release semaphore1 before second return:

if(down_interruptible(&semaphore1))
   return -ERESTARTSYS;

if(down_interruptible(&semaphore2)) {
   up(&semaphore1);
   return -ERESTARTSYS;
}

   critical code here!

up(&semaphore2);

up(&semaphore1);

Description about usage ERESTARTSYS see the answer.

In general, down_interruptible could be interrupted by signal to userspace application. Returning ERESTARTSYS you tell the kernel to handle signal and to call driver again. Unless you release semaphore1 second call to driver would block on semaphore1.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top