From patchwork Fri Jun 14 12:11:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Organov X-Patchwork-Id: 10995155 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 1648776 for ; Fri, 14 Jun 2019 12:12:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0590A28658 for ; Fri, 14 Jun 2019 12:12:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EC90A28682; Fri, 14 Jun 2019 12:12:15 +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,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,DKIM_VALID,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 792C62867F for ; Fri, 14 Jun 2019 12:12:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=Ve2ZXChfkCywBwo2Nr1l6wXPgX1y/GKog2Ubuk3HjWA=; b=pxTR8ae2ky1sC8o9lOnfRhiCRZ 7xW9GMD3yrDotGvHgB1kwSWg9t68qftvwTI2dislNAO2Vz1HrSxIrQAHKneReCegeDsKbe0OjK7La 9EfjflWT0G2j301d6GIq8peYdk+SV/oBMN7cdGc21WRfEHny/2p1ApbfgggyCCT67es/A4W/nM7pR LNheXUFrs8q6MVXAV7F2ez4FbxZpsc0fDFXDNg0UJ7lKw3/MxyL/NPlTOMHKTUtQA4AWkLIPWujz5 uQ0FR/cXInQ7gWjokAG17UlmFyMXA0DzQZYMwz9QVZ/azlcgs0qGDxWMudU+q6gGWFW2bji6nYECD jizd34mw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92 #3 (Red Hat Linux)) id 1hbl3v-0002xO-Oc; Fri, 14 Jun 2019 12:12:07 +0000 Received: from mail-lj1-x244.google.com ([2a00:1450:4864:20::244]) by bombadil.infradead.org with esmtps (Exim 4.92 #3 (Red Hat Linux)) id 1hbl3h-0002mB-QA for linux-arm-kernel@lists.infradead.org; Fri, 14 Jun 2019 12:11:55 +0000 Received: by mail-lj1-x244.google.com with SMTP id x25so2186203ljh.2 for ; Fri, 14 Jun 2019 05:11:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=SMvfNXrBGoXH5Rlv9ONYJ/L8T8ix7gsLxKZmilMNxjo=; b=OXEqXdFYrhgQa3V2TQRJ6AexhYZVCKIhb6Wb2iFCOCEBjuFOJsvlSGlw2eceWJHHuI J3HnTHQ/O69pnE7PhM11LQoC9pBVLOi8Le5efld4HJZ5cME2mtIHVpzxpwNWLYBJcNkn 18L/HO059DbOznsGU6aJUH91YebrU8EEugRNjmIql3Yzus7dzWMKtVEnzugWhHGK9TzI 6Z8vN3veiqG0OlqYRurW7nx/skf1qIoF6FnK7WLKdwM2+cjGuHE8hnyNEJMnQ0WzVSuw jx1tjf/WMkNVcvdlMfww+7kzk6IzSJXjWhD+vPQSYH2tpmrJbWd7z5wytPvrk6D7CGCG T35w== 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; bh=SMvfNXrBGoXH5Rlv9ONYJ/L8T8ix7gsLxKZmilMNxjo=; b=knFy8cf64/49/sDnkf5/Loi1wiutxVa1p9hBZpRwcG2hpwp05z0pGocGfcN7Zu9b8v RCHOzkp9PZdi2kzdG4l3wlLZ+3Lxz5nkwrrogfGoiEwDOvluoDApxlivqts9apKPbGb2 sE7VSfgIAxZXFIQAgLws6akjMZqx0T0QRn1ZD7oCxA28NL8bleM92S8Lhti3MOVncyKa tvyK7USn2xYVV1MnTAbl+6r+1xpU/DyEgNF9RbUuC8Zllvt5kMYvPLvBrW1yugmHqwDl qfF7JRzHQU43+CTpakBnbOUhdmqVkKeqaNzZUv2vkkbPdQeXWnvjJygPlJxdtYMffVXc HGgA== X-Gm-Message-State: APjAAAWx4cidm2lF52lym/cBXt/n9CXC/Nbr7/P5uho4Rxh80Ye8BLZW QWH8iICfb9UqhnYPCrvU/xo= X-Google-Smtp-Source: APXvYqyfSBKlleftIibV4Y83OfC6jOJA6VbY7mUXU01V8OkZkjsW9ihhGp/DrZuxDvinUc5ATsuWFw== X-Received: by 2002:a2e:9e4c:: with SMTP id g12mr25518090ljk.3.1560514312085; Fri, 14 Jun 2019 05:11:52 -0700 (PDT) Received: from osv.localdomain ([89.175.180.246]) by smtp.gmail.com with ESMTPSA id p27sm459683lfh.8.2019.06.14.05.11.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 14 Jun 2019 05:11:51 -0700 (PDT) From: Sergey Organov To: Sascha Hauer Subject: [PATCH RFC 1/7] serial: imx: fix locking in set_termios() Date: Fri, 14 Jun 2019 15:11:28 +0300 Message-Id: <1560514294-29111-2-git-send-email-sorganov@gmail.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1560514294-29111-1-git-send-email-sorganov@gmail.com> References: <20190614072801.3187-1-s.hauer@pengutronix.de> <1560514294-29111-1-git-send-email-sorganov@gmail.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190614_051153_843878_E4E24CBB X-CRM114-Status: GOOD ( 12.76 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Pengutronix Kernel Team , NXP Linux Team , linux-arm-kernel@lists.infradead.org, linux-serial@vger.kernel.org, =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP imx_uart_set_termios() called imx_uart_rts_active(), or imx_uart_rts_inactive() before taking port->port.lock. As a consequence, sport->port.mctrl that these functions modify could have been changed without holding port->port.lock. Moved locking of port->port.lock above the calls to fix the issue. Signed-off-by: Sergey Organov --- drivers/tty/serial/imx.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/drivers/tty/serial/imx.c b/drivers/tty/serial/imx.c index dff75dc..1055124 100644 --- a/drivers/tty/serial/imx.c +++ b/drivers/tty/serial/imx.c @@ -383,6 +383,7 @@ static void imx_uart_ucrs_restore(struct imx_port *sport, } #endif +/* called with port.lock taken and irqs caller dependent */ static void imx_uart_rts_active(struct imx_port *sport, u32 *ucr2) { *ucr2 &= ~(UCR2_CTSC | UCR2_CTS); @@ -391,6 +392,7 @@ static void imx_uart_rts_active(struct imx_port *sport, u32 *ucr2) mctrl_gpio_set(sport->gpios, sport->port.mctrl); } +/* called with port.lock taken and irqs caller dependent */ static void imx_uart_rts_inactive(struct imx_port *sport, u32 *ucr2) { *ucr2 &= ~UCR2_CTSC; @@ -400,6 +402,7 @@ static void imx_uart_rts_inactive(struct imx_port *sport, u32 *ucr2) mctrl_gpio_set(sport->gpios, sport->port.mctrl); } +/* called with port.lock taken and irqs caller dependent */ static void imx_uart_rts_auto(struct imx_port *sport, u32 *ucr2) { *ucr2 |= UCR2_CTSC; @@ -1550,6 +1553,16 @@ imx_uart_set_termios(struct uart_port *port, struct ktermios *termios, old_csize = CS8; } + del_timer_sync(&sport->timer); + + /* + * Ask the core to calculate the divisor for us. + */ + baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16); + quot = uart_get_divisor(port, baud); + + spin_lock_irqsave(&sport->port.lock, flags); + if ((termios->c_cflag & CSIZE) == CS8) ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS; else @@ -1593,16 +1606,6 @@ imx_uart_set_termios(struct uart_port *port, struct ktermios *termios, ucr2 |= UCR2_PROE; } - del_timer_sync(&sport->timer); - - /* - * Ask the core to calculate the divisor for us. - */ - baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16); - quot = uart_get_divisor(port, baud); - - spin_lock_irqsave(&sport->port.lock, flags); - sport->port.read_status_mask = 0; if (termios->c_iflag & INPCK) sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);