From patchwork Tue Oct 30 22:11:04 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Doug Anderson X-Patchwork-Id: 10661673 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 21ECE13A4 for ; Tue, 30 Oct 2018 22:11:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 12D782A89E for ; Tue, 30 Oct 2018 22:11:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 06E6C2AA78; Tue, 30 Oct 2018 22:11:46 +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=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 85DA42A89E for ; Tue, 30 Oct 2018 22:11:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728455AbeJaHHA (ORCPT ); Wed, 31 Oct 2018 03:07:00 -0400 Received: from mail-pl1-f193.google.com ([209.85.214.193]:37284 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728428AbeJaHHA (ORCPT ); Wed, 31 Oct 2018 03:07:00 -0400 Received: by mail-pl1-f193.google.com with SMTP id bh10-v6so6235709plb.4 for ; Tue, 30 Oct 2018 15:11:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=bspTmsO922c2ahVBjIPUGFrdX2Jss9sHKADxEgLE8Zg=; b=fXrkSqJb02IXYc9OaPtpE8lukq5eiftMgaB1hlPx0vqODF6bT9VcXBRt4g9hiDHRMU JKR240rzzYMjLgN14UurlmJEjHE3KhP1u+z4ogtnhyLALwSmcw6cJbRzW8/SVwdQpZS3 Osnt6c+YhFoHSKHMgD7J0HVl/zna8h2Us3tL4= 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:in-reply-to :references:mime-version:content-transfer-encoding; bh=bspTmsO922c2ahVBjIPUGFrdX2Jss9sHKADxEgLE8Zg=; b=HujvlFvHe1cTR6xe/RMN/ndBmF7rDAHa1xok34XPoy9tyYQzeytuqGVvmZX5xLu2h0 5dnKG08ERSZG/nk7Qa0UxQbQlza8BCQHraRr5SuKjIBX78FIjNkaCRxVT8QRqcCGdBP3 uvuRJ5TMEk5Xp9S719K8X4P8ggA/4MUy6fKlAZq2Udrg0ZRjiQUvpVM7y07iQbklDi+U DgoOLMSYiMyvfI2y9PvfShYx25bOTm961eLIr6718NMKBMVO5g1o4HwDlnyf0mFKDdSC a/86PlxqR9SwUXVNbNe8OrM4IrWLxxvtI9Gk0dMD0rkDpzCDQ0ShDL1zBNJSnKcfM/h6 cU0Q== X-Gm-Message-State: AGRZ1gKQaxgSG+S5iB0qNzSzbgQWKakosjLf1ZWCmnawqzT6T/vrKf05 Mm0SDtppMrVHM8S8wZz32bKwiQ== X-Google-Smtp-Source: AJdET5e0EgLWFkrFaDzyWDjr7tELzbl7ptVfBIkX9G8E0uwGHrGEl3sb848cXEq/Khpz1LFEs46ASw== X-Received: by 2002:a17:902:7c94:: with SMTP id y20-v6mr525436pll.218.1540937504215; Tue, 30 Oct 2018 15:11:44 -0700 (PDT) Received: from tictac2.mtv.corp.google.com ([2620:15c:202:1:c8e0:70d7:4be7:a36]) by smtp.gmail.com with ESMTPSA id b11-v6sm12844491pgi.10.2018.10.30.15.11.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 30 Oct 2018 15:11:43 -0700 (PDT) From: Douglas Anderson To: Greg Kroah-Hartman , Jiri Slaby Cc: linux-arm-kernel@lists.infradead.org, vigneshr@ti.com, linux-aspeed@lists.ozlabs.org, andrew@aj.id.au, tony@atomide.com, joel@jms.id.au, linux-serial@vger.kernel.org, linux-arm-msm@vger.kernel.org, andriy.shevchenko@linux.intel.com, dan.carpenter@oracle.com, jk@ozlabs.org, Douglas Anderson Subject: [PATCH v2 2/5] serial: core: Allow processing sysrq at port unlock time Date: Tue, 30 Oct 2018 15:11:04 -0700 Message-Id: <20181030221107.79758-3-dianders@chromium.org> X-Mailer: git-send-email 2.19.1.568.g152ad8e336-goog In-Reply-To: <20181030221107.79758-1-dianders@chromium.org> References: <20181030221107.79758-1-dianders@chromium.org> MIME-Version: 1.0 Sender: linux-arm-msm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Right now serial drivers process sysrq keys deep in their character receiving code. This means that they've already grabbed their port->lock spinlock. This can end up getting in the way if we've go to do serial stuff (especially kgdb) in response to the sysrq. Serial drivers have various hacks in them to handle this. Looking at '8250_port.c' you can see that the console_write() skips locking if we're in the sysrq handler. Looking at 'msm_serial.c' you can see that the port lock is dropped around uart_handle_sysrq_char(). It turns out that these hacks aren't exactly perfect. If you have lockdep turned on and use something like the 8250_port hack you'll get a splat that looks like: WARNING: possible circular locking dependency detected [...] is trying to acquire lock: ... (console_owner){-.-.}, at: console_unlock+0x2e0/0x5e4 but task is already holding lock: ... (&port_lock_key){-.-.}, at: serial8250_handle_irq+0x30/0xe4 which lock already depends on the new lock. the existing dependency chain (in reverse order) is: -> #1 (&port_lock_key){-.-.}: _raw_spin_lock_irqsave+0x58/0x70 serial8250_console_write+0xa8/0x250 univ8250_console_write+0x40/0x4c console_unlock+0x528/0x5e4 register_console+0x2c4/0x3b0 uart_add_one_port+0x350/0x478 serial8250_register_8250_port+0x350/0x3a8 dw8250_probe+0x67c/0x754 platform_drv_probe+0x58/0xa4 really_probe+0x150/0x294 driver_probe_device+0xac/0xe8 __driver_attach+0x98/0xd0 bus_for_each_dev+0x84/0xc8 driver_attach+0x2c/0x34 bus_add_driver+0xf0/0x1ec driver_register+0xb4/0x100 __platform_driver_register+0x60/0x6c dw8250_platform_driver_init+0x20/0x28 ... -> #0 (console_owner){-.-.}: lock_acquire+0x1e8/0x214 console_unlock+0x35c/0x5e4 vprintk_emit+0x230/0x274 vprintk_default+0x7c/0x84 vprintk_func+0x190/0x1bc printk+0x80/0xa0 __handle_sysrq+0x104/0x21c handle_sysrq+0x30/0x3c serial8250_read_char+0x15c/0x18c serial8250_rx_chars+0x34/0x74 serial8250_handle_irq+0x9c/0xe4 dw8250_handle_irq+0x98/0xcc serial8250_interrupt+0x50/0xe8 ... other info that might help us debug this: Possible unsafe locking scenario: CPU0 CPU1 ---- ---- lock(&port_lock_key); lock(console_owner); lock(&port_lock_key); lock(console_owner); *** DEADLOCK *** The hack used in 'msm_serial.c' doesn't cause the above splats but it seems a bit ugly to unlock / lock our spinlock deep in our irq handler. It seems like we could defer processing the sysrq until the end of the interrupt handler right after we've unlocked the port. With this scheme if a whole batch of sysrq characters comes in one irq then we won't handle them all, but that seems like it should be a fine compromise. Signed-off-by: Douglas Anderson --- include/linux/serial_core.h | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h index 406edae44ca3..3460b15a2607 100644 --- a/include/linux/serial_core.h +++ b/include/linux/serial_core.h @@ -173,6 +173,7 @@ struct uart_port { struct console *cons; /* struct console, if any */ #if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(SUPPORT_SYSRQ) unsigned long sysrq; /* sysrq timeout */ + unsigned int sysrq_ch; /* char for sysrq */ #endif /* flags must be updated while holding port mutex */ @@ -482,8 +483,42 @@ uart_handle_sysrq_char(struct uart_port *port, unsigned int ch) } return 0; } +static inline int +uart_prepare_sysrq_char(struct uart_port *port, unsigned int ch) +{ + if (port->sysrq) { + if (ch && time_before(jiffies, port->sysrq)) { + port->sysrq_ch = ch; + port->sysrq = 0; + return 1; + } + port->sysrq = 0; + } + return 0; +} +static inline void +uart_unlock_and_check_sysrq(struct uart_port *port, unsigned long irqflags) +{ + int sysrq_ch; + + sysrq_ch = port->sysrq_ch; + port->sysrq_ch = 0; + + spin_unlock_irqrestore(&port->lock, irqflags); + + if (sysrq_ch) + handle_sysrq(sysrq_ch); +} #else -#define uart_handle_sysrq_char(port,ch) ({ (void)port; 0; }) +static inline int +uart_handle_sysrq_char(struct uart_port *port, unsigned int ch) { return 0; } +static inline int +uart_prepare_sysrq_char(struct uart_port *port, unsigned int ch) { return 0; } +static inline void +uart_unlock_and_check_sysrq(struct uart_port *port, unsigned long irqflags) +{ + spin_unlock_irqrestore(&port->lock, irqflags); +} #endif /*