Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2580927pxj; Mon, 14 Jun 2021 02:07:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxzM6iHj3w/zVV7ylw3MIMxb+FgTHU1oLsFzOx8DV2Naf6KRMCRv1Ucw20npDXTrLPsGsW9 X-Received: by 2002:a17:906:f192:: with SMTP id gs18mr14765826ejb.114.1623661632906; Mon, 14 Jun 2021 02:07:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623661632; cv=none; d=google.com; s=arc-20160816; b=pQBpDsCx8mkH9OggvPwsSPylSM2HLyVbpPgcRJ5CeMkOc4O6A9Qw+zCtlER8JbYqXb q3FXFnHOIFRysyo9BAj9FjfRc/MLStPmTPqJI7HQ9Nk0v9pjJm+L/ubXTzM9HGKbC6Ug FzzAsBhF80jWrcDcMiNul6t/QapsbPTWufvWGnt0DVjhsM8JnVjL2ZbT+wPtaqtdPX83 90nEwZKHgCrg++WfK2LLjgoZLadW/z2ag7216NSsSaGoCblcGeEm/AFEiZzwzJEbcACE p4rf0BXxNVxFZ/C//aAInhIIBCkIoFd0DvAGLa2p4uZaBq/hDx5u0LxFgf/Vo9cseLCn ATtA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=joxMYTZUCX1BC21tA/DaehTb6DBwVfxfu64+YN1t/iM=; b=i8tXb6gDiCGo3gdNzIsCbw5HvUrwIJ/IBKjdpHNt2Mz+Ac21AcpTykBkuExR7653/V BelNDiF0gOEoNEOvELbVCoipPWn6xi1VH57/TVXnaR7+WzrKtY3SL/dUh3crgEcU/ONn Lo3yI+VvBbcbb0Lbr2pINljhlDbvW0ylYR/a6Bb/O2/MUDw9l2iak1q+Wr5/yXpFfBe5 MScpKtOqg9wBc5rS12316cERbonC3KGkA5cB2+Ivv3ycN9XVv3yaxQy2ZHdA4NlQPLlr 9d6GJ78J2IvMomuOqP0zl1XXM+Pty2NHsKx3x+95DknoIJe9oqOsk9dz3PBm0pVQzGL8 uz4A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v28si11757459ejk.538.2021.06.14.02.06.50; Mon, 14 Jun 2021 02:07:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232678AbhFNJHv (ORCPT + 99 others); Mon, 14 Jun 2021 05:07:51 -0400 Received: from foss.arm.com ([217.140.110.172]:58250 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232679AbhFNJHt (ORCPT ); Mon, 14 Jun 2021 05:07:49 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 11D1911D4; Mon, 14 Jun 2021 02:05:47 -0700 (PDT) Received: from e112269-lin.arm.com (autoplooker.cambridge.arm.com [10.1.194.57]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 110F73F694; Mon, 14 Jun 2021 02:05:43 -0700 (PDT) From: Steven Price To: Catalin Marinas , Marc Zyngier , Will Deacon Cc: Steven Price , James Morse , Julien Thierry , Suzuki K Poulose , kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Dave Martin , Mark Rutland , Thomas Gleixner , qemu-devel@nongnu.org, Juan Quintela , "Dr. David Alan Gilbert" , Richard Henderson , Peter Maydell , Haibo Xu , Andrew Jones Subject: [PATCH v15 2/7] arm64: mte: Sync tags for pages where PTE is untagged Date: Mon, 14 Jun 2021 10:05:20 +0100 Message-Id: <20210614090525.4338-3-steven.price@arm.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210614090525.4338-1-steven.price@arm.com> References: <20210614090525.4338-1-steven.price@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org A KVM guest could store tags in a page even if the VMM hasn't mapped the page with PROT_MTE. So when restoring pages from swap we will need to check to see if there are any saved tags even if !pte_tagged(). However don't check pages for which pte_access_permitted() returns false as these will not have been swapped out. Reviewed-by: Catalin Marinas Signed-off-by: Steven Price --- arch/arm64/include/asm/mte.h | 4 ++-- arch/arm64/include/asm/pgtable.h | 22 +++++++++++++++++++--- arch/arm64/kernel/mte.c | 17 +++++++++++++---- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/arch/arm64/include/asm/mte.h b/arch/arm64/include/asm/mte.h index bc88a1ced0d7..347ef38a35f7 100644 --- a/arch/arm64/include/asm/mte.h +++ b/arch/arm64/include/asm/mte.h @@ -37,7 +37,7 @@ void mte_free_tag_storage(char *storage); /* track which pages have valid allocation tags */ #define PG_mte_tagged PG_arch_2 -void mte_sync_tags(pte_t *ptep, pte_t pte); +void mte_sync_tags(pte_t old_pte, pte_t pte); void mte_copy_page_tags(void *kto, const void *kfrom); void mte_thread_init_user(void); void mte_thread_switch(struct task_struct *next); @@ -53,7 +53,7 @@ int mte_ptrace_copy_tags(struct task_struct *child, long request, /* unused if !CONFIG_ARM64_MTE, silence the compiler */ #define PG_mte_tagged 0 -static inline void mte_sync_tags(pte_t *ptep, pte_t pte) +static inline void mte_sync_tags(pte_t old_pte, pte_t pte) { } static inline void mte_copy_page_tags(void *kto, const void *kfrom) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 0b10204e72fc..db5402168841 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -314,9 +314,25 @@ static inline void set_pte_at(struct mm_struct *mm, unsigned long addr, if (pte_present(pte) && pte_user_exec(pte) && !pte_special(pte)) __sync_icache_dcache(pte); - if (system_supports_mte() && - pte_present(pte) && pte_tagged(pte) && !pte_special(pte)) - mte_sync_tags(ptep, pte); + /* + * If the PTE would provide user space access to the tags associated + * with it then ensure that the MTE tags are synchronised. Although + * pte_access_permitted() returns false for exec only mappings, they + * don't expose tags (instruction fetches don't check tags). + */ + if (system_supports_mte() && pte_access_permitted(pte, false) && + !pte_special(pte)) { + pte_t old_pte = READ_ONCE(*ptep); + /* + * We only need to synchronise if the new PTE has tags enabled + * or if swapping in (in which case another mapping may have + * set tags in the past even if this PTE isn't tagged). + * (!pte_none() && !pte_present()) is an open coded version of + * is_swap_pte() + */ + if (pte_tagged(pte) || (!pte_none(old_pte) && !pte_present(old_pte))) + mte_sync_tags(old_pte, pte); + } __check_racy_pte_update(mm, ptep, pte); diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c index a3583a7fd400..ae0a3c68fece 100644 --- a/arch/arm64/kernel/mte.c +++ b/arch/arm64/kernel/mte.c @@ -33,10 +33,10 @@ DEFINE_STATIC_KEY_FALSE(mte_async_mode); EXPORT_SYMBOL_GPL(mte_async_mode); #endif -static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap) +static void mte_sync_page_tags(struct page *page, pte_t old_pte, + bool check_swap, bool pte_is_tagged) { unsigned long flags; - pte_t old_pte = READ_ONCE(*ptep); spin_lock_irqsave(&tag_sync_lock, flags); @@ -53,6 +53,9 @@ static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap) } } + if (!pte_is_tagged) + goto out; + page_kasan_tag_reset(page); /* * We need smp_wmb() in between setting the flags and clearing the @@ -69,16 +72,22 @@ static void mte_sync_page_tags(struct page *page, pte_t *ptep, bool check_swap) spin_unlock_irqrestore(&tag_sync_lock, flags); } -void mte_sync_tags(pte_t *ptep, pte_t pte) +void mte_sync_tags(pte_t old_pte, pte_t pte) { struct page *page = pte_page(pte); long i, nr_pages = compound_nr(page); bool check_swap = nr_pages == 1; + bool pte_is_tagged = pte_tagged(pte); + + /* Early out if there's nothing to do */ + if (!check_swap && !pte_is_tagged) + return; /* if PG_mte_tagged is set, tags have already been initialised */ for (i = 0; i < nr_pages; i++, page++) { if (!test_bit(PG_mte_tagged, &page->flags)) - mte_sync_page_tags(page, ptep, check_swap); + mte_sync_page_tags(page, old_pte, check_swap, + pte_is_tagged); } } -- 2.20.1