mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Eugen Hristev <ehristev@kernel.org>
To: Vinod Koul <vkoul@kernel.org>, Frank Li <Frank.Li@kernel.org>
Cc: dmaengine@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Eugen Hristev <ehristev@kernel.org>
Subject: [PATCH] dmaengine: pl330: Bound UNTIL loop
Date: Wed, 04 Nov 2026 23:45:32 +0200	[thread overview]
Message-ID: <20261104-b4-pl330-until-v1-1-b541929da163@kernel.org> (raw)

Bound UNTIL loop with a timeout.
It may happen that by accident pl330 will read/write a wrong address
and generate a bus fault. In such cases it will not complete the
condition that UNTIL waits for. To have the driver continue and not
hang, use a timeout.

Assisted-by: Github Copilot
Signed-off-by: Eugen Hristev <ehristev@kernel.org>
---
 drivers/dma/pl330.c | 36 ++++++++++++++++++++++++++++++------
 1 file changed, 30 insertions(+), 6 deletions(-)

diff --git a/drivers/dma/pl330.c b/drivers/dma/pl330.c
index c2d0b473b8f4..9ced60fef9ff 100644
--- a/drivers/dma/pl330.c
+++ b/drivers/dma/pl330.c
@@ -248,8 +248,15 @@ enum pl330_byteswap {
  */
 #define MCODE_BUFF_PER_REQ	256
 
-/* Use this _only_ to wait on transient states */
-#define UNTIL(t, s)	while (!(_state(t) & (s))) cpu_relax();
+/* returns true if state reached within loop budget */
+#define UNTIL(t, s, loops) \
+	({ \
+		unsigned long _l = (loops); \
+		while (!(_state(t) & (s)) && _l) { \
+			cpu_relax(); _l--; \
+		 } \
+		!!(_state(t) & (s)); \
+	 })
 
 #ifdef PL330_DEBUG_MCGEN
 static unsigned cmd_line;
@@ -979,7 +986,8 @@ static void _stop(struct pl330_thread *thrd)
 	u32 inten = readl(regs + INTEN);
 
 	if (_state(thrd) == PL330_STATE_FAULT_COMPLETING)
-		UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
+		UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING,
+		      msecs_to_loops(5));
 
 	/* Return if nothing needs to be done */
 	if (_state(thrd) == PL330_STATE_COMPLETING
@@ -1059,12 +1067,24 @@ static bool _trigger(struct pl330_thread *thrd)
 
 static bool pl330_start_thread(struct pl330_thread *thrd)
 {
+	/* msecs_to_loops(5) gives the same budget used by _until_dmac_idle */
+	const unsigned long tout = msecs_to_loops(5);
+
 	switch (_state(thrd)) {
 	case PL330_STATE_FAULT_COMPLETING:
-		UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING);
+		if (!UNTIL(thrd, PL330_STATE_FAULTING | PL330_STATE_KILLING,
+			   tout)) {
+			dev_err(thrd->dmac->ddma.dev,
+				"pl330: thread stuck in FAULT_COMPLETING\n");
+			return false;
+		}
 
 		if (_state(thrd) == PL330_STATE_KILLING)
-			UNTIL(thrd, PL330_STATE_STOPPED)
+			if (!UNTIL(thrd, PL330_STATE_STOPPED, tout)) {
+				dev_err(thrd->dmac->ddma.dev,
+					"pl330: thread stuck KILLING->STOPPED\n");
+				return false;
+			}
 		fallthrough;
 
 	case PL330_STATE_FAULTING:
@@ -1073,7 +1093,11 @@ static bool pl330_start_thread(struct pl330_thread *thrd)
 
 	case PL330_STATE_KILLING:
 	case PL330_STATE_COMPLETING:
-		UNTIL(thrd, PL330_STATE_STOPPED)
+		if (!UNTIL(thrd, PL330_STATE_STOPPED, tout)) {
+			dev_err(thrd->dmac->ddma.dev,
+				"pl330: thread stuck, not reaching STOPPED\n");
+			return false;
+		}
 		fallthrough;
 
 	case PL330_STATE_STOPPED:

---
base-commit: 6d0933ae9d46674a6ea63a87e9e9ab4e662670c9
change-id: 20261104-b4-pl330-until-8a287981ed5d

Best regards,
--  
Eugen Hristev <ehristev@kernel.org>


                 reply	other threads:[~2026-11-04 21:45 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261104-b4-pl330-until-v1-1-b541929da163@kernel.org \
    --to=ehristev@kernel.org \
    --cc=Frank.Li@kernel.org \
    --cc=dmaengine@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=vkoul@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®