From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ej1-f52.google.com (mail-ej1-f52.google.com [209.85.218.52]) (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 C026646AA80 for ; Mon, 5 Oct 2026 09:55:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791194158; cv=none; b=gKy7Nq958hzKCHWonX1pP51pDfaE5QwLMlcdkbeSKhIHzvRMa8fkRsnJqDsj96k8sfNdueozu5sjUPrx3YU7CAGWZ0/ZuIL2JbBVABYWZZX83F+0W5slX27lqYUeDfi18MwZvX7f4aFEkEuBTXE0kF2t1Ig46IcAEGEIm9rHfew= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1791194158; c=relaxed/simple; bh=zIgQPFMqRQism6lMFZ7Hr5G+GkKpMxBGzTbpENtH8zU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hg1UekcIJ1z5LzS+tYNh5LYEwAl91Ioc2++hXbZ0YtkBKLkoG2skEhXT+8Fq/jzdNATIUhjTu/x8ijk4A0OsSan0MTkXLvlT277BSWdCXAG/wwvjXeKrU/XPPfc0Ec02F8cpAxpi7x35vp1afrPqsTJvEt7Tk4Y7yqXVkXaKp94= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=OYBg5O1M; arc=none smtp.client-ip=209.85.218.52 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="OYBg5O1M" Received: by mail-ej1-f52.google.com with SMTP id a640c23a62f3a-c2e69aa07c3so219910966b.1 for ; Mon, 05 Oct 2026 02:55:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1791194154; x=1791798954; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=y5KZiX26aqwUyZIYbya9tfV4Mvg5MCMOLcvC9aKPc0E=; b=OYBg5O1M7WF+0xsmp5t3QOAR5Lr3XmuYyehdwHXeQ2i/Vso/vcctYzFfi+lFG6rYld oL91G4OL5lP8QVGDlf18Ma9EuR92jdk1m0hFrTaQy6wJSyXvxIV9HzQn7+sv/KVeWzAv /5EPdUPGfWD33k8VP5P+ipkCgS6kBSn0tWqhSMz6xfQjQ46t5ICSI2JEyoOsPEJs62i0 HAnNaQ39gy4sNEzYGpSm1n3qgEl+BKW1GxGA1IXTVIXBFGkIEZ6PuOA9LZvpS9jlW8ll AWPxhEn+zKNesFekacBDvJ/mCju/b0HF7auRMBcn2XCRcHUdtTvzLOtXaMdh/n+yrxsN ub6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1791194154; x=1791798954; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=y5KZiX26aqwUyZIYbya9tfV4Mvg5MCMOLcvC9aKPc0E=; b=L7lgOIrml4+IkW66bjcHNCvW9bNHKYyNlobe+Y9zOJhssSYbwg13/sZuME7BGK/OyM 6nQensJBI+ayGiQtr4nP7LmFNxHG0gHFYRhjhufnwd2wrTMJWzq7IrrfRXfbS4pZLUYV 9faAFbCcWq1WGs8xUeAb8nXd1uqZ+F+r1ZTTnJNBZNxykTHJyYbfxzkcfd9Ki5jWZ7kC mYM/xqcP+53BSa4aT22ISrvobT6wb5IE4vfIFxsL/cDaETG5Uz8cUDqEsgjg0USqLAJT nRdAivBu4SVi5woZN43Aonw6ByU8Je6eIgBpUd3LyfxJtuXFLMksN97BofvjtbbaJHJF UFag== X-Forwarded-Encrypted: i=1; AKwUvBy8CUlHngqldTq6OlYkcLz+8goyBcQGBtC1uso7Yr2EKWkyJHbF2szKItUMMFoLNkwhQm9DTgLCNaB3Daw=@vger.kernel.org X-Gm-Message-State: AFuF++lhjsVZ9daJFkTUATOtNaJYW2F48RzRGDGqtPAqCTl/C6sggoHO wZkrIuAQp5YevO6A6OW/CDp16vJhK9F+uVqMtJs8UP/Dga5NbGnQKCAF X-Gm-Gg: AYBFou3pT0yx4ZvZAhS1fXT0TNwdlPeP8/i0rO2NC4vdyCYRMIZXvxh8SRDps+2i62T TXVxcjccEyADdnosahlaqn2U3VV0U/HsTGo0B6Ss8rrkiKoh0Ke9PDeSfEwZsMauM5fTY7iaNiH Qf5twLl3E3vEAbzHEFIe2E0DYeGePf0k5C/tKJOamE/pxSjS2bRWr2Id3UG2cSYY9C98F88e6fo 3akwD1cyqq08jkR9DFXR+auaDc1gsl5P3GJgSfYXymPxhairFez880KUodK08zEo2nZii3n+d0E y9XHDpvSMrAH1QgbbfJVCUN35WhLkAKVxU8yBL+OFS6zAsr1U/R8A7BKgnIxR2XQusF5DFfs2xf m5/MYAC+u86QhRBUS4Wc9EOYsmXdF7ZShDvRTu5iONjOeuvjQYWnjK+I7u15qQyXAcSV5tlflSa ZPYqO4D3Q/h1kyaRS/rO8TbqgNryLOcMor+aq6ehXqAvLdMg954yvROJ79K+DSiaVoCokEWyG9k NWJxW/14Xt3otUzRLYJk0UwE44GAImqo5u42C4apUV8E6h1pL71vt7pYKvd076eCGBXbTN5FqPg STVX/0i89ejySm59n4hxZb8u5uD3OIOEg28= X-Received: by 2002:a17:907:7b8c:b0:c2d:fc0b:551a with SMTP id a640c23a62f3a-c2e6e7ce8femr537001966b.0.1791194153808; Mon, 05 Oct 2026 02:55:53 -0700 (PDT) Received: from dev-dsk-fgriffo-1c-93421965.eu-west-1.amazon.com (54-240-197-234.amazon.com. [54.240.197.234]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-c31562aba16sm50753266b.23.2026.10.05.02.55.52 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Oct 2026 02:55:53 -0700 (PDT) From: Fred Griffoul To: Paolo Bonzini , Sean Christopherson , Marc Zyngier , Oliver Upton , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Jason Gunthorpe , Kevin Tian Cc: David Woodhouse , Ackerley Tng , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Steffen Eiden , Catalin Marinas , Will Deacon , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H . Peter Anvin" , Joerg Roedel , Robin Murphy , Alex Williamson , Shuah Khan , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org, iommu@lists.linux.dev, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-kselftest@vger.kernel.org, linux-trace-kernel@vger.kernel.org, x86@kernel.org Subject: [RFC PATCH 0/6] KVM: guest_memfd: back guest_memfd with an imported dma-buf Date: Mon, 5 Oct 2026 09:55:46 +0000 Message-ID: <20261005095552.52748-1-griffoul@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260720111259.122911-1-dwmw2@infradead.org> References: <20260720111259.122911-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Fred Griffoul This extends David Woodhouse's "KVM: Allow alternative providers of guest_memfd backed by PFNMAP memory" RFC (v2) to let guest_memfd import a dma-buf as its page source: https://lore.kernel.org/kvm/20260720111259.122911-1-dwmw2@infradead.org/ The memory owner is a plain dma-buf exporter, linking against neither KVM nor iommufd. guest_memfd imports it for the guest (stage-2) plane and iommufd imports the same buffer for the device (DMA) plane, so one object and one invalidation protocol serve both. This is a concrete answer to the "how do we plug into iommufd without masquerading as a dma-buf" question from David's cover: the provider does not pretend to be anything; the dma-buf is the shared object. Applies on top of David's v2 series. Patches: 1 KVM: guest_memfd: Add a writable result to get_pfn() Give get_pfn() a writable out-param so a backing can map a page present but read-only (KVM_MEM_READONLY is rejected on guest_memfd slots). A guest write then exits as a memory fault. Stage-2 only; KVM's own host-mapped accesses are unaffected. 2 dma-buf: Add get_phys() to describe a physical run New exporter op: for an offset+len, return one physically contiguous backed run and an attribute word (RAM/MMIO, plus READONLY). -ENOENT for an unbacked byte, -ENODEV when revoked. Lets a page-table-building importer ask where the memory is instead of taking a scatterlist. Converts vfio-pci, the iommufd selftest exporter and the sample; iommufd behaviour is unchanged. 3 dma-buf: Add ranged mapping invalidation Invalidate a byte range rather than the whole buffer. The callback means "re-query this range" and covers loss, replacement and read-only flips. Importers without the callback still get a whole-buffer invalidation. 4 dma-buf: Allow dynamic attach without a device Allow a dynamic importer with no struct device, for a consumer (KVM) that only needs the physical layout and builds its own tables. Such an importer must not map the attachment for DMA. 5 KVM: guest_memfd: Add dma-buf backing Add GUEST_MEMFD_FLAG_USE_DMABUF and a dmabuf_fd to KVM_CREATE_GUEST_MEMFD. guest_memfd attaches as a revocable importer without a device, serves faults from get_phys() (largest aligned RAM run, read-only honoured), and zaps the range on the invalidation callback. 6 samples/kvm, selftests/kvm: Exercise dma-buf backing Turn the sample into a dma-buf exporter (a toy memory owner: one root region, a child per VM, move/donate/reclaim, absent and read-only pages, a scratch page, per-child ioctl allowlists) and move the selftests to feed one dma-buf fd to both guest_memfd and iommufd. Scope: non-confidential VMs, RAM backings only; iommufd keeps its single-range import for now. As with David's series, this is largely AI-assisted and is posted to get the interface shape reviewed. Fred Griffoul (6): KVM: guest_memfd: Add a writable result to get_pfn() dma-buf: Add get_phys() to describe a physical run dma-buf: Add ranged mapping invalidation dma-buf: Allow dynamic attach without a device KVM: guest_memfd: Add dma-buf backing samples/kvm, selftests/kvm: Exercise dma-buf backing arch/arm64/kvm/mmu.c | 13 +- arch/arm64/kvm/nested.c | 10 +- arch/x86/kvm/mmu/mmu.c | 18 +- arch/x86/kvm/svm/sev.c | 4 +- drivers/dma-buf/dma-buf.c | 92 +- drivers/iommu/iommufd/iommufd_private.h | 8 - drivers/iommu/iommufd/iommufd_test.h | 16 + drivers/iommu/iommufd/pages.c | 74 +- drivers/iommu/iommufd/selftest.c | 106 +- drivers/vfio/pci/vfio_pci_dmabuf.c | 58 +- include/linux/dma-buf.h | 70 + include/linux/kvm_host.h | 12 +- include/linux/vfio_pci_core.h | 3 - include/trace/events/dma_buf.h | 2 +- include/uapi/linux/kvm.h | 6 +- samples/kvm/gmem_provider.c | 1311 ++++++++++------- samples/kvm/gmem_provider.h | 144 +- tools/include/uapi/linux/kvm.h | 6 +- tools/testing/selftests/kvm/Makefile.kvm | 3 +- .../kvm/gmem_provider_nvme_dma_test.c | 28 +- .../testing/selftests/kvm/include/kvm_util.h | 25 +- .../testing/selftests/kvm/x86/gmem_poc_test.c | 824 +++++++++++ .../kvm/x86/gmem_provider_hugepage_test.c | 30 +- .../kvm/x86/gmem_provider_iommufd_test.c | 42 +- .../kvm/x86/gmem_provider_readonly_test.c | 172 +++ .../kvm/x86/gmem_provider_revoke_test.c | 34 +- .../selftests/kvm/x86/gmem_provider_test.c | 190 +-- .../kvm/x86/gmem_provider_vfio_test.c | 45 +- virt/kvm/Kconfig | 1 + virt/kvm/guest_memfd.c | 252 +++- 30 files changed, 2703 insertions(+), 896 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86/gmem_poc_test.c create mode 100644 tools/testing/selftests/kvm/x86/gmem_provider_readonly_test.c -- 2.47.3