Received: by 2002:a05:7412:b795:b0:e2:908c:2ebd with SMTP id iv21csp276788rdb; Thu, 2 Nov 2023 03:33:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFKO74g67U8/t9BTABmC7gvtL1/atjd1wtCGVOXNxKEQS3VeFKh9Vn5wAK/Ld9cCqFPA3s2 X-Received: by 2002:a17:90b:4c87:b0:280:2823:6615 with SMTP id my7-20020a17090b4c8700b0028028236615mr13506933pjb.36.1698921239047; Thu, 02 Nov 2023 03:33:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698921239; cv=none; d=google.com; s=arc-20160816; b=pJl9hHes8un7id4kHr7SNDZFOSL5rC4JAOVcy0c8hcGHT3cdPHRgmEGq3I3Do16uUV 7io6uLv9Uxq25KwfoXjWf2Oxt8QiMK6s1p059lc8itc8epq/pIaYGSwCULLz8Qc80oL3 DsCJZRnvUbjYAOSPfvHv0S3TO2uw2K0xwU+9Osi/I4tECDfXYIpO2F0Htg3KfYXiLvAM zMxXRtcAwQ1XkRnDbYoNYPjorr+EmkPMoej2DX7bWWkmi9RRgw1S9QodKVsLuLl1nl2/ w7kYBgU84XkEcHpI2hFwdHCn6l7C7ElsNZOq8CDEaO0mXrmWkVuLi3AD7fN6ThNo0Xp7 a5EA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:autocrypt :from:references:cc:to:content-language:subject:user-agent :mime-version:date:message-id:dkim-signature; bh=EePgrsn5C6IrnNIxegoYsuWMp1Sc1phl1cmFpN1d7s8=; fh=NA2h7sxIhw9EdtEi9fDuKOsKmh7An3+7lRcEhIX6BJ8=; b=avaqGDozedNdYq3WHZsRPIVwu1A7tFPsL7EdDG9KpmEs+SkmWH82qQblqQPD4yNzqS oNcsf5VwyILrmMdVVC0C0b9GdIPyLruebFQRp6eLAisPhheFwa0eTK1zvxLwPYPEuoFO Esnsr75QxOK1UqEWlZZpn3qFa6Ex65eCBhBJoyaUmTJAUtR6PWvEz8e8ahFhJjWRFqYy cPDABvScBPl/a8qs2euGjc7hDII6itgwyMe4LQ2Fp0eVcGgW29cS+Ifb9w7LFjwTDkw3 ezJbEWi79XQV/7vK/fDSmFlnAU4VfiB0AYfV2PX5xXpHGtK/0ADvXaYNc0EVX6yYEA8p onYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dgirWlph; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id mv23-20020a17090b199700b0027d11201a11si2431711pjb.170.2023.11.02.03.33.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Nov 2023 03:33:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dgirWlph; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id B94F480CFD0B; Thu, 2 Nov 2023 03:33:55 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347115AbjKBKdl (ORCPT + 99 others); Thu, 2 Nov 2023 06:33:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47832 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346874AbjKBKdi (ORCPT ); Thu, 2 Nov 2023 06:33:38 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3355C197 for ; Thu, 2 Nov 2023 03:32:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698921163; 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=EePgrsn5C6IrnNIxegoYsuWMp1Sc1phl1cmFpN1d7s8=; b=dgirWlphzrGO6MHqpIIjT84Uqd1QdkjyJr11Ajs/ItOiS8dzU1FxYRNSDOLCrLNNdTNtse ejUhg5GZ7/GPtFI8wDLDWTtmpzhJjqPYGv/LjXS0j09CUI50+QfeHdoPZBE+xPUBwR+0ez B6ktf1dgPFxaX9eCJJdWhBQFNMXS+gI= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-96-_U5Cx30EPkGv7_0QTibORw-1; Thu, 02 Nov 2023 06:32:41 -0400 X-MC-Unique: _U5Cx30EPkGv7_0QTibORw-1 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-9ae7663e604so53087066b.3 for ; Thu, 02 Nov 2023 03:32:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698921160; x=1699525960; h=content-transfer-encoding:in-reply-to:autocrypt:from:references:cc :to:content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=EePgrsn5C6IrnNIxegoYsuWMp1Sc1phl1cmFpN1d7s8=; b=PSuhYvZvtpOsICESueb9yG3nMghivoI5OnKUgn3NgG5Nbi9NdyP+2JAJs+gXtxbcUo Mph+rMH+LokPK7ZrQuTVFN3Iewrlzgq6ZTu80aewW9A/h4Fo4WEAbJNZiKMWqnroYoKY Ct03IgX4/YpwQtyIQg8dcP9t3V3IIvO0448MUPCejvnegd9S5M+uu9rsWN6IU77Q3/N/ 6w4Ypb1BX+64+ID7Uh6h1PFT/vkw8GRDWN4Ubn3/sRmLcAAs+6LshFEHJUzSpudAWJB8 ZT7y+aPCrQTE1f3aCxp9aZeG/am2HIYqJWy/WLzc8AoWfftk1uN63YAoDzm0RcREkeCz tLdA== X-Gm-Message-State: AOJu0Yy6sEOTZOokSCaDIxfJ2iDxrp9KAnTKVJK9lGtcUfntf6TsJY6f 3EZ5oIN2mpNpQFe/LHPxv4ONKPFOQW7sUhuflP0YoR8pdrrK+TMZIQuiforHxLnjhmQTHwhgPxq n9jenr6aKpesSO5hNTpMBIp8X X-Received: by 2002:a17:907:70a:b0:9bd:9bfe:e410 with SMTP id xb10-20020a170907070a00b009bd9bfee410mr4134671ejb.72.1698921160424; Thu, 02 Nov 2023 03:32:40 -0700 (PDT) X-Received: by 2002:a17:907:70a:b0:9bd:9bfe:e410 with SMTP id xb10-20020a170907070a00b009bd9bfee410mr4134618ejb.72.1698921159974; Thu, 02 Nov 2023 03:32:39 -0700 (PDT) Received: from ?IPV6:2001:b07:6468:f312:9af8:e5f5:7516:fa89? ([2001:b07:6468:f312:9af8:e5f5:7516:fa89]) by smtp.googlemail.com with ESMTPSA id y13-20020a170906470d00b0099bd1a78ef5sm957651ejq.74.2023.11.02.03.32.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 02 Nov 2023 03:32:39 -0700 (PDT) Message-ID: Date: Thu, 2 Nov 2023 11:32:34 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v13 13/35] KVM: Introduce per-page memory attributes Content-Language: en-US To: "Huang, Kai" , "viro@zeniv.linux.org.uk" , "aou@eecs.berkeley.edu" , "Christopherson,, Sean" , "brauner@kernel.org" , "oliver.upton@linux.dev" , "chenhuacai@kernel.org" , "paul.walmsley@sifive.com" , "palmer@dabbelt.com" , "maz@kernel.org" , "mpe@ellerman.id.au" , "willy@infradead.org" , "anup@brainfault.org" , "akpm@linux-foundation.org" Cc: "Li, Xiaoyao" , "kvm-riscv@lists.infradead.org" , "mic@digikod.net" , "liam.merwick@oracle.com" , "kvm@vger.kernel.org" , "Yamahata, Isaku" , "kirill.shutemov@linux.intel.com" , "david@redhat.com" , "tabba@google.com" , "amoorthy@google.com" , "linuxppc-dev@lists.ozlabs.org" , "michael.roth@amd.com" , "kvmarm@lists.linux.dev" , "linux-kernel@vger.kernel.org" , "linux-fsdevel@vger.kernel.org" , "linux-riscv@lists.infradead.org" , "chao.p.peng@linux.intel.com" , "linux-mips@vger.kernel.org" , "Annapurve, Vishal" , "vbabka@suse.cz" , "mail@maciej.szmigiero.name" , "yu.c.zhang@linux.intel.com" , "qperret@google.com" , "dmatlack@google.com" , "Xu, Yilun" , "isaku.yamahata@gmail.com" , "ackerleytng@google.com" , "jarkko@kernel.org" , "linux-arm-kernel@lists.infradead.org" , "linux-mm@kvack.org" , "Wang, Wei W" References: <20231027182217.3615211-1-seanjc@google.com> <20231027182217.3615211-14-seanjc@google.com> From: Paolo Bonzini Autocrypt: addr=pbonzini@redhat.com; keydata= xsEhBFRCcBIBDqDGsz4K0zZun3jh+U6Z9wNGLKQ0kSFyjN38gMqU1SfP+TUNQepFHb/Gc0E2 CxXPkIBTvYY+ZPkoTh5xF9oS1jqI8iRLzouzF8yXs3QjQIZ2SfuCxSVwlV65jotcjD2FTN04 hVopm9llFijNZpVIOGUTqzM4U55sdsCcZUluWM6x4HSOdw5F5Utxfp1wOjD/v92Lrax0hjiX DResHSt48q+8FrZzY+AUbkUS+Jm34qjswdrgs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XSiH76ZvqzV0Z/cAMBo5NF/w= In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.3 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.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 (groat.vger.email [0.0.0.0]); Thu, 02 Nov 2023 03:33:55 -0700 (PDT) On 11/2/23 04:01, Huang, Kai wrote: > On Fri, 2023-10-27 at 11:21 -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. >> >> Use bit 3 for the PRIVATE attribute so that KVM can use bits 0-2 for RWX >> attributes/protections in the future, e.g. to give userspace fine-grained >> control over read, write, and execute protections for guest memory. >> >> Provide arch hooks for handling attribute changes before and after common >> code sets the new attributes, e.g. x86 will use the "pre" hook to zap all >> relevant mappings, and the "post" hook to track whether or not hugepages >> can be used to map the range. >> >> To simplify the implementation wrap the entire sequence with >> kvm_mmu_invalidate_{begin,end}() even though the operation isn't strictly >> guaranteed to be an invalidation. For the initial use case, x86 *will* >> always invalidate memory, and preventing arch code from creating new >> mappings while the attributes are in flux makes it much easier to reason >> about the correctness of consuming attributes. >> >> 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 >> Cc: Xu Yilun >> Cc: Mickaël Salaün >> Signed-off-by: Chao Peng >> Co-developed-by: Sean Christopherson >> Signed-off-by: Sean Christopherson >> > > [...] > >> +Note, there is no "get" API. Userspace is responsible for explicitly tracking >> +the state of a gfn/page as needed. >> + >> > > [...] > >> >> +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES >> +static inline unsigned long kvm_get_memory_attributes(struct kvm *kvm, gfn_t gfn) >> +{ >> + return xa_to_value(xa_load(&kvm->mem_attr_array, gfn)); >> +} > > Only call xa_to_value() when xa_load() returns !NULL? This xarray does not store a pointer, therefore xa_load() actually returns an integer that is tagged with 1 in the low bit: static inline unsigned long xa_to_value(const void *entry) { return (unsigned long)entry >> 1; } Returning zero for an empty entry is okay, so the result of xa_load() can be used directly. >> + >> +bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end, >> + unsigned long attrs); > > Seems it's not immediately clear why this function is needed in this patch, > especially when you said there is no "get" API above. Add some material to > changelog? It's used by later patches; even without a "get" API, it's a pretty fundamental functionality. >> +bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm, >> + struct kvm_gfn_range *range); >> +bool kvm_arch_post_set_memory_attributes(struct kvm *kvm, >> + struct kvm_gfn_range *range); > > Looks if this Kconfig is on, the above two arch hooks won't have implementation. > > Is it better to have two __weak empty versions here in this patch? > > Anyway, from the changelog it seems it's not mandatory for some ARCH to provide > the above two if one wants to turn this on, i.e., the two hooks can be empty and > the ARCH can just use the __weak version. I think this can be added by the first arch that needs memory attributes and also doesn't need one of these hooks. Or perhaps the x86 kvm_arch_pre_set_memory_attributes() could be made generic and thus that would be the __weak version. It's too early to tell, so it's better to leave the implementation to the architectures for now. Paolo