Commit ab00af85 authored by Chuang Wang's avatar Chuang Wang Committed by David S. Miller

net: tun: rebuild error handling in tun_get_user

The error handling in tun_get_user is very scattered.
This patch unifies error handling, reduces duplication of code, and
makes the logic clearer.
Signed-off-by: default avatarChuang Wang <nashuiliang@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent b548b17a
...@@ -1746,7 +1746,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, ...@@ -1746,7 +1746,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
u32 rxhash = 0; u32 rxhash = 0;
int skb_xdp = 1; int skb_xdp = 1;
bool frags = tun_napi_frags_enabled(tfile); bool frags = tun_napi_frags_enabled(tfile);
enum skb_drop_reason drop_reason; enum skb_drop_reason drop_reason = SKB_DROP_REASON_NOT_SPECIFIED;
if (!(tun->flags & IFF_NO_PI)) { if (!(tun->flags & IFF_NO_PI)) {
if (len < sizeof(pi)) if (len < sizeof(pi))
...@@ -1807,10 +1807,9 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, ...@@ -1807,10 +1807,9 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
* skb was created with generic XDP routine. * skb was created with generic XDP routine.
*/ */
skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp); skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp);
if (IS_ERR(skb)) { err = PTR_ERR_OR_ZERO(skb);
dev_core_stats_rx_dropped_inc(tun->dev); if (err)
return PTR_ERR(skb); goto drop;
}
if (!skb) if (!skb)
return total_len; return total_len;
} else { } else {
...@@ -1835,13 +1834,9 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, ...@@ -1835,13 +1834,9 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
noblock); noblock);
} }
if (IS_ERR(skb)) { err = PTR_ERR_OR_ZERO(skb);
if (PTR_ERR(skb) != -EAGAIN) if (err)
dev_core_stats_rx_dropped_inc(tun->dev); goto drop;
if (frags)
mutex_unlock(&tfile->napi_mutex);
return PTR_ERR(skb);
}
if (zerocopy) if (zerocopy)
err = zerocopy_sg_from_iter(skb, from); err = zerocopy_sg_from_iter(skb, from);
...@@ -1851,27 +1846,14 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, ...@@ -1851,27 +1846,14 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
if (err) { if (err) {
err = -EFAULT; err = -EFAULT;
drop_reason = SKB_DROP_REASON_SKB_UCOPY_FAULT; drop_reason = SKB_DROP_REASON_SKB_UCOPY_FAULT;
drop: goto drop;
dev_core_stats_rx_dropped_inc(tun->dev);
kfree_skb_reason(skb, drop_reason);
if (frags) {
tfile->napi.skb = NULL;
mutex_unlock(&tfile->napi_mutex);
}
return err;
} }
} }
if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) { if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) {
atomic_long_inc(&tun->rx_frame_errors); atomic_long_inc(&tun->rx_frame_errors);
kfree_skb(skb); err = -EINVAL;
if (frags) { goto free_skb;
tfile->napi.skb = NULL;
mutex_unlock(&tfile->napi_mutex);
}
return -EINVAL;
} }
switch (tun->flags & TUN_TYPE_MASK) { switch (tun->flags & TUN_TYPE_MASK) {
...@@ -1887,9 +1869,8 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, ...@@ -1887,9 +1869,8 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
pi.proto = htons(ETH_P_IPV6); pi.proto = htons(ETH_P_IPV6);
break; break;
default: default:
dev_core_stats_rx_dropped_inc(tun->dev); err = -EINVAL;
kfree_skb(skb); goto drop;
return -EINVAL;
} }
} }
...@@ -1931,11 +1912,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, ...@@ -1931,11 +1912,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
if (ret != XDP_PASS) { if (ret != XDP_PASS) {
rcu_read_unlock(); rcu_read_unlock();
local_bh_enable(); local_bh_enable();
if (frags) { goto unlock_frags;
tfile->napi.skb = NULL;
mutex_unlock(&tfile->napi_mutex);
}
return total_len;
} }
} }
rcu_read_unlock(); rcu_read_unlock();
...@@ -2015,6 +1992,22 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, ...@@ -2015,6 +1992,22 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
tun_flow_update(tun, rxhash, tfile); tun_flow_update(tun, rxhash, tfile);
return total_len; return total_len;
drop:
if (err != -EAGAIN)
dev_core_stats_rx_dropped_inc(tun->dev);
free_skb:
if (!IS_ERR_OR_NULL(skb))
kfree_skb_reason(skb, drop_reason);
unlock_frags:
if (frags) {
tfile->napi.skb = NULL;
mutex_unlock(&tfile->napi_mutex);
}
return err ?: total_len;
} }
static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from) static ssize_t tun_chr_write_iter(struct kiocb *iocb, struct iov_iter *from)
......
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