Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

gdth: replace struct timeval with ktime_get_real_seconds()

struct timeval will overflow on 32-bit systems in y2038 and is being
removed from the kernel. Replace the use of struct timeval and
do_gettimeofday() with ktime_get_real_seconds() which provides a 64-bit
seconds value and is y2038 safe.

gdth driver requires changes in two areas:

1) gdth_store_event() loads two u32 timestamp fields for ioctl GDTIOCTL_EVENT

These timestamp fields are part of struct gdth_evt_str used for passing
event data to userspace. At the first instance of an event we do
(first_stamp=last_stamp="current time"). If that same event repeats,
we do (last_stamp="current time") AND increment same_count to indicate
how many times the event has repeated since first_stamp.

This patch replaces the use of timeval and do_gettimeofday() with
ktime_get_real_seconds() cast to u32 to extend the timestamp fields
to y2106.

Beyond y2106, the userspace tools (ie. RAID controller monitors) can
work around the time rollover and this driver would still not need to
change.

Alternative: The alternative approach is to introduce a new ioctl in gdth
with the u32 time fields defined as u64. This would require userspace
changes now, but not in y2106.

2) gdth_show_info() calculates elapsed time using u32 first_stamp

It is adding events with timestamps to a seq_file. Timestamps are
calculated as the "current time" minus the first_stamp.

This patch replaces the use of timeval and do_gettimeofday() with
ktime_get_real_seconds() cast to u32 to calculate the timestamp.

This elapsed time calculation is safe even when the time wraps (beyond
y2106) due to how unsigned subtraction works. A comment has been added
to the code to indicate this safety.

Alternative: This piece itself doesn't warrant an alternative, but
if we do introduce a new structure & ioctl with u64 timestamps, this
would change accordingly.

Signed-off-by: Alison Schofield <amsfield22@gmail.com>
Reviewed-by: Arnd Bergmann <arnd@arndb.de>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>

authored by

Alison Schofield and committed by
Martin K. Petersen
5a412c38 ba690c79

+10 -8
+2 -5
drivers/scsi/gdth.c
··· 2838 2838 u16 idx, gdth_evt_data *evt) 2839 2839 { 2840 2840 gdth_evt_str *e; 2841 - struct timeval tv; 2842 2841 2843 2842 /* no GDTH_LOCK_HA() ! */ 2844 2843 TRACE2(("gdth_store_event() source %d idx %d\n", source, idx)); ··· 2853 2854 !strcmp((char *)&ebuffer[elastidx].event_data.event_string, 2854 2855 (char *)&evt->event_string)))) { 2855 2856 e = &ebuffer[elastidx]; 2856 - do_gettimeofday(&tv); 2857 - e->last_stamp = tv.tv_sec; 2857 + e->last_stamp = (u32)ktime_get_real_seconds(); 2858 2858 ++e->same_count; 2859 2859 } else { 2860 2860 if (ebuffer[elastidx].event_source != 0) { /* entry not free ? */ ··· 2869 2871 e = &ebuffer[elastidx]; 2870 2872 e->event_source = source; 2871 2873 e->event_idx = idx; 2872 - do_gettimeofday(&tv); 2873 - e->first_stamp = e->last_stamp = tv.tv_sec; 2874 + e->first_stamp = e->last_stamp = (u32)ktime_get_real_seconds(); 2874 2875 e->same_count = 1; 2875 2876 e->event_data = *evt; 2876 2877 e->application = 0;
+8 -3
drivers/scsi/gdth_proc.c
··· 148 148 gdth_cmd_str *gdtcmd; 149 149 gdth_evt_str *estr; 150 150 char hrec[161]; 151 - struct timeval tv; 152 151 153 152 char *buf; 154 153 gdth_dskstat_str *pds; ··· 539 540 if (estr->event_data.eu.driver.ionode == ha->hanum && 540 541 estr->event_source == ES_ASYNC) { 541 542 gdth_log_event(&estr->event_data, hrec); 542 - do_gettimeofday(&tv); 543 - sec = (int)(tv.tv_sec - estr->first_stamp); 543 + 544 + /* 545 + * Elapsed seconds subtraction with unsigned operands is 546 + * safe from wrap around in year 2106. Executes as: 547 + * operand a + (2's complement operand b) + 1 548 + */ 549 + 550 + sec = (int)((u32)ktime_get_real_seconds() - estr->first_stamp); 544 551 if (sec < 0) sec = 0; 545 552 seq_printf(m," date- %02d:%02d:%02d\t%s\n", 546 553 sec/3600, sec%3600/60, sec%60, hrec);