diff mbox series

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

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

Commit Message

Github ODP bot Sept. 22, 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 the 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: c2ed3f71a8d61449c5e35ce201f020a74a8ea244
 ** Merge commit sha: 3f5a1162ab09ace4e127eb630fcce86c84f6d9d1
 **/
 platform/linux-generic/odp_packet.c | 49 +++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
diff mbox series

Patch

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;