Message ID | 20230803101754.1149-4-thunder.leizhen@huaweicloud.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp1065139vqx; Thu, 3 Aug 2023 04:04:08 -0700 (PDT) X-Google-Smtp-Source: APBJJlGz+z6WQ6U68ZrGTM740S4b4og04PPrH0L8eXFu0Jo8TcGFLJ8Wee8wusdL86XJtEVLeEcI X-Received: by 2002:a17:903:41d1:b0:1b8:936f:c346 with SMTP id u17-20020a17090341d100b001b8936fc346mr22704576ple.30.1691060647747; Thu, 03 Aug 2023 04:04:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691060647; cv=none; d=google.com; s=arc-20160816; b=0xcsazYhvyZSuf+Ce9aUx02MCXVqVvrXtByT7iBJX1eK0CrSj2ZKklZyAVBvM8feKt AF2+Xlk9vFydW82zOo4tcpV9rNPBvc0DWxOiqGMolui7K/v+UFYQoMptrT9IcGUkFmKo ekfQexHsqmtEOi9ThheoRCPoEClnoKaDq3CFfERXCE9yYbpjU52V57UuxYTOZoeZzVUv jzCdvCNdT8ZXZIOau298mVVhcjkwAc29W7Y1oX9Qdh+4TJxqCrBhGpqOq3kb6DS4Q6n5 2qYFNCFt3kfShyrC8AgSJEH6mNkMC5BUXl5Hp7QDY32PIoC1tXJ9x5d1cmt7FBTMHyro Sb+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=NMVaGcW27s8Y6PJmZS1W6GaXqTOirqWruPixr0mtmy0=; fh=ixpq6thPVOOZuciljZRZ5gp6/ROsGXjbN7hNRM43S+c=; b=FeR5CPY4ic7MvRav0IMrotGsyeGbS0bM5B5fbFI6uJRiQ6KdqQF5g6kDroEOQUTuHW f31ax1aO3UnVmEp949sN1FzEbq8g2Jm81RRJYeFzx4eqzcbt28wi+GTYmdE+K28Fr+kd +XTrWEN84vC8Z6R5cAWhLlCnhwx/cgLxVIltHRhrCNo3Q26qNYRRpVwr8L3EiN6VEsu7 yXOwiOK9xU04gzoWtq7zOn0c+LRzr3ss636fvByfLffNzfiDpONfT+6CzqHGd4AY0RLI 3MBsfAImaTu6WZk163TODq19B5yEmx16g288n8rtUH3dYsKzX3dpP/5jgHzdMtbwFu1V hLHw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u17-20020a170902e5d100b001bbb56b34e8si13057320plf.432.2023.08.03.04.03.44; Thu, 03 Aug 2023 04:04:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235418AbjHCKTo (ORCPT <rfc822;guoshuai5156@gmail.com> + 99 others); Thu, 3 Aug 2023 06:19:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235320AbjHCKTO (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 3 Aug 2023 06:19:14 -0400 Received: from dggsgout11.his.huawei.com (unknown [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7AB3A35BE; Thu, 3 Aug 2023 03:19:12 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.143]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4RGlF92nt6z4f3yDV; Thu, 3 Aug 2023 18:19:09 +0800 (CST) Received: from huaweicloud.com (unknown [10.174.178.55]) by APP4 (Coremail) with SMTP id gCh0CgD3mp4Uf8tk4+TaPQ--.35755S7; Thu, 03 Aug 2023 18:19:09 +0800 (CST) From: thunder.leizhen@huaweicloud.com To: Christoph Lameter <cl@linux.com>, Pekka Enberg <penberg@kernel.org>, David Rientjes <rientjes@google.com>, Joonsoo Kim <iamjoonsoo.kim@lge.com>, Andrew Morton <akpm@linux-foundation.org>, Vlastimil Babka <vbabka@suse.cz>, Roman Gushchin <roman.gushchin@linux.dev>, Hyeonggon Yoo <42.hyeyoo@gmail.com>, linux-mm@kvack.org, "Paul E . McKenney" <paulmck@kernel.org>, Frederic Weisbecker <frederic@kernel.org>, Neeraj Upadhyay <quic_neeraju@quicinc.com>, Joel Fernandes <joel@joelfernandes.org>, Josh Triplett <josh@joshtriplett.org>, Boqun Feng <boqun.feng@gmail.com>, Steven Rostedt <rostedt@goodmis.org>, Mathieu Desnoyers <mathieu.desnoyers@efficios.com>, Lai Jiangshan <jiangshanlai@gmail.com>, Zqiang <qiang.zhang1211@gmail.com>, rcu@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Zhen Lei <thunder.leizhen@huawei.com> Subject: [PATCH v5 3/3] mm: Dump the memory of slab object in kmem_dump_obj() Date: Thu, 3 Aug 2023 18:17:54 +0800 Message-Id: <20230803101754.1149-4-thunder.leizhen@huaweicloud.com> X-Mailer: git-send-email 2.37.3.windows.1 In-Reply-To: <20230803101754.1149-1-thunder.leizhen@huaweicloud.com> References: <20230803101754.1149-1-thunder.leizhen@huaweicloud.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: gCh0CgD3mp4Uf8tk4+TaPQ--.35755S7 X-Coremail-Antispam: 1UD129KBjvJXoW7Ary7WFWDKrWxKF1rKFW8tFb_yoW8Kr13pr 9xGr1UK3yxArnrCrs3J3WkXF45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bp/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,MAY_BE_FORGED, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773205609865766170 X-GMAIL-MSGID: 1773205609865766170 |
Series |
rcu: Dump memory object info if callback function is invalid
|
|
Commit Message
Leizhen (ThunderTown)
Aug. 3, 2023, 10:17 a.m. UTC
From: Zhen Lei <thunder.leizhen@huawei.com> The contents of the slab object may contain some magic words and other useful information that may be helpful in locating problems such as memory corruption and use-after-free. To avoid print flooding, dump up to "16 * sizeof(int) = 64" bytes centered on argument 'ojbect'. For example: slab kmalloc-64 start ffff4043802d8b40 pointer offset 24 size 64 [8b40]: 12345678 00000000 8092d000 ffff8000 [8b50]: 00101000 00000000 8199ee00 ffff4043 [8b60]: 00000000 00000000 00000000 00000100 [8b70]: 00000000 9abcdef0 a8744de4 ffffc7fe Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com> --- mm/slab_common.c | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-)
Comments
On 8/3/23 12:17, thunder.leizhen@huaweicloud.com wrote: > From: Zhen Lei <thunder.leizhen@huawei.com> > > The contents of the slab object may contain some magic words and other > useful information that may be helpful in locating problems such as > memory corruption and use-after-free. > > To avoid print flooding, dump up to "16 * sizeof(int) = 64" bytes > centered on argument 'ojbect'. > > For example: > slab kmalloc-64 start ffff4043802d8b40 pointer offset 24 size 64 > [8b40]: 12345678 00000000 8092d000 ffff8000 > [8b50]: 00101000 00000000 8199ee00 ffff4043 > [8b60]: 00000000 00000000 00000000 00000100 > [8b70]: 00000000 9abcdef0 a8744de4 ffffc7fe > > Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com> > --- > mm/slab_common.c | 30 +++++++++++++++++++++++++++--- > 1 file changed, 27 insertions(+), 3 deletions(-) > > diff --git a/mm/slab_common.c b/mm/slab_common.c > index ee6ed6dd7ba9fa5..0232de9a3b29cf5 100644 > --- a/mm/slab_common.c > +++ b/mm/slab_common.c > @@ -553,7 +553,7 @@ static void kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab * > bool kmem_dump_obj(void *object) > { > char *cp = IS_ENABLED(CONFIG_MMU) ? "" : "/vmalloc"; > - int i; > + int i, object_size = 0; > struct slab *slab; > unsigned long ptroffset; > struct kmem_obj_info kp = { }; > @@ -580,12 +580,36 @@ bool kmem_dump_obj(void *object) > ptroffset = ((char *)object - (char *)kp.kp_objp) - kp.kp_data_offset; > pr_cont(" pointer offset %lu", ptroffset); > } > - if (kp.kp_slab_cache && kp.kp_slab_cache->object_size) > - pr_cont(" size %u", kp.kp_slab_cache->object_size); > + if (kp.kp_slab_cache && kp.kp_slab_cache->object_size) { > + object_size = kp.kp_slab_cache->object_size; > + pr_cont(" size %u", object_size); > + } > if (kp.kp_ret) > pr_cont(" allocated at %pS\n", kp.kp_ret); > else > pr_cont("\n"); > + > + /* Dump a small piece of memory centered on 'object' */ > + if (kp.kp_objp && object_size) { > + int *p = object, n = 16; > + > + p += n / 2; > + if ((void *)p > kp.kp_objp + object_size) > + p = kp.kp_objp + object_size; > + > + p -= n; > + if ((void *)p < kp.kp_objp) > + p = kp.kp_objp; > + > + n = min_t(int, object_size / sizeof(int), n); > + for (i = 0; i < n; i++, p++) { > + if (i % 4 == 0) > + pr_info("[%04lx]:", 0xffff & (unsigned long)p); > + pr_cont(" %08x", *p); > + } > + pr_cont("\n"); There's a print_hex_dump() for this, see how it's used from e.g. __dump_page(). > + } > + > for (i = 0; i < ARRAY_SIZE(kp.kp_stack); i++) { > if (!kp.kp_stack[i]) > break;
On 2023/8/3 18:34, Vlastimil Babka wrote: > On 8/3/23 12:17, thunder.leizhen@huaweicloud.com wrote: >> From: Zhen Lei <thunder.leizhen@huawei.com> >> >> The contents of the slab object may contain some magic words and other >> useful information that may be helpful in locating problems such as >> memory corruption and use-after-free. >> >> To avoid print flooding, dump up to "16 * sizeof(int) = 64" bytes >> centered on argument 'ojbect'. >> >> For example: >> slab kmalloc-64 start ffff4043802d8b40 pointer offset 24 size 64 >> [8b40]: 12345678 00000000 8092d000 ffff8000 >> [8b50]: 00101000 00000000 8199ee00 ffff4043 >> [8b60]: 00000000 00000000 00000000 00000100 >> [8b70]: 00000000 9abcdef0 a8744de4 ffffc7fe >> >> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com> >> --- >> mm/slab_common.c | 30 +++++++++++++++++++++++++++--- >> 1 file changed, 27 insertions(+), 3 deletions(-) >> >> diff --git a/mm/slab_common.c b/mm/slab_common.c >> index ee6ed6dd7ba9fa5..0232de9a3b29cf5 100644 >> --- a/mm/slab_common.c >> +++ b/mm/slab_common.c >> @@ -553,7 +553,7 @@ static void kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab * >> bool kmem_dump_obj(void *object) >> { >> char *cp = IS_ENABLED(CONFIG_MMU) ? "" : "/vmalloc"; >> - int i; >> + int i, object_size = 0; >> struct slab *slab; >> unsigned long ptroffset; >> struct kmem_obj_info kp = { }; >> @@ -580,12 +580,36 @@ bool kmem_dump_obj(void *object) >> ptroffset = ((char *)object - (char *)kp.kp_objp) - kp.kp_data_offset; >> pr_cont(" pointer offset %lu", ptroffset); >> } >> - if (kp.kp_slab_cache && kp.kp_slab_cache->object_size) >> - pr_cont(" size %u", kp.kp_slab_cache->object_size); >> + if (kp.kp_slab_cache && kp.kp_slab_cache->object_size) { >> + object_size = kp.kp_slab_cache->object_size; >> + pr_cont(" size %u", object_size); >> + } >> if (kp.kp_ret) >> pr_cont(" allocated at %pS\n", kp.kp_ret); >> else >> pr_cont("\n"); >> + >> + /* Dump a small piece of memory centered on 'object' */ >> + if (kp.kp_objp && object_size) { >> + int *p = object, n = 16; >> + >> + p += n / 2; >> + if ((void *)p > kp.kp_objp + object_size) >> + p = kp.kp_objp + object_size; >> + >> + p -= n; >> + if ((void *)p < kp.kp_objp) >> + p = kp.kp_objp; >> + >> + n = min_t(int, object_size / sizeof(int), n); >> + for (i = 0; i < n; i++, p++) { >> + if (i % 4 == 0) >> + pr_info("[%04lx]:", 0xffff & (unsigned long)p); >> + pr_cont(" %08x", *p); >> + } >> + pr_cont("\n"); > > There's a print_hex_dump() for this, see how it's used from e.g. __dump_page(). Thank you very much. The code has suddenly been a lot simpler. However, print_hex_dump() can be further enhanced, I will add a patch, let's discuss it together. > > >> + } >> + >> for (i = 0; i < ARRAY_SIZE(kp.kp_stack); i++) { >> if (!kp.kp_stack[i]) >> break; > > . >
diff --git a/mm/slab_common.c b/mm/slab_common.c index ee6ed6dd7ba9fa5..0232de9a3b29cf5 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -553,7 +553,7 @@ static void kmem_obj_info(struct kmem_obj_info *kpp, void *object, struct slab * bool kmem_dump_obj(void *object) { char *cp = IS_ENABLED(CONFIG_MMU) ? "" : "/vmalloc"; - int i; + int i, object_size = 0; struct slab *slab; unsigned long ptroffset; struct kmem_obj_info kp = { }; @@ -580,12 +580,36 @@ bool kmem_dump_obj(void *object) ptroffset = ((char *)object - (char *)kp.kp_objp) - kp.kp_data_offset; pr_cont(" pointer offset %lu", ptroffset); } - if (kp.kp_slab_cache && kp.kp_slab_cache->object_size) - pr_cont(" size %u", kp.kp_slab_cache->object_size); + if (kp.kp_slab_cache && kp.kp_slab_cache->object_size) { + object_size = kp.kp_slab_cache->object_size; + pr_cont(" size %u", object_size); + } if (kp.kp_ret) pr_cont(" allocated at %pS\n", kp.kp_ret); else pr_cont("\n"); + + /* Dump a small piece of memory centered on 'object' */ + if (kp.kp_objp && object_size) { + int *p = object, n = 16; + + p += n / 2; + if ((void *)p > kp.kp_objp + object_size) + p = kp.kp_objp + object_size; + + p -= n; + if ((void *)p < kp.kp_objp) + p = kp.kp_objp; + + n = min_t(int, object_size / sizeof(int), n); + for (i = 0; i < n; i++, p++) { + if (i % 4 == 0) + pr_info("[%04lx]:", 0xffff & (unsigned long)p); + pr_cont(" %08x", *p); + } + pr_cont("\n"); + } + for (i = 0; i < ARRAY_SIZE(kp.kp_stack); i++) { if (!kp.kp_stack[i]) break;