From patchwork Fri Apr 15 21:58:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 12815448 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 76342C433F5 for ; Fri, 15 Apr 2022 22:01:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=387+uCNYy/2lP02jUgyNJwOX/H7JMCr0cZtjFRBCuDg=; b=wDarZEMW7Ajij3bVo60I4tKMbt Uvqt2sQA21b5vwxH77jCCGPDb1JGi00BVrIoO82/OLlvUqAQrOULLs2Wx7fQbvs1MNjFHm59iofN0 DFF6GixjQPOSQR0PWNH05Gs/V/By3r+Ph7k9KLnbjbWN9rBRdL5G8jh0eFCkvw0/LmNYkSehHvLA9 e//2Dix3tLMstQnXhIooQAWffIHe/biq1HEY6DB56mGiN96Cg/CRQ5hoF/gVTOONN/0JrtdopxXI+ xFtUo9enf8t4cxmZf5BsLapSkoU6Keli+hRjhnkqe9e6HsiGZLn9TBUIsYuOmTa6RiYr5W3D3iK3N LPNxQfzQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nfTzE-00BSTa-L4; Fri, 15 Apr 2022 22:00:16 +0000 Received: from mail-il1-x14a.google.com ([2607:f8b0:4864:20::14a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nfTyG-00BS2Z-7P for linux-arm-kernel@lists.infradead.org; Fri, 15 Apr 2022 21:59:18 +0000 Received: by mail-il1-x14a.google.com with SMTP id i2-20020a056e021d0200b002cac9b3b46cso5434595ila.5 for ; Fri, 15 Apr 2022 14:59:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=JyIVyQg6UGD612cbxcwyNwFE+I6JblVf7Z7F7spTUWo=; b=LMqmd6P7Ue7Y9IYx3CzgSFOTxT7tT239JnYQyS2EtWQ3fN4qbQ0VflsOtSv+zaS5bk +bvfK/SmAwgiQklAVqKG84UBqOcNnfZBYDShTxLbmp4TCyU+thTgl31lI4p91fxWMOn/ dd2QO+9mQAqSvAt90OOkgKcjFxVpl7VjcraP/FuiseUcuGPj+F+x30DmaWdzLXajn575 2MLUXCslyvGHkc1BlYeAgCkKGGLpODBbWpAJnCW0QHKTJCA67zs859BL+KVaEhsWfoh0 j+lM1SBjp+89lbo+kBMwvVR9gQck0ZWmyt7cTKZrx3+vLXytC9/xjlO4AIrc/U0c/XjQ nkAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=JyIVyQg6UGD612cbxcwyNwFE+I6JblVf7Z7F7spTUWo=; b=NTrfUta372Av0TLeJiJ4cWBancpsTZ1vxn6l/be9Xun7m3XZTGcb4W3Aeg+NLYe2zZ My7XIJWAXmfhtWqM9roChRq1rHnaXZkw6mMinTfWMUB9g+rf9VTmyWu9xjJTja9eocq9 hianoU8eKEAq8VpXtm2xcdkdSYQ6Wgq7lf3KsJ5uNIvOwHj53Gbz47d1fj77NBfsYGFG Lsn+a0ffwEEobi+dlSbKfdxwmDbKKEV5lUTtmrdSozf5rH+ePHBn3daxdxnHlM+adrJJ Nv5mLYuvVr7LBWCewxVkxmgKKDA+nWXFjnaZJrg2W3fTgdTs67kpevc27rtWAsrMo9to KuAA== X-Gm-Message-State: AOAM530+cUyoCCc8eYdXHAc0Nmt+1SOjKJhMWqoVCK/Eaga3zNJec9fS 5dGss+StHJfb3xR7c7ToBR5OCScDVZE= X-Google-Smtp-Source: ABdhPJwW/MzIxEmQ87y7GaINMs+YHxfeosd37KaEM3yVPDMpYVf1oEXa8efMhhfJEE88ApB9wQ629pQLxlQ= X-Received: from oupton.c.googlers.com ([fda3:e722:ac3:cc00:2b:ff92:c0a8:404]) (user=oupton job=sendgmr) by 2002:a05:6602:27cc:b0:5f0:876e:126b with SMTP id l12-20020a05660227cc00b005f0876e126bmr328774ios.129.1650059954862; Fri, 15 Apr 2022 14:59:14 -0700 (PDT) Date: Fri, 15 Apr 2022 21:58:51 +0000 In-Reply-To: <20220415215901.1737897-1-oupton@google.com> Message-Id: <20220415215901.1737897-8-oupton@google.com> Mime-Version: 1.0 References: <20220415215901.1737897-1-oupton@google.com> X-Mailer: git-send-email 2.36.0.rc0.470.gd361397f0d-goog Subject: [RFC PATCH 07/17] KVM: arm64: Enlighten perm relax path about parallel walks From: Oliver Upton To: kvmarm@lists.cs.columbia.edu Cc: kvm@vger.kernel.org, Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , linux-arm-kernel@lists.infradead.org, Peter Shier , Ricardo Koller , Reiji Watanabe , Paolo Bonzini , Sean Christopherson , Ben Gardon , David Matlack , Oliver Upton X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220415_145916_370450_54AA9042 X-CRM114-Status: GOOD ( 13.54 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org To date the permission relax path of the stage-2 fault handler hasn't had to worry about the paging structures changing under its nose, as map operations acquire the write lock. That's about to change, which means a permission relaxation walker could traverse in parallel with a map operation. If at any point during traversal the permission relax walker finds a locked pte, bail immediately. Either the instruction will succeed or the vCPU will fault once more and (hopefully) walk the tables successfully. Signed-off-by: Oliver Upton --- arch/arm64/kvm/hyp/pgtable.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index 059ebb921125..ff6f14755d0c 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -1168,6 +1168,11 @@ static int stage2_attr_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, struct stage2_attr_data *data = arg; struct kvm_pgtable_mm_ops *mm_ops = data->mm_ops; + if (stage2_pte_is_locked(pte)) { + WARN_ON(!shared); + return -EAGAIN; + } + if (!kvm_pte_valid(pte)) return 0; @@ -1190,7 +1195,9 @@ static int stage2_attr_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, stage2_pte_executable(pte) && !stage2_pte_executable(*ptep)) mm_ops->icache_inval_pou(kvm_pte_follow(pte, mm_ops), kvm_granule_size(level)); - WRITE_ONCE(*ptep, pte); + + if (!kvm_try_set_pte(ptep, data->pte, pte, shared)) + return -EAGAIN; } return 0; @@ -1199,7 +1206,7 @@ static int stage2_attr_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, static int stage2_update_leaf_attrs(struct kvm_pgtable *pgt, u64 addr, u64 size, kvm_pte_t attr_set, kvm_pte_t attr_clr, kvm_pte_t *orig_pte, - u32 *level) + u32 *level, bool shared) { int ret; kvm_pte_t attr_mask = KVM_PTE_LEAF_ATTR_LO | KVM_PTE_LEAF_ATTR_HI; @@ -1214,7 +1221,7 @@ static int stage2_update_leaf_attrs(struct kvm_pgtable *pgt, u64 addr, .flags = KVM_PGTABLE_WALK_LEAF, }; - ret = kvm_pgtable_walk(pgt, addr, size, &walker, false); + ret = kvm_pgtable_walk(pgt, addr, size, &walker, shared); if (ret) return ret; @@ -1230,14 +1237,14 @@ int kvm_pgtable_stage2_wrprotect(struct kvm_pgtable *pgt, u64 addr, u64 size) { return stage2_update_leaf_attrs(pgt, addr, size, 0, KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W, - NULL, NULL); + NULL, NULL, false); } kvm_pte_t kvm_pgtable_stage2_mkyoung(struct kvm_pgtable *pgt, u64 addr) { kvm_pte_t pte = 0; stage2_update_leaf_attrs(pgt, addr, 1, KVM_PTE_LEAF_ATTR_LO_S2_AF, 0, - &pte, NULL); + &pte, NULL, false); dsb(ishst); return pte; } @@ -1246,7 +1253,7 @@ kvm_pte_t kvm_pgtable_stage2_mkold(struct kvm_pgtable *pgt, u64 addr) { kvm_pte_t pte = 0; stage2_update_leaf_attrs(pgt, addr, 1, 0, KVM_PTE_LEAF_ATTR_LO_S2_AF, - &pte, NULL); + &pte, NULL, false); /* * "But where's the TLBI?!", you scream. * "Over in the core code", I sigh. @@ -1259,7 +1266,7 @@ kvm_pte_t kvm_pgtable_stage2_mkold(struct kvm_pgtable *pgt, u64 addr) bool kvm_pgtable_stage2_is_young(struct kvm_pgtable *pgt, u64 addr) { kvm_pte_t pte = 0; - stage2_update_leaf_attrs(pgt, addr, 1, 0, 0, &pte, NULL); + stage2_update_leaf_attrs(pgt, addr, 1, 0, 0, &pte, NULL, false); return pte & KVM_PTE_LEAF_ATTR_LO_S2_AF; } @@ -1282,7 +1289,7 @@ int kvm_pgtable_stage2_relax_perms(struct kvm_pgtable *pgt, u64 addr, if (prot & KVM_PGTABLE_PROT_X) clr |= KVM_PTE_LEAF_ATTR_HI_S2_XN; - ret = stage2_update_leaf_attrs(pgt, addr, 1, set, clr, NULL, &level); + ret = stage2_update_leaf_attrs(pgt, addr, 1, set, clr, NULL, &level, true); if (!ret) kvm_call_hyp(__kvm_tlb_flush_vmid_ipa, pgt->mmu, addr, level); return ret;