From patchwork Mon Feb 12 22:30:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 200089 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:bc8a:b0:106:860b:bbdd with SMTP id dn10csp200817dyb; Mon, 12 Feb 2024 14:30:39 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWtfj3jSRVzC7IWWB+CPzsNLr5cNLkV+l5pKxvT+M4FoP85jpPDqMYobwd3cto5bIsdqVMH5RNmpLqUByM4UW/rW2J42g== X-Google-Smtp-Source: AGHT+IGB2D2uHuWARkiNcYmsjU3dsDqcoiGS7hp9FxY7fi9TQiXthzL6ublSK9mmwuaMa1Xagc6Z X-Received: by 2002:a17:906:da0c:b0:a3c:5e3:df84 with SMTP id fi12-20020a170906da0c00b00a3c05e3df84mr5134540ejb.31.1707777039784; Mon, 12 Feb 2024 14:30:39 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707777039; cv=pass; d=google.com; s=arc-20160816; b=GNYvsg8aqncuAFzSiOQoT9DO8TIyZU6Z906PzHBiBMazyF0puaeh33YEw2xZtFzKtn XpiucDRnJUWDD1m2KXOOmyrV91WSyaQ87D7gCPOo+emIVcZ3ZY3CWXE6QKHkm8xocTqz ONa3kExHy42mkkdIUThrWGCjWe2yjKz+i4e4n1J5sUXRKOQsHZPRDdVfkjaOYirXeLTS BuNZR2FClzZg2k4EA9hmdqeCEDSu0NdCOHz5NUbvDoMKtE/DK09I/cEv3m3WVv4KEdU4 kcjRMIUuvA5+zXbanvGVGpBxh3DVq6t12pGj0V+mp/+xP3dsVXLNkOcw/MzqKsCZlVcO Lvrw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature:dkim-signature :dkim-signature:dkim-signature; bh=S9GgOB2bpckE3olLSUoq4h/Hy5/g1rCORxaucTcFT3o=; fh=/a2K6YjvOr5qbABnP4J7WLeu68L34XolGfhm2JAouIU=; b=CdvK90Uc+V5xDcBLxOBTyYIKSDunrFwrVBrnNYd8I1RlwU1TU4MHWa4+/SEELCm3f1 j4DNKCgZScK+QTdpDZCxCoXCwiri01HqNZ0BS8+AmF6ryhI3se4S0elO/DndyKAoy8nu f+RsaxZPaGDCZeUYHGoq9Iz5OdnSbUf3W2wk/DB0jXSjhOJOXgrzpiOm4t+Ir0uT7VWl X9qn0T06m71rCT/aUSloRZr+hCfKddCpasHE0YZv9Hs2Qm5bXsUdN6Lfh97QsR/nLQnL MGYq7a8pohzFpzIAORkFYI/1gzuGcHd5AarvlTMB7oJcBwVQutwxBhe9+nMFtvhzsQ3W zVtA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=aO8sIvNw; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=aO8sIvNw; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; arc=pass (i=1 spf=pass spfdomain=suse.de dkim=pass dkdomain=suse.de dkim=pass dkdomain=suse.de dmarc=pass fromdomain=suse.de); spf=pass (google.com: domain of linux-kernel+bounces-62480-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-62480-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de X-Forwarded-Encrypted: i=2; AJvYcCW8PW1Boxwk0W1JbyxvHPjVDSa+vIkv/ht/84fs1ZHeHRerNhKK8Gp9gsAbeuM4X8KWfDftKkV6NUUR9y0VJZrVbfrB/Q== Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id j5-20020a170906474500b00a37adf06a45si557561ejs.258.2024.02.12.14.30.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 14:30:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-62480-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=aO8sIvNw; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=aO8sIvNw; dkim=neutral (no key) header.i=@suse.de header.s=susede2_ed25519; arc=pass (i=1 spf=pass spfdomain=suse.de dkim=pass dkdomain=suse.de dkim=pass dkdomain=suse.de dmarc=pass fromdomain=suse.de); spf=pass (google.com: domain of linux-kernel+bounces-62480-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-62480-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 384601F2285A for ; Mon, 12 Feb 2024 22:30:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B02144D595; Mon, 12 Feb 2024 22:29:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="aO8sIvNw"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="IMz7vyf+"; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b="aO8sIvNw"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="IMz7vyf+" Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F2CCD4EB34 for ; Mon, 12 Feb 2024 22:29:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707776973; cv=none; b=LmqbZtN6ey2xtTH2DND4M8Juhy19aRf9FLZtnDsIJGNfagw3qtFgfA5sKYUgyfSI+iNDAtND8SoMa42ua6pQJiNkn7r9HjtnCKa7CT4b/FLnxW3TGCOuI/B9fUr+llcPrYK8SVZv+FvfcO1dsfO1RaemH9zKoof3ZF+twjFpDEE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707776973; c=relaxed/simple; bh=PslCDCM3yg1q5QFWu02AeTqtUBHZaW7ctPxFlrHtAsM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Rauubtk7JUTCwRbPpR4noMBI8JrpzHf+5R81rsJa7Rd0RguenazMHLO3uwXfQ/7PY6OhyStoh1TdBM5boua4X9b6qV6l4w2hPIJiG2aCpmP/II1j0nFMWiiOtQ3iNjxIzbv5SNSi2BYlK8monVaU9sY1PwTHsPnZiuHWx8NvdA8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de; spf=pass smtp.mailfrom=suse.de; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=aO8sIvNw; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=IMz7vyf+; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.b=aO8sIvNw; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b=IMz7vyf+; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.de 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-out1.suse.de (Postfix) with ESMTPS id 4330622160; Mon, 12 Feb 2024 22:29:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1707776970; 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=S9GgOB2bpckE3olLSUoq4h/Hy5/g1rCORxaucTcFT3o=; b=aO8sIvNwoxgZohBoscNS6wvlgcWjwXje8eEteMwwtBuB/+u2WRUjh8NwW4qXahgHFoOv/3 /2MuhVHGZzW70sU0W/+D51GvcRLU9+pVPIMz7GxwfGGqH/5FTYg667EBvytadc8R/KNzwU i3X2tsgY6Ob+sgqpA4KA0ngkfibk9tQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1707776970; 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=S9GgOB2bpckE3olLSUoq4h/Hy5/g1rCORxaucTcFT3o=; b=IMz7vyf+FqsnnTqxhLZ2ZG4SOkTO2sPVHymdrZdBtZvjkwp8tB+6RN2TSyC8izEmJRrrPE aY8LnAReVrEaukCA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1707776970; 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=S9GgOB2bpckE3olLSUoq4h/Hy5/g1rCORxaucTcFT3o=; b=aO8sIvNwoxgZohBoscNS6wvlgcWjwXje8eEteMwwtBuB/+u2WRUjh8NwW4qXahgHFoOv/3 /2MuhVHGZzW70sU0W/+D51GvcRLU9+pVPIMz7GxwfGGqH/5FTYg667EBvytadc8R/KNzwU i3X2tsgY6Ob+sgqpA4KA0ngkfibk9tQ= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1707776970; 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=S9GgOB2bpckE3olLSUoq4h/Hy5/g1rCORxaucTcFT3o=; b=IMz7vyf+FqsnnTqxhLZ2ZG4SOkTO2sPVHymdrZdBtZvjkwp8tB+6RN2TSyC8izEmJRrrPE aY8LnAReVrEaukCA== 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 79FDA13212; Mon, 12 Feb 2024 22:29:29 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap2.dmz-prg2.suse.org with ESMTPSA id eIdfGcmbymX5JgAAn2gu4w (envelope-from ); Mon, 12 Feb 2024 22:29:29 +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 v8 3/5] mm,page_owner: Display all stacks and their count Date: Mon, 12 Feb 2024 23:30:27 +0100 Message-ID: <20240212223029.30769-4-osalvador@suse.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240212223029.30769-1-osalvador@suse.de> References: <20240212223029.30769-1-osalvador@suse.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Authentication-Results: smtp-out1.suse.de; none X-Spam-Level: X-Spam-Score: 0.70 X-Spamd-Result: default: False [0.70 / 50.00]; ARC_NA(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; R_MISSING_CHARSET(2.50)[]; FREEMAIL_ENVRCPT(0.00)[gmail.com]; MIME_GOOD(-0.10)[text/plain]; TO_MATCH_ENVRCPT_ALL(0.00)[]; BROKEN_CONTENT_TYPE(1.50)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; RCVD_COUNT_THREE(0.00)[3]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; NEURAL_HAM_SHORT(-0.20)[-1.000]; RCPT_COUNT_SEVEN(0.00)[9]; MID_CONTAINS_FROM(1.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.de:email]; FUZZY_BLOCKED(0.00)[rspamd.com]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; FREEMAIL_CC(0.00)[vger.kernel.org,kvack.org,suse.com,suse.cz,google.com,gmail.com,suse.de]; RCVD_TLS_ALL(0.00)[]; BAYES_HAM(-3.00)[100.00%] X-Spam-Flag: NO X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790734017060122311 X-GMAIL-MSGID: 1790734017060122311 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 --- mm/page_owner.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 1 deletion(-) diff --git a/mm/page_owner.c b/mm/page_owner.c index 7d1b3f75cef3..3e4b7cd7c8f8 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -84,7 +84,12 @@ static void add_stack_record_to_list(struct stack_record *stack_record) stack_list = stack; } else { 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); } @@ -792,8 +797,97 @@ 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) +{ + char *buf; + int ret = 0; + struct stack *stack = v; + struct stack_record *stack_record = stack->stack_record; + + if (!stack_record->size || stack_record->size < 0 || + refcount_read(&stack_record->count) < 2) + return 0; + + buf = kzalloc(PAGE_SIZE, GFP_KERNEL); + + ret += stack_trace_snprint(buf, PAGE_SIZE, stack_record->entries, + stack_record->size, 0); + if (!ret) + goto out; + + scnprintf(buf + ret, PAGE_SIZE - ret, "stack_count: %d\n\n", + refcount_read(&stack_record->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, 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; @@ -801,6 +895,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; }