Skip to contentSkip to conversation

mbuf: add comment explaining confusing code

PATCH Applied Bruce Richardson 9 participants 18 messages Mar 26, 2015 – Mar 31, 2015 8 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mbuf: add comment explaining confusing code

The logic used in the condition check before freeing an mbuf is sometimes confusing, so explain it in a proper comment.

---
 lib/librte_mbuf/rte_mbuf.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
lib/librte_mbuf/rte_mbuf.h +10 -0
index 17ba791..0265172 100644
@@ -764,6 +764,16 @@ __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
 {
 	__rte_mbuf_sanity_check(m, 0);
 
+	/*
+	 * Check to see if this is the last reference to the mbuf.
+	 * Note: the double check here is deliberate. If the ref_cnt is "atomic"
+	 * the call to "refcnt_update" is a very expensive operation, so we
+	 * don't want to call it in the case where we know we are the holder
+	 * of the last reference to this mbuf i.e. ref_cnt == 1.
+	 * If however, ref_cnt != 1, it's still possible that we may still be
+	 * the final decrementer of the count, so we need to check that
+	 * result also, to make sure the mbuf is freed properly.
+	 */
 	if (likely (rte_mbuf_refcnt_read(m) == 1) ||
 			likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
 
Signature · 1 line
2.1.0
Acked-by
#2 lore raw
In reply to a message not in the archive

Hi Bruce,

Quoted 20 lines · Bruce Richardson

The logic used in the condition check before freeing an mbuf is sometimes confusing, so explain it in a proper comment.

---
 lib/librte_mbuf/rte_mbuf.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
lib/librte_mbuf/rte_mbuf.h +3 -0
index 17ba791..0265172 100644
@@ -764,6 +764,16 @@ __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
 {
 	__rte_mbuf_sanity_check(m, 0);
 
+	/*
+	 * Check to see if this is the last reference to the mbuf.
+	 * Note: the double check here is deliberate. If the ref_cnt is "atomic"

lib/librte_mbuf/rte_mbuf.h __rte_pktmbuf_prefree_seg(struct rte_mbuf *m) · line 779

Quoted 10 lines
lib/librte_mbuf/rte_mbuf.h +7 -0
+	 * the call to "refcnt_update" is a very expensive operation, so we
+	 * don't want to call it in the case where we know we are the holder
+	 * of the last reference to this mbuf i.e. ref_cnt == 1.
+	 * If however, ref_cnt != 1, it's still possible that we may still be
+	 * the final decrementer of the count, so we need to check that
+	 * result also, to make sure the mbuf is freed properly.
+	 */
 	if (likely (rte_mbuf_refcnt_read(m) == 1) ||
 			likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
 

Thanks!

#3 lore raw
In reply to a message not in the archive
Quoted 33 lines · Bruce Richardson

The logic used in the condition check before freeing an mbuf is sometimes confusing, so explain it in a proper comment.

---
 lib/librte_mbuf/rte_mbuf.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
lib/librte_mbuf/rte_mbuf.h +10 -0
index 17ba791..0265172 100644
@@ -764,6 +764,16 @@ __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
 {
 	__rte_mbuf_sanity_check(m, 0);
 
+	/*
+	 * Check to see if this is the last reference to the mbuf.
+	 * Note: the double check here is deliberate. If the ref_cnt is "atomic"
+	 * the call to "refcnt_update" is a very expensive operation, so we
+	 * don't want to call it in the case where we know we are the holder
+	 * of the last reference to this mbuf i.e. ref_cnt == 1.
+	 * If however, ref_cnt != 1, it's still possible that we may still be
+	 * the final decrementer of the count, so we need to check that
+	 * result also, to make sure the mbuf is freed properly.
+	 */
 	if (likely (rte_mbuf_refcnt_read(m) == 1) ||
 			likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
 
Signature · 1 line
2.1.0

NAK
the comment is incorrect, a return code of 1 from rte_mbuf_refcnt_read doesn't
guarantee you are the last holder of the buffer if two contexts have a pointer to it.

Zoltans patch is the correct solution here, expensive or not. I wrote up my explination in this thread:
http://dpdk.org/ml/archives/dev/2015-March/015839.html

#5 lore raw
In reply to a message not in the archive
Quoted 39 lines

-----Original Message-----
From: dev [mailto:[email protected]] On Behalf Of Neil Horman
Sent: Friday, March 27, 2015 10:30 AM
To: Richardson, Bruce
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH] mbuf: add comment explaining confusing code

Quoted 29 lines · Bruce Richardson

The logic used in the condition check before freeing an mbuf is sometimes confusing, so explain it in a proper comment.

---
 lib/librte_mbuf/rte_mbuf.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
lib/librte_mbuf/rte_mbuf.h +10 -0
index 17ba791..0265172 100644
@@ -764,6 +764,16 @@ __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
 {
 	__rte_mbuf_sanity_check(m, 0);
 
+	/*
+	 * Check to see if this is the last reference to the mbuf.
+	 * Note: the double check here is deliberate. If the ref_cnt is "atomic"
+	 * the call to "refcnt_update" is a very expensive operation, so we
+	 * don't want to call it in the case where we know we are the holder
+	 * of the last reference to this mbuf i.e. ref_cnt == 1.
+	 * If however, ref_cnt != 1, it's still possible that we may still be
+	 * the final decrementer of the count, so we need to check that
+	 * result also, to make sure the mbuf is freed properly.
+	 */
 	if (likely (rte_mbuf_refcnt_read(m) == 1) ||
 			likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
 
Quoted 9 lines
Quoted 4 lines

--
2.1.0

NAK
the comment is incorrect, a return code of 1 from rte_mbuf_refcnt_read doesn't
guarantee you are the last holder of the buffer if two contexts have a pointer to it.

Comment is absolutely correct.
Zoltan's 'fix' will introduce unnecessary slowdown.

Quoted 5 lines

Zoltans patch is the correct solution here, expensive or not. I wrote up my explination in this thread:
http://dpdk.org/ml/archives/dev/2015-March/015839.html

#6 lore raw
In reply to a message not in the archive
Quoted 30 lines · Neil Horman
Quoted 29 lines · Bruce Richardson

The logic used in the condition check before freeing an mbuf is sometimes confusing, so explain it in a proper comment.

---
 lib/librte_mbuf/rte_mbuf.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
lib/librte_mbuf/rte_mbuf.h +10 -0
index 17ba791..0265172 100644
@@ -764,6 +764,16 @@ __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
 {
 	__rte_mbuf_sanity_check(m, 0);
 
+	/*
+	 * Check to see if this is the last reference to the mbuf.
+	 * Note: the double check here is deliberate. If the ref_cnt is "atomic"
+	 * the call to "refcnt_update" is a very expensive operation, so we
+	 * don't want to call it in the case where we know we are the holder
+	 * of the last reference to this mbuf i.e. ref_cnt == 1.
+	 * If however, ref_cnt != 1, it's still possible that we may still be
+	 * the final decrementer of the count, so we need to check that
+	 * result also, to make sure the mbuf is freed properly.
+	 */
 	if (likely (rte_mbuf_refcnt_read(m) == 1) ||
 			likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
 
Quoted 9 lines
Quoted 4 lines
Signature · 1 line
2.1.0

NAK
the comment is incorrect, a return code of 1 from rte_mbuf_refcnt_read doesn't
guarantee you are the last holder of the buffer if two contexts have a pointer to it.

If two threads have pointers to it, and are both going to free it, the refcnt must be 2 not one, otherwise the refcnt is meaningless.

/Bruce

Quoted 6 lines

Zoltans patch is the correct solution here, expensive or not. I wrote up my explination in this thread:
http://dpdk.org/ml/archives/dev/2015-March/015839.html

#7 lore raw
Quoted 40 lines · Bruce Richardson
Quoted 30 lines · Neil Horman
Quoted 29 lines · Bruce Richardson

The logic used in the condition check before freeing an mbuf is sometimes confusing, so explain it in a proper comment.

---
 lib/librte_mbuf/rte_mbuf.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
lib/librte_mbuf/rte_mbuf.h +10 -0
index 17ba791..0265172 100644
@@ -764,6 +764,16 @@ __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
 {
 	__rte_mbuf_sanity_check(m, 0);
 
+	/*
+	 * Check to see if this is the last reference to the mbuf.
+	 * Note: the double check here is deliberate. If the ref_cnt is "atomic"
+	 * the call to "refcnt_update" is a very expensive operation, so we
+	 * don't want to call it in the case where we know we are the holder
+	 * of the last reference to this mbuf i.e. ref_cnt == 1.
+	 * If however, ref_cnt != 1, it's still possible that we may still be
+	 * the final decrementer of the count, so we need to check that
+	 * result also, to make sure the mbuf is freed properly.
+	 */
 	if (likely (rte_mbuf_refcnt_read(m) == 1) ||
 			likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
 
Quoted 9 lines
Quoted 4 lines
Signature · 1 line
2.1.0

NAK
the comment is incorrect, a return code of 1 from rte_mbuf_refcnt_read doesn't
guarantee you are the last holder of the buffer if two contexts have a pointer to it.

Quoted 3 lines

If two threads have pointers to it, and are both going to free it, the refcnt must be 2 not one, otherwise the refcnt is meaningless.

Please see my note in zoltans thread. I illustrate why its important to fix this in several ways. Relying of every context to have a refcnt on an object before having pointer reference to it is impossible to do without additional locking.

Quoted 9 lines

/Bruce

Quoted 6 lines

Zoltans patch is the correct solution here, expensive or not. I wrote up my explination in this thread:
http://dpdk.org/ml/archives/dev/2015-March/015839.html

#8 lore raw
In reply to Bruce Richardson
Quoted 40 lines · Bruce Richardson
Quoted 30 lines · Neil Horman
Quoted 29 lines · Bruce Richardson

The logic used in the condition check before freeing an mbuf is sometimes confusing, so explain it in a proper comment.

---
 lib/librte_mbuf/rte_mbuf.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
lib/librte_mbuf/rte_mbuf.h +10 -0
index 17ba791..0265172 100644
@@ -764,6 +764,16 @@ __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
 {
 	__rte_mbuf_sanity_check(m, 0);
 
+	/*
+	 * Check to see if this is the last reference to the mbuf.
+	 * Note: the double check here is deliberate. If the ref_cnt is "atomic"
+	 * the call to "refcnt_update" is a very expensive operation, so we
+	 * don't want to call it in the case where we know we are the holder
+	 * of the last reference to this mbuf i.e. ref_cnt == 1.
+	 * If however, ref_cnt != 1, it's still possible that we may still be
+	 * the final decrementer of the count, so we need to check that
+	 * result also, to make sure the mbuf is freed properly.
+	 */
 	if (likely (rte_mbuf_refcnt_read(m) == 1) ||
 			likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
 
Quoted 9 lines
Quoted 4 lines
Signature · 1 line
2.1.0

NAK
the comment is incorrect, a return code of 1 from rte_mbuf_refcnt_read doesn't
guarantee you are the last holder of the buffer if two contexts have a pointer to it.

Quoted 3 lines

If two threads have pointers to it, and are both going to free it, the refcnt must be 2 not one, otherwise the refcnt is meaningless.

What about the other concrete case that I illustrated, where one context is attempting to increment the refcount, while the other is decrementing it with the intention to free? By making the read and set operation disctinct here you've broken the atomicity of the read and update logic that atomics are there for and created a race condition. I don't know how else to explain this to you. if(atomic_read == 1) then atomic_set(0), breaks the entire notion of what atomics are meant to do (namely update and read state as an atomic unit), you just can't get away with not having that atomicity here. If you could, you might as well be using plain integers for the reference count, as you're not using the atomic properties of the type.

Neil

Quoted 9 lines

/Bruce

Quoted 6 lines

Zoltans patch is the correct solution here, expensive or not. I wrote up my explination in this thread:
http://dpdk.org/ml/archives/dev/2015-March/015839.html

#10 lore raw
In reply to a message not in the archive
Quoted 56 lines · Neil Horman
Quoted 40 lines · Bruce Richardson
Quoted 30 lines · Neil Horman
Quoted 29 lines · Bruce Richardson

The logic used in the condition check before freeing an mbuf is sometimes confusing, so explain it in a proper comment.

---
 lib/librte_mbuf/rte_mbuf.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
lib/librte_mbuf/rte_mbuf.h +10 -0
index 17ba791..0265172 100644
@@ -764,6 +764,16 @@ __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
 {
 	__rte_mbuf_sanity_check(m, 0);
 
+	/*
+	 * Check to see if this is the last reference to the mbuf.
+	 * Note: the double check here is deliberate. If the ref_cnt is "atomic"
+	 * the call to "refcnt_update" is a very expensive operation, so we
+	 * don't want to call it in the case where we know we are the holder
+	 * of the last reference to this mbuf i.e. ref_cnt == 1.
+	 * If however, ref_cnt != 1, it's still possible that we may still be
+	 * the final decrementer of the count, so we need to check that
+	 * result also, to make sure the mbuf is freed properly.
+	 */
 	if (likely (rte_mbuf_refcnt_read(m) == 1) ||
 			likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
 
Quoted 9 lines
Quoted 4 lines
Signature · 1 line
2.1.0

NAK
the comment is incorrect, a return code of 1 from rte_mbuf_refcnt_read doesn't
guarantee you are the last holder of the buffer if two contexts have a pointer to it.

Quoted 3 lines

If two threads have pointers to it, and are both going to free it, the refcnt must be 2 not one, otherwise the refcnt is meaningless.

What about the other concrete case that I illustrated, where one context is attempting to increment the refcount, while the other is decrementing it with the intention to free? By making the read and set operation disctinct here you've broken the atomicity of the read and update logic that atomics are there for and created a race condition. I don't know how else to explain this to you. if(atomic_read == 1) then atomic_set(0), breaks the entire notion of what atomics are meant to do (namely update and read state as an atomic unit), you just can't get away with not having that atomicity here. If you could, you might as well be using plain integers for the reference count, as you're not using the atomic properties of the type.

Quoted 1 line

Neil

I disagree.

A value of one, indicates that there is only one owner of the mbuf, and therefore since we are in the free routine, we are that owner. If there are to be two owners, the refcnt must be incremented before handing over the pointer to the other thread - to get to the example you make. If that does not occur, we can also have the situation where the "sending" thread calls free - and therefore this function - before the other thread receives the pointer. In that case, we will have the receiving thread getting a pointer to an mbuf which is now invalid as it has been put back into the mempool

Again, in short, if refcnt == 1, there is only one mbuf owner. If refcnt == 1 and we are currently executing in prefree_seg, we are the owner and no other thread is allow to muck about with the mbuf.

/Bruce

#11 lore raw
Quoted 70 lines · Bruce Richardson
Quoted 56 lines · Neil Horman
Quoted 40 lines · Bruce Richardson
Quoted 30 lines · Neil Horman
Quoted 29 lines · Bruce Richardson

The logic used in the condition check before freeing an mbuf is sometimes confusing, so explain it in a proper comment.

---
 lib/librte_mbuf/rte_mbuf.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
lib/librte_mbuf/rte_mbuf.h +10 -0
index 17ba791..0265172 100644
@@ -764,6 +764,16 @@ __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
 {
 	__rte_mbuf_sanity_check(m, 0);
 
+	/*
+	 * Check to see if this is the last reference to the mbuf.
+	 * Note: the double check here is deliberate. If the ref_cnt is "atomic"
+	 * the call to "refcnt_update" is a very expensive operation, so we
+	 * don't want to call it in the case where we know we are the holder
+	 * of the last reference to this mbuf i.e. ref_cnt == 1.
+	 * If however, ref_cnt != 1, it's still possible that we may still be
+	 * the final decrementer of the count, so we need to check that
+	 * result also, to make sure the mbuf is freed properly.
+	 */
 	if (likely (rte_mbuf_refcnt_read(m) == 1) ||
 			likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
 
Quoted 9 lines
Quoted 4 lines
Signature · 1 line
2.1.0

NAK
the comment is incorrect, a return code of 1 from rte_mbuf_refcnt_read doesn't
guarantee you are the last holder of the buffer if two contexts have a pointer to it.

Quoted 3 lines

If two threads have pointers to it, and are both going to free it, the refcnt must be 2 not one, otherwise the refcnt is meaningless.

What about the other concrete case that I illustrated, where one context is attempting to increment the refcount, while the other is decrementing it with the intention to free? By making the read and set operation disctinct here you've broken the atomicity of the read and update logic that atomics are there for and created a race condition. I don't know how else to explain this to you. if(atomic_read == 1) then atomic_set(0), breaks the entire notion of what atomics are meant to do (namely update and read state as an atomic unit), you just can't get away with not having that atomicity here. If you could, you might as well be using plain integers for the reference count, as you're not using the atomic properties of the type.

Quoted 1 line

Neil

I disagree.

A value of one, indicates that there is only one owner of the mbuf, and therefore since we are in the free routine, we are that owner. If there are to be two owners, the refcnt must be incremented before handing over the pointer to the other thread - to get to the example you make. If that does not occur, we can also have the situation where the "sending" thread calls free - and therefore this function - before the other thread receives the pointer. In that case, we will have the receiving thread getting a pointer to an mbuf which is now invalid as it has been put back into the mempool

Quoted 4 lines

Again, in short, if refcnt == 1, there is only one mbuf owner. If refcnt == 1 and we are currently executing in prefree_seg, we are the owner and no other thread is allow to muck about with the mbuf.

Then the question remains, why aren't you just using ints here? What is the purpose of even bothering with atomics, if you don't feel like you need any reliance on the atomic set and read state, which it was created for??

Neil

Quoted 3 lines

/Bruce

#13 lore raw
In reply to a message not in the archive
Quoted 75 lines · Neil Horman
Quoted 70 lines · Bruce Richardson
Quoted 56 lines · Neil Horman
Quoted 40 lines · Bruce Richardson
Quoted 30 lines · Neil Horman
Quoted 29 lines · Bruce Richardson

The logic used in the condition check before freeing an mbuf is sometimes confusing, so explain it in a proper comment.

---
 lib/librte_mbuf/rte_mbuf.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
lib/librte_mbuf/rte_mbuf.h +10 -0
index 17ba791..0265172 100644
@@ -764,6 +764,16 @@ __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
 {
 	__rte_mbuf_sanity_check(m, 0);
 
+	/*
+	 * Check to see if this is the last reference to the mbuf.
+	 * Note: the double check here is deliberate. If the ref_cnt is "atomic"
+	 * the call to "refcnt_update" is a very expensive operation, so we
+	 * don't want to call it in the case where we know we are the holder
+	 * of the last reference to this mbuf i.e. ref_cnt == 1.
+	 * If however, ref_cnt != 1, it's still possible that we may still be
+	 * the final decrementer of the count, so we need to check that
+	 * result also, to make sure the mbuf is freed properly.
+	 */
 	if (likely (rte_mbuf_refcnt_read(m) == 1) ||
 			likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
 
Quoted 9 lines
Quoted 4 lines
Signature · 1 line
2.1.0

NAK
the comment is incorrect, a return code of 1 from rte_mbuf_refcnt_read doesn't
guarantee you are the last holder of the buffer if two contexts have a pointer to it.

Quoted 3 lines

If two threads have pointers to it, and are both going to free it, the refcnt must be 2 not one, otherwise the refcnt is meaningless.

What about the other concrete case that I illustrated, where one context is attempting to increment the refcount, while the other is decrementing it with the intention to free? By making the read and set operation disctinct here you've broken the atomicity of the read and update logic that atomics are there for and created a race condition. I don't know how else to explain this to you. if(atomic_read == 1) then atomic_set(0), breaks the entire notion of what atomics are meant to do (namely update and read state as an atomic unit), you just can't get away with not having that atomicity here. If you could, you might as well be using plain integers for the reference count, as you're not using the atomic properties of the type.

Quoted 1 line

Neil

I disagree.

A value of one, indicates that there is only one owner of the mbuf, and therefore since we are in the free routine, we are that owner. If there are to be two owners, the refcnt must be incremented before handing over the pointer to the other thread - to get to the example you make. If that does not occur, we can also have the situation where the "sending" thread calls free - and therefore this function - before the other thread receives the pointer. In that case, we will have the receiving thread getting a pointer to an mbuf which is now invalid as it has been put back into the mempool

Quoted 4 lines

Again, in short, if refcnt == 1, there is only one mbuf owner. If refcnt == 1 and we are currently executing in prefree_seg, we are the owner and no other thread is allow to muck about with the mbuf.

Quoted 5 lines

Then the question remains, why aren't you just using ints here? What is the purpose of even bothering with atomics, if you don't feel like you need any reliance on the atomic set and read state, which it was created for??

Neil

Because for the case where refcnt != 1, you need the atomics. If you have two threads using the mbuf and refcnt is 2, both of them simultaneously can hand over their copies to two more threads. In that case, we need to guarantee refcnt to be 4, so we need to use atomics. Similarly, if both threads attempt to free at the same time, we need to ensure that only one of them actually returns the buf to the mempool - hence the atomic decrement and return value check.

/Bruce

Quoted 4 lines
Quoted 4 lines

/Bruce

Acked-by
#14 lore raw
Quoted 82 lines · Bruce Richardson
Quoted 75 lines · Neil Horman
Quoted 70 lines · Bruce Richardson
Quoted 56 lines · Neil Horman
Quoted 40 lines · Bruce Richardson
Quoted 30 lines · Neil Horman
Quoted 29 lines · Bruce Richardson

The logic used in the condition check before freeing an mbuf is sometimes confusing, so explain it in a proper comment.

---
 lib/librte_mbuf/rte_mbuf.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
lib/librte_mbuf/rte_mbuf.h +10 -0
index 17ba791..0265172 100644
@@ -764,6 +764,16 @@ __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
 {
 	__rte_mbuf_sanity_check(m, 0);
 
+	/*
+	 * Check to see if this is the last reference to the mbuf.
+	 * Note: the double check here is deliberate. If the ref_cnt is "atomic"
+	 * the call to "refcnt_update" is a very expensive operation, so we
+	 * don't want to call it in the case where we know we are the holder
+	 * of the last reference to this mbuf i.e. ref_cnt == 1.
+	 * If however, ref_cnt != 1, it's still possible that we may still be
+	 * the final decrementer of the count, so we need to check that
+	 * result also, to make sure the mbuf is freed properly.
+	 */
 	if (likely (rte_mbuf_refcnt_read(m) == 1) ||
 			likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
 
Quoted 9 lines
Quoted 4 lines
Signature · 1 line
2.1.0

NAK
the comment is incorrect, a return code of 1 from rte_mbuf_refcnt_read doesn't
guarantee you are the last holder of the buffer if two contexts have a pointer to it.

Quoted 3 lines

If two threads have pointers to it, and are both going to free it, the refcnt must be 2 not one, otherwise the refcnt is meaningless.

What about the other concrete case that I illustrated, where one context is attempting to increment the refcount, while the other is decrementing it with the intention to free? By making the read and set operation disctinct here you've broken the atomicity of the read and update logic that atomics are there for and created a race condition. I don't know how else to explain this to you. if(atomic_read == 1) then atomic_set(0), breaks the entire notion of what atomics are meant to do (namely update and read state as an atomic unit), you just can't get away with not having that atomicity here. If you could, you might as well be using plain integers for the reference count, as you're not using the atomic properties of the type.

Quoted 1 line

Neil

I disagree.

A value of one, indicates that there is only one owner of the mbuf, and therefore since we are in the free routine, we are that owner. If there are to be two owners, the refcnt must be incremented before handing over the pointer to the other thread - to get to the example you make. If that does not occur, we can also have the situation where the "sending" thread calls free - and therefore this function - before the other thread receives the pointer. In that case, we will have the receiving thread getting a pointer to an mbuf which is now invalid as it has been put back into the mempool

Quoted 4 lines

Again, in short, if refcnt == 1, there is only one mbuf owner. If refcnt == 1 and we are currently executing in prefree_seg, we are the owner and no other thread is allow to muck about with the mbuf.

Quoted 5 lines

Then the question remains, why aren't you just using ints here? What is the purpose of even bothering with atomics, if you don't feel like you need any reliance on the atomic set and read state, which it was created for??

Neil

Quoted 8 lines

Because for the case where refcnt != 1, you need the atomics. If you have two threads using the mbuf and refcnt is 2, both of them simultaneously can hand over their copies to two more threads. In that case, we need to guarantee refcnt to be 4, so we need to use atomics. Similarly, if both threads attempt to free at the same time, we need to ensure that only one of them actually returns the buf to the mempool - hence the atomic decrement and return value check.

/Bruce

Sigh, ok, so that makes some sense. This thing is entirely for the purposes of special casing the single use case? That seems like alot of effort and confusion to go through for this. Perhaps macrotizing it for multiple use cases would clarify it:
#define mbuf_orphaned(mbuf) atomic_ref_read(mbuf)==1 || atomic_ref_dec(mbuf)==0

regardless, you've convinced me that its not broken.

Quoted 5 lines
Quoted 4 lines
Quoted 4 lines

/Bruce

#16 lore raw
In reply to a message not in the archive
Quoted 118 lines

-----Original Message-----
From: Neil Horman [mailto:[email protected]]
Sent: Friday, March 27, 2015 4:44 PM
To: Richardson, Bruce
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH] mbuf: add comment explaining confusing code

Quoted 6 lines · Bruce Richardson
Quoted 5 lines · Neil Horman
Quoted 4 lines · Bruce Richardson
Quoted 3 lines · Neil Horman
Quoted 2 lines · Bruce Richardson
Quoted 1 line · Neil Horman

On Thu, Mar 26, 2015 at 09:14:54PM +0000, Bruce Richardson

Quoted 2 lines · wrote:
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

The logic used in the condition check before freeing an mbuf is sometimes confusing, so explain it in a proper

comment.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

Signed-off-by: Bruce Richardson <[email protected]>

---
 lib/librte_mbuf/rte_mbuf.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
(quoted diff) +0 -0

b/lib/librte_mbuf/rte_mbuf.h index 17ba791..0265172 100644

lib/librte_mbuf/rte_mbuf.h +0 -0
@@ -764,6 +764,16 @@ __rte_pktmbuf_prefree_seg(struct
 rte_mbuf *m)  {
 	__rte_mbuf_sanity_check(m, 0);
 
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

lib/librte_mbuf/rte_mbuf.h __rte_pktmbuf_prefree_seg(struct · line 768

Quoted 2 lines
lib/librte_mbuf/rte_mbuf.h +2 -0
+	/*
+	 * Check to see if this is the last reference to the

mbuf.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

lib/librte_mbuf/rte_mbuf.h __rte_pktmbuf_prefree_seg(struct · line 769

Quoted 1 line
lib/librte_mbuf/rte_mbuf.h +1 -0
+	 * Note: the double check here is deliberate. If the

ref_cnt is "atomic"

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

lib/librte_mbuf/rte_mbuf.h __rte_pktmbuf_prefree_seg(struct · line 770

Quoted 1 line
lib/librte_mbuf/rte_mbuf.h +1 -0
+	 * the call to "refcnt_update" is a very expensive

operation, so we

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

lib/librte_mbuf/rte_mbuf.h __rte_pktmbuf_prefree_seg(struct · line 771

Quoted 1 line
lib/librte_mbuf/rte_mbuf.h +1 -0
+	 * don't want to call it in the case where we know we

are the holder

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

lib/librte_mbuf/rte_mbuf.h __rte_pktmbuf_prefree_seg(struct · line 773

Quoted 2 lines
lib/librte_mbuf/rte_mbuf.h +2 -0
+	 * of the last reference to this mbuf i.e. ref_cnt == 1.
+	 * If however, ref_cnt != 1, it's still possible that we

may still be

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

lib/librte_mbuf/rte_mbuf.h __rte_pktmbuf_prefree_seg(struct · line 774

Quoted 1 line
lib/librte_mbuf/rte_mbuf.h +1 -0
+	 * the final decrementer of the count, so we need to

check that

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

lib/librte_mbuf/rte_mbuf.h __rte_pktmbuf_prefree_seg(struct · line 778

Quoted 4 lines
lib/librte_mbuf/rte_mbuf.h +2 -0
+	 * result also, to make sure the mbuf is freed properly.
+	 */
 	if (likely (rte_mbuf_refcnt_read(m) == 1) ||
 			likely (rte_mbuf_refcnt_update(m, -1) == 0))

{

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
lib/librte_mbuf/rte_mbuf.h +0 -1
--

2.1.0

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

NAK
the comment is incorrect, a return code of 1 from
rte_mbuf_refcnt_read doesn't guarantee you are the last holder of the buffer if two contexts have a pointer to it.

If two threads have pointers to it, and are both going to free it, the refcnt must be 2 not one, otherwise the refcnt is

meaningless.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

What about the other concrete case that I illustrated, where one context is attempting to increment the refcount, while the other is decrementing it with the intention to free? By making the read and set operation disctinct here you've broken the atomicity of the read and update logic that atomics are there for

and created a race condition. I don't know how else to explain this to you.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

if(atomic_read == 1) then atomic_set(0), breaks the entire notion of what atomics are meant to do (namely update and read state as an atomic unit), you just can't get away with not having that atomicity here. If you could, you might as well be using plain integers for the reference count, as you're not using

the atomic properties of the type.

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 2 lines

Neil

I disagree.

A value of one, indicates that there is only one owner of the mbuf, and therefore since we are in the free routine, we are that owner. If there are to be two owners, the refcnt must be incremented before handing over the pointer to the other thread - to get to the example you make. If that does not occur, we can also have the situation where the "sending" thread calls free - and therefore this function - before the other thread receives the pointer. In that case, we will have the receiving thread getting a pointer to an mbuf which is now invalid as it has been put back into the mempool

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

Again, in short, if refcnt == 1, there is only one mbuf owner. If refcnt == 1 and we are currently executing in prefree_seg, we are the owner and no other thread is allow to muck about with the mbuf.

Then the question remains, why aren't you just using ints here? What is the purpose of even bothering with atomics, if you don't feel like you need any reliance on the atomic set and read state,

which it was created for??

Quoted 9 lines
Quoted 2 lines

Neil

Because for the case where refcnt != 1, you need the atomics. If you have two threads using the mbuf and refcnt is 2, both of them simultaneously can hand over their copies to two more threads. In that case, we need to guarantee refcnt to be 4, so we need to use atomics. Similarly, if both threads attempt to free at the same time, we need to ensure that only one of them actually returns the buf to the mempool

- hence the atomic decrement and return value check.

Quoted 1 line
Quoted 8 lines
Quoted 1 line

/Bruce

Sigh, ok, so that makes some sense. This thing is entirely for the purposes of special casing the single use case? That seems like alot of effort and confusion to go through for this. Perhaps macrotizing it for multiple use cases would clarify it:
#define mbuf_orphaned(mbuf) atomic_ref_read(mbuf)==1 || atomic_ref_dec(mbuf)==0

Yes, we could, except it's not "orphaned" since it has got a single thread owner, and this is the normal use-case we are special-casing. The comment should adequately cover things, I think, and for cases where it doesn't we now have this thread to refer to also. :-)

Quoted 3 lines

regardless, you've convinced me that its not broken.

Thanks,
/Bruce

#18 lore raw
In reply to a message not in the archive

2015-03-27 16:56, Richardson, Bruce:

Quoted 130 lines
Quoted 117 lines

-----Original Message-----
From: Neil Horman [mailto:[email protected]]
Sent: Friday, March 27, 2015 4:44 PM
To: Richardson, Bruce
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH] mbuf: add comment explaining confusing code

Quoted 6 lines · Bruce Richardson
Quoted 5 lines · Neil Horman
Quoted 4 lines · Bruce Richardson
Quoted 3 lines · Neil Horman
Quoted 2 lines · Bruce Richardson
Quoted 1 line · Neil Horman

On Thu, Mar 26, 2015 at 09:14:54PM +0000, Bruce Richardson

Quoted 2 lines · wrote:
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

The logic used in the condition check before freeing an mbuf is sometimes confusing, so explain it in a proper

comment.

Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines
Quoted 15 lines

Signed-off-by: Bruce Richardson <[email protected]>

---
 lib/librte_mbuf/rte_mbuf.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
(quoted diff) +0 -0

b/lib/librte_mbuf/rte_mbuf.h index 17ba791..0265172 100644

lib/librte_mbuf/rte_mbuf.h +0 -0
@@ -764,6 +764,16 @@ __rte_pktmbuf_prefree_seg(struct
 rte_mbuf *m)  {
 	__rte_mbuf_sanity_check(m, 0);
 
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

lib/librte_mbuf/rte_mbuf.h __rte_pktmbuf_prefree_seg(struct · line 768

Quoted 2 lines
lib/librte_mbuf/rte_mbuf.h +2 -0
+	/*
+	 * Check to see if this is the last reference to the

mbuf.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

lib/librte_mbuf/rte_mbuf.h __rte_pktmbuf_prefree_seg(struct · line 769

Quoted 1 line
lib/librte_mbuf/rte_mbuf.h +1 -0
+	 * Note: the double check here is deliberate. If the

ref_cnt is "atomic"

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

lib/librte_mbuf/rte_mbuf.h __rte_pktmbuf_prefree_seg(struct · line 770

Quoted 1 line
lib/librte_mbuf/rte_mbuf.h +1 -0
+	 * the call to "refcnt_update" is a very expensive

operation, so we

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

lib/librte_mbuf/rte_mbuf.h __rte_pktmbuf_prefree_seg(struct · line 771

Quoted 1 line
lib/librte_mbuf/rte_mbuf.h +1 -0
+	 * don't want to call it in the case where we know we

are the holder

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

lib/librte_mbuf/rte_mbuf.h __rte_pktmbuf_prefree_seg(struct · line 773

Quoted 2 lines
lib/librte_mbuf/rte_mbuf.h +2 -0
+	 * of the last reference to this mbuf i.e. ref_cnt == 1.
+	 * If however, ref_cnt != 1, it's still possible that we

may still be

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

lib/librte_mbuf/rte_mbuf.h __rte_pktmbuf_prefree_seg(struct · line 774

Quoted 1 line
lib/librte_mbuf/rte_mbuf.h +1 -0
+	 * the final decrementer of the count, so we need to

check that

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

lib/librte_mbuf/rte_mbuf.h __rte_pktmbuf_prefree_seg(struct · line 778

Quoted 4 lines
lib/librte_mbuf/rte_mbuf.h +2 -0
+	 * result also, to make sure the mbuf is freed properly.
+	 */
 	if (likely (rte_mbuf_refcnt_read(m) == 1) ||
 			likely (rte_mbuf_refcnt_update(m, -1) == 0))

{

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
lib/librte_mbuf/rte_mbuf.h +0 -1
--

2.1.0

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

NAK
the comment is incorrect, a return code of 1 from
rte_mbuf_refcnt_read doesn't guarantee you are the last holder of the buffer if two contexts have a pointer to it.

If two threads have pointers to it, and are both going to free it, the refcnt must be 2 not one, otherwise the refcnt is

meaningless.

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 1 line

What about the other concrete case that I illustrated, where one context is attempting to increment the refcount, while the other is decrementing it with the intention to free? By making the read and set operation disctinct here you've broken the atomicity of the read and update logic that atomics are there for

and created a race condition. I don't know how else to explain this to you.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

if(atomic_read == 1) then atomic_set(0), breaks the entire notion of what atomics are meant to do (namely update and read state as an atomic unit), you just can't get away with not having that atomicity here. If you could, you might as well be using plain integers for the reference count, as you're not using

the atomic properties of the type.

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 2 lines

Neil

I disagree.

A value of one, indicates that there is only one owner of the mbuf, and therefore since we are in the free routine, we are that owner. If there are to be two owners, the refcnt must be incremented before handing over the pointer to the other thread - to get to the example you make. If that does not occur, we can also have the situation where the "sending" thread calls free - and therefore this function - before the other thread receives the pointer. In that case, we will have the receiving thread getting a pointer to an mbuf which is now invalid as it has been put back into the mempool

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

Again, in short, if refcnt == 1, there is only one mbuf owner. If refcnt == 1 and we are currently executing in prefree_seg, we are the owner and no other thread is allow to muck about with the mbuf.

Then the question remains, why aren't you just using ints here? What is the purpose of even bothering with atomics, if you don't feel like you need any reliance on the atomic set and read state,

which it was created for??

Quoted 9 lines
Quoted 2 lines

Neil

Because for the case where refcnt != 1, you need the atomics. If you have two threads using the mbuf and refcnt is 2, both of them simultaneously can hand over their copies to two more threads. In that case, we need to guarantee refcnt to be 4, so we need to use atomics. Similarly, if both threads attempt to free at the same time, we need to ensure that only one of them actually returns the buf to the mempool

- hence the atomic decrement and return value check.

Quoted 1 line
Quoted 8 lines
Quoted 1 line

/Bruce

Sigh, ok, so that makes some sense. This thing is entirely for the purposes of special casing the single use case? That seems like alot of effort and confusion to go through for this. Perhaps macrotizing it for multiple use cases would clarify it:
#define mbuf_orphaned(mbuf) atomic_ref_read(mbuf)==1 || atomic_ref_dec(mbuf)==0

Yes, we could, except it's not "orphaned" since it has got a single thread owner, and this is the normal use-case we are special-casing. The comment should adequately cover things, I think, and for cases where it doesn't we now have this thread to refer to also. :-)

Quoted 1 line
Quoted 5 lines
Quoted 2 lines

regardless, you've convinced me that its not broken.

Thanks,
/Bruce

Applied, thanks

#19 lore raw
In reply to a message not in the archive
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Review

Quoted 2 lines
(quoted diff) +0 -0
 	if (likely (rte_mbuf_refcnt_read(m) == 1) ||
 			likely (rte_mbuf_refcnt_update(m, -1) == 0))

In all the debate about atomics, I don't think anyone got around to pointing out that in the unlikely case that the refcnt is not 1, then it's equally unlikely that decrementing it will result in 0 despite the code's claim to the contrary. That's the part that confused me. Would it make sense to fix this while adding the comment? -don
[email protected]

#21 lore raw
In reply to a message not in the archive
Quoted 1 line · On Mon, 30 Mar 2015 17:39:06 +0000 Don Provan…

In all the debate about atomics, I don't think anyone got around to pointing out that in the unlikely case that the refcnt is not 1, then it's equally unlikely that decrementing it will result in 0 despite the code's claim to the contrary. That's the part that confused me. Would it make sense to fix this while adding the comment?

Really doubt the second likely() makes any difference in code speed. Should probably just be removed.

#23 lore raw
In reply to Bruce Richardson
Quoted 55 lines · Bruce Richardson
Quoted 54 lines · Neil Horman
Quoted 39 lines · Bruce Richardson
Quoted 30 lines · Neil Horman
Quoted 29 lines · Bruce Richardson

The logic used in the condition check before freeing an mbuf is sometimes confusing, so explain it in a proper comment.

---
  lib/librte_mbuf/rte_mbuf.h | 10 ++++++++++
  1 file changed, 10 insertions(+)
lib/librte_mbuf/rte_mbuf.h +10 -0
index 17ba791..0265172 100644
@@ -764,6 +764,16 @@ __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
  {
  	__rte_mbuf_sanity_check(m, 0);
 
+	/*
+	 * Check to see if this is the last reference to the mbuf.
+	 * Note: the double check here is deliberate. If the ref_cnt is "atomic"
+	 * the call to "refcnt_update" is a very expensive operation, so we
+	 * don't want to call it in the case where we know we are the holder
+	 * of the last reference to this mbuf i.e. ref_cnt == 1.
+	 * If however, ref_cnt != 1, it's still possible that we may still be
+	 * the final decrementer of the count, so we need to check that
+	 * result also, to make sure the mbuf is freed properly.
+	 */
  	if (likely (rte_mbuf_refcnt_read(m) == 1) ||
  			likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
 
Quoted 8 lines
Quoted 4 lines
Signature · 1 line
2.1.0

NAK
the comment is incorrect, a return code of 1 from rte_mbuf_refcnt_read doesn't
guarantee you are the last holder of the buffer if two contexts have a pointer to it.

Quoted 3 lines

If two threads have pointers to it, and are both going to free it, the refcnt must be 2 not one, otherwise the refcnt is meaningless.

What about the other concrete case that I illustrated, where one context is attempting to increment the refcount, while the other is decrementing it with the intention to free? By making the read and set operation disctinct here you've broken the atomicity of the read and update logic that atomics are there for and created a race condition. I don't know how else to explain this to you. if(atomic_read == 1) then atomic_set(0), breaks the entire notion of what atomics are meant to do (namely update and read state as an atomic unit), you just can't get away with not having that atomicity here. If you could, you might as well be using plain integers for the reference count, as you're not using the atomic properties of the type.

Quoted 5 lines
Quoted 1 line

Neil

I disagree.

A value of one, indicates that there is only one owner of the mbuf, and therefore since we are in the free routine, we are that owner.

This is true if you assume the application is bug-free and two threads will never hold mistakenly the same mbuf pointer with ref_cnt=1. That's ok in general, but if it doesn't, debugging it can be pretty painful.

Marc

Quoted 14 lines

If there
are to be two owners, the refcnt must be incremented before handing over the pointer to the other thread - to get to the example you make. If that does not occur, we can also have the situation where the "sending" thread calls free - and therefore this function - before the other thread receives the pointer. In that case, we will have the receiving thread getting a pointer to an mbuf which is now invalid as it has been put back into the mempool

Again, in short, if refcnt == 1, there is only one mbuf owner. If refcnt == 1 and we are currently executing in prefree_seg, we are the owner and no other thread is allow to muck about with the mbuf.

/Bruce

#25 lore raw
In reply to a message not in the archive
Quoted 59 lines · Marc Sune
Quoted 56 lines · Bruce Richardson
Quoted 52 lines · Neil Horman
Quoted 37 lines · Bruce Richardson
Quoted 28 lines · Neil Horman
Quoted 27 lines · Bruce Richardson

The logic used in the condition check before freeing an mbuf is sometimes confusing, so explain it in a proper comment.

---
 lib/librte_mbuf/rte_mbuf.h | 10 ++++++++++
 1 file changed, 10 insertions(+)
lib/librte_mbuf/rte_mbuf.h +10 -0
index 17ba791..0265172 100644
@@ -764,6 +764,16 @@ __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
 {
 	__rte_mbuf_sanity_check(m, 0);
+	/*
+	 * Check to see if this is the last reference to the mbuf.
+	 * Note: the double check here is deliberate. If the ref_cnt is "atomic"
+	 * the call to "refcnt_update" is a very expensive operation, so we
+	 * don't want to call it in the case where we know we are the holder
+	 * of the last reference to this mbuf i.e. ref_cnt == 1.
+	 * If however, ref_cnt != 1, it's still possible that we may still be
+	 * the final decrementer of the count, so we need to check that
+	 * result also, to make sure the mbuf is freed properly.
+	 */
 	if (likely (rte_mbuf_refcnt_read(m) == 1) ||
 			likely (rte_mbuf_refcnt_update(m, -1) == 0)) {
Quoted 8 lines
Quoted 4 lines
Signature · 1 line
2.1.0

NAK
the comment is incorrect, a return code of 1 from rte_mbuf_refcnt_read doesn't
guarantee you are the last holder of the buffer if two contexts have a pointer to it.

Quoted 3 lines

If two threads have pointers to it, and are both going to free it, the refcnt must be 2 not one, otherwise the refcnt is meaningless.

What about the other concrete case that I illustrated, where one context is attempting to increment the refcount, while the other is decrementing it with the intention to free? By making the read and set operation disctinct here you've broken the atomicity of the read and update logic that atomics are there for and created a race condition. I don't know how else to explain this to you. if(atomic_read == 1) then atomic_set(0), breaks the entire notion of what atomics are meant to do (namely update and read state as an atomic unit), you just can't get away with not having that atomicity here. If you could, you might as well be using plain integers for the reference count, as you're not using the atomic properties of the type.

Quoted 1 line

Neil

I disagree.

Quoted 9 lines
Quoted 2 lines

A value of one, indicates that there is only one owner of the mbuf, and therefore since we are in the free routine, we are that owner.

This is true if you assume the application is bug-free and two threads will never hold mistakenly the same mbuf pointer with ref_cnt=1. That's ok in general, but if it doesn't, debugging it can be pretty painful.

Marc

The use case for the above is situations in which mbufs are stored/queued and must be looked up prior to taking a hold on the refcnt (i.e. you have to get the pointer so that you can pass it in to rte_pktmbuf_refcnt_inc or whtever increases the hold count. I don't think that use case is considered here though, in that it presumes that in a provider/receiver context relationship, the provider always increases the refcount before giving it to the receiver. I don't care for it either, but it is what it is.
Neil

Quoted 16 lines
Quoted 14 lines

If there
are to be two owners, the refcnt must be incremented before handing over the pointer to the other thread - to get to the example you make. If that does not occur, we can also have the situation where the "sending" thread calls free - and therefore this function - before the other thread receives the pointer. In that case, we will have the receiving thread getting a pointer to an mbuf which is now invalid as it has been put back into the mempool

Again, in short, if refcnt == 1, there is only one mbuf owner. If refcnt == 1 and we are currently executing in prefree_seg, we are the owner and no other thread is allow to muck about with the mbuf.

/Bruce

#22 lore raw
In reply to a message not in the archive

Hi,

Quoted 7 lines · Don Provan
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

if (likely (rte_mbuf_refcnt_read(m) == 1) ||
likely (rte_mbuf_refcnt_update(m, -1) == 0))

In all the debate about atomics, I don't think anyone got around to pointing out that in the unlikely case that the refcnt is not 1, then it's equally unlikely that decrementing it will result in 0 despite the code's claim to the contrary. That's the part that confused me. Would it make sense to fix this while adding the comment? -don
[email protected]

I was thinking about that too, either remove it or turn it into "unlikely". Currently it suggest that "if there are more than one users, they are likely to release at the same time". If that's not true, we should remove it, but as Don said, it would hardly make a difference in real world cases as more than one users is not really a hot usecase, AFAIK.

Regards,

Zoltan