Received: by 2002:ab2:788f:0:b0:1ee:8f2e:70ae with SMTP id b15csp78444lqi; Wed, 6 Mar 2024 10:29:48 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXsb/OfA9olFXUVl/xWeYo5NGqaJWq/smay744GDpO3F+6+leWaXeX8351d2xNexbK9L3Z95WF8MY3RqgcVkyjWYZAonOoOynW/QdHLVw== X-Google-Smtp-Source: AGHT+IH57SEJKxj1PgxKQ+O2Sq8ssW+zG+B7tH3BpRfGmVohTw187iJlLn7TR3IrjIv2Sfh6aQbH X-Received: by 2002:a17:902:ec91:b0:1db:f6b0:92d with SMTP id x17-20020a170902ec9100b001dbf6b0092dmr7589201plg.6.1709749788239; Wed, 06 Mar 2024 10:29:48 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709749788; cv=pass; d=google.com; s=arc-20160816; b=kmfULEqe3CE2uN+FvtmRXA38qlIDTUFXfTpRYYpEMKhR5pZt+uUJsRW3GQjCPH9PuJ Lg2OvzvF7hdrfFMtjVRES9cbHyckYgp4j2deLo+qoT7tr6sX6hqMSZdEGP2Lj32d0bzU 3mIHZZgWKIHL6EyIBnEiXHEVH1AbdCFQxnLkTGd8ExjTzPE3Gl57VI/9s0iKwK5R+evz E/A+ekuWwHAuHRolDJJgPox0mFOXqHVRAmLYVQWCTFtvy3ubPhQpRtsqumuuJ1cxSQv6 TT77T/XMDC0F/RDM6UN99H+NYJlzacVjb00GS+PTt1E9RpO1YePtPTG3bYW8qFHFndk5 L8RA== 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=nqAwBcStn3+iWVa0bV8AV7qbDJDX846MDYUJTJVUXBw=; fh=TD9/+H5AGdrQoIBwByqsDKacMxiAcVDLs3IHiy5GdlY=; b=pmPibcKZZTe9IUHbTtmzcCQTg4pvlt/MKdzcR+B0FOEj9sg88JoZk6p0k9vEwk54Nr g9zYL3HYK7nyd9Bs4D4Bl0JjaAtEwlHjjHKvx6RkW6oj2pDnss6peAPR68IZPRL+OSU4 jIQ0q6GWcEmman2Ez10CLamqHFnXnS16Q1uUOI02m2ZJw8bh3sPNfj25c343fNREYIe+ 96hf7nfYGjI2S7HU2yP0BVHV4W4p5gbbuUaViRkqO0jPCtvE5cnRLr0+3nHppKbJkYMz 6A3oNLJSPR40hJUawC+KEUrfyfznbnN37IkqK+0Mz7gdToX4GzNOpP8U6QwOKmRf6xOQ 0eQg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=BfZnAjTv; 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-94406-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94406-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id p18-20020a170902e75200b001dc813bcda4si12811933plf.349.2024.03.06.10.29.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 10:29:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-94406-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=BfZnAjTv; 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-94406-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-94406-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id A35272871F0 for ; Wed, 6 Mar 2024 18:29:47 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DF1501487E8; Wed, 6 Mar 2024 18:25:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="BfZnAjTv" Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.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 DAF5114691A for ; Wed, 6 Mar 2024 18:25:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709749519; cv=none; b=OVZwWDLR3P70YA46+BUbfyT47RGA4gsVzoTkd6WzZ8M1NjQpAzShTeB1r1S4cZ0179MZzWasY6oWBg2c8rQ0kbxgt05hTAPxSjPQKXPYN/tTpo6zD2iyL2ARk/2NzDcKBnt2hf4vL1eJeiOmLEioWh47WY3IYuMKaNKo8YXrLYA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709749519; c=relaxed/simple; bh=3pKe8DymeGCzzCDzC7IH+hBg1k55V6+wJbdT27mHcek=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=hKp51UwQrV2YFM8oI1mH3slGMgQHTyUCjck4SFKQEiJ8+P5OeLfu7dMXXG4XuUUkaezYREDkxQc2efjw7Pt2nmXZk6XHCaQ9/WQIeW9lCsROvk8qyGxxnMYsAnVoeYxh55eHtNrh83uNHBVeftLMP7qw1kNESNM+mqHvUkqBfts= 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=BfZnAjTv; arc=none smtp.client-ip=209.85.219.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-yb1-f201.google.com with SMTP id 3f1490d57ef6-dc6ceade361so11997140276.0 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=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=nqAwBcStn3+iWVa0bV8AV7qbDJDX846MDYUJTJVUXBw=; b=BfZnAjTv/f13elJsdbe6AVJNr4VoW0mMjFTgWFFFR0Ou8YPOMk2DlrzM5cFBGtsYjg 82qe/H7xZGEHLFalQoPsUXmz+AQIQFhQlY7UQ+cO/MJdSgb40QXp44oKE55z0MgVhZay bgs10xOKzhsFAUWlzGEwXB1dEXFK2tlZP3eBbsdyX/fouxdMgduVEER0pXwjveOLqVOY UvCfoCxINyTdp0sGRLDykVCpIvtP77oHaTX1zSLoluRtUqbGhjyv0l7aymb1UHUxnT/E VuJNS7SaXyfb8bvn0ty9JhtSflCjMdSpsrf+Kny8ld7EA5Glh+9GU2uYMfAhTn/wjILd QfJg== 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=kGyaS+GQWxUpNaaTisKIYOf6rTB//rNgxsFBVtQKeZQNoe2XiXl8e2bB/oNtZeGI7e Fuq6+lS9/ei2vt2ZEEMawJ255UqC9tVW9JuDjUO8SgVP0y1HptRAxbEGMMosiT7VqzTZ /lsFMrrJeSLsJImR52XPmn2RvnqPJarQFdGgA+TJgEsJnkY8+/4iS0A+F8DW1RG/kMqu aNuBlGbEpcRBKfbsayUmiO6u5w/w7HIMYI51I6G30PrBkb8Q2LI15bF7u2D1P+8dFe5e ToYT/ZjoNsbI2Tijtk4en+DGGxcwWDeTmsSv4HX5XOwH+NHy2vGU8K+qI0LWYUKIYwlh +yqg== X-Forwarded-Encrypted: i=1; AJvYcCUd5B1kYCq6MGHSJ9WOI+mYA1W8Z1w7F7GAYeI1M6chx2I24rKXMRQWV9bJg7sbE1J42FLJm/Vbx6Hk+tjyQWp4ObOcTdSiIRaA1apf X-Gm-Message-State: AOJu0Yx2qS4YEhdbfxECsNzpMRvqXDUitDP8Vetehg04M9aQim/Kwuam BelXvYgsVC7gtmVNbZOeCarLDGf0u4or7iJu77kn64toTByQD5iKmrbMjmknKHy37pn9mTiFnFt VzQ== 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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..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 -- 2.44.0.278.ge034bb2e1d-goog