From patchwork Wed Mar 6 18:24:12 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13584424 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 CE973C54E49 for ; Wed, 6 Mar 2024 18:25:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 50D776B00A1; Wed, 6 Mar 2024 13:25:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 492E26B00A2; Wed, 6 Mar 2024 13:25:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2BF936B00A3; Wed, 6 Mar 2024 13:25:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 11DD26B00A1 for ; Wed, 6 Mar 2024 13:25:18 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id D6EA4A0F49 for ; Wed, 6 Mar 2024 18:25:17 +0000 (UTC) X-FDA: 81867441474.10.B2D55A5 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf03.hostedemail.com (Postfix) with ESMTP id DFC8A20014 for ; Wed, 6 Mar 2024 18:25:15 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="TSI0uQ/p"; spf=pass (imf03.hostedemail.com: domain of 3CrXoZQYKCE0796t2qv33v0t.r310x29C-11zAprz.36v@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3CrXoZQYKCE0796t2qv33v0t.r310x29C-11zAprz.36v@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709749515; a=rsa-sha256; cv=none; b=fX4Bkbok9fMl5AZSHLZvwbIgcJ1xljsPnrYvuPj9gKQsfhyVSNvOH57VqBB/zUxHUoYXn6 ycrsm959e6fjVcu4D6YGrS7nqBNPLRx02GzbqIzWa9i1rR04h2U31eMQtcMB41rnVyl66v j+5Gskk5dwtNR09TgETn3T83IdMwDYs= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="TSI0uQ/p"; spf=pass (imf03.hostedemail.com: domain of 3CrXoZQYKCE0796t2qv33v0t.r310x29C-11zAprz.36v@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3CrXoZQYKCE0796t2qv33v0t.r310x29C-11zAprz.36v@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=1709749515; 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=nqAwBcStn3+iWVa0bV8AV7qbDJDX846MDYUJTJVUXBw=; b=5YgGSzDWLIOufIxn5TLM2NgtOlz4g5ePiFLY9st7RQL8YkqBs3S4psSdyGRfU3nRWct2Ko foiyHAok0ytC3LbTMd28Pk89xpqToHqjh/P1yXkvnvZZLL7kcCbefaa8UicnJKqtROGpYn vxz6HSOm4tG9TDdYipMR3XnFYbxec5s= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc6dbdcfd39so13633850276.2 for ; Wed, 06 Mar 2024 10:25:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709749515; x=1710354315; 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=nqAwBcStn3+iWVa0bV8AV7qbDJDX846MDYUJTJVUXBw=; b=TSI0uQ/poSxJS/6F9T24CQ3Q5ErR1x5wwHaSOFjQ0++Y77tya9HKMSm1/sVCPIox31 9z9ZHX7jZbj2nGTyCBDJUr/lCWjQIunU6F8u+nQlDVIXyAn/BAQ+lCV0PjoKjuMz21EN 9iP01ZYqioMBGdx1pybPReLC5Jy2GNycWv8Y4ijWUGRZyF4hQFdLsfhhM6/gcTz5mp1e PZSme9Lbr3hjF0XM5N9FqHntQrXNSzB8jEvd/EWQUiIi1HRm0Ndl2+6meWmd/sNsy3rO 9ILCi37zmdvDfgfQIp/5/zszS9PlY+X1JUEk/Ka4Yjqa0t4HaOPhLXj8UDur39RmbNaj 2kfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709749515; x=1710354315; 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=nqAwBcStn3+iWVa0bV8AV7qbDJDX846MDYUJTJVUXBw=; b=YiHaXu6X+p8HsneF0+qD6481u0rsuNqzfLSFAqr3WkbVwsx4Cs9btssNUvWRdS8Jno lDUWxlDQ6nbTUsWGuYKv9xaPa2PuIRjOdhha+3CjcXdKe80907H7ioOgDjbOaEDD9FM/ 982+kNxnizjBUTAno+vhux2ySoZjQxx3BDi+s05w7SVL+XWsLz3V5jXJGuami3xBYnOX 5NWeiNYp8xBpb1DVJHp0PnTB+YlPVuTJL7/8brSbTpxNtyOn2Nzw4h2+wBqG1YWSOfNj ItH028cOOziBeOA/tNOONm6hPi/gZJ6nhWh7lwRmNyCaLE9VdsRPzCQkTB/E/7t++th7 IaiA== X-Forwarded-Encrypted: i=1; AJvYcCU0xCkDxlUOP0ed4jl3mt7RprKjjVmJorB8jL/pF27fSuIceY+F+KRKTUi7AUS1bSONFUVzYUjgPgMAuRviZE6+DeM= X-Gm-Message-State: AOJu0YxXxCvo+LYfICLS4ysoFjJt1Bjc+0anDQnbFOdi12ZmBgHCQnZb eo198QR6kwtOiHxLmWK+dyrs6/wqhJ/vrSrvCJSki9+xzIXECY4COGoNSjFBDXXWeQOOtlE4Nsf hFQ== X-Google-Smtp-Source: AGHT+IHUeBGtAOO+/l11NzJyIxqA9udwD8w5OmuvXdYK78869+3pCDSzsZpyxOUQHcMci8NQK2ebtmqhDyo= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:85f0:e3db:db05:85e2]) (user=surenb job=sendgmr) by 2002:a05:6902:1744:b0:dcf:6b50:9bd7 with SMTP id bz4-20020a056902174400b00dcf6b509bd7mr3993743ybb.7.1709749514977; Wed, 06 Mar 2024 10:25:14 -0800 (PST) Date: Wed, 6 Mar 2024 10:24:12 -0800 In-Reply-To: <20240306182440.2003814-1-surenb@google.com> 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-15-surenb@google.com> Subject: [PATCH v5 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, 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 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: DFC8A20014 X-Stat-Signature: qu565ina1i7cuc6czy4zwiqq1tp4iz9q X-Rspam-User: X-HE-Tag: 1709749515-373180 X-HE-Meta: U2FsdGVkX196OdHerOT0ZYfRph28DOKTw2aarUmP4tPo6duA0t2WGzjBtsGDDlQSQtgu1ew2ez7JyTCwTXkaPLms/LqakBEJTNzYVwCqP1nTjbuzytVE7ni6i4EqMsF4WHfCw+wTXn8BXeCI37UUsPqbH2HZ0925kOcXwFIvsaniOf1j7lXEa2xxqagv5vEmnrSgfU9Di1hlyWLvuqjijd1A58KlUCMbJbGcyt9BWTBNNn2DyXcJzucbZARQgt/fCkgzcIEghaUlnGSuAAmpots5aeNlANe5qzPSFsACOkTgV22wZaFqvuwtFiI/we4jRj1ucM7W7kArwWwkE3qePLoSn/FvaMlLFwj9FLk/XGW4jQICWRI3nOnvL1+hRdV9H1ZBaC2HupKyYdcX93xzXgLtfc33IhhByet7lTE+eM7y2GUhZQ9noHFQdP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mFt6wYDFHJkR7rAWrHJdREp0MDcYNpqGqgyETeM4JBRR0Cjmphec9vJUy/QlcNOSUxSO2XuiSbs68kHcPIMMKS7VJw1kSPk71Nw1rqitSRz52l7ewRh4iCGthoTDq7bfBIDjFHr2y0BY1fbS2v79lTpPA= 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..b49ab955300f --- /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 order) +{ + 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 << order); + put_page_tag_ref(ref); + } + } +} + +static inline void pgalloc_tag_sub(struct page *page, unsigned int order) +{ + if (mem_alloc_profiling_enabled()) { + union codetag_ref *ref = get_page_tag_ref(page); + + if (ref) { + alloc_tag_sub(ref, PAGE_SIZE << order); + 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 order) {} +static inline void pgalloc_tag_sub(struct page *page, unsigned int order) {} + +#endif /* CONFIG_MEM_ALLOC_PROFILING */ + +#endif /* _LINUX_PGALLOC_TAG_H */ diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 0dd6ab986246..3e06320474d4 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 549e76af8f82..2fd9bf044a79 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 16241906a368..9a91c8074556 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, 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, 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, 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