Skip to content

Commit 85cf955

Browse files
Tinabr7axboe
authored andcommitted
aoe: use ktime_t instead of timeval
'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 <ruchandani.tina@gmail.com> Cc: Ed Cashin <ed.cashin@acm.org> Signed-off-by: Arnd Bergmann <arnd@arndb.de> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent ddc2123 commit 85cf955

2 files changed

Lines changed: 14 additions & 37 deletions

File tree

‎drivers/block/aoe/aoe.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ enum frame_flags {
112112
struct frame {
113113
struct list_head head;
114114
u32 tag;
115-
struct timeval sent; /* high-res time packet was sent */
116-
u32 sent_jiffs; /* low-res jiffies-based sent time */
115+
ktime_t sent; /* high-res time packet was sent */
117116
ulong waited;
118117
ulong waited_total;
119118
struct aoetgt *t; /* parent target I belong to */

‎drivers/block/aoe/aoecmd.c‎

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,7 @@ aoecmd_ata_rw(struct aoedev *d)
398398

399399
skb = skb_clone(f->skb, GFP_ATOMIC);
400400
if (skb) {
401-
do_gettimeofday(&f->sent);
402-
f->sent_jiffs = (u32) jiffies;
401+
f->sent = ktime_get();
403402
__skb_queue_head_init(&queue);
404403
__skb_queue_tail(&queue, skb);
405404
aoenet_xmit(&queue);
@@ -489,8 +488,7 @@ resend(struct aoedev *d, struct frame *f)
489488
skb = skb_clone(skb, GFP_ATOMIC);
490489
if (skb == NULL)
491490
return;
492-
do_gettimeofday(&f->sent);
493-
f->sent_jiffs = (u32) jiffies;
491+
f->sent = ktime_get();
494492
__skb_queue_head_init(&queue);
495493
__skb_queue_tail(&queue, skb);
496494
aoenet_xmit(&queue);
@@ -499,33 +497,17 @@ resend(struct aoedev *d, struct frame *f)
499497
static int
500498
tsince_hr(struct frame *f)
501499
{
502-
struct timeval now;
503-
int n;
500+
u64 delta = ktime_to_ns(ktime_sub(ktime_get(), f->sent));
504501

505-
do_gettimeofday(&now);
506-
n = now.tv_usec - f->sent.tv_usec;
507-
n += (now.tv_sec - f->sent.tv_sec) * USEC_PER_SEC;
502+
/* delta is normally under 4.2 seconds, avoid 64-bit division */
503+
if (likely(delta <= UINT_MAX))
504+
return (u32)delta / NSEC_PER_USEC;
508505

509-
if (n < 0)
510-
n = -n;
506+
/* avoid overflow after 71 minutes */
507+
if (delta > ((u64)INT_MAX * NSEC_PER_USEC))
508+
return INT_MAX;
511509

512-
/* For relatively long periods, use jiffies to avoid
513-
* discrepancies caused by updates to the system time.
514-
*
515-
* On system with HZ of 1000, 32-bits is over 49 days
516-
* worth of jiffies, or over 71 minutes worth of usecs.
517-
*
518-
* Jiffies overflow is handled by subtraction of unsigned ints:
519-
* (gdb) print (unsigned) 2 - (unsigned) 0xfffffffe
520-
* $3 = 4
521-
* (gdb)
522-
*/
523-
if (n > USEC_PER_SEC / 4) {
524-
n = ((u32) jiffies) - f->sent_jiffs;
525-
n *= USEC_PER_SEC / HZ;
526-
}
527-
528-
return n;
510+
return div_u64(delta, NSEC_PER_USEC);
529511
}
530512

531513
static int
@@ -589,7 +571,6 @@ reassign_frame(struct frame *f)
589571
nf->waited = 0;
590572
nf->waited_total = f->waited_total;
591573
nf->sent = f->sent;
592-
nf->sent_jiffs = f->sent_jiffs;
593574
f->skb = skb;
594575

595576
return nf;
@@ -633,8 +614,7 @@ probe(struct aoetgt *t)
633614

634615
skb = skb_clone(f->skb, GFP_ATOMIC);
635616
if (skb) {
636-
do_gettimeofday(&f->sent);
637-
f->sent_jiffs = (u32) jiffies;
617+
f->sent = ktime_get();
638618
__skb_queue_head_init(&queue);
639619
__skb_queue_tail(&queue, skb);
640620
aoenet_xmit(&queue);
@@ -1432,10 +1412,8 @@ aoecmd_ata_id(struct aoedev *d)
14321412
d->timer.function = rexmit_timer;
14331413

14341414
skb = skb_clone(skb, GFP_ATOMIC);
1435-
if (skb) {
1436-
do_gettimeofday(&f->sent);
1437-
f->sent_jiffs = (u32) jiffies;
1438-
}
1415+
if (skb)
1416+
f->sent = ktime_get();
14391417

14401418
return skb;
14411419
}

0 commit comments

Comments
 (0)