From nobody Tue Dec 2 01:26:44 2025 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 E83A73502B3 for ; Fri, 21 Nov 2025 18:14:43 +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=1763748885; cv=none; b=MeoEfupt5ZYZv7teSNQMTbqr/z3UZbB2Chr1fwugr5t1Y4c/8AtghiFD7s8EdQkMgN5Zljlxh3BZSVa5fRDzIKNiiLrgS07yDQan2b8CFhvMNMod/eTy0qT4NvD6SEPxIACWyqRQytl3ED2+s8un+zBmdP0g5X3knMUARg6W0iw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763748885; c=relaxed/simple; bh=Wjo5S3jm4rIHHKDO95Npld0R/MM3G2rBK7det+W9InI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=NFkF9jCJvwfmS/XW8j77oAA80fXL/6yCN1+tomUOrG2pxSk9PHh+hDHHrDRwrjtopixU+SJAiyXBirKrAqVWnkYpIsKQ8fvOqFzNrzIA3o3MRXW4GLSW/jAfHJU+qVjQ94nvkOQogOdaGXvHb2PIMd66HKYHolt0at7DwGSA1XA= 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=eYnDilA6; 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="eYnDilA6" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-7b8973c4608so7110774b3a.3 for ; Fri, 21 Nov 2025 10:14:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1763748883; x=1764353683; 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=g124QBzcdvPEK+J/83+56gYFpABGkICYbD/An5ySYhE=; b=eYnDilA6k6/ZEs5TTKKO4ywrCh60P18AKcvg/PZUOHaSsJANIExPmtpcu/NBoabIAO AqJXa6VWjYZznz3oZRUVNBfXByzEcjgVSQM7pBKcOw0cph+BVtt3/JOzOtgB8rKh0Kab msvYoeqlO2u0ICxBY5N7FusUGs9bMkijbz/qjkzy0V7dJ3PNrcbzg2UKJ3zP78dv7aEZ 3QLQJzkiBMqglThs4J/MAq2OdS6b5h7X6ndoSHTRaAB1fVZtToJKqCQHhZ8rIZPkxnrh WDAEt7tEpanqgnuYXOnoZKOoADQBiHVLBCZ0Bl/utpLGbdoyOW5/zNBT5+ay5gJajjD7 UECw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763748883; x=1764353683; 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=g124QBzcdvPEK+J/83+56gYFpABGkICYbD/An5ySYhE=; b=HZh4jsJ6CpkFPZ00jN8+bHaPMDnpXHqr+J8sk6HUGUFEDOSeHeTMjZTqlIkghc2VDm Y3TLzPKfcqC3i1in8dE0q8EPeTxh4OHN2yqD5/CJA2qHJwuyykm4vjAvtFwpfHGCM18t GJVWFc3P4hFqF9fmVHSfe4dQUocOY1u0sRVB+7f9U5zIREG7oiNOJGrvIpa1S2hViH5w ntAOODTzRkc86/rUeEKsNOkNeb9ihjd6mGmW8m1k7phkeIPkD17XQuhIUtAnBnV3PlJD gqDJQluhdEw7c6zosKR+kULTcFWyjE1FLWm9EZwShu36MA+k63DBik+FIpDpysVI8j9j Sl9Q== X-Forwarded-Encrypted: i=1; AJvYcCUG1+2jaVVLUlQUcx5pD1e80Ismf/YO660Jhb+izJ1d2GCqYGRQP4RnrSRA+hW8MbAehVfXCsfsebBaBxY=@vger.kernel.org X-Gm-Message-State: AOJu0YxT3CTt0jpYT4pFusa7kbvu16l/8Z9a5VACdTzPe3+d08I4rx0o PMxhE6qR6YLf0WdynwTbInoLiQdkJ6IMvsZ45g+zdgFj/pj8X6aKlWDgWO1YbB9NKx0LAeqsxjI snv9GeP5j56zCvw== X-Google-Smtp-Source: AGHT+IE6bTYkPneatI//ZdOy1x5PgnIneKQ606XIGGaa0iqn+syJEbxJdH/cKE3PeHga9Qitv+c9GDWTPxOr8g== X-Received: from pfjt15.prod.google.com ([2002:a05:6a00:21cf:b0:7c2:a212:2b32]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:22cb:b0:7b9:d7c2:fdf6 with SMTP id d2e1a72fcca58-7c58e601920mr4056673b3a.24.1763748883301; Fri, 21 Nov 2025 10:14:43 -0800 (PST) Date: Fri, 21 Nov 2025 18:14:14 +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-4-dmatlack@google.com> Subject: [PATCH v3 03/18] vfio: selftests: Allow passing multiple BDFs on the command line 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" Add support for passing multiple device BDFs to a test via the command line. This is a prerequisite for multi-device tests. Single-device tests can continue using vfio_selftests_get_bdf(), which will continue to return argv[argc - 1] (if it is a BDF string), or the environment variable $VFIO_SELFTESTS_BDF otherwise. For multi-device tests, a new helper called vfio_selftests_get_bdfs() is introduced which will return an array of all BDFs found at the end of argv[], as well as the number of BDFs found (passed back to the caller via argument). The array of BDFs returned does not need to be freed by the caller. The environment variable VFIO_SELFTESTS_BDF continues to support only a single BDF for the time being. Reviewed-by: Alex Mastro Tested-by: Alex Mastro Signed-off-by: David Matlack --- .../selftests/vfio/lib/include/vfio_util.h | 2 + .../selftests/vfio/lib/vfio_pci_device.c | 57 +++++++++++++++---- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/tools/testing/selftests/vfio/lib/include/vfio_util.h b/tools/t= esting/selftests/vfio/lib/include/vfio_util.h index 69ec0c856481..0b9a5628d23e 100644 --- a/tools/testing/selftests/vfio/lib/include/vfio_util.h +++ b/tools/testing/selftests/vfio/lib/include/vfio_util.h @@ -208,6 +208,8 @@ struct iova_allocator { * If BDF cannot be determined then the test will exit with KSFT_SKIP. */ const char *vfio_selftests_get_bdf(int *argc, char *argv[]); +char **vfio_selftests_get_bdfs(int *argc, char *argv[], int *nr_bdfs); + const char *vfio_pci_get_cdev_path(const char *bdf); =20 extern const char *default_iommu_mode; diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index b479a359da12..eda8f14de797 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -868,29 +868,64 @@ static bool is_bdf(const char *str) return count =3D=3D 4 && length =3D=3D strlen(str); } =20 -const char *vfio_selftests_get_bdf(int *argc, char *argv[]) +static char **get_bdfs_cmdline(int *argc, char *argv[], int *nr_bdfs) { - char *bdf; + int i; =20 - if (*argc > 1 && is_bdf(argv[*argc - 1])) - return argv[--(*argc)]; + for (i =3D *argc - 1; i > 0 && is_bdf(argv[i]); i--) + continue; + + i++; + *nr_bdfs =3D *argc - i; + *argc -=3D *nr_bdfs; + + return *nr_bdfs ? &argv[i] : NULL; +} + +static char *get_bdf_env(void) +{ + char *bdf; =20 bdf =3D getenv("VFIO_SELFTESTS_BDF"); - if (bdf) { - VFIO_ASSERT_TRUE(is_bdf(bdf), "Invalid BDF: %s\n", bdf); - return bdf; + if (!bdf) + return NULL; + + VFIO_ASSERT_TRUE(is_bdf(bdf), "Invalid BDF: %s\n", bdf); + return bdf; +} + +char **vfio_selftests_get_bdfs(int *argc, char *argv[], int *nr_bdfs) +{ + static char *env_bdf; + char **bdfs; + + bdfs =3D get_bdfs_cmdline(argc, argv, nr_bdfs); + if (bdfs) + return bdfs; + + env_bdf =3D get_bdf_env(); + if (env_bdf) { + *nr_bdfs =3D 1; + return &env_bdf; } =20 - fprintf(stderr, "Unable to determine which device to use, skipping test.\= n"); + fprintf(stderr, "Unable to determine which device(s) to use, skipping tes= t.\n"); fprintf(stderr, "\n"); fprintf(stderr, "To pass the device address via environment variable:\n"); fprintf(stderr, "\n"); - fprintf(stderr, " export VFIO_SELFTESTS_BDF=3Dsegment:bus:device.funct= ion\n"); + fprintf(stderr, " export VFIO_SELFTESTS_BDF=3D\"segment:bus:device.fun= ction\"\n"); fprintf(stderr, " %s [options]\n", argv[0]); fprintf(stderr, "\n"); - fprintf(stderr, "To pass the device address via argv:\n"); + fprintf(stderr, "To pass the device address(es) via argv:\n"); fprintf(stderr, "\n"); - fprintf(stderr, " %s [options] segment:bus:device.function\n", argv[0]= ); + fprintf(stderr, " %s [options] segment:bus:device.function ...\n", arg= v[0]); fprintf(stderr, "\n"); exit(KSFT_SKIP); } + +const char *vfio_selftests_get_bdf(int *argc, char *argv[]) +{ + int nr_bdfs; + + return vfio_selftests_get_bdfs(argc, argv, &nr_bdfs)[0]; +} --=20 2.52.0.rc2.455.g230fcf2819-goog