Skip to contentSkip to conversation

mbuf: optimize rte_mbuf_refcnt_update

PATCH Hanoch Haim (hhaim) 3 participants 9 messages Dec 27, 2015 – Jan 13, 2016

lore
Share & tools
lore

Hanoch Haim (hhaim)

author Acked-by Bruce Richardson
#0 lore raw

Hi Bruce,

I'm Hanoch from Cisco Systems works for the https://github.com/cisco-system-traffic-generator/trex-core traffic generator project.

While upgrading from DPDK 1.8 to 2.2 Ido found that the following commit creates a mbuf corruption and result in Tx hang

commit f20b50b946da9070d21e392e4dbc7d9f68bc983e

Author: Olivier Matz <[email protected]>

Date: Mon Jun 8 16:57:22 2015 +0200

Looking at the change it is clear why there is an issue, wanted to get your input.

Init

-----

alloc const mbuf ==> mbuf-a (ref=1)

Simple case that works

---------------------

thread 1 , tx: alloc-mbuf->attach(mbuf-a) (ref=2) inc- non atomic

thread 1 , tx: alloc-mbuf->attach(mbuf-a) (ref32) inc- atomic

thread 1 , drv : free() (ref=2) dec- atomic

thread 1 , drv : free() (ref=3) dec - non atomic

Simple case that does not work

---------------------

Both do that in parallel

thread 2 tx : alloc-mbuf->attach(mbuf-a) (ref=2) inc- non atomic

thread 1 tx : alloc-mbuf->attach(mbuf-a) (ref=2) inc- non atomic

==> ref in corrupted

thread 1 drv : free() (ref=0) - free

thread 1 drv : free() (ref=-1) - ???

Just as a side note that in our case we could benefit from this optimization by making a small change in our code, as our common case could be alloc/free of simple mbuf and in such scenario there is no atomic operation in both cases. But I think the general case is broken.

Could you explain what was your use case or this optimization? Is it a valid example the aforementioned example

thanks,

Hanoh

Cisco Systems

Quoted 12 lines · Olivier Matz

In __rte_pktmbuf_prefree_seg(), there was an optimization to avoid using

a costly atomic operation when updating the mbuf reference counter if

its value is 1. Indeed, it means that we are the only owner of the mbuf,

and therefore nobody can change it at the same time.

We can generalize this optimization directly in rte_mbuf_refcnt_update()

Quoted 7 lines

so the other callers of this function, like rte_pktmbuf_attach(), can

also take advantage of this optimization.

Signed-off-by Olivier Matz <olivier.matz at 6wind.com<http://dpdk.org/ml/listinfo/dev>>
Acked-by Bruce Richardson <bruce.richardson at intel.com<http://dpdk.org/ml/listinfo/dev>>

Hanoh

#1 lore raw

Hi Hanoch,

Please find some comments below.

Quoted 30 lines · Hanoch Haim (hhaim)

Hi Bruce,

I'm Hanoch from Cisco Systems works for the https://github.com/cisco-system-traffic-generator/trex-core traffic generator project.

While upgrading from DPDK 1.8 to 2.2 Ido found that the following commit creates a mbuf corruption and result in Tx hang

commit f20b50b946da9070d21e392e4dbc7d9f68bc983e

Author: Olivier Matz <[email protected]>

Date: Mon Jun 8 16:57:22 2015 +0200

Looking at the change it is clear why there is an issue, wanted to get your input.

Init

-----

alloc const mbuf ==> mbuf-a (ref=1)

Simple case that works

Quoted 7 lines

---------------------

thread 1 , tx: alloc-mbuf->attach(mbuf-a) (ref=2) inc- non atomic

thread 1 , tx: alloc-mbuf->attach(mbuf-a) (ref32) inc- atomic

do you mean "(ref=3)" ?

Quoted 4 lines

thread 1 , drv : free() (ref=2) dec- atomic

thread 1 , drv : free() (ref=3) dec - non atomic

do you mean "(ref=1)" ?

Quoted 7 lines

Simple case that does not work

---------------------

Quoted 7 lines

Both do that in parallel

thread 2 tx : alloc-mbuf->attach(mbuf-a) (ref=2) inc- non atomic

thread 1 tx : alloc-mbuf->attach(mbuf-a) (ref=2) inc- non atomic

It is not allowed to call a function from the mbuf API in parallel.

Example:

	core0			|	core1
--------------------------------|---------------------------------------
m = rte_pktmbuf_alloc(m);	|
enqueue(m);			|
				|m = dequeue();

do_something(m); |do_something(m);

do_something() is not allowed because it accesses the same mbuf structure.
do_something() can be any function of mbuf API: rte_pktmbuf_prepend(), rte_pktmbuf_attach(), ...

This is allowed:

	core0			|	core1

--------------------------------|---------------------------------------
m = rte_pktmbuf_alloc(m); |
m2 = rte_pktmbuf_attach(m); |
enqueue(m2); |

				|m2 = dequeue();

do_something(m); |do_something(m2);

Regards,
Olivier

#2 lore raw

Hi Oliver,

Let's take your drawing as a reference and add my question
The use case is sending a duplicate multicast packet by many threads.
I can split it to x threads to do the job and with atomic-ref (my multicast not mbuf) count it until it reaches zero.

In my following example the two cores (0 and 1) sending the indirect m1/m2 do alloc/attach/send

core0 | core1
--------------------------------- |---------------------------------------
m_const=rte_pktmbuf_alloc(mp) |

                                                                  |

while true: | while True:
m1 =rte_pktmbuf_alloc(mp_64) | m2 =rte_pktmbuf_alloc(mp_64)
rte_pktmbuf_attach(m1, m_const) | rte_pktmbuf_attach(m1, m_const)
tx_burst(m1) | tx_burst(m2)

Is this example is not valid?

BTW this is our workaround

core0 | core1
--------------------------------- |---------------------------------------
m_const=rte_pktmbuf_alloc(mp) |
rte_mbuf_refcnt_update(m_const,1)| <<-- workaround

                                                           |

while true: | while True:
m1 =rte_pktmbuf_alloc(mp_64) | m2 =rte_pktmbuf_alloc(mp_64)
rte_pktmbuf_attach(m1, m_const) | rte_pktmbuf_attach(m1, m_const)
tx_burst(m1) | tx_burst(m2)

thanks,
Hanoh

-----Original Message-----
From: Olivier MATZ [mailto:[email protected]]
Sent: Monday, January 04, 2016 3:53 PM
To: Hanoch Haim (hhaim); [email protected]
Cc: [email protected]; Ido Barnea (ibarnea); Itay Marom (imarom)
Subject: Re: [dpdk-dev] [PATCH v2] mbuf: optimize rte_mbuf_refcnt_update

Hi Hanoch,

Please find some comments below.

Quoted 31 lines · Hanoch Haim (hhaim)

Hi Bruce,

I'm Hanoch from Cisco Systems works for the https://github.com/cisco-system-traffic-generator/trex-core traffic generator project.

While upgrading from DPDK 1.8 to 2.2 Ido found that the following commit creates a mbuf corruption and result in Tx hang

commit f20b50b946da9070d21e392e4dbc7d9f68bc983e

Author: Olivier Matz <[email protected]>

Date: Mon Jun 8 16:57:22 2015 +0200

Looking at the change it is clear why there is an issue, wanted to get your input.

Init

-----

alloc const mbuf ==> mbuf-a (ref=1)

Simple case that works

Quoted 7 lines

---------------------

thread 1 , tx: alloc-mbuf->attach(mbuf-a) (ref=2) inc- non atomic

thread 1 , tx: alloc-mbuf->attach(mbuf-a) (ref32) inc- atomic

do you mean "(ref=3)" ?
[hh] yes ref=3.

Quoted 4 lines

thread 1 , drv : free() (ref=2) dec- atomic

thread 1 , drv : free() (ref=3) dec - non atomic

do you mean "(ref=1)" ?

Quoted 7 lines

Simple case that does not work

---------------------

Quoted 7 lines

Both do that in parallel

thread 2 tx : alloc-mbuf->attach(mbuf-a) (ref=2) inc- non atomic

thread 1 tx : alloc-mbuf->attach(mbuf-a) (ref=2) inc- non atomic

It is not allowed to call a function from the mbuf API in parallel.

Example:

	core0			|	core1
--------------------------------|---------------------------------------
m = rte_pktmbuf_alloc(m);	|
enqueue(m);			|
				|m = dequeue();

do_something(m); |do_something(m);

do_something() is not allowed because it accesses the same mbuf structure. do_something() can be any function of mbuf API: rte_pktmbuf_prepend(), rte_pktmbuf_attach(), ...

This is allowed:

	core0			|	core1

--------------------------------|---------------------------------------
m = rte_pktmbuf_alloc(m); |
m2 = rte_pktmbuf_attach(m); |
enqueue(m2); |

				|m2 = dequeue();

do_something(m); |do_something(m2);

Regards,
Olivier

#3 lore raw

Hi Hanoch,

Quoted 11 lines · Hanoch Haim (hhaim)

Hi Oliver,

Let's take your drawing as a reference and add my question
The use case is sending a duplicate multicast packet by many threads.
I can split it to x threads to do the job and with atomic-ref (my multicast not mbuf) count it until it reaches zero.

In my following example the two cores (0 and 1) sending the indirect m1/m2 do alloc/attach/send

core0 | core1
--------------------------------- |---------------------------------------
m_const=rte_pktmbuf_alloc(mp) |

Quoted 7 lines
                                                                   |

while true: | while True:
m1 =rte_pktmbuf_alloc(mp_64) | m2 =rte_pktmbuf_alloc(mp_64)
rte_pktmbuf_attach(m1, m_const) | rte_pktmbuf_attach(m1, m_const)
tx_burst(m1) | tx_burst(m2)

Is this example is not valid?

For me, m_const is not expected to be used concurrently on several cores. By "used", I mean calling a function that modifies the mbuf, which is the case for rte_pktmbuf_attach().

Quoted 7 lines

BTW this is our workaround

core0 | core1
--------------------------------- |---------------------------------------
m_const=rte_pktmbuf_alloc(mp) |
rte_mbuf_refcnt_update(m_const,1)| <<-- workaround

Quoted 5 lines
                                                            |

while true: | while True:
m1 =rte_pktmbuf_alloc(mp_64) | m2 =rte_pktmbuf_alloc(mp_64)
rte_pktmbuf_attach(m1, m_const) | rte_pktmbuf_attach(m1, m_const)
tx_burst(m1) | tx_burst(m2)

This workaround indeed solves the issue. Another solution would be to protect the call to attach() with a lock, or call all the rte_pktmbuf_attach() on the same core.

I'm open to discuss this behavior for rte_pktmbuf_attach() function (should concurrent calls be allowed or not). In any case, we may want to better document it in the doxygen API comments.

Regards,
Olivier

#4 lore raw

Hi Oliver,
Thank you for the fast response and it would be great to open a discussion on that.
In general our project can leverage your optimization and I think it is great (we should have thought about it) . We can use it using the workaround I described. However, for me it seems odd that rte_pktmbuf_attach () that does not *change* anything in m_const, except of the *atomic* ref counter does not work in parallel. The example I gave is a classic use case of rte_pktmbuf_attach (multicast ) and I don't see why it wouldn't work after your optimization.

Do you have a pointer to the documentation that state that that you can't call the atomic ref counter from more than one thread?

Thanks,
Hanoh

-----Original Message-----
From: Olivier MATZ [mailto:[email protected]]
Sent: Tuesday, January 05, 2016 12:58 PM
To: Hanoch Haim (hhaim); [email protected]
Cc: [email protected]; Ido Barnea (ibarnea); Itay Marom (imarom)
Subject: Re: [dpdk-dev] [PATCH v2] mbuf: optimize rte_mbuf_refcnt_update

Hi Hanoch,

Quoted 12 lines · Hanoch Haim (hhaim)

Hi Oliver,

Let's take your drawing as a reference and add my question The use
case is sending a duplicate multicast packet by many threads.
I can split it to x threads to do the job and with atomic-ref (my multicast not mbuf) count it until it reaches zero.

In my following example the two cores (0 and 1) sending the indirect m1/m2 do alloc/attach/send

core0 | core1
--------------------------------- |---------------------------------------
m_const=rte_pktmbuf_alloc(mp) |

Quoted 7 lines
                                                                   |

while true: | while True:
m1 =rte_pktmbuf_alloc(mp_64) | m2 =rte_pktmbuf_alloc(mp_64)
rte_pktmbuf_attach(m1, m_const) | rte_pktmbuf_attach(m1, m_const)
tx_burst(m1) | tx_burst(m2)

Is this example is not valid?

For me, m_const is not expected to be used concurrently on several cores. By "used", I mean calling a function that modifies the mbuf, which is the case for rte_pktmbuf_attach().

Quoted 7 lines

BTW this is our workaround

core0 | core1
--------------------------------- |---------------------------------------
m_const=rte_pktmbuf_alloc(mp) |
rte_mbuf_refcnt_update(m_const,1)| <<-- workaround

Quoted 5 lines
                                                            |

while true: | while True:
m1 =rte_pktmbuf_alloc(mp_64) | m2 =rte_pktmbuf_alloc(mp_64)
rte_pktmbuf_attach(m1, m_const) | rte_pktmbuf_attach(m1, m_const)
tx_burst(m1) | tx_burst(m2)

This workaround indeed solves the issue. Another solution would be to protect the call to attach() with a lock, or call all the rte_pktmbuf_attach() on the same core.

I'm open to discuss this behavior for rte_pktmbuf_attach() function (should concurrent calls be allowed or not). In any case, we may want to better document it in the doxygen API comments.

Regards,
Olivier

#5 lore raw

Hi Hanoch,

Quoted 7 lines · Hanoch Haim (hhaim)

Hi Oliver,
Thank you for the fast response and it would be great to open a discussion on that.
In general our project can leverage your optimization and I think it is great (we should have thought about it) . We can use it using the workaround I described. However, for me it seems odd that rte_pktmbuf_attach () that does not *change* anything in m_const, except of the *atomic* ref counter does not work in parallel. The example I gave is a classic use case of rte_pktmbuf_attach (multicast ) and I don't see why it wouldn't work after your optimization.

Do you have a pointer to the documentation that state that that you can't call the atomic ref counter from more than one thread?

Unfortunately it's not documented yet, but it's something we should better describe.

Regards,
Olivier

#6 lore raw
In reply to Hanoch Haim (hhaim)
Quoted 8 lines · Hanoch Haim (hhaim)

Hi Oliver,
Thank you for the fast response and it would be great to open a discussion on that.
In general our project can leverage your optimization and I think it is great (we should have thought about it) . We can use it using the workaround I described. However, for me it seems odd that rte_pktmbuf_attach () that does not *change* anything in m_const, except of the *atomic* ref counter does not work in parallel. The example I gave is a classic use case of rte_pktmbuf_attach (multicast ) and I don't see why it wouldn't work after your optimization.

Do you have a pointer to the documentation that state that that you can't call the atomic ref counter from more than one thread?

Hi,

actually, the issue is not that you can't work with the reference counter field from multiple threads, or that you can't use an mbuf from multiple threads, it's that if you are working with the same mbuf in multiple threads you have multiple references to the mbuf and your application must increase the reference counter appropriately. For example, if thread A is going to pass an mbuf to thread B and keep using it itself, you must increment the reference counter in thread A before enqueuing it to B.

Regards,
/Bruce

#7 lore raw

Hi Bruce.
This is exactly what was possible before the patch and does *not* work after this patch.
I think it is more related to the semantic of the API. My understanding of the API was that I could allocate a share mbuf (ref=1) and then attach it from many concurrent threads (as each thread attach opt inc in 1 and driver reduces, atomic by 1) After the patch there is a need to make the ref of the share mbuf to be 2 (why?) and free it twice at the end( ?), this makes it cumbersome to use it and for sure there is a need to describe this semantic change.

However, this change is good for the common case, and I think it is better to keep it. The options I see are:
1) Document the current behavior
2) Create two sets of API with two types of semantic

thanks,
Hanoh

-----Original Message-----
From: Bruce Richardson [mailto:[email protected]]
Sent: Wednesday, January 13, 2016 1:48 PM
To: Hanoch Haim (hhaim)
Cc: Olivier MATZ; [email protected]; Ido Barnea (ibarnea); Itay Marom (imarom)
Subject: Re: [dpdk-dev] [PATCH v2] mbuf: optimize rte_mbuf_refcnt_update

Quoted 8 lines · Hanoch Haim (hhaim)

Hi Oliver,
Thank you for the fast response and it would be great to open a discussion on that.
In general our project can leverage your optimization and I think it is great (we should have thought about it) . We can use it using the workaround I described. However, for me it seems odd that rte_pktmbuf_attach () that does not *change* anything in m_const, except of the *atomic* ref counter does not work in parallel. The example I gave is a classic use case of rte_pktmbuf_attach (multicast ) and I don't see why it wouldn't work after your optimization.

Do you have a pointer to the documentation that state that that you can't call the atomic ref counter from more than one thread?

Hi,

actually, the issue is not that you can't work with the reference counter field from multiple threads, or that you can't use an mbuf from multiple threads, it's that if you are working with the same mbuf in multiple threads you have multiple references to the mbuf and your application must increase the reference counter appropriately. For example, if thread A is going to pass an mbuf to thread B and keep using it itself, you must increment the reference counter in thread A before enqueuing it to B.

Regards,
/Bruce

#8 lore raw
Quoted 5 lines · Hanoch Haim (hhaim)

Hi Bruce.
This is exactly what was possible before the patch and does *not* work after this patch.
I think it is more related to the semantic of the API. My understanding of the API was that I could allocate a share mbuf (ref=1) and then attach it from many concurrent threads (as each thread attach opt inc in 1 and driver reduces, atomic by 1) After the patch there is a need to make the ref of the share mbuf to be 2 (why?) and free it twice at the end( ?), this makes it cumbersome to use it and for sure there is a need to describe this semantic change.

There is no such thing as allocating a shared mbuf.
Mbufs are always allocated with a reference count of one. To share an mbuf among threads, then the ref count must be increased.

NOTE: this is a different operation to that of "attaching" another mbuf to it - it just happens that when attaching one mbuf to another you have to increase the reference count as the attaching mbuf acts like another thread holding a pointer to the original mbuf.

Once finished with the mbuf, each thread (or pointing mbuf) needs to decrement the reference counter, and the last one holding the reference is then responsible for freeing that buffer. The easiest way to do this is to use the free call and let it handle the reference counts, but if it makes more logical sense in an app to do the decrement and checking itself (so that inc's and dec's match in the app code), then that can work too, obviously.

Quoted 4 lines

However, this change is good for the common case, and I think it is better to keep it. The options I see are:
1) Document the current behavior
2) Create two sets of API with two types of semantic

Given the confusion I think documenting the way things are "meant to work" is a good idea.

/Bruce

Quoted 27 lines

thanks,
Hanoh

-----Original Message-----
From: Bruce Richardson [mailto:[email protected]]
Sent: Wednesday, January 13, 2016 1:48 PM
To: Hanoch Haim (hhaim)
Cc: Olivier MATZ; [email protected]; Ido Barnea (ibarnea); Itay Marom (imarom)
Subject: Re: [dpdk-dev] [PATCH v2] mbuf: optimize rte_mbuf_refcnt_update

Quoted 8 lines · Hanoch Haim (hhaim)

Hi Oliver,
Thank you for the fast response and it would be great to open a discussion on that.
In general our project can leverage your optimization and I think it is great (we should have thought about it) . We can use it using the workaround I described. However, for me it seems odd that rte_pktmbuf_attach () that does not *change* anything in m_const, except of the *atomic* ref counter does not work in parallel. The example I gave is a classic use case of rte_pktmbuf_attach (multicast ) and I don't see why it wouldn't work after your optimization.

Do you have a pointer to the documentation that state that that you can't call the atomic ref counter from more than one thread?

Hi,

actually, the issue is not that you can't work with the reference counter field from multiple threads, or that you can't use an mbuf from multiple threads, it's that if you are working with the same mbuf in multiple threads you have multiple references to the mbuf and your application must increase the reference counter appropriately. For example, if thread A is going to pass an mbuf to thread B and keep using it itself, you must increment the reference counter in thread A before enqueuing it to B.

Regards,
/Bruce