[v4,2/2] linux-generic: packet: add debugging asserts in packet code

Message ID 1507564818-31998-3-git-send-email-odpbot@yandex.ru
State Superseded
Headers show
Series
  • Pkt debug
Related show

Commit Message

Github ODP bot Oct. 9, 2017, 4 p.m.
From: Bill Fischofer <bill.fischofer@linaro.org>


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 known shared data sections of packet references.

Signed-off-by: Bill Fischofer <bill.fischofer@linaro.org>

---
/** 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: c16af6486eea240609f334b1bdc81a11404275de
 ** Merge commit sha: 02c568dd05ef6e800819dd30516c1dc8016d268c
 **/
 platform/linux-generic/odp_packet.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

Patch

diff --git a/platform/linux-generic/odp_packet.c b/platform/linux-generic/odp_packet.c
index 012c9318e..5db4a4764 100644
--- a/platform/linux-generic/odp_packet.c
+++ b/platform/linux-generic/odp_packet.c
@@ -354,6 +354,11 @@  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;
+
+			ODP_ASSERT(cur == 0 || /* init_segments() does this */
+				   odp_atomic_fetch_inc_u32(
+					   &pkt_hdr[cur]->buf_hdr.ref_cnt)
+				   == 0);
 			cur++;
 
 			if (cur == num) {
@@ -389,6 +394,9 @@  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;
 
+		ODP_ASSERT(
+			odp_atomic_fetch_inc_u32(&hdr->buf_hdr.ref_cnt) == 0);
+
 		if (odp_unlikely(num > 1))
 			link_segments(pkt_hdr, num);
 	}
@@ -848,6 +856,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) > 0);
+
 	if (odp_likely(CONFIG_PACKET_MAX_SEGS == 1 || num_seg == 1)) {
 		odp_buffer_hdr_t *buf_hdr[2];
 		int num = 1;
@@ -877,6 +887,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) > 0);
+
 		if (odp_unlikely(num_seg > 1)) {
 			free_all_segments(pkt_hdr, num_seg);
 			num_freed++;
@@ -1057,6 +1069,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 +1086,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 +1120,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 +1141,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 +1406,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 +1445,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 +1477,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));