mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Alison Wang <alison.wang@nxp.com>
To: gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org
Cc: leoyang.li@nxp.com, xuelin.shi@nxp.com, xiaofeng.ren@nxp.com,
	feng.guo@nxp.com, Alison Wang <alison.wang@nxp.com>
Subject: [PATCH 8/8] ethosu: Add rwlock when alloc and remove msg id
Date: Fri, 16 Jun 2023 13:59:13 +0800	[thread overview]
Message-ID: <20230616055913.2360-9-alison.wang@nxp.com> (raw)
In-Reply-To: <20230616055913.2360-1-alison.wang@nxp.com>

The msg id will be removed unpredictable when one thread calls
ethosu_rpmsg_register and another thread calls ethosu_rpmsg_deregister
at the same time. This patch adds write_lock in
ethosu_rpmsg_register/ethosu_rpmsg_deregister and adds read_lcok in
ethosu_rpmsg_find.

Signed-off-by: Feng Guo <feng.guo@nxp.com>
Reviewed-by: Peng Fan <peng.fan@nxp.com>
---
 drivers/firmware/ethosu/ethosu_driver.c |  2 +-
 drivers/firmware/ethosu/ethosu_rpmsg.c  | 20 ++++++++++++++------
 drivers/firmware/ethosu/ethosu_rpmsg.h  |  1 +
 3 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/drivers/firmware/ethosu/ethosu_driver.c b/drivers/firmware/ethosu/ethosu_driver.c
index 59ae79256564..0742564cfaf2 100644
--- a/drivers/firmware/ethosu/ethosu_driver.c
+++ b/drivers/firmware/ethosu/ethosu_driver.c
@@ -155,7 +155,7 @@ static int __init ethosu_init(void)
 {
 	int ret;
 
-	ethosu_class = class_create(THIS_MODULE, ETHOSU_DRIVER_NAME);
+	ethosu_class = class_create(ETHOSU_DRIVER_NAME);
 	if (IS_ERR(ethosu_class)) {
 		printk("Failed to create class '%s'.\n", ETHOSU_DRIVER_NAME);
 
diff --git a/drivers/firmware/ethosu/ethosu_rpmsg.c b/drivers/firmware/ethosu/ethosu_rpmsg.c
index 351a1046e65e..d3e597e82762 100644
--- a/drivers/firmware/ethosu/ethosu_rpmsg.c
+++ b/drivers/firmware/ethosu/ethosu_rpmsg.c
@@ -35,7 +35,9 @@ static void ethosu_core_set_capacity(struct ethosu_buffer *buf,
 int ethosu_rpmsg_register(struct ethosu_rpmsg *erp,
 			  struct ethosu_rpmsg_msg *msg)
 {
+	write_lock(&erp->lock);
 	msg->id = idr_alloc_cyclic(&erp->msg_idr, msg, 0, INT_MAX, GFP_KERNEL);
+	write_unlock(&erp->lock);
 	if (msg->id < 0)
 		return msg->id;
 
@@ -45,14 +47,19 @@ int ethosu_rpmsg_register(struct ethosu_rpmsg *erp,
 void ethosu_rpmsg_deregister(struct ethosu_rpmsg *erp,
 			     struct ethosu_rpmsg_msg *msg)
 {
+	write_lock(&erp->lock);
 	idr_remove(&erp->msg_idr, msg->id);
+	write_unlock(&erp->lock);
 }
 
 struct ethosu_rpmsg_msg *ethosu_rpmsg_find(struct ethosu_rpmsg *erp,
 					   int msg_id)
 {
-	struct ethosu_rpmsg_msg *ptr =
-		(struct ethosu_rpmsg_msg *)idr_find(&erp->msg_idr, msg_id);
+	struct ethosu_rpmsg_msg *ptr;
+
+	read_lock(&erp->lock);
+	ptr = (struct ethosu_rpmsg_msg *)idr_find(&erp->msg_idr, msg_id);
+	read_unlock(&erp->lock);
 
 	if (!ptr)
 		return ERR_PTR(-EINVAL);
@@ -97,8 +104,8 @@ static int ethosu_rpmsg_send(struct ethosu_rpmsg *erp, uint32_t type)
 	msg.type = type;
 	msg.length = 0;
 
-	print_hex_dump(KERN_DEBUG, __func__, DUMP_PREFIX_NONE, 16, 1,
-			(void *)&msg, sizeof(msg),  true);
+	print_hex_dump_debug(__func__, DUMP_PREFIX_NONE, 16, 1, (void *)&msg,
+			     sizeof(msg), true);
 
 	ret = rpmsg_send(rpdev->ept, (void *)&msg, sizeof(msg));
 	if (ret) {
@@ -337,8 +344,8 @@ static int rpmsg_ethosu_cb(struct rpmsg_device *rpdev,
 
 	dev_dbg(&rpdev->dev, "msg(<- src 0x%x) len %d\n", src, len);
 
-	print_hex_dump(KERN_DEBUG, __func__, DUMP_PREFIX_NONE, 16, 1,
-			data, len,  true);
+	print_hex_dump_debug(__func__, DUMP_PREFIX_NONE, 16, 1, data,
+			     len, true);
 
 	rpmsg->callback(rpmsg->user_arg, data);
 
@@ -391,6 +398,7 @@ int ethosu_rpmsg_init(struct ethosu_rpmsg *erp,
 	erp->user_arg = user_arg;
 	erp->ping_count = 0;
 	idr_init(&erp->msg_idr);
+        rwlock_init(&erp->lock);
 
 	return register_rpmsg_driver(&ethosu_rpmsg_driver);
 }
diff --git a/drivers/firmware/ethosu/ethosu_rpmsg.h b/drivers/firmware/ethosu/ethosu_rpmsg.h
index 283d401a0dbf..a4b879e9ef6c 100644
--- a/drivers/firmware/ethosu/ethosu_rpmsg.h
+++ b/drivers/firmware/ethosu/ethosu_rpmsg.h
@@ -21,6 +21,7 @@ struct ethosu_core_msg;
 typedef void (*ethosu_rpmsg_cb)(void *user_arg, void *data);
 
 struct ethosu_rpmsg {
+	rwlock_t		lock;
 	struct rpmsg_device	*rpdev;
 	ethosu_rpmsg_cb		callback;
 	void			*user_arg;
-- 
2.17.1


  parent reply	other threads:[~2023-06-16  5:53 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-16  5:59 [PATCH 0/8] ethosu: Add Arm Ethos-U driver Alison Wang
2023-06-16  5:59 ` [PATCH 1/8] " Alison Wang
2023-06-16  6:25   ` Greg KH
2023-06-16  8:28     ` [EXT] " Alison Wang
2023-06-16 15:08       ` Andrew Lunn
2023-07-06  6:01     ` Alison Wang
2023-06-16  6:26   ` Greg KH
2023-06-16  8:28     ` [EXT] " Alison Wang
2023-06-16 10:42   ` kernel test robot
2023-06-16 15:49   ` Robin Murphy
2023-06-16 17:16   ` kernel test robot
2023-06-24 18:30   ` kernel test robot
2023-06-16  5:59 ` [PATCH 2/8] ethosu: Use RPMsg messaging protocol based on i.MX Rpmsg implementation Alison Wang
2023-06-16  5:59 ` [PATCH 3/8] ethosu: Add inference type option for model and op Alison Wang
2023-06-16  5:59 ` [PATCH 4/8] ethosu: Add suspend/resume power management Alison Wang
2023-06-16  5:59 ` [PATCH 5/8] ethosu: Use ids for identifying messages sent to Ethos-U firmware Alison Wang
2023-06-16  5:59 ` [PATCH 6/8] ethosu: Add core message about network info Alison Wang
2023-06-16  5:59 ` [PATCH 7/8] ethosu: Add core message about inference cancellation Alison Wang
2023-06-16 16:13   ` kernel test robot
2023-06-16  5:59 ` Alison Wang [this message]
2023-06-16  6:22 ` [PATCH 0/8] ethosu: Add Arm Ethos-U driver Greg KH
2023-06-16  8:26   ` [EXT] " Alison Wang
2023-06-16  9:04     ` Greg KH
2023-06-16 10:47       ` Alison Wang
2023-06-16 11:11         ` Greg KH

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=20230616055913.2360-9-alison.wang@nxp.com \
    --to=alison.wang@nxp.com \
    --cc=feng.guo@nxp.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=leoyang.li@nxp.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=xiaofeng.ren@nxp.com \
    --cc=xuelin.shi@nxp.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

Powered by JetHome