linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Wen Yang <wen.yang@linux.dev>
To: Sudeep Holla <sudeep.holla@arm.com>,
	Cristian Marussi <cristian.marussi@arm.com>
Cc: Wen Yang <wen.yang@linux.dev>,
	arm-scmi@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH] firmware: arm_scmi: protect xfer->async_done with xfer->lock when changing and using it
Date: Tue, 27 Aug 2024 22:32:37 +0800	[thread overview]
Message-ID: <20240827143237.10208-1-wen.yang@linux.dev> (raw)

do_xfer_with_response() first assigns xfer->async_done to &async_response,
then calls wait_for_completion_timeout(), and finally sets xfer->async_done
to null.
However, scmi_handle_response() may calls complete(xfer->async_done) while
xfer->async_done is null, may causing a crash.

Protect xfer->async_done with xfer->lock when changing and using it,
just like scmi_msg_response_validate().

Signed-off-by: Wen Yang <wen.yang@linux.dev>
Cc: Sudeep Holla <sudeep.holla@arm.com>
Cc: Cristian Marussi <cristian.marussi@arm.com>
Cc: arm-scmi@vger.kernel.org
Cc: linux-arm-kernel@lists.infradead.org
Cc: linux-kernel@vger.kernel.org
---
 drivers/firmware/arm_scmi/driver.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c
index 66806578df5a..24ed94e3cbd4 100644
--- a/drivers/firmware/arm_scmi/driver.c
+++ b/drivers/firmware/arm_scmi/driver.c
@@ -1110,6 +1110,7 @@ static void scmi_handle_response(struct scmi_chan_info *cinfo,
 {
 	struct scmi_xfer *xfer;
 	struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
+	unsigned long flags;
 
 	xfer = scmi_xfer_command_acquire(cinfo, msg_hdr);
 	if (IS_ERR(xfer)) {
@@ -1144,7 +1145,10 @@ static void scmi_handle_response(struct scmi_chan_info *cinfo,
 
 	if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP) {
 		scmi_clear_channel(info, cinfo);
-		complete(xfer->async_done);
+		spin_lock_irqsave(&xfer->lock, flags);
+		if (xfer->async_done)
+			complete(xfer->async_done);
+		spin_unlock_irqrestore(&xfer->lock, flags);
 		scmi_inc_count(info->dbg->counters, DELAYED_RESPONSE_OK);
 	} else {
 		complete(&xfer->done);
@@ -1479,9 +1483,13 @@ static int do_xfer_with_response(const struct scmi_protocol_handle *ph,
 				 struct scmi_xfer *xfer)
 {
 	int ret, timeout = msecs_to_jiffies(SCMI_MAX_RESPONSE_TIMEOUT);
+	unsigned long flags;
+
 	DECLARE_COMPLETION_ONSTACK(async_response);
 
+	spin_lock_irqsave(&xfer->lock, flags);
 	xfer->async_done = &async_response;
+	spin_unlock_irqrestore(&xfer->lock, flags);
 
 	/*
 	 * Delayed responses should not be polled, so an async command should
@@ -1503,7 +1511,10 @@ static int do_xfer_with_response(const struct scmi_protocol_handle *ph,
 		}
 	}
 
+	spin_lock_irqsave(&xfer->lock, flags);
 	xfer->async_done = NULL;
+	spin_unlock_irqrestore(&xfer->lock, flags);
+
 	return ret;
 }
 
-- 
2.25.1



                 reply	other threads:[~2024-08-27 14:35 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=20240827143237.10208-1-wen.yang@linux.dev \
    --to=wen.yang@linux.dev \
    --cc=arm-scmi@vger.kernel.org \
    --cc=cristian.marussi@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sudeep.holla@arm.com \
    /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;
as well as URLs for NNTP newsgroup(s).