page allocation tag compression
Subsystems (13)
Suren Baghdasaryan
maintainerv4 cover page allocation tag compression
This patchset implements several improvements:
1. Gracefully handles module unloading while there are used allocations allocated from that module;
2. Provides an option to store page allocation tag references in the page flags, removing dependency on page extensions and eliminating the memory overhead from storing page allocation references (~0.2% of total system memory). This also improves page allocation performance when CONFIG_MEM_ALLOC_PROFILING is enabled by eliminating page extension lookup. Page allocation performance overhead is reduced from 41% to 5.5%.
Patch #1 introduces mas_for_each_rev() helper function.
Patch #2 introduces shutdown_mem_profiling() helper function to be used when disabling memory allocation profiling.
Patch #3 copies module tags into virtually contiguous memory which serves two purposes:
- Lets us deal with the situation when module is unloaded while there are still live allocations from that module. Since we are using a copy version of the tags we can safely unload the module. Space and gaps in this contiguous memory are managed using a maple tree.
- Enables simple indexing of the tags in the later patches.
Patch #4 changes the way we allocate virtually contiguous memory for module tags to reserve only vitrual area and populate physical pages only as needed at module load time.
Patch #5 abstracts page allocation tag reference to simplify later changes.
Patch #6 adds compression option to the sysctl.vm.mem_profiling boot parameter for storing page allocation tag references inside page flags if they fit. If the number of available page flag bits is insufficient to address all kernel allocations, memory allocation profiling gets disabled with an appropriate warning.
Patchset applies to mm-unstable.
Changes since v3 [1]:
- rebased over Mike's patchset in mm-unstable
- added Reviewed-by, per Liam Howlett
- limited execmem_vmap to work with EXECMEM_MODULE_DATA only, per Mike Rapoport
- moved __get_vm_area_node() declaration into mm/internal.h, per Mike Rapoport
- split parts of reserve_module_tags() into helper functions to make it more readable, per Mike Rapoport
- introduced shutdown_mem_profiling() to be used when disabling memory allocation profiling
- replaced CONFIG_PGALLOC_TAG_USE_PAGEFLAGS with a new boot parameter option, per Michal Hocko
- minor code cleanups and refactoring to make the code more readable
- added VMALLOC and MODULE SUPPORT reviewers I missed before
[1] https://lore.kernel.org/all/[email protected]/
Suren Baghdasaryan (6):
maple_tree: add mas_for_each_rev() helper
alloc_tag: introduce shutdown_mem_profiling helper function
alloc_tag: load module tags into separate contiguous memory
alloc_tag: populate memory for module tags as needed
alloc_tag: introduce pgtag_ref_handle to abstract page tag references
alloc_tag: support for page allocation tag compression
Documentation/mm/allocation-profiling.rst | 7 +- include/asm-generic/codetag.lds.h | 19 + include/linux/alloc_tag.h | 21 +- include/linux/codetag.h | 40 +- include/linux/execmem.h | 10 + include/linux/maple_tree.h | 14 + include/linux/mm.h | 25 +- include/linux/page-flags-layout.h | 7 + include/linux/pgalloc_tag.h | 197 +++++++-- include/linux/vmalloc.h | 3 + kernel/module/main.c | 80 ++-- lib/alloc_tag.c | 467 ++++++++++++++++++++-- lib/codetag.c | 104 ++++- mm/execmem.c | 16 + mm/internal.h | 6 + mm/mm_init.c | 5 +- mm/vmalloc.c | 4 +- scripts/module.lds.S | 5 +- 18 files changed, 903 insertions(+), 127 deletions(-)
base-commit: b5d43fad926a3f542cd06f3c9d286f6f489f7129
Signature · 1 line
2.47.0.105.g07ac214952-goog
Suren Baghdasaryan
maintainerPATCH v4 1/6 maple_tree: add mas_for_each_rev() helper
Add mas_for_each_rev() function to iterate maple tree nodes in reverse order.
--- include/linux/maple_tree.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
include/linux/maple_tree.h +14 -0
@@ -592,6 +592,20 @@ static __always_inline void mas_reset(struct ma_state *mas)#define mas_for_each (__mas, __entry, __max) \ while (((__entry) = mas_find((__mas), (__max))) != NULL) +/** +* mas_for_each_rev() - Iterate over a range of the maple tree in reverse order. +* @__mas: Maple Tree operation state (maple_state) +* @__entry: Entry retrieved from the tree +* @__min: minimum index to retrieve from the tree +* +* When returned, mas->index and mas->last will hold the entire range for the +* entry. +* +* Note: may return the zero entry. +*/ +#define mas_for_each_rev (__mas, __entry, __min) \ + while (((__entry) = mas_find_rev((__mas), (__min))) != NULL) +#ifdef CONFIG_DEBUG_MAPLE_TREE enum mt_dump_format { mt_dump_dec,
Signature · 1 line
2.47.0.105.g07ac214952-goog
Suren Baghdasaryan
maintainerPATCH v4 2/6 alloc_tag: introduce shutdown_mem_profiling helper function
In reply to Suren BaghdasaryanImplement a helper function to disable memory allocation profiling and use it when creation of /proc/allocinfo fails.
Ensure /proc/allocinfo does not get created when memory allocation profiling is disabled.
--- lib/alloc_tag.c | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-)
lib/alloc_tag.c +26 -7
@@ -8,6 +8,14 @@#include <linux/seq_buf.h> #include <linux/seq_file.h> +#define ALLOCINFO_FILE_NAME "allocinfo" + +#ifdef CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT +static bool mem_profiling_support__meminitdata = true; +#else +static bool mem_profiling_support__meminitdata ; +#endif +static struct codetag_type *alloc_tag_cttype; DEFINE_PER_CPU(struct alloc_tag_counters, _shared_alloc_tag);
@@ -144,9 +152,26 @@ size_t alloc_tag_top_users(struct codetag_bytes *tags, size_t count, bool can_slreturn nr; } +static void __init shutdown_mem_profiling (void ) +{ +if (mem_alloc_profiling_enabled ()) +static_branch_disable (&mem_alloc_profiling_key); + +if (!mem_profiling_support) +return ; + + mem_profiling_support = false; +} +static void __init procfs_init (void ) { -proc_create_seq ("allocinfo" ,0400 ,NULL , &allocinfo_seq_op); +if (!mem_profiling_support) +return ; + +if (!proc_create_seq (ALLOCINFO_FILE_NAME ,0400 ,NULL , &allocinfo_seq_op)) { +pr_err ("Failed to create %s file\n" ,ALLOCINFO_FILE_NAME ); +shutdown_mem_profiling (); + } } static bool alloc_tag_module_unload(struct codetag_type *cttype,
@@ -174,12 +199,6 @@ static bool alloc_tag_module_unload(struct codetag_type *cttype,return module_unused; } -#ifdef CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT -static bool mem_profiling_support__meminitdata = true; -#else -static bool mem_profiling_support__meminitdata ; -#endif -static int __init setup_early_mem_profiling (char *str) {bool enable;
Signature · 1 line
2.47.0.105.g07ac214952-goog
Suren Baghdasaryan
maintainerPATCH v4 3/6 alloc_tag: load module tags into separate contiguous memory
In reply to Suren BaghdasaryanWhen 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.
--- include/asm-generic/codetag.lds.h | 19 +++ include/linux/alloc_tag.h | 13 +- include/linux/codetag.h | 37 ++++- kernel/module/main.c | 80 ++++++---- lib/alloc_tag.c | 249 +++++++++++++++++++++++++++--- lib/codetag.c | 100 +++++++++++- scripts/module.lds.S | 5 +- 7 files changed, 441 insertions(+), 62 deletions(-)
include/asm-generic/codetag.lds.h +19 -0
@@ -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 */
include/linux/alloc_tag.h +11 -2
@@ -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 };
include/linux/codetag.h +32 -5
@@ -35,8 +35,15 @@ 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); +#ifdef CONFIG_MODULES +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 used); +#endif };struct codetag_iterator {
@@ -71,11 +78,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 */
kernel/module/main.c +54 -26
171 lines not on this page · raw message
lib/alloc_tag.c +229 -20
285 lines not on this page · raw message
lib/codetag.c +93 -7
130 lines not on this page · raw message
scripts/module.lds.S +3 -2
@@ -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 */
Signature · 1 line
2.47.0.105.g07ac214952-goog
Suren Baghdasaryan
maintainerPATCH v4 4/6 alloc_tag: populate memory for module tags as needed
In reply to Suren BaghdasaryanThe memory reserved for module tags does not need to be backed by physical pages until there are tags to store there. Change the way we reserve this memory to allocate only virtual area for the tags and populate it with physical pages as needed when we load a module.
--- include/linux/execmem.h | 10 ++++++ include/linux/vmalloc.h | 3 ++ lib/alloc_tag.c | 73 ++++++++++++++++++++++++++++++++++++----- mm/execmem.c | 16 +++++++++ mm/internal.h | 6 ++++ mm/vmalloc.c | 4 +-- 6 files changed, 101 insertions(+), 11 deletions(-)
include/linux/execmem.h +10 -0
@@ -139,6 +139,16 @@ void *execmem_alloc(enum execmem_type type, size_t size);*/ void execmem_free (void *ptr); +/** +* execmem_vmap - create virtual mapping for EXECMEM_MODULE_DATA memory +* @size: size of the virtual mapping in bytes +* +* Maps virtually contiguous area in the range suitable for EXECMEM_MODULE_DATA. +* +* Return: the area descriptor on success or %NULL on failure. +*/ +struct vm_struct *execmem_vmap (size_t size); +/** * execmem_update_copy - copy an update to executable memory * @dst: destination address to update
include/linux/vmalloc.h +3 -0
@@ -202,6 +202,9 @@ extern int remap_vmalloc_range_partial(struct vm_area_struct *vma,extern int remap_vmalloc_range (struct vm_area_struct *vma,void *addr,unsigned long pgoff); +int vmap_pages_range (unsigned long addr,unsigned long end,pgprot_t prot, +struct page **pages,unsigned int page_shift); +/* * Architectures can set this mask to a combination of PGTBL_P?D_MODIFIED values * and let generic vmalloc and ioremap code know when arch_sync_kernel_mappings()
lib/alloc_tag.c +64 -9
125 lines not on this page · raw message
mm/execmem.c +16 -0
@@ -368,6 +368,22 @@ void execmem_free(void *ptr)vfree (ptr); } +struct vm_struct *execmem_vmap (size_t size) +{ +struct execmem_range *range = &execmem_info->ranges[EXECMEM_MODULE_DATA ]; +struct vm_struct *area; + + area =__get_vm_area_node (size, range->alignment,PAGE_SHIFT ,VM_ALLOC , + range->start, range->end,NUMA_NO_NODE , +GFP_KERNEL ,__builtin_return_address (0 )); +if (!area && range->fallback_start) + area =__get_vm_area_node (size, range->alignment,PAGE_SHIFT ,VM_ALLOC , + range->fallback_start, range->fallback_end, +NUMA_NO_NODE ,GFP_KERNEL ,__builtin_return_address (0 )); + +return area; +} +void *execmem_update_copy (void *dst,const void *src,size_t size) {return text_poke_copy (dst, src, size);
mm/internal.h +6 -0
@@ -1219,6 +1219,12 @@ int numa_migrate_check(struct folio *folio, struct vm_fault *vmf,void free_zone_device_folio (struct folio *folio);int migrate_device_coherent_folio (struct folio *folio); +struct vm_struct *__get_vm_area_node (unsigned long size, +unsigned long align,unsigned long shift, +unsigned long flags,unsigned long start, +unsigned long end,int node,gfp_t gfp_mask, +const void *caller); +/* * mm/gup.c */
mm/vmalloc.c +2 -2
@@ -653,7 +653,7 @@ int vmap_pages_range_noflush(unsigned long addr, unsigned long end,* RETURNS: * 0 on success, -errno on failure. */ -static int vmap_pages_range (unsigned long addr,unsigned long end, +int vmap_pages_range (unsigned long addr,unsigned long end,pgprot_t prot,struct page **pages,unsigned int page_shift) {int err;
@@ -3106,7 +3106,7 @@ static void clear_vm_uninitialized_flag(struct vm_struct *vm) vm->flags &= ~VM_UNINITIALIZED ; } -static struct vm_struct *__get_vm_area_node (unsigned long size, +struct vm_struct *__get_vm_area_node (unsigned long size,unsigned long align,unsigned long shift,unsigned long flags,unsigned long start,unsigned long end,int node,gfp_t gfp_mask,const void *caller)
Signature · 1 line
2.47.0.105.g07ac214952-goog
Suren Baghdasaryan
maintainerPATCH v4 5/6 alloc_tag: introduce pgtag_ref_handle to abstract page tag references
In reply to Suren BaghdasaryanTo simplify later changes to page tag references, introduce new pgtag_ref_handle type. This allows easy replacement of page_ext as a storage of page allocation tags.
--- include/linux/mm.h | 25 +++++----- include/linux/pgalloc_tag.h | 92 ++++++++++++++++++++++--------------- 2 files changed, 67 insertions(+), 50 deletions(-)
include/linux/mm.h +13 -12
@@ -4180,37 +4180,38 @@ static inline void pgalloc_tag_split(struct folio *folio, int old_order, int newreturn ;for (i = nr_pages; i < (1 << old_order); i += nr_pages) { -union codetag_ref *ref =get_page_tag_ref (folio_page (folio, i)); +union pgtag_ref_handle handle; +union codetag_ref ref; -if (ref) { +if (get_page_tag_ref (folio_page (folio, i), &ref, &handle)) {/* Set new reference to point to the original tag */ -alloc_tag_ref_set (ref, tag); -put_page_tag_ref (ref); +alloc_tag_ref_set (&ref, tag); +update_page_tag_ref (handle, &ref); +put_page_tag_ref (handle); } } }static inline void pgalloc_tag_copy (struct folio *new,struct folio *old) { +union pgtag_ref_handle handle; +union codetag_ref ref;struct alloc_tag *tag; -union codetag_ref *ref; tag =pgalloc_tag_get (&old->page);if (!tag)return ; - ref =get_page_tag_ref (&new->page); -if (!ref) +if (!get_page_tag_ref (&new->page, &ref, &handle))return ;/* Clear the old ref to the original allocation tag. */ clear_page_tag_ref (&old->page);/* Decrement the counters of the tag on get_new_folio. */ -alloc_tag_sub (ref,folio_nr_pages (new)); - -__alloc_tag_ref_set (ref, tag); - -put_page_tag_ref (ref); +alloc_tag_sub (&ref,folio_nr_pages (new)); +__alloc_tag_ref_set (&ref, tag); +update_page_tag_ref (handle, &ref); +put_page_tag_ref (handle); }#else /* !CONFIG_MEM_ALLOC_PROFILING */ static inline void pgalloc_tag_split (struct folio *folio,int old_order,int new_order)
include/linux/pgalloc_tag.h +54 -38
143 lines not on this page · raw message
Signature · 1 line
2.47.0.105.g07ac214952-goog
Suren Baghdasaryan
maintainerPATCH v4 6/6 alloc_tag: support for page allocation tag compression
In reply to Suren BaghdasaryanImplement support for storing page allocation tag references directly in the page flags instead of page extensions. sysctl.vm.mem_profiling boot parameter it extended to provide a way for a user to request this mode. Enabling compression eliminates memory overhead caused by page_ext and results in better performance for page allocations. However this mode will not work if the number of available page flag bits is insufficient to address all kernel allocations. Such condition can happen during boot or when loading a module. If this condition is detected, memory allocation profiling gets disabled with an appropriate warning. By default compression mode is disabled.
--- Documentation/mm/allocation-profiling.rst | 7 +- include/linux/alloc_tag.h | 10 +- include/linux/codetag.h | 3 + include/linux/page-flags-layout.h | 7 ++ include/linux/pgalloc_tag.h | 145 +++++++++++++++++++--- lib/alloc_tag.c | 142 +++++++++++++++++++-- lib/codetag.c | 4 +- mm/mm_init.c | 5 +- 8 files changed, 290 insertions(+), 33 deletions(-)
Documentation/mm/allocation-profiling.rst +6 -1
@@ -18,12 +18,17 @@ kconfig options: missing annotation Boot parameter: - sysctl.vm.mem_profiling=0|1|never + sysctl.vm.mem_profiling={0|1|never}[,compressed] When set to "never", memory allocation profiling overhead is minimized and it cannot be enabled at runtime (sysctl becomes read-only). When CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT=y, default value is "1". When CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT=n, default value is "never". + "compressed" optional parameter will try to store page tag references in a + compact format, avoiding page extensions. This results in improved performance + and memory consumption, however it might fail depending on system configuration. + If compression fails, a warning is issued and memory allocation profiling gets + disabled. sysctl: /proc/sys/vm/mem_profiling
include/linux/alloc_tag.h +9 -1
@@ -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;
include/linux/codetag.h +3 -0
@@ -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
include/linux/page-flags-layout.h +7 -0
@@ -111,5 +111,12 @@ ZONES_WIDTH - LRU_GEN_WIDTH - SECTIONS_WIDTH - \ NODES_WIDTH - KASAN_TAG_WIDTH - LAST_CPUPID_WIDTH) +#define NR_NON_PAGEFLAG_BITS (SECTIONS_WIDTH + NODES_WIDTH + ZONES_WIDTH + \ + LAST_CPUPID_SHIFT + KASAN_TAG_WIDTH + \ + LRU_GEN_WIDTH + LRU_REFS_WIDTH) + +#define NR_UNUSED_PAGEFLAG_BITS (BITS_PER_LONG - \ + (NR_NON_PAGEFLAG_BITS + NR_PAGEFLAGS)) +#endif #endif /* _LINUX_PAGE_FLAGS_LAYOUT */
include/linux/pgalloc_tag.h +128 -17
185 lines not on this page · raw message
lib/alloc_tag.c +133 -9
241 lines not on this page · raw message
lib/codetag.c +2 -2
@@ -149,8 +149,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), }; }
mm/mm_init.c +2 -3
@@ -83,8 +83,7 @@ void __init mminit_verify_pageflags_layout(void)unsigned long or_mask, add_mask; shift =BITS_PER_LONG ; - width = shift -SECTIONS_WIDTH -NODES_WIDTH -ZONES_WIDTH - -LAST_CPUPID_SHIFT -KASAN_TAG_WIDTH -LRU_GEN_WIDTH -LRU_REFS_WIDTH ; + width = shift -NR_NON_PAGEFLAG_BITS ; mminit_dprintk(MMINIT_TRACE, "pageflags_layout_widths", "Section %d Node %d Zone %d Lastcpupid %d Kasantag %d Gen %d Tier %d Flags %d\n", SECTIONS_WIDTH,
@@ -2639,7 +2638,7 @@ void __init mm_core_init(void)BUILD_BUG_ON (MAX_ZONELISTS >2 );build_all_zonelists (NULL );page_alloc_init_cpuhp (); - +alloc_tag_sec_init ();/* * page_ext requires contiguous pages, * bigger than MAX_PAGE_ORDER unless SPARSEMEM.
Signature · 1 line
2.47.0.105.g07ac214952-goog
Re: [PATCH v4 1/6] maple_tree: add mas_for_each_rev() helper
In reply to Suren BaghdasaryanQuoted 7 lines · Suren Baghdasaryan
Add
mas_for_each_rev()function to iterate maple tree nodes in reverse order.Suggested-by Liam R. Howlett <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>Reviewed-by Liam R. Howlett <[email protected]>
Quoted 32 lines
--- include/linux/maple_tree.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+)include/linux/maple_tree.h +14
-1@@ -592,6 +592,20 @@ static __always_inline void mas_reset(struct ma_state *mas)#define mas_for_each (__mas, __entry, __max) \ while (((__entry) = mas_find((__mas), (__max))) != NULL) +/** +* mas_for_each_rev() - Iterate over a range of the maple tree in reverse order. +* @__mas: Maple Tree operation state (maple_state) +* @__entry: Entry retrieved from the tree +* @__min: minimum index to retrieve from the tree +* +* When returned, mas->index and mas->last will hold the entire range for the +* entry. +* +* Note: may return the zero entry. +*/ +#define mas_for_each_rev (__mas, __entry, __min) \ + while (((__entry) = mas_find_rev((__mas), (__min))) != NULL) +#ifdef CONFIG_DEBUG_MAPLE_TREE enum mt_dump_format { mt_dump_dec, --2.47.0.105.g07ac214952-goog
Re: [PATCH v4 2/6] alloc_tag: introduce shutdown_mem_profiling helper function
In reply to Suren BaghdasaryanQuoted 7 lines · Suren Baghdasaryan
Implement a helper function to disable memory allocation profiling and use it when creation of
/proc/allocinfofails.
Ensure/proc/allocinfodoes not get created when memory allocation profiling is disabled.Signed-off-by Suren Baghdasaryan <[email protected]>
Re: [PATCH v4 5/6] alloc_tag: introduce pgtag_ref_handle to abstract page tag references
In reply to Suren BaghdasaryanQuoted 6 lines · Suren Baghdasaryan
To simplify later changes to page tag references, introduce new
pgtag_ref_handletype. This allows easy replacement ofpage_extas a storage of page allocation tags.Signed-off-by Suren Baghdasaryan <[email protected]>
Re: [PATCH v4 3/6] alloc_tag: load module tags into separate contiguous memory
In reply to Suren BaghdasaryanQuoted 15 lines · Suren Baghdasaryan
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.
Quoted 1 line
Signed-off-by Suren Baghdasaryan <[email protected]>
Quoted 784 lines
--- include/asm-generic/codetag.lds.h | 19 +++ include/linux/alloc_tag.h | 13 +- include/linux/codetag.h | 37 ++++- kernel/module/main.c | 80 ++++++---- lib/alloc_tag.c | 249 +++++++++++++++++++++++++++--- lib/codetag.c | 100 +++++++++++- scripts/module.lds.S | 5 +- 7 files changed, 441 insertions(+), 62 deletions(-)include/asm-generic/codetag.lds.h +19
-0@@ -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 */ include/linux/alloc_tag.h +11
-2@@ -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 };include/linux/codetag.h +32
-5@@ -35,8 +35,15 @@ 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); +#ifdef CONFIG_MODULES +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 used); +#endif };struct codetag_iterator {@@ -71,11 +78,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 */ kernel/module/main.c +54
-26@@ -1254,22 +1254,17 @@ 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) {struct module_memory *mem = &mod->mem[type]; -void *ptr = mem->base;if (mem->is_rox)vfree (mem->rw_copy); -if (!unload_codetags &&mod_mem_type_is_core_data (type)) -return ; - -execmem_free (ptr); +execmem_free (mem->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];@@ -1280,25 +1275,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);@@ -1341,7 +1331,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)@@ -1606,6 +1596,20 @@ static void __layout_sections(struct module *mod, struct load_info *info, bool iif (WARN_ON_ONCE (type ==MOD_INVALID ))continue ; +/* +* Do not allocate codetag memory as we load it into +* preallocated contiguous memory. +*/ +if (codetag_needs_module_section (mod, sname, s->sh_size)) { +/* +* s->sh_entsize won't be used but populate the +* type field to avoid confusion. +*/ + s->sh_entsize = ((unsigned long )(type) &SH_ENTSIZE_TYPE_MASK ) + <<SH_ENTSIZE_TYPE_SHIFT ; +continue ; + } + s->sh_entsize =module_get_offset_and_type (mod, type, s, i);pr_debug ("\t%s\n" , sname); }@@ -2280,6 +2284,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) {@@ -2291,7 +2296,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; } }@@ -2300,15 +2305,33 @@ static int move_module(struct module *mod, struct load_info *info)for (i =0 ; i < info->hdr->e_shnum; i++) {void *dest;Elf_Shdr *shdr = &info->sechdrs[i]; -enum mod_mem_type type = shdr->sh_entsize >>SH_ENTSIZE_TYPE_SHIFT ; -unsigned long offset = shdr->sh_entsize &SH_ENTSIZE_OFFSET_MASK ; +const char *sname;unsigned long addr;if (!(shdr->sh_flags &SHF_ALLOC ))continue ; - addr = (unsigned long )mod->mem[type].base + offset; - dest = mod->mem[type].rw_copy + offset; + 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; + } + addr = (unsigned long )dest; + codetag_section_found = true; + }else { +enum mod_mem_type type = shdr->sh_entsize >>SH_ENTSIZE_TYPE_SHIFT ; +unsigned long offset = shdr->sh_entsize &SH_ENTSIZE_OFFSET_MASK ; + + addr = (unsigned long )mod->mem[type].base + offset; + dest = mod->mem[type].rw_copy + offset; + }if (shdr->sh_type !=SHT_NOBITS ) {/* @@ -2320,7 +2343,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); }@@ -2336,9 +2359,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; }@@ -2459,6 +2485,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; }@@ -2468,7 +2496,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,lib/alloc_tag.c +229
-20@@ -1,5 +1,6 @@// SPDX-License-Identifier: GPL-2.0-only #include <linux/alloc_tag.h> +#include <linux/execmem.h> #include <linux/fs.h> #include <linux/gfp.h> #include <linux/module.h> @@ -9,6 +10,7 @@#include <linux/seq_file.h> #define ALLOCINFO_FILE_NAME "allocinfo" +#define MODULE_ALLOC_TAG_VMAP_SIZE (100000UL * sizeof(struct alloc_tag))#ifdef CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT static bool mem_profiling_support__meminitdata = true;@@ -174,31 +176,226 @@ static void __init procfs_init(void) } } -static bool alloc_tag_module_unload (struct codetag_type *cttype, -struct codetag_module *cmod) +#ifdef CONFIG_MODULES + +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; + +static struct alloc_tag_module_section module_tags; + +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 ); +} + +static struct alloc_tag *find_used_tag (struct alloc_tag *from,struct alloc_tag *to) +{ +while (from <= to) { +struct alloc_tag_counters counter; -for (ct =codetag_next_ct (&iter); ct; ct =codetag_next_ct (&iter)) { -if (iter.cmod != cmod) + counter =alloc_tag_read (from); +if (counter.bytes) +return from; + from++; + } + +return NULL ; +} + +/* Called with mod_area_mt locked */ +static void clean_unused_module_areas_locked (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)continue ; - tag =ct_to_alloc_tag (ct); - counter =alloc_tag_read (tag); +/* Release area if all tags are unused */ +if (!find_used_tag ((struct alloc_tag *)(module_tags.start_addr + mas.index), + (struct alloc_tag *)(module_tags.start_addr + mas.last))) +mas_erase (&mas); + } +} + +/* Called with mod_area_mt locked */ +static bool find_aligned_area (struct ma_state *mas,unsigned long section_size, +unsigned long size,unsigned int prepend,unsigned long align) +{ +bool cleanup_done = false; + +repeat: +/* Try finding exact size and hope the start is aligned */ +if (!mas_empty_area (mas,0 , section_size -1 , prepend + size)) { +if (IS_ALIGNED (mas->index + prepend, align)) +return true; + +/* Try finding larger area to align later */ +mas_reset (mas); +if (!mas_empty_area (mas,0 , section_size -1 , + size + prepend + align -1 )) +return true; + } -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; +/* No free area, try cleanup stale data and repeat the search once */ +if (!cleanup_done) { +clean_unused_module_areas_locked (); + cleanup_done = true; +mas_reset (mas); + goto repeat; } -return module_unused; +return false; +} + +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 ); +unsigned long offset; +void *ret =NULL ; + +/* 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 ; + +mas_lock (&mas); +if (!find_aligned_area (&mas, section_size, size, prepend, align)) { + ret =ERR_PTR (-ENOMEM ); + goto unlock; + } + +/* Mark found area as reserved */ + offset = mas.index; + offset += prepend; + offset =ALIGN (offset, align); +if (offset != mas.index) { +unsigned long pad_start = mas.index; + + mas.last = offset -1 ; +mas_store (&mas, &prepend_mod); +if (mas_is_err (&mas)) { + ret =ERR_PTR (xa_err (mas.node)); + goto unlock; + } + mas.index = offset; + mas.last = offset + size -1 ; +mas_store (&mas, mod); +if (mas_is_err (&mas)) { + mas.index = pad_start; +mas_erase (&mas); + ret =ERR_PTR (xa_err (mas.node)); + } + }else { + mas.last = offset + size -1 ; +mas_store (&mas, mod); +if (mas_is_err (&mas)) + ret =ERR_PTR (xa_err (mas.node)); + } +unlock: +mas_unlock (&mas); + +if (IS_ERR (ret)) +return ret; + +if (module_tags.size < offset + size) + module_tags.size = offset + size; + +return (struct alloc_tag *)(module_tags.start_addr + offset); } +static void release_module_tags (struct module *mod,bool used) +{ +MA_STATE (mas, &mod_area_mt, module_tags.size, module_tags.size); +struct alloc_tag *tag; +struct module *val; + +mas_lock (&mas); +mas_for_each_rev (&mas, val,0 ) +if (val == mod) +break ; + +if (!val)/* module not found */ + goto out; + +if (!used) + goto release_area; + +/* Find out if the area is used */ + tag =find_used_tag ((struct alloc_tag *)(module_tags.start_addr + mas.index), + (struct alloc_tag *)(module_tags.start_addr + mas.last)); +if (tag) { +struct alloc_tag_counters counter =alloc_tag_read (tag); + +pr_info ("%s:%u module %s func:%s has %llu allocated at module unload\n" , + tag->ct.filename, tag->ct.lineno, tag->ct.modname, + tag->ct.function, counter.bytes); + }else { + used = false; + } +release_area: +mas_store (&mas, used ? &unloaded_mod :NULL ); + val =mas_prev_range (&mas,0 ); +if (val == &prepend_mod) +mas_store (&mas,NULL ); +out: +mas_unlock (&mas); +} + +static void replace_module (struct module *mod,struct module *new_mod) +{ +MA_STATE (mas, &mod_area_mt,0 , module_tags.size); +struct module *val; + +mas_lock (&mas); +mas_for_each (&mas, val, module_tags.size) { +if (val != mod) +continue ; + +mas_store_gfp (&mas, new_mod,GFP_KERNEL ); +break ; + } +mas_unlock (&mas); +} + +static int __init alloc_mod_tags_mem (void ) +{ +/* Allocate space to copy allocation tags */ + module_tags.start_addr = (unsigned long )execmem_alloc (EXECMEM_MODULE_DATA , +MODULE_ALLOC_TAG_VMAP_SIZE ); +if (!module_tags.start_addr) +return -ENOMEM ; + + module_tags.end_addr = module_tags.start_addr +MODULE_ALLOC_TAG_VMAP_SIZE ; + +return 0 ; +} + +static void __init free_mod_tags_mem (void ) +{ +execmem_free ((void *)module_tags.start_addr); + module_tags.start_addr =0 ; +} + +#else /* CONFIG_MODULES */ + +static inline int alloc_mod_tags_mem (void ) {return 0 ; } +static inline void free_mod_tags_mem (void ) {} + +#endif /* CONFIG_MODULES */ +static int __init setup_early_mem_profiling (char *str) {bool enable;@@ -274,14 +471,26 @@ static inline void sysctl_init(void) {}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 ), +#ifdef CONFIG_MODULES + .needs_section_mem = needs_section_mem, + .alloc_section_mem = reserve_module_tags, + .free_section_mem = release_module_tags, + .module_replaced = replace_module, +#endif }; +int res; + + res =alloc_mod_tags_mem (); +if (res) +return res; alloc_tag_cttype =codetag_register_type (&desc); -if (IS_ERR (alloc_tag_cttype)) +if (IS_ERR (alloc_tag_cttype)) { +free_mod_tags_mem ();return PTR_ERR (alloc_tag_cttype); + }sysctl_init ();procfs_init ();lib/codetag.c +93
-7@@ -207,6 +207,94 @@ static int codetag_module_init(struct codetag_type *cttype, struct module *mod) }#ifdef CONFIG_MODULES +#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, false); +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); +} +void codetag_load_module (struct module *mod) {struct codetag_type *cttype;@@ -220,13 +308,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 ;/* await any module's kfree_rcu() operations to complete */ kvfree_rcu_barrier ();@@ -246,18 +333,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, true); }mutex_unlock (&codetag_lock); - -return unload_ok; }#endif /* CONFIG_MODULES */ scripts/module.lds.S +3
-3@@ -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 */ --2.47.0.105.g07ac214952-goog
Re: [PATCH v4 4/6] alloc_tag: populate memory for module tags as needed
In reply to Suren BaghdasaryanQuoted 7 lines · Suren Baghdasaryan
The memory reserved for module tags does not need to be backed by physical pages until there are tags to store there. Change the way we reserve this memory to allocate only virtual area for the tags and populate it with physical pages as needed when we load a module.
Signed-off-by Suren Baghdasaryan <[email protected]>
Re: [PATCH v4 6/6] alloc_tag: support for page allocation tag compression
In reply to Suren BaghdasaryanQuoted 12 lines · Suren Baghdasaryan
Implement support for storing page allocation tag references directly in the page flags instead of page extensions. sysctl.vm.mem_profiling boot parameter it extended to provide a way for a user to request this mode. Enabling compression eliminates memory overhead caused by
page_extand results in better performance for page allocations. However this mode will not work if the number of available page flag bits is insufficient to address all kernel allocations. Such condition can happen during boot or when loading a module. If this condition is detected, memory allocation profiling gets disabled with an appropriate warning. By default compression mode is disabled.
Quoted 1 line
Signed-off-by Suren Baghdasaryan <[email protected]>
Thank you very much Suren for doing this work. This is a very significant improvement for the fleet users.
Andrew Morton
maintainerRe: [PATCH v4 5/6] alloc_tag: introduce pgtag_ref_handle to abstract page tag references
In reply to Suren BaghdasaryanQuoted 21 lines · On Wed, 23 Oct 2024 10:07:58 -0700 Suren Baghdasaryan <[email protected]> wrote:
To simplify later changes to page tag references, introduce new
pgtag_ref_handletype. This allows easy replacement ofpage_extas a storage of page allocation tags....
static inline void
pgalloc_tag_copy(struct folio *new, struct folio *old){include/linux/mm.h +3
-3+union pgtag_ref_handle handle; +union codetag_ref ref;struct alloc_tag *tag; -union codetag_ref *ref; tag =pgalloc_tag_get (&old->page);if (!tag)return ; - ref =get_page_tag_ref (&new->page); -if (!ref) +if (!get_page_tag_ref (&new->page, &ref, &handle))return ;
include/linux/mm.h static inline void pgalloc_tag_split(struct folio *folio, int old_order, int new · line 4211
Quoted 10 lines
include/linux/mm.h +1
-5/* Clear the old ref to the original allocation tag. */ clear_page_tag_ref (&old->page);/* Decrement the counters of the tag on get_new_folio. */ -alloc_tag_sub (ref,folio_nr_pages (new)); - -__alloc_tag_ref_set (ref, tag); - -put_page_tag_ref (ref); +alloc_tag_sub (&ref,folio_nr_pages (new));
mm-stable has folio_size(new) here, fixed up.
I think we aleady discussed this, but there's a crazy amount of inlining here. pgalloc_tag_split() is huge, and has four callsites.
Suren Baghdasaryan
maintainerRe: [PATCH v4 5/6] alloc_tag: introduce pgtag_ref_handle to abstract page tag references
Quoted 25 lines · Andrew Morton
Quoted 22 lines · On Wed, 23 Oct 2024 10:07:58 -0700 Suren Baghdasaryan <[email protected]> wrote:
To simplify later changes to page tag references, introduce new
pgtag_ref_handletype. This allows easy replacement ofpage_extas a storage of page allocation tags....
static inline void
pgalloc_tag_copy(struct folio *new, struct folio *old){include/linux/mm.h +3
-3+union pgtag_ref_handle handle; +union codetag_ref ref;struct alloc_tag *tag; -union codetag_ref *ref; tag =pgalloc_tag_get (&old->page);if (!tag)return ; - ref =get_page_tag_ref (&new->page); -if (!ref) +if (!get_page_tag_ref (&new->page, &ref, &handle))return ;
Quoted 11 lines
include/linux/mm.h static inline void pgalloc_tag_split(struct folio *folio, int old_order, int new · line 4211
Quoted 9 lines
/* Clear the old ref to the original allocation tag. */ clear_page_tag_ref(&old->page); /* Decrement the counters of the tag on get_new_folio. */include/linux/mm.h +1
-5-alloc_tag_sub (ref,folio_nr_pages (new)); - -__alloc_tag_ref_set (ref, tag); - -put_page_tag_ref (ref); +alloc_tag_sub (&ref,folio_nr_pages (new));mm-stable has
folio_size(new)here, fixed up.
Oh, right. You merged that patch tonight and I formatted my patchset yesterday :)
Thanks for the fixup.
Quoted 3 lines
I think we aleady discussed this, but there's a crazy amount of inlining here.
pgalloc_tag_split()is huge, and has four callsites.
I must have missed that discussion but I am happy to unline this function. I think splitting is heavy enough operation that this uninlining would not have be noticeable.
Thanks!
Suren Baghdasaryan
maintainerRe: [PATCH v4 6/6] alloc_tag: support for page allocation tag compression
In reply to Pasha TatashinQuoted 14 lines · Pasha Tatashin
Quoted 12 lines · Suren Baghdasaryan
Implement support for storing page allocation tag references directly in the page flags instead of page extensions. sysctl.vm.mem_profiling boot parameter it extended to provide a way for a user to request this mode. Enabling compression eliminates memory overhead caused by
page_extand results in better performance for page allocations. However this mode will not work if the number of available page flag bits is insufficient to address all kernel allocations. Such condition can happen during boot or when loading a module. If this condition is detected, memory allocation profiling gets disabled with an appropriate warning. By default compression mode is disabled.
Quoted 6 lines
Quoted 1 line
Signed-off-by Suren Baghdasaryan <[email protected]>Thank you very much Suren for doing this work. This is a very significant improvement for the fleet users.
Reviewed-by Pasha Tatashin <[email protected]>
Thank you for the reviews and I'm glad it's useful for others as well!
Suren Baghdasaryan
maintainerRe: [PATCH v4 5/6] alloc_tag: introduce pgtag_ref_handle to abstract page tag references
In reply to Suren BaghdasaryanQuoted 44 lines · Suren Baghdasaryan
Quoted 25 lines · Andrew Morton
Quoted 22 lines · On Wed, 23 Oct 2024 10:07:58 -0700 Suren Baghdasaryan <[email protected]> wrote:
To simplify later changes to page tag references, introduce new
pgtag_ref_handletype. This allows easy replacement ofpage_extas a storage of page allocation tags....
static inline void
pgalloc_tag_copy(struct folio *new, struct folio *old){include/linux/mm.h +3
-3+union pgtag_ref_handle handle; +union codetag_ref ref;struct alloc_tag *tag; -union codetag_ref *ref; tag =pgalloc_tag_get (&old->page);if (!tag)return ; - ref =get_page_tag_ref (&new->page); -if (!ref) +if (!get_page_tag_ref (&new->page, &ref, &handle))return ;Quoted 11 lines
include/linux/mm.h static inline void pgalloc_tag_split(struct folio *folio, int old_order, int new · line 4211
Quoted 9 lines
/* Clear the old ref to the original allocation tag. */ clear_page_tag_ref(&old->page); /* Decrement the counters of the tag on get_new_folio. */include/linux/mm.h +1
-5-alloc_tag_sub (ref,folio_nr_pages (new)); - -__alloc_tag_ref_set (ref, tag); - -put_page_tag_ref (ref); +alloc_tag_sub (&ref,folio_nr_pages (new));mm-stable has
folio_size(new)here, fixed up.Oh, right. You merged that patch tonight and I formatted my patchset yesterday :)
Thanks for the fixup.Quoted 1 line
Quoted 6 lines
Quoted 2 lines
I think we aleady discussed this, but there's a crazy amount of inlining here.
pgalloc_tag_split()is huge, and has four callsites.I must have missed that discussion but I am happy to unline this function. I think splitting is heavy enough operation that this uninlining would not have be noticeable.
Posted requested uninlining at
https://lore.kernel.org/all/[email protected]/
Quoted 1 line
Thanks!
kernel test robot
build botRe: [PATCH v4 4/6] alloc_tag: populate memory for module tags as needed
In reply to Suren Baghdasaryankernel test robot noticed the following build errors:
Hi Suren,
kernel test robot noticed the following build errors:
[auto build test ERROR on b5d43fad926a3f542cd06f3c9d286f6f489f7129]
url: https://github.com/intel-lab-lkp/linux/commits/Suren-Baghdasaryan/maple_tree-add-mas_for_each_rev-helper/20241024-010916 base: b5d43fad926a3f542cd06f3c9d286f6f489f7129 patch link: https://lore.kernel.org/r/20241023170759.999909-5-surenb%40google.com patch subject: [PATCH v4 4/6] alloc_tag: populate memory for module tags as needed config: sh-rsk7203_defconfig (https://download.01.org/0day-ci/archive/20241025/[email protected]/config) compiler: sh4-linux-gcc (GCC) 13.3.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241025/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
sh4-linux-ld: mm/execmem.o: in function `execmem_vmap':
>> mm/execmem.c:385:(.text+0xf4): undefined reference to `__get_vm_area_node'
vim +385 mm/execmem.c
370
371 struct vm_struct *execmem_vmap(size_t size)
372 {
373 struct execmem_range *range = &execmem_info->ranges[EXECMEM_MODULE_DATA];
374 struct vm_struct *area;
375
376 area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC,
377 range->start, range->end, NUMA_NO_NODE,
378 GFP_KERNEL, __builtin_return_address(0));
379 if (!area && range->fallback_start)
380 area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC,
381 range->fallback_start, range->fallback_end,
382 NUMA_NO_NODE, GFP_KERNEL, __builtin_return_address(0));
383
384 return area;
> 385 }
386
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Suren Baghdasaryan
maintainerRe: [PATCH v4 4/6] alloc_tag: populate memory for module tags as needed
Quoted 15 lines · kernel test robot
Hi Suren,
kernel test robot noticed the following build errors:
[auto build test ERROR on
b5d43fad926a3f542cd06f3c9d286f6f489f7129]url: https://github.com/intel-lab-lkp/linux/commits/Suren-Baghdasaryan/maple_tree-add-mas_for_each_rev-helper/20241024-010916 base: b5d43fad926a3f542cd06f3c9d286f6f489f7129 patch link: https://lore.kernel.org/r/20241023170759.999909-5-surenb%40google.com patch subject: [PATCH v4 4/6] alloc_tag: populate memory for module tags as needed config: sh-rsk7203_defconfig (https://download.01.org/0day-ci/archive/20241025/[email protected]/config) compiler: sh4-linux-gcc (GCC) 13.3.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241025/[email protected]/reproduce)
Quoted 9 lines
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/All errors (new ones prefixed by >>):
sh4-linux-ld:
mm/execmem.o: in function `execmem_vmap':Quoted 1 line
Quoted 1 line
mm/execmem.c:385:(.text+0xf4): undefined reference to `__get_vm_area_node'
Thanks for the report!
I think this is due to CONFIG_NOMMU=y. I'll fix that in the next version.
Quoted 25 lines
vim +385
mm/execmem.c370 371 struct vm_struct *execmem_vmap(size_t size) 372 { 373 struct execmem_range *range = &execmem_info->ranges[EXECMEM_MODULE_DATA]; 374 struct vm_struct *area; 375 376 area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC, 377 range->start, range->end, NUMA_NO_NODE, 378 GFP_KERNEL, __builtin_return_address(0)); 379 if (!area && range->fallback_start) 380 area = __get_vm_area_node(size, range->alignment, PAGE_SHIFT, VM_ALLOC, 381 range->fallback_start, range->fallback_end, 382 NUMA_NO_NODE, GFP_KERNEL, __builtin_return_address(0)); 383 384 return area;Quoted 1 line
385 }
386
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH v4 4/6] alloc_tag: populate memory for module tags as needed
In reply to Suren Baghdasaryankernel test robot noticed the following build errors:
Hi Suren,
kernel test robot noticed the following build errors:
[auto build test ERROR on b5d43fad926a3f542cd06f3c9d286f6f489f7129]
url: https://github.com/intel-lab-lkp/linux/commits/Suren-Baghdasaryan/maple_tree-add-mas_for_each_rev-helper/20241024-010916 base: b5d43fad926a3f542cd06f3c9d286f6f489f7129 patch link: https://lore.kernel.org/r/20241023170759.999909-5-surenb%40google.com patch subject: [PATCH v4 4/6] alloc_tag: populate memory for module tags as needed config: sh-se7206_defconfig (https://download.01.org/0day-ci/archive/20241025/[email protected]/config) compiler: sh4-linux-gcc (GCC) 13.3.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20241025/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
sh4-linux-ld: mm/execmem.o: in function `execmem_vmap':
>> execmem.c:(.text+0xf4): undefined reference to `__get_vm_area_node'
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki