Received: by 10.192.165.156 with SMTP id m28csp337925imm; Tue, 10 Apr 2018 23:10:20 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/u2UJd1wluL7Z4ZI/y5k3LaCgHstAxCsHQvdBiRg5TLWLr9kg8//cJjmvEJO6JDrE4vHGX X-Received: by 10.98.196.83 with SMTP id y80mr2886460pff.117.1523427019997; Tue, 10 Apr 2018 23:10:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523427019; cv=none; d=google.com; s=arc-20160816; b=Cd6hmppCEwh8w8idhNYxrtcXqn/TUVnBBkeJg28ihASOWFLSahiNuWkwqe1gUKE7ci LrVQl1rF0ailS6rCJaJleGvreXUnTXyVz9juxwHSIRSOMnRYKdyFHfhxpxbCobC3HGdW sPrSox1Ma/KG/M3dlZPtWfbQlP+2D/GdBpHSMlZ7BgwapoXdR9XuYILEVr5Et7aPyq2H QpJuwkn1wtdiXeaKt3M1HniV1eXIMGoPu503C2JhVZyrfXCju7abDKZi+Msyb+OmUvkH ZJIGX2gxBl3ti4+wWVd98xsrhk5qQo5ca07tWNnXZ3lj/JZ3JHOjKh7G9tGo0OJ+p+kn LP+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=3K9KGBThVg/j0KRiczfJZUUjqWbwbc9tesjZJk9BYsM=; b=RxC/v5DvaSNFgc2StcXeRW6tGAZ87m8SKOd8qmRIi7WnvARbFxCNRlBiHl0Ga+ARwP /auePrpQwgKRRE9AvjW0pcOX1qPESnpiGq11+VDyWr1SyWaA9/uBtFay8/2cRzvBuuqU Mg55a2aNECmMjmDyap+UTIe0zDiPzXJPwbiMdZHBZ9Xd/wy0pvEiJEyt0sPTHSBzWIlX 5de6bF3IwyJR9CZjIe/rWlsu/WxPw59mY0Zo7LV1SOF9qAZAsvqvPBN+2sj/PxfGQ3dS 2bp3Em5f2JXnrXtNB/4eKPrgK//kbQdHS7WQua4s+Y/UGmJtaEYEfn12xMsV2dN/sXXZ Q+ag== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=VC2IZKPz; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f12si273812pgr.829.2018.04.10.23.09.42; Tue, 10 Apr 2018 23:10:19 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=VC2IZKPz; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752244AbeDKGDt (ORCPT + 99 others); Wed, 11 Apr 2018 02:03:49 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:47316 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751921AbeDKGDa (ORCPT ); Wed, 11 Apr 2018 02:03:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=References:In-Reply-To:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=3K9KGBThVg/j0KRiczfJZUUjqWbwbc9tesjZJk9BYsM=; b=VC2IZKPzbOpz+W9HPha2Y4YlC lS3Cl1Kmnjf9yAFyRsk1F/B8m/GSv1Oy7AXSGhskoJZO9x25i12S1q304N+P6akhjmSs9S+3Fj7gx SY4MAG3ZZwHxMmKaXFBx8zhv8OVEUlMkFfW71/5m1C/wsAMrcoXFfKoKraxo61aipMX58Df5vF5Qx 4xhe7sVYo2qXJJcrPBH1ret8kySBW9ieZuYPG8rZoe/xtwh5XTC1i35d7ETuigFq/En2k0L7+n+Ea OIJemRvKgRycg71W6t+evpmwt3Mt4qTVmM9PH17LWUm5co5WEU8KTqATvSp2RBSfddCoJgylZBOO+ m/qKkUhyg==; Received: from willy by bombadil.infradead.org with local (Exim 4.90_1 #2 (Red Hat Linux)) id 1f68qp-0003mC-Np; Wed, 11 Apr 2018 06:03:23 +0000 From: Matthew Wilcox To: linux-mm@kvack.org Cc: Matthew Wilcox , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , linux-kernel@vger.kernel.org, Jan Kara , Jeff Layton , Mel Gorman Subject: [PATCH v2 2/2] slab: __GFP_ZERO is incompatible with a constructor Date: Tue, 10 Apr 2018 23:03:20 -0700 Message-Id: <20180411060320.14458-3-willy@infradead.org> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180411060320.14458-1-willy@infradead.org> References: <20180411060320.14458-1-willy@infradead.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Matthew Wilcox __GFP_ZERO requests that the object be initialised to all-zeroes, while the purpose of a constructor is to initialise an object to a particular pattern. We cannot do both. Add a warning to catch any users who mistakenly pass a __GFP_ZERO flag when allocating a slab with a constructor. Fixes: d07dbea46405 ("Slab allocators: support __GFP_ZERO in all allocators") Signed-off-by: Matthew Wilcox Acked-by: Johannes Weiner Acked-by: Vlastimil Babka --- mm/slab.c | 7 ++++--- mm/slab.h | 7 +++++++ mm/slob.c | 4 +++- mm/slub.c | 5 +++-- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/mm/slab.c b/mm/slab.c index 58c8cecc26ab..9ad85fd9fca8 100644 --- a/mm/slab.c +++ b/mm/slab.c @@ -2661,6 +2661,7 @@ static struct page *cache_grow_begin(struct kmem_cache *cachep, invalid_mask, &invalid_mask, flags, &flags); dump_stack(); } + BUG_ON(cachep->ctor && (flags & __GFP_ZERO)); local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK); check_irq_off(); @@ -3325,7 +3326,7 @@ slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid, local_irq_restore(save_flags); ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller); - if (unlikely(flags & __GFP_ZERO) && ptr) + if (unlikely(flags & __GFP_ZERO) && ptr && slab_no_ctor(cachep)) memset(ptr, 0, cachep->object_size); slab_post_alloc_hook(cachep, flags, 1, &ptr); @@ -3382,7 +3383,7 @@ slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller) objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller); prefetchw(objp); - if (unlikely(flags & __GFP_ZERO) && objp) + if (unlikely(flags & __GFP_ZERO) && objp && slab_no_ctor(cachep)) memset(objp, 0, cachep->object_size); slab_post_alloc_hook(cachep, flags, 1, &objp); @@ -3589,7 +3590,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, cache_alloc_debugcheck_after_bulk(s, flags, size, p, _RET_IP_); /* Clear memory outside IRQ disabled section */ - if (unlikely(flags & __GFP_ZERO)) + if (unlikely(flags & __GFP_ZERO) && slab_no_ctor(s)) for (i = 0; i < size; i++) memset(p[i], 0, s->object_size); diff --git a/mm/slab.h b/mm/slab.h index 3cd4677953c6..896818c7b30a 100644 --- a/mm/slab.h +++ b/mm/slab.h @@ -515,6 +515,13 @@ static inline void dump_unreclaimable_slab(void) void ___cache_free(struct kmem_cache *cache, void *x, unsigned long addr); +static inline bool slab_no_ctor(struct kmem_cache *s) +{ + if (IS_ENABLED(CONFIG_DEBUG_VM)) + return !WARN_ON_ONCE(s->ctor); + return true; +} + #ifdef CONFIG_SLAB_FREELIST_RANDOM int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count, gfp_t gfp); diff --git a/mm/slob.c b/mm/slob.c index 1a46181b675c..958173fd7c24 100644 --- a/mm/slob.c +++ b/mm/slob.c @@ -556,8 +556,10 @@ static void *slob_alloc_node(struct kmem_cache *c, gfp_t flags, int node) flags, node); } - if (b && c->ctor) + if (b && c->ctor) { + WARN_ON_ONCE(flags & __GFP_ZERO); c->ctor(b); + } kmemleak_alloc_recursive(b, c->size, 1, c->flags, flags); return b; diff --git a/mm/slub.c b/mm/slub.c index a28488643603..9f8f38a552e5 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -1576,6 +1576,7 @@ static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node) if (gfpflags_allow_blocking(flags)) local_irq_enable(); + BUG_ON(s->ctor && (flags & __GFP_ZERO)); flags |= s->allocflags; @@ -2725,7 +2726,7 @@ static __always_inline void *slab_alloc_node(struct kmem_cache *s, stat(s, ALLOC_FASTPATH); } - if (unlikely(gfpflags & __GFP_ZERO) && object) + if (unlikely(gfpflags & __GFP_ZERO) && object && slab_no_ctor(s)) memset(object, 0, s->object_size); slab_post_alloc_hook(s, gfpflags, 1, &object); @@ -3149,7 +3150,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, local_irq_enable(); /* Clear memory outside IRQ disabled fastpath loop */ - if (unlikely(flags & __GFP_ZERO)) { + if (unlikely(flags & __GFP_ZERO) && slab_no_ctor(s)) { int j; for (j = 0; j < i; j++) -- 2.16.3