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 99056EEB570 for ; Fri, 8 Sep 2023 23:22:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C17966B0100; Fri, 8 Sep 2023 19:21:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BC7936B0102; Fri, 8 Sep 2023 19:21:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A8FE06B0103; Fri, 8 Sep 2023 19:21:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 946A16B0100 for ; Fri, 8 Sep 2023 19:21:59 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 634CE1CA419 for ; Fri, 8 Sep 2023 23:21:59 +0000 (UTC) X-FDA: 81215005158.27.5DD3C44 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf27.hostedemail.com (Postfix) with ESMTP id 7FC3A40015 for ; Fri, 8 Sep 2023 23:21:57 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=PUdCqybR; spf=pass (imf27.hostedemail.com: domain of longman@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694215317; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=F+MW0YycyfuvGIR5zmiQX89HyMyFCOfpYnvtqX4xsko=; b=39xenkpAgtZj3Pa2T0m5ce7JTfbZUhxrSBWgUDg7U1MkqJOirHvxHeAnXrBPiFkfR2XuER PoNXHqjmdfCr92+65zezwwbNHfvWrHJakzJRJO70K8xhZICqmOAlrNZxGs5HDqP1jeeuBh mR52fAHiBT3VnvM5oGkXygAlPTpvkZU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694215317; a=rsa-sha256; cv=none; b=BBsrHwDGnQ6NrIVTNILKppZ39mYUtzgTsKGXZHaFTa9ZOGeLazVDas9rFTuuAcJ/D9HQwo TOv2b85ECHmFh8AiirZikLkO7A7jJt7fWaq8tKz0A7aJACeMYJxNAATlCq69ZvynZ9gphO 3n/QBff32AvCKe4KxJrqweZ9UGvQisI= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=PUdCqybR; spf=pass (imf27.hostedemail.com: domain of longman@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694215316; 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=PUdCqybR/Ph7sAbPQsutqzzuoWcvOEt8gmytb3nESUb8N9BNg39WOajsnnKaWTUkxqEW2a DM0B0Co1KmpOARbJngl8RRFHuB9N8UZZYHjQwwDg3AYJ9zGlMdUkq8b3US/nnNvjNxYewo A10b3feu2hJtlpncpD+1JSahkFo70T4= 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 X-Rspamd-Queue-Id: 7FC3A40015 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: wsnfmw6ymtfto3fxse4bbwpmq3e384c4 X-HE-Tag: 1694215317-690337 X-HE-Meta: U2FsdGVkX1+bhhE+ZbQWBJue73xZnIQHnUbIArDbgypc8e4DiBtl3Ap3QnMeRv5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kKdXxC4C4XLt6d25ZHhhbxCR45/Nu5tEoWLvyyMGKDItvTlRPbr7Gv1161zeCeQdDaUNPay/0QTNh4Jgp2P56Gr/NFLxx53JGaUT9CQDOCDXwAAeqgF5ZT5i/f9xFJOw 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: 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