Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp7970417imm; Thu, 28 Jun 2018 12:16:44 -0700 (PDT) X-Google-Smtp-Source: AAOMgpfeE0uzci24xCIuWaabKiMta3tqCnrPDnPShwmLayOM9x36nrnrxvZKnJSKZ7Hf44oBApqr X-Received: by 2002:a62:d39b:: with SMTP id z27-v6mr6006083pfk.182.1530213404378; Thu, 28 Jun 2018 12:16:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530213404; cv=none; d=google.com; s=arc-20160816; b=jsPR26D5yEBKArHxMLF9ci15fPV6LFSAVoQ6VrOB/P7MzC28NvFXjE3EW7hzttMOTv Zl9KsXL7lS1qAkakP4koKEV8sMZkKKN4ladLiaGgSd9LFtLA7aShyZfvKnZeedXNELx7 qGkulRSzV3Yz9R6hEsMP94DTGXTgTsMzDdFezd3C95u1Ezr20Twy/2O81Wi5//J+q2c9 mAFugCSNmeOH/RbJ9IGsu8mZGLxgY6q7Q8iYBPd4PrlpQGnwITw+OlC6VSG7Es4tXG73 1LWUxVRjkR57WerC/DS5pL9xn01NJ3zmcfnGYLthCAquhZ3OwfHUdlfB954lR4YYbYff NKrA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=r7pgOUKB6Go2eLUr+zOBKo7HG6H3a06g9Qxm+jNxYyM=; b=at9krEvuBX4IEXrjQvxomkPVOc/uID1f8f6PYwj75vPMsK7LZ5Z/HOpNk/od9CmLLH qMfB1yAMTKk7rlawrmgW6Ocjrb3q7tBjW17IRdDj30GY6lSwKxw2a1EWq1DImTojLzzr POOuWBo+whJ4bh7zutX9LkNBnNcuWwF+OSh+blf1RirWqa22Eub2YDU3HsAwcW/eczDR tbWFf2RUP1nM7wI1HyD3iJiIzxD6weahXH2pSZ/+noqvwKJdBBK1lKnDtIoP5+L0KGUB Y40kcoVp8Uo8sft3Dgpabi3UlNTkg5AUG51MIdL5AirdIZfXHugUDyo8AhnOpHp1W/Lt u1og== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l196-v6si5506799pga.38.2018.06.28.12.16.29; Thu, 28 Jun 2018 12:16:44 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935677AbeF1Ql0 (ORCPT + 99 others); Thu, 28 Jun 2018 12:41:26 -0400 Received: from mx0a-00010702.pphosted.com ([148.163.156.75]:41500 "EHLO mx0b-00010702.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S935628AbeF1QlL (ORCPT ); Thu, 28 Jun 2018 12:41:11 -0400 Received: from pps.filterd (m0098780.ppops.net [127.0.0.1]) by mx0a-00010702.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w5SGa0BZ014758; Thu, 28 Jun 2018 11:40:52 -0500 Received: from ni.com (skprod2.natinst.com [130.164.80.23]) by mx0a-00010702.pphosted.com with ESMTP id 2jvdqwun5u-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 28 Jun 2018 11:40:52 -0500 Received: from us-aus-exhub1.ni.corp.natinst.com (us-aus-exhub1.ni.corp.natinst.com [130.164.68.41]) by us-aus-skprod2.natinst.com (8.16.0.22/8.16.0.22) with ESMTPS id w5SGepDv018133 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT); Thu, 28 Jun 2018 11:40:51 -0500 Received: from us-aus-exch5.ni.corp.natinst.com (130.164.68.15) by us-aus-exhub1.ni.corp.natinst.com (130.164.68.41) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Thu, 28 Jun 2018 11:40:51 -0500 Received: from us-aus-exhub1.ni.corp.natinst.com (130.164.68.41) by us-aus-exch5.ni.corp.natinst.com (130.164.68.15) with Microsoft SMTP Server (TLS) id 15.0.1156.6; Thu, 28 Jun 2018 11:40:51 -0500 Received: from harisdt.amer.corp.natinst.com (130.164.49.7) by us-aus-exhub1.ni.corp.natinst.com (130.164.68.41) with Microsoft SMTP Server id 15.0.1156.6 via Frontend Transport; Thu, 28 Jun 2018 11:40:51 -0500 From: Haris Okanovic To: , CC: , , , , , , , , Subject: [PATCH v5 2/2] timers: Don't search for expired timers while TIMER_SOFTIRQ is scheduled Date: Thu, 28 Jun 2018 11:40:36 -0500 Message-ID: <20180628164036.27378-2-haris.okanovic@ni.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180628164036.27378-1-haris.okanovic@ni.com> References: <7d21ff63-3dce-b8ea-a0f1-81d53617747d@ni.com> <20180628164036.27378-1-haris.okanovic@ni.com> MIME-Version: 1.0 Content-Type: text/plain X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-06-28_08:,, signatures=0 X-Proofpoint-Spam-Reason: safe Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This change avoids 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 [PATCH v4] - Rebase onto v4.14.20-rt17 [PATCH v5] no change https://github.com/harisokanovic/linux/tree/dev/hokanovi/timer-peek-v7 --- 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 dd67c18c16d0..723c3667de2b 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -207,6 +207,7 @@ struct timer_base { unsigned int cpu; bool is_idle; bool must_forward_clk; + bool block_softirq; DECLARE_BITMAP(pending_map, WHEEL_SIZE); struct hlist_head vectors[WHEEL_SIZE]; struct hlist_head expired_lists[LVL_DEPTH]; @@ -1404,9 +1405,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; @@ -1748,6 +1751,9 @@ static __latent_entropy void run_timer_softirq(struct softirq_action *h) __run_timers(base); if (IS_ENABLED(CONFIG_NO_HZ_COMMON)) __run_timers(this_cpu_ptr(&timer_bases[BASE_DEF])); + + /* Allow new TIMER_SOFTIRQs to get scheduled by run_local_timers() */ + base->block_softirq = false; } /* @@ -1758,6 +1764,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)) @@ -1766,7 +1780,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.17.1