From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <slayercat.subscription@gmail.com>
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 <dev@dpdk.org>; Fri,  2 Sep 2016 07:25:14 +0200 (CEST)
Received: by mail-pa0-f68.google.com with SMTP id hm5so582151pac.1
 for <dev@dpdk.org>; Thu, 01 Sep 2016 22:25:14 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;
 h=from:to:cc:subject:date:message-id:mime-version
 :content-transfer-encoding;
 bh=Kq0BRQxbXKNMOdtflvqjLlCPMddeiNrXI2izzVd3MvY=;
 b=S36Hr76IXzz8VWShJEK9j5bTV1hOOMdD17jgWXT2zvRuccpEvdTvPulNF0QYMyhrIx
 uvDZsSPB/vFVuqFtio5wd8Qo3iRrl15qJHYUnzGxqQMSRJ4K5x6NlZAH8OFVqOPHlec4
 /6/90wFrGNiSj9x9Bwco/4Fdi0fvfZvn2lbw8gP6wYpMYizznuLAaUZxtI222Rjo48jr
 o3xJZ5qjVdoqoEwapsM8x5IJWZorcI3+qkwcl3SnGz2zu4nDD1DQEqHVcNwu0VgPLrgp
 uBvtjP8SbEfdVyQATmqjhTtCvKZkmkaShSz/cFfBpOwKuNeQb5UdbGM/HsAeoHWs9xt5
 rfVA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20130820;
 h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version
 :content-transfer-encoding;
 bh=Kq0BRQxbXKNMOdtflvqjLlCPMddeiNrXI2izzVd3MvY=;
 b=MUtF6m2x9o3tRtXPy1SbKbWhcQcunvd2J/00+OwExlpwJhCptCWzc23IZi0L0b7BPa
 H6H95/yzauVUVMW2LO/kf3YJVHOi01tK/902FBOIRphpZr2x0+HnhV8UdtGocS9sqawX
 RjRc4nrFDwk4M+IgU9ICVc/pPhGKkcLlJA7V2W4t4dJwM3h+9x2p02s9Ft9fBY4+OaiL
 0ahk4SZOzqryxNRS3CXNjxk0H4fPndH5Dz/DWJLjYhAxaUFO1V9YGof1EaXsuapTOmLu
 JQvWILJW3lR3LG0xQw6Xp91Lijd81WdA+XIku9gA1dxtdEJ86wbyzggiW49WC3wSVsfW
 6JIg==
X-Gm-Message-State: AE9vXwO0BYC18oUAZgYa8/8lc/PhFWf2iyQgi+fBh58BwTbwkpzDzAZr/TIJPZI6/DSA1w==
X-Received: by 10.66.242.201 with SMTP id ws9mr33035674pac.7.1472793913530;
 Thu, 01 Sep 2016 22:25:13 -0700 (PDT)
Received: from localhost.localdomain
 (2607-8700-0101-a4c2-ca35-dc2d-6794-aee9.16clouds.com.
 [2607:8700:101:a4c2:ca35:dc2d:6794:aee9])
 by smtp.googlemail.com with ESMTPSA id o80sm11300825pfa.67.2016.09.01.22.25.12
 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
 Thu, 01 Sep 2016 22:25:13 -0700 (PDT)
From: lilinzhe <slayercat.subscription@gmail.com>
To: dev@dpdk.org
Cc: =?UTF-8?q?=E6=9D=8E=E6=9E=97=E5=93=B2?= <lilinzhe@ijinshan.com>
Date: Fri,  2 Sep 2016 13:25:06 +0800
Message-Id: <1472793906-5699-1-git-send-email-slayercat.subscription@gmail.com>
X-Mailer: git-send-email 2.7.0.rc2
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Subject: [dpdk-dev] [PATCH] mbuf: fix atomic refcnt update synchronization
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: patches and discussions about DPDK <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: Fri, 02 Sep 2016 05:25:14 -0000

From: 李林哲 <lilinzhe@ijinshan.com>

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