Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp756476yba; Thu, 9 May 2019 05:34:44 -0700 (PDT) X-Google-Smtp-Source: APXvYqzIbwiCnxvlI6jj62vQM8/8DebLLaV5FaiAX267/RZcUFa65ylVO4F1ya64N4cGGEi+kkD1 X-Received: by 2002:a62:30c2:: with SMTP id w185mr4723823pfw.175.1557405284235; Thu, 09 May 2019 05:34:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557405284; cv=none; d=google.com; s=arc-20160816; b=orRY6466Klu3b89tsdWH9Yc41oNqWye19qTAbAhoX3au5OkKH7cHqdxor79RUxXFGa JmkaV42Ntm+SOGZuptN369qJW1zgbDuX2odly3QsD4VtEgaWlpkJcBStXY/lbLgk83kA FI8Ll+++C7PShR5YnYOCFGSL0cDpqIKyKRHJoFKe1vAnIjuKb8eHQc/4YSmwDRrc5aph otq6c40DFg+2tc/Gc9FolBd1D47M7MD0nv/Nzx1a2gNdWfMJA/2LcSGLhGLZWK4hJ9WN 7QDgTAofV39whYepIWl2qZKzULn27iF+Dot/8ZmzNgHNHFoC6m2Fod6zM1q/khfFPQzY GJng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=STuNDqhHWk4xUPlw4l8ibpyKNVMs8YJpESd8dG0MbCA=; b=qTSY4orgvKkOj1JD5VdD8XirH6upF0fAmGG5aqoqVHwYFDSxCZ225KJVB4pHzMT+ye LkpFsbB64oeTUnORUEW1NGeqF/5dfCW5IXshAKMoRMi1QTtiSQ2DTJlRbnfzwkANJ+W+ dfu1qgwqNYzuBPbfSkrVAyO7D/HGUHDQUVYni2psjol169kwCp4XQU5ifLdR7zWwzNBK DvW4YNhsXwNfFxij7gKH/bGLPDsX+/BdB0AOCckmGit0OGp33REXqdVVvEu4H1lxQ2jw MbZNpTnJRHnJtKZvsZk9P5veVSSfWAityF5YIq+LMCo2H0wPC/BnbDb3aWQvm9/C4p/h 7v1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=MDs2QHjl; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d186si2652343pgc.311.2019.05.09.05.34.27; Thu, 09 May 2019 05:34:44 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=merlin.20170209 header.b=MDs2QHjl; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726704AbfEIMbd (ORCPT + 99 others); Thu, 9 May 2019 08:31:33 -0400 Received: from merlin.infradead.org ([205.233.59.134]:57890 "EHLO merlin.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726426AbfEIMbd (ORCPT ); Thu, 9 May 2019 08:31:33 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=merlin.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=STuNDqhHWk4xUPlw4l8ibpyKNVMs8YJpESd8dG0MbCA=; b=MDs2QHjlE/RbLxY2I/iid5ZpW GshO7LKO+WxIhPVZCajrx7EVEqK+CKKczBKtK5Or3RAXVoh0TFWiI2fNxWfmCZsjee/oDW3qHCHK8 cxwH951iYsoPvcVFDFeE9+o5qu8Ahk5UESKy70qrO4dz3jLNxDrzgVuPqGQ/+JMTevEtjWWkN3vyP IG1owGFEZWKeVgJUFNjzV0pep/I7uBW47bzY7K6wxYDN/SRHIAUA2LatMcitFhfjuV1YnPF5SlOYF 8lGAoQNRLs+HqgKOBPZHRmTrrvhfc9kKPgig33rBKnGdg8Q0Kxrjxy/0ZzbuaXnk4nHvT3nM2NOI9 operbSoww==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by merlin.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1hOiCX-00033y-Vv; Thu, 09 May 2019 12:31:06 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 76A562029F884; Thu, 9 May 2019 14:31:04 +0200 (CEST) Date: Thu, 9 May 2019 14:31:04 +0200 From: Peter Zijlstra To: Daniel Vetter Cc: Intel Graphics Development , DRI Development , Daniel Vetter , Ingo Molnar , Will Deacon , Petr Mladek , Sergey Senozhatsky , Steven Rostedt , John Ogness , Chris Wilson , linux-kernel@vger.kernel.org Subject: Re: [PATCH] RFC: console: hack up console_lock more v3 Message-ID: <20190509123104.GQ2589@hirez.programming.kicks-ass.net> References: <20190509120903.28939-1-daniel.vetter@ffwll.ch> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190509120903.28939-1-daniel.vetter@ffwll.ch> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 09, 2019 at 02:09:03PM +0200, Daniel Vetter wrote: > Fix this by creating a prinkt_safe_up() which calls wake_up_process > outside of the spinlock. This isn't correct in full generality, but > good enough for console_lock: > > - console_lock doesn't use interruptible or killable or timeout down() > calls, hence an up() is the only thing that can wake up a process. Wrong :/ Any task can be woken at any random time. We must, at all times, assume spurious wakeups will happen. > +void printk_safe_up(struct semaphore *sem) > +{ > + unsigned long flags; > + struct semaphore_waiter *waiter = NULL; > + > + raw_spin_lock_irqsave(&sem->lock, flags); > + if (likely(list_empty(&sem->wait_list))) { > + sem->count++; > + } else { > + waiter = list_first_entry(&sem->wait_list, > + struct semaphore_waiter, list); > + list_del(&waiter->list); > + waiter->up = true; > + } > + raw_spin_unlock_irqrestore(&sem->lock, flags); > + > + if (waiter) /* protected by being sole wake source */ > + wake_up_process(waiter->task); > +} > +EXPORT_SYMBOL(printk_safe_up); Since its only used from printk, that EXPORT really isn't needed. Something like the below might work. --- kernel/locking/semaphore.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/kernel/locking/semaphore.c b/kernel/locking/semaphore.c index 561acdd39960..ac0a67e95aac 100644 --- a/kernel/locking/semaphore.c +++ b/kernel/locking/semaphore.c @@ -38,7 +38,6 @@ static noinline void __down(struct semaphore *sem); static noinline int __down_interruptible(struct semaphore *sem); static noinline int __down_killable(struct semaphore *sem); static noinline int __down_timeout(struct semaphore *sem, long timeout); -static noinline void __up(struct semaphore *sem); /** * down - acquire the semaphore @@ -178,14 +177,24 @@ EXPORT_SYMBOL(down_timeout); */ void up(struct semaphore *sem) { + struct semaphore_waiter *waiter; + DEFINE_WAKE_Q(wake_q); unsigned long flags; raw_spin_lock_irqsave(&sem->lock, flags); - if (likely(list_empty(&sem->wait_list))) + if (likely(list_empty(&sem->wait_list))) { sem->count++; - else - __up(sem); + goto unlock; + } + + waiter = list_first_entry(&sem->wait_list, struct semaphore_waiter, list); + list_del(&waiter->list); + waiter->up = true; + wake_q_add(&wake_q, waiter->task); +unlock: raw_spin_unlock_irqrestore(&sem->lock, flags); + + wake_up_q(&wake_q); } EXPORT_SYMBOL(up); @@ -252,12 +261,3 @@ static noinline int __sched __down_timeout(struct semaphore *sem, long timeout) { return __down_common(sem, TASK_UNINTERRUPTIBLE, timeout); } - -static noinline void __sched __up(struct semaphore *sem) -{ - struct semaphore_waiter *waiter = list_first_entry(&sem->wait_list, - struct semaphore_waiter, list); - list_del(&waiter->list); - waiter->up = true; - wake_up_process(waiter->task); -}