Skip to content

Commit

Permalink
Merge branch 'iov_iter'
Browse files Browse the repository at this point in the history
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: David S. Miller <davem@davemloft.net>
  • Loading branch information
David S. Miller committed Nov 7, 2014
2 parents 4e84b49 + bfe1be3 commit 7f6d467
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 92 deletions.
46 changes: 21 additions & 25 deletions drivers/net/macvtap.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <linux/cdev.h>
#include <linux/idr.h>
#include <linux/fs.h>
#include <linux/uio.h>

#include <net/ipv6.h>
#include <net/net_namespace.h>
Expand Down Expand Up @@ -778,31 +779,29 @@ static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv,
/* Put packet to the user space buffer */
static ssize_t macvtap_put_user(struct macvtap_queue *q,
const struct sk_buff *skb,
const struct iovec *iv, int len)
struct iov_iter *iter)
{
int ret;
int vnet_hdr_len = 0;
int vlan_offset = 0;
int copied, total;
int total;

if (q->flags & IFF_VNET_HDR) {
struct virtio_net_hdr vnet_hdr;
vnet_hdr_len = q->vnet_hdr_sz;
if ((len -= vnet_hdr_len) < 0)
if (iov_iter_count(iter) < vnet_hdr_len)
return -EINVAL;

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;
}
total = copied = vnet_hdr_len;
total = vnet_hdr_len;
total += skb->len;

if (!vlan_tx_tag_present(skb))
len = min_t(int, skb->len, len);
else {
int copy;
if (vlan_tx_tag_present(skb)) {
struct {
__be16 h_vlan_proto;
__be16 h_vlan_TCI;
Expand All @@ -811,37 +810,33 @@ static ssize_t macvtap_put_user(struct macvtap_queue *q,
veth.h_vlan_TCI = htons(vlan_tx_tag_get(skb));

vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);
len = min_t(int, skb->len + VLAN_HLEN, len);
total += VLAN_HLEN;

copy = min_t(int, vlan_offset, len);
ret = skb_copy_datagram_const_iovec(skb, 0, iv, copied, copy);
len -= copy;
copied += copy;
if (ret || !len)
ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
if (ret || !iov_iter_count(iter))
goto done;

copy = min_t(int, sizeof(veth), len);
ret = memcpy_toiovecend(iv, (void *)&veth, copied, copy);
len -= copy;
copied += copy;
if (ret || !len)
ret = copy_to_iter(&veth, sizeof(veth), iter);
if (ret != sizeof(veth) || !iov_iter_count(iter))
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:
return ret ? ret : total;
}

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)
{
DEFINE_WAIT(wait);
struct sk_buff *skb;
ssize_t ret = 0;
struct iov_iter iter;

while (len) {
if (!noblock)
Expand All @@ -863,7 +858,8 @@ static ssize_t macvtap_do_read(struct macvtap_queue *q,
schedule();
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);
break;
}
Expand All @@ -886,7 +882,7 @@ static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv,
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);
if (ret > 0)
iocb->ki_pos = ret;
Expand Down Expand Up @@ -1117,7 +1113,7 @@ static int macvtap_recvmsg(struct kiocb *iocb, struct socket *sock,
int ret;
if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
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);
if (ret > total_len) {
m->msg_flags |= MSG_TRUNC;
Expand Down
65 changes: 30 additions & 35 deletions drivers/net/tun.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
#include <net/rtnetlink.h>
#include <net/sock.h>
#include <linux/seq_file.h>
#include <linux/uio.h>

#include <asm/uaccess.h>

Expand Down Expand Up @@ -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,
struct tun_file *tfile,
struct sk_buff *skb,
const struct iovec *iv, int len)
struct iov_iter *iter)
{
struct tun_pi pi = { 0, skb->protocol };
ssize_t total = 0;
int vlan_offset = 0, copied;
ssize_t total;
int vlan_offset;
int vlan_hlen = 0;
int vnet_hdr_sz = 0;

Expand All @@ -1244,23 +1245,25 @@ static ssize_t tun_put_user(struct tun_struct *tun,
if (tun->flags & TUN_VNET_HDR)
vnet_hdr_sz = tun->vnet_hdr_sz;

total = skb->len + vlan_hlen + vnet_hdr_sz;

if (!(tun->flags & TUN_NO_PI)) {
if ((len -= sizeof(pi)) < 0)
if (iov_iter_count(iter) < sizeof(pi))
return -EINVAL;

if (len < skb->len + vlan_hlen + vnet_hdr_sz) {
total += sizeof(pi);
if (iov_iter_count(iter) < total) {
/* Packet will be striped */
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;
total += sizeof(pi);
}

if (vnet_hdr_sz) {
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;

if (skb_is_gso(skb)) {
Expand Down Expand Up @@ -1299,17 +1302,12 @@ static ssize_t tun_put_user(struct tun_struct *tun,
gso.flags = VIRTIO_NET_HDR_F_DATA_VALID;
} /* else everything is zero */

if (unlikely(memcpy_toiovecend(iv, (void *)&gso, total,
sizeof(gso))))
if (copy_to_iter(&gso, sizeof(gso), iter) != sizeof(gso))
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) {
int copy, ret;
int ret;
struct {
__be16 h_vlan_proto;
__be16 h_vlan_TCI;
Expand All @@ -1320,36 +1318,32 @@ static ssize_t tun_put_user(struct tun_struct *tun,

vlan_offset = offsetof(struct vlan_ethhdr, h_vlan_proto);

copy = min_t(int, vlan_offset, len);
ret = skb_copy_datagram_const_iovec(skb, 0, iv, copied, copy);
len -= copy;
copied += copy;
if (ret || !len)
ret = skb_copy_datagram_iter(skb, 0, iter, vlan_offset);
if (ret || !iov_iter_count(iter))
goto done;

copy = min_t(int, sizeof(veth), len);
ret = memcpy_toiovecend(iv, (void *)&veth, copied, copy);
len -= copy;
copied += copy;
if (ret || !len)
ret = copy_to_iter(&veth, sizeof(veth), iter);
if (ret != sizeof(veth) || !iov_iter_count(iter))
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:
tun->dev->stats.tx_packets++;
tun->dev->stats.tx_bytes += len;
tun->dev->stats.tx_bytes += skb->len + vlan_hlen;

return total;
}

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;
ssize_t ret = 0;
int peeked, err, off = 0;
struct iov_iter iter;

tun_debug(KERN_INFO, tun, "tun_do_read\n");

Expand All @@ -1362,11 +1356,12 @@ static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile,
/* Read frames from queue */
skb = __skb_recv_datagram(tfile->socket.sk, noblock ? MSG_DONTWAIT : 0,
&peeked, &off, &err);
if (skb) {
ret = tun_put_user(tun, tfile, skb, iv, len);
kfree_skb(skb);
} else
ret = err;
if (!skb)
return ret;

iov_iter_init(&iter, READ, iv, segs, len);
ret = tun_put_user(tun, tfile, skb, &iter);
kfree_skb(skb);

return ret;
}
Expand All @@ -1387,7 +1382,7 @@ static ssize_t tun_chr_aio_read(struct kiocb *iocb, const struct iovec *iv,
goto out;
}

ret = tun_do_read(tun, tfile, iv, len,
ret = tun_do_read(tun, tfile, iv, count, len,
file->f_flags & O_NONBLOCK);
ret = min_t(ssize_t, ret, len);
if (ret > 0)
Expand Down Expand Up @@ -1488,7 +1483,7 @@ static int tun_recvmsg(struct kiocb *iocb, struct socket *sock,
SOL_PACKET, TUN_TX_TIMESTAMP);
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);
if (ret > total_len) {
m->msg_flags |= MSG_TRUNC;
Expand Down
6 changes: 3 additions & 3 deletions include/linux/skbuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
struct net_device;
struct scatterlist;
struct pipe_inode_info;
struct iov_iter;

#if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
struct nf_conntrack {
Expand Down Expand Up @@ -2650,9 +2651,8 @@ int skb_copy_datagram_from_iovec(struct sk_buff *skb, int offset,
int len);
int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *frm,
int offset, size_t count);
int skb_copy_datagram_const_iovec(const struct sk_buff *from, int offset,
const struct iovec *to, int to_offset,
int size);
int skb_copy_datagram_iter(const struct sk_buff *from, int offset,
struct iov_iter *to, int size);
void skb_free_datagram(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);
Expand Down
Loading

0 comments on commit 7f6d467

Please sign in to comment.