From nobody Sat Jul 4 21:11:22 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1782985670; cv=none; d=zohomail.com; s=zohoarc; b=U2ViHurAxnhJkBlFrJ+xOM4Cbi/464zbr8QUcPgTH8j4wpCgfZFH02pkHu7FBBFkdPglHIXusxdHEY6oMBwTt+kug0haHqWU4SLQPf9zg2lJTTGZ62Et49+hROIV6wbrk+w6Yg6A15iP2RygajuuwEh0UCmD4YQ+QGDztVMypRM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1782985670; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=BHsFCClOErBCb2hCTW5Kdx5hfrAxpl2r1lI9X/xFrPM=; b=G8SA+MUno/eXk7amwptNRX10tOr5U0f3kM6tf5MZtduJfnGsus2/33y9j50e4gQrp8gqgINLvuhqVJPd4j9XcC6/1GTbDVwhse/puQzo41K3w4Z9m2Wm9nJf/jsVJQ0Sk+LEERNv0D//zLnPf9h85nTUxKo4vRknzRwCjXr9Nfk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1782985670034128.1060653465869; Thu, 2 Jul 2026 02:47:50 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1351517.1608681 (Exim 4.92) (envelope-from ) id 1wfE11-0003zg-Cr; Thu, 02 Jul 2026 09:47:27 +0000 Received: by outflank-mailman (output) from mailman id 1351517.1608681; Thu, 02 Jul 2026 09:47:27 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wfE11-0003zZ-9r; Thu, 02 Jul 2026 09:47:27 +0000 Received: by outflank-mailman (input) for mailman id 1351517; Thu, 02 Jul 2026 09:47:26 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wfE10-0003mp-Dq for xen-devel@lists.xenproject.org; Thu, 02 Jul 2026 09:47:26 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wfE0z-00DHuU-7Z for xen-devel@lists.xenproject.org; Thu, 02 Jul 2026 11:47:25 +0200 Received: from [10.42.69.7] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a463395-bab6-0a2a0a5309dd-0a2a4507b6fc-44 for ; Thu, 02 Jul 2026 11:47:25 +0200 Received: from [209.85.221.51] (helo=mail-wr1-f51.google.com) by tlsNG-ef75cf.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6a4633ac-9c8e-0a2a45070019-d155dd33a968-3 for ; Thu, 02 Jul 2026 11:47:25 +0200 Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-4720f3bf164so186008f8f.1 for ; Thu, 02 Jul 2026 02:47:25 -0700 (PDT) Received: from RTRKN1313-LIN.domain.local ([89.216.37.146]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-477db8a4a15sm7539152f8f.14.2026.07.02.02.47.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Jul 2026 02:47:24 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=20251104 header.d=gmail.com header.i="@gmail.com" header.h="Cc:To:In-Reply-To:References:Message-Id:Content-Transfer-Encoding:MIME-Version:Subject:Date:From" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1782985644; x=1783590444; darn=lists.xenproject.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=BHsFCClOErBCb2hCTW5Kdx5hfrAxpl2r1lI9X/xFrPM=; b=ITmOfOavKJQSNtGMR+RLZvrYCDrxTxb3AUuhd/0VvvyaHZVIqPDS9qRr/S+hLpkL9D /XO0wJKGXeJUJRZ3h3zURv77ws5Aj2iS819OLKOITpGLGbMja8lECXReJFxR/K6juqL7 3C8VlmHGQaAy7Nw5cRs0TLdinIfx3lMfdqy49G2NuaDm1Ms9Z1bNjmKOiCquRL9gFG4U LopsGbWiIRGzEEKWEjCpRwE4E5QDNkegvexGieyfJpCmyZ6Wc289J3iuWyC1/XhqQWTw sHew7LmEnN30dXv0HhWjebNxOjyaBPids0LJKEIHCPXN2vs9m0mLMQfT5ou1gPmllkVu q8Uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782985644; x=1783590444; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=BHsFCClOErBCb2hCTW5Kdx5hfrAxpl2r1lI9X/xFrPM=; b=eqm2ne+C41bzLTg9V7AOfq7dfHs1L41BPnbncOR58SlIA30p6IDFWNKbzgYDZEpbh3 Ai9wF5MU4ZtJ3lsSI6YcWyiYN/k02YyiqwehiANyEHaA5h9KlaqIU+3Tm2mftHYshf5X h0ZK4XG17tNe/Ah2y4c/xWDic21cu+Gcl0X9YQmN8npOLmHlQWRVLEylzk1l0cdzmFua Rhrf2/5ySIILszqXqALpx4RimN55xJZrB7Uk3aDYSQ08uWxRExPEe86eFYNEu9655t/U e1yTg5HI+BfAv4gQIds5Q6oyzZuc6wfvLVY8svWJUeCrhI4LwQ3eAGB+B0srdc76X+tF g8Vg== X-Forwarded-Encrypted: i=1; AHgh+RpkPPzCltjP74RsT8YgvqwcJGOcF/dvzNqO9+mgFk7pKnYjaZ7jqklGQLfiAyhoxrT30RMWNWdkoz8=@lists.xenproject.org X-Gm-Message-State: AOJu0YzE+tHTkMG7uxP4ZhFNerldfUOYZob/gLY0AjCgfeII3q9TZIg5 YnNaKN0I0sWhnQVo1R1Oc+/yww/GGuE2T5W5CRxqLg9K25MLXfdD801s X-Gm-Gg: AfdE7cn5WoxBF86mu5m44DpbXiyn6SD7qVeR5uy39U1gg5VivjGJ0b9rDKLdIF7q+HE d1chUS7eVOnlO4zbggXNPEE/IImSnTZj9xG8fFvP7Pu6LVYIBD2meORvEuHfZ0mGzdk41Tb9rgd 5j0s4wtRllILB+cFFTeVygkM0UjaOjRhlBwRFIBWeM/3WscSI5jAWyNuGdqkMuFYOcOnCEqegKF rmvYdGeNMSfGV/kSZgMvLc092ts0jRJdzBsh7eSGX8RC2phiU58fh08dbustyZewZsZn5zxbIJY rHx2Trey51xDS/41dhm3I4doX28U/CoXqpobRxP7JubaJiNbIStMmbTVVZ6p3QQk52PPZK1Sw92 /5DkgjOlHSGzUhocnqAAce96ehjqLOjaK93dIrLqqdigyTzXCakFtjb5Ifdct4pJIXfg+cQVxxK VMPAf/GH/Efk4dAdZwJJVgJM2XdGjHO3mhIrQ+9FRAuQ== X-Received: by 2002:a05:6000:290f:b0:46f:9026:4971 with SMTP id ffacd0b85a97d-475de54e175mr13729534f8f.11.1782985644267; Thu, 02 Jul 2026 02:47:24 -0700 (PDT) From: Dusan Stojkovic Date: Thu, 02 Jul 2026 11:46:12 +0200 Subject: [PATCH RFC v3 1/2] vhost: accept Xen guest RAM sections for vhost-user MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260702-vhost-xen-foreign-mapping-v3-1-2b8ef913382b@rt-rk.com> References: <20260702-vhost-xen-foreign-mapping-v3-0-2b8ef913382b@rt-rk.com> In-Reply-To: <20260702-vhost-xen-foreign-mapping-v3-0-2b8ef913382b@rt-rk.com> To: qemu-devel@nongnu.org Cc: "Michael S. Tsirkin" , Stefano Garzarella , Stefano Stabellini , Anthony PERARD , "Edgar E. Iglesias" , xen-devel@lists.xenproject.org, Viresh Kumar , Dusan Stojkovic , Nikola Jelic X-Mailer: b4 0.13.0 X-purgate-ID: tlsNG-ef75cf/1782985645-FF33F25E-264A153B/0/0 X-purgate-type: clean X-purgate-size: 3365 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1782985670687158500 From: Dusan Stojkovic When QEMU runs as a Xen device model, the guest's RAM is not allocated by QEMU and is not backed by a file descriptor that could be shared with a vhost-user backend: accesses from QEMU go through the Xen mapcache and memory_region_get_fd() returns -1. vhost_section() therefore filters out every RAM section, the vhost memory listener registers no regions, and starting any vhost-user device fails with "Failed initializing vhost-user memory map". With VHOST_USER_PROTOCOL_F_XEN_MMAP the backend does not need an fd or a process-local mapping it maps guest memory itself through the Xen foreign mapping interface, using the guest physical address and domain id. Accept the Xen RAM region in vhost_section() so that it reaches the backend's memory table. The Xen grant region (xen.grants) must never be accepted: grant references can only be mapped individually on demand via address_space_map(), and deriving a host pointer for the whole region, as vhost_region_add_section() does, aborts in the Xen mapcache. Note that xen_mr_is_memory() returns true for both the RAM and the grants region, so the grants region is excluded explicitly. Because of the necessity to exlude xen.grants, the missing stub for xen_mr_is_grants is added so that it can be called from common code. Signed-off-by: Dusan Stojkovic Signed-off-by: Nikola Jelic --- hw/virtio/vhost.c | 18 ++++++++++++++++++ hw/xen/xen_stubs.c | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c index af41841b52..ffc692b3e2 100644 --- a/hw/virtio/vhost.c +++ b/hw/virtio/vhost.c @@ -29,6 +29,7 @@ #include "system/dma.h" #include "system/memory.h" #include "system/ramblock.h" +#include "system/xen.h" #include "trace.h" =20 /* enabled until disconnected backend stabilizes */ @@ -657,6 +658,23 @@ static bool vhost_section(struct vhost_dev *dev, Memor= yRegionSection *section) return false; } =20 + /* + * Under Xen, the guest's RAM is not backed by an fd that + * can be passed to a vhost-user backend. The backend + * instead maps guest memory through the Xen foreign mapping + * interface, by guest physical address and domain id (see + * VHOST_USER_PROTOCOL_F_XEN_MMAP), so accept the Xen RAM + * region even though it has no fd. + */ + if (xen_enabled()) { + if (xen_mr_is_memory(mr) && !xen_mr_is_grants(mr)) { + trace_vhost_section(mr->name); + return true; + } + trace_vhost_reject_section(mr->name, 4); + return false; + } + /* * Some backends (like vhost-user) can only handle memory regions * that have an fd (can be mapped into a different process). Filter diff --git a/hw/xen/xen_stubs.c b/hw/xen/xen_stubs.c index f830768d99..7af39bceb0 100644 --- a/hw/xen/xen_stubs.c +++ b/hw/xen/xen_stubs.c @@ -29,6 +29,11 @@ bool xen_mr_is_memory(const MemoryRegion *mr) g_assert_not_reached(); } =20 +bool xen_mr_is_grants(const MemoryRegion *mr) +{ + g_assert_not_reached(); +} + bool xen_map_cache_enabled(void) { return false; --=20 2.43.0 From nobody Sat Jul 4 21:11:22 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1782985672; cv=none; d=zohomail.com; s=zohoarc; b=fIytZQK2sWZH6KO6zc5KYJ2aVC+Ulgm3wAIvbafhFhYgGU+WAisaae2A5I2zL+pL1aVbRZV6LOPSH+1QEBYFbvzgsP3ugWUKtRcQmPhier68m5Jm46qGuzo1Y6bAOOXZGBucasamYoaOJ/k52ameM7gaxCdeLjb6dKX5Y3ju6oo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1782985672; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=ylBpQ0bt/QK9zQ9BEMnv21TmNMn+xhUv/pb6/Do4RGs=; b=nxePFFVpb/VE4TZW4Zgacp2OOSiNo6FNJKFrnE4yKC0xs51X9MGvLTDERR/Jsffs3RKoNiKjGViltudvvlIxzmIVq2y+Y77xttE6vcKuWaD/b3k267rSAgA+aIcSZcETEVEp6Soo0CNqgrwA/JTigztk7QitG1spHyV2rMfx2ns= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1782985672750319.65708980500176; Thu, 2 Jul 2026 02:47:52 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.1351518.1608690 (Exim 4.92) (envelope-from ) id 1wfE15-0004Eq-LH; Thu, 02 Jul 2026 09:47:31 +0000 Received: by outflank-mailman (output) from mailman id 1351518.1608690; Thu, 02 Jul 2026 09:47:31 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wfE15-0004Ee-HL; Thu, 02 Jul 2026 09:47:31 +0000 Received: by outflank-mailman (input) for mailman id 1351518; Thu, 02 Jul 2026 09:47:30 +0000 Received: from mx.expurgate.net ([195.190.135.10]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1wfE14-0004Dm-Oq for xen-devel@lists.xenproject.org; Thu, 02 Jul 2026 09:47:30 +0000 Received: from mx.expurgate.net (helo=localhost) by mx.expurgate.net with esmtp id 1wfE14-00DI0b-4w for xen-devel@lists.xenproject.org; Thu, 02 Jul 2026 11:47:30 +0200 Received: from [10.42.69.2] (helo=localhost) by localhost with ESMTP (eXpurgate MTA 0.9.1) (envelope-from ) id 6a4633ad-bab6-0a2a0a5309dd-0a2a4502a6dc-20 for ; Thu, 02 Jul 2026 11:47:30 +0200 Received: from [209.85.221.45] (helo=mail-wr1-f45.google.com) by tlsNG-720697.mxtls.expurgate.net with ESMTPS (eXpurgate 4.57.1) (envelope-from ) id 6a4633ae-5a27-0a2a45020019-d155dd2df0fe-3 for ; Thu, 02 Jul 2026 11:47:26 +0200 Received: by mail-wr1-f45.google.com with SMTP id ffacd0b85a97d-4763b0c1dcdso1632335f8f.2 for ; Thu, 02 Jul 2026 02:47:26 -0700 (PDT) Received: from RTRKN1313-LIN.domain.local ([89.216.37.146]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-477db8a4a15sm7539152f8f.14.2026.07.02.02.47.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Jul 2026 02:47:26 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" Authentication-Results: eu.smtp.expurgate.cloud; dkim=pass header.s=20251104 header.d=gmail.com header.i="@gmail.com" header.h="Cc:To:In-Reply-To:References:Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date:From" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1782985646; x=1783590446; darn=lists.xenproject.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=ylBpQ0bt/QK9zQ9BEMnv21TmNMn+xhUv/pb6/Do4RGs=; b=Ff1iX/hV73wsQ6BINWM9CpYruivuosCueau4Yv4HSs0BKcCQv9Y8fqJmf8964YuLhx ydu8XxV65ETj4EVw/9Zj/kFRcwGHp0r62C5jIBWaBkClP5vPtWkxXu+iLpbGwSjAMu0b tVLtszB4W6A2877XPSCCJkFrU9+5iUGEodRMeS8uWd0mKlf1cOWKv1YYE2Ms1o82DO4K MT/RTa+h6byM0cxmLuiPgmWtvWhFd4MGdukvppcZkEwNQTkKamTB1mMlAvctGrhC+ni5 Tv1cfIgqM5WD/B29p0Bt3jxFcYMiLlqslggb2X1zmb08jhHwZS9Y8yENVPr67e54nUQr Qujw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782985646; x=1783590446; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=ylBpQ0bt/QK9zQ9BEMnv21TmNMn+xhUv/pb6/Do4RGs=; b=AmvXrLJh+n0lOvp/ZcADQwcpy+hZL7n47uk6P6yhSPRXdhior11qpzSUwXHnLNA/0W PAz74Pu09VAMCSL28rGrpJFxqXAUMA8aK7lFVCnJ8jdwt9RzSU8YLqP/grZoQt2Q0yW0 yvNtSlkq0CQDz7jcM3t7h9SNr201nrXGNyaW/3IpObKOzzFEpWZ1b6bajnJQ8457GJin /WZgUfHcz6nZvo9n/kBbEq+esaVqd/41T4W/fK9fEUgAtjdATA3VSHBCt4P85aLvKN55 0rkz3LNoaoywpNO+CoMq8KDJ1m7d7vlfXEtoPZOSznZPVOpab9kfbKA/oBMqf8FB9pDf Utvg== X-Forwarded-Encrypted: i=1; AFNElJ8KgeDXSOkOEiJ4rkGRhOW490EsDM9kWmp/Hz+ga3AyArVjHQpTWsPpRrQvVbjtpIpqTJPXZVMch2w=@lists.xenproject.org X-Gm-Message-State: AOJu0Yysq0C9MyzdYV1+b97G0yfmVOQXdxeExJDmzYXBsvODFXnAoy+X tQx/P74YWCYJ4DJhJFUaCx5U8hvjpI1CEVW0XJJFeLbrB1t2zJEMYM0Y X-Gm-Gg: AfdE7cmVbrXD2qQeZ72FFMppTpev9cn9GUfxVOZ8VkhKh243BKGNIB2A5cKLEYlmLWL oGpUhRGRvt99n5lTjqrtW8n5GTGsQRJOI3lQL76ux7pKhloVPU1Ceh317zUnqpAZ/EqMHzIuMeI by3vRaw+Ly9BrQF5E0cBDpmbnkfnJNiOFgePvLm9jadbSznVCoZDgCxfdMd6yye+31RngIG3NwJ QE7JbmUKaYJKrT13EYQV6pV8tkes/Kmg357bzhOEITzR3KQ7ZJ/6gCNRJLj1Opc0nY67p4oS4hk PtfNDE6r1j4twEgPUx1FiHsZhaGn0yzZ03WeQs7nBC5ZrzEP/41MMY5V2siJmmearL1cNriPk39 XRvLY97Fi9MKEUjTH1INgxTNaA2d3oRI9IJkTKqGKm8+9q3YgTM/3xgJkplZOvmAo90Vuu27nhk e7e4FYbkTK+COSIyfmeGifFrtogaOW3idt+1QGPx2Nig== X-Received: by 2002:a05:600c:8286:b0:493:c42c:7e88 with SMTP id 5b1f17b1804b1-493c42c8307mr52961315e9.34.1782985646320; Thu, 02 Jul 2026 02:47:26 -0700 (PDT) From: Dusan Stojkovic Date: Thu, 02 Jul 2026 11:46:13 +0200 Subject: [PATCH RFC v3 2/2] vhost-user: implement VHOST_USER_PROTOCOL_F_XEN_MMAP MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260702-vhost-xen-foreign-mapping-v3-2-2b8ef913382b@rt-rk.com> References: <20260702-vhost-xen-foreign-mapping-v3-0-2b8ef913382b@rt-rk.com> In-Reply-To: <20260702-vhost-xen-foreign-mapping-v3-0-2b8ef913382b@rt-rk.com> To: qemu-devel@nongnu.org Cc: "Michael S. Tsirkin" , Stefano Garzarella , Stefano Stabellini , Anthony PERARD , "Edgar E. Iglesias" , xen-devel@lists.xenproject.org, Viresh Kumar , Dusan Stojkovic , Nikola Jelic X-Mailer: b4 0.13.0 X-purgate-ID: tlsNG-720697/1782985646-4FB1F7C5-5DBB5988/0/0 X-purgate-type: clean X-purgate-size: 12093 X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1782985674505158500 From: Dusan Stojkovic The vhost-user specification reserves protocol feature bit 17 and documents an extended memory region description for backends that map guest memory through Xen rather than mapping a file descriptor each region carries two extra fields, "xen mmap flags" and "domid" (see docs/interop/vhost-user.rst, "Memory region description"). The layout is implemented by rust-vmm's vhost and vm-memory crates and used by Xen vhost-user device backends. Implement the front-end side for foreign mappings: - negotiate VHOST_USER_PROTOCOL_F_XEN_MMAP - when negotiated, build SET_MEM_TABLE payloads from the extended region layout, with xen_mmap_flags =3D FOREIGN and xen_mmap_data set to the guest's domain id. - under Xen, do not call vhost_user_get_mr_data(): guest RAM has no fd and its userspace_addr does not correspond to a valid mapping in the address space. Backends map regions through privcmd using the guest physical address and domid; the fd accompanying each region only satisfies the protocol's one-fd-per-region requirement. Pass a /dev/xen/privcmd fd and close it once the message has been sent. Tracepoints for opening and closing xen fds are added as well. - suppress VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS under Xen: Postcopy is likewise refused. The userspace_addr field is carried unchanged; Xen backends derive mappings from guest_phys_addr and domid and do not interpret it. Signed-off-by: Dusan Stojkovic Signed-off-by: Nikola Jelic --- hw/virtio/trace-events | 2 + hw/virtio/vhost-user.c | 120 +++++++++++++++++++++++++++++++++++++= ++-- include/hw/virtio/vhost-user.h | 2 +- 3 files changed, 120 insertions(+), 4 deletions(-) diff --git a/hw/virtio/trace-events b/hw/virtio/trace-events index 2a57edc21e..0f3c58fd78 100644 --- a/hw/virtio/trace-events +++ b/hw/virtio/trace-events @@ -30,6 +30,8 @@ vhost_user_postcopy_fault_handler_found(int i, uint64_t r= egion_offset, uint64_t vhost_user_postcopy_listen(void) "" vhost_user_set_mem_table_postcopy(uint64_t client_addr, uint64_t qhva, int= reply_i, int region_i) "client:0x%"PRIx64" for hva: 0x%"PRIx64" reply %d r= egion %d" vhost_user_set_mem_table_withfd(int index, const char *name, uint64_t memo= ry_size, uint64_t guest_phys_addr, uint64_t userspace_addr, uint64_t offset= ) "%d:%s: size:0x%"PRIx64" GPA:0x%"PRIx64" QVA/userspace:0x%"PRIx64" RB off= set:0x%"PRIx64 +vhost_user_open_region_fd(int index, int fd) "region:%d fd:%d" +vhost_user_put_region_fds(int index, int fd) "region:%d fd:%d" vhost_user_postcopy_waker(const char *rb, uint64_t rb_offset) "%s + 0x%"PR= Ix64 vhost_user_postcopy_waker_found(uint64_t client_addr) "0x%"PRIx64 vhost_user_postcopy_waker_nomatch(const char *rb, uint64_t rb_offset) "%s = + 0x%"PRIx64 diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c index d627351f45..932ead4eeb 100644 --- a/hw/virtio/vhost-user.c +++ b/hw/virtio/vhost-user.c @@ -30,6 +30,8 @@ #include "migration/postcopy-ram.h" #include "trace.h" #include "system/ramblock.h" +#include "system/xen.h" +#include "hw/xen/xen.h" =20 #include #include @@ -181,12 +183,36 @@ typedef struct VhostUserMemoryRegion { uint64_t mmap_offset; } VhostUserMemoryRegion; =20 +/* + * Memory region flags for VHOST_USER_PROTOCOL_F_XEN_MMAP, matching the + * values used by rust-vmm's vm-memory (MmapXenFlags). + */ +#define VHOST_USER_XEN_MMAP_FLAG_FOREIGN 0x1 +#define VHOST_USER_XEN_MMAP_FLAG_GRANT 0x2 + +/* + * Extended memory region description, used when + * VHOST_USER_PROTOCOL_F_XEN_MMAP has been negotiated. + */ +typedef struct VhostUserMemoryRegionXen { + VhostUserMemoryRegion region; + uint32_t xen_mmap_flags; + uint32_t xen_mmap_data; /* domain id for FOREIGN/GRANT mappings */ +} VhostUserMemoryRegionXen; + + typedef struct VhostUserMemory { uint32_t nregions; uint32_t padding; VhostUserMemoryRegion regions[VHOST_MEMORY_BASELINE_NREGIONS]; } VhostUserMemory; =20 +typedef struct VhostUserMemoryXen { + uint32_t nregions; + uint32_t padding; + VhostUserMemoryRegionXen regions[VHOST_MEMORY_BASELINE_NREGIONS]; +} VhostUserMemoryXen; + typedef struct VhostUserMemRegMsg { uint64_t padding; VhostUserMemoryRegion region; @@ -294,6 +320,7 @@ typedef union { struct vhost_vring_state state; struct vhost_vring_addr addr; VhostUserMemory memory; + VhostUserMemoryXen memory_xen; VhostUserMemRegMsg mem_reg; VhostUserLog log; struct vhost_iotlb_msg iotlb; @@ -594,6 +621,8 @@ static MemoryRegion *vhost_user_get_mr_data(uint64_t ad= dr, ram_addr_t *offset, static bool vhost_user_gpa_addresses(struct vhost_dev *dev) { return vhost_user_has_protocol_feature( + dev, VHOST_USER_PROTOCOL_F_XEN_MMAP) || + vhost_user_has_protocol_feature( dev, VHOST_USER_PROTOCOL_F_GPA_ADDRESSES); } =20 @@ -612,6 +641,23 @@ static void vhost_user_fill_msg_region(struct vhost_de= v *dev, dst->mmap_offset =3D mmap_offset; } =20 +/* + * With VHOST_USER_PROTOCOL_F_XEN_MMAP the region fds are opened by us + * rather than owned by the RAMBlocks, so they must be closed once the + * message carrying them has been sent (or on error). + */ +static void vhost_user_put_region_fds(struct vhost_dev *dev, int *fds, + size_t fd_num) +{ + if (!vhost_user_has_protocol_feature(dev, VHOST_USER_PROTOCOL_F_XEN_MM= AP)) { + return; + } + for (size_t i =3D 0; i < fd_num; i++) { + trace_vhost_user_put_region_fds(i, fds[i]); + close(fds[i]); + } +} + static int vhost_user_fill_set_mem_table_msg(struct vhost_user *u, struct vhost_dev *dev, VhostUserMsg *msg, @@ -623,13 +669,41 @@ static int vhost_user_fill_set_mem_table_msg(struct v= host_user *u, MemoryRegion *mr; struct vhost_memory_region *reg; VhostUserMemoryRegion region_buffer; + bool xen_mmap =3D vhost_user_has_protocol_feature(dev, + VHOST_USER_PROTOCOL_F_XEN_MMAP); + + if (track_ramblocks && xen_mmap) { + error_report("vhost-user: postcopy is not supported under Xen"); + return -ENOTSUP; + } =20 msg->hdr.request =3D VHOST_USER_SET_MEM_TABLE; =20 for (i =3D 0; i < dev->mem->nregions; ++i) { reg =3D dev->mem->regions + i; =20 - mr =3D vhost_user_get_mr_data(reg->userspace_addr, &offset, &fd); + if (xen_mmap) { + /* + * Under Xen the guest RAM is not mapped into our address + * space; the backend maps it through the Xen foreign + * mapping interface using the guest physical address and + * domain id carried in the region descriptor. The file + * descriptor only satisfies the one-fd-per-region + * requirement of the protocol: pass /dev/xen/privcmd and + * close it once the message has been sent. + */ + mr =3D NULL; + offset =3D 0; + fd =3D open("/dev/xen/privcmd", O_RDWR | O_CLOEXEC); + if (fd < 0) { + error_report("vhost-user: failed to open /dev/xen/privcmd:" + " %s", strerror(errno)); + return -errno; + } + trace_vhost_user_open_region_fd(i, fd); + } else { + mr =3D vhost_user_get_mr_data(reg->userspace_addr, &offset, &f= d); + } if (fd > 0) { if (track_ramblocks) { assert(*fd_num < VHOST_MEMORY_BASELINE_NREGIONS); @@ -642,10 +716,21 @@ static int vhost_user_fill_set_mem_table_msg(struct v= host_user *u, u->region_rb[i] =3D mr->ram_block; } else if (*fd_num =3D=3D VHOST_MEMORY_BASELINE_NREGIONS) { error_report("Failed preparing vhost-user memory table msg= "); + if (xen_mmap) { + close(fd); + } return -ENOBUFS; } vhost_user_fill_msg_region(dev, ®ion_buffer, reg, offset); - msg->payload.memory.regions[*fd_num] =3D region_buffer; + if (xen_mmap) { + msg->payload.memory_xen.regions[*fd_num].region =3D region= _buffer; + msg->payload.memory_xen.regions[*fd_num].xen_mmap_flags = =3D + VHOST_USER_XEN_MMAP_FLAG_FOREIGN; + msg->payload.memory_xen.regions[*fd_num].xen_mmap_data =3D + xen_domid; + } else { + msg->payload.memory.regions[*fd_num] =3D region_buffer; + } fds[(*fd_num)++] =3D fd; } else if (track_ramblocks) { u->region_rb_offset[i] =3D 0; @@ -663,7 +748,11 @@ static int vhost_user_fill_set_mem_table_msg(struct vh= ost_user *u, =20 msg->hdr.size =3D sizeof(msg->payload.memory.nregions); msg->hdr.size +=3D sizeof(msg->payload.memory.padding); - msg->hdr.size +=3D *fd_num * sizeof(VhostUserMemoryRegion); + if (xen_mmap) { + msg->hdr.size +=3D *fd_num * sizeof(VhostUserMemoryRegionXen); + } else { + msg->hdr.size +=3D *fd_num * sizeof(VhostUserMemoryRegion); + } =20 return 0; } @@ -1149,10 +1238,12 @@ static int vhost_user_set_mem_table(struct vhost_de= v *dev, ret =3D vhost_user_fill_set_mem_table_msg(u, dev, &msg, fds, &fd_n= um, false); if (ret < 0) { + vhost_user_put_region_fds(dev, fds, fd_num); return ret; } =20 ret =3D vhost_user_write(dev, &msg, fds, fd_num); + vhost_user_put_region_fds(dev, fds, fd_num); if (ret < 0) { return ret; } @@ -2551,6 +2642,29 @@ static int vhost_user_backend_init(struct vhost_dev = *dev, void *opaque, VHOST_USER_PROTOCOL_F_GET_VRING_BASE_INFLIG= HT); } =20 + if (!xen_enabled()) { + /* + * Xen memory mappings only make sense when QEMU itself runs + * as a Xen device model. + */ + protocol_features &=3D ~(1ULL << VHOST_USER_PROTOCOL_F_XEN_MMA= P); + } else { + if (!virtio_has_feature(protocol_features, + VHOST_USER_PROTOCOL_F_XEN_MMAP)) { + error_setg(errp, "vhost-user backend does not support " + "VHOST_USER_PROTOCOL_F_XEN_MMAP, which is " + "required when running under Xen"); + return -EPROTO; + } + /* + * The ADD/REM_MEM_REG message path has not been adapted to + * the Xen region format. Xen guests expose a single RAM + * region, so fall back to SET_MEM_TABLE. + */ + protocol_features &=3D + ~(1ULL << VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS); + } + /* final set of protocol features */ u->protocol_features =3D protocol_features; err =3D vhost_user_set_protocol_features(dev, u->protocol_features= ); diff --git a/include/hw/virtio/vhost-user.h b/include/hw/virtio/vhost-user.h index 06c360af18..46be9cd57c 100644 --- a/include/hw/virtio/vhost-user.h +++ b/include/hw/virtio/vhost-user.h @@ -30,7 +30,7 @@ enum VhostUserProtocolFeature { VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS =3D 14, VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS =3D 15, VHOST_USER_PROTOCOL_F_STATUS =3D 16, - /* Feature 17 reserved for VHOST_USER_PROTOCOL_F_XEN_MMAP. */ + VHOST_USER_PROTOCOL_F_XEN_MMAP =3D 17, VHOST_USER_PROTOCOL_F_SHARED_OBJECT =3D 18, VHOST_USER_PROTOCOL_F_DEVICE_STATE =3D 19, VHOST_USER_PROTOCOL_F_GET_VRING_BASE_INFLIGHT =3D 20, --=20 2.43.0