From patchwork Thu Feb 15 21:59:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 13559169 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 1B087C48BC4 for ; Thu, 15 Feb 2024 21:58:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6B7D18D000E; Thu, 15 Feb 2024 16:58:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 640C78D0001; Thu, 15 Feb 2024 16:58:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4B9848D000E; Thu, 15 Feb 2024 16:58:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 3673F8D0001 for ; Thu, 15 Feb 2024 16:58:03 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 0B3DD1A0D60 for ; Thu, 15 Feb 2024 21:58:03 +0000 (UTC) X-FDA: 81795401646.15.E241F65 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf15.hostedemail.com (Postfix) with ESMTP id BEC4DA0011 for ; Thu, 15 Feb 2024 21:58:00 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=GwDF9glj; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=xqN+Z09O; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=ovQH+Xdy; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b="hsHFgKb/"; dmarc=pass (policy=none) header.from=suse.de; spf=pass (imf15.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.130 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=1708034281; 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=wlH79iu0m6ZVE4B9lZS6vBJUdRZFChv732g2RiDmk+0=; b=4O4Z1Ifv74ejRZNpVMVUvg1g74R3BgOLGeNrz81A2Jb0tPRCUpW8o5AbzBWk1Ir7rsNwmQ tIPinnkgYYanNSDdXRGxGg/lRNumxMrPzLj8yV/jFVZTSZxQZSEUqaq+9Hv02EO8p30wFv s7a7Wvum4tAlmbgM7pQjBxpfnxZBKOw= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=GwDF9glj; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=xqN+Z09O; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=ovQH+Xdy; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b="hsHFgKb/"; dmarc=pass (policy=none) header.from=suse.de; spf=pass (imf15.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.130 as permitted sender) smtp.mailfrom=osalvador@suse.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708034281; a=rsa-sha256; cv=none; b=fEkjYZu2hm+7ZRqyNlhdxTvTE8D/444TAYLHqyrMhM4M7trOzq0PJrFZDRO+kaO8NQdfTK drcxpRobrKPx9BUbqX6k5Rhqj+uyxV7vReB94O1BB2rxgK3Mw4nrbuChk9TVCmCZ6d667G RYl4rn+yih07bzv5wQ1+Dqtvt5TX5e4= 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-out1.suse.de (Postfix) with ESMTPS id E1468220A6; Thu, 15 Feb 2024 21:57:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1708034279; 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=wlH79iu0m6ZVE4B9lZS6vBJUdRZFChv732g2RiDmk+0=; b=GwDF9gljQuIzaJfVQkpkYGZKGO11aj8ecVwVM2P84OMEdoSrLZKRF+GQDuzCFKpugOhgCG UhderIGKZwTtj3oU3ZI51kMMpcH62kD0W2ZDrDvEAtPxJrzrJwf0h6QWFoz9m0sCmcf7gz H/9W7tXlYWSmcks9vMRtNbdLT81WJTc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1708034279; 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=wlH79iu0m6ZVE4B9lZS6vBJUdRZFChv732g2RiDmk+0=; b=xqN+Z09OmW9oK/rb8qfdNHj1tQNH2Exxuxd0+4mcQwVx2NSrMiIfOheU4S0TLg8x6lTBG2 mZry7FZuBomB/oAw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1708034278; 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=wlH79iu0m6ZVE4B9lZS6vBJUdRZFChv732g2RiDmk+0=; b=ovQH+XdyniSgEVPH4ikrO22IjJfUynMkDkUA2sIe6xiUJ9o8gUQJ0LasZdFlm/o/2Y3sTB XGgfh1ojRsclQWDw9wpND3MyY6+WONszNUfEVApn9aSyUQbb9agcTHEzZIqH5gfWdtn1gU 6mQoDchGPHbUKpjhXNyC1xI3BeX0/TU= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1708034278; 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=wlH79iu0m6ZVE4B9lZS6vBJUdRZFChv732g2RiDmk+0=; b=hsHFgKb/+LBsgEKLnLLF38y9Br6d4A/EP3cJMENlHibHH1INdM4pYhhDu4iw4VIHRrRk64 RPDp2ISydDP/8ACg== 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 57C2F1346A; Thu, 15 Feb 2024 21:57:58 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap2.dmz-prg2.suse.org with ESMTPSA id KDotEuaIzmXPAQAAn2gu4w (envelope-from ); Thu, 15 Feb 2024 21:57: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 v10 0/7] page_owner: print stacks and their outstanding allocations Date: Thu, 15 Feb 2024 22:59:00 +0100 Message-ID: <20240215215907.20121-1-osalvador@suse.de> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spamd-Bar: / X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: BEC4DA0011 X-Stat-Signature: 3ymiit5wym8ib6xzfq9owtodgbjd5j87 X-HE-Tag: 1708034280-172693 X-HE-Meta: U2FsdGVkX19px7RExO3kpgyKy6GKTKpgP3h0pxfZ/gA9gocegZstfaers36YI9uopoweBvD6gb3CBSOMQj8MrbtxmG/Gc8xsQmARr4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DIcQ70rtHZPZm7uBb2FqJnVpxtw7OOvq0bTktxiuaSo6WFjymza40Czg29SMR216lWeJb6l0wmXoaiEJzBJIu7CeZcDA== 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 v9 -> v10 - Fix unwanted change in patch#2 - Collect Acked-by and Reviewed-by from Marco and Vlastimil for the missing patches - Fix stack_record count by substracting 1 in stack_print by Vlastimil Changes v8 -> v9 - Fix handle-0 for the very first stack_record entry - Collect Acked-by and Reviewed-by from Marco and Vlastimil - Adressed feedback from Marco and Vlastimil - stack_print() no longer allocates a memory buffer, prints directly using seq_printf: by Vlastimil - Added two static struct stack for dummy_handle and faiure_handle - add_stack_record_to_list() now filters out the gfp_mask the same way stackdepot does, for consistency - Rename set_threshold to count_threshold 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 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 (7): lib/stackdepot: Fix first entry having a 0-handle lib/stackdepot: Move stack_record struct definition into the header mm,page_owner: Maintain own list of stack_records structs 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 | 45 +++++++ include/linux/stackdepot.h | 58 +++++++++ lib/stackdepot.c | 65 +++-------- mm/page_owner.c | 200 +++++++++++++++++++++++++++++++- 4 files changed, 318 insertions(+), 50 deletions(-) Acked-by: Andrey Konovalov