Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4562649rdb; Fri, 15 Sep 2023 06:11:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH6zMTgrqFqYyJvPCm1K9ApWRxxGxe6CzBECvxJQz0IM3oVQ91vQwhZUYf+Lm0YFU6qZ30O X-Received: by 2002:a17:90b:350e:b0:268:5620:cfc7 with SMTP id ls14-20020a17090b350e00b002685620cfc7mr1386628pjb.30.1694783483335; Fri, 15 Sep 2023 06:11:23 -0700 (PDT) Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id t11-20020a17090a5d8b00b00263a923c189si3233243pji.100.2023.09.15.06.11.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Sep 2023 06:11:23 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=F+4s1Bad; arc=fail (signature failed); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id C2DDC833DE14; Fri, 15 Sep 2023 00:00:34 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232574AbjIOHAY (ORCPT + 99 others); Fri, 15 Sep 2023 03:00:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43368 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232556AbjIOHAX (ORCPT ); Fri, 15 Sep 2023 03:00:23 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B93632710; Fri, 15 Sep 2023 00:00:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1694761214; x=1726297214; h=date:from:to:cc:subject:message-id:reply-to:references: in-reply-to:mime-version; bh=M9GjEY03fiX8KCiHZYYoK8RDlfaIcSXwjSnIoXGA9B0=; b=F+4s1Badp3OisJ7Tq8pBshXzY8t8e7eXbRv25kF5/m244fsJFNvD4yBW 8+ORa4I7x+uxZl0U4Ja78ylxzzlphRpVwHtpG5JhjAg9cMKlLRXcvwK4M HbxADDh/ZP+UrGoNyXHQh2Zt+/mwGRIPGN0oHozIN3CfAcAiy7heUfffB GEiMxWHpYaIfi/o3zx6gVmtro2WFg9tvrBuobf6hvEpK8qRZogvfs9fWb p/jqu+1eHpcq5DqDav4KdvvZd7ietm99QFE0um0Uu8WfPZzOy55vYdHtD n1Bzc5lO5hZOvgx9MQ9RF6Nk0DvtS0Q34dXqMqC6jpE073NHTkahYayiS A==; X-IronPort-AV: E=McAfee;i="6600,9927,10833"; a="376516846" X-IronPort-AV: E=Sophos;i="6.02,148,1688454000"; d="scan'208";a="376516846" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Sep 2023 00:00:13 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10833"; a="991735383" X-IronPort-AV: E=Sophos;i="6.02,148,1688454000"; d="scan'208";a="991735383" Received: from orsmsx602.amr.corp.intel.com ([10.22.229.15]) by fmsmga006.fm.intel.com with ESMTP/TLS/AES256-GCM-SHA384; 15 Sep 2023 00:00:11 -0700 Received: from orsmsx611.amr.corp.intel.com (10.22.229.24) by ORSMSX602.amr.corp.intel.com (10.22.229.15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.32; Fri, 15 Sep 2023 00:00:11 -0700 Received: from orsmsx610.amr.corp.intel.com (10.22.229.23) by ORSMSX611.amr.corp.intel.com (10.22.229.24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.32; Fri, 15 Sep 2023 00:00:10 -0700 Received: from orsedg603.ED.cps.intel.com (10.7.248.4) by orsmsx610.amr.corp.intel.com (10.22.229.23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.32 via Frontend Transport; Fri, 15 Sep 2023 00:00:10 -0700 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (104.47.55.173) by edgegateway.intel.com (134.134.137.100) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.32; Fri, 15 Sep 2023 00:00:10 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hr5wmzI0XkgBmwYmMcO3hykqhVemAjzZovt8sg8PZveqmI75Fg/g2mYsA9p79E6dz034XQZJLYZtHJQ4/iXNIiqrukfHkda3dgEK+WN8WUhGnaeCrO+qUULyI+Qk3JL0i/C70VxTkiJi5cXLojEfbOsYMhhGuaPfZSLmJKw9KiXwLk93FTvAB9LmPHUbUWcAIZAha0Cz7GyUpMeuyscwjmaX2+wYlnUncuJuKHX0YFhRrkvKxs/UpianhxzrVmQGtcOKhAS3g00i7x2WiqgvACFCI64I+puRFjtrlS+OC3MZ9Rh8QMeUrn+DG6WT9va3+YW9CFZ6TqtxXzzSmen6dw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=EBtuz6enzGqcqo0B/WUQJ5kcTFtSVAZy38ivF/2KSJo=; b=KNiVtyOVrTw0zRSlye1uOlomByDpzvNtXNjrInEflsGY+iE1bgs+cBKQ56CRB8lDjx3XLFPCKjVr/DACQKH8HZtuGoIoWrXn261roNDvEJLdIO7s7tcyWH2La6NdUyuLh8O4P+Eq3ZMzA4Lg/DJMi3OV4Y/JHdJ6jCLEsZGDRYYw2+0NlpIVR09nqUQTJ3xHCAo6U8K1yKG1va7bbP980K5+pdFHgIVK4yo0XRO1GHgDf+mQr8YrY2LTlYluRSrg7AcJDVw8FodAoWvEPYn8L1bNf5K013fNd/BC3i2EhMu6jcBLb6kl5y/RWsOmVW/23ZFuteoY7r9O6wYdexYkDQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=intel.com; Received: from DS7PR11MB5966.namprd11.prod.outlook.com (2603:10b6:8:71::6) by MN0PR11MB6181.namprd11.prod.outlook.com (2603:10b6:208:3c7::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.21; Fri, 15 Sep 2023 07:00:08 +0000 Received: from DS7PR11MB5966.namprd11.prod.outlook.com ([fe80::5e79:2d52:51e:f602]) by DS7PR11MB5966.namprd11.prod.outlook.com ([fe80::5e79:2d52:51e:f602%6]) with mapi id 15.20.6768.029; Fri, 15 Sep 2023 07:00:07 +0000 Date: Fri, 15 Sep 2023 14:32:06 +0800 From: Yan Zhao 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" , , , , , , , , , , , , Chao Peng , Fuad Tabba , Jarkko Sakkinen , Anish Moorthy , Yu Zhang , Isaku Yamahata , Xu Yilun , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , "Liam Merwick" , Isaku Yamahata , "Kirill A . Shutemov" Subject: Re: [RFC PATCH v12 11/33] KVM: Introduce per-page memory attributes Message-ID: Reply-To: Yan Zhao References: <20230914015531.1419405-1-seanjc@google.com> <20230914015531.1419405-12-seanjc@google.com> Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline In-Reply-To: <20230914015531.1419405-12-seanjc@google.com> X-ClientProxiedBy: SG2PR01CA0184.apcprd01.prod.exchangelabs.com (2603:1096:4:189::9) To DS7PR11MB5966.namprd11.prod.outlook.com (2603:10b6:8:71::6) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS7PR11MB5966:EE_|MN0PR11MB6181:EE_ X-MS-Office365-Filtering-Correlation-Id: 4301786c-6c79-4955-7eee-08dbb5b9653e X-LD-Processed: 46c98d88-e344-4ed4-8496-4ed7712e255d,ExtAddr X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: pAyYfDDNw0hfV7ILb2mMMJitM8RhBJAsF/lboEXMiHuQPiAn6mvGwke/qaCIAOX6MiOtoz2ZDjrLiMc5D2nuL4eSTQr1be44ydcJi5VlD/IzT48+51OQG/lI6gV1bhrIBup9hyuBlhP0fauOAXC7rd2CddOx8DkcYZlCawY0m/kPFXGoZlmwAdSIt8Ctk1ReL3nijKRGZTaUjYAc9o+mPP9f6HzQ2XwJfq6tJuEx6gLErrVuoc3PuTC6QumHqeo2Psxo+IVqyMVXGeY+S1GFpPeaBLj+gbovCIiXLvmRh3rUyeKiOPpzl9TZ0qHtvayIqCy3htEIPH1yfBJqnGS0Zf4ECcApsep5DIuzqS74qc0vo4xxPpeEsPAte62Ia3uvoqSJweQQavRrDLUgO+EnQhXAPhPqKarGK8PHEfFLWUbaMuBvaIQikQuwqcAUlcp4ntuFAI8x5MbxFbPN6wtgJBBPCjdPaONznnnSh1zO5qoErMbKDdBV6sZi+TIQU1nStN7Wpffklnxtclmu2CbTMEoCAhsugxldikH7/ZwkQEsWed06kVriGJ/hOJcsei/B X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS7PR11MB5966.namprd11.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(346002)(136003)(366004)(396003)(39860400002)(376002)(451199024)(1800799009)(186009)(7416002)(2906002)(7406005)(8676002)(5660300002)(41300700001)(4326008)(3450700001)(478600001)(8936002)(54906003)(66476007)(66556008)(66946007)(316002)(6916009)(6512007)(6486002)(6506007)(26005)(66899024)(38100700002)(82960400001)(86362001)(83380400001);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?cJ/xVeZqbZbggo9Tjs7RGGngV/kc34E/K19QCIH+UGGk5dpgfayBtzcjmJ4J?= =?us-ascii?Q?hRi1GfByQ1/sMkhg6cmDG+BTAKhq8vW91KaYutCDfTswfsJnddx9VO77Hh7t?= =?us-ascii?Q?QyPHP4ZWHpibXQOyXYjzbDsrYANxr4chxgM5mfpE30OJgDRI90dduk/RLCKi?= =?us-ascii?Q?IkQbRUVAcM8gADdin2olj3dNhwsajzgipHF73IzcxqR20IheDX9X2GXwbwuO?= =?us-ascii?Q?i3hBEE4XgUmrM8WReHqBvGkd6X9nCVocugi3lvwzIxvjvONUNa/40rafY5S9?= =?us-ascii?Q?SalJlqw8/P5romUexK1UmWcDiA2swyUJcHteFRYZ4d9abFEe7lXgSV13Zes9?= =?us-ascii?Q?zTZp6j/6+CHpD2UqT+bAkNYty0YsD7B6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UkiPmhFcuF8WY4J61Z8IEUGp2ySvOguTd/+gWHTTryyFUNR9n?= =?us-ascii?Q?WaoEA8wikUm3FBgNXEeSAOOD4mgt04s9dZnjJpMQamfQXDn0d63XigeZGCwn?= =?us-ascii?Q?Qk8RwMbE/ZOEoOYW9NvF+/wUlJ9pWXAukTa7aaqxiW+Cbfhv8/JljPma2pdC?= =?us-ascii?Q?e4e/vL/QRFuhRXYVtqUdYCNaFyFd+UoNs7zqQTkIxv7C8vnb4TTD7JgNi5z2?= =?us-ascii?Q?zvwtr1g1lY/WgjCIRd47Hz6HxzGvuvJmKUnVHM4v9k7J1wXoTBFA9iNKJd0v?= =?us-ascii?Q?lgCVoW/Wo06h+F5JzTn96s4nfYvQFOAV48BiBITy?= X-MS-Exchange-CrossTenant-Network-Message-Id: 4301786c-6c79-4955-7eee-08dbb5b9653e X-MS-Exchange-CrossTenant-AuthSource: DS7PR11MB5966.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Sep 2023 07:00:07.6713 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: m06sK2+7ALEC7pkBv7zLGGHvlo71jRH/OwYBkf30C9UZOGi4hGOe5o2sCizMV6ce4HTBpLdB/ZGyMmYQnsLmJw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR11MB6181 X-OriginatorOrg: intel.com X-Spam-Status: No, score=-0.9 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 autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.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 (morse.vger.email [0.0.0.0]); Fri, 15 Sep 2023 00:00:35 -0700 (PDT) On Wed, Sep 13, 2023 at 06:55:09PM -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. > ... >> +bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end, > + unsigned long attrs) > +{ > + XA_STATE(xas, &kvm->mem_attr_array, start); > + unsigned long index; > + bool has_attrs; > + void *entry; > + > + rcu_read_lock(); > + > + if (!attrs) { > + has_attrs = !xas_find(&xas, end); > + goto out; > + } > + > + has_attrs = true; > + for (index = start; index < end; index++) { > + do { > + entry = xas_next(&xas); > + } while (xas_retry(&xas, entry)); > + > + if (xas.xa_index != index || xa_to_value(entry) != attrs) { Should "xa_to_value(entry) != attrs" be "!(xa_to_value(entry) & attrs)" ? > + has_attrs = false; > + break; > + } > + } > + > +out: > + rcu_read_unlock(); > + return has_attrs; > +} > + ... > +/* Set @attributes for the gfn range [@start, @end). */ > +static int kvm_vm_set_mem_attributes(struct kvm *kvm, gfn_t start, gfn_t end, > + unsigned long attributes) > +{ > + struct kvm_mmu_notifier_range pre_set_range = { > + .start = start, > + .end = end, > + .handler = kvm_arch_pre_set_memory_attributes, > + .on_lock = kvm_mmu_invalidate_begin, > + .flush_on_ret = true, > + .may_block = true, > + }; > + struct kvm_mmu_notifier_range post_set_range = { > + .start = start, > + .end = end, > + .arg.attributes = attributes, > + .handler = kvm_arch_post_set_memory_attributes, > + .on_lock = kvm_mmu_invalidate_end, > + .may_block = true, > + }; > + unsigned long i; > + void *entry; > + int r = 0; > + > + entry = attributes ? xa_mk_value(attributes) : NULL; Also here, do we need to get existing attributes of a GFN first ? > + mutex_lock(&kvm->slots_lock); > + > + /* Nothing to do if the entire range as the desired attributes. */ > + if (kvm_range_has_memory_attributes(kvm, start, end, attributes)) > + goto out_unlock; > + > + /* > + * Reserve memory ahead of time to avoid having to deal with failures > + * partway through setting the new attributes. > + */ > + for (i = start; i < end; i++) { > + r = xa_reserve(&kvm->mem_attr_array, i, GFP_KERNEL_ACCOUNT); > + if (r) > + goto out_unlock; > + } > + > + kvm_handle_gfn_range(kvm, &pre_set_range); > + > + for (i = start; i < end; i++) { > + r = xa_err(xa_store(&kvm->mem_attr_array, i, entry, > + GFP_KERNEL_ACCOUNT)); > + KVM_BUG_ON(r, kvm); > + } > + > + kvm_handle_gfn_range(kvm, &post_set_range); > + > +out_unlock: > + mutex_unlock(&kvm->slots_lock); > + > + return r; > +}