From patchwork Mon May 17 23:55:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evgenii Stepanov X-Patchwork-Id: 12263369 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.1 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 B6ACEC433ED for ; Mon, 17 May 2021 23:58:05 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (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 479596113C for ; Mon, 17 May 2021 23:58:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 479596113C 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=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:To:From:Subject:Mime-Version:Message-Id:Date: Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=B0Tvu9T4BXkJfgT7bheuibPqv0M09z96RH/ZWAN57rk=; b=nP4OUrRhT0z5zN UaNK53+SthCjY3mhg9bl8iimq8C+6Zaibl7hAIRhhKVQPkizlmvAxQYTm2p2N2UWFvvFUt+8ZFCzE God7DgnMNCJBsxISZwyQ5U0momVS4CRi9CNxI0vPmlolGNaGowDaZEixPrbOVa8kgP6qJgKDhP2OB 6kdPQXvQNhaxJ/kg52vkyI3BvddSqjAumJ0tZUiB+DxCcJ9Tu60abI4GjaLOWYpQmOOoiDrF7SDfx CbIAG9exCUC16CyLGLAjMoiERBbLgQJf6xvBFTXxzjkCoa/0PLJA0K2WKr9/3KRpvVCT+0EWPWYfO 2InOrpLsNkkjrZs3+akw==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lin5c-00GSA0-7t; Mon, 17 May 2021 23:56:00 +0000 Received: from bombadil.infradead.org ([2607:7c80:54:e::133]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lin5Z-00GS9t-JS for linux-arm-kernel@desiato.infradead.org; Mon, 17 May 2021 23:55:57 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=Content-Type:To:From:Subject: Mime-Version:Message-Id:Date:Sender:Reply-To:Cc:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To:References; bh=pJYdEWZccGedi7sOusNpe9uQTsCIvJdO8bX0ACEO4b0=; b=tIBDjZEZrfAoXMmuB5byyJohOJ A5GAh+MGCP0SNUy6jSOSR8Df12m/XCaMeeUtcUbgL0VkeO8CmKnbvhUsM9dDJHUnS7ZFs+Dp2woPF OgmyjfE+kMp/gowUrRrrlmPOc28Va3W8MwaOHvUtqvHrHU8Abf31SaLTuTMHX0YPkQAmYoTmq8GF8 8glQ6K0FFoeyj2zDl/XPd3fJq3voj6bxFn4FJ/5dXfjJO7FgPrH30Awq94fntOiEcsRoynCN3YP6l ECKEVLc/sva+b9t1B7+Pc7IsAGGH+V05CTjHGyrAb9Qrkgj0twaufFo8p4Ytz4xjHnYOPzoipA7VO s29nxECg==; Received: from mail-qv1-xf49.google.com ([2607:f8b0:4864:20::f49]) by bombadil.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lin5W-00EDjn-IQ for linux-arm-kernel@lists.infradead.org; Mon, 17 May 2021 23:55:56 +0000 Received: by mail-qv1-xf49.google.com with SMTP id a29-20020a0ca99d0000b02901ec0ad2c871so6053672qvb.0 for ; Mon, 17 May 2021 16:55:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to; bh=pJYdEWZccGedi7sOusNpe9uQTsCIvJdO8bX0ACEO4b0=; b=f7J+W6v+cr2PexYHfeEZy6Gc7Yj9Pu4xSLYL2CxJn9ftrGcNzcXGtQtLLHYiNa0trr f6TUFbKbBs4imcupnDLerV721OZXfVE2ZApMrj44/gnlWTt8gku6Tx811Ik3i32BaoAC Wd2My8imlxuiTqdHrOVaW+qcO+5tAZ+/U+uCxNPv5x0TF95wdGQtH3frqSOZvcmrzqrs dh2Q0NWBRb0dB4IU6FXfy1ZFFWcgj0mCnLTvT40yh6uWqnAkQbG4iNJW6tY6lqe/ISK6 gkCUmGIds35U0jA8qit1Uuf6YeiEhRb7cj7ErEIbIuteF2ynSFmsbDwjYSufboRYFeyj 8k9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to; bh=pJYdEWZccGedi7sOusNpe9uQTsCIvJdO8bX0ACEO4b0=; b=Z8gvmdAV8inIjopQxT+mE+RccehlWws0TrBg45yvw2x0SDRpencFgKIrcU6G5a+eDJ qYryNG7re2OPx+qyz6i0Bpa6XzR/XzWW/4LzkDSyAg2/0b7bp4/NBPPXEEKn3ZtQVlCb M6nucnRLi1jOgJ4ulITrDQxEEnwzX99Z2yd6ph0TkXFCzm58Wu31JrvyNZMaZAzUTif8 DDbSXMvpgenXZgU7Y+IE+gU6dSi3daZWaCc/b+3aDeUUe8otAOwz7yuqLkNxk4D+HDD6 8fjX12tuXvUkpmy7XlI5xPNhLuA2AO+znxguaYtXigEQJqfHOgqsISRqoR3AGZF13GVM 2mdw== X-Gm-Message-State: AOAM530I1HtpZxPWNAwl3YMUdJNpcDFKz16nzQU/udO/UypZLFlK9uJZ ZvCGBvnn4rEKjkr4+4EwXLc2cFmqEF4T X-Google-Smtp-Source: ABdhPJzQgxhP49V5cIDudbSANXAj+t7IoJ7LIiohVBZ8KIyNsMBpRJ3UdsPcmL91UtEftGnPLXhekWC4NQAC X-Received: from eugenis.svl.corp.google.com ([2620:15c:2ce:200:c60e:2f76:b979:5cae]) (user=eugenis job=sendgmr) by 2002:a0c:e6c5:: with SMTP id l5mr2636123qvn.2.1621295752535; Mon, 17 May 2021 16:55:52 -0700 (PDT) Date: Mon, 17 May 2021 16:55:46 -0700 Message-Id: <20210517235546.3038875-1-eugenis@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.31.1.751.gd2f1c929bd-goog Subject: [PATCH v3] kasan: speed up mte_set_mem_tag_range From: Evgenii Stepanov To: Andrey Ryabinin , Alexander Potapenko , Andrey Konovalov , Dmitry Vyukov , Catalin Marinas , Will Deacon , Steven Price , Peter Collingbourne , Evgenii Stepanov , kasan-dev@googlegroups.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210517_165554_650260_F8747D73 X-CRM114-Status: GOOD ( 16.71 ) 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 Use DC GVA / DC GZVA to speed up KASan memory tagging in HW tags mode. The first cacheline is always tagged using STG/STZG even if the address is cacheline-aligned, as benchmarks show it is faster than a conditional branch. Signed-off-by: Evgenii Stepanov Co-developed-by: Peter Collingbourne Signed-off-by: Peter Collingbourne --- Changelog since v1: - Added Co-developed-by. Changelog since v2: - Added Signed-off-by. arch/arm64/include/asm/mte-kasan.h | 40 +------------------ arch/arm64/lib/Makefile | 2 + arch/arm64/lib/mte-kasan.S | 63 ++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 39 deletions(-) create mode 100644 arch/arm64/lib/mte-kasan.S diff --git a/arch/arm64/include/asm/mte-kasan.h b/arch/arm64/include/asm/mte-kasan.h index ddd4d17cf9a0..e29a0e2ab35c 100644 --- a/arch/arm64/include/asm/mte-kasan.h +++ b/arch/arm64/include/asm/mte-kasan.h @@ -48,45 +48,7 @@ static inline u8 mte_get_random_tag(void) return mte_get_ptr_tag(addr); } -/* - * Assign allocation tags for a region of memory based on the pointer tag. - * Note: The address must be non-NULL and MTE_GRANULE_SIZE aligned and - * size must be non-zero and MTE_GRANULE_SIZE aligned. - */ -static inline void mte_set_mem_tag_range(void *addr, size_t size, - u8 tag, bool init) -{ - u64 curr, end; - - if (!size) - return; - - curr = (u64)__tag_set(addr, tag); - end = curr + size; - - /* - * 'asm volatile' is required to prevent the compiler to move - * the statement outside of the loop. - */ - if (init) { - do { - asm volatile(__MTE_PREAMBLE "stzg %0, [%0]" - : - : "r" (curr) - : "memory"); - curr += MTE_GRANULE_SIZE; - } while (curr != end); - } else { - do { - asm volatile(__MTE_PREAMBLE "stg %0, [%0]" - : - : "r" (curr) - : "memory"); - curr += MTE_GRANULE_SIZE; - } while (curr != end); - } -} - +void mte_set_mem_tag_range(void *addr, size_t size, u8 tag, bool init); void mte_enable_kernel_sync(void); void mte_enable_kernel_async(void); void mte_init_tags(u64 max_tag); diff --git a/arch/arm64/lib/Makefile b/arch/arm64/lib/Makefile index d31e1169d9b8..c06ada79a437 100644 --- a/arch/arm64/lib/Makefile +++ b/arch/arm64/lib/Makefile @@ -18,3 +18,5 @@ obj-$(CONFIG_CRC32) += crc32.o obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o obj-$(CONFIG_ARM64_MTE) += mte.o + +obj-$(CONFIG_KASAN_HW_TAGS) += mte-kasan.o diff --git a/arch/arm64/lib/mte-kasan.S b/arch/arm64/lib/mte-kasan.S new file mode 100644 index 000000000000..9f6975e2af60 --- /dev/null +++ b/arch/arm64/lib/mte-kasan.S @@ -0,0 +1,63 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2021 Google Inc. + */ +#include +#include + +#include + + .arch armv8.5-a+memtag + + .macro __set_mem_tag_range, stg, gva, start, size, linesize, tmp1, tmp2, tmp3 + add \tmp3, \start, \size + cmp \size, \linesize, lsl #1 + b.lt .Lsmtr3_\@ + + sub \tmp1, \linesize, #1 + bic \tmp2, \tmp3, \tmp1 + orr \tmp1, \start, \tmp1 + +.Lsmtr1_\@: + \stg \start, [\start], #MTE_GRANULE_SIZE + cmp \start, \tmp1 + b.lt .Lsmtr1_\@ + +.Lsmtr2_\@: + dc \gva, \start + add \start, \start, \linesize + cmp \start, \tmp2 + b.lt .Lsmtr2_\@ + +.Lsmtr3_\@: + cmp \start, \tmp3 + b.ge .Lsmtr4_\@ + \stg \start, [\start], #MTE_GRANULE_SIZE + b .Lsmtr3_\@ +.Lsmtr4_\@: + .endm + +/* + * Assign allocation tags for a region of memory based on the pointer tag. + * Note: The address must be non-NULL and MTE_GRANULE_SIZE aligned and + * size must be non-zero and MTE_GRANULE_SIZE aligned. + * x0 - start address + * x1 - region size + * x2 - tag + * x3 - bool init + */ +SYM_FUNC_START(mte_set_mem_tag_range) + mrs x4, dczid_el0 + and w4, w4, #0xf + mov x5, #4 + lsl x4, x5, x4 + + bfi x0, x2, #56, #8 + + cbz x3, .Lnoinit + __set_mem_tag_range stzg, gzva, x0, x1, x4, x2, x3, x5 + ret +.Lnoinit: + __set_mem_tag_range stg, gva, x0, x1, x4, x2, x3, x5 + ret +SYM_FUNC_END(mte_set_mem_tag_range)