From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 56BF941D4F for ; Thu, 23 Feb 2023 10:38:48 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 4E9A343164; Thu, 23 Feb 2023 10:38:48 +0100 (CET) Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) by mails.dpdk.org (Postfix) with ESMTP id 2A538431AC for ; Thu, 23 Feb 2023 10:38:47 +0100 (CET) Received: by mail-wm1-f42.google.com with SMTP id j3so6260430wms.2 for ; Thu, 23 Feb 2023 01:38:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=txngOH9Jn1Xae7dKb3rg5uZ/6L478HdshlSFGtspoXU=; b=kdIQzKdgj98Gww8Y9bmzgkZ+ONiceG/sFdnqaNJvXqvgx8YnLNrIt17JYxzKxSZYAs LDLn+Ra+IdDIvIdRnjeS3/PytgpWQBEoCqYpgicpl7ph571Utta8AS9KPtMC6IgflbCh SJ3gd53qNfGwVNiBU++IUfZfQsYQckIvUS1Ush2P6dBZsvaV3/rsGlh46vQ8slCymGBB otU3wzNSnfo42yaPmC65aL/xwyEAR310OB3IhddlnL9AejH3YI+jGYIiCiheJTvkm8/V oEQhCt68tZ3sEGIz6EE7yfRDtbM8pBJz9E+Kc+1qc6YH1s1tazMeP+T4j1Rka2eAWytg N16Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=txngOH9Jn1Xae7dKb3rg5uZ/6L478HdshlSFGtspoXU=; b=4d4VUzkGMQiZVF3XGaZU0A1e13wuUJFcJOw2NxaqUY86J3ZT/XmoilkRqn6QxFxb18 R9YgO8WzTX8HWG1CKeyUb7tqhb7oqjccwzwIaKGZXC/Gq+HknGepiN5R/SyDMfDr6ryp eVAiQBwreUiZM77xK1S0PhY+PLbaoO/uDtwutOlOOqZghOHJ6DNOVopNw9FEkfChD1hY /Ptxm4HV/p3k7qbTx1ZNDoIi7I+tomwniZCiB41LyCjlUmkMVy6GUi+tk5utQkrVw/P4 6xNF7BHSKbOf1RT9HY9RMikopeE92uT7uw47HVdPT1AqkUTE4qo+5ph3Agrkv/9+hrH5 f+mw== X-Gm-Message-State: AO0yUKXjCKlQLnknGFyxdXDrdCKlGS351Ef3viAyoRWGFoVgZY2QeUxa O3uBBqw7maNd+UnSLynKRNM= X-Google-Smtp-Source: AK7set9qjPj04mwJcqs/EvgNAd7qX59LHdsmjzWLaUq5AyVnkiImu16D7F+oEeXiOFC+ElPgkFCVpw== X-Received: by 2002:a05:600c:34d1:b0:3df:9858:c030 with SMTP id d17-20020a05600c34d100b003df9858c030mr9731367wmq.5.1677145126780; Thu, 23 Feb 2023 01:38:46 -0800 (PST) Received: from localhost ([2a01:4b00:d307:1000:f1d3:eb5e:11f4:a7d9]) by smtp.gmail.com with ESMTPSA id w18-20020a05600c475200b003e220998b6bsm12287883wmo.17.2023.02.23.01.38.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Feb 2023 01:38:46 -0800 (PST) From: luca.boccassi@gmail.com To: Erik Gabriel Carrillo Cc: dpdk stable Subject: patch 'eventdev/timer: fix overflow' has been queued to stable release 20.11.8 Date: Thu, 23 Feb 2023 09:36:34 +0000 Message-Id: <20230223093715.3926893-30-luca.boccassi@gmail.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230223093715.3926893-1-luca.boccassi@gmail.com> References: <20230223093715.3926893-1-luca.boccassi@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: stable@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: patches for DPDK stable branches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: stable-bounces@dpdk.org Hi, FYI, your patch has been queued to stable release 20.11.8 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/25/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/bluca/dpdk-stable This queued commit can be viewed at: https://github.com/bluca/dpdk-stable/commit/cbc71a34d04d53cbd915cd5fd3bd5d845f96744d Thanks. Luca Boccassi --- >From cbc71a34d04d53cbd915cd5fd3bd5d845f96744d Mon Sep 17 00:00:00 2001 From: Erik Gabriel Carrillo 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 --- lib/librte_eventdev/rte_event_timer_adapter.c | 97 +++++++++++-------- 1 file changed, 59 insertions(+), 38 deletions(-) diff --git a/lib/librte_eventdev/rte_event_timer_adapter.c b/lib/librte_eventdev/rte_event_timer_adapter.c index 64b0f7ed0f..fac32e2b2d 100644 --- a/lib/librte_eventdev/rte_event_timer_adapter.c +++ b/lib/librte_eventdev/rte_event_timer_adapter.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "rte_eventdev.h" #include "rte_eventdev_pmd.h" @@ -645,13 +646,51 @@ 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; } /* This function returns true if one or more (adapter) ticks have occurred since @@ -685,23 +724,6 @@ swtim_did_tick(struct swtim *sw) return false; } -/* 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 */ @@ -1072,21 +1094,6 @@ __swtim_arm_burst(const struct rte_event_timer_adapter *adapter, break; } - 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)) { __atomic_store_n(&evtims[i]->state, @@ -1102,7 +1109,21 @@ __swtim_arm_burst(const struct rte_event_timer_adapter *adapter, evtims[i]->impl_opaque[0] = (uintptr_t)tim; 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]); if (ret < 0) { -- 2.39.1 --- Diff of the applied patch vs upstream commit (please double-check if non-empty: --- --- - 2023-02-23 09:36:29.459059393 +0000 +++ 0030-eventdev-timer-fix-overflow.patch 2023-02-23 09:36:28.226169940 +0000 @@ -1 +1 @@ -From 13aba5f88d2e1bd8644a2d52fd6b1e4fc73fc3ca Mon Sep 17 00:00:00 2001 +From cbc71a34d04d53cbd915cd5fd3bd5d845f96744d Mon Sep 17 00:00:00 2001 @@ -5,0 +6,2 @@ +[ upstream commit 13aba5f88d2e1bd8644a2d52fd6b1e4fc73fc3ca ] + @@ -17 +18,0 @@ -Cc: stable@dpdk.org @@ -21 +22 @@ - lib/eventdev/rte_event_timer_adapter.c | 97 ++++++++++++++++---------- + lib/librte_eventdev/rte_event_timer_adapter.c | 97 +++++++++++-------- @@ -24,5 +25,5 @@ -diff --git a/lib/eventdev/rte_event_timer_adapter.c b/lib/eventdev/rte_event_timer_adapter.c -index 7f4f347369..23eb1d4a7d 100644 ---- a/lib/eventdev/rte_event_timer_adapter.c -+++ b/lib/eventdev/rte_event_timer_adapter.c -@@ -18,6 +18,7 @@ +diff --git a/lib/librte_eventdev/rte_event_timer_adapter.c b/lib/librte_eventdev/rte_event_timer_adapter.c +index 64b0f7ed0f..fac32e2b2d 100644 +--- a/lib/librte_eventdev/rte_event_timer_adapter.c ++++ b/lib/librte_eventdev/rte_event_timer_adapter.c +@@ -19,6 +19,7 @@ @@ -31 +32 @@ - #include + #include @@ -34,3 +35,3 @@ - #include "event_timer_adapter_pmd.h" - #include "eventdev_pmd.h" -@@ -734,13 +735,51 @@ swtim_callback(struct rte_timer *tim) + #include "rte_eventdev.h" + #include "rte_eventdev_pmd.h" +@@ -645,13 +646,51 @@ swtim_callback(struct rte_timer *tim) @@ -93 +94 @@ -@@ -774,23 +813,6 @@ swtim_did_tick(struct swtim *sw) +@@ -685,23 +724,6 @@ swtim_did_tick(struct swtim *sw) @@ -117 +118 @@ -@@ -1210,21 +1232,6 @@ __swtim_arm_burst(const struct rte_event_timer_adapter *adapter, +@@ -1072,21 +1094,6 @@ __swtim_arm_burst(const struct rte_event_timer_adapter *adapter, @@ -139 +140 @@ -@@ -1240,7 +1247,21 @@ __swtim_arm_burst(const struct rte_event_timer_adapter *adapter, +@@ -1102,7 +1109,21 @@ __swtim_arm_burst(const struct rte_event_timer_adapter *adapter, @@ -160 +161 @@ - type, lcore_id, NULL, evtims[i]); + SINGLE, lcore_id, NULL, evtims[i]);