Skip to contentSkip to conversation

kernel/locking: make __down_common use flags previously saved

PATCH In review Hyeonggon Yoo 2 participants 4 messages Apr 24, 2021 – May 17, 2021 Locking primitives

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernel/locking: make __down_common use flags previously saved

down, down_interruptible, down_killable, and down_timeout call raw_spin_lock_irqsave that saves current status to flags.

but in __down_common, that is called by functions above, calls raw_spin_lock_irq and raw_spin_unlock_irq regardless of flags previously saved.

this mismatch can potentially cause problem.
so made __down_common use raw_spin_lock_irqsave and raw_spin_unlock_irqrestore.

---
 kernel/locking/semaphore.c | 42 +++++++++++++++++++++-----------------
 1 file changed, 23 insertions(+), 19 deletions(-)
kernel/locking/semaphore.c +23 -19
index 9aa855a96c4a..0ea174223441 100644
@@ -33,10 +33,10 @@
 #include <linux/spinlock.h>
 #include <linux/ftrace.h>
 
-static noinline void __down(struct semaphore *sem);
-static noinline int __down_interruptible(struct semaphore *sem);
-static noinline int __down_killable(struct semaphore *sem);
-static noinline int __down_timeout(struct semaphore *sem, long timeout);
+static noinline void __down(struct semaphore *sem, unsigned long flags);
+static noinline int __down_interruptible(struct semaphore *sem, unsigned long flags);
+static noinline int __down_killable(struct semaphore *sem, unsigned long flags);
+static noinline int __down_timeout(struct semaphore *sem, long timeout, unsigned long flags);
 static noinline void __up(struct semaphore *sem);
 
 /**
@@ -58,7 +58,7 @@ void down(struct semaphore *sem)
 	if (likely(sem->count > 0))
 		sem->count--;
 	else
-		__down(sem);
+		__down(sem, flags);
 	raw_spin_unlock_irqrestore(&sem->lock, flags);
 }
 EXPORT_SYMBOL(down);
@@ -81,7 +81,7 @@ int down_interruptible(struct semaphore *sem)
 	if (likely(sem->count > 0))
 		sem->count--;
 	else
-		result = __down_interruptible(sem);
+		result = __down_interruptible(sem, flags);
 	raw_spin_unlock_irqrestore(&sem->lock, flags);
 
 	return result;
@@ -107,7 +107,7 @@ int down_killable(struct semaphore *sem)
 	if (likely(sem->count > 0))
 		sem->count--;
 	else
-		result = __down_killable(sem);
+		result = __down_killable(sem, flags);
 	raw_spin_unlock_irqrestore(&sem->lock, flags);
 
 	return result;
@@ -161,7 +161,7 @@ int down_timeout(struct semaphore *sem, long timeout)
 	if (likely(sem->count > 0))
 		sem->count--;
 	else
-		result = __down_timeout(sem, timeout);
+		result = __down_timeout(sem, timeout, flags);
 	raw_spin_unlock_irqrestore(&sem->lock, flags);
 
 	return result;
@@ -202,7 +202,7 @@ struct semaphore_waiter {
  * 'timeout' parameter for the cases without timeouts.
  */
 static inline int __sched __down_common(struct semaphore *sem, long state,
-								long timeout)
+					long timeout, unsigned long flags)
 {
 	struct semaphore_waiter waiter;
 
@@ -216,9 +216,9 @@ static inline int __sched __down_common(struct semaphore *sem, long state,
 		if (unlikely(timeout <= 0))
 			goto timed_out;
 		__set_current_state(state);
-		raw_spin_unlock_irq(&sem->lock);
+		raw_spin_unlock_irqrestore(&sem->lock, flags);
 		timeout = schedule_timeout(timeout);
-		raw_spin_lock_irq(&sem->lock);
+		raw_spin_lock_irqsave(&sem->lock, flags);
 		if (waiter.up)
 			return 0;
 	}
@@ -232,24 +232,28 @@ static inline int __sched __down_common(struct semaphore *sem, long state,
 	return -EINTR;
 }
 
-static noinline void __sched __down(struct semaphore *sem)
+static noinline void __sched __down(struct semaphore *sem, unsigned long flags)
 {
-	__down_common(sem, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
+	__down_common(sem, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT, flags);
 }
 
-static noinline int __sched __down_interruptible(struct semaphore *sem)
+static noinline int __sched __down_interruptible(struct semaphore *sem,
+							unsigned long flags)
 {
-	return __down_common(sem, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
+	return __down_common(sem, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT,
+									flags);
 }
 
-static noinline int __sched __down_killable(struct semaphore *sem)
+static noinline int __sched __down_killable(struct semaphore *sem,
+							unsigned long flags)
 {
-	return __down_common(sem, TASK_KILLABLE, MAX_SCHEDULE_TIMEOUT);
+	return __down_common(sem, TASK_KILLABLE, MAX_SCHEDULE_TIMEOUT, flags);
 }
 
-static noinline int __sched __down_timeout(struct semaphore *sem, long timeout)
+static noinline int __sched __down_timeout(struct semaphore *sem,
+					long timeout, unsigned long flags)
 {
-	return __down_common(sem, TASK_UNINTERRUPTIBLE, timeout);
+	return __down_common(sem, TASK_UNINTERRUPTIBLE, timeout, flags);
 }
 
 static noinline void __sched __up(struct semaphore *sem)
Signature · 1 line
2.25.1
#1 lore raw

Hello, is there someone?

can you tell me if my approach was wrong, or Can I improve anything? it was weird to me that __down_common disable / restores regardless of flag that was previously saved

#2 lore raw
In reply to Hyeonggon Yoo
Quoted 5 lines · Hyeonggon Yoo

down, down_interruptible, down_killable, and down_timeout call raw_spin_lock_irqsave that saves current status to flags.

but in __down_common, that is called by functions above, calls raw_spin_lock_irq and raw_spin_unlock_irq regardless of flags previously saved.

Yes, this is intentional, because if we get into __down_common() we have to schedule, so we have to enable interrupts.

Quoted 1 line

this mismatch can potentially cause problem.

What problems?

Thanks,

Ingo

#3 lore raw
Quoted 2 lines · Ingo Molnar

Yes, this is intentional, because if we get into __down_common() we have to schedule, so we have to enable interrupts.

when I sent this patch I thought we have to match XXXX_irqsave with XXXX_irqrestore, but that was my misunderstanding. I'm sorry for sending patch saying that it's wrong without explicit evidence.

now I know it's wrong so I won't do that again.

thanks,
Hyeonggon