Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp466640rdb; Sat, 17 Feb 2024 18:21:49 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVGRRu8GpstYVBuJrpivfjD/N6QDaE0SGifdwOn+Iim1feEdQqZ2/KzUu63kXw809xxOs8uebz0kWab8EjspYJ/QHDp24a2UU2R5XfeUQ== X-Google-Smtp-Source: AGHT+IH1l3za9YFzDnDUby5rXJUWRRDbYxQPDch8hlzUIUR7Ka3RQGZQgj5z3j3nV6dY0CdtwK0D X-Received: by 2002:a81:71d6:0:b0:604:a75:4274 with SMTP id m205-20020a8171d6000000b006040a754274mr8229970ywc.51.1708222909484; Sat, 17 Feb 2024 18:21:49 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708222909; cv=pass; d=google.com; s=arc-20160816; b=bmKCUSie1JpVyazvZd1OGqnXLtCOIT4Z4IpgWUoYZ0+6ar5MG+Q6dtZ9T41tlBHQ6z +9w3jaKwk0MDWdaIQ/l1cROwy42TJZxyja8meo7Em0PO6keySp0mwiJ9yesVOMsyoXcy INzH48tQheiLyvo+XWy8jrzrkGPtcA9KmZJA5vLPHTySHz6vZkEn5BZn67anEsLBnhAt jvJ/Q5dIk1ItFxfoebCW0O8SSatw+MZyoX14yskLfdJYF8ZN++Gg7V8lUBg1+IsW/Rhx rK3mndTlIq8bLvFlTdyu+1Cu4FqPJXX1BTmvKaP9H8CFmtVT7mXbnoXYoCmpj/AMrJ0w IxxQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=aGwMPlFmMn7i+PsCRsmYPKBrgHwqE0wRHG6PNy5zjw0=; fh=0v7Gs5GqZBARDbZpOZxBFpcufuhLqC3sHf0crIR1zkI=; b=fBc8bILxGyX61ubsJ9xDxkH18lf/JOb/ljdHQxAPRnSW4eQFTc3rB3BIVZKT23lU0o IsDKx3PRx6qa0+aEj1kY7Gj6JydRVmTaiS7ET6VvWWPC0yxdBQr5rS3e2MZkTJSH95Na bzFZNeUmGoPhBxGTFmAuMsphKXleof5ggSmlyUvHHuG5Ql/HGUzLlBjBQPgOPC8IGZ1/ vpOz2qZVXrmSfYK3zHZLjxs34IKD+KJS3D9kBCQPTcBZnWGfMtFqXmmel+b0dGHpNRaO jaXwxPKGJcOOTFJGVzlA97Ws/OLo79x6Jtud+zDwUhO5WUliggwt9YHjT8dG6pwCMwKi Vbsg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Dy0+oSSo; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-70151-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70151-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id y12-20020ac8524c000000b0042de91145f4si3108260qtn.728.2024.02.17.18.21.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Feb 2024 18:21:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70151-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Dy0+oSSo; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-70151-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70151-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id E84881C20B98 for ; Sun, 18 Feb 2024 02:21:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 66D6F4A2D; Sun, 18 Feb 2024 02:21:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Dy0+oSSo" Received: from mail-yb1-f170.google.com (mail-yb1-f170.google.com [209.85.219.170]) (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 83AC91103 for ; Sun, 18 Feb 2024 02:21:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.170 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708222896; cv=none; b=CdHL0mfeoCF809RINTip0BX1w9/M4uVZzUrYzeMlsR7EN/OIZGhOpd1IeyP+Ijt326uXWfFev7CN7ppW+wuOk4stdD/2X1DvKgXQ5DmB+Z0gItKYm5EK8/DP3AfC2x7AwTrXpHSJFXnizzRzBxO1OADmVuu55h1xkcZB/yuFpO8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708222896; c=relaxed/simple; bh=sGLQWy2gJ22i5/11EUF6i6TeDZNPTR8RdMNYUPKuXtM=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=jufLUhn+OB+yBz0He96TwH85iRYFs48Zr4wHHc53lgT4dhVP2Kq4wg/wNq/wEEOdFPC/ZP1tZH6c08WwG+/2FoZbeSVpwUvkQZUPPAq3hu32tr0nFw6LCw2y1tEqjUcWbtY7ukOu4qQtaXBo56sKQsL1OiSqEVv2ZiDXbyYbmtY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Dy0+oSSo; arc=none smtp.client-ip=209.85.219.170 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=google.com Received: by mail-yb1-f170.google.com with SMTP id 3f1490d57ef6-dcc73148611so3718164276.3 for ; Sat, 17 Feb 2024 18:21:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708222892; x=1708827692; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=aGwMPlFmMn7i+PsCRsmYPKBrgHwqE0wRHG6PNy5zjw0=; b=Dy0+oSSo/rLVLFYRNniJ8/xLna969/++gNkbWpS6mx2DBrdgoFAbwDKo7xwr5EaXEc BxdvFTlyenLEwcJGQy5hh7gYtHG65bMckXjyD3oskfkiunAoXiTycAClPHN6Wtun9zVN JD+KyRvBXIKOLfDUgllhnEXBged/RY5qcN5ZgvnvDxdQbsTWJEjnfv6/+0yPBPKJ39ld N1sfaXsKEwSAdu7Fcsymy3YO8aLDkEiX+qeJI4YFI7QdKWay0Q6eUnpsUmOCeYLoGYX5 jVQMRCoKm1AlRWnExVzMTyMzwdyBte1Oka+Eh+wI18AhcMi56/IQzNmSZ3A6ClUI8o/+ c6yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708222892; x=1708827692; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=aGwMPlFmMn7i+PsCRsmYPKBrgHwqE0wRHG6PNy5zjw0=; b=GMynr4eKChWTtocb8wWbi1bepGXRzpGp8suucMc4nDvDTpFdL9y/C6Uni8JWCR6jlt 6E/7IYvqeZpaoLlyc/mAXjL/j6KlW7g/HUG+INHK0mrOA9BHcQDnZuIujYCgtdWgfBuC r2xA1gYVbvqdtfggLDwL+jl+l16L3qP24t7V5N6dU47JgkFOh6Z/+oW3y7q7MmZjBrm0 bUAtwDn/78SUD+FPCeFqU3IXq2SD0IZxqZlSSk/ajbKLGG6luLegzrFlGJy/hdkMl4bv p9fVGkQ7N+F1grBo6CUm9Uh3qfhMM/jZ9AdBKG0XBEynxD0VPphDA66dYQot0yBR1Xoj LAFw== X-Forwarded-Encrypted: i=1; AJvYcCVV9PfBEnIbcI5jHGKpCS34t18d55wutY5+zZj1zsInJm3OhQjXZ5rwOiymAa9o5BlRUCKDirHSsn+bMFYeahnGu0PGpHq21BHdFzVM X-Gm-Message-State: AOJu0YwpNjBvnBvCwrcKy4J5bQaRTNBUc5LrmOFE/udd7DesWn2Fn0wQ 0syvHamnNEVjn+Xu97rdW6Vc0sSh7GY2YRCrzMfe29sAXfpqskrCAddNa2b7QXZe0TJdY4xGCvZ GV30cBQpJBfOVdo9CLoyZl33Ef0F5ihZr8pEQ X-Received: by 2002:a05:6902:268a:b0:dcd:4e54:9420 with SMTP id dx10-20020a056902268a00b00dcd4e549420mr9768527ybb.5.1708222891980; Sat, 17 Feb 2024 18:21:31 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240212213922.783301-1-surenb@google.com> <20240212213922.783301-14-surenb@google.com> In-Reply-To: From: Suren Baghdasaryan Date: Sun, 18 Feb 2024 02:21:18 +0000 Message-ID: Subject: Re: [PATCH v3 13/35] lib: add allocation tagging support for memory allocation profiling To: Vlastimil Babka Cc: akpm@linux-foundation.org, kent.overstreet@linux.dev, mhocko@suse.com, hannes@cmpxchg.org, roman.gushchin@linux.dev, mgorman@suse.de, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, 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, 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" Content-Transfer-Encoding: quoted-printable On Fri, Feb 16, 2024 at 8:57=E2=80=AFAM Vlastimil Babka wr= ote: > > On 2/12/24 22:38, Suren Baghdasaryan wrote: > > Introduce CONFIG_MEM_ALLOC_PROFILING which provides definitions to easi= ly > > instrument memory allocators. It registers an "alloc_tags" codetag type > > with /proc/allocinfo interface to output allocation tag information whe= n > > the feature is enabled. > > CONFIG_MEM_ALLOC_PROFILING_DEBUG is provided for debugging the memory > > allocation profiling instrumentation. > > Memory allocation profiling can be enabled or disabled at runtime using > > /proc/sys/vm/mem_profiling sysctl when CONFIG_MEM_ALLOC_PROFILING_DEBUG= =3Dn. > > CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT enables memory allocation > > profiling by default. > > > > Signed-off-by: Suren Baghdasaryan > > Co-developed-by: Kent Overstreet > > Signed-off-by: Kent Overstreet > > --- > > Documentation/admin-guide/sysctl/vm.rst | 16 +++ > > Documentation/filesystems/proc.rst | 28 +++++ > > include/asm-generic/codetag.lds.h | 14 +++ > > include/asm-generic/vmlinux.lds.h | 3 + > > include/linux/alloc_tag.h | 133 ++++++++++++++++++++ > > include/linux/sched.h | 24 ++++ > > lib/Kconfig.debug | 25 ++++ > > lib/Makefile | 2 + > > lib/alloc_tag.c | 158 ++++++++++++++++++++++++ > > scripts/module.lds.S | 7 ++ > > 10 files changed, 410 insertions(+) > > create mode 100644 include/asm-generic/codetag.lds.h > > create mode 100644 include/linux/alloc_tag.h > > create mode 100644 lib/alloc_tag.c > > > > diff --git a/Documentation/admin-guide/sysctl/vm.rst b/Documentation/ad= min-guide/sysctl/vm.rst > > index c59889de122b..a214719492ea 100644 > > --- a/Documentation/admin-guide/sysctl/vm.rst > > +++ b/Documentation/admin-guide/sysctl/vm.rst > > @@ -43,6 +43,7 @@ Currently, these files are in /proc/sys/vm: > > - legacy_va_layout > > - lowmem_reserve_ratio > > - max_map_count > > +- mem_profiling (only if CONFIG_MEM_ALLOC_PROFILING=3Dy) > > - memory_failure_early_kill > > - memory_failure_recovery > > - min_free_kbytes > > @@ -425,6 +426,21 @@ e.g., up to one or two maps per allocation. > > The default value is 65530. > > > > > > +mem_profiling > > +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > > + > > +Enable memory profiling (when CONFIG_MEM_ALLOC_PROFILING=3Dy) > > + > > +1: Enable memory profiling. > > + > > +0: Disabld memory profiling. > > Disable Ack. > > ... > > > +allocinfo > > +~~~~~~~ > > + > > +Provides information about memory allocations at all locations in the = code > > +base. Each allocation in the code is identified by its source file, li= ne > > +number, module and the function calling the allocation. The number of = bytes > > +allocated at each location is reported. > > See, it even says "number of bytes" :) Yes, we are changing the output to bytes. > > > + > > +Example output. > > + > > +:: > > + > > + > cat /proc/allocinfo > > + > > + 153MiB mm/slub.c:1826 module:slub func:alloc_slab_page > > Is "module" meant in the usual kernel module sense? In that case IIRC is > more common to annotate things e.g. [xfs] in case it's really a module, a= nd > nothing if it's built it, such as slub. Is that "slub" simply derived fro= m > "mm/slub.c"? Then it's just redundant? Sounds good. The new example would look like this: > sort -rn /proc/allocinfo 127664128 31168 mm/page_ext.c:270 func:alloc_page_ext 56373248 4737 mm/slub.c:2259 func:alloc_slab_page 14880768 3633 mm/readahead.c:247 func:page_cache_ra_unbounded 14417920 3520 mm/mm_init.c:2530 func:alloc_large_system_hash 13377536 234 block/blk-mq.c:3421 func:blk_mq_alloc_rqs 11718656 2861 mm/filemap.c:1919 func:__filemap_get_folio 9192960 2800 kernel/fork.c:307 func:alloc_thread_stack_node 4206592 4 net/netfilter/nf_conntrack_core.c:2567 func:nf_ct_alloc_hashtable 4136960 1010 drivers/staging/ctagmod/ctagmod.c:20 [ctagmod] func:ctagmod_start 3940352 962 mm/memory.c:4214 func:alloc_anon_folio 2894464 22613 fs/kernfs/dir.c:615 func:__kernfs_new_node ... Note that [ctagmod] is the only allocation from a module in this example. > > > + 6.08MiB mm/slab_common.c:950 module:slab_common func:_kmalloc= _order > > + 5.09MiB mm/memcontrol.c:2814 module:memcontrol func:alloc_sla= b_obj_exts > > + 4.54MiB mm/page_alloc.c:5777 module:page_alloc func:alloc_pag= es_exact > > + 1.32MiB include/asm-generic/pgalloc.h:63 module:pgtable func:= __pte_alloc_one > > + 1.16MiB fs/xfs/xfs_log_priv.h:700 module:xfs func:xlog_kvmall= oc > > + 1.00MiB mm/swap_cgroup.c:48 module:swap_cgroup func:swap_cgro= up_prepare > > + 734KiB fs/xfs/kmem.c:20 module:xfs func:kmem_alloc > > + 640KiB kernel/rcu/tree.c:3184 module:tree func:fill_page_cac= he_func > > + 640KiB drivers/char/virtio_console.c:452 module:virtio_conso= le func:alloc_buf > > + ... > > + > > + > > meminfo > > ... > > > diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug > > index 0be2d00c3696..78d258ca508f 100644 > > --- a/lib/Kconfig.debug > > +++ b/lib/Kconfig.debug > > @@ -972,6 +972,31 @@ config CODE_TAGGING > > bool > > select KALLSYMS > > > > +config MEM_ALLOC_PROFILING > > + bool "Enable memory allocation profiling" > > + default n > > + depends on PROC_FS > > + depends on !DEBUG_FORCE_WEAK_PER_CPU > > + select CODE_TAGGING > > + help > > + Track allocation source code and record total allocation size > > + initiated at that code location. The mechanism can be used to t= rack > > + memory leaks with a low performance and memory impact. > > + > > +config MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT > > + bool "Enable memory allocation profiling by default" > > + default y > > I'd go with default n as that I'd select for a general distro. Well, we have MEM_ALLOC_PROFILING=3Dn by default, so if it was switched on manually, that is a strong sign that the user wants it enabled IMO. So, enabling this switch by default seems logical to me. If a distro wants to have the feature compiled in but disabled by default then this is perfectly doable, just need to set both options appropriately. Does my logic make sense? > > > + depends on MEM_ALLOC_PROFILING > > + > > +config MEM_ALLOC_PROFILING_DEBUG > > + bool "Memory allocation profiler debugging" > > + default n > > + depends on MEM_ALLOC_PROFILING > > + select MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT > > + help > > + Adds warnings with helpful error messages for memory allocation > > + profiling. > > + >