From patchwork Fri Nov 10 15:58:27 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnd Bergmann X-Patchwork-Id: 10053405 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3232C60365 for ; Fri, 10 Nov 2017 15:59:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 24E982B2B3 for ; Fri, 10 Nov 2017 15:59:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 199D82B35A; Fri, 10 Nov 2017 15:59:59 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C66702B2B3 for ; Fri, 10 Nov 2017 15:59:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751885AbdKJP7Q (ORCPT ); Fri, 10 Nov 2017 10:59:16 -0500 Received: from mout.kundenserver.de ([217.72.192.74]:56813 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751191AbdKJP7P (ORCPT ); Fri, 10 Nov 2017 10:59:15 -0500 Received: from wuerfel.lan ([109.193.157.232]) by mrelayeu.kundenserver.de (mreue103 [212.227.15.145]) with ESMTPA (Nemesis) id 0Lo2Ke-1esr7S2DlI-00fyum; Fri, 10 Nov 2017 16:59:06 +0100 From: Arnd Bergmann To: Adam Radford , "James E.J. Bottomley" , "Martin K. Petersen" Cc: Arnd Bergmann , Sumit Saxena , linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/3] scsi: 3w-9xxx: rework lock timeouts Date: Fri, 10 Nov 2017 16:58:27 +0100 Message-Id: <20171110155840.1941250-3-arnd@arndb.de> X-Mailer: git-send-email 2.9.0 In-Reply-To: <20171110155840.1941250-1-arnd@arndb.de> References: <20171110155840.1941250-1-arnd@arndb.de> X-Provags-ID: V03:K0:6KCvk5auDwKDHtsDO5IWcEMfx+CBrbjts+B2Y8yA352GP5nQOrL VoO9Vupcynn8p+OKuDqxOD0GUeUe9KK8Tm/xIdlpPpgMjWcPgboHlw11Z+shRYlWL8GdlY1 3O/6NBR1dalK1pEjYex+X/1/6N+32W2T9Q8MZL9uPksjtpR19qSDsD9G9yqHqKPTvrO2Oln g/pmToQJpjJ3zdIwIGlJg== X-UI-Out-Filterresults: notjunk:1; V01:K0:Yfi0bK9b1iU=:eEf5vz0mAElNMceiK7Vs/y nEn3y23na4qfWilX1MCD6rKAy3nX1VgDMd0+B4XvWKiqlsAKYKElI5Hs55x+ATVBJ4AF1cGKI cidDLUaENvGRklciGX6UfUxh8a/ZTq76eu8DxXl4rRVvHPABgUx5gJ3NRFGcANsXsRdZaWlEi 2i3ycvOxz+9nv36VW8l9sf9bQvsId0O5pw0OBVHurXjOtphfyDysDeDYnAxXKp02B4f7FoW8n gY8+++8x+YIaiEk/Ze7i3Mf9ZGy6Rs45OdlD64M/OY3m/RGVOGZoWs3C1BONcTbMtOHq8O3vU iZLPJgq+acM3aHAj+swAWmuYafBzoSoH5zFaBMC3akqx9t+beHgqpiJfTx4p59yQ7mCen7vdM gGEO/T7pt0HcyCwVARitwpxeQngRoWmOGmO0fsKwdodrCvLs0OQL8/06xlSTDaxX6oixCwDdY +ya8CJ8/GyD584JgczMvFnMSKs5THqEAmtNl7QxlDPH0aFZW84KcEYW8htQYPw+8ad1TnWtUn Flw2aGse656YjCR3WUfmje76H9oOldiFRTPfA+eTG+E9XKc6Hx3AMCYDHuY/+HKbhqjrh0gfL 5wnA4S3EBnMzVrC1x8II1SUnYmfN4rQKL7n1fDtKa5hkb61AtZ0/YjoW+lnVu2AwjQAR6t1uP 8z7UD1gkf2/2rKPbeLJAVXAy+CFVaoxVRsJLwnZU6sT5NpGOWidjHCpz8pm5RWZsQa8zDntsG PVgV9ggh2PK9t9CfT3mS8NpUrECEcISht0IRqw== Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 Acked-by: Adam Radford --- drivers/scsi/3w-9xxx.c | 13 ++++++------- drivers/scsi/3w-9xxx.h | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/drivers/scsi/3w-9xxx.c b/drivers/scsi/3w-9xxx.c index b1c9bd9c1bfd..b42c9c479d4b 100644 --- a/drivers/scsi/3w-9xxx.c +++ b/drivers/scsi/3w-9xxx.c @@ -645,8 +645,7 @@ static long twa_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long TW_Command_Full *full_command_packet; TW_Compatibility_Info *tw_compat_info; TW_Event *event; - struct timeval current_time; - u32 current_time_ms; + ktime_t current_time; TW_Device_Extension *tw_dev = twa_device_extension_list[iminor(inode)]; int retval = TW_IOCTL_ERROR_OS_EFAULT; void __user *argp = (void __user *)arg; @@ -837,17 +836,17 @@ static long twa_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long break; case TW_IOCTL_GET_LOCK: tw_lock = (TW_Lock *)tw_ioctl->data_buffer; - do_gettimeofday(¤t_time); - current_time_ms = (current_time.tv_sec * 1000) + (current_time.tv_usec / 1000); + current_time = ktime_get(); - if ((tw_lock->force_flag == 1) || (tw_dev->ioctl_sem_lock == 0) || (current_time_ms >= tw_dev->ioctl_msec)) { + if ((tw_lock->force_flag == 1) || (tw_dev->ioctl_sem_lock == 0) || + ktime_after(current_time, tw_dev->ioctl_time)) { tw_dev->ioctl_sem_lock = 1; - tw_dev->ioctl_msec = current_time_ms + tw_lock->timeout_msec; + tw_dev->ioctl_time = ktime_add_ms(current_time, tw_lock->timeout_msec); tw_ioctl->driver_command.status = 0; tw_lock->time_remaining_msec = tw_lock->timeout_msec; } else { tw_ioctl->driver_command.status = TW_IOCTL_ERROR_STATUS_LOCKED; - tw_lock->time_remaining_msec = tw_dev->ioctl_msec - current_time_ms; + tw_lock->time_remaining_msec = ktime_ms_delta(tw_dev->ioctl_time, current_time); } break; case TW_IOCTL_RELEASE_LOCK: diff --git a/drivers/scsi/3w-9xxx.h b/drivers/scsi/3w-9xxx.h index b6c208cc474f..d88cd3499bd5 100644 --- a/drivers/scsi/3w-9xxx.h +++ b/drivers/scsi/3w-9xxx.h @@ -666,7 +666,7 @@ typedef struct TAG_TW_Device_Extension { unsigned char event_queue_wrapped; unsigned int error_sequence_id; int ioctl_sem_lock; - u32 ioctl_msec; + ktime_t ioctl_time; int chrdev_request_id; wait_queue_head_t ioctl_wqueue; struct mutex ioctl_lock;