From patchwork Mon Jul 19 10:47:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Perret X-Patchwork-Id: 12385393 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 B4DC5C07E9B for ; Mon, 19 Jul 2021 10:54:50 +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 8221861006 for ; Mon, 19 Jul 2021 10:54:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8221861006 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=EuYCn3WG8ZG+R33NA2iQswwqcCSyFOnoeBDG02tE3BM=; b=XGHlco0etMPL7f8LAd8K0SmY6x KVtX9aebfJc6fWBK8sjCtsHW7xg1qFXS9uP18nH6fzpP5fXd8QsEWQhqZXpvaWKYb2EczMQaaCzo0 pVF5ygIbcAuJGJrWwfc5kOHaPXbT27hymasAt25pDbSMvBH0Vb0eDtrfX1M3MCjBg6M97V1JAFe7M j+R2fy0KL+p+oXMTsl40te/w5WhsXaqpRIVn1jEIhLQQSzOzpdmymATbOA1DZ31JE56hZf5i6razL UrZSVf/hNmhLBcUa/60O9KKsTfmtvdjjoAPPYPCHOCL0LYMmuHUjeyB3py4OVoy6hFd1F73xfkNCY cr4en08Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1m5Qsg-009MLp-0S; Mon, 19 Jul 2021 10:52:14 +0000 Received: from mail-wm1-x349.google.com ([2a00:1450:4864:20::349]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1m5Qob-009KYS-HT for linux-arm-kernel@lists.infradead.org; Mon, 19 Jul 2021 10:48:03 +0000 Received: by mail-wm1-x349.google.com with SMTP id l18-20020a05600c1d12b02902278758ab90so3803388wms.9 for ; Mon, 19 Jul 2021 03:48:00 -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=jCVZUl7mrKXhOgc92CUwmjy/8GLL8qBZeqvKPdsULBo=; b=Iv6HLotZSYKnqU+lUooERsXuSEAWx3VwKrcAVoV7fvzbOe+NzdG1VEHTwtAWje0rIB FBjobDwaueZrOJExzeI7sdg7O2/F4Y5IOMh136apaQCyUSWAEPqaYj8KLp39ymbgQFzN AyCeCZTZKejh/1vxFoUB+cZ5GImkWfPknL6py9BFxSeKNLh1bEqTIwkkWL6/9QrqiZhx bufScWZMa+3N64Gm/bfzeL1huLY0dIepVZRrRZvKUeSf1CMleCvkwSbfE+lOV7YzkSqj LXRGJqpFmm/qRenArrM8tK3lx0xGKtC867Gs8dXTkq9S3SqJLJMrrTHmRgHEhXRK2f7q F7Jw== 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=jCVZUl7mrKXhOgc92CUwmjy/8GLL8qBZeqvKPdsULBo=; b=GK9g3uqR+p1lQBTZJ5x4D/gc9CSAizcA02qkSRE1UGRh7ku7KvXlv9gzZAIr1UyJV0 eBrAusT6DCvb41GLtIeTV9cd4uK9Dxf4e5rrS4Z8FtSobe5xVqEc+MYN7GM3gr0zswK3 wpvzywjk8IVkdFa/dRAeP2VKJ0mKRQBA84AaMIzEEuX0FERosJ5JnSqLLCRyXVT6aIG3 5IkT28eyk9q0VYc6rohxKHQEvf9ENJkiHvSaR/6pXWCDSFN1islnld1HL31aKUEjoHBd qrzFPXEETB7kcxBUyS6GkUUaccWLfPDSroAORubP5r0qDps04ORJ82f44U8LJaf4wYpQ 7keg== X-Gm-Message-State: AOAM532c6kYDxs83Ib77kphsIxwPgxlbRuWikUx1DkOxBKel/xLfrVTl jVvs/uk8a6R51rv+V8L57Dv5WI45rA8J X-Google-Smtp-Source: ABdhPJzWhrN0PU3u2TAQJIPoHRtvJQ+RMyNfI8W8ip/YuHiS+H6N8RNCyCq0QLT9GDqXoBSdi1WyWB01xUsv X-Received: from luke.lon.corp.google.com ([2a00:79e0:d:210:1a96:a43f:6c2e:bb5a]) (user=qperret job=sendgmr) by 2002:a05:600c:287:: with SMTP id 7mr25592126wmk.1.1626691679361; Mon, 19 Jul 2021 03:47:59 -0700 (PDT) Date: Mon, 19 Jul 2021 11:47:29 +0100 In-Reply-To: <20210719104735.3681732-1-qperret@google.com> Message-Id: <20210719104735.3681732-9-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 08/14] KVM: arm64: Add support for tagging shared pages in page-table 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_034801_672508_34579C0B X-CRM114-Status: GOOD ( 16.87 ) 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 The hypervisor will soon be in charge of tracking ownership of all memory pages in the system. The current page-tracking infrastructure at EL2 only allows binary states: a page is either owned or not by an entity. But a number of use-cases will require more complex states for pages that are shared between two entities (host, hypervisor, or guests). In preparation for supporting these use-cases, introduce in the KVM page-table library some infrastructure allowing to tag shared pages using ignored bits (a.k.a. software bits) in PTEs. Signed-off-by: Quentin Perret --- arch/arm64/include/asm/kvm_pgtable.h | 5 +++++ arch/arm64/kvm/hyp/pgtable.c | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h index dd72653314c7..f6d3d5c8910d 100644 --- a/arch/arm64/include/asm/kvm_pgtable.h +++ b/arch/arm64/include/asm/kvm_pgtable.h @@ -81,6 +81,8 @@ enum kvm_pgtable_stage2_flags { * @KVM_PGTABLE_PROT_W: Write permission. * @KVM_PGTABLE_PROT_R: Read permission. * @KVM_PGTABLE_PROT_DEVICE: Device attributes. + * @KVM_PGTABLE_STATE_SHARED: Page shared with another entity. + * @KVM_PGTABLE_STATE_BORROWED: Page borrowed from another entity. */ enum kvm_pgtable_prot { KVM_PGTABLE_PROT_X = BIT(0), @@ -88,6 +90,9 @@ enum kvm_pgtable_prot { KVM_PGTABLE_PROT_R = BIT(2), KVM_PGTABLE_PROT_DEVICE = BIT(3), + + KVM_PGTABLE_STATE_SHARED = BIT(4), + KVM_PGTABLE_STATE_BORROWED = BIT(5), }; #define KVM_PGTABLE_PROT_RW (KVM_PGTABLE_PROT_R | KVM_PGTABLE_PROT_W) diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index 5bdbe7a31551..51598b79dafc 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -211,6 +211,29 @@ static kvm_pte_t kvm_init_invalid_leaf_owner(u8 owner_id) return FIELD_PREP(KVM_INVALID_PTE_OWNER_MASK, owner_id); } +static kvm_pte_t pte_ignored_bit_prot(enum kvm_pgtable_prot prot) +{ + kvm_pte_t ignored_bits = 0; + + /* + * Ignored bits 0 and 1 are reserved to track the memory ownership + * state of each page: + * 00: The page is owned solely by the page-table owner. + * 01: The page is owned by the page-table owner, but is shared + * with another entity. + * 10: The page is shared with, but not owned by the page-table owner. + * 11: Reserved for future use (lending). + */ + if (prot & KVM_PGTABLE_STATE_SHARED) { + if (prot & KVM_PGTABLE_STATE_BORROWED) + ignored_bits |= BIT(1); + else + ignored_bits |= BIT(0); + } + + return FIELD_PREP(KVM_PTE_LEAF_ATTR_IGNORED, ignored_bits); +} + static int kvm_pgtable_visitor_cb(struct kvm_pgtable_walk_data *data, u64 addr, u32 level, kvm_pte_t *ptep, enum kvm_pgtable_walk_flags flag) @@ -357,6 +380,7 @@ static int hyp_set_prot_attr(enum kvm_pgtable_prot prot, kvm_pte_t *ptep) attr |= FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S1_AP, ap); attr |= FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S1_SH, sh); attr |= KVM_PTE_LEAF_ATTR_LO_S1_AF; + attr |= pte_ignored_bit_prot(prot); *ptep = attr; return 0; @@ -558,6 +582,7 @@ static int stage2_set_prot_attr(struct kvm_pgtable *pgt, enum kvm_pgtable_prot p attr |= FIELD_PREP(KVM_PTE_LEAF_ATTR_LO_S2_SH, sh); attr |= KVM_PTE_LEAF_ATTR_LO_S2_AF; + attr |= pte_ignored_bit_prot(prot); *ptep = attr; return 0;