From patchwork Thu May 9 12:09:03 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Vetter X-Patchwork-Id: 10937073 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3431813AD for ; Thu, 9 May 2019 12:09:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2236B289B0 for ; Thu, 9 May 2019 12:09:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 12F4E28A7D; Thu, 9 May 2019 12:09:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 60941289B0 for ; Thu, 9 May 2019 12:09:15 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id EC33889BAC; Thu, 9 May 2019 12:09:12 +0000 (UTC) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mail-ed1-x541.google.com (mail-ed1-x541.google.com [IPv6:2a00:1450:4864:20::541]) by gabe.freedesktop.org (Postfix) with ESMTPS id 723B889BAC for ; Thu, 9 May 2019 12:09:11 +0000 (UTC) Received: by mail-ed1-x541.google.com with SMTP id w37so1814435edw.4 for ; Thu, 09 May 2019 05:09:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=izuW0WNa96R8N+iy6p0UdDercxEPAtv8yqEdooTmbYE=; b=OHwSp8wLv9uB6cKsQ71NwIwLL5/8r1rl7T8xuaNRj6qIl9bRICWfxAxEowYJdtrn4Z OMv1Vf16AaiWMVUxszPoDASs5SiBBW68dkxVV1DqMFU8Iysx0DZbwU63ljtkwohLj7Wt xKWosqA1dCcHDvrN/U+e5UB4LhPSLCtiUrIgPOe/JkTx04V5GxFTFEHkq3Xb60qWOo0Z sH+DUomUIwxKYlNPO3l9D1dpwPg5zqNJCuqKriDiIC/P7ZvymChEFR31rw9q39HZS8AF BpEXdbIcpb/pxPM4g4B/V6IBxBaYi1xZPxKEgDzv5k02bl0kJ2PHhYDDFHaRzWhJd23G YNqg== X-Gm-Message-State: APjAAAUBvJ4jlCZV9eLsi5yaXSOjp1MfYOi4OrQJ+FwCPY4122ph+P4/ TDMPDWjpuXIPc6akHsPseCjQeg== X-Google-Smtp-Source: APXvYqwcwgvTfdphrsa3kEoaNQEIBDMhqSu5+txfEoivDIHjwl+D0rUvKi5h5/fyf2hFHFwq+rK2qA== X-Received: by 2002:a17:906:7b58:: with SMTP id n24mr2925648ejo.224.1557403750029; Thu, 09 May 2019 05:09:10 -0700 (PDT) Received: from phenom.ffwll.local ([2a02:168:569e:0:3106:d637:d723:e855]) by smtp.gmail.com with ESMTPSA id d90sm531586edd.69.2019.05.09.05.09.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 09 May 2019 05:09:09 -0700 (PDT) From: Daniel Vetter To: Intel Graphics Development Subject: [PATCH] RFC: console: hack up console_lock more v3 Date: Thu, 9 May 2019 14:09:03 +0200 Message-Id: <20190509120903.28939-1-daniel.vetter@ffwll.ch> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ffwll.ch; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=izuW0WNa96R8N+iy6p0UdDercxEPAtv8yqEdooTmbYE=; b=U4lWH3eJOpAsvj58mxXHPpMtRfisBHitmT6mFOFWr/3ksRQiQm+Hsu36wTr/igC5yN /PPIy8CnryRK9hwpLLPYKnL9h3uqh1UirlV2HvbBTlfqczkYNmuoyT3bpF41c803ZgcC AFXC9pr+uuGtOw8ytjHHmeuB0cmQl5OgEzkXY= X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Petr Mladek , John Ogness , Peter Zijlstra , Daniel Vetter , Will Deacon , linux-kernel@vger.kernel.org, DRI Development , Sergey Senozhatsky , Ingo Molnar , Steven Rostedt , Daniel Vetter Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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): Call Trace: console_trylock+0xe/0x60 vprintk_emit+0xf1/0x320 printk+0x4d/0x69 __warn_printk+0x46/0x90 native_smp_send_reschedule+0x2f/0x40 check_preempt_curr+0x81/0xa0 ttwu_do_wakeup+0x14/0x220 try_to_wake_up+0x218/0x5f0 pollwake+0x6f/0x90 credit_entropy_bits+0x204/0x310 add_interrupt_randomness+0x18f/0x210 handle_irq+0x67/0x160 do_IRQ+0x5e/0x130 common_interrupt+0xf/0xf This alone isn't a problem, but the spinlock in the semaphore is also still held while waking up waiters (up() -> __up() -> try_to_wake_up() callchain), which then closes the runqueue vs. semaphore.lock loop, and upsets lockdep, which issues a circular locking splat to dmesg. Worse it upsets developers, since we don't want to spam dmesg with clutter when the machine is dying already. 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. Hence the process can't get woken and killed and reaped while we try to wake it up too. - semaphore.c always updates the waiter list while under the spinlock, so there's no other races. Specifically another process that races with a quick console_lock/unlock while we've dropped the spinlock already won't see our own waiter. Note that we only have to break the recursion for the semaphore.lock spinlock of the console_lock. Recursion within various scheduler related locks is already prevented by the printk_safe_enter/exit pair in __up_console_sem(). Also cc'ing John Ogness since perhaps his printk rework fixes this all properly. v2: Ditch attempt to fix console_trylock. v3: Add a comment explaining why the taks we're waking won't disappear (Chris), and improve commit message to address review questions. Signed-off-by: Daniel Vetter Cc: Peter Zijlstra Cc: Ingo Molnar Cc: Will Deacon Cc: Petr Mladek Cc: Sergey Senozhatsky Cc: Steven Rostedt Cc: Daniel Vetter Cc: John Ogness Cc: Chris Wilson Cc: linux-kernel@vger.kernel.org Signed-off-by: Daniel Vetter Reviewed-by: Chris Wilson Signed-off-by: Petr Mladek --- include/linux/semaphore.h | 1 + kernel/locking/semaphore.c | 31 +++++++++++++++++++++++++++++++ kernel/printk/printk.c | 2 +- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/include/linux/semaphore.h b/include/linux/semaphore.h index 11c86fbfeb98..7e839c72809d 100644 --- a/include/linux/semaphore.h +++ b/include/linux/semaphore.h @@ -41,6 +41,7 @@ extern int __must_check down_interruptible(struct semaphore *sem); extern int __must_check down_killable(struct semaphore *sem); extern int __must_check down_trylock(struct semaphore *sem); extern int __must_check down_timeout(struct semaphore *sem, long jiffies); +extern void printk_safe_up(struct semaphore *sem); extern void up(struct semaphore *sem); #endif /* __LINUX_SEMAPHORE_H */ diff --git a/kernel/locking/semaphore.c b/kernel/locking/semaphore.c index 561acdd39960..55a896f18d91 100644 --- a/kernel/locking/semaphore.c +++ b/kernel/locking/semaphore.c @@ -197,6 +197,37 @@ struct semaphore_waiter { bool up; }; +/** + * printk_safe_up - release the semaphore in console_unlock + * @sem: the semaphore to release + * + * Release the semaphore. Unlike mutexes, up() may be called from any + * context and even by tasks which have never called down(). + * + * NOTE: This is a special version of up() for console_unlock only. It is only + * safe if there are no killable, interruptible or timing out down() calls. + */ +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); + /* * Because this function is inlined, the 'state' parameter will be * constant, and thus optimised away by the compiler. Likewise the diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 02ca827b8fac..62303929afda 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -234,7 +234,7 @@ static void __up_console_sem(unsigned long ip) mutex_release(&console_lock_dep_map, 1, ip); printk_safe_enter_irqsave(flags); - up(&console_sem); + printk_safe_up(&console_sem); printk_safe_exit_irqrestore(flags); } #define up_console_sem() __up_console_sem(_RET_IP_)