From patchwork Sat Jan 10 00:34:24 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 42933 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-la0-f72.google.com (mail-la0-f72.google.com [209.85.215.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 7627926CF1 for ; Sat, 10 Jan 2015 00:34:55 +0000 (UTC) Received: by mail-la0-f72.google.com with SMTP id gq15sf9299754lab.3 for ; Fri, 09 Jan 2015 16:34: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=PyCf6Q7om8nZnzDz5m823lZCZJDxUCpBVLzmXga2vas=; b=jFCvWE96vaAqorrcrNSZEaiUQoQ9i4RTYWUU2txzNBEcJBWF7gA9ZCLpGO+hjk4x9u Z/Z1GsIBP+FOTLfERPhrfbTfpyvRb0xZOBQczSYoWC7pTZ/ZKAQK9dUVQjpFagaOLtKh 8hRB+FhhfESnn0ygJ0cp0X6knV8zTt6DMrnnKqt8emyG4AKXIPUw5cCE0hNEfVI93wA3 l3Qs1Q6LmqlJkC2rh/GaihBfoSIDegXv+dxUjKMx7O6yRZk+WYglssC9Nwv74cTH22R6 cizRg5+QPL5geVuyBFYd8zLMbVzJnAuY3YU8h03rwYZt4GFV/0eZ6XRRzsJeZB6+2cC0 5qjw== X-Gm-Message-State: ALoCoQn99guCF7WOUCGUexr6SDwQ8B6xAAhEse2DUCDT4tj/OBJZsH6MBfvvsH3aGz0MMicDejGr X-Received: by 10.180.76.40 with SMTP id h8mr761573wiw.3.1420850094442; Fri, 09 Jan 2015 16:34:54 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.5.74 with SMTP id q10ls482613laq.43.gmail; Fri, 09 Jan 2015 16:34:54 -0800 (PST) X-Received: by 10.152.203.137 with SMTP id kq9mr24439964lac.51.1420850094190; Fri, 09 Jan 2015 16:34:54 -0800 (PST) Received: from mail-la0-f50.google.com (mail-la0-f50.google.com. [209.85.215.50]) by mx.google.com with ESMTPS id l8si14703092lag.2.2015.01.09.16.34.54 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 09 Jan 2015 16:34:54 -0800 (PST) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.50 as permitted sender) client-ip=209.85.215.50; Received: by mail-la0-f50.google.com with SMTP id pn19so17439136lab.9 for ; Fri, 09 Jan 2015 16:34:54 -0800 (PST) X-Received: by 10.112.30.71 with SMTP id q7mr24531364lbh.41.1420850094104; Fri, 09 Jan 2015 16:34: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 c8csp484833lbb; Fri, 9 Jan 2015 16:34:53 -0800 (PST) X-Received: by 10.70.90.10 with SMTP id bs10mr27116485pdb.165.1420850082838; Fri, 09 Jan 2015 16:34:42 -0800 (PST) Received: from mail-pa0-f41.google.com (mail-pa0-f41.google.com. [209.85.220.41]) by mx.google.com with ESMTPS id we6si15382976pbc.83.2015.01.09.16.34.42 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 09 Jan 2015 16:34:42 -0800 (PST) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; Received: by mail-pa0-f41.google.com with SMTP id rd3so21449354pab.0 for ; Fri, 09 Jan 2015 16:34:42 -0800 (PST) X-Received: by 10.70.90.39 with SMTP id bt7mr27222047pdb.52.1420850081929; Fri, 09 Jan 2015 16:34:41 -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 hc10sm8046439pbd.78.2015.01.09.16.34.40 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 09 Jan 2015 16:34:41 -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/10] time: Cap clocksource reads to the clocksource max_cycles value Date: Fri, 9 Jan 2015 16:34:24 -0800 Message-Id: <1420850068-27828-7-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1420850068-27828-1-git-send-email-john.stultz@linaro.org> References: <1420850068-27828-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.50 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 such a cap that limits the read delta value to the max_cycles value, which is where an overflow would occur. 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 | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index 0dcceba..9740fd8 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -202,6 +202,9 @@ static inline s64 timekeeping_get_ns(struct tk_read_base *tkr) /* 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 */ + delta = min(delta, tkr->clock->max_cycles); + nsec = delta * tkr->mult + tkr->xtime_nsec; nsec >>= tkr->shift; @@ -221,6 +224,9 @@ static inline s64 timekeeping_get_ns_raw(struct timekeeper *tk) /* calculate the delta since the last update_wall_time: */ delta = clocksource_delta(cycle_now, tk->tkr.cycle_last, tk->tkr.mask); + /* Cap delta value to the max_cycles values to avoid mult overflows */ + delta = min(delta, tk->tkr.clock->max_cycles); + /* convert delta to nanoseconds. */ nsec = clocksource_cyc2ns(delta, clock->mult, clock->shift); @@ -482,6 +488,9 @@ static void timekeeping_forward_now(struct timekeeper *tk) delta = clocksource_delta(cycle_now, tk->tkr.cycle_last, tk->tkr.mask); tk->tkr.cycle_last = cycle_now; + /* Cap delta value to the max_cycles values to avoid mult overflows */ + delta = min(delta, tk->tkr.clock->max_cycles); + tk->tkr.xtime_nsec += delta * tk->tkr.mult; /* If arch requires, add in get_arch_timeoffset() */