From mboxrd@z Thu Jan 1 00:00:00 1970 From: lilinzhe Subject: [PATCH] mbuf: fix atomic refcnt update synchronization Date: Fri, 2 Sep 2016 13:25:06 +0800 Message-ID: <1472793906-5699-1-git-send-email-slayercat.subscription@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: =?UTF-8?q?=E6=9D=8E=E6=9E=97=E5=93=B2?= To: dev@dpdk.org Return-path: Received: from mail-pa0-f68.google.com (mail-pa0-f68.google.com [209.85.220.68]) by dpdk.org (Postfix) with ESMTP id 8DFC137AA for ; Fri, 2 Sep 2016 07:25:14 +0200 (CEST) Received: by mail-pa0-f68.google.com with SMTP id hm5so582151pac.1 for ; Thu, 01 Sep 2016 22:25:14 -0700 (PDT) List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" From: 李林哲 chagne atomic ref update to always call atomic_add when mbuf is allocated by cpu1 and freed by cpu2. cpu1 cache may not be updated by such a set operation. causes refcnt reads incorrect values. --- lib/librte_mbuf/rte_mbuf.h | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/lib/librte_mbuf/rte_mbuf.h b/lib/librte_mbuf/rte_mbuf.h index 7ea66ed..63e6588 100644 --- a/lib/librte_mbuf/rte_mbuf.h +++ b/lib/librte_mbuf/rte_mbuf.h @@ -786,7 +786,7 @@ struct rte_mbuf { */ union { rte_atomic16_t refcnt_atomic; /**< Atomically accessed refcnt */ - uint16_t refcnt; /**< Non-atomically accessed refcnt */ + volatile uint16_t refcnt; /**< Non-atomically accessed refcnt */ }; uint8_t nb_segs; /**< Number of segments. */ uint8_t port; /**< Input port. */ @@ -1060,16 +1060,12 @@ static inline uint16_t rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value) { /* - * The atomic_add is an expensive operation, so we don't want to - * call it in the case where we know we are the uniq holder of - * this mbuf (i.e. ref_cnt == 1). Otherwise, an atomic - * operation has to be used because concurrent accesses on the - * reference counter can occur. + * This shell always call atomic_add + * + * when mbuf is allocated by cpu1 and freed by cpu2. cpu1 cache may not be updated by such a set operation. + * causes refcnt reads incorrect values + * */ - if (likely(rte_mbuf_refcnt_read(m) == 1)) { - rte_mbuf_refcnt_set(m, 1 + value); - return 1 + value; - } return (uint16_t)(rte_atomic16_add_return(&m->refcnt_atomic, value)); } -- 2.7.0.rc2