Re: [PATCH v2 1/2] Change DEFINE_SEMAPHORE() to take a number argument
From: Matthew Wilcox
Date: Fri Apr 07 2023 - 16:37:28 EST
On Wed, Apr 05, 2023 at 01:35:04PM -0700, Luis Chamberlain wrote:
> diff --git a/include/linux/semaphore.h b/include/linux/semaphore.h
> index 6694d0019a68..2d6aa3fd7861 100644
> --- a/include/linux/semaphore.h
> +++ b/include/linux/semaphore.h
> @@ -25,8 +25,15 @@ struct semaphore {
> .wait_list = LIST_HEAD_INIT((name).wait_list), \
> }
>
> -#define DEFINE_SEMAPHORE(name) \
> - struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1)
> +/*
> + * There is a big difference between a binary semaphore and a mutex.
> + * You cannot call mutex_unlock() from IRQ context because it takes an
> + * internal mutex spin_lock in a non-IRQ-safe manner. Both try_lock()
> + * and unlock() can be called from IRQ context. A mutex must also be
> + * released in the same context that locked it.
> + */
I think this confuses cause and effect. How about this:
/*
* Binary semaphores and mutexes differ in that mutexes have an owner
* so they cannot be used from interrupt context and cannot be passed
* from one thread to another. down_trylock() and up() can be called
* from interrupt context.
*/
Or this:
/*
* Unlike mutexes, binary semaphores do not have an owner, so up() can
* be called in a different thread from the one which called down().
* It is also safe to call down_trylock() and up() from interrupt
* context.
*/
I'd like to mention completions as an alternative to semaphores, but
can't figure out a nice way to fit that in.