From mboxrd@z Thu Jan 1 00:00:00 1970 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=-26.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=ham 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 CFDF7C4320A for ; Mon, 26 Jul 2021 09:29:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B7E1360F42 for ; Mon, 26 Jul 2021 09:29:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233001AbhGZIsu (ORCPT ); Mon, 26 Jul 2021 04:48:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232917AbhGZIss (ORCPT ); Mon, 26 Jul 2021 04:48:48 -0400 Received: from mail-qv1-xf49.google.com (mail-qv1-xf49.google.com [IPv6:2607:f8b0:4864:20::f49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46660C061757 for ; Mon, 26 Jul 2021 02:29:16 -0700 (PDT) Received: by mail-qv1-xf49.google.com with SMTP id v19-20020ad455530000b029032620cab853so6146145qvy.8 for ; Mon, 26 Jul 2021 02:29:16 -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=SklhMqwq/2h4UO2xk/TmhlS4/BSmhG4vZDkK4FOF1kk=; b=LcLE/Jxwj2rOGbqgdQiy58nAwXiFuJnR0+9TqiIE8hs+Iyxf9iUvWeng8nwkqOFgWB BuOc+xFO+Gucu5aszLn/tMe2JBUDM0tc2WMztZaRwSiTi0Z4dI7WRe4nF1DGw2Ada5mQ EJsy1NU9tzqDQEMIt0kQYVRgSEktTdB91LYl0+Z47z30mnDwuEWgGlbsuqbVEr+rmgwo WUV8b9MRyYMu/8EWYGeJZ2scufoqME6mhLaF+TJYVQIj6VnxRKpweuJnPiv8LSh/Uy7l v+NIhechSjHyaVP7EIEbLZpQNJ+rJ7CFadROzDvvnI3yU16amLsOWHV/8yMYGz9H3HUz fFZg== 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=SklhMqwq/2h4UO2xk/TmhlS4/BSmhG4vZDkK4FOF1kk=; b=kq29ta5bd25eLtmi8LhBRcwDPcI45UXAJKunWpiHBuDmGkRXPjt/j5z3Jj/bzLU5S0 S9mHIPtceD52+osxzaP6j4rpX7bsV6DXz5pPbW4fCwz3ypKcwBdGoKZyjFH0MVmKp70m WIIEMyOB7RjZfVdwJJcUemlMMF/Rm7J8VwdNYiS+QPdChZIgGdvEDFLdHccWHS2OoicW zNIz0+tKTtGIpd5BNtYbVpkn132I6RVtxKF9qvfAB1Jh9rEE6GX0+oNHUWW7YHW20mnH kOEhgO2j6sjajifWXjoBP0AFeHgoM+3LM2tNaMI5GCkUXS9zlTrJJI+cM4lCr1BJQU7c NzdQ== X-Gm-Message-State: AOAM530CTg8Nd916UiMI6HWfd/Hk7/8/DxTlOq3+nfp1AJXPXAoFv641 XFGk3o51FBk2bddEiguW5G7Mo3wsPip3 X-Google-Smtp-Source: ABdhPJyogngKNGamlr33/F+qdMicL+YqDoeMIdDoBqR2/1/6VaABd58xqeW5MV1R2hLXiW7ahS2yx7ibByaj X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:23a0:2f14:433:e6cb]) (user=qperret job=sendgmr) by 2002:a05:6214:ca5:: with SMTP id s5mr16960121qvs.58.1627291755454; Mon, 26 Jul 2021 02:29:15 -0700 (PDT) Date: Mon, 26 Jul 2021 10:28:51 +0100 In-Reply-To: <20210726092905.2198501-1-qperret@google.com> Message-Id: <20210726092905.2198501-3-qperret@google.com> Mime-Version: 1.0 References: <20210726092905.2198501-1-qperret@google.com> X-Mailer: git-send-email 2.32.0.432.gabb21c7263-goog Subject: [PATCH v2 02/16] 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, Quentin Perret Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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. 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..871149246f5f 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. + * + * Must be called with host_kvm.lock held. + */ +#define host_stage2_try(fn, ...) \ + ({ \ + int __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; -- 2.32.0.432.gabb21c7263-goog From mboxrd@z Thu Jan 1 00:00:00 1970 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=-16.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,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 AAA21C4320A for ; Mon, 26 Jul 2021 09:29:25 +0000 (UTC) Received: from mm01.cs.columbia.edu (mm01.cs.columbia.edu [128.59.11.253]) by mail.kernel.org (Postfix) with ESMTP id 529F360F4C for ; Mon, 26 Jul 2021 09:29:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 529F360F4C Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.cs.columbia.edu Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 065704B086; Mon, 26 Jul 2021 05:29:25 -0400 (EDT) X-Virus-Scanned: at lists.cs.columbia.edu Authentication-Results: mm01.cs.columbia.edu (amavisd-new); dkim=softfail (fail, message has been altered) header.i=@google.com Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id N7pLX8ZJHdAL; Mon, 26 Jul 2021 05:29:23 -0400 (EDT) Received: from mm01.cs.columbia.edu (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id B9A264A522; Mon, 26 Jul 2021 05:29:22 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 0116949F5D for ; Mon, 26 Jul 2021 05:29:20 -0400 (EDT) X-Virus-Scanned: at lists.cs.columbia.edu Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id wA60MwoNKByb for ; Mon, 26 Jul 2021 05:29:16 -0400 (EDT) Received: from mail-qv1-f73.google.com (mail-qv1-f73.google.com [209.85.219.73]) by mm01.cs.columbia.edu (Postfix) with ESMTPS id D57EC49E93 for ; Mon, 26 Jul 2021 05:29:15 -0400 (EDT) Received: by mail-qv1-f73.google.com with SMTP id r14-20020a0c8d0e0000b02902e82df307f0so7906457qvb.4 for ; Mon, 26 Jul 2021 02:29:15 -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=SklhMqwq/2h4UO2xk/TmhlS4/BSmhG4vZDkK4FOF1kk=; b=LcLE/Jxwj2rOGbqgdQiy58nAwXiFuJnR0+9TqiIE8hs+Iyxf9iUvWeng8nwkqOFgWB BuOc+xFO+Gucu5aszLn/tMe2JBUDM0tc2WMztZaRwSiTi0Z4dI7WRe4nF1DGw2Ada5mQ EJsy1NU9tzqDQEMIt0kQYVRgSEktTdB91LYl0+Z47z30mnDwuEWgGlbsuqbVEr+rmgwo WUV8b9MRyYMu/8EWYGeJZ2scufoqME6mhLaF+TJYVQIj6VnxRKpweuJnPiv8LSh/Uy7l v+NIhechSjHyaVP7EIEbLZpQNJ+rJ7CFadROzDvvnI3yU16amLsOWHV/8yMYGz9H3HUz fFZg== 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=SklhMqwq/2h4UO2xk/TmhlS4/BSmhG4vZDkK4FOF1kk=; b=VxDV0PAZ/eS99eeuhLtmVTU0UXpHLq0kfFDLuNAPFRc/SjLYjFm2WaX8x8gckZxD8s 8jGNRrsYUmB+wl2/e+6iSVTSvqt0EtYihwuakLr1p9j61cxClUHRnm43BceI9YybhOxS LEPdRH0xWzcVWGiPPdTnfg58u2hpg77rn8jOPovbCrdpdvL0+w0Qctuj8uhMS0PB9Qqq DKW8BaV5yNkUnSzBniQzd5WooeQaqdYRX+0PC++YkmmGHjWq5L0zwsenKjswvUe9hbVW x57DxXb9yk8DE5idNzTruoFmLQUKZ7ldXW7xLipQny0mvbqPDCQgJYR3sHjKiroPMIw8 lRGQ== X-Gm-Message-State: AOAM533KGpZ6/FuZe88SJiVtj7+mps2Cy9/vh47kVG8p5HqCXjGTewyd 9kOtV72T6MKp74+KlDWr6l/+1cf1CF0j X-Google-Smtp-Source: ABdhPJyogngKNGamlr33/F+qdMicL+YqDoeMIdDoBqR2/1/6VaABd58xqeW5MV1R2hLXiW7ahS2yx7ibByaj X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:23a0:2f14:433:e6cb]) (user=qperret job=sendgmr) by 2002:a05:6214:ca5:: with SMTP id s5mr16960121qvs.58.1627291755454; Mon, 26 Jul 2021 02:29:15 -0700 (PDT) Date: Mon, 26 Jul 2021 10:28:51 +0100 In-Reply-To: <20210726092905.2198501-1-qperret@google.com> Message-Id: <20210726092905.2198501-3-qperret@google.com> Mime-Version: 1.0 References: <20210726092905.2198501-1-qperret@google.com> X-Mailer: git-send-email 2.32.0.432.gabb21c7263-goog Subject: [PATCH v2 02/16] 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: qwandor@google.com, linux-kernel@vger.kernel.org, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, kernel-team@android.com X-BeenThere: kvmarm@lists.cs.columbia.edu X-Mailman-Version: 2.1.14 Precedence: list List-Id: Where KVM/ARM decisions are made List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: kvmarm-bounces@lists.cs.columbia.edu Sender: kvmarm-bounces@lists.cs.columbia.edu 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. 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..871149246f5f 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. + * + * Must be called with host_kvm.lock held. + */ +#define host_stage2_try(fn, ...) \ + ({ \ + int __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; -- 2.32.0.432.gabb21c7263-goog _______________________________________________ kvmarm mailing list kvmarm@lists.cs.columbia.edu https://lists.cs.columbia.edu/mailman/listinfo/kvmarm From mboxrd@z Thu Jan 1 00:00:00 1970 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 1F33DC4338F for ; Mon, 26 Jul 2021 09:32:01 +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 DB6A460238 for ; Mon, 26 Jul 2021 09:32:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org DB6A460238 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=7PG83Z9ARrSqfzhvUZnd8cKdCjHZoOwdtLzdvo02PYk=; b=yiexu0+zP8+XZ5dqpx2QXdCL+F XNP4LBzWjqLI6raCDE/uSzboHpXSO6sfkjAZ2lUAcKKsvW+Amx0quiH0YLF3FpGF/RoZM2HBMC9um fz3g7bxbMawsIOT9cVrkOD1JU4wOhmErcOHbmbCSItRZzBRFYomUjEy2pm0w1vcCzbwpqxwU1tY4Z HFHnDEj6ux7ChDrh5ZzQHMnTO6qXGCMQz5S9SQGw/3MQIus3FSHPmPrK3JoQqckKpUfwLhqGdEtzc Ld1YFQyKaxM2wXOc+PmXEuewx/I0mJhmzEmRU3aXwPkbdW6gPYq7sDaZa7AJSyzBTqWW/mvS1eXw3 mSe4VSTQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m7wva-00ARal-MM; Mon, 26 Jul 2021 09:29:38 +0000 Received: from mail-qk1-x749.google.com ([2607:f8b0:4864:20::749]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1m7wvH-00ARVc-OW for linux-arm-kernel@lists.infradead.org; Mon, 26 Jul 2021 09:29:21 +0000 Received: by mail-qk1-x749.google.com with SMTP id b9-20020a05620a1269b02903b8bd5c7d95so8470169qkl.12 for ; Mon, 26 Jul 2021 02:29:16 -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=SklhMqwq/2h4UO2xk/TmhlS4/BSmhG4vZDkK4FOF1kk=; b=LcLE/Jxwj2rOGbqgdQiy58nAwXiFuJnR0+9TqiIE8hs+Iyxf9iUvWeng8nwkqOFgWB BuOc+xFO+Gucu5aszLn/tMe2JBUDM0tc2WMztZaRwSiTi0Z4dI7WRe4nF1DGw2Ada5mQ EJsy1NU9tzqDQEMIt0kQYVRgSEktTdB91LYl0+Z47z30mnDwuEWgGlbsuqbVEr+rmgwo WUV8b9MRyYMu/8EWYGeJZ2scufoqME6mhLaF+TJYVQIj6VnxRKpweuJnPiv8LSh/Uy7l v+NIhechSjHyaVP7EIEbLZpQNJ+rJ7CFadROzDvvnI3yU16amLsOWHV/8yMYGz9H3HUz fFZg== 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=SklhMqwq/2h4UO2xk/TmhlS4/BSmhG4vZDkK4FOF1kk=; b=JQtfhwX5t9MMk7zJM4Q/84uz2r7ulZHpLKgrzNXpGnRzT4UXK0VUwU4m0AP3vEMqob Vn1mDqIch6WchBXurVk3LIVqjxiO4AZlO8NFKyGvUj4dcfci+3ZWu53ZKYTXpUfFAILu 5m14D/FvUaCRUpm9x3N+c50JBDdbJJeS1Vj2Dw03FR/SSymzDoYLm3DMg6M3r+7H9uYm TjuR8owqI9RBF4sot+emzPq7KaL7OP799QuQ8X34wk6oGO6/0shLQTzGrDma8zeU5aZy bOM8gHTPYxOkYL0GPs+0mKZLfoulwcYumv/RlHvVSW/Vla6UPS2I49ocqoiAmE4Mwq/I JEoA== X-Gm-Message-State: AOAM532Uh0HicgV/skOJKhGIHQFhvqanFHgqNM5P5vxeUv8kzc3z4N3b FMVPe+fBdMIiz3jrlvCzlh9Za7FU+9Dg X-Google-Smtp-Source: ABdhPJyogngKNGamlr33/F+qdMicL+YqDoeMIdDoBqR2/1/6VaABd58xqeW5MV1R2hLXiW7ahS2yx7ibByaj X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:23a0:2f14:433:e6cb]) (user=qperret job=sendgmr) by 2002:a05:6214:ca5:: with SMTP id s5mr16960121qvs.58.1627291755454; Mon, 26 Jul 2021 02:29:15 -0700 (PDT) Date: Mon, 26 Jul 2021 10:28:51 +0100 In-Reply-To: <20210726092905.2198501-1-qperret@google.com> Message-Id: <20210726092905.2198501-3-qperret@google.com> Mime-Version: 1.0 References: <20210726092905.2198501-1-qperret@google.com> X-Mailer: git-send-email 2.32.0.432.gabb21c7263-goog Subject: [PATCH v2 02/16] 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, Quentin Perret X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210726_022919_845222_E4F62652 X-CRM114-Status: GOOD ( 16.28 ) 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit 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. 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..871149246f5f 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. + * + * Must be called with host_kvm.lock held. + */ +#define host_stage2_try(fn, ...) \ + ({ \ + int __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; -- 2.32.0.432.gabb21c7263-goog _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel