Skip to contentSkip to conversation

rpmsg: glink stability fixes

PATCH In review Bjorn Andersson 2 participants 11 messages Oct 4, 2019 – Oct 9, 2019

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

v2 cover rpmsg: glink stability fixes

Fixes for issues found in GLINK during reboot testing of a remoteproc.

Arun Kumar Neelakantam (2):
rpmsg: glink: Fix reuse intents memory leak issue
rpmsg: glink: Fix use after free in open_ack TIMEOUT case

Bjorn Andersson (2):
rpmsg: glink: Don't send pending rx_done during remove
rpmsg: glink: Free pending deferred work on remove

Chris Lew (2):
rpmsg: glink: Put an extra reference during cleanup
rpmsg: glink: Fix rpmsg_register_device err handling

 drivers/rpmsg/qcom_glink_native.c | 52 +++++++++++++++++++++++++------
 1 file changed, 42 insertions(+), 10 deletions(-)
Signature · 1 line
2.18.0
#1 lore raw

PATCH v2 1/6 rpmsg: glink: Fix reuse intents memory leak issue

From: Arun Kumar Neelakantam <[email protected]>

Memory allocated for re-usable intents are not freed during channel cleanup which causes memory leak in system.

Check and free all re-usable memory to avoid memory leak.

---

Changes since v1:
- None

 drivers/rpmsg/qcom_glink_native.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/rpmsg/qcom_glink_native.c +9 -0
index 621f1afd4d6b..9355ce26fd98 100644
@@ -241,10 +241,19 @@ static void qcom_glink_channel_release(struct kref *ref)
 {
 	struct glink_channel *channel = container_of(ref, struct glink_channel,
 						     refcount);
+	struct glink_core_rx_intent *tmp;
 	unsigned long flags;
+	int iid;
 
 	spin_lock_irqsave(&channel->intent_lock, flags);
+	idr_for_each_entry(&channel->liids, tmp, iid) {
+		kfree(tmp->data);
+		kfree(tmp);
+	}
 	idr_destroy(&channel->liids);
+
+	idr_for_each_entry(&channel->riids, tmp, iid)
+		kfree(tmp);
 	idr_destroy(&channel->riids);
 	spin_unlock_irqrestore(&channel->intent_lock, flags);
 
Signature · 1 line
2.18.0
Acked-by on v2 1/6
#2 lore raw

Re: [PATCH v2 1/6] rpmsg: glink: Fix reuse intents memory leak issue

Quoted 7 lines · Bjorn Andersson

From: Arun Kumar Neelakantam <[email protected]>

Memory allocated for re-usable intents are not freed during channel cleanup which causes memory leak in system.

Check and free all re-usable memory to avoid memory leak.

Quoted 7 lines

---

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
#3 lore raw

PATCH v2 2/6 rpmsg: glink: Fix use after free in open_ack TIMEOUT case

In reply to Bjorn Andersson

From: Arun Kumar Neelakantam <[email protected]>

Extra channel reference put when remote sending OPEN_ACK after timeout causes use-after-free while handling next remote CLOSE command.

Remove extra reference put in timeout case to avoid use-after-free.

Fixes b4f8e52b89f6 ("rpmsg: Introduce Qualcomm RPM glink driver")

---

Changes since v1:
- None

 drivers/rpmsg/qcom_glink_native.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/rpmsg/qcom_glink_native.c +3 -4
index 9355ce26fd98..72ed671f5dcd 100644
@@ -1103,13 +1103,12 @@ static int qcom_glink_create_remote(struct qcom_glink *glink,
 close_link:
 	/*
 	 * Send a close request to "undo" our open-ack. The close-ack will
-	 * release the last reference.
+	 * release qcom_glink_send_open_req() reference and the last reference
+	 * will be relesed after receiving remote_close or transport unregister
+	 * by calling qcom_glink_native_remove().
 	 */
 	qcom_glink_send_close_req(glink, channel);
 
-	/* Release qcom_glink_send_open_req() reference */
-	kref_put(&channel->refcount, qcom_glink_channel_release);
-
 	return ret;
 }
 
Signature · 1 line
2.18.0
Acked-by on v2 2/6
#4 lore raw

Re: [PATCH v2 2/6] rpmsg: glink: Fix use after free in open_ack TIMEOUT case

Quoted 5 lines · Bjorn Andersson

From: Arun Kumar Neelakantam <[email protected]>

Extra channel reference put when remote sending OPEN_ACK after timeout causes use-after-free while handling next remote CLOSE command.

Quoted 8 lines

Remove extra reference put in timeout case to avoid use-after-free.

Fixes b4f8e52b89f6 ("rpmsg: Introduce Qualcomm RPM glink driver")

---

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
#5 lore raw

PATCH v2 3/6 rpmsg: glink: Put an extra reference during cleanup

In reply to Bjorn Andersson

From: Chris Lew <[email protected]>

In a remote processor crash scenario, there is no guarantee the remote processor sent close requests before it went into a bad state. Remove the reference that is normally handled by the close command in the so channel resources can be released.

Fixes b4f8e52b89f6 ("rpmsg: Introduce Qualcomm RPM glink driver")

---

Changes since v1:
- None

 drivers/rpmsg/qcom_glink_native.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/rpmsg/qcom_glink_native.c +4 -0
index 72ed671f5dcd..21fd2ae5f7f1 100644
@@ -1641,6 +1641,10 @@ void qcom_glink_native_remove(struct qcom_glink *glink)
 	idr_for_each_entry(&glink->lcids, channel, cid)
 		kref_put(&channel->refcount, qcom_glink_channel_release);
 
+	/* Release any defunct local channels, waiting for close-req */
+	idr_for_each_entry(&glink->rcids, channel, cid)
+		kref_put(&channel->refcount, qcom_glink_channel_release);
+
 	idr_destroy(&glink->lcids);
 	idr_destroy(&glink->rcids);
 	spin_unlock_irqrestore(&glink->idr_lock, flags);
Signature · 1 line
2.18.0
#6 lore raw

PATCH v2 4/6 rpmsg: glink: Fix rpmsg_register_device err handling

In reply to Bjorn Andersson

From: Chris Lew <[email protected]>

The device release function is set before registering with rpmsg. If rpmsg registration fails, the framework will call device_put(), which invokes the release function. The channel create logic does not need to free rpdev if rpmsg_register_device() fails and release is called.

Fixes b4f8e52b89f6 ("rpmsg: Introduce Qualcomm RPM glink driver")

---

Changes since v1:
- None

 drivers/rpmsg/qcom_glink_native.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/rpmsg/qcom_glink_native.c +1 -3
index 21fd2ae5f7f1..89e02baea2d0 100644
@@ -1423,15 +1423,13 @@ static int qcom_glink_rx_open(struct qcom_glink *glink, unsigned int rcid,
 
 		ret = rpmsg_register_device(rpdev);
 		if (ret)
-			goto free_rpdev;
+			goto rcid_remove;
 
 		channel->rpdev = rpdev;
 	}
 
 	return 0;
 
-free_rpdev:
-	kfree(rpdev);
 rcid_remove:
 	spin_lock_irqsave(&glink->idr_lock, flags);
 	idr_remove(&glink->rcids, channel->rcid);
Signature · 1 line
2.18.0
#7 lore raw

PATCH v2 5/6 rpmsg: glink: Don't send pending rx_done during remove

In reply to Bjorn Andersson

Attempting to transmit rx_done messages after the GLINK instance is being torn down will cause use after free and memory leaks. So cancel the intent_work and free up the pending intents.

With this there are no concurrent accessors of the channel left during qcom_glink_native_remove() and there is therefor no need to hold the spinlock during this operation - which would prohibit the use of cancel_work_sync() in the release function. So remove this.

Fixes 1d2ea36eead9 ("rpmsg: glink: Add rx done command")

---

Changes since v1:
- Drop the locking of idr_lock in qcom_glink_native_remove()

 drivers/rpmsg/qcom_glink_native.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/rpmsg/qcom_glink_native.c +12 -2
index 89e02baea2d0..4117818db6a1 100644
@@ -241,11 +241,23 @@ static void qcom_glink_channel_release(struct kref *ref)
 {
 	struct glink_channel *channel = container_of(ref, struct glink_channel,
 						     refcount);
+	struct glink_core_rx_intent *intent;
 	struct glink_core_rx_intent *tmp;
 	unsigned long flags;
 	int iid;
 
+	/* cancel pending rx_done work */
+	cancel_work_sync(&channel->intent_work);
+
 	spin_lock_irqsave(&channel->intent_lock, flags);
+	/* Free all non-reuse intents pending rx_done work */
+	list_for_each_entry_safe(intent, tmp, &channel->done_intents, node) {
+		if (!intent->reuse) {
+			kfree(intent->data);
+			kfree(intent);
+		}
+	}
+
 	idr_for_each_entry(&channel->liids, tmp, iid) {
 		kfree(tmp->data);
 		kfree(tmp);
@@ -1634,7 +1646,6 @@ void qcom_glink_native_remove(struct qcom_glink *glink)
 	if (ret)
 		dev_warn(glink->dev, "Can't remove GLINK devices: %d\n", ret);
 
-	spin_lock_irqsave(&glink->idr_lock, flags);
 	/* Release any defunct local channels, waiting for close-ack */
 	idr_for_each_entry(&glink->lcids, channel, cid)
 		kref_put(&channel->refcount, qcom_glink_channel_release);
@@ -1645,7 +1656,6 @@ void qcom_glink_native_remove(struct qcom_glink *glink)
 
 	idr_destroy(&glink->lcids);
 	idr_destroy(&glink->rcids);
-	spin_unlock_irqrestore(&glink->idr_lock, flags);
 	mbox_free_channel(glink->mbox_chan);
 }
 EXPORT_SYMBOL_GPL(qcom_glink_native_remove);
Signature · 1 line
2.18.0
Acked-by on v2 5/6
#8 lore raw

Re: [PATCH v2 5/6] rpmsg: glink: Don't send pending rx_done during remove

Quoted 9 lines · Bjorn Andersson

Attempting to transmit rx_done messages after the GLINK instance is being torn down will cause use after free and memory leaks. So cancel the intent_work and free up the pending intents.

With this there are no concurrent accessors of the channel left during qcom_glink_native_remove() and there is therefor no need to hold the spinlock during this operation - which would prohibit the use of cancel_work_sync() in the release function. So remove this.

Quoted 5 lines
Fixes 1d2ea36eead9 ("rpmsg: glink: Add rx done command")

---

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
#9 lore raw

PATCH v2 6/6 rpmsg: glink: Free pending deferred work on remove

In reply to Bjorn Andersson

By just cancelling the deferred rx worker during GLINK instance teardown any pending deferred commands are leaked, so free them.

Fixes b4f8e52b89f6 ("rpmsg: Introduce Qualcomm RPM glink driver")

---

Changes since v1:
- None

 drivers/rpmsg/qcom_glink_native.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
drivers/rpmsg/qcom_glink_native.c +13 -1
index 4117818db6a1..862f89c128a0 100644
@@ -1562,6 +1562,18 @@ static void qcom_glink_work(struct work_struct *work)
 	}
 }
 
+static void qcom_glink_cancel_rx_work(struct qcom_glink *glink)
+{
+	struct glink_defer_cmd *dcmd;
+	struct glink_defer_cmd *tmp;
+
+	/* cancel any pending deferred rx_work */
+	cancel_work_sync(&glink->rx_work);
+
+	list_for_each_entry_safe(dcmd, tmp, &glink->rx_queue, node)
+		kfree(dcmd);
+}
+
 struct qcom_glink *qcom_glink_native_probe(struct device *dev,
 					   unsigned long features,
 					   struct qcom_glink_pipe *rx,
@@ -1640,7 +1652,7 @@ void qcom_glink_native_remove(struct qcom_glink *glink)
 	unsigned long flags;
 
 	disable_irq(glink->irq);
-	cancel_work_sync(&glink->rx_work);
+	qcom_glink_cancel_rx_work(glink);
 
 	ret = device_for_each_child(glink->dev, NULL, qcom_glink_remove_device);
 	if (ret)
Signature · 1 line
2.18.0
Acked-by on v2 6/6
#10 lore raw

Re: [PATCH v2 6/6] rpmsg: glink: Free pending deferred work on remove

Quoted 8 lines · Bjorn Andersson

By just cancelling the deferred rx worker during GLINK instance teardown any pending deferred commands are leaked, so free them.

Fixes b4f8e52b89f6 ("rpmsg: Introduce Qualcomm RPM glink driver")

---

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project