@@ -1012,36 +1012,38 @@ static struct sk_buff *tun_alloc_skb(struct tun_file *tfile,
1012
1012
1013
1013
/* Get packet from user space buffer */
1014
1014
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 )
1017
1017
{
1018
1018
struct tun_pi pi = { 0 , cpu_to_be16 (ETH_P_IP ) };
1019
1019
struct sk_buff * skb ;
1020
+ size_t total_len = iov_iter_count (from );
1020
1021
size_t len = total_len , align = NET_SKB_PAD , linear ;
1021
1022
struct virtio_net_hdr gso = { 0 };
1022
1023
int good_linear ;
1023
- int offset = 0 ;
1024
1024
int copylen ;
1025
1025
bool zerocopy = false;
1026
1026
int err ;
1027
1027
u32 rxhash ;
1028
+ ssize_t n ;
1028
1029
1029
1030
if (!(tun -> flags & TUN_NO_PI )) {
1030
1031
if (len < sizeof (pi ))
1031
1032
return - EINVAL ;
1032
1033
len -= sizeof (pi );
1033
1034
1034
- if (memcpy_fromiovecend ((void * )& pi , iv , 0 , sizeof (pi )))
1035
+ n = copy_from_iter (& pi , sizeof (pi ), from );
1036
+ if (n != sizeof (pi ))
1035
1037
return - EFAULT ;
1036
- offset += sizeof (pi );
1037
1038
}
1038
1039
1039
1040
if (tun -> flags & TUN_VNET_HDR ) {
1040
1041
if (len < tun -> vnet_hdr_sz )
1041
1042
return - EINVAL ;
1042
1043
len -= tun -> vnet_hdr_sz ;
1043
1044
1044
- if (memcpy_fromiovecend ((void * )& gso , iv , offset , sizeof (gso )))
1045
+ n = copy_from_iter (& gso , sizeof (gso ), from );
1046
+ if (n != sizeof (gso ))
1045
1047
return - EFAULT ;
1046
1048
1047
1049
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
1052
1051
1053
if (gso .hdr_len > len )
1052
1054
return - EINVAL ;
1053
- offset += tun -> vnet_hdr_sz ;
1055
+ iov_iter_advance ( from , tun -> vnet_hdr_sz ) ;
1054
1056
}
1055
1057
1056
1058
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
1065
good_linear = SKB_MAX_HEAD (align );
1064
1066
1065
1067
if (msg_control ) {
1068
+ struct iov_iter i = * from ;
1069
+
1066
1070
/* There are 256 bytes to be copied in skb, so there is
1067
1071
* enough room for skb expand head in case it is used.
1068
1072
* 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
1075
if (copylen > good_linear )
1072
1076
copylen = good_linear ;
1073
1077
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 )
1075
1080
zerocopy = true;
1076
1081
}
1077
1082
@@ -1091,9 +1096,9 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
1091
1096
}
1092
1097
1093
1098
if (zerocopy )
1094
- err = zerocopy_sg_from_iovec (skb , iv , offset , count );
1099
+ err = zerocopy_sg_from_iter (skb , from );
1095
1100
else {
1096
- err = skb_copy_datagram_from_iovec (skb , 0 , iv , offset , len );
1101
+ err = skb_copy_datagram_from_iter (skb , 0 , from , len );
1097
1102
if (!err && msg_control ) {
1098
1103
struct ubuf_info * uarg = msg_control ;
1099
1104
uarg -> callback (uarg , false);
@@ -1207,8 +1212,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
1207
1212
return total_len ;
1208
1213
}
1209
1214
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 )
1212
1216
{
1213
1217
struct file * file = iocb -> ki_filp ;
1214
1218
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
1222
if (!tun )
1219
1223
return - EBADFD ;
1220
1224
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 );
1225
1226
1226
1227
tun_put (tun );
1227
1228
return result ;
@@ -1445,11 +1446,14 @@ static int tun_sendmsg(struct kiocb *iocb, struct socket *sock,
1445
1446
int ret ;
1446
1447
struct tun_file * tfile = container_of (sock , struct tun_file , socket );
1447
1448
struct tun_struct * tun = __tun_get (tfile );
1449
+ struct iov_iter from ;
1448
1450
1449
1451
if (!tun )
1450
1452
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 );
1453
1457
tun_put (tun );
1454
1458
return ret ;
1455
1459
}
@@ -2233,9 +2237,9 @@ static const struct file_operations tun_fops = {
2233
2237
.owner = THIS_MODULE ,
2234
2238
.llseek = no_llseek ,
2235
2239
.read = new_sync_read ,
2240
+ .write = new_sync_write ,
2236
2241
.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 ,
2239
2243
.poll = tun_chr_poll ,
2240
2244
.unlocked_ioctl = tun_chr_ioctl ,
2241
2245
#ifdef CONFIG_COMPAT
0 commit comments