From nobody Mon Oct 6 20:56:42 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 8409F28982C; Thu, 17 Jul 2025 09:01: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=1752742886; cv=none; b=Ga2P+tRIgBpO8AvswoxPcgXcp5ughl+dO2KUenqPE6aDDdnNMK4vPA19xlkQRn5L+ZrLUrnFhpPhFzjs8JrezFlXIlsj/1NI1ULZ8+lz0TnxriTHi3Lg1nwf/95WxJ5IdG5BTmm1yYwt1lEIhRgrTeJhAhMz1mYGByw7lpuH6tY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752742886; c=relaxed/simple; bh=EsK5W6bKoLJTlrARe6cyHuC1eYdjdkYRPm1cbOahWk8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kBZGCYwoHrNNsJ3NGaXWQiFAquctOxkzbZZ+19M/vy80lzLoJAo2SGB8AcQ1Hy6k+7Trliq+SyOuQH6IIYKlCJJlPT2BDFFI1dj6Cd4YecWIYJy2RPHuFGMD/KUFP46e88X10gjjTjrKep3l7cICQy/gx+lVLIIlsdqsS4UTbXA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=UEBGyB45; 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="UEBGyB45" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CE273C4CEED; Thu, 17 Jul 2025 09:01:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752742886; bh=EsK5W6bKoLJTlrARe6cyHuC1eYdjdkYRPm1cbOahWk8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UEBGyB45TrIiT5GWYzekGwu7EMmsz4w1KBIN+8Tyh9pHDt9o73hKh8e0qBrHQL6CB VdMMF3SAclC6iCeR6jDj4umG8X2Ceon46V1DCOqN6WCO1tlfciOklrVctOS3rgjuwu 5FPVVKYFngKj9yQdEa9hUDlkUOUs9AgiRvGlG/1b2APkjgyaRAUZEaME8iYYQO7CjJ BSSWCzK9B61Y2Pns7DCRa/v6KTYvLWizvB+zDReyzZJsVFG9LaR/BgPepZcR1skFtt 1+MN6FkrxudJi41fAPqePsUHpFxF+/z6dhJaptAsoNEyocxFtuDh1YmjHrIF58F6gs w6GKHlGCjqvsQ== 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 v4 1/9] vhost/vsock: Avoid allocating arbitrarily-sized SKBs Date: Thu, 17 Jul 2025 10:01:08 +0100 Message-Id: <20250717090116.11987-2-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250717090116.11987-1-will@kernel.org> References: <20250717090116.11987-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") Reviewed-by: Stefano Garzarella Signed-off-by: Will Deacon Acked-by: Michael S. Tsirkin --- 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 Mon Oct 6 20:56:42 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 53B8828A72A; Thu, 17 Jul 2025 09:01:29 +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=1752742889; cv=none; b=tBSZP7x3UJiFQ0OQhbuTm2WjPjS29Xw+H0bfgTSDStUFs4utf/umTBVlQovrmx9LKNuqH+Q90nucd2vwq59/bKLJHFVhBVGwRcBwjQRCS0wRA9CabBHzduj4XOt28bkX+hGivwRU5usQi4O0SDmJlUx89D3I/tWVhf4MuFiqKQI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752742889; c=relaxed/simple; bh=6D5cFK2vNGL2QsQ9IOnTKvvlTeWZ5xospOK0ukeQYVU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Q19nnP0rsiUQbyCmwq50g69PeOpXRrnIFiMlSYY7uE+QXpoReLvDzuSU6/Eeh0NW87NZn/6clYzEa2ar8/+WXFQR21VNkeGxw5uPj9ixnLKr2NzXDFHRL1cs90qKKzEQnLxVXBW+bt1h+KP7a0i8yYuhDiugCX34MBB9Oz/pHOA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GP5E/DxQ; 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="GP5E/DxQ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 96BA9C4CEE3; Thu, 17 Jul 2025 09:01:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752742888; bh=6D5cFK2vNGL2QsQ9IOnTKvvlTeWZ5xospOK0ukeQYVU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GP5E/DxQaUSooC8bh5SMhxjNsoRAr9rMBHD3EOasY5iXkqtH1s2idUvtfEOyC0bmh 2sK4QuCFPg19W91sta+1nYVZEK++8vc+lm+kekSdH7XnsM469sREHcj1N1a0lJ3juT o0rme5WVnSSrwwwQjFwhXWnATeJCc8OA78fX0JfwRA2OF48HFk+jib5j1KrEsJi6ii W/YUIX07mLcilOUVwkjoR/mXtEi0pcH4BpXYabOBU3ypE2fcof1WocPTaRwEXKG38n Zo/dYM7vq+INmEaUojmFmxLExwfPswbEpALx+RfAAaUSwgw3DCs73GbPmdg9wUfE9L pLCM6heF0g+8g== 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 v4 2/9] vsock/virtio: Validate length in packet header before skb_put() Date: Thu, 17 Jul 2025 10:01:09 +0100 Message-Id: <20250717090116.11987-3-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250717090116.11987-1-will@kernel.org> References: <20250717090116.11987-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 Acked-by: Michael S. Tsirkin Reviewed-by: Stefano Garzarella --- 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..eb08a393413d 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 (unlikely(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 Mon Oct 6 20:56:42 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 9978528BA9C; Thu, 17 Jul 2025 09:01: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=1752742891; cv=none; b=MyGgCvGQU9mhN4pt3VzoHyxT5JZAz5G2DBmkhHk3kUJuDpHwIDsQX+VR86sKQjk26a6tjShQwm/Bhoy0hweOAIctDmweZziIXZJmuiv8VqjBbz2+M/hvbURJarzeJ0SmDiz7OOJ58yaWf+5EmBspyByerGk7DtXeB1omvToc4zQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752742891; c=relaxed/simple; bh=jGY71brbCxVnS7zG6bdYK5nYqAE6JIwRi73eOnclaeI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XxPEvH8376WMF/NZNRoue32DACBs07gHz9dRSLeH0D7PMBu8V+OLnXXvQz34hKNKHLPMm1wnrseOolcf1VBGaC+WrgFdHC7fNZEV3ylMi1UV50dJk9vsD+edhduKfOxol/ry4TRS9nv/IhwboFwAVgzaWMpCEO5gpZIP8U61uO0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lrIib8TX; 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="lrIib8TX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5A541C4CEED; Thu, 17 Jul 2025 09:01:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752742891; bh=jGY71brbCxVnS7zG6bdYK5nYqAE6JIwRi73eOnclaeI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lrIib8TX64gvSL8fPYeug8DY7bXH1v3evjQ/EEOwOvPPt+D7M0xLW90RwDv/4Tk9k lNi6SbrZg/Ueyib7I5B63HKu11Pz/m24tIzbR3UYhlNfbXszqrYAMuw/OOuQS6EX0r ORCFo0QblHMdLeTumF03r146fH4UKo6C7KxUVwcnvGp80Bk5x1apU/Ne5KXTF71wa3 OBawlnw/sbl72EmvoPvk07ROAThBhuRPmMlckZFuMmza+vSJYaPWcelz9JQsUI3ikD go7JUkxvKtOFCY5xXb/FGrFlqSEJQ7zJy2WMHCB4JoyPkVlX3A/O+IE1C7GgvP2zS6 6eR62UTY2MMjw== 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 v4 3/9] vsock/virtio: Move length check to callers of virtio_vsock_skb_rx_put() Date: Thu, 17 Jul 2025 10:01:10 +0100 Message-Id: <20250717090116.11987-4-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250717090116.11987-1-will@kernel.org> References: <20250717090116.11987-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(). Reviewed-by: Stefano Garzarella Signed-off-by: Will Deacon Acked-by: Michael S. Tsirkin --- 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 eb08a393413d..0166919f8705 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 Mon Oct 6 20:56:42 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 39B0E28C5CF; Thu, 17 Jul 2025 09:01: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=1752742894; cv=none; b=rGsbQuxAgh+CHj5dLZgNAxTUuTJaHzfP2I6uP24zfeHnAOOyPDBwS1PQjIbHQ7EZ+HTzGx99SmyfRNHV0jvlXXbFeZQvdnGlT9GoUQpYdC7UNQbKqdsAiR8DfkxAKPrYgtSQasXFyz0WvtJH9iIXEG63hhjp8etounOnumZt9uU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752742894; c=relaxed/simple; bh=SfSAPODvLeyhcY58xHztgQC88eZuBiN28FsEP9S31Hs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lDvclWCeWrA4yGYULuA45AtQ1kep8zU7Yml2BdkX4zsjBrSEKRy+hFs/LEBpR2jcJUeG9g7OM3+D6YP1AEJtM5csnYOgH+Y7oHwtveUvP52AOdPjoDIOTm3IzZmoPjYE/VteU+20S7unzVLNOvTs5+VJWpp9f36rfr2sud2G7E8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=F/oecDGo; 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="F/oecDGo" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EEE5EC4CEED; Thu, 17 Jul 2025 09:01:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752742894; bh=SfSAPODvLeyhcY58xHztgQC88eZuBiN28FsEP9S31Hs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=F/oecDGoqCFyMi5FqRTj7DC28h5FeojL9EMLDCF91eJbUVm+V7i0SNX5vafDzW5+3 PADuXL2z5zPNKrHfNDsM3707tA4LPDX58geFhPmOqMbzr2xFjL9bScSHQ5WWGbNB+8 oSrQnIXQC8vF+MgbXexSp5847zC9q4LpPJ751GoyCYzXunr3f0VZ4Xmavg+Y8hj244 s2W6cSrKbCeu21l6GkXCjXRtgF4SamN0h4JqYbwbnEIXqBeNFzuTltxME/mQXYYXzn jqaTccvNijekTHwixugdX14QlDXKSVYTdj1HvFfMdEp26p+CcRFKQAa/52gpuS0FQC taUIZZJP6h0ow== 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 v4 4/9] vsock/virtio: Resize receive buffers so that each SKB fits in a 4K page Date: Thu, 17 Jul 2025 10:01:11 +0100 Message-Id: <20250717090116.11987-5-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250717090116.11987-1-will@kernel.org> References: <20250717090116.11987-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. Reviewed-by: Stefano Garzarella Signed-off-by: Will Deacon Acked-by: Michael S. Tsirkin --- 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 0166919f8705..39f346890f7f 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 Mon Oct 6 20:56:42 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 18A5728D8DB; Thu, 17 Jul 2025 09:01: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=1752742897; cv=none; b=Y1nyoaN0czJ5H6aDhRjVCcsSu3Gyktqqsd0ryfPwKPF1DXM9N6fzgZF19fZ3YuYN8kapIGm5DGh9Z3G8rPb9UDRaIECIEUa/qF56n1zz9j9t1A9FhcopqcsHvCQcY5YMYTJH/pMGGfBGCsLa8pwlzfG/A7Ee1rX6tL72HyweM7E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752742897; c=relaxed/simple; bh=KwXpHKVzwAEGl09Zp22el4vE43Y3lNfWFQ5n8BlpBqw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=aZaaQ1Mi+kXzMVIMVHKb1SvnImUdazHmwUzOQOIhUm+ThBY3AFobTDoGpe6WkB5N4Twuy8Okqd8ZzQpkaFqhMy8y/F1aNeV0Y7P1m++Miwzf/PI/FXW/+LhYt2GkkqGQeKulyqPmVDpZblqntY0veZHHTSVJfylp+rIxzgW4gwA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=j/QZQ7Pj; 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="j/QZQ7Pj" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 90306C4CEEB; Thu, 17 Jul 2025 09:01:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752742896; bh=KwXpHKVzwAEGl09Zp22el4vE43Y3lNfWFQ5n8BlpBqw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=j/QZQ7Pj6FMi0ncGhta00mLUQN6O0WHkdg0IfPADdrnmuwxnoo/fCF98/Obl7iJmJ eJG89O2fQccdGcG9HtYjpd9AgVyDeWpQTBHRb3KYNJBox79XI+tfoljid10uYv2DPj 4tyAWrqWRmRKwpW1dYGBLHI3QSch/m471SZ9/nqWqujB7ZsTleIhzGhhb3DG79u2fU Hskld+0jl7RssHayQ6gTcPwNG10xQVqVPR3goy6tqK6JBnLFBa6owLahwJIxDRAvLc GCU9yuB+TpjHbQDUTcOgUTEsFXxkOOzLZtd0SPLdQD2JyZVbg0nWjnN1tMhnLVRmgJ +C68lc/4jUATg== 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 v4 5/9] vsock/virtio: Rename virtio_vsock_alloc_skb() Date: Thu, 17 Jul 2025 10:01:12 +0100 Message-Id: <20250717090116.11987-6-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250717090116.11987-1-will@kernel.org> References: <20250717090116.11987-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. Reviewed-by: Stefano Garzarella Signed-off-by: Will Deacon Acked-by: Michael S. Tsirkin --- 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 39f346890f7f..80dcf6ac1e72 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 Mon Oct 6 20:56:42 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 BE56428D8C9; Thu, 17 Jul 2025 09:01:39 +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=1752742899; cv=none; b=m1Whsp/AsrG4Ca0zgGoIWOmqso8Q6emCNtnMpJrLy1S2KWfTe127ZUKZh1AECuL++lyt/d580udGbsGSZ59OkrHAXECzbRI9bwIMSkCuMiRuNPDlxfeXcqIbOBWUSMMChTkK/egYBKUSXRaSScW2co37kelGqLd/eUENr3RNgZs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752742899; c=relaxed/simple; bh=oTtY3jHyStEX2GnaGcXZjfEA5bS7mFHBhZ1ogKJDpQY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MI5eEGL9zJU8Y6GdZWIgilUKT3fbx3g/W+PkfntPvVxRO1tDiEsA4B5PhFBRfSkiZPVsgVsR8ejhUzJwoN4BQthx0tt2KpZwwwjU1M+nfsb4M3WV2qtdVdRctpEmG7k9weOzC+r82SNTni4b9zqtPpm7iFq8yAMG3y0PWY+RPWs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=sWej8tKI; 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="sWej8tKI" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 320E0C4CEF5; Thu, 17 Jul 2025 09:01:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752742899; bh=oTtY3jHyStEX2GnaGcXZjfEA5bS7mFHBhZ1ogKJDpQY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sWej8tKIFVvEZ5VKIpU2h/5bUSPSI/FJB6b0tDr4CZTEHPfEdup+lfrtcFKOdf8ov HfxAM/0nszu2FQ5dYuOisWOEBgWP1FwFLmoHeJXLyH691CIdexwr/9V40fjk6aSBUM zH0eYfI0hfJuR5GSwFRKhg14dA3lJGH2dwdYiK625Ns4j5VWigZbYY8lWbiIHMh5Ni mG29pB1ijlfVjgxyKVsunSmHyeGK0DCglT7z1ZBrFobBnbwoGwWngmOQ2w6BIePvI4 NVw89ZhcPlSYykoR5WMAueT1MA4Nm87h8d2mUqsBY+Bp/dQk35wBAjaLTtJPWGpqej b++YspBsrSOpQ== 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 v4 6/9] vsock/virtio: Move SKB allocation lower-bound check to callers Date: Thu, 17 Jul 2025 10:01:13 +0100 Message-Id: <20250717090116.11987-7-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250717090116.11987-1-will@kernel.org> References: <20250717090116.11987-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(). Reviewed-by: Stefano Garzarella Signed-off-by: Will Deacon Acked-by: Michael S. Tsirkin --- 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 Mon Oct 6 20:56:42 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 6765D28C5C9; Thu, 17 Jul 2025 09:01:42 +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=1752742902; cv=none; b=jMRuevUOO9V6wrlDZ1WZfzChfmTiDLLyAxEvP7CYacPN2BvOAaeryUD95v2SyDBhfeTUn5icfzvlvWa59C5F/3I3ThBcMMvJraRWJpPj0BBlLnK2BOhNzoOKmIpofnkN+G/emt/1Fa81hWa5O/J2zGO6Yvp6q1oseLvvqxfAnxE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752742902; c=relaxed/simple; bh=NiKPR/htVJnmrk6MTJRQgZc1xHv3Eu5IvMBg2dmm0rM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CdPwaCr9qeB6WdqMUQW3TWpFaBGt9VsbEs7DYScE2sf68muRl0K/ONu85NpKBc3WLU6g1hYQagjnSFpfdT5U3g25EP44o4AMTmKleydqk4jGkGU9B3tvM0K4gTR9yaJ4iNN7M3Au9gzGKMKy0Al18lan9lYJZ96IrKSSYKpnN0c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HJPUiuj7; 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="HJPUiuj7" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C5428C4CEEB; Thu, 17 Jul 2025 09:01:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752742901; bh=NiKPR/htVJnmrk6MTJRQgZc1xHv3Eu5IvMBg2dmm0rM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HJPUiuj76la303hxKP6iAfQsaoEAzt5WTvZWcxpG01b6nkFDbcjY9Q+NALb0yWdG0 Z4UEaQctddtCRPto8ar+11OXvVrr88ijuVVSzv0x+xwXjolwynjxqEW1cR3T4T8U2V +s2XzHzY2Uytp1PcW4YlxukZ2/MpNFH1Rsl0WPgHyGd0WurN7oNs85nAQcYVkrYCbX rTNNf9nIGKUvwdtUwSeAajPLIMWd57SQ1Ur9ILaomF+TMJ9XcicbQkew0nwupQCIZ/ jUA01Xu5QZqccIztV5XTm7mprV4uzCpDcm3urJzfS8gLuyGSbV8wzBRbV0F3Lb01wo OdSL/ukGObVaQ== 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 v4 7/9] vhost/vsock: Allocate nonlinear SKBs for handling large receive buffers Date: Thu, 17 Jul 2025 10:01:14 +0100 Message-Id: <20250717090116.11987-8-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250717090116.11987-1-will@kernel.org> References: <20250717090116.11987-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. Reviewed-by: Stefano Garzarella Signed-off-by: Will Deacon Acked-by: Michael S. Tsirkin --- 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 Mon Oct 6 20:56:42 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 A1A902877F3; Thu, 17 Jul 2025 09:01:44 +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=1752742904; cv=none; b=jqzuCOj1pGU7Dz2oQos3z6x3SmJOmYTW2q+7DjupHbgWZTR/MjLygGoiJb3aFLzKNN8euAuBX0GslnVjRyStBPkE7Flem3wkaDqBf3qvPneOHAhlhnuVRbTI4moUFl/k0ZZ6Si7WsRft+V9eTOd/oFeXJtdJVrUaAxvuTe5aPds= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752742904; c=relaxed/simple; bh=wYs1S2rTX4fH2s4I7jI+Sd6BQo9XPGIBbbUJtE/9wBM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cec4QsdjRoQz/cXETHQRxZCnZvjtXLb/7yi0kNrvdo2Y2tZKRIIs+uxUBYr4rRcxv9oN/eOg3LF+QoxIk2yzGzfIu9knZTuaMGiHw2YmMfEoAk50gq7i7fkO2+gr3VyRI/wwLCfiDENsExJ/yqJ1wz5Vvs8vlqrbv+eyWgDPtkg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OF9QRyEX; 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="OF9QRyEX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6301DC4CEE3; Thu, 17 Jul 2025 09:01:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752742904; bh=wYs1S2rTX4fH2s4I7jI+Sd6BQo9XPGIBbbUJtE/9wBM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OF9QRyEXr3dw6Ugf6pbqIgNMPyM9hEL7O3VsWRN/4jyhQxhpqdNM66mwesrR25X1d HQuqvAdtwo/hObeY/8vkBumn6W59tn77QouaMPMrH1zWnizpU95mfAakprUXDxYsvB nqLFCttdlRBq7rtuTtOCppbs10cr+0Iy+kZzcsaYozvWbJOeAtqsmBMW5/d/pfjXgQ VIN7egyf4/ViUuVGQcNKnNEqLohYZZYLBrAloJx+xsEk98VzbDTqpv5ljNsmP7391r 0gsg9fIL6pB26PqZPCeL9VKxd4olRMg2UF6t3/YgFuqj+HyOwE/YuHlufJmq1x/lHg zid0Orb4x23BQ== 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 v4 8/9] vsock/virtio: Rename virtio_vsock_skb_rx_put() Date: Thu, 17 Jul 2025 10:01:15 +0100 Message-Id: <20250717090116.11987-9-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250717090116.11987-1-will@kernel.org> References: <20250717090116.11987-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 Acked-by: Michael S. Tsirkin --- 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 80dcf6ac1e72..b6569b0ca2bb 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 Mon Oct 6 20:56:42 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 4124C288C33; Thu, 17 Jul 2025 09:01:47 +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=1752742907; cv=none; b=oy2exyqj6uLMG5MXiax0QjEQDk46mNpVEmFan59QosQq5lSKturVliDxII/omvuiaq9I1CCkKW1CMyo9Za6+9CI6znEhZtaSLMz7UQLjYqwj51kNKR3Hvp90jCo12/lpDrkeCKmPQNSAMp6zitXf6TuA8/LIgczvxEXD1adg7uY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752742907; c=relaxed/simple; bh=tWBft60ygd6gngCGX7mXJTwPqVkgMB4+83ojVoEpFi4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RphxDmKvjpTXrMX8mbaMvrJItOpyjtOO1Z2XWcbM7lhSVJ9F0dViGv2AJLqMMgbBcHW0YbVvCsqzcrI8Azr0Zw/w5FBsN+LoaJ4iPgTTxGfQq4Dn9kG+42Oh+EjIPCpAl4G0v5yktxP1scubErnj1I6X+S8f0LjMrBWSL/3POKI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=otEWzRLD; 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="otEWzRLD" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 02D6EC4CEEB; Thu, 17 Jul 2025 09:01:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752742907; bh=tWBft60ygd6gngCGX7mXJTwPqVkgMB4+83ojVoEpFi4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=otEWzRLDXZQDwtPujyy82OfLxmGjrLjC4FO/Rlnbv635mdYQDGAMI/SCK6Jj8YQ7d RLbzarPAfVcKj2a5cdBHr8YIit2NL1tQeXaxA2nDGyFB0InH09R7uR1dzX0TkfXnBQ JYILx+mgkkn0LFsrQWpTbhUfFDzD86QXUZUwP+HDyTMyAbcjUdk0A/XPpuHYjdIg6V Kxz9jJalT5vJtS/EDmfhGvvBOL1dfj99VV8017J557CS1yqpgom3AW6jphlzoQJ4a7 17MRJx/X9mJNDkEA0AutRhlG4khnaNLGdget1zCx4ftD4O0hF+5bFtE/GV7oEwDxd9 1JV/LVgNi+F7w== 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 v4 9/9] vsock/virtio: Allocate nonlinear SKBs for handling large transmit buffers Date: Thu, 17 Jul 2025 10:01:16 +0100 Message-Id: <20250717090116.11987-10-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250717090116.11987-1-will@kernel.org> References: <20250717090116.11987-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. Note that this affects both the vhost and virtio transports. Reviewed-by: Stefano Garzarella Signed-off-by: Will Deacon Acked-by: Michael S. Tsirkin --- 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