sv6160: fix the host-rebind guard latching before it could fail

Found in review of 53493a1.  The one-shot guard was set BEFORE the two steps
that can fail:

    sv6160_host_rebound = true;
    rb = kzalloc(sizeof(*rb), GFP_ATOMIC);
    if (!rb)
            return;                 /* latched, nothing queued */
    if (!try_module_get(THIS_MODULE)) {
            kfree(rb);
            return;                 /* latched, nothing queued */
    }

Either failure left the guard latched with no work queued, and the only release
was sv6160_rebind_note_probe_ok() from a SUCCESSFUL probe -- which is precisely
what cannot happen in the state this mechanism exists for, where the bus is
wedged and probe fails -16 before drv->probe is even entered.  So one transient
allocation failure would have silently disabled the escalation for the rest of
the module's life, logging "already re-created once" forever while never
actually re-binding anything.  Now claimed with cmpxchg and released again on
every path that does not queue the work.

Three more from the same review:

  - The guard was a plain bool with a non-atomic test-and-set.  Two callers on
    different CPUs could both see it clear and queue a rebind each; the second
    teardown would then destroy the host the first had just re-created.  It is
    an atomic_t claimed with cmpxchg now.

  - The re-arm from probe() raced the attempt it belonged to.  mmc_rescan runs
    asynchronously, so the new probe can land while device_attach() is still in
    flight and clear the guard from under it.  The release moves into the work
    function, after the attempt finishes, and happens only when the re-bind
    actually succeeded -- a failing one stays latched rather than looping.
    sv6160_rebind_note_probe_ok() is gone.

  - GFP_ATOMIC was both unnecessary and the worst flag for the moment: every
    caller is sleepable (each holds sc->lock, or is a work item or a syscall),
    and atomic reserves are exactly what is scarce when a wifi driver is dying.
    GFP_KERNEL, and the "safe from any context" comment corrected to say
    process context.

Also: the failure path now prints the sysfs bind recipe, since a re-bind that
does not bind leaves the card absent with no automatic way back; the
"not looping" warning is ratelimited (the caller resets its counter every 3 bus
recoveries, so it could stream); and a note records that this pair is what
device_reprobe() does, and that module_put() at the end of a work item leaves a
narrow window where rmmod can free the text the epilogue is still running in.

Verified on hardware: triggered the rebind twice in a row via debugfs.  Both
re-created the host and reported OK, with zero spurious "already tried"
warnings -- i.e. the guard is released after a success, which is what the racy
probe re-arm only achieved by accident.  Link healthy afterwards, counters 1:1.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JSNniou44nN9EFGqTmZ6my
This commit is contained in:
Paul KilarandClaude Opus 5 committed 2026-09-17 23:43:05 -04:00
1 parent d79c78015c
commit a7dfd19029
3 files changed
+57 -26

No files matched your search

@@ -1296,7 +1296,6 @@ void sv6160_start_queues(struct sv6160_softc *sc);
* that owns our card. Takes the sdio_func, not the softc, because the wedge
* that most needs it is the one where PROBE fails -16 and no softc exists. */
void sv6160_rebind_sdio_host(struct sdio_func *func);
void sv6160_rebind_note_probe_ok(void);
/* PM Support */
void sv6160_notify_pm(struct sv6160_softc *sc, int wakeup);
@@ -658,6 +658,23 @@ struct sv6160_host_rebind {
struct device *host_dev;
};
/* The guard lives at file scope rather than in the softc: the wedge that most
* needs this mechanism is the one where there is no softc at all.
0 = a rebind may be started, 1 = one is in flight or has already been tried.
*
* Claimed with cmpxchg, not a plain test-and-set: two callers on different CPUs
* (the RX worker and the TX path, say) can otherwise both see it clear and
* queue a rebind each. That is not a memory bug -- each gets its own work item
* with balanced references -- but the second teardown would destroy the host the
* first had just successfully re-created.
*
* Released by sv6160_host_rebind_fn() only when the re-bind actually SUCCEEDED,
* so a future wedge gets a fresh attempt while a failing one does not loop. It
* is deliberately NOT released from probe(): mmc_rescan runs asynchronously, so
* the new probe can land while device_attach() is still in flight and would
* clear the guard from under the attempt that is still running. */
static atomic_t sv6160_host_rebound = ATOMIC_INIT(0);
static void sv6160_host_rebind_fn(struct work_struct *work)
{
struct sv6160_host_rebind *rb =
@@ -666,29 +683,40 @@ static void sv6160_host_rebind_fn(struct work_struct *work)
int ret;
dev_warn(dev, "sv6160: re-creating the SDIO host to clear a wedged bus\n");
/* This pair is what device_reprobe() does; spelled out because the two
* halves are separately interesting when reading a wedge log. */
device_release_driver(dev); /* takes this driver down with it */
ret = device_attach(dev);
dev_warn(dev, "sv6160: SDIO host re-bind %s (%d)\n",
ret > 0 ? "OK" : "FAILED", ret);
if (ret > 0) {
dev_warn(dev, "sv6160: SDIO host re-bind OK\n");
/* Released only here, after the attempt has finished, so a later
* wedge gets a fresh one. A failed re-bind stays latched: there
* is nothing to gain from retrying a bind that just refused. */
atomic_set(&sv6160_host_rebound, 0);
} else {
/* ret == 0 also covers -EPROBE_DEFER absorbed into the deferred
* list, in which case it may still bind by itself shortly. */
dev_err(dev,
"sv6160: SDIO host re-bind did not bind (%d) - the card is now absent. Recover with: echo %s > /sys/bus/platform/drivers/dwmmc_rockchip/bind\n",
ret, dev_name(dev));
}
put_device(dev);
kfree(rb);
/* Note: this wakes an rmmod already blocked on the refcount, which can
* then free this module's text while this function's epilogue is still
* executing. The window is a few instructions and the return lands in
* core-kernel workqueue code; there is no module_put_and_*_exit() helper
* for a work item. */
module_put(THIS_MODULE);
}
/* One shot per module load, reset by a probe that succeeds. The guard has to
* live here rather than in the softc: the wedge that most needs this is the one
* where PROBE itself fails -16, and at that point there is no softc at all. */
static bool sv6160_host_rebound;
void sv6160_rebind_note_probe_ok(void)
{
sv6160_host_rebound = false;
}
/* Ask for the host to be re-created. Safe from any context, and safe to call
* with no softc. Does nothing if the topology is not what we expect rather
* than guessing at a device. */
/* Ask for the host to be re-created. Process context only (every caller either
* holds sc->lock or is a work item / syscall), and safe to call with no softc.
* Does nothing if the topology is not what we expect rather than guessing at a
* device. */
void sv6160_rebind_sdio_host(struct sdio_func *func)
{
struct sv6160_host_rebind *rb;
@@ -701,23 +729,31 @@ void sv6160_rebind_sdio_host(struct sdio_func *func)
host_dev = func->card->host->parent;
if (!host_dev)
return;
if (sv6160_host_rebound) {
dev_warn(&func->dev,
"SDIO host already re-created once - not looping on it\n");
if (atomic_cmpxchg(&sv6160_host_rebound, 0, 1)) {
dev_warn_ratelimited(&func->dev,
"SDIO host re-create already tried - not looping on it\n");
return;
}
sv6160_host_rebound = true;
rb = kzalloc(sizeof(*rb), GFP_ATOMIC);
/* Release the claim again on any path that does not actually queue the
* work. Latching it here and then failing would silently disable the
* escalation for the rest of this module's life, because the only other
* release is from a successful re-bind -- which is exactly what cannot
* happen while the bus is wedged. */
rb = kzalloc(sizeof(*rb), GFP_KERNEL);
if (!rb)
return;
goto unclaim;
if (!try_module_get(THIS_MODULE)) {
kfree(rb);
return;
goto unclaim;
}
INIT_WORK(&rb->work, sv6160_host_rebind_fn);
rb->host_dev = get_device(host_dev);
queue_work(system_unbound_wq, &rb->work);
return;
unclaim:
atomic_set(&sv6160_host_rebound, 0);
}
static void sv6160_recover_work_fn(struct work_struct *work)
@@ -334,10 +334,6 @@ static int sv6160_probe(struct sdio_func *func,
goto err_disable;
}
/* Far enough in that the host is demonstrably alive: re-arm the
* one-shot so a later wedge can be rescued too. */
sv6160_rebind_note_probe_ok();
/* Allocate mac80211 hardware struct */
hw = ieee80211_alloc_hw(sizeof(struct sv6160_softc), &sv6160_ops);
if (!hw) {