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

scsi: 3w-9xxx: rework lock timeouts

The TW_IOCTL_GET_LOCK ioctl uses do_gettimeofday() to check whether a
lock has expired. This can misbehave due to a concurrent settimeofday()
call, as it is based on 'real' time, and it will overflow in y2038 on
32-bit architectures, producing unexpected results when used across the
overflow time.

This changes it to using monotonic time, using ktime_get() to simplify
the code.

Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Acked-by: Adam Radford <aradford@gmail.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>

authored by

Arnd Bergmann and committed by
Martin K. Petersen
07ffd4ce bc8f9166

+7 -8
+6 -7
drivers/scsi/3w-9xxx.c
··· 645 645 TW_Command_Full *full_command_packet; 646 646 TW_Compatibility_Info *tw_compat_info; 647 647 TW_Event *event; 648 - struct timeval current_time; 649 - u32 current_time_ms; 648 + ktime_t current_time; 650 649 TW_Device_Extension *tw_dev = twa_device_extension_list[iminor(inode)]; 651 650 int retval = TW_IOCTL_ERROR_OS_EFAULT; 652 651 void __user *argp = (void __user *)arg; ··· 836 837 break; 837 838 case TW_IOCTL_GET_LOCK: 838 839 tw_lock = (TW_Lock *)tw_ioctl->data_buffer; 839 - do_gettimeofday(&current_time); 840 - current_time_ms = (current_time.tv_sec * 1000) + (current_time.tv_usec / 1000); 840 + current_time = ktime_get(); 841 841 842 - if ((tw_lock->force_flag == 1) || (tw_dev->ioctl_sem_lock == 0) || (current_time_ms >= tw_dev->ioctl_msec)) { 842 + if ((tw_lock->force_flag == 1) || (tw_dev->ioctl_sem_lock == 0) || 843 + ktime_after(current_time, tw_dev->ioctl_time)) { 843 844 tw_dev->ioctl_sem_lock = 1; 844 - tw_dev->ioctl_msec = current_time_ms + tw_lock->timeout_msec; 845 + tw_dev->ioctl_time = ktime_add_ms(current_time, tw_lock->timeout_msec); 845 846 tw_ioctl->driver_command.status = 0; 846 847 tw_lock->time_remaining_msec = tw_lock->timeout_msec; 847 848 } else { 848 849 tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_LOCKED; 849 - tw_lock->time_remaining_msec = tw_dev->ioctl_msec - current_time_ms; 850 + tw_lock->time_remaining_msec = ktime_ms_delta(tw_dev->ioctl_time, current_time); 850 851 } 851 852 break; 852 853 case TW_IOCTL_RELEASE_LOCK:
+1 -1
drivers/scsi/3w-9xxx.h
··· 666 666 unsigned char event_queue_wrapped; 667 667 unsigned int error_sequence_id; 668 668 int ioctl_sem_lock; 669 - u32 ioctl_msec; 669 + ktime_t ioctl_time; 670 670 int chrdev_request_id; 671 671 wait_queue_head_t ioctl_wqueue; 672 672 struct mutex ioctl_lock;