From nobody Sat Feb 7 15:40:37 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 05F2A156661 for ; Wed, 4 Feb 2026 01:01:04 +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=1770166866; cv=none; b=TpbkuSbRhZs/pPSml4SQ8UQE7uYPeLhijLgdnQnWyE4W31G/L63xzZAZnHMOMNySW5QrD1RfDeuvhnPJNX5eZGr4fTtuaL9ROyFaQ0hMWuyv0r4vWrwy1DQw1Fr8EzUP2Miw38wdWBHSsge7c5/SfMrDoSTNFhlzFJcBfqKdir0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770166866; c=relaxed/simple; bh=K2zIAuJBNri8ncpYokfo8/PgoablQcYF5/SefsPDxWs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gAvgfSa/qVDwwITgjQVsw/OX9Q6vDWMNwngVYhkE6pAubm3JU11iOu4oPIQlNwTiVi4BmGRs7s7/hZuG5+sheI8dwwtvwTqvZatj4kqymYpzqlap6vYnaP7H23/oWpa7wHlax9gc000clq9Y3B/GUWMEtx2nndAvUZsrIZsUzBU= 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=REdnQZxp; 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="REdnQZxp" Received: by mail-oa1-f73.google.com with SMTP id 586e51a60fabf-4081db82094so1273496fac.0 for ; Tue, 03 Feb 2026 17:01:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770166864; x=1770771664; 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=3UPnxndr62TXi74jS5ggLuZonBBoj400RRud0Xm5q3w=; b=REdnQZxpKJhqrpHh6jLnUbdoANS1YOhkDUhCiBMw01VBzdtfDoGp3YPCDWRu4KzA5B p03ylwnT1wXyBXsB5Uxv6Av72ysqUpsoUqgL49Zjvh3Wrnl7h/xgwLVRGGVe2F/WQ5+1 LqFT6gru/46hzZ7JxlnvWHXKGlDh2/ki4jthnnL1gDhpLWeCIb/r8OFtMQ4O8t5vobyM 23rSdYg4RD+Bb+RIE5+z44olsnuEr/GrPR9oLeUlFfBEyTyYFklAlMqg43KdyJEwLRwa VJjHk864gun/eeQVFrJfFp0XgdEyFHLnXbp6zQs9h4cozy7Mh7Pq1VtbIh6x3icbcWfy 3NVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770166864; x=1770771664; 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=3UPnxndr62TXi74jS5ggLuZonBBoj400RRud0Xm5q3w=; b=QDwqmKc4j+3M1gCnU43mQxBZ6jJ/LVl6XXG79BdXWaPxxeTdYng/y+HEyWGy8Fm7JR h9XZlxTGBMyun33jLw5paVHqvhG0qBQ6LAWpsQcNlonVsMDN4BrQN27KePd4NQSs1va/ R5MGCLulZm9bCpVkVM5ro0lCvDGIJo5DHL6VewbeG5Zu0DiucHfW8RS36iHWL0J+gRmM /r7EDTNhGBtZ8MsTLHRFrIZPhabUsDuCn/3hixq4sg4h5L17zu6exuL3m78cn/R1gnZZ ibAsZpZO/I5qlxzt5zFRRZbOGQoYfM0lKTzjvV6dfOGPaY4SOhNY/BPZ2j2NZlPVZIdH mRzg== X-Forwarded-Encrypted: i=1; AJvYcCVmKq0dMSnhrMaFwUWEYBOFbuGKuQDamVTcHNLyqI6A5Rt9PM+MJE8uld7x5mzwhMXEaD7o4i+gyuRhwM8=@vger.kernel.org X-Gm-Message-State: AOJu0YwvecrwrApIQYIfazOVJduRJrPpLNpQcvf4QLeuVCsL+Zr3db41 44+KP2WLG+AYn6zOi9bQCI5yY4HYMQVizIKZmnB7KL9TdvnmVXt9/oWYoD6fSLW5BgC33XgDH/A 3LX96oYJJdQ== X-Received: from iljw1.prod.google.com ([2002:a05:6e02:13e1:b0:449:39df:417b]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:4610:b0:662:c6ea:ceba with SMTP id 006d021491bc7-66a2113cb6bmr619875eaf.28.1770166863725; Tue, 03 Feb 2026 17:01:03 -0800 (PST) Date: Wed, 4 Feb 2026 01:00:50 +0000 In-Reply-To: <20260204010057.1079647-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: <20260204010057.1079647-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.rc2.204.g2597b5adb4-goog Message-ID: <20260204010057.1079647-2-rananta@google.com> Subject: [PATCH v3 1/8] vfio: selftests: Add -Wall and -Werror to the Makefile From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson , 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 3c796ca99a50..e8f9023cf247 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.rc2.204.g2597b5adb4-goog From nobody Sat Feb 7 15:40:37 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 D020157C9F for ; Wed, 4 Feb 2026 01:01:05 +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=1770166867; cv=none; b=rtTerzigyl5fMMTc7hcA81uUE24EZYtdstqY9JUowpKHna1gHVIaw1HaQ+B2FRx9UAh1ynayMfD9m0BwRpVRMJYVRkguuC2xJIlJIKmkwcQcuGA/OJeEcT+f4aoHh1+K8yX5RNnzaSuwSMICbMpfRhEoSI0btMFIke1BY9/uEmY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770166867; c=relaxed/simple; bh=VFxQIZn4TYxXpKKCGjEzt9avNpb8gXQuzJhThGmBsVE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hjg9LtOK+1TVxfMgqoATkvD1IQfCMr7djMBF8E8ni4tGIyH4/rM65XSBUN5hei2OjYkhFV9u+ch56JyhEMr/1yy5kOwen2vwOP/foQZe2HTre74e+2JyCnki+7XZjJAQcMffBBBKVq1SQmjHSbLpSAtlFHBWgzVNHTO0TaYTaPI= 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=pkiWk46X; 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="pkiWk46X" Received: by mail-oa1-f74.google.com with SMTP id 586e51a60fabf-409483fc17aso23879818fac.2 for ; Tue, 03 Feb 2026 17:01:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770166864; x=1770771664; 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=nCv44Flx4qZkEtd63Kgnsoh9YeyaQDnrLAILkWlfP7I=; b=pkiWk46XjMq7tUYcT3vEMgjUZfptLT1EX5CWNFFQK4hXhXRoddVYZBDGG8RvmCxjuG eC//CjtlhgYKkTK3FTJ/q1yCw0Dth9EiIMImjNNLrS4jaZwUiISxQxuBcQYsU6TAqGBx Vizg2OZCS2eLZNdw05MDD25UFw0xUdHaVtWxuwNCWzbL8l4OrZtUOw9UULRHQ0wT9cum Zj5rvKYS3wFzeB817wh+vPEed+HPO0H4lJv1HGarCQSt+7CqG/YWaDtSXiWQK4RZlM3/ JPpTpkgApWRIZt0GIO18M1voiHoxD0WyTHaUUGQ+TisskmD6p7sEPVDN9ZnUMyMqtwp3 YLoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770166864; x=1770771664; 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=nCv44Flx4qZkEtd63Kgnsoh9YeyaQDnrLAILkWlfP7I=; b=B1vstGMIUPS/B7XjBxG2j8PI1dygpfrjx9jpCD/cD7CGESsagSl2BNQ1sF15TPZzen 4Vhy5xWj9qsIWciKfAZThT2ZVzdlAguf3hll8OKo1q0j+dcFuI4OMJ3z7yb6WEnhJVGP fXkMPXQNzRMQ8q1PJW5wSG1AQo2yMYkrXZySpJSFhwP2nvkPEg7a14yu9QrYVa4QMb2r 0fj4guAJtufQD7R+eECs5UVpUrv1+oHMpVFkCxYdU89HqksAot+CJZf8naxSbzX7iLXp NrFO75U0ckzKvJ8jgirmjbpXOvYFfjS6oObKvLc7vFlpgTuNendOKLZr9pp5I/rqcMpf Va6g== X-Forwarded-Encrypted: i=1; AJvYcCU6RdD+ONyopjDGjhM6A9ipSJnf6ozofTiCtVNzL3gBBFMbMpmN5ELBaye6SWIr1GxyMMH1KmR6/sbwnyc=@vger.kernel.org X-Gm-Message-State: AOJu0YxQgEWdDiqvvmUmFeomHfRZci9QRqYt1qj+xgGD1Gori5hcWc5r Y2D/+fV/+cCG/cbwhpywQ1M4ECOX8aQgavkHHBWFTJshskN9ATB1lIZFACG53O6WM/vdWngMZv0 YIQM5btqqSA== X-Received: from jabio39.prod.google.com ([2002:a05:6638:8327:b0:5ca:f895:d02e]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:138f:b0:663:170:3b with SMTP id 006d021491bc7-66a23043abdmr795256eaf.55.1770166864623; Tue, 03 Feb 2026 17:01:04 -0800 (PST) Date: Wed, 4 Feb 2026 01:00:51 +0000 In-Reply-To: <20260204010057.1079647-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: <20260204010057.1079647-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.rc2.204.g2597b5adb4-goog Message-ID: <20260204010057.1079647-3-rananta@google.com> Subject: [PATCH v3 2/8] vfio: selftests: Introduce snprintf_assert() From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson , 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 f4ebd122d9b6..77b68c7129a6 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 13fdb4b0b10f..64a19481b734 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 5397822c3dd4..3a0bea5e2648 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 ecbb669b3765..723b56b485f6 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.rc2.204.g2597b5adb4-goog From nobody Sat Feb 7 15:40:37 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 88EA523C8C7 for ; Wed, 4 Feb 2026 01:01:06 +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=1770166868; cv=none; b=XfkqNa4W8Xad2zqT7gZcBNOL9bAhE979sB/fNpxLCQ/pAMANaxJDX2orGDeUK238Sfjt8H0YIiZS+nZpkAGrLBZ7LDP90yUAqDCCLKO+AxeEYfT3m5IDgKeDvw7BfcE7jy4AExZhhbWb738IVNoW7P3rlSMR2S+nVuay6zcDJA8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770166868; c=relaxed/simple; bh=dTEap82p0lWIHNXhLL0j9to/MOQV3JnfKe9u2JMrO2M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DBKYxvZt5iquRUzySiTjeG+J7yIhw6YwO31/Y773NMQuVsx7vuot9FyTrgdnlnEG6Y63hAYi9byPVBNAJnAzyMYzyopFNC/iuSN3+s/m6WkUEDPpSMezOz8Z42FdsRL7fydoPbLUOSaxB6xa9bd3yBIQLTX2eLL2f2Mve6Cod9c= 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=LfxMvSgB; 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="LfxMvSgB" Received: by mail-ot1-f73.google.com with SMTP id 46e09a7af769-7d18673d47aso18778658a34.2 for ; Tue, 03 Feb 2026 17:01:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770166865; x=1770771665; 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=IWH+g8vTvrydnuyLXC+vnU/OGm9oUti0fPKisjKukZY=; b=LfxMvSgBU1lqdgdYwiLzaI5gGLQzHFHn61hvpLry3jFjrGDrppkftnaOvZLtSDzOv2 eWA6lH7HplPXbzHei+UKbHz9eT3rEBiI5hNi6YYYZohIYDESPiuZ4nI9AaZw5RKDCuiW g6vh0CFx9x9Igb2gbXC8qmPKJMt2ZbtXqclBVlpcAJ1skai4KwHopnmsLW0mYg/yBt1r bhElPscvSGqBPu79zMpdLiPXHDrOa9A8D8RvS6s+raguJvVuZWEqgEwxESq1/TRzXC5D kfMKJXNviONmgTCWOLnp/exmyku/9bfbMuI3/FNJS/28ESozMxUDzu66iFPCEbYcWk93 wp8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770166865; x=1770771665; 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=IWH+g8vTvrydnuyLXC+vnU/OGm9oUti0fPKisjKukZY=; b=DGFA7aQTIeoxsky7lbYXPGI80IOgJVQD8okL3g0Teo+WemZQRS4h4n5VWkqqez/HdU S+6YccQEYTnxOYzt6b4sM5885pEBxObZFCeL5+ykKYAMnUE+oviqNJ4UR03QgBij/F71 XDWBCw6AKRryWAUStDxYm/aVwAvhdc6xyQuxFgeIXXdYlaYXuxx4qPKh6NqElQsmzX9d VtfF8ElybgddriFtbzqMObaoYy5WGsfr4YUxm8KIfj/8YDfFfPadwWHS+J4jKITaK7Xz nj00tINzY/IYz7PuAzvFAXZb47QaM1wO1PxDXEsmzPjUMp+Phu8ffFUoyx5QmKbf+U2O SpOg== X-Forwarded-Encrypted: i=1; AJvYcCV6uMuBPooVkJH58QFGKw5XhMpbel/pGF1CgzVLGsunJFF+wux2QN/6pSU0+/drihpEq4oYjwQZQas1A4E=@vger.kernel.org X-Gm-Message-State: AOJu0YzfqX6KdMOW7nUt2kEsJQNlrCZ2fTTcHsRV7DYdfMig389HOHlY RlCqzjtDlgofHG+GWpXEreOZ+hrTOynWKwxZiWQleMMLDAslsZXaTt3KAerGGuTuVqWgwqYqEsW sZnhuqM9ROQ== X-Received: from iolk1.prod.google.com ([2002:a5d:8741:0:b0:95f:3071:6133]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:f04:b0:669:fb20:e9e7 with SMTP id 006d021491bc7-66a2113abf1mr760101eaf.32.1770166865450; Tue, 03 Feb 2026 17:01:05 -0800 (PST) Date: Wed, 4 Feb 2026 01:00:52 +0000 In-Reply-To: <20260204010057.1079647-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: <20260204010057.1079647-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.rc2.204.g2597b5adb4-goog Message-ID: <20260204010057.1079647-4-rananta@google.com> Subject: [PATCH v3 3/8] vfio: selftests: Introduce a sysfs lib From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson , 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 | 136 ++++++++++++++++++ .../selftests/vfio/lib/vfio_pci_device.c | 22 +-- 5 files changed, 151 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 279ddcd70194..bbe1d7616a64 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 000000000000..c48d5ef00ba6 --- /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 9f47bceed16f..b7857319c3f1 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 000000000000..f01598ff15d7 --- /dev/null +++ b/tools/testing/selftests/vfio/lib/sysfs.c @@ -0,0 +1,136 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include + +#include + +static int sysfs_val_get(const char *component, const char *name, + const char *file) +{ + char path[PATH_MAX]; + char buf[32]; + 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); + + return strtol(buf, NULL, 0); +} + +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("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 64a19481b734..a7e00d017fc6 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.rc2.204.g2597b5adb4-goog From nobody Sat Feb 7 15:40:37 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 9A87A246797 for ; Wed, 4 Feb 2026 01:01:07 +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=1770166869; cv=none; b=RR0wemHtP3xjIhTtQZF3Zab4I5yrEx4ny0t5+tJ58f0685tXVaLd28B5rlFAaqtmB3jizBtnQ66EwRLPAxyx0DEZEiTzjYSopu1/qfW2bcmCrFRIuu2jMzVSdlGjiZ+LGptfjDLA9d4XJWHKR9bKSi4wsjM+zGd6ESTD+D5t2zs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770166869; c=relaxed/simple; bh=meRWmIXrY31FrBFGb8YW9mRyiEhUcftdtXX5LNfWTdo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qpXrPXrPIXI7YgS942wBUNQ3YF5d7bNcFCNdXlBnSvCUTDr4WZTHhvwDApm1NraAduSdn0bQxaajmtKy6GLH0VA4gi7hTcaSDMoq2Ohg/3pSv3SMLzDJtzIE5OkEPo1evlCsehDeF8FjdaDBFSJjVTqGGgrHKRokVYvzAQwcChU= 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=S5w9sgOK; 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="S5w9sgOK" Received: by mail-oo1-f73.google.com with SMTP id 006d021491bc7-6640dc1ca05so17443686eaf.1 for ; Tue, 03 Feb 2026 17:01:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770166866; x=1770771666; 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=9PsrwmgPvoL3V2W1GGLMcJD/aNZYUylwYoW0T18FZqw=; b=S5w9sgOK3pJ5wEqsb3fsZ6i0+DUDF+s9aP8G8uEO+IKjeai2S8xuWjaBvv+nWjGozb AU2h8QVwJG77vuAepCor0bnBdFD9oiE3ZjGY3x8w23ywF9HSq1a5srxVj5slEqij6WxX ijnip9dthxZ6ldboDhLq+bES5urq/CkuB0ks0l22CqKvXYXiR9Q9TjRX36DeB4rYp1/3 d4x14RNCl42H2Zqa6nW0quBCJ3aK3miSzwY3iHfrGuXmaO2S9AIdOXRWbWRb8AjQSJvt vMdkMVuOiJiKMmvsxtJDUyp701E9CwFlvrJdwyRl73M5AcMzkLYw0wzTi2XyTYVJaFJo dR1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770166866; x=1770771666; 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=9PsrwmgPvoL3V2W1GGLMcJD/aNZYUylwYoW0T18FZqw=; b=VrZcPwV4i0GByj+evjHa8x/zmcnt5SIM6SooNkP9accokJEB7bqt2EuOiOgd6cS5l4 O/4r2aAAiOU9giEEv9RYE27DyC+Y/Z6QaDuwMlbaltXkhOC6OM+4221p4mkZqKtQe85T cnJMBIqNWzhpv0Mpfn2WBqOhb9GkseAgGmc3tnSr9tBb0x4gbI4yDgJb3IDonPPfOEw2 Dl6A3ryuLJkiO6sKHwiVCHEEtL0flNWRecIFQQLAHtpdJTQZ5RJ5HvXqMzHOr0vdLC2D 3RQ+9fW3wSfQsRjawKqVwoELnVzYKiVaucYKkV5jcC0YkC+7DmAx6G5IDpZbhOD5SwKZ EvOA== X-Forwarded-Encrypted: i=1; AJvYcCU3W/2YywK5wBscE/FN+NQf7jVtMgcx+N+xvtJ7pQ/cUp0ERRvtW4kkSgn+M77GVb9Gx7tdr4PJTP/ZI7s=@vger.kernel.org X-Gm-Message-State: AOJu0YzufZ0npoJsz8A3xNuH+R2QOLedanf/knwKK7GTc1CPakDZcTGs RYImjeBQqVdRNXTSYSxOCy9TQUZcPFCx6k/XXVVa7efRTqWvGCnKDD4FyjquKgFckatzTgUUyyH CutIf6UGmNA== X-Received: from ioda14-n1.prod.google.com ([2002:a05:6602:204e:10b0:957:4ce0:f83c]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:4a10:b0:663:c86:e9de with SMTP id 006d021491bc7-66a203a4c11mr550864eaf.20.1770166866231; Tue, 03 Feb 2026 17:01:06 -0800 (PST) Date: Wed, 4 Feb 2026 01:00:53 +0000 In-Reply-To: <20260204010057.1079647-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: <20260204010057.1079647-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.rc2.204.g2597b5adb4-goog Message-ID: <20260204010057.1079647-5-rananta@google.com> Subject: [PATCH v3 4/8] vfio: selftests: Extend container/iommufd setup for passing vf_token From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson , 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 e8f9023cf247..120c9fdee5c0 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 a7e00d017fc6..4c79557cc4e0 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.rc2.204.g2597b5adb4-goog From nobody Sat Feb 7 15:40:37 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 9F07425A2CF for ; Wed, 4 Feb 2026 01:01: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=1770166870; cv=none; b=SQOdm/WRmZOb7Njzlyd1KkI9R0jXN6BZLfkikuf9FPatymfkeq7lj7pZ1CX/gimSZ8KVUQZ3Tf3QefRukmZsH0F2HBYU0P7kFihPMzcowj1Bzhv+BSklrjnd0TuzwaFqHi5oit+CGNV5JJR/miJw5y5/qYVFeFuajVcDDvWnI2s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770166870; c=relaxed/simple; bh=+vdQEGyCwSDq42HprLYyZAxVP9gA4Sg0HO4ld5kKHfs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PC297I2Fvv2LIy2Px6uB2v9Vn5XVGRgoOTdTmfwUQA/0TIGko/tZskMDnnwPNLuozuBTsY7BIKN8hppewthD+JwtbJnqCBR0SUidRxiBfLWkzIJoLCgVuyiI4Z8UOj5+lQOJlCp3KmDQPnQv6DZRHgni9G4NiwP+Uhrlw2ET6KU= 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=2CSZt8Ch; 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="2CSZt8Ch" Received: by mail-oa1-f73.google.com with SMTP id 586e51a60fabf-40418f60513so26869797fac.2 for ; Tue, 03 Feb 2026 17:01:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770166867; x=1770771667; 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=0udRq4j88lomrvASezCNuuV2zCz1GvvKR3IJOID3phM=; b=2CSZt8ChnE/IHXY0bUrS8EYfOV6q4VE7+DjNT4CB889byXiEud5g4MjRrqPS1leYaL P3p/tIgeUYPI945pm55E6YRHKI2K19wKp4T5cHJaIt4RcBHH69rG9w/RdnYb+x4FyeVq e6deu44qT1l9vBMoDT/MoEyjFVpZAOwtdp+vkRLTbWF4SKdveTPqD098ceLwu1XNQ8Ul qgV0/C4DG88mgFCra4G4L9C6HtqjI0jX2rAk7UA/VXA1MMwyFJpb9cf1C3kUcBzAfeDZ C9Uty/xKkHiSF54uo8H/BwPR1MlkpvxG4CM29vqlOhOzSG5+qKpriGNp/cIHrkeJroCw IR6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770166867; x=1770771667; 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=0udRq4j88lomrvASezCNuuV2zCz1GvvKR3IJOID3phM=; b=nY5/EfMZmV6llQ9aF+p5s1VH7db+rcwVuNBZ9QnETadVX9lZTeadhv/6Y1NSNca56L K+CtntrmsdKIA7Ox3gRx4HP752gCXWVeIR8B9sJw5ZkmEJ3ckyK3+alw31FdUjIaF3PL 5rHsUSvrL4plNq4ERinPRyQnbzHE5sWOuUZ7MlKpme5DK12A7aAjxS9DQEe5kUyAwGx/ 0p/oMAxjOIolTrWaNiRgD/pmy5M1FFIgmBvjJgBNr0tnUbYxF2/GF7qFYdI46YaStHJy 6/wRxtY2sxGAsNWapK+OlzyF7UpDyu5WNtPs6dWwFhBzRz5r2QBTQoyYBayKT9ZNE6FP kegw== X-Forwarded-Encrypted: i=1; AJvYcCW2phbBKPFq+k8kkG6aI2gqBfmQsdcRWXPWs9vVj7E628Des32tO8krHwHSF72WimZBty6IGQegUMI//EQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yz2X2Ve/xVjT/hC/qq0gSLBcJuKCknrrCLXV9+Z5UXUD7/4lbqp 1gme3Dq2XT+o1YmLdGRQqAbJEXnmFRnC+1B6d5EdmjIlOQ/YwR/b6OsS7ITssiIyuO2H120hUJc eXGlgz5N5yg== X-Received: from iock2.prod.google.com ([2002:a6b:6f02:0:b0:957:7451:a858]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:602:b0:659:9a49:8fd9 with SMTP id 006d021491bc7-66a23df34aemr676968eaf.82.1770166867191; Tue, 03 Feb 2026 17:01:07 -0800 (PST) Date: Wed, 4 Feb 2026 01:00:54 +0000 In-Reply-To: <20260204010057.1079647-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: <20260204010057.1079647-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.rc2.204.g2597b5adb4-goog Message-ID: <20260204010057.1079647-6-rananta@google.com> Subject: [PATCH v3 5/8] vfio: selftests: Expose more vfio_pci_device functions From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson , 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 2858885a89bb..898de032fed5 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 4c79557cc4e0..142b13a77ab8 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.rc2.204.g2597b5adb4-goog From nobody Sat Feb 7 15:40:37 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 96EE926AA93 for ; Wed, 4 Feb 2026 01:01:09 +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=1770166871; cv=none; b=WzPYbHoEAO52wQ3SqgEV50ZytonrCyokZGbpCwT615VIeFQpLg3LWfYfFc3oAxqRqKkMxb25cnFuBc6/geMstzUXTHU6Z4RrSOe5MXcfMhU/jOKVgnDmWOLGDSg49tKyZ9mSkNLvcBFmdpKN3A1/PSSfC2ZQSYINvmRJtKq7GjU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770166871; c=relaxed/simple; bh=7YkkwT8Ex1if3r5ZOMQl6e11dL3Xs5KyYdtiDRYnHq8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=t3dfQ9lB2DL0l++8x48VF+F7LPh05iIxfDXLUlJodtoF/Q+4cq7B4Ul+UH7/kd1WYpI1uI/1GrjlL36Ixr3Y2CHcg5wM70tiKUuH/ceG+GJhlXdpXX7hfBDu3hhiFxLfzfvB59GLBcOCPXigE0Fj1gRLK27g0o4SnbDu4+VQHJs= 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=OYBShe2A; 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="OYBShe2A" Received: by mail-ot1-f74.google.com with SMTP id 46e09a7af769-7d18e95c658so26261307a34.2 for ; Tue, 03 Feb 2026 17:01:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770166868; x=1770771668; 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=mnxn6qhPWGoTsS1DGhblGS2iCQnfBI1ygyGZY7nd+fI=; b=OYBShe2ACMi0I26CJlFrBOckyiXvlA/M+Tt75d/rnJRnUWJIf4anbinz4mm8t4NQvN bv5zeBH0lRKXyP+lfkkoT+4bGQ+Ka7QmSVdUqs+evFNFyKwi5fCp9mXopHuUX+NQxafN xbp82hxMPaA125SCviDsDoPRdWeh2I4Y0MD5HwK60H0Jd/adM90ReCiGZPwQSBs9EpJ3 xNdvmROs8YXi6zH+/6WWkUzcOWxPU8cx436DBj3myWW8PpXFs/VvrQe/3/oQEQeX50Pm 6D8m95vO+i64cQj4Ju3sdyv/k/iP7nX33ItLhUM/pn2aciM7jHVGnhe2lAIaN0b5CONx bH0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770166868; x=1770771668; 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=mnxn6qhPWGoTsS1DGhblGS2iCQnfBI1ygyGZY7nd+fI=; b=m+uSlQGPI/zjTfB6M+8WJUDUwqUzeQo7RPjlt66+Fr3aUmN23wbSsQTTVR8QwPH/Rg oWtc/QBBUY0Oax6SvQppuI6F3SL59blShtaGgmmWgoI+PpglK1Pfu/xFJ9NbJzIGOcXN jyTfmsLuMUzVEfHuF8UfVKTGMT5vGFUHA76wL5UBr2mUH2BA9JruiC++UXzpEnkDFfXG +fN7HCNflAQt/0yCGml7F5SQDDvk/RyV32uLl9p1MKyI5PprbsFZnaDPpw7s6XzkqFPn zrDSMNKuSW7DR75WwpOnhiQQGqn6d1ur6tYnBNI8bKUB+Q2NAt9nwQt/ZHtLCuFDPUue hKDg== X-Forwarded-Encrypted: i=1; AJvYcCW+yjBAu7gg9M6sXGsVT1C00Tw+922q6xleELafPOwNTU6D6wMoRST+IDfskKiQ7IxFZ6bnwPc/snMxlZM=@vger.kernel.org X-Gm-Message-State: AOJu0YziP0GclhNXnyIJNDpyIj2Hrx8uDQtyOtKLVTRj7hpNcosFDtu3 1fcBaH4ch/wPTCH76XoXK+jVFNbpOR+HtcnPLVO9R8qlTTHJscHIwLmlFr5gCeEYYNn8powhHaj B4XKmUZIDDA== X-Received: from iohm24.prod.google.com ([2002:a6b:f318:0:b0:957:66a7:af96]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a4a:e844:0:b0:662:f763:c52b with SMTP id 006d021491bc7-66a20592891mr726381eaf.9.1770166868323; Tue, 03 Feb 2026 17:01:08 -0800 (PST) Date: Wed, 4 Feb 2026 01:00:55 +0000 In-Reply-To: <20260204010057.1079647-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: <20260204010057.1079647-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.rc2.204.g2597b5adb4-goog Message-ID: <20260204010057.1079647-7-rananta@google.com> Subject: [PATCH v3 6/8] vfio: selftests: Add helper to set/override a vf_token From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson , 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 898de032fed5..4ebdc00e20fc 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 142b13a77ab8..f15065f53156 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.rc2.204.g2597b5adb4-goog From nobody Sat Feb 7 15:40:37 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 646BE26ED45 for ; Wed, 4 Feb 2026 01:01:10 +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=1770166872; cv=none; b=NbNJvlAYi9w0pR6TrtAyuZ/9dqAJh/D/ap4BIIzozG1SdvWGYqVWpwdifv586qfHLq+4CKijwgNqcejGeUdrcWc53123QPDNauz5iGvH+4bvtSgExqLvfM56iXc/ZJbfSnyKyC/p/JbrQpDfhQDQsXaA0FYc7fSTA+mfNY9C2xE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770166872; c=relaxed/simple; bh=0xtJ+pqCo/uE1p9MpFM/9m9xzVYuLjsVR39VzqvKZ/c=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XQa2NlAlvTNqsJ2ziRrHbsrIekl1gzVqUVOQWSnNNlzZ0geOI/d41MdX+me7S8/jP6/N7yr4NNp6fZqkQB59NwbJE1ZM+zdyYanvFWfVuJOLKX7a0lZkiBziipQlst7h9LKRoqXUQzmsVnVOP7KQbicS6Vu+QhvF+9k+3toPacs= 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=mB2jinRy; 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="mB2jinRy" Received: by mail-oa1-f73.google.com with SMTP id 586e51a60fabf-40a585edd8aso648958fac.3 for ; Tue, 03 Feb 2026 17:01:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770166869; x=1770771669; 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=A3FiC1IXjLa+0JqDoxdMPEgWbheV2hH/CBO7B1cQRs0=; b=mB2jinRyCejaTJu333RBfbMRNG5YUZ/QHtE26tp4VH28gJRxDf5hCZez80dFO49PwQ FtYISwNOsgZ8bRko+IzcJ7xOE+BrYwzvuF5JXoYNyzKspKzVcO3zHE2OPJ9ijRU4kPtK lFeuycyjChm3dRgtsb9NaaDf4B4d4BrLGIv1QHFaZsyb42tmvCi25O7v083u8oFOom3z YT2pok9NwjvOms/6tT2cZ+v86MO9wZk7wt/TOa5YqIPn6d96VVquHxHprbejkbQKyr7G dW4z4wGL5v5VHz4AX69kmAxGJ5NBfDnbXHyvWFgStlGNMtV4V9zOh3V2I3Advbnl31g/ 9l+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770166869; x=1770771669; 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=A3FiC1IXjLa+0JqDoxdMPEgWbheV2hH/CBO7B1cQRs0=; b=mDXj6gKEz5nsweoqcVwDUM+x3mhmbd4rPf4ZBpi0vvddDgwOUe/JeHf7SOfm8je3lk w9BrQvINAEMYGYpU/+WC1l1wOjTy9rBPTp2k/bxaYNDEWIfoXw8fPHqVPzlK/X1kGC7u Th/s15U4/uRM65+w9v3BCOeky/K1f+jAVQQXNjCioljPqzhhYBxn6/oQHJEytHkZ9kYZ hvKc3lCe+rrZLJBkjLFgUAH2pgvkk4CbQLv8xJ1dKF1Wnm83t8U23+0SfVXy/N5NxWMh LcG12YVCvnPYJi67pYHE4xrNUcLN/8a3CHlDbhGXelmOR80hpsKnUKDPSxp7bHfsr2kZ /NvQ== X-Forwarded-Encrypted: i=1; AJvYcCXZho+KmeywAtzbUnGrwtw6/YtQCR6LniJIcynOmb8MtNM1lxlEc1tSJ9EfqUAm2x86t2nRflyuBDNCbdQ=@vger.kernel.org X-Gm-Message-State: AOJu0YwTJhR2STM8k+37Tv0e1z5a2kxAdk1E/qOPrd31X6F1sfhAeIzM oNp8HOZmI0VTRxk0JHkWpBFLWcexjCEIR01p5/zp0rSpV28bRk5DccgvjmLLNisT8Pvx1uD9+yD JbPiAnqiQJQ== X-Received: from oagg5-n2.prod.google.com ([2002:a05:6871:5:20b0:404:63d0:3ed2]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6870:400d:b0:40a:5f51:c9e5 with SMTP id 586e51a60fabf-40a5f522243mr32311fac.42.1770166869395; Tue, 03 Feb 2026 17:01:09 -0800 (PST) Date: Wed, 4 Feb 2026 01:00:56 +0000 In-Reply-To: <20260204010057.1079647-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: <20260204010057.1079647-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.rc2.204.g2597b5adb4-goog Message-ID: <20260204010057.1079647-8-rananta@google.com> Subject: [PATCH v3 7/8] vfio: selftests: Add helpers to alloc/free vfio_pci_device From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson , 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. 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 4ebdc00e20fc..3eabead717bb 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 f15065f53156..6f3f65021bd6 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.rc2.204.g2597b5adb4-goog From nobody Sat Feb 7 15:40:37 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 A8766277C86 for ; Wed, 4 Feb 2026 01:01:11 +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=1770166873; cv=none; b=Ufg18Uc8YztykZPTKDlaS6ktgZCF36JBOeHTo2KD0jSA3ZxsPyDmsEMCC2DG6qeRfvWsM9lq0P1AeSCRXlUnaZmzL+1XI9Zux82RZ7qIcxyysr7JP8TGtod3f+htprcr/sCKAo4zy/ykQb0YU9zExC8Iwxt5fxhuu8T8VoyREDo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770166873; c=relaxed/simple; bh=9iQwlJIHXJlRgSNvrGLR8wXlFf/oJocttagfEKyaD4I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OrQSrVihJN3yHuRkSbXK+9quE4oNvqiAgVfBXV2wlfyeCcrYIoN74OvM2931gFyPsD9b7tScjxQ+Xuru98OvbzEAg1T4daMC2mknTYkXMZE8GuGzskoGgb3S1gmXAeq8xwINSLithtV06xkyehff1fMZtQFdKS1KqFbI0vWyTSY= 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=oKO2PWN1; 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="oKO2PWN1" Received: by mail-oo1-f74.google.com with SMTP id 006d021491bc7-66308f16ea0so22619666eaf.0 for ; Tue, 03 Feb 2026 17:01:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770166870; x=1770771670; 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=BiyvR40w7AY7Ci9X5DwiDavarBL0gnsawVId6L6rv0E=; b=oKO2PWN1+irge1WDGyqzrkiw+4ZhwbMQKQSKe5vWY+dmdahLaNA5dxr1pJjuWYAZok kq4cTxswhyYCB+754TW/fTtP4uNsiBTpdITNAjxj6FsPUj5ij9T0B90quyAe5x6YFeCt NQkxV2GX3kXrpNKxX2+wqWORng47CcpMb2nGM/KfU6ftkhuNrPw1R7Hnozc/2MoBSE2Z igKi45cuAktD6oN5ZBK20mvsBRRjsAd7tH/nI+4tHig59jAftKhjUmKqCYDUM+eBD8nR ccRyBj5G9a122KHA12bUZiOIIZ7TKVVU/sFVYo0f5VlYH0gxVGLaKT+CQYakmSTTXuT4 /NPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770166870; x=1770771670; 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=BiyvR40w7AY7Ci9X5DwiDavarBL0gnsawVId6L6rv0E=; b=ILwIrTri0MT/1hyzMiBh8dQyGGPuVLWdacc0tSdHW+P7+qlZOpiefzRTnPjKzMv7h7 nqQV24yG0/LQeCj33Nl8AleMni/dP+bzO1K2MxD1h6wTR1aOm1UGTp2cV2myN8EJxr3c yNmJJ1oKASNdNo+qQIyQ6DhjhxEP7mBSCPQMFsDBpArQlIMA+0alfGc2tS1FqceGHBVF 51pZoiZBIFcHiF4E0v+R4aH/oTt4hl4MpfGXC19wxCeewl9/WjOkPZ6toX4RAzJm9vKf v2pE5UIpPVe261Ro82fLm6OHhJNMP/Q8bUlEKwOJhSI4SguysVpME+NW4N6k5FiKVcKD R7mA== X-Forwarded-Encrypted: i=1; AJvYcCVU0hb2vyVK8MXQUBUNh/x9tYGrCtyPnU9y55YMMTRaFOY6RcJIY88MhZGzbim+csqMd6/4HFAlrZNRHQU=@vger.kernel.org X-Gm-Message-State: AOJu0YxQDxZcCNmd/jdwgB+ixc3H8+vDTJYnnrnWWtNehnRJCbEEOYAn vye16vOItnvcwP4nZpJsW6rNB9IjTCSoXWd9psdIlEQKZopwqlVdaPBzgH5B67kM0r99l0WaDk8 7zUv/c5MUzg== X-Received: from iobgw7.prod.google.com ([2002:a05:6602:65c7:b0:95a:49ea:e587]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a4a:e844:0:b0:662:c6ea:ceb3 with SMTP id 006d021491bc7-66a20882561mr573596eaf.33.1770166870417; Tue, 03 Feb 2026 17:01:10 -0800 (PST) Date: Wed, 4 Feb 2026 01:00:57 +0000 In-Reply-To: <20260204010057.1079647-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: <20260204010057.1079647-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.rc2.204.g2597b5adb4-goog Message-ID: <20260204010057.1079647-9-rananta@google.com> Subject: [PATCH v3 8/8] vfio: selftests: Add tests to validate SR-IOV UAPI From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson , 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 120c9fdee5c0..f83b054ed329 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 000000000000..9cfbecccb759 --- /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.rc2.204.g2597b5adb4-goog