diff mbox series

[v2] eal: fix use wrong time API

Message ID 1619662241-61955-1-git-send-email-humin29@huawei.com (mailing list archive)
State Superseded
Delegated to: Thomas Monjalon
Headers show
Series [v2] eal: fix use wrong time API | expand

Checks

Context Check Description
ci/iol-mellanox-Performance success Performance Testing PASS
ci/intel-Testing success Testing PASS
ci/Intel-compilation success Compilation OK
ci/github-robot success github build: passed
ci/iol-testing success Testing PASS
ci/iol-abi-testing success Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/checkpatch success coding style OK

Commit Message

Min Hu (Connor) April 29, 2021, 2:10 a.m. UTC
Currently, the mp uses gettimeofday() API to get the time, and used as
timeout parameter.

But the time which gets from gettimeofday() API isn't monotonically
increasing. The process may fail if the system time is changed.

This fixes it by using clock_gettime() API with monotonic attribution.

Fixes: 783b6e54971d ("eal: add synchronous multi-process communication")
Fixes: f05e26051c15 ("eal: add IPC asynchronous request")
Cc: stable@dpdk.org

Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
Signed-off-by: Min Hu (Connor) <humin29@huawei.com>
---
v2:
* get rid of the CLOCK_TYPE_ID definition and use CLOCK_MONOTONIC
instead.
---
 lib/eal/common/eal_common_proc.c | 39 +++++++++++++--------------------------
 1 file changed, 13 insertions(+), 26 deletions(-)

Comments

Morten Brørup April 29, 2021, 7:12 a.m. UTC | #1
> From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Min Hu (Connor)
> Sent: Thursday, April 29, 2021 4:11 AM
> To: dev@dpdk.org
> 
> Currently, the mp uses gettimeofday() API to get the time, and used as
> timeout parameter.
> 
> But the time which gets from gettimeofday() API isn't monotonically
> increasing. The process may fail if the system time is changed.
> 
> This fixes it by using clock_gettime() API with monotonic attribution.
> 
> Fixes: 783b6e54971d ("eal: add synchronous multi-process
> communication")
> Fixes: f05e26051c15 ("eal: add IPC asynchronous request")
> Cc: stable@dpdk.org
> 
> Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
> Signed-off-by: Min Hu (Connor) <humin29@huawei.com>
> ---
> v2:
> * get rid of the CLOCK_TYPE_ID definition and use CLOCK_MONOTONIC
> instead.
> ---
>  lib/eal/common/eal_common_proc.c | 39 +++++++++++++-------------------
> -------
>  1 file changed, 13 insertions(+), 26 deletions(-)
> 
> diff --git a/lib/eal/common/eal_common_proc.c
> b/lib/eal/common/eal_common_proc.c
> index 6d1af3c..6af8ba2 100644
> --- a/lib/eal/common/eal_common_proc.c
> +++ b/lib/eal/common/eal_common_proc.c
> @@ -490,14 +490,8 @@ async_reply_handle_thread_unsafe(void *arg)
>  	struct pending_request *req = (struct pending_request *)arg;
>  	enum async_action action;
>  	struct timespec ts_now;
> -	struct timeval now;
> 
> -	if (gettimeofday(&now, NULL) < 0) {
> -		RTE_LOG(ERR, EAL, "Cannot get current time\n");
> -		goto no_trigger;
> -	}
> -	ts_now.tv_nsec = now.tv_usec * 1000;
> -	ts_now.tv_sec = now.tv_sec;
> +	clock_gettime(CLOCK_MONOTONIC, &ts_now);
> 
>  	action = process_async_request(req, &ts_now);
> 
> @@ -896,6 +890,7 @@ mp_request_sync(const char *dst, struct rte_mp_msg
> *req,
>  	       struct rte_mp_reply *reply, const struct timespec *ts)
>  {
>  	int ret;
> +	pthread_condattr_t attr;
>  	struct rte_mp_msg msg, *tmp;
>  	struct pending_request pending_req, *exist;
> 
> @@ -904,7 +899,9 @@ mp_request_sync(const char *dst, struct rte_mp_msg
> *req,
>  	strlcpy(pending_req.dst, dst, sizeof(pending_req.dst));
>  	pending_req.request = req;
>  	pending_req.reply = &msg;
> -	pthread_cond_init(&pending_req.sync.cond, NULL);
> +	pthread_condattr_init(&attr);
> +	pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
> +	pthread_cond_init(&pending_req.sync.cond, &attr);
> 
>  	exist = find_pending_request(dst, req->name);
>  	if (exist) {
> @@ -967,8 +964,7 @@ rte_mp_request_sync(struct rte_mp_msg *req, struct
> rte_mp_reply *reply,
>  	int dir_fd, ret = -1;
>  	DIR *mp_dir;
>  	struct dirent *ent;
> -	struct timeval now;
> -	struct timespec end;
> +	struct timespec now, end;
>  	const struct internal_config *internal_conf =
>  		eal_get_internal_configuration();
> 
> @@ -987,15 +983,10 @@ rte_mp_request_sync(struct rte_mp_msg *req,
> struct rte_mp_reply *reply,
>  		return -1;
>  	}
> 
> -	if (gettimeofday(&now, NULL) < 0) {
> -		RTE_LOG(ERR, EAL, "Failed to get current time\n");
> -		rte_errno = errno;
> -		goto end;
> -	}
> -
> -	end.tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
> +	clock_gettime(CLOCK_MONOTONIC, &now);
> +	end.tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
>  	end.tv_sec = now.tv_sec + ts->tv_sec +
> -			(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
> +			(now.tv_nsec + ts->tv_nsec) / 1000000000;
> 
>  	/* for secondary process, send request to the primary process
> only */
>  	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
> @@ -1069,7 +1060,7 @@ rte_mp_request_async(struct rte_mp_msg *req,
> const struct timespec *ts,
>  	int dir_fd, ret = 0;
>  	DIR *mp_dir;
>  	struct dirent *ent;
> -	struct timeval now;
> +	struct timespec now;
>  	struct timespec *end;
>  	bool dummy_used = false;
>  	const struct internal_config *internal_conf =
> @@ -1086,11 +1077,6 @@ rte_mp_request_async(struct rte_mp_msg *req,
> const struct timespec *ts,
>  		return -1;
>  	}
> 
> -	if (gettimeofday(&now, NULL) < 0) {
> -		RTE_LOG(ERR, EAL, "Failed to get current time\n");
> -		rte_errno = errno;
> -		return -1;
> -	}
>  	copy = calloc(1, sizeof(*copy));
>  	dummy = calloc(1, sizeof(*dummy));
>  	param = calloc(1, sizeof(*param));
> @@ -1108,9 +1094,10 @@ rte_mp_request_async(struct rte_mp_msg *req,
> const struct timespec *ts,
>  	end = &param->end;
>  	reply = &param->user_reply;
> 
> -	end->tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
> +	clock_gettime(CLOCK_MONOTONIC, &now);
> +	end->tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
>  	end->tv_sec = now.tv_sec + ts->tv_sec +
> -			(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
> +			(now.tv_nsec + ts->tv_nsec) / 1000000000;
>  	reply->nb_sent = 0;
>  	reply->nb_received = 0;
>  	reply->msgs = NULL;
> --
> 2.7.4
> 

Acked-by: Morten Brørup <mb@smartsharesystems.com>
Thomas Monjalon May 4, 2021, 4:49 p.m. UTC | #2
29/04/2021 04:10, Min Hu (Connor):
> Currently, the mp uses gettimeofday() API to get the time, and used as
> timeout parameter.
> 
> But the time which gets from gettimeofday() API isn't monotonically
> increasing. The process may fail if the system time is changed.
> 
> This fixes it by using clock_gettime() API with monotonic attribution.
> 
> Fixes: 783b6e54971d ("eal: add synchronous multi-process communication")
> Fixes: f05e26051c15 ("eal: add IPC asynchronous request")
> Cc: stable@dpdk.org
> 
> Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
> Signed-off-by: Min Hu (Connor) <humin29@huawei.com>
> ---
[...]
> --- a/lib/eal/common/eal_common_proc.c
> +++ b/lib/eal/common/eal_common_proc.c
> -	if (gettimeofday(&now, NULL) < 0) {
> -		RTE_LOG(ERR, EAL, "Cannot get current time\n");
> -		goto no_trigger;
> -	}
> -	ts_now.tv_nsec = now.tv_usec * 1000;
> -	ts_now.tv_sec = now.tv_sec;
> +	clock_gettime(CLOCK_MONOTONIC, &ts_now);

Why not testing the return value?

I think this change would not be appropriate after -rc1.
If you agree, I will postpone to DPDK 21.08.
Morten Brørup May 4, 2021, 7:12 p.m. UTC | #3
> From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Thomas Monjalon
> Sent: Tuesday, May 4, 2021 6:50 PM
> 
> 29/04/2021 04:10, Min Hu (Connor):
> > Currently, the mp uses gettimeofday() API to get the time, and used
> as
> > timeout parameter.
> >
> > But the time which gets from gettimeofday() API isn't monotonically
> > increasing. The process may fail if the system time is changed.
> >
> > This fixes it by using clock_gettime() API with monotonic
> attribution.
> >
> > Fixes: 783b6e54971d ("eal: add synchronous multi-process
> communication")
> > Fixes: f05e26051c15 ("eal: add IPC asynchronous request")
> > Cc: stable@dpdk.org
> >
> > Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
> > Signed-off-by: Min Hu (Connor) <humin29@huawei.com>
> > ---
> [...]
> > --- a/lib/eal/common/eal_common_proc.c
> > +++ b/lib/eal/common/eal_common_proc.c
> > -	if (gettimeofday(&now, NULL) < 0) {
> > -		RTE_LOG(ERR, EAL, "Cannot get current time\n");
> > -		goto no_trigger;
> > -	}
> > -	ts_now.tv_nsec = now.tv_usec * 1000;
> > -	ts_now.tv_sec = now.tv_sec;
> > +	clock_gettime(CLOCK_MONOTONIC, &ts_now);
> 
> Why not testing the return value?

Because it is guaranteed not to fail. Ref:
https://linux.die.net/man/3/clock_gettime
https://www.freebsd.org/cgi/man.cgi?query=clock_gettime

> 
> I think this change would not be appropriate after -rc1.
> If you agree, I will postpone to DPDK 21.08.

It does fix a serious bug, where IPC timeouts can incorrectly happen. And this is not a theoretical bug; I have seen errors happen due to using the wrong clock source in other projects.

However, I have no clue if these IPC library functions are important or not. So I have no qualified opinion about postponing the change.
Thomas Monjalon May 5, 2021, 6:14 a.m. UTC | #4
04/05/2021 21:12, Morten Brørup:
> > From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Thomas Monjalon
> > Sent: Tuesday, May 4, 2021 6:50 PM
> > 
> > 29/04/2021 04:10, Min Hu (Connor):
> > > Currently, the mp uses gettimeofday() API to get the time, and used
> > as
> > > timeout parameter.
> > >
> > > But the time which gets from gettimeofday() API isn't monotonically
> > > increasing. The process may fail if the system time is changed.
> > >
> > > This fixes it by using clock_gettime() API with monotonic
> > attribution.
> > >
> > > Fixes: 783b6e54971d ("eal: add synchronous multi-process
> > communication")
> > > Fixes: f05e26051c15 ("eal: add IPC asynchronous request")
> > > Cc: stable@dpdk.org
> > >
> > > Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
> > > Signed-off-by: Min Hu (Connor) <humin29@huawei.com>
> > > ---
> > [...]
> > > --- a/lib/eal/common/eal_common_proc.c
> > > +++ b/lib/eal/common/eal_common_proc.c
> > > -	if (gettimeofday(&now, NULL) < 0) {
> > > -		RTE_LOG(ERR, EAL, "Cannot get current time\n");
> > > -		goto no_trigger;
> > > -	}
> > > -	ts_now.tv_nsec = now.tv_usec * 1000;
> > > -	ts_now.tv_sec = now.tv_sec;
> > > +	clock_gettime(CLOCK_MONOTONIC, &ts_now);
> > 
> > Why not testing the return value?
> 
> Because it is guaranteed not to fail. Ref:
> https://linux.die.net/man/3/clock_gettime
> https://www.freebsd.org/cgi/man.cgi?query=clock_gettime

I see "return 0 for success, or -1 for failure".
Where is it said it cannot fail?

> > I think this change would not be appropriate after -rc1.
> > If you agree, I will postpone to DPDK 21.08.
> 
> It does fix a serious bug, where IPC timeouts can incorrectly happen. And this is not a theoretical bug; I have seen errors happen due to using the wrong clock source in other projects.
> 
> However, I have no clue if these IPC library functions are important or not. So I have no qualified opinion about postponing the change.

I think nobody hit such bug with DPDK IPC.
Morten Brørup May 5, 2021, 6:26 a.m. UTC | #5
> From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Thomas Monjalon
> Sent: Wednesday, May 5, 2021 8:14 AM
> 
> 04/05/2021 21:12, Morten Brørup:
> > > From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Thomas
> Monjalon
> > > Sent: Tuesday, May 4, 2021 6:50 PM
> > >
> > > 29/04/2021 04:10, Min Hu (Connor):
> > > > Currently, the mp uses gettimeofday() API to get the time, and
> used
> > > as
> > > > timeout parameter.
> > > >
> > > > But the time which gets from gettimeofday() API isn't
> monotonically
> > > > increasing. The process may fail if the system time is changed.
> > > >
> > > > This fixes it by using clock_gettime() API with monotonic
> > > attribution.
> > > >
> > > > Fixes: 783b6e54971d ("eal: add synchronous multi-process
> > > communication")
> > > > Fixes: f05e26051c15 ("eal: add IPC asynchronous request")
> > > > Cc: stable@dpdk.org
> > > >
> > > > Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
> > > > Signed-off-by: Min Hu (Connor) <humin29@huawei.com>
> > > > ---
> > > [...]
> > > > --- a/lib/eal/common/eal_common_proc.c
> > > > +++ b/lib/eal/common/eal_common_proc.c
> > > > -	if (gettimeofday(&now, NULL) < 0) {
> > > > -		RTE_LOG(ERR, EAL, "Cannot get current time\n");
> > > > -		goto no_trigger;
> > > > -	}
> > > > -	ts_now.tv_nsec = now.tv_usec * 1000;
> > > > -	ts_now.tv_sec = now.tv_sec;
> > > > +	clock_gettime(CLOCK_MONOTONIC, &ts_now);
> > >
> > > Why not testing the return value?
> >
> > Because it is guaranteed not to fail. Ref:
> > https://linux.die.net/man/3/clock_gettime
> > https://www.freebsd.org/cgi/man.cgi?query=clock_gettime
> 
> I see "return 0 for success, or -1 for failure".
> Where is it said it cannot fail?

I'm sorry about being unclear. Referring to the "Errors" chapter in the function's man page, this function call is guaranteed not to fail with these parameters. So there is no need to check the return value.

> 
> > > I think this change would not be appropriate after -rc1.
> > > If you agree, I will postpone to DPDK 21.08.
> >
> > It does fix a serious bug, where IPC timeouts can incorrectly happen.
> And this is not a theoretical bug; I have seen errors happen due to
> using the wrong clock source in other projects.
> >
> > However, I have no clue if these IPC library functions are important
> or not. So I have no qualified opinion about postponing the change.
> 
> I think nobody hit such bug with DPDK IPC.
> 

Then I don't object to postponing.
Thomas Monjalon May 5, 2021, 6:51 a.m. UTC | #6
05/05/2021 08:26, Morten Brørup:
> > From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Thomas Monjalon
> > Sent: Wednesday, May 5, 2021 8:14 AM
> > 
> > 04/05/2021 21:12, Morten Brørup:
> > > > From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Thomas
> > Monjalon
> > > > Sent: Tuesday, May 4, 2021 6:50 PM
> > > >
> > > > 29/04/2021 04:10, Min Hu (Connor):
> > > > > Currently, the mp uses gettimeofday() API to get the time, and
> > used
> > > > as
> > > > > timeout parameter.
> > > > >
> > > > > But the time which gets from gettimeofday() API isn't
> > monotonically
> > > > > increasing. The process may fail if the system time is changed.
> > > > >
> > > > > This fixes it by using clock_gettime() API with monotonic
> > > > attribution.
> > > > >
> > > > > Fixes: 783b6e54971d ("eal: add synchronous multi-process
> > > > communication")
> > > > > Fixes: f05e26051c15 ("eal: add IPC asynchronous request")
> > > > > Cc: stable@dpdk.org
> > > > >
> > > > > Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
> > > > > Signed-off-by: Min Hu (Connor) <humin29@huawei.com>
> > > > > ---
> > > > [...]
> > > > > --- a/lib/eal/common/eal_common_proc.c
> > > > > +++ b/lib/eal/common/eal_common_proc.c
> > > > > -	if (gettimeofday(&now, NULL) < 0) {
> > > > > -		RTE_LOG(ERR, EAL, "Cannot get current time\n");
> > > > > -		goto no_trigger;
> > > > > -	}
> > > > > -	ts_now.tv_nsec = now.tv_usec * 1000;
> > > > > -	ts_now.tv_sec = now.tv_sec;
> > > > > +	clock_gettime(CLOCK_MONOTONIC, &ts_now);
> > > >
> > > > Why not testing the return value?
> > >
> > > Because it is guaranteed not to fail. Ref:
> > > https://linux.die.net/man/3/clock_gettime
> > > https://www.freebsd.org/cgi/man.cgi?query=clock_gettime
> > 
> > I see "return 0 for success, or -1 for failure".
> > Where is it said it cannot fail?
> 
> I'm sorry about being unclear. Referring to the "Errors" chapter in the function's man page, this function call is guaranteed not to fail with these parameters. So there is no need to check the return value.

I don't agree.
Especially for this error:
"The clk_id specified is not supported on this system."
How can you be sure it is always supported for any system
we try to run this code now and in future experiments?
Morten Brørup May 5, 2021, 7:08 a.m. UTC | #7
> From: Thomas Monjalon [mailto:thomas@monjalon.net]
> Sent: Wednesday, May 5, 2021 8:51 AM
> 
> 05/05/2021 08:26, Morten Brørup:
> > > From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Thomas
> Monjalon
> > > Sent: Wednesday, May 5, 2021 8:14 AM
> > >
> > > 04/05/2021 21:12, Morten Brørup:
> > > > > From: dev [mailto:dev-bounces@dpdk.org] On Behalf Of Thomas
> > > Monjalon
> > > > > Sent: Tuesday, May 4, 2021 6:50 PM
> > > > >
> > > > > 29/04/2021 04:10, Min Hu (Connor):
> > > > > > Currently, the mp uses gettimeofday() API to get the time,
> and
> > > used
> > > > > as
> > > > > > timeout parameter.
> > > > > >
> > > > > > But the time which gets from gettimeofday() API isn't
> > > monotonically
> > > > > > increasing. The process may fail if the system time is
> changed.
> > > > > >
> > > > > > This fixes it by using clock_gettime() API with monotonic
> > > > > attribution.
> > > > > >
> > > > > > Fixes: 783b6e54971d ("eal: add synchronous multi-process
> > > > > communication")
> > > > > > Fixes: f05e26051c15 ("eal: add IPC asynchronous request")
> > > > > > Cc: stable@dpdk.org
> > > > > >
> > > > > > Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
> > > > > > Signed-off-by: Min Hu (Connor) <humin29@huawei.com>
> > > > > > ---
> > > > > [...]
> > > > > > --- a/lib/eal/common/eal_common_proc.c	
> > > > > > +++ b/lib/eal/common/eal_common_proc.c
> > > > > > -	if (gettimeofday(&now, NULL) < 0) {
> > > > > > -		RTE_LOG(ERR, EAL, "Cannot get current time\n");
> > > > > > -		goto no_trigger;
> > > > > > -	}
> > > > > > -	ts_now.tv_nsec = now.tv_usec * 1000;
> > > > > > -	ts_now.tv_sec = now.tv_sec;
> > > > > > +	clock_gettime(CLOCK_MONOTONIC, &ts_now);
> > > > >
> > > > > Why not testing the return value?
> > > >
> > > > Because it is guaranteed not to fail. Ref:
> > > > https://linux.die.net/man/3/clock_gettime
> > > > https://www.freebsd.org/cgi/man.cgi?query=clock_gettime
> > >
> > > I see "return 0 for success, or -1 for failure".
> > > Where is it said it cannot fail?
> >
> > I'm sorry about being unclear. Referring to the "Errors" chapter in
> the function's man page, this function call is guaranteed not to fail
> with these parameters. So there is no need to check the return value.
> 
> I don't agree.
> Especially for this error:
> "The clk_id specified is not supported on this system."
> How can you be sure it is always supported for any system
> we try to run this code now and in future experiments?
> 

I referred to both Linux and BSD man pages because they say that it is defined there. This should cover current supported systems, excl. Windows, which doesn't have gettimeofday() or clock_gettime() anyway.

However, for future operating systems that might be supported by DPDK, you are correct that CLOCK_MONOTONIC is optional.
Ref: https://pubs.opengroup.org/onlinepubs/009695399/basedefs/time.h.html

This can be fixed by adding:
#if !defined(CLOCK_MONOTONIC)
#error CLOCK_MONOTONIC is not defined.
#endif

If we only check the return value at runtime, the function would fail every time on a system without CLOCK_MONOTONIC, and eal_common_proc.c would need to be fixed anyway. It's better to catch at compile time.
diff mbox series

Patch

diff --git a/lib/eal/common/eal_common_proc.c b/lib/eal/common/eal_common_proc.c
index 6d1af3c..6af8ba2 100644
--- a/lib/eal/common/eal_common_proc.c
+++ b/lib/eal/common/eal_common_proc.c
@@ -490,14 +490,8 @@  async_reply_handle_thread_unsafe(void *arg)
 	struct pending_request *req = (struct pending_request *)arg;
 	enum async_action action;
 	struct timespec ts_now;
-	struct timeval now;
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Cannot get current time\n");
-		goto no_trigger;
-	}
-	ts_now.tv_nsec = now.tv_usec * 1000;
-	ts_now.tv_sec = now.tv_sec;
+	clock_gettime(CLOCK_MONOTONIC, &ts_now);
 
 	action = process_async_request(req, &ts_now);
 
@@ -896,6 +890,7 @@  mp_request_sync(const char *dst, struct rte_mp_msg *req,
 	       struct rte_mp_reply *reply, const struct timespec *ts)
 {
 	int ret;
+	pthread_condattr_t attr;
 	struct rte_mp_msg msg, *tmp;
 	struct pending_request pending_req, *exist;
 
@@ -904,7 +899,9 @@  mp_request_sync(const char *dst, struct rte_mp_msg *req,
 	strlcpy(pending_req.dst, dst, sizeof(pending_req.dst));
 	pending_req.request = req;
 	pending_req.reply = &msg;
-	pthread_cond_init(&pending_req.sync.cond, NULL);
+	pthread_condattr_init(&attr);
+	pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
+	pthread_cond_init(&pending_req.sync.cond, &attr);
 
 	exist = find_pending_request(dst, req->name);
 	if (exist) {
@@ -967,8 +964,7 @@  rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
 	int dir_fd, ret = -1;
 	DIR *mp_dir;
 	struct dirent *ent;
-	struct timeval now;
-	struct timespec end;
+	struct timespec now, end;
 	const struct internal_config *internal_conf =
 		eal_get_internal_configuration();
 
@@ -987,15 +983,10 @@  rte_mp_request_sync(struct rte_mp_msg *req, struct rte_mp_reply *reply,
 		return -1;
 	}
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Failed to get current time\n");
-		rte_errno = errno;
-		goto end;
-	}
-
-	end.tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
+	clock_gettime(CLOCK_MONOTONIC, &now);
+	end.tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
 	end.tv_sec = now.tv_sec + ts->tv_sec +
-			(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
+			(now.tv_nsec + ts->tv_nsec) / 1000000000;
 
 	/* for secondary process, send request to the primary process only */
 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
@@ -1069,7 +1060,7 @@  rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
 	int dir_fd, ret = 0;
 	DIR *mp_dir;
 	struct dirent *ent;
-	struct timeval now;
+	struct timespec now;
 	struct timespec *end;
 	bool dummy_used = false;
 	const struct internal_config *internal_conf =
@@ -1086,11 +1077,6 @@  rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
 		return -1;
 	}
 
-	if (gettimeofday(&now, NULL) < 0) {
-		RTE_LOG(ERR, EAL, "Failed to get current time\n");
-		rte_errno = errno;
-		return -1;
-	}
 	copy = calloc(1, sizeof(*copy));
 	dummy = calloc(1, sizeof(*dummy));
 	param = calloc(1, sizeof(*param));
@@ -1108,9 +1094,10 @@  rte_mp_request_async(struct rte_mp_msg *req, const struct timespec *ts,
 	end = &param->end;
 	reply = &param->user_reply;
 
-	end->tv_nsec = (now.tv_usec * 1000 + ts->tv_nsec) % 1000000000;
+	clock_gettime(CLOCK_MONOTONIC, &now);
+	end->tv_nsec = (now.tv_nsec + ts->tv_nsec) % 1000000000;
 	end->tv_sec = now.tv_sec + ts->tv_sec +
-			(now.tv_usec * 1000 + ts->tv_nsec) / 1000000000;
+			(now.tv_nsec + ts->tv_nsec) / 1000000000;
 	reply->nb_sent = 0;
 	reply->nb_received = 0;
 	reply->msgs = NULL;