From mboxrd@z Thu Jan 1 00:00:00 1970 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.4 required=3.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham 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 D23C1CA9EC5 for ; Wed, 30 Oct 2019 16:07:17 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 43C742080F for ; Wed, 30 Oct 2019 16:07:17 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="syTv2npP" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 43C742080F Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id B0F636B0005; Wed, 30 Oct 2019 12:07:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AC0846B0006; Wed, 30 Oct 2019 12:07:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9878C6B0008; Wed, 30 Oct 2019 12:07:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0077.hostedemail.com [216.40.44.77]) by kanga.kvack.org (Postfix) with ESMTP id 784CE6B0005 for ; Wed, 30 Oct 2019 12:07:16 -0400 (EDT) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with SMTP id F17D2180AD817 for ; Wed, 30 Oct 2019 16:07:15 +0000 (UTC) X-FDA: 76100930430.05.page62_5c6ee90534f0d X-HE-Tag: page62_5c6ee90534f0d X-Filterd-Recvd-Size: 10679 Received: from mail-pg1-f195.google.com (mail-pg1-f195.google.com [209.85.215.195]) by imf21.hostedemail.com (Postfix) with ESMTP for ; Wed, 30 Oct 2019 16:07:15 +0000 (UTC) Received: by mail-pg1-f195.google.com with SMTP id c8so1790909pgb.2 for ; Wed, 30 Oct 2019 09:07:14 -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=B06AbUd74nrlLUdzEIzJ6R859oBS/GXTayxWLig8iWg=; b=syTv2npPp6p6wk2d1EroVNn3zcUwDkRI4qpcrDod7noPxpSuZOYc8YTszQh1ZckKDr j6e5bqEHnD1D3MK46VAY2oqS6M7kXvoAmShvieE8MJaFhZOOzHOPWEBVKIJ4DzOzQ6DB ZSnC64dK/R8LedD5C4uaIAtz9Lx0M02juGrAe4TS0cZXI5R8RcCPAkkN0hFpfpL/OSnH MepTl0FQdstd2sFVAvdmbmcx9XwKOwCKOrz82swRX8pT2oH2hJzG/mikh2+ae5pGiSfU qvi4pSboxAnZm+kymMrIPIg00lOXflaQmo7XxpNPcZsRZUJBw/n0uJ3LhmGPQGnsdZkN CKWQ== 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=B06AbUd74nrlLUdzEIzJ6R859oBS/GXTayxWLig8iWg=; b=PKDj/Twmc1cPZ2fPuAABRb8i3EK9D0Z40DVwh/wCYh1Fs+I2/tOr7H5JNgSwdrh5Hc +E2KwP19fqQn557EiBMVJrYQVv6YHJP96nnl27MYgyluvuhiuEi4bG3qCnLxxStcdvXu O4+VFTqPR1whREhjTtmFZ7XGtx19JsPFBG9pSrGtpj2pqKj8TOx5Du1s9/JKsKDdcol7 YDcTqD+nT6rN97d9AtRWQZsIL97KBgzpkM9ScSIVbTpXKPuZDeBbiq2GaYTX8k5hT7Jg HAEi5CUnEQVjXPW/zT3rZjbTQXDa5J96JIVkK4aaW8oHgn1OujelIaT1z+eEq92dzQ7v dCVA== X-Gm-Message-State: APjAAAUWERB0jmupVdXjZ113gxgHAH+r7Pfkfl1Tdf/dpClz1wPdQxxe +aeqmp905QGzRpdw3s77OTgnZu4xMwQQwNTjPFVX+w== X-Google-Smtp-Source: APXvYqxOtjpOkNsNpdPbl4BXg4MZgXkPAtSOoeMssRiawM6x54EV4kS1fzhwkdyOSeDmWZmyvyDayAG/xFA2vuapb/w= X-Received: by 2002:a65:4183:: with SMTP id a3mr159293pgq.440.1572451633541; Wed, 30 Oct 2019 09:07:13 -0700 (PDT) MIME-Version: 1.0 References: <20191030142237.249532-1-glider@google.com> <20191030142237.249532-18-glider@google.com> In-Reply-To: <20191030142237.249532-18-glider@google.com> From: Andrey Konovalov Date: Wed, 30 Oct 2019 17:07:02 +0100 Message-ID: Subject: Re: [PATCH RFC v2 17/25] kmsan: mm: call KMSAN hooks from SLUB code To: Alexander Potapenko Cc: Andrew Morton , Vegard Nossum , Dmitry Vyukov , Linux Memory Management List , Alexander Viro , Andrey Ryabinin , Andy Lutomirski , Ard Biesheuvel , Arnd Bergmann , Christoph Hellwig , Dmitry Torokhov , Eric Dumazet , ericvh@gmail.com, Greg Kroah-Hartman , harry.wentland@amd.com, Herbert Xu , mingo@elte.hu, axboe@kernel.dk, "Martin K. Petersen" , Martin Schwidefsky , "Michael S . Tsirkin" , Michal Simek , pmladek@suse.com, sergey.senozhatsky@gmail.com, Steven Rostedt , Takashi Iwai , "Theodore Ts'o" , Thomas Gleixner , wsa@the-dreams.de, gor@linux.ibm.com, iii@linux.ibm.com, Mark Rutland , Matthew Wilcox , Randy Dunlap , Marco Elver Content-Type: text/plain; charset="UTF-8" X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Wed, Oct 30, 2019 at 3:23 PM wrote: > > In order to report uninitialized memory coming from heap allocations > KMSAN has to poison them unless they're created with __GFP_ZERO. > > It's handy that we need KMSAN hooks in the places where > init_on_alloc/init_on_free initialization is performed. > > Signed-off-by: Alexander Potapenko > To: Alexander Potapenko > Cc: Andrew Morton > Cc: Vegard Nossum > Cc: Dmitry Vyukov > Cc: linux-mm@kvack.org > --- > > Change-Id: I51103b7981d3aabed747d0c85cbdc85568665871 > --- > mm/slub.c | 37 +++++++++++++++++++++++++++++++------ > 1 file changed, 31 insertions(+), 6 deletions(-) > > diff --git a/mm/slub.c b/mm/slub.c > index b25c807a111f..8b7069812801 100644 > --- a/mm/slub.c > +++ b/mm/slub.c > @@ -21,6 +21,8 @@ > #include > #include > #include > +#include > +#include /* KMSAN_INIT_VALUE */ > #include > #include > #include > @@ -285,17 +287,27 @@ static void prefetch_freepointer(const struct kmem_cache *s, void *object) > prefetch(object + s->offset); > } > > +/* > + * When running under KMSAN, get_freepointer_safe() may return an uninitialized > + * pointer value in the case the current thread loses the race for the next > + * memory chunk in the freelist. In that case this_cpu_cmpxchg_double() in > + * slab_alloc_node() will fail, so the uninitialized value won't be used, but > + * KMSAN will still check all arguments of cmpxchg because of imperfect > + * handling of inline assembly. > + * To work around this problem, use KMSAN_INIT_VALUE() to force initialize the > + * return value of get_freepointer_safe(). > + */ > static inline void *get_freepointer_safe(struct kmem_cache *s, void *object) > { > unsigned long freepointer_addr; > void *p; > > if (!debug_pagealloc_enabled()) > - return get_freepointer(s, object); > + return KMSAN_INIT_VALUE(get_freepointer(s, object)); > > freepointer_addr = (unsigned long)object + s->offset; > probe_kernel_read(&p, (void **)freepointer_addr, sizeof(p)); > - return freelist_ptr(s, p, freepointer_addr); > + return KMSAN_INIT_VALUE(freelist_ptr(s, p, freepointer_addr)); > } > > static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp) > @@ -1390,6 +1402,7 @@ static inline void *kmalloc_large_node_hook(void *ptr, size_t size, gfp_t flags) > ptr = kasan_kmalloc_large(ptr, size, flags); > /* As ptr might get tagged, call kmemleak hook after KASAN. */ > kmemleak_alloc(ptr, size, 1, flags); > + kmsan_kmalloc_large(ptr, size, flags); > return ptr; > } > > @@ -1397,6 +1410,7 @@ static __always_inline void kfree_hook(void *x) > { > kmemleak_free(x); > kasan_kfree_large(x, _RET_IP_); > + kmsan_kfree_large(x); > } > > static __always_inline bool slab_free_hook(struct kmem_cache *s, void *x) > @@ -1453,6 +1467,12 @@ static inline bool slab_free_freelist_hook(struct kmem_cache *s, > } while (object != old_tail); > } > > + do { > + object = next; > + next = get_freepointer(s, object); > + kmsan_slab_free(s, object); > + } while (object != old_tail); > + > /* > * Compiler cannot detect this function can be removed if slab_free_hook() > * evaluates to nothing. Thus, catch all relevant config debug options here. > @@ -2776,6 +2796,7 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s, > if (unlikely(slab_want_init_on_alloc(gfpflags, s)) && object) > memset(object, 0, s->object_size); > > + kmsan_slab_alloc(s, object, gfpflags); > slab_post_alloc_hook(s, gfpflags, 1, &object); > > return object; > @@ -2804,6 +2825,7 @@ void *kmem_cache_alloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size) > void *ret = slab_alloc(s, gfpflags, _RET_IP_); > trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags); > ret = kasan_kmalloc(s, ret, size, gfpflags); > + Looks like unrelated change. > return ret; > } > EXPORT_SYMBOL(kmem_cache_alloc_trace); > @@ -2816,7 +2838,6 @@ void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node) > > trace_kmem_cache_alloc_node(_RET_IP_, ret, > s->object_size, s->size, gfpflags, node); > - Same here. > return ret; > } > EXPORT_SYMBOL(kmem_cache_alloc_node); > @@ -2832,6 +2853,7 @@ void *kmem_cache_alloc_node_trace(struct kmem_cache *s, > size, s->size, gfpflags, node); > > ret = kasan_kmalloc(s, ret, size, gfpflags); > + And here. > return ret; > } > EXPORT_SYMBOL(kmem_cache_alloc_node_trace); > @@ -3157,7 +3179,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, > void **p) > { > struct kmem_cache_cpu *c; > - int i; > + int i, j; > > /* memcg and kmem_cache debug support */ > s = slab_pre_alloc_hook(s, flags); > @@ -3198,11 +3220,11 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, > > /* Clear memory outside IRQ disabled fastpath loop */ > if (unlikely(slab_want_init_on_alloc(flags, s))) { > - int j; > - > for (j = 0; j < i; j++) > memset(p[j], 0, s->object_size); > } > + for (j = 0; j < i; j++) > + kmsan_slab_alloc(s, p[j], flags); > > /* memcg and kmem_cache debug support */ > slab_post_alloc_hook(s, flags, size, p); > @@ -3803,6 +3825,7 @@ static int __init setup_slub_min_objects(char *str) > > __setup("slub_min_objects=", setup_slub_min_objects); > > +__no_sanitize_memory > void *__kmalloc(size_t size, gfp_t flags) > { > struct kmem_cache *s; > @@ -5717,6 +5740,7 @@ static char *create_unique_id(struct kmem_cache *s) > p += sprintf(p, "%07u", s->size); > > BUG_ON(p > name + ID_STR_LENGTH - 1); > + kmsan_unpoison_shadow(name, p - name); > return name; > } > > @@ -5866,6 +5890,7 @@ static int sysfs_slab_alias(struct kmem_cache *s, const char *name) > al->name = name; > al->next = alias_list; > alias_list = al; > + kmsan_unpoison_shadow(al, sizeof(struct saved_alias)); > return 0; > } > > -- > 2.24.0.rc0.303.g954a862665-goog >