Received: by 2002:ab2:3319:0:b0:1ef:7a0f:c32d with SMTP id i25csp62579lqc; Thu, 7 Mar 2024 10:18:26 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWwikSRQX6t1skKrSXjBHu4TFXrnC1+56H5+X5yMH4EX5MvQatNLhAhWzhu4dqHICLIuPfNWIHSKbEjKMcuq+MbKczCFEyx21QpG4ctNA== X-Google-Smtp-Source: AGHT+IFKQFG/0dm8NCroKckF2SObE8T3aOwS6c/V+vPpiS7EQoY77n7gp50ptRUY4AhX5xUATvor X-Received: by 2002:a50:8718:0:b0:567:824:e36c with SMTP id i24-20020a508718000000b005670824e36cmr444979edb.14.1709835506005; Thu, 07 Mar 2024 10:18:26 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709835505; cv=pass; d=google.com; s=arc-20160816; b=iywlbfd19+mqid+zpjPVAMR2bXGDHbiW7uYLuzS5byaiVbzqh4QieqlMbggcYKfvYw nZiBsLHHpQkQ0jg1L92gYQKcgAWVNzvtcD3L0Yun52NSOSD7RCEwaCXrRy2kFWd6ZzsG JHVB8+svE5pTgCSEYFhixGmHdXWM418GlWtsl5zCVbXH16sQ2aTrDyY9kmKJeG3cBglJ LycyYIW/8PnFylbTXc0zgHyKG0c+TqJZpR+pHCYC1mtxHSkO50AQpeFgSnIVmpBV2l6q yUGWv+5/f91+kIJOaYK2oIR1JP5s4XsHsDMZK+mTk+vxsiNnOisTXLITsJzK7v2Vuevq jJtA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:dkim-signature:date; bh=iHaIxY/+BiJn46m1UxNdZN63kb5NNOFAVQARDvBki5g=; fh=FsnSm+GOq/wVsP+lve324Rei+JublQJ0B26xncKEPoM=; b=AqhsdMoWvY8KdofTZhj24yXH+Z9LyFfptY00UdnpK823yhj1TPxTOxsnqHiT96yafJ oRZ1V2iGfF1iULYArScv/zYt7vxa4daiaZFgQDqLhX5uw8SrKorVtxB/3L0JxvPwKp+N 0VRw47x4R3ipf5J7Bqc+mJFyG+/lRCx/3bZOZgoh/SsesnpcVj60gnLVFrwwc9kYmImr LhqWtuy41VwH+27GqVvBn20LBvI80sb1wR3+lHGewmQr2FI9jJCrtuYU2Yd7P+MG1OzY ZAU12euBcAQYdBg8/NHtBOYAr/+BVepJ0wxBdHPASQdqbYmOks6Y4wnUjqHmmI/EMWk8 OhRw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=H5aOzJ+M; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-96006-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-96006-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id f12-20020a056402354c00b005668059aa3fsi7031341edd.15.2024.03.07.10.18.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Mar 2024 10:18:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-96006-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=H5aOzJ+M; arc=pass (i=1 spf=pass spfdomain=linux.dev dkim=pass dkdomain=linux.dev dmarc=pass fromdomain=linux.dev); spf=pass (google.com: domain of linux-kernel+bounces-96006-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-96006-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 90D1C1F22FBA for ; Thu, 7 Mar 2024 18:18:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5A0D7133420; Thu, 7 Mar 2024 18:18:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="H5aOzJ+M" Received: from out-189.mta0.migadu.com (out-189.mta0.migadu.com [91.218.175.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B89EA131E31 for ; Thu, 7 Mar 2024 18:18:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.189 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709835490; cv=none; b=K9pDYrq1Q95RPybA2EJdeqFumrpS3QXansoaNoeloFPJzrqqDruAf8sc2p33W22KI952QR08tZXx3qxOlOPmZAsSwf/hEoxLJB5tWua3d17jyxZiPrXgkHPdI9X0oXhcj5lrs5MeE7borrGwsaGhiCcHRMCpo1Fm7oOH/4Pfick= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709835490; c=relaxed/simple; bh=Gm1+12tqQKv7crZi1oC909n2tXEtrT/LTbYSqj5bPhw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Q3nlC2QFv+5fdvQWFHGZhhvof6ZfP2sW1InG81+bg3u7bWrmJNxEtj8UwKFuMQCXOhit9HVhf+qN9cPdX+v7VM1gq2FLnm1M/wTgrP2FMEEehoF7YscRq7lWBqQfDXzOAq19VslG622gY0QpCMPqG2BZoydtyX8SRM3SApcgIaI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=H5aOzJ+M; arc=none smtp.client-ip=91.218.175.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Date: Thu, 7 Mar 2024 13:17:48 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1709835485; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=iHaIxY/+BiJn46m1UxNdZN63kb5NNOFAVQARDvBki5g=; b=H5aOzJ+MeszkUjW7Gdq8GIb9xZvvYFafE54Qajm1X53jP+9kSZaHykmws15lGZbxbVC4sP xB+aj3bB+XZoL9vVS6ke4qTd3NSX0xuXSFczxjIN4xYv/lKG+5cGIuGnJ1kVWq3Q8k+XsG i3YlKbhrl9zIpdvnTlhFtaDJQqe9fLg= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Kent Overstreet To: Randy Dunlap Cc: Suren Baghdasaryan , akpm@linux-foundation.org, 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, 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 Subject: Re: [PATCH v5 37/37] memprofiling: Documentation Message-ID: References: <20240306182440.2003814-1-surenb@google.com> <20240306182440.2003814-38-surenb@google.com> <10a95079-86e4-41bf-8e82-e387936c437d@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <10a95079-86e4-41bf-8e82-e387936c437d@infradead.org> X-Migadu-Flow: FLOW_OUT On Wed, Mar 06, 2024 at 07:18:57PM -0800, Randy Dunlap wrote: > Hi, > This includes some editing suggestions and some doc build fixes. > > > On 3/6/24 10:24, Suren Baghdasaryan wrote: > > 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 > > overhead is > > > + 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 > > typically > > > +some way, hence code tagging) and then finding and operating on them at runtime > > at runtime, > > > +- i.e. iterating over them to print them in debugfs/procfs. > > i.e., iterating i.e. latin id est, that is: grammatically my version is fine > > > + > > +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 > > maybe end the line above with a '.' like the following line. > > > + - 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(...)) > > > Finally, there are a number of documentation build warnings in this patch. > I'm no ReST expert, but the attached patch fixes them for me. > > -- > #Randy