DPDK patches and discussions
 help / color / mirror / Atom feed
From: "Morten Brørup" <mb@smartsharesystems.com>
To: "Feifei Wang" <feifei.wang2@arm.com>
Cc: <dev@dpdk.org>, <nd@arm.com>, "Ruifeng Wang" <ruifeng.wang@arm.com>
Subject: RE: [PATCH v1] mbuf: remove the redundant code for mbuf prefree
Date: Tue, 5 Dec 2023 09:04:08 +0100	[thread overview]
Message-ID: <98CBD80474FA8B44BF855DF32C47DC35E9F08C@smartserver.smartshare.dk> (raw)
In-Reply-To: <19ea4729-bdfa-4ee3-83d6-5922c1b42c0a@arm.com>

> From: Feifei Wang [mailto:feifei.wang2@arm.com]
> Sent: Tuesday, 5 December 2023 04.13
> 
> 在 2023/12/4 15:41, Morten Brørup 写道:
> >> From: Feifei Wang [mailto:feifei.wang2@arm.com]
> >> Sent: Monday, 4 December 2023 03.39
> >>
> >> 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/20170404162807.20157-4-
> >> olivier.matz@6wind.com/
> >>
> >> Suggested-by: Ruifeng Wang <ruifeng.wang@arm.com>
> >> Signed-off-by: Feifei Wang <feifei.wang2@arm.com>
> >> ---
> >>   lib/mbuf/rte_mbuf.h | 22 +++-------------------
> >>   1 file changed, 3 insertions(+), 19 deletions(-)
> >>
> >> diff --git a/lib/mbuf/rte_mbuf.h b/lib/mbuf/rte_mbuf.h
> >> index 286b32b788..42e9b50d51 100644
> >> --- a/lib/mbuf/rte_mbuf.h
> >> +++ b/lib/mbuf/rte_mbuf.h
> >> @@ -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.
> 
> 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?

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))



  reply	other threads:[~2023-12-05  8:04 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-04  2:39 Feifei Wang
2023-12-04  7:41 ` Morten Brørup
2023-12-04 11:07   ` Konstantin Ananyev
2023-12-05 18:50     ` Stephen Hemminger
2023-12-06 10:12       ` Bruce Richardson
2023-12-06 10:21         ` Konstantin Ananyev
2023-12-05  3:13   ` Feifei Wang
2023-12-05  8:04     ` Morten Brørup [this message]
2023-12-05  9:53       ` Bruce Richardson

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=98CBD80474FA8B44BF855DF32C47DC35E9F08C@smartserver.smartshare.dk \
    --to=mb@smartsharesystems.com \
    --cc=dev@dpdk.org \
    --cc=feifei.wang2@arm.com \
    --cc=nd@arm.com \
    --cc=ruifeng.wang@arm.com \
    /path/to/YOUR_REPLY

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

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