Skip to content

Commit 1cb36e2

Browse files
stefano-garzarellaPaolo Abeni
authored andcommitted
vsock/virtio: fix MSG_ZEROCOPY pinned-pages accounting
virtio_transport_init_zcopy_skb() uses iter->count as the size argument for msg_zerocopy_realloc(), which in turn passes it to mm_account_pinned_pages() for RLIMIT_MEMLOCK accounting. However, this function is called after virtio_transport_fill_skb() has already consumed the iterator via __zerocopy_sg_from_iter(), so on the last skb, iter->count will be 0, skipping the RLIMIT_MEMLOCK enforcement. Pass pkt_len (the total bytes being sent) as an explicit parameter to virtio_transport_init_zcopy_skb() instead of reading the already-consumed iter->count. This matches TCP and UDP, which both call msg_zerocopy_realloc() with the original message size. Fixes: 581512a ("vsock/virtio: MSG_ZEROCOPY flag support") Reported-by: Yiming Qian <yimingqian591@gmail.com> Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> Reviewed-by: Bobby Eshleman <bobbyeshleman@meta.com> Link: https://patch.msgid.link/20260420132051.217589-1-sgarzare@redhat.com Signed-off-by: Paolo Abeni <pabeni@redhat.com>
1 parent 42ea37b commit 1cb36e2

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

net/vmw_vsock/virtio_transport_common.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ static bool virtio_transport_can_zcopy(const struct virtio_transport *t_ops,
7373
static int virtio_transport_init_zcopy_skb(struct vsock_sock *vsk,
7474
struct sk_buff *skb,
7575
struct msghdr *msg,
76+
size_t pkt_len,
7677
bool zerocopy)
7778
{
7879
struct ubuf_info *uarg;
@@ -81,12 +82,10 @@ static int virtio_transport_init_zcopy_skb(struct vsock_sock *vsk,
8182
uarg = msg->msg_ubuf;
8283
net_zcopy_get(uarg);
8384
} else {
84-
struct iov_iter *iter = &msg->msg_iter;
8585
struct ubuf_info_msgzc *uarg_zc;
8686

8787
uarg = msg_zerocopy_realloc(sk_vsock(vsk),
88-
iter->count,
89-
NULL, false);
88+
pkt_len, NULL, false);
9089
if (!uarg)
9190
return -1;
9291

@@ -398,11 +397,17 @@ static int virtio_transport_send_pkt_info(struct vsock_sock *vsk,
398397
* each iteration. If this is last skb for this buffer
399398
* and MSG_ZEROCOPY mode is in use - we must allocate
400399
* completion for the current syscall.
400+
*
401+
* Pass pkt_len because msg iter is already consumed
402+
* by virtio_transport_fill_skb(), so iter->count
403+
* can not be used for RLIMIT_MEMLOCK pinned-pages
404+
* accounting done by msg_zerocopy_realloc().
401405
*/
402406
if (info->msg && info->msg->msg_flags & MSG_ZEROCOPY &&
403407
skb_len == rest_len && info->op == VIRTIO_VSOCK_OP_RW) {
404408
if (virtio_transport_init_zcopy_skb(vsk, skb,
405409
info->msg,
410+
pkt_len,
406411
can_zcopy)) {
407412
kfree_skb(skb);
408413
ret = -ENOMEM;

0 commit comments

Comments
 (0)