From nobody Tue Apr 7 14:36:46 2026 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4C5353C73F8 for ; Thu, 12 Mar 2026 15:56:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773331002; cv=none; b=JXhjP84jHvzdPC09Z0sTheG3SKNvP9g64kFnvUi/w19CiwTkI9swN2WZ/s68hYb8OxPPAmutPEXLeRCyW4cT6uHq4dqj46/VndiIl4LEa0XChq8ocAy3S7EhwD7SrUMPitFJhIDT0VSF/1QqeJJYSa7lgjmYS6EDCo75AyoQRaQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773331002; c=relaxed/simple; bh=jq5qTcsaoCOYsl1jW0F01Uz6kAYc6e2RZaLegbuHqqs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cXwgX4vPGesAnmSZ+m/mdBEhp2MtoFtxeAWDVPtrbSiVAbn9h8M+BE668akh4CZz2WqRL9FF/cSjDFEjz1jYQGjHgKXe0yvNP6xN1fnv6JWJfF3CKnN00lI5Rgh6jb9I2NHRV1a24ui8QpMa2n5RSa7JayTWrl3dEG+oWV16wSY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=BJAVaP6J; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="BJAVaP6J" Received: from DESKTOP-0403QTC.corp.microsoft.com (unknown [52.148.138.235]) by linux.microsoft.com (Postfix) with ESMTPSA id 3A03420B6F08; Thu, 12 Mar 2026 08:56:39 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 3A03420B6F08 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1773330999; bh=1aYAnL2eR52QY8qJUJRJndthj5NuHcgN66qFz2M8XyQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BJAVaP6JqME4zdg/8KGPnFuoMGvANyUNsZZrk6r0gqsvpcjm2W7RLn0Qyqi/dSr8y jx4gUCmE8lxvnRIdRtCUF3O0bkLdRVgWwHJ+Dzky9E+96P9zFGLjfZGr3aAgu1nYfq 7KMegAWzVZBs5+qil3PnF7Ct0prsBUhVZjQFcTx0= From: Jacob Pan To: linux-kernel@vger.kernel.org, "iommu@lists.linux.dev" , Jason Gunthorpe , Alex Williamson , Joerg Roedel , David Matlack , Robin Murphy , Nicolin Chen , "Tian, Kevin" , Yi Liu Cc: skhawaja@google.com, pasha.tatashin@soleen.com, Will Deacon , Jacob Pan , Baolu Lu Subject: [PATCH V2 01/11] iommufd: Support a HWPT without an iommu driver for noiommu Date: Thu, 12 Mar 2026 08:56:27 -0700 Message-Id: <20260312155637.376854-2-jacob.pan@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260312155637.376854-1-jacob.pan@linux.microsoft.com> References: <20260312155637.376854-1-jacob.pan@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Jason Gunthorpe Create just a little part of a real iommu driver, enough to slot in under the dev_iommu_ops() and allow iommufd to call domain_alloc_paging_flags() and fail everything else. This allows explicitly creating a HWPT under an IOAS. Signed-off-by: Jason Gunthorpe Signed-off-by: Jacob Pan --- drivers/iommu/iommufd/Makefile | 1 + drivers/iommu/iommufd/hw_pagetable.c | 11 ++- drivers/iommu/iommufd/hwpt_noiommu.c | 91 +++++++++++++++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 2 + 4 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 drivers/iommu/iommufd/hwpt_noiommu.c diff --git a/drivers/iommu/iommufd/Makefile b/drivers/iommu/iommufd/Makefile index 71d692c9a8f4..2b1a020b14a6 100644 --- a/drivers/iommu/iommufd/Makefile +++ b/drivers/iommu/iommufd/Makefile @@ -10,6 +10,7 @@ iommufd-y :=3D \ vfio_compat.o \ viommu.o =20 +iommufd-$(CONFIG_VFIO_NOIOMMU) +=3D hwpt_noiommu.o iommufd-$(CONFIG_IOMMUFD_TEST) +=3D selftest.o =20 obj-$(CONFIG_IOMMUFD) +=3D iommufd.o diff --git a/drivers/iommu/iommufd/hw_pagetable.c b/drivers/iommu/iommufd/h= w_pagetable.c index fe789c2dc0c9..37316d77277d 100644 --- a/drivers/iommu/iommufd/hw_pagetable.c +++ b/drivers/iommu/iommufd/hw_pagetable.c @@ -8,6 +8,13 @@ #include "../iommu-priv.h" #include "iommufd_private.h" =20 +static const struct iommu_ops *get_iommu_ops(struct iommufd_device *idev) +{ + if (IS_ENABLED(CONFIG_VFIO_NOIOMMU) && !idev->igroup->group) + return &iommufd_noiommu_ops; + return dev_iommu_ops(idev->dev); +} + static void __iommufd_hwpt_destroy(struct iommufd_hw_pagetable *hwpt) { if (hwpt->domain) @@ -114,7 +121,7 @@ iommufd_hwpt_paging_alloc(struct iommufd_ctx *ictx, str= uct iommufd_ioas *ioas, IOMMU_HWPT_ALLOC_DIRTY_TRACKING | IOMMU_HWPT_FAULT_ID_VALID | IOMMU_HWPT_ALLOC_PASID; - const struct iommu_ops *ops =3D dev_iommu_ops(idev->dev); + const struct iommu_ops *ops =3D get_iommu_ops(idev); struct iommufd_hwpt_paging *hwpt_paging; struct iommufd_hw_pagetable *hwpt; int rc; @@ -229,7 +236,7 @@ iommufd_hwpt_nested_alloc(struct iommufd_ctx *ictx, struct iommufd_device *idev, u32 flags, const struct iommu_user_data *user_data) { - const struct iommu_ops *ops =3D dev_iommu_ops(idev->dev); + const struct iommu_ops *ops =3D get_iommu_ops(idev); struct iommufd_hwpt_nested *hwpt_nested; struct iommufd_hw_pagetable *hwpt; int rc; diff --git a/drivers/iommu/iommufd/hwpt_noiommu.c b/drivers/iommu/iommufd/h= wpt_noiommu.c new file mode 100644 index 000000000000..0aa99f581ca3 --- /dev/null +++ b/drivers/iommu/iommufd/hwpt_noiommu.c @@ -0,0 +1,91 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES + */ +#include +#include +#include "iommufd_private.h" + +static const struct iommu_domain_ops noiommu_amdv1_ops; + +struct noiommu_domain { + union { + struct iommu_domain domain; + struct pt_iommu_amdv1 amdv1; + }; + spinlock_t lock; +}; +PT_IOMMU_CHECK_DOMAIN(struct noiommu_domain, amdv1.iommu, domain); + +static void noiommu_change_top(struct pt_iommu *iommu_table, + phys_addr_t top_paddr, unsigned int top_level) +{ +} + +static spinlock_t *noiommu_get_top_lock(struct pt_iommu *iommupt) +{ + struct noiommu_domain *domain =3D + container_of(iommupt, struct noiommu_domain, amdv1.iommu); + + return &domain->lock; +} + +static const struct pt_iommu_driver_ops noiommu_driver_ops =3D { + .get_top_lock =3D noiommu_get_top_lock, + .change_top =3D noiommu_change_top, +}; + +static struct iommu_domain * +noiommu_alloc_paging_flags(struct device *dev, u32 flags, + const struct iommu_user_data *user_data) +{ + struct pt_iommu_amdv1_cfg cfg =3D {}; + struct noiommu_domain *dom; + int rc; + + if (flags || user_data) + return ERR_PTR(-EOPNOTSUPP); + + cfg.common.hw_max_vasz_lg2 =3D 64; + cfg.common.hw_max_oasz_lg2 =3D 52; + cfg.starting_level =3D 2; + cfg.common.features =3D + (BIT(PT_FEAT_DYNAMIC_TOP) | BIT(PT_FEAT_AMDV1_ENCRYPT_TABLES) | + BIT(PT_FEAT_AMDV1_FORCE_COHERENCE)); + + dom =3D kzalloc(sizeof(*dom), GFP_KERNEL); + if (!dom) + return ERR_PTR(-ENOMEM); + + spin_lock_init(&dom->lock); + dom->amdv1.iommu.nid =3D NUMA_NO_NODE; + dom->amdv1.iommu.driver_ops =3D &noiommu_driver_ops; + dom->domain.ops =3D &noiommu_amdv1_ops; + + /* Use mock page table which is based on AMDV1 */ + rc =3D pt_iommu_amdv1_init(&dom->amdv1, &cfg, GFP_KERNEL); + if (rc) { + kfree(dom); + return ERR_PTR(rc); + } + + return &dom->domain; +} + +static void noiommu_domain_free(struct iommu_domain *iommu_domain) +{ + struct noiommu_domain *domain =3D + container_of(iommu_domain, struct noiommu_domain, domain); + + pt_iommu_deinit(&domain->amdv1.iommu); + kfree(domain); +} + +static const struct iommu_domain_ops noiommu_amdv1_ops =3D { + IOMMU_PT_DOMAIN_OPS(amdv1), + .free =3D noiommu_domain_free, +}; + +struct iommu_ops iommufd_noiommu_ops =3D { + .domain_alloc_paging_flags =3D noiommu_alloc_paging_flags, +}; diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommuf= d/iommufd_private.h index 6ac1965199e9..9c18c5eb1899 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -464,6 +464,8 @@ static inline void iommufd_hw_pagetable_put(struct iomm= ufd_ctx *ictx, refcount_dec(&hwpt->obj.users); } =20 +extern struct iommu_ops iommufd_noiommu_ops; + struct iommufd_attach; =20 struct iommufd_group { --=20 2.34.1 From nobody Tue Apr 7 14:36:46 2026 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EF6A73CAE88 for ; Thu, 12 Mar 2026 15:56:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773331002; cv=none; b=UsFHHBOG5w1ZG+EFNhw4YR1HPbLyENUabtV2rbt1Q/VkJUIK5mqKPd7pco2HtSPQZQR03iSD0yP7BDx0gxwhUbf+zOGQcskec8GYZKwsv89cZ4a6WBRTVRPyH/JM4yJUVw8hvUxCnYW3mzciV96ZmXiGYMwkCv1WSLtJQvnuncI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773331002; c=relaxed/simple; bh=2pSNNjCMJCzi4E9NiX/bZFyADwrRK7bee1k67bm0SZQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=R5y2Gh+dEFfin+RHmX0qPwk+ll3E7ea38A5o3MKQE/P/k2/wgaQaSmKsAsa5+ie/Yqm2v/Q+8y999lueJuZ6jdgP8G3aPhUFWMR/da1dHajCLqgjQe/ModqUu4GdpfeG7KLzbWHWMJrIxCmC3MirPjyIPV9Y/GHE+P+v2VwFes8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=BLYsY5PL; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="BLYsY5PL" Received: from DESKTOP-0403QTC.corp.microsoft.com (unknown [52.148.138.235]) by linux.microsoft.com (Postfix) with ESMTPSA id 644D220B6F0C; Thu, 12 Mar 2026 08:56:40 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 644D220B6F0C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1773331000; bh=4uSNyWj8lWw77lOFEo+XTvKl1no/K1YZKJTwbDlGzmY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BLYsY5PLCEsBtVOqNoasSLZbDu0kmw3Y8j710uBlL2a/qbtBPT9RN72pFABsF4JSh qjJQOJqberD9TKru/li5qacQU5ZxzUGsQKi0vPd9Si3aDfFi3dEOfVTEfpjozJDRSH 2CMb/NvS0UmCtXtpPhwI27R5xh1G1SRZQZ2LP5J4= From: Jacob Pan To: linux-kernel@vger.kernel.org, "iommu@lists.linux.dev" , Jason Gunthorpe , Alex Williamson , Joerg Roedel , David Matlack , Robin Murphy , Nicolin Chen , "Tian, Kevin" , Yi Liu Cc: skhawaja@google.com, pasha.tatashin@soleen.com, Will Deacon , Jacob Pan , Baolu Lu Subject: [PATCH V2 02/11] iommufd: Move igroup allocation to a function Date: Thu, 12 Mar 2026 08:56:28 -0700 Message-Id: <20260312155637.376854-3-jacob.pan@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260312155637.376854-1-jacob.pan@linux.microsoft.com> References: <20260312155637.376854-1-jacob.pan@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Jason Gunthorpe So it can be reused in the next patch which allows binding to noiommu device. Signed-off-by: Jason Gunthorpe Signed-off-by: Jacob Pan Reviewed-by: Samiullah Khawaja --- drivers/iommu/iommufd/device.c | 48 +++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 344d620cdecc..54d73016468f 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -30,8 +30,9 @@ static void iommufd_group_release(struct kref *kref) =20 WARN_ON(!xa_empty(&igroup->pasid_attach)); =20 - xa_cmpxchg(&igroup->ictx->groups, iommu_group_id(igroup->group), igroup, - NULL, GFP_KERNEL); + if (igroup->group) + xa_cmpxchg(&igroup->ictx->groups, iommu_group_id(igroup->group), + igroup, NULL, GFP_KERNEL); iommu_group_put(igroup->group); mutex_destroy(&igroup->lock); kfree(igroup); @@ -56,6 +57,30 @@ static bool iommufd_group_try_get(struct iommufd_group *= igroup, return kref_get_unless_zero(&igroup->ref); } =20 +static struct iommufd_group *iommufd_alloc_group(struct iommufd_ctx *ictx, + struct iommu_group *group) +{ + struct iommufd_group *new_igroup; + + new_igroup =3D kzalloc(sizeof(*new_igroup), GFP_KERNEL); + if (!new_igroup) + return ERR_PTR(-ENOMEM); + + kref_init(&new_igroup->ref); + mutex_init(&new_igroup->lock); + xa_init(&new_igroup->pasid_attach); + new_igroup->sw_msi_start =3D PHYS_ADDR_MAX; + /* group reference moves into new_igroup */ + new_igroup->group =3D group; + + /* + * The ictx is not additionally refcounted here becase all objects using + * an igroup must put it before their destroy completes. + */ + new_igroup->ictx =3D ictx; + return new_igroup; +} + /* * iommufd needs to store some more data for each iommu_group, we keep a * parallel xarray indexed by iommu_group id to hold this instead of putti= ng it @@ -87,25 +112,12 @@ static struct iommufd_group *iommufd_get_group(struct = iommufd_ctx *ictx, } xa_unlock(&ictx->groups); =20 - new_igroup =3D kzalloc_obj(*new_igroup); - if (!new_igroup) { + new_igroup =3D iommufd_alloc_group(ictx, group); + if (IS_ERR(new_igroup)) { iommu_group_put(group); - return ERR_PTR(-ENOMEM); + return new_igroup; } =20 - kref_init(&new_igroup->ref); - mutex_init(&new_igroup->lock); - xa_init(&new_igroup->pasid_attach); - new_igroup->sw_msi_start =3D PHYS_ADDR_MAX; - /* group reference moves into new_igroup */ - new_igroup->group =3D group; - - /* - * The ictx is not additionally refcounted here becase all objects using - * an igroup must put it before their destroy completes. - */ - new_igroup->ictx =3D ictx; - /* * We dropped the lock so igroup is invalid. NULL is a safe and likely * value to assume for the xa_cmpxchg algorithm. --=20 2.34.1 From nobody Tue Apr 7 14:36:46 2026 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 57D123F0AB1 for ; Thu, 12 Mar 2026 15:56:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773331008; cv=none; b=GsdUoZTajrXAkHQCgLDvXbFchSfGLmcrygNXneaVVwo4sFi9x/PWEoVDNg3euOQ2V3ZW6BEoZstbk6tTs3tMQ1rtGo65Roqsq4tVFnlzhtrB8eQGVDGj3wV+OFppq3Q08u9sWRl/onkOBx/I8OtD3Wgize5PFSH1yqaf92U0x50= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773331008; c=relaxed/simple; bh=/6zjW23pvLpPHPAOXtukezbhsq5kWKQr6msf/ZPV7UE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tHzS4h2rFFcMfSQ5OBiMpT69C9hsH3d6AIOAQpOyLLUzoTfutDVtYHP1CHg2EXylEZ5Vkn2LAVIEExpPV+e6/VlySqon0WJLk+Lz5I0+xu/Eu1fLPp2MOb/WAEn0eZfD+iE4+wu4K6zCKNfY75Z5YxegY+7hZGcjspXYFVveWsM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=j/JEJtGA; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="j/JEJtGA" Received: from DESKTOP-0403QTC.corp.microsoft.com (unknown [52.148.138.235]) by linux.microsoft.com (Postfix) with ESMTPSA id 254F420B6F12; Thu, 12 Mar 2026 08:56:41 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 254F420B6F12 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1773331001; bh=tz2IbcvQXBLS4zfZdPH/7krrGF/PbgxjGvCMudQfOGc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=j/JEJtGAhV3Y+rlBFbFBTmj7KENyrSIbrZMGCWAej7D6GKTHxS8kdfR8fLluPA/me NwJ685no08HraNRiKAC3kaABnsT+1uR8z6pTAGZcgLq3Xhda2uy2hn6wEh2rTdO1Sd /vO1n4ko/dNAe5GG8yF0R+dPTI53OLs8CxFxOwbk= From: Jacob Pan To: linux-kernel@vger.kernel.org, "iommu@lists.linux.dev" , Jason Gunthorpe , Alex Williamson , Joerg Roedel , David Matlack , Robin Murphy , Nicolin Chen , "Tian, Kevin" , Yi Liu Cc: skhawaja@google.com, pasha.tatashin@soleen.com, Will Deacon , Jacob Pan , Baolu Lu Subject: [PATCH V2 03/11] iommufd: Allow binding to a noiommu device Date: Thu, 12 Mar 2026 08:56:29 -0700 Message-Id: <20260312155637.376854-4-jacob.pan@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260312155637.376854-1-jacob.pan@linux.microsoft.com> References: <20260312155637.376854-1-jacob.pan@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Jason Gunthorpe Allow iommufd to bind devices without an IOMMU (noiommu mode) by creating a dummy IOMMU group for such devices and skipping hwpt operations. This enables noiommu devices to operate through the same iommufd API as IOM= MU- capable devices. Signed-off-by: Jason Gunthorpe Signed-off-by: Jacob Pan --- drivers/iommu/iommufd/device.c | 113 ++++++++++++++++++++++----------- 1 file changed, 76 insertions(+), 37 deletions(-) diff --git a/drivers/iommu/iommufd/device.c b/drivers/iommu/iommufd/device.c index 54d73016468f..c38d3efa3d6f 100644 --- a/drivers/iommu/iommufd/device.c +++ b/drivers/iommu/iommufd/device.c @@ -23,6 +23,11 @@ struct iommufd_attach { struct xarray device_array; }; =20 +static bool is_vfio_noiommu(struct iommufd_device *idev) +{ + return !device_iommu_mapped(idev->dev) || !idev->dev->iommu; +} + static void iommufd_group_release(struct kref *kref) { struct iommufd_group *igroup =3D @@ -205,32 +210,17 @@ void iommufd_device_destroy(struct iommufd_object *ob= j) struct iommufd_device *idev =3D container_of(obj, struct iommufd_device, obj); =20 - iommu_device_release_dma_owner(idev->dev); + if (!is_vfio_noiommu(idev)) + iommu_device_release_dma_owner(idev->dev); iommufd_put_group(idev->igroup); if (!iommufd_selftest_is_mock_dev(idev->dev)) iommufd_ctx_put(idev->ictx); } =20 -/** - * iommufd_device_bind - Bind a physical device to an iommu fd - * @ictx: iommufd file descriptor - * @dev: Pointer to a physical device struct - * @id: Output ID number to return to userspace for this device - * - * A successful bind establishes an ownership over the device and returns - * struct iommufd_device pointer, otherwise returns error pointer. - * - * A driver using this API must set driver_managed_dma and must not touch - * the device until this routine succeeds and establishes ownership. - * - * Binding a PCI device places the entire RID under iommufd control. - * - * The caller must undo this with iommufd_device_unbind() - */ -struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, - struct device *dev, u32 *id) +static int iommufd_bind_iommu(struct iommufd_device *idev) { - struct iommufd_device *idev; + struct iommufd_ctx *ictx =3D idev->ictx; + struct device *dev =3D idev->dev; struct iommufd_group *igroup; int rc; =20 @@ -239,11 +229,11 @@ struct iommufd_device *iommufd_device_bind(struct iom= mufd_ctx *ictx, * to restore cache coherency. */ if (!device_iommu_capable(dev, IOMMU_CAP_CACHE_COHERENCY)) - return ERR_PTR(-EINVAL); + return -EINVAL; =20 - igroup =3D iommufd_get_group(ictx, dev); + igroup =3D iommufd_get_group(idev->ictx, dev); if (IS_ERR(igroup)) - return ERR_CAST(igroup); + return PTR_ERR(igroup); =20 /* * For historical compat with VFIO the insecure interrupt path is @@ -269,21 +259,66 @@ struct iommufd_device *iommufd_device_bind(struct iom= mufd_ctx *ictx, if (rc) goto out_group_put; =20 + /* igroup refcount moves into iommufd_device */ + idev->igroup =3D igroup; + return 0; + +out_group_put: + iommufd_put_group(igroup); + return rc; +} + +/** + * iommufd_device_bind - Bind a physical device to an iommu fd + * @ictx: iommufd file descriptor + * @dev: Pointer to a physical device struct + * @id: Output ID number to return to userspace for this device + * + * A successful bind establishes an ownership over the device and returns + * struct iommufd_device pointer, otherwise returns error pointer. + * + * A driver using this API must set driver_managed_dma and must not touch + * the device until this routine succeeds and establishes ownership. + * + * Binding a PCI device places the entire RID under iommufd control. + * + * The caller must undo this with iommufd_device_unbind() + */ +struct iommufd_device *iommufd_device_bind(struct iommufd_ctx *ictx, + struct device *dev, u32 *id) +{ + struct iommufd_device *idev; + int rc; + idev =3D iommufd_object_alloc(ictx, idev, IOMMUFD_OBJ_DEVICE); - if (IS_ERR(idev)) { - rc =3D PTR_ERR(idev); - goto out_release_owner; - } + if (IS_ERR(idev)) + return idev; idev->ictx =3D ictx; - if (!iommufd_selftest_is_mock_dev(dev)) - iommufd_ctx_get(ictx); idev->dev =3D dev; idev->enforce_cache_coherency =3D device_iommu_capable(dev, IOMMU_CAP_ENFORCE_CACHE_COHERENCY); + + if (!is_vfio_noiommu(idev)) { + rc =3D iommufd_bind_iommu(idev); + if (rc) + return ERR_PTR(rc); + } else { + struct iommufd_group *igroup; + + /* + * Create a dummy igroup, lots of stuff expects ths igroup to be + * present, but a NULL igroup->group is OK + */ + igroup =3D iommufd_alloc_group(ictx, NULL); + if (IS_ERR(igroup)) + return ERR_CAST(igroup); + idev->igroup =3D igroup; + } + + if (!iommufd_selftest_is_mock_dev(dev)) + iommufd_ctx_get(ictx); /* The calling driver is a user until iommufd_device_unbind() */ refcount_inc(&idev->obj.users); - /* igroup refcount moves into iommufd_device */ - idev->igroup =3D igroup; =20 /* * If the caller fails after this success it must call @@ -295,11 +330,6 @@ struct iommufd_device *iommufd_device_bind(struct iomm= ufd_ctx *ictx, *id =3D idev->obj.id; return idev; =20 -out_release_owner: - iommu_device_release_dma_owner(dev); -out_group_put: - iommufd_put_group(igroup); - return ERR_PTR(rc); } EXPORT_SYMBOL_NS_GPL(iommufd_device_bind, "IOMMUFD"); =20 @@ -513,6 +543,9 @@ static int iommufd_hwpt_attach_device(struct iommufd_hw= _pagetable *hwpt, struct iommufd_attach_handle *handle; int rc; =20 + if (is_vfio_noiommu(idev)) + return 0; + if (!iommufd_hwpt_compatible_device(hwpt, idev)) return -EINVAL; =20 @@ -560,6 +593,9 @@ static void iommufd_hwpt_detach_device(struct iommufd_h= w_pagetable *hwpt, { struct iommufd_attach_handle *handle; =20 + if (is_vfio_noiommu(idev)) + return; + handle =3D iommufd_device_get_attach_handle(idev, pasid); if (pasid =3D=3D IOMMU_NO_PASID) iommu_detach_group_handle(hwpt->domain, idev->igroup->group); @@ -578,6 +614,9 @@ static int iommufd_hwpt_replace_device(struct iommufd_d= evice *idev, struct iommufd_attach_handle *handle, *old_handle; int rc; =20 + if (is_vfio_noiommu(idev)) + return 0; + if (!iommufd_hwpt_compatible_device(hwpt, idev)) return -EINVAL; =20 @@ -653,7 +692,7 @@ int iommufd_hw_pagetable_attach(struct iommufd_hw_paget= able *hwpt, goto err_release_devid; } =20 - if (attach_resv) { + if (attach_resv && !is_vfio_noiommu(idev)) { rc =3D iommufd_device_attach_reserved_iova(idev, hwpt_paging); if (rc) goto err_release_devid; --=20 2.34.1 From nobody Tue Apr 7 14:36:46 2026 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C99F33F2105 for ; Thu, 12 Mar 2026 15:56:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773331011; cv=none; b=F61npLjbe11rdEbX1JKLRHca6/dnhEiJq57TTScoaUPX9EogIoK4t0xh4K23biyfRbsuN8GzgtvV2PYJWjEkMInZ4bIqhaZHe9HUzmPvVkNMh8jn4WyS3gQdRp1anPX1/lk7S8GfX2wQjpqW7fZqx98Z2EAXkVhGs8Qb4CCqodw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773331011; c=relaxed/simple; bh=J1MjUooEohoENhIL8QUkXStO7rua38fh1ZsGTa4WoVQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=p55hGSjayynePFM/c9ropY9ifnOaVFJ6bR1Fk8HiJ9Wrlf0fy2khN3afX+S+Kj3YrMFjM3GQ87TRgXXpC71uGXPZoRcmsq8PFLD77qwmQq4tg5x/J6UjX+Q9Zsa2vfc1vzIn6Gxck8ZjxbnNbFV2rlFx+UameZfmPH0GRFEAMRg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=LxZc7YRM; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="LxZc7YRM" Received: from DESKTOP-0403QTC.corp.microsoft.com (unknown [52.148.138.235]) by linux.microsoft.com (Postfix) with ESMTPSA id 77A7620B6F1F; Thu, 12 Mar 2026 08:56:45 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 77A7620B6F1F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1773331005; bh=DEeRiFjjUPp8FN5qUPUbZMkRcjx8KXMnJWP7mQRB9V4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LxZc7YRMHhTYnsWjGvymIUb36sBiN3VvevHcV/A3GtyzLP2ZgqSLBeATcCm/Dn/Nt ZLhRQAgTM07tnIfFvY3vKbb2q8b+YBcx19+0NfKCDSqdYHPqJy7C+odLRpNZqMN6z2 SKdOIWU66NXkhwC8QPR9uIbcB5ogHR6Pr10cTlt8= From: Jacob Pan To: linux-kernel@vger.kernel.org, "iommu@lists.linux.dev" , Jason Gunthorpe , Alex Williamson , Joerg Roedel , David Matlack , Robin Murphy , Nicolin Chen , "Tian, Kevin" , Yi Liu Cc: skhawaja@google.com, pasha.tatashin@soleen.com, Will Deacon , Jacob Pan , Baolu Lu Subject: [PATCH V2 04/11] iommufd: Add an ioctl IOMMU_IOAS_GET_PA to query PA from IOVA Date: Thu, 12 Mar 2026 08:56:30 -0700 Message-Id: <20260312155637.376854-5-jacob.pan@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260312155637.376854-1-jacob.pan@linux.microsoft.com> References: <20260312155637.376854-1-jacob.pan@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To support no-IOMMU mode where userspace drivers perform unsafe DMA using physical addresses, introduce a new API to retrieve the physical address of a user-allocated DMA buffer that has been mapped to an IOVA via IOAS. The mapping is backed by mock I/O page tables maintained by generic IOMMUPT framework. Suggested-by: Jason Gunthorpe Signed-off-by: Jacob Pan Signed-off-by: Jason Gunthorpe --- v2: - Scan the contiguous physical-address span beyond the first page and re= turn its length. --- drivers/iommu/iommufd/io_pagetable.c | 60 +++++++++++++++++++++++++ drivers/iommu/iommufd/ioas.c | 28 ++++++++++++ drivers/iommu/iommufd/iommufd_private.h | 3 ++ drivers/iommu/iommufd/main.c | 3 ++ include/uapi/linux/iommufd.h | 25 +++++++++++ 5 files changed, 119 insertions(+) diff --git a/drivers/iommu/iommufd/io_pagetable.c b/drivers/iommu/iommufd/i= o_pagetable.c index ee003bb2f647..5372fac6077b 100644 --- a/drivers/iommu/iommufd/io_pagetable.c +++ b/drivers/iommu/iommufd/io_pagetable.c @@ -849,6 +849,66 @@ int iopt_unmap_iova(struct io_pagetable *iopt, unsigne= d long iova, return iopt_unmap_iova_range(iopt, iova, iova_last, unmapped); } =20 +int iopt_get_phys(struct io_pagetable *iopt, unsigned long iova, u64 *padd= r, + u64 *length) +{ + struct iopt_area *area; + u64 tmp_length =3D 0; + u64 tmp_paddr =3D 0; + int rc =3D 0; + + if (!IS_ENABLED(CONFIG_VFIO_NOIOMMU)) + return -EOPNOTSUPP; + + down_read(&iopt->iova_rwsem); + area =3D iopt_area_iter_first(iopt, iova, iova); + if (!area || !area->pages) { + rc =3D -ENOENT; + goto unlock_exit; + } + + if (!area->storage_domain || + area->storage_domain->owner !=3D &iommufd_noiommu_ops) { + rc =3D -EOPNOTSUPP; + goto unlock_exit; + } + + *paddr =3D iommu_iova_to_phys(area->storage_domain, iova); + if (!*paddr) { + rc =3D -EINVAL; + goto unlock_exit; + } + + tmp_length =3D PAGE_SIZE; + tmp_paddr =3D *paddr; + /* + * Scan the domain for the contiguous physical address length so that + * userspace search can be optimized for fewer ioctls. + */ + while (iova < iopt_area_last_iova(area)) { + unsigned long next_iova; + u64 next_paddr; + + if (check_add_overflow(iova, PAGE_SIZE, &next_iova)) + break; + + next_paddr =3D iommu_iova_to_phys(area->storage_domain, next_iova); + + if (!next_paddr || next_paddr !=3D tmp_paddr + PAGE_SIZE) + break; + + iova =3D next_iova; + tmp_paddr +=3D PAGE_SIZE; + tmp_length +=3D PAGE_SIZE; + } + *length =3D tmp_length; + +unlock_exit: + up_read(&iopt->iova_rwsem); + + return rc; +} + int iopt_unmap_all(struct io_pagetable *iopt, unsigned long *unmapped) { /* If the IOVAs are empty then unmap all succeeds */ diff --git a/drivers/iommu/iommufd/ioas.c b/drivers/iommu/iommufd/ioas.c index fed06c2b728e..a724f380a465 100644 --- a/drivers/iommu/iommufd/ioas.c +++ b/drivers/iommu/iommufd/ioas.c @@ -375,6 +375,34 @@ int iommufd_ioas_unmap(struct iommufd_ucmd *ucmd) return rc; } =20 +int iommufd_ioas_get_pa(struct iommufd_ucmd *ucmd) +{ + struct iommu_ioas_get_pa *cmd =3D ucmd->cmd; + struct iommufd_ioas *ioas; + int rc; + + if (cmd->flags || cmd->__reserved) + return -EOPNOTSUPP; + + if (!cmd->iova || cmd->iova >=3D ULONG_MAX) + return -EINVAL; + + ioas =3D iommufd_get_ioas(ucmd->ictx, cmd->ioas_id); + if (IS_ERR(ioas)) + return PTR_ERR(ioas); + + rc =3D iopt_get_phys(&ioas->iopt, cmd->iova, &cmd->out_phys, + &cmd->out_length); + if (rc) + goto out_put; + + rc =3D iommufd_ucmd_respond(ucmd, sizeof(*cmd)); +out_put: + iommufd_put_object(ucmd->ictx, &ioas->obj); + + return rc; +} + static void iommufd_release_all_iova_rwsem(struct iommufd_ctx *ictx, struct xarray *ioas_list) { diff --git a/drivers/iommu/iommufd/iommufd_private.h b/drivers/iommu/iommuf= d/iommufd_private.h index 9c18c5eb1899..3302c6a1f99e 100644 --- a/drivers/iommu/iommufd/iommufd_private.h +++ b/drivers/iommu/iommufd/iommufd_private.h @@ -118,6 +118,8 @@ int iopt_map_pages(struct io_pagetable *iopt, struct li= st_head *pages_list, int iopt_unmap_iova(struct io_pagetable *iopt, unsigned long iova, unsigned long length, unsigned long *unmapped); int iopt_unmap_all(struct io_pagetable *iopt, unsigned long *unmapped); +int iopt_get_phys(struct io_pagetable *iopt, unsigned long iova, u64 *padd= r, + u64 *length); =20 int iopt_read_and_clear_dirty_data(struct io_pagetable *iopt, struct iommu_domain *domain, @@ -346,6 +348,7 @@ int iommufd_ioas_map_file(struct iommufd_ucmd *ucmd); int iommufd_ioas_change_process(struct iommufd_ucmd *ucmd); int iommufd_ioas_copy(struct iommufd_ucmd *ucmd); int iommufd_ioas_unmap(struct iommufd_ucmd *ucmd); +int iommufd_ioas_get_pa(struct iommufd_ucmd *ucmd); int iommufd_ioas_option(struct iommufd_ucmd *ucmd); int iommufd_option_rlimit_mode(struct iommu_option *cmd, struct iommufd_ctx *ictx); diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index 8c6d43601afb..ebae01ed947d 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -432,6 +432,7 @@ union ucmd_buffer { struct iommu_veventq_alloc veventq; struct iommu_vfio_ioas vfio_ioas; struct iommu_viommu_alloc viommu; + struct iommu_ioas_get_pa get_pa; #ifdef CONFIG_IOMMUFD_TEST struct iommu_test_cmd test; #endif @@ -484,6 +485,8 @@ static const struct iommufd_ioctl_op iommufd_ioctl_ops[= ] =3D { struct iommu_ioas_map_file, iova), IOCTL_OP(IOMMU_IOAS_UNMAP, iommufd_ioas_unmap, struct iommu_ioas_unmap, length), + IOCTL_OP(IOMMU_IOAS_GET_PA, iommufd_ioas_get_pa, struct iommu_ioas_get_pa, + out_phys), IOCTL_OP(IOMMU_OPTION, iommufd_option, struct iommu_option, val64), IOCTL_OP(IOMMU_VDEVICE_ALLOC, iommufd_vdevice_alloc_ioctl, struct iommu_vdevice_alloc, virt_id), diff --git a/include/uapi/linux/iommufd.h b/include/uapi/linux/iommufd.h index 1dafbc552d37..9afe0a1b11a0 100644 --- a/include/uapi/linux/iommufd.h +++ b/include/uapi/linux/iommufd.h @@ -57,6 +57,7 @@ enum { IOMMUFD_CMD_IOAS_CHANGE_PROCESS =3D 0x92, IOMMUFD_CMD_VEVENTQ_ALLOC =3D 0x93, IOMMUFD_CMD_HW_QUEUE_ALLOC =3D 0x94, + IOMMUFD_CMD_IOAS_GET_PA =3D 0x95, }; =20 /** @@ -219,6 +220,30 @@ struct iommu_ioas_map { }; #define IOMMU_IOAS_MAP _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_MAP) =20 +/** + * struct iommu_ioas_get_pa - ioctl(IOMMU_IOAS_GET_PA) + * @size: sizeof(struct iommu_ioas_get_pa) + * @flags: Reserved, must be 0 for now + * @ioas_id: IOAS ID to query IOVA to PA mapping from + * @__reserved: Must be 0 + * @iova: IOVA to query + * @out_length: Number of bytes contiguous physical address starting from = phys + * @out_phys: Output physical address the IOVA maps to + * + * Query the physical address backing an IOVA range. The entire range must= be + * mapped already. For noiommu devices doing unsafe DMA only. + */ +struct iommu_ioas_get_pa { + __u32 size; + __u32 flags; + __u32 ioas_id; + __u32 __reserved; + __aligned_u64 iova; + __aligned_u64 out_length; + __aligned_u64 out_phys; +}; +#define IOMMU_IOAS_GET_PA _IO(IOMMUFD_TYPE, IOMMUFD_CMD_IOAS_GET_PA) + /** * struct iommu_ioas_map_file - ioctl(IOMMU_IOAS_MAP_FILE) * @size: sizeof(struct iommu_ioas_map_file) --=20 2.34.1 From nobody Tue Apr 7 14:36:46 2026 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9F0313F2112 for ; Thu, 12 Mar 2026 15:56:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773331011; cv=none; b=Mtv840essDAbDmZ8xwGDrMGtfKnUtHlr42n6wd/cNMfZoAkkhtnpHJUTiwJBBzL97il3OXiCCA9Tx1ZP9p6QxadsPRZJRN1tg6P4ftdtRxyF8DXtXm2PClwmrpGCjxdYRKzEB/VDJ0ozqes9snQb2N4ptr5raJjOTHITeV061E0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773331011; c=relaxed/simple; bh=xQZojr3GFxPw6/kHY8lEwIocu5M2D6Kr99QXLjDyrug=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=jxXELYJuSC766RwJmYLR5vn1OTOV6V/IXKvAxxXq9oL7BarOsf0FtzbYf7N8ZD8g01nJJAa4xgrXX8lbH5bmnIEk33lO48Ykjay3SZZi6w/7+g389dCmMWRM7Du1IPcQ/71pqRdI5bkIiVmBHbQ2C/0MC+JaOkI4LUGfeJIUeDU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=dz/42FJ1; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="dz/42FJ1" Received: from DESKTOP-0403QTC.corp.microsoft.com (unknown [52.148.138.235]) by linux.microsoft.com (Postfix) with ESMTPSA id F0DC120B6F01; Thu, 12 Mar 2026 08:56:49 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com F0DC120B6F01 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1773331010; bh=FcyuZJLhmRk9Mvmk2JcfREx8/0r88nbmt1CfI0Gl/EM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dz/42FJ1K9X0Svi3dlsabPIcLoeFYNVXOsntIqHO3HAGki6cy5/uzxxgnzzCTmWMQ VmRd6cxD7Uq+2cRWxCot0ywpFEocd3U6GGLlhHFL/X3L4RmeCBTKgyv8UUuNYU61Qq clnetLuzy0RaSwsREu0KwXvY8wVPjNo/5KW6CrY0= From: Jacob Pan To: linux-kernel@vger.kernel.org, "iommu@lists.linux.dev" , Jason Gunthorpe , Alex Williamson , Joerg Roedel , David Matlack , Robin Murphy , Nicolin Chen , "Tian, Kevin" , Yi Liu Cc: skhawaja@google.com, pasha.tatashin@soleen.com, Will Deacon , Jacob Pan , Baolu Lu Subject: [PATCH V2 05/11] vfio: Allow null group for noiommu without containers Date: Thu, 12 Mar 2026 08:56:31 -0700 Message-Id: <20260312155637.376854-6-jacob.pan@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260312155637.376854-1-jacob.pan@linux.microsoft.com> References: <20260312155637.376854-1-jacob.pan@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In case of noiommu mode is enabled for VFIO cdev without VFIO container nor IOMMUFD provided compatibility container, there is no need to create a dummy group. Update the group operations to tolerate null group pointer. Signed-off-by: Jacob Pan --- drivers/vfio/group.c | 14 ++++++++++++++ drivers/vfio/vfio.h | 17 +++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/drivers/vfio/group.c b/drivers/vfio/group.c index 4f15016d2a5f..98f2a4f2ebff 100644 --- a/drivers/vfio/group.c +++ b/drivers/vfio/group.c @@ -381,6 +381,9 @@ int vfio_device_block_group(struct vfio_device *device) struct vfio_group *group =3D device->group; int ret =3D 0; =20 + if (vfio_null_group_allowed() && !group) + return 0; + mutex_lock(&group->group_lock); if (group->opened_file) { ret =3D -EBUSY; @@ -398,6 +401,9 @@ void vfio_device_unblock_group(struct vfio_device *devi= ce) { struct vfio_group *group =3D device->group; =20 + if (vfio_null_group_allowed() && !group) + return; + mutex_lock(&group->group_lock); group->cdev_device_open_cnt--; mutex_unlock(&group->group_lock); @@ -589,6 +595,14 @@ static struct vfio_group *vfio_noiommu_group_alloc(str= uct device *dev, struct vfio_group *group; int ret; =20 + /* + * With noiommu enabled under cdev interface only, there is no need to + * create a vfio_group if the group based containers are not enabled. + * The cdev interface is exclusively used for iommufd. + */ + if (vfio_null_group_allowed()) + return NULL; + iommu_group =3D iommu_group_alloc(); if (IS_ERR(iommu_group)) return ERR_CAST(iommu_group); diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 50128da18bca..838c08077ce2 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -113,6 +113,18 @@ bool vfio_device_has_container(struct vfio_device *dev= ice); int __init vfio_group_init(void); void vfio_group_cleanup(void); =20 +/* + * With noiommu enabled and no containers are supported, allow devices that + * don't have a dummy group. + */ +static inline bool vfio_null_group_allowed(void) +{ + if (vfio_noiommu && (!IS_ENABLED(CONFIG_VFIO_CONTAINER) && !IS_ENABLED(CO= NFIG_IOMMUFD_VFIO_CONTAINER))) + return true; + + return false; +} + static inline bool vfio_device_is_noiommu(struct vfio_device *vdev) { return IS_ENABLED(CONFIG_VFIO_NOIOMMU) && @@ -189,6 +201,11 @@ static inline void vfio_group_cleanup(void) { } =20 +static inline bool vfio_null_group_allowed(void) +{ + return false; +} + static inline bool vfio_device_is_noiommu(struct vfio_device *vdev) { return false; --=20 2.34.1 From nobody Tue Apr 7 14:36:46 2026 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 77CA73F23B5 for ; Thu, 12 Mar 2026 15:56:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773331012; cv=none; b=QLEJ8v8hrTXQc+CF08pbObePF/xDWPe+Dh08CQ+J9xO7zRUjETT/pbN3Kg2zIU65GapGlOldrww5TWRd9P/ITatGu9Xs1wV66imvxAsb810tDQEXdwr0ewXe8Q+VVhAOqOeYLucJNkPhVYjqZ7GhUmlm33QQ7K4dURKVudqcN+8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773331012; c=relaxed/simple; bh=vIsw4yuGQk7MJfcxOGm893uXXqE/sHLD0s/h51EyV40=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JjPb8F/ZuE4zMHam72dIgmmKrC/6P3kXW0uooXcfabCReG07mGey06caEpBxqT72Arx4KOlWJOrN+P27VmJXU5HyOln/57l+ufHejDsznwucQ7l9fQrw9mnx0XddoKCYisQOCkQlwZ1j//69XopSYJYrXEl1IKO/mIizbHuc20g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=lIYtQCHF; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="lIYtQCHF" Received: from DESKTOP-0403QTC.corp.microsoft.com (unknown [52.148.138.235]) by linux.microsoft.com (Postfix) with ESMTPSA id C20C220B6F0C; Thu, 12 Mar 2026 08:56:50 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com C20C220B6F0C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1773331011; bh=W96SnSZLqhLnwJNZa3jg8QlmCEk8GqczL6oQb+wbHn0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lIYtQCHFaGDkQim+UzSqc7bzah4QE7W1O5wrBRL9fcPI9bLJDH/vjoPix/3zRgIyj 0wXC+rXj19IN3x7FDXssc9QxI74NGXt8HiCu5+sxtBd9mQfF8wf0k/5Jtmv2/lhVNu 5sqrA34kbZTgGw8L//ERzQz2oFAxkgMtKYUqJTuo= From: Jacob Pan To: linux-kernel@vger.kernel.org, "iommu@lists.linux.dev" , Jason Gunthorpe , Alex Williamson , Joerg Roedel , David Matlack , Robin Murphy , Nicolin Chen , "Tian, Kevin" , Yi Liu Cc: skhawaja@google.com, pasha.tatashin@soleen.com, Will Deacon , Jacob Pan , Baolu Lu Subject: [PATCH V2 06/11] vfio: Introduce and set noiommu flag on vfio_device Date: Thu, 12 Mar 2026 08:56:32 -0700 Message-Id: <20260312155637.376854-7-jacob.pan@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260312155637.376854-1-jacob.pan@linux.microsoft.com> References: <20260312155637.376854-1-jacob.pan@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When a VFIO device is added to a noiommu group, set the noiommu flag on the vfio_device structure to indicate that the device operates in noiommu mode. Also update function signatures to pass vfio_device instead of device, which has the direct access to the noiommu flag. Signed-off-by: Jacob Pan Reviewed-by: Mostafa Saleh --- drivers/vfio/group.c | 21 +++++++++++---------- include/linux/vfio.h | 1 + 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/drivers/vfio/group.c b/drivers/vfio/group.c index 98f2a4f2ebff..6f98c57de9e0 100644 --- a/drivers/vfio/group.c +++ b/drivers/vfio/group.c @@ -588,7 +588,7 @@ static struct vfio_group *vfio_create_group(struct iomm= u_group *iommu_group, return ret; } =20 -static struct vfio_group *vfio_noiommu_group_alloc(struct device *dev, +static struct vfio_group *vfio_noiommu_group_alloc(struct vfio_device *vde= v, enum vfio_group_type type) { struct iommu_group *iommu_group; @@ -610,7 +610,7 @@ static struct vfio_group *vfio_noiommu_group_alloc(stru= ct device *dev, ret =3D iommu_group_set_name(iommu_group, "vfio-noiommu"); if (ret) goto out_put_group; - ret =3D iommu_group_add_device(iommu_group, dev); + ret =3D iommu_group_add_device(iommu_group, vdev->dev); if (ret) goto out_put_group; =20 @@ -625,7 +625,7 @@ static struct vfio_group *vfio_noiommu_group_alloc(stru= ct device *dev, return group; =20 out_remove_device: - iommu_group_remove_device(dev); + iommu_group_remove_device(vdev->dev); out_put_group: iommu_group_put(iommu_group); return ERR_PTR(ret); @@ -646,23 +646,24 @@ static bool vfio_group_has_device(struct vfio_group *= group, struct device *dev) return false; } =20 -static struct vfio_group *vfio_group_find_or_alloc(struct device *dev) +static struct vfio_group *vfio_group_find_or_alloc(struct vfio_device *vde= v) { struct iommu_group *iommu_group; struct vfio_group *group; =20 - iommu_group =3D iommu_group_get(dev); + iommu_group =3D iommu_group_get(vdev->dev); if (!iommu_group && vfio_noiommu) { + vdev->noiommu =3D 1; /* * With noiommu enabled, create an IOMMU group for devices that * don't already have one, implying no IOMMU hardware/driver * exists. Taint the kernel because we're about to give a DMA * capable device to a user without IOMMU protection. */ - group =3D vfio_noiommu_group_alloc(dev, VFIO_NO_IOMMU); + group =3D vfio_noiommu_group_alloc(vdev, VFIO_NO_IOMMU); if (!IS_ERR(group)) { add_taint(TAINT_USER, LOCKDEP_STILL_OK); - dev_warn(dev, "Adding kernel taint for vfio-noiommu group on device\n"); + dev_warn(vdev->dev, "Adding kernel taint for vfio-noiommu group on devi= ce\n"); } return group; } @@ -673,7 +674,7 @@ static struct vfio_group *vfio_group_find_or_alloc(stru= ct device *dev) mutex_lock(&vfio.group_lock); group =3D vfio_group_find_from_iommu(iommu_group); if (group) { - if (WARN_ON(vfio_group_has_device(group, dev))) + if (WARN_ON(vfio_group_has_device(group, vdev->dev))) group =3D ERR_PTR(-EINVAL); else refcount_inc(&group->drivers); @@ -693,9 +694,9 @@ int vfio_device_set_group(struct vfio_device *device, struct vfio_group *group; =20 if (type =3D=3D VFIO_IOMMU) - group =3D vfio_group_find_or_alloc(device->dev); + group =3D vfio_group_find_or_alloc(device); else - group =3D vfio_noiommu_group_alloc(device->dev, type); + group =3D vfio_noiommu_group_alloc(device, type); =20 if (IS_ERR(group)) return PTR_ERR(group); diff --git a/include/linux/vfio.h b/include/linux/vfio.h index e90859956514..844d14839f96 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -72,6 +72,7 @@ struct vfio_device { u8 iommufd_attached:1; #endif u8 cdev_opened:1; + u8 noiommu:1; #ifdef CONFIG_DEBUG_FS /* * debug_root is a static property of the vfio_device --=20 2.34.1 From nobody Tue Apr 7 14:36:46 2026 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F30723F23C6 for ; Thu, 12 Mar 2026 15:56:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773331013; cv=none; b=i9gN7bNrZH/a7CRALsZNkB3EsWJW5S+lOBX0P41NAvl/w0RW0auzJvYyQZlZr3egax82f5HVx2iAarHExiFoyyoewMMauFmdYwaaOSw711AZmyGopl5HaQCojdeBkMz1wPuUsgWw6+3uGpPdJtXwz5kyRO+Y7xaq5mHYZlMaN30= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773331013; c=relaxed/simple; bh=xVSDUH51PrQlO9FZjAgm0NH0MdnAZeIcuaw7S9IuRFI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=bwRsMGy8taqC4oGCyQ5UWgIwD/Ouzh+Ba+WgCLis7JTTmNtycqYzKZiwmQ80ZuayRfT6zQ1MFmMQsQaio2yVKV96M6CpzullulIFTQZigt9A8c/wp2PTiw6kHMskPBmy7URtkHydyG5q9SYjBbDIxUg06PMJDXADIW9NZM0Ds98= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=NvEF6qWa; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="NvEF6qWa" Received: from DESKTOP-0403QTC.corp.microsoft.com (unknown [52.148.138.235]) by linux.microsoft.com (Postfix) with ESMTPSA id 7DD1820B6F08; Thu, 12 Mar 2026 08:56:51 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 7DD1820B6F08 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1773331011; bh=78agt53msrArRhO0OIzV37Pk6/mDTJ+S/0LVkBcuyc0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NvEF6qWaBx37P6rvFNFteLOY6DiNj1pZ/fx21SqJ432lfxbMrUnbDeNma9ZN2Fti2 9ghOzdWCMbwUFnl7zAM6QgdyssN8wvlBdrG3Ck2OqiFbWOp0Mkl/hABGRO6qod1ith fTsERs9M8nFy+CKViXQT0ySLyyer2URYu2K2QZjw= From: Jacob Pan To: linux-kernel@vger.kernel.org, "iommu@lists.linux.dev" , Jason Gunthorpe , Alex Williamson , Joerg Roedel , David Matlack , Robin Murphy , Nicolin Chen , "Tian, Kevin" , Yi Liu Cc: skhawaja@google.com, pasha.tatashin@soleen.com, Will Deacon , Jacob Pan , Baolu Lu Subject: [PATCH V2 07/11] vfio: Update noiommu device detection logic for cdev Date: Thu, 12 Mar 2026 08:56:33 -0700 Message-Id: <20260312155637.376854-8-jacob.pan@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260312155637.376854-1-jacob.pan@linux.microsoft.com> References: <20260312155637.376854-1-jacob.pan@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rework vfio_device_is_noiommu() to derive noiommu mode based on device, group type, and configurations. Signed-off-by: Jacob Pan --- drivers/vfio/vfio.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 838c08077ce2..c5541967ef9b 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -127,8 +127,13 @@ static inline bool vfio_null_group_allowed(void) =20 static inline bool vfio_device_is_noiommu(struct vfio_device *vdev) { - return IS_ENABLED(CONFIG_VFIO_NOIOMMU) && - vdev->group->type =3D=3D VFIO_NO_IOMMU; + if (!IS_ENABLED(CONFIG_VFIO_NOIOMMU)) + return false; + + if (vfio_null_group_allowed()) + return vdev->noiommu; + + return vdev->group->type =3D=3D VFIO_NO_IOMMU; } #else struct vfio_group; --=20 2.34.1 From nobody Tue Apr 7 14:36:46 2026 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D299E3F54AA for ; Thu, 12 Mar 2026 15:56:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773331014; cv=none; b=UEh87HTQ7nqdrKf+zHAeByFKN087CwjK6KNgGmz2gixnLsqEDZsYv2B79XaAO3W24GG0otHEoIGj9MZjQbmSBg0Z6vis6k2KLSVyeohuo5k56GnXoOaxVvqWI1OrJGtfESKMzNXubuoIjooY7ode5mTKIA9sWdvXcF3Xwqyhtuo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773331014; c=relaxed/simple; bh=SavvkJ5bNxk/P/8LG/BekWiO2WEHd51gjqTsZXLBRVs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cwxk8T/13l5kwQMJc4PjHDvmz7KyRzpP4yJMGnFBtQ/DRsGnQjkeqKyqZf1d3lGi/o5tCeIC2HLoy1bq/v55iTBRbsIozX8NsuTrSYW0/0/Uy9+SF/skP/L/zRi5aZYz+Q5pLpXoUfbN2Bq47QBS2gDHMl1lt1VFYxbaRfWeTHU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=TRfeDbgK; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="TRfeDbgK" Received: from DESKTOP-0403QTC.corp.microsoft.com (unknown [52.148.138.235]) by linux.microsoft.com (Postfix) with ESMTPSA id 290FA20B6F12; Thu, 12 Mar 2026 08:56:52 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 290FA20B6F12 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1773331012; bh=7RSfYj9T7LuqQNLKAWtutzfQn7FQ0Vdz0zSCUEceyQU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TRfeDbgKNp4zMigFE+lILXtzKiSIQoDF3YZnAjGLEBbKsXoX4Zt7XZuBDLBBZtdmj v3wr5cacPfRD5uHF5Lc7fRHWzzcY04qmT/ZHvQlvCsMYxfMzHfB01KwZbDly0cgz9K IQ7+MLd7yrFj06hcCrtI6MKrYFSkj8zKn10sK1u0= From: Jacob Pan To: linux-kernel@vger.kernel.org, "iommu@lists.linux.dev" , Jason Gunthorpe , Alex Williamson , Joerg Roedel , David Matlack , Robin Murphy , Nicolin Chen , "Tian, Kevin" , Yi Liu Cc: skhawaja@google.com, pasha.tatashin@soleen.com, Will Deacon , Jacob Pan , Baolu Lu Subject: [PATCH V2 08/11] vfio: Enable cdev noiommu mode under iommufd Date: Thu, 12 Mar 2026 08:56:34 -0700 Message-Id: <20260312155637.376854-9-jacob.pan@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260312155637.376854-1-jacob.pan@linux.microsoft.com> References: <20260312155637.376854-1-jacob.pan@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that devices under noiommu mode can bind with IOMMUFD and perform IOAS operations, lift restrictions on cdev from VFIO side. No IOMMU cdevs are explicitly named with noiommu prefix. e.g. /dev/vfio/ |-- 7 |-- devices | `-- noiommu-vfio0 `-- vfio Signed-off-by: Jacob Pan --- v2: - Fix build dependency on IOMMU_SUPPORT Signed-off-by: Jacob Pan --- drivers/vfio/Kconfig | 7 +++++-- drivers/vfio/iommufd.c | 7 ------- drivers/vfio/vfio.h | 8 +------- drivers/vfio/vfio_main.c | 22 +++++++++++++++++++--- include/linux/vfio.h | 9 +++++++++ 5 files changed, 34 insertions(+), 19 deletions(-) diff --git a/drivers/vfio/Kconfig b/drivers/vfio/Kconfig index ceae52fd7586..78feca3d0c8b 100644 --- a/drivers/vfio/Kconfig +++ b/drivers/vfio/Kconfig @@ -22,8 +22,7 @@ config VFIO_DEVICE_CDEV The VFIO device cdev is another way for userspace to get device access. Userspace gets device fd by opening device cdev under /dev/vfio/devices/vfioX, and then bind the device fd with an iommufd - to set up secure DMA context for device access. This interface does - not support noiommu. + to set up secure DMA context for device access. =20 If you don't know what to do here, say N. =20 @@ -63,6 +62,10 @@ endif config VFIO_NOIOMMU bool "VFIO No-IOMMU support" depends on VFIO_GROUP + select GENERIC_PT + select IOMMU_PT + select IOMMU_PT_AMDV1 + depends on IOMMU_SUPPORT help VFIO is built on the ability to isolate devices using the IOMMU. Only with an IOMMU can userspace access to DMA capable devices be diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c index a38d262c6028..26c9c3068c77 100644 --- a/drivers/vfio/iommufd.c +++ b/drivers/vfio/iommufd.c @@ -25,10 +25,6 @@ int vfio_df_iommufd_bind(struct vfio_device_file *df) =20 lockdep_assert_held(&vdev->dev_set->lock); =20 - /* Returns 0 to permit device opening under noiommu mode */ - if (vfio_device_is_noiommu(vdev)) - return 0; - return vdev->ops->bind_iommufd(vdev, ictx, &df->devid); } =20 @@ -58,9 +54,6 @@ void vfio_df_iommufd_unbind(struct vfio_device_file *df) =20 lockdep_assert_held(&vdev->dev_set->lock); =20 - if (vfio_device_is_noiommu(vdev)) - return; - if (vdev->ops->unbind_iommufd) vdev->ops->unbind_iommufd(vdev); } diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index c5541967ef9b..f6262f2cc7a6 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -381,19 +381,13 @@ void vfio_init_device_cdev(struct vfio_device *device= ); =20 static inline int vfio_device_add(struct vfio_device *device) { - /* cdev does not support noiommu device */ - if (vfio_device_is_noiommu(device)) - return device_add(&device->device); vfio_init_device_cdev(device); return cdev_device_add(&device->cdev, &device->device); } =20 static inline void vfio_device_del(struct vfio_device *device) { - if (vfio_device_is_noiommu(device)) - device_del(&device->device); - else - cdev_device_del(&device->cdev, &device->device); + cdev_device_del(&device->cdev, &device->device); } =20 int vfio_device_fops_cdev_open(struct inode *inode, struct file *filep); diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 742477546b15..099d9b1ade4c 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -331,13 +331,15 @@ static int __vfio_register_dev(struct vfio_device *de= vice, if (!device->dev_set) vfio_assign_device_set(device, device); =20 - ret =3D dev_set_name(&device->device, "vfio%d", device->index); + ret =3D vfio_device_set_group(device, type); if (ret) return ret; =20 - ret =3D vfio_device_set_group(device, type); + /* Just to be safe, expose to user explicitly noiommu cdev node */ + ret =3D dev_set_name(&device->device, "%svfio%d", + device->noiommu ? "noiommu-" : "", device->index); if (ret) - return ret; + goto err_out; =20 /* * VFIO always sets IOMMU_CACHE because we offer no way for userspace to @@ -357,6 +359,10 @@ static int __vfio_register_dev(struct vfio_device *dev= ice, /* Refcounting can't start until the driver calls register */ refcount_set(&device->refcount, 1); =20 + /* noiommu device w/o container may have NULL group */ + if (vfio_device_is_noiommu(device) && !vfio_device_has_group(device)) + return 0; + vfio_device_group_register(device); vfio_device_debugfs_init(device); =20 @@ -391,6 +397,16 @@ void vfio_unregister_group_dev(struct vfio_device *dev= ice) bool interrupted =3D false; long rc; =20 + /* + * For noiommu devices without a container, thus no dummy group, + * simply delete and unregister to balance refcount. + */ + if (device->noiommu && !vfio_device_has_group(device)) { + vfio_device_del(device); + vfio_device_put_registration(device); + return; + } + /* * Prevent new device opened by userspace via the * VFIO_GROUP_GET_DEVICE_FD in the group path. diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 844d14839f96..775bd4f6bae9 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -327,6 +327,10 @@ struct iommu_group *vfio_file_iommu_group(struct file = *file); #if IS_ENABLED(CONFIG_VFIO_GROUP) bool vfio_file_is_group(struct file *file); bool vfio_file_has_dev(struct file *file, struct vfio_device *device); +static inline bool vfio_device_has_group(struct vfio_device *device) +{ + return device->group; +} #else static inline bool vfio_file_is_group(struct file *file) { @@ -337,6 +341,11 @@ static inline bool vfio_file_has_dev(struct file *file= , struct vfio_device *devi { return false; } + +static inline bool vfio_device_has_group(struct vfio_device *device) +{ + return false; +} #endif bool vfio_file_is_valid(struct file *file); bool vfio_file_enforced_coherent(struct file *file); --=20 2.34.1 From nobody Tue Apr 7 14:36:46 2026 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 81F1F3DA7E2 for ; Thu, 12 Mar 2026 15:56:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773331015; cv=none; b=Cm5lwxRlQ/CacuB7AqS6B80Pdrm6r2hwURdcjyzasyEBcD92RwZ1rzfJS5CSPZp2hBC6+eCqdkJ0tdai4XYfWyLm1y3Rd0B21HZqUMP/RIbrCJ1Wd44RxmgAn/YOHwquTN2ZI1i0sIw/6xRhpjNWyfT0OXWOo1zDLiB8Vx5f7Ck= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773331015; c=relaxed/simple; bh=Yfl32e/wH1s1fkSBmCkA0l5uMrhboi+dTtlW0Ww5QD4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=THYHmiug+MKn2GwQZI9bAnq8C9xeRFWhgA47SPUqw6xAFuBZHfeuzSBHS4PX5hb8z2HBvh+cvjC/8LhDGtQkQVvrUzP+JqOOMp++si5YlrY5oNv3uzkcyTg3FKgycLBQoESIezpdRyGkXsF9KInp275fFUmPlK9h9W3YF9r+zyg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=Sp6dT90h; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="Sp6dT90h" Received: from DESKTOP-0403QTC.corp.microsoft.com (unknown [52.148.138.235]) by linux.microsoft.com (Postfix) with ESMTPSA id EFC4B20B6F15; Thu, 12 Mar 2026 08:56:52 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com EFC4B20B6F15 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1773331013; bh=6C5HqNSNlEXnphrTuI5WMJTUuVyBmzclsZEl1pFAJmU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Sp6dT90he5TB472gC+2sQoU1xJirIipV0ygV75bDlFlyCl3Su0+45XCgHZumUDQDc BAZPwgtztN9dJt0OwzN6o/yt2WatoNlbNJlmCbqnW1ZiVvKA2jeelORU5lKZTGnX4q SBqPtIkHsFY7z85Z0MTGWB/2IF0uPFLmjtsEEvMI= From: Jacob Pan To: linux-kernel@vger.kernel.org, "iommu@lists.linux.dev" , Jason Gunthorpe , Alex Williamson , Joerg Roedel , David Matlack , Robin Murphy , Nicolin Chen , "Tian, Kevin" , Yi Liu Cc: skhawaja@google.com, pasha.tatashin@soleen.com, Will Deacon , Jacob Pan , Baolu Lu Subject: [PATCH V2 09/11] vfio:selftest: Handle VFIO noiommu cdev Date: Thu, 12 Mar 2026 08:56:35 -0700 Message-Id: <20260312155637.376854-10-jacob.pan@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260312155637.376854-1-jacob.pan@linux.microsoft.com> References: <20260312155637.376854-1-jacob.pan@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" With unsafe DMA noiommu mode, the vfio devices are prefixed with noiommu-, e.g. /dev/vfio/ |-- devices | `-- noiommu-vfio0 |-- noiommu-0 `-- vfio Let vfio tests, such as luo kexec test, accommodate the noiommu device files. Signed-off-by: Jacob Pan --- .../selftests/vfio/lib/vfio_pci_device.c | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index 4e5871f1ebc3..15ddeb634a8d 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -290,6 +290,24 @@ static void vfio_pci_device_setup(struct vfio_pci_devi= ce *device) device->msi_eventfds[i] =3D -1; } =20 + +static int is_unsafe_noiommu_mode_enabled(void) +{ + const char *path =3D "/sys/module/vfio/parameters/enable_unsafe_noiommu_m= ode"; + FILE *f; + int c; + + f =3D fopen(path, "re"); + if (!f) + return 0; + + c =3D fgetc(f); + fclose(f); + if (c =3D=3D 'Y' || c =3D=3D 'y') + return 1; + return 0; +} + const char *vfio_pci_get_cdev_path(const char *bdf) { char dir_path[PATH_MAX]; @@ -306,8 +324,11 @@ const char *vfio_pci_get_cdev_path(const char *bdf) VFIO_ASSERT_NOT_NULL(dir, "Failed to open directory %s\n", dir_path); =20 while ((entry =3D readdir(dir)) !=3D NULL) { - /* Find the file that starts with "vfio" */ - if (strncmp("vfio", entry->d_name, 4)) + /* Find the file that starts with "noiommu-vfio" or "vfio" */ + if (is_unsafe_noiommu_mode_enabled()) { + if (strncmp("noiommu-vfio", entry->d_name, strlen("noiommu-vfio"))) + continue; + } else if (strncmp("vfio", entry->d_name, 4)) continue; =20 snprintf(cdev_path, PATH_MAX, "/dev/vfio/devices/%s", entry->d_name); --=20 2.34.1 From nobody Tue Apr 7 14:36:46 2026 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C564E3F65F7 for ; Thu, 12 Mar 2026 15:56:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773331017; cv=none; b=US0mtWMBc4Va7+nVQPchc8PgSWz04BM/WAI3gu4TEsIS1CBTTcm/yzx5bHN16YgtNkPBEeegr4cPsBLfj6jNMEZn5dIgcf7zpbxQuVnydhIWzvFheM3sZ54ZzK7VEyCPRxCP40SnaiLyelkZmVIA74TCFunCTcLvY2i2SlclpaY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773331017; c=relaxed/simple; bh=0GnQ6swHf++IRf9uF6DLE/TfYZzCPFnXpiREOAhmKy4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kHEEUYh+pLtQG0tsCfzfKApxM2dVDTEMxW7qujNR1+UZqqVN3wLp8gYYKqlY74gquhqXFF2Gw5lb1Bh/DK3p8eqjttQa4Cvw0rBdTMCmzn6nx4y4Y4N1OSREXbGApvjd4oxCths5WUV0uyk5uSoZ79acHor4wX8/3lPlQoaSiEA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=Q1SxJovb; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="Q1SxJovb" Received: from DESKTOP-0403QTC.corp.microsoft.com (unknown [52.148.138.235]) by linux.microsoft.com (Postfix) with ESMTPSA id B592720B6F1B; Thu, 12 Mar 2026 08:56:53 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com B592720B6F1B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1773331014; bh=GIFO/r4Zge31+IvYJM2kJV9vimDfgfGPrhcDaKGBQe8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Q1SxJovbDtxTyvqbj2K05AiNBdJWjwacbYsVQs82hsngwcP1bfvTm4sgua3Z7Kh93 MFewLM4wLWUTSi7EzQwliRjv53YiumbZnEuA7Y+25xz2fRsO5oyjIm+IHftqCnFHZ/ Lnx2RUlo5J9xL4UzZBWBo459Up1s8b8rcABdkZ9g= From: Jacob Pan To: linux-kernel@vger.kernel.org, "iommu@lists.linux.dev" , Jason Gunthorpe , Alex Williamson , Joerg Roedel , David Matlack , Robin Murphy , Nicolin Chen , "Tian, Kevin" , Yi Liu Cc: skhawaja@google.com, pasha.tatashin@soleen.com, Will Deacon , Jacob Pan , Baolu Lu Subject: [PATCH V2 10/11] selftests/vfio: Add iommufd noiommu mode selftest for cdev Date: Thu, 12 Mar 2026 08:56:36 -0700 Message-Id: <20260312155637.376854-11-jacob.pan@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260312155637.376854-1-jacob.pan@linux.microsoft.com> References: <20260312155637.376854-1-jacob.pan@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add comprehensive selftest for VFIO device operations with iommufd in noiommu mode. Tests cover: - Device binding to iommufd - IOAS (I/O Address Space) allocation, mapping with dummy IOVA - Retrieve PA from dummy IOVA - Device attach/detach operations as usual Signed-off-by: Jacob Pan --- v2: - Use huge page ioas map to test GET_PA searching for contiguous PA range. --- tools/testing/selftests/vfio/Makefile | 1 + .../vfio/vfio_iommufd_noiommu_test.c | 549 ++++++++++++++++++ 2 files changed, 550 insertions(+) create mode 100644 tools/testing/selftests/vfio/vfio_iommufd_noiommu_test.c diff --git a/tools/testing/selftests/vfio/Makefile b/tools/testing/selftest= s/vfio/Makefile index 8e90e409e91d..90f41d8ce3c7 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -9,6 +9,7 @@ CFLAGS =3D $(KHDR_INCLUDES) TEST_GEN_PROGS +=3D vfio_dma_mapping_test TEST_GEN_PROGS +=3D vfio_dma_mapping_mmio_test TEST_GEN_PROGS +=3D vfio_iommufd_setup_test +TEST_GEN_PROGS +=3D vfio_iommufd_noiommu_test TEST_GEN_PROGS +=3D vfio_pci_device_test TEST_GEN_PROGS +=3D vfio_pci_device_init_perf_test TEST_GEN_PROGS +=3D vfio_pci_driver_test diff --git a/tools/testing/selftests/vfio/vfio_iommufd_noiommu_test.c b/too= ls/testing/selftests/vfio/vfio_iommufd_noiommu_test.c new file mode 100644 index 000000000000..c4e4fcd09342 --- /dev/null +++ b/tools/testing/selftests/vfio/vfio_iommufd_noiommu_test.c @@ -0,0 +1,549 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * VFIO iommufd NoIOMMU Mode Selftest + * + * Tests VFIO device operations with iommufd in noiommu mode, including: + * - Device binding to iommufd + * - IOAS (I/O Address Space) allocation and management + * - Device attach/detach to IOAS + * - Memory mapping in IOAS + * - Device info queries and reset + */ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "kselftest_harness.h" + +static const char iommu_dev_path[] =3D "/dev/iommu"; +static const char *cdev_path; + +static char *vfio_noiommu_get_device_id(const char *bdf) +{ + char *path =3D NULL; + char *vfio_id =3D NULL; + struct dirent *dentry; + DIR *dp; + + if (asprintf(&path, "/sys/bus/pci/devices/%s/vfio-dev", bdf) < 0) + return NULL; + + dp =3D opendir(path); + if (!dp) { + free(path); + return NULL; + } + + while ((dentry =3D readdir(dp)) !=3D NULL) { + if (strncmp("noiommu-vfio", dentry->d_name, 12) =3D=3D 0) { + vfio_id =3D strdup(dentry->d_name); + break; + } + } + + closedir(dp); + free(path); + return vfio_id; +} + +static char *vfio_noiommu_get_cdev_path(const char *bdf) +{ + char *vfio_id =3D vfio_noiommu_get_device_id(bdf); + char *cdev =3D NULL; + + if (vfio_id) { + asprintf(&cdev, "/dev/vfio/devices/%s", vfio_id); + free(vfio_id); + } + return cdev; +} + +static int vfio_device_bind_iommufd_ioctl(int cdev_fd, int iommufd) +{ + struct vfio_device_bind_iommufd bind_args =3D { + .argsz =3D sizeof(bind_args), + .iommufd =3D iommufd, + }; + + return ioctl(cdev_fd, VFIO_DEVICE_BIND_IOMMUFD, &bind_args); +} + +static int vfio_device_get_info_ioctl(int cdev_fd, + struct vfio_device_info *info) +{ + info->argsz =3D sizeof(*info); + return ioctl(cdev_fd, VFIO_DEVICE_GET_INFO, info); +} + +static int vfio_device_ioas_alloc_ioctl(int iommufd, + struct iommu_ioas_alloc *alloc_args) +{ + alloc_args->size =3D sizeof(*alloc_args); + alloc_args->flags =3D 0; + return ioctl(iommufd, IOMMU_IOAS_ALLOC, alloc_args); +} + +static int vfio_device_attach_iommufd_pt_ioctl(int cdev_fd, u32 pt_id) +{ + struct vfio_device_attach_iommufd_pt attach_args =3D { + .argsz =3D sizeof(attach_args), + .pt_id =3D pt_id, + }; + + return ioctl(cdev_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach_args); +} + +static int vfio_device_detach_iommufd_pt_ioctl(int cdev_fd) +{ + struct vfio_device_detach_iommufd_pt detach_args =3D { + .argsz =3D sizeof(detach_args), + }; + + return ioctl(cdev_fd, VFIO_DEVICE_DETACH_IOMMUFD_PT, &detach_args); +} + +static int vfio_device_get_region_info_ioctl(int cdev_fd, uint32_t index, + struct vfio_region_info *info) +{ + info->argsz =3D sizeof(*info); + info->index =3D index; + return ioctl(cdev_fd, VFIO_DEVICE_GET_REGION_INFO, info); +} + +static int vfio_device_reset_ioctl(int cdev_fd) +{ + return ioctl(cdev_fd, VFIO_DEVICE_RESET); +} + +static int ioas_map_pages(int iommufd, uint32_t ioas_id, uint64_t iova, + size_t length, bool hugepages) +{ + struct iommu_ioas_map map_args =3D { + .size =3D sizeof(map_args), + .ioas_id =3D ioas_id, + .iova =3D iova, + .length =3D length, + .flags =3D IOMMU_IOAS_MAP_READABLE | IOMMU_IOAS_MAP_WRITEABLE | IOMMU_IO= AS_MAP_FIXED_IOVA, + }; + void *pages; + int ret; + + /* Allocate test pages */ + if (hugepages) + pages =3D mmap(NULL, length, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0); + else + pages =3D mmap(NULL, length, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (pages =3D=3D MAP_FAILED) { + printf("mmap failed for length 0x%lx\n", (unsigned long)length); + return -ENOMEM; + } + + /* Set up page pointer for mapping */ + map_args.user_va =3D (uintptr_t)pages; + + printf(" ioas_map_pages: ioas_id=3D%u, iova=3D0x%lx, length=3D0x%lx, use= r_va=3D%p\n", + ioas_id, (unsigned long)iova, (unsigned long)length, pages); + + /* Map into IOAS */ + ret =3D ioctl(iommufd, IOMMU_IOAS_MAP, &map_args); + if (ret !=3D 0) + printf(" IOMMU_IOAS_MAP failed: %d (%s)\n", ret, strerror(errno)); + else + printf(" IOMMU_IOAS_MAP succeeded, IOVA=3D0x%lx\n", (unsigned long)map_= args.iova); + + munmap(pages, length); + return ret; +} + +static int ioas_unmap_pages(int iommufd, uint32_t ioas_id, uint64_t iova, + size_t length) +{ + struct iommu_ioas_unmap unmap_args =3D { + .size =3D sizeof(unmap_args), + .ioas_id =3D ioas_id, + .iova =3D iova, + .length =3D length, + }; + + return ioctl(iommufd, IOMMU_IOAS_UNMAP, &unmap_args); +} + +static int ioas_destroy_ioctl(int iommufd, uint32_t ioas_id) +{ + struct iommu_destroy destroy_args =3D { + .size =3D sizeof(destroy_args), + .id =3D ioas_id, + }; + + return ioctl(iommufd, IOMMU_DESTROY, &destroy_args); +} + +static int ioas_get_pa_ioctl(int iommufd, uint32_t ioas_id, uint64_t iova, + uint64_t *phys_out, uint64_t *length_out) +{ + struct { + __u32 size; + __u32 flags; + __u32 ioas_id; + __u32 __reserved; + __u64 iova; + __u64 out_length; + __u64 out_phys; + } get_pa =3D { + .size =3D sizeof(get_pa), + .flags =3D 0, + .ioas_id =3D ioas_id, + .iova =3D iova, + }; + + printf(" ioas_get_pa_ioctl: ioas_id=3D%u, iova=3D0x%lx\n", + ioas_id, (unsigned long)iova); + + if (ioctl(iommufd, IOMMU_IOAS_GET_PA, &get_pa) !=3D 0) { + printf(" IOMMU_IOAS_GET_PA failed: %s (errno=3D%d)\n", + strerror(errno), errno); + return -1; + } + + printf(" IOMMU_IOAS_GET_PA succeeded: PA=3D0x%lx, length=3D0x%lx\n", + (unsigned long)get_pa.out_phys, (unsigned long)get_pa.out_length); + + if (phys_out) + *phys_out =3D get_pa.out_phys; + if (length_out) + *length_out =3D get_pa.out_length; + + return 0; +} + +FIXTURE(vfio_noiommu) { + int cdev_fd; + int iommufd; +}; + +FIXTURE_SETUP(vfio_noiommu) +{ + ASSERT_LE(0, (self->cdev_fd =3D open(cdev_path, O_RDWR, 0))); + ASSERT_LE(0, (self->iommufd =3D open(iommu_dev_path, O_RDWR, 0))); +} + +FIXTURE_TEARDOWN(vfio_noiommu) +{ + if (self->cdev_fd >=3D 0) + close(self->cdev_fd); + if (self->iommufd >=3D 0) + close(self->iommufd); +} + +/* + * Test: Device cdev can be opened + */ +TEST_F(vfio_noiommu, device_cdev_open) +{ + ASSERT_LE(0, self->cdev_fd); +} + +/* + * Test: Device can be bound to iommufd + */ +TEST_F(vfio_noiommu, device_bind_iommufd) +{ + ASSERT_EQ(0, vfio_device_bind_iommufd_ioctl(self->cdev_fd, + self->iommufd)); +} + +/* + * Test: Device info can be queried after binding + */ +TEST_F(vfio_noiommu, device_get_info_after_bind) +{ + struct vfio_device_info info; + + ASSERT_EQ(0, vfio_device_bind_iommufd_ioctl(self->cdev_fd, + self->iommufd)); + ASSERT_EQ(0, vfio_device_get_info_ioctl(self->cdev_fd, &info)); + ASSERT_NE(0, info.argsz); +} + +/* + * Test: Getting device info fails without bind + */ +TEST_F(vfio_noiommu, device_get_info_without_bind_fails) +{ + struct vfio_device_info info; + + ASSERT_NE(0, vfio_device_get_info_ioctl(self->cdev_fd, &info)); +} + +/* + * Test: Binding with invalid iommufd fails + */ +TEST_F(vfio_noiommu, device_bind_bad_iommufd_fails) +{ + ASSERT_NE(0, vfio_device_bind_iommufd_ioctl(self->cdev_fd, -2)); +} + +/* + * Test: Cannot bind twice to same device + */ +TEST_F(vfio_noiommu, device_repeated_bind_fails) +{ + ASSERT_EQ(0, vfio_device_bind_iommufd_ioctl(self->cdev_fd, + self->iommufd)); + ASSERT_NE(0, vfio_device_bind_iommufd_ioctl(self->cdev_fd, + self->iommufd)); +} + +/* + * Test: IOAS can be allocated + */ +TEST_F(vfio_noiommu, ioas_alloc) +{ + struct iommu_ioas_alloc alloc_args; + + ASSERT_EQ(0, vfio_device_ioas_alloc_ioctl(self->iommufd, + &alloc_args)); + ASSERT_NE(0, alloc_args.out_ioas_id); +} + +/* + * Test: IOAS can be destroyed + */ +TEST_F(vfio_noiommu, ioas_destroy) +{ + struct iommu_ioas_alloc alloc_args; + + ASSERT_EQ(0, vfio_device_ioas_alloc_ioctl(self->iommufd, + &alloc_args)); + ASSERT_EQ(0, ioas_destroy_ioctl(self->iommufd, + alloc_args.out_ioas_id)); +} + +/* + * Test: Device can attach to IOAS after binding + */ +TEST_F(vfio_noiommu, device_attach_to_ioas) +{ + struct iommu_ioas_alloc alloc_args; + + ASSERT_EQ(0, vfio_device_bind_iommufd_ioctl(self->cdev_fd, + self->iommufd)); + ASSERT_EQ(0, vfio_device_ioas_alloc_ioctl(self->iommufd, + &alloc_args)); + ASSERT_EQ(0, vfio_device_attach_iommufd_pt_ioctl(self->cdev_fd, + alloc_args.out_ioas_id)); +} + +/* + * Test: Attaching to invalid IOAS fails + */ +TEST_F(vfio_noiommu, device_attach_invalid_ioas_fails) +{ + ASSERT_EQ(0, vfio_device_bind_iommufd_ioctl(self->cdev_fd, + self->iommufd)); + ASSERT_NE(0, vfio_device_attach_iommufd_pt_ioctl(self->cdev_fd, + UINT32_MAX)); +} + +/* + * Test: Device can detach from IOAS + */ +TEST_F(vfio_noiommu, device_detach_from_ioas) +{ + struct iommu_ioas_alloc alloc_args; + + ASSERT_EQ(0, vfio_device_bind_iommufd_ioctl(self->cdev_fd, + self->iommufd)); + ASSERT_EQ(0, vfio_device_ioas_alloc_ioctl(self->iommufd, + &alloc_args)); + ASSERT_EQ(0, vfio_device_attach_iommufd_pt_ioctl(self->cdev_fd, + alloc_args.out_ioas_id)); + ASSERT_EQ(0, vfio_device_detach_iommufd_pt_ioctl(self->cdev_fd)); +} + +/* + * Test: Full lifecycle - bind, attach, detach, reset + */ +TEST_F(vfio_noiommu, device_lifecycle) +{ + struct iommu_ioas_alloc alloc_args; + struct vfio_device_info info; + + /* Bind device to iommufd */ + ASSERT_EQ(0, vfio_device_bind_iommufd_ioctl(self->cdev_fd, + self->iommufd)); + + /* Allocate IOAS */ + ASSERT_EQ(0, vfio_device_ioas_alloc_ioctl(self->iommufd, + &alloc_args)); + + /* Attach device to IOAS */ + ASSERT_EQ(0, vfio_device_attach_iommufd_pt_ioctl(self->cdev_fd, + alloc_args.out_ioas_id)); + + /* Query device info */ + ASSERT_EQ(0, vfio_device_get_info_ioctl(self->cdev_fd, &info)); + + /* Detach device from IOAS */ + ASSERT_EQ(0, vfio_device_detach_iommufd_pt_ioctl(self->cdev_fd)); + + /* Reset device */ + ASSERT_EQ(0, vfio_device_reset_ioctl(self->cdev_fd)); +} + +/* + * Test: Get region info + */ +TEST_F(vfio_noiommu, device_get_region_info) +{ + struct vfio_device_info dev_info; + struct vfio_region_info region_info; + + ASSERT_EQ(0, vfio_device_bind_iommufd_ioctl(self->cdev_fd, + self->iommufd)); + ASSERT_EQ(0, vfio_device_get_info_ioctl(self->cdev_fd, &dev_info)); + + /* Try to get first region info if device has regions */ + if (dev_info.num_regions > 0) { + ASSERT_EQ(0, vfio_device_get_region_info_ioctl(self->cdev_fd, 0, + ®ion_info)); + ASSERT_NE(0, region_info.argsz); + } +} + +TEST_F(vfio_noiommu, device_reset) +{ + ASSERT_EQ(0, vfio_device_bind_iommufd_ioctl(self->cdev_fd, + self->iommufd)); + ASSERT_EQ(0, vfio_device_reset_ioctl(self->cdev_fd)); +} + +TEST_F(vfio_noiommu, ioas_map_pages) +{ + struct iommu_ioas_alloc alloc_args; + long page_size =3D sysconf(_SC_PAGESIZE); + uint64_t iova =3D 0x10000; + int i; + + ASSERT_GT(page_size, 0); + + ASSERT_EQ(0, vfio_device_ioas_alloc_ioctl(self->iommufd, + &alloc_args)); + + printf("Page size: %ld bytes\n", page_size); + /* Test mapping regions of different sizes: 1, 2, 4, 8 pages */ + for (i =3D 0; i < 4; i++) { + size_t map_size =3D page_size * (1 << i); /* 1, 2, 4, 8 pages */ + uint64_t test_iova =3D iova + (i * 0x100000); + + /* Attempt to map each region (may fail if not supported) */ + ioas_map_pages(self->iommufd, alloc_args.out_ioas_id, + test_iova, map_size, false); + } +} + +TEST_F(vfio_noiommu, multiple_ioas_alloc) +{ + struct iommu_ioas_alloc alloc1, alloc2; + + ASSERT_EQ(0, vfio_device_ioas_alloc_ioctl(self->iommufd, &alloc1)); + ASSERT_EQ(0, vfio_device_ioas_alloc_ioctl(self->iommufd, &alloc2)); + ASSERT_NE(alloc1.out_ioas_id, alloc2.out_ioas_id); +} + +/* + * Test: Query physical address for IOVA + * Tests IOMMU_IOAS_GET_PA ioctl to translate IOVA to physical address + * Note: Device must be attached to IOAS for PA query to work + */ +#define NR_PAGES 32 +TEST_F(vfio_noiommu, ioas_get_pa_mapped) +{ + struct iommu_ioas_alloc alloc_args; + long page_size =3D sysconf(_SC_PAGESIZE); + uint64_t iova =3D 0x200000; + uint64_t phys =3D 0; + uint64_t length =3D 0; + int ret; + + ASSERT_GT(page_size, 0); + + ASSERT_EQ(0, vfio_device_bind_iommufd_ioctl(self->cdev_fd, + self->iommufd)); + + ASSERT_EQ(0, vfio_device_ioas_alloc_ioctl(self->iommufd, + &alloc_args)); + + ASSERT_EQ(0, vfio_device_attach_iommufd_pt_ioctl(self->cdev_fd, + alloc_args.out_ioas_id)); + + /* + * Map a page into an arbitrary IOAS, used as a cookie for lookup. + * Use hugepages to test contiguous PA. Make sure hugepages are + * available. e.g. echo 64 > /proc/sys/vm/nr_hugepages + */ + ret =3D ioas_map_pages(self->iommufd, alloc_args.out_ioas_id, + iova, page_size * NR_PAGES, true); + if (ret !=3D 0) + return; + + /* Query the physical address for the mapped dummy IOVA */ + ret =3D ioas_get_pa_ioctl(self->iommufd, alloc_args.out_ioas_id, + iova, &phys, &length); + + if (ret =3D=3D 0) { + /* If we got a result, verify it's valid */ + ASSERT_NE(0, phys); + ASSERT_GE((uint64_t)page_size * NR_PAGES, length); + } +} + +TEST_F(vfio_noiommu, ioas_get_pa_unmapped_fails) +{ + struct iommu_ioas_alloc alloc_args; + + ASSERT_EQ(0, vfio_device_ioas_alloc_ioctl(self->iommufd, + &alloc_args)); + + /* Try to retrieve unmapped IOVA (should fail) */ + ASSERT_NE(0, ioas_get_pa_ioctl(self->iommufd, alloc_args.out_ioas_id, + 0x10000, NULL, NULL)); +} + +int main(int argc, char *argv[]) +{ + const char *device_bdf =3D vfio_selftests_get_bdf(&argc, argv); + char *cdev =3D NULL; + + if (!device_bdf) { + ksft_print_msg("No device BDF provided\n"); + return KSFT_SKIP; + } + + cdev =3D vfio_noiommu_get_cdev_path(device_bdf); + if (!cdev) { + ksft_print_msg("Could not find cdev for device %s\n", + device_bdf); + return KSFT_SKIP; + } + + cdev_path =3D cdev; + ksft_print_msg("Using cdev device %s for BDF %s\n", cdev_path, + device_bdf); + + return test_harness_run(argc, argv); +} --=20 2.34.1 From nobody Tue Apr 7 14:36:46 2026 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9D92B3F6613 for ; Thu, 12 Mar 2026 15:56:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=13.77.154.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773331017; cv=none; b=BG8izTlf1tFYZTILcYsReuAOH6BYa6jWl3zVxpIkgOuCQdCLLDSgRRk3SWbYLuhtKTeXkrwi+/X0/TNEj4dydDZUOnJrCdBq9Tn1uCosH3h6+dSAQcsbGN/mfpdkcKWugdRnK5N3+r4oEbokzdItye/5+oZL/VQaoJf4j8h2t9w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773331017; c=relaxed/simple; bh=I5gAUUB8Ti6eWgykzRpibFwTRcv9KWB0V/PC/QTQ5AM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EI4DKDy5kWxL75yWNE0aMeupCULZzOwnifeAfYpRzz3ZYuC0ta/uyEP1Gc6iKdSu/x/R8tWP/HBXKXaeSe4nIFBDaa6XhEY3+zgkGilo3pw4EeqWEfY+Q1fxJMtxfAjLaDzI61gY+ivqL7t8jhhle13aq7f90zubuY3DhEiT37g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b=ByqJJuHE; arc=none smtp.client-ip=13.77.154.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.b="ByqJJuHE" Received: from DESKTOP-0403QTC.corp.microsoft.com (unknown [52.148.138.235]) by linux.microsoft.com (Postfix) with ESMTPSA id 096C320B6F01; Thu, 12 Mar 2026 08:56:54 -0700 (PDT) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 096C320B6F01 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1773331015; bh=tizjuLFHEk/Wc4SiQ1zGGxLYp2aa4CoKwD5TQrr+Y0E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ByqJJuHEuZbxzdTQo1dJQxfrt55PNrUOCV1jWLdgpBiBysyzCs58lXq43bdvcU5mD TFc1sR5XAEoqcwljwwASIOLOVCYiIMymnp4E6HgBLWVAhpMSKEtB7gZNtwwSNF3dgv xzFO3GirFjlX47M2bNGW8/wnd7zFE2dK42rH6Ezo= From: Jacob Pan To: linux-kernel@vger.kernel.org, "iommu@lists.linux.dev" , Jason Gunthorpe , Alex Williamson , Joerg Roedel , David Matlack , Robin Murphy , Nicolin Chen , "Tian, Kevin" , Yi Liu Cc: skhawaja@google.com, pasha.tatashin@soleen.com, Will Deacon , Jacob Pan , Baolu Lu Subject: [PATCH V2 11/11] Doc: Update VFIO NOIOMMU mode Date: Thu, 12 Mar 2026 08:56:37 -0700 Message-Id: <20260312155637.376854-12-jacob.pan@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260312155637.376854-1-jacob.pan@linux.microsoft.com> References: <20260312155637.376854-1-jacob.pan@linux.microsoft.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Document the NOIOMMU mode with newly added cdev support under iommufd. Signed-off-by: Jacob Pan --- Documentation/driver-api/vfio.rst | 44 +++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/Documentation/driver-api/vfio.rst b/Documentation/driver-api/v= fio.rst index 2a21a42c9386..d1ee13dc6e98 100644 --- a/Documentation/driver-api/vfio.rst +++ b/Documentation/driver-api/vfio.rst @@ -275,8 +275,6 @@ in a VFIO group. With CONFIG_VFIO_DEVICE_CDEV=3Dy the user can now acquire a device fd by directly opening a character device /dev/vfio/devices/vfioX where "X" is the number allocated uniquely by VFIO for registered devices. -cdev interface does not support noiommu devices, so user should use -the legacy group interface if noiommu is wanted. =20 The cdev only works with IOMMUFD. Both VFIO drivers and applications must adapt to the new cdev security model which requires using @@ -370,6 +368,48 @@ IOMMUFD IOAS/HWPT to enable userspace DMA:: =20 /* Other device operations as stated in "VFIO Usage Example" */ =20 +VFIO NOIOMMU mode +--------------------------------------------------------------------------= ----- +VFIO also supports a no-IOMMU mode, intended for usages where unsafe DMA c= an +be performed by userspace drivers w/o physical IOMMU protection. This mode +is controlled by the parameter: + +/sys/module/vfio/parameters/enable_unsafe_noiommu_mode + +Upon enabling this mode, with an assigned device, the user will be present= ed +with a VFIO group and device file, e.g. + +/dev/vfio/ +|-- devices +| `-- noiommu-vfio0 /* VFIO device cdev */ +|-- noiommu-0 /* VFIO group */ +`-- vfio + +The capabilities vary depending on the device programming interface and ke= rnel +configuration used. The following table summarizes the differences: + ++-------------------+---------------------+---------------------+ +| Feature | VFIO group | VFIO device cdev | ++=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=3D+ +| VFIO device UAPI | Yes | Yes | ++-------------------+---------------------+---------------------+ +| VFIO container | No | No | ++-------------------+---------------------+---------------------+ +| IOMMUFD IOAS | No | Yes* | ++-------------------+---------------------+---------------------+ +Note that the VFIO container case includes IOMMUFD provided VFIO compatibi= lity +interfaces when either CONFIG_VFIO_CONTAINER or CONFIG_IOMMUFD_VFIO_CONTAI= NER is +enabled. + +* IOMMUFD UAPI is available for VFIO device cdev to pin and map user memor= y with +the ability to retrieve physical addresses for DMA command submission. + +A new IOMMUFD ioctl IOMMU_IOAS_GET_PA is added to retrieve the physical ad= dress +for a given user virtual address. Note that IOMMU_IOAS_MAP_FIXED_IOVA flag= is +ignored in no-IOMMU mode since there is no physical DMA remapping hardware. +tools/testing/selftests/vfio/vfio_iommufd_noiommu_test.c provides an examp= le of +using this ioctl in no-IOMMU mode. + VFIO User API --------------------------------------------------------------------------= ----- =20 --=20 2.34.1