From patchwork Thu Jul 4 13:00:23 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Sergey Organov X-Patchwork-Id: 11031461 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 1903B14F6 for ; Thu, 4 Jul 2019 13:01:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0564128751 for ; Thu, 4 Jul 2019 13:01:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E8AAB28992; Thu, 4 Jul 2019 13:01:24 +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 8C68828751 for ; Thu, 4 Jul 2019 13:01:24 +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:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version: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=e4F20iVqNw1g4Qo7xiiqPyH7pwa1LPvq0hl32V/qHeA=; b=DI8EaW5PZ5G4nI DAL2VIUfFbU6bjNM+f1L9G6UeYqj7QI/jOQh1QsGA6T4IKXhZtsmdo1FSisf+xcXKuI/D4BCzgqQs iKPEbkPJHt7GTZUy5U1CJn101snSmo/i0cJGx57aNWKR2wfnlMnZDh6coPwvQ7M6HhEGPhtvug2/J KY2PRhE+p3tm8C4E/k17kXnTasl/L/8VldkugBfO0SQKj6TBdT88KOVeWVcngw+Opy8s85IRUIdUv Ycf4XHf7f1oPV43zFnaU3pnn6Jn9jC9BWIIklowolLYThARDa+QsgjFVdJUlyuRwr4Kw1sdGnok4c vWeWdge1z3OjN+OFM0OA==; 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 1hj1Ma-0000GI-0z; Thu, 04 Jul 2019 13:01:24 +0000 Received: from mail-lj1-x243.google.com ([2a00:1450:4864:20::243]) by bombadil.infradead.org with esmtps (Exim 4.92 #3 (Red Hat Linux)) id 1hj1ME-0008R1-NH for linux-arm-kernel@lists.infradead.org; Thu, 04 Jul 2019 13:01:04 +0000 Received: by mail-lj1-x243.google.com with SMTP id 131so6103831ljf.4 for ; Thu, 04 Jul 2019 06:01:02 -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 :mime-version:content-transfer-encoding; bh=bILvk7zww/MHzS0Ry8ydW5zQpvSdBYxtDr+zE8tM+sU=; b=jMNp8dC0A+sar0j+Ndv2aZcg+m3SjKWN/qql2KbBPlRWm3MHYJ8akksCCNPYMD8J4l 4OPaCADR1zli5dsaAtbhCQFInApBlhTWZVZWUyGWgByqiH5/WIkVE/E/r65y9UlAYyCp cEGYQ2Fz27/v/Y6QegXtdZFNfg6XEyTzebOIe0OO/2xKABH9tD1UQJbDPwXD3wQ71wqY 2/nhBLK5ntmjo2Ubnf5i6xQi7ZglF7sIHR5IArXx4eeCG4OwmhYKnMn+k8hVtdhn0eWi M5DQ9Z0ET1yMFygcSBJ73IKC2JVjIatVm8U60D9Bvzx8GDItnOPCJtwcfmo4H+vlQI36 NInQ== 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=bILvk7zww/MHzS0Ry8ydW5zQpvSdBYxtDr+zE8tM+sU=; b=KwtZxOjAYGTXB8YRx1Vfy/x2kzbPtWBNhCfJp8XGkTJfBE6Q2Ucu1Cb1rjoMvXf/mv RrdxwC/Oy8WrYFffFYw8fQrcF++OdlaUGn2laGnAP6AjDa8sMGtiqK5f9uetpTNORlix d7gnrquOBJ8zZbAIPd8FFGuzDgx4jiRRImjMvR66jk4K+dDJ0sbNOd7liN4XoC5BsUQh IjkAno8eogqXNDpR2seQbo/m90bDxBCJgqE0JAlPKLxKIp5OhuSAKKQ4kBH6ajXtBQQK NRroQUdlONKc3aFQxwaixcb844KpJMCyt+5SPjRIU6pJsFdi00E8pg1J4PU7Nk0zkciL oSSQ== X-Gm-Message-State: APjAAAXuikkrRhbActm/gszFurb2x1vr9IRM7yUP5gDoT6C0nVuaK0aS RMge8N53AJTh4IuBFXeS5RKDJ8M1 X-Google-Smtp-Source: APXvYqwqe6vwvjs4G2dhy4KMCh3DxCD4S1TAcq7FkC6YdKvcUHqEmzDueEOmUFZoOHStbQl7zawztg== X-Received: by 2002:a2e:88d3:: with SMTP id a19mr11892513ljk.32.1562245260861; Thu, 04 Jul 2019 06:01:00 -0700 (PDT) Received: from osv.localdomain ([89.175.180.246]) by smtp.gmail.com with ESMTPSA id h1sm485914lfj.21.2019.07.04.06.00.59 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 04 Jul 2019 06:01:00 -0700 (PDT) From: Sergey Organov To: linux-serial@vger.kernel.org Subject: [PATCH v3 1/7] serial: imx: fix locking in set_termios() Date: Thu, 4 Jul 2019 16:00:23 +0300 Message-Id: <1562245229-709-2-git-send-email-sorganov@gmail.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1562245229-709-1-git-send-email-sorganov@gmail.com> References: <20190614072801.3187-1-s.hauer@pengutronix.de> <1562245229-709-1-git-send-email-sorganov@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190704_060102_757255_4713756A X-CRM114-Status: GOOD ( 13.72 ) 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: Greg Kroah-Hartman , Sascha Hauer , Sergey Organov , NXP Linux Team , Pengutronix Kernel Team , =?utf-8?q?Uwe_Kleine-K?= =?utf-8?q?=C3=B6nig?= , linux-arm-kernel@lists.infradead.org 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. Reviewed-by: Sascha Hauer Tested-by: Sascha Hauer Uwe Kleine-König 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);