mbuf: remove the redundant code for mbuf prefree
PATCH 1/1 mbuf: remove the redundant code for mbuf prefree
For 'rte_pktmbuf_prefree_seg' function, 'rte_mbuf_refcnt_read(m) == 1' and '__rte_mbuf_refcnt_update(m, -1) == 0' are the same cases where mbuf's refcnt value should be 1. Thus we can simplify the code and remove the redundant part.
Furthermore, according to [1], when the mbuf is stored inside the mempool, the m->refcnt value should be 1. And then it is detached from its parent for an indirect mbuf. Thus change the description of 'rte_pktmbuf_prefree_seg' function.
[1] https://patches.dpdk.org/project/dpdk/patch/[email protected]/
--- lib/mbuf/rte_mbuf.h | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-)
lib/mbuf/rte_mbuf.h +3 -19
@@ -1328,7 +1328,7 @@ static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m)* * This function does the same than a free, except that it does not * return the segment to its pool. -* It decreases the reference counter, and if it reaches 0, it is +* It decreases the reference counter, and if it reaches 1, it is * detached from its parent for an indirect mbuf. * * @param m
@@ -1358,25 +1358,9 @@ rte_pktmbuf_prefree_seg(struct rte_mbuf *m) m->nb_segs =1 ;return m; - - }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; -rte_mbuf_refcnt_set (m,1 ); - -return m; } + +__rte_mbuf_refcnt_update (m,-1 );return NULL ; }
Signature · 1 line
2.25.1
Morten Brørup
NAKQuoted 27 lines
From: Feifei Wang [mailto:[email protected]]
Sent: Monday, 4 December 2023 03.39For '
rte_pktmbuf_prefree_seg' function, 'rte_mbuf_refcnt_read(m)== 1' and '__rte_mbuf_refcnt_update(m, -1)== 0' are the same cases where mbuf's refcnt value should be 1. Thus we can simplify the code and remove the redundant part.Furthermore, according to [1], when the mbuf is stored inside the mempool, the
m->refcntvalue should be 1. And then it is detached from its parent for an indirect mbuf. Thus change the description of 'rte_pktmbuf_prefree_seg' function.[1] https://patches.dpdk.org/project/dpdk/patch/20170404162807.20157-4-
[email protected]/Suggested-by Ruifeng Wang <[email protected]>Signed-off-by Feifei Wang <[email protected]>--- lib/mbuf/rte_mbuf.h | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-)lib/mbuf/rte_mbuf.h +0
-0
lib/mbuf/rte_mbuf.h static inline int · line 1332
Quoted 6 lines
lib/mbuf/rte_mbuf.h +1
-1@@ -1328,7 +1328,7 @@ static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m) * * This function does the same than a free, except that it does not * return the segment to its pool. - * It decreases the reference counter, and if it reaches 0, it is + * It decreases the reference counter, and if it reaches 1, it is
No, the original description is correct.
However, the reference counter is set to 1 when put back in the pool, as a shortcut so it isn't needed to be set back to 1 when allocated from the pool.
lib/mbuf/rte_mbuf.h rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
Quoted 4 lines
lib/mbuf/rte_mbuf.h +0
-0* detached from its parent for an indirect mbuf. * * @param m
lib/mbuf/rte_mbuf.h +0
-0@@ -1358,25 +1358,9 @@ rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
The preceding "if (likely(rte_mbuf_refcnt_read(m) == 1)) {" is only a shortcut for the likely case.
Quoted 28 lines
lib/mbuf/rte_mbuf.h +2
-19m->nb_segs =1 ;return m; - - }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; -rte_mbuf_refcnt_set (m,1 ); - -return m; } + +__rte_mbuf_refcnt_update (m,-1 );return NULL ; } --
Quoted 1 line
2.25.1
NAK.
This patch is not race safe. With the patch:
This thread:
if (likely(rte_mbuf_refcnt_read(m) == 1)) { // Assume it's 2.
The other thread:
if (likely(rte_mbuf_refcnt_read(m) == 1)) { // It's 2. __rte_mbuf_refcnt_update(m, -1); // Now it's 1. return NULL;
This thread:__rte_mbuf_refcnt_update(m, -1); // Now it's 0.
return NULL;
None of the threads have done the "prefree" work.
Quoted 70 lines
Quoted 25 lines
For '
rte_pktmbuf_prefree_seg' function, 'rte_mbuf_refcnt_read(m)== 1' and '__rte_mbuf_refcnt_update(m, -1)== 0' are the same cases where mbuf's refcnt value should be 1. Thus we can simplify the code and remove the redundant part.Furthermore, according to [1], when the mbuf is stored inside the mempool, the
m->refcntvalue should be 1. And then it is detached from its parent for an indirect mbuf. Thus change the description of 'rte_pktmbuf_prefree_seg' function.[1] https://patches.dpdk.org/project/dpdk/patch/20170404162807.20157-4-
[email protected]/Suggested-by Ruifeng Wang <[email protected]>Signed-off-by Feifei Wang <[email protected]>--- lib/mbuf/rte_mbuf.h | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-)lib/mbuf/rte_mbuf.h +0
-0lib/mbuf/rte_mbuf.h static inline int · line 1332
Quoted 6 lines
lib/mbuf/rte_mbuf.h +1
-1@@ -1328,7 +1328,7 @@ static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m) * * This function does the same than a free, except that it does not * return the segment to its pool. - * It decreases the reference counter, and if it reaches 0, it is + * It decreases the reference counter, and if it reaches 1, it isNo, the original description is correct.
However, the reference counter is set to 1 when put back in the pool, as a shortcut so it isn't needed to be set back to 1 when allocated from the pool.lib/mbuf/rte_mbuf.h rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
Quoted 4 lines
lib/mbuf/rte_mbuf.h +0
-0* detached from its parent for an indirect mbuf. * * @param m
lib/mbuf/rte_mbuf.h +0
-0@@ -1358,25 +1358,9 @@ rte_pktmbuf_prefree_seg(struct rte_mbuf *m)The preceding "if (
likely(rte_mbuf_refcnt_read(m) == 1)) {" is only a shortcut for the likely case.Quoted 27 lines
lib/mbuf/rte_mbuf.h +2
-18m->nb_segs =1 ;return m; - - }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; -rte_mbuf_refcnt_set (m,1 ); - -return m; } + +__rte_mbuf_refcnt_update (m,-1 );return NULL ; }
Quoted 6 lines
Quoted 2 lines
--
2.25.1NAK.
This patch is not race safe.
+1, It is a bad idea.
Quoted 15 lines
With the patch:
This thread:
if (likely(rte_mbuf_refcnt_read(m) == 1)) { // Assume it's 2.The other thread:
if (likely(rte_mbuf_refcnt_read(m) == 1)) { // It's 2.__rte_mbuf_refcnt_update(m, -1); // Now it's 1. return NULL;This thread:
__rte_mbuf_refcnt_update(m, -1); // Now it's 0.
return NULL;None of the threads have done the "prefree" work.
Quoted 7 lines · On Mon, 4 Dec 2023 11:07:08 +0000 Konstantin Ananyev…
Quoted 5 lines
Quoted 1 line
2.25.1
NAK.
This patch is not race safe.
+1, It is a bad idea.
The patch does raise a couple of issues that could be addressed by rearranging. There is duplicate code, and there are no comments to explain the rationale.
Maybe something like the following (untested):
lib/mbuf/rte_mbuf.h +23 -33
@@ -1342,42 +1342,32 @@ rte_pktmbuf_prefree_seg(struct rte_mbuf *m) {__rte_mbuf_sanity_check (m,0 ); -if (likely (rte_mbuf_refcnt_read (m) ==1 )) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; - -return m; - - }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +if (likely (rte_mbuf_refcnt_read (m) !=1 ) ) { +/* If this is the only reference to the mbuf it can just +* be setup for reuse without modifying reference count. +*/ + }else if (unlikely (__rte_mbuf_refcnt_update (m,-1 ) ==0 )) { +/* This was last reference reset to 1 for recycling/free. */ rte_mbuf_refcnt_set (m,1 ); + }else { +/* mbuf is still in use */ +return NULL ; + } -return m; +if (!RTE_MBUF_DIRECT (m)) { +rte_pktmbuf_detach (m); +if (RTE_MBUF_HAS_EXTBUF (m) && +RTE_MBUF_HAS_PINNED_EXTBUF (m) && +__rte_pktmbuf_pinned_extbuf_decref (m)) + +return NULL ; } -return NULL ; + +if (m->next !=NULL ) + m->next =NULL ; +if (m->nb_segs !=1 ) + m->nb_segs =1 ; +return m; }/**
Quoted 47 lines · Stephen Hemminger
Quoted 7 lines · On Mon, 4 Dec 2023 11:07:08 +0000 Konstantin Ananyev…
Quoted 5 lines
Quoted 1 line
2.25.1
NAK.
This patch is not race safe.
+1, It is a bad idea.
The patch does raise a couple of issues that could be addressed by rearranging. There is duplicate code, and there are no comments to explain the rationale.
Maybe something like the following (untested):
lib/mbuf/rte_mbuf.h +0
-22@@ -1342,42 +1342,32 @@ rte_pktmbuf_prefree_seg(struct rte_mbuf *m) {__rte_mbuf_sanity_check (m,0 ); -if (likely (rte_mbuf_refcnt_read (m) ==1 )) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; - -return m; - - }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); - if (RTE_MBUF_HAS_EXTBUF(m) &&
lib/mbuf/rte_mbuf.h rte_pktmbuf_prefree_seg(struct rte_mbuf *m) · line 1345
Quoted 10 lines
lib/mbuf/rte_mbuf.h +1
-9- RTE_MBUF_HAS_PINNED_EXTBUF(m) && - __rte_pktmbuf_pinned_extbuf_decref(m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +if (likely (rte_mbuf_refcnt_read (m) !=1 ) ) {
== 1
lib/mbuf/rte_mbuf.h rte_pktmbuf_prefree_seg(struct rte_mbuf *m) · line 1356
Quoted 11 lines
lib/mbuf/rte_mbuf.h +9
-0+/* If this is the only reference to the mbuf it can just +* be setup for reuse without modifying reference count. +*/ + }else if (unlikely (__rte_mbuf_refcnt_update (m,-1 ) ==0 )) { +/* This was last reference reset to 1 for recycling/free. */ rte_mbuf_refcnt_set (m,1 ); + }else { +/* mbuf is still in use */ +return NULL ; + }
This seems much clearer with good comments.
Quoted 19 lines
lib/mbuf/rte_mbuf.h +13
-2-return m; +if (!RTE_MBUF_DIRECT (m)) { +rte_pktmbuf_detach (m); +if (RTE_MBUF_HAS_EXTBUF (m) && +RTE_MBUF_HAS_PINNED_EXTBUF (m) && +__rte_pktmbuf_pinned_extbuf_decref (m)) + +return NULL ; } -return NULL ; + +if (m->next !=NULL ) + m->next =NULL ; +if (m->nb_segs !=1 ) + m->nb_segs =1 ; +return m; }/**
Quoted 57 lines
Quoted 44 lines
Quoted 7 lines
Quoted 5 lines
NAK.
This patch is not race safe.
+1, It is a bad idea.
The patch does raise a couple of issues that could be addressed by rearranging. There is duplicate code, and there are no comments to explain the rationale.
Maybe something like the following (untested):
lib/mbuf/rte_mbuf.h +0
-22@@ -1342,42 +1342,32 @@ rte_pktmbuf_prefree_seg(struct rte_mbuf *m) {__rte_mbuf_sanity_check (m,0 ); -if (likely (rte_mbuf_refcnt_read (m) ==1 )) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; - -return m; - - }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); - if (RTE_MBUF_HAS_EXTBUF(m) &&lib/mbuf/rte_mbuf.h rte_pktmbuf_prefree_seg(struct rte_mbuf *m) · line 1345
Quoted 10 lines
lib/mbuf/rte_mbuf.h +1
-9- RTE_MBUF_HAS_PINNED_EXTBUF(m) && - __rte_pktmbuf_pinned_extbuf_decref(m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; +if (likely (rte_mbuf_refcnt_read (m) !=1 ) ) {== 1
Quoted 13 lines
lib/mbuf/rte_mbuf.h rte_pktmbuf_prefree_seg(struct rte_mbuf *m) · line 1356
Quoted 11 lines
lib/mbuf/rte_mbuf.h +9
-0+/* If this is the only reference to the mbuf it can just +* be setup for reuse without modifying reference count. +*/ + }else if (unlikely (__rte_mbuf_refcnt_update (m,-1 ) ==0 )) { +/* This was last reference reset to 1 for recycling/free. */ rte_mbuf_refcnt_set (m,1 ); + }else { +/* mbuf is still in use */ +return NULL ; + }This seems much clearer with good comments.
Then, it could be just:
/* put whatever likely/unlikely we believe would be the most common case */ if (rte_mbuf_refcnt_read(m) != 1 && __rte_mbuf_refcnt_update(m, -1) != 0)
return NULL;
/* do whatever cleanup is necessary */
Quoted 20 lines
Quoted 20 lines
lib/mbuf/rte_mbuf.h +13
-2-return m; +if (!RTE_MBUF_DIRECT (m)) { +rte_pktmbuf_detach (m); +if (RTE_MBUF_HAS_EXTBUF (m) && +RTE_MBUF_HAS_PINNED_EXTBUF (m) && +__rte_pktmbuf_pinned_extbuf_decref (m)) + +return NULL ; } -return NULL ; + +if (m->next !=NULL ) + m->next =NULL ; +if (m->nb_segs !=1 ) + m->nb_segs =1 ; +return m; }/**
Quoted 22 lines · Morten Brørup
Quoted 22 lines
From: Feifei Wang [mailto:[email protected]]
Sent: Monday, 4 December 2023 03.39For '
rte_pktmbuf_prefree_seg' function, 'rte_mbuf_refcnt_read(m)== 1' and '__rte_mbuf_refcnt_update(m, -1)== 0' are the same cases where mbuf's refcnt value should be 1. Thus we can simplify the code and remove the redundant part.Furthermore, according to [1], when the mbuf is stored inside the mempool, the
m->refcntvalue should be 1. And then it is detached from its parent for an indirect mbuf. Thus change the description of 'rte_pktmbuf_prefree_seg' function.[1] https://patches.dpdk.org/project/dpdk/patch/20170404162807.20157-4-
[email protected]/Suggested-by Ruifeng Wang <[email protected]>Signed-off-by Feifei Wang <[email protected]>--- lib/mbuf/rte_mbuf.h | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-)
Quoted 13 lines
lib/mbuf/rte_mbuf.h static inline int · line 1332
Quoted 11 lines
lib/mbuf/rte_mbuf.h +1
-1@@ -1328,7 +1328,7 @@ static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m) * * This function does the same than a free, except that it does not * return the segment to its pool. - * It decreases the reference counter, and if it reaches 0, it is + * It decreases the reference counter, and if it reaches 1, it isNo, the original description is correct.
However, the reference counter is set to 1 when put back in the pool, as a shortcut so it isn't needed to be set back to 1 when allocated from the pool.
Ok.
for 'else if (__rte_mbuf_refcnt_update(m, -1) == 0)' case, it is easy to understand.
but for '(likely(rte_mbuf_refcnt_read(m) == 1))' case, I think this will create misleading. dpdk users maybe difficult to understand why the code can not match the function description.
Maybe we need some explanation here?
Quoted 5 lines
lib/mbuf/rte_mbuf.h rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
Quoted 4 lines
lib/mbuf/rte_mbuf.h +0
-0* detached from its parent for an indirect mbuf. * * @param m
lib/mbuf/rte_mbuf.h +0
-0@@ -1358,25 +1358,9 @@ rte_pktmbuf_prefree_seg(struct rte_mbuf *m)The preceding "if (
likely(rte_mbuf_refcnt_read(m) == 1)) {" is only a shortcut for the likely case.
Ok.
Quoted 41 lines
Quoted 28 lines
lib/mbuf/rte_mbuf.h +2
-19m->nb_segs =1 ;return m; - - }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) { - -if (!RTE_MBUF_DIRECT (m)) { -rte_pktmbuf_detach (m); -if (RTE_MBUF_HAS_EXTBUF (m) && -RTE_MBUF_HAS_PINNED_EXTBUF (m) && -__rte_pktmbuf_pinned_extbuf_decref (m)) -return NULL ; - } - -if (m->next !=NULL ) - m->next =NULL ; -if (m->nb_segs !=1 ) - m->nb_segs =1 ; -rte_mbuf_refcnt_set (m,1 ); - -return m; } + +__rte_mbuf_refcnt_update (m,-1 );return NULL ; } --Quoted 1 line
2.25.1
NAK.
This patch is not race safe. With the patch:
This thread:
if (likely(rte_mbuf_refcnt_read(m) == 1)) { // Assume it's 2.The other thread:
if (likely(rte_mbuf_refcnt_read(m) == 1)) { // It's 2.__rte_mbuf_refcnt_update(m, -1); // Now it's 1. return NULL;
Quoted 6 lines
This thread:
__rte_mbuf_refcnt_update(m, -1); // Now it's 0.
return NULL;None of the threads have done the "prefree" work.
Agree. After we see the failture of unit_test, we realize that we ignored the mutiple thread case.
Also maybe we need to add extra descripion to avoid misleading?
Quoted 51 lines
From: Feifei Wang [mailto:[email protected]]
Sent: Tuesday, 5 December 2023 04.13Quoted 4 lines · Morten Brørup
Quoted 4 lines
From: Feifei Wang [mailto:[email protected]]
Sent: Monday, 4 December 2023 03.39For '
rte_pktmbuf_prefree_seg' function, 'rte_mbuf_refcnt_read(m)==1'
Quoted 10 lines
Quoted 10 lines
and '
__rte_mbuf_refcnt_update(m, -1)== 0' are the same cases where mbuf's refcnt value should be 1. Thus we can simplify the code and remove the redundant part.Furthermore, according to [1], when the mbuf is stored inside the mempool, the
m->refcntvalue should be 1. And then it is detached from its parent for an indirect mbuf. Thus change the description of 'rte_pktmbuf_prefree_seg' function.[1]
https://patches.dpdk.org/project/dpdk/patch/20170404162807.20157-4-
Quoted 8 lines
Quoted 8 lines
Suggested-by Ruifeng Wang <[email protected]>Signed-off-by Feifei Wang <[email protected]>--- lib/mbuf/rte_mbuf.h | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-)Quoted 8 lines
lib/mbuf/rte_mbuf.h static inline int · line 1330
Quoted 8 lines
lib/mbuf/rte_mbuf.h +0
-0@@ -1328,7 +1328,7 @@ static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m) * * This function does the same than a free, except that it doesnot
Quoted 5 lines
lib/mbuf/rte_mbuf.h static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m) · line 1331
Quoted 3 lines
lib/mbuf/rte_mbuf.h +1
-1* return the segment to its pool. -* It decreases the reference counter, and if it reaches 0, it is +* It decreases the reference counter, and if it reaches 1, it is No, the original description is correct.
However, the reference counter is set to 1 when put back in the pool,as a shortcut so it isn't needed to be set back to 1 when allocated from the pool.
Ok.
for 'else if (
__rte_mbuf_refcnt_update(m, -1)== 0)' case, it is easy to
understand.
Quoted 7 lines
but for '(
likely(rte_mbuf_refcnt_read(m) == 1))' case, I think this will
create misleading. dpdk users maybe difficult to understand why the code
can not match the function description.Maybe we need some explanation here?
Agree. It is quite counterintuitive (but a clever optimization!) that the reference counter is 1 instead of 0 when free.
Something like:
static __rte_always_inline struct rte_mbuf *rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
{
__rte_mbuf_sanity_check(m, 0);
if (likely(rte_mbuf_refcnt_read(m) == 1)) {+ /* This branch is a performance optimized variant of the branch below.
+ * If the reference counter would reach 0 when decrementing it,
+ * do not decrement it to 0 and then initialize it to 1;
+ * just leave it at 1, thereby avoiding writing to memory.
+ */
if (!RTE_MBUF_DIRECT(m)) {
rte_pktmbuf_detach(m);
if (RTE_MBUF_HAS_EXTBUF(m) &&
RTE_MBUF_HAS_PINNED_EXTBUF(m) &&
__rte_pktmbuf_pinned_extbuf_decref(m))
return NULL;
}
if (m->next != NULL)
m->next = NULL;
if (m->nb_segs != 1)
m->nb_segs = 1;+ /* No need to initialize the reference counter; it is already 1. */
return m;
} else if (__rte_mbuf_refcnt_update(m, -1) == 0) {
if (!RTE_MBUF_DIRECT(m)) {
rte_pktmbuf_detach(m);
if (RTE_MBUF_HAS_EXTBUF(m) &&
RTE_MBUF_HAS_PINNED_EXTBUF(m) &&
__rte_pktmbuf_pinned_extbuf_decref(m))
return NULL;
}
if (m->next != NULL)
m->next = NULL;
if (m->nb_segs != 1)
m->nb_segs = 1;
+ /* Initialize the reference counter to 1, so
+ * incrementing it is unnecessary when allocating the mbuf.
+ */
rte_mbuf_refcnt_set(m, 1);
return m;
}
return NULL;
}Alternatively, add a function to do the initialization work:
static __rte_always_inline struct rte_mbuf * rte_pktmbuf_prefree_seg_last_ref(struct rte_mbuf *m, const bool init_refcnt)
{
if (!RTE_MBUF_DIRECT(m)) {
rte_pktmbuf_detach(m);
if (RTE_MBUF_HAS_EXTBUF(m) &&
RTE_MBUF_HAS_PINNED_EXTBUF(m) &&
__rte_pktmbuf_pinned_extbuf_decref(m))
return NULL;
}
if (m->next != NULL)
m->next = NULL;
if (m->nb_segs != 1)
m->nb_segs = 1;+ /* The reference counter must be initialized to 1 when the mbuf is free,
+ * so incrementing to 1 is unnecessary when allocating the mbuf.
+ */
if (init_refcnt) rte_mbuf_refcnt_set(m, 1); }
static __rte_always_inline struct rte_mbuf *rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
{
__rte_mbuf_sanity_check(m, 0);
if (likely(rte_mbuf_refcnt_read(m) == 1)) {+ /* This branch is a performance optimized variant of the branch below.
+ * If the reference counter would reach 0 when decrementing it,
+ * do not decrement it to 0 and then initialize it to 1;
+ * just leave it at 1, thereby avoiding writing to memory.
+ */
return rte_pktmbuf_prefree_seg_last_ref(m, false);
} else if (__rte_mbuf_refcnt_update(m, -1) == 0) {
return rte_pktmbuf_prefree_seg_last_ref(m, true);
}
return NULL;
}And while we're at it, we could add unlikely() to the second comparison:
if (unlikely(__rte_mbuf_refcnt_update(m, -1) == 0))
Quoted 70 lines · Morten Brørup
Quoted 51 lines
From: Feifei Wang [mailto:[email protected]]
Sent: Tuesday, 5 December 2023 04.13Quoted 4 lines · Morten Brørup
Quoted 4 lines
From: Feifei Wang [mailto:[email protected]]
Sent: Monday, 4 December 2023 03.39For '
rte_pktmbuf_prefree_seg' function, 'rte_mbuf_refcnt_read(m)==1'
Quoted 10 lines
Quoted 10 lines
and '
__rte_mbuf_refcnt_update(m, -1)== 0' are the same cases where mbuf's refcnt value should be 1. Thus we can simplify the code and remove the redundant part.Furthermore, according to [1], when the mbuf is stored inside the mempool, the
m->refcntvalue should be 1. And then it is detached from its parent for an indirect mbuf. Thus change the description of 'rte_pktmbuf_prefree_seg' function.[1]
https://patches.dpdk.org/project/dpdk/patch/20170404162807.20157-4-
Quoted 8 lines
Quoted 8 lines
Suggested-by Ruifeng Wang <[email protected]>Signed-off-by Feifei Wang <[email protected]>--- lib/mbuf/rte_mbuf.h | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-)Quoted 8 lines
lib/mbuf/rte_mbuf.h static inline int · line 1330
Quoted 8 lines
lib/mbuf/rte_mbuf.h +0
-0@@ -1328,7 +1328,7 @@ static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m) * * This function does the same than a free, except that it doesnot
Quoted 5 lines
lib/mbuf/rte_mbuf.h static inline int __rte_pktmbuf_pinned_extbuf_decref(struct rte_mbuf *m) · line 1331
Quoted 3 lines
lib/mbuf/rte_mbuf.h +1
-1* return the segment to its pool. -* It decreases the reference counter, and if it reaches 0, it is +* It decreases the reference counter, and if it reaches 1, it is No, the original description is correct.
However, the reference counter is set to 1 when put back in the pool,as a shortcut so it isn't needed to be set back to 1 when allocated from the pool.
Ok.
for 'else if (
__rte_mbuf_refcnt_update(m, -1)== 0)' case, it is easy to
understand.Quoted 7 lines
but for '(
likely(rte_mbuf_refcnt_read(m) == 1))' case, I think this will
create misleading. dpdk users maybe difficult to understand why the code
can not match the function description.Maybe we need some explanation here?
Agree. It is quite counterintuitive (but a clever optimization!) that the reference counter is 1 instead of 0 when free.
Something like:
static
__rte_always_inlinestructrte_mbuf*rte_pktmbuf_prefree_seg(struct rte_mbuf *m){ __rte_mbuf_sanity_check(m, 0); if (likely(rte_mbuf_refcnt_read(m) == 1)) {
Review
Quoted 5 lines
(quoted diff) +5
-0+ /* This branch is a performance optimized variant of the branch below. + * If the reference counter would reach 0 when decrementing it, + * do not decrement it to 0 and then initialize it to 1; + * just leave it at 1, thereby avoiding writing to memory. + */
Even more than avoiding writing to memory, we know that with a refcount of 1, this thread is the only thread using this mbuf, so we don't need to use atomic operations at all. The decrement we avoid is an especially expensive one because of the atomic aspect of it.
/Bruce