From patchwork Mon Aug 17 20:40:56 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 52478 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 patches.linaro.org (Postfix) with ESMTPS id 1D6722156D for ; Mon, 17 Aug 2015 20:41:17 +0000 (UTC) Received: by lbck9 with SMTP id k9sf49284249lbc.0 for ; Mon, 17 Aug 2015 13:41:16 -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:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-type :content-transfer-encoding:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=3d0BFbPDjduspNraW2OxHMyaEZjBcPrLT9QwiFupJU8=; b=TT6XsnoUB0An3H9wIdd+TTxhyXMXP32P7L6zYJTHs3AnogFcdjfZnigUms7THt/878 CY4oL+Z8g+BxoXrxzmjCx4k9mJTHK7qlrMI5C7MXJVOqCuf91Gv3zQICrnuQtJwVgYSH Ta1JCfwdUcpfhDw5X3a48zYT0t8CJHGfXExwncePbaxmmzu3lAsXZ+gF0rFMDkzux0ma 9SMPJUO1MIl7gQgHHIWyBqtXM5GwDuigVSl7dV2ZMAKbFeq2nuVUPyTE7cFT0umDpfwp ER0r4s9NPyEaxG4BCF/r5wB9/O1gxkxAn1JiIpcKjLROGkM9Sl8xXWGBoJtIYUIYQtIv g9LA== X-Gm-Message-State: ALoCoQmkse9ZA38oVWTGR/xdLsyJICPAtt4TmYYoojZwK2IHUow8WgWOFLNu8pdQNPk8ab/MA+3T X-Received: by 10.180.187.180 with SMTP id ft20mr1014570wic.1.1439844076015; Mon, 17 Aug 2015 13:41:16 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.207.8 with SMTP id ls8ls700061lac.71.gmail; Mon, 17 Aug 2015 13:41:15 -0700 (PDT) X-Received: by 10.152.178.229 with SMTP id db5mr2680260lac.55.1439844075851; Mon, 17 Aug 2015 13:41:15 -0700 (PDT) Received: from mail-la0-f54.google.com (mail-la0-f54.google.com. [209.85.215.54]) by mx.google.com with ESMTPS id pk1si12357791lac.126.2015.08.17.13.41.15 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 17 Aug 2015 13:41:15 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.54 as permitted sender) client-ip=209.85.215.54; Received: by labd1 with SMTP id d1so87269201lab.1 for ; Mon, 17 Aug 2015 13:41:15 -0700 (PDT) X-Received: by 10.152.5.228 with SMTP id v4mr2593890lav.36.1439844075558; Mon, 17 Aug 2015 13:41:15 -0700 (PDT) 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.162.200 with SMTP id yc8csp85870lbb; Mon, 17 Aug 2015 13:41:14 -0700 (PDT) X-Received: by 10.68.241.166 with SMTP id wj6mr6094674pbc.114.1439844071783; Mon, 17 Aug 2015 13:41:11 -0700 (PDT) Received: from mail-pa0-f54.google.com (mail-pa0-f54.google.com. [209.85.220.54]) by mx.google.com with ESMTPS id q2si26346937pdo.21.2015.08.17.13.41.10 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 17 Aug 2015 13:41:11 -0700 (PDT) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 209.85.220.54 as permitted sender) client-ip=209.85.220.54; Received: by pacgr6 with SMTP id gr6so115680116pac.2 for ; Mon, 17 Aug 2015 13:41:10 -0700 (PDT) X-Received: by 10.68.203.42 with SMTP id kn10mr6107396pbc.43.1439844070749; Mon, 17 Aug 2015 13:41:10 -0700 (PDT) Received: from localhost.localdomain (c-76-115-103-22.hsd1.or.comcast.net. [76.115.103.22]) by smtp.gmail.com with ESMTPSA id gu2sm85199pbc.1.2015.08.17.13.41.09 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 17 Aug 2015 13:41:09 -0700 (PDT) From: John Stultz To: lkml Cc: Karsten Blees , Prarit Bhargava , Richard Cochran , Ingo Molnar , Thomas Gleixner , Karsten Blees , John Stultz Subject: [PATCH 2/9] time: Fix nanosecond file time rounding in timespec_trunc() Date: Mon, 17 Aug 2015 13:40:56 -0700 Message-Id: <1439844063-7957-3-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1439844063-7957-1-git-send-email-john.stultz@linaro.org> References: <1439844063-7957-1-git-send-email-john.stultz@linaro.org> MIME-Version: 1.0 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.54 as permitted sender) smtp.mailfrom=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: , From: Karsten Blees timespec_trunc() avoids rounding if granularity <= nanoseconds-per-jiffie (or TICK_NSEC). This optimization assumes that: 1. current_kernel_time().tv_nsec is already rounded to TICK_NSEC (i.e. with HZ=1000 you'd get 1000000, 2000000, 3000000... but never 1000001). This is no longer true (probably since hrtimers introduced in 2.6.16). 2. TICK_NSEC is evenly divisible by all possible granularities. This may be true for HZ=100, 250, 1000, but obviously not for HZ=300 / TICK_NSEC=3333333 (introduced in 2.6.20). Thus, sub-second portions of in-core file times are not rounded to on-disk granularity. I.e. file times may change when the inode is re-read from disk or when the file system is remounted. This affects all file systems with file time granularities > 1 ns and < 1s, e.g. CEPH (1000 ns), UDF (1000 ns), CIFS (100 ns), NTFS (100 ns) and FUSE (configurable from user mode via struct fuse_init_out.time_gran). Steps to reproduce with e.g. UDF: $ dd if=/dev/zero of=udfdisk count=10000 && mkudffs udfdisk $ mkdir udf && mount udfdisk udf $ touch udf/test && stat -c %y udf/test 2015-06-09 10:22:56.130006767 +0200 $ umount udf && mount udfdisk udf $ stat -c %y udf/test 2015-06-09 10:22:56.130006000 +0200 Remounting truncates the mtime to 1 µs. Fix the rounding in timespec_trunc() and update the documentation. timespec_trunc() is exclusively used to calculate inode's [acm]time (mostly via current_fs_time()), and always with super_block.s_time_gran as second argument. So this can safely be changed without side effects. Note: This does _not_ fix the issue for FAT's 2 second mtime resolution, as super_block.s_time_gran isn't prepared to handle different ctime / mtime / atime resolutions nor resolutions > 1 second. Cc: Prarit Bhargava Cc: Richard Cochran Cc: Ingo Molnar Cc: Thomas Gleixner Signed-off-by: Karsten Blees Signed-off-by: John Stultz --- kernel/time/time.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/kernel/time/time.c b/kernel/time/time.c index 85d5bb1..34dbd42 100644 --- a/kernel/time/time.c +++ b/kernel/time/time.c @@ -287,26 +287,20 @@ EXPORT_SYMBOL(jiffies_to_usecs); * @t: Timespec * @gran: Granularity in ns. * - * Truncate a timespec to a granularity. gran must be smaller than a second. - * Always rounds down. - * - * This function should be only used for timestamps returned by - * current_kernel_time() or CURRENT_TIME, not with do_gettimeofday() because - * it doesn't handle the better resolution of the latter. + * Truncate a timespec to a granularity. Always rounds down. gran must + * not be 0 nor greater than a second (NSEC_PER_SEC, or 10^9 ns). */ struct timespec timespec_trunc(struct timespec t, unsigned gran) { - /* - * Division is pretty slow so avoid it for common cases. - * Currently current_kernel_time() never returns better than - * jiffies resolution. Exploit that. - */ - if (gran <= jiffies_to_usecs(1) * 1000) { + /* Avoid division in the common cases 1 ns and 1 s. */ + if (gran == 1) { /* nothing */ - } else if (gran == 1000000000) { + } else if (gran == NSEC_PER_SEC) { t.tv_nsec = 0; - } else { + } else if (gran > 1 && gran < NSEC_PER_SEC) { t.tv_nsec -= t.tv_nsec % gran; + } else { + WARN(1, "illegal file time granularity: %u", gran); } return t; }