From patchwork Wed Feb 14 17:01:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 13556793 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 0574EC48BC1 for ; Wed, 14 Feb 2024 17:01:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C31A86B00AC; Wed, 14 Feb 2024 12:01:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BB8016B00AD; Wed, 14 Feb 2024 12:01:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9E2926B00AE; Wed, 14 Feb 2024 12:01:03 -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 86C0A6B00AC for ; Wed, 14 Feb 2024 12:01:03 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 3D3B1160EA7 for ; Wed, 14 Feb 2024 17:01:03 +0000 (UTC) X-FDA: 81791024406.01.67E14DD Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by imf10.hostedemail.com (Postfix) with ESMTP id CDA5CC003C for ; Wed, 14 Feb 2024 17:01:00 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=KpOOoFhu; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=8mM7AYOV; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=KpOOoFhu; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=8mM7AYOV; dmarc=pass (policy=none) header.from=suse.de; spf=pass (imf10.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.131 as permitted sender) smtp.mailfrom=osalvador@suse.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707930061; 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=XK6TmU+hXbwqsi+dsf3umxmeEe7QW0b6t55ktNSdV6M=; b=aE/CSFd5TN+2neRQLRB9yB8w29EuWUMYqYVDF5WZKj10bPc3WAIDLNiwgVXx0CIaqCsRIg S5d3oU2+6OTMDshFeJZqwd4lrTNoMuGvLkSIwLxRIGgBOeuKR9CBBo8lXwSQD4hPjjDpTT 86SPLADs+Bp4UrG//0CsXnYVW+cbCJM= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=KpOOoFhu; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=8mM7AYOV; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=KpOOoFhu; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=8mM7AYOV; dmarc=pass (policy=none) header.from=suse.de; spf=pass (imf10.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.131 as permitted sender) smtp.mailfrom=osalvador@suse.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707930061; a=rsa-sha256; cv=none; b=vU3l59nPrFWsf7bTgGswz0nV0ajSdu02wBHCj6trpB50kpOw200XrsixiTrIzYczAFR+hN SAHMFi7HArubEdNZC7KID0/UCoa8lnPyswPTNfSFKfV1DVy1GQ8wHYzV0NzocLeq9+1Snf woka5842HFUfkV7585Z7neL0qIwobUM= Received: from imap2.dmz-prg2.suse.org (imap2.dmz-prg2.suse.org [10.150.64.98]) (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-out2.suse.de (Postfix) with ESMTPS id 0E76C1FD19; Wed, 14 Feb 2024 17:00:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1707930059; 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=XK6TmU+hXbwqsi+dsf3umxmeEe7QW0b6t55ktNSdV6M=; b=KpOOoFhuvYfq+25Mh6bBdoeH7GvbWv8AgabL7VOc1wQmWvBRymwKSBbSQjDzDfRuc/Q9Mo txj5A0xrdL8pqoN64tArqndeTjo6g/lWUslv//Ks8seGJAO8qR3XoAqr7WCcCxdK/h85kU BcflLpVREbh9rlhp7M2Y3sP/YrHAf5w= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1707930059; 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=XK6TmU+hXbwqsi+dsf3umxmeEe7QW0b6t55ktNSdV6M=; b=8mM7AYOVWw6nRhbI0vrmeuOYM3JCnE5+Zjw2UKNfuIuXA1z8eD/LkAg2BJ6dK8RTTChnpc CCpaNpkuhk81GvCQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1707930059; 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=XK6TmU+hXbwqsi+dsf3umxmeEe7QW0b6t55ktNSdV6M=; b=KpOOoFhuvYfq+25Mh6bBdoeH7GvbWv8AgabL7VOc1wQmWvBRymwKSBbSQjDzDfRuc/Q9Mo txj5A0xrdL8pqoN64tArqndeTjo6g/lWUslv//Ks8seGJAO8qR3XoAqr7WCcCxdK/h85kU BcflLpVREbh9rlhp7M2Y3sP/YrHAf5w= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1707930059; 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=XK6TmU+hXbwqsi+dsf3umxmeEe7QW0b6t55ktNSdV6M=; b=8mM7AYOVWw6nRhbI0vrmeuOYM3JCnE5+Zjw2UKNfuIuXA1z8eD/LkAg2BJ6dK8RTTChnpc CCpaNpkuhk81GvCQ== Received: from imap2.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 imap2.dmz-prg2.suse.org (Postfix) with ESMTPS id 815BD13A0B; Wed, 14 Feb 2024 17:00:58 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap2.dmz-prg2.suse.org with ESMTPSA id gMbiHMrxzGUfMwAAn2gu4w (envelope-from ); Wed, 14 Feb 2024 17:00:58 +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 v9 5/7] mm,page_owner: Display all stacks and their count Date: Wed, 14 Feb 2024 18:01:55 +0100 Message-ID: <20240214170157.17530-6-osalvador@suse.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214170157.17530-1-osalvador@suse.de> References: <20240214170157.17530-1-osalvador@suse.de> MIME-Version: 1.0 X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: CDA5CC003C X-Stat-Signature: jbijdax9kukhjbaffinxutrhqae6jdqu X-Rspam-User: X-HE-Tag: 1707930060-767021 X-H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RRkNNq0bPDpnas3+UWHGltTtNn5+uPFYme5YbpWg06ayK6FTJ1nSPqARJOD/nrY4c1eUz3+2DL8m+/4bvjmQh5sZepyUnOYQCM7Opy7Ez8QJhFs/qR0bKvcqXDKBKChL/r5x8iNfhaOuXfuCKhk/ax0gX9twoSW5pYd43sjKV8CAcW2JPYRIgwtjYPB76SvCahSakjQ== 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 directory called 'page_owner_stacks' under /sys/kernel/debug/, with a file called 'show_stacks' in it. Reading from that file 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 The seq stack_{start,next} functions will iterate through the list stack_list in order to print all stacks. Signed-off-by: Oscar Salvador Acked-by: Marco Elver Reviewed-by: Vlastimil Babka --- mm/page_owner.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/mm/page_owner.c b/mm/page_owner.c index df6a923af5de..5258a417f4d1 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -171,7 +171,13 @@ static void add_stack_record_to_list(struct stack_record *stack_record, spin_lock_irqsave(&stack_list_lock, flags); stack->next = stack_list; - stack_list = stack; + /* + * This pairs with smp_load_acquire() from function + * stack_start(). This guarantees that stack_start() + * will see an updated stack_list before starting to + * traverse the list. + */ + smp_store_release(&stack_list, stack); spin_unlock_irqrestore(&stack_list_lock, flags); } @@ -805,8 +811,90 @@ static const struct file_operations proc_page_owner_operations = { .llseek = lseek_page_owner, }; +static void *stack_start(struct seq_file *m, loff_t *ppos) +{ + struct stack *stack; + + if (*ppos == -1UL) + return NULL; + + if (!*ppos) { + /* + * This pairs with smp_store_release() from function + * add_stack_record_to_list(), so we get a consistent + * value of stack_list. + */ + stack = smp_load_acquire(&stack_list); + } else { + stack = m->private; + stack = stack->next; + } + + m->private = stack; + + return stack; +} + +static void *stack_next(struct seq_file *m, void *v, loff_t *ppos) +{ + struct stack *stack = v; + + stack = stack->next; + *ppos = stack ? *ppos + 1 : -1UL; + m->private = stack; + + return stack; +} + +static int stack_print(struct seq_file *m, void *v) +{ + int i; + struct stack *stack = v; + unsigned long *entries; + unsigned long nr_entries; + struct stack_record *stack_record = stack->stack_record; + + nr_entries = stack_record->size; + entries = stack_record->entries; + + if (!nr_entries || nr_entries < 0 || + refcount_read(&stack_record->count) < 2) + return 0; + + for (i = 0; i < nr_entries; i++) + seq_printf(m, " %pS\n", (void *)entries[i]); + seq_printf(m, "stack_count: %d\n\n", refcount_read(&stack_record->count)); + + 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, 0); +} + +static 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) { + struct dentry *dir; + if (!static_branch_unlikely(&page_owner_inited)) { pr_info("page_owner is disabled\n"); return 0; @@ -814,6 +902,9 @@ static int __init pageowner_init(void) debugfs_create_file("page_owner", 0400, NULL, NULL, &proc_page_owner_operations); + dir = debugfs_create_dir("page_owner_stacks", NULL); + debugfs_create_file("show_stacks", 0400, dir, NULL, + &page_owner_stack_operations); return 0; }