Skip to content

Commit 1854082

Browse files
devnexenvinodkoul
authored andcommitted
phy: apple: atc: Fix typec switch/mux leak on unbind
atcphy_probe_switch() and atcphy_probe_mux() discard the pointers returned by typec_switch_register() and typec_mux_register(). The platform driver has no .remove callback, so when the driver unbinds (e.g. via sysfs unbind) neither typec_switch_unregister() nor typec_mux_unregister() is called. The framework reference taken in typec_switch_register() (device_initialize() + device_add() in drivers/usb/typec/mux.c) is therefore never dropped and the typec_switch_dev / typec_mux_dev objects stay live forever, with their sysfs entries under the typec_mux class also left behind. A subsequent rebind cannot recreate them with the same fwnode-derived name. Save the registered handles and unregister them through devm_add_action_or_reset() so framework registration is torn down in step with the driver's other devm-managed state. While here, drop struct apple_atcphy::sw and ::mux: they were declared with the consumer-side types (typec_switch *, typec_mux *) instead of the provider-side types and were never assigned. Scope of the fix ================ This patch fixes the registration leak only. It does not close the use-after-free window that arises when a consumer that obtained a reference via fwnode_typec_switch_get() / fwnode_typec_mux_get() outlives the provider unbind: such consumers keep the underlying typec_switch_dev / typec_mux_dev alive past device_unregister(), and a later typec_switch_set() / typec_mux_set() still invokes the registered atcphy_sw_set() / atcphy_mux_set(), which dereferences the freed apple_atcphy through typec_{switch,mux}_get_drvdata(). On Apple Silicon the relevant consumers are the typec port and the cd321x controller registered by drivers/usb/typec/tipd/core.c. Cable plug / orientation events and alt-mode transitions trigger the .set callbacks via: tps6598x_interrupt() drivers/usb/typec/tipd/core.c tps6598x_handle_plug_event() tps6598x_connect()/_disconnect() typec_set_orientation() drivers/usb/typec/class.c typec_switch_set(port->sw) drivers/usb/typec/mux.c atcphy_sw_set() drivers/phy/apple/atc.c cd321x_update_work() drivers/usb/typec/tipd/core.c cd321x_typec_update_mode() typec_mux_set(cd321x->mux) drivers/usb/typec/mux.c atcphy_mux_set() drivers/phy/apple/atc.c Closing that window requires framework support for invalidating consumer-held references on provider unbind. The same consumer-survives-provider pattern has been discussed for the PHY framework [1] and is out of scope here. [1] https://lore.kernel.org/linux-phy/aZejMSJ9qqRWb2pX@google.com/ Fixes: 8e98ca1 ("phy: apple: Add Apple Type-C PHY") Signed-off-by: David Carlier <devnexen@gmail.com> Reviewed-by: Vladimir Oltean <olteanv@gmail.com> Tested-by: Joshua Peisach <jpeisach@ubuntu.com> Link: https://lkml.kernel.org/r/6ec1ed08328340db42655287afd5fa4067316b11.camel@perches.com Link: https://patch.msgid.link/20260508201958.30060-1-devnexen@gmail.com Signed-off-by: Vinod Koul <vkoul@kernel.org>
1 parent a4058c0 commit 1854082

1 file changed

Lines changed: 22 additions & 5 deletions

File tree

drivers/phy/apple/atc.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,6 @@ struct apple_atcphy {
628628

629629
struct reset_controller_dev rcdev;
630630

631-
struct typec_switch *sw;
632-
struct typec_mux *mux;
633-
634631
struct mutex lock;
635632
};
636633

@@ -2066,15 +2063,25 @@ static int atcphy_sw_set(struct typec_switch_dev *sw, enum typec_orientation ori
20662063
return 0;
20672064
}
20682065

2066+
static void atcphy_typec_switch_unregister(void *data)
2067+
{
2068+
typec_switch_unregister(data);
2069+
}
2070+
20692071
static int atcphy_probe_switch(struct apple_atcphy *atcphy)
20702072
{
2073+
struct typec_switch_dev *sw;
20712074
struct typec_switch_desc sw_desc = {
20722075
.drvdata = atcphy,
20732076
.fwnode = atcphy->dev->fwnode,
20742077
.set = atcphy_sw_set,
20752078
};
20762079

2077-
return PTR_ERR_OR_ZERO(typec_switch_register(atcphy->dev, &sw_desc));
2080+
sw = typec_switch_register(atcphy->dev, &sw_desc);
2081+
if (IS_ERR(sw))
2082+
return PTR_ERR(sw);
2083+
2084+
return devm_add_action_or_reset(atcphy->dev, atcphy_typec_switch_unregister, sw);
20782085
}
20792086

20802087
static int atcphy_mux_set(struct typec_mux_dev *mux, struct typec_mux_state *state)
@@ -2146,15 +2153,25 @@ static int atcphy_mux_set(struct typec_mux_dev *mux, struct typec_mux_state *sta
21462153
return atcphy_configure(atcphy, target_mode);
21472154
}
21482155

2156+
static void atcphy_typec_mux_unregister(void *data)
2157+
{
2158+
typec_mux_unregister(data);
2159+
}
2160+
21492161
static int atcphy_probe_mux(struct apple_atcphy *atcphy)
21502162
{
2163+
struct typec_mux_dev *mux;
21512164
struct typec_mux_desc mux_desc = {
21522165
.drvdata = atcphy,
21532166
.fwnode = atcphy->dev->fwnode,
21542167
.set = atcphy_mux_set,
21552168
};
21562169

2157-
return PTR_ERR_OR_ZERO(typec_mux_register(atcphy->dev, &mux_desc));
2170+
mux = typec_mux_register(atcphy->dev, &mux_desc);
2171+
if (IS_ERR(mux))
2172+
return PTR_ERR(mux);
2173+
2174+
return devm_add_action_or_reset(atcphy->dev, atcphy_typec_mux_unregister, mux);
21582175
}
21592176

21602177
static int atcphy_load_tunables(struct apple_atcphy *atcphy)

0 commit comments

Comments
 (0)