Received: by 2002:a05:6500:1b8f:b0:1fa:5c73:8e2d with SMTP id df15csp496692lqb; Wed, 29 May 2024 01:51:36 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVowwGJFiqs+JBifCH0tpp6OWwqrptvU0Nc6Qw8KBJMP6rhQHbBtseeKTIFDEU8uON+J9KVMWZ/vlk2UdRoY2LWrt66URc/nwL2OV8GIQ== X-Google-Smtp-Source: AGHT+IGrOUZe9yx+F+WLdW3HI9rAfYx9ulYM8Hfde3PSIv4QRC8UXIrTVYkCfyR+Xaey9vuRLmPL X-Received: by 2002:a17:90a:fd17:b0:2bd:9319:3da1 with SMTP id 98e67ed59e1d1-2bf5ef1c8bamr15417722a91.25.1716972695858; Wed, 29 May 2024 01:51:35 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716972695; cv=pass; d=google.com; s=arc-20160816; b=qvScmSfJZMibvK3sWr5dL168GG92gRFQa9afLzjCRkX3jaYWcIvmp6k2Y8ZijQeG+1 kl29E6+RYlJ2ndmn7IVn8SVnZksVkn4gB0GuiDGolaAM/ZTsOArijhP1PyImge3jpO/F qvVe5tgG4tw71NBLtilDJYuli7UAV1UCDdHSd/JG3dXApHhE2Rvukvm4bcFCAqKi2O6D Bk32OKBwgjxWN4/NWYrrZUwKhZkJEwmkt58rk/H2yY686S2kiEhO90cwQKi9P/XjPv9L k7TnwT067Pc2EXpC9v+ED6RZVNKMAXwcafFbpwrBVJRJ+s25Rqw+u6ORVkgRD90ReCUZ BScQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=kBqE4j5z86E2dnbIeeWXjkmm4QgYtkEAv9tnNpqDh2I=; fh=IHw9CZInyDz4eeukB+MeGIfxKdckQtGjS/8RXZVRppE=; b=XozcTb0PeSugHsuiPScc0zcnlNruppSIohp/HK/Y0st8mdCj9vw2yI+sGaJpeEfMRx rhgaA5/NMLNPpY0sszR4i5H7YQNOSebAji4mljLdzQZOqpDjV5mZj+HyFfPijk+ovaHK t/yUiUMIptj/pIuKLjDKnS+FX2InAQpEehluACA4r1EQU0LjPJ4pteRIKsSNjHfGDgSz NN8pAgaf0OmOBRHU84fpA014IWYn7Ug69wJTJmfhzq9GlXYy0vQtNhpV82ZfalUpQ/pk 2lMmz7THhFUWpl1FhsNF1QOY3PPBGatq3GsEbMzwmD+M6/6XPdLC1N+r36BZGHjf+5RM tC7g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b="tn4BW0K/"; arc=pass (i=1 spf=pass spfdomain=mediatek.com dkim=pass dkdomain=mediatek.com dmarc=pass fromdomain=mediatek.com); spf=pass (google.com: domain of linux-kernel+bounces-193770-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-193770-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id 98e67ed59e1d1-2bf5fe61c4asi9384946a91.157.2024.05.29.01.51.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 29 May 2024 01:51:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-193770-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b="tn4BW0K/"; arc=pass (i=1 spf=pass spfdomain=mediatek.com dkim=pass dkdomain=mediatek.com dmarc=pass fromdomain=mediatek.com); spf=pass (google.com: domain of linux-kernel+bounces-193770-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-193770-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 0916128959B for ; Wed, 29 May 2024 08:46:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1ECA2180A7E; Wed, 29 May 2024 08:43:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=mediatek.com header.i=@mediatek.com header.b="tn4BW0K/" Received: from mailgw01.mediatek.com (unknown [60.244.123.138]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 50207167D92; Wed, 29 May 2024 08:42:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=60.244.123.138 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716972174; cv=none; b=OUQgIRNfDo1GJk+L6PzULAwqWG1rdHEOlxJCyLogipXDEKv3emNYMCVWe+96q4q7/GxfbSBO2CgycSEttPYR2SNs571dkmf9syLKFlmDIddlhY1JFB/cC/E9olJ5X83StL00RvDhI4a7XTnoyh1C2aDegQ9W6PxwSWwtBfd9mj8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716972174; c=relaxed/simple; bh=qfdBbe69nDUVN2YQQNsj3wTHPQ8kwV5tGcY7tpAr9og=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=XxsgEDof1SI+ZjwBYbn1118rRbAuZx7EGgdSTw3zb4GAG8HvC4nui+fW2UErfN8dOXgqlfM1ZT8+fJ9J7MnUvM7mGlIJ1WPrxkpVG96gdxmnOuid03wRr8YxPslb1XBsGsuYRqRfdXcLYHXkooTGmOQEL/vqcttl0HG5DtmzqdA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mediatek.com; spf=pass smtp.mailfrom=mediatek.com; dkim=pass (1024-bit key) header.d=mediatek.com header.i=@mediatek.com header.b=tn4BW0K/; arc=none smtp.client-ip=60.244.123.138 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mediatek.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mediatek.com X-UUID: 69c868421d9711ef8c37dd7afa272265-20240529 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:CC:To:From; bh=kBqE4j5z86E2dnbIeeWXjkmm4QgYtkEAv9tnNpqDh2I=; b=tn4BW0K/To2qrjKMOWd4F8/+EUTcJX9kckxJ9TZJ5N0IFJ50kRCZDAp9hrNAKRniMDRIQPG4WGBO7iiJNII+86rlyx8OnwwlWQF7csbW5mtQHyNnwQXV6p7MeKJctxWTUg5apFsfaVG8GOm2VLT7j1vk5dVPu+xrn5e/4llw8wk=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.39,REQID:27052ddd-8306-480e-a91d-16720015cdf8,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-25 X-CID-META: VersionHash:393d96e,CLOUDID:aaf35b93-e2c0-40b0-a8fe-7c7e47299109,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:nil,Content:0,EDM:-3,IP:nil,U RL:11|1,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,LES :1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR,TF_CID_SPAM_ULN X-UUID: 69c868421d9711ef8c37dd7afa272265-20240529 Received: from mtkmbs11n2.mediatek.inc [(172.21.101.187)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1656790647; Wed, 29 May 2024 16:42:42 +0800 Received: from mtkmbs11n1.mediatek.inc (172.21.101.185) by mtkmbs11n1.mediatek.inc (172.21.101.185) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Wed, 29 May 2024 16:42:41 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkmbs11n1.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.26 via Frontend Transport; Wed, 29 May 2024 16:42:41 +0800 From: Liju-clr Chen To: Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jonathan Corbet , Catalin Marinas , Will Deacon , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Richard Cochran , Matthias Brugger , AngeloGioacchino Del Regno , Liju-clr Chen , Yingshiuan Pan , Ze-yu Wang CC: , , , , , , , David Bradil , Trilok Soni , Shawn Hsiao , PeiLun Suei , Chi-shen Yeh , Kevenny Hsieh Subject: [PATCH v11 14/21] virt: geniezone: Optimize performance of protected VM memory Date: Wed, 29 May 2024 16:42:32 +0800 Message-ID: <20240529084239.11478-15-liju-clr.chen@mediatek.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20240529084239.11478-1-liju-clr.chen@mediatek.com> References: <20240529084239.11478-1-liju-clr.chen@mediatek.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain X-MTK: N From: Yi-De Wu From: "Yingshiuan Pan" The memory protection mechanism performs better with batch operations on memory pages. To leverage this, we pre-allocate memory for VMs that are set to protected mode. As a result, the memory protection mechanism can proactively protect the pre-allocated memory in advance through batch operations, leading to improved performance during VM booting. Signed-off-by: Yingshiuan Pan Signed-off-by: Jerry Wang Signed-off-by: Liju Chen Signed-off-by: Yi-De Wu --- arch/arm64/geniezone/vm.c | 127 ++++++++++++++++++++++++++ include/linux/soc/mediatek/gzvm_drv.h | 1 + 2 files changed, 128 insertions(+) diff --git a/arch/arm64/geniezone/vm.c b/arch/arm64/geniezone/vm.c index 00c74a4bcfd7..db16716f5b8d 100644 --- a/arch/arm64/geniezone/vm.c +++ b/arch/arm64/geniezone/vm.c @@ -11,6 +11,8 @@ #include #include "gzvm_arch_common.h" +#define PAR_PA47_MASK GENMASK_ULL(47, 12) + /** * gzvm_hypcall_wrapper() - the wrapper for hvc calls * @a0: arguments passed in registers 0 @@ -201,6 +203,126 @@ static int gzvm_vm_ioctl_get_pvmfw_size(struct gzvm *gzvm, return 0; } +/** + * fill_constituents() - Populate pa to buffer until full + * @consti: Pointer to struct mem_region_addr_range. + * @consti_cnt: Constituent count. + * @max_nr_consti: Maximum number of constituent count. + * @gfn: Guest frame number. + * @total_pages: Total page numbers. + * @slot: Pointer to struct gzvm_memslot. + * + * Return: how many pages we've fill in, negative if error + */ +static int fill_constituents(struct mem_region_addr_range *consti, + int *consti_cnt, int max_nr_consti, u64 gfn, + u32 total_pages, struct gzvm_memslot *slot) +{ + u64 pfn = 0, prev_pfn = 0, gfn_end = 0; + int nr_pages = 0; + int i = -1; + + if (unlikely(total_pages == 0)) + return -EINVAL; + gfn_end = gfn + total_pages; + + while (i < max_nr_consti && gfn < gfn_end) { + if (pfn == (prev_pfn + 1)) { + consti[i].pg_cnt++; + } else { + i++; + if (i >= max_nr_consti) + break; + consti[i].address = PFN_PHYS(pfn); + consti[i].pg_cnt = 1; + } + prev_pfn = pfn; + gfn++; + nr_pages++; + } + if (i != max_nr_consti) + i++; + *consti_cnt = i; + + return nr_pages; +} + +/** + * gzvm_vm_populate_mem_region() - Iterate all mem slot and populate pa to + * buffer until it's full + * @gzvm: Pointer to struct gzvm. + * @slot_id: Memory slot id to be populated. + * + * Return: 0 if it is successful, negative if error + */ +int gzvm_vm_populate_mem_region(struct gzvm *gzvm, int slot_id) +{ + struct gzvm_memslot *memslot = &gzvm->memslot[slot_id]; + struct gzvm_memory_region_ranges *region; + int max_nr_consti, remain_pages; + u64 gfn, gfn_end; + u32 buf_size; + + buf_size = PAGE_SIZE * 2; + region = alloc_pages_exact(buf_size, GFP_KERNEL); + if (!region) + return -ENOMEM; + + max_nr_consti = (buf_size - sizeof(*region)) / + sizeof(struct mem_region_addr_range); + + region->slot = memslot->slot_id; + remain_pages = memslot->npages; + gfn = memslot->base_gfn; + gfn_end = gfn + remain_pages; + + while (gfn < gfn_end) { + int nr_pages; + + nr_pages = fill_constituents(region->constituents, + ®ion->constituent_cnt, + max_nr_consti, gfn, + remain_pages, memslot); + + if (nr_pages < 0) { + pr_err("Failed to fill constituents\n"); + free_pages_exact(region, buf_size); + return -EFAULT; + } + + region->gpa = PFN_PHYS(gfn); + region->total_pages = nr_pages; + remain_pages -= nr_pages; + gfn += nr_pages; + + if (gzvm_arch_set_memregion(gzvm->vm_id, buf_size, + virt_to_phys(region))) { + pr_err("Failed to register memregion to hypervisor\n"); + free_pages_exact(region, buf_size); + return -EFAULT; + } + } + free_pages_exact(region, buf_size); + + return 0; +} + +static int populate_all_mem_regions(struct gzvm *gzvm) +{ + int ret, i; + + for (i = 0; i < GZVM_MAX_MEM_REGION; i++) { + if (gzvm->memslot[i].npages == 0) + continue; + + ret = gzvm_vm_populate_mem_region(gzvm, i); + if (ret != 0) + return ret; + } + + return 0; +} + /** * gzvm_vm_ioctl_cap_pvm() - Proceed GZVM_CAP_PROTECTED_VM's subcommands * @gzvm: Pointer to struct gzvm. @@ -222,6 +344,11 @@ static int gzvm_vm_ioctl_cap_pvm(struct gzvm *gzvm, case GZVM_CAP_PVM_SET_PVMFW_GPA: fallthrough; case GZVM_CAP_PVM_SET_PROTECTED_VM: + /* + * To improve performance for protected VM, we have to populate VM's memory + * before VM booting + */ + populate_all_mem_regions(gzvm); ret = gzvm_vm_arch_enable_cap(gzvm, cap, &res); return ret; case GZVM_CAP_PVM_GET_PVMFW_SIZE: diff --git a/include/linux/soc/mediatek/gzvm_drv.h b/include/linux/soc/mediatek/gzvm_drv.h index 982463eea4f6..54ac91670611 100644 --- a/include/linux/soc/mediatek/gzvm_drv.h +++ b/include/linux/soc/mediatek/gzvm_drv.h @@ -159,6 +159,7 @@ int gzvm_vm_ioctl_arch_enable_cap(struct gzvm *gzvm, int gzvm_gfn_to_hva_memslot(struct gzvm_memslot *memslot, u64 gfn, u64 *hva_memslot); +int gzvm_vm_populate_mem_region(struct gzvm *gzvm, int slot_id); int gzvm_vm_ioctl_create_vcpu(struct gzvm *gzvm, u32 cpuid); int gzvm_arch_vcpu_update_one_reg(struct gzvm_vcpu *vcpu, __u64 reg_id, -- 2.18.0