From patchwork Mon Aug 9 15:24:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Perret X-Patchwork-Id: 12426793 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=-17.5 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,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 C8101C432BE for ; Mon, 9 Aug 2021 15:27:35 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 7BECB60EBB for ; Mon, 9 Aug 2021 15:27:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 7BECB60EBB Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org 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=FDDCHIiifPt42yA8+y6fai5bRUuFkMUfkHEDwICX4C4=; b=TAiNJdjQEndItvfaYlJkGYPz4+ gSn36GmhbEl5Jk5HB1xAPgEpzjS1pWgzL1/ystknSskmqIrpEuzIXSlxjC+ZDC3R3jAN49LRcTkNX Hrp7UpZMmyIyX46glNa1mPcilq0pIfQBvzJWuKXN+rGvaKIJ3veM0+qSn1txviY8+oWCmkkdgvcsd 0JkGoV/afuTelpXe8+F63vZLPQrPqumx8/GBV7I+VTSLA9DyG4hyCfNJQoinbDJxzHvVUK+W5OLVo pK+DouHcJVWpqZ4oZ2a13EDA1XNLjrMv/uy+c7EFUKt5Wt80TtB+8Igpq6wBbgvj527d54DGxshoO J8xMeShw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mD79m-00186T-Un; Mon, 09 Aug 2021 15:25:39 +0000 Received: from mail-wr1-x449.google.com ([2a00:1450:4864:20::449]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mD79A-0017r0-5R for linux-arm-kernel@lists.infradead.org; Mon, 09 Aug 2021 15:25:01 +0000 Received: by mail-wr1-x449.google.com with SMTP id v18-20020adfe2920000b029013bbfb19640so5452087wri.17 for ; Mon, 09 Aug 2021 08:24:58 -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=g0Tec8zCIIV6B7UFt/na0xdI02dI+qYcG36GmUIxQNs=; b=u/8T1pZALwA4/sFdX62scy5dCtljsLkrBi8I1Wo5YlC53+ou/YjrvDL8JNsEIB71bE LRivkMFVgCH0BtjYzhigCs/bBsZnzm1aZdrrbJrAhXN2dDwjESl94VxP5qszcWj4CCEd kb6ZNqQAPWs3owfE5gBV3uIxdOads4TKkeSBC+CEa86jU1FkSsXByuLM+TGmtl+JwI1s V5+s+6x3trNBHDLQF9LXi2sMfrQr2qHgz74SlBLo1h0tqRcsn8LkQJbmZFl6TVJkfNSa l+1c8KgXcM3JN+xcOybD93Qo/Y8JcdC1KdNJjrp98+bqF+lnmdb8LFXpm1F/xsbnlAZG wATw== 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=g0Tec8zCIIV6B7UFt/na0xdI02dI+qYcG36GmUIxQNs=; b=aGPaA9NSafWeALoDCG84iL8goP1uHlHrHIf4UOP7J32mOR4JaFb8iqCxE3dG2N5Hb0 KyyeS4/69Lr1yK82Ort7P26XNzVgXjiQNX9hJOhxTw7IugkufCwri0/2aDVVFJZD1Z7U c8MbWYrTDSDbeCji8w+HaqMhJ2Id1qwifPDw7ybIYVIrzNdBNFi/LqaeuqBQ/CurAIde 4VXoLSXfGy800iI9WLbJpfn5gaE1u3Rk/ArPnDeKNlPGpojdabe4TfCVnqrIVdVtgSvT 4Y0RKfYXxP95nzpq30OVwDcc2HuqSna350SORn9cG1As5zrmMt+ml7qYR3wUQrpdxIJe EQKQ== X-Gm-Message-State: AOAM532wfE49H6pZcGxiRND618ux6CixXUadhF5gjIqpVf3qBrde2Th3 wjnf3Lx2vBJZ5IxUWtqyPc6gyXUcwcUC X-Google-Smtp-Source: ABdhPJwdhbneOWgiW5/bK5oKtxthf7W2lS34xOPxnKsvszEVciXZLPa8/F4hD9ZjXEHPO3uTXXdiY7U47vX5 X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:b0e8:d460:758b:a0ae]) (user=qperret job=sendgmr) by 2002:a05:6000:232:: with SMTP id l18mr25664566wrz.24.1628522697474; Mon, 09 Aug 2021 08:24:57 -0700 (PDT) Date: Mon, 9 Aug 2021 16:24:30 +0100 In-Reply-To: <20210809152448.1810400-1-qperret@google.com> Message-Id: <20210809152448.1810400-4-qperret@google.com> Mime-Version: 1.0 References: <20210809152448.1810400-1-qperret@google.com> X-Mailer: git-send-email 2.32.0.605.g8dce9f2422-goog Subject: [PATCH v4 03/21] KVM: arm64: Provide the host_stage2_try() helper macro 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, qperret@google.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210809_082500_283922_80663062 X-CRM114-Status: GOOD ( 16.73 ) 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 We currently unmap all MMIO mappings from the host stage-2 to recycle the pages whenever we run out. In order to make this pattern easy to re-use from other places, factor the logic out into a dedicated macro. While at it, apply the macro for the kvm_pgtable_stage2_set_owner() calls. They're currently only called early on and are guaranteed to succeed, but making them robust to the -ENOMEM case doesn't hurt and will avoid painful debugging sessions later on. Reviewed-by: Fuad Tabba Signed-off-by: Quentin Perret --- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 40 +++++++++++++++------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c index d938ce95d3bd..74280a753efb 100644 --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c @@ -208,6 +208,25 @@ static inline int __host_stage2_idmap(u64 start, u64 end, prot, &host_s2_pool); } +/* + * The pool has been provided with enough pages to cover all of memory with + * page granularity, but it is difficult to know how much of the MMIO range + * we will need to cover upfront, so we may need to 'recycle' the pages if we + * run out. + */ +#define host_stage2_try(fn, ...) \ + ({ \ + int __ret; \ + hyp_assert_lock_held(&host_kvm.lock); \ + __ret = fn(__VA_ARGS__); \ + if (__ret == -ENOMEM) { \ + __ret = host_stage2_unmap_dev_all(); \ + if (!__ret) \ + __ret = fn(__VA_ARGS__); \ + } \ + __ret; \ + }) + static int host_stage2_idmap(u64 addr) { enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R | KVM_PGTABLE_PROT_W; @@ -223,22 +242,7 @@ static int host_stage2_idmap(u64 addr) if (ret) goto unlock; - ret = __host_stage2_idmap(range.start, range.end, prot); - if (ret != -ENOMEM) - goto unlock; - - /* - * The pool has been provided with enough pages to cover all of memory - * with page granularity, but it is difficult to know how much of the - * MMIO range we will need to cover upfront, so we may need to 'recycle' - * the pages if we run out. - */ - ret = host_stage2_unmap_dev_all(); - if (ret) - goto unlock; - - ret = __host_stage2_idmap(range.start, range.end, prot); - + ret = host_stage2_try(__host_stage2_idmap, range.start, range.end, prot); unlock: hyp_spin_unlock(&host_kvm.lock); @@ -257,8 +261,8 @@ int __pkvm_mark_hyp(phys_addr_t start, phys_addr_t end) return -EINVAL; hyp_spin_lock(&host_kvm.lock); - ret = kvm_pgtable_stage2_set_owner(&host_kvm.pgt, start, end - start, - &host_s2_pool, pkvm_hyp_id); + ret = host_stage2_try(kvm_pgtable_stage2_set_owner, &host_kvm.pgt, + start, end - start, &host_s2_pool, pkvm_hyp_id); hyp_spin_unlock(&host_kvm.lock); return ret != -EAGAIN ? ret : 0;