Commit f5ff53b4 authored by Al Viro's avatar Al Viro

{macvtap,tun}_get_user(): switch to iov_iter

allows to switch macvtap and tun from ->aio_write() to ->write_iter()
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
parent 3a654f97
...@@ -640,12 +640,12 @@ static void macvtap_skb_to_vnet_hdr(const struct sk_buff *skb, ...@@ -640,12 +640,12 @@ static void macvtap_skb_to_vnet_hdr(const struct sk_buff *skb,
/* Get packet from user space buffer */ /* Get packet from user space buffer */
static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m, static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
const struct iovec *iv, unsigned long total_len, struct iov_iter *from, int noblock)
size_t count, int noblock)
{ {
int good_linear = SKB_MAX_HEAD(NET_IP_ALIGN); int good_linear = SKB_MAX_HEAD(NET_IP_ALIGN);
struct sk_buff *skb; struct sk_buff *skb;
struct macvlan_dev *vlan; struct macvlan_dev *vlan;
unsigned long total_len = iov_iter_count(from);
unsigned long len = total_len; unsigned long len = total_len;
int err; int err;
struct virtio_net_hdr vnet_hdr = { 0 }; struct virtio_net_hdr vnet_hdr = { 0 };
...@@ -653,6 +653,7 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m, ...@@ -653,6 +653,7 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
int copylen = 0; int copylen = 0;
bool zerocopy = false; bool zerocopy = false;
size_t linear; size_t linear;
ssize_t n;
if (q->flags & IFF_VNET_HDR) { if (q->flags & IFF_VNET_HDR) {
vnet_hdr_len = q->vnet_hdr_sz; vnet_hdr_len = q->vnet_hdr_sz;
...@@ -662,10 +663,11 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m, ...@@ -662,10 +663,11 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
goto err; goto err;
len -= vnet_hdr_len; len -= vnet_hdr_len;
err = memcpy_fromiovecend((void *)&vnet_hdr, iv, 0, err = -EFAULT;
sizeof(vnet_hdr)); n = copy_from_iter(&vnet_hdr, sizeof(vnet_hdr), from);
if (err < 0) if (n != sizeof(vnet_hdr))
goto err; goto err;
iov_iter_advance(from, vnet_hdr_len - sizeof(vnet_hdr));
if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 > vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
vnet_hdr.hdr_len) vnet_hdr.hdr_len)
...@@ -680,17 +682,16 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m, ...@@ -680,17 +682,16 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
if (unlikely(len < ETH_HLEN)) if (unlikely(len < ETH_HLEN))
goto err; goto err;
err = -EMSGSIZE;
if (unlikely(count > UIO_MAXIOV))
goto err;
if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) { if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
struct iov_iter i;
copylen = vnet_hdr.hdr_len ? vnet_hdr.hdr_len : GOODCOPY_LEN; copylen = vnet_hdr.hdr_len ? vnet_hdr.hdr_len : GOODCOPY_LEN;
if (copylen > good_linear) if (copylen > good_linear)
copylen = good_linear; copylen = good_linear;
linear = copylen; linear = copylen;
if (iov_pages(iv, vnet_hdr_len + copylen, count) i = *from;
<= MAX_SKB_FRAGS) iov_iter_advance(&i, copylen);
if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
zerocopy = true; zerocopy = true;
} }
...@@ -708,10 +709,9 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m, ...@@ -708,10 +709,9 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
goto err; goto err;
if (zerocopy) if (zerocopy)
err = zerocopy_sg_from_iovec(skb, iv, vnet_hdr_len, count); err = zerocopy_sg_from_iter(skb, from);
else { else {
err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len, err = skb_copy_datagram_from_iter(skb, 0, from, len);
len);
if (!err && m && m->msg_control) { if (!err && m && m->msg_control) {
struct ubuf_info *uarg = m->msg_control; struct ubuf_info *uarg = m->msg_control;
uarg->callback(uarg, false); uarg->callback(uarg, false);
...@@ -764,16 +764,12 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m, ...@@ -764,16 +764,12 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
return err; return err;
} }
static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv, static ssize_t macvtap_write_iter(struct kiocb *iocb, struct iov_iter *from)
unsigned long count, loff_t pos)
{ {
struct file *file = iocb->ki_filp; struct file *file = iocb->ki_filp;
ssize_t result = -ENOLINK;
struct macvtap_queue *q = file->private_data; struct macvtap_queue *q = file->private_data;
result = macvtap_get_user(q, NULL, iv, iov_length(iv, count), count, return macvtap_get_user(q, NULL, from, file->f_flags & O_NONBLOCK);
file->f_flags & O_NONBLOCK);
return result;
} }
/* Put packet to the user space buffer */ /* Put packet to the user space buffer */
...@@ -1081,8 +1077,9 @@ static const struct file_operations macvtap_fops = { ...@@ -1081,8 +1077,9 @@ static const struct file_operations macvtap_fops = {
.open = macvtap_open, .open = macvtap_open,
.release = macvtap_release, .release = macvtap_release,
.read = new_sync_read, .read = new_sync_read,
.write = new_sync_write,
.read_iter = macvtap_read_iter, .read_iter = macvtap_read_iter,
.aio_write = macvtap_aio_write, .write_iter = macvtap_write_iter,
.poll = macvtap_poll, .poll = macvtap_poll,
.llseek = no_llseek, .llseek = no_llseek,
.unlocked_ioctl = macvtap_ioctl, .unlocked_ioctl = macvtap_ioctl,
...@@ -1095,8 +1092,9 @@ static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock, ...@@ -1095,8 +1092,9 @@ static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock,
struct msghdr *m, size_t total_len) struct msghdr *m, size_t total_len)
{ {
struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock); struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
return macvtap_get_user(q, m, m->msg_iov, total_len, m->msg_iovlen, struct iov_iter from;
m->msg_flags & MSG_DONTWAIT); iov_iter_init(&from, WRITE, m->msg_iov, m->msg_iovlen, total_len);
return macvtap_get_user(q, m, &from, m->msg_flags & MSG_DONTWAIT);
} }
static int macvtap_recvmsg(struct kiocb *iocb, struct socket *sock, static int macvtap_recvmsg(struct kiocb *iocb, struct socket *sock,
......
...@@ -1012,28 +1012,29 @@ static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, ...@@ -1012,28 +1012,29 @@ static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
/* Get packet from user space buffer */ /* Get packet from user space buffer */
static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
void *msg_control, const struct iovec *iv, void *msg_control, struct iov_iter *from,
size_t total_len, size_t count, int noblock) int noblock)
{ {
struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) }; struct tun_pi pi = { 0, cpu_to_be16(ETH_P_IP) };
struct sk_buff *skb; struct sk_buff *skb;
size_t total_len = iov_iter_count(from);
size_t len = total_len, align = NET_SKB_PAD, linear; size_t len = total_len, align = NET_SKB_PAD, linear;
struct virtio_net_hdr gso = { 0 }; struct virtio_net_hdr gso = { 0 };
int good_linear; int good_linear;
int offset = 0;
int copylen; int copylen;
bool zerocopy = false; bool zerocopy = false;
int err; int err;
u32 rxhash; u32 rxhash;
ssize_t n;
if (!(tun->flags & TUN_NO_PI)) { if (!(tun->flags & TUN_NO_PI)) {
if (len < sizeof(pi)) if (len < sizeof(pi))
return -EINVAL; return -EINVAL;
len -= sizeof(pi); len -= sizeof(pi);
if (memcpy_fromiovecend((void *)&pi, iv, 0, sizeof(pi))) n = copy_from_iter(&pi, sizeof(pi), from);
if (n != sizeof(pi))
return -EFAULT; return -EFAULT;
offset += sizeof(pi);
} }
if (tun->flags & TUN_VNET_HDR) { if (tun->flags & TUN_VNET_HDR) {
...@@ -1041,7 +1042,8 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, ...@@ -1041,7 +1042,8 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
return -EINVAL; return -EINVAL;
len -= tun->vnet_hdr_sz; len -= tun->vnet_hdr_sz;
if (memcpy_fromiovecend((void *)&gso, iv, offset, sizeof(gso))) n = copy_from_iter(&gso, sizeof(gso), from);
if (n != sizeof(gso))
return -EFAULT; return -EFAULT;
if ((gso.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && 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, ...@@ -1050,7 +1052,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
if (gso.hdr_len > len) if (gso.hdr_len > len)
return -EINVAL; return -EINVAL;
offset += tun->vnet_hdr_sz; iov_iter_advance(from, tun->vnet_hdr_sz);
} }
if ((tun->flags & TUN_TYPE_MASK) == TUN_TAP_DEV) { 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, ...@@ -1063,6 +1065,8 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
good_linear = SKB_MAX_HEAD(align); good_linear = SKB_MAX_HEAD(align);
if (msg_control) { if (msg_control) {
struct iov_iter i = *from;
/* There are 256 bytes to be copied in skb, so there is /* There are 256 bytes to be copied in skb, so there is
* enough room for skb expand head in case it is used. * enough room for skb expand head in case it is used.
* The rest of the buffer is mapped from userspace. * 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, ...@@ -1071,7 +1075,8 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
if (copylen > good_linear) if (copylen > good_linear)
copylen = good_linear; copylen = good_linear;
linear = copylen; linear = copylen;
if (iov_pages(iv, offset + copylen, count) <= MAX_SKB_FRAGS) iov_iter_advance(&i, copylen);
if (iov_iter_npages(&i, INT_MAX) <= MAX_SKB_FRAGS)
zerocopy = true; zerocopy = true;
} }
...@@ -1091,9 +1096,9 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, ...@@ -1091,9 +1096,9 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
} }
if (zerocopy) if (zerocopy)
err = zerocopy_sg_from_iovec(skb, iv, offset, count); err = zerocopy_sg_from_iter(skb, from);
else { else {
err = skb_copy_datagram_from_iovec(skb, 0, iv, offset, len); err = skb_copy_datagram_from_iter(skb, 0, from, len);
if (!err && msg_control) { if (!err && msg_control) {
struct ubuf_info *uarg = msg_control; struct ubuf_info *uarg = msg_control;
uarg->callback(uarg, false); uarg->callback(uarg, false);
...@@ -1207,8 +1212,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, ...@@ -1207,8 +1212,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
return total_len; return total_len;
} }
static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv, static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from)
unsigned long count, loff_t pos)
{ {
struct file *file = iocb->ki_filp; struct file *file = iocb->ki_filp;
struct tun_struct *tun = tun_get(file); 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, ...@@ -1218,10 +1222,7 @@ static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
if (!tun) if (!tun)
return -EBADFD; return -EBADFD;
tun_debug(KERN_INFO, tun, "tun_chr_write %ld\n", count); result = tun_get_user(tun, tfile, NULL, from, file->f_flags & O_NONBLOCK);
result = tun_get_user(tun, tfile, NULL, iv, iov_length(iv, count),
count, file->f_flags & O_NONBLOCK);
tun_put(tun); tun_put(tun);
return result; return result;
...@@ -1445,11 +1446,14 @@ static int tun_sendmsg(struct kiocb *iocb, struct socket *sock, ...@@ -1445,11 +1446,14 @@ static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
int ret; int ret;
struct tun_file *tfile = container_of(sock, struct tun_file, socket); struct tun_file *tfile = container_of(sock, struct tun_file, socket);
struct tun_struct *tun = __tun_get(tfile); struct tun_struct *tun = __tun_get(tfile);
struct iov_iter from;
if (!tun) if (!tun)
return -EBADFD; return -EBADFD;
ret = tun_get_user(tun, tfile, m->msg_control, m->msg_iov, total_len,
m->msg_iovlen, m->msg_flags & MSG_DONTWAIT); iov_iter_init(&from, WRITE, m->msg_iov, m->msg_iovlen, total_len);
ret = tun_get_user(tun, tfile, m->msg_control, &from,
m->msg_flags & MSG_DONTWAIT);
tun_put(tun); tun_put(tun);
return ret; return ret;
} }
...@@ -2233,9 +2237,9 @@ static const struct file_operations tun_fops = { ...@@ -2233,9 +2237,9 @@ static const struct file_operations tun_fops = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.llseek = no_llseek, .llseek = no_llseek,
.read = new_sync_read, .read = new_sync_read,
.write = new_sync_write,
.read_iter = tun_chr_read_iter, .read_iter = tun_chr_read_iter,
.write = do_sync_write, .write_iter = tun_chr_write_iter,
.aio_write = tun_chr_aio_write,
.poll = tun_chr_poll, .poll = tun_chr_poll,
.unlocked_ioctl = tun_chr_ioctl, .unlocked_ioctl = tun_chr_ioctl,
#ifdef CONFIG_COMPAT #ifdef CONFIG_COMPAT
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment