Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2194980iof; Tue, 7 Jun 2022 22:50:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzO4bprw6JT72rbVEhKJ9q8XyMp9YZovPBoCwUfmKfwEz8sujQ2QVaVU22CMTB78/L6uIoZ X-Received: by 2002:a17:90b:3ec6:b0:1e8:a001:5c95 with SMTP id rm6-20020a17090b3ec600b001e8a0015c95mr8275750pjb.96.1654667435003; Tue, 07 Jun 2022 22:50:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654667434; cv=none; d=google.com; s=arc-20160816; b=wqpdzul/zJvP/fZ/LcsWGkhHOm2snSuILcKWNUpSvnNgXa0qGask4nyKRQf064gk02 kzib2BsOFHnD9Pmy1/Z4NKTafmObThWTWoVKg/fLKNWgorZ9wB3G9DUfO4tg7sZ19O4w I1/OVo+iHDHYHjRM4rO14SPrChCcak9mA2vxkUgvsnN8CxKuEAFFLUojM6aHu5WVZ0H2 RgsGt5veDY/LhZTJwY9V9wP7ZTj3Fih/zKBa81mx6/saOtiOkOxcLf0vOpO6Daifzsn/ Ppf4s2+ALmp1HzkKuRBC9ygJIbd+3tbQxVoDn85SqPg4h3Gj+Ec2uxESkX3MtOZUO1td nsfw== 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=nGt8+744AP5pGu8ktqcugQRsWt9538XTaTkG5X1XVdA=; b=0JyBIjyZdyAH1EcXeIp9wqdU5kOLy4o2BRYDWQnOZckdCntvdpd+Ews6zGA9awcSYu 4PRIr8XmL7+IHWLHuvw5matI/4YeRaZyxlGR3AhTbnpeP4niIESEbDLlVtEbhzquszLq SMAv0BQeDQJ2uHliy5hN3lJJt6oUbipeNwy7IRrGohqOJH0AhepoxpYR+R0BXJZj9FKZ gH9Kfgu59b4uiUjA+9WngIJt+auXlm5zl01paSmfVPyGo0l2wdsKDZI6NomYgLK2sBUE YckYUo8cpz2xcwJhM5YBBUOCu0zKBTge3AdVuVAf5yqHZfB/UPrSc8+S22RdI9zOf9mO QW2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=R0uEdJk4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id 198-20020a6301cf000000b003fd8a009a30si13283381pgb.264.2022.06.07.22.50.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 22:50:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=R0uEdJk4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B58A94A63FC; Tue, 7 Jun 2022 22:17:27 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1384978AbiFHAo5 (ORCPT + 99 others); Tue, 7 Jun 2022 20:44:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382587AbiFGWDr (ORCPT ); Tue, 7 Jun 2022 18:03:47 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D350625227F; Tue, 7 Jun 2022 12:15:28 -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 81844B8233E; Tue, 7 Jun 2022 19:15:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DE500C385A2; Tue, 7 Jun 2022 19:15:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654629326; bh=MU2+vNNvD/C4Et7c0mjGwxnPA8RogRxCydOV6Jl+oY8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=R0uEdJk4YRFH4aFPeo1caQja3ZREHJLtwfaDEd87cJ2ltUnIB4JR3VHBuYVeeyMwJ cuQ64eLOPtXzSLg7L7aGdG8y8ifPty7Udzu4f7+MgrMCgDwvESX5ldFdnutuPiRVia weAqj2Ra4N15MgtqSRHy7TObhkvArFV8tRRmm9oM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, =?UTF-8?q?Bj=C3=B6rn=20Ard=C3=B6?= , Jassi Brar , Sasha Levin Subject: [PATCH 5.18 651/879] mailbox: forward the hrtimer if not queued and under a lock Date: Tue, 7 Jun 2022 19:02:49 +0200 Message-Id: <20220607165021.743865142@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607165002.659942637@linuxfoundation.org> References: <20220607165002.659942637@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=-3.1 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=unavailable 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: Björn Ardö [ Upstream commit bca1a1004615efe141fd78f360ecc48c60bc4ad5 ] This reverts commit c7dacf5b0f32957b24ef29df1207dc2cd8307743, "mailbox: avoid timer start from callback" The previous commit was reverted since it lead to a race that caused the hrtimer to not be started at all. The check for hrtimer_active() in msg_submit() will return true if the callback function txdone_hrtimer() is currently running. This function could return HRTIMER_NORESTART and then the timer will not be restarted, and also msg_submit() will not start the timer. This will lead to a message actually being submitted but no timer will start to check for its compleation. The original fix that added checking hrtimer_active() was added to avoid a warning with hrtimer_forward. Looking in the kernel another solution to avoid this warning is to check hrtimer_is_queued() before calling hrtimer_forward_now() instead. This however requires a lock so the timer is not started by msg_submit() inbetween this check and the hrtimer_forward() call. Fixes: c7dacf5b0f32 ("mailbox: avoid timer start from callback") Signed-off-by: Björn Ardö Signed-off-by: Jassi Brar Signed-off-by: Sasha Levin --- drivers/mailbox/mailbox.c | 19 +++++++++++++------ include/linux/mailbox_controller.h | 1 + 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/drivers/mailbox/mailbox.c b/drivers/mailbox/mailbox.c index 3e7d4b20ab34..4229b9b5da98 100644 --- a/drivers/mailbox/mailbox.c +++ b/drivers/mailbox/mailbox.c @@ -82,11 +82,11 @@ static void msg_submit(struct mbox_chan *chan) exit: spin_unlock_irqrestore(&chan->lock, flags); - /* kick start the timer immediately to avoid delays */ if (!err && (chan->txdone_method & TXDONE_BY_POLL)) { - /* but only if not already active */ - if (!hrtimer_active(&chan->mbox->poll_hrt)) - hrtimer_start(&chan->mbox->poll_hrt, 0, HRTIMER_MODE_REL); + /* kick start the timer immediately to avoid delays */ + spin_lock_irqsave(&chan->mbox->poll_hrt_lock, flags); + hrtimer_start(&chan->mbox->poll_hrt, 0, HRTIMER_MODE_REL); + spin_unlock_irqrestore(&chan->mbox->poll_hrt_lock, flags); } } @@ -120,20 +120,26 @@ static enum hrtimer_restart txdone_hrtimer(struct hrtimer *hrtimer) container_of(hrtimer, struct mbox_controller, poll_hrt); bool txdone, resched = false; int i; + unsigned long flags; for (i = 0; i < mbox->num_chans; i++) { struct mbox_chan *chan = &mbox->chans[i]; if (chan->active_req && chan->cl) { - resched = true; txdone = chan->mbox->ops->last_tx_done(chan); if (txdone) tx_tick(chan, 0); + else + resched = true; } } if (resched) { - hrtimer_forward_now(hrtimer, ms_to_ktime(mbox->txpoll_period)); + spin_lock_irqsave(&mbox->poll_hrt_lock, flags); + if (!hrtimer_is_queued(hrtimer)) + hrtimer_forward_now(hrtimer, ms_to_ktime(mbox->txpoll_period)); + spin_unlock_irqrestore(&mbox->poll_hrt_lock, flags); + return HRTIMER_RESTART; } return HRTIMER_NORESTART; @@ -500,6 +506,7 @@ int mbox_controller_register(struct mbox_controller *mbox) hrtimer_init(&mbox->poll_hrt, CLOCK_MONOTONIC, HRTIMER_MODE_REL); mbox->poll_hrt.function = txdone_hrtimer; + spin_lock_init(&mbox->poll_hrt_lock); } for (i = 0; i < mbox->num_chans; i++) { diff --git a/include/linux/mailbox_controller.h b/include/linux/mailbox_controller.h index 36d6ce673503..6fee33cb52f5 100644 --- a/include/linux/mailbox_controller.h +++ b/include/linux/mailbox_controller.h @@ -83,6 +83,7 @@ struct mbox_controller { const struct of_phandle_args *sp); /* Internal to API */ struct hrtimer poll_hrt; + spinlock_t poll_hrt_lock; struct list_head node; }; -- 2.35.1