usb: ucsi: 3 bug fixes
Subsystems (3)
v3 cover usb: ucsi: 3 bug fixes
Hi Greg, Heikki,
Here is v3 of my ucsi bugfix series.
Changes in v3:
- Assign connector[i].index before calling ucsi_register_port() instead of passing i to ucsi_register_port()
- Add Link and Reviewed-by tags to commitmsg
Changes in v2:
- Delay setting ucsi->ntfy in ucsi_init() instead of adding a NULL pointer check to ucsi_connector_change()
Regards,
Hans
Hans de Goede (3):
usb: ucsi: Fix NULL pointer deref in ucsi_connector_change()
usb: ucsi: Fix ucsi->connector race
usb: ucsi_acpi: Increase the command completion timeout
drivers/usb/typec/ucsi/ucsi.c | 33 ++++++++++++++---------------- drivers/usb/typec/ucsi/ucsi_acpi.c | 2 +- 2 files changed, 16 insertions(+), 19 deletions(-)
Signature · 1 line
2.39.1
PATCH v3 1/3 usb: ucsi: Fix NULL pointer deref in ucsi_connector_change()
When ucsi_init() fails, ucsi->connector is NULL, yet in case of ucsi_acpi we may still get events which cause the ucs_acpi code to call ucsi_connector_change(), which then derefs the NULL ucsi->connector pointer.
Fix this by not setting ucsi->ntfy inside ucsi_init() until ucsi_init() has succeeded, so that ucsi_connector_change() ignores the events because UCSI_ENABLE_NTFY_CONNECTOR_CHANGE is not set in the ntfy mask.
---
Changes in v2:
- Delay setting ucsi->ntfy in ucsi_init() instead of adding a NULL pointer check to ucsi_connector_change()
Changes in v3:
- Add Link tag to commitmsg
--- drivers/usb/typec/ucsi/ucsi.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
drivers/usb/typec/ucsi/ucsi.c +6 -5
@@ -1205,7 +1205,7 @@ static int ucsi_register_port(struct ucsi *ucsi, int index)static int ucsi_init (struct ucsi *ucsi) {struct ucsi_connector *con; -u64 command; +u64 command, ntfy;int ret;int i;
@@ -1217,8 +1217,8 @@ static int ucsi_init(struct ucsi *ucsi) }/* Enable basic notifications */ - ucsi->ntfy =UCSI_ENABLE_NTFY_CMD_COMPLETE |UCSI_ENABLE_NTFY_ERROR ; - command =UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy; + ntfy =UCSI_ENABLE_NTFY_CMD_COMPLETE |UCSI_ENABLE_NTFY_ERROR ; + command =UCSI_SET_NOTIFICATION_ENABLE | ntfy; ret =ucsi_send_command (ucsi, command,NULL ,0 );if (ret <0 ) goto err_reset;
@@ -1250,12 +1250,13 @@ static int ucsi_init(struct ucsi *ucsi) }/* Enable all notifications */ - ucsi->ntfy =UCSI_ENABLE_NTFY_ALL ; - command =UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy; + ntfy =UCSI_ENABLE_NTFY_ALL ; + command =UCSI_SET_NOTIFICATION_ENABLE | ntfy; ret =ucsi_send_command (ucsi, command,NULL ,0 );if (ret <0 ) goto err_unregister; + ucsi->ntfy = ntfy;return 0 ; err_unregister:
Signature · 1 line
2.39.1
PATCH v3 2/3 usb: ucsi: Fix ucsi->connector race
In reply to Hans de Goedeucsi_init() which runs from a workqueue sets ucsi->connector and on an error will clear it again.
ucsi->connector gets dereferenced by ucsi_resume(), this checks for ucsi->connector being NULL in case ucsi_init() has not finished yet; or in case ucsi_init() has failed.
ucsi_init() setting ucsi->connector and then clearing it again on an error creates a race where the check in ucsi_resume() may pass, only to have ucsi->connector free-ed underneath it when ucsi_init() hits an error.
Fix this race by making ucsi_init() store the connector array in a local variable and only assign it to ucsi->connector on success.
---
Changes in v3:
- Assign connector[i].index before calling ucsi_register_port() instead of passing i to ucsi_register_port()
--- drivers/usb/typec/ucsi/ucsi.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-)
drivers/usb/typec/ucsi/ucsi.c +9 -13
@@ -1039,9 +1039,8 @@ static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)return NULL ; } -static int ucsi_register_port (struct ucsi *ucsi,int index) +static int ucsi_register_port (struct ucsi *ucsi,struct ucsi_connector *con) { -struct ucsi_connector *con = &ucsi->connector[index];struct typec_capability *cap = &con->typec_cap;enum typec_accessory *accessory = cap->accessory;enum usb_role u_role =USB_ROLE_NONE ;
@@ -1062,7 +1061,6 @@ static int ucsi_register_port(struct ucsi *ucsi, int index)init_completion (&con->complete);mutex_init (&con->lock);INIT_LIST_HEAD (&con->partner_tasks); - con->num = index +1 ; con->ucsi = ucsi; cap->fwnode =ucsi_find_fwnode (con);
@@ -1204,7 +1202,7 @@ static int ucsi_register_port(struct ucsi *ucsi, int index)*/ static int ucsi_init (struct ucsi *ucsi) { -struct ucsi_connector *con; +struct ucsi_connector *con, *connector;u64 command, ntfy;int ret;int i;
@@ -1235,16 +1233,16 @@ static int ucsi_init(struct ucsi *ucsi) }/* Allocate the connectors. Released in ucsi_unregister() */ - ucsi->connector =kcalloc (ucsi->cap.num_connectors +1 , -sizeof (*ucsi->connector),GFP_KERNEL ); -if (!ucsi->connector) { + connector =kcalloc (ucsi->cap.num_connectors +1 ,sizeof (*connector),GFP_KERNEL ); +if (!connector) { ret = -ENOMEM ; goto err_reset; }/* Register all connectors */ for (i =0 ; i < ucsi->cap.num_connectors; i++) { - ret =ucsi_register_port (ucsi, i); + connector[i].num = i +1 ; + ret =ucsi_register_port (ucsi, &connector[i]);if (ret) goto err_unregister; }
@@ -1256,11 +1254,12 @@ static int ucsi_init(struct ucsi *ucsi)if (ret <0 ) goto err_unregister; + ucsi->connector = connector; ucsi->ntfy = ntfy;return 0 ; err_unregister: -for (con = ucsi->connector; con->port; con++) { +for (con = connector; con->port; con++) {ucsi_unregister_partner (con);ucsi_unregister_altmodes (con,UCSI_RECIPIENT_CON );ucsi_unregister_port_psy (con);
@@ -1269,10 +1268,7 @@ static int ucsi_init(struct ucsi *ucsi)typec_unregister_port (con->port); con->port =NULL ; } - -kfree (ucsi->connector); - ucsi->connector =NULL ; - +kfree (connector); err_reset:memset (&ucsi->cap,0 ,sizeof (ucsi->cap));ucsi_reset_ppm (ucsi);
Signature · 1 line
2.39.1
Re: [PATCH v3 2/3] usb: ucsi: Fix ucsi->connector race
Quoted 12 lines · Hans de Goede
ucsi_init()which runs from a workqueue setsucsi->connectorand on an error will clear it again.
ucsi->connectorgets dereferenced byucsi_resume(), this checks forucsi->connectorbeing NULL in caseucsi_init()has not finished yet; or in caseucsi_init()has failed.
ucsi_init()settingucsi->connectorand then clearing it again on an error creates a race where the check inucsi_resume()may pass, only to haveucsi->connectorfree-ed underneath it whenucsi_init()hits an error.
Quoted 6 lines
Fix this race by making
ucsi_init()store the connector array in a local variable and only assign it toucsi->connectoron success.Fixes bdc62f2bae8f ("usb: typec: ucsi: Simplified registration and I/O API")Signed-off-by Hans de Goede <[email protected]>
Quoted 89 lines
---
Changes in v3:
- Assign connector[i].index before callingucsi_register_port()instead of passing i toucsi_register_port()--- drivers/usb/typec/ucsi/ucsi.c | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-)drivers/usb/typec/ucsi/ucsi.c +9
-13@@ -1039,9 +1039,8 @@ static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)return NULL ; } -static int ucsi_register_port (struct ucsi *ucsi,int index) +static int ucsi_register_port (struct ucsi *ucsi,struct ucsi_connector *con) { -struct ucsi_connector *con = &ucsi->connector[index];struct typec_capability *cap = &con->typec_cap;enum typec_accessory *accessory = cap->accessory;enum usb_role u_role =USB_ROLE_NONE ;@@ -1062,7 +1061,6 @@ static int ucsi_register_port(struct ucsi *ucsi, int index)init_completion (&con->complete);mutex_init (&con->lock);INIT_LIST_HEAD (&con->partner_tasks); - con->num = index +1 ; con->ucsi = ucsi; cap->fwnode =ucsi_find_fwnode (con);@@ -1204,7 +1202,7 @@ static int ucsi_register_port(struct ucsi *ucsi, int index)*/ static int ucsi_init (struct ucsi *ucsi) { -struct ucsi_connector *con; +struct ucsi_connector *con, *connector;u64 command, ntfy;int ret;int i;@@ -1235,16 +1233,16 @@ static int ucsi_init(struct ucsi *ucsi) }/* Allocate the connectors. Released in ucsi_unregister() */ - ucsi->connector =kcalloc (ucsi->cap.num_connectors +1 , -sizeof (*ucsi->connector),GFP_KERNEL ); -if (!ucsi->connector) { + connector =kcalloc (ucsi->cap.num_connectors +1 ,sizeof (*connector),GFP_KERNEL ); +if (!connector) { ret = -ENOMEM ; goto err_reset; }/* Register all connectors */ for (i =0 ; i < ucsi->cap.num_connectors; i++) { - ret =ucsi_register_port (ucsi, i); + connector[i].num = i +1 ; + ret =ucsi_register_port (ucsi, &connector[i]);if (ret) goto err_unregister; }@@ -1256,11 +1254,12 @@ static int ucsi_init(struct ucsi *ucsi)if (ret <0 ) goto err_unregister; + ucsi->connector = connector; ucsi->ntfy = ntfy;return 0 ; err_unregister: -for (con = ucsi->connector; con->port; con++) { +for (con = connector; con->port; con++) {ucsi_unregister_partner (con);ucsi_unregister_altmodes (con,UCSI_RECIPIENT_CON );ucsi_unregister_port_psy (con);@@ -1269,10 +1268,7 @@ static int ucsi_init(struct ucsi *ucsi)typec_unregister_port (con->port); con->port =NULL ; } - -kfree (ucsi->connector); - ucsi->connector =NULL ; - +kfree (connector); err_reset:memset (&ucsi->cap,0 ,sizeof (ucsi->cap));ucsi_reset_ppm (ucsi);Signature · 1 line
2.39.1
Signature · 1 line
heikki
Heikki Krogerus
maintainerRe: [PATCH v3 2/3] usb: ucsi: Fix ucsi->connector race
In reply to Hans de GoedeQuoted 15 lines · Hans de Goede
ucsi_init()which runs from a workqueue setsucsi->connectorand on an error will clear it again.
ucsi->connectorgets dereferenced byucsi_resume(), this checks forucsi->connectorbeing NULL in caseucsi_init()has not finished yet; or in caseucsi_init()has failed.
ucsi_init()settingucsi->connectorand then clearing it again on an error creates a race where the check inucsi_resume()may pass, only to haveucsi->connectorfree-ed underneath it whenucsi_init()hits an error.Fix this race by making
ucsi_init()store the connector array in a local variable and only assign it toucsi->connectoron success.
Quoted 7 lines
Fixes bdc62f2bae8f ("usb: typec: ucsi: Simplified registration and I/O API")Signed-off-by Hans de Goede <[email protected]>---
Changes in v3:
- Assign connector[i].index before callingucsi_register_port()instead of passing i toucsi_register_port()
You forgot to rebase this. It does not apply.
thanks,
Signature · 1 line
heikki
Re: [PATCH v3 2/3] usb: ucsi: Fix ucsi->connector race
Hi,
Quoted 24 lines · Heikki Krogerus
Quoted 15 lines · Hans de Goede
ucsi_init()which runs from a workqueue setsucsi->connectorand on an error will clear it again.
ucsi->connectorgets dereferenced byucsi_resume(), this checks forucsi->connectorbeing NULL in caseucsi_init()has not finished yet; or in caseucsi_init()has failed.
ucsi_init()settingucsi->connectorand then clearing it again on an error creates a race where the check inucsi_resume()may pass, only to haveucsi->connectorfree-ed underneath it whenucsi_init()hits an error.Fix this race by making
ucsi_init()store the connector array in a local variable and only assign it toucsi->connectoron success.Quoted 7 lines
Fixes bdc62f2bae8f ("usb: typec: ucsi: Simplified registration and I/O API")Signed-off-by Hans de Goede <[email protected]>---
Changes in v3:
- Assign connector[i].index before callingucsi_register_port()instead of passing i toucsi_register_port()
Quoted 1 line
You forgot to rebase this. It does not apply.
Ugh my bad, sorry about that. I'll send out a v4 fixing this.
Regards,
Hans
PATCH v3 3/3 usb: ucsi_acpi: Increase the command completion timeout
In reply to Hans de GoedeCommit 130a96d698d7 ("usb: typec: ucsi: acpi: Increase command completion timeout value") increased the timeout from 5 seconds to 60 seconds due to issues related to alternate mode discovery.
After the alternate mode discovery switch to polled mode the timeout was reduced, but instead of being set back to 5 seconds it was reduced to 1 second.
This is causing problems when using a Lenovo ThinkPad X1 yoga gen7 connected over Type-C to a LG 27UL850-W (charging DP over Type-C).
When the monitor is already connected at boot the following error is logged: "PPM init failed (-110)", /sys/class/typec is empty and on unplugging the NULL pointer deref fixed earlier in this series happens.
When the monitor is connected after boot the following error is logged instead: "GET_CONNECTOR_STATUS failed (-110)".
Setting the timeout back to 5 seconds fixes both cases.
--- drivers/usb/typec/ucsi/ucsi_acpi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/typec/ucsi/ucsi_acpi.c +1 -1
@@ -78,7 +78,7 @@ static int ucsi_acpi_sync_write(struct ucsi *ucsi, unsigned int offset,if (ret) goto out_clear_bit; -if (!wait_for_completion_timeout (&ua->complete,HZ )) +if (!wait_for_completion_timeout (&ua->complete,5 *HZ )) ret = -ETIMEDOUT ; out_clear_bit:
Signature · 1 line
2.39.1