Received: by 2002:a05:6358:701b:b0:131:369:b2a3 with SMTP id 27csp2672062rwo; Sun, 23 Jul 2023 22:08:50 -0700 (PDT) X-Google-Smtp-Source: APBJJlEtboGU+8zAC9sQo/hzMwIwp8Cw0/kIlbgU9QQsj9SuhM2eoMlLF3jCvvJfM3maUZGqlxsZ X-Received: by 2002:a05:6a00:2185:b0:666:ecf4:ed6d with SMTP id h5-20020a056a00218500b00666ecf4ed6dmr4983263pfi.18.1690175329924; Sun, 23 Jul 2023 22:08:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690175329; cv=none; d=google.com; s=arc-20160816; b=OIjl2NmJsPrkpH8gVvTmIyUH81gfZOLEJBFXz1BZq0BDDBOOebEcDGLA12GSDIBfAq +uuAnmUmbP9a1gDK3j0R5HeEj/kyv53v3IQ1V9uxAwhPhdVkuwWVbY+XRRirb9wuqXde daceQL2V3/lQy1GLGQpBXCLKtWZ2lUE9+Qn8+3l/mYZcXUuMTaZbJ3Uh+2/zfK8LDCRE pL7ZLkr2tnsTmvdf8KmD8vh8OBm/Hn+tg+p/ktPXC9ewQeG0a5a31pdYFyBiYo9zObSQ 3kizdGp/mfMA6Yq1lp63Qy5x16NjkZNuaGXGXS9FTM8pjsL9txmnva4lyVhH0+YTgHoD mPzA== 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=ABqWGqgrqaXNLB7krIWsJ7qG31V949kzE6IfQVbXDeM=; fh=8Not1fDpAokYGWYQqDKzQyOHaNe3Yii5zgkCRaRcXag=; b=FHvnoTuSHlWCQ7TRyV/IxUgLcyks0JJhcTk0b3QvrH6Mx60P7oFrf2MCWVq4xoOTG8 usAHWP6RsOVsR6cdFovfY5QAkzvAbAPdGuQnIyP502yvcKC7VJrfpYVw5teHT9ge/Xfy i3QvA2F8iM84U+tSe5zQ01ljDYJ9TSaIhJeKS838DLvGoLygxiVRCDVt8+fDbtVc6emz LO5XfWaYthZqYIJ7MophxOTqmO9J7dSfWVRsZy55gmlnv3J1LVTNo3W83UoKzkCHvGnM 2RQMKOdS63CRzyBno2EAq/vrVpaWozWaChWQwOYl7ln/4DTOjRYt4Gx+fzmaaVHBRTJx ifLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=NtpaCJWf; 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=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g13-20020a056a000b8d00b006535a72d26csi8607197pfj.152.2023.07.23.22.08.37; Sun, 23 Jul 2023 22:08:49 -0700 (PDT) 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=@intel.com header.s=Intel header.b=NtpaCJWf; 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=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229676AbjGXEpx (ORCPT + 99 others); Mon, 24 Jul 2023 00:45:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53930 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229456AbjGXEpv (ORCPT ); Mon, 24 Jul 2023 00:45:51 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 157E31B6; Sun, 23 Jul 2023 21:45:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1690173950; x=1721709950; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=q9qzwcL7N7viHeki30qyeMCUuWnJ87fuGcM4hbUg7Mg=; b=NtpaCJWf6JJa6XKHuLRhKK7pFYUMMQofEh2yTs7d9WA3ZIjgFpTCtoO8 Edq8KDCrQHBYbTHzNoEKucA3E/6cCWGpY+/cHVV3MX4ik/jHgQCEORqb7 wSvC3eAiwOb9mxxDhb2CFyIoMeZLOYM/+LtMn7OQ5nxM9W4wZ/sPTI1tK rK2afDRyd8OL3KOXO4edKYiz4plKbrPXt9jP806iSOeAifT4wf4mrQrJT P+1iFE9zVghp6PjgfNxT1AKG3HPWrObiD8mhr5u8bIAStC5OxIqIhqmVJ 9oPghZ7iu+7LX+G8RepHnjlob953+e312EqqjRnbgQpH2pS+GLieZdvYo Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="352240346" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="352240346" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jul 2023 21:45:49 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10780"; a="972119310" X-IronPort-AV: E=Sophos;i="6.01,228,1684825200"; d="scan'208";a="972119310" Received: from yilunxu-optiplex-7050.sh.intel.com (HELO localhost) ([10.239.159.165]) by fmsmga006.fm.intel.com with ESMTP; 23 Jul 2023 21:45:40 -0700 Date: Mon, 24 Jul 2023 12:43:53 +0800 From: Xu Yilun To: Sean Christopherson Cc: Paolo Bonzini , Marc Zyngier , Oliver Upton , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , "Matthew Wilcox (Oracle)" , Andrew Morton , Paul Moore , James Morris , "Serge E. Hallyn" , 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-security-module@vger.kernel.org, linux-kernel@vger.kernel.org, Chao Peng , Fuad Tabba , Jarkko Sakkinen , Yu Zhang , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , Vlastimil Babka , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" Subject: Re: [RFC PATCH v11 08/29] KVM: Introduce per-page memory attributes Message-ID: References: <20230718234512.1690985-1-seanjc@google.com> <20230718234512.1690985-9-seanjc@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230718234512.1690985-9-seanjc@google.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham 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 2023-07-18 at 16:44:51 -0700, Sean Christopherson wrote: > From: Chao Peng > > In confidential computing usages, whether a page is private or shared is > necessary information for KVM to perform operations like page fault > handling, page zapping etc. There are other potential use cases for > per-page memory attributes, e.g. to make memory read-only (or no-exec, > or exec-only, etc.) without having to modify memslots. > > Introduce two ioctls (advertised by KVM_CAP_MEMORY_ATTRIBUTES) to allow > userspace to operate on the per-page memory attributes. > - KVM_SET_MEMORY_ATTRIBUTES to set the per-page memory attributes to > a guest memory range. > - KVM_GET_SUPPORTED_MEMORY_ATTRIBUTES to return the KVM supported > memory attributes. > > Use an xarray to store the per-page attributes internally, with a naive, > not fully optimized implementation, i.e. prioritize correctness over > performance for the initial implementation. > > Because setting memory attributes is roughly analogous to mprotect() on > memory that is mapped into the guest, zap existing mappings prior to > updating the memory attributes. Opportunistically provide an arch hook > for the post-set path (needed to complete invalidation anyways) in > anticipation of x86 needing the hook to update metadata related to > determining whether or not a given gfn can be backed with various sizes > of hugepages. > > It's possible that future usages may not require an invalidation, e.g. > if KVM ends up supporting RWX protections and userspace grants _more_ > protections, but again opt for simplicity and punt optimizations to > if/when they are needed. > > Suggested-by: Sean Christopherson > Link: https://lore.kernel.org/all/Y2WB48kD0J4VGynX@google.com > Cc: Fuad Tabba > Signed-off-by: Chao Peng > Co-developed-by: Sean Christopherson > Signed-off-by: Sean Christopherson > --- > Documentation/virt/kvm/api.rst | 60 ++++++++++++ > include/linux/kvm_host.h | 14 +++ > include/uapi/linux/kvm.h | 14 +++ > virt/kvm/Kconfig | 4 + > virt/kvm/kvm_main.c | 170 +++++++++++++++++++++++++++++++++ > 5 files changed, 262 insertions(+) > Only some trivial concerns below. [...] > @@ -1175,6 +1176,9 @@ static struct kvm *kvm_create_vm(unsigned long type, const char *fdname) > spin_lock_init(&kvm->mn_invalidate_lock); > rcuwait_init(&kvm->mn_memslots_update_rcuwait); > xa_init(&kvm->vcpu_array); > +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES > + xa_init(&kvm->mem_attr_array); > +#endif > > INIT_LIST_HEAD(&kvm->gpc_list); > spin_lock_init(&kvm->gpc_lock); > @@ -1346,6 +1350,9 @@ static void kvm_destroy_vm(struct kvm *kvm) > kvm_free_memslots(kvm, &kvm->__memslots[i][0]); > kvm_free_memslots(kvm, &kvm->__memslots[i][1]); > } > +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES > + xa_destroy(&kvm->mem_attr_array); > +#endif Is it better to make the destruction in reverse order from the creation? To put xa_destroy(&kvm->mem_attr_array) after cleanup_srcu_struct(&kvm->srcu), or put xa_init(&kvm->mem_attr_array) after init_srcu_struct(&kvm->irq_srcu). > cleanup_srcu_struct(&kvm->irq_srcu); > cleanup_srcu_struct(&kvm->srcu); > kvm_arch_free_vm(kvm); > @@ -2346,6 +2353,145 @@ static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, > } > #endif /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */ [...] > +static int kvm_vm_ioctl_set_mem_attributes(struct kvm *kvm, > + struct kvm_memory_attributes *attrs) > +{ > + gfn_t start, end; > + > + /* flags is currently not used. */ > + if (attrs->flags) > + return -EINVAL; > + if (attrs->attributes & ~kvm_supported_mem_attributes(kvm)) > + 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; As the attrs->address/size are both garanteed to be non-zero, non-wrap and page aligned in prevous check. Is it OK to simplify the calculation, like: end = (attrs->address + attrs->size) >> PAGE_SHIFT; > + > + if (WARN_ON_ONCE(start == end)) > + return -EINVAL; Also, is this check possible to be hit? Maybe remove it? Thanks, Yilun > + > + /* > + * xarray tracks data using "unsigned long", and as a result so does > + * KVM. For simplicity, supports generic attributes only on 64-bit > + * architectures. > + */ > + BUILD_BUG_ON(sizeof(attrs->attributes) != sizeof(unsigned long)); > + > + return kvm_vm_set_mem_attributes(kvm, attrs->attributes, start, end); > +} > +#endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */