linux-stable/drivers/net/ethernet/freescale/enetc
Vladimir Oltean 84ce1ca3fe net: enetc: survive memory pressure without crashing
Under memory pressure, enetc_refill_rx_ring() may fail, and when called
during the enetc_open() -> enetc_setup_rxbdr() procedure, this is not
checked for.

An extreme case of memory pressure will result in exactly zero buffers
being allocated for the RX ring, and in such a case it is expected that
hardware drops all RX packets due to lack of buffers.

This does not happen, because the reset-default value of the consumer
and produces index is 0, and this makes the ENETC think that all buffers
have been initialized and that it owns them (when in reality none were).

The hardware guide explains this best:

| Configure the receive ring producer index register RBaPIR with a value
| of 0. The producer index is initially configured by software but owned
| by hardware after the ring has been enabled. Hardware increments the
| index when a frame is received which may consume one or more BDs.
| Hardware is not allowed to increment the producer index to match the
| consumer index since it is used to indicate an empty condition. The ring
| can hold at most RBLENR[LENGTH]-1 received BDs.
|
| Configure the receive ring consumer index register RBaCIR. The
| consumer index is owned by software and updated during operation of the
| of the BD ring by software, to indicate that any receive data occupied
| in the BD has been processed and it has been prepared for new data.
| - If consumer index and producer index are initialized to the same
|   value, it indicates that all BDs in the ring have been prepared and
|   hardware owns all of the entries.
| - If consumer index is initialized to producer index plus N, it would
|   indicate N BDs have been prepared. Note that hardware cannot start if
|   only a single buffer is prepared due to the restrictions described in
|   (2).
| - Software may write consumer index to match producer index anytime
|   while the ring is operational to indicate all received BDs prior have
|   been processed and new BDs prepared for hardware.

Normally, the value of rx_ring->rcir (consumer index) is brought in sync
with the rx_ring->next_to_use software index, but this only happens if
page allocation ever succeeded.

When PI==CI==0, the hardware appears to receive frames and write them to
DMA address 0x0 (?!), then set the READY bit in the BD.

The enetc_clean_rx_ring() function (and its XDP derivative) is naturally
not prepared to handle such a condition. It will attempt to process
those frames using the rx_swbd structure associated with index i of the
RX ring, but that structure is not fully initialized (enetc_new_page()
does all of that). So what happens next is undefined behavior.

To operate using no buffer, we must initialize the CI to PI + 1, which
will block the hardware from advancing the CI any further, and drop
everything.

The issue was seen while adding support for zero-copy AF_XDP sockets,
where buffer memory comes from user space, which can even decide to
supply no buffers at all (example: "xdpsock --txonly"). However, the bug
is present also with the network stack code, even though it would take a
very determined person to trigger a page allocation failure at the
perfect time (a series of ifup/ifdown under memory pressure should
eventually reproduce it given enough retries).

Fixes: d4fd0404c1 ("enetc: Introduce basic PF and VF ENETC ethernet drivers")
Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Reviewed-by: Claudiu Manoil <claudiu.manoil@nxp.com>
Link: https://lore.kernel.org/r/20221027182925.3256653-1-vladimir.oltean@nxp.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2022-10-27 11:32:25 -07:00
..
Kconfig
Makefile net: enetc: deny offload of tc-based TSN features on VF interfaces 2022-09-20 11:27:10 -07:00
enetc.c net: enetc: survive memory pressure without crashing 2022-10-27 11:32:25 -07:00
enetc.h net: enetc: offload per-tc max SDU from tc-taprio 2022-09-29 18:52:06 -07:00
enetc_cbdr.c net:enetc: command BD ring data memory alloc as one function alone 2022-02-09 13:23:25 +00:00
enetc_ethtool.c net: enetc: expose some standardized ethtool counters 2022-09-19 17:58:25 -07:00
enetc_hw.h net: enetc: use common naming scheme for PTGCR and PTGCAPR registers 2022-09-29 18:52:06 -07:00
enetc_ierb.c net: update NXP copyright text 2021-09-17 13:52:17 +01:00
enetc_ierb.h net: update NXP copyright text 2021-09-17 13:52:17 +01:00
enetc_mdio.c enetc: use correct format characters 2022-03-17 16:30:48 -07:00
enetc_msg.c
enetc_pci_mdio.c net: enetc: Use pci_release_region() to release some resources 2022-05-28 20:20:42 +01:00
enetc_pf.c net: enetc: offload per-tc max SDU from tc-taprio 2022-09-29 18:52:06 -07:00
enetc_pf.h net: phy: lynx: refactor Lynx PCS module to use generic phylink_pcs 2022-01-02 18:48:47 +00:00
enetc_ptp.c net: enetc: Remove useless DMA-32 fallback configuration 2022-01-09 16:52:20 -08:00
enetc_qos.c net: enetc: Remove duplicated include in enetc_qos.c 2022-10-07 08:45:16 +01:00
enetc_vf.c net: enetc: deny offload of tc-based TSN features on VF interfaces 2022-09-20 11:27:10 -07:00