Message ID | 20230601141445.11321-1-tony@atomide.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp368652vqr; Thu, 1 Jun 2023 07:22:19 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7fA9XwlC5fA0y8OSGbY2L9qhaiG58gEjuxmasemd/Dzjqs/Y7bwDWXqF0Go8Hj/Z3f6/d4 X-Received: by 2002:a05:6a20:8e1d:b0:10e:96b5:45fa with SMTP id y29-20020a056a208e1d00b0010e96b545famr6644075pzj.43.1685629337658; Thu, 01 Jun 2023 07:22:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685629337; cv=none; d=google.com; s=arc-20160816; b=amKsD3ajOciVTK9MvoiesTiE+YHLdUa/t6ftbYHV1wttcx7omHPu90QeRi0sL1dnXJ gmH6mRN43AbHAtzyOa0+MHwoa738YXWQqNoNLrsm/rCZalIYYKzo/8+db7ufkFE1P9GV 31YHEa3wAGzAo0ACQB1Bi7sHk1LNN9CXgv+n+q9sDGneKsqUEytoFuG6d0E6KcOxutkC ty2ZfpsBFFAXSe1w7b7B6z0cS4KqacNgI5IrYIGci1DZUdL8595JTk9Nx6OWmgQBmAgi 72N93p9J0MDfFmiiI9vi8EcafgjdZ78WOfykSCqmVXslGSkh/a0NtjzYLtYhxGxjuwij RcPQ== 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=ZRbsWXvXAVBIUcOobYQgtCNBMRMUYMMNhGMig065jQc=; b=JzEjkswaa9QOPI3/CeqPc1I7VAuXEmhjEHCsSYYppHmwFYCQqxO0Oo1Xl7Wl0Wyoe2 qpWqZI1JZSyfNqJ1Z5B3n4dua0AhkBWQqH8GdOkmPI2v2sllnk6xXLXkuhlTUj+zSLaK EhqKMTIrgbBGp6LJoXvWcX2dcXi6UTVSOh51kh99GTiVml5wiQG/w8DHVxb1tm9Hz51J WizjSOiszu5ci8CKR86ns+DFwfjkYySgYIhShNi+wxATDZm3SvKh62QhJQ/8oc16QAFk yq47ZK/JnQD5Yle9g6GXvLlNtgcLv6z4jcJrrcQhWYbBIkU8pGmzRKBZtaPYzsJkDEHg UMSQ== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n11-20020a170902d2cb00b001ab23ced2d2si7847plc.613.2023.06.01.07.22.04; Thu, 01 Jun 2023 07:22:17 -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 S234212AbjFAOPI (ORCPT <rfc822;limurcpp@gmail.com> + 99 others); Thu, 1 Jun 2023 10:15:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234107AbjFAOPB (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 1 Jun 2023 10:15:01 -0400 Received: from muru.com (muru.com [72.249.23.125]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 90E058E; Thu, 1 Jun 2023 07:15:00 -0700 (PDT) Received: from hillo.muru.com (localhost [127.0.0.1]) by muru.com (Postfix) with ESMTP id 0386F80F1; Thu, 1 Jun 2023 14:14:57 +0000 (UTC) From: Tony Lindgren <tony@atomide.com> To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>, Jiri Slaby <jirislaby@kernel.org>, Andy Shevchenko <andriy.shevchenko@linux.intel.com> Cc: Andy Shevchenko <andriy.shevchenko@intel.com>, Dhruva Gole <d-gole@ti.com>, =?utf-8?q?Ilpo_J=C3=A4rvinen?= <ilpo.jarvinen@linux.intel.com>, John Ogness <john.ogness@linutronix.de>, Johan Hovold <johan@kernel.org>, Sebastian Andrzej Siewior <bigeasy@linutronix.de>, Vignesh Raghavendra <vigneshr@ti.com>, linux-omap@vger.kernel.org, Marek Szyprowski <m.szyprowski@samsung.com>, linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] serial: core: Fix probing serial_base_bus devices Date: Thu, 1 Jun 2023 17:14:44 +0300 Message-Id: <20230601141445.11321-1-tony@atomide.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767510468349922944?= X-GMAIL-MSGID: =?utf-8?q?1767510468349922944?= |
Series |
serial: core: Fix probing serial_base_bus devices
|
|
Commit Message
Tony Lindgren
June 1, 2023, 2:14 p.m. UTC
If a physical serial port device driver uses arch_initcall() we fail to
probe the serial_base_bus devices and the serial port tx fails. This is
because as serial_base_bus uses module_initcall().
Let's fix the issue by changing serial_base_bus to use arch_initcall().
Let's also return an error if a driver attempts to call uart_add_one_port()
too early.
Reported-by: Marek Szyprowski <m.szyprowski@samsung.com>
Closes: https://lore.kernel.org/linux-serial/20230601132012.GB14287@atomide.com/T/#m6a40440fc04d551d27b147da8602e065c982a115
Fixes: 84a9582fd203 ("serial: core: Start managing serial controllers to enable runtime PM")
Signed-off-by: Tony Lindgren <tony@atomide.com>
---
drivers/tty/serial/serial_base_bus.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
Comments
On Thu, Jun 01, 2023 at 05:14:44PM +0300, Tony Lindgren wrote: > If a physical serial port device driver uses arch_initcall() we fail to > probe the serial_base_bus devices and the serial port tx fails. This is > because as serial_base_bus uses module_initcall(). > > Let's fix the issue by changing serial_base_bus to use arch_initcall(). This will only work if the linking order is such that this will always come before the drivers. Is that the case here? thanks, greg k-h
* Greg Kroah-Hartman <gregkh@linuxfoundation.org> [230601 14:21]: > On Thu, Jun 01, 2023 at 05:14:44PM +0300, Tony Lindgren wrote: > > If a physical serial port device driver uses arch_initcall() we fail to > > probe the serial_base_bus devices and the serial port tx fails. This is > > because as serial_base_bus uses module_initcall(). > > > > Let's fix the issue by changing serial_base_bus to use arch_initcall(). > > This will only work if the linking order is such that this will always > come before the drivers. Is that the case here? I guess based on Makefile. And also if serial drivers are modules as we export uart_add_one_port() from serial_base.ko. But yeah this is pretty fragile potentially. Hmm maybe we could keep module_init() and then also call serial_base_init() on uart_add_one_port() path if not yet initialized? Probably the module_init() should be still there for case when no serial port device drivers are loaded and serial_base is unloaded.. Regards, Tony
On 01.06.2023 16:21, Greg Kroah-Hartman wrote: > On Thu, Jun 01, 2023 at 05:14:44PM +0300, Tony Lindgren wrote: >> If a physical serial port device driver uses arch_initcall() we fail to >> probe the serial_base_bus devices and the serial port tx fails. This is >> because as serial_base_bus uses module_initcall(). >> >> Let's fix the issue by changing serial_base_bus to use arch_initcall(). > This will only work if the linking order is such that this will always > come before the drivers. Is that the case here? Yes, serial_base_bus is linked as a second object, just after the serial_core. Device drivers come later. Best regards
On Thu, Jun 01, 2023 at 05:09:52PM +0200, Marek Szyprowski wrote: > On 01.06.2023 16:21, Greg Kroah-Hartman wrote: > > On Thu, Jun 01, 2023 at 05:14:44PM +0300, Tony Lindgren wrote: > >> If a physical serial port device driver uses arch_initcall() we fail to > >> probe the serial_base_bus devices and the serial port tx fails. This is > >> because as serial_base_bus uses module_initcall(). > >> > >> Let's fix the issue by changing serial_base_bus to use arch_initcall(). > > This will only work if the linking order is such that this will always > > come before the drivers. Is that the case here? > > Yes, serial_base_bus is linked as a second object, just after the > serial_core. Device drivers come later. Oh good, I guess it wouldn't work at all as the serial_core is needed by all of those drivers first too, so this should work, thanks for checking. greg k-h
On Thu, Jun 01, 2023 at 06:07:06PM +0300, Tony Lindgren wrote: > * Greg Kroah-Hartman <gregkh@linuxfoundation.org> [230601 14:21]: > > On Thu, Jun 01, 2023 at 05:14:44PM +0300, Tony Lindgren wrote: > > > If a physical serial port device driver uses arch_initcall() we fail to > > > probe the serial_base_bus devices and the serial port tx fails. This is > > > because as serial_base_bus uses module_initcall(). > > > > > > Let's fix the issue by changing serial_base_bus to use arch_initcall(). > > > > This will only work if the linking order is such that this will always > > come before the drivers. Is that the case here? > > I guess based on Makefile. And also if serial drivers are modules as we > export uart_add_one_port() from serial_base.ko. But yeah this is pretty > fragile potentially. It's fine, and normal, the Makefile is the ordering here so all is good. > Hmm maybe we could keep module_init() and then also call serial_base_init() > on uart_add_one_port() path if not yet initialized? > > Probably the module_init() should be still there for case when no serial > port device drivers are loaded and serial_base is unloaded.. I'll leave this as-is, it looks correct, and I'll queue it up now, thanks. greg k-h
* Marek Szyprowski <m.szyprowski@samsung.com> [230601 15:09]: > On 01.06.2023 16:21, Greg Kroah-Hartman wrote: > > On Thu, Jun 01, 2023 at 05:14:44PM +0300, Tony Lindgren wrote: > >> If a physical serial port device driver uses arch_initcall() we fail to > >> probe the serial_base_bus devices and the serial port tx fails. This is > >> because as serial_base_bus uses module_initcall(). > >> > >> Let's fix the issue by changing serial_base_bus to use arch_initcall(). > > This will only work if the linking order is such that this will always > > come before the drivers. Is that the case here? > > Yes, serial_base_bus is linked as a second object, just after the > serial_core. Device drivers come later. If we don't want to rely on the Makefile order here, we could do something like the patch below. Regards, Tony 8< --------------------- diff --git a/drivers/tty/serial/serial_base_bus.c b/drivers/tty/serial/serial_base_bus.c --- a/drivers/tty/serial/serial_base_bus.c +++ b/drivers/tty/serial/serial_base_bus.c @@ -11,12 +11,18 @@ #include <linux/container_of.h> #include <linux/device.h> #include <linux/module.h> +#include <linux/mutex.h> #include <linux/serial_core.h> #include <linux/slab.h> #include <linux/spinlock.h> #include "serial_base.h" +static bool serial_base_initialized; +static DEFINE_MUTEX(serial_base_lock); + +static int serial_base_init(void); + static int serial_base_match(struct device *dev, struct device_driver *drv) { int len = strlen(drv->name); @@ -48,6 +54,12 @@ static int serial_base_device_init(struct uart_port *port, void (*release)(struct device *dev), int id) { + /* + * Initialize bus if not yet initialized. Some drivers are using + * arch_initcall() + */ + serial_base_init(); + device_initialize(dev); dev->type = type; dev->parent = parent_dev; @@ -163,6 +175,14 @@ static int serial_base_init(void) { int ret; + mutex_lock(&serial_base_lock); + + /* Also called from serial_base_device_init() in some cases */ + if (serial_base_initialized) { + ret = 0; + goto out_unlock; + } + ret = bus_register(&serial_base_bus_type); if (ret) return ret; @@ -175,6 +195,10 @@ static int serial_base_init(void) if (ret) goto err_ctrl_exit; + serial_base_initialized = true; + + mutex_unlock(&serial_base_lock); + return 0; err_ctrl_exit: @@ -183,15 +207,21 @@ static int serial_base_init(void) err_bus_unregister: bus_unregister(&serial_base_bus_type); +out_unlock: + mutex_unlock(&serial_base_lock); + return ret; } module_init(serial_base_init); static void serial_base_exit(void) { + mutex_lock(&serial_base_lock); serial_base_port_exit(); serial_base_ctrl_exit(); bus_unregister(&serial_base_bus_type); + serial_base_initialized = false; + mutex_unlock(&serial_base_lock); } module_exit(serial_base_exit);
diff --git a/drivers/tty/serial/serial_base_bus.c b/drivers/tty/serial/serial_base_bus.c --- a/drivers/tty/serial/serial_base_bus.c +++ b/drivers/tty/serial/serial_base_bus.c @@ -17,6 +17,8 @@ #include "serial_base.h" +static bool serial_base_initialized; + static int serial_base_match(struct device *dev, struct device_driver *drv) { int len = strlen(drv->name); @@ -48,6 +50,11 @@ static int serial_base_device_init(struct uart_port *port, void (*release)(struct device *dev), int id) { + if (!serial_base_initialized) { + dev_err(port->dev, "uart_add_one_port() called before arch_initcall()?\n"); + return -EPROBE_DEFER; + } + device_initialize(dev); dev->type = type; dev->parent = parent_dev; @@ -175,6 +182,8 @@ static int serial_base_init(void) if (ret) goto err_ctrl_exit; + serial_base_initialized = true; + return 0; err_ctrl_exit: @@ -185,7 +194,7 @@ static int serial_base_init(void) return ret; } -module_init(serial_base_init); +arch_initcall(serial_base_init); static void serial_base_exit(void) {