firmware: tegra: ivc: back off when a peer stalls the handshake
PATCH 1/1 firmware: tegra: ivc: back off when a peer stalls the handshake
An untrusted peer can wedge a channel for ever: park its state word on SYNC and stop touching the ring. No path in tegra_ivc_notified() can change the victim's rx word, so rx_state stays SYNC and the loop is absorbing rather than slow -- pass 7,651,085 is indistinguishable from pass 1. Each pass rewrites the ACK already in tx.state, re-zeroes both counters, rings the doorbell and returns -EAGAIN.
The only in-tree caller, tegra186_bpmp_channel_reset(), retries on that with no timeout, no iteration cap and no cpu_relax(), on both the probe and the resume path, so the victim burns a core at roughly 1.5M iterations per second for as long as the peer stays quiet.
A peer is a service, not a VM: one untrusted guest can hold a channel to each of a rack of service partitions, and nothing here makes wedging the tenth harder than the first.
This is not a new class of concern for ivc.c. tegra_ivc_empty() already carries an over-full check "to prevent denial of service attacks", and calls that condition "an invalid case caused by a potentially malicious peer". That defence covers the counters; the handshake has none.
Apply geometric backoff to calls that make no progress, modelled on xen_irq_lateeoi_locked(): a saturating count, a doubling delay, a hard cap, and a reset the moment the peer moves. Progress is detected by reading tx.state back rather than by tracking which branch ran, so the states from which no branch can escape are covered too. The wait is a bounded busy-wait by default because tegra_bpmp_transfer_atomic() reaches this with interrupts disabled, and it uses local_clock() because the resume path can run with timekeeping suspended.
This is a floor, not a fix. The return value is unchanged, the caller still retries for ever and probe or resume still never completes -- the spin just stops costing what other channels have to share. Bounding the loop belongs in the caller.
---
Precedent
---------ivc.c already models a hostile peer. tegra_ivc_empty() carries an over-full check "to prevent denial of service attacks", and calls the condition "an invalid case caused by a potentially malicious peer". That defence covers the counters; the handshake has none. This closes the second path, not a new class of concern.
The handshake has never been revisited: since 2016, every patch to ivc.c has been the import itself or a tree-wide sweep.
Scope
-----
A peer is a service on one channel, not a VM: DRIVE OS is one untrusted Linux guest with a channel to each of about ten service partitions. One hostile peer wedges one channel, and nothing makes wedging the tenth harder than the first.
Whether x services down becomes the SoC down, I cannot show. That needs the spinning partitions to share cores rather than being pinned apart by the PCT, and the hypervisor to enforce no per-partition CPU budget. I have no Tegra hardware and have not looked at the hypervisor -- treat the SoC-wide version as something to check, not a claim made here.
Measurements
------------
Keying the test on the first branch -- where the doorbell amplification lives -- would cover only a third of the reachable cases. Over 30 inputs that stall the loop:
looping endpoint n before after
rx=SYNC, tx=ACK (branch A) 9 ~7,700,000 50-64
rx=SYNC, tx=SYNC (enters A) 2 ~7,700,000 65
tx=SYNC, rx=EST (empty else) 15 8,387,013 63-64
tx=SYNC, rx=OTHER (empty else) 3 7,648,606 64
tx=OTHER, rx=EST (no escape) 1 8,244,347 64Two thirds are the do-nothing else at the end of the chain, which emits no doorbell at all -- silent, and so easier to miss. The last row is an out-of-enum tx.state from which no branch can escape.
Why only a floor
----------------
xen-netback puts five layers between a hostile frontend and the host that IVC has none of: a thread per peer, a blocking wait, stall detection that parks and recovers, a per-peer quota, and a fatal way to declare one peer dead. Backoff is the floor under those, not a substitute.
The two tunables and two counters are documented at their declarations in ivc.h. Nothing in the tree sets or reads them yet.
Testing
-------
Exercised under LKL with a userspace fuzzer driving ivc.c directly; the numbers above are from those runs.
arm64 defconfig builds clean to vmlinux and Image under W=1 with TEGRA_IVC, TEGRA_BPMP and the 186/194/234/264 SoC options on. ivc.o also builds clean for 32-bit ARM and pulls in no 64-bit division helper. Not run on Tegra hardware.
Tool assistance
---------------
AI tooling was used throughout -- audit, fuzzing setup, patch and changelog -- but never fully automated; every step was hand-checked. The Assisted-by trailer is on the patch.
--- drivers/firmware/tegra/ivc.c | 132 +++++++++++++++++++++++++++++++++++++++++++ include/soc/tegra/ivc.h | 44 +++++++++++++++ 2 files changed, 176 insertions(+)
drivers/firmware/tegra/ivc.c +132 -0
@@ -3,6 +3,12 @@* Copyright (c) 2014-2016, NVIDIA CORPORATION. All rights reserved. */ +#include <linux/delay.h> +#include <linux/math64.h> +#include <linux/minmax.h> +#include <linux/sched/clock.h> +#include <linux/time64.h> +#include <soc/tegra/ivc.h> #define TEGRA_IVC_ALIGN 64
@@ -395,10 +401,111 @@ int tegra_ivc_write_advance(struct tegra_ivc *ivc) }EXPORT_SYMBOL (tegra_ivc_write_advance); +/* +* A peer that moves to the SYNC state and then stops responding leaves this +* end with nothing to do. Each call to tegra_ivc_notified() repeats the same +* work, changes no state and returns -EAGAIN again, and the only in-tree +* caller retries in a loop with no timeout and no iteration limit. The result +* is a doorbell and several shared memory writes per iteration, for as long as +* the peer stays silent. +* +* Delay the call instead of skipping the work. Each call still does everything +* it did before, so the behaviour a caller sees is unchanged apart from how +* long the call takes. The delay doubles on each fruitless call, up to +* TEGRA_IVC_RESYNC_MAX_NS, which reduces the doorbell rate, the memory traffic +* and the CPU time together. +* +* A call counts as fruitless if it did not change this end's own state and is +* about to return -EAGAIN. That covers every case where a peer can stall the +* handshake, including the states from which no branch below can make +* progress, without naming any of them. +* +* The loop itself is not bounded by this. The channel really is not ready, so +* the return value does not change and a caller that retries forever still +* does. Only the cost of retrying falls. +*/ +#define TEGRA_IVC_RESYNC_MIN_NS (10 * NSEC_PER_USEC) +#define TEGRA_IVC_RESYNC_MAX_NS (100 * NSEC_PER_MSEC) +#define TEGRA_IVC_RESYNC_MAX_SHIFT 16 +#define TEGRA_IVC_RESYNC_THRESHOLD 1 + +/* +* resync_count stops increasing at U8_MAX, so a threshold near that value +* would leave no room for the delay to grow and the backoff would never take +* effect. Clamp the threshold so that a larger value always means a longer +* grace period, and leave TEGRA_IVC_RESYNC_DISABLED as the only way to turn +* the backoff off. +*/ +#define TEGRA_IVC_RESYNC_THRESHOLD_MAX 32 + +static void tegra_ivc_resync_restart (struct tegra_ivc *ivc) +{ + ivc->resync_count =0 ; +} + +static void tegra_ivc_resync_wait (struct tegra_ivc *ivc) +{ +unsigned int threshold = ivc->resync_threshold; +unsigned int shift; +u64 deadline; +u64 delay; + +if (threshold ==TEGRA_IVC_RESYNC_DISABLED ) +return ; + +if (!threshold) + threshold =TEGRA_IVC_RESYNC_THRESHOLD ; +else if (threshold >TEGRA_IVC_RESYNC_THRESHOLD_MAX ) + threshold =TEGRA_IVC_RESYNC_THRESHOLD_MAX ; + + ivc->resync_events++; + +if (ivc->resync_count !=U8_MAX ) + ivc->resync_count++; + +/* +* A small number of fruitless calls is normal while a peer catches +* up, so they are not delayed. Still yield to any sibling thread, +* because the caller is likely to be spinning. +*/ +if (ivc->resync_count <= threshold) { +cpu_relax (); +return ; + } + + shift = ivc->resync_count -1 - threshold; +if (shift >=TEGRA_IVC_RESYNC_MAX_SHIFT ) + delay =TEGRA_IVC_RESYNC_MAX_NS ; +else + delay =min_t (u64,TEGRA_IVC_RESYNC_MIN_NS << shift, +TEGRA_IVC_RESYNC_MAX_NS ); + + ivc->resync_delayed_ns += delay; + +if (ivc->resync_can_sleep) { +might_sleep (); +fsleep (div_u64 (delay,NSEC_PER_USEC )); +return ; + } + +/* +* This can be reached while the device is suspended, where +* timekeeping may also be suspended and ktime_get() would warn and +* return a clock that is not advancing. local_clock() remains valid +* there, and tegra_bpmp_wait_request_channel_free() busy-waits the +* same way for the same reason. +*/ + deadline =local_clock () + delay; +while (local_clock () < deadline) +cpu_relax (); +} +void tegra_ivc_reset (struct tegra_ivc *ivc) { unsigned int offset = offsetof(struct tegra_ivc_header, tx.count); +tegra_ivc_resync_restart (ivc); +tegra_ivc_header_write_field (&ivc->tx.map, tx.state,TEGRA_IVC_STATE_SYNC );tegra_ivc_flush (ivc, ivc->tx.phys + offset); ivc->notify (ivc, ivc->notify_data);
@@ -546,6 +653,17 @@ int tegra_ivc_notified(struct tegra_ivc *ivc)*/ } +/* +* Read the state back to find out whether any of the branches above +* changed it, rather than tracking which branch ran. A peer that is +* making progress moves us along and resets the backoff; a peer that +* has stopped leaves the value sampled at the top of this function. +*/ +if (tegra_ivc_header_read_field (&ivc->tx.map, tx.state) != tx_state) +tegra_ivc_resync_restart (ivc); +else if (tx_state !=TEGRA_IVC_STATE_ESTABLISHED ) +tegra_ivc_resync_wait (ivc); +if (tx_state !=TEGRA_IVC_STATE_ESTABLISHED )return -EAGAIN ;
@@ -702,6 +820,20 @@ int tegra_ivc_init(struct tegra_ivc *ivc, struct device *peer, const struct iosy ivc->tx.position =0 ; ivc->rx.position =0 ; +/* +* This function assigns the fields it uses and does not zero the +* structure, so clear the backoff state explicitly. A caller that +* passes an uninitialised structure would otherwise inherit a random +* resync_can_sleep and could end up sleeping in atomic context. Both +* of the caller-settable fields are cleared here, so they have to be +* set after this function returns. +*/ + ivc->resync_can_sleep = false; + ivc->resync_threshold =0 ; + ivc->resync_count =0 ; + ivc->resync_events =0 ; + ivc->resync_delayed_ns =0 ; +return 0 ; }EXPORT_SYMBOL (tegra_ivc_init);
include/soc/tegra/ivc.h +44 -0
62 lines not on this page · raw message
---
base-commit: dc9c9c4fcb88a5660259dcef93c1ef5acd767189
change-id: 20260917-ivc-resync-backoff-b768569eb0cc
Best regards,
Signature · 1 line
0xSmash0th <[email protected]>
Thierry Reding
maintainerQuoted 32 lines · On Thu, Sep 17, 2026 at 05:20:24PM -0400, 0xSmash0th via B4 Relay wrote:
From: 0xSmash0th <[email protected]>
An untrusted peer can wedge a channel for ever: park its state word on SYNC and stop touching the ring. No path in
tegra_ivc_notified()can change the victim's rx word, sorx_statestays SYNC and the loop is absorbing rather than slow -- pass 7,651,085 is indistinguishable from pass 1. Each pass rewrites the ACK already in tx.state, re-zeroes both counters, rings the doorbell and returns-EAGAIN.The only in-tree caller,
tegra186_bpmp_channel_reset(), retries on that with no timeout, no iteration cap and nocpu_relax(), on both the probe and the resume path, so the victim burns a core at roughly 1.5M iterations per second for as long as the peer stays quiet.A peer is a service, not a VM: one untrusted guest can hold a channel to each of a rack of service partitions, and nothing here makes wedging the tenth harder than the first.
This is not a new class of concern for
ivc.c.tegra_ivc_empty()already carries an over-full check "to prevent denial of service attacks", and calls that condition "an invalid case caused by a potentially malicious peer". That defence covers the counters; the handshake has none.Apply geometric backoff to calls that make no progress, modelled on
xen_irq_lateeoi_locked(): a saturating count, a doubling delay, a hard cap, and a reset the moment the peer moves. Progress is detected by reading tx.state back rather than by tracking which branch ran, so the states from which no branch can escape are covered too. The wait is a bounded busy-wait by default becausetegra_bpmp_transfer_atomic()reaches this with interrupts disabled, and it useslocal_clock()because the resume path can run with timekeeping suspended.
Quoted 6 lines
This is a floor, not a fix. The return value is unchanged, the caller still retries for ever and probe or resume still never completes -- the spin just stops costing what other channels have to share. Bounding the loop belongs in the caller.
Assisted-by LLM
I'm not going to bother even reading all of this. There is no such thing as a hostile IVC peer. The one peer that currently uses this IVC in the Linux kernel is the BPMP and if that is somehow compromised, wedging the channel is probably the most harmless thing it can do.
Quoted 1 line
Signed-off-by 0xSmash0th <[email protected]>
Please use your full name when submitting patches to Linux.
Thierry