From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf0-f196.google.com (mail-pf0-f196.google.com [209.85.192.196]) by dpdk.org (Postfix) with ESMTP id 965FC69A4 for ; Tue, 17 May 2016 18:35:14 +0200 (CEST) Received: by mail-pf0-f196.google.com with SMTP id g132so2402558pfb.3 for ; Tue, 17 May 2016 09:35:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=t78/dBlDJfXo2mZojAfSBDFVlWDCE7xWJtl02+NyKnU=; b=YRU275SYEw6DHlwDOM7pC3AqhtitNct8O3MHpyktBJsG1k5rNmMrR9wia8qE5f4Ke5 eTd8Kwb6PvrwXyZwFu/vMuHVJuxA3wSvlL5XN2BHGpg0u7gWgAqnEWXW4cJTa9Irdick 2pjH+fOyb4nGlC3yv60KxB8Pv8dSugLu712DkavKq82Q8vgYgDMvXA5s1Kub8if2T9eZ XSoYJCw5YOeVFq1n+RLyBNzEIrmF/2QdQpIuH9oSMZ93o0be7ipDNH2TdfGR51LsevJc b/fBTFkxXcH9bD1hupVW6Next9oHKfhK3+Pqem6ErDiFrGq2Rv6WB9yjY6a7+1+gpfLa 7/RQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=t78/dBlDJfXo2mZojAfSBDFVlWDCE7xWJtl02+NyKnU=; b=ZqRwZBEhwle3gyPatT7lyLq2cIvPciWMKqxX8VqBOlcfbb4DHF84qdzFEWrAbgxsYz BwmafSaD+40C63iPQiOb1hMh8eXdSo2IprcgJS+SfFaHdOjJh2aItYo3C+4qTXqk6enT K89GKTG5pnj8ykdLahEwxVTzUri7dE/FaIA0qfAKn/XAFaPVWz7QL2cakSlM9d37WFvH g9g3aAiQ6MKyyJ/x0bVUL/Rw8p9vbWxQUJQAATU7QEpqlTuPa/IJtxycXfMRtloang/2 DwsY0OgqVcaD1bItncGCTMS+W5A+Fp4287yPpTK5mnbKJnaZRvaJM63tGZmC+Sv1xMeU 75FQ== X-Gm-Message-State: AOPr4FVfzAtq6Fvs5ztx566+3VYLMliSymRqtv3m2ViAbLm84h9zeI/sKZ0Un2wdng/54Q== X-Received: by 10.98.67.7 with SMTP id q7mr3498894pfa.70.1463502913982; Tue, 17 May 2016 09:35:13 -0700 (PDT) Received: from localhost.localdomain (183.180.67.214.ap.gmobb-fix.jp. [183.180.67.214]) by smtp.gmail.com with ESMTPSA id h5sm6012491pat.0.2016.05.17.09.35.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 17 May 2016 09:35:13 -0700 (PDT) From: Hiroyuki Mikita To: olivier.matz@6wind.com, thomas.monjalon@6wind.com, konstantin.ananyev@intel.com Cc: dev@dpdk.org Date: Wed, 18 May 2016 01:35:02 +0900 Message-Id: <1463502902-5295-1-git-send-email-h.mikita89@gmail.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1463417600-20943-1-git-send-email-h.mikita89@gmail.com> References: <1463417600-20943-1-git-send-email-h.mikita89@gmail.com> Subject: [dpdk-dev] [PATCH v3] mbuf: decrease refcnt when detaching X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 17 May 2016 16:35:14 -0000 The rte_pktmbuf_detach() function should decrease refcnt on a direct buffer. Signed-off-by: Hiroyuki Mikita --- 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(-) diff --git a/app/test/test_mbuf.c b/app/test/test_mbuf.c index 98ff93a..8460db7 100644 --- a/app/test/test_mbuf.c +++ b/app/test/test_mbuf.c @@ -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); diff --git a/doc/guides/rel_notes/release_16_07.rst b/doc/guides/rel_notes/release_16_07.rst index f6d543c..8283373 100644 --- a/doc/guides/rel_notes/release_16_07.rst +++ b/doc/guides/rel_notes/release_16_07.rst @@ -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 ~~~~~~~~ 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. + * * 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; } -- 1.9.1