Received: by 2002:ab2:788f:0:b0:1ee:8f2e:70ae with SMTP id b15csp83140lqi; Wed, 6 Mar 2024 10:37:33 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVgq6D+37HgrrlEBceKmCvNPPkeZCn89DJ2gVXLnnpX55+YX/R1CMmsx/IjP7lN7ugb2CLnG9x+skf+gSveP6iznNXoNEyxOFsQ3a/MUg== X-Google-Smtp-Source: AGHT+IG62IycHFjenlwoa91OZQFvBBqgfpnJ2VvKclzIUGQTSDi4wVc3ZJwAMchLuAsxa4/nsHHu X-Received: by 2002:a05:6a20:12d5:b0:1a1:4d85:5838 with SMTP id v21-20020a056a2012d500b001a14d855838mr6138772pzg.22.1709750253196; Wed, 06 Mar 2024 10:37:33 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709750253; cv=pass; d=google.com; s=arc-20160816; b=Jp/OUHMAqw/+w9xkk46mwt69jfr3aS25vlD+MwqyEDhouswAtEwKsCkB/kg2+zxmKy 4VxzwRZNTURm476k4kALTiYeNK2I5OD7Mxjv4Eg7jVnGqeNiVd0PTSOi+JBKr97NZtWB JKh2oOwHrg58c3CRbrTih3rurWFn7Fy0SIT0CXWgMJbJvt7Jvgn/pE23u8FGzprXqgMw WddpyEP1zXL29o0A8AVjlmps+6+vshgDLdU/Zr6P0+7awgWdmiQnevpkvZj4etEv8rmS iw2mo/sol0OnZV2HCLH86GuRFT1CXHWcD6TGixfErBkKad7j0w/r0T4p3WZ26zl8gJR3 CeyQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=yGhey761h+yk0goaaYaIkNpiVVUjQ3rocN1HojLD7sY=; fh=LOiTKsXDvlxHwZX21PqpaLG6nZGmywrk8e8ZzlQczfI=; b=c/eMvDVHiFEzv8I1Rd6JYlDsidQ5bI3w9hbJLvTgbY2hYdPcM/yuAbGSpV+sM3etnZ xaRqfyFZ9MHMeak/VjohpnsJ56aFtp9/+n61CDjhoQzea23NObJVutr4S4wIKVHMR1Dn LdAoOMqvHzySj7dpGDN+hzvEI3uxtLW4GW5u/hZky9xaDkJFFTwfq9yxv1oZ2o9DK8/a XhY1p5s2wTMEkZab1qKrZ2KJKuhEhyTCyKjJIO9VcPqtewxJgBI4H4g6CpedhAlSHbxF VSF+IIUWPbtQ3A5CxwNOLlAeNS0gqvXCE2IJP57Yv9hDT0xswVTT9ST1CHI6vOLVpYDS xuTg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="D/UMJVvP"; arc=pass (i=1 spf=pass spfdomain=flex--surenb.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-94429-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94429-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id w19-20020aa78593000000b006e5078ce5f5si11946177pfn.35.2024.03.06.10.37.32 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 10:37:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-94429-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="D/UMJVvP"; arc=pass (i=1 spf=pass spfdomain=flex--surenb.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-94429-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94429-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 5F981B22FDE for ; Wed, 6 Mar 2024 18:37:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AA284142635; Wed, 6 Mar 2024 18:26:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="D/UMJVvP" Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D19D1152DEE for ; Wed, 6 Mar 2024 18:26:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709749568; cv=none; b=mt0EqZiOWm3ffFUbrUIngrZHfML8vnonEe4728VaHohsAlCOUTwomLGmLGO3vDvmf0ozpNOl4PPlTw3geovlvBemKqIgZiLmw51Tkdr9P3EWXr6iuUETLq1dHQiuedheghWMTtKfttuiCkKuodDm1x0ezt4AcsdXzxoKn19MNxo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709749568; c=relaxed/simple; bh=WZ9Wfhph9SqMvpwjpOilQz7uq5chx2Sd29yINS9foKA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Mkp/IDqtm+PpN7YcncxIzXM2z2bW942UerJcTtWjmztmPN87eZscuDeUNGFVT9qRt4JeXpAISJx5ulDFUUAGCDulSRquGKGxG/dSddB9BcqEh5k2hWyYKOWH7ieYu7Sngp4XiyVzJAFVy+CdRyYgWN5ft4v95coIHG6F1UGN6ak= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=D/UMJVvP; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-dc6b267bf11so8287226276.2 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=vger.kernel.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=D/UMJVvP9HSPPg/+UMFUn2uZmSu2zF9hVOmrJ8EDa6yA8umDSMaFwniCa11IGfob6+ pPTYXHkvmUQZ7h5CbcIAv6z5Jc6pNpACFiOM0RanoRglnxOK2Us+/8PdMVd7VNPGpn5R gEWA/O7R6ylKAL9OWCKGt56aPulT1OItTwVIqjTMjP9+CPPayXSqheN4JJYkAlh+YWpf YWjsqWfjx5qQ3iirKrDcIsg76y44ErK7ViJMUMtFnB9PCUMx8lXIA1e+0+sCnVZW+Pdv ZLCNBRifYQXpn7hbpYkEIZJjuiSaHX54snkLPAT8zxkbs7l/Bvf1uPpdw74AvHsWkS3N vK9Q== 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=v4kaXZni+lqaEqpEu+aKKN64YfBn5LV9/ZY2GgAcbACpe3mMSmAN0FThn5K4daR51u mebK8W18s7nN5Szq0JTx5maxzfEIGHqf+cOmkma6ddIqdTMZFCSMkuRCDwSKr+/hXSUj Yv0MAjTkaTzyWo6Zridg+ABfQ2T7MgfeQOmZXXrTtSUvqm+GdfQ1DL6Rfd40sF5T903q 7RW4DdFKul0LuPDn2jQw8YV2N/obcXgBzISD71NUB1jQMJcVR0rhKMrKX4nl3viJbXPE PqBTae3SerMFF3Vp6W1g8HYlB42HGZMscExiVi5qjX44TQYzNNAaJU4tQEyJSBrnOCqm Eexg== X-Forwarded-Encrypted: i=1; AJvYcCX4zpZbw7T2XCCaq48U4/IK5W+/tcUb4SAkroUH6UVVHpRcynrxF5pZqBoSq9OpH07l58/IDoptF9Gbtq4U6u1rrKzUpD28M5OVGY4j X-Gm-Message-State: AOJu0YzInNgXISe0Eqt3Uc/GwBPNFPYAxydq+I6mV8NRmjiIjWIMSdRY LUI/iCcSWdY8/TCMwpqKwqIA84FayjXLnZc6CQtT8FYJ4Q89UHT7VKzDzv5t9nNvaqfVHoZs1sr XTg== 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 Content-Type: text/plain; charset="UTF-8" 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(...)) -- 2.44.0.278.ge034bb2e1d-goog