From patchwork Thu Jan 12 16:12:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 42520 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp3970590wrt; Thu, 12 Jan 2023 08:17:47 -0800 (PST) X-Google-Smtp-Source: AMrXdXsuzzaMIYhJ+y+QR7TB4YBL0ruM1+zdEVEF/4JzvOdY8ng8jzrPZ2OYvbm1Sl6BoWXeT3ei X-Received: by 2002:a17:906:b108:b0:843:a9fe:f115 with SMTP id u8-20020a170906b10800b00843a9fef115mr69138627ejy.32.1673540267771; Thu, 12 Jan 2023 08:17:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673540267; cv=none; d=google.com; s=arc-20160816; b=lZCPJQ5z9pWc4feyH7GF+0INt199HzbmxMqbDJeL5COMVaVeOuMxfoozcGHgX38E1c 7lryDAZcIX9tou3otliIFaRElBHkkmLfklJEhB4gEFadSKpKeJmCD/b65XvJmBLMMjwF tBmcCeFXpWA2/t3SY7msTK24r8ZUhZsoZFdrYiaKpE0rdolo/364fx9ghSzxyWoWu+1e K6LNSrVF164o4/1+ubt0ytyWZtIy3berF1/dHIG4eTdnU9/r1c6C/p1qwwYekBKByUg+ C3hqWvhlWxisiG1445RFMTxr+qfvQRgKSaH60DDyHy9nzma3GTDYZAmR6PwQeOn8ir/P Kd0w== 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:dkim-signature:dkim-signature:from; bh=Zio+51P9JQjITYvWQp49hE5xV03adpaYIVB4A9ebG0s=; b=sgHIYBPLnycJ9nKwlBuLEmREPKLBxMRwOoybTNozfPCkE7eLCpV4EpKzdr89BLjZxM c2GhTB4skhwgO4Zkk4w/dXMPo7XZhArHkXu3rYWyX1g9bsF7k5xdzFaLsU3Lftj6F2un e+CfWwiIi3h5ywLD9LWUjzxlNzgHabT52zJ4au0MXwchKaLapqO3z63ZrottZ+X/gUdn bqHNo+0aeg99e82XpQNLIetldmM31TAxLxAGzCICSV0tDyKrrTQLADDSg8hzI/oUufwO tpg1aGxh63AdaLFJGFxAPir8sCHTQoSQIopVtYgEE8INDDHUGN4R1Xyd0+mt/gExE1zu coMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=oeH67roc; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=2tI6D3Ly; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ji6-20020a170907980600b00866bd1c81e9si2745827ejc.419.2023.01.12.08.17.22; Thu, 12 Jan 2023 08:17:47 -0800 (PST) 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; dkim=pass header.i=@linutronix.de header.s=2020 header.b=oeH67roc; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=2tI6D3Ly; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233686AbjALQQP (ORCPT + 99 others); Thu, 12 Jan 2023 11:16:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53682 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232596AbjALQPg (ORCPT ); Thu, 12 Jan 2023 11:15:36 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8025ADEC3; Thu, 12 Jan 2023 08:13:01 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1673539980; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Zio+51P9JQjITYvWQp49hE5xV03adpaYIVB4A9ebG0s=; b=oeH67rocXqFE0wOjpM0qYGrT7qnKT79gavwGqxgwH02OT4+XbIKGAuQ7c+UL7SEKDU8Z8w zOlZkgBo3zIgTEttTGKRcN7Bt77mZm5bKr3CMzaQkHO1Gcp6XgiX6Egg+VYRNifRAHtntR ZJwz0HgjlPX4oS3FddvKdyYV4h2XWdsFrCd+YL1jwzmEgucmsg9ufJpIEZ7L0X74lEDk78 mJW0NCIwfgaG5UWqGETAyZPkO2unqAVLIXuk7nfDuXdAcvs/RpH7KuIyMYKZnZ/AzyRM43 oSwSAyUdkRnP0WRG4Id+7gXZYlepdGWKYLqs6FRmjnMzLH0WtaAg0wjMe8o4Zg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1673539980; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Zio+51P9JQjITYvWQp49hE5xV03adpaYIVB4A9ebG0s=; b=2tI6D3LysuGwg+CiOyQT626xIs49+W49l8rirwzqVSC7m7rK/geYH9Se5JebkDWpEpo4yM lnE7YjwxKN9wVFDg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Jason Wessel , Daniel Thompson , Douglas Anderson , Greg Kroah-Hartman , Jiri Slaby , kgdb-bugreport@lists.sourceforge.net, linux-serial@vger.kernel.org Subject: [PATCH v2] tty: serial: kgdboc: fix mutex locking order for configure_kgdboc() Date: Thu, 12 Jan 2023 17:18:13 +0106 Message-Id: <20230112161213.1434854-1-john.ogness@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,INVALID_DATE_TZ_ABSURD, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754834159927269218?= X-GMAIL-MSGID: =?utf-8?q?1754834159927269218?= Several mutexes are taken while setting up console serial ports. In particular, the tty_port->mutex and @console_mutex are taken: serial_pnp_probe serial8250_register_8250_port uart_add_one_port (locks tty_port->mutex) uart_configure_port register_console (locks @console_mutex) In order to synchronize kgdb's tty_find_polling_driver() with register_console(), commit 6193bc90849a ("tty: serial: kgdboc: synchronize tty_find_polling_driver() and register_console()") takes the @console_mutex. However, this leads to the following call chain (with locking): platform_probe kgdboc_probe configure_kgdboc (locks @console_mutex) tty_find_polling_driver uart_poll_init (locks tty_port->mutex) uart_set_options This is clearly deadlock potential due to the reverse lock ordering. Since uart_set_options() requires holding @console_mutex in order to serialize early initialization of the serial-console lock, take the @console_mutex in uart_poll_init() instead of configure_kgdboc(). Since configure_kgdboc() was using @console_mutex for safe traversal of the console list, change it to use the SRCU iterator instead. Add comments to uart_set_options() kerneldoc mentioning that it requires holding @console_mutex (aka the console_list_lock). Fixes: 6193bc90849a ("tty: serial: kgdboc: synchronize tty_find_polling_driver() and register_console()") Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- drivers/tty/serial/kgdboc.c | 20 +++++--------------- drivers/tty/serial/serial_core.c | 5 +++++ 2 files changed, 10 insertions(+), 15 deletions(-) base-commit: b7bfaa761d760e72a969d116517eaa12e404c262 diff --git a/drivers/tty/serial/kgdboc.c b/drivers/tty/serial/kgdboc.c index a3ed9b34e2ab..7ce7bb164005 100644 --- a/drivers/tty/serial/kgdboc.c +++ b/drivers/tty/serial/kgdboc.c @@ -171,6 +171,7 @@ static int configure_kgdboc(void) int err = -ENODEV; char *cptr = config; struct console *cons; + int cookie; if (!strlen(config) || isspace(config[0])) { err = 0; @@ -189,20 +190,9 @@ static int configure_kgdboc(void) if (kgdboc_register_kbd(&cptr)) goto do_register; - /* - * tty_find_polling_driver() can call uart_set_options() - * (via poll_init) to configure the uart. Take the console_list_lock - * in order to synchronize against register_console(), which can also - * configure the uart via uart_set_options(). This also allows safe - * traversal of the console list. - */ - console_list_lock(); - p = tty_find_polling_driver(cptr, &tty_line); - if (!p) { - console_list_unlock(); + if (!p) goto noconfig; - } /* * Take console_lock to serialize device() callback with @@ -211,7 +201,8 @@ static int configure_kgdboc(void) */ console_lock(); - for_each_console(cons) { + cookie = console_srcu_read_lock(); + for_each_console_srcu(cons) { int idx; if (cons->device && cons->device(cons, &idx) == p && idx == tty_line) { @@ -219,11 +210,10 @@ static int configure_kgdboc(void) break; } } + console_srcu_read_unlock(cookie); console_unlock(); - console_list_unlock(); - kgdb_tty_driver = p; kgdb_tty_line = tty_line; diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c index b9fbbee598b8..ec874f3a567c 100644 --- a/drivers/tty/serial/serial_core.c +++ b/drivers/tty/serial/serial_core.c @@ -2212,6 +2212,9 @@ EXPORT_SYMBOL_GPL(uart_parse_options); * @parity: parity character - 'n' (none), 'o' (odd), 'e' (even) * @bits: number of data bits * @flow: flow control character - 'r' (rts) + * + * Locking: Caller must hold console_list_lock in order to serialize + * early initialization of the serial-console lock. */ int uart_set_options(struct uart_port *port, struct console *co, @@ -2619,7 +2622,9 @@ static int uart_poll_init(struct tty_driver *driver, int line, char *options) if (!ret && options) { uart_parse_options(options, &baud, &parity, &bits, &flow); + console_list_lock(); ret = uart_set_options(port, NULL, baud, parity, bits, flow); + console_list_unlock(); } out: mutex_unlock(&tport->mutex);