mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: KhaiWenTan <khai.wen.tan@linux.intel.com>
To: Rodolfo Giometti <giometti@enneenne.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Jonathan Corbet <corbet@lwn.net>
Cc: Thomas Gleixner <tglx@kernel.org>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	david.zage@intel.com, weifeng.voon@intel.com,
	hong.aun.looi@intel.com, christopher.s.hall@intel.com,
	khai.wen.tan@intel.com, linux-kernel@vger.kernel.org,
	linux-doc@vger.kernel.org
Subject: [PATCH 1/4] pps: generators: synchronize sysfs and registration failure
Date: Thu,  3 Dec 2026 01:21:55 +0800	[thread overview]
Message-ID: <20261202172254.1188109-2-khai.wen.tan@linux.intel.com> (raw)
In-Reply-To: <20261202172254.1188109-1-khai.wen.tan@linux.intel.com>

From: Voon Weifeng <weifeng.voon@intel.com>

Serialize sysfs callbacks with ioctls and source removal using the
existing info lock. Reject opens and report a hangup after removal.

If cdev_device_add() fails after making the character device available,
stop the generator, invalidate its driver info and wake event readers
before releasing the device.

Fixes: 86b525bed275 ("drivers pps: add PPS generators support")
Signed-off-by: Voon Weifeng <weifeng.voon@intel.com>
Signed-off-by: KhaiWenTan <khai.wen.tan@linux.intel.com>
---
 drivers/pps/generators/pps_gen.c | 20 ++++++++++++++++++--
 drivers/pps/generators/sysfs.c   | 27 +++++++++++++++++++++++----
 2 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/drivers/pps/generators/pps_gen.c b/drivers/pps/generators/pps_gen.c
index 474e17c36458..8f68061b8698 100644
--- a/drivers/pps/generators/pps_gen.c
+++ b/drivers/pps/generators/pps_gen.c
@@ -42,6 +42,8 @@ static __poll_t pps_gen_cdev_poll(struct file *file, poll_table *wait)
 	struct pps_gen_device *pps_gen = file->private_data;
 
 	poll_wait(file, &pps_gen->queue, wait);
+	if (!READ_ONCE(pps_gen->info))
+		return EPOLLHUP | EPOLLERR;
 	return EPOLLIN | EPOLLRDNORM;
 }
 
@@ -134,10 +136,18 @@ static int pps_gen_cdev_open(struct inode *inode, struct file *file)
 {
 	struct pps_gen_device *pps_gen = container_of(inode->i_cdev,
 				struct pps_gen_device, cdev);
+	int ret = 0;
 
 	get_device(&pps_gen->dev);
-	file->private_data = pps_gen;
-	return 0;
+	mutex_lock(&pps_gen->info_lock);
+	if (pps_gen->info)
+		file->private_data = pps_gen;
+	else
+		ret = -ENODEV;
+	mutex_unlock(&pps_gen->info_lock);
+	if (ret)
+		put_device(&pps_gen->dev);
+	return ret;
 }
 
 static int pps_gen_cdev_release(struct inode *inode, struct file *file)
@@ -209,6 +219,12 @@ static int pps_gen_register_cdev(struct pps_gen_device *pps_gen)
 	if (err) {
 		pr_err("failed to add char device %d:%d\n",
 				MAJOR(pps_gen_devt), pps_gen->id);
+		mutex_lock(&pps_gen->info_lock);
+		if (pps_gen->enabled)
+			pps_gen->info->enable(pps_gen, false);
+		WRITE_ONCE(pps_gen->info, NULL);
+		mutex_unlock(&pps_gen->info_lock);
+		wake_up_interruptible_all(&pps_gen->queue);
 		goto put_dev;
 	}
 
diff --git a/drivers/pps/generators/sysfs.c b/drivers/pps/generators/sysfs.c
index 6d6bc0006fea..43d833383fe9 100644
--- a/drivers/pps/generators/sysfs.c
+++ b/drivers/pps/generators/sysfs.c
@@ -18,8 +18,15 @@ static ssize_t system_show(struct device *dev, struct device_attribute *attr,
 				char *buf)
 {
 	struct pps_gen_device *pps_gen = dev_get_drvdata(dev);
+	ssize_t ret;
 
-	return sysfs_emit(buf, "%d\n", pps_gen->info->use_system_clock);
+	mutex_lock(&pps_gen->info_lock);
+	if (pps_gen->info)
+		ret = sysfs_emit(buf, "%d\n", pps_gen->info->use_system_clock);
+	else
+		ret = -ENODEV;
+	mutex_unlock(&pps_gen->info_lock);
+	return ret;
 }
 static DEVICE_ATTR_RO(system);
 
@@ -30,7 +37,12 @@ static ssize_t time_show(struct device *dev, struct device_attribute *attr,
 	struct timespec64 time;
 	int ret;
 
-	ret = pps_gen->info->get_time(pps_gen, &time);
+	mutex_lock(&pps_gen->info_lock);
+	if (pps_gen->info)
+		ret = pps_gen->info->get_time(pps_gen, &time);
+	else
+		ret = -ENODEV;
+	mutex_unlock(&pps_gen->info_lock);
 	if (ret)
 		return ret;
 
@@ -49,10 +61,17 @@ static ssize_t enable_store(struct device *dev, struct device_attribute *attr,
 	if (ret)
 		return ret;
 
-	ret = pps_gen->info->enable(pps_gen, status);
+	mutex_lock(&pps_gen->info_lock);
+	if (pps_gen->info) {
+		ret = pps_gen->info->enable(pps_gen, status);
+		if (!ret)
+			pps_gen->enabled = status;
+	} else {
+		ret = -ENODEV;
+	}
+	mutex_unlock(&pps_gen->info_lock);
 	if (ret)
 		return ret;
-	pps_gen->enabled = status;
 
 	return count;
 }
-- 
2.43.0


  reply	other threads:[~2026-12-02  9:08 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 17:21 [PATCH 0/4] pps: add Intel Timed I/O PPS input support KhaiWenTan
2026-12-02 17:21 ` KhaiWenTan [this message]
2026-12-02 17:21 ` [PATCH 2/4] pps: generators: cancel TIO timer outside spinlock KhaiWenTan
2026-12-02 17:21 ` [PATCH 3/4] pps: generators: move TIO ownership to auxiliary parent KhaiWenTan
2026-12-02 17:21 ` [PATCH 4/4] pps: clients: add Intel TIO PPS capture KhaiWenTan

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=20261202172254.1188109-2-khai.wen.tan@linux.intel.com \
    --to=khai.wen.tan@linux.intel.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=christopher.s.hall@intel.com \
    --cc=corbet@lwn.net \
    --cc=david.zage@intel.com \
    --cc=giometti@enneenne.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=hong.aun.looi@intel.com \
    --cc=khai.wen.tan@intel.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tglx@kernel.org \
    --cc=weifeng.voon@intel.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

all inboxes | Powered by JetHome®