Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp424777pxb; Fri, 16 Apr 2021 08:55:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxzhvhQ5IRX0/pyM1fJ8xeufIEWH4ZS+Vpe2xKMpVP4DvfxITU1sIlEAhL0HcTOXtsgXTl+ X-Received: by 2002:a63:eb10:: with SMTP id t16mr8893265pgh.393.1618588533821; Fri, 16 Apr 2021 08:55:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618588533; cv=none; d=google.com; s=arc-20160816; b=zxur97iUgl+G4aRlzN54qGPx3IcVtGkbfNwQMASJDFVlv+ho0yCiCDcD80lst4Wg+H HhBQ59WpJNNSJzKywj0G1aaKs+TboXEW6rocL5+Cqpz++5oY0MBO9Xr7oLOIX0x+1R67 8u4cNGGZrVoPIhSTLBNrX4agxinPFIobML3EvBh1Bgq6B7pmS0Df5QMcfidE/qqXgIAU fvJ7Tsi55IozDaHyMLGT1xRyz3a7qvhpmYYLko0vGAgJvLwniFfgmYEeyplEe25onaZp p24J6Dw1xbQp39ihSEoDYOFf/IgePkf+un2NFlT5nlCsN1dJE9K4UnyqEswYZzIviNrp tfCA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=7rX6B7H+rQul3gQgNJmnR6pK+/RYb3ej5CGoNA9DLFE=; b=sNKxEvRixZe/ZvMrbhccfMjIq15LlHxh3Q7vi0iJIdfDoi/0cwpihhF4Hh1g9ikrqo JFX7yGtmhxAd1maLDhpTsOrLjyAuGW1cta5tUHWUKQNWmL2nIuqnxMQCkKLSUmRvRgxx d6RB2z9xotYJGhpLYfF2kFtfB48Q599VmEURFCiiPvjGyo9GVFXpLtBF2G0EoL1L2Bor KeJInULWlpr1tA49rdJTwg+TsZ55OEV7tQEF2R8mFKMU7cGihBv0TN7lfgIuCPmXxQJ2 XtUA+LV4GsB+Z3jkIaxKN0/CUevFPSQZTpjOZsVApzpQ6G0nAuZp0s2l+3ytJ3sunDa8 TYjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jJxMI97e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z1si7095424pjp.103.2021.04.16.08.55.21; Fri, 16 Apr 2021 08:55:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jJxMI97e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243789AbhDPOLU (ORCPT + 99 others); Fri, 16 Apr 2021 10:11:20 -0400 Received: from mail.kernel.org ([198.145.29.99]:53740 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243191AbhDPOLS (ORCPT ); Fri, 16 Apr 2021 10:11:18 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7D1E0610FC; Fri, 16 Apr 2021 14:10:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1618582253; bh=cAhCGbjsIcnQmKbjtlZBVSsWgb2a1Ru4th5RZmMnqJY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=jJxMI97e4swiDq/Q+O4TnOuLTquXQr/xwgoXMeYgqD0WYahYOM5pJV6dADOhenoSe qLvcsbtQAXCucR7OuAZ0vPZlowsKzLf9DlRlkRT+/95LD0+VefU14DSNK4Jto9Ai+i kTsqsPhuaBHbJb9w1TLXFdqlnghz6GDxznamunOsek5//hphbWmsYq2XYuvCv9Wu9q MqHnKysiUO2fFSL5fg2C4LYei6d+0kJo0wrT48QlMV0Y5ksAb4m9Hm/PJK65b0hSds /As4l1ebVNBR8Akn7gX0LiOcEfFri1U/NpfZinY3hYORMi6MSI7+56TkSNYru21lYR H7l8KvOKz4V8A== Received: from johan by xi.lan with local (Exim 4.93.0.4) (envelope-from ) id 1lXPBM-0006a1-UI; Fri, 16 Apr 2021 16:10:53 +0200 Date: Fri, 16 Apr 2021 16:10:52 +0200 From: Johan Hovold To: dillon.minfei@gmail.com Cc: gregkh@linuxfoundation.org, jirislaby@kernel.org, mcoquelin.stm32@gmail.com, alexandre.torgue@foss.st.com, lkp@intel.com, gerald.baeza@foss.st.com, erwan.leray@foss.st.com, linux-serial@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kbuild-all@lists.01.org, clang-built-linux@googlegroups.com Subject: Re: [PATCH v3] serial: stm32: optimize spin lock usage Message-ID: References: <1618567841-18546-1-git-send-email-dillon.minfei@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1618567841-18546-1-git-send-email-dillon.minfei@gmail.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Apr 16, 2021 at 06:10:41PM +0800, dillon.minfei@gmail.com wrote: > From: dillon min > > This patch aims to fix two potential bug: > - no lock to protect uart register in this case > > stm32_usart_threaded_interrupt() > spin_lock(&port->lock); > ... > stm32_usart_receive_chars() > uart_handle_sysrq_char(); > sysrq_function(); > printk(); > stm32_usart_console_write(); > locked = 0; //since port->sysrq is not zero, > no lock to protect forward register > access. > > - if add spin_trylock_irqsave() to protect uart register for sysrq = 1 case, > that might got recursive locking under UP. > So, use uart_prepare_sysrq_char(), uart_unlock_and_check_sysrq() > move sysrq handler position to irq/thread_d handler, just record > sysrq_ch in stm32_usart_receive_chars() by uart_prepare_sysrq_char() > delay the sysrq process to next interrupt handler. > > new flow: > > stm32_usart_threaded_interrupt()/stm32_usart_interrupt() > spin_lock_irqsave(&port->lock); > ... > uart_unlock_and_check_sysrq(); > spin_unlock_irqrestore(); > handle_sysrq(sysrq_ch); > stm32_usart_threaded_interrupt()//stm32_usart_interrupt() return > > Cc: Johan Hovold > Cc: Alexandre Torgue > Cc: Maxime Coquelin > Cc: Gerald Baeza > Cc: Erwan Le Ray > Reported-by: kernel test robot > Signed-off-by: dillon min > --- > v3: add uart_prepare_sysrq_char(), uart_unlock_and_check_sysrq() to move > sysrq handler inside interrupt routinei to avoid recursive locking, > according to Johan Hovold suggestion, thanks. > > drivers/tty/serial/stm32-usart.c | 24 +++++++++++------------- > 1 file changed, 11 insertions(+), 13 deletions(-) > > diff --git a/drivers/tty/serial/stm32-usart.c b/drivers/tty/serial/stm32-usart.c > index b3675cf25a69..981f50ec784e 100644 > --- a/drivers/tty/serial/stm32-usart.c > +++ b/drivers/tty/serial/stm32-usart.c > @@ -271,7 +271,7 @@ static void stm32_usart_receive_chars(struct uart_port *port, bool threaded) > } > } > > - if (uart_handle_sysrq_char(port, c)) > + if (uart_prepare_sysrq_char(port, c)) > continue; > uart_insert_char(port, sr, USART_SR_ORE, c, flag); > } > @@ -457,9 +457,10 @@ static irqreturn_t stm32_usart_interrupt(int irq, void *ptr) > struct uart_port *port = ptr; > struct stm32_port *stm32_port = to_stm32_port(port); > const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs; > + unsigned long flags; > u32 sr; > > - spin_lock(&port->lock); > + spin_lock_irqsave(&port->lock, flags); > > sr = readl_relaxed(port->membase + ofs->isr); > > @@ -477,7 +478,7 @@ static irqreturn_t stm32_usart_interrupt(int irq, void *ptr) > if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) > stm32_usart_transmit_chars(port); > > - spin_unlock(&port->lock); > + uart_unlock_and_check_sysrq(port, flags); > > if (stm32_port->rx_ch) > return IRQ_WAKE_THREAD; > @@ -489,13 +490,14 @@ static irqreturn_t stm32_usart_threaded_interrupt(int irq, void *ptr) > { > struct uart_port *port = ptr; > struct stm32_port *stm32_port = to_stm32_port(port); > + unsigned long flags; > > - spin_lock(&port->lock); > + spin_lock_irqsave(&port->lock, flags); This essentially turns the threaded handler into a non-threaded one, which is a bad idea. > if (stm32_port->rx_ch) > stm32_usart_receive_chars(port, true); > > - spin_unlock(&port->lock); > + uart_unlock_and_check_sysrq(port, flags); > > return IRQ_HANDLED; > } You also didn't base this patch on tty-next, which has a number of updates to this driver. Before noting that myself, I had fixed a couple of deadlocks in this driver which turned out to have been incidentally fixed by an unrelated path in -next. I'll be posting a series that should fix up all of this. Johan