Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp947561pxb; Wed, 6 Apr 2022 05:04:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxAkEenwGGFMpJNPzjvr5+2t56ikmp2lVyD6AU8xm/au79e1zb1LvcPSjgoBVOsSQE1v+P4 X-Received: by 2002:a05:6638:14c6:b0:323:bb70:7053 with SMTP id l6-20020a05663814c600b00323bb707053mr4079423jak.141.1649246691152; Wed, 06 Apr 2022 05:04:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649246691; cv=none; d=google.com; s=arc-20160816; b=SiKQhPQjc0NQyenUZ52W6QOsWO6tJH9QEhodgJwVcz/125m5/cXngLkk3tLYe//SgU 1s3IafYfMF/V7ktD49/AyvKG2kW4D22nnd6hngmXZa07MKX+lQ5FezkvSuCg1ZJh/ndd ONQsxyOalU6gRiw7CtbryOVO6WLUFH80Yfhl/Gl36NYgX++7aclnWCyAWS5+w72AZ9O+ j4LSIzuO06yhG7SZRj/HiaaB2LI+FhOJQEVxvJaStqG5SparXnMEOadeF7aT+aySResm qYopoeZa3KMVY+F3xtFeaf8Hk1hIP8A39KA6ix2CPC1e48rnTeSmwMibtAuRf45pl4Xc eGlQ== 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=BWrn/deCFAZShlUH7u/aIV5JogVpmPJ7rSVY3m/XqMU=; b=tSjmmoe1eFb3Jg3jwgsRv7KdkrL43mkXnZYtJjsAFoXp6HaUM8dg2MQMVF3qP9A4Dq McX1UrS/9/Bvd9aVPQx57k6oQwM9MLlHDGhr7muCvFFdvwWtmdgdca1bqvwOSncqXlQQ 57D6tujvwUKqDYYmbidfmwHxgYmusOExR9AdWIajoMn5hojlyp4bsLx2Zi6Y2gh2bnVm A56lhHjyoAV5LaA7W+15Ye7i7wOBTPKTeohTdxZhGxdtQNA1GyfwoeZy5oSGA1Cor6Ny dapTPEHxosSi/3BUPW2NJw+AVPy/Mq+bQztxKn3fsbbU8HwqECS0WOOdM8JBFwA6kIEO 30hQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=wqQ0YLQQ; 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 v21-20020a05663812d500b0031a2a421606si10108607jas.40.2022.04.06.05.04.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 05:04:51 -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=wqQ0YLQQ; 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 AD1BE47F139; Wed, 6 Apr 2022 03:32:07 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1845631AbiDFB4H (ORCPT + 99 others); Tue, 5 Apr 2022 21:56:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39192 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349441AbiDEJtw (ORCPT ); Tue, 5 Apr 2022 05:49:52 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2AC926AC7; Tue, 5 Apr 2022 02:46:01 -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 80520B817D3; Tue, 5 Apr 2022 09:46:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C135AC385A2; Tue, 5 Apr 2022 09:45:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649151959; bh=R7OELOo+Oi/SyfgfAsS9oeLiXh3ryotLhL8VqMLThOs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wqQ0YLQQXpHO/BYFQdARcdQWrRegefTQKOioszjzZ9WuekiiQq6B5fPZXCd96KgY8 5JIBnQ+GfpiH2gKHaeTLa1ACdIH0r9RgYQwWw3CgQdRyhfx9wGpfDwHWpdsk1sU5Nh a8w75SxwHlwC07UIiHG0XapY7SvoPsHBzHvY1xoI= 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.15 603/913] serial: 8250: Fix race condition in RTS-after-send handling Date: Tue, 5 Apr 2022 09:27:45 +0200 Message-Id: <20220405070357.915942151@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070339.801210740@linuxfoundation.org> References: <20220405070339.801210740@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 b470bc747b99..868ccb3e16cf 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