From nobody Fri Apr 17 00:17:59 2026 Received: from mail-ot1-f74.google.com (mail-ot1-f74.google.com [209.85.210.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 9A7DD39A7E6 for ; Tue, 24 Feb 2026 18:25:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771957539; cv=none; b=G4bTo4Xi09ZwOomd7llcsCbP9SNBGlTqaO+krCxJrCCmph9ihmLbOKoyAd10H14mkwU77Bw3xPl0OxCS3UTARtE3UrEz622nemrwVFOCazmTzSODdiaxgLcalXIeu3VdYGwFAWHNE/HhM3yLnUAsVTlAqqn0UYWi7mlcXNgfpgw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771957539; c=relaxed/simple; bh=VPgmUc1W/PstaSNg9BA0aLpGy2DMtKlMVZ2vml+wXjk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=iw5ZAh9CW0kcQ0aYzl4ahAu4k8t+OSF+S033K/WvIOvcfLcn0zF6iP0bOhi1YKyx22ZJSd6n6eYverMILygiSLUQlf7bSwXoI3MaWYJoKvegVEW9Tof3cubwzpW2S5A3MIwXKMlIdv1uuxZPdUhFupNi95/L8QQRf4h6PDbqL4o= 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=A5qTWEda; arc=none smtp.client-ip=209.85.210.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="A5qTWEda" Received: by mail-ot1-f74.google.com with SMTP id 46e09a7af769-7d4bd29099eso71121257a34.2 for ; Tue, 24 Feb 2026 10:25:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771957538; x=1772562338; 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=7yYcH80TlRJEoXywxjzS5+u+dAUmU/HoiYfOPoDy8HQ=; b=A5qTWEdajE5fzN0gcNA4ixGeTeEsn675C24bOFpysdcIHcosWS4NUJJyxI+pztJmpf 9JcywjjGhgd8Ych9cePNsiaY0zXblMyxg89RR1LPoYJWgqOdp8Camyb8NRZHpTvWYmYX 9VyG1KegwN08aMgJGYSZ9+YdLmJ04YNPVGvCQxOUwxHxi4be52nx3rX6WCS+6C6iev8V jNns6sFuLhEt0jDPLR4J/0nHowvMQEJpBHqxBgWkAt4Igj4f/lrebJjqfHtbRQk7yFxR vWg394ngJ47bHMVCsoKOEVg885E+oFHqaKqZTBYKR1mooxJWxL5ilJp+jJ+o8EiIM8jx v/2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771957538; x=1772562338; 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=7yYcH80TlRJEoXywxjzS5+u+dAUmU/HoiYfOPoDy8HQ=; b=LjX0MNZZBwanfiUQK2FBWiSuFEQX9BQ4WoFtlCbJ5aVnKaSR3bO5cR03hSSM61S2mg Eyn0y5MJjW1TE9gMTGlCosyAuvwnAT+OYus1bs2xfklHMvSq0wyNXZaujWl5P537ZYrv VnmTEdF3YD4cdq4kn3hEv1v7wPRKYgvoqHfO+J13cPhFjdB4q83zktkLxCMszCbJ9Jw6 F31SbiS/dOdhi5Z6bSWzn6I9eeUbMMtT62+Ei3gmd+oc+Y+/ChAvUbjH5HRvb8LDQTnI PMWxGTUHTV4AcQj4E0V+14K1CC0xZy4UJUbpLpg2sywDvjgB6UQq28tSmtLTmpJOo4Gh PSAA== X-Forwarded-Encrypted: i=1; AJvYcCXLpvvJU5F+4v9qSu+RZFwTb+n1zTkQlAvluE46euIiZhdyH1hWNhTVv3nWmR3I7RtVbC69svzHm5HVudY=@vger.kernel.org X-Gm-Message-State: AOJu0YwuPbXwoQe321jVRBtYFKog9+YUt4DkJYqdfKQYTBD4NpJvf4dN doWehBjuZkeHQ5stNpAVwZmJyT771FB86fBQ8jloXTSnrr2lDbJSbfe9tfrc9AinO20dOaqimAC XeZlxp5sjkw== X-Received: from ilqk1.prod.google.com ([2002:a92:c9c1:0:b0:4e0:8c1e:e15]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:169e:b0:678:a4f1:c369 with SMTP id 006d021491bc7-679c44754c9mr6789966eaf.24.1771957537453; Tue, 24 Feb 2026 10:25:37 -0800 (PST) Date: Tue, 24 Feb 2026 18:25:25 +0000 In-Reply-To: <20260224182532.3914470-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: <20260224182532.3914470-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog Message-ID: <20260224182532.3914470-2-rananta@google.com> Subject: [PATCH v4 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 3c796ca99a509..e8f9023cf2479 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -15,6 +15,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.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:59 2026 Received: from mail-ot1-f74.google.com (mail-ot1-f74.google.com [209.85.210.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 7AB983A7830 for ; Tue, 24 Feb 2026 18:25:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771957542; cv=none; b=VkzzpxOQnZhOANSqgnRSbajjWMm54AWhZAkPNdB93mU4neRwkwjLRPUCVQGzIwjzE1x5ekKUhRgdG83ZBSjRkAN/F7C7px/adNmP+7Q4TF9RYhrks/t8veulmj4IRZJEOh7HTAB6x3/ebqIA9P3FXOT4JbWbrxbiEbl8dXlWwuU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771957542; c=relaxed/simple; bh=c3RxJ5NfBDnbTVGAVuqYFGnYsYR7E9Z82BqrkHaXlc0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mdI4DKt3g4cFPRayx1cktuF/7bVO2ZGf6p2QMJ+gb++q4Mbwwg8ozlGDdEI+IoEMxgKtOPvUB2Slsg1QUa3eeAbrRjeci5u5Hb6Vr/Gkds1wvY2rGHl1Unb+QhcpweoaJPuNxQQI0sYO0dcTalloFWP4SI5pUBaHmTuUm8XRpok= 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=sM/VPDFS; arc=none smtp.client-ip=209.85.210.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="sM/VPDFS" Received: by mail-ot1-f74.google.com with SMTP id 46e09a7af769-7d18fa0728aso63150988a34.0 for ; Tue, 24 Feb 2026 10:25:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771957538; x=1772562338; 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=W43LUhh0LTU1//BOJUSbLb590wyjt02CHFj96YxulfY=; b=sM/VPDFS1LI5zqRMh2LUmqqBHKCnOaFjdC3i7m6GTWjXFLwKwYosYGxepJ/iJcaHF2 LPvfIOi1IktEkaDfp8E0SOPoVmimPa3TDG5g0Ucz0SLTEXs2d7bSSD+yVrw6fUgfICFm wXoy0bDKaGkrTYcdopYZP4jM8kFbqk6OkVg8KxXaY2lbX35YNhrhtCR2svKkRFgBX0vZ d1mX+jm/W+4Jj5kr9tX1OegzBV9zkiFwqF/5YY5GKW2e098qc/ZFN+VBD5945ItjIX1B fzfJxJ87RKCMpiP4o7zlFxoIbm/2loZhGCrs/rFHKNzIGIRe0BGhCyk000W2z+3Hywk9 mhOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771957538; x=1772562338; 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=W43LUhh0LTU1//BOJUSbLb590wyjt02CHFj96YxulfY=; b=TyOF+L54lO/HpC8KRsz+bu/PkEzxf5esap7KJ3WTrfo9Gp/zeHnb1HshHdOzmAnPXN J8KREyqQOPyfrIDd89w1LMmBZK7Noa80aPtkwya032RtPx8tN80nT1WBOXnedVXXHgHN vIK7OJMYIkzFflFtwTGTUsEC8Qx8zVC/KhRT+vOrsAlLkejRqHfu1ImHTSytJ7oRnQAu bYcQgf0EjFzKLX2KdK/3d5dwbNN35gQ5RkLeHcGGjGmBfwjWS0Gu/AY0TeyQmGm1mpMm sWMoqCRHz0YRT04jahenrGw2YvxExw0SEMHZarPlHqcU4+q9FtKbxleMMiZTmWQtcHW+ gTWw== X-Forwarded-Encrypted: i=1; AJvYcCVgs3O1QtTF95UXVBrXPNetYiufbuT/WmTbThubKBM8lJciNTScsBFGJKCIKUj5HQQCYKmYKOMfhd2yEgs=@vger.kernel.org X-Gm-Message-State: AOJu0YzJfIfE7Pq2DJZDtIgf0kPz4c0e6Tv3GfdT6CwLVxhfeav72WQ0 5bhH/JO0MzCQRHzoR4R2RLHlPwzXBec0R1os1YxoP4ASwaeVWK+xv2PIISJDSTIJt6K1NDm5BxG eXJkIreHaAQ== X-Received: from jabl3.prod.google.com ([2002:a05:6638:c383:b0:5cb:752:3749]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:4dc6:b0:662:f2ea:8f9b with SMTP id 006d021491bc7-679c428264bmr7129729eaf.20.1771957538427; Tue, 24 Feb 2026 10:25:38 -0800 (PST) Date: Tue, 24 Feb 2026 18:25:26 +0000 In-Reply-To: <20260224182532.3914470-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: <20260224182532.3914470-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog Message-ID: <20260224182532.3914470-3-rananta@google.com> Subject: [PATCH v4 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 13fdb4b0b10f3..64a19481b734f 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -188,7 +188,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); @@ -208,7 +208,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); @@ -279,7 +279,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); @@ -289,7 +289,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 5397822c3dd4b..3a0bea5e26480 100644 --- a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c +++ b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c @@ -45,9 +45,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 ecbb669b37651..723b56b485f67 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.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:59 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 BDDD43AA1AD for ; Tue, 24 Feb 2026 18:25:40 +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=1771957543; cv=none; b=pfJYzkms0iCBNFHPxONXmRLHXwPczXnS58jSBmR4SUNLoXjGXwQ+NaEuAQKU2WVyfL3wh+5LA32Fe6ikUvr5/SWeEoxM9Vj5Rsoqa6ORJMmqQQCTPFSYCGcujJDSvEiSF0JVdKZvA60x55+e8cw4oOZEk56h/lja2GVA0KX6H3E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771957543; c=relaxed/simple; bh=YxFmcC4VMLn8Bc90e1lqgg3nadvbujj4UdXuj/W0n5k=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jbkQJqytSvbopio5Wp/9XbyGsHK/fKG5cc3kJL1b+rWUlgpWh7ItanfidI4YSkZc0CQTxV2YqHIulvId030PAzEsVfebyYssgqW3WJj616bhkvc6RXp9V6SyiyHou5wh6dC1l839Cmv/QeJoNT3hcuFiZ65mXJ2WeNKtKXM3A54= 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=uaY4emPn; 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="uaY4emPn" Received: by mail-oi1-f202.google.com with SMTP id 5614622812f47-463c4133ccaso60077060b6e.2 for ; Tue, 24 Feb 2026 10:25:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771957539; x=1772562339; 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=AVyMI95lvwVBSZ9jVuKxs5avFT/uJ4CVmSz3r8TYmwg=; b=uaY4emPn8QnyvIIxan/82J2RMFlJru1xz4N4zgaAwLHXX8F+m01avuG4yO3F60fLjp b9D9cY3ZBXWDNBDC9+wGSJhQ/y6wQgUKJ7SWcHC0v1wDrpPGTzSupoXQixns9l4U28X+ N5mWEQG5y0iQ7nVjbW04buRncW+jUrQTtWQLTlIeTaIlqzewPzb5WaJo3wkWZBOT3inn r2Q8OkHyFVvfFaN+G0p7Al7qHyrMftjGnOWjvVRnOQN8wH/yaNomy/TQahieXZn2EOQy 4TbhSZLOrMbgN9xl4kNqMwG9vCPgOrRPuZRfhd5WJkhsHolbzAOALcpHjNcfWXForCsa LtHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771957539; x=1772562339; 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=AVyMI95lvwVBSZ9jVuKxs5avFT/uJ4CVmSz3r8TYmwg=; b=lSSyRBvhYI1qOYykf6G/gFe8Q1EfuYdyqDPGhN1IgkHVij0Yxz+cR5bS8WvhGvo5h1 WYLdgzQiYn/4yftA0oIeqsoX8YCd04+sBwyKhnaSqG7AlzZwBDSmOwdM48yMK9yysGZD aNQEvJTHGdZcWKDCAaOd04/Anh8b2fXQMAkBPwsA2bJBucVMWNDrtVnJpxfqu/Gu0CJb /8tpYOzlXVkIpGLk4TUo9xpADCklhI4WLz0AP4ZJVG31WBXUTBwr/VPDGlfisl5AoWxO /1sK2Cy8Kwu9IN0SRRZRiAbWbziALf/n8Q2HP+rdgSyRmuHBo4/jwoyY8Y/3CwOkQA05 vnVw== X-Forwarded-Encrypted: i=1; AJvYcCX1VIwPOkebVDCBnPeOW1ENm/M50kYKP2svDqJi1s18i0xWlFgz5g+TNKXnB86tIaG/ls9eDQMP88xLhXM=@vger.kernel.org X-Gm-Message-State: AOJu0YyWyDnf/RH3Ct2CXY2frI8HBCEkzQZYemftE+7C4Yr07itxRFYZ ZRAhH8PaggOr2ouyxkyp/rODbuuivUc78uOPjl0SiXru2cJRCc4pfH6tdSPmkNsi+S8QtjfjckZ w20xsbqZxUg== X-Received: from oabw8.prod.google.com ([2002:a05:6871:188:b0:3db:801e:3b43]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6871:e2:b0:40e:e219:fba9 with SMTP id 586e51a60fabf-4157ac7ed53mr7023767fac.19.1771957539393; Tue, 24 Feb 2026 10:25:39 -0800 (PST) Date: Tue, 24 Feb 2026 18:25:27 +0000 In-Reply-To: <20260224182532.3914470-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: <20260224182532.3914470-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog Message-ID: <20260224182532.3914470-4-rananta@google.com> Subject: [PATCH v4 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. 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 | 141 ++++++++++++++++++ .../selftests/vfio/lib/vfio_pci_device.c | 22 +-- 5 files changed, 156 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 279ddcd701944..bbe1d7616a648 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..c48d5ef00ba6f --- /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); +unsigned 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..86a560cf16528 --- /dev/null +++ b/tools/testing/selftests/vfio/lib/sysfs.c @@ -0,0 +1,141 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include + +#include + +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 vf_path[PATH_MAX]; + char path[PATH_MAX]; + char *out_vf_bdf; + int ret; + + 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); + + ret =3D readlink(path, vf_path, PATH_MAX); + VFIO_ASSERT_NE(ret, -1); + + ret =3D sscanf(basename(vf_path), "%s", out_vf_bdf); + VFIO_ASSERT_EQ(ret, 1); + + return out_vf_bdf; +} + +unsigned int sysfs_iommu_group_get(const char *bdf) +{ + char dev_iommu_group_path[PATH_MAX]; + char path[PATH_MAX]; + unsigned int group; + int ret; + + snprintf_assert(path, PATH_MAX, "/sys/bus/pci/devices/%s/iommu_group", bd= f); + + ret =3D readlink(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; +} + +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(64, sizeof(char)); + VFIO_ASSERT_NOT_NULL(out_driver); + + snprintf_assert(path, PATH_MAX, "/sys/bus/pci/devices/%s/driver", bdf); + ret =3D readlink(path, driver_path, PATH_MAX); + if (ret =3D=3D -1) { + free(out_driver); + + if (errno =3D=3D ENOENT) + return NULL; + + VFIO_FAIL("Failed to read %s\n", path); + } + + strcpy(out_driver, basename(driver_path)); + 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 64a19481b734f..a7e00d017fc6e 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -22,8 +22,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) { @@ -181,24 +179,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 { @@ -207,7 +187,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.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:59 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 D7A833A1A34 for ; Tue, 24 Feb 2026 18:25:41 +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=1771957544; cv=none; b=jLMExZF/o/og9UC01xdZjqkjeN307Ku3Q7qcT2skauDjyE5vexG63s4SOHDPef3I+Q3/A0YKPmgmiRGrLV2fin5coV5oqx5AERXUYsjA23azZL1MTkuOpztqZPdRhCHgOdM4eqGeos/HFXGQ2/812hZYC7Jev4WTH7cQPQM/VU8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771957544; c=relaxed/simple; bh=ZGhcMmp96suPt1L+uYHC9+7SbnPLfccNrAp6sr5cOPQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=QG78BnWlVCgnM0O4IHTLExspJRfW3sVMPzGIQHd9x5KnQbIuzYWKPcBM4ARGfM+pNXXnVA7PvP8VbmcbygFBxtKA4UZronw0Cr06Yv1mx0x2+IeOvFxSBLzewv7uj+Az5RDbPjQj/WiicMZPtwqzWNFWr8ia70DSJr2G+y3IGHk= 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=RAxFKwjO; 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="RAxFKwjO" Received: by mail-oi1-f202.google.com with SMTP id 5614622812f47-463ee33f9b2so23260630b6e.3 for ; Tue, 24 Feb 2026 10:25:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771957541; x=1772562341; 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=GhsCJ6Jzd72tpP6akMGZKaD93fr7NGPRDnXMiqTemDc=; b=RAxFKwjOQYcgdV23/w/TiHD3uPc1Xy6EI0tbapmXJiD41ThGKJX1lGAp5bctxAKCTB jVJnkq3QAk2xU/l5kWjGGNYeD6Rp/el5inYkuCkqNZ0tT1lUfzN8XKUe1850LsQ559Hf ITc3GA96JFe/53QzytUce7AQZ3skFYQu9OJzfD2PdQiFH1Shua5UcK2r70uqhjT3QwS+ u1JLFJB8u6As5T41PfXMwCTyh1Bo+XGA29m3BZvCNIdZoqsGlQxDKF710dQT53PRmcTb CiePmW+OkwUMWqROx51BCYuFi16vU3rB/urfcOg9u6XX72CyfD8+aPeaIclFcK3YUc2C qvBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771957541; x=1772562341; 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=GhsCJ6Jzd72tpP6akMGZKaD93fr7NGPRDnXMiqTemDc=; b=u/KJ82sEjmiDKJmH6FG2yZWWv60DLJTUXHmL9UTKRmdRylXZo9aiUNx9c/FwgV4+8d BcTSv0kViZGnOJAA+bHSAPQr6SqR15ec08tAaCTVlSh4v/o1VJHKFJ5JWCY5XASHSD9w PaczOKcC4zeof5eiAAZUHc9xTbKjiPyly3bLh25d4dL/K1Az6Tjrk8txNEHcCy9rWkvY Jh7AfogiSxcOggX4nMylgIh8hjND9HOsGvE9DO3eh5o0zOLd2q9gnmCEBWyxWv1fyEWp +52FE34UieyvIplqQcWIUMmyxs8aJZf6foTbYN8m2tFjU998s3r2B90hFTawY9MESECQ 6nQg== X-Forwarded-Encrypted: i=1; AJvYcCVCmpffFUboJ7tSZc2/yoSy6IceRbIoKzmNHtInvNqU4Zq9Xym9qOAS88jythSqR5V53QZTQY8kw2mMJI8=@vger.kernel.org X-Gm-Message-State: AOJu0YyDdJ1Qa2twraAtS5PkstZUNdeu+VeXmQP4OSgounBnhys5aMEY 2NVunwiBuknqJ764TuBqwTS8DHD/KLxRGAr0+3r3qgWiAiMvfMoydc6phrmTgC7R8eWZQbBEPB3 Aidz4k4RQdQ== X-Received: from oapo20.prod.google.com ([2002:a05:6871:794:b0:403:fa87:8459]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6870:37d5:b0:40e:9dbc:3b63 with SMTP id 586e51a60fabf-4157b0bbab2mr7216936fac.35.1771957540691; Tue, 24 Feb 2026 10:25:40 -0800 (PST) Date: Tue, 24 Feb 2026 18:25:28 +0000 In-Reply-To: <20260224182532.3914470-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: <20260224182532.3914470-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog Message-ID: <20260224182532.3914470-5-rananta@google.com> Subject: [PATCH v4 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 | 45 +++++++++++++++---- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/vfio/Makefile b/tools/testing/selftest= s/vfio/Makefile index e8f9023cf2479..120c9fdee5c09 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -20,6 +20,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 a7e00d017fc6e..4c79557cc4e09 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -12,6 +12,7 @@ #include =20 #include +#include #include #include #include @@ -199,7 +200,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; @@ -217,8 +238,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) @@ -279,12 +299,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); } @@ -299,7 +327,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 @@ -307,7 +336,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 @@ -323,9 +352,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.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:59 2026 Received: from mail-ot1-f73.google.com (mail-ot1-f73.google.com [209.85.210.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 29F563AA1B2 for ; Tue, 24 Feb 2026 18:25:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771957545; cv=none; b=PpS/9I37v57TeAcz8vJ5l35psUv/J7goMPQ666F3AQzHPDF0vL44RU42RENuvgAABezNrnrvLxlkbDxiGM6VcGHnT6EOlHJ7s+J/cn/qcqsaGBH8hQhVPATTJHSf8cJREe2/ZgJHqI7FNxVk86QQWIIdZJUTFOpm+iXOxe9WoSM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771957545; c=relaxed/simple; bh=vGdV3ktht32WblZs3LsiIfE3xzMkY/XL4LUK0CaWYe4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kw3/3BEBDDU3DaE7AWRY8EonWaWLj81tbrjRHxz7BZRLP1FBahjWms5zBRFmM4aGFP3JYjXS6NVzw//X50r7DJCgvsPhNrNzfLeIQJZqFEzuGTAp+xZWotgP4l4QNEoAibemVPJY1aDr4B9PvNtY2fNFX0iKSV3mO5JL8cIl9T8= 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=h5GhGTp9; arc=none smtp.client-ip=209.85.210.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="h5GhGTp9" Received: by mail-ot1-f73.google.com with SMTP id 46e09a7af769-7d4d4db1523so25879182a34.1 for ; Tue, 24 Feb 2026 10:25:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771957542; x=1772562342; 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=W5wQFF+d0vZbX1lJAkgEnS9lDKccfjOQiCEdtZxbCbg=; b=h5GhGTp9g3+N4nqB9Xm1HO41HlAK+7iEhZSUF8FwcGZbbpF9/sbmECw3W70xvHLS76 o+Ux60Lu0vq809XSQBQQsm9uRhN/hhHX3DUEgtw2aKd8FAg5ukJNxVxNfSYByNP1xvRh Ut0yl3FV68pn0A9VfaVewGJnzwtLlYQABnLXeVB5EICGdJ6KOo7zX8qpOrfkI/kdyEu/ 2jk39nK+PLi0c4KQ5letBI4XTNv8R1FHJ4FashDaZgiXYa0LA6elnE9kUD3oF7R82CoP e9u4gw34YvqvkfRlahMj8m56Ztehdr71qkxAXg/tR1nd0L22KVWIu1J4rJpY71jYu5sR L/oA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771957542; x=1772562342; 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=W5wQFF+d0vZbX1lJAkgEnS9lDKccfjOQiCEdtZxbCbg=; b=SVteZx/izaGwOoo/E5UFKpqg38mKZG/N4ec3V1y3BcDpGlqW5DnD/2a8n9U4DjVzko scISyP8L1cWV7KWg36KX9KCsxd9Gk6eDbKsS04SmPnD0JxhUcmnP8ah56cf9YyW9yANn XM8WoRUCltFw6G8ydGioRFEVLAb6gjyhpeUKs4Ufe5ctDA1aSUNVjq8lrLnpBDo1xCNJ 1Rw4sDI+rsV9gfgp4qIiS7XCvz6rjyXj9UY/OBEcM1rmauMuF38uwnmXONB9hMhKvatS EPf8A1PsnEyVhr8MnL8hXzrBY4J4qbtMe6F9SmaxZyWWO0QRn1keXswLIDyXzwjUTnou Zmvw== X-Forwarded-Encrypted: i=1; AJvYcCXLv80YEpVBNX1UZ+rXGGiM5RXwjTBeHUq9NBuwz8D81M7LKY1lnJW9HvOqiTCiuJn01J49CPLDptzzDlY=@vger.kernel.org X-Gm-Message-State: AOJu0YyONvQbcyuBwmgkhbA6U9prWYp0Dd1lZy8XkTAYZojY9t0yKKKI i78Llz2Fo/vY+u+NZGgYVUDijuoqxr24J8yY4cNbI5D7Y6Uuk1xdA9eKbdcTPPt0LXTaAP/BOz9 aXkkkbzqojQ== X-Received: from ilst5.prod.google.com ([2002:a05:6e02:605:b0:4b4:27a0:c01e]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:d0e:b0:66a:9d34:6cc0 with SMTP id 006d021491bc7-679c4280f47mr7318572eaf.27.1771957541683; Tue, 24 Feb 2026 10:25:41 -0800 (PST) Date: Tue, 24 Feb 2026 18:25:29 +0000 In-Reply-To: <20260224182532.3914470-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: <20260224182532.3914470-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog Message-ID: <20260224182532.3914470-6-rananta@google.com> Subject: [PATCH v4 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 4c79557cc4e09..142b13a77ab83 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -180,7 +180,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), @@ -200,8 +200,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 @@ -216,18 +216,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 @@ -237,7 +240,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 @@ -299,8 +308,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), @@ -314,7 +322,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) @@ -327,15 +346,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.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:59 2026 Received: from mail-ot1-f73.google.com (mail-ot1-f73.google.com [209.85.210.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 C59673ACA5D for ; Tue, 24 Feb 2026 18:25:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771957545; cv=none; b=PcymnBE2a6O9EgL8N4zC0oGYZj1XzaZXSKpPktfLUwcw++okSIvHPlJyUABH4AJl/J4aLKVmZbTTjYHaHOwb8RwmTlXTDFmlvhMmwoYuLomPrIZP5oAFIOqyYFXgUV3iilYu8AIiaUlfP6bkGjrCGywc9rv78PJrvhX+kruW1ac= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771957545; c=relaxed/simple; bh=D5cFtGr3FjZqUbVXZMPOo8EQG+SRoqsZOQDRNB7YSGs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ps98Ojkn2eGNctDANKiUpBdDqq/9uk3WdVajIOUbUwStwQczcyEi6njfajuu2sJ6EfCNBR9O/1lfAvkc4G7VqiLQodhueiB8se12Bv8DpUD+0jXp7ZoiCTRAS8H2+DFNItko/y4fmAd9AgYOFAe9yOFLxUttK2PkeoeV+SCNECg= 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=V5QBS5Ab; arc=none smtp.client-ip=209.85.210.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="V5QBS5Ab" Received: by mail-ot1-f73.google.com with SMTP id 46e09a7af769-7d49709f87aso59377831a34.1 for ; Tue, 24 Feb 2026 10:25:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771957543; x=1772562343; 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=76o1BjsM23otY5E0m8PR5i+qHZGjPQ7/pAlHnOvU6/4=; b=V5QBS5AbtCx2PLzN/JCQwQ3qAKELMjp0IMKTUfsWBqcBiimlB/60UaGaDeF4T3eue4 0gWasYsigQaO5HUjO5b5V4ZERjisJdleBhAjbnPuxM5cTD5B6LDko+1aSdNPtGGloJx8 fqBKMGavbdJZCISKWUfN9rHkvGJnPnPs0mXN/RT2DNjPpXrBwMYu8P8HXOf5B5dQJRzJ HqaJiqfH2dm0pBZnVTXt5qmXHfjFIwKhZwlLhcUl1GOkaH7Nh3+W+AvskRFTFBoeycDb M1s1jX0ha9gOM7c+6vtEcIe/AFBnRjojdkeanWWODcGVLnjpsiSyL5D9LYr8GQ43LWej OGTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771957543; x=1772562343; 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=76o1BjsM23otY5E0m8PR5i+qHZGjPQ7/pAlHnOvU6/4=; b=pSlv/EyhwXfezXp8intw9TQL/dyfBJOPq+nd8OXGfLeNMK3BANanePZXAKEvQwXoTG Teksr6kna6UL9iTR+KSMWx9k/csLrjfeppMKDtG8eCnvU8lcJWbXEVJv+0stHLcQqk6h jYCjDo+Ese7eEbPUgKSG8ZgB0Fpcs7Zq1gVoAom0F6YlVILBZ6J8IoSVpbQMvPrJOdVL AliZDuSNPdP4eCv2C5c/TOYXV96apYn2hJITGHcjcn6OrFlWhqc5B2JiurQX6zQ2GDy8 4huTDFMZVHrCehfjl8P+c+XOPpmzDR2fi7Qf3FiIteLZw8nClsaYnYzatJX4pNO7EOaq fRUg== X-Forwarded-Encrypted: i=1; AJvYcCUZtRmezCLjtmzmXrppiPmX44bkfoOuH36IKK5O3oEDJdAyGUAfBwGWAT012UB3qCdJXJ/c7ZDNCjOBcqw=@vger.kernel.org X-Gm-Message-State: AOJu0Yzv4reFHP9eNMic1ACuhwqoOIqCfeOkNMwL6avkEnGl+/bdnPro 4KdWoFZEqgoYgk0uPoRy3xKOfp1vBLt5S5U/YiH/8J8n/+pbDNN1Xdm08GLK5Z37YAKkUihHpep ckcsvH/yKhA== X-Received: from iluf4.prod.google.com ([2002:a05:6e02:b44:b0:4b4:b52c:62e]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a4a:e909:0:b0:679:8861:f58c with SMTP id 006d021491bc7-679c45080c7mr6970843eaf.39.1771957542675; Tue, 24 Feb 2026 10:25:42 -0800 (PST) Date: Tue, 24 Feb 2026 18:25:30 +0000 In-Reply-To: <20260224182532.3914470-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: <20260224182532.3914470-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog Message-ID: <20260224182532.3914470-7-rananta@google.com> Subject: [PATCH v4 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 142b13a77ab83..f15065f531562 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -109,6 +109,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.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:59 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 BDC983ACA72 for ; Tue, 24 Feb 2026 18:25:44 +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=1771957547; cv=none; b=sufgIIarWnUNOliuyUNG/2Za5mUtWy14bPAJMMhJhqcfJRY2KyD6s6hg85vAhf/Zd2bCa2bQtsFtgyozhM7/vtQ+DJMY8VGDdFEfadVQu1Yh54p4kAW4nJCDGj3GVIGX3L56aT+RjO/e7Zy0YxoWj1/Bnxr1qRcADKkibVtFbeo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771957547; c=relaxed/simple; bh=VmQzJHJCdVhJpl0lXLHmrt7QhBYTiLTqnSuYTYveJt8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=QysVrlJ1yn82DOWAjSRL8l1fDcht6noiirONuYUCyfcl2Lqp6tVUjBpmiUxETgsUlS3EoTdAyGmpITeSk09dYFpUeeVLn/XsZJ+SeCrKvSRUqjHcCRAT4zbxcuyXK2vAzRrfg+fJjbwfLSlQq5YYhxjDE6RXLWcc506sCA26l+c= 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=08RRW07L; 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="08RRW07L" Received: by mail-oo1-f74.google.com with SMTP id 006d021491bc7-66308f16ea1so70199447eaf.2 for ; Tue, 24 Feb 2026 10:25:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771957544; x=1772562344; 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=X0JgQKedteVjhK5W38tsOG4zN99RY1VmLQMUHBFPZ+M=; b=08RRW07LrFxmudD+s2XYwZXTniSH90haA/icWBHqW8to5rmZrnL4qO64EFDPtEItpW jCy51vkKWdblRsDMdI95XmLo2u5/Xbduz1T0w6bw/VLFN3rQeHyXcT3dtu8SxO6Xf//R 885XPThwWLGjTJtfLs0XewX2KIWSVMgJAk4PSURRZq7PRwmVwLW26iRBqMm41iZd/QJg LJeBoRa51ylIYK5g8bjPyWXJrn6QimZ+Lnp3h1zRJ7AJvZxQpAKcjD57narmTtedbdBt MwYyJi1Gg7+z03XPqKnLllwct8wxFmPQfmoTqGRKKv4O4tsXdI/v+9pFEax2s8SPJZ8d BDfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771957544; x=1772562344; 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=X0JgQKedteVjhK5W38tsOG4zN99RY1VmLQMUHBFPZ+M=; b=Z5ZGp+RQDF0q1O9uQtNL5+eTTRqlPJDy9lWKTW4eAjXQ3ULXX9BEGk9fBphS1I/JgF tjgtxk0yUJL3chMk5bdcpMyOHwSnrKANjTkZ57RcU4YoqaB+z/lFdIBYd8niCu+ERs9M 3Z3o1JLYhlPZB4siDLsM85dtCcVECGlDodXzkG8o4e6UsHeJ9WN5CG9nvZq1Aytv1R72 4RQmgDHmbwN7XHeJPpcjR0Wp1aTliup4jfhYzommXEmQ6dg2fuH0ayC2e7CIg1hmch/+ +866t6CTq5/Z3DWSgoEwciutS18gkFliUdpMl63OHRhQ7G+lDLxv97sS7Xgjrib3LVDk ZpSA== X-Forwarded-Encrypted: i=1; AJvYcCVcdLlnzYfQyGCewL/G6zE3YCwxun4C0Ewm7Pk5667nnWrqIwOg1pQaE89B6oii+3NZWnCn6tDL5d1EyLw=@vger.kernel.org X-Gm-Message-State: AOJu0YzhElswQsgQSdSAznkxK5SJGnuao7FqcMErb+c3GlUDGYxFEfJZ y324BEMX2PhyEbXmaaxQGFakbI7T3WxJrTZr1rODxr1Wl5GkqSnJ9Y5MQerQ+7V6L1EEb6PCl5u A75hFEEkFxA== X-Received: from jajt19.prod.google.com ([2002:a05:6638:2053:b0:5cf:f38:8cf1]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:4b18:b0:66e:9d89:d8bf with SMTP id 006d021491bc7-679c44ebbaamr6942557eaf.54.1771957543578; Tue, 24 Feb 2026 10:25:43 -0800 (PST) Date: Tue, 24 Feb 2026 18:25:31 +0000 In-Reply-To: <20260224182532.3914470-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: <20260224182532.3914470-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog Message-ID: <20260224182532.3914470-8-rananta@google.com> Subject: [PATCH v4 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 f15065f531562..6f3f65021bd6c 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -397,7 +397,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 @@ -408,6 +408,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 @@ -440,5 +454,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.414.gf7e9f6c205-goog From nobody Fri Apr 17 00:17:59 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 C58F33AA1B5 for ; Tue, 24 Feb 2026 18:25:45 +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=1771957548; cv=none; b=EywtMKKlIPH7bxYPxxFzaw5ouQkRjYFfmP2cq5RYhpmi722+Ye1ri69snczlYurD2SLjO9Kxw05bOvWj8UiB8Fr3F1IDx4NULp2lEDEyDLVkfWs0o1GUHSL8Hr03e2KmqCLsX27ycNmeGOoFpMqbx0spAMxhhBDT+nmjfsVeRmU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771957548; c=relaxed/simple; bh=3AaRb9kd+m/BFw4Y6l/wr/f93IKu7ns9mtPVYDvNhBA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cGu0V12A5la++SPadwnpWOmyukmfTBYZbyxzX74q40/Ik8v1bQSqEhdN3Uyw6XPdC/DfamrBv57+Oz7A+8W4ptoGUvcVZpUp9X1lnAADV5bvmdZiUIV2eWcpOiiILHd/0Zv7+3F2sZh9zAU0d8kKehDbjCfkD15HEUFFdXNkUaw= 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=NucyXhFF; 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="NucyXhFF" Received: by mail-oo1-f74.google.com with SMTP id 006d021491bc7-663006e4c3cso72154225eaf.2 for ; Tue, 24 Feb 2026 10:25:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771957545; x=1772562345; 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=zGOpBCQfWbYJpBgZENbILjr62MefWye9jntkGa/QTiA=; b=NucyXhFFwq0JhG09E7XHeWZoRUEMNh4Lz4RKdWb1+cSGe3fh6aUeEJ4V3hlB7UqsAC /7P1cxo4YvFRa7YyLHwQ4kZrn2R0p5Qwr0Gg001mfUuMiR8gQwK6w5HuWpQ1E3hf0mtF 8DsSFkdy+Gy7UeSf9tPde/RlYG46dPz8+Ch4KUhrituw90GYyF9ZwZHW0KIWrf0FJwPv SepYWO/PymJ8lrpAnOE9aW5GOBS7+SPiQQDAMPuFG9IxjpAN31sMR3r5U4sgXXTE3Cg5 cgUA36nAMiUS5SXwKeKzOqrUj6wvW52iFLr7v2JkMBrhafuLuCjUkDJf93glGB40OLAt 3t9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771957545; x=1772562345; 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=zGOpBCQfWbYJpBgZENbILjr62MefWye9jntkGa/QTiA=; b=AuYlgDlAQF/f26mRnDBP/sOp2TleoRB75LZgivWKCi6Bvf+40eIWFGkGepkd9znfz/ OK+x7UjnKz3gy0tqE4+YMwjuS36LZJvXSkS7NknfofgyizgKv2v2KAdKglmsB+rWVuhM I2CnkLn2+0F9pfjc2S2ffi8r234K5yypkJlPiDN2W1gyfO6NKEV3pquO/bRKxeN+h2HK E+pQSQoSu5JzuSH9P0+R/wEX/d/ycTFhKl1xLew+nEwOuS19KqZcYeagwACkDDeYeuyB fpXdzZdTwkKBoo31/gkrxa+w6WgwgJckU+5UoVBrxoHv5EKDieU++fcHU+Ujrq1PmGfz rk9A== X-Forwarded-Encrypted: i=1; AJvYcCWyh9KEmRWfZbfVxOvwzQbHPFhkBx4dDnUmi8GrCN04NdmibdkoxlXZdAnOns5LFl3hvap5eqIqYgyQQR8=@vger.kernel.org X-Gm-Message-State: AOJu0YwLppPcbbSptavd+lsKztWF2PyaPVPNqthflWZR2pfDRWMLeuz7 bGYhog1LFAAC3u4dlvQesdt8+kUGC89I/pnjnvPRjd7vX2XFOBpae67nuu5Qn647AmQD2evVEQ/ eOT2qZWuR1Q== X-Received: from ilnm6.prod.google.com ([2002:a92:d706:0:b0:470:e9b1:e0fe]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:1792:b0:679:8b1d:ba0f with SMTP id 006d021491bc7-679c44ebb9emr7562956eaf.39.1771957544704; Tue, 24 Feb 2026 10:25:44 -0800 (PST) Date: Tue, 24 Feb 2026 18:25:32 +0000 In-Reply-To: <20260224182532.3914470-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: <20260224182532.3914470-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog Message-ID: <20260224182532.3914470-9-rananta@google.com> Subject: [PATCH v4 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 Reviewed-by: David Matlack --- tools/testing/selftests/vfio/Makefile | 1 + .../selftests/vfio/vfio_pci_sriov_uapi_test.c | 200 ++++++++++++++++++ 2 files changed, 201 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 120c9fdee5c09..f83b054ed329b 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -4,6 +4,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..9cfbecccb759f --- /dev/null +++ b/tools/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c @@ -0,0 +1,200 @@ +// SPDX-License-Identifier: GPL-2.0-only +#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 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); +} + +FIXTURE(vfio_pci_sriov_uapi_test) { + char *vf_bdf; +}; + +FIXTURE_SETUP(vfio_pci_sriov_uapi_test) +{ + char *vf_driver; + int nr_vfs; + + nr_vfs =3D sysfs_sriov_totalvfs_get(pf_bdf); + if (nr_vfs <=3D 0) + SKIP(return, "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) + SKIP(return, "SR-IOV already configured for the PF: %s\n", pf_bdf); + + /* Create only one VF for testing */ + sysfs_sriov_numvfs_set(pf_bdf, 1); + self->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(self->vf_bdf); + ASSERT_NE(vf_driver, NULL); + ASSERT_EQ(strcmp(vf_driver, "vfio-pci"), 0); + free(vf_driver); + + printf("Created 1 VF (%s) under the PF: %s\n", self->vf_bdf, pf_bdf); +} + +FIXTURE_TEARDOWN(vfio_pci_sriov_uapi_test) +{ + free(self->vf_bdf); + sysfs_sriov_numvfs_set(pf_bdf, 0); +} + +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); + vf =3D device_init(self->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); + device_cleanup(pf); + + vf =3D device_init(self->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); + vfio_device_set_vf_token(pf->fd, UUID_1); + + vf =3D device_init(self->vf_bdf, iommu, variant->vf_token, &ret); + + ASSERT_VF_CREATION(ret); + + device_cleanup(vf); + device_cleanup(pf); + iommu_cleanup(iommu); +} + +int main(int argc, char *argv[]) +{ + pf_bdf =3D vfio_selftests_get_bdf(&argc, argv); + return test_harness_run(argc, argv); +} --=20 2.53.0.414.gf7e9f6c205-goog