From mboxrd@z Thu Jan 1 00:00:00 1970 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 0B320C4829E for ; Thu, 15 Feb 2024 23:37:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 953F56B0093; Thu, 15 Feb 2024 18:37:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 903836B009E; Thu, 15 Feb 2024 18:37:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7A47D6B009F; Thu, 15 Feb 2024 18:37:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 669D16B0093 for ; Thu, 15 Feb 2024 18:37:17 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 4201CC03FF for ; Thu, 15 Feb 2024 23:37:17 +0000 (UTC) X-FDA: 81795651714.03.F4397FB Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) by imf27.hostedemail.com (Postfix) with ESMTP id 7B4CF40002 for ; Thu, 15 Feb 2024 23:37:15 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=alpYRFLy; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf27.hostedemail.com: domain of andreyknvl@gmail.com designates 209.85.221.47 as permitted sender) smtp.mailfrom=andreyknvl@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708040235; a=rsa-sha256; cv=none; b=GvAm93OSy3XYSY52SGgWG5ZPqTp5HfxSn5G3I07eEbJr4Cqs0fd4gPJ7PYDM282bU9h3J9 WwhfhqnCeVdj+f/6W9jfpnDtY03RZWaZgQwh45e/5WuNguWud7w1A1Bq3K0VYWiTtmRYVl rcxUPEakUmXdpOWsbQdCmIJWkq3z2FQ= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=alpYRFLy; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf27.hostedemail.com: domain of andreyknvl@gmail.com designates 209.85.221.47 as permitted sender) smtp.mailfrom=andreyknvl@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708040235; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Y/m8RYJ/2xy2EJlSTReMNxRLLHi4TElTKh67Aw8kBy8=; b=QOwxb/JUpgCes3XTzjpng+/Pehg/kU0zCNYzkpUEq39Z9Y87ljxPTOj2ZXsV1rcd5GUepM d9QRDdg1P4lurCn9Kbtw0a94pyuspgZOOJ+eE1LXqoCwO+fK79Oqp2wIN8NWf2e7ZvIf+P jiCPAEgg3GqM7PjfdeHy+KH+JhA8pJo= Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-33934567777so832338f8f.1 for ; Thu, 15 Feb 2024 15:37:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708040234; x=1708645034; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=Y/m8RYJ/2xy2EJlSTReMNxRLLHi4TElTKh67Aw8kBy8=; b=alpYRFLy+s1QsVIRXU5yJarS+gkNN6GO6hmKRpMSMQbD5f//C6HcSh/k05p0x2Tdg1 rn6dCu77arfHLnDosDsSy2F0gBOTorZkiueWmLunRwme92ADwlt/FJ/sSeYN4TnRzORF zv+O7YcAOhApXsS6/YZLp3Y0gz/++zLjQPtWxmtA7vChQLfVvVK3eoM05aJf9RFWg09F xUHEzuLPhMXY/YkgIZSEtXIXl6va1e6pspplffBPkJVbTWsGB3q3rbrH4saq3HZUXDwI g9Tvb9QCj40a+CYgEurt7i3NlgPwYnkVIFMdcNHrzg7qFfEJOwYqJgp5bLBB/JMKoq6M Lxhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708040234; x=1708645034; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Y/m8RYJ/2xy2EJlSTReMNxRLLHi4TElTKh67Aw8kBy8=; b=JuitbJc+mo0+TdZZDcFYaGR5FzVZJNau68ZI6i2mLxZURQ38T0b+33YUNXhyNwcvV4 L/bHfrJZZuBrPyQItkU94Kx5h3J8PgcZL2Lh+WDhTrAKNcPRCxHKu54RUiBFpd536Esj oC15bq9MOeP86jecRksWLLDdmbmSX6dqlEgueaqi3ak8hqgfyK0IJo4AfFn8OWfe80rq mbiqRB7cbVKsz2RcY+IQfDf2IS/l9eZq/nK+b+RWff1hgYF92rWHlJX82aCOrv3bcKFO JddNRm2lxqet7Mdx4MmoWT8Vbc2wZ8zQsnrxsUA8tHWOeY60GB+ZHiA4aq7Ua/L42Piy lirQ== X-Forwarded-Encrypted: i=1; AJvYcCUJCkQEKeDvYUOFkJYd4drVaiLHe9GAhXEaVDnmYwGlP+lCbKLlGcWH5Ie32VY+5W0K/7b4LU//mysKUSvgU9h/W0g= X-Gm-Message-State: AOJu0Yzl1NGujD8IYD/z5FbzbWM8yCo2l86oS66yuWaEw7nYv7pc+OPU +91kbhGN0hsZiIAsnGd9lsu1aurw9Ilu4juyqVunmNx/3rZYmpWd9VVm0HuknxyhoCNChp5PC4K QWq/NWc9wqA/EqcFgZi0+CJqnBha3+D4L X-Google-Smtp-Source: AGHT+IG8IeXazwKjfl2t4ZhPPlmb0lTrd7uK1A+7dlMs7k9Ly5CAcoJ3L0coWubOHXiOFB0X7MK3ocRk2XkMLC2wepA= X-Received: by 2002:adf:f44e:0:b0:33b:2a6f:385a with SMTP id f14-20020adff44e000000b0033b2a6f385amr2601719wrp.30.1708040233947; Thu, 15 Feb 2024 15:37:13 -0800 (PST) MIME-Version: 1.0 References: <20240215215907.20121-1-osalvador@suse.de> In-Reply-To: <20240215215907.20121-1-osalvador@suse.de> From: Andrey Konovalov Date: Fri, 16 Feb 2024 00:37:03 +0100 Message-ID: Subject: Re: [PATCH v10 0/7] page_owner: print stacks and their outstanding allocations To: Oscar Salvador Cc: Andrew Morton , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Michal Hocko , Vlastimil Babka , Marco Elver , Alexander Potapenko Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 7B4CF40002 X-Stat-Signature: 9xr6bhs9pa93xor33mbb5ts73cj5k7ea X-HE-Tag: 1708040235-471479 X-HE-Meta: U2FsdGVkX18/qNoTQXc8Nz7E2uyx5Ws4+rN4+UUdomyEEYt0qPplCZrGexfyWP+h+dNk/ujYDTM/HSRbNmJUcB36jqxZYztDwwdN7AVAv6syxvFcydChGuIsp+1OsfD/oKAT5ox7pVrtMhjVR0t0f4bTH9e6ezl4xGIBddTxK276medUPJY62HhZuy8VOllIYHySDdFEY1l3WErrwcFoN5f/W3IUu0BVLX5ZH1nr7NjYzuBQgY3gIbz1JRf+5hzTYraj8TSq642QBd68AmKwFLSwM368+ywryCULJAAqqHZXYumjdZjZhGCNJlVEGDkhizRRx/v06l537zim1Za2pOX0EdLwcEazSj6IG9Lw35kF4W63HqnXYpN99P1Z4NWhEr/DSjDNEtqxG+GpwatPySn9rEKac5A/zxQoz8M52chujLZvw2DG9HKAeMeJLXpSwSVitkdKpUYaNC+WzKYMAl6ZXoiMP/VQesimZX80jwDtpO83HN8xOHw7lDpTg19eWQXcu+1PM+rZ5WyLs37S04mnGr0daFaiP5vaf0s/NhcYLXM12FhnaJl19qbwFOiOuG8khdvEMpdBbKNgcBsZ9kkvV1UHGuCdxO6rMMBkufJ2kI6CyTCun5XjTwNHLT3YNBGNPYebnXPCBnU0zHTHD/O6C0ZdSBpnqOyo05I+DxF1hq/sKGiKG3fDhwEeHAFFu3nU3oFXCBvDfPKwtBsRkcHBEaaoyLprebEl6AGsFnZrmrDtFF/idk2vq92DB2n6uErReWijS7ygKs7KP+5VEfBaJpj6/WUjcBTwj3hjD8qu9WXRuI418BlSAetlvoC7g9e/AL5MQLN0RA+P4apTpfTx2PRfIgymKHUiPJriCMO5Rw/J6a5SoQnD6zJurpasljjPgbcNX4f4VP6Tay16ubCiBYv93uY/Y5xR8WeEdD9ylh5y5uGC3N7YLrnD0rNyoeSpjK+6eN0/QIWX/fM DXGpQKH0 AyR0kR0HI0q1y3l4UETj1xgb2/kqt8TK/oENVYWm419SknFvpCsd2ojMca5hbA4ou/8I6NZgkaQbLbeZyUY8HsXqosoKD0r71bUM5SzxFe5MuxJu5y0leh3ECy4HkfDdNQgLJzO0B6+P80uDmzXt1Tf6BTPyTZ5fiuYyutXlaqQcLi7FNXqSH9nzAJC5AW3nyerXUk1W7UKjq+RaI0XSVnqSAoLT4L7VAjr80fQOR2p5EX4k5NMGafWKamykbAarOiqVM3FP72MkZfVbPiWGOHoMjGOn9RVqE4wPDesvS2lPX0xbhhicbfkjKYOObC/OV879Zn4SzYfhKcyskXkoWMOCoVa0djty4eXwWxOjeeoUSorCdbG7w1MXl46FI8G7d7OWendSyRVPPJYLVrxVHgdOSRwC655wrhF2KI5PB6LbP+nA= 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: On Thu, Feb 15, 2024 at 10:57=E2=80=AFPM Oscar Salvador = wrote: > > 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 Vlastim= il > > 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 w= ay > 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 operati= on) > 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(-) > > -- > 2.43.0 > Acked-by: Andrey Konovalov