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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4805BEEB574 for ; Fri, 8 Sep 2023 23:22:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345209AbjIHXWr (ORCPT ); Fri, 8 Sep 2023 19:22:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345237AbjIHXWp (ORCPT ); Fri, 8 Sep 2023 19:22:45 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 321FC1FFF for ; Fri, 8 Sep 2023 16:21:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694215313; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=F+MW0YycyfuvGIR5zmiQX89HyMyFCOfpYnvtqX4xsko=; b=D0Tvyr+2P1gvZgVcvtPaANjSaLrxLjAvR/XLI6GNACqveO5hFQ9lVgBzs9s5GkFcCS8Ofj 2uOmiJH/pfAnjQc7ZOb7sohQF4ucyscK9zQ7MVGNqLl0HEyxYn/anSrYA8gfcpC6qh4MJd V5O749M3QOC2lqKhL+Tb3Id/JKTXUqs= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-250-4Kp_58QdPEuXVXXpHHp0Yg-1; Fri, 08 Sep 2023 19:21:51 -0400 X-MC-Unique: 4Kp_58QdPEuXVXXpHHp0Yg-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (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 F127881653D; Fri, 8 Sep 2023 23:21:50 +0000 (UTC) Received: from [10.22.33.35] (unknown [10.22.33.35]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9B078493112; Fri, 8 Sep 2023 23:21:50 +0000 (UTC) Message-ID: <09b69257-afb0-af0a-a3c4-f227b0cf4292@redhat.com> Date: Fri, 8 Sep 2023 19:21:50 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.14.0 Subject: Re: [PATCH] mm/slab_common: fix slab_caches list corruption after kmem_cache_destroy() Content-Language: en-US To: Rafael Aquini , linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Vlastimil Babka , Rafael Aquini , stable@vger.kernel.org References: <20230908230649.802560-1-aquini@redhat.com> From: Waiman Long In-Reply-To: <20230908230649.802560-1-aquini@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 9/8/23 19: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 > --- > 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); Thanks for fixing this corner case. Reviewed-by: Waiman Long