Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp1318379rwb; Wed, 26 Jul 2023 10:35:18 -0700 (PDT) X-Google-Smtp-Source: APBJJlGd2yVNTUeeMu4K0xjIZEQTRKi5NrgB2w3muNdcSkUiqNNR8wktPPYKpV80TBIKM6ZDLFI0 X-Received: by 2002:a17:902:bf46:b0:1b8:8670:541 with SMTP id u6-20020a170902bf4600b001b886700541mr1908994pls.25.1690392917949; Wed, 26 Jul 2023 10:35:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690392917; cv=none; d=google.com; s=arc-20160816; b=iOUDj7DC8d54SbBEdnIVHOZh5DQ55wjs/w8X5AsKbGf6B6oUG5jQ1DB7NZmkLRW3Di hvfmqpcbr+m/od8Y/osa+wHRiXUI3XeI9yi6r3qNc27LY1j3eCBHL7M/028PpWrKRFGm MNrm2Xuda4jsj7SQGcIjtxpR7A0SWyR4C46iLPoueaSr7ztg4TkqOU01bzFsagIQwO9s 8RwCIPxkR1SEh2fAe5td+IVJbb8L2TDhl7t70hNynYxNclzRR/S5rOYNYQrFP4fhP5mH mxgIfU2Yr/MQ5r0lF0nhp5APS3W1yYUFd+1wwoo20xBJxmUsSEHNeCSJCG8Ycu6576pi F8Cw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=x1RCz9AGh0t/+COzjYLcha5fkSMq/GNrC4CuEfgGTfc=; fh=zpEO9IDsCz5DickMM/FFTFEQt08AaAMLhFQ7lg5qPII=; b=C5+MlRJMlUa9tT0aXJeJHH9t4R4TDYyVn5+0B7JHpXdG07cHldIvpGc+ZYeQGEEdkx VP8j37Jawq9jjJKWvj85dYK3cI7pICOuRw8Gr+c8wMcg+K7Ap0NPIa9FrtukOB+CjgKc pjnmgwmEt+8xwhTRoO3I9EHysgeZV/TyCGaCBE7QTCkvqoYxpMhC2K5VBBFtTZM/e5gR vTIZ4FKCKRO1aO8oWQOiWgyCp9CK3HVAjG0pEcKVfSRMKtstlFwuRVLeN1YPiwy5wgAC NPP50Oj9TUuhHBRr8PZj83HcW1d5Aev6vX9y6ga6Rt3RZXsIZL3J4CMlz763zz8MJQTV OR8g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=ypSxifbz; 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 z10-20020a170903018a00b001b896f8ae83si14305378plg.110.2023.07.26.10.35.05; Wed, 26 Jul 2023 10:35:17 -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=@google.com header.s=20221208 header.b=ypSxifbz; 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 S234007AbjGZQAL (ORCPT + 99 others); Wed, 26 Jul 2023 12:00:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58134 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235009AbjGZP75 (ORCPT ); Wed, 26 Jul 2023 11:59:57 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E936E2109 for ; Wed, 26 Jul 2023 08:59:55 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-d10792c7582so3434713276.3 for ; Wed, 26 Jul 2023 08:59:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690387195; x=1690991995; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=x1RCz9AGh0t/+COzjYLcha5fkSMq/GNrC4CuEfgGTfc=; b=ypSxifbz+3xr2TyQi3H8IoM4jyz7RGQWDnGcHi1NC0Po8RTEHh8icWGPEeM5z47awl loHCPZy4rtZMbUI9RqdI0QLfF+ad98/C7r2wxAa7ZpGCgkUVuuHTSwwJ1OUjNqHnXY8X 84BBat8mly4SBV0f6KqsOvHk8cSJA6wdZ1HsQOFr2IirR9RtsavVXkabaGLdXW2RSaSS UJmT6LqQAsULk7Nz99DvDaEAqSJ5Xa6WPtogY6EBxsD6vOblRb8mRKx+BIAVyZOlnkaE pEMJT5wcLP195pWDDcCypB4wv+dW/iWZkWl4YuLww/x6MLh6go/yMgsbLUu6B2dDA/yw w4bA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690387195; x=1690991995; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=x1RCz9AGh0t/+COzjYLcha5fkSMq/GNrC4CuEfgGTfc=; b=dv6qMB8GaRi59fNZ9GvUaZC9oGLUUJiLcxAhKd4LvGiee3LKbKFZjdUaRlHco0ogzO E+lKcj+ieDTHX4oXHkWS3MEtoEfgRe6BvFdhbsjYoxwz/lVumY0DpsRxG0H3Kkw8dSk/ SdzkH8j+4/iu/f//J1728sSDyUVj7mFY6CpJzvcQCWjBoynfjAcOhPoughlcaQp+o/Og Py1yGJgQ8z1AfqfmIywXctbaEOgoKYx7URRRExKa7vsz9o+ev8kf1nvOKwdpaymEE8KU Gt5XiJAvneqKo7sEMI5H13SXhaaIrpEu78ZMR2UWCT86ZkvDRLO1CePOh9LkEPhzO1XM +nKw== X-Gm-Message-State: ABy/qLZMAh2Q0jwDuFy2RIxTb7s/XgcRjojwbR3U573QFJYdoZzrvom4 jGPLCGlNAnNysSfukfRHFcEClt4DbjE= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:99c8:0:b0:d1c:e102:95a5 with SMTP id q8-20020a2599c8000000b00d1ce10295a5mr15014ybo.7.1690387195164; Wed, 26 Jul 2023 08:59:55 -0700 (PDT) Date: Wed, 26 Jul 2023 08:59:53 -0700 In-Reply-To: Mime-Version: 1.0 References: <20230718234512.1690985-1-seanjc@google.com> <20230718234512.1690985-9-seanjc@google.com> Message-ID: Subject: Re: [RFC PATCH v11 08/29] KVM: Introduce per-page memory attributes From: Sean Christopherson To: Xu Yilun 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" Content-Type: text/plain; charset="us-ascii" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL 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 Mon, Jul 24, 2023, Xu Yilun wrote: > On 2023-07-18 at 16:44:51 -0700, Sean Christopherson wrote: > > @@ -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? Yeah. It _shoudn't_ matter, but there's no reason not keep things tidy and consistent. > 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). The former, because init_srcu_struct() can fail (allocates memory), whereas xa_init() is a "pure" initialization routine. > > 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; Yes, that should work. Chao, am I missing something? Or did we just end up with unnecessarly convoluted code as things evolved? > > + > > + if (WARN_ON_ONCE(start == end)) > > + return -EINVAL; > > Also, is this check possible to be hit? Maybe remove it? It should be impossible to, hence the WARN. I added the check for two reasons: (1) to help document that end is exclusive, and (2) to guard against future bugs.