From nobody Thu Apr 2 06:10:00 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 D85823976A1 for ; Tue, 3 Mar 2026 19:38:29 +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=1772566711; cv=none; b=cTQYF1u8JafufslHeHOrvvBdG/6yT/KTdv9aoEOuPku1DKB6wZzGq4crGSbdnLI9tX6NBRcSUMxrtx3EgZz7oTXbilw/ohXnqzZdhh7QJq2DzsL2mYwiV+OdOjSvEhjS7+blvsb8ZVunbmI6Wu0g5ja4KEeSDmMM/j6LDoyRihA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772566711; c=relaxed/simple; bh=jjqO7YZCeRuBMZ1mewA6ckPXJHycFjyo86Cvx8Zot6k=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WhD/kuJkBpLEQs7nL/d6zV0gVZh3GQmssT79mJv4lIHlbfmXfoTVZ8A8rPIRh9QcM7N+ZFcP//DUidqWQCcBevC0wpmij1nTLsFE9MSqpuk6qJ/HzsGTp3es95CND3O05gcpyakei4rQeZks0swQ+V58S9uwqZOceup0BR/aFjY= 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=B4hP5t4d; 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="B4hP5t4d" Received: by mail-oo1-f73.google.com with SMTP id 006d021491bc7-6799654a158so50225122eaf.0 for ; Tue, 03 Mar 2026 11:38:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772566709; x=1773171509; 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=bd3WQNQbvy17FPuSWSlePhR5MFAZI/OI9QfmsSTSzes=; b=B4hP5t4d8SMhp1zVlvhhLlCT0np0GP2IwMGEippf1SDDbUqvSk9qUAz6ufiNSdJyyp /6BCja5hlMV5J4Mj7J3BoQEspB4+jlRug5U7nHjfDNo3H3D9yZv5oL/2PV7krbUr42hM EfAFfEY1UpLK6nRzQt9NaUYVbsp3s82cacTkkmkYyxlXkdJyZkQO8cm8u6EMC6uXeuVG 3j7nX5QYaO9MeML5GoETETSwPfOPEzlzbH2D83t0yrf10r3SEeXFE3VolGcNwOAWIyoO lNZdTghFMpT5I+4kQND7P80YLHhK5naL/7scli7TI69gWNNBLwTaBhyz1L4SJ40usMBe Mh1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772566709; x=1773171509; 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=bd3WQNQbvy17FPuSWSlePhR5MFAZI/OI9QfmsSTSzes=; b=Njaw8ZL+xWi3c8QvWobLa4ljC2iaAC/b+Quw8QP25ir0XzmW5VbGkmy/87wLVYfzoc FoCyW09M7k7d34go+FQBp1GLyVZfE6ENpoGFnjGEe761o9NOYvYxjwSLbKR7WpR81Ezs eimPyCvhyv/0YYQn39fNlEt9khwNdJIPtpOTBTDPOJOijsUQy2gGX6o0/diGiqYeehH6 KEFu3ylYCc9h9VAgmEM+vEIz2x0nVFOZeq1qgcrmdjQ0YlQfWaERZtQbEB2TJrqGhgF3 iWGEaNsLFgqhC9POuxPfZNNWqZF4hzBtFvtAoVhDn9FK49u4gn38fZ4htWwThCl4bSv9 72rA== X-Forwarded-Encrypted: i=1; AJvYcCWiH1N/cp3Qf8Nex1OvG7CvSclWdt4QLbBqYqdscqpTDxNDNKqQmh/BKxsPzRqyvdq14uwuBR44zzAnhO4=@vger.kernel.org X-Gm-Message-State: AOJu0YxHKECHqWXvUA7aGJqSLgxnSGsNoUmfeR1Vbvxb7ZlEl8tGqRIL kTuEOhgRNtQ/gKQd47fUQ1WzxORSJMEDJ17/k/qA+aM2VdCTz2kvZwqFgSf7Nvcom8b9zXgWewX eCr4sGkcNRQ== X-Received: from jabls22.prod.google.com ([2002:a05:6638:acd6:b0:5ce:b671:f1bf]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:1b0f:b0:679:e4c0:e022 with SMTP id 006d021491bc7-679faf3e904mr8235457eaf.52.1772566708525; Tue, 03 Mar 2026 11:38:28 -0800 (PST) Date: Tue, 3 Mar 2026 19:38:15 +0000 In-Reply-To: <20260303193822.2526335-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: <20260303193822.2526335-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog Message-ID: <20260303193822.2526335-2-rananta@google.com> Subject: [PATCH v6 1/8] vfio: selftests: Add -Wall and -Werror to the Makefile From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson Cc: Vipin Sharma , Josh Hilke , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Raghavendra Rao Ananta Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add the compiler flags, -Wall and -Werror, to catch all the build warnings and flag them as a build error, respectively. This is to ensure that no obvious programmer errors are introduced. We can add -Wno-* flags in the future to ignore specific warnings as necesasry. Signed-off-by: Raghavendra Rao Ananta Reviewed-by: David Matlack --- tools/testing/selftests/vfio/Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/testing/selftests/vfio/Makefile b/tools/testing/selftest= s/vfio/Makefile index 8e90e409e91d8..6a9ac6dd32cb6 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -23,6 +23,7 @@ include lib/libvfio.mk =20 CFLAGS +=3D -I$(top_srcdir)/tools/include CFLAGS +=3D -MD +CFLAGS +=3D -Wall -Werror CFLAGS +=3D $(EXTRA_CFLAGS) =20 LDFLAGS +=3D -pthread --=20 2.53.0.473.g4a7958ca14-goog From nobody Thu Apr 2 06:10:00 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 242F339769D for ; Tue, 3 Mar 2026 19:38:30 +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=1772566713; cv=none; b=BtytrGYtaK7P9E/yP5JCGqRNn93iYy9ZZMJ3aY8UgTgCRPaJyDv79mqi23NOSO8mc2wg4oakR97gNxn+xYDTdkjqLvrz82Vm5+pzyEGYCz4XuhHGg/hHNwiLc/CyiVDhUKiMrMc8/QSbv8M98T1w1/vdC3ygcWFZc6NXMePqXo8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772566713; c=relaxed/simple; bh=BjDIIe88AEsckrtwAjj93CmAX57GI3uuDWU+eakcZrQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OwiPerxjd8lyh2S/g1qH4SO7lr6GDgzZs+rmdoHTEuXdTCruWoV4o11lAEwSkMxIqKLRWZSEsRcfrV7P6b/c8xrJIvKFDW1SGKPT220gTLnezYXA0t4kH7gQuctMu/LxKlDcoNnXW5RBOUJQF/ck4pb88J5t9r2JtsokpaTxlUw= 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=WM4Oq/RD; 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="WM4Oq/RD" Received: by mail-oo1-f74.google.com with SMTP id 006d021491bc7-672c40f3873so109842638eaf.2 for ; Tue, 03 Mar 2026 11:38:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772566710; x=1773171510; 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=wjWzLo2XlCuyIDdW74QDqar0cCQChKUuTAH2m9cJEjM=; b=WM4Oq/RDzRMfliaOa/8fOEudwv/BZK/LYMBe2VgEoiKWx4fLJU+bmvPpgT4k7DQwtN nl0KqwzYRaBAa5hNwBslUo9qFVfz6i3LauamUj7Y/62d9veQjhJNYt3+32U+SHOOzML8 O8TzdQ2BK3BGwz50vQcqtdByU3kOCX91r28LpBa2zsrAesasV2tpS/NdZruJyIGlMWtl lsggJzWSwGE6OEhkLvq5mReQ5kTBJFza9YCFmCETCcY2lRllquBysNHZdbYBa2MKskXo xj0InyYKHtg46EZqV76LwL1lyBXVB6O1Ww8E0PE0SYJjFXoaYJ+omSnv+TZUsylG8AEv QVhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772566710; x=1773171510; 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=wjWzLo2XlCuyIDdW74QDqar0cCQChKUuTAH2m9cJEjM=; b=jfFiW62dwLnjgRl0vjB8kRx7a8l9ISPruXZ0Zs5d7oq4EiR84BPf7AvVq9yrg8k0xA 9r9eHciX90rZb4iw1ErOHUTaKkRzXVGmHjoyjvKVs40qpZOH4WB4gmWN4sC7CPiMUAtb QF0YlQmrDbfwaHFuCtcuWCTz0tBWafs96fKR4b0twf7BgtUnY8hTKEETTP01xgP+3mX8 sYC/gYMxig4kSQPSxF/sO8OmYwW0Y1yPCcJ6YMVMHPtfMet8Sr0X/e/jztDDYgZ1E9lr gZ7kD6zb4BD+UaWpANaHSVj5nHD9av31RrD/HXNpEsZBInvsCqqTEq7D0YROZUcDzbIH piOQ== X-Forwarded-Encrypted: i=1; AJvYcCV4dgrTKQnURJumUD4MV/cfr5R064rEoguoQ+l0K/SaKPNFtfGRkVjN3L15P+WpJ+gF4GHSQheKmmDMzxM=@vger.kernel.org X-Gm-Message-State: AOJu0YzOUNfPT/mHfDEPgduHYNgx7q6/IXoaIejhYQDQlReR+WtPyJ4n eEZy6efAOiKptg4nRb+oKZsiKmzU5+GIPV47viiS35pKPmaCySlxvmEnTg4NXUw34SHBrtFYKJs lUUf/StFP7g== X-Received: from ilkg5.prod.google.com ([2002:a92:c7c5:0:b0:48b:c9f5:33ef]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:2107:b0:679:7aed:9529 with SMTP id 006d021491bc7-679fadb732emr10456053eaf.1.1772566709726; Tue, 03 Mar 2026 11:38:29 -0800 (PST) Date: Tue, 3 Mar 2026 19:38:16 +0000 In-Reply-To: <20260303193822.2526335-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: <20260303193822.2526335-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog Message-ID: <20260303193822.2526335-3-rananta@google.com> Subject: [PATCH v6 2/8] vfio: selftests: Introduce snprintf_assert() From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson Cc: Vipin Sharma , Josh Hilke , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Raghavendra Rao Ananta Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce snprintf_assert() to protect the users of snprintf() to fail if the requested operation was truncated due to buffer limits. VFIO tests and libraries, including a new sysfs library that will be introduced by an upcoming patch, rely quite heavily on snprintf()s to build PCI sysfs paths. Having a protection against this will be helpful to prevent false test failures. Signed-off-by: Raghavendra Rao Ananta Reviewed-by: David Matlack --- .../vfio/lib/include/libvfio/assert.h | 5 +++++ .../selftests/vfio/lib/vfio_pci_device.c | 8 +++---- .../selftests/vfio/vfio_dma_mapping_test.c | 6 +++--- .../selftests/vfio/vfio_pci_device_test.c | 21 ++++++++++--------- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/assert.h b/to= ols/testing/selftests/vfio/lib/include/libvfio/assert.h index f4ebd122d9b6f..77b68c7129a64 100644 --- a/tools/testing/selftests/vfio/lib/include/libvfio/assert.h +++ b/tools/testing/selftests/vfio/lib/include/libvfio/assert.h @@ -51,4 +51,9 @@ VFIO_ASSERT_EQ(__ret, 0, "ioctl(%s, %s, %s) returned %d\n", #_fd, #_op, #= _arg, __ret); \ } while (0) =20 +#define snprintf_assert(_s, _size, _fmt, ...) do { \ + int __ret =3D snprintf(_s, _size, _fmt, ##__VA_ARGS__); \ + VFIO_ASSERT_LT(__ret, _size); \ +} while (0) + #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_ASSERT_H */ diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index 4e5871f1ebc3b..1538d2b30ae59 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -209,7 +209,7 @@ static unsigned int vfio_pci_get_group_from_dev(const c= har *bdf) unsigned int group; int ret; =20 - snprintf(sysfs_path, PATH_MAX, "%s/%s/iommu_group", PCI_SYSFS_PATH, bdf); + snprintf_assert(sysfs_path, PATH_MAX, "%s/%s/iommu_group", PCI_SYSFS_PATH= , bdf); =20 ret =3D readlink(sysfs_path, dev_iommu_group_path, sizeof(dev_iommu_group= _path)); VFIO_ASSERT_NE(ret, -1, "Failed to get the IOMMU group for device: %s\n",= bdf); @@ -229,7 +229,7 @@ static void vfio_pci_group_setup(struct vfio_pci_device= *device, const char *bdf int group; =20 group =3D vfio_pci_get_group_from_dev(bdf); - snprintf(group_path, sizeof(group_path), "/dev/vfio/%d", group); + snprintf_assert(group_path, sizeof(group_path), "/dev/vfio/%d", group); =20 device->group_fd =3D open(group_path, O_RDWR); VFIO_ASSERT_GE(device->group_fd, 0, "open(%s) failed\n", group_path); @@ -300,7 +300,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf) cdev_path =3D calloc(PATH_MAX, 1); VFIO_ASSERT_NOT_NULL(cdev_path); =20 - snprintf(dir_path, sizeof(dir_path), "/sys/bus/pci/devices/%s/vfio-dev/",= bdf); + snprintf_assert(dir_path, sizeof(dir_path), "/sys/bus/pci/devices/%s/vfio= -dev/", bdf); =20 dir =3D opendir(dir_path); VFIO_ASSERT_NOT_NULL(dir, "Failed to open directory %s\n", dir_path); @@ -310,7 +310,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf) if (strncmp("vfio", entry->d_name, 4)) continue; =20 - snprintf(cdev_path, PATH_MAX, "/dev/vfio/devices/%s", entry->d_name); + snprintf_assert(cdev_path, PATH_MAX, "/dev/vfio/devices/%s", entry->d_na= me); break; } =20 diff --git a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c b/tools/t= esting/selftests/vfio/vfio_dma_mapping_test.c index abb170bdcef7e..7d0de8c79de13 100644 --- a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c +++ b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c @@ -44,9 +44,9 @@ static int intel_iommu_mapping_get(const char *bdf, u64 i= ova, FILE *file; char *rest; =20 - snprintf(iommu_mapping_path, sizeof(iommu_mapping_path), - "/sys/kernel/debug/iommu/intel/%s/domain_translation_struct", - bdf); + snprintf_assert(iommu_mapping_path, sizeof(iommu_mapping_path), + "/sys/kernel/debug/iommu/intel/%s/domain_translation_struct", + bdf); =20 printf("Searching for IOVA 0x%lx in %s\n", iova, iommu_mapping_path); =20 diff --git a/tools/testing/selftests/vfio/vfio_pci_device_test.c b/tools/te= sting/selftests/vfio/vfio_pci_device_test.c index 7c0fe8ce3a61f..93c11fd5e0818 100644 --- a/tools/testing/selftests/vfio/vfio_pci_device_test.c +++ b/tools/testing/selftests/vfio/vfio_pci_device_test.c @@ -39,16 +39,17 @@ FIXTURE_TEARDOWN(vfio_pci_device_test) iommu_cleanup(self->iommu); } =20 -#define read_pci_id_from_sysfs(_file) ({ \ - char __sysfs_path[PATH_MAX]; \ - char __buf[32]; \ - int __fd; \ - \ - snprintf(__sysfs_path, PATH_MAX, "/sys/bus/pci/devices/%s/%s", device_bdf= , _file); \ - ASSERT_GT((__fd =3D open(__sysfs_path, O_RDONLY)), 0); \ - ASSERT_GT(read(__fd, __buf, ARRAY_SIZE(__buf)), 0); \ - ASSERT_EQ(0, close(__fd)); \ - (u16)strtoul(__buf, NULL, 0); \ +#define read_pci_id_from_sysfs(_file) ({ \ + char __sysfs_path[PATH_MAX]; \ + char __buf[32]; \ + int __fd; \ + \ + snprintf_assert(__sysfs_path, PATH_MAX, "/sys/bus/pci/devices/%s/%s", \ + device_bdf, _file); \ + ASSERT_GT((__fd =3D open(__sysfs_path, O_RDONLY)), 0); \ + ASSERT_GT(read(__fd, __buf, ARRAY_SIZE(__buf)), 0); \ + ASSERT_EQ(0, close(__fd)); \ + (u16)strtoul(__buf, NULL, 0); \ }) =20 TEST_F(vfio_pci_device_test, config_space_read_write) --=20 2.53.0.473.g4a7958ca14-goog From nobody Thu Apr 2 06:10:00 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 1FA20397695 for ; Tue, 3 Mar 2026 19:38:31 +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=1772566715; cv=none; b=ApNgS1SiTbnqQZjTOOSLLBqwUmWAFAbwPQI78Urm4vMQMstlOeiRvQ/lxyHt3ZlNWO2oUVbrFCUHcpKnsYgMz9rEA8eSStaugvuDq0CL4rRPGb2F1CqbAA0nrfR043BxvR3mEQBN0vpFBh2Z3jX+4TjxzBg4pN1UPRTEP5O6KXY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772566715; c=relaxed/simple; bh=j55EITZPamGOl8xWZUsMHzflfEtE+hCWlGiq3rkNOLU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=KAhrxj6kPpcRu+zcrhZnPXbpPwnf0gXnjUMRJYz+zAwFUa3AiHx8H9Ys2iUHZ+Zg0kOY2MR1nUzMiOpzI5P3SKgdGOqHnMDd+3uN6WJbDTmE1lxCK/CV7K6WEyfpoKEzjz9RTmkcTedv3mGVAOWyR5B+WQ1Q4EcJ9XNPvv5DKMg= 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=V/gBPckI; 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="V/gBPckI" Received: by mail-ot1-f73.google.com with SMTP id 46e09a7af769-7d4c1b2651fso31886377a34.3 for ; Tue, 03 Mar 2026 11:38:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772566711; x=1773171511; 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=qL4uX3Hf+gcab1Fp0qED7ANoBgaJYjjpW0rRoMZC+68=; b=V/gBPckICWZMj3Z9BUDFdk0gm4kJitU8t6Re/gqJOZbXkAz0w9GKID418eIUCY0UzP wKXaBPrDU2gWGE5HRE4P6e6pDXR1W/CodHfStIVMnafQv3V9tIA/vcIQIrdD+4UcQvcY ZgeMak52Zfz1i9QK64osfn4IQ05k4ffq0igqYAQ+NqE1EEfcxLXmcwsjMdjiEkukN6vk M3fkRKGh8ELqluvxtc9ExSpj/C6UCLc32/kG9ddhHgJa1uHeY7Ar5O50eNEFYjoS29kk qNJx9dAPi2hTdiOc5PCssGMU5WnjM5qTbNcFpTLJwaks+3T9v61e+rK6GO6tsfcPtoEd pxNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772566711; x=1773171511; 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=qL4uX3Hf+gcab1Fp0qED7ANoBgaJYjjpW0rRoMZC+68=; b=aO7XOjC2ZD8KYh8i2UASTqWU/5u63YRyGr/2w+/2V7zjTZBshNAznYhvIJL/KC0Ygk yrCGnnZrxeo0wc9CQTxzeW99JdOtFEK7dTKUfuMPydvZFMs973Y9mpHSkXce8vdGyJjo wfWy/qKuG6VRzwpLPLmGC33RmunH28O25WU6YEEJ/ygldjzmYpTjbBAiaBKhLljD3YvA Rlefwy8C/5JE4Jewtrba7hp+HlkQGEEol2AsV3t0hEZvUjGhpEX+8LSWn7hnNjVP3M+y piwGvjum/zJsBz3D13wq/zhZGeJo1Op0uyFKMofbmENRqcacIZqJfzmVee9ZppcM4BnV r7Dw== X-Forwarded-Encrypted: i=1; AJvYcCVUfaNuuwSEvnXKzSsF0Vz+Dx/hd2u2t1nivF9KsL9w4n1znKDKnzty93RmqYxsa7TZP8/D3SYK2mvhy5w=@vger.kernel.org X-Gm-Message-State: AOJu0Yy1Ktu39Dt4qa700hdV79xI8PY7hnH8RLSivrNbkMUwNufcmMdp OiWXnlkJ+tGGY4vFT396xAR4FCErwxRLl0XeRYlbZXyD3Zzm9KijQZ21EFbmlaLT4i1IoEHGfUD 7x/kThQyW2A== X-Received: from jadw1.prod.google.com ([2002:a05:6638:1381:b0:5c8:f6e4:d33d]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:1794:b0:662:c0f7:bed8 with SMTP id 006d021491bc7-679fadb6c42mr10151810eaf.12.1772566710765; Tue, 03 Mar 2026 11:38:30 -0800 (PST) Date: Tue, 3 Mar 2026 19:38:17 +0000 In-Reply-To: <20260303193822.2526335-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: <20260303193822.2526335-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog Message-ID: <20260303193822.2526335-4-rananta@google.com> Subject: [PATCH v6 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 | 144 ++++++++++++++++++ .../selftests/vfio/lib/vfio_pci_device.c | 22 +-- 5 files changed, 159 insertions(+), 21 deletions(-) create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/sysfs.h create mode 100644 tools/testing/selftests/vfio/lib/sysfs.c diff --git a/tools/testing/selftests/vfio/lib/include/libvfio.h b/tools/tes= ting/selftests/vfio/lib/include/libvfio.h index 1b6da54cc2cb7..07862b470777b 100644 --- a/tools/testing/selftests/vfio/lib/include/libvfio.h +++ b/tools/testing/selftests/vfio/lib/include/libvfio.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include =20 diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/sysfs.h b/too= ls/testing/selftests/vfio/lib/include/libvfio/sysfs.h new file mode 100644 index 0000000000000..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..1fec6c7a7fce7 --- /dev/null +++ b/tools/testing/selftests/vfio/lib/sysfs.c @@ -0,0 +1,144 @@ +// 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); + vf_path[ret] =3D '\0'; + + 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); + dev_iommu_group_path[ret] =3D '\0'; + + 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); + } + driver_path[ret] =3D '\0'; + + 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 1538d2b30ae59..82f255f0486dc 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -25,8 +25,6 @@ #include "kselftest.h" #include =20 -#define PCI_SYSFS_PATH "/sys/bus/pci/devices" - static void vfio_pci_irq_set(struct vfio_pci_device *device, u32 index, u32 vector, u32 count, int *fds) { @@ -202,24 +200,6 @@ void vfio_pci_device_reset(struct vfio_pci_device *dev= ice) ioctl_assert(device->fd, VFIO_DEVICE_RESET, NULL); } =20 -static unsigned int vfio_pci_get_group_from_dev(const char *bdf) -{ - char dev_iommu_group_path[PATH_MAX] =3D {0}; - char sysfs_path[PATH_MAX] =3D {0}; - unsigned int group; - int ret; - - snprintf_assert(sysfs_path, PATH_MAX, "%s/%s/iommu_group", PCI_SYSFS_PATH= , bdf); - - ret =3D readlink(sysfs_path, dev_iommu_group_path, sizeof(dev_iommu_group= _path)); - VFIO_ASSERT_NE(ret, -1, "Failed to get the IOMMU group for device: %s\n",= bdf); - - ret =3D sscanf(basename(dev_iommu_group_path), "%u", &group); - VFIO_ASSERT_EQ(ret, 1, "Failed to get the IOMMU group for device: %s\n", = bdf); - - return group; -} - static void vfio_pci_group_setup(struct vfio_pci_device *device, const cha= r *bdf) { struct vfio_group_status group_status =3D { @@ -228,7 +208,7 @@ static void vfio_pci_group_setup(struct vfio_pci_device= *device, const char *bdf char group_path[32]; int group; =20 - group =3D vfio_pci_get_group_from_dev(bdf); + group =3D sysfs_iommu_group_get(bdf); snprintf_assert(group_path, sizeof(group_path), "/dev/vfio/%d", group); =20 device->group_fd =3D open(group_path, O_RDWR); --=20 2.53.0.473.g4a7958ca14-goog From nobody Thu Apr 2 06:10:00 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 E85D5331220 for ; Tue, 3 Mar 2026 19:38:32 +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=1772566716; cv=none; b=LSvl2v27bT5K7ewELLpC0MGAMmjZbGZ1Oh7rUp+vyREEiUGe3iHRofXmsPtad5CHdVXpG1iiKRtmr9jW1TU8oZb0JlUD5xfeTEOf6curaAS1s0CSt06O3ZCLUOrjxq2xpz2LWlWaUP6kwvplpdAXhA/b0XsZOtV30L5A59Y/E3w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772566716; c=relaxed/simple; bh=gEp5dckqG5cP6r+qRr0XG/bWUuqISDwDcz3OMbSqQOA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=h2ZPsr2WFMZXxzQO+n8VMec/dDlG8yuZlkc7I8uHJ8cobFEJaB3Y00NGWGOpBKm6WjsZKeoEkvV3E8YCARQ3SXcEhrZJpgo4FoGpW+1Mk8d0yNQZv3+0tMjtZeiURmjEHBhpBD41OU67gxDR6V5LcL33C9IOeCI+nRJjX7wAIa4= 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=rpBT5V0Z; 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="rpBT5V0Z" Received: by mail-oo1-f73.google.com with SMTP id 006d021491bc7-67995e1ecacso110817997eaf.0 for ; Tue, 03 Mar 2026 11:38:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772566712; x=1773171512; 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=0LK6yP7R5CEoceLoxZTwdESH49A+lkP7zsPWLGIA5lg=; b=rpBT5V0Z1z/OcdY0pbGbqBVmaueYLpV7VKZ7yrGYIbdzsbelIKqU1Nl4cVFq/1znXQ XpA1AaDCUUucVUQh8ZxPlAPehe2mE08pRth54L76H2GRLoDNm3tLKzI2sI7dUC5OfhiF teQXDaEHWIfS5PJb23BW2aHHCf3BRuvHCY4+A71ZbtFnSrqImQIy+GvghsM2D3jp5Yoo fybNMkgRN9vBCKF19OFksabTHi+I6W3FlX5s3OBmz+qEAYsRLigLUX1Y7l7yGKxJQVE8 THz6S/b9eg3HWk9X0zK6YYTkrATdMQcJJvMOqvcOkHX+HrBuTqRE8mQYb+TSr8oFrIGW GWOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772566712; x=1773171512; 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=0LK6yP7R5CEoceLoxZTwdESH49A+lkP7zsPWLGIA5lg=; b=FdzKnwQQj9xyXTW6IGlg4N3/cybgTzncgbPkZm6RdB1y7HuNjNSJlwUh8tZB9+ifuu hhDYzZaYCeqS40zL4GHq+6s0oCvO8zgEqsNR6kx44YZ7joX2DbRhXZ3VqFuzvGyyfjdW d0pIIerUyfh2deU3hVVKq4UHYQHb7HYFwpVKggFslvK3Gh2Dw69XP8BsZs/ezSPnqXCR 6HSVZQyPo6GHCebdPljpv/UQJLDDVU1INRmTeFH98gzuoqJEs7gxnNPoVxNVD4xLs88P iSrjMQMkOxGgVrzQX0/wuECH9aMo8WCvhHjaoiPa1L3kTXjUVReRgJxE8f78Pgi4ZoD4 LJAQ== X-Forwarded-Encrypted: i=1; AJvYcCUwuK1LIHx9afGPqVxnltjtYeTeZRVwntg8GV3k23rxlU2fuvhl4Qwm7IzX7uSAVdcPaeZkBxzdNThV8As=@vger.kernel.org X-Gm-Message-State: AOJu0YxjAZHrttS9tMkIkiC1nCHUZ/UJeOL0MDodgwMntReddN/4IyUa Df4tcEd3NPOKOeqBfYi3V4Z+cJTUPvVOUR4LTCJLov/eCULxbt4bzl1+gV6zOmVYiioA1fYJ1ye VIApgPnu+0A== X-Received: from jaad15-n1.prod.google.com ([2002:a05:6638:c78f:10b0:5d1:5fa2:71ef]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:2214:b0:67a:2256:e70b with SMTP id 006d021491bc7-67a2256ed3amr763619eaf.41.1772566711689; Tue, 03 Mar 2026 11:38:31 -0800 (PST) Date: Tue, 3 Mar 2026 19:38:18 +0000 In-Reply-To: <20260303193822.2526335-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: <20260303193822.2526335-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog Message-ID: <20260303193822.2526335-5-rananta@google.com> Subject: [PATCH v6 4/8] vfio: selftests: Extend container/iommufd setup for passing vf_token From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson Cc: Vipin Sharma , Josh Hilke , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Raghavendra Rao Ananta Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" A UUID is normally set as a vf_token to correspond the VFs with the PFs, if they are both bound by the vfio-pci driver. This is true for iommufd-based approach and container-based approach. The token can be set either during device creation (VFIO_GROUP_GET_DEVICE_FD) in container-based approach or during iommu bind (VFIO_DEVICE_BIND_IOMMUFD) in the iommu-fd case. Hence extend the functions, vfio_pci_iommufd_setup() and vfio_pci_container_setup(), to accept vf_token as an (optional) argument and handle the necessary setup. No functional changes are expected. Signed-off-by: Raghavendra Rao Ananta Reviewed-by: David Matlack --- tools/testing/selftests/vfio/Makefile | 2 + .../selftests/vfio/lib/vfio_pci_device.c | 46 +++++++++++++++---- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/vfio/Makefile b/tools/testing/selftest= s/vfio/Makefile index 6a9ac6dd32cb6..f27ed18070f14 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -28,6 +28,8 @@ CFLAGS +=3D $(EXTRA_CFLAGS) =20 LDFLAGS +=3D -pthread =20 +LDLIBS +=3D -luuid + $(TEST_GEN_PROGS): %: %.o $(LIBVFIO_O) $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $< $(LIBVFIO_O) $(LDLIBS) -o $@ =20 diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index 82f255f0486dc..dc8b37df8d1f1 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -22,6 +22,8 @@ #include #include =20 +#include + #include "kselftest.h" #include =20 @@ -220,7 +222,27 @@ static void vfio_pci_group_setup(struct vfio_pci_devic= e *device, const char *bdf ioctl_assert(device->group_fd, VFIO_GROUP_SET_CONTAINER, &device->iommu->= container_fd); } =20 -static void vfio_pci_container_setup(struct vfio_pci_device *device, const= char *bdf) +static void vfio_pci_group_get_device_fd(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) +{ + char arg[64]; + + /* + * If a vf_token exists, argument to VFIO_GROUP_GET_DEVICE_FD + * will be in the form of the following example: + * "0000:04:10.0 vf_token=3Dbd8d9d2b-5a5f-4f5a-a211-f591514ba1f3" + */ + if (vf_token) + snprintf_assert(arg, ARRAY_SIZE(arg), "%s vf_token=3D%s", bdf, vf_token); + else + snprintf_assert(arg, ARRAY_SIZE(arg), "%s", bdf); + + device->fd =3D ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, arg); + VFIO_ASSERT_GE(device->fd, 0); +} + +static void vfio_pci_container_setup(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) { struct iommu *iommu =3D device->iommu; unsigned long iommu_type =3D iommu->mode->iommu_type; @@ -238,8 +260,7 @@ static void vfio_pci_container_setup(struct vfio_pci_de= vice *device, const char */ (void)ioctl(iommu->container_fd, VFIO_SET_IOMMU, (void *)iommu_type); =20 - device->fd =3D ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, bdf); - VFIO_ASSERT_GE(device->fd, 0); + vfio_pci_group_get_device_fd(device, bdf, vf_token); } =20 static void vfio_pci_device_setup(struct vfio_pci_device *device) @@ -300,12 +321,20 @@ const char *vfio_pci_get_cdev_path(const char *bdf) return cdev_path; } =20 -static void vfio_device_bind_iommufd(int device_fd, int iommufd) +static void vfio_device_bind_iommufd(int device_fd, int iommufd, + const char *vf_token) { struct vfio_device_bind_iommufd args =3D { .argsz =3D sizeof(args), .iommufd =3D iommufd, }; + uuid_t token_uuid; + + if (vf_token) { + VFIO_ASSERT_EQ(uuid_parse(vf_token, token_uuid), 0); + args.flags |=3D VFIO_DEVICE_BIND_FLAG_TOKEN; + args.token_uuid_ptr =3D (u64)token_uuid; + } =20 ioctl_assert(device_fd, VFIO_DEVICE_BIND_IOMMUFD, &args); } @@ -320,7 +349,8 @@ static void vfio_device_attach_iommufd_pt(int device_fd= , u32 pt_id) ioctl_assert(device_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &args); } =20 -static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const c= har *bdf) +static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) { const char *cdev_path =3D vfio_pci_get_cdev_path(bdf); =20 @@ -328,7 +358,7 @@ static void vfio_pci_iommufd_setup(struct vfio_pci_devi= ce *device, const char *b VFIO_ASSERT_GE(device->fd, 0); free((void *)cdev_path); =20 - vfio_device_bind_iommufd(device->fd, device->iommu->iommufd); + vfio_device_bind_iommufd(device->fd, device->iommu->iommufd, vf_token); vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id); } =20 @@ -344,9 +374,9 @@ struct vfio_pci_device *vfio_pci_device_init(const char= *bdf, struct iommu *iomm device->bdf =3D bdf; =20 if (iommu->mode->container_path) - vfio_pci_container_setup(device, bdf); + vfio_pci_container_setup(device, bdf, NULL); else - vfio_pci_iommufd_setup(device, bdf); + vfio_pci_iommufd_setup(device, bdf, NULL); =20 vfio_pci_device_setup(device); vfio_pci_driver_probe(device); --=20 2.53.0.473.g4a7958ca14-goog From nobody Thu Apr 2 06:10:00 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 DE683381AE7 for ; Tue, 3 Mar 2026 19:38:33 +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=1772566717; cv=none; b=CWsW7OZ2DoFHHXOfldwvKHkiSv1S1ccRB2xiDm5jE9bDbbRdE+2LU9c/rq33ssk0QouzFqaR1ME5fcWsjSCa1OKZpE5vHiHUChLexMZWF3eHsZrzqJctAC4vPCMDiOGTvDcrpy61ZJ5fLWzvs7y9dzvJqRttfKdabh3TPRAbg3k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772566717; c=relaxed/simple; bh=oxhmS7APbnCUHGUofsuGlwO+hnUqBHnMlenmB97s6rM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=iZVT0j3TBs6swJ0QLZWrI/o4shIJuptW/BnzVfsxK6sx6PPyxwcpeaJglsU2ZQUEP570cNN+iCQKKUvwnflbV/KmpayeNtu+vxpTzCrk4RyeYjV69a0orUaAIP2knGR7LEK1w+r4J2sIJKBDELwHAImm1o9yb06pbzkG3/YCs6Q= 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=fM25oWYW; 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="fM25oWYW" Received: by mail-ot1-f73.google.com with SMTP id 46e09a7af769-7d4c7afa9e0so70478061a34.0 for ; Tue, 03 Mar 2026 11:38:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772566713; x=1773171513; 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=UbzD3tYuZ5OwHztPK3YrRtp3AOj3URoZ8XrfGPi4048=; b=fM25oWYWWwNF/p+Mh58CuzNtB4ykFGyYkQZuj63ioCnYuZUoq0QKGIwFCysm2nWDxR KN5RyCAI7JsV6pZ3K2x6B5iGEy9Falj/wuOhmRdmqnBP4bdDDAUUn/44mCdLSs2WaMlO QHVRzlN5xjYN9RmZZwJcy80yuRnuVmv7qD4EW9oyp5bJWC25txWB30AQ2JLOgHdAqet5 3qm1wW7xpTmVQkuvoIfs7sXda6cCOEneKT5G/1ALfi3+pkW3927IgKXN+aP633gK6NqC mdn835Dnlpz55+0AWun/pan/DFSu9++qsV3h8erJuovvZy0z8pZz9iPrZMfnPntp6ujG KoCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772566713; x=1773171513; 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=UbzD3tYuZ5OwHztPK3YrRtp3AOj3URoZ8XrfGPi4048=; b=qlE1cbIJfjZD/363fT+E3d9O/XOoAPkqVfLDyP6EviuMCamvnM3xctlZ9RalWPtVRi eY/ZkoZT+L5XwHWpNWUEvHsdXx/Z3pZUJ4hnugC2WtMEZKE0ZoPo5RX9O3yztjCX9zhn wsz/74E2zBuuXctRrk/0sOT7JV8Xza19nFfjh5GqKSW9SrFb2tANMzu5QVfNbEz+DMkR QGkh6QRyZ2u14NDiKBdV0MAFVAYPUNzlNaC1jJ4j20Pv7M2zA0EDmsj4axA+yp4V2764 wwaN9X2sTjJHO8RoWSDrp2RUOgPT+wJue1Z4tD4VM6S2arpBtH2rVfaaySkXdtlSViHI aMbQ== X-Forwarded-Encrypted: i=1; AJvYcCWZLeG5Pug46CMIvfHCsaNiS5qEK6NR42onQYf4ZorEPehUojwYOrwRUyEYURG49IsDTswQSAWOTMNXXiE=@vger.kernel.org X-Gm-Message-State: AOJu0YxeVITUQBhiiYxQHFsJwL1DXD3GPt8nKDacoQWj8pFz8R6fUt1Q lbfsvYQ5kt4fLawOWEUnfJV4MvcS4WNclRHKOPXjw96AGF9ruPkeF+/ZHz0mw3QJlh8jR6pjmXI YF8/kE1+Kaw== X-Received: from iols5.prod.google.com ([2002:a5d:9145:0:b0:95f:753b:ff52]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:1b10:b0:678:f8f3:d6cb with SMTP id 006d021491bc7-679fadc2b58mr11868852eaf.6.1772566712613; Tue, 03 Mar 2026 11:38:32 -0800 (PST) Date: Tue, 3 Mar 2026 19:38:19 +0000 In-Reply-To: <20260303193822.2526335-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: <20260303193822.2526335-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog Message-ID: <20260303193822.2526335-6-rananta@google.com> Subject: [PATCH v6 5/8] vfio: selftests: Expose more vfio_pci_device functions From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson Cc: Vipin Sharma , Josh Hilke , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Raghavendra Rao Ananta Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Refactor and make the functions called under device initialization public. A later patch adds a test that calls these functions to validate the UAPI of SR-IOV devices. Opportunistically, to test the success and failure cases of the UAPI, split the functions dealing with VFIO_GROUP_GET_DEVICE_FD and VFIO_DEVICE_BIND_IOMMUFD into a core function and another one that asserts the ioctl. The former will be used for testing the SR-IOV UAPI, hence only export these. Signed-off-by: Raghavendra Rao Ananta Reviewed-by: David Matlack --- .../lib/include/libvfio/vfio_pci_device.h | 7 +++ .../selftests/vfio/lib/vfio_pci_device.c | 47 ++++++++++++++----- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_devi= ce.h b/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h index 2858885a89bbb..898de032fed5a 100644 --- a/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h +++ b/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h @@ -122,4 +122,11 @@ static inline bool vfio_pci_device_match(struct vfio_p= ci_device *device, =20 const char *vfio_pci_get_cdev_path(const char *bdf); =20 +void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf); +void __vfio_pci_group_get_device_fd(struct vfio_pci_device *device, + const char *bdf, const char *vf_token); +void vfio_container_set_iommu(struct vfio_pci_device *device); +void vfio_pci_cdev_open(struct vfio_pci_device *device, const char *bdf); +int __vfio_device_bind_iommufd(int device_fd, int iommufd, const char *vf_= token); + #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_VFIO_PCI_DEVICE_H */ diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index dc8b37df8d1f1..3123ba591f088 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -202,7 +202,7 @@ void vfio_pci_device_reset(struct vfio_pci_device *devi= ce) ioctl_assert(device->fd, VFIO_DEVICE_RESET, NULL); } =20 -static void vfio_pci_group_setup(struct vfio_pci_device *device, const cha= r *bdf) +void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf) { struct vfio_group_status group_status =3D { .argsz =3D sizeof(group_status), @@ -222,8 +222,8 @@ static void vfio_pci_group_setup(struct vfio_pci_device= *device, const char *bdf ioctl_assert(device->group_fd, VFIO_GROUP_SET_CONTAINER, &device->iommu->= container_fd); } =20 -static void vfio_pci_group_get_device_fd(struct vfio_pci_device *device, - const char *bdf, const char *vf_token) +void __vfio_pci_group_get_device_fd(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) { char arg[64]; =20 @@ -238,18 +238,21 @@ static void vfio_pci_group_get_device_fd(struct vfio_= pci_device *device, snprintf_assert(arg, ARRAY_SIZE(arg), "%s", bdf); =20 device->fd =3D ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, arg); +} + +static void vfio_pci_group_get_device_fd(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) +{ + __vfio_pci_group_get_device_fd(device, bdf, vf_token); VFIO_ASSERT_GE(device->fd, 0); } =20 -static void vfio_pci_container_setup(struct vfio_pci_device *device, - const char *bdf, const char *vf_token) +void vfio_container_set_iommu(struct vfio_pci_device *device) { struct iommu *iommu =3D device->iommu; unsigned long iommu_type =3D iommu->mode->iommu_type; int ret; =20 - vfio_pci_group_setup(device, bdf); - ret =3D ioctl(iommu->container_fd, VFIO_CHECK_EXTENSION, iommu_type); VFIO_ASSERT_GT(ret, 0, "VFIO IOMMU type %lu not supported\n", iommu_type); =20 @@ -259,7 +262,13 @@ static void vfio_pci_container_setup(struct vfio_pci_d= evice *device, * because the IOMMU type is already set. */ (void)ioctl(iommu->container_fd, VFIO_SET_IOMMU, (void *)iommu_type); +} =20 +static void vfio_pci_container_setup(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) +{ + vfio_pci_group_setup(device, bdf); + vfio_container_set_iommu(device); vfio_pci_group_get_device_fd(device, bdf, vf_token); } =20 @@ -321,8 +330,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf) return cdev_path; } =20 -static void vfio_device_bind_iommufd(int device_fd, int iommufd, - const char *vf_token) +int __vfio_device_bind_iommufd(int device_fd, int iommufd, const char *vf_= token) { struct vfio_device_bind_iommufd args =3D { .argsz =3D sizeof(args), @@ -336,7 +344,18 @@ static void vfio_device_bind_iommufd(int device_fd, in= t iommufd, args.token_uuid_ptr =3D (u64)token_uuid; } =20 - ioctl_assert(device_fd, VFIO_DEVICE_BIND_IOMMUFD, &args); + if (ioctl(device_fd, VFIO_DEVICE_BIND_IOMMUFD, &args)) + return -errno; + + return 0; +} + +static void vfio_device_bind_iommufd(int device_fd, int iommufd, + const char *vf_token) +{ + int ret =3D __vfio_device_bind_iommufd(device_fd, iommufd, vf_token); + + VFIO_ASSERT_EQ(ret, 0, "Failed VFIO_DEVICE_BIND_IOMMUFD ioctl\n"); } =20 static void vfio_device_attach_iommufd_pt(int device_fd, u32 pt_id) @@ -349,15 +368,19 @@ static void vfio_device_attach_iommufd_pt(int device_= fd, u32 pt_id) ioctl_assert(device_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &args); } =20 -static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, - const char *bdf, const char *vf_token) +void vfio_pci_cdev_open(struct vfio_pci_device *device, const char *bdf) { const char *cdev_path =3D vfio_pci_get_cdev_path(bdf); =20 device->fd =3D open(cdev_path, O_RDWR); VFIO_ASSERT_GE(device->fd, 0); free((void *)cdev_path); +} =20 +static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) +{ + vfio_pci_cdev_open(device, bdf); vfio_device_bind_iommufd(device->fd, device->iommu->iommufd, vf_token); vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id); } --=20 2.53.0.473.g4a7958ca14-goog From nobody Thu Apr 2 06:10:00 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 75C373976BD for ; Tue, 3 Mar 2026 19:38:35 +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=1772566720; cv=none; b=fxWh6V+tRFq95dhHwJBplzbDrotnooBQYDs2/oz9a3jgOnD0ECcZkaGiTX+vb9a5o6yd5P6ejF/FszYCpwlGd0PLvurw5U+i++VFfp+OOtenDh3HzowuH0/0NJsw9bsZVObg6nqhjf++/RggsvJcOCHyqTXDkmy+FCG+A8y3hKc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772566720; c=relaxed/simple; bh=4t42tWvrlKcV4lvYfuVx3UTtWZAafmp91JTe6pU7yS0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gn7HpqPgmeU/ThOdsA+yTeTAke/EXFjDsqD4tdpv7/xUdJKhVBkv/pzCJvpnfSgMaoX/ozaThJJqSb7ihpOd1Ki0d/8VtqfL8BKK5YyE46Luk2c4q2ulnCZDGFGAgKj5SlHq3/s95EAOuJsg+Z+087Dr3DCIErt6KQ9w/F+Am9E= 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=lRVi6F+h; 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="lRVi6F+h" Received: by mail-oo1-f74.google.com with SMTP id 006d021491bc7-679caf7ec07so114914488eaf.1 for ; Tue, 03 Mar 2026 11:38:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772566714; x=1773171514; 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=QW1ukqnI7vV/78Yh9+DpzQAxrAq0/IU7SQ5b40XVy3Q=; b=lRVi6F+hv4Kit+qb4VJ3dHeScNaBtQyrZ9zKky51FChRotle0RzR298wGXyFGvPSnl UJb+v+xF0Fyh4N/wVmA63vPF+cnknQ0VHpZkKe7LGY7cvtrsa6ynsUsV5gKMzAItN1fU QE8VQT3siWrqyRVQsJx8nwKV9OubME7uG7+gWOxNPNPD4crziIP/aFPc/mTELRmVG68q AviO5nxh1Wk48Xzd6THesT4GEFZmwepD4pqxSuNj1eso3xYvd/FAeCWF6G5JEW7sEGxc CZ7Xi59cQPvCljSkAFzdHCfk+FqXZluyby1tx5FMnIMXfRwJrDQkT7+gYDZrwNTso4DL Ridw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772566714; x=1773171514; 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=QW1ukqnI7vV/78Yh9+DpzQAxrAq0/IU7SQ5b40XVy3Q=; b=rjhJo0231ib08aU/ylnSXdhuTqcGBs2ttgqp2zLtCpNx7zDmbp98gy3pCh+KzFb9+z aieP6EgZkk9rPdfOcHVPpc8FzWI0OjKIXDuOX/Q3DRDUjykB8FczmfJlPUAWXBo7NHUj PPwbd7qP/VKsNwX+tXk2eDcVbe4egX/kNQ2+mBg3Pkew1kH2wND+ysGfA+EHGpMbttmr 2DEwgxSipiURJ/YIWu6/yi/qFUmvK9iTBXu4rhQzemGut9TmCHZnvlAfO6jDJKxwNEaM uvCzq7mNWjKzYbQLad68PFmohVI6Kgw9QN9NP+eQj1zqseSUi2y0KAGe7HPi3Kf9QnxS l8bQ== X-Forwarded-Encrypted: i=1; AJvYcCVJd1xl/h4r48Q9w3nu60csIVAiUqijSYEisbkQTmQ7eJ7s8v/lftxO9j0DttaXmIBSPGTZliAXRk6VS0w=@vger.kernel.org X-Gm-Message-State: AOJu0YysSdkuKbZRkrknhYPXFhMKQpHciXkHfZYO13ZxK+pgmJHyEohe 7wRvmTd8/XbzZ8ZXeU/iuFbD4RQV0m38z5uSkuSEaHVGuPm/E3fylIHRlECCaBqqT1lldtLB3X6 CZFn/KkDkVQ== X-Received: from ilbby5.prod.google.com ([2002:a05:6e02:2605:b0:4d4:4196:1ade]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:4de9:b0:679:e85c:10e7 with SMTP id 006d021491bc7-679fadc2b88mr9666644eaf.9.1772566714118; Tue, 03 Mar 2026 11:38:34 -0800 (PST) Date: Tue, 3 Mar 2026 19:38:20 +0000 In-Reply-To: <20260303193822.2526335-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: <20260303193822.2526335-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog Message-ID: <20260303193822.2526335-7-rananta@google.com> Subject: [PATCH v6 6/8] vfio: selftests: Add helper to set/override a vf_token From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson Cc: Vipin Sharma , Josh Hilke , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Raghavendra Rao Ananta Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a helper function, vfio_device_set_vf_token(), to set or override a vf_token. Not only at init, but a vf_token can also be set via the VFIO_DEVICE_FEATURE ioctl, by setting the VFIO_DEVICE_FEATURE_PCI_VF_TOKEN flag. Hence, add an API to utilize this functionality from the test code. The subsequent commit will use this to test the functionality of this method to set the vf_token. Signed-off-by: Raghavendra Rao Ananta Reviewed-by: David Matlack --- .../lib/include/libvfio/vfio_pci_device.h | 2 ++ .../selftests/vfio/lib/vfio_pci_device.c | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_devi= ce.h b/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h index 898de032fed5a..4ebdc00e20fca 100644 --- a/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h +++ b/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h @@ -129,4 +129,6 @@ void vfio_container_set_iommu(struct vfio_pci_device *d= evice); void vfio_pci_cdev_open(struct vfio_pci_device *device, const char *bdf); int __vfio_device_bind_iommufd(int device_fd, int iommufd, const char *vf_= token); =20 +void vfio_device_set_vf_token(int fd, const char *vf_token); + #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_VFIO_PCI_DEVICE_H */ diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index 3123ba591f088..4673b148f8c44 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -113,6 +113,40 @@ static void vfio_pci_irq_get(struct vfio_pci_device *d= evice, u32 index, ioctl_assert(device->fd, VFIO_DEVICE_GET_IRQ_INFO, irq_info); } =20 +static int vfio_device_feature_ioctl(int fd, u32 flags, void *data, + size_t data_size) +{ + u8 buffer[sizeof(struct vfio_device_feature) + data_size] =3D {}; + struct vfio_device_feature *feature =3D (void *)buffer; + + memcpy(feature->data, data, data_size); + + feature->argsz =3D sizeof(buffer); + feature->flags =3D flags; + + return ioctl(fd, VFIO_DEVICE_FEATURE, feature); +} + +static void vfio_device_feature_set(int fd, u16 feature, void *data, size_= t data_size) +{ + u32 flags =3D VFIO_DEVICE_FEATURE_SET | feature; + int ret; + + ret =3D vfio_device_feature_ioctl(fd, flags, data, data_size); + VFIO_ASSERT_EQ(ret, 0, "Failed to set feature %u\n", feature); +} + +void vfio_device_set_vf_token(int fd, const char *vf_token) +{ + uuid_t token_uuid =3D {0}; + + VFIO_ASSERT_NOT_NULL(vf_token, "vf_token is NULL"); + VFIO_ASSERT_EQ(uuid_parse(vf_token, token_uuid), 0); + + vfio_device_feature_set(fd, VFIO_DEVICE_FEATURE_PCI_VF_TOKEN, + token_uuid, sizeof(uuid_t)); +} + static void vfio_pci_region_get(struct vfio_pci_device *device, int index, struct vfio_region_info *info) { --=20 2.53.0.473.g4a7958ca14-goog From nobody Thu Apr 2 06:10:00 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 D7F2F38425E for ; Tue, 3 Mar 2026 19:38:36 +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=1772566720; cv=none; b=mNsgRYgXIeNjvovaoCd1gw6gNZS1AyNTbx6pky+NaiB2f2XKURDD0b1dKe+GUkqRoJ9i8VLtpQ5sXMKpuJj1EK5itsHVEFhU3dhbMzo/gldle9Hr5D8cN18bRs1TWDt7p6lOOs++04tLvxFTZ+Mb+q9B+v4gGGH7iQSoG7hO3iY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772566720; c=relaxed/simple; bh=Zea1mJb1nV+2ZNc0sYa8z4jFdj6XxBzN4tc0WpNB6wM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=D5+pEhesHvoL8zB7dp5P4A2OQzb3y6KkZF7KKj7OkmrULI6YY5mVyoheD8zOO6CDoNpLK1OA/zm8EVlM/wUh8l65pSb5wLe1OVHQ8fL9/Il0IqAOl9KWta+mmJyo9sbyw9lUxquV350FWRObzqZD5pTa3tuqjLMF02wiSwWGSBI= 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=GsDBHvJg; 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="GsDBHvJg" Received: by mail-ot1-f73.google.com with SMTP id 46e09a7af769-7d4c1b2651fso31886792a34.3 for ; Tue, 03 Mar 2026 11:38:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772566715; x=1773171515; 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=zp13jkTAk6bWujFIQ+dHCM/k/mA1jLnbVAVVBxM74mU=; b=GsDBHvJg1LDAS2rxgxM+anQMJYvHIBBxPHglAi0Vf8OsSG9Bto8Y+uoh7YkkxQxGOA ScEf0KjqvvTgJWX2vx/RcDPCQq1iBNnDd5d5+utCRxPe2ZqtJKHMzxGphIo83FTgxqqY 9R01swzJep/HwcxGvca+Xq62E4DvgIWxk9DjC9VHv4aTa9c2PTQpt9OvljWjLd0LWa77 bZQxB3925UE3DMTpzw/iMP1JaUwl2+NY28/R1F4vVJ2qTBp6grkclZLzh4JFseHfyeY/ jjqQCAKMOqS6aZi6VWoWHceY/JFZFR5AcSju3/zHij088rtmEJM525vjfJxb19PuAKvx WCEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772566715; x=1773171515; 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=zp13jkTAk6bWujFIQ+dHCM/k/mA1jLnbVAVVBxM74mU=; b=SO33cGPAd4QwdFwObcI7tRtsg3F33+mH3ch56FjOr9RiRDwee0rE8SyjEby1atYQ+w g5uB01LMve1ywFLcSeGyyQQgm25becxqCO+g9k2aUfG7iUbUhdS+di21bHxC/fqyKiMk DV6cSu6FEBBHDRqMc6pO1ALs5qnGoWySFRhlsehA1NLE/vva9IzON5bcmII6b45jOOcj tb17tUhue9AtK4e6hwUn+aFaiAG+3YH2OjWzNi+tRq0h73drl4GnmFcpXOL4dtSTbCUp C730ljlrkDlKn4wywO4uRAHYiA21Ho7vj306jx6sjJSgaohIqmf3ghGszxgLMI6fPbCP tr8A== X-Forwarded-Encrypted: i=1; AJvYcCXIdPL8LpRLj7PYXfsDOsUmRfmz7AftRYp9XS8rYYaJcyfCVXykC5tw2puhY9UGLAM8gEwsFfxiNgrJVCY=@vger.kernel.org X-Gm-Message-State: AOJu0Yy35puyBrhS6hK5kj03jam/ffjUiscDXzpwQmt/2eI8aHBP4zNZ u2CvVMqwrVfuIVDELI7K1ybXnFBIgesXTOnf1fTLhWQne3Sp+8emgUhXyacB85hn2f9uhtP7yVa u4vyxx0wKGA== X-Received: from ioql7.prod.google.com ([2002:a6b:7f07:0:b0:954:4c52:95e1]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:213:b0:679:c33d:b07a with SMTP id 006d021491bc7-679faf001eemr9910418eaf.44.1772566715368; Tue, 03 Mar 2026 11:38:35 -0800 (PST) Date: Tue, 3 Mar 2026 19:38:21 +0000 In-Reply-To: <20260303193822.2526335-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: <20260303193822.2526335-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog Message-ID: <20260303193822.2526335-8-rananta@google.com> Subject: [PATCH v6 7/8] vfio: selftests: Add helpers to alloc/free vfio_pci_device From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson Cc: Vipin Sharma , Josh Hilke , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Raghavendra Rao Ananta Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a helper, vfio_pci_device_alloc(), to allocate 'struct vfio_pci_device'. The subsequent test patch will utilize this to get the struct with very minimal initialization done. Internally, let vfio_pci_device_init() also make use of this function and later do the full initialization. Symmetrically, add a free variant, vfio_pci_device_free(), to be used in a similar fashion. No functional change intended. Signed-off-by: Raghavendra Rao Ananta Reviewed-by: David Matlack --- .../vfio/lib/include/libvfio/vfio_pci_device.h | 2 ++ .../selftests/vfio/lib/vfio_pci_device.c | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_devi= ce.h b/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h index 4ebdc00e20fca..3eabead717bbd 100644 --- a/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h +++ b/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h @@ -38,6 +38,8 @@ struct vfio_pci_device { #define dev_info(_dev, _fmt, ...) printf("%s: " _fmt, (_dev)->bdf, ##__VA_= ARGS__) #define dev_err(_dev, _fmt, ...) fprintf(stderr, "%s: " _fmt, (_dev)->bdf,= ##__VA_ARGS__) =20 +struct vfio_pci_device *vfio_pci_device_alloc(const char *bdf, struct iomm= u *iommu); +void vfio_pci_device_free(struct vfio_pci_device *device); struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu= *iommu); void vfio_pci_device_cleanup(struct vfio_pci_device *device); =20 diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index 4673b148f8c44..4ff76970e3791 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -419,7 +419,7 @@ static void vfio_pci_iommufd_setup(struct vfio_pci_devi= ce *device, vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id); } =20 -struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu= *iommu) +struct vfio_pci_device *vfio_pci_device_alloc(const char *bdf, struct iomm= u *iommu) { struct vfio_pci_device *device; =20 @@ -430,6 +430,20 @@ struct vfio_pci_device *vfio_pci_device_init(const cha= r *bdf, struct iommu *iomm device->iommu =3D iommu; device->bdf =3D bdf; =20 + return device; +} + +void vfio_pci_device_free(struct vfio_pci_device *device) +{ + free(device); +} + +struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu= *iommu) +{ + struct vfio_pci_device *device; + + device =3D vfio_pci_device_alloc(bdf, iommu); + if (iommu->mode->container_path) vfio_pci_container_setup(device, bdf, NULL); else @@ -462,5 +476,5 @@ void vfio_pci_device_cleanup(struct vfio_pci_device *de= vice) if (device->group_fd) VFIO_ASSERT_EQ(close(device->group_fd), 0); =20 - free(device); + vfio_pci_device_free(device); } --=20 2.53.0.473.g4a7958ca14-goog From nobody Thu Apr 2 06:10:00 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 D0E883890EC for ; Tue, 3 Mar 2026 19:38:37 +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=1772566722; cv=none; b=SKHVPsgJWYs9wiBX2blBU40nEFon5yhpoQ0Nc4DBGVj0+g74oSO8LCAviegnS5jNFMrUSQOemUkpaT5AzTEgCK3nOc2UFj3J+1HKJt3tNfdCewd8SJ9ywb5FTp4blAMdBV3lTRGg304Psn7I0DGDGoxbys6qL9NPJwIpnUJmS9U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772566722; c=relaxed/simple; bh=/VSsOyaX6EyV7wMFzKkZLOSuNCiEI31rXP6mtKFERc0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=b1nxJ6rQ8eAlEZ4s7X4tqTKjig1ga1BShGq+vz+KEU75bD8/kZJREAPqt8Fvc7KDDC6VQGf1B6jx9jKqVMDmcRZMkfaVGU3a4jkZLG+i2+Ku5GN52zENADvGB7U+9FGEim4zHfHwE4UQ6lEtUUspseu7CxfSvOXbt5v67KSQzh4= 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=LwnywPok; 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="LwnywPok" Received: by mail-ot1-f74.google.com with SMTP id 46e09a7af769-7d194b631d6so47486539a34.0 for ; Tue, 03 Mar 2026 11:38:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772566716; x=1773171516; 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=y+ORTt1MFPaW/G6zWDl2Wih/SPL6Dj/YV1ENjiNtHmk=; b=LwnywPok536rO43/8mILJGOWM17ddFwpr8RpRGMZX4Jmq4RiuRhqdQxmHKbLJqUaXZ efYGjFI/RAXxCeazcR4OBGItqlzGd+dQm4yJ4LSQ/tGJ7ObEknKFiHhVJ4yJNn3Ma+JD SUX2KYafP4O1HtdhTO+3xyr0f9/QHYyr8Q4KoV1Jar8fK3JyP0DqzFAUWqFgNihRm8/b NK6G1iIb11GeB8CffPGYsEFj0+2c69anW2moLt2WNVl/UjiFOAWv5vvczFH8rfwzaS+j UjnuJjSpnTACszf6+eGXp4Y9CKIKo54oCAT1/CSTZeujP37P8lvKq0+dU0QokM0WfUBT ZENA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772566716; x=1773171516; 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=y+ORTt1MFPaW/G6zWDl2Wih/SPL6Dj/YV1ENjiNtHmk=; b=k0NF0A67MbPHkucU3RZ89YWjWobXFQ0p63B0SZsALSJLyGKXTAj5TqVho2vCnPMYbD qQSsrkRydldhe3qnvFcpj0EC0+CE2y1LKMCBUXogiZshMv0tiO2qCPfLfcpS0PjAfDtJ aee1Ak7R1XZvNbo8Nq273LsiQJoxaSkjd7lA0RjbNX2Xze7PkNldFWDiAzZErx7lbJMH zOlrsptOOz0Udk0wo2eTEMGDo/bkpgqurOyRy08975Db3NDwXNMYcRt++KMDapA4z2mJ jLSKhLyvYENa5CysYCwoduX53sHM02fcNBJT43NQB03FGtH0NMTyfQRbSnsXYIMUph1S 0LjQ== X-Forwarded-Encrypted: i=1; AJvYcCUVq16XSOaQ1RfBIjf/k+zNd92Bz+MeV7IQ2sLc9q4d2xYBaHmDd+I8u/Euoh2n35ZLl8dUQcScE+WyUS0=@vger.kernel.org X-Gm-Message-State: AOJu0YytriTf4GApls1Tw4olsRg0EUEgYZeMdeSe99fA05CHen4LRGDw flYQq0Z5jYodESXdOmTjpkkslx32te6qCCwi7pPOVN7g7EhdCVgqpujc5/41/hCF7M4hrUeJKsX c+hcH+yMVeQ== X-Received: from ilbbk10.prod.google.com ([2002:a05:6e02:328a:b0:4f6:193c:64ae]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:1627:b0:679:946d:351f with SMTP id 006d021491bc7-679fadb7ee1mr9856215eaf.13.1772566716431; Tue, 03 Mar 2026 11:38:36 -0800 (PST) Date: Tue, 3 Mar 2026 19:38:22 +0000 In-Reply-To: <20260303193822.2526335-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: <20260303193822.2526335-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog Message-ID: <20260303193822.2526335-9-rananta@google.com> Subject: [PATCH v6 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 f27ed18070f14..d0c8cea53eb54 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -12,6 +12,7 @@ TEST_GEN_PROGS +=3D vfio_iommufd_setup_test TEST_GEN_PROGS +=3D vfio_pci_device_test TEST_GEN_PROGS +=3D vfio_pci_device_init_perf_test TEST_GEN_PROGS +=3D vfio_pci_driver_test +TEST_GEN_PROGS +=3D vfio_pci_sriov_uapi_test =20 TEST_FILES +=3D scripts/cleanup.sh TEST_FILES +=3D scripts/lib.sh diff --git a/tools/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c b/tool= s/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c new file mode 100644 index 0000000000000..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.473.g4a7958ca14-goog