Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp690424pxb; Tue, 5 Apr 2022 18:56:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxbQm1C+QWQ5rRMFVWXOhUB4FH2gPoMDVd5k+JkuhUlNwte+U63NearYwrQZFIcsMwMYIcr X-Received: by 2002:a17:902:ef48:b0:156:8ff6:e2cc with SMTP id e8-20020a170902ef4800b001568ff6e2ccmr6416028plx.130.1649210184827; Tue, 05 Apr 2022 18:56:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649210184; cv=none; d=google.com; s=arc-20160816; b=kILQRxD64KJvbuFg6eJe2OJ7baG24IRGF3nN6q4fH61FXCE8yzg0QYeDm0INKp9wxI VI2fTz1ysgsu3icwI8ixJKd6d5Cs0e1KcKXGd+q5WuLDE1LkKxTbZRf0iteg6jOOosnV SPPiOQOAULfNeykQIDFPxYdjgoCZ53Crz1HAHa84ZPLDMpIf4Ty4zK01mruDqTPjfjZm QJ8fW9JTz4L8Xl2/njuzyQrQK/aoX2+X2kpQb9+UZurTVKhnxQgRm3J0QoMhCV2llVXB Kpayq2pBFHGPq+3V+LznHjvbyfuzxJAsDeW6n6IfGbK1SLLjKWVLSNhOTwXqGflgwSDt gqug== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=J7HzFKBMKZ17F2HKSLUftJ80COOtvxqbRVToBVH0qfk=; b=QbA4b6rfyyCv0gLXDGITE8nUej3sWn6KeQGSmPyfWQtwkMXxXRZzzikr5km4EY1Ed3 g57Vz9HBOxfQHlzNbetA8oOGol1iieTDvgRcA47cNzY3nHL4DFeBKVwhNFXnLDcp7dm7 bz4v1ROFihMKSxND0cx/k+OsoMZ9euyRkRtlx5X5nZzgSXFqjhFzu6XAK8jLN48ml18S DSdWtUeukcFVDOVLbbM6smsjFze+a8L9R8M1/Xx8qd0VMiUpmf4txMJTzyZedAuvcEKu WqEmQCP4HAZrLFzfwd+4ScPeYlko8k+eEjYykKcrQNl9udMTSNFO5Wglu7GUtHfgl4La jOIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=FRu8R2S5; 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=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t70-20020a637849000000b003816043ee11si14839627pgc.6.2022.04.05.18.56.07; Tue, 05 Apr 2022 18:56:24 -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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=FRu8R2S5; 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=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244358AbiDENEP (ORCPT + 99 others); Tue, 5 Apr 2022 09:04:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51034 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238747AbiDEJQR (ORCPT ); Tue, 5 Apr 2022 05:16:17 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC392D1CED; Tue, 5 Apr 2022 02:01:50 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 9D454B81A22; Tue, 5 Apr 2022 09:01:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E8F1EC385A0; Tue, 5 Apr 2022 09:01:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649149308; bh=nHD0pg0JPdDBQlubw4lET+r6B3XXrvHryuZbDJCYbcE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FRu8R2S5m9o8Lap9wGsgH0GjpbT83S8H0v4SgFeclbSf7cLx5+4eGdEe9e7f9C92G oX8JruHOsX+tWzuUGflaWMwukwtd06l0j+RZ7jAmGTqp6OBaa5xvJ6gnHYWRSM9JzP Ie5blp7kR+WcwN4dDyOGo5k9PWx/3smAYE7sUfBE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , Sasha Levin Subject: [PATCH 5.16 0668/1017] serial: 8250: Fix race condition in RTS-after-send handling Date: Tue, 5 Apr 2022 09:26:21 +0200 Message-Id: <20220405070414.110149314@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070354.155796697@linuxfoundation.org> References: <20220405070354.155796697@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 From: Uwe Kleine-König [ Upstream commit dedab69fd650ea74710b2e626e63fd35584ef773 ] Set em485->active_timer = NULL isn't always enough to take out the stop timer. While there is a check that it acts in the right state (i.e. waiting for RTS-after-send to pass after sending some chars) but the following might happen: - CPU1: some chars send, shifter becomes empty, stop tx timer armed - CPU0: more chars send before RTS-after-send expired - CPU0: shifter empty irq, port lock taken - CPU1: tx timer triggers, waits for port lock - CPU0: em485->active_timer = &em485->stop_tx_timer, hrtimer_start(), releases lock() - CPU1: get lock, see em485->active_timer == &em485->stop_tx_timer, tear down RTS too early This fix bases on research done by Steffen Trumtrar. Fixes: b86f86e8e7c5 ("serial: 8250: fix potential deadlock in rs485-mode") Signed-off-by: Uwe Kleine-König Link: https://lore.kernel.org/r/20220215160236.344236-1-u.kleine-koenig@pengutronix.de Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/tty/serial/8250/8250_port.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/drivers/tty/serial/8250/8250_port.c b/drivers/tty/serial/8250/8250_port.c index b34e84695c8c..344fbe7426f6 100644 --- a/drivers/tty/serial/8250/8250_port.c +++ b/drivers/tty/serial/8250/8250_port.c @@ -1623,6 +1623,18 @@ static inline void start_tx_rs485(struct uart_port *port) struct uart_8250_port *up = up_to_u8250p(port); struct uart_8250_em485 *em485 = up->em485; + /* + * While serial8250_em485_handle_stop_tx() is a noop if + * em485->active_timer != &em485->stop_tx_timer, it might happen that + * the timer is still armed and triggers only after the current bunch of + * chars is send and em485->active_timer == &em485->stop_tx_timer again. + * So cancel the timer. There is still a theoretical race condition if + * the timer is already running and only comes around to check for + * em485->active_timer when &em485->stop_tx_timer is armed again. + */ + if (em485->active_timer == &em485->stop_tx_timer) + hrtimer_try_to_cancel(&em485->stop_tx_timer); + em485->active_timer = NULL; if (em485->tx_stopped) { -- 2.34.1