From nobody Mon Feb 9 01:45:22 2026 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 1B25C352FA5 for ; Wed, 12 Nov 2025 19:23:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762975384; cv=none; b=o8x/SfMPyGY43GXNhMNNpDbW7odSJMmnci01XAM0RsI7hfIwdPqkhSJEWW2GWNjV/Tci24XZFiMCEaozUgx8q7OfL8Z+BtREeZUr0qzm+4mm5hDZnJK5VX40vxdVZBSIN9PWycy1rP+RInCciEdbPsRhyrOV9h2TuRC4Lm1zjzo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762975384; c=relaxed/simple; bh=CkyC4vEq22xK1S2P6qHF080PFKlKoHtyS9Y4y4gmwns=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jjsIOZqPeWksyNDx2O05RQIr1po3jncGtlYCeZs2ipxxb6/yecErEMfY+qIWih+KYHGVPSliOLa4CkzQVBmisIurrHAQdEwimgtcLapwFayu3PSUg2k3mBh7mOOjk0nfHL3sMhnpCG5mIEbzVUUEtx5KEzkDH6gF2Aje53k/paY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=e9GX4Siu; arc=none smtp.client-ip=209.85.210.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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="e9GX4Siu" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-7a440d38452so1920962b3a.0 for ; Wed, 12 Nov 2025 11:23:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1762975382; x=1763580182; 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=/x7TxElv1Y0m6LMl9OrnCN+PJlJwFyshUcn1NViYdY8=; b=e9GX4SiuGpZ4KxxJdYuGGOkaUUkaYN4xL7f8kfZCChu4R2M9EMBb59YHdacXF2+glJ dPSkd8dzYKrAv7psurVDQ3EGTaRZNyGtGOiqmxsJeQhwa4jbD6l1nnT5aYwkA6mePugj SI4vTakuP4t5aJnLFQMOBNaNqjJzHyYe8m1OlBoDmiIFtPh6o+UqIl2tCHPMWe6nsxVX mD5PzUYMe3aBsz/xBnTfpZvZn0ZbIB/xuXs16Re6LHg9tpbOeu1vkRGNOKauvvBipjmn tqDrqGGXe+XYIHWsubrzfTHXHG/T86NBWJhZoDoipCQ+iFSMvQYcsP+gNECVIw8c+uL/ LhoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762975382; x=1763580182; 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=/x7TxElv1Y0m6LMl9OrnCN+PJlJwFyshUcn1NViYdY8=; b=LIc2cuxnsR8T4gnfbeh5uqhB6Qb+pEZIbX3dC63Yd8rwdde3LrodannPscipn5n2XA HsKoBNGnGgzUhneQpAkiCarmwE8akW893iOUX+6KfWjpkZ/Kl6ISQHKPLrdMoHDl/9um AFgatVN75/sSuyVCBK0VQftphjYawxG8FldoBmeeryZ6k4hzzrlHLcGG77CiE81rlcrZ RDnRsY4XZ5BUYQXWf0Q2LrrUo1Wye+8CYvLoBloOWvAfUWNLEfq8e1EMbdt4lVlTZyp3 XwQVCkTIUBSZ/4dRUhNXJA6mVeugss5jDiIyRJNDdsaf2GgObo6JlGSBawzD2r2w1uEa N90g== X-Forwarded-Encrypted: i=1; AJvYcCWg8eRMdIkDKWBMELP+5K6S12z5KemAmRB+U923caXkHHpg56tSvHx+zd6w4UAOUYY2v4ggq8vuOn+ca9w=@vger.kernel.org X-Gm-Message-State: AOJu0YxT2mrwOpuE76O3wL7vJmLzCI6Ff0gVRY+0DhN/F8jwl32lPX6D zQ0gMQbrQ7gCTIyHc6VgQmlaoA5DewS2hImTz1ATh+fYxxLAOgKWVMPMPxod0UBN8Tep+PLiWuC Zos7u6iE9fYA6jw== X-Google-Smtp-Source: AGHT+IHlexuAvDhnFGtg1cjQs73mJGUfm4wmIrjXrh2KvcJRouRaW+y+oFum0/DZIEri+Qoz+nn0V8NIBFvvdQ== X-Received: from pfoc3.prod.google.com ([2002:aa7:8803:0:b0:781:1533:32ab]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1491:b0:792:5db2:73ac with SMTP id d2e1a72fcca58-7b7a23b3a68mr5122550b3a.7.1762975382516; Wed, 12 Nov 2025 11:23:02 -0800 (PST) Date: Wed, 12 Nov 2025 19:22:26 +0000 In-Reply-To: <20251112192232.442761-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251112192232.442761-1-dmatlack@google.com> X-Mailer: git-send-email 2.52.0.rc1.455.g30608eb744-goog Message-ID: <20251112192232.442761-13-dmatlack@google.com> Subject: [PATCH v2 12/18] vfio: selftests: Move IOVA allocator into iova_allocator.c From: David Matlack To: Alex Williamson Cc: Alex Mastro , Alex Williamson , David Matlack , Jason Gunthorpe , Josh Hilke , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Raghavendra Rao Ananta , Vipin Sharma Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move the IOVA allocator into its own file, to provide better separation between the allocator and the struct vfio_pci_device helper code. The allocator could go into iommu.c, but it is standalone enough that a separate file seems cleaner. This also continues the trend of having a .c for every major object in VFIO selftests (vfio_pci_device.c, vfio_pci_driver.c, iommu.c, and now iova_allocator.c). No functional change intended. Signed-off-by: David Matlack --- .../selftests/vfio/lib/iova_allocator.c | 94 +++++++++++++++++++ tools/testing/selftests/vfio/lib/libvfio.mk | 1 + .../selftests/vfio/lib/vfio_pci_device.c | 71 -------------- 3 files changed, 95 insertions(+), 71 deletions(-) create mode 100644 tools/testing/selftests/vfio/lib/iova_allocator.c diff --git a/tools/testing/selftests/vfio/lib/iova_allocator.c b/tools/test= ing/selftests/vfio/lib/iova_allocator.c new file mode 100644 index 000000000000..f03648361ba2 --- /dev/null +++ b/tools/testing/selftests/vfio/lib/iova_allocator.c @@ -0,0 +1,94 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +struct iova_allocator *iova_allocator_init(struct vfio_pci_device *device) +{ + struct iova_allocator *allocator; + struct iommu_iova_range *ranges; + u32 nranges; + + ranges =3D vfio_pci_iova_ranges(device, &nranges); + VFIO_ASSERT_NOT_NULL(ranges); + + allocator =3D malloc(sizeof(*allocator)); + VFIO_ASSERT_NOT_NULL(allocator); + + *allocator =3D (struct iova_allocator){ + .ranges =3D ranges, + .nranges =3D nranges, + .range_idx =3D 0, + .range_offset =3D 0, + }; + + return allocator; +} + +void iova_allocator_cleanup(struct iova_allocator *allocator) +{ + free(allocator->ranges); + free(allocator); +} + +iova_t iova_allocator_alloc(struct iova_allocator *allocator, size_t size) +{ + VFIO_ASSERT_GT(size, 0, "Invalid size arg, zero\n"); + VFIO_ASSERT_EQ(size & (size - 1), 0, "Invalid size arg, non-power-of-2\n"= ); + + for (;;) { + struct iommu_iova_range *range; + iova_t iova, last; + + VFIO_ASSERT_LT(allocator->range_idx, allocator->nranges, + "IOVA allocator out of space\n"); + + range =3D &allocator->ranges[allocator->range_idx]; + iova =3D range->start + allocator->range_offset; + + /* Check for sufficient space at the current offset */ + if (check_add_overflow(iova, size - 1, &last) || + last > range->last) + goto next_range; + + /* Align iova to size */ + iova =3D last & ~(size - 1); + + /* Check for sufficient space at the aligned iova */ + if (check_add_overflow(iova, size - 1, &last) || + last > range->last) + goto next_range; + + if (last =3D=3D range->last) { + allocator->range_idx++; + allocator->range_offset =3D 0; + } else { + allocator->range_offset =3D last - range->start + 1; + } + + return iova; + +next_range: + allocator->range_idx++; + allocator->range_offset =3D 0; + } +} + diff --git a/tools/testing/selftests/vfio/lib/libvfio.mk b/tools/testing/se= lftests/vfio/lib/libvfio.mk index 7ecf2ad75c67..f15b966877e9 100644 --- a/tools/testing/selftests/vfio/lib/libvfio.mk +++ b/tools/testing/selftests/vfio/lib/libvfio.mk @@ -4,6 +4,7 @@ ARCH ?=3D $(SUBARCH) LIBVFIO_SRCDIR :=3D $(selfdir)/vfio/lib =20 LIBVFIO_C :=3D iommu.c +LIBVFIO_C +=3D iova_allocator.c LIBVFIO_C +=3D vfio_pci_device.c LIBVFIO_C +=3D vfio_pci_driver.c =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 5e0de10df04e..a59c86797897 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -24,77 +24,6 @@ =20 #define PCI_SYSFS_PATH "/sys/bus/pci/devices" =20 -struct iova_allocator *iova_allocator_init(struct vfio_pci_device *device) -{ - struct iova_allocator *allocator; - struct iommu_iova_range *ranges; - u32 nranges; - - ranges =3D vfio_pci_iova_ranges(device, &nranges); - VFIO_ASSERT_NOT_NULL(ranges); - - allocator =3D malloc(sizeof(*allocator)); - VFIO_ASSERT_NOT_NULL(allocator); - - *allocator =3D (struct iova_allocator){ - .ranges =3D ranges, - .nranges =3D nranges, - .range_idx =3D 0, - .range_offset =3D 0, - }; - - return allocator; -} - -void iova_allocator_cleanup(struct iova_allocator *allocator) -{ - free(allocator->ranges); - free(allocator); -} - -iova_t iova_allocator_alloc(struct iova_allocator *allocator, size_t size) -{ - VFIO_ASSERT_GT(size, 0, "Invalid size arg, zero\n"); - VFIO_ASSERT_EQ(size & (size - 1), 0, "Invalid size arg, non-power-of-2\n"= ); - - for (;;) { - struct iommu_iova_range *range; - iova_t iova, last; - - VFIO_ASSERT_LT(allocator->range_idx, allocator->nranges, - "IOVA allocator out of space\n"); - - range =3D &allocator->ranges[allocator->range_idx]; - iova =3D range->start + allocator->range_offset; - - /* Check for sufficient space at the current offset */ - if (check_add_overflow(iova, size - 1, &last) || - last > range->last) - goto next_range; - - /* Align iova to size */ - iova =3D last & ~(size - 1); - - /* Check for sufficient space at the aligned iova */ - if (check_add_overflow(iova, size - 1, &last) || - last > range->last) - goto next_range; - - if (last =3D=3D range->last) { - allocator->range_idx++; - allocator->range_offset =3D 0; - } else { - allocator->range_offset =3D last - range->start + 1; - } - - return iova; - -next_range: - allocator->range_idx++; - allocator->range_offset =3D 0; - } -} - static void vfio_pci_irq_set(struct vfio_pci_device *device, u32 index, u32 vector, u32 count, int *fds) { --=20 2.52.0.rc1.455.g30608eb744-goog