From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by dpdk.org (Postfix) with ESMTP id 321143F9 for ; Mon, 8 Dec 2014 08:26:04 +0100 (CET) Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga101.fm.intel.com with ESMTP; 07 Dec 2014 23:26:03 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="4.97,862,1389772800"; d="scan'208";a="426371534" Received: from pgsmsx105.gar.corp.intel.com ([10.221.44.96]) by FMSMGA003.fm.intel.com with ESMTP; 07 Dec 2014 23:15:28 -0800 Received: from shsmsx102.ccr.corp.intel.com (10.239.4.154) by pgsmsx105.gar.corp.intel.com (10.221.44.96) with Microsoft SMTP Server (TLS) id 14.3.195.1; Mon, 8 Dec 2014 15:25:56 +0800 Received: from shsmsx101.ccr.corp.intel.com ([169.254.1.110]) by shsmsx102.ccr.corp.intel.com ([169.254.2.216]) with mapi id 14.03.0195.001; Mon, 8 Dec 2014 15:25:54 +0800 From: "Qiu, Michael" To: "Burakov, Anatoly" , Michael Qiu , "dev@dpdk.org" Thread-Topic: [PATCH v2] VFIO: Avoid to enable vfio while the module not loaded Thread-Index: AQHQEFe5Z9KwEf2S2E2ARpK72Nbfmw== Date: Mon, 8 Dec 2014 07:25:53 +0000 Message-ID: <533710CFB86FA344BFBF2D6802E60286C9D920@SHSMSX101.ccr.corp.intel.com> References: <1417664219-19679-1-git-send-email-michael.qiu@intel.com> <1417687227-21854-1-git-send-email-michael.qiu@intel.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.239.127.40] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Subject: Re: [dpdk-dev] [PATCH v2] VFIO: Avoid to enable vfio while the module not loaded X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 08 Dec 2014 07:26:05 -0000 On 12/5/2014 6:00 PM, Burakov, Anatoly wrote:=0A= > Hi Michael,=0A= >=0A= > Few nitpicks :-) (wording of the log message I guess is up to Thomas, I w= on't comment on that)=0A= >=0A= >> lib/librte_eal/common/eal_private.h | 36=0A= >> ++++++++++++++++++++++++++++++=0A= >> lib/librte_eal/linuxapp/eal/eal_pci_vfio.c | 24 +++++++++++++++++---=0A= >> 2 files changed, 57 insertions(+), 3 deletions(-)=0A= >>=0A= >> diff --git a/lib/librte_eal/common/eal_private.h=0A= >> b/lib/librte_eal/common/eal_private.h=0A= >> index 232fcec..e741bdb 100644=0A= >> --- a/lib/librte_eal/common/eal_private.h=0A= >> +++ b/lib/librte_eal/common/eal_private.h=0A= >> @@ -35,6 +35,7 @@=0A= >> #define _EAL_PRIVATE_H_=0A= >>=0A= >> #include =0A= >> +#include =0A= >>=0A= >> /**=0A= >> * Initialize the memzone subsystem (private to eal).=0A= >> @@ -203,4 +204,39 @@ int rte_eal_alarm_init(void);=0A= >> */=0A= >> int rte_eal_dev_init(void);=0A= >>=0A= >> +/**=0A= >> + * Function is to check if the kernel module(like, vfio,=0A= >> +vfio_iommu_type1,=0A= >> + * etc.) loaded.=0A= >> + *=0A= >> + * @param module_name=0A= >> + * The module's name which need to be checked=0A= >> + *=0A= >> + * @return=0A= >> + * -1 means error happens(NULL pointer or open failure)=0A= >> + * 0 means the module not loaded=0A= >> + * 1 means the module loaded=0A= >> + */=0A= >> +static inline int=0A= >> +rte_eal_check_module(const char *module_name) {=0A= >> + char mod_name[30]; /* Any module names can be longer than 30=0A= >> bytes? */=0A= >> + int ret =3D 0;=0A= >> +=0A= >> + if (NULL =3D=3D module_name)=0A= >> + return -1;=0A= >> + FILE * fd =3D fopen("/proc/modules", "r");=0A= >> + if (fd =3D=3D NULL)=0A= >> + return -1;=0A= > Can we add RTE_LOG statement here, with an strerror(errno) like in other = places? Fopen failed, we should at least know why :-)=0A= >=0A= >> + while(!feof(fd)) {=0A= >> + fscanf(fd, "%s %*[^\n]", mod_name);=0A= >> + if(!strcmp(mod_name, module_name)) {=0A= > Probably should use strncmp instead of strcmp.=0A= =0A= I don't think so, if we check module "vfio", but if given module name=0A= is "vfio_xx", it will also correct if use strncmp.=0A= =0A= Thanks,=0A= Michael=0A= >=0A= >> + ret =3D 1;=0A= >> + break;=0A= >> + }=0A= >> + }=0A= >> + fclose(fd);=0A= >> +=0A= >> + return ret;=0A= >> +}=0A= >> +=0A= >> #endif /* _EAL_PRIVATE_H_ */=0A= >> diff --git a/lib/librte_eal/linuxapp/eal/eal_pci_vfio.c=0A= >> b/lib/librte_eal/linuxapp/eal/eal_pci_vfio.c=0A= >> index c1246e8..52ab2d0 100644=0A= >> --- a/lib/librte_eal/linuxapp/eal/eal_pci_vfio.c=0A= >> +++ b/lib/librte_eal/linuxapp/eal/eal_pci_vfio.c=0A= >> @@ -44,6 +44,7 @@=0A= >> #include =0A= >> #include =0A= >> #include =0A= >> +#include =0A= >>=0A= >> #include "eal_filesystem.h"=0A= >> #include "eal_pci_init.h"=0A= >> @@ -339,10 +340,13 @@ pci_vfio_get_container_fd(void)=0A= >> ret =3D ioctl(vfio_container_fd, VFIO_CHECK_EXTENSION,=0A= >> VFIO_TYPE1_IOMMU);=0A= >> if (ret !=3D 1) {=0A= >> if (ret < 0)=0A= >> - RTE_LOG(ERR, EAL, " could not get IOMMU=0A= >> type, "=0A= >> - "error %i (%s)\n", errno,=0A= >> strerror(errno));=0A= >> + RTE_LOG(ERR, EAL, " could not get IOMMU=0A= >> type,"=0A= >> + " error %i (%s)\n", errno,=0A= >> + strerror(errno));=0A= >> else=0A= >> - RTE_LOG(ERR, EAL, " unsupported IOMMU=0A= >> type!\n");=0A= >> + RTE_LOG(ERR, EAL, " unsupported IOMMU=0A= >> type! "=0A= >> + "expect: VFIO_TYPE1_IOMMU, "=0A= >> + "actual: %d\n", ret);=0A= > I'm not even sure we need this "expected" bit at all. We don't get back t= he IOMMU type VFIO currently supports; rather, this code checks if VFIO's s= upport for VFIO_TYPE1_IOMMU is enabled or not. So I would change the error = message to something more descriptive, such as " required IOMMU type suppor= t not present in VFIO!\n", and get rid of the "expected".=0A= >=0A= >> close(vfio_container_fd);=0A= >> return -1;=0A= >> }=0A= >> @@ -788,6 +792,20 @@ pci_vfio_enable(void)=0A= >> vfio_cfg.vfio_groups[i].fd =3D -1;=0A= >> vfio_cfg.vfio_groups[i].group_no =3D -1;=0A= >> }=0A= >> +=0A= >> + /* return error directly */=0A= >> + if (rte_eal_check_module("vfio") =3D=3D -1 ||=0A= >> + rte_eal_check_module("vfio_iommu_type1") =3D=3D -1)=0A= >> + return -1;=0A= >> +=0A= >> + /* return 0 if not all VFIO modules loaded */=0A= >> + if (rte_eal_check_module("vfio") =3D=3D 0 ||=0A= >> + rte_eal_check_module("vfio_iommu_type1") =3D=3D 0) {=0A= >> + RTE_LOG(INFO, EAL, "VFIO modules not all loaded,"=0A= >> + " skip VFIO support ...\n");=0A= >> + return 0;=0A= >> + }=0A= > Can we perhaps make one call per module instead of two? i.e. something li= ke:=0A= >=0A= > int vfio_ret, vfio_ type1_ret;=0A= > vfio_ret =3D rte_eal_check_module("vfio");=0A= > vfio_type1_ret =3D rte_eal_check_module("vfio_iommu_type1");=0A= >=0A= > if (vfio_ret =3D=3D -1 || vfio_type1_ret =3D=3D -1)=0A= > return -1;=0A= > else if (vfio_ret =3D=3D 0 || vfio_type1_ret =3D=3D 0) {=0A= > ....=0A= > return 0;=0A= > }=0A= >=0A= >> +=0A= >> vfio_cfg.vfio_container_fd =3D pci_vfio_get_container_fd();=0A= >>=0A= >> /* check if we have VFIO driver enabled */=0A= >> --=0A= >> 1.9.3=0A= >=0A= =0A=