From patchwork Sat May 15 01:06:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evgenii Stepanov X-Patchwork-Id: 12259333 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.4 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 43F55C433B4 for ; Sat, 15 May 2021 01:08:56 +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 B024B61278 for ; Sat, 15 May 2021 01:08:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B024B61278 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=jxVbeU9POyk1FwgXXpNg3U4MvCtAtHXyotjrp7a6jZs=; b=HOB3GUbXZPZQFY SDLpNVE+L5oV2fhVfHMnLaoIEGt8yj044KIGTlSesJO03iwOOKS3OXwCgz6BHVoGK6wGZhHdUEDeJ BvjaGf7wQqdt63flPrcXHeOm6FQ//EuNd7fP32qWpOrSghEPdb84omIHv6zRdawQ+3/sz6ncg3YVX KOqAIcnocNCUljWaGTtLu81Z0kZNDjZPqUc7wVU41W6fSz1XwkosDwdyjVwj9D4lbxynRbIkhfZrC 0sUoJ/89jqwc+LZjGT2f/NTt/0Nfe9ZLugv54POAAx6r2rzyDnVcUrAH8eFAl+ebjJgwUsS3vSrtQ oFsv7cUL488xrSlNVMVg==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lhili-009PAA-9x; Sat, 15 May 2021 01:07:02 +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 1lhile-009P9b-38 for linux-arm-kernel@desiato.infradead.org; Sat, 15 May 2021 01:06:58 +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=c6kHqF9mJQYVoqbE7Z/Zt84eKpUDeq460Qh7vhm+vhE=; b=BSF3IWTx/wrRF5OGlElbsen8lt i1KmJjti9bAw1rDXMG/0kTHfxlzxSDig/rj2YniLQGqUEjqx/KHlT1kw6bST4EZEC1XOBHypNVQIw f94VBwFIgLnH3lteeieOqYssSkvnNcS3jd8WvUPzqnWKr/j6sqJWWI48WMgKlricOUdSWjmKXYmL1 lNMGQjUZSfse78Qxn3ry8WmcIKaEGNLyxWAAzdJa9LjxO+I70wTA8p82Ts7FbPN30uXmCymxKOZYO mOkrIk8CpSIc+egXnzoksudlxHxCmywMh8a6VhzpefwlhRUtFjiKDncgfimsHucPkhf/uk4d+0m3A WVEbaMvQ==; Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lhilY-00CLTU-5u for linux-arm-kernel@lists.infradead.org; Sat, 15 May 2021 01:06:53 +0000 Received: by mail-yb1-xb49.google.com with SMTP id o12-20020a5b050c0000b02904f4a117bd74so1298272ybp.17 for ; Fri, 14 May 2021 18:06:51 -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=c6kHqF9mJQYVoqbE7Z/Zt84eKpUDeq460Qh7vhm+vhE=; b=BpCJqUDvDyGuB3hF0sUqajb22Nkkel1SKN78FTdQqPaYwQEWCe4sDEVdohBIqR6DX/ +/6W83lVKqgZ6Clx2KlQgIhw4Gmx+K4zouhLVROJl9RQGNMhmt0bryDVX8XNUpD3ha36 /TKIhcZa3CXGmUu9QCsXrRMK5X1LLUgSiUck1gH8mVHzA/mJyUQ5akQDzfHU2rWkKxvl ip93JlZg/L2e8i2NuQhx61inMdjwIAdX9veLWJn1Zf0KfMnXCQuNCHrL+XaXwWHvpX9R xnNfSKKyyi/WmgOuLmtBFXZQO3Wjs0RJFltAER+zPtMWGvFYUzfpzByiTfCu5mFutjdn 2yVA== 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=c6kHqF9mJQYVoqbE7Z/Zt84eKpUDeq460Qh7vhm+vhE=; b=XKyd3TUN2kqw1FaNhJJKFaOcSn3+kJAGWMw1zcck8EPTKtMwCgqKNAmYvbg0b//rVN PS3D/0tGd43KArS3HV7r0OUOOiplBBUMi2WiROgK3pWWnVqRcNoPv9NIT9h4d4m/gyZA 2o/bkJrfv9n7oBl4ckzXzXSdMUtDiMHVT7k9dlBHTxSU6HpJSHqaNOsBQj14/Y/Nqwiw t/dX5Bbh04VZKh0NHkG6B+Ms1goUfhC5i765KeACUetNLORYMC1qJLa6dSrWOnD8WfgO suVlGthW7J2/6lPgAecjnHp4SALCquqO2r70UuI5m0sx+I9z0ONvxU4Ve6Aqblw7Eca3 kz5Q== X-Gm-Message-State: AOAM532uApW3jJgU2aa4hO3ho0xArwESCEW4YFA8ufYEnSuoQKtF/Gse SPrMi2IUxhjpU4XbSMllHPKGo/Ci1nII X-Google-Smtp-Source: ABdhPJxu3OOXvL6IugqBKr6nA6kKnXtO3jIjivnCBCtjBjtnofku66OBwEP33Srv86w5WeKfWC8zRLtHGiHo X-Received: from eugenis.svl.corp.google.com ([2620:15c:2ce:200:9c55:d7a2:8f8f:5c4e]) (user=eugenis job=sendgmr) by 2002:a25:820e:: with SMTP id q14mr8001948ybk.152.1621040810242; Fri, 14 May 2021 18:06:50 -0700 (PDT) Date: Fri, 14 May 2021 18:06:43 -0700 Message-Id: <20210515010643.2340448-1-eugenis@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.31.1.751.gd2f1c929bd-goog Subject: [PATCH] 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-20210514_180652_265504_F6FC32BA X-CRM114-Status: GOOD ( 16.95 ) 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 --- 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)