From patchwork Fri Nov 3 22:46:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnd Bergmann X-Patchwork-Id: 117967 Delivered-To: patch@linaro.org Received: by 10.140.22.164 with SMTP id 33csp262715qgn; Fri, 3 Nov 2017 15:47:25 -0700 (PDT) X-Google-Smtp-Source: ABhQp+S/b2BCGOO6ZzxoylE6y3WRNf3QEpEEU791/g5H4sVf2PAF+dNH/VaWNDn+1Ba89UwU2r1m X-Received: by 10.101.73.7 with SMTP id p7mr8707837pgs.106.1509749244950; Fri, 03 Nov 2017 15:47:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1509749244; cv=none; d=google.com; s=arc-20160816; b=G8XBe+heCvMV59AoLUnT7TyUB0VLWoxqP3I8T7+Q6qPb1VgYYly+lwc6z8+2+zf/GP LKF2HS6hEtrgUrPQfzmHsggRuEczdZplTjK5P9FdBjemEM1Q85o32atK3WMJpFqpWIAF 1Jaq7DM0yicUwn38egpwbPGw6RKLCJhsAnYVil08X8NYNo8QujfCKOnXFRb4buPLkrtM 9HJVasEHTDidqCJ81nP27Il3Oc3sBAgTHU1IdAUc5Z1hGqWB1Jr9MMutX3AYmgn2Euto bHF86IZV4TZFjOaZWRmwT5aaTkDeMYsoRwAWewdrOn0hph/loCHhDKxjce5aG101qHe9 F3uw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :arc-authentication-results; bh=4Aq2sBVHbAbj1b7zPgq19hEp8y56FtqeQ4Y6qLYzq3s=; b=O6Nb1WyGvZGRvqWw7tXClRhD8OjmA9brUZ8Q4r8rxRYEjTiceFCecXxhLIVUCgBkaY eP58dB809tshlK2F4d+hXPjEJd39ilONGTYCYu5veDAZvDf4yIB+QDdBfWHfy1PYB5GU PqNpmza9vgujkILJIFZ3TDKaDZofhfHf5m99nGDUv4P6Krn0B5jdkd9dKHXnUNB4XhQ6 VvTlC/pDNbt9T5/nxq2qr4K7Q1I+ON26cyryIKr6wVW/pQzmXEVzOAtItNkhpUsDj+iE DlVvWurKvUERF+LNxD6N1iziIlo+J0zBDQEvTX43CMlZlnKtTiJ137RZfTDScuS1lqCX RudQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 3si5700281plz.564.2017.11.03.15.47.24; Fri, 03 Nov 2017 15:47:24 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752446AbdKCWrT (ORCPT + 26 others); 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 Received: from wuerfel.lan ([109.193.157.232]) by mrelayeu.kundenserver.de (mreue105 [212.227.15.145]) with ESMTPA (Nemesis) id 0MLyRQ-1e336g3HGG-007jUK; Fri, 03 Nov 2017 23:47:10 +0100 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 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9qZOQ== Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk 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(-) -- 2.9.0 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; }