Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp1686195rwb; Fri, 13 Jan 2023 16:18:25 -0800 (PST) X-Google-Smtp-Source: AMrXdXvyNLzbwWf5OucTKMOqFVVK0CxWtS+SayDvwGNpxrcs//a8aiReB/w6OIO9m3QvF8RI81Ls X-Received: by 2002:a17:90b:3c8d:b0:223:ed95:651c with SMTP id pv13-20020a17090b3c8d00b00223ed95651cmr88845991pjb.7.1673655504797; Fri, 13 Jan 2023 16:18:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673655504; cv=none; d=google.com; s=arc-20160816; b=PP6VxVrpi8nODMnjsljxuzvA8hZLKC5X/AEh+zBRxK/NQBTuNo4cyBA/REt/VViAP6 O1rpVuW/Uio86tzawoWeRTTEimszVUcTRsQLH/aom0B/x1xjRwCZFmHA0v3N3iSXmj88 ryHTRhFmmKG8Ur2tu0ccRf4BpfmGoaKAVm1YWVFgHv1U+yFrkVCMQF8KOKnVgh/kROu+ WuaR+NqwUcI0OEXK9dtDyBOBQVdgLRYO3W7LGRhRMHCctfHnV1h81VrfzGKCyDG2+529 9N9d+CuV2E7TdwAPFsXP1q0w6/ZL9SPaaeQiZDkvc60CfJ2gDbXdfUiZPQv/Ij2VUP/W xO1g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=pnZMr3msysr3F7hQXLH7AQBYvEUMLHsk9nNkCMpDY14=; b=bpE/V3Qg7yW6BpWFbQ5CMB6ndconT9cs0fNg5t3cBc3lF+S5rp3brN8L4uJZwFRj7v P0551pBEAlGk+qWI+D6AGoKeTjq6IOXmQsFRG1JTzQ9O4ObgzoEXswCUpH5kFSuq1Dco K3FuKhHWG+tprUbwZvgiHZzNDdHFiJ4IxedSOcUUhkO82HJTU0sry3rMdeqvtJrAThfm fHJuyreMEX3C5EHQNMJS1lu2Olf0dXC8glqBLPIUCEpIooWL8hl8DnkK8UsXDaaeQOiW ZWS5WX85nq4IhKuCvlsY/zc4Ga7Bi3rbbiamZSn4z2Ypsi8a5DknsrP9sgeTcP4r2h74 /I5g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=AKE7gl1e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 128-20020a630286000000b00477b29bb008si22682221pgc.631.2023.01.13.16.18.17; Fri, 13 Jan 2023 16:18:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=AKE7gl1e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229509AbjANABN (ORCPT + 55 others); Fri, 13 Jan 2023 19:01:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229914AbjANABH (ORCPT ); Fri, 13 Jan 2023 19:01:07 -0500 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2FB38CD24 for ; Fri, 13 Jan 2023 16:01:05 -0800 (PST) Received: by mail-pl1-x629.google.com with SMTP id d9so24972267pll.9 for ; Fri, 13 Jan 2023 16:01:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=pnZMr3msysr3F7hQXLH7AQBYvEUMLHsk9nNkCMpDY14=; b=AKE7gl1eiHLqGoN0SnyVqNEPplEIwVO+riNAN02PwmozMjE2L10QCVf4rXYqWYXwrc nM+dfsbEmjg4aU4wDbXk81gsNGcxDwcFMrhjKV0dxv5tgPv5HRD69rmvva17U2MwBylu ox8qjH1cD99q2HmCdnSQ0OAN2PKSiP6XIZtUS7wpznd/WxqipSgn4SXTj5GoK28DsB/h /h+/U8gm5nSazzO1cze9VAQXTqkUirpJW7NcMgscsM2IWWic/84oK1838Dl2K3mrzzcM +5+ItvAPTnRvsTbhTQfUPMUE3qPaOrcvkhrQopsnh/lwNWCnC0CJndMiYCyxUMoXegK5 gjIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=pnZMr3msysr3F7hQXLH7AQBYvEUMLHsk9nNkCMpDY14=; b=oYVIef6T3xdjP9AyP0J02GwpBusU5QHtTaQcEBSbG/2gdp7T3u0CdVmBtI+XOC/fr9 BcVVjXvlcQfAyyOdkMfsxWdi7rm+3yNXymQLfHoQRoBHrRFH7Ek69xEFexenDBRvKkGH W1PADL6pghpQo91aFAivTd1splla4WFjemImc0kcq2rRQ9fnzPT5WnqlHXn9cc3Ru20I xrGlFwSoaVcsUoEizjU99IpE8ZM9E+K9mwsWP+6gqttuNjbhFHT3GM2lFkd3L6eQzzz6 +ZF5ao3wrKEZnUhKZXzHswRAF5z3KhMTiqbg9YpYhR5KPcYkDXTA5K7m2el7IBPuZ2pO 63UQ== X-Gm-Message-State: AFqh2koelNkjubEU2bwBY2bE0CNVJtaBP8cao7O8btkdeA0nvsCXghZO ZLWfLJcBqUrAoG0I998P1P+yqQ== X-Received: by 2002:a17:902:c409:b0:194:6d3c:38a5 with SMTP id k9-20020a170902c40900b001946d3c38a5mr516748plk.1.1673654465280; Fri, 13 Jan 2023 16:01:05 -0800 (PST) Received: from google.com (7.104.168.34.bc.googleusercontent.com. [34.168.104.7]) by smtp.gmail.com with ESMTPSA id k7-20020a170902760700b00192bf7eaf28sm14649057pll.286.2023.01.13.16.01.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 16:01:04 -0800 (PST) Date: Sat, 14 Jan 2023 00:01:01 +0000 From: Sean Christopherson To: Chao Peng Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, linux-doc@vger.kernel.org, qemu-devel@nongnu.org, Paolo Bonzini , Jonathan Corbet , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Arnd Bergmann , Naoya Horiguchi , Miaohe Lin , x86@kernel.org, "H . Peter Anvin" , Hugh Dickins , Jeff Layton , "J . Bruce Fields" , Andrew Morton , Shuah Khan , Mike Rapoport , Steven Price , "Maciej S . Szmigiero" , Vlastimil Babka , Vishal Annapurve , Yu Zhang , "Kirill A . Shutemov" , luto@kernel.org, jun.nakajima@intel.com, dave.hansen@intel.com, ak@linux.intel.com, david@redhat.com, aarcange@redhat.com, ddutile@redhat.com, dhildenb@redhat.com, Quentin Perret , tabba@google.com, Michael Roth , mhocko@suse.com, wei.w.wang@intel.com Subject: Re: [PATCH v10 9/9] KVM: Enable and expose KVM_MEM_PRIVATE Message-ID: References: <20221202061347.1070246-1-chao.p.peng@linux.intel.com> <20221202061347.1070246-10-chao.p.peng@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221202061347.1070246-10-chao.p.peng@linux.intel.com> X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Dec 02, 2022, Chao Peng wrote: > @@ -10357,6 +10364,12 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) > > if (kvm_check_request(KVM_REQ_UPDATE_CPU_DIRTY_LOGGING, vcpu)) > static_call(kvm_x86_update_cpu_dirty_logging)(vcpu); > + > + if (kvm_check_request(KVM_REQ_MEMORY_MCE, vcpu)) { > + vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; Synthesizing triple fault shutdown is not the right approach. Even with TDX's MCE "architecture" (heavy sarcasm), it's possible that host userspace and the guest have a paravirt interface for handling memory errors without killing the host. > + r = 0; > + goto out; > + } > } > @@ -1982,6 +2112,10 @@ int __kvm_set_memory_region(struct kvm *kvm, > !access_ok((void __user *)(unsigned long)mem->userspace_addr, > mem->memory_size)) > return -EINVAL; > + if (mem->flags & KVM_MEM_PRIVATE && > + (mem->restricted_offset & (PAGE_SIZE - 1) || Align indentation. > + mem->restricted_offset > U64_MAX - mem->memory_size)) Strongly prefer to use similar logic to existing code that detects wraps: mem->restricted_offset + mem->memory_size < mem->restricted_offset This is also where I'd like to add the "gfn is aligned to offset" check, though my brain is too fried to figure that out right now. > + return -EINVAL; > if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_MEM_SLOTS_NUM) > return -EINVAL; > if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr) > @@ -2020,6 +2154,9 @@ int __kvm_set_memory_region(struct kvm *kvm, > if ((kvm->nr_memslot_pages + npages) < kvm->nr_memslot_pages) > return -EINVAL; > } else { /* Modify an existing slot. */ > + /* Private memslots are immutable, they can only be deleted. */ I'm 99% certain I suggested this, but if we're going to make these memslots immutable, then we should straight up disallow dirty logging, otherwise we'll end up with a bizarre uAPI. > + if (mem->flags & KVM_MEM_PRIVATE) > + return -EINVAL; > if ((mem->userspace_addr != old->userspace_addr) || > (npages != old->npages) || > ((mem->flags ^ old->flags) & KVM_MEM_READONLY)) > @@ -2048,10 +2185,28 @@ int __kvm_set_memory_region(struct kvm *kvm, > new->npages = npages; > new->flags = mem->flags; > new->userspace_addr = mem->userspace_addr; > + if (mem->flags & KVM_MEM_PRIVATE) { > + new->restricted_file = fget(mem->restricted_fd); > + if (!new->restricted_file || > + !file_is_restrictedmem(new->restricted_file)) { > + r = -EINVAL; > + goto out; > + } > + new->restricted_offset = mem->restricted_offset; > + } > + > + new->kvm = kvm; Set this above, just so that the code flows better.