mbuf: cleanup rte_pktmbuf_lastseg(), fix atomic usage
PATCH v3 1/1 mbuf: cleanup rte_pktmbuf_lastseg(), fix atomic usage
--- lib/librte_mbuf/rte_mbuf.h | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-)
lib/librte_mbuf/rte_mbuf.h +0 -0
@@ -1159,6 +1159,15 @@ static inline void rte_pktmbuf_reset(struct rte_mbuf *m)
__rte_mbuf_sanity_check(m, 1);
}
+
+static __rte_always_inline void rte_pktmbuf_reset_before_free(struct rte_mbuf *m) +{
+ if (m->next != NULL) {
+ m->next = NULL;
+ m->nb_segs = 1;
+ }
+}
+
/**
* Allocate a new mbuf from a mempool.
*
@@ -1323,8 +1332,7 @@ static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
m->ol_flags = 0;
if (rte_mbuf_refcnt_update(md, -1) == 0) {
- md->next = NULL;
- md->nb_segs = 1;
+ rte_pktmbuf_reset_before_free(md);
rte_mbuf_refcnt_set(md, 1);
rte_mbuf_raw_free(md);
}
@@ -1354,25 +1362,16 @@ rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
if (RTE_MBUF_INDIRECT(m))
rte_pktmbuf_detach(m);- if (m->next != NULL) {
- m->next = NULL;
- m->nb_segs = 1;
- }
-
+ rte_pktmbuf_reset_before_free(m); return m;
- } else if (rte_atomic16_add_return(&m->refcnt_atomic, -1) == 0) {
-
+ } else if (rte_mbuf_refcnt_update(m, -1) == 0) {
if (RTE_MBUF_INDIRECT(m)) rte_pktmbuf_detach(m);
- if (m->next != NULL) {
- m->next = NULL;
- m->nb_segs = 1;
- }
+ rte_pktmbuf_reset_before_free(m); rte_mbuf_refcnt_set(m, 1); - return m; } return NULL;
Signature · 1 line
2.9.3
Quoted 15 lines · Hanoh Haim
Signed-off-by Hanoh Haim <[email protected]>---
lib/librte_mbuf/rte_mbuf.h| 27 +++++++++++++--------------1 file changed, 13 insertions(+), 14 deletions(-)lib/librte_mbuf/rte_mbuf.h +0
-0@@ -1159,6 +1159,15 @@ static inline void rte_pktmbuf_reset(struct rte_mbuf *m)__rte_mbuf_sanity_check (m,1 ); }
lib/librte_mbuf/rte_mbuf.h static inline void rte_pktmbuf_reset(struct rte_mbuf *m) · line 1170
Quoted 10 lines
lib/librte_mbuf/rte_mbuf.h +9
-0+ +static __rte_always_inline void rte_pktmbuf_reset_before_free(struct rte_mbuf *m) +{ +if (m->next !=NULL ) { + m->next =NULL ; + m->nb_segs =1 ; + } +} +
Probably it will be more clean to add something __te_pktmbuf_reset_nb_segs() without check for (m->next != NULL) and use it everywhere in mbuf’s the code, not only in rte_pktmbuf_prefree_seg() function. And I think it will be better to have separate patch for that.
lib/librte_mbuf/rte_mbuf.h static inline void rte_pktmbuf_detach(struct rte_mbuf *m) · line 1327
Quoted 9 lines
lib/librte_mbuf/rte_mbuf.h +0
-2/** * Allocate a new mbuf from a mempool. * @@ -1323,8 +1332,7 @@ static inline void rte_pktmbuf_detach(struct rte_mbuf *m) m->ol_flags =0 ;if (rte_mbuf_refcnt_update (md,-1 ) ==0 ) { - md->next =NULL ; - md->nb_segs =1 ;
Using rte_pktmbuf_reset_before_free() here adds unnecessary check for m->next in that path.
Quoted 36 lines
lib/librte_mbuf/rte_mbuf.h +4
-12+rte_pktmbuf_reset_before_free (md);rte_mbuf_refcnt_set (md,1 );rte_mbuf_raw_free (md); }@@ -1354,25 +1362,16 @@ rte_pktmbuf_prefree_seg(struct rte_mbuf *m)if (RTE_MBUF_INDIRECT (m))rte_pktmbuf_detach (m); -if (m->next !=NULL ) { - m->next =NULL ; - m->nb_segs =1 ; - } - +rte_pktmbuf_reset_before_free (m);return m; - }else if (rte_atomic16_add_return (&m->refcnt_atomic,-1 ) ==0 ) { - + }else if (rte_mbuf_refcnt_update (m,-1 ) ==0 ) {if (RTE_MBUF_INDIRECT (m))rte_pktmbuf_detach (m); -if (m->next !=NULL ) { - m->next =NULL ; - m->nb_segs =1 ; - } +rte_pktmbuf_reset_before_free (m);rte_mbuf_refcnt_set (m,1 ); -return m; }return NULL ;Signature · 1 line
2.9.3
+Oliver,
Ilia, I assume there is a reason why Oliver did that, I just consolidate the code.
He didn't want to *write* the same value to mbuf field.
In the common case that pointer was already cleared by the driver, it is better to just read it. From cache synchronization perspective it will run faster.
Thanks,
Hanoh
-----Original Message-----
From: Ilya Matveychikov [mailto:[email protected]]
Sent: Wednesday, November 15, 2017 1:14 PM
To: Hanoch Haim (hhaim)
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH v3] mbuf: cleanup rte_pktmbuf_lastseg(), fix atomic usage
Quoted 14 lines · Hanoh Haim
Signed-off-by Hanoh Haim <[email protected]>---
lib/librte_mbuf/rte_mbuf.h| 27 +++++++++++++--------------1 file changed, 13 insertions(+), 14 deletions(-)lib/librte_mbuf/rte_mbuf.h +0
-0@@ -1159,6 +1159,15 @@ static inline void rte_pktmbuf_reset(struct rte_mbuf *m)__rte_mbuf_sanity_check (m,1 ); }
lib/librte_mbuf/rte_mbuf.h static inline void rte_pktmbuf_reset(struct rte_mbuf *m) · line 1170
Quoted 10 lines
lib/librte_mbuf/rte_mbuf.h +9
-0+ +static __rte_always_inline void rte_pktmbuf_reset_before_free(struct +rte_mbuf *m) { +if (m->next !=NULL ) { + m->next =NULL ; + m->nb_segs =1 ; + } +} +
Probably it will be more clean to add something __te_pktmbuf_reset_nb_segs() without check for (m->next != NULL) and use it everywhere in mbuf’s the code, not only in rte_pktmbuf_prefree_seg() function. And I think it will be better to have separate patch for that.
lib/librte_mbuf/rte_mbuf.h static inline void rte_pktmbuf_detach(struct rte_mbuf *m) · line 1327
Quoted 9 lines
lib/librte_mbuf/rte_mbuf.h +0
-2/** * Allocate a new mbuf from a mempool. * @@ -1323,8 +1332,7 @@ static inline void rte_pktmbuf_detach(struct rte_mbuf *m) m->ol_flags =0 ;if (rte_mbuf_refcnt_update (md,-1 ) ==0 ) { - md->next =NULL ; - md->nb_segs =1 ;
Using rte_pktmbuf_reset_before_free() here adds unnecessary check for m->next in that path.
Quoted 36 lines
lib/librte_mbuf/rte_mbuf.h +4
-13+rte_pktmbuf_reset_before_free (md);rte_mbuf_refcnt_set (md,1 );rte_mbuf_raw_free (md); }@@ -1354,25 +1362,16 @@ rte_pktmbuf_prefree_seg(struct rte_mbuf *m)if (RTE_MBUF_INDIRECT (m))rte_pktmbuf_detach (m); -if (m->next !=NULL ) { - m->next =NULL ; - m->nb_segs =1 ; - } - +rte_pktmbuf_reset_before_free (m);return m; - }else if (rte_atomic16_add_return (&m->refcnt_atomic,-1 ) ==0 ) { - + }else if (rte_mbuf_refcnt_update (m,-1 ) ==0 ) {if (RTE_MBUF_INDIRECT (m))rte_pktmbuf_detach (m); -if (m->next !=NULL ) { - m->next =NULL ; - m->nb_segs =1 ; - } +rte_pktmbuf_reset_before_free (m);rte_mbuf_refcnt_set (m,1 ); -return m; }return NULL ; --2.9.3
Hi,
Quoted 9 lines · Hanoch Haim (hhaim)
+Oliver,
Ilia, I assume there is a reason why Oliver did that, I just consolidate the code.
He didn't want to *write* the same value to mbuf field.
In the common case that pointer was already cleared by the driver, it is better to just read it. From cache synchronization perspective it will run faster.Thanks,
Hanoh
Quoted 10 lines
-----Original Message-----
From: Ilya Matveychikov [mailto:[email protected]]
Sent: Wednesday, November 15, 2017 1:14 PM
To: Hanoch Haim (hhaim)
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH v3] mbuf: cleanuprte_pktmbuf_lastseg(), fix atomic usageQuoted 2 lines · Hanoh Haim
I think the patch should be renamed in something like:
mbuf: fix mbuf free performance with non atomic refcnt
A description of the problem in the commit log would also be welcome.
It looks it is a regression introduced by commit 8f094a9ac5d7. In that case, we should also have:
Quoted 13 lines
Quoted 13 lines
Signed-off-by Hanoh Haim <[email protected]>---
lib/librte_mbuf/rte_mbuf.h| 27 +++++++++++++--------------1 file changed, 13 insertions(+), 14 deletions(-)lib/librte_mbuf/rte_mbuf.h +0
-0@@ -1159,6 +1159,15 @@ static inline void rte_pktmbuf_reset(struct rte_mbuf *m)__rte_mbuf_sanity_check (m,1 ); }
Quoted 12 lines
lib/librte_mbuf/rte_mbuf.h static inline void rte_pktmbuf_reset(struct rte_mbuf *m) · line 1170
Quoted 9 lines
lib/librte_mbuf/rte_mbuf.h +9
-0+ +static __rte_always_inline void rte_pktmbuf_reset_before_free(struct +rte_mbuf *m) { +if (m->next !=NULL ) { + m->next =NULL ; + m->nb_segs =1 ; + } +} +Probably it will be more clean to add something
__te_pktmbuf_reset_nb_segs()without check for (m->next!= NULL) and use it everywhere in mbuf’s the code, not only inrte_pktmbuf_prefree_seg()function. And I think it will be better to have separate patch for that.
I'm not convinced that:
__rte_pktmbuf_reset_nb_segs(m);
is clearer than:
m->next = NULL; m->nb_segs = 1;
Anyway, I agree this should not be part of this patch. We should only keep the fix.
Quoted 7 lines
Quoted 7 lines
lib/librte_mbuf/rte_mbuf.h +0
-0/** * Allocate a new mbuf from a mempool. * @@ -1323,8 +1332,7 @@ static inline void rte_pktmbuf_detach(struct rte_mbuf *m) m->ol_flags =0 ;
Quoted 5 lines
lib/librte_mbuf/rte_mbuf.h static inline void rte_pktmbuf_detach(struct rte_mbuf *m) · line 1327
Quoted 3 lines
lib/librte_mbuf/rte_mbuf.h +0
-2if (rte_mbuf_refcnt_update (md,-1 ) ==0 ) { - md->next =NULL ; - md->nb_segs =1 ;Using
rte_pktmbuf_reset_before_free()here adds unnecessary check form->nextin that path.
Yes, agree with Ilya.
Quoted 17 lines
Quoted 17 lines
lib/librte_mbuf/rte_mbuf.h +2
-5+rte_pktmbuf_reset_before_free (md);rte_mbuf_refcnt_set (md,1 );rte_mbuf_raw_free (md); }@@ -1354,25 +1362,16 @@ rte_pktmbuf_prefree_seg(struct rte_mbuf *m)if (RTE_MBUF_INDIRECT (m))rte_pktmbuf_detach (m); -if (m->next !=NULL ) { - m->next =NULL ; - m->nb_segs =1 ; - } - +rte_pktmbuf_reset_before_free (m);return m;
Quoted 3 lines
lib/librte_mbuf/rte_mbuf.h rte_pktmbuf_prefree_seg(struct rte_mbuf *m) · line 1368
Quoted 3 lines
lib/librte_mbuf/rte_mbuf.h +1
-2- }else if (rte_atomic16_add_return (&m->refcnt_atomic,-1 ) ==0 ) { - + }else if (rte_mbuf_refcnt_update (m,-1 ) ==0 ) {
I agree with Konstantin's comment done in another thread [1]:
'''
That would cause extra read; cmp (and possible slowdown) for atomic refcnt.
If that really need to be fixed - probably we need to introduce a new function that would do update without trying to read refctn first - rte_mbuf_refcnt_update_blind() or so. '''
However I'm not sure a new function is really needed: the name is not ideal, and it would only be used once. What about the patch below?
==============================
lib/librte_mbuf/rte_mbuf.h +11 -2
@@ -1361,8 +1361,18 @@ rte_pktmbuf_prefree_seg(struct rte_mbuf *m)return m; - }else if (rte_atomic16_add_return (&m->refcnt_atomic,-1 ) ==0 ) { + }else { +/* We don't use rte_mbuf_refcnt_update() because we already +* tested that refcnt != 1. +*/ +#ifdef RTE_MBUF_REFCNT_ATOMIC + ret =rte_atomic16_add_return (&m->refcnt_atomic,-1 ); +#else + ret = --m->refcnt; +#endif +if (ret !=0 ) +return NULL ;if (RTE_MBUF_INDIRECT (m))rte_pktmbuf_detach (m);
@@ -1375,7 +1385,6 @@ rte_pktmbuf_prefree_seg(struct rte_mbuf *m)return m; } -return NULL ; }/* deprecated, replaced by rte_pktmbuf_prefree_seg() */
==============================
[1] http://dpdk.org/dev/patchwork/patch/31378/
Regards,
Olivier
Hi Oliver,
It's hard for me to follow this thread.
1) It is not about clear/not-clear, it is error prone to *replicate* code that has the same logic.
"I'm not convinced that:
__rte_pktmbuf_reset_nb_segs(m);
is clearer than:
m->next = NULL; m->nb_segs = 1;
Anyway, I agree this should not be part of this patch. We should only keep the fix.
"
2) This definitely does not look good.
All the point in my patch is to move the ref-cnt operations to set of API that already taking care of RTE_MBUF_REFCNT_ATOMIC
+ /* We don't use rte_mbuf_refcnt_update() because we already
+ * tested that refcnt != 1.
+ */
+#ifdef RTE_MBUF_REFCNT_ATOMIC
+ ret = rte_atomic16_add_return(&m->refcnt_atomic, -1);
+#else
+ ret = --m->refcnt;
+#endif
+ if (ret != 0)
+ return NULL;
Hanoh
-----Original Message-----
From: Olivier MATZ [mailto:[email protected]]
Sent: Wednesday, November 15, 2017 7:31 PM
To: Hanoch Haim (hhaim)
Cc: Ilya Matveychikov; [email protected]; Konstantin Ananyev
Subject: Re: [dpdk-dev] [PATCH v3] mbuf: cleanup rte_pktmbuf_lastseg(), fix atomic usage
Hi,
Quoted 9 lines · Hanoch Haim (hhaim)
+Oliver,
Ilia, I assume there is a reason why Oliver did that, I just consolidate the code.
He didn't want to *write* the same value to mbuf field.
In the common case that pointer was already cleared by the driver, it is better to just read it. From cache synchronization perspective it will run faster.Thanks,
Hanoh
Quoted 11 lines
-----Original Message-----
From: Ilya Matveychikov [mailto:[email protected]]
Sent: Wednesday, November 15, 2017 1:14 PM
To: Hanoch Haim (hhaim)
Cc: [email protected]
Subject: Re: [dpdk-dev] [PATCH v3] mbuf: cleanuprte_pktmbuf_lastseg(), fix atomic usageQuoted 2 lines · Hanoh Haim
I think the patch should be renamed in something like:
mbuf: fix mbuf free performance with non atomic refcnt
A description of the problem in the commit log would also be welcome.
It looks it is a regression introduced by commit 8f094a9ac5d7. In that case, we should also have:
Quoted 13 lines
Quoted 13 lines
Signed-off-by Hanoh Haim <[email protected]>---
lib/librte_mbuf/rte_mbuf.h| 27 +++++++++++++--------------1 file changed, 13 insertions(+), 14 deletions(-)lib/librte_mbuf/rte_mbuf.h +0
-0@@ -1159,6 +1159,15 @@ static inline void rte_pktmbuf_reset(struct rte_mbuf *m)__rte_mbuf_sanity_check (m,1 ); }
Quoted 15 lines
lib/librte_mbuf/rte_mbuf.h static inline void rte_pktmbuf_reset(struct rte_mbuf *m) · line 1171
Quoted 10 lines
lib/librte_mbuf/rte_mbuf.h +10
-0+ +static __rte_always_inline void +rte_pktmbuf_reset_before_free (struct +rte_mbuf *m) { +if (m->next !=NULL ) { + m->next =NULL ; + m->nb_segs =1 ; + } +} +Probably it will be more clean to add something
__te_pktmbuf_reset_nb_segs()without check for (m->next!= NULL) and
use it everywhere in mbuf’s the code, not only inrte_pktmbuf_prefree_seg()function. And I think it will be better to have separate patch for that.
I'm not convinced that:
__rte_pktmbuf_reset_nb_segs(m);
is clearer than:
m->next = NULL; m->nb_segs = 1;
Anyway, I agree this should not be part of this patch. We should only keep the fix.
Quoted 7 lines
Quoted 7 lines
lib/librte_mbuf/rte_mbuf.h +0
-0/** * Allocate a new mbuf from a mempool. * @@ -1323,8 +1332,7 @@ static inline void rte_pktmbuf_detach(struct rte_mbuf *m) m->ol_flags =0 ;
Quoted 5 lines
lib/librte_mbuf/rte_mbuf.h static inline void rte_pktmbuf_detach(struct rte_mbuf *m) · line 1327
Quoted 3 lines
lib/librte_mbuf/rte_mbuf.h +0
-2if (rte_mbuf_refcnt_update (md,-1 ) ==0 ) { - md->next =NULL ; - md->nb_segs =1 ;Using
rte_pktmbuf_reset_before_free()here adds unnecessary check form->nextin that path.
Yes, agree with Ilya.
Quoted 17 lines
Quoted 17 lines
lib/librte_mbuf/rte_mbuf.h +2
-5+rte_pktmbuf_reset_before_free (md);rte_mbuf_refcnt_set (md,1 );rte_mbuf_raw_free (md); }@@ -1354,25 +1362,16 @@ rte_pktmbuf_prefree_seg(struct rte_mbuf *m)if (RTE_MBUF_INDIRECT (m))rte_pktmbuf_detach (m); -if (m->next !=NULL ) { - m->next =NULL ; - m->nb_segs =1 ; - } - +rte_pktmbuf_reset_before_free (m);return m;
Quoted 3 lines
lib/librte_mbuf/rte_mbuf.h rte_pktmbuf_prefree_seg(struct rte_mbuf *m) · line 1368
Quoted 3 lines
lib/librte_mbuf/rte_mbuf.h +1
-2- }else if (rte_atomic16_add_return (&m->refcnt_atomic,-1 ) ==0 ) { - + }else if (rte_mbuf_refcnt_update (m,-1 ) ==0 ) {
I agree with Konstantin's comment done in another thread [1]:
'''
That would cause extra read; cmp (and possible slowdown) for atomic refcnt.
If that really need to be fixed - probably we need to introduce a new function that would do update without trying to read refctn first - rte_mbuf_refcnt_update_blind() or so. '''
However I'm not sure a new function is really needed: the name is not ideal, and it would only be used once. What about the patch below?
==============================
lib/librte_mbuf/rte_mbuf.h +11 -1
@@ -1361,8 +1361,18 @@ rte_pktmbuf_prefree_seg(struct rte_mbuf *m)return m; - }else if (rte_atomic16_add_return (&m->refcnt_atomic,-1 ) ==0 ) { + }else { +/* We don't use rte_mbuf_refcnt_update() because we already +* tested that refcnt != 1. +*/ +#ifdef RTE_MBUF_REFCNT_ATOMIC + ret =rte_atomic16_add_return (&m->refcnt_atomic,-1 ); +#else + ret = --m->refcnt; +#endif +if (ret !=0 ) +return NULL ;if (RTE_MBUF_INDIRECT (m)) rte_pktmbuf_detach(m); @@ -1375,7 +1385,6 @@ rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
return m;
}- return NULL;
}
/* deprecated, replaced by rte_pktmbuf_prefree_seg() */ ==============================
[1] http://dpdk.org/dev/patchwork/patch/31378/
Regards,
Olivier
Quoted 24 lines · Hanoch Haim (hhaim)
Hi Oliver,
It's hard for me to follow this thread.
1) It is not about clear/not-clear, it is error prone to *replicate* code that has the same logic.
"I'm not convinced that:
__rte_pktmbuf_reset_nb_segs(m);is clearer than:
m->next = NULL; m->nb_segs = 1;Anyway, I agree this should not be part of this patch. We should only keep the fix.
"
2) This definitely does not look good.
All the point in my patch is to move the ref-cnt operations to set of API that already taking care ofRTE_MBUF_REFCNT_ATOMIC
lib/librte_mbuf/rte_mbuf.h rte_pktmbuf_prefree_seg(struct rte_mbuf *m) · line 1375
Quoted 10 lines
lib/librte_mbuf/rte_mbuf.h +10
-0+/* We don't use rte_mbuf_refcnt_update() because we already +* tested that refcnt != 1. +*/ +#ifdef RTE_MBUF_REFCNT_ATOMIC + ret =rte_atomic16_add_return (&m->refcnt_atomic,-1 ); +#else + ret = --m->refcnt; +#endif +if (ret !=0 ) +return NULL ;
Looks ugly, agreed.
Hi Hanoh,
Quoted 3 lines · Hanoch Haim (hhaim)
Hi Oliver,
It's hard for me to follow this thread.
Yes, here are some few tips to make it easier to follow:
- avoid top-posting
- prefix quoted lines with "> "
- describe the problem and how you solve it in the commit log
- one problem = one patch
Quoted 6 lines
1) It is not about clear/not-clear, it is error prone to *replicate* code that has the same logic.
"I'm not convinced that:
__rte_pktmbuf_reset_nb_segs(m);
Quoted 7 lines
is clearer than:
m->next = NULL; m->nb_segs = 1;Anyway, I agree this should not be part of this patch. We should only keep the fix. "
rte_mbuf_refcnt_update() was not used in rte_pktmbuf_prefree_seg() to avoid reading the refcount twice.
The problem of having clear or unclear is fundamental. I don't see the point of having a function __rte_pktmbuf_reset_nb_segs(). Keeping the two affectations makes things explicit.
Quoted 4 lines
2) This definitely does not look good.
All the point in my patch is to move the ref-cnt operations to set of API that already taking care ofRTE_MBUF_REFCNT_ATOMIC
lib/librte_mbuf/rte_mbuf.h rte_pktmbuf_prefree_seg(struct rte_mbuf *m) · line 1375
Quoted 10 lines
lib/librte_mbuf/rte_mbuf.h +10
-0+/* We don't use rte_mbuf_refcnt_update() because we already +* tested that refcnt != 1. +*/ +#ifdef RTE_MBUF_REFCNT_ATOMIC + ret =rte_atomic16_add_return (&m->refcnt_atomic,-1 ); +#else + ret = --m->refcnt; +#endif +if (ret !=0 ) +return NULL ;
We cannot use the existing API taking care of atomic refcount rte_mbuf_refcnt_update() because it would read the refcount twice.
We cannot change the behavior of rte_mbuf_refcnt_update() because it's a public API.
An option proposed by Konstantin is to introduce a new helper rte_mbuf_refcnt_update_blind() that does the same than rte_mbuf_refcnt_update() but without the first test. It think it is a bit overkill to have this function for one caller.
That's why I end up with this patch. I don't see why it would be an issue to have a mbuf ifdef inside the mbuf code.
Olivier
Understood
rte_mbuf_refcnt_update_blind()
should be good., it will take care the RTE_MBUF_REFCNT_ATOMIC
Hanoh
-----Original Message-----
From: Olivier MATZ [mailto:[email protected]]
Sent: Thursday, November 16, 2017 10:42 AM
To: Hanoch Haim (hhaim)
Cc: Konstantin Ananyev; Ilya Matveychikov; [email protected]
Subject: Re: [dpdk-dev] [PATCH v3] mbuf: cleanup rte_pktmbuf_lastseg(), fix atomic usage
Hi Hanoh,
Quoted 3 lines · Hanoch Haim (hhaim)
Hi Oliver,
It's hard for me to follow this thread.
Yes, here are some few tips to make it easier to follow:
- avoid top-posting
- prefix quoted lines with "> "
- describe the problem and how you solve it in the commit log
- one problem = one patch
Quoted 6 lines
1) It is not about clear/not-clear, it is error prone to *replicate* code that has the same logic.
"I'm not convinced that:
__rte_pktmbuf_reset_nb_segs(m);
Quoted 7 lines
is clearer than:
m->next = NULL; m->nb_segs = 1;Anyway, I agree this should not be part of this patch. We should only keep the fix. "
rte_mbuf_refcnt_update() was not used in rte_pktmbuf_prefree_seg() to avoid reading the refcount twice.
The problem of having clear or unclear is fundamental. I don't see the point of having a function __rte_pktmbuf_reset_nb_segs(). Keeping the two affectations makes things explicit.
Quoted 5 lines
2) This definitely does not look good.
All the point in my patch is to move the ref-cnt operations to set of API that already taking care ofRTE_MBUF_REFCNT_ATOMIC
lib/librte_mbuf/rte_mbuf.h rte_pktmbuf_prefree_seg(struct rte_mbuf *m) · line 1375
Quoted 10 lines
lib/librte_mbuf/rte_mbuf.h +10
-0+/* We don't use rte_mbuf_refcnt_update() because we already +* tested that refcnt != 1. +*/ +#ifdef RTE_MBUF_REFCNT_ATOMIC + ret =rte_atomic16_add_return (&m->refcnt_atomic,-1 ); +#else + ret = --m->refcnt; +#endif +if (ret !=0 ) +return NULL ;
We cannot use the existing API taking care of atomic refcount rte_mbuf_refcnt_update() because it would read the refcount twice.
We cannot change the behavior of rte_mbuf_refcnt_update() because it's a public API.
An option proposed by Konstantin is to introduce a new helperrte_mbuf_refcnt_update_blind() that does the same thanrte_mbuf_refcnt_update() but without the first test. It think it is a bit overkill to have this function for one caller.
That's why I end up with this patch. I don't see why it would be an issue to have a mbuf ifdef inside the mbuf code.
Olivier
Quoted 9 lines · Hanoch Haim (hhaim)
Understood
rte_mbuf_refcnt_update_blind()should be good., it will take care the
RTE_MBUF_REFCNT_ATOMIC
Why guys not to add just __rte_mbuf_refcnt_update() as a wrapper over rte_atomic16_add_return() and use it in inside rte_mbuf_refcnt_update() and rte_pktmbuf_prefree_seg() as well?
Quoted 7 lines · Ilya Matveychikov
Quoted 7 lines · Hanoch Haim (hhaim)
Understood
rte_mbuf_refcnt_update_blind()
Quoted 8 lines
Quoted 2 lines
should be good., it will take care the
RTE_MBUF_REFCNT_ATOMICWhy guys not to add just
__rte_mbuf_refcnt_update()as a wrapper overrte_atomic16_add_return()and use it in insiderte_mbuf_refcnt_update()andrte_pktmbuf_prefree_seg()as well?
Is there any other difference with rte_mbuf_refcnt_update_blind() except the function name?
Quoted 15 lines · Olivier MATZ
Quoted 11 lines · Ilya Matveychikov
Quoted 9 lines · Hanoch Haim (hhaim)
Understood
rte_mbuf_refcnt_update_blind()should be good., it will take care the
RTE_MBUF_REFCNT_ATOMIC
Quoted 7 lines
Quoted 4 lines
Why guys not to add just
__rte_mbuf_refcnt_update()as a wrapper overrte_atomic16_add_return()and use it in insiderte_mbuf_refcnt_update()andrte_pktmbuf_prefree_seg()as well?Is there any other difference with
rte_mbuf_refcnt_update_blind()except the function name?
No really, but my suggestion was not only about the name but to use such a function inside rte_mbuf_refcnt_update() too. Also, that is common naming scheme in Linux kernel — to add “__” prefix for for “lightweight” functions.
Anyway, IMO having a function will be better than having ifdef/else/endif block.
Hi Olivier,
Quoted 9 lines
I agree with Konstantin's comment done in another thread [1]:
'''
That would cause extra read; cmp (and possible slowdown) for atomic refcnt.
If that really need to be fixed - probably we need to introduce a new function that would do update without trying to read refctn first -rte_mbuf_refcnt_update_blind()or so. '''However I'm not sure a new function is really needed: the name is not ideal,
That was just the first one from top of my head :)
If the issue in naming only - I suppose we can find something better. Personally I like Ilya's one: __rte_mbuf_refcnt_update().
Quoted 1 line
and it would only be used once. What about the patch below?
It would do the job too, but looks a bit clumsy to me. Konstantin
Quoted 39 lines
==============================
lib/librte_mbuf/rte_mbuf.h +11
-2@@ -1361,8 +1361,18 @@ rte_pktmbuf_prefree_seg(struct rte_mbuf *m)return m; - }else if (rte_atomic16_add_return (&m->refcnt_atomic,-1 ) ==0 ) { + }else { +/* We don't use rte_mbuf_refcnt_update() because we already +* tested that refcnt != 1. +*/ +#ifdef RTE_MBUF_REFCNT_ATOMIC + ret =rte_atomic16_add_return (&m->refcnt_atomic,-1 ); +#else + ret = --m->refcnt; +#endif +if (ret !=0 ) +return NULL ;if (RTE_MBUF_INDIRECT (m))rte_pktmbuf_detach (m);@@ -1375,7 +1385,6 @@ rte_pktmbuf_prefree_seg(struct rte_mbuf *m)return m; } -return NULL ; }/* deprecated, replaced by rte_pktmbuf_prefree_seg() */ ==============================
[1] http://dpdk.org/dev/patchwork/patch/31378/
Regards,
Olivier
PATCH v4 1/1 mbuf: fix mbuf free performance with non atomic refcnt
In reply to Hanoh HaimWhen RTE_MBUF_REFCNT_ATOMIC=n, the decrement of the mbuf reference counter uses an atomic operation. This is not necessary and impacts the performance (seen with TRex traffic generator).
We cannot replace rte_atomic16_add_return() by rte_mbuf_refcnt_update() because it would add an additional check.
Solves this by introducing __rte_mbuf_refcnt_update(), which updates the reference counter without doing anything else.
---
Hanoh,
The following patch implements what was discussed in the thread. Are you ok with it?
Thanks,
Olivier
lib/librte_mbuf/rte_mbuf.h | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-)
lib/librte_mbuf/rte_mbuf.h +18 -5
@@ -764,6 +764,13 @@ rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)rte_atomic16_set (&m->refcnt_atomic, new_value); } +/* internal */ +static inline uint16_t +__rte_mbuf_refcnt_update (struct rte_mbuf *m,int16_t value) +{ +return (uint16_t )(rte_atomic16_add_return (&m->refcnt_atomic, value)); +} +/** * Adds given value to an mbuf's refcnt and returns its new value. * @param m
@@ -788,19 +795,26 @@ rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value) return 1 + value; } -return (uint16_t )(rte_atomic16_add_return (&m->refcnt_atomic, value)); +return __rte_mbuf_refcnt_update (m, value); }#else /* ! RTE_MBUF_REFCNT_ATOMIC */ +/* internal */ +static inline uint16_t +__rte_mbuf_refcnt_update (struct rte_mbuf *m,int16_t value) +{ + m->refcnt = (uint16_t )(m->refcnt + value); +return m->refcnt; +} +/** * Adds given value to an mbuf's refcnt and returns its new value. */ static inline uint16_t rte_mbuf_refcnt_update (struct rte_mbuf *m,int16_t value) { - m->refcnt = (uint16_t )(m->refcnt + value); -return m->refcnt; +return __rte_mbuf_refcnt_update (m, value); }/**
@@ -1364,8 +1378,7 @@ rte_pktmbuf_prefree_seg(struct rte_mbuf *m)return m; - }else if (rte_atomic16_add_return (&m->refcnt_atomic,-1 ) ==0 ) { - + }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) {if (RTE_MBUF_INDIRECT (m))rte_pktmbuf_detach (m);
Signature · 1 line
2.11.0
Re: [PATCH v4] mbuf: fix mbuf free performance with non atomic refcnt
Olivier,
Quoted 12 lines · Olivier Matz
lib/librte_mbuf/rte_mbuf.h| 23 ++++++++++++++++++-----1 file changed, 18 insertions(+), 5 deletions(-)lib/librte_mbuf/rte_mbuf.h +0
-0
lib/librte_mbuf/rte_mbuf.h rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value) · line 771
Quoted 8 lines
lib/librte_mbuf/rte_mbuf.h +5
-0@@ -764,6 +764,13 @@ rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)rte_atomic16_set (&m->refcnt_atomic, new_value); } +/* internal */ +static inline uint16_t +__rte_mbuf_refcnt_update (struct rte_mbuf *m,int16_t value) +{ +return (uint16_t )(rte_atomic16_add_return (&m->refcnt_atomic, value));
What’s the purpose of using direct cast to uint16_t here and in other places?
Quoted 48 lines
lib/librte_mbuf/rte_mbuf.h +11
-1+} +/** * Adds given value to an mbuf's refcnt and returns its new value. * @param m @@ -788,19 +795,26 @@ rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value) return 1 + value; } -return (uint16_t )(rte_atomic16_add_return (&m->refcnt_atomic, value)); +return __rte_mbuf_refcnt_update (m, value); }#else /* ! RTE_MBUF_REFCNT_ATOMIC */ +/* internal */ +static inline uint16_t +__rte_mbuf_refcnt_update (struct rte_mbuf *m,int16_t value) +{ + m->refcnt = (uint16_t )(m->refcnt + value); +return m->refcnt; +} +/** * Adds given value to an mbuf's refcnt and returns its new value. */ static inline
uint16_trte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value){lib/librte_mbuf/rte_mbuf.h +1
-2- m->refcnt = (uint16_t )(m->refcnt + value); -return m->refcnt; +return __rte_mbuf_refcnt_update (m, value);}
/**
lib/librte_mbuf/rte_mbuf.h +1
-2@@ -1364,8 +1378,7 @@ rte_pktmbuf_prefree_seg(struct rte_mbuf *m)return m; - }else if (rte_atomic16_add_return (&m->refcnt_atomic,-1 ) ==0 ) { - + }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) {if (RTE_MBUF_INDIRECT (m))rte_pktmbuf_detach (m);Signature · 1 line
2.11.0
Re: [PATCH v4] mbuf: fix mbuf free performance with non atomic refcnt
Quoted 17 lines · Ilya Matveychikov
Olivier,
Quoted 15 lines · Olivier Matz
lib/librte_mbuf/rte_mbuf.h| 23 ++++++++++++++++++-----1 file changed, 18 insertions(+), 5 deletions(-)lib/librte_mbuf/rte_mbuf.h +0
-0@@ -764,6 +764,13 @@ rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)rte_atomic16_set (&m->refcnt_atomic, new_value); }
Quoted 7 lines
lib/librte_mbuf/rte_mbuf.h rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value) · line 771
Quoted 5 lines
lib/librte_mbuf/rte_mbuf.h +5
-0+/* internal */ +static inline uint16_t +__rte_mbuf_refcnt_update (struct rte_mbuf *m,int16_t value) +{ +return (uint16_t )(rte_atomic16_add_return (&m->refcnt_atomic, value));What’s the purpose of using direct cast to
uint16_there and in other places?
This is just a code move.
Few years ago, I remember that icc was quite quick to trigger warnings when doing implicit casts. I don't know it it's still true, but that may be the reason why this was done like this initially, or not.
I agree we could remove this explicit cast, but I think it should go in another patch, because there are several of them.
Olivier
Re: [PATCH v4] mbuf: fix mbuf free performance with non atomic refcnt
In reply to Olivier Matzlib/librte_mbuf/rte_mbuf.h rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value) · line 772
Quoted 6 lines · On Fri, 8 Dec 2017 16:46:51 +0100 Olivier Matz <[email protected]> wrote:
lib/librte_mbuf/rte_mbuf.h +6
-0+/* internal */ +static inline uint16_t +__rte_mbuf_refcnt_update (struct rte_mbuf *m,int16_t value) +{ +return (uint16_t )(rte_atomic16_add_return (&m->refcnt_atomic, value)); +}
You don't need the cast (or paren's around rte_atomic16_addr_return) because C has implicit cast to return value.
Re: [PATCH v4] mbuf: fix mbuf free performance with non atomic refcnt
In reply to Olivier MatzOliver, Looks great.
Thanks,
Hanoh
-----Original Message-----
From: Olivier Matz [mailto:[email protected]]
Sent: Friday, December 08, 2017 5:47 PM
To: [email protected]; Hanoch Haim (hhaim)
Cc: [email protected]; [email protected]
Subject: [PATCH v4] mbuf: fix mbuf free performance with non atomic refcnt
When RTE_MBUF_REFCNT_ATOMIC=n, the decrement of the mbuf reference counter uses an atomic operation. This is not necessary and impacts the performance (seen with TRex traffic generator).
We cannot replace rte_atomic16_add_return() by rte_mbuf_refcnt_update() because it would add an additional check.
Solves this by introducing __rte_mbuf_refcnt_update(), which updates the reference counter without doing anything else.
---
Hanoh,
The following patch implements what was discussed in the thread. Are you ok with it?
Thanks,
Olivier
lib/librte_mbuf/rte_mbuf.h | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-)
lib/librte_mbuf/rte_mbuf.h +16 -5
@@ -764,6 +764,13 @@ rte_mbuf_refcnt_set(struct rte_mbuf *m, uint16_t new_value)rte_atomic16_set (&m->refcnt_atomic, new_value); } +/* internal */ +static inline uint16_t +__rte_mbuf_refcnt_update (struct rte_mbuf *m,int16_t value) { +return (uint16_t )(rte_atomic16_add_return (&m->refcnt_atomic, value)); +} +/** * Adds given value to an mbuf's refcnt and returns its new value. * @param m
@@ -788,19 +795,26 @@ rte_mbuf_refcnt_update(struct rte_mbuf *m, int16_t value) return 1 + value; } -return (uint16_t )(rte_atomic16_add_return (&m->refcnt_atomic, value)); +return __rte_mbuf_refcnt_update (m, value); }#else /* ! RTE_MBUF_REFCNT_ATOMIC */ +/* internal */ +static inline uint16_t +__rte_mbuf_refcnt_update (struct rte_mbuf *m,int16_t value) { + m->refcnt = (uint16_t )(m->refcnt + value); +return m->refcnt; +} +/** * Adds given value to an mbuf's refcnt and returns its new value. */ static inline uint16_t rte_mbuf_refcnt_update (struct rte_mbuf *m,int16_t value) { - m->refcnt = (uint16_t )(m->refcnt + value); -return m->refcnt; +return __rte_mbuf_refcnt_update (m, value); }/**
@@ -1364,8 +1378,7 @@ rte_pktmbuf_prefree_seg(struct rte_mbuf *m)return m; - }else if (rte_atomic16_add_return (&m->refcnt_atomic,-1 ) ==0 ) { - + }else if (__rte_mbuf_refcnt_update (m,-1 ) ==0 ) {if (RTE_MBUF_INDIRECT (m))rte_pktmbuf_detach (m);
--
2.11.0
Re: [PATCH v4] mbuf: fix mbuf free performance with non atomic refcnt
In reply to Olivier MatzQuoted 7 lines · Olivier Matz
When RTE_MBUF_REFCNT_ATOMIC=n, the decrement of the mbuf reference counter uses an atomic operation. This is not necessary and impacts the performance (seen with TRex traffic generator).
We cannot replace
rte_atomic16_add_return()byrte_mbuf_refcnt_update()because it would add an additional check.
Quoted 6 lines
Solves this by introducing
__rte_mbuf_refcnt_update(), which updates the reference counter without doing anything else.Fixes 8f094a9ac5d7 ("mbuf: set mbuf fields while in pool")Suggested-by Hanoch Haim <[email protected]>Signed-off-by Olivier Matz <[email protected]>
Forgot to Cc [email protected]
Thomas Monjalon
AppliedRe: [PATCH v4] mbuf: fix mbuf free performance with non atomic refcnt
In reply to Olivier Matz08/12/2017 16:46, Olivier Matz:
Quoted 7 lines
When RTE_MBUF_REFCNT_ATOMIC=n, the decrement of the mbuf reference counter uses an atomic operation. This is not necessary and impacts the performance (seen with TRex traffic generator).
We cannot replace
rte_atomic16_add_return()byrte_mbuf_refcnt_update()because it would add an additional check.
Quoted 6 lines
Solves this by introducing
__rte_mbuf_refcnt_update(), which updates the reference counter without doing anything else.Fixes 8f094a9ac5d7 ("mbuf: set mbuf fields while in pool")Suggested-by Hanoch Haim <[email protected]>Signed-off-by Olivier Matz <[email protected]>
Applied, thanks