Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp2218474rdf; Mon, 6 Nov 2023 07:56:49 -0800 (PST) X-Google-Smtp-Source: AGHT+IGrYAr5hPylG9g9FxP8sKoBTks9I0kRWptxKfszkJbnKBoeSJnBNLnEqMAJHNUd4FIeEeTM X-Received: by 2002:a17:90b:1d86:b0:27d:5568:e867 with SMTP id pf6-20020a17090b1d8600b0027d5568e867mr17111502pjb.9.1699286209227; Mon, 06 Nov 2023 07:56:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699286209; cv=none; d=google.com; s=arc-20160816; b=afcFVzQ4r52ZuA+F1JejaKoI/O8DfBIHgIsTRtd2mn8o9NbnjB8NRQLKGCXt2icEca D4JQQYcEiO6gVUFxkfeVC+2wEKb5cG0b96WFTzHC3lqxFzhJOfN2RVeiA7/NZrr+Z4Ge XXYt5vw6vbMlaJWUoQStesYKhKxuSWoHst99rG3QLV1PpoKAvugrwYhJc0FHMJ5f9pqt 3A3NcPeNjG9YK/zRALpuhJXLNh4AKVT8U7FqBapuFTt8fG8XvjiBvUebIHtux14Ph1Pj 6NbbaFi7eGi79U0hX1FigSGGv90iqNuPIS3KHmiTjQjNwqpPXwUhfGVdphtk3ITdLeLS kMnA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:from:subject :message-id:references:mime-version:in-reply-to:date:dkim-signature; bh=2j2Mtk4lW9HEtWavGEnwteIJF5J/7dhiq9rSq3IBx+E=; fh=az0dYthJxY2XaQxbVlWHFpi9od2C/z1Z8eryZcjliGE=; b=QtK6v0e7QLNBNhnaSLoECtskBg+14opUvRI6wLlJxib2Jt7rZScWwx+Ee7KA+aertu zMUTPGTAvi847ZFlatIT8UWEwRHUBdM7LnJOuWUrZNYHe256zzgDLeo/iIuk9WtWtjf6 xeGpSAw6YsJmRnaGCqgvdfJxryxlvy+0AOr8sAq/jB70QCvs/egaKnE3eOFJ6Mr7SK92 7ftHrjA2VpIGjR+l9M8BvtDj4dalleVkrr3ovlc2InoWCKuXIMTfLtBC/AZ1WWfoNTFP wo6gJHuH++tkidPxqcHT7oMuI1BuJ0+oFNqdSLNmTS2NB3RmtzOqTHeOhJ+p6c431px0 RVVQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=jfgAGp2H; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id t6-20020a17090abc4600b00280c97a5663si8387832pjv.132.2023.11.06.07.56.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 07:56:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=jfgAGp2H; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 815318058B42; Mon, 6 Nov 2023 07:56:22 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232230AbjKFP4L (ORCPT + 99 others); Mon, 6 Nov 2023 10:56:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232064AbjKFP4J (ORCPT ); Mon, 6 Nov 2023 10:56:09 -0500 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 092B9118 for ; Mon, 6 Nov 2023 07:56:06 -0800 (PST) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-d99ec34829aso5356460276.1 for ; Mon, 06 Nov 2023 07:56:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1699286165; x=1699890965; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=2j2Mtk4lW9HEtWavGEnwteIJF5J/7dhiq9rSq3IBx+E=; b=jfgAGp2HE5/xK9g4UWhCfe0wF8pepGfMwluQ7Me8zjfyk6kURtfjawQkzEKUL79x23 GA7yRpH7oYJ7PauF34lFwNQOzPOo074h3+rK5Nbw9zxFiNBJVL7dPbJmt303qnicGXhF nEfy/sj/763fXTU7n9f9aw4t+i+ogT4UuEHWx8nRxxHclr16shV25An5cvb61i6Now6A HZvr0t1B/eWzYe7LBjBhD2PvN13RVPGJ0vCSFvcA0giqJQMP2Ia9JWL28QQRK4AcKjCE yKF3NGLMBB0N+rtxWhEtTE6umISScKcM05FoJLvnyi5f4Ogi9MTGBHt+4K6q2C36ozTG krEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699286165; x=1699890965; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=2j2Mtk4lW9HEtWavGEnwteIJF5J/7dhiq9rSq3IBx+E=; b=DP5f6fKdt1E1DVrhT+f+xk2DyfnxozWdfJkP13wWp7Gnvg17vr0+aGXLO2J6Xwf31I UYyfurgR6GME37dEFLZnaUOb63R0osFgVGenzS1636r1cPZnPzawTd6mRXXqZB3gHllW dJWOajdf4MM/nUjevMQM4EbF+n8SdBEixk+nGYceg0W/CTWHPVPFKAVriUXdPfMomNty 5V2HMCdzLkfxxCzZ22FZz4GHRBlbcf1K3MiZ+SXCJn51B7DymjYSU59k6zdVVy6YAVsJ mqYFu5W9YelKrZvG6gs/zttniESaG8wYl/spApHgVEQPFpSF2/SIRTvz/evS0DV2ESTy zzPg== X-Gm-Message-State: AOJu0YzX5m1h1ZiJJPpypI9sY5mPB1qV0LwnYbDDySODRZcmHO4cEEya cgmqEOWDThzU23Kkbz5DYkWXMyA7KzU= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1083:b0:d9a:c3b8:4274 with SMTP id v3-20020a056902108300b00d9ac3b84274mr683795ybu.7.1699286165114; Mon, 06 Nov 2023 07:56:05 -0800 (PST) Date: Mon, 6 Nov 2023 07:56:03 -0800 In-Reply-To: Mime-Version: 1.0 References: <20231027182217.3615211-1-seanjc@google.com> <20231027182217.3615211-21-seanjc@google.com> Message-ID: Subject: Re: [PATCH v13 20/35] KVM: x86/mmu: Handle page fault for private memory From: Sean Christopherson To: Xu Yilun Cc: Paolo Bonzini , Marc Zyngier , Oliver Upton , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexander Viro , Christian Brauner , "Matthew Wilcox (Oracle)" , Andrew Morton , kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Xu Yilun , Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , David Matlack , Yu Zhang , Isaku Yamahata , "=?utf-8?Q?Micka=C3=ABl_Sala=C3=BCn?=" , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Mon, 06 Nov 2023 07:56:22 -0800 (PST) On Mon, Nov 06, 2023, Xu Yilun wrote: > On Sun, Nov 05, 2023 at 05:19:36PM +0100, Paolo Bonzini wrote: > > On Sun, Nov 5, 2023 at 2:04=E2=80=AFPM Xu Yilun wrote: > > > > > > > +static void kvm_mmu_prepare_memory_fault_exit(struct kvm_vcpu *vcp= u, > > > > + struct kvm_page_fault *= fault) > > > > +{ > > > > + kvm_prepare_memory_fault_exit(vcpu, fault->gfn << PAGE_SHIFT, > > > > + PAGE_SIZE, fault->write, fault-= >exec, > > > > + fault->is_private); > > > > +} > > > > + > > > > +static int kvm_faultin_pfn_private(struct kvm_vcpu *vcpu, > > > > + struct kvm_page_fault *fault) > > > > +{ > > > > + int max_order, r; > > > > + > > > > + if (!kvm_slot_can_be_private(fault->slot)) { > > > > + kvm_mmu_prepare_memory_fault_exit(vcpu, fault); > > > > + return -EFAULT; > > > > + } > > > > + > > > > + r =3D kvm_gmem_get_pfn(vcpu->kvm, fault->slot, fault->gfn, &f= ault->pfn, > > > > + &max_order); > > > > + if (r) { > > > > + kvm_mmu_prepare_memory_fault_exit(vcpu, fault); > > > > + return r; > > > > > > Why report KVM_EXIT_MEMORY_FAULT here? even with a ret !=3D -EFAULT? > >=20 > > The cases are EFAULT, EHWPOISON (which can report > > KVM_EXIT_MEMORY_FAULT) and ENOMEM. I think it's fine > > that even -ENOMEM can return KVM_EXIT_MEMORY_FAULT, > > and it doesn't violate the documentation. The docs tell you "what > > can you do if error if EFAULT or EHWPOISON?"; they don't > > exclude that other errnos result in KVM_EXIT_MEMORY_FAULT, > > it's just that you're not supposed to look at it >=20 > Thanks, it's OK for ENOMEM + KVM_EXIT_MEMORY_FAULT. >=20 > Another concern is, now 3 places to report EFAULT + KVM_EXIT_MEMORY_FAULT= : >=20 > if (!kvm_slot_can_be_private(fault->slot)) { > kvm_mmu_prepare_memory_fault_exit(vcpu, fault); > return -EFAULT; > } >=20 > file =3D kvm_gmem_get_file(slot); > if (!file) > return -EFAULT; >=20 > if (fault->is_private !=3D kvm_mem_is_private(vcpu->kvm, fault->gfn)) { > kvm_mmu_prepare_memory_fault_exit(vcpu, fault); > return -EFAULT; > } >=20 > They are different cases, and seems userspace should handle them > differently, but not enough information to distinguish them. For the first, the memory_fault exit will inform userspace that the guest w= ants to map memory as private, and userspace will see that the memslot isn't con= figured to support private mappings. Userspace may not even need to query memslots= , e.g. if the gfn in question has been enumerated to the guest as something that c= an only be mapped shared. For the second (no valid guest_memfd file), userspace put the last referenc= e to the guest_memfd file without informing the guest or creating a memslot. Th= at's firmly a userspace bug. For the third and last, userspace will see that the guest is requesting a p= rivate mapping but the gfn is configured for shared mappings. In all cases, userspace has the necessary information to resolve the issue,= where "resolving the issue" may mean terminating the guest. If userspace isn't t= racking memslots or the private attribute, then userspace has far bigger problems.