Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp2236792pxu; Mon, 7 Dec 2020 00:46:18 -0800 (PST) X-Google-Smtp-Source: ABdhPJyysumLa5GEuXi/yXx9mlLbWebG3uDLSvAnWhLdTfAcxQJ2SJ2ffETvprwEW9v6fRCVtZJI X-Received: by 2002:a17:907:206a:: with SMTP id qp10mr18220129ejb.432.1607330778113; Mon, 07 Dec 2020 00:46:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607330778; cv=none; d=google.com; s=arc-20160816; b=stpdO3KZohBlh5U3C9aZMJCKIY/iOWLdtyr5BEn7yH82GxR71F0JOeKiO6Uf65/hJP T/jhs2U64/yyFWRhBgwPbWYVGhaVXC2pLxZF3BtIWWxbTqj5Sp8pioiCFZ7NjENT1yZS jreTP1rg5RMhlojnJcCjUNUexhRIjY+UVAK5DFQrauO8B6DOX9iPl02q6K0GQa8OWl/5 SuhTywvppYJ4MJuGOS3qz4BsUSqzSgqwmJ88MQeHDtIDN+6v87oi71m1lsa1dPDkom0i QF8VIEK8VqA78n/+/cL8sikybchDEiKBvETXEsixfzw/umZ2K7KhYqQu33z0HIF9hcIk pGuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=rLjX9Hw9aQyIgMfFb2JmIrqFJsgjExanqZrXuZLnEKY=; b=A9gYtryFw9EGcaPf1NeZmEN3jIKIY8TMdP83DgAEC3O7eHLmjyHPuWc9nQ+Bch3ens dYCGWskP9KRjFN9kRhgXHgoniY5618/ZiAH6QSegexHiTXrsr4m4goQNlSaizdZtBfL9 M7M/QrqLqhaCnqH48kdMI8TSF2JSc8i0lEGr+uHAaOMjHI86WP+d3CBx9MQDXBP0cOdG 2ixdR2QFm4YVI+CVWP5kSL2zkxzz21L1X4xezZl0xYrUabxe/HmlhMq2j4lzrit3FOyH GZQLN2FrM/sWdF2P5crn/q7vk8TbOnQCokRXidCLeU53CU9uQqGHaN1Q0+DaHsxuPPMX FE8A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mediatek.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id hc33si5972024ejc.219.2020.12.07.00.45.54; Mon, 07 Dec 2020 00:46:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mediatek.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726173AbgLGIoB (ORCPT + 99 others); Mon, 7 Dec 2020 03:44:01 -0500 Received: from mailgw01.mediatek.com ([210.61.82.183]:56600 "EHLO mailgw01.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1725905AbgLGIoB (ORCPT ); Mon, 7 Dec 2020 03:44:01 -0500 X-UUID: ec42a9eb35114c3c9cac96d0c82bc287-20201207 X-UUID: ec42a9eb35114c3c9cac96d0c82bc287-20201207 Received: from mtkcas06.mediatek.inc [(172.21.101.30)] by mailgw01.mediatek.com (envelope-from ) (Cellopoint E-mail Firewall v4.1.14 Build 0819 with TLSv1.2 ECDHE-RSA-AES256-SHA384 256/256) with ESMTP id 480189470; Mon, 07 Dec 2020 16:43:17 +0800 Received: from MTKCAS06.mediatek.inc (172.21.101.30) by mtkmbs08n2.mediatek.inc (172.21.101.56) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Mon, 7 Dec 2020 16:43:14 +0800 Received: from mtksdccf07.mediatek.inc (172.21.84.99) by MTKCAS06.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Mon, 7 Dec 2020 16:43:13 +0800 From: Kuan-Ying Lee To: Zqiang , Andrey Ryabinin , Alexander Potapenko , Dmitry Vyukov , Andrew Morton , Matthias Brugger CC: , , , , , , Kuan-Ying Lee , Andrey Konovalov , Nicholas Tang , Miles Chen , Qian Cai , Stephen Rothwell Subject: [PATCH v4 1/1] kasan: fix object remain in offline per-cpu quarantine Date: Mon, 7 Dec 2020 16:42:58 +0800 Message-ID: <1607330578-417-2-git-send-email-Kuan-Ying.Lee@mediatek.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1607330578-417-1-git-send-email-Kuan-Ying.Lee@mediatek.com> References: <1607330578-417-1-git-send-email-Kuan-Ying.Lee@mediatek.com> MIME-Version: 1.0 Content-Type: text/plain X-TM-SNTS-SMTP: C16DCC12230BA6B4BDF99DDD0101C59782C26CF3814D12337F1858DEAB8FA5CD2000:8 X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Signed-off-by: Zqiang Suggested-by: Dmitry Vyukov Reported-by: Guangye Yang Reviewed-by: Dmitry Vyukov Cc: Andrey Ryabinin Cc: Alexander Potapenko Cc: Andrew Morton Cc: Matthias Brugger Cc: Andrey Konovalov Cc: Nicholas Tang Cc: Miles Chen Cc: Qian Cai Cc: Stephen Rothwell --- mm/kasan/quarantine.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/mm/kasan/quarantine.c b/mm/kasan/quarantine.c index a598c3514e1a..55783125a767 100644 --- a/mm/kasan/quarantine.c +++ b/mm/kasan/quarantine.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "../slab.h" #include "kasan.h" @@ -33,6 +34,7 @@ struct qlist_head { struct qlist_node *head; struct qlist_node *tail; size_t bytes; + bool offline; }; #define QLIST_INIT { NULL, NULL, 0 } @@ -191,6 +193,10 @@ bool quarantine_put(struct kmem_cache *cache, void *object) local_irq_save(flags); q = this_cpu_ptr(&cpu_quarantine); + if (q->offline) { + local_irq_restore(flags); + return false; + } qlist_put(q, &meta->quarantine_link, cache->size); if (unlikely(q->bytes > QUARANTINE_PERCPU_SIZE)) { qlist_move_all(q, &temp); @@ -333,3 +339,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