[dpdk-dev,v3] mbuf: decrease refcnt when detaching
Commit Message
The rte_pktmbuf_detach() function should decrease refcnt on a direct
buffer.
Signed-off-by: Hiroyuki Mikita <h.mikita89@gmail.com>
---
v3:
* fixed rte_pktmbuf_detach() to decrease refcnt.
* free the direct mbuf when refcnt becomes 0.
* added this issue to Resolved Issues in release notes.
v2:
* introduced a new function rte_pktmbuf_detach2() which decrease refcnt.
* marked rte_pktmbuf_detach() as deprecated.
* added comments about refcnt to rte_pktmbuf_attach() and rte_pktmbuf_detach().
* checked refcnt when detaching in unit tests.
* added this issue to release notes.
app/test/test_mbuf.c | 4 ++++
doc/guides/rel_notes/release_16_07.rst | 6 ++++++
lib/librte_mbuf/rte_mbuf.h | 17 ++++++++++-------
3 files changed, 20 insertions(+), 7 deletions(-)
Comments
Hi Hiroyuki,
Thanks for submitting a new version.
There are some styling issues in the patch, I highlighted them below
but you can check them by using checkpatch:
DPDK_CHECKPATCH_PATH=/path/to/linux/checkpatch.pl \
scripts/checkpatches.sh file.patch
On 05/17/2016 06:35 PM, Hiroyuki Mikita wrote:
> The rte_pktmbuf_detach() function should decrease refcnt on a direct
> buffer.
>
> Signed-off-by: Hiroyuki Mikita <h.mikita89@gmail.com>
>
> [...]
>
> diff --git a/lib/librte_mbuf/rte_mbuf.h b/lib/librte_mbuf/rte_mbuf.h
> index 529debb..299b60e 100644
> --- a/lib/librte_mbuf/rte_mbuf.h
> +++ b/lib/librte_mbuf/rte_mbuf.h
> @@ -1408,6 +1408,8 @@ static inline int rte_pktmbuf_alloc_bulk(struct rte_mempool *pool,
> *
> * After attachment we refer the mbuf we attached as 'indirect',
> * while mbuf we attached to as 'direct'.
> + * The direct mbuf's reference counter is incremented.
> + *
ERROR:TRAILING_WHITESPACE: trailing whitespace
#82: FILE: lib/librte_mbuf/rte_mbuf.h:1412:
+ * $
> * Right now, not supported:
> * - attachment for already indirect mbuf (e.g. - mi has to be direct).
> * - mbuf we trying to attach (mi) is used by someone else
> @@ -1462,12 +1464,15 @@ static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *m)
> * - restore original mbuf address and length values.
> * - reset pktmbuf data and data_len to their default values.
> * All other fields of the given packet mbuf will be left intact.
> + * - decrement the direct mbuf's reference counter.
> + * When the reference counter becomes 0, the direct mbuf is freed.
Minor comment here: I think something like that would be clearer:
* - restore original mbuf address and length values.
* - reset pktmbuf data and data_len to their default values.
* - decrement the direct mbuf's reference counter. When the
* reference counter becomes 0, the direct mbuf is freed.
*
* All other fields of the given packet mbuf will be left intact.
> *
> * @param m
> * The indirect attached packet mbuf.
> */
> static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
> {
> + struct rte_mbuf *md = rte_mbuf_from_indirect(m);
> struct rte_mempool *mp = m->pool;
> uint32_t mbuf_size, buf_len, priv_size;
>
> @@ -1482,6 +1487,10 @@ static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
> m->data_off = RTE_MIN(RTE_PKTMBUF_HEADROOM, (uint16_t)m->buf_len);
> m->data_len = 0;
> m->ol_flags = 0;
> +
> + if (rte_mbuf_refcnt_update(md, -1) == 0) {
> + __rte_mbuf_raw_free(md);
> + }
> }
WARNING:BRACES: braces {} are not necessary for single statement blocks
#107: FILE: lib/librte_mbuf/rte_mbuf.h:1491:
+ if (rte_mbuf_refcnt_update(md, -1) == 0) {
+ __rte_mbuf_raw_free(md);
+ }
>
> static inline struct rte_mbuf* __attribute__((always_inline))
> @@ -1491,15 +1500,9 @@ __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
>
> if (likely(rte_mbuf_refcnt_update(m, -1) == 0)) {
>
> - /* if this is an indirect mbuf, then
> - * - detach mbuf
> - * - free attached mbuf segment
> - */
> + /* if this is an indirect mbuf, it is detached. */
> if (RTE_MBUF_INDIRECT(m)) {
> - struct rte_mbuf *md = rte_mbuf_from_indirect(m);
> rte_pktmbuf_detach(m);
> - if (rte_mbuf_refcnt_update(md, -1) == 0)
> - __rte_mbuf_raw_free(md);
> }
> return m;
> }
>
It's not seen by checkpatch, but the braces could also be removed
here.
Apart from this, it looks good to me. I'm ok with not providing a
compat function as we could consider it was a bug.
Thanks for working on this.
Olivier
Hi Olivier,
Thanks for reviewing.
I am fixing the patch to follow your comments.
Regards,
Hiroyuki
2016-05-18 20:58 GMT+09:00 Olivier Matz <olivier.matz@6wind.com>:
> Hi Hiroyuki,
>
> Thanks for submitting a new version.
>
> There are some styling issues in the patch, I highlighted them below
> but you can check them by using checkpatch:
>
> DPDK_CHECKPATCH_PATH=/path/to/linux/checkpatch.pl \
> scripts/checkpatches.sh file.patch
>
>
> On 05/17/2016 06:35 PM, Hiroyuki Mikita wrote:
>> The rte_pktmbuf_detach() function should decrease refcnt on a direct
>> buffer.
>>
>> Signed-off-by: Hiroyuki Mikita <h.mikita89@gmail.com>
>>
>> [...]
>>
>> diff --git a/lib/librte_mbuf/rte_mbuf.h b/lib/librte_mbuf/rte_mbuf.h
>> index 529debb..299b60e 100644
>> --- a/lib/librte_mbuf/rte_mbuf.h
>> +++ b/lib/librte_mbuf/rte_mbuf.h
>> @@ -1408,6 +1408,8 @@ static inline int rte_pktmbuf_alloc_bulk(struct rte_mempool *pool,
>> *
>> * After attachment we refer the mbuf we attached as 'indirect',
>> * while mbuf we attached to as 'direct'.
>> + * The direct mbuf's reference counter is incremented.
>> + *
>
> ERROR:TRAILING_WHITESPACE: trailing whitespace
> #82: FILE: lib/librte_mbuf/rte_mbuf.h:1412:
> + * $
>
>
>> * Right now, not supported:
>> * - attachment for already indirect mbuf (e.g. - mi has to be direct).
>> * - mbuf we trying to attach (mi) is used by someone else
>> @@ -1462,12 +1464,15 @@ static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *m)
>> * - restore original mbuf address and length values.
>> * - reset pktmbuf data and data_len to their default values.
>> * All other fields of the given packet mbuf will be left intact.
>> + * - decrement the direct mbuf's reference counter.
>> + * When the reference counter becomes 0, the direct mbuf is freed.
>
> Minor comment here: I think something like that would be clearer:
>
> * - restore original mbuf address and length values.
> * - reset pktmbuf data and data_len to their default values.
> * - decrement the direct mbuf's reference counter. When the
> * reference counter becomes 0, the direct mbuf is freed.
> *
> * All other fields of the given packet mbuf will be left intact.
>
>
>> *
>> * @param m
>> * The indirect attached packet mbuf.
>> */
>> static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
>> {
>> + struct rte_mbuf *md = rte_mbuf_from_indirect(m);
>> struct rte_mempool *mp = m->pool;
>> uint32_t mbuf_size, buf_len, priv_size;
>>
>> @@ -1482,6 +1487,10 @@ static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
>> m->data_off = RTE_MIN(RTE_PKTMBUF_HEADROOM, (uint16_t)m->buf_len);
>> m->data_len = 0;
>> m->ol_flags = 0;
>> +
>> + if (rte_mbuf_refcnt_update(md, -1) == 0) {
>> + __rte_mbuf_raw_free(md);
>> + }
>> }
>
> WARNING:BRACES: braces {} are not necessary for single statement blocks
> #107: FILE: lib/librte_mbuf/rte_mbuf.h:1491:
> + if (rte_mbuf_refcnt_update(md, -1) == 0) {
> + __rte_mbuf_raw_free(md);
> + }
>
>
>>
>> static inline struct rte_mbuf* __attribute__((always_inline))
>> @@ -1491,15 +1500,9 @@ __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
>>
>> if (likely(rte_mbuf_refcnt_update(m, -1) == 0)) {
>>
>> - /* if this is an indirect mbuf, then
>> - * - detach mbuf
>> - * - free attached mbuf segment
>> - */
>> + /* if this is an indirect mbuf, it is detached. */
>> if (RTE_MBUF_INDIRECT(m)) {
>> - struct rte_mbuf *md = rte_mbuf_from_indirect(m);
>> rte_pktmbuf_detach(m);
>> - if (rte_mbuf_refcnt_update(md, -1) == 0)
>> - __rte_mbuf_raw_free(md);
>> }
>> return m;
>> }
>>
>
> It's not seen by checkpatch, but the braces could also be removed
> here.
>
> Apart from this, it looks good to me. I'm ok with not providing a
> compat function as we could consider it was a bug.
>
> Thanks for working on this.
>
> Olivier
>
@@ -511,10 +511,14 @@ test_attach_from_different_pool(void)
rte_pktmbuf_detach(clone);
if (c_data != rte_pktmbuf_mtod(clone, char *))
GOTO_FAIL("clone was not detached properly\n");
+ if (rte_mbuf_refcnt_read(m) != 2)
+ GOTO_FAIL("invalid refcnt in m\n");
rte_pktmbuf_detach(clone2);
if (c_data2 != rte_pktmbuf_mtod(clone2, char *))
GOTO_FAIL("clone2 was not detached properly\n");
+ if (rte_mbuf_refcnt_read(m) != 1)
+ GOTO_FAIL("invalid refcnt in m\n");
/* free the clones and the initial mbuf */
rte_pktmbuf_free(clone2);
@@ -65,6 +65,12 @@ Drivers
Libraries
~~~~~~~~~
+* **mbuf: Fixed refcnt update when detaching.**
+
+ Fix the ``rte_pktmbuf_detach()`` function to decrement the direct
+ mbuf's reference counter. The previous behavior was not to affect
+ the reference counter. It lead a memory leak of the direct mbuf.
+
Examples
~~~~~~~~
@@ -1408,6 +1408,8 @@ static inline int rte_pktmbuf_alloc_bulk(struct rte_mempool *pool,
*
* After attachment we refer the mbuf we attached as 'indirect',
* while mbuf we attached to as 'direct'.
+ * The direct mbuf's reference counter is incremented.
+ *
* Right now, not supported:
* - attachment for already indirect mbuf (e.g. - mi has to be direct).
* - mbuf we trying to attach (mi) is used by someone else
@@ -1462,12 +1464,15 @@ static inline void rte_pktmbuf_attach(struct rte_mbuf *mi, struct rte_mbuf *m)
* - restore original mbuf address and length values.
* - reset pktmbuf data and data_len to their default values.
* All other fields of the given packet mbuf will be left intact.
+ * - decrement the direct mbuf's reference counter.
+ * When the reference counter becomes 0, the direct mbuf is freed.
*
* @param m
* The indirect attached packet mbuf.
*/
static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
{
+ struct rte_mbuf *md = rte_mbuf_from_indirect(m);
struct rte_mempool *mp = m->pool;
uint32_t mbuf_size, buf_len, priv_size;
@@ -1482,6 +1487,10 @@ static inline void rte_pktmbuf_detach(struct rte_mbuf *m)
m->data_off = RTE_MIN(RTE_PKTMBUF_HEADROOM, (uint16_t)m->buf_len);
m->data_len = 0;
m->ol_flags = 0;
+
+ if (rte_mbuf_refcnt_update(md, -1) == 0) {
+ __rte_mbuf_raw_free(md);
+ }
}
static inline struct rte_mbuf* __attribute__((always_inline))
@@ -1491,15 +1500,9 @@ __rte_pktmbuf_prefree_seg(struct rte_mbuf *m)
if (likely(rte_mbuf_refcnt_update(m, -1) == 0)) {
- /* if this is an indirect mbuf, then
- * - detach mbuf
- * - free attached mbuf segment
- */
+ /* if this is an indirect mbuf, it is detached. */
if (RTE_MBUF_INDIRECT(m)) {
- struct rte_mbuf *md = rte_mbuf_from_indirect(m);
rte_pktmbuf_detach(m);
- if (rte_mbuf_refcnt_update(md, -1) == 0)
- __rte_mbuf_raw_free(md);
}
return m;
}