When calling tcpm_reset_port() after destroying the worker and the associated hrtimers, the TCPM implementation may still send some TCPM events and call back into the TCPM core and encounter a NULL port->wq. Move the power_supply_put_system_batteries() & tcpm_reset_port() earlier, since it's the last function that calls into TCPM callback. Cc: stable@vger.kernel.org Fixes: 98d00deb6ae4 ("usb: typec: tcpm: fix use-after-free of the kthread worker on port unregister") Reported-by: Sashiko Closes: https://lore.kernel.org/all/sashiko-outbox-224916@kernel.org/ Signed-off-by: Neil Armstrong --- drivers/usb/typec/tcpm/tcpm.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c index d0abe8100742..5e68ce2ea1ac 100644 --- a/drivers/usb/typec/tcpm/tcpm.c +++ b/drivers/usb/typec/tcpm/tcpm.c @@ -9025,6 +9025,10 @@ void tcpm_unregister_port(struct tcpm_port *port) int i; port->registered = false; + + power_supply_put_system_batteries(port->fixed_batt, port->fixed_batt_cnt); + tcpm_reset_port(port); + kthread_destroy_worker(port->wq); port->wq = NULL; @@ -9033,9 +9037,6 @@ void tcpm_unregister_port(struct tcpm_port *port) hrtimer_cancel(&port->vdm_state_machine_timer); hrtimer_cancel(&port->state_machine_timer); - power_supply_put_system_batteries(port->fixed_batt, port->fixed_batt_cnt); - tcpm_reset_port(port); - tcpm_port_unregister_pd(port); for (i = 0; i < ARRAY_SIZE(port->port_altmode); i++) -- 2.34.1