From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ED72A3E3C7D for ; Fri, 8 May 2026 20:20:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778271606; cv=none; b=LxmF3kJfeuUtFkW0LfLPtbZSvUGoS4b1EjXjUOweKFEy7dtmYL/Vvry+gjouOR82oWuHY1+r7v0S3fUx/FgHP42pwyOrACiEXBowx4ubYfS3RmyxzMtJRhZh8MLx4Zoh14ZdG9//qouoCEq0zD2Obw8EChpIulMje8RsDZaes7E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778271606; c=relaxed/simple; bh=OBFGSiE7H/QhxVXYn15clyP+cvz7m6tk69dlpz2LGLc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=M3Rv6n6F4rBH9Fas2CwRaTVYWQF8uRKpDvkWg8aSuNKykCZkburX7WB3wXgsC98iuKycG3uSrxyH4WgI62CHlvq3tcMLhAQEs7TqcatcFUXCeDByG6F8kSfWW7gur/klNcPOZcZA9l5aWeS2DT79f/CkorCaKt+bunAP9gA5mdw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MGSQpRZ+; arc=none smtp.client-ip=209.85.128.54 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MGSQpRZ+" Received: by mail-wm1-f54.google.com with SMTP id 5b1f17b1804b1-488ab2db91aso25936335e9.3 for ; Fri, 08 May 2026 13:20:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778271603; x=1778876403; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HP40aocTsGkB8//cxZMak90EHfdlyQYnHT+ooa5+Bl4=; b=MGSQpRZ+MY2qc/vbu6tKhMhnkO2yAs8KucsIgwYXDxGgc1lrvm+m/Qz87gdjquE4eP 2Vm4QnTnPEg067G8zN9ADUX8t1WrqyWE/Fc+YM4neN5xrLrlObuFVh5QqvOvqyZrTacN buMZ5WUXRGSmSpxYKM19HX5/6mTuYO54xjOLxYqwZRtfCA2pKktWRCnbuI5n0rMv292f z1nKd+U58BJN9Kbe5/8L0eq+UaujPia0nc+msh0bkWtvL7EdedQ5c+3X+YRtDwPy51kY 0PHjeOsd3/t6MrkVzshPJmzyriQjw/u+RttgKJpzZeGMW2uUJ2PP1kmeQpZsWRjEsvPm mspg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778271603; x=1778876403; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=HP40aocTsGkB8//cxZMak90EHfdlyQYnHT+ooa5+Bl4=; b=URcFzjCZkTGtLr/TR4u81SMP7/KDtAnDaJgyeLhY+NlJilNLUOMGmue/EVnn9CCF/G LmO44T/R424kgu6ISkXVL0oVlV9AXYGIPoSPXWgHGsI9lFImnYBCH1uNpopXYcu7hj7M tV9DJ7jPJjNVuKM3Hj87mkGGMrkXJKudv7GCqM7aRTTqrwblJozXejkrzqYisEttPDKe bxnTbsdvIddXc5jDA30ivCqP8J4p03zhxoxuN8qVK5jJ/BDytlw5624NyEDN4/Td19Pf 0pO4KV2U8dwRzjE/xgeGuRL4VFFJoX2X8uyArswoWB7AHTv6BcjS4FO3Wmt/WZctrayE l8xg== X-Forwarded-Encrypted: i=1; AFNElJ8tMDPYqZ9Nnuyp1zmfwQRmX+CLond9NMwWx5XKkbyqaTc+YCjnLSXu7oU7QmiaNF2ZaxSREBpXzZ6DPbs=@vger.kernel.org X-Gm-Message-State: AOJu0YynaG03AaiWq4uTDYGPV4mvbL31k7evX/Y8lhVmObBvyp4DtiIK WUsCuXOYXqypPoc1Js/kd/NAFE70MRkZjBiyxBRgUbHU9rg8APbymeon X-Gm-Gg: AeBDieuqVHVvgOBgyJOwKb5wHbKVlRNMS6m7KbDYxdnkTLEaeOPTBbMhdlSrhVQJKy5 pUb0uk2tcW9Cf859xqruQY4QVFnUFJPCVbLvPlauAvCeD39vwjz8tapNOqB78QEwgbuDyb7haVS vgzhVmK7EVQ8tVUrsSTrB+Yb4DP2zXNiryqi5kJkl9j+SKa5KIRCJPNS0joljGFAftWNYksxrwd dn9i1s2F8IBIRFLPnq7au0iJ8QqXfCkHA/TYSooWVXvFbJq0kVQLNwam1DvcVIuJHEZ76VdemdQ rwAoKOUFelnSBCVEkRe06Ntl2WQqrMc1LmoHQvNf1EpB+RjStt9CyblIj2Dqvp+aud7xpSuu/Xs HXptj3qJs+ZTVu5vyDHEOQc/IoONH2A0Gh38I/9ZJniOPbrIIMtsRDliXbPDo3+9G0PUAa3mBKm pPUfDZ5e5XjWc1Q61EB9JXtRuqV+TM7SPP83sxPRjrxzl/HNiGXczIzka3cBOAUOcRWVKQt3bH3 dXVxj4e9EY= X-Received: by 2002:a05:600c:3151:b0:48a:7605:751e with SMTP id 5b1f17b1804b1-48e51e0837amr215462255e9.4.1778271603054; Fri, 08 May 2026 13:20:03 -0700 (PDT) Received: from dohko.chello.ie (188-141-5-72.dynamic.upc.ie. [188.141.5.72]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48e6dd328c2sm15825895e9.6.2026.05.08.13.20.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 May 2026 13:20:02 -0700 (PDT) From: David Carlier To: sven@kernel.org, j@jannau.net, neal@gompa.dev, vkoul@kernel.org, neil.armstrong@linaro.org, marcan@marcan.st, p.zabel@pengutronix.de Cc: olteanv@gmail.com, asahi@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-phy@lists.infradead.org, linux-kernel@vger.kernel.org, David Carlier Subject: [PATCH v3] phy: apple: atc: Fix typec switch/mux leak on unbind Date: Fri, 8 May 2026 21:19:58 +0100 Message-ID: <20260508201958.30060-1-devnexen@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260507163746.108086-1-devnexen@gmail.com> References: <20260507163746.108086-1-devnexen@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: 8e98ca1e74db ("phy: apple: Add Apple Type-C PHY") Signed-off-by: David Carlier Reviewed-by: Vladimir Oltean --- drivers/phy/apple/atc.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/drivers/phy/apple/atc.c b/drivers/phy/apple/atc.c index e9d106f135c5..4156fabad742 100644 --- a/drivers/phy/apple/atc.c +++ b/drivers/phy/apple/atc.c @@ -628,9 +628,6 @@ struct apple_atcphy { struct reset_controller_dev rcdev; - struct typec_switch *sw; - struct typec_mux *mux; - struct mutex lock; }; @@ -2066,15 +2063,25 @@ static int atcphy_sw_set(struct typec_switch_dev *sw, enum typec_orientation ori return 0; } +static void atcphy_typec_switch_unregister(void *data) +{ + typec_switch_unregister(data); +} + static int atcphy_probe_switch(struct apple_atcphy *atcphy) { + struct typec_switch_dev *sw; struct typec_switch_desc sw_desc = { .drvdata = atcphy, .fwnode = atcphy->dev->fwnode, .set = atcphy_sw_set, }; - return PTR_ERR_OR_ZERO(typec_switch_register(atcphy->dev, &sw_desc)); + sw = typec_switch_register(atcphy->dev, &sw_desc); + if (IS_ERR(sw)) + return PTR_ERR(sw); + + return devm_add_action_or_reset(atcphy->dev, atcphy_typec_switch_unregister, sw); } 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 return atcphy_configure(atcphy, target_mode); } +static void atcphy_typec_mux_unregister(void *data) +{ + typec_mux_unregister(data); +} + static int atcphy_probe_mux(struct apple_atcphy *atcphy) { + struct typec_mux_dev *mux; struct typec_mux_desc mux_desc = { .drvdata = atcphy, .fwnode = atcphy->dev->fwnode, .set = atcphy_mux_set, }; - return PTR_ERR_OR_ZERO(typec_mux_register(atcphy->dev, &mux_desc)); + mux = typec_mux_register(atcphy->dev, &mux_desc); + if (IS_ERR(mux)) + return PTR_ERR(mux); + + return devm_add_action_or_reset(atcphy->dev, atcphy_typec_mux_unregister, mux); } static int atcphy_load_tunables(struct apple_atcphy *atcphy) -- 2.53.0