From patchwork Thu Mar 21 16:36:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13599193 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 141F9C6FD1F for ; Thu, 21 Mar 2024 16:39:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 28D066B00CB; Thu, 21 Mar 2024 12:38:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 216EE6B00CD; Thu, 21 Mar 2024 12:38:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F0D9D6B00CE; Thu, 21 Mar 2024 12:38:32 -0400 (EDT) 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 DACED6B00CB for ; Thu, 21 Mar 2024 12:38:32 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id AEB82C0A2A for ; Thu, 21 Mar 2024 16:38:32 +0000 (UTC) X-FDA: 81921604464.03.81FC081 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf19.hostedemail.com (Postfix) with ESMTP id 188FF1A000C for ; Thu, 21 Mar 2024 16:38:30 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=vuYeiDfv; spf=pass (imf19.hostedemail.com: domain of 3hmL8ZQYKCHMjliVeSXffXcV.TfdcZelo-ddbmRTb.fiX@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3hmL8ZQYKCHMjliVeSXffXcV.TfdcZelo-ddbmRTb.fiX@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=1711039111; 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=zMtv/wiplx2DxF9seTFx3IcU+R2+PFSDAr6m0+A1ntI=; b=65D0MRstYkcL+gb1YwILciLOtpEQFhVOCqYdeqDVKZH1NEp13v5Ftz3kIfOZH7c7jTRxPO IzsI0ogxT49FO0bzV3ZPCB5x2e76Io7lqRmPZ3b8JJC2m0Cmfn2J2JUI1g/BR15CnfDsrQ +itG5LJDQs5nfBbSgkc8xtemZ0GIr4s= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711039111; a=rsa-sha256; cv=none; b=lFDpbBm84cE1xvoiNfmRQ34K9SchiA9EV8xGOgLL3wgutV50kaDsMPxyxGoYppB6JDxcSi GDkTNwelWPphUQdwKCqsOcW7KAN7wtxHTNHXOyCx7Y9kudW6FqrHnvhsepUbU5XNVLUy72 b2DFsgdkABbMQ6AjIzTfRp+Kn9Zinyk= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=vuYeiDfv; spf=pass (imf19.hostedemail.com: domain of 3hmL8ZQYKCHMjliVeSXffXcV.TfdcZelo-ddbmRTb.fiX@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3hmL8ZQYKCHMjliVeSXffXcV.TfdcZelo-ddbmRTb.fiX@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dbf618042daso1830976276.0 for ; Thu, 21 Mar 2024 09:38:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711039110; x=1711643910; 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=zMtv/wiplx2DxF9seTFx3IcU+R2+PFSDAr6m0+A1ntI=; b=vuYeiDfvy3pS+uqkDshpifo1OCZ2vJZNIsg0rCKL0+43nZoK+gAVoOlu73KoF6Mosv E/+14pJR9p+JzEygW5VJg4fHNfUJjhEIpdDg62EUyWJYmOKcb/BiixsVwfZGOC2d7nWF NEmJAEBi4gjpjNjVhdlsPQzNbfyIVKGck37+uprxY3BPu/YCxFpGrT2Afc9e0Pvpk8/2 YMfQ1dlGQgYy2E92BRc5VW5NdaCgyszMt4Sla5PbIBq+8+XUBJZgHBeb2hr8/eCnW8DO +q26xkMnxpLxGFbkqiuiVfjOK0IUjL5m8MLihtuOhdOaKeUrjK9VFcbK3y62NmOpaHca /2RQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711039110; x=1711643910; 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=zMtv/wiplx2DxF9seTFx3IcU+R2+PFSDAr6m0+A1ntI=; b=PQsZkPOWwX3NaCKat332a3I8YGtWh3NzveS013Gc5ukbpW3UHjUhcIZs4XpJR3EuHE F1hn+VUH3y9NO3rwpizs9F3etwNJa9ZZq9SjhIvc7DHy6v2IjCR5CWQaQgc0+yWJWpWS KiBmuMF9SYdBgNWRp4nk1dVK6YqelEf1CMhd8w2BKDLKvNF7IVDvEvYaDTPLQnmi2+KD 7qm1L/6TkVyFJkaZnJg339R+xXfrIvTvIn3i9s+1jfqnr9FSWPMIyJ7urAnDNWLV9CdD BMujJxf7ZSEz9ImzvyL0qLNt53svR8D5JmmkjzV3jkX2akqtk5XjdSZToc1m8FvkFWSK l2Vg== X-Forwarded-Encrypted: i=1; AJvYcCXUtsrNksvJ641MLS3XpztcmcTGbx0V2em0Doyg8Qwh3tosIMFNhR8uEbvxgGE/uFeB0QIXzl9vKSHH7v67vccFM8I= X-Gm-Message-State: AOJu0YxNNge7MNAnFoBGBfrtK6YfykkO54XknbpW8KJLLIpDy4V5yGH1 //h2Kwkuksy3glwxG7UpZ7N156lMLqTZjGcjb1y0IcYJ+aoIxn2GcvqTGVEEbKxvJ6Jm6PLp3bs P0A== X-Google-Smtp-Source: AGHT+IHubMkzttSDdMzbBJ1mYbEtkJMz1sywK2vk2BxZuZG66cHkzs6E2Eyy/Ht6uVFkEMl2Xl2C/1q/RI8= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:a489:6433:be5d:e639]) (user=surenb job=sendgmr) by 2002:a05:6902:240e:b0:dc2:5273:53f9 with SMTP id dr14-20020a056902240e00b00dc2527353f9mr1211362ybb.1.1711039110014; Thu, 21 Mar 2024 09:38:30 -0700 (PDT) Date: Thu, 21 Mar 2024 09:36:59 -0700 In-Reply-To: <20240321163705.3067592-1-surenb@google.com> Mime-Version: 1.0 References: <20240321163705.3067592-1-surenb@google.com> X-Mailer: git-send-email 2.44.0.291.gc1ea87d7ee-goog Message-ID: <20240321163705.3067592-38-surenb@google.com> Subject: [PATCH v6 37/37] 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, jhubbard@nvidia.com, 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, songmuchun@bytedance.com, jbaron@akamai.com, aliceryhl@google.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: 188FF1A000C X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: ck6ts8iih93h9tm6uqomp1pf58wrpdfp X-HE-Tag: 1711039110-949802 X-HE-Meta: U2FsdGVkX1+UTf0lxmgTADEk/mdV0ayjLWCl7E8L+ZvMarO0D9XPx3wF5Je0cnVf9Kfj5qcd+mc4/eYAnIaPnjzvwrgRRTL3owL1u6lisMlOoVlDdr/6U8/T0/hvXP6dwjRf0SqV3hJlIZimn1RVfU99NGjF/wJOHZHycUPtbUy4JSPEh5SAvMXk7j4OW5ByP/FzDBnvjEYlx5j7r7XPdypaj0jXHYQyE6bjnc9dhMWCOBsgq/ukyKRmWM71t29k3WCRxUEk7jzuqhK9ybAsX6Kq+IlK+jZ2+L4e/c2xmY7Vzmntpu3svBj4jZpe5swXKUa7HZ0aA9hudz/TXqSKnDNzzQhRRjayOCgnYfw3MlWVqPE9dX69Bu+2mTZgALr9maIid4dCwxj0I3RQf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ence: 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 | 100 ++++++++++++++++++++++ Documentation/mm/index.rst | 1 + 2 files changed, 101 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..d3b733b41ae6 --- /dev/null +++ b/Documentation/mm/allocation-profiling.rst @@ -0,0 +1,100 @@ +.. 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=0|1|never + + When set to "never", memory allocation profiling overhead is minimized and it + cannot be enabled at runtime (sysctl becomes read-only). + When CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT=y, default value is "1". + When CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT=n, default value is "never". + +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 typically 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(...)) diff --git a/Documentation/mm/index.rst b/Documentation/mm/index.rst index 31d2ac306438..48b9b559ca7b 100644 --- a/Documentation/mm/index.rst +++ b/Documentation/mm/index.rst @@ -26,6 +26,7 @@ see the :doc:`admin guide <../admin-guide/mm/index>`. page_cache shmfs oom + allocation-profiling Legacy Documentation ====================