DPDK-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: David Marchand <david.marchand@redhat.com>
To: hemant.agrawal@nxp.com, dev@dpdk.org
Cc: Sachin Saxena <sachin.saxena@nxp.com>, Jun Yang <jun.yang@nxp.com>
Subject: [RFC v2 08/16] bus/fslmc: fix some VFIO device FD and memory leaks
Date: Thu, 17 Sep 2026 09:49:08 +0200	[thread overview]
Message-ID: <20260917074917.595603-9-david.marchand@redhat.com> (raw)
In-Reply-To: <20260917074917.595603-1-david.marchand@redhat.com>

In case VFIO_DEVICE_GET_INFO fails, the vfio_dev_fd was leaked.
In a similar way, failing to add this device FD in the group list would
result in leaking the FD.

When uninitialising a device or doing a bus cleanup, the per device VFIO
FD and the associated memory used for tracking it were leaked.

Fixes: 57cb02edf122 ("bus/fslmc: enhance MC VFIO multi-process support")

Signed-off-by: David Marchand <david.marchand@redhat.com>
---
 drivers/bus/fslmc/fslmc_vfio.c | 45 ++++++++++++++++++++++------------
 1 file changed, 30 insertions(+), 15 deletions(-)

diff --git a/drivers/bus/fslmc/fslmc_vfio.c b/drivers/bus/fslmc/fslmc_vfio.c
index 6e0b35f391..705f5aeffc 100644
--- a/drivers/bus/fslmc/fslmc_vfio.c
+++ b/drivers/bus/fslmc/fslmc_vfio.c
@@ -205,13 +205,17 @@ static int
 fslmc_vfio_clear_group(int vfio_group_fd)
 {
 	struct fslmc_vfio_group *group;
-	struct fslmc_vfio_device *dev;
 	int clear = 0;
 
 	LIST_FOREACH(group, &s_vfio_container.groups, next) {
 		if (group->fd == vfio_group_fd) {
-			LIST_FOREACH(dev, &group->vfio_devices, next)
+			while (!LIST_EMPTY(&group->vfio_devices)) {
+				struct fslmc_vfio_device *dev = LIST_FIRST(&group->vfio_devices);
+
+				close(dev->fd);
 				LIST_REMOVE(dev, next);
+				rte_free(dev);
+			}
 
 			close(vfio_group_fd);
 			LIST_REMOVE(group, next);
@@ -311,6 +315,8 @@ fslmc_vfio_group_add_dev(int vfio_group_fd,
 		if (group->fd == vfio_group_fd) {
 			dev = rte_zmalloc(NULL,
 				sizeof(struct fslmc_vfio_device), 0);
+			if (dev == NULL)
+				return -ENOMEM;
 			dev->fd = dev_fd;
 			rte_strscpy(dev->dev_name, name, sizeof(dev->dev_name));
 			LIST_INSERT_HEAD(&group->vfio_devices, dev, next);
@@ -326,26 +332,25 @@ fslmc_vfio_group_remove_dev(int vfio_group_fd,
 {
 	struct fslmc_vfio_group *group = NULL;
 	struct fslmc_vfio_device *dev;
-	int removed = 0;
 
 	LIST_FOREACH(group, &s_vfio_container.groups, next) {
 		if (group->fd == vfio_group_fd)
 			break;
 	}
 
-	if (group) {
-		LIST_FOREACH(dev, &group->vfio_devices, next) {
-			if (!strcmp(dev->dev_name, name)) {
-				LIST_REMOVE(dev, next);
-				removed = 1;
-				break;
-			}
+	if (group == NULL)
+		goto err;
+
+	LIST_FOREACH(dev, &group->vfio_devices, next) {
+		if (strcmp(dev->dev_name, name) == 0) {
+			close(dev->fd);
+			LIST_REMOVE(dev, next);
+			rte_free(dev);
+			return 0;
 		}
 	}
 
-	if (removed)
-		return 0;
-
+err:
 	return -ENODEV;
 }
 
@@ -1240,11 +1245,21 @@ fslmc_vfio_setup_device(const char *dev_addr,
 	if (ret) {
 		DPAA2_BUS_ERR("%s cannot get device info err(%d)(%s)",
 			dev_addr, errno, strerror(errno));
+		close(*vfio_dev_fd);
+		*vfio_dev_fd = -1;
 		return ret;
 	}
 
-	return fslmc_vfio_group_add_dev(vfio_group_fd, *vfio_dev_fd,
-			dev_addr);
+	ret = fslmc_vfio_group_add_dev(vfio_group_fd, *vfio_dev_fd, dev_addr);
+	if (ret) {
+		DPAA2_BUS_ERR("%s cannot add device in group err(%d)(%s)",
+			dev_addr, ret, strerror(-ret));
+		close(*vfio_dev_fd);
+		*vfio_dev_fd = -1;
+		return ret;
+	}
+
+	return 0;
 }
 
 static intptr_t vfio_map_mcp_obj(const char *mcp_obj)
-- 
2.54.0


  parent reply	other threads:[~2026-09-17  7:50 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-23 13:53 [RFC 00/11] Device unplug and bus cleanup refactoring for NXP David Marchand
2026-07-23 13:53 ` [RFC 01/11] drivers/bus: cleanup device freeing in NXP bus scan David Marchand
2026-07-23 13:53 ` [RFC 02/11] bus/dpaa: allocate interrupt during probing David Marchand
2026-07-23 13:53 ` [RFC 03/11] bus/dpaa: support unplug and use generic cleanup David Marchand
2026-07-23 13:53 ` [RFC 04/11] bus/fslmc: fix memory leaks in scan David Marchand
2026-07-23 13:53 ` [RFC 05/11] bus/fslmc: fix per type device count David Marchand
2026-07-23 13:53 ` [RFC 06/11] bus/fslmc: simplify device parsing in scan David Marchand
2026-07-23 13:53 ` [RFC 07/11] bus/fslmc: refactor device filtering for multiprocess David Marchand
2026-07-23 13:53 ` [RFC 08/11] bus/fslmc: move unplug for some device out of VFIO David Marchand
2026-07-23 13:53 ` [RFC 09/11] bus/fslmc: call VFIO setup for some device from bus layer David Marchand
2026-07-23 13:53 ` [RFC 10/11] bus/fslmc: allocate interrupt during probing David Marchand
2026-07-23 13:53 ` [RFC 11/11] bus/fslmc: use generic cleanup David Marchand
2026-07-23 14:11 ` [RFC 00/11] Device unplug and bus cleanup refactoring for NXP Hemant Agrawal
2026-09-03 13:30   ` David Marchand
2026-09-08 14:20     ` Hemant Agrawal
2026-09-10 11:54       ` David Marchand
2026-08-11 20:09 ` Stephen Hemminger
2026-09-17  7:49 ` [RFC v2 00/16] " David Marchand
2026-09-17  7:49   ` [RFC v2 01/16] drivers/bus: cleanup device freeing in NXP bus scan David Marchand
2026-09-17  7:49   ` [RFC v2 02/16] drivers/bus: fix FD leak during scan in NXP drivers David Marchand
2026-09-17  7:49   ` [RFC v2 03/16] bus/dpaa: allocate interrupt during probing David Marchand
2026-09-17  7:49   ` [RFC v2 04/16] bus/dpaa: support unplug and use generic cleanup David Marchand
2026-09-17  7:49   ` [RFC v2 05/16] bus/fslmc: fix device name leak David Marchand
2026-09-17  7:49   ` [RFC v2 06/16] bus/fslmc: fix memory leaks in scan David Marchand
2026-09-17  7:49   ` [RFC v2 07/16] bus/fslmc: fix per type device count David Marchand
2026-09-17  7:49   ` David Marchand [this message]
2026-09-17  7:49   ` [RFC v2 09/16] bus/fslmc: fix interrupt leak in DPIO cleanup David Marchand
2026-09-17  7:49   ` [RFC v2 10/16] bus/fslmc: simplify device parsing in scan David Marchand
2026-09-17  7:49   ` [RFC v2 11/16] bus/fslmc: release resources on scan failure David Marchand
2026-09-17  7:49   ` [RFC v2 12/16] bus/fslmc: refactor device filtering for multiprocess David Marchand
2026-09-17  7:49   ` [RFC v2 13/16] bus/fslmc: move unplug for some device out of VFIO David Marchand
2026-09-17  7:49   ` [RFC v2 14/16] bus/fslmc: call VFIO setup for some device from bus layer David Marchand
2026-09-17  7:49   ` [RFC v2 15/16] bus/fslmc: allocate interrupt during probing David Marchand
2026-09-17  7:49   ` [RFC v2 16/16] bus/fslmc: use generic cleanup David Marchand
2026-09-24  6:46   ` [RFC v2 00/16] Device unplug and bus cleanup refactoring for NXP David Marchand
2026-09-24  6:53     ` Hemant Agrawal

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=20260917074917.595603-9-david.marchand@redhat.com \
    --to=david.marchand@redhat.com \
    --cc=dev@dpdk.org \
    --cc=hemant.agrawal@nxp.com \
    --cc=jun.yang@nxp.com \
    --cc=sachin.saxena@nxp.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