From patchwork Thu Feb 8 23:45:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 13550767 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 29F58C48260 for ; Thu, 8 Feb 2024 23:45:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 15ABE6B0083; Thu, 8 Feb 2024 18:45:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 10BEC6B0085; Thu, 8 Feb 2024 18:45:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E2AA26B0087; Thu, 8 Feb 2024 18:45:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id C9F4C6B0083 for ; Thu, 8 Feb 2024 18:45:19 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 6F8821A06DD for ; Thu, 8 Feb 2024 23:45:19 +0000 (UTC) X-FDA: 81770270358.20.8CD4259 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf19.hostedemail.com (Postfix) with ESMTP id 44AF81A0003 for ; Thu, 8 Feb 2024 23:45:17 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=hY9JCYvA; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=p5HZ5uBZ; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=hY9JCYvA; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=p5HZ5uBZ; spf=pass (imf19.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.130 as permitted sender) smtp.mailfrom=osalvador@suse.de; dmarc=pass (policy=none) header.from=suse.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707435917; a=rsa-sha256; cv=none; b=pW7JF42jORvhkc5v8BjVgulLjSSMOvJoEmwYvvE7axH0V3Ta9UcbYK6ZBPliQ19kS9bBNn zR7hiNR5/dsdjOgLDXdm+rUkBTzK71Iccf3mM7BQ/dmaoCN7ZClShkva9YcMm+W746QC1i WME58Mx2v7sH00bNPJA9iuNcAEBG8U8= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=hY9JCYvA; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=p5HZ5uBZ; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=hY9JCYvA; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=p5HZ5uBZ; spf=pass (imf19.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.130 as permitted sender) smtp.mailfrom=osalvador@suse.de; dmarc=pass (policy=none) header.from=suse.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707435917; 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:in-reply-to:references:references:dkim-signature; bh=1RIuerYMWRH7Xo5xR9Dr7KugCllJ88k2PizonkIPXIU=; b=BxR/hgf2IxLJL5dY5Hcjj77MGvqcqx1cuKd+mwvuQv1gHKfhvru9wrZtOG/HF80BCLE3gO XgypXpoyDKsBAnTPmFJCZD5MORwVq9mlRPHl15TMzXSZoTl2T5L/XTvWsH8CsB92RmsbvI vURHWrIYnoOlzNq7MIMtxhD+nTUO5N8= Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id A783422317; Thu, 8 Feb 2024 23:45:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1707435915; 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=1RIuerYMWRH7Xo5xR9Dr7KugCllJ88k2PizonkIPXIU=; b=hY9JCYvAncAGepUu81hefJSrPIqWxz/6UK81x78gggd7DefAzQkSaFtX/UeMHzsUrsprWg UnkgoUNyQrDRszamEUWOuLhaRRJBjVfAf5YiWMHqvwRf62A7ru9jv/bv/vvDNy+oUMNH+u QAu0UK+6FYmPjn6QLgfthVizcCiWcww= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1707435915; 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=1RIuerYMWRH7Xo5xR9Dr7KugCllJ88k2PizonkIPXIU=; b=p5HZ5uBZO2fi/Vl014Q4Qrb0lax8dsjC8lQ2L5wn/MAUf37MHid3+N7LFaSZ6Sd95BS7TW b1gGtfcxLmlURUDQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1707435915; 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=1RIuerYMWRH7Xo5xR9Dr7KugCllJ88k2PizonkIPXIU=; b=hY9JCYvAncAGepUu81hefJSrPIqWxz/6UK81x78gggd7DefAzQkSaFtX/UeMHzsUrsprWg UnkgoUNyQrDRszamEUWOuLhaRRJBjVfAf5YiWMHqvwRf62A7ru9jv/bv/vvDNy+oUMNH+u QAu0UK+6FYmPjn6QLgfthVizcCiWcww= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1707435915; 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=1RIuerYMWRH7Xo5xR9Dr7KugCllJ88k2PizonkIPXIU=; b=p5HZ5uBZO2fi/Vl014Q4Qrb0lax8dsjC8lQ2L5wn/MAUf37MHid3+N7LFaSZ6Sd95BS7TW b1gGtfcxLmlURUDQ== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 1BB6413984; Thu, 8 Feb 2024 23:45:15 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id WI3VA4tnxWWUfAAAD6G6ig (envelope-from ); Thu, 08 Feb 2024 23:45:15 +0000 From: Oscar Salvador To: Andrew Morton Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Michal Hocko , Vlastimil Babka , Marco Elver , Andrey Konovalov , Alexander Potapenko , Oscar Salvador Subject: [PATCH v7 3/4] mm,page_owner: Display all stacks and their count Date: Fri, 9 Feb 2024 00:45:38 +0100 Message-ID: <20240208234539.19113-4-osalvador@suse.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240208234539.19113-1-osalvador@suse.de> References: <20240208234539.19113-1-osalvador@suse.de> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 44AF81A0003 X-Stat-Signature: unc78gkm686ei88uffskmpaa33hrxgt5 X-Rspam-User: X-HE-Tag: 1707435917-725421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24ioInCdJ0feh+SrpJjr2qY+JhwD3ijo3ch1HOx0FUk8FEQAu5burBSBDdIUmMtbJrx+/jxch97LYNcNwZquAc+OquZQCFT3U9fYy98FKSdHtdhxRSJ8hAPMfPKohGZtM0ha99rJ6wfrKssga1Ti6MWmBCmL1zVsrLifgzitMqnyGptnnGo1GdSF5Zsigh+D/qHkRppgrtKZ63Xlc40SD9hlPA98M 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: List-Subscribe: List-Unsubscribe: This patch adds a new file called 'page_owner_stacks', which will show all stacks that were added by page_owner followed by their counting, giving us a clear overview of stack <-> count relationship. E.g: prep_new_page+0xa9/0x120 get_page_from_freelist+0x801/0x2210 __alloc_pages+0x18b/0x350 alloc_pages_mpol+0x91/0x1f0 folio_alloc+0x14/0x50 filemap_alloc_folio+0xb2/0x100 __filemap_get_folio+0x14a/0x490 ext4_write_begin+0xbd/0x4b0 [ext4] generic_perform_write+0xc1/0x1e0 ext4_buffered_write_iter+0x68/0xe0 [ext4] ext4_file_write_iter+0x70/0x740 [ext4] vfs_write+0x33d/0x420 ksys_write+0xa5/0xe0 do_syscall_64+0x80/0x160 entry_SYSCALL_64_after_hwframe+0x6e/0x76 stack_count: 4578 In order to show all the stacks, we implement stack_depot_get_next_stack(), which walks all buckets while retrieving the stacks stored in them. stack_depot_get_next_stack() will return all stacks, one at a time, by first finding a non-empty bucket, and then retrieving all the stacks stored in that bucket. Once we have completely gone through it, we get the next non-empty bucket and repeat the same steps, and so on until we have completely checked all buckets. Signed-off-by: Oscar Salvador --- include/linux/stackdepot.h | 20 +++++++++ lib/stackdepot.c | 46 +++++++++++++++++++++ mm/page_owner.c | 85 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+) diff --git a/include/linux/stackdepot.h b/include/linux/stackdepot.h index ac62de4d4999..d851ec821e6f 100644 --- a/include/linux/stackdepot.h +++ b/include/linux/stackdepot.h @@ -183,6 +183,26 @@ depot_stack_handle_t stack_depot_save(unsigned long *entries, */ struct stack_record *stack_depot_get_stack(depot_stack_handle_t handle); +/** + * stack_depot_get_next_stack - Returns all stacks, one at a time + * + * @table: Current table we are checking + * @bucket: Current bucket we are checking + * @last_found: Last stack that was found + * + * This function finds first a non-empty bucket and returns the first stack + * stored in it. On consequent calls, it walks the bucket to see whether + * it contains more stacks. + * Once we have walked all the stacks in a bucket, we check + * the next one, and we repeat the same steps until we have checked all of them + * + * Return: A pointer a to stack_record struct, or NULL when we have walked all + * buckets. + */ +struct stack_record *stack_depot_get_next_stack(unsigned long *table, + struct list_head **bucket, + struct stack_record **last_found); + /** * stack_depot_fetch - Fetch a stack trace from stack depot * diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 197c355601f9..107bd0174cd6 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -782,6 +782,52 @@ unsigned int stack_depot_get_extra_bits(depot_stack_handle_t handle) } EXPORT_SYMBOL(stack_depot_get_extra_bits); +struct stack_record *stack_depot_get_next_stack(unsigned long *table, + struct list_head **curr_bucket, + struct stack_record **last_found) +{ + struct list_head *bucket = *curr_bucket; + unsigned long nr_table = *table; + struct stack_record *found = NULL; + unsigned long stack_table_entries = stack_hash_mask + 1; + + rcu_read_lock_sched_notrace(); + if (!bucket) { + /* + * Find a non-empty bucket. Once we have found it, + * we will use list_for_each_entry_continue_rcu() on the next + * call to keep walking the bucket. + */ +new_table: + bucket = &stack_table[nr_table]; + list_for_each_entry_rcu(found, bucket, hash_list) { + goto out; + } + } else { + /* Check whether we have more stacks in this bucket */ + found = *last_found; + list_for_each_entry_continue_rcu(found, bucket, hash_list) { + goto out; + } + } + + /* No more stacks in this bucket, check the next one */ + nr_table++; + if (nr_table < stack_table_entries) + goto new_table; + + /* We are done walking all buckets */ + found = NULL; + +out: + *table = nr_table; + *curr_bucket = bucket; + *last_found = found; + rcu_read_unlock_sched_notrace(); + + return found; +} + static int stats_show(struct seq_file *seq, void *v) { /* diff --git a/mm/page_owner.c b/mm/page_owner.c index 0adf41702b9d..aea212734557 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -749,6 +749,89 @@ static const struct file_operations proc_page_owner_operations = { .llseek = lseek_page_owner, }; +struct stack_iterator { + unsigned long nr_table; + struct list_head *bucket; + struct stack_record *last_stack; +}; + +static void *stack_start(struct seq_file *m, loff_t *ppos) +{ + struct stack_iterator *iter = m->private; + + if (*ppos == -1UL) + return NULL; + + return stack_depot_get_next_stack(&iter->nr_table, + &iter->bucket, + &iter->last_stack); +} + +static void *stack_next(struct seq_file *m, void *v, loff_t *ppos) +{ + struct stack_iterator *iter = m->private; + struct stack_record *stack; + + stack = stack_depot_get_next_stack(&iter->nr_table, + &iter->bucket, + &iter->last_stack); + *ppos = stack ? *ppos + 1 : -1UL; + + return stack; +} + +static int stack_print(struct seq_file *m, void *v) +{ + char *buf; + int ret = 0; + struct stack_iterator *iter = m->private; + struct stack_record *stack = iter->last_stack; + + if (!stack->size || stack->size < 0 || refcount_read(&stack->count) < 2) + return 0; + + buf = kzalloc(PAGE_SIZE, GFP_KERNEL); + + ret += stack_trace_snprint(buf, PAGE_SIZE, stack->entries, stack->size, + 0); + if (!ret) + goto out; + + scnprintf(buf + ret, PAGE_SIZE - ret, "stack_count: %d\n\n", + refcount_read(&stack->count)); + + seq_printf(m, buf); + seq_puts(m, "\n\n"); +out: + kfree(buf); + + return 0; +} + +static void stack_stop(struct seq_file *m, void *v) +{ +} + +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(struct stack_iterator)); +} + +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)) { @@ -758,6 +841,8 @@ static int __init pageowner_init(void) debugfs_create_file("page_owner", 0400, NULL, NULL, &proc_page_owner_operations); + debugfs_create_file("page_owner_stacks", 0400, NULL, NULL, + &page_owner_stack_operations); return 0; }