From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.160.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794425059; b=YDEXqmrb2D8crcRU/TnTsIKt9g06IVm09HfPGhaMylQXa/LV7kZuce9SCwEQxCtdPl+n VJu67b8i5gLiNGfvS5M4m3+LT5sHyDM1+6GF0Teh9PPX/krKlWqlCGtBzGZzY5tA70tdm 2vBsFm5FGEZ6tRfhQzyVVeXaI0K1SizWIt02Nj9HQ7H0ybIYZMM7CFaQ6E3AeR4/t7NAw MTea8Jxs0nse3HpdSGkIoYjztwrlsPFubPX9koMqHP/FsY3n/yPFM2woA5L3gK3tU34X0 Kje7+cwbm0Ep8WCrvbkBx0ZsG4/e000q+BUzykWIy8Drzg7lkyYeqR8pXVtD/pzGdXQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794425059; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=c1gmqyzGXPZYxPfKkRYqwd2gnFVkzUbXgN8JNV1Au+8=; b=ZL3ianALgRK5QwQm9G+msCTn+Ldby/jKSMd8SoHwPxAskBbgNvvoyPmZe01vgOMjueXj Sb22zcvb3bbPfKwQ0w/DVmSBD7kIwLI1f797omv3N2CxyQQMz1nlZWutR8D7RUKQuah5o xiDi1pG+nH/Q2wBa3XRTMOrjbh/O79GpRVTVG3OC7+vorP9RX135vNEm/gXNPvPvYugcb 2s751YITrLf7HbNuc0kl1SKIFXGBpfs63a7NotugtVggB3ex2cgLecDtEl4KYtuJ1oA02 qVmQ15n9XajQ3B4lzGVCJAAvmgUV3C+rJKNFqab9oLmCl464bnpgdTvKDAgn406WIQw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=K8+SIvuN; arc=none smtp.remote-ip=209.85.160.70 Received: from mail-oa1-f70.google.com (mail-oa1-f70.google.com [209.85.160.70]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 24EB1606A8BB for ; Wed, 11 Nov 2026 19:24:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 24EB1606A8BB 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.a=rsa-sha256 header.s=20260707 header.b=K8+SIvuN Received: by mail-oa1-f70.google.com with SMTP id 586e51a60fabf-4adae323d5fso2095356fac.3 for ; Wed, 11 Nov 2026 11:24:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1794425057; x=1795029857; darn=lists.linux.dev; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=c1gmqyzGXPZYxPfKkRYqwd2gnFVkzUbXgN8JNV1Au+8=; b=K8+SIvuNMd2MHXkdpke9f9xZNMFNdkVi0h3Y1hI/SIkJXfP5ta6qtCH/pcqqCRSeUq AAIHNvdgc7ZDpZ7TKahFJ49PuLxw60fT9LpsoOcpsDW5NsiEcPdxlgpasdXlPJsgNOjW wqJpdn/VtURdRQ8UB6trFdiZAvZxFCnqyfuJkENEEGupGKLV+Hyr91n89S//qfpbJkWS uGooFAYe5ijKR47s6YCgHLw8HZiEXfF6WTJrGfBD0dj7Z8dHpsYkh37tajggG7HuakbP NIYJu/UnhVnKWpppHb/wPLmVZWXvef2a7C+yPgj95LkzTnaDXFjEE791UB3E28mVvUsn NSrA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794425057; x=1795029857; h=content-type: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:content-type; bh=c1gmqyzGXPZYxPfKkRYqwd2gnFVkzUbXgN8JNV1Au+8=; b=EoxSwiD0wbuRIqX2rQ6nhlYbH/qIU8ea8tloIoTzryGakGRmUMosth8qyMzXGkRdhy K3EKdV9ZoMT9Cdj74llssAb0J2z5SAtrQMXeIUyeQIOH8WF/lkWhurtd3OBqQuAu3Emf Qm2BMRn+kxqtOfOINbKUXVLQ3ZEdAOZLlgaFBoe+LF1gbDoy6MwE/4TqKbs0buYdu8kl aLZSzYkln/Bjr7tb03I7QOnB1tK8nbtrrXbiAshWIGTna/ZVIFaGrVRwyLjVwGLYNyU/ Xvkd9kRVepAW6s1Z3sFwajz3KgU8iZPK/8ap4FIuMI4BYaTkmvd9qaye/V8d5C1+jDZh zbZg== X-Forwarded-Encrypted: i=1; AL+hErGgSJuB6NMnAdiM+a5LKftc8Up9xvQpCwdvKm2Qw0Uc3LzcUsa5u3BZXZcLZo/Fa+VsEKyHLQ==@lists.linux.dev X-Gm-Message-State: APuU4Z+Uk9JkFzR7E/ks+5YpL0Rm3bIHpXP31t8zpoRlnlwEwvXK0gib lG8AxN9d7qsvHMXq0/2QARaNXu+RyBM6oE1XjVvpW+UypgoZonXs4TqyNJdf+jj2/AbSmHQ9zpk 2IkRf8Dqf0GTg0g== X-Received: from ilbdx27-n1.prod.google.com ([2002:a05:6e02:421b:10b0:53d:3ab5:58fa]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:1382:b0:567:f784:f939 with SMTP id 5614622812f47-56b9791019amr2605655b6e.51.1794425056843; Wed, 11 Nov 2026 11:24:16 -0800 (PST) Date: Wed, 11 Nov 2026 19:24:05 +0000 In-Reply-To: <20261111192411.4133780-1-dmatlack@google.com> X-Mailing-List: iommu@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261111192411.4133780-1-dmatlack@google.com> X-Mailer: git-send-email 2.56.0.567.gdb80ae8cd5-goog Message-ID: <20261111192411.4133780-2-dmatlack@google.com> Subject: [RFC PATCH 1/7] vfio: selftests: Add vfio_pci_find_mem_bar() helper From: David Matlack To: dri-devel@lists.freedesktop.org, iommu@lists.linux.dev, kvm@vger.kernel.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-media@vger.kernel.org, linux-pci@vger.kernel.org Cc: Alex Mastro , Alex Williamson , Bjorn Helgaas , "=?UTF-8?q?Christian=20K=C3=B6nig?=" , David Matlack , Jason Gunthorpe , Josh Hilke , Kevin Tian , Leon Romanovsky , Logan Gunthorpe , Matt Evans , Matt Evans , Mina Almasry , Samiullah Khawaja , Shuah Khan , Sumit Semwal , Vipin Sharma Content-Type: text/plain; charset="UTF-8" From: Mina Almasry Add vfio_pci_find_mem_bar() to libvfio to locate a BAR (or range within a BAR) of at least min_size bytes that behaves like ordinary memory and can be safely read and written without device-specific side effects. Detection is backed by a table of known devices, starting with QEMU's pci-testdev (1b36:0005) BAR 2 when configured with membar= and membar-backed=on. Suggested-by: David Matlack Cc: David Matlack Cc: Matt Evans Signed-off-by: Mina Almasry Reviewed-by: David Matlack Signed-off-by: David Matlack --- .../lib/include/libvfio/vfio_pci_device.h | 10 +++ .../selftests/vfio/lib/vfio_pci_device.c | 67 +++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h b/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h index 007adc322c1e..19d458adba1a 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 @@ -15,6 +15,13 @@ struct vfio_pci_bar { void *vaddr; }; +struct vfio_pci_mem_bar { + struct vfio_pci_bar *bar; + void *vaddr; + u64 offset; + u64 size; +}; + struct vfio_pci_device { const char *bdf; int fd; @@ -162,6 +169,9 @@ static inline bool vfio_pci_device_match(struct vfio_pci_device *device, (device_id == vfio_pci_config_readw(device, PCI_DEVICE_ID)); } +bool vfio_pci_find_mem_bar(struct vfio_pci_device *device, u64 min_size, + struct vfio_pci_mem_bar *mem_bar); + const char *vfio_pci_get_cdev_path(const char *bdf); void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf); diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/testing/selftests/vfio/lib/vfio_pci_device.c index 4063a0e2b3df..d5308fe61490 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -245,6 +246,72 @@ static void vfio_pci_bar_unmap_all(struct vfio_pci_device *device) } } +#define PCI_DEVICE_ID_REDHAT_TEST 0x0005 + +struct vfio_pci_mem_bar_desc { + u16 vendor_id; + u16 device_id; + u8 bar_index; + u64 offset; + u64 size; +}; + +static const struct vfio_pci_mem_bar_desc mem_bar_descs[] = { + /* QEMU pci-testdev with membar=,membar-backed=on */ + { + .vendor_id = PCI_VENDOR_ID_REDHAT, + .device_id = PCI_DEVICE_ID_REDHAT_TEST, + .bar_index = 2, + }, +}; + +bool vfio_pci_find_mem_bar(struct vfio_pci_device *device, u64 min_size, + struct vfio_pci_mem_bar *mem_bar) +{ + const u32 rw_mmap_flags = VFIO_REGION_INFO_FLAG_MMAP | + VFIO_REGION_INFO_FLAG_READ | + VFIO_REGION_INFO_FLAG_WRITE; + size_t i; + + for (i = 0; i < ARRAY_SIZE(mem_bar_descs); i++) { + const struct vfio_pci_mem_bar_desc *desc = &mem_bar_descs[i]; + struct vfio_pci_bar *bar; + u64 size; + + if (!vfio_pci_device_match(device, desc->vendor_id, + desc->device_id)) + continue; + + VFIO_ASSERT_LT(desc->bar_index, PCI_STD_NUM_BARS); + bar = &device->bars[desc->bar_index]; + + if ((bar->info.flags & rw_mmap_flags) != rw_mmap_flags) + continue; + + VFIO_ASSERT_NOT_NULL(bar->vaddr); + + if (bar->info.size <= desc->offset) + continue; + + size = bar->info.size - desc->offset; + if (desc->size) + size = min(size, desc->size); + + if (size < min_size) + continue; + + *mem_bar = (struct vfio_pci_mem_bar){ + .bar = bar, + .vaddr = bar->vaddr + desc->offset, + .offset = desc->offset, + .size = size, + }; + return true; + } + + return false; +} + void vfio_pci_config_access(struct vfio_pci_device *device, bool write, size_t config, size_t size, void *data) { -- 2.56.0.567.gdb80ae8cd5-goog