Received: by 2002:a05:6a10:1287:0:0:0:0 with SMTP id d7csp3575934pxv; Mon, 19 Jul 2021 03:48:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyUnazBzoawa5P6Vg1X/Kz26a3gpmJf5P6jYjOdMyRBdPy+A92JkmGL1fekcdoq7KhpnTHp X-Received: by 2002:a02:866b:: with SMTP id e98mr21725325jai.48.1626691712159; Mon, 19 Jul 2021 03:48:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626691712; cv=none; d=google.com; s=arc-20160816; b=WOnQCswOMiQX7uLtYS+QNt0ooW7b0kuEragQWm+08jRvdgnMWFg0vhjdnrmPzEzIxC bR6Ih6IAJTpeGADDYsXHcl3Q9vADH0nw0MR+odFJ4g4rk7+Tgkb2DFXKjvTE+mDhia6O LjFcq7iPDLiOgtS/Qkl5zzzcYDgcg0fdUJhJbm4uLnFRpFhs5gueZDt/WBc0mZlkhDgk mPzqebrbC67TSy0WVYR2pHbKwr+D2C6HxC1YTvslwHtMGC70w4LAD8PWXr+kdCOAY9WB 81T4fl9FUjDybXoo+97FYIz+fW0Goy7WWwWW+AKAtlEc7ZliBttoAFdaFA7uEML8AagD HEgA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=lb/gYwEJ52V8NJMW9ljOtVfH6PwbyXsZiRh6U1L061M=; b=RhDsKamzNEuCfzx454wRQB3bPEHXP+5vrWfaAS4hllQfx4snIX/80z4cVMJ4CRVLAZ S82KrxthrKOR3E1VJJRNiFv4j0UxHkpkfMflgfMqDkmeeu1b7jtbyr4qbLsWFhWvaB6Z qobTuyBUoP2e9CbKI8qIRsWS4IxmawGDlNmgbMeRQTPooj7ovDozX7wMyZ/DOWDPBoWh C01vd/XOfSEAe8nGOoogwLPD8p8/XVf/ad0eg+NcGjwzE+NnjGA04CQB5d+8aGGnwlze TEarZNPdWq6fWfYYNV9AlLUy790Sc4GUi6G0NIEMpUD1sq6DH+IYktuSL+1kZFYtR3UU RNhg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=dVcGRuTe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y143si7401015iof.80.2021.07.19.03.48.20; Mon, 19 Jul 2021 03:48:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=dVcGRuTe; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236673AbhGSKHN (ORCPT + 99 others); Mon, 19 Jul 2021 06:07:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54474 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236391AbhGSKHI (ORCPT ); Mon, 19 Jul 2021 06:07:08 -0400 Received: from mail-qv1-xf4a.google.com (mail-qv1-xf4a.google.com [IPv6:2607:f8b0:4864:20::f4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92840C061574 for ; Mon, 19 Jul 2021 02:55:43 -0700 (PDT) Received: by mail-qv1-xf4a.google.com with SMTP id q11-20020ad4434b0000b029030449450d37so8441119qvs.14 for ; Mon, 19 Jul 2021 03:47:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=lb/gYwEJ52V8NJMW9ljOtVfH6PwbyXsZiRh6U1L061M=; b=dVcGRuTeuLeRZV2QwASmH66frNnilIZiSJsAaKb1s76JoXlM4AtD3npXiYQzxt28XK WRmijrISMsClSbfD/j1ZqOtq+NAIkXrS2vs0tgWk2FNH+4EGuiPN0pghsbdxN4MoQF5k dIL201YwNFekTdUovrgHjn/z4+PZxwtyG77qNXlJ+DbJY5BfxZyGA38C7WIW2DXBG+c7 cAVEg+Vw0+L5Ce2pVQUm3xmwl4XlB7do98+Wr4I207ZGws9WsRZZ3sWM5nrre3xUZJ+Q 0GH6Old6+be+vA+5Ga601BkY28wqGSPU4phFvCk5iEc0GkkR2DcpFMBULOEnVKIW5OcC zJew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=lb/gYwEJ52V8NJMW9ljOtVfH6PwbyXsZiRh6U1L061M=; b=LyICh5E85R/qhQN08DuyESeD8UtsL4IcqqxROatVbhZ1JKt2D+8HL+5f9Jl0JbN6lK QLalL6I3IqOjzgZtv2awztppGPNF6DDSxJWGYYpx5M8lcBx7UkG7nCg6u7I/u6w2lfiB HHj4EQ5vaTX6vgZTY0cR4Fv0mJ4ZPm1tSXxvRQpDJHxxdtctQhpLPYqwtAS7iniROhX2 Gaqz9yusOTQAu89m2Un8NObMQOrD6mkIkRFWRXfpVWQHqfTWQgOFSkJaTpBn2te2uqo3 sl2xcS1t0mVt32dfU+04HxbQkAEloHWpvUJLWxgyTBAv4LXBolJNf+O8+UGKAit+TH/h 8R6w== X-Gm-Message-State: AOAM5324wtLbB0lzPGCrfcFU+VxMdI0cf5TMl9hAkNIq5KcD6S9azg9H K32lYMNB3J+8rutNj63J/wGjimEBCSte X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:1a96:a43f:6c2e:bb5a]) (user=qperret job=sendgmr) by 2002:a05:6214:7e9:: with SMTP id bp9mr24508149qvb.4.1626691668039; Mon, 19 Jul 2021 03:47:48 -0700 (PDT) Date: Mon, 19 Jul 2021 11:47:24 +0100 In-Reply-To: <20210719104735.3681732-1-qperret@google.com> Message-Id: <20210719104735.3681732-4-qperret@google.com> Mime-Version: 1.0 References: <20210719104735.3681732-1-qperret@google.com> X-Mailer: git-send-email 2.32.0.402.g57bb445576-goog Subject: [PATCH 03/14] KVM: arm64: Continue stage-2 map when re-creating mappings From: Quentin Perret To: maz@kernel.org, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, will@kernel.org Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, ardb@kernel.org, qwandor@google.com, tabba@google.com, dbrazdil@google.com, kernel-team@android.com, Quentin Perret , Yanan Wang Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The stage-2 map walkers currently return -EAGAIN when re-creating identical mappings or only changing access permissions. This allows to optimize mapping pages for concurrent (v)CPUs faulting on the same page. While this works as expected when touching one page-table leaf at a time, this can lead to difficult situations when mapping larger ranges. Indeed, a large map operation can fail in the middle if an existing mapping is found in the range, even if it has compatible attributes, hence leaving only half of the range mapped. To avoid having to deal with such failures in the caller, don't interrupt the map operation when hitting existing PTEs, but make sure to still return -EAGAIN so that user_mem_abort() can mark the page dirty when needed. Cc: Yanan Wang Signed-off-by: Quentin Perret --- arch/arm64/include/asm/kvm_pgtable.h | 2 +- arch/arm64/kvm/hyp/pgtable.c | 21 +++++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h index d6649352c8b3..af62203d2f7a 100644 --- a/arch/arm64/include/asm/kvm_pgtable.h +++ b/arch/arm64/include/asm/kvm_pgtable.h @@ -258,7 +258,7 @@ void kvm_pgtable_stage2_destroy(struct kvm_pgtable *pgt); * If device attributes are not explicitly requested in @prot, then the * mapping will be normal, cacheable. * - * Note that the update of a valid leaf PTE in this function will be aborted, + * Note that the update of a valid leaf PTE in this function will be skipped, * if it's trying to recreate the exact same mapping or only change the access * permissions. Instead, the vCPU will exit one more time from guest if still * needed and then go through the path of relaxing permissions. diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index 978f341d02ca..bb73c5331b7c 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -475,6 +475,8 @@ struct stage2_map_data { void *memcache; struct kvm_pgtable_mm_ops *mm_ops; + + int ret; }; u64 kvm_get_vtcr(u64 mmfr0, u64 mmfr1, u32 phys_shift) @@ -612,8 +614,10 @@ static int stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level, * the vCPU will exit one more time from guest if still needed * and then go through the path of relaxing permissions. */ - if (!stage2_pte_needs_update(old, new)) - return -EAGAIN; + if (!stage2_pte_needs_update(old, new)) { + data->ret = -EAGAIN; + goto out; + } stage2_put_pte(ptep, data->mmu, addr, level, mm_ops); } @@ -629,6 +633,7 @@ static int stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level, smp_store_release(ptep, new); if (stage2_pte_is_counted(new)) mm_ops->get_page(ptep); +out: if (kvm_phys_is_valid(phys)) data->phys += granule; return 0; @@ -771,6 +776,7 @@ int kvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size, .mmu = pgt->mmu, .memcache = mc, .mm_ops = pgt->mm_ops, + .ret = 0, }; struct kvm_pgtable_walker walker = { .cb = stage2_map_walker, @@ -789,7 +795,10 @@ int kvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size, ret = kvm_pgtable_walk(pgt, addr, size, &walker); dsb(ishst); - return ret; + if (ret) + return ret; + + return map_data.ret; } int kvm_pgtable_stage2_set_owner(struct kvm_pgtable *pgt, u64 addr, u64 size, @@ -802,6 +811,7 @@ int kvm_pgtable_stage2_set_owner(struct kvm_pgtable *pgt, u64 addr, u64 size, .memcache = mc, .mm_ops = pgt->mm_ops, .owner_id = owner_id, + .ret = 0, }; struct kvm_pgtable_walker walker = { .cb = stage2_map_walker, @@ -815,7 +825,10 @@ int kvm_pgtable_stage2_set_owner(struct kvm_pgtable *pgt, u64 addr, u64 size, return -EINVAL; ret = kvm_pgtable_walk(pgt, addr, size, &walker); - return ret; + if (ret) + return ret; + + return map_data.ret; } static int stage2_unmap_walker(u64 addr, u64 end, u32 level, kvm_pte_t *ptep, -- 2.32.0.402.g57bb445576-goog