diff mbox series

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

Message ID 1507993210-15088-3-git-send-email-odpbot@yandex.ru
State New
Headers show
Series [v5,1/2] linux-generic: packet: correct syntax issue | expand

Commit Message

Github ODP bot Oct. 14, 2017, 3 p.m. UTC
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: ae4f1d82ed704992c1d8284c23795b9e076b33b9
 ** Merge commit sha: 75ccc68f61089fffbf834b8dacd952c887b70ff8
 **/
 platform/linux-generic/odp_packet.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
diff mbox series

Patch

diff --git a/platform/linux-generic/odp_packet.c b/platform/linux-generic/odp_packet.c
index 012c9318e..744ee9a34 100644
--- a/platform/linux-generic/odp_packet.c
+++ b/platform/linux-generic/odp_packet.c
@@ -354,6 +354,10 @@  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(0 == cur || /* init_segments() does this */
+				   0 == odp_atomic_fetch_inc_u32(
+					   &pkt_hdr[cur]->buf_hdr.ref_cnt));
 			cur++;
 
 			if (cur == num) {
@@ -389,6 +393,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 +855,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 +886,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 +1068,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 +1085,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 +1119,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 +1140,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 +1405,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 +1444,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 +1476,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));