patch 'eventdev/timer: fix overflow' has been queued to stable release 21.11.4

Kevin Traynor ktraynor at redhat.com
Thu Feb 23 16:05:36 CET 2023


Hi,

FYI, your patch has been queued to stable release 21.11.4

Note it hasn't been pushed to http://dpdk.org/browse/dpdk-stable yet.
It will be pushed if I get no objections before 02/28/23. So please
shout if anyone has objections.

Also note that after the patch there's a diff of the upstream commit vs the
patch applied to the branch. This will indicate if there was any rebasing
needed to apply to the stable branch. If there were code changes for rebasing
(ie: not only metadata diffs), please double check that the rebase was
correctly done.

Queued patches are on a temporary branch at:
https://github.com/kevintraynor/dpdk-stable

This queued commit can be viewed at:
https://github.com/kevintraynor/dpdk-stable/commit/02397094e9874acb172dcba4e4d095d5389b63bd

Thanks.

Kevin

---
>From 02397094e9874acb172dcba4e4d095d5389b63bd Mon Sep 17 00:00:00 2001
From: Erik Gabriel Carrillo <erik.g.carrillo at intel.com>
Date: Thu, 9 Feb 2023 09:13:49 -0600
Subject: [PATCH] eventdev/timer: fix overflow

[ upstream commit 13aba5f88d2e1bd8644a2d52fd6b1e4fc73fc3ca ]

The software timer adapter converts event timer timeout ticks to a
number of TSC cycles at which an rte_timer should expire. The
computation uses an integer multiplication that can result in overflow.

If necessary, reduce the timeout_nsecs value by the number of whole
seconds it contains to keep the value of the multiplier within a
range that will not result in overflow.  Add the saved value back later
to get the final result. Also, move the logic that checks the timeout
range into the function that performs the above computation.

Fixes: 6750b21bd6af ("eventdev: add default software timer adapter")

Signed-off-by: Erik Gabriel Carrillo <erik.g.carrillo at intel.com>
---
 lib/eventdev/rte_event_timer_adapter.c | 97 ++++++++++++++++----------
 1 file changed, 59 insertions(+), 38 deletions(-)

diff --git a/lib/eventdev/rte_event_timer_adapter.c b/lib/eventdev/rte_event_timer_adapter.c
index 9dad170b5a..5fad079be2 100644
--- a/lib/eventdev/rte_event_timer_adapter.c
+++ b/lib/eventdev/rte_event_timer_adapter.c
@@ -20,4 +20,5 @@
 #include <rte_service_component.h>
 #include <rte_cycles.h>
+#include <rte_reciprocal.h>
 
 #include "event_timer_adapter_pmd.h"
@@ -678,11 +679,49 @@ swtim_callback(struct rte_timer *tim)
 }
 
-static __rte_always_inline uint64_t
+static __rte_always_inline int
 get_timeout_cycles(struct rte_event_timer *evtim,
-		   const struct rte_event_timer_adapter *adapter)
+		   const struct rte_event_timer_adapter *adapter,
+		   uint64_t *timeout_cycles)
 {
-	struct swtim *sw = swtim_pmd_priv(adapter);
-	uint64_t timeout_ns = evtim->timeout_ticks * sw->timer_tick_ns;
-	return timeout_ns * rte_get_timer_hz() / NSECPERSEC;
+	static struct rte_reciprocal_u64 nsecpersec_inverse;
+	static uint64_t timer_hz;
+	uint64_t rem_cycles, secs_cycles = 0;
+	uint64_t secs, timeout_nsecs;
+	uint64_t nsecpersec;
+	struct swtim *sw;
+
+	sw = swtim_pmd_priv(adapter);
+	nsecpersec = (uint64_t)NSECPERSEC;
+
+	timeout_nsecs = evtim->timeout_ticks * sw->timer_tick_ns;
+	if (timeout_nsecs > sw->max_tmo_ns)
+		return -1;
+	if (timeout_nsecs < sw->timer_tick_ns)
+		return -2;
+
+	/* Set these values in the first invocation */
+	if (!timer_hz) {
+		timer_hz = rte_get_timer_hz();
+		nsecpersec_inverse = rte_reciprocal_value_u64(nsecpersec);
+	}
+
+	/* If timeout_nsecs > nsecpersec, decrease timeout_nsecs by the number
+	 * of whole seconds it contains and convert that value to a number
+	 * of cycles. This keeps timeout_nsecs in the interval [0..nsecpersec)
+	 * in order to avoid overflow when we later multiply by timer_hz.
+	 */
+	if (timeout_nsecs > nsecpersec) {
+		secs = rte_reciprocal_divide_u64(timeout_nsecs,
+						 &nsecpersec_inverse);
+		secs_cycles = secs * timer_hz;
+		timeout_nsecs -= secs * nsecpersec;
+	}
+
+	rem_cycles = rte_reciprocal_divide_u64(timeout_nsecs * timer_hz,
+					       &nsecpersec_inverse);
+
+	*timeout_cycles = secs_cycles + rem_cycles;
+
+	return 0;
 }
 
@@ -718,21 +757,4 @@ swtim_did_tick(struct swtim *sw)
 }
 
-/* Check that event timer timeout value is in range */
-static __rte_always_inline int
-check_timeout(struct rte_event_timer *evtim,
-	      const struct rte_event_timer_adapter *adapter)
-{
-	uint64_t tmo_nsec;
-	struct swtim *sw = swtim_pmd_priv(adapter);
-
-	tmo_nsec = evtim->timeout_ticks * sw->timer_tick_ns;
-	if (tmo_nsec > sw->max_tmo_ns)
-		return -1;
-	if (tmo_nsec < sw->timer_tick_ns)
-		return -2;
-
-	return 0;
-}
-
 /* Check that event timer event queue sched type matches destination event queue
  * sched type
@@ -1108,19 +1130,4 @@ __swtim_arm_burst(const struct rte_event_timer_adapter *adapter,
 		}
 
-		ret = check_timeout(evtims[i], adapter);
-		if (unlikely(ret == -1)) {
-			__atomic_store_n(&evtims[i]->state,
-					RTE_EVENT_TIMER_ERROR_TOOLATE,
-					__ATOMIC_RELAXED);
-			rte_errno = EINVAL;
-			break;
-		} else if (unlikely(ret == -2)) {
-			__atomic_store_n(&evtims[i]->state,
-					RTE_EVENT_TIMER_ERROR_TOOEARLY,
-					__ATOMIC_RELAXED);
-			rte_errno = EINVAL;
-			break;
-		}
-
 		if (unlikely(check_destination_event_queue(evtims[i],
 							   adapter) < 0)) {
@@ -1138,5 +1145,19 @@ __swtim_arm_burst(const struct rte_event_timer_adapter *adapter,
 		evtims[i]->impl_opaque[1] = (uintptr_t)adapter;
 
-		cycles = get_timeout_cycles(evtims[i], adapter);
+		ret = get_timeout_cycles(evtims[i], adapter, &cycles);
+		if (unlikely(ret == -1)) {
+			__atomic_store_n(&evtims[i]->state,
+					RTE_EVENT_TIMER_ERROR_TOOLATE,
+					__ATOMIC_RELAXED);
+			rte_errno = EINVAL;
+			break;
+		} else if (unlikely(ret == -2)) {
+			__atomic_store_n(&evtims[i]->state,
+					RTE_EVENT_TIMER_ERROR_TOOEARLY,
+					__ATOMIC_RELAXED);
+			rte_errno = EINVAL;
+			break;
+		}
+
 		ret = rte_timer_alt_reset(sw->timer_data_id, tim, cycles,
 					  SINGLE, lcore_id, NULL, evtims[i]);
-- 
2.39.1

---
  Diff of the applied patch vs upstream commit (please double-check if non-empty:
---
--- -	2023-02-23 14:46:24.904932935 +0000
+++ 0045-eventdev-timer-fix-overflow.patch	2023-02-23 14:46:23.766235952 +0000
@@ -1 +1 @@
-From 13aba5f88d2e1bd8644a2d52fd6b1e4fc73fc3ca Mon Sep 17 00:00:00 2001
+From 02397094e9874acb172dcba4e4d095d5389b63bd Mon Sep 17 00:00:00 2001
@@ -5,0 +6,2 @@
+[ upstream commit 13aba5f88d2e1bd8644a2d52fd6b1e4fc73fc3ca ]
+
@@ -17 +18,0 @@
-Cc: stable at dpdk.org
@@ -25 +26 @@
-index 7f4f347369..23eb1d4a7d 100644
+index 9dad170b5a..5fad079be2 100644
@@ -28 +29 @@
-@@ -19,4 +19,5 @@
+@@ -20,4 +20,5 @@
@@ -30 +31 @@
- #include <rte_telemetry.h>
+ #include <rte_cycles.h>
@@ -34 +35 @@
-@@ -735,11 +736,49 @@ swtim_callback(struct rte_timer *tim)
+@@ -678,11 +679,49 @@ swtim_callback(struct rte_timer *tim)
@@ -89 +90 @@
-@@ -775,21 +814,4 @@ swtim_did_tick(struct swtim *sw)
+@@ -718,21 +757,4 @@ swtim_did_tick(struct swtim *sw)
@@ -111 +112 @@
-@@ -1211,19 +1233,4 @@ __swtim_arm_burst(const struct rte_event_timer_adapter *adapter,
+@@ -1108,19 +1130,4 @@ __swtim_arm_burst(const struct rte_event_timer_adapter *adapter,
@@ -131 +132 @@
-@@ -1241,5 +1248,19 @@ __swtim_arm_burst(const struct rte_event_timer_adapter *adapter,
+@@ -1138,5 +1145,19 @@ __swtim_arm_burst(const struct rte_event_timer_adapter *adapter,
@@ -151 +152 @@
- 					  type, lcore_id, NULL, evtims[i]);
+ 					  SINGLE, lcore_id, NULL, evtims[i]);



More information about the stable mailing list