From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934509AbcCJAir (ORCPT ); Wed, 9 Mar 2016 19:38:47 -0500 Received: from LGEAMRELO12.lge.com ([156.147.23.52]:54996 "EHLO lgeamrelo12.lge.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752281AbcCJAij (ORCPT ); Wed, 9 Mar 2016 19:38:39 -0500 X-Original-SENDERIP: 156.147.1.151 X-Original-MAILFROM: byungchul.park@lge.com X-Original-SENDERIP: 10.177.222.33 X-Original-MAILFROM: byungchul.park@lge.com Date: Thu, 10 Mar 2016 09:38:34 +0900 From: Byungchul Park To: Ingo Molnar 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 Subject: Re: [PATCH v3] lock/semaphore: Avoid an unnecessary deadlock within up() Message-ID: <20160310003834.GC5220@X58A-UD3R> References: <1455700311-2308-1-git-send-email-byungchul.park@lge.com> <20160217092828.GA19001@gmail.com> <20160309020037.GA2865@X58A-UD3R> <20160309060701.GA5220@X58A-UD3R> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20160309060701.GA5220@X58A-UD3R> User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Mar 09, 2016 at 03:07:01PM +0900, Byungchul Park wrote: > On Wed, Mar 09, 2016 at 11:00:37AM +0900, Byungchul Park wrote: > > On Wed, Feb 17, 2016 at 10:28:29AM +0100, Ingo Molnar wrote: > > > > > > * Byungchul Park wrote: > > > > > > > diff --git a/kernel/locking/semaphore.c b/kernel/locking/semaphore.c > > > > index b8120ab..6634b68 100644 > > > > --- a/kernel/locking/semaphore.c > > > > +++ b/kernel/locking/semaphore.c > > > > @@ -130,13 +130,14 @@ EXPORT_SYMBOL(down_killable); > > > > int down_trylock(struct semaphore *sem) > > > > { > > > > unsigned long flags; > > > > - int count; > > > > + int count = -1; > > > > > > > > - raw_spin_lock_irqsave(&sem->lock, flags); > > > > - count = sem->count - 1; > > > > - if (likely(count >= 0)) > > > > - sem->count = count; > > > > - raw_spin_unlock_irqrestore(&sem->lock, flags); > > > > + if (raw_spin_trylock_irqsave(&sem->lock, flags)) { > > > > + count = sem->count - 1; > > > > + if (likely(count >= 0)) > > > > + sem->count = count; > > > > + raw_spin_unlock_irqrestore(&sem->lock, flags); > > > > + } > > > > > > I still don't really like it: two parallel trylocks will cause one of them to fail > > > - while with the previous code they would both succeed. > > > > > > None of these changes are necessary with all the printk robustification > > > changes/enhancements we talked about, right? > > > > Not only printk() but any code using a semaphore, mutex and so on, can also > > cause a deadlock if wake_up_process() eventually tries to acquire the lock. > > There are several ways to solve this problem. > > > > 1. ensure wake_up_process() does not try to acquire the locks. > > 2. ensure wake_up_process() isn't protected by a spinlock of the locks. > > 3. ensure any kind of trylock stuff never cause waiting and deadlock. > > 4. and so on.. > > > > I am not sure which one is the best. But I think 3rd one is the one since > > it can be done by a generic way, even though it might decrease the success > > ratio as Ingo said, but IMHO it's not a big problem since a trylock user > > only uses the trylock when it doesn't need to be cared whether it succeed > > or fail. > > > > Which one among those do you think the best approach? Please let me know, > > then I will try to solve this problem by the appoach. > > Or what do you think about this approach in which I replace the semaphore > with mutex and apply this patch to mutex trylock? Since the parallelism > does not mean that much to mutex trylock.. Right? I am sorry for bothering you. I found that mutex trylock can be used in interrupt context. Let me think more. > > > > > > > > > Thanks, > > > > > > Ingo