Skip to content

Commit f5ff53b

Browse files
author
Al Viro
committed
{macvtap,tun}_get_user(): switch to iov_iter
allows to switch macvtap and tun from ->aio_write() to ->write_iter() Signed-off-by: Al Viro <[email protected]>
1 parent 3a654f9 commit f5ff53b

File tree

2 files changed

+45
-43
lines changed

2 files changed

+45
-43
lines changed

drivers/net/macvtap.c

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -640,19 +640,20 @@ static void macvtap_skb_to_vnet_hdr(const struct sk_buff *skb,
640640

641641
/* Get packet from user space buffer */
642642
static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
643-
const struct iovec *iv, unsigned long total_len,
644-
size_t count, int noblock)
643+
struct iov_iter *from, int noblock)
645644
{
646645
int good_linear = SKB_MAX_HEAD(NET_IP_ALIGN);
647646
struct sk_buff *skb;
648647
struct macvlan_dev *vlan;
648+
unsigned long total_len = iov_iter_count(from);
649649
unsigned long len = total_len;
650650
int err;
651651
struct virtio_net_hdr vnet_hdr = { 0 };
652652
int vnet_hdr_len = 0;
653653
int copylen = 0;
654654
bool zerocopy = false;
655655
size_t linear;
656+
ssize_t n;
656657

657658
if (q->flags & IFF_VNET_HDR) {
658659
vnet_hdr_len = q->vnet_hdr_sz;
@@ -662,10 +663,11 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
662663
goto err;
663664
len -= vnet_hdr_len;
664665

665-
err = memcpy_fromiovecend((void *)&vnet_hdr, iv, 0,
666-
sizeof(vnet_hdr));
667-
if (err < 0)
666+
err = -EFAULT;
667+
n = copy_from_iter(&vnet_hdr, sizeof(vnet_hdr), from);
668+
if (n != sizeof(vnet_hdr))
668669
goto err;
670+
iov_iter_advance(from, vnet_hdr_len - sizeof(vnet_hdr));
669671
if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
670672
vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
671673
vnet_hdr.hdr_len)
@@ -680,17 +682,16 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
680682
if (unlikely(len < ETH_HLEN))
681683
goto err;
682684

683-
err = -EMSGSIZE;
684-
if (unlikely(count > UIO_MAXIOV))
685-
goto err;
686-
687685
if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
686+
struct iov_iter i;
687+
688688
copylen = vnet_hdr.hdr_len ? vnet_hdr.hdr_len : GOODCOPY_LEN;
689689
if (copylen > good_linear)
690690
copylen = good_linear;
691691
linear = copylen;
692-
if (iov_pages(iv, vnet_hdr_len + copylen, count)
693-
<= MAX_SKB_FRAGS)
692+
i = *from;
693+
iov_iter_advance(&i, copylen);
694+
if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
694695
zerocopy = true;
695696
}
696697

@@ -708,10 +709,9 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
708709
goto err;
709710

710711
if (zerocopy)
711-
err = zerocopy_sg_from_iovec(skb, iv, vnet_hdr_len, count);
712+
err = zerocopy_sg_from_iter(skb, from);
712713
else {
713-
err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len,
714-
len);
714+
err = skb_copy_datagram_from_iter(skb, 0, from, len);
715715
if (!err && m && m->msg_control) {
716716
struct ubuf_info *uarg = m->msg_control;
717717
uarg->callback(uarg, false);
@@ -764,16 +764,12 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
764764
return err;
765765
}
766766

767-
static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv,
768-
unsigned long count, loff_t pos)
767+
static ssize_t macvtap_write_iter(struct kiocb *iocb, struct iov_iter *from)
769768
{
770769
struct file *file = iocb->ki_filp;
771-
ssize_t result = -ENOLINK;
772770
struct macvtap_queue *q = file->private_data;
773771

774-
result = macvtap_get_user(q, NULL, iv, iov_length(iv, count), count,
775-
file->f_flags & O_NONBLOCK);
776-
return result;
772+
return macvtap_get_user(q, NULL, from, file->f_flags & O_NONBLOCK);
777773
}
778774

779775
/* Put packet to the user space buffer */
@@ -1081,8 +1077,9 @@ static const struct file_operations macvtap_fops = {
10811077
.open = macvtap_open,
10821078
.release = macvtap_release,
10831079
.read = new_sync_read,
1080+
.write = new_sync_write,
10841081
.read_iter = macvtap_read_iter,
1085-
.aio_write = macvtap_aio_write,
1082+
.write_iter = macvtap_write_iter,
10861083
.poll = macvtap_poll,
10871084
.llseek = no_llseek,
10881085
.unlocked_ioctl = macvtap_ioctl,
@@ -1095,8 +1092,9 @@ static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock,
10951092
struct msghdr *m, size_t total_len)
10961093
{
10971094
struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
1098-
return macvtap_get_user(q, m, m->msg_iov, total_len, m->msg_iovlen,
1099-
m->msg_flags & MSG_DONTWAIT);
1095+
struct iov_iter from;
1096+
iov_iter_init(&from, WRITE, m->msg_iov, m->msg_iovlen, total_len);
1097+
return macvtap_get_user(q, m, &from, m->msg_flags & MSG_DONTWAIT);
11001098
}
11011099

11021100
static int macvtap_recvmsg(struct kiocb *iocb, struct socket *sock,

drivers/net/tun.c

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,36 +1012,38 @@ static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
10121012

10131013
/* Get packet from user space buffer */
10141014
static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
1015-
void *msg_control, const struct iovec *iv,
1016-
size_t total_len, size_t count, int noblock)
1015+
void *msg_control, struct iov_iter *from,
1016+
int noblock)
10171017
{
10181018
struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
10191019
struct sk_buff *skb;
1020+
size_t total_len = iov_iter_count(from);
10201021
size_t len = total_len, align = NET_SKB_PAD, linear;
10211022
struct virtio_net_hdr gso = { 0 };
10221023
int good_linear;
1023-
int offset = 0;
10241024
int copylen;
10251025
bool zerocopy = false;
10261026
int err;
10271027
u32 rxhash;
1028+
ssize_t n;
10281029

10291030
if (!(tun->flags & TUN_NO_PI)) {
10301031
if (len < sizeof(pi))
10311032
return -EINVAL;
10321033
len -= sizeof(pi);
10331034

1034-
if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi)))
1035+
n = copy_from_iter(&pi, sizeof(pi), from);
1036+
if (n != sizeof(pi))
10351037
return -EFAULT;
1036-
offset += sizeof(pi);
10371038
}
10381039

10391040
if (tun->flags & TUN_VNET_HDR) {
10401041
if (len < tun->vnet_hdr_sz)
10411042
return -EINVAL;
10421043
len -= tun->vnet_hdr_sz;
10431044

1044-
if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso)))
1045+
n = copy_from_iter(&gso, sizeof(gso), from);
1046+
if (n != sizeof(gso))
10451047
return -EFAULT;
10461048

10471049
if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
@@ -1050,7 +1052,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
10501052

10511053
if (gso.hdr_len > len)
10521054
return -EINVAL;
1053-
offset += tun->vnet_hdr_sz;
1055+
iov_iter_advance(from, tun->vnet_hdr_sz);
10541056
}
10551057

10561058
if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) {
@@ -1063,6 +1065,8 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
10631065
good_linear = SKB_MAX_HEAD(align);
10641066

10651067
if (msg_control) {
1068+
struct iov_iter i = *from;
1069+
10661070
/* There are 256 bytes to be copied in skb, so there is
10671071
* enough room for skb expand head in case it is used.
10681072
* The rest of the buffer is mapped from userspace.
@@ -1071,7 +1075,8 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
10711075
if (copylen > good_linear)
10721076
copylen = good_linear;
10731077
linear = copylen;
1074-
if (iov_pages(iv, offset + copylen, count) <= MAX_SKB_FRAGS)
1078+
iov_iter_advance(&i, copylen);
1079+
if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
10751080
zerocopy = true;
10761081
}
10771082

@@ -1091,9 +1096,9 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
10911096
}
10921097

10931098
if (zerocopy)
1094-
err = zerocopy_sg_from_iovec(skb, iv, offset, count);
1099+
err = zerocopy_sg_from_iter(skb, from);
10951100
else {
1096-
err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len);
1101+
err = skb_copy_datagram_from_iter(skb, 0, from, len);
10971102
if (!err && msg_control) {
10981103
struct ubuf_info *uarg = msg_control;
10991104
uarg->callback(uarg, false);
@@ -1207,8 +1212,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
12071212
return total_len;
12081213
}
12091214

1210-
static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
1211-
unsigned long count, loff_t pos)
1215+
static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from)
12121216
{
12131217
struct file *file = iocb->ki_filp;
12141218
struct tun_struct *tun = tun_get(file);
@@ -1218,10 +1222,7 @@ static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
12181222
if (!tun)
12191223
return -EBADFD;
12201224

1221-
tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count);
1222-
1223-
result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
1224-
count, file->f_flags & O_NONBLOCK);
1225+
result = tun_get_user(tun, tfile, NULL, from, file->f_flags & O_NONBLOCK);
12251226

12261227
tun_put(tun);
12271228
return result;
@@ -1445,11 +1446,14 @@ static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
14451446
int ret;
14461447
struct tun_file *tfile = container_of(sock, struct tun_file, socket);
14471448
struct tun_struct *tun = __tun_get(tfile);
1449+
struct iov_iter from;
14481450

14491451
if (!tun)
14501452
return -EBADFD;
1451-
ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
1452-
m->msg_iovlen, m->msg_flags & MSG_DONTWAIT);
1453+
1454+
iov_iter_init(&from, WRITE, m->msg_iov, m->msg_iovlen, total_len);
1455+
ret = tun_get_user(tun, tfile, m->msg_control, &from,
1456+
m->msg_flags & MSG_DONTWAIT);
14531457
tun_put(tun);
14541458
return ret;
14551459
}
@@ -2233,9 +2237,9 @@ static const struct file_operations tun_fops = {
22332237
.owner = THIS_MODULE,
22342238
.llseek = no_llseek,
22352239
.read = new_sync_read,
2240+
.write = new_sync_write,
22362241
.read_iter = tun_chr_read_iter,
2237-
.write = do_sync_write,
2238-
.aio_write = tun_chr_aio_write,
2242+
.write_iter = tun_chr_write_iter,
22392243
.poll = tun_chr_poll,
22402244
.unlocked_ioctl = tun_chr_ioctl,
22412245
#ifdef CONFIG_COMPAT

0 commit comments

Comments
 (0)