From nobody Sat Jun 13 19:09:48 2026 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 22D6631716D for ; Tue, 5 May 2026 22:15:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778019347; cv=none; b=iLVUm789sOrXuZVPMuGBl6EmmE0mX5AlIRph8/vaQfhuku/ArhftUSUxGayb851OpMXbwciTHii78YbODN0eCOl6GwNpUmzT0AqM02WEe+6Ko7A1z0VNQd17l5y+uP7+ieAlBNpd/zlwJHX5sEQGkPv6nkwejFL8aZV1SZxuZeI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778019347; c=relaxed/simple; bh=M6sqpJW0xvMdEYGbV5sZ5uTFofuz5exwIar0To56mu0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Hqjl44nspgfkzarPqEYm/F6v5S9npg+yc2r6hXHlZCxO3agC4bIa0KW1XHJO7+yfoiJw01gG/PjnT6FRnXPdM7wOEPC1IAxWLgLlTBA6g6OYy0bhnGwYFAvKQwBSMpjogFCBntCaXbn9MNH7rFKokIrYyXwQ7EYpu/8K1fl9oOY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--skhawaja.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=q4f8ovFI; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--skhawaja.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="q4f8ovFI" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-82fa6c3a77cso3611113b3a.3 for ; Tue, 05 May 2026 15:15:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778019345; x=1778624145; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=X2nOvlk7tLe7Q+77TqRRJEoyFcx+9Lc6salUKy253Io=; b=q4f8ovFIWRtSaRAEdVK6nyDZ8106vR0QOoUCFeqMDQnoto8gG/74PTzEOf06qdC191 ZKgCjb1tn80u9vExNV+sCnqXX9vc1h0dCRJ5BPZ1BNt0kn4eBi/UT1/EwcpGLUhE/gZ2 HryrbThpaa6FnJxtvlWsynEHlwOOGBQXUOBTvtrsmbMfiM+aghOHRmwDisJLzIjzcwn7 qcWsxx5smq1G3EbaQEV3kc+3ZoyQeu0rPGd7gHf9EI8/0n/7tzr0kDPF+iCYPM2c/b7y rA4GwdjY5meEyWe8K6mKdldQxC1gzJwVPS5zBSD6LZYLKKVsnDv7R15cLYREr/UcwuKh WW0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778019345; x=1778624145; h=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; bh=X2nOvlk7tLe7Q+77TqRRJEoyFcx+9Lc6salUKy253Io=; b=qRbZ375bfKscHQxaB14VQ/0InDlAsJqfOLvAwC91hBbiRW+pmQ/jqCcII0cT+QA9nO HGhPAKBauI2hoiwtbYvGCV48wL6J0uLaehIogScOwyCgOVwbPJj3fjy7gN5TM4lqyNUO VTja6lFHEzmT2/XFOfTmBGGy3H6wjvwb68qZOrKKuDsJlPYjo4q8YWDo2jHqv326n5QL TdsmyqEOS6UVNWZoDeolbgEwgvvZ33zsN2uazHYZ8xN84rmPBdutlHZAa6MO61YVBSRt 2eBo+q8DPMNkmOVky2w3T1CNyaN5wVaEunoPE39Mb0THsIJsAW/YJiLW67LVcq03Xmpb YMKA== X-Forwarded-Encrypted: i=1; AFNElJ/mnWJfbqGVNT0Lk+iwxg0qWX0qM1fAPyHGv+01/MxVqK3snDOiZtxlMmBuA/ZK56DjFhNGBvjghFV6AuA=@vger.kernel.org X-Gm-Message-State: AOJu0YzXuk8aPYAQpwhRJl9CCYP7Tllw541874gHXmJiYg7rk7ZuxKIL XY3nSqleCAQ0SAwsiINkN9CiijJJ3igvU5AeNo0vzyptIvjowkrEkO5I7T8+foUuJJ0HIDx1B9F bmX3o3DAukermVA== X-Received: from pfoo26.prod.google.com ([2002:a05:6a00:1a1a:b0:82f:b388:7a9e]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2d84:b0:837:7e7d:3d9 with SMTP id d2e1a72fcca58-83a5e137d1dmr577850b3a.44.1778019345245; Tue, 05 May 2026 15:15:45 -0700 (PDT) Date: Tue, 5 May 2026 22:14:35 +0000 In-Reply-To: <20260505221518.619123-1-skhawaja@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260505221518.619123-1-skhawaja@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260505221518.619123-2-skhawaja@google.com> Subject: [PATCH 1/2] vfio: selftests: Add support of creating multiple iommus from iommufd From: Samiullah Khawaja To: David Matlack , Aex Williamson , Shuah Khan Cc: Samiullah Khawaja , Alex Mastro , Raghavendra Rao Ananta , kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" IOMMUFD allows creating multiple IOAS and HWPTs under one iommufd, Add API to init a struct iommu using an already opened iommufd. The API internally creates a new IOAS and also a new HWPT as an option based on the flags passed to the function. Signed-off-by: Samiullah Khawaja --- .../vfio/lib/include/libvfio/iommu.h | 5 ++ .../lib/include/libvfio/vfio_pci_device.h | 2 + tools/testing/selftests/vfio/lib/iommu.c | 62 +++++++++++++++++-- .../selftests/vfio/lib/vfio_pci_device.c | 22 ++++++- 4 files changed, 84 insertions(+), 7 deletions(-) diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/iommu.h b/too= ls/testing/selftests/vfio/lib/include/libvfio/iommu.h index e9a3386a4719..89249a294920 100644 --- a/tools/testing/selftests/vfio/lib/include/libvfio/iommu.h +++ b/tools/testing/selftests/vfio/lib/include/libvfio/iommu.h @@ -9,6 +9,9 @@ =20 typedef u64 iova_t; =20 +/* Create IOMMU with page tables */ +#define IOMMUFD_IOMMU_INIT_CREATE_PT 1 + struct iommu_mode { const char *name; const char *container_path; @@ -29,10 +32,12 @@ struct iommu { int container_fd; int iommufd; u32 ioas_id; + u32 hwpt_id; struct list_head dma_regions; }; =20 struct iommu *iommu_init(const char *iommu_mode); +struct iommu *iommufd_iommu_init(int iommufd, u32 dev_id, u32 flags); void iommu_cleanup(struct iommu *iommu); =20 int __iommu_map(struct iommu *iommu, struct dma_region *region); diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_devi= ce.h b/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h index 2858885a89bb..1143ceb6a9b8 100644 --- a/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h +++ b/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h @@ -19,6 +19,7 @@ struct vfio_pci_device { const char *bdf; int fd; int group_fd; + u32 dev_id; =20 struct iommu *iommu; =20 @@ -65,6 +66,7 @@ void vfio_pci_config_access(struct vfio_pci_device *devic= e, bool write, #define vfio_pci_config_writew(_d, _o, _v) vfio_pci_config_write(_d, _o, _= v, u16) #define vfio_pci_config_writel(_d, _o, _v) vfio_pci_config_write(_d, _o, _= v, u32) =20 +void vfio_pci_device_attach_iommu(struct vfio_pci_device *device, struct i= ommu *iommu); void vfio_pci_irq_enable(struct vfio_pci_device *device, u32 index, u32 vector, int count); void vfio_pci_irq_disable(struct vfio_pci_device *device, u32 index); diff --git a/tools/testing/selftests/vfio/lib/iommu.c b/tools/testing/selft= ests/vfio/lib/iommu.c index 035dac069d60..644c049cf9f0 100644 --- a/tools/testing/selftests/vfio/lib/iommu.c +++ b/tools/testing/selftests/vfio/lib/iommu.c @@ -408,6 +408,18 @@ struct iommu_iova_range *iommu_iova_ranges(struct iomm= u *iommu, u32 *nranges) return ranges; } =20 +static u32 iommufd_hwpt_alloc(struct iommu *iommu, u32 dev_id) +{ + struct iommu_hwpt_alloc args =3D { + .size =3D sizeof(args), + .pt_id =3D iommu->ioas_id, + .dev_id =3D dev_id, + }; + + ioctl_assert(iommu->iommufd, IOMMU_HWPT_ALLOC, &args); + return args.out_hwpt_id; +} + static u32 iommufd_ioas_alloc(int iommufd) { struct iommu_ioas_alloc args =3D { @@ -418,11 +430,9 @@ static u32 iommufd_ioas_alloc(int iommufd) return args.out_ioas_id; } =20 -struct iommu *iommu_init(const char *iommu_mode) +static struct iommu *iommu_alloc(const char *iommu_mode) { - const char *container_path; struct iommu *iommu; - int version; =20 iommu =3D calloc(1, sizeof(*iommu)); VFIO_ASSERT_NOT_NULL(iommu); @@ -430,6 +440,16 @@ struct iommu *iommu_init(const char *iommu_mode) INIT_LIST_HEAD(&iommu->dma_regions); =20 iommu->mode =3D lookup_iommu_mode(iommu_mode); + return iommu; +} + +struct iommu *iommu_init(const char *iommu_mode) +{ + const char *container_path; + struct iommu *iommu; + int version; + + iommu =3D iommu_alloc(iommu_mode); =20 container_path =3D iommu->mode->container_path; if (container_path) { @@ -453,10 +473,44 @@ struct iommu *iommu_init(const char *iommu_mode) return iommu; } =20 +struct iommu *iommufd_iommu_init(int iommufd, u32 dev_id, u32 flags) +{ + struct iommu *iommu; + + iommu =3D iommu_alloc("iommufd"); + + iommu->iommufd =3D dup(iommufd); + VFIO_ASSERT_GT(iommu->iommufd, 0); + + iommu->ioas_id =3D iommufd_ioas_alloc(iommu->iommufd); + + if (flags & IOMMUFD_IOMMU_INIT_CREATE_PT) + iommu->hwpt_id =3D iommufd_hwpt_alloc(iommu, dev_id); + + return iommu; +} + +static void iommufd_cleanup(struct iommu *iommu) +{ + struct iommu_destroy args =3D { + .size =3D sizeof(args), + }; + + if (iommu->hwpt_id) { + args.id =3D iommu->hwpt_id; + ioctl_assert(iommu->iommufd, IOMMU_DESTROY, &args); + } + + args.id =3D iommu->ioas_id; + ioctl_assert(iommu->iommufd, IOMMU_DESTROY, &args); + + VFIO_ASSERT_EQ(close(iommu->iommufd), 0); +} + void iommu_cleanup(struct iommu *iommu) { if (iommu->iommufd) - VFIO_ASSERT_EQ(close(iommu->iommufd), 0); + iommufd_cleanup(iommu); else VFIO_ASSERT_EQ(close(iommu->container_fd), 0); =20 diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index fc75e04ef010..e2b71fe63cae 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -322,7 +322,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf) return cdev_path; } =20 -static void vfio_device_bind_iommufd(int device_fd, int iommufd) +static int vfio_device_bind_iommufd(int device_fd, int iommufd) { struct vfio_device_bind_iommufd args =3D { .argsz =3D sizeof(args), @@ -330,6 +330,7 @@ static void vfio_device_bind_iommufd(int device_fd, int= iommufd) }; =20 ioctl_assert(device_fd, VFIO_DEVICE_BIND_IOMMUFD, &args); + return args.out_devid; } =20 static void vfio_device_attach_iommufd_pt(int device_fd, u32 pt_id) @@ -342,6 +343,21 @@ static void vfio_device_attach_iommufd_pt(int device_f= d, u32 pt_id) ioctl_assert(device_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &args); } =20 +void vfio_pci_device_attach_iommu(struct vfio_pci_device *device, struct i= ommu *iommu) +{ + u32 pt_id =3D iommu->ioas_id; + + /* Only iommufd supports changing struct iommu attachments */ + VFIO_ASSERT_TRUE(iommu->iommufd); + + if (iommu->hwpt_id) + pt_id =3D iommu->hwpt_id; + + VFIO_ASSERT_NE(pt_id, 0); + vfio_device_attach_iommufd_pt(device->fd, pt_id); + device->iommu =3D iommu; +} + static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const c= har *bdf) { const char *cdev_path =3D vfio_pci_get_cdev_path(bdf); @@ -350,8 +366,8 @@ static void vfio_pci_iommufd_setup(struct vfio_pci_devi= ce *device, const char *b VFIO_ASSERT_GE(device->fd, 0); free((void *)cdev_path); =20 - vfio_device_bind_iommufd(device->fd, device->iommu->iommufd); - vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id); + device->dev_id =3D vfio_device_bind_iommufd(device->fd, device->iommu->io= mmufd); + vfio_pci_device_attach_iommu(device, device->iommu); } =20 struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu= *iommu) --=20 2.54.0.545.g6539524ca2-goog From nobody Sat Jun 13 19:09:48 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A48004A2E12 for ; Tue, 5 May 2026 22:15:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778019352; cv=none; b=B+56qbSj7TSa7GLDOqt3Dk7ZgetDmi1QJYDNcF1Ei6LRA42TNuiEU5Q0deQq96M0OTsoCxXvG4k/9G9ak9lnu0nTSdDkxj+i2RSzHQkg0M7dfeHGMBStnER/1FYqgvqKKrvrBSRUhqH81tuxB4fNPG9Le/Zkwrqmxzkm3WRjNqw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778019352; c=relaxed/simple; bh=DQLsDlRg9cK3KamXiSx3uW2lS+tLFTLFL9c5dcS3gjU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ehKA5dv1GKnBAtCs0jVnC9Uzrye2Fvf2UWXx9AJrD7aFD4T44ZI4LfbLiCginBvX+7RezGEw3S1NzJAhzBKqL1FDzzrSex3rDcBC5jW4eQNTBg5aX1qDOVu0Dh5dwJgxILUbWlfVxOXEcE1HUNFTIdQl+Yw0Aah5zaCYo0+MxJI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--skhawaja.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=a/7ZKapQ; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--skhawaja.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="a/7ZKapQ" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-3653638874cso3352345a91.2 for ; Tue, 05 May 2026 15:15:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778019350; x=1778624150; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=aoB9bNzq2kNeo0SI5dGif4n9lwcujzy/IuEbYoKZRSY=; b=a/7ZKapQt/t6sA5tjdJCLfU1mI8aGWJAgwnAsqGFFk4df9QpkhXk1GbNNbi8Zho97q 4i/8LeEV6seulWWyDLmVtDkVNLTmK3N31TPCnLwm2jK3H9hsWPkQRqh+4bckaog7al7E BRnQoWZdwE7P+eIJvlFAQFBnGy2M+QELUAZhChnLiuOhB3dA8ULv1ILv4Kqfll/QsM0k GDuga7x7pyCjmqNFPF129TQXgUx42qZyVeDxi1oACScww+m2qkM7ehW+L6eHwT8lXofU mQcVZAQMLtDMSUBVZ5db++nyDuC/zCj8VklHCHF/7CoG+XEtNOh2prgyFgVs0ZnEuNV+ 7mZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778019350; x=1778624150; h=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; bh=aoB9bNzq2kNeo0SI5dGif4n9lwcujzy/IuEbYoKZRSY=; b=kuB6JxyRsdmxum/F2MSefJLR1OVgNn+c0Pv9fWUFyaVrNbS0B2dRjHCmS4fe/QKwZ9 +ckLvn15TvH0+PhJLmzSuSuebixWElamO7PHCj56BxU7Ysjez7AUrIlwsH/DxvG2JvlL aLDfqjAxxhCsE5ayQ3oyZhNxnW1tIHJ3X/WvDP8sZm8/1qltt2JgrpaxfY2g4IazHy4x 8rjqB3zJh2rzYzd+TFJUUOr/DuQWEDwmnXY4PPZD8YonkmDoiXU/YrrD7XDtdqsRHJvp nJWdKI43zFO3mcf8acdWoFa16twYYR2CU9aICafAIf7dTNCaVkPHOpdtWxTy5xnCOnLQ rcOQ== X-Forwarded-Encrypted: i=1; AFNElJ+dya6VC3SJfQUCGwez8kemakzYr2LYQF8NX6dAwQRHGc01suJzuTPNuquYZdPaC3RrJXQqIDgT8bAq9bk=@vger.kernel.org X-Gm-Message-State: AOJu0YxaSaEtqL3gWKx3KWMVSmF/2BYOsFxgugiAxP+70XRwUHgL2u/Q 2cC94wQM04sdSLiSqiZLARINrfZqc4YSdCY2RIrsORtZFeY1s/AS2e+pGmpk0L/9woKUQ8Oev6x OMTSJUiFQoWnzaA== X-Received: from pldp9.prod.google.com ([2002:a17:902:eac9:b0:2ba:f65:8ab8]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1b0b:b0:2b4:5d51:ce96 with SMTP id d9443c01a7336-2ba794b8457mr7527095ad.24.1778019349876; Tue, 05 May 2026 15:15:49 -0700 (PDT) Date: Tue, 5 May 2026 22:14:36 +0000 In-Reply-To: <20260505221518.619123-1-skhawaja@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260505221518.619123-1-skhawaja@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260505221518.619123-3-skhawaja@google.com> Subject: [PATCH 2/2] vfio: selftests: Add iommufd multi iommu test From: Samiullah Khawaja To: David Matlack , Aex Williamson , Shuah Khan Cc: Samiullah Khawaja , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a test for iommufd to verify creation of multiple iommus using an already setup iommufd and switch between them. Signed-off-by: Samiullah Khawaja --- tools/testing/selftests/vfio/Makefile | 1 + .../vfio/vfio_iommufd_multi_iommu_test.c | 203 ++++++++++++++++++ 2 files changed, 204 insertions(+) create mode 100644 tools/testing/selftests/vfio/vfio_iommufd_multi_iommu_t= est.c diff --git a/tools/testing/selftests/vfio/Makefile b/tools/testing/selftest= s/vfio/Makefile index 0684932d91bf..24bdeaad590f 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -8,6 +8,7 @@ else 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_multi_iommu_test TEST_GEN_PROGS +=3D vfio_iommufd_setup_test TEST_GEN_PROGS +=3D vfio_pci_device_test TEST_GEN_PROGS +=3D vfio_pci_device_init_perf_test diff --git a/tools/testing/selftests/vfio/vfio_iommufd_multi_iommu_test.c b= /tools/testing/selftests/vfio/vfio_iommufd_multi_iommu_test.c new file mode 100644 index 000000000000..7199c662637c --- /dev/null +++ b/tools/testing/selftests/vfio/vfio_iommufd_multi_iommu_test.c @@ -0,0 +1,203 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include + +#include +#include + +#include + +#include "kselftest_harness.h" + +static const char *device_bdf; + +#define IOMMU_DEFAULT 0 +#define IOMMU_WITH_PT 1 +#define IOMMU_WITHOUT_PT 2 + +static void region_setup(struct iommu *iommu, + struct iova_allocator *iova_allocator, + struct dma_region *region, u64 size) +{ + const int flags =3D MAP_SHARED | MAP_ANONYMOUS; + const int prot =3D PROT_READ | PROT_WRITE; + void *vaddr; + + vaddr =3D mmap(NULL, size, prot, flags, -1, 0); + VFIO_ASSERT_NE(vaddr, MAP_FAILED); + + region->vaddr =3D vaddr; + region->iova =3D iova_allocator_alloc(iova_allocator, size); + region->size =3D size; + + iommu_map(iommu, region); +} + +static void region_teardown(struct iommu *iommu, struct dma_region *region) +{ + iommu_unmap(iommu, region); + VFIO_ASSERT_EQ(munmap(region->vaddr, region->size), 0); +} + +FIXTURE(vfio_iommufd_multi_iommu_test) { + struct iommu *iommu; + struct vfio_pci_device *device; + struct iova_allocator *iova_allocator; + struct dma_region memcpy_region; + void *vaddr; + + u64 size; + void *src; + void *dst; + iova_t src_iova; + iova_t dst_iova; +}; + +FIXTURE_SETUP(vfio_iommufd_multi_iommu_test) +{ + struct vfio_pci_driver *driver; + + self->iommu =3D iommu_init("iommufd"); + self->device =3D vfio_pci_device_init(device_bdf, self->iommu); + self->iova_allocator =3D iova_allocator_init(self->iommu); + + driver =3D &self->device->driver; + + region_setup(self->iommu, self->iova_allocator, &self->memcpy_region, SZ_= 1G); + region_setup(self->iommu, self->iova_allocator, &driver->region, SZ_2M); + + if (driver->ops) + vfio_pci_driver_init(self->device); + + self->size =3D self->memcpy_region.size / 2; + self->src =3D self->memcpy_region.vaddr; + self->dst =3D self->src + self->size; + + self->src_iova =3D to_iova(self->device, self->src); + self->dst_iova =3D to_iova(self->device, self->dst); +} + +FIXTURE_TEARDOWN(vfio_iommufd_multi_iommu_test) +{ + struct vfio_pci_driver *driver =3D &self->device->driver; + + if (driver->ops) + vfio_pci_driver_remove(self->device); + + region_teardown(self->iommu, &self->memcpy_region); + region_teardown(self->iommu, &driver->region); + + iova_allocator_cleanup(self->iova_allocator); + vfio_pci_device_cleanup(self->device); + iommu_cleanup(self->iommu); +} + +FIXTURE_VARIANT(vfio_iommufd_multi_iommu_test) { + u32 start_iommu; + u32 end_iommu; +}; + +#define ADD_IOMMU_VARIANT(S, E) \ + FIXTURE_VARIANT_ADD(vfio_iommufd_multi_iommu_test, S##_to_##E) { \ + .start_iommu =3D IOMMU_##S, \ + .end_iommu =3D IOMMU_##E \ + }; + +#define ADD_ALL_VARIANTS(S) \ + ADD_IOMMU_VARIANT(S, DEFAULT) \ + ADD_IOMMU_VARIANT(S, WITH_PT) \ + ADD_IOMMU_VARIANT(S, WITHOUT_PT) + +ADD_ALL_VARIANTS(DEFAULT) +ADD_ALL_VARIANTS(WITH_PT) +ADD_ALL_VARIANTS(WITHOUT_PT) + +static struct iommu *setup_variant_iommu(struct iommu *fixture_iommu, + u32 dev_id, u32 type) +{ + switch (type) { + case IOMMU_WITH_PT: + return iommufd_iommu_init(fixture_iommu->iommufd, dev_id, + IOMMUFD_IOMMU_INIT_CREATE_PT); + case IOMMU_WITHOUT_PT: + return iommufd_iommu_init(fixture_iommu->iommufd, dev_id, 0); + default: + return fixture_iommu; + } +} + +static void test_memcpy(struct vfio_pci_device *device, void *src, void *d= st, + iova_t src_iova, iova_t dst_iova, u64 size, char val) +{ + if (!device->driver.ops) + return; + + memset(src, val, size); + memset(dst, 0, size); + + vfio_pci_driver_memcpy_start(device, src_iova, dst_iova, size, 100); + VFIO_ASSERT_EQ(vfio_pci_driver_memcpy_wait(device), 0); + VFIO_ASSERT_EQ(memcmp(src, dst, size), 0); +} + +TEST_F(vfio_iommufd_multi_iommu_test, memcpy) +{ + struct dma_region memcpy_region1, driver_region1; + struct dma_region memcpy_region2, driver_region2; + struct iommu *iommu1; + struct iommu *iommu2; + + iommu1 =3D setup_variant_iommu(self->iommu, self->device->dev_id, + variant->start_iommu); + if (iommu1 !=3D self->iommu) { + memcpy_region1 =3D self->memcpy_region; + driver_region1 =3D self->device->driver.region; + + iommu_map(iommu1, &memcpy_region1); + iommu_map(iommu1, &driver_region1); + + vfio_pci_device_attach_iommu(self->device, iommu1); + } + + test_memcpy(self->device, self->src, self->dst, self->src_iova, + self->dst_iova, self->size, 'x'); + + iommu2 =3D setup_variant_iommu(self->iommu, self->device->dev_id, + variant->end_iommu); + if (iommu2 !=3D self->iommu) { + memcpy_region2 =3D self->memcpy_region; + driver_region2 =3D self->device->driver.region; + + iommu_map(iommu2, &memcpy_region2); + iommu_map(iommu2, &driver_region2); + } + + vfio_pci_device_attach_iommu(self->device, iommu2); + test_memcpy(self->device, self->src, self->dst, self->src_iova, + self->dst_iova, self->size, 'a'); + + vfio_pci_device_attach_iommu(self->device, iommu1); + test_memcpy(self->device, self->src, self->dst, self->src_iova, + self->dst_iova, self->size, '1'); + + if (iommu1 !=3D self->iommu) { + /* attach back to default iommu for cleanup */ + vfio_pci_device_attach_iommu(self->device, self->iommu); + iommu_unmap(iommu1, &memcpy_region1); + iommu_unmap(iommu1, &driver_region1); + iommu_cleanup(iommu1); + } + + if (iommu2 !=3D self->iommu) { + iommu_unmap(iommu2, &memcpy_region2); + iommu_unmap(iommu2, &driver_region2); + iommu_cleanup(iommu2); + } +} + +int main(int argc, char *argv[]) +{ + device_bdf =3D vfio_selftests_get_bdf(&argc, argv); + + return test_harness_run(argc, argv); +} --=20 2.54.0.545.g6539524ca2-goog