Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp1552596rwi; Mon, 10 Oct 2022 18:50:54 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7rn5NpHSSDftA3vJT+w8pPJEst7mz813a61Ur1XfUz7xzMKHQHdCgacHbv9umQg2RRg+Lv X-Received: by 2002:a17:907:75c1:b0:783:a093:2d34 with SMTP id jl1-20020a17090775c100b00783a0932d34mr16931054ejc.454.1665453053771; Mon, 10 Oct 2022 18:50:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665453053; cv=none; d=google.com; s=arc-20160816; b=v37YPANKUoVqZMhHWVx5+nN9HIFSgvOTubCgBSzxJKPHxJkFtlzZjT/xmzKt3q5rdX Q7+H9cr3LCIAAk8XxB20x5k8XmABd4Up0K3JDiprst1TGETHZxKWI3sme89pFNTfasl7 xnTGuLjWf5MtYWgpy5eldjvk0LNifby0PoIomkIy1ymENVW7JJlvERcPG/U7OWy6BKp4 fJChECmHkUEbIOvqLoxHljRf8rKltQaUEbTUyFkjA8UONKuhpjioVkp8Wf5jdX5/a3C0 XlrYAPX9Pk7Ok7F3wX0sDQn6frbBVp/r1UZciiFMjG5//WKa8WkUjuinYS+b6KU8da4f PJSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=20onCpfWpwkHM6YrH8+6NcOzMR0zA9uR0cJi6DXMSdc=; b=XrWVL0yh2LXOcdxLCpG0Cq29yMgWZeC4RDoYg+rGQ8LcD6HjPq50hahPp4WeV66Pwm y6o5cGj0KC7ZpmWb96VhnDzCyZ4amWNjpgyKNZegjFUZaXZQIAENJXDswZlpjqqhppGU 0nLlEalohXHeH5ZwY/KXXQTRInKWyLtfP6S3wFUKPbPb3+sSQPOmeyfhkVrW+No617/e WJYeMv64929Ir2m4pEJWdSaDzUyJ2zI99bt79p/kPn6x69gpvTVm7gh7jY4Hg47AgQAH 2x3n1ApRyiPc/s6tJSqCLWU3HBn0rOO0DuEwM5kRe4wHJYii2ZI5JDq2uKBxZfA3dwHU trSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=pQkoFcsZ; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id er9-20020a056402448900b00458e5293cd3si9575171edb.70.2022.10.10.18.50.27; Mon, 10 Oct 2022 18:50:53 -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; dkim=pass header.i=@gmail.com header.s=20210112 header.b=pQkoFcsZ; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229734AbiJKBdd (ORCPT + 99 others); Mon, 10 Oct 2022 21:33:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230282AbiJKBd0 (ORCPT ); Mon, 10 Oct 2022 21:33:26 -0400 Received: from mail-lj1-x234.google.com (mail-lj1-x234.google.com [IPv6:2a00:1450:4864:20::234]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 966AA7DF41; Mon, 10 Oct 2022 18:33:24 -0700 (PDT) Received: by mail-lj1-x234.google.com with SMTP id s10so15122546ljp.5; Mon, 10 Oct 2022 18:33:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=20onCpfWpwkHM6YrH8+6NcOzMR0zA9uR0cJi6DXMSdc=; b=pQkoFcsZDJH0Kh/HwwTAZAl8UOddRp0WjCd+gy1m0rYnrMV4AaFygOjukdhF/v7gLX f8ypt1hxNz3Ia26Nbe+JdfgE3KrxXHVtjyNdfgegBzDQyoryG7V1QIpV8GDx8phGPeMW Lctt1vtfSaImLEdQ5nwbnwj/ucUJyRWEbLAsS8XgYBysPI3zAnpXk6IyhDYWwk9TV6Ne HhbMNzhfny3ittE9/HV7d4edRAEuk2dZJ8BXqRfk7Z1dGGKg0lTAwhh/AicTxapzQdWB wZRDEl01AApLxLqqfp13QR7sp6bNf2FG0viWFYysDcZEBz/MA06Pf9TLsQg1tr+pSij3 8Phg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=20onCpfWpwkHM6YrH8+6NcOzMR0zA9uR0cJi6DXMSdc=; b=JWxs+vFTOASNiRHYFaC7hp2sugpmvYFrDvWo8iVBHNyiyxx+8MVKEYZ4yXGXXw+Ufg Guq1TfUEEGZQXX46VfUfNxp5qv9hbv8gwvfgMLOczklnNruyQb3s/+NfBR31z8TKVw7L O5/LlEF6IpWOfgMogZqmYxuTPkT0xbE+Bdo5y4cZUXIpkcxaSjB1tf/LxiIGtxrES/0Q DX5JtSbk2+teKEmrSJRJqYQUiIp+1Fhr/0Gco+tUPOpdABwIbou3deMdJz4egIsTzf+2 kH2opVkoI5APOhFgmZp+exK1t8vSDf2ikgoxVNJWQHDjO+ma23+cpkfbBg/kK/9CJtz1 B7QQ== X-Gm-Message-State: ACrzQf2xGLsv5KnGz/iZo0oja8RnoA1fggjJdVmc5VPO2A0cPNx9rRgL zkuFCL2BIMhWYE0ZLt71R43J4XQTvXVdpHC9KypK72xo X-Received: by 2002:a05:651c:222c:b0:26b:dec5:a4f0 with SMTP id y44-20020a05651c222c00b0026bdec5a4f0mr8460069ljq.359.1665452002769; Mon, 10 Oct 2022 18:33:22 -0700 (PDT) MIME-Version: 1.0 References: <1665450964-27487-1-git-send-email-zhaoyang.huang@unisoc.com> In-Reply-To: <1665450964-27487-1-git-send-email-zhaoyang.huang@unisoc.com> From: Zhaoyang Huang Date: Tue, 11 Oct 2022 09:32:53 +0800 Message-ID: Subject: Re: [Resend PATCH] mm: use stack_depot for recording kmemleak's backtrace To: "zhaoyang.huang" , Catalin Marinas Cc: Andrew Morton , Matthew Wilcox , Vlastimil Babka , linux-mm@kvack.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, ke.wang@unisoc.com, steve.kang@unisoc.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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: X-Mailing-List: linux-kernel@vger.kernel.org We can account and sort the output via backtrace under this change On Tue, Oct 11, 2022 at 9:18 AM zhaoyang.huang wrote: > > From: Zhaoyang Huang > > Using stack_depot to record kmemleak's backtrace which has been implemented > on slub for reducing redundant information. > > Signed-off-by: Zhaoyang Huang > --- > mm/kmemleak.c | 43 +++++++++++++++++++++++++++++++++---------- > 1 file changed, 33 insertions(+), 10 deletions(-) > > diff --git a/mm/kmemleak.c b/mm/kmemleak.c > index 1eddc01..c9cee3a 100644 > --- a/mm/kmemleak.c > +++ b/mm/kmemleak.c > @@ -79,6 +79,7 @@ > #include > #include > #include > +#include > #include > #include > #include > @@ -159,8 +160,7 @@ struct kmemleak_object { > u32 checksum; > /* memory ranges to be scanned inside an object (empty for all) */ > struct hlist_head area_list; > - unsigned long trace[MAX_TRACE]; > - unsigned int trace_len; > + depot_stack_handle_t trace_handle; > unsigned long jiffies; /* creation timestamp */ > pid_t pid; /* pid of the current task */ > char comm[TASK_COMM_LEN]; /* executable name */ > @@ -346,8 +346,11 @@ static void print_unreferenced(struct seq_file *seq, > struct kmemleak_object *object) > { > int i; > + unsigned long *entries; > + unsigned int nr_entries; > unsigned int msecs_age = jiffies_to_msecs(jiffies - object->jiffies); > > + nr_entries = stack_depot_fetch(object->trace_handle, &entries); > warn_or_seq_printf(seq, "unreferenced object 0x%08lx (size %zu):\n", > object->pointer, object->size); > warn_or_seq_printf(seq, " comm \"%s\", pid %d, jiffies %lu (age %d.%03ds)\n", > @@ -356,10 +359,10 @@ static void print_unreferenced(struct seq_file *seq, > hex_dump_object(seq, object); > warn_or_seq_printf(seq, " backtrace:\n"); > > - for (i = 0; i < object->trace_len; i++) { > - void *ptr = (void *)object->trace[i]; > - warn_or_seq_printf(seq, " [<%p>] %pS\n", ptr, ptr); > - } > + for (i = 0; i < nr_entries; i++) { > + void *ptr = (void *)entries[i]; > + warn_or_seq_printf(seq, " [<%p>] %pS\n", ptr, ptr); > + } > } > > /* > @@ -378,7 +381,8 @@ static void dump_object_info(struct kmemleak_object *object) > pr_notice(" flags = 0x%x\n", object->flags); > pr_notice(" checksum = %u\n", object->checksum); > pr_notice(" backtrace:\n"); > - stack_trace_print(object->trace, object->trace_len, 4); > + if(object->trace_handle) > + stack_depot_print(object->trace_handle); > } > > /* > @@ -591,6 +595,25 @@ static struct kmemleak_object *find_and_remove_object(unsigned long ptr, int ali > return object; > } > > +#ifdef CONFIG_STACKDEPOT > +static noinline depot_stack_handle_t set_track_prepare(void) > +{ > + depot_stack_handle_t trace_handle; > + unsigned long entries[MAX_TRACE]; > + unsigned int nr_entries; > + > + nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 3); > + trace_handle = stack_depot_save(entries, nr_entries, GFP_NOWAIT); > + > + return trace_handle; > +} > +#else > +static inline depot_stack_handle_t set_track_prepare(void) > +{ > + return 0; > +} > +#endif > + > /* > * Save stack trace to the given array of MAX_TRACE size. > */ > @@ -654,7 +677,7 @@ static struct kmemleak_object *__create_object(unsigned long ptr, size_t size, > } > > /* kernel backtrace */ > - object->trace_len = __save_stack_trace(object->trace); > + object->trace_handle = set_track_prepare(); > > raw_spin_lock_irqsave(&kmemleak_lock, flags); > > @@ -694,7 +717,6 @@ static struct kmemleak_object *__create_object(unsigned long ptr, size_t size, > rb_link_node(&object->rb_node, rb_parent, link); > rb_insert_color(&object->rb_node, is_phys ? &object_phys_tree_root : > &object_tree_root); > - > list_add_tail_rcu(&object->object_list, &object_list); > out: > raw_spin_unlock_irqrestore(&kmemleak_lock, flags); > @@ -1094,7 +1116,7 @@ void __ref kmemleak_update_trace(const void *ptr) > } > > raw_spin_lock_irqsave(&object->lock, flags); > - object->trace_len = __save_stack_trace(object->trace); > + object->trace_handle = set_track_prepare(); > raw_spin_unlock_irqrestore(&object->lock, flags); > > put_object(object); > @@ -2064,6 +2086,7 @@ void __init kmemleak_init(void) > if (kmemleak_error) > return; > > + stack_depot_init(); > jiffies_min_age = msecs_to_jiffies(MSECS_MIN_AGE); > jiffies_scan_wait = msecs_to_jiffies(SECS_SCAN_WAIT * 1000); > > -- > 1.9.1 >