Received: by 2002:a05:6358:701b:b0:131:369:b2a3 with SMTP id 27csp4119119rwo; Tue, 25 Jul 2023 00:28:48 -0700 (PDT) X-Google-Smtp-Source: APBJJlHsUejxYAWpWlcvSRCY62gYQrV88N4CbGQlVqWJEctBkMiRG0hdSNmWSq53RwU91503q+fH X-Received: by 2002:a05:6512:3987:b0:4f8:56cd:da8c with SMTP id j7-20020a056512398700b004f856cdda8cmr8321500lfu.34.1690270128405; Tue, 25 Jul 2023 00:28:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690270128; cv=none; d=google.com; s=arc-20160816; b=sX7u7edTUX+jEkAZ7fuIQb31tpOiEaCGYwAVX601ex7dydUId0d/ifpYZVfS1D8y8w 8jndOvrnAHll4dE6zQp5XhVLLEHlCD31PhCLi4GQUPl4+v54huF/8HuBw615L+aFDNEG KDA/Yp8tGoO8umXe2z7jH8vcvNoip0iGLpiaWQVC8IAHLFxFJjRikPiluigDMan2y63q A/2t/iyISbBmeqJM956qtogV6I9oOWopK1cdQDagRT5Zx/gLNqFCtAv5Y+L6ldQ2I/Ot qZAdAbhzoxe577TXKW5xrOxW/LtRrhVJ6zc4xkmxn7/M8edQBIs5sdO630Bydyg/Xcfg zSAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=HJ/YKosE4LuZ4C3/DvnE/j3rn331/yq++de2hj4/TC0=; fh=BDDFGzP6DtAXrcnaNDA87KqSZT/NeOSnb+Ik/sQKV/g=; b=MMu+auU+wjRpOprQb4j0RO3thWhZW3SJWW1qTZ8gfJ4oOlwEJkrEgdq9wuta3qmK4U 1xnKiudsXqDyz5ixEjyIZidjuf/5Z5hzj0Z6aR0o17di/IkZjtNBGwnxw37BP3Ung2Q3 Pp9+pxpak7QfgX7e0NTLb0lDQrmDC+1YVG8GfHN7vQ8HARvPSqXxgcAJ+Gx34H7Hq6gY B9Tu7M/ApEiUklO0BK5BatDG//bKMUUDS7ADDiuNX+ytXjmyKcOQZvoJXYnluVju/d4l eqT/qvE1XiTiE3ozBminnRFwkQBwLCB6vA3Y4erGgLGw/icCXtvXme9B6EQO/6v3yCBS e/OA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v19-20020a056402185300b0052229e936c9si3327839edy.648.2023.07.25.00.28.23; Tue, 25 Jul 2023 00:28:48 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231472AbjGYGl7 (ORCPT + 99 others); Tue, 25 Jul 2023 02:41:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230187AbjGYGl5 (ORCPT ); Tue, 25 Jul 2023 02:41:57 -0400 Received: from SHSQR01.spreadtrum.com (unknown [222.66.158.135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44416116; Mon, 24 Jul 2023 23:41:55 -0700 (PDT) Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTP id 36P6f7i5071057; Tue, 25 Jul 2023 14:41:07 +0800 (+08) (envelope-from Chunyan.Zhang@unisoc.com) Received: from SHDLP.spreadtrum.com (bjmbx02.spreadtrum.com [10.0.64.8]) by dlp.unisoc.com (SkyGuard) with ESMTPS id 4R96p358q2z2K4D5v; Tue, 25 Jul 2023 14:39:39 +0800 (CST) Received: from ubt.spreadtrum.com (10.0.73.70) by BJMBX02.spreadtrum.com (10.0.64.8) with Microsoft SMTP Server (TLS) id 15.0.1497.23; Tue, 25 Jul 2023 14:41:04 +0800 From: Chunyan Zhang To: Greg Kroah-Hartman , Jiri Slaby CC: , Baolin Wang , Orson Zhai , Chunyan Zhang , Chunyan Zhang , LKML Subject: [PATCH V3 1/2] serial: sprd: Assign sprd_port after initialized to avoid wrong access Date: Tue, 25 Jul 2023 14:40:52 +0800 Message-ID: <20230725064053.235448-1-chunyan.zhang@unisoc.com> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.0.73.70] X-ClientProxiedBy: SHCAS03.spreadtrum.com (10.0.1.207) To BJMBX02.spreadtrum.com (10.0.64.8) X-MAIL: SHSQR01.spreadtrum.com 36P6f7i5071057 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The global pointer 'sprd_port' may not zero when sprd_probe returns failure, that is a risk for sprd_port to be accessed afterward, and may lead to unexpected errors. For example: There are two UART ports, UART1 is used for console and configured in kernel command line, i.e. "console="; The UART1 probe failed and the memory allocated to sprd_port[1] was released, but sprd_port[1] was not set to NULL; In UART2 probe, the same virtual address was allocated to sprd_port[2], and UART2 probe process finally will go into sprd_console_setup() to register UART1 as console since it is configured as preferred console (filled to console_cmdline[]), but the console parameters (sprd_port[1]) belong to UART2. So move the sprd_port[] assignment to where the port already initialized can avoid the above issue. Fixes: b7396a38fb28 ("tty/serial: Add Spreadtrum sc9836-uart driver support") Signed-off-by: Chunyan Zhang --- V3: - Call uart_unregister_driver() only when the 'sprd_ports_num' decreases to 0; - Add calling sprd_rx_free_buf() instread of sprd_remove() under clean_up lable. V2: - Leave sprd_remove() to keep the unrelated code logic the same. --- drivers/tty/serial/sprd_serial.c | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/drivers/tty/serial/sprd_serial.c b/drivers/tty/serial/sprd_serial.c index b58f51296ace..fc1377029021 100644 --- a/drivers/tty/serial/sprd_serial.c +++ b/drivers/tty/serial/sprd_serial.c @@ -1106,7 +1106,7 @@ static bool sprd_uart_is_console(struct uart_port *uport) static int sprd_clk_init(struct uart_port *uport) { struct clk *clk_uart, *clk_parent; - struct sprd_uart_port *u = sprd_port[uport->line]; + struct sprd_uart_port *u = container_of(uport, struct sprd_uart_port, port); clk_uart = devm_clk_get(uport->dev, "uart"); if (IS_ERR(clk_uart)) { @@ -1149,22 +1149,22 @@ static int sprd_probe(struct platform_device *pdev) { struct resource *res; struct uart_port *up; + struct sprd_uart_port *sport; int irq; int index; int ret; index = of_alias_get_id(pdev->dev.of_node, "serial"); - if (index < 0 || index >= ARRAY_SIZE(sprd_port)) { + if (index < 0 || index >= UART_NR_MAX) { dev_err(&pdev->dev, "got a wrong serial alias id %d\n", index); return -EINVAL; } - sprd_port[index] = devm_kzalloc(&pdev->dev, sizeof(*sprd_port[index]), - GFP_KERNEL); - if (!sprd_port[index]) + sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL); + if (!sport) return -ENOMEM; - up = &sprd_port[index]->port; + up = &sport->port; up->dev = &pdev->dev; up->line = index; up->type = PORT_SPRD; @@ -1195,7 +1195,7 @@ static int sprd_probe(struct platform_device *pdev) * Allocate one dma buffer to prepare for receive transfer, in case * memory allocation failure at runtime. */ - ret = sprd_rx_alloc_buf(sprd_port[index]); + ret = sprd_rx_alloc_buf(sport); if (ret) return ret; @@ -1206,14 +1206,23 @@ static int sprd_probe(struct platform_device *pdev) return ret; } } + sprd_ports_num++; + sprd_port[index] = sport; ret = uart_add_one_port(&sprd_uart_driver, up); if (ret) - sprd_remove(pdev); + goto clean_port; platform_set_drvdata(pdev, up); + return 0; + +clean_port: + sprd_port[index] = NULL; + if (--sprd_ports_num == 0) + uart_unregister_driver(&sprd_uart_driver); + sprd_rx_free_buf(sport); return ret; } -- 2.41.0