Netdev List
 help / color / mirror / Atom feed
* [PATCH net] net: qrtr: smd: hold packets that arrive before probe has finished
@ 2026-12-03 17:05 Andrey Derkach
  2026-12-04 17:46 ` netdev-bot+sashiko
  0 siblings, 1 reply; 3+ messages in thread
From: Andrey Derkach @ 2026-12-03 17:05 UTC (permalink / raw)
  To: Manivannan Sadhasivam, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Willem de Bruijn
  Cc: Simon Horman, Bjorn Andersson, netdev, linux-arm-msm

The rpmsg core opens the channel before it calls qcom_smd_qrtr_probe(),
and probe sets the driver data only after qrtr_endpoint_register(). A
packet that arrives in between finds no driver data, and the callback
returns -EAGAIN. SMD delivers such a packet again later, but GLINK
ignores the return value, so the packet is lost. GLINK can also drop
such a packet even earlier; that is fixed in a separate GLINK patch.

The first packet the modem sends is its QRTR HELLO. If it is lost, our
name service never tells the modem about TFTP or the service locator.
The modem then never loads its WLAN firmware and crashes 40 seconds
later ("fatal error without message"). On two sdm636 phones (Lenovo S5
Pro, Xiaomi Redmi Note 5 Pro), where udev loads qrtr-smd when the modem
opens the channel, the HELLO is lost on some boots, here or in GLINK.
With a 50 ms delay added to probe it is lost here on every boot. When
qrtr-smd is built in or already loaded, probe runs in the GLINK worker,
which answers the modem's request for a receive buffer only after probe,
so nothing can arrive early.

Keep the packets that arrive during probe in a device resource and pass
them on once the endpoint is registered. The held packets are passed on
and the driver data is set under one lock, so they go out before any
packet that arrives later. If probe fails or the driver is unbound, the
driver core frees whatever is still held.

Fixes: 5052de8deff5 ("soc: qcom: smd: Transition client drivers from smd to rpmsg")
Signed-off-by: Andrey Derkach <gabin278@gmail.com>
---
GLINK can lose the same packet one step earlier, before the endpoint
callback is set. That is fixed in "rpmsg: glink: set up the endpoint
before sending the open-ack":
https://lore.kernel.org/r/20261203170512.3597156-1-gabin278@gmail.com/
The two patches don't depend on each other.

Tests, with qrtr-smd loaded by udev when the modem opens IPCRTR:
- Redmi Note 5 Pro (sdm636), with the GLINK patch. This patch on the
  net tree's net/qrtr, built for a 7.3-based kernel (devm_kzalloc_obj()
  changed back to devm_kzalloc() for that): 0 of 29 boots bad
  (normal boots, a 50 ms delay in probe, udevd held off its CPU during
  probe, a failed first probe). Without it, 6 of 6 boots where the
  HELLO arrived before probe had finished were bad. The same change
  on the 7.3 tree: 0 of 23 bad.
- Lenovo S5 Pro (sdm636), 7.3, an earlier version of this patch, no
  GLINK patch: 5 of 18 normal boots bad before, 0 of 3 with it; with
  the delay 2 of 2 bad before, 0 of 2 with it.

Not tested: a full mainline kernel; only the net tree's net/qrtr on a
7.3-based kernel.

Who is affected: the modem crash needs the modem to open the first
IPCRTR channel. With the ADSP booting by itself, the ADSP opened
IPCRTR 1.5 s before the modem on all 13 boots. qrtr-smd was then
loaded for the ADSP's channel, the modem's channel was probed later in
the GLINK worker, and the modem was fine on all of them, also with the
delay (2 of 2 bad without the ADSP). The ADSP sends its HELLO about
42 ms after it opens IPCRTR; on normal boots probe had finished by
then. With the delay, the ADSP's HELLO was lost on 3 of 3 boots: it
sent no second one, our name service never told it about our
services, and its DIAG service never showed up. Nothing else visibly
broke in 5 minutes (audio and sensor drivers were not loaded). With
this patch the ADSP's HELLO was held and passed on (2 of 2).

qcom_smd_qrtr_remove() has a separate ordering problem (it unregisters
the endpoint before it clears the driver data); I'll send that fix on
its own.

 net/qrtr/smd.c | 118 +++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 109 insertions(+), 9 deletions(-)

diff --git a/net/qrtr/smd.c b/net/qrtr/smd.c
index 8b30b8275..e9e984db1 100644
--- a/net/qrtr/smd.c
+++ b/net/qrtr/smd.c
@@ -16,15 +16,28 @@ struct qrtr_smd_dev {
 	struct device *dev;
 };
 
-/* from smd to qrtr */
-static int qcom_smd_qrtr_callback(struct rpmsg_device *rpdev,
-				  void *data, int len, void *priv, u32 addr)
+/* The rpmsg core opens the channel before it calls our probe, so the remote
+ * may send packets before the QRTR endpoint is registered. SMD retries a
+ * packet whose callback fails, but GLINK drops it, and losing the remote's
+ * HELLO this way leaves the link without any service announcements. Packets
+ * that arrive while the driver data is not set are therefore held in a
+ * device resource. Probe posts them once the endpoint is registered; if
+ * probe fails or the driver is unbound, the driver core frees them.
+ *
+ * One lock for all devices is enough: the callback takes it only while the
+ * driver data is not set, and before probe there is no per-device place to
+ * keep a lock.
+ */
+static DEFINE_SPINLOCK(qrtr_smd_held_lock);
+
+static void qcom_smd_qrtr_held_release(struct device *dev, void *res)
 {
-	struct qrtr_smd_dev *qdev = dev_get_drvdata(&rpdev->dev);
-	int rc;
+	__skb_queue_purge(res);
+}
 
-	if (!qdev)
-		return -EAGAIN;
+static int qcom_smd_qrtr_post(struct qrtr_smd_dev *qdev, void *data, int len)
+{
+	int rc;
 
 	rc = qrtr_endpoint_post(&qdev->ep, data, len);
 	if (rc == -EINVAL) {
@@ -36,6 +49,91 @@ static int qcom_smd_qrtr_callback(struct rpmsg_device *rpdev,
 	return rc;
 }
 
+static int qcom_smd_qrtr_hold(struct rpmsg_device *rpdev, void *data, int len)
+{
+	struct qrtr_smd_dev *qdev;
+	struct sk_buff_head *held;
+	struct sk_buff *skb;
+	unsigned long flags;
+	int rc = 0;
+
+	spin_lock_irqsave(&qrtr_smd_held_lock, flags);
+
+	/* probe may have finished in the meantime */
+	qdev = dev_get_drvdata(&rpdev->dev);
+	if (qdev) {
+		rc = qcom_smd_qrtr_post(qdev, data, len);
+		goto out;
+	}
+
+	held = devres_find(&rpdev->dev, qcom_smd_qrtr_held_release, NULL, NULL);
+	if (!held) {
+		held = devres_alloc(qcom_smd_qrtr_held_release, sizeof(*held),
+				    GFP_ATOMIC);
+		if (!held) {
+			rc = -ENOMEM;
+			goto out;
+		}
+		__skb_queue_head_init(held);
+		devres_add(&rpdev->dev, held);
+	}
+
+	skb = alloc_skb(len, GFP_ATOMIC);
+	if (!skb) {
+		rc = -ENOMEM;
+		goto out;
+	}
+	skb_put_data(skb, data, len);
+	__skb_queue_tail(held, skb);
+
+out:
+	spin_unlock_irqrestore(&qrtr_smd_held_lock, flags);
+
+	return rc;
+}
+
+/* Post the packets held during probe, then publish @qdev so the callback
+ * posts directly. Both happen under the lock, so held packets are posted
+ * before any packet that arrives later.
+ */
+static unsigned int qcom_smd_qrtr_post_held(struct rpmsg_device *rpdev,
+					    struct qrtr_smd_dev *qdev)
+{
+	struct sk_buff_head *held;
+	unsigned int count = 0;
+	struct sk_buff *skb;
+	unsigned long flags;
+
+	spin_lock_irqsave(&qrtr_smd_held_lock, flags);
+
+	held = devres_find(&rpdev->dev, qcom_smd_qrtr_held_release, NULL, NULL);
+	if (held) {
+		while ((skb = __skb_dequeue(held))) {
+			qcom_smd_qrtr_post(qdev, skb->data, skb->len);
+			consume_skb(skb);
+			count++;
+		}
+	}
+
+	dev_set_drvdata(&rpdev->dev, qdev);
+
+	spin_unlock_irqrestore(&qrtr_smd_held_lock, flags);
+
+	return count;
+}
+
+/* from smd to qrtr */
+static int qcom_smd_qrtr_callback(struct rpmsg_device *rpdev,
+				  void *data, int len, void *priv, u32 addr)
+{
+	struct qrtr_smd_dev *qdev = dev_get_drvdata(&rpdev->dev);
+
+	if (!qdev)
+		return qcom_smd_qrtr_hold(rpdev, data, len);
+
+	return qcom_smd_qrtr_post(qdev, data, len);
+}
+
 /* from qrtr to smd */
 static int qcom_smd_qrtr_send(struct qrtr_endpoint *ep, struct sk_buff *skb)
 {
@@ -59,6 +157,7 @@ static int qcom_smd_qrtr_send(struct qrtr_endpoint *ep, struct sk_buff *skb)
 static int qcom_smd_qrtr_probe(struct rpmsg_device *rpdev)
 {
 	struct qrtr_smd_dev *qdev;
+	unsigned int held;
 	int rc;
 
 	qdev = devm_kzalloc_obj(&rpdev->dev, *qdev);
@@ -73,9 +172,10 @@ static int qcom_smd_qrtr_probe(struct rpmsg_device *rpdev)
 	if (rc)
 		return rc;
 
-	dev_set_drvdata(&rpdev->dev, qdev);
+	held = qcom_smd_qrtr_post_held(rpdev, qdev);
 
-	dev_dbg(&rpdev->dev, "Qualcomm SMD QRTR driver probed\n");
+	dev_dbg(&rpdev->dev, "Qualcomm SMD QRTR driver probed, held packets: %u\n",
+		held);
 
 	return 0;
 }

base-commit: 90ae4042bd9f35a9098d14b1a12a43df9b326e2b
-- 
2.53.0


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

* Re: [PATCH net] net: qrtr: smd: hold packets that arrive before probe has finished
  2026-12-03 17:05 [PATCH net] net: qrtr: smd: hold packets that arrive before probe has finished Andrey Derkach
@ 2026-12-04 17:46 ` netdev-bot+sashiko
  2026-12-04 21:06   ` Andrey Derkach
  0 siblings, 1 reply; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-12-04 17:46 UTC (permalink / raw)
  To: gabin278
  Cc: mani, davem, edumazet, kuba, pabeni, willemb, horms, andersson,
	netdev, linux-arm-msm

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] On SMD, a packet held during probe can now be lost, and
  nothing logs it.
- [Medium] While drvdata is NULL, the remote can fill the held queue
  without limit.

--

net: qrtr: smd: hold packets that arrive before probe has finished

This patch changes qrtr-smd so that packets arriving before probe has set
the driver data are held in a devres-backed sk_buff_head. Once the QRTR
endpoint is registered, the held packets are posted and drvdata is
published under a global spinlock. The goal is to stop losing the remote's
HELLO on GLINK, which ignores the callback's return value.

> diff --git a/net/qrtr/smd.c b/net/qrtr/smd.c
> index 8b30b8275a2d9..e9e984db1e961 100644
> --- a/net/qrtr/smd.c
> +++ b/net/qrtr/smd.c

[ ... ]

> @@ -36,6 +49,91 @@ static int qcom_smd_qrtr_callback(struct rpmsg_device *rpdev,
>  	return rc;
>  }
>  
> +static int qcom_smd_qrtr_hold(struct rpmsg_device *rpdev, void *data, int len)
> +{

[ ... ]

> +	skb = alloc_skb(len, GFP_ATOMIC);
> +	if (!skb) {
> +		rc = -ENOMEM;
> +		goto out;
> +	}
> +	skb_put_data(skb, data, len);
> +	__skb_queue_tail(held, skb);

[Severity: Medium]
Is there a limit on how much the remote can queue here while drvdata is
NULL?

Each packet gets a GFP_ATOMIC copy, which is added to the held queue
without any check on qlen or total bytes, and the function then returns 0.
qcom_smd_channel_recv_single() only skips qcom_smd_channel_advance() when
the callback returns a negative value. So SMD keeps reading from the FIFO.
What used to be back-pressure in shared memory turns into kernel
allocations.

The window includes qrtr_endpoint_register(), which does GFP_KERNEL
allocations and takes qrtr_node_lock. Under memory pressure the window can
get longer.

The window also seems to open again on unbind. rpmsg_dev_remove() calls
rpdrv->remove() before rpmsg_destroy_ept(), and qcom_smd_qrtr_remove()
does:

	dev_set_drvdata(&rpdev->dev, NULL);

Packets that arrive between those two calls are copied, only to be freed
by devres_release_all().

Later, qcom_smd_qrtr_post_held() drains the whole queue through
qrtr_endpoint_post(). That is one more GFP_ATOMIC allocation per packet,
plus sock_queue_rcv_skb() and the wakeups. All of it runs inside
spin_lock_irqsave() on the global qrtr_smd_held_lock, which every qrtr-smd
device shares.

Does the time with IRQs off on the probing CPU then grow with the number
of packets the remote queued?

Would it help to cap the held queue, returning -EAGAIN on SMD when the cap
is reached? The queue could also be spliced out under the lock, so that
the posting happens with IRQs enabled.

> +
> +out:
> +	spin_unlock_irqrestore(&qrtr_smd_held_lock, flags);
> +
> +	return rc;
> +}
> +
> +/* Post the packets held during probe, then publish @qdev so the callback
> + * posts directly. Both happen under the lock, so held packets are posted
> + * before any packet that arrives later.
> + */
> +static unsigned int qcom_smd_qrtr_post_held(struct rpmsg_device *rpdev,
> +					    struct qrtr_smd_dev *qdev)
> +{
> +	struct sk_buff_head *held;
> +	unsigned int count = 0;
> +	struct sk_buff *skb;
> +	unsigned long flags;
> +
> +	spin_lock_irqsave(&qrtr_smd_held_lock, flags);
> +
> +	held = devres_find(&rpdev->dev, qcom_smd_qrtr_held_release, NULL, NULL);
> +	if (held) {
> +		while ((skb = __skb_dequeue(held))) {
> +			qcom_smd_qrtr_post(qdev, skb->data, skb->len);
> +			consume_skb(skb);
> +			count++;
> +		}
> +	}

[Severity: Medium]
Can a held packet be lost here with nothing logged? The return value of
qcom_smd_qrtr_post() is ignored, and the skb is consumed and counted
whatever happens.

qrtr_endpoint_post() can return -ENOMEM:

	skb = __netdev_alloc_skb(NULL, len, GFP_ATOMIC | __GFP_NOWARN);
	if (!skb)
		return -ENOMEM;

Before this patch, a packet that arrived on SMD while drvdata was NULL got
-EAGAIN. qcom_smd_channel_recv_single() then left it in the RX FIFO to be
delivered again:

	ret = ept->cb(ept->rpdev, ptr, len, ept->priv, RPMSG_ADDR_ANY);
	if (ret < 0)
		return ret;

	/* Only forward the tail if the client consumed the data */
	qcom_smd_channel_advance(channel, len);

Now qcom_smd_qrtr_hold() copies the packet and returns 0. SMD advances the
tail, and the devres queue holds the only copy. If the GFP_ATOMIC
allocation fails during this drain, the packet is gone for good. That
packet is often the remote's HELLO. The "held packets: %u" debug line
still counts it as passed on.

The commit message doesn't mention that SMD moves from keeping the packet
in the FIFO for a retry to copying and holding it. Is that change
intended?

Could rc be checked here, so the code at least warns or keeps the packet?
Another option would be to keep returning -EAGAIN on SMD.

> +
> +	dev_set_drvdata(&rpdev->dev, qdev);
> +
> +	spin_unlock_irqrestore(&qrtr_smd_held_lock, flags);
> +
> +	return count;
> +}

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261203170532.3598521-1-gabin278%40gmail.com

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

* Re: [PATCH net] net: qrtr: smd: hold packets that arrive before probe has finished
  2026-12-04 17:46 ` netdev-bot+sashiko
@ 2026-12-04 21:06   ` Andrey Derkach
  0 siblings, 0 replies; 3+ messages in thread
From: Andrey Derkach @ 2026-12-04 21:06 UTC (permalink / raw)
  To: netdev-bot+sashiko
  Cc: Manivannan Sadhasivam, David S. Miller, Eric Dumazet,
	Jakub Kicinski, Paolo Abeni, Willem de Bruijn, Simon Horman,
	Bjorn Andersson, netdev, linux-arm-msm

On Fri, 04 Dec 2026 17:46:28 +0000, netdev-bot+sashiko@kernel.org wrote:
> - [Medium] On SMD, a packet held during probe can now be lost, and
>   nothing logs it.

Agreed. Before this patch SMD left such a packet in the FIFO, and
qcom_smd_announce_create() delivered it right after probe, so SMD
doesn't need the hold and only gains this failure case. In v2,
qcom_smd_qrtr_post_held() checks the return value, counts posted and
dropped packets, and warns when a held packet is dropped. The commit
message now says that SMD holds the packet instead of leaving it in
the FIFO.

> - [Medium] While drvdata is NULL, the remote can fill the held queue
>   without limit.

Agreed. In my tests at most 3 packets were held (the modem's HELLO and
two NEW_SERVER). v2 caps the queue at 32 packets. Once it is full,
the callback returns -EAGAIN, so SMD leaves the rest in the FIFO and
GLINK drops them, both as before the patch. The cap also bounds the
time spent posting under the lock. I kept the posting under the lock
so the held packets stay ahead of later ones; the callback already
runs in hard IRQ context on both SMD and GLINK.

pw-bot: cr

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

end of thread, other threads:[~2026-12-04 21:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 17:05 [PATCH net] net: qrtr: smd: hold packets that arrive before probe has finished Andrey Derkach
2026-12-04 17:46 ` netdev-bot+sashiko
2026-12-04 21:06   ` Andrey Derkach

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox