From nobody Wed Oct 8 05:55:38 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 79C5927FD60; Tue, 1 Jul 2025 16:45:18 +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=1751388318; cv=none; b=sC2DhRESpFQfiddQYeFimApK/PAvmcqVi2/qGtWNVuEx4pi7cPSAW9Qheym+EdAcPKDvdSLlymc6jVTGeXn+oRVRJ6ZQSy8FPWc+9/0PIJNUqUdCW1vsf8oe70y1zahazy57uW0zbyz26wn1NxJ2/bY65dRmt6FfGutMjculZxk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751388318; c=relaxed/simple; bh=iISDn9L+D3wVAHPNNIcp/TCqX/3WWeX/S60myFyZsyQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MAWf8uaBb5PEWwq4/kJHuMjqeg0RBfnhuGwFvIWtHR5sCMXOEWODj774cILK4yzHJVK4TsxRc4SfY6Zf2fSTpXx/RzK8bmwFG369KkFHeYdPoCpiSU7d0SOJTrnYr+WCXf43Z+XeInC3g+d2MoYhXGIXaNB2rtFXKIzIehVh/1Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=XZxKbsjn; 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="XZxKbsjn" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9E564C4CEEB; Tue, 1 Jul 2025 16:45:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751388318; bh=iISDn9L+D3wVAHPNNIcp/TCqX/3WWeX/S60myFyZsyQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XZxKbsjnxeim9dro/dDexHotSos9MPh1hZEwwprrdBVkWopSRky5SfOKmk99xuGc8 /EFW4YGONjdpQcXnsQaAAcAI1xDMpbxIY/o/CirhEf+z1d0iwAH92bvfrPVzrf3wS/ GX5JNReJYcw3N7JjTkuPpMdiAXPnH7pE1eDqteRu8YXnZl/8wNXSNzsAyHg/RI9RUj YtNAhTYLqzLAzlYX9R/TgloOe/O6INgJ8oc44kVEFELJblgEOxvMqsfVxu396ITnU+ 4HKPmSWjTb3ICsOvpjOxIDyPLdxweWKNEq1NWztMTActZYxAY8ALNLNFTPtKy+NZTq UbfT6WsCvuK3g== 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 v2 1/8] vhost/vsock: Avoid allocating arbitrarily-sized SKBs Date: Tue, 1 Jul 2025 17:45:00 +0100 Message-Id: <20250701164507.14883-2-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250701164507.14883-1-will@kernel.org> References: <20250701164507.14883-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 Tested-by: Lei Yang --- 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 Wed Oct 8 05:55:38 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 5E06B28136C; Tue, 1 Jul 2025 16:45:21 +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=1751388321; cv=none; b=V1dbSFfvdu8WcicuofpxZs+/GzqzejiC3Uck79NJ6mWtevL0OQPJxaAdvFINJPVRNzTb88JlIkhMRQtrJWm/JhsYwkujM4UR/7IhLhAsahkMaV1az+tnlKcKpnq7WgqIzkHRSsiI2p0/S2DDB0Qdd9QGvZ6Fh5v9VdSWs/Y12dE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751388321; c=relaxed/simple; bh=oKf208jR2snmwBDg9o+AlVnnid4IgiStTjfyVYgKtp0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HK163XWPS8LBmoanV2wZbHZEJJbOvcip1X8VIByNIjQ+EKkiYp+boZnMnBmGzl54qV59GtueEo5c9Yi0ZW+JmsiAQzkwhXpPFX3IbQbc0qGo7P9V+asQpsCNyfU22A83KwYtIydlx5/2qpe6lD0k7kLuVmyucdvp/6v9ScEFiRc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=HU1oFe1E; 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="HU1oFe1E" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 82E14C4CEEF; Tue, 1 Jul 2025 16:45:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751388320; bh=oKf208jR2snmwBDg9o+AlVnnid4IgiStTjfyVYgKtp0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HU1oFe1EOSzMivrXmQKxagYBpX613BZimJ3G5eHkNnYSGERMRtBl88f1FN/dfZwzA 9s1O+N8wHs2DdsjNALXKVO4Wuy56r+dXj+NazqC3IdFV3ATaS5FMZzAaHRBCOuZOHs mW5UvIC0DnIJCeAXV1PXhVhcE5QFfgTvLdojmwQJRjRJVXA+02tvZh6hGVkYEK1qcn aRBbbGEOiA9rtDrFxnEvZbCqyb+FYpw7AYuTZtTlIbS6uj4Y+0hkoUgXYomTBO4hO5 WA3ng+rlqqslLovpjZtqGWc33ExxgFojUseZfYgRP6w2F1da68OzuIWql7uU2A5gFg cLZS+RdLRHYjw== 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 v2 2/8] vsock/virtio: Validate length in packet header before skb_put() Date: Tue, 1 Jul 2025 17:45:01 +0100 Message-Id: <20250701164507.14883-3-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250701164507.14883-1-will@kernel.org> References: <20250701164507.14883-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 Tested-by: Lei Yang --- 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 Wed Oct 8 05:55:38 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 B76EA2820DC; Tue, 1 Jul 2025 16:45: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=1751388323; cv=none; b=QzUQuwQgBTmvHwDqR/+fasnWcwQzq4fn1gJ4j+wn4I3GT2A5yXeOHIaJ0nT6CqLDrMEmDjDrMsCcywgC7e2qTMZXF0yXmg0p+0PC7I6u457fALZmWTsGA4RRu3oBNUvjotAcDVtOvw6X5Erx09OeLAC+wSKEEhDpWWbIIbOnf/0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751388323; c=relaxed/simple; bh=GgQ5G49UOh/QqE8jeN3uL8vKvyjvdeIwwuEdW9qSZAk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UtVdMU9kHZgXGo91XCdKtJ23/de1YUekeY9Fz2VA5N0TjlUFQAKnPTnIcBzjJOrNF0Ii1YpzrNBXBfD8mKMcn35fTh7G1Flq62EtFbdraeFYqkUjRXogz/MjWChXxPhxcpQ631ZC0zffjlQAXsDONQjNLbsdiHp1qHn54spC6OQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=McUT/a0E; 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="McUT/a0E" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 63902C4AF09; Tue, 1 Jul 2025 16:45:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751388323; bh=GgQ5G49UOh/QqE8jeN3uL8vKvyjvdeIwwuEdW9qSZAk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=McUT/a0EQskzE+/VPkVpz8d6gCSjc7EPrQhKVcvPgcjw4br9dKzO42S/ReJXLM2Df SAjukmrIpEIoSVYztLgnCuH1/Re/tz3oDFL+XqsShLgAsEpwkLiYm9gRDGHD+HKV2p 3O5nIML2cm4F6qNc91yIVhGTsbLMsHradGRLQkYLfOPxKEkbvDuVgtLBb9dJJ3RUhW n/YGZaaz4Fg49p3ympbb+B7yRqB4hx8f4+6O4lOUXohptZUItX5DZGResObYHr7eOp EfyHdsorAjIqMnUmmeXkwoibZKLlmYX4z0sTi+RP96jQAeXtm8n+FFQg2NnV7Em+Ga lO7MSt3rVTmmg== 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 v2 3/8] vsock/virtio: Move length check to callers of virtio_vsock_skb_rx_put() Date: Tue, 1 Jul 2025 17:45:02 +0100 Message-Id: <20250701164507.14883-4-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250701164507.14883-1-will@kernel.org> References: <20250701164507.14883-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. Note that the vhost code already has similar logic in vhost_vsock_alloc_skb(). Signed-off-by: Will Deacon Tested-by: Lei Yang --- include/linux/virtio_vsock.h | 4 +--- net/vmw_vsock/virtio_transport.c | 4 +++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h index 36fb3edfa403..eb6980aa19fd 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -52,9 +52,7 @@ static inline void virtio_vsock_skb_rx_put(struct sk_buff= *skb) u32 len; =20 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..488e6ddc6ffa 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); + virtio_transport_deliver_tap_pkt(skb); virtio_transport_recv_pkt(&virtio_transport, skb); } --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Wed Oct 8 05:55:38 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 CD140283FD6; Tue, 1 Jul 2025 16:45: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=1751388326; cv=none; b=kydTNDCBk1IAUwPLJduj4O2p/gNNPnh+ZU+a8GA7s+/ec9ZYgeePcDkumj4+2+fAWEtttDzR1KB4u0MRJwqU+DseJBQCh03hAhznelYohIRINpd8+WzBJanEKx7Bhvok1JnciJ/T5ew57rG9PFm1DUY22uGmcOhjlEJa26/tRD8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751388326; c=relaxed/simple; bh=B1w0qcrCgepHrxOTMugcVkliDb4+0LWgsIR7uZB8lLM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OS6RphYV4YchdchY+7PzqiX7xes09JrhfcrxgnFQQPRWKXO5UMZ9tRZkjr2DxVXznTgVCfip6qw/XwLTlwiDE+a1mszHfO4bJQLMjSHeNgKFlJVx38ELgLNAETlZnNjcP9KRvbqZuOu2NZ4ewMDxIjt8REBTixwfvTkInyuKbl4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oTn8bUBe; 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="oTn8bUBe" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1E4F0C4CEEB; Tue, 1 Jul 2025 16:45:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751388326; bh=B1w0qcrCgepHrxOTMugcVkliDb4+0LWgsIR7uZB8lLM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oTn8bUBeJ/lie7A6fbC05ty2k4Lfx7Ieir/ROFNOPROE5jTUOzeu3zrcWKosBxFpJ +0OLClWuoIuqv8+LmssHpix2mOYrOwEtI4bLUi/pCb/oBnwtjlQDlXICbOfnqCLwGU 8eb+7HpDOAwLm8zn4QcTHZiic+LCpi0fDnIK2VheEkyhNaH7HCO+3zYJTRUW32DntW atV/saUnkQyu5JAoe1aIqs7OU65J7aRJXu4FzL9xzeHsfyDGa1HtjuvPufpEi5dHGF 6nvQwy2pNIczC1s7JCcCKf29yOzprQ7DQFuEMYSYLDEVIMdYox6IX5JU+tJ2PGZU8x OTjBbQi+LJe6A== 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 v2 4/8] vsock/virtio: Resize receive buffers so that each SKB fits in a page Date: Tue, 1 Jul 2025 17:45:03 +0100 Message-Id: <20250701164507.14883-5-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250701164507.14883-1-will@kernel.org> References: <20250701164507.14883-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 for the sake of a few hundred bytes of packet data. Limit the vsock virtio RX buffers to a page per SKB, resulting in much better memory utilisation and removing the need to allocate higher-order pages entirely. Signed-off-by: Will Deacon Tested-by: Lei Yang --- include/linux/virtio_vsock.h | 1 - net/vmw_vsock/virtio_transport.c | 7 ++++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/linux/virtio_vsock.h b/include/linux/virtio_vsock.h index eb6980aa19fd..1b5731186095 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -109,7 +109,6 @@ static inline size_t virtio_vsock_skb_len(struct sk_buf= f *skb) return (size_t)(skb_end_pointer(skb) - skb->head); } =20 -#define VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE (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 488e6ddc6ffa..3daba06ed499 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -307,7 +307,12 @@ 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; + /* Dimension the SKB so that the entire thing fits exactly into + * a single 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. + */ + int total_len =3D SKB_WITH_OVERHEAD(PAGE_SIZE); struct scatterlist pkt, *p; struct virtqueue *vq; struct sk_buff *skb; --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Wed Oct 8 05:55:38 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 79EAB283683; Tue, 1 Jul 2025 16:45: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=1751388329; cv=none; b=RFW9fll5lsLYImwpPx/CIgLrAgiAr27amRAMJu83QgPE/9jjgUsBYCVGmdDPjGYyXp/FTwH53X1AYQ9t1PVK9FdpJv0dGf+yc3OUBmnECYgb3HKV+/Slf5D9uAQSoPiTAH81AcsPW9wUNDL1o2u+8PyvtTJEve1vk1VP/j9Qzys= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751388329; c=relaxed/simple; bh=ll+/aFb8wcFEbLCTKUL+CegPo8SzmsaGprgTn8Gb6+E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=uIVfPoxRdSVj9L8kngN36aKEs96t3UJddH7cKRpUC/Suyer8ei6XwO0tY28lc9WAj7wJMoDl+O+zxroN1FpYh+7Z0InAGmoKTWnzZ8CzYpPDyXgVStWKqupozNGXm9iu3W9LhP5qHA9mg+o38H8HOV/jZ5EU2Fnv1kpUZDmOddI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KMmAGp1M; 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="KMmAGp1M" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D0B38C4CEEF; Tue, 1 Jul 2025 16:45:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751388329; bh=ll+/aFb8wcFEbLCTKUL+CegPo8SzmsaGprgTn8Gb6+E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KMmAGp1MT5xfOR1gGBkq4LZZ8Sqpz+nbcmf/pg1QEFKEaNZPeuvcbo4DYzOrTkS8m qtf8EmIEvsC+N48qM5guBBhcnkZ9JNTXinoYB8vNvhKB/bSzquhVZ2H8kBgmtKzzO0 7sFBxps6GypVm2Bply6TvQQTmYT5CmHDWeXcjwwYZ3Pg4KDx0US3StClOOge0wnOvX cqAPVUmUG3F6nRuaQXJy+nIFO5HTnhrc9G2PafE5Z24LHpWcllyue3MHvbbXhYdEmp 3VUVnzwXkxWt1xwhARtPYBeAI1HIFNWYwcWYn0WWbZpFijfsRJxX5xPeg4XBSS3ju7 voo03PK0Ub52w== 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 v2 5/8] vsock/virtio: Add vsock helper for linear SKB allocation Date: Tue, 1 Jul 2025 17:45:04 +0100 Message-Id: <20250701164507.14883-6-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250701164507.14883-1-will@kernel.org> References: <20250701164507.14883-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, introduce a new virtio_vsock_alloc_linear_skb() helper to return linear SKBs unconditionally and switch all callers over to this new interface for now. No functional change. Signed-off-by: Will Deacon Tested-by: Lei Yang --- drivers/vhost/vsock.c | 2 +- include/linux/virtio_vsock.h | 6 ++++++ net/vmw_vsock/virtio_transport.c | 2 +- net/vmw_vsock/virtio_transport_common.c | 2 +- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index 66a0f060770e..b13f6be452ba 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 1b5731186095..6d4a933c895a 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -70,6 +70,12 @@ static inline struct sk_buff *virtio_vsock_alloc_skb(uns= igned int size, gfp_t ma return skb; } =20 +static inline struct sk_buff * +virtio_vsock_alloc_linear_skb(unsigned int size, gfp_t mask) +{ + return virtio_vsock_alloc_skb(size, mask); +} + static inline void virtio_vsock_skb_queue_head(struct sk_buff_head *list, struct sk_buff *skb) { diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transp= ort.c index 3daba06ed499..2959db0404ed 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -321,7 +321,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 Wed Oct 8 05:55:38 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 336C0285074; Tue, 1 Jul 2025 16:45: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=1751388332; cv=none; b=AYAlcD7u4I1pAP3prXPPSpV2jCE23vWW9XWcR4z7ma2Q2NYSCORd4PM7XVCN8pcaDPdS8DawzOvRw9LTKDZYeq0BpRumxHPvgl3J0wkeAqGQuJ2z95t/+ONnmxSwomPewt3R2Et+FN50oqXcREOvy3ywtIKXSopjolWl5OFPqrw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751388332; c=relaxed/simple; bh=WW0Oy/HLwiKqJEO7w3O99UJACNVQrCQ/O89R8zhmn8M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dQsxGlR2iM5lXjzszTSf+OwapVX8h+ygH+SXJsP04SPtFOljdonLEJcBt189SHbiTcfO7ZYBiygnpgajOmBRqiFcm9G8GQw+5XNnVBnS6WQYvWpOMu4Vz75OA78ocz98QqWuVTRl+TZBwYrr/JdRivjI6eiG/58UfIE7XqWDVmM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Dnu8kSMM; 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="Dnu8kSMM" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 883AFC4CEF1; Tue, 1 Jul 2025 16:45:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751388331; bh=WW0Oy/HLwiKqJEO7w3O99UJACNVQrCQ/O89R8zhmn8M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Dnu8kSMMtJ3xfoOanUIdgAEQh2nmF0yLNOuZrcyhW+JGpIiGU7uEtEfJkOYsyEjP0 +WF+m25K4ZPIH3S433be3X9J6ItMKqKZxPlY+DWpH7Vcq3o3BvIWebfi8C/3cAYAKx UlZInKiM5Z78Q4YVgEJ65zVe+GE7Daqcd+PA57XOGzwgEXGZmZyjwU+mrsoZ4svnWY S75hpaaxIy56PbIjzaIESjzryQERzI2SjuO0N0uDbocHMxUdtYbD6Atu3AwmwyH1cM g0zZj5yrbZ0vhVwrZIfDoqk26zudURykM+woIQCUlAZ2F4glkjRQqxNZ4EjXgJI0Lf j0oyZGaZf73HQ== 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 v2 6/8] vhost/vsock: Allocate nonlinear SKBs for handling large receive buffers Date: Tue, 1 Jul 2025 17:45:05 +0100 Message-Id: <20250701164507.14883-7-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250701164507.14883-1-will@kernel.org> References: <20250701164507.14883-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. Move the VIRTIO_VSOCK_SKB_HEADROOM check out of the allocation function and into the single caller that needs it and 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 Tested-by: Lei Yang --- drivers/vhost/vsock.c | 11 +++++------ include/linux/virtio_vsock.h | 32 +++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/drivers/vhost/vsock.c b/drivers/vhost/vsock.c index b13f6be452ba..f3c2ea1d0ae7 100644 --- a/drivers/vhost/vsock.c +++ b/drivers/vhost/vsock.c @@ -344,11 +344,12 @@ 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 */ - skb =3D virtio_vsock_alloc_linear_skb(len, GFP_KERNEL); + skb =3D virtio_vsock_alloc_skb(len, GFP_KERNEL); if (!skb) return NULL; =20 @@ -377,10 +378,8 @@ vhost_vsock_alloc_skb(struct vhost_virtqueue *vq, =20 virtio_vsock_skb_rx_put(skb); =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 6d4a933c895a..ad69668f6b91 100644 --- a/include/linux/virtio_vsock.h +++ b/include/linux/virtio_vsock.h @@ -51,29 +51,47 @@ static inline void virtio_vsock_skb_rx_put(struct sk_bu= ff *skb) { u32 len; =20 + DEBUG_NET_WARN_ON_ONCE(skb->len); len =3D le32_to_cpu(virtio_vsock_hdr(skb)->len); - skb_put(skb, len); + + if (skb_is_nonlinear(skb)) + skb->len =3D len; + else + 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_skb_with_frags(unsigned int header_len, + unsigned int data_len, + gfp_t mask) { struct sk_buff *skb; + int err; =20 - if (size < VIRTIO_VSOCK_SKB_HEADROOM) - return NULL; - - skb =3D alloc_skb(size, mask); + skb =3D alloc_skb_with_frags(header_len, data_len, + PAGE_ALLOC_COSTLY_ORDER, &err, mask); if (!skb) return NULL; =20 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) { - return virtio_vsock_alloc_skb(size, mask); + return __virtio_vsock_alloc_skb_with_frags(size, 0, mask); +} + +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); + + 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 Wed Oct 8 05:55:38 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 90E052853F7; Tue, 1 Jul 2025 16:45: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=1751388334; cv=none; b=lvhF9F15ibpxXWJgXXi4jxwC8sTyWEFJlt7aEoGThbWqkpr37K5oqT6fwEcPjrS/dIa9UQ2Ts+HaKkoMzXwiQOdSx3l0qbpigIFUVk922XTsS/dThB8NkiKvoAH+GDJBOwGIXaq77MHsHfzISoaomdatMzQ3bqGOTMncoEkPHzU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751388334; c=relaxed/simple; bh=04XFkYAVsDTo05+5VTb2GZyNBXtBavyJjtbY6aD1Ra0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kyKvSAo7UpeQSHNDSG4DSgqA5rXZh2udHJXRqJgKpH2XJtjbbaOiojZ0nyfda1KWXbAYUN1PbJ6MNOmynWHbYOCDfOCUqmrO1eaWyALa4x5MivEi3XR2o3HKHNnc4yrv+5pa1ESBSdBPbolEoJ0Wn5VPG2uN59lUObNtPUTuTMw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QTxyQjfz; 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="QTxyQjfz" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3D6D6C4CEEB; Tue, 1 Jul 2025 16:45:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751388334; bh=04XFkYAVsDTo05+5VTb2GZyNBXtBavyJjtbY6aD1Ra0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QTxyQjfz6Si2y+b0Yw73V9GCBZqG+bR6QGNoRuArgc6P4Zi/Kxum7A9jf2wujCJfG hb2Oial7dTUKfMuWXsxBlR7CSR9ZNvUiFpqo/cD/odY8bpKhlLPtMU9jh5dxafgC7Z s85EU6W3WLalMai341O75MckawkDXeM+vUkiSCFmmgpFX6W2ifJzEfPugBfV1HW3IF jSemwd1ybxmKroMvL5yMtb5m0qc1Y71rw4tShwSouZ67sh1rGOr1d/5auHDsH4IIt/ WGu/Izgskibu5Q2CnnHSFYY2sXq899fAmX7f43R/Vide3ONx1/5kX5Dz4fKRN1AQzy nxGezQQGZafVA== 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 v2 7/8] vsock/virtio: Rename virtio_vsock_skb_rx_put() to virtio_vsock_skb_put() Date: Tue, 1 Jul 2025 17:45:06 +0100 Message-Id: <20250701164507.14883-8-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250701164507.14883-1-will@kernel.org> References: <20250701164507.14883-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 Tested-by: Lei Yang --- 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 f3c2ea1d0ae7..a6cd72a32f63 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); + virtio_vsock_skb_put(skb); =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 ad69668f6b91..ed5eab46e3dc 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) +static inline void virtio_vsock_skb_put(struct sk_buff *skb) { u32 len; =20 diff --git a/net/vmw_vsock/virtio_transport.c b/net/vmw_vsock/virtio_transp= ort.c index 2959db0404ed..44751cf8dfca 100644 --- a/net/vmw_vsock/virtio_transport.c +++ b/net/vmw_vsock/virtio_transport.c @@ -662,7 +662,7 @@ static void virtio_transport_rx_work(struct work_struct= *work) } =20 if (payload_len) - virtio_vsock_skb_rx_put(skb); + virtio_vsock_skb_put(skb); =20 virtio_transport_deliver_tap_pkt(skb); virtio_transport_recv_pkt(&virtio_transport, skb); --=20 2.50.0.727.gbf7dc18ff4-goog From nobody Wed Oct 8 05:55:38 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 46B501F428F; Tue, 1 Jul 2025 16:45:37 +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=1751388337; cv=none; b=k/jN6hw/BSQHNKX6SzfQ64/HlwpRYQVQNGCi2s1Vq3dDxOITewe3LF8kekG24nQhe9IZJGXbKRCzdyM0E1O+vYS1cjugrRlArM3jQ0scJfsvxt7SagQY+uTNNwur67XDvOYFkY5DVqkgsPHwZR78WsnIWxb9GJVpyboHo9fN6Sg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751388337; c=relaxed/simple; bh=rxWh8Ki7CjYyO7eLldQromF2K92SNjwMD3JOw4J+aS8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=raL2s83e0URX9cx5aoVzIahOrHjMuKbIvqKpqEHrvGZRIgy/f7EItclu4MMmeMEj1adMUWQ6K7VkBeMxFyGpveEGt+QKHUnNzCYimP+rlG/HKzqEAPb5NOQ5HrDXSjTb8pdhzL6HdHnYBUF84dlbPBenqfqfistUCaYnNat1Tzk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=eR9QmGzH; 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="eR9QmGzH" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E886CC4CEEB; Tue, 1 Jul 2025 16:45:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751388337; bh=rxWh8Ki7CjYyO7eLldQromF2K92SNjwMD3JOw4J+aS8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eR9QmGzHQTPJOUXHscZaXBiFkTMBbyTzR7G/sBld7Ic9V60jXRqf0m5M5vMZ7wnKU cGnWMNe+NAuvcs/Xk4dPNlNxSMBH3BBY6cXSO+ztptHOI/AsT9DSr4gJoHjC3wMhir 2uaEB2a2bLz6mV6zgTIDZ3pw2vIIv6qKD5oQynQ3S10ZaACE8f92q/ulo0Sd06tT9k SXMkoKdYpgENjdYG+qf2++gTzhJDr5lvHbFli5S/CY4yOZvuRF5Ri2kb8EhxdW2aYu Zc+a9cnrZl0qcGQ5/FXC4PTEFttpitUVXiV/QT5tVh/P2frGhVHvT65AWOfKc3sFG2 qOifKOFuNz3ng== 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 v2 8/8] vsock/virtio: Allocate nonlinear SKBs for handling large transmit buffers Date: Tue, 1 Jul 2025 17:45:07 +0100 Message-Id: <20250701164507.14883-9-will@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250701164507.14883-1-will@kernel.org> References: <20250701164507.14883-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. Signed-off-by: Will Deacon Reviewed-by: Stefano Garzarella Tested-by: Lei Yang --- 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..f74677c3511e 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); + 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