From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <liucheng11@huawei.com>
Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187])
 by dpdk.org (Postfix) with ESMTP id B21A47CDE
 for <dev@dpdk.org>; Thu, 21 Sep 2017 05:19:29 +0200 (CEST)
Received: from 172.30.72.55 (EHLO DGGEML402-HUB.china.huawei.com)
 ([172.30.72.55])
 by dggrg01-dlp.huawei.com (MOS 4.4.6-GA FastPath queued)
 with ESMTP id AWS42980; Thu, 21 Sep 2017 11:19:27 +0800 (CST)
Received: from DGGEML502-MBX.china.huawei.com ([169.254.2.114]) by
 DGGEML402-HUB.china.huawei.com ([fe80::fca6:7568:4ee3:c776%31]) with mapi id
 14.03.0301.000; Thu, 21 Sep 2017 11:19:22 +0800
From: "liucheng (J)" <liucheng11@huawei.com>
To: "dev@dpdk.org" <dev@dpdk.org>
Thread-Topic: [dpdk-dev] [PATCH]librte_eal:add heap lock in
 malloc_heap_get_stats
Thread-Index: AdMyiGmQJ6oOhd2HQFSE0Q/Nx0NNOQ==
Date: Thu, 21 Sep 2017 03:19:21 +0000
Message-ID: <8B0A0B0226AB0F47A8D0EBBBBA1F005102BC7F7D@DGGEML502-MBX.china.huawei.com>
Accept-Language: en-US
Content-Language: zh-CN
X-MS-Has-Attach: 
X-MS-TNEF-Correlator: 
x-originating-ip: [10.177.253.242]
MIME-Version: 1.0
X-CFilter-Loop: Reflected
X-Mirapoint-Virus-RAPID-Raw: score=unknown(0),
 refid=str=0001.0A090203.59C32FBF.00A6, ss=1, re=0.000, recu=0.000, reip=0.000,
 cl=1, cld=1, fgs=0, ip=169.254.2.114,
 so=2014-11-16 11:51:01, dmn=2013-03-21 17:37:32
X-Mirapoint-Loop-Id: 71d8172bcb911666744ef971097ea138
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
X-Content-Filtered-By: Mailman/MimeDel 2.1.15
Subject: [dpdk-dev] [PATCH]librte_eal:add heap lock in malloc_heap_get_stats
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <http://dpdk.org/ml/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://dpdk.org/ml/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <http://dpdk.org/ml/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
X-List-Received-Date: Thu, 21 Sep 2017 03:19:31 -0000

There is no lock in function malloc_heap_get_stats. When we call this funct=
ion, the elem may free by other thread.

the call stack:
Core was generated by `ovs-vswitchd unix:/usr/var/run/openvswitch/db.sock -=
vconsole:emer -vsyslog:info'.
Program terminated with signal 11, Segmentation fault.
#0  malloc_heap_get_stats (heap=3D0x7f3abf08ee1c, socket_stats=3Dsocket_sta=
ts@entry=3D0x7ffce811ffc0) at /usr/src/debug/dpdk-16.04/lib/librte_eal/comm=
on/malloc_heap.c:198
198                              socket_stats->heap_freesz_bytes +=3D elem-=
>size;
(gdb) bt
#0  malloc_heap_get_stats (heap=3D0x7f3abf08ee1c, socket_stats=3Dsocket_sta=
ts@entry=3D0x7ffce811ffc0) at /usr/src/debug/dpdk-16.04/lib/librte_eal/comm=
on/malloc_heap.c:198
#1  0x00007f3abd6d6299 in rte_malloc_get_socket_stats (socket=3Dsocket@entr=
y=3D0, socket_stats=3Dsocket_stats@entry=3D0x7ffce811ffc0) at /usr/src/debu=
g/dpdk-16.04/lib/librte_eal/common/rte_malloc.c:214
......



lib/librte_eal/common/malloc_heap.c | 3 +++
lib/librte_eal/common/malloc_heap.h | 2 +-
2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/lib/librte_eal/common/malloc_heap.c b/lib/librte_eal/common/ma=
lloc_heap.c
index 267a4c6..1952ddd 100644
--- a/lib/librte_eal/common/malloc_heap.c
+++ b/lib/librte_eal/common/malloc_heap.c
@@ -189,6 +189,7 @@ malloc_heap_get_stats(const struct malloc_heap *heap,
       socket_stats->heap_freesz_bytes =3D 0;
       socket_stats->greatest_free_size =3D 0;
+       rte_spinlock_lock(&heap->lock);
       /* Iterate through free list */
       for (idx =3D 0; idx < RTE_HEAP_NUM_FREELISTS; idx++) {
                for (elem =3D LIST_FIRST(&heap->free_head[idx]);
@@ -200,6 +201,8 @@ malloc_heap_get_stats(const struct malloc_heap *heap,
                                   socket_stats->greatest_free_size =3D ele=
m->size;
                }
       }
+       rte_spinlock_unlock(&heap->lock);
+
       /* Get stats on overall heap and allocated memory on this heap */
       socket_stats->heap_totalsz_bytes =3D heap->total_size;
       socket_stats->heap_allocsz_bytes =3D (socket_stats->heap_totalsz_byt=
es -
diff --git a/lib/librte_eal/common/malloc_heap.h b/lib/librte_eal/common/ma=
lloc_heap.h
index 3ccbef0..3b1166f 100644
--- a/lib/librte_eal/common/malloc_heap.h
+++ b/lib/librte_eal/common/malloc_heap.h
@@ -57,7 +57,7 @@ malloc_heap_alloc(struct malloc_heap *heap,  const char *=
type, size_t size,
                unsigned flags, size_t align, size_t bound);
 int
-malloc_heap_get_stats(const struct malloc_heap *heap,
+malloc_heap_get_stats(struct malloc_heap *heap,
                struct rte_malloc_socket_stats *socket_stats);
 int