From nobody Tue Oct 7 03:46:26 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5AE342627FC; Mon, 14 Jul 2025 15:21:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752506475; cv=none; b=fSa5Kvq1bMZIYcXh1YG7n0z+ai0UhUDWzGBMZ3CV4yNcnLRDW89GaUbVgfFuAwVt/kgnMpzTSyrOGHjXnfVRPwFIwTeYLPRiqdTcTKihF+nKyRzKLq3DsyCivIlSLJOvBgzkTyVrP2KWq4neUrtYs7O/hQfZaL5+n7vmpGKT6nI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752506475; c=relaxed/simple; bh=iISDn9L+D3wVAHPNNIcp/TCqX/3WWeX/S60myFyZsyQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fMhqXtVCahyKYTjYBtN+0DhSX/wicKEzjtNzt8CFSI/oZaJzet9mpmwvglDsFaf0/HQ0qq4fckrlR8eQN//Q2sZXgISL7y35mYsZwpAKZSXvpzfE0/tdC52Agr0rTEDyT/Ha9WAKooZaRSOgqLm2uK6iFn1YUEo16RPYd+qOYiw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=cU9/P+kD; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="cU9/P+kD" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7E5BEC4CEF0; Mon, 14 Jul 2025 15:21:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752506474; bh=iISDn9L+D3wVAHPNNIcp/TCqX/3WWeX/S60myFyZsyQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cU9/P+kDTb3VLqs+GQ/BAkKRBw8CyTjCxzTm4rbVmE9cIWhZ4tsKrFOnsr/NQYfBB ybyjk5TUFAgVXnNH5DpiiB9cB9aiXnwWWyjwksYKFN8scGaawPY3rouhHjfngKZXl+ xAXw9D7iQh0nCLmI/e6D3/vW0qw+E6ZFx7vLFPmiAqMZpUvF3W5Gk4vexxtA/PEJZw S3/nPh1QtzuTAnr4ghzxr5/F32KE4W5EG9q5GWuJ+VArWqTPkBP/4LFSJAklps2/Fm UXucZZKDy20diWMOfEexVZv+Qhp7VlnBD7gSfyrBbOH4sNzQEq8rJQIU12aQ5/Szyt tJ1A+drGgRUoQ== From: Will Deacon To: linux-kernel@vger.kernel.org Cc: Will Deacon , Keir Fraser , Steven Moreland , Frederick Mayle , Stefan Hajnoczi , Stefano Garzarella , "Michael S. Tsirkin" , Jason Wang , =?UTF-8?q?Eugenio=20P=C3=A9rez?= , netdev@vger.kernel.org, virtualization@lists.linux.dev, stable@vger.kernel.org Subject: [PATCH v3 1/9] vhost/vsock: Avoid allocating arbitrarily-sized SKBs Date: Mon, 14 Jul 2025 16:20:55 +0100 Message-Id: <20250714152103.6949-2-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250714152103.6949-1-will@kernel.org> References: <20250714152103.6949-1-will@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" vhost_vsock_alloc_skb() returns NULL for packets advertising a length larger than VIRTIO_VSOCK_MAX_PKT_BUF_SIZE in the packet header. However, this is only checked once the SKB has been allocated and, if the length in the packet header is zero, the SKB may not be freed immediately. Hoist the size check before the SKB allocation so that an iovec larger than VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + the header size is rejected outright. The subsequent check on the length field in the header can then simply check that the allocated SKB is indeed large enough to hold the packet. Cc: Fixes: 71dc9ec9ac7d ("virtio/vsock: replace virtio_vsock_pkt with sk_buff") Signed-off-by: Will Deacon Reviewed-by: Stefano Garzarella --- drivers/vhost/vsock.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index 802153e23073..66a0f060770e 100644 --- a/drivers/vhost/vsock.c +++ b/drivers/vhost/vsock.c @@ -344,6 +344,9 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq, =20 len =3D iov_length(vq->iov, out); =20 + if (len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM) + return NULL; + /* len contains both payload and hdr */ skb =3D virtio_vsock_alloc_skb(len, GFP_KERNEL); if (!skb) @@ -367,8 +370,7 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq, return skb; =20 /* The pkt is too big or the length in the header is invalid */ - if (payload_len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE || - payload_len + sizeof(*hdr) > len) { + if (payload_len + sizeof(*hdr) > len) { kfree_skb(skb); return NULL; } --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 03:46:26 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E7A0126462E; Mon, 14 Jul 2025 15:21:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752506478; cv=none; b=FXyRlehl6IMFJAa5ce/xEe6sIld/jjpFw3zArHvrqLv5+11QtcLo5jku701s507lUOt+lDppAlVHAfWc1k9O8s2XsqhTbO0N/EhDrw1wGnisvZNkVSUEdA3VjPj+EmmQT7CiwEX6LLn/8YCgFBmue2KhaAdK/ilqlgNUzq3Z21s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752506478; c=relaxed/simple; bh=oKf208jR2snmwBDg9o+AlVnnid4IgiStTjfyVYgKtp0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ApipDRbO4+9PfsDP4mHV5yVOhkSpcpldQtBAimodG/FSyCztYbdE4w8rNQulmBaAC6wQbFsk9inuVgEFF2k3b97aMhwzxZk2Nsv5nLTK5/wHFqid2+EGyj+HUy2sxOVCv7uhJl/UNFAy21NGwx0NcsHaawkmnkMRokpJ1dLsHFk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BFQcd8iz; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="BFQcd8iz" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 60278C4CEF5; Mon, 14 Jul 2025 15:21:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752506477; bh=oKf208jR2snmwBDg9o+AlVnnid4IgiStTjfyVYgKtp0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BFQcd8izY8tenB38W6DzhOcXP2i3ZQLYLWqpDlBlnBQIhbyiBhsJqUo/uuhJy62+l n/4mnE90DP+7YeflVKcA4DalJ1BHNPFVENQTgRRj9FYh6g7KeFswzk21m55YrPkTsl NF1S+cwYi4o8qPrf+kBFgBd8fjzUmGg6sxYjOIl35E+sh+IE1DFGyJs9O3cIKjKfeZ vyjNzRguNaVladnJ7HxjhR932uM/L26KqYMoxQiKvfsCT02FFoB1/egVHFICmtPFfi 6sXUDtXTDWp9OG6RlcSTz5wgj1HlNPhaGOvli31yRIJJneOrz0PC9m4jHk/99A91Md DqPegSk3ECUSg== From: Will Deacon To: linux-kernel@vger.kernel.org Cc: Will Deacon , Keir Fraser , Steven Moreland , Frederick Mayle , Stefan Hajnoczi , Stefano Garzarella , "Michael S. Tsirkin" , Jason Wang , =?UTF-8?q?Eugenio=20P=C3=A9rez?= , netdev@vger.kernel.org, virtualization@lists.linux.dev, stable@vger.kernel.org Subject: [PATCH v3 2/9] vsock/virtio: Validate length in packet header before skb_put() Date: Mon, 14 Jul 2025 16:20:56 +0100 Message-Id: <20250714152103.6949-3-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250714152103.6949-1-will@kernel.org> References: <20250714152103.6949-1-will@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When receiving a vsock packet in the guest, only the virtqueue buffer size is validated prior to virtio_vsock_skb_rx_put(). Unfortunately, virtio_vsock_skb_rx_put() uses the length from the packet header as the length argument to skb_put(), potentially resulting in SKB overflow if the host has gone wonky. Validate the length as advertised by the packet header before calling virtio_vsock_skb_rx_put(). Cc: Fixes: 71dc9ec9ac7d ("virtio/vsock: replace virtio_vsock_pkt with sk_buff") Signed-off-by: Will Deacon --- net/vmw_vsock/virtio_transport.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transp= ort.c index f0e48e6911fc..bd2c6aaa1a93 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -624,8 +624,9 @@ static void virtio_transport_rx_work(struct work_struct= *work) do { virtqueue_disable_cb(vq); for (;;) { + unsigned int len, payload_len; + struct virtio_vsock_hdr *hdr; struct sk_buff *skb; - unsigned int len; =20 if (!virtio_transport_more_replies(vsock)) { /* Stop rx until the device processes already @@ -642,12 +643,19 @@ static void virtio_transport_rx_work(struct work_stru= ct *work) vsock->rx_buf_nr--; =20 /* Drop short/long packets */ - if (unlikely(len < sizeof(struct virtio_vsock_hdr) || + if (unlikely(len < sizeof(*hdr) || len > virtio_vsock_skb_len(skb))) { kfree_skb(skb); continue; } =20 + hdr =3D virtio_vsock_hdr(skb); + payload_len =3D le32_to_cpu(hdr->len); + if (payload_len > len - sizeof(*hdr)) { + kfree_skb(skb); + continue; + } + virtio_vsock_skb_rx_put(skb); virtio_transport_deliver_tap_pkt(skb); virtio_transport_recv_pkt(&virtio_transport, skb); --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 03:46:26 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E92C92652AF; Mon, 14 Jul 2025 15:21:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752506481; cv=none; b=nzeoS9ldZ3pb80uIHNgIc5mvCVNSWYcIEZdcjNgaBCaUCotICo+z+9qlNMEQ2gHIxSRM9IN0g3c0jNM5kWl4LwOweoc9n8SJsHCqJFHYzyh5VZphVPSrK33iuoK7bj/4NNPVC0AvpjnMipqLn5j84FmqhLMDPbiI21tvWygSeew= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752506481; c=relaxed/simple; bh=niC6TH6Xk0NRMoecU58eySzzPeyslGYIOlCVhRbWLZk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=aiTs46vn+nsQYf1Ihu99HnFE+GvZKHFmvsuE8X4VhxGzyoATwDn5FJQqObz7DpSsUpdYlbXWhftdi6FrLmssB5VzFzhGWhwEkCJhq/m4JkR5Z5fC31tm0kSj9coJjVb4eXwKVzpwzmqyDTTqfRBsiNK6xUFw1IQ6zh7w0exuC+k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZunlIl11; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ZunlIl11" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 42354C4CEED; Mon, 14 Jul 2025 15:21:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752506480; bh=niC6TH6Xk0NRMoecU58eySzzPeyslGYIOlCVhRbWLZk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZunlIl114m7pBc1swV0/emnh3M5q8dONunc7t+dOe1i4uT9fk/kOIiwCFL2XSzhh6 BmGptrslyL8Y/sAESj2vLbRRn7Xvh6ROKlYi8xu7DBNddbe6IwJ7oHp9oBTQbprLq6 2zvzgsrM7KsuRiseBoVcyozwzBplBWPCt9uJoFPTkZRMiQs0hbk2KzqkAQtkBhmusR 4/oqfMHj4RvvozFgGdPb2vjnXHX3YUCE1ZdPVyQ6gQH1ZA9XbImsi+kj+z5VBeLLe7 0VFMtc3NAm5Zwo3Xex7EAEXgs88wvtoeOHToxJ07eTCuqfPgj5nX235wkhkqotgWW+ YbkOocXJ9Ub9Q== From: Will Deacon To: linux-kernel@vger.kernel.org Cc: Will Deacon , Keir Fraser , Steven Moreland , Frederick Mayle , Stefan Hajnoczi , Stefano Garzarella , "Michael S. Tsirkin" , Jason Wang , =?UTF-8?q?Eugenio=20P=C3=A9rez?= , netdev@vger.kernel.org, virtualization@lists.linux.dev Subject: [PATCH v3 3/9] vsock/virtio: Move length check to callers of virtio_vsock_skb_rx_put() Date: Mon, 14 Jul 2025 16:20:57 +0100 Message-Id: <20250714152103.6949-4-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250714152103.6949-1-will@kernel.org> References: <20250714152103.6949-1-will@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" virtio_vsock_skb_rx_put() only calls skb_put() if the length in the packet header is not zero even though skb_put() handles this case gracefully. Remove the functionally redundant check from virtio_vsock_skb_rx_put() and, on the assumption that this is a worthwhile optimisation for handling credit messages, augment the existing length checks in virtio_transport_rx_work() to elide the call for zero-length payloads. Since the callers all have the length, extend virtio_vsock_skb_rx_put() to take it as an additional parameter rather than fish it back out of the packet header. Note that the vhost code already has similar logic in vhost_vsock_alloc_skb(). Signed-off-by: Will Deacon Reviewed-by: Stefano Garzarella --- drivers/vhost/vsock.c | 2 +- include/linux/virtio_vsock.h | 9 ++------- net/vmw_vsock/virtio_transport.c | 4 +++- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index 66a0f060770e..4c4a642945eb 100644 --- a/drivers/vhost/vsock.c +++ b/drivers/vhost/vsock.c @@ -375,7 +375,7 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq, return NULL; } =20 - virtio_vsock_skb_rx_put(skb); + virtio_vsock_skb_rx_put(skb, payload_len); =20 nbytes =3D copy_from_iter(skb->data, payload_len, &iov_iter); if (nbytes !=3D payload_len) { diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h index 36fb3edfa403..97465f378ade 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -47,14 +47,9 @@ static inline void virtio_vsock_skb_clear_tap_delivered(= struct sk_buff *skb) VIRTIO_VSOCK_SKB_CB(skb)->tap_delivered =3D false; } =20 -static inline void virtio_vsock_skb_rx_put(struct sk_buff *skb) +static inline void virtio_vsock_skb_rx_put(struct sk_buff *skb, u32 len) { - u32 len; - - len =3D le32_to_cpu(virtio_vsock_hdr(skb)->len); - - if (len > 0) - skb_put(skb, len); + skb_put(skb, len); } =20 static inline struct sk_buff *virtio_vsock_alloc_skb(unsigned int size, gf= p_t mask) diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transp= ort.c index bd2c6aaa1a93..1af7723669cb 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -656,7 +656,9 @@ static void virtio_transport_rx_work(struct work_struct= *work) continue; } =20 - virtio_vsock_skb_rx_put(skb); + if (payload_len) + virtio_vsock_skb_rx_put(skb, payload_len); + virtio_transport_deliver_tap_pkt(skb); virtio_transport_recv_pkt(&virtio_transport, skb); } --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 03:46:26 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ACBB9265CC8; Mon, 14 Jul 2025 15:21:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752506483; cv=none; b=RRN65Q8NQZn38aBkF+yUCyma+/gYmwmtT1H38iRLofFfMwrBYxFvJSrOgLP/3rbJXdkjtTBOQ37oEsG6KFlZAxvybcLMtOmw55a9yrJ4UbiOMvoHmL2WQETc7YS3bjrmGqfA+NjlzESN6X4g+6NWHfzHfotBzGxIRIv1SPfZz/Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752506483; c=relaxed/simple; bh=Vx2pfAKdJSquUFDasHiryHQmmc0i9STm5fwpK+Q3Mek=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KOk0zIj6mUB118UWDVtVso6fQbXxKgfUn/yo8ixcY5+8WuS6syuSI8aM+iM/oajEg6vb32bvwM5S8QyXTuhVxctgmLgPxvRngqxdfFlmoZbPUY2EkpVZ3xyq6z+Xtbc39ba4VKIopZQBj3DhQxVLfiHiVKM9Y/MR1jek+ctAQcg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ayH8CWqp; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ayH8CWqp" Received: by smtp.kernel.org (Postfix) with ESMTPSA id F3724C4CEF0; Mon, 14 Jul 2025 15:21:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752506483; bh=Vx2pfAKdJSquUFDasHiryHQmmc0i9STm5fwpK+Q3Mek=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ayH8CWqp0PPFYMr3lzg5Je9Jm5zHynZG1ds1IS+hLKkInDlfzsuz4fglFwTl2tB8y EOsGnMpTrgpERDQZdMQ3eU/hvdz1gjWA8f1Aqx+F91qL3FvTJF4VBotRaThaKWkJzF TAAJoTKF5n9mMG2ak9cFqQ2o1B1rqdlA269QO3HPRLx9pB2NyIk+6iLNQlgdmWVZ9C t9W6hO0VytBOgh852m+imm3lVNZlIon7Yfalrd/O3Tmryc31enwbkuMP4XPAdhFi1h acYm29cqi566DTGItKXmzom1Zvc6q5048o8/6pvA2967PZypsOdiJKw3eahncCnTXM Vkl5hnYxROzDw== From: Will Deacon To: linux-kernel@vger.kernel.org Cc: Will Deacon , Keir Fraser , Steven Moreland , Frederick Mayle , Stefan Hajnoczi , Stefano Garzarella , "Michael S. Tsirkin" , Jason Wang , =?UTF-8?q?Eugenio=20P=C3=A9rez?= , netdev@vger.kernel.org, virtualization@lists.linux.dev Subject: [PATCH v3 4/9] vsock/virtio: Resize receive buffers so that each SKB fits in a 4K page Date: Mon, 14 Jul 2025 16:20:58 +0100 Message-Id: <20250714152103.6949-5-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250714152103.6949-1-will@kernel.org> References: <20250714152103.6949-1-will@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When allocating receive buffers for the vsock virtio RX virtqueue, an SKB is allocated with a 4140 data payload (the 44-byte packet header + VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE). Even when factoring in the SKB overhead, the resulting 8KiB allocation thanks to the rounding in kmalloc_reserve() is wasteful (~3700 unusable bytes) and results in a higher-order page allocation on systems with 4KiB pages just for the sake of a few hundred bytes of packet data. Limit the vsock virtio RX buffers to 4KiB per SKB, resulting in much better memory utilisation and removing the need to allocate higher-order pages entirely. Signed-off-by: Will Deacon Reviewed-by: Stefano Garzarella --- include/linux/virtio_vsock.h | 7 ++++++- net/vmw_vsock/virtio_transport.c | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h index 97465f378ade..879f1dfa7d3a 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -106,7 +106,12 @@ static inline size_t virtio_vsock_skb_len(struct sk_bu= ff *skb) return (size_t)(skb_end_pointer(skb) - skb->head); } =20 -#define VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE (1024 * 4) +/* Dimension the RX SKB so that the entire thing fits exactly into + * a single 4KiB page. This avoids wasting memory due to alloc_skb() + * rounding up to the next page order and also means that we + * don't leave higher-order pages sitting around in the RX queue. + */ +#define VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE SKB_WITH_OVERHEAD(1024 * 4) #define VIRTIO_VSOCK_MAX_BUF_SIZE 0xFFFFFFFFUL #define VIRTIO_VSOCK_MAX_PKT_BUF_SIZE (1024 * 64) =20 diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transp= ort.c index 1af7723669cb..5416214ae666 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -307,7 +307,7 @@ virtio_transport_cancel_pkt(struct vsock_sock *vsk) =20 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock) { - int total_len =3D VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE + VIRTIO_VSOCK_SKB_HEA= DROOM; + int total_len =3D VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE; struct scatterlist pkt, *p; struct virtqueue *vq; struct sk_buff *skb; --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 03:46:26 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7558C266EF8; Mon, 14 Jul 2025 15:21:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752506486; cv=none; b=jlIEmPvWwjdUPkbFLjoaQ+XZLuuFukCivqyBA2M3AjEQ6HtZtLIguNswpqQ1+R9UP6XZmS7rEYlHU7V49QQxEoBi//hn1ULFws43Qp0sduOT2UhQ+2/Q5f+KQZXL22ZHSR6IBMte/mcjM16p02ZRk8AaVY/1bOG7vXMaWe+dUF0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752506486; c=relaxed/simple; bh=P6/iRHnilU2cPLDO49VvL0Sx0zgXeNiQB3tnoQ0dbsg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OcX1yf90pd+M2JlgzYX39U0uwSG2AZo1roS9wdDbMHSpKIv/O6/ewRURhRY4Zs8rXLn3+EQNZEhWU2+4oHIdxLKglXBgeWF6Y2lN/LZYPjEZlONF1oGvvTVdEE36IQwXCmghtqbKT4JeeSeyRX7t70unW7yIMxwpyWMAvmH89ys= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DtdVMVzb; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DtdVMVzb" Received: by smtp.kernel.org (Postfix) with ESMTPSA id AA9EFC4CEED; Mon, 14 Jul 2025 15:21:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752506485; bh=P6/iRHnilU2cPLDO49VvL0Sx0zgXeNiQB3tnoQ0dbsg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DtdVMVzb78nKywbtGOLWvfVMRhf8it0OFH99Ca3OhGF9n9HGP5cf4IdEoOA6vMBEP ATj6A/tr9ms6LsK8bHFOK5DMDzW/CKh37dDUmMkGRZOo6aT7o658SseoT2OeEtfW9+ 7WIcnqbtayMokKLPor+x9QO1Kwp6TMEIzGgDBe8ovZ+1yqTrTmRiC5ldXwTYxGjx54 MPSG3BsGsBPfV422Mk12BtyqoimBQNrJo4pbg2nwgV4JloHdnra/+itkMNuGr4ZbSN FwUV4i75raSyTQLGHwphVuP76/aTPjKdOVFsD92u/8i7oMKpH2DWK5c3158JWziYOd WJdsLuD5jnLrQ== From: Will Deacon To: linux-kernel@vger.kernel.org Cc: Will Deacon , Keir Fraser , Steven Moreland , Frederick Mayle , Stefan Hajnoczi , Stefano Garzarella , "Michael S. Tsirkin" , Jason Wang , =?UTF-8?q?Eugenio=20P=C3=A9rez?= , netdev@vger.kernel.org, virtualization@lists.linux.dev Subject: [PATCH v3 5/9] vsock/virtio: Rename virtio_vsock_alloc_skb() Date: Mon, 14 Jul 2025 16:20:59 +0100 Message-Id: <20250714152103.6949-6-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250714152103.6949-1-will@kernel.org> References: <20250714152103.6949-1-will@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In preparation for nonlinear allocations for large SKBs, rename virtio_vsock_alloc_skb() to virtio_vsock_alloc_linear_skb() to indicate that it returns linear SKBs unconditionally and switch all callers over to this new interface for now. No functional change. Signed-off-by: Will Deacon Reviewed-by: Stefano Garzarella --- drivers/vhost/vsock.c | 2 +- include/linux/virtio_vsock.h | 3 ++- net/vmw_vsock/virtio_transport.c | 2 +- net/vmw_vsock/virtio_transport_common.c | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index 4c4a642945eb..1ad96613680e 100644 --- a/drivers/vhost/vsock.c +++ b/drivers/vhost/vsock.c @@ -348,7 +348,7 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq, return NULL; =20 /* len contains both payload and hdr */ - skb =3D virtio_vsock_alloc_skb(len, GFP_KERNEL); + skb =3D virtio_vsock_alloc_linear_skb(len, GFP_KERNEL); if (!skb) return NULL; =20 diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h index 879f1dfa7d3a..4504ea29ff82 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -52,7 +52,8 @@ static inline void virtio_vsock_skb_rx_put(struct sk_buff= *skb, u32 len) skb_put(skb, len); } =20 -static inline struct sk_buff *virtio_vsock_alloc_skb(unsigned int size, gf= p_t mask) +static inline struct sk_buff * +virtio_vsock_alloc_linear_skb(unsigned int size, gfp_t mask) { struct sk_buff *skb; =20 diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transp= ort.c index 5416214ae666..c983fd62e37a 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -316,7 +316,7 @@ static void virtio_vsock_rx_fill(struct virtio_vsock *v= sock) vq =3D vsock->vqs[VSOCK_VQ_RX]; =20 do { - skb =3D virtio_vsock_alloc_skb(total_len, GFP_KERNEL); + skb =3D virtio_vsock_alloc_linear_skb(total_len, GFP_KERNEL); if (!skb) break; =20 diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio= _transport_common.c index 1b5d9896edae..c9eb7f7ac00d 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -261,7 +261,7 @@ static struct sk_buff *virtio_transport_alloc_skb(struc= t virtio_vsock_pkt_info * if (!zcopy) skb_len +=3D payload_len; =20 - skb =3D virtio_vsock_alloc_skb(skb_len, GFP_KERNEL); + skb =3D virtio_vsock_alloc_linear_skb(skb_len, GFP_KERNEL); if (!skb) return NULL; =20 --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 03:46:26 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C20AC267701; Mon, 14 Jul 2025 15:21:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752506488; cv=none; b=AGfcfdZax25clPBMcFk3UVKusIpm8h5znLAgSHMpWyOVs2O9Qer0HqmB4mtC5acp57aFEzyNAtpjI6gMSIg+kHR8lT0qcSC4b4wyGF29S8PKEj7r3HKV18AQukggAfBhqfy0CXEldgeoL6Pza0bCvzk2lP361miXJtbWgXP/K6U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752506488; c=relaxed/simple; bh=aXuehvXRYvlandQmY0urJztIprrr/Nm70E9dGNiRXL0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gpL+4ynonczSdjiZp33K6Wd0XU6gUHsnJdUjvVNvj+84VPLDAH2RVAHaxNbtl85Li6SM3YqJS31SAuJb7BVJNR9d6NGVzEumu5oGxbdmhey2hL5RE3R+J/ntsgS4VKMqOveE1q0Mxpa34ined8Xacz716PRyuqpOjcS9mx9NQ1g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=cXNhauAR; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="cXNhauAR" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 64B53C4CEF0; Mon, 14 Jul 2025 15:21:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752506488; bh=aXuehvXRYvlandQmY0urJztIprrr/Nm70E9dGNiRXL0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cXNhauARGx6jZs0Wtlh7QraW7W7BAy2zQ+WYBs6JaMz+vWHg/cCK9OTUoAeZDn0n7 TMvlYK2RZMM/J4AuvWbsTR6eNCMB6A1At9JJBFqcTDV/RZ+vK5D63Po+amFzol5QRi p70lglecPHd8NeOz08HdSOgy2Mw77ICokUOc/D5riJhD6UY15YvyZNHrUKfVLfmfi0 jaRKuruEbXN4h+uC4XFKHDG1yFXuneEV9dQUM82p2NJj2a0jvcv2eMk8yMBqhs8vSo WsyJC0AaFBe4+EseRIaj/WowXpc0ai2OZn0OV85NDsr1yCn2hNZSM8/S2ZacKZeUZI er0k1auGt7KeQ== From: Will Deacon To: linux-kernel@vger.kernel.org Cc: Will Deacon , Keir Fraser , Steven Moreland , Frederick Mayle , Stefan Hajnoczi , Stefano Garzarella , "Michael S. Tsirkin" , Jason Wang , =?UTF-8?q?Eugenio=20P=C3=A9rez?= , netdev@vger.kernel.org, virtualization@lists.linux.dev Subject: [PATCH v3 6/9] vsock/virtio: Move SKB allocation lower-bound check to callers Date: Mon, 14 Jul 2025 16:21:00 +0100 Message-Id: <20250714152103.6949-7-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250714152103.6949-1-will@kernel.org> References: <20250714152103.6949-1-will@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" virtio_vsock_alloc_linear_skb() checks that the requested size is at least big enough for the packet header (VIRTIO_VSOCK_SKB_HEADROOM). Of the three callers of virtio_vsock_alloc_linear_skb(), only vhost_vsock_alloc_skb() can potentially pass a packet smaller than the header size and, as it already has a check against the maximum packet size, extend its bounds checking to consider the minimum packet size and remove the check from virtio_vsock_alloc_linear_skb(). Signed-off-by: Will Deacon Reviewed-by: Stefano Garzarella --- drivers/vhost/vsock.c | 3 ++- include/linux/virtio_vsock.h | 3 --- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index 1ad96613680e..24b7547b05a6 100644 --- a/drivers/vhost/vsock.c +++ b/drivers/vhost/vsock.c @@ -344,7 +344,8 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq, =20 len =3D iov_length(vq->iov, out); =20 - if (len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM) + if (len < VIRTIO_VSOCK_SKB_HEADROOM || + len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM) return NULL; =20 /* len contains both payload and hdr */ diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h index 4504ea29ff82..36dd0cd55368 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -57,9 +57,6 @@ virtio_vsock_alloc_linear_skb(unsigned int size, gfp_t ma= sk) { struct sk_buff *skb; =20 - if (size < VIRTIO_VSOCK_SKB_HEADROOM) - return NULL; - skb =3D alloc_skb(size, mask); if (!skb) return NULL; --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 03:46:26 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8AED0268684; Mon, 14 Jul 2025 15:21:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752506491; cv=none; b=oQI9ePMCobPCkv4HlK5eMX06zJFuS4ak0Qzv55ObnMLLxwq/ysq1ZbNa3OMFf824Ya9nLAf4ewv6hbsZdGI9PuJxYWMkputy2EPnqvC5QXdgTbxeJ8UnoS2901im3KDiCySWR4p2Wjb9rKE0gSsIiCbqYWE+9y/v04yrVW0mKfQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752506491; c=relaxed/simple; bh=J3h0bzv+66l5Vs9eafGDJARdqv3+sQK6ZF2ENvt/3ao=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LDo3COxIvmjWfCbcBuZMK34zQlsx9L+VO2qWYvr/b0WLoxvh23od+cpp19RI4Rk4Ih4CrqIjED+4W0c/ZMIZOscP4DSRFVfb7DU9hmgmMC1Ooesk99cfwGkCY6TqenSo9gbCJjournsWT1y9EqlksEg5B6dA9+Qep7RPwy8r9U8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NFspP8lE; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="NFspP8lE" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 21068C4CEED; Mon, 14 Jul 2025 15:21:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752506491; bh=J3h0bzv+66l5Vs9eafGDJARdqv3+sQK6ZF2ENvt/3ao=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NFspP8lExdSkLPa6lXOanaUhJmwVlfQi/GCal5vgjXZqHppSz2xS7g/g+K1IUvdgB N4dl+zrFV5eV5OZMYl8EDjDMZ+hEj0gkB1WpcSeX2r1Ljgewo08+83Jd5DlGQxxL26 61/+jY0z7+Ub/L2TBd3juZmSUt1EgX0no/4bHeKjoxsHK19NUPdmHk5MhVL2jwIBpX 4bz4q3xUf1fbSLgMLmsS25z9mRSdE++0JGZ5Pd4UKGynIFRUmwmCZsspgO4YtEBpae DiydLS14INe/vl/i08Sz1AATZqiLeW9atahKEDm7ncy5Yr2gUoESsoCTzZtFr43G7T 1qIY6mIrQSZlA== From: Will Deacon To: linux-kernel@vger.kernel.org Cc: Will Deacon , Keir Fraser , Steven Moreland , Frederick Mayle , Stefan Hajnoczi , Stefano Garzarella , "Michael S. Tsirkin" , Jason Wang , =?UTF-8?q?Eugenio=20P=C3=A9rez?= , netdev@vger.kernel.org, virtualization@lists.linux.dev Subject: [PATCH v3 7/9] vhost/vsock: Allocate nonlinear SKBs for handling large receive buffers Date: Mon, 14 Jul 2025 16:21:01 +0100 Message-Id: <20250714152103.6949-8-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250714152103.6949-1-will@kernel.org> References: <20250714152103.6949-1-will@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When receiving a packet from a guest, vhost_vsock_handle_tx_kick() calls vhost_vsock_alloc_linear_skb() to allocate and fill an SKB with the receive data. Unfortunately, these are always linear allocations and can therefore result in significant pressure on kmalloc() considering that the maximum packet size (VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM) is a little over 64KiB, resulting in a 128KiB allocation for each packet. Rework the vsock SKB allocation so that, for sizes with page order greater than PAGE_ALLOC_COSTLY_ORDER, a nonlinear SKB is allocated instead with the packet header in the SKB and the receive data in the fragments. Finally, add a debug warning if virtio_vsock_skb_rx_put() is ever called on an SKB with a non-zero length, as this would be destructive for the nonlinear case. Signed-off-by: Will Deacon Reviewed-by: Stefano Garzarella --- drivers/vhost/vsock.c | 8 +++----- include/linux/virtio_vsock.h | 40 +++++++++++++++++++++++++++++------- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index 24b7547b05a6..0679a706ebc0 100644 --- a/drivers/vhost/vsock.c +++ b/drivers/vhost/vsock.c @@ -349,7 +349,7 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq, return NULL; =20 /* len contains both payload and hdr */ - skb =3D virtio_vsock_alloc_linear_skb(len, GFP_KERNEL); + skb =3D virtio_vsock_alloc_skb(len, GFP_KERNEL); if (!skb) return NULL; =20 @@ -378,10 +378,8 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq, =20 virtio_vsock_skb_rx_put(skb, payload_len); =20 - nbytes =3D copy_from_iter(skb->data, payload_len, &iov_iter); - if (nbytes !=3D payload_len) { - vq_err(vq, "Expected %zu byte payload, got %zu bytes\n", - payload_len, nbytes); + if (skb_copy_datagram_from_iter(skb, 0, &iov_iter, payload_len)) { + vq_err(vq, "Failed to copy %zu byte payload\n", payload_len); kfree_skb(skb); return NULL; } diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h index 36dd0cd55368..fa5934ea9c81 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -49,20 +49,46 @@ static inline void virtio_vsock_skb_clear_tap_delivered= (struct sk_buff *skb) =20 static inline void virtio_vsock_skb_rx_put(struct sk_buff *skb, u32 len) { - skb_put(skb, len); + DEBUG_NET_WARN_ON_ONCE(skb->len); + + if (skb_is_nonlinear(skb)) + skb->len =3D len; + else + skb_put(skb, len); +} + +static inline struct sk_buff * +__virtio_vsock_alloc_skb_with_frags(unsigned int header_len, + unsigned int data_len, + gfp_t mask) +{ + struct sk_buff *skb; + int err; + + skb =3D alloc_skb_with_frags(header_len, data_len, + PAGE_ALLOC_COSTLY_ORDER, &err, mask); + if (!skb) + return NULL; + + skb_reserve(skb, VIRTIO_VSOCK_SKB_HEADROOM); + skb->data_len =3D data_len; + return skb; } =20 static inline struct sk_buff * virtio_vsock_alloc_linear_skb(unsigned int size, gfp_t mask) { - struct sk_buff *skb; + return __virtio_vsock_alloc_skb_with_frags(size, 0, mask); +} =20 - skb =3D alloc_skb(size, mask); - if (!skb) - return NULL; +static inline struct sk_buff *virtio_vsock_alloc_skb(unsigned int size, gf= p_t mask) +{ + if (size <=3D SKB_WITH_OVERHEAD(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) + return virtio_vsock_alloc_linear_skb(size, mask); =20 - skb_reserve(skb, VIRTIO_VSOCK_SKB_HEADROOM); - return skb; + size -=3D VIRTIO_VSOCK_SKB_HEADROOM; + return __virtio_vsock_alloc_skb_with_frags(VIRTIO_VSOCK_SKB_HEADROOM, + size, mask); } =20 static inline void --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 03:46:26 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 31E3226A0AD; Mon, 14 Jul 2025 15:21:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752506494; cv=none; b=IBjRE1azRLIQqd16l8on7twLqL8/fcrUm5cExM7H9LD1sTTRxLR5WPb4KuzdbuxfcOnkwq0o/UOrz94i3i17fWVHgdhjaXOvsGMsgTk4kmufwNcS2tqRBBa2pcAGrL+3EgO4r9amEhiACqXD09ssicHgLcKdor0J6TDnoykDEF0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752506494; c=relaxed/simple; bh=6dFc2pj54Tf3DihdCujZcgocrkZgNMlBdT6L3xux37o=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WLPGMP+pFo9yOIRDGEEUfBQYerGWNkIGIFLFUpvHbqVfIFRIT3NKaX6QWrjZDaep9rqM3w3LTatD9if5wfroUgsxLcCZKdO370Lulv8QiIaIgfRwo99tzqp2cXE3LAE4YoDvITxXpfaE8PdfopVQ5GyRxJNLafSUJ8oNNYHslRk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=NZQj5SjG; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="NZQj5SjG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CE4DAC4CEED; Mon, 14 Jul 2025 15:21:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752506494; bh=6dFc2pj54Tf3DihdCujZcgocrkZgNMlBdT6L3xux37o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NZQj5SjG0nKwG6IsMgiWmCa2cvUcj4u/oBu5Tncu8+HT8vc+m+qbKH/sdUJ7JhZ5W XBGW6HJG7LlFtigQLSYbr3q5dAFZYgXwOC1pyvl1OsqXUa51/a3K8eiLeFPQzErLsH WRbninklb+4oUj82uSP05giZ1BVpshFGxKAqRzIdqAJtEuERkyesmaSUqTwpDlpsUT ATnixcwQiFZ65fJAUOVyy68ewTWkulRbz+0gnh7Rsn/5bcfej1oPra92cV4nGgxZpn QQaTQyNdxqYz+dPIPk3q2LRd3F5238aNToVFbxvw+muze+NuJfodRnr8A6NIqvm3CQ WNza1tcqHpELA== From: Will Deacon To: linux-kernel@vger.kernel.org Cc: Will Deacon , Keir Fraser , Steven Moreland , Frederick Mayle , Stefan Hajnoczi , Stefano Garzarella , "Michael S. Tsirkin" , Jason Wang , =?UTF-8?q?Eugenio=20P=C3=A9rez?= , netdev@vger.kernel.org, virtualization@lists.linux.dev Subject: [PATCH v3 8/9] vsock/virtio: Rename virtio_vsock_skb_rx_put() Date: Mon, 14 Jul 2025 16:21:02 +0100 Message-Id: <20250714152103.6949-9-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250714152103.6949-1-will@kernel.org> References: <20250714152103.6949-1-will@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In preparation for using virtio_vsock_skb_rx_put() when populating SKBs on the vsock TX path, rename virtio_vsock_skb_rx_put() to virtio_vsock_skb_put(). No functional change. Reviewed-by: Stefano Garzarella Signed-off-by: Will Deacon --- drivers/vhost/vsock.c | 2 +- include/linux/virtio_vsock.h | 2 +- net/vmw_vsock/virtio_transport.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index 0679a706ebc0..ae01457ea2cd 100644 --- a/drivers/vhost/vsock.c +++ b/drivers/vhost/vsock.c @@ -376,7 +376,7 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq, return NULL; } =20 - virtio_vsock_skb_rx_put(skb, payload_len); + virtio_vsock_skb_put(skb, payload_len); =20 if (skb_copy_datagram_from_iter(skb, 0, &iov_iter, payload_len)) { vq_err(vq, "Failed to copy %zu byte payload\n", payload_len); diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h index fa5934ea9c81..0c67543a45c8 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -47,7 +47,7 @@ static inline void virtio_vsock_skb_clear_tap_delivered(s= truct sk_buff *skb) VIRTIO_VSOCK_SKB_CB(skb)->tap_delivered =3D false; } =20 -static inline void virtio_vsock_skb_rx_put(struct sk_buff *skb, u32 len) +static inline void virtio_vsock_skb_put(struct sk_buff *skb, u32 len) { DEBUG_NET_WARN_ON_ONCE(skb->len); =20 diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transp= ort.c index c983fd62e37a..f65ad8706bbd 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -657,7 +657,7 @@ static void virtio_transport_rx_work(struct work_struct= *work) } =20 if (payload_len) - virtio_vsock_skb_rx_put(skb, payload_len); + virtio_vsock_skb_put(skb, payload_len); =20 virtio_transport_deliver_tap_pkt(skb); virtio_transport_recv_pkt(&virtio_transport, skb); --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Tue Oct 7 03:46:26 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 36A122690CB; Mon, 14 Jul 2025 15:21:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752506497; cv=none; b=QdxxpdfsJKA3H6chc9vkuG7mkhJGGfNnrNdXjhGMU06zCok7yum05sjdSFHIp3gMagkWlh1XTCuBopC5+FJ883CGSmhX2fOtzW6LtNoSh34RyBCDwiC4dTMs38p7oV1OV6xSdfPpRR4QuWHU33zICpWQgYkhJ0JbPlIqyQnNmRE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752506497; c=relaxed/simple; bh=VC+ApeBCzBvySjxO+5RtrC8kfJb3G3Dm+10WW4fjuOE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gAl1No+sjWhW4c9bdzbz+Aq74jy96hAyXpzvnGrXW1RNVKJEl1xllWzgIJ/eXaaPO1iRMbvBZae5patZfwgrpQUMIssNYTrelkrCBYuIW3o/npdUjx4FDCWYFnlc7xnFuCCCXajns/jX9iZ2ewsMim5fdaVVW+Ldy7jL+5eJaq8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=c/Dg0YK7; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="c/Dg0YK7" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8BD69C4CEED; Mon, 14 Jul 2025 15:21:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752506496; bh=VC+ApeBCzBvySjxO+5RtrC8kfJb3G3Dm+10WW4fjuOE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=c/Dg0YK7VVpqlxi6jGzlQbBi0jkqGeBzsmhRp1bJpie+mZwa3XqOK5DalJAe79gbz +j1CVnIbRPSBmiH3eXf3tLNU73P/liN+4BOw57lL5vBvzp5hW7l2NqTvsa3uxu8Dom BO0Zg1qETSn9Ey2PnIdGjliDesuWY5TMILr5d1iN7MuVN3Xl+Yuv01EOhH4xq0Py0P dyBycMNqcVPGBBk+G/1WyD+J+ZBYBBiwBUnlK6B6nUttCkOH8Kj8Pgp+i9odAEnbCu lEyhXHs4GiXhvdju1bP3NqLieqZjm/oeHjh1+nVQKOm8a/Z2c7u9TT40erRrbSt//y OyQYwtGONWCag== From: Will Deacon To: linux-kernel@vger.kernel.org Cc: Will Deacon , Keir Fraser , Steven Moreland , Frederick Mayle , Stefan Hajnoczi , Stefano Garzarella , "Michael S. Tsirkin" , Jason Wang , =?UTF-8?q?Eugenio=20P=C3=A9rez?= , netdev@vger.kernel.org, virtualization@lists.linux.dev Subject: [PATCH v3 9/9] vsock/virtio: Allocate nonlinear SKBs for handling large transmit buffers Date: Mon, 14 Jul 2025 16:21:03 +0100 Message-Id: <20250714152103.6949-10-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250714152103.6949-1-will@kernel.org> References: <20250714152103.6949-1-will@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When transmitting a vsock packet, virtio_transport_send_pkt_info() calls virtio_transport_alloc_linear_skb() to allocate and fill SKBs with the transmit data. Unfortunately, these are always linear allocations and can therefore result in significant pressure on kmalloc() considering that the maximum packet size (VIRTIO_VSOCK_MAX_PKT_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM) is a little over 64KiB, resulting in a 128KiB allocation for each packet. Rework the vsock SKB allocation so that, for sizes with page order greater than PAGE_ALLOC_COSTLY_ORDER, a nonlinear SKB is allocated instead with the packet header in the SKB and the transmit data in the fragments. No that this affects both the vhost and virtio transports. Reviewed-by: Stefano Garzarella Signed-off-by: Will Deacon --- net/vmw_vsock/virtio_transport_common.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/net/vmw_vsock/virtio_transport_common.c b/net/vmw_vsock/virtio= _transport_common.c index c9eb7f7ac00d..fe92e5fa95b4 100644 --- a/net/vmw_vsock/virtio_transport_common.c +++ b/net/vmw_vsock/virtio_transport_common.c @@ -109,7 +109,8 @@ static int virtio_transport_fill_skb(struct sk_buff *sk= b, return __zerocopy_sg_from_iter(info->msg, NULL, skb, &info->msg->msg_iter, len, NULL); =20 - return memcpy_from_msg(skb_put(skb, len), info->msg, len); + virtio_vsock_skb_put(skb, len); + return skb_copy_datagram_from_iter(skb, 0, &info->msg->msg_iter, len); } =20 static void virtio_transport_init_hdr(struct sk_buff *skb, @@ -261,7 +262,7 @@ static struct sk_buff *virtio_transport_alloc_skb(struc= t virtio_vsock_pkt_info * if (!zcopy) skb_len +=3D payload_len; =20 - skb =3D virtio_vsock_alloc_linear_skb(skb_len, GFP_KERNEL); + skb =3D virtio_vsock_alloc_skb(skb_len, GFP_KERNEL); if (!skb) return NULL; =20 --=20 2.50.0.727.gbf7dc18ff4-goog