linux-fpga.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Peter Colberg <peter.colberg@intel.com>
To: Wu Hao <hao.wu@intel.com>, Tom Rix <trix@redhat.com>,
	Moritz Fischer <mdf@kernel.org>, Xu Yilun <yilun.xu@intel.com>,
	linux-fpga@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Russ Weight <russ.weight@linux.dev>,
	Marco Pagani <marpagan@redhat.com>,
	Matthew Gerlach <matthew.gerlach@linux.intel.com>,
	Basheer Ahmed Muddebihal
	<basheer.ahmed.muddebihal@linux.intel.com>,
	Peter Colberg <peter.colberg@intel.com>
Subject: [PATCH v5 14/18] fpga: dfl: store platform device id in feature device data
Date: Tue, 19 Nov 2024 20:10:30 -0500	[thread overview]
Message-ID: <20241120011035.230574-15-peter.colberg@intel.com> (raw)
In-Reply-To: <20241120011035.230574-1-peter.colberg@intel.com>

Delay the feature device id allocation from build_info_create_dev() to
binfo_create_feature_dev_data() and store the id in the feature device
data before copying it to the device. This will allow reusing the same
id in a subsequent commit which completely destroys and recreates the
feature device when releasing and reassigning the corresponding port.

Instead of manually freeing the id when no longer needed, use a
device-managed resource with a custom action to automatically free
the id right before the feature device data is freed. The id registry
is guaranteed to be allocated when dfl_id_free_action() is invoked,
since the DFL PCIe device and its device-managed resources will be
destroyed before dfl_ids_destroy() is called in dfl_fpga_exit().

Signed-off-by: Peter Colberg <peter.colberg@intel.com>
Reviewed-by: Matthew Gerlach <matthew.gerlach@linux.intel.com>
Reviewed-by: Basheer Ahmed Muddebihal <basheer.ahmed.muddebihal@linux.intel.com>
---
Changes since v4:
- In the function dfl_fpga_feature_devs_remove(), remove braces around
  single-line conditional statement.

Changes since v3:
- New patch extracted from last patch of v3 series.
---
 drivers/fpga/dfl.c | 46 +++++++++++++++++++++++-----------------------
 drivers/fpga/dfl.h |  2 ++
 2 files changed, 25 insertions(+), 23 deletions(-)

diff --git a/drivers/fpga/dfl.c b/drivers/fpga/dfl.c
index c8e4b977a233..aeae5befc6c6 100644
--- a/drivers/fpga/dfl.c
+++ b/drivers/fpga/dfl.c
@@ -740,6 +740,13 @@ static void dfl_fpga_cdev_add_port_data(struct dfl_fpga_cdev *cdev,
 	mutex_unlock(&cdev->lock);
 }
 
+static void dfl_id_free_action(void *arg)
+{
+	struct dfl_feature_dev_data *fdata = arg;
+
+	dfl_id_free(fdata->type, fdata->pdev_id);
+}
+
 static struct dfl_feature_dev_data *
 binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 {
@@ -747,7 +754,7 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 	enum dfl_id_type type = binfo->type;
 	struct dfl_feature_info *finfo, *p;
 	struct dfl_feature_dev_data *fdata;
-	int index = 0, res_idx = 0;
+	int ret, index = 0, res_idx = 0;
 
 	if (WARN_ON_ONCE(type >= DFL_ID_MAX))
 		return ERR_PTR(-EINVAL);
@@ -768,6 +775,17 @@ binfo_create_feature_dev_data(struct build_feature_devs_info *binfo)
 
 	fdata->dev = fdev;
 	fdata->type = type;
+
+	fdata->pdev_id = dfl_id_alloc(type, binfo->dev);
+	if (fdata->pdev_id < 0)
+		return ERR_PTR(fdata->pdev_id);
+
+	ret = devm_add_action_or_reset(binfo->dev, dfl_id_free_action, fdata);
+	if (ret)
+		return ERR_PTR(ret);
+
+	fdev->id = fdata->pdev_id;
+
 	fdata->pdev_name = dfl_devs[type].name;
 	fdata->num = binfo->feature_num;
 	fdata->dfl_cdev = binfo->cdev;
@@ -866,10 +884,6 @@ build_info_create_dev(struct build_feature_devs_info *binfo)
 
 	INIT_LIST_HEAD(&binfo->sub_features);
 
-	fdev->id = dfl_id_alloc(type, &fdev->dev);
-	if (fdev->id < 0)
-		return fdev->id;
-
 	return 0;
 }
 
@@ -946,17 +960,9 @@ static void build_info_free(struct build_feature_devs_info *binfo)
 {
 	struct dfl_feature_info *finfo, *p;
 
-	/*
-	 * it is a valid id, free it. See comments in
-	 * build_info_create_dev()
-	 */
-	if (binfo->feature_dev && binfo->feature_dev->id >= 0) {
-		dfl_id_free(binfo->type, binfo->feature_dev->id);
-
-		list_for_each_entry_safe(finfo, p, &binfo->sub_features, node) {
-			list_del(&finfo->node);
-			kfree(finfo);
-		}
+	list_for_each_entry_safe(finfo, p, &binfo->sub_features, node) {
+		list_del(&finfo->node);
+		kfree(finfo);
 	}
 
 	platform_device_put(binfo->feature_dev);
@@ -1547,13 +1553,9 @@ EXPORT_SYMBOL_GPL(dfl_fpga_enum_info_add_irq);
 static int remove_feature_dev(struct device *dev, void *data)
 {
 	struct dfl_feature_dev_data *fdata = to_dfl_feature_dev_data(dev);
-	struct platform_device *pdev = to_platform_device(dev);
-	int id = pdev->id;
 
 	feature_dev_unregister(fdata);
 
-	dfl_id_free(fdata->type, id);
-
 	return 0;
 }
 
@@ -1656,10 +1658,8 @@ void dfl_fpga_feature_devs_remove(struct dfl_fpga_cdev *cdev)
 		struct platform_device *port_dev = fdata->dev;
 
 		/* remove released ports */
-		if (!device_is_registered(&port_dev->dev)) {
-			dfl_id_free(fdata->type, port_dev->id);
+		if (!device_is_registered(&port_dev->dev))
 			platform_device_put(port_dev);
-		}
 
 		list_del(&fdata->node);
 		put_device(&port_dev->dev);
diff --git a/drivers/fpga/dfl.h b/drivers/fpga/dfl.h
index cbff5d543c44..2e38c42b3920 100644
--- a/drivers/fpga/dfl.h
+++ b/drivers/fpga/dfl.h
@@ -309,6 +309,7 @@ struct dfl_feature {
  * @lock: mutex to protect feature dev data.
  * @dev: ptr to the feature's platform device linked with this structure.
  * @type: type of DFL FIU for the feature dev. See enum dfl_id_type.
+ * @pdev_id: platform device id for the feature dev.
  * @pdev_name: platform device name for the feature dev.
  * @dfl_cdev: ptr to container device.
  * @id: id used for the feature device.
@@ -326,6 +327,7 @@ struct dfl_feature_dev_data {
 	struct mutex lock;
 	struct platform_device *dev;
 	enum dfl_id_type type;
+	int pdev_id;
 	const char *pdev_name;
 	struct dfl_fpga_cdev *dfl_cdev;
 	int id;
-- 
2.47.0


  parent reply	other threads:[~2024-11-20  1:11 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-20  1:10 [PATCH v5 00/18] fpga: dfl: fix kernel warning on port release/assign for SRIOV Peter Colberg
2024-11-20  1:10 ` [PATCH v5 01/18] fpga: dfl: omit unneeded argument pdata from dfl_feature_instance_init() Peter Colberg
2024-11-20  1:10 ` [PATCH v5 02/18] fpga: dfl: return platform data from dfl_fpga_inode_to_feature_dev_data() Peter Colberg
2024-11-20  1:10 ` [PATCH v5 03/18] fpga: dfl: afu: use parent device to log errors on port enable/disable Peter Colberg
2024-11-20  1:10 ` [PATCH v5 04/18] fpga: dfl: afu: define local pointer to feature device Peter Colberg
2024-11-20  1:10 ` [PATCH v5 05/18] fpga: dfl: pass feature platform data instead of device as argument Peter Colberg
2024-11-20  1:10 ` [PATCH v5 06/18] fpga: dfl: factor out feature data creation from build_info_commit_dev() Peter Colberg
2024-11-20  1:10 ` [PATCH v5 07/18] fpga: dfl: store FIU type in feature platform data Peter Colberg
2024-11-20  1:10 ` [PATCH v5 08/18] fpga: dfl: refactor internal DFL APIs to take/return feature device data Peter Colberg
2024-11-20  1:10 ` [PATCH v5 09/18] fpga: dfl: factor out feature device registration Peter Colberg
2024-11-20  1:10 ` [PATCH v5 10/18] fpga: dfl: factor out feature device data from platform device data Peter Colberg
2024-11-20  1:10 ` [PATCH v5 11/18] fpga: dfl: convert features from flexible array member to separate array Peter Colberg
2024-11-20  1:10 ` [PATCH v5 12/18] fpga: dfl: store MMIO resources in feature device data Peter Colberg
2024-11-20  1:10 ` [PATCH v5 13/18] fpga: dfl: store platform device name " Peter Colberg
2024-11-20  1:10 ` Peter Colberg [this message]
2024-11-20  1:10 ` [PATCH v5 15/18] fpga: dfl: allocate platform device after " Peter Colberg
2024-11-20  1:10 ` [PATCH v5 16/18] fpga: dfl: remove unneeded function build_info_create_dev() Peter Colberg
2024-11-20  1:10 ` [PATCH v5 17/18] fpga: dfl: drop unneeded get_device() and put_device() of feature device Peter Colberg
2024-11-20  1:10 ` [PATCH v5 18/18] fpga: dfl: destroy/recreate feature platform device on port release/assign Peter Colberg
2024-12-10  9:07 ` [PATCH v5 00/18] fpga: dfl: fix kernel warning on port release/assign for SRIOV Xu Yilun

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=20241120011035.230574-15-peter.colberg@intel.com \
    --to=peter.colberg@intel.com \
    --cc=basheer.ahmed.muddebihal@linux.intel.com \
    --cc=hao.wu@intel.com \
    --cc=linux-fpga@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=marpagan@redhat.com \
    --cc=matthew.gerlach@linux.intel.com \
    --cc=mdf@kernel.org \
    --cc=russ.weight@linux.dev \
    --cc=trix@redhat.com \
    --cc=yilun.xu@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;
as well as URLs for NNTP newsgroup(s).