Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1324986

Re: [PATCH v2] lock/semaphore: Avoid an unnecessary deadlock within up()

From Ingo Molnar <mingo@kernel.org>
Newsgroups linux.kernel
Subject Re: [PATCH v2] lock/semaphore: Avoid an unnecessary deadlock within up()
Date 2016-02-03 08:30 +0100
Message-ID <qY030-7gg-19@gated-at.bofh.it> (permalink)
References <qXYNz-6z9-15@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


* Byungchul Park <byungchul.park@lge.com> wrote:

>  void up(struct semaphore *sem)
>  {
>  	unsigned long flags;
> +	struct task_struct *p = NULL;
>  
>  	raw_spin_lock_irqsave(&sem->lock, flags);
>  	if (likely(list_empty(&sem->wait_list)))
>  		sem->count++;
>  	else
> -		__up(sem);
> +		p = __up(sem);
>  	raw_spin_unlock_irqrestore(&sem->lock, flags);
> +
> +	/*
> +	 * wake_up_process() needs not to be protected by a spinlock.
> +	 * Thus move it from the protected region to here. What is
> +	 * worse, this unnecessary protection can cause a deadlock by
> +	 * acquiring the same sem->lock within wake_up_process().
> +	 */
> +	if (unlikely(p))
> +		wake_up_process(p);

So I'm not sure this is completely race free, for cases where a semaphore is 
attached to a task and is managed/destroyed on task exit.

Since we don't have a guaranteed reference to 'p' here, the task might wake up 
(via a signal) and exit (and its task struct might be freed and the semaphore 
might be freed), after we unlocked the semaphore but before we wake the task up.

So why not move printk away from semaphores? Semaphores are classical constructs 
that have legacies and are somewhat non-obvious to use, compared to modern, 
simpler locking primitives. I'd not touch their implementation, unless we are 
absolutely sure this is a safe optimization.

Thanks,

	Ingo

Back to linux.kernel | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH v2] lock/semaphore: Avoid an unnecessary deadlock within up() Byungchul Park <byungchul.park@lge.com> - 2016-02-03 07:10 +0100
  Re: [PATCH v2] lock/semaphore: Avoid an unnecessary deadlock within  up() Ingo Molnar <mingo@kernel.org> - 2016-02-03 08:30 +0100
    Re: [PATCH v2] lock/semaphore: Avoid an unnecessary deadlock within  up() Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com> - 2016-02-03 08:50 +0100
      Re: [PATCH v2] lock/semaphore: Avoid an unnecessary deadlock within  up() Ingo Molnar <mingo@kernel.org> - 2016-02-03 09:10 +0100
        Re: [PATCH v2] lock/semaphore: Avoid an unnecessary deadlock within  up() Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com> - 2016-02-03 09:30 +0100
          Re: [PATCH v2] lock/semaphore: Avoid an unnecessary deadlock within  up() Ingo Molnar <mingo@kernel.org> - 2016-02-03 10:10 +0100
      Re: [PATCH v2] lock/semaphore: Avoid an unnecessary deadlock within  up() Byungchul Park <byungchul.park@lge.com> - 2016-02-03 09:20 +0100
        Re: [PATCH v2] lock/semaphore: Avoid an unnecessary deadlock within  up() Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com> - 2016-02-03 09:30 +0100

csiph-web