From nobody Sun Oct 25 09:31:20 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=1791362275; cv=none; d=zohomail.com; s=zohoarc; b=CTuvnnOGfThAgC1nNlF/V2iJ8Ro1P5O7iIZnLY8NegOFEMPcFQDIz7sdyLkowIrtdBD9Yn/rJOJz2XkdhyKqU1l+5Wx9h72KbEel/23W/IjXwVdoQIFE+xFP2ns1HhsbM0kppDncUF6VXM5WWHZVU7q3lLc5p6UCAWqtNk9Kgiw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791362275; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=C0I73QCKyMNmeu8wpKfVAtKeOubS0N3TmdbBuxaYXOo=; b=WQpIDgJOo5FuOJzRGJWcURG1qXl7qiB/QHoVYhurAT+xRlazz/TPLR4vWAFotbC7N0Bmh/qnR+eKlZyED7r/fJi6ru3zQd2AdmN3GTSw2yYpGIVHvjpeZaVwJOAfthBrdi1QxaENRGcpJS4cGXTPCGjI5I7rXFkDBEBrPmyyPJ0= 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 1791362275044605.3563519721058; Wed, 7 Oct 2026 01:37:55 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xEN8v-0006K9-Fd; Wed, 07 Oct 2026 04:36:55 -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 <3hI3FagsKChM6t5t6zD4tC1z77z4x.v759x5D-wxEx4676z6D.7Az@flex--namangulati.bounces.google.com>) id 1xEI5Z-0001OD-6I for qemu-devel@nongnu.org; Tue, 06 Oct 2026 23:13:30 -0400 Received: from mail-pg1-x545.google.com ([2607:f8b0:4864:20::545]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3hI3FagsKChM6t5t6zD4tC1z77z4x.v759x5D-wxEx4676z6D.7Az@flex--namangulati.bounces.google.com>) id 1xEI5P-0001T1-Rx for qemu-devel@nongnu.org; Tue, 06 Oct 2026 23:13:01 -0400 Received: by mail-pg1-x545.google.com with SMTP id 41be03b00d2f7-cc1ca15334cso3576109a12.1 for ; Tue, 06 Oct 2026 20:12:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1791342762; x=1791947562; 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=C0I73QCKyMNmeu8wpKfVAtKeOubS0N3TmdbBuxaYXOo=; b=OqwxRfL5LDYsVtePxcFPz83TwM+gKXwTzSlbMEpzDhuSbuCe1w09Cd62DPOiFvtLJe 7YjByT4009RCd14r9owBO5stU0mj3i39Jr/XzvNcBfkI4yT7vwssH4At7vMTZPpynim0 5zETNTxvzY54mbjMHNK+2vlRsUVdcNMymEwZ6LgzZJBumnRq6SFF6fA3Oq1eeBmjS0jE 2/dng4WEUDrO7cUIuyJBi4NksJOUyhUUcxAJ4rzF33svKaJF+ExHObxfrhjDPwKjqwI8 Tb0Q+1DGop5/S7X5tua+VZC3Vmwvb0i+yuBqM/dwYWfPMLAcxtJ9HZ0BHP+ABDQWVOcp BVPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1791342762; x=1791947562; 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=C0I73QCKyMNmeu8wpKfVAtKeOubS0N3TmdbBuxaYXOo=; b=qMXsY3/X9Zq2I3DkuHvWrL+LOMab2bBhjczDWXtVGcnTzjIxZ9dy3/XsZhY6/CQxGR Zr0n2Mir/OyBwo53TLaJzC5ZtT019Fgaofcp5y7HmrcuYHmjfIoRDDKZ2xJh68zwHdeP uMDXOjmJaUccirtVQuvMaPPwz2ugT3pPJkklQeHiAsiJ2aE93ds0ZQ8ouQG+fFHZhLnB 155g0RqhQ9ds95ZBHyzmEAUX2Z++rce2DONYb+QUMamKIspZCxN3dTilXVR9LyF4Q2bt e0mLkyjPqIvteGxh2yghUj2rgnySRk0B2Bmz/81KC41lSk00oa7cI/Jj3W+wVaHuMVdC JIpg== X-Gm-Message-State: AFuF++nSHf7UM1yfv87qCyECrsztv3lc2tm/r469LhNtzUBHVmbtWWRQ BmJLgzeeR5wBMhErFRQDyRdVyRjmvZPvOy4HAPYcIR6+N5tMvCTDtHciiqbGErSJbUMw549TL4J 13yoUq73Ju/8k+T2hpsQGF80K/FqNJZ1EMK8trdj1SDdrMPT++OS2suPkCgjBPJqD6cWcZmKSHf SY0l7gcQRWhFoM4TTqeFSSi/EQ6GCmuSVkcxMr1pThOU9QsUtQvQUlTTSwLaB2dA== X-Received: from qtmr24.prod.google.com ([2002:ac8:4258:0:b0:535:1f86:d438]) (user=namangulati job=prod-delivery.src-stubby-dispatcher) by 2002:a05:622a:11d3:b0:533:26bd:65e1 with SMTP id d75a77b69052e-53575545a29mr9893001cf.36.1791331716139; Tue, 06 Oct 2026 17:08:36 -0700 (PDT) Date: Wed, 7 Oct 2026 00:08:30 +0000 In-Reply-To: <20261007000833.2445641-1-namangulati@google.com> Mime-Version: 1.0 References: <20261007000833.2445641-1-namangulati@google.com> X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: <20261007000833.2445641-2-namangulati@google.com> Subject: [PATCH v3 RESEND 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::545; envelope-from=3hI3FagsKChM6t5t6zD4tC1z77z4x.v759x5D-wxEx4676z6D.7Az@flex--namangulati.bounces.google.com; helo=mail-pg1-x545.google.com X-Spam_score_int: -79 X-Spam_score: -8.0 X-Spam_bar: -------- X-Spam_report: (-8.0 / 5.0 requ) BAYES_00=-1.9, DATE_IN_PAST_03_06=1.592, 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=no 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: 1791362276872158500 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 Reviewed-by: Thanos Makatos --- 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.56.0.rc1.315.gc6ed9934b7-goog From nobody Sun Oct 25 09:31:20 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=1791362342; cv=none; d=zohomail.com; s=zohoarc; b=U/mNYgUMujnPwlAbTQUP+yGA7C3S90mDUbwbNQewZklpynAbzjwybXVw99GbAWrGUFYDNSw6868TGF168kLnVhESKADKDdqyhobQmWPYY221peS1VSN4dCN8PtQnfy5tzg3tWrOzbKUEJSYQYdKjs5quWpN3wy5por5I95MsArg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791362342; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=carpSQDzf24ZpvGYVcletgPsKvLeICa/ra3tGkAOE6o=; b=Kzw7pdjFezfwbaAjgir69fkaqdobSe+DcvSAp9tlp7HrvSj1kRe9Z1oRNnbx6AnqRh/RCV70GG8yvF7q/InBo48VgdToMUF6P1G+r6rV56Z1pvjYRw9iIxv91nzJQ2LGnjUykRWzh6L1EgizK+OFWQDPPigLYLECPT3tfjzeU00= 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 1791362342609886.188329057135; Wed, 7 Oct 2026 01:39:02 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xEMve-0003v2-FK; Wed, 07 Oct 2026 04:23:10 -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 <3hY3FagsKChQ7u6u70E5uD208805y.w86Ay6E-xyFy578707E.8B0@flex--namangulati.bounces.google.com>) id 1xEH3H-0000oz-Gg for qemu-devel@nongnu.org; Tue, 06 Oct 2026 22:06:47 -0400 Received: from mail-ua1-x946.google.com ([2607:f8b0:4864:20::946]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3hY3FagsKChQ7u6u70E5uD208805y.w86Ay6E-xyFy578707E.8B0@flex--namangulati.bounces.google.com>) id 1xEH32-0004qb-Fl for qemu-devel@nongnu.org; Tue, 06 Oct 2026 22:06:36 -0400 Received: by mail-ua1-x946.google.com with SMTP id a1e0cc1a2514c-98c6374b678so2775006241.3 for ; Tue, 06 Oct 2026 19:03:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1791338616; x=1791943416; 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=carpSQDzf24ZpvGYVcletgPsKvLeICa/ra3tGkAOE6o=; b=M2wYqHcfsZohZ67FVbi6YRHHUlktmjbduq9DbbBlY65091TvODx4kxQbuxcUoL4+X0 H/JkmTS7Y6OfeydlB5ZInMms1VEztJRM+Wm0pZkSsKEbXbsgqfL99zvKxGCx7ky+cX/m A1BTQyZQmkC9rgHkhofyBJSX2fiQgO0V3LwGIEPtfA6q/AzfBiykPPjQ/zUKTbfP6mRe WiJkNM4H3VLNy5YyCnJi2aUzt8fSC4Xf+GvYdt31A9xWsjUna+f6km2qIIsYhSeYuz+E 2QgkIaD1xGn3I/itQzptA1THgBNCv22GzFle1gJ5eXY54fcfKXTBY8HhnMIK89wzyRxF rAeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1791338616; x=1791943416; 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=carpSQDzf24ZpvGYVcletgPsKvLeICa/ra3tGkAOE6o=; b=y+Ms7OlRSvSqwEJRL1cQgZyQoxv58oWkxr+CShUpwwRKtlSn38km3tQcknPic8w0zM YGIPcSmEEZMv5j1lZLC3xJiU+p6G1nQNvE4C9t2xcXaUbXWbtUCJDZPBi+hUw91DDuHm Baerw19w+QKQMpK157908wJA2TdO5nr+p5N0TNyrXW9T0nBKAuCQRlDzCAJFw5KAyEaY atvPCAYOYBkO76WCrkptzQa2KKiF9MRGqj/jdH/HPyNZwO8dWatmvYNqcwcZmGGfabpq C6xAkrJv5I8kuDsooHAinKiIM5azCISv4O50ZjQFv5lhv9c6nbyXqNvSPxjjoTkB4VGj Kqjg== X-Gm-Message-State: AFq9FYJRRBoplGDd54QTyuJm/908vMbbWXcHvYdssGa8V3s+mF7GRXPq E+J5x2Cvd5YAmzcnQ00EjWFuLgzpvRcE+ChanBAAJY9ZS/eTH4G8rh5OAYQSPzZBEtIw+mc3MEw FJrQyPCvXl/fQceN5RRgkqHIfxajNOXWG0alfdT3F1az6TpOW4+bP/IiF0DOYOE/ytale1PSi9w Cx0hck5WgUNU5qMP5oJiwohHzAaiXg4HbzfZK1lGMK1lRVjqmSc8IgvqoH87WhYg== X-Received: from qtbkl10.prod.google.com ([2002:a05:622a:2c4a:b0:535:6836:909d]) (user=namangulati job=prod-delivery.src-stubby-dispatcher) by 2002:a05:622a:346:b0:535:80b:9900 with SMTP id d75a77b69052e-535755e9271mr9283811cf.44.1791331717281; Tue, 06 Oct 2026 17:08:37 -0700 (PDT) Date: Wed, 7 Oct 2026 00:08:31 +0000 In-Reply-To: <20261007000833.2445641-1-namangulati@google.com> Mime-Version: 1.0 References: <20261007000833.2445641-1-namangulati@google.com> X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: <20261007000833.2445641-3-namangulati@google.com> Subject: [PATCH v3 RESEND 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::946; envelope-from=3hY3FagsKChQ7u6u70E5uD208805y.w86Ay6E-xyFy578707E.8B0@flex--namangulati.bounces.google.com; helo=mail-ua1-x946.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: 1791362343284158500 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 Reviewed-by: Thanos Makatos --- 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 428ab2f069..f851082114 100644 --- a/hw/vfio/pci.c +++ b/hw/vfio/pci.c @@ -1623,6 +1623,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, @@ -1649,6 +1650,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 54ad11a6c8..16efed2fce 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; } @@ -354,7 +359,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; @@ -391,8 +395,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, @@ -404,12 +406,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; /* @@ -454,8 +466,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.56.0.rc1.315.gc6ed9934b7-goog From nobody Sun Oct 25 09:31:20 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=1791333601; cv=none; d=zohomail.com; s=zohoarc; b=Uz4v6KCyYmUiaTs+zj5cF05xWUTVlochxzQ7u83kydgiESZehBDSxwP8IfBfzDgKETukGnF38ZjVOtcJPGwo097GIBHVkIIU5BQCgPqScApsXYblxonXuU3IBVN//6cDAXjP75aw1cfIpElU+xsYhQICGKy0528JTEU4b5vgEpE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791333601; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=QRS73chvJqOCdqEfGNpEpRd63O/QrAFEMU7J5ZoA3Yg=; b=KnwDDODKYhHQy0sA1flqdeBC3kIMVon2R01LmyAr3QKJO2j2qg1drJ0oIG+OCHIARDxxUpDO/senzjlIytU1i6Tnf6EmdA+UCta4ggOfdbUiKrZ9I6UNkYm3uougIbaq9alGSjfl4b47VwI//5BfesI3HTzBp6LNJRegLfPQoHA= 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 1791333601719339.2317843657439; Tue, 6 Oct 2026 17:40:01 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xEFfU-0004Cn-Ht; Tue, 06 Oct 2026 20:38:48 -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 <3ho3FagsKChU8v7v81F6vE319916z.x97Bz7F-yzGz689818F.9C1@flex--namangulati.bounces.google.com>) id 1xEFHm-0001Km-2w for qemu-devel@nongnu.org; Tue, 06 Oct 2026 20:14:00 -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 <3ho3FagsKChU8v7v81F6vE319916z.x97Bz7F-yzGz689818F.9C1@flex--namangulati.bounces.google.com>) id 1xEFH8-0000lG-HD for qemu-devel@nongnu.org; Tue, 06 Oct 2026 20:13:08 -0400 Received: by mail-qt1-x846.google.com with SMTP id d75a77b69052e-5338f659170so55121491cf.0 for ; Tue, 06 Oct 2026 17:08:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1791331719; x=1791936519; 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=QRS73chvJqOCdqEfGNpEpRd63O/QrAFEMU7J5ZoA3Yg=; b=r3nZPHNKni2kFVCtcEDQuEAH6djB6DS5Vh0G3KjKluCKOO/3oxKDp+DjSE/ITLCch/ n7+P9dTXI7QchhsLwuEa/uVV6YtEuEqz5qtALl4jGyjYlSt9gk2YJ04RWLGPSaIOIGev IhlboZHTZrM71FSBsccZcF1TEsEwfJbXshRyy5jwzWbqz97GhAJq2RnJtw7klLkYutQk 2v0ZFBEBBipYZeVaWkzZENbYnABkBnorZ8LcNZg6vKie8p9Y7ZyOHgvKiGeMD3ipHhq9 PYrdvfHd6WVtZ037O0Eu/vGRHQZEtQlJ4PKXAJ2MO8Elv7Si/U3nvjXi1QLkikIJ6IGj dotw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1791331719; x=1791936519; 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=QRS73chvJqOCdqEfGNpEpRd63O/QrAFEMU7J5ZoA3Yg=; b=UzEw1YXjnykCXvMQeXt1uJtEW9VftZjS4gbFKVDz4Gi0i0MRpCGB3Wdk4o1WqWgeJW 6l0LjLGb6ZiTTXPi+MfWKzxQ4DU0IlRcHsJG4L/Lk9RcOy9eVHUwwqpFNBLiQHE4rSZ/ IN6KPk8eXasJsaGaIjOTI2z1Yeccho/LgHWmjrvxVeJFSNkiysDbHVkL9c387mhYd30o sn7JWsKTT50E+PSc6xQhb8l8hBGSSvqJdODTwUB2tkjTI7bOkSGbNWXn3/K8UU06a9tN /XwwxS8EjxDtfGdH1NmsgNf/gPnlM/NNdd3qufwo/Gntjb+bubobiRj1rzrSXnBeDlIh 70iA== X-Gm-Message-State: AFuF++lQLxiKMN5z+L2yYYKgJ1cPMZYIQLdRHv+OPvYttFnlGxH1Q9co 5MwvDfSYXw/mtk0ZDMnGM2TBh3ETmME/28fRcn7dToMUyVwu4e4EXKV0yZllE5KurbHR3t5/XKM iLWlxTvl2a551Nhk20Qn9zQcqD17mDJdTEp2j50jyRKMwxOf8+bAdtmEutfVJWGGYFsxWZKYCx2 vyLdAFH4B8LM8H49ns2Pme18/2B3zNYSYoYSRfgEBT6+RsHToivnHU5uz5lHs7gg== X-Received: from qto20.prod.google.com ([2002:a05:622a:a6d4:b0:533:6ff9:e7b7]) (user=namangulati job=prod-delivery.src-stubby-dispatcher) by 2002:a05:622a:64f:b0:530:f86a:5e30 with SMTP id d75a77b69052e-53575605576mr10432341cf.47.1791331718318; Tue, 06 Oct 2026 17:08:38 -0700 (PDT) Date: Wed, 7 Oct 2026 00:08:32 +0000 In-Reply-To: <20261007000833.2445641-1-namangulati@google.com> Mime-Version: 1.0 References: <20261007000833.2445641-1-namangulati@google.com> X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: <20261007000833.2445641-4-namangulati@google.com> Subject: [PATCH v3 RESEND 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::846; envelope-from=3ho3FagsKChU8v7v81F6vE319916z.x97Bz7F-yzGz689818F.9C1@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 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: 1791333604068158500 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 16efed2fce..d6cd5d4743 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.56.0.rc1.315.gc6ed9934b7-goog From nobody Sun Oct 25 09:31:20 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=1791333459; cv=none; d=zohomail.com; s=zohoarc; b=KPsnLdBKjWPx4xdOVJTLcjnoVYZBJiRcCCY3NlZDQQ/GaB6WRMux5/Fragaw2dLUDPw1gT5pimHSJw4aBWguqVqE2i0/Hw6W/vuV66VFyfVgjndtDJy6UK6rBrklaX4gI2tv5rVvBa1FYt9IaPiPPFrdRLNnC6rw41BFHTsUhiE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1791333459; 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:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=48YWQL5SaxeiqHBx5CeDimJtkbddU1hOzOaZwEJ0A2I=; b=G0RQoKJ+I1t53nkNnkOdYV86Hmu5dw9PFVKIXvp6bDQm/e1zbt1hW6w7CvRSNmDcVMXREY0m96VgvmGTcWuPygW5zkTCsJFZo1lGa+QH66V0WSW5LLz7P/nUASEIww2mxuL/h+WWntQN9EzJoqbq3gJN5RIrgZiqlVjjYhgG01k= 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 1791333459849831.269725375883; Tue, 6 Oct 2026 17:37:39 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xEFbX-0003XS-DA; Tue, 06 Oct 2026 20:35:22 -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 <3h43FagsKChY9w8w92G7wF42AA270.yA8C08G-z0H079A929G.AD2@flex--namangulati.bounces.google.com>) id 1xEFIU-0001N3-5G for qemu-devel@nongnu.org; Tue, 06 Oct 2026 20:14:59 -0400 Received: from mail-qt1-f199.google.com ([209.85.160.199]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from <3h43FagsKChY9w8w92G7wF42AA270.yA8C08G-z0H079A929G.AD2@flex--namangulati.bounces.google.com>) id 1xEFHo-0000lT-SL for qemu-devel@nongnu.org; Tue, 06 Oct 2026 20:13:44 -0400 Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-5352a4bba0dso52637081cf.0 for ; Tue, 06 Oct 2026 17:08:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1791331720; x=1791936520; 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=48YWQL5SaxeiqHBx5CeDimJtkbddU1hOzOaZwEJ0A2I=; b=UHzQDK65GbttkZHhUh1AHUP22Ys15sJDtb9ISBHlYyi/hijdshtSqjsbJ4/DYgJgva S8gFe9mFqlR74UVw2qEEr8A3a2wFsxIoxaXQ3rVod8KQ+I+sOpajd6S8IaVvWd4LE+bD tl+RCtM/UWamqm4AZLlbhaholWR/GJTYY1PSAYJgC62nqzxo1gO99b4yjTR6GboxIMej gQjDN24NFv+NWPjz35SHDtbHSg7grAkX69y6CjHTWtpTjLG4tkPZDD4Fp4er23ZK+zKm I3DzcolP/jgv/e7g3EsvngTioA576qwE+xfDVaCM2UW/ycXkBF1gfnQ9KdhUrqcWCNV7 fiHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1791331720; x=1791936520; 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=48YWQL5SaxeiqHBx5CeDimJtkbddU1hOzOaZwEJ0A2I=; b=gV/Mz6JVph9ofO1G15TRbSL8L3cjZvx99p9WuCVu5H4ALFEt/1xfzvfY51c+ZgVcsg Tf9XNYgKYdM9inc6MHhaNgD+bZKy+kNrn27DDdc2Tm1ACDV4ODn1z8yTWpkiATJ8joLJ Kaf0ArN7G0WFCtQnydp5PBo85fiqKP5up1tr2woTnyXRNqeXxHmlUkg24OUA2TGqIQkZ XHypsAhlf7WvtA8PqrMBUgsWrqFZP/t63Jy0yzTb55ipaEB2a0623I+6+J3sJ9xRFUyM Jk5RR8Q1Fq1FCu4tCTyw1sSkmT6s+hbysduV2G0QKHtDuwISRSYxy78lgmct/WeaTSfo CpIQ== X-Gm-Message-State: AFuF++nAFcS4s2tTzZMnmaO1Bx7DmEUYn7ica7BwYQQcBYwThExWzJCW SdBBkIjjM5NDiioMso7E6CUHlMXRRX1DUp7yMdF5h2PInGVD2IW03yeII1oHOLOu+ZrN5r76TVq WQ+nBn2pqcaIgvJMkv1LYWtmLYogC5Og5222aZWQswQk/i2I5fiQNGqsZajybVzOH7RDc3byUuj XiSyrAvV/lKufUHeSne1nyfcs5DS5UUDrMHMM5hwQvnnBiOhg8ZfyU6o2h/HLaNQ== X-Received: from qttr3.prod.google.com ([2002:ac8:7943:0:b0:535:247e:c776]) (user=namangulati job=prod-delivery.src-stubby-dispatcher) by 2002:a05:622a:4d44:b0:530:430d:87f7 with SMTP id d75a77b69052e-53575592babmr9413421cf.47.1791331719590; Tue, 06 Oct 2026 17:08:39 -0700 (PDT) Date: Wed, 7 Oct 2026 00:08:33 +0000 In-Reply-To: <20261007000833.2445641-1-namangulati@google.com> Mime-Version: 1.0 References: <20261007000833.2445641-1-namangulati@google.com> X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: <20261007000833.2445641-5-namangulati@google.com> Subject: [PATCH v3 RESEND 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=209.85.160.199; envelope-from=3h43FagsKChY9w8w92G7wF42AA270.yA8C08G-z0H079A929G.AD2@flex--namangulati.bounces.google.com; helo=mail-qt1-f199.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_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, 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: 1791333462050158500 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 1a01e748cd..1b5025f205 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, @@ -209,14 +211,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; } @@ -225,10 +230,29 @@ static int vfio_user_device_io_get_region_info(VFIODe= vice *vbasedev, if ((info->flags & VFIO_REGION_INFO_FLAG_CAPS) && (info->cap_offset < sizeof(*info) || info->cap_offset + sizeof(struct vfio_info_cap_header) > info-= >argsz)) { - 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, @@ -518,12 +542,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.56.0.rc1.315.gc6ed9934b7-goog