Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752009AbdHCVHU (ORCPT ); Thu, 3 Aug 2017 17:07:20 -0400 Received: from mx0a-00010702.pphosted.com ([148.163.156.75]:36442 "EHLO mx0b-00010702.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751746AbdHCVHR (ORCPT ); Thu, 3 Aug 2017 17:07:17 -0400 From: Haris Okanovic To: , CC: , , , , , , Subject: [PATCH v3 2/2] timers: Don't search for expired timers while TIMER_SOFTIRQ is scheduled Date: Thu, 3 Aug 2017 16:06:57 -0500 Message-ID: <20170803210657.19179-2-haris.okanovic@ni.com> X-Mailer: git-send-email 2.13.2 In-Reply-To: <20170803210657.19179-1-haris.okanovic@ni.com> References: <20170803210657.19179-1-haris.okanovic@ni.com> MIME-Version: 1.0 Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-08-03_10:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_policy_notspam policy=outbound_policy score=30 priorityscore=1501 malwarescore=0 suspectscore=2 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=30 reason=mlx scancount=1 engine=8.0.1-1706020000 definitions=main-1708030321 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2875 Lines: 86 This change avoid needlessly searching for more timers in run_local_timers() (hard interrupt context) when they can't fire. For example, when ktimersoftd/run_timer_softirq() is scheduled but preempted due to cpu contention. When it runs, run_timer_softirq() will discover newly expired timers up to current jiffies in addition to firing previously expired timers. However, this change also adds an edge case where non-hrtimer firing is sometimes delayed by an additional tick. This is acceptable since we don't make latency guarantees for non-hrtimers and would prefer to minimize hard interrupt time instead. Signed-off-by: Haris Okanovic --- [PATCH v3] - Split block_softirq into separate commit https://github.com/harisokanovic/linux/tree/dev/hokanovi/timer-peek-v5 --- kernel/time/timer.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/kernel/time/timer.c b/kernel/time/timer.c index 078027d8a866..f0ef9675abdf 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -208,6 +208,7 @@ struct timer_base { bool migration_enabled; bool nohz_active; bool is_idle; + bool block_softirq; DECLARE_BITMAP(pending_map, WHEEL_SIZE); struct hlist_head vectors[WHEEL_SIZE]; struct hlist_head expired_lists[LVL_DEPTH]; @@ -1376,9 +1377,11 @@ static int __collect_expired_timers(struct timer_base *base) /* * expire_timers() must be called at least once before we can - * collect more timers. + * collect more timers. We should never hit this case unless + * TIMER_SOFTIRQ got raised without expired timers. */ - if (base->expired_levels) + if (WARN_ONCE(base->expired_levels, + "Must expire collected timers before collecting more")) return base->expired_levels; clk = base->clk; @@ -1702,6 +1705,9 @@ static __latent_entropy void run_timer_softirq(struct softirq_action *h) __run_timers(base); if (IS_ENABLED(CONFIG_NO_HZ_COMMON) && base->nohz_active) __run_timers(this_cpu_ptr(&timer_bases[BASE_DEF])); + + /* Allow new TIMER_SOFTIRQs to get scheduled by run_local_timers() */ + base->block_softirq = false; } /* @@ -1712,6 +1718,14 @@ void run_local_timers(void) struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]); hrtimer_run_queues(); + + /* + * Skip if TIMER_SOFTIRQ is already running on this CPU, since it + * will find and expire all timers up to current jiffies. + */ + if (base->block_softirq) + return; + /* Raise the softirq only if required. */ if (time_before(jiffies, base->clk) || !tick_find_expired(base)) { if (!IS_ENABLED(CONFIG_NO_HZ_COMMON) || !base->nohz_active) @@ -1720,7 +1734,10 @@ void run_local_timers(void) base++; if (time_before(jiffies, base->clk) || !tick_find_expired(base)) return; + base--; } + + base->block_softirq = true; raise_softirq(TIMER_SOFTIRQ); } -- 2.13.2