From nobody Wed Apr 8 22:07:15 2026 Received: from mail-oo1-f73.google.com (mail-oo1-f73.google.com [209.85.161.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 D869C3CBE7D for ; Thu, 2 Apr 2026 17:31:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775151067; cv=none; b=tjkGT4i8DXGqFqsSn1/RIwZcz3fHxdyjqIy2e0qsCQCbimZigCIoWzHKW09gg9RmR7alldMi/cGl83pdJJIDidhS+9Ur+ZTK6tVj+Cent7/RlS/KNMJhiRloldYs4lWVKQ9RuOK9hqr6q5WaQVYi0083PLoxC3d2tK+Y+XlZMnA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775151067; c=relaxed/simple; bh=bz1PD9XLinTkJOb+HRe4/8KWqjjiYrFx/Gxzb9xXPdQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jjstZlu8uJURb/tMlIt5CUFFh7H8e4/pE8yDfvtLJddeLFOF12E+AduMm25sBzuMcthLK7yxXXaz9deUgIjPV7PKqk7n82+CAgK9PE9VY2/JsQDRuRBf5Q6N9uXXzJ42Nexdgheau9ZrVjc8Zsrd71EClfVEuDeC5aNnF8AcMdo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--rananta.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=bFOrpoth; arc=none smtp.client-ip=209.85.161.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--rananta.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="bFOrpoth" Received: by mail-oo1-f73.google.com with SMTP id 006d021491bc7-67e43c3c741so1947033eaf.2 for ; Thu, 02 Apr 2026 10:31:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1775151064; x=1775755864; 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=BzrGyK2uc3zUqIxfmzmHZpI3JXkJONbs+6fwqNeAC60=; b=bFOrpothlL6eHu6IsMo3kVQ9CWm2f8YywsQnHdSqpz3ZpiuzxJ5vRHa64MOdftnWYW hhjtPI19wRVreY8g50nrAsVi4PhdVy/MC6xPAgs2Xvg4s3A2t0JwngIJ3PAOCZ2PgZgQ szodvCTpohf629H5MgOIwKkrzYwl83f6tEQRBpve/DMUUVt35uo3dKJGYCg+52rq11hN 53liCYmoFFQTGLxLg+X1Ad0tj6/6+VmI9ZlcUsje7y2KDrwHfiXmm/T022aR29fMS431 c0mQ17VSrzyyg7+mtrkMISMAgFCCEJsiOLtTnyWMwnIVwHziNtsijL48DqNohnvv4rvm +f8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775151064; x=1775755864; 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=BzrGyK2uc3zUqIxfmzmHZpI3JXkJONbs+6fwqNeAC60=; b=Nz/QktRO0ULBnpEh3z8HV3ffYXO3XxGo/vgiG/tjAxBVuYLmgrHDzXWJkw6bkbjv3L Mn6N5pVF2XMc7jIkEJci7jtB1lUWv6od8llhgsTdIoni3jx0sRSR8euiMSazpRETBTgu Q/8SsY8PU3g5qzY27gsuRPIHqA0+V6erWjUPo11TO5BVO9awTbqmFHE0qdyqLwBr8hbK POW+3CE2DxsAKZwiSaJMaMe6F69fOr7c9/ZBmYFcEjkeiOH9qTPBpT/2Hh869i3x90Iy zXp/+sQ9GRd+6lV8ZKPDzaOobBCbkdikxCe0TLFWzquOS7HGhgdXuFGaOBZEkzMP4prA 6VLw== X-Forwarded-Encrypted: i=1; AJvYcCWj2e0XxBO05iwW10Ydnz5i4YDhZPg+cOl/ne+bxhiDqElOzocTC3vTAk20q2owwLk7KjOUAVoESQuNQJ8=@vger.kernel.org X-Gm-Message-State: AOJu0Yw6L/AfMrtMWPdtmOKv64lBXOUUJ4j86CAXTJ7zmzjfKM3ynhFu J5Op9xsUaL48iMl7voxX08lF3CZnGSFAiYukt7CFffLe3iVlviWT4Y0PU2B2LYLYFu3Vju06dJj Znfs+JaztPA== X-Received: from jabkl12.prod.google.com ([2002:a05:6638:a98c:b0:5d6:7669:6fe4]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:a08:b0:67e:16b4:aa1a with SMTP id 006d021491bc7-67fabd0842cmr4453106eaf.54.1775151063588; Thu, 02 Apr 2026 10:31:03 -0700 (PDT) Date: Thu, 2 Apr 2026 17:30:52 +0000 In-Reply-To: <20260402173059.1018805-1-rananta@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260402173059.1018805-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.1213.gd9a14994de-goog Message-ID: <20260402173059.1018805-2-rananta@google.com> Subject: [PATCH v7 1/8] vfio: selftests: Add -Wall and -Werror to the Makefile From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson Cc: Vipin Sharma , Josh Hilke , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Raghavendra Rao Ananta Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add the compiler flags, -Wall and -Werror, to catch all the build warnings and flag them as a build error, respectively. This is to ensure that no obvious programmer errors are introduced. We can add -Wno-* flags in the future to ignore specific warnings as necesasry. Signed-off-by: Raghavendra Rao Ananta Reviewed-by: David Matlack --- tools/testing/selftests/vfio/Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/testing/selftests/vfio/Makefile b/tools/testing/selftest= s/vfio/Makefile index 8e90e409e91d8..6a9ac6dd32cb6 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -23,6 +23,7 @@ include lib/libvfio.mk =20 CFLAGS +=3D -I$(top_srcdir)/tools/include CFLAGS +=3D -MD +CFLAGS +=3D -Wall -Werror CFLAGS +=3D $(EXTRA_CFLAGS) =20 LDFLAGS +=3D -pthread --=20 2.53.0.1213.gd9a14994de-goog From nobody Wed Apr 8 22:07:15 2026 Received: from mail-oo1-f74.google.com (mail-oo1-f74.google.com [209.85.161.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 890C831079B for ; Thu, 2 Apr 2026 17:31:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775151068; cv=none; b=GSfV3P4egHgvDL1TFlk0DnYcI5s/ZXQhRSF1xdOpR+TR4yLtrl7ZckzGHsoR9KeljlbNGa/tm6mKJRYxtEBhoXcTyKWdwrPL4D3ogBzxXd6FTtXTZR4pD2E4rqASw0x4c5IVqfa0Mn1dJWkDH8qWGkDoTadbawDIFFik5wyvIow= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775151068; c=relaxed/simple; bh=8HVoR5cd52fPXzFWtfK66AFBeWL0bKkn7rRvDwCk50g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=dIPGBBH7W2s/hsidPvLS6xXvZfiU5YiZJhuwQW3XpJIqd0toMboo+DxgYpGhK9d71bG2pTjUBi7/dCf2s4QqtYSUDWabQ9zAOkPDBu/BDNlZLd7iBYBXXX159yWqpj1iUbrT8nnsTSq+MdDgL9TdEk+OtqKjPUEMkfWWkKAL9cM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--rananta.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=tPjsUJQ8; arc=none smtp.client-ip=209.85.161.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--rananta.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="tPjsUJQ8" Received: by mail-oo1-f74.google.com with SMTP id 006d021491bc7-67dfa1ab0b1so3839815eaf.0 for ; Thu, 02 Apr 2026 10:31:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1775151064; x=1775755864; 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=sIFGgzex0DVn9nBXSWjs3Yu5GHhJ8YfSqgclQSiXhzQ=; b=tPjsUJQ8cWfOOrGvVD4R+gEZTX+AF/IY3GhiwwaiOuzcDOuGXi554llf3Mxiw1LdjS 0JDcZ+FdQY9AojVCjMOd4SJ8jetCUkaYOB3yfBnJYpBeR8vjz3Pc5HqNEofIIZIW6jbF qFCnnAgyw3uTFul6bOSuKuRcf+Jyd3rW7wdZ6fZBy+QLcrVQo5nL3Y7gwSpgIYLt7Omf HCPk5gDcXI+SqzM/izdUMpz5U7sCBk7WTOiLsI8U03LilOJ8FkKNhC0s2/Ej9VVamnX6 LgPXYnh/9jDt4Q0mx9/omVfPBvc9WhuwRtwuRWJEknSvje2K5PXqZnuke6yNuxY5y2um NbGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775151064; x=1775755864; 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=sIFGgzex0DVn9nBXSWjs3Yu5GHhJ8YfSqgclQSiXhzQ=; b=ekAZTRv8PBQvDGLakHWF9jms1hhhG01B9hXm/Va4gQcq8BoT/cMdPAkXEiAbag1ivf Yf7URhEnXKdBP5cdfdWG45W0mwcJ+16mqBbSWpDxCXSJYk2yJSCa0lwCCYnn1XOW34lo Wuwjz60rnXSyd2vrkE5nvn+o7J1/ZMw80y9z9XDz95yr2drhUFOwDZ18h4KMdLTn70Zq cmUM1IcBUQ7qsIK1dugiBhHps3BIDYGyMiFGgFWxkiYGqTGw67q31e3/Ys+k75GM+Ls8 X6Z0eiLFzs25yFs0qrjlGs3ufmB3FEczzLWHaxzEsgy+fo0yne0defvRvmQl1k3rm/O6 5aDg== X-Forwarded-Encrypted: i=1; AJvYcCUQHmcOUHdi4ES01aFJviHPumZc+IjKbbWDxOXGXXE5/h1/oCpRwIHAgruX+5wXTDvkDGyA6c7j97/lxHk=@vger.kernel.org X-Gm-Message-State: AOJu0YyLbtsQH0Vpb4F26T50wIr7qJGsqO8l4sW8bFDBb3IcAZwP7qFK 6NIWIsNLHmm1AnczKOkpvrTsaa4RZmyzawBwGwam5Ekru+rdDYAex6GbMkwQvb8dlFcrLFx41zg kIYcV9hRi+w== X-Received: from jaan28-n2.prod.google.com ([2002:a05:6638:aadc:20b0:5d6:9ff5:de3a]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:c8d:b0:67b:decf:eef3 with SMTP id 006d021491bc7-6821f57e22emr11803eaf.37.1775151064489; Thu, 02 Apr 2026 10:31:04 -0700 (PDT) Date: Thu, 2 Apr 2026 17:30:53 +0000 In-Reply-To: <20260402173059.1018805-1-rananta@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260402173059.1018805-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.1213.gd9a14994de-goog Message-ID: <20260402173059.1018805-3-rananta@google.com> Subject: [PATCH v7 2/8] vfio: selftests: Introduce snprintf_assert() From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson Cc: Vipin Sharma , Josh Hilke , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Raghavendra Rao Ananta Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce snprintf_assert() to protect the users of snprintf() to fail if the requested operation was truncated due to buffer limits. VFIO tests and libraries, including a new sysfs library that will be introduced by an upcoming patch, rely quite heavily on snprintf()s to build PCI sysfs paths. Having a protection against this will be helpful to prevent false test failures. Signed-off-by: Raghavendra Rao Ananta Reviewed-by: David Matlack --- .../vfio/lib/include/libvfio/assert.h | 5 +++++ .../selftests/vfio/lib/vfio_pci_device.c | 8 +++---- .../selftests/vfio/vfio_dma_mapping_test.c | 6 +++--- .../selftests/vfio/vfio_pci_device_test.c | 21 ++++++++++--------- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/assert.h b/to= ols/testing/selftests/vfio/lib/include/libvfio/assert.h index f4ebd122d9b6f..77b68c7129a64 100644 --- a/tools/testing/selftests/vfio/lib/include/libvfio/assert.h +++ b/tools/testing/selftests/vfio/lib/include/libvfio/assert.h @@ -51,4 +51,9 @@ VFIO_ASSERT_EQ(__ret, 0, "ioctl(%s, %s, %s) returned %d\n", #_fd, #_op, #= _arg, __ret); \ } while (0) =20 +#define snprintf_assert(_s, _size, _fmt, ...) do { \ + int __ret =3D snprintf(_s, _size, _fmt, ##__VA_ARGS__); \ + VFIO_ASSERT_LT(__ret, _size); \ +} while (0) + #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_ASSERT_H */ diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index 4e5871f1ebc3b..1538d2b30ae59 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -209,7 +209,7 @@ static unsigned int vfio_pci_get_group_from_dev(const c= har *bdf) unsigned int group; int ret; =20 - snprintf(sysfs_path, PATH_MAX, "%s/%s/iommu_group", PCI_SYSFS_PATH, bdf); + snprintf_assert(sysfs_path, PATH_MAX, "%s/%s/iommu_group", PCI_SYSFS_PATH= , bdf); =20 ret =3D readlink(sysfs_path, dev_iommu_group_path, sizeof(dev_iommu_group= _path)); VFIO_ASSERT_NE(ret, -1, "Failed to get the IOMMU group for device: %s\n",= bdf); @@ -229,7 +229,7 @@ static void vfio_pci_group_setup(struct vfio_pci_device= *device, const char *bdf int group; =20 group =3D vfio_pci_get_group_from_dev(bdf); - snprintf(group_path, sizeof(group_path), "/dev/vfio/%d", group); + snprintf_assert(group_path, sizeof(group_path), "/dev/vfio/%d", group); =20 device->group_fd =3D open(group_path, O_RDWR); VFIO_ASSERT_GE(device->group_fd, 0, "open(%s) failed\n", group_path); @@ -300,7 +300,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf) cdev_path =3D calloc(PATH_MAX, 1); VFIO_ASSERT_NOT_NULL(cdev_path); =20 - snprintf(dir_path, sizeof(dir_path), "/sys/bus/pci/devices/%s/vfio-dev/",= bdf); + snprintf_assert(dir_path, sizeof(dir_path), "/sys/bus/pci/devices/%s/vfio= -dev/", bdf); =20 dir =3D opendir(dir_path); VFIO_ASSERT_NOT_NULL(dir, "Failed to open directory %s\n", dir_path); @@ -310,7 +310,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf) if (strncmp("vfio", entry->d_name, 4)) continue; =20 - snprintf(cdev_path, PATH_MAX, "/dev/vfio/devices/%s", entry->d_name); + snprintf_assert(cdev_path, PATH_MAX, "/dev/vfio/devices/%s", entry->d_na= me); break; } =20 diff --git a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c b/tools/t= esting/selftests/vfio/vfio_dma_mapping_test.c index abb170bdcef7e..7d0de8c79de13 100644 --- a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c +++ b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c @@ -44,9 +44,9 @@ static int intel_iommu_mapping_get(const char *bdf, u64 i= ova, FILE *file; char *rest; =20 - snprintf(iommu_mapping_path, sizeof(iommu_mapping_path), - "/sys/kernel/debug/iommu/intel/%s/domain_translation_struct", - bdf); + snprintf_assert(iommu_mapping_path, sizeof(iommu_mapping_path), + "/sys/kernel/debug/iommu/intel/%s/domain_translation_struct", + bdf); =20 printf("Searching for IOVA 0x%lx in %s\n", iova, iommu_mapping_path); =20 diff --git a/tools/testing/selftests/vfio/vfio_pci_device_test.c b/tools/te= sting/selftests/vfio/vfio_pci_device_test.c index 7c0fe8ce3a61f..93c11fd5e0818 100644 --- a/tools/testing/selftests/vfio/vfio_pci_device_test.c +++ b/tools/testing/selftests/vfio/vfio_pci_device_test.c @@ -39,16 +39,17 @@ FIXTURE_TEARDOWN(vfio_pci_device_test) iommu_cleanup(self->iommu); } =20 -#define read_pci_id_from_sysfs(_file) ({ \ - char __sysfs_path[PATH_MAX]; \ - char __buf[32]; \ - int __fd; \ - \ - snprintf(__sysfs_path, PATH_MAX, "/sys/bus/pci/devices/%s/%s", device_bdf= , _file); \ - ASSERT_GT((__fd =3D open(__sysfs_path, O_RDONLY)), 0); \ - ASSERT_GT(read(__fd, __buf, ARRAY_SIZE(__buf)), 0); \ - ASSERT_EQ(0, close(__fd)); \ - (u16)strtoul(__buf, NULL, 0); \ +#define read_pci_id_from_sysfs(_file) ({ \ + char __sysfs_path[PATH_MAX]; \ + char __buf[32]; \ + int __fd; \ + \ + snprintf_assert(__sysfs_path, PATH_MAX, "/sys/bus/pci/devices/%s/%s", \ + device_bdf, _file); \ + ASSERT_GT((__fd =3D open(__sysfs_path, O_RDONLY)), 0); \ + ASSERT_GT(read(__fd, __buf, ARRAY_SIZE(__buf)), 0); \ + ASSERT_EQ(0, close(__fd)); \ + (u16)strtoul(__buf, NULL, 0); \ }) =20 TEST_F(vfio_pci_device_test, config_space_read_write) --=20 2.53.0.1213.gd9a14994de-goog From nobody Wed Apr 8 22:07:15 2026 Received: from mail-oa1-f74.google.com (mail-oa1-f74.google.com [209.85.160.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 9AB7D3EF658 for ; Thu, 2 Apr 2026 17:31:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775151069; cv=none; b=uUU49iWin+vMePfqRCAEfxq/C0NLOJi2dRd1qac291/787gEkQ6A5q3NXlk10tCg3WkMc/Az8KPFUFVi7UFY4YrEw/IsrltQ1e6abqDyWYM+vTAAn+0TeqNW1X4hu4v3kDsk/s4StZgCg9VZOeClD4pXOePZXfD4u+tz/XKnIHI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775151069; c=relaxed/simple; bh=xOg2XQUdu61sT+2G91062zQbfARrtgq/PaA7i+bKeuY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=b1IneiRD8IVd8I7eoF7xwnUu/dXdNk3zb1vBfN5n0hhlElZUXfZW+UCnjX+AxAjtlHlC4muOb7g2iYV8nBkE+b0kPdK27fIbMs0nBda/L56O7wViudlxuSE5F4sYlW3HOk7pHpT0RR9aDBrZs/Gp69rH+FqVwX1+y3fP/nyvyOw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--rananta.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Y5Z/vksU; arc=none smtp.client-ip=209.85.160.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--rananta.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Y5Z/vksU" Received: by mail-oa1-f74.google.com with SMTP id 586e51a60fabf-41702ea8343so1705477fac.0 for ; Thu, 02 Apr 2026 10:31:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1775151066; x=1775755866; 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=r9hf8Fj5Eo8Pp1WM7FCKHr9aIVKr8I/VqWCHYJtcPZw=; b=Y5Z/vksUXfv9DLOadWDa0bYRQ63kONl+JGufFCqz829/zTEflbQP8GV0TaNIh+xbBd X7xorOPQcSrT8wQQ6y2/qBjZgXn6LyFhvzALmLX49jkxRyVElRk7WrgSQOOBfUceYkLt 9/FZ2tXN5RhEYKHykDJmx4dWAO3UzxWhngD6WJfILVq4doVUe36xA1Wqpdj0ndTd06Tv B6j+LSY0nJSZnrB6EB8nJCrwiaZma0dFebke8HYnxR3zmadihbjfZ9P6SP8kml4mmwBF XIZjlbI6dCSMZ07Fszj0t6oZ6EK+WP7dp+TbKoMiMhrX7RzikT2xF/bG1sdelH06ymQd lUqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775151066; x=1775755866; 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=r9hf8Fj5Eo8Pp1WM7FCKHr9aIVKr8I/VqWCHYJtcPZw=; b=Gqm0n+1TYb+dSwzZ2D4cDK+SGLTLQcOyaPlXm1vpQA+O63Zp7baFp3mDtIznqjsQmQ oU4dLz4Jju5I32EBCSE+KYO86SsYoN1r0trseQXfVGxZA4fJYgFwRSabm6BYDHkrW327 bDIo0QN1QiG8sNSCs1DLpJStSHZj112uKpJdAc72R19B7FFqrZpInIeArUAZ0LNKG4No O/Zp687FR4IPtsgbZHLpPbk/3tVafI6f4Gd6HhqlkYcKMUgjQw3OBnVA657Bc8WLBYXb SDEHrF0n/wBcTLUtwj0b6VtgGTJnjkEMMMq3VoBuaYU5vuIDbKzD9JTxV1VnV6faKcaJ AcwQ== X-Forwarded-Encrypted: i=1; AJvYcCUxBkGUjsA4cRTNVeNHKf2UsPl+dKKrEqM4lVBxGn1axNyovUKlK1aVBxEr0EbU5g+RcqS2VuKd30t7nfw=@vger.kernel.org X-Gm-Message-State: AOJu0YxUHHbnK24IznZwELqXHyFKVcDcLpoDoj8B04Cek2fqAWhyc0cV c5Xyhk4vQXzgh1HyGkFDLIMY5RuGikAPn8J1/sMk8z4ASMdhy3Hvsog376WSMiayEJnqCTdrt7R Wjd0tTX6gyw== X-Received: from jatx12.prod.google.com ([2002:a05:6638:248c:b0:5d6:6409:5c6]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:3092:b0:67e:2d12:c1ef with SMTP id 006d021491bc7-6822034eaecmr7830eaf.54.1775151065309; Thu, 02 Apr 2026 10:31:05 -0700 (PDT) Date: Thu, 2 Apr 2026 17:30:54 +0000 In-Reply-To: <20260402173059.1018805-1-rananta@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260402173059.1018805-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.1213.gd9a14994de-goog Message-ID: <20260402173059.1018805-4-rananta@google.com> Subject: [PATCH v7 3/8] vfio: selftests: Introduce a sysfs lib From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson Cc: Vipin Sharma , Josh Hilke , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Raghavendra Rao Ananta Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce a sysfs library to handle the common reads/writes to the PCI sysfs files, for example, getting the total number of VFs supported by the device via /sys/bus/pci/devices/$BDF/sriov_totalvfs. The library will be used in the upcoming test patch to configure the VFs for a given PF device. Since readlink() is quite commonly used in the lib, introduce and use readlink_safe() to take care of potential buffer overrun errors and to safely terminate the buffer with '\0'. Opportunistically, move vfio_pci_get_group_from_dev() to this library as it falls under the same bucket. Rename it to sysfs_iommu_group_get() to align with other function names. Signed-off-by: Raghavendra Rao Ananta Reviewed-by: David Matlack --- .../selftests/vfio/lib/include/libvfio.h | 1 + .../vfio/lib/include/libvfio/sysfs.h | 12 ++ tools/testing/selftests/vfio/lib/libvfio.mk | 1 + tools/testing/selftests/vfio/lib/sysfs.c | 150 ++++++++++++++++++ .../selftests/vfio/lib/vfio_pci_device.c | 22 +-- 5 files changed, 165 insertions(+), 21 deletions(-) create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/sysfs.h create mode 100644 tools/testing/selftests/vfio/lib/sysfs.c diff --git a/tools/testing/selftests/vfio/lib/include/libvfio.h b/tools/tes= ting/selftests/vfio/lib/include/libvfio.h index 1b6da54cc2cb7..07862b470777b 100644 --- a/tools/testing/selftests/vfio/lib/include/libvfio.h +++ b/tools/testing/selftests/vfio/lib/include/libvfio.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include =20 diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/sysfs.h b/too= ls/testing/selftests/vfio/lib/include/libvfio/sysfs.h new file mode 100644 index 0000000000000..c1d945ea62a10 --- /dev/null +++ b/tools/testing/selftests/vfio/lib/include/libvfio/sysfs.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_SYSFS_H +#define SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_SYSFS_H + +int sysfs_sriov_totalvfs_get(const char *bdf); +int sysfs_sriov_numvfs_get(const char *bdf); +void sysfs_sriov_numvfs_set(const char *bdfs, int numvfs); +char *sysfs_sriov_vf_bdf_get(const char *pf_bdf, int i); +int sysfs_iommu_group_get(const char *bdf); +char *sysfs_driver_get(const char *bdf); + +#endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_SYSFS_H */ diff --git a/tools/testing/selftests/vfio/lib/libvfio.mk b/tools/testing/se= lftests/vfio/lib/libvfio.mk index 9f47bceed16f4..b7857319c3f1f 100644 --- a/tools/testing/selftests/vfio/lib/libvfio.mk +++ b/tools/testing/selftests/vfio/lib/libvfio.mk @@ -6,6 +6,7 @@ LIBVFIO_SRCDIR :=3D $(selfdir)/vfio/lib LIBVFIO_C :=3D iommu.c LIBVFIO_C +=3D iova_allocator.c LIBVFIO_C +=3D libvfio.c +LIBVFIO_C +=3D sysfs.c LIBVFIO_C +=3D vfio_pci_device.c LIBVFIO_C +=3D vfio_pci_driver.c =20 diff --git a/tools/testing/selftests/vfio/lib/sysfs.c b/tools/testing/selft= ests/vfio/lib/sysfs.c new file mode 100644 index 0000000000000..f1e6889556527 --- /dev/null +++ b/tools/testing/selftests/vfio/lib/sysfs.c @@ -0,0 +1,150 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include + +#include + +/* Enough to hold MODULE_NAME_LEN */ +#define DRIVER_NAME_SZ 64 + +#define readlink_safe(_path, _buf) ({ \ + int __ret =3D readlink(_path, _buf, sizeof(_buf) - 1); \ + if (__ret !=3D -1) \ + _buf[__ret] =3D 0; \ + __ret; \ +}) + +static void readlink_base(const char *path, const char *data_fmt, void *ou= t_data) +{ + char rl_path[PATH_MAX]; + int ret; + + ret =3D readlink_safe(path, rl_path); + VFIO_ASSERT_NE(ret, -1); + + ret =3D sscanf(basename(rl_path), data_fmt, out_data); + VFIO_ASSERT_EQ(ret, 1); +} + +static int sysfs_val_get_int(const char *component, const char *name, + const char *file) +{ + char path[PATH_MAX]; + char buf[32]; + int ret; + int fd; + + snprintf_assert(path, PATH_MAX, "/sys/bus/pci/%s/%s/%s", component, name,= file); + fd =3D open(path, O_RDONLY); + if (fd < 0) + return fd; + + VFIO_ASSERT_GT(read(fd, buf, ARRAY_SIZE(buf)), 0); + VFIO_ASSERT_EQ(close(fd), 0); + + errno =3D 0; + ret =3D strtol(buf, NULL, 0); + VFIO_ASSERT_EQ(errno, 0, "sysfs path \"%s\" is not an integer: \"%s\"\n",= path, buf); + + return ret; +} + +static void sysfs_val_set(const char *component, const char *name, + const char *file, const char *val) +{ + char path[PATH_MAX]; + int fd; + + snprintf_assert(path, PATH_MAX, "/sys/bus/pci/%s/%s/%s", component, name,= file); + VFIO_ASSERT_GT(fd =3D open(path, O_WRONLY), 0); + + VFIO_ASSERT_EQ(write(fd, val, strlen(val)), strlen(val)); + VFIO_ASSERT_EQ(close(fd), 0); +} + +static int sysfs_device_val_get(const char *bdf, const char *file) +{ + return sysfs_val_get_int("devices", bdf, file); +} + +static void sysfs_device_val_set(const char *bdf, const char *file, const = char *val) +{ + sysfs_val_set("devices", bdf, file, val); +} + +static void sysfs_device_val_set_int(const char *bdf, const char *file, in= t val) +{ + char val_str[32]; + + snprintf_assert(val_str, sizeof(val_str), "%d", val); + sysfs_device_val_set(bdf, file, val_str); +} + +int sysfs_sriov_totalvfs_get(const char *bdf) +{ + return sysfs_device_val_get(bdf, "sriov_totalvfs"); +} + +int sysfs_sriov_numvfs_get(const char *bdf) +{ + return sysfs_device_val_get(bdf, "sriov_numvfs"); +} + +void sysfs_sriov_numvfs_set(const char *bdf, int numvfs) +{ + sysfs_device_val_set_int(bdf, "sriov_numvfs", numvfs); +} + +char *sysfs_sriov_vf_bdf_get(const char *pf_bdf, int i) +{ + char path[PATH_MAX]; + char *out_vf_bdf; + + /* Fit "0000:00:00.0" */ + out_vf_bdf =3D calloc(16, sizeof(char)); + VFIO_ASSERT_NOT_NULL(out_vf_bdf); + + snprintf_assert(path, PATH_MAX, "/sys/bus/pci/devices/%s/virtfn%d", pf_bd= f, i); + readlink_base(path, "%s", out_vf_bdf); + + return out_vf_bdf; +} + +int sysfs_iommu_group_get(const char *bdf) +{ + char path[PATH_MAX]; + int group; + + snprintf_assert(path, PATH_MAX, "/sys/bus/pci/devices/%s/iommu_group", bd= f); + readlink_base(path, "%d", &group); + + return group; +} + +char *sysfs_driver_get(const char *bdf) +{ + char driver_path[PATH_MAX]; + char path[PATH_MAX]; + char *out_driver; + int ret; + + out_driver =3D calloc(DRIVER_NAME_SZ, sizeof(char)); + VFIO_ASSERT_NOT_NULL(out_driver); + + snprintf_assert(path, PATH_MAX, "/sys/bus/pci/devices/%s/driver", bdf); + ret =3D readlink_safe(path, driver_path); + if (ret =3D=3D -1) { + free(out_driver); + + if (errno =3D=3D ENOENT) + return NULL; + + VFIO_FAIL("Failed to read %s\n", path); + } + + strncpy(out_driver, basename(driver_path), DRIVER_NAME_SZ - 1); + return out_driver; +} diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index 1538d2b30ae59..82f255f0486dc 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -25,8 +25,6 @@ #include "kselftest.h" #include =20 -#define PCI_SYSFS_PATH "/sys/bus/pci/devices" - static void vfio_pci_irq_set(struct vfio_pci_device *device, u32 index, u32 vector, u32 count, int *fds) { @@ -202,24 +200,6 @@ void vfio_pci_device_reset(struct vfio_pci_device *dev= ice) ioctl_assert(device->fd, VFIO_DEVICE_RESET, NULL); } =20 -static unsigned int vfio_pci_get_group_from_dev(const char *bdf) -{ - char dev_iommu_group_path[PATH_MAX] =3D {0}; - char sysfs_path[PATH_MAX] =3D {0}; - unsigned int group; - int ret; - - snprintf_assert(sysfs_path, PATH_MAX, "%s/%s/iommu_group", PCI_SYSFS_PATH= , bdf); - - ret =3D readlink(sysfs_path, dev_iommu_group_path, sizeof(dev_iommu_group= _path)); - VFIO_ASSERT_NE(ret, -1, "Failed to get the IOMMU group for device: %s\n",= bdf); - - ret =3D sscanf(basename(dev_iommu_group_path), "%u", &group); - VFIO_ASSERT_EQ(ret, 1, "Failed to get the IOMMU group for device: %s\n", = bdf); - - return group; -} - static void vfio_pci_group_setup(struct vfio_pci_device *device, const cha= r *bdf) { struct vfio_group_status group_status =3D { @@ -228,7 +208,7 @@ static void vfio_pci_group_setup(struct vfio_pci_device= *device, const char *bdf char group_path[32]; int group; =20 - group =3D vfio_pci_get_group_from_dev(bdf); + group =3D sysfs_iommu_group_get(bdf); snprintf_assert(group_path, sizeof(group_path), "/dev/vfio/%d", group); =20 device->group_fd =3D open(group_path, O_RDWR); --=20 2.53.0.1213.gd9a14994de-goog From nobody Wed Apr 8 22:07:15 2026 Received: from mail-oa1-f73.google.com (mail-oa1-f73.google.com [209.85.160.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 4E0263F0AA2 for ; Thu, 2 Apr 2026 17:31:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775151070; cv=none; b=gTzo/oDYatAgTwmWis9b1DIj7qEMcDq0K3Me+0T39nAgpqPZqtWYq2ICZWY2wkCRcq0Esz8c6kH2e8C8Qioh+M5NOB09txg31MN5rhz5sPph3uR9nYlQpDsuM2osJ1SwS5fYU1ccLgkBl7qSuFeENgHDpcOER/irnOTJppmOQn4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775151070; c=relaxed/simple; bh=aioNoLlDzw5Q8eJ+PJale07yZdVO/sraFRI5eymlHV0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XIz9tTZp3LUjNN1t7QZGNp4l5KtJFyyzzfhprLzYfTwRigimsXEWFOpD89znAb/PjGeMiRfJfQNhVgJ9ll/C6mem1Ux4/hol4zG+2638fDa5OG+Bgexn+SfUCzhUQ2u0DEauK9gIPHUDMVvzU8AZgsAfhy52ZFlH966KdZocV/Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--rananta.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=pFJh5Hjs; arc=none smtp.client-ip=209.85.160.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--rananta.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="pFJh5Hjs" Received: by mail-oa1-f73.google.com with SMTP id 586e51a60fabf-40450320b4fso1828164fac.0 for ; Thu, 02 Apr 2026 10:31:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1775151067; x=1775755867; 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=LjG0Gd3MypaxsBilbokFGxAV+Zt5lu/q3yDnocf8EBM=; b=pFJh5HjsTimr1bWreO1wTldCTRrcdYbOd9pD1xzhINYvkcXcaVmMLD9EMUP1iOdsy2 UuzAKV1bBnP1QEjxwKN1SSkDncjg4iinBPPm5I/5YBqvG2gvDjkytjPtPmBmzHXwMXCJ hFVdTKdMHTqhf2DaFdk34aPmdIViK0ywshOx7nf5A9fvSOt5GY5FZVfbV+j2JeyIgUoi k5QbZ1X5/2rar7vpNTe6puNGmy5VdZiIvUKcZl0SgjxhvsSEb/WH8rIi+RSYiHLDkuxW +m5LbJhUTADuE/Ec3ej5woRsFMBMzoOJp29J5WuF/LkbCiPCl7VH7afmS509gONp7++5 7+kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775151067; x=1775755867; 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=LjG0Gd3MypaxsBilbokFGxAV+Zt5lu/q3yDnocf8EBM=; b=Cl4NPtyIXcZYZxg63wBhXWMzFfJE2OE6STdBAame4aoOUD+92gSOBdl5XuhBh6LDYY i77t4oeQm4u2JK/0+mMv1gEDAew2rCpvRuclq50b/aTG6r8OY+PYzGDQJjN7OBjWJbJg Du0yc8f7NLP+Q62yYMKl2ldZAK9or8Va1eVHo2k1fHj7LYmKbbMeFtU/vaOZrQ6AQcGq Re4SLchhQdoN66DdApuA5UsR6cjJpCkKBtpKfNutyHBZDTDTU/69RadKuYGPdEyfNF7N iQEzH1DZBJQ3DQaqYYphQyxUacJdjTSyMU/o6aYNoQbQm2UQh4y9P195Xmvqh3x1ZoJg 3Tzw== X-Forwarded-Encrypted: i=1; AJvYcCUlPuoVaqZEKKoYlDxQAY62vDn1EtISg9p/7eIScqOR39McqHewKPGm1gJ7GwhUK51FVM7o5RdctXxFdpQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxoaEbHcsbU4Sb/4UzfgMM4WGZDAbD1ELfZEfYgLCfVjBLcXc13 TgDkb17uQ02LFJ5eW0AVAp8ItHCqhoPVMv2VRj/lgxUZJCf4wCJQB00sYlONY2kZfazPUj7CpWQ f/3HWVvNiVg== X-Received: from jakz8.prod.google.com ([2002:a05:6638:3188:b0:5d6:5cd9:76c7]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:1614:b0:67e:2c8:c0c0 with SMTP id 006d021491bc7-680a8548985mr2283825eaf.58.1775151067129; Thu, 02 Apr 2026 10:31:07 -0700 (PDT) Date: Thu, 2 Apr 2026 17:30:55 +0000 In-Reply-To: <20260402173059.1018805-1-rananta@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260402173059.1018805-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.1213.gd9a14994de-goog Message-ID: <20260402173059.1018805-5-rananta@google.com> Subject: [PATCH v7 4/8] vfio: selftests: Extend container/iommufd setup for passing vf_token From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson Cc: Vipin Sharma , Josh Hilke , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Raghavendra Rao Ananta Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" A UUID is normally set as a vf_token to correspond the VFs with the PFs, if they are both bound by the vfio-pci driver. This is true for iommufd-based approach and container-based approach. The token can be set either during device creation (VFIO_GROUP_GET_DEVICE_FD) in container-based approach or during iommu bind (VFIO_DEVICE_BIND_IOMMUFD) in the iommu-fd case. Hence extend the functions, vfio_pci_iommufd_setup() and vfio_pci_container_setup(), to accept vf_token as an (optional) argument and handle the necessary setup. No functional changes are expected. Signed-off-by: Raghavendra Rao Ananta Reviewed-by: David Matlack --- tools/testing/selftests/vfio/Makefile | 2 + .../selftests/vfio/lib/vfio_pci_device.c | 46 +++++++++++++++---- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/vfio/Makefile b/tools/testing/selftest= s/vfio/Makefile index 6a9ac6dd32cb6..f27ed18070f14 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -28,6 +28,8 @@ CFLAGS +=3D $(EXTRA_CFLAGS) =20 LDFLAGS +=3D -pthread =20 +LDLIBS +=3D -luuid + $(TEST_GEN_PROGS): %: %.o $(LIBVFIO_O) $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $< $(LIBVFIO_O) $(LDLIBS) -o $@ =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 82f255f0486dc..dc8b37df8d1f1 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -22,6 +22,8 @@ #include #include =20 +#include + #include "kselftest.h" #include =20 @@ -220,7 +222,27 @@ static void vfio_pci_group_setup(struct vfio_pci_devic= e *device, const char *bdf ioctl_assert(device->group_fd, VFIO_GROUP_SET_CONTAINER, &device->iommu->= container_fd); } =20 -static void vfio_pci_container_setup(struct vfio_pci_device *device, const= char *bdf) +static void vfio_pci_group_get_device_fd(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) +{ + char arg[64]; + + /* + * If a vf_token exists, argument to VFIO_GROUP_GET_DEVICE_FD + * will be in the form of the following example: + * "0000:04:10.0 vf_token=3Dbd8d9d2b-5a5f-4f5a-a211-f591514ba1f3" + */ + if (vf_token) + snprintf_assert(arg, ARRAY_SIZE(arg), "%s vf_token=3D%s", bdf, vf_token); + else + snprintf_assert(arg, ARRAY_SIZE(arg), "%s", bdf); + + device->fd =3D ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, arg); + VFIO_ASSERT_GE(device->fd, 0); +} + +static void vfio_pci_container_setup(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) { struct iommu *iommu =3D device->iommu; unsigned long iommu_type =3D iommu->mode->iommu_type; @@ -238,8 +260,7 @@ static void vfio_pci_container_setup(struct vfio_pci_de= vice *device, const char */ (void)ioctl(iommu->container_fd, VFIO_SET_IOMMU, (void *)iommu_type); =20 - device->fd =3D ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, bdf); - VFIO_ASSERT_GE(device->fd, 0); + vfio_pci_group_get_device_fd(device, bdf, vf_token); } =20 static void vfio_pci_device_setup(struct vfio_pci_device *device) @@ -300,12 +321,20 @@ 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 void vfio_device_bind_iommufd(int device_fd, int iommufd, + const char *vf_token) { struct vfio_device_bind_iommufd args =3D { .argsz =3D sizeof(args), .iommufd =3D iommufd, }; + uuid_t token_uuid; + + if (vf_token) { + VFIO_ASSERT_EQ(uuid_parse(vf_token, token_uuid), 0); + args.flags |=3D VFIO_DEVICE_BIND_FLAG_TOKEN; + args.token_uuid_ptr =3D (u64)token_uuid; + } =20 ioctl_assert(device_fd, VFIO_DEVICE_BIND_IOMMUFD, &args); } @@ -320,7 +349,8 @@ static void vfio_device_attach_iommufd_pt(int device_fd= , u32 pt_id) ioctl_assert(device_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &args); } =20 -static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const c= har *bdf) +static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) { const char *cdev_path =3D vfio_pci_get_cdev_path(bdf); =20 @@ -328,7 +358,7 @@ 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_bind_iommufd(device->fd, device->iommu->iommufd, vf_token); vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id); } =20 @@ -344,9 +374,9 @@ struct vfio_pci_device *vfio_pci_device_init(const char= *bdf, struct iommu *iomm device->bdf =3D bdf; =20 if (iommu->mode->container_path) - vfio_pci_container_setup(device, bdf); + vfio_pci_container_setup(device, bdf, NULL); else - vfio_pci_iommufd_setup(device, bdf); + vfio_pci_iommufd_setup(device, bdf, NULL); =20 vfio_pci_device_setup(device); vfio_pci_driver_probe(device); --=20 2.53.0.1213.gd9a14994de-goog From nobody Wed Apr 8 22:07:15 2026 Received: from mail-oa1-f74.google.com (mail-oa1-f74.google.com [209.85.160.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 A437A3F23B6 for ; Thu, 2 Apr 2026 17:31:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775151071; cv=none; b=XKa4jzyuupSuW109YWxjFa/ELoZW/5si8FCR2FZuMbaBGCUDmTbcWbGWn2VTzZWOWXJ/Jhwaouv/1IirODtdfTAEFEdz+u+5/Aj6NJiltaMQWhJSZPd/l5JwfmxqsduEVyDl7jKDa47G41bbdmfjH5pMYtU5Bktu7E8jFvgXFec= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775151071; c=relaxed/simple; bh=5kAapb6H3jt5z27uMf4lcYsqLp7SZXEPQi7pHGIiIrA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Jts8KoVViff2K5T8RajLX+PdAPo7ESvRWSxjA4oEbq786VA4LS3ktjdX6jwCX7c4g762WyUzjN27uzzz//13IYnB9FI+u5+sXS7T/cjBGizpnW91mHYcgf0LNiNU3MPh253ByUPCyLMCO3ouaoC44sHPeh+yKDnyy2nvs9A8PDk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--rananta.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=rrZOkrFO; arc=none smtp.client-ip=209.85.160.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--rananta.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="rrZOkrFO" Received: by mail-oa1-f74.google.com with SMTP id 586e51a60fabf-41702ea8343so1705575fac.0 for ; Thu, 02 Apr 2026 10:31:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1775151068; x=1775755868; 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=HXv7N1eCd4/DFCswAatT4Z+rDzWVRHHu2CK7LpYrXaw=; b=rrZOkrFOIj6lUdEasflt3dMvsE4Sq4Xaai+pFZvoIXJS2xcsCOByimIFElDs0B/9tG uIRygW5ZmkwiQ4QgVHpVa4yxQ9RsjQTiDrz+CRUkO17E4ZCggBmCF1ShoPcQHtrf29GM MdEYmrHhbn0DPlkREMbi2kSIG1h9dksujDLc8W62MHkR7uewLi+qZ0Zn1BA5kPNUe7UD mfzz3ZWx39ELG2B6YLHr5YeAw8ZF8ylZfO79ht87BB/DEaXr+h9vw4oxTB2OnHu5vv2Q he5HgALURYNvUeo+sVyFVmzRk4nFUNrw5LRe/D03/DCoaN21EFOgOoZks4wWVHsOIVzN 5e4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775151068; x=1775755868; 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=HXv7N1eCd4/DFCswAatT4Z+rDzWVRHHu2CK7LpYrXaw=; b=SwoQmvB46KUQbQy8WZfZwMFdxoA358cFVgYSaHPl6welZ1qXuNXopLP5N5XHNhGRR9 iUJ/u5+3+4XyGDRbOWVb54rDP+MlsTpC+5Rl9vndDZp+VeOz1/83x4+qXubSPoowTeZH ekOkvrCiHgneRJ/WEWAU43FU4qFeTR0YlGdAJY4W2MJ7f6aDQ4+RJXXOVllnoHveHiEw vgZbxQ7UUwncoY3NFwZazkwflm2KL0ozgjLwEJlLPMNUizf3UTnMj9XlY2Ouljb33wUN HRDW8MCgjv5Fn8RHzHmamBNd39AP0SuK0H3oH2e8jtBGyM2t12KJbfiUACRXpj2uNDOm 1HyQ== X-Forwarded-Encrypted: i=1; AJvYcCU/zRpaqx44IyUcl6WWvv9ygu+mSXB/dfL+DPsmwkZ3G1dFcawz5jEFmBGQyfeP20/mN2Y7B3yYx4Z2Rns=@vger.kernel.org X-Gm-Message-State: AOJu0YxLTKxDq9UoAmQShCBvzpACyFh/7PQ10pPaDBy7dDo2h5wmqTGu fIAnwHXNVxoFj07hcb0cozcHApYIEh2WgiDbanT5bob6mouhDuwZwGEfxIi3Cw8mjuoA95iF0Xy w/cQqVWTPgw== X-Received: from jabko12.prod.google.com ([2002:a05:6638:8f0c:b0:5d3:9f1b:b8a0]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a4a:e911:0:b0:681:a608:5e99 with SMTP id 006d021491bc7-6821d23824emr36944eaf.11.1775151068318; Thu, 02 Apr 2026 10:31:08 -0700 (PDT) Date: Thu, 2 Apr 2026 17:30:56 +0000 In-Reply-To: <20260402173059.1018805-1-rananta@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260402173059.1018805-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.1213.gd9a14994de-goog Message-ID: <20260402173059.1018805-6-rananta@google.com> Subject: [PATCH v7 5/8] vfio: selftests: Expose more vfio_pci_device functions From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson Cc: Vipin Sharma , Josh Hilke , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Raghavendra Rao Ananta Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Refactor and make the functions called under device initialization public. A later patch adds a test that calls these functions to validate the UAPI of SR-IOV devices. Opportunistically, to test the success and failure cases of the UAPI, split the functions dealing with VFIO_GROUP_GET_DEVICE_FD and VFIO_DEVICE_BIND_IOMMUFD into a core function and another one that asserts the ioctl. The former will be used for testing the SR-IOV UAPI, hence only export these. Signed-off-by: Raghavendra Rao Ananta Reviewed-by: David Matlack --- .../lib/include/libvfio/vfio_pci_device.h | 7 +++ .../selftests/vfio/lib/vfio_pci_device.c | 47 ++++++++++++++----- 2 files changed, 42 insertions(+), 12 deletions(-) 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 2858885a89bbb..898de032fed5a 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 @@ -122,4 +122,11 @@ static inline bool vfio_pci_device_match(struct vfio_p= ci_device *device, =20 const char *vfio_pci_get_cdev_path(const char *bdf); =20 +void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf); +void __vfio_pci_group_get_device_fd(struct vfio_pci_device *device, + const char *bdf, const char *vf_token); +void vfio_container_set_iommu(struct vfio_pci_device *device); +void vfio_pci_cdev_open(struct vfio_pci_device *device, const char *bdf); +int __vfio_device_bind_iommufd(int device_fd, int iommufd, const char *vf_= token); + #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_VFIO_PCI_DEVICE_H */ diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index dc8b37df8d1f1..3123ba591f088 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -202,7 +202,7 @@ void vfio_pci_device_reset(struct vfio_pci_device *devi= ce) ioctl_assert(device->fd, VFIO_DEVICE_RESET, NULL); } =20 -static void vfio_pci_group_setup(struct vfio_pci_device *device, const cha= r *bdf) +void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf) { struct vfio_group_status group_status =3D { .argsz =3D sizeof(group_status), @@ -222,8 +222,8 @@ static void vfio_pci_group_setup(struct vfio_pci_device= *device, const char *bdf ioctl_assert(device->group_fd, VFIO_GROUP_SET_CONTAINER, &device->iommu->= container_fd); } =20 -static void vfio_pci_group_get_device_fd(struct vfio_pci_device *device, - const char *bdf, const char *vf_token) +void __vfio_pci_group_get_device_fd(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) { char arg[64]; =20 @@ -238,18 +238,21 @@ static void vfio_pci_group_get_device_fd(struct vfio_= pci_device *device, snprintf_assert(arg, ARRAY_SIZE(arg), "%s", bdf); =20 device->fd =3D ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, arg); +} + +static void vfio_pci_group_get_device_fd(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) +{ + __vfio_pci_group_get_device_fd(device, bdf, vf_token); VFIO_ASSERT_GE(device->fd, 0); } =20 -static void vfio_pci_container_setup(struct vfio_pci_device *device, - const char *bdf, const char *vf_token) +void vfio_container_set_iommu(struct vfio_pci_device *device) { struct iommu *iommu =3D device->iommu; unsigned long iommu_type =3D iommu->mode->iommu_type; int ret; =20 - vfio_pci_group_setup(device, bdf); - ret =3D ioctl(iommu->container_fd, VFIO_CHECK_EXTENSION, iommu_type); VFIO_ASSERT_GT(ret, 0, "VFIO IOMMU type %lu not supported\n", iommu_type); =20 @@ -259,7 +262,13 @@ static void vfio_pci_container_setup(struct vfio_pci_d= evice *device, * because the IOMMU type is already set. */ (void)ioctl(iommu->container_fd, VFIO_SET_IOMMU, (void *)iommu_type); +} =20 +static void vfio_pci_container_setup(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) +{ + vfio_pci_group_setup(device, bdf); + vfio_container_set_iommu(device); vfio_pci_group_get_device_fd(device, bdf, vf_token); } =20 @@ -321,8 +330,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, - const char *vf_token) +int __vfio_device_bind_iommufd(int device_fd, int iommufd, const char *vf_= token) { struct vfio_device_bind_iommufd args =3D { .argsz =3D sizeof(args), @@ -336,7 +344,18 @@ static void vfio_device_bind_iommufd(int device_fd, in= t iommufd, args.token_uuid_ptr =3D (u64)token_uuid; } =20 - ioctl_assert(device_fd, VFIO_DEVICE_BIND_IOMMUFD, &args); + if (ioctl(device_fd, VFIO_DEVICE_BIND_IOMMUFD, &args)) + return -errno; + + return 0; +} + +static void vfio_device_bind_iommufd(int device_fd, int iommufd, + const char *vf_token) +{ + int ret =3D __vfio_device_bind_iommufd(device_fd, iommufd, vf_token); + + VFIO_ASSERT_EQ(ret, 0, "Failed VFIO_DEVICE_BIND_IOMMUFD ioctl\n"); } =20 static void vfio_device_attach_iommufd_pt(int device_fd, u32 pt_id) @@ -349,15 +368,19 @@ static void vfio_device_attach_iommufd_pt(int device_= fd, u32 pt_id) ioctl_assert(device_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &args); } =20 -static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, - const char *bdf, const char *vf_token) +void vfio_pci_cdev_open(struct vfio_pci_device *device, const char *bdf) { const char *cdev_path =3D vfio_pci_get_cdev_path(bdf); =20 device->fd =3D open(cdev_path, O_RDWR); VFIO_ASSERT_GE(device->fd, 0); free((void *)cdev_path); +} =20 +static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) +{ + vfio_pci_cdev_open(device, bdf); vfio_device_bind_iommufd(device->fd, device->iommu->iommufd, vf_token); vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id); } --=20 2.53.0.1213.gd9a14994de-goog From nobody Wed Apr 8 22:07:15 2026 Received: from mail-oo1-f74.google.com (mail-oo1-f74.google.com [209.85.161.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 7E2FD3E1237 for ; Thu, 2 Apr 2026 17:31:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775151071; cv=none; b=b3JB2E+xCyiPteq6D3TUlYFR4h2URTSN4n3qM2m4oOjiVZ/puI17zuFgTxFg7kzzXBJAH40/sC79L4u0SNqRWuW19ov2u+zlzb4dNlLPXSZW1FyKw7+f8BdluQM3KqjgtiFRTEG7PwFEQ4I8LBf6fLfk6P17LHyhgfzxWc1qOFs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775151071; c=relaxed/simple; bh=fKGrqOe7/gcHbceYdJgmC+BFNKB4nT/qhL7SRADHx9U=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=K9RTIK+9eeh9K41lMYwkbbTCwuCFwpE1TJ6oCHHC3v4qEziUNC08UVcPWr5R3ff99xRwUufeI9wOs3TAy7CLKumwWhZDNvIk31d/6GqwDSb9ez+4mMq26rg+8Y8nlIirldgmAuB19AS4Ded9MsWEuzbYS/6laJdcT3TGiXZG2HM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--rananta.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=GlvIcSWu; arc=none smtp.client-ip=209.85.161.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--rananta.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GlvIcSWu" Received: by mail-oo1-f74.google.com with SMTP id 006d021491bc7-66b612efb4aso2390239eaf.0 for ; Thu, 02 Apr 2026 10:31:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1775151069; x=1775755869; 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=Qy6Cj7fcucn9v9YQ8Oo1NV9Ds993VMVKePqjlbtzguo=; b=GlvIcSWuQ/1C8PYWVB2sSlBR+++fr/TbMKnexsum7NfMaYyJzzkKlPDR0g277Gl4ES r7A5OX4ZA5jiYC8FEvKHuvYmYX0BAbdBRFFquOJX1sGe/loLnRzGmeST+MfjD+Ie6ShM JJlUPUNICvF8XbNZN5zxLB5vEUdbslr+/JXobz3kOwxc442rmz5mf8oWra/NjArR38U3 ZG6rUeb8XFy/E5caDZjFJkajZoAc61sZiDwK94/EGYMMdrrqltLvU0YRc4/W5SuHDLFp b57T/poCSj3EE9Z3p2/r/1ii31Xsho/+ztjqiofPtOWr3H2BoDny15hdE+SnT8w75XLb n3bQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775151069; x=1775755869; 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=Qy6Cj7fcucn9v9YQ8Oo1NV9Ds993VMVKePqjlbtzguo=; b=FRvN1vqon7GTbQKP1ndK1s9yCw4AyT1X24KrYoKWypKI/gHkJE4xdyMGIQAtUjY4/P S+4Opr1VDWF7gScv4vOHKEYx24JDJ4PX3bmXEmqqayza6EjuaUvip9+FyuaAsdC+MhMO 30qD21/xbgNOOJ/kkw26Vpue/UmuRJ9qgZ9x6h75c22B1Hg5GYYpifRKlq76TinZWlAe okg39b3Orzckw8Zdxno0pvAqqi+Cg/7kwG6rwgLsHblRVZP39Fv34O9pR53/b920nngd oqjJUAZtf3IWstUuZ/lq7z5t+C3AVdvUPk4mseRKcwdEJvwi1i+1riiaRDor5OZxhct6 VekQ== X-Forwarded-Encrypted: i=1; AJvYcCXdxO40IgLwZdBjiGUxiWaas0tI7BbsLDcQMi4h7z8hnyYylAxR6TolxYpbTL3RZFyCaoJXxnAEtHBhgcw=@vger.kernel.org X-Gm-Message-State: AOJu0YzcBScbRQz/UOkgCxsnTASq0QDtPDmvUzt84/vSP8oKzIMywy0H lC88en/z8UVVNPy8QkgfljTLFaCmQhSNOYPvqEGm0RUltlACUm7+8tmw2ch+EWB8qbnw7J6luOz +ZCkbVt2IsA== X-Received: from ilnp16.prod.google.com ([2002:a92:d690:0:b0:4f8:9187:e599]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:a089:b0:682:1c53:89fc with SMTP id 006d021491bc7-6821c538eb5mr29264eaf.8.1775151069353; Thu, 02 Apr 2026 10:31:09 -0700 (PDT) Date: Thu, 2 Apr 2026 17:30:57 +0000 In-Reply-To: <20260402173059.1018805-1-rananta@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260402173059.1018805-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.1213.gd9a14994de-goog Message-ID: <20260402173059.1018805-7-rananta@google.com> Subject: [PATCH v7 6/8] vfio: selftests: Add helper to set/override a vf_token From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson Cc: Vipin Sharma , Josh Hilke , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Raghavendra Rao Ananta Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a helper function, vfio_device_set_vf_token(), to set or override a vf_token. Not only at init, but a vf_token can also be set via the VFIO_DEVICE_FEATURE ioctl, by setting the VFIO_DEVICE_FEATURE_PCI_VF_TOKEN flag. Hence, add an API to utilize this functionality from the test code. The subsequent commit will use this to test the functionality of this method to set the vf_token. Signed-off-by: Raghavendra Rao Ananta Reviewed-by: David Matlack --- .../lib/include/libvfio/vfio_pci_device.h | 2 ++ .../selftests/vfio/lib/vfio_pci_device.c | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+) 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 898de032fed5a..4ebdc00e20fca 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 @@ -129,4 +129,6 @@ void vfio_container_set_iommu(struct vfio_pci_device *d= evice); void vfio_pci_cdev_open(struct vfio_pci_device *device, const char *bdf); int __vfio_device_bind_iommufd(int device_fd, int iommufd, const char *vf_= token); =20 +void vfio_device_set_vf_token(int fd, const char *vf_token); + #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_VFIO_PCI_DEVICE_H */ diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index 3123ba591f088..4673b148f8c44 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -113,6 +113,40 @@ static void vfio_pci_irq_get(struct vfio_pci_device *d= evice, u32 index, ioctl_assert(device->fd, VFIO_DEVICE_GET_IRQ_INFO, irq_info); } =20 +static int vfio_device_feature_ioctl(int fd, u32 flags, void *data, + size_t data_size) +{ + u8 buffer[sizeof(struct vfio_device_feature) + data_size] =3D {}; + struct vfio_device_feature *feature =3D (void *)buffer; + + memcpy(feature->data, data, data_size); + + feature->argsz =3D sizeof(buffer); + feature->flags =3D flags; + + return ioctl(fd, VFIO_DEVICE_FEATURE, feature); +} + +static void vfio_device_feature_set(int fd, u16 feature, void *data, size_= t data_size) +{ + u32 flags =3D VFIO_DEVICE_FEATURE_SET | feature; + int ret; + + ret =3D vfio_device_feature_ioctl(fd, flags, data, data_size); + VFIO_ASSERT_EQ(ret, 0, "Failed to set feature %u\n", feature); +} + +void vfio_device_set_vf_token(int fd, const char *vf_token) +{ + uuid_t token_uuid =3D {0}; + + VFIO_ASSERT_NOT_NULL(vf_token, "vf_token is NULL"); + VFIO_ASSERT_EQ(uuid_parse(vf_token, token_uuid), 0); + + vfio_device_feature_set(fd, VFIO_DEVICE_FEATURE_PCI_VF_TOKEN, + token_uuid, sizeof(uuid_t)); +} + static void vfio_pci_region_get(struct vfio_pci_device *device, int index, struct vfio_region_info *info) { --=20 2.53.0.1213.gd9a14994de-goog From nobody Wed Apr 8 22:07:15 2026 Received: from mail-oi1-f202.google.com (mail-oi1-f202.google.com [209.85.167.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 82CD53F7A90 for ; Thu, 2 Apr 2026 17:31:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775151072; cv=none; b=GZK2+PPY7LQObpbTkmG+JLexM/B9AGgleZ8juaG6K5hrKM4PeWIN4bHJtysw6T6LD/6+/ozq2hO64Iqc+T7//oZG8kdDUazp7h3iJ3X+hB3XedB/qQu/cO7IaODb0EFI8XzzGp+MOMCdgIkj2SttMFRjZwg4Phq8nO71MSuwDIo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775151072; c=relaxed/simple; bh=JvspclGG//MbGhKd05tl7x7zTJzEXGzkxs42ikNGdSg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=F3RkS1FsiIiy1RoTqF6WAQambW26MgjFY7G6Y2348qBmZjR4QLavJu2wQJir/yMXrCxF2dZC+tUsosyAlvOl6pok/G2cft7etAj88v7pVAeeKzUIYa7NoZwYIVgxVAuHzqGKNtRtnHYPw8ZgDj6P545p39nMVj/AnIbrnVJGZrQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--rananta.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=pVkeId73; arc=none smtp.client-ip=209.85.167.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--rananta.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="pVkeId73" Received: by mail-oi1-f202.google.com with SMTP id 5614622812f47-466ef11c4baso2361156b6e.1 for ; Thu, 02 Apr 2026 10:31:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1775151070; x=1775755870; 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=B0ib/Tb14sQxCq5zPTYDnrbZcYfKDUfA5Bwx4aPAdsY=; b=pVkeId733giV/Nw5L7Dr7mSYkf44MQZNPkRhy62qN9uFF49rjDop/hwp34eWjQn88j +pi0/GQVbDa8GJBCtbnw3hAKTEJxELtMk2TU95K+X8d/s+K82OFSN44jXgNIOOAb1zI1 6uAbzn5vOrAXn3KCu8YXdWRik5WPuhouZvh9u96EGrMiLoPqKkCFZ/ISi1MhUkx2Iic8 AIpVhOLKQv3MJILFGfd4/g61JumfJVcLw0WfcjKaSQleLCBpTaqhjKWPCrWSM3kh/MkC y4q5ymI1el2w1NZUmVuEz5jM4I+Avp+6Ioe9MrMooDQHLTpLe1gJfqhF4aq5ST0nSRMa Y2lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775151070; x=1775755870; 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=B0ib/Tb14sQxCq5zPTYDnrbZcYfKDUfA5Bwx4aPAdsY=; b=H1qLXUE1XL4/7PcdmLavIBUYJL9MhAaaKp7Cw681C1JqtDy5fIAqSiE5F8ZySFVqaV /dv4W62yObjX/dPBpRidWJOpstrQzE2NmBbFVOqi4C3OUdzffKsKFwcossg6PGrb8pd6 gyUqAGpiU47JEIxN5+BN7XQhadF36nYcgAtEGD9Ol2MifZYvbEAz+z4imYrF+sCsZDOZ 0KKzC0e66vaV8bdAFUewIYgV562nSZycf+WuASCnbQJd/XpJKmFfDX3ruFbL7OjqVKrp fOv65pO2VjMUR4Iln2UzOPwaNjhovOzyniPxPWk70lYRrx5ngYXYfQfwOKCA6RkZeQxC 8Kjg== X-Forwarded-Encrypted: i=1; AJvYcCXWK+sbnZ3JKsCxqXequhWin67dudfpw7NXzEgA0TAFKnU6ShpYlxg+2i9NnrRtj/eKbUy3n3aFl3xd/eI=@vger.kernel.org X-Gm-Message-State: AOJu0Yxl42jO5msaVt22dV/VyoJeXFOgHKph2rGEQkqqZImaSc11EMeS mugWyvv08HXV6TRhXAzZhA7BXDwQG7QsXPDCusUKJseZ9cJy0Xk466nj99Z3bVfFCWAmKL62RQV rGZmyiB3zLg== X-Received: from ilqu13.prod.google.com ([2002:a92:cccd:0:b0:4f8:aea6:eab7]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a54:4e82:0:b0:464:82c5:567c with SMTP id 5614622812f47-46ef8314beemr88833b6e.41.1775151070223; Thu, 02 Apr 2026 10:31:10 -0700 (PDT) Date: Thu, 2 Apr 2026 17:30:58 +0000 In-Reply-To: <20260402173059.1018805-1-rananta@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260402173059.1018805-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.1213.gd9a14994de-goog Message-ID: <20260402173059.1018805-8-rananta@google.com> Subject: [PATCH v7 7/8] vfio: selftests: Add helpers to alloc/free vfio_pci_device From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson Cc: Vipin Sharma , Josh Hilke , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Raghavendra Rao Ananta Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a helper, vfio_pci_device_alloc(), to allocate 'struct vfio_pci_device'. The subsequent test patch will utilize this to get the struct with very minimal initialization done. Internally, let vfio_pci_device_init() also make use of this function and later do the full initialization. Symmetrically, add a free variant, vfio_pci_device_free(), to be used in a similar fashion. No functional change intended. Signed-off-by: Raghavendra Rao Ananta Reviewed-by: David Matlack --- .../vfio/lib/include/libvfio/vfio_pci_device.h | 2 ++ .../selftests/vfio/lib/vfio_pci_device.c | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) 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 4ebdc00e20fca..3eabead717bbd 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 @@ -38,6 +38,8 @@ struct vfio_pci_device { #define dev_info(_dev, _fmt, ...) printf("%s: " _fmt, (_dev)->bdf, ##__VA_= ARGS__) #define dev_err(_dev, _fmt, ...) fprintf(stderr, "%s: " _fmt, (_dev)->bdf,= ##__VA_ARGS__) =20 +struct vfio_pci_device *vfio_pci_device_alloc(const char *bdf, struct iomm= u *iommu); +void vfio_pci_device_free(struct vfio_pci_device *device); struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu= *iommu); void vfio_pci_device_cleanup(struct vfio_pci_device *device); =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 4673b148f8c44..4ff76970e3791 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -419,7 +419,7 @@ static void vfio_pci_iommufd_setup(struct vfio_pci_devi= ce *device, vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id); } =20 -struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu= *iommu) +struct vfio_pci_device *vfio_pci_device_alloc(const char *bdf, struct iomm= u *iommu) { struct vfio_pci_device *device; =20 @@ -430,6 +430,20 @@ struct vfio_pci_device *vfio_pci_device_init(const cha= r *bdf, struct iommu *iomm device->iommu =3D iommu; device->bdf =3D bdf; =20 + return device; +} + +void vfio_pci_device_free(struct vfio_pci_device *device) +{ + free(device); +} + +struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu= *iommu) +{ + struct vfio_pci_device *device; + + device =3D vfio_pci_device_alloc(bdf, iommu); + if (iommu->mode->container_path) vfio_pci_container_setup(device, bdf, NULL); else @@ -462,5 +476,5 @@ void vfio_pci_device_cleanup(struct vfio_pci_device *de= vice) if (device->group_fd) VFIO_ASSERT_EQ(close(device->group_fd), 0); =20 - free(device); + vfio_pci_device_free(device); } --=20 2.53.0.1213.gd9a14994de-goog From nobody Wed Apr 8 22:07:15 2026 Received: from mail-oo1-f73.google.com (mail-oo1-f73.google.com [209.85.161.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 663573F7E7A for ; Thu, 2 Apr 2026 17:31:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775151074; cv=none; b=KrVqvgeISBWIhGMdxy740NZqLOnCUzjS8fcduoclhXQaPHuhmfi/D4JusEXs3F8D+8iem9TbmeT8dJx5QztYJXlQKOPDJ+IguXCpk+wmDUL+gicQjr1z1cF3NG76o0rblELtdAUwk2wC3Ujt6yaVedUJodUWBX5qsm3j0HkVq2g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775151074; c=relaxed/simple; bh=wBT3SOqrN7GgpQlXXyPj9EpUQ0dPzwTBiblJ7id3wLU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bFOW3mM2UKw0F5HyeS8kZtmvrXa5Pv8D6MOX/5pHCuWkkIqlxYgfBy7OgFxhd8lgT5ky1WP0kMHh40MNwmTuMqcIhqvsDqiY7naNubALX/PFcJlb2Js84XR+hRuO+wTrj/8SgTYw5j/02MFSATi5WajLsHuPSyji+aXRijxM4e4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--rananta.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=A4uc9WK/; arc=none smtp.client-ip=209.85.161.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--rananta.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="A4uc9WK/" Received: by mail-oo1-f73.google.com with SMTP id 006d021491bc7-67dfa1ab0b1so3840101eaf.0 for ; Thu, 02 Apr 2026 10:31:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1775151071; x=1775755871; 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=BsAK2c6NlREYTGsIPFCjENwVQZJrHsunFqwEtFuFdLA=; b=A4uc9WK/hfDwcJqgEZ9FCWOYqXmweySETHoR//SbxO7DI2du9AHeVqE7GBFCgAiNNQ mLnoJSUngif0LOnGnM6qfG5qJSExoDmRhr+eMqMwHTSPIDymye0mExnL84tFTVrA45wT zNHuCMny9oQkmTAS7gFa8TfmLH+/TD380STCIPgqh17XbC+zz7a1MZytegUaA0JQevxe Gu1QKy5qQmI6X3WQDjqV3DHwH5E1eORar6/r+X6a6cFWziWxgKgLxVOhkvvdgq1oKMBL 70ctCK019EAyS9x2To+EVeP+o5m+ytknAQgfI8TLasoYjIrWN4LWysfB3HoKEiMKzRAu 1Pmw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775151071; x=1775755871; 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=BsAK2c6NlREYTGsIPFCjENwVQZJrHsunFqwEtFuFdLA=; b=Zq+rpLNpdNLtvvzrw//2nzvCTDz9EX9+1Dox1SKqdWeIYEWVo/tZF6VDUAPM+1Gj2s ldRXx2ndrswBJuEBWjbAH2dpLXoFT2xKyEZ9c4dDnXprH0aDyS32bZny9KZDqwFJptyi xJ0V816d03p6/TXuTo4PhtpESKNxLKDPYMmXvTU7Z0Le6rRQUPmFZWu4MLkRdocHjgJw 5+dhoRrJxLXdr7s214/19YmacBDIypsQPMxyZG8TYb+4X7qigH9At3oQMi4Hm6JlZa4t YuSvmLy6BaMwLjDemUJ32oZKyYGx7sZ/xeOYH0JIAME1ahHGZtInmjysJ/NHcEIF3b8K xPag== X-Forwarded-Encrypted: i=1; AJvYcCWihWr0aYDz6x/Ul6x1dqB0uExACiVNjXDEMFmgouFSfd/g1CiwKiPeK/TARgNxTemKzCHNlfJDle+Id3k=@vger.kernel.org X-Gm-Message-State: AOJu0YzivJEScZSMPfVSZa2B96jhPNgJz3jdNE7mriLhxsCPEPLHXpro THi/cVLrogJtK5k8phaLV3cq27tAb3x8u5ukLFZCqIo7WIIEhmu4C1cT3m6dRjmuWSr2HvfxXvu +vdBAUtSLUg== X-Received: from iluo15.prod.google.com ([2002:a05:6e02:188f:b0:4f8:9ea7:99d9]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:55d7:20b0:67e:15a6:e013 with SMTP id 006d021491bc7-68220647265mr2757eaf.56.1775151071234; Thu, 02 Apr 2026 10:31:11 -0700 (PDT) Date: Thu, 2 Apr 2026 17:30:59 +0000 In-Reply-To: <20260402173059.1018805-1-rananta@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260402173059.1018805-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.1213.gd9a14994de-goog Message-ID: <20260402173059.1018805-9-rananta@google.com> Subject: [PATCH v7 8/8] vfio: selftests: Add tests to validate SR-IOV UAPI From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson Cc: Vipin Sharma , Josh Hilke , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Raghavendra Rao Ananta Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a selftest, vfio_pci_sriov_uapi_test.c, to validate the SR-IOV UAPI, including the following cases, iterating over all the IOMMU modes currently supported: - Setting correct/incorrect/NULL tokens during device init. - Close the PF device immediately after setting the token. - Change/override the PF's token after device init. Signed-off-by: Raghavendra Rao Ananta --- tools/testing/selftests/vfio/Makefile | 1 + .../selftests/vfio/vfio_pci_sriov_uapi_test.c | 230 ++++++++++++++++++ 2 files changed, 231 insertions(+) create mode 100644 tools/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c diff --git a/tools/testing/selftests/vfio/Makefile b/tools/testing/selftest= s/vfio/Makefile index f27ed18070f14..d0c8cea53eb54 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -12,6 +12,7 @@ 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 TEST_GEN_PROGS +=3D vfio_pci_driver_test +TEST_GEN_PROGS +=3D vfio_pci_sriov_uapi_test =20 TEST_FILES +=3D scripts/cleanup.sh TEST_FILES +=3D scripts/lib.sh diff --git a/tools/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c b/tool= s/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c new file mode 100644 index 0000000000000..3acc3d78e7422 --- /dev/null +++ b/tools/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c @@ -0,0 +1,230 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include "lib/include/libvfio/assert.h" +#include +#include +#include +#include +#include + +#include + +#include "../kselftest_harness.h" + +#define UUID_1 "52ac9bff-3a88-4fbd-901a-0d767c3b6c97" +#define UUID_2 "88594674-90a0-47a9-aea8-9d9b352ac08a" + +static const char *pf_bdf; +static char *vf_bdf; + +static pid_t main_pid; + +static int container_setup(struct vfio_pci_device *device, const char *bdf, + const char *vf_token) +{ + vfio_pci_group_setup(device, bdf); + vfio_container_set_iommu(device); + __vfio_pci_group_get_device_fd(device, bdf, vf_token); + + /* The device fd will be -1 in case of mismatched tokens */ + return (device->fd < 0); +} + +static int iommufd_setup(struct vfio_pci_device *device, const char *bdf, + const char *vf_token) +{ + vfio_pci_cdev_open(device, bdf); + return __vfio_device_bind_iommufd(device->fd, + device->iommu->iommufd, vf_token); +} + +static struct vfio_pci_device *device_init(const char *bdf, struct iommu *= iommu, + const char *vf_token, int *out_ret) +{ + struct vfio_pci_device *device =3D vfio_pci_device_alloc(bdf, iommu); + + if (iommu->mode->container_path) + *out_ret =3D container_setup(device, bdf, vf_token); + else + *out_ret =3D iommufd_setup(device, bdf, vf_token); + + return device; +} + +static void device_cleanup(struct vfio_pci_device *device) +{ + if (device->fd > 0) + VFIO_ASSERT_EQ(close(device->fd), 0); + + if (device->group_fd) + VFIO_ASSERT_EQ(close(device->group_fd), 0); + + vfio_pci_device_free(device); +} + +/* clang-format off */ +FIXTURE(vfio_pci_sriov_uapi_test) {}; +/* clang-format on */ + +FIXTURE_SETUP(vfio_pci_sriov_uapi_test) +{ +} + +FIXTURE_TEARDOWN(vfio_pci_sriov_uapi_test) +{ +} + +FIXTURE_VARIANT(vfio_pci_sriov_uapi_test) { + const char *iommu_mode; + char *vf_token; +}; + +#define FIXTURE_VARIANT_ADD_IOMMU_MODE(_iommu_mode, _name, _vf_token) \ +FIXTURE_VARIANT_ADD(vfio_pci_sriov_uapi_test, _iommu_mode ## _ ## _name) {= \ + .iommu_mode =3D #_iommu_mode, \ + .vf_token =3D (_vf_token), \ +} + +FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(same_uuid, UUID_1); +FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(diff_uuid, UUID_2); +FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(null_uuid, NULL); + +/* + * PF's token is always set with UUID_1 and VF's token is rotated with + * various tokens (including UUID_1 and NULL). + * This asserts if the VF device is successfully created for a match + * in the token or actually fails during a mismatch. + */ +#define ASSERT_VF_CREATION(_ret) do { \ + if (!variant->vf_token || strcmp(UUID_1, variant->vf_token)) { \ + ASSERT_NE((_ret), 0); \ + } else { \ + ASSERT_EQ((_ret), 0); \ + } \ +} while (0) + +/* + * Validate if the UAPI handles correctly and incorrectly set token on the= VF. + */ +TEST_F(vfio_pci_sriov_uapi_test, init_token_match) +{ + struct vfio_pci_device *pf; + struct vfio_pci_device *vf; + struct iommu *iommu; + int ret; + + iommu =3D iommu_init(variant->iommu_mode); + + pf =3D device_init(pf_bdf, iommu, UUID_1, &ret); + ASSERT_EQ(ret, 0); + + vf =3D device_init(vf_bdf, iommu, variant->vf_token, &ret); + ASSERT_VF_CREATION(ret); + + device_cleanup(vf); + device_cleanup(pf); + iommu_cleanup(iommu); +} + +/* + * After setting a token on the PF, validate if the VF can still set the + * expected token. + */ +TEST_F(vfio_pci_sriov_uapi_test, pf_early_close) +{ + struct vfio_pci_device *pf; + struct vfio_pci_device *vf; + struct iommu *iommu; + int ret; + + iommu =3D iommu_init(variant->iommu_mode); + + pf =3D device_init(pf_bdf, iommu, UUID_1, &ret); + ASSERT_EQ(ret, 0); + device_cleanup(pf); + + vf =3D device_init(vf_bdf, iommu, variant->vf_token, &ret); + ASSERT_VF_CREATION(ret); + + device_cleanup(vf); + iommu_cleanup(iommu); +} + +/* + * After PF device init, override the existing token and validate if the n= ewly + * set token is the one that's active. + */ +TEST_F(vfio_pci_sriov_uapi_test, override_token) +{ + struct vfio_pci_device *pf; + struct vfio_pci_device *vf; + struct iommu *iommu; + int ret; + + iommu =3D iommu_init(variant->iommu_mode); + + pf =3D device_init(pf_bdf, iommu, UUID_2, &ret); + ASSERT_EQ(ret, 0); + vfio_device_set_vf_token(pf->fd, UUID_1); + + vf =3D device_init(vf_bdf, iommu, variant->vf_token, &ret); + ASSERT_VF_CREATION(ret); + + device_cleanup(vf); + device_cleanup(pf); + iommu_cleanup(iommu); +} + +static void vf_teardown(void) +{ + /* Destroy the VF only when the main/parent process exits. */ + if (getpid() !=3D main_pid) + return; + + free(vf_bdf); + sysfs_sriov_numvfs_set(pf_bdf, 0); +} + +static void vf_setup(void) +{ + char *vf_driver; + int nr_vfs; + + nr_vfs =3D sysfs_sriov_totalvfs_get(pf_bdf); + if (nr_vfs <=3D 0) + ksft_exit_skip("SR-IOV may not be supported by the PF: %s\n", pf_bdf); + + nr_vfs =3D sysfs_sriov_numvfs_get(pf_bdf); + if (nr_vfs !=3D 0) + ksft_exit_skip("SR-IOV already configured for the PF: %s\n", pf_bdf); + + /* Create only one VF for testing */ + sysfs_sriov_numvfs_set(pf_bdf, 1); + + /* + * Setup an exit handler to destroy the VF in case of failures + * during further setup at the end of the test run. + */ + main_pid =3D getpid(); + VFIO_ASSERT_EQ(atexit(vf_teardown), 0); + + vf_bdf =3D sysfs_sriov_vf_bdf_get(pf_bdf, 0); + + /* + * The VF inherits the driver from the PF. + * Ensure this is 'vfio-pci' before proceeding. + */ + vf_driver =3D sysfs_driver_get(vf_bdf); + VFIO_ASSERT_NE(vf_driver, NULL); + VFIO_ASSERT_EQ(strcmp(vf_driver, "vfio-pci"), 0); + free(vf_driver); + + printf("Created 1 VF (%s) under the PF: %s\n", vf_bdf, pf_bdf); +} + +int main(int argc, char *argv[]) +{ + pf_bdf =3D vfio_selftests_get_bdf(&argc, argv); + vf_setup(); + + return test_harness_run(argc, argv); +} --=20 2.53.0.1213.gd9a14994de-goog