Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp837587imm; Wed, 1 Aug 2018 06:11:25 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdhXLHHhzzMHpDgu0nOCsP0ddEc99vl6B3u0be1zPCdBqvmlND4MeceV+in9F412I1EXtvJ X-Received: by 2002:a62:fd06:: with SMTP id p6-v6mr26189708pfh.167.1533129085197; Wed, 01 Aug 2018 06:11:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533129085; cv=none; d=google.com; s=arc-20160816; b=nUxSxlxtQpOhJdXkLbD4syaefETxOzvu1sFz117mDEM1ENFUJYfSgM+j19PcdFRMxK 1XN1E2o8aVDeaoZmS4AbvUMIx6Cln1DBzeWwh8T0KqvgksJuo+OEt8RL1DirPBlEs0c+ Q20Hdj3lPc14zbM1tkz4zw1Np7634UYQI0YJoZ3nktte6l1h+XpWaME0kQFZ/1gxbvpC ApIXNVe+i1Sz6zeI8YD+F8/qjfm3DJ8jPzUDf+kflv4qwcYzxE17WtYpr+vnTsWkX4Pl GPA1r84WZyU/tuTd086QMqZdWGPyNmxeqmA0Mc/rgfU1r7oyGqiwLoqB5Tfx/hVzc9YD jLVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=xXNGOOU8NA9A1Fq6nPTWJNBz9j+37fSgKXdW01Szg/Y=; b=LsR5VxxDesrqyoOKzUuYHoyfbq3QWnGIeXDMTZoL1t4mhRaV4TJ5t4P7VzsCwMVoj2 vR4EZ7jMI6S2DQ6AG6+QKuo5TMjL6ezn7Y26BWdpm9aLN5Yxn9OfJdhf9fqib9Cu9JB1 B0hoaNXE96xgpUNCY3PZAopBsrkogFm7gq2w+p0IqrscQiiZ3HDp3tM7/Bw7251zkmbJ +cEnpg4INOMNUcfjSjxfAaoMB1YykelVMo7Fq1K4Bk/Sk+cs6ALYXGf1d4gTyviyhjoS bkpZWQP/67KZVN0LyrhToX2w4zNbH4NugCBWwPW2vcI6Wx2BDvsWyTeXj4k0RPeFtVXo 4joA== 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 l9-v6si9480744pgp.503.2018.08.01.06.11.10; Wed, 01 Aug 2018 06:11:25 -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 S2389343AbeHAO4F (ORCPT + 99 others); Wed, 1 Aug 2018 10:56:05 -0400 Received: from alexa-out-blr-02.qualcomm.com ([103.229.18.198]:17418 "EHLO alexa-out-blr.qualcomm.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2389266AbeHAO4E (ORCPT ); Wed, 1 Aug 2018 10:56:04 -0400 X-IronPort-AV: E=Sophos;i="5.51,432,1526322600"; d="scan'208";a="119713" Received: from ironmsg03-blr.qualcomm.com ([10.86.208.132]) by alexa-out-blr.qualcomm.com with ESMTP/TLS/AES256-SHA; 01 Aug 2018 18:40:20 +0530 X-IronPort-AV: E=McAfee;i="5900,7806,8971"; a="713655" Received: from gkohli-linux.qualcomm.com ([10.204.78.26]) by ironmsg03-blr.qualcomm.com with ESMTP; 01 Aug 2018 18:40:19 +0530 Received: by gkohli-linux.qualcomm.com (Postfix, from userid 427023) id 2F02B30CD; Wed, 1 Aug 2018 18:40:18 +0530 (IST) From: Gaurav Kohli To: tglx@linutronix.de, john.stultz@linaro.org, sboyd@kernel.org Cc: linux-kernel@vger.kernel.org, Gaurav Kohli Subject: [PATCH v1] timers: Clear must_forward_clk inside base lock Date: Wed, 1 Aug 2018 18:40:16 +0530 Message-Id: <1533129016-28050-1-git-send-email-gkohli@codeaurora.org> X-Mailer: git-send-email 1.9.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Timer wheel base->must_forward_clock is indicating that the base clock might be stale due to a long idle sleep. The forwarding of base clock takes place in softirq of timer or when a timer is enqueued to base which is idle. While migrate timer from remote CPU to the new base which is idle, then following race can happen: CPU0 CPU1 run_timer_softirq timers_dead_cpu base = lock_timer_base(timer); base->must_forward_clk = false if (base->must_forward_clk) forward(base); >>skip migrate_timer_list enqueue_timer(base, timer, idx); >> idx is calculated high due to >> stale base unlock_timer_base(timer); base = lock_timer_base(timer); forward(base); The root cause is that base->must_forward_clk is cleared outside the base->lock held region, so the remote queuing CPU observes it as cleared, but the base clock is still stale. This can cause large granularity values for timers, i.e. the accuracy of the expiry time suffers. Prevent this by clearing the flag with base->lock held, so that the forwarding takes place before the cleared flag is observable by a remote CPU. Signed-off-by: Gaurav Kohli --- Changes since v0: - Updated commit text and comment suggested by Thomas. diff --git a/kernel/time/timer.c b/kernel/time/timer.c index cc2d23e..70aa1c6 100644 --- a/kernel/time/timer.c +++ b/kernel/time/timer.c @@ -1657,6 +1657,17 @@ static inline void __run_timers(struct timer_base *base) raw_spin_lock_irq(&base->lock); + /* + * The must_forward_clk flag is cleared unconditionally also for + * the deferrable base. The deferrable base is not affected by idle + * tracking and never forwarded, so clearing the flag is a NOOP. + * + * The fact that the deferrable base is never forwarded can cause + * large variations in granularity for deferrable timers, but they + * can be deferred for long periods due to idle anyway. + */ + base->must_forward_clk = false; + while (time_after_eq(jiffies, base->clk)) { levels = collect_expired_timers(base, heads); @@ -1676,19 +1687,6 @@ static __latent_entropy void run_timer_softirq(struct softirq_action *h) { struct timer_base *base = this_cpu_ptr(&timer_bases[BASE_STD]); - /* - * must_forward_clk must be cleared before running timers so that any - * timer functions that call mod_timer will not try to forward the - * base. idle trcking / clock forwarding logic is only used with - * BASE_STD timers. - * - * The deferrable base does not do idle tracking at all, so we do - * not forward it. This can result in very large variations in - * granularity for deferrable timers, but they can be deferred for - * long periods due to idle. - */ - base->must_forward_clk = false; - __run_timers(base); if (IS_ENABLED(CONFIG_NO_HZ_COMMON)) __run_timers(this_cpu_ptr(&timer_bases[BASE_DEF])); -- 1.9.1