Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 842B024469B for ; Fri, 20 Jun 2025 23:21:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750461671; cv=none; b=b9hZnEGumQg5tNGUpb0iQ5X6Ok4gViaGjIYg+isfBIyupbSUpjhvMNyZiNiwpDTJkIDmmydlDGIvYaE7XUcxoEFa/vnWy9WpNJvzqjNBxjAIlI8QOJOAfim+Oup+6ErfYwJ0GselDgss9i6OOdaJCdkPO5Sa8DLQhO+yyx43HVQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1750461671; c=relaxed/simple; bh=ToqLScmHN3DSsc952/jO8WDXxsKZLo5aLewcAjBE8Wg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=d0JXF5sr6tP2yzKgKb0TXf7Pq1xVFmPG6NJIFntQFSE3L9KoNmwPGu3RCmk5jVgBtJX5gpYVJIVG0Xfbtftzr2Fn7zURd96vUHUaSfgJTB4UTrmgiE6Vyc3riAClXEx9v5YcM6TIrhaB5yqV1p5i3b7fGMIn6+yQi3FFjzDnNaY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=pcLTXwLv; arc=none smtp.client-ip=209.85.216.73 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="pcLTXwLv" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-313ff01d2a6so2158361a91.3 for ; Fri, 20 Jun 2025 16:21:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1750461669; x=1751066469; 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=untHY3DOoNJcKLsu1KM0rh5oWEjId54b1NLV4MrE9gA=; b=pcLTXwLvPGfD+XmJ30fJOWz+OQKNqSKchCVnHlbcvwQaGXzeoZ5gb6ALNRYv1IlUER mlme5TMuWijXBWh/1Cnnw1EfjkpjmryiYZ1xMtZ9xr6ixdTaH4iWdrdjkMGOT50oBcJF xW/08PZSuZwD1JsuH7KKd2zYU81dm//482u4ntEHegEum355ff1tMas++97AQcygAMMB AfCsGxVvhPzOjJKAFTcdA4+wA8Her6HYC/hsL1A6y449GNyFr4P7kHl4wj9qDT3my+b0 zzRv7FFPsoGn1QkNQlAHOukHWOqZdC/P9g8wcDfpyNVvGvn2rnkrO2iX7ttFDq/c9ZHG Yoxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750461669; x=1751066469; 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=untHY3DOoNJcKLsu1KM0rh5oWEjId54b1NLV4MrE9gA=; b=NcVB43UdxlCU07K6E193l4HHekSgGd8qSmfMGNG1q42iYIgTcg5D8Bu1QmJVaZaRtQ 9J3/AdqTj3rhZcXB7bEU+MoFOGe1axcjlpAEiZ7yz9N5DuVUZfC+NvOJpN23T5d7DhLi T7zVUV1PW2kl4l4swMSq9FyW5J0p9p3fTPqy/Gy+iUCB92rpnhbxy/aQcKffPGX7ownd 0lvBi0zy7QatCXMWsb9p9hp9/+VaGMkctA9DzO6L+nc7j23SCgFSvZ58WW5UPsjGOJJL rfBb1T2fU6M7275IVV/tCJdrfdvE64SFUikFj45zi2qVedZsu2Hlww785nnrHv8nzrCx DMjw== X-Forwarded-Encrypted: i=1; AJvYcCUD74cte/YX5+WNQUSNrPrkdSC/rDfzq+CmLfXJTi979BYef1F3YaOYH5Ok5HjCIjJWVHw2zt/t6i0=@vger.kernel.org X-Gm-Message-State: AOJu0YyfCOXtqWJlzII/9JMYyftQ9HlOF+WVJkQ0EJIs6jjNSLpGOcx/ YZFnjXNhWvpCFBg4MRaBnNhJ+O/WLSbijKvNttfoRGTo7V1d+z6Vr+lfrP+N2rKNYa1DQbP7M5n gzp0FrZElEYdoPg== X-Google-Smtp-Source: AGHT+IFqYxIiSKt8a5AmjSXG1MmQ+MktKjJepsYJopwKjRdcTcOUQYW6lDtVHvqeJkwGpqhLO/b2ARAYa3x4xQ== X-Received: from pjbsf12.prod.google.com ([2002:a17:90b:51cc:b0:313:274d:3007]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:dfcb:b0:312:e9bd:5d37 with SMTP id 98e67ed59e1d1-3159d633c74mr6755299a91.6.1750461669072; Fri, 20 Jun 2025 16:21:09 -0700 (PDT) Date: Fri, 20 Jun 2025 23:20:02 +0000 In-Reply-To: <20250620232031.2705638-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: dmaengine@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250620232031.2705638-1-dmatlack@google.com> X-Mailer: git-send-email 2.50.0.rc2.701.gf1e915cc24-goog Message-ID: <20250620232031.2705638-5-dmatlack@google.com> Subject: [PATCH 04/33] vfio: selftests: Test basic VFIO and IOMMUFD integration From: David Matlack To: Alex Williamson Cc: Aaron Lewis , Adhemerval Zanella , Adithya Jayachandran , Andrew Jones , Ard Biesheuvel , Arnaldo Carvalho de Melo , Bibo Mao , Claudio Imbrenda , Dan Williams , Dave Jiang , David Matlack , dmaengine@vger.kernel.org, Huacai Chen , James Houghton , Jason Gunthorpe , Joel Granados , Josh Hilke , Kevin Tian , kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, "Mike Rapoport (Microsoft)" , Paolo Bonzini , Pasha Tatashin , "Pratik R. Sampat" , Saeed Mahameed , Sean Christopherson , Shuah Khan , Vinicius Costa Gomes , Vipin Sharma , Wei Yang , "Yury Norov [NVIDIA]" Content-Type: text/plain; charset="UTF-8" From: Josh Hilke Add a vfio test suite which verifies that userspace can bind and unbind devices, allocate I/O address space, and attach a device to an IOMMU domain using the cdev + IOMMUfd VFIO interface. Signed-off-by: Josh Hilke Signed-off-by: David Matlack --- tools/testing/selftests/vfio/Makefile | 1 + .../selftests/vfio/vfio_iommufd_setup_test.c | 157 ++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 tools/testing/selftests/vfio/vfio_iommufd_setup_test.c diff --git a/tools/testing/selftests/vfio/Makefile b/tools/testing/selftests/vfio/Makefile index 828419537250..e4a5d6eadff3 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -1,4 +1,5 @@ CFLAGS = $(KHDR_INCLUDES) +TEST_GEN_PROGS += vfio_iommufd_setup_test TEST_GEN_PROGS += vfio_pci_device_test include ../lib.mk include lib/libvfio.mk diff --git a/tools/testing/selftests/vfio/vfio_iommufd_setup_test.c b/tools/testing/selftests/vfio/vfio_iommufd_setup_test.c new file mode 100644 index 000000000000..f45335d9260f --- /dev/null +++ b/tools/testing/selftests/vfio/vfio_iommufd_setup_test.c @@ -0,0 +1,157 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include "../kselftest_harness.h" + +static const char iommu_dev_path[] = "/dev/iommu"; +static char cdev_path[PATH_MAX] = { '\0' }; + +static void set_cdev_path(const char *bdf) +{ + char dir_path[PATH_MAX]; + DIR *dir; + struct dirent *entry; + + snprintf(dir_path, sizeof(dir_path), "/sys/bus/pci/devices/%s/vfio-dev/", bdf); + + dir = opendir(dir_path); + assert(dir); + + /* Find the file named "vfio" */ + while ((entry = readdir(dir)) != NULL) { + if (!strncmp("vfio", entry->d_name, 4)) { + snprintf(cdev_path, sizeof(cdev_path), "/dev/vfio/devices/%s", + entry->d_name); + break; + } + } + + assert(strlen(cdev_path) > 0); + + closedir(dir); +} + +static int vfio_device_bind_iommufd_ioctl(int cdev_fd, int iommufd) +{ + struct vfio_device_bind_iommufd bind_args = { + .argsz = sizeof(bind_args), + .iommufd = 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_args = { .argsz = sizeof(info_args) }; + + return ioctl(cdev_fd, VFIO_DEVICE_GET_INFO, &info_args); +} + +static int vfio_device_ioas_alloc_ioctl(int iommufd, struct iommu_ioas_alloc *alloc_args) +{ + *alloc_args = (struct iommu_ioas_alloc){ + .size = sizeof(struct iommu_ioas_alloc), + }; + + 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 = { + .argsz = sizeof(attach_args), + .pt_id = 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 = { + .argsz = sizeof(detach_args), + }; + + return ioctl(cdev_fd, VFIO_DEVICE_DETACH_IOMMUFD_PT, &detach_args); +} + +FIXTURE(vfio_cdev) { + int cdev_fd; + int iommufd; +}; + +FIXTURE_SETUP(vfio_cdev) +{ + ASSERT_LE(0, (self->cdev_fd = open(cdev_path, O_RDWR, 0))); + ASSERT_LE(0, (self->iommufd = open(iommu_dev_path, O_RDWR, 0))); +} + +FIXTURE_TEARDOWN(vfio_cdev) +{ + ASSERT_EQ(0, close(self->cdev_fd)); + ASSERT_EQ(0, close(self->iommufd)); +} + +TEST_F(vfio_cdev, bind) +{ + ASSERT_EQ(0, vfio_device_bind_iommufd_ioctl(self->cdev_fd, self->iommufd)); + ASSERT_EQ(0, vfio_device_get_info_ioctl(self->cdev_fd)); +} + +TEST_F(vfio_cdev, get_info_without_bind_fails) +{ + ASSERT_NE(0, vfio_device_get_info_ioctl(self->cdev_fd)); +} + +TEST_F(vfio_cdev, bind_bad_iommufd_fails) +{ + ASSERT_NE(0, vfio_device_bind_iommufd_ioctl(self->cdev_fd, -2)); +} + +TEST_F(vfio_cdev, 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_F(vfio_cdev, attach_detatch_pt) +{ + 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_F(vfio_cdev, attach_invalid_pt_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)); +} + +int main(int argc, char *argv[]) +{ + const char *device_bdf = vfio_selftests_get_bdf(&argc, argv); + + set_cdev_path(device_bdf); + printf("Using cdev device %s\n", cdev_path); + + return test_harness_run(argc, argv); +} -- 2.50.0.rc2.701.gf1e915cc24-goog