From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752425AbdKCWrT (ORCPT ); Fri, 3 Nov 2017 18:47:19 -0400 Received: from mout.kundenserver.de ([217.72.192.74]:62130 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751536AbdKCWrS (ORCPT ); Fri, 3 Nov 2017 18:47:18 -0400 From: Arnd Bergmann To: "Ed L. Cashin" Cc: Tina Ruchandani , Arnd Bergmann , Jens Axboe , Kees Cook , linux-kernel@vger.kernel.org Subject: [PATCH] aoe: use ktime_t instead of timeval Date: Fri, 3 Nov 2017 23:46:54 +0100 Message-Id: <20171103224709.3882760-1-arnd@arndb.de> X-Mailer: git-send-email 2.9.0 X-Provags-ID: V03:K0:i7qZf0LrpWw43CjzpDIDfq82SO632/K0nkBrU55KIlpEQ6Bv7j5 FJ+GsPr1UO2K+GK3nIfPjqgNWf/AtsIO3uvOVNBjEEe1Z3HDd/x/NFv0GY9miHV/C73Elp+ S5AABiFpQglkPU/OgI47DDoLeJc+MNIuADvQ2km8p7KnMUAayFzDfzakHH2zdKbc922XPG9 zq+bd6t/5tzuqerCklYTQ== X-UI-Out-Filterresults: notjunk:1;V01:K0:qLyxr/jL+Io=:5ZgN1RgABiMxcE6E0IE8BD SylKvAbtHdwyyDtE7WDyTb7wtgwCtvPVygE3Pg9/25qi8A/aWEeY/nHBGu2Mq+wnHQYuFbdv4 DiTyHS5Oap16A12cf9IJZzg0Tp0ez36NOmWku+W2i0biXcMWSIvOSBnI/XogNg3rFgHyJhDJ/ aZ+j8Owl4JvNCuQ1QVuwI58Ss1Vjya8P2MdbH2/CJtl+xCfZggCX/S2OzfDpJfHrHM63VPzKA vowRbJ1vx91DWvpEDVT5pHXmmtTMFDcRkz/BhnKIAn8yVEf4HLdGhwzRjn1iGd0w1tQA8gkQd mBUGCkO2dUItY89gVcUJYPjie9jnYfFfKEC8HX8gZAqN4nfahdAu9NosAwcgLYv1OrKEtEqXR hoEzU9nQiFNvAlB9Q32plLup30znMYKcNA64fEGtQgsEzr0mxuCUNiINwJvVtNvEj093IsNQu Ql4ptzwMNSYAV/lnkU2xk2UOFlakZrZ815HT/HVt8jBIXLAs/KVT4RDUf0h7o5emzXgHNfqoj BiyyRxoMFUERn2r0KONnOOlo67tMOW3H/S4jN34L9o4m1T+6ZupKbP1k77zBc13sTtF9O2iZ3 9IE5PTsT1oVEEq7xgv06QFJiZYWmxg68WkH6VDk9ptmgNDG1ML7zcJCWbJYUyjSOnoDy88Eom 8+CxmkzyETxAWu3Wtni2++hyLbI0/6s1aYxjyv7fZysvtdXYRbRJQr0qijpJgrWjPAUdmmHs5 WYlFsaIB1CC8lAv7BmMJpU1EI3OzZV2FA9qZOQ== Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Tina Ruchandani 'struct frame' uses two variables to store the sent timestamp - 'struct timeval' and jiffies. jiffies is used to avoid discrepancies caused by updates to system time. 'struct timeval' is deprecated because it uses 32-bit representation for seconds which will overflow in year 2038. This patch does the following: - Replace the use of 'struct timeval' and jiffies with ktime_t, which is the recommended type for timestamping - ktime_t provides both long range (like jiffies) and high resolution (like timeval). Using ktime_get (monotonic time) instead of wall-clock time prevents any discprepancies caused by updates to system time. [updates by Arnd below] The original patch from Tina never went anywhere as we discussed how to keep the impact on performance minimal. I've started over now but arrived at basically the same patch that she had originally, except for an slightly improved tsince_hr() function. I'm making it more robust against overflows, and also optimize explicitly for the common case in which a frame is less than 4.2 seconds old, using only a 32-bit division in that case. This should make the new version more efficient than the old code, since we replace the existing two 32-bit division in do_gettimeofday() plus one multiplication with a single single 32-bit division in tsince_hr() and drop the double bookkeeping. It's also more efficient than the ktime_get_us() API we discussed before, since that would also rely on multiple divisions. Link: https://lists.linaro.org/pipermail/y2038/2015-May/000276.html Signed-off-by: Tina Ruchandani Cc: Ed Cashin Signed-off-by: Arnd Bergmann --- drivers/block/aoe/aoe.h | 3 +-- drivers/block/aoe/aoecmd.c | 48 +++++++++++++--------------------------------- 2 files changed, 14 insertions(+), 37 deletions(-) diff --git a/drivers/block/aoe/aoe.h b/drivers/block/aoe/aoe.h index 9220f8e833d0..c0ebda1283cc 100644 --- a/drivers/block/aoe/aoe.h +++ b/drivers/block/aoe/aoe.h @@ -112,8 +112,7 @@ enum frame_flags { struct frame { struct list_head head; u32 tag; - struct timeval sent; /* high-res time packet was sent */ - u32 sent_jiffs; /* low-res jiffies-based sent time */ + ktime_t sent; /* high-res time packet was sent */ ulong waited; ulong waited_total; struct aoetgt *t; /* parent target I belong to */ diff --git a/drivers/block/aoe/aoecmd.c b/drivers/block/aoe/aoecmd.c index 55ab25f79a08..ee8217cbd8e9 100644 --- a/drivers/block/aoe/aoecmd.c +++ b/drivers/block/aoe/aoecmd.c @@ -398,8 +398,7 @@ aoecmd_ata_rw(struct aoedev *d) skb = skb_clone(f->skb, GFP_ATOMIC); if (skb) { - do_gettimeofday(&f->sent); - f->sent_jiffs = (u32) jiffies; + f->sent = ktime_get(); __skb_queue_head_init(&queue); __skb_queue_tail(&queue, skb); aoenet_xmit(&queue); @@ -489,8 +488,7 @@ resend(struct aoedev *d, struct frame *f) skb = skb_clone(skb, GFP_ATOMIC); if (skb == NULL) return; - do_gettimeofday(&f->sent); - f->sent_jiffs = (u32) jiffies; + f->sent = ktime_get(); __skb_queue_head_init(&queue); __skb_queue_tail(&queue, skb); aoenet_xmit(&queue); @@ -499,33 +497,17 @@ resend(struct aoedev *d, struct frame *f) static int tsince_hr(struct frame *f) { - struct timeval now; - int n; + u64 delta = ktime_to_ns(ktime_sub(ktime_get(), f->sent)); - do_gettimeofday(&now); - n = now.tv_usec - f->sent.tv_usec; - n += (now.tv_sec - f->sent.tv_sec) * USEC_PER_SEC; + /* delta is normally under 4.2 seconds, avoid 64-bit division */ + if (likely(delta <= UINT_MAX)) + return (u32)delta / NSEC_PER_USEC; - if (n < 0) - n = -n; + /* avoid overflow after 71 minutes */ + if (delta > ((u64)INT_MAX * NSEC_PER_USEC)) + return INT_MAX; - /* For relatively long periods, use jiffies to avoid - * discrepancies caused by updates to the system time. - * - * On system with HZ of 1000, 32-bits is over 49 days - * worth of jiffies, or over 71 minutes worth of usecs. - * - * Jiffies overflow is handled by subtraction of unsigned ints: - * (gdb) print (unsigned) 2 - (unsigned) 0xfffffffe - * $3 = 4 - * (gdb) - */ - if (n > USEC_PER_SEC / 4) { - n = ((u32) jiffies) - f->sent_jiffs; - n *= USEC_PER_SEC / HZ; - } - - return n; + return div_u64(delta, NSEC_PER_USEC); } static int @@ -589,7 +571,6 @@ reassign_frame(struct frame *f) nf->waited = 0; nf->waited_total = f->waited_total; nf->sent = f->sent; - nf->sent_jiffs = f->sent_jiffs; f->skb = skb; return nf; @@ -633,8 +614,7 @@ probe(struct aoetgt *t) skb = skb_clone(f->skb, GFP_ATOMIC); if (skb) { - do_gettimeofday(&f->sent); - f->sent_jiffs = (u32) jiffies; + f->sent = ktime_get(); __skb_queue_head_init(&queue); __skb_queue_tail(&queue, skb); aoenet_xmit(&queue); @@ -1432,10 +1412,8 @@ aoecmd_ata_id(struct aoedev *d) d->timer.function = (TIMER_FUNC_TYPE)rexmit_timer; skb = skb_clone(skb, GFP_ATOMIC); - if (skb) { - do_gettimeofday(&f->sent); - f->sent_jiffs = (u32) jiffies; - } + if (skb) + f->sent = ktime_get(); return skb; } -- 2.9.0