Re: [PATCH] semaphore: Add might_sleep() to down_*() family

From: Waiman Long
Date: Fri Aug 13 2021 - 14:47:49 EST


On 8/13/21 1:27 PM, Thomas Gleixner wrote:
On Sun, Aug 08 2021 at 23:01, Waiman Long wrote:
On 8/8/21 10:12 PM, Xiaoming Ni wrote:
Semaphore is sleeping lock. Add might_sleep() to down*() family
(with exception of down_trylock()) to detect atomic context sleep.
@@ -157,6 +160,7 @@ int down_timeout(struct semaphore *sem, long timeout)
unsigned long flags;
int result = 0;
+ might_sleep();
raw_spin_lock_irqsave(&sem->lock, flags);
if (likely(sem->count > 0))
sem->count--;
I think it is simpler to just put a "might_sleep()" in __down_common()
which is the function where sleep can actually happen.
No. Putting it in __down_common() is wrong, because that covers only the
contended case.

But we want to cover the potential sleep, i.e. checking even in the
non-contended case, which is what might_sleep() is about.

You are right. Thanks for the correction.

Cheers,
Longman