From patchwork Fri Jul 11 13:44:10 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 33494 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-qc0-f199.google.com (mail-qc0-f199.google.com [209.85.216.199]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 09766203C0 for ; Fri, 11 Jul 2014 14:00:08 +0000 (UTC) Received: by mail-qc0-f199.google.com with SMTP id c9sf4183412qcz.2 for ; Fri, 11 Jul 2014 07:00:07 -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=glndgiHuiWWXeosPben3VhiZvz99VLE6Mp119baYgK4pyw4bo9hdlb33ktNQvg/FWC 3nkn9uESnDJD69oBS/U5I9Gd0N+/TZF5mfxQsunXy+8ewavt+0Oh9+B3aW4UJHKlenDu cA9GdHuKvZadV+WPlWP7iC/OlVG1Qjb4lziDmQ29FqRJ7XT2uzqyF6WvZm/sDl6hliYp EOzLAH0CszBSPg/FHGYyK5KzS/vqtpQR/jZ0hPEZK9yMml2CRO92dTovijSIDRaCQ5y/ Baxtw6qHSNpr5GcEe+JliOOQbiL8rIoZ2c4R3Y21pne7j4Pn7m9ftZzkCVkKJVuZ+AtG GRgg== X-Gm-Message-State: ALoCoQleZj1GFJPMU/wtDBQ5lx+lSOGYA6kzYoG+yUID49Z9p/+Xw36Bu6W4c9hTSE/9nrXzXzb8 X-Received: by 10.236.198.134 with SMTP id v6mr22929034yhn.49.1405087207862; Fri, 11 Jul 2014 07:00:07 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.90.21 with SMTP id w21ls396135qgd.1.gmail; Fri, 11 Jul 2014 07:00:07 -0700 (PDT) X-Received: by 10.221.68.135 with SMTP id xy7mr825966vcb.65.1405087207776; Fri, 11 Jul 2014 07:00:07 -0700 (PDT) Received: from mail-vc0-f169.google.com (mail-vc0-f169.google.com [209.85.220.169]) by mx.google.com with ESMTPS id rq6si1534278vcb.97.2014.07.11.07.00.07 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 11 Jul 2014 07:00:07 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.169 as permitted sender) client-ip=209.85.220.169; Received: by mail-vc0-f169.google.com with SMTP id la4so2122138vcb.14 for ; Fri, 11 Jul 2014 07:00:07 -0700 (PDT) X-Received: by 10.58.207.84 with SMTP id lu20mr8461708vec.9.1405087207684; Fri, 11 Jul 2014 07:00:07 -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 tc5csp37561vcb; Fri, 11 Jul 2014 07:00:06 -0700 (PDT) X-Received: by 10.70.103.74 with SMTP id fu10mr23868947pdb.119.1405087206551; Fri, 11 Jul 2014 07:00:06 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x10si1280738pde.159.2014.07.11.07.00.05; Fri, 11 Jul 2014 07:00:05 -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 S1754653AbaGKN7z (ORCPT + 28 others); Fri, 11 Jul 2014 09:59:55 -0400 Received: from www.linutronix.de ([62.245.132.108]:56062 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753937AbaGKNoL (ORCPT ); Fri, 11 Jul 2014 09:44:11 -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 1X5b7S-0004Bt-An; Fri, 11 Jul 2014 15:44:10 +0200 Message-Id: <20140711133705.904278824@linutronix.de> User-Agent: quilt/0.63-1 Date: Fri, 11 Jul 2014 13:44:10 -0000 From: Thomas Gleixner To: LKML Cc: John Stultz , Peter Zijlstra Subject: [patch 08/55] time: More core infrastructure for timespec64 References: <20140711133623.530368377@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.169 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: