From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C71A1C77B73 for ; Wed, 31 May 2023 11:36:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 403148E0001; Wed, 31 May 2023 07:36:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3B3BC6B0074; Wed, 31 May 2023 07:36:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 27AED8E0001; Wed, 31 May 2023 07:36:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 1112B6B0072 for ; Wed, 31 May 2023 07:36:07 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D2A4B120369 for ; Wed, 31 May 2023 11:36:06 +0000 (UTC) X-FDA: 80850346332.20.9A1C08C Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf28.hostedemail.com (Postfix) with ESMTP id E9D6DC0008 for ; Wed, 31 May 2023 11:36:04 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=none; spf=pass (imf28.hostedemail.com: domain of cmarinas@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cmarinas@kernel.org; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=arm.com (policy=none) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1685532965; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=A5Hjk6/gmnafI1ufkCYmWcUnqyb4i/TmIxGg7NstbJM=; b=wSDa9crqxXd5sB2rk6t2c6OZe8STxPR0OTofiF118eHhr4Brmdfor4k1UzqxrWflRkHyi8 7DgizpDQ7PMnDdmWJsA+o4qgK5faWTR8PMR9AKv/ObnPzq7hkAZL2yBOhvpRgtn9BRReb3 jUlaEt2041Re72ABL0FPrbIFNUul7Nw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685532965; a=rsa-sha256; cv=none; b=Wod7duQlFkSApoOx94z1/0Oi7LBGsJDfvtirWyTN3o8FnjVF7Um2BL17WboislD7JAXVqb 79/Ru5EGh6tDcj4fi75Ja6fs/UaFnEX15fZQFlwmHzMpPrB5vvDkP4X7/rS18PKcg/rm03 9wQfVWEbqwCotsAGWy8wyfLeAXXXHLE= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=none; spf=pass (imf28.hostedemail.com: domain of cmarinas@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cmarinas@kernel.org; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=arm.com (policy=none) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C9C8E639EC; Wed, 31 May 2023 11:36:03 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EEED4C433EF; Wed, 31 May 2023 11:35:59 +0000 (UTC) Date: Wed, 31 May 2023 12:35:57 +0100 From: Catalin Marinas To: Jason Gunthorpe Cc: Marc Zyngier , ankita@nvidia.com, alex.williamson@redhat.com, naoya.horiguchi@nec.com, oliver.upton@linux.dev, aniketa@nvidia.com, cjia@nvidia.com, kwankhede@nvidia.com, targupta@nvidia.com, vsethi@nvidia.com, acurrid@nvidia.com, apopple@nvidia.com, jhubbard@nvidia.com, danw@nvidia.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, Lorenzo Pieralisi Subject: Re: [PATCH v3 1/6] kvm: determine memory type from VMA Message-ID: References: <20230405180134.16932-1-ankita@nvidia.com> <20230405180134.16932-2-ankita@nvidia.com> <86r0spl18x.wl-maz@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Queue-Id: E9D6DC0008 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: w3kd4s6z55upgen85qmmomyw14ytgmhm X-HE-Tag: 1685532964-277479 X-HE-Meta: U2FsdGVkX1+Ifa5bRTX1nnUl6CD9UANUN2+BMSP9NR2trdheg7uKQ7MDovm6QMbvt7hkJxjNAgoLPJIibCCTGX9mehi7ozeX2r66hvQPDnfnQV/mN/iGLzaHhBLS/vsetecDooS6t8yUom9VB8gjG/eUJa08ZubmjcPuc1/g2Q4LnoO34IbPAkT27S7zzZ6M/mkuHWpi2hrhbC8ngCrA/wR9Wpdd1Tez0N1lqj0aSaVwuAsK2hsALTrcqwdefxuyUd/Wc4NKIx3n8699RDnq07VwsuMwRRUB88t37E6IuDkNdBr17aizEDdta8s5cWezLvyuRe5hYmv5N8T6+7nY+kc23BsayP/mfFMazrIVmW/fe0IFZKS7OCaBDQbGxW18CUohL54Ns7GpPkEm4ZQ/YHYBM0wLRCCjHHtYZ4k7DRBEu31GiOMrl6m2b6eY0GPURc5eStnyQkM9btWkD0IXgJTU1Jq8ZetvffcxF6jNGoDo+1mJE1PV2vODduyNOIGflaDI0h6F2N+GkHnsDSFFZMFcM+YLiy6CY3ssid+g5ZstulbVIzYG0O35pVGfRrpbqXOg7+LRBMfU2qOKOx2UidIEY107W6Tx5CZ+CyTauc2xpEsAq/tp5NtFZ0+tAO2UJ89Fe371Bv1TBk+a9VQt+RMpwkDgVoouKCg3tGBo7qJ8tytS/aXDzHvBZ7oRpf+JAynS0KpZF1/z6yQZ4zPqvnngse6GdTeePMGIbHKnC+HrHoIwnJf2YTWsPFTmqEqyMZG37YbYpIAy2i2QtYXIG4w74sYoTgFV+5UCI646fD0WObGaWjys6Tr2ifDSj8YTbYvH0JeAmfXOfQ4dYubo0rfBcfxJd8AdEmKHKa/Zkpmiuy/aHgvW1Jk5JSmXS/VMm6Vf1P5Vo037ZfKLni2sUy0XHDPP/Wo3oOJ3xv7pJYxmDS3jBtXCb8p5sEApIcWgosSB15EagEdQa+llg1/ SONgYCtT 4t0XCZnnwp8i4FhOYGUNRdh5gEOAmVfPBA+YbM8XexvQZUNfStRFPl8gzgsLeayBkyrL5LCoHJ3QebFBFKbGZMuK0XG9SPwB++VDWFqmlMqcdYkXqrjz4rE00J2+dSkVZNyBa5wZ/xe0dIVheoyWfaJEKZd5wbb47LaYmTMy+7MZPiHtv2m+IHHnqAFZz02Grhi8qbqMlsWMWZ+PIEH+MWhK2G8VYkIbjoI7uQTxWp/utldcR1m4XnKCgbXK7X4dsQ5EsPOCLPOuRKjy7LxuS/BkmOcbD8RQ6xIG1Y0vpwFmvMV2hI4vBzz0vhuynrlYf5flJm13W1PJzXuUdsq+aQ+5iarOlYb3J4tF5ggH6MFK/EonNUda2g9DTJ6tfkGQqwp8e/2KW3J2zMOLJni5UMnK8QCapDzF6OiIvY92f0MoMd3E= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Wed, Apr 12, 2023 at 10:01:33AM -0300, Jason Gunthorpe wrote: > On Wed, Apr 12, 2023 at 01:43:26PM +0100, Marc Zyngier wrote: > > What makes it safe? How does VFIO ensures that the memory type used is > > correct in all circumstances? This has to hold for *ANY* device, not > > just your favourite toy of the day. Nothing in this patch is horribly > > wrong, but the above question must be answered before we can consider > > any of this. > > In VFIO we now have the concept of "variant drivers" which work with > specific PCI IDs. The variant drivers can inject device specific > knowledge into VFIO. In this series the driver injects the cachable > pgprot when it creates some of the VMAs because it knows the PCI IDs > it supports, parses the ACPI description, and knows for sure that the > memory it puts in the cachable VMA is linked with a cache coherent > interconnect. > > The generic vfio-pci path is not changed, so 'any device' is not > relevant here. There were several off-list discussions, I'm trying to summarise my understanding here. This series aims to relax the VFIO mapping to cacheable and have KVM map it into the guest with the same attributes. Somewhat related past threads also tried to relax the KVM device pass-through mapping from Device_nGnRnE (pgprot_noncached) to Normal_NC (pgprot_writecombine). Those were initially using the PCIe prefetchable BAR attribute but that's not a reliable means to infer whether Normal vs Device is safe. Anyway, I think we'd need to unify these threads and come up with some common handling that can cater for various attributes required by devices/drivers. Therefore replying in this thread. Current status on arm64: vfio-pci user mapping (Qemu) of PCIe BARs is Device_nGnRnE. KVM also maps it at Stage 2 with the same attributes. The user mapping is fine since the VMM may not have detailed knowledge about how to safely map the BAR. KVM doesn't have such detailed knowledge either in the device pass-through case but for safety reasons it follows the same attributes as the user mapping. >From a safety perspective, relaxing the KVM stage 2 mapping has some implications: 1. It creates multiple memory aliases with different attributes. 2. Speculative loads, unaligned accesses. 3. Potentially new uncontained errors introduced by Normal NC vs Device mappings. >From the private discussions I had regarding point (3), Device doesn't really make any difference and it can be even worse. For example, Normal mappings would allow a contained error while Device would trigger an uncontained SError. So the only safe bet here is for the platform, root complex to behave properly when device pass-through is involved (e.g. PCIe to ignore writes, return 0xff on reads if there are errors). That's something Arm is working on to require in the BSA specs (base system architecture). Presumably cloud vendors allowing device pass-through already know their system well enough, no need for further policing in KVM (which it can't do properly anyway). As long as the errors are contained, point (2) becomes the responsibility of the guest, given its detailed knowledge of the device, using the appropriate attributes (whether x86 WC maps exactly onto arm64 Normal NC is the subject of a separate discussion; so far that's the assumption we took in the arm64 kernel). Regarding vfio-pci, the user mapping must remain Device_nGnRnE on arm64 to avoid unexpected speculative loads. This leaves us with (1) and since the vfio-pci user mapping must remain Device, there's a potential mismatched alias if the guest maps the corresponding BAR as Normal NC. Luckily the Arm ARM constrains the hardware behaviour here to basically allowing the Device mapping in the VMM to behave somewhat the Normal NC mapping in the guest. IOW, the VMM loses the Device guarantees (non-speculation, ordering). That's not a problem for the device since the guest already deemed such mapping to be safe. While I think it's less likely for the VMM to access the same BAR that was mapped into the guest, if we want to be on the safe side from an ABI perspective (the returned mmap() now has different memory guarantees), we could make this an opt-in. That's pretty much the VMM stating that it is ok with losing some of the Device guarantees for the vfio-pci mapping. A question here is whether we want to this to be done at the vfio-pci level, the KVM memory slot or a big knob per VMM. Or whether we need to bother with this at all (if it's just theoretical, maybe we can have a global opt-out). Going back to this series, allowing Cacheable mapping in KVM has similar implications as above. (2) and (3) would be assumed safe by the platform vendors. Regarding (1), to avoid confusion, I would only allow it if FWB (force write-back) is present so that KVM can enforce a cacheable mapping at Stage 2 if the vfio variant driver also maps it as cacheable in user space. There were some other thoughts on only allowing different attributes in KVM if the user counterpart does not have any mapping (e.g. fd-based KVM memory slots). However, this won't work well with CXL-attached memory for example where the VMM may want access to it (e.g. virtio). So I wouldn't spend more thoughts on this. The TL;DR summary of what I think we should do: a) Relax the KVM Stage 2 mapping for vfio-pci to Normal NC (pgprot_writecombine). TBD whether we need a VMM opt-in is at the vfio-pci level, KVM slot or per-VMM level. Another option is to do this by default and have a global opt-out (cmdline). I don't think the latter is such a bad idea since I find it unlikely for the VMM to also touch the same PCIe BAR _and_ expect different memory ordering guarantees than the guest device driver. b) Map the KVM stage 2 mapping as cacheable+FWB iff the VMM has a corresponding cacheable mapping. Thoughts? -- Catalin