On Mon, Mar 18, 2024, Vishal Annapurve wrote: > On Mon, Mar 18, 2024 at 3:02 PM David Hildenbrand <david@xxxxxxxxxx> wrote: > > Second, we should find better ways to let an IOMMU map these pages, > > *not* using GUP. There were already discussions on providing a similar > > fd+offset-style interface instead. GUP really sounds like the wrong > > approach here. Maybe we should look into passing not only guest_memfd, > > but also "ordinary" memfds. +1. I am not completely opposed to letting SNP and TDX effectively convert pages between private and shared, but I also completely agree that letting anything gup() guest_memfd memory is likely to end in tears. > I need to dig into past discussions around this, but agree that > passing guest memfds to VFIO drivers in addition to HVAs seems worth > exploring. This may be required anyways for devices supporting TDX > connect [1]. > > If we are talking about the same file catering to both private and > shared memory, there has to be some way to keep track of references on > the shared memory from both host userspace and IOMMU. > > > > > Third, I don't think we should be using huge pages where huge pages > > don't make any sense. Using a 1 GiB page so the VM will convert some > > pieces to map it using PTEs will destroy the whole purpose of using 1 > > GiB pages. It doesn't make any sense. I don't disagree, but the fundamental problem is that we have no guarantees as to what that guest will or will not do. We can certainly make very educated guesses, and probably be right 99.99% of the time, but being wrong 0.01% of the time probably means a lot of broken VMs, and a lot of unhappy customers. > I had started a discussion for this [2] using an RFC series. David is talking about the host side of things, AFAICT you're talking about the guest side... > challenge here remain: > 1) Unifying all the conversions under one layer > 2) Ensuring shared memory allocations are huge page aligned at boot > time and runtime. > > Using any kind of unified shared memory allocator (today this part is > played by SWIOTLB) will need to support huge page aligned dynamic > increments, which can be only guaranteed by carving out enough memory > at boot time for CMA area and using CMA area for allocation at > runtime. > - Since it's hard to come up with a maximum amount of shared memory > needed by VM, especially with GPUs/TPUs around, it's difficult to come > up with CMA area size at boot time. ...which is very relevant as carving out memory in the guest is nigh impossible, but carving out memory in the host for systems whose sole purpose is to run VMs is very doable. > I think it's arguable that even if a VM converts 10 % of its memory to > shared using 4k granularity, we still have fewer page table walks on > the rest of the memory when using 1G/2M pages, which is a significant > portion. Performance is a secondary concern. If this were _just_ about guest performance, I would unequivocally side with David: the guest gets to keep the pieces if it fragments a 1GiB page. The main problem we're trying to solve is that we want to provision a host such that the host can serve 1GiB pages for non-CoCo VMs, and can also simultaneously run CoCo VMs, with 100% fungibility. I.e. a host could run 100% non-CoCo VMs, 100% CoCo VMs, or more likely, some sliding mix of the two. Ideally, CoCo VMs would also get the benefits of 1GiB mappings, that's not the driving motiviation for this discussion. As HugeTLB exists today, supporting that use case isn't really feasible because there's no sane way to convert/free just a sliver of a 1GiB page (and reconstitute the 1GiB when the sliver is converted/freed back). Peeking ahead at my next comment, I don't think that solving this in the guest is a realistic option, i.e. IMO, we need to figure out a way to handle this in the host, without relying on the guest to cooperate. Luckily, we haven't added hugepage support of any kind to guest_memfd, i.e. we have a fairly blank slate to work with. The other big advantage that we should lean into is that we can make assumptions about guest_memfd usage that would never fly for a general purpose backing stores, e.g. creating a dedicated memory pool for guest_memfd is acceptable, if not desirable, for (almost?) all of the CoCo use cases. I don't have any concrete ideas at this time, but my gut feeling is that this won't be _that_ crazy hard to solve if commit hard to guest_memfd _not_ being general purposes, and if we we account for conversion scenarios when designing hugepage support for guest_memfd. > > For example, one could create a GPA layout where some regions are backed > > by gigantic pages that cannot be converted/can only be converted as a > > whole, and some are backed by 4k pages that can be converted back and > > forth. We'd use multiple guest_memfds for that. I recall that physically > > restricting such conversions/locations (e.g., for bounce buffers) in > > Linux was already discussed somewhere, but I don't recall the details. > > > > It's all not trivial and not easy to get "clean". > > Yeah, agree with this point, it's difficult to get a clean solution > here, but the host side solution might be easier to deploy (not > necessarily easier to implement) and possibly cleaner than attempts to > regulate the guest side. I think we missed the opportunity to regulate the guest side by several years. To be able to rely on such a scheme, e.g. to deploy at scale and not DoS customer VMs, KVM would need to be able to _enforce_ the scheme. And while I am more than willing to put my foot down on things where the guest is being blatantly ridiculous, wanting to convert an arbitrary 4KiB chunk of memory between private and shared isn't ridiculous (likely inefficient, but not ridiculous). I.e. I'm not willing to have KVM refuse conversions that are legal according to the SNP and TDX specs (and presumably the CCA spec, too). That's why I think we're years too late; this sort of restriction needs to go in the "hardware" spec, and that ship has sailed.