DPDK patches and discussions
 help / color / mirror / Atom feed
From: lilinzhe <slayercat.subscription@gmail.com>
To: dev@dpdk.org
Cc: 李林哲 <lilinzhe@ijinshan.com>
Subject: [dpdk-dev] [PATCH] mbuf: fix atomic refcnt update synchronization
Date: Fri,  2 Sep 2016 13:25:06 +0800	[thread overview]
Message-ID: <1472793906-5699-1-git-send-email-slayercat.subscription@gmail.com> (raw)

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

             reply	other threads:[~2016-09-02  5:25 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-02  5:25 lilinzhe [this message]
2016-09-02 16:12 ` Stephen Hemminger
2016-09-02 16:31   ` Linzhe Lee
2016-09-02 16:51     ` Stephen Hemminger
2016-09-03  2:05       ` Linzhe Lee
2016-09-03 22:52         ` Ananyev, Konstantin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1472793906-5699-1-git-send-email-slayercat.subscription@gmail.com \
    --to=slayercat.subscription@gmail.com \
    --cc=dev@dpdk.org \
    --cc=lilinzhe@ijinshan.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).