From: Tzung-Bi Shih <tzungbi@kernel.org>
To: bleung@chromium.org
Cc: tzungbi@kernel.org, dawidn@google.com, chrome-platform@lists.linux.dev
Subject: [PATCH 1/2] platform/chrome: cros_ec_chardev: Don't use wait queue's lock
Date: Mon, 21 Jul 2025 04:50:08 +0000 [thread overview]
Message-ID: <20250721045009.2737376-2-tzungbi@kernel.org> (raw)
In-Reply-To: <20250721045009.2737376-1-tzungbi@kernel.org>
Lock inside wait_queue_head_t is not designed for protecting custom
data structures. Don't use it.
Use a dedicated lock for the structure instead.
Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
---
drivers/platform/chrome/cros_ec_chardev.c | 40 +++++++++++------------
1 file changed, 19 insertions(+), 21 deletions(-)
diff --git a/drivers/platform/chrome/cros_ec_chardev.c b/drivers/platform/chrome/cros_ec_chardev.c
index 1cb09ca3b850..5e9ff70c6cf5 100644
--- a/drivers/platform/chrome/cros_ec_chardev.c
+++ b/drivers/platform/chrome/cros_ec_chardev.c
@@ -11,6 +11,7 @@
*/
#include <linux/init.h>
+#include <linux/cleanup.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
@@ -23,6 +24,7 @@
#include <linux/platform_device.h>
#include <linux/poll.h>
#include <linux/slab.h>
+#include <linux/spinlock.h>
#include <linux/types.h>
#include <linux/uaccess.h>
@@ -37,6 +39,8 @@ struct chardev_priv {
wait_queue_head_t wait_event;
unsigned long event_mask;
struct list_head events;
+ /* This protects `events`. */
+ spinlock_t lock;
size_t event_len;
u16 cmd_offset;
};
@@ -109,11 +113,10 @@ static int cros_ec_chardev_mkbp_event(struct notifier_block *nb,
event->event_type = ec_dev->event_data.event_type;
memcpy(event->data, &ec_dev->event_data.data, ec_dev->event_size);
- spin_lock(&priv->wait_event.lock);
+ guard(spinlock_irq)(&priv->lock);
list_add_tail(&event->node, &priv->events);
priv->event_len += total_size;
- wake_up_locked(&priv->wait_event);
- spin_unlock(&priv->wait_event.lock);
+ wake_up(&priv->wait_event);
return NOTIFY_OK;
}
@@ -124,30 +127,23 @@ static struct ec_event *cros_ec_chardev_fetch_event(struct chardev_priv *priv,
struct ec_event *event;
int err;
- spin_lock(&priv->wait_event.lock);
- if (!block && list_empty(&priv->events)) {
- event = ERR_PTR(-EWOULDBLOCK);
- goto out;
- }
+ guard(spinlock_irq)(&priv->lock);
- if (!fetch) {
- event = NULL;
- goto out;
- }
+ if (!block && list_empty(&priv->events))
+ return ERR_PTR(-EWOULDBLOCK);
- err = wait_event_interruptible_locked(priv->wait_event,
- !list_empty(&priv->events));
- if (err) {
- event = ERR_PTR(err);
- goto out;
- }
+ if (!fetch)
+ return NULL;
+
+ err = wait_event_interruptible_lock_irq(priv->wait_event,
+ !list_empty(&priv->events),
+ priv->lock);
+ if (err)
+ return ERR_PTR(err);
event = list_first_entry(&priv->events, struct ec_event, node);
list_del(&event->node);
priv->event_len -= sizeof(*event) + event->size;
-
-out:
- spin_unlock(&priv->wait_event.lock);
return event;
}
@@ -170,6 +166,7 @@ static int cros_ec_chardev_open(struct inode *inode, struct file *filp)
priv->cmd_offset = ec->cmd_offset;
filp->private_data = priv;
INIT_LIST_HEAD(&priv->events);
+ spin_lock_init(&priv->lock);
init_waitqueue_head(&priv->wait_event);
nonseekable_open(inode, filp);
@@ -191,6 +188,7 @@ static __poll_t cros_ec_chardev_poll(struct file *filp, poll_table *wait)
poll_wait(filp, &priv->wait_event, wait);
+ guard(spinlock_irq)(&priv->lock);
if (list_empty(&priv->events))
return 0;
--
2.50.0.727.gbf7dc18ff4-goog
next prev parent reply other threads:[~2025-07-21 4:50 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-21 4:50 [PATCH 0/2] platform/chrome: cros_ec_chardev: To be ec_dev registered-aware Tzung-Bi Shih
2025-07-21 4:50 ` Tzung-Bi Shih [this message]
2025-07-21 4:50 ` [PATCH 2/2] platform/chrome: cros_ec_chardev: Check ec_dev's availability in fops Tzung-Bi Shih
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=20250721045009.2737376-2-tzungbi@kernel.org \
--to=tzungbi@kernel.org \
--cc=bleung@chromium.org \
--cc=chrome-platform@lists.linux.dev \
--cc=dawidn@google.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