From nobody Tue Dec 2 01:27:31 2025 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.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 6BF70351FDE for ; Fri, 21 Nov 2025 18:15:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763748908; cv=none; b=o+7ZIt/+9xh2yW6GlQsiDqfiW3h0D1tWL962jSIl5WII2K9ZvJfK7tjXInP+le8JPuTDJ1cD4LDsBO/t8NFKnernY2fP52+tDGSV0b8PbigOvhVAKO1kDDcI78DidcJPyzIXACrKKxw1EzC4PnhKwlAQO4tdzB9isWfaEZt8PMA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763748908; c=relaxed/simple; bh=KYSZvwIYuGqWONSGyW/cf+OrbkOd5CohW/aJh2gqPA4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=a1dkCFKnWoJiESMd3lwBGm46igYlHYJrfRcxyGFJg/dYanVDKzNEb4QJfSyp+r69+ZKnBCde0UtQxh8W0dDtRvwF/Tnuml8VbxUuphuLr4F+W+96GAFdJWmu6uulUvdJHY/fII4OLQNwJz+rCpKfJC82FbTu1Khd5LuGpNexaxk= 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=TVM8pyJe; arc=none smtp.client-ip=209.85.216.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--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="TVM8pyJe" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-340ad9349b3so4822940a91.1 for ; Fri, 21 Nov 2025 10:15:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1763748898; x=1764353698; 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=P6NpR6v3M9pBWa8mqzX9JiTE9lvDMmHIPBrLIUR3fl0=; b=TVM8pyJeWP4jICSK4W32NS0AZtf/rlYi1R944/LImVOYN1UKdxAxCuEEmbWhH6drrN IeFHqPLqnsz5wKzqczhNWlmWI4kRsGg1SeGJQ+8q5WlsYOYsA+EoHDZoWrFd68xzn/bh Nt6JcCqWWTcQin8jNVC9+nCtfgX5MXx1PE2rOSMaw34z7IaOvHOxOGneGsbRDDaBsIMJ aHtfuoXDZf1DzKdldMcRtCopaAntTbRreuP/4Q3zAN6fxLl2vcMyuQLD/ZH2ibrKFAFf 6muyXnPCw2+wIRSpBAnj4KMUWk2TBd3F69Gnl+KWF+cH1CH31EnzOXzfI/VFmPcY+jJ7 5Asw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763748898; x=1764353698; 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=P6NpR6v3M9pBWa8mqzX9JiTE9lvDMmHIPBrLIUR3fl0=; b=hHFqKs19qfFsmecPY5v1rrqKQCKp1GvIeDXmKIodl1of9GnkjYzQgSpolaGEvxoChq 6CD7tMcIOa3WYVcva9e5Br5lbaD7XM8LbaSFtoUEFj2zWx6JyaJWgVOP6yX5rNL1Lsj6 ANd7xnaoxxPstIS2dPIk82vVs21+fzGZAOZGbT5GfHPZ5tsflBMVMlelT5KtkZd0M2sx zk/Aje180FlF2kbCT48WofF72XoIk8McE6rg4U2f3cHLBvGTDiWbT8JqZYWuu72cE6rQ Ao9YgumbQVl8cwwOKyg0v8LDtRBr/DxSFmHDkKnL/H7aDmDa6VwbW1CkF6DzeD3VZ3bz WL6g== X-Forwarded-Encrypted: i=1; AJvYcCVpr+e9uXC4YPTQJid32NLJ712VcpCme/qWkjHhhyMV+FgbUOe9YLvnu0ybzhCCc4MNK9qvzNRs+l4+h+g=@vger.kernel.org X-Gm-Message-State: AOJu0Yyk1T63SnXlOsuKhaYmw117hnjOktPtI8x7EtwU/Z137t6yFmzB uX0tCLmlgTrA/Y1wH1PUJ0W2802vJF5Ah3si3IE6OxrVYJ5JU0ELwfaTkV3H67bR77APGBmM8+m Zf5ZytbXOP+Y3YA== X-Google-Smtp-Source: AGHT+IGaxmIgScgBNXnSMh0Y/1HTLLgh3WxboedX5JBI9DISIv1p1rfPRqF9Eq05K/1531C61+/6sE2AMWZStw== X-Received: from pfee15.prod.google.com ([2002:a05:6a00:bc8f:b0:7a3:23d4:f5f9]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:9186:b0:350:1a0e:7fc5 with SMTP id adf61e73a8af0-3614eeb0d8emr4019398637.60.1763748898201; Fri, 21 Nov 2025 10:14:58 -0800 (PST) Date: Fri, 21 Nov 2025 18:14:23 +0000 In-Reply-To: <20251121181429.1421717-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: <20251121181429.1421717-1-dmatlack@google.com> X-Mailer: git-send-email 2.52.0.rc2.455.g230fcf2819-goog Message-ID: <20251121181429.1421717-13-dmatlack@google.com> Subject: [PATCH v3 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 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.rc2.455.g230fcf2819-goog