From nobody Tue Dec 16 12:21:17 2025 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 E7D93285C91 for ; Wed, 10 Dec 2025 18:14:22 +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=1765390464; cv=none; b=DpXJJDu79FvUFfpzC0TmMrnATzY1O32c9RGZifIrjkXei3ps8ud2A476dHsdDe4Sfq9C3uTHofnrudrD8XLMjva0FwkPIO7VUGHJXjGLxNB5aVAjIrFRJ+6Yb5cKI/Ih7nNTzMK120THpNYNT/uutVnInrZ6g/uv9ZOwa/m+PIc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765390464; c=relaxed/simple; bh=lhAXB7A8G2v17fgR7GievtgMw7aHmutfq6cVjY4fsC0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WLtu8ACtrLW7wg4Cn2aGoJEU9/1dOr+L/jkpsBNl0nM+148p2DAWxUqdKf4peocS3VoYwW5cinUSHGXOu6eJrxdrWY+KWw+lJQJt40SVl+I0kZvV4K2AH3GAYIGXh73iE3G4mnikVy1DW0aTO55iO2T3Xkoa137YSsk4c08lNKA= 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=0KfjAsuP; 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="0KfjAsuP" Received: by mail-oo1-f74.google.com with SMTP id 006d021491bc7-657537cef7cso131533eaf.1 for ; Wed, 10 Dec 2025 10:14:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1765390462; x=1765995262; 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=TA+dFiCFfE/neZMvBvdBl7grrAyeiSJc0c8DLMfcbps=; b=0KfjAsuPORKwXQd0WlfyxrATau9rPcSTrVXqhEcNzGHUuQ4TJQ6fBYl52uGeFEyFqE nBYoeajrCHwzVJ9TR4DjuI7Z0lb9u6ZfUeuNigIP73KBy9nwUgbsapU56jWtqWFKubfJ 1tukrWic+a0IndMRtCv3EGHGivYFDgrlvssK0IUD2W+JrdW7TVxi1s+mPpeN5u+hSt2H 70XlufZMsuxK6xDWJOpqPpeMfKx3ZkmgI7Q2qOgWyf588D21EDlq8UEWrVBT5T4bJmYN JkIVV+l3W9SDORAeJ4aVS5NnZhHh+5tCnFyFAyyqm81A+FMF3vi9IQsnt0APaQ89CO3v jt9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765390462; x=1765995262; 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=TA+dFiCFfE/neZMvBvdBl7grrAyeiSJc0c8DLMfcbps=; b=nJPtqHu+6269Nlbr/EXnV7qjfWMcn70aoUXzsy1MBmwsEamzZlL8WF07sdLODIvZOv PdjjDddD/1Nw2FILGFqagVld7SKDNPM1IK7SSR82RiaPDNI3aCWiDFiDPx2yUO2R/Z7r ebat5EMmcOID0scMt7R5+x2CQD1Iuy6+7gKo1Wn8nn6ZB/K8m7DxihH7TprX4bVGa75c CuuHCTvPzKbPvMan7lX92xApqarY1VL64qqiT7LCQiNXc6q/+MFFY04T4lrp26c4c8Ko c4rhLDErZAryyUQN+Ty5Bo9DpHMd/Nu0oKwlfFcv2Qda5hz4EMD/rxt8/IBUYs0hb5Pj ge6A== X-Forwarded-Encrypted: i=1; AJvYcCXZYRfV5MjINXsdnyX47SXaAktWYeZfgpFqGidD5KO2gWj1MZJFX9n5bT4mIShxniepCpe9qS11bBX7a1w=@vger.kernel.org X-Gm-Message-State: AOJu0Yy5lXc/I04AaSCPNFCv+kBEDWsyPHBX52bYcSUDZI7/IwtPOXol Aeps8f5wgw0vpHx4IKZtYElZW8l/sKaHkx0jPDjhLiwWhYjU+2+JVZ+/JvmjWCg8pwRgA032sLM enlGTqAT1/g== X-Google-Smtp-Source: AGHT+IGWhLSFoMUjQWfjmNMZkoR8qR5Wr00Wt6Ecah3pTSGPB9crDFJhJUsYYCKig83u1qz0VeKbkBJ3k3Zh X-Received: from ilbee24.prod.google.com ([2002:a05:6e02:4918:b0:438:237b:ed42]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:905:b0:659:9a49:900d with SMTP id 006d021491bc7-65b2ad632a6mr2034002eaf.58.1765390461916; Wed, 10 Dec 2025 10:14:21 -0800 (PST) Date: Wed, 10 Dec 2025 18:14:12 +0000 In-Reply-To: <20251210181417.3677674-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: <20251210181417.3677674-1-rananta@google.com> X-Mailer: git-send-email 2.52.0.239.gd5f0c6e74e-goog Message-ID: <20251210181417.3677674-2-rananta@google.com> Subject: [PATCH v2 1/6] vfio: selftests: Introduce snprintf_assert() From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson , Alex Williamson Cc: 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 --- .../vfio/lib/include/libvfio/assert.h | 5 +++++ .../selftests/vfio/lib/vfio_pci_device.c | 8 +++---- .../selftests/vfio/vfio_dma_mapping_test.c | 6 +++--- .../selftests/vfio/vfio_pci_device_test.c | 21 ++++++++++--------- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/assert.h b/to= ols/testing/selftests/vfio/lib/include/libvfio/assert.h index f4ebd122d9b6f..77b68c7129a64 100644 --- a/tools/testing/selftests/vfio/lib/include/libvfio/assert.h +++ b/tools/testing/selftests/vfio/lib/include/libvfio/assert.h @@ -51,4 +51,9 @@ VFIO_ASSERT_EQ(__ret, 0, "ioctl(%s, %s, %s) returned %d\n", #_fd, #_op, #= _arg, __ret); \ } while (0) =20 +#define snprintf_assert(_s, _size, _fmt, ...) do { \ + int __ret =3D snprintf(_s, _size, _fmt, ##__VA_ARGS__); \ + VFIO_ASSERT_LT(__ret, _size); \ +} while (0) + #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_ASSERT_H */ diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index 13fdb4b0b10f3..64a19481b734f 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -188,7 +188,7 @@ static unsigned int vfio_pci_get_group_from_dev(const c= har *bdf) unsigned int group; int ret; =20 - snprintf(sysfs_path, PATH_MAX, "%s/%s/iommu_group", PCI_SYSFS_PATH, bdf); + snprintf_assert(sysfs_path, PATH_MAX, "%s/%s/iommu_group", PCI_SYSFS_PATH= , bdf); =20 ret =3D readlink(sysfs_path, dev_iommu_group_path, sizeof(dev_iommu_group= _path)); VFIO_ASSERT_NE(ret, -1, "Failed to get the IOMMU group for device: %s\n",= bdf); @@ -208,7 +208,7 @@ static void vfio_pci_group_setup(struct vfio_pci_device= *device, const char *bdf int group; =20 group =3D vfio_pci_get_group_from_dev(bdf); - snprintf(group_path, sizeof(group_path), "/dev/vfio/%d", group); + snprintf_assert(group_path, sizeof(group_path), "/dev/vfio/%d", group); =20 device->group_fd =3D open(group_path, O_RDWR); VFIO_ASSERT_GE(device->group_fd, 0, "open(%s) failed\n", group_path); @@ -279,7 +279,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf) cdev_path =3D calloc(PATH_MAX, 1); VFIO_ASSERT_NOT_NULL(cdev_path); =20 - snprintf(dir_path, sizeof(dir_path), "/sys/bus/pci/devices/%s/vfio-dev/",= bdf); + snprintf_assert(dir_path, sizeof(dir_path), "/sys/bus/pci/devices/%s/vfio= -dev/", bdf); =20 dir =3D opendir(dir_path); VFIO_ASSERT_NOT_NULL(dir, "Failed to open directory %s\n", dir_path); @@ -289,7 +289,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf) if (strncmp("vfio", entry->d_name, 4)) continue; =20 - snprintf(cdev_path, PATH_MAX, "/dev/vfio/devices/%s", entry->d_name); + snprintf_assert(cdev_path, PATH_MAX, "/dev/vfio/devices/%s", entry->d_na= me); break; } =20 diff --git a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c b/tools/t= esting/selftests/vfio/vfio_dma_mapping_test.c index 5397822c3dd4b..3a0bea5e26480 100644 --- a/tools/testing/selftests/vfio/vfio_dma_mapping_test.c +++ b/tools/testing/selftests/vfio/vfio_dma_mapping_test.c @@ -45,9 +45,9 @@ static int intel_iommu_mapping_get(const char *bdf, u64 i= ova, FILE *file; char *rest; =20 - snprintf(iommu_mapping_path, sizeof(iommu_mapping_path), - "/sys/kernel/debug/iommu/intel/%s/domain_translation_struct", - bdf); + snprintf_assert(iommu_mapping_path, sizeof(iommu_mapping_path), + "/sys/kernel/debug/iommu/intel/%s/domain_translation_struct", + bdf); =20 printf("Searching for IOVA 0x%lx in %s\n", iova, iommu_mapping_path); =20 diff --git a/tools/testing/selftests/vfio/vfio_pci_device_test.c b/tools/te= sting/selftests/vfio/vfio_pci_device_test.c index ecbb669b37651..723b56b485f67 100644 --- a/tools/testing/selftests/vfio/vfio_pci_device_test.c +++ b/tools/testing/selftests/vfio/vfio_pci_device_test.c @@ -39,16 +39,17 @@ FIXTURE_TEARDOWN(vfio_pci_device_test) iommu_cleanup(self->iommu); } =20 -#define read_pci_id_from_sysfs(_file) ({ \ - char __sysfs_path[PATH_MAX]; \ - char __buf[32]; \ - int __fd; \ - \ - snprintf(__sysfs_path, PATH_MAX, "/sys/bus/pci/devices/%s/%s", device_bdf= , _file); \ - ASSERT_GT((__fd =3D open(__sysfs_path, O_RDONLY)), 0); \ - ASSERT_GT(read(__fd, __buf, ARRAY_SIZE(__buf)), 0); \ - ASSERT_EQ(0, close(__fd)); \ - (u16)strtoul(__buf, NULL, 0); \ +#define read_pci_id_from_sysfs(_file) ({ \ + char __sysfs_path[PATH_MAX]; \ + char __buf[32]; \ + int __fd; \ + \ + snprintf_assert(__sysfs_path, PATH_MAX, "/sys/bus/pci/devices/%s/%s", \ + device_bdf, _file); \ + ASSERT_GT((__fd =3D open(__sysfs_path, O_RDONLY)), 0); \ + ASSERT_GT(read(__fd, __buf, ARRAY_SIZE(__buf)), 0); \ + ASSERT_EQ(0, close(__fd)); \ + (u16)strtoul(__buf, NULL, 0); \ }) =20 TEST_F(vfio_pci_device_test, config_space_read_write) --=20 2.52.0.239.gd5f0c6e74e-goog From nobody Tue Dec 16 12:21:17 2025 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 A1B972868A6 for ; Wed, 10 Dec 2025 18:14:23 +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=1765390465; cv=none; b=fd/o+yAnu5uYlnCLuT7WiYyT1xke1X2tMPROhF/00b69cUmJy8nmTHSDZEKr0QZ/HIwPEXc43Rn7CKwYFGCXLW+rGuBANpbUHMEoAvetqRTc+aZZbKs4H22FTn0b3Aro4kUah1BD1dr3s0SzDcfzQrLoBulKt1uHJUsfcPd3kbU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765390465; c=relaxed/simple; bh=/ESBV/uLL2EfJ9csOv0gKPED2tI1fK2KD3Shy693wVU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=H37I4Pj+KPYqWTKNHmGAuK7XrTmLagHz0T5gFs+jjIakSbFiO4tIxSSJQsTmEIQbjqgu1SLgtZ49P9apCzX8BhximWxI/clJEP3GS+renWeDDTV2re/grnLRmoz3FQS44AMt/9/MwGGcU/S1knIxMob2aulTFTUvrDi15OXp/rw= 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=L254X9hK; 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="L254X9hK" Received: by mail-oo1-f73.google.com with SMTP id 006d021491bc7-6574d3d44f9so121114eaf.3 for ; Wed, 10 Dec 2025 10:14:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1765390462; x=1765995262; 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=OADA+w36zyF2QwiRAwxFp/cNGe8R1v4XJPBiRDY9Wv0=; b=L254X9hKt5Mx1TcHuw9Ns03lmTl7p5PLTothqUBRx1xWrzXsO7jDTWRG2ZMAUYyjuZ kk6qzR1I46QA/wKqNciFt7/4/aewL+SN7jbZB9BW/Q5dlZJkPg8EuN3kNbCotTWipp3w 5I4xjz82DU8xZJt7Gb3Dn38jAJPBV+r4GBLl2DN7ElDLGsird12+AP/xvB209MTZ01i1 wi35XnUC3u6zRfzMB5TKysvk/E0ZKfnd4G0kZmyK1da1+c5hKAZ6q8HsacVfC8JJBmMi jFPeqUuzwp+/5lH5Y0kHVOcYe9xpThUxQ8bEiQ7ljJ50Id/qUXLaLSK+aj1GAmn4qKIx DlPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765390462; x=1765995262; 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=OADA+w36zyF2QwiRAwxFp/cNGe8R1v4XJPBiRDY9Wv0=; b=N1I0FQ4/z4u4xJ6BKWe/HN6QL+bBaFNe1E9vNHHscKuXSe7mmGatZkk7Dbf7Mls/wM czmDzFCZLV8KivC+JkV5HBj8ME7wO/I2v67Kgz3R0HHuDxwvDXiy10qZz8gEIvGROOOy TYJ/0r3EWOT0Z+pTxjBqmOpbNLMFLUzb187aSiL9421sqAED/lY9BB4iizAIz6FT9ruH pDPwgPG3eXlAM+nhIsG/u+b5bOqd9q6iBJUUd+G2OZiaoebRnAoZucIiMBNkEX4bE12/ l6LWbYIw+KjHnOB6UefLLGa1Mh8Qk2OG5kRdou3i/J/kxwGEjf5+OuK82uchzYxvmiD+ Q5MA== X-Forwarded-Encrypted: i=1; AJvYcCWfN0DYm/183hpPoM0IhtoogH7+3gkiNZVNC2/8ghn9YDvkQ6vUUAaoIWj5T0c4us8Gf4/xc8MCkmTqAa4=@vger.kernel.org X-Gm-Message-State: AOJu0YzsXNEJVdhTUXGF4+58TygvzNpHFU0qzLauCV3oq++LrUBSrYoi v8HcaCLhEuvxbW0E/KzBrxal3pTMaTAc4lvRxcPLwwCPYg6QenYWZvtz7ncSIGaWnrhKVC6DHh4 qmNuJImZhUw== X-Google-Smtp-Source: AGHT+IEuUilpfnFwrJTIh0iqV5NAI2XsS8nmu4AsQnCNm+ygdHl/IjoxgPSIRn1z8y5iHoKh8fA4kQBxFgRB X-Received: from ilbbz2.prod.google.com ([2002:a05:6e02:2682:b0:436:f324:45]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:2917:b0:659:9a49:9017 with SMTP id 006d021491bc7-65b2ad8f869mr2143102eaf.68.1765390462665; Wed, 10 Dec 2025 10:14:22 -0800 (PST) Date: Wed, 10 Dec 2025 18:14:13 +0000 In-Reply-To: <20251210181417.3677674-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: <20251210181417.3677674-1-rananta@google.com> X-Mailer: git-send-email 2.52.0.239.gd5f0c6e74e-goog Message-ID: <20251210181417.3677674-3-rananta@google.com> Subject: [PATCH v2 2/6] vfio: selftests: Introduce a sysfs lib From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson , Alex Williamson Cc: 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 liibrary 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_get_device_group() to align with other function names. Signed-off-by: Raghavendra Rao Ananta --- .../selftests/vfio/lib/include/libvfio.h | 1 + .../vfio/lib/include/libvfio/sysfs.h | 16 ++ tools/testing/selftests/vfio/lib/libvfio.mk | 1 + tools/testing/selftests/vfio/lib/sysfs.c | 151 ++++++++++++++++++ .../selftests/vfio/lib/vfio_pci_device.c | 22 +-- 5 files changed, 170 insertions(+), 21 deletions(-) create mode 100644 tools/testing/selftests/vfio/lib/include/libvfio/sysfs.h create mode 100644 tools/testing/selftests/vfio/lib/sysfs.c diff --git a/tools/testing/selftests/vfio/lib/include/libvfio.h b/tools/tes= ting/selftests/vfio/lib/include/libvfio.h index 279ddcd701944..bbe1d7616a648 100644 --- a/tools/testing/selftests/vfio/lib/include/libvfio.h +++ b/tools/testing/selftests/vfio/lib/include/libvfio.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include =20 diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/sysfs.h b/too= ls/testing/selftests/vfio/lib/include/libvfio/sysfs.h new file mode 100644 index 0000000000000..1eca6b5cbcfcc --- /dev/null +++ b/tools/testing/selftests/vfio/lib/include/libvfio/sysfs.h @@ -0,0 +1,16 @@ +/* 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_get_sriov_totalvfs(const char *bdf); +int sysfs_get_sriov_numvfs(const char *bdf); +void sysfs_set_sriov_numvfs(const char *bdfs, int numvfs); +void sysfs_get_sriov_vf_bdf(const char *pf_bdf, int i, char *out_vf_bdf); +bool sysfs_get_sriov_drivers_autoprobe(const char *bdf); +void sysfs_set_sriov_drivers_autoprobe(const char *bdf, bool val); +void sysfs_bind_driver(const char *bdf, const char *driver); +void sysfs_unbind_driver(const char *bdf, const char *driver); +int sysfs_get_driver(const char *bdf, char *out_driver); +unsigned int sysfs_get_device_group(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..5551e8b981075 --- /dev/null +++ b/tools/testing/selftests/vfio/lib/sysfs.c @@ -0,0 +1,151 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include + +#include + +static int sysfs_get_val(const char *component, const char *name, + const char *file) +{ + char path[PATH_MAX] =3D {0}; + char buf[32] =3D {0}; + int fd; + + snprintf(path, PATH_MAX, "/sys/bus/pci/%s/%s/%s", component, name, file); + fd =3D open(path, O_RDONLY); + if (fd < 0) + return fd; + + VFIO_ASSERT_GT(read(fd, buf, ARRAY_SIZE(buf)), 0); + VFIO_ASSERT_EQ(close(fd), 0); + + return strtol(buf, NULL, 0); +} + +static void sysfs_set_val(const char *component, const char *name, + const char *file, const char *val) +{ + char path[PATH_MAX] =3D {0}; + int fd; + + snprintf(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_get_device_val(const char *bdf, const char *file) +{ + sysfs_get_val("devices", bdf, file); +} + +static void sysfs_set_device_val(const char *bdf, const char *file, const = char *val) +{ + sysfs_set_val("devices", bdf, file, val); +} + +static void sysfs_set_driver_val(const char *driver, const char *file, con= st char *val) +{ + sysfs_set_val("drivers", driver, file, val); +} + +static void sysfs_set_device_val_int(const char *bdf, const char *file, in= t val) +{ + char val_str[32] =3D {0}; + + snprintf(val_str, sizeof(val_str), "%d", val); + sysfs_set_device_val(bdf, file, val_str); +} + +int sysfs_get_sriov_totalvfs(const char *bdf) +{ + return sysfs_get_device_val(bdf, "sriov_totalvfs"); +} + +int sysfs_get_sriov_numvfs(const char *bdf) +{ + return sysfs_get_device_val(bdf, "sriov_numvfs"); +} + +void sysfs_set_sriov_numvfs(const char *bdf, int numvfs) +{ + sysfs_set_device_val_int(bdf, "sriov_numvfs", numvfs); +} + +bool sysfs_get_sriov_drivers_autoprobe(const char *bdf) +{ + return (bool)sysfs_get_device_val(bdf, "sriov_drivers_autoprobe"); +} + +void sysfs_set_sriov_drivers_autoprobe(const char *bdf, bool val) +{ + sysfs_set_device_val_int(bdf, "sriov_drivers_autoprobe", val); +} + +void sysfs_bind_driver(const char *bdf, const char *driver) +{ + sysfs_set_driver_val(driver, "bind", bdf); +} + +void sysfs_unbind_driver(const char *bdf, const char *driver) +{ + sysfs_set_driver_val(driver, "unbind", bdf); +} + +void sysfs_get_sriov_vf_bdf(const char *pf_bdf, int i, char *out_vf_bdf) +{ + char vf_path[PATH_MAX] =3D {0}; + char path[PATH_MAX] =3D {0}; + int ret; + + snprintf(path, PATH_MAX, "/sys/bus/pci/devices/%s/virtfn%d", pf_bdf, i); + + ret =3D readlink(path, vf_path, PATH_MAX); + VFIO_ASSERT_NE(ret, -1); + + ret =3D sscanf(basename(vf_path), "%s", out_vf_bdf); + VFIO_ASSERT_EQ(ret, 1); +} + +unsigned int sysfs_get_device_group(const char *bdf) +{ + char dev_iommu_group_path[PATH_MAX] =3D {0}; + char path[PATH_MAX] =3D {0}; + unsigned int group; + int ret; + + snprintf(path, PATH_MAX, "/sys/bus/pci/devices/%s/iommu_group", bdf); + + ret =3D readlink(path, dev_iommu_group_path, sizeof(dev_iommu_group_path)= ); + VFIO_ASSERT_NE(ret, -1, "Failed to get the IOMMU group for device: %s\n",= bdf); + + ret =3D sscanf(basename(dev_iommu_group_path), "%u", &group); + VFIO_ASSERT_EQ(ret, 1, "Failed to get the IOMMU group for device: %s\n", = bdf); + + return group; +} + +int sysfs_get_driver(const char *bdf, char *out_driver) +{ + char driver_path[PATH_MAX] =3D {0}; + char path[PATH_MAX] =3D {0}; + int ret; + + snprintf(path, PATH_MAX, "/sys/bus/pci/devices/%s/driver", bdf); + ret =3D readlink(path, driver_path, PATH_MAX); + if (ret =3D=3D -1) { + if (errno =3D=3D ENOENT) + return -1; + + VFIO_FAIL("Failed to read %s\n", path); + } + + ret =3D sscanf(basename(driver_path), "%s", out_driver); + VFIO_ASSERT_EQ(ret, 1); + + return 0; +} diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index 64a19481b734f..9b2a123cee5fc 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -22,8 +22,6 @@ #include "../../../kselftest.h" #include =20 -#define PCI_SYSFS_PATH "/sys/bus/pci/devices" - static void vfio_pci_irq_set(struct vfio_pci_device *device, u32 index, u32 vector, u32 count, int *fds) { @@ -181,24 +179,6 @@ void vfio_pci_device_reset(struct vfio_pci_device *dev= ice) ioctl_assert(device->fd, VFIO_DEVICE_RESET, NULL); } =20 -static unsigned int vfio_pci_get_group_from_dev(const char *bdf) -{ - char dev_iommu_group_path[PATH_MAX] =3D {0}; - char sysfs_path[PATH_MAX] =3D {0}; - unsigned int group; - int ret; - - snprintf_assert(sysfs_path, PATH_MAX, "%s/%s/iommu_group", PCI_SYSFS_PATH= , bdf); - - ret =3D readlink(sysfs_path, dev_iommu_group_path, sizeof(dev_iommu_group= _path)); - VFIO_ASSERT_NE(ret, -1, "Failed to get the IOMMU group for device: %s\n",= bdf); - - ret =3D sscanf(basename(dev_iommu_group_path), "%u", &group); - VFIO_ASSERT_EQ(ret, 1, "Failed to get the IOMMU group for device: %s\n", = bdf); - - return group; -} - static void vfio_pci_group_setup(struct vfio_pci_device *device, const cha= r *bdf) { struct vfio_group_status group_status =3D { @@ -207,7 +187,7 @@ static void vfio_pci_group_setup(struct vfio_pci_device= *device, const char *bdf char group_path[32]; int group; =20 - group =3D vfio_pci_get_group_from_dev(bdf); + group =3D sysfs_get_device_group(bdf); snprintf_assert(group_path, sizeof(group_path), "/dev/vfio/%d", group); =20 device->group_fd =3D open(group_path, O_RDWR); --=20 2.52.0.239.gd5f0c6e74e-goog From nobody Tue Dec 16 12:21:17 2025 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 A792B328607 for ; Wed, 10 Dec 2025 18:14:24 +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=1765390466; cv=none; b=FmINA1dn7XUh01XV1Z6cU7kHn4h6JOq8r6QzoSihCRxF3oColtOvbAMDWgi+uEkeEfKVayjEdl3GJgqkrZdprYBNLcS3lqzKs34hCTtLrEOXhpUt6VqAamw7uK5QQ/VamPu0FHRsz9VgojvxrACsl5EWavUIE7JPZI0Kntx6M74= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765390466; c=relaxed/simple; bh=l+cCwjio5EPRmT49D2SJmoZp0TKezhIl5PRxRDOzXLU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gZncHGhY3Aop7pwoEoEjj72goic5UAMJKkjB51u1gf6k7CYVzgNFKo6LEse5mbBtMmfpTncj6H3++qVEo/QYjlHLMmn0q9xD/FeHgnDFt6DUw3/zQ59fShGuh4jxNaL5dy1H9V1KkSPPy2cUWuxPGq9qmwtgIOCHpcUPO/eq9bY= 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=IEh41gQ3; 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="IEh41gQ3" Received: by mail-ot1-f74.google.com with SMTP id 46e09a7af769-7c702347c6eso222587a34.1 for ; Wed, 10 Dec 2025 10:14:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1765390464; x=1765995264; 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=cc3yaQBIDdGsG6IwnoL+tF4JZwHQKMPcuou0OF8CLFo=; b=IEh41gQ3wPZ35EPFuJ8/LUzLftTj1XpZ3uH/fb/qAL6b9IkEb+PJqHyLVr82U4SUoE 2iBVat6pL13bT3keVlXnV0SVw2YQl1nrI19DvplEjjZxvw/pvrs4PK8xC561v+SFynzx EZ6ZUwJ3e/Ljg/R2EQrxjdFMp/uZWhgK6+0kjrzhgsgenV2yykSLr2HkhG9sCEQJT3oE Xd/2dSPh0VhOgzF3AjNd/X65VY8AfIz4BcKswSvrLqRPMecoUdvMDSvyouJgS9GiY2fa 6xogG7QaW04oMxOAoPGwQpLpsjmvPyBqRv8Aq8X/or+iCUcF3oSfK1bHWYTdZPMKrY+m Kr7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765390464; x=1765995264; 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=cc3yaQBIDdGsG6IwnoL+tF4JZwHQKMPcuou0OF8CLFo=; b=IbGFwlE7fhJ+aYiPguoQZrozz/V9Up4K54e+OOIsVYPpmZdEP5qzjMEeW48nnMoaPl L8sUKLDzx51n0MiZhSC+EQ3i93s3EzVCSM670N7zGXZkv3B+u7TueIAk3an9Qk3h74i8 c3FcSiP4ABOtA+br92drLLJscdbs1h/C2eSHSvixnHzV35ZHG/UIlBmGABP0nF/lLHHE nAK5GQJU7uPfbNBAtRHBpH0C5JPtQclK21cwz1dkoTZySqjszRmIn6GlDiuvKD0yvgVx QoRVpxwP40Khv0OqKfmIGlWBLNWvRXgx68TpkyUide/K8BeG5+6mTH6DM5HNFxVBba+E vk9w== X-Forwarded-Encrypted: i=1; AJvYcCV5XgHGsN8aet6Pt/NsTccHzZyHWR6cgv93c6v09Uh2mefyTDg4agpHV2YVn8gT8f+nsiX0j67pWo9TKvQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yx9veoHwpisCrjJRjFfg9NOLRDjXQcQYA8tqpIf/9GnNbKIaFos qLoRd/PoKkb91cJzPfjpxhpWg/1KbgU5G9swqJbhlarH+7vmy38agzRdaCd4cPY7m24DHritZ2/ yGXgW4pwa0g== X-Google-Smtp-Source: AGHT+IH7asKz8/hYJaH0fCIkSFlYh1Npe7OXIQ50oexIrSsl3V8oTPQzBwYi5e+FIdrmm5DKQ0bUjKgBpNRR X-Received: from ilbcb14.prod.google.com ([2002:a05:6e02:318e:b0:433:78dd:2882]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:1849:b0:659:9a49:8e4b with SMTP id 006d021491bc7-65b2ac11e38mr2143163eaf.27.1765390463721; Wed, 10 Dec 2025 10:14:23 -0800 (PST) Date: Wed, 10 Dec 2025 18:14:14 +0000 In-Reply-To: <20251210181417.3677674-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: <20251210181417.3677674-1-rananta@google.com> X-Mailer: git-send-email 2.52.0.239.gd5f0c6e74e-goog Message-ID: <20251210181417.3677674-4-rananta@google.com> Subject: [PATCH v2 3/6] vfio: selftests: Extend container/iommufd setup for passing vf_token From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson , Alex Williamson Cc: 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 --- tools/testing/selftests/vfio/lib/libvfio.mk | 4 +- .../selftests/vfio/lib/vfio_pci_device.c | 45 +++++++++++++++---- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/vfio/lib/libvfio.mk b/tools/testing/se= lftests/vfio/lib/libvfio.mk index b7857319c3f1f..459b14c6885a8 100644 --- a/tools/testing/selftests/vfio/lib/libvfio.mk +++ b/tools/testing/selftests/vfio/lib/libvfio.mk @@ -15,6 +15,8 @@ LIBVFIO_C +=3D drivers/ioat/ioat.c LIBVFIO_C +=3D drivers/dsa/dsa.c endif =20 +LDLIBS +=3D -luuid + LIBVFIO_OUTPUT :=3D $(OUTPUT)/libvfio =20 LIBVFIO_O :=3D $(patsubst %.c, $(LIBVFIO_OUTPUT)/%.o, $(LIBVFIO_C)) @@ -25,6 +27,6 @@ $(shell mkdir -p $(LIBVFIO_O_DIRS)) CFLAGS +=3D -I$(LIBVFIO_SRCDIR)/include =20 $(LIBVFIO_O): $(LIBVFIO_OUTPUT)/%.o : $(LIBVFIO_SRCDIR)/%.c - $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@ + $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< $(LDLIBS) -o $@ =20 EXTRA_CLEAN +=3D $(LIBVFIO_OUTPUT) diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index 9b2a123cee5fc..ac9a5244ddc46 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -12,6 +12,7 @@ #include =20 #include +#include #include #include #include @@ -199,7 +200,27 @@ static void vfio_pci_group_setup(struct vfio_pci_devic= e *device, const char *bdf ioctl_assert(device->group_fd, VFIO_GROUP_SET_CONTAINER, &device->iommu->= container_fd); } =20 -static void vfio_pci_container_setup(struct vfio_pci_device *device, const= char *bdf) +static void vfio_pci_group_get_device_fd(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) +{ + char arg[64] =3D {0}; + + /* + * 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(arg, ARRAY_SIZE(arg), "%s vf_token=3D%s", bdf, vf_token); + else + snprintf(arg, ARRAY_SIZE(arg), "%s", bdf); + + device->fd =3D ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, arg); + VFIO_ASSERT_GE(device->fd, 0); +} + +static void vfio_pci_container_setup(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) { struct iommu *iommu =3D device->iommu; unsigned long iommu_type =3D iommu->mode->iommu_type; @@ -217,8 +238,7 @@ static void vfio_pci_container_setup(struct vfio_pci_de= vice *device, const char */ (void)ioctl(iommu->container_fd, VFIO_SET_IOMMU, (void *)iommu_type); =20 - device->fd =3D ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, bdf); - VFIO_ASSERT_GE(device->fd, 0); + vfio_pci_group_get_device_fd(device, bdf, vf_token); } =20 static void vfio_pci_device_setup(struct vfio_pci_device *device) @@ -279,12 +299,20 @@ const char *vfio_pci_get_cdev_path(const char *bdf) return cdev_path; } =20 -static void vfio_device_bind_iommufd(int device_fd, int iommufd) +static void vfio_device_bind_iommufd(int device_fd, int iommufd, + const char *vf_token) { struct vfio_device_bind_iommufd args =3D { .argsz =3D sizeof(args), .iommufd =3D iommufd, }; + uuid_t token_uuid =3D {0}; + + if (vf_token) { + VFIO_ASSERT_EQ(uuid_parse(vf_token, token_uuid), 0); + args.flags |=3D VFIO_DEVICE_BIND_FLAG_TOKEN; + args.token_uuid_ptr =3D (u64)token_uuid; + } =20 ioctl_assert(device_fd, VFIO_DEVICE_BIND_IOMMUFD, &args); } @@ -299,7 +327,8 @@ static void vfio_device_attach_iommufd_pt(int device_fd= , u32 pt_id) ioctl_assert(device_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &args); } =20 -static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const c= har *bdf) +static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) { const char *cdev_path =3D vfio_pci_get_cdev_path(bdf); =20 @@ -307,7 +336,7 @@ static void vfio_pci_iommufd_setup(struct vfio_pci_devi= ce *device, const char *b VFIO_ASSERT_GE(device->fd, 0); free((void *)cdev_path); =20 - vfio_device_bind_iommufd(device->fd, device->iommu->iommufd); + vfio_device_bind_iommufd(device->fd, device->iommu->iommufd, vf_token); vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id); } =20 @@ -323,9 +352,9 @@ struct vfio_pci_device *vfio_pci_device_init(const char= *bdf, struct iommu *iomm device->bdf =3D bdf; =20 if (iommu->mode->container_path) - vfio_pci_container_setup(device, bdf); + vfio_pci_container_setup(device, bdf, NULL); else - vfio_pci_iommufd_setup(device, bdf); + vfio_pci_iommufd_setup(device, bdf, NULL); =20 vfio_pci_device_setup(device); vfio_pci_driver_probe(device); --=20 2.52.0.239.gd5f0c6e74e-goog From nobody Tue Dec 16 12:21:17 2025 Received: from mail-oi1-f202.google.com (mail-oi1-f202.google.com [209.85.167.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA48C32938B for ; Wed, 10 Dec 2025 18:14:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765390467; cv=none; b=jiAS88qSPlyLAFjfu/VVkMC3F/nWTiZvIJeISUFcEaj3maz2aqGq3zB2BIjXKsXwRljnX9bikUagtl+QVMSCkmYmO3umeU9HnJqV+W5qSfNsdFbSGbl2j0/0YgFYqgjkgwfildrHKoSSIOn6Inr0v6q4+ZKBjI0gXCutBYnePZ0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765390467; c=relaxed/simple; bh=JW2tTbcIH9EEyXRToUoNxG+ERli4JmNx0hWIw6Yzw+A=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pLem4X6GM7gYajrFCNdjPG+SInzmJAkmlxGaR1eYzaI9/gfqVGn7+gfD7zuuLd8UJFo4csxG971Ogte+Y91vGOfFhcuzBMMX+d3LiCT0MTmDXO5/frNJoqogNotsDamVtEmIQyOAhOR5VH7dUyU82Jg9WtRnjlhk6nlL3mISBD0= 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=WkuUMxXK; arc=none smtp.client-ip=209.85.167.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--rananta.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="WkuUMxXK" Received: by mail-oi1-f202.google.com with SMTP id 5614622812f47-450239c8c44so82150b6e.2 for ; Wed, 10 Dec 2025 10:14:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1765390465; x=1765995265; 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=168r6GWEsV32677D69udXhu4G6miZjg9d8osoe8KIxQ=; b=WkuUMxXKdc5wQOjd4Fafv/PdpX6pFtowikGPfYMpBmFTUnYqbGyZVGMfj2fxOJm9A9 3NYGGDOHO98f0GwqKLicHllqkcT4vw/kK2UVIxhi/G/Jh9FBjv/sxlYF7dE7D3oWfAzO E4g8ve5gumTKTIdiKHAfe8Cowo/JtaV+Ka2JzfDFmq/HkdOE4BxyGAN0tCQ8YpNz3BHk toqeLb1hgSPA6j1cHeMP2R0l2bUNt+zIlrYwv2t/MP6iasBO2ZHSrzwQcvz2Y4P2rGCI KP3bX30QReWqgxdTc9JxoQBF+8uQLQJ2DbMyXYaOlmESAxdOaUmLNOO0DtLN3ieJgHWb 8yWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765390465; x=1765995265; 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=168r6GWEsV32677D69udXhu4G6miZjg9d8osoe8KIxQ=; b=bM9mdNqOsR1h41vyaNeYKGlrOb7WBsqvwVYdGEfAeiRMfxgXlNbM+gULzSpW2q4nQ2 /OJlJ552Bt/DGju+5DJdYcBOuGIJ+ro6ZDFpsZ3ZTotb012jP7T9z8weenbwSFN8s0eU 8E21RPLzyZh+rTOvECUK0V0jxgPnFg7ecrjysxpOWPGd1XOIkdSrYhbAHUc/lIQtQhxh sH6fylRPGbDhpSG56l8FWkc2DiFWqFxOiytBD8Eoh0IisONzlu/euak9jIu4t6vao7S/ TFdcM3M7S6ttOcUhTrPEYCCMc45xwu1+EZCb0UooW4Lpoo12v3LtRh1bdYBTxrIzBIBz zY7w== X-Forwarded-Encrypted: i=1; AJvYcCXnOoGoyzwcCO1JH2JujXX0lim51o/jFwstNLVDj9tGL+1bQAw/dZTkp+25D3bb/XXrKw0wiMikUR+JGWU=@vger.kernel.org X-Gm-Message-State: AOJu0Yz31CZcxCXcUGDtTOhWeyd/LNCiRK4KNJS9zZmrAGJ79SuVfjyw 6BRd707NvHaCgoik0eX1GGVoOO10SnQATAjoeYztzuDBoGpuSVyOukNUgoFz9/qWwobNSmsEl85 uhNRlaIGFWw== X-Google-Smtp-Source: AGHT+IF1iaf5yxFXv//JDSg0yT6uFqXirv+dwMapWnO8hIFHxEP4/ISw2Vf6RwskJbffES3r6mEXUh7ZUBhI X-Received: from ilbbd1.prod.google.com ([2002:a05:6e02:3001:b0:438:1576:ce32]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:81d8:b0:659:9a49:908d with SMTP id 006d021491bc7-65b2ac06718mr1994513eaf.12.1765390464773; Wed, 10 Dec 2025 10:14:24 -0800 (PST) Date: Wed, 10 Dec 2025 18:14:15 +0000 In-Reply-To: <20251210181417.3677674-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: <20251210181417.3677674-1-rananta@google.com> X-Mailer: git-send-email 2.52.0.239.gd5f0c6e74e-goog Message-ID: <20251210181417.3677674-5-rananta@google.com> Subject: [PATCH v2 4/6] vfio: selftests: Export more vfio_pci functions From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson , Alex Williamson Cc: 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 --- .../lib/include/libvfio/vfio_pci_device.h | 7 +++ .../selftests/vfio/lib/vfio_pci_device.c | 44 ++++++++++++++----- 2 files changed, 39 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..6186ca463ca6e 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_iommufd_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 ac9a5244ddc46..208da2704d9e2 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -180,7 +180,7 @@ void vfio_pci_device_reset(struct vfio_pci_device *devi= ce) ioctl_assert(device->fd, VFIO_DEVICE_RESET, NULL); } =20 -static void vfio_pci_group_setup(struct vfio_pci_device *device, const cha= r *bdf) +void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf) { struct vfio_group_status group_status =3D { .argsz =3D sizeof(group_status), @@ -200,8 +200,8 @@ static void vfio_pci_group_setup(struct vfio_pci_device= *device, const char *bdf ioctl_assert(device->group_fd, VFIO_GROUP_SET_CONTAINER, &device->iommu->= container_fd); } =20 -static void vfio_pci_group_get_device_fd(struct vfio_pci_device *device, - const char *bdf, const char *vf_token) +void __vfio_pci_group_get_device_fd(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) { char arg[64] =3D {0}; =20 @@ -216,18 +216,21 @@ static void vfio_pci_group_get_device_fd(struct vfio_= pci_device *device, snprintf(arg, ARRAY_SIZE(arg), "%s", bdf); =20 device->fd =3D ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, arg); +} + +static void vfio_pci_group_get_device_fd(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) +{ + __vfio_pci_group_get_device_fd(device, bdf, vf_token); VFIO_ASSERT_GE(device->fd, 0); } =20 -static void vfio_pci_container_setup(struct vfio_pci_device *device, - const char *bdf, const char *vf_token) +void vfio_container_set_iommu(struct vfio_pci_device *device) { struct iommu *iommu =3D device->iommu; unsigned long iommu_type =3D iommu->mode->iommu_type; int ret; =20 - vfio_pci_group_setup(device, bdf); - ret =3D ioctl(iommu->container_fd, VFIO_CHECK_EXTENSION, iommu_type); VFIO_ASSERT_GT(ret, 0, "VFIO IOMMU type %lu not supported\n", iommu_type); =20 @@ -237,7 +240,13 @@ static void vfio_pci_container_setup(struct vfio_pci_d= evice *device, * because the IOMMU type is already set. */ (void)ioctl(iommu->container_fd, VFIO_SET_IOMMU, (void *)iommu_type); +} =20 +static void vfio_pci_container_setup(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) +{ + vfio_pci_group_setup(device, bdf); + vfio_container_set_iommu(device); vfio_pci_group_get_device_fd(device, bdf, vf_token); } =20 @@ -299,8 +308,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf) return cdev_path; } =20 -static void vfio_device_bind_iommufd(int device_fd, int iommufd, - const char *vf_token) +int __vfio_device_bind_iommufd(int device_fd, int iommufd, const char *vf_= token) { struct vfio_device_bind_iommufd args =3D { .argsz =3D sizeof(args), @@ -314,7 +322,15 @@ 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); + return ioctl(device_fd, VFIO_DEVICE_BIND_IOMMUFD, &args); +} + +static void vfio_device_bind_iommufd(int device_fd, int iommufd, + const char *vf_token) +{ + int ret =3D __vfio_device_bind_iommufd(device_fd, iommufd, vf_token); + + VFIO_ASSERT_EQ(ret, 0, "Failed VFIO_DEVICE_BIND_IOMMUFD ioctl\n"); } =20 static void vfio_device_attach_iommufd_pt(int device_fd, u32 pt_id) @@ -327,15 +343,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_iommufd_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_iommufd_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.52.0.239.gd5f0c6e74e-goog From nobody Tue Dec 16 12:21:17 2025 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 9A407329C41 for ; Wed, 10 Dec 2025 18:14:26 +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=1765390468; cv=none; b=H3AubeOYIpogejuxtzwrF1vSJ4ihOvXp6FQMPZOdoY2ue2aCzVDpRcDb8zvdhzY+5ARjnYrGneJN/zq7YeVMIFOEDh09MnZta6r9eDbqMbWhbN9rp6wzTaecHnZ2Hy1xgO0pjT7fI5y48/q16adXZ0XoetZGZe3oK8odo8Iqw0I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765390468; c=relaxed/simple; bh=pwlFgeh1zV9BNL0tvm8yEEyE54pw7ioKi1EzIbT/3Hk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MI3MKYUoNa0pt8cz8+EJ9hPG+d2xME6g+LPTPJivXVtEU1NuR1KWrpv2mDqlLHwOJ8xmkVqjV3EFhVPmUJK/DsaFK5Wnc9Oqf4I7xeIMBXGiuMTjJAgesMZZG3f6zM5FMfb2ObYZH53gzfvmwIDWGcW60wLh4E/4kpvnNKvjh3g= 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=xXi10Tcp; 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="xXi10Tcp" Received: by mail-ot1-f74.google.com with SMTP id 46e09a7af769-7c72ccd60f5so359670a34.0 for ; Wed, 10 Dec 2025 10:14:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1765390466; x=1765995266; 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=4gUwEMI4CPC8nG6DpXhg5Tl6v8j6U1PzBCdzq7Inos8=; b=xXi10TcpFHB83Uay3V25poivP7UPmNooomxicHJjAhe2BuMMNRc1vjRKVtXGbVoScU HQTJvEj9hd4+T9toWvOi3EBM7gpP6toIIe5N0YDEL3mu0AKcP+oQF1Ot3nH0JA06zbcR 7ZPzlGJU0otoqDgYSfLu8tEqrSPK6Nrkr4UjZjaqs8ReBZjYUaVHutQqYxz7TJgaSnJf wP8zAZzcIlwaoVpmyUqlcxRS3ZFmjaBKAcqPs9nA8Vf7VA9e9LsYcHmbIxzEomYYEDpA OBgmj01JC25kfBskcmXdnOef8OoOPFajBy4Ra+W8nq5+Z9CMnkxxis926DH6NbwFYjzt 6EZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765390466; x=1765995266; 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=4gUwEMI4CPC8nG6DpXhg5Tl6v8j6U1PzBCdzq7Inos8=; b=dp84e3CNaK359t7e4BWHzZqomBsntj3+OIu5+nzlqdi/p89tLxQ7fMOzPF0vK/LIsA OzYW81qbRYNRuy2CPI9g3FiWgkdDjVPhfZIH7G5ciRxhiVwRGRW673oz3Ks/dLK/jPSE FlSDOSAE7jN0XtbbSYFG0V338RYZzlL1zko91Neu7EhM6FKJTvVgNkCZY2PNw2mw2gSu QKd12mMz+DbM4xptY6X+TQ1PDG8M5tawxUStgK9vf1E97lZk2TTTe8nh07Y9KsnSkbW9 86LjtdNFZ2GzWRL60zAwRWsbKXH29V8KhHiJYWMG3m5ZqH5XK0IlBYuc1QU6WlHbZMlY 9VPg== X-Forwarded-Encrypted: i=1; AJvYcCUrt+kXUpC0onwiqKXdNchzHo94hmcsymJvJgdK4TfiCJnxU2fF84j0ljrRNbKUQHr8bFfLkTo3dv4lAv4=@vger.kernel.org X-Gm-Message-State: AOJu0YwAp/jSJnqpVwrzNkDOwugq/FX9U/tmUjKIlU9b59zXWNjf2xt0 H3cYtBuVhQo87jW4xuSJYaen5ZXOfzt1ApKSR/mGIuPcDDYNFXhi9Z6tIkombrm2I1F2CUYZz7J DlpK53JtD0Q== X-Google-Smtp-Source: AGHT+IHG44j9+i0T0oJH5qztj8QDLXfbSZEMuigfmk9lH3MjDr9iEI8CBWmAD9bZ6oOzQ/GYCPP3dTlLaWN7 X-Received: from iomw16.prod.google.com ([2002:a5e:d610:0:b0:949:68d:8b40]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:f008:b0:65b:257b:a885 with SMTP id 006d021491bc7-65b2ac3ec9fmr2171394eaf.25.1765390465579; Wed, 10 Dec 2025 10:14:25 -0800 (PST) Date: Wed, 10 Dec 2025 18:14:16 +0000 In-Reply-To: <20251210181417.3677674-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: <20251210181417.3677674-1-rananta@google.com> X-Mailer: git-send-email 2.52.0.239.gd5f0c6e74e-goog Message-ID: <20251210181417.3677674-6-rananta@google.com> Subject: [PATCH v2 5/6] vfio: selftests: Add helper to set/override a vf_token From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson , Alex Williamson Cc: 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 --- .../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 6186ca463ca6e..b370aa6a74d0b 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_iommufd_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 208da2704d9e2..7725ecc62b024 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -109,6 +109,40 @@ static void vfio_pci_irq_get(struct vfio_pci_device *d= evice, u32 index, ioctl_assert(device->fd, VFIO_DEVICE_GET_IRQ_INFO, irq_info); } =20 +static int vfio_device_feature_ioctl(int fd, u32 flags, void *data, + size_t data_size) +{ + u8 buffer[sizeof(struct vfio_device_feature) + data_size] =3D {}; + struct vfio_device_feature *feature =3D (void *)buffer; + + memcpy(feature->data, data, data_size); + + feature->argsz =3D sizeof(buffer); + feature->flags =3D flags; + + return ioctl(fd, VFIO_DEVICE_FEATURE, feature); +} + +static void vfio_device_feature_set(int fd, u16 feature, void *data, size_= t data_size) +{ + u32 flags =3D VFIO_DEVICE_FEATURE_SET | feature; + int ret; + + ret =3D vfio_device_feature_ioctl(fd, flags, data, data_size); + VFIO_ASSERT_EQ(ret, 0, "Failed to set feature %u\n", feature); +} + +void vfio_device_set_vf_token(int fd, const char *vf_token) +{ + uuid_t token_uuid =3D {0}; + + VFIO_ASSERT_NOT_NULL(vf_token, "vf_token is NULL"); + VFIO_ASSERT_EQ(uuid_parse(vf_token, token_uuid), 0); + + vfio_device_feature_set(fd, VFIO_DEVICE_FEATURE_PCI_VF_TOKEN, + token_uuid, sizeof(uuid_t)); +} + static void vfio_pci_region_get(struct vfio_pci_device *device, int index, struct vfio_region_info *info) { --=20 2.52.0.239.gd5f0c6e74e-goog From nobody Tue Dec 16 12:21:17 2025 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 B391C32937F for ; Wed, 10 Dec 2025 18:14:27 +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=1765390470; cv=none; b=q70TR3RfZ9ovr3k4pp5CqOFu2CkcUS8AMMfDIpKhNoZnaCK5BsR4SnePyO8g6OkeKMy6ejPGprDuITOHryInFWm1qQlGDTqwBxM1axFS0q1qKUxTkv3BgGqvW7UR2/wPf2fNUsaZ5wYQ4DlZqkJid+kmLWkO4ap5kiOJol9+ihI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765390470; c=relaxed/simple; bh=an2Zn6RpDfaa/xhfmBAktY0KXvX4xDoqrCBPSIi9TNE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=saQnLJ9hQBhN7Jh+r7dC1hBONO8d2zyCKc2Efw4KmdKptHoN8RA2uR82EyhUSm8zpk0pLNX/QFFUruTI6XJLRM62N0mec3G5jJFRmx/DroUU5wtiATj7qm5Ly8PMCazFpe4A1uC3MDOKaON4RmA53da/VoASwddG/trcHeH+OoM= 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=jnqvUF1p; 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="jnqvUF1p" Received: by mail-ot1-f73.google.com with SMTP id 46e09a7af769-7cad1393ec8so545639a34.0 for ; Wed, 10 Dec 2025 10:14:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1765390466; x=1765995266; 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=Xvj1DeRBj73MVA6K+8aEadH2MXzDd0NTh71ZLBFvl/I=; b=jnqvUF1pCsusvV8Qdt0wcLdADk2o9X/Kvt6z3XNN4xpTTBxJxZ7yi8BZE5BBm75F3y JRIEzb8KYTdJUPt/RfEuQKXsjB73A/SBJaDIdsk83Dz+Jna5EPBJbnffwmJiggRmxaTC rK1ILZ7Qu7LRVjUp3nVuAMJ6aL/SUhZxpHiopsStRf7TE25uh00YNc4ObD6qXqXRVrzN giVbciPx73o/wgoRgeyB7m71MKEmT+JyPHaZ79QzoHDvV1I8GC0Vtq3VSzMA5Y/VTpZ3 wFBpwzo9nNBOB5k9fe7TewKS4a7DakTwQSBpOpRZqDQA6+E+AY+X43of9MDhCYHO/cc0 mcvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765390466; x=1765995266; 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=Xvj1DeRBj73MVA6K+8aEadH2MXzDd0NTh71ZLBFvl/I=; b=RarvUzrw5VC8KozwCVPtFM9SzkCw1rlkDav7LXFkcIb03Bl/xuwKURuV7eY7gXH9A9 3xysQJ9SRCWm9x2FrFuHv7G5e+CKfCG6XKTrIF4kgwDxrkFSazzHLsVzDxodKeIHG/wW v8uKFPKjd+l4gaaiW/ecsL1C7PEV3Mx35shJBbZDH9A+FV8TU/NFix3vAahNDa0aAs0f BBqQn9Ur7q66RcPckAVmwzxRY29w0RyuThQRyBf6M51Nc4gMUAHc8T0J3SVuXKfqBWST bMXzMuOLYJwiI2QpyYqx4gN5VBNWCFNGyGoQM7/++LnDgdH5t4AKGRAuVchFD/Obg+dt 8pRg== X-Forwarded-Encrypted: i=1; AJvYcCVbGm1Qh/5tLNJh1OErVoTdHlR5856ZqjYMNyQyMLhfmLFKD1gurMHmGu2LHZED3OipvCmnf2J9bYJT0d0=@vger.kernel.org X-Gm-Message-State: AOJu0Yx5ieCgGu1cE7lJ0d5X+hpjq/cJYk5o/JCn9Zabet3TE6+7ggyD e5JX1Mmme9/HK+mS8LO7H90JjmifhJfNCsMjeK89K9ulReoY5t2Q+TX2kdGc7kHRcKKvnDTYO2B AmGTf7+BbkA== X-Google-Smtp-Source: AGHT+IEidK7dxGFWEkxN+dDoCoKZUv8ekllLT6ISbeM5jutX6QSt//SlyN74EilCTF+wBTlNjbu2z0HHTlKR X-Received: from ilbbs17.prod.google.com ([2002:a05:6e02:2411:b0:434:972f:bf8f]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:f48:b0:659:9a49:8f8a with SMTP id 006d021491bc7-65b2ad07bccmr1902553eaf.79.1765390466476; Wed, 10 Dec 2025 10:14:26 -0800 (PST) Date: Wed, 10 Dec 2025 18:14:17 +0000 In-Reply-To: <20251210181417.3677674-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: <20251210181417.3677674-1-rananta@google.com> X-Mailer: git-send-email 2.52.0.239.gd5f0c6e74e-goog Message-ID: <20251210181417.3677674-7-rananta@google.com> Subject: [PATCH v2 6/6] vfio: selftests: Add tests to validate SR-IOV UAPI From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson , Alex Williamson Cc: 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 selfttest, vfio_pci_sriov_uapi_test.c, to validate the SR-IOV UAPI, including the following cases, iterating over all the IOMMU modes currently supported: - Setting correct/incorrect/NULL tokens during device init. - Close the PF device immediately after setting the token. - Change/override the PF's token after device init. Signed-off-by: Raghavendra Rao Ananta --- tools/testing/selftests/vfio/Makefile | 1 + .../selftests/vfio/vfio_pci_sriov_uapi_test.c | 215 ++++++++++++++++++ 2 files changed, 216 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 3c796ca99a509..f00a63902fbfb 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -4,6 +4,7 @@ TEST_GEN_PROGS +=3D vfio_iommufd_setup_test TEST_GEN_PROGS +=3D vfio_pci_device_test TEST_GEN_PROGS +=3D vfio_pci_device_init_perf_test TEST_GEN_PROGS +=3D vfio_pci_driver_test +TEST_GEN_PROGS +=3D vfio_pci_sriov_uapi_test =20 TEST_FILES +=3D scripts/cleanup.sh TEST_FILES +=3D scripts/lib.sh diff --git a/tools/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c b/tool= s/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c new file mode 100644 index 0000000000000..4c2951d6e049c --- /dev/null +++ b/tools/testing/selftests/vfio/vfio_pci_sriov_uapi_test.c @@ -0,0 +1,215 @@ +// 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_dev_bdf; + +static int test_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); + + /* The device fd will be -1 in case of mismatched tokens */ + return (device->fd < 0); +} + +static int test_vfio_pci_iommufd_setup(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) +{ + vfio_pci_iommufd_cdev_open(device, bdf); + return __vfio_device_bind_iommufd(device->fd, + device->iommu->iommufd, vf_token); +} + +static struct vfio_pci_device *test_vfio_pci_device_init(const char *bdf, + struct iommu *iommu, + const char *vf_token, + int *out_ret) +{ + struct vfio_pci_device *device; + + device =3D calloc(1, sizeof(*device)); + VFIO_ASSERT_NOT_NULL(device); + + device->iommu =3D iommu; + device->bdf =3D bdf; + + if (iommu->mode->container_path) + *out_ret =3D test_vfio_pci_container_setup(device, bdf, vf_token); + else + *out_ret =3D test_vfio_pci_iommufd_setup(device, bdf, vf_token); + + return device; +} + +static void test_vfio_pci_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); + + free(device); +} + +FIXTURE(vfio_pci_sriov_uapi_test) { + char vf_dev_bdf[16]; + char vf_driver[32]; + bool sriov_drivers_autoprobe; +}; + +FIXTURE_SETUP(vfio_pci_sriov_uapi_test) +{ + int nr_vfs; + int ret; + + nr_vfs =3D sysfs_get_sriov_totalvfs(pf_dev_bdf); + if (nr_vfs < 0) + SKIP(return, "SR-IOV may not be supported by the device\n"); + + nr_vfs =3D sysfs_get_sriov_numvfs(pf_dev_bdf); + if (nr_vfs !=3D 0) + SKIP(return, "SR-IOV already configured for the PF\n"); + + self->sriov_drivers_autoprobe =3D + sysfs_get_sriov_drivers_autoprobe(pf_dev_bdf); + if (self->sriov_drivers_autoprobe) + sysfs_set_sriov_drivers_autoprobe(pf_dev_bdf, 0); + + /* Export only one VF for testing */ + sysfs_set_sriov_numvfs(pf_dev_bdf, 1); + + sysfs_get_sriov_vf_bdf(pf_dev_bdf, 0, self->vf_dev_bdf); + if (sysfs_get_driver(self->vf_dev_bdf, self->vf_driver) =3D=3D 0) + sysfs_unbind_driver(self->vf_dev_bdf, self->vf_driver); + sysfs_bind_driver(self->vf_dev_bdf, "vfio-pci"); +} + +FIXTURE_TEARDOWN(vfio_pci_sriov_uapi_test) +{ + sysfs_unbind_driver(self->vf_dev_bdf, "vfio-pci"); + sysfs_bind_driver(self->vf_dev_bdf, self->vf_driver); + sysfs_set_sriov_numvfs(pf_dev_bdf, 0); + sysfs_set_sriov_drivers_autoprobe(pf_dev_bdf, + self->sriov_drivers_autoprobe); +} + +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_device; + struct vfio_pci_device *vf_device; + struct iommu *iommu; + int ret; + + iommu =3D iommu_init(variant->iommu_mode); + pf_device =3D test_vfio_pci_device_init(pf_dev_bdf, iommu, UUID_1, &ret); + vf_device =3D test_vfio_pci_device_init(self->vf_dev_bdf, iommu, + variant->vf_token, &ret); + + ASSERT_VF_CREATION(ret); + + test_vfio_pci_device_cleanup(vf_device); + test_vfio_pci_device_cleanup(pf_device); + 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_device; + struct vfio_pci_device *vf_device; + struct iommu *iommu; + int ret; + + iommu =3D iommu_init(variant->iommu_mode); + pf_device =3D test_vfio_pci_device_init(pf_dev_bdf, iommu, UUID_1, &ret); + test_vfio_pci_device_cleanup(pf_device); + + vf_device =3D test_vfio_pci_device_init(self->vf_dev_bdf, iommu, + variant->vf_token, &ret); + + ASSERT_VF_CREATION(ret); + + test_vfio_pci_device_cleanup(vf_device); + 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_device; + struct vfio_pci_device *vf_device; + struct iommu *iommu; + int ret; + + iommu =3D iommu_init(variant->iommu_mode); + pf_device =3D test_vfio_pci_device_init(pf_dev_bdf, iommu, UUID_2, &ret); + vfio_device_set_vf_token(pf_device->fd, UUID_1); + + vf_device =3D test_vfio_pci_device_init(self->vf_dev_bdf, iommu, + variant->vf_token, &ret); + + ASSERT_VF_CREATION(ret); + + test_vfio_pci_device_cleanup(vf_device); + test_vfio_pci_device_cleanup(pf_device); + iommu_cleanup(iommu); +} + +int main(int argc, char *argv[]) +{ + pf_dev_bdf =3D vfio_selftests_get_bdf(&argc, argv); + return test_harness_run(argc, argv); +} --=20 2.52.0.239.gd5f0c6e74e-goog