Skip to content

Commit 11bb2ff

Browse files
quic-bjorandeandersson
authored andcommitted
usb: typec: ucsi: Move unregister out of atomic section
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: [email protected] Reviewed-by: Heikki Krogerus <[email protected]> Reviewed-by: Neil Armstrong <[email protected]> Reviewed-by: Dmitry Baryshkov <[email protected]> Tested-by: Amit Pundir <[email protected]> Reviewed-by: Johan Hovold <[email protected]> Tested-by: Johan Hovold <[email protected]> Signed-off-by: Bjorn Andersson <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Bjorn Andersson <[email protected]>
1 parent 3568aff commit 11bb2ff

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
@@ -112,8 +112,16 @@ EXPORT_SYMBOL_GPL(pmic_glink_client_register);
112112
int pmic_glink_send(struct pmic_glink_client *client, void *data, size_t len)
113113
{
114114
struct pmic_glink *pg = client->pg;
115+
int ret;
115116

116-
return rpmsg_send(pg->ept, data, len);
117+
mutex_lock(&pg->state_lock);
118+
if (!pg->ept)
119+
ret = -ECONNRESET;
120+
else
121+
ret = rpmsg_send(pg->ept, data, len);
122+
mutex_unlock(&pg->state_lock);
123+
124+
return ret;
117125
}
118126
EXPORT_SYMBOL_GPL(pmic_glink_send);
119127

drivers/usb/typec/ucsi/ucsi_glink.c

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

6969
struct work_struct notify_work;
7070
struct work_struct register_work;
71+
spinlock_t state_lock;
72+
bool ucsi_registered;
73+
bool pd_running;
7174

7275
u8 read_buf[UCSI_BUF_SIZE];
7376
};
@@ -244,8 +247,20 @@ static void pmic_glink_ucsi_notify(struct work_struct *work)
244247
static void pmic_glink_ucsi_register(struct work_struct *work)
245248
{
246249
struct pmic_glink_ucsi *ucsi = container_of(work, struct pmic_glink_ucsi, register_work);
250+
unsigned long flags;
251+
bool pd_running;
247252

248-
ucsi_register(ucsi->ucsi);
253+
spin_lock_irqsave(&ucsi->state_lock, flags);
254+
pd_running = ucsi->pd_running;
255+
spin_unlock_irqrestore(&ucsi->state_lock, flags);
256+
257+
if (!ucsi->ucsi_registered && pd_running) {
258+
ucsi_register(ucsi->ucsi);
259+
ucsi->ucsi_registered = true;
260+
} else if (ucsi->ucsi_registered && !pd_running) {
261+
ucsi_unregister(ucsi->ucsi);
262+
ucsi->ucsi_registered = false;
263+
}
249264
}
250265

251266
static void pmic_glink_ucsi_callback(const void *data, size_t len, void *priv)
@@ -269,11 +284,12 @@ static void pmic_glink_ucsi_callback(const void *data, size_t len, void *priv)
269284
static void pmic_glink_ucsi_pdr_notify(void *priv, int state)
270285
{
271286
struct pmic_glink_ucsi *ucsi = priv;
287+
unsigned long flags;
272288

273-
if (state == SERVREG_SERVICE_STATE_UP)
274-
schedule_work(&ucsi->register_work);
275-
else if (state == SERVREG_SERVICE_STATE_DOWN)
276-
ucsi_unregister(ucsi->ucsi);
289+
spin_lock_irqsave(&ucsi->state_lock, flags);
290+
ucsi->pd_running = (state == SERVREG_SERVICE_STATE_UP);
291+
spin_unlock_irqrestore(&ucsi->state_lock, flags);
292+
schedule_work(&ucsi->register_work);
277293
}
278294

279295
static void pmic_glink_ucsi_destroy(void *data)
@@ -320,6 +336,7 @@ static int pmic_glink_ucsi_probe(struct auxiliary_device *adev,
320336
INIT_WORK(&ucsi->register_work, pmic_glink_ucsi_register);
321337
init_completion(&ucsi->read_ack);
322338
init_completion(&ucsi->write_ack);
339+
spin_lock_init(&ucsi->state_lock);
323340
mutex_init(&ucsi->lock);
324341

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

0 commit comments

Comments
 (0)