From patchwork Sat Mar 7 02:49:57 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 45508 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f198.google.com (mail-wi0-f198.google.com [209.85.212.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 0797B214A0 for ; Sat, 7 Mar 2015 02:50:35 +0000 (UTC) Received: by wivr20 with SMTP id r20sf4925554wiv.0 for ; Fri, 06 Mar 2015 18:50:34 -0800 (PST) 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=D/tNENRVIxUfTpJZeTm6c6A6ECd145dKgTDfKkhk9BM=; b=fbcr3FIHq+InxN7yJXR00BhPJHmFBBiKzPzKP0VeXe8Q1nut4YKLsJ2PXGcDlARFvC ZXXcDErWFSlfMioCOf+ivvmfUmuwXB31GRA+ijRqCbFenH6glNkNBVpNx/6eWDX6ApId /6NUQs8aaa8QXoN2ikHpRnk0x0ci4o7LaqzlNj66ggVHuZ5RAdYIkW8IL0/ZEUeVCrCA K2FfW/4n2+yF3JKBUHd0gxO0Xb6IcyAr0h0jWk9yBJXWnBm5c+jZDWnuIZbRAJIPTP3k eX8pDeZQiB/YEdnztnBjf4pOCO3Ppk3sPSpeZiizSL8JxTZNiH9bqHygJ8U1Inb1MSog E+Qw== X-Gm-Message-State: ALoCoQkXuadnpbQTV7JVv+3jg2tcjb4rZBhOFPl23ME8TE/L3ln6kSQBW7IYTT3vfUpEqU3Mb/mg X-Received: by 10.112.156.65 with SMTP id wc1mr2779361lbb.7.1425696634249; Fri, 06 Mar 2015 18:50:34 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.18.163 with SMTP id x3ls128191lad.56.gmail; Fri, 06 Mar 2015 18:50:33 -0800 (PST) X-Received: by 10.152.197.34 with SMTP id ir2mr15635281lac.36.1425696633908; Fri, 06 Mar 2015 18:50:33 -0800 (PST) Received: from mail-lb0-f173.google.com (mail-lb0-f173.google.com. [209.85.217.173]) by mx.google.com with ESMTPS id ji3si1898499lbc.63.2015.03.06.18.50.33 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 Mar 2015 18:50:33 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.173 as permitted sender) client-ip=209.85.217.173; Received: by lbvp9 with SMTP id p9so26127285lbv.10 for ; Fri, 06 Mar 2015 18:50:33 -0800 (PST) X-Received: by 10.152.116.18 with SMTP id js18mr15739999lab.106.1425696633495; Fri, 06 Mar 2015 18:50:33 -0800 (PST) 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 h5csp256454lbj; Fri, 6 Mar 2015 18:50:32 -0800 (PST) X-Received: by 10.70.130.10 with SMTP id oa10mr710430pdb.65.1425696624736; Fri, 06 Mar 2015 18:50:24 -0800 (PST) Received: from mail-pd0-f173.google.com (mail-pd0-f173.google.com. [209.85.192.173]) by mx.google.com with ESMTPS id pm7si13842pdb.71.2015.03.06.18.50.23 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 Mar 2015 18:50:24 -0800 (PST) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.192.173 as permitted sender) client-ip=209.85.192.173; Received: by pdjz10 with SMTP id z10so28671807pdj.11 for ; Fri, 06 Mar 2015 18:50:23 -0800 (PST) X-Received: by 10.68.239.102 with SMTP id vr6mr30959396pbc.35.1425696623854; Fri, 06 Mar 2015 18:50:23 -0800 (PST) Received: from localhost.localdomain (c-67-170-153-23.hsd1.or.comcast.net. [67.170.153.23]) by mx.google.com with ESMTPSA id a2sm8381456pbu.27.2015.03.06.18.50.22 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 06 Mar 2015 18:50:23 -0800 (PST) From: John Stultz To: lkml Cc: John Stultz , Dave Jones , Linus Torvalds , Thomas Gleixner , Richard Cochran , Prarit Bhargava , Stephen Boyd , Ingo Molnar , Peter Zijlstra Subject: [PATCH 06/12] time: Add infrastructure to cap clocksource reads to the max_cycles value Date: Fri, 6 Mar 2015 18:49:57 -0800 Message-Id: <1425696603-16878-7-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1425696603-16878-1-git-send-email-john.stultz@linaro.org> References: <1425696603-16878-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.217.173 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: , When calculating the current delta since the last tick, we currently have no hard protections to prevent a multiplciation overflow from ocurring. This patch introduces infrastructure to allow a capp that limits the read delta value to the max_cycles value, which is where an overflow would occur. Since this is in the hotpath, it adds the extra checking under CONFIG_DEBUG_TIMEKEEPING. There was some concern that capping time like this could cause problems as we may stop expiring timers, which could go circular if the timer that triggers time accumulation were misscheduled too far in the future, which would cause time to stop. However, since the mult overflow would result in a smaller time value, we would effectively have the same problem there. Cc: Dave Jones Cc: Linus Torvalds Cc: Thomas Gleixner Cc: Richard Cochran Cc: Prarit Bhargava Cc: Stephen Boyd Cc: Ingo Molnar Cc: Peter Zijlstra Signed-off-by: John Stultz --- kernel/time/timekeeping.c | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 7e9d433..8b9e328 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -134,11 +134,34 @@ static void timekeeping_check_update(struct timekeeper *tk, cycle_t offset) " the %s 50%% safety margin (%lld)\n", offset, name, max_cycles>>1); } + +static inline cycle_t timekeeping_get_delta(struct tk_read_base *tkr) +{ + cycle_t cycle_now, delta; + + /* read clocksource */ + cycle_now = tkr->read(tkr->clock); + + /* calculate the delta since the last update_wall_time */ + delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask); + + /* Cap delta value to the max_cycles values to avoid mult overflows */ + if (unlikely(delta > tkr->clock->max_cycles)) + delta = tkr->clock->max_cycles; + + return delta; +} #else static inline void timekeeping_check_update(struct timekeeper *tk, cycle_t offset) { } +static inline cycle_t timekeeping_get_delta(struct tk_read_base *tkr) +{ + /* calculate the delta since the last update_wall_time */ + return clocksource_delta(tkr->read(tkr->clock), tkr->cycle_last, + tkr->mask); +} #endif /** @@ -216,14 +239,10 @@ static inline u32 arch_gettimeoffset(void) { return 0; } static inline s64 timekeeping_get_ns(struct tk_read_base *tkr) { - cycle_t cycle_now, delta; + cycle_t delta; s64 nsec; - /* read clocksource: */ - cycle_now = tkr->read(tkr->clock); - - /* calculate the delta since the last update_wall_time: */ - delta = clocksource_delta(cycle_now, tkr->cycle_last, tkr->mask); + delta = timekeeping_get_delta(tkr); nsec = delta * tkr->mult + tkr->xtime_nsec; nsec >>= tkr->shift; @@ -235,14 +254,10 @@ static inline s64 timekeeping_get_ns(struct tk_read_base *tkr) static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk) { struct clocksource *clock = tk->tkr.clock; - cycle_t cycle_now, delta; + cycle_t delta; s64 nsec; - /* read clocksource: */ - cycle_now = tk->tkr.read(clock); - - /* calculate the delta since the last update_wall_time: */ - delta = clocksource_delta(cycle_now, tk->tkr.cycle_last, tk->tkr.mask); + delta = timekeeping_get_delta(&tk->tkr); /* convert delta to nanoseconds. */ nsec = clocksource_cyc2ns(delta, clock->mult, clock->shift);