Skip to contentSkip to conversation

rpmsg: glink: Use spinlock in tx path

PATCH In review Bjorn Andersson 3 participants 3 messages Feb 13, 2018 – Mar 20, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rpmsg: glink: Use spinlock in tx path

Switch the tx_lock to a spinlock we allow clients to use rpmsg_trysend() from atomic context.

In order to allow clients to sleep while waiting for space in the FIFO we release the lock temporarily around the delay; which should be replaced by sending a READ_NOTIF and waiting for the remote to signal us that space has been made available.

---
 drivers/rpmsg/qcom_glink_native.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
drivers/rpmsg/qcom_glink_native.c +10 -6
index e0f31ed096a5..6e950e9afa82 100644
@@ -113,7 +113,7 @@ struct qcom_glink {
 	spinlock_t rx_lock;
 	struct list_head rx_queue;
 
-	struct mutex tx_lock;
+	spinlock_t tx_lock;
 
 	spinlock_t idr_lock;
 	struct idr lcids;
@@ -288,15 +288,14 @@ static int qcom_glink_tx(struct qcom_glink *glink,
 			 const void *data, size_t dlen, bool wait)
 {
 	unsigned int tlen = hlen + dlen;
+	unsigned long flags;
 	int ret;
 
 	/* Reject packets that are too big */
 	if (tlen >= glink->tx_pipe->length)
 		return -EINVAL;
 
-	ret = mutex_lock_interruptible(&glink->tx_lock);
-	if (ret)
-		return ret;
+	spin_lock_irqsave(&glink->tx_lock, flags);
 
 	while (qcom_glink_tx_avail(glink) < tlen) {
 		if (!wait) {
@@ -304,7 +303,12 @@ static int qcom_glink_tx(struct qcom_glink *glink,
 			goto out;
 		}
 
+		/* Wait without holding the tx_lock */
+		spin_unlock_irqrestore(&glink->tx_lock, flags);
+
 		usleep_range(10000, 15000);
+
+		spin_lock_irqsave(&glink->tx_lock, flags);
 	}
 
 	qcom_glink_tx_write(glink, hdr, hlen, data, dlen);
@@ -313,7 +317,7 @@ static int qcom_glink_tx(struct qcom_glink *glink,
 	mbox_client_txdone(glink->mbox_chan, 0);
 
 out:
-	mutex_unlock(&glink->tx_lock);
+	spin_unlock_irqrestore(&glink->tx_lock, flags);
 
 	return ret;
 }
@@ -1567,7 +1571,7 @@ struct qcom_glink *qcom_glink_native_probe(struct device *dev,
 	glink->features = features;
 	glink->intentless = intentless;
 
-	mutex_init(&glink->tx_lock);
+	spin_lock_init(&glink->tx_lock);
 	spin_lock_init(&glink->rx_lock);
 	INIT_LIST_HEAD(&glink->rx_queue);
 	INIT_WORK(&glink->rx_work, qcom_glink_work);
Signature · 1 line
2.15.0
#1 lore raw

Hey Bjorn,

Minor issue with the glink patch.

Thanks,
Chris

On 2/13/2018 11:04 AM, Bjorn Andersson wrote:

[..]

Quoted 6 lines
drivers/rpmsg/qcom_glink_native.c +1 -0
@@ -288,15 +288,14 @@ static int qcom_glink_tx(struct qcom_glink *glink,
  			 const void *data, size_t dlen, bool wait)
  {
  	unsigned int tlen = hlen + dlen;
+	unsigned long flags;
  	int ret;

drivers/rpmsg/qcom_glink_native.c static int qcom_glink_tx(struct qcom_glink *glink, · line 299

Quoted 10 lines
drivers/rpmsg/qcom_glink_native.c +1 -3
 
  	/* Reject packets that are too big */
  	if (tlen >= glink->tx_pipe->length)
  		return -EINVAL;
 
-	ret = mutex_lock_interruptible(&glink->tx_lock);
-	if (ret)
-		return ret;
+	spin_lock_irqsave(&glink->tx_lock, flags);
 

qcom_glink_tx will return an uninitialized ret value with removal of mutex_lock_interruptible.

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
Tested-by on v1 1/1
#2 lore raw
In reply to Bjorn Andersson
Quoted 10 lines · Bjorn Andersson

Switch the tx_lock to a spinlock we allow clients to use rpmsg_trysend() from atomic context.

In order to allow clients to sleep while waiting for space in the FIFO we release the lock temporarily around the delay; which should be replaced by sending a READ_NOTIF and waiting for the remote to signal us that space has been made available.

---

with ret set to 0 in qcom_glink_tx()

Quoted 66 lines
  drivers/rpmsg/qcom_glink_native.c | 16 ++++++++++------
  1 file changed, 10 insertions(+), 6 deletions(-)
drivers/rpmsg/qcom_glink_native.c +10 -6
index e0f31ed096a5..6e950e9afa82 100644
@@ -113,7 +113,7 @@ struct qcom_glink {
  	spinlock_t rx_lock;
  	struct list_head rx_queue;
 
-	struct mutex tx_lock;
+	spinlock_t tx_lock;
 
  	spinlock_t idr_lock;
  	struct idr lcids;
@@ -288,15 +288,14 @@ static int qcom_glink_tx(struct qcom_glink *glink,
  			 const void *data, size_t dlen, bool wait)
  {
  	unsigned int tlen = hlen + dlen;
+	unsigned long flags;
  	int ret;
 
  	/* Reject packets that are too big */
  	if (tlen >= glink->tx_pipe->length)
  		return -EINVAL;
 
-	ret = mutex_lock_interruptible(&glink->tx_lock);
-	if (ret)
-		return ret;
+	spin_lock_irqsave(&glink->tx_lock, flags);
 
  	while (qcom_glink_tx_avail(glink) < tlen) {
  		if (!wait) {
@@ -304,7 +303,12 @@ static int qcom_glink_tx(struct qcom_glink *glink,
  			goto out;
  		}
 
+		/* Wait without holding the tx_lock */
+		spin_unlock_irqrestore(&glink->tx_lock, flags);
+
  		usleep_range(10000, 15000);
+
+		spin_lock_irqsave(&glink->tx_lock, flags);
  	}
 
  	qcom_glink_tx_write(glink, hdr, hlen, data, dlen);
@@ -313,7 +317,7 @@ static int qcom_glink_tx(struct qcom_glink *glink,
  	mbox_client_txdone(glink->mbox_chan, 0);
 
  out:
-	mutex_unlock(&glink->tx_lock);
+	spin_unlock_irqrestore(&glink->tx_lock, flags);
 
  	return ret;
  }
@@ -1567,7 +1571,7 @@ struct qcom_glink *qcom_glink_native_probe(struct device *dev,
  	glink->features = features;
  	glink->intentless = intentless;
 
-	mutex_init(&glink->tx_lock);
+	spin_lock_init(&glink->tx_lock);
  	spin_lock_init(&glink->rx_lock);
  	INIT_LIST_HEAD(&glink->rx_queue);
  	INIT_WORK(&glink->rx_work, qcom_glink_work);