Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp961663pxb; Wed, 6 Apr 2022 05:24:34 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxM33P7gT1u95YPHgJ6vCmbMhSzpjBipqFsXfyeLpX3UsXu/mWlA/xAcZLm1JgJ0gIcRrvz X-Received: by 2002:a63:4918:0:b0:399:569c:e408 with SMTP id w24-20020a634918000000b00399569ce408mr6836474pga.180.1649247874658; Wed, 06 Apr 2022 05:24:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649247874; cv=none; d=google.com; s=arc-20160816; b=0Y5xs8BBLJ5jTu5lS+c7C78lUX7jtD5i8r1rGAlVV9D6Vy9dwLbendvqbpM4nGtapa FnYCeakNF/eUpLc4pUJ7r0iIMt92JZSGDnzd/wN7se1taU5mQCC6OpOX+1QsHBxrMLfc o8eKbMsxdu+3RhKE50DefrOCmgzLf9WhS/HJ7BJdBGhvvL2Bm6NDipCpS2y31rfpiv3l NRnZJRZabBXiuhtubWu8+VXjkJxQBE+sfZf4DaVVPNA7Dd/yLTmvL1ISQS52SwWHHQm8 ykomKOHXvupzHA52Yfv6FnJejNf8jCx1DYMXJ9mwFsiQD/4ULScfxb++IMlIrnWUN3IQ WBoA== 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=cfvBaFf8CtPlQMw2R6HgRrXgrrNdcblXX7bIJFEgLxg=; b=JrCJ2mdo+67yd01SS7I5J+3ojqxiiTPehbCud1fexOFnrSWHSVmZSkOTQeEuVxvAcE pUNkXPU0h1PWPGhmiQ/G0AaqTiSysF+sTnPALj1d5zjRzTJw+oF9DSA0sg65bjcX261Q 3Ko/XUe3cWQLbyAFefvDX+VYosLBW8VjYB5A0UFXVUBd8eKGAvLlZUW+MByVoDZYcJrF MHQWgyKBw8lwmSxEvM/iC8ejmI8SMBieS9IEU1qQnasNELsKYD5ru5GW8kP/ZMPDB+EQ IPWJ//hU/ABRFe43kDiEEGrT+JZCg/VMbiNlGLhqXHL1/gPGgZi9rLlUM61uWvD9JxRd PhJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Gy7Tuc8v; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id l3-20020a63ba43000000b00382b21d7ab8si15999934pgu.761.2022.04.06.05.24.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 05:24:34 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Gy7Tuc8v; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7D60560C6FF; Wed, 6 Apr 2022 04:34:10 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354272AbiDEUDd (ORCPT + 99 others); Tue, 5 Apr 2022 16:03:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242193AbiDEKcm (ORCPT ); Tue, 5 Apr 2022 06:32:42 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1EF8DE919; Tue, 5 Apr 2022 03:18:44 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 7E2D96176C; Tue, 5 Apr 2022 10:18:44 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 88835C385A1; Tue, 5 Apr 2022 10:18:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649153923; bh=SHgMIsg7sWxKjDxzCiPkM4eU2XEZLf2akJirYWO4Tm4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Gy7Tuc8vJl7RXwkXtiuc5Lo6csfFW1SHYXcy/4OybqOJyTNDnajoKPfevFwArtrKK iyRr5Pe39X9icOLGMIyVAoUEaUOjeDjEyeNKyHKH/vq722JkE9MGyR+sfSRCqLBLUS 5SQqzQy56RYIO1p4XoGj8onT/jBgaPtseyNkSfRI= 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.10 395/599] serial: 8250: Fix race condition in RTS-after-send handling Date: Tue, 5 Apr 2022 09:31:29 +0200 Message-Id: <20220405070310.585238979@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070258.802373272@linuxfoundation.org> References: <20220405070258.802373272@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=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 7c07ebb37b1b..1733f03a7da7 100644 --- a/drivers/tty/serial/8250/8250_port.c +++ b/drivers/tty/serial/8250/8250_port.c @@ -1620,6 +1620,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