From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <stable-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id B6DBA41C2B
	for <public@inbox.dpdk.org>; Tue,  7 Feb 2023 07:01:02 +0100 (CET)
Received: from mails.dpdk.org (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id AED5D427E9;
	Tue,  7 Feb 2023 07:01:02 +0100 (CET)
Received: from mail-ua1-f49.google.com (mail-ua1-f49.google.com
 [209.85.222.49])
 by mails.dpdk.org (Postfix) with ESMTP id E84C840E6E;
 Tue,  7 Feb 2023 07:01:00 +0100 (CET)
Received: by mail-ua1-f49.google.com with SMTP id u3so2590579uae.0;
 Mon, 06 Feb 2023 22:01:00 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
 h=cc:to:subject:message-id:date:from:in-reply-to:references
 :mime-version:from:to:cc:subject:date:message-id:reply-to;
 bh=eFdDxEckg+wjs9ZkPx0woSvofTFew2CHbCfOY7uC9xM=;
 b=ZrialEFInnr9atqr8YKbeQm3JeExxmJOKUx4RsGoEz6FEYelOT4FCqic5bFZMuje9c
 PevPsSNQq/IYVAWIf6y15R+HCNsZ/nWyo9i4eBtDx24OCN0Ukx+JiUh5YCJEavoPYGGC
 9AWEjcHOK749Dc8eb0FbFBy0NYLsVpLquhDurdLn6BEWaEEufaQegY5iKwp+1mbgsSyt
 +qMpALtCCw76ulWqhYcvmgD/id8wV1kuMp+Nj0NI2h9IiabdcwnpV/A7yycrNY3izaaA
 GWyoMRRmDmOeSj4aggFNGRTv8pm/zHWBYMuruFPdrtOFZ6w23wZgUjJvKoiBLIhQVP8N
 wcvA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20210112;
 h=cc:to:subject:message-id:date:from:in-reply-to:references
 :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id
 :reply-to;
 bh=eFdDxEckg+wjs9ZkPx0woSvofTFew2CHbCfOY7uC9xM=;
 b=woQBwNj+F3uUNuijOGoGuBpJsmv/xfXOeLTbTaFHygbVtt7AH9zm1b6aUYB8J1X6Np
 ycb5r7f8SCpFTZdlsUqJff6mmWcH0k4wP2yGoe+dtwxiSPqthsi89btitYQzV9Etfen2
 HSXK8M8DWDBd02cOb3jO31Bsfxilmb0wL36KFX2kUW+V674bZR1LGMfYFCx6yXpH1vqi
 zRT8EiguhLrKMISDpdM6YmIgOhHCLrb8oL5qHgraorSZf1JIM2alAXxdbuQQVlsVpX+U
 wUOZaT8iFH+jxVlLjkXwmnVt6O6eqILKu2ohz6yz3paYAXW+OW3WFIZrVIM11/LMhV7h
 PqEg==
X-Gm-Message-State: AO0yUKVDhtl9tFSOg4MRvEoB9o3EQd1M3cnLZoWQv+lZmmVFODUuAUMf
 Kpd5L58727MIjvTENxUXDb4+PDgvTj8uJDPQI/nMXFB2hSM=
X-Google-Smtp-Source: AK7set+NwHhX7DhNdooIbFpOATGG6aaDSqocCyLsTJaHAGZxP90pWGjitTzSsk8rDpJP1TT6OSEPQtnHeArY/8TyDEc=
X-Received: by 2002:ab0:2401:0:b0:683:8d8f:2671 with SMTP id
 f1-20020ab02401000000b006838d8f2671mr389411uan.24.1675749660179; Mon, 06 Feb
 2023 22:01:00 -0800 (PST)
MIME-Version: 1.0
References: <20230124160945.3003303-1-erik.g.carrillo@intel.com>
 <20230124204555.3022361-1-erik.g.carrillo@intel.com>
In-Reply-To: <20230124204555.3022361-1-erik.g.carrillo@intel.com>
From: Jerin Jacob <jerinjacobk@gmail.com>
Date: Tue, 7 Feb 2023 11:30:33 +0530
Message-ID: <CALBAE1PaWLhXU=1AAFDHNu1hqnE3w1e6GL2XE1i0vSU6cVf3iw@mail.gmail.com>
Subject: Re: [PATCH v2] eventdev/timer: fix overflow issue
To: Erik Gabriel Carrillo <erik.g.carrillo@intel.com>, 
 Stephen Hemminger <stephen@networkplumber.org>
Cc: jerinj@marvell.com, dev@dpdk.org, stable@dpdk.org
Content-Type: text/plain; charset="UTF-8"
X-BeenThere: stable@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: patches for DPDK stable branches <stable.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/stable>,
 <mailto:stable-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/stable/>
List-Post: <mailto:stable@dpdk.org>
List-Help: <mailto:stable-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/stable>,
 <mailto:stable-request@dpdk.org?subject=subscribe>
Errors-To: stable-bounces@dpdk.org

On Wed, Jan 25, 2023 at 2:16 AM Erik Gabriel Carrillo
<erik.g.carrillo@intel.com> wrote:
>
> The software timer adapter converts event timer timeout ticks to a
> number of CPU cycles at which an rte_timer should expire. The
> computation uses integer operations that can result in overflow.
>
> Use floating point operations instead to perform the computation, and
> convert the final result back to an integer type when returning. 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")
> Cc: stable@dpdk.org
>
> Signed-off-by: Erik Gabriel Carrillo <erik.g.carrillo@intel.com>
> ---
> v2:
> * Fix implicit int to float conversion build warning on Clang

Not yet addressed the @Stephen Hemminger  comments on using of
rte_reciprocal. Also means to check what compute can be moved to
slowpath.
Marking as "Change requested", Looking for next version.


>
>  lib/eventdev/rte_event_timer_adapter.c | 71 ++++++++++++--------------
>  1 file changed, 34 insertions(+), 37 deletions(-)
>
> diff --git a/lib/eventdev/rte_event_timer_adapter.c b/lib/eventdev/rte_event_timer_adapter.c
> index d357f7403a..2efeb73bce 100644
> --- a/lib/eventdev/rte_event_timer_adapter.c
> +++ b/lib/eventdev/rte_event_timer_adapter.c
> @@ -719,13 +719,28 @@ 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;
> +       uint64_t timeout_nsecs;
> +       double cycles_per_nsec;
> +
> +       cycles_per_nsec = (double)rte_get_timer_hz() / 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;
> +
> +       if (timeout_cycles)
> +               *timeout_cycles = (uint64_t)ceil(timeout_nsecs *
> +                                                cycles_per_nsec);
> +
> +       return 0;
>  }
>
>  /* This function returns true if one or more (adapter) ticks have occurred since
> @@ -759,23 +774,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
>   */
> @@ -1195,21 +1193,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,
> @@ -1225,7 +1208,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,
>                                           type, lcore_id, NULL, evtims[i]);
>                 if (ret < 0) {
> --
> 2.23.0
>