DPDK-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: Zaiyu Wang <zaiyuwang@trustnetic.com>
Cc: dev@dpdk.org, stable@dpdk.org, Jiawen Wu <jiawenwu@trustnetic.com>
Subject: Re: [PATCH v4 07/20] net/txgbe: fix Tx desc free logic
Date: Sun, 17 May 2026 16:44:44 -0700	[thread overview]
Message-ID: <20260517164444.7c0cb0e0@phoenix.local> (raw)
In-Reply-To: <20260511103604.19724-8-zaiyuwang@trustnetic.com>

On Mon, 11 May 2026 18:35:49 +0800
Zaiyu Wang <zaiyuwang@trustnetic.com> wrote:

> On some server environments, this driver caused TDM non-fatal errors
> or PCIe request errors during Tx operation
> 
> In Amber-Lite NIC's Tx head write-back mode, the hardware periodically
> writes back a head index pointing to the next descriptor it is adout
> to process in Tx ring. All descriptors before the head are considered
> processed by hardware and can be safely freed by the driver.
> 
> The root cause is that the driver can safely free a batch of descriptors
> only when the hardware's write-back head pointer has advanced beyond all
> descriptors in that batch, meaning they have all been processed by the
> hardware. If the driver frees a descriptor before the hardware has
> finished processing it, invalid memory access may occur, leading to the
> observed bug.
> 
> To fix the issue, correct the boundary check in all three Tx cleanup
> functions, each of which was missing the proper condition to prevent
> freeing unprocessed descriptors.
> 
> Fixes: 8ada71d0bb7f ("net/txgbe: add Tx head write-back mode for Amber-Lite")
> Cc: stable@dpdk.org
> 
> Signed-off-by: Zaiyu Wang <zaiyuwang@trustnetic.com>
> ---

Lots of AI feedback on this one.


# Review of DPDK Patch: net/txgbe: fix Tx desc free logic

## Errors

### 1. Shared variable access without atomics (3 locations)

**Lines affected:** 
- `drivers/net/txgbe/txgbe_rxtx.c:101` (txgbe_tx_free_bufs)
- `drivers/net/txgbe/txgbe_rxtx.c:655` (txgbe_xmit_cleanup)
- `drivers/net/txgbe/txgbe_rxtx_vec_common.h:98` (txgbe_tx_free_bufs)

```c
volatile uint16_t head = (uint16_t)*txq->headwb_mem;
```

**Problem:** The code reads a hardware-updated value (`*txq->headwb_mem`) using a `volatile` qualifier. The `volatile` keyword does NOT provide atomicity or memory ordering guarantees between the hardware and CPU. On some architectures, reading a `uint16_t` from DMA-accessible memory without proper barriers or atomic operations can result in torn reads, stale cached values, or reordering issues.

**Fix:** Use atomic load with appropriate memory ordering:

```c
uint16_t head = rte_atomic_load_explicit(
    (uint16_t *)txq->headwb_mem, 
    rte_memory_order_acquire);
```

The `acquire` ordering ensures that all descriptor writes by the hardware are visible to the CPU after reading the head pointer. This is critical for correctness in hardware write-back scenarios.

Note: The `headwb_mem` field type may need adjustment to be an `rte_atomic` type or cast appropriately.

---

### 2. Whitespace/indentation error in vec_common.h

**Line:** `drivers/net/txgbe/txgbe_rxtx_vec_common.h:97-98`

```c
		tx_last_dd -= txq->nb_tx_desc;

		volatile uint16_t head = (uint16_t)*txq->headwb_mem;
```

**Problem:** Inconsistent indentation. The `volatile uint16_t head` declaration is indented with tabs-only (correct), but it visually appears to be part of a removed block based on the diff context showing the previous line had different indentation.

**Fix:** Verify the indentation matches the surrounding code block. Based on the diff, this appears correct after the change, but confirm the tabs align with the function's indentation level.

---

## Warnings

### 1. Logic change correctness verification needed

The patch adds three new early-return conditions checking if `head == tx_next_dd`. While this appears correct (hardware hasn't advanced past the point we want to free to), the logic for wrap-around cases should be verified:

```c
if (txq->tx_next_dd == head)
    return 0;
else if (txq->tx_next_dd > head && head > tx_last_dd)
    return 0;
else if (tx_last_dd > txq->tx_next_dd &&
         (head > tx_last_dd || head < txq->tx_next_dd))
    return 0;
```

**Concern:** The wrap-around logic assumes the ring wraps at `nb_tx_desc`. The condition `tx_last_dd > txq->tx_next_dd` handles the wrap case, but verify that:
- `tx_last_dd` calculation correctly wraps (it does: `tx_last_dd -= txq->nb_tx_desc`)
- The inequality checks are strict `>` not `>=` consistently

The existing code appears correct, but this is complex boundary logic that warrants testing, especially the second `else if` condition in wrap-around scenarios.

---

### 2. Missing const qualifier on read-only atomic load result

**All three locations:** The `head` variable is read from hardware but never modified:

```c
volatile uint16_t head = (uint16_t)*txq->headwb_mem;
```

**Suggestion:** Once converted to atomic operations, declare as `const`:

```c
const uint16_t head = rte_atomic_load_explicit(...);
```

This documents that `head` is never modified after the initial read and prevents accidental writes.

---

## Info

### 1. Consider helper function for repeated logic

The same boundary-check logic appears in three separate functions with only minor variations. Consider extracting it to a static inline helper:

```c
static inline bool
txgbe_hw_head_in_range(uint16_t hw_head, uint16_t start, 
                       uint16_t end, uint16_t ring_size)
{
    if (start == hw_head)
        return false;
    
    if (start > hw_head)
        return hw_head <= end;
    else /* wrap case */
        return hw_head > end || hw_head < start;
}
```

This would reduce code duplication and make the logic easier to test and maintain.

---

### 2. Comment explaining write-back head semantics

The commit message mentions "Tx head write-back mode" but the code lacks comments explaining the hardware behavior. Consider adding:

```c
/*
 * In Amber-Lite NIC Tx head write-back mode, hardware periodically
 * updates *headwb_mem with the index of the next descriptor to process.
 * Descriptors before this head pointer are safe to free.
 */
const uint16_t head = rte_atomic_load_explicit(...);
```

---

## Summary

**Must fix (Errors):**
1. Replace `volatile` read with `rte_atomic_load_explicit()` with `rte_memory_order_acquire` ordering in all three locations

**Should fix (Warnings):**
1. Add `const` qualifier to the loaded `head` value after converting to atomics
2. Verify wrap-around boundary logic with testing (appears correct but is complex)

**Consider (Info):**
1. Extract repeated boundary-check logic to a helper function
2. Add comments documenting hardware write-back semantics

The core fix (adding the `head == tx_next_dd` check) appears correct and addresses the described bug. The primary issue is the use of `volatile` instead of proper atomic operations for accessing hardware-updated shared memory.


  reply	other threads:[~2026-05-17 23:56 UTC|newest]

Thread overview: 105+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-23  3:40 [PATCH 00/18] Wangxun Fixes Zaiyu Wang
2026-04-23  3:40 ` [PATCH 01/18] net/txgbe: remove duplicate xstats counters Zaiyu Wang
2026-04-23  3:40 ` [PATCH 02/18] net/ngbe: " Zaiyu Wang
2026-04-23  3:40 ` [PATCH 03/18] net/ngbe: add missing CDR config for YT PHY Zaiyu Wang
2026-04-23  3:40 ` [PATCH 04/18] net/ngbe: fix VF promiscuous and allmulticast Zaiyu Wang
2026-04-23  3:40 ` [PATCH 05/18] net/txgbe: fix inaccuracy in TX rate limiting Zaiyu Wang
2026-04-23  3:40 ` [PATCH 06/18] net/txgbe: fix link status check condition Zaiyu Wang
2026-04-23  3:40 ` [PATCH 07/18] net/txgbe: fix Tx desc free logic Zaiyu Wang
2026-04-23  3:40 ` [PATCH 08/18] net/txgbe: fix link flow control registers for Amber-Lite Zaiyu Wang
2026-04-23  7:54   ` Jiawen Wu
2026-04-23  3:40 ` [PATCH 09/18] net/txgbe: fix link flow control config for Sapphire Zaiyu Wang
2026-04-23  3:40 ` [PATCH 10/18] net/txgbe: fix a mass of unknown interrupts Zaiyu Wang
2026-04-23  3:40 ` [PATCH 11/18] net/txgbe: fix traffic class priority configuration Zaiyu Wang
2026-04-23  3:40 ` [PATCH 12/18] net/txgbe: fix link stability for 25G NIC Zaiyu Wang
2026-04-23  8:22   ` Jiawen Wu
2026-04-23  3:40 ` [PATCH 13/18] net/txgbe: fix link stability for 40G NIC Zaiyu Wang
2026-04-23  3:40 ` [PATCH 14/18] net/txgbe: fix link stability for Amber-Lite backplane mode Zaiyu Wang
2026-04-23  3:40 ` [PATCH 15/18] net/txgbe: fix FEC mode configuration on 25G NIC Zaiyu Wang
2026-04-23  3:40 ` [PATCH 16/18] net/txgbe: fix SFP module identification Zaiyu Wang
2026-04-23  3:40 ` [PATCH 17/18] net/txgbe: fix get module info operation Zaiyu Wang
2026-04-23  3:40 ` [PATCH 18/18] net/txgbe: fix get eeprom operation Zaiyu Wang
2026-04-24 21:59   ` Stephen Hemminger
2026-04-29 10:24 ` [PATCH v2 00/20] Wangxun Fixes Zaiyu Wang
2026-04-29 10:24   ` [PATCH v2 01/20] net/txgbe: remove duplicate xstats counters Zaiyu Wang
2026-04-29 10:24   ` [PATCH v2 02/20] net/ngbe: " Zaiyu Wang
2026-04-29 10:24   ` [PATCH v2 03/20] net/ngbe: add missing CDR config for YT PHY Zaiyu Wang
2026-04-29 10:24   ` [PATCH v2 04/20] net/ngbe: fix VF promiscuous and allmulticast Zaiyu Wang
2026-04-29 10:24   ` [PATCH v2 05/20] net/txgbe: fix inaccuracy in TX rate limiting Zaiyu Wang
2026-04-29 10:25   ` [PATCH v2 06/20] net/txgbe: fix link status check condition Zaiyu Wang
2026-04-29 10:25   ` [PATCH v2 07/20] net/txgbe: fix Tx desc free logic Zaiyu Wang
2026-04-29 10:25   ` [PATCH v2 08/20] net/txgbe: fix link flow control registers for Amber-Lite Zaiyu Wang
2026-04-29 15:10     ` Stephen Hemminger
2026-04-29 10:25   ` [PATCH v2 09/20] net/txgbe: fix link flow control config for Sapphire Zaiyu Wang
2026-04-29 10:25   ` [PATCH v2 10/20] net/txgbe: fix a mass of unknown interrupts Zaiyu Wang
2026-04-29 10:25   ` [PATCH v2 11/20] net/txgbe: fix traffic class priority configuration Zaiyu Wang
2026-04-29 15:11     ` Stephen Hemminger
2026-05-09 11:06       ` Zaiyu Wang
2026-04-29 10:25   ` [PATCH v2 12/20] net/txgbe: fix link stability for 25G NIC Zaiyu Wang
2026-04-29 15:12     ` Stephen Hemminger
2026-04-29 10:25   ` [PATCH v2 13/20] net/txgbe: fix link stability for 40G NIC Zaiyu Wang
2026-04-29 10:25   ` [PATCH v2 14/20] net/txgbe: fix link stability for Amber-Lite backplane mode Zaiyu Wang
2026-04-29 10:25   ` [PATCH v2 15/20] net/txgbe: fix FEC mode configuration on 25G NIC Zaiyu Wang
2026-04-29 10:25   ` [PATCH v2 16/20] net/txgbe: fix SFP module identification Zaiyu Wang
2026-04-29 10:25   ` [PATCH v2 17/20] net/txgbe: fix get module info operation Zaiyu Wang
2026-04-29 10:25   ` [PATCH v2 18/20] net/txgbe: fix get eeprom operation Zaiyu Wang
2026-04-29 10:25   ` [PATCH v2 19/20] net/txgbe: fix to reset Tx write-back pointer Zaiyu Wang
2026-04-29 10:25   ` [PATCH v2 20/20] net/txgbe: fix to enable Tx desc check Zaiyu Wang
2026-05-09 11:28 ` [PATCH v3 00/20] Wangxun Fixes Zaiyu Wang
2026-05-09 11:28   ` [PATCH v3 01/20] net/txgbe: remove duplicate xstats counters Zaiyu Wang
2026-05-09 11:28   ` [PATCH v3 02/20] net/ngbe: " Zaiyu Wang
2026-05-09 11:28   ` [PATCH v3 03/20] net/ngbe: add missing CDR config for YT PHY Zaiyu Wang
2026-05-09 11:28   ` [PATCH v3 04/20] net/ngbe: fix VF promiscuous and allmulticast Zaiyu Wang
2026-05-09 11:28   ` [PATCH v3 05/20] net/txgbe: fix inaccuracy in TX rate limiting Zaiyu Wang
2026-05-09 11:28   ` [PATCH v3 06/20] net/txgbe: fix link status check condition Zaiyu Wang
2026-05-09 11:28   ` [PATCH v3 07/20] net/txgbe: fix Tx desc free logic Zaiyu Wang
2026-05-09 11:28   ` [PATCH v3 08/20] net/txgbe: fix link flow control registers for Amber-Lite Zaiyu Wang
2026-05-09 11:28   ` [PATCH v3 09/20] net/txgbe: fix link flow control config for Sapphire Zaiyu Wang
2026-05-09 11:28   ` [PATCH v3 10/20] net/txgbe: fix a mass of unknown interrupts Zaiyu Wang
2026-05-09 11:28   ` [PATCH v3 11/20] net/txgbe: fix traffic class priority configuration Zaiyu Wang
2026-05-09 11:28   ` [PATCH v3 12/20] net/txgbe: fix link stability for 25G NIC Zaiyu Wang
2026-05-09 11:28   ` [PATCH v3 13/20] net/txgbe: fix link stability for 40G NIC Zaiyu Wang
2026-05-09 11:28   ` [PATCH v3 14/20] net/txgbe: fix link stability for Amber-Lite backplane mode Zaiyu Wang
2026-05-09 11:28   ` [PATCH v3 15/20] net/txgbe: fix FEC mode configuration on 25G NIC Zaiyu Wang
2026-05-09 11:28   ` [PATCH v3 16/20] net/txgbe: fix SFP module identification Zaiyu Wang
2026-05-09 11:28   ` [PATCH v3 17/20] net/txgbe: fix get module info operation Zaiyu Wang
2026-05-09 11:28   ` [PATCH v3 18/20] net/txgbe: fix get eeprom operation Zaiyu Wang
2026-05-09 11:28   ` [PATCH v3 19/20] net/txgbe: fix to reset Tx write-back pointer Zaiyu Wang
2026-05-09 11:28   ` [PATCH v3 20/20] net/txgbe: fix to enable Tx desc check Zaiyu Wang
2026-05-09 15:44   ` [PATCH v3 00/20] Wangxun Fixes Stephen Hemminger
2026-05-09 17:07   ` Stephen Hemminger
2026-05-11 10:28     ` Zaiyu Wang
2026-05-11 10:35 ` [PATCH v4 " Zaiyu Wang
2026-05-11 10:35   ` [PATCH v4 01/20] net/txgbe: remove duplicate xstats counters Zaiyu Wang
2026-05-11 10:35   ` [PATCH v4 02/20] net/ngbe: " Zaiyu Wang
2026-05-11 10:35   ` [PATCH v4 03/20] net/ngbe: add missing CDR config for YT PHY Zaiyu Wang
2026-05-17 23:37     ` Stephen Hemminger
2026-05-11 10:35   ` [PATCH v4 04/20] net/ngbe: fix VF promiscuous and allmulticast Zaiyu Wang
2026-05-17 23:39     ` Stephen Hemminger
2026-05-11 10:35   ` [PATCH v4 05/20] net/txgbe: fix inaccuracy in Tx rate limiting Zaiyu Wang
2026-05-17 23:40     ` Stephen Hemminger
2026-05-11 10:35   ` [PATCH v4 06/20] net/txgbe: fix link status check condition Zaiyu Wang
2026-05-11 10:35   ` [PATCH v4 07/20] net/txgbe: fix Tx desc free logic Zaiyu Wang
2026-05-17 23:44     ` Stephen Hemminger [this message]
2026-05-11 10:35   ` [PATCH v4 08/20] net/txgbe: fix link flow control registers for Amber-Lite Zaiyu Wang
2026-05-11 10:35   ` [PATCH v4 09/20] net/txgbe: fix link flow control config for Sapphire Zaiyu Wang
2026-05-17 23:46     ` Stephen Hemminger
2026-05-11 10:35   ` [PATCH v4 10/20] net/txgbe: fix a mass of unknown interrupts Zaiyu Wang
2026-05-11 10:35   ` [PATCH v4 11/20] net/txgbe: fix traffic class priority configuration Zaiyu Wang
2026-05-11 10:35   ` [PATCH v4 12/20] net/txgbe: fix link stability for 25G NIC Zaiyu Wang
2026-05-17 23:49     ` Stephen Hemminger
2026-05-11 10:35   ` [PATCH v4 13/20] net/txgbe: fix link stability for 40G NIC Zaiyu Wang
2026-05-11 10:35   ` [PATCH v4 14/20] net/txgbe: fix link stability for Amber-Lite backplane mode Zaiyu Wang
2026-05-17 23:50     ` Stephen Hemminger
2026-05-11 10:35   ` [PATCH v4 15/20] net/txgbe: fix FEC mode configuration on 25G NIC Zaiyu Wang
2026-05-11 10:35   ` [PATCH v4 16/20] net/txgbe: fix SFP module identification Zaiyu Wang
2026-05-17 23:52     ` Stephen Hemminger
2026-05-11 10:35   ` [PATCH v4 17/20] net/txgbe: fix get module info operation Zaiyu Wang
2026-05-17 23:53     ` Stephen Hemminger
2026-05-11 10:36   ` [PATCH v4 18/20] net/txgbe: fix get EEPROM operation Zaiyu Wang
2026-05-17 23:54     ` Stephen Hemminger
2026-05-11 10:36   ` [PATCH v4 19/20] net/txgbe: fix to reset Tx write-back pointer Zaiyu Wang
2026-05-11 10:36   ` [PATCH v4 20/20] net/txgbe: fix to enable Tx desc check Zaiyu Wang
2026-05-17 23:55     ` Stephen Hemminger
2026-05-18 14:54   ` [PATCH v4 00/20] Wangxun Fixes Stephen Hemminger
2026-05-19  6:56     ` Zaiyu Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260517164444.7c0cb0e0@phoenix.local \
    --to=stephen@networkplumber.org \
    --cc=dev@dpdk.org \
    --cc=jiawenwu@trustnetic.com \
    --cc=stable@dpdk.org \
    --cc=zaiyuwang@trustnetic.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox