From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9BD2F605C4 for ; Fri, 22 Mar 2024 17:52:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711129943; cv=none; b=Vaj2BawqrTqvNwRWRTNO03/pSL62FwdoZBHVEZOrYEPLqi54gbT9FZ8AdHT2ZGUNZ1fQ+XNmj8cSAk6n4IozkxKlpK/4d4NPQ44wzf63w4nIy5ruL9TWkWkVL/L8WnGWZZyzwDcWD0AWpe3ezbM2gPpSp8V7o+sRG63yOHiw7uU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711129943; c=relaxed/simple; bh=Ps/2zmED4V/4Mugj19V5O+VRXCAuSuCB45zhIkms8Fs=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=Ot15ZkMeE4zGyL6RGdOUQAaex/djUwyHKhh3MRA99PyHRSpS/TaElARNQzAj/etZv/HDoO3/YHD7zanofFvjm51igWeN/4Z15KF5oAa85w/gDofOMbaBWBb7WWIzoYyTWFS53XiVE63Dt3ZvRU50aa4Jb5K6yoQre8s27IsqnlU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=R1HW8Qqi; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="R1HW8Qqi" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711129940; h=from:from: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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:autocrypt:autocrypt; bh=Gb9Qa2HC9PUpp7JrzETLi4Q7+02gJGK5TZHjydmjtS0=; b=R1HW8QqiOukJmQSguZbsa5hxS5q18/FhC1o7aDlr8/Ak66/fsSN8VL/P7tvrUKQSuvQXfd +z2Sw3h0yIUzKELgFWURO03s9CTnLX1ClX9GsaZpHDfY5tEtr49VMy1JGrWvmb2ovNKbRN Rt7aYUCTcc5BFWGMDTk4njUB/yDWLDs= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-167-ZuO839wINoGbMbASADD7Ig-1; Fri, 22 Mar 2024 13:52:19 -0400 X-MC-Unique: ZuO839wINoGbMbASADD7Ig-1 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-33ed71800f2so1236998f8f.2 for ; Fri, 22 Mar 2024 10:52:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711129938; x=1711734738; h=content-transfer-encoding:in-reply-to:organization:autocrypt:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Gb9Qa2HC9PUpp7JrzETLi4Q7+02gJGK5TZHjydmjtS0=; b=Ldr6j/BDAdOe6zjJ/6T9bNah7SreNxO7KFApIA/O4/g69gyrS7K5p+cift2lj5sUEa 7L7mwTgCdOHDGK7IF3XRpPrjenpEd9f1iVgsFInxQhY6E2YQi7SggO7kedDIwM08OREl eyUBXfRgT6/Obyqu16uXlNfzAcZlYdoYY1lz1tEcDf2iDyd3TB+xTT8mvhXE3NxUh8S1 xpPX8lT6v3VOfXrt2OiY6g45G61NqG0+L5UwAxAY6YOXcc4XdIfB7GkXcQtRONbxpiQF 6bTYy6VIfaVuXLzJ/6X5PaBUCyte6DiLIKcxjRgUFfgJNInW/gVBkrS8BNL2IhzUk3yI YTxQ== X-Forwarded-Encrypted: i=1; AJvYcCUSnRvSp5iMlhn7egb1ov0k6jK+bIb5+LL7M5y62ImLQ9SYI5K/XEAPfvuB1GACVIely2EM41f4LElbquxWFKNTdr79yQ2S X-Gm-Message-State: AOJu0YzDCajm/psnTGDaU1A3Eti2T0Yy6X1t0BgVNTwOIpOaYuSYMEM0 Vds1pdMN7IvAlNtgtH6S9RGP7wVCeunhZ9es/nhPh8VlhR1FB97H2hbOgmwus6W+UjWLW43fVOH xQTRzMQhAG/u6Om+sh0/HD76W9HJWkbO63JdT8x+wbBYxtbrPRSoaMO3nBMfvCg== X-Received: by 2002:adf:e7c5:0:b0:33e:7b2d:7419 with SMTP id e5-20020adfe7c5000000b0033e7b2d7419mr70066wrn.15.1711129937967; Fri, 22 Mar 2024 10:52:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFNYbeWlc5wQeQXdFbbaLRwiJJvNBai7rukIfba3St2pQT4smTvMpf+YFXpFMLEBtq2T62Osg== X-Received: by 2002:adf:e7c5:0:b0:33e:7b2d:7419 with SMTP id e5-20020adfe7c5000000b0033e7b2d7419mr70003wrn.15.1711129937431; Fri, 22 Mar 2024 10:52:17 -0700 (PDT) Received: from ?IPV6:2003:cb:c71b:7e00:9339:4017:7111:82d0? (p200300cbc71b7e0093394017711182d0.dip0.t-ipconnect.de. [2003:cb:c71b:7e00:9339:4017:7111:82d0]) by smtp.gmail.com with ESMTPSA id x16-20020a5d6b50000000b0033e93e00f68sm2544543wrw.61.2024.03.22.10.52.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 22 Mar 2024 10:52:16 -0700 (PDT) Message-ID: <2d6fc3c0-a55b-4316-90b8-deabb065d007@redhat.com> Date: Fri, 22 Mar 2024 18:52:14 +0100 Precedence: bulk X-Mailing-List: kvmarm@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: folio_mmapped To: Will Deacon Cc: Sean Christopherson , Vishal Annapurve , Quentin Perret , Matthew Wilcox , Fuad Tabba , kvm@vger.kernel.org, kvmarm@lists.linux.dev, pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, viro@zeniv.linux.org.uk, brauner@kernel.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, ackerleytng@google.com, mail@maciej.szmigiero.name, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, keirf@google.com, linux-mm@kvack.org References: <7470390a-5a97-475d-aaad-0f6dfb3d26ea@redhat.com> <40f82a61-39b0-4dda-ac32-a7b5da2a31e8@redhat.com> <20240319143119.GA2736@willie-the-truck> From: David Hildenbrand Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiH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usdqx8nPEpfOqCtbbwJMATbHyqLt7/oz/5deGuwxgb65pWIzufa N7eop7uh+6bezi+rugUI+w6DABEBAAHCwXwEGAEIACYCGwwWIQQb2cqtc1xMOkYN/MpN3hD3 AP+DWgUCXw7HsgUJEqkpoQAKCRBN3hD3AP+DWrrpD/4qS3dyVRxDcDHIlmguXjC1Q5tZTwNB boaBTPHSy/Nksu0eY7x6HfQJ3xajVH32Ms6t1trDQmPx2iP5+7iDsb7OKAb5eOS8h+BEBDeq 3ecsQDv0fFJOA9ag5O3LLNk+3x3q7e0uo06XMaY7UHS341ozXUUI7wC7iKfoUTv03iO9El5f XpNMx/YrIMduZ2+nd9Di7o5+KIwlb2mAB9sTNHdMrXesX8eBL6T9b+MZJk+mZuPxKNVfEQMQ a5SxUEADIPQTPNvBewdeI80yeOCrN+Zzwy/Mrx9EPeu59Y5vSJOx/z6OUImD/GhX7Xvkt3kq Er5KTrJz3++B6SH9pum9PuoE/k+nntJkNMmQpR4MCBaV/J9gIOPGodDKnjdng+mXliF3Ptu6 3oxc2RCyGzTlxyMwuc2U5Q7KtUNTdDe8T0uE+9b8BLMVQDDfJjqY0VVqSUwImzTDLX9S4g/8 kC4HRcclk8hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: <20240319143119.GA2736@willie-the-truck> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 19.03.24 15:31, Will Deacon wrote: > Hi David, Hi Will, sorry for the late reply! > > On Tue, Mar 19, 2024 at 11:26:05AM +0100, David Hildenbrand wrote: >> On 19.03.24 01:10, Sean Christopherson wrote: >>> On Mon, Mar 18, 2024, Vishal Annapurve wrote: >>>> On Mon, Mar 18, 2024 at 3:02 PM David Hildenbrand 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. >> >> Yes. Avoid it right from the start, if possible. >> >> People wanted guest_memfd to *not* have to mmap guest memory ("even for >> ordinary VMs"). Now people are saying we have to be able to mmap it in order >> to GUP it. It's getting tiring, really. > > From the pKVM side, we're working on guest_memfd primarily to avoid > diverging from what other CoCo solutions end up using, but if it gets > de-featured (e.g. no huge pages, no GUP, no mmap) compared to what we do > today with anonymous memory, then it's a really hard sell to switch over > from what we have in production. We're also hoping that, over time, > guest_memfd will become more closely integrated with the mm subsystem to > enable things like hypervisor-assisted page migration, which we would > love to have. Reading Sean's reply, he has a different view on that. And I think that's the main issue: there are too many different use cases and too many different requirements that could turn guest_memfd into something that maybe it really shouldn't be. > > Today, we use the existing KVM interfaces (i.e. based on anonymous > memory) and it mostly works with the one significant exception that > accessing private memory via a GUP pin will crash the host kernel. If > all guest_memfd() can offer to solve that problem is preventing GUP > altogether, then I'd sooner just add that same restriction to what we > currently have instead of overhauling the user ABI in favour of > something which offers us very little in return. > > On the mmap() side of things for guest_memfd, a simpler option for us > than what has currently been proposed might be to enforce that the VMM > has unmapped all private pages on vCPU run, failing the ioctl if that's > not the case. It needs a little more tracking in guest_memfd but I think > GUP will then fall out in the wash because only shared pages will be > mapped by userspace and so GUP will fail by construction for private > pages. > > We're happy to pursue alternative approaches using anonymous memory if > you'd prefer to keep guest_memfd limited in functionality (e.g. > preventing GUP of private pages by extending mapping_flags as per [1]), > but we're equally willing to contribute to guest_memfd if extensions are > welcome. > > What do you prefer? Let me summarize the history: AMD had its thing running and it worked for them (but I recall it was hacky :) ). TDX made it possible to crash the machine when accessing secure memory from user space (MCE). So secure memory must not be mapped into user space -- no page tables. Prototypes with anonymous memory existed (and I didn't hate them, although hacky), but one of the other selling points of guest_memfd was that we could create VMs that wouldn't need any page tables at all, which I found interesting. There was a bit more to that (easier conversion, avoiding GUP, specifying on allocation that the memory was unmovable ...), but I'll get to that later. The design principle was: nasty private memory (unmovable, unswappable, inaccessible, un-GUPable) is allocated from guest_memfd, ordinary "shared" memory is allocated from an ordinary memfd. This makes sense: shared memory is neither nasty nor special. You can migrate it, swap it out, map it into page tables, GUP it, ... without any issues. So if I would describe some key characteristics of guest_memfd as of today, it would probably be: 1) Memory is unmovable and unswappable. Right from the beginning, it is allocated as unmovable (e.g., not placed on ZONE_MOVABLE, CMA, ...). 2) Memory is inaccessible. It cannot be read from user space, the kernel, it cannot be GUP'ed ... only some mechanisms might end up touching that memory (e.g., hibernation, /proc/kcore) might end up touching it "by accident", and we usually can handle these cases. 3) Memory can be discarded in page granularity. There should be no cases where you cannot discard memory to over-allocate memory for private pages that have been replaced by shared pages otherwise. 4) Page tables are not required (well, it's an memfd), and the fd could in theory be passed to other processes. Having "ordinary shared" memory in there implies that 1) and 2) will have to be adjusted for them, which kind-of turns it "partially" into ordinary shmem again. Going back to the beginning: with pKVM, we likely want the following 1) Convert pages private<->shared in-place 2) Stop user space + kernel from accessing private memory in process context. Likely for pKVM we would only crash the process, which would be acceptable. 3) Prevent GUP to private memory. Otherwise we could crash the kernel. 4) Prevent private pages from swapout+migration until supported. I suspect your current solution with anonymous memory gets all but 3) sorted out, correct? I'm curious, may there be a requirement in the future that shared memory could be mapped into other processes? (thinking vhost-user and such things). Of course that's impossible with anonymous memory; teaching shmem to contain private memory would kind-of lead to ... guest_memfd, just that we don't have shared memory there. -- Cheers, David / dhildenb