Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2436439rwd; Fri, 2 Jun 2023 09:20:48 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4GtJtu7JV3UACzUAps4FBySowwLVb/+j4/9fGKt32qi1/SJDPqqe/F0Yf9Ge0APwZ19W9M X-Received: by 2002:a17:902:f68a:b0:1a6:71b1:a0b9 with SMTP id l10-20020a170902f68a00b001a671b1a0b9mr555323plg.47.1685722847855; Fri, 02 Jun 2023 09:20:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685722847; cv=none; d=google.com; s=arc-20160816; b=ZlIwTYZMg6DWyDyPfT8x9ctDsJWqe53+D0J66xxWoc77qGDFDLWHZbCBL5Ej219cF0 9/r5v0vyFwwmp9Mp/AixO+s/N1TYfYHI6uHhSrW3yaR32hxEVYEcQDDc3t+EruFeBG+1 9fBWn0Isu60726osXOkh8fsSfx1uhxoz2fDQyJkW4jrPErPiWDioMStvUtHMTDKPTQTy h62CMWHP5v8GEDfYUpkXwVpR/HbxgtBTzSI3bep+gpYAWXxstpzBERuWFX58CmPnvo/u nkgN9eN90l0QIrDbAWEUaAro8TRl48SoBo/Vvh8krQuTiDzttz3H6YWvYa0c0ZP7RI5T jeJw== 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:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=dYi36v/YAwaQRjgMVH+8EY8FA7aBBtQ+BKuLP6eDkvQ=; b=BRympbahVyLNQijdNjlafswdvSa4A0gVhNmeqSDcRg065oxh7v0b7YZyyG1AiCuQKN YV991j8hOmXMpmnNeCg5gbkLpQxyxat9L1v6hodOQgfehJEmCnTDWBbZ95Aztt0P2q1E uTjQ/WRAx5hUq5bFuDSqdNvaqvcAgz6QtxiffHST44stG9ysrl405z3KCAkmNzFG2oJC JzYF2U0AQWawPGQsF3W+JVdV+EWqSRsvDfB3nRv0HDJ9SbRPRIZ+IjIZ66DZimPwGusr JX1OtR1bxrRqo3aG+ZFONvueVOxhU4b3zZ4pxUk/x4CU3SeFOn7BMIRRmcSJaCNpCDHl YW8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=Dw12ocPS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x10-20020a170902ec8a00b001a52db202c7si1149267plg.382.2023.06.02.09.20.35; Fri, 02 Jun 2023 09:20:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=Dw12ocPS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S236858AbjFBQLD (ORCPT + 99 others); Fri, 2 Jun 2023 12:11:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236649AbjFBQKm (ORCPT ); Fri, 2 Jun 2023 12:10:42 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 012ACE78 for ; Fri, 2 Jun 2023 09:10:01 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id 98e67ed59e1d1-2562fbb193bso845137a91.1 for ; Fri, 02 Jun 2023 09:10:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685722184; x=1688314184; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=dYi36v/YAwaQRjgMVH+8EY8FA7aBBtQ+BKuLP6eDkvQ=; b=Dw12ocPSe9vJUzYvSsSf9AU3CWoiwTk/XWOJWSQ28tLpoS2cFvmMFQbSf/qpjpxy7v oO7YPmr7slJeCTpuInsqqCsWP0mQqS3ua3oN9mzqlTzskeKCsUc+Eng3akxDWWH0DIw4 jhV0Z/aBx0lDztLRsncoYBKeU0QZUKmq2gyk1zRIGBfBu0xTC7gF0pRwGDV7061++Ztg o5Q/lPpRn/0BOjk3RHl0WBfcrcdb3R1NScbQhD6quOJddTL53tPtBy4zB10NE0mDM/7t Ap9iqqT0iz8St+1DIUrYSZSYDvTKdSKGgplDSGDJzXhKejoEUVmNl+mnTfSIjic7IX8O rhDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685722184; x=1688314184; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=dYi36v/YAwaQRjgMVH+8EY8FA7aBBtQ+BKuLP6eDkvQ=; b=a648lOrDJ0Gs+kVeu5FjsyQ6uxU9z0itXLvVlhGos/hWHXhydQsf3EDSpwd6poujF5 Ush2MAnvwMMCl/aG0KJDP2ZAWJWFJtH2u4E6nEPxBYbUVTYLcrWk0XZtkHt4DGpBybxg +g4kZMHrMwgr6yuqq4NSH7HnkZ08wYBYtKqVE1cvZqtAkdhGUOjdEYYWeXqwYGOzPp6s gt7X9pApnVXlG0Mzb/Sy+9sXZ7ckDXqREUSfm2oyD8JxLHhakFU2uoLXKADCo9ilaWKo 7R26+M9lyCRBfLI66tvxRpXiKnxsKCgGv0uR+rEgK4IujlRdORs16X4daC5ODS3raRiB gmeQ== X-Gm-Message-State: AC+VfDzXoVTSNqKy0zgdaY7hxctcvWp9Fbj9twVWnIu5EY3xQIPP3svW F7yOrc/rLixTN78IIK5jL++qhbjCRBtO X-Received: from vipin.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:479f]) (user=vipinsh job=sendgmr) by 2002:a17:90a:c08e:b0:252:733d:15dc with SMTP id o14-20020a17090ac08e00b00252733d15dcmr97816pjs.2.1685722183850; Fri, 02 Jun 2023 09:09:43 -0700 (PDT) Date: Fri, 2 Jun 2023 09:09:10 -0700 In-Reply-To: <20230602160914.4011728-1-vipinsh@google.com> Mime-Version: 1.0 References: <20230602160914.4011728-1-vipinsh@google.com> X-Mailer: git-send-email 2.41.0.rc0.172.g3f132b7071-goog Message-ID: <20230602160914.4011728-13-vipinsh@google.com> Subject: [PATCH v2 12/16] KVM: arm64: Retry shared page table walks outside of fault handler From: Vipin Sharma To: maz@kernel.org, oliver.upton@linux.dev, james.morse@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, will@kernel.org, chenhuacai@kernel.org, aleksandar.qemu.devel@gmail.com, tsbogend@alpha.franken.de, anup@brainfault.org, atishp@atishpatra.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, pbonzini@redhat.com, dmatlack@google.com, ricarkol@google.com Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kselftest@vger.kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vipin Sharma Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org For a shared page walker which is not fault handler, retry the walk if walker callback function returns -EAGAIN, or continue to the next SPTE if callback function return -ENOENT. Update the kvm_pgtable_walk documentation. For fault handler logic remains same, i.e. exit the walk and resume the guest when getting -EAGAIN and -ENOENT errors from walker callback function. Currently, there is no page walker which is shared and not a fault handler, but this will change in future patches when clear-dirty-log walker will use MMU read lock and run via shared walker. Signed-off-by: Vipin Sharma --- arch/arm64/include/asm/kvm_pgtable.h | 23 ++++++++++------- arch/arm64/kvm/hyp/pgtable.c | 38 +++++++++++++++++++++++----- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h index 23e7e7851f1d..145be12a5fc2 100644 --- a/arch/arm64/include/asm/kvm_pgtable.h +++ b/arch/arm64/include/asm/kvm_pgtable.h @@ -716,15 +716,20 @@ int kvm_pgtable_stage2_split(struct kvm_pgtable *pgt, u64 addr, u64 size, * type of the walker, i.e. shared walker (vCPU fault handlers) or non-shared * walker. * - * Walker Type | Callback | Walker action - * -------------|------------------|-------------- - * Non-Shared | 0 | Continue - * Non-Shared | -EAGAIN | Continue - * Non-Shared | -ENOENT | Continue - * Non-Shared | Any other | Exit - * -------------|------------------|-------------- - * Shared | 0 | Continue - * Shared | Any other | Exit + * Walker Type | Callback | Walker action + * -----------------------|------------------|-------------- + * Non-Shared | 0 | Continue + * Non-Shared | -EAGAIN | Continue + * Non-Shared | -ENOENT | Continue + * Non-Shared | Any other | Exit + * -----------------------|------------------|-------------- + * Shared | 0 | Continue + * Shared | -EAGAIN | Retry + * Shared | -ENOENT | Continue + * Shared | Any other | Exit + * -----------------------|------------------|-------------- + * Shared (Fault Handler) | 0 | Continue + * Shared (Fault Handler) | Any other | Exit * * Return: 0 on success, negative error code on failure. */ diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index 7f80e953b502..23cda3de2dd4 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -191,15 +191,21 @@ static bool kvm_pgtable_walk_continue(const struct kvm_pgtable_walker *walker, * Callbacks can also return ENOENT when PTE which is visited is not * valid. * - * In the context of a shared walker interpret these as a signal + * In the context of a fault handler interpret these as a signal * to retry guest execution. * - * Ignore these return codes altogether for walkers outside a fault - * handler (e.g. write protecting a range of memory) and chug along + * In the context of a shared walker which is not fault handler + * interpret: + * 1. EAGAIN - A signal to retry walk again. + * 2. ENOENT - A signal to ignore and move on to next SPTE. + * + * Ignore these return codes altogether for other walkers and chug along * with the page table walk. */ - if (r == -EAGAIN || r == -ENOENT) + if (r == -EAGAIN) return !(walker->flags & KVM_PGTABLE_WALK_SHARED); + if (r == -ENOENT) + return !(walker->flags & KVM_PGTABLE_WALK_HANDLE_FAULT); return !r; } @@ -260,24 +266,44 @@ static inline int __kvm_pgtable_visit(struct kvm_pgtable_walk_data *data, return ret; } +static bool kvm_pgtable_walk_retry(const struct kvm_pgtable_walker *walker, + int r) +{ + /* + * All shared page table walks where visitor callbacks return -EAGAIN + * should be retried with the exception of fault handler. In case of + * fault handler retry is achieved by resuming the guest. + */ + if (r == -EAGAIN) + return (walker->flags & KVM_PGTABLE_WALK_SHARED) && + !(walker->flags & KVM_PGTABLE_WALK_HANDLE_FAULT); + + return !r; +} + static int __kvm_pgtable_walk(struct kvm_pgtable_walk_data *data, struct kvm_pgtable_mm_ops *mm_ops, kvm_pteref_t pgtable, u32 level) { u32 idx; int ret = 0; + kvm_pteref_t pteref; if (WARN_ON_ONCE(level >= KVM_PGTABLE_MAX_LEVELS)) return -EINVAL; for (idx = kvm_pgtable_idx(data, level); idx < PTRS_PER_PTE; ++idx) { - kvm_pteref_t pteref = &pgtable[idx]; +retry: + pteref = &pgtable[idx]; if (data->addr >= data->end) break; ret = __kvm_pgtable_visit(data, mm_ops, pteref, level); - if (ret) + if (ret) { + if (kvm_pgtable_walk_retry(data->walker, ret)) + goto retry; break; + } } return ret; -- 2.41.0.rc0.172.g3f132b7071-goog