diff mbox series

[1/2] usb: typec: Only attempt to link USB ports if there is fwnode

Message ID 20220124090228.41396-2-heikki.krogerus@linux.intel.com (mailing list archive)
State Accepted
Commit 7817adb03cfb52ebb5bdb25fd9fc8f683a1a09d9
Headers show
Series usb: typec: Two fixes | expand

Commit Message

Heikki Krogerus Jan. 24, 2022, 9:02 a.m. UTC
The code that creates the links to the USB ports attached to
a connector inside the system assumed that the ACPI nodes
(fwnodes) always exist for the connectors, but it can not do
that.

There is no guarantee that every USB Type-C connector has
ACPI device node representing it in the ACPI tables, and
even if there are the nodes in the ACPI tables, the _STA
method in those nodes may still return 0 (which means the
device does not exist from ACPI PoW).

This fixes NULL pointer dereference that happens if the
nodes are missing.

Reported-and-tested-by: Robert Święcki <robert@swiecki.net>
Reported-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>
Fixes: 730b49aac426 ("usb: typec: port-mapper: Convert to the component framework")
Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
---
 drivers/usb/typec/port-mapper.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

Comments

Marc Zyngier Jan. 24, 2022, 4:26 p.m. UTC | #1
On 2022-01-24 09:02, Heikki Krogerus wrote:
> The code that creates the links to the USB ports attached to
> a connector inside the system assumed that the ACPI nodes
> (fwnodes) always exist for the connectors, but it can not do
> that.
> 
> There is no guarantee that every USB Type-C connector has
> ACPI device node representing it in the ACPI tables, and
> even if there are the nodes in the ACPI tables, the _STA
> method in those nodes may still return 0 (which means the
> device does not exist from ACPI PoW).
> 
> This fixes NULL pointer dereference that happens if the
> nodes are missing.
> 
> Reported-and-tested-by: Robert Święcki <robert@swiecki.net>
> Reported-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>
> Fixes: 730b49aac426 ("usb: typec: port-mapper: Convert to the
> component framework")
> Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
> ---
>  drivers/usb/typec/port-mapper.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/typec/port-mapper.c 
> b/drivers/usb/typec/port-mapper.c
> index 07d307418b470..b6e0c6acc628c 100644
> --- a/drivers/usb/typec/port-mapper.c
> +++ b/drivers/usb/typec/port-mapper.c
> @@ -56,6 +56,9 @@ int typec_link_ports(struct typec_port *con)
>  {
>  	struct each_port_arg arg = { .port = con, .match = NULL };
> 
> +	if (!has_acpi_companion(&con->dev))
> +		return 0;
> +
>  	bus_for_each_dev(&acpi_bus_type, NULL, &arg, typec_port_match);
> 
>  	/*
> @@ -74,5 +77,6 @@ int typec_link_ports(struct typec_port *con)
> 
>  void typec_unlink_ports(struct typec_port *con)
>  {
> -	component_master_del(&con->dev, &typec_aggregate_ops);
> +	if (has_acpi_companion(&con->dev))
> +		component_master_del(&con->dev, &typec_aggregate_ops);
>  }

This fixes an ugly Oops on the Apple M1. So FWIW:

Acked-by: Marc Zyngier <maz@kernel.org>
Tested-by: Marc Zyngier <maz@kernel.org>

         M.
diff mbox series

Patch

diff --git a/drivers/usb/typec/port-mapper.c b/drivers/usb/typec/port-mapper.c
index 07d307418b470..b6e0c6acc628c 100644
--- a/drivers/usb/typec/port-mapper.c
+++ b/drivers/usb/typec/port-mapper.c
@@ -56,6 +56,9 @@  int typec_link_ports(struct typec_port *con)
 {
 	struct each_port_arg arg = { .port = con, .match = NULL };
 
+	if (!has_acpi_companion(&con->dev))
+		return 0;
+
 	bus_for_each_dev(&acpi_bus_type, NULL, &arg, typec_port_match);
 
 	/*
@@ -74,5 +77,6 @@  int typec_link_ports(struct typec_port *con)
 
 void typec_unlink_ports(struct typec_port *con)
 {
-	component_master_del(&con->dev, &typec_aggregate_ops);
+	if (has_acpi_companion(&con->dev))
+		component_master_del(&con->dev, &typec_aggregate_ops);
 }