From patchwork Wed Jul 16 21:03:59 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 33763 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-oi0-f69.google.com (mail-oi0-f69.google.com [209.85.218.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id C6FD120792 for ; Wed, 16 Jul 2014 21:21:56 +0000 (UTC) Received: by mail-oi0-f69.google.com with SMTP id h136sf1035769oig.0 for ; Wed, 16 Jul 2014 14:21:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:message-id:user-agent:date:from:to :cc:subject:references:mime-version:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe:content-type :content-disposition; bh=1Sw5tZwSJgJQKdlCueyz3NUY0gU5Jp+NNilo5LJSmEU=; b=SQ2PuIuqA+JLTIx6YXGu/hV1nGuhZajmsUHttCBhJTyufD7wQtNo5jH8AT9yN07dRx OyNe6UQUZi+NHx5+kQeuLx8P0N0otuy3ifcLLRfItsvDkCu2irSEPrtcGXf7kUgmZAgM 4e6ri3e5V06Du1cKGQQHKHE7rAKouQIyE0rCPRfjeLUQPrOPz1lH+tYKfiJtg/iYQvai x95C6Bpwg9MMwnGlL6rF0322ys5U1Kqmm5RR/N17MgVzTxcxKESPIf4WX2b2IRpSqZtb Cj3/pLPZMOEBgCvPViZFCYse4OY9awefddCncLbdK+vkPG77+ZjWN1lq1/eg77sEtFo+ HTCg== X-Gm-Message-State: ALoCoQmFQGyR1JNP7pjOn4ugPQtM6nQevoMKkn1QNg7jcf/PP/lczWquinU5O6rz5RpI7386VllB X-Received: by 10.182.105.230 with SMTP id gp6mr16992208obb.18.1405545716279; Wed, 16 Jul 2014 14:21:56 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.102.18 with SMTP id v18ls514607qge.51.gmail; Wed, 16 Jul 2014 14:21:56 -0700 (PDT) X-Received: by 10.220.250.203 with SMTP id mp11mr33212741vcb.2.1405545716195; Wed, 16 Jul 2014 14:21:56 -0700 (PDT) Received: from mail-vc0-f178.google.com (mail-vc0-f178.google.com [209.85.220.178]) by mx.google.com with ESMTPS id ab11si788716vec.98.2014.07.16.14.21.56 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 16 Jul 2014 14:21:56 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.178 as permitted sender) client-ip=209.85.220.178; Received: by mail-vc0-f178.google.com with SMTP id la4so2838369vcb.37 for ; Wed, 16 Jul 2014 14:21:56 -0700 (PDT) X-Received: by 10.53.5.230 with SMTP id cp6mr6769430vdd.25.1405545716115; Wed, 16 Jul 2014 14:21:56 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.221.37.5 with SMTP id tc5csp330125vcb; Wed, 16 Jul 2014 14:21:55 -0700 (PDT) X-Received: by 10.68.201.167 with SMTP id kb7mr32718871pbc.38.1405545715189; Wed, 16 Jul 2014 14:21:55 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id hb11si417978pbd.88.2014.07.16.14.21.54 for ; Wed, 16 Jul 2014 14:21:54 -0700 (PDT) Received-SPF: none (google.com: linux-kernel-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754325AbaGPVVw (ORCPT + 26 others); Wed, 16 Jul 2014 17:21:52 -0400 Received: from www.linutronix.de ([62.245.132.108]:59617 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752330AbaGPVEB (ORCPT ); Wed, 16 Jul 2014 17:04:01 -0400 Received: from localhost ([127.0.0.1] helo=[127.0.1.1]) by Galois.linutronix.de with esmtp (Exim 4.80) (envelope-from ) id 1X7WMq-000409-3f; Wed, 16 Jul 2014 23:04:00 +0200 Message-Id: <20140716205052.849635925@linutronix.de> User-Agent: quilt/0.63-1 Date: Wed, 16 Jul 2014 21:03:59 -0000 From: Thomas Gleixner To: LKML Cc: John Stultz , Peter Zijlstra Subject: [patch V2 08/64] time: More core infrastructure for timespec64 References: <20140716205018.175419210@linutronix.de> MIME-Version: 1.0 X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1, SHORTCIRCUIT=-0.0001 Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: tglx@linutronix.de X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.178 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , Content-Disposition: inline; filename=jstultz-tk-99c585d.patch From: John Stultz Helper and conversion functions for timespec64. Signed-off-by: John Stultz Signed-off-by: Thomas Gleixner --- include/linux/ktime.h | 28 ++++++++++++++++++++++ include/linux/time64.h | 28 ++++++++++++++++++++++ kernel/time/time.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+) -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/ Index: tip/include/linux/ktime.h =================================================================== --- tip.orig/include/linux/ktime.h +++ tip/include/linux/ktime.h @@ -83,6 +83,12 @@ static inline ktime_t timespec_to_ktime( return ktime_set(ts.tv_sec, ts.tv_nsec); } +/* convert a timespec64 to ktime_t format: */ +static inline ktime_t timespec64_to_ktime(struct timespec64 ts) +{ + return ktime_set(ts.tv_sec, ts.tv_nsec); +} + /* convert a timeval to ktime_t format: */ static inline ktime_t timeval_to_ktime(struct timeval tv) { @@ -92,6 +98,9 @@ static inline ktime_t timeval_to_ktime(s /* Map the ktime_t to timespec conversion to ns_to_timespec function */ #define ktime_to_timespec(kt) ns_to_timespec((kt).tv64) +/* Map the ktime_t to timespec conversion to ns_to_timespec function */ +#define ktime_to_timespec64(kt) ns_to_timespec64((kt).tv64) + /* Map the ktime_t to timeval conversion to ns_to_timeval function */ #define ktime_to_timeval(kt) ns_to_timeval((kt).tv64) @@ -186,6 +195,25 @@ static inline __must_check bool ktime_to return true; } else { return false; + } +} + +/** + * ktime_to_timespec64_cond - convert a ktime_t variable to timespec64 + * format only if the variable contains data + * @kt: the ktime_t variable to convert + * @ts: the timespec variable to store the result in + * + * Return: %true if there was a successful conversion, %false if kt was 0. + */ +static inline __must_check bool ktime_to_timespec64_cond(const ktime_t kt, + struct timespec64 *ts) +{ + if (kt.tv64) { + *ts = ktime_to_timespec64(kt); + return true; + } else { + return false; } } Index: tip/include/linux/time64.h =================================================================== --- tip.orig/include/linux/time64.h +++ tip/include/linux/time64.h @@ -33,6 +33,16 @@ struct timespec64 { #if __BITS_PER_LONG == 64 +static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64) +{ + return ts64; +} + +static inline struct timespec64 timespec_to_timespec64(const struct timespec ts) +{ + return ts; +} + # define timespec64_equal timespec_equal # define timespec64_compare timespec_compare # define set_normalized_timespec64 set_normalized_timespec @@ -47,6 +57,24 @@ struct timespec64 { #else +static inline struct timespec timespec64_to_timespec(const struct timespec64 ts64) +{ + struct timespec ret; + + ret.tv_sec = (time_t)ts64.tv_sec; + ret.tv_nsec = ts64.tv_nsec; + return ret; +} + +static inline struct timespec64 timespec_to_timespec64(const struct timespec ts) +{ + struct timespec64 ret; + + ret.tv_sec = ts.tv_sec; + ret.tv_nsec = ts.tv_nsec; + return ret; +} + static inline int timespec64_equal(const struct timespec64 *a, const struct timespec64 *b) { Index: tip/kernel/time/time.c =================================================================== --- tip.orig/kernel/time/time.c +++ tip/kernel/time/time.c @@ -420,6 +420,68 @@ struct timeval ns_to_timeval(const s64 n } EXPORT_SYMBOL(ns_to_timeval); +#if BITS_PER_LONG == 32 +/** + * set_normalized_timespec - set timespec sec and nsec parts and normalize + * + * @ts: pointer to timespec variable to be set + * @sec: seconds to set + * @nsec: nanoseconds to set + * + * Set seconds and nanoseconds field of a timespec variable and + * normalize to the timespec storage format + * + * Note: The tv_nsec part is always in the range of + * 0 <= tv_nsec < NSEC_PER_SEC + * For negative values only the tv_sec field is negative ! + */ +void set_normalized_timespec64(struct timespec64 *ts, time64_t sec, s64 nsec) +{ + while (nsec >= NSEC_PER_SEC) { + /* + * The following asm() prevents the compiler from + * optimising this loop into a modulo operation. See + * also __iter_div_u64_rem() in include/linux/time.h + */ + asm("" : "+rm"(nsec)); + nsec -= NSEC_PER_SEC; + ++sec; + } + while (nsec < 0) { + asm("" : "+rm"(nsec)); + nsec += NSEC_PER_SEC; + --sec; + } + ts->tv_sec = sec; + ts->tv_nsec = nsec; +} +EXPORT_SYMBOL(set_normalized_timespec64); + +/** + * ns_to_timespec64 - Convert nanoseconds to timespec64 + * @nsec: the nanoseconds value to be converted + * + * Returns the timespec64 representation of the nsec parameter. + */ +struct timespec64 ns_to_timespec64(const s64 nsec) +{ + struct timespec64 ts; + s32 rem; + + if (!nsec) + return (struct timespec64) {0, 0}; + + ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem); + if (unlikely(rem < 0)) { + ts.tv_sec--; + rem += NSEC_PER_SEC; + } + ts.tv_nsec = rem; + + return ts; +} +EXPORT_SYMBOL(ns_to_timespec64); +#endif /* * When we convert to jiffies then we interpret incoming values * the following way: