Skip to contentSkip to conversation

net: refactor tcp splice receive path to improve readability

PATCH Superseded Octavian Purdila 2 participants 2 messages Jun 24, 2008 – Jul 6, 2008

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: refactor tcp splice receive path to improve readability

Hi David,

Sorry for the duplicate, forgot to CC netdev.

Please consider applying this to net-next-2.6. I've minimally tested it and no performance or functional problems were found. It applies on top of the previous fix I've sent ([PATH] tcp: fix for splice receive when used with software LRO).

Thanks,
tavi

commit 8caea829369378e2dff254978eea6b59d2d4629e

Author: Octavian Purdila <[email protected]>
Date:   Mon Jun 23 18:08:00 2008 +0300

net: refactor tcp splice receive path to improve readability

- move all of the details on offsets, lengths and buffers into a single function instead of doing these operation from multiple places

- use a bottom up approach: try to avoid details in the high level functions, introduce them gradually as we go deeper in the function call stack

With helpful feedback from Jarek Poplawski.

net/core/skbuff.c +59 -80
index 3895b1c..418c958 100644
@@ -1280,104 +1280,83 @@ static inline int spd_fill_page(struct splice_pipe_desc *spd, struct page *page,
 	return 0;
 }
 
-/*
- * Map linear and fragment data from the skb to spd. Returns number of
- * pages mapped.
- */
-static int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
-			     unsigned int *total_len,
-			     struct splice_pipe_desc *spd)
+static inline void __segment_seek(struct page **page, unsigned int *poff,
+				  unsigned int *plen, unsigned int off)
 {
-	unsigned int poff, plen, len, toff, tlen;
-	int headlen, seg;
+	*poff += off;
+	*page += *poff / PAGE_SIZE;
+	*poff = *poff % PAGE_SIZE;
+	*plen -= off;
+}
 
-	toff = *offset;
-	tlen = *total_len;
-	if (!tlen)
-		goto err;
+static inline int __splice_segment(struct page *page, unsigned int poff,
+				   unsigned int plen, unsigned int *off,
+				   unsigned int *len, struct sk_buff *skb,
+				   struct splice_pipe_desc *spd)
+{
+	if (!*len)
+		return 1;
 
-	/*
-	 * if the offset is greater than the linear part, go directly to
-	 * the fragments.
-	 */
-	headlen = skb_headlen(skb);
-	if (toff >= headlen) {
-		toff -= headlen;
-		goto map_frag;
+	/* skip this segment if already processed */
+	if (*off >= plen) {
+		*off -= plen;
+		return 0;
 	}
 
-	/*
-	 * first map the linear region into the pages/partial map, skipping
-	 * any potential initial offset.
-	 */
-	len = 0;
-	while (len < headlen) {
-		void *p = skb->data + len;
-
-		poff = (unsigned long) p & (PAGE_SIZE - 1);
-		plen = min_t(unsigned int, headlen - len, PAGE_SIZE - poff);
-		len += plen;
-
-		if (toff) {
-			if (plen <= toff) {
-				toff -= plen;
-				continue;
-			}
-			plen -= toff;
-			poff += toff;
-			toff = 0;
-		}
+	/* ignore any bits we already processed */
+	if (*off) {
+		__segment_seek(&page, &poff, &plen, *off);
+		*off = 0;
+	}
 
-		plen = min(plen, tlen);
-		if (!plen)
-			break;
+	do {
+		unsigned int flen = min(*len, plen);
 
-		/*
-		 * just jump directly to update and return, no point
-		 * in going over fragments when the output is full.
-		 */
-		if (spd_fill_page(spd, virt_to_page(p), plen, poff, skb))
-			goto err;
+		/* the linear region may spread across several pages  */
+		flen = min_t(unsigned int, flen, PAGE_SIZE - poff);
 
-		tlen -= plen;
-	}
+		if (spd_fill_page(spd, page, flen, poff, skb))
+			return 1;
+
+		__segment_seek(&page, &poff, &plen, flen);
+		*len -= flen;
+
+	} while (*len && plen);
+
+	return 0;
+}
+
+/*
+ * Map linear and fragment data from the skb to spd. It reports failure if the
+ * pipe is full or if we already spliced the requested length.
+ */
+int __skb_splice_bits(struct sk_buff *skb, unsigned int *offset,
+		      unsigned int *len,
+		      struct splice_pipe_desc *spd)
+{
+	int seg;
+
+	/*
+	 * map the linear part
+	 */
+	if (__splice_segment(virt_to_page(skb->data),
+			     (unsigned long) skb->data & (PAGE_SIZE - 1),
+			     skb_headlen(skb),
+			     offset, len, skb, spd))
+		return 1;
 
 	/*
 	 * then map the fragments
 	 */
-map_frag:
 	for (seg = 0; seg < skb_shinfo(skb)->nr_frags; seg++) {
 		const skb_frag_t *f = &skb_shinfo(skb)->frags[seg];
 
-		plen = f->size;
-		poff = f->page_offset;
-
-		if (toff) {
-			if (plen <= toff) {
-				toff -= plen;
-				continue;
-			}
-			plen -= toff;
-			poff += toff;
-			toff = 0;
-		}
-
-		plen = min(plen, tlen);
-		if (!plen)
-			break;
-
-		if (spd_fill_page(spd, f->page, plen, poff, skb))
-			goto err;
-
-		tlen -= plen;
+		if (__splice_segment(f->page, f->page_offset, f->size,
+				     offset, len, skb, spd))
+			return 1;
 	}
 
-	*offset = toff;
-	*total_len = tlen;
-
 	return 0;
-err:
-	return 1;
 }
 
 /*
maintainer
#1 lore raw

From: Octavian Purdila <[email protected]>
Date: Tue, 24 Jun 2008 13:03:36 +0300

Quoted 4 lines

Please consider applying this to net-next-2.6. I've minimally tested it and no performance or functional problems were found. It applies on top of the previous fix I've sent ([PATH] tcp: fix for splice receive when used with software LRO).

It doesn't apply cleanly even though I've merged in net-2.6 which includes your splice receive bug fix.

Please respin this patch, thanks.