Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp35029138rwd; Mon, 10 Jul 2023 01:15:27 -0700 (PDT) X-Google-Smtp-Source: APBJJlGRzOo+tXZPzdeuDnXEPDUESwTNXkhlKX2xH44TwZ6gMH27W9UPl9zG2iojGOaO7I8nX0gr X-Received: by 2002:a17:907:2ce1:b0:98d:4b97:acc8 with SMTP id hz1-20020a1709072ce100b0098d4b97acc8mr19321251ejc.25.1688976927140; Mon, 10 Jul 2023 01:15:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688976927; cv=none; d=google.com; s=arc-20160816; b=KFkpS+2ToqaregTcnIlpJ6ZYzdWZVP5EzeHvcvkvMQnaQ/q/R+X3Hplw1/imEPv3UW dE+hZxP1r1GqKheeUkwJ++1JPAjLdX+vaizbL+kKQxWvn7/OvYfaDw5VWHLlOT7oDrGL rXbCf9lqlL+dSlkSnyYXGcXWRs/W7P1Ql34ycrDC5H9w9a0DaQy8AONaeKkLXaTHxO+W OotMnIE5fn83p8V70U6Xh6pOe6QQa7n0I4k1HDLFAHnbVlcTq6g9dsQUlfTTDAAL9mMQ ngFR9ElkSGf2c+vTSTHUE+7wHxSxFuYLN3zf85VD7ga/Re6pEpRrL5+4vczEbKtxb6NM 5VUQ== 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=q3FerMjlChr8QBQYZIeZ/Y1pI39Gydm3tdL26CMdabs=; fh=BDDFGzP6DtAXrcnaNDA87KqSZT/NeOSnb+Ik/sQKV/g=; b=XlB252vbBJ1Kx05OHjRWbmhIG8sU3HCiPl6PLzCxaHy2kJN4mRK8eJcC3O+C06jTQZ jSFW5wAG4o0v2WyssrwsD/GwAyvrMXHOa5RiztRK1JJI9P66wQxdBiT6m9lIL4YfD9Mx cIHR9+K4iwlFCkuLiRpIeZyKm8gu1AwxWP4znXiyOPEg48izMqkGOMUqGc0xgjsjjCpb Bs21F2i3/q/4IDz+wqU639b1COdXMlQIQGH4e0XyX5f7GvVHS3HXnOS926um0z/83xs3 QOyrjBmhOOpxuK43ojfWYQMYpOYpWoXqcgd1B/bK+OApe3H70h1g0H3ojnNcgU/oxNka XC+A== 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 k15-20020a1709065fcf00b0099307f52a2esi7603055ejv.1003.2023.07.10.01.15.02; Mon, 10 Jul 2023 01:15:27 -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 S232248AbjGJIEj (ORCPT + 99 others); Mon, 10 Jul 2023 04:04:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34330 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232143AbjGJIEh (ORCPT ); Mon, 10 Jul 2023 04:04:37 -0400 Received: from SHSQR01.spreadtrum.com (mx1.unisoc.com [222.66.158.135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3C1B8D7; Mon, 10 Jul 2023 01:04:34 -0700 (PDT) Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTP id 36A83rnY045515; Mon, 10 Jul 2023 16:03:54 +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 4QzxM35jjNz2LcN4n; Mon, 10 Jul 2023 16:02:55 +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; Mon, 10 Jul 2023 16:03:52 +0800 From: Chunyan Zhang To: Greg Kroah-Hartman , Jiri Slaby CC: , Baolin Wang , Orson Zhai , Chunyan Zhang , Chunyan Zhang , LKML Subject: [PATCH 1/2] serial: sprd: Assign sprd_port after initialized to avoid wrong access Date: Mon, 10 Jul 2023 16:03:47 +0800 Message-ID: <20230710080348.4137875-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: SHCAS01.spreadtrum.com (10.0.1.201) To BJMBX02.spreadtrum.com (10.0.64.8) X-MAIL: SHSQR01.spreadtrum.com 36A83rnY045515 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE,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' maybe not zero when sprd_probe returns fail, that is a risk for sprd_port to be accessed afterward, and will lead unexpected errors. For example: There're two UART ports, UART1 is used for console and configured in kernel command line, i.e. "console="; The UART1 probe fail 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]) actually belongs 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 --- drivers/tty/serial/sprd_serial.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/drivers/tty/serial/sprd_serial.c b/drivers/tty/serial/sprd_serial.c index b58f51296ace..942808517393 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; @@ -1208,12 +1208,20 @@ static int sprd_probe(struct platform_device *pdev) } 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; + sprd_ports_num--; + uart_unregister_driver(&sprd_uart_driver); return ret; } -- 2.41.0