diff mbox

[rdma-core,v4,4/4] ibacm: Use MONOTONIC time base to avoid timer expiration issues

Message ID 20171115193524.470.17499.stgit@phlsvslse11.ph.intel.com (mailing list archive)
State Accepted
Headers show

Commit Message

Michael J. Ruhl Nov. 15, 2017, 7:35 p.m. UTC
From: Michael J. Ruhl <michael.j.ruhl@intel.com>

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.

Use appropriate typing to avoid unnecessary typecasting.

Reviewed-by: Mike Marciniszyn <mike.marciniszyn@intel.com>
Signed-off-by: Michael J. Ruhl <michael.j.ruhl@intel.com>
---
 ibacm/linux/osd.h |   22 ++++++++++++----------
 1 files changed, 12 insertions(+), 10 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

Comments

Michael J. Ruhl Nov. 16, 2017, 12:09 p.m. UTC | #1
Hmm,

I don't know where my cover letter went for this patch set.

v4 cleans up the suggested type casting issues in this patch.

Thanks,
Mike

> -----Original Message-----

> From: linux-rdma-owner@vger.kernel.org [mailto:linux-rdma-

> owner@vger.kernel.org] On Behalf Of Michael J. Ruhl

> Sent: Wednesday, November 15, 2017 2:36 PM

> To: linux-rdma@vger.kernel.org

> Subject: [PATCH rdma-core v4 4/4] ibacm: Use MONOTONIC time base to avoid

> timer expiration issues

> 

> From: Michael J. Ruhl <michael.j.ruhl@intel.com>

> 

> 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.

> 

> Use appropriate typing to avoid unnecessary typecasting.

> 

> Reviewed-by: Mike Marciniszyn <mike.marciniszyn@intel.com>

> Signed-off-by: Michael J. Ruhl <michael.j.ruhl@intel.com>

> ---

>  ibacm/linux/osd.h |   22 ++++++++++++----------

>  1 files changed, 12 insertions(+), 10 deletions(-)

> 

> diff --git a/ibacm/linux/osd.h b/ibacm/linux/osd.h

> index eabf5ed..4ef7b55 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)

> +static inline int event_wait(event_t *e, unsigned 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 + timeout / 1000;

> +	wait.tv_nsec = wait.tv_nsec + (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)

> 

> --

> 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 mbox

Patch

diff --git a/ibacm/linux/osd.h b/ibacm/linux/osd.h
index eabf5ed..4ef7b55 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)
+static inline int event_wait(event_t *e, unsigned 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 + timeout / 1000;
+	wait.tv_nsec = wait.tv_nsec + (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)