Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp7279224yba; Thu, 2 May 2019 07:19:40 -0700 (PDT) X-Google-Smtp-Source: APXvYqxmKik5/OZEi2QSRN58iVmx25zRjW3AQ42n6DQm89nxwisq3JgKpAiAl2n0fIWC8yzKzmub X-Received: by 2002:a62:4115:: with SMTP id o21mr4442083pfa.153.1556806780770; Thu, 02 May 2019 07:19:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556806780; cv=none; d=google.com; s=arc-20160816; b=d96+uedadHZiHXGwksgkcZA1dCYB8Wnl7s2Ve3yS5MWlYIQEjS4wzTXy6YBEIF7rCV gFkrhqeON0/1uIGQdMpn8UgFptTRm1OpRYUXc7LTDDqmbBjqcUcVu0YT4DMgmHtPCwEX RGapZR7HBPcR3gKc8jj2wuyOerKzuwvS7CcSGSTX7tOH63zwhHQ8CX+k5g9ywRMF8UyJ yvu9RVN4MB6zvsp+r02vKRz1oUtheSdT8d9KrEOAR3v9403n/9UyQM3FQQiXkeOQ2KP0 6/FLSzIzYBjuom/3oTCqRr6yqNlOfiLKGkuoTcpEOgT11n6M19l8uY2s0smzK/CYZtEy 4iPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=j6jUhArYSkidgJUMQ6b0WS+/knU0557UDozCsjweJWs=; b=ARe9JyerVrfbRlUuj3PEChPH8v8Dd18DzdNvTT1mT+HstcGD6BPAHWNazEhdE3onhA MTrzHBS3q3OEV2jva7+USBkwiR4z57kbOBE34BbMGRsa/SRSDxDtEsk8axOEFktAXzZD KARH1e/38lqojxCAV9r8fLqChjBFlNRC0RtOBALZ7WiP/8MJesrRCvTYghxTwlJnBGyl Jwzis34OpHRy0DPLKnOOZevSnxpAzmeadJEmqQiBqzLk758xxtVvDvzvlxT2CRzTvK2a crLgzj3lU+Q3dWpg1tG8VY/0eCRFQnW7m8o+GqoWFzKVuL8Rxgq5nwmzg2nfjJgzXEcS SWSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ffwll.ch header.s=google header.b=UFHGOIH3; 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 i9si41193826pgq.23.2019.05.02.07.19.25; Thu, 02 May 2019 07:19:40 -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=UFHGOIH3; 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 S1726409AbfEBOQx (ORCPT + 99 others); Thu, 2 May 2019 10:16:53 -0400 Received: from mail-ed1-f65.google.com ([209.85.208.65]:43927 "EHLO mail-ed1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726197AbfEBOQx (ORCPT ); Thu, 2 May 2019 10:16:53 -0400 Received: by mail-ed1-f65.google.com with SMTP id j20so2244596edq.10 for ; Thu, 02 May 2019 07:16:51 -0700 (PDT) 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=j6jUhArYSkidgJUMQ6b0WS+/knU0557UDozCsjweJWs=; b=UFHGOIH302CnF6JpJHIaEB1nf04/eQzLwfnVDas8dsLvkpH29rOZVhgNglnFzzidu+ M+Q97vTxbRGNzagU9B9kO8u/k0UXJmpNv9q5YEker0ICoHosdbgAOU9MzI71il1E6+BL mkIqvJeR+HAAxcxDhaXSmlYAMDuR0qNgj5CFs= 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=j6jUhArYSkidgJUMQ6b0WS+/knU0557UDozCsjweJWs=; b=TGPqXmiKRYWQ6++nuE91lk8Uk5ooeXsPDmYqmtL8WiKjdvcvWjr5rmU4yNO0t4T4qj b851uLAha2kdDpmQ7MK1pvvZsfTvO/Q7EDMR8YA8B/aH/URizadu4IJJWBgzRQ7/UxLp LZGuasA95d0j1my5GUiRdimFIhfEEq5vuyGZasQ8njaXf21qvqAmpB+ak2dqFjkWQnh3 OigXj+5eZ8n3qXHnzhDFIPAt2ImnpS8N+MRmY+QWGkPjhOdMuhIY3MMTr573bFUZpejS nfTZTBs5sna3c4gC6Sjryjusmpvbp+PjvipTUcoBepHiw1S7sxxCYtkTHD6fRGachH+k 8Q/g== X-Gm-Message-State: APjAAAUc/04rL+yFxNEeyLUfU84R5M1WynoMETOl+zGx7aWC4gB/OdrT e1WFs/ZZ5Q6yhz/Yfqj0HWA7bo+whZc= X-Received: by 2002:a17:906:65d4:: with SMTP id z20mr1979407ejn.38.1556806610779; Thu, 02 May 2019 07:16:50 -0700 (PDT) Received: from phenom.ffwll.local ([2a02:168:569e:0:3106:d637:d723:e855]) by smtp.gmail.com with ESMTPSA id r24sm5339705ejo.84.2019.05.02.07.16.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 02 May 2019 07:16:49 -0700 (PDT) From: Daniel Vetter To: DRI Development Cc: Intel Graphics Development , Daniel Vetter , Daniel Vetter , Peter Zijlstra , Ingo Molnar , Will Deacon , Petr Mladek , Sergey Senozhatsky , Steven Rostedt , John Ogness , linux-kernel@vger.kernel.org Subject: [PATCH] RFC: console: hack up console_trylock more Date: Thu, 2 May 2019 16:16:43 +0200 Message-Id: <20190502141643.21080-1-daniel.vetter@ffwll.ch> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 __down_trylock which only trylocks the semaphore.lock. This isn't correct in full generality, but good enough for console_lock: - there's only ever one console_lock holder, we won't fail spuriously because someone is doing a down() or up() while there's still room (unlike other semaphores with count > 1). - console_unlock() has one massive retry loop, which will catch anyone who races the trylock against the up(). This makes sure that no printk lines will get lost. Making the trylock more racy therefore has no further impact. Also cc'ing John Ogness since perhaps his printk rework fixes this all properly. 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: linux-kernel@vger.kernel.org --- include/linux/semaphore.h | 1 + kernel/locking/semaphore.c | 33 +++++++++++++++++++++++++++++++++ kernel/printk/printk.c | 2 +- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/include/linux/semaphore.h b/include/linux/semaphore.h index 11c86fbfeb98..82f6db6121c3 100644 --- a/include/linux/semaphore.h +++ b/include/linux/semaphore.h @@ -40,6 +40,7 @@ extern void down(struct semaphore *sem); 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_trylock(struct semaphore *sem); extern int __must_check down_timeout(struct semaphore *sem, long jiffies); extern void up(struct semaphore *sem); diff --git a/kernel/locking/semaphore.c b/kernel/locking/semaphore.c index 561acdd39960..345123336019 100644 --- a/kernel/locking/semaphore.c +++ b/kernel/locking/semaphore.c @@ -143,6 +143,39 @@ int down_trylock(struct semaphore *sem) } EXPORT_SYMBOL(down_trylock); +/** + * __down_trylock - try to acquire the semaphore, without any locking + * @sem: the semaphore to be acquired + * + * Try to acquire the semaphore atomically. Returns 0 if the semaphore has + * been acquired successfully or 1 if it it cannot be acquired. + * + * NOTE: This return value is inverted from both spin_trylock and + * mutex_trylock! Be careful about this when converting code. + * + * Unlike mutex_trylock, this function can be used from interrupt context, + * and the semaphore can be released by any task or interrupt. + * + * WARNING: Unlike down_trylock this function doesn't guarantee that that the + * semaphore will be acquired if it could, it's best effort only. Use for + * down_trylock_console_sem only. + */ +int __down_trylock(struct semaphore *sem) +{ + unsigned long flags; + int count; + + if (!raw_spin_trylock_irqsave(&sem->lock, flags)) + return 1; + count = sem->count - 1; + if (likely(count >= 0)) + sem->count = count; + raw_spin_unlock_irqrestore(&sem->lock, flags); + + return (count < 0); +} +EXPORT_SYMBOL(__down_trylock); + /** * down_timeout - acquire the semaphore within a specified time * @sem: the semaphore to be acquired diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 02ca827b8fac..5293803eec1f 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -217,7 +217,7 @@ static int __down_trylock_console_sem(unsigned long ip) * deadlock in printk()->down_trylock_console_sem() otherwise. */ printk_safe_enter_irqsave(flags); - lock_failed = down_trylock(&console_sem); + lock_failed = __down_trylock(&console_sem); printk_safe_exit_irqrestore(flags); if (lock_failed) -- 2.20.1