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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 751C3EE57DF for ; Mon, 11 Sep 2023 16:47:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DCB8B6B02B0; Mon, 11 Sep 2023 12:47:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D7BC56B02B3; Mon, 11 Sep 2023 12:47:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C43646B02B5; Mon, 11 Sep 2023 12:47:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id B54A06B02B0 for ; Mon, 11 Sep 2023 12:47:55 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 762DB8052E for ; Mon, 11 Sep 2023 16:47:55 +0000 (UTC) X-FDA: 81224898510.14.E237D2B Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf13.hostedemail.com (Postfix) with ESMTP id A215E20023 for ; Mon, 11 Sep 2023 16:47:53 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=jKhIPDiW; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf13.hostedemail.com: domain of aquini@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=aquini@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694450873; a=rsa-sha256; cv=none; b=x4x7NClK1w62YZ66dmvaVGDYP5QQvmaDhGPPgyqm1Yb+L1S/P8pe6ec3t6wpioHVv3XRL5 bUqFt3ddP/5rkVnuSD+iJ5oHKZRhCiLo37lb2ds+weeNkGz+/zZE+GiANGJEpdEg/rNcIi A2+RGK9tfY3sgsBLqmrEHn18nkPhGss= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=jKhIPDiW; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf13.hostedemail.com: domain of aquini@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=aquini@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694450873; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=SlE5hxD39QAwP7iSa7A3UcuUWfmPcVu6XAYVvHUfNKU=; b=2TmVrEh8SSoSqqst93G17oI90HmIJkgxdKR1Stu4oxvOfykQiNJNaIr/umKYFdH53OswOj Q0jzxyUuWKVts47tnHhKWnRoQEzKGxpMpIRl8XGgvhsVnUHFTjMVkmPloaz6RNbrHQ9ZB0 ficdHpuXtHIY5AY0BHHbbFQLmx7uBu4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694450872; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=SlE5hxD39QAwP7iSa7A3UcuUWfmPcVu6XAYVvHUfNKU=; b=jKhIPDiWjwUwEjhjcu9Bj962hnkr/7ty97mw3HnRkazVy4tM5DtOYbSPByADa3p7pNohDa kD/HL59Q0VCgvoBku/B/qwM1UFd+cCrBHG53lsqAk+lzBs8+HZpZvqwG2iN1adrd/+3SgH DKOTG0NsxeVEVgyQcGcUzawUNJsYpFQ= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-642-dKd75GJuOlmpkmOBMSDFWw-1; Mon, 11 Sep 2023 12:47:51 -0400 X-MC-Unique: dKd75GJuOlmpkmOBMSDFWw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9D42D3C19371; Mon, 11 Sep 2023 16:47:50 +0000 (UTC) Received: from optiplex-fbsd (unknown [10.22.48.12]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8A13D2156702; Mon, 11 Sep 2023 16:47:49 +0000 (UTC) Date: Mon, 11 Sep 2023 12:47:47 -0400 From: Rafael Aquini To: Vlastimil Babka Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton , Waiman Long , Rafael Aquini , stable@vger.kernel.org Subject: Re: [PATCH] mm/slab_common: fix slab_caches list corruption after kmem_cache_destroy() Message-ID: References: <20230908230649.802560-1-aquini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: A215E20023 X-Stat-Signature: jnfj4z96416rhktn6ccx15anj9r59o56 X-HE-Tag: 1694450873-106253 X-HE-Meta: U2FsdGVkX1+Q4w1/mbX8vg/x/OcbnC1aXS6gwfhlFs1z7Pu3BnkzNZiD2/1pIvnKYox9yKgzU245HyWuIdL/HNLgx+hQKeucQff3CritzZY1n3Gje/gBKaeHvguj2UgQbay9isHRG6nwuyak1EQ+Oq6+j5b+CNHVxOq2X/XBxV2Z15foBFE4dCYIKB0IpqbAaN7B/PS+1rasRpIdzagADdg5gbUMXI/y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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 Mon, Sep 11, 2023 at 05:06:15PM +0200, Vlastimil Babka wrote: > On 9/9/23 01:06, Rafael Aquini wrote: > > After the commit in Fixes:, if a module that created a slab cache does not > > release all of its allocated objects before destroying the cache (at rmmod > > time), we might end up releasing the kmem_cache object without removing it > > from the slab_caches list thus corrupting the list as kmem_cache_destroy() > > ignores the return value from shutdown_cache(), which in turn never removes > > the kmem_cache object from slabs_list in case __kmem_cache_shutdown() fails > > to release all of the cache's slabs. > > > > This is easily observable on a kernel built with CONFIG_DEBUG_LIST=y > > as after that ill release the system will immediately trip on list_add, > > or list_del, assertions similar to the one shown below as soon as another > > kmem_cache gets created, or destroyed: > > > > [ 1041.213632] list_del corruption. next->prev should be ffff89f596fb5768, but was 52f1e5016aeee75d. (next=ffff89f595a1b268) > > [ 1041.219165] ------------[ cut here ]------------ > > [ 1041.221517] kernel BUG at lib/list_debug.c:62! > > [ 1041.223452] invalid opcode: 0000 [#1] PREEMPT SMP PTI > > [ 1041.225408] CPU: 2 PID: 1852 Comm: rmmod Kdump: loaded Tainted: G B W OE 6.5.0 #15 > > [ 1041.228244] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS edk2-20230524-3.fc37 05/24/2023 > > [ 1041.231212] RIP: 0010:__list_del_entry_valid+0xae/0xb0 > > > > Another quick way to trigger this issue, in a kernel with CONFIG_SLUB=y, > > is to set slub_debug to poison the released objects and then just run > > cat /proc/slabinfo after removing the module that leaks slab objects, > > in which case the kernel will panic: > > > > [ 50.954843] general protection fault, probably for non-canonical address 0xa56b6b6b6b6b6b8b: 0000 [#1] PREEMPT SMP PTI > > [ 50.961545] CPU: 2 PID: 1495 Comm: cat Kdump: loaded Tainted: G B W OE 6.5.0 #15 > > [ 50.966808] Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS edk2-20230524-3.fc37 05/24/2023 > > [ 50.972663] RIP: 0010:get_slabinfo+0x42/0xf0 > > > > This patch fixes this issue by properly checking shutdown_cache()'s > > return value before taking the kmem_cache_release() branch. > > > > Fixes: 0495e337b703 ("mm/slab_common: Deleting kobject in kmem_cache_destroy() without holding slab_mutex/cpu_hotplug_lock") > > Signed-off-by: Rafael Aquini > > Cc: stable@vger.kernel.org > > Thanks, added to slab.git. Tweaked the code a bit: > > https://git.kernel.org/pub/scm/linux/kernel/git/vbabka/slab.git/commit/?h=slab/for-6.6/hotfixes&id=46a9ea6681907a3be6b6b0d43776dccc62cad6cf > Thank you, Vlastimil. > > --- > > mm/slab_common.c | 13 ++++++++----- > > 1 file changed, 8 insertions(+), 5 deletions(-) > > > > diff --git a/mm/slab_common.c b/mm/slab_common.c > > index cd71f9581e67..31e581dc6e85 100644 > > --- a/mm/slab_common.c > > +++ b/mm/slab_common.c > > @@ -479,7 +479,7 @@ void slab_kmem_cache_release(struct kmem_cache *s) > > > > void kmem_cache_destroy(struct kmem_cache *s) > > { > > - int refcnt; > > + int err; > > bool rcu_set; > > > > if (unlikely(!s) || !kasan_check_byte(s)) > > @@ -490,17 +490,20 @@ void kmem_cache_destroy(struct kmem_cache *s) > > > > rcu_set = s->flags & SLAB_TYPESAFE_BY_RCU; > > > > - refcnt = --s->refcount; > > - if (refcnt) > > + s->refcount--; > > + if (s->refcount) { > > + err = -EBUSY; > > goto out_unlock; > > + } > > > > - WARN(shutdown_cache(s), > > + err = shutdown_cache(s); > > + WARN(err, > > "%s %s: Slab cache still has objects when called from %pS", > > __func__, s->name, (void *)_RET_IP_); > > out_unlock: > > mutex_unlock(&slab_mutex); > > cpus_read_unlock(); > > - if (!refcnt && !rcu_set) > > + if (!err && !rcu_set) > > kmem_cache_release(s); > > } > > EXPORT_SYMBOL(kmem_cache_destroy); > >