[PATCH] semaphore: use raw_spin_lock_irq instead of raw_spin_lock_irqsave

From: Mikulas Patocka
Date: Wed May 30 2018 - 11:24:49 EST


The sleeping functions down, down_interruptible, down_killable and
down_timeout can't be called with interrupts disabled, so we don't have to
save and restore interrupt flag.

This patch avoids the costly pushf and popf instructions on the semaphore
path.

Signed-off-by: Mikulas Patocka <mpatocka@xxxxxxxxxx>

---
kernel/locking/semaphore.c | 21 ++++++++-------------
1 file changed, 8 insertions(+), 13 deletions(-)

Index: linux-2.6/kernel/locking/semaphore.c
===================================================================
--- linux-2.6.orig/kernel/locking/semaphore.c 2017-06-03 00:22:44.000000000 +0200
+++ linux-2.6/kernel/locking/semaphore.c 2018-05-30 14:46:35.000000000 +0200
@@ -53,14 +53,12 @@ static noinline void __up(struct semapho
*/
void down(struct semaphore *sem)
{
- unsigned long flags;
-
- raw_spin_lock_irqsave(&sem->lock, flags);
+ raw_spin_lock_irq(&sem->lock);
if (likely(sem->count > 0))
sem->count--;
else
__down(sem);
- raw_spin_unlock_irqrestore(&sem->lock, flags);
+ raw_spin_unlock_irq(&sem->lock);
}
EXPORT_SYMBOL(down);

@@ -75,15 +73,14 @@ EXPORT_SYMBOL(down);
*/
int down_interruptible(struct semaphore *sem)
{
- unsigned long flags;
int result = 0;

- raw_spin_lock_irqsave(&sem->lock, flags);
+ raw_spin_lock_irq(&sem->lock);
if (likely(sem->count > 0))
sem->count--;
else
result = __down_interruptible(sem);
- raw_spin_unlock_irqrestore(&sem->lock, flags);
+ raw_spin_unlock_irq(&sem->lock);

return result;
}
@@ -101,15 +98,14 @@ EXPORT_SYMBOL(down_interruptible);
*/
int down_killable(struct semaphore *sem)
{
- unsigned long flags;
int result = 0;

- raw_spin_lock_irqsave(&sem->lock, flags);
+ raw_spin_lock_irq(&sem->lock);
if (likely(sem->count > 0))
sem->count--;
else
result = __down_killable(sem);
- raw_spin_unlock_irqrestore(&sem->lock, flags);
+ raw_spin_unlock_irq(&sem->lock);

return result;
}
@@ -155,15 +151,14 @@ EXPORT_SYMBOL(down_trylock);
*/
int down_timeout(struct semaphore *sem, long timeout)
{
- unsigned long flags;
int result = 0;

- raw_spin_lock_irqsave(&sem->lock, flags);
+ raw_spin_lock_irq(&sem->lock);
if (likely(sem->count > 0))
sem->count--;
else
result = __down_timeout(sem, timeout);
- raw_spin_unlock_irqrestore(&sem->lock, flags);
+ raw_spin_unlock_irq(&sem->lock);

return result;
}