From nobody Mon Dec 1 22:34:45 2025 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (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 6A1DE348468 for ; Wed, 26 Nov 2025 23:18:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764199083; cv=none; b=Im31a9J2cogju3fJh/TxMwOKqYAKzFA93p/YtsFHJHHPkpvj4PWNaW8hJsivzo1LNu5xdV8QU4bOKfMbOBl00p2ZS9Ypn5azc/TJbEkXBnai2N3am75E8morZdT82BbDwa0EmCPOyB8Bxl9YahRschwprAJGs2k+weAriu71+Hs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764199083; c=relaxed/simple; bh=7PVn76b8rX0MxJTUfhyoehbiKMnipdTAASaM5dmOZs0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=vEft7i++zSSMUXrk6iQNOPNi5YxfiRSCuHHh/AeF3nHGO/LEtEv7/E8TJGfEF5lWoG7wA0BE+v4DE4vzR4lfcXOoQPYuMN+F5dW8WT6vU8hnb8zQpfkCZdtSP7y4UjpU7SQHYFzbNwTjFztYIWxgmO/9UDtwhKcrsAvWdQzoZFY= 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=3kiLKW8p; arc=none smtp.client-ip=209.85.210.201 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="3kiLKW8p" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7b8973c4608so638604b3a.3 for ; Wed, 26 Nov 2025 15:18:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764199081; x=1764803881; 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=EK277X+oVRo/3waBm9mTxQf5GxrA/dPpZ/zTyzS81bg=; b=3kiLKW8pqKLgErADigkgQQZ8ywsz+TYlrJteNXQKO+14WxQTWiykb6HI8rKJ/4xYQ0 cvja3AMzk+JSNQksyntkiq7Q8eDmxYjFhVqN3DSqHxB0tVYXcaLq+xnKSShtfTT8h+N6 f9Tb31cORdjWphy3y5+E1UFCdn7HErQcxFmw/YR50elXJD/ioQ53wqPAxD+AmSk7owxH SlC6JIzV0qNSMdC5T1tSP7ct9Ep4vGhkm4x5cmhuwl0f1IOUzuIUMctacI+70oGoMukg jAKyFuEcwNGR7OF5G9Cn63T5QMlUnbffEC6JpFTRKeY9TzTwLflPFZc6PbUU3K9GrU/f Od7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764199081; x=1764803881; 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=EK277X+oVRo/3waBm9mTxQf5GxrA/dPpZ/zTyzS81bg=; b=nW3/7cpRYFrN/dsjhK/TtLRduYEPBEiNTO9tW2o4Ys19bTo06CnQuoApAbMoycTgVk K9yv7+nzNFoR9XRgALGaZZFe3O6+XNFwflv06SVRPlJknX4YHFpvZ5ejLEVw5L3JuSSL 6hcvb/GE3E+xYuRRRD++U3ihn8s59tXt8uS1Kjv13Ef3QgPgRuvaYQWVLLGUdFJEXiP9 737jBM0Hr0zCXs12mFfqjLZnQmQWFv1N8umB593UswCxrRv3hICuKelQQSShnfZ5cGg9 gShij5p6c6JnAbrCL7nIy9BpMOIsfXIndEdGYta4FqCaok+a/hix81LP/SvgQBtxEFdn T71Q== X-Forwarded-Encrypted: i=1; AJvYcCVmNuyBxjFIbk2vcd+D14eMkFh9NtopnhViNL2Fy1FF0imLu+VVQ1+UJ/9JYqjzdKyVfq/jkzNfxBMM1ec=@vger.kernel.org X-Gm-Message-State: AOJu0YxlDGRgqTVoHhdI/DKE0soJrAORNanfFF9livK1RwjQP3fiKOG7 OxwpWwOv1L3jgRVkewAYPfeVjcJb+i1iYL7l7nJUfETekP/iKpFdn2ILHsx0IjVG3+96xBTnzGb pKi6+9YWsBZ3a4w== X-Google-Smtp-Source: AGHT+IF4HzJU/io7p5u55ySnDmOnh3kDCjx8YWbt0k4JunTmVTYnjTqHIkASsk3cbqu5Mw+7w80+iyJ1PeJs/w== X-Received: from pgbdl10.prod.google.com ([2002:a05:6a02:d0a:b0:b6c:f35f:bae9]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:3c8d:b0:35f:84c7:4031 with SMTP id adf61e73a8af0-3637e0d3593mr9350366637.55.1764199080759; Wed, 26 Nov 2025 15:18:00 -0800 (PST) Date: Wed, 26 Nov 2025 23:17:27 +0000 In-Reply-To: <20251126231733.3302983-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: <20251126231733.3302983-1-dmatlack@google.com> X-Mailer: git-send-email 2.52.0.487.g5c8c507ade-goog Message-ID: <20251126231733.3302983-13-dmatlack@google.com> Subject: [PATCH v4 12/18] vfio: selftests: Move IOVA allocator into iova_allocator.c From: David Matlack To: Alex Williamson Cc: Alex Mastro , 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. Reviewed-by: Alex Mastro Tested-by: Alex Mastro Reviewed-by: Raghavendra Rao Ananta 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 10fc016a2a3e..aa5b45052c77 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.487.g5c8c507ade-goog