From patchwork Fri Sep 8 23:06:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rafael Aquini X-Patchwork-Id: 13377892 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 AFA27EEB570 for ; Fri, 8 Sep 2023 23:07:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C0A836B00FC; Fri, 8 Sep 2023 19:07:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BB9BF6B00FE; Fri, 8 Sep 2023 19:07:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A817B6B0100; Fri, 8 Sep 2023 19:07:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 965956B00FC for ; Fri, 8 Sep 2023 19:07:00 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 51C771411A5 for ; Fri, 8 Sep 2023 23:07:00 +0000 (UTC) X-FDA: 81214967400.01.7400E62 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf03.hostedemail.com (Postfix) with ESMTP id 90A9B2002B for ; Fri, 8 Sep 2023 23:06:58 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ILXJ9PBG; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf03.hostedemail.com: domain of aquini@redhat.com designates 170.10.129.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=1694214418; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=WGIk1aHDuftDWuM242kRZNF3ZGX4IZN2ja3DENxRTe4=; b=fy6MAK8pZAJtTT5zmquIqhWScyRaVm8+6o1uX+bFPfhuL/PQaLfOkfmdIUZTGn13gBEe5s krPtCqV7TvSDnZAJLxlAh2GoQAFdLCA2HGH2DEgUH7rqn40W7mQJbEsbIibWjv5aeDa7ig lWXLDsLRmzc8tiHDgD5xoS60vWaqCCI= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ILXJ9PBG; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf03.hostedemail.com: domain of aquini@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=aquini@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694214418; a=rsa-sha256; cv=none; b=YcmxxIWeOIOyTaXoat7L2Uu29SL5SSRLFCEFzc132PaG+odfY9fW6srjJkCoOBl0/bNwqF CsAREGmEzhIXlqU5+yYICEjJHkniHaeWf1I93abZt2KRdaqO84FJP057kIoU2mku3pP1tP SDD7AWn1vDoYPfRH0l4jF+zG8iFvl8E= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694214417; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=WGIk1aHDuftDWuM242kRZNF3ZGX4IZN2ja3DENxRTe4=; b=ILXJ9PBG6lLy72yB0P/Extg7waiLmYYBPKFp6BEjMY8Nic4sZGdKqofSZ53vbn7t3laQh5 6Q0wccZowVPHPKpiBd4aUAjUI4UwZ7CG1AGl+fyHbrWu91oNOj5cP5pPywzbIU7FvJ1lNz VCBHII8SDrzpy+epeYIqbOjLYIey8YI= 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-265-naP80jUdPpC9vJUQx9zLPg-1; Fri, 08 Sep 2023 19:06:53 -0400 X-MC-Unique: naP80jUdPpC9vJUQx9zLPg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 25D9D3C01BB3; Fri, 8 Sep 2023 23:06:53 +0000 (UTC) Received: from optiplex-lnx.redhat.com (unknown [10.22.17.25]) by smtp.corp.redhat.com (Postfix) with ESMTP id BA867140E962; Fri, 8 Sep 2023 23:06:52 +0000 (UTC) From: Rafael Aquini To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Vlastimil Babka , Waiman Long , Rafael Aquini , stable@vger.kernel.org Subject: [PATCH] mm/slab_common: fix slab_caches list corruption after kmem_cache_destroy() Date: Fri, 8 Sep 2023 19:06:49 -0400 Message-ID: <20230908230649.802560-1-aquini@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Rspamd-Queue-Id: 90A9B2002B X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: fifndg6e7ogum79c7j7uj9jkqk64kixn X-HE-Tag: 1694214418-512318 X-HE-Meta: U2FsdGVkX18OiRno6aM5ll2Hk+ZlOVSH47EOKW05vujYD8owY/5DcOA8uUYRqredeULZF/5NY2+Y9YYcxQyFyq9qwL1rNzZ4X6CzvmorMSVkP7rCRToTE3w9E/3ot0GEEM2iDD4IPaPDCqmmp8DlIahW+D/HBuFkFTa+dpS7NYqWwvBfhocxD+RFwpv6+u9tU2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5Bsma8TVl6Q== X-Bogosity: 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: 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 Reviewed-by: Waiman Long --- 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);