public inbox for chrome-platform@lists.linux.dev
 help / color / mirror / Atom feed
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 2/2] platform/chrome: cros_ec_chardev: Check ec_dev's availability in fops
Date: Mon, 21 Jul 2025 04:50:09 +0000	[thread overview]
Message-ID: <20250721045009.2737376-3-tzungbi@kernel.org> (raw)
In-Reply-To: <20250721045009.2737376-1-tzungbi@kernel.org>

It is possible that a userland program is waiting for further MKBP
events while the underlying ec_dev is already unregistered.

Check ec_dev's availability in the fops to avoid the userland program
from waiting for some events that should never happen.

Note: if the userland program uses poll() without a timeout, e.g.:
: fd = os.open('/dev/cros_ec', os.O_RDONLY)
: fcntl.ioctl(fd, 0xec02, 0xfff)
: p = select.poll()
: p.register(fd, select.POLLIN)
:
: while True:
:     es = p.poll()            # <--- without a timeout
:     if not es:
:         print("no events")
:         continue
:
:     for f, e in es:
:         if e & select.POLLIN:
:             data = os.read(f, 100)
:             print(data)

It'd still block.  The patch doesn't help with the case.

Signed-off-by: Tzung-Bi Shih <tzungbi@kernel.org>
---
 drivers/platform/chrome/cros_ec_chardev.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/drivers/platform/chrome/cros_ec_chardev.c b/drivers/platform/chrome/cros_ec_chardev.c
index 5e9ff70c6cf5..675bcca16cb5 100644
--- a/drivers/platform/chrome/cros_ec_chardev.c
+++ b/drivers/platform/chrome/cros_ec_chardev.c
@@ -135,11 +135,17 @@ static struct ec_event *cros_ec_chardev_fetch_event(struct chardev_priv *priv,
 	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);
+	do {
+		err = wait_event_interruptible_lock_irq_timeout(priv->wait_event,
+								!list_empty(&priv->events),
+								priv->lock,
+								HZ);
+		if (err < 0)
+			return ERR_PTR(err);
+
+		if (!cros_ec_device_registered(priv->ec_dev))
+			return ERR_PTR(-ENODEV);
+	} while (!err);
 
 	event = list_first_entry(&priv->events, struct ec_event, node);
 	list_del(&event->node);
@@ -186,6 +192,9 @@ static __poll_t cros_ec_chardev_poll(struct file *filp, poll_table *wait)
 {
 	struct chardev_priv *priv = filp->private_data;
 
+	if (!cros_ec_device_registered(priv->ec_dev))
+		return EPOLLERR;
+
 	poll_wait(filp, &priv->wait_event, wait);
 
 	guard(spinlock_irq)(&priv->lock);
-- 
2.50.0.727.gbf7dc18ff4-goog


      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 ` [PATCH 1/2] platform/chrome: cros_ec_chardev: Don't use wait queue's lock Tzung-Bi Shih
2025-07-21  4:50 ` Tzung-Bi Shih [this message]

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-3-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