From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-io1-f70.google.com (mail-io1-f70.google.com [209.85.166.70]) by kanga.kvack.org (Postfix) with ESMTP id 2754F8E0001 for ; Fri, 21 Sep 2018 10:28:30 -0400 (EDT) Received: by mail-io1-f70.google.com with SMTP id w19-v6so21527676ioa.10 for ; Fri, 21 Sep 2018 07:28:30 -0700 (PDT) Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id k77-v6sor16227261iok.317.2018.09.21.07.28.28 for (Google Transport Security); Fri, 21 Sep 2018 07:28:29 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Andrey Konovalov Date: Fri, 21 Sep 2018 16:28:27 +0200 Message-ID: Subject: Re: [PATCH v8 16/20] kasan: add hooks implementation for tag-based mode Content-Type: text/plain; charset="UTF-8" Sender: owner-linux-mm@kvack.org List-ID: To: Dmitry Vyukov Cc: Andrey Ryabinin , Alexander Potapenko , Catalin Marinas , Will Deacon , Christoph Lameter , Andrew Morton , Mark Rutland , Nick Desaulniers , Marc Zyngier , Dave Martin , Ard Biesheuvel , "Eric W . Biederman" , Ingo Molnar , Paul Lawrence , Geert Uytterhoeven , Arnd Bergmann , "Kirill A . Shutemov" , Greg Kroah-Hartman , Kate Stewart , Mike Rapoport , kasan-dev , "open list:DOCUMENTATION" , LKML , Linux ARM , linux-sparse@vger.kernel.org, Linux-MM , "open list:KERNEL BUILD + fi..." , Kostya Serebryany , Evgeniy Stepanov , Lee Smith , Ramana Radhakrishnan , Jacob Bramley , Ruben Ayrapetyan , Jann Horn , Mark Brand , Chintan Pandya , Vishwath Mohan On Fri, Sep 21, 2018 at 1:37 PM, Dmitry Vyukov wrote: > On Wed, Sep 19, 2018 at 8:54 PM, Andrey Konovalov wrote: >> + /* >> + * Since it's desirable to only call object contructors ones during > > s/ones/once/ Will fix. > >> + * slab allocation, we preassign tags to all such objects. > > While we are here, it can make sense to mention that we can't repaint > objects with ctors after reallocation (even for > non-SLAB_TYPESAFE_BY_RCU) because the ctor code can memorize pointer > to the object somewhere (e.g. in the object itself). Then if we > repaint it, the old memorized pointer will become invalid. Will mention. >> - kasan_unpoison_shadow(object, size); >> + /* See the comment in kasan_init_slab_obj regarding preassigned tags */ >> + if (IS_ENABLED(CONFIG_KASAN_SW_TAGS) && >> + (cache->ctor || cache->flags & SLAB_TYPESAFE_BY_RCU)) { >> +#ifdef CONFIG_SLAB >> + struct page *page = virt_to_page(object); >> + >> + tag = (u8)obj_to_index(cache, page, (void *)object); >> +#else >> + tag = get_tag(object); >> +#endif > > This kinda _almost_ matches the chunk of code in kasan_init_slab_obj, > but not exactly. Wonder if there is some nice way to unify this code? > > Maybe something like: > > static u8 tag_for_object(struct kmem_cache *cache, const void *object, new bool) > { > if (!IS_ENABLED(CONFIG_KASAN_SW_TAGS) || > !cache->ctor && !(cache->flags & SLAB_TYPESAFE_BY_RCU)) > return random_tag(); > #ifdef CONFIG_SLAB > struct page *page = virt_to_page(object); > return (u8)obj_to_index(cache, page, (void *)object); > #else > return new ? random_tag() : get_tag(object); > #endif > } > > Then we can call this in both places. Will do, however I think it's better to do the CONFIG_KASAN_SW_TAGS check outside this helper function. > As a side effect this will assign tags to pointers during slab > initialization even if we don't have ctors, but it should be fine (?). We don't have to assign tag in this case, can just leave 0xff.