From nobody Wed Jun 10 17:15:45 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 A85054949EB for ; Tue, 5 May 2026 21:28:42 +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=1778016523; cv=none; b=V6nfH7GVzyMWHV330sBt6vJUIU4mqoVf3aOz/VehAwskqxb8VKY8qCvD/IspEvYZjjgn+c/Ygq+ECjfzky2V9lEKXTiY7wqCAQ/RA4EfvHOwCCaznYbccRPgf7+NeK+R5cxRv0aZ6W99YSLaCYFit9ix9VY94XbFkXdz8MbZ9kw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778016523; c=relaxed/simple; bh=mNkQDudgu3sz5Rigm/44n6X6+QGkPsR7h3tJUvaVOpM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IH5aZeB3sfDE2GMDfLUvwVrZyqb9PP+ztZr918NBFPgeSSlTdRdgkZs0UCpS+dm1udKjpBbv1xm7Q679g7oeuYAWK3NfutpNEjYyuNaxoaI09SdfEnKC74kQNhP3zHQlICDM4xxxgVn0EKPj/+oUS68x/Lm/a8wVo/zUC8TvfRI= 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=rpID15rl; 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="rpID15rl" Received: by mail-oo1-f74.google.com with SMTP id 006d021491bc7-69491bd267fso12208326eaf.0 for ; Tue, 05 May 2026 14:28:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778016521; x=1778621321; 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=8tkEMgBMEnFdcG4d9cOlR5kG+qYJi3E0o1F61AgqdJM=; b=rpID15rleOl5W7FzsUUbKTLVLePYwTqC0nev/DhM6XOORK15IakMswawq/CkgnhmsX 7OLIVO/GyL5HNGZPwgdPTcz/+Gilw9vKkyhBxkYFBq+dDJ/i2p2/fbcjPMIAgHaNh2Xc 5PNo4OaJppnGlRIIindrkBr3sdmz998Hu6NrjZSYq8YnLGy2u5ybW4wLUV2zkGe4Ue4G t9C0va7dIwequGW4koXQ82TfeMoZbbBWcmdmN+/44aP5nJatbjk/zVisrOE8xvhNycPT NCnSAieHPUCLb1AOwPvPsUzO9VyfdCskNUvAvM6iCyD6ZbxzgwDH3aC2f7jORU3SDr7+ PzMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778016521; x=1778621321; 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=8tkEMgBMEnFdcG4d9cOlR5kG+qYJi3E0o1F61AgqdJM=; b=iQnScTgwdKu7tyJkLMrVA0b7HzSgf/DFmjhKt0WKl1Pke4b/1IAmfeulcWPXffY6V0 KAnLqRRsgHu5B85yUj6+I9aotM77v2KBzz+woj8rozR1N7tiZZ4WHeoxM4qfHYh/ji/Z o7rm48MuszKYXImjoV4HcIY6t0MJNLLhKLrZ5p4k4YuFduWhBJvJ1Cy6mrC8l5MKOrGq 8CgbOCqg+oIGok5CmMq7QCn3C4rFRki4ZTi9orF41uCKxT+lXMpomCw0ErgHRuXbUeav ICH+w74ohfqr3z0KDx2F6lPtQbaer9if4tBhR4yPTTabcqtsb5H80RBch+JmG+1UtJ2U 6aAg== X-Forwarded-Encrypted: i=1; AFNElJ8DprCI2rUUF99Ea/BsSYx36ni99VWlL7q+eAXPqCK1jfKiyrd/OFrO/cpc9Z4hW5cucJRZu9K0jizMfTs=@vger.kernel.org X-Gm-Message-State: AOJu0Yxu7MsplYqGhP/8oGhSHECii6ZNcWfjEhgPgvmBk8ApOhgZFWca ZZ1QxSZBRMmYhrw9bJovG/RIiYwKZrOEsE4AQOkwgxbXyL9qPMBT/eF0213I0HQ9uFhPyI+xwOG 1e0a7vcFxBQ== X-Received: from ilbed17.prod.google.com ([2002:a05:6e02:4811:b0:4fa:aa1:e611]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a4a:ee19:0:b0:696:637e:4837 with SMTP id 006d021491bc7-69998cd5138mr375629eaf.27.1778016521453; Tue, 05 May 2026 14:28:41 -0700 (PDT) Date: Tue, 5 May 2026 21:28:31 +0000 In-Reply-To: <20260505212838.1698034-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: <20260505212838.1698034-1-rananta@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260505212838.1698034-2-rananta@google.com> Subject: [PATCH v8 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 Reviewed-by: Vipin Sharma Tested-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 0684932d91bfc..e26917ee169c5 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.54.0.545.g6539524ca2-goog From nobody Wed Jun 10 17:15:45 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 82B73494A06 for ; Tue, 5 May 2026 21:28:43 +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=1778016525; cv=none; b=NqS0VUA5uLUqRcRkT+Az9F6e7Cyqm7sbe1AjKnFTJwmgv6O4QIGFSKlyRJKmvY1P1655m3kH3PQasAlHoZ0mxVxus/JeqbNKOZroRi3dU4JJ4BgJFcNQ/UPsWXnW9XgmbuK2TDGJwtpyABwVIUGW3Jfn2bgGsk64fp8PEXDLt2g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778016525; c=relaxed/simple; bh=haQ+V/eSVuPh98JTXE41z2vcTYc/ehzcvVHagcUNwEk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Cxr0AHE7suUO3bT2/kQU1tRKbflMyCP/9LW1tcyH/hWI8UQQXubZ+zcLQGfmwn7GL1QSnSK6J0RvkjJqfgAe3pi9GG+UIz4Y1VKDvkRsGJ6p+SpD1JvpSDP+OXA4N0VOvtsHoRQF44mC+1rCN4CO9qQMs5rz4omz1HbBbN7r1f8= 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=piGxjUqk; 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="piGxjUqk" Received: by mail-oo1-f74.google.com with SMTP id 006d021491bc7-696133ef271so11115239eaf.2 for ; Tue, 05 May 2026 14:28:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778016522; x=1778621322; 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=5unDfi3wJ1sRon+uGFErvJbzVefhiGwdX/c5VDstEAQ=; b=piGxjUqkHtI8JI6AaP6iPEv1m+1tt/AiHuC/b8s66Q3u3vhFM9SaS4q6WgZMrwbnSW KR+wsZqfsgxCXCE4JNZ0Xu1SeNa52YqW/EnbW48GgvZqmf4eGGvc3BaeqTMItA8C9EJW FQKfdig+z0oZh8KUXjcmDpAQ2qGCr39+KZnTgCo7EYW4Rzwmray1Qk63EdNuL1H0dBqX RSntPB1xi62Bs86AXBoZtbwOzg9mL/gFpDTGN9hxjuGhHO0rm2y+5WY1K2cnkDVqBIzA +4RY9ELI5RyIIafb3nh/J9rT6EI6OalPbTUgH2bQPA0yfIJ15px87bt2mOmnxXflcSf8 kBfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778016522; x=1778621322; 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=5unDfi3wJ1sRon+uGFErvJbzVefhiGwdX/c5VDstEAQ=; b=XqIwM4xeKWD0WDNf67VDf4hu/NbbaalnSrfXkSqhNcAffrkhA2DVfNROKry5UU3xBq gXrNt7dWOm54xxsrmqOQoBu+3/7H0qLl0XU0WjsogcvppZW97Y6YO+mY2TtPkdROfbpV nOC2BQq0D5uFp5hYGVgSz5tkXeljweoskUPzKsmYwPZ9BeSZrlcA4hHc+WkjZ29orTS0 mkg0VeP0DzJaF4gIa4hKcP2AsYhmwfwcAY6pTO1EXbSi7UIIPAvpVUqB89jkQdPBjJcv GpNhcIa7Ws3AiLX7rptLUAkbtRZP2TmMTDWMryqjYLuVeLGEBMSt2QO5RUJ7xccIchm1 fWDA== X-Forwarded-Encrypted: i=1; AFNElJ9C1+NdnwilGWOe+R1VTNQFbH9heZqJQDvU1BMZASNRxPZ9VhPTtfXIIg4cCpiQzDLKbT5N+kuE78urfPI=@vger.kernel.org X-Gm-Message-State: AOJu0YwwH/Yb1AX3x21vAyH+rajcpt/DxhmkyUnbRfHGe/8qrLW4TInn WBU4uzzjFC92xE/1rn46JnQtyNaVEPATKR36SekgK2g8mp7XgWVU0oCy1MRZRasnscjRCCykL0Y 6aj5umh0DZA== X-Received: from iobb8.prod.google.com ([2002:a05:6602:2188:b0:964:7981:da07]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:4c06:b0:694:914f:c18f with SMTP id 006d021491bc7-69998cbc0d4mr330922eaf.3.1778016522423; Tue, 05 May 2026 14:28:42 -0700 (PDT) Date: Tue, 5 May 2026 21:28:32 +0000 In-Reply-To: <20260505212838.1698034-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: <20260505212838.1698034-1-rananta@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260505212838.1698034-3-rananta@google.com> Subject: [PATCH v8 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 Reviewed-by: Vipin Sharma Tested-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 fc75e04ef010c..efbf9ab63cea7 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -211,7 +211,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); @@ -231,7 +231,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); @@ -302,7 +302,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); @@ -312,7 +312,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.54.0.545.g6539524ca2-goog From nobody Wed Jun 10 17:15:45 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 755604963D2 for ; Tue, 5 May 2026 21:28:44 +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=1778016526; cv=none; b=P4hsrSqAK5ahxu+pZfRghFUZcdawIul0TAKxLGGdXGzyW/C1zZeSa8kqJwbkl0MkzItjcInWOk7OoFXKnctp6IESmezvzchJ5+/dquNCU3m/mq4ixicAStKHsSQdWe/17ekoCoXfS4VSZbP6k7yQ0zvnikp5QBz+5XVTgp+HPTk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778016526; c=relaxed/simple; bh=IcgJrFNktyNPn1dfHtLJq09UgzpIZjt0gqU9505v6Hg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=CWyQCb5dOVs2qtW6fBJrjbFHYcfx41dHOsCtK78tNbQceojE4vWmm5iVgBGcDE5I5go+LAoSNFT5deZhFwva16wk6FQrhevRw4tnIVpOOxsHCTED5U08/S0FTfRRwOXX5ePDu2+BZP5ClHffogoMyNEkihJ7z6ogiS8cm46zsik= 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=fBr3Bmt4; 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="fBr3Bmt4" Received: by mail-ot1-f74.google.com with SMTP id 46e09a7af769-7e0632c733aso4800137a34.1 for ; Tue, 05 May 2026 14:28:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778016523; x=1778621323; 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=C/NAaMUewODgHwyC7Y4Tcis2yg7tI8JXbAhWmt5KiHQ=; b=fBr3Bmt4GDKJ9UjU78mX7D+bFXVo28WbLicdFW/ZZqfR3GxfXNBGTa1CQ+1mrx8tbd px7QuNr3/yT5eHKoDkm8CnhNPwYOux+ckNKviwnjX+7MbcWhemO5thhptOFjPmtT9obL yuMrQHlyRgZ1cD2DCBEH+iTYfgN2Y56NB5H1WGF4YF/iy/5ghYShxV6ivGM+9Cc9Y1Mq YqJIQFIIWX8+Doa923bRQgJ9x8Tc9MwyUFZ+FR/h0XdT3z4dCN5q6ajc7h5yI6XDqgON oArHCU7OlRf6pUssMjVpRxd8pCpsiUhUlEnxqdFABDBX4mJscsJp4tj11AlmMwLcGzOJ afcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778016523; x=1778621323; 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=C/NAaMUewODgHwyC7Y4Tcis2yg7tI8JXbAhWmt5KiHQ=; b=lVZOmMvwbMfHlIeqPLWYIktsOjVRMIuM2ewU0Txf9jCow08sR+hWpBjyvw/JLQ30qL +BVN1elAkX4OeXDtQJ+fKWjHVdca/ARqYqTlS64LLjb48is5vcUvwfCtbZW2vPB3LP3W h0z0KT9Tq3SoffZndOItkmIQ8oiXaf+S/RGZuV4rlMRi+7sC2ZS52sz8WAjr2ysYopPc UKu07lteEDdTgubz22oiZRYtsjBvRE7YXq2QFGjzQrarSLlVd3qxea9EKZgjPcc3tYs9 enBWQp2utP51IAA+bfPn/Fqw0Sy/c4jmLCvVr4qgFKwHA0VASYpuGudqyZ96O8xYVuC4 jZyQ== X-Forwarded-Encrypted: i=1; AFNElJ97Gp1gUSgyolC8Jex4KXy5gWU9m58uG9/FZkSDeu6D+sbE+fvPlRTbjIlAAoxDi+zUNb7sjM0sbop6zvw=@vger.kernel.org X-Gm-Message-State: AOJu0YzsnMpuUOcTQuRdplshP4/90PveJee27S8Xpi4esp4LQOyZoKQp Ivyu4gLtIGVsOugWvqhgbAHCtMaJfKuV6ckX1ZwAaE8gfhjZQnymKX6DeSjE5XUPWcBfb8aSluN uE/5LpDh5wA== X-Received: from ilaj28.prod.google.com ([2002:a05:6e02:219c:b0:4fb:4caf:c442]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:4dc4:b0:694:9b71:3745 with SMTP id 006d021491bc7-69998d609b7mr345111eaf.57.1778016523340; Tue, 05 May 2026 14:28:43 -0700 (PDT) Date: Tue, 5 May 2026 21:28:33 +0000 In-Reply-To: <20260505212838.1698034-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: <20260505212838.1698034-1-rananta@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260505212838.1698034-4-rananta@google.com> Subject: [PATCH v8 3/8] vfio: selftests: Introduce a sysfs lib From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson Cc: Vipin Sharma , Josh Hilke , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Raghavendra Rao Ananta Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce a sysfs library to handle the common reads/writes to the PCI sysfs files, for example, getting the total number of VFs supported by the device via /sys/bus/pci/devices/$BDF/sriov_totalvfs. The library will be used in the upcoming test patch to configure the VFs for a given PF device. Since readlink() is quite commonly used in the lib, introduce and use readlink_safe() to take care of potential buffer overrun errors and to safely terminate the buffer with '\0'. Opportunistically, move vfio_pci_get_group_from_dev() to this library as it falls under the same bucket. Rename it to sysfs_iommu_group_get() to align with other function names. Signed-off-by: Raghavendra Rao Ananta Reviewed-by: David Matlack Reviewed-by: Vipin Sharma Tested-by: David Matlack --- .../selftests/vfio/lib/include/libvfio.h | 1 + .../vfio/lib/include/libvfio/sysfs.h | 12 ++ tools/testing/selftests/vfio/lib/libvfio.mk | 1 + tools/testing/selftests/vfio/lib/sysfs.c | 150 ++++++++++++++++++ .../selftests/vfio/lib/vfio_pci_device.c | 22 +-- 5 files changed, 165 insertions(+), 21 deletions(-) create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/sysfs.h create mode 100644 tools/testing/selftests/vfio/lib/sysfs.c diff --git a/tools/testing/selftests/vfio/lib/include/libvfio.h b/tools/tes= ting/selftests/vfio/lib/include/libvfio.h index 1b6da54cc2cb7..07862b470777b 100644 --- a/tools/testing/selftests/vfio/lib/include/libvfio.h +++ b/tools/testing/selftests/vfio/lib/include/libvfio.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include =20 diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/sysfs.h b/too= ls/testing/selftests/vfio/lib/include/libvfio/sysfs.h new file mode 100644 index 0000000000000..c9ab1ea8f5a9c --- /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 *bdf, int numvfs); +char *sysfs_sriov_vf_bdf_get(const char *pf_bdf, int i); +int sysfs_iommu_group_get(const char *bdf); +char *sysfs_driver_get(const char *bdf); + +#endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_SYSFS_H */ diff --git a/tools/testing/selftests/vfio/lib/libvfio.mk b/tools/testing/se= lftests/vfio/lib/libvfio.mk index 9f47bceed16f4..b7857319c3f1f 100644 --- a/tools/testing/selftests/vfio/lib/libvfio.mk +++ b/tools/testing/selftests/vfio/lib/libvfio.mk @@ -6,6 +6,7 @@ LIBVFIO_SRCDIR :=3D $(selfdir)/vfio/lib LIBVFIO_C :=3D iommu.c LIBVFIO_C +=3D iova_allocator.c LIBVFIO_C +=3D libvfio.c +LIBVFIO_C +=3D sysfs.c LIBVFIO_C +=3D vfio_pci_device.c LIBVFIO_C +=3D vfio_pci_driver.c =20 diff --git a/tools/testing/selftests/vfio/lib/sysfs.c b/tools/testing/selft= ests/vfio/lib/sysfs.c new file mode 100644 index 0000000000000..11415448b2e29 --- /dev/null +++ b/tools/testing/selftests/vfio/lib/sysfs.c @@ -0,0 +1,150 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include + +#include + +#define readlink_safe(_path, _buf) ({ \ + int __ret; \ + \ + _Static_assert(!__builtin_types_compatible_p( \ + __typeof__(_buf), char *), \ + "readlink_safe: _buf must be an array, not a pointer"); \ + \ + __ret =3D readlink(_path, _buf, sizeof(_buf) - 1); \ + if (__ret !=3D -1) \ + _buf[__ret] =3D 0; \ + __ret; \ +}) + +static void readlink_base(const char *path, const char *data_fmt, void *ou= t_data) +{ + char rl_path[PATH_MAX]; + int ret; + + ret =3D readlink_safe(path, rl_path); + VFIO_ASSERT_NE(ret, -1); + + ret =3D sscanf(basename(rl_path), data_fmt, out_data); + VFIO_ASSERT_EQ(ret, 1); +} + +static int sysfs_val_get_int(const char *component, const char *name, + const char *file) +{ + char path[PATH_MAX]; + char buf[32]; + int ret; + int fd; + + snprintf_assert(path, PATH_MAX, "/sys/bus/pci/%s/%s/%s", component, name,= file); + fd =3D open(path, O_RDONLY); + if (fd < 0) + return fd; + + VFIO_ASSERT_GT(read(fd, buf, ARRAY_SIZE(buf)), 0); + VFIO_ASSERT_EQ(close(fd), 0); + + errno =3D 0; + ret =3D strtol(buf, NULL, 0); + VFIO_ASSERT_EQ(errno, 0, "sysfs path \"%s\" is not an integer: \"%s\"\n",= path, buf); + + return ret; +} + +static void sysfs_val_set(const char *component, const char *name, + const char *file, const char *val) +{ + char path[PATH_MAX]; + int fd; + + snprintf_assert(path, PATH_MAX, "/sys/bus/pci/%s/%s/%s", component, name,= file); + VFIO_ASSERT_GT(fd =3D open(path, O_WRONLY), 0); + + VFIO_ASSERT_EQ(write(fd, val, strlen(val)), strlen(val)); + VFIO_ASSERT_EQ(close(fd), 0); +} + +static int sysfs_device_val_get(const char *bdf, const char *file) +{ + return sysfs_val_get_int("devices", bdf, file); +} + +static void sysfs_device_val_set(const char *bdf, const char *file, const = char *val) +{ + sysfs_val_set("devices", bdf, file, val); +} + +static void sysfs_device_val_set_int(const char *bdf, const char *file, in= t val) +{ + char val_str[32]; + + snprintf_assert(val_str, sizeof(val_str), "%d", val); + sysfs_device_val_set(bdf, file, val_str); +} + +int sysfs_sriov_totalvfs_get(const char *bdf) +{ + return sysfs_device_val_get(bdf, "sriov_totalvfs"); +} + +int sysfs_sriov_numvfs_get(const char *bdf) +{ + return sysfs_device_val_get(bdf, "sriov_numvfs"); +} + +void sysfs_sriov_numvfs_set(const char *bdf, int numvfs) +{ + sysfs_device_val_set_int(bdf, "sriov_numvfs", numvfs); +} + +char *sysfs_sriov_vf_bdf_get(const char *pf_bdf, int i) +{ + char path[PATH_MAX]; + char *out_vf_bdf; + + /* Fit "0000:00:00.0" */ + out_vf_bdf =3D calloc(16, sizeof(char)); + VFIO_ASSERT_NOT_NULL(out_vf_bdf); + + snprintf_assert(path, PATH_MAX, "/sys/bus/pci/devices/%s/virtfn%d", pf_bd= f, i); + readlink_base(path, "%s", out_vf_bdf); + + return out_vf_bdf; +} + +int sysfs_iommu_group_get(const char *bdf) +{ + char path[PATH_MAX]; + int group; + + snprintf_assert(path, PATH_MAX, "/sys/bus/pci/devices/%s/iommu_group", bd= f); + readlink_base(path, "%d", &group); + + return group; +} + +char *sysfs_driver_get(const char *bdf) +{ + char driver_path[PATH_MAX]; + char path[PATH_MAX]; + char *out_driver; + int ret; + + snprintf_assert(path, PATH_MAX, "/sys/bus/pci/devices/%s/driver", bdf); + ret =3D readlink_safe(path, driver_path); + if (ret =3D=3D -1) { + if (errno =3D=3D ENOENT) + return NULL; + + VFIO_FAIL("Failed to read %s\n", path); + } + + out_driver =3D strdup(basename(driver_path)); + VFIO_ASSERT_NOT_NULL(out_driver); + + 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 efbf9ab63cea7..80e8e5bebd7ca 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) { @@ -204,24 +202,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 { @@ -230,7 +210,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.54.0.545.g6539524ca2-goog From nobody Wed Jun 10 17:15:45 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 59BB64A13A7 for ; Tue, 5 May 2026 21:28:45 +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=1778016527; cv=none; b=PhfKg7bnbAiHBtq3ouqFCseVtnvj2PV2M/jHqnQophV00v2z6l56E3K2XaPysEH7XF7JccHpmUI4c6EW0xQiSMGgKRiMz1N4bVogbTldEV787UyG13vZnKdCGB9l5zmxKFJWBaLuM+i9cFgOSYAxKvUnLubz4Dm5MF1d1RJCqxM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778016527; c=relaxed/simple; bh=GB4ku3pK7qy2Q3fNgcaU7TIQGx6FeyMOCfJZAay7Ke4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Yf/bX7C5/ACqCKcgmB67xeagbNiq0UUbf4mWh0shLQ4sF0Po/k+n81YlT3/VNaApnAdGGKrD5e10DBpTmik67dKQYcHRMfSzZZNVvc/btjXi+z5PhWweubRYuXu9HssIs2kPLsKZtLFtdDyfcnAtBSfSwucSc5FjZ8ylvGhGRVA= 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=e2FJwQ/f; 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="e2FJwQ/f" Received: by mail-oa1-f73.google.com with SMTP id 586e51a60fabf-409037c3f0bso8872602fac.1 for ; Tue, 05 May 2026 14:28:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778016524; x=1778621324; 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=XN1YjqFohZ0l8R/PyhFFwsCbsJd+viLRi9YWcXkC5ec=; b=e2FJwQ/fYkgPmo+VyNiSVPePRFaenEQVgvTBRqGZiWF2EHzFX5yfqlQQBFpvRIjyl9 qZX4SD+cUQB3x3xZ7L8KBfOeBuxi86I2N5OITD5j23I/43l/IY2oqWasfVr9IOa7Ibet IL8Z2UmIIAAmPZJFi3S5VrA8Y3sBcEWGZWO69bImBvtv6W3ElCnUMj8uge/6QSq+ublV LY+m/HZruGkHFZwLpd/QNGJE/tvyj6Kf7z0z6zkDYJI12kK3ZjIcI/XrYLeKPGlitQy4 UX2q2kE1yQaLzpleJk+Bnq6uU3/EpY1DlSBaKS1sV/CmbXaVIARse95JApa98XeUv9cD jBPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778016524; x=1778621324; 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=XN1YjqFohZ0l8R/PyhFFwsCbsJd+viLRi9YWcXkC5ec=; b=LobOehfgO3tDC5gVPw+/Pj3eFZDoVi2ZsJaPMPsdEt6Oqs5iH/uI3I3QMiHRGfnALk EGFN1MOzNn94HtFVQujwmTsnXYbgrHNxltEiRxd9/2vJg8DKlg8SfKcGsbrcDBWjOLsm lDDHG+H+PnuYW/1vWqslq1m3sPzgtYxpHxUa/haL5zJyopLPCal7UDDHZFQ5nePXpZV2 B83EUUZgCr9+C6jnqpEkshqY1eD9lfxiwJ1GQupB1yuE8phPUUKexS62TkkDArwynHXf Pf5voxCfcG2C76BCgVOCqTrePCiK0Jb7mQ23AXs82l6RpL65WvHva0G/2WNae4DvnD/H YJgw== X-Forwarded-Encrypted: i=1; AFNElJ8RMCjV6rf69NnW2EfLY0yjvfxFq4QVGF3mdBA1Ne6apb7MVWHs9Xu+Ld2bl8OhHtPQrrawhK/M+0E0rhQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxvYaSlkxVRA2gHkVU+IR1Jo37KizEwfVcIuB6OMdTbiRGbbvQm eLVXhzZ1nG3tDECDfx0w4iGeXkiWEB468OzhthITYtQGS+DWb1/zkf3AuPZZz6sVuEnrLQLCqoQ oWueJh7KBCQ== X-Received: from ilmx7.prod.google.com ([2002:a92:de07:0:b0:4fe:76d8:c6ad]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:4c06:b0:694:a1d1:f363 with SMTP id 006d021491bc7-69998cbc264mr348924eaf.15.1778016524166; Tue, 05 May 2026 14:28:44 -0700 (PDT) Date: Tue, 5 May 2026 21:28:34 +0000 In-Reply-To: <20260505212838.1698034-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: <20260505212838.1698034-1-rananta@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260505212838.1698034-5-rananta@google.com> Subject: [PATCH v8 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 Reviewed-by: Vipin Sharma Tested-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 e26917ee169c5..4437f04660089 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 80e8e5bebd7ca..e59b6689f4576 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 @@ -222,7 +224,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; @@ -240,8 +262,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) @@ -302,12 +323,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); } @@ -322,7 +351,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 @@ -330,7 +360,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 @@ -346,9 +376,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.54.0.545.g6539524ca2-goog From nobody Wed Jun 10 17:15:45 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 46AD54ADD81 for ; Tue, 5 May 2026 21:28:46 +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=1778016529; cv=none; b=RG9s2/Qyy2n7HqiBsAUiE8ZYGCfcsWsgzyvobnVH2bgDRpvhcGh4syYl9UoTKGukvFl4MGibJgaknPiJ/BZ/ADuBHOpn4TP23lI5kbN4HGny45Ny6aQISUipaLhcDF+Srkng/Xfu66/8QMK7zLOlHyVvzBlwZ9Z8rGQjN/PVSiQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778016529; c=relaxed/simple; bh=vYDCQu3hcomQSugSfAjIukmGYxxUvvzFcXYHkbRSEB8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=BB5wgDsg18lpaJ6cxwoTtpbWU3FyrVDWJ8kC8VFgknXGDoT90Z6jpIbp6rMjZY3k1HxRHqTwbkfrHMESHyUxHpJEvf0KJdrsaOaEq7z3tIF6JEXh3TU/FFWohLJXn0xGAXp3CwtddKPtUA5F+938DnK56OeTyrZ+cOP80p0DJKc= 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=ENUOZS4G; 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="ENUOZS4G" Received: by mail-ot1-f74.google.com with SMTP id 46e09a7af769-7d4c7afa9e0so10029867a34.0 for ; Tue, 05 May 2026 14:28:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778016525; x=1778621325; 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=91qp2yf2pBn59DztCHuRcXM+/bOVP8MbUY5pGcMns7Y=; b=ENUOZS4Gk+pHsLfuY+Rt4SphrFECI0+h7f9EYX5v5UWPa2Z8i4ZKaJ9Jy1+Rbu1DV4 e++hxVSWzeeaNN/FyZ6vsh+0xTjvH1sgnJ64Pyo6negrUC5q+glZk0SzQMPhjLHCo4iF d6UjM5Iz/SVZcP9ZBMZQAdJY49BDwZcSNFEsJC2nreZbYI+252zvtP2KvAle9W98HZ2r yPws/fYRHwWT1PBTfSGA71WHutdd8Y/cFpzXy42iZp1ehrOP/zTxUcBev9PXRdgENU+B xDsn33C1tt5jeczSVMIIwArpjHLmfqtTM7995nBuK3lv3rE5j0LS+H0JIDbMNG08Yr3d kmIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778016525; x=1778621325; 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=91qp2yf2pBn59DztCHuRcXM+/bOVP8MbUY5pGcMns7Y=; b=q79MXy+7qqn2ALSCq5JHCjB/i4l5658f2sB99L99vQ68rcHG60G2hAFYGLMrsAZs4t M7IlJO6h9IBk3l/J8ixKvTcG5zEu+yJyWanqsVeLZORLjO+H66WtLTOTG2z9lZwm1+G+ /A88/6rmQf+hZwNw4OtZR+cXmAfkCcI7CohuXo2a0vcSFpNfcbDdAuylkE3jD8o5xFaE 5wo0HYgxwTkWq6x77jj80yvs71KIVEm+Mm92yxbRb7yhdECQnPbSD/JO4n/cvcOkUnHI UaoC0tcVbiUtAn1Stm8JXR8WgS4YHsmDoXlttYFLxnvDIUDM3xpgfrW/oq4Jz/snxu3p cmpA== X-Forwarded-Encrypted: i=1; AFNElJ8FCnMye3wed+YeBxDDZSMEvfz2Ga8JdnW1xne8NvM25QJdRItwW6ke2ybBrrsG/wbQ+XRtqCFnJy8KG1M=@vger.kernel.org X-Gm-Message-State: AOJu0Yw3rG4bXFbhdcnqrVxzkLb/cCVwtCeRfmfyPZoNpUjdypmypmh7 WZnP1wBV0trUSq+t+rUiSkfvJFL4fMs+MN9uAX2lzj9Fa5KB6vkah0+lowK9Pi/Ib/3em3IMIdP 8vg3MTvFe0A== X-Received: from ilto2.prod.google.com ([2002:a05:6e02:922:b0:4fa:21bf:973b]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:1789:b0:695:a638:c6ba with SMTP id 006d021491bc7-69998c8c89cmr395270eaf.7.1778016524970; Tue, 05 May 2026 14:28:44 -0700 (PDT) Date: Tue, 5 May 2026 21:28:35 +0000 In-Reply-To: <20260505212838.1698034-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: <20260505212838.1698034-1-rananta@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260505212838.1698034-6-rananta@google.com> Subject: [PATCH v8 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 Reviewed-by: Vipin Sharma Tested-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 e59b6689f4576..53e01cdb2b4e5 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -204,7 +204,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), @@ -224,8 +224,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 @@ -240,18 +240,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 @@ -261,7 +264,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 @@ -323,8 +332,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), @@ -338,7 +346,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) @@ -351,15 +370,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.54.0.545.g6539524ca2-goog From nobody Wed Jun 10 17:15:45 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 06BCC4968EF for ; Tue, 5 May 2026 21:28:46 +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=1778016529; cv=none; b=t3Eyo4zGyYOStcMM3g7B3FxJVv095v1wObpUgmu1PaZa5NCywqIBg821k9HUHgR30axxq62Q/keZFvEuTDKK3Et+COyV2Le0uWq/yeMAf+51UAeiZyc0hdPMqIUOriu0Q7HeUVgv2g/o036KCi1z5/eP6x1cck5CIQP72b2hzoA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778016529; c=relaxed/simple; bh=KQmibtmI+xu5liPsMJf+5p5Lt3l2eDymMQXEWvyYjxM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=d4QItK4ryZM2ckJH7v/Fv6SxO+JNMjfxB3jLAijURRZB1tuA5Wr3YgWPAg/LEUl1hzQ0H6wzojJ2TvtdkP1I3IbRKnrzpFOJF1/zRnGoQ0hC53mhXATW6aYJ8TuudnYCkN35RG6vgWdS/eXQBlf1QutqyS+NDC7lMyXJGNpb0rg= 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=XwWajQfQ; 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="XwWajQfQ" Received: by mail-oo1-f74.google.com with SMTP id 006d021491bc7-69491bd267fso12208399eaf.0 for ; Tue, 05 May 2026 14:28:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778016526; x=1778621326; 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=3tvcZVP6cn5IGaKeTjBEK0jxizc/mIRujnOHig1BUO4=; b=XwWajQfQvWPc7ugFy8/Ys7irEQ5+/H/vKls4Kmy1UDfXlhJHQCP2T45HkgO9LsetFn +CZ4wmozPUHt5vjTx9SrBm/4aNGq4EPqPE38EdV8WGGiPQT4JBgLde8fAxQVKQsgkyso PhsNFQ3GtK8fT97Vw460nI5YmK5C7t97iuqB5y3MbSa7gKN3k0WOerN92WBvPk49YQWy d/LYPPwugJrVu82j5QayS8r0k6PdUKZPZRsG8gKbhaAqMbcgKuYIwa6SQqMZ8E/u0Ppc +FX7u9J11Nyec9rLX7/TXP4fNzr12gXrzfsr/68X7jLRlnBXCpa3Secd8TTngAXJWM9D 7y6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778016526; x=1778621326; 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=3tvcZVP6cn5IGaKeTjBEK0jxizc/mIRujnOHig1BUO4=; b=jaaFSISpPuJpiiwM14leESziIuVRCGqRleXC0cncbwvCd12EubS1z/f+wQQcPzB++7 zcVdK2kgX/UpXekEoxQpwHJ7H1uhikizyw733p4mXXdVkG0TagNfpZLXhDJ2XGQuRSUg 4MAnVcA/TeGaNcmNX78K17MK+dmpVtG4HCL/bJH89SZIuXgPVAfyeq2L9VuJuQ0fMQUX cD/HpTNwUgGp/BK6iKB3+aoBawLK8hSmhg/g88gDG5FBBMY0gVqO45k35RLjKm4yefUE +YA3UmpoTOu71fwn9CIojh9se5EyjMUOCJKxHRXFIs1SwBmjUNtSQdaxxn6HERsqUMDr Drnw== X-Forwarded-Encrypted: i=1; AFNElJ8ELhYE9zTVLq2U1oFNWNsRX3xwQsJOiF3oJahTNY821UyisWUOORhBeyBV+mFl2KeMRZpienE0oBihKdE=@vger.kernel.org X-Gm-Message-State: AOJu0Yzp8m5h6UCY8JG169z+CgNebRVm/sZ1voPf8zo4kdx+LDMJ+JuX gprv9l+h4+lki7TL1YYskZKDLTLYbCQ4FMNxEtz9AGQwPzZwfdwikGjLDm7nWU3OsTIwwdQbPyY FyxXhGacMeQ== X-Received: from ilaf8.prod.google.com ([2002:a05:6e02:1688:b0:4fc:57e4:69b3]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a4a:e914:0:b0:696:1291:fb29 with SMTP id 006d021491bc7-69998cbe145mr363532eaf.24.1778016525870; Tue, 05 May 2026 14:28:45 -0700 (PDT) Date: Tue, 5 May 2026 21:28:36 +0000 In-Reply-To: <20260505212838.1698034-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: <20260505212838.1698034-1-rananta@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260505212838.1698034-7-rananta@google.com> Subject: [PATCH v8 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 Reviewed-by: Vipin Sharma Tested-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 53e01cdb2b4e5..6b855fbcb4a22 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -115,6 +115,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.54.0.545.g6539524ca2-goog From nobody Wed Jun 10 17:15:45 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 696424C0425 for ; Tue, 5 May 2026 21:28:50 +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=1778016531; cv=none; b=RVuY7e8aZ6vWxfUWg6vznlcbetOmmGnMzT9RAXZyyR+3GxQDfUAsmTAhZQ1yeVBATZIHCQB7yFKc86aEAmKzVzo6ANfk07mj+vGZvLHPv+iGgfNl7TNYVRQVkEl1N8iXOjVA9GwzzSTaqyJ5cFvQ/Wnjpe3yCs7dehKnWrQHm4M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778016531; c=relaxed/simple; bh=nWWEHlDRK/55H9sc+sSWQt7sjQhogZRCW4tUvpUazfU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aVkIgKeDLbv0mId5/C6p/LzgO7d46TsqMyrxKCY+YCOiwZRk6Vv3LgrGzypKH1P+5J9mCeLQFar92W0XQT+0gmGuqkabcGvJragfgu86bLTiEbzu/XALi9FHQmSJkaVjc7CypBW2AojnMHIwrG8DUzDx3nQuwt0ZRD8lVpdqMak= 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=Qv20H/vi; 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="Qv20H/vi" Received: by mail-oo1-f74.google.com with SMTP id 006d021491bc7-6961e8fa90bso11854625eaf.0 for ; Tue, 05 May 2026 14:28:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778016527; x=1778621327; 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=/ycq5rNhvfEuPJ4FnDxTrIkEq4BYeEOANTDKltW6JNU=; b=Qv20H/vi6NRYLSvG3zZ68HbU/vT2RLQwmMRdI6IOq+X4H3RPA/A/RXWTobKsYynOkc Zq+rc9QLjBQhXRITjomonq/veysMV0t4gKPUfLKd8vhU2BHObRwTF+IjDclIzNK82bKM d3Gnb9Os0mA/XGqdFJVPmpqXmvJA61sxd9FX9vJtd+Qi32otZDJITphP1rp8T2UvV562 BcdtgrUktPdwdq4EHQAP8oTPhSJtk/QrwUVmnzNnpmfanX6PPTvTYnjAs87UisUyhO4v n+kfKSsZ30m3qFURIDcLQGZHipzqp3vK1hHo1yzt3WvIqbvwm/CAWMQ8vrSSQB9VNGMn HN/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778016527; x=1778621327; 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=/ycq5rNhvfEuPJ4FnDxTrIkEq4BYeEOANTDKltW6JNU=; b=YOY8Tfn092a8FN/iw9eiVSAyMp9nkX6HVY8Ai92WRK8JuW2YrxAqPtPche/2pN8YVo aE+GSCtH6JIA2ksuoGadtMLa/W1jZQ4TGSohyifTnW+01EFS5miITuVyPSluIYCWek1r oafAfDsQmC7cZsY14FbY0CYg4iArS9DHX/t6XFiFuDlOOgwcvgObl4m2HFCSsvHSchwT XKBY7f6JeXxhn61cBDfMRn5InOy8KHDxl26GlsBKYBUP2ZBTKW79kkboLE+eADyHgsH3 KXmmnW0mKleurvlAqhvCCbWLU0hlUq5moPN+g5VEs5LMkEPJOoEQo6z+NOapQSjNUmyq 463A== X-Forwarded-Encrypted: i=1; AFNElJ95uSjT7WiynACSgbTJnfzZqNy9Ko04mCpYAXQ8OfDMbU8g/zBiBuZCyqgUt3rzZBt7ajUq+wzMsnrq4Hc=@vger.kernel.org X-Gm-Message-State: AOJu0YwQx/UNholXoETexwDrjr0TXdePZLMMVh3vXXNMlO6gV7cnl96D GSCMIwndNPUN3qq8PW4z07ZytTuaDr89i40xGtZvin8ETsmvXOW6EzCKPT4RathTGytOn0lRwzX fCi09HYyQHA== X-Received: from ilbdz7-n1.prod.google.com ([2002:a05:6e02:4407:10b0:4f9:ee58:8f69]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:81e:b0:696:7f04:eca2 with SMTP id 006d021491bc7-69998c90567mr355495eaf.9.1778016527078; Tue, 05 May 2026 14:28:47 -0700 (PDT) Date: Tue, 5 May 2026 21:28:37 +0000 In-Reply-To: <20260505212838.1698034-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: <20260505212838.1698034-1-rananta@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260505212838.1698034-8-rananta@google.com> Subject: [PATCH v8 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 Reviewed-by: Vipin Sharma Tested-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 6b855fbcb4a22..94dc5fcecbeb6 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -421,7 +421,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 @@ -432,6 +432,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 @@ -464,5 +478,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.54.0.545.g6539524ca2-goog From nobody Wed Jun 10 17:15:45 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 0C63F4C0413 for ; Tue, 5 May 2026 21:28:49 +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=1778016533; cv=none; b=qqNl5t1okHHZQrNwevj+XFMPkE79opqym1nTs8GtBQl7B8C+IEeLQ7JuJIRPu/rJEEuG+e/XmJnE2cg7LEgHfqw2Xa/JeJzAJ9oerF9d/1VjeFWiEp3Af3S3hD69w9tNDsjLDpRHhnAJK4cHG7d4lEoudnCSTKMzngRK9RNXdSU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778016533; c=relaxed/simple; bh=maXaugutjtZYAm4sRDneiyDXHzDeaLzoh6JDTFiH1Rk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=u8DSRcOI+08Xr9AF7GFsFn/yznyzwO+x8uBNtfBgbYWakN7Lcr0COnGWkyqEC3DiTh1nY7MFXz7gG8vf6j3GmVRKXA9j94wigt+8M9HH2XEE1vJ2V12xaIVv0jbHnot5lBTINs5tr2K3aa0Lw8ZU5xCO/N6AdjDWgILQY3OlV6Q= 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=AIigyocl; 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="AIigyocl" Received: by mail-oo1-f73.google.com with SMTP id 006d021491bc7-6948d83a5c0so15487839eaf.1 for ; Tue, 05 May 2026 14:28:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778016528; x=1778621328; 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=aeeuaD10nbSlLro29O11/wH4Kr2qjMV+qBca2WcLDA4=; b=AIigyoclSA+yfhClYZxPBPQ2/FfKzdpi5YrTCgi8I94K6DntpohR4RKp21Yovc5q5A oS0+3jVJxuC6UN55/FJNX9DjHLvJtETnt1Bx7/sTzZQ8T5Dxg7SthBd4rUuqKxoBpmiO 78StDBUTr4VGFMTWLE0QpejAsLs/R33qZVOCGQZYvdp9MzLYepJkx91SJv3PbzOtb2D5 2209uDwa8ME/6vATcA1n3lk20ALuuvOqQ3d4MwBIBeVpe8EaR6fSmmM64dGlewdnmjwh inqBfJL6OQvGn+9vb8jscgHTqp+kTXE3dhIpApK2O8j8VZ8mHaiufvJJ+ujCX0xme0rZ 94Mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778016528; x=1778621328; 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=aeeuaD10nbSlLro29O11/wH4Kr2qjMV+qBca2WcLDA4=; b=SCjVbtdmXPmceTgJjtCEv2yyTmHxMqZCKnviL4i7mLkk24lADQSxh18HDdXbMJRBrN x+x+xKtdHCSFofnWoTWyGLBZaWzEsVkvbsTN9ppvI9WfJBK85IzXYXzMC+N+AMUQdM2V iOrvw1jHX3OJkumYYFwl0mAU0TOk1icRnoj7e6cW1X05HJKZHOMmaSdJPIGNSRBJOLEc IUoPUOqTSPbsgRZTHLF7seW6UuvuCC1FmILqzMgnU9kgRzIkMDKfitWiMFEqGT2RRvIV cZnK3spqWDothoF3Me1bideQmwSAHH51AQ/iGSMIpdlptDQz/CapddrZ8iq5RDFBNzTs saBQ== X-Forwarded-Encrypted: i=1; AFNElJ/4xGm2TsYkCLfddALK15SWiKxegewby9UOtcRK4bX4a9ZWGbL1B+MAl3Eb2wCTfX28CjGb56UrgT2mGbI=@vger.kernel.org X-Gm-Message-State: AOJu0Yy+d2uImnO9SeiLM/8Oiaz2jX13pJjwXJm6EmYudOPBVV5DB6hk qgB4VR90i0GVc24J3N/TO6gzEj88wDiK7tqOCPE+zy/8pSQw5NxcgHUbW2djStzOZebO2MeabF0 0P11VaQ+tbw== X-Received: from ilpy1.prod.google.com ([2002:a92:c741:0:b0:4fb:4c9c:2186]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:607:b0:694:a30f:a6d6 with SMTP id 006d021491bc7-69998ceebfamr405274eaf.25.1778016527917; Tue, 05 May 2026 14:28:47 -0700 (PDT) Date: Tue, 5 May 2026 21:28:38 +0000 In-Reply-To: <20260505212838.1698034-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: <20260505212838.1698034-1-rananta@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260505212838.1698034-9-rananta@google.com> Subject: [PATCH v8 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 Reviewed-by: Vipin Sharma Tested-by: David Matlack --- tools/testing/selftests/vfio/Makefile | 1 + .../selftests/vfio/vfio_pci_sriov_uapi_test.c | 217 ++++++++++++++++++ 2 files changed, 218 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 4437f04660089..d77ff5368e72f 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..19d657d00b753 --- /dev/null +++ b/tools/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c @@ -0,0 +1,217 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include "lib/include/libvfio/assert.h" +#include +#include +#include +#include +#include + +#include + +#include "../kselftest_harness.h" + +#define UUID_1 "52ac9bff-3a88-4fbd-901a-0d767c3b6c97" +#define UUID_2 "88594674-90a0-47a9-aea8-9d9b352ac08a" + +static const char *pf_bdf; +static char *vf_bdf; + +static pid_t main_pid; + +static int container_setup(struct vfio_pci_device *device, const char *bdf, + const char *vf_token) +{ + vfio_pci_group_setup(device, bdf); + vfio_container_set_iommu(device); + __vfio_pci_group_get_device_fd(device, bdf, vf_token); + + /* The device fd will be -1 in case of mismatched tokens */ + return (device->fd < 0); +} + +static int iommufd_setup(struct vfio_pci_device *device, const char *bdf, + const char *vf_token) +{ + vfio_pci_cdev_open(device, bdf); + return __vfio_device_bind_iommufd(device->fd, + device->iommu->iommufd, vf_token); +} + +static int device_init(const char *bdf, struct iommu *iommu, + const char *vf_token, struct vfio_pci_device **out_dev) +{ + struct vfio_pci_device *device =3D vfio_pci_device_alloc(bdf, iommu); + int ret; + + if (iommu->mode->container_path) + ret =3D container_setup(device, bdf, vf_token); + else + ret =3D iommufd_setup(device, bdf, vf_token); + + *out_dev =3D device; + return ret; +} + +static void device_cleanup(struct vfio_pci_device *device) +{ + if (!device) + return; + + 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) { + struct vfio_pci_device *pf; + struct vfio_pci_device *vf; + struct iommu *iommu; + char *pf_token; +}; + +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); + +FIXTURE_SETUP(vfio_pci_sriov_uapi_test) +{ + self->iommu =3D iommu_init(variant->iommu_mode); + + self->pf_token =3D UUID_1; + ASSERT_EQ(device_init(pf_bdf, self->iommu, self->pf_token, &self->pf), 0); +} + +FIXTURE_TEARDOWN(vfio_pci_sriov_uapi_test) +{ + device_cleanup(self->vf); + device_cleanup(self->pf); + iommu_cleanup(self->iommu); +} + +/* + * This asserts if the VF device is successfully created if its token matc= hes + * with the token used to create/override the PF or fails during a mismatc= h. + */ +#define ASSERT_COND_VF_CREATION(_ret) do { \ + if (!variant->vf_token || strcmp(self->pf_token, 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) +{ + int ret; + + ret =3D device_init(vf_bdf, self->iommu, variant->vf_token, &self->vf); + ASSERT_COND_VF_CREATION(ret); +} + +/* + * After closing the PF, validate if the VF access still needs the right t= oken. + */ +TEST_F(vfio_pci_sriov_uapi_test, pf_early_close) +{ + int ret; + + device_cleanup(self->pf); + + /* Clean the 'pf' to avoid calling device_cleanup() again. */ + self->pf =3D NULL; + + ret =3D device_init(vf_bdf, self->iommu, variant->vf_token, &self->vf); + ASSERT_COND_VF_CREATION(ret); +} + +/* + * 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) +{ + int ret; + + self->pf_token =3D UUID_2; + vfio_device_set_vf_token(self->pf->fd, self->pf_token); + + ret =3D device_init(vf_bdf, self->iommu, variant->vf_token, &self->vf); + ASSERT_COND_VF_CREATION(ret); +} + +static void vf_teardown(void) +{ + /* + * The child processes, created by TEST_F()s, inherits this atexit() + * handler. Hence, check and destroy the VF only when the main/parent + * process exits. + */ + if (getpid() !=3D main_pid) + return; + + free(vf_bdf); + sysfs_sriov_numvfs_set(pf_bdf, 0); +} + +static void vf_setup(void) +{ + char *vf_driver; + int nr_vfs; + + nr_vfs =3D sysfs_sriov_totalvfs_get(pf_bdf); + if (nr_vfs <=3D 0) + ksft_exit_skip("SR-IOV may not be supported by the PF: %s\n", pf_bdf); + + nr_vfs =3D sysfs_sriov_numvfs_get(pf_bdf); + if (nr_vfs !=3D 0) + ksft_exit_skip("SR-IOV already configured for the PF: %s\n", pf_bdf); + + /* Create only one VF for testing */ + sysfs_sriov_numvfs_set(pf_bdf, 1); + + /* + * Setup an exit handler to destroy the VF in case of failures + * during further setup at the end of the test run. + */ + main_pid =3D getpid(); + VFIO_ASSERT_EQ(atexit(vf_teardown), 0); + + vf_bdf =3D sysfs_sriov_vf_bdf_get(pf_bdf, 0); + + /* + * The VF inherits the driver from the PF. + * Ensure this is 'vfio-pci' before proceeding. + */ + vf_driver =3D sysfs_driver_get(vf_bdf); + VFIO_ASSERT_NE(vf_driver, NULL); + VFIO_ASSERT_EQ(strcmp(vf_driver, "vfio-pci"), 0); + free(vf_driver); + + printf("Created 1 VF (%s) under the PF: %s\n", vf_bdf, pf_bdf); +} + +int main(int argc, char *argv[]) +{ + pf_bdf =3D vfio_selftests_get_bdf(&argc, argv); + vf_setup(); + + return test_harness_run(argc, argv); +} --=20 2.54.0.545.g6539524ca2-goog