mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Ionut Nechita (Wind River)" <ionut.nechita@windriver.com>
To: Xu Yilun <yilun.xu@linux.intel.com>, Moritz Fischer <mdf@kernel.org>
Cc: Xu Yilun <yilun.xu@intel.com>, Tom Rix <trix@redhat.com>,
	Peter Colberg <peter.colberg@intel.com>,
	linux-fpga@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [RFC PATCH v2 1/2] fpga: dfl: sync device file operations with feature device removal
Date: Mon, 30 Nov 2026 20:21:31 +0200	[thread overview]
Message-ID: <20261130182132.56809-2-ionut.nechita@windriver.com> (raw)
In-Reply-To: <20261130182132.56809-1-ionut.nechita@windriver.com>

From: Ionut Nechita <ionut.nechita@windriver.com>

The device file of an FME or a port can stay open after its feature
device is removed, for example when the card is removed for an FPGA
image reload. The file operations keep using the feature device data,
which is allocated with devm on the parent PCI device and freed when
dfl-pci is unbound, so a later ioctl, mmap or release() accesses freed
memory. Nothing orders the operations in progress against the removal
either.

Add a rw_semaphore to the feature platform data, which an open file
keeps alive, and hold it for read around the file operations. When the
cdev ops are unregistered, take it for write and mark them unregistered:
operations in progress finish first, later ones fail with -ENODEV, and
release() no longer touches the device.

The DMA map ioctl takes mmap_lock with the semaphore held, and mmap is
called with mmap_lock held, so mmap only trylocks the semaphore.

Fixes: 39ea74e33edc ("fpga: dfl: factor out feature device data from platform device data")
Signed-off-by: Ionut Nechita <ionut.nechita@windriver.com>
---
 drivers/fpga/dfl-afu-main.c | 75 ++++++++++++++++++++++++-------
 drivers/fpga/dfl-fme-main.c | 48 ++++++++++++++++----
 drivers/fpga/dfl.c          | 88 ++++++++++++++++++++++++++++++++++++-
 drivers/fpga/dfl.h          | 26 ++++++++---
 4 files changed, 203 insertions(+), 34 deletions(-)

diff --git a/drivers/fpga/dfl-afu-main.c b/drivers/fpga/dfl-afu-main.c
index 245468b7b9220..1caf60a19687c 100644
--- a/drivers/fpga/dfl-afu-main.c
+++ b/drivers/fpga/dfl-afu-main.c
@@ -602,31 +602,40 @@ static struct dfl_feature_driver port_feature_drvs[] = {
 
 static int afu_open(struct inode *inode, struct file *filp)
 {
-	struct dfl_feature_dev_data *fdata = dfl_fpga_inode_to_feature_dev_data(inode);
-	struct platform_device *fdev = fdata->dev;
+	struct dfl_feature_platform_data *pdata = dfl_fpga_inode_to_pdata(inode);
+	struct dfl_feature_dev_data *fdata;
 	int ret;
 
+	fdata = dfl_fpga_dev_ops_enter(pdata);
+	if (!fdata)
+		return -ENODEV;
+
 	mutex_lock(&fdata->lock);
 	ret = dfl_feature_dev_use_begin(fdata, filp->f_flags & O_EXCL);
 	if (!ret) {
-		dev_dbg(&fdev->dev, "Device File Opened %d Times\n",
+		dev_dbg(&fdata->dev->dev, "Device File Opened %d Times\n",
 			dfl_feature_dev_use_count(fdata));
-		filp->private_data = fdev;
+		filp->private_data = pdata;
 	}
 	mutex_unlock(&fdata->lock);
 
+	dfl_fpga_dev_ops_exit(pdata);
+
 	return ret;
 }
 
 static int afu_release(struct inode *inode, struct file *filp)
 {
-	struct platform_device *pdev = filp->private_data;
+	struct dfl_feature_platform_data *pdata = filp->private_data;
 	struct dfl_feature_dev_data *fdata;
 	struct dfl_feature *feature;
 
-	dev_dbg(&pdev->dev, "Device File Release\n");
+	/* The device is gone, and so may be fdata. */
+	fdata = dfl_fpga_dev_ops_enter(pdata);
+	if (!fdata)
+		return 0;
 
-	fdata = to_dfl_feature_dev_data(&pdev->dev);
+	dev_dbg(&fdata->dev->dev, "Device File Release\n");
 
 	mutex_lock(&fdata->lock);
 	dfl_feature_dev_use_end(fdata);
@@ -640,6 +649,8 @@ static int afu_release(struct inode *inode, struct file *filp)
 	}
 	mutex_unlock(&fdata->lock);
 
+	dfl_fpga_dev_ops_exit(pdata);
+
 	return 0;
 }
 
@@ -760,17 +771,15 @@ afu_ioctl_dma_unmap(struct dfl_feature_dev_data *fdata, void __user *arg)
 	return afu_dma_unmap_region(fdata, unmap.iova);
 }
 
-static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+static long __afu_ioctl(struct dfl_feature_dev_data *fdata, unsigned int cmd,
+			unsigned long arg)
 {
-	struct platform_device *pdev = filp->private_data;
-	struct dfl_feature_dev_data *fdata;
+	struct platform_device *pdev = fdata->dev;
 	struct dfl_feature *f;
 	long ret;
 
 	dev_dbg(&pdev->dev, "%s cmd 0x%x\n", __func__, cmd);
 
-	fdata = to_dfl_feature_dev_data(&pdev->dev);
-
 	switch (cmd) {
 	case DFL_FPGA_GET_API_VERSION:
 		return DFL_FPGA_API_VERSION;
@@ -802,17 +811,33 @@ static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 	return -EINVAL;
 }
 
+static long afu_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+{
+	struct dfl_feature_platform_data *pdata = filp->private_data;
+	struct dfl_feature_dev_data *fdata;
+	long ret;
+
+	fdata = dfl_fpga_dev_ops_enter(pdata);
+	if (!fdata)
+		return -ENODEV;
+
+	ret = __afu_ioctl(fdata, cmd, arg);
+
+	dfl_fpga_dev_ops_exit(pdata);
+
+	return ret;
+}
+
 static const struct vm_operations_struct afu_vma_ops = {
 #ifdef CONFIG_HAVE_IOREMAP_PROT
 	.access = generic_access_phys,
 #endif
 };
 
-static int afu_mmap(struct file *filp, struct vm_area_struct *vma)
+static int __afu_mmap(struct dfl_feature_dev_data *fdata,
+		      struct vm_area_struct *vma)
 {
-	struct platform_device *pdev = filp->private_data;
 	u64 size = vma->vm_end - vma->vm_start;
-	struct dfl_feature_dev_data *fdata;
 	struct dfl_afu_mmio_region region;
 	u64 offset;
 	int ret;
@@ -820,8 +845,6 @@ static int afu_mmap(struct file *filp, struct vm_area_struct *vma)
 	if (!(vma->vm_flags & VM_SHARED))
 		return -EINVAL;
 
-	fdata = to_dfl_feature_dev_data(&pdev->dev);
-
 	offset = vma->vm_pgoff << PAGE_SHIFT;
 	ret = afu_mmio_region_get_by_offset(fdata, offset, size, &region);
 	if (ret)
@@ -847,6 +870,24 @@ static int afu_mmap(struct file *filp, struct vm_area_struct *vma)
 			size, vma->vm_page_prot);
 }
 
+static int afu_mmap(struct file *filp, struct vm_area_struct *vma)
+{
+	struct dfl_feature_platform_data *pdata = filp->private_data;
+	struct dfl_feature_dev_data *fdata;
+	int ret;
+
+	/* mmap_lock is held, see dfl_fpga_dev_ops_tryenter() */
+	fdata = dfl_fpga_dev_ops_tryenter(pdata);
+	if (!fdata)
+		return -ENODEV;
+
+	ret = __afu_mmap(fdata, vma);
+
+	dfl_fpga_dev_ops_exit(pdata);
+
+	return ret;
+}
+
 static const struct file_operations afu_fops = {
 	.owner = THIS_MODULE,
 	.open = afu_open,
diff --git a/drivers/fpga/dfl-fme-main.c b/drivers/fpga/dfl-fme-main.c
index b5f22d97742e5..10473dd13bfc3 100644
--- a/drivers/fpga/dfl-fme-main.c
+++ b/drivers/fpga/dfl-fme-main.c
@@ -604,29 +604,40 @@ static long fme_ioctl_check_extension(struct dfl_feature_dev_data *fdata,
 
 static int fme_open(struct inode *inode, struct file *filp)
 {
-	struct dfl_feature_dev_data *fdata = dfl_fpga_inode_to_feature_dev_data(inode);
-	struct platform_device *fdev = fdata->dev;
+	struct dfl_feature_platform_data *pdata = dfl_fpga_inode_to_pdata(inode);
+	struct dfl_feature_dev_data *fdata;
 	int ret;
 
+	fdata = dfl_fpga_dev_ops_enter(pdata);
+	if (!fdata)
+		return -ENODEV;
+
 	mutex_lock(&fdata->lock);
 	ret = dfl_feature_dev_use_begin(fdata, filp->f_flags & O_EXCL);
 	if (!ret) {
-		dev_dbg(&fdev->dev, "Device File Opened %d Times\n",
+		dev_dbg(&fdata->dev->dev, "Device File Opened %d Times\n",
 			dfl_feature_dev_use_count(fdata));
-		filp->private_data = fdata;
+		filp->private_data = pdata;
 	}
 	mutex_unlock(&fdata->lock);
 
+	dfl_fpga_dev_ops_exit(pdata);
+
 	return ret;
 }
 
 static int fme_release(struct inode *inode, struct file *filp)
 {
-	struct dfl_feature_dev_data *fdata = filp->private_data;
-	struct platform_device *pdev = fdata->dev;
+	struct dfl_feature_platform_data *pdata = filp->private_data;
+	struct dfl_feature_dev_data *fdata;
 	struct dfl_feature *feature;
 
-	dev_dbg(&pdev->dev, "Device File Release\n");
+	/* The device is gone, and so may be fdata. */
+	fdata = dfl_fpga_dev_ops_enter(pdata);
+	if (!fdata)
+		return 0;
+
+	dev_dbg(&fdata->dev->dev, "Device File Release\n");
 
 	mutex_lock(&fdata->lock);
 	dfl_feature_dev_use_end(fdata);
@@ -637,12 +648,14 @@ static int fme_release(struct inode *inode, struct file *filp)
 						  feature->nr_irqs, NULL);
 	mutex_unlock(&fdata->lock);
 
+	dfl_fpga_dev_ops_exit(pdata);
+
 	return 0;
 }
 
-static long fme_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+static long __fme_ioctl(struct dfl_feature_dev_data *fdata, unsigned int cmd,
+			unsigned long arg)
 {
-	struct dfl_feature_dev_data *fdata = filp->private_data;
 	struct platform_device *pdev = fdata->dev;
 	struct dfl_feature *f;
 	long ret;
@@ -673,6 +686,23 @@ static long fme_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 	return -EINVAL;
 }
 
+static long fme_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+{
+	struct dfl_feature_platform_data *pdata = filp->private_data;
+	struct dfl_feature_dev_data *fdata;
+	long ret;
+
+	fdata = dfl_fpga_dev_ops_enter(pdata);
+	if (!fdata)
+		return -ENODEV;
+
+	ret = __fme_ioctl(fdata, cmd, arg);
+
+	dfl_fpga_dev_ops_exit(pdata);
+
+	return ret;
+}
+
 static int fme_dev_init(struct platform_device *pdev)
 {
 	struct dfl_feature_dev_data *fdata = to_dfl_feature_dev_data(&pdev->dev);
diff --git a/drivers/fpga/dfl.c b/drivers/fpga/dfl.c
index 6f5fa7b2fc54f..f1b7e22954c6c 100644
--- a/drivers/fpga/dfl.c
+++ b/drivers/fpga/dfl.c
@@ -45,6 +45,17 @@ static const char *dfl_pdata_key_strings[DFL_ID_MAX] = {
 	"dfl-port-pdata",
 };
 
+/*
+ * An FME ioctl can release a port, which unregisters the port's cdev ops
+ * while the FME's ops_rwsem is held for read.
+ */
+static struct lock_class_key dfl_ops_rwsem_keys[DFL_ID_MAX];
+
+static const char *dfl_ops_rwsem_key_strings[DFL_ID_MAX] = {
+	"dfl-fme-ops",
+	"dfl-port-ops",
+};
+
 /**
  * struct dfl_dev_info - dfl feature device information.
  * @name: name string of the feature platform device.
@@ -645,6 +656,10 @@ int dfl_fpga_dev_ops_register(struct platform_device *pdev,
 {
 	struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
 
+	down_write(&pdata->ops_rwsem);
+	pdata->ops_unregistered = false;
+	up_write(&pdata->ops_rwsem);
+
 	cdev_init(&pdata->cdev, fops);
 	pdata->cdev.owner = owner;
 
@@ -663,15 +678,80 @@ EXPORT_SYMBOL_GPL(dfl_fpga_dev_ops_register);
 /**
  * dfl_fpga_dev_ops_unregister - unregister cdev ops for feature dev
  * @pdev: feature dev.
+ *
+ * The device file may still be open. Wait for the file operations in
+ * progress and make the later ones fail, so that none of them reaches
+ * the feature device once it is gone.
  */
 void dfl_fpga_dev_ops_unregister(struct platform_device *pdev)
 {
 	struct dfl_feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
 
 	cdev_del(&pdata->cdev);
+
+	down_write(&pdata->ops_rwsem);
+	pdata->ops_unregistered = true;
+	up_write(&pdata->ops_rwsem);
 }
 EXPORT_SYMBOL_GPL(dfl_fpga_dev_ops_unregister);
 
+/**
+ * dfl_fpga_dev_ops_enter - begin a file operation on a feature device
+ * @pdata: platform data of the feature device.
+ *
+ * Keep the cdev ops from being unregistered until dfl_fpga_dev_ops_exit().
+ *
+ * Return: the feature device data, or NULL if the cdev ops have been
+ * unregistered. In that case the feature device data must not be used.
+ */
+struct dfl_feature_dev_data *
+dfl_fpga_dev_ops_enter(struct dfl_feature_platform_data *pdata)
+{
+	down_read(&pdata->ops_rwsem);
+	if (pdata->ops_unregistered) {
+		up_read(&pdata->ops_rwsem);
+		return NULL;
+	}
+
+	return pdata->fdata;
+}
+EXPORT_SYMBOL_GPL(dfl_fpga_dev_ops_enter);
+
+/**
+ * dfl_fpga_dev_ops_tryenter - dfl_fpga_dev_ops_enter() for mmap
+ * @pdata: platform data of the feature device.
+ *
+ * An ioctl can take mmap_lock with the ops held, e.g. to account pinned
+ * pages. With mmap_lock already held, do not wait behind an unregister
+ * that waits for that ioctl: fail if the cdev ops are being unregistered.
+ *
+ * Return: the feature device data, or NULL.
+ */
+struct dfl_feature_dev_data *
+dfl_fpga_dev_ops_tryenter(struct dfl_feature_platform_data *pdata)
+{
+	if (!down_read_trylock(&pdata->ops_rwsem))
+		return NULL;
+
+	if (pdata->ops_unregistered) {
+		up_read(&pdata->ops_rwsem);
+		return NULL;
+	}
+
+	return pdata->fdata;
+}
+EXPORT_SYMBOL_GPL(dfl_fpga_dev_ops_tryenter);
+
+/**
+ * dfl_fpga_dev_ops_exit - end a file operation on a feature device
+ * @pdata: platform data of the feature device.
+ */
+void dfl_fpga_dev_ops_exit(struct dfl_feature_platform_data *pdata)
+{
+	up_read(&pdata->ops_rwsem);
+}
+EXPORT_SYMBOL_GPL(dfl_fpga_dev_ops_exit);
+
 /**
  * struct build_feature_devs_info - info collected during feature dev build.
  *
@@ -863,7 +943,7 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
  */
 static int feature_dev_register(struct dfl_feature_dev_data *fdata)
 {
-	struct dfl_feature_platform_data pdata = {};
+	struct dfl_feature_platform_data pdata = {}, *ppdata;
 	struct platform_device *fdev;
 	struct dfl_feature *feature;
 	int ret;
@@ -890,6 +970,12 @@ static int feature_dev_register(struct dfl_feature_dev_data *fdata)
 	if (ret)
 		goto err_put_dev;
 
+	ppdata = dev_get_platdata(&fdev->dev);
+	init_rwsem(&ppdata->ops_rwsem);
+	lockdep_set_class_and_name(&ppdata->ops_rwsem,
+				   &dfl_ops_rwsem_keys[fdata->type],
+				   dfl_ops_rwsem_key_strings[fdata->type]);
+
 	ret = platform_device_add(fdev);
 	if (ret)
 		goto err_put_dev;
diff --git a/drivers/fpga/dfl.h b/drivers/fpga/dfl.h
index 9a65617bb8702..b8db43519f6d6 100644
--- a/drivers/fpga/dfl.h
+++ b/drivers/fpga/dfl.h
@@ -24,6 +24,7 @@
 #include <linux/iopoll.h>
 #include <linux/io-64-nonatomic-lo-hi.h>
 #include <linux/platform_device.h>
+#include <linux/rwsem.h>
 #include <linux/slab.h>
 #include <linux/uuid.h>
 #include <linux/fpga/fpga-region.h>
@@ -345,10 +346,19 @@ struct dfl_feature_dev_data {
  *
  * @cdev: cdev of feature dev.
  * @fdata: dfl enumeration data for the dfl feature device.
+ * @ops_rwsem: held for read by the file operations, for write when the cdev
+ *	       ops are unregistered.
+ * @ops_unregistered: set when the cdev ops are unregistered.
+ *
+ * The platform data lives as long as the feature device, which an open
+ * device file keeps alive. @fdata does not: it belongs to the parent device
+ * and may be freed once the feature device is removed.
  */
 struct dfl_feature_platform_data {
 	struct cdev cdev;
 	struct dfl_feature_dev_data *fdata;
+	struct rw_semaphore ops_rwsem;
+	bool ops_unregistered;
 };
 
 static inline
@@ -418,15 +428,17 @@ int dfl_fpga_dev_ops_register(struct platform_device *pdev,
 			      const struct file_operations *fops,
 			      struct module *owner);
 void dfl_fpga_dev_ops_unregister(struct platform_device *pdev);
+struct dfl_feature_dev_data *
+dfl_fpga_dev_ops_enter(struct dfl_feature_platform_data *pdata);
+struct dfl_feature_dev_data *
+dfl_fpga_dev_ops_tryenter(struct dfl_feature_platform_data *pdata);
+void dfl_fpga_dev_ops_exit(struct dfl_feature_platform_data *pdata);
 
-static inline struct dfl_feature_dev_data *
-dfl_fpga_inode_to_feature_dev_data(struct inode *inode)
+static inline struct dfl_feature_platform_data *
+dfl_fpga_inode_to_pdata(struct inode *inode)
 {
-	struct dfl_feature_platform_data *pdata;
-
-	pdata = container_of(inode->i_cdev, struct dfl_feature_platform_data,
-			     cdev);
-	return pdata->fdata;
+	return container_of(inode->i_cdev, struct dfl_feature_platform_data,
+			    cdev);
 }
 
 #define dfl_fpga_dev_for_each_feature(fdata, feature)			    \
-- 
2.56.1


  reply	other threads:[~2026-11-30 18:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 18:21 [RFC PATCH v2 0/2] " Ionut Nechita (Wind River)
2026-11-30 18:21 ` Ionut Nechita (Wind River) [this message]
2026-11-30 18:21 ` [RFC PATCH v2 2/2] fpga: dfl: release irq triggers when the device file ops are unregistered Ionut Nechita (Wind River)

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=20261130182132.56809-2-ionut.nechita@windriver.com \
    --to=ionut.nechita@windriver.com \
    --cc=linux-fpga@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mdf@kernel.org \
    --cc=peter.colberg@intel.com \
    --cc=trix@redhat.com \
    --cc=yilun.xu@intel.com \
    --cc=yilun.xu@linux.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®