From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 3A4083E9585 for ; Thu, 7 May 2026 16:37:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778171873; cv=none; b=ss8aSyYaH6OJle8d7hLObgmUsKp/LDshkrjPKXFz+TMcnOsVR6Qn1bMIbPk+peXdu7/X2TK1I0BYMeZ0uohdEvPoB70GQSteSoF8mWZsQajuRoaJ5/XhscJfJ7BbcvsFwj6yR4iiEE0+m+Iufg/LP5399Za5az8CPFKDu0YttUU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778171873; c=relaxed/simple; bh=LgRf70lT96wFO857GUhZpj1XP/ybMQwFCMTG3W98hCI=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=C5HNoWwTlb/omp25iUEX7KzQcuNU5csK+K6txpsIOGjHT0Om00Z04Ikm8mttp+18UEOZl2vSSMDRusXxoVvDkhvMP80Dj6FiuEUofMjvQzCQ2LdfewgdszomijVzsHxjb+wIlRnj6AqX4Cz9YsMbRc+It9l/fSaO/MkxL33nYcM= 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=moJ2/nEw; arc=none smtp.client-ip=209.85.128.51 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="moJ2/nEw" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-4896c22fcbaso10943975e9.0 for ; Thu, 07 May 2026 09:37:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1778171871; x=1778776671; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=pGCU5B8lEdAiwWeUe+DGhLUkoxpExSxBEl0W5p2Dn8g=; b=moJ2/nEwhsYnvQ7iMWg3z3LhM+iW5plzHgG9iQtwTe6XLg5txz7EC3IcxaZhLOG1mo h8Fsbw/FdOZmhiazcHAyXN3v0bw7TNFnV3CnmxnaL+DAZTcD6Hb3uplaAnoHiZLQN8YU gjXbzIFEyeLM7zKsMaKJvvDVQIcgglqAWWacwLaAAholm0hzNGIjHDwR3xdZxn6Cpnrx 24YYPQnrDltqKmOV8cc0y9WjfMbHPvsldxQ948pRou6bm7fpWaDLLE22aTiK9e19UJYB 784DOP5aDj5xpehWrnLr1rOnzEs6OckKyLSU1vGHbvmGw5/EFXSLTXsndgMgssJTInNn sPhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778171871; x=1778776671; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=pGCU5B8lEdAiwWeUe+DGhLUkoxpExSxBEl0W5p2Dn8g=; b=FciHZOL3qvGohlKUTRz9Slq40GBMhmFeT8FIhoABUd51COVGyrtGfj+/ubVWpIY+xw /hVMDDE3rnRUVqC26Af3LOFzFFHl1EAlfbDbp5/S2YQyGr6pwV1JC0z5ZrVahPucytX+ S8265iu4L3FZf4A1bUuRbj5j8ReFBemzY/78pvoYGCv9EJNdtvl/Sq/YPADAP1V2w3e6 CXODp3CuIMNtculaIMSkEt6xKWMMnblU/h/akSBpEFgIuNvrxrWwW2ihChm1C8TspHt1 mxfAvRF3riCsOoX51Gy66HhRTrynzHxnJm4z7RYe/UVOwTWFt38qXjyVPO+bs9I1uDdZ pfxg== X-Forwarded-Encrypted: i=1; AFNElJ9eNkjZ8UR1yyuHZ1rYzxL9i/3q5aywgAICtLt0vON+GsocMzl+I9IS6zmNwWJjJLqOiBLuJfjl3LCq0iQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yy6j1Jm6r0rMZAiYDLTmOzImgtk0NpV3qkiVpO2BM+WeF2zu1/K fmgoF8X1ncMjpa7FUd9J0C/vVLoz8cAsa0P5pJa8PX7oPs9/Wm/CAieN X-Gm-Gg: AeBDieuY6ivbGxF0XeFGYtA39gvfuuKv6Hu1c2FNpGDXk42TGRszPQVeEfJ0TJKtSxf Qb1TlFt1iQW1cNH2BX2n0REb8r0kFVemQ+ag/1sTpz2Jia+2g2xiQ2Rdq0g0o53JAAdT9Z92AHy fMHPqAHoBo/Z1a7t97ap9bCOhzKRxdBvXs+NNbnm0/gtY9PbY7Pxl3nxTTkTPPEhOPdGSwE8b4J LphaeUI/DIgTF9cyzjuEDzudyPcm2b1bH28S7puqXNwmhtaXBVE7VKKr8r7R3e0okNw4FbX0CAX IAKlvoqqQ4KnxeB35X3ViSjFqEuQuG6UXvKC32OifoZ7tebvEbLnTq8zI6MdDcOYD9RmURvVTJ/ JZPJyMulnlgy/s8YTK1fsWZys8wcalSW8NT1OVKUGyv4C/gFGSuly7feIvMHe9w8g2ijWm3DlRD sdn1yHLj00YcFCPFcXOmMYCXrmYBxO0HD9K+vjg+qRjJzUdfkKr//5iFULoYaMvD/vwjJrlBOdA n+Gf8JPxd6mV5qAY5Pehg== X-Received: by 2002:a05:600c:c11c:b0:488:c078:bfda with SMTP id 5b1f17b1804b1-48e51f4835dmr101988665e9.26.1778171870182; Thu, 07 May 2026 09:37:50 -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-48e6428e746sm590165e9.0.2026.05.07.09.37.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 May 2026 09:37:49 -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 v2] phy: apple: atc: Fix typec switch/mux leak on unbind Date: Thu, 7 May 2026 17:37:46 +0100 Message-ID: <20260507163746.108086-1-devnexen@gmail.com> X-Mailer: git-send-email 2.53.0 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 --- 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