netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Vito Caputo <vcaputo@pengaru.com>
To: davem@davemloft.net
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] net: core: datagram: tidy up copy functions a bit
Date: Sat, 12 Oct 2019 04:55:09 -0700	[thread overview]
Message-ID: <20191012115509.jrqe43yozs7kknv5@shells.gnugeneration.com> (raw)

Eliminate some verbosity by using min() macro and consolidating some
things, also fix inconsistent zero tests (! vs. == 0).

Signed-off-by: Vito Caputo <vcaputo@pengaru.com>
---
 net/core/datagram.c | 44 ++++++++++++++------------------------------
 1 file changed, 14 insertions(+), 30 deletions(-)

diff --git a/net/core/datagram.c b/net/core/datagram.c
index 4cc8dc5db2b7..08d403f93952 100644
--- a/net/core/datagram.c
+++ b/net/core/datagram.c
@@ -413,13 +413,11 @@ static int __skb_datagram_iter(const struct sk_buff *skb, int offset,
 					    struct iov_iter *), void *data)
 {
 	int start = skb_headlen(skb);
-	int i, copy = start - offset, start_off = offset, n;
+	int i, copy, start_off = offset, n;
 	struct sk_buff *frag_iter;
 
 	/* Copy header. */
-	if (copy > 0) {
-		if (copy > len)
-			copy = len;
+	if ((copy = min(start - offset, len)) > 0) {
 		n = cb(skb->data + offset, copy, data, to);
 		offset += n;
 		if (n != copy)
@@ -430,39 +428,33 @@ static int __skb_datagram_iter(const struct sk_buff *skb, int offset,
 
 	/* Copy paged appendix. Hmm... why does this look so complicated? */
 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
-		int end;
 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
+		int end = start + skb_frag_size(frag);
 
 		WARN_ON(start > offset + len);
 
-		end = start + skb_frag_size(frag);
-		if ((copy = end - offset) > 0) {
+		if ((copy = min(end - offset, len)) > 0) {
 			struct page *page = skb_frag_page(frag);
 			u8 *vaddr = kmap(page);
 
-			if (copy > len)
-				copy = len;
 			n = cb(vaddr + skb_frag_off(frag) + offset - start,
 			       copy, data, to);
 			kunmap(page);
 			offset += n;
 			if (n != copy)
 				goto short_copy;
-			if (!(len -= copy))
+			if ((len -= copy) == 0)
 				return 0;
 		}
 		start = end;
 	}
 
 	skb_walk_frags(skb, frag_iter) {
-		int end;
+		int end = start + frag_iter->len;
 
 		WARN_ON(start > offset + len);
 
-		end = start + frag_iter->len;
-		if ((copy = end - offset) > 0) {
-			if (copy > len)
-				copy = len;
+		if ((copy = min(end - offset, len)) > 0) {
 			if (__skb_datagram_iter(frag_iter, offset - start,
 						to, copy, fault_short, cb, data))
 				goto fault;
@@ -545,13 +537,11 @@ int skb_copy_datagram_from_iter(struct sk_buff *skb, int offset,
 				 int len)
 {
 	int start = skb_headlen(skb);
-	int i, copy = start - offset;
 	struct sk_buff *frag_iter;
+	int i, copy;
 
 	/* Copy header. */
-	if (copy > 0) {
-		if (copy > len)
-			copy = len;
+	if ((copy = min(start - offset, len)) > 0) {
 		if (copy_from_iter(skb->data + offset, copy, from) != copy)
 			goto fault;
 		if ((len -= copy) == 0)
@@ -561,24 +551,21 @@ int skb_copy_datagram_from_iter(struct sk_buff *skb, int offset,
 
 	/* Copy paged appendix. Hmm... why does this look so complicated? */
 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
-		int end;
 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
+		int end = start + skb_frag_size(frag);
 
 		WARN_ON(start > offset + len);
 
-		end = start + skb_frag_size(frag);
-		if ((copy = end - offset) > 0) {
+		if ((copy = min(end - offset, len)) > 0) {
 			size_t copied;
 
-			if (copy > len)
-				copy = len;
 			copied = copy_page_from_iter(skb_frag_page(frag),
 					  skb_frag_off(frag) + offset - start,
 					  copy, from);
 			if (copied != copy)
 				goto fault;
 
-			if (!(len -= copy))
+			if ((len -= copy) == 0)
 				return 0;
 			offset += copy;
 		}
@@ -586,14 +573,11 @@ int skb_copy_datagram_from_iter(struct sk_buff *skb, int offset,
 	}
 
 	skb_walk_frags(skb, frag_iter) {
-		int end;
+		int end = start + frag_iter->len;
 
 		WARN_ON(start > offset + len);
 
-		end = start + frag_iter->len;
-		if ((copy = end - offset) > 0) {
-			if (copy > len)
-				copy = len;
+		if ((copy = min(end - offset, len)) > 0) {
 			if (skb_copy_datagram_from_iter(frag_iter,
 							offset - start,
 							from, copy))
-- 
2.11.0


             reply	other threads:[~2019-10-12 11:57 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-12 11:55 Vito Caputo [this message]
2019-10-13 19:30 ` [PATCH] net: core: datagram: tidy up copy functions a bit Eric Dumazet
2019-10-13 20:01   ` Vito Caputo
2019-10-13 20:17     ` Eric Dumazet
2019-10-13 22:41       ` Vito Caputo
2019-10-14  1:04         ` Eric Dumazet
2019-10-15 21:59 ` David Miller
2019-10-21  8:53 ` [net] 635f03c839: WARNING:at_net/core/datagram.c:#__skb_datagram_iter kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191012115509.jrqe43yozs7kknv5@shells.gnugeneration.com \
    --to=vcaputo@pengaru.com \
    --cc=davem@davemloft.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).