Skip to contentSkip to conversation

VFIO: Avoid to enable vfio while the module not loaded

PATCH Superseded Michael Qiu 5 participants 31 messages Dec 4, 2014 – Jan 15, 2015 13 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 VFIO: Avoid to enable vfio while the module not loaded

When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description.

This action is not safe, and of cause got error messages:

EAL: Detected 40 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

This may make user confuse, this patch make it reasonable and much more soomth to user.

---
 lib/librte_eal/common/include/rte_common.h | 37 ++++++++++++++++++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci_vfio.c | 23 +++++++++++++------
 2 files changed, 53 insertions(+), 7 deletions(-)
lib/librte_eal/common/include/rte_common.h +37 -0
index 921b91f..333aa6b 100644
@@ -50,6 +50,8 @@ extern "C" {
 #include <ctype.h>
 #include <errno.h>
 #include <limits.h>
+#include <string.h>
+#include <stdio.h>
 
 /*********** Macros to eliminate unused variable warnings ********/
 
@@ -382,6 +384,41 @@ rte_exit(int exit_code, const char *format, ...)
 	__attribute__((noreturn))
 	__attribute__((format(printf, 2, 3)));
 
+/**
+ * Function is to check if the kernel module(like, vfio, vfio_iommu_type1,
+ * etc.) loaded.
+ *
+ * @param module_name
+ *	The module's name which need to be checked
+ *
+ * @return
+ * 	-1 means some error happens(NULL pointer or open failure)
+ * 	0  means the module not loaded
+ * 	1  means the module loaded
+ */
+static inline int
+check_module(const char *module_name)
+{
+	char mod_name[30]; /* Any module names can be longer than 30 bytes? */
+	int ret = 0;
+
+	if (NULL == module_name)
+		return -1;
+	FILE * fd = fopen("/proc/modules", "r");
+	if( fd == NULL)
+		return -1;
+	while(!feof(fd)) {
+		fscanf(fd, "%s %*[^\n]", mod_name);
+		if(!strcmp(mod_name, module_name)) {
+			ret = 1;
+			break;
+		}
+	}
+	fclose(fd);
+
+	return ret;
+}
+
 #ifdef __cplusplus
 }
 #endif
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +16 -7
index c1246e8..a11cc4b 100644
@@ -44,6 +44,7 @@
 #include <rte_tailq.h>
 #include <rte_eal_memconfig.h>
 #include <rte_malloc.h>
+#include <rte_common.h>
 
 #include "eal_filesystem.h"
 #include "eal_pci_init.h"
@@ -342,7 +343,8 @@ pci_vfio_get_container_fd(void)
 				RTE_LOG(ERR, EAL, "  could not get IOMMU type, "
 						"error %i (%s)\n", errno, strerror(errno));
 			else
-				RTE_LOG(ERR, EAL, "  unsupported IOMMU type!\n");
+				RTE_LOG(ERR, EAL, "  unsupported IOMMU type! "
+					"expect: 1, actual: %d\n", ret);
 			close(vfio_container_fd);
 			return -1;
 		}
@@ -788,13 +790,20 @@ pci_vfio_enable(void)
 		vfio_cfg.vfio_groups[i].fd = -1;
 		vfio_cfg.vfio_groups[i].group_no = -1;
 	}
-	vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
 
-	/* check if we have VFIO driver enabled */
-	if (vfio_cfg.vfio_container_fd != -1)
-		vfio_cfg.vfio_enabled = 1;
-	else
-		RTE_LOG(INFO, EAL, "VFIO support could not be initialized\n");
+	if (check_module("vfio") == 1 &&
+	    check_module("vfio_iommu_type1") == 1) {
+		vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
+
+		/* check if we have VFIO driver enabled */
+		if (vfio_cfg.vfio_container_fd != -1)
+			vfio_cfg.vfio_enabled = 1;
+		else
+			RTE_LOG(INFO, EAL, "VFIO support could not be"
+				" initialized\n");
+	} else
+		RTE_LOG(INFO, EAL, "VFIO modules are not all loaded,"
+			" skip VFIO support ...\n");
 
 	return 0;
 }
Signature · 1 line
1.9.3

A message that is not in the archive (sent off-list or not yet received): 1417664219-19679-1-git-send-email-michael.qiu-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org

#2 lore raw

PATCH v2 1/1 VFIO: Avoid to enable vfio while the module not loaded

When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description.

This action is not safe, and of cause got error messages:

EAL: Detected 40 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

This may make user confuse, this patch make it reasonable and much more soomth to user.

---
v2 --> v1:
1. Move check_module() from rte_common.h to eal_private.h and rename to rte_eal_check_module().
To make it linuxapp only.
2. Some code clean up.

Tested in host and guest, the below is results in VM:

1. VM | without patch | os: 3.11.10-301.fc20.x86_64 | VFIO not loaded

EAL: Support maximum 64 logical core(s) by configuration.
EAL: Detected 2 lcore(s)
EAL: cannot open VFIO container, error 2 (No such file or directory)
EAL: VFIO support could not be initialized
EAL: Setting up memory...

2. VM | without patch | os: 3.11.10-301.fc20.x86_64 | VFIO loaded vfio_iommu_type1 not loaded modprobe vfio

lsmod | grep "vfio"
vfio 19626 0

modprobe vfio_iommu_type1
modprobe: ERROR: could not insert 'vfio_iommu_type1': No such device

ls /dev/vfio/vfio
/dev/vfio/vfio

EAL: Support maximum 64 logical core(s) by configuration.
EAL: Detected 2 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

3. VM | with patch | os: 3.11.10-301.fc20.x86_64 | VFIO(vfio_iommu_type1 never can be loaded inside VM)

EAL: Detected 2 lcore(s)
EAL: VFIO modules not all loaded, skip VFIO support ...
EAL: Setting up memory...

 lib/librte_eal/common/eal_private.h        | 36 ++++++++++++++++++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci_vfio.c | 24 +++++++++++++++++---
 2 files changed, 57 insertions(+), 3 deletions(-)
lib/librte_eal/common/eal_private.h +36 -0
index 232fcec..e741bdb 100644
@@ -35,6 +35,7 @@
 #define _EAL_PRIVATE_H_
 
 #include <stdio.h>
+#include <string.h>
 
 /**
  * Initialize the memzone subsystem (private to eal).
@@ -203,4 +204,39 @@ int rte_eal_alarm_init(void);
  */
 int rte_eal_dev_init(void);
 
+/**
+ * Function is to check if the kernel module(like, vfio, vfio_iommu_type1,
+ * etc.) loaded.
+ *
+ * @param module_name
+ *	The module's name which need to be checked
+ *
+ * @return
+ * 	-1 means error happens(NULL pointer or open failure)
+ * 	 0 means the module not loaded
+ * 	 1 means the module loaded
+ */
+static inline int
+rte_eal_check_module(const char *module_name)
+{
+	char mod_name[30]; /* Any module names can be longer than 30 bytes? */
+	int ret = 0;
+
+	if (NULL == module_name)
+		return -1;
+	FILE * fd = fopen("/proc/modules", "r");
+	if (fd == NULL)
+		return -1;
+	while(!feof(fd)) {
+		fscanf(fd, "%s %*[^\n]", mod_name);
+		if(!strcmp(mod_name, module_name)) {
+			ret = 1;
+			break;
+		}
+	}
+	fclose(fd);
+
+	return ret;
+}
+
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +21 -3
index c1246e8..52ab2d0 100644
@@ -44,6 +44,7 @@
 #include <rte_tailq.h>
 #include <rte_eal_memconfig.h>
 #include <rte_malloc.h>
+#include <eal_private.h>
 
 #include "eal_filesystem.h"
 #include "eal_pci_init.h"
@@ -339,10 +340,13 @@ pci_vfio_get_container_fd(void)
 		ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU);
 		if (ret != 1) {
 			if (ret < 0)
-				RTE_LOG(ERR, EAL, "  could not get IOMMU type, "
-						"error %i (%s)\n", errno, strerror(errno));
+				RTE_LOG(ERR, EAL, "  could not get IOMMU type,"
+					" error %i (%s)\n", errno,
+					strerror(errno));
 			else
-				RTE_LOG(ERR, EAL, "  unsupported IOMMU type!\n");
+				RTE_LOG(ERR, EAL, "  unsupported IOMMU type! "
+					"expect: VFIO_TYPE1_IOMMU, "
+					"actual: %d\n", ret);
 			close(vfio_container_fd);
 			return -1;
 		}
@@ -788,6 +792,20 @@ pci_vfio_enable(void)
 		vfio_cfg.vfio_groups[i].fd = -1;
 		vfio_cfg.vfio_groups[i].group_no = -1;
 	}
+
+	/* return error directly */
+	if (rte_eal_check_module("vfio") == -1 ||
+	    rte_eal_check_module("vfio_iommu_type1") == -1)
+		return -1;
+
+	/* return 0 if not all VFIO modules loaded */
+	if (rte_eal_check_module("vfio") == 0 ||
+	    rte_eal_check_module("vfio_iommu_type1") == 0) {
+		RTE_LOG(INFO, EAL, "VFIO modules not all loaded,"
+			" skip VFIO support ...\n");
+		return 0;
+	}
+
 	vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
 
 	/* check if we have VFIO driver enabled */
Signature · 1 line
1.9.3

A message that is not in the archive (sent off-list or not yet received): 1417687227-21854-1-git-send-email-michael.qiu-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org

#4 lore raw

Hi Michael,

Few nitpicks :-) (wording of the log message I guess is up to Thomas, I won't comment on that)

Quoted 39 lines
 lib/librte_eal/common/eal_private.h        | 36
++++++++++++++++++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci_vfio.c | 24 +++++++++++++++++---
 2 files changed, 57 insertions(+), 3 deletions(-)
lib/librte_eal/common/eal_private.h +17 -0
index 232fcec..e741bdb 100644
@@ -35,6 +35,7 @@
 #define _EAL_PRIVATE_H_
 
 #include <stdio.h>
+#include <string.h>
 
 /**
  * Initialize the memzone subsystem (private to eal).
@@ -203,4 +204,39 @@ int rte_eal_alarm_init(void);
  */
 int rte_eal_dev_init(void);
 
+/**
+ * Function is to check if the kernel module(like, vfio,
+vfio_iommu_type1,
+ * etc.) loaded.
+ *
+ * @param module_name
+ *	The module's name which need to be checked
+ *
+ * @return
+ * 	-1 means error happens(NULL pointer or open failure)
+ * 	 0 means the module not loaded
+ * 	 1 means the module loaded
+ */
+static inline int
+rte_eal_check_module(const char *module_name) {
+	char mod_name[30]; /* Any module names can be longer than 30

bytes? */

lib/librte_eal/common/eal_private.h int rte_eal_alarm_init(void); · line 229

Quoted 7 lines
lib/librte_eal/common/eal_private.h +7 -0
+	int ret = 0;
+
+	if (NULL == module_name)
+		return -1;
+	FILE * fd = fopen("/proc/modules", "r");
+	if (fd == NULL)
+		return -1;

Can we add RTE_LOG statement here, with an strerror(errno) like in other places? Fopen failed, we should at least know why :-)

lib/librte_eal/common/eal_private.h int rte_eal_alarm_init(void); · line 232

Quoted 3 lines
lib/librte_eal/common/eal_private.h +3 -0
+	while(!feof(fd)) {
+		fscanf(fd, "%s %*[^\n]", mod_name);
+		if(!strcmp(mod_name, module_name)) {

Probably should use strncmp instead of strcmp.

Quoted 41 lines
lib/librte_eal/common/eal_private.h +9 -0
+			ret = 1;
+			break;
+		}
+	}
+	fclose(fd);
+
+	return ret;
+}
+
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -1
index c1246e8..52ab2d0 100644
@@ -44,6 +44,7 @@
 #include <rte_tailq.h>
 #include <rte_eal_memconfig.h>
 #include <rte_malloc.h>
+#include <eal_private.h>
 
 #include "eal_filesystem.h"
 #include "eal_pci_init.h"
@@ -339,10 +340,13 @@ pci_vfio_get_container_fd(void)
 		ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
 VFIO_TYPE1_IOMMU);
 		if (ret != 1) {
 			if (ret < 0)
-				RTE_LOG(ERR, EAL, "  could not get IOMMU

type, "

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +0 -1
-						"error %i (%s)\n", errno,

strerror(errno));

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				RTE_LOG(ERR, EAL, "  could not get IOMMU

type,"

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +2 -1
+					" error %i (%s)\n", errno,
+					strerror(errno));
 			else
-				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type!\n");

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type! "

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c pci_vfio_get_container_fd(void) · line 349

Quoted 2 lines
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +2 -0
+					"expect: VFIO_TYPE1_IOMMU, "
+					"actual: %d\n", ret);

I'm not even sure we need this "expected" bit at all. We don't get back the IOMMU type VFIO currently supports; rather, this code checks if VFIO's support for VFIO_TYPE1_IOMMU is enabled or not. So I would change the error message to something more descriptive, such as " required IOMMU type support not present in VFIO!\n", and get rid of the "expected".

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +3 -0
 			close(vfio_container_fd);
 			return -1;
 		}
@@ -788,6 +792,20 @@ pci_vfio_enable(void)
 		vfio_cfg.vfio_groups[i].fd = -1;
 		vfio_cfg.vfio_groups[i].group_no = -1;
 	}
+
+	/* return error directly */
+	if (rte_eal_check_module("vfio") == -1 ||

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c pci_vfio_enable(void) · line 807

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +10 -0
+	    rte_eal_check_module("vfio_iommu_type1") == -1)
+		return -1;
+
+	/* return 0 if not all VFIO modules loaded */
+	if (rte_eal_check_module("vfio") == 0 ||
+	    rte_eal_check_module("vfio_iommu_type1") == 0) {
+		RTE_LOG(INFO, EAL, "VFIO modules not all loaded,"
+			" skip VFIO support ...\n");
+		return 0;
+	}

Can we perhaps make one call per module instead of two? i.e. something like:

int vfio_ret, vfio_ type1_ret;
vfio_ret = rte_eal_check_module("vfio"); vfio_type1_ret = rte_eal_check_module("vfio_iommu_type1");

if (vfio_ret == -1 || vfio_type1_ret == -1)

    return -1;
else if (vfio_ret == 0 || vfio_type1_ret == 0) {
     ....
    return 0;
}
Quoted 6 lines
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -1
+
 	vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
 
 	/* check if we have VFIO driver enabled */
--

1.9.3

#5 lore raw
Quoted 52 lines · Anatoly

Hi Michael,

Few nitpicks :-) (wording of the log message I guess is up to Thomas, I won't comment on that)

Quoted 39 lines
 lib/librte_eal/common/eal_private.h        | 36
++++++++++++++++++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci_vfio.c | 24 +++++++++++++++++---
 2 files changed, 57 insertions(+), 3 deletions(-)
lib/librte_eal/common/eal_private.h +17 -0
index 232fcec..e741bdb 100644
@@ -35,6 +35,7 @@
 #define _EAL_PRIVATE_H_
 
 #include <stdio.h>
+#include <string.h>
 
 /**
  * Initialize the memzone subsystem (private to eal).
@@ -203,4 +204,39 @@ int rte_eal_alarm_init(void);
  */
 int rte_eal_dev_init(void);
 
+/**
+ * Function is to check if the kernel module(like, vfio,
+vfio_iommu_type1,
+ * etc.) loaded.
+ *
+ * @param module_name
+ *	The module's name which need to be checked
+ *
+ * @return
+ * 	-1 means error happens(NULL pointer or open failure)
+ * 	 0 means the module not loaded
+ * 	 1 means the module loaded
+ */
+static inline int
+rte_eal_check_module(const char *module_name) {
+	char mod_name[30]; /* Any module names can be longer than 30

bytes? */

lib/librte_eal/common/eal_private.h int rte_eal_alarm_init(void); · line 229

Quoted 7 lines
lib/librte_eal/common/eal_private.h +7 -0
+	int ret = 0;
+
+	if (NULL == module_name)
+		return -1;
+	FILE * fd = fopen("/proc/modules", "r");
+	if (fd == NULL)
+		return -1;

Can we add RTE_LOG statement here, with an strerror(errno) like in other places? Fopen failed, we should at least know why :-)

Quoted 4 lines

lib/librte_eal/common/eal_private.h int rte_eal_alarm_init(void); · line 232

Quoted 3 lines
lib/librte_eal/common/eal_private.h +3 -0
+	while(!feof(fd)) {
+		fscanf(fd, "%s %*[^\n]", mod_name);
+		if(!strcmp(mod_name, module_name)) {

Probably should use strncmp instead of strcmp.

I don't think so, if we check module "vfio", but if given module name is "vfio_xx", it will also correct if use strncmp.

Thanks,
Michael

Quoted 86 lines
Quoted 42 lines
lib/librte_eal/common/eal_private.h +9 -0
+			ret = 1;
+			break;
+		}
+	}
+	fclose(fd);
+
+	return ret;
+}
+
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -1
index c1246e8..52ab2d0 100644
@@ -44,6 +44,7 @@
 #include <rte_tailq.h>
 #include <rte_eal_memconfig.h>
 #include <rte_malloc.h>
+#include <eal_private.h>
 
 #include "eal_filesystem.h"
 #include "eal_pci_init.h"
@@ -339,10 +340,13 @@ pci_vfio_get_container_fd(void)
 		ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
 VFIO_TYPE1_IOMMU);
 		if (ret != 1) {
 			if (ret < 0)
-				RTE_LOG(ERR, EAL, "  could not get IOMMU

type, "

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +0 -1
-						"error %i (%s)\n", errno,

strerror(errno));

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				RTE_LOG(ERR, EAL, "  could not get IOMMU

type,"

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +2 -1
+					" error %i (%s)\n", errno,
+					strerror(errno));
 			else
-				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type!\n");

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type! "

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c pci_vfio_get_container_fd(void) · line 349

Quoted 2 lines
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +2 -0
+					"expect: VFIO_TYPE1_IOMMU, "
+					"actual: %d\n", ret);

I'm not even sure we need this "expected" bit at all. We don't get back the IOMMU type VFIO currently supports; rather, this code checks if VFIO's support for VFIO_TYPE1_IOMMU is enabled or not. So I would change the error message to something more descriptive, such as " required IOMMU type support not present in VFIO!\n", and get rid of the "expected".

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +3 -0
 			close(vfio_container_fd);
 			return -1;
 		}
@@ -788,6 +792,20 @@ pci_vfio_enable(void)
 		vfio_cfg.vfio_groups[i].fd = -1;
 		vfio_cfg.vfio_groups[i].group_no = -1;
 	}
+
+	/* return error directly */
+	if (rte_eal_check_module("vfio") == -1 ||

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c pci_vfio_enable(void) · line 807

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +10 -0
+	    rte_eal_check_module("vfio_iommu_type1") == -1)
+		return -1;
+
+	/* return 0 if not all VFIO modules loaded */
+	if (rte_eal_check_module("vfio") == 0 ||
+	    rte_eal_check_module("vfio_iommu_type1") == 0) {
+		RTE_LOG(INFO, EAL, "VFIO modules not all loaded,"
+			" skip VFIO support ...\n");
+		return 0;
+	}

Can we perhaps make one call per module instead of two? i.e. something like:

int vfio_ret, vfio_ type1_ret;
vfio_ret = rte_eal_check_module("vfio"); vfio_type1_ret = rte_eal_check_module("vfio_iommu_type1");

if (vfio_ret == -1 || vfio_type1_ret == -1)

    return -1;
else if (vfio_ret == 0 || vfio_type1_ret == 0) {
     ....
    return 0;
}
Quoted 6 lines
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -1
+
 	vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
 
 	/* check if we have VFIO driver enabled */
--

1.9.3

A message that is not in the archive (sent off-list or not yet received): 533710CFB86FA344BFBF2D6802E60286C9D920-0J0gbvR4kThpB2pF5aRoyrfspsVTdybXVpNB7YpNyf8@public.gmane.org

#7 lore raw

Hi Michael

Quoted 2 lines

I don't think so, if we check module "vfio", but if given module name is "vfio_xx", it will also correct if use strncmp.

Sorry I missed this the last time. I don't think that is the case. If you do strncmp on sizeof(buffer), strncmp will always check 30 bytes. That way if you check vfio against vfio_xx, you'll get a mismatch. Of course, replacing fscanf with fgets would be better too, to make sure we never go over our buffer size when dealing with /proc/modules.

Thanks,
Anatoly

#8 lore raw
Quoted 5 lines · Anatoly

Hi Michael

Quoted 2 lines

I don't think so, if we check module "vfio", but if given module name is "vfio_xx", it will also correct if use strncmp.

Sorry I missed this the last time. I don't think that is the case. If you do strncmp on sizeof(buffer), strncmp will always check 30 bytes. That way if you check vfio against vfio_xx, you'll get a mismatch. Of course, replacing

Yes, you are right, strncmp() will check 30 bytes if use sizeof(buffer).

But any issue of strcmp() ? This rountin cares about exactly match. I think no need to convert to strncmp() if it does have other issue.

Quoted 1 line

fscanf with fgets would be better too, to make sure we never go over our buffer size when dealing with /proc/modules.

If we use fgets, we need additional efforts to get the modname, for potential overflow issue, we can limit counts of fscanf(). like below:

fscanf(fd, "%30s %*[^\n]", mod_name);

Thanks,
Michael

Quoted 4 lines

Thanks,
Anatoly

A message that is not in the archive (sent off-list or not yet received): 533710CFB86FA344BFBF2D6802E60286C9DE19-0J0gbvR4kThpB2pF5aRoyrfspsVTdybXVpNB7YpNyf8@public.gmane.org

#10 lore raw

Hi Michael,

Quoted 6 lines

Yes, you are right, strncmp() will check 30 bytes if use sizeof(buffer).

But any issue of strcmp() ? This rountin cares about exactly match. I think no need to convert to strncmp() if it does have other issue.

Quoted 8 lines
Quoted 1 line

fscanf with fgets would be better too, to make sure we never go over our

buffer size when dealing with /proc/modules.

If we use fgets, we need additional efforts to get the modname, for potential overflow issue, we can limit counts of fscanf(). like below:

fscanf(fd, "%30s %*[^\n]", mod_name);

As long as it doesn't cause easy buffer overruns, I'm fine with it :-)

Thanks,
Anatoly

#11 lore raw

PATCH v3 1/1 VFIO: Avoid to enable vfio while the module not loaded

In reply to a message not in the archive

When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description.

This action is not safe, and of cause got error messages:

EAL: Detected 40 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

This may make user confuse, this patch make it reasonable and much more soomth to user.

---
v3 --> v2:
1. Add error log in rte_eal_check_module()
2. Some code clean up.

v2 --> v1:
1. Move check_module() from rte_common.h to eal_private.h and rename to rte_eal_check_module().
To make it linuxapp only.
2. Some code clean up.

 lib/librte_eal/common/eal_private.h        | 43 ++++++++++++++++++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci_vfio.c | 30 ++++++++++++++++++---
 2 files changed, 70 insertions(+), 3 deletions(-)
lib/librte_eal/common/eal_private.h +43 -0
index 232fcec..d1d8126 100644
@@ -35,6 +35,9 @@
 #define _EAL_PRIVATE_H_
 
 #include <stdio.h>
+#include <string.h>
+#include <rte_log.h>
+#include <errno.h>
 
 /**
  * Initialize the memzone subsystem (private to eal).
@@ -203,4 +206,44 @@ int rte_eal_alarm_init(void);
  */
 int rte_eal_dev_init(void);
 
+/**
+ * Function is to check if the kernel module(like, vfio, vfio_iommu_type1,
+ * etc.) loaded.
+ *
+ * @param module_name
+ *	The module's name which need to be checked
+ *
+ * @return
+ * 	-1 means some error happens(NULL pointer or open failure)
+ * 	0  means the module not loaded
+ * 	1  means the module loaded
+ */
+static inline int
+rte_eal_check_module(const char *module_name)
+{
+	char mod_name[30]; /* Any module names can be longer than 30 bytes? */
+	int ret = 0;
+
+	if (NULL == module_name) {
+		RTE_LOG(ERR, EAL, "The module name is NULL\n");
+		return -1;
+	}
+	FILE * fd = fopen("/proc/modules", "r");
+	if (NULL == fd) {
+		RTE_LOG(ERR, EAL, "Open /proc/modules failed!"
+			" error %i (%s)\n", errno, strerror(errno));
+		return -1;
+	}
+	while(!feof(fd)) {
+		fscanf(fd, "%s %*[^\n]", mod_name);
+		if(!strcmp(mod_name, module_name)) {
+			ret = 1;
+			break;
+		}
+	}
+	fclose(fd);
+
+	return ret;
+}
+
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +27 -3
index c1246e8..b34b3f5 100644
@@ -44,6 +44,7 @@
 #include <rte_tailq.h>
 #include <rte_eal_memconfig.h>
 #include <rte_malloc.h>
+#include <eal_private.h>
 
 #include "eal_filesystem.h"
 #include "eal_pci_init.h"
@@ -339,10 +340,15 @@ pci_vfio_get_container_fd(void)
 		ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU);
 		if (ret != 1) {
 			if (ret < 0)
-				RTE_LOG(ERR, EAL, "  could not get IOMMU type, "
-						"error %i (%s)\n", errno, strerror(errno));
+				RTE_LOG(ERR, EAL, "  could not get IOMMU type,"
+					" error %i (%s)\n", errno,
+					strerror(errno));
 			else
-				RTE_LOG(ERR, EAL, "  unsupported IOMMU type!\n");
+				/* Better to show the IOMMU type return from
+				 * kernel for easy debug
+				 */
+				RTE_LOG(ERR, EAL, "  unsupported IOMMU type"
+					" detected: %d in VFIO\n", ret);
 			close(vfio_container_fd);
 			return -1;
 		}
@@ -783,11 +789,29 @@ pci_vfio_enable(void)
 {
 	/* initialize group list */
 	int i;
+	int module_vfio, module_vfio_type1;
 
 	for (i = 0; i < VFIO_MAX_GROUPS; i++) {
 		vfio_cfg.vfio_groups[i].fd = -1;
 		vfio_cfg.vfio_groups[i].group_no = -1;
 	}
+
+	module_vfio = rte_eal_check_module("vfio");
+	module_vfio_type1 = rte_eal_check_module("vfio_iommu_type1");
+
+	/* return error directly */
+	if (module_vfio == -1 || module_vfio_type1 == -1) {
+		RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
+		return -1;
+	}
+
+	/* return 0 if not all VFIO modules loaded */
+	if (module_vfio == 0 || module_vfio_type1 == 0) {
+		RTE_LOG(INFO, EAL, "VFIO modules not all loaded,"
+			" skip VFIO support ...\n");
+		return 0;
+	}
+
 	vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
 
 	/* check if we have VFIO driver enabled */
Signature · 1 line
1.9.3

A message that is not in the archive (sent off-list or not yet received): 1418027255-7727-1-git-send-email-michael.qiu-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org

#13 lore raw
Quoted 66 lines

When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description.

This action is not safe, and of cause got error messages:

EAL: Detected 40 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

This may make user confuse, this patch make it reasonable and much more soomth to user.

---
v3 --> v2:
1. Add error log in rte_eal_check_module()
2. Some code clean up.

v2 --> v1:
1. Move check_module() from rte_common.h to eal_private.h and rename to rte_eal_check_module().
To make it linuxapp only.
2. Some code clean up.

 lib/librte_eal/common/eal_private.h        | 43
++++++++++++++++++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci_vfio.c | 30 ++++++++++++++++++---
 2 files changed, 70 insertions(+), 3 deletions(-)
lib/librte_eal/common/eal_private.h +19 -0
index 232fcec..d1d8126 100644
@@ -35,6 +35,9 @@
 #define _EAL_PRIVATE_H_
 
 #include <stdio.h>
+#include <string.h>
+#include <rte_log.h>
+#include <errno.h>
 
 /**
  * Initialize the memzone subsystem (private to eal).
@@ -203,4 +206,44 @@ int rte_eal_alarm_init(void);
  */
 int rte_eal_dev_init(void);
 
+/**
+ * Function is to check if the kernel module(like, vfio,
+vfio_iommu_type1,
+ * etc.) loaded.
+ *
+ * @param module_name
+ *	The module's name which need to be checked
+ *
+ * @return
+ * 	-1 means some error happens(NULL pointer or open failure)
+ * 	0  means the module not loaded
+ * 	1  means the module loaded
+ */
+static inline int
+rte_eal_check_module(const char *module_name) {
+	char mod_name[30]; /* Any module names can be longer than 30

bytes? */

lib/librte_eal/common/eal_private.h int rte_eal_alarm_init(void); · line 228

Quoted 4 lines
lib/librte_eal/common/eal_private.h +4 -0
+	int ret = 0;
+
+	if (NULL == module_name) {
+		RTE_LOG(ERR, EAL, "The module name is NULL\n");

I don't think this RTE_LOG is necessary - we never RTE_LOG invalid parameters.

Quoted 62 lines
lib/librte_eal/common/eal_private.h +20 -0
+		return -1;
+	}
+	FILE * fd = fopen("/proc/modules", "r");
+	if (NULL == fd) {
+		RTE_LOG(ERR, EAL, "Open /proc/modules failed!"
+			" error %i (%s)\n", errno, strerror(errno));
+		return -1;
+	}
+	while(!feof(fd)) {
+		fscanf(fd, "%s %*[^\n]", mod_name);
+		if(!strcmp(mod_name, module_name)) {
+			ret = 1;
+			break;
+		}
+	}
+	fclose(fd);
+
+	return ret;
+}
+
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -1
index c1246e8..b34b3f5 100644
@@ -44,6 +44,7 @@
 #include <rte_tailq.h>
 #include <rte_eal_memconfig.h>
 #include <rte_malloc.h>
+#include <eal_private.h>
 
 #include "eal_filesystem.h"
 #include "eal_pci_init.h"
@@ -339,10 +340,15 @@ pci_vfio_get_container_fd(void)
 		ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
 VFIO_TYPE1_IOMMU);
 		if (ret != 1) {
 			if (ret < 0)
-				RTE_LOG(ERR, EAL, "  could not get IOMMU

type, "

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +0 -1
-						"error %i (%s)\n", errno,

strerror(errno));

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				RTE_LOG(ERR, EAL, "  could not get IOMMU

type,"

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +2 -1
+					" error %i (%s)\n", errno,
+					strerror(errno));
 			else
-				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type!\n");

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				/* Better to show the IOMMU type return

from

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +3 -0
+				 * kernel for easy debug
+				 */
+				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type"

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+					" detected: %d in VFIO\n", ret);
 			close(vfio_container_fd);
 			return -1;
 		}
@@ -783,11 +789,29 @@ pci_vfio_enable(void)  {
 	/* initialize group list */

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c pci_vfio_enable(void) { · line 799

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +4 -0
 	int i;
+	int module_vfio, module_vfio_type1;
 
 	for (i = 0; i < VFIO_MAX_GROUPS; i++) {
 		vfio_cfg.vfio_groups[i].fd = -1;
 		vfio_cfg.vfio_groups[i].group_no = -1;
 	}
+
+	module_vfio = rte_eal_check_module("vfio");
+	module_vfio_type1 = rte_eal_check_module("vfio_iommu_type1");

We can actually get away with checking just vfio_iommu_type1 (having it loaded implies that vfio is loaded as well).

Quoted 20 lines
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +14 -1
+
+	/* return error directly */
+	if (module_vfio == -1 || module_vfio_type1 == -1) {
+		RTE_LOG(INFO, EAL, "Could not get loaded module
 details!\n");
+		return -1;
+	}
+
+	/* return 0 if not all VFIO modules loaded */
+	if (module_vfio == 0 || module_vfio_type1 == 0) {
+		RTE_LOG(INFO, EAL, "VFIO modules not all loaded,"
+			" skip VFIO support ...\n");
+		return 0;
+	}
+
 	vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
 
 	/* check if we have VFIO driver enabled */
--

1.9.3

#14 lore raw
Quoted 63 lines · Anatoly
Quoted 63 lines

When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description.

This action is not safe, and of cause got error messages:

EAL: Detected 40 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

This may make user confuse, this patch make it reasonable and much more soomth to user.

---
v3 --> v2:
1. Add error log in rte_eal_check_module()
2. Some code clean up.

v2 --> v1:
1. Move check_module() from rte_common.h to eal_private.h and rename to rte_eal_check_module().
To make it linuxapp only.
2. Some code clean up.

 lib/librte_eal/common/eal_private.h        | 43
++++++++++++++++++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci_vfio.c | 30 ++++++++++++++++++---
 2 files changed, 70 insertions(+), 3 deletions(-)
lib/librte_eal/common/eal_private.h +17 -0
index 232fcec..d1d8126 100644
@@ -35,6 +35,9 @@
 #define _EAL_PRIVATE_H_
 
 #include <stdio.h>
+#include <string.h>
+#include <rte_log.h>
+#include <errno.h>
 
 /**
  * Initialize the memzone subsystem (private to eal).
@@ -203,4 +206,44 @@ int rte_eal_alarm_init(void);
  */
 int rte_eal_dev_init(void);
 
+/**
+ * Function is to check if the kernel module(like, vfio,
+vfio_iommu_type1,
+ * etc.) loaded.
+ *
+ * @param module_name
+ *	The module's name which need to be checked
+ *
+ * @return
+ * 	-1 means some error happens(NULL pointer or open failure)
+ * 	0  means the module not loaded
+ * 	1  means the module loaded
+ */
+static inline int
Quoted 8 lines

lib/librte_eal/common/eal_private.h int rte_eal_alarm_init(void); · line 228

Quoted 7 lines
lib/librte_eal/common/eal_private.h +2 -0
+rte_eal_check_module(const char *module_name) {
+	char mod_name[30]; /* Any module names can be longer than 30

bytes? */

lib/librte_eal/common/eal_private.h +4 -0
+	int ret = 0;
+
+	if (NULL == module_name) {
+		RTE_LOG(ERR, EAL, "The module name is NULL\n");

I don't think this RTE_LOG is necessary - we never RTE_LOG invalid parameters.

OK, I will remove this.

Quoted 66 lines
Quoted 66 lines
lib/librte_eal/common/eal_private.h +20 -0
+		return -1;
+	}
+	FILE * fd = fopen("/proc/modules", "r");
+	if (NULL == fd) {
+		RTE_LOG(ERR, EAL, "Open /proc/modules failed!"
+			" error %i (%s)\n", errno, strerror(errno));
+		return -1;
+	}
+	while(!feof(fd)) {
+		fscanf(fd, "%s %*[^\n]", mod_name);
+		if(!strcmp(mod_name, module_name)) {
+			ret = 1;
+			break;
+		}
+	}
+	fclose(fd);
+
+	return ret;
+}
+
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -1
index c1246e8..b34b3f5 100644
@@ -44,6 +44,7 @@
 #include <rte_tailq.h>
 #include <rte_eal_memconfig.h>
 #include <rte_malloc.h>
+#include <eal_private.h>
 
 #include "eal_filesystem.h"
 #include "eal_pci_init.h"
@@ -339,10 +340,15 @@ pci_vfio_get_container_fd(void)
 		ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
 VFIO_TYPE1_IOMMU);
 		if (ret != 1) {
 			if (ret < 0)
-				RTE_LOG(ERR, EAL, "  could not get IOMMU

type, "

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +0 -1
-						"error %i (%s)\n", errno,

strerror(errno));

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				RTE_LOG(ERR, EAL, "  could not get IOMMU

type,"

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +2 -1
+					" error %i (%s)\n", errno,
+					strerror(errno));
 			else
-				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type!\n");

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				/* Better to show the IOMMU type return

from

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +3 -0
+				 * kernel for easy debug
+				 */
+				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type"

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +2 -0
+					" detected: %d in VFIO\n", ret);
 			close(vfio_container_fd);
 			return -1;
 		}
@@ -783,11 +789,29 @@ pci_vfio_enable(void)  {
 	/* initialize group list */
 	int i;
+	int module_vfio, module_vfio_type1;
 
Quoted 8 lines

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c pci_vfio_enable(void) · line 800

Quoted 7 lines
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +3 -0
 	for (i = 0; i < VFIO_MAX_GROUPS; i++) {
 		vfio_cfg.vfio_groups[i].fd = -1;
 		vfio_cfg.vfio_groups[i].group_no = -1;
 	}
+
+	module_vfio = rte_eal_check_module("vfio");
+	module_vfio_type1 = rte_eal_check_module("vfio_iommu_type1");

We can actually get away with checking just vfio_iommu_type1 (having it loaded implies that vfio is loaded as well).

Yes, you are right. That's the point, I will rework this.

Thanks,
Michael

Quoted 22 lines
Quoted 21 lines
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +14 -1
+
+	/* return error directly */
+	if (module_vfio == -1 || module_vfio_type1 == -1) {
+		RTE_LOG(INFO, EAL, "Could not get loaded module
 details!\n");
+		return -1;
+	}
+
+	/* return 0 if not all VFIO modules loaded */
+	if (module_vfio == 0 || module_vfio_type1 == 0) {
+		RTE_LOG(INFO, EAL, "VFIO modules not all loaded,"
+			" skip VFIO support ...\n");
+		return 0;
+	}
+
 	vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
 
 	/* check if we have VFIO driver enabled */
--

1.9.3

#15 lore raw

PATCH v4 1/1 VFIO: Avoid to enable vfio while the module not loaded

In reply to a message not in the archive

When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description.

This action is not safe, and of cause got error messages:

EAL: Detected 40 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

This may make user confuse, this patch make it reasonable and much more soomth to user.

---
v4 --> v3:
1. Remove RTE_LOG for params check
2. Remove "vfio" module check as "vfio_iommu_type1" loaded indecated "vfio" loaded

v3 --> v2:
1. Add error log in rte_eal_check_module()
2. Some code clean up.

v2 --> v1:
1. Move check_module() from rte_common.h to eal_private.h and rename to rte_eal_check_module().
To make it linuxapp only.
2. Some code clean up.

 lib/librte_eal/common/eal_private.h        | 42 ++++++++++++++++++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci_vfio.c | 29 ++++++++++++++++++---
 2 files changed, 68 insertions(+), 3 deletions(-)
lib/librte_eal/common/eal_private.h +42 -0
index 232fcec..e877a25 100644
@@ -35,6 +35,9 @@
 #define _EAL_PRIVATE_H_
 
 #include <stdio.h>
+#include <string.h>
+#include <rte_log.h>
+#include <errno.h>
 
 /**
  * Initialize the memzone subsystem (private to eal).
@@ -203,4 +206,43 @@ int rte_eal_alarm_init(void);
  */
 int rte_eal_dev_init(void);
 
+/**
+ * Function is to check if the kernel module(like, vfio, vfio_iommu_type1,
+ * etc.) loaded.
+ *
+ * @param module_name
+ *	The module's name which need to be checked
+ *
+ * @return
+ * 	-1 means some error happens(NULL pointer or open failure)
+ * 	0  means the module not loaded
+ * 	1  means the module loaded
+ */
+static inline int
+rte_eal_check_module(const char *module_name)
+{
+	char mod_name[30]; /* Any module names can be longer than 30 bytes? */
+	int ret = 0;
+
+	if (NULL == module_name)
+		return -1;
+
+	FILE * fd = fopen("/proc/modules", "r");
+	if (NULL == fd) {
+		RTE_LOG(ERR, EAL, "Open /proc/modules failed!"
+			" error %i (%s)\n", errno, strerror(errno));
+		return -1;
+	}
+	while(!feof(fd)) {
+		fscanf(fd, "%s %*[^\n]", mod_name);
+		if(!strcmp(mod_name, module_name)) {
+			ret = 1;
+			break;
+		}
+	}
+	fclose(fd);
+
+	return ret;
+}
+
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +26 -3
index c1246e8..8c54d2a 100644
@@ -44,6 +44,7 @@
 #include <rte_tailq.h>
 #include <rte_eal_memconfig.h>
 #include <rte_malloc.h>
+#include <eal_private.h>
 
 #include "eal_filesystem.h"
 #include "eal_pci_init.h"
@@ -339,10 +340,15 @@ pci_vfio_get_container_fd(void)
 		ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU);
 		if (ret != 1) {
 			if (ret < 0)
-				RTE_LOG(ERR, EAL, "  could not get IOMMU type, "
-						"error %i (%s)\n", errno, strerror(errno));
+				RTE_LOG(ERR, EAL, "  could not get IOMMU type,"
+					" error %i (%s)\n", errno,
+					strerror(errno));
 			else
-				RTE_LOG(ERR, EAL, "  unsupported IOMMU type!\n");
+				/* Better to show the IOMMU type return from
+				 * kernel for easy debug
+				 */
+				RTE_LOG(ERR, EAL, "  unsupported IOMMU type"
+					" detected: %d in VFIO\n", ret);
 			close(vfio_container_fd);
 			return -1;
 		}
@@ -783,11 +789,28 @@ pci_vfio_enable(void)
 {
 	/* initialize group list */
 	int i;
+	int module_vfio_type1;
 
 	for (i = 0; i < VFIO_MAX_GROUPS; i++) {
 		vfio_cfg.vfio_groups[i].fd = -1;
 		vfio_cfg.vfio_groups[i].group_no = -1;
 	}
+
+	module_vfio_type1 = rte_eal_check_module("vfio_iommu_type1");
+
+	/* return error directly */
+	if (module_vfio_type1 == -1) {
+		RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
+		return -1;
+	}
+
+	/* return 0 if VFIO modules not loaded */
+	if (module_vfio_type1 == 0) {
+		RTE_LOG(INFO, EAL, "VFIO modules not all loaded,"
+			" skip VFIO support ...\n");
+		return 0;
+	}
+
 	vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
 
 	/* check if we have VFIO driver enabled */
Signature · 1 line
1.9.3

A message that is not in the archive (sent off-list or not yet received): 1418035455-9434-1-git-send-email-michael.qiu-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org

#17 lore raw
Quoted 84 lines

When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description.

This action is not safe, and of cause got error messages:

EAL: Detected 40 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

This may make user confuse, this patch make it reasonable and much more soomth to user.

---
v4 --> v3:
1. Remove RTE_LOG for params check
2. Remove "vfio" module check as "vfio_iommu_type1" loaded indecated "vfio" loaded

v3 --> v2:
1. Add error log in rte_eal_check_module()
2. Some code clean up.

v2 --> v1:
1. Move check_module() from rte_common.h to eal_private.h and rename to rte_eal_check_module().
To make it linuxapp only.
2. Some code clean up.

 lib/librte_eal/common/eal_private.h        | 42
++++++++++++++++++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci_vfio.c | 29 ++++++++++++++++++---
 2 files changed, 68 insertions(+), 3 deletions(-)
lib/librte_eal/common/eal_private.h +19 -0
index 232fcec..e877a25 100644
@@ -35,6 +35,9 @@
 #define _EAL_PRIVATE_H_
 
 #include <stdio.h>
+#include <string.h>
+#include <rte_log.h>
+#include <errno.h>
 
 /**
  * Initialize the memzone subsystem (private to eal).
@@ -203,4 +206,43 @@ int rte_eal_alarm_init(void);
  */
 int rte_eal_dev_init(void);
 
+/**
+ * Function is to check if the kernel module(like, vfio,
+vfio_iommu_type1,
+ * etc.) loaded.
+ *
+ * @param module_name
+ *	The module's name which need to be checked
+ *
+ * @return
+ * 	-1 means some error happens(NULL pointer or open failure)
+ * 	0  means the module not loaded
+ * 	1  means the module loaded
+ */
+static inline int
+rte_eal_check_module(const char *module_name) {
+	char mod_name[30]; /* Any module names can be longer than 30

bytes? */

lib/librte_eal/common/eal_private.h +13 -0
+	int ret = 0;
+
+	if (NULL == module_name)
+		return -1;
+
+	FILE * fd = fopen("/proc/modules", "r");
+	if (NULL == fd) {
+		RTE_LOG(ERR, EAL, "Open /proc/modules failed!"
+			" error %i (%s)\n", errno, strerror(errno));
+		return -1;
+	}
+	while(!feof(fd)) {
+		fscanf(fd, "%s %*[^\n]", mod_name);

lib/librte_eal/common/eal_private.h int rte_eal_alarm_init(void); · line 247

Quoted 10 lines
lib/librte_eal/common/eal_private.h +10 -0
+		if(!strcmp(mod_name, module_name)) {
+			ret = 1;
+			break;
+		}
+	}
+	fclose(fd);
+
+	return ret;
+}
+

Apologies for not bringing this up before, but do we really want the rte_eal_check_module inline in the header? I think it would be better to declare it in eal_private but move the definition into eal.c.

Quoted 36 lines

#endif /* _EAL_PRIVATE_H_ */

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -1
index c1246e8..8c54d2a 100644
@@ -44,6 +44,7 @@
 #include <rte_tailq.h>
 #include <rte_eal_memconfig.h>
 #include <rte_malloc.h>
+#include <eal_private.h>
 
 #include "eal_filesystem.h"
 #include "eal_pci_init.h"
@@ -339,10 +340,15 @@ pci_vfio_get_container_fd(void)
 		ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
 VFIO_TYPE1_IOMMU);
 		if (ret != 1) {
 			if (ret < 0)
-				RTE_LOG(ERR, EAL, "  could not get IOMMU

type, "

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +0 -1
-						"error %i (%s)\n", errno,

strerror(errno));

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				RTE_LOG(ERR, EAL, "  could not get IOMMU

type,"

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +2 -1
+					" error %i (%s)\n", errno,
+					strerror(errno));
 			else
-				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type!\n");

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				/* Better to show the IOMMU type return

from

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +3 -0
+				 * kernel for easy debug
+				 */
+				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type"

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c pci_vfio_get_container_fd(void) · line 351

Quoted 1 line
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+					" detected: %d in VFIO\n", ret);

I'm not sure this message is meaningful. That ioctl call can either -1, 0 or 1. We already handle 1 separately; -1 means an error; 0 means IOMMU type 1 is not supported. The return value will *not* indicate which IOMMU types *are* currently supported - it will only indicate that the IOMMU type you requested is not supported. So there's really no point in indicating the return value in case of ret 0 - it is best to just mention that requested IOMMU type support is not enabled in VFIO.

Quoted 34 lines
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +17 -1
 			close(vfio_container_fd);
 			return -1;
 		}
@@ -783,11 +789,28 @@ pci_vfio_enable(void)  {
 	/* initialize group list */
 	int i;
+	int module_vfio_type1;
 
 	for (i = 0; i < VFIO_MAX_GROUPS; i++) {
 		vfio_cfg.vfio_groups[i].fd = -1;
 		vfio_cfg.vfio_groups[i].group_no = -1;
 	}
+
+	module_vfio_type1 = rte_eal_check_module("vfio_iommu_type1");
+
+	/* return error directly */
+	if (module_vfio_type1 == -1) {
+		RTE_LOG(INFO, EAL, "Could not get loaded module
 details!\n");
+		return -1;
+	}
+
+	/* return 0 if VFIO modules not loaded */
+	if (module_vfio_type1 == 0) {
+		RTE_LOG(INFO, EAL, "VFIO modules not all loaded,"
+			" skip VFIO support ...\n");
+		return 0;
+	}
+
 	vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
 
 	/* check if we have VFIO driver enabled */
--

1.9.3

#18 lore raw
Quoted 87 lines · Anatoly
Quoted 87 lines

When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description.

This action is not safe, and of cause got error messages:

EAL: Detected 40 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

This may make user confuse, this patch make it reasonable and much more soomth to user.

---
v4 --> v3:
1. Remove RTE_LOG for params check
2. Remove "vfio" module check as "vfio_iommu_type1" loaded indecated "vfio" loaded

v3 --> v2:
1. Add error log in rte_eal_check_module()
2. Some code clean up.

v2 --> v1:
1. Move check_module() from rte_common.h to eal_private.h and rename to rte_eal_check_module().
To make it linuxapp only.
2. Some code clean up.

 lib/librte_eal/common/eal_private.h        | 42
++++++++++++++++++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci_vfio.c | 29 ++++++++++++++++++---
 2 files changed, 68 insertions(+), 3 deletions(-)
lib/librte_eal/common/eal_private.h +19 -0
index 232fcec..e877a25 100644
@@ -35,6 +35,9 @@
 #define _EAL_PRIVATE_H_
 
 #include <stdio.h>
+#include <string.h>
+#include <rte_log.h>
+#include <errno.h>
 
 /**
  * Initialize the memzone subsystem (private to eal).
@@ -203,4 +206,43 @@ int rte_eal_alarm_init(void);
  */
 int rte_eal_dev_init(void);
 
+/**
+ * Function is to check if the kernel module(like, vfio,
+vfio_iommu_type1,
+ * etc.) loaded.
+ *
+ * @param module_name
+ *	The module's name which need to be checked
+ *
+ * @return
+ * 	-1 means some error happens(NULL pointer or open failure)
+ * 	0  means the module not loaded
+ * 	1  means the module loaded
+ */
+static inline int
+rte_eal_check_module(const char *module_name) {
+	char mod_name[30]; /* Any module names can be longer than 30

bytes? */

lib/librte_eal/common/eal_private.h +16 -0
+	int ret = 0;
+
+	if (NULL == module_name)
+		return -1;
+
+	FILE * fd = fopen("/proc/modules", "r");
+	if (NULL == fd) {
+		RTE_LOG(ERR, EAL, "Open /proc/modules failed!"
+			" error %i (%s)\n", errno, strerror(errno));
+		return -1;
+	}
+	while(!feof(fd)) {
+		fscanf(fd, "%s %*[^\n]", mod_name);
+		if(!strcmp(mod_name, module_name)) {
+			ret = 1;
+			break;
Quoted 8 lines

lib/librte_eal/common/eal_private.h int rte_eal_alarm_init(void); · line 247

Quoted 7 lines
lib/librte_eal/common/eal_private.h +7 -0
+		}
+	}
+	fclose(fd);
+
+	return ret;
+}
+

Apologies for not bringing this up before, but do we really want the rte_eal_check_module inline in the header? I think it would be better to declare it in eal_private but move the definition into eal.c.

No need, actually, I'm very appreciate that you can spend your time to review my patch again and again. I really want to say thank you to you.

For rte_eal_check_module inline in the header, it really no need stay in header, so ugly. I will make new version of it, and re-post.

Quoted 30 lines
Quoted 30 lines

#endif /* _EAL_PRIVATE_H_ */

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -1
index c1246e8..8c54d2a 100644
@@ -44,6 +44,7 @@
 #include <rte_tailq.h>
 #include <rte_eal_memconfig.h>
 #include <rte_malloc.h>
+#include <eal_private.h>
 
 #include "eal_filesystem.h"
 #include "eal_pci_init.h"
@@ -339,10 +340,15 @@ pci_vfio_get_container_fd(void)
 		ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
 VFIO_TYPE1_IOMMU);
 		if (ret != 1) {
 			if (ret < 0)
-				RTE_LOG(ERR, EAL, "  could not get IOMMU

type, "

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +0 -1
-						"error %i (%s)\n", errno,

strerror(errno));

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				RTE_LOG(ERR, EAL, "  could not get IOMMU

type,"

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +2 -1
+					" error %i (%s)\n", errno,
+					strerror(errno));
 			else
-				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type!\n");

Quoted 8 lines

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c pci_vfio_get_container_fd(void) · line 351

Quoted 7 lines
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				/* Better to show the IOMMU type return

from

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +3 -0
+				 * kernel for easy debug
+				 */
+				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type"

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+					" detected: %d in VFIO\n", ret);

I'm not sure this message is meaningful. That ioctl call can either -1, 0 or 1. We already handle 1 separately; -1 means an error; 0 means IOMMU type 1 is not supported. The return value will *not* indicate which IOMMU types *are* currently supported - it will only indicate that the IOMMU type you requested is not supported. So there's really no point in indicating the return value in case of ret 0 - it is best to just mention that requested IOMMU type support is not enabled in VFIO.

Yes, you are right, I make a mistake.

Quoted 36 lines
Quoted 35 lines
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +17 -1
 			close(vfio_container_fd);
 			return -1;
 		}
@@ -783,11 +789,28 @@ pci_vfio_enable(void)  {
 	/* initialize group list */
 	int i;
+	int module_vfio_type1;
 
 	for (i = 0; i < VFIO_MAX_GROUPS; i++) {
 		vfio_cfg.vfio_groups[i].fd = -1;
 		vfio_cfg.vfio_groups[i].group_no = -1;
 	}
+
+	module_vfio_type1 = rte_eal_check_module("vfio_iommu_type1");
+
+	/* return error directly */
+	if (module_vfio_type1 == -1) {
+		RTE_LOG(INFO, EAL, "Could not get loaded module
 details!\n");
+		return -1;
+	}
+
+	/* return 0 if VFIO modules not loaded */
+	if (module_vfio_type1 == 0) {
+		RTE_LOG(INFO, EAL, "VFIO modules not all loaded,"
+			" skip VFIO support ...\n");
+		return 0;
+	}
+
 	vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
 
 	/* check if we have VFIO driver enabled */
--

1.9.3

A message that is not in the archive (sent off-list or not yet received): 533710CFB86FA344BFBF2D6802E60286C9DB9E-0J0gbvR4kThpB2pF5aRoyrfspsVTdybXVpNB7YpNyf8@public.gmane.org

#20 lore raw

Hi Michael:

Quoted 84 lines

-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Qiu, Michael
Sent: Monday, December 08, 2014 8:28 AM
To: Burakov, Anatoly; [email protected]
Subject: Re: [dpdk-dev] [PATCH v4] VFIO: Avoid to enable vfio while the module not loaded

Quoted 76 lines · Anatoly
Quoted 76 lines

When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description.

This action is not safe, and of cause got error messages:

EAL: Detected 40 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

This may make user confuse, this patch make it reasonable and much more soomth to user.

---
v4 --> v3:
1. Remove RTE_LOG for params check
2. Remove "vfio" module check as "vfio_iommu_type1" loaded indecated "vfio" loaded

v3 --> v2:
1. Add error log in rte_eal_check_module()
2. Some code clean up.

v2 --> v1:
1. Move check_module() from rte_common.h to eal_private.h and rename to rte_eal_check_module().
To make it linuxapp only.
2. Some code clean up.

 lib/librte_eal/common/eal_private.h        | 42
++++++++++++++++++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci_vfio.c | 29 ++++++++++++++++++---
 2 files changed, 68 insertions(+), 3 deletions(-)
lib/librte_eal/common/eal_private.h +19 -0
index 232fcec..e877a25 100644
@@ -35,6 +35,9 @@
 #define _EAL_PRIVATE_H_
 
 #include <stdio.h>
+#include <string.h>
+#include <rte_log.h>
+#include <errno.h>
 
 /**
  * Initialize the memzone subsystem (private to eal).
@@ -203,4 +206,43 @@ int rte_eal_alarm_init(void);
  */
 int rte_eal_dev_init(void);
 
+/**
+ * Function is to check if the kernel module(like, vfio,
+vfio_iommu_type1,
+ * etc.) loaded.
+ *
+ * @param module_name
+ *	The module's name which need to be checked
+ *
+ * @return
+ * 	-1 means some error happens(NULL pointer or open failure)
+ * 	0  means the module not loaded
+ * 	1  means the module loaded
+ */
+static inline int
+rte_eal_check_module(const char *module_name) {
+	char mod_name[30]; /* Any module names can be longer than 30

bytes? */

lib/librte_eal/common/eal_private.h +5 -0
+	int ret = 0;
+
+	if (NULL == module_name)
+		return -1;
+
Quoted 8 lines
Quoted 8 lines

lib/librte_eal/common/eal_private.h int rte_eal_alarm_init(void); · line 237

Quoted 8 lines
lib/librte_eal/common/eal_private.h +8 -0
+	FILE * fd = fopen("/proc/modules", "r");
+	if (NULL == fd) {
+		RTE_LOG(ERR, EAL, "Open /proc/modules failed!"
+			" error %i (%s)\n", errno, strerror(errno));
+		return -1;
+	}
+	while(!feof(fd)) {
+		fscanf(fd, "%s %*[^\n]", mod_name);

I see it is already discussed in the other mail that we could limit the count to avoid overflow. I don't understand why you don't apply it here.
There are already several existing modules that has 20+ length.

Quoted 104 lines
Quoted 11 lines

lib/librte_eal/common/eal_private.h int rte_eal_alarm_init(void); · line 247

Quoted 10 lines
lib/librte_eal/common/eal_private.h +10 -0
+		if(!strcmp(mod_name, module_name)) {
+			ret = 1;
+			break;
+		}
+	}
+	fclose(fd);
+
+	return ret;
+}
+

Apologies for not bringing this up before, but do we really want the

rte_eal_check_module inline in the header? I think it would be better to declare it in eal_private but move the definition into eal.c.

No need, actually, I'm very appreciate that you can spend your time to review my patch again and again. I really want to say thank you to you.

For rte_eal_check_module inline in the header, it really no need stay in header, so ugly. I will make new version of it, and re-post.

Quoted 30 lines
Quoted 30 lines

#endif /* _EAL_PRIVATE_H_ */

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -1
index c1246e8..8c54d2a 100644
@@ -44,6 +44,7 @@
 #include <rte_tailq.h>
 #include <rte_eal_memconfig.h>
 #include <rte_malloc.h>
+#include <eal_private.h>
 
 #include "eal_filesystem.h"
 #include "eal_pci_init.h"
@@ -339,10 +340,15 @@ pci_vfio_get_container_fd(void)
 		ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
 VFIO_TYPE1_IOMMU);
 		if (ret != 1) {
 			if (ret < 0)
-				RTE_LOG(ERR, EAL, "  could not get IOMMU

type, "

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +0 -1
-						"error %i (%s)\n", errno,

strerror(errno));

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				RTE_LOG(ERR, EAL, "  could not get IOMMU

type,"

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +2 -1
+					" error %i (%s)\n", errno,
+					strerror(errno));
 			else
-				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type!\n");

Quoted 8 lines

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c pci_vfio_get_container_fd(void) · line 351

Quoted 7 lines
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				/* Better to show the IOMMU type return

from

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +3 -0
+				 * kernel for easy debug
+				 */
+				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type"

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+					" detected: %d in VFIO\n", ret);

I'm not sure this message is meaningful. That ioctl call can either -1, 0 or 1. We

already handle 1 separately; -1 means an error; 0 means IOMMU type 1 is not supported. The return value will *not* indicate which IOMMU types *are* currently supported - it will only indicate that the IOMMU type you requested is not supported. So there's really no point in indicating the return value in case of ret 0 - it is best to just mention that requested IOMMU type support is not enabled in VFIO.

Yes, you are right, I make a mistake.

Quoted 36 lines
Quoted 35 lines
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +17 -1
 			close(vfio_container_fd);
 			return -1;
 		}
@@ -783,11 +789,28 @@ pci_vfio_enable(void)  {
 	/* initialize group list */
 	int i;
+	int module_vfio_type1;
 
 	for (i = 0; i < VFIO_MAX_GROUPS; i++) {
 		vfio_cfg.vfio_groups[i].fd = -1;
 		vfio_cfg.vfio_groups[i].group_no = -1;
 	}
+
+	module_vfio_type1 = rte_eal_check_module("vfio_iommu_type1");
+
+	/* return error directly */
+	if (module_vfio_type1 == -1) {
+		RTE_LOG(INFO, EAL, "Could not get loaded module
 details!\n");
+		return -1;
+	}
+
+	/* return 0 if VFIO modules not loaded */
+	if (module_vfio_type1 == 0) {
+		RTE_LOG(INFO, EAL, "VFIO modules not all loaded,"
+			" skip VFIO support ...\n");
+		return 0;
+	}
+
 	vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
 
 	/* check if we have VFIO driver enabled */
--

1.9.3

#21 lore raw
Quoted 88 lines · Huawei

Hi Michael:

Quoted 86 lines

-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Qiu, Michael
Sent: Monday, December 08, 2014 8:28 AM
To: Burakov, Anatoly; [email protected]
Subject: Re: [dpdk-dev] [PATCH v4] VFIO: Avoid to enable vfio while the module not loaded

Quoted 78 lines · Anatoly
Quoted 78 lines

When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description.

This action is not safe, and of cause got error messages:

EAL: Detected 40 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

This may make user confuse, this patch make it reasonable and much more soomth to user.

---
v4 --> v3:
1. Remove RTE_LOG for params check
2. Remove "vfio" module check as "vfio_iommu_type1" loaded indecated "vfio" loaded

v3 --> v2:
1. Add error log in rte_eal_check_module()
2. Some code clean up.

v2 --> v1:
1. Move check_module() from rte_common.h to eal_private.h and rename to rte_eal_check_module().
To make it linuxapp only.
2. Some code clean up.

 lib/librte_eal/common/eal_private.h        | 42
++++++++++++++++++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci_vfio.c | 29 ++++++++++++++++++---
 2 files changed, 68 insertions(+), 3 deletions(-)
lib/librte_eal/common/eal_private.h +19 -0
index 232fcec..e877a25 100644
@@ -35,6 +35,9 @@
 #define _EAL_PRIVATE_H_
 
 #include <stdio.h>
+#include <string.h>
+#include <rte_log.h>
+#include <errno.h>
 
 /**
  * Initialize the memzone subsystem (private to eal).
@@ -203,4 +206,43 @@ int rte_eal_alarm_init(void);
  */
 int rte_eal_dev_init(void);
 
+/**
+ * Function is to check if the kernel module(like, vfio,
+vfio_iommu_type1,
+ * etc.) loaded.
+ *
+ * @param module_name
+ *	The module's name which need to be checked
+ *
+ * @return
+ * 	-1 means some error happens(NULL pointer or open failure)
+ * 	0  means the module not loaded
+ * 	1  means the module loaded
+ */
+static inline int
+rte_eal_check_module(const char *module_name) {
+	char mod_name[30]; /* Any module names can be longer than 30

bytes? */

lib/librte_eal/common/eal_private.h +7 -0
+	int ret = 0;
+
+	if (NULL == module_name)
+		return -1;
+
+	FILE * fd = fopen("/proc/modules", "r");
+	if (NULL == fd) {
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

lib/librte_eal/common/eal_private.h int rte_eal_alarm_init(void); · line 237

Quoted 6 lines
lib/librte_eal/common/eal_private.h +6 -0
+		RTE_LOG(ERR, EAL, "Open /proc/modules failed!"
+			" error %i (%s)\n", errno, strerror(errno));
+		return -1;
+	}
+	while(!feof(fd)) {
+		fscanf(fd, "%s %*[^\n]", mod_name);

I see it is already discussed in the other mail that we could limit the count to avoid overflow. I don't understand why you don't apply it here.

Yes, I have reply in this thread with below:
fscanf(fd, "%30s %*[^\n]", mod_name);

Quoted 1 line

There are already several existing modules that has 20+ length.

Yes, would you think 30 length is enough? Otherwise I need to increase the length

Thanks,
Michael

Quoted 103 lines
Quoted 102 lines
Quoted 11 lines

lib/librte_eal/common/eal_private.h int rte_eal_alarm_init(void); · line 247

Quoted 10 lines
lib/librte_eal/common/eal_private.h +10 -0
+		if(!strcmp(mod_name, module_name)) {
+			ret = 1;
+			break;
+		}
+	}
+	fclose(fd);
+
+	return ret;
+}
+

Apologies for not bringing this up before, but do we really want the

rte_eal_check_module inline in the header? I think it would be better to declare it in eal_private but move the definition into eal.c.

No need, actually, I'm very appreciate that you can spend your time to review my patch again and again. I really want to say thank you to you.

For rte_eal_check_module inline in the header, it really no need stay in header, so ugly. I will make new version of it, and re-post.

Quoted 30 lines
Quoted 30 lines

#endif /* _EAL_PRIVATE_H_ */

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -1
index c1246e8..8c54d2a 100644
@@ -44,6 +44,7 @@
 #include <rte_tailq.h>
 #include <rte_eal_memconfig.h>
 #include <rte_malloc.h>
+#include <eal_private.h>
 
 #include "eal_filesystem.h"
 #include "eal_pci_init.h"
@@ -339,10 +340,15 @@ pci_vfio_get_container_fd(void)
 		ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
 VFIO_TYPE1_IOMMU);
 		if (ret != 1) {
 			if (ret < 0)
-				RTE_LOG(ERR, EAL, "  could not get IOMMU

type, "

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +0 -1
-						"error %i (%s)\n", errno,

strerror(errno));

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				RTE_LOG(ERR, EAL, "  could not get IOMMU

type,"

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +2 -1
+					" error %i (%s)\n", errno,
+					strerror(errno));
 			else
-				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type!\n");

Quoted 8 lines

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c pci_vfio_get_container_fd(void) · line 351

Quoted 7 lines
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				/* Better to show the IOMMU type return

from

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +3 -0
+				 * kernel for easy debug
+				 */
+				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type"

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+					" detected: %d in VFIO\n", ret);

I'm not sure this message is meaningful. That ioctl call can either -1, 0 or 1. We

already handle 1 separately; -1 means an error; 0 means IOMMU type 1 is not supported. The return value will *not* indicate which IOMMU types *are* currently supported - it will only indicate that the IOMMU type you requested is not supported. So there's really no point in indicating the return value in case of ret 0 - it is best to just mention that requested IOMMU type support is not enabled in VFIO.

Yes, you are right, I make a mistake.

Quoted 34 lines
Quoted 34 lines
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +17 -1
 			close(vfio_container_fd);
 			return -1;
 		}
@@ -783,11 +789,28 @@ pci_vfio_enable(void)  {
 	/* initialize group list */
 	int i;
+	int module_vfio_type1;
 
 	for (i = 0; i < VFIO_MAX_GROUPS; i++) {
 		vfio_cfg.vfio_groups[i].fd = -1;
 		vfio_cfg.vfio_groups[i].group_no = -1;
 	}
+
+	module_vfio_type1 = rte_eal_check_module("vfio_iommu_type1");
+
+	/* return error directly */
+	if (module_vfio_type1 == -1) {
+		RTE_LOG(INFO, EAL, "Could not get loaded module
 details!\n");
+		return -1;
+	}
+
+	/* return 0 if VFIO modules not loaded */
+	if (module_vfio_type1 == 0) {
+		RTE_LOG(INFO, EAL, "VFIO modules not all loaded,"
+			" skip VFIO support ...\n");
+		return 0;
+	}
+
 	vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
 
 	/* check if we have VFIO driver enabled */
--

1.9.3

#22 lore raw

PATCH v5 1/1 VFIO: Avoid to enable vfio while the module not loaded

In reply to a message not in the archive

When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description.

This action is not safe, and of cause got error messages:

EAL: Detected 40 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

This may make user confuse, this patch make it reasonable and much more soomth to user.

---
v5 --> v4
1. Move rte_eal_check_module() body to eal.c
2. Clean up "unsupported IOMMU type" log

v4 --> v3:
1. Remove RTE_LOG for params check
2. Remove "vfio" module check as "vfio_iommu_type1" loaded indecated "vfio" loaded

v3 --> v2:
1. Add error log in rte_eal_check_module()
2. Some code clean up.

v2 --> v1:
1. Move check_module() from rte_common.h to eal_private.h and rename to rte_eal_check_module().
To make it linuxapp only.
2. Some code clean up.

 lib/librte_eal/common/eal_private.h        | 15 +++++++++++++++
 lib/librte_eal/linuxapp/eal/eal.c          | 27 +++++++++++++++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci_vfio.c | 26 +++++++++++++++++++++++---
 3 files changed, 65 insertions(+), 3 deletions(-)
lib/librte_eal/common/eal_private.h +15 -0
index 232fcec..4183b54 100644
@@ -203,4 +203,19 @@ int rte_eal_alarm_init(void);
  */
 int rte_eal_dev_init(void);
 
+/**
+ * Function is to check if the kernel module(like, vfio, vfio_iommu_type1,
+ * etc.) loaded.
+ *
+ * @param module_name
+ *	The module's name which need to be checked
+ *
+ * @return
+ * 	-1 means some error happens(NULL pointer or open failure)
+ * 	0  means the module not loaded
+ * 	1  means the module loaded
+ */
+inline int
+rte_eal_check_module(const char *module_name);
+
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/linuxapp/eal/eal.c +27 -0
index 89f3b5e..40b462e 100644
@@ -859,3 +859,30 @@ int rte_eal_has_hugepages(void)
 {
 	return ! internal_config.no_hugetlbfs;
 }
+
+inline int
+rte_eal_check_module(const char *module_name)
+{
+	char mod_name[30]; /* Any module names can be longer than 30 bytes? */
+	int ret = 0;
+
+	if (NULL == module_name)
+		return -1;
+
+	FILE * fd = fopen("/proc/modules", "r");
+	if (NULL == fd) {
+		RTE_LOG(ERR, EAL, "Open /proc/modules failed!"
+			" error %i (%s)\n", errno, strerror(errno));
+		return -1;
+	}
+	while(!feof(fd)) {
+		fscanf(fd, "%30s %*[^\n]", mod_name);
+		if(!strcmp(mod_name, module_name)) {
+			ret = 1;
+			break;
+		}
+	}
+	fclose(fd);
+
+	return ret;
+}
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +23 -3
index c1246e8..16fe10f 100644
@@ -44,6 +44,7 @@
 #include <rte_tailq.h>
 #include <rte_eal_memconfig.h>
 #include <rte_malloc.h>
+#include <eal_private.h>
 
 #include "eal_filesystem.h"
 #include "eal_pci_init.h"
@@ -339,10 +340,12 @@ pci_vfio_get_container_fd(void)
 		ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU);
 		if (ret != 1) {
 			if (ret < 0)
-				RTE_LOG(ERR, EAL, "  could not get IOMMU type, "
-						"error %i (%s)\n", errno, strerror(errno));
+				RTE_LOG(ERR, EAL, "  could not get IOMMU type,"
+					" error %i (%s)\n", errno,
+					strerror(errno));
 			else
-				RTE_LOG(ERR, EAL, "  unsupported IOMMU type!\n");
+				RTE_LOG(ERR, EAL, "  unsupported IOMMU type"
+					" detected in VFIO\n");
 			close(vfio_container_fd);
 			return -1;
 		}
@@ -783,11 +786,28 @@ pci_vfio_enable(void)
 {
 	/* initialize group list */
 	int i;
+	int module_vfio_type1;
 
 	for (i = 0; i < VFIO_MAX_GROUPS; i++) {
 		vfio_cfg.vfio_groups[i].fd = -1;
 		vfio_cfg.vfio_groups[i].group_no = -1;
 	}
+
+	module_vfio_type1 = rte_eal_check_module("vfio_iommu_type1");
+
+	/* return error directly */
+	if (module_vfio_type1 == -1) {
+		RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
+		return -1;
+	}
+
+	/* return 0 if VFIO modules not loaded */
+	if (module_vfio_type1 == 0) {
+		RTE_LOG(INFO, EAL, "VFIO modules not all loaded,"
+			" skip VFIO support ...\n");
+		return 0;
+	}
+
 	vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
 
 	/* check if we have VFIO driver enabled */
Signature · 1 line
1.9.3

A message that is not in the archive (sent off-list or not yet received): 1418178156-4153-1-git-send-email-michael.qiu-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org

#24 lore raw
Quoted 55 lines

When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description.

This action is not safe, and of cause got error messages:

EAL: Detected 40 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

This may make user confuse, this patch make it reasonable and much more soomth to user.

---
v5 --> v4
1. Move rte_eal_check_module() body to eal.c
2. Clean up "unsupported IOMMU type" log

v4 --> v3:
1. Remove RTE_LOG for params check
2. Remove "vfio" module check as "vfio_iommu_type1" loaded indecated "vfio" loaded

v3 --> v2:
1. Add error log in rte_eal_check_module()
2. Some code clean up.

v2 --> v1:
1. Move check_module() from rte_common.h to eal_private.h and rename to rte_eal_check_module().
To make it linuxapp only.
2. Some code clean up.

 lib/librte_eal/common/eal_private.h        | 15 +++++++++++++++
 lib/librte_eal/linuxapp/eal/eal.c          | 27 +++++++++++++++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci_vfio.c | 26
+++++++++++++++++++++++---
 3 files changed, 65 insertions(+), 3 deletions(-)
lib/librte_eal/common/eal_private.h +5 -0
index 232fcec..4183b54 100644
@@ -203,4 +203,19 @@ int rte_eal_alarm_init(void);
  */
 int rte_eal_dev_init(void);
 
+/**
+ * Function is to check if the kernel module(like, vfio,
+vfio_iommu_type1,
+ * etc.) loaded.
+ *

lib/librte_eal/common/eal_private.h int rte_eal_alarm_init(void); · line 220

Quoted 10 lines
lib/librte_eal/common/eal_private.h +10 -0
+ * @param module_name
+ *	The module's name which need to be checked
+ *
+ * @return
+ * 	-1 means some error happens(NULL pointer or open failure)
+ * 	0  means the module not loaded
+ * 	1  means the module loaded
+ */
+inline int
+rte_eal_check_module(const char *module_name);

Just curious - why inline?

Quoted 16 lines
lib/librte_eal/common/eal_private.h +1 -0
+
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/linuxapp/eal/eal.c +4 -0
index 89f3b5e..40b462e 100644
@@ -859,3 +859,30 @@ int rte_eal_has_hugepages(void)  {
 	return ! internal_config.no_hugetlbfs;  }
+
+inline int
+rte_eal_check_module(const char *module_name) {
+	char mod_name[30]; /* Any module names can be longer than 30

bytes? */

lib/librte_eal/linuxapp/eal/eal.c +3 -0
+	int ret = 0;
+
+	if (NULL == module_name)

lib/librte_eal/linuxapp/eal/eal.c int rte_eal_has_hugepages(void) · line 879

Quoted 10 lines
lib/librte_eal/linuxapp/eal/eal.c +10 -0
+		return -1;
+
+	FILE * fd = fopen("/proc/modules", "r");
+	if (NULL == fd) {
+		RTE_LOG(ERR, EAL, "Open /proc/modules failed!"
+			" error %i (%s)\n", errno, strerror(errno));
+		return -1;
+	}
+	while(!feof(fd)) {
+		fscanf(fd, "%30s %*[^\n]", mod_name);

As far as I know, in fscanf terms, %30s will result in at most 30 character string, i.e. 31 bytes (30 characters + null terminator). So it probably should be %29s.

Quoted 75 lines
lib/librte_eal/linuxapp/eal/eal.c +9 -0
+		if(!strcmp(mod_name, module_name)) {
+			ret = 1;
+			break;
+		}
+	}
+	fclose(fd);
+
+	return ret;
+}
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -1
index c1246e8..16fe10f 100644
@@ -44,6 +44,7 @@
 #include <rte_tailq.h>
 #include <rte_eal_memconfig.h>
 #include <rte_malloc.h>
+#include <eal_private.h>
 
 #include "eal_filesystem.h"
 #include "eal_pci_init.h"
@@ -339,10 +340,12 @@ pci_vfio_get_container_fd(void)
 		ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
 VFIO_TYPE1_IOMMU);
 		if (ret != 1) {
 			if (ret < 0)
-				RTE_LOG(ERR, EAL, "  could not get IOMMU

type, "

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +0 -1
-						"error %i (%s)\n", errno,

strerror(errno));

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				RTE_LOG(ERR, EAL, "  could not get IOMMU

type,"

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +2 -1
+					" error %i (%s)\n", errno,
+					strerror(errno));
 			else
-				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type!\n");

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type"

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +18 -1
+					" detected in VFIO\n");
 			close(vfio_container_fd);
 			return -1;
 		}
@@ -783,11 +786,28 @@ pci_vfio_enable(void)  {
 	/* initialize group list */
 	int i;
+	int module_vfio_type1;
 
 	for (i = 0; i < VFIO_MAX_GROUPS; i++) {
 		vfio_cfg.vfio_groups[i].fd = -1;
 		vfio_cfg.vfio_groups[i].group_no = -1;
 	}
+
+	module_vfio_type1 = rte_eal_check_module("vfio_iommu_type1");
+
+	/* return error directly */
+	if (module_vfio_type1 == -1) {
+		RTE_LOG(INFO, EAL, "Could not get loaded module
 details!\n");
+		return -1;
+	}
+
+	/* return 0 if VFIO modules not loaded */
+	if (module_vfio_type1 == 0) {
+		RTE_LOG(INFO, EAL, "VFIO modules not all loaded,"
+			" skip VFIO support ...\n");
+		return 0;
+	}
+
 	vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
 
 	/* check if we have VFIO driver enabled */
--

1.9.3

#25 lore raw
Quoted 49 lines · Anatoly
Quoted 49 lines

When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description.

This action is not safe, and of cause got error messages:

EAL: Detected 40 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

This may make user confuse, this patch make it reasonable and much more soomth to user.

---
v5 --> v4
1. Move rte_eal_check_module() body to eal.c
2. Clean up "unsupported IOMMU type" log

v4 --> v3:
1. Remove RTE_LOG for params check
2. Remove "vfio" module check as "vfio_iommu_type1" loaded indecated "vfio" loaded

v3 --> v2:
1. Add error log in rte_eal_check_module()
2. Some code clean up.

v2 --> v1:
1. Move check_module() from rte_common.h to eal_private.h and rename to rte_eal_check_module().
To make it linuxapp only.
2. Some code clean up.

 lib/librte_eal/common/eal_private.h        | 15 +++++++++++++++
 lib/librte_eal/linuxapp/eal/eal.c          | 27 +++++++++++++++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci_vfio.c | 26
+++++++++++++++++++++++---
 3 files changed, 65 insertions(+), 3 deletions(-)
lib/librte_eal/common/eal_private.h +0 -0
index 232fcec..4183b54 100644
@@ -203,4 +203,19 @@ int rte_eal_alarm_init(void);
  */
 int rte_eal_dev_init(void);
 
Quoted 16 lines
Quoted 5 lines
lib/librte_eal/common/eal_private.h +5 -0
+/**
+ * Function is to check if the kernel module(like, vfio,
+vfio_iommu_type1,
+ * etc.) loaded.
+ *

lib/librte_eal/common/eal_private.h int rte_eal_alarm_init(void); · line 219

Quoted 10 lines
lib/librte_eal/common/eal_private.h +10 -0
+ * @param module_name
+ *	The module's name which need to be checked
+ *
+ * @return
+ * 	-1 means some error happens(NULL pointer or open failure)
+ * 	0  means the module not loaded
+ * 	1  means the module loaded
+ */
+inline int
+rte_eal_check_module(const char *module_name);

Just curious - why inline?

Just want to make it inline, no strong reason. If you do not agree I will remove.

Quoted 20 lines
Quoted 20 lines
lib/librte_eal/common/eal_private.h +1 -0
+
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/linuxapp/eal/eal.c +4 -0
index 89f3b5e..40b462e 100644
@@ -859,3 +859,30 @@ int rte_eal_has_hugepages(void)  {
 	return ! internal_config.no_hugetlbfs;  }
+
+inline int
+rte_eal_check_module(const char *module_name) {
+	char mod_name[30]; /* Any module names can be longer than 30

bytes? */

lib/librte_eal/linuxapp/eal/eal.c +6 -0
+	int ret = 0;
+
+	if (NULL == module_name)
+		return -1;
+
+	FILE * fd = fopen("/proc/modules", "r");
Quoted 8 lines

lib/librte_eal/linuxapp/eal/eal.c int rte_eal_has_hugepages(void) · line 879

Quoted 7 lines
lib/librte_eal/linuxapp/eal/eal.c +7 -0
+	if (NULL == fd) {
+		RTE_LOG(ERR, EAL, "Open /proc/modules failed!"
+			" error %i (%s)\n", errno, strerror(errno));
+		return -1;
+	}
+	while(!feof(fd)) {
+		fscanf(fd, "%30s %*[^\n]", mod_name);

As far as I know, in fscanf terms, %30s will result in at most 30 character string, i.e. 31 bytes (30 characters + null terminator). So it probably should be %29s.

Good catch, you are right.

Thanks,
Michael

Quoted 77 lines
Quoted 76 lines
lib/librte_eal/linuxapp/eal/eal.c +9 -0
+		if(!strcmp(mod_name, module_name)) {
+			ret = 1;
+			break;
+		}
+	}
+	fclose(fd);
+
+	return ret;
+}
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -1
index c1246e8..16fe10f 100644
@@ -44,6 +44,7 @@
 #include <rte_tailq.h>
 #include <rte_eal_memconfig.h>
 #include <rte_malloc.h>
+#include <eal_private.h>
 
 #include "eal_filesystem.h"
 #include "eal_pci_init.h"
@@ -339,10 +340,12 @@ pci_vfio_get_container_fd(void)
 		ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
 VFIO_TYPE1_IOMMU);
 		if (ret != 1) {
 			if (ret < 0)
-				RTE_LOG(ERR, EAL, "  could not get IOMMU

type, "

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +0 -1
-						"error %i (%s)\n", errno,

strerror(errno));

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				RTE_LOG(ERR, EAL, "  could not get IOMMU

type,"

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +2 -1
+					" error %i (%s)\n", errno,
+					strerror(errno));
 			else
-				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type!\n");

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type"

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +18 -1
+					" detected in VFIO\n");
 			close(vfio_container_fd);
 			return -1;
 		}
@@ -783,11 +786,28 @@ pci_vfio_enable(void)  {
 	/* initialize group list */
 	int i;
+	int module_vfio_type1;
 
 	for (i = 0; i < VFIO_MAX_GROUPS; i++) {
 		vfio_cfg.vfio_groups[i].fd = -1;
 		vfio_cfg.vfio_groups[i].group_no = -1;
 	}
+
+	module_vfio_type1 = rte_eal_check_module("vfio_iommu_type1");
+
+	/* return error directly */
+	if (module_vfio_type1 == -1) {
+		RTE_LOG(INFO, EAL, "Could not get loaded module
 details!\n");
+		return -1;
+	}
+
+	/* return 0 if VFIO modules not loaded */
+	if (module_vfio_type1 == 0) {
+		RTE_LOG(INFO, EAL, "VFIO modules not all loaded,"
+			" skip VFIO support ...\n");
+		return 0;
+	}
+
 	vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
 
 	/* check if we have VFIO driver enabled */
--

1.9.3

#26 lore raw

PATCH v6 1/1 VFIO: Avoid to enable vfio while the module not loaded

In reply to a message not in the archive

When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description.

This action is not safe, and of cause got error messages:

EAL: Detected 40 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

This may make user confuse, this patch make it reasonable and much more soomth to user.

---
v6 --> v5
1. Change rte_eal_check_module() to normal function instead of inline
2. limit fscanf to get 29 charactors not include '\0'

 lib/librte_eal/common/eal_private.h        | 14 ++++++++++++++
 lib/librte_eal/linuxapp/eal/eal.c          | 27 +++++++++++++++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci_vfio.c | 26 +++++++++++++++++++++++---
 3 files changed, 64 insertions(+), 3 deletions(-)
lib/librte_eal/common/eal_private.h +14 -0
index 232fcec..2c751c6 100644
@@ -203,4 +203,18 @@ int rte_eal_alarm_init(void);
  */
 int rte_eal_dev_init(void);
 
+/**
+ * Function is to check if the kernel module(like, vfio, vfio_iommu_type1,
+ * etc.) loaded.
+ *
+ * @param module_name
+ *	The module's name which need to be checked
+ *
+ * @return
+ * 	-1 means some error happens(NULL pointer or open failure)
+ * 	0  means the module not loaded
+ * 	1  means the module loaded
+ */
+int rte_eal_check_module(const char *module_name);
+
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/linuxapp/eal/eal.c +27 -0
index 89f3b5e..9c1a1cc 100644
@@ -859,3 +859,30 @@ int rte_eal_has_hugepages(void)
 {
 	return ! internal_config.no_hugetlbfs;
 }
+
+int
+rte_eal_check_module(const char *module_name)
+{
+	char mod_name[30]; /* Any module names can be longer than 30 bytes? */
+	int ret = 0;
+
+	if (NULL == module_name)
+		return -1;
+
+	FILE * fd = fopen("/proc/modules", "r");
+	if (NULL == fd) {
+		RTE_LOG(ERR, EAL, "Open /proc/modules failed!"
+			" error %i (%s)\n", errno, strerror(errno));
+		return -1;
+	}
+	while(!feof(fd)) {
+		fscanf(fd, "%29s %*[^\n]", mod_name);
+		if(!strcmp(mod_name, module_name)) {
+			ret = 1;
+			break;
+		}
+	}
+	fclose(fd);
+
+	return ret;
+}
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +23 -3
index c1246e8..16fe10f 100644
@@ -44,6 +44,7 @@
 #include <rte_tailq.h>
 #include <rte_eal_memconfig.h>
 #include <rte_malloc.h>
+#include <eal_private.h>
 
 #include "eal_filesystem.h"
 #include "eal_pci_init.h"
@@ -339,10 +340,12 @@ pci_vfio_get_container_fd(void)
 		ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU);
 		if (ret != 1) {
 			if (ret < 0)
-				RTE_LOG(ERR, EAL, "  could not get IOMMU type, "
-						"error %i (%s)\n", errno, strerror(errno));
+				RTE_LOG(ERR, EAL, "  could not get IOMMU type,"
+					" error %i (%s)\n", errno,
+					strerror(errno));
 			else
-				RTE_LOG(ERR, EAL, "  unsupported IOMMU type!\n");
+				RTE_LOG(ERR, EAL, "  unsupported IOMMU type"
+					" detected in VFIO\n");
 			close(vfio_container_fd);
 			return -1;
 		}
@@ -783,11 +786,28 @@ pci_vfio_enable(void)
 {
 	/* initialize group list */
 	int i;
+	int module_vfio_type1;
 
 	for (i = 0; i < VFIO_MAX_GROUPS; i++) {
 		vfio_cfg.vfio_groups[i].fd = -1;
 		vfio_cfg.vfio_groups[i].group_no = -1;
 	}
+
+	module_vfio_type1 = rte_eal_check_module("vfio_iommu_type1");
+
+	/* return error directly */
+	if (module_vfio_type1 == -1) {
+		RTE_LOG(INFO, EAL, "Could not get loaded module details!\n");
+		return -1;
+	}
+
+	/* return 0 if VFIO modules not loaded */
+	if (module_vfio_type1 == 0) {
+		RTE_LOG(INFO, EAL, "VFIO modules not all loaded,"
+			" skip VFIO support ...\n");
+		return 0;
+	}
+
 	vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
 
 	/* check if we have VFIO driver enabled */
Signature · 1 line
1.9.3

A message that is not in the archive (sent off-list or not yet received): 1418211977-8218-1-git-send-email-michael.qiu-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org

#28 lore raw
Quoted 149 lines

When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description.

This action is not safe, and of cause got error messages:

EAL: Detected 40 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

This may make user confuse, this patch make it reasonable and much more soomth to user.

---
v6 --> v5
1. Change rte_eal_check_module() to normal function instead of inline
2. limit fscanf to get 29 charactors not include '\0'

 lib/librte_eal/common/eal_private.h        | 14 ++++++++++++++
 lib/librte_eal/linuxapp/eal/eal.c          | 27 +++++++++++++++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci_vfio.c | 26
+++++++++++++++++++++++---
 3 files changed, 64 insertions(+), 3 deletions(-)
lib/librte_eal/common/eal_private.h +15 -0
index 232fcec..2c751c6 100644
@@ -203,4 +203,18 @@ int rte_eal_alarm_init(void);
  */
 int rte_eal_dev_init(void);
 
+/**
+ * Function is to check if the kernel module(like, vfio,
+vfio_iommu_type1,
+ * etc.) loaded.
+ *
+ * @param module_name
+ *	The module's name which need to be checked
+ *
+ * @return
+ * 	-1 means some error happens(NULL pointer or open failure)
+ * 	0  means the module not loaded
+ * 	1  means the module loaded
+ */
+int rte_eal_check_module(const char *module_name);
+
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/linuxapp/eal/eal.c +4 -0
index 89f3b5e..9c1a1cc 100644
@@ -859,3 +859,30 @@ int rte_eal_has_hugepages(void)  {
 	return ! internal_config.no_hugetlbfs;  }
+
+int
+rte_eal_check_module(const char *module_name) {
+	char mod_name[30]; /* Any module names can be longer than 30

bytes? */

lib/librte_eal/linuxapp/eal/eal.c +22 -0
+	int ret = 0;
+
+	if (NULL == module_name)
+		return -1;
+
+	FILE * fd = fopen("/proc/modules", "r");
+	if (NULL == fd) {
+		RTE_LOG(ERR, EAL, "Open /proc/modules failed!"
+			" error %i (%s)\n", errno, strerror(errno));
+		return -1;
+	}
+	while(!feof(fd)) {
+		fscanf(fd, "%29s %*[^\n]", mod_name);
+		if(!strcmp(mod_name, module_name)) {
+			ret = 1;
+			break;
+		}
+	}
+	fclose(fd);
+
+	return ret;
+}
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -1
index c1246e8..16fe10f 100644
@@ -44,6 +44,7 @@
 #include <rte_tailq.h>
 #include <rte_eal_memconfig.h>
 #include <rte_malloc.h>
+#include <eal_private.h>
 
 #include "eal_filesystem.h"
 #include "eal_pci_init.h"
@@ -339,10 +340,12 @@ pci_vfio_get_container_fd(void)
 		ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
 VFIO_TYPE1_IOMMU);
 		if (ret != 1) {
 			if (ret < 0)
-				RTE_LOG(ERR, EAL, "  could not get IOMMU

type, "

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +0 -1
-						"error %i (%s)\n", errno,

strerror(errno));

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				RTE_LOG(ERR, EAL, "  could not get IOMMU

type,"

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +2 -1
+					" error %i (%s)\n", errno,
+					strerror(errno));
 			else
-				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type!\n");

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type"

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +18 -1
+					" detected in VFIO\n");
 			close(vfio_container_fd);
 			return -1;
 		}
@@ -783,11 +786,28 @@ pci_vfio_enable(void)  {
 	/* initialize group list */
 	int i;
+	int module_vfio_type1;
 
 	for (i = 0; i < VFIO_MAX_GROUPS; i++) {
 		vfio_cfg.vfio_groups[i].fd = -1;
 		vfio_cfg.vfio_groups[i].group_no = -1;
 	}
+
+	module_vfio_type1 = rte_eal_check_module("vfio_iommu_type1");
+
+	/* return error directly */
+	if (module_vfio_type1 == -1) {
+		RTE_LOG(INFO, EAL, "Could not get loaded module
 details!\n");
+		return -1;
+	}
+
+	/* return 0 if VFIO modules not loaded */
+	if (module_vfio_type1 == 0) {
+		RTE_LOG(INFO, EAL, "VFIO modules not all loaded,"
+			" skip VFIO support ...\n");
+		return 0;
+	}
+
 	vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
 
 	/* check if we have VFIO driver enabled */
--
Quoted 1 line

1.9.3

#29 lore raw

Hi Thomas,

Will you plan to merge this patch? If yes, you plan to merge it into
1.8 or 2.0?

Thanks,
Michael

Quoted 153 lines · Anatoly
Quoted 149 lines

When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description.

This action is not safe, and of cause got error messages:

EAL: Detected 40 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

This may make user confuse, this patch make it reasonable and much more soomth to user.

---
v6 --> v5
1. Change rte_eal_check_module() to normal function instead of inline
2. limit fscanf to get 29 charactors not include '\0'

 lib/librte_eal/common/eal_private.h        | 14 ++++++++++++++
 lib/librte_eal/linuxapp/eal/eal.c          | 27 +++++++++++++++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci_vfio.c | 26
+++++++++++++++++++++++---
 3 files changed, 64 insertions(+), 3 deletions(-)
lib/librte_eal/common/eal_private.h +15 -0
index 232fcec..2c751c6 100644
@@ -203,4 +203,18 @@ int rte_eal_alarm_init(void);
  */
 int rte_eal_dev_init(void);
 
+/**
+ * Function is to check if the kernel module(like, vfio,
+vfio_iommu_type1,
+ * etc.) loaded.
+ *
+ * @param module_name
+ *	The module's name which need to be checked
+ *
+ * @return
+ * 	-1 means some error happens(NULL pointer or open failure)
+ * 	0  means the module not loaded
+ * 	1  means the module loaded
+ */
+int rte_eal_check_module(const char *module_name);
+
 #endif /* _EAL_PRIVATE_H_ */
lib/librte_eal/linuxapp/eal/eal.c +4 -0
index 89f3b5e..9c1a1cc 100644
@@ -859,3 +859,30 @@ int rte_eal_has_hugepages(void)  {
 	return ! internal_config.no_hugetlbfs;  }
+
+int
+rte_eal_check_module(const char *module_name) {
+	char mod_name[30]; /* Any module names can be longer than 30

bytes? */

lib/librte_eal/linuxapp/eal/eal.c +22 -0
+	int ret = 0;
+
+	if (NULL == module_name)
+		return -1;
+
+	FILE * fd = fopen("/proc/modules", "r");
+	if (NULL == fd) {
+		RTE_LOG(ERR, EAL, "Open /proc/modules failed!"
+			" error %i (%s)\n", errno, strerror(errno));
+		return -1;
+	}
+	while(!feof(fd)) {
+		fscanf(fd, "%29s %*[^\n]", mod_name);
+		if(!strcmp(mod_name, module_name)) {
+			ret = 1;
+			break;
+		}
+	}
+	fclose(fd);
+
+	return ret;
+}
lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -1
index c1246e8..16fe10f 100644
@@ -44,6 +44,7 @@
 #include <rte_tailq.h>
 #include <rte_eal_memconfig.h>
 #include <rte_malloc.h>
+#include <eal_private.h>
 
 #include "eal_filesystem.h"
 #include "eal_pci_init.h"
@@ -339,10 +340,12 @@ pci_vfio_get_container_fd(void)
 		ret = ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,
 VFIO_TYPE1_IOMMU);
 		if (ret != 1) {
 			if (ret < 0)
-				RTE_LOG(ERR, EAL, "  could not get IOMMU

type, "

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +0 -1
-						"error %i (%s)\n", errno,

strerror(errno));

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				RTE_LOG(ERR, EAL, "  could not get IOMMU

type,"

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +2 -1
+					" error %i (%s)\n", errno,
+					strerror(errno));
 			else
-				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type!\n");

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +1 -0
+				RTE_LOG(ERR, EAL, "  unsupported IOMMU

type"

lib/librte_eal/linuxapp/eal/eal_pci_vfio.c +18 -1
+					" detected in VFIO\n");
 			close(vfio_container_fd);
 			return -1;
 		}
@@ -783,11 +786,28 @@ pci_vfio_enable(void)  {
 	/* initialize group list */
 	int i;
+	int module_vfio_type1;
 
 	for (i = 0; i < VFIO_MAX_GROUPS; i++) {
 		vfio_cfg.vfio_groups[i].fd = -1;
 		vfio_cfg.vfio_groups[i].group_no = -1;
 	}
+
+	module_vfio_type1 = rte_eal_check_module("vfio_iommu_type1");
+
+	/* return error directly */
+	if (module_vfio_type1 == -1) {
+		RTE_LOG(INFO, EAL, "Could not get loaded module
 details!\n");
+		return -1;
+	}
+
+	/* return 0 if VFIO modules not loaded */
+	if (module_vfio_type1 == 0) {
+		RTE_LOG(INFO, EAL, "VFIO modules not all loaded,"
+			" skip VFIO support ...\n");
+		return 0;
+	}
+
 	vfio_cfg.vfio_container_fd = pci_vfio_get_container_fd();
 
 	/* check if we have VFIO driver enabled */
--
Quoted 1 line

1.9.3

A message that is not in the archive (sent off-list or not yet received): 533710CFB86FA344BFBF2D6802E60286CA1227-0J0gbvR4kThpB2pF5aRoyrfspsVTdybXVpNB7YpNyf8@public.gmane.org

#31 lore raw

2014-12-19 07:09, Qiu, Michael:

Quoted 4 lines

Hi Thomas,

Will you plan to merge this patch? If yes, you plan to merge it into
1.8 or 2.0?

It won't be in 1.8 for 2 reasons:
- there is a risk to break something in some environments, so need more tests
- you add a function protoype in eal common without bsd implementation

Signature · 1 line
Thomas
#32 lore raw
Quoted 8 lines · Thomas Monjalon

2014-12-19 07:09, Qiu, Michael:

Quoted 4 lines

Hi Thomas,

Will you plan to merge this patch? If yes, you plan to merge it into
1.8 or 2.0?

It won't be in 1.8 for 2 reasons:
- there is a risk to break something in some environments, so need more tests
- you add a function protoype in eal common without bsd implementation

OK, fair enough, I check with you just as a reminder to be ensure that you do not miss this thread.

A message that is not in the archive (sent off-list or not yet received): C6ECDF3AB251BE4894318F4E4512369780C24883-kPTMFJFq+rHjxeytcECX8bfspsVTdybXVpNB7YpNyf8@public.gmane.org

#34 lore raw
Quoted 10 lines
Quoted 10 lines

When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description.

This action is not safe, and of cause got error messages:

EAL: Detected 40 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

Quoted 6 lines
Quoted 4 lines

This may make user confuse, this patch make it reasonable and much more soomth to user.

Note that rte_eal_check_module has no bsd counterpart. It could be needed later.

Applied

Thanks

Signature · 1 line
Thomas
#35 lore raw

Yep, apologies, it's my fault as it was my suggestion. I knew there was a linuxapp-only EAL header, for some reason I thought it's eal_private. Any suggestions on where to put this function? I don't think BSD needs this function.

Thanks,
Anatoly

Quoted 33 lines

-----Original Message-----
From: Thomas Monjalon [mailto:[email protected]]
Sent: Thursday, January 15, 2015 1:38 PM
To: Qiu, Michael
Cc: Burakov, Anatoly; [email protected]; Xie, Huawei
Subject: Re: [PATCH v6] VFIO: Avoid to enable vfio while the module not loaded

Quoted 10 lines
Quoted 10 lines

When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description.

This action is not safe, and of cause got error messages:

EAL: Detected 40 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

Quoted 6 lines
Quoted 4 lines

This may make user confuse, this patch make it reasonable and much more soomth to user.

Note that rte_eal_check_module has no bsd counterpart. It could be needed later.

Applied

Thanks
--
Thomas

A message that is not in the archive (sent off-list or not yet received): C6ECDF3AB251BE4894318F4E4512369780C3CDC1-kPTMFJFq+rHjxeytcECX8bfspsVTdybXVpNB7YpNyf8@public.gmane.org

#37 lore raw

2015-01-15 13:42, Burakov, Anatoly:

Quoted 3 lines

Yep, apologies, it's my fault as it was my suggestion.
I knew there was a linuxapp-only EAL header, for some reason I thought it's eal_private. Any suggestions on where to put this function? I don't think BSD needs this function.

No, it's OK. I think it could be needed in bsd if a PMD depends on a kernel driver or try to unload one.

PS: please don't top post

Signature · 1 line
Thomas
Quoted 33 lines
Quoted 33 lines

-----Original Message-----
From: Thomas Monjalon [mailto:[email protected]]
Sent: Thursday, January 15, 2015 1:38 PM
To: Qiu, Michael
Cc: Burakov, Anatoly; [email protected]; Xie, Huawei
Subject: Re: [PATCH v6] VFIO: Avoid to enable vfio while the module not loaded

Quoted 10 lines
Quoted 10 lines

When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description.

This action is not safe, and of cause got error messages:

EAL: Detected 40 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

Quoted 6 lines
Quoted 4 lines

This may make user confuse, this patch make it reasonable and much more soomth to user.

Note that rte_eal_check_module has no bsd counterpart. It could be needed later.

Applied

Thanks
--
Thomas

#38 lore raw
In reply to a message not in the archive

Hi Michael

Quoted 5 lines

When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description.

This action is not safe, and of cause got error messages:

Quoted 7 lines

EAL: Detected 40 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

This may make user confuse, this patch make it reasonable and much more soomth to user.

Not sure I agree with the premise of this patch.

First of all, if VFIO driver is not enabled, the container file would not be present and you would get a different error (namely, "cannot open VFIO container", in pci_vfio_get_container_fd()). If you have a container file, that means VFIO driver is loaded, so I'm not sure why you get the "unsupported IOMMU type" error. I suppose it could happen when vfio is loaded but vfio_iommu_type1 isn't?

And even then, this error is harmless and doesn't do anything, so I'm not sure what this patch is supposed to fix. The error messages tells the user exactly what happens.

Thanks,
Anatoly

#39 lore raw
Quoted 12 lines · Anatoly

Hi Michael

Quoted 10 lines

When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description.

This action is not safe, and of cause got error messages:

EAL: Detected 40 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

Quoted 5 lines
Quoted 2 lines

This may make user confuse, this patch make it reasonable and much more soomth to user.

Not sure I agree with the premise of this patch.

First of all, if VFIO driver is not enabled, the container file would not be present and you would get a different error (namely, "cannot open VFIO container", in pci_vfio_get_container_fd()). If you have a container file, that means VFIO driver is loaded, so I'm not sure why you get the "unsupported IOMMU type" error. I suppose it could happen when vfio is loaded but vfio_iommu_type1 isn't?

But indeed, when try to unload both vfio and vfio_iommu_type1, /dev/vfio/vfio still there, I'm also surprise.

My ENV is fedora20, kernel version 3.6.7-200 X86_64.

Believe or not, you can have a try, it seems a kernel issue.

When you unload both two modules, then open /dev/vfio/vfio, you will find it can be opened with no errors(but this time both two modules loaded automatically, strange enough)

Also you can use ioctl to get API Version. But when you try to get the iommu type, it will return a "0" not expect value of '1'.

Then you can shutdown DPDK, reopen like test-pmd, all works fine :)

I will take a deep look at in the kernel side, to find out why this happens.

Thanks,
Michael

Quoted 5 lines

And even then, this error is harmless and doesn't do anything, so I'm not sure what this patch is supposed to fix. The error messages tells the user exactly what happens.

Thanks,
Anatoly

A message that is not in the archive (sent off-list or not yet received): 533710CFB86FA344BFBF2D6802E60286C9CCC1-0J0gbvR4kThpB2pF5aRoyrfspsVTdybXVpNB7YpNyf8@public.gmane.org

#41 lore raw

Hi Michael

Quoted 11 lines

But indeed, when try to unload both vfio and vfio_iommu_type1, /dev/vfio/vfio still there, I'm also surprise.

My ENV is fedora20, kernel version 3.6.7-200 X86_64.

Believe or not, you can have a try, it seems a kernel issue.

When you unload both two modules, then open /dev/vfio/vfio, you will find it can be opened with no errors(but this time both two modules loaded automatically, strange enough)

Thanks to Sergio, we found a most likely cause for this. This patch to Linux kernel by Alex Williamson of Red Hat:

https://lkml.org/lkml/2013/12/12/421

it seems, however, that it has been merged into 3.14. Your kernel, by your own admission, is 3.6. Are you sure this is the right kernel version? Because my own machine has Fedora 18 with a 3.11 kernel, and it (correctly) does not display this behavior. So unless Fedora 20 backported those changes to kernel 3.6, this shouldn't happen on your set up. (but it doesn't really matter, just FYI - the patch still should be fixed and resubmitted, just as we discussed)

Thanks,
Anatoly

#42 lore raw
Quoted 13 lines · Anatoly

Hi Michael

Quoted 11 lines

But indeed, when try to unload both vfio and vfio_iommu_type1, /dev/vfio/vfio still there, I'm also surprise.

My ENV is fedora20, kernel version 3.6.7-200 X86_64.

Believe or not, you can have a try, it seems a kernel issue.

When you unload both two modules, then open /dev/vfio/vfio, you will find it can be opened with no errors(but this time both two modules loaded automatically, strange enough)

Quoted 5 lines

Thanks to Sergio, we found a most likely cause for this. This patch to Linux kernel by Alex Williamson of Red Hat:

https://lkml.org/lkml/2013/12/12/421

it seems, however, that it has been merged into 3.14. Your kernel, by your own admission, is 3.6. Are you sure this is the right kernel version? Because my own machine has Fedora 18 with a 3.11 kernel, and it (correctly) does not

Sorry, the kernel version is 3.16 :), just make a mistake :)

Thanks,
Michael

Quoted 5 lines

display this behavior. So unless Fedora 20 backported those changes to kernel 3.6, this shouldn't happen on your set up. (but it doesn't really matter, just FYI - the patch still should be fixed and resubmitted, just as we discussed)

Thanks,
Anatoly

#43 lore raw
In reply to a message not in the archive

Hi Michael

Quoted 5 lines

When vfio module is not loaded when kernel support vfio feature, the routine still try to open the container to get file description.

This action is not safe, and of cause got error messages:

Quoted 7 lines

EAL: Detected 40 lcore(s)
EAL: unsupported IOMMU type!
EAL: VFIO support could not be initialized
EAL: Setting up memory...

This may make user confuse, this patch make it reasonable and much more soomth to user.

Not sure I agree with the premise of this patch.

First of all, if VFIO driver is not enabled, the container file would not be present and you would get a different error (namely, "cannot open VFIO container", in pci_vfio_get_container_fd()). If you have a container file, that means VFIO driver is loaded, so I'm not sure why you get the "unsupported IOMMU type" error. I suppose it could happen when vfio is loaded but vfio_iommu_type1 isn't?

And even then, this error is harmless and doesn't do anything, so I'm not sure what this patch is supposed to fix. The error messages tells the user exactly what happens.

Thanks,
Anatoly