Skip to content
/ linux Public

Commit 1e3769a

Browse files
quic-bjorandegregkh
authored andcommitted
usb: typec: ucsi: Move unregister out of atomic section
[ Upstream commit 11bb2ff ] Commit '9329933699b3 ("soc: qcom: pmic_glink: Make client-lock non-sleeping")' moved the pmic_glink client list under a spinlock, as it is accessed by the rpmsg/glink callback, which in turn is invoked from IRQ context. This means that ucsi_unregister() is now called from atomic context, which isn't feasible as it's expecting a sleepable context. An effort is under way to get GLINK to invoke its callbacks in a sleepable context, but until then lets schedule the unregistration. A side effect of this is that ucsi_unregister() can now happen after the remote processor, and thereby the communication link with it, is gone. pmic_glink_send() is amended with a check to avoid the resulting NULL pointer dereference. This does however result in the user being informed about this error by the following entry in the kernel log: ucsi_glink.pmic_glink_ucsi pmic_glink.ucsi.0: failed to send UCSI write request: -5 Fixes: 9329933 ("soc: qcom: pmic_glink: Make client-lock non-sleeping") Cc: stable@vger.kernel.org Reviewed-by: Heikki Krogerus <heikki.krogerus@linux.intel.com> Reviewed-by: Neil Armstrong <neil.armstrong@linaro.org> Reviewed-by: Dmitry Baryshkov <dmitry.baryshkov@linaro.org> Tested-by: Amit Pundir <amit.pundir@linaro.org> Reviewed-by: Johan Hovold <johan+linaro@kernel.org> Tested-by: Johan Hovold <johan+linaro@kernel.org> Signed-off-by: Bjorn Andersson <quic_bjorande@quicinc.com> Link: https://lore.kernel.org/r/20240820-pmic-glink-v6-11-races-v3-2-eec53c750a04@quicinc.com Signed-off-by: Bjorn Andersson <andersson@kernel.org> [ The context change is due to the commit 584e8df ("usb: typec: ucsi: extract common code for command handling") in v6.11 which is irrelevant to the logic of this patch. ] Signed-off-by: Rahul Sharma <black.hawk@163.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent c57387d commit 1e3769a

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

drivers/soc/qcom/pmic_glink.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,16 @@ EXPORT_SYMBOL_GPL(pmic_glink_client_register);
115115
int pmic_glink_send(struct pmic_glink_client *client, void *data, size_t len)
116116
{
117117
struct pmic_glink *pg = client->pg;
118+
int ret;
119+
120+
mutex_lock(&pg->state_lock);
121+
if (!pg->ept)
122+
ret = -ECONNRESET;
123+
else
124+
ret = rpmsg_send(pg->ept, data, len);
125+
mutex_unlock(&pg->state_lock);
118126

119-
return rpmsg_send(pg->ept, data, len);
127+
return ret;
120128
}
121129
EXPORT_SYMBOL_GPL(pmic_glink_send);
122130

drivers/usb/typec/ucsi/ucsi_glink.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ struct pmic_glink_ucsi {
7272

7373
struct work_struct notify_work;
7474
struct work_struct register_work;
75+
spinlock_t state_lock;
76+
bool ucsi_registered;
77+
bool pd_running;
7578

7679
u8 read_buf[UCSI_BUF_SIZE];
7780
};
@@ -270,8 +273,20 @@ static void pmic_glink_ucsi_notify(struct work_struct *work)
270273
static void pmic_glink_ucsi_register(struct work_struct *work)
271274
{
272275
struct pmic_glink_ucsi *ucsi = container_of(work, struct pmic_glink_ucsi, register_work);
276+
unsigned long flags;
277+
bool pd_running;
273278

274-
ucsi_register(ucsi->ucsi);
279+
spin_lock_irqsave(&ucsi->state_lock, flags);
280+
pd_running = ucsi->pd_running;
281+
spin_unlock_irqrestore(&ucsi->state_lock, flags);
282+
283+
if (!ucsi->ucsi_registered && pd_running) {
284+
ucsi_register(ucsi->ucsi);
285+
ucsi->ucsi_registered = true;
286+
} else if (ucsi->ucsi_registered && !pd_running) {
287+
ucsi_unregister(ucsi->ucsi);
288+
ucsi->ucsi_registered = false;
289+
}
275290
}
276291

277292
static void pmic_glink_ucsi_callback(const void *data, size_t len, void *priv)
@@ -295,11 +310,12 @@ static void pmic_glink_ucsi_callback(const void *data, size_t len, void *priv)
295310
static void pmic_glink_ucsi_pdr_notify(void *priv, int state)
296311
{
297312
struct pmic_glink_ucsi *ucsi = priv;
313+
unsigned long flags;
298314

299-
if (state == SERVREG_SERVICE_STATE_UP)
300-
schedule_work(&ucsi->register_work);
301-
else if (state == SERVREG_SERVICE_STATE_DOWN)
302-
ucsi_unregister(ucsi->ucsi);
315+
spin_lock_irqsave(&ucsi->state_lock, flags);
316+
ucsi->pd_running = (state == SERVREG_SERVICE_STATE_UP);
317+
spin_unlock_irqrestore(&ucsi->state_lock, flags);
318+
schedule_work(&ucsi->register_work);
303319
}
304320

305321
static void pmic_glink_ucsi_destroy(void *data)
@@ -332,6 +348,7 @@ static int pmic_glink_ucsi_probe(struct auxiliary_device *adev,
332348
init_completion(&ucsi->read_ack);
333349
init_completion(&ucsi->write_ack);
334350
init_completion(&ucsi->sync_ack);
351+
spin_lock_init(&ucsi->state_lock);
335352
mutex_init(&ucsi->lock);
336353

337354
ucsi->ucsi = ucsi_create(dev, &pmic_glink_ucsi_ops);

0 commit comments

Comments
 (0)