Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp2239828yba; Fri, 10 May 2019 08:22:03 -0700 (PDT) X-Google-Smtp-Source: APXvYqxwJhTM+YN6KJ4j15u5r8DQrv6hddqP5YYSgZ10HYrhLgEAmBTsmCh3AGkn71pbpYpZ7BDL X-Received: by 2002:a17:902:2962:: with SMTP id g89mr13800314plb.190.1557501723601; Fri, 10 May 2019 08:22:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557501723; cv=none; d=google.com; s=arc-20160816; b=TLIdPiUEiCYvhlzfAM8Fm27B5BHPSyfjw+sV8P7s7yhzRZj/wX8Bsbf9LmuJiqZqXA M8vw4Gx5boTzTBxQCm3v7+euDqZ+nI+8WZL/lhtatDfShePGkfsB/QAHvUi95ggqHCa5 NFPjFDaPNACgnNmaS+4TtTuahGhKvteGw/VlGhn0gdKJo+Q7EP6JEGjE3G1fAqNFIZCC BeFg3GztTol1hj5smK4hSKcFRVUIwxYJFchhB/OmNz6WOJyjIZbb1XNdjTt71sthMGAW Dz/+lNnKYdbHnO4/i0Tf1TEGksE80fgLEAsWwt54S2ueVqt/LMjQr/M7gF+xZk1Rt6x2 h/ww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=BrbeovF+fjWSeVPTpilNjNVE8DYyqu+aTis0l/oYkL4=; b=UteUk9LyP3HYx247OsG8gDX4ePjLRZDI+gI/1Q2E78Vn0DZ2jgE6PDUsQu4OwuouPX iZWGjmz+kfcO3EDyqfFpLOLNr/QyLL+aCogO+VYCjjmZS3tSrrngUlQof3PPw8WcXznE MyihkJ1Wc/QH9Q94Y76v3x4PluRUcOyZi1yQrR3E6JQn9HMDrhe0Ws+A5uWVTMYXW0tA NgHH514+xuGXGSO+VBtXvTc38CGOecA32xSvl5JkhjSdaBDk1DZJCIO7NufpnArcmGi/ eWOtra1t3/+M4I6lDUBBzXHZNu8v8QYLMx0kDLpBJ2VdWcP5784HkgAc+ADnL++Feh8f NQGQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=bc5Spi54; 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 5si8133127pgm.540.2019.05.10.08.21.45; Fri, 10 May 2019 08:22:03 -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=pass header.i=@ffwll.ch header.s=google header.b=bc5Spi54; 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 S1727663AbfEJPU2 (ORCPT + 99 others); Fri, 10 May 2019 11:20:28 -0400 Received: from mail-io1-f68.google.com ([209.85.166.68]:35139 "EHLO mail-io1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727471AbfEJPU2 (ORCPT ); Fri, 10 May 2019 11:20:28 -0400 Received: by mail-io1-f68.google.com with SMTP id p2so4803405iol.2 for ; Fri, 10 May 2019 08:20:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=BrbeovF+fjWSeVPTpilNjNVE8DYyqu+aTis0l/oYkL4=; b=bc5Spi54huGVHztgDsdTF8soN5Lb8gcKIzqHvBSey/7ADIFfzvhOL5QJ5yDvBjJeVc KAUUU71MlDw88D+naXb7K5FeXTVRxHTTgo/KsCWFU71JQfqVO52TZXz3wVmyYs+Ootx4 7F2Up23rhUJgrLKL9ACsI64nAWHP+/0NI+XwY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=BrbeovF+fjWSeVPTpilNjNVE8DYyqu+aTis0l/oYkL4=; b=gT3K78r3iyK5ZZwF8uhV/a08DoRIWwrqOAgYvrc5zyegyUnKDwOli6S4114QgEvi2g veQWikXPi3SpuhuQwNlBJhX8RDACVNUgBRfVG0IuqXA/wquaY4z8XhrXQkxuNVcKgbAS exWpuQpQD98h2Xn7WgwyRXC1p0Nj9vpFFXUb/ipm3g4CcnuTX3801/lW6aQOPaJPzH7S AzXomNdkxFMKQ0H4UZAOoPZyqgFaU+d5GHM4yhjADqt54GO16Sd3DcvM8osl1AP3ir5/ Hk0A8D5vyelPofSxCqndEK+8SbbDWF3CcvhkD71d337CG3pNOqS7kcQEmz8RFJf1rE/b 3TSA== X-Gm-Message-State: APjAAAU0T+Rvvh6xVvmxmldsPlsk7IGfZWyU4NhyBrreWGh1h+22TJPE rnFwF+iXbXJ+HTkJ3wwxaC00dEasiqlNZ7SXjXCdkQ== X-Received: by 2002:a6b:400a:: with SMTP id k10mr8036136ioa.291.1557501627148; Fri, 10 May 2019 08:20:27 -0700 (PDT) MIME-Version: 1.0 References: <20190509120903.28939-1-daniel.vetter@ffwll.ch> <20190509200633.19678-1-daniel.vetter@ffwll.ch> <20190510092819.elu4b7fcojzcek2q@pathway.suse.cz> In-Reply-To: <20190510092819.elu4b7fcojzcek2q@pathway.suse.cz> From: Daniel Vetter Date: Fri, 10 May 2019 17:20:15 +0200 Message-ID: Subject: Re: [PATCH] kernel/locking/semaphore: use wake_q in up() To: Petr Mladek Cc: Intel Graphics Development , DRI Development , Daniel Vetter , Peter Zijlstra , Ingo Molnar , Will Deacon , Sergey Senozhatsky , Steven Rostedt , John Ogness , Chris Wilson , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 10, 2019 at 11:28 AM Petr Mladek wrote: > > On Thu 2019-05-09 22:06:33, Daniel Vetter wrote: > > console_trylock, called from within printk, can be called from pretty > > much anywhere. Including try_to_wake_up. Note that this isn't common, > > usually the box is in pretty bad shape at that point already. But it > > really doesn't help when then lockdep jumps in and spams the logs, > > potentially obscuring the real backtrace we're really interested in. > > One case I've seen (slightly simplified backtrace): > > > > Fix this specific locking recursion by moving the wake_up_process out > > from under the semaphore.lock spinlock, using wake_q as recommended by > > Peter Zijlstra. > > It might make sense to mention also the optimization effect mentioned > by Peter. > > > diff --git a/kernel/locking/semaphore.c b/kernel/locking/semaphore.c > > index 561acdd39960..7a6f33715688 100644 > > --- a/kernel/locking/semaphore.c > > +++ b/kernel/locking/semaphore.c > > @@ -169,6 +169,14 @@ int down_timeout(struct semaphore *sem, long timeout) > > } > > EXPORT_SYMBOL(down_timeout); > > > > +/* Functions for the contended case */ > > + > > +struct semaphore_waiter { > > + struct list_head list; > > + struct task_struct *task; > > + bool up; > > +}; > > + > > /** > > * up - release the semaphore > > * @sem: the semaphore to release > > @@ -179,24 +187,25 @@ EXPORT_SYMBOL(down_timeout); > > void up(struct semaphore *sem) > > { > > unsigned long flags; > > + struct semaphore_waiter *waiter; > > + DEFINE_WAKE_Q(wake_q); > > We need to call wake_q_init(&wake_q) to make sure that > it is empty. DEFINE_WAKE_Q does that already, and if it didn't, I'd wonder how I managed to boot with this patch. console_lock is usally terribly contented because thanks to fbcon we must do a full display modeset while holding it, which takes forever. As long as anyone printks meanwhile (guaranteed while loading drivers really) you have contention. -Daniel > Best Regards, > Petr > > > 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); > > + } else { > > + 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); > > + } > > raw_spin_unlock_irqrestore(&sem->lock, flags); > > + > > + wake_up_q(&wake_q); > > } > > EXPORT_SYMBOL(up); > > > > -/* Functions for the contended case */ > > - > > -struct semaphore_waiter { > > - struct list_head list; > > - struct task_struct *task; > > - bool up; > > -}; > > - > > /* > > * Because this function is inlined, the 'state' parameter will be > > * constant, and thus optimised away by the compiler. Likewise the -- Daniel Vetter Software Engineer, Intel Corporation +41 (0) 79 365 57 48 - http://blog.ffwll.ch