From patchwork Tue Nov 14 16:44:17 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Michael J. Ruhl" X-Patchwork-Id: 10057895 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 BAD2960231 for ; Tue, 14 Nov 2017 16:44:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AC9632986E for ; Tue, 14 Nov 2017 16:44:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A163A2988B; Tue, 14 Nov 2017 16:44:24 +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=ham 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 914462988D for ; Tue, 14 Nov 2017 16:44:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753869AbdKNQoW (ORCPT ); Tue, 14 Nov 2017 11:44:22 -0500 Received: from mga02.intel.com ([134.134.136.20]:19884 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753670AbdKNQoW (ORCPT ); Tue, 14 Nov 2017 11:44:22 -0500 Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 14 Nov 2017 08:44:18 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.44,395,1505804400"; d="scan'208";a="173447770" Received: from sedona.ch.intel.com ([143.182.228.65]) by orsmga005.jf.intel.com with ESMTP; 14 Nov 2017 08:44:18 -0800 Received: from phlsvsles11.ph.intel.com (phlsvsles11.ph.intel.com [10.228.195.43]) by sedona.ch.intel.com (8.13.6/8.14.3/Standard MailSET/Hub) with ESMTP id vAEGiHFD014380 for ; Tue, 14 Nov 2017 09:44:18 -0700 Received: from phlsvslse11.ph.intel.com (localhost [127.0.0.1]) by phlsvsles11.ph.intel.com with ESMTP id vAEGiHlH024713 for ; Tue, 14 Nov 2017 11:44:17 -0500 Subject: [PATCH v3 4/4] ibacm: Use MONOTONIC time base to avoid timer expiration issues To: linux-rdma@vger.kernel.org From: "Michael J. Ruhl" Date: Tue, 14 Nov 2017 11:44:17 -0500 Message-ID: <20171114164352.24557.86825.stgit@phlsvslse11.ph.intel.com> In-Reply-To: <20171114164006.24557.72093.stgit@phlsvslse11.ph.intel.com> References: <20171114164006.24557.72093.stgit@phlsvslse11.ph.intel.com> User-Agent: StGit/0.16 MIME-Version: 1.0 Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Michael J. Ruhl The event_wait() function uses the CLOCK_REALTIME time base for calculating expiration times (default time base for gettimeofday()). Using the CLOCK_REALTIME time base can introduce incorrect expiration timeout calculations if the REALTIME clock changes, making a timeout too long (possibly hours or days), or too short. Update time base usage to the CLOCK_MONOTONIC time base to avoid time change issues. Reviewed-by: Dennis Dalessandro Reviewed-by: Mike Marciniszyn Signed-off-by: Michael J. Ruhl --- ibacm/linux/osd.h | 20 +++++++++++--------- 1 files changed, 11 insertions(+), 9 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/ibacm/linux/osd.h b/ibacm/linux/osd.h index eabf5ed..1617176 100644 --- a/ibacm/linux/osd.h +++ b/ibacm/linux/osd.h @@ -88,20 +88,23 @@ typedef struct { volatile int val; } atomic_t; typedef struct { pthread_cond_t cond; pthread_mutex_t mutex; } event_t; static inline void event_init(event_t *e) { - pthread_cond_init(&e->cond, NULL); + pthread_condattr_t attr; + + pthread_condattr_init(&attr); + pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); + pthread_cond_init(&e->cond, &attr); pthread_mutex_init(&e->mutex, NULL); } #define event_signal(e) pthread_cond_signal(&(e)->cond) #define ONE_SEC_IN_NSEC 1000000000ULL static inline int event_wait(event_t *e, int timeout) { - struct timeval curtime; struct timespec wait; int ret; - gettimeofday(&curtime, NULL); - wait.tv_sec = curtime.tv_sec + ((unsigned) timeout) / 1000; - wait.tv_nsec = (curtime.tv_usec + (((unsigned) timeout) % 1000) * 1000) * 1000; + clock_gettime(CLOCK_MONOTONIC, &wait); + wait.tv_sec = wait.tv_sec + ((unsigned int) timeout) / 1000; + wait.tv_nsec = (wait.tv_nsec + (((unsigned int) timeout) % 1000) * 1000000); if (wait.tv_nsec > ONE_SEC_IN_NSEC) { wait.tv_sec++; wait.tv_nsec -= ONE_SEC_IN_NSEC; @@ -114,10 +117,9 @@ static inline int event_wait(event_t *e, int timeout) static inline uint64_t time_stamp_us(void) { - struct timeval curtime; - timerclear(&curtime); - gettimeofday(&curtime, NULL); - return (uint64_t) curtime.tv_sec * 1000000 + (uint64_t) curtime.tv_usec; + struct timespec t; + clock_gettime(CLOCK_MONOTONIC, &t); + return (t.tv_sec * ONE_SEC_IN_NSEC + t.tv_nsec) / 1000; } #define time_stamp_ms() (time_stamp_us() / (uint64_t) 1000)