From patchwork Sat Jan 10 00:34: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: 42931 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f69.google.com (mail-wg0-f69.google.com [74.125.82.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 95D5F26CF1 for ; Sat, 10 Jan 2015 00:34:49 +0000 (UTC) Received: by mail-wg0-f69.google.com with SMTP id x12sf5670177wgg.0 for ; Fri, 09 Jan 2015 16:34:48 -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=4wG6t7BCbQ349sbsIYcB2mhpIRSebSBC+LBMZiDuWXY=; b=hOa43l+8q8ZtvGmWrCXekrN5kIdK6zGPl2fnPzSxzemIZLYq0kCagatRyNLv31sZwe DrPjKda1LAiMpzjVd+CUFCB1yeJz28c03CBaqGjwB+/SKcQMd62nzv+ZvH6wngN0lOPK e/44+mbXnJGWTuaDknpPJQPVY1zZUoGt47z8xKHaiD69/i5HCN4e2VsbcexyH5MJ3/NW 0rSkQjRO8+oWL/nroz002uXbha/UkFloL01ZW9c11Ro9UZQXXo+8vt/kYRkCo8lCtE/t HK0dIchacObKYOdvyPmhThlDmUFJhPDsnSeSkilBGZYQ3Rf0l68y86hlNtoFZl66Cqvc 8zdw== X-Gm-Message-State: ALoCoQlzzTjPYreNuewRg+tHqa3L7JJqbmp2gymROM3plxaQulQEP6I7cgttBPoTuz03ZTfDsiss X-Received: by 10.180.82.34 with SMTP id f2mr759091wiy.1.1420850088917; Fri, 09 Jan 2015 16:34:48 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.28.41 with SMTP id y9ls527752lag.54.gmail; Fri, 09 Jan 2015 16:34:48 -0800 (PST) X-Received: by 10.112.159.195 with SMTP id xe3mr24249150lbb.64.1420850088719; Fri, 09 Jan 2015 16:34:48 -0800 (PST) Received: from mail-la0-f51.google.com (mail-la0-f51.google.com. [209.85.215.51]) by mx.google.com with ESMTPS id q8si14599888laj.104.2015.01.09.16.34.48 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 09 Jan 2015 16:34:48 -0800 (PST) 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 mail-la0-f51.google.com with SMTP id ms9so17065532lab.10 for ; Fri, 09 Jan 2015 16:34:48 -0800 (PST) X-Received: by 10.152.23.38 with SMTP id j6mr24554890laf.81.1420850088634; Fri, 09 Jan 2015 16:34:48 -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 c8csp484818lbb; Fri, 9 Jan 2015 16:34:47 -0800 (PST) X-Received: by 10.70.47.202 with SMTP id f10mr27332822pdn.127.1420850080097; Fri, 09 Jan 2015 16:34:40 -0800 (PST) Received: from mail-pa0-f42.google.com (mail-pa0-f42.google.com. [209.85.220.42]) by mx.google.com with ESMTPS id wm4si15309016pbc.131.2015.01.09.16.34.39 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 09 Jan 2015 16:34:40 -0800 (PST) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.42 as permitted sender) client-ip=209.85.220.42; Received: by mail-pa0-f42.google.com with SMTP id et14so21482987pad.1 for ; Fri, 09 Jan 2015 16:34:39 -0800 (PST) X-Received: by 10.70.45.106 with SMTP id l10mr27930455pdm.12.1420850079219; Fri, 09 Jan 2015 16:34:39 -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.38 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 09 Jan 2015 16:34:38 -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 04/10] clocksource: Add max_cycles to clocksource structure Date: Fri, 9 Jan 2015 16:34:22 -0800 Message-Id: <1420850068-27828-5-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.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: , In order to facilitate some clocksource validation, add a max_cycles entry to the structure which will hold the maximum cycle value that can safely be multiplied without potentially causing an overflow. 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 --- include/linux/clocksource.h | 5 +++-- kernel/time/clocksource.c | 15 ++++++++++++--- kernel/time/sched_clock.c | 2 +- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/include/linux/clocksource.h b/include/linux/clocksource.h index abcafaa..9b54cb9 100644 --- a/include/linux/clocksource.h +++ b/include/linux/clocksource.h @@ -178,7 +178,7 @@ struct clocksource { #ifdef CONFIG_ARCH_CLOCKSOURCE_DATA struct arch_clocksource_data archdata; #endif - + u64 max_cycles; const char *name; struct list_head list; int rating; @@ -291,7 +291,8 @@ extern struct clocksource * __init clocksource_default_clock(void); extern void clocksource_mark_unstable(struct clocksource *cs); extern u64 -clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask); +clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, + u64 *max_cycles); extern void clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 minsec); diff --git a/kernel/time/clocksource.c b/kernel/time/clocksource.c index 0696559..c2f8639 100644 --- a/kernel/time/clocksource.c +++ b/kernel/time/clocksource.c @@ -545,11 +545,14 @@ static u32 clocksource_max_adjustment(struct clocksource *cs) * @shift: cycle to nanosecond divisor (power of two) * @maxadj: maximum adjustment value to mult (~11%) * @mask: bitmask for two's complement subtraction of non 64 bit counters + * @max_cyc: maximum cycle value before potential overflow (does not include + * any saftey margin) * * NOTE: This function includes a saftey margin of 50%, so that bad clock values * can be detected. */ -u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask) +u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, + u64 *max_cyc) { u64 max_nsecs, max_cycles; @@ -569,6 +572,10 @@ u64 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask) max_cycles = min(max_cycles, mask); max_nsecs = clocksource_cyc2ns(max_cycles, mult - maxadj, shift); + /* return the max_cycles value as well if requested */ + if (max_cyc) + *max_cyc = max_cycles; + /* Return 50% of the actual maximum, so we can detect bad values */ max_nsecs >>= 1; @@ -747,7 +754,8 @@ void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq) } cs->max_idle_ns = clocks_calc_max_nsecs(cs->mult, cs->shift, - cs->maxadj, cs->mask); + cs->maxadj, cs->mask, + &cs->max_cycles); } EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale); @@ -795,7 +803,8 @@ int clocksource_register(struct clocksource *cs) /* calculate max idle time permitted for this clocksource */ cs->max_idle_ns = clocks_calc_max_nsecs(cs->mult, cs->shift, - cs->maxadj, cs->mask); + cs->maxadj, cs->mask, + &cs->max_cycles); mutex_lock(&clocksource_mutex); clocksource_enqueue(cs); diff --git a/kernel/time/sched_clock.c b/kernel/time/sched_clock.c index c794b84..2910f00 100644 --- a/kernel/time/sched_clock.c +++ b/kernel/time/sched_clock.c @@ -126,7 +126,7 @@ void __init sched_clock_register(u64 (*read)(void), int bits, new_mask = CLOCKSOURCE_MASK(bits); /* calculate how many ns until we risk wrapping */ - wrap = clocks_calc_max_nsecs(new_mult, new_shift, 0, new_mask); + wrap = clocks_calc_max_nsecs(new_mult, new_shift, 0, new_mask, 0); new_wrap_kt = ns_to_ktime(wrap); /* update epoch for new counter and update epoch_ns from old counter*/