From patchwork Fri Nov 4 09:33:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 15441 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp277533wru; Fri, 4 Nov 2022 02:35:40 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4qLnA7EpAeDLoSu4GmzFyLYnb6RuqeqZxMXqJIEX/bF29az6Bxi0XOhfKU73GrRgpTamfB X-Received: by 2002:a05:6a00:1394:b0:56d:2237:c1b0 with SMTP id t20-20020a056a00139400b0056d2237c1b0mr32411387pfg.5.1667554540419; Fri, 04 Nov 2022 02:35:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667554540; cv=none; d=google.com; s=arc-20160816; b=MOb/htu4goCaEPrdFKMJF5zUoQ7RhiAzrLc41b95ueJHnc+54Oly2TlBdJszyhD0cp +jE0RBAm4bEA2TaiRzSq3yXq3OkHWOBykcmgk16pK5i7psRu/8gQdVoatkqTeDMldGeV Hv09o5fKrgFzDCPPTZfslZihY3meRRh7MFMuafANg0DdVc50I1WhrSVMYG7o2inU9oiC q4Xp5X3tLKwXtEXCeDO0NqYgsMsjt9NXo3I2fzUtePvf2W7jrI2f/G3ih3kOPaQyWdpo f8hjxOKJg4SkHBmc9E4zGEcO6koEjUyJ6Qxyew0Of4xqahwjfdrie69E2Y2W2c/hCGw/ Qxyg== 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 :dkim-signature:dkim-signature; bh=F7Ie4B7a50PC9OXXhMZ93ubifOsPcPLwHTCVlnm01O8=; b=xknEGdxiKqHOf/SB2qp5VhS3GFrnrhuoDeYNmrPG8+F1J1mzly4E1F8uOTbHDpOVeH HNhU4hvN9I6COMgHkK4oGTaHMwlQfqr51wf+McWURC95118vWChxjRa4GvvVkirkDpUm 20uWY1kCwtcSWnKPi4JtnH+Yy90BtQ13mnS5YctyXNSIvJveeGBxEiWAsHLKovdEqZhK uzA0BKl1shHoDCHGY5psUumlRVhCwdydnUWuXvTTvhLZvTa9vTajAuAVrIhfqPJRJ580 UlI8ovj8OdcSarZ40cV5BTSuImVCO1IRTTYnuvERSg4mjcZ216XuEugbrD+NhMshIeUH aeKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=JS67XKlo; dkim=neutral (no key) header.i=@suse.de header.b=EScadhPK; 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=NONE dis=NONE) header.from=suse.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q6-20020a634306000000b00461f71a53e1si3968202pga.505.2022.11.04.02.35.27; Fri, 04 Nov 2022 02:35:40 -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=@suse.de header.s=susede2_rsa header.b=JS67XKlo; dkim=neutral (no key) header.i=@suse.de header.b=EScadhPK; 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=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231475AbiKDJeP (ORCPT + 99 others); Fri, 4 Nov 2022 05:34:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51596 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230021AbiKDJeM (ORCPT ); Fri, 4 Nov 2022 05:34:12 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 84A8FD46 for ; Fri, 4 Nov 2022 02:34:11 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 349D1218A8; Fri, 4 Nov 2022 09:34:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1667554450; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=F7Ie4B7a50PC9OXXhMZ93ubifOsPcPLwHTCVlnm01O8=; b=JS67XKloZpe13Rvp7lovCKGGpXmT7MHrFB6vYDTn1w75sS8ekkSdqlzFHrD+mpVYYfdPnZ dWrB7MrgPCj5vKCaw935VkBnZsYuLBe2W3sCpKiyt2ULbfV3Fiq0T/RKm9JdBq5OVJxfcV G3yyx/Pqi3vZXYtzXvzo7Gq1Vgr2lhk= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1667554450; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=F7Ie4B7a50PC9OXXhMZ93ubifOsPcPLwHTCVlnm01O8=; b=EScadhPK1XVUeNJyMquoIFdOv6eEkF+WA3kydq5Gc7zXzmxxTucSUuF1256Ilq9Bk0JDiW vRcC9o3hiLXGfjBw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 482E41346F; Fri, 4 Nov 2022 09:34:09 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 6KooDZHcZGP3BQAAMHmgww (envelope-from ); Fri, 04 Nov 2022 09:34:09 +0000 From: Oscar Salvador To: Andrew Morton Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Michal Hocko , Vlastimil Babka , Eric Dumazet , Waiman Long , Suren Baghdasaryan , Marco Elver , Andrey Konovalov , Alexander Potapenko , Oscar Salvador Subject: [PATCH v3 1/3] lib/stackdepot: Add a refcount field in stack_record Date: Fri, 4 Nov 2022 10:33:52 +0100 Message-Id: <20221104093354.6218-2-osalvador@suse.de> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221104093354.6218-1-osalvador@suse.de> References: <20221104093354.6218-1-osalvador@suse.de> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748557669537977663?= X-GMAIL-MSGID: =?utf-8?q?1748557669537977663?= We want to filter out page_owner output and print only those stacks that have been repeated beyond a certain threshold. This gives us the chance to get rid of a lot of noise. In order to do that, we need to keep track of how many repeated stacks (for allocation) do we have, so we add a new refcount_t field in the stack_record struct. Note that this might increase the size of the struct for some architectures. E.g: x86_64 is not affected due to alignment, but x86 32bits might. The alternative would be to have some kind of struct like this: struct track_stacks { struct stack_record *stack; struct track_stacks *next; refcount_t stack_count; But ithat would imply to perform more allocations and glue everything together, which would make the code more complex, so I think that going with a new field in the struct stack_record is good enough. Note that on __set_page_owner_handle(), page_owner->handle is set, and on __reset_page_owner(), page_owner->free_handle is set. We are interested in page_owner->handle, so when __set_page_owner() gets called, we derive the stack_record struct from page_owner->handle, and we increment its refcount_t field; and when __reset_page_owner() gets called, we derive its stack_record from page_owner->handle() and we decrement its refcount_t field. Signed-off-by: Oscar Salvador --- include/linux/stackdepot.h | 13 ++++++- lib/stackdepot.c | 79 +++++++++++++++++++++++++++++++------- mm/kasan/common.c | 3 +- mm/page_owner.c | 14 +++++-- 4 files changed, 89 insertions(+), 20 deletions(-) diff --git a/include/linux/stackdepot.h b/include/linux/stackdepot.h index bc2797955de9..4e3a88f135ee 100644 --- a/include/linux/stackdepot.h +++ b/include/linux/stackdepot.h @@ -15,9 +15,16 @@ typedef u32 depot_stack_handle_t; +enum stack_depot_action { + STACK_DEPOT_ACTION_NONE, + STACK_DEPOT_ACTION_COUNT, +}; + depot_stack_handle_t __stack_depot_save(unsigned long *entries, unsigned int nr_entries, - gfp_t gfp_flags, bool can_alloc); + gfp_t gfp_flags, bool can_alloc, + enum stack_depot_action action); +void stack_depot_dec_count(depot_stack_handle_t handle); /* * Every user of stack depot has to call stack_depot_init() during its own init @@ -55,6 +62,10 @@ static inline int stack_depot_early_init(void) { return 0; } depot_stack_handle_t stack_depot_save(unsigned long *entries, unsigned int nr_entries, gfp_t gfp_flags); +depot_stack_handle_t stack_depot_save_action(unsigned long *entries, + unsigned int nr_entries, + gfp_t gfp_flags, + enum stack_depot_action action); unsigned int stack_depot_fetch(depot_stack_handle_t handle, unsigned long **entries); diff --git a/lib/stackdepot.c b/lib/stackdepot.c index e73fda23388d..a806ef58a385 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -64,6 +64,7 @@ struct stack_record { u32 hash; /* Hash in the hastable */ u32 size; /* Number of frames in the stack */ union handle_parts handle; + refcount_t count; /* Number of the same repeated stacks */ unsigned long entries[]; /* Variable-sized array of entries. */ }; @@ -140,6 +141,7 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) stack->handle.slabindex = depot_index; stack->handle.offset = depot_offset >> STACK_ALLOC_ALIGN; stack->handle.valid = 1; + refcount_set(&stack->count, 1); memcpy(stack->entries, entries, flex_array_size(stack, entries, size)); depot_offset += required_size; @@ -341,6 +343,29 @@ void stack_depot_print(depot_stack_handle_t stack) } EXPORT_SYMBOL_GPL(stack_depot_print); +static struct stack_record *stack_depot_getstack(depot_stack_handle_t handle) +{ + union handle_parts parts = { .handle = handle }; + void *slab; + size_t offset = parts.offset << STACK_ALLOC_ALIGN; + struct stack_record *stack; + + if(!handle) + return NULL; + + if (parts.slabindex > depot_index) { + WARN(1, "slab index %d out of bounds (%d) for stack id %08x\n", + parts.slabindex, depot_index, handle); + return NULL; + } + slab = stack_slabs[parts.slabindex]; + if (!slab) + return NULL; + + stack = slab + offset; + return stack; +} + /** * stack_depot_fetch - Fetch stack entries from a depot * @@ -353,30 +378,42 @@ EXPORT_SYMBOL_GPL(stack_depot_print); unsigned int stack_depot_fetch(depot_stack_handle_t handle, unsigned long **entries) { - union handle_parts parts = { .handle = handle }; - void *slab; - size_t offset = parts.offset << STACK_ALLOC_ALIGN; struct stack_record *stack; *entries = NULL; if (!handle) return 0; - if (parts.slabindex > depot_index) { - WARN(1, "slab index %d out of bounds (%d) for stack id %08x\n", - parts.slabindex, depot_index, handle); - return 0; - } - slab = stack_slabs[parts.slabindex]; - if (!slab) + stack = stack_depot_getstack(handle); + if (!stack) return 0; - stack = slab + offset; *entries = stack->entries; return stack->size; } EXPORT_SYMBOL_GPL(stack_depot_fetch); +static void stack_depot_inc_count(struct stack_record *stack) +{ + refcount_inc(&stack->count); +} + +void stack_depot_dec_count(depot_stack_handle_t handle) +{ + struct stack_record *stack = NULL; + + stack = stack_depot_getstack(handle); + if (stack) { + /* + * page_owner creates some stacks via create_dummy_stack(). + * We are not interested in those, so make sure we only decrement + * "valid" stacks. + */ + if (refcount_read(&stack->count) > 1) + refcount_dec(&stack->count); + } +} + /** * __stack_depot_save - Save a stack trace from an array * @@ -402,7 +439,8 @@ EXPORT_SYMBOL_GPL(stack_depot_fetch); */ depot_stack_handle_t __stack_depot_save(unsigned long *entries, unsigned int nr_entries, - gfp_t alloc_flags, bool can_alloc) + gfp_t alloc_flags, bool can_alloc, + enum stack_depot_action action) { struct stack_record *found = NULL, **bucket; depot_stack_handle_t retval = 0; @@ -488,8 +526,11 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, /* Nobody used this memory, ok to free it. */ free_pages((unsigned long)prealloc, STACK_ALLOC_ORDER); } - if (found) + if (found) { retval = found->handle.handle; + if (action == STACK_DEPOT_ACTION_COUNT) + stack_depot_inc_count(found); + } fast_exit: return retval; } @@ -511,6 +552,16 @@ depot_stack_handle_t stack_depot_save(unsigned long *entries, unsigned int nr_entries, gfp_t alloc_flags) { - return __stack_depot_save(entries, nr_entries, alloc_flags, true); + return __stack_depot_save(entries, nr_entries, alloc_flags, true, + STACK_DEPOT_ACTION_NONE); } EXPORT_SYMBOL_GPL(stack_depot_save); + +depot_stack_handle_t stack_depot_save_action(unsigned long *entries, + unsigned int nr_entries, + gfp_t alloc_flags, + enum stack_depot_action action) +{ + return __stack_depot_save(entries, nr_entries, alloc_flags, true, action); +} +EXPORT_SYMBOL_GPL(stack_depot_save_action); diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 69f583855c8b..8077c6e70815 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -36,7 +36,8 @@ depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc) unsigned int nr_entries; nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0); - return __stack_depot_save(entries, nr_entries, flags, can_alloc); + return __stack_depot_save(entries, nr_entries, flags, can_alloc, + STACK_DEPOT_ACTION_NONE); } void kasan_set_track(struct kasan_track *track, gfp_t flags) diff --git a/mm/page_owner.c b/mm/page_owner.c index e4c6f3f1695b..8730f377fa91 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -106,7 +106,8 @@ static inline struct page_owner *get_page_owner(struct page_ext *page_ext) return (void *)page_ext + page_owner_ops.offset; } -static noinline depot_stack_handle_t save_stack(gfp_t flags) +static noinline depot_stack_handle_t save_stack(gfp_t flags, + enum stack_depot_action action) { unsigned long entries[PAGE_OWNER_STACK_DEPTH]; depot_stack_handle_t handle; @@ -125,7 +126,7 @@ static noinline depot_stack_handle_t save_stack(gfp_t flags) current->in_page_owner = 1; nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 2); - handle = stack_depot_save(entries, nr_entries, flags); + handle = stack_depot_save_action(entries, nr_entries, flags, action); if (!handle) handle = failure_handle; @@ -138,6 +139,7 @@ void __reset_page_owner(struct page *page, unsigned short order) int i; struct page_ext *page_ext; depot_stack_handle_t handle; + depot_stack_handle_t alloc_handle; struct page_owner *page_owner; u64 free_ts_nsec = local_clock(); @@ -145,7 +147,10 @@ void __reset_page_owner(struct page *page, unsigned short order) if (unlikely(!page_ext)) return; - handle = save_stack(GFP_NOWAIT | __GFP_NOWARN); + page_owner = get_page_owner(page_ext); + alloc_handle = page_owner->handle; + + handle = save_stack(GFP_NOWAIT | __GFP_NOWARN, STACK_DEPOT_ACTION_NONE); for (i = 0; i < (1 << order); i++) { __clear_bit(PAGE_EXT_OWNER_ALLOCATED, &page_ext->flags); page_owner = get_page_owner(page_ext); @@ -153,6 +158,7 @@ void __reset_page_owner(struct page *page, unsigned short order) page_owner->free_ts_nsec = free_ts_nsec; page_ext = page_ext_next(page_ext); } + stack_depot_dec_count(alloc_handle); } static inline void __set_page_owner_handle(struct page_ext *page_ext, @@ -189,7 +195,7 @@ noinline void __set_page_owner(struct page *page, unsigned short order, if (unlikely(!page_ext)) return; - handle = save_stack(gfp_mask); + handle = save_stack(gfp_mask, STACK_DEPOT_ACTION_COUNT); __set_page_owner_handle(page_ext, handle, order, gfp_mask); } From patchwork Fri Nov 4 09:33:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 15442 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp277613wru; Fri, 4 Nov 2022 02:35:54 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5o7Mtmip1G5eD3I6FxXcZdamP58FoouRDFAJhtYKRKHKtvggT8cPZTWW4f77SK+lcpk55j X-Received: by 2002:a17:90a:e0f:b0:213:c7cc:892a with SMTP id v15-20020a17090a0e0f00b00213c7cc892amr29934850pje.201.1667554554120; Fri, 04 Nov 2022 02:35:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667554554; cv=none; d=google.com; s=arc-20160816; b=E4+MJFQFH4ml9q7bSsF5mGlXAusVrfdhyCH02c3k1uycVft6CJh5RUZ7fT35s9nnVX R0ztjhU3XXuTGWqtXu7rQWPBqkxIyGlAOAumNLFOU8cV/+rtHueKftkkCh1yrc3+1QNC G0+DgFld9za5/dF0vbPIxdmJZLRYnnTW2Lo3xXBcoWR2hwG/RUdN6OCO52yCVwHMjfxf TRR+1udyFQm7nPQxIGwseWFdJ0Z2Jb9XwmhV91JaSvc42LlBhIdZEyXQObuTjQzLKQHa MRfzfLo2i/fUmI3DKhQdCvq1hmtzFWFdaMsKzlffb4RFbefxzZJhP12Lv2ZJM/U4I1ER CrIQ== 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 :dkim-signature:dkim-signature; bh=06k17Kzb8I50UujN65615D+OfTo3VSRSefXLgWtXH4s=; b=I6r4QprvrJmxWIbrDUZR6TRVWH+gryCEet6ep4wjZw7MU+zUFStX2PEmDFG/UtOg+a kcC172jcsKN6HYBqQ9Y4ItojuSaSgqM0wMoi41ifzgYW3OqplNJwWhFEkuwSf8Gyppyu c3407aBKl/5jwPofg+YDK1cXP17ZP+s/DbEbtxiq4hsTrG7u2rgp0y4iEJpG5jTPFEnh kNk6WaCgpgWqe0VI3gluzeZGU89yKUFPyhk0XyFsNfC+zDWS8hHLfTCd5gt9KNuyvVs0 hip2mX21IawvkM0WrfNKMnPQeynj3GqRSZ06lK7WhPi+wuMNi4SeuULWTj2j3K6OckhD 7aCQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=REMbrl1a; dkim=neutral (no key) header.i=@suse.de; 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=NONE dis=NONE) header.from=suse.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s3-20020a632143000000b0043cb43f584fsi4212843pgm.482.2022.11.04.02.35.41; Fri, 04 Nov 2022 02:35:54 -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=@suse.de header.s=susede2_rsa header.b=REMbrl1a; dkim=neutral (no key) header.i=@suse.de; 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=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231497AbiKDJeZ (ORCPT + 99 others); Fri, 4 Nov 2022 05:34:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51634 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229995AbiKDJeP (ORCPT ); Fri, 4 Nov 2022 05:34:15 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 30E622BDC for ; Fri, 4 Nov 2022 02:34:14 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id C2FE51F8B6; Fri, 4 Nov 2022 09:34:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1667554452; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=06k17Kzb8I50UujN65615D+OfTo3VSRSefXLgWtXH4s=; b=REMbrl1azfOylp4pAnJt1FL6ZrrasKRv5lozVitWbG2UHqREoEBIF9GKRdfjQsD57D2svL 91z2mW8bta4V4blD0Kipp2nS2ajmH+IaDYaGy5Ww/NqmRa8DKIQ3USjuAZ7krdSYoH42MB l5/FyUmXRDlT5BZlv2SCY2rZVipQhkg= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1667554452; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=06k17Kzb8I50UujN65615D+OfTo3VSRSefXLgWtXH4s=; b=vLH12hbo4td915aOcm9kAB3t3BajZdE4D3pmeF2CdDKx7OiOv3kzGVpUpIkwW1rjLXYFYt 19mmpa0C1bJlKpBw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 535FB1346F; Fri, 4 Nov 2022 09:34:10 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id 0LWDD5LcZGP3BQAAMHmgww (envelope-from ); Fri, 04 Nov 2022 09:34:10 +0000 From: Oscar Salvador To: Andrew Morton Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Michal Hocko , Vlastimil Babka , Eric Dumazet , Waiman Long , Suren Baghdasaryan , Marco Elver , Andrey Konovalov , Alexander Potapenko , Oscar Salvador Subject: [PATCH v3 2/3] mm, page_owner: Add page_owner_stacks file to print out only stacks and their counte Date: Fri, 4 Nov 2022 10:33:53 +0100 Message-Id: <20221104093354.6218-3-osalvador@suse.de> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221104093354.6218-1-osalvador@suse.de> References: <20221104093354.6218-1-osalvador@suse.de> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748557684244245111?= X-GMAIL-MSGID: =?utf-8?q?1748557684244245111?= We might be only interested in knowing about stacks <-> count relationship, so instead of having to fiddle with page_owner output and screen through pfns, let us add a new file called 'page_owner_stacks' that does just that. By cating such file, we will get all the stacktraces followed by its counter, so we can have a more global view. Signed-off-by: Oscar Salvador #include #include +#include +#include #define DEPOT_STACK_BITS (sizeof(depot_stack_handle_t) * 8) @@ -565,3 +567,74 @@ depot_stack_handle_t stack_depot_save_action(unsigned long *entries, return __stack_depot_save(entries, nr_entries, alloc_flags, true, action); } EXPORT_SYMBOL_GPL(stack_depot_save_action); + +#ifdef CONFIG_PAGE_OWNER +void *stack_start(struct seq_file *m, loff_t *ppos) +{ + unsigned long *table = m->private; + struct stack_record **stacks, *stack; + + /* First time */ + if (*ppos == 0) + *table = 0; + + if (*ppos == -1UL) + return NULL; + + stacks = &stack_table[*table]; + stack = (struct stack_record *)stacks; + + return stack; +} + +void *stack_next(struct seq_file *m, void *v, loff_t *ppos) +{ + unsigned long *table = m->private; + unsigned long nr_table = *table; + struct stack_record *next = NULL, *stack = v, **stacks; + unsigned long stack_table_entries = stack_hash_mask + 1; + + if (!stack) { +new_table: + /* New table */ + nr_table++; + if (nr_table >= stack_table_entries) + goto out; + stacks = &stack_table[nr_table]; + stack = (struct stack_record *)stacks; + next = stack; + } else { + next = stack->next; + } + + if (!next) + goto new_table; + +out: + *table = nr_table; + *ppos = (nr_table >= stack_table_entries) ? -1UL : *ppos + 1; + return next; +} + +int stack_print(struct seq_file *m, void *v) +{ + char *buf; + int ret = 0; + struct stack_record *stack =v; + + if (!stack->size || stack->size < 0 || + stack->size > PAGE_SIZE || stack->handle.valid != 1 || + refcount_read(&stack->count) < 1) + return 0; + + buf = kzalloc(PAGE_SIZE, GFP_KERNEL); + ret += stack_trace_snprint(buf, PAGE_SIZE, stack->entries, stack->size, 0); + scnprintf(buf + ret, PAGE_SIZE - ret, "stack count: %d\n\n", + refcount_read(&stack->count)); + seq_printf(m, buf); + seq_puts(m, "\n\n"); + kfree(buf); + + return 0; +} +#endif diff --git a/mm/page_owner.c b/mm/page_owner.c index 8730f377fa91..3808c0985060 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -668,6 +668,32 @@ static const struct file_operations proc_page_owner_operations = { .read = read_page_owner, }; +static void stack_stop(struct seq_file *m, void *v) +{ + return; +} + +static const struct seq_operations page_owner_stack_op = { + .start = stack_start, + .next = stack_next, + .stop = stack_stop, + .show = stack_print +}; + +static int page_owner_stack_open(struct inode *inode, struct file *file) +{ + return seq_open_private(file, &page_owner_stack_op, + sizeof(unsigned long)); +} + + +const struct file_operations page_owner_stack_operations = { + .open = page_owner_stack_open, + .read = seq_read, + .llseek = seq_lseek, + .release = seq_release, +}; + static int __init pageowner_init(void) { if (!static_branch_unlikely(&page_owner_inited)) { @@ -678,6 +704,9 @@ static int __init pageowner_init(void) debugfs_create_file("page_owner", 0400, NULL, NULL, &proc_page_owner_operations); + debugfs_create_file("page_owner_stacks", S_IRUSR, NULL, NULL, + &page_owner_stack_operations); + return 0; } late_initcall(pageowner_init) From patchwork Fri Nov 4 09:33:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 15443 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp278242wru; Fri, 4 Nov 2022 02:37:35 -0700 (PDT) X-Google-Smtp-Source: AMsMyM67Sn3lfO2KsBKmOGiWM1BiSXJTIeP/i1IcYfsOT7RllliwkiZwrd2vQDCjchX9CwDaEMQh X-Received: by 2002:a65:6d1a:0:b0:3fb:2109:7b87 with SMTP id bf26-20020a656d1a000000b003fb21097b87mr14661403pgb.127.1667554655302; Fri, 04 Nov 2022 02:37:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667554655; cv=none; d=google.com; s=arc-20160816; b=eU6m8RqaVwF+e35+zWil7MDVYXcrnfAfk0WexQgeQ8Vult1jblCw8koPGLYZPSc42s X2jFxpHdcqt0W8u9Xw5Mj4/BPbs/tcpxfbtwL9qZxfbYFJKrFL+bBLS23uhbjZM8jzwD 9iYfQ0rzA8zckvivWaDK1dSpRvPaBBluLzFTQygwhvrFiqNekUR1e9Bca6+SxHi8hNTs 1OIWKz2rR0lvRRqt6X3+21A5xY7t2aBlof/xmtvS3hNC7ZF0upi0KZyOrmZleXHH3Z9L CXIVJvjkDeP9jimTYGI8DPbmElxEhzB0GhGj7gGbjUgBsLMZRalO2kxTyXTiEHOi5NnS R9bA== 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 :dkim-signature:dkim-signature; bh=uL2I7PHjAFL8wwyrnZLGiRmIfuUYeK00tu0Z3hdbT3k=; b=H5+B4CeFHbjnDnPzoPhNxNJawD1ziy5wkDEDB2ma+QBoYsF1/ul5q3fy+P2zy4BA5f z0A5c6o6XLwJoyIiBS6Hu5JFoGSUpbxzRjZy1Hzx2+tGGX58TVVlBhzw5yhDfXRBMFun D9IT42fURDisClWiNRSDWWc/w29lUOJEvb5yxA7quUNUh65VK9CnYbHPs5KAj6uzPvnY 3C+fbHuJ5xPj15797XtpYSAMSadUzschQEJ8iAP6B5JIHwIB1V+AhxX1HNo5yprSApAv 1dZuHvkXUWOOT9uQyQP20hTgOO0bMbDmQYCl36Wcz0bNlQPis7JJcWaES0wn2VZlBqQ1 p9dQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=DUQ8gDjB; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=8gpZar+F; 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=NONE dis=NONE) header.from=suse.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x64-20020a638643000000b0046f94467a3bsi4281409pgd.76.2022.11.04.02.37.21; Fri, 04 Nov 2022 02:37:35 -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=@suse.de header.s=susede2_rsa header.b=DUQ8gDjB; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519 header.b=8gpZar+F; 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=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231596AbiKDJe2 (ORCPT + 99 others); Fri, 4 Nov 2022 05:34:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231511AbiKDJeX (ORCPT ); Fri, 4 Nov 2022 05:34:23 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 84E4A26565 for ; Fri, 4 Nov 2022 02:34:17 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id EF5191F8C8; Fri, 4 Nov 2022 09:34:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1667554455; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uL2I7PHjAFL8wwyrnZLGiRmIfuUYeK00tu0Z3hdbT3k=; b=DUQ8gDjB3AbF7aAFYp3cf73rrg7rxISxQVeKrFk9UMeDmSEnJr0x88quAbrXTReruO23Up nGm6Us5KaFkT3xbbaotZldg3Se0q9CkRtBsQuFiSzTroeGdXfyizEkFCh9Pj20vRO61zep ruzmGb6fhZm1+EgfQxmnZoe9I7tMMmI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1667554455; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uL2I7PHjAFL8wwyrnZLGiRmIfuUYeK00tu0Z3hdbT3k=; b=8gpZar+FBkFd8Tjn016GU3FcGMaGjV6S9jhIbQtaLCw+FxLyOi665XxaLoCzzVIcFUOHhV ej1iy+tCaLfJ3rBw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 1DB401346F; Fri, 4 Nov 2022 09:34:13 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id MJNsApXcZGP3BQAAMHmgww (envelope-from ); Fri, 04 Nov 2022 09:34:13 +0000 From: Oscar Salvador To: Andrew Morton Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Michal Hocko , Vlastimil Babka , Eric Dumazet , Waiman Long , Suren Baghdasaryan , Marco Elver , Andrey Konovalov , Alexander Potapenko , Oscar Salvador Subject: [PATCH v3 3/3] mm,page_owner: Filter out stacks by a threshold counter Date: Fri, 4 Nov 2022 10:33:54 +0100 Message-Id: <20221104093354.6218-4-osalvador@suse.de> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20221104093354.6218-1-osalvador@suse.de> References: <20221104093354.6218-1-osalvador@suse.de> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748557790030986040?= X-GMAIL-MSGID: =?utf-8?q?1748557790030986040?= We want to be able to filter out the output on a threshold basis, in this way we can get rid of a lot of noise and focus only on those stacks which have an allegedly high counter. We can control the threshold value by a new file called 'page_owner_threshold', which is 0 by default. Signed-off-by: Oscar Salvador --- include/linux/stackdepot.h | 3 +++ lib/stackdepot.c | 17 ++++++++++++++++- mm/page_owner.c | 5 +++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/include/linux/stackdepot.h b/include/linux/stackdepot.h index ca048a79bf7c..c998b8024534 100644 --- a/include/linux/stackdepot.h +++ b/include/linux/stackdepot.h @@ -70,6 +70,9 @@ depot_stack_handle_t stack_depot_save_action(unsigned long *entries, void *stack_start(struct seq_file *m, loff_t *ppos); void *stack_next(struct seq_file *m, void *v, loff_t *ppos); int stack_print(struct seq_file *m, void *v); + +int page_owner_threshold_get(void *data, u64 *val); +int page_owner_threshold_set(void *data, u64 val); #endif unsigned int stack_depot_fetch(depot_stack_handle_t handle, diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 97e5dce40f4b..b70d081d7c61 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -569,6 +569,9 @@ depot_stack_handle_t stack_depot_save_action(unsigned long *entries, EXPORT_SYMBOL_GPL(stack_depot_save_action); #ifdef CONFIG_PAGE_OWNER + +static unsigned long page_owner_stack_threshold = 0; + void *stack_start(struct seq_file *m, loff_t *ppos) { unsigned long *table = m->private; @@ -624,7 +627,7 @@ int stack_print(struct seq_file *m, void *v) if (!stack->size || stack->size < 0 || stack->size > PAGE_SIZE || stack->handle.valid != 1 || - refcount_read(&stack->count) < 1) + refcount_read(&stack->count) < page_owner_stack_threshold) return 0; buf = kzalloc(PAGE_SIZE, GFP_KERNEL); @@ -637,4 +640,16 @@ int stack_print(struct seq_file *m, void *v) return 0; } + +int page_owner_threshold_get(void *data, u64 *val) +{ + *val = page_owner_stack_threshold; + return 0; +} + +int page_owner_threshold_set(void *data, u64 val) +{ + page_owner_stack_threshold = val; + return 0; +} #endif diff --git a/mm/page_owner.c b/mm/page_owner.c index 3808c0985060..3e05ebd25ba0 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -694,6 +694,9 @@ const struct file_operations page_owner_stack_operations = { .release = seq_release, }; +DEFINE_SIMPLE_ATTRIBUTE(proc_page_owner_threshold, &page_owner_threshold_get, + &page_owner_threshold_set, "%lu"); + static int __init pageowner_init(void) { if (!static_branch_unlikely(&page_owner_inited)) { @@ -706,6 +709,8 @@ static int __init pageowner_init(void) debugfs_create_file("page_owner_stacks", S_IRUSR, NULL, NULL, &page_owner_stack_operations); + debugfs_create_file("page_owner_threshold", 0600, NULL, NULL, + &proc_page_owner_threshold); return 0; }