From patchwork Wed Feb 21 19:40:49 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13566216 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 EA81BC48BEB for ; Wed, 21 Feb 2024 19:42:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E9C736B00C9; Wed, 21 Feb 2024 14:42:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D5C786B00D6; Wed, 21 Feb 2024 14:42:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9F8936B00D4; Wed, 21 Feb 2024 14:42:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 9DE116B00CB for ; Wed, 21 Feb 2024 14:42:19 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 6C8391C0CD5 for ; Wed, 21 Feb 2024 19:42:19 +0000 (UTC) X-FDA: 81816832398.08.4768DBF Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf25.hostedemail.com (Postfix) with ESMTP id AF705A001C for ; Wed, 21 Feb 2024 19:42:17 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Mpb8R9p6; spf=pass (imf25.hostedemail.com: domain of 3GFLWZQYKCEs574r0ot11tyr.p1zyv07A-zzx8npx.14t@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3GFLWZQYKCEs574r0ot11tyr.p1zyv07A-zzx8npx.14t@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708544537; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=iHJNBSQgY470Nm1vo9kQv1HWTlhW4/9LqFry/+3J5f0=; b=1H1gViQ4yYf4iHawnUV58aRbZYJ1GIWBlGDmFyWbTeJWLIax+wE1WBtjRCY2iKKNCbBSVp EbNPZeKqx423U9fdgzFEHdllxBOFDwFWJ0cVKxkKbJm8KHHylaixFvbBRp0oB7wWFaYotR 1oDsLZZkV7wEhOsBTnHWW2I9rV8sFjk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708544537; a=rsa-sha256; cv=none; b=xhNR9y8BO0WzC6jufnMojqckX4t8Vy6Yxn7+B+Sjq+Z6rTgW25vLUGRrFKc5F3LD76oLRI cPCHT9rqIqXHsRDxNU9/YgIqsbi8Xq/DdUKJjFGMaPflKJd2mlQfca1B0TG3N94AZJkRqE BxNmhtaCbJw2hB931Ec3d1gS7FwJlek= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Mpb8R9p6; spf=pass (imf25.hostedemail.com: domain of 3GFLWZQYKCEs574r0ot11tyr.p1zyv07A-zzx8npx.14t@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3GFLWZQYKCEs574r0ot11tyr.p1zyv07A-zzx8npx.14t@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dc691f1f83aso76390276.1 for ; Wed, 21 Feb 2024 11:42:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708544537; x=1709149337; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=iHJNBSQgY470Nm1vo9kQv1HWTlhW4/9LqFry/+3J5f0=; b=Mpb8R9p6XkftoELXGCt5DWf1FnZmbIkAHIXrJ7XPGLkE6ZpywodK5ZaXADbPhCDAaw MBM1b2R6B2mFGZiAY++HLi4f2t0/Zy6QORBFxEdpahIonFEpGOu5AiaXIBrQHOFIlGxU hLcDkpRxzIN1z0UtXSDjLzYiT87+vHeKMrFF3JqYHwiH6C7iux6JJSp316xIB0RH5bPW B6ezIRTuVb6TACrWLsMd3iUuvEzqntuVeSdWc2RzmD3mgRiiVbpbPIWdKIhbzTcQME/T vYi2DshMVKHwsgyQ8LJ13WZSvy1i3+Z8/ygEp8UnhujY8nN4C1An5di3QwpN7TPWmZ2C goTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708544537; x=1709149337; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=iHJNBSQgY470Nm1vo9kQv1HWTlhW4/9LqFry/+3J5f0=; b=OyE1BGGOSvYQI+ySNtsgVwOdbq69xOW6WDCEePGo0rvghA1Fbc3uaUGPUCW4SjzOkG bhfa+ts2gVkJIyB1g7PvFmxryrWcfK17bOIUkUQxSaF+oinycWC+7tkWT42HwXo/k0pX wvyPNjWmknAFbwhqD454/AWb8IF2vjyEtxYFcpscalRBbrnvp3kYjUFrg1YXSNX5SdL0 vYjGNlfTB6WMiUFq1AYXVcLTzbLLHZ96mHUiX112f2FxJSxfTXh/65q/G1NoYBYKOhtH X0f9noGemTDyhUm6Kpi/AVrhzcJtXQQOceeWt6WOkEmQ95wpC5KHUCzAMwHRbvOS/Wvb 0APA== X-Forwarded-Encrypted: i=1; AJvYcCWovk6lV9imLEAkLSmo2WFr6kAOr6Zo0yoONpF6tdTIMqLUskH7azawBWdT4MMBsF8vx+YgD/N/9yGA8AKatQp/8fk= X-Gm-Message-State: AOJu0Yxw3vIBuKClsFTSN4yntdxZIkxPAOiLKhcePcx0se44qkiVr8Cy NgJkqtL4BAKqMakkzHBBrs5bQC3nYDFz1f2GI4d/DfiAO4gdZ6m5AR/pFaAEab7Kwb9YrQc3uGs JqQ== X-Google-Smtp-Source: AGHT+IHkeXMTMo11jEJZVLK3x/LUPbuFY0r5OfLvnPbsUo0+TD2woe9SeAhjFOGiRZt6wf97J57l2VuRNJw= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:953b:9a4e:1e10:3f07]) (user=surenb job=sendgmr) by 2002:a05:6902:1008:b0:dc6:e884:2342 with SMTP id w8-20020a056902100800b00dc6e8842342mr25280ybt.5.1708544536561; Wed, 21 Feb 2024 11:42:16 -0800 (PST) Date: Wed, 21 Feb 2024 11:40:49 -0800 In-Reply-To: <20240221194052.927623-1-surenb@google.com> Mime-Version: 1.0 References: <20240221194052.927623-1-surenb@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240221194052.927623-37-surenb@google.com> Subject: [PATCH v4 36/36] memprofiling: Documentation From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: kent.overstreet@linux.dev, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, roman.gushchin@linux.dev, mgorman@suse.de, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, penguin-kernel@i-love.sakura.ne.jp, corbet@lwn.net, void@manifault.com, peterz@infradead.org, juri.lelli@redhat.com, catalin.marinas@arm.com, will@kernel.org, arnd@arndb.de, tglx@linutronix.de, mingo@redhat.com, dave.hansen@linux.intel.com, x86@kernel.org, peterx@redhat.com, david@redhat.com, axboe@kernel.dk, mcgrof@kernel.org, masahiroy@kernel.org, nathan@kernel.org, dennis@kernel.org, tj@kernel.org, muchun.song@linux.dev, rppt@kernel.org, paulmck@kernel.org, pasha.tatashin@soleen.com, yosryahmed@google.com, yuzhao@google.com, dhowells@redhat.com, hughd@google.com, andreyknvl@gmail.com, keescook@chromium.org, ndesaulniers@google.com, vvvvvv@google.com, gregkh@linuxfoundation.org, ebiggers@google.com, ytcoode@gmail.com, vincent.guittot@linaro.org, dietmar.eggemann@arm.com, rostedt@goodmis.org, bsegall@google.com, bristot@redhat.com, vschneid@redhat.com, cl@linux.com, penberg@kernel.org, iamjoonsoo.kim@lge.com, 42.hyeyoo@gmail.com, glider@google.com, elver@google.com, dvyukov@google.com, shakeelb@google.com, songmuchun@bytedance.com, jbaron@akamai.com, rientjes@google.com, minchan@google.com, kaleshsingh@google.com, surenb@google.com, kernel-team@android.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, iommu@lists.linux.dev, linux-arch@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, kasan-dev@googlegroups.com, cgroups@vger.kernel.org X-Rspamd-Queue-Id: AF705A001C X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 9k9h5gdpue3rytsgiogk46cg4t7r8mao X-HE-Tag: 1708544537-79461 X-HE-Meta: U2FsdGVkX1/LauYGd6TcQ8OqQWMUPj1/xK3IHxZiMIZDXTCqywKQ/vggs0A0QTcauI4lHzzpwj5QQMglEibOsk/sEOkINTrDttzcdqxBhvFCJOh9fua/P77GZ1WXYUOwqWLhT00H4JuoU76GSNsbM/KGPh32LEhjm6URsXSKFIqV1whFEK8oDrPkP3LF8584g+m3WO9hyUozNteHs8osQKFQBhb6a+xuP7Y+PPWc8D2R9Uw5UL8LfylwF9FFKMR015s+dem5u5+U56Ko8r77oUMwcUIT0EqEFePIegkMoPwjeitVvWd+r9vgJyKBo1laPezLx3Uac8X9BJn9sNpcEPV7IzjoQzTZvxAs/fj6SBsqrsP4AxovRxxBrQRi4yCqDQnUwcS3tBANRbRMS5n4o8xOnkIvyAozYZjB3T9psIRQnp8mdzlGYrYPygMaKKW4Kccrk6rj/2LrDGBllhGqpIekGdycnBahGFSe1dsVdoGM57YZHuqGTej3hZJF9BQVYk0h8PEqboUConqvt05W34b1w/NLkzqoOdN7Yn7+gMcAwovgxX7ntz6tjZRzpYbL11LYScoJ7zN50JnK6xNHFfUdvZj9KtvpTEWcZXsR8n1hhZkl5MidDhGkvKYNNsiihYgM0g8ftbYXO21rvRXRz2UVokGT8+03VsD1S+LxE20TSFxHh3jr3f9vlFZOuJfl42dt6OeCYZvNImSDGSrPqKEbOcQZD7Nle7w5uYiyAh6UUMCem/VOUY1m5SqlFFDjUQ6B1/6318cv6NyV3EJ1YRLgzp0WShaq+sPMq6YmwdAKexoXYS81j5ECKKDXkZKJjwUKLrN9066uydqKxdHZI6ZRT6j69dA7xlxX0kiWkj0kTqPOm3ENnt19VaXvVr0FNmIGay6lk0Tx3z6v82H0TBnP9VnhwV4ACZcCkpTUu5KY//KvXSqID4XE0TKO3dwOeN0m3i9UbjAz9s5LwJd l5JD1TnL 96qrpRQ5+EZxtMcszOog9Eh3sQRJ4ROesnJB5hZK2AvJYh1N/Uu3qjxdbSG+gxrdozbrU0fQ1KiExb1TkRdIg4Dtz46rGVpvbL7JhPSktRV25wVSxZO3gJPqM8kVHmDjGUzNBDfFdTu49b6p9PdqMG94vfDDNGTno0q+ItJD6yxy31Gq1LWFjtWPk0x0jllV75OTxlqK0hx7bwzed+SVH9ZTmhf+/Vfkz8GrEtdpQZE7oo85+qsUXnTwtYRtrJRM7RxRhhDenMlQxozJMtofzvvDHD7N0JA4YJnQyrnourfWGD6Wo1VATqfySkerpqXZsct0G5D3LzngU3DEgJ5bKsnj706wW2KhCtnmXiFGa+qAnSyP76viYHNVdMldZmANUCb1R43KIE3o7gKw3+2sv42FlhP0+sXEUsc1v 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: From: Kent Overstreet Provide documentation for memory allocation profiling. Signed-off-by: Kent Overstreet Signed-off-by: Suren Baghdasaryan --- Documentation/mm/allocation-profiling.rst | 86 +++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 Documentation/mm/allocation-profiling.rst diff --git a/Documentation/mm/allocation-profiling.rst b/Documentation/mm/allocation-profiling.rst new file mode 100644 index 000000000000..2bcbd9e51fe4 --- /dev/null +++ b/Documentation/mm/allocation-profiling.rst @@ -0,0 +1,86 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=========================== +MEMORY ALLOCATION PROFILING +=========================== + +Low overhead (suitable for production) accounting of all memory allocations, +tracked by file and line number. + +Usage: +kconfig options: + - CONFIG_MEM_ALLOC_PROFILING + - CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT + - CONFIG_MEM_ALLOC_PROFILING_DEBUG + adds warnings for allocations that weren't accounted because of a + missing annotation + +Boot parameter: + sysctl.vm.mem_profiling=1 + +sysctl: + /proc/sys/vm/mem_profiling + +Runtime info: + /proc/allocinfo + +Example output: + root@moria-kvm:~# sort -g /proc/allocinfo|tail|numfmt --to=iec + 2.8M 22648 fs/kernfs/dir.c:615 func:__kernfs_new_node + 3.8M 953 mm/memory.c:4214 func:alloc_anon_folio + 4.0M 1010 drivers/staging/ctagmod/ctagmod.c:20 [ctagmod] func:ctagmod_start + 4.1M 4 net/netfilter/nf_conntrack_core.c:2567 func:nf_ct_alloc_hashtable + 6.0M 1532 mm/filemap.c:1919 func:__filemap_get_folio + 8.8M 2785 kernel/fork.c:307 func:alloc_thread_stack_node + 13M 234 block/blk-mq.c:3421 func:blk_mq_alloc_rqs + 14M 3520 mm/mm_init.c:2530 func:alloc_large_system_hash + 15M 3656 mm/readahead.c:247 func:page_cache_ra_unbounded + 55M 4887 mm/slub.c:2259 func:alloc_slab_page + 122M 31168 mm/page_ext.c:270 func:alloc_page_ext +=================== +Theory of operation +=================== + +Memory allocation profiling builds off of code tagging, which is a library for +declaring static structs (that typcially describe a file and line number in +some way, hence code tagging) and then finding and operating on them at runtime +- i.e. iterating over them to print them in debugfs/procfs. + +To add accounting for an allocation call, we replace it with a macro +invocation, alloc_hooks(), that + - declares a code tag + - stashes a pointer to it in task_struct + - calls the real allocation function + - and finally, restores the task_struct alloc tag pointer to its previous value. + +This allows for alloc_hooks() calls to be nested, with the most recent one +taking effect. This is important for allocations internal to the mm/ code that +do not properly belong to the outer allocation context and should be counted +separately: for example, slab object extension vectors, or when the slab +allocates pages from the page allocator. + +Thus, proper usage requires determining which function in an allocation call +stack should be tagged. There are many helper functions that essentially wrap +e.g. kmalloc() and do a little more work, then are called in multiple places; +we'll generally want the accounting to happen in the callers of these helpers, +not in the helpers themselves. + +To fix up a given helper, for example foo(), do the following: + - switch its allocation call to the _noprof() version, e.g. kmalloc_noprof() + - rename it to foo_noprof() + - define a macro version of foo() like so: + #define foo(...) alloc_hooks(foo_noprof(__VA_ARGS__)) + +It's also possible to stash a pointer to an alloc tag in your own data structures. + +Do this when you're implementing a generic data structure that does allocations +"on behalf of" some other code - for example, the rhashtable code. This way, +instead of seeing a large line in /proc/allocinfo for rhashtable.c, we can +break it out by rhashtable type. + +To do so: + - Hook your data structure's init function, like any other allocation function + - Within your init function, use the convenience macro alloc_tag_record() to + record alloc tag in your data structure. + - Then, use the following form for your allocations: + alloc_hooks_tag(ht->your_saved_tag, kmalloc_noprof(...))