From patchwork Wed Mar 6 18:24:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13584447 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 EE301C54E41 for ; Wed, 6 Mar 2024 18:26:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 497BC6B00C9; Wed, 6 Mar 2024 13:26:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3F7EA6B00CA; Wed, 6 Mar 2024 13:26:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 24C046B00CB; Wed, 6 Mar 2024 13:26:07 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 113AD6B00C9 for ; Wed, 6 Mar 2024 13:26:07 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D7CA3120C7B for ; Wed, 6 Mar 2024 18:26:06 +0000 (UTC) X-FDA: 81867443532.30.0C601D5 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf07.hostedemail.com (Postfix) with ESMTP id 0C0F04000F for ; Wed, 6 Mar 2024 18:26:04 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=fmGvDDlk; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3O7XoZQYKCH4uwtgpdiqqing.eqonkpwz-oomxcem.qti@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3O7XoZQYKCH4uwtgpdiqqing.eqonkpwz-oomxcem.qti@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709749565; 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=yGhey761h+yk0goaaYaIkNpiVVUjQ3rocN1HojLD7sY=; b=IOUPZzzy+WIn9zruVx6MIqqA5v02qLK8SCg7wyUPEUBe78vLhUZYzxh4gni8WV8vxL1uvl 3hwhK6re1kMEu/dhloI3069976sX+qw+EiLQn1xSw5/1Dfwfmffj9YrD3aYHrkAzYiD6PO HCU8aHORFv/b3cD0OlK/fBqMT9D/CWc= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=fmGvDDlk; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3O7XoZQYKCH4uwtgpdiqqing.eqonkpwz-oomxcem.qti@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3O7XoZQYKCH4uwtgpdiqqing.eqonkpwz-oomxcem.qti@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709749565; a=rsa-sha256; cv=none; b=r6Oq+a1NW7kc/GzuXy8ySZH9PED/Pta/H4E2frsKkUnv3F9v9wJKA+5sdvDyYMBo+/iexO YDXP6BdOJxiAwRh3J216DM91lGFVPECxkrzfeTY4L3Be1BnGbx3VOlwQSAnsKpbLVelPf+ 5Dk5fjO3wbg9s2sgXJ3JcQ+UJiYUXwM= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dcc05887ee9so10100151276.1 for ; Wed, 06 Mar 2024 10:26:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709749564; x=1710354364; 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=yGhey761h+yk0goaaYaIkNpiVVUjQ3rocN1HojLD7sY=; b=fmGvDDlkqKgqBfinoXO7ZcIZURnXuv+OgG1IZtbaMlrYOfVQuagjOS2mivOqKJektE yAAtpJs4uMJS5YlSV8TdECedusn6XLB6QyiIEt5c5S9GaT2LFdJPzjj8Nng7DhZ+f8OL dLPRaEiI8Ay1iKEj8+5BBvmn2UmaYcd+0XRCnAtjkhDZj92R0WJ02RPVMuh6w7azkZgx RDWZhtJ2XX/BKOby3X0gporbVV+duXIKQvRmBuOhSj/9lEFmo4xC071xHjfasSqrvO5e gocGInKUp3Qx9j3QDG+5iBC87FffnIn2AQCtELrJEYZRixGLTZ7szvH9GY398fh+Jd7d fS5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709749564; x=1710354364; 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=yGhey761h+yk0goaaYaIkNpiVVUjQ3rocN1HojLD7sY=; b=OdFKkvLtGpG6xwxBPcEpwVtg5WuHa/CYzS9OY/L2rQEW2Jq5T0wsuaW+K47b8+BNYY bvYRjRUqfeZ8zyqxYDsTmc87s+DsMj95ZIygUe+EWnAPMiBOawc84hxWlAiilrM0euXT OEFdwjEwjmC0EF29fhno3T6RG1ni0q1NrlysS5iufJhsASpMvzxXryaw+Huwwlt5mFuO EaYBpwBUAV4sq3LGRiy3NxhpUvwUl+HVabrFabEoRrHyLOyO8lrbYJwViPITOeWxWaEh rUjC+khqov/ZfLyn9XulDOQI7mgLcLB3HBYQhj9VeGRakX9sHXu4FRYoPXu3BI0wIETI 61Gw== X-Forwarded-Encrypted: i=1; AJvYcCUbD/n6jJ6LNMrVYDG8djKgXSyyWYyn+l7mnpCls7bQCpusV+9fNOQ9EuBQPRJv6WRgduxvaRiLxDEW4GgQWqHSccQ= X-Gm-Message-State: AOJu0YyVFA771Qj58qRLGUunb5+wLnKINRWefA0LmhRzF7uvtKpS7dRy s0iV7yNMGAxpxtewJGzbBhfWtIlBV/CGeu3c/8UkewjYbhuGx4pG8kt51BNxjqnf6qcJl5wn/G4 mKg== X-Google-Smtp-Source: AGHT+IGSwvnidbX0/rOv0INS3he6frJgJB+IsYvN52GAPjMVdVuwPoKCHiyeHTzcN2rL9OOwKUgSVjL4/4I= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:85f0:e3db:db05:85e2]) (user=surenb job=sendgmr) by 2002:a25:2fc1:0:b0:dc6:ebd4:cca2 with SMTP id v184-20020a252fc1000000b00dc6ebd4cca2mr528159ybv.11.1709749563843; Wed, 06 Mar 2024 10:26:03 -0800 (PST) Date: Wed, 6 Mar 2024 10:24:35 -0800 In-Reply-To: <20240306182440.2003814-1-surenb@google.com> Mime-Version: 1.0 References: <20240306182440.2003814-1-surenb@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240306182440.2003814-38-surenb@google.com> Subject: [PATCH v5 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, shakeelb@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-Rspam-User: X-Stat-Signature: d1s3k5c6yupe9o84eff51xitsqgg81jh X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 0C0F04000F X-HE-Tag: 1709749564-722991 X-HE-Meta: U2FsdGVkX1+eXOylHYgEEkcj94qRAl8m7VArospfPluBC65w72B8lYLKSfQ1UcZc8hE/cKWic0lrDg5dbBvLInsOL5IXiFyy6Z5iX0GMEWE4lxnotCjSmY5mUDsEpTPaox73SQsgSJAe9L4x7UB6PLNazEoO6ozFoj2CJMDECYjsWgyt3hrxerLcyM55reUDFy0LL7P4o/sFtxopnltaVav01VBdG6JtVVu14BW69jLmhWCa8N+NLI1lqjUjyw8oakH5ji0/MwIKg4kKPqQhHvb1dFWI1BbGmWglXtLbCH754rD96JxGwBNT+XL6m7bSkazHcsHbnusid91ElbV1qNk47UviSYd4haob13lFpD56xDtDKKohkTnVsraRmQAN8dlN4wb3yM/atmod18Ig1tVuKXHPAqUDYDwWOrnaHlXcrEO80mjjVnczS35up2RpMjzPnMgDidvAztTE7gHpb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D6166EQ71ZEKWBWEppFfGIxQfTv3fiTegX7m2Gnk4avj3cmKs/npoMVDapcDdUsbSOTJShvV9eOcrYad7xyMId2cQPhjXQRkPxSrP2pWpDlIRTJ13ONCGE8poKpJQ== 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 | 91 +++++++++++++++++++++++ 1 file changed, 91 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..8a862c7d3aab --- /dev/null +++ b/Documentation/mm/allocation-profiling.rst @@ -0,0 +1,91 @@ +.. 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 overheads 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 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(...))