Received: by 2002:a05:6a10:6006:0:0:0:0 with SMTP id w6csp408148pxa; Thu, 27 Aug 2020 05:56:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzu+Ql1Ym1tOLq3Syw4emjRWyn7hVhZZCUgD0YXRxDVUK/p4Dt7NFKTkvYiyGDZKYiHCDWe X-Received: by 2002:a50:ed1a:: with SMTP id j26mr10207069eds.352.1598532969215; Thu, 27 Aug 2020 05:56:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598532969; cv=none; d=google.com; s=arc-20160816; b=IOT81AsuNqGZRkuht0OfDdbbsQYv9VpjAqv7ApJfBIU1mBFkQi9vZzVZPkwttNznXT hQwZ4rfrvlHlTMAsa/VFQRk8GBr1/AqTOiqW5p6lOyEa3/RnVBCV9lagrZk8NxyWDepu irki4Xb2SEqWDwDdXnwn/bDrEjGyYHjCKLBsvSmZ2w2bbVxGrgvxJPvfzUnwCkXObOSY jVf9ioG7q7M6gjBSmzzsWyacg6QBZYh6GFl1HmWXHslXJlfl273//4eK8x7bQSSxhftz wBLXD/zKNSf2NspSU8mKIkRhNuGYGYo1hIPduNeA9EfzzYKysQDnUGBl7opYkZ+7vP1I e1NQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=BHmZEvhLEtaO7aqOAe1Kz0vxpzzyDph3TU1ME4obtnc=; b=0BUKw2ey94nC84TuYM89d8lXN8lcINt0kevjDNzjmIKXFpqzX0YX4wNSRD6EIuN0gs CwhDHRY/lWRhim5E6bp7g4Fpfc6e17qBeJEN4kQM3OI91N9QeU4QG408ZjHjGZYUfEM5 t7x+1c8KiV/r9uQ+arYBOjK6ro0Yfu34X5lKajn5SAu6Mnhdt46MTmsg+0lc51RI8Uu4 YCtAZs+e9edZXTEhg9kHdtHJWcmgup6WQgOt6kYW9crnhIRgmI8OPbAsgOyJqZ+yQ5ab Mmnm4aNU96Fo6zBM4ibWBOsmEbDxOwx9zsosrLmFQScXYvxOhq9hIiQ6QSvTwVcfedlw oCCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=gdEkfTqR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h1si1403043edt.387.2020.08.27.05.55.46; Thu, 27 Aug 2020 05:56:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=gdEkfTqR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729154AbgH0MuA (ORCPT + 99 others); Thu, 27 Aug 2020 08:50:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41848 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729129AbgH0Mqa (ORCPT ); Thu, 27 Aug 2020 08:46:30 -0400 Received: from mail-pj1-x1043.google.com (mail-pj1-x1043.google.com [IPv6:2607:f8b0:4864:20::1043]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57676C061264 for ; Thu, 27 Aug 2020 05:46:30 -0700 (PDT) Received: by mail-pj1-x1043.google.com with SMTP id kx11so2530723pjb.5 for ; Thu, 27 Aug 2020 05:46:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=BHmZEvhLEtaO7aqOAe1Kz0vxpzzyDph3TU1ME4obtnc=; b=gdEkfTqRbB/Kce9J4U43NYTJ0FSw5bKcLylX1r5TuuVtKYudxTe4eisWe7++garpR3 AIMwQObELacZK6InF/KInf7LuBEgBr6AaRHQStv9BaZVNp6lw7JL0/vXQnlmoNs1v9jh UvrUCzc9kXrr+f1K62TpVeUwHjaTrQM0Q8ckQazsHehf9HIC23NGw5PmXOgku21EWYw2 ju1tontr5nq6w6KtK+/MwdSriWs0LnK1SptCBWazsG9VU3nERavUd14/lRcn/w1Q0396 A1OBPTCV1T9KDVtn6B6aMYmFHLFQKXRCnkc264DNEfKyXuPKbpWhGoPvpgJoroJWzE5h 8vWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=BHmZEvhLEtaO7aqOAe1Kz0vxpzzyDph3TU1ME4obtnc=; b=tgU52QZHyWBqbQ7DOVNQ2vViAy7tuJA8bQbmPXKHKaD7kdq5yiC2AM9e12ctWtUl4n PNHXz3ZaXQaGggcSKPyU2Ql57X4o3zbaRT/Jbii1ju9wu14IxCXTGQcztD3HdT+GcuUW 5BwL5TCzv23vKtBJBgQ9l2O3XE/IQ/Zp8bSctL/iwjsD6Mh3e5NkSPz9DaNBcUHDPh+N 8SkgQfqCVIwmsoemadpJNAP0S2MsElabZLKFEvpsj3+v/f/SCU1KTe6wrBWsvGpmDR8f Hajj/ch78Eo38l9gG3QCuZkxjdTgQ9c4f2y8u+mBaOm64gJLk6uPgWU0tJJre1cc9Nu3 WsJQ== X-Gm-Message-State: AOAM530MX8BXc96QDpxgdOYfHXFHuNG5pDuAcg63r2RhL5O96M5VcsNH xOOmDqb8cgQjVUD/UsGpuuVOvjtmIVBxoxz4x/tzDg== X-Received: by 2002:a17:90a:2d82:: with SMTP id p2mr9503738pjd.166.1598532389629; Thu, 27 Aug 2020 05:46:29 -0700 (PDT) MIME-Version: 1.0 References: <2cf260bdc20793419e32240d2a3e692b0adf1f80.1597425745.git.andreyknvl@google.com> <20200827093808.GB29264@gaia> In-Reply-To: <20200827093808.GB29264@gaia> From: Andrey Konovalov Date: Thu, 27 Aug 2020 14:46:18 +0200 Message-ID: Subject: Re: [PATCH 20/35] arm64: mte: Add in-kernel MTE helpers To: Catalin Marinas Cc: Dmitry Vyukov , Vincenzo Frascino , kasan-dev , Andrey Ryabinin , Alexander Potapenko , Marco Elver , Evgenii Stepanov , Elena Petrova , Branislav Rankov , Kevin Brodsky , Will Deacon , Andrew Morton , Linux ARM , Linux Memory Management List , LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Aug 27, 2020 at 11:38 AM Catalin Marinas wrote: > > On Fri, Aug 14, 2020 at 07:27:02PM +0200, Andrey Konovalov wrote: > > diff --git a/arch/arm64/include/asm/mte.h b/arch/arm64/include/asm/mte.h > > index 1c99fcadb58c..733be1cb5c95 100644 > > --- a/arch/arm64/include/asm/mte.h > > +++ b/arch/arm64/include/asm/mte.h > > @@ -5,14 +5,19 @@ > > #ifndef __ASM_MTE_H > > #define __ASM_MTE_H > > > > -#define MTE_GRANULE_SIZE UL(16) > > +#include > > So the reason for this move is to include it in asm/cache.h. Fine by > me but... > > > #define MTE_GRANULE_MASK (~(MTE_GRANULE_SIZE - 1)) > > #define MTE_TAG_SHIFT 56 > > #define MTE_TAG_SIZE 4 > > +#define MTE_TAG_MASK GENMASK((MTE_TAG_SHIFT + (MTE_TAG_SIZE - 1)), MTE_TAG_SHIFT) > > +#define MTE_TAG_MAX (MTE_TAG_MASK >> MTE_TAG_SHIFT) > > ... I'd rather move all these definitions in a file with a more > meaningful name like mte-def.h. The _asm implies being meant for .S > files inclusion which isn't the case. Sounds good, I'll leave fixing this and other arm64-specific comments to Vincenzo. I'll change KASAN code to use mte-def.h once I have patches where this file is renamed. > > > diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c > > index eb39504e390a..e2d708b4583d 100644 > > --- a/arch/arm64/kernel/mte.c > > +++ b/arch/arm64/kernel/mte.c > > @@ -72,6 +74,47 @@ int memcmp_pages(struct page *page1, struct page *page2) > > return ret; > > } > > > > +u8 mte_get_mem_tag(void *addr) > > +{ > > + if (system_supports_mte()) > > + addr = mte_assign_valid_ptr_tag(addr); > > The mte_assign_valid_ptr_tag() is slightly misleading. All it does is > read the allocation tag from memory. > > I also think this should be inline asm, possibly using alternatives. > It's just an LDG instruction (and it saves us from having to invent a > better function name). > > > + > > + return 0xF0 | mte_get_ptr_tag(addr); > > +} > > + > > +u8 mte_get_random_tag(void) > > +{ > > + u8 tag = 0xF; > > + > > + if (system_supports_mte()) > > + tag = mte_get_ptr_tag(mte_assign_random_ptr_tag(NULL)); > > Another alternative inline asm with an IRG instruction. > > > + > > + return 0xF0 | tag; > > +} > > + > > +void * __must_check mte_set_mem_tag_range(void *addr, size_t size, u8 tag) > > +{ > > + void *ptr = addr; > > + > > + if ((!system_supports_mte()) || (size == 0)) > > + return addr; > > + > > + tag = 0xF0 | (tag & 0xF); > > + ptr = (void *)__tag_set(ptr, tag); > > + size = ALIGN(size, MTE_GRANULE_SIZE); > > I think aligning the size is dangerous. Can we instead turn it into a > WARN_ON if not already aligned? At a quick look, the callers of > kasan_{un,}poison_memory() already align the size. > > > + > > + mte_assign_mem_tag_range(ptr, size); > > + > > + /* > > + * mte_assign_mem_tag_range() can be invoked in a multi-threaded > > + * context, ensure that tags are written in memory before the > > + * reference is used. > > + */ > > + smp_wmb(); > > + > > + return ptr; > > I'm not sure I understand the barrier here. It ensures the relative > ordering of memory (or tag) accesses on a CPU as observed by other CPUs. > While the first access here is setting the tag, I can't see what other > access on _this_ CPU it is ordered with. > > > +} > > + > > static void update_sctlr_el1_tcf0(u64 tcf0) > > { > > /* ISB required for the kernel uaccess routines */ > > diff --git a/arch/arm64/lib/mte.S b/arch/arm64/lib/mte.S > > index 03ca6d8b8670..8c743540e32c 100644 > > --- a/arch/arm64/lib/mte.S > > +++ b/arch/arm64/lib/mte.S > > @@ -149,3 +149,44 @@ SYM_FUNC_START(mte_restore_page_tags) > > > > ret > > SYM_FUNC_END(mte_restore_page_tags) > > + > > +/* > > + * Assign pointer tag based on the allocation tag > > + * x0 - source pointer > > + * Returns: > > + * x0 - pointer with the correct tag to access memory > > + */ > > +SYM_FUNC_START(mte_assign_valid_ptr_tag) > > + ldg x0, [x0] > > + ret > > +SYM_FUNC_END(mte_assign_valid_ptr_tag) > > + > > +/* > > + * Assign random pointer tag > > + * x0 - source pointer > > + * Returns: > > + * x0 - pointer with a random tag > > + */ > > +SYM_FUNC_START(mte_assign_random_ptr_tag) > > + irg x0, x0 > > + ret > > +SYM_FUNC_END(mte_assign_random_ptr_tag) > > As I said above, these two can be inline asm. > > > + > > +/* > > + * Assign allocation tags for a region of memory based on the pointer tag > > + * x0 - source pointer > > + * x1 - size > > + * > > + * Note: size is expected to be MTE_GRANULE_SIZE aligned > > + */ > > +SYM_FUNC_START(mte_assign_mem_tag_range) > > + /* if (src == NULL) return; */ > > + cbz x0, 2f > > + /* if (size == 0) return; */ > > You could skip the cbz here and just document that the size should be > non-zero and aligned. The caller already takes care of this check. > > > + cbz x1, 2f > > +1: stg x0, [x0] > > + add x0, x0, #MTE_GRANULE_SIZE > > + sub x1, x1, #MTE_GRANULE_SIZE > > + cbnz x1, 1b > > +2: ret > > +SYM_FUNC_END(mte_assign_mem_tag_range) > > -- > Catalin > > -- > You received this message because you are subscribed to the Google Groups "kasan-dev" group. > To unsubscribe from this group and stop receiving emails from it, send an email to kasan-dev+unsubscribe@googlegroups.com. > To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20200827093808.GB29264%40gaia.