From patchwork Wed Dec 16 12:28:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yanan Wang X-Patchwork-Id: 11977451 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-11.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D929CC4361B for ; Wed, 16 Dec 2020 12:31:00 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 6E43A2311B for ; Wed, 16 Dec 2020 12:31:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6E43A2311B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=huawei.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:To:From: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=PDuAPfMn6gMGjtu40J2q5aNwgwta30wlsMh5Us4ehPc=; b=sBbWo4ErWl2QGAlF3i7sqNYCkU oAhwAlmuEoSkdPC4au9MTQ45m+FBTc/udq1YVkHorXnk5Ly20d7Zlnvx8tVl6vHxylFKi1LyI784U XCDoyVF9bm3TfyuCpNSaFsJn6+cslSuJa/bhXOP8GmfeDW5yytrNZJgFu5g1ee1ATlvMKIr4CAZOe lvgr5+Qp3omyVm6uK34Krtbm0NYRNywpIozVxeFug2T1rUUozLhAOHbdjnSp4a4HGbINKDL0zehuI NTGR8JR4zJJwscLYTmQDnPW8UXwG9O8u6FNg9qgNddHH/4LjzWsFas3ZlVWlKiwSt3b0th1QgWlg4 W/djt3GQ==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kpVvl-0004tu-GM; Wed, 16 Dec 2020 12:29:21 +0000 Received: from szxga04-in.huawei.com ([45.249.212.190]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kpVvW-0004jb-P8 for linux-arm-kernel@lists.infradead.org; Wed, 16 Dec 2020 12:29:08 +0000 Received: from DGGEMS414-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4CwvYD03Mjz15cqV; Wed, 16 Dec 2020 20:28:16 +0800 (CST) Received: from DESKTOP-TMVL5KK.china.huawei.com (10.174.187.128) by DGGEMS414-HUB.china.huawei.com (10.3.19.214) with Microsoft SMTP Server id 14.3.498.0; Wed, 16 Dec 2020 20:28:47 +0800 From: Yanan Wang To: , , Marc Zyngier , Catalin Marinas , Will Deacon , James Morse , "Julien Thierry" , Suzuki K Poulose , Gavin Shan , Quentin Perret Subject: [PATCH v2 0/3] RFC: Solve several problems in stage 2 translation Date: Wed, 16 Dec 2020 20:28:41 +0800 Message-ID: <20201216122844.25092-1-wangyanan55@huawei.com> X-Mailer: git-send-email 2.8.4.windows.1 MIME-Version: 1.0 X-Originating-IP: [10.174.187.128] X-CFilter-Loop: Reflected X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201216_072907_389714_A2F481EC X-CRM114-Status: GOOD ( 13.24 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: yuzenghui@huawei.com, wanghaibin.wang@huawei.com, Yanan Wang , zhukeqian1@huawei.com, yezengruan@huawei.com Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hi, this is the second version, thanks for reading. PATCH1/3: Procedures of hyp stage 1 mapping and guest stage 2 mapping are different, but they are tied closely by function kvm_set_valid_leaf_pte(). So separate them by rewriting kvm_set_valid_leaf_pte(). PATCH2/3: To avoid unnecessary update and small loops, add prejudgement in the translation fault handler: Skip updating the PTE with break-before-make if we are trying to recreate the exact same mapping or only change the access permissions. Actually, change of permissions will be handled through the relax_perms path next time if necessary. (1) If there are some vCPUs accessing the same GPA at the same time and the leaf PTE is not set yet, then they will all cause translation faults and the first vCPU holding mmu_lock will set valid leaf PTE, and the others will later update the old PTE with a new one if they are different. (2) When changing a leaf entry or a table entry with break-before-make, if there are some vCPUs accessing the same GPA just catch the moment when the target PTE is set invalid in a BBM procedure coincidentally, they will all cause translation faults and will later update the old PTE with a new one if they are different. The worst case can be like this: vCPU A causes a translation fault with RW prot and sets the leaf PTE with RW permissions, and then the next vCPU B with RO prot updates the PTE back to RO permissions with break-before-make. And the BBM-invalid moment may trigger more unnecessary translation faults, then some useless small loops might occur which could lead to vCPU stuck. PATCH3/3: We now mark the page dirty and set the bitmap before calling fault handlers in user_mem_abort(), and we might end up having spurious dirty pages if update of permissions or mapping has failed. So, mark the page dirty only if the fault is handled successfully. Let the guest directly enter again but not return to userspace if we were trying to recreate the same mapping or only change access permissions with BBM, which is not permitted in the mapping path. Changes from v1: - Make part of the diff as an independent patch (PATCH1/3), and add Will's Signed-off-by. - Use *return -EPERM* way when changing permissions only in the mapping path. - Add a new patch (PATCH3/3). Yanan Wang (3): KVM: arm64: Decouple partial code of hyp stage 1 mapping and guest stage 2 mapping KVM: arm64: Add prejudgement for relaxing permissions only case in stage2 translation fault handler KVM: arm64: Mark the page dirty only if the fault is handled successfully arch/arm64/kvm/hyp/pgtable.c | 78 ++++++++++++++++++++---------------- arch/arm64/kvm/mmu.c | 18 +++++++-- 2 files changed, 58 insertions(+), 38 deletions(-)