Skip to contentSkip to conversation

mbuf: cleanup rte_pktmbuf_lastseg(), fix atomic usage

PATCH In review Hanoh Haim 6 participants 19 messages Nov 15, 2017 – Jan 18, 2018

lore
Share & tools
lore

Hanoh Haim

author
#0 lore raw

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
index 7e326bb..ab110f8 100644
@@ -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
#1 lore raw
Quoted 15 lines · Hanoh Haim

---
lib/librte_mbuf/rte_mbuf.h | 27 +++++++++++++--------------

1 file changed, 13 insertions(+), 14 deletions(-)
lib/librte_mbuf/rte_mbuf.h +0 -0
index 7e326bb..ab110f8 100644
@@ -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
#2 lore raw

+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

---
lib/librte_mbuf/rte_mbuf.h | 27 +++++++++++++--------------

1 file changed, 13 insertions(+), 14 deletions(-)
lib/librte_mbuf/rte_mbuf.h +0 -0
index 7e326bb..ab110f8 100644
@@ -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

#3 lore raw

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: cleanup rte_pktmbuf_lastseg(), fix atomic usage

Quoted 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:

Fixes 8f094a9ac5d7 ("mbuf: set mbuf fields while in pool")
Quoted 13 lines
Quoted 13 lines

---
lib/librte_mbuf/rte_mbuf.h | 27 +++++++++++++--------------

1 file changed, 13 insertions(+), 14 deletions(-)
lib/librte_mbuf/rte_mbuf.h +0 -0
index 7e326bb..ab110f8 100644
@@ -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 in rte_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 -2
 	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.

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

#4 lore raw

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: cleanup rte_pktmbuf_lastseg(), fix atomic usage

Quoted 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:

Fixes 8f094a9ac5d7 ("mbuf: set mbuf fields while in pool")
Quoted 13 lines
Quoted 13 lines

---
lib/librte_mbuf/rte_mbuf.h | 27 +++++++++++++--------------

1 file changed, 13 insertions(+), 14 deletions(-)
lib/librte_mbuf/rte_mbuf.h +0 -0
index 7e326bb..ab110f8 100644
@@ -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 in
rte_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 -2
 	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.

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

#5 lore raw
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 of RTE_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.

#6 lore raw
In reply to Hanoch Haim (hhaim)

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 of RTE_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

#7 lore raw

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 of RTE_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

#8 lore raw
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?

#9 lore raw
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_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?

Is there any other difference with rte_mbuf_refcnt_update_blind() except the function name?

#10 lore raw
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 over rte_atomic16_add_return() and use it in inside rte_mbuf_refcnt_update() and rte_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.

#11 lore raw
In reply to Olivier MATZ

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

#12 lore raw

PATCH v4 1/1 mbuf: fix mbuf free performance with non atomic refcnt

In reply to Hanoh Haim

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.

Fixes 8f094a9ac5d7 ("mbuf: set mbuf fields while in pool")

---

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
index ce8a05ddf..dd08cb72b 100644
@@ -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
#13 lore raw

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
index ce8a05ddf..dd08cb72b 100644

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_t rte_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
#14 lore raw

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
index ce8a05ddf..dd08cb72b 100644
@@ -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_t here 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

#15 lore raw

Re: [PATCH v4] mbuf: fix mbuf free performance with non atomic refcnt

In reply to Olivier Matz

lib/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.

#16 lore raw

Re: [PATCH v4] mbuf: fix mbuf free performance with non atomic refcnt

In reply to Olivier Matz

Oliver, 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.

Fixes 8f094a9ac5d7 ("mbuf: set mbuf fields while in pool")

---

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

#17 lore raw

Re: [PATCH v4] mbuf: fix mbuf free performance with non atomic refcnt

In reply to Olivier Matz
Quoted 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() by rte_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")

Forgot to Cc [email protected]

#18 lore raw

Re: [PATCH v4] mbuf: fix mbuf free performance with non atomic refcnt

In reply to Olivier Matz

08/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() by rte_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")

Applied, thanks