From patchwork Mon May 17 23:40:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evgenii Stepanov X-Patchwork-Id: 12263365 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=-14.3 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_NONE, UNWANTED_LANGUAGE_BODY,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 E924EC433ED for ; Mon, 17 May 2021 23:42:50 +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 5808260FD7 for ; Mon, 17 May 2021 23:42:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5808260FD7 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=kC51h9HUJUY+MYOW+hEcFoQgcm2SWlwon/rjm0C8NmE=; b=ebMov9WtOL3+xW t4GjrH+7oibpn3r/lH3ku0RR7H1B0Y9gUOWBLJztZd1QgJJSIHPdRv8WG4eIPKuJ1OTxU1P2iPMaa rHPhNIwPVlTdvXE278vUQLm0Ad6dAtJTJDhmgElmrqh3mhniRcwMDCQxG+KATJ7xGvkoZ1KlEnYL9 KuWfZBoTDJ2nU9pVW+eeKp48HjMCox4knktyj3OK56H7CW2N08Rhvg9iTi8/cuiiH1LpE3WUAezFD xzFyXO0EIyPZuh7yMCiNQnkZfeneyOcaNAofzwiUbQaD9PIgcRsmTbCcNXJGveno1BIv3JFn2ClzF rphVi5LxpvSSzl0g3ivg==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1limqo-00GQcT-Ae; Mon, 17 May 2021 23:40:42 +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 1limqk-00GQcE-PH for linux-arm-kernel@desiato.infradead.org; Mon, 17 May 2021 23:40:39 +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=coDTUdE+DGzuNtOpTVsbEzy4lno8tcR+JrndAuiFaAY=; b=vKDZ7Mj472NyJwSfqn7sHMx+9n UFwMStWu6BGT6UW1O0KIA/gJ4nGCIa7iK7nu3IKW3Psl3RX4junKFrn2vg2LpL0+CqFN0Vj62fUJI UOaDqu51f4bdagpE5YAJVZJl7c4pfjSPm4DZyNX3xQ7uHkNM3r26nkUzcAVtkbVRPwp191Wcd6CaE HGSx2R7aBAVZBqspBWjFAy8ajLr/EhfDgZuxZiW7jlEUZbKRHy5/twP19P6DMZLWBGexNH4secxqX zxXeBhwqxEDHwyWbWy+KxZ48Nu6y6qISQvYVNAOYN0hANB5J5ofLf4CS81EVMfjKrhvZMIqYM+kPG LUOk3IvA==; 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 1limqh-00EDFv-VT for linux-arm-kernel@lists.infradead.org; Mon, 17 May 2021 23:40:37 +0000 Received: by mail-yb1-xb49.google.com with SMTP id c9-20020a2580c90000b02904f86395a96dso11302910ybm.19 for ; Mon, 17 May 2021 16:40:34 -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=coDTUdE+DGzuNtOpTVsbEzy4lno8tcR+JrndAuiFaAY=; b=ETZHVsVLM6zgfAvFBtbD1eu4omETNnBZpVC9qpg6rPwd/2LA3IgM3nHyM2dtZp36+a tVAtGh/v9soIJc06yJM/EsjEhwtONB5uFWnRaQE/A6mk4QdzJxFwVcBvnQcVci4jYpeB M9Rm8wdkNb11GHZ8gZu+j1QikI//+KGAodS/1iFfuSpQ1Q9cKd+d7zro7NFiWJv2BnNd eI3OpC/2OL+nwVTrxy1YUtjyd/5h/9PeHTc5Kl2CjMug5EppZ1X1RvUyWv85BpSkxixJ FvzsFW3nokBYvmaCXZq/eJNitnvl5XLy5r/iTr15RFL/KWg5ux9RUotXKQBoXeZviB6/ Nlmw== 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=coDTUdE+DGzuNtOpTVsbEzy4lno8tcR+JrndAuiFaAY=; b=FLDXm+FeuEkNo3dz6Trsdi0zdIihuNt6SJnjKpj+K9NQMI2sRC52OyKXNQD1YOF+fR q9iDR/d1NP0IMmN4uQCn5rBW1Y4sJN24QqyZ02JRzXv54HlmOX9RBpyNf3qg4MfsLzQU hxPgT+svFeGBohbK8uURGwTkOTQa2tv0pluly4FLjMtbdMj/ZHLGsuYkrp4AuaFTH4N/ nMk+cYJejfyfakoqUIdY26951Lcnlb2/gpcguQu2yKk73N0BfniZPsEDetp9rEhuJX5F LLrHIMccq/fhLRaObmlzbGfr5jq+VrCs+u1199ffVWqP4LZ9XWm1wxDTkOzxx/L1VQut I0AA== X-Gm-Message-State: AOAM532iG4LnctZ/0DwvFDeR/iKpkkNVATYhKsaEbob/6ExBiRR2ZAo4 JUCsnHGciYVO48M4Gi3sb6FrdDeCcUVE X-Google-Smtp-Source: ABdhPJzynIaiP53CBZP4ukqMMREB5dMDyFdgXUpZpI0qqMj0RK0GzXOoaDstQ3Y0tBa3AMBmrsgXG/JEmFGz X-Received: from eugenis.svl.corp.google.com ([2620:15c:2ce:200:c60e:2f76:b979:5cae]) (user=eugenis job=sendgmr) by 2002:a5b:84c:: with SMTP id v12mr3203460ybq.77.1621294833200; Mon, 17 May 2021 16:40:33 -0700 (PDT) Date: Mon, 17 May 2021 16:40:18 -0700 Message-Id: <20210517234018.3031003-1-eugenis@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.31.1.751.gd2f1c929bd-goog Subject: [PATCH v2] 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_164036_036437_654ED2EA X-CRM114-Status: GOOD ( 17.02 ) 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 --- Changelog since v1: - Added Co-developed-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)