From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794425059; b=37No+ozvBKiY5HqnmZ+wpWa44gSEnSMeYp+2rpDEKWPpDH1MhSj3Ww8oVtPwdNFDOQcO kZlUsnyJq8J5zKoaSokn5jNgYLiIRmJbsqANshEoN05+dbWDrzFra3RmJ8JfzRW8Cb/Zs KHeeDlYGivN555zWqxuMv0QbCpiRHbj6cLYdU+RiATEhYbdPSvXIFM8GkZquzNUHUQJx1 yFWXUoZbKlYhszXma4je2M2KLF9CXoZc0LX4Lhk4mmwQNKUOCXB0FE16nP6PYm11fg2uu 4Z+Szfoop+olbBfmsI6N4xcJJ37V8hIyWPg7DEUR3rFJV0UKbwn5xrLL6go+puzs0OA== 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=s6lt1PxCFx0agGLbXpFKlY/hoUAzM5xkPqAiIkWnGNA=; b=btahhH0eCwMtUckFn/t6/ZK7DZeHKOZzHJ2GOWe5AjsYPxLSHcGTIztUkkcBAe7khTEd F+vOpxaA+1W4g5W5F1PTDp8cKlSEL5lAq0XyC3y94ZizF1ouY5w61dJK5lYWw2QEa6Hsm KVQkp7soRE7uQ7EtmrfDuuyGPEY0bFlMg1UxGV1ur0oxRrFHfSP5UlEWm1fvxUilxGLns ZmRhmZ7q4cNX9hkjl8awB96JfLAzewCxaAuGaQPrVo7/mtq4c8GFhzDW4bsMxg0hRWflB VnKTPS5NfkLiDssOn6OBcopd4jyMIaivpeNW2qO+eLZpidABAz+KRipu7QEs8eXwROw== 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=UOq3nT8j; arc=none smtp.remote-ip=209.85.167.198 Received: from mail-oi1-f198.google.com (mail-oi1-f198.google.com [209.85.167.198]) (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 5BB96606DD50 for ; Wed, 11 Nov 2026 19:24:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5BB96606DD50 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=UOq3nT8j Received: by mail-oi1-f198.google.com with SMTP id 5614622812f47-4b26d8d2498so4016122b6e.2 for ; Wed, 11 Nov 2026 11:24:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1794425058; x=1795029858; darn=vger.kernel.org; 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=s6lt1PxCFx0agGLbXpFKlY/hoUAzM5xkPqAiIkWnGNA=; b=UOq3nT8jfNIxrMk+znyvt21C9lwSewOGrh1jpeVu5iK5VmdqJeDT4fPjN6nDS+s/rz E6Ho+bO3iAXvXkGBGN7CjciNZSs05x40V5C4Z9f0REOobKvYRI7U+31LkNo568hjhRz2 VA+FxI4lBIGhJ+ugD1He8j+J8VFk+dt26q+Q+oobnfzOprmYbN898bflauRMBfEEy4rd 5Njig5Se7CnUBaBXRSmjOERE/lZ83r/qYtfGEy9EiotAynG+Zijcmr8jieR2cggp/ebU BtjWu6T0pL3mwZ94u0gf7+eonYPgaPav+OYjwJPpOUc8KIt158SxR+gGtte9ruBukesx e0+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794425058; x=1795029858; 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=s6lt1PxCFx0agGLbXpFKlY/hoUAzM5xkPqAiIkWnGNA=; b=xZ3lIgC1phba/33TiCaF66p6nr/tRscNYeiLmX51gnx/aYclsr1M3Hj3AzT6++rjzo 7XHIHsprD+vjyokA9mXTMCA26u5KKXOYL/88ANnhbGRZgWpQlwdhAt2ggBP8Aoa2IyBV Pi9TbGkBfWyeZ5Z15F1/3dclGCmh8WIYJVDM7I2C2XOwW+29AUv/EkEX8k9cUNdk8Hwa xyydpsdtOh3h02NsrnIIStiOZEYK06UXMh0jvTMCSA27U7ArJgcTrizkDOogUqUn3rUF YfmKmFKDfHhsGTnDKQSX9+DfCUTxwJaxOUFnVJrKjmcqtJpthe5SuOlnOzBWD0WFYoYX OdhQ== X-Forwarded-Encrypted: i=1; AL+hErHuOcBMM4v8whBrajpUYvSbmxsmpElQlIqfXG4IhPv+qTZ60rrRUKPCD+Z+3dIUuJW+1a6KibL9bxD22A==@vger.kernel.org X-Gm-Message-State: APuU4Z97vIkLGBrKcD/enz1VpDGjuRYAfwWc69VqVEkWFBIxcoEOyD0p qovG6doeSlhvkbOHgE+h3NkzHy+lS3NEXrsXyerg0fgy6fOtFZjaMCbLD+2NE0QInI28YJi5sB8 ftFSNxTBxUmMl+A== X-Received: from iltr6.prod.google.com ([2002:a92:c5a6:0:b0:542:f469:6209]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:23c7:b0:568:e5f:5dbe with SMTP id 5614622812f47-56b97d0f43cmr3278637b6e.61.1794425058062; Wed, 11 Nov 2026 11:24:18 -0800 (PST) Date: Wed, 11 Nov 2026 19:24:06 +0000 In-Reply-To: <20261111192411.4133780-1-dmatlack@google.com> X-Mailing-List: linux-media@vger.kernel.org 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-3-dmatlack@google.com> Subject: [RFC PATCH 2/7] vfio: selftests: Verify memory behavior in vfio_pci_find_mem_bar() 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" Write and read back test patterns before returning a BAR from vfio_pci_find_mem_bar(), restoring the original contents and PCI_COMMAND_MEMORY state afterward. QEMU's pci-testdev exposes a readable, writable, mmap-capable BAR2 whenever membar= is passed, even if membar-backed=on was omitted and accesses are dropped as unimplemented MMIO. Probing the BAR upfront lets callers skip cleanly instead of failing later DMA or fault checks. Suggested-by: Alex Williamson Signed-off-by: David Matlack --- .../selftests/vfio/lib/vfio_pci_device.c | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/testing/selftests/vfio/lib/vfio_pci_device.c index d5308fe61490..9840463594ab 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -13,6 +13,7 @@ #include #include +#include #include #include #include @@ -265,6 +266,33 @@ static const struct vfio_pci_mem_bar_desc mem_bar_descs[] = { }, }; +static bool vfio_pci_mem_bar_verify(struct vfio_pci_device *device, void *vaddr) +{ + const u64 pattern = 0x55aa55aa55aa55aaULL; + u16 orig_cmd; + u64 orig; + bool ok; + + orig_cmd = vfio_pci_config_readw(device, PCI_COMMAND); + if (!(orig_cmd & PCI_COMMAND_MEMORY)) + vfio_pci_config_writew(device, PCI_COMMAND, + orig_cmd | PCI_COMMAND_MEMORY); + + orig = readq(vaddr); + writeq(pattern, vaddr); + ok = (readq(vaddr) == pattern); + if (ok) { + writeq(~pattern, vaddr); + ok = (readq(vaddr) == ~pattern); + } + writeq(orig, vaddr); + + if (!(orig_cmd & PCI_COMMAND_MEMORY)) + vfio_pci_config_writew(device, PCI_COMMAND, orig_cmd); + + return ok; +} + bool vfio_pci_find_mem_bar(struct vfio_pci_device *device, u64 min_size, struct vfio_pci_mem_bar *mem_bar) { @@ -273,6 +301,8 @@ bool vfio_pci_find_mem_bar(struct vfio_pci_device *device, u64 min_size, VFIO_REGION_INFO_FLAG_WRITE; size_t i; + min_size = max_t(u64, min_size, sizeof(u64)); + 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; @@ -300,6 +330,9 @@ bool vfio_pci_find_mem_bar(struct vfio_pci_device *device, u64 min_size, if (size < min_size) continue; + if (!vfio_pci_mem_bar_verify(device, bar->vaddr + desc->offset)) + continue; + *mem_bar = (struct vfio_pci_mem_bar){ .bar = bar, .vaddr = bar->vaddr + desc->offset, -- 2.56.0.567.gdb80ae8cd5-goog