linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] Fix thread synchronization issues
@ 2025-04-24  8:44 Andrei Kuchynski
  2025-04-24  8:44 ` [PATCH v2 1/2] usb: typec: ucsi: displayport: Fix deadlock Andrei Kuchynski
  2025-04-24  8:44 ` [PATCH v2 2/2] usb: typec: ucsi: displayport: Fix NULL pointer access Andrei Kuchynski
  0 siblings, 2 replies; 5+ messages in thread
From: Andrei Kuchynski @ 2025-04-24  8:44 UTC (permalink / raw)
  To: Heikki Krogerus, Greg Kroah-Hartman, Jameson Thies,
	Abhishek Pandit-Subedi, Benson Leung, Dmitry Baryshkov, Diogo Ivo,
	Pooja Katiyar, Madhu M
  Cc: linux-usb, linux-kernel, Andrei Kuchynski

v2: ucsi_con_mutex_lock / ucsi_con_mutex_unlock functions are not longer
exported. Thanks to Heikki Krogerus for noticing it.

This patch series addresses deadlock issue and prevents a NULL pointer
access within the UCSI driver.

The first patch resolves a deadlock that occurs during partner device
removal. The deadlock scenario involves:
Thread 1: ucsi_handle_connector_change holding the connector mutex
(con->mutex) while waiting for dp_altmode_work() to complete
Thread 2: dp_altmode_work concurrently attempting to acquire the same mutex
Thread 3: ucsi_poll_worker is indirectly affected as it would be blocked
by the held mutex

The crash log:
 INFO: task kworker/u16:16:7254 blocked for more than 122 seconds.
       Not tainted 6.6.75-g5e7fdbab3246 #1
 task:kworker/u16:16  state:D stack:0 pid:7254  ppid:2 flags:0x00000208
 Workqueue: 16130000.spi:ec@0:typec ucsi_poll_worker [typec_ucsi]
 Call trace:
  __switch_to+0x130/0x158
  __schedule+0x950/0xa18
  schedule+0x68/0xc8
  schedule_preempt_disabled+0x28/0x40
  __mutex_lock+0x390/0xac0
  __mutex_lock_slowpath+0x18/0x28
  mutex_lock+0x28/0x60
  ucsi_poll_worker+0x24/0x138 [typec_ucsi (HASH:5d4a 1)]
  process_scheduled_works+0x258/0x4d8
  worker_thread+0x330/0x428
  kthread+0x108/0x1d0
  ret_from_fork+0x10/0x20
 INFO: task kworker/2:0:25045 blocked for more than 122 seconds.
       Not tainted 6.6.75-g5e7fdbab3246 #1
 task:kworker/2:0     state:D stack:0 pid:25045 ppid:2 flags:0x00000208
 Workqueue: events dp_altmode_work [typec_displayport]
 Call trace:
  __switch_to+0x130/0x158
  __schedule+0x950/0xa18
  schedule+0x68/0xc8
  schedule_preempt_disabled+0x28/0x40
  __mutex_lock+0x390/0xac0
  __mutex_lock_slowpath+0x18/0x28
  mutex_lock+0x28/0x60
  ucsi_displayport_enter+0x44/0x160 [typec_ucsi (HASH:5d4a 1)]
  typec_altmode_enter+0x110/0x120
  dp_altmode_work+0xe8/0x170 [typec_displayport (HASH:c690 2)]
  process_scheduled_works+0x258/0x4d8
  worker_thread+0x330/0x428
  kthread+0x108/0x1d0
  ret_from_fork+0x10/0x20
 INFO: task kworker/2:3:25952 blocked for more than 122 seconds.
       Not tainted 6.6.75-g5e7fdbab3246 #1
 task:kworker/2:3     state:D stack:0 pid:25952 ppid:2 flags:0x00000208
 Workqueue: events ucsi_handle_connector_change [typec_ucsi]
 Call trace:
  __switch_to+0x130/0x158
  __schedule+0x950/0xa18
  schedule+0x68/0xc8
  schedule_timeout+0x44/0x1b8
  wait_for_common+0xe4/0x1d8
  wait_for_completion+0x1c/0x30
  __flush_work+0x22c/0x2a8
  __cancel_work_timer+0x130/0x1d8
  cancel_work_sync+0x18/0x30
  dp_altmode_remove+0x20/0x70 [typec_displayport (HASH:c690 2)]
  typec_remove+0x60/0x120
  device_release_driver_internal+0x164/0x258
  device_release_driver+0x1c/0x30
  bus_remove_device+0x140/0x160
  device_del+0x1c0/0x330
  device_unregister+0x1c/0x40
  typec_unregister_altmode+0x40/0x58
  ucsi_unregister_partner+0x90/0x170 [typec_ucsi (HASH:5d4a 1)]
  ucsi_handle_connector_change+0x184/0x468 [typec_ucsi (HASH:5d4a 1)]
  process_scheduled_works+0x258/0x4d8
  worker_thread+0x330/0x428
  kthread+0x108/0x1d0
  ret_from_fork+0x10/0x20

The second patch prevents a NULL pointer access that occurs if
ucsi_displayport_work is still accessing partner-related data after
the partner has been unregistered.

The crash log:
 BUG: kernel NULL pointer dereference, address: 0000000000000300
 #PF: supervisor read access in kernel mode
 #PF: error_code(0x0000) - not-present page
 Oops: 0000 [#1] PREEMPT SMP NOPTI
 CPU: 3 PID: 3413 Comm: kworker/3:3 Tainted: G     U 6.6.76-07480
 Hardware name: Google Brox/Brox, BIOS Google_Brox.16080.59.0 12/18/2024
 Workqueue: events ucsi_displayport_work [typec_ucsi]
 RIP: 0010:typec_altmode_vdm+0x8f/0x4d0 [typec]
 RSP: 0018:ffffa929c825fe28 EFLAGS: 00010246
 RAX: 0000000000000014 RBX: 0000000000000001 RCX: 43032ac4505ac800
 RDX: ffff9d8def8e77c0 RSI: ffff9d8def8db5c8 RDI: ffff9d8def8db5c8
 RBP: ffffa929c825fe50 R08: 0000000000001fff R09: ffffffff9385e1e0
 R10: 0000000000005ffd R11: 00000000ffffdfff R12: ffff9d8ab4b67c08
 R13: 0000000000000000 R14: 0000000000000000 R15: 00000000ff01a144
 FS:  0000000000000000(0000) GS:ffff9d8def8c0000(0000) knlGS:0000000000000000
 CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
 CR2: 0000000000000300 CR3: 0000000148438000 CR4: 0000000000750ee0
 PKRU: 55555554
 Call Trace:
  ? __die_body+0xae/0xb0
  ? page_fault_oops+0x381/0x3e0
  ? prb_read_valid+0x29/0x50
  ? exc_page_fault+0x4f/0xa0
  ? asm_exc_page_fault+0x22/0x30
  ? typec_altmode_vdm+0x8f/0x4d0 [typec (HASH:c6ec 5)]
  ? typec_altmode_vdm+0x87/0x4d0 [typec (HASH:c6ec 5)]
  ucsi_displayport_work+0x34/0x70 [typec_ucsi (HASH:698a 6)]
  worker_thread+0x386/0x930

Andrei Kuchynski (2):
  usb: typec: ucsi: displayport: Fix deadlock
  usb: typec: ucsi: displayport: Fix NULL pointer access

 drivers/usb/typec/ucsi/displayport.c | 21 ++++++++++-------
 drivers/usb/typec/ucsi/ucsi.c        | 34 ++++++++++++++++++++++++++++
 drivers/usb/typec/ucsi/ucsi.h        |  2 ++
 3 files changed, 49 insertions(+), 8 deletions(-)

-- 
2.49.0.805.g082f7c87e0-goog


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/2] usb: typec: ucsi: displayport: Fix deadlock
  2025-04-24  8:44 [PATCH v2 0/2] Fix thread synchronization issues Andrei Kuchynski
@ 2025-04-24  8:44 ` Andrei Kuchynski
  2025-04-28 13:23   ` Heikki Krogerus
  2025-04-24  8:44 ` [PATCH v2 2/2] usb: typec: ucsi: displayport: Fix NULL pointer access Andrei Kuchynski
  1 sibling, 1 reply; 5+ messages in thread
From: Andrei Kuchynski @ 2025-04-24  8:44 UTC (permalink / raw)
  To: Heikki Krogerus, Greg Kroah-Hartman, Jameson Thies,
	Abhishek Pandit-Subedi, Benson Leung, Dmitry Baryshkov, Diogo Ivo,
	Pooja Katiyar, Madhu M
  Cc: linux-usb, linux-kernel, Andrei Kuchynski, stable

This patch introduces the ucsi_con_mutex_lock / ucsi_con_mutex_unlock
functions to the UCSI driver. ucsi_con_mutex_lock ensures the connector
mutex is only locked if a connection is established and the partner pointer
is valid. This resolves a deadlock scenario where
ucsi_displayport_remove_partner holds con->mutex waiting for
dp_altmode_work to complete while dp_altmode_work attempts to acquire it.

Cc: stable@vger.kernel.org
Fixes: af8622f6a585 ("usb: typec: ucsi: Support for DisplayPort alt mode")
Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
---
 drivers/usb/typec/ucsi/displayport.c | 19 +++++++++-------
 drivers/usb/typec/ucsi/ucsi.c        | 34 ++++++++++++++++++++++++++++
 drivers/usb/typec/ucsi/ucsi.h        |  2 ++
 3 files changed, 47 insertions(+), 8 deletions(-)

diff --git a/drivers/usb/typec/ucsi/displayport.c b/drivers/usb/typec/ucsi/displayport.c
index 420af5139c70..acd053d4e38c 100644
--- a/drivers/usb/typec/ucsi/displayport.c
+++ b/drivers/usb/typec/ucsi/displayport.c
@@ -54,7 +54,8 @@ static int ucsi_displayport_enter(struct typec_altmode *alt, u32 *vdo)
 	u8 cur = 0;
 	int ret;
 
-	mutex_lock(&dp->con->lock);
+	if (!ucsi_con_mutex_lock(dp->con))
+		return -ENOTCONN;
 
 	if (!dp->override && dp->initialized) {
 		const struct typec_altmode *p = typec_altmode_get_partner(alt);
@@ -100,7 +101,7 @@ static int ucsi_displayport_enter(struct typec_altmode *alt, u32 *vdo)
 	schedule_work(&dp->work);
 	ret = 0;
 err_unlock:
-	mutex_unlock(&dp->con->lock);
+	ucsi_con_mutex_unlock(dp->con);
 
 	return ret;
 }
@@ -112,7 +113,8 @@ static int ucsi_displayport_exit(struct typec_altmode *alt)
 	u64 command;
 	int ret = 0;
 
-	mutex_lock(&dp->con->lock);
+	if (!ucsi_con_mutex_lock(dp->con))
+		return -ENOTCONN;
 
 	if (!dp->override) {
 		const struct typec_altmode *p = typec_altmode_get_partner(alt);
@@ -144,7 +146,7 @@ static int ucsi_displayport_exit(struct typec_altmode *alt)
 	schedule_work(&dp->work);
 
 out_unlock:
-	mutex_unlock(&dp->con->lock);
+	ucsi_con_mutex_unlock(dp->con);
 
 	return ret;
 }
@@ -202,20 +204,21 @@ static int ucsi_displayport_vdm(struct typec_altmode *alt,
 	int cmd = PD_VDO_CMD(header);
 	int svdm_version;
 
-	mutex_lock(&dp->con->lock);
+	if (!ucsi_con_mutex_lock(dp->con))
+		return -ENOTCONN;
 
 	if (!dp->override && dp->initialized) {
 		const struct typec_altmode *p = typec_altmode_get_partner(alt);
 
 		dev_warn(&p->dev,
 			 "firmware doesn't support alternate mode overriding\n");
-		mutex_unlock(&dp->con->lock);
+		ucsi_con_mutex_unlock(dp->con);
 		return -EOPNOTSUPP;
 	}
 
 	svdm_version = typec_altmode_get_svdm_version(alt);
 	if (svdm_version < 0) {
-		mutex_unlock(&dp->con->lock);
+		ucsi_con_mutex_unlock(dp->con);
 		return svdm_version;
 	}
 
@@ -259,7 +262,7 @@ static int ucsi_displayport_vdm(struct typec_altmode *alt,
 		break;
 	}
 
-	mutex_unlock(&dp->con->lock);
+	ucsi_con_mutex_unlock(dp->con);
 
 	return 0;
 }
diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
index e8c7e9dc4930..01ce858a1a2b 100644
--- a/drivers/usb/typec/ucsi/ucsi.c
+++ b/drivers/usb/typec/ucsi/ucsi.c
@@ -1922,6 +1922,40 @@ void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
 }
 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
 
+/**
+ * ucsi_con_mutex_lock - Acquire the connector mutex
+ * @con: The connector interface to lock
+ *
+ * Returns true on success, false if the connector is disconnected
+ */
+bool ucsi_con_mutex_lock(struct ucsi_connector *con)
+{
+	bool mutex_locked = false;
+	bool connected = true;
+
+	while (connected && !mutex_locked) {
+		mutex_locked = mutex_trylock(&con->lock) != 0;
+		connected = UCSI_CONSTAT(con, CONNECTED);
+		if (connected && !mutex_locked)
+			msleep(20);
+	}
+
+	connected = connected && con->partner;
+	if (!connected && mutex_locked)
+		mutex_unlock(&con->lock);
+
+	return connected;
+}
+
+/**
+ * ucsi_con_mutex_unlock - Release the connector mutex
+ * @con: The connector interface to unlock
+ */
+void ucsi_con_mutex_unlock(struct ucsi_connector *con)
+{
+	mutex_unlock(&con->lock);
+}
+
 /**
  * ucsi_create - Allocate UCSI instance
  * @dev: Device interface to the PPM (Platform Policy Manager)
diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h
index 3a2c1762bec1..9c5278a0c5d4 100644
--- a/drivers/usb/typec/ucsi/ucsi.h
+++ b/drivers/usb/typec/ucsi/ucsi.h
@@ -94,6 +94,8 @@ int ucsi_register(struct ucsi *ucsi);
 void ucsi_unregister(struct ucsi *ucsi);
 void *ucsi_get_drvdata(struct ucsi *ucsi);
 void ucsi_set_drvdata(struct ucsi *ucsi, void *data);
+bool ucsi_con_mutex_lock(struct ucsi_connector *con);
+void ucsi_con_mutex_unlock(struct ucsi_connector *con);
 
 void ucsi_connector_change(struct ucsi *ucsi, u8 num);
 
-- 
2.49.0.805.g082f7c87e0-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/2] usb: typec: ucsi: displayport: Fix NULL pointer access
  2025-04-24  8:44 [PATCH v2 0/2] Fix thread synchronization issues Andrei Kuchynski
  2025-04-24  8:44 ` [PATCH v2 1/2] usb: typec: ucsi: displayport: Fix deadlock Andrei Kuchynski
@ 2025-04-24  8:44 ` Andrei Kuchynski
  2025-04-24 16:20   ` Benson Leung
  1 sibling, 1 reply; 5+ messages in thread
From: Andrei Kuchynski @ 2025-04-24  8:44 UTC (permalink / raw)
  To: Heikki Krogerus, Greg Kroah-Hartman, Jameson Thies,
	Abhishek Pandit-Subedi, Benson Leung, Dmitry Baryshkov, Diogo Ivo,
	Pooja Katiyar, Madhu M
  Cc: linux-usb, linux-kernel, Andrei Kuchynski, stable

This patch ensures that the UCSI driver waits for all pending tasks in the
ucsi_displayport_work workqueue to finish executing before proceeding with
the partner removal.

Cc: stable@vger.kernel.org
Fixes: af8622f6a585 ("usb: typec: ucsi: Support for DisplayPort alt mode")
Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
---
 drivers/usb/typec/ucsi/displayport.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/usb/typec/ucsi/displayport.c b/drivers/usb/typec/ucsi/displayport.c
index acd053d4e38c..8aae80b457d7 100644
--- a/drivers/usb/typec/ucsi/displayport.c
+++ b/drivers/usb/typec/ucsi/displayport.c
@@ -299,6 +299,8 @@ void ucsi_displayport_remove_partner(struct typec_altmode *alt)
 	if (!dp)
 		return;
 
+	cancel_work_sync(&dp->work);
+
 	dp->data.conf = 0;
 	dp->data.status = 0;
 	dp->initialized = false;
-- 
2.49.0.805.g082f7c87e0-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 2/2] usb: typec: ucsi: displayport: Fix NULL pointer access
  2025-04-24  8:44 ` [PATCH v2 2/2] usb: typec: ucsi: displayport: Fix NULL pointer access Andrei Kuchynski
@ 2025-04-24 16:20   ` Benson Leung
  0 siblings, 0 replies; 5+ messages in thread
From: Benson Leung @ 2025-04-24 16:20 UTC (permalink / raw)
  To: Andrei Kuchynski
  Cc: Heikki Krogerus, Greg Kroah-Hartman, Jameson Thies,
	Abhishek Pandit-Subedi, Benson Leung, Dmitry Baryshkov, Diogo Ivo,
	Pooja Katiyar, Madhu M, linux-usb, linux-kernel, stable

[-- Attachment #1: Type: text/plain, Size: 1142 bytes --]

On Thu, Apr 24, 2025 at 08:44:29AM +0000, Andrei Kuchynski wrote:
> This patch ensures that the UCSI driver waits for all pending tasks in the
> ucsi_displayport_work workqueue to finish executing before proceeding with
> the partner removal.
> 
> Cc: stable@vger.kernel.org
> Fixes: af8622f6a585 ("usb: typec: ucsi: Support for DisplayPort alt mode")
> Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>
> Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>

Reviewed-by: Benson Leung <bleung@chromium.org>

> ---
>  drivers/usb/typec/ucsi/displayport.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/usb/typec/ucsi/displayport.c b/drivers/usb/typec/ucsi/displayport.c
> index acd053d4e38c..8aae80b457d7 100644
> --- a/drivers/usb/typec/ucsi/displayport.c
> +++ b/drivers/usb/typec/ucsi/displayport.c
> @@ -299,6 +299,8 @@ void ucsi_displayport_remove_partner(struct typec_altmode *alt)
>  	if (!dp)
>  		return;
>  
> +	cancel_work_sync(&dp->work);
> +
>  	dp->data.conf = 0;
>  	dp->data.status = 0;
>  	dp->initialized = false;
> -- 
> 2.49.0.805.g082f7c87e0-goog
> 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 1/2] usb: typec: ucsi: displayport: Fix deadlock
  2025-04-24  8:44 ` [PATCH v2 1/2] usb: typec: ucsi: displayport: Fix deadlock Andrei Kuchynski
@ 2025-04-28 13:23   ` Heikki Krogerus
  0 siblings, 0 replies; 5+ messages in thread
From: Heikki Krogerus @ 2025-04-28 13:23 UTC (permalink / raw)
  To: Andrei Kuchynski
  Cc: Greg Kroah-Hartman, Jameson Thies, Abhishek Pandit-Subedi,
	Benson Leung, Dmitry Baryshkov, Diogo Ivo, Pooja Katiyar, Madhu M,
	linux-usb, linux-kernel, stable

On Thu, Apr 24, 2025 at 08:44:28AM +0000, Andrei Kuchynski wrote:
> This patch introduces the ucsi_con_mutex_lock / ucsi_con_mutex_unlock
> functions to the UCSI driver. ucsi_con_mutex_lock ensures the connector
> mutex is only locked if a connection is established and the partner pointer
> is valid. This resolves a deadlock scenario where
> ucsi_displayport_remove_partner holds con->mutex waiting for
> dp_altmode_work to complete while dp_altmode_work attempts to acquire it.
> 
> Cc: stable@vger.kernel.org
> Fixes: af8622f6a585 ("usb: typec: ucsi: Support for DisplayPort alt mode")
> Signed-off-by: Andrei Kuchynski <akuchynski@chromium.org>

Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>

> ---
>  drivers/usb/typec/ucsi/displayport.c | 19 +++++++++-------
>  drivers/usb/typec/ucsi/ucsi.c        | 34 ++++++++++++++++++++++++++++
>  drivers/usb/typec/ucsi/ucsi.h        |  2 ++
>  3 files changed, 47 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/usb/typec/ucsi/displayport.c b/drivers/usb/typec/ucsi/displayport.c
> index 420af5139c70..acd053d4e38c 100644
> --- a/drivers/usb/typec/ucsi/displayport.c
> +++ b/drivers/usb/typec/ucsi/displayport.c
> @@ -54,7 +54,8 @@ static int ucsi_displayport_enter(struct typec_altmode *alt, u32 *vdo)
>  	u8 cur = 0;
>  	int ret;
>  
> -	mutex_lock(&dp->con->lock);
> +	if (!ucsi_con_mutex_lock(dp->con))
> +		return -ENOTCONN;
>  
>  	if (!dp->override && dp->initialized) {
>  		const struct typec_altmode *p = typec_altmode_get_partner(alt);
> @@ -100,7 +101,7 @@ static int ucsi_displayport_enter(struct typec_altmode *alt, u32 *vdo)
>  	schedule_work(&dp->work);
>  	ret = 0;
>  err_unlock:
> -	mutex_unlock(&dp->con->lock);
> +	ucsi_con_mutex_unlock(dp->con);
>  
>  	return ret;
>  }
> @@ -112,7 +113,8 @@ static int ucsi_displayport_exit(struct typec_altmode *alt)
>  	u64 command;
>  	int ret = 0;
>  
> -	mutex_lock(&dp->con->lock);
> +	if (!ucsi_con_mutex_lock(dp->con))
> +		return -ENOTCONN;
>  
>  	if (!dp->override) {
>  		const struct typec_altmode *p = typec_altmode_get_partner(alt);
> @@ -144,7 +146,7 @@ static int ucsi_displayport_exit(struct typec_altmode *alt)
>  	schedule_work(&dp->work);
>  
>  out_unlock:
> -	mutex_unlock(&dp->con->lock);
> +	ucsi_con_mutex_unlock(dp->con);
>  
>  	return ret;
>  }
> @@ -202,20 +204,21 @@ static int ucsi_displayport_vdm(struct typec_altmode *alt,
>  	int cmd = PD_VDO_CMD(header);
>  	int svdm_version;
>  
> -	mutex_lock(&dp->con->lock);
> +	if (!ucsi_con_mutex_lock(dp->con))
> +		return -ENOTCONN;
>  
>  	if (!dp->override && dp->initialized) {
>  		const struct typec_altmode *p = typec_altmode_get_partner(alt);
>  
>  		dev_warn(&p->dev,
>  			 "firmware doesn't support alternate mode overriding\n");
> -		mutex_unlock(&dp->con->lock);
> +		ucsi_con_mutex_unlock(dp->con);
>  		return -EOPNOTSUPP;
>  	}
>  
>  	svdm_version = typec_altmode_get_svdm_version(alt);
>  	if (svdm_version < 0) {
> -		mutex_unlock(&dp->con->lock);
> +		ucsi_con_mutex_unlock(dp->con);
>  		return svdm_version;
>  	}
>  
> @@ -259,7 +262,7 @@ static int ucsi_displayport_vdm(struct typec_altmode *alt,
>  		break;
>  	}
>  
> -	mutex_unlock(&dp->con->lock);
> +	ucsi_con_mutex_unlock(dp->con);
>  
>  	return 0;
>  }
> diff --git a/drivers/usb/typec/ucsi/ucsi.c b/drivers/usb/typec/ucsi/ucsi.c
> index e8c7e9dc4930..01ce858a1a2b 100644
> --- a/drivers/usb/typec/ucsi/ucsi.c
> +++ b/drivers/usb/typec/ucsi/ucsi.c
> @@ -1922,6 +1922,40 @@ void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
>  }
>  EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
>  
> +/**
> + * ucsi_con_mutex_lock - Acquire the connector mutex
> + * @con: The connector interface to lock
> + *
> + * Returns true on success, false if the connector is disconnected
> + */
> +bool ucsi_con_mutex_lock(struct ucsi_connector *con)
> +{
> +	bool mutex_locked = false;
> +	bool connected = true;
> +
> +	while (connected && !mutex_locked) {
> +		mutex_locked = mutex_trylock(&con->lock) != 0;
> +		connected = UCSI_CONSTAT(con, CONNECTED);
> +		if (connected && !mutex_locked)
> +			msleep(20);
> +	}
> +
> +	connected = connected && con->partner;
> +	if (!connected && mutex_locked)
> +		mutex_unlock(&con->lock);
> +
> +	return connected;
> +}
> +
> +/**
> + * ucsi_con_mutex_unlock - Release the connector mutex
> + * @con: The connector interface to unlock
> + */
> +void ucsi_con_mutex_unlock(struct ucsi_connector *con)
> +{
> +	mutex_unlock(&con->lock);
> +}
> +
>  /**
>   * ucsi_create - Allocate UCSI instance
>   * @dev: Device interface to the PPM (Platform Policy Manager)
> diff --git a/drivers/usb/typec/ucsi/ucsi.h b/drivers/usb/typec/ucsi/ucsi.h
> index 3a2c1762bec1..9c5278a0c5d4 100644
> --- a/drivers/usb/typec/ucsi/ucsi.h
> +++ b/drivers/usb/typec/ucsi/ucsi.h
> @@ -94,6 +94,8 @@ int ucsi_register(struct ucsi *ucsi);
>  void ucsi_unregister(struct ucsi *ucsi);
>  void *ucsi_get_drvdata(struct ucsi *ucsi);
>  void ucsi_set_drvdata(struct ucsi *ucsi, void *data);
> +bool ucsi_con_mutex_lock(struct ucsi_connector *con);
> +void ucsi_con_mutex_unlock(struct ucsi_connector *con);
>  
>  void ucsi_connector_change(struct ucsi *ucsi, u8 num);
>  
> -- 
> 2.49.0.805.g082f7c87e0-goog

-- 
heikki

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-04-28 13:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-24  8:44 [PATCH v2 0/2] Fix thread synchronization issues Andrei Kuchynski
2025-04-24  8:44 ` [PATCH v2 1/2] usb: typec: ucsi: displayport: Fix deadlock Andrei Kuchynski
2025-04-28 13:23   ` Heikki Krogerus
2025-04-24  8:44 ` [PATCH v2 2/2] usb: typec: ucsi: displayport: Fix NULL pointer access Andrei Kuchynski
2025-04-24 16:20   ` Benson Leung

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).