From patchwork Fri Jan 23 00:09:21 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 43549 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f199.google.com (mail-wi0-f199.google.com [209.85.212.199]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id F2798218DB for ; Fri, 23 Jan 2015 00:09:54 +0000 (UTC) Received: by mail-wi0-f199.google.com with SMTP id r20sf3226996wiv.2 for ; Thu, 22 Jan 2015 16:09:54 -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=PkVwHuqlyE0Gd7MkHiRbKsNTIQw0fz52Y2b47drogZc=; b=DbKlus0HTyoSGI4jKCPJVJVHREwL4yLvij0FIDkUYYjQM8hnRcRsgtHTQHFVD6RM5I 7Kpje9h5x0z/tczPDztjhBWAB0GyWeIpnCbN3jHc40QfbUK2aIwPBl3Y7AjcMmEpdaAR a1Uzmo49CEajfTZ3w/FJp1ykbfwU/peJcMQA3t32eNFavxXPVDAEPqA1NxyqwII2sHUC su38Zjat0gl0zoSXHSH50a3JHPR30S+4GhreG0yV19CIiCAfKsOMLvyUPD+too3z/dg0 YdeH06+EpN+hPBz8hkWC9bxMl9vWXfzCbOQAqW317OvhbZ21zBHPZzhY41u9avMNH52u zhAw== X-Gm-Message-State: ALoCoQnfhn18L+zYbK4YzfgGQ9XHiObGRYz8iCfR4Ss49R6LmYnVF2eaoJkSwCmXk6XkS3JAZRcW X-Received: by 10.194.178.234 with SMTP id db10mr585506wjc.2.1421971794295; Thu, 22 Jan 2015 16:09:54 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.242.129 with SMTP id wq1ls200224lac.56.gmail; Thu, 22 Jan 2015 16:09:54 -0800 (PST) X-Received: by 10.112.14.134 with SMTP id p6mr4659933lbc.36.1421971794127; Thu, 22 Jan 2015 16:09:54 -0800 (PST) Received: from mail-lb0-f182.google.com (mail-lb0-f182.google.com. [209.85.217.182]) by mx.google.com with ESMTPS id dv9si22039851lbc.63.2015.01.22.16.09.54 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Jan 2015 16:09:54 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.182 as permitted sender) client-ip=209.85.217.182; Received: by mail-lb0-f182.google.com with SMTP id l4so4420186lbv.13 for ; Thu, 22 Jan 2015 16:09:54 -0800 (PST) X-Received: by 10.152.45.4 with SMTP id i4mr4571320lam.74.1421971794025; Thu, 22 Jan 2015 16:09:54 -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.9.200 with SMTP id c8csp63929lbb; Thu, 22 Jan 2015 16:09:53 -0800 (PST) X-Received: by 10.70.61.130 with SMTP id p2mr6881553pdr.0.1421971781911; Thu, 22 Jan 2015 16:09:41 -0800 (PST) Received: from mail-pd0-f180.google.com (mail-pd0-f180.google.com. [209.85.192.180]) by mx.google.com with ESMTPS id od7si9597991pdb.99.2015.01.22.16.09.40 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Jan 2015 16:09:41 -0800 (PST) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.192.180 as permitted sender) client-ip=209.85.192.180; Received: by mail-pd0-f180.google.com with SMTP id ft15so4749389pdb.11 for ; Thu, 22 Jan 2015 16:09:40 -0800 (PST) X-Received: by 10.70.89.207 with SMTP id bq15mr6582508pdb.68.1421971780145; Thu, 22 Jan 2015 16:09:40 -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 am14sm10405657pac.35.2015.01.22.16.09.39 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 22 Jan 2015 16:09:39 -0800 (PST) From: John Stultz To: Linux Kernel Mailing List 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: Thu, 22 Jan 2015 16:09:21 -0800 Message-Id: <1421971767-17707-7-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1421971767-17707-1-git-send-email-john.stultz@linaro.org> References: <1421971767-17707-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.182 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 daa0d43..ae3945d 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -134,8 +134,31 @@ 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 #define timekeeping_check_update(x, y) +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 /** @@ -213,14 +236,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; @@ -232,14 +251,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);