Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp1578299rwb; Fri, 13 Jan 2023 14:27:36 -0800 (PST) X-Google-Smtp-Source: AMrXdXsVXHlLHK38SiyDCiCSuT46P3rxieJ0e4+JYX7fcem5D/vpsGVO+CCooOu6/4s8vB8+HaSM X-Received: by 2002:a05:6402:710:b0:49b:9c5f:fe30 with SMTP id w16-20020a056402071000b0049b9c5ffe30mr5709616edx.29.1673648856622; Fri, 13 Jan 2023 14:27:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673648856; cv=none; d=google.com; s=arc-20160816; b=y4NeOmSl8TXegFtFnbWlKYKCnMkdn0Zk35Pf7xzKc7bMPWdmYR0fChqAizxknllvIC jM+5Oe0FUU0+Aw01rbcXeEuSjU3cYiSYA3kAyHAPQNF5KxKMjABO8MMmHfWeEzbMpSse c0bE4thClF5W92t5NSLa/B8NXbMaq5ugFQzSM0Ao6eMx9/bPaixcynmp+Nkdtw4HfPDF fgqdAW89Xio6vf/tJmHYBBtcZlRu9w6MvzkXJVlYDr17cqRH2/G+UWi9xl4RPgx31BDH SkOlrMnsip3czPW4GLGKHDyJATPBkSd37cPoN/KKGei4ZxANuZCkW09jLl1HTB2KWniH 3smQ== 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=z7l9vd6TJMxdcmrmIlHi+1aamG6yNr9jBZqHMxmbVBg=; b=TUCRFfbiNq9V2ECPMT4BM39bf3EfLMfehFehEERVHXeTaClZ/bXPpYBuypreJ0Qlk9 ufYiPZEUpdXCvkRSD9BYFDkUG4j86UKlYm3gmH+E7WDvex8L60J0Fqe7teKTRaR0srrK +Bi7gXbPn212zpK3y51PGWkBooG3y0LCZPFq7e3tDjVtIgkAQ9/PZmn6pmdWrGYA1WHA PHe0ePebhGRNXuIGlQ2TrEv3PwHPFj5z5kdevuvR9QZzSRLVZzFe4S36LAk4IDq8i7da RxW31+9C5J7gtswSdHenHceg42o7GCfLcgvsmgpVdw73VtldUWF0JzCOlOGxo5hyErZY fKwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=BR3tUqUT; 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 b13-20020a056402278d00b0047010e04c4dsi26831706ede.481.2023.01.13.14.27.23; Fri, 13 Jan 2023 14:27:36 -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=BR3tUqUT; 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 S231415AbjAMWCg (ORCPT + 52 others); Fri, 13 Jan 2023 17:02:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52458 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231334AbjAMWCb (ORCPT ); Fri, 13 Jan 2023 17:02:31 -0500 Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 561DF7464D for ; Fri, 13 Jan 2023 14:02:16 -0800 (PST) Received: by mail-pj1-x1031.google.com with SMTP id o1-20020a17090a678100b00219cf69e5f0so28317371pjj.2 for ; Fri, 13 Jan 2023 14:02:16 -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=z7l9vd6TJMxdcmrmIlHi+1aamG6yNr9jBZqHMxmbVBg=; b=BR3tUqUT1wdTPEhuyI88J1yG4yZUf5PuICve+KLW/6jG7Wb1v6McPHOp0JH41FPvHo cA2xNTdp4kP3XHFHgUTLdE2qj40XKo3fIu/xNDgFentclcLH8D4+aqHtGytwQRH7EFT0 SvW3dkQzsILPvPKa4Is4mjjYyZmLU2ZjXaSMTILUI2Ql4PmYTWFYg91PuBLsgKKGoru5 2kNrKIxXaIvBTzLY81Ye3wChF7mPYngBYjfHrPWew9lZluOUDNoM3QdVM4dxlS9vgJCj 1PCCo9ULYh3HxSCxalWt2dPVhw7cT4s2LG6uTB7ArEkfMypRVuMINSetLuB5egod0Oa5 0CYQ== 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=z7l9vd6TJMxdcmrmIlHi+1aamG6yNr9jBZqHMxmbVBg=; b=fQmvWZDXwnSo107OPi+qghtlXJGz6+o/X4Xh2sTfH7wWVsE2UzzEpH3OsqWPeESOwe Y7Po4A2JirqlXpXOvRdYAqpOeTmdLhrUk5XrUJBWnpeD8cK9dxBTykTPSZCrST897zUL S7s/XPOYlWwEdXYHVMPEti/iFXyr/l96KOhETJ6L/Ch8x9RlOjrja1dR5dcIco6qUKME wjqsb8bmfcVfhOfF4hOKuEeX8bplj0dWPcxQdRlvbmAypUf6n+2JlIHSEHYZOazdHQxq xcnjHxOwMiuSlnC9ioUBUPoPFhNBAOvq/xh8f8aPlFQ6xfjovjI5iXNZwfmkrHOm0zEX yiRQ== X-Gm-Message-State: AFqh2koekFDmOHDK03jlTG3S013nXbe6LlMgpiuUIamhVQR+MaxFnqzi jqt3ykyBa0f/4tXecqGrvsLizg== X-Received: by 2002:a05:6a20:47de:b0:b3:66b7:24ff with SMTP id ey30-20020a056a2047de00b000b366b724ffmr1359880pzb.1.1673647335304; Fri, 13 Jan 2023 14:02:15 -0800 (PST) Received: from google.com (7.104.168.34.bc.googleusercontent.com. [34.168.104.7]) by smtp.gmail.com with ESMTPSA id k36-20020a635a64000000b0048988ed9e4bsm11687157pgm.19.2023.01.13.14.02.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jan 2023 14:02:14 -0800 (PST) Date: Fri, 13 Jan 2023 22:02:11 +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 2/9] KVM: Introduce per-page memory attributes Message-ID: References: <20221202061347.1070246-1-chao.p.peng@linux.intel.com> <20221202061347.1070246-3-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-3-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: > diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig > index fbeaa9ddef59..a8e379a3afee 100644 > --- a/arch/x86/kvm/Kconfig > +++ b/arch/x86/kvm/Kconfig > @@ -49,6 +49,7 @@ config KVM > select SRCU > select INTERVAL_TREE > select HAVE_KVM_PM_NOTIFIER if PM > + select HAVE_KVM_MEMORY_ATTRIBUTES I would prefer to call this KVM_GENERIC_MEMORY_ATTRIBUTES. Similar to KVM_GENERIC_HARDWARE_ENABLING, ARM does need/have hardware enabling, it just doesn't want KVM's generic implementation. In this case, pKVM does support memory attributes, but uses stage-2 tables to track ownership and doesn't need/want the overhead of the generic implementation. > help ... > +#define KVM_MEMORY_ATTRIBUTE_READ (1ULL << 0) > +#define KVM_MEMORY_ATTRIBUTE_WRITE (1ULL << 1) > +#define KVM_MEMORY_ATTRIBUTE_EXECUTE (1ULL << 2) > +#define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) I think we should carve out bits 0-2 for RWX, but I don't think we should actually define them until they're actually accepted by KVM. > +static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, > + struct kvm_memory_attributes *attrs) > +{ > + gfn_t start, end; > + unsigned long i; > + void *entry; > + u64 supported_attrs = kvm_supported_mem_attributes(kvm); > + > + /* flags is currently not used. */ > + if (attrs->flags) > + return -EINVAL; > + if (attrs->attributes & ~supported_attrs) Nit, no need for "supported_attrs", just consume kvm_supported_mem_attributes() directly. > + return -EINVAL; > + if (attrs->size == 0 || attrs->address + attrs->size < attrs->address) > + return -EINVAL; > + if (!PAGE_ALIGNED(attrs->address) || !PAGE_ALIGNED(attrs->size)) > + return -EINVAL; > + > + start = attrs->address >> PAGE_SHIFT; > + end = (attrs->address + attrs->size - 1 + PAGE_SIZE) >> PAGE_SHIFT; > + > + entry = attrs->attributes ? xa_mk_value(attrs->attributes) : NULL; > + > + mutex_lock(&kvm->lock); Peeking forward multiple patches, this needs to take kvm->slots_lock, not kvm->lock. There's a bug in the lpage_disallowed patch that I believe can most easily be solved by making this mutually exclusive with memslot changes. When a memslot is created, KVM needs to walk through the attributes to detect whether or not the attributes are identical for the entire slot. To avoid races, that means taking slots_lock. The alternative would be to query the attributes when adjusting the hugepage level and avoid lpage_disallowed entirely, but in the (very brief) time I've thought about this I haven't come up with a way to do that in a performant manner. > + for (i = start; i < end; i++) Curly braces needed on the for-loop.