From patchwork Mon Jul 19 10:47:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Perret X-Patchwork-Id: 12385351 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=-18.2 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, 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 B22F8C07E9D for ; Mon, 19 Jul 2021 10:49:55 +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 7D24C6101E for ; Mon, 19 Jul 2021 10:49:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7D24C6101E Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@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=9+bCnENcg9F/B/eZpf0V9iGZtdw5ZKspAmy9Mzp7ThQ=; b=Fgez54TrXSnBAotW1UNLSVtW9L R2KiinZImB/Tk90mc98k4EpOgvJCav13cUFNDtSzgMDRs5n4mcuZmqj0TPWEC07yFweCV9rnC9T5o NYPBsENoHDLcNT9xKhX+jy/Wu+Zm31/iGPG8TK9fK9fDNd2X+MV5QtEd1wajU2jzUvlXeV2T4o/e8 ijQsFVZybwNKi3BgkgEC87Wrs7KCb5e3LUZLdo5MAumZ+Yu8cXV7uxVXnC+8OPRvKE+4oozm37Gpg Ad6yqzwoxCgcokpfGd+BNJV9KONAZ8Ukp4L8fRApi75mpCHPnGFHPtJKAs4zM036+VJiHcjy3LkKY wk+nLa7Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m5Qop-009Kdu-QL; Mon, 19 Jul 2021 10:48:16 +0000 Received: from mail-qk1-x74a.google.com ([2607:f8b0:4864:20::74a]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1m5QoL-009KQj-7V for linux-arm-kernel@lists.infradead.org; Mon, 19 Jul 2021 10:47:47 +0000 Received: by mail-qk1-x74a.google.com with SMTP id 81-20020a370b540000b02903b854c43335so14233687qkl.21 for ; Mon, 19 Jul 2021 03:47:43 -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=hd3eXFI12/cSmrxyxLEWCVWM4Lg6AoDwKdvReeRGEGk=; b=HGOnJdXy3Mnr4dlmP09v+4kaA2VwsExFHRqk7q43R4drwSHPnf1kYry4Wl5kKwf0Ee q/AgvmFXPCqnse7moGoX/peLgHs08Z53JM0cUkTPG1e+mtiHg1mJ2gYMZZUr8eQckACF wPuNxnbctRGG/p9PXW+RIQz859Ea6UKp0pNoSz1H4BUNYSpYklqGDGeZKmZm2FHM5kEr vGVayYqJLFtEQKuIrH7lDHglKIh66lQcveE6hfwNqQ1Tdyg10vEx/33yvUTrcxgKd8UJ LivnrEnDoNFECAWWZ+T5aV5YpgdGclh8bF5DU/PHmR1fk5vvK3XCxKeOwUm2PcqyFwgY tglQ== 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=hd3eXFI12/cSmrxyxLEWCVWM4Lg6AoDwKdvReeRGEGk=; b=bDubkLPu3BvwlMA6OORZsV6ZnB9frw2c+gm8yQFZxtOv8Q2PHD2smbTrImD/mtKtw8 GqD2JRqv0DYElJkrDQNGGSYzY8DxtQ3lj30xodmlYyag0RnJo560TicGe15BDxiEV5O6 uGfsKqnka2M89u6f977NsQuK4K593J4ZP/pla7+zyNNjbK71+91HjGKrLUBD1rZeeptJ 51YD3wq1q3OQ1gZR+aufjkzu5HiRiwevqHS1tPUil3OtuhCy4jqc9ef8LYCjVU/Cv4Yz hrtzV1va3PbD5T1He6cFkP+2JVqBH1JuTI0lqwoh2zHsmlr7LW+FQNRVVaZuQ4SyiIjy pteg== X-Gm-Message-State: AOAM532Z6Iki9ZpiZbTsCwt0HmelW+9E6wngpQnhdNjfVtGvcgWmLWQM sDqLPPcK561CNpcPB4YYFxbMdLKKgFQG X-Google-Smtp-Source: ABdhPJymJx2U/vPY7h8AjNJlQB0JopARk77JVivMgxwS6x59HR1TE3wCFjBaR7svOiHcbbVPqjLfxnY80gtQ X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:1a96:a43f:6c2e:bb5a]) (user=qperret job=sendgmr) by 2002:a0c:c350:: with SMTP id j16mr24060301qvi.51.1626691663046; Mon, 19 Jul 2021 03:47:43 -0700 (PDT) Date: Mon, 19 Jul 2021 11:47:22 +0100 In-Reply-To: <20210719104735.3681732-1-qperret@google.com> Message-Id: <20210719104735.3681732-2-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 01/14] 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-20210719_034745_341553_6731B94B X-CRM114-Status: GOOD ( 16.35 ) 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. Signed-off-by: Quentin Perret --- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 38 ++++++++++++++------------- 1 file changed, 20 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..56f2117c877b 100644 --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c @@ -208,6 +208,23 @@ 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 = 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 +240,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 +259,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;