From patchwork Mon Oct 27 05:46:32 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "pang.xunlei" X-Patchwork-Id: 39545 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f200.google.com (mail-lb0-f200.google.com [209.85.217.200]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 1A49724026 for ; Mon, 27 Oct 2014 05:47:21 +0000 (UTC) Received: by mail-lb0-f200.google.com with SMTP id f15sf971978lbj.3 for ; Sun, 26 Oct 2014 22:47:20 -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:sender:precedence:list-id:x-original-sender :x-original-authentication-results:mailing-list:list-post:list-help :list-archive:list-unsubscribe; bh=cqIP5ddCYCWLUhSgsBYl0LMy+gA8H3SMoKzexQlEXlQ=; b=mj3F9sZPjXAzaUXQI+vM3rws91fzrAaYIeA5khnJf/L0jI1zsuFSU6ahTmIjd/NuTn sAE6pzXYy79/zhhYqtfOt/NZ031SrU78p+hAm6BdDAMF6amlRPEVb6CMmpihDVxl68fR g+QjoV34rHsYfIcnWWx2iCw89AUmwqIhq3fMFJXA0Cv+bqgbDci4BXXs/f0gyj5THGyj PSi336JREfHrcbvaRPcORvx51hgBFpr2d0CSJMyvTLtTZWSqFG2we6QTiqbM7n+0Xqtl AK16Ho8exKnOCjk2nbv7kLytzmNGhboXSZKsLrTZZYZsFzjnSgLoQrx11bYoUpyeummw yjeg== X-Gm-Message-State: ALoCoQm4ezHbwCifD2RyW6cjKVs9vDfDe3lq5E7HXoTt5G6eWIva/GEgvPUot50ml4K+rdiz53YR X-Received: by 10.112.247.74 with SMTP id yc10mr146159lbc.8.1414388840518; Sun, 26 Oct 2014 22:47:20 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.115.207 with SMTP id jq15ls736449lab.49.gmail; Sun, 26 Oct 2014 22:47:20 -0700 (PDT) X-Received: by 10.152.120.133 with SMTP id lc5mr21072866lab.62.1414388840284; Sun, 26 Oct 2014 22:47:20 -0700 (PDT) Received: from mail-la0-f46.google.com (mail-la0-f46.google.com. [209.85.215.46]) by mx.google.com with ESMTPS id b4si18211394lbd.70.2014.10.26.22.47.20 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 26 Oct 2014 22:47:20 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.46 as permitted sender) client-ip=209.85.215.46; Received: by mail-la0-f46.google.com with SMTP id gi9so5289592lab.33 for ; Sun, 26 Oct 2014 22:47:20 -0700 (PDT) X-Received: by 10.152.6.228 with SMTP id e4mr20920388laa.71.1414388840184; Sun, 26 Oct 2014 22:47:20 -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 c5csp235666lbz; Sun, 26 Oct 2014 22:47:19 -0700 (PDT) X-Received: by 10.70.87.201 with SMTP id ba9mr913142pdb.124.1414388838492; Sun, 26 Oct 2014 22:47:18 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ku4si9693440pab.87.2014.10.26.22.47.15 for ; Sun, 26 Oct 2014 22:47:18 -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 S1751999AbaJ0FrL (ORCPT + 26 others); Mon, 27 Oct 2014 01:47:11 -0400 Received: from mail-pd0-f180.google.com ([209.85.192.180]:42789 "EHLO mail-pd0-f180.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751368AbaJ0FrJ (ORCPT ); Mon, 27 Oct 2014 01:47:09 -0400 Received: by mail-pd0-f180.google.com with SMTP id ft15so3044563pdb.11 for ; Sun, 26 Oct 2014 22:47:09 -0700 (PDT) X-Received: by 10.69.31.74 with SMTP id kk10mr21439198pbd.119.1414388829436; Sun, 26 Oct 2014 22:47:09 -0700 (PDT) Received: from wangdeqiang.com ([210.21.227.66]) by mx.google.com with ESMTPSA id jq5sm9723922pbc.32.2014.10.26.22.47.05 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 26 Oct 2014 22:47:08 -0700 (PDT) From: "pang.xunlei" To: linux-kernel@vger.kernel.org, rtc-linux@googlegroups.com Cc: John Stultz , Thomas Gleixner , Alessandro Zummo , "pang.xunlei" Subject: [PATCH RFC 02/12] time: Add mktime() safe version(using time64_t) Date: Mon, 27 Oct 2014 13:46:32 +0800 Message-Id: <1414388802-5866-1-git-send-email-pang.xunlei@linaro.org> X-Mailer: git-send-email 1.7.9.5 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.215.46 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: , The kernel uses 32-bit signed value(time_t) for seconds since 1970-01-01:00:00:00, thus it will overflow at 2038-01-19 03:14:08 on 32-bit systems. We call this "2038 safety" issue. As part of addressing 2038 saftey for in-kernel uses, this patch adds the safe mktime() using time64_t. After this patch, mktime_unsafe() should be replaced by mktime() one by one. Eventually, mktime_unsafe() will be removed from the kernel when it has no users. Signed-off-by: pang.xunlei --- include/linux/time.h | 8 ++++++-- kernel/time/time.c | 39 ++++++++++++++++++++++++++------------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/include/linux/time.h b/include/linux/time.h index e49d421..08f413c 100644 --- a/include/linux/time.h +++ b/include/linux/time.h @@ -39,9 +39,13 @@ static inline int timeval_compare(const struct timeval *lhs, const struct timeva return lhs->tv_usec - rhs->tv_usec; } +extern time64_t 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); + extern unsigned long mktime_unsafe(const unsigned int year, const unsigned int mon, - const unsigned int day, const unsigned int hour, - const unsigned int min, const unsigned int sec); + const unsigned int day, const unsigned int hour, + const unsigned int min, const unsigned int sec); extern void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec); diff --git a/kernel/time/time.c b/kernel/time/time.c index d6a4464..f9521fa 100644 --- a/kernel/time/time.c +++ b/kernel/time/time.c @@ -304,7 +304,8 @@ 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. +/* + * 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. * @@ -314,16 +315,13 @@ EXPORT_SYMBOL(timespec_trunc); * -year/100+year/400 terms, and add 10.] * * This algorithm was first published by Gauss (I think). - * - * 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) */ -unsigned long -mktime_unsafe(const unsigned int year0, const unsigned int mon0, +time64_t +mktime(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 */ @@ -332,12 +330,27 @@ mktime_unsafe(const unsigned int year0, const unsigned int mon0, year -= 1; } - return ((((unsigned long) - (year/4 - year/100 + year/400 + 367*mon/12 + day) + - year*365 - 719499 - )*24 + hour /* now have hours */ - )*60 + min /* now have minutes */ - )*60 + sec; /* finally seconds */ + 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(mktime); + +/* + * TODO: [2038 safety] 2038 unsafe for legacy, replace it by mktime(). + * WARNING: mktime_unsafe() 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) + */ +unsigned long mktime_unsafe(const unsigned int year0, const unsigned int mon0, + const unsigned int day, const unsigned int hour, + const unsigned int min, const unsigned int sec) +{ + return (unsigned long) mktime(year0, mon0, day, hour, min, sec); } EXPORT_SYMBOL(mktime_unsafe);