From patchwork Fri May 21 01:00:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evgenii Stepanov X-Patchwork-Id: 12271639 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 A0366C433ED for ; Fri, 21 May 2021 01:03:06 +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 1AE4B611AB for ; Fri, 21 May 2021 01:03:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1AE4B611AB 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=Wevo7JcifPXS+GV5Kew1xnCPCHBO3fiWbLTWO4+MXRw=; b=nK0SsZUHB+lYmC jiy5EjnvGSga4xKuqE9tlAGqzIrF1mn/5xdkefkeWB14+z/xqfqRDvFeKumIhyjia/6H0IABJjUtp 7jMDDQEHFEIHY4KaqQyiQPWnSSnP0tSunG8jURYBnWimmPrz9YoWatxtMwYJ/yzdkI6AtMOuMnQrN Pahbe9uLbaVKFt2zbq1P+gJVQFwWXsPMyL21g8a4jsiUAabH/M3e0W5QZiLXGHaaNz4ZAIMPJwtgu LXD+grOQg9Fd4vMfB7n2thdbCa3xA1hJpic+5JZyXNf9JStFmj/WRoLyvvuLIOH02+SwA0lesYrMe wFEc0PEgLlBJmBDOQtPQ==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1ljtWs-003Bbo-4W; Fri, 21 May 2021 01:00:42 +0000 Received: from bombadil.infradead.org ([2607:7c80:54:e::133]) by desiato.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1ljtWn-003BbZ-96 for linux-arm-kernel@desiato.infradead.org; Fri, 21 May 2021 01:00: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=MP2sMnnxnX2LmSdngJ3nolM2oLXjZHItYoP+hS8tzaE=; b=yNLjAnncD4bp8oeTUi9wK1jxaq URISboKMGvV+TxNWp6wGVJrkokgOvou992tIg68E8QVeH+yE5re3XAHCkHhKsZ2s4VpDpNBqPBorn Qr47bLjpkSIHCn/rEczonKwPXhYaIliLUN1Sm7wwdaOQ7sm8JA/tUh1ACBsKtoTzX0ow642OywxSo SBuLmfD4B3DujhalIcW2ABMUmDFIC7w+fV4OH93wsS62hnyMoB2Ty23F7DAVaUHRi+YdxS48EFuBt ZUW8UnF0uc1q9b7KmQqBiAo6tWP8pgQdMYgUvP1zpaRkTzkIkvAtO2jNYfWZoeMXte4c+zHSK6SQV ccQQb2Qw==; Received: from mail-qv1-xf4a.google.com ([2607:f8b0:4864:20::f4a]) by bombadil.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1ljtWk-00GjtY-I8 for linux-arm-kernel@lists.infradead.org; Fri, 21 May 2021 01:00:35 +0000 Received: by mail-qv1-xf4a.google.com with SMTP id l19-20020a0ce5130000b02901b6795e3304so14909794qvm.2 for ; Thu, 20 May 2021 18:00:32 -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=MP2sMnnxnX2LmSdngJ3nolM2oLXjZHItYoP+hS8tzaE=; b=BwoAvtTf8usTATeG9OiejaUofx5iwWkEgQwq37n8U7yNK84zh7n2Sb2UMjiuGxMiW7 U706zjC7a8LsWOAATMOJ0Jh04bE3BNNFxAF2b08vInw7kXjhj9FSt9i07vKOlP0kvoZB 6eeIRzZpc4xvjj/iE1Gg2MUViPhRm6Orq8uP6ThM8F8v5PHb0GCo1Hf7fuA4B6uSbQXA gwWIAFhK+eLN0w8I2yStuNjnwXck7RzFajrdDLehoL0H/ftKImHumrHsIJV0iwZYSJNF qlJ41DtOawtZJ5YVruu5QOIkHO7XOl/W4oLion3YBmP8U3K51htHEtAYmmzeImt/8ciW 7kKg== 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=MP2sMnnxnX2LmSdngJ3nolM2oLXjZHItYoP+hS8tzaE=; b=R3KY+ljIQXwHGyJyr+JpigKnR3mw7F1KdRiNXuWO1fYgGeXm/pnIqKFAx8/jl7HJ4n Q2gIIDaRwkCofB6TPlmpCqL7tpd0soEXu8ynha5v29Hc4Jb0XoifHxnt+6c78xk7qC7J ngzzGxB5w6DKZgPSpww9IfShm+bmea2lmtCW8U6CtcDNWbJtPl6utcPDef98udm+OeAV tNLzOzDfBhHtdri1BS7e8nKVZzWM5Y2Jt13M0MHOq0dCX71aBbSRB+XZSOAXbfli/Jls bjzblaSq3iTMc1MgrAyXva9/qEjJmoJZDP/F81aCiQpb9nJ4Dz4BXk69HC6hF/5jvlBJ lPwQ== X-Gm-Message-State: AOAM5312gG/b3Nnwc+3/hWqj1SS6tvHGRUPMB3d83TI1hnW4QnNSEVdI FacNMYbP82x9RU/YmKKYzgaqqG6dKhKU X-Google-Smtp-Source: ABdhPJwUjMEJCrd4sDUvwQNiJo67iUx3mk1WNCVvJbfWRphbOWNDM1kr5cHpRPFXezyhMxb0WFybiOwtGj64 X-Received: from eugenis.svl.corp.google.com ([2620:15c:2ce:200:d894:cb92:45a3:f171]) (user=eugenis job=sendgmr) by 2002:a05:6214:391:: with SMTP id l17mr9195252qvy.22.1621558831163; Thu, 20 May 2021 18:00:31 -0700 (PDT) Date: Thu, 20 May 2021 18:00:23 -0700 Message-Id: <20210521010023.3244784-1-eugenis@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.31.1.818.g46aad6cb9e-goog Subject: [PATCH v5] 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-20210520_180034_629223_ACFCCB42 X-CRM114-Status: GOOD ( 19.31 ) 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 Reviewed-by: Catalin Marinas --- Changelog since v1: - Added Co-developed-by. Changelog since v2: - Added Signed-off-by. Changelog since v3: - Move the implementation back to C with a bit of inline asm. Changelog since v3: - Fixed coding style issues. arch/arm64/include/asm/mte-kasan.h | 93 +++++++++++++++++++++--------- 1 file changed, 67 insertions(+), 26 deletions(-) diff --git a/arch/arm64/include/asm/mte-kasan.h b/arch/arm64/include/asm/mte-kasan.h index ddd4d17cf9a0..d952352bd008 100644 --- a/arch/arm64/include/asm/mte-kasan.h +++ b/arch/arm64/include/asm/mte-kasan.h @@ -48,43 +48,84 @@ static inline u8 mte_get_random_tag(void) return mte_get_ptr_tag(addr); } +static inline u64 __stg_post(u64 p) +{ + asm volatile(__MTE_PREAMBLE "stg %0, [%0], #16" + : "+r"(p) + : + : "memory"); + return p; +} + +static inline u64 __stzg_post(u64 p) +{ + asm volatile(__MTE_PREAMBLE "stzg %0, [%0], #16" + : "+r"(p) + : + : "memory"); + return p; +} + +static inline void __dc_gva(u64 p) +{ + asm volatile(__MTE_PREAMBLE "dc gva, %0" : : "r"(p) : "memory"); +} + +static inline void __dc_gzva(u64 p) +{ + asm volatile(__MTE_PREAMBLE "dc gzva, %0" : : "r"(p) : "memory"); +} + /* * 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. + * size must be MTE_GRANULE_SIZE aligned. */ -static inline void mte_set_mem_tag_range(void *addr, size_t size, - u8 tag, bool init) +static inline void mte_set_mem_tag_range(void *addr, size_t size, u8 tag, + bool init) { - u64 curr, end; + u64 curr, mask, dczid_bs, end1, end2, end3; - if (!size) - return; + /* Read DC G(Z)VA block size from the system register. */ + dczid_bs = 4ul << (read_cpuid(DCZID_EL0) & 0xf); curr = (u64)__tag_set(addr, tag); - end = curr + size; + mask = dczid_bs - 1; + /* STG/STZG up to the end of the first block. */ + end1 = curr | mask; + end3 = curr + size; + /* DC GVA / GZVA in [end1, end2) */ + end2 = end3 & ~mask; /* - * 'asm volatile' is required to prevent the compiler to move - * the statement outside of the loop. + * The following code uses STG on the first DC GVA block even if the + * start address is aligned - it appears to be faster than an alignment + * check + conditional branch. Also, if the range size is at least 2 DC + * GVA blocks, the first two loops can use post-condition to save one + * branch each. */ - 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); - } +#define SET_MEMTAG_RANGE(stg_post, dc_gva) \ + do { \ + if (size >= 2 * dczid_bs) { \ + do { \ + curr = stg_post(curr); \ + } while (curr < end1); \ + \ + do { \ + dc_gva(curr); \ + curr += dczid_bs; \ + } while (curr < end2); \ + } \ + \ + while (curr < end3) \ + curr = stg_post(curr); \ + } while (0) + + if (init) + SET_MEMTAG_RANGE(__stzg_post, __dc_gzva); + else + SET_MEMTAG_RANGE(__stg_post, __dc_gva); +#undef SET_MEMTAG_RANGE } void mte_enable_kernel_sync(void);