Commit ebf918cf authored by Tina Ruchandani's avatar Tina Ruchandani Committed by David S. Miller

isdn: Use ktime_t instead of 'struct timeval'

'struct timeval' uses 32-bit representation for seconds which will
overflow in year 2038 and beyond. mISDN/clock.c needs to compute and
store elapsed time in intervals of 125 microseconds. This patch replaces
the usage of 'struct timeval' with 64-bit ktime_t which is y2038 safe.
The patch also replaces do_gettimeofday() (wall-clock time) with
ktime_get() (monotonic time) since we only care about elapsed time here.
Signed-off-by: default avatarTina Ruchandani <ruchandani.tina@gmail.com>
Suggested-by: default avatarArnd Bergmnann <arnd@arndb.de>
Suggested-by: default avatarDavid Miller <davem@davemloft.net>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 4320f219
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
#include <linux/types.h> #include <linux/types.h>
#include <linux/stddef.h> #include <linux/stddef.h>
#include <linux/spinlock.h> #include <linux/spinlock.h>
#include <linux/ktime.h>
#include <linux/mISDNif.h> #include <linux/mISDNif.h>
#include <linux/export.h> #include <linux/export.h>
#include "core.h" #include "core.h"
...@@ -45,15 +46,15 @@ static u_int *debug; ...@@ -45,15 +46,15 @@ static u_int *debug;
static LIST_HEAD(iclock_list); static LIST_HEAD(iclock_list);
static DEFINE_RWLOCK(iclock_lock); static DEFINE_RWLOCK(iclock_lock);
static u16 iclock_count; /* counter of last clock */ static u16 iclock_count; /* counter of last clock */
static struct timeval iclock_tv; /* time stamp of last clock */ static ktime_t iclock_timestamp; /* time stamp of last clock */
static int iclock_tv_valid; /* already received one timestamp */ static int iclock_timestamp_valid; /* already received one timestamp */
static struct mISDNclock *iclock_current; static struct mISDNclock *iclock_current;
void void
mISDN_init_clock(u_int *dp) mISDN_init_clock(u_int *dp)
{ {
debug = dp; debug = dp;
do_gettimeofday(&iclock_tv); iclock_timestamp = ktime_get();
} }
static void static void
...@@ -86,7 +87,7 @@ select_iclock(void) ...@@ -86,7 +87,7 @@ select_iclock(void)
} }
if (bestclock != iclock_current) { if (bestclock != iclock_current) {
/* no clock received yet */ /* no clock received yet */
iclock_tv_valid = 0; iclock_timestamp_valid = 0;
} }
iclock_current = bestclock; iclock_current = bestclock;
} }
...@@ -139,12 +140,11 @@ mISDN_unregister_clock(struct mISDNclock *iclock) ...@@ -139,12 +140,11 @@ mISDN_unregister_clock(struct mISDNclock *iclock)
EXPORT_SYMBOL(mISDN_unregister_clock); EXPORT_SYMBOL(mISDN_unregister_clock);
void void
mISDN_clock_update(struct mISDNclock *iclock, int samples, struct timeval *tv) mISDN_clock_update(struct mISDNclock *iclock, int samples, ktime_t *timestamp)
{ {
u_long flags; u_long flags;
struct timeval tv_now; ktime_t timestamp_now;
time_t elapsed_sec; u16 delta;
int elapsed_8000th;
write_lock_irqsave(&iclock_lock, flags); write_lock_irqsave(&iclock_lock, flags);
if (iclock_current != iclock) { if (iclock_current != iclock) {
...@@ -156,33 +156,27 @@ mISDN_clock_update(struct mISDNclock *iclock, int samples, struct timeval *tv) ...@@ -156,33 +156,27 @@ mISDN_clock_update(struct mISDNclock *iclock, int samples, struct timeval *tv)
write_unlock_irqrestore(&iclock_lock, flags); write_unlock_irqrestore(&iclock_lock, flags);
return; return;
} }
if (iclock_tv_valid) { if (iclock_timestamp_valid) {
/* increment sample counter by given samples */ /* increment sample counter by given samples */
iclock_count += samples; iclock_count += samples;
if (tv) { /* tv must be set, if function call is delayed */ if (timestamp) { /* timestamp must be set, if function call is delayed */
iclock_tv.tv_sec = tv->tv_sec; iclock_timestamp = *timestamp;
iclock_tv.tv_usec = tv->tv_usec; } else {
} else iclock_timestamp = ktime_get();
do_gettimeofday(&iclock_tv); }
} else { } else {
/* calc elapsed time by system clock */ /* calc elapsed time by system clock */
if (tv) { /* tv must be set, if function call is delayed */ if (timestamp) { /* timestamp must be set, if function call is delayed */
tv_now.tv_sec = tv->tv_sec; timestamp_now = *timestamp;
tv_now.tv_usec = tv->tv_usec; } else {
} else timestamp_now = ktime_get();
do_gettimeofday(&tv_now);
elapsed_sec = tv_now.tv_sec - iclock_tv.tv_sec;
elapsed_8000th = (tv_now.tv_usec / 125)
- (iclock_tv.tv_usec / 125);
if (elapsed_8000th < 0) {
elapsed_sec -= 1;
elapsed_8000th += 8000;
} }
delta = ktime_divns(ktime_sub(timestamp_now, iclock_timestamp),
(NSEC_PER_SEC / 8000));
/* add elapsed time to counter and set new timestamp */ /* add elapsed time to counter and set new timestamp */
iclock_count += elapsed_sec * 8000 + elapsed_8000th; iclock_count += delta;
iclock_tv.tv_sec = tv_now.tv_sec; iclock_timestamp = timestamp_now;
iclock_tv.tv_usec = tv_now.tv_usec; iclock_timestamp_valid = 1;
iclock_tv_valid = 1;
if (*debug & DEBUG_CLOCK) if (*debug & DEBUG_CLOCK)
printk("Received first clock from source '%s'.\n", printk("Received first clock from source '%s'.\n",
iclock_current ? iclock_current->name : "nothing"); iclock_current ? iclock_current->name : "nothing");
...@@ -195,22 +189,17 @@ unsigned short ...@@ -195,22 +189,17 @@ unsigned short
mISDN_clock_get(void) mISDN_clock_get(void)
{ {
u_long flags; u_long flags;
struct timeval tv_now; ktime_t timestamp_now;
time_t elapsed_sec; u16 delta;
int elapsed_8000th;
u16 count; u16 count;
read_lock_irqsave(&iclock_lock, flags); read_lock_irqsave(&iclock_lock, flags);
/* calc elapsed time by system clock */ /* calc elapsed time by system clock */
do_gettimeofday(&tv_now); timestamp_now = ktime_get();
elapsed_sec = tv_now.tv_sec - iclock_tv.tv_sec; delta = ktime_divns(ktime_sub(timestamp_now, iclock_timestamp),
elapsed_8000th = (tv_now.tv_usec / 125) - (iclock_tv.tv_usec / 125); (NSEC_PER_SEC / 8000));
if (elapsed_8000th < 0) {
elapsed_sec -= 1;
elapsed_8000th += 8000;
}
/* add elapsed time to counter */ /* add elapsed time to counter */
count = iclock_count + elapsed_sec * 8000 + elapsed_8000th; count = iclock_count + delta;
read_unlock_irqrestore(&iclock_lock, flags); read_unlock_irqrestore(&iclock_lock, flags);
return count; return count;
} }
......
...@@ -596,7 +596,7 @@ static inline struct mISDNdevice *dev_to_mISDN(struct device *dev) ...@@ -596,7 +596,7 @@ static inline struct mISDNdevice *dev_to_mISDN(struct device *dev)
} }
extern void set_channel_address(struct mISDNchannel *, u_int, u_int); extern void set_channel_address(struct mISDNchannel *, u_int, u_int);
extern void mISDN_clock_update(struct mISDNclock *, int, struct timeval *); extern void mISDN_clock_update(struct mISDNclock *, int, ktime_t *);
extern unsigned short mISDN_clock_get(void); extern unsigned short mISDN_clock_get(void);
extern const char *mISDNDevName4ch(struct mISDNchannel *); extern const char *mISDNDevName4ch(struct mISDNchannel *);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment