From patchwork Thu Mar 21 16:36:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13599171 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 8915EC54E58 for ; Thu, 21 Mar 2024 16:37:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2649B6B00A6; Thu, 21 Mar 2024 12:37:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1F0076B00A7; Thu, 21 Mar 2024 12:37:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0197C6B00A8; Thu, 21 Mar 2024 12:37:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id D68936B00A6 for ; Thu, 21 Mar 2024 12:37:43 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id A7D8A1601DA for ; Thu, 21 Mar 2024 16:37:43 +0000 (UTC) X-FDA: 81921602406.10.83C22CF Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf24.hostedemail.com (Postfix) with ESMTP id AEE1A18002D for ; Thu, 21 Mar 2024 16:37:41 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=IIJiKUky; spf=pass (imf24.hostedemail.com: domain of 3VGL8ZQYKCEEvxuhqejrrjoh.frpolqx0-ppnydfn.ruj@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3VGL8ZQYKCEEvxuhqejrrjoh.frpolqx0-ppnydfn.ruj@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711039061; 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=VEuENejKrKvwi5jd3fEQOIFk9pMWLkQIYT8gJCjoD3E=; b=UR+lWZR0a6QywjMrM/qp9FMX+y7QMKGuhOXTlTqs/6HH151FnSbHeffqhhsJDJyImuOJVI Pk73gUZLn0S6ijMYoDL5Y2kQruaqPiy5hGjrexq5lXeDvtYoH88bDjT0k0Z0HY8K1HaKYe aCeRsRWktk61izV62IMkjBYVJmY8rq4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711039061; a=rsa-sha256; cv=none; b=z5t30tu6aZ+MM2gshrUAA+MpOTk2K3Mx3u6Z17lI4NvEvhBomrmNmuUtdpk9+IE2YlV8aa ObV+N4zX3y//sk4tKQgk4THfF3u38blF2oF1Gic8PMPjRGyvX++gKplUZ2hMwATwR+KjPf Qzd2Flp5HIE1cacvi8F9/TmB0HqIwVk= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=IIJiKUky; spf=pass (imf24.hostedemail.com: domain of 3VGL8ZQYKCEEvxuhqejrrjoh.frpolqx0-ppnydfn.ruj@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3VGL8ZQYKCEEvxuhqejrrjoh.frpolqx0-ppnydfn.ruj@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc6ceade361so2069962276.0 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=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=VEuENejKrKvwi5jd3fEQOIFk9pMWLkQIYT8gJCjoD3E=; b=IIJiKUkyktaBvN/XI+YvF583THPOZj/FCszOCDXnYI6l6POIlA3NM9I74EzYk7PS7Z NJ6FXcBMl7jUgl05Gm6T2AeIkAPEIp/zbyVttWZLGxdyKQVkld0o1IE/rcfZhjUy7HUT LxKd0guJteRnVFp3abZ6Dd9zdnq31MSNhndz33jVg0G3BIilhhVM6BWDmLrYM772nNRO EE5qVukId8AZR3Gvg9T4F3SsAuBjW73YbBROTOHqJ4t42RTnq0MVnpZuSIVrsPd6CqlA 2ZBRESqE7BznKI2GjqyrB14P+36YtIN4W7fw5KEPN0U6IcA9KSo0jvNiK8zi07x5fJPu stCA== 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=uh4RS/15PDgtjjf0sLAMMnUf0b61tbUfMuJ7wSx/Yb3WTcqFAbpOObBAwTzCdKIOIN Jh8Csm84AYmT385swN9WL884i52HljH4px7Ylcg0oB//QpPxe+SQ/5p8UUSlK5IwS9JR W6V0lLxOoaxS+vaP+nGt9022Tz48DsYxIvc733fXplgcqEOFaGZSHHbv8NiBkfdGusAQ ce32RU4iDDbQ3FF4NxMlmXTE/n3qCyYt54uzj6TpAtCbyeunWtOeN+KlxTG7pBAuuzcN meGeWAgsYmmEVvZyqG7qhDS/0ptN3fOMkHzEFchm5aBGw9pDYKho78L9QSThQXmtmFP5 i8pg== X-Forwarded-Encrypted: i=1; AJvYcCVBctOwCf1S0c40byCUmCkX7BbrixottiIItuc4h31Brpz0tulXecFh55Y1veCliQqLB8kgPbTCxD/SMwFKWooEjic= X-Gm-Message-State: AOJu0YytfwP4RQyrSi+3jiFV+ZNrqep+bNDDm6GAFSUiTnA7AllSthfA pzgb6Y71TNgqoJBsyEwm1XGG53MwULxFJ1wBRqupsvhV7zp3lc4PDPJBgSGqmHQEW1qZpZnca/o jdQ== X-Google-Smtp-Source: AGHT+IFNQMTWyzDytJBsiwO6OlHwO/XgSIlxcnK6gjsE8Q4BevLiMexdP0sp4DeK7t/kONH0yJROFT8KJZw= 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> 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 X-Rspamd-Queue-Id: AEE1A18002D X-Rspam-User: X-Stat-Signature: h5eodtujs3d3n9y9rstx89e9389744kp X-Rspamd-Server: rspam03 X-HE-Tag: 1711039061-678563 X-HE-Meta: U2FsdGVkX1+rcqvzpQz1tPJQnW7aLEJU0Ck8hTp4qrm8RJBLvqVVGEmqRoFs9995tBMBUAFF3vFdEhnDj+5HEZUwhs4IhEbHQ2LHYLnMnzCRGgoQDj/6DMyoDMkibWof6j8gLhWBydMR+/lYJJVxKkrCGYoGxNXtBdpDm5pSWbxq7KvdC9JUdnRguxOo1j8qZ+yb8xEg/J6twXRLUCkuDJv9rl/EuplnuhlE2DFDmbkzfT9JjMNH9xsFyZq5SXd/LnkWAUfGmJzjjajucyexnWTj6GBwQDzzeLrTpkHHSgRf6cUNsm7m8n2R5/wW7vnIR2038GSd0vIwkPqTMhYnPne8E5+rAdrp7AHceG2WynDM5dF4nAtexlgTzmJsEwPd2m7XLAse9aJUhuNaJ48KGIBbUqMetCxROU7da40BXZuTdY23MBQDAD73ZvhkBkyWWMWUzX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BgjEd9dSn+aNM7YNss/koZDghaAuqsJWDzmgB3YnIzgz4gjnPxhBi4vAgfJ/NttaYIdFwSKdwR0dVqVEbLddsgH/5FXQdHeA2CsaaJwCPw+kA0ryDMWIRQbUreOJjjTwg1QvIJjhPA7kXSBpdF/Wew== 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: 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