From nobody Sat Sep 26 20:51:08 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=google.com ARC-Seal: i=1; a=rsa-sha256; t=1789598392; cv=none; d=zohomail.com; s=zohoarc; b=j+3c/ZhR/alrFBJCgKlWsDFxRFCFj3V8y+leGtneJZAs/cgPDMVXQGwL5nIzTMQIgY8g9VgqyqFz2f5QXLaXakg0Hr5cfbVtIuOdnDYzE3KS9aYfD92LfxD6QeRx4ZGDpdn4LYT3Cqn9cEMKBdrwK2GXh9lfDIm8B874dfI1BCk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1789598392; h=Content-Type:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=ofTJnNz39s39xGGcF18xN1v2YtrLJYeKv8kRZ+0FYAY=; b=VxorDVmBz9HLTTgA3BJZnISUyUYYt/wuM5siMud7HZ+CC/ETDSRXvoNy5HxnE372qE6aahMdabcsR6hN5T0FTQElzgJxtF8RoBBoOXLOrUQlxwG9Gc2ruWNFL4ecrXATcYHmKaXsXt1Abzt14DfM6Ht1AECdxiTRZJdr5y5pgaA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1789598392300603.5008768482694; Wed, 16 Sep 2026 15:39:52 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1x6yHL-00037p-6P; Wed, 16 Sep 2026 18:38:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3dxqragsKCrQhUgUhaofUncaiiafY.WigkYgo-XYpYfhihaho.ila@flex--namangulati.bounces.google.com>) id 1x6yHE-00036S-Ts for qemu-devel@nongnu.org; Wed, 16 Sep 2026 18:38:52 -0400 Received: from mail-qt1-x846.google.com ([2607:f8b0:4864:20::846]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3dxqragsKCrQhUgUhaofUncaiiafY.WigkYgo-XYpYfhihaho.ila@flex--namangulati.bounces.google.com>) id 1x6yHC-0006zM-Bs for qemu-devel@nongnu.org; Wed, 16 Sep 2026 18:38:51 -0400 Received: by mail-qt1-x846.google.com with SMTP id d75a77b69052e-530f5e827c6so3013331cf.0 for ; Wed, 16 Sep 2026 15:38:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789598327; x=1790203127; darn=nongnu.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=ofTJnNz39s39xGGcF18xN1v2YtrLJYeKv8kRZ+0FYAY=; b=QVBsZIklWEIQ3gj8bVF/5GLoShI0trujD+flvtOTsptY7ReaSYwq9j/6tBWUhk2mQB 5FceJy+O072wlMtuR1NgC8NXgH5D+JLbuMcCIDCYK/IygI7k2tXNjVSjWJ5sAOohhGMO XeLzDcGJ3Hnujb5spzLAaYlXJqFNCmfOWd37CI/fpF06nMTtj8kTMrgJEvWTmzfMlcop jWFjdefW8GGOdQmRCtVYy8i1WnkcBYWHOlxGOxEwga2F0cHx2Qxo7GFFrYaQnD9TO4BB sc6OC0F9tItpCpRJTrCs3fErvzTPvUlAbpR+LSVIAwPxv/VNJjg77EGwZaXA91yK01h3 3jgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789598327; x=1790203127; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=ofTJnNz39s39xGGcF18xN1v2YtrLJYeKv8kRZ+0FYAY=; b=B9L6789LgDivqRaEyOWk8G32yDY8eCrOMB0oBE1WfUshTIFugL/Qk0Kuf63LewHtKq 1BbxqoZmoE8oFeJbF4hZ/ahG7h9gciiwKGU02+ORAKq+yl2Pvhi1Sdekkl8eJjdjVIrd Z5R8lsUgOANcU8Bzyh0c6JxBttG1WzQ/oUpVkEzeX0D3UAT2M2gMeNy7nOFkcCi7hWH1 Gj27qrgdNOf0sDbgjnEEo+m8iSm/yI840YlbXs4VVl5aybZqucdVnYDeVo7NNiID0wpa RGpNzMH/TecUx2U9nwftCd0Z3Kd64AgimRHzWaMbndp395Ac4zb9K6kcB2VGSLNd4zCt N9hg== X-Gm-Message-State: AFuF++mTcf4S/rNtdpLjqbb8uW5PwI+/ZeA+cpg8IfTslFi771KANxeD zNHFkZSHcKJ96TbeqfAhkX0lixgGGKVfAP65UiUQt/ynjhuNOtPgXcoaIkqrXOofOjcrQj2/rym HXS1Ay136bch1MQx+8sGX9oF6dIxwj/C8uE6DFIlLs+AoSRFB9LPKQNsFQXko/cB7NOY5ZSzgXK jBPDRN2M2Xh0j3/vu4cokZqXFcg068+QFEqa+LeDc8QqHM6vwt9TI0QLwGjWIJmA== X-Received: from qtbli7-n2.prod.google.com ([2002:a05:622a:8447:20b0:532:8d89:9699]) (user=namangulati job=prod-delivery.src-stubby-dispatcher) by 2002:a05:622a:2489:b0:531:11ac:23ce with SMTP id d75a77b69052e-5327f17c679mr67973611cf.49.1789598327132; Wed, 16 Sep 2026 15:38:47 -0700 (PDT) Date: Wed, 16 Sep 2026 22:38:40 +0000 In-Reply-To: <20260916223843.3995552-1-namangulati@google.com> Mime-Version: 1.0 References: <20260916223843.3995552-1-namangulati@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260916223843.3995552-2-namangulati@google.com> Subject: [PATCH v3 1/4] vfio-user: add multi-fd region mmap capability From: Naman Gulati To: qemu-devel@nongnu.org Cc: John Levon , Thanos Makatos , "=?UTF-8?q?C=C3=A9dric=20Le=20Goater?=" , Alex Williamson , Pierrick Bouvier , Matt Evans , Naman Gulati Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::846; envelope-from=3dxqragsKCrQhUgUhaofUncaiiafY.WigkYgo-XYpYfhihaho.ila@flex--namangulati.bounces.google.com; helo=mail-qt1-x846.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5, WEIRD_QUOTING=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @google.com) X-ZM-MESSAGEID: 1789598393478158500 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extend the vfio-user protocol specification to allow servers to pass multiple file descriptors for different sparse memory areas within a single device region with capability VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS. VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS defines an array of sparse mmap areas where each area contains: - offset: Start of the area within the device region (BAR). - fd_offset: Start of the mapping within the backing file descriptor. - size: Size of the mappable area. - fd_index: Zero-based index into the array of file descriptors received in the SCM_RIGHTS ancillary data of the VFIO_USER_DEVICE_GET_REGION_INFO reply message. Signed-off-by: Naman Gulati --- docs/interop/vfio-user.rst | 71 +++++++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 4 deletions(-) diff --git a/docs/interop/vfio-user.rst b/docs/interop/vfio-user.rst index 1c92f442d2..5744f58e31 100644 --- a/docs/interop/vfio-user.rst +++ b/docs/interop/vfio-user.rst @@ -125,7 +125,10 @@ Sparse Regions A region can be memory-mappable in whole or in part. When only a subset of= a region can be mapped by the client, a ``VFIO_REGION_INFO_CAP_SPARSE_MMAP`` capability is included in the region info reply. This capability describes -which portions can be mapped by the client. +which portions can be mapped by the client. When sparse mmap areas are bac= ked +by multiple distinct file descriptors, a +``VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS`` capability is included in the reg= ion +info reply instead. =20 .. Note:: For example, in a virtual NVMe controller, sparse regions can be used so @@ -859,9 +862,9 @@ Reply * ``VFIO_REGION_INFO_FLAG_READ`` allows client read access to the region. * ``VFIO_REGION_INFO_FLAG_WRITE`` allows client write access to the regi= on. * ``VFIO_REGION_INFO_FLAG_MMAP`` specifies the client can mmap() the reg= ion. - When this flag is set, the reply will include a file descriptor in its - meta-data. On ``AF_UNIX`` sockets, the file descriptors will be passed= as - ``SCM_RIGHTS`` type ancillary data. + When this flag is set, the reply will include one or more file descrip= tors + in its meta-data. On ``AF_UNIX`` sockets, the file descriptors will be= passed + as ``SCM_RIGHTS`` type ancillary data. * ``VFIO_REGION_INFO_FLAG_CAPS`` indicates additional capabilities found= in the reply. =20 @@ -950,6 +953,66 @@ The VFIO sparse mmap area is defined in ```` (``struct vfio_region_info_cap_sparse_mmap``). =20 =20 +VFIO sparse mmap fds cap header +""""""""""""""""""""""""""""""" + ++----------------------+--------------------------------------+ +| Name | Value | ++=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D+=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D+ +| id | VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS | ++----------------------+--------------------------------------+ +| version | 0x1 | ++----------------------+--------------------------------------+ +| next | | ++----------------------+--------------------------------------+ +| sparse mmap fds info | VFIO region info sparse mmap fds | ++----------------------+--------------------------------------+ + +This capability is defined when a region supports direct access by the cli= ent +via mmap() where different sparse mmap areas are backed by distinct file +descriptors in the ``SCM_RIGHTS`` ancillary data array. Capability ID +``VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS`` is defined as 16 (0x10) to avoid +clashing with kernel VFIO capabilities. + +VFIO region info cap sparse mmap fds +"""""""""""""""""""""""""""""""""""" + ++-----------+--------+------+ +| Name | Offset | Size | ++=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D+=3D=3D=3D=3D=3D=3D=3D=3D+=3D=3D=3D=3D= =3D=3D+ +| nr_areas | 0 | 4 | ++-----------+--------+------+ +| reserved | 4 | 4 | ++-----------+--------+------+ +| offset | 8 | 8 | ++-----------+--------+------+ +| fd_offset | 16 | 8 | ++-----------+--------+------+ +| size | 24 | 8 | ++-----------+--------+------+ +| fd_index | 32 | 4 | ++-----------+--------+------+ +| pad | 36 | 4 | ++-----------+--------+------+ +| ... | | | ++-----------+--------+------+ + +* *nr_areas* is the number of sparse mmap areas in the region. +* *offset*, *fd_offset*, *size*, and *fd_index* describe a single area tha= t can be + mapped by the client. There will be *nr_areas* tuples of offset, fd_offs= et, size, + fd_index, and pad. +* *offset* specifies the start of the area within the device region (BAR). +* *fd_offset* specifies the start of the mapping within the backing file d= escriptor + referenced by *fd_index*. +* *fd_index* is the zero-based index into the array of file descriptors + received in the ``SCM_RIGHTS`` ancillary data of the + ``VFIO_USER_DEVICE_GET_REGION_INFO`` reply message. + +If all sparse mmap areas are backed by a single file descriptor, the server +may instead send ``VFIO_REGION_INFO_CAP_SPARSE_MMAP`` and exactly one file +descriptor in ``SCM_RIGHTS`` for compatibility with older clients. + + ``VFIO_USER_DEVICE_GET_REGION_IO_FDS`` -------------------------------------- =20 --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Sat Sep 26 20:51:08 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=google.com ARC-Seal: i=1; a=rsa-sha256; t=1789598397; cv=none; d=zohomail.com; s=zohoarc; b=TbpBMJUuhjp3VxA8r8ZO7F9r7OG3rwZyPJ/V35siqiE65VRIQP9tJ1CumibBgGEUUlkuwiO9e5Iu/oHvr0BiAfYmVTR9WMBaAAGqvBfCNQVeIt08GlkINzjWG6YE8kvHsEvZM62ox7FGBVyY6xM59antE6WKYdTF4j+s9NOUScA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1789598397; h=Content-Type:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=63YJ0YQfQIblNFZSy78YMF4B/IKo3iiX9RepmEXFxiU=; b=ZKFni3HY8FmY7yasy8k0KmdMGWosLhnMZa2CW6Y1GLRI6z6VnMVoAFvdysEL0M4nCcNfnR2gXQ+woXzkAfn/NkACtbGcHS8jWWVf8YE857YPcHuSp3dWWXkpKGxuL3jckCYsJvOMCb0y7rKCVXVSDkfhdaLjXC+PddlYsFLYRyc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1789598397912519.8731659822957; Wed, 16 Sep 2026 15:39:57 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1x6yHK-00037T-59; Wed, 16 Sep 2026 18:38:58 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3eBqragsKCrUiVhVibpgVodbjjbgZ.XjhlZhp-YZqZgijibip.jmb@flex--namangulati.bounces.google.com>) id 1x6yHF-00036f-KS for qemu-devel@nongnu.org; Wed, 16 Sep 2026 18:38:53 -0400 Received: from mail-qk1-x747.google.com ([2607:f8b0:4864:20::747]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3eBqragsKCrUiVhVibpgVodbjjbgZ.XjhlZhp-YZqZgijibip.jmb@flex--namangulati.bounces.google.com>) id 1x6yHC-0006zR-Ft for qemu-devel@nongnu.org; Wed, 16 Sep 2026 18:38:52 -0400 Received: by mail-qk1-x747.google.com with SMTP id af79cd13be357-934956beec8so34862585a.0 for ; Wed, 16 Sep 2026 15:38:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789598328; x=1790203128; darn=nongnu.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=63YJ0YQfQIblNFZSy78YMF4B/IKo3iiX9RepmEXFxiU=; b=mR05M2YmCQHTF1DgX8GE9wpnGMOSEDaOrSZz6o3wfP248UXgwWcUxeCaYYm8YeDF3x CZb14uRze5GrmscGIFL7MlxG6XfdMy7ffHbgaNkOQ8Nt+s97Sftn5qW4EtrRQccY9hgr boy21YPJ7O5tVx3QRzZYIt5zrQ+kyczKxqPJ83pzetAokn2VJiE1Y+hYDFXdwKJNs/Ws H5X5zbTQ/w+RQtByVcvOJl/PO0NrqX8ZFFMkK0ZMlVCcXR7CvC3xdTRe77rYcqdcJ6bp ZBhpSqRS2aDCC/S5kzj/NTp39kKCmAUL7kjdv/tVYxpkNv18gWdQ8iFQsTQqnhUIpuuX T+qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789598328; x=1790203128; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=63YJ0YQfQIblNFZSy78YMF4B/IKo3iiX9RepmEXFxiU=; b=VCYofUXsgA3SekrXkurpGGXc8NcBSMa1BeBuLKqaXBAU0uCVcKggYvesi75FgZFIkT +NVGS8k6989/GHATs1Ev8rX26ina8k4ZX3WOrhmPQoQoA7nuACyN5moyw1Ald6voh24h od9+feM8UnyXD6UzkS98fNiCD7ZmPlzcmGWg/CnAh0pQTEvyq/4m4i21duxBNJmQQeoR ovvRamUoCsJiRgz4QUtiTCUTQbDaT/QRoOnDIA1xFGrgk+W/TVI8eFR5BX9TrNHPRGL2 f/9Wv39JnAlV2Xmt5ghvZCN8J+NNVOCVaKpx6sNWboWIrep/1AE3Pnp482W3NccwIoQT mTig== X-Gm-Message-State: AFuF++lDGVZaQbBT3sEbCINQgyrYoMiiu26DUz6N1wYYoo6W8FRDgXkn cUGQMX5sv7w6+tlSh1fIk4JWHrkt331u8dCnXFNdxHL9w4UHj510HRbIL0KRGH7BIOJTR4SNiM9 KsKDsESyNv+2mjflAoXuLlKX6HqBXahpsyTkU+barUL8bbFpMSI+0a+wPpj//6hKXaycCOT5JeR 41jWTPrmGh5FyoHk4rVmZXav/LP2lTBYAxQSaTpIiypbtKsmfnMsCSajgmVTCRFQ== X-Received: from qkbe17-n2.prod.google.com ([2002:a05:620a:a411:20b0:939:fefa:5251]) (user=namangulati job=prod-delivery.src-stubby-dispatcher) by 2002:a05:620a:28cc:b0:939:6dea:4f1b with SMTP id af79cd13be357-93bb7947dd8mr718538685a.49.1789598328144; Wed, 16 Sep 2026 15:38:48 -0700 (PDT) Date: Wed, 16 Sep 2026 22:38:41 +0000 In-Reply-To: <20260916223843.3995552-1-namangulati@google.com> Mime-Version: 1.0 References: <20260916223843.3995552-1-namangulati@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260916223843.3995552-3-namangulati@google.com> Subject: [PATCH v3 2/4] vfio: add 'fd' and 'fd_offset' to VFIOMmap From: Naman Gulati To: qemu-devel@nongnu.org Cc: John Levon , Thanos Makatos , "=?UTF-8?q?C=C3=A9dric=20Le=20Goater?=" , Alex Williamson , Pierrick Bouvier , Matt Evans , Naman Gulati Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::747; envelope-from=3eBqragsKCrUiVhVibpgVodbjjbgZ.XjhlZhp-YZqZgijibip.jmb@flex--namangulati.bounces.google.com; helo=mail-qk1-x747.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @google.com) X-ZM-MESSAGEID: 1789598399654158500 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Allow each sparse mmap subregion to specify its own file descriptor and file offset for the mmap call. Add 'fd' (set to -1 when the region fd should be used) and 'fd_offset' to VFIOMmap, and update vfio_region_mmap() to use them. Signed-off-by: Naman Gulati --- hw/vfio/vfio-region.h | 4 +++- hw/vfio/pci.c | 2 ++ hw/vfio/region.c | 26 +++++++++++++++++++------- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/hw/vfio/vfio-region.h b/hw/vfio/vfio-region.h index 58b236f113..9f6a5cf7d5 100644 --- a/hw/vfio/vfio-region.h +++ b/hw/vfio/vfio-region.h @@ -14,8 +14,10 @@ typedef struct VFIOMmap { MemoryRegion mem; void *mmap; - off_t offset; + off_t offset; /* offset within the region */ size_t size; + int fd; /* per-mmap fd, or -1 to use the region fd */ + uint64_t fd_offset; /* offset within fd for the mmap call */ } VFIOMmap; =20 typedef struct VFIODevice VFIODevice; diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c index c204706e63..0a61a060d9 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -1608,6 +1608,7 @@ static void vfio_pci_fixup_msix_region(VFIOPCIDevice = *vdev) vdev->msix->table_bar, 0, 0); } else { region->mmaps[0].offset =3D end; + region->mmaps[0].fd_offset +=3D end; region->mmaps[0].size =3D region->size - end; trace_vfio_msix_fixup(vdev->vbasedev.name, vdev->msix->table_bar, region->mmaps[0].offs= et, @@ -1634,6 +1635,7 @@ static void vfio_pci_fixup_msix_region(VFIOPCIDevice = *vdev) region->mmaps[0].offset + region->mmaps[0].s= ize); =20 region->mmaps[1].offset =3D end; + region->mmaps[1].fd_offset +=3D end; region->mmaps[1].size =3D region->size - end; trace_vfio_msix_fixup(vdev->vbasedev.name, vdev->msix->table_bar, region->mmaps[1].offs= et, diff --git a/hw/vfio/region.c b/hw/vfio/region.c index dbde339180..3c746200ca 100644 --- a/hw/vfio/region.c +++ b/hw/vfio/region.c @@ -189,6 +189,9 @@ static int vfio_setup_region_sparse_mmaps(VFIORegion *r= egion, sparse->areas[i].size - 1); region->mmaps[j].offset =3D sparse->areas[i].offset; region->mmaps[j].size =3D sparse->areas[i].size; + region->mmaps[j].fd =3D -1; + region->mmaps[j].fd_offset =3D region->fd_offset + + sparse->areas[i].offset; j++; } } @@ -262,6 +265,8 @@ int vfio_region_setup(Object *obj, VFIODevice *vbasedev= , VFIORegion *region, region->mmaps =3D g_new0(VFIOMmap, region->nr_mmaps); region->mmaps[0].offset =3D 0; region->mmaps[0].size =3D region->size; + region->mmaps[0].fd =3D -1; + region->mmaps[0].fd_offset =3D region->fd_offset; } else if (ret) { return ret; } @@ -355,7 +360,6 @@ int vfio_region_mmap(VFIORegion *region) off_t map_offset =3D 0; size_t align; char *name; - int fd; =20 if (!region->mem || !region->nr_mmaps) { return 0; @@ -392,8 +396,6 @@ int vfio_region_mmap(VFIORegion *region) return ret; } =20 - fd =3D vfio_device_get_region_fd(region->vbasedev, region->nr); - map_align =3D (void *)ROUND_UP((uintptr_t)map_base, (uintptr_t)align); munmap(map_base, map_align - map_base); munmap(map_align + region->size, @@ -405,12 +407,22 @@ int vfio_region_mmap(VFIORegion *region) * offsets being in ascending order. */ for (i =3D 0; i < region->nr_mmaps; i++) { + int fd =3D region->mmaps[i].fd; + + if (fd < 0) { + fd =3D vfio_device_get_region_fd(region->vbasedev, region->nr); + } + if (fd < 0) { + ret =3D -EINVAL; + munmap(map_align + map_offset, region->size - map_offset); + goto no_mmap; + } + munmap(map_align + map_offset, region->mmaps[i].offset - map_offse= t); region->mmaps[i].mmap =3D mmap(map_align + region->mmaps[i].offset, region->mmaps[i].size, prot, MAP_SHARED | MAP_FIXED, fd, - region->fd_offset + - region->mmaps[i].offset); + region->mmaps[i].fd_offset); if (region->mmaps[i].mmap =3D=3D MAP_FAILED) { ret =3D -errno; /* @@ -455,8 +467,8 @@ int vfio_region_mmap(VFIORegion *region) =20 no_mmap: trace_vfio_region_mmap_fault(memory_region_name(region->mem), i, - region->fd_offset + region->mmaps[i].offs= et, - region->fd_offset + region->mmaps[i].offs= et + + region->mmaps[i].fd_offset, + region->mmaps[i].fd_offset + region->mmaps[i].size - 1, ret); =20 region->mmaps[i].mmap =3D NULL; --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Sat Sep 26 20:51:08 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=google.com ARC-Seal: i=1; a=rsa-sha256; t=1789598390; cv=none; d=zohomail.com; s=zohoarc; b=jlqRuhbCqxnKu0Lr6mcaOiFGzeJFjQDVqoM1VDn4/Sb8OF2Tzdvlvri1jNErkvBKimxQmdF19epk1v5BijqG7nqYr2hPbWUywy2sMcSMg+FrMghtDEjPOE9u63KgZ22D3S23ZoQUjDNOkEDv8Om2u1LNlDAnumIr60QrC/i0yjY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1789598390; h=Content-Type:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=iAdMALx2mMoLKGnIQ4bKYCrFV5RPfccmk6okLE1XBSc=; b=SA3EfSs8+SUwYLfDX/St9Mk75btVYxNLnv2zOmIGFijPTLYd4qcR3zjDNTJbJyhzggu2pDnX8O9f483LkFck5gBaTcNuMhlKHvU0UT01Ch+61XJx25I7acyh1kQckJ6FNC6IQUsOA1SoccfpSjATdBW6r29jYhBC3WCh0OPRxeo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1789598390298894.3822210526843; Wed, 16 Sep 2026 15:39:50 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1x6yHN-000386-7p; Wed, 16 Sep 2026 18:39:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3eRqragsKCrYjWiWjcqhWpeckkcha.Ykimaiq-Zarahjkjcjq.knc@flex--namangulati.bounces.google.com>) id 1x6yHF-00036g-Kf for qemu-devel@nongnu.org; Wed, 16 Sep 2026 18:38:53 -0400 Received: from mail-qk1-x746.google.com ([2607:f8b0:4864:20::746]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3eRqragsKCrYjWiWjcqhWpeckkcha.Ykimaiq-Zarahjkjcjq.knc@flex--namangulati.bounces.google.com>) id 1x6yHD-0006zV-GL for qemu-devel@nongnu.org; Wed, 16 Sep 2026 18:38:53 -0400 Received: by mail-qk1-x746.google.com with SMTP id af79cd13be357-93a082c014aso35328285a.0 for ; Wed, 16 Sep 2026 15:38:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789598329; x=1790203129; darn=nongnu.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=iAdMALx2mMoLKGnIQ4bKYCrFV5RPfccmk6okLE1XBSc=; b=jZfxIz4jRF/tmTRVLG5en5MDh8kuLoxjTOKOxkZ+4YC26ECW+waASnD7eTDaaGUKL4 5c+3Jo56d3b59gJl7MIIrb7MNNTAamMkVFrPc/Tyffb83Z8/FqlC/qf4tZOb//oql+yO 70zD3Ge9ovDWGaNr3nkJtaMp+wme/sZLpRVd6WQTrI99QU/ZtWUv2JpGCKPuTmfunGft tvPl7Tr3alPIUIIeN4huN5A9eToPe87S8NiHrFHRTHcYlyHR/VWTJUy0OP2qNgizynrv Wqn1ruPyAKInDaETufPvWGSKjVLMvcWoAMBJ4Oqv7sZSn1lfaY2jL/cTgQs6tpmckPcJ Ohjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789598329; x=1790203129; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=iAdMALx2mMoLKGnIQ4bKYCrFV5RPfccmk6okLE1XBSc=; b=q3J3G5MP+wR4vj1FNJrkIs8pWkooz2oRpyjUmsnSS2kSrhpUSziHOSMHDL3e4UgIIO dBu/GeTDd/n1ca4ASb+a3ZudsFoP3BLP4dv9/20z+F8BVj1jnn7e9mBFOYJBbX9r6mUl 8H0r1LVBhcyMPuIGln2BKUSYgkv5RtDYN9LFjHDoO5+4YGq5ZT4r45mxwxMf/SMN7EWw 8S29Nw0KVDrzVPkkQ61bq4JvbqkG1REKWOiD5YjN140ymEZVA/YAIvdQznIshToJqhgL UywY7DhtPZ07rW/9AN0NsCGr6W8PP/wWPqUdbQpKITXkRnlaIHzbdzHVUgGGyI1Bcl5t 5Aog== X-Gm-Message-State: AFuF++nh4NHM0aYm2g0LtVCaxat4nd28Kh+yWEnI9h8+tYU2aybH7kNV 9Q6YUy/3ybfvk4Q/0W1uaxkQuj2Ibui1AxtQVtfL/vqFJzH2OnzTPi/1HLmB8PspWhD9dN4D1Ba 7HCSV0L3ZIuTihlaXcga2ueEjaMzeiQ/1KxOP5s8PIv7obaW4iRy7PTcLkwsLRVwe7ed+FTItDP DVjqsg3NKhUmM4mnDrwM3vSJjFB3rk/1aMfekd+Dq3ON13La1JE7sbUrf1xNDv1w== X-Received: from qkbc9.prod.google.com ([2002:a05:620a:31c9:b0:939:2bbb:b2ae]) (user=namangulati job=prod-delivery.src-stubby-dispatcher) by 2002:a05:620a:2810:b0:939:a1d0:b5b3 with SMTP id af79cd13be357-93bc629d4ddmr207659985a.11.1789598329159; Wed, 16 Sep 2026 15:38:49 -0700 (PDT) Date: Wed, 16 Sep 2026 22:38:42 +0000 In-Reply-To: <20260916223843.3995552-1-namangulati@google.com> Mime-Version: 1.0 References: <20260916223843.3995552-1-namangulati@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260916223843.3995552-4-namangulati@google.com> Subject: [PATCH v3 3/4] vfio: add setup_sparse_mmaps hook to VFIODeviceIOOps From: Naman Gulati To: qemu-devel@nongnu.org Cc: John Levon , Thanos Makatos , "=?UTF-8?q?C=C3=A9dric=20Le=20Goater?=" , Alex Williamson , Pierrick Bouvier , Matt Evans , Naman Gulati Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::746; envelope-from=3eRqragsKCrYjWiWjcqhWpeckkcha.Ykimaiq-Zarahjkjcjq.knc@flex--namangulati.bounces.google.com; helo=mail-qk1-x746.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @google.com) X-ZM-MESSAGEID: 1789598391570158500 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a setup_sparse_mmaps callback to VFIODeviceIOOps so transport backends (such as vfio-user) can parse custom sparse mmap capabilities, falling back to vfio_default_setup_sparse_mmaps() to process shared VFIO mmap capabilities. Signed-off-by: Naman Gulati --- hw/vfio/vfio-region.h | 5 +++++ include/hw/vfio/vfio-device.h | 16 ++++++++++++++++ hw/vfio/region.c | 25 ++++++++++++++++++++++--- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/hw/vfio/vfio-region.h b/hw/vfio/vfio-region.h index 9f6a5cf7d5..a0c9533f9f 100644 --- a/hw/vfio/vfio-region.h +++ b/hw/vfio/vfio-region.h @@ -46,4 +46,9 @@ void vfio_region_mmaps_set_enabled(VFIORegion *region, bo= ol enabled); void vfio_region_exit(VFIORegion *region); void vfio_region_finalize(VFIORegion *region); =20 +struct vfio_region_info; +int vfio_default_setup_sparse_mmaps(VFIORegion *region, + struct vfio_region_info *info, + Error **errp); + #endif /* HW_VFIO_REGION_H */ diff --git a/include/hw/vfio/vfio-device.h b/include/hw/vfio/vfio-device.h index 8472420d3f..3136e152f2 100644 --- a/include/hw/vfio/vfio-device.h +++ b/include/hw/vfio/vfio-device.h @@ -43,6 +43,7 @@ enum { =20 typedef struct VFIODeviceOps VFIODeviceOps; typedef struct VFIODeviceIOOps VFIODeviceIOOps; +typedef struct VFIORegion VFIORegion; typedef struct VFIOMigration VFIOMigration; =20 typedef struct IOMMUFDBackend IOMMUFDBackend; @@ -218,6 +219,21 @@ struct VFIODeviceIOOps { int (*get_region_info)(VFIODevice *vdev, struct vfio_region_info *info, int *fd); =20 + /** + * @setup_sparse_mmaps + * + * Parse sparse mmap capabilities for a region and initialize region->= mmaps. + * + * @region: #VFIORegion to set up + * @info: region info struct containing capabilities + * @errp: pointer to Error*, to store an error if it happens + * + * Returns 0 on success, -ENODEV if not sparse, or negative error code. + */ + int (*setup_sparse_mmaps)(VFIORegion *region, + struct vfio_region_info *info, + Error **errp); + /** * @get_irq_info * diff --git a/hw/vfio/region.c b/hw/vfio/region.c index 3c746200ca..3525f4183f 100644 --- a/hw/vfio/region.c +++ b/hw/vfio/region.c @@ -162,9 +162,9 @@ static int vfio_mmap_compare_offset(const void *a, cons= t void *b) return 0; } =20 -static int vfio_setup_region_sparse_mmaps(VFIORegion *region, - struct vfio_region_info *info, - Error **errp) +int vfio_default_setup_sparse_mmaps(VFIORegion *region, + struct vfio_region_info *info, + Error **errp) { struct vfio_info_cap_header *hdr; struct vfio_region_info_cap_sparse_mmap *sparse; @@ -198,6 +198,25 @@ static int vfio_setup_region_sparse_mmaps(VFIORegion *= region, =20 region->nr_mmaps =3D j; region->mmaps =3D g_realloc(region->mmaps, j * sizeof(VFIOMmap)); + return 0; +} + +static int vfio_setup_region_sparse_mmaps(VFIORegion *region, + struct vfio_region_info *info, + Error **errp) +{ + int ret, i; + + if (region->vbasedev->io_ops && + region->vbasedev->io_ops->setup_sparse_mmaps) { + ret =3D region->vbasedev->io_ops->setup_sparse_mmaps(region, info,= errp); + } else { + ret =3D vfio_default_setup_sparse_mmaps(region, info, errp); + } + + if (ret) { + return ret; + } =20 /* * Sort sparse mmaps by offset to ensure proper handling of gaps --=20 2.55.0.1082.g2b9226bbc0-goog From nobody Sat Sep 26 20:51:08 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=reject dis=none) header.from=google.com ARC-Seal: i=1; a=rsa-sha256; t=1789598384; cv=none; d=zohomail.com; s=zohoarc; b=NnrXYTavrHlQk+c1MIdP21qDSb/uu4uHNxbkoXIHYbRuIcRl+sjOBhjNCL1qxJfR8bRhIDfHZo1iHXLH6O3HpOsIonzp6fFIb2Qr2ga5g/4+OfFm3KYy/UQlVzGOM/oyojXGuIEueQujgVAaoPD6eKRn7vJMvEdxyHJ925wdLvs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1789598384; h=Content-Type:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=b9efIHp4+HhSVXI85vz8ntN5hQXERDbX6ri96OxHJs4=; b=UUk7jbqVXpTiRzuNbVupCA46guyfVpIVp77aUOpiTN2hlCbyoPqD/X/hfF0573hrgW6t+/6eEb6eSttNJngvroX7uJN79oELUtL6sqCIS6sf8/iWZrCQBJpvJcRfqmrl0hQ8pvQBJ1462liZCXtyI8UA030+29ocQ24CEKekcQY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 178959838486663.18925167018472; Wed, 16 Sep 2026 15:39:44 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1x6yHJ-00037P-6j; Wed, 16 Sep 2026 18:38:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from <3ehqragsKCrckXjXkdriXqfdlldib.Zljnbjr-absbiklkdkr.lod@flex--namangulati.bounces.google.com>) id 1x6yHG-00036z-Uh for qemu-devel@nongnu.org; Wed, 16 Sep 2026 18:38:55 -0400 Received: from mail-qt1-x845.google.com ([2607:f8b0:4864:20::845]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3ehqragsKCrckXjXkdriXqfdlldib.Zljnbjr-absbiklkdkr.lod@flex--namangulati.bounces.google.com>) id 1x6yHE-0006zc-MB for qemu-devel@nongnu.org; Wed, 16 Sep 2026 18:38:54 -0400 Received: by mail-qt1-x845.google.com with SMTP id d75a77b69052e-52fb8f67601so1400181cf.3 for ; Wed, 16 Sep 2026 15:38:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1789598330; x=1790203130; darn=nongnu.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=b9efIHp4+HhSVXI85vz8ntN5hQXERDbX6ri96OxHJs4=; b=hBNfQ4TcW8+QWo1a8usxqg3Jw2VcLpw+cY6461iEeizjVj6MjVpOJrLPnysHFOyIvW Z9aeKJ0SCW8QhdowjTooTT527QVLxXeENORe5PVB0iDxIY5YAq4RhAIbQ22L4IHGKtpG nB9i57S6IEqW3yhThdSr96gXt82ujISe6dAK1CZAky3nDWg+GpwwqXg310+Ve1LpQTeH r8jPymfFDtCqdCSe2xEAmxTdP5exLQDHx3xBoObHMdMhH+r9pM1/pX6evWLBuhjSpmx+ q2Uk4R75O9xnvaPn5HMj2Az1QvHzwLvjKqHe879BPVPFGM5pRqHLH4+Qk2T8cnqvaSBr MQMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1789598330; x=1790203130; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=b9efIHp4+HhSVXI85vz8ntN5hQXERDbX6ri96OxHJs4=; b=Rc6iYj7YHFLcywg0Njdd7XTRpwHZROaXBJDIPoNlNaaeAgTbxfGM8UUzQE2kDcCCfj Y5yhNhq82wDwe2prsRT4y3AyvHdAknzkftDjm31+gMk6AkHFAkUjh2WrqIT9+wRzlB28 wyavCfv/s/ziGej6KXHih5S7I5/6LfyMZl0aR/C4XlRmkwkUzpIRy2ZxGArBiFpqiahV 8IbDK452YE5UhebOfIHeo4zSaJWA2EGg41EgeFtNKzSfdCo7gYjwMdDSuJQICCIgpnMR FMmID4QxcyXkLnR5qXwyALVzWLs0IE5Yq4g6tI1RP7x+qmKWYQ93QSqmZ9/HFbo5bR8n WU/w== X-Gm-Message-State: AFuF++kP2qTf+LYPiHaspjcEPMxw1ZlC9ah9oyg4mo7vcI2j+B1xxN5T GiK4g29t3N7YQJgV3N/q1k6XpQurr1Rwcm2jBVm0jNcLfS/jmyZojd24tFs/6b6mWqQGnJcJml9 YXCnpOo7ApGnr++uKCtWUJP766RHfB/hLkUimCMZlHQ/rEOsiVTojbZYsPs/Fp6ljo7i0dtQ9UA Hpt1kRnavINr1/8kmzGPSfZflvRitKnt/VtmjBNU8b9J3X8gKjsm57wDr3tqH7Mw== X-Received: from qtpf1.prod.google.com ([2002:ac8:4701:0:b0:530:fe8b:15f2]) (user=namangulati job=prod-delivery.src-stubby-dispatcher) by 2002:a05:622a:451:b0:530:aa4a:993a with SMTP id d75a77b69052e-5327eef1639mr80082431cf.41.1789598330125; Wed, 16 Sep 2026 15:38:50 -0700 (PDT) Date: Wed, 16 Sep 2026 22:38:43 +0000 In-Reply-To: <20260916223843.3995552-1-namangulati@google.com> Mime-Version: 1.0 References: <20260916223843.3995552-1-namangulati@google.com> X-Mailer: git-send-email 2.55.0.1082.g2b9226bbc0-goog Message-ID: <20260916223843.3995552-5-namangulati@google.com> Subject: [PATCH v3 4/4] vfio-user: support multiple FDs for sparse mmap regions From: Naman Gulati To: qemu-devel@nongnu.org Cc: John Levon , Thanos Makatos , "=?UTF-8?q?C=C3=A9dric=20Le=20Goater?=" , Alex Williamson , Pierrick Bouvier , Matt Evans , Naman Gulati Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::845; envelope-from=3ehqragsKCrckXjXkdriXqfdlldib.Zljnbjr-absbiklkdkr.lod@flex--namangulati.bounces.google.com; helo=mail-qt1-x845.google.com X-Spam_score_int: -95 X-Spam_score: -9.6 X-Spam_bar: --------- X-Spam_report: (-9.6 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_MED=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @google.com) X-ZM-MESSAGEID: 1789598386417158500 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add client support for capability VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS (capability ID 16) in vfio-user. Update vfio_user_device_io_get_region_info() to receive multiple file descriptors passed in SCM_RIGHTS ancillary data for a region, store them in VFIOUserProxy, and implement setup_sparse_mmaps in vfio_user_device_io_ops_sock to populate per-area file descriptors and fd_offset values. Signed-off-by: Naman Gulati --- hw/vfio-user/protocol.h | 22 ++++++++++ hw/vfio-user/proxy.h | 2 + hw/vfio-user/device.c | 90 +++++++++++++++++++++++++++++++++++++---- hw/vfio-user/proxy.c | 32 +++++++++++++-- 4 files changed, 135 insertions(+), 11 deletions(-) diff --git a/hw/vfio-user/protocol.h b/hw/vfio-user/protocol.h index c478d1353f..7b27a70db5 100644 --- a/hw/vfio-user/protocol.h +++ b/hw/vfio-user/protocol.h @@ -15,6 +15,8 @@ * SPDX-License-Identifier: GPL-2.0-or-later */ =20 +#define VFIO_USER_MAX_REGIONS 100 + typedef struct { uint16_t id; uint16_t command; @@ -166,6 +168,26 @@ typedef struct { uint64_t offset; } VFIOUserRegionInfo; =20 +/* + * VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS + */ +#define VFIO_REGION_INFO_CAP_SPARSE_MMAP_FDS 16 + +struct vfio_region_sparse_mmap_fd_area { + uint64_t offset; + uint64_t fd_offset; + uint64_t size; + uint32_t fd_index; + uint32_t pad; +}; + +struct vfio_region_info_cap_sparse_mmap_fds { + struct vfio_info_cap_header header; + uint32_t nr_areas; + uint32_t reserved; + struct vfio_region_sparse_mmap_fd_area areas[]; +}; + /* * VFIO_USER_DEVICE_GET_IRQ_INFO * imported from struct vfio_irq_info diff --git a/hw/vfio-user/proxy.h b/hw/vfio-user/proxy.h index 7b97460cc5..4b9834a436 100644 --- a/hw/vfio-user/proxy.h +++ b/hw/vfio-user/proxy.h @@ -72,6 +72,7 @@ typedef struct VFIOUserProxy { AioContext *ctx; QEMUBH *req_bh; bool async_ops; + VFIOUserFDs *region_fds[VFIO_USER_MAX_REGIONS]; =20 /* * above only changed when BQL is held @@ -111,6 +112,7 @@ bool vfio_user_validate_version(VFIOUserProxy *proxy, E= rror **errp); =20 VFIOUserFDs *vfio_user_getfds(int numfds); void vfio_user_putfds(VFIOUserMsg *msg); +void vfio_user_free_fds(VFIOUserFDs *fds); =20 void vfio_user_disable_posted_writes(VFIOUserProxy *proxy); =20 diff --git a/hw/vfio-user/device.c b/hw/vfio-user/device.c index b8d2b7c1a8..f70321c060 100644 --- a/hw/vfio-user/device.c +++ b/hw/vfio-user/device.c @@ -14,12 +14,14 @@ =20 #include "hw/vfio-user/device.h" #include "hw/vfio-user/trace.h" +#include "hw/vfio/vfio-region.h" +#include "hw/vfio/vfio-helpers.h" +#include "hw/vfio/trace.h" =20 /* * These are to defend against a malign server trying * to force us to run out of memory. */ -#define VFIO_USER_MAX_REGIONS 100 #define VFIO_USER_MAX_IRQS 50 =20 bool vfio_user_get_device_info(VFIOUserProxy *proxy, @@ -172,14 +174,17 @@ static int vfio_user_device_io_get_region_info(VFIODe= vice *vbasedev, struct vfio_region_info *in= fo, int *fd) { - VFIOUserFDs fds =3D { 0, 1, fd}; - int ret; + int fds[VFIO_USER_MAX_MAX_FDS]; + VFIOUserFDs user_fds =3D { 0, VFIO_USER_MAX_MAX_FDS, fds }; + int i, ret; + + *fd =3D -1; =20 if (info->index > vbasedev->num_initial_regions) { return -EINVAL; } =20 - ret =3D vfio_user_get_region_info(vbasedev->proxy, info, &fds); + ret =3D vfio_user_get_region_info(vbasedev->proxy, info, &user_fds); if (ret) { return ret; } @@ -187,10 +192,29 @@ static int vfio_user_device_io_get_region_info(VFIODe= vice *vbasedev, /* cap_offset in valid area */ if ((info->flags & VFIO_REGION_INFO_FLAG_CAPS) && (info->cap_offset < sizeof(*info) || info->cap_offset > info->args= z)) { - return -EINVAL; + ret =3D -EINVAL; + } else if (vfio_get_region_info_cap(info, + VFIO_REGION_INFO_CAP_SPARSE_MMAP_F= DS)) { + if (info->index < VFIO_USER_MAX_REGIONS && user_fds.recv_fds > 0) { + VFIOUserFDs *saved =3D vfio_user_getfds(user_fds.recv_fds); + + saved->recv_fds =3D user_fds.recv_fds; + memcpy(saved->fds, fds, user_fds.recv_fds * sizeof(int)); + vfio_user_free_fds(vbasedev->proxy->region_fds[info->index]); + vbasedev->proxy->region_fds[info->index] =3D saved; + return 0; + } + } else if (user_fds.recv_fds > 0) { + *fd =3D fds[0]; + fds[0] =3D -1; } =20 - return 0; + for (i =3D 0; i < user_fds.recv_fds; i++) { + if (fds[i] >=3D 0) { + close(fds[i]); + } + } + return ret; } =20 static int vfio_user_device_io_get_irq_info(VFIODevice *vbasedev, @@ -472,12 +496,64 @@ static int vfio_user_device_io_region_write(VFIODevic= e *vbasedev, uint8_t index, /* * Socket-based io_ops */ +static int vfio_user_device_io_setup_sparse_mmaps(VFIORegion *region, + struct vfio_region_info = *info, + Error **errp) +{ + struct vfio_info_cap_header *hdr; + struct vfio_region_info_cap_sparse_mmap_fds *sparse_fds; + VFIOUserFDs *user_fds =3D NULL; + int i, j =3D 0; + + hdr =3D vfio_get_region_info_cap(info, VFIO_REGION_INFO_CAP_SPARSE_MMA= P_FDS); + if (!hdr) { + return vfio_default_setup_sparse_mmaps(region, info, errp); + } + + sparse_fds =3D container_of(hdr, struct vfio_region_info_cap_sparse_mm= ap_fds, + header); + + trace_vfio_region_sparse_mmap_header(region->vbasedev->name, + region->nr, sparse_fds->nr_areas); + + if (region->nr < VFIO_USER_MAX_REGIONS) { + user_fds =3D region->vbasedev->proxy->region_fds[region->nr]; + } + + region->mmaps =3D g_new0(VFIOMmap, sparse_fds->nr_areas); + + for (i =3D 0; i < sparse_fds->nr_areas; i++) { + if (sparse_fds->areas[i].size) { + uint64_t end =3D sparse_fds->areas[i].offset + + sparse_fds->areas[i].size - 1; + int fd =3D -1; + + if (user_fds && + sparse_fds->areas[i].fd_index < user_fds->recv_fds) { + fd =3D user_fds->fds[sparse_fds->areas[i].fd_index]; + } + + trace_vfio_region_sparse_mmap_entry(i, sparse_fds->areas[i].of= fset, + end); + region->mmaps[j].offset =3D sparse_fds->areas[i].offset; + region->mmaps[j].fd_offset =3D sparse_fds->areas[i].fd_offset; + region->mmaps[j].size =3D sparse_fds->areas[i].size; + region->mmaps[j].fd =3D fd; + j++; + } + } + + region->nr_mmaps =3D j; + region->mmaps =3D g_realloc(region->mmaps, j * sizeof(VFIOMmap)); + return 0; +} + VFIODeviceIOOps vfio_user_device_io_ops_sock =3D { .device_feature =3D vfio_user_device_io_device_feature, .get_region_info =3D vfio_user_device_io_get_region_info, + .setup_sparse_mmaps =3D vfio_user_device_io_setup_sparse_mmaps, .get_irq_info =3D vfio_user_device_io_get_irq_info, .set_irqs =3D vfio_user_device_io_set_irqs, .region_read =3D vfio_user_device_io_region_read, .region_write =3D vfio_user_device_io_region_write, - }; diff --git a/hw/vfio-user/proxy.c b/hw/vfio-user/proxy.c index 197aee07bf..a686ffe25e 100644 --- a/hw/vfio-user/proxy.c +++ b/hw/vfio-user/proxy.c @@ -363,6 +363,8 @@ static int vfio_user_recv_one(VFIOUserProxy *proxy, Err= or **errp) } =20 memcpy(msg->fds->fds, fdp, numfds * sizeof(int)); + } else if (msg->fds !=3D NULL) { + msg->fds->recv_fds =3D 0; } =20 /* @@ -740,6 +742,10 @@ bool vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUse= rHdr *hdr, =20 qemu_mutex_unlock(&proxy->lock); =20 + if (!ok && fds !=3D NULL) { + fds->recv_fds =3D 0; + } + return ok; } =20 @@ -867,17 +873,30 @@ void vfio_user_send_error(VFIOUserProxy *proxy, VFIOU= serHdr *hdr, int error) } =20 /* - * Close FDs erroneously received in an incoming request. + * Close and free received FDs. */ -void vfio_user_putfds(VFIOUserMsg *msg) +void vfio_user_free_fds(VFIOUserFDs *fds) { - VFIOUserFDs *fds =3D msg->fds; int i; =20 + if (!fds) { + return; + } + for (i =3D 0; i < fds->recv_fds; i++) { - close(fds->fds[i]); + if (fds->fds[i] >=3D 0) { + close(fds->fds[i]); + } } g_free(fds); +} + +/* + * Close FDs erroneously received in an incoming request. + */ +void vfio_user_putfds(VFIOUserMsg *msg) +{ + vfio_user_free_fds(msg->fds); msg->fds =3D NULL; } =20 @@ -967,6 +986,7 @@ void vfio_user_set_handler(VFIODevice *vbasedev, void vfio_user_disconnect(VFIOUserProxy *proxy) { VFIOUserMsg *r1, *r2; + int i; =20 qemu_mutex_lock(&proxy->lock); =20 @@ -1027,6 +1047,10 @@ void vfio_user_disconnect(VFIOUserProxy *proxy) vfio_user_iothread =3D NULL; } =20 + for (i =3D 0; i < VFIO_USER_MAX_REGIONS; i++) { + vfio_user_free_fds(proxy->region_fds[i]); + } + g_free(proxy->sockname); g_free(proxy); } --=20 2.55.0.1082.g2b9226bbc0-goog