Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2056396iof; Tue, 7 Jun 2022 18:17:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw2tlPLCFw4M169pB2uXAZVzVbsgleaB36TpXHDW7OQoTUSnPxIEy6wIrF6ALSIrlS4Q1gh X-Received: by 2002:a17:90b:3e8a:b0:1e9:d56e:347b with SMTP id rj10-20020a17090b3e8a00b001e9d56e347bmr4020530pjb.42.1654651079045; Tue, 07 Jun 2022 18:17:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654651079; cv=none; d=google.com; s=arc-20160816; b=GIf7QH9W4GDVJMkUdpRpAeSSCT+k90RudOlH5Z4FnMALTHELia6yQjKOZ4c4L9Os36 tmiwWN1iQBrUtJC4UCUJrvapVd+PwavMpgqIooucFPqRzNJnHPRZW86DeZ25gmccxK+E YZ4dKR+d/pANj1gRNE1e8GW2VJxrTN1jQ6PRPdaisBV79zrY+tn2mPX4GrC1RTXU/Nct gXAK5omkrgwxZf+KAFaQSZgBHwa54lxKouo4gI6R3Vb2uAfJCRuFdyp6WcWtqNef2GBd en96/kX4I/oMya1YylF6CGBg+idbN61cp/NxNawiB4hH/qezJD7/BzNNx1cy07CKh5Dx h6DQ== 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 :message-id:date:subject:cc:to:from; bh=2IcJNTiHKGCtW7DPU7PZ37C+lDAfeMUQ54tTJc0beNI=; b=ArWPnN8h3X9WyLOLMCZ9RP+MocW+rYB5+r7+xs8xjgntqatyuoBYvnJccbSXCKOBUj kBRm9rtfBOJzIGpPUuosKe3tOXUtOgE9x9kx9uL+0GVIgGwitrJPKSsXjZwOEVDg3w8S LI1AIrTOjNaTAf2qcEuRNsBylynKOsoZqp1NISMYaAYk0FnUxNlX+koxR33aNTbnNZCb 1haEn5zh0NzjBcQ4GIaTkWYI8gKPQ363EJctIFAsDMT5XI8VRvTPvjAC2KZKzh8w8DXp 7q+Mi6gDc/aAwh5SHOTHFPEz6mK8GblME6DM2RfaRypZqrUq0yCJpihfuliHnL6STrmG J0+w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id y10-20020a655a0a000000b003fddd12f176si8122470pgs.861.2022.06.07.18.17.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 18:17:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3C5DC2584E9; Tue, 7 Jun 2022 18:11:19 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241703AbiFGLcW (ORCPT + 99 others); Tue, 7 Jun 2022 07:32:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233557AbiFGLcU (ORCPT ); Tue, 7 Jun 2022 07:32:20 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 7E8E612751 for ; Tue, 7 Jun 2022 04:32:19 -0700 (PDT) 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 1763814BF; Tue, 7 Jun 2022 04:32:19 -0700 (PDT) Received: from e119884-lin.cambridge.arm.com (e119884-lin.cambridge.arm.com [10.1.196.72]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 1B90D3F73B; Tue, 7 Jun 2022 04:32:18 -0700 (PDT) From: Vincenzo Frascino To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com Cc: vincenzo.frascino@arm.com, Catalin Marinas , Will Deacon Subject: [PATCH] mte: Initialize tag storage to KASAN_TAG_INVALID Date: Tue, 7 Jun 2022 12:31:50 +0100 Message-Id: <20220607113150.55140-1-vincenzo.frascino@arm.com> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When the kernel is entered on aarch64, the MTE allocation tags are in an UNKNOWN state. With MTE enabled, the tags are initialized: - When a page is allocated and the user maps it with PROT_MTE. - On allocation, with in-kernel MTE enabled (KHWASAN). If the tag pool is zeroed by the hardware at reset, it makes it difficult to track potential places where the initialization of the tags was missed. This can be observed under QEMU for aarch64, which initializes the MTE allocation tags to zero. Initialize to tag storage to KASAN_TAG_INVALID to catch potential places where the initialization of the tags was missed. This is done introducing a new kernel command line parameter "mte.tags_init" that enables the debug option. Note: The proposed solution should be considered a debug option because it might have performance impact on large machines at boot. Cc: Catalin Marinas Cc: Will Deacon Signed-off-by: Vincenzo Frascino --- arch/arm64/kernel/mte.c | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c index 57b30bcf9f21..259a826363f1 100644 --- a/arch/arm64/kernel/mte.c +++ b/arch/arm64/kernel/mte.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -35,6 +36,8 @@ DEFINE_STATIC_KEY_FALSE(mte_async_or_asymm_mode); EXPORT_SYMBOL_GPL(mte_async_or_asymm_mode); #endif +static bool mte_tags_init __ro_after_init; + static void mte_sync_page_tags(struct page *page, pte_t old_pte, bool check_swap, bool pte_is_tagged) { @@ -107,6 +110,48 @@ int memcmp_pages(struct page *page1, struct page *page2) return ret; } +/* mte.tags_init=off/on */ +static int __init early_mte_tags_init(char *arg) +{ + if (!arg) + return -EINVAL; + + if (!strcmp(arg, "off")) + mte_tags_init = false; + else if (!strcmp(arg, "on")) + mte_tags_init = true; + else + return -EINVAL; + + return 0; +} +early_param("mte.tags_init", early_mte_tags_init); + +static inline void __mte_tag_storage_init(void) +{ + static bool mte_tags_uninitialized = true; + phys_addr_t pa_start, pa_end; + u64 index; + + if (mte_tags_init && !mte_tags_uninitialized) + return; + + for_each_mem_range(index, &pa_start, &pa_end) { + void *va_start = (void *)__phys_to_virt(pa_start); + void *va_end = (void *)__phys_to_virt(pa_end); + size_t va_size = (u64)va_end - (u64)va_start; + + if (va_start >= va_end) + break; + + mte_set_mem_tag_range(va_start, va_size, KASAN_TAG_INVALID, false); + } + + /* Tags are now initialized to KASAN_TAG_INVALID */ + mte_tags_uninitialized = false; + pr_info("MTE: Tag Storage Initialized\n"); +} + static inline void __mte_enable_kernel(const char *mode, unsigned long tcf) { /* Enable MTE Sync Mode for EL1. */ @@ -114,6 +159,8 @@ static inline void __mte_enable_kernel(const char *mode, unsigned long tcf) SYS_FIELD_PREP(SCTLR_EL1, TCF, tcf)); isb(); + __mte_tag_storage_init(); + pr_info_once("MTE: enabled in %s mode at EL1\n", mode); } -- 2.36.1