From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 20070C4167D for ; Thu, 2 Nov 2023 10:33:45 +0000 (UTC) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=VpsKDnE5; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=VpsKDnE5; dkim-atps=neutral Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4SLgFz4XgVz3cWq for ; Thu, 2 Nov 2023 21:33:43 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=VpsKDnE5; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=VpsKDnE5; dkim-atps=neutral Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=redhat.com (client-ip=170.10.133.124; helo=us-smtp-delivery-124.mimecast.com; envelope-from=pbonzini@redhat.com; receiver=lists.ozlabs.org) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4SLgDx3XZyz2ys9 for ; Thu, 2 Nov 2023 21:32:48 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698921165; 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=VpsKDnE5+vy1ipNe+M59ZzucbG66sJQWn+JflI6XAwfBn51go3p0AgXLW6aloPzkQGnaUz iQ0joLrNX8kq6AViEx1uVRowkudjD/YkTuSUc1xk8Wm9QHVwHO0PeBEl8Kwu3bDpk094KE OQJw7eVeBMGBGpTHA6FhvceFj5dzBUw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698921165; 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=VpsKDnE5+vy1ipNe+M59ZzucbG66sJQWn+JflI6XAwfBn51go3p0AgXLW6aloPzkQGnaUz iQ0joLrNX8kq6AViEx1uVRowkudjD/YkTuSUc1xk8Wm9QHVwHO0PeBEl8Kwu3bDpk094KE OQJw7eVeBMGBGpTHA6FhvceFj5dzBUw= Received: from mail-ej1-f70.google.com (mail-ej1-f70.google.com [209.85.218.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-658-9iyk1bCUPYuBfVR75NNd9w-1; Thu, 02 Nov 2023 06:32:41 -0400 X-MC-Unique: 9iyk1bCUPYuBfVR75NNd9w-1 Received: by mail-ej1-f70.google.com with SMTP id a640c23a62f3a-9d4b8735f48so54137666b.1 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=QlRKZr3rJf9BG3qqBI+lkjSjOSnXo7jxPnA7iYe9LZoIgiTHikwwqlFNadyeJJPXFK noZxmvtzXG4CzdFvpP52WJv4DS1UYO6F3MDcS8vz8YFAqnZ2ee+6AdKSYYR1EiYkhCE6 Ph+bB3bKv1RpihctgPi1zURd6faVV5mWJIXVfgCjscASw5odsn8TNlor3N/gPSzMk0Hx CE37OunB0TRM+uh3kgXT6wIra5JKIaHBnb7aTRzrvym+cOJc5s9LfqBOBMv6QIbmT7x2 lokiyOd/7NSeYxvxdXIXE6mQXxaebuTsZONwwWubdBRgYcpWwvx2EANDooKEiEjvXrl3 B9bQ== X-Gm-Message-State: AOJu0YykwVum5+bCBBKy+ZwGOU1IfzJXL0EsGBIGDsrA6vWm3WMCiH6f OcqXepQzqq+Gu3IqUddbpq45l2kxhww99expJy1Cicsw6fEyjpI77a4b9tThftzMPeKaTE2C715 /Ss2iM9GdxxPT/WPtCcCqJEFEqw== X-Received: by 2002:a17:907:70a:b0:9bd:9bfe:e410 with SMTP id xb10-20020a170907070a00b009bd9bfee410mr4134641ejb.72.1698921160410; Thu, 02 Nov 2023 03:32:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHdKWTZCjPNQtOILp1najbjDKUZt1gJ0TmclwPTaP4Nu9rDAFXiy/5MsPxAUlznxhN/2PedEg== 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 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" 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+Jm34qjswdrgsC5uxeVcLkBgWLmov2kMaMROT0YmFY6A3m1S P/kXmHDXxhe23gKb3dgwxUTpENDBGcfEzrzilWueOeUWiOcWuFOed/C3SyijBx3Av/lbCsHU Vx6pMycNTdzU1BuAroB+Y3mNEuW56Yd44jlInzG2UOwt9XjjdKkJZ1g0P9dwptwLEgTEd3Fo UdhAQyRXGYO8oROiuh+RZ1lXp6AQ4ZjoyH8WLfTL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R+vMY3i3Ip0e41zKx0CvlAhMOo6otaHmcxr35sWq1Jk tLkbn3wG+fPQCVudJJECvVQ//UAthSSEklA50QtD2sBkmQ14ZryEyTHQ+E42K3j2IUmOLriF dNr9NvE1QGmGyIcbw2NIVEBOK/GWxkS5+dmxM2iD4Jdaf2nSn3jlHjEXoPwpMs0KZsgdU0pP JQzMUMwmB1wM8JxovFlPYrhNT9MAEQEAAcLBMwQYAQIACQUCVEJx7gIbDAAKCRB+FRAMzTZp sadRDqCctLmYICZu4GSnie4lKXl+HqlLanpVMOoFNnWs9oRP47MbE2wv8OaYh5pNR9VVgyhD OG0AU7oidG36OeUlrFDTfnPYYSF/mPCxHttosyt8O5kabxnIPv2URuAxDByz+iVbL+RjKaGM GDph56ZTswlx75nZVtIukqzLAQ5fa8OALSGum0cFi4ptZUOhDNz1onz61klD6z3MODi0sBZN Aj6guB2L/+2ZwElZEeRBERRd/uommlYuToAXfNRdUwrwl9gRMiA0WSyTb190zneRRDfpSK5d usXnM/O+kr3Dm+Ui+UioPf6wgbn3T0o6I5BhVhs4h4hWmIW7iNhPjX1iybXfmb1gAFfjtHfL xRUr64svXpyfJMScIQtBAm0ihWPltXkyITA92ngCmPdHa6M1hMh4RDX+Jf1fiWubzp1voAg0 JBrdmNZSQDz0iKmSrx8xkoXYfA3bgtFN8WJH2xgFL28XnqY4M6dLhJwV3z08tPSRqYFm4NMP dRsn0/7oymhneL8RthIvjDDQ5ktUjMe8LtHr70OZE/TT88qvEdhiIVUogHdo4qBrk41+gGQh b906Dudw5YhTJFU3nC6bbF2nrLlB4C/XSiH76ZvqzV0Z/cAMBo5NF/w= In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "kvm@vger.kernel.org" , "david@redhat.com" , "linux-mips@vger.kernel.org" , "linux-mm@kvack.org" , "mic@digikod.net" , "chao.p.peng@linux.intel.com" , "linux-riscv@lists.infradead.org" , "isaku.yamahata@gmail.com" , "Li, Xiaoyao" , "Wang, Wei W" , "tabba@google.com" , "linux-arm-kernel@lists.infradead.org" , "mail@maciej.szmigiero.name" , "michael.roth@amd.com" , "ackerleytng@google.com" , "kvmarm@lists.linux.dev" , "vbabka@suse.cz" , "Yamahata, Isaku" , "qperret@google.com" , "linux-kernel@vger.kernel.org" , "dmatlack@google.com" , "jarkko@kernel.org" , "yu.c.zhang@linux.intel.com" , "kirill.shutemov@linux.intel.com" , "kvm-riscv@lists.infradead.org" , "linux-fsdevel@vger.kernel.org" , "liam.merwick@oracle.com" , "Annapurve, Vishal" , "linuxppc-dev@lists.ozlabs.org" , "Xu, Yilun" , "amoorthy@google.com" Errors-To: linuxppc-dev-bounces+linuxppc-dev=archiver.kernel.org@lists.ozlabs.org Sender: "Linuxppc-dev" 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