From patchwork Mon Aug 19 15:15:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13768488 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 654DEC5320E for ; Mon, 19 Aug 2024 15:15:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D63DB6B0089; Mon, 19 Aug 2024 11:15:21 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D11506B008A; Mon, 19 Aug 2024 11:15:21 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ACA6D6B008C; Mon, 19 Aug 2024 11:15:21 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 844CC6B0089 for ; Mon, 19 Aug 2024 11:15:21 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 2B3ADC1291 for ; Mon, 19 Aug 2024 15:15:21 +0000 (UTC) X-FDA: 82469343642.18.951077E Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf03.hostedemail.com (Postfix) with ESMTP id 1AFF820006 for ; Mon, 19 Aug 2024 15:15:17 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=r8R6f6ZU; spf=pass (imf03.hostedemail.com: domain of 3hWHDZgYKCJIEGD09x2AA270.yA8749GJ-886Hwy6.AD2@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3hWHDZgYKCJIEGD09x2AA270.yA8749GJ-886Hwy6.AD2@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=1724080457; 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=vP5Y3+St8DLMnl+ojNb4BcLnBgri+HpVXEz96Xrg0no=; b=sXrRJWd92KWlWSgrubOvx6tNA8TgMmxBznUDIgrdAKKo7tPKBLxv0xExq9XbRCTrbM1liu 2nrjbdj4T1VadU1jriElFNwYLGzO/X6GYIUYGhf3v2FtPmSv8p7MMjUGUMeC3SXvClrP5O PfzDvWzB4HU3NPpKSUWSTNcOh4XE8MQ= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=r8R6f6ZU; spf=pass (imf03.hostedemail.com: domain of 3hWHDZgYKCJIEGD09x2AA270.yA8749GJ-886Hwy6.AD2@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3hWHDZgYKCJIEGD09x2AA270.yA8749GJ-886Hwy6.AD2@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724080457; a=rsa-sha256; cv=none; b=vHs0Cx2WK2bsFRGL32KsFU/47TRXkWhsj9QwHaKYxIVuFa3SUzHiczrA7zmc6J7X7WxQ2K 3H9GMJUoW+t8tmssIlzWTBaqPffNa6eJ0iCTDyHeUT5OKXKS20Jr3O6xaL9GoVvH5xTeCI +lFEuoE5bOYIBw6FyMdnKC9eiJG8WtI= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6b73fb4ff56so26650387b3.0 for ; Mon, 19 Aug 2024 08:15:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1724080517; x=1724685317; 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=vP5Y3+St8DLMnl+ojNb4BcLnBgri+HpVXEz96Xrg0no=; b=r8R6f6ZUKGzOYQgrdTEa8bcntD2rAF5z1UZp1xK+jF03SFns7anZqvHUWk1qLkSXce StwfiEP3L36UZAiG0rgVDyFAnLhuZkQKFeVTaUkQU3KrRtRFif2W+TZYuCdavrkaV7uE QZvOIroPq9obVl+Fdz5TRoFnK87h2PCao8+DM/IjmPHI4RNq7kb7wTPnwaSDhderElim GFTyvcb+r71O070H2Vl/usWO9jwYabjsseqJ/M96112G6mbpGTVJtqOvXMvFIRoVOwC3 pxkgp7qyaFf/b90Ha57VaOoMRjLUHJGEPH3rWzd0pc/GwCg6Ar7hNcYh9x+z1oFdYL2a o0ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724080517; x=1724685317; 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=vP5Y3+St8DLMnl+ojNb4BcLnBgri+HpVXEz96Xrg0no=; b=iEd+wESs9IkEEVzkYDqsSnucq3ayN2/zs1AV6xRHGU8i2MRYAb7e5t2/HHUCpTpJac C8glVmWqxhTBubfwTv6/zbs7Pxg8LGspF9rD6U6yTsZUOQ/Fhda1J2KYmsGbSrQbeVlz fri21F/q09WkJ1aRfBZr/FsTZAN395Ol423cYlYtO9E6Ri6qx7eu06+gD54Ohp3n0uxt gMlTU/WM6QcGqHTvu3m9Tc/dNi4jpQvo26q9HPRH/XQ6oq29H+7baJDjW90LUAgczqoW bOGoJQi/R3NJEOhFJCd4qBUtSrKzNJdBYIqLZ/lHboI9+kHw0xdVy1CBkWIF5J0nIRv6 g63A== X-Forwarded-Encrypted: i=1; AJvYcCXLxqz8VjVeq3tt5TLf0Htxf+O4t6FSUilrP3mtE9Gb2pWSvlcBiUDa1qDIrPL5yfawq69iOBWUbBohNR0zssjZVAQ= X-Gm-Message-State: AOJu0Yx0i5h2YwuZwWXBBkImda9BFgNqhA9yJbZMAc6ZtW74Rrc6afbY ZZrVhhK/yFGcayWaCTITEI+5ZdTqsQwH9d7ruUJ26aLWOoj+5X/MYvSbqKa/51RxtLWdiXWbYHT UXA== X-Google-Smtp-Source: AGHT+IHIzYk9Q273USL5AsYZzJs11m0hcVvSOdyFJga9/z80thJTPdIa+orVuheupXR2PsXCT+j74zPGSXM= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:5aea:cf26:50f4:76db]) (user=surenb job=sendgmr) by 2002:a25:ec0a:0:b0:e11:5b8c:f9c with SMTP id 3f1490d57ef6-e1180bb40a8mr216454276.0.1724080517086; Mon, 19 Aug 2024 08:15:17 -0700 (PDT) Date: Mon, 19 Aug 2024 08:15:07 -0700 In-Reply-To: <20240819151512.2363698-1-surenb@google.com> Mime-Version: 1.0 References: <20240819151512.2363698-1-surenb@google.com> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240819151512.2363698-2-surenb@google.com> Subject: [PATCH 1/5] alloc_tag: load module tags into separate continuous memory From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: kent.overstreet@linux.dev, corbet@lwn.net, arnd@arndb.de, mcgrof@kernel.org, rppt@kernel.org, paulmck@kernel.org, thuth@redhat.com, tglx@linutronix.de, bp@alien8.de, xiongwei.song@windriver.com, ardb@kernel.org, david@redhat.com, vbabka@suse.cz, mhocko@suse.com, hannes@cmpxchg.org, roman.gushchin@linux.dev, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, pasha.tatashin@soleen.com, souravpanda@google.com, keescook@chromium.org, dennis@kernel.org, jhubbard@nvidia.com, yuzhao@google.com, vvvvvv@google.com, rostedt@goodmis.org, iamjoonsoo.kim@lge.com, rientjes@google.com, minchan@google.com, kaleshsingh@google.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, kernel-team@android.com, surenb@google.com X-Stat-Signature: ifos696gqgxobidqx9598gir3png4hn3 X-Rspam-User: X-Rspamd-Queue-Id: 1AFF820006 X-Rspamd-Server: rspam02 X-HE-Tag: 1724080517-340773 X-HE-Meta: U2FsdGVkX1/3JMW2Lenasljeq2zsp0qsOhtmNNOA/XWitzrU4QjQait4BHhWZ6GJFq0/HGoTZGa9V5aGYEgGa3oyOqBiOw3k8/CuclucbXfbUq2PCIudWZS5zeCgLUr8S4nSqKcQxJtrZClE2CTa/f+E8amnoHmIW+g3cbzX8qISI9/ddTwI/43ga/W9qkVB7hTHw67h+Xda8eJZQHtPJC6lHTpc+zzmeUcBrEHsurOZT+i8u8XdT9R6RmbYF6ZKwpJOtMH/YYr1Thene5Dg4jHTlyo3Ixsz9H+woGJPqc6ACUZAKLmV3TPHR3PRvjDkuaWqzvMXQ++Zfte/NQLlr98+gebZqgdPs15iB/uAtXx3u1rInGwSL10b0H4ja6pyJaP42jP6y5aTkfOZnOHKrXUlhQ8OFFj11J2gq8HI8D3Z2c43d2Sf4Pm8IrnuVwxdYTBCUwzPXmW/DmkHC1wzzw75WrYmbNTQ/S4WtEER1otvmnH1kp4bWjAD8ju09uz1yxILDPCWZkJOnV1QBQ5OO6vyMtH/P65k7tUHgLvWs/VEUU2EZPZuXP55fnCjdfXpc4ltZJkvwOUhNao/X3q+tRdJ4U9/VblMX4JkWNcXBsKQ4KVD8z5nPOId0eObx8M6DvWlryoC/OckEjqrxIb7cZzxdNt8+wieHq34QamP5RkIBuvOURoiwfrpEqxq6Z3yT1o/9U9o5KvT6o3p0QFEcXkh60lIJ5o8+azRxWilCQDYt0OOTJAxW11H6FE91o6kbDqCE48xxY6Vuk1nqcBr9OFakj3DB4oxKfY6RBvAE3JRPinyfVeGSwrTZlnHf3WjpzVy4QG1zEccMwu+nB3lHUizepmEk4jTU91MDQFNFjYgxA7LsZbtT4XEnTaL6kgRthPmgGzpWu3/Nt5Db7XNDgeSR/kXAmVKvdR2wLEQqAN2HMcCcPUDPL9k2CCnAXR+5jpZYd3OWfU0yXd+aHQ rRVcjCiG 4Jr+ZKbPWAwUN9pAGu6R9PGYrplpjoQolK9onnlTbA9+9dCj5lk1DoWT10FNwx2i88u0xb4t6nAsPbawgopunkLeSKWoj0i05Chtm37Hn7qZ8gh/h8uWoCDjbtJNsczSPlatRKw55cO+t4pxWA9bblsANCtBVNhDh0hW27p6ohiIVgkPy1l3DUY1MgZr4I7bZAWgZ37P2j6A0YV0MwIf3aPQM8A8t25EsDQTj+ASfWAg6gcygaJNrP760eNi2gPxama73X8Lx2+Dz+wkeMhvuJ0e+vg+noJ/sctmt/cZbq3WRC/0znu68fOcoN1GQ/dF7JDr1oZEjcxeXYb18v5FUiSJTL8DmF5Y5ZctrKLnl5hoGWhkqAzrtnTIKtCqZJaXn6hN6SL48m69G2N9n1FyeT0Ruo4DO/2J92v6zYR4nRTU1lM3Y2Bx8Gk9c6XrN80kiAnmqOqr825W7yIkmuX/dehZY6mMtLdClfWCLB+ygJS6woh3m2zrCd9S5t3xGq5f1fBieWdCprPohnmFTf3eibSmBAu97wM2Xg6iQWDSUBvRHdLndrtv9pEJ43OfC/2rLwaZK 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: When a module gets unloaded there is a possibility that some of the allocations it made are still used and therefore the allocation tags corresponding to these allocations are still referenced. As such, the memory for these tags can't be freed. This is currently handled as an abnormal situation and module's data section is not being unloaded. To handle this situation without keeping module's data in memory, allow codetags with longer lifespan than the module to be loaded into their own separate memory. The in-use memory areas and gaps after module unloading in this separate memory are tracked using maple trees. Allocation tags arrange their separate memory so that it is virtually contiguous and that will allow simple allocation tag indexing later on in this patchset. The size of this virtually contiguous memory is set to store up to 100000 allocation tags and max_module_alloc_tags kernel parameter is introduced to change this size. Signed-off-by: Suren Baghdasaryan --- .../admin-guide/kernel-parameters.txt | 4 + include/asm-generic/codetag.lds.h | 19 ++ include/linux/alloc_tag.h | 13 +- include/linux/codetag.h | 35 ++- kernel/module/main.c | 67 +++-- lib/alloc_tag.c | 245 ++++++++++++++++-- lib/codetag.c | 101 +++++++- scripts/module.lds.S | 5 +- 8 files changed, 429 insertions(+), 60 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index d0d141d50638..17f9f811a9c0 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -3205,6 +3205,10 @@ devices can be requested on-demand with the /dev/loop-control interface. + + max_module_alloc_tags= [KNL] Max supported number of allocation tags + in modules. + mce [X86-32] Machine Check Exception mce=option [X86-64] See Documentation/arch/x86/x86_64/boot-options.rst diff --git a/include/asm-generic/codetag.lds.h b/include/asm-generic/codetag.lds.h index 64f536b80380..372c320c5043 100644 --- a/include/asm-generic/codetag.lds.h +++ b/include/asm-generic/codetag.lds.h @@ -11,4 +11,23 @@ #define CODETAG_SECTIONS() \ SECTION_WITH_BOUNDARIES(alloc_tags) +/* + * Module codetags which aren't used after module unload, therefore have the + * same lifespan as the module and can be safely unloaded with the module. + */ +#define MOD_CODETAG_SECTIONS() + +#define MOD_SEPARATE_CODETAG_SECTION(_name) \ + .codetag.##_name : { \ + SECTION_WITH_BOUNDARIES(_name) \ + } + +/* + * For codetags which might be used after module unload, therefore might stay + * longer in memory. Each such codetag type has its own section so that we can + * unload them individually once unused. + */ +#define MOD_SEPARATE_CODETAG_SECTIONS() \ + MOD_SEPARATE_CODETAG_SECTION(alloc_tags) + #endif /* __ASM_GENERIC_CODETAG_LDS_H */ diff --git a/include/linux/alloc_tag.h b/include/linux/alloc_tag.h index 8c61ccd161ba..99cbc7f086ad 100644 --- a/include/linux/alloc_tag.h +++ b/include/linux/alloc_tag.h @@ -30,6 +30,13 @@ struct alloc_tag { struct alloc_tag_counters __percpu *counters; } __aligned(8); +struct alloc_tag_module_section { + unsigned long start_addr; + unsigned long end_addr; + /* used size */ + unsigned long size; +}; + #ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG #define CODETAG_EMPTY ((void *)1) @@ -54,6 +61,8 @@ static inline void set_codetag_empty(union codetag_ref *ref) {} #ifdef CONFIG_MEM_ALLOC_PROFILING +#define ALLOC_TAG_SECTION_NAME "alloc_tags" + struct codetag_bytes { struct codetag *ct; s64 bytes; @@ -76,7 +85,7 @@ DECLARE_PER_CPU(struct alloc_tag_counters, _shared_alloc_tag); #define DEFINE_ALLOC_TAG(_alloc_tag) \ static struct alloc_tag _alloc_tag __used __aligned(8) \ - __section("alloc_tags") = { \ + __section(ALLOC_TAG_SECTION_NAME) = { \ .ct = CODE_TAG_INIT, \ .counters = &_shared_alloc_tag }; @@ -85,7 +94,7 @@ DECLARE_PER_CPU(struct alloc_tag_counters, _shared_alloc_tag); #define DEFINE_ALLOC_TAG(_alloc_tag) \ static DEFINE_PER_CPU(struct alloc_tag_counters, _alloc_tag_cntr); \ static struct alloc_tag _alloc_tag __used __aligned(8) \ - __section("alloc_tags") = { \ + __section(ALLOC_TAG_SECTION_NAME) = { \ .ct = CODE_TAG_INIT, \ .counters = &_alloc_tag_cntr }; diff --git a/include/linux/codetag.h b/include/linux/codetag.h index c2a579ccd455..c4a3dd60205e 100644 --- a/include/linux/codetag.h +++ b/include/linux/codetag.h @@ -35,8 +35,13 @@ struct codetag_type_desc { size_t tag_size; void (*module_load)(struct codetag_type *cttype, struct codetag_module *cmod); - bool (*module_unload)(struct codetag_type *cttype, + void (*module_unload)(struct codetag_type *cttype, struct codetag_module *cmod); + void (*module_replaced)(struct module *mod, struct module *new_mod); + bool (*needs_section_mem)(struct module *mod, unsigned long size); + void *(*alloc_section_mem)(struct module *mod, unsigned long size, + unsigned int prepend, unsigned long align); + void (*free_section_mem)(struct module *mod, bool unused); }; struct codetag_iterator { @@ -71,11 +76,31 @@ struct codetag_type * codetag_register_type(const struct codetag_type_desc *desc); #if defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES) + +bool codetag_needs_module_section(struct module *mod, const char *name, + unsigned long size); +void *codetag_alloc_module_section(struct module *mod, const char *name, + unsigned long size, unsigned int prepend, + unsigned long align); +void codetag_free_module_sections(struct module *mod); +void codetag_module_replaced(struct module *mod, struct module *new_mod); void codetag_load_module(struct module *mod); -bool codetag_unload_module(struct module *mod); -#else +void codetag_unload_module(struct module *mod); + +#else /* defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES) */ + +static inline bool +codetag_needs_module_section(struct module *mod, const char *name, + unsigned long size) { return false; } +static inline void * +codetag_alloc_module_section(struct module *mod, const char *name, + unsigned long size, unsigned int prepend, + unsigned long align) { return NULL; } +static inline void codetag_free_module_sections(struct module *mod) {} +static inline void codetag_module_replaced(struct module *mod, struct module *new_mod) {} static inline void codetag_load_module(struct module *mod) {} -static inline bool codetag_unload_module(struct module *mod) { return true; } -#endif +static inline void codetag_unload_module(struct module *mod) {} + +#endif /* defined(CONFIG_CODE_TAGGING) && defined(CONFIG_MODULES) */ #endif /* _LINUX_CODETAG_H */ diff --git a/kernel/module/main.c b/kernel/module/main.c index 71396e297499..d195d788835c 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1225,18 +1225,12 @@ static int module_memory_alloc(struct module *mod, enum mod_mem_type type) return 0; } -static void module_memory_free(struct module *mod, enum mod_mem_type type, - bool unload_codetags) +static void module_memory_free(struct module *mod, enum mod_mem_type type) { - void *ptr = mod->mem[type].base; - - if (!unload_codetags && mod_mem_type_is_core_data(type)) - return; - - execmem_free(ptr); + execmem_free(mod->mem[type].base); } -static void free_mod_mem(struct module *mod, bool unload_codetags) +static void free_mod_mem(struct module *mod) { for_each_mod_mem_type(type) { struct module_memory *mod_mem = &mod->mem[type]; @@ -1247,25 +1241,20 @@ static void free_mod_mem(struct module *mod, bool unload_codetags) /* Free lock-classes; relies on the preceding sync_rcu(). */ lockdep_free_key_range(mod_mem->base, mod_mem->size); if (mod_mem->size) - module_memory_free(mod, type, unload_codetags); + module_memory_free(mod, type); } /* MOD_DATA hosts mod, so free it at last */ lockdep_free_key_range(mod->mem[MOD_DATA].base, mod->mem[MOD_DATA].size); - module_memory_free(mod, MOD_DATA, unload_codetags); + module_memory_free(mod, MOD_DATA); } /* Free a module, remove from lists, etc. */ static void free_module(struct module *mod) { - bool unload_codetags; - trace_module_free(mod); - unload_codetags = codetag_unload_module(mod); - if (!unload_codetags) - pr_warn("%s: memory allocation(s) from the module still alive, cannot unload cleanly\n", - mod->name); + codetag_unload_module(mod); mod_sysfs_teardown(mod); @@ -1308,7 +1297,7 @@ static void free_module(struct module *mod) kfree(mod->args); percpu_modfree(mod); - free_mod_mem(mod, unload_codetags); + free_mod_mem(mod); } void *__symbol_get(const char *symbol) @@ -1573,6 +1562,14 @@ static void __layout_sections(struct module *mod, struct load_info *info, bool i if (WARN_ON_ONCE(type == MOD_INVALID)) continue; + /* + * Do not allocate codetag memory as we load it into + * preallocated contiguous memory. s->sh_entsize will + * not be used for this section, so leave it as is. + */ + if (codetag_needs_module_section(mod, sname, s->sh_size)) + continue; + s->sh_entsize = module_get_offset_and_type(mod, type, s, i); pr_debug("\t%s\n", sname); } @@ -2247,6 +2244,7 @@ static int move_module(struct module *mod, struct load_info *info) int i; enum mod_mem_type t = 0; int ret = -ENOMEM; + bool codetag_section_found = false; for_each_mod_mem_type(type) { if (!mod->mem[type].size) { @@ -2257,7 +2255,7 @@ static int move_module(struct module *mod, struct load_info *info) ret = module_memory_alloc(mod, type); if (ret) { t = type; - goto out_enomem; + goto out_err; } } @@ -2267,11 +2265,27 @@ static int move_module(struct module *mod, struct load_info *info) void *dest; Elf_Shdr *shdr = &info->sechdrs[i]; enum mod_mem_type type = shdr->sh_entsize >> SH_ENTSIZE_TYPE_SHIFT; + const char *sname; if (!(shdr->sh_flags & SHF_ALLOC)) continue; - dest = mod->mem[type].base + (shdr->sh_entsize & SH_ENTSIZE_OFFSET_MASK); + sname = info->secstrings + shdr->sh_name; + /* + * Load codetag sections separately as they might still be used + * after module unload. + */ + if (codetag_needs_module_section(mod, sname, shdr->sh_size)) { + dest = codetag_alloc_module_section(mod, sname, shdr->sh_size, + arch_mod_section_prepend(mod, i), shdr->sh_addralign); + if (IS_ERR(dest)) { + ret = PTR_ERR(dest); + goto out_err; + } + codetag_section_found = true; + } else { + dest = mod->mem[type].base + (shdr->sh_entsize & SH_ENTSIZE_OFFSET_MASK); + } if (shdr->sh_type != SHT_NOBITS) { /* @@ -2283,7 +2297,7 @@ static int move_module(struct module *mod, struct load_info *info) if (i == info->index.mod && (WARN_ON_ONCE(shdr->sh_size != sizeof(struct module)))) { ret = -ENOEXEC; - goto out_enomem; + goto out_err; } memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size); } @@ -2299,9 +2313,12 @@ static int move_module(struct module *mod, struct load_info *info) } return 0; -out_enomem: +out_err: for (t--; t >= 0; t--) - module_memory_free(mod, t, true); + module_memory_free(mod, t); + if (codetag_section_found) + codetag_free_module_sections(mod); + return ret; } @@ -2422,6 +2439,8 @@ static struct module *layout_and_allocate(struct load_info *info, int flags) /* Module has been copied to its final place now: return it. */ mod = (void *)info->sechdrs[info->index.mod].sh_addr; kmemleak_load_module(mod, info); + codetag_module_replaced(info->mod, mod); + return mod; } @@ -2431,7 +2450,7 @@ static void module_deallocate(struct module *mod, struct load_info *info) percpu_modfree(mod); module_arch_freeing_init(mod); - free_mod_mem(mod, true); + free_mod_mem(mod); } int __weak module_finalize(const Elf_Ehdr *hdr, diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c index 81e5f9a70f22..f33784f48dd2 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0-only #include +#include #include #include #include @@ -9,6 +10,12 @@ #include static struct codetag_type *alloc_tag_cttype; +static struct alloc_tag_module_section module_tags; +static struct maple_tree mod_area_mt = MTREE_INIT(mod_area_mt, MT_FLAGS_ALLOC_RANGE); +/* A dummy object used to indicate an unloaded module */ +static struct module unloaded_mod; +/* A dummy object used to indicate a module prepended area */ +static struct module prepend_mod; DEFINE_PER_CPU(struct alloc_tag_counters, _shared_alloc_tag); EXPORT_SYMBOL(_shared_alloc_tag); @@ -149,29 +156,198 @@ static void __init procfs_init(void) proc_create_seq("allocinfo", 0400, NULL, &allocinfo_seq_op); } -static bool alloc_tag_module_unload(struct codetag_type *cttype, - struct codetag_module *cmod) +static bool needs_section_mem(struct module *mod, unsigned long size) { - struct codetag_iterator iter = codetag_get_ct_iter(cttype); - struct alloc_tag_counters counter; - bool module_unused = true; - struct alloc_tag *tag; - struct codetag *ct; + return size >= sizeof(struct alloc_tag); +} + +/* Called under RCU read lock */ +static void clean_unused_module_areas(void) +{ + MA_STATE(mas, &mod_area_mt, 0, module_tags.size); + struct module *val; + + mas_for_each(&mas, val, module_tags.size) { + if (val == &unloaded_mod) { + struct alloc_tag *tag; + struct alloc_tag *last; + bool unused = true; + + tag = (struct alloc_tag *)(module_tags.start_addr + mas.index); + last = (struct alloc_tag *)(module_tags.start_addr + mas.last); + while (tag <= last) { + struct alloc_tag_counters counter; + + counter = alloc_tag_read(tag); + if (counter.bytes) { + unused = false; + break; + } + tag++; + } + if (unused) { + mtree_store_range(&mod_area_mt, mas.index, + mas.last, NULL, GFP_KERNEL); + } + } + } +} + +static void *reserve_module_tags(struct module *mod, unsigned long size, + unsigned int prepend, unsigned long align) +{ + unsigned long section_size = module_tags.end_addr - module_tags.start_addr; + MA_STATE(mas, &mod_area_mt, 0, section_size - 1); + bool cleanup_done = false; + unsigned long offset; + void *ret; + + /* If no tags return NULL */ + if (size < sizeof(struct alloc_tag)) + return NULL; + + /* + * align is always power of 2, so we can use IS_ALIGNED and ALIGN. + * align 0 or 1 means no alignment, to simplify set to 1. + */ + if (!align) + align = 1; + + rcu_read_lock(); +repeat: + /* Try finding exact size and hope the start is aligned */ + if (mas_empty_area(&mas, 0, section_size - 1, prepend + size)) + goto cleanup; + + if (IS_ALIGNED(mas.index + prepend, align)) + goto found; + + /* Try finding larger area to align later */ + mas_reset(&mas); + if (!mas_empty_area(&mas, 0, section_size - 1, + size + prepend + align - 1)) + goto found; + +cleanup: + /* No free area, try cleanup stale data and repeat the search once */ + if (!cleanup_done) { + clean_unused_module_areas(); + cleanup_done = true; + mas_reset(&mas); + goto repeat; + } else { + ret = ERR_PTR(-ENOMEM); + goto out; + } + +found: + offset = mas.index; + offset += prepend; + offset = ALIGN(offset, align); + + if (mtree_insert_range(&mod_area_mt, offset, offset + size - 1, mod, + GFP_KERNEL)) { + ret = ERR_PTR(-ENOMEM); + goto out; + } + + if (offset != mas.index) { + if (mtree_insert_range(&mod_area_mt, mas.index, offset - 1, + &prepend_mod, GFP_KERNEL)) { + mtree_store_range(&mod_area_mt, offset, offset + size - 1, + NULL, GFP_KERNEL); + ret = ERR_PTR(-ENOMEM); + goto out; + } + } + + if (module_tags.size < offset + size) + module_tags.size = offset + size; + + ret = (struct alloc_tag *)(module_tags.start_addr + offset); +out: + rcu_read_unlock(); + + return ret; +} + +static void release_module_tags(struct module *mod, bool unused) +{ + MA_STATE(mas, &mod_area_mt, 0, module_tags.size); + unsigned long padding_start; + bool padding_found = false; + struct module *val; + + if (unused) + return; + + unused = true; + rcu_read_lock(); + mas_for_each(&mas, val, module_tags.size) { + struct alloc_tag *tag; + struct alloc_tag *last; + + if (val == &prepend_mod) { + padding_start = mas.index; + padding_found = true; + continue; + } - for (ct = codetag_next_ct(&iter); ct; ct = codetag_next_ct(&iter)) { - if (iter.cmod != cmod) + if (val != mod) { + padding_found = false; continue; + } + + tag = (struct alloc_tag *)(module_tags.start_addr + mas.index); + last = (struct alloc_tag *)(module_tags.start_addr + mas.last); + while (tag <= last) { + struct alloc_tag_counters counter; + + counter = alloc_tag_read(tag); + if (counter.bytes) { + struct codetag *ct = &tag->ct; - tag = ct_to_alloc_tag(ct); - counter = alloc_tag_read(tag); + pr_info("%s:%u module %s func:%s has %llu allocated at module unload\n", + ct->filename, ct->lineno, ct->modname, + ct->function, counter.bytes); + unused = false; + break; + } + tag++; + } + if (unused) { + mtree_store_range(&mod_area_mt, + padding_found ? padding_start : mas.index, + mas.last, NULL, GFP_KERNEL); + } else { + /* Release the padding and mark the module unloaded */ + if (padding_found) + mtree_store_range(&mod_area_mt, padding_start, + mas.index - 1, NULL, GFP_KERNEL); + mtree_store_range(&mod_area_mt, mas.index, mas.last, + &unloaded_mod, GFP_KERNEL); + } - if (WARN(counter.bytes, - "%s:%u module %s func:%s has %llu allocated at module unload", - ct->filename, ct->lineno, ct->modname, ct->function, counter.bytes)) - module_unused = false; + break; } + rcu_read_unlock(); +} + +static void replace_module(struct module *mod, struct module *new_mod) +{ + MA_STATE(mas, &mod_area_mt, 0, module_tags.size); + struct module *val; - return module_unused; + rcu_read_lock(); + mas_for_each(&mas, val, module_tags.size) { + if (val != mod) + continue; + + mtree_store_range(&mod_area_mt, mas.index, mas.last, + new_mod, GFP_KERNEL); + break; + } + rcu_read_unlock(); } #ifdef CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT @@ -252,17 +428,46 @@ static void __init sysctl_init(void) static inline void sysctl_init(void) {} #endif /* CONFIG_SYSCTL */ +static unsigned long max_module_alloc_tags __initdata = 100000; + +static int __init set_max_module_alloc_tags(char *arg) +{ + if (!arg) + return -EINVAL; + + return kstrtoul(arg, 0, &max_module_alloc_tags); +} +early_param("max_module_alloc_tags", set_max_module_alloc_tags); + static int __init alloc_tag_init(void) { const struct codetag_type_desc desc = { - .section = "alloc_tags", - .tag_size = sizeof(struct alloc_tag), - .module_unload = alloc_tag_module_unload, + .section = ALLOC_TAG_SECTION_NAME, + .tag_size = sizeof(struct alloc_tag), + .needs_section_mem = needs_section_mem, + .alloc_section_mem = reserve_module_tags, + .free_section_mem = release_module_tags, + .module_replaced = replace_module, }; + unsigned long module_tags_mem_sz; + module_tags_mem_sz = max_module_alloc_tags * sizeof(struct alloc_tag); + pr_info("%lu bytes reserved for module allocation tags\n", module_tags_mem_sz); + + /* Allocate space to copy allocation tags */ + module_tags.start_addr = (unsigned long)execmem_alloc(EXECMEM_MODULE_DATA, + module_tags_mem_sz); + if (!module_tags.start_addr) + return -ENOMEM; + + module_tags.end_addr = module_tags.start_addr + module_tags_mem_sz; + mt_set_in_rcu(&mod_area_mt); alloc_tag_cttype = codetag_register_type(&desc); - if (IS_ERR(alloc_tag_cttype)) + if (IS_ERR(alloc_tag_cttype)) { + execmem_free((void *)module_tags.start_addr); + module_tags.start_addr = 0; return PTR_ERR(alloc_tag_cttype); + } sysctl_init(); procfs_init(); diff --git a/lib/codetag.c b/lib/codetag.c index 5ace625f2328..d602a81bdc03 100644 --- a/lib/codetag.c +++ b/lib/codetag.c @@ -126,6 +126,7 @@ static inline size_t range_size(const struct codetag_type *cttype, } #ifdef CONFIG_MODULES + static void *get_symbol(struct module *mod, const char *prefix, const char *name) { DECLARE_SEQ_BUF(sb, KSYM_NAME_LEN); @@ -155,6 +156,94 @@ static struct codetag_range get_section_range(struct module *mod, }; } +#define CODETAG_SECTION_PREFIX ".codetag." + +/* Some codetag types need a separate module section */ +bool codetag_needs_module_section(struct module *mod, const char *name, + unsigned long size) +{ + const char *type_name; + struct codetag_type *cttype; + bool ret = false; + + if (strncmp(name, CODETAG_SECTION_PREFIX, strlen(CODETAG_SECTION_PREFIX))) + return false; + + type_name = name + strlen(CODETAG_SECTION_PREFIX); + mutex_lock(&codetag_lock); + list_for_each_entry(cttype, &codetag_types, link) { + if (strcmp(type_name, cttype->desc.section) == 0) { + if (!cttype->desc.needs_section_mem) + break; + + down_write(&cttype->mod_lock); + ret = cttype->desc.needs_section_mem(mod, size); + up_write(&cttype->mod_lock); + break; + } + } + mutex_unlock(&codetag_lock); + + return ret; +} + +void *codetag_alloc_module_section(struct module *mod, const char *name, + unsigned long size, unsigned int prepend, + unsigned long align) +{ + const char *type_name = name + strlen(CODETAG_SECTION_PREFIX); + struct codetag_type *cttype; + void *ret = NULL; + + mutex_lock(&codetag_lock); + list_for_each_entry(cttype, &codetag_types, link) { + if (strcmp(type_name, cttype->desc.section) == 0) { + if (WARN_ON(!cttype->desc.alloc_section_mem)) + break; + + down_write(&cttype->mod_lock); + ret = cttype->desc.alloc_section_mem(mod, size, prepend, align); + up_write(&cttype->mod_lock); + break; + } + } + mutex_unlock(&codetag_lock); + + return ret; +} + +void codetag_free_module_sections(struct module *mod) +{ + struct codetag_type *cttype; + + mutex_lock(&codetag_lock); + list_for_each_entry(cttype, &codetag_types, link) { + if (!cttype->desc.free_section_mem) + continue; + + down_write(&cttype->mod_lock); + cttype->desc.free_section_mem(mod, true); + up_write(&cttype->mod_lock); + } + mutex_unlock(&codetag_lock); +} + +void codetag_module_replaced(struct module *mod, struct module *new_mod) +{ + struct codetag_type *cttype; + + mutex_lock(&codetag_lock); + list_for_each_entry(cttype, &codetag_types, link) { + if (!cttype->desc.module_replaced) + continue; + + down_write(&cttype->mod_lock); + cttype->desc.module_replaced(mod, new_mod); + up_write(&cttype->mod_lock); + } + mutex_unlock(&codetag_lock); +} + static int codetag_module_init(struct codetag_type *cttype, struct module *mod) { struct codetag_range range; @@ -212,13 +301,12 @@ void codetag_load_module(struct module *mod) mutex_unlock(&codetag_lock); } -bool codetag_unload_module(struct module *mod) +void codetag_unload_module(struct module *mod) { struct codetag_type *cttype; - bool unload_ok = true; if (!mod) - return true; + return; mutex_lock(&codetag_lock); list_for_each_entry(cttype, &codetag_types, link) { @@ -235,18 +323,17 @@ bool codetag_unload_module(struct module *mod) } if (found) { if (cttype->desc.module_unload) - if (!cttype->desc.module_unload(cttype, cmod)) - unload_ok = false; + cttype->desc.module_unload(cttype, cmod); cttype->count -= range_size(cttype, &cmod->range); idr_remove(&cttype->mod_idr, mod_id); kfree(cmod); } up_write(&cttype->mod_lock); + if (found && cttype->desc.free_section_mem) + cttype->desc.free_section_mem(mod, false); } mutex_unlock(&codetag_lock); - - return unload_ok; } #else /* CONFIG_MODULES */ diff --git a/scripts/module.lds.S b/scripts/module.lds.S index 3f43edef813c..711c6e029936 100644 --- a/scripts/module.lds.S +++ b/scripts/module.lds.S @@ -50,7 +50,7 @@ SECTIONS { .data : { *(.data .data.[0-9a-zA-Z_]*) *(.data..L*) - CODETAG_SECTIONS() + MOD_CODETAG_SECTIONS() } .rodata : { @@ -59,9 +59,10 @@ SECTIONS { } #else .data : { - CODETAG_SECTIONS() + MOD_CODETAG_SECTIONS() } #endif + MOD_SEPARATE_CODETAG_SECTIONS() } /* bring in arch-specific sections */ From patchwork Mon Aug 19 15:15:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13768489 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 A26ABC3DA4A for ; Mon, 19 Aug 2024 15:15:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3EBDC6B008A; Mon, 19 Aug 2024 11:15:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 39D716B008C; Mon, 19 Aug 2024 11:15:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1EFB06B0092; Mon, 19 Aug 2024 11:15:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id F1D866B008C for ; Mon, 19 Aug 2024 11:15:21 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id A79E141376 for ; Mon, 19 Aug 2024 15:15:21 +0000 (UTC) X-FDA: 82469343642.12.3A64F81 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf07.hostedemail.com (Postfix) with ESMTP id D34B740015 for ; Mon, 19 Aug 2024 15:15:19 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=tJtqginb; spf=pass (imf07.hostedemail.com: domain of 3hmHDZgYKCJMFHE1Ay3BB381.zB985AHK-997Ixz7.BE3@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3hmHDZgYKCJMFHE1Ay3BB381.zB985AHK-997Ixz7.BE3@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=1724080504; 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=VNxQmk8ltcvVRdy69Ay4z75/w4T2K+JdAEqULautUD8=; b=7zhAr1Pdlr1QcIxeJmO+8ZC872kot0FPkKbXq+c9CagAe1QoZty8wsW7Fv+rrNVqHGhvUX TuJXMvezTg+ABmHm0aLnMD6OocK6nXWGB3ByZqs4JLLNleMa1UfObc00Lmu59CbZYHboeH anWyvYxRI54StRCNfZ9l/s6EDrA1fvs= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=tJtqginb; spf=pass (imf07.hostedemail.com: domain of 3hmHDZgYKCJMFHE1Ay3BB381.zB985AHK-997Ixz7.BE3@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3hmHDZgYKCJMFHE1Ay3BB381.zB985AHK-997Ixz7.BE3@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724080504; a=rsa-sha256; cv=none; b=ZRXg6M9E3Cg/BcVPaJR+hTpZVy614FDJgOPLgzRDgEPnGc0kTD55IAmSJ53vcQqmNgBy8u dALEYByML+eMlVyarHGLRtr4aO7paaD3iQsqK0nFQ22B6JR3B+1h8aBvSqHAYc5TAZbVcU mc5C1OZv+Wl+rTBWl7cGJMj91LFKhOc= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6ad660add0fso56110637b3.0 for ; Mon, 19 Aug 2024 08:15:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1724080519; x=1724685319; 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=VNxQmk8ltcvVRdy69Ay4z75/w4T2K+JdAEqULautUD8=; b=tJtqginbwzV/8hyqK9RM/a0lFL2oZtE+ANAqwXvHRfeJPnulYPorurSqgIWxDc493K WZaNQe8ILQsZE0Oqx4W926V/rdYaFrLu1MLT+vsO7K4YssqwOLuC5GKijffBmE3OSofQ Y+R8kj9FyEmERwzxR6Id8XPkrd3EFF8XTdXh8yMGvd9nXuhmFRQhKM2q5mS3y2DUt9dT +N8Q/sUA1vro5VyeroNklRC6jbUPWagQ6yY0+wGuiUgOB4AxCRc/HpzSvhzfJ+a2lBQI 6ZxD4sofv/P2Vz0w51RawPpSpDhNryMa4G9t8lGja5Zc4Rw5Ies8durwwSgnJjUCpvsg Z4RQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724080519; x=1724685319; 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=VNxQmk8ltcvVRdy69Ay4z75/w4T2K+JdAEqULautUD8=; b=FQ5HRXkhfGCdef03oY7hGaKESFWVkCkfiCRrPwyWa/qrw2hhzoDFr/e0iHlKKSwRRk NUUOhGHn8e/eZvFoTZBuvh98czvI7LqvPn4HjFqwBvTRy4cXAT9NxYZule0hNKiow41K OK+A0CgHlRF6Z4Oz7umvtH+1rtj5ZnJyJRj6H1UdIxGmfmT5ZmR2/6id7mn9LHYz775a +25D5eqygw5NCIhywRKceFyp8GM0qBrhtuAiZGW/UMlBv/05to1FTNwJ+K4qOeU8Fz7y 1ryPwU6Z8TzMGk2MkCDM5m5DxDB2LGwbtvM1LEvlTvSPtSnjCA9vF6rQaHVDKgeksmIT N+Wg== X-Forwarded-Encrypted: i=1; AJvYcCU1qnokCsXYrOinwtp6yvzV8kU3icwfNgwU+MOeEvbffI8jt3ENcI52zP1bJuPEdjtkf965+LnEtQ==@kvack.org X-Gm-Message-State: AOJu0Yxl0IhbZEq6UHBvjfoRbvYyKtrgSB4dZgMwNjX4xGWUXirsOThS QzyPqTYMEwK5wQKbhAve+AXT3hTznWrtsWqOWH/PT8UZ3eQ5ihctUBT2MzLHBCM4HNUynnbctQF nEg== X-Google-Smtp-Source: AGHT+IFc3XJvlFR9abCJBzx6LfnqtUZrq0wbNXmeGLgIyv9XIwJOTdcKULoLpjDTZrzYGsBBSDXaDMNiqLo= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:5aea:cf26:50f4:76db]) (user=surenb job=sendgmr) by 2002:a05:690c:b8e:b0:648:2f1d:1329 with SMTP id 00721157ae682-6b1d38cac51mr8818697b3.1.1724080518814; Mon, 19 Aug 2024 08:15:18 -0700 (PDT) Date: Mon, 19 Aug 2024 08:15:08 -0700 In-Reply-To: <20240819151512.2363698-1-surenb@google.com> Mime-Version: 1.0 References: <20240819151512.2363698-1-surenb@google.com> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240819151512.2363698-3-surenb@google.com> Subject: [PATCH 2/5] alloc_tag: eliminate alloc_tag_ref_set From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: kent.overstreet@linux.dev, corbet@lwn.net, arnd@arndb.de, mcgrof@kernel.org, rppt@kernel.org, paulmck@kernel.org, thuth@redhat.com, tglx@linutronix.de, bp@alien8.de, xiongwei.song@windriver.com, ardb@kernel.org, david@redhat.com, vbabka@suse.cz, mhocko@suse.com, hannes@cmpxchg.org, roman.gushchin@linux.dev, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, pasha.tatashin@soleen.com, souravpanda@google.com, keescook@chromium.org, dennis@kernel.org, jhubbard@nvidia.com, yuzhao@google.com, vvvvvv@google.com, rostedt@goodmis.org, iamjoonsoo.kim@lge.com, rientjes@google.com, minchan@google.com, kaleshsingh@google.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, kernel-team@android.com, surenb@google.com X-Rspam-User: X-Stat-Signature: sbpgwntaiwt8hh41675fx9pf7o4xy3tm X-Rspamd-Queue-Id: D34B740015 X-Rspamd-Server: rspam11 X-HE-Tag: 1724080519-299301 X-HE-Meta: U2FsdGVkX1/WTEGHRkwd5326eqxP2GWQ9d3wjRfSbnf5mCwPle1rrsjgyewUDc1lZGfUaaX+bWHQEb0mSolfK1qvipau6FAsiKfNSFFk6tDMENdXVmLhenY6fg0jHMwFCBeLENtGdy/KgGsIUt+HFlJG8WzVv0Z4A3cMzDq+SdYFIUW6ZdI/kWJoXEGqYDqI7ECubRfFmqLorb0xtrz+mqMIN5lALabTTiKyyKc5aJ6OJDxQVvzI6HTFfnfltxl9wvk/wkevMxeQlVljbCPzX3CcECsxtCKBPmkBV4LiWKiTnXFaGcSJGaMLFBePF4kr8gZ2aGssn25KDEHtWGwA901mtcWu5bYwooaM0ko1izra02Ok+dt96Fqztbkl56FyUkeGTKeZlGjLZGMVy3e5eGhmgwFYSd8K7ZTh3DYDon5jFEgsXE6u0w4O7TktM8yKkQ08oL/wRtH7QPuDIFmWo+6kUwrXaKDfGHVAuc2CsR6GOKxpA7nSe/MifUJnSFXwf0MduOqEULY3/8DeR/XD5yRB2+MQ3dbBI/JtlYraiUU5vTqcNP9QRii9Ti5piuLltnVKg4+QCi6nIHNmKHnGYE+CZxW4bHClHEBFd8FyNwnnnFoAki0qgLx1rNfvdw+2jwH1xIpC6eGTo5UeR/B0A35mvCNhmHVBrhMowTrVo4K5WeydaP6Ak91CafiBhHA+tif5sdDGmBAHwEAzL0zmDXF0ILpRr5C+icj35dJ9DI8x9hf4Zesp1coH95ovY8sXcLoZnPbfffjn7hjvxGDp6tRrnZk/WhPcB+MDxm5yyJoucuzWs6xdUVN1tXaWiZBxJwnaAYO5LrGqK4x/zs+wPnndGlAsuVT77IhH64rjvNf4eXlbmmKDNoHtH3SA9Osp6AEVqdwTPupFqDYkblJl58Gl4QE8P7F/XJjq9wtwapz+FyzhXtSiFruXuLz+xJg4rjQZl6IfL59/l1sZVn2 JEllruMq iWrNWscY5fuS8JTSU80NigIdPY361iH66hDUzyT5f2TPM5NT9lSakzROQxfiy+WcWlCd0HuP/lR4lWuwS+n08xJq68ebVgb+nsnKMxrX+f+JAAcjEhCSqXALr17PzY6HCquMmuEwyueqR/31K7tviNghQLFvf+Uhceid0fxM7MIhcEhQ8yaMUjXzAZh1UW6yXAlRunhaPi7Y4Jb4SEwkOuaSbBtaNNrwDTuUOVlVct5UsslY3e4cUxNj9L/y9GdZ9RD/i0YdXpPPSPs3UazUZjiiKddKRT6CZ8mmCjRdZvjcI1H+IMB1/3GshLZLZpcUtaBnlVnp5BHFo2FsDKcfNERt1VIvE+bQ9P8jpIxl1SaXijEUnBExzmH8D7m8UGig8wV+ztY1IJc2fJP066IuLNM4dyHIoQgfQrXgjW9yWThAeUPXbl+WjQWkNFB7gwJtYLaidOE9BHF1wpA4OtMkpjYUMaeH938qtjXdqgswBSOsks+12cda8hAWVGEo+bxWikSiKpfHahZrDegzA3vgehnLMfEHXNMLK7z6jxqWGATA5d5SRlZmeVogRNrgB40rTCjUmfvObSHhIZSF74Dig0nx13OutUYjmlKvV X-Bogosity: Ham, tests=bogofilter, spamicity=0.000005, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: To simplify further refactoring, open-code the only two callers of alloc_tag_ref_set(). Signed-off-by: Suren Baghdasaryan --- include/linux/alloc_tag.h | 25 ++----------------------- include/linux/pgalloc_tag.h | 12 +++++++++++- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/include/linux/alloc_tag.h b/include/linux/alloc_tag.h index 99cbc7f086ad..21e3098220e3 100644 --- a/include/linux/alloc_tag.h +++ b/include/linux/alloc_tag.h @@ -143,36 +143,15 @@ static inline void alloc_tag_add_check(union codetag_ref *ref, struct alloc_tag static inline void alloc_tag_sub_check(union codetag_ref *ref) {} #endif -/* Caller should verify both ref and tag to be valid */ -static inline void __alloc_tag_ref_set(union codetag_ref *ref, struct alloc_tag *tag) -{ - ref->ct = &tag->ct; - /* - * We need in increment the call counter every time we have a new - * allocation or when we split a large allocation into smaller ones. - * Each new reference for every sub-allocation needs to increment call - * counter because when we free each part the counter will be decremented. - */ - this_cpu_inc(tag->counters->calls); -} - -static inline void alloc_tag_ref_set(union codetag_ref *ref, struct alloc_tag *tag) -{ - alloc_tag_add_check(ref, tag); - if (!ref || !tag) - return; - - __alloc_tag_ref_set(ref, tag); -} - static inline void alloc_tag_add(union codetag_ref *ref, struct alloc_tag *tag, size_t bytes) { alloc_tag_add_check(ref, tag); if (!ref || !tag) return; - __alloc_tag_ref_set(ref, tag); + ref->ct = &tag->ct; this_cpu_add(tag->counters->bytes, bytes); + this_cpu_inc(tag->counters->calls); } static inline void alloc_tag_sub(union codetag_ref *ref, size_t bytes) diff --git a/include/linux/pgalloc_tag.h b/include/linux/pgalloc_tag.h index 207f0c83c8e9..244a328dff62 100644 --- a/include/linux/pgalloc_tag.h +++ b/include/linux/pgalloc_tag.h @@ -103,7 +103,17 @@ static inline void pgalloc_tag_split(struct page *page, unsigned int nr) page_ext = page_ext_next(page_ext); for (i = 1; i < nr; i++) { /* Set new reference to point to the original tag */ - alloc_tag_ref_set(codetag_ref_from_page_ext(page_ext), tag); + ref = codetag_ref_from_page_ext(page_ext); + alloc_tag_add_check(ref, tag); + if (ref) { + ref->ct = &tag->ct; + /* + * We need in increment the call counter every time we split a + * large allocation into smaller ones because when we free each + * part the counter will be decremented. + */ + this_cpu_inc(tag->counters->calls); + } page_ext = page_ext_next(page_ext); } out: From patchwork Mon Aug 19 15:15:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13768490 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 BBFFDC5320E for ; Mon, 19 Aug 2024 15:15:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3EBDE6B008C; Mon, 19 Aug 2024 11:15:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 39BAC6B0092; Mon, 19 Aug 2024 11:15:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1EE276B0093; Mon, 19 Aug 2024 11:15:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id F3DE96B008C for ; Mon, 19 Aug 2024 11:15:23 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id BA0A5A13A1 for ; Mon, 19 Aug 2024 15:15:23 +0000 (UTC) X-FDA: 82469343726.16.B6C2FCC Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf04.hostedemail.com (Postfix) with ESMTP id DC6C440006 for ; Mon, 19 Aug 2024 15:15:21 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="h/H9P7l/"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf04.hostedemail.com: domain of 3iGHDZgYKCJUHJG3C05DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3iGHDZgYKCJUHJG3C05DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724080435; 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=JgTjJwDNOOBGmN3SYEoTtFKEJl/MBoFNsUNbiHHZqOc=; b=48RMXEHtTofDekt7vsvEO3xUwkuhw+Bqgo2nRzbjqC7lgNweoL4YHF8X7c1GiabcGiezhh wtp1nFtkVfZJ/1jYaBA/wgSfP6tQZeiV/8MkuHSlv2nVYAOQWMe/zad/NRS1mSbDnEa8D5 7O7l7k8wcqeUOEwEuG9Yw3MS0plX8S4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724080435; a=rsa-sha256; cv=none; b=X6xL3bY+AgrxLOkWCzJnr12V3izPZXYwuaIsd5FbYfiS8Fm57UFfZYh408GFS4SSkNAB7l TO6+P+8VwZOKKZ01iLIzkXbk2fNDn+8109l0vznEOT2gB53axVf0gUAYx5S/mBC67H/MXQ ln5Wa+SoxYOiGCjyj+KIne1eoLAgNV4= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="h/H9P7l/"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf04.hostedemail.com: domain of 3iGHDZgYKCJUHJG3C05DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3iGHDZgYKCJUHJG3C05DD5A3.1DBA7CJM-BB9Kz19.DG5@flex--surenb.bounces.google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6adcb88da08so88462947b3.3 for ; Mon, 19 Aug 2024 08:15:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1724080521; x=1724685321; 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=JgTjJwDNOOBGmN3SYEoTtFKEJl/MBoFNsUNbiHHZqOc=; b=h/H9P7l/ze4L1ylPdOLHnqMnWxWfwefgcfcOnHMXHYI6qRHgDQ8mxRC2o5PbDNTB92 pjOkGJcauj57iqFYT30Hvl1dmcDpzH4Ztm7p59AhttIYkX9W2LUtIUKxubGjWMtWg0JO mTv0AOWtaD6YQQlLzxrCUDRwWweGXBnn97UpUgtX4A8ZQ3KV2RWkes1OpY1kRyQbsxbC rhGPE2Exi8ZKKsx75IG6zGJlmuAhobPFcrN1FBL0W5HDptDr4Cba8OSd2dXunNJNFLdw +13ZCWalR8WWzPQ4lup9fVMz69QWHSRwv14PAmcaE3pBW3lWbk/CRvnCfVeAFSQD6TEt fWzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724080521; x=1724685321; 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=JgTjJwDNOOBGmN3SYEoTtFKEJl/MBoFNsUNbiHHZqOc=; b=QDuI5Y+EqDVn8g6uI8c+71MMZ4gdkfa3YuRmsEArjfrZjFldDYYEKWJvulL9VKa30U V+pKgg1za3p8wnJ5oeNX8P/Lszd/nbG/JixDc5hJJzW8m4mZOO1cSTbePKw2z2Y+qvaE rwRzD8v6XLPKocgqE11K5FeBnIyuW5u+4gCZdOOFdGHr7tbNgM8hiN6rao5+rydt6Mhw Z45ikryr9ul+Ud1MBfAaKpYrdMs/lHTT5winCBSXDm0aQF1GgSFM5PL8hc/WMyjKlM9M Sm1DsVxRxoa3CNkkwXTHZ5KHD0pesyEd3an/sPyE9hV0TzzzKpm3uklVYRkjjTz3iCIb QUew== X-Forwarded-Encrypted: i=1; AJvYcCUkO/jZvkssVCSE81uOUEqf13WZnmNUodlbaFp3Q72tuW0eWwRnm2CFIn63dQXfutPBx4tArtSP6W7B/+dJtP8cLPM= X-Gm-Message-State: AOJu0Ywgw6hILtg3bwJE0Mxz+U11QJonZ97836Fq256nDH7x8m95pxS+ 7Dgcm5F5wM/rgy5aRYPRnIBlQo3cSE/p2Yfv5KTNgTvbNclolaxakGniJLI2UlrhiV/l0/wi0x6 QAw== X-Google-Smtp-Source: AGHT+IF6wQq4hojn2WvBl6Ky3Y5Ec+1miO/ZVJTIsvzHPNXwtXX0T5qfBPLHg5VMXezc7PRztRYvs2+xsUs= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:5aea:cf26:50f4:76db]) (user=surenb job=sendgmr) by 2002:a05:690c:4e0e:b0:64a:d1b0:4f24 with SMTP id 00721157ae682-6b1bba55dd5mr2194777b3.7.1724080520869; Mon, 19 Aug 2024 08:15:20 -0700 (PDT) Date: Mon, 19 Aug 2024 08:15:09 -0700 In-Reply-To: <20240819151512.2363698-1-surenb@google.com> Mime-Version: 1.0 References: <20240819151512.2363698-1-surenb@google.com> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240819151512.2363698-4-surenb@google.com> Subject: [PATCH 3/5] alloc_tag: introduce pgalloc_tag_ref to abstract page tag references From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: kent.overstreet@linux.dev, corbet@lwn.net, arnd@arndb.de, mcgrof@kernel.org, rppt@kernel.org, paulmck@kernel.org, thuth@redhat.com, tglx@linutronix.de, bp@alien8.de, xiongwei.song@windriver.com, ardb@kernel.org, david@redhat.com, vbabka@suse.cz, mhocko@suse.com, hannes@cmpxchg.org, roman.gushchin@linux.dev, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, pasha.tatashin@soleen.com, souravpanda@google.com, keescook@chromium.org, dennis@kernel.org, jhubbard@nvidia.com, yuzhao@google.com, vvvvvv@google.com, rostedt@goodmis.org, iamjoonsoo.kim@lge.com, rientjes@google.com, minchan@google.com, kaleshsingh@google.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, kernel-team@android.com, surenb@google.com X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: DC6C440006 X-Stat-Signature: p4r16nt7je5xhid36ufewdot7j3pxzej X-Rspam-User: X-HE-Tag: 1724080521-921719 X-HE-Meta: U2FsdGVkX18Rao4SgnMPBm/1chzHy+UaO2qLtOrcvTTkXyv2VmkEBX7jLhogD7YM56C9oVEObB6C8AosJDQ6trq1FYaMQhG6DO7cEIp9Jt0lA2hW+K+pwj0VsnNzsOtpbr6EBi/58sEmXJE2CgWsjLLjKANXJiu6c0ADoezP4n4prxy20ltwo6z15DCyt74e7kHxaVw5r62gf8kegBDHKrH9yeGxTAXVxT0rrLxImc4EUlKpKZVk4kjUUfsF2ctZmQB2CzU3MiuJDmaQLQ8Dj7R/lOHmNgNbSrasKA9umD1VIafseJAK7IWdpw7RRNzzGm8lpgRT02sBUN09b0FcBqN1eVESo1tdId2Bjjwa2OwOnipv7zkDFNlSHL84HA7hGDwcZZn0w5R2pTqiSoHf9PYSJVRWF0cMo3zmaM5ityhkxH/FhBLL9EUJxGaMcQkcx7l0eK6LfHrbkMmUY1LvzutCfTcpdDTfW9DFO3ykZd4j/MRvNy2R9DYj0yNbgkO2f1gPCfihF6+YNo+3Xp0gJLT072lz4EvPqWsHSd4ojEDfn8r/AwafA2m7UE6AarWq1qZfG52u1yBDHszHSYLhhm++Lbt86/PbTiL+v18/jJVR3jRHEIqASW+Wec7644/6pb622JRmihdmCnWW6GWOSA95w2HJzilmK2A8sExTtxQF5rGgdVuBeUfWzl1YXllzPeigX8cX+DUNpHjnOArXCAl6Gy2uj/px2Ta/4JETP8LYtODAFTMkxCOB3s9H4H6ndhYTcNgaksNIP70MEzOLkXAapSD/5DSsqnNvLtEg/FMFPLzI/m5r0/+Ih21Rea/1yFVB8qJ3TVnWz20T3lc4TozJiDyVZDmAHQJBosEsXAP3Ul3TfyTtUAuEUnnmSOktKpGQ+JZmCGFGxyOu6q2A7wjyQmQ99mBBvnkDaNEY7iZWI37VAoM2TJ3mby3tV6GfkSUg7WkFi/zvatPHM7g e7quZFou 59rFf4fEeiTA6z7AdKWGF5p+ikeq22rBRNjoP07TaQGmFKALePa9B5M0JO37f5hEVHWNyuY7GJzlUjw/mIlIlW2OWsheBqwHzdnIriR/w/WX7m0L29dQgOlzzGv/GyKxwcDeoStcLxpLOQG+Iar2cDakVZ+X40sb6Q6yqoqMFGNes6sBMJFkF3e2y3BEzzrVQuob0GM9AhIz6oPwcJpCd3wraiLSNkIUBCoF4Sojr6AB1KQpod5vHpKNFmut/8ZO548lL2nKwXv6YC+IIsqwrUYeX0EfX2f2ygixOFeE6Z6yjKn9sLmVBPXgGZZpCH67kI9xKowiX1Zw6RsP2bMZnRWVSj7lQ5OiKFiuBF4Iu/fRIdEOObOvEUcqBrplQ5G/TKRxoo19QC0mO33jyJ1OOB8CVrdbPClmYKKUuBjWWDdCxFTEw+mxmqPVTgcUryy6WRgZCZ0N5m2pOhlwwGNYOYP+Y5aPgl2ikwAsoz9IFHgDjBnYWsEgwmrzHcMb1GnsU+Z+OjVGJ0nCGX07lgMGTEPgO2CYhIey7b1VgNgjS8nEZ3xlf9B4ktvaIivVboS4X3prG 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: To simplify later changes to page tag references, introduce new pgalloc_tag_ref and pgtag_ref_handle types. This allows easy replacement of page_ext as a storage of page allocation tags Signed-off-by: Suren Baghdasaryan --- include/linux/pgalloc_tag.h | 144 +++++++++++++++++++++++------------- lib/alloc_tag.c | 3 +- 2 files changed, 95 insertions(+), 52 deletions(-) diff --git a/include/linux/pgalloc_tag.h b/include/linux/pgalloc_tag.h index 244a328dff62..c76b629d0206 100644 --- a/include/linux/pgalloc_tag.h +++ b/include/linux/pgalloc_tag.h @@ -9,48 +9,76 @@ #ifdef CONFIG_MEM_ALLOC_PROFILING +typedef union codetag_ref pgalloc_tag_ref; + +static inline void read_pgref(pgalloc_tag_ref *pgref, union codetag_ref *ref) +{ + ref->ct = pgref->ct; +} + +static inline void write_pgref(pgalloc_tag_ref *pgref, union codetag_ref *ref) +{ + pgref->ct = ref->ct; +} #include extern struct page_ext_operations page_alloc_tagging_ops; -static inline union codetag_ref *codetag_ref_from_page_ext(struct page_ext *page_ext) +static inline pgalloc_tag_ref *pgref_from_page_ext(struct page_ext *page_ext) { - return (union codetag_ref *)page_ext_data(page_ext, &page_alloc_tagging_ops); + return (pgalloc_tag_ref *)page_ext_data(page_ext, &page_alloc_tagging_ops); } -static inline struct page_ext *page_ext_from_codetag_ref(union codetag_ref *ref) +static inline struct page_ext *page_ext_from_pgref(pgalloc_tag_ref *pgref) { - return (void *)ref - page_alloc_tagging_ops.offset; + return (void *)pgref - page_alloc_tagging_ops.offset; } +typedef pgalloc_tag_ref *pgtag_ref_handle; + /* Should be called only if mem_alloc_profiling_enabled() */ -static inline union codetag_ref *get_page_tag_ref(struct page *page) +static inline pgtag_ref_handle get_page_tag_ref(struct page *page, union codetag_ref *ref) { if (page) { struct page_ext *page_ext = page_ext_get(page); - if (page_ext) - return codetag_ref_from_page_ext(page_ext); + if (page_ext) { + pgalloc_tag_ref *pgref = pgref_from_page_ext(page_ext); + + read_pgref(pgref, ref); + return pgref; + } } return NULL; } -static inline void put_page_tag_ref(union codetag_ref *ref) +static inline void put_page_tag_ref(pgtag_ref_handle pgref) { - if (WARN_ON(!ref)) + if (WARN_ON(!pgref)) return; - page_ext_put(page_ext_from_codetag_ref(ref)); + page_ext_put(page_ext_from_pgref(pgref)); +} + +static inline void update_page_tag_ref(pgtag_ref_handle pgref, union codetag_ref *ref) +{ + if (WARN_ON(!pgref || !ref)) + return; + + write_pgref(pgref, ref); } static inline void clear_page_tag_ref(struct page *page) { if (mem_alloc_profiling_enabled()) { - union codetag_ref *ref = get_page_tag_ref(page); - - if (ref) { - set_codetag_empty(ref); - put_page_tag_ref(ref); + pgtag_ref_handle handle; + union codetag_ref ref; + + handle = get_page_tag_ref(page, &ref); + if (handle) { + set_codetag_empty(&ref); + update_page_tag_ref(handle, &ref); + put_page_tag_ref(handle); } } } @@ -59,11 +87,14 @@ 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); + pgtag_ref_handle handle; + union codetag_ref ref; + + handle = get_page_tag_ref(page, &ref); + if (handle) { + alloc_tag_add(&ref, task->alloc_tag, PAGE_SIZE * nr); + update_page_tag_ref(handle, &ref); + put_page_tag_ref(handle); } } } @@ -71,53 +102,58 @@ static inline void pgalloc_tag_add(struct page *page, struct task_struct *task, 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); + pgtag_ref_handle handle; + union codetag_ref ref; + + handle = get_page_tag_ref(page, &ref); + if (handle) { + alloc_tag_sub(&ref, PAGE_SIZE * nr); + update_page_tag_ref(handle, &ref); + put_page_tag_ref(handle); } } } static inline void pgalloc_tag_split(struct page *page, unsigned int nr) { - int i; - struct page_ext *first_page_ext; - struct page_ext *page_ext; - union codetag_ref *ref; + pgtag_ref_handle first_pgref; + union codetag_ref first_ref; struct alloc_tag *tag; + int i; if (!mem_alloc_profiling_enabled()) return; - first_page_ext = page_ext = page_ext_get(page); - if (unlikely(!page_ext)) + first_pgref = get_page_tag_ref(page, &first_ref); + if (unlikely(!first_pgref)) return; - ref = codetag_ref_from_page_ext(page_ext); - if (!ref->ct) + if (!first_ref.ct) goto out; - tag = ct_to_alloc_tag(ref->ct); - page_ext = page_ext_next(page_ext); + tag = ct_to_alloc_tag(first_ref.ct); for (i = 1; i < nr; i++) { - /* Set new reference to point to the original tag */ - ref = codetag_ref_from_page_ext(page_ext); - alloc_tag_add_check(ref, tag); - if (ref) { - ref->ct = &tag->ct; + pgtag_ref_handle handle; + union codetag_ref ref; + + page++; + handle = get_page_tag_ref(page, &ref); + if (handle) { + /* Set new reference to point to the original tag */ + alloc_tag_add_check(&ref, tag); + ref.ct = &tag->ct; /* * We need in increment the call counter every time we split a * large allocation into smaller ones because when we free each * part the counter will be decremented. */ this_cpu_inc(tag->counters->calls); + update_page_tag_ref(handle, &ref); + put_page_tag_ref(handle); } - page_ext = page_ext_next(page_ext); } out: - page_ext_put(first_page_ext); + put_page_tag_ref(first_pgref); } static inline struct alloc_tag *pgalloc_tag_get(struct page *page) @@ -125,13 +161,15 @@ static inline struct alloc_tag *pgalloc_tag_get(struct page *page) struct alloc_tag *tag = NULL; if (mem_alloc_profiling_enabled()) { - union codetag_ref *ref = get_page_tag_ref(page); - - alloc_tag_sub_check(ref); - if (ref) { - if (ref->ct) - tag = ct_to_alloc_tag(ref->ct); - put_page_tag_ref(ref); + pgtag_ref_handle handle; + union codetag_ref ref; + + handle = get_page_tag_ref(page, &ref); + if (handle) { + alloc_tag_sub_check(&ref); + if (ref.ct) + tag = ct_to_alloc_tag(ref.ct); + put_page_tag_ref(handle); } } @@ -146,8 +184,12 @@ static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr) #else /* CONFIG_MEM_ALLOC_PROFILING */ -static inline union codetag_ref *get_page_tag_ref(struct page *page) { return NULL; } -static inline void put_page_tag_ref(union codetag_ref *ref) {} +typedef void *pgtag_ref_handle; + +static inline pgtag_ref_handle +get_page_tag_ref(struct page *page, union codetag_ref *ref) { return NULL; } +static inline void put_page_tag_ref(pgtag_ref_handle handle) {} +static inline void update_page_tag_ref(pgtag_ref_handle handle, union codetag_ref *ref) {} static inline void clear_page_tag_ref(struct page *page) {} static inline void pgalloc_tag_add(struct page *page, struct task_struct *task, unsigned int nr) {} diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c index f33784f48dd2..a1d80d2ef512 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -397,7 +398,7 @@ static __init void init_page_alloc_tagging(void) } struct page_ext_operations page_alloc_tagging_ops = { - .size = sizeof(union codetag_ref), + .size = sizeof(pgalloc_tag_ref), .need = need_page_alloc_tagging, .init = init_page_alloc_tagging, }; From patchwork Mon Aug 19 15:15:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13768491 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 06BC1C52D7C for ; Mon, 19 Aug 2024 15:15:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CEF0A6B0093; Mon, 19 Aug 2024 11:15:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C4EB56B0096; Mon, 19 Aug 2024 11:15:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9E7876B0099; Mon, 19 Aug 2024 11:15:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 6A7306B0093 for ; Mon, 19 Aug 2024 11:15:26 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 24BD94136C for ; Mon, 19 Aug 2024 15:15:26 +0000 (UTC) X-FDA: 82469343852.21.01E9AC3 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf05.hostedemail.com (Postfix) with ESMTP id 1B22B100009 for ; Mon, 19 Aug 2024 15:15:23 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=ObRppGls; spf=pass (imf05.hostedemail.com: domain of 3imHDZgYKCJcJLI5E27FF7C5.3FDC9ELO-DDBM13B.FI7@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3imHDZgYKCJcJLI5E27FF7C5.3FDC9ELO-DDBM13B.FI7@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=1724080485; 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=gI06ovdfCz8cO9NK2nQDa04h5rjz0kRy+tQsOrEFgfI=; b=2A2Jx7iocSQq+sCleJOGt5lrW4GYD7XlNwThl7X79WArjvaS5IwtvNG0mnI6mJ8a+vvrHv DyfazQ5VI0EkBHFNz2HxwV3fHEFf0NCOMqKE75tU8zZmm5Xg+2iMPSSRKTsRyCb9Wa8rX6 jiOpsSGEduNRxXUwFoSiVYfp9BfaYCg= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=ObRppGls; spf=pass (imf05.hostedemail.com: domain of 3imHDZgYKCJcJLI5E27FF7C5.3FDC9ELO-DDBM13B.FI7@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3imHDZgYKCJcJLI5E27FF7C5.3FDC9ELO-DDBM13B.FI7@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724080485; a=rsa-sha256; cv=none; b=DyCXt70GJ99H8Tz5MfF0kBiE7wra7nxCRCV2G9nlV3Ei/cY6jfP29gpEmM4DoEjvQwEx0y 6EsFjckgnJoSvk/Ex9nZ9NE/RmlZZQ/zPZWq/V/tR+yoQWlJyesgSsFUcZYw2vPLmHVzI+ a2mgXxw/o1QbMdI+WyN8mp83YFvbf9o= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6b73fb4ff56so26652557b3.0 for ; Mon, 19 Aug 2024 08:15:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1724080523; x=1724685323; 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=gI06ovdfCz8cO9NK2nQDa04h5rjz0kRy+tQsOrEFgfI=; b=ObRppGlshRcy8VP9cRAvUN8/7U7w4qJjlOuVPgCF9EOVs7Bc/qemV9fnsVQDDMZHk0 7kokLTy/+Cvqf4+5qrf9dv+jIaPJFBkqeDoDKfmmmr4JjNym3pImhEqBubA+cxcfu+ZF 1iOIkutHoFFNE/CFhblj6QlAZQn69ju9722mW20/OurmbB/FAc115WPG/Wn+I/Z5mClR jfTesKwvtRYLrWptDosJPkw5vFHL5zOGhkYxTGfzp4BCTw9H9I97tz935AwteK5A2OfW rhHXf1DUzJgtZEenNh6lFzXycxjNnzTLtCuQmX06MJYIgfjXZKbtmST5Aqd+ZjV+RUGK zn6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724080523; x=1724685323; 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=gI06ovdfCz8cO9NK2nQDa04h5rjz0kRy+tQsOrEFgfI=; b=VTLzI2LtZ+ln2NfA+Z6THA9dgg9JJqGQGprMpyaY0W9eimJIkG5cQH2snTOFdn8o0+ dM6isHscC7Et+2bNNJJ9EX5arouzZNfZC5iyVSUtJGG6g5a8fYbLr0mmidk1iGLXRwln db0Xlg3uEtHa1LnZQxBVvaibqp8YU6sR2yrEmpf5WpqCjb48gP04ymal98eYAS6UvdQZ tKQf6+NtqvBSYczzrxHvu7UTpyXnrvggHwCjJkfG/8Df++ZUzgpcW2VUQXKK/p440qEs Wep2UrwKZ37+c80FlrxxGZNtn1bozKFMp7UE+Zp3QEP2bYx+mJf+xRzY3HgDji37ygjc aveA== X-Forwarded-Encrypted: i=1; AJvYcCUWFJbs/c6gccZukAXF+tEyA7zy1+UEPqMhs10yzKJ4YVq1ORrT8VSNg3hVWkSEAMxowKeOuLOdeo8NaLUvQjhTrSw= X-Gm-Message-State: AOJu0YxBrQ9928oU7aEafMaYS83gN7LE4gV43OkcRuF1UJiPRs9L/DFJ UripeJyOa+mqWtr/UQd2vtfT8EVk3XwJhJnAJFThpmkAJeX+rxuHJU6/DuWS8ZvEtOCe9DyBMS9 oIA== X-Google-Smtp-Source: AGHT+IG/BKrJXqYP6cRYhE+Lu+8cVFetj3uIJoL3u5FtbKpk/WoSJ/pAYSGnYrr9RqZE8k4lDy0dOBXbaE4= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:5aea:cf26:50f4:76db]) (user=surenb job=sendgmr) by 2002:a05:690c:c04:b0:6b2:6cd4:7f9a with SMTP id 00721157ae682-6b26cd48046mr5942197b3.8.1724080522969; Mon, 19 Aug 2024 08:15:22 -0700 (PDT) Date: Mon, 19 Aug 2024 08:15:10 -0700 In-Reply-To: <20240819151512.2363698-1-surenb@google.com> Mime-Version: 1.0 References: <20240819151512.2363698-1-surenb@google.com> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240819151512.2363698-5-surenb@google.com> Subject: [PATCH 4/5] alloc_tag: make page allocation tag reference size configurable From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: kent.overstreet@linux.dev, corbet@lwn.net, arnd@arndb.de, mcgrof@kernel.org, rppt@kernel.org, paulmck@kernel.org, thuth@redhat.com, tglx@linutronix.de, bp@alien8.de, xiongwei.song@windriver.com, ardb@kernel.org, david@redhat.com, vbabka@suse.cz, mhocko@suse.com, hannes@cmpxchg.org, roman.gushchin@linux.dev, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, pasha.tatashin@soleen.com, souravpanda@google.com, keescook@chromium.org, dennis@kernel.org, jhubbard@nvidia.com, yuzhao@google.com, vvvvvv@google.com, rostedt@goodmis.org, iamjoonsoo.kim@lge.com, rientjes@google.com, minchan@google.com, kaleshsingh@google.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, kernel-team@android.com, surenb@google.com X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 1B22B100009 X-Stat-Signature: zjdjhjn6wi4ffeqsjrosf8cm4rt3adzk X-HE-Tag: 1724080523-405218 X-HE-Meta: U2FsdGVkX1+gxaXwXoOWcXiNXD+4OwdRb8+bQ3WASmWoqe/hxXp6kPA38LLL6jr15/dYgzD/5mbwIhwiwKaYfuD29J0EZ/W5gIJg2NQsREdeY1IHiQmtKbktibJOx/cvnE3YWQYLRLUhc6KZILHhi94sF+9p8QIyv/r1ArZdmJ6Dme3c3UbLRg5TG1WpgaRZeznIeocDniXmwCGJ4WLvONy8fAxICX8kkI2pg6HwBgnv9fr/uyENfu9cyJf/ob2248yk5d5tzsihPcLWN1UOjiBpH0/15lvaZHs0skM7JgO9oAs6pdQGyRkq3xaY8WvE/PMOU171AVhTm7TOkzCTYCQPsFuFzZz6dyaZq0giNm2a8vYvwJLBtXy2HWkTgabH8lYY1VYVT8njyBnyugqnBlvim9gHAHMWBmjq63SNRSSkAHMnizQZRYPawYx1M9RxBcPjBGsoo2EGmPUC3T/plCedGVDZ6YCWJT8LskZvyfIkT2/Tekx84LF1Rd1p96xtCro1PtXv4kdEYP4tNPIanKuimoKZMnCMB+dS90XXVNbFpR30E/CJwzhoMpbq9wNEZWyF0N2HE0KwyYwyCYXABX+D0JS5IGhFd/9OXLOjWS/AOAE78kS2nrqPY4GZbLI+4CF/Tw1AaMJW+A/9oKwnYQ4Ee0ZIGLauTZ9pExt43OV7AF8KaOnr7LJfwDtxcfuULZz71vRS4/Kl1noSvzh0hXgsyMcPcfENievuanQB2JKt6MhT66kBFR2V3pXa5Hv7baankXx7qEBpd+cdHNSZIa+BGPIFMI6iae1eKIubK/BXamLccJd/YBPl2G5OkZN5nEkpmLpdJY7dEdI82PZX+AtJqWUPz1fj2pY1Y+oi2wcgiQsTNnZ1QGpsAOrWai8nTBkQOlhgu+bKeDt1KhiqhfYp3z7lkHlVvU/aIDT3BT5PUcrv5hxjKLdcpV26dKAIPLV0g+yhJicR46EjK2H HAd0DQZv wKZhqWujzO8M2CAOkww9PnhDUjVA3u/GgpmlSRztfhapMlUq2sMc7DwuGF0zfiSTXrL09e5P2ppf/+Q6KW0TemVIVmZWP4Sdh51CKhQRr68gEuKlFeUZp+N6UW0g1lI3Qc0aW3QtUFol5gZjlZ6kkgiW74BbkfuVKsfqrOHEKt1aFWLkc4ldFAn8hT9x+G8Ilct4ucd+cv4wB8QNjj9SXXiOLeG3fCerWAANZ0sadL1zFbjZD6WK4yoEarJpen2p2FsZO7xD4EqHl3iXzZpQ+acapf13lts65AhSf8UgApjwn2OBltPTfFzTX+yrPIGi5FpSw2nz9+EvV/nZgeEgnLWThKgr8JqaidbdPjv36wuCHGIbI6fvPz4YnE00m3HrJgzLsGIgCaIOiq3HQZqSFhcDuP7/x1gPI2n9wCJLvsruAsEiXpAwvd+/jh7QPamHjQKQ2yb9Ek5zEASHf51r2b1MdZiC75/Md88MJbBacv6dzx3tfCSChw40ICM5n7x5wogtzQuof/CPzV2k6uqDOT4UghVjy37pG3OC4buxQwpPFjbY9WCHC9jzipXbDQSoHnith 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 CONFIG_PGALLOC_TAG_REF_BITS to control the size of the page allocation tag references. When the size is configured to be less than a direct pointer, the tags are searched using an index stored as the tag reference. Signed-off-by: Suren Baghdasaryan --- include/linux/alloc_tag.h | 10 +++++- include/linux/codetag.h | 3 ++ include/linux/pgalloc_tag.h | 69 +++++++++++++++++++++++++++++++++++++ lib/Kconfig.debug | 11 ++++++ lib/alloc_tag.c | 50 ++++++++++++++++++++++++++- lib/codetag.c | 4 +-- mm/mm_init.c | 1 + 7 files changed, 144 insertions(+), 4 deletions(-) diff --git a/include/linux/alloc_tag.h b/include/linux/alloc_tag.h index 21e3098220e3..b5cf24517333 100644 --- a/include/linux/alloc_tag.h +++ b/include/linux/alloc_tag.h @@ -30,8 +30,16 @@ struct alloc_tag { struct alloc_tag_counters __percpu *counters; } __aligned(8); +struct alloc_tag_kernel_section { + struct alloc_tag *first_tag; + unsigned long count; +}; + struct alloc_tag_module_section { - unsigned long start_addr; + union { + unsigned long start_addr; + struct alloc_tag *first_tag; + }; unsigned long end_addr; /* used size */ unsigned long size; diff --git a/include/linux/codetag.h b/include/linux/codetag.h index c4a3dd60205e..dafc59838d87 100644 --- a/include/linux/codetag.h +++ b/include/linux/codetag.h @@ -13,6 +13,9 @@ struct codetag_module; struct seq_buf; struct module; +#define CODETAG_SECTION_START_PREFIX "__start_" +#define CODETAG_SECTION_STOP_PREFIX "__stop_" + /* * An instance of this structure is created in a special ELF section at every * code location being tagged. At runtime, the special section is treated as diff --git a/include/linux/pgalloc_tag.h b/include/linux/pgalloc_tag.h index c76b629d0206..80b8801cb90b 100644 --- a/include/linux/pgalloc_tag.h +++ b/include/linux/pgalloc_tag.h @@ -9,7 +9,18 @@ #ifdef CONFIG_MEM_ALLOC_PROFILING +#if !defined(CONFIG_PGALLOC_TAG_REF_BITS) || CONFIG_PGALLOC_TAG_REF_BITS > 32 +#define PGALLOC_TAG_DIRECT_REF typedef union codetag_ref pgalloc_tag_ref; +#else /* !defined(CONFIG_PGALLOC_TAG_REF_BITS) || CONFIG_PGALLOC_TAG_REF_BITS > 32 */ +#if CONFIG_PGALLOC_TAG_REF_BITS > 16 +typedef u32 pgalloc_tag_ref; +#else +typedef u16 pgalloc_tag_ref; +#endif +#endif /* !defined(CONFIG_PGALLOC_TAG_REF_BITS) || CONFIG_PGALLOC_TAG_REF_BITS > 32 */ + +#ifdef PGALLOC_TAG_DIRECT_REF static inline void read_pgref(pgalloc_tag_ref *pgref, union codetag_ref *ref) { @@ -20,6 +31,63 @@ static inline void write_pgref(pgalloc_tag_ref *pgref, union codetag_ref *ref) { pgref->ct = ref->ct; } + +static inline void alloc_tag_sec_init(void) {} + +#else /* PGALLOC_TAG_DIRECT_REF */ + +extern struct alloc_tag_kernel_section kernel_tags; +extern struct alloc_tag_module_section module_tags; + +#define CODETAG_ID_NULL 0 +#define CODETAG_ID_EMPTY 1 +#define CODETAG_ID_FIRST 2 + +static inline void read_pgref(pgalloc_tag_ref *pgref, union codetag_ref *ref) +{ + pgalloc_tag_ref idx = *pgref; + + switch (idx) { + case (CODETAG_ID_NULL): + ref->ct = NULL; + break; + case (CODETAG_ID_EMPTY): + set_codetag_empty(ref); + break; + default: + idx -= CODETAG_ID_FIRST; + ref->ct = idx < kernel_tags.count ? + &kernel_tags.first_tag[idx].ct : + &module_tags.first_tag[idx - kernel_tags.count].ct; + } +} + +static inline void write_pgref(pgalloc_tag_ref *pgref, union codetag_ref *ref) +{ + struct alloc_tag *tag; + + if (!ref->ct) { + *pgref = CODETAG_ID_NULL; + return; + } + + if (is_codetag_empty(ref)) { + *pgref = CODETAG_ID_EMPTY; + return; + } + + tag = ct_to_alloc_tag(ref->ct); + if (tag >= kernel_tags.first_tag && tag < kernel_tags.first_tag + kernel_tags.count) { + *pgref = CODETAG_ID_FIRST + (tag - kernel_tags.first_tag); + return; + } + + *pgref = CODETAG_ID_FIRST + kernel_tags.count + (tag - module_tags.first_tag); +} + +void __init alloc_tag_sec_init(void); + +#endif /* PGALLOC_TAG_DIRECT_REF */ #include extern struct page_ext_operations page_alloc_tagging_ops; @@ -197,6 +265,7 @@ static inline void pgalloc_tag_sub(struct page *page, unsigned int nr) {} static inline void pgalloc_tag_split(struct page *page, unsigned int nr) {} static inline struct alloc_tag *pgalloc_tag_get(struct page *page) { return NULL; } static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr) {} +static inline void alloc_tag_sec_init(void) {} #endif /* CONFIG_MEM_ALLOC_PROFILING */ diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index a30c03a66172..253f9c2028da 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -1000,6 +1000,17 @@ config MEM_ALLOC_PROFILING_DEBUG Adds warnings with helpful error messages for memory allocation profiling. +config PGALLOC_TAG_REF_BITS + int "Number of bits for page allocation tag reference (10-64)" + range 10 64 + default "64" + depends on MEM_ALLOC_PROFILING + help + Number of bits used to encode a page allocation tag reference. + + Smaller number results in less memory overhead but limits the number of + allocations which can be tagged (including allocations from modules). + source "lib/Kconfig.kasan" source "lib/Kconfig.kfence" source "lib/Kconfig.kmsan" diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c index a1d80d2ef512..d0da206d539e 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -11,13 +12,14 @@ #include static struct codetag_type *alloc_tag_cttype; -static struct alloc_tag_module_section module_tags; static struct maple_tree mod_area_mt = MTREE_INIT(mod_area_mt, MT_FLAGS_ALLOC_RANGE); /* A dummy object used to indicate an unloaded module */ static struct module unloaded_mod; /* A dummy object used to indicate a module prepended area */ static struct module prepend_mod; +struct alloc_tag_module_section module_tags; + DEFINE_PER_CPU(struct alloc_tag_counters, _shared_alloc_tag); EXPORT_SYMBOL(_shared_alloc_tag); @@ -157,6 +159,33 @@ static void __init procfs_init(void) proc_create_seq("allocinfo", 0400, NULL, &allocinfo_seq_op); } +#ifndef PGALLOC_TAG_DIRECT_REF + +#define SECTION_START(NAME) (CODETAG_SECTION_START_PREFIX NAME) +#define SECTION_STOP(NAME) (CODETAG_SECTION_STOP_PREFIX NAME) + +struct alloc_tag_kernel_section kernel_tags = { NULL, 0 }; + +void __init alloc_tag_sec_init(void) +{ + struct alloc_tag *last_codetag; + + kernel_tags.first_tag = (struct alloc_tag *)kallsyms_lookup_name( + SECTION_START(ALLOC_TAG_SECTION_NAME)); + last_codetag = (struct alloc_tag *)kallsyms_lookup_name( + SECTION_STOP(ALLOC_TAG_SECTION_NAME)); + kernel_tags.count = last_codetag - kernel_tags.first_tag; +} + +static inline unsigned long alloc_tag_align(unsigned long val) +{ + if (val % sizeof(struct alloc_tag) == 0) + return val; + return ((val / sizeof(struct alloc_tag)) + 1) * sizeof(struct alloc_tag); +} + +#endif /* PGALLOC_TAG_DIRECT_REF */ + static bool needs_section_mem(struct module *mod, unsigned long size) { return size >= sizeof(struct alloc_tag); @@ -214,6 +243,21 @@ static void *reserve_module_tags(struct module *mod, unsigned long size, if (!align) align = 1; +#ifndef PGALLOC_TAG_DIRECT_REF + /* + * If alloc_tag size is not a multiple of required alignment tag + * indexing does not work. + */ + if (!IS_ALIGNED(sizeof(struct alloc_tag), align)) { + pr_err("%s: alignment %lu is incompatible with allocation tag indexing (CONFIG_PGALLOC_TAG_REF_BITS)", + mod->name, align); + return ERR_PTR(-EINVAL); + } + + /* Ensure prepend consumes multiple of alloc_tag-sized blocks */ + if (prepend) + prepend = alloc_tag_align(prepend); +#endif /* PGALLOC_TAG_DIRECT_REF */ rcu_read_lock(); repeat: /* Try finding exact size and hope the start is aligned */ @@ -462,6 +506,10 @@ static int __init alloc_tag_init(void) return -ENOMEM; module_tags.end_addr = module_tags.start_addr + module_tags_mem_sz; +#ifndef PGALLOC_TAG_DIRECT_REF + /* Ensure the base is alloc_tag aligned */ + module_tags.start_addr = alloc_tag_align(module_tags.start_addr); +#endif mt_set_in_rcu(&mod_area_mt); alloc_tag_cttype = codetag_register_type(&desc); if (IS_ERR(alloc_tag_cttype)) { diff --git a/lib/codetag.c b/lib/codetag.c index d602a81bdc03..53585518a103 100644 --- a/lib/codetag.c +++ b/lib/codetag.c @@ -151,8 +151,8 @@ static struct codetag_range get_section_range(struct module *mod, const char *section) { return (struct codetag_range) { - get_symbol(mod, "__start_", section), - get_symbol(mod, "__stop_", section), + get_symbol(mod, CODETAG_SECTION_START_PREFIX, section), + get_symbol(mod, CODETAG_SECTION_STOP_PREFIX, section), }; } diff --git a/mm/mm_init.c b/mm/mm_init.c index 4ba5607aaf19..231a95782455 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -2650,6 +2650,7 @@ void __init mm_core_init(void) report_meminit(); kmsan_init_shadow(); stack_depot_early_init(); + alloc_tag_sec_init(); mem_init(); kmem_cache_init(); /* From patchwork Mon Aug 19 15:15:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13768492 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 51DE7C52D6F for ; Mon, 19 Aug 2024 15:15:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6FE206B0096; Mon, 19 Aug 2024 11:15:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6AA1F6B0098; Mon, 19 Aug 2024 11:15:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4D6426B0099; Mon, 19 Aug 2024 11:15:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 2D6D36B0096 for ; Mon, 19 Aug 2024 11:15:28 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id D65911A131C for ; Mon, 19 Aug 2024 15:15:27 +0000 (UTC) X-FDA: 82469343894.28.16E01C5 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf08.hostedemail.com (Postfix) with ESMTP id 19BE216002E for ; Mon, 19 Aug 2024 15:15:25 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=HLgcLooE; spf=pass (imf08.hostedemail.com: domain of 3jWHDZgYKCJoMOL8H5AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3jWHDZgYKCJoMOL8H5AIIAF8.6IGFCHOR-GGEP46E.ILA@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=1724080422; 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=YvUli7DxjvCadNZqU9R55CXUnn1ASOBr3/ifBorIX/s=; b=AF7Khg0RA5QPLCI9+1IvCRmJmchhYfh9vUZmW7QXu1C2iX+6lCtX9Hqg1S7z5oEsKD+YcV IizYqYi8cxi7dbGexa+XW4NJeesT0p9vA1TvAoaNzL5/POu3UN3GGkEl6IXm8Ni0BPerX2 b2zxYGp/T3DIMIcYISiRWMz24CMsffM= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=HLgcLooE; spf=pass (imf08.hostedemail.com: domain of 3jWHDZgYKCJoMOL8H5AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3jWHDZgYKCJoMOL8H5AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724080422; a=rsa-sha256; cv=none; b=aVUdGY5T4XvHSv/e320M72RCX1s7JaXSsJOjaB6igwqqNxK/iTIfF6/M2WhaIuA6kemC3u s5/Uu/axtydzBe+3+ss4hSU5BCR3/Qp7jtkD7qXGlqvulQE5BkMuDDVYagx6Hs2/L+l0rj Ivc9PhcfCodZ7iZlxixYtgRI+xpJ6aM= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e163641feb9so1530466276.0 for ; Mon, 19 Aug 2024 08:15:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1724080525; x=1724685325; 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=YvUli7DxjvCadNZqU9R55CXUnn1ASOBr3/ifBorIX/s=; b=HLgcLooEykRM7HGDE1+JqlsK0iZ3qbV3iifyhi0XwkPEFf44RhqDkIE+8bFDrzIZRK PC+saNsX6rb0aBYdQWrOeBzsBTFTLQ/OxLYTDLAeJPEOLKismEr9G4oLPduPa2fTssPE Ux9JkIHPA5IFtCQEb6krpgbT9b2F+kt+kH8j0Kcs5WXavCX/IMgnc2980nEduDNVinsl rDbE7GkXKg1AjMwFx2dB4rQGAGR0PMEIoWAHkCfRjL8UKHCFtsfcLucnEvR78Z5h5BI+ QsYm3hM72DzNKmUUqnogkV+r5kaUSudOGi3g3BFjAgq4uNpMh6er/JCt663Gh1S5sjBc g5AQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724080525; x=1724685325; 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=YvUli7DxjvCadNZqU9R55CXUnn1ASOBr3/ifBorIX/s=; b=CqeN3UW333dOu7bcQ2xVKU8O+B9+Cw0olt2vCl9BSp71Ys5GwqmV4b3kIbDgdKu/Ep B+C1RLe9c1HguU9S/UZ/91fsCMwCr/8hrZqM5aN6TLbkmThbsSlocTCOsaYjRMWrFcPP sUZ6WItsb4jEvEEHnBmXULIcpADIU8lZQQGUwn19Yc1jQFm5KQ8fVGgxyipZ3woSIiQf eB9FkTHVmcTMR3WWALcsRDSIr4Jg6NBSnFJ/wgPwnhJ2fbvIpKY42Ay8ty7Bv/+/hXLU WfsMCQckBt3m1+w9Bh9vWTulUj2rZpy6zOFKBROAdkTib6U+RxNVgoGFg3b5OkztBkV+ QiGw== X-Forwarded-Encrypted: i=1; AJvYcCV4RQknRJ0YcSzSdZsTvaHF/R+V+rsv0dwGvGJsgzX0mMy8b77nNeJv8aPpQHUgQphAxv6NL4fymGz+eDRUPDO+fz0= X-Gm-Message-State: AOJu0YyMFhKoc4JySFceX00j+iKBTHxGZk9uOpJ9vgk9v09sWXdorUkS u7QWn5y+hI+5N/ZAJ/txd4aA8G4WtsZYr604YISDJlTJZiwfiMAaKTHJd6cMlCAkpcZkGnX6hEp u+Q== X-Google-Smtp-Source: AGHT+IGmKKGKkjcmWysWP9m62+j5ch91LeGhxiVj6StWd2Ak+bhrYPJ7oEQRjLH7HKKJlPCdxmQ+SytC38U= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:5aea:cf26:50f4:76db]) (user=surenb job=sendgmr) by 2002:a25:951:0:b0:e0b:af9b:fb94 with SMTP id 3f1490d57ef6-e1182f824e6mr313544276.6.1724080525039; Mon, 19 Aug 2024 08:15:25 -0700 (PDT) Date: Mon, 19 Aug 2024 08:15:11 -0700 In-Reply-To: <20240819151512.2363698-1-surenb@google.com> Mime-Version: 1.0 References: <20240819151512.2363698-1-surenb@google.com> X-Mailer: git-send-email 2.46.0.184.g6999bdac58-goog Message-ID: <20240819151512.2363698-6-surenb@google.com> Subject: [PATCH 5/5] alloc_tag: config to store page allocation tag refs in page flags From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: kent.overstreet@linux.dev, corbet@lwn.net, arnd@arndb.de, mcgrof@kernel.org, rppt@kernel.org, paulmck@kernel.org, thuth@redhat.com, tglx@linutronix.de, bp@alien8.de, xiongwei.song@windriver.com, ardb@kernel.org, david@redhat.com, vbabka@suse.cz, mhocko@suse.com, hannes@cmpxchg.org, roman.gushchin@linux.dev, dave@stgolabs.net, willy@infradead.org, liam.howlett@oracle.com, pasha.tatashin@soleen.com, souravpanda@google.com, keescook@chromium.org, dennis@kernel.org, jhubbard@nvidia.com, yuzhao@google.com, vvvvvv@google.com, rostedt@goodmis.org, iamjoonsoo.kim@lge.com, rientjes@google.com, minchan@google.com, kaleshsingh@google.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, kernel-team@android.com, surenb@google.com X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 19BE216002E X-Stat-Signature: brww6ogjct6mbu3r6j916m5niij8zwbj X-Rspam-User: X-HE-Tag: 1724080525-195366 X-HE-Meta: U2FsdGVkX1+nA4OxgT5vQC0ZMhuAPQprCMYEf/8iok6qXLTV/gEeWSZzC+7G7E7FOyW+opjtOb1/xGz0SWQ94tzlBHadtU6cvkFZU5fYKMltLOe128u5A7s88hErMczF61P19IJ/Tkvt1EwqGAzpnZJDm+O+lnZ7q/u8IIRnLTLlm8aam4QOt/uzURL/xMQbi8W3YWaERxI3vcG1JW2sxQYF8d++Bf+vf5h6muTDxTGHdmVgZFgAjfpsUMc3vttml374wDPAjReprmfaXXcuPU16YRKjNVOCchd0GRVl/CSKQlj3rEk1Lw7czeQA3vKlyHjp1vnaSMLJrMHa3BY60drjpkVbIKNj2zZtA+SZZrUjH1xYOBUuYcUC4kKLXsEUMGKHSlqm0/FSt74dKkXe5wP08ucOfNGONJ90LBIx3P7shfuaTnzszuLP0tAwqIVIp8PuubPtRCPo1SlhAmkRrZIVyTpxXQyo8q6LhPGX/jppmj6Svy1yxroPtRHU1G+Q5ag8CRKeZxl35QU9/zo8oVW09gVCaTt668r1nng/hpWY/5f3F9LfHD5weUo+3Oq32m5wITIGFpRgG3cS7qMvzWu5PBGigzYKD6ZQpyY2OJp4dgGlJbkEdOYewkZudHRIG1+XNchG3uyB4cno+K+PdfTeTK3SB/wSJsv/H6MtHvdk6V04qkQDhW03Qgxdw4ULRcbAxmkskmCx6oZbcn7ylUACuEBsKMYSi0TFuvpV9x+Stdc/LzONTWLXPaeLxbdS2+wR3x3QwHqLhPkVD4Q0qBiSDx2mhWH8D51VyjE18IEhaIN7//DEPcRTUMxnfWr9yF1scEsMuofQPx+Z2gNM7dWXE4Nwxe7sWU1NqPfK8TSLbD91ZzEECPaeX+StA4Fvx6QEVW1zhp9/GN/iJjcHnxJkPYNHm8cHdXLlaEgcD1YOv3B+MkQ7knqD+R4oIekUYyhKTI59P3JbPBLGrk+ Z4BPDOKP OZ8Mw7PTZVTaw4kFFTFlgrzw7/PG6s6xvcF+F75gVGfy2y2zHvXi4Q4KpSnQCF4J6NMO/H296wBfTrxaBI8BmpE/CWobqbtvVDYQSsht8Fb1abojK9rKlfF4Ux1185ri9BUNouScUdEYT7KAX1k5KqIA+cOWJb7pm13m8TwJSeOg1j2t5cb/VQ5u2sfOXpkfDFVGunZyACiY8ZZRucNCmHLEiff1wLAsbfo4bvbIzkX8hrDJgkGGy8IncylCkmuHkIlfUd95aeWDPkjyThDsraN9jAiXt07F6MSr3xNv83x5fLqOCt/hcxf3DFkHqJObY/IIU6e21ChCiF+8a3HMdDbo9sKaT2xYeAuYdjJXNVNaKY5LKZSNAcC4ysBO650dVSDBci21yKqdnsh1z+4GbEoo7LOHBa3oZfNsO9iSEdzGbme6op4iLf6UC2oooTfkatHWjEOaBwsqUFX4yBgVJECCelE2LDCpOhfmKy1OrXs3s3BOgfbFurYp4dcu+RgscbrMn2e2rl4XOq8l0ydV0OWzsI9xfvfziK0dR/8WUrpojpZN1TrKghUKnzw== 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: Add CONFIG_PGALLOC_TAG_USE_PAGEFLAGS to store allocation tag references directly in the page flags. This removes dependency on page_ext and results in better performance for page allocations as well as reduced page_ext memory overhead. CONFIG_PGALLOC_TAG_REF_BITS controls the number of bits required to be available in the page flags to store the references. If the number of page flag bits is insufficient, the build will fail and either CONFIG_PGALLOC_TAG_REF_BITS would have to be lowered or CONFIG_PGALLOC_TAG_USE_PAGEFLAGS should be disabled. Signed-off-by: Suren Baghdasaryan --- include/linux/mmzone.h | 3 ++ include/linux/page-flags-layout.h | 10 +++++-- include/linux/pgalloc_tag.h | 48 +++++++++++++++++++++++++++++++ lib/Kconfig.debug | 27 +++++++++++++++-- lib/alloc_tag.c | 4 +++ mm/page_ext.c | 2 +- 6 files changed, 89 insertions(+), 5 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 17506e4a2835..0dd2b42f7cb6 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -1085,6 +1085,7 @@ static inline bool zone_is_empty(struct zone *zone) #define KASAN_TAG_PGOFF (LAST_CPUPID_PGOFF - KASAN_TAG_WIDTH) #define LRU_GEN_PGOFF (KASAN_TAG_PGOFF - LRU_GEN_WIDTH) #define LRU_REFS_PGOFF (LRU_GEN_PGOFF - LRU_REFS_WIDTH) +#define ALLOC_TAG_REF_PGOFF (LRU_REFS_PGOFF - ALLOC_TAG_REF_WIDTH) /* * Define the bit shifts to access each section. For non-existent @@ -1096,6 +1097,7 @@ static inline bool zone_is_empty(struct zone *zone) #define ZONES_PGSHIFT (ZONES_PGOFF * (ZONES_WIDTH != 0)) #define LAST_CPUPID_PGSHIFT (LAST_CPUPID_PGOFF * (LAST_CPUPID_WIDTH != 0)) #define KASAN_TAG_PGSHIFT (KASAN_TAG_PGOFF * (KASAN_TAG_WIDTH != 0)) +#define ALLOC_TAG_REF_PGSHIFT (ALLOC_TAG_REF_PGOFF * (ALLOC_TAG_REF_WIDTH != 0)) /* NODE:ZONE or SECTION:ZONE is used to ID a zone for the buddy allocator */ #ifdef NODE_NOT_IN_PAGE_FLAGS @@ -1116,6 +1118,7 @@ static inline bool zone_is_empty(struct zone *zone) #define LAST_CPUPID_MASK ((1UL << LAST_CPUPID_SHIFT) - 1) #define KASAN_TAG_MASK ((1UL << KASAN_TAG_WIDTH) - 1) #define ZONEID_MASK ((1UL << ZONEID_SHIFT) - 1) +#define ALLOC_TAG_REF_MASK ((1UL << ALLOC_TAG_REF_WIDTH) - 1) static inline enum zone_type page_zonenum(const struct page *page) { diff --git a/include/linux/page-flags-layout.h b/include/linux/page-flags-layout.h index 7d79818dc065..21bba7c8c965 100644 --- a/include/linux/page-flags-layout.h +++ b/include/linux/page-flags-layout.h @@ -5,6 +5,12 @@ #include #include +#ifdef CONFIG_PGALLOC_TAG_USE_PAGEFLAGS +#define ALLOC_TAG_REF_WIDTH CONFIG_PGALLOC_TAG_REF_BITS +#else +#define ALLOC_TAG_REF_WIDTH 0 +#endif + /* * When a memory allocation must conform to specific limitations (such * as being suitable for DMA) the caller will pass in hints to the @@ -91,7 +97,7 @@ #endif #if ZONES_WIDTH + LRU_GEN_WIDTH + SECTIONS_WIDTH + NODES_WIDTH + \ - KASAN_TAG_WIDTH + LAST_CPUPID_SHIFT <= BITS_PER_LONG - NR_PAGEFLAGS + KASAN_TAG_WIDTH + ALLOC_TAG_REF_WIDTH + LAST_CPUPID_SHIFT <= BITS_PER_LONG - NR_PAGEFLAGS #define LAST_CPUPID_WIDTH LAST_CPUPID_SHIFT #else #define LAST_CPUPID_WIDTH 0 @@ -102,7 +108,7 @@ #endif #if ZONES_WIDTH + LRU_GEN_WIDTH + SECTIONS_WIDTH + NODES_WIDTH + \ - KASAN_TAG_WIDTH + LAST_CPUPID_WIDTH > BITS_PER_LONG - NR_PAGEFLAGS + KASAN_TAG_WIDTH + ALLOC_TAG_REF_WIDTH + LAST_CPUPID_WIDTH > BITS_PER_LONG - NR_PAGEFLAGS #error "Not enough bits in page flags" #endif diff --git a/include/linux/pgalloc_tag.h b/include/linux/pgalloc_tag.h index 80b8801cb90b..da95c09bcdf1 100644 --- a/include/linux/pgalloc_tag.h +++ b/include/linux/pgalloc_tag.h @@ -88,6 +88,52 @@ static inline void write_pgref(pgalloc_tag_ref *pgref, union codetag_ref *ref) void __init alloc_tag_sec_init(void); #endif /* PGALLOC_TAG_DIRECT_REF */ + +#ifdef CONFIG_PGALLOC_TAG_USE_PAGEFLAGS + +typedef struct page *pgtag_ref_handle; + +/* Should be called only if mem_alloc_profiling_enabled() */ +static inline pgtag_ref_handle get_page_tag_ref(struct page *page, + union codetag_ref *ref) +{ + if (page) { + pgalloc_tag_ref pgref; + + pgref = (page->flags >> ALLOC_TAG_REF_PGSHIFT) & ALLOC_TAG_REF_MASK; + read_pgref(&pgref, ref); + return page; + } + + return NULL; +} + +static inline void put_page_tag_ref(pgtag_ref_handle page) +{ + WARN_ON(!page); +} + +static inline void update_page_tag_ref(pgtag_ref_handle page, union codetag_ref *ref) +{ + unsigned long old_flags, flags, val; + pgalloc_tag_ref pgref; + + if (WARN_ON(!page || !ref)) + return; + + write_pgref(&pgref, ref); + val = (unsigned long)pgref; + val = (val & ALLOC_TAG_REF_MASK) << ALLOC_TAG_REF_PGSHIFT; + do { + old_flags = READ_ONCE(page->flags); + flags = old_flags; + flags &= ~(ALLOC_TAG_REF_MASK << ALLOC_TAG_REF_PGSHIFT); + flags |= val; + } while (unlikely(!try_cmpxchg(&page->flags, &old_flags, flags))); +} + +#else /* CONFIG_PGALLOC_TAG_USE_PAGEFLAGS */ + #include extern struct page_ext_operations page_alloc_tagging_ops; @@ -136,6 +182,8 @@ static inline void update_page_tag_ref(pgtag_ref_handle pgref, union codetag_ref write_pgref(pgref, ref); } +#endif /* CONFIG_PGALLOC_TAG_USE_PAGEFLAGS */ + static inline void clear_page_tag_ref(struct page *page) { if (mem_alloc_profiling_enabled()) { diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 253f9c2028da..9fc8c1981f27 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -979,7 +979,7 @@ config MEM_ALLOC_PROFILING depends on PROC_FS depends on !DEBUG_FORCE_WEAK_PER_CPU select CODE_TAGGING - select PAGE_EXTENSION + select PAGE_EXTENSION if !PGALLOC_TAG_USE_PAGEFLAGS select SLAB_OBJ_EXT help Track allocation source code and record total allocation size @@ -1000,10 +1000,26 @@ config MEM_ALLOC_PROFILING_DEBUG Adds warnings with helpful error messages for memory allocation profiling. +config PGALLOC_TAG_USE_PAGEFLAGS + bool "Use pageflags to encode page allocation tag reference" + default n + depends on MEM_ALLOC_PROFILING + help + When set, page allocation tag references are encoded inside page + flags, otherwise they are encoded in page extensions. + + Setting this flag reduces memory and performance overhead of memory + allocation profiling but also limits how many allocations can be + tagged. The number of bits is set by PGALLOC_TAG_USE_PAGEFLAGS and + they must fit in the page flags field. + + Say N if unsure. + config PGALLOC_TAG_REF_BITS int "Number of bits for page allocation tag reference (10-64)" range 10 64 - default "64" + default "16" if PGALLOC_TAG_USE_PAGEFLAGS + default "64" if !PGALLOC_TAG_USE_PAGEFLAGS depends on MEM_ALLOC_PROFILING help Number of bits used to encode a page allocation tag reference. @@ -1011,6 +1027,13 @@ config PGALLOC_TAG_REF_BITS Smaller number results in less memory overhead but limits the number of allocations which can be tagged (including allocations from modules). + If PGALLOC_TAG_USE_PAGEFLAGS is set, the number of requested bits should + fit inside the page flags. + + If PGALLOC_TAG_USE_PAGEFLAGS is not set, the number of bits used to store + a reference is rounded up to the closest basic type. If set higher than 32, + a direct pointer to the allocation tag is stored for performance reasons. + source "lib/Kconfig.kasan" source "lib/Kconfig.kfence" source "lib/Kconfig.kmsan" diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c index d0da206d539e..1fbe80e68fdb 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -432,6 +432,8 @@ static int __init setup_early_mem_profiling(char *str) } early_param("sysctl.vm.mem_profiling", setup_early_mem_profiling); +#ifndef CONFIG_PGALLOC_TAG_USE_PAGEFLAGS + static __init bool need_page_alloc_tagging(void) { return mem_profiling_support; @@ -448,6 +450,8 @@ struct page_ext_operations page_alloc_tagging_ops = { }; EXPORT_SYMBOL(page_alloc_tagging_ops); +#endif /* CONFIG_PGALLOC_TAG_USE_PAGEFLAGS */ + #ifdef CONFIG_SYSCTL static struct ctl_table memory_allocation_profiling_sysctls[] = { { diff --git a/mm/page_ext.c b/mm/page_ext.c index 641d93f6af4c..5f993c271ee7 100644 --- a/mm/page_ext.c +++ b/mm/page_ext.c @@ -83,7 +83,7 @@ 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 +#if defined(CONFIG_MEM_ALLOC_PROFILING) && !defined(CONFIG_PGALLOC_TAG_USE_PAGEFLAGS) &page_alloc_tagging_ops, #endif #ifdef CONFIG_PAGE_TABLE_CHECK