Received: by 2002:a05:6358:111d:b0:dc:6189:e246 with SMTP id f29csp3620624rwi; Wed, 2 Nov 2022 00:47:39 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4P/ihW2KBfR3pKrWlmIOICyy2kx5nJiHRyIgRtADAgHpIs0NiaqgUNnPJsbW06Vb7RxHtI X-Received: by 2002:a05:6402:31f4:b0:461:604d:2607 with SMTP id dy20-20020a05640231f400b00461604d2607mr23887753edb.330.1667375258978; Wed, 02 Nov 2022 00:47:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667375258; cv=none; d=google.com; s=arc-20160816; b=Ia4JcTot2sRXlLJRxCdXMB6NJ08Mqirwr1JkPIzyaOpMFVkkAYq1fdTTXuQNcdEng5 TbVjBftjSUiB/woWm1Up9Z0T3EZhG6AQIF8PepAgYFRtRnchqXoaIk6p7XBSu+7EUhjA T7yR/eTjvkQS3oQDYw09aMX8B509CEHApuN44kqzjw+W+Fg8xBonf1ilmhu5hmECco1t L4Yf98AV0glvJZenPS0WwBAYg9b5BVm7gsdwWS+m9yNf0ddsp+XO45hZMpiMtOpqdb8O 4HlQP5YHkBKlUazNeJh94l9wlVJ/XH6iJgidn89usdJBWtXZ0LoTJC057nZDvP02RCeD ZyyA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=JxnaJtGnHY9c3pR6sfYcqGHFdJa7CTSVoKFDBdpZd/E=; b=G0xyhltNQJcgC8CXWBUq6qMhTqZ4pmQrMQdO4D5VYiCuBBmsCG0uD0lEgD+0VJpopQ N3zD5yiIAw5hsRfmDy1toD8WKJsyydS6W2XYNmBCp100qM44d+7tDTz39er3hru+AxHW hsGS00id104Ni/OF3ZJHj8VQNjV2K/YduexSQ2rEhjhjdF1tX1PuSTCDTi6tuz3309Yx DCvMgdPoBHBdr3BpuAK80b/rnfDHPpVxlVrXnl3TF4tfIkqA+diDn7HQ9ijVcTyxa+MP WD96pSF+dvmYA0fWuTcPRX0nz8oghs8cqyCFQFY3p90oKRskbGxRdqQdhN6jPEQejC2V hAaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=RfYfUanU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i6-20020a1709064fc600b00791a3dd01b1si16259831ejw.865.2022.11.02.00.47.15; Wed, 02 Nov 2022 00:47:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=RfYfUanU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230296AbiKBHQT (ORCPT + 97 others); Wed, 2 Nov 2022 03:16:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43476 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229909AbiKBHQR (ORCPT ); Wed, 2 Nov 2022 03:16:17 -0400 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B287F2314C; Wed, 2 Nov 2022 00:16:16 -0700 (PDT) Received: by mail-pl1-x62e.google.com with SMTP id c24so15735829pls.9; Wed, 02 Nov 2022 00:16:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=JxnaJtGnHY9c3pR6sfYcqGHFdJa7CTSVoKFDBdpZd/E=; b=RfYfUanUzsmrD4NrnTpZIgg1ZpZyGlfKzER+Us1F3hVaaryHC5ViWPZ+qmbEwqPuxd J2qSCoI5sTwQVQlLmuZh7I+FE4P94psmy3fwJ3YnEpJvsaWgy/SZawU4IwEy7h55LHwp ZfzW9i0Xl9JIXi5JTuuri+byzb++DGZWNaUFJFlQUSPL+oF6sGH9opKSDboMTikMSxsF cPXQJNrjUi/HIj9n5/4tMg9TDSnguq45BE4iOzAAMOeGtH7iW/N/HSRRTxufRWzDLb+j rCDWTTpFcK7SAN8CGxlGZny+kj6rD3HikkdTKoYhyvt+Td2WR/cwAXagCff2k/tLYjP1 AiAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=JxnaJtGnHY9c3pR6sfYcqGHFdJa7CTSVoKFDBdpZd/E=; b=Lf6mZZImi4NKfs/h2ppiuo1Pv9vnElA6MF0G8ievSyCov7Lh1RM4jTnAoCmTUSWncg RxxurLG47rS5CXlS8ATCanWfNLGfZXa8ExN+zVXuni06D1E8U6XcF0Zw6IAMyJMX0yLd tyLYW3tVWlv1hNovXFgICELDQ/chZJiOC9+PkXCQWvKg+N01GoJ0R9pVxMkZU6QMVbZ/ qEGW4ubfBACO6TzmPxj1wrno3JFxlezPqqqOW24r7i1L9zk6Kz390+6xJMyzAN3+FCF/ hVUbkoShJ177uMdTl7wz2ySieyZs6BSNt6ovp8ziADvheCAW+Zhx5+EmeUxGJ+zUSrpI ZQdA== X-Gm-Message-State: ACrzQf1LMhJdlVq7CSnqf7yqmUGZs8oH3AV+hR0gx5o10QUSpOcJxv89 8N6f45QKj2N/Jq9Ra9NKBOk= X-Received: by 2002:a17:90a:e147:b0:213:bd97:d6b7 with SMTP id ez7-20020a17090ae14700b00213bd97d6b7mr20173753pjb.199.1667373376131; Wed, 02 Nov 2022 00:16:16 -0700 (PDT) Received: from hyeyoo ([114.29.91.56]) by smtp.gmail.com with ESMTPSA id 28-20020a17090a195c00b001f8c532b93dsm747383pjh.15.2022.11.02.00.16.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Nov 2022 00:16:14 -0700 (PDT) Date: Wed, 2 Nov 2022 16:16:06 +0900 From: Hyeonggon Yoo <42.hyeyoo@gmail.com> To: Feng Tang Cc: John Thomson , Vlastimil Babka , Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , Dmitry Vyukov , Jonathan Corbet , Andrey Konovalov , "Hansen, Dave" , "linux-mm@kvack.org" , "linux-kernel@vger.kernel.org" , "kasan-dev@googlegroups.com" , Robin Murphy , John Garry , Kefeng Wang , Thomas Bogendoerfer , John Crispin , Matthias Brugger , "linux-mips@vger.kernel.org" Subject: Re: [PATCH v6 1/4] mm/slub: enable debugging memory wasting of kmalloc Message-ID: References: <9b71ae3e-7f53-4c9e-90c4-79d3d649f94c@app.fastmail.com> <53b53476-bb1e-402e-9f65-fd7f0ecf94c2@app.fastmail.com> <29271a2b-cf19-4af9-bfe5-5bcff8a23fda@app.fastmail.com> <70002fbe-34ec-468e-af67-97e4bf97819b@app.fastmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HK_RANDOM_ENVFROM, HK_RANDOM_FROM,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Nov 02, 2022 at 02:08:09PM +0800, Feng Tang wrote: > On Tue, Nov 01, 2022 at 07:39:13PM +0000, John Thomson wrote: > > > > > > On Tue, 1 Nov 2022, at 13:55, Feng Tang wrote: > > > On Tue, Nov 01, 2022 at 06:42:23PM +0800, Hyeonggon Yoo wrote: > > >> setup_arch() is too early to use slab allocators. > > >> I think slab received NULL pointer because kmalloc is not initialized. > > >> > > >> It seems arch/mips/ralink/mt7621.c is using slab too early. > > > > > > Cool! it is finally root caused :) Thanks! > > > > > > The following patch should solve it and give it a warning message, though > > > I'm not sure if there is other holes. > > > > > > Thanks, > > > Feng > > > > > > --- > > > diff --git a/mm/slab_common.c b/mm/slab_common.c > > > index 33b1886b06eb..429c21b7ecbc 100644 > > > --- a/mm/slab_common.c > > > +++ b/mm/slab_common.c > > > @@ -1043,7 +1043,14 @@ size_t __ksize(const void *object) > > > #ifdef CONFIG_TRACING > > > void *kmalloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size) > > > { > > > - void *ret = __kmem_cache_alloc_node(s, gfpflags, NUMA_NO_NODE, > > > + void *ret; > > > + > > > + if (unlikely(ZERO_OR_NULL_PTR(s))) { > > > + WARN_ON_ONCE(1); > > > + return s; > > > + } > > > + > > > + ret = __kmem_cache_alloc_node(s, gfpflags, NUMA_NO_NODE, > > > size, _RET_IP_); > > > > > > trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags, NUMA_NO_NODE); > > > diff --git a/mm/slub.c b/mm/slub.c > > > index 157527d7101b..85d24bb6eda7 100644 > > > --- a/mm/slub.c > > > +++ b/mm/slub.c > > > @@ -3410,8 +3410,14 @@ static __always_inline > > > void *__kmem_cache_alloc_lru(struct kmem_cache *s, struct list_lru *lru, > > > gfp_t gfpflags) > > > { > > > - void *ret = slab_alloc(s, lru, gfpflags, _RET_IP_, s->object_size); > > > + void *ret; > > > > > > + if (unlikely(ZERO_OR_NULL_PTR(s))) { > > > + WARN_ON_ONCE(1); > > > + return s; > > > + } > > > + Thank you for suggestion! I think the holes are: kmalloc_node_trace(), kmem_cache_alloc_node(), __do_kmalloc_node() And want to suggest: What about using VM_WARN_ON_ONCE() instead? > > > + ret = slab_alloc(s, lru, gfpflags, _RET_IP_, s->object_size); > > > trace_kmem_cache_alloc(_RET_IP_, ret, s, gfpflags, NUMA_NO_NODE); > > > > > > return ret; > > > > Yes, thank you, that patch atop v6.1-rc3 lets me boot, and shows the warning and stack dump. > > Will you submit that, or how do we want to proceed? > > Thanks for confirming. I wanted to wait for Vlastimil, Hyeonggon and > other developer's opinion. And yes, I can also post a more formal one. > > > transfer started ......................................... transfer ok, time=2.11s > > setting up elf image... OK > > jumping to kernel code > > zimage at: 80B842A0 810B4BC0 > > > > Uncompressing Linux at load address 80001000 > > > > Copy device tree to address 80B80EE0 > > > > Now, booting the kernel... > > > > [ 0.000000] Linux version 6.1.0-rc3+ (john@john) (mipsel-buildroot-linux-gnu-gcc.br_real (Buildroot 2021.11-4428-g6b6741b) 12.2.0, GNU ld (GNU Binutils) 2.39) #73 SMP Wed Nov 2 05:10:01 AEST 2022 > > [ 0.000000] ------------[ cut here ]------------ > > [ 0.000000] WARNING: CPU: 0 PID: 0 at mm/slub.c:3416 kmem_cache_alloc+0x5a4/0x5e8 > > [ 0.000000] Modules linked in: > > [ 0.000000] CPU: 0 PID: 0 Comm: swapper Not tainted 6.1.0-rc3+ #73 > > [ 0.000000] Stack : 810fff78 80084d98 00000000 00000004 00000000 00000000 80889d04 80c90000 > > [ 0.000000] 80920000 807bd328 8089d368 80923bd3 00000000 00000001 80889cb0 00000000 > > [ 0.000000] 00000000 00000000 807bd328 8084bcb1 00000002 00000002 00000001 6d6f4320 > > [ 0.000000] 00000000 80c97d3d 80c97d68 fffffffc 807bd328 00000000 00000000 00000000 > > [ 0.000000] 00000000 a0000000 80910000 8110a0b4 00000000 00000020 80010000 80010000 > > [ 0.000000] ... > > [ 0.000000] Call Trace: > > [ 0.000000] [<80008260>] show_stack+0x28/0xf0 > > [ 0.000000] [<8070c958>] dump_stack_lvl+0x60/0x80 > > [ 0.000000] [<8002e184>] __warn+0xc4/0xf8 > > [ 0.000000] [<8002e210>] warn_slowpath_fmt+0x58/0xa4 > > [ 0.000000] [<801c0fac>] kmem_cache_alloc+0x5a4/0x5e8 > > [ 0.000000] [<8092856c>] prom_soc_init+0x1fc/0x2b4 > > [ 0.000000] [<80928060>] prom_init+0x44/0xf0 > > [ 0.000000] [<80929214>] setup_arch+0x4c/0x6a8 > > [ 0.000000] [<809257e0>] start_kernel+0x88/0x7c0 > > [ 0.000000] > > [ 0.000000] ---[ end trace 0000000000000000 ]--- > > [ 0.000000] SoC Type: MediaTek MT7621 ver:1 eco:3 > > [ 0.000000] printk: bootconsole [early0] enabled > > > > Thank you for working through this with me. > > I will try to address the root cause in mt7621.c. > > It looks like other arch/** soc_device_register users use postcore_initcall, device_initcall, > > or the ARM DT_MACHINE_START .init_machine. A quick hack to use postcore_initcall in mt7621 > > avoided this zero ptr kmem_cache passed to kmem_cache_alloc_lru. > > If IIUC, the prom_soc_init() is only called once in kernel, can the > 'soc_dev_attr' just be defined as a global data structure instead > of calling kzalloc(), as its size is small only containing 7 pointers. But soc_device_registers() too uses kmalloc. I think calling it after slab initialization will be best solution - if that is correct. > > Thanks, > Feng > -- Thanks, Hyeonggon