Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tuntap: do not zerocopy if iov needs more pages than MAX_SKB_FRAGS

We try to linearize part of the skb when the number of iov is greater than
MAX_SKB_FRAGS. This is not enough since each single vector may occupy more than
one pages, so zerocopy_sg_fromiovec() may still fail and may break the guest
network.

Solve this problem by calculate the pages needed for iov before trying to do
zerocopy and switch to use copy instead of zerocopy if it needs more than
MAX_SKB_FRAGS.

This is done through introducing a new helper to count the pages for iov, and
call uarg->callback() manually when switching from zerocopy to copy to notify
vhost.

We can do further optimization on top.

The bug were introduced from commit 0690899b4d4501b3505be069b9a687e68ccbe15b
(tun: experimental zero copy tx support)

Cc: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Jason Wang and committed by
David S. Miller
88529176 87f40dd6

+38 -24
+38 -24
drivers/net/tun.c
··· 1035 1035 return 0; 1036 1036 } 1037 1037 1038 + static unsigned long iov_pages(const struct iovec *iv, int offset, 1039 + unsigned long nr_segs) 1040 + { 1041 + unsigned long seg, base; 1042 + int pages = 0, len, size; 1043 + 1044 + while (nr_segs && (offset >= iv->iov_len)) { 1045 + offset -= iv->iov_len; 1046 + ++iv; 1047 + --nr_segs; 1048 + } 1049 + 1050 + for (seg = 0; seg < nr_segs; seg++) { 1051 + base = (unsigned long)iv[seg].iov_base + offset; 1052 + len = iv[seg].iov_len - offset; 1053 + size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT; 1054 + pages += size; 1055 + offset = 0; 1056 + } 1057 + 1058 + return pages; 1059 + } 1060 + 1038 1061 /* Get packet from user space buffer */ 1039 1062 static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, 1040 1063 void *msg_control, const struct iovec *iv, ··· 1105 1082 return -EINVAL; 1106 1083 } 1107 1084 1108 - if (msg_control) 1109 - zerocopy = true; 1110 - 1111 - if (zerocopy) { 1112 - /* Userspace may produce vectors with count greater than 1113 - * MAX_SKB_FRAGS, so we need to linearize parts of the skb 1114 - * to let the rest of data to be fit in the frags. 1115 - */ 1116 - if (count > MAX_SKB_FRAGS) { 1117 - copylen = iov_length(iv, count - MAX_SKB_FRAGS); 1118 - if (copylen < offset) 1119 - copylen = 0; 1120 - else 1121 - copylen -= offset; 1122 - } else 1123 - copylen = 0; 1124 - /* There are 256 bytes to be copied in skb, so there is enough 1125 - * room for skb expand head in case it is used. 1085 + if (msg_control) { 1086 + /* There are 256 bytes to be copied in skb, so there is 1087 + * enough room for skb expand head in case it is used. 1126 1088 * The rest of the buffer is mapped from userspace. 1127 1089 */ 1128 - if (copylen < gso.hdr_len) 1129 - copylen = gso.hdr_len; 1130 - if (!copylen) 1131 - copylen = GOODCOPY_LEN; 1090 + copylen = gso.hdr_len ? gso.hdr_len : GOODCOPY_LEN; 1132 1091 linear = copylen; 1133 - } else { 1092 + if (iov_pages(iv, offset + copylen, count) <= MAX_SKB_FRAGS) 1093 + zerocopy = true; 1094 + } 1095 + 1096 + if (!zerocopy) { 1134 1097 copylen = len; 1135 1098 linear = gso.hdr_len; 1136 1099 } ··· 1130 1121 1131 1122 if (zerocopy) 1132 1123 err = zerocopy_sg_from_iovec(skb, iv, offset, count); 1133 - else 1124 + else { 1134 1125 err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len); 1126 + if (!err && msg_control) { 1127 + struct ubuf_info *uarg = msg_control; 1128 + uarg->callback(uarg, false); 1129 + } 1130 + } 1135 1131 1136 1132 if (err) { 1137 1133 tun->dev->stats.rx_dropped++;