From patchwork Fri Sep 22 15:00:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Github ODP bot X-Patchwork-Id: 114057 Delivered-To: patch@linaro.org Received: by 10.140.106.117 with SMTP id d108csp3398633qgf; Fri, 22 Sep 2017 08:02:54 -0700 (PDT) X-Google-Smtp-Source: AOwi7QBicWjpzzI59OZd3AMwug3u68c2h+9ryX7i7uG3tc2l3YeXpp0k/jaNezKzoOozHDxofnXc X-Received: by 10.200.47.173 with SMTP id l42mr8830613qta.298.1506092574725; Fri, 22 Sep 2017 08:02:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1506092574; cv=none; d=google.com; s=arc-20160816; b=ay3hpQFbt8fpRqh0TJ+xcMl/T6olz9rgv3mGwQqwtXdf0lwjWWkqI757ivIh9XdNq4 SnWWLz64G0KyxEKTlvkamBKer3ydU3SgCwDA8bqBDMWHeGYP6RwT7mB5Ykbl81QoZUC3 mRJY/8xRG8kE5eRLtwfVzU1qL89pc9rJXgUlXYKaXNFw9iEQqaX0LMsHE7eQYIiXjPDC Er+HdWNGwR9VtL/aO3mmIk6v09tqFu55xoy/5PLJshEh1Ba7psVXGf7uhnM62F/AkDUj OrFCW4iEf3JX0bIQVi0/E2C3vORRRUs1I2M0wL1DXRNj554UXo/JhLxNtF+NGqB7OVLz kSRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:github-pr-num :references:in-reply-to:message-id:date:to:from:delivered-to :arc-authentication-results; bh=dCUy2T7RFFg1ckdSc19P8my6+oDyTJJUtZcyB56JMAg=; b=CMq0EpUiT1JR4ii7FQ1ey8Q1Wo3zlZXl6fpU7QcU/xAkakIw2asTCSSdOh/bHx363a +L3kW9hjv9MNoEhMi2+y9BPJhLZuzizyY9W9VMfLY9O9Vxfnod4tgolrP0gvfzaxut6w HwJWQgP2rNnrSGWZc0fHc8knqSsKuRFPhRCQMm2uKTu/O7wIigGlMQ6HEnSt12UP7kC5 mLp0rhiAhPCBmzZzJhv3bRpl17bQi7EQdnjAR3g+u4U23Sny+TWs8qQ9ouGGxVxpLhDX bJYqS+A99tBHWYUFExEN6RXUpVE5RFBOaMgf7D5a75NBSBAFK6lXF85a/mxVD1DhQ4vA 9ZkQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=yandex.ru Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id m25si18872qtk.384.2017.09.22.08.02.54; Fri, 22 Sep 2017 08:02:54 -0700 (PDT) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=yandex.ru Received: by lists.linaro.org (Postfix, from userid 109) id 47D0360948; Fri, 22 Sep 2017 15:02:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 79E036094D; Fri, 22 Sep 2017 15:01:12 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id BD7AD60993; Fri, 22 Sep 2017 15:00:58 +0000 (UTC) Received: from forward103j.mail.yandex.net (forward103j.mail.yandex.net [5.45.198.246]) by lists.linaro.org (Postfix) with ESMTPS id 0FAF660985 for ; Fri, 22 Sep 2017 15:00:52 +0000 (UTC) Received: from mxback4g.mail.yandex.net (mxback4g.mail.yandex.net [IPv6:2a02:6b8:0:1472:2741:0:8b7:165]) by forward103j.mail.yandex.net (Yandex) with ESMTP id 6E0A834C2C50 for ; Fri, 22 Sep 2017 18:00:44 +0300 (MSK) Received: from smtp2p.mail.yandex.net (smtp2p.mail.yandex.net [2a02:6b8:0:1472:2741:0:8b6:7]) by mxback4g.mail.yandex.net (nwsmtp/Yandex) with ESMTP id iZa7dUkGdW-0iCiCOsp; Fri, 22 Sep 2017 18:00:44 +0300 Received: by smtp2p.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id oL55wxV1gc-0hVigKNV; Fri, 22 Sep 2017 18:00:43 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) From: Github ODP bot To: lng-odp@lists.linaro.org Date: Fri, 22 Sep 2017 18:00:38 +0300 Message-Id: <1506092438-26151-3-git-send-email-odpbot@yandex.ru> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1506092438-26151-1-git-send-email-odpbot@yandex.ru> References: <1506092438-26151-1-git-send-email-odpbot@yandex.ru> Github-pr-num: 180 Subject: [lng-odp] [PATCH v3 2/2] linux-generic: packet: add debugging asserts in packet code X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" From: Bill Fischofer Restore the ODP_ASSERTS() that assist application debugging that were lost in the recent packet reference rework. When using --enable-debug insert guards to detect duplicate packet frees as well as attempts to modify the shared data sections of packet references. Signed-off-by: Bill Fischofer --- /** Email created from pull request 180 (Bill-Fischofer-Linaro:pkt-debug) ** https://github.com/Linaro/odp/pull/180 ** Patch: https://github.com/Linaro/odp/pull/180.patch ** Base sha: c2ed3f71a8d61449c5e35ce201f020a74a8ea244 ** Merge commit sha: 3f5a1162ab09ace4e127eb630fcce86c84f6d9d1 **/ platform/linux-generic/odp_packet.c | 49 +++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/platform/linux-generic/odp_packet.c b/platform/linux-generic/odp_packet.c index a3fd0559f..6123cf733 100644 --- a/platform/linux-generic/odp_packet.c +++ b/platform/linux-generic/odp_packet.c @@ -354,6 +354,15 @@ static inline void link_segments(odp_packet_hdr_t *pkt_hdr[], int num) hdr->buf_hdr.seg[i].hdr = buf_hdr; hdr->buf_hdr.seg[i].data = buf_hdr->base_data; hdr->buf_hdr.seg[i].len = BASE_LEN; + + /* Ensure that ref_cnt is set to 1 for debug builds. + * This makes use of the comma operator to force + * the store only for debug builds since the expression + * (void_func(), 1) always evaluates to 1 so the + * ODP_ASSERT always succeeds. + */ + ODP_ASSERT((odp_atomic_store_u32(&hdr->buf_hdr.ref_cnt, + 1), 1)); cur++; if (cur == num) { @@ -389,6 +398,14 @@ static inline void init_segments(odp_packet_hdr_t *pkt_hdr[], int num) hdr->buf_hdr.next_seg = NULL; hdr->buf_hdr.last_seg = &hdr->buf_hdr; + /* Ensure that ref_cnt is set to 1 for debug builds. + * This makes use of the comma operator to force + * the store only for debug builds since the expression + * (void_func(), 1) always evaluates to 1 so the + * ODP_ASSERT always succeeds. + */ + ODP_ASSERT((odp_atomic_store_u32(&hdr->buf_hdr.ref_cnt, 1), 1)); + if (odp_unlikely(num > 1)) link_segments(pkt_hdr, num); } @@ -848,6 +865,8 @@ void odp_packet_free(odp_packet_t pkt) odp_packet_hdr_t *pkt_hdr = packet_hdr(pkt); int num_seg = pkt_hdr->buf_hdr.segcount; + ODP_ASSERT(buffer_ref(&pkt_hdr->buf_hdr) >= 1); + if (odp_likely(CONFIG_PACKET_MAX_SEGS == 1 || num_seg == 1)) { odp_buffer_hdr_t *buf_hdr[2]; int num = 1; @@ -877,6 +896,8 @@ void odp_packet_free_multi(const odp_packet_t pkt[], int num) odp_packet_hdr_t *pkt_hdr = packet_hdr(pkt[i]); int num_seg = pkt_hdr->buf_hdr.segcount; + ODP_ASSERT(buffer_ref(&pkt_hdr->buf_hdr) >= 1); + if (odp_unlikely(num_seg > 1)) { free_all_segments(pkt_hdr, num_seg); num_freed++; @@ -1010,6 +1031,8 @@ void *odp_packet_pull_head(odp_packet_t pkt, uint32_t len) if (len > pkt_hdr->frame_len) return NULL; + ODP_ASSERT(len <= odp_packet_unshared_len(pkt)); + pull_head(pkt_hdr, len); return packet_data(pkt_hdr); } @@ -1023,6 +1046,8 @@ int odp_packet_trunc_head(odp_packet_t *pkt, uint32_t len, if (len > pkt_hdr->frame_len) return -1; + ODP_ASSERT(len <= odp_packet_unshared_len(*pkt)); + if (len < seg_len) { pull_head(pkt_hdr, len); } else if (CONFIG_PACKET_MAX_SEGS != 1) { @@ -1057,6 +1082,8 @@ void *odp_packet_push_tail(odp_packet_t pkt, uint32_t len) if (len > pkt_hdr->tailroom) return NULL; + ODP_ASSERT(odp_packet_has_ref(pkt) == 0); + old_tail = packet_tail(pkt_hdr); push_tail(pkt_hdr, len); @@ -1072,6 +1099,8 @@ int odp_packet_extend_tail(odp_packet_t *pkt, uint32_t len, uint32_t tail_off = frame_len; int ret = 0; + ODP_ASSERT(odp_packet_has_ref(*pkt) == 0); + if (len > tailroom) { pool_t *pool = pkt_hdr->buf_hdr.pool_ptr; int num; @@ -1104,6 +1133,8 @@ void *odp_packet_pull_tail(odp_packet_t pkt, uint32_t len) odp_packet_hdr_t *pkt_hdr = packet_hdr(pkt); seg_entry_t *last_seg = seg_entry_last(pkt_hdr); + ODP_ASSERT(odp_packet_has_ref(pkt) == 0); + if (len > last_seg->len) return NULL; @@ -1123,6 +1154,8 @@ int odp_packet_trunc_tail(odp_packet_t *pkt, uint32_t len, if (len > pkt_hdr->frame_len) return -1; + ODP_ASSERT(odp_packet_has_ref(*pkt) == 0); + last = packet_last_seg(pkt_hdr); last_seg = seg_entry_last(pkt_hdr); seg_len = last_seg->len; @@ -1386,6 +1419,8 @@ int odp_packet_align(odp_packet_t *pkt, uint32_t offset, uint32_t len, if (align > ODP_CACHE_LINE_SIZE) return -1; + ODP_ASSERT(odp_packet_has_ref(*pkt) == 0); + if (seglen >= len) { misalign = align <= 1 ? 0 : ROUNDUP_ALIGN(uaddr, align) - uaddr; @@ -1423,6 +1458,8 @@ int odp_packet_concat(odp_packet_t *dst, odp_packet_t src) uint32_t dst_len = dst_hdr->frame_len; uint32_t src_len = src_hdr->frame_len; + ODP_ASSERT(odp_packet_has_ref(*dst) == 0); + /* Do a copy if packets are from different pools. */ if (odp_unlikely(dst_pool != src_pool)) { if (odp_packet_extend_tail(dst, src_len, NULL, NULL) >= 0) { @@ -1453,6 +1490,8 @@ int odp_packet_split(odp_packet_t *pkt, uint32_t len, odp_packet_t *tail) if (len >= pktlen || tail == NULL) return -1; + ODP_ASSERT(odp_packet_has_ref(*pkt) == 0); + *tail = odp_packet_copy_part(*pkt, len, pktlen - len, odp_packet_pool(*pkt)); @@ -1538,6 +1577,8 @@ int odp_packet_copy_from_mem(odp_packet_t pkt, uint32_t offset, if (offset + len > pkt_hdr->frame_len) return -1; + ODP_ASSERT(offset + len <= odp_packet_unshared_len(pkt)); + while (len > 0) { mapaddr = packet_map(pkt_hdr, offset, &seglen, NULL); cpylen = len > seglen ? seglen : len; @@ -1567,6 +1608,8 @@ int odp_packet_copy_from_pkt(odp_packet_t dst, uint32_t dst_offset, src_offset + len > src_hdr->frame_len) return -1; + ODP_ASSERT(dst_offset + len <= odp_packet_unshared_len(dst)); + overlap = (dst_hdr == src_hdr && ((dst_offset <= src_offset && dst_offset + len >= src_offset) || @@ -1607,6 +1650,8 @@ int odp_packet_copy_from_pkt(odp_packet_t dst, uint32_t dst_offset, int odp_packet_copy_data(odp_packet_t pkt, uint32_t dst_offset, uint32_t src_offset, uint32_t len) { + ODP_ASSERT(dst_offset + len <= odp_packet_unshared_len(pkt)); + return odp_packet_copy_from_pkt(pkt, dst_offset, pkt, src_offset, len); } @@ -1614,6 +1659,8 @@ int odp_packet_copy_data(odp_packet_t pkt, uint32_t dst_offset, int odp_packet_move_data(odp_packet_t pkt, uint32_t dst_offset, uint32_t src_offset, uint32_t len) { + ODP_ASSERT(dst_offset + len <= odp_packet_unshared_len(pkt)); + return odp_packet_copy_from_pkt(pkt, dst_offset, pkt, src_offset, len); } @@ -1629,6 +1676,8 @@ int _odp_packet_set_data(odp_packet_t pkt, uint32_t offset, if (offset + len > pkt_hdr->frame_len) return -1; + ODP_ASSERT(offset + len <= odp_packet_unshared_len(pkt)); + while (len > 0) { mapaddr = packet_map(pkt_hdr, offset, &seglen, NULL); setlen = len > seglen ? seglen : len;