From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933566AbcBCH2y (ORCPT ); Wed, 3 Feb 2016 02:28:54 -0500 Received: from mail-wm0-f68.google.com ([74.125.82.68]:33229 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933155AbcBCH2w (ORCPT ); Wed, 3 Feb 2016 02:28:52 -0500 Date: Wed, 3 Feb 2016 08:28:48 +0100 From: Ingo Molnar To: Byungchul Park Cc: willy@linux.intel.com, akpm@linux-foundation.org, linux-kernel@vger.kernel.org, akinobu.mita@gmail.com, jack@suse.cz, sergey.senozhatsky.work@gmail.com, peter@hurleysoftware.com, Linus Torvalds Subject: Re: [PATCH v2] lock/semaphore: Avoid an unnecessary deadlock within up() Message-ID: <20160203072847.GA32026@gmail.com> References: <1454479377-24434-1-git-send-email-byungchul.park@lge.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1454479377-24434-1-git-send-email-byungchul.park@lge.com> User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org * Byungchul Park 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