From patchwork Tue Mar 17 17:38:22 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 45890 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f71.google.com (mail-wg0-f71.google.com [74.125.82.71]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id DF28E2153C for ; Tue, 17 Mar 2015 17:38:40 +0000 (UTC) Received: by wggy19 with SMTP id y19sf3063340wgg.2 for ; Tue, 17 Mar 2015 10:38:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=FyW4EHt0n5b596pV4//ML2uxhjYvXrK25W5xZ774QVs=; b=mmxLGpklCPynY0YYm/Vrk2Y0RBgBiAXCwbGN+a8NY8cES1sgEzL+kwslROqvu0mgiT tTrAy/po8QZPLJLoCyyd2efGsu6OFrSmG7lZYf4Ns8Qc8eXu0v/zjp/JscIlF2jsKRcw D0PZBRbcivDDhjNWEDCtRF5tZ+Fq/aHs+8D/COZtIfXnrLOJBrEuFYGLlRhqGRxqNcK/ 8EZa1dOAt/VAfAEzqFhUgmMu6+AsUNADN+mTRri6GGSPFN6+y50W7IamvcDCQmTES3xi +4GkIQizbec/YHMj35/ShwHSmjtJ3mrHTKiPtSW9x5cS0vYs3v9o0mXB+x0/vjA+/SZf bMMA== X-Gm-Message-State: ALoCoQmx3SCUB4ti9oMfc2QuHCkKMFH54xAyn05yW/Ft9LodkG1I7hwLQ93xhjZ9Mm+kayYc3Ezm X-Received: by 10.180.75.232 with SMTP id f8mr13505809wiw.0.1426613920227; Tue, 17 Mar 2015 10:38:40 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.181.137 with SMTP id dw9ls65003lac.85.gmail; Tue, 17 Mar 2015 10:38:40 -0700 (PDT) X-Received: by 10.152.18.225 with SMTP id z1mr61818885lad.124.1426613920060; Tue, 17 Mar 2015 10:38:40 -0700 (PDT) Received: from mail-la0-f51.google.com (mail-la0-f51.google.com. [209.85.215.51]) by mx.google.com with ESMTPS id f4si11059228lbd.26.2015.03.17.10.38.39 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 17 Mar 2015 10:38:39 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.51 as permitted sender) client-ip=209.85.215.51; Received: by ladw1 with SMTP id w1so15148738lad.0 for ; Tue, 17 Mar 2015 10:38:39 -0700 (PDT) X-Received: by 10.112.130.39 with SMTP id ob7mr62691141lbb.32.1426613919645; Tue, 17 Mar 2015 10:38:39 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.112.35.133 with SMTP id h5csp660899lbj; Tue, 17 Mar 2015 10:38:38 -0700 (PDT) X-Received: by 10.70.44.100 with SMTP id d4mr152264132pdm.36.1426613916216; Tue, 17 Mar 2015 10:38:36 -0700 (PDT) Received: from mail-pa0-f47.google.com (mail-pa0-f47.google.com. [209.85.220.47]) by mx.google.com with ESMTPS id pc10si30617230pdb.109.2015.03.17.10.38.35 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 17 Mar 2015 10:38:36 -0700 (PDT) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.47 as permitted sender) client-ip=209.85.220.47; Received: by pabyw6 with SMTP id yw6so15738861pab.2 for ; Tue, 17 Mar 2015 10:38:35 -0700 (PDT) X-Received: by 10.66.150.165 with SMTP id uj5mr111300354pab.54.1426613915313; Tue, 17 Mar 2015 10:38:35 -0700 (PDT) Received: from localhost.localdomain (c-67-170-153-23.hsd1.or.comcast.net. [67.170.153.23]) by mx.google.com with ESMTPSA id hk6sm23410539pdb.83.2015.03.17.10.38.31 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 17 Mar 2015 10:38:34 -0700 (PDT) From: John Stultz To: lkml Cc: Daniel Thompson , Russell King , Will Deacon , Catalin Marinas , Thomas Gleixner , Stephen Boyd , Ingo Molnar , Peter Zijlstra , John Stultz Subject: [PATCH 1/5] sched_clock: Match scope of read and write seqcounts Date: Tue, 17 Mar 2015 10:38:22 -0700 Message-Id: <1426613906-19718-2-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1426613906-19718-1-git-send-email-john.stultz@linaro.org> References: <1426613906-19718-1-git-send-email-john.stultz@linaro.org> X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: john.stultz@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.51 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , From: Daniel Thompson Currently the scope of the raw_write_seqcount_begin/end in sched_clock_register far exceeds the scope of the read section in sched_clock. This gives the impression of safety during cursory review but achieves little. Note that this is likely to be a latent issue at present because sched_clock_register() is typically called before we enable interrupts, however the issue does risk bugs being needlessly introduced as the code evolves. This patch fixes the problem by increasing the scope of the read locking performed by sched_clock() to cover all data modified by sched_clock_register. We also improve clarity by moving writes to struct clock_data that do not impact sched_clock() outside of the critical section. Cc: Russell King Cc: Will Deacon Cc: Catalin Marinas Cc: Thomas Gleixner Cc: Stephen Boyd Cc: Ingo Molnar Cc: Peter Zijlstra Signed-off-by: Daniel Thompson Reviewed-by: Stephen Boyd Signed-off-by: John Stultz --- kernel/time/sched_clock.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/kernel/time/sched_clock.c b/kernel/time/sched_clock.c index 01d2d15..3d21a87 100644 --- a/kernel/time/sched_clock.c +++ b/kernel/time/sched_clock.c @@ -58,23 +58,21 @@ static inline u64 notrace cyc_to_ns(u64 cyc, u32 mult, u32 shift) unsigned long long notrace sched_clock(void) { - u64 epoch_ns; - u64 epoch_cyc; - u64 cyc; + u64 cyc, res; unsigned long seq; - if (cd.suspended) - return cd.epoch_ns; - do { seq = raw_read_seqcount_begin(&cd.seq); - epoch_cyc = cd.epoch_cyc; - epoch_ns = cd.epoch_ns; + + res = cd.epoch_ns; + if (!cd.suspended) { + cyc = read_sched_clock(); + cyc = (cyc - cd.epoch_cyc) & sched_clock_mask; + res += cyc_to_ns(cyc, cd.mult, cd.shift); + } } while (read_seqcount_retry(&cd.seq, seq)); - cyc = read_sched_clock(); - cyc = (cyc - epoch_cyc) & sched_clock_mask; - return epoch_ns + cyc_to_ns(cyc, cd.mult, cd.shift); + return res; } /* @@ -124,10 +122,11 @@ void __init sched_clock_register(u64 (*read)(void), int bits, clocks_calc_mult_shift(&new_mult, &new_shift, rate, NSEC_PER_SEC, 3600); new_mask = CLOCKSOURCE_MASK(bits); + cd.rate = rate; /* calculate how many ns until we wrap */ wrap = clocks_calc_max_nsecs(new_mult, new_shift, 0, new_mask); - new_wrap_kt = ns_to_ktime(wrap - (wrap >> 3)); + cd.wrap_kt = ns_to_ktime(wrap - (wrap >> 3)); /* update epoch for new counter and update epoch_ns from old counter*/ new_epoch = read(); @@ -138,8 +137,6 @@ void __init sched_clock_register(u64 (*read)(void), int bits, raw_write_seqcount_begin(&cd.seq); read_sched_clock = read; sched_clock_mask = new_mask; - cd.rate = rate; - cd.wrap_kt = new_wrap_kt; cd.mult = new_mult; cd.shift = new_shift; cd.epoch_cyc = new_epoch;