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=-23.2 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_IN_DEF_DKIM_WL autolearn=unavailable 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 910BBC63777 for ; Thu, 3 Dec 2020 12:47:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3B37720709 for ; Thu, 3 Dec 2020 12:47:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729311AbgLCMrw (ORCPT ); Thu, 3 Dec 2020 07:47:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52520 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728255AbgLCMrv (ORCPT ); Thu, 3 Dec 2020 07:47:51 -0500 Received: from mail-pf1-x443.google.com (mail-pf1-x443.google.com [IPv6:2607:f8b0:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78AE2C061A4F for ; Thu, 3 Dec 2020 04:47:11 -0800 (PST) Received: by mail-pf1-x443.google.com with SMTP id c79so1220257pfc.2 for ; Thu, 03 Dec 2020 04:47:11 -0800 (PST) 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=LyhE2Gh6cABbrVZ7ov6yfexMpJrP0k999yqFA1LYZCc=; b=RVIUWgPtVJpSq34PZD9bY+aGnf8j+weA4FOfHjRVQJmUYTLo4fnlvYPIRondXFPrhJ Mf66xvkoLkQ5CeJ3l/Ik/Uz0KGrK8y3LSJUEZu88TpMlMEtqJCoaaB4BCIzTLRQ7TJwJ T+4ccip/UhjP+OHTOsqyeK3m2NU7278vfreDpRCLPCqwSmKr/38/rVhvfR35Giwk0tgk NGsDWRhkxXCoERrwXSc2sWjTCF0M3Yvsdt2ahHgb7nDoXet1hJJrsW9VzCKpM7yp3EGa Mwia3bqqqE/6TUVDMcAXJr7lwxyu+BgV+q0AxlQGFwzHFysC5UfkrTALSWTXCSmr/j3G tWyg== 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=LyhE2Gh6cABbrVZ7ov6yfexMpJrP0k999yqFA1LYZCc=; b=Y6iCV9uiBTl1y9riLlX7PVsH65bl/yNnWHQOF7XSAt1Ajoh9odoICKRl2i1MartbVt LGn9QmtdYQrng3U4bd7x+EYCiTpFBABKdmQWEtQWj5sb8t/hxH3n/lkM24Czu1FBx0aa r5nObvwnOPkDjKQVxw+u4BNJZqbwtGxr2ENQDI5HrhVF289qY5+tzl/gEta5YlPbpR4K TJD0G55+e4Pwmx00pcsrx5B2EBn49pNSnnIfvuYyCHQuue+vNigyz9qnoj5Z6PJptdIG kTXbbCwG9il2CTQETD90xc6TuRCTM89iMiflwbyQOH77epOPlc6fLHQk2H9AFtrrf/UT zk3A== X-Gm-Message-State: AOAM530gMDFdm+VNua/0AdnF8wCTnYqVBNielSfWSBqn47fXA3vxLAxw 9ttqkERuEskO7Tlvm4efiqzq5/TbH+p67NwO6olvBg== X-Google-Smtp-Source: ABdhPJzUdDE/gpu4h72S7w9Bh53pDtC/8FlltR0gvMuL/8CEwzlFpfP6QFQ0Rzbzcxg1N8SSRasp8M0poZqL3UUc6so= X-Received: by 2002:a63:f20:: with SMTP id e32mr2860728pgl.130.1606999630819; Thu, 03 Dec 2020 04:47:10 -0800 (PST) MIME-Version: 1.0 References: <1606895585-17382-1-git-send-email-Kuan-Ying.Lee@mediatek.com> <1606895585-17382-2-git-send-email-Kuan-Ying.Lee@mediatek.com> In-Reply-To: <1606895585-17382-2-git-send-email-Kuan-Ying.Lee@mediatek.com> From: Andrey Konovalov Date: Thu, 3 Dec 2020 13:46:59 +0100 Message-ID: Subject: Re: [PATCH v3 1/1] kasan: fix object remain in offline per-cpu quarantine To: Kuan-Ying Lee Cc: Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Andrew Morton , Matthias Brugger , Nicholas Tang , Miles Chen , kasan-dev , Linux Memory Management List , LKML , Linux ARM , linux-mediatek@lists.infradead.org, wsd_upstream Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Dec 2, 2020 at 8:58 AM Kuan-Ying Lee wrote: > > We hit this issue in our internal test. > When enabling generic kasan, a kfree()'d object is put into per-cpu > quarantine first. If the cpu goes offline, object still remains in > the per-cpu quarantine. If we call kmem_cache_destroy() now, slub > will report "Objects remaining" error. > > [ 74.982625] ============================================================================= > [ 74.983380] BUG test_module_slab (Not tainted): Objects remaining in test_module_slab on __kmem_cache_shutdown() > [ 74.984145] ----------------------------------------------------------------------------- > [ 74.984145] > [ 74.984883] Disabling lock debugging due to kernel taint > [ 74.985561] INFO: Slab 0x(____ptrval____) objects=34 used=1 fp=0x(____ptrval____) flags=0x2ffff00000010200 > [ 74.986638] CPU: 3 PID: 176 Comm: cat Tainted: G B 5.10.0-rc1-00007-g4525c8781ec0-dirty #10 > [ 74.987262] Hardware name: linux,dummy-virt (DT) > [ 74.987606] Call trace: > [ 74.987924] dump_backtrace+0x0/0x2b0 > [ 74.988296] show_stack+0x18/0x68 > [ 74.988698] dump_stack+0xfc/0x168 > [ 74.989030] slab_err+0xac/0xd4 > [ 74.989346] __kmem_cache_shutdown+0x1e4/0x3c8 > [ 74.989779] kmem_cache_destroy+0x68/0x130 > [ 74.990176] test_version_show+0x84/0xf0 > [ 74.990679] module_attr_show+0x40/0x60 > [ 74.991218] sysfs_kf_seq_show+0x128/0x1c0 > [ 74.991656] kernfs_seq_show+0xa0/0xb8 > [ 74.992059] seq_read+0x1f0/0x7e8 > [ 74.992415] kernfs_fop_read+0x70/0x338 > [ 74.993051] vfs_read+0xe4/0x250 > [ 74.993498] ksys_read+0xc8/0x180 > [ 74.993825] __arm64_sys_read+0x44/0x58 > [ 74.994203] el0_svc_common.constprop.0+0xac/0x228 > [ 74.994708] do_el0_svc+0x38/0xa0 > [ 74.995088] el0_sync_handler+0x170/0x178 > [ 74.995497] el0_sync+0x174/0x180 > [ 74.996050] INFO: Object 0x(____ptrval____) @offset=15848 > [ 74.996752] INFO: Allocated in test_version_show+0x98/0xf0 age=8188 cpu=6 pid=172 > [ 75.000802] stack_trace_save+0x9c/0xd0 > [ 75.002420] set_track+0x64/0xf0 > [ 75.002770] alloc_debug_processing+0x104/0x1a0 > [ 75.003171] ___slab_alloc+0x628/0x648 > [ 75.004213] __slab_alloc.isra.0+0x2c/0x58 > [ 75.004757] kmem_cache_alloc+0x560/0x588 > [ 75.005376] test_version_show+0x98/0xf0 > [ 75.005756] module_attr_show+0x40/0x60 > [ 75.007035] sysfs_kf_seq_show+0x128/0x1c0 > [ 75.007433] kernfs_seq_show+0xa0/0xb8 > [ 75.007800] seq_read+0x1f0/0x7e8 > [ 75.008128] kernfs_fop_read+0x70/0x338 > [ 75.008507] vfs_read+0xe4/0x250 > [ 75.008990] ksys_read+0xc8/0x180 > [ 75.009462] __arm64_sys_read+0x44/0x58 > [ 75.010085] el0_svc_common.constprop.0+0xac/0x228 > [ 75.011006] kmem_cache_destroy test_module_slab: Slab cache still has objects > > Register a cpu hotplug function to remove all objects in the offline > per-cpu quarantine when cpu is going offline. Set a per-cpu variable > to indicate this cpu is offline. > > Signed-off-by: Kuan-Ying Lee > Suggested-by: Dmitry Vyukov > Reported-by: Guangye Yang > Cc: Andrey Ryabinin > Cc: Alexander Potapenko > Cc: Andrew Morton > Cc: Matthias Brugger > --- > mm/kasan/quarantine.c | 40 ++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 40 insertions(+) > > diff --git a/mm/kasan/quarantine.c b/mm/kasan/quarantine.c > index 4c5375810449..cac7c617df72 100644 > --- a/mm/kasan/quarantine.c > +++ b/mm/kasan/quarantine.c > @@ -29,6 +29,7 @@ > #include > #include > #include > +#include > > #include "../slab.h" > #include "kasan.h" > @@ -43,6 +44,7 @@ struct qlist_head { > struct qlist_node *head; > struct qlist_node *tail; > size_t bytes; > + bool offline; > }; > > #define QLIST_INIT { NULL, NULL, 0 } > @@ -188,6 +190,11 @@ void quarantine_put(struct kasan_free_meta *info, struct kmem_cache *cache) > local_irq_save(flags); > > q = this_cpu_ptr(&cpu_quarantine); > + if (q->offline) { > + qlink_free(&info->quarantine_link, cache); Hi Kuan-Ying, This needs to be rebased onto the mm tree: it has some KASAN patches that touch this code and rename the info variable to meta. Thanks! > + local_irq_restore(flags); > + return; > + } > qlist_put(q, &info->quarantine_link, cache->size); > if (unlikely(q->bytes > QUARANTINE_PERCPU_SIZE)) { > qlist_move_all(q, &temp); > @@ -328,3 +335,36 @@ void quarantine_remove_cache(struct kmem_cache *cache) > > synchronize_srcu(&remove_cache_srcu); > } > + > +static int kasan_cpu_online(unsigned int cpu) > +{ > + this_cpu_ptr(&cpu_quarantine)->offline = false; > + return 0; > +} > + > +static int kasan_cpu_offline(unsigned int cpu) > +{ > + struct qlist_head *q; > + > + q = this_cpu_ptr(&cpu_quarantine); > + /* Ensure the ordering between the writing to q->offline and > + * qlist_free_all. Otherwise, cpu_quarantine may be corrupted > + * by interrupt. > + */ > + WRITE_ONCE(q->offline, true); > + barrier(); > + qlist_free_all(q, NULL); > + return 0; > +} > + > +static int __init kasan_cpu_quarantine_init(void) > +{ > + int ret = 0; > + > + ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "mm/kasan:online", > + kasan_cpu_online, kasan_cpu_offline); > + if (ret < 0) > + pr_err("kasan cpu quarantine register failed [%d]\n", ret); > + return ret; > +} > +late_initcall(kasan_cpu_quarantine_init); > -- > 2.18.0 > > -- > 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/1606895585-17382-2-git-send-email-Kuan-Ying.Lee%40mediatek.com.