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 BA9234563B; Wed, 17 Jul 2024 22:29:55 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 344A0406B8; Wed, 17 Jul 2024 22:29:55 +0200 (CEST) Received: from mail-qv1-f44.google.com (mail-qv1-f44.google.com [209.85.219.44]) by mails.dpdk.org (Postfix) with ESMTP id 7B936402B3 for ; Wed, 17 Jul 2024 22:29:54 +0200 (CEST) Received: by mail-qv1-f44.google.com with SMTP id 6a1803df08f44-6b796667348so631856d6.0 for ; Wed, 17 Jul 2024 13:29:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=atomicrules-com.20230601.gappssmtp.com; s=20230601; t=1721248193; x=1721852993; darn=dpdk.org; 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=1olSn59d6J0gGiDkoJYhwn1j6GkA3/IPUDEop9dGBbo=; b=3M1fQ1HA82Df1b9VulnFzCMSj2DqT2GIZ/lBIfb7Fr8gr+YIs1BRWSfTjrXi0DaRaq EVhQNrsNS7cpOmqZS7xH5ojx0NtULC0jHCdQ2PP+XsLYL3w09d2gN42PVpVfueXyx7DQ 7sHWh2qFaC6LvL/jgJa5iU6YTtpsJu5tFdHFEKKNJncDcMZVzck/ot5PqmN14kTg64OO v0IoFaTCUvMFuh3b11L1Acq1B4IzfsSVNmreRX0pJH6GWM40wsEqY3kZCfCSqPJUN0lN la66n+qDeaFo/N89bbuIT2fCYxMbe2obZHQCwgHcB8SwNlhEZ4ThPopMtmyAnu1lrpWb ME6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721248193; x=1721852993; 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=1olSn59d6J0gGiDkoJYhwn1j6GkA3/IPUDEop9dGBbo=; b=OBNXVb8q1RSftCNDiUyZxo3+iv/tcUXx4g7bmc1Y6j8nP6olPqwTeerJ4S3awdm75w dkiQzKlSNlRz57guwysjEtvyFwRDHLnXq5SH9a/p0Sb9//8EuVJR55hI6o5jjs4lViiL 8tMQ7nnk1KWc1c4c4MOeYVA6SrxA40Tw6SV7L6ipMqn+wvObWbgiGUIPsmeoVlsAVfrZ ZpBE1EdP4Y+uNQBH8fclfkdIVG/ekvm9TCNiuSZywuBYVQfe6OZTdaH/ArRzm1EscnK2 SHhGSRP953KE8wiDtt51D8e2en3tVhRH6uFiSIm74N6x8WB+LLVu/StXSDNY5hqaCjfM tp9A== X-Gm-Message-State: AOJu0YwBWRx2G2tUe5DEVQed4yLCSAaD3dnSGXyIcq1rVlzEbyCYFcDW LwwiuZ6C4UnXBttUA005HbkJc2XU7jSc0uNAo3EOOwcOySnWLvg84k2XJOCcpE3vQqbMQjX5zqd amA== X-Google-Smtp-Source: AGHT+IEPk7ckEKC5Sx4SnPyNl/xIO41e5rzxq+wXa3Z9mXDNUlr9zxFcZbpaPFQTI8jgM290CC58Zg== X-Received: by 2002:a0c:d844:0:b0:6b5:2062:dd5c with SMTP id 6a1803df08f44-6b79cc177damr8046946d6.8.1721248193248; Wed, 17 Jul 2024 13:29:53 -0700 (PDT) Received: from localhost.localdomain (pool-173-48-111-149.bstnma.fios.verizon.net. [173.48.111.149]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b79c606840sm1940996d6.98.2024.07.17.13.29.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Jul 2024 13:29:52 -0700 (PDT) From: Ed Czeck To: dev@dpdk.org, ferruh.yigit@amd.com Cc: stable@dpdk.org, Shepard Siegel , John Miller Subject: [PATCH v4] net/ark: fix index arithmetic bug Date: Wed, 17 Jul 2024 16:29:49 -0400 Message-Id: <20240717202949.2647461-1-ed.czeck@atomicrules.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240716210631.2557642-1-ed.czeck@atomicrules.com> References: <20240716210631.2557642-1-ed.czeck@atomicrules.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Behavior for signed integer overflow is not defined which can causes undesired behavior at values near max and min bounds. The used of unsigned is defined as to use modulo arithmetic which is the desired behavior. This patch replaces int32_t with uint32_t except for necessary casts. Fixes: 9ee9e0d3b85e ("net/ark: update to reflect FPGA updates") Cc: stable@dpdk.org Signed-off-by: Ed Czeck --- v4: * fix typo in comment v3: * Clarify commit message. int_32_t overflow is undefined, while uint32_t is defined as modulo. * Additional comments in code. v2: * update patch to apply to dpdk-next-net --- drivers/net/ark/ark_ethdev_tx.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/drivers/net/ark/ark_ethdev_tx.c b/drivers/net/ark/ark_ethdev_tx.c index 9c89c85f50..16985bac22 100644 --- a/drivers/net/ark/ark_ethdev_tx.c +++ b/drivers/net/ark/ark_ethdev_tx.c @@ -39,8 +39,8 @@ struct __rte_cache_aligned ark_tx_queue { uint32_t queue_mask; /* 3 indexes to the paired data rings. */ - int32_t prod_index; /* where to put the next one */ - int32_t free_index; /* mbuf has been freed */ + uint32_t prod_index; /* where to put the next one */ + uint32_t free_index; /* mbuf has been freed */ /* The queue Id is used to identify the HW Q */ uint16_t phys_qid; @@ -49,7 +49,7 @@ struct __rte_cache_aligned ark_tx_queue { /* next cache line - fields written by device */ alignas(RTE_CACHE_LINE_MIN_SIZE) RTE_MARKER cacheline1; - volatile int32_t cons_index; /* hw is done, can be freed */ + volatile uint32_t cons_index; /* hw is done, can be freed */ }; /* Forward declarations */ @@ -108,7 +108,7 @@ eth_ark_xmit_pkts(void *vtxq, struct rte_mbuf **tx_pkts, uint16_t nb_pkts) uint32_t user_meta[5]; int stat; - int32_t prod_index_limit; + uint32_t prod_index_limit; uint16_t nb; uint8_t user_len = 0; const uint32_t min_pkt_len = ARK_MIN_TX_PKTLEN; @@ -123,8 +123,13 @@ eth_ark_xmit_pkts(void *vtxq, struct rte_mbuf **tx_pkts, uint16_t nb_pkts) /* leave 4 elements mpu data */ prod_index_limit = queue->queue_size + queue->free_index - 4; + /* Populate the buffer bringing prod_index up to or slightly beyond + * prod_index_limit. Prod_index will increament by 2 or more each + * iteration. Note: indexes are uint32_t, cast to (signed) int32_t + * to catch the slight overage case; e.g. (200 - 201) + */ for (nb = 0; - (nb < nb_pkts) && (prod_index_limit - queue->prod_index) > 0; + (nb < nb_pkts) && (int32_t)(prod_index_limit - queue->prod_index) > 0; ++nb) { mbuf = tx_pkts[nb]; @@ -194,13 +199,13 @@ eth_ark_tx_jumbo(struct ark_tx_queue *queue, struct rte_mbuf *mbuf, uint32_t *user_meta, uint8_t meta_cnt) { struct rte_mbuf *next; - int32_t free_queue_space; + uint32_t free_queue_space; uint8_t flags = ARK_DDM_SOP; free_queue_space = queue->queue_mask - (queue->prod_index - queue->free_index); /* We need up to 4 mbufs for first header and 2 for subsequent ones */ - if (unlikely(free_queue_space < (2 + (2 * mbuf->nb_segs)))) + if (unlikely(free_queue_space < (2U + (2U * mbuf->nb_segs)))) return -1; while (mbuf != NULL) { @@ -392,10 +397,11 @@ free_completed_tx(struct ark_tx_queue *queue) { struct rte_mbuf *mbuf; union ark_tx_meta *meta; - int32_t top_index; + uint32_t top_index; top_index = queue->cons_index; /* read once */ - while ((top_index - queue->free_index) > 0) { + + while ((int32_t)(top_index - queue->free_index) > 0) { meta = &queue->meta_q[queue->free_index & queue->queue_mask]; if (likely((meta->flags & ARK_DDM_SOP) != 0)) { mbuf = queue->bufs[queue->free_index & -- 2.34.1