From patchwork Mon Feb 12 22:30:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 13554126 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 83F03C4829F for ; Mon, 12 Feb 2024 22:29:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D87D16B00B9; Mon, 12 Feb 2024 17:29:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D376A6B00BC; Mon, 12 Feb 2024 17:29:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BD7E86B00BD; Mon, 12 Feb 2024 17:29:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id AB4A06B00B9 for ; Mon, 12 Feb 2024 17:29:28 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 79E3D120387 for ; Mon, 12 Feb 2024 22:29:28 +0000 (UTC) X-FDA: 81784594416.22.51D6E79 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by imf05.hostedemail.com (Postfix) with ESMTP id 486BC100003 for ; Mon, 12 Feb 2024 22:29:26 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=nbcxLWgI; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=lDRmpKBB; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=nbcxLWgI; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=lDRmpKBB; spf=pass (imf05.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.131 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=1707776966; a=rsa-sha256; cv=none; b=0757yu3lNzY0aq+KwkhZOknx1/Hio+igrjjZh7WKCHA4ImB6Pdkr3Qh12r5R/3DH40aBy3 kXYyATns8kzvHE3CMQMJzvGN2WzW4ES3YIsn5G0sElOJirqziTRHQc5aXpGh7WJavKttgP nPESvKLwZs05TIxAoa6nywv1weT1VcM= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=nbcxLWgI; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=lDRmpKBB; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=nbcxLWgI; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=lDRmpKBB; spf=pass (imf05.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.131 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=1707776966; 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:references:dkim-signature; bh=o6jCQmVShx+JNBNIxXtCno2N5m0Jm6QgesrlwAl86bc=; b=azIbvuWKwcsioSqh07b56K+ts7Uzr7g32ZgcQtKfZxow81IEWQp5JaLlhr5BimfaW4YvUT RA1qBsJQpeHrjdGy4x+aQ/Y6l0dV/R1UH+uYTJaodw003bumEyggwbfNmF4mjsycvktvaJ 8i93ENBqol1TlvPX93VLzIWq2zOwUi8= Received: from imap2.dmz-prg2.suse.org (imap2.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104: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 6CEEF1F45E; Mon, 12 Feb 2024 22:29:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1707776964; 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; bh=o6jCQmVShx+JNBNIxXtCno2N5m0Jm6QgesrlwAl86bc=; b=nbcxLWgIqRfcJ+ifSOBTVelKhoaBwsLY3/xYSpZphbTT5qkeRb6ajbNssKHRbuOItRi/ye yYfn3EAYRICG7l04m1yEQzjEb7BO5XccOLLXyT4PCmIoIP3BB8gYL/yl6rbCd0vSCj+CZF 8rw8t2OrjrDEBzgrJ/j53/IIwqZwCsU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1707776964; 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; bh=o6jCQmVShx+JNBNIxXtCno2N5m0Jm6QgesrlwAl86bc=; b=lDRmpKBBAlU+RlXHXdzF6n+BpzCzOmhRRb3lcKqvXZ3RTHwDcq61ivPz9fkxCSzepY1uYO zumaXf/OGfqI9ACA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1707776964; 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; bh=o6jCQmVShx+JNBNIxXtCno2N5m0Jm6QgesrlwAl86bc=; b=nbcxLWgIqRfcJ+ifSOBTVelKhoaBwsLY3/xYSpZphbTT5qkeRb6ajbNssKHRbuOItRi/ye yYfn3EAYRICG7l04m1yEQzjEb7BO5XccOLLXyT4PCmIoIP3BB8gYL/yl6rbCd0vSCj+CZF 8rw8t2OrjrDEBzgrJ/j53/IIwqZwCsU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1707776964; 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; bh=o6jCQmVShx+JNBNIxXtCno2N5m0Jm6QgesrlwAl86bc=; b=lDRmpKBBAlU+RlXHXdzF6n+BpzCzOmhRRb3lcKqvXZ3RTHwDcq61ivPz9fkxCSzepY1uYO zumaXf/OGfqI9ACA== 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 9065D13212; Mon, 12 Feb 2024 22:29:23 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap2.dmz-prg2.suse.org with ESMTPSA id R1PhHsObymX5JgAAn2gu4w (envelope-from ); Mon, 12 Feb 2024 22:29:23 +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 0/5] page_owner: print stacks and their outstanding allocations Date: Mon, 12 Feb 2024 23:30:24 +0100 Message-ID: <20240212223029.30769-1-osalvador@suse.de> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spamd-Bar: / X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 486BC100003 X-Stat-Signature: wpnior6695tupcthgo5rahk975pruyzp X-Rspam-User: X-HE-Tag: 1707776966-396482 X-HE-Meta: U2FsdGVkX1+F5aIHIIXYUEpwqUvfpx/iNnG1yPxIt5yq/HH9iPUIjMJEqHJ0PutS++oEZDtim4qO9aRSOG/cH3CJlMLqN0bMUYTnBP/8pZThUXpK/l19bFAzq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zv3nDOmk38UTbnT/wgL65GCTe7fBam20HZnt6v7F1trcy3CW0Rr4L9+sqOYyAj4yzn9WBgt8fAsfA== 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: Changes v7 -> v8 - Rebased on top of -next - page_owner maintains its own stack_records list now - Kill auxiliary stackdepot function to traverse buckets - page_owner_stacks is now a directory with 'show_stacks' and 'set_threshold' - Update Documentation/mm/page_owner.rst - Adressed feedback from Marco Changes v6 -> v7: - Rebased on top of Andrey Konovalov's libstackdepot patchset - Reformulated the changelogs Changes v5 -> v6: - Rebase on top of v6.7-rc1 - Move stack_record struct to the header - Addressed feedback from Vlastimil (some code tweaks and changelogs suggestions) Changes v4 -> v5: - Addressed feedback from Alexander Potapenko Changes v3 -> v4: - Rebase (long time has passed) - Use boolean instead of enum for action by Alexander Potapenko - (I left some feedback untouched because it's been long and would like to discuss it here now instead of re-vamping and old thread) Changes v2 -> v3: - Replace interface in favor of seq operations (suggested by Vlastimil) - Use debugfs interface to store/read valued (suggested by Ammar) page_owner is a great debug functionality tool that lets us know about all pages that have been allocated/freed and their specific stacktrace. This comes very handy when debugging memory leaks, since with some scripting we can see the outstanding allocations, which might point to a memory leak. In my experience, that is one of the most useful cases, but it can get really tedious to screen through all pages and try to reconstruct the stack <-> allocated/freed relationship, becoming most of the time a daunting and slow process when we have tons of allocation/free operations. This patchset aims to ease that by adding a new functionality into page_owner. This functionality creates a new directory called 'page_owner_stacks' under 'sys/kernel//debug' with a read-only file called 'show_stacks', which prints out all the stacks followed by their outstanding number of allocations (being that the times the stacktrace has allocated but not freed yet). This gives us a clear and a quick overview of stacks <-> allocated/free. We take advantage of the new refcount_f field that stack_record struct gained, and increment/decrement the stack refcount on every __set_page_owner() (alloc operation) and __reset_page_owner (free operation) call. Unfortunately, we cannot use the new stackdepot api STACK_DEPOT_FLAG_{GET,PUT} because it does not fulfill page_owner needs, meaning we would have to special case things, at which point makes more sense for page_owner to do its own {dec,inc}rementing of the stacks. E.g: Using STACK_DEPOT_FLAG_PUT, once the refcount reaches 0, such stack gets evicted, so page_owner would lose information. This patch also creates a new file called 'set_threshold' within 'page_owner_stacks' directory, and by writing a value to it, the stacks which refcount is below such value will be filtered out. A PoC can be found below: # cat /sys/kernel/debug/page_owner_stacks/show_stacks > page_owner_full_stacks.txt # head -40 page_owner_full_stacks.txt 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 page_cache_ra_unbounded+0x96/0x180 filemap_get_pages+0xfd/0x590 filemap_read+0xcc/0x330 blkdev_read_iter+0xb8/0x150 vfs_read+0x285/0x320 ksys_read+0xa5/0xe0 do_syscall_64+0x80/0x160 entry_SYSCALL_64_after_hwframe+0x6e/0x76 stack_count: 521 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: 4609 ... ... # echo 5000 > /sys/kernel/debug/page_owner_stacks/set_threshold # cat /sys/kernel/debug/page_owner_stacks/show_stacks > page_owner_full_stacks_5000.txt # head -40 page_owner_full_stacks_5000.txt 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_pwrite64+0x75/0x90 do_syscall_64+0x80/0x160 entry_SYSCALL_64_after_hwframe+0x6e/0x76 stack_count: 6781 prep_new_page+0xa9/0x120 get_page_from_freelist+0x801/0x2210 __alloc_pages+0x18b/0x350 pcpu_populate_chunk+0xec/0x350 pcpu_balance_workfn+0x2d1/0x4a0 process_scheduled_works+0x84/0x380 worker_thread+0x12a/0x2a0 kthread+0xe3/0x110 ret_from_fork+0x30/0x50 ret_from_fork_asm+0x1b/0x30 stack_count: 8641 Oscar Salvador (5): lib/stackdepot: Move stack_record struct definition into the header mm,page_owner: Implement the tracking of the stacks count mm,page_owner: Display all stacks and their count mm,page_owner: Filter out stacks by a threshold mm,page_owner: Update Documentation regarding page_owner_stacks Documentation/mm/page_owner.rst | 44 ++++++++ include/linux/stackdepot.h | 53 +++++++++ lib/stackdepot.c | 51 ++------- mm/page_owner.c | 190 ++++++++++++++++++++++++++++++++ 4 files changed, 295 insertions(+), 43 deletions(-)