Received: by 2002:a05:7412:d1aa:b0:fc:a2b0:25d7 with SMTP id ba42csp255468rdb; Mon, 29 Jan 2024 00:46:46 -0800 (PST) X-Google-Smtp-Source: AGHT+IG6qbB9UorlwtQReOL2lAM0DXGxFSd4VdcoRB4xkUqr3E/I/d0oKZ4YrtKgirrwaIQtvOuc X-Received: by 2002:a17:906:f0c9:b0:a35:e4e4:a5f6 with SMTP id dk9-20020a170906f0c900b00a35e4e4a5f6mr443965ejb.37.1706518006136; Mon, 29 Jan 2024 00:46:46 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706518006; cv=pass; d=google.com; s=arc-20160816; b=WbehaRgjcfTYpBvZaNjQJgUUbls3ut2gaRYJSU3Y8dsXsP2B7BWZucT51NFbBq+sSy NRqj+VXrUFFNw5PxEb11G63LhqbP72HollvjJ7g7UkFTIt2AanjGW7SmcGViRnmrRyt4 7wCZKuxs7X5vnRaxBSqhOgB+Yw45S2qMaZZ+NhAc76phsxoK4V54dRTLzUKfD+PUdsX/ gwhWEHIiQ5Hg+uvedCfnEu+WN7FCUOCeRe4u97qhPkEBZn4ppfFDZ7/1MTwuy8RSiS89 ehLpNMZsy9CznkfTo8aJ0Qy8HJ/rGe1EzNlJCcWMaSW5LyRV+2WSmK4oNmvM1Pw9BD8K gVmg== 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=rM4PeM5hdGK8vkQCs8uiZPUIqfcZ2G8VIqcv0jjx+i8=; fh=hH5L/ld0CjFhRvhMbGDzE2BAF3OQ89jjQIXHqlUV0cw=; b=o/HBxV7svYmbo/VOmKGjDfL54/3/yNK6schq3WbU4guAmhK7f9rjG6lffTHWgxYw3T 2YCf/DEVMwTssVyBAvAdLFXLmr0nhEJYbm0ZFPQr7fsjhx1b9ecPMSsdhx8b/Y3qrvvk An+4eQJg7rlKgo97ZnV4uOQEkhcn3xiSTqnkDskqd5wrxdB4ME55YkVdsocWZMF1URfE yLmEI7l6oGHu3JITGDDzWvY+IkIfd2oNEXPoZxKpMSVNqjazD2uv66yI64hLiKMbuv84 R/+t6mhGaiOKHxKkI8llOJQR0qTOqKN6wQnguou6mzEd7EtzWjSsijrkfenLi8lnLMUC yvog== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b=YDdg6Ljm; 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-42371-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-42371-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=mediatek.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id pw23-20020a17090720b700b00a303ea589desi3084427ejb.382.2024.01.29.00.46.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jan 2024 00:46:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-42371-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@mediatek.com header.s=dk header.b=YDdg6Ljm; 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-42371-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-42371-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 am.mirrors.kernel.org (Postfix) with ESMTPS id C37121F24B1C for ; Mon, 29 Jan 2024 08:40:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4EB7F612EC; Mon, 29 Jan 2024 08:33:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=mediatek.com header.i=@mediatek.com header.b="YDdg6Ljm" 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 854D656459; Mon, 29 Jan 2024 08:33:18 +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=1706517201; cv=none; b=YpWVbRgKj+iEXqqLWlyFkzcqx2aQlltsLJKI5/p8TZy/CcwW0BYpeXrWV9+/blyASp3x2ZPR8GX2sBBXq8/0VegtDSGMHhBcWb9g4+VbMxtK6Qx6wzNhqReHA1g8s3VNKz0xO2lEfkgUICVMT3aR5ey6eyaTXdA8ZCtwdUi2Zmw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706517201; c=relaxed/simple; bh=wNHBzgQo42duOG6Ze9zKkCStxfLv6D+G1FcpDCzKEPM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Sp0FBA96YWZSobd3crQk7gZZoojN78ZD/dDw0NeRStiubM9SiDdfIrINRURo8HIDhDtHDraPcKuAvwWBH5Q+q7F++PRcoyhaPX7rJplteygNC27lSGtgBI+UjJ09YuSLssoX72XlIAC+0rvIa5rGayiIVV5uZWypCDYnvFJaG1U= 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=YDdg6Ljm; 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: 08c7c4b8be8111ee9e680517dc993faa-20240129 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=rM4PeM5hdGK8vkQCs8uiZPUIqfcZ2G8VIqcv0jjx+i8=; b=YDdg6LjmUtQSFYhKdeKZbOQQjUzJYljCQI41YpYweVMHfRpRVs8TghvwdNtpnfeIWUxlFDyg3ayvMPFI9r5pej7Cy2F8AdLY7i50NCr8mgk91kTBVJXJYJsYSaMJxNnEILGhdAcwpbpDDdvECgS6JQXyK2LpNs+vGh4wF1m4Fgw=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.1.36,REQID:31e2c8ea-62c2-4191-9227-c42ec3488d49,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:6e16cf4,CLOUDID:6d4143fe-c16b-4159-a099-3b9d0558e447,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 X-CID-BVR: 0 X-CID-BAS: 0,_,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR,TF_CID_SPAM_ULN X-UUID: 08c7c4b8be8111ee9e680517dc993faa-20240129 Received: from mtkmbs13n2.mediatek.inc [(172.21.101.108)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 877775346; Mon, 29 Jan 2024 16:33:10 +0800 Received: from mtkmbs13n1.mediatek.inc (172.21.101.193) by mtkmbs13n2.mediatek.inc (172.21.101.108) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.26; Mon, 29 Jan 2024 16:33:09 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by mtkmbs13n1.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1118.26 via Frontend Transport; Mon, 29 Jan 2024 16:33:09 +0800 From: Yi-De Wu To: Yingshiuan Pan , Ze-Yu Wang , Yi-De Wu , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Jonathan Corbet , Catalin Marinas , Will Deacon , Richard Cochran , Matthias Brugger , AngeloGioacchino Del Regno CC: , , , , , , David Bradil , Trilok Soni , My Chuang , Shawn Hsiao , PeiLun Suei , Liju Chen , Willix Yeh , Kevenny Hsieh Subject: [PATCH v9 18/21] virt: geniezone: Add memory relinquish support Date: Mon, 29 Jan 2024 16:32:59 +0800 Message-ID: <20240129083302.26044-19-yi-de.wu@mediatek.com> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20240129083302.26044-1-yi-de.wu@mediatek.com> References: <20240129083302.26044-1-yi-de.wu@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: "Jerry Wang" Unpin the pages when VM relinquish the pages or is destroyed. Signed-off-by: Jerry Wang Signed-off-by: Yingshiuan Pan Signed-off-by: Liju-Clr Chen Signed-off-by: Yi-De Wu --- drivers/virt/geniezone/gzvm_exception.c | 23 ++++++++++++ drivers/virt/geniezone/gzvm_mmu.c | 49 +++++++++++++++++++++++++ drivers/virt/geniezone/gzvm_vcpu.c | 6 ++- include/linux/gzvm_drv.h | 2 + include/uapi/linux/gzvm.h | 5 +++ 5 files changed, 83 insertions(+), 2 deletions(-) diff --git a/drivers/virt/geniezone/gzvm_exception.c b/drivers/virt/geniezone/gzvm_exception.c index 31fdb4ae8db4..af26d1c82791 100644 --- a/drivers/virt/geniezone/gzvm_exception.c +++ b/drivers/virt/geniezone/gzvm_exception.c @@ -37,3 +37,26 @@ bool gzvm_handle_guest_exception(struct gzvm_vcpu *vcpu) else return false; } + +/** + * gzvm_handle_guest_hvc() - Handle guest hvc + * @vcpu: Pointer to struct gzvm_vcpu struct + * Return: + * * true - This hvc has been processed, no need to back to VMM. + * * false - This hvc has not been processed, require userspace. + */ +bool gzvm_handle_guest_hvc(struct gzvm_vcpu *vcpu) +{ + unsigned long ipa; + int ret; + + switch (vcpu->run->hypercall.args[0]) { + case GZVM_HVC_MEM_RELINQUISH: + ipa = vcpu->run->hypercall.args[1]; + ret = gzvm_handle_relinquish(vcpu, ipa); + return (ret == 0) ? true : false; + default: + break; + } + return false; +} diff --git a/drivers/virt/geniezone/gzvm_mmu.c b/drivers/virt/geniezone/gzvm_mmu.c index cf619b3833aa..cf9484736874 100644 --- a/drivers/virt/geniezone/gzvm_mmu.c +++ b/drivers/virt/geniezone/gzvm_mmu.c @@ -131,6 +131,36 @@ static int gzvm_insert_ppage(struct gzvm *vm, struct gzvm_pinned_page *ppage) return 0; } +static int rb_ppage_cmp(const void *key, const struct rb_node *node) +{ + struct gzvm_pinned_page *p = container_of(node, + struct gzvm_pinned_page, + node); + phys_addr_t ipa = (phys_addr_t)key; + + return (ipa < p->ipa) ? -1 : (ipa > p->ipa); +} + +/* Invoker of this function is responsible for locking */ +static int gzvm_remove_ppage(struct gzvm *vm, phys_addr_t ipa) +{ + struct gzvm_pinned_page *ppage; + struct rb_node *node; + + node = rb_find((void *)ipa, &vm->pinned_pages, rb_ppage_cmp); + + if (node) + rb_erase(node, &vm->pinned_pages); + else + return 0; + + ppage = container_of(node, struct gzvm_pinned_page, node); + unpin_user_pages_dirty_lock(&ppage->page, 1, true); + kfree(ppage); + + return 0; +} + static int pin_one_page(struct gzvm *vm, unsigned long hva, u64 gpa) { unsigned int flags = FOLL_HWPOISON | FOLL_LONGTERM | FOLL_WRITE; @@ -167,6 +197,25 @@ static int pin_one_page(struct gzvm *vm, unsigned long hva, u64 gpa) return ret; } +/** + * gzvm_handle_relinquish() - Handle memory relinquish request from hypervisor + * + * @vcpu: Pointer to struct gzvm_vcpu_run in userspace + * @ipa: Start address(gpa) of a reclaimed page + * + * Return: Always return 0 because there are no cases of failure + */ +int gzvm_handle_relinquish(struct gzvm_vcpu *vcpu, phys_addr_t ipa) +{ + struct gzvm *vm = vcpu->gzvm; + + mutex_lock(&vm->mem_lock); + gzvm_remove_ppage(vm, ipa); + mutex_unlock(&vm->mem_lock); + + return 0; +} + int gzvm_vm_allocate_guest_page(struct gzvm *vm, struct gzvm_memslot *slot, u64 gfn, u64 *pfn) { diff --git a/drivers/virt/geniezone/gzvm_vcpu.c b/drivers/virt/geniezone/gzvm_vcpu.c index 455ae2e4285c..86c690749277 100644 --- a/drivers/virt/geniezone/gzvm_vcpu.c +++ b/drivers/virt/geniezone/gzvm_vcpu.c @@ -113,12 +113,14 @@ static long gzvm_vcpu_run(struct gzvm_vcpu *vcpu, void __user *argp) * it's geniezone's responsibility to fill corresponding data * structure */ + case GZVM_EXIT_HYPERCALL: + if (!gzvm_handle_guest_hvc(vcpu)) + need_userspace = true; + break; case GZVM_EXIT_EXCEPTION: if (!gzvm_handle_guest_exception(vcpu)) need_userspace = true; break; - case GZVM_EXIT_HYPERCALL: - fallthrough; case GZVM_EXIT_DEBUG: fallthrough; case GZVM_EXIT_FAIL_ENTRY: diff --git a/include/linux/gzvm_drv.h b/include/linux/gzvm_drv.h index bb47de2cf5a2..2e50ee3011e8 100644 --- a/include/linux/gzvm_drv.h +++ b/include/linux/gzvm_drv.h @@ -178,6 +178,8 @@ int gzvm_arch_inform_exit(u16 vm_id); int gzvm_find_memslot(struct gzvm *vm, u64 gpa); int gzvm_handle_page_fault(struct gzvm_vcpu *vcpu); bool gzvm_handle_guest_exception(struct gzvm_vcpu *vcpu); +int gzvm_handle_relinquish(struct gzvm_vcpu *vcpu, phys_addr_t ipa); +bool gzvm_handle_guest_hvc(struct gzvm_vcpu *vcpu); int gzvm_arch_create_device(u16 vm_id, struct gzvm_create_device *gzvm_dev); int gzvm_arch_inject_irq(struct gzvm *gzvm, unsigned int vcpu_idx, diff --git a/include/uapi/linux/gzvm.h b/include/uapi/linux/gzvm.h index ce4c80bf254c..205fcf7cdfa7 100644 --- a/include/uapi/linux/gzvm.h +++ b/include/uapi/linux/gzvm.h @@ -189,6 +189,11 @@ enum { GZVM_EXCEPTION_PAGE_FAULT = 0x1, }; +/* hypercall definitions of GZVM_EXIT_HYPERCALL */ +enum { + GZVM_HVC_MEM_RELINQUISH = 0xc6000009, +}; + /** * struct gzvm_vcpu_run: Same purpose as kvm_run, this struct is * shared between userspace, kernel and -- 2.18.0