Commit 7f6d4670 authored by David S. Miller's avatar David S. Miller

Merge branch 'iov_iter'

Herbert Xu says:

====================
Replace skb_copy_datagram_const_iovec with iterator version

This patch series adds the helper skb_copy_datagram_iter, which
is meant to replace both skb_copy_datagram_iovec and its evil
twin skb_copy_datagram_const_iovec.

It then converts tun and macvtap over to the new helper and finally
removes skb_copy_datagram_const_iovec which is only used by tun
and macvtap.

The copy_to_iter return value issue pointed out by Al has now been
fixed.
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 4e84b496 bfe1be38
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include <linux/cdev.h> #include <linux/cdev.h>
#include <linux/idr.h> #include <linux/idr.h>
#include <linux/fs.h> #include <linux/fs.h>
#include <linux/uio.h>
#include <net/ipv6.h> #include <net/ipv6.h>
#include <net/net_namespace.h> #include <net/net_namespace.h>
...@@ -778,31 +779,29 @@ static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv, ...@@ -778,31 +779,29 @@ static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv,
/* Put packet to the user space buffer */ /* Put packet to the user space buffer */
static ssize_t macvtap_put_user(struct macvtap_queue *q, static ssize_t macvtap_put_user(struct macvtap_queue *q,
const struct sk_buff *skb, const struct sk_buff *skb,
const struct iovec *iv, int len) struct iov_iter *iter)
{ {
int ret; int ret;
int vnet_hdr_len = 0; int vnet_hdr_len = 0;
int vlan_offset = 0; int vlan_offset = 0;
int copied, total; int total;
if (q->flags & IFF_VNET_HDR) { if (q->flags & IFF_VNET_HDR) {
struct virtio_net_hdr vnet_hdr; struct virtio_net_hdr vnet_hdr;
vnet_hdr_len = q->vnet_hdr_sz; vnet_hdr_len = q->vnet_hdr_sz;
if ((len -= vnet_hdr_len) < 0) if (iov_iter_count(iter) < vnet_hdr_len)
return -EINVAL; return -EINVAL;
macvtap_skb_to_vnet_hdr(skb, &vnet_hdr); macvtap_skb_to_vnet_hdr(skb, &vnet_hdr);
if (memcpy_toiovecend(iv, (void *)&vnet_hdr, 0, sizeof(vnet_hdr))) if (copy_to_iter(&vnet_hdr, sizeof(vnet_hdr), iter) !=
sizeof(vnet_hdr))
return -EFAULT; return -EFAULT;
} }
total = copied = vnet_hdr_len; total = vnet_hdr_len;
total += skb->len; total += skb->len;
if (!vlan_tx_tag_present(skb)) if (vlan_tx_tag_present(skb)) {
len = min_t(int, skb->len, len);
else {
int copy;
struct { struct {
__be16 h_vlan_proto; __be16 h_vlan_proto;
__be16 h_vlan_TCI; __be16 h_vlan_TCI;
...@@ -811,37 +810,33 @@ static ssize_t macvtap_put_user(struct macvtap_queue *q, ...@@ -811,37 +810,33 @@ static ssize_t macvtap_put_user(struct macvtap_queue *q,
veth.h_vlan_TCI = htons(vlan_tx_tag_get(skb)); veth.h_vlan_TCI = htons(vlan_tx_tag_get(skb));
vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto); vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
len = min_t(int, skb->len + VLAN_HLEN, len);
total += VLAN_HLEN; total += VLAN_HLEN;
copy = min_t(int, vlan_offset, len); ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
ret = skb_copy_datagram_const_iovec(skb, 0, iv, copied, copy); if (ret || !iov_iter_count(iter))
len -= copy;
copied += copy;
if (ret || !len)
goto done; goto done;
copy = min_t(int, sizeof(veth), len); ret = copy_to_iter(&veth, sizeof(veth), iter);
ret = memcpy_toiovecend(iv, (void *)&veth, copied, copy); if (ret != sizeof(veth) || !iov_iter_count(iter))
len -= copy;
copied += copy;
if (ret || !len)
goto done; goto done;
} }
ret = skb_copy_datagram_const_iovec(skb, vlan_offset, iv, copied, len); ret = skb_copy_datagram_iter(skb, vlan_offset, iter,
skb->len - vlan_offset);
done: done:
return ret ? ret : total; return ret ? ret : total;
} }
static ssize_t macvtap_do_read(struct macvtap_queue *q, static ssize_t macvtap_do_read(struct macvtap_queue *q,
const struct iovec *iv, unsigned long len, const struct iovec *iv, unsigned long segs,
unsigned long len,
int noblock) int noblock)
{ {
DEFINE_WAIT(wait); DEFINE_WAIT(wait);
struct sk_buff *skb; struct sk_buff *skb;
ssize_t ret = 0; ssize_t ret = 0;
struct iov_iter iter;
while (len) { while (len) {
if (!noblock) if (!noblock)
...@@ -863,7 +858,8 @@ static ssize_t macvtap_do_read(struct macvtap_queue *q, ...@@ -863,7 +858,8 @@ static ssize_t macvtap_do_read(struct macvtap_queue *q,
schedule(); schedule();
continue; continue;
} }
ret = macvtap_put_user(q, skb, iv, len); iov_iter_init(&iter, READ, iv, segs, len);
ret = macvtap_put_user(q, skb, &iter);
kfree_skb(skb); kfree_skb(skb);
break; break;
} }
...@@ -886,7 +882,7 @@ static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv, ...@@ -886,7 +882,7 @@ static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv,
goto out; goto out;
} }
ret = macvtap_do_read(q, iv, len, file->f_flags & O_NONBLOCK); ret = macvtap_do_read(q, iv, count, len, file->f_flags & O_NONBLOCK);
ret = min_t(ssize_t, ret, len); ret = min_t(ssize_t, ret, len);
if (ret > 0) if (ret > 0)
iocb->ki_pos = ret; iocb->ki_pos = ret;
...@@ -1117,7 +1113,7 @@ static int macvtap_recvmsg(struct kiocb *iocb, struct socket *sock, ...@@ -1117,7 +1113,7 @@ static int macvtap_recvmsg(struct kiocb *iocb, struct socket *sock,
int ret; int ret;
if (flags & ~(MSG_DONTWAIT|MSG_TRUNC)) if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
return -EINVAL; return -EINVAL;
ret = macvtap_do_read(q, m->msg_iov, total_len, ret = macvtap_do_read(q, m->msg_iov, m->msg_iovlen, total_len,
flags & MSG_DONTWAIT); flags & MSG_DONTWAIT);
if (ret > total_len) { if (ret > total_len) {
m->msg_flags |= MSG_TRUNC; m->msg_flags |= MSG_TRUNC;
......
...@@ -71,6 +71,7 @@ ...@@ -71,6 +71,7 @@
#include <net/rtnetlink.h> #include <net/rtnetlink.h>
#include <net/sock.h> #include <net/sock.h>
#include <linux/seq_file.h> #include <linux/seq_file.h>
#include <linux/uio.h>
#include <asm/uaccess.h> #include <asm/uaccess.h>
...@@ -1230,11 +1231,11 @@ static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv, ...@@ -1230,11 +1231,11 @@ static ssize_t tun_chr_aio_write(struct kiocb *iocb, const struct iovec *iv,
static ssize_t tun_put_user(struct tun_struct *tun, static ssize_t tun_put_user(struct tun_struct *tun,
struct tun_file *tfile, struct tun_file *tfile,
struct sk_buff *skb, struct sk_buff *skb,
const struct iovec *iv, int len) struct iov_iter *iter)
{ {
struct tun_pi pi = { 0, skb->protocol }; struct tun_pi pi = { 0, skb->protocol };
ssize_t total = 0; ssize_t total;
int vlan_offset = 0, copied; int vlan_offset;
int vlan_hlen = 0; int vlan_hlen = 0;
int vnet_hdr_sz = 0; int vnet_hdr_sz = 0;
...@@ -1244,23 +1245,25 @@ static ssize_t tun_put_user(struct tun_struct *tun, ...@@ -1244,23 +1245,25 @@ static ssize_t tun_put_user(struct tun_struct *tun,
if (tun->flags & TUN_VNET_HDR) if (tun->flags & TUN_VNET_HDR)
vnet_hdr_sz = tun->vnet_hdr_sz; vnet_hdr_sz = tun->vnet_hdr_sz;
total = skb->len + vlan_hlen + vnet_hdr_sz;
if (!(tun->flags & TUN_NO_PI)) { if (!(tun->flags & TUN_NO_PI)) {
if ((len -= sizeof(pi)) < 0) if (iov_iter_count(iter) < sizeof(pi))
return -EINVAL; return -EINVAL;
if (len < skb->len + vlan_hlen + vnet_hdr_sz) { total += sizeof(pi);
if (iov_iter_count(iter) < total) {
/* Packet will be striped */ /* Packet will be striped */
pi.flags |= TUN_PKT_STRIP; pi.flags |= TUN_PKT_STRIP;
} }
if (memcpy_toiovecend(iv, (void *) &pi, 0, sizeof(pi))) if (copy_to_iter(&pi, sizeof(pi), iter) != sizeof(pi))
return -EFAULT; return -EFAULT;
total += sizeof(pi);
} }
if (vnet_hdr_sz) { if (vnet_hdr_sz) {
struct virtio_net_hdr gso = { 0 }; /* no info leak */ struct virtio_net_hdr gso = { 0 }; /* no info leak */
if ((len -= vnet_hdr_sz) < 0) if (iov_iter_count(iter) < vnet_hdr_sz)
return -EINVAL; return -EINVAL;
if (skb_is_gso(skb)) { if (skb_is_gso(skb)) {
...@@ -1299,17 +1302,12 @@ static ssize_t tun_put_user(struct tun_struct *tun, ...@@ -1299,17 +1302,12 @@ static ssize_t tun_put_user(struct tun_struct *tun,
gso.flags = VIRTIO_NET_HDR_F_DATA_VALID; gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
} /* else everything is zero */ } /* else everything is zero */
if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total, if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
sizeof(gso))))
return -EFAULT; return -EFAULT;
total += vnet_hdr_sz;
} }
copied = total;
len = min_t(int, skb->len + vlan_hlen, len);
total += skb->len + vlan_hlen;
if (vlan_hlen) { if (vlan_hlen) {
int copy, ret; int ret;
struct { struct {
__be16 h_vlan_proto; __be16 h_vlan_proto;
__be16 h_vlan_TCI; __be16 h_vlan_TCI;
...@@ -1320,36 +1318,32 @@ static ssize_t tun_put_user(struct tun_struct *tun, ...@@ -1320,36 +1318,32 @@ static ssize_t tun_put_user(struct tun_struct *tun,
vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto); vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
copy = min_t(int, vlan_offset, len); ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
ret = skb_copy_datagram_const_iovec(skb, 0, iv, copied, copy); if (ret || !iov_iter_count(iter))
len -= copy;
copied += copy;
if (ret || !len)
goto done; goto done;
copy = min_t(int, sizeof(veth), len); ret = copy_to_iter(&veth, sizeof(veth), iter);
ret = memcpy_toiovecend(iv, (void *)&veth, copied, copy); if (ret != sizeof(veth) || !iov_iter_count(iter))
len -= copy;
copied += copy;
if (ret || !len)
goto done; goto done;
} }
skb_copy_datagram_const_iovec(skb, vlan_offset, iv, copied, len); skb_copy_datagram_iter(skb, vlan_offset, iter, skb->len - vlan_offset);
done: done:
tun->dev->stats.tx_packets++; tun->dev->stats.tx_packets++;
tun->dev->stats.tx_bytes += len; tun->dev->stats.tx_bytes += skb->len + vlan_hlen;
return total; return total;
} }
static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
const struct iovec *iv, ssize_t len, int noblock) const struct iovec *iv, unsigned long segs,
ssize_t len, int noblock)
{ {
struct sk_buff *skb; struct sk_buff *skb;
ssize_t ret = 0; ssize_t ret = 0;
int peeked, err, off = 0; int peeked, err, off = 0;
struct iov_iter iter;
tun_debug(KERN_INFO, tun, "tun_do_read\n"); tun_debug(KERN_INFO, tun, "tun_do_read\n");
...@@ -1362,11 +1356,12 @@ static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, ...@@ -1362,11 +1356,12 @@ static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
/* Read frames from queue */ /* Read frames from queue */
skb = __skb_recv_datagram(tfile->socket.sk, noblock ? MSG_DONTWAIT : 0, skb = __skb_recv_datagram(tfile->socket.sk, noblock ? MSG_DONTWAIT : 0,
&peeked, &off, &err); &peeked, &off, &err);
if (skb) { if (!skb)
ret = tun_put_user(tun, tfile, skb, iv, len); return ret;
kfree_skb(skb);
} else iov_iter_init(&iter, READ, iv, segs, len);
ret = err; ret = tun_put_user(tun, tfile, skb, &iter);
kfree_skb(skb);
return ret; return ret;
} }
...@@ -1387,7 +1382,7 @@ static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv, ...@@ -1387,7 +1382,7 @@ static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
goto out; goto out;
} }
ret = tun_do_read(tun, tfile, iv, len, ret = tun_do_read(tun, tfile, iv, count, len,
file->f_flags & O_NONBLOCK); file->f_flags & O_NONBLOCK);
ret = min_t(ssize_t, ret, len); ret = min_t(ssize_t, ret, len);
if (ret > 0) if (ret > 0)
...@@ -1488,7 +1483,7 @@ static int tun_recvmsg(struct kiocb *iocb, struct socket *sock, ...@@ -1488,7 +1483,7 @@ static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
SOL_PACKET, TUN_TX_TIMESTAMP); SOL_PACKET, TUN_TX_TIMESTAMP);
goto out; goto out;
} }
ret = tun_do_read(tun, tfile, m->msg_iov, total_len, ret = tun_do_read(tun, tfile, m->msg_iov, m->msg_iovlen, total_len,
flags & MSG_DONTWAIT); flags & MSG_DONTWAIT);
if (ret > total_len) { if (ret > total_len) {
m->msg_flags |= MSG_TRUNC; m->msg_flags |= MSG_TRUNC;
......
...@@ -150,6 +150,7 @@ ...@@ -150,6 +150,7 @@
struct net_device; struct net_device;
struct scatterlist; struct scatterlist;
struct pipe_inode_info; struct pipe_inode_info;
struct iov_iter;
#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
struct nf_conntrack { struct nf_conntrack {
...@@ -2650,9 +2651,8 @@ int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset, ...@@ -2650,9 +2651,8 @@ int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset,
int len); int len);
int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *frm, int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *frm,
int offset, size_t count); int offset, size_t count);
int skb_copy_datagram_const_iovec(const struct sk_buff *from, int offset, int skb_copy_datagram_iter(const struct sk_buff *from, int offset,
const struct iovec *to, int to_offset, struct iov_iter *to, int size);
int size);
void skb_free_datagram(struct sock *sk, struct sk_buff *skb); void skb_free_datagram(struct sock *sk, struct sk_buff *skb);
void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb); void skb_free_datagram_locked(struct sock *sk, struct sk_buff *skb);
int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags); int skb_kill_datagram(struct sock *sk, struct sk_buff *skb, unsigned int flags);
......
...@@ -49,6 +49,7 @@ ...@@ -49,6 +49,7 @@
#include <linux/spinlock.h> #include <linux/spinlock.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/pagemap.h> #include <linux/pagemap.h>
#include <linux/uio.h>
#include <net/protocol.h> #include <net/protocol.h>
#include <linux/skbuff.h> #include <linux/skbuff.h>
...@@ -393,34 +394,30 @@ int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset, ...@@ -393,34 +394,30 @@ int skb_copy_datagram_iovec(const struct sk_buff *skb, int offset,
EXPORT_SYMBOL(skb_copy_datagram_iovec); EXPORT_SYMBOL(skb_copy_datagram_iovec);
/** /**
* skb_copy_datagram_const_iovec - Copy a datagram to an iovec. * skb_copy_datagram_iter - Copy a datagram to an iovec iterator.
* @skb: buffer to copy * @skb: buffer to copy
* @offset: offset in the buffer to start copying from * @offset: offset in the buffer to start copying from
* @to: io vector to copy to * @to: iovec iterator to copy to
* @to_offset: offset in the io vector to start copying to
* @len: amount of data to copy from buffer to iovec * @len: amount of data to copy from buffer to iovec
*
* Returns 0 or -EFAULT.
* Note: the iovec is not modified during the copy.
*/ */
int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset, int skb_copy_datagram_iter(const struct sk_buff *skb, int offset,
const struct iovec *to, int to_offset, struct iov_iter *to, int len)
int len)
{ {
int start = skb_headlen(skb); int start = skb_headlen(skb);
int i, copy = start - offset; int i, copy = start - offset;
struct sk_buff *frag_iter; struct sk_buff *frag_iter;
trace_skb_copy_datagram_iovec(skb, len);
/* Copy header. */ /* Copy header. */
if (copy > 0) { if (copy > 0) {
if (copy > len) if (copy > len)
copy = len; copy = len;
if (memcpy_toiovecend(to, skb->data + offset, to_offset, copy)) if (copy_to_iter(skb->data + offset, copy, to) != copy)
goto fault; goto short_copy;
if ((len -= copy) == 0) if ((len -= copy) == 0)
return 0; return 0;
offset += copy; offset += copy;
to_offset += copy;
} }
/* Copy paged appendix. Hmm... why does this look so complicated? */ /* Copy paged appendix. Hmm... why does this look so complicated? */
...@@ -432,22 +429,15 @@ int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset, ...@@ -432,22 +429,15 @@ int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset,
end = start + skb_frag_size(frag); end = start + skb_frag_size(frag);
if ((copy = end - offset) > 0) { if ((copy = end - offset) > 0) {
int err;
u8 *vaddr;
struct page *page = skb_frag_page(frag);
if (copy > len) if (copy > len)
copy = len; copy = len;
vaddr = kmap(page); if (copy_page_to_iter(skb_frag_page(frag),
err = memcpy_toiovecend(to, vaddr + frag->page_offset + frag->page_offset + offset -
offset - start, to_offset, copy); start, copy, to) != copy)
kunmap(page); goto short_copy;
if (err)
goto fault;
if (!(len -= copy)) if (!(len -= copy))
return 0; return 0;
offset += copy; offset += copy;
to_offset += copy;
} }
start = end; start = end;
} }
...@@ -461,25 +451,33 @@ int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset, ...@@ -461,25 +451,33 @@ int skb_copy_datagram_const_iovec(const struct sk_buff *skb, int offset,
if ((copy = end - offset) > 0) { if ((copy = end - offset) > 0) {
if (copy > len) if (copy > len)
copy = len; copy = len;
if (skb_copy_datagram_const_iovec(frag_iter, if (skb_copy_datagram_iter(frag_iter, offset - start,
offset - start, to, copy))
to, to_offset,
copy))
goto fault; goto fault;
if ((len -= copy) == 0) if ((len -= copy) == 0)
return 0; return 0;
offset += copy; offset += copy;
to_offset += copy;
} }
start = end; start = end;
} }
if (!len) if (!len)
return 0; return 0;
/* This is not really a user copy fault, but rather someone
* gave us a bogus length on the skb. We should probably
* print a warning here as it may indicate a kernel bug.
*/
fault: fault:
return -EFAULT; return -EFAULT;
short_copy:
if (iov_iter_count(to))
goto fault;
return 0;
} }
EXPORT_SYMBOL(skb_copy_datagram_const_iovec); EXPORT_SYMBOL(skb_copy_datagram_iter);
/** /**
* skb_copy_datagram_from_iovec - Copy a datagram from an iovec. * skb_copy_datagram_from_iovec - Copy a datagram from an iovec.
......
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