From patchwork Wed Jan 17 15:30:39 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnd Bergmann X-Patchwork-Id: 124814 Delivered-To: patch@linaro.org Received: by 10.46.62.1 with SMTP id l1csp83022lja; Wed, 17 Jan 2018 07:31:04 -0800 (PST) X-Google-Smtp-Source: ACJfBouoikD37Y3i2Vnv5W5E8WZ1ZWYpj5xnSPuYGR8LdmlIpKh9dNMoCv/ZbuuQADKH+2CL0Lps X-Received: by 10.99.134.194 with SMTP id x185mr1311801pgd.252.1516203064433; Wed, 17 Jan 2018 07:31:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516203064; cv=none; d=google.com; s=arc-20160816; b=LFiXF398OhF24hDoYAgUYO0K82J3OkGiC038tDzKJikt5a1gCxEyEfzcqIhzranDJb geCU8IsLcEktN914n7090fIfK00q/djXymJPB9rqFY5DAbjbolHOWjE6hq9EXD8v2uRh Er7S0iGl9+JRwzZ0nsYNZem+uCy2SGkOgtdOBMxZ+Lh8+Sny2oHOTupfabHRK/xiaDbe kPMv50u40G+Saf1kxmoICGZ3tkwz2MM+H7L2ZU/BU1X7vW23pOosj9VWTvhc0/LwZF10 Z8lrqMe7N4cIx03mY+FYioDB9R/Sz/jx7T27zD9qHAIy0izhTgovZc9bxcIr5Jdu5c1A 58uQ== 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=Iqg4lVt3hHJx1hAsftwVBQ3aljVxxqAg+eABjXq2Sqw=; b=pkzsvahlsFjYI0tUfUYDCOThsb6HGCIoSYwFCDWDln9PU9HkRBi52i8PUWryqz2Jnu KzhNhGG2sU33zA/L1bQmIcRkH7g4/0sSCZNQZdlkSEkcwWeLFIHByPTF2+jA9q+fgT/C 6y+HeGUMMIFzO+U2LYCP0z6q2DZdaMYiZQaXQ8ooLciiYj911JbfFOwCeBO08CN5Jr/v hmya9DB8zXmngBEBmJtIuEZn/+xFS/UKSlf+6E1qdEqstK2Q0G6MfUFzxqNsrre7TWIS kDwKW3LfDJgYXobvSWen5gvUU34fDvW0/IHpiD25ZSKW7v7L9fyqHwandXO0hj6GVL+f GCSQ== 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 v67si4544659pfj.193.2018.01.17.07.31.04; Wed, 17 Jan 2018 07:31:04 -0800 (PST) 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 S1754093AbeAQPbD (ORCPT + 28 others); Wed, 17 Jan 2018 10:31:03 -0500 Received: from mout.kundenserver.de ([212.227.126.131]:58599 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753570AbeAQPbA (ORCPT ); Wed, 17 Jan 2018 10:31:00 -0500 Received: from wuerfel.lan ([95.208.111.237]) by mrelayeu.kundenserver.de (mreue001 [212.227.15.129]) with ESMTPA (Nemesis) id 0MBO57-1ejDek2MK5-00AFuV; Wed, 17 Jan 2018 16:30:55 +0100 From: Arnd Bergmann To: "Ed L. Cashin" Cc: Tina Ruchandani , Arnd Bergmann , Jens Axboe , linux-kernel@vger.kernel.org Subject: [PATCH] [RESEND] aoe: use ktime_t instead of timeval Date: Wed, 17 Jan 2018 16:30:39 +0100 Message-Id: <20180117153052.3831762-1-arnd@arndb.de> X-Mailer: git-send-email 2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ernel-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 --- I submitted this version in November 2017, but got no reply from Ed. Jens, could you pick it up directly? --- 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 812fed069708..540bb60cd071 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 = 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; }