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 F3C011624E0 for ; Wed, 26 Feb 2025 15:30:26 +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=1740583829; cv=none; b=YBN2sczOeYvGpw4DeFyXzgxtFgnbP5tdKltXSFokxTKeDa4LOLBJzDWiI5m9z3TXo1D1xVdcByhA0iF63PYC/LRwnZCnIZuJx4tIbx7ZYeKaTyzI7Gmv07pjxhjg5QOBCxy6pBTZ+CcgBN9lJL49nmNNJ06nnAHRLrUgCIJLhQY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740583829; c=relaxed/simple; bh=/wpDNLjk/fNjvfimoUDJdbZDLqeC6MxKiLnD31fpHaI=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=gYFLQr1S1l4G9178DcXMvYhmFL2G1337bK3ZutvF0OgCWhSKra4m1+oli4K/9TCQJgV+eZotJBaYpZAOMvEFTzZvGb4xwFPcnlzP6nkbgskg3UYkx03J0N7p7n0QelRnimvdVE9Gv7mqK3sXKSNHz9PTM81q3UTLJmTLbWh1WHQ= 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=edqRUQV0; 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="edqRUQV0" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740583826; 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=gVt0qzRRcwLYzm8lqhyi37nC1mPHa9gt3lqzImxJBfw=; b=edqRUQV0AeD27IJ2kVini1s+8CP49iUPft5w9AJtdMZ8n0zWz94qer80+gNWBDe5v7DGI5 2d2Vx39j2izDgr0x+1ifjN74+cKRvTzN8yCH+eqKfjN042mIBYP1pLbbXX74Lb0KwrWqVf aumwsnXWpnJ9/fzntLsl0BZ6ptxYsjc= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-513-7ruVLePNNPy5OFtrzWVmrg-1; Wed, 26 Feb 2025 10:30:24 -0500 X-MC-Unique: 7ruVLePNNPy5OFtrzWVmrg-1 X-Mimecast-MFC-AGG-ID: 7ruVLePNNPy5OFtrzWVmrg_1740583823 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-43935e09897so47508145e9.1 for ; Wed, 26 Feb 2025 07:30:24 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740583823; x=1741188623; h=content-transfer-encoding:in-reply-to:organization:autocrypt :content-language:from:references:cc:to:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=gVt0qzRRcwLYzm8lqhyi37nC1mPHa9gt3lqzImxJBfw=; b=rTJ5YQabQu1+sMEtFesif0gyMSgbknlwhKebk+t6UWWiEqym20XKcjIiy5YMvHGIZ5 R4VHtagrKyLozJnmJh7JdrUH2Jc8NfgdV4paEgBWj+CkGrW3ts6VV24EwI+2QCtxwC91 XdQHoR6pPxy3zFmcwgTpMFDZUt/hw/YBs2feOJgT+kJ2koTxQMOEMrDATuWP41Oyhkxd rR4d10e+gDrzNXEsdmSgoP8XxglbruAHqgbVf/s7iOSZZuKdmZbOCGZRVbzrV2A7Un+s JZp5QQXzn5Lds8sHVIX56JNSTw856DgcuE6E5lGwZ4UoOv1ZHtE0fFr/GYb0JECRAHyl L9Yw== X-Forwarded-Encrypted: i=1; AJvYcCWIdPQTZcaP44ULUh991zsN+NDF7tkbGzmDTcQOoI4KCywz/o0iAo/OBQIj0genFICnsQmCPs7Rpsw=@vger.kernel.org X-Gm-Message-State: AOJu0Yz2pOwfZQYxvkhFt/Y0f2tafQL8LRpIY0hQZSaORUIw1hUhnUvO kCT3FuXjmYh8iTTKf660dD3GH98kBDPJyKbiuOJsShenZRxxX1hCKofAq/gMHE3UHpcn6bQdwX5 zrWLVA1SzaQTzSbEzfcvqirzaZ3f2E/anZ5qGVplmsUUlS07Vzgg8oZqy1Q== X-Gm-Gg: ASbGnctPqTH+OjUO5mlKx9FRwMikXNG0RzZ9GZwUAZNBCBhIcTKKfXJjg5P5NJbALam LT6ZnWfzNEsBY7tpBofdDTidJaApFF4YRM52JDncwlqLafAgSmjpQf0OCXtqHauDThGqVgr/lxa ydZLifzxtTwVKmMde4qAwN16RRm8r74qP7Dvq4M/9V1FvLGkdllrssl6Z1Xj3CkB/IR4NCM7EJS /+1RXaRG8GAd3UB5uFZg4wztC1sCf7aEQzlZzs66EC/rOss6aaFm2xubwgfZ9wZhcXabHvVpcsv 3FL2IMifTBGOsWsL6+8C12JV7QkhUVXiAM4C9qhGrd2qnMBOndcO6yZ4UZH9fAsp41qk0KWFtOu QEzJ0OG8FzebLH8ulWAPGAuIYn0aqYf17sgBvbOAx/Xo= X-Received: by 2002:a05:600c:3b13:b0:439:8bc3:a697 with SMTP id 5b1f17b1804b1-43ab8fd1f46mr35729545e9.4.1740583823428; Wed, 26 Feb 2025 07:30:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IHDQ1Rg5mJ7gASlDo38k12X1agotgAvbBBn2p1lMp4aSsVrgnQa+7aXZITK3QN/pmv/rHAjsw== X-Received: by 2002:a05:600c:3b13:b0:439:8bc3:a697 with SMTP id 5b1f17b1804b1-43ab8fd1f46mr35729205e9.4.1740583822963; Wed, 26 Feb 2025 07:30:22 -0800 (PST) Received: from ?IPV6:2003:cb:c747:ff00:9d85:4afb:a7df:6c45? (p200300cbc747ff009d854afba7df6c45.dip0.t-ipconnect.de. [2003:cb:c747:ff00:9d85:4afb:a7df:6c45]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43aba52bc54sm26843285e9.5.2025.02.26.07.30.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 26 Feb 2025 07:30:22 -0800 (PST) Message-ID: <9ffce724-23c9-4aa1-bc53-8292e1029991@redhat.com> Date: Wed, 26 Feb 2025 16:30:20 +0100 Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4 03/12] KVM: guest_memfd: Add flag to remove from direct map To: Patrick Roy , rppt@kernel.org, seanjc@google.com Cc: pbonzini@redhat.com, corbet@lwn.net, willy@infradead.org, akpm@linux-foundation.org, song@kernel.org, jolsa@kernel.org, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, eddyz87@gmail.com, yonghong.song@linux.dev, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@fomichev.me, haoluo@google.com, Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, vbabka@suse.cz, jannh@google.com, shuah@kernel.org, kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org, tabba@google.com, jgowans@amazon.com, graf@amazon.com, kalyazin@amazon.com, xmarcalx@amazon.com, derekmn@amazon.com, jthoughton@google.com, Elliot Berman References: <20250221160728.1584559-1-roypat@amazon.co.uk> <20250221160728.1584559-4-roypat@amazon.co.uk> <8642de57-553a-47ec-81af-803280a360ec@amazon.co.uk> <7f38018b-dc89-4d79-a309-149557796121@amazon.co.uk> From: David Hildenbrand Content-Language: en-US Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/Y91XuSBdz0uOyU/S8kM1+ag0wvsGlpBVxRR/xw/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+SYhmhpDrti9Fq1EsmhiHSkxJcGREoMK/63r9WLZYI3+4W2rAc UucZa4OT27U5ZISjNg3Ev0rxU5UH2/pT4wJCfxwocmqaRr6UYmrtZmND89X0KigoFD/XSeVv jwBRNjPAubK9/k5NoRrYqztM9W6sJqrH8+UWZ1Idd/DdmogJh0gNC0+N42Za9yBRURfIdKSb B3JfpUqcWwE7vUaYrHG1nw54pLUoPG6sAA7Mehl3nd4pZUALHwARAQABzSREYXZpZCBIaWxk ZW5icmFuZCA8ZGF2aWRAcmVkaGF0LmNvbT7CwZgEEwEIAEICGwMGCwkIBwMCBhUIAgkKCwQW AgMBAh4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Aw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: <7f38018b-dc89-4d79-a309-149557796121@amazon.co.uk> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 26.02.25 16:14, Patrick Roy wrote: > > > On Wed, 2025-02-26 at 09:08 +0000, David Hildenbrand wrote: >> On 26.02.25 09:48, Patrick Roy wrote: >>> >>> >>> On Tue, 2025-02-25 at 16:54 +0000, David Hildenbrand wrote:> On 21.02.25 17:07, Patrick Roy wrote: >>>>> Add KVM_GMEM_NO_DIRECT_MAP flag for KVM_CREATE_GUEST_MEMFD() ioctl. When >>>>> set, guest_memfd folios will be removed from the direct map after >>>>> preparation, with direct map entries only restored when the folios are >>>>> freed. >>>>> >>>>> To ensure these folios do not end up in places where the kernel cannot >>>>> deal with them, set AS_NO_DIRECT_MAP on the guest_memfd's struct >>>>> address_space if KVM_GMEM_NO_DIRECT_MAP is requested. >>>>> >>>>> Note that this flag causes removal of direct map entries for all >>>>> guest_memfd folios independent of whether they are "shared" or "private" >>>>> (although current guest_memfd only supports either all folios in the >>>>> "shared" state, or all folios in the "private" state if >>>>> !IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM)). The usecase for removing >>>>> direct map entries of also the shared parts of guest_memfd are a special >>>>> type of non-CoCo VM where, host userspace is trusted to have access to >>>>> all of guest memory, but where Spectre-style transient execution attacks >>>>> through the host kernel's direct map should still be mitigated. >>>>> >>>>> Note that KVM retains access to guest memory via userspace >>>>> mappings of guest_memfd, which are reflected back into KVM's memslots >>>>> via userspace_addr. This is needed for things like MMIO emulation on >>>>> x86_64 to work. Previous iterations attempted to instead have KVM >>>>> temporarily restore direct map entries whenever such an access to guest >>>>> memory was needed, but this turned out to have a significant performance >>>>> impact, as well as additional complexity due to needing to refcount >>>>> direct map reinsertion operations and making them play nicely with gmem >>>>> truncations. >>>>> >>>>> This iteration also doesn't have KVM perform TLB flushes after direct >>>>> map manipulations. This is because TLB flushes resulted in a up to 40x >>>>> elongation of page faults in guest_memfd (scaling with the number of CPU >>>>> cores), or a 5x elongation of memory population. On the one hand, TLB >>>>> flushes are not needed for functional correctness (the virt->phys >>>>> mapping technically stays "correct", the kernel should simply to not it >>>>> for a while), so this is a correct optimization to make. On the other >>>>> hand, it means that the desired protection from Spectre-style attacks is >>>>> not perfect, as an attacker could try to prevent a stale TLB entry from >>>>> getting evicted, keeping it alive until the page it refers to is used by >>>>> the guest for some sensitive data, and then targeting it using a >>>>> spectre-gadget. >>>>> >>>>> Signed-off-by: Patrick Roy >>>> >>>> ... >>>> >>>>> >>>>> +static bool kvm_gmem_test_no_direct_map(struct inode *inode) >>>>> +{ >>>>> + return ((unsigned long) inode->i_private) & KVM_GMEM_NO_DIRECT_MAP; >>>>> +} >>>>> + >>>>> static inline void kvm_gmem_mark_prepared(struct folio *folio) >>>>> { >>>>> + struct inode *inode = folio_inode(folio); >>>>> + >>>>> + if (kvm_gmem_test_no_direct_map(inode)) { >>>>> + int r = set_direct_map_valid_noflush(folio_page(folio, 0), folio_nr_pages(folio), >>>>> + false); >>>> >>>> Will this work if KVM is built as a module, or is this another good >>>> reason why we might want guest_memfd core part of core-mm? >>> >>> mh, I'm admittedly not too familiar with the differences that would come >>> from building KVM as a module vs not. I do remember something about the >>> direct map accessors not being available for modules, so this would >>> indeed not work. Does that mean moving gmem into core-mm will be a >>> pre-requisite for the direct map removal stuff? >> >> Likely, we'd need some shim. >> >> Maybe for the time being it could be fenced using #if IS_BUILTIN() ... >> but that sure won't win in a beauty contest. > > Is anyone working on such a shim at the moment? Otherwise, would it make > sense for me to look into it? (although I'll probably need a pointer or > two for what is actually needed) > > I saw your comment on Fuad's series [1] indicating that he'll also need > some shim, so probably makes sense to tackle it anyway instead of > hacking around it with #if-ery. Elliot (CC) was working on "guestmem library" project [1], but it was unclear what we could factor out into the core. Looks like a simple shim for such stuff might be a good starting point, although not the final idea of encapsulating more in the library. @Elliot, are you currently still looking into this? [1] https://lore.kernel.org/all/20241113-guestmem-library-v3-0-71fdee85676b@quicinc.com/T/#u -- Cheers, David / dhildenb