From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by dpdk.org (Postfix) with ESMTP id 4D1CF106B for ; Wed, 25 Mar 2015 16:00:57 +0100 (CET) Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga103.jf.intel.com with ESMTP; 25 Mar 2015 08:00:55 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.11,465,1422950400"; d="scan'208";a="697614161" Received: from irsmsx103.ger.corp.intel.com ([163.33.3.157]) by fmsmga002.fm.intel.com with ESMTP; 25 Mar 2015 08:00:54 -0700 Received: from irsmsx108.ger.corp.intel.com ([169.254.11.49]) by IRSMSX103.ger.corp.intel.com ([169.254.3.143]) with mapi id 14.03.0224.002; Wed, 25 Mar 2015 15:00:53 +0000 From: "Iremonger, Bernard" To: Tetsuya Mukawa , "dev@dpdk.org" Thread-Topic: [PATCH v2 3/6] eal: Fix memory leaks and needless increment of pci_map_addr Thread-Index: AQHQZemn8LsDUZydmkmk/mpbBaJzB50tSyqA Date: Wed, 25 Mar 2015 15:00:52 +0000 Message-ID: <8CEF83825BEC744B83065625E567D7C204A02409@IRSMSX108.ger.corp.intel.com> References: <1426584645-28828-7-git-send-email-mukawa@igel.co.jp> <1427170717-13879-1-git-send-email-mukawa@igel.co.jp> <1427170717-13879-4-git-send-email-mukawa@igel.co.jp> In-Reply-To: <1427170717-13879-4-git-send-email-mukawa@igel.co.jp> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [163.33.239.180] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Subject: Re: [dpdk-dev] [PATCH v2 3/6] eal: Fix memory leaks and needless increment of pci_map_addr 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: Wed, 25 Mar 2015 15:00:58 -0000 > -----Original Message----- > From: Tetsuya Mukawa [mailto:mukawa@igel.co.jp] > Sent: Tuesday, March 24, 2015 4:19 AM > To: dev@dpdk.org > Cc: Iremonger, Bernard; Richardson, Bruce; david.marchand@6wind.com; Tets= uya Mukawa > Subject: [PATCH v2 3/6] eal: Fix memory leaks and needless increment of p= ci_map_addr >=20 > This patch fixes following memory leaks. > - When pci_map_resource() is failed but path is allocated correctly, > path won't be freed in pci_uio_map_recource(). > - When open() is failed, uio_res won't be freed in > pci_uio_map_resource(). > - When pci_uio_unmap() is called, path should be freed. >=20 > Also, fixes below. > - When pci_map_resource() is failed, mapaddr will be MAP_FAILED. > In this case, pci_map_addr should not be incremented in > pci_uio_map_resource(). > - To shrink code, move close(). > - Remove fail variable. >=20 > Signed-off-by: Tetsuya Mukawa > --- > lib/librte_eal/linuxapp/eal/eal_pci_uio.c | 35 ++++++++++++++++++-------= ------ > 1 file changed, 20 insertions(+), 15 deletions(-) >=20 > diff --git a/lib/librte_eal/linuxapp/eal/eal_pci_uio.c b/lib/librte_eal/l= inuxapp/eal/eal_pci_uio.c > index f0277be..0128cec 100644 > --- a/lib/librte_eal/linuxapp/eal/eal_pci_uio.c > +++ b/lib/librte_eal/linuxapp/eal/eal_pci_uio.c > @@ -333,7 +333,6 @@ pci_uio_map_resource(struct rte_pci_device *dev) > maps =3D uio_res->maps; > for (i =3D 0, map_idx =3D 0; i !=3D PCI_MAX_RESOURCE; i++) { > int fd; > - int fail =3D 0; >=20 > /* skip empty BAR */ > phaddr =3D dev->mem_resource[i].phys_addr; @@ -347,6 +346,11 @@ > pci_uio_map_resource(struct rte_pci_device *dev) > loc->domain, loc->bus, loc->devid, loc->function, > i); >=20 > + /* allocate memory to keep path */ > + maps[map_idx].path =3D rte_malloc(NULL, strlen(devname) + 1, 0); > + if (maps[map_idx].path =3D=3D NULL) > + goto fail0; > + > /* > * open resource file, to mmap it > */ > @@ -354,7 +358,7 @@ pci_uio_map_resource(struct rte_pci_device *dev) > if (fd < 0) { > RTE_LOG(ERR, EAL, "Cannot open %s: %s\n", > devname, strerror(errno)); > - return -1; > + goto fail1; > } >=20 > /* try mapping somewhere close to the end of hugepages */ @@ -363,23 += 367,13 @@ > pci_uio_map_resource(struct rte_pci_device *dev) >=20 > mapaddr =3D pci_map_resource(pci_map_addr, fd, 0, > (size_t)dev->mem_resource[i].len, 0); > + close(fd); > if (mapaddr =3D=3D MAP_FAILED) > - fail =3D 1; > + goto fail1; >=20 > pci_map_addr =3D RTE_PTR_ADD(mapaddr, > (size_t)dev->mem_resource[i].len); >=20 > - maps[map_idx].path =3D rte_malloc(NULL, strlen(devname) + 1, 0); > - if (maps[map_idx].path =3D=3D NULL) > - fail =3D 1; > - > - if (fail) { > - rte_free(uio_res); > - close(fd); > - return -1; > - } > - close(fd); > - > maps[map_idx].phaddr =3D dev->mem_resource[i].phys_addr; > maps[map_idx].size =3D dev->mem_resource[i].len; > maps[map_idx].addr =3D mapaddr; > @@ -394,6 +388,15 @@ pci_uio_map_resource(struct rte_pci_device *dev) > TAILQ_INSERT_TAIL(uio_res_list, uio_res, next); >=20 > return 0; > + > +fail1: > + rte_free(maps[map_idx].path); > +fail0: > + for (i =3D 0; i < map_idx; i++) Hi Tetsuya, fail1: falls through to fail0: Would it be cleaner to drop fail1: and change the for loop in fail0: to for (i =3D 0; i <=3D map_idx; i++)=20 Regards, Bernard. > + rte_free(maps[i].path); > + rte_free(uio_res); > + > + return -1; > } >=20 > #ifdef RTE_LIBRTE_EAL_HOTPLUG > @@ -405,9 +408,11 @@ pci_uio_unmap(struct mapped_pci_resource *uio_res) > if (uio_res =3D=3D NULL) > return; >=20 > - for (i =3D 0; i !=3D uio_res->nb_maps; i++) > + for (i =3D 0; i !=3D uio_res->nb_maps; i++) { > pci_unmap_resource(uio_res->maps[i].addr, > (size_t)uio_res->maps[i].size); > + rte_free(uio_res->maps[i].path); > + } > } >=20 > static struct mapped_pci_resource * > -- > 1.9.1