Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp598848lqp; Thu, 21 Mar 2024 09:52:18 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV8IGhtQIiTMyBRCpitX2HQNNqs9sG3qdHQ7QT06dMMQ1mJEYnezdlcpGcFzdhA8Tt/Ma5BXkOUicvpZvA7nAeY/vKw8c5JoUn9bKJW8A== X-Google-Smtp-Source: AGHT+IEDuSl4WNToYg92U1m853K/VtY0plxIM3KCTXzImfLnFr9gluHLxk1BDt97Nl75DQeIHaRE X-Received: by 2002:a17:906:24d9:b0:a46:fb2d:3980 with SMTP id f25-20020a17090624d900b00a46fb2d3980mr4085441ejb.19.1711039938434; Thu, 21 Mar 2024 09:52:18 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711039938; cv=pass; d=google.com; s=arc-20160816; b=zBLjPcuTiBn27vrMLUL2S76zRN5ABUJ0dJjS1IXJYA32VmA1z3nFPopWDJ5ndCCKLq yBKyG5VJ1lNpr/87h1CTvlz5ZbnGomQETxsuvKZHUmflyF9rE9SjzxglZOQS9hzh2RTY YYJgQPpNPRhKd9RZvBMMWBGPZXaLla+DijhY+zCUXcYq8ts8LfsbVXF2g8CUltewd0pQ QCNbF1GPxODBKITXQ/VWuX7DwzcPFbYtjnkE7OW4zPaLV0jDqnEb1dM5jL+uaIAcHCZc fTh2cEMIqXFZDg1gwPyIwTREvN1TMUtGAhsAgI/2qOjjJtJTDAX/PRucCsfQ4mHGFz6Y QLTA== 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=VEuENejKrKvwi5jd3fEQOIFk9pMWLkQIYT8gJCjoD3E=; fh=Y8FGf/qyaqS83NR6wUn7GWaZD/3HtjM31FeHFIgHb/I=; b=SBcPPZIH9j0//KcJJ3hbkDynEKEGr+IEJHASttlgze38VbzS1Ay0RFeZOHIyrY4XYc OSH93ofSH4J8MaehsRvWSXhpcVSz4BX/whqoZQ8GQ8zCK3RGAfsxLT3rtsKWlyiWWmAR IbV9N+GUYavKfEW7zId1QH1MIkLWNmnDwwM+YMox7F4wcIevvkZ7vFr1CS6QsEeTbLlq PZyQnoml3wBeMg6sq9yKERFWuyFX0Qs3ksxwiGJ5z8ZCohZ/pkb7aHAAPIRuwjWB+bLb 4gWqzxURk7GUgYU9bSeWbKtC2RxoMZMDFuKvFTICeIDqmjRqJAJM810h0SJjuU4CWZyn NOvQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=gAPRlQUz; 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-110369-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110369-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id ga5-20020a170906b84500b00a466b3e9b8bsi81949ejb.945.2024.03.21.09.52.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 09:52:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-110369-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=@google.com header.s=20230601 header.b=gAPRlQUz; 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-110369-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110369-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 7F8031F261F7 for ; Thu, 21 Mar 2024 16:43:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 213A5137939; Thu, 21 Mar 2024 16:37:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="gAPRlQUz" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 9B18713774F for ; Thu, 21 Mar 2024 16:37:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711039064; cv=none; b=kpFxK/5uLSwEMgnPUEmW+hx/NNfqHxdKfV6LSzcdSei6c1GBmFHbXUV5/rkSYWqAbV8q5QR17rBKgy5iR+My88E+b/lnTUkGVAHCbl6xC/5fiMUIEEpGgK7Vdjj41ujPNqjfZTyZojW3MI8ezlqiZlux/Y074pq8P2XyUgvDoa0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711039064; c=relaxed/simple; bh=+nXctbG4IOQrslvig2NTXz9yuPec0H8Ft5HG0m+Y1H4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=L15oAMc729EisVQwYw4PDdGdR7flXm8jePR131U6ess4Q3heCT19Wu9mAs7lAzACYmz0Gu1t1U3g8Xs8p7AOoGK3O2m5ppQha48PCjGT76fT8Bp/j3bsQUN3eM1tFOUERudIFz69VixZcBa2F7M47oYe47XEEsW0tymMuU4j7FU= 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=gAPRlQUz; arc=none smtp.client-ip=209.85.128.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-yw1-f201.google.com with SMTP id 00721157ae682-60cbba6f571so21986847b3.1 for ; Thu, 21 Mar 2024 09:37:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1711039061; x=1711643861; 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=VEuENejKrKvwi5jd3fEQOIFk9pMWLkQIYT8gJCjoD3E=; b=gAPRlQUz4PanaYdh7wgqgIcC5oNsJozBVWIYC4pQOjELSgbx4IRw3QlQRALhhExZZg +XR7PjTNxaWQ8zT+BLMLEBBXmw3GBfBxeNrna6K+EzOF6XUuqggvU9EKrpxggwc81z7T qkhdwgOYuNiE8CnMsYqSYiEkQN2ggBYnOJWDf0H1ePAX57rmGvKcxoLbea+cDhaLxHj8 GxpyKRin2Cht2im7fzixdgo6y9UZeb6gJaCrSvA75ZwGX5IeA0AU/6r2nlNz7hTFkDtz 5+ocaWjBiIUUH+jfvdfRHG4POKDt5S7sOmvMBw3swLJbVL3skK8TUdqfyGgT7rJNNOtg sQ5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711039061; x=1711643861; 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=VEuENejKrKvwi5jd3fEQOIFk9pMWLkQIYT8gJCjoD3E=; b=maJE4+2dEqo1UuuJgVr/78avrDHGC9FsfhHp6ZCNpAIfm/ij8PEfj5/xhiSq90Ajiw OMjEi5v2QWZDaiHK9vDd5krR3hYnMn4Cgh8gqWxfvkb2rp52ybazf7UCUSKuVcIRqId6 1HSt/rwGBGkelUG7Rs8Q30Ith85Kj0et/WCce089zN6hwFpgbVpqtkUnzfs0RhlkBAIF tyedcfPkQB+ag8ZG3a0DEpOClpHiuyrZ4a1CAkPGuL3IiLygATl0xzOcLtmvrlG3Qua6 LulaAQrGQyGFaXcYO9SEfnLd01Ip2yfIqa+3ZzEhRhSBCxXy3npCtlO2ccfZHttGdRoF jBng== X-Forwarded-Encrypted: i=1; AJvYcCWLLR5NUkdRlqOZsi+ZIjDH6+YTUR4vxYrIQNhKeclOlDn935V2xn9vvJj40z586vSCXZejTkBUGhZXmSaew5HVnB1vtvtPdR+NTr1S X-Gm-Message-State: AOJu0YwlXROTHFcER8+OHXIq1JJbpiZ2sC1hTYZhxcynrKVg8aaaLzLc XVgsKCQcumxZZJ1vOH5IQpO4TIs7GHGd1pyGLkANIeEfot1uGeTv44mefoEYUMXv+PEOE7++0o2 onA== 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:1507:b0:dcd:ad52:6932 with SMTP id q7-20020a056902150700b00dcdad526932mr5791743ybu.5.1711039060482; Thu, 21 Mar 2024 09:37:40 -0700 (PDT) Date: Thu, 21 Mar 2024 09:36:36 -0700 In-Reply-To: <20240321163705.3067592-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: <20240321163705.3067592-1-surenb@google.com> X-Mailer: git-send-email 2.44.0.291.gc1ea87d7ee-goog Message-ID: <20240321163705.3067592-15-surenb@google.com> Subject: [PATCH v6 14/37] lib: introduce support for page allocation tagging 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 Content-Type: text/plain; charset="UTF-8" Introduce helper functions to easily instrument page allocators by storing a pointer to the allocation tag associated with the code that allocated the page in a page_ext field. Signed-off-by: Suren Baghdasaryan Co-developed-by: Kent Overstreet Signed-off-by: Kent Overstreet Reviewed-by: Vlastimil Babka --- include/linux/page_ext.h | 1 - include/linux/pgalloc_tag.h | 78 +++++++++++++++++++++++++++++++++++++ lib/Kconfig.debug | 1 + lib/alloc_tag.c | 17 ++++++++ mm/mm_init.c | 1 + mm/page_alloc.c | 4 ++ mm/page_ext.c | 4 ++ 7 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 include/linux/pgalloc_tag.h diff --git a/include/linux/page_ext.h b/include/linux/page_ext.h index be98564191e6..07e0656898f9 100644 --- a/include/linux/page_ext.h +++ b/include/linux/page_ext.h @@ -4,7 +4,6 @@ #include #include -#include struct pglist_data; diff --git a/include/linux/pgalloc_tag.h b/include/linux/pgalloc_tag.h new file mode 100644 index 000000000000..66bd021eb46e --- /dev/null +++ b/include/linux/pgalloc_tag.h @@ -0,0 +1,78 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * page allocation tagging + */ +#ifndef _LINUX_PGALLOC_TAG_H +#define _LINUX_PGALLOC_TAG_H + +#include + +#ifdef CONFIG_MEM_ALLOC_PROFILING + +#include + +extern struct page_ext_operations page_alloc_tagging_ops; +extern struct page_ext *page_ext_get(struct page *page); +extern void page_ext_put(struct page_ext *page_ext); + +static inline union codetag_ref *codetag_ref_from_page_ext(struct page_ext *page_ext) +{ + return (void *)page_ext + page_alloc_tagging_ops.offset; +} + +static inline struct page_ext *page_ext_from_codetag_ref(union codetag_ref *ref) +{ + return (void *)ref - page_alloc_tagging_ops.offset; +} + +/* Should be called only if mem_alloc_profiling_enabled() */ +static inline union codetag_ref *get_page_tag_ref(struct page *page) +{ + if (page) { + struct page_ext *page_ext = page_ext_get(page); + + if (page_ext) + return codetag_ref_from_page_ext(page_ext); + } + return NULL; +} + +static inline void put_page_tag_ref(union codetag_ref *ref) +{ + page_ext_put(page_ext_from_codetag_ref(ref)); +} + +static inline void pgalloc_tag_add(struct page *page, struct task_struct *task, + unsigned int nr) +{ + if (mem_alloc_profiling_enabled()) { + union codetag_ref *ref = get_page_tag_ref(page); + + if (ref) { + alloc_tag_add(ref, task->alloc_tag, PAGE_SIZE * nr); + put_page_tag_ref(ref); + } + } +} + +static inline void pgalloc_tag_sub(struct page *page, unsigned int nr) +{ + if (mem_alloc_profiling_enabled()) { + union codetag_ref *ref = get_page_tag_ref(page); + + if (ref) { + alloc_tag_sub(ref, PAGE_SIZE * nr); + put_page_tag_ref(ref); + } + } +} + +#else /* CONFIG_MEM_ALLOC_PROFILING */ + +static inline void pgalloc_tag_add(struct page *page, struct task_struct *task, + unsigned int nr) {} +static inline void pgalloc_tag_sub(struct page *page, unsigned int nr) {} + +#endif /* CONFIG_MEM_ALLOC_PROFILING */ + +#endif /* _LINUX_PGALLOC_TAG_H */ diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index d9a6477afdb1..ca2c466056d5 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -978,6 +978,7 @@ config MEM_ALLOC_PROFILING depends on PROC_FS depends on !DEBUG_FORCE_WEAK_PER_CPU select CODE_TAGGING + select PAGE_EXTENSION help Track allocation source code and record total allocation size initiated at that code location. The mechanism can be used to track diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c index f09c8a422bc2..cb5adec4b2e2 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -115,6 +116,22 @@ static bool alloc_tag_module_unload(struct codetag_type *cttype, return module_unused; } +static __init bool need_page_alloc_tagging(void) +{ + return true; +} + +static __init void init_page_alloc_tagging(void) +{ +} + +struct page_ext_operations page_alloc_tagging_ops = { + .size = sizeof(union codetag_ref), + .need = need_page_alloc_tagging, + .init = init_page_alloc_tagging, +}; +EXPORT_SYMBOL(page_alloc_tagging_ops); + static struct ctl_table memory_allocation_profiling_sysctls[] = { { .procname = "mem_profiling", diff --git a/mm/mm_init.c b/mm/mm_init.c index 370a057dae97..3e48afcd0faa 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 4491d0240bc6..48cdd25261ea 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -54,6 +54,7 @@ #include #include #include +#include #include #include "internal.h" #include "shuffle.h" @@ -1101,6 +1102,7 @@ __always_inline bool free_pages_prepare(struct page *page, /* Do not let hwpoison pages hit pcplists/buddy */ reset_page_owner(page, order); page_table_check_free(page, order); + pgalloc_tag_sub(page, 1 << order); return false; } @@ -1140,6 +1142,7 @@ __always_inline bool free_pages_prepare(struct page *page, page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP; reset_page_owner(page, order); page_table_check_free(page, order); + pgalloc_tag_sub(page, 1 << order); if (!PageHighMem(page)) { debug_check_no_locks_freed(page_address(page), @@ -1533,6 +1536,7 @@ inline void post_alloc_hook(struct page *page, unsigned int order, set_page_owner(page, order, gfp_flags); page_table_check_alloc(page, order); + pgalloc_tag_add(page, current, 1 << order); } static void prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags, diff --git a/mm/page_ext.c b/mm/page_ext.c index 4548fcc66d74..3c58fe8a24df 100644 --- a/mm/page_ext.c +++ b/mm/page_ext.c @@ -10,6 +10,7 @@ #include #include #include +#include /* * struct page extension @@ -82,6 +83,9 @@ static struct page_ext_operations *page_ext_ops[] __initdata = { #if defined(CONFIG_PAGE_IDLE_FLAG) && !defined(CONFIG_64BIT) &page_idle_ops, #endif +#ifdef CONFIG_MEM_ALLOC_PROFILING + &page_alloc_tagging_ops, +#endif #ifdef CONFIG_PAGE_TABLE_CHECK &page_table_check_ops, #endif -- 2.44.0.291.gc1ea87d7ee-goog