From patchwork Thu Oct 30 11:15:36 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "pang.xunlei" X-Patchwork-Id: 39817 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f198.google.com (mail-wi0-f198.google.com [209.85.212.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 901DC202FE for ; Thu, 30 Oct 2014 11:17:16 +0000 (UTC) Received: by mail-wi0-f198.google.com with SMTP id n3sf3041814wiv.1 for ; Thu, 30 Oct 2014 04:17:15 -0700 (PDT) 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:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe; bh=U8vr9mLTxD71XjBL9sUyMTWkO14cAdFJ8oYh3G0LGMY=; b=JUc7NpsTw97x55jM0+5og72UCMPG1PJAu0twCATKPquem291vTHRPbeIs0u16WW0JW esfm65oIr7J15Z5u6eRPJ8gfkHTk0i5SExYtRXeTqyB0D3Ztw/oal6SW1n+6pRwhogIx zIAaP4cBdNGoY7nfRwvgiyMZxLt0vKnphyXJxhKhNTQLrBesg85OR7bCPk1j1Z9JnaJL un5eZuRett4op6ZUrkm1Xuc3s34WBoz98eEWx7e5NrI1m0OiqQE0kd42Bbz1CoLYYx9h FWAUqqMKcdMO01y9tyIUlq19g/uuLYsW5VSKDsE8UdMbkS7LsThh/v3eXNLpW/XL4w2o 9MjA== X-Gm-Message-State: ALoCoQmrQV7sckCHhETC8l7J2Ss+ivQDGg5nomZ8109seJX9Rb5DzyV+HXnI1iSappwJ+W+jt0SG X-Received: by 10.194.178.163 with SMTP id cz3mr2793713wjc.1.1414667835741; Thu, 30 Oct 2014 04:17:15 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.153.4.33 with SMTP id cb1ls349799lad.89.gmail; Thu, 30 Oct 2014 04:17:15 -0700 (PDT) X-Received: by 10.112.239.12 with SMTP id vo12mr18212732lbc.81.1414667835450; Thu, 30 Oct 2014 04:17:15 -0700 (PDT) Received: from mail-lb0-f180.google.com (mail-lb0-f180.google.com. [209.85.217.180]) by mx.google.com with ESMTPS id x1si11540342lbw.43.2014.10.30.04.17.15 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 30 Oct 2014 04:17:15 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.217.180 as permitted sender) client-ip=209.85.217.180; Received: by mail-lb0-f180.google.com with SMTP id z12so4155153lbi.11 for ; Thu, 30 Oct 2014 04:17:15 -0700 (PDT) X-Received: by 10.152.87.98 with SMTP id w2mr17869776laz.27.1414667835338; Thu, 30 Oct 2014 04:17:15 -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.112.84.229 with SMTP id c5csp22347lbz; Thu, 30 Oct 2014 04:17:14 -0700 (PDT) X-Received: by 10.70.42.15 with SMTP id j15mr16683056pdl.17.1414667833675; Thu, 30 Oct 2014 04:17:13 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ao2si6410123pad.52.2014.10.30.04.17.12 for ; Thu, 30 Oct 2014 04:17:13 -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 S1759327AbaJ3LRA (ORCPT + 26 others); Thu, 30 Oct 2014 07:17:00 -0400 Received: from mail-pd0-f175.google.com ([209.85.192.175]:55742 "EHLO mail-pd0-f175.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1759304AbaJ3LQ6 (ORCPT ); Thu, 30 Oct 2014 07:16:58 -0400 Received: by mail-pd0-f175.google.com with SMTP id y13so4975021pdi.34 for ; Thu, 30 Oct 2014 04:16:58 -0700 (PDT) X-Received: by 10.68.163.65 with SMTP id yg1mr16434491pbb.21.1414667818116; Thu, 30 Oct 2014 04:16:58 -0700 (PDT) Received: from wangdeqiang.com ([210.21.227.66]) by mx.google.com with ESMTPSA id i10sm6874826pdr.21.2014.10.30.04.16.53 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Thu, 30 Oct 2014 04:16:57 -0700 (PDT) From: "pang.xunlei" To: linux-kernel@vger.kernel.org Cc: rtc-linux@googlegroups.com, xen-devel@lists.xenproject.org, John Stultz , Thomas Gleixner , Alessandro Zummo , Stefano Stabellini , "pang.xunlei" Subject: [RFC PATCH v2 02/11] time: Add mktime64() safe version(using time64_t) Date: Thu, 30 Oct 2014 19:15:36 +0800 Message-Id: <1414667745-7703-3-git-send-email-pang.xunlei@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1414667745-7703-1-git-send-email-pang.xunlei@linaro.org> References: <1414667745-7703-1-git-send-email-pang.xunlei@linaro.org> 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: pang.xunlei@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.180 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: , As part of addressing 2038 saftey for in-kernel uses, this patch adds safe mktime64() using time64_t. After this patch, mktime() should be replaced by mktime64() one by one. Eventually, mktime() will be removed from the kernel when it has no users. Signed-off-by: pang.xunlei --- include/linux/time.h | 4 ++++ kernel/time/time.c | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/include/linux/time.h b/include/linux/time.h index 8c42cf8..5c2c077 100644 --- a/include/linux/time.h +++ b/include/linux/time.h @@ -39,6 +39,10 @@ static inline int timeval_compare(const struct timeval *lhs, const struct timeva return lhs->tv_usec - rhs->tv_usec; } +extern time64_t mktime64(const unsigned int year, const unsigned int mon, + const unsigned int day, const unsigned int hour, + const unsigned int min, const unsigned int sec); + extern unsigned long mktime(const unsigned int year, const unsigned int mon, const unsigned int day, const unsigned int hour, const unsigned int min, const unsigned int sec); diff --git a/kernel/time/time.c b/kernel/time/time.c index a9ae20f..f44efdb 100644 --- a/kernel/time/time.c +++ b/kernel/time/time.c @@ -304,6 +304,44 @@ struct timespec timespec_trunc(struct timespec t, unsigned gran) } EXPORT_SYMBOL(timespec_trunc); +/* + * Converts Gregorian date to seconds since 1970-01-01 00:00:00. + * Assumes input in normal date format, i.e. 1980-12-31 23:59:59 + * => year=1980, mon=12, day=31, hour=23, min=59, sec=59. + * + * [For the Julian calendar (which was used in Russia before 1917, + * Britain & colonies before 1752, anywhere else before 1582, + * and is still in use by some communities) leave out the + * -year/100+year/400 terms, and add 10.] + * + * This algorithm was first published by Gauss (I think). + * + * Safe version 2038 safety on 32-bit systems. + */ +time64_t +mktime64(const unsigned int year0, const unsigned int mon0, + const unsigned int day, const unsigned int hour, + const unsigned int min, const unsigned int sec) +{ + time64_t ret; + unsigned int mon = mon0, year = year0; + + /* 1..12 -> 11,12,1..10 */ + if (0 >= (int) (mon -= 2)) { + mon += 12; /* Puts Feb last since it has leap day */ + year -= 1; + } + + ret = (year/4 - year/100 + year/400 + 367*mon/12 + day) + year*365 - 719499; + ret = ret*24 + hour; /* now have hours */ + ret = ret*60 + min; /* now have minutes */ + ret = ret*60 + sec; /* finally seconds */ + + return ret; +} + +EXPORT_SYMBOL(mktime64); + /* Converts Gregorian date to seconds since 1970-01-01 00:00:00. * Assumes input in normal date format, i.e. 1980-12-31 23:59:59 * => year=1980, mon=12, day=31, hour=23, min=59, sec=59. @@ -318,6 +356,7 @@ EXPORT_SYMBOL(timespec_trunc); * WARNING: this function will overflow on 2106-02-07 06:28:16 on * machines where long is 32-bit! (However, as time_t is signed, we * will already get problems at other places on 2038-01-19 03:14:08) + * TODO: [2038 safety] should be replaced by mktime64(). */ unsigned long mktime(const unsigned int year0, const unsigned int mon0,