Memory allocation profiling
Subsystems (55)
Suren Baghdasaryan
maintainerv3 cover Memory allocation profiling
Memory allocation, v3 and final:
Overview:
Low overhead [1] per-callsite memory allocation profiling. Not just for debug kernels, overhead low enough to be deployed in production.
We're aiming to get this in the next merge window, for 6.9. The feedback we've gotten has been that even out of tree this patchset has already been useful, and there's a significant amount of other work gated on the code tagging functionality included in this patchset [2].
Example output:
root@moria-kvm:~# sort -h /proc/allocinfo|tail
3.11MiB 2850 fs/ext4/super.c:1408 module:ext4 func:ext4_alloc_inode
3.52MiB 225 kernel/fork.c:356 module:fork func:alloc_thread_stack_node
3.75MiB 960 mm/page_ext.c:270 module:page_ext func:alloc_page_ext
4.00MiB 2 mm/khugepaged.c:893 module:khugepaged func:hpage_collapse_alloc_folio
10.5MiB 168 block/blk-mq.c:3421 module:blk_mq func:blk_mq_alloc_rqs
14.0MiB 3594 include/linux/gfp.h:295 module:filemap func:folio_alloc_noprof
26.8MiB 6856 include/linux/gfp.h:295 module:memory func:folio_alloc_noprof
64.5MiB 98315 fs/xfs/xfs_rmap_item.c:147 module:xfs func:xfs_rui_init
98.7MiB 25264 include/linux/gfp.h:295 module:readahead func:folio_alloc_noprof
125MiB 7357 mm/slub.c:2201 module:slub func:alloc_slab_pageSince v2:
- tglx noticed a circular header dependency between sched.h and percpu.h; a bunch of header cleanups were merged into 6.8 to ameliorate this [3].
- a number of improvements, moving alloc_hooks() annotations to the correct place for better tracking (mempool), and bugfixes.
- looked at alternate hooking methods.
There were suggestions on alternate methods (compiler attribute, trampolines), but they wouldn't have made the patchset any cleaner (we still need to have different function versions for accounting vs. no accounting to control at which point in a call chain the accounting happens), and they would have added a dependency on toolchain support.
Usage:
kconfig options:
- CONFIG_MEM_ALLOC_PROFILING
- CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT
- CONFIG_MEM_ALLOC_PROFILING_DEBUG
adds warnings for allocations that weren't accounted because of a missing annotation
sysctl:
/proc/sys/vm/mem_profiling
Runtime info:
/proc/allocinfo
Notes:
[1]: Overhead
To measure the overhead we are comparing the following configurations:
(1) Baseline with CONFIG_MEMCG_KMEM=n
(2) Disabled by default (CONFIG_MEM_ALLOC_PROFILING=y && CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT=n)
(3) Enabled by default (CONFIG_MEM_ALLOC_PROFILING=y && CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT=y)
(4) Enabled at runtime (CONFIG_MEM_ALLOC_PROFILING=y &&
CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT=n && /proc/sys/vm/mem_profiling=1)
(5) Baseline with CONFIG_MEMCG_KMEM=y && allocating with __GFP_ACCOUNT
Performance overhead:
To evaluate performance we implemented an in-kernel test executing multiple get_free_page/free_page and kmalloc/kfree calls with allocation sizes growing from 8 to 240 bytes with CPU frequency set to max and CPU affinity set to a specific CPU to minimize the noise. Below are results from running the test on Ubuntu 22.04.2 LTS with 6.8.0-rc1 kernel on 56 core Intel Xeon:
kmalloc pgalloc (1 baseline) 6.764s 16.902s (2 default disabled) 6.793s (+0.43%) 17.007s (+0.62%) (3 default enabled) 7.197s (+6.40%) 23.666s (+40.02%) (4 runtime enabled) 7.405s (+9.48%) 23.901s (+41.41%) (5 memcg) 13.388s (+97.94%) 48.460s (+186.71%)
Memory overhead:
Kernel size:
text data bss dec diff
(1) 26515311 18890222 17018880 62424413 (2) 26524728 19423818 16740352 62688898 264485 (3) 26524724 19423818 16740352 62688894 264481 (4) 26524728 19423818 16740352 62688898 264485 (5) 26541782 18964374 16957440 62463596 39183
Memory consumption on a 56 core Intel CPU with 125GB of memory:
Code tags: 192 kB PageExts: 262144 kB (256MB) SlabExts: 9876 kB (9.6MB) PcpuExts: 512 kB (0.5MB)
Total overhead is 0.2% of total memory.
[2]: Improved fault injection is the big one; the alloc_hooks() macro this patchset introduces is also used for per-callsite fault injection points in the dynamic fault injection patchset, which means we can easily do fault injection on a per module or per file basis; this makes it much easier to integrate memory fault injection into existing tests.
Vlastimil recently raised concerns about exposing GFP_NOWAIT as a PF_MEMALLOC_* flag, as this might introduce GFP_NOWAIT to allocation paths that have never had their failure paths tested - this is something we need to address.
[3]: The circular dependency looks to be unavoidable; the issue is that alloc_tag_save() -> current -> get_current() requires percpu.h, and percpu.h requires sched.h because of course it does. But this doesn't actually cause build errors because we're only using macros, so the main concern is just not leaving a difficult-to-disentangle minefield for later.
So, sched.h is now pretty close to being a types only header that imports types and declares types - this is the header cleanups that were merged for 6.8.
Kent Overstreet (11):
lib/string_helpers: Add flags param to string_get_size() scripts/kallysms: Always include __start and __stop symbols
fs: Convert alloc_inode_sb() to a macro
mm/slub: Mark slab_free_freelist_hook() __always_inline
mempool: Hook up to memory allocation profiling
xfs: Memory allocation profiling fixups
mm: percpu: Introduce pcpuobj_ext
mm: percpu: Add codetag reference into pcpuobj_ext
mm: vmalloc: Enable memory allocation profiling
rhashtable: Plumb through alloc tag
MAINTAINERS: Add entries for code tagging and memory allocation
profiling
Suren Baghdasaryan (24):
mm: enumerate all gfp flags
mm: introduce slabobj_ext to support slab object extensions
mm: introduce __GFP_NO_OBJ_EXT flag to selectively prevent slabobj_ext
creation
mm/slab: introduce SLAB_NO_OBJ_EXT to avoid obj_ext creation
mm: prevent slabobj_ext allocations for slabobj_ext and kmem_cache
objects
slab: objext: introduce objext_flags as extension to
page_memcg_data_flags
lib: code tagging framework
lib: code tagging module support
lib: prevent module unloading if memory is not freed
lib: add allocation tagging support for memory allocation profiling
lib: introduce support for page allocation tagging
mm: percpu: increase PERCPU_MODULE_RESERVE to accommodate allocation
tags
change alloc_pages name in dma_map_ops to avoid name conflicts
mm: enable page allocation tagging
mm: create new codetag references during page splitting mm/page_ext: enable early_page_ext when
CONFIG_MEM_ALLOC_PROFILING_DEBUG=y
lib: add codetag reference into slabobj_ext
mm/slab: add allocation accounting into slab allocation and free paths mm/slab: enable slab allocation tagging for kmalloc and friends
mm: percpu: enable per-cpu allocation tagging
lib: add memory allocations report in show_mem()
codetag: debug: skip objext checking when it's for objext itself
codetag: debug: mark codetags for reserved pages as empty
codetag: debug: introduce OBJEXTS_ALLOC_FAIL to mark failed slab_ext
allocations
Documentation/admin-guide/sysctl/vm.rst | 16 ++ Documentation/filesystems/proc.rst | 28 ++ MAINTAINERS | 16 ++ arch/alpha/kernel/pci_iommu.c | 2 +- arch/mips/jazz/jazzdma.c | 2 +- arch/powerpc/kernel/dma-iommu.c | 2 +- arch/powerpc/mm/book3s64/radix_pgtable.c | 2 +- arch/powerpc/platforms/ps3/system-bus.c | 4 +- arch/powerpc/platforms/pseries/vio.c | 2 +- arch/x86/kernel/amd_gart_64.c | 2 +- drivers/block/virtio_blk.c | 4 +- drivers/gpu/drm/gud/gud_drv.c | 2 +- drivers/iommu/dma-iommu.c | 2 +- drivers/mmc/core/block.c | 4 +- drivers/mtd/spi-nor/debugfs.c | 6 +- .../ethernet/chelsio/cxgb4/cxgb4_debugfs.c | 4 +- drivers/parisc/ccio-dma.c | 2 +- drivers/parisc/sba_iommu.c | 2 +- drivers/scsi/sd.c | 8 +- drivers/staging/media/atomisp/pci/hmm/hmm.c | 2 +- drivers/xen/grant-dma-ops.c | 2 +- drivers/xen/swiotlb-xen.c | 2 +- fs/xfs/kmem.c | 4 +- fs/xfs/kmem.h | 10 +- include/asm-generic/codetag.lds.h | 14 + include/asm-generic/vmlinux.lds.h | 3 + include/linux/alloc_tag.h | 188 +++++++++++++ include/linux/codetag.h | 83 ++++++ include/linux/dma-map-ops.h | 2 +- include/linux/fortify-string.h | 5 +- include/linux/fs.h | 6 +- include/linux/gfp.h | 126 +++++---- include/linux/gfp_types.h | 101 +++++-- include/linux/memcontrol.h | 56 +++- include/linux/mempool.h | 73 +++-- include/linux/mm.h | 8 + include/linux/mm_types.h | 4 +- include/linux/page_ext.h | 1 - include/linux/pagemap.h | 9 +- include/linux/percpu.h | 27 +- include/linux/pgalloc_tag.h | 105 +++++++ include/linux/rhashtable-types.h | 11 +- include/linux/sched.h | 24 ++ include/linux/slab.h | 184 +++++++------ include/linux/string.h | 4 +- include/linux/string_helpers.h | 11 +- include/linux/vmalloc.h | 60 +++- init/Kconfig | 4 + kernel/dma/mapping.c | 4 +- kernel/kallsyms_selftest.c | 2 +- kernel/module/main.c | 25 +- lib/Kconfig.debug | 31 +++ lib/Makefile | 3 + lib/alloc_tag.c | 213 +++++++++++++++ lib/codetag.c | 258 ++++++++++++++++++ lib/rhashtable.c | 52 +++- lib/string_helpers.c | 22 +- lib/test-string_helpers.c | 4 +- mm/compaction.c | 7 +- mm/filemap.c | 6 +- mm/huge_memory.c | 2 + mm/hugetlb.c | 8 +- mm/kfence/core.c | 14 +- mm/kfence/kfence.h | 4 +- mm/memcontrol.c | 56 +--- mm/mempolicy.c | 52 ++-- mm/mempool.c | 36 +-- mm/mm_init.c | 10 + mm/page_alloc.c | 66 +++-- mm/page_ext.c | 13 + mm/page_owner.c | 2 +- mm/percpu-internal.h | 26 +- mm/percpu.c | 120 ++++---- mm/show_mem.c | 15 + mm/slab.h | 176 ++++++++++-- mm/slab_common.c | 65 ++++- mm/slub.c | 138 ++++++---- mm/util.c | 44 +-- mm/vmalloc.c | 88 +++--- scripts/kallsyms.c | 13 + scripts/module.lds.S | 7 + 81 files changed, 2126 insertions(+), 695 deletions(-) create mode 100644 include/asm-generic/codetag.lds.h create mode 100644 include/linux/alloc_tag.h create mode 100644 include/linux/codetag.h create mode 100644 include/linux/pgalloc_tag.h create mode 100644 lib/alloc_tag.c create mode 100644 lib/codetag.c
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 1/35 lib/string_helpers: Add flags param to string_get_size()
From: Kent Overstreet <[email protected]>
The new flags parameter allows controlling
- Whether or not the units suffix is separated by a space, for compatibility with sort -h
- Whether or not to append a B suffix - we're not always printing bytes.
--- arch/powerpc/mm/book3s64/radix_pgtable.c | 2 +- drivers/block/virtio_blk.c | 4 ++-- drivers/gpu/drm/gud/gud_drv.c | 2 +- drivers/mmc/core/block.c | 4 ++-- drivers/mtd/spi-nor/debugfs.c | 6 ++--- .../ethernet/chelsio/cxgb4/cxgb4_debugfs.c | 4 ++-- drivers/scsi/sd.c | 8 +++---- include/linux/string_helpers.h | 11 +++++----- lib/string_helpers.c | 22 ++++++++++++++----- lib/test-string_helpers.c | 4 ++-- mm/hugetlb.c | 8 +++---- 11 files changed, 42 insertions(+), 33 deletions(-)
arch/powerpc/mm/book3s64/radix_pgtable.c +1 -1
@@ -260,7 +260,7 @@ print_mapping(unsigned long start, unsigned long end, unsigned long size, bool eif (end <= start)return ; -string_get_size (size,1 ,STRING_UNITS_2 , buf,sizeof (buf)); +string_get_size (size,1 ,STRING_SIZE_BASE2 , buf,sizeof (buf));pr_info ("Mapped 0x%016lx-0x%016lx with %s pages%s\n" , start, end, buf, exec ?" (exec)" :"" );
drivers/block/virtio_blk.c +2 -2
@@ -934,9 +934,9 @@ static void virtblk_update_capacity(struct virtio_blk *vblk, bool resize) nblocks =DIV_ROUND_UP_ULL (capacity,queue_logical_block_size (q) >>9 );string_get_size (nblocks,queue_logical_block_size (q), -STRING_UNITS_2 , cap_str_2,sizeof (cap_str_2)); +STRING_SIZE_BASE2 , cap_str_2,sizeof (cap_str_2));string_get_size (nblocks,queue_logical_block_size (q), -STRING_UNITS_10 , cap_str_10,sizeof (cap_str_10)); +0 , cap_str_10,sizeof (cap_str_10)); dev_notice(&vdev->dev, "[%s] %s%llu %d-byte logical blocks (%s/%s)\n",
drivers/gpu/drm/gud/gud_drv.c +1 -1
@@ -329,7 +329,7 @@ static int gud_stats_debugfs(struct seq_file *m, void *data)struct gud_device *gdrm =to_gud_device (entry->dev);char buf[10 ]; -string_get_size (gdrm->bulk_len,1 ,STRING_UNITS_2 , buf,sizeof (buf)); +string_get_size (gdrm->bulk_len,1 ,STRING_SIZE_BASE2 , buf,sizeof (buf));seq_printf (m,"Max buffer size: %s\n" , buf);seq_printf (m,"Number of errors: %u\n" , gdrm->stats_num_errors);
drivers/mmc/core/block.c +2 -2
@@ -2557,7 +2557,7 @@ static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,blk_queue_write_cache (md->queue.queue, cache_enabled, fua_enabled); -string_get_size ((u64 )size,512 ,STRING_UNITS_2 , +string_get_size ((u64 )size,512 ,STRING_SIZE_BASE2 , cap_str,sizeof (cap_str)); pr_info("%s: %s %s %s%s\n", md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
@@ -2753,7 +2753,7 @@ static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,list_add (&rpmb->node, &md->rpmbs); -string_get_size ((u64 )size,512 ,STRING_UNITS_2 , +string_get_size ((u64 )size,512 ,STRING_SIZE_BASE2 , cap_str,sizeof (cap_str)); pr_info("%s: %s %s %s, chardev (%d:%d)\n",
drivers/mtd/spi-nor/debugfs.c +3 -3
@@ -85,7 +85,7 @@ static int spi_nor_params_show(struct seq_file *s, void *data)seq_printf (s,"name\t\t%s\n" , info->name);seq_printf (s,"id\t\t%*ph\n" ,SPI_NOR_MAX_ID_LEN , nor->id); -string_get_size (params->size,1 ,STRING_UNITS_2 , buf,sizeof (buf)); +string_get_size (params->size,1 ,STRING_SIZE_BASE2 , buf,sizeof (buf));seq_printf (s,"size\t\t%s\n" , buf);seq_printf (s,"write size\t%u\n" , params->writesize);seq_printf (s,"page size\t%u\n" , params->page_size);
@@ -130,14 +130,14 @@ static int spi_nor_params_show(struct seq_file *s, void *data)struct spi_nor_erase_type *et = &erase_map->erase_type[i];if (et->size) { -string_get_size (et->size,1 ,STRING_UNITS_2 , buf, +string_get_size (et->size,1 ,STRING_SIZE_BASE2 , buf,sizeof (buf));seq_printf (s," %02x (%s) [%d]\n" , et->opcode, buf, i); } }if (!(nor->flags &SNOR_F_NO_OP_CHIP_ERASE )) { -string_get_size (params->size,1 ,STRING_UNITS_2 , buf,sizeof (buf)); +string_get_size (params->size,1 ,STRING_SIZE_BASE2 , buf,sizeof (buf));seq_printf (s," %02x (%s)\n" , nor->params->die_erase_opcode, buf); }
drivers/net/ethernet/chelsio/cxgb4/cxgb4_debugfs.c +2 -2
@@ -3457,8 +3457,8 @@ static void mem_region_show(struct seq_file *seq, const char *name, {char buf[40 ]; -string_get_size ((u64 )to - from +1 ,1 ,STRING_UNITS_2 , buf, -sizeof (buf)); +string_get_size ((u64 )to - from +1 ,1 ,STRING_SIZE_BASE2 , + buf,sizeof (buf));seq_printf (seq,"%-15s %#x-%#x [%s]\n" , name, from, to, buf); }
drivers/scsi/sd.c +4 -4
@@ -2731,10 +2731,10 @@ sd_print_capacity(struct scsi_disk *sdkp,if (!sdkp->first_scan && old_capacity == sdkp->capacity)return ; -string_get_size (sdkp->capacity, sector_size, -STRING_UNITS_2 , cap_str_2,sizeof (cap_str_2)); -string_get_size (sdkp->capacity, sector_size, -STRING_UNITS_10 , cap_str_10,sizeof (cap_str_10)); +string_get_size (sdkp->capacity, sector_size,STRING_SIZE_BASE2 , + cap_str_2,sizeof (cap_str_2)); +string_get_size (sdkp->capacity, sector_size,0 , + cap_str_10,sizeof (cap_str_10)); sd_printk(KERN_NOTICE, sdkp, "%llu %d-byte logical blocks: (%s/%s)\n",
include/linux/string_helpers.h +5 -6
@@ -17,14 +17,13 @@ static inline bool string_is_terminated(const char *s, int len)return memchr (s,'\0' , len) ? true : false; } -/* Descriptions of the types of units to -* print in */ -enum string_size_units { -STRING_UNITS_10 ,/* use powers of 10^3 (standard SI) */ -STRING_UNITS_2 ,/* use binary powers of 2^10 */ +enum string_size_flags { +STRING_SIZE_BASE2 = (1 <<0 ), +STRING_SIZE_NOSPACE = (1 <<1 ), +STRING_SIZE_NOBYTES = (1 <<2 ), }; -int string_get_size (u64 size,u64 blk_size,enum string_size_units units, +int string_get_size (u64 size,u64 blk_size,enum string_size_flags flags,char *buf,int len);int parse_int_array_user (const char __user *from,size_t count,int **array);
lib/string_helpers.c +16 -6
@@ -19,11 +19,17 @@#include <linux/string.h> #include <linux/string_helpers.h> +enum string_size_units { +STRING_UNITS_10 ,/* use powers of 10^3 (standard SI) */ +STRING_UNITS_2 ,/* use binary powers of 2^10 */ +}; +/** * string_get_size - get the size in the specified units * @size: The size to be converted in blocks * @blk_size: Size of the block (use 1 for size in bytes) -* @units: units to use (powers of 1000 or 1024) +* @flags: units to use (powers of 1000 or 1024), whether to include space +* separator * @buf: buffer to format to * @len: length of buffer *
@@ -34,14 +40,16 @@* Return value: number of characters of output that would have been written * (which may be greater than len, if output was truncated). */ -int string_get_size (u64 size,u64 blk_size,const enum string_size_units units, +int string_get_size (u64 size,u64 blk_size,enum string_size_flags flags,char *buf,int len) { +enum string_size_units units = flags & flags &STRING_SIZE_BASE2 + ?STRING_UNITS_2 :STRING_UNITS_10 ;static const char *const units_10[] = { -"B" ,"kB" ,"MB" ,"GB" ,"TB" ,"PB" ,"EB" ,"ZB" ,"YB" +"" ,"k" ,"M" ,"G" ,"T" ,"P" ,"E" ,"Z" ,"Y" };static const char *const units_2[] = { -"B" ,"KiB" ,"MiB" ,"GiB" ,"TiB" ,"PiB" ,"EiB" ,"ZiB" ,"YiB" +"" ,"Ki" ,"Mi" ,"Gi" ,"Ti" ,"Pi" ,"Ei" ,"Zi" ,"Yi" };static const char *const *const units_str[] = { [STRING_UNITS_10 ] = units_10,
@@ -128,8 +136,10 @@ int string_get_size(u64 size, u64 blk_size, const enum string_size_units units,else unit = units_str[units][i]; -return snprintf (buf, len,"%u%s %s" , (u32 )size, - tmp, unit); +return snprintf (buf, len,"%u%s%s%s%s" , (u32 )size, tmp, + (flags &STRING_SIZE_NOSPACE ) ?"" :" " , + unit, + (flags &STRING_SIZE_NOBYTES ) ?"" :"B" ); }EXPORT_SYMBOL (string_get_size);
lib/test-string_helpers.c +2 -2
@@ -507,8 +507,8 @@ static __init void __test_string_get_size(const u64 size, const u64 blk_size,char buf10[string_get_size_maxbuf];char buf2[string_get_size_maxbuf]; -string_get_size (size, blk_size,STRING_UNITS_10 , buf10,sizeof (buf10)); -string_get_size (size, blk_size,STRING_UNITS_2 , buf2,sizeof (buf2)); +string_get_size (size, blk_size,0 , buf10,sizeof (buf10)); +string_get_size (size, blk_size,STRING_SIZE_BASE2 , buf2,sizeof (buf2));test_string_get_size_check ("STRING_UNITS_10" , exp_result10, buf10, size, blk_size);
mm/hugetlb.c +4 -4
@@ -3475,7 +3475,7 @@ static void __init hugetlb_hstate_alloc_pages_onenode(struct hstate *h, int nid)if (i == h->max_huge_pages_node[nid])return ; -string_get_size (huge_page_size (h),1 ,STRING_UNITS_2 , buf,32 ); +string_get_size (huge_page_size (h),1 ,STRING_SIZE_BASE2 , buf,32 );pr_warn ("HugeTLB: allocating %u of page size %s failed node%d. Only allocated %lu hugepages.\n" , h->max_huge_pages_node[nid], buf, nid, i); h->max_huge_pages -= (h->max_huge_pages_node[nid] - i);
@@ -3561,7 +3561,7 @@ static void __init hugetlb_hstate_alloc_pages(struct hstate *h)if (i < h->max_huge_pages) {char buf[32 ]; -string_get_size (huge_page_size (h),1 ,STRING_UNITS_2 , buf,32 ); +string_get_size (huge_page_size (h),1 ,STRING_SIZE_BASE2 , buf,32 );pr_warn ("HugeTLB: allocating %lu of page size %s failed. Only allocated %lu hugepages.\n" , h->max_huge_pages, buf, i); h->max_huge_pages = i;
@@ -3607,7 +3607,7 @@ static void __init report_hugepages(void)for_each_hstate (h) {char buf[32 ]; -string_get_size (huge_page_size (h),1 ,STRING_UNITS_2 , buf,32 ); +string_get_size (huge_page_size (h),1 ,STRING_SIZE_BASE2 , buf,32 );pr_info ("HugeTLB: registered %s page size, pre-allocated %ld pages\n" , buf, h->free_huge_pages); pr_info("HugeTLB: %d KiB vmemmap can be freed for a %s page\n",
@@ -4527,7 +4527,7 @@ static int __init hugetlb_init(void)char buf[32 ];string_get_size (huge_page_size (&default_hstate), -1 ,STRING_UNITS_2 , buf,32 ); +1 ,STRING_SIZE_BASE2 , buf,32 );pr_warn ("HugeTLB: Ignoring hugepages=%lu associated with %s page size\n" , default_hstate.max_huge_pages, buf); pr_warn("HugeTLB: Using hugepages=%lu for number of default huge pages\n",
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 2/35 scripts/kallysms: Always include __start and __stop symbols
In reply to Suren BaghdasaryanFrom: Kent Overstreet <[email protected]>
These symbols are used to denote section boundaries: by always including them we can unify loading sections from modules with loading built-in sections, which leads to some significant cleanup.
--- scripts/kallsyms.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
scripts/kallsyms.c +13 -0
@@ -204,6 +204,11 @@ static int symbol_in_range(const struct sym_entry *s,return 0 ; } +static bool string_starts_with (const char *s,const char *prefix) +{ +return strncmp (s, prefix,strlen (prefix)) ==0 ; +} +static int symbol_valid (const struct sym_entry *s) {const char *name =sym_name (s);
@@ -211,6 +216,14 @@ static int symbol_valid(const struct sym_entry *s)/* if --all-symbols is not specified, then symbols outside the text * and inittext sections are discarded */ if (!all_symbols) { +/* +* Symbols starting with __start and __stop are used to denote +* section boundaries, and should always be included: +*/ +if (string_starts_with (name,"__start_" ) || +string_starts_with (name,"__stop_" )) +return 1 ; +if (symbol_in_range (s, text_ranges,ARRAY_SIZE (text_ranges)) ==0 )return 0 ;
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 3/35 fs: Convert alloc_inode_sb() to a macro
In reply to Suren BaghdasaryanFrom: Kent Overstreet <[email protected]>
We're introducing alloc tagging, which tracks memory allocations by callsite. Converting alloc_inode_sb() to a macro means allocations will be tracked by its caller, which is a bit more useful.
--- include/linux/fs.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
include/linux/fs.h +1 -5
@@ -3013,11 +3013,7 @@ int setattr_should_drop_sgid(struct mnt_idmap *idmap,* This must be used for allocating filesystems specific inodes to set * up the inode reclaim context correctly. */ -static inline void * -alloc_inode_sb (struct super_block *sb,struct kmem_cache *cache,gfp_t gfp) -{ -return kmem_cache_alloc_lru (cache, &sb->s_inode_lru, gfp); -} +#define alloc_inode_sb (_sb, _cache, _gfp) kmem_cache_alloc_lru(_cache, &_sb->s_inode_lru, _gfp)extern void __insert_inode_hash (struct inode *,unsigned long hashval);static inline void insert_inode_hash (struct inode *inode)
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 4/35 mm: enumerate all gfp flags
In reply to Suren BaghdasaryanIntroduce GFP bits enumeration to let compiler track the number of used bits (which depends on the config options) instead of hardcoding them. That simplifies __GFP_BITS_SHIFT calculation.
--- include/linux/gfp_types.h | 90 +++++++++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 28 deletions(-)
include/linux/gfp_types.h +62 -28
@@ -21,44 +21,78 @@ typedef unsigned int __bitwise gfp_t;* include/trace/events/mmflags.h and tools/perf/builtin-kmem.c */ +enum { + ___GFP_DMA_BIT, + ___GFP_HIGHMEM_BIT, + ___GFP_DMA32_BIT, + ___GFP_MOVABLE_BIT, + ___GFP_RECLAIMABLE_BIT, + ___GFP_HIGH_BIT, + ___GFP_IO_BIT, + ___GFP_FS_BIT, + ___GFP_ZERO_BIT, + ___GFP_UNUSED_BIT,/* 0x200u unused */ + ___GFP_DIRECT_RECLAIM_BIT, + ___GFP_KSWAPD_RECLAIM_BIT, + ___GFP_WRITE_BIT, + ___GFP_NOWARN_BIT, + ___GFP_RETRY_MAYFAIL_BIT, + ___GFP_NOFAIL_BIT, + ___GFP_NORETRY_BIT, + ___GFP_MEMALLOC_BIT, + ___GFP_COMP_BIT, + ___GFP_NOMEMALLOC_BIT, + ___GFP_HARDWALL_BIT, + ___GFP_THISNODE_BIT, + ___GFP_ACCOUNT_BIT, + ___GFP_ZEROTAGS_BIT, +#ifdef CONFIG_KASAN_HW_TAGS + ___GFP_SKIP_ZERO_BIT, + ___GFP_SKIP_KASAN_BIT, +#endif +#ifdef CONFIG_LOCKDEP + ___GFP_NOLOCKDEP_BIT, +#endif + ___GFP_LAST_BIT +}; +/* Plain integer GFP bitmasks. Do not use this directly. */ -#define ___GFP_DMA 0x01u -#define ___GFP_HIGHMEM 0x02u -#define ___GFP_DMA32 0x04u -#define ___GFP_MOVABLE 0x08u -#define ___GFP_RECLAIMABLE 0x10u -#define ___GFP_HIGH 0x20u -#define ___GFP_IO 0x40u -#define ___GFP_FS 0x80u -#define ___GFP_ZERO 0x100u +#define ___GFP_DMA BIT(___GFP_DMA_BIT) +#define ___GFP_HIGHMEM BIT(___GFP_HIGHMEM_BIT) +#define ___GFP_DMA32 BIT(___GFP_DMA32_BIT) +#define ___GFP_MOVABLE BIT(___GFP_MOVABLE_BIT) +#define ___GFP_RECLAIMABLE BIT(___GFP_RECLAIMABLE_BIT) +#define ___GFP_HIGH BIT(___GFP_HIGH_BIT) +#define ___GFP_IO BIT(___GFP_IO_BIT) +#define ___GFP_FS BIT(___GFP_FS_BIT) +#define ___GFP_ZERO BIT(___GFP_ZERO_BIT)/* 0x200u unused */ -#define ___GFP_DIRECT_RECLAIM 0x400u -#define ___GFP_KSWAPD_RECLAIM 0x800u -#define ___GFP_WRITE 0x1000u -#define ___GFP_NOWARN 0x2000u -#define ___GFP_RETRY_MAYFAIL 0x4000u -#define ___GFP_NOFAIL 0x8000u -#define ___GFP_NORETRY 0x10000u -#define ___GFP_MEMALLOC 0x20000u -#define ___GFP_COMP 0x40000u -#define ___GFP_NOMEMALLOC 0x80000u -#define ___GFP_HARDWALL 0x100000u -#define ___GFP_THISNODE 0x200000u -#define ___GFP_ACCOUNT 0x400000u -#define ___GFP_ZEROTAGS 0x800000u +#define ___GFP_DIRECT_RECLAIM BIT(___GFP_DIRECT_RECLAIM_BIT) +#define ___GFP_KSWAPD_RECLAIM BIT(___GFP_KSWAPD_RECLAIM_BIT) +#define ___GFP_WRITE BIT(___GFP_WRITE_BIT) +#define ___GFP_NOWARN BIT(___GFP_NOWARN_BIT) +#define ___GFP_RETRY_MAYFAIL BIT(___GFP_RETRY_MAYFAIL_BIT) +#define ___GFP_NOFAIL BIT(___GFP_NOFAIL_BIT) +#define ___GFP_NORETRY BIT(___GFP_NORETRY_BIT) +#define ___GFP_MEMALLOC BIT(___GFP_MEMALLOC_BIT) +#define ___GFP_COMP BIT(___GFP_COMP_BIT) +#define ___GFP_NOMEMALLOC BIT(___GFP_NOMEMALLOC_BIT) +#define ___GFP_HARDWALL BIT(___GFP_HARDWALL_BIT) +#define ___GFP_THISNODE BIT(___GFP_THISNODE_BIT) +#define ___GFP_ACCOUNT BIT(___GFP_ACCOUNT_BIT) +#define ___GFP_ZEROTAGS BIT(___GFP_ZEROTAGS_BIT)#ifdef CONFIG_KASAN_HW_TAGS -#define ___GFP_SKIP_ZERO 0x1000000u -#define ___GFP_SKIP_KASAN 0x2000000u +#define ___GFP_SKIP_ZERO BIT(___GFP_SKIP_ZERO_BIT) +#define ___GFP_SKIP_KASAN BIT(___GFP_SKIP_KASAN_BIT)#else #define ___GFP_SKIP_ZERO 0#define ___GFP_SKIP_KASAN 0#endif #ifdef CONFIG_LOCKDEP -#define ___GFP_NOLOCKDEP 0x4000000u +#define ___GFP_NOLOCKDEP BIT(___GFP_NOLOCKDEP_BIT)#else #define ___GFP_NOLOCKDEP 0#endif -/* If the above are modified, __GFP_BITS_SHIFT may need updating */ /* * Physical address zone modifiers (see linux/mmzone.h - low four bits)
@@ -249,7 +283,7 @@ typedef unsigned int __bitwise gfp_t;#define __GFP_NOLOCKDEP ((__force gfp_t)___GFP_NOLOCKDEP)/* Room for N __GFP_FOO bits */ -#define __GFP_BITS_SHIFT (26 + IS_ENABLED(CONFIG_LOCKDEP)) +#define __GFP_BITS_SHIFT ___GFP_LAST_BIT#define __GFP_BITS_MASK ((__force gfp_t)((1 << __GFP_BITS_SHIFT) - 1))/**
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 5/35 mm: introduce slabobj_ext to support slab object extensions
In reply to Suren BaghdasaryanCurrently slab pages can store only vectors of obj_cgroup pointers in page->memcg_data. Introduce slabobj_ext structure to allow more data to be stored for each slab object. Wrap obj_cgroup into slabobj_ext to support current functionality while allowing to extend slabobj_ext in the future.
--- include/linux/memcontrol.h | 20 ++++++--- include/linux/mm_types.h | 4 +- init/Kconfig | 4 ++ mm/kfence/core.c | 14 +++--- mm/kfence/kfence.h | 4 +- mm/memcontrol.c | 56 +++-------------------- mm/page_owner.c | 2 +- mm/slab.h | 92 +++++++++++++++++++++++++++++--------- mm/slab_common.c | 48 ++++++++++++++++++++ mm/slub.c | 64 +++++++++++++------------- 10 files changed, 189 insertions(+), 119 deletions(-)
include/linux/memcontrol.h +14 -6
@@ -348,8 +348,8 @@ struct mem_cgroup {extern struct mem_cgroup *root_mem_cgroup;enum page_memcg_data_flags { -/* page->memcg_data is a pointer to an objcgs vector */ -MEMCG_DATA_OBJCGS = (1UL <<0 ), +/* page->memcg_data is a pointer to an slabobj_ext vector */ +MEMCG_DATA_OBJEXTS = (1UL <<0 ),/* page has been accounted as a non-slab kernel page */ MEMCG_DATA_KMEM = (1UL <<1 ),/* the next bit after the last actual flag */
@@ -387,7 +387,7 @@ static inline struct mem_cgroup *__folio_memcg(struct folio *folio)unsigned long memcg_data = folio->memcg_data;VM_BUG_ON_FOLIO (folio_test_slab (folio), folio); -VM_BUG_ON_FOLIO (memcg_data &MEMCG_DATA_OBJCGS , folio); +VM_BUG_ON_FOLIO (memcg_data &MEMCG_DATA_OBJEXTS , folio);VM_BUG_ON_FOLIO (memcg_data &MEMCG_DATA_KMEM , folio);return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK );
@@ -408,7 +408,7 @@ static inline struct obj_cgroup *__folio_objcg(struct folio *folio)unsigned long memcg_data = folio->memcg_data;VM_BUG_ON_FOLIO (folio_test_slab (folio), folio); -VM_BUG_ON_FOLIO (memcg_data &MEMCG_DATA_OBJCGS , folio); +VM_BUG_ON_FOLIO (memcg_data &MEMCG_DATA_OBJEXTS , folio);VM_BUG_ON_FOLIO (!(memcg_data &MEMCG_DATA_KMEM ), folio);return (struct obj_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK );
@@ -505,7 +505,7 @@ static inline struct mem_cgroup *folio_memcg_check(struct folio *folio)*/ unsigned long memcg_data =READ_ONCE (folio->memcg_data); -if (memcg_data &MEMCG_DATA_OBJCGS ) +if (memcg_data &MEMCG_DATA_OBJEXTS )return NULL ;if (memcg_data &MEMCG_DATA_KMEM ) {
@@ -551,7 +551,7 @@ static inline struct mem_cgroup *get_mem_cgroup_from_objcg(struct obj_cgroup *obstatic inline bool folio_memcg_kmem (struct folio *folio) {VM_BUG_ON_PGFLAGS (PageTail (&folio->page), &folio->page); -VM_BUG_ON_FOLIO (folio->memcg_data &MEMCG_DATA_OBJCGS , folio); +VM_BUG_ON_FOLIO (folio->memcg_data &MEMCG_DATA_OBJEXTS , folio);return folio->memcg_data &MEMCG_DATA_KMEM ; }
@@ -1633,6 +1633,14 @@ unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order, }#endif /* CONFIG_MEMCG */ +/* +* Extended information for slab objects stored as an array in page->memcg_data +* if MEMCG_DATA_OBJEXTS is set. +*/ +struct slabobj_ext { +struct obj_cgroup *objcg; +}__aligned(8) ; +static inline void __inc_lruvec_kmem_state (void *p,enum node_stat_item idx) {__mod_lruvec_kmem_state (p, idx,1 );
include/linux/mm_types.h +2 -2
@@ -169,7 +169,7 @@ struct page {/* Usage count. *DO NOT USE DIRECTLY*. See page_ref.h */ atomic_t _refcount; -#ifdef CONFIG_MEMCG +#ifdef CONFIG_SLAB_OBJ_EXT unsigned long memcg_data;#endif
@@ -306,7 +306,7 @@ struct folio { };atomic_t _mapcount;atomic_t _refcount; -#ifdef CONFIG_MEMCG +#ifdef CONFIG_SLAB_OBJ_EXT unsigned long memcg_data;#endif #if defined(WANT_PAGE_VIRTUAL )
init/Kconfig +4 -0
@@ -949,10 +949,14 @@ config CGROUP_FAVOR_DYNMODS Say N if unsure. +config SLAB_OBJ_EXT + bool + config MEMCG bool "Memory controller" select PAGE_COUNTER select EVENTFD + select SLAB_OBJ_EXT help Provides control over the memory footprint of tasks in a cgroup.
mm/kfence/core.c +7 -7
@@ -595,9 +595,9 @@ static unsigned long kfence_init_pool(void)continue ;__folio_set_slab (slab_folio (slab)); -#ifdef CONFIG_MEMCG - slab->memcg_data = (unsigned long )&kfence_metadata_init[i /2 -1 ].objcg | -MEMCG_DATA_OBJCGS ; +#ifdef CONFIG_MEMCG_KMEM + slab->obj_exts = (unsigned long )&kfence_metadata_init[i /2 -1 ].obj_exts | +MEMCG_DATA_OBJEXTS ;#endif }
@@ -645,8 +645,8 @@ static unsigned long kfence_init_pool(void)if (!i || (i %2 ))continue ; -#ifdef CONFIG_MEMCG - slab->memcg_data =0 ; +#ifdef CONFIG_MEMCG_KMEM + slab->obj_exts =0 ;#endif __folio_clear_slab (slab_folio (slab)); }
@@ -1139,8 +1139,8 @@ void __kfence_free(void *addr) {struct kfence_metadata *meta =addr_to_metadata ((unsigned long )addr); -#ifdef CONFIG_MEMCG -KFENCE_WARN_ON (meta->objcg); +#ifdef CONFIG_MEMCG_KMEM +KFENCE_WARN_ON (meta->obj_exts.objcg);#endif /* * If the objects of the cache are SLAB_TYPESAFE_BY_RCU, defer freeing
mm/kfence/kfence.h +2 -2
@@ -97,8 +97,8 @@ struct kfence_metadata {struct kfence_track free_track;/* For updating alloc_covered on frees. */ u32 alloc_stack_hash; -#ifdef CONFIG_MEMCG -struct obj_cgroup *objcg; +#ifdef CONFIG_MEMCG_KMEM +struct slabobj_ext obj_exts;#endif };
mm/memcontrol.c +7 -49
89 lines not on this page · raw message
mm/page_owner.c +1 -1
@@ -377,7 +377,7 @@ static inline int print_page_owner_memcg(char *kbuf, size_t count, int ret,if (!memcg_data) goto out_unlock; -if (memcg_data &MEMCG_DATA_OBJCGS ) +if (memcg_data &MEMCG_DATA_OBJEXTS ) ret +=scnprintf (kbuf + ret, count - ret,"Slab cache page\n" );
mm/slab.h +70 -22
128 lines not on this page · raw message
mm/slab_common.c +48 -0
54 lines not on this page · raw message
mm/slub.c +34 -30
161 lines not on this page · raw message
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 6/35 mm: introduce __GFP_NO_OBJ_EXT flag to selectively prevent slabobj_ext creation
In reply to Suren BaghdasaryanIntroduce __GFP_NO_OBJ_EXT flag in order to prevent recursive allocations when allocating slabobj_ext on a slab.
--- include/linux/gfp_types.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
include/linux/gfp_types.h +11 -0
@@ -52,6 +52,9 @@ enum {#endif #ifdef CONFIG_LOCKDEP ___GFP_NOLOCKDEP_BIT, +#endif +#ifdef CONFIG_SLAB_OBJ_EXT + ___GFP_NO_OBJ_EXT_BIT, #endif ___GFP_LAST_BIT };
@@ -93,6 +96,11 @@ enum {#else #define ___GFP_NOLOCKDEP 0#endif +#ifdef CONFIG_SLAB_OBJ_EXT +#define ___GFP_NO_OBJ_EXT BIT(___GFP_NO_OBJ_EXT_BIT) +#else +#define ___GFP_NO_OBJ_EXT 0 +#endif /* * Physical address zone modifiers (see linux/mmzone.h - low four bits)
@@ -133,12 +141,15 @@ enum {* node with no fallbacks or placement policy enforcements. * * %__GFP_ACCOUNT causes the allocation to be accounted to kmemcg. +* +* %__GFP_NO_OBJ_EXT causes slab allocation to have no object extension. */ #define __GFP_RECLAIMABLE ((__force gfp_t)___GFP_RECLAIMABLE)#define __GFP_WRITE ((__force gfp_t)___GFP_WRITE)#define __GFP_HARDWALL ((__force gfp_t)___GFP_HARDWALL)#define __GFP_THISNODE ((__force gfp_t)___GFP_THISNODE)#define __GFP_ACCOUNT ((__force gfp_t)___GFP_ACCOUNT) +#define __GFP_NO_OBJ_EXT ((__force gfp_t)___GFP_NO_OBJ_EXT)/** * DOC: Watermark modifiers
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 7/35 mm/slab: introduce SLAB_NO_OBJ_EXT to avoid obj_ext creation
In reply to Suren BaghdasaryanSlab extension objects can't be allocated before slab infrastructure is initialized. Some caches, like kmem_cache and kmem_cache_node, are created before slab infrastructure is initialized. Objects from these caches can't have extension objects. Introduce SLAB_NO_OBJ_EXT slab flag to mark these caches and avoid creating extensions for objects allocated from these slabs.
--- include/linux/slab.h | 7 +++++++ mm/slub.c | 5 +++-- 2 files changed, 10 insertions(+), 2 deletions(-)
include/linux/slab.h +7 -0
@@ -164,6 +164,13 @@#endif #define SLAB_TEMPORARY SLAB_RECLAIM_ACCOUNT/* Objects are short-lived */ +#ifdef CONFIG_SLAB_OBJ_EXT +/* Slab created using create_boot_cache */ +#define SLAB_NO_OBJ_EXT ((slab_flags_t __force)0x20000000U) +#else +#define SLAB_NO_OBJ_EXT 0 +#endif +/* * ZERO_SIZE_PTR will be returned for zero sized kmalloc requests. *
mm/slub.c +3 -2
@@ -5650,7 +5650,8 @@ void __init kmem_cache_init(void)node_set (node, slab_nodes);create_boot_cache (kmem_cache_node,"kmem_cache_node" , -sizeof (struct kmem_cache_node ),SLAB_HWCACHE_ALIGN ,0 ,0 ); +sizeof (struct kmem_cache_node ), +SLAB_HWCACHE_ALIGN |SLAB_NO_OBJ_EXT ,0 ,0 );hotplug_memory_notifier (slab_memory_callback,SLAB_CALLBACK_PRI );
@@ -5660,7 +5661,7 @@ void __init kmem_cache_init(void)create_boot_cache (kmem_cache,"kmem_cache" , offsetof(struct kmem_cache , node) + nr_node_ids *sizeof (struct kmem_cache_node *), -SLAB_HWCACHE_ALIGN ,0 ,0 ); +SLAB_HWCACHE_ALIGN |SLAB_NO_OBJ_EXT ,0 ,0 ); kmem_cache =bootstrap (&boot_kmem_cache); kmem_cache_node =bootstrap (&boot_kmem_cache_node);
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 8/35 mm: prevent slabobj_ext allocations for slabobj_ext and kmem_cache objects
In reply to Suren BaghdasaryanUse __GFP_NO_OBJ_EXT to prevent recursions when allocating slabobj_ext objects. Also prevent slabobj_ext allocations for kmem_cache objects.
--- mm/slab.h | 6 ++++++ mm/slab_common.c | 2 ++ 2 files changed, 8 insertions(+)
mm/slab.h +6 -0
@@ -589,6 +589,12 @@ prepare_slab_obj_exts_hook(struct kmem_cache *s, gfp_t flags, void *p)if (!need_slab_obj_ext ())return NULL ; +if (s->flags &SLAB_NO_OBJ_EXT ) +return NULL ; + +if (flags & __GFP_NO_OBJ_EXT) +return NULL ; + slab =virt_to_slab (p);if (!slab_obj_exts (slab) && WARN(alloc_slab_obj_exts(slab, s, flags, false),
mm/slab_common.c +2 -0
@@ -218,6 +218,8 @@ int alloc_slab_obj_exts(struct slab *slab, struct kmem_cache *s,void *vec; gfp &= ~OBJCGS_CLEAR_MASK ; +/* Prevent recursive extension vector allocation */ + gfp |= __GFP_NO_OBJ_EXT; vec =kcalloc_node (objects,sizeof (struct slabobj_ext ), gfp,slab_nid (slab));if (!vec)
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 9/35 slab: objext: introduce objext_flags as extension to page_memcg_data_flags
In reply to Suren BaghdasaryanIntroduce objext_flags to store additional objext flags unrelated to memcg.
--- include/linux/memcontrol.h | 29 ++++++++++++++++++++++------- mm/slab.h | 4 +--- 2 files changed, 23 insertions(+), 10 deletions(-)
include/linux/memcontrol.h +22 -7
@@ -356,7 +356,22 @@ enum page_memcg_data_flags { __NR_MEMCG_DATA_FLAGS = (1UL << 2), }; -#define MEMCG_DATA_FLAGS_MASK (__NR_MEMCG_DATA_FLAGS - 1) +#define __FIRST_OBJEXT_FLAG __NR_MEMCG_DATA_FLAGS + +#else /* CONFIG_MEMCG */ + +#define __FIRST_OBJEXT_FLAG (1UL << 0) + +#endif /* CONFIG_MEMCG */ + +enum objext_flags { +/* the next bit after the last actual flag */ + __NR_OBJEXTS_FLAGS = __FIRST_OBJEXT_FLAG, +}; + +#define OBJEXTS_FLAGS_MASK (__NR_OBJEXTS_FLAGS - 1) + +#ifdef CONFIG_MEMCG static inline bool folio_memcg_kmem (struct folio *folio);
@@ -390,7 +405,7 @@ static inline struct mem_cgroup *__folio_memcg(struct folio *folio)VM_BUG_ON_FOLIO (memcg_data &MEMCG_DATA_OBJEXTS , folio);VM_BUG_ON_FOLIO (memcg_data &MEMCG_DATA_KMEM , folio); -return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK ); +return (struct mem_cgroup *)(memcg_data & ~OBJEXTS_FLAGS_MASK ); }/*
@@ -411,7 +426,7 @@ static inline struct obj_cgroup *__folio_objcg(struct folio *folio)VM_BUG_ON_FOLIO (memcg_data &MEMCG_DATA_OBJEXTS , folio);VM_BUG_ON_FOLIO (!(memcg_data &MEMCG_DATA_KMEM ), folio); -return (struct obj_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK ); +return (struct obj_cgroup *)(memcg_data & ~OBJEXTS_FLAGS_MASK ); }/*
@@ -468,11 +483,11 @@ static inline struct mem_cgroup *folio_memcg_rcu(struct folio *folio)if (memcg_data &MEMCG_DATA_KMEM ) {struct obj_cgroup *objcg; - objcg = (void *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK ); + objcg = (void *)(memcg_data & ~OBJEXTS_FLAGS_MASK );return obj_cgroup_memcg (objcg); } -return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK ); +return (struct mem_cgroup *)(memcg_data & ~OBJEXTS_FLAGS_MASK ); }/*
@@ -511,11 +526,11 @@ static inline struct mem_cgroup *folio_memcg_check(struct folio *folio)if (memcg_data &MEMCG_DATA_KMEM ) {struct obj_cgroup *objcg; - objcg = (void *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK ); + objcg = (void *)(memcg_data & ~OBJEXTS_FLAGS_MASK );return obj_cgroup_memcg (objcg); } -return (struct mem_cgroup *)(memcg_data & ~MEMCG_DATA_FLAGS_MASK ); +return (struct mem_cgroup *)(memcg_data & ~OBJEXTS_FLAGS_MASK ); }static inline struct mem_cgroup *page_memcg_check (struct page *page)
mm/slab.h +1 -3
@@ -560,10 +560,8 @@ static inline struct slabobj_ext *slab_obj_exts(struct slab *slab) slab_page(slab));VM_BUG_ON_PAGE (obj_exts &MEMCG_DATA_KMEM ,slab_page (slab)); -return (struct slabobj_ext *)(obj_exts & ~MEMCG_DATA_FLAGS_MASK ); -#else -return (struct slabobj_ext *)obj_exts;#endif +return (struct slabobj_ext *)(obj_exts & ~OBJEXTS_FLAGS_MASK ); } int alloc_slab_obj_exts(struct slab *slab, struct kmem_cache *s,
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 10/35 lib: code tagging framework
In reply to Suren BaghdasaryanAdd basic infrastructure to support code tagging which stores tag common information consisting of the module name, function, file name and line number. Provide functions to register a new code tag type and navigate between code tags.
--- include/linux/codetag.h | 71 ++++++++++++++ lib/Kconfig.debug | 4 + lib/Makefile | 1 + lib/codetag.c | 199 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 275 insertions(+) create mode 100644 include/linux/codetag.h create mode 100644 lib/codetag.c
include/linux/codetag.h new file +71 -0
@@ -0,0 +1,71 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* code tagging framework +*/ +#ifndef _LINUX_CODETAG_H +#define _LINUX_CODETAG_H + +#include <linux/types.h> + +struct codetag_iterator ; +struct codetag_type ; +struct seq_buf ; +struct module ; + +/* +* 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 +* an array of these. +*/ +struct codetag { +unsigned int flags;/* used in later patches */ +unsigned int lineno; +const char *modname; +const char *function; +const char *filename; +}__aligned(8) ; + +union codetag_ref { +struct codetag *ct; +}; + +struct codetag_range { +struct codetag *start; +struct codetag *stop; +}; + +struct codetag_module { +struct module *mod; +struct codetag_range range; +}; + +struct codetag_type_desc { +const char *section; +size_t tag_size; +}; + +struct codetag_iterator { +struct codetag_type *cttype; +struct codetag_module *cmod; +unsigned long mod_id; +struct codetag *ct; +}; + +#define CODE_TAG_INIT { \ + .modname = KBUILD_MODNAME, \ + .function = __func__, \ + .filename = __FILE__, \ + .lineno = __LINE__, \ + .flags = 0, \ +} + +void codetag_lock_module_list (struct codetag_type *cttype,bool lock); +struct codetag_iterator codetag_get_ct_iter (struct codetag_type *cttype); +struct codetag *codetag_next_ct (struct codetag_iterator *iter); + +void codetag_to_text (struct seq_buf *out,struct codetag *ct); + +struct codetag_type * +codetag_register_type (const struct codetag_type_desc *desc); + +#endif /* _LINUX_CODETAG_H */
lib/Kconfig.debug +4 -0
@@ -968,6 +968,10 @@ config DEBUG_STACKOVERFLOW If in doubt, say "N". +config CODE_TAGGING + bool + select KALLSYMS + source "lib/Kconfig.kasan" source "lib/Kconfig.kfence" source "lib/Kconfig.kmsan"
lib/Makefile +1 -0
@@ -235,6 +235,7 @@ obj-$(CONFIG_OF_RECONFIG_NOTIFIER_ERROR_INJECT) += \ of-reconfig-notifier-error-inject.o obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o +obj-$(CONFIG_CODE_TAGGING) += codetag.o lib-$(CONFIG_GENERIC_BUG) += bug.o obj-$(CONFIG_HAVE_ARCH_TRACEHOOK) += syscall.o
lib/codetag.c new file +199 -0
199 lines not on this page · raw message
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 11/35 lib: code tagging module support
In reply to Suren BaghdasaryanAdd support for code tagging from dynamically loaded modules.
--- include/linux/codetag.h | 12 +++++++++ kernel/module/main.c | 4 +++ lib/codetag.c | 58 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 72 insertions(+), 2 deletions(-)
include/linux/codetag.h +12 -0
@@ -42,6 +42,10 @@ struct codetag_module {struct codetag_type_desc {const char *section;size_t tag_size; +void (*module_load)(struct codetag_type *cttype, +struct codetag_module *cmod); +void (*module_unload)(struct codetag_type *cttype, +struct codetag_module *cmod); };struct codetag_iterator {
@@ -68,4 +72,12 @@ void codetag_to_text(struct seq_buf *out, struct codetag *ct);struct codetag_type *codetag_register_type (const struct codetag_type_desc *desc); +#ifdef CONFIG_CODE_TAGGING +void codetag_load_module (struct module *mod); +void codetag_unload_module (struct module *mod); +#else +static inline void codetag_load_module (struct module *mod) {} +static inline void codetag_unload_module (struct module *mod) {} +#endif +#endif /* _LINUX_CODETAG_H */
kernel/module/main.c +4 -0
@@ -56,6 +56,7 @@#include <linux/dynamic_debug.h> #include <linux/audit.h> #include <linux/cfi.h> +#include <linux/codetag.h> #include <linux/debugfs.h> #include <uapi/linux/module.h> #include "internal.h"
@@ -1242,6 +1243,7 @@ static void free_module(struct module *mod) {trace_module_free (mod); +codetag_unload_module (mod);mod_sysfs_teardown (mod);/*
@@ -2978,6 +2980,8 @@ static int load_module(struct load_info *info, const char __user *uargs,/* Get rid of temporary copy. */ free_copy (info, flags); +codetag_load_module (mod); +/* Done! */ trace_module_load (mod);
lib/codetag.c +56 -2
82 lines not on this page · raw message
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 12/35 lib: prevent module unloading if memory is not freed
In reply to Suren BaghdasaryanSkip freeing module's data section if there are non-zero allocation tags because otherwise, once these allocations are freed, the access to their code tag would cause UAF.
--- include/linux/codetag.h | 6 +++--- kernel/module/main.c | 23 +++++++++++++++-------- lib/codetag.c | 11 ++++++++--- 3 files changed, 26 insertions(+), 14 deletions(-)
include/linux/codetag.h +3 -3
@@ -44,7 +44,7 @@ struct codetag_type_desc {size_t tag_size;void (*module_load)(struct codetag_type *cttype,struct codetag_module *cmod); -void (*module_unload)(struct codetag_type *cttype, +bool (*module_unload)(struct codetag_type *cttype,struct codetag_module *cmod); };
@@ -74,10 +74,10 @@ codetag_register_type(const struct codetag_type_desc *desc);#ifdef CONFIG_CODE_TAGGING void codetag_load_module (struct module *mod); -void codetag_unload_module (struct module *mod); +bool codetag_unload_module (struct module *mod);#else static inline void codetag_load_module (struct module *mod) {} -static inline void codetag_unload_module (struct module *mod) {} +static inline bool codetag_unload_module (struct module *mod) {return true; }#endif #endif /* _LINUX_CODETAG_H */
kernel/module/main.c +15 -8
@@ -1211,15 +1211,19 @@ static void *module_memory_alloc(unsigned int size, enum mod_mem_type type)return module_alloc (size); } -static void module_memory_free (void *ptr,enum mod_mem_type type) +static void module_memory_free (void *ptr,enum mod_mem_type type, +bool unload_codetags) { +if (!unload_codetags &&mod_mem_type_is_core_data (type)) +return ; +if (mod_mem_use_vmalloc (type))vfree (ptr);else module_memfree (ptr); } -static void free_mod_mem (struct module *mod) +static void free_mod_mem (struct module *mod,bool unload_codetags) {for_each_mod_mem_type (type) {struct module_memory *mod_mem = &mod->mem[type];
@@ -1230,20 +1234,23 @@ static void free_mod_mem(struct module *mod)/* 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_mem->base, type); +module_memory_free (mod_mem->base, type, + unload_codetags); }/* 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->mem[MOD_DATA ].base,MOD_DATA ); +module_memory_free (mod->mem[MOD_DATA ].base,MOD_DATA , unload_codetags); }/* Free a module, remove from lists, etc. */ static void free_module (struct module *mod) { +bool unload_codetags; +trace_module_free (mod); -codetag_unload_module (mod); + unload_codetags =codetag_unload_module (mod);mod_sysfs_teardown (mod);/*
@@ -1285,7 +1292,7 @@ static void free_module(struct module *mod)kfree (mod->args);percpu_modfree (mod); -free_mod_mem (mod); +free_mod_mem (mod, unload_codetags); }void *__symbol_get (const char *symbol)
@@ -2298,7 +2305,7 @@ static int move_module(struct module *mod, struct load_info *info)return 0 ; out_enomem:for (t--; t >=0 ; t--) -module_memory_free (mod->mem[t].base, t); +module_memory_free (mod->mem[t].base, t, true);return ret; }
@@ -2428,7 +2435,7 @@ static void module_deallocate(struct module *mod, struct load_info *info)percpu_modfree (mod);module_arch_freeing_init (mod); -free_mod_mem (mod); +free_mod_mem (mod, true); } int __weak module_finalize(const Elf_Ehdr *hdr,
lib/codetag.c +8 -3
@@ -5,6 +5,7 @@#include <linux/module.h> #include <linux/seq_buf.h> #include <linux/slab.h> +#include <linux/vmalloc.h> struct codetag_type {struct list_head link;
@@ -219,12 +220,13 @@ void codetag_load_module(struct module *mod)mutex_unlock (&codetag_lock); } -void codetag_unload_module (struct module *mod) +bool codetag_unload_module (struct module *mod) {struct codetag_type *cttype; +bool unload_ok = true;if (!mod) -return ; +return true;mutex_lock (&codetag_lock);list_for_each_entry (cttype, &codetag_types, link) {
@@ -241,7 +243,8 @@ void codetag_unload_module(struct module *mod) }if (found) {if (cttype->desc.module_unload) - cttype->desc.module_unload (cttype, cmod); +if (!cttype->desc.module_unload (cttype, cmod)) + unload_ok = false; cttype->count -=range_size (cttype, &cmod->range);idr_remove (&cttype->mod_idr, mod_id);
@@ -250,4 +253,6 @@ void codetag_unload_module(struct module *mod)up_write (&cttype->mod_lock); }mutex_unlock (&codetag_lock); + +return unload_ok; }
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 13/35 lib: add allocation tagging support for memory allocation profiling
In reply to Suren BaghdasaryanIntroduce CONFIG_MEM_ALLOC_PROFILING which provides definitions to easily instrument memory allocators. It registers an "alloc_tags" codetag type with /proc/allocinfo interface to output allocation tag information when the feature is enabled.CONFIG_MEM_ALLOC_PROFILING_DEBUG is provided for debugging the memory allocation profiling instrumentation.
Memory allocation profiling can be enabled or disabled at runtime using /proc/sys/vm/mem_profiling sysctl when CONFIG_MEM_ALLOC_PROFILING_DEBUG=n. CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT enables memory allocation profiling by default.
--- Documentation/admin-guide/sysctl/vm.rst | 16 +++ Documentation/filesystems/proc.rst | 28 +++++ include/asm-generic/codetag.lds.h | 14 +++ include/asm-generic/vmlinux.lds.h | 3 + include/linux/alloc_tag.h | 133 ++++++++++++++++++++ include/linux/sched.h | 24 ++++ lib/Kconfig.debug | 25 ++++ lib/Makefile | 2 + lib/alloc_tag.c | 158 ++++++++++++++++++++++++ scripts/module.lds.S | 7 ++ 10 files changed, 410 insertions(+) create mode 100644 include/asm-generic/codetag.lds.h create mode 100644 include/linux/alloc_tag.h create mode 100644 lib/alloc_tag.c
Documentation/admin-guide/sysctl/vm.rst +16 -0
@@ -43,6 +43,7 @@ Currently, these files are in /proc/sys/vm: - legacy_va_layout - lowmem_reserve_ratio - max_map_count +- mem_profiling (only if CONFIG_MEM_ALLOC_PROFILING=y) - memory_failure_early_kill - memory_failure_recovery - min_free_kbytes
@@ -425,6 +426,21 @@ e.g., up to one or two maps per allocation. The default value is 65530. +mem_profiling +============== + +Enable memory profiling (when CONFIG_MEM_ALLOC_PROFILING=y) + +1: Enable memory profiling. + +0: Disabld memory profiling. + +Enabling memory profiling introduces a small performance overhead for all +memory allocations. + +The default value depends on CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT. + + memory_failure_early_kill: ==========================
Documentation/filesystems/proc.rst +28 -0
@@ -688,6 +688,7 @@ files are there, and which are missing. ============ =============================================================== File Content ============ =============================================================== + allocinfo Memory allocations profiling information apm Advanced power management info bootconfig Kernel command line obtained from boot config, and, if there were kernel parameters from the
@@ -953,6 +954,33 @@ also be allocatable although a lot of filesystem metadata may have to be reclaimed to achieve this. +allocinfo +~~~~~~~ + +Provides information about memory allocations at all locations in the code +base. Each allocation in the code is identified by its source file, line +number, module and the function calling the allocation. The number of bytes +allocated at each location is reported. + +Example output. + +:: + + > cat /proc/allocinfo + + 153MiB mm/slub.c:1826 module:slub func:alloc_slab_page + 6.08MiB mm/slab_common.c:950 module:slab_common func:_kmalloc_order + 5.09MiB mm/memcontrol.c:2814 module:memcontrol func:alloc_slab_obj_exts + 4.54MiB mm/page_alloc.c:5777 module:page_alloc func:alloc_pages_exact + 1.32MiB include/asm-generic/pgalloc.h:63 module:pgtable func:__pte_alloc_one + 1.16MiB fs/xfs/xfs_log_priv.h:700 module:xfs func:xlog_kvmalloc + 1.00MiB mm/swap_cgroup.c:48 module:swap_cgroup func:swap_cgroup_prepare + 734KiB fs/xfs/kmem.c:20 module:xfs func:kmem_alloc + 640KiB kernel/rcu/tree.c:3184 module:tree func:fill_page_cache_func + 640KiB drivers/char/virtio_console.c:452 module:virtio_console func:alloc_buf + ... + + meminfo ~~~~~~~
include/asm-generic/codetag.lds.h new file +14 -0
@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __ASM_GENERIC_CODETAG_LDS_H +#define __ASM_GENERIC_CODETAG_LDS_H + +#define SECTION_WITH_BOUNDARIES (_name) \ + . = ALIGN(8); \ + __start_##_name = .; \ + KEEP(*(_name)) \ + __stop_##_name = .; + +#define CODETAG_SECTIONS () \ + SECTION_WITH_BOUNDARIES(alloc_tags) + +#endif /* __ASM_GENERIC_CODETAG_LDS_H */
include/asm-generic/vmlinux.lds.h +3 -0
@@ -50,6 +50,8 @@* [__nosave_begin, __nosave_end] for the nosave data */ +#include <asm-generic/codetag.lds.h> +#ifndef LOAD_OFFSET #define LOAD_OFFSET 0#endif
@@ -366,6 +368,7 @@ . = ALIGN(8); \ BOUNDED_SECTION_BY(__dyndbg_classes, ___dyndbg_classes) \ BOUNDED_SECTION_BY(__dyndbg, ___dyndbg) \ + CODETAG_SECTIONS() \ LIKELY_PROFILE() \ BRANCH_PROFILE() \TRACE_PRINTKS () \
include/linux/alloc_tag.h new file +133 -0
133 lines not on this page · raw message
include/linux/sched.h +24 -0
@@ -770,6 +770,10 @@ struct task_struct {unsigned int flags;unsigned int ptrace; +#ifdef CONFIG_MEM_ALLOC_PROFILING +struct alloc_tag *alloc_tag; +#endif +#ifdef CONFIG_SMP int on_cpu;struct __call_single_node wake_entry;
@@ -810,6 +814,7 @@ struct task_struct {struct task_group *sched_task_group;#endif +#ifdef CONFIG_UCLAMP_TASK /* * Clamp values requested for a scheduling entity.
@@ -2183,4 +2188,23 @@ static inline int sched_core_idle_cpu(int cpu) { return idle_cpu(cpu); }extern void sched_set_stop_task (int cpu,struct task_struct *stop); +#ifdef CONFIG_MEM_ALLOC_PROFILING +static inline struct alloc_tag *alloc_tag_save (struct alloc_tag *tag) +{ +swap (current->alloc_tag, tag); +return tag; +} + +static inline void alloc_tag_restore (struct alloc_tag *tag,struct alloc_tag *old) +{ +#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG +WARN (current->alloc_tag != tag,"current->alloc_tag was changed:\n" ); +#endif + current->alloc_tag = old; +} +#else +static inline struct alloc_tag *alloc_tag_save (struct alloc_tag *tag) {return NULL ; } +#define alloc_tag_restore (_tag, _old) +#endif +#endif
lib/Kconfig.debug +25 -0
@@ -972,6 +972,31 @@ config CODE_TAGGING bool select KALLSYMS +config MEM_ALLOC_PROFILING + bool "Enable memory allocation profiling" + default n + depends on PROC_FS + depends on !DEBUG_FORCE_WEAK_PER_CPU + select CODE_TAGGING + help + Track allocation source code and record total allocation size + initiated at that code location. The mechanism can be used to track + memory leaks with a low performance and memory impact. + +config MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT + bool "Enable memory allocation profiling by default" + default y + depends on MEM_ALLOC_PROFILING + +config MEM_ALLOC_PROFILING_DEBUG + bool "Memory allocation profiler debugging" + default n + depends on MEM_ALLOC_PROFILING + select MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT + help + Adds warnings with helpful error messages for memory allocation + profiling. + source "lib/Kconfig.kasan" source "lib/Kconfig.kfence" source "lib/Kconfig.kmsan"
lib/Makefile +2 -0
@@ -236,6 +236,8 @@ obj-$(CONFIG_OF_RECONFIG_NOTIFIER_ERROR_INJECT) += \ obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o obj-$(CONFIG_CODE_TAGGING) += codetag.o +obj-$(CONFIG_MEM_ALLOC_PROFILING) += alloc_tag.o + lib-$(CONFIG_GENERIC_BUG) += bug.o obj-$(CONFIG_HAVE_ARCH_TRACEHOOK) += syscall.o
lib/alloc_tag.c new file +158 -0
158 lines not on this page · raw message
scripts/module.lds.S +7 -0
@@ -9,6 +9,8 @@ #define DISCARD_EH_FRAME *(.eh_frame) #endif +#include <asm-generic/codetag.lds.h> + SECTIONS { /DISCARD/ : { *(.discard)
@@ -47,12 +49,17 @@ SECTIONS { .data : { *(.data .data.[0-9a-zA-Z_]*) *(.data..L*) + CODETAG_SECTIONS() } .rodata : { *(.rodata .rodata.[0-9a-zA-Z_]*) *(.rodata..L*) } +#else + .data : { + CODETAG_SECTIONS() + } #endif }
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 14/35 lib: introduce support for page allocation tagging
In reply to Suren BaghdasaryanIntroduce helper functions to easily instrument page allocators by storing a pointer to the allocation tag associated with the code that allocated the page in a page_ext field.
--- include/linux/page_ext.h | 1 - include/linux/pgalloc_tag.h | 73 +++++++++++++++++++++++++++++++++++++ lib/Kconfig.debug | 1 + lib/alloc_tag.c | 17 +++++++++ mm/mm_init.c | 1 + mm/page_alloc.c | 4 ++ mm/page_ext.c | 4 ++ 7 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 include/linux/pgalloc_tag.h
include/linux/page_ext.h +0 -1
@@ -4,7 +4,6 @@#include <linux/types.h> #include <linux/stacktrace.h> -#include <linux/stackdepot.h> struct pglist_data ;
include/linux/pgalloc_tag.h new file +73 -0
73 lines not on this page · raw message
lib/Kconfig.debug +1 -0
@@ -978,6 +978,7 @@ config MEM_ALLOC_PROFILING depends on PROC_FS depends on !DEBUG_FORCE_WEAK_PER_CPU select CODE_TAGGING + select PAGE_EXTENSION help Track allocation source code and record total allocation size initiated at that code location. The mechanism can be used to track
lib/alloc_tag.c +17 -0
@@ -3,6 +3,7 @@#include <linux/fs.h> #include <linux/gfp.h> #include <linux/module.h> +#include <linux/page_ext.h> #include <linux/proc_fs.h> #include <linux/seq_buf.h> #include <linux/seq_file.h>
@@ -124,6 +125,22 @@ static bool alloc_tag_module_unload(struct codetag_type *cttype,return module_unused; } +static __init bool need_page_alloc_tagging (void ) +{ +return true; +} + +static __init void init_page_alloc_tagging (void ) +{ +} + +struct page_ext_operations page_alloc_tagging_ops = { + .size =sizeof (union codetag_ref ), + .need = need_page_alloc_tagging, + .init = init_page_alloc_tagging, +}; +EXPORT_SYMBOL (page_alloc_tagging_ops); +static struct ctl_table memory_allocation_profiling_sysctls[] = { { .procname ="mem_profiling" ,
mm/mm_init.c +1 -0
@@ -24,6 +24,7 @@#include <linux/page_ext.h> #include <linux/pti.h> #include <linux/pgtable.h> +#include <linux/stackdepot.h> #include <linux/swap.h> #include <linux/cma.h> #include <linux/crash_dump.h>
mm/page_alloc.c +4 -0
@@ -53,6 +53,7 @@#include <linux/khugepaged.h> #include <linux/delayacct.h> #include <linux/cacheinfo.h> +#include <linux/pgalloc_tag.h> #include <asm/div64.h> #include "internal.h" #include "shuffle.h"
@@ -1100,6 +1101,7 @@ static __always_inline bool free_pages_prepare(struct page *page,/* Do not let hwpoison pages hit pcplists/buddy */ reset_page_owner (page, order);page_table_check_free (page, order); +pgalloc_tag_sub (page, order);return false; }
@@ -1139,6 +1141,7 @@ static __always_inline bool free_pages_prepare(struct page *page, page->flags &= ~PAGE_FLAGS_CHECK_AT_PREP ;reset_page_owner (page, order);page_table_check_free (page, order); +pgalloc_tag_sub (page, order);if (!PageHighMem (page)) { debug_check_no_locks_freed(page_address(page),
@@ -1532,6 +1535,7 @@ inline void post_alloc_hook(struct page *page, unsigned int order,set_page_owner (page, order, gfp_flags);page_table_check_alloc (page, order); +pgalloc_tag_add (page, current, order); } static void prep_new_page(struct page *page, unsigned int order, gfp_t gfp_flags,
mm/page_ext.c +4 -0
@@ -10,6 +10,7 @@#include <linux/page_idle.h> #include <linux/page_table_check.h> #include <linux/rcupdate.h> +#include <linux/pgalloc_tag.h> /* * struct page extension
@@ -82,6 +83,9 @@ static struct page_ext_operations *page_ext_ops[] __initdata = {#if defined(CONFIG_PAGE_IDLE_FLAG ) && !defined(CONFIG_64BIT ) &page_idle_ops,#endif +#ifdef CONFIG_MEM_ALLOC_PROFILING + &page_alloc_tagging_ops, +#endif #ifdef CONFIG_PAGE_TABLE_CHECK &page_table_check_ops,#endif
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 15/35 mm: percpu: increase PERCPU_MODULE_RESERVE to accommodate allocation tags
In reply to Suren BaghdasaryanAs each allocation tag generates a per-cpu variable, more space is required to store them. Increase PERCPU_MODULE_RESERVE to provide enough area. A better long-term solution would be to allocate this memory dynamically.
--- include/linux/percpu.h | 4 ++++ 1 file changed, 4 insertions(+)
include/linux/percpu.h +4 -0
@@ -14,7 +14,11 @@/* enough to cover all DEFINE_PER_CPUs in modules */ #ifdef CONFIG_MODULES +#ifdef CONFIG_MEM_ALLOC_PROFILING +#define PERCPU_MODULE_RESERVE (8 << 12) +#else #define PERCPU_MODULE_RESERVE (8 << 10) +#endif #else #define PERCPU_MODULE_RESERVE 0#endif
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 16/35 change alloc_pages name in dma_map_ops to avoid name conflicts
In reply to Suren BaghdasaryanAfter redefining alloc_pages, all uses of that name are being replaced. Change the conflicting names to prevent preprocessor from replacing them when it's not intended.
--- arch/alpha/kernel/pci_iommu.c | 2 +- arch/mips/jazz/jazzdma.c | 2 +- arch/powerpc/kernel/dma-iommu.c | 2 +- arch/powerpc/platforms/ps3/system-bus.c | 4 ++-- arch/powerpc/platforms/pseries/vio.c | 2 +- arch/x86/kernel/amd_gart_64.c | 2 +- drivers/iommu/dma-iommu.c | 2 +- drivers/parisc/ccio-dma.c | 2 +- drivers/parisc/sba_iommu.c | 2 +- drivers/xen/grant-dma-ops.c | 2 +- drivers/xen/swiotlb-xen.c | 2 +- include/linux/dma-map-ops.h | 2 +- kernel/dma/mapping.c | 4 ++-- 13 files changed, 15 insertions(+), 15 deletions(-)
arch/alpha/kernel/pci_iommu.c +1 -1
@@ -929,7 +929,7 @@ const struct dma_map_ops alpha_pci_ops = { .dma_supported = alpha_pci_supported, .mmap = dma_common_mmap, .get_sgtable = dma_common_get_sgtable, - .alloc_pages = dma_common_alloc_pages, + .alloc_pages_op = dma_common_alloc_pages, .free_pages = dma_common_free_pages, };EXPORT_SYMBOL (alpha_pci_ops);
arch/mips/jazz/jazzdma.c +1 -1
@@ -617,7 +617,7 @@ const struct dma_map_ops jazz_dma_ops = { .sync_sg_for_device = jazz_dma_sync_sg_for_device, .mmap = dma_common_mmap, .get_sgtable = dma_common_get_sgtable, - .alloc_pages = dma_common_alloc_pages, + .alloc_pages_op = dma_common_alloc_pages, .free_pages = dma_common_free_pages, };EXPORT_SYMBOL (jazz_dma_ops);
arch/powerpc/kernel/dma-iommu.c +1 -1
@@ -216,6 +216,6 @@ const struct dma_map_ops dma_iommu_ops = { .get_required_mask = dma_iommu_get_required_mask, .mmap = dma_common_mmap, .get_sgtable = dma_common_get_sgtable, - .alloc_pages = dma_common_alloc_pages, + .alloc_pages_op = dma_common_alloc_pages, .free_pages = dma_common_free_pages, };
arch/powerpc/platforms/ps3/system-bus.c +2 -2
@@ -695,7 +695,7 @@ static const struct dma_map_ops ps3_sb_dma_ops = { .unmap_page = ps3_unmap_page, .mmap = dma_common_mmap, .get_sgtable = dma_common_get_sgtable, - .alloc_pages = dma_common_alloc_pages, + .alloc_pages_op = dma_common_alloc_pages, .free_pages = dma_common_free_pages, };
@@ -709,7 +709,7 @@ static const struct dma_map_ops ps3_ioc0_dma_ops = { .unmap_page = ps3_unmap_page, .mmap = dma_common_mmap, .get_sgtable = dma_common_get_sgtable, - .alloc_pages = dma_common_alloc_pages, + .alloc_pages_op = dma_common_alloc_pages, .free_pages = dma_common_free_pages, };
arch/powerpc/platforms/pseries/vio.c +1 -1
@@ -611,7 +611,7 @@ static const struct dma_map_ops vio_dma_mapping_ops = { .get_required_mask = dma_iommu_get_required_mask, .mmap = dma_common_mmap, .get_sgtable = dma_common_get_sgtable, - .alloc_pages = dma_common_alloc_pages, + .alloc_pages_op = dma_common_alloc_pages, .free_pages = dma_common_free_pages, };
arch/x86/kernel/amd_gart_64.c +1 -1
@@ -676,7 +676,7 @@ static const struct dma_map_ops gart_dma_ops = { .get_sgtable = dma_common_get_sgtable, .dma_supported = dma_direct_supported, .get_required_mask = dma_direct_get_required_mask, - .alloc_pages = dma_direct_alloc_pages, + .alloc_pages_op = dma_direct_alloc_pages, .free_pages = dma_direct_free_pages, };
drivers/iommu/dma-iommu.c +1 -1
@@ -1710,7 +1710,7 @@ static const struct dma_map_ops iommu_dma_ops = { .flags = DMA_F_PCI_P2PDMA_SUPPORTED, .alloc = iommu_dma_alloc, .free = iommu_dma_free, - .alloc_pages = dma_common_alloc_pages, + .alloc_pages_op = dma_common_alloc_pages, .free_pages = dma_common_free_pages, .alloc_noncontiguous = iommu_dma_alloc_noncontiguous, .free_noncontiguous = iommu_dma_free_noncontiguous,
drivers/parisc/ccio-dma.c +1 -1
@@ -1022,7 +1022,7 @@ static const struct dma_map_ops ccio_ops = { .map_sg = ccio_map_sg, .unmap_sg = ccio_unmap_sg, .get_sgtable = dma_common_get_sgtable, - .alloc_pages = dma_common_alloc_pages, + .alloc_pages_op = dma_common_alloc_pages, .free_pages = dma_common_free_pages, };
drivers/parisc/sba_iommu.c +1 -1
@@ -1090,7 +1090,7 @@ static const struct dma_map_ops sba_ops = { .map_sg = sba_map_sg, .unmap_sg = sba_unmap_sg, .get_sgtable = dma_common_get_sgtable, - .alloc_pages = dma_common_alloc_pages, + .alloc_pages_op = dma_common_alloc_pages, .free_pages = dma_common_free_pages, };
drivers/xen/grant-dma-ops.c +1 -1
@@ -282,7 +282,7 @@ static int xen_grant_dma_supported(struct device *dev, u64 mask)static const struct dma_map_ops xen_grant_dma_ops = { .alloc = xen_grant_dma_alloc, .free = xen_grant_dma_free, - .alloc_pages = xen_grant_dma_alloc_pages, + .alloc_pages_op = xen_grant_dma_alloc_pages, .free_pages = xen_grant_dma_free_pages, .mmap = dma_common_mmap, .get_sgtable = dma_common_get_sgtable,
drivers/xen/swiotlb-xen.c +1 -1
@@ -403,7 +403,7 @@ const struct dma_map_ops xen_swiotlb_dma_ops = { .dma_supported = xen_swiotlb_dma_supported, .mmap = dma_common_mmap, .get_sgtable = dma_common_get_sgtable, - .alloc_pages = dma_common_alloc_pages, + .alloc_pages_op = dma_common_alloc_pages, .free_pages = dma_common_free_pages, .max_mapping_size = swiotlb_max_mapping_size, };
include/linux/dma-map-ops.h +1 -1
@@ -29,7 +29,7 @@ struct dma_map_ops { unsigned long attrs);void (*free)(struct device *dev,size_t size,void *vaddr,dma_addr_t dma_handle,unsigned long attrs); -struct page *(*alloc_pages)(struct device *dev,size_t size, +struct page *(*alloc_pages_op)(struct device *dev,size_t size,dma_addr_t *dma_handle,enum dma_data_direction dir,gfp_t gfp); void (*free_pages)(struct device *dev, size_t size, struct page *vaddr,
kernel/dma/mapping.c +2 -2
@@ -570,9 +570,9 @@ static struct page *__dma_alloc_pages(struct device *dev, size_t size, size =PAGE_ALIGN (size);if (dma_alloc_direct (dev, ops))return dma_direct_alloc_pages (dev, size, dma_handle, dir, gfp); -if (!ops->alloc_pages) +if (!ops->alloc_pages_op)return NULL ; -return ops->alloc_pages (dev, size, dma_handle, dir, gfp); +return ops->alloc_pages_op (dev, size, dma_handle, dir, gfp); } struct page *dma_alloc_pages(struct device *dev, size_t size,
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 17/35 mm: enable page allocation tagging
In reply to Suren BaghdasaryanRedefine page allocators to record allocation tags upon their invocation. Instrument post_alloc_hook and free_pages_prepare to modify current allocation tag.
--- include/linux/alloc_tag.h | 10 +++ include/linux/gfp.h | 126 ++++++++++++++++++++++++-------------- include/linux/pagemap.h | 9 ++- mm/compaction.c | 7 ++- mm/filemap.c | 6 +- mm/mempolicy.c | 52 ++++++++-------- mm/page_alloc.c | 60 +++++++++--------- 7 files changed, 160 insertions(+), 110 deletions(-)
include/linux/alloc_tag.h +10 -0
@@ -130,4 +130,14 @@ static inline void alloc_tag_add(union codetag_ref *ref, struct alloc_tag *tag,#endif +#define alloc_hooks (_do_alloc) \ +({ \ + typeof(_do_alloc) _res; \ + DEFINE_ALLOC_TAG(_alloc_tag, _old); \ + \ + _res = _do_alloc; \ + alloc_tag_restore(&_alloc_tag, _old); \ + _res; \ +}) +#endif /* _LINUX_ALLOC_TAG_H */
include/linux/gfp.h +79 -47
219 lines not on this page · raw message
include/linux/pagemap.h +6 -3
@@ -542,14 +542,17 @@ static inline void *detach_page_private(struct page *page)#endif #ifdef CONFIG_NUMA -struct folio *filemap_alloc_folio (gfp_t gfp,unsigned int order); +struct folio *filemap_alloc_folio_noprof (gfp_t gfp,unsigned int order);#else -static inline struct folio *filemap_alloc_folio (gfp_t gfp,unsigned int order) +static inline struct folio *filemap_alloc_folio_noprof (gfp_t gfp,unsigned int order) { -return folio_alloc (gfp, order); +return folio_alloc_noprof (gfp, order); }#endif +#define filemap_alloc_folio (...) \ + alloc_hooks(filemap_alloc_folio_noprof(__VA_ARGS__)) +static inline struct page *__page_cache_alloc (gfp_t gfp) {return &filemap_alloc_folio (gfp,0 )->page;
mm/compaction.c +6 -1
@@ -1781,7 +1781,7 @@ static void isolate_freepages(struct compact_control *cc)* This is a migrate-callback that "allocates" freepages by taking pages * from the isolated freelists in the block we are migrating to. */ -static struct folio *compaction_alloc (struct folio *src,unsigned long data) +static struct folio *compaction_alloc_noprof (struct folio *src,unsigned long data) {struct compact_control *cc = (struct compact_control *)data;struct folio *dst;
@@ -1800,6 +1800,11 @@ static struct folio *compaction_alloc(struct folio *src, unsigned long data)return dst; } +static struct folio *compaction_alloc (struct folio *src,unsigned long data) +{ +return alloc_hooks (compaction_alloc_noprof (src, data)); +} +/* * This is a migrate-callback that "frees" freepages back to the isolated * freelist. All pages on the freelist are from the same zone, so there is no
mm/filemap.c +3 -3
@@ -957,7 +957,7 @@ int filemap_add_folio(struct address_space *mapping, struct folio *folio,EXPORT_SYMBOL_GPL (filemap_add_folio);#ifdef CONFIG_NUMA -struct folio *filemap_alloc_folio (gfp_t gfp,unsigned int order) +struct folio *filemap_alloc_folio_noprof (gfp_t gfp,unsigned int order) {int n;struct folio *folio;
@@ -972,9 +972,9 @@ struct folio *filemap_alloc_folio(gfp_t gfp, unsigned int order) return folio; } -return folio_alloc (gfp, order); +return folio_alloc_noprof (gfp, order); } -EXPORT_SYMBOL (filemap_alloc_folio); +EXPORT_SYMBOL (filemap_alloc_folio_noprof);#endif /*
mm/mempolicy.c +26 -26
153 lines not on this page · raw message
mm/page_alloc.c +30 -30
180 lines not on this page · raw message
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 18/35 mm: create new codetag references during page splitting
In reply to Suren BaghdasaryanWhen a high-order page is split into smaller ones, each newly split page should get its codetag. The original codetag is reused for these pages but it's recorded as 0-byte allocation because original codetag already accounts for the original high-order allocated page.
--- include/linux/pgalloc_tag.h | 30 ++++++++++++++++++++++++++++++ mm/huge_memory.c | 2 ++ mm/page_alloc.c | 2 ++ 3 files changed, 34 insertions(+)
include/linux/pgalloc_tag.h +30 -0
@@ -62,11 +62,41 @@ static inline void pgalloc_tag_sub(struct page *page, unsigned int order) } } +static inline void pgalloc_tag_split (struct page *page,unsigned int nr) +{ +int i; +struct page_ext *page_ext; +union codetag_ref *ref; +struct alloc_tag *tag; + +if (!mem_alloc_profiling_enabled ()) +return ; + + page_ext =page_ext_get (page); +if (unlikely (!page_ext)) +return ; + + ref =codetag_ref_from_page_ext (page_ext); +if (!ref->ct) + goto out; + + tag =ct_to_alloc_tag (ref->ct); + page_ext =page_ext_next (page_ext); +for (i =1 ; i < nr; i++) { +/* New reference with 0 bytes accounted */ +alloc_tag_add (codetag_ref_from_page_ext (page_ext), tag,0 ); + page_ext =page_ext_next (page_ext); + } +out: +page_ext_put (page_ext); +} +#else /* CONFIG_MEM_ALLOC_PROFILING */ static inline void pgalloc_tag_add (struct page *page,struct task_struct *task,unsigned int order) {}static inline void pgalloc_tag_sub (struct page *page,unsigned int order) {} +static inline void pgalloc_tag_split (struct page *page,unsigned int nr) {}#endif /* CONFIG_MEM_ALLOC_PROFILING */
mm/huge_memory.c +2 -0
@@ -38,6 +38,7 @@#include <linux/sched/sysctl.h> #include <linux/memory-tiers.h> #include <linux/compat.h> +#include <linux/pgalloc_tag.h> #include <asm/tlb.h> #include <asm/pgalloc.h>
@@ -2899,6 +2900,7 @@ static void __split_huge_page(struct page *page, struct list_head *list,/* Caller disabled irqs, so they are still disabled here */ split_page_owner (head, nr); +pgalloc_tag_split (head, nr);/* See comment in __split_huge_page_tail() */ if (PageAnon (head)) {
mm/page_alloc.c +2 -0
@@ -2621,6 +2621,7 @@ void split_page(struct page *page, unsigned int order)for (i =1 ; i < (1 << order); i++)set_page_refcounted (page + i);split_page_owner (page,1 << order); +pgalloc_tag_split (page,1 << order);split_page_memcg (page,1 << order); }EXPORT_SYMBOL_GPL (split_page);
@@ -4806,6 +4807,7 @@ static void *make_alloc_exact(unsigned long addr, unsigned int order,struct page *last = page + nr;split_page_owner (page,1 << order); +pgalloc_tag_split (page,1 << order);split_page_memcg (page,1 << order);while (page < --last)set_page_refcounted (last);
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 19/35 mm/page_ext: enable early_page_ext when CONFIG_MEM_ALLOC_PROFILING_DEBUG=y
In reply to Suren BaghdasaryanFor all page allocations to be tagged, page_ext has to be initialized before the first page allocation. Early tasks allocate their stacks using page allocator before alloc_node_page_ext() initializes page_ext area, unless early_page_ext is enabled. Therefore these allocations will generate a warning when CONFIG_MEM_ALLOC_PROFILING_DEBUG is enabled. Enable early_page_ext whenever CONFIG_MEM_ALLOC_PROFILING_DEBUG=y to ensure page_ext initialization prior to any page allocation. This will have all the negative effects associated with early_page_ext, such as possible longer boot time, therefore we enable it only when debugging with CONFIG_MEM_ALLOC_PROFILING_DEBUG enabled and not universally for CONFIG_MEM_ALLOC_PROFILING.
--- mm/page_ext.c | 9 +++++++++ 1 file changed, 9 insertions(+)
mm/page_ext.c +9 -0
@@ -95,7 +95,16 @@ unsigned long page_ext_size;static unsigned long total_usage; +#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG +/* +* To ensure correct allocation tagging for pages, page_ext should be available +* before the first page allocation. Otherwise early task stacks will be +* allocated before page_ext initialization and missing tags will be flagged. +*/ +bool early_page_ext__meminitdata = true; +#else bool early_page_ext__meminitdata ; +#endif static int __init setup_early_page_ext (char *str) { early_page_ext = true;
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 20/35 lib: add codetag reference into slabobj_ext
In reply to Suren BaghdasaryanTo store code tag for every slab object, a codetag reference is embedded into slabobj_ext when CONFIG_MEM_ALLOC_PROFILING=y.
--- include/linux/memcontrol.h | 5 +++++ lib/Kconfig.debug | 1 + mm/slab.h | 4 ++++ 3 files changed, 10 insertions(+)
include/linux/memcontrol.h +5 -0
@@ -1653,7 +1653,12 @@ unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,* if MEMCG_DATA_OBJEXTS is set. */ struct slabobj_ext { +#ifdef CONFIG_MEMCG_KMEM struct obj_cgroup *objcg; +#endif +#ifdef CONFIG_MEM_ALLOC_PROFILING +union codetag_ref ref; +#endif }__aligned(8) ;static inline void __inc_lruvec_kmem_state (void *p,enum node_stat_item idx)
lib/Kconfig.debug +1 -0
@@ -979,6 +979,7 @@ config MEM_ALLOC_PROFILING depends on !DEBUG_FORCE_WEAK_PER_CPU select CODE_TAGGING select PAGE_EXTENSION + select SLAB_OBJ_EXT help Track allocation source code and record total allocation size initiated at that code location. The mechanism can be used to track
mm/slab.h +4 -0
@@ -569,6 +569,10 @@ int alloc_slab_obj_exts(struct slab *slab, struct kmem_cache *s,static inline bool need_slab_obj_ext (void ) { +#ifdef CONFIG_MEM_ALLOC_PROFILING +if (mem_alloc_profiling_enabled ()) +return true; +#endif /* * CONFIG_MEMCG_KMEM creates vector of obj_cgroup objects conditionally * inside memcg_slab_post_alloc_hook. No other users for now.
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 21/35 mm/slab: add allocation accounting into slab allocation and free paths
In reply to Suren BaghdasaryanAccount slab allocations using codetag reference embedded into slabobj_ext.
--- mm/slab.h | 26 ++++++++++++++++++++++++++ mm/slub.c | 5 +++++ 2 files changed, 31 insertions(+)
mm/slab.h +26 -0
@@ -629,6 +629,32 @@ prepare_slab_obj_exts_hook(struct kmem_cache *s, gfp_t flags, void *p)#endif /* CONFIG_SLAB_OBJ_EXT */ +#ifdef CONFIG_MEM_ALLOC_PROFILING + +static inline void alloc_tagging_slab_free_hook (struct kmem_cache *s,struct slab *slab, +void **p,int objects) +{ +struct slabobj_ext *obj_exts; +int i; + + obj_exts =slab_obj_exts (slab); +if (!obj_exts) +return ; + +for (i =0 ; i < objects; i++) { +unsigned int off =obj_to_index (s, slab, p[i]); + +alloc_tag_sub (&obj_exts[off].ref, s->size); + } +} + +#else + +static inline void alloc_tagging_slab_free_hook (struct kmem_cache *s,struct slab *slab, +void **p,int objects) {} + +#endif /* CONFIG_MEM_ALLOC_PROFILING */ +#ifdef CONFIG_MEMCG_KMEM void mod_objcg_state (struct obj_cgroup *objcg,struct pglist_data *pgdat,enum node_stat_item idx,int nr);
mm/slub.c +5 -0
@@ -3821,6 +3821,11 @@ void slab_post_alloc_hook(struct kmem_cache *s, struct obj_cgroup *objcg, s->flags, init_flags);kmsan_slab_alloc (s, p[i], init_flags); obj_exts =prepare_slab_obj_exts_hook (s, flags, p[i]); +#ifdef CONFIG_MEM_ALLOC_PROFILING +/* obj_exts can be allocated for other reasons */ +if (likely (obj_exts) &&mem_alloc_profiling_enabled ()) +alloc_tag_add (&obj_exts->ref, current->alloc_tag, s->size); +#endif }memcg_slab_post_alloc_hook (s, objcg, flags, size, p);
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 22/35 mm/slab: enable slab allocation tagging for kmalloc and friends
In reply to Suren BaghdasaryanRedefine kmalloc, krealloc, kzalloc, kcalloc, etc. to record allocations and deallocations done by these functions.
--- include/linux/fortify-string.h | 5 +- include/linux/slab.h | 177 ++++++++++++++++----------------- include/linux/string.h | 4 +- mm/slab_common.c | 6 +- mm/slub.c | 54 +++++----- mm/util.c | 20 ++-- 6 files changed, 134 insertions(+), 132 deletions(-)
include/linux/fortify-string.h +3 -2
@@ -697,9 +697,9 @@ __FORTIFY_INLINE void *memchr_inv(const void * const POS0 p, int c, size_t size)return __real_memchr_inv (p, c, size); } -extern void *__real_kmemdup (const void *src,size_t len,gfp_t gfp)__RENAME (kmemdup) +extern void *__real_kmemdup (const void *src,size_t len,gfp_t gfp)__RENAME (kmemdup_noprof)__realloc_size (2 ); -__FORTIFY_INLINE void *kmemdup(const void * const POS0 p, size_t size, gfp_t gfp) +__FORTIFY_INLINE void *kmemdup_noprof(const void * const POS0 p, size_t size, gfp_t gfp) {const size_t p_size =__struct_size (p);
@@ -709,6 +709,7 @@ __FORTIFY_INLINE void *kmemdup(const void * const POS0 p, size_t size, gfp_t gfpfortify_panic (__func__);return __real_kmemdup (p, size, gfp); } +#define kmemdup (...) alloc_hooks(kmemdup_noprof(__VA_ARGS__))/** * strcpy - Copy a string into another string buffer
include/linux/slab.h +87 -90
312 lines not on this page · raw message
include/linux/string.h +3 -1
@@ -214,7 +214,9 @@ extern void kfree_const(const void *x);extern char *kstrdup (const char *s,gfp_t gfp) __malloc;extern const char *kstrdup_const (const char *s,gfp_t gfp);extern char *kstrndup (const char *s,size_t len,gfp_t gfp); -extern void *kmemdup (const void *src,size_t len,gfp_t gfp)__realloc_size (2 ); +extern void *kmemdup_noprof (const void *src,size_t len,gfp_t gfp)__realloc_size (2 ); +#define kmemdup (...) alloc_hooks(kmemdup_noprof(__VA_ARGS__)) +extern void *kvmemdup (const void *src,size_t len,gfp_t gfp)__realloc_size (2 );extern char *kmemdup_nul (const char *s,size_t len,gfp_t gfp);
mm/slab_common.c +3 -3
@@ -1234,7 +1234,7 @@ __do_krealloc(const void *p, size_t new_size, gfp_t flags)return (void *)p; } - ret =kmalloc_track_caller (new_size, flags); + ret =kmalloc_node_track_caller_noprof (new_size, flags,NUMA_NO_NODE , _RET_IP_);if (ret && p) {/* Disable KASAN checks as the object's redzone is accessed. */ kasan_disable_current ();
@@ -1258,7 +1258,7 @@ __do_krealloc(const void *p, size_t new_size, gfp_t flags)* * Return: pointer to the allocated memory or %NULL in case of error */ -void *krealloc (const void *p,size_t new_size,gfp_t flags) +void *krealloc_noprof (const void *p,size_t new_size,gfp_t flags) {void *ret;
@@ -1273,7 +1273,7 @@ void *krealloc(const void *p, size_t new_size, gfp_t flags)return ret; } -EXPORT_SYMBOL (krealloc); +EXPORT_SYMBOL (krealloc_noprof);/** * kfree_sensitive - Clear sensitive information in memory before freeing
mm/slub.c +28 -26
167 lines not on this page · raw message
mm/util.c +10 -10
@@ -115,7 +115,7 @@ char *kstrndup(const char *s, size_t max, gfp_t gfp)EXPORT_SYMBOL (kstrndup);/** -* kmemdup - duplicate region of memory +* kmemdup_noprof - duplicate region of memory * * @src: memory region to duplicate * @len: memory region length
@@ -124,16 +124,16 @@ EXPORT_SYMBOL(kstrndup);* Return: newly allocated copy of @src or %NULL in case of error, * result is physically contiguous. Use kfree() to free. */ -void *kmemdup (const void *src,size_t len,gfp_t gfp) +void *kmemdup_noprof (const void *src,size_t len,gfp_t gfp) {void *p; - p =kmalloc_track_caller (len, gfp); + p =kmalloc_node_track_caller_noprof (len, gfp,NUMA_NO_NODE , _RET_IP_);if (p)memcpy (p, src, len);return p; } -EXPORT_SYMBOL (kmemdup); +EXPORT_SYMBOL (kmemdup_noprof);/** * kvmemdup - duplicate region of memory
@@ -577,7 +577,7 @@ unsigned long vm_mmap(struct file *file, unsigned long addr,EXPORT_SYMBOL (vm_mmap);/** -* kvmalloc_node - attempt to allocate physically contiguous memory, but upon +* kvmalloc_node_noprof - attempt to allocate physically contiguous memory, but upon * failure, fall back to non-contiguous (vmalloc) allocation. * @size: size of the request. * @flags: gfp mask for the allocation - must be compatible (superset) with GFP_KERNEL.
@@ -592,7 +592,7 @@ EXPORT_SYMBOL(vm_mmap);* * Return: pointer to the allocated memory of %NULL in case of failure */ -void *kvmalloc_node (size_t size,gfp_t flags,int node) +void *kvmalloc_node_noprof (size_t size,gfp_t flags,int node) {gfp_t kmalloc_flags = flags;void *ret;
@@ -614,7 +614,7 @@ void *kvmalloc_node(size_t size, gfp_t flags, int node) kmalloc_flags &= ~__GFP_NOFAIL; } - ret =kmalloc_node (size, kmalloc_flags, node); + ret =kmalloc_node_noprof (size, kmalloc_flags, node);/* * It doesn't really make sense to fallback to vmalloc for sub page
@@ -643,7 +643,7 @@ void *kvmalloc_node(size_t size, gfp_t flags, int node) flags,PAGE_KERNEL ,VM_ALLOW_HUGE_VMAP , node, __builtin_return_address(0)); } -EXPORT_SYMBOL (kvmalloc_node); +EXPORT_SYMBOL (kvmalloc_node_noprof);/** * kvfree() - Free memory.
@@ -682,7 +682,7 @@ void kvfree_sensitive(const void *addr, size_t len) }EXPORT_SYMBOL (kvfree_sensitive); -void *kvrealloc (const void *p,size_t oldsize,size_t newsize,gfp_t flags) +void *kvrealloc_noprof (const void *p,size_t oldsize,size_t newsize,gfp_t flags) {void *newp;
@@ -695,7 +695,7 @@ void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags)kvfree (p);return newp; } -EXPORT_SYMBOL (kvrealloc); +EXPORT_SYMBOL (kvrealloc_noprof);/** * __vmalloc_array - allocate memory for a virtually contiguous array.
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 23/35 mm/slub: Mark slab_free_freelist_hook() __always_inline
In reply to Suren BaghdasaryanFrom: Kent Overstreet <[email protected]>
It seems we need to be more forceful with the compiler on this one.
--- mm/slub.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/slub.c +1 -1
@@ -2124,7 +2124,7 @@ bool slab_free_hook(struct kmem_cache *s, void *x, bool init)return !kasan_slab_free (s, x, init); } -static inline bool slab_free_freelist_hook (struct kmem_cache *s, +static __always_inline bool slab_free_freelist_hook(struct kmem_cache *s,void **head,void **tail,int *cnt) {
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 24/35 mempool: Hook up to memory allocation profiling
In reply to Suren BaghdasaryanFrom: Kent Overstreet <[email protected]>
This adds hooks to mempools for correctly annotating mempool-backed allocations at the correct source line, so they show up correctly in /sys/kernel/debug/allocations.
Various inline functions are converted to wrappers so that we can invoke alloc_hooks() in fewer places.
--- include/linux/mempool.h | 73 ++++++++++++++++++++--------------------- mm/mempool.c | 36 ++++++++------------ 2 files changed, 49 insertions(+), 60 deletions(-)
include/linux/mempool.h +35 -38
@@ -5,6 +5,8 @@#ifndef _LINUX_MEMPOOL_H#define _LINUX_MEMPOOL_H +#include <linux/sched.h> +#include <linux/alloc_tag.h> #include <linux/wait.h> #include <linux/compiler.h>
@@ -39,18 +41,32 @@ void mempool_exit(mempool_t *pool);int mempool_init_node (mempool_t *pool,int min_nr,mempool_alloc_t *alloc_fn,mempool_free_t *free_fn,void *pool_data,gfp_t gfp_mask,int node_id); -int mempool_init (mempool_t *pool,int min_nr,mempool_alloc_t *alloc_fn, + +int mempool_init_noprof (mempool_t *pool,int min_nr,mempool_alloc_t *alloc_fn,mempool_free_t *free_fn,void *pool_data); +#define mempool_init (...) \ + alloc_hooks(mempool_init_noprof(__VA_ARGS__))extern mempool_t *mempool_create (int min_nr,mempool_alloc_t *alloc_fn,mempool_free_t *free_fn,void *pool_data); -extern mempool_t *mempool_create_node (int min_nr,mempool_alloc_t *alloc_fn, + +extern mempool_t *mempool_create_node_noprof (int min_nr,mempool_alloc_t *alloc_fn,mempool_free_t *free_fn,void *pool_data,gfp_t gfp_mask,int nid); +#define mempool_create_node (...) \ + alloc_hooks(mempool_create_node_noprof(__VA_ARGS__)) + +#define mempool_create (_min_nr, _alloc_fn, _free_fn, _pool_data) \ + mempool_create_node(_min_nr, _alloc_fn, _free_fn, _pool_data, \ + GFP_KERNEL, NUMA_NO_NODE)extern int mempool_resize (mempool_t *pool,int new_min_nr);extern void mempool_destroy (mempool_t *pool); -extern void *mempool_alloc (mempool_t *pool,gfp_t gfp_mask) __malloc; + +extern void *mempool_alloc_noprof (mempool_t *pool,gfp_t gfp_mask) __malloc; +#define mempool_alloc (...) \ + alloc_hooks(mempool_alloc_noprof(__VA_ARGS__)) +extern void *mempool_alloc_preallocated (mempool_t *pool) __malloc;extern void mempool_free (void *element,mempool_t *pool);
@@ -62,19 +78,10 @@ extern void mempool_free(void *element, mempool_t *pool);void *mempool_alloc_slab (gfp_t gfp_mask,void *pool_data);void mempool_free_slab (void *element,void *pool_data); -static inline int -mempool_init_slab_pool (mempool_t *pool,int min_nr,struct kmem_cache *kc) -{ -return mempool_init (pool, min_nr, mempool_alloc_slab, - mempool_free_slab, (void *) kc); -} - -static inline mempool_t * -mempool_create_slab_pool (int min_nr,struct kmem_cache *kc) -{ -return mempool_create (min_nr, mempool_alloc_slab, mempool_free_slab, - (void *) kc); -} +#define mempool_init_slab_pool (_pool, _min_nr, _kc) \ + mempool_init(_pool, (_min_nr), mempool_alloc_slab, mempool_free_slab, (void *)(_kc)) +#define mempool_create_slab_pool (_min_nr, _kc) \ + mempool_create((_min_nr), mempool_alloc_slab, mempool_free_slab, (void *)(_kc))/* * a mempool_alloc_t and a mempool_free_t to kmalloc and kfree the
@@ -83,17 +90,12 @@ mempool_create_slab_pool(int min_nr, struct kmem_cache *kc)void *mempool_kmalloc (gfp_t gfp_mask,void *pool_data);void mempool_kfree (void *element,void *pool_data); -static inline int mempool_init_kmalloc_pool (mempool_t *pool,int min_nr,size_t size) -{ -return mempool_init (pool, min_nr, mempool_kmalloc, - mempool_kfree, (void *) size); -} - -static inline mempool_t *mempool_create_kmalloc_pool (int min_nr,size_t size) -{ -return mempool_create (min_nr, mempool_kmalloc, mempool_kfree, - (void *) size); -} +#define mempool_init_kmalloc_pool (_pool, _min_nr, _size) \ + mempool_init(_pool, (_min_nr), mempool_kmalloc, mempool_kfree, \ + (void *)(unsigned long)(_size)) +#define mempool_create_kmalloc_pool (_min_nr, _size) \ + mempool_create((_min_nr), mempool_kmalloc, mempool_kfree, \ + (void *)(unsigned long)(_size))/* * A mempool_alloc_t and mempool_free_t for a simple page allocator that
@@ -102,16 +104,11 @@ static inline mempool_t *mempool_create_kmalloc_pool(int min_nr, size_t size)void *mempool_alloc_pages (gfp_t gfp_mask,void *pool_data);void mempool_free_pages (void *element,void *pool_data); -static inline int mempool_init_page_pool (mempool_t *pool,int min_nr,int order) -{ -return mempool_init (pool, min_nr, mempool_alloc_pages, - mempool_free_pages, (void *)(long )order); -} - -static inline mempool_t *mempool_create_page_pool (int min_nr,int order) -{ -return mempool_create (min_nr, mempool_alloc_pages, mempool_free_pages, - (void *)(long )order); -} +#define mempool_init_page_pool (_pool, _min_nr, _order) \ + mempool_init(_pool, (_min_nr), mempool_alloc_pages, \ + mempool_free_pages, (void *)(long)(_order)) +#define mempool_create_page_pool (_min_nr, _order) \ + mempool_create((_min_nr), mempool_alloc_pages, \ + mempool_free_pages, (void *)(long)(_order))#endif /* _LINUX_MEMPOOL_H */
mm/mempool.c +14 -22
@@ -240,17 +240,17 @@ EXPORT_SYMBOL(mempool_init_node);* * Return: %0 on success, negative error code otherwise. */ -int mempool_init (mempool_t *pool,int min_nr,mempool_alloc_t *alloc_fn, -mempool_free_t *free_fn,void *pool_data) +int mempool_init_noprof (mempool_t *pool,int min_nr,mempool_alloc_t *alloc_fn, +mempool_free_t *free_fn,void *pool_data) {return mempool_init_node (pool, min_nr, alloc_fn, free_fn, pool_data,GFP_KERNEL ,NUMA_NO_NODE ); } -EXPORT_SYMBOL (mempool_init); +EXPORT_SYMBOL (mempool_init_noprof);/** -* mempool_create - create a memory pool +* mempool_create_node - create a memory pool * @min_nr: the minimum number of elements guaranteed to be * allocated for this pool. * @alloc_fn: user-defined element-allocation function.
@@ -265,17 +265,9 @@ EXPORT_SYMBOL(mempool_init);* * Return: pointer to the created memory pool object or %NULL on error. */ -mempool_t *mempool_create (int min_nr,mempool_alloc_t *alloc_fn, -mempool_free_t *free_fn,void *pool_data) -{ -return mempool_create_node (min_nr, alloc_fn, free_fn, pool_data, -GFP_KERNEL ,NUMA_NO_NODE ); -} -EXPORT_SYMBOL (mempool_create); - -mempool_t *mempool_create_node (int min_nr,mempool_alloc_t *alloc_fn, -mempool_free_t *free_fn,void *pool_data, -gfp_t gfp_mask,int node_id) +mempool_t *mempool_create_node_noprof (int min_nr,mempool_alloc_t *alloc_fn, +mempool_free_t *free_fn,void *pool_data, +gfp_t gfp_mask,int node_id) {mempool_t *pool;
@@ -291,7 +283,7 @@ mempool_t *mempool_create_node(int min_nr, mempool_alloc_t *alloc_fn,return pool; } -EXPORT_SYMBOL (mempool_create_node); +EXPORT_SYMBOL (mempool_create_node_noprof);/** * mempool_resize - resize an existing memory pool
@@ -374,7 +366,7 @@ int mempool_resize(mempool_t *pool, int new_min_nr)EXPORT_SYMBOL (mempool_resize);/** -* mempool_alloc - allocate an element from a specific memory pool +* mempool_alloc_noprof - allocate an element from a specific memory pool * @pool: pointer to the memory pool which was allocated via * mempool_create(). * @gfp_mask: the usual allocation bitmask.
@@ -387,7 +379,7 @@ EXPORT_SYMBOL(mempool_resize);* * Return: pointer to the allocated element or %NULL on error. */ -void *mempool_alloc (mempool_t *pool,gfp_t gfp_mask) +void *mempool_alloc_noprof (mempool_t *pool,gfp_t gfp_mask) {void *element;unsigned long flags;
@@ -454,7 +446,7 @@ void *mempool_alloc(mempool_t *pool, gfp_t gfp_mask)finish_wait (&pool->wait, &wait); goto repeat_alloc; } -EXPORT_SYMBOL (mempool_alloc); +EXPORT_SYMBOL (mempool_alloc_noprof);/** * mempool_alloc_preallocated - allocate an element from preallocated elements
@@ -562,7 +554,7 @@ void *mempool_alloc_slab(gfp_t gfp_mask, void *pool_data) {struct kmem_cache *mem = pool_data;VM_BUG_ON (mem->ctor); -return kmem_cache_alloc (mem, gfp_mask); +return kmem_cache_alloc_noprof (mem, gfp_mask); }EXPORT_SYMBOL (mempool_alloc_slab);
@@ -580,7 +572,7 @@ EXPORT_SYMBOL(mempool_free_slab);void *mempool_kmalloc (gfp_t gfp_mask,void *pool_data) {size_t size = (size_t )pool_data; -return kmalloc (size, gfp_mask); +return kmalloc_noprof (size, gfp_mask); }EXPORT_SYMBOL (mempool_kmalloc);
@@ -597,7 +589,7 @@ EXPORT_SYMBOL(mempool_kfree);void *mempool_alloc_pages (gfp_t gfp_mask,void *pool_data) {int order = (int )(long )pool_data; -return alloc_pages (gfp_mask, order); +return alloc_pages_noprof (gfp_mask, order); }EXPORT_SYMBOL (mempool_alloc_pages);
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 25/35 xfs: Memory allocation profiling fixups
In reply to Suren BaghdasaryanFrom: Kent Overstreet <[email protected]>
This adds an alloc_hooks() wrapper around kmem_alloc(), so that we can have allocations accounted to the proper callsite.
--- fs/xfs/kmem.c | 4 ++-- fs/xfs/kmem.h | 10 ++++------ 2 files changed, 6 insertions(+), 8 deletions(-)
fs/xfs/kmem.c +2 -2
@@ -8,7 +8,7 @@#include "xfs_trace.h" void * -kmem_alloc (size_t size,xfs_km_flags_t flags) +kmem_alloc_noprof (size_t size,xfs_km_flags_t flags) {int retries =0 ;gfp_t lflags =kmem_flags_convert (flags);
@@ -17,7 +17,7 @@ kmem_alloc(size_t size, xfs_km_flags_t flags)trace_kmem_alloc (size, flags, _RET_IP_); do { - ptr = kmalloc(size, lflags); + ptr = kmalloc_noprof(size, lflags); if (ptr || (flags & KM_MAYFAIL)) return ptr; if (!(++retries % 100))
fs/xfs/kmem.h +4 -6
@@ -6,6 +6,7 @@#ifndef __XFS_SUPPORT_KMEM_H__#define __XFS_SUPPORT_KMEM_H__ +#include <linux/alloc_tag.h> #include <linux/slab.h> #include <linux/sched.h> #include <linux/mm.h>
@@ -56,18 +57,15 @@ kmem_flags_convert(xfs_km_flags_t flags)return lflags; } -extern void *kmem_alloc (size_t ,xfs_km_flags_t );static inline void kmem_free (const void *ptr) {kvfree (ptr); } +extern void *kmem_alloc_noprof (size_t ,xfs_km_flags_t ); +#define kmem_alloc (...) alloc_hooks(kmem_alloc_noprof(__VA_ARGS__)) -static inline void * -kmem_zalloc (size_t size,xfs_km_flags_t flags) -{ -return kmem_alloc (size, flags |KM_ZERO ); -} +#define kmem_zalloc (_size, _flags) kmem_alloc((_size), (_flags) | KM_ZERO)/* * Zone interfaces
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 26/35 mm: percpu: Introduce pcpuobj_ext
In reply to Suren BaghdasaryanFrom: Kent Overstreet <[email protected]>
Upcoming alloc tagging patches require a place to stash per-allocation metadata.
We already do this when memcg is enabled, so this patch generalizes the obj_cgroup * vector in struct pcpu_chunk by creating a pcpu_obj_ext type, which we will be adding to in an upcoming patch - similarly to the previous slabobj_ext patch.
--- mm/percpu-internal.h | 19 +++++++++++++++++-- mm/percpu.c | 30 +++++++++++++++--------------- 2 files changed, 32 insertions(+), 17 deletions(-)
mm/percpu-internal.h +17 -2
@@ -32,6 +32,16 @@ struct pcpu_block_md {int nr_bits;/* total bits responsible for */ }; +struct pcpuobj_ext { +#ifdef CONFIG_MEMCG_KMEM +struct obj_cgroup *cgroup; +#endif +}; + +#ifdef CONFIG_MEMCG_KMEM +#define NEED_PCPUOBJ_EXT +#endif +struct pcpu_chunk {#ifdef CONFIG_PERCPU_STATS int nr_alloc;/* # of allocations */
@@ -64,8 +74,8 @@ struct pcpu_chunk {int end_offset;/* additional area required to have the region end page aligned */ -#ifdef CONFIG_MEMCG_KMEM -struct obj_cgroup **obj_cgroups;/* vector of object cgroups */ +#ifdef NEED_PCPUOBJ_EXT +struct pcpuobj_ext *obj_exts;/* vector of object cgroups */ #endif int nr_pages;/* # of pages served by this chunk */
@@ -74,6 +84,11 @@ struct pcpu_chunk {unsigned long populated[];/* populated bitmap */ }; +static inline bool need_pcpuobj_ext (void ) +{ +return !mem_cgroup_kmem_disabled (); +} +extern spinlock_t pcpu_lock;extern struct list_head *pcpu_chunk_lists;
mm/percpu.c +15 -15
@@ -1392,9 +1392,9 @@ static struct pcpu_chunk * __init pcpu_alloc_first_chunk(unsigned long tmp_addr,panic ("%s: Failed to allocate %zu bytes\n" , __func__, alloc_size); -#ifdef CONFIG_MEMCG_KMEM +#ifdef NEED_PCPUOBJ_EXT /* first chunk is free to use */ - chunk->obj_cgroups =NULL ; + chunk->obj_exts =NULL ;#endif pcpu_init_md_blocks (chunk);
@@ -1463,12 +1463,12 @@ static struct pcpu_chunk *pcpu_alloc_chunk(gfp_t gfp)if (!chunk->md_blocks) goto md_blocks_fail; -#ifdef CONFIG_MEMCG_KMEM -if (!mem_cgroup_kmem_disabled ()) { - chunk->obj_cgroups = +#ifdef NEED_PCPUOBJ_EXT +if (need_pcpuobj_ext ()) { + chunk->obj_exts =pcpu_mem_zalloc (pcpu_chunk_map_bits (chunk) * -sizeof (struct obj_cgroup *), gfp); -if (!chunk->obj_cgroups) +sizeof (struct pcpuobj_ext ), gfp); +if (!chunk->obj_exts) goto objcg_fail; }#endif
@@ -1480,7 +1480,7 @@ static struct pcpu_chunk *pcpu_alloc_chunk(gfp_t gfp)return chunk; -#ifdef CONFIG_MEMCG_KMEM +#ifdef NEED_PCPUOBJ_EXT objcg_fail:pcpu_mem_free (chunk->md_blocks);#endif
@@ -1498,8 +1498,8 @@ static void pcpu_free_chunk(struct pcpu_chunk *chunk) {if (!chunk)return ; -#ifdef CONFIG_MEMCG_KMEM -pcpu_mem_free (chunk->obj_cgroups); +#ifdef NEED_PCPUOBJ_EXT +pcpu_mem_free (chunk->obj_exts);#endif pcpu_mem_free (chunk->md_blocks);pcpu_mem_free (chunk->bound_map);
@@ -1646,9 +1646,9 @@ static void pcpu_memcg_post_alloc_hook(struct obj_cgroup *objcg,if (!objcg)return ; -if (likely (chunk && chunk->obj_cgroups)) { +if (likely (chunk && chunk->obj_exts)) {obj_cgroup_get (objcg); - chunk->obj_cgroups[off >>PCPU_MIN_ALLOC_SHIFT ] = objcg; + chunk->obj_exts[off >>PCPU_MIN_ALLOC_SHIFT ].cgroup = objcg;rcu_read_lock (); mod_memcg_state(obj_cgroup_memcg(objcg), MEMCG_PERCPU_B,
@@ -1663,13 +1663,13 @@ static void pcpu_memcg_free_hook(struct pcpu_chunk *chunk, int off, size_t size) {struct obj_cgroup *objcg; -if (unlikely (!chunk->obj_cgroups)) +if (unlikely (!chunk->obj_exts))return ; - objcg = chunk->obj_cgroups[off >>PCPU_MIN_ALLOC_SHIFT ]; + objcg = chunk->obj_exts[off >>PCPU_MIN_ALLOC_SHIFT ].cgroup;if (!objcg)return ; - chunk->obj_cgroups[off >>PCPU_MIN_ALLOC_SHIFT ] =NULL ; + chunk->obj_exts[off >>PCPU_MIN_ALLOC_SHIFT ].cgroup =NULL ;obj_cgroup_uncharge (objcg,pcpu_obj_full_size (size));
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 27/35 mm: percpu: Add codetag reference into pcpuobj_ext
In reply to Suren BaghdasaryanFrom: Kent Overstreet <[email protected]>
To store codetag for every per-cpu allocation, a codetag reference is embedded into pcpuobj_ext when CONFIG_MEM_ALLOC_PROFILING=y. Hooks to use the newly introduced codetag are added.
--- mm/percpu-internal.h | 11 +++++++++-- mm/percpu.c | 26 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-)
mm/percpu-internal.h +9 -2
@@ -36,9 +36,12 @@ struct pcpuobj_ext {#ifdef CONFIG_MEMCG_KMEM struct obj_cgroup *cgroup;#endif +#ifdef CONFIG_MEM_ALLOC_PROFILING +union codetag_ref tag; +#endif }; -#ifdef CONFIG_MEMCG_KMEM +#if defined(CONFIG_MEMCG_KMEM ) || defined(CONFIG_MEM_ALLOC_PROFILING )#define NEED_PCPUOBJ_EXT #endif
@@ -86,7 +89,11 @@ struct pcpu_chunk {static inline bool need_pcpuobj_ext (void ) { -return !mem_cgroup_kmem_disabled (); +if (IS_ENABLED (CONFIG_MEM_ALLOC_PROFILING )) +return true; +if (!mem_cgroup_kmem_disabled ()) +return true; +return false; }extern spinlock_t pcpu_lock;
mm/percpu.c +26 -0
@@ -1699,6 +1699,32 @@ static void pcpu_memcg_free_hook(struct pcpu_chunk *chunk, int off, size_t size) }#endif /* CONFIG_MEMCG_KMEM */ +#ifdef CONFIG_MEM_ALLOC_PROFILING +static void pcpu_alloc_tag_alloc_hook (struct pcpu_chunk *chunk,int off, +size_t size) +{ +if (mem_alloc_profiling_enabled () &&likely (chunk->obj_exts)) { +alloc_tag_add (&chunk->obj_exts[off >>PCPU_MIN_ALLOC_SHIFT ].tag, + current->alloc_tag, size); + } +} + +static void pcpu_alloc_tag_free_hook (struct pcpu_chunk *chunk,int off,size_t size) +{ +if (mem_alloc_profiling_enabled () &&likely (chunk->obj_exts)) +alloc_tag_sub_noalloc (&chunk->obj_exts[off >>PCPU_MIN_ALLOC_SHIFT ].tag, size); +} +#else +static void pcpu_alloc_tag_alloc_hook (struct pcpu_chunk *chunk,int off, +size_t size) +{ +} + +static void pcpu_alloc_tag_free_hook (struct pcpu_chunk *chunk,int off,size_t size) +{ +} +#endif +/** * pcpu_alloc - the percpu allocator * @size: size of area to allocate in bytes
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 28/35 mm: percpu: enable per-cpu allocation tagging
In reply to Suren BaghdasaryanRedefine __alloc_percpu, __alloc_percpu_gfp and __alloc_reserved_percpu to record allocations and deallocations done by these functions.
--- include/linux/alloc_tag.h | 15 +++++++++ include/linux/percpu.h | 23 +++++++++----- mm/percpu.c | 64 +++++---------------------------------- 3 files changed, 38 insertions(+), 64 deletions(-)
include/linux/alloc_tag.h +15 -0
@@ -140,4 +140,19 @@ static inline void alloc_tag_add(union codetag_ref *ref, struct alloc_tag *tag, _res; \ }) +/* +* workaround for a sparse bug: it complains about res_type_to_err() when +* typeof(_do_alloc) is a __percpu pointer, but gcc won't let us add a separate +* __percpu case to res_type_to_err(): +*/ +#define alloc_hooks_pcpu (_do_alloc) \ +({ \ + typeof(_do_alloc) _res; \ + DEFINE_ALLOC_TAG(_alloc_tag, _old); \ + \ + _res = _do_alloc; \ + alloc_tag_restore(&_alloc_tag, _old); \ + _res; \ +}) +#endif /* _LINUX_ALLOC_TAG_H */
include/linux/percpu.h +16 -7
@@ -2,6 +2,7 @@#ifndef __LINUX_PERCPU_H#define __LINUX_PERCPU_H +#include <linux/alloc_tag.h> #include <linux/mmdebug.h> #include <linux/preempt.h> #include <linux/smp.h>
@@ -9,6 +10,7 @@#include <linux/pfn.h> #include <linux/init.h> #include <linux/cleanup.h> +#include <linux/sched.h> #include <asm/percpu.h>
@@ -125,7 +127,6 @@ extern int __init pcpu_page_first_chunk(size_t reserved_size, pcpu_fc_cpu_to_node_fn_t cpu_to_nd_fn);#endif -extern void __percpu *__alloc_reserved_percpu (size_t size,size_t align)__alloc_size(1) ;extern bool __is_kernel_percpu_address (unsigned long addr,unsigned long *can_addr);extern bool is_kernel_percpu_address (unsigned long addr);
@@ -133,14 +134,16 @@ extern bool is_kernel_percpu_address(unsigned long addr);extern void __init setup_per_cpu_areas (void );#endif -extern void __percpu *__alloc_percpu_gfp (size_t size,size_t align,gfp_t gfp)__alloc_size(1) ; -extern void __percpu *__alloc_percpu (size_t size,size_t align)__alloc_size(1) ; -extern void free_percpu (void __percpu *__pdata); +extern void __percpu *pcpu_alloc_noprof (size_t size,size_t align,bool reserved, +gfp_t gfp)__alloc_size(1) ;extern size_t pcpu_alloc_size (void __percpu *__pdata); -DEFINE_FREE(free_percpu, void __percpu *, free_percpu(_T)) - -extern phys_addr_t per_cpu_ptr_to_phys (void *addr); +#define __alloc_percpu_gfp (_size, _align, _gfp) \ + alloc_hooks_pcpu(pcpu_alloc_noprof(_size, _align, false, _gfp)) +#define __alloc_percpu (_size, _align) \ + alloc_hooks_pcpu(pcpu_alloc_noprof(_size, _align, false, GFP_KERNEL)) +#define __alloc_reserved_percpu (_size, _align) \ + alloc_hooks_pcpu(pcpu_alloc_noprof(_size, _align, true, GFP_KERNEL))#define alloc_percpu_gfp (type, gfp) \ (typeof(type) __percpu *)__alloc_percpu_gfp(sizeof(type), \
@@ -149,6 +152,12 @@ extern phys_addr_t per_cpu_ptr_to_phys(void *addr); (typeof(type) __percpu *)__alloc_percpu(sizeof(type), \ __alignof__(type)) +extern void free_percpu (void __percpu *__pdata); + +DEFINE_FREE(free_percpu, void __percpu *, free_percpu(_T)) + +extern phys_addr_t per_cpu_ptr_to_phys (void *addr); +extern unsigned long pcpu_nr_pages (void );#endif /* __LINUX_PERCPU_H */
mm/percpu.c +7 -57
94 lines not on this page · raw message
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 29/35 mm: vmalloc: Enable memory allocation profiling
In reply to Suren BaghdasaryanFrom: Kent Overstreet <[email protected]>
This wrapps all external vmalloc allocation functions with the alloc_hooks() wrapper, and switches internal allocations to _noprof variants where appropriate, for the new memory allocation profiling feature.
--- drivers/staging/media/atomisp/pci/hmm/hmm.c | 2 +- include/linux/vmalloc.h | 60 ++++++++++---- kernel/kallsyms_selftest.c | 2 +- mm/util.c | 24 +++--- mm/vmalloc.c | 88 ++++++++++----------- 5 files changed, 103 insertions(+), 73 deletions(-)
drivers/staging/media/atomisp/pci/hmm/hmm.c +1 -1
@@ -205,7 +205,7 @@ static ia_css_ptr __hmm_alloc(size_t bytes, enum hmm_bo_type type, }dev_dbg (atomisp_dev,"pages: 0x%08x (%zu bytes), type: %d, vmalloc %p\n" , - bo->start, bytes, type, vmalloc); + bo->start, bytes, type, vmalloc_noprof);return bo->start;
include/linux/vmalloc.h +45 -15
77 lines not on this page · raw message
kernel/kallsyms_selftest.c +1 -1
@@ -82,7 +82,7 @@ static struct test_item test_items[] = {ITEM_FUNC (kallsyms_test_func_static),ITEM_FUNC (kallsyms_test_func),ITEM_FUNC (kallsyms_test_func_weak), -ITEM_FUNC (vmalloc), +ITEM_FUNC (vmalloc_noprof),ITEM_FUNC (vfree), #ifdef CONFIG_KALLSYMS_ALL ITEM_DATA(kallsyms_test_var_bss_static),
mm/util.c +12 -12
@@ -639,7 +639,7 @@ void *kvmalloc_node_noprof(size_t size, gfp_t flags, int node)* about the resulting pointer, and cannot play * protection games. */ -return __vmalloc_node_range (size,1 ,VMALLOC_START ,VMALLOC_END , +return __vmalloc_node_range_noprof (size,1 ,VMALLOC_START ,VMALLOC_END , flags,PAGE_KERNEL ,VM_ALLOW_HUGE_VMAP , node,__builtin_return_address (0 )); }
@@ -698,12 +698,12 @@ void *kvrealloc_noprof(const void *p, size_t oldsize, size_t newsize, gfp_t flagEXPORT_SYMBOL (kvrealloc_noprof);/** -* __vmalloc_array - allocate memory for a virtually contiguous array. +* __vmalloc_array_noprof - allocate memory for a virtually contiguous array. * @n: number of elements. * @size: element size. * @flags: the type of memory to allocate (see kmalloc). */ -void *__vmalloc_array (size_t n,size_t size,gfp_t flags) +void *__vmalloc_array_noprof (size_t n,size_t size,gfp_t flags) {size_t bytes;
@@ -711,18 +711,18 @@ void *__vmalloc_array(size_t n, size_t size, gfp_t flags)return NULL ;return __vmalloc (bytes, flags); } -EXPORT_SYMBOL (__vmalloc_array); +EXPORT_SYMBOL (__vmalloc_array_noprof);/** -* vmalloc_array - allocate memory for a virtually contiguous array. +* vmalloc_array_noprof - allocate memory for a virtually contiguous array. * @n: number of elements. * @size: element size. */ -void *vmalloc_array (size_t n,size_t size) +void *vmalloc_array_noprof (size_t n,size_t size) {return __vmalloc_array (n, size,GFP_KERNEL ); } -EXPORT_SYMBOL (vmalloc_array); +EXPORT_SYMBOL (vmalloc_array_noprof);/** * __vcalloc - allocate and zero memory for a virtually contiguous array.
@@ -730,22 +730,22 @@ EXPORT_SYMBOL(vmalloc_array);* @size: element size. * @flags: the type of memory to allocate (see kmalloc). */ -void *__vcalloc (size_t n,size_t size,gfp_t flags) +void *__vcalloc_noprof (size_t n,size_t size,gfp_t flags) {return __vmalloc_array (n, size, flags | __GFP_ZERO); } -EXPORT_SYMBOL (__vcalloc); +EXPORT_SYMBOL (__vcalloc_noprof);/** -* vcalloc - allocate and zero memory for a virtually contiguous array. +* vcalloc_noprof - allocate and zero memory for a virtually contiguous array. * @n: number of elements. * @size: element size. */ -void *vcalloc (size_t n,size_t size) +void *vcalloc_noprof (size_t n,size_t size) {return __vmalloc_array (n, size,GFP_KERNEL | __GFP_ZERO); } -EXPORT_SYMBOL (vcalloc); +EXPORT_SYMBOL (vcalloc_noprof);struct anon_vma *folio_anon_vma (struct folio *folio) {
mm/vmalloc.c +44 -44
241 lines not on this page · raw message
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 30/35 rhashtable: Plumb through alloc tag
In reply to Suren BaghdasaryanFrom: Kent Overstreet <[email protected]>
This gives better memory allocation profiling results; rhashtable allocations will be accounted to the code that initialized the rhashtable.
--- include/linux/rhashtable-types.h | 11 +++++-- lib/rhashtable.c | 52 +++++++++++++++++++++++++------- 2 files changed, 50 insertions(+), 13 deletions(-)
include/linux/rhashtable-types.h +9 -2
@@ -9,6 +9,7 @@#ifndef _LINUX_RHASHTABLE_TYPES_H#define _LINUX_RHASHTABLE_TYPES_H +#include <linux/alloc_tag.h> #include <linux/atomic.h> #include <linux/compiler.h> #include <linux/mutex.h>
@@ -88,6 +89,9 @@ struct rhashtable {struct mutex mutex;spinlock_t lock;atomic_t nelems; +#ifdef CONFIG_MEM_ALLOC_PROFILING +struct alloc_tag *alloc_tag; +#endif };/**
@@ -127,9 +131,12 @@ struct rhashtable_iter { bool end_of_table; }; -int rhashtable_init (struct rhashtable *ht, +int rhashtable_init_noprof (struct rhashtable *ht,const struct rhashtable_params *params); -int rhltable_init (struct rhltable *hlt, +#define rhashtable_init (...) alloc_hooks(rhashtable_init_noprof(__VA_ARGS__)) + +int rhltable_init_noprof (struct rhltable *hlt,const struct rhashtable_params *params); +#define rhltable_init (...) alloc_hooks(rhltable_init_noprof(__VA_ARGS__))#endif /* _LINUX_RHASHTABLE_TYPES_H */
lib/rhashtable.c +41 -11
133 lines not on this page · raw message
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 31/35 lib: add memory allocations report in show_mem()
In reply to Suren BaghdasaryanInclude allocations in show_mem reports.
--- include/linux/alloc_tag.h | 2 ++ lib/alloc_tag.c | 38 ++++++++++++++++++++++++++++++++++++++ mm/show_mem.c | 15 +++++++++++++++ 3 files changed, 55 insertions(+)
include/linux/alloc_tag.h +2 -0
@@ -30,6 +30,8 @@ struct alloc_tag {#ifdef CONFIG_MEM_ALLOC_PROFILING +void alloc_tags_show_mem_report (struct seq_buf *s); +static inline struct alloc_tag *ct_to_alloc_tag (struct codetag *ct) { return container_of(ct, struct alloc_tag, ct);
lib/alloc_tag.c +38 -0
@@ -96,6 +96,44 @@ static const struct seq_operations allocinfo_seq_op = { .show = allocinfo_show, }; +void alloc_tags_show_mem_report (struct seq_buf *s) +{ +struct codetag_iterator iter; +struct codetag *ct; +struct { +struct codetag *tag; +size_t bytes; + } tags[10 ], n; +unsigned int i, nr =0 ; + +codetag_lock_module_list (alloc_tag_cttype, true); + iter =codetag_get_ct_iter (alloc_tag_cttype); +while ((ct =codetag_next_ct (&iter))) { +struct alloc_tag_counters counter =alloc_tag_read (ct_to_alloc_tag (ct)); + + n.tag = ct; + n.bytes = counter.bytes; + +for (i =0 ; i < nr; i++) +if (n.bytes > tags[i].bytes) +break ; + +if (i <ARRAY_SIZE (tags)) { + nr -= nr ==ARRAY_SIZE (tags); +memmove (&tags[i +1 ], + &tags[i], +sizeof (tags[0 ]) * (nr - i)); + nr++; + tags[i] = n; + } + } + +for (i =0 ; i < nr; i++) +alloc_tag_to_text (s, tags[i].tag); + +codetag_lock_module_list (alloc_tag_cttype, false); +} +static void __init procfs_init (void ) {proc_create_seq ("allocinfo" ,0444 ,NULL , &allocinfo_seq_op);
mm/show_mem.c +15 -0
@@ -12,6 +12,7 @@#include <linux/hugetlb.h> #include <linux/mm.h> #include <linux/mmzone.h> +#include <linux/seq_buf.h> #include <linux/swap.h> #include <linux/vmstat.h>
@@ -423,4 +424,18 @@ void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)#ifdef CONFIG_MEMORY_FAILURE printk ("%lu pages hwpoisoned\n" ,atomic_long_read (&num_poisoned_pages));#endif +#ifdef CONFIG_MEM_ALLOC_PROFILING + { +struct seq_buf s; +char *buf =kmalloc (4096 ,GFP_ATOMIC ); + +if (buf) { +printk ("Memory allocations:\n" ); +seq_buf_init (&s, buf,4096 ); +alloc_tags_show_mem_report (&s); +printk ("%s" , buf); +kfree (buf); + } + } +#endif }
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 32/35 codetag: debug: skip objext checking when it's for objext itself
In reply to Suren Baghdasaryanobjext objects are created with __GFP_NO_OBJ_EXT flag and therefore have no corresponding objext themselves (otherwise we would get an infinite recursion). When freeing these objects their codetag will be empty and when CONFIG_MEM_ALLOC_PROFILING_DEBUG is enabled this will lead to false warnings. Introduce CODETAG_EMPTY special codetag value to mark allocations which intentionally lack codetag to avoid these warnings. Set objext codetags to CODETAG_EMPTY before freeing to indicate that the codetag is expected to be empty.
--- include/linux/alloc_tag.h | 26 ++++++++++++++++++++++++++ mm/slab.h | 25 +++++++++++++++++++++++++ mm/slab_common.c | 1 + mm/slub.c | 8 ++++++++ 4 files changed, 60 insertions(+)
include/linux/alloc_tag.h +26 -0
@@ -77,6 +77,27 @@ static inline struct alloc_tag_counters alloc_tag_read(struct alloc_tag *tag)return v; } +#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG + +#define CODETAG_EMPTY (void *)1 + +static inline bool is_codetag_empty (union codetag_ref *ref) +{ +return ref->ct ==CODETAG_EMPTY ; +} + +static inline void set_codetag_empty (union codetag_ref *ref) +{ +if (ref) + ref->ct =CODETAG_EMPTY ; +} + +#else /* CONFIG_MEM_ALLOC_PROFILING_DEBUG */ + +static inline bool is_codetag_empty (union codetag_ref *ref) {return false; } + +#endif /* CONFIG_MEM_ALLOC_PROFILING_DEBUG */ +static inline void __alloc_tag_sub (union codetag_ref *ref,size_t bytes) {struct alloc_tag *tag;
@@ -87,6 +108,11 @@ static inline void __alloc_tag_sub(union codetag_ref *ref, size_t bytes)if (!ref || !ref->ct)return ; +if (is_codetag_empty (ref)) { + ref->ct =NULL ; +return ; + } + tag =ct_to_alloc_tag (ref->ct);this_cpu_sub (tag->counters->bytes, bytes);
mm/slab.h +25 -0
@@ -567,6 +567,31 @@ static inline struct slabobj_ext *slab_obj_exts(struct slab *slab)int alloc_slab_obj_exts (struct slab *slab,struct kmem_cache *s,gfp_t gfp,bool new_slab); + +#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG + +static inline void mark_objexts_empty (struct slabobj_ext *obj_exts) +{ +struct slabobj_ext *slab_exts; +struct slab *obj_exts_slab; + + obj_exts_slab =virt_to_slab (obj_exts); + slab_exts =slab_obj_exts (obj_exts_slab); +if (slab_exts) { +unsigned int offs =obj_to_index (obj_exts_slab->slab_cache, + obj_exts_slab, obj_exts); +/* codetag should be NULL */ +WARN_ON (slab_exts[offs].ref.ct); +set_codetag_empty (&slab_exts[offs].ref); + } +} + +#else /* CONFIG_MEM_ALLOC_PROFILING_DEBUG */ + +static inline void mark_objexts_empty (struct slabobj_ext *obj_exts) {} + +#endif /* CONFIG_MEM_ALLOC_PROFILING_DEBUG */ +static inline bool need_slab_obj_ext (void ) {#ifdef CONFIG_MEM_ALLOC_PROFILING
mm/slab_common.c +1 -0
@@ -242,6 +242,7 @@ int alloc_slab_obj_exts(struct slab *slab, struct kmem_cache *s,* assign slabobj_exts in parallel. In this case the existing * objcg vector should be reused. */ +mark_objexts_empty (vec);kfree (vec);return 0 ; }
mm/slub.c +8 -0
@@ -1890,6 +1890,14 @@ static inline void free_slab_obj_exts(struct slab *slab)if (!obj_exts)return ; +/* +* obj_exts was created with __GFP_NO_OBJ_EXT flag, therefore its +* corresponding extension will be NULL. alloc_tag_sub() will throw a +* warning if slab has extensions but the extension of an object is +* NULL, therefore replace NULL with CODETAG_EMPTY to indicate that +* the extension for obj_exts is expected to be NULL. +*/ +mark_objexts_empty (obj_exts);kfree (obj_exts); slab->obj_exts =0 ; }
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 33/35 codetag: debug: mark codetags for reserved pages as empty
In reply to Suren BaghdasaryanTo avoid debug warnings while freeing reserved pages which were not allocated with usual allocators, mark their codetags as empty before freeing.
--- include/linux/alloc_tag.h | 2 ++ include/linux/mm.h | 8 ++++++++ include/linux/pgalloc_tag.h | 2 ++ mm/mm_init.c | 9 +++++++++ 4 files changed, 21 insertions(+)
include/linux/alloc_tag.h +2 -0
@@ -95,6 +95,7 @@ static inline void set_codetag_empty(union codetag_ref *ref)#else /* CONFIG_MEM_ALLOC_PROFILING_DEBUG */ static inline bool is_codetag_empty (union codetag_ref *ref) {return false; } +static inline void set_codetag_empty (union codetag_ref *ref) {}#endif /* CONFIG_MEM_ALLOC_PROFILING_DEBUG */
@@ -155,6 +156,7 @@ static inline void alloc_tag_sub(union codetag_ref *ref, size_t bytes) {}static inline void alloc_tag_sub_noalloc (union codetag_ref *ref,size_t bytes) {}static inline void alloc_tag_add (union codetag_ref *ref,struct alloc_tag *tag,size_t bytes) {} +static inline void set_codetag_empty (union codetag_ref *ref) {}#endif
include/linux/mm.h +8 -0
@@ -5,6 +5,7 @@#include <linux/errno.h> #include <linux/mmdebug.h> #include <linux/gfp.h> +#include <linux/pgalloc_tag.h> #include <linux/bug.h> #include <linux/list.h> #include <linux/mmzone.h>
@@ -3112,6 +3113,13 @@ extern void reserve_bootmem_region(phys_addr_t start,/* Free the reserved page into the buddy system, so it gets managed. */ static inline void free_reserved_page (struct page *page) { +union codetag_ref *ref; + + ref =get_page_tag_ref (page); +if (ref) { +set_codetag_empty (ref); +put_page_tag_ref (ref); + }ClearPageReserved (page);init_page_count (page);__free_page (page);
include/linux/pgalloc_tag.h +2 -0
@@ -93,6 +93,8 @@ static inline void pgalloc_tag_split(struct page *page, 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) {}static inline void pgalloc_tag_add (struct page *page,struct task_struct *task,unsigned int order) {}static inline void pgalloc_tag_sub (struct page *page,unsigned int order) {}
mm/mm_init.c +9 -0
@@ -2566,6 +2566,7 @@ void __init set_dma_reserve(unsigned long new_dma_reserve)void __init memblock_free_pages (struct page *page,unsigned long pfn,unsigned int order) { +union codetag_ref *ref;if (IS_ENABLED (CONFIG_DEFERRED_STRUCT_PAGE_INIT )) {int nid =early_pfn_to_nid (pfn);
@@ -2578,6 +2579,14 @@ void __init memblock_free_pages(struct page *page, unsigned long pfn,/* KMSAN will take care of these pages. */ return ; } + +/* pages were reserved and not allocated */ + ref =get_page_tag_ref (page); +if (ref) { +set_codetag_empty (ref); +put_page_tag_ref (ref); + } +__free_pages_core (page, order); }
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 34/35 codetag: debug: introduce OBJEXTS_ALLOC_FAIL to mark failed slab_ext allocations
In reply to Suren BaghdasaryanIf slabobj_ext vector allocation for a slab object fails and later on it succeeds for another object in the same slab, the slabobj_ext for the original object will be NULL and will be flagged in case when CONFIG_MEM_ALLOC_PROFILING_DEBUG is enabled.
Mark failed slabobj_ext vector allocations using a new objext_flags flag stored in the lower bits of slab->obj_exts. When new allocation succeeds it marks all tag references in the same slabobj_ext vector as empty to avoid warnings implemented by CONFIG_MEM_ALLOC_PROFILING_DEBUG checks.
--- include/linux/memcontrol.h | 4 +++- mm/slab.h | 25 +++++++++++++++++++++++++ mm/slab_common.c | 22 +++++++++++++++------- 3 files changed, 43 insertions(+), 8 deletions(-)
include/linux/memcontrol.h +3 -1
@@ -365,8 +365,10 @@ enum page_memcg_data_flags {#endif /* CONFIG_MEMCG */ enum objext_flags { +/* slabobj_ext vector failed to allocate */ +OBJEXTS_ALLOC_FAIL = __FIRST_OBJEXT_FLAG,/* the next bit after the last actual flag */ - __NR_OBJEXTS_FLAGS = __FIRST_OBJEXT_FLAG, + __NR_OBJEXTS_FLAGS = (__FIRST_OBJEXT_FLAG <<1 ), };#define OBJEXTS_FLAGS_MASK (__NR_OBJEXTS_FLAGS - 1)
mm/slab.h +25 -0
@@ -586,9 +586,34 @@ static inline void mark_objexts_empty(struct slabobj_ext *obj_exts) } } +static inline void mark_failed_objexts_alloc (struct slab *slab) +{ + slab->obj_exts =OBJEXTS_ALLOC_FAIL ; +} + +static inline void handle_failed_objexts_alloc (unsigned long obj_exts, +struct slabobj_ext *vec,unsigned int objects) +{ +/* +* If vector previously failed to allocate then we have live +* objects with no tag reference. Mark all references in this +* vector as empty to avoid warnings later on. +*/ +if (obj_exts &OBJEXTS_ALLOC_FAIL ) { +unsigned int i; + +for (i =0 ; i < objects; i++) +set_codetag_empty (&vec[i].ref); + } +} + +#else /* CONFIG_MEM_ALLOC_PROFILING_DEBUG */ static inline void mark_objexts_empty (struct slabobj_ext *obj_exts) {} +static inline void mark_failed_objexts_alloc (struct slab *slab) {} +static inline void handle_failed_objexts_alloc (unsigned long obj_exts, +struct slabobj_ext *vec,unsigned int objects) {}#endif /* CONFIG_MEM_ALLOC_PROFILING_DEBUG */
mm/slab_common.c +15 -7
@@ -214,29 +214,37 @@ int alloc_slab_obj_exts(struct slab *slab, struct kmem_cache *s, gfp_t gfp, bool new_slab) {unsigned int objects =objs_per_slab (s, slab); -unsigned long obj_exts; -void *vec; +unsigned long new_exts; +unsigned long old_exts; +struct slabobj_ext *vec; gfp &= ~OBJCGS_CLEAR_MASK ;/* Prevent recursive extension vector allocation */ gfp |= __GFP_NO_OBJ_EXT; vec =kcalloc_node (objects,sizeof (struct slabobj_ext ), gfp,slab_nid (slab)); -if (!vec) +if (!vec) { +/* Mark vectors which failed to allocate */ +if (new_slab) +mark_failed_objexts_alloc (slab); +return -ENOMEM ; + } - obj_exts = (unsigned long )vec; + new_exts = (unsigned long )vec;#ifdef CONFIG_MEMCG - obj_exts |=MEMCG_DATA_OBJEXTS ; + new_exts |=MEMCG_DATA_OBJEXTS ;#endif + old_exts = slab->obj_exts; +handle_failed_objexts_alloc (old_exts, vec, objects);if (new_slab) {/* * If the slab is brand new and nobody can yet access its * obj_exts, no synchronization is required and obj_exts can * be simply assigned. */ - slab->obj_exts = obj_exts; - }else if (cmpxchg (&slab->obj_exts,0 , obj_exts)) { + slab->obj_exts = new_exts; + }else if (cmpxchg (&slab->obj_exts, old_exts, new_exts) != old_exts) {/* * If the slab is already in use, somebody can allocate and * assign slabobj_exts in parallel. In this case the existing
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Suren Baghdasaryan
maintainerPATCH v3 35/35 MAINTAINERS: Add entries for code tagging and memory allocation profiling
In reply to Suren BaghdasaryanFrom: Kent Overstreet <[email protected]>
The new code & libraries added are being maintained - mark them as such.
--- MAINTAINERS | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
MAINTAINERS +16 -0
@@ -5210,6 +5210,13 @@ S: Supported F: Documentation/process/code-of-conduct-interpretation.rst F: Documentation/process/code-of-conduct.rst +CODE TAGGING +M: Suren Baghdasaryan <[email protected]> +M: Kent Overstreet <[email protected]> +S: Maintained +F: include/linux/codetag.h +F: lib/codetag.c + COMEDI DRIVERS M: Ian Abbott <[email protected]> M: H Hartley Sweeten <[email protected]>
@@ -14056,6 +14063,15 @@ F: mm/memblock.c F: mm/mm_init.c F: tools/testing/memblock/ +MEMORY ALLOCATION PROFILING +M: Suren Baghdasaryan <[email protected]> +M: Kent Overstreet <[email protected]> +S: Maintained +F: include/linux/alloc_tag.h +F: include/linux/codetag_ctx.h +F: lib/alloc_tag.c +F: lib/pgalloc_tag.c + MEMORY CONTROLLER DRIVERS M: Krzysztof Kozlowski <[email protected]> L: [email protected]
Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Re: [PATCH v3 02/35] scripts/kallysms: Always include __start and __stop symbols
In reply to Suren BaghdasaryanQuoted 7 lines · Suren Baghdasaryan
From: Kent Overstreet <[email protected]>
These symbols are used to denote section boundaries: by always including them we can unify loading sections from modules with loading built-in sections, which leads to some significant cleanup.
Signed-off-by Kent Overstreet <[email protected]>
Seems reasonable!
Signature · 1 line
Kees Cook
Re: [PATCH v3 03/35] fs: Convert alloc_inode_sb() to a macro
In reply to Suren BaghdasaryanQuoted 7 lines · Suren Baghdasaryan
From: Kent Overstreet <[email protected]>
We're introducing alloc tagging, which tracks memory allocations by callsite. Converting
alloc_inode_sb()to a macro means allocations will be tracked by its caller, which is a bit more useful.Signed-off-by Kent Overstreet <[email protected]>
Yup, getting these all doing direct calls will be nice.
Signature · 1 line
Kees Cook
Re: [PATCH v3 01/35] lib/string_helpers: Add flags param to string_get_size()
In reply to Suren BaghdasaryanQuoted 9 lines · Suren Baghdasaryan
From: Kent Overstreet <[email protected]>
The new flags parameter allows controlling
- Whether or not the units suffix is separated by a space, for compatibility with sort -h
- Whether or not to append a B suffix - we're not always printing bytes.Signed-off-by Kent Overstreet <[email protected]>
If there is a v4, please include some short examples with the .h bit field documentation. It's pretty obvious right now but these kinds of things have a habit of growing, so let's set a good example for documenting the flags.
Signature · 1 line
Kees Cook
Re: [PATCH v3 04/35] mm: enumerate all gfp flags
In reply to Suren BaghdasaryanQuoted 6 lines · Suren Baghdasaryan
Introduce GFP bits enumeration to let compiler track the number of used bits (which depends on the config options) instead of hardcoding them. That simplifies
__GFP_BITS_SHIFTcalculation.Suggested-by Petr Tesařík <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>
Yeah, looks good.
Signature · 1 line
Kees Cook
Re: [PATCH v3 05/35] mm: introduce slabobj_ext to support slab object extensions
In reply to Suren BaghdasaryanQuoted 7 lines · Suren Baghdasaryan
Currently slab pages can store only vectors of
obj_cgrouppointers inpage->memcg_data. Introduceslabobj_extstructure to allow more data to be stored for each slab object. Wrapobj_cgroupintoslabobj_extto support current functionality while allowing to extendslabobj_extin the future.Signed-off-by Suren Baghdasaryan <[email protected]>
It looks like this doesn't change which buckets GFP_KERNEL_ACCOUNT comes out of, is that correct? I'd love it if we didn't have separate buckets so GFP_KERNEL and GFP_KERNEL_ACCOUNT came from the same pools (so that the randomized pools would cover GFP_KERNEL_ACCOUNT ...)
Regardless:
Signature · 1 line
Kees Cook
Re: [PATCH v3 06/35] mm: introduce __GFP_NO_OBJ_EXT flag to selectively prevent slabobj_ext creation
In reply to Suren BaghdasaryanQuoted 4 lines · Suren Baghdasaryan
Introduce
__GFP_NO_OBJ_EXTflag in order to prevent recursive allocations when allocatingslabobj_exton a slab.Signed-off-by Suren Baghdasaryan <[email protected]>
Signature · 1 line
Kees Cook
Re: [PATCH v3 07/35] mm/slab: introduce SLAB_NO_OBJ_EXT to avoid obj_ext creation
In reply to Suren BaghdasaryanQuoted 8 lines · Suren Baghdasaryan
Slab extension objects can't be allocated before slab infrastructure is initialized. Some caches, like
kmem_cacheandkmem_cache_node, are created before slab infrastructure is initialized. Objects from these caches can't have extension objects. IntroduceSLAB_NO_OBJ_EXTslab flag to mark these caches and avoid creating extensions for objects allocated from these slabs.Signed-off-by Suren Baghdasaryan <[email protected]>
Signature · 1 line
Kees Cook
Re: [PATCH v3 08/35] mm: prevent slabobj_ext allocations for slabobj_ext and kmem_cache objects
In reply to Suren BaghdasaryanQuoted 4 lines · Suren Baghdasaryan
Use
__GFP_NO_OBJ_EXTto prevent recursions when allocatingslabobj_extobjects. Also preventslabobj_extallocations forkmem_cacheobjects.Signed-off-by Suren Baghdasaryan <[email protected]>
I almost feel like this can be collapsed into earlier patches, but regardless:
Signature · 1 line
Kees Cook
Re: [PATCH v3 09/35] slab: objext: introduce objext_flags as extension to page_memcg_data_flags
In reply to Suren BaghdasaryanQuoted 3 lines · Suren Baghdasaryan
Introduce
objext_flagsto store additional objext flags unrelated to memcg.Signed-off-by Suren Baghdasaryan <[email protected]>
Signature · 1 line
Kees Cook
Re: [PATCH v3 10/35] lib: code tagging framework
In reply to Suren BaghdasaryanQuoted 216 lines · Suren Baghdasaryan
Add basic infrastructure to support code tagging which stores tag common information consisting of the module name, function, file name and line number. Provide functions to register a new code tag type and navigate between code tags.
Co-developed-by Kent Overstreet <[email protected]>Signed-off-by Kent Overstreet <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>--- include/linux/codetag.h | 71 ++++++++++++++ lib/Kconfig.debug | 4 + lib/Makefile | 1 + lib/codetag.c | 199 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 275 insertions(+) create mode 100644 include/linux/codetag.h create mode 100644 lib/codetag.cinclude/linux/codetag.h new file +71
-0@@ -0,0 +1,71 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* code tagging framework +*/ +#ifndef _LINUX_CODETAG_H +#define _LINUX_CODETAG_H + +#include <linux/types.h> + +struct codetag_iterator ; +struct codetag_type ; +struct seq_buf ; +struct module ; + +/* +* 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 +* an array of these. +*/ +struct codetag { +unsigned int flags;/* used in later patches */ +unsigned int lineno; +const char *modname; +const char *function; +const char *filename; +}__aligned(8) ; + +union codetag_ref { +struct codetag *ct; +}; + +struct codetag_range { +struct codetag *start; +struct codetag *stop; +}; + +struct codetag_module { +struct module *mod; +struct codetag_range range; +}; + +struct codetag_type_desc { +const char *section; +size_t tag_size; +}; + +struct codetag_iterator { +struct codetag_type *cttype; +struct codetag_module *cmod; +unsigned long mod_id; +struct codetag *ct; +}; + +#define CODE_TAG_INIT { \ + .modname = KBUILD_MODNAME, \ + .function = __func__, \ + .filename = __FILE__, \ + .lineno = __LINE__, \ + .flags = 0, \ +} + +void codetag_lock_module_list (struct codetag_type *cttype,bool lock); +struct codetag_iterator codetag_get_ct_iter (struct codetag_type *cttype); +struct codetag *codetag_next_ct (struct codetag_iterator *iter); + +void codetag_to_text (struct seq_buf *out,struct codetag *ct); + +struct codetag_type * +codetag_register_type (const struct codetag_type_desc *desc); + +#endif /* _LINUX_CODETAG_H */ lib/Kconfig.debug +4
-0@@ -968,6 +968,10 @@ config DEBUG_STACKOVERFLOW If in doubt, say "N". +config CODE_TAGGING + bool + select KALLSYMS + source "lib/Kconfig.kasan" source "lib/Kconfig.kfence" source "lib/Kconfig.kmsan"lib/Makefile +1
-0@@ -235,6 +235,7 @@ obj-$(CONFIG_OF_RECONFIG_NOTIFIER_ERROR_INJECT) += \ of-reconfig-notifier-error-inject.o obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o +obj-$(CONFIG_CODE_TAGGING) += codetag.o lib-$(CONFIG_GENERIC_BUG) += bug.o obj-$(CONFIG_HAVE_ARCH_TRACEHOOK) += syscall.olib/codetag.c new file +89
-0@@ -0,0 +1,199 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <linux/codetag.h> +#include <linux/idr.h> +#include <linux/kallsyms.h> +#include <linux/module.h> +#include <linux/seq_buf.h> +#include <linux/slab.h> + +struct codetag_type { +struct list_head link; +unsigned int count; +struct idr mod_idr; +struct rw_semaphore mod_lock;/* protects mod_idr */ +struct codetag_type_desc desc; +}; + +static DEFINE_MUTEX (codetag_lock); +static LIST_HEAD (codetag_types); + +void codetag_lock_module_list (struct codetag_type *cttype,bool lock) +{ +if (lock) +down_read (&cttype->mod_lock); +else +up_read (&cttype->mod_lock); +} + +struct codetag_iterator codetag_get_ct_iter (struct codetag_type *cttype) +{ +struct codetag_iterator iter = { + .cttype = cttype, + .cmod =NULL , + .mod_id =0 , + .ct =NULL , + }; + +return iter; +} + +static inline struct codetag *get_first_module_ct (struct codetag_module *cmod) +{ +return cmod->range.start < cmod->range.stop ? cmod->range.start :NULL ; +} + +static inline +struct codetag *get_next_module_ct (struct codetag_iterator *iter) +{ +struct codetag *res = (struct codetag *) + ((char *)iter->ct + iter->cttype->desc.tag_size); + +return res < iter->cmod->range.stop ? res :NULL ; +} + +struct codetag *codetag_next_ct (struct codetag_iterator *iter) +{ +struct codetag_type *cttype = iter->cttype; +struct codetag_module *cmod; +struct codetag *ct; + +lockdep_assert_held (&cttype->mod_lock); + +if (unlikely (idr_is_empty (&cttype->mod_idr))) +return NULL ; + + ct =NULL ; +while (true) { + cmod =idr_find (&cttype->mod_idr, iter->mod_id); + +/* If module was removed move to the next one */ +if (!cmod) + cmod =idr_get_next_ul (&cttype->mod_idr, + &iter->mod_id); + +/* Exit if no more modules */ +if (!cmod) +break ; + +if (cmod != iter->cmod) { + iter->cmod = cmod; + ct =get_first_module_ct (cmod); + }else + ct =get_next_module_ct (iter); + +if (ct) +break ; + + iter->mod_id++; + } +
lib/codetag.c line 99
Quoted 10 lines
lib/codetag.c new file +10
-0+ iter->ct = ct; +return ct; +} + +void codetag_to_text (struct seq_buf *out,struct codetag *ct) +{ +seq_buf_printf (out,"%s:%u module:%s func:%s" , + ct->filename, ct->lineno, + ct->modname, ct->function); +}
Thank you for using seq_buf here!
Also, will this need an EXPORT_SYMBOL_GPL()?
lib/codetag.c line 110
Quoted 11 lines
lib/codetag.c new file +11
-0+ +static inline size_t range_size (const struct codetag_type *cttype, +const struct codetag_range *range) +{ +return ((char *)range->stop - (char *)range->start) / + cttype->desc.tag_size; +} + +static void *get_symbol (struct module *mod,const char *prefix,const char *name) +{ +char buf[64 ];
Why is 64 enough? I was expecting KSYM_NAME_LEN here, but perhaps this is specialized enough to section names that it will not be a problem? If so, please document it clearly with a comment.
lib/codetag.c line 115
Quoted 5 lines
lib/codetag.c new file +5
-0+int res; + + res =snprintf (buf,sizeof (buf),"%s%s" , prefix, name); +if (WARN_ON (res <1 || res >sizeof (buf))) +return NULL ;
Please use a seq_buf here instead of snprintf, which we're trying to get rid of.
DECLARE_SEQ_BUF(sb, KSYM_NAME_LEN); char *buf; seq_buf_printf(sb, "%s%s", prefix, name); if (seq_buf_has_overflowed(sb)) return NULL; buf = seq_buf_str(sb);
Quoted 87 lines
lib/codetag.c new file +84
-0+ +return mod ? + (void *)find_kallsyms_symbol_value (mod, buf) : + (void *)kallsyms_lookup_name (buf); +} + +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), + }; +} + +static int codetag_module_init (struct codetag_type *cttype,struct module *mod) +{ +struct codetag_range range; +struct codetag_module *cmod; +int err; + + range =get_section_range (mod, cttype->desc.section); +if (!range.start || !range.stop) { +pr_warn ("Failed to load code tags of type %s from the module %s\n" , + cttype->desc.section, + mod ? mod->name :"(built-in)" ); +return -EINVAL ; + } + +/* Ignore empty ranges */ +if (range.start == range.stop) +return 0 ; + +BUG_ON (range.start > range.stop); + + cmod =kmalloc (sizeof (*cmod),GFP_KERNEL ); +if (unlikely (!cmod)) +return -ENOMEM ; + + cmod->mod = mod; + cmod->range = range; + +down_write (&cttype->mod_lock); + err =idr_alloc (&cttype->mod_idr, cmod,0 ,0 ,GFP_KERNEL ); +if (err >=0 ) + cttype->count +=range_size (cttype, &range); +up_write (&cttype->mod_lock); + +if (err <0 ) { +kfree (cmod); +return err; + } + +return 0 ; +} + +struct codetag_type * +codetag_register_type (const struct codetag_type_desc *desc) +{ +struct codetag_type *cttype; +int err; + +BUG_ON (desc->tag_size <=0 ); + + cttype =kzalloc (sizeof (*cttype),GFP_KERNEL ); +if (unlikely (!cttype)) +return ERR_PTR (-ENOMEM ); + + cttype->desc = *desc; +idr_init (&cttype->mod_idr); +init_rwsem (&cttype->mod_lock); + + err =codetag_module_init (cttype,NULL ); +if (unlikely (err)) { +kfree (cttype); +return ERR_PTR (err); + } + +mutex_lock (&codetag_lock); +list_add_tail (&cttype->link, &codetag_types); +mutex_unlock (&codetag_lock); + +return cttype; +}Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Signature · 1 line
Kees Cook
Re: [PATCH v3 13/35] lib: add allocation tagging support for memory allocation profiling
In reply to Suren BaghdasaryanQuoted 3 lines · Suren Baghdasaryan
Introduce
CONFIG_MEM_ALLOC_PROFILINGwhich provides definitions to easily instrument memory allocators. It registers an "alloc_tags" codetag type with/proc/allocinfointerface to output allocation tag information when
Please don't add anything new to the top-level /proc directory. This should likely live in /sys.
Quoted 187 lines
the feature is enabled.
CONFIG_MEM_ALLOC_PROFILING_DEBUGis provided for debugging the memory allocation profiling instrumentation.
Memory allocation profiling can be enabled or disabled at runtime using/proc/sys/vm/mem_profilingsysctl whenCONFIG_MEM_ALLOC_PROFILING_DEBUG=n.CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULTenables memory allocation profiling by default.Signed-off-by Suren Baghdasaryan <[email protected]>Co-developed-by Kent Overstreet <[email protected]>Signed-off-by Kent Overstreet <[email protected]>--- Documentation/admin-guide/sysctl/vm.rst | 16 +++ Documentation/filesystems/proc.rst | 28 +++++ include/asm-generic/codetag.lds.h | 14 +++ include/asm-generic/vmlinux.lds.h | 3 + include/linux/alloc_tag.h | 133 ++++++++++++++++++++ include/linux/sched.h | 24 ++++ lib/Kconfig.debug | 25 ++++ lib/Makefile | 2 + lib/alloc_tag.c | 158 ++++++++++++++++++++++++ scripts/module.lds.S | 7 ++ 10 files changed, 410 insertions(+) create mode 100644 include/asm-generic/codetag.lds.h create mode 100644 include/linux/alloc_tag.h create mode 100644 lib/alloc_tag.cDocumentation/admin-guide/sysctl/vm.rst +16
-0@@ -43,6 +43,7 @@ Currently, these files are in /proc/sys/vm: - legacy_va_layout - lowmem_reserve_ratio - max_map_count +- mem_profiling (only if CONFIG_MEM_ALLOC_PROFILING=y) - memory_failure_early_kill - memory_failure_recovery - min_free_kbytes@@ -425,6 +426,21 @@ e.g., up to one or two maps per allocation. The default value is 65530. +mem_profiling +============== + +Enable memory profiling (when CONFIG_MEM_ALLOC_PROFILING=y) + +1: Enable memory profiling. + +0: Disabld memory profiling. + +Enabling memory profiling introduces a small performance overhead for all +memory allocations. + +The default value depends on CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT. + + memory_failure_early_kill: ==========================Documentation/filesystems/proc.rst +28
-0@@ -688,6 +688,7 @@ files are there, and which are missing. ============ =============================================================== File Content ============ =============================================================== + allocinfo Memory allocations profiling information apm Advanced power management info bootconfig Kernel command line obtained from boot config, and, if there were kernel parameters from the@@ -953,6 +954,33 @@ also be allocatable although a lot of filesystem metadata may have to be reclaimed to achieve this. +allocinfo +~~~~~~~ + +Provides information about memory allocations at all locations in the code +base. Each allocation in the code is identified by its source file, line +number, module and the function calling the allocation. The number of bytes +allocated at each location is reported. + +Example output. + +:: + + > cat /proc/allocinfo + + 153MiB mm/slub.c:1826 module:slub func:alloc_slab_page + 6.08MiB mm/slab_common.c:950 module:slab_common func:_kmalloc_order + 5.09MiB mm/memcontrol.c:2814 module:memcontrol func:alloc_slab_obj_exts + 4.54MiB mm/page_alloc.c:5777 module:page_alloc func:alloc_pages_exact + 1.32MiB include/asm-generic/pgalloc.h:63 module:pgtable func:__pte_alloc_one + 1.16MiB fs/xfs/xfs_log_priv.h:700 module:xfs func:xlog_kvmalloc + 1.00MiB mm/swap_cgroup.c:48 module:swap_cgroup func:swap_cgroup_prepare + 734KiB fs/xfs/kmem.c:20 module:xfs func:kmem_alloc + 640KiB kernel/rcu/tree.c:3184 module:tree func:fill_page_cache_func + 640KiB drivers/char/virtio_console.c:452 module:virtio_console func:alloc_buf + ... + + meminfo ~~~~~~~include/asm-generic/codetag.lds.h new file +14
-0@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __ASM_GENERIC_CODETAG_LDS_H +#define __ASM_GENERIC_CODETAG_LDS_H + +#define SECTION_WITH_BOUNDARIES (_name) \ + . = ALIGN(8); \ + __start_##_name = .; \ + KEEP(*(_name)) \ + __stop_##_name = .; + +#define CODETAG_SECTIONS () \ + SECTION_WITH_BOUNDARIES(alloc_tags) + +#endif /* __ASM_GENERIC_CODETAG_LDS_H */ include/asm-generic/vmlinux.lds.h +3
-0@@ -50,6 +50,8 @@* [__nosave_begin, __nosave_end] for the nosave data */ +#include <asm-generic/codetag.lds.h> +#ifndef LOAD_OFFSET #define LOAD_OFFSET 0#endif @@ -366,6 +368,7 @@ . = ALIGN(8); \ BOUNDED_SECTION_BY(__dyndbg_classes, ___dyndbg_classes) \ BOUNDED_SECTION_BY(__dyndbg, ___dyndbg) \ + CODETAG_SECTIONS() \ LIKELY_PROFILE() \ BRANCH_PROFILE() \TRACE_PRINTKS () \include/linux/alloc_tag.h new file +33
-0@@ -0,0 +1,133 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* allocation tagging +*/ +#ifndef _LINUX_ALLOC_TAG_H +#define _LINUX_ALLOC_TAG_H + +#include <linux/bug.h> +#include <linux/codetag.h> +#include <linux/container_of.h> +#include <linux/preempt.h> +#include <asm/percpu.h> +#include <linux/cpumask.h> +#include <linux/static_key.h> + +struct alloc_tag_counters { +u64 bytes; +u64 calls; +}; + +/* +* An instance of this structure is created in a special ELF section at every +* allocation callsite. At runtime, the special section is treated as +* an array of these. Embedded codetag utilizes codetag framework. +*/ +struct alloc_tag { +struct codetag ct; +struct alloc_tag_counters __percpu *counters; +}__aligned(8) ; + +#ifdef CONFIG_MEM_ALLOC_PROFILING + +static inline struct alloc_tag *ct_to_alloc_tag (struct codetag *ct)
include/linux/alloc_tag.h line 43
Quoted 10 lines
include/linux/alloc_tag.h new file +10
-0+{ + return container_of(ct, struct alloc_tag, ct); +} + +#ifdef ARCH_NEEDS_WEAK_PER_CPU +/* +* When percpu variables are required to be defined as weak, static percpu +* variables can't be used inside a function (see comments for DECLARE_PER_CPU_SECTION). +*/ +#error "Memory allocation profiling is incompatible with ARCH_NEEDS_WEAK_PER_CPU"
Is this enforced via Kconfig as well? (Looks like only alpha and s390?)
Quoted 95 lines
include/linux/alloc_tag.h new file +90
-0+#endif + +#define DEFINE_ALLOC_TAG (_alloc_tag, _old) \ + static DEFINE_PER_CPU(struct alloc_tag_counters, _alloc_tag_cntr); \ + static struct alloc_tag _alloc_tag __used __aligned(8) \ + __section("alloc_tags") = { \ + .ct = CODE_TAG_INIT, \ + .counters = &_alloc_tag_cntr }; \ + struct alloc_tag * __maybe_unused _old = alloc_tag_save(&_alloc_tag) + +DECLARE_STATIC_KEY_MAYBE (CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT , + mem_alloc_profiling_key); + +static inline bool mem_alloc_profiling_enabled (void ) +{ +return static_branch_maybe (CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT , + &mem_alloc_profiling_key); +} + +static inline struct alloc_tag_counters alloc_tag_read (struct alloc_tag *tag) +{ +struct alloc_tag_counters v = {0 ,0 }; +struct alloc_tag_counters *counter; +int cpu; + +for_each_possible_cpu (cpu) { + counter =per_cpu_ptr (tag->counters, cpu); + v.bytes += counter->bytes; + v.calls += counter->calls; + } + +return v; +} + +static inline void __alloc_tag_sub (union codetag_ref *ref,size_t bytes) +{ +struct alloc_tag *tag; + +#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG +WARN_ONCE (ref && !ref->ct,"alloc_tag was not set\n" ); +#endif +if (!ref || !ref->ct) +return ; + + tag =ct_to_alloc_tag (ref->ct); + +this_cpu_sub (tag->counters->bytes, bytes); +this_cpu_dec (tag->counters->calls); + + ref->ct =NULL ; +} + +static inline void alloc_tag_sub (union codetag_ref *ref,size_t bytes) +{ +__alloc_tag_sub (ref, bytes); +} + +static inline void alloc_tag_sub_noalloc (union codetag_ref *ref,size_t bytes) +{ +__alloc_tag_sub (ref, bytes); +} + +static inline void alloc_tag_add (union codetag_ref *ref,struct alloc_tag *tag,size_t bytes) +{ +#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG +WARN_ONCE (ref && ref->ct, +"alloc_tag was not cleared (got tag for %s:%u)\n" ,\ + ref->ct->filename, ref->ct->lineno); + +WARN_ONCE (!tag,"current->alloc_tag not set" ); +#endif +if (!ref || !tag) +return ; + + ref->ct = &tag->ct; +this_cpu_add (tag->counters->bytes, bytes); +this_cpu_inc (tag->counters->calls); +} + +#else + +#define DEFINE_ALLOC_TAG (_alloc_tag, _old) +static inline void alloc_tag_sub (union codetag_ref *ref,size_t bytes) {} +static inline void alloc_tag_sub_noalloc (union codetag_ref *ref,size_t bytes) {} +static inline void alloc_tag_add (union codetag_ref *ref,struct alloc_tag *tag, +size_t bytes) {} + +#endif + +#endif /* _LINUX_ALLOC_TAG_H */ include/linux/sched.h +0
-0
include/linux/sched.h struct task_struct { · line 775
Quoted 6 lines
include/linux/sched.h +3
-0@@ -770,6 +770,10 @@ struct task_struct {unsigned int flags;unsigned int ptrace; +#ifdef CONFIG_MEM_ALLOC_PROFILING +struct alloc_tag *alloc_tag; +#endif
Normally scheduling is very sensitive to having anything early in task_struct. I would suggest moving this the CONFIG_SCHED_CORE ifdef area.
Quoted 157 lines
include/linux/sched.h +21
-0+#ifdef CONFIG_SMP int on_cpu;struct __call_single_node wake_entry;@@ -810,6 +814,7 @@ struct task_struct {struct task_group *sched_task_group;#endif +#ifdef CONFIG_UCLAMP_TASK /* * Clamp values requested for a scheduling entity. @@ -2183,4 +2188,23 @@ static inline int sched_core_idle_cpu(int cpu) { return idle_cpu(cpu); }extern void sched_set_stop_task (int cpu,struct task_struct *stop); +#ifdef CONFIG_MEM_ALLOC_PROFILING +static inline struct alloc_tag *alloc_tag_save (struct alloc_tag *tag) +{ +swap (current->alloc_tag, tag); +return tag; +} + +static inline void alloc_tag_restore (struct alloc_tag *tag,struct alloc_tag *old) +{ +#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG +WARN (current->alloc_tag != tag,"current->alloc_tag was changed:\n" ); +#endif + current->alloc_tag = old; +} +#else +static inline struct alloc_tag *alloc_tag_save (struct alloc_tag *tag) {return NULL ; } +#define alloc_tag_restore (_tag, _old) +#endif +#endif lib/Kconfig.debug +25
-0@@ -972,6 +972,31 @@ config CODE_TAGGING bool select KALLSYMS +config MEM_ALLOC_PROFILING + bool "Enable memory allocation profiling" + default n + depends on PROC_FS + depends on !DEBUG_FORCE_WEAK_PER_CPU + select CODE_TAGGING + help + Track allocation source code and record total allocation size + initiated at that code location. The mechanism can be used to track + memory leaks with a low performance and memory impact. + +config MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT + bool "Enable memory allocation profiling by default" + default y + depends on MEM_ALLOC_PROFILING + +config MEM_ALLOC_PROFILING_DEBUG + bool "Memory allocation profiler debugging" + default n + depends on MEM_ALLOC_PROFILING + select MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT + help + Adds warnings with helpful error messages for memory allocation + profiling. + source "lib/Kconfig.kasan" source "lib/Kconfig.kfence" source "lib/Kconfig.kmsan"lib/Makefile +2
-0@@ -236,6 +236,8 @@ obj-$(CONFIG_OF_RECONFIG_NOTIFIER_ERROR_INJECT) += \ obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o obj-$(CONFIG_CODE_TAGGING) += codetag.o +obj-$(CONFIG_MEM_ALLOC_PROFILING) += alloc_tag.o + lib-$(CONFIG_GENERIC_BUG) += bug.o obj-$(CONFIG_HAVE_ARCH_TRACEHOOK) += syscall.olib/alloc_tag.c new file +66
-0@@ -0,0 +1,158 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <linux/alloc_tag.h> +#include <linux/fs.h> +#include <linux/gfp.h> +#include <linux/module.h> +#include <linux/proc_fs.h> +#include <linux/seq_buf.h> +#include <linux/seq_file.h> + +static struct codetag_type *alloc_tag_cttype; + +DEFINE_STATIC_KEY_MAYBE (CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT , + mem_alloc_profiling_key); + +static void *allocinfo_start (struct seq_file *m,loff_t *pos) +{ +struct codetag_iterator *iter; +struct codetag *ct; +loff_t node = *pos; + + iter =kzalloc (sizeof (*iter),GFP_KERNEL ); + m->private = iter; +if (!iter) +return NULL ; + +codetag_lock_module_list (alloc_tag_cttype, true); + *iter =codetag_get_ct_iter (alloc_tag_cttype); +while ((ct =codetag_next_ct (iter)) !=NULL && node) + node--; + +return ct ? iter :NULL ; +} + +static void *allocinfo_next (struct seq_file *m,void *arg,loff_t *pos) +{ +struct codetag_iterator *iter = (struct codetag_iterator *)arg; +struct codetag *ct =codetag_next_ct (iter); + + (*pos)++; +if (!ct) +return NULL ; + +return iter; +} + +static void allocinfo_stop (struct seq_file *m,void *arg) +{ +struct codetag_iterator *iter = (struct codetag_iterator *)m->private; + +if (iter) { +codetag_lock_module_list (alloc_tag_cttype, false); +kfree (iter); + } +} + +static void alloc_tag_to_text (struct seq_buf *out,struct codetag *ct) +{ +struct alloc_tag *tag =ct_to_alloc_tag (ct); +struct alloc_tag_counters counter =alloc_tag_read (tag); +s64 bytes = counter.bytes; +char val[10 ], *p = val; + +if (bytes <0 ) { + *p++ ='-' ; + bytes = -bytes; + }
lib/alloc_tag.c line 76
Quoted 10 lines
lib/alloc_tag.c new file +10
-0+ +string_get_size (bytes,1 , +STRING_SIZE_BASE2 |STRING_SIZE_NOSPACE , + p, val +ARRAY_SIZE (val) - p); + +seq_buf_printf (out,"%8s %8llu " , val, counter.calls); +codetag_to_text (out, ct); +seq_buf_putc (out,' ' ); +seq_buf_putc (out,'\n' ); +}
/me does happy seq_buf dance!
Quoted 15 lines
lib/alloc_tag.c new file +15
-0+ +static int allocinfo_show (struct seq_file *m,void *arg) +{ +struct codetag_iterator *iter = (struct codetag_iterator *)arg; +char *bufp; +size_t n =seq_get_buf (m, &bufp); +struct seq_buf buf; + +seq_buf_init (&buf, bufp, n); +alloc_tag_to_text (&buf, iter->ct); +seq_commit (m,seq_buf_used (&buf)); +return 0 ; +} + +static const struct seq_operations allocinfo_seq_op = {
lib/alloc_tag.c line 101
Quoted 10 lines
lib/alloc_tag.c new file +10
-0+ .start = allocinfo_start, + .next = allocinfo_next, + .stop = allocinfo_stop, + .show = allocinfo_show, +}; + +static void __init procfs_init (void ) +{ +proc_create_seq ("allocinfo" ,0444 ,NULL , &allocinfo_seq_op); +}
As mentioned, this really should be in /sys somewhere.
Quoted 77 lines
lib/alloc_tag.c new file +57
-0+ +static bool alloc_tag_module_unload (struct codetag_type *cttype, +struct codetag_module *cmod) +{ +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; + +for (ct =codetag_next_ct (&iter); ct; ct =codetag_next_ct (&iter)) { +if (iter.cmod != cmod) +continue ; + + tag =ct_to_alloc_tag (ct); + counter =alloc_tag_read (tag); + +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; + } + +return module_unused; +} + +static struct ctl_table memory_allocation_profiling_sysctls[] = { + { + .procname ="mem_profiling" , + .data = &mem_alloc_profiling_key, +#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG + .mode =0444 , +#else + .mode =0644 , +#endif + .proc_handler = proc_do_static_key, + }, + { } +}; + +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, + }; + + alloc_tag_cttype =codetag_register_type (&desc); +if (IS_ERR_OR_NULL (alloc_tag_cttype)) +return PTR_ERR (alloc_tag_cttype); + +register_sysctl_init ("vm" , memory_allocation_profiling_sysctls); +procfs_init (); + +return 0 ; +} +module_init (alloc_tag_init);scripts/module.lds.S +3
-0@@ -9,6 +9,8 @@ #define DISCARD_EH_FRAME *(.eh_frame) #endif +#include <asm-generic/codetag.lds.h> + SECTIONS { /DISCARD/ : { *(.discard)@@ -47,12 +49,17 @@ SECTIONS { .data : { *(.data .data.[0-9a-zA-Z_]*) *(.data..L*) + CODETAG_SECTIONS() }
scripts/module.lds.S SECTIONS { · line 64
Quoted 10 lines
scripts/module.lds.S +4
-0.rodata : { *(.rodata .rodata.[0-9a-zA-Z_]*) *(.rodata..L*) } +#else + .data : { + CODETAG_SECTIONS() + } #endif }
Otherwise, looks good.
Signature · 1 line
Kees Cook
Re: [PATCH v3 35/35] MAINTAINERS: Add entries for code tagging and memory allocation profiling
In reply to Suren BaghdasaryanQuoted 30 lines · Suren Baghdasaryan
From: Kent Overstreet <[email protected]>
The new code & libraries added are being maintained - mark them as such.
Signed-off-by Kent Overstreet <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>--- MAINTAINERS | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)MAINTAINERS +7
-0@@ -5210,6 +5210,13 @@ S: Supported F: Documentation/process/code-of-conduct-interpretation.rst F: Documentation/process/code-of-conduct.rst +CODE TAGGING +M: Suren Baghdasaryan <[email protected]> +M: Kent Overstreet <[email protected]> +S: Maintained +F: include/linux/codetag.h +F: lib/codetag.c + COMEDI DRIVERS M: Ian Abbott <[email protected]> M: H Hartley Sweeten <[email protected]>@@ -14056,6 +14063,15 @@ F: mm/memblock.c F: mm/mm_init.c
MAINTAINERS F: mm/memblock.c · line 14073
Quoted 10 lines
MAINTAINERS +8
-0F: tools/testing/memblock/ +MEMORY ALLOCATION PROFILING +M: Suren Baghdasaryan <[email protected]> +M: Kent Overstreet <[email protected]> +S: Maintained +F: include/linux/alloc_tag.h +F: include/linux/codetag_ctx.h +F: lib/alloc_tag.c +F: lib/pgalloc_tag.c
Any mailing list to aim at? linux-mm maybe?
Regardless:
Quoted 7 lines
MAINTAINERS +1
-0+ MEMORY CONTROLLER DRIVERS M: Krzysztof Kozlowski <[email protected]> L: [email protected]Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Signature · 1 line
Kees Cook
Re: [PATCH v3 33/35] codetag: debug: mark codetags for reserved pages as empty
In reply to Suren BaghdasaryanQuoted 3 lines · Suren Baghdasaryan
To avoid debug warnings while freeing reserved pages which were not allocated with usual allocators, mark their codetags as empty before freeing.
How do these get their codetags to begin with? Regardless:
Signature · 1 line
Kees Cook
Re: [PATCH v3 34/35] codetag: debug: introduce OBJEXTS_ALLOC_FAIL to mark failed slab_ext allocations
In reply to Suren BaghdasaryanQuoted 37 lines · Suren Baghdasaryan
If
slabobj_extvector allocation for a slab object fails and later on it succeeds for another object in the same slab, theslabobj_extfor the original object will be NULL and will be flagged in case whenCONFIG_MEM_ALLOC_PROFILING_DEBUGis enabled.
Mark failedslabobj_extvector allocations using a newobjext_flagsflag stored in the lower bits ofslab->obj_exts. When new allocation succeeds it marks all tag references in the sameslabobj_extvector as empty to avoid warnings implemented byCONFIG_MEM_ALLOC_PROFILING_DEBUGchecks.Signed-off-by Suren Baghdasaryan <[email protected]>--- include/linux/memcontrol.h | 4 +++- mm/slab.h | 25 +++++++++++++++++++++++++ mm/slab_common.c | 22 +++++++++++++++------- 3 files changed, 43 insertions(+), 8 deletions(-)include/linux/memcontrol.h +3
-1@@ -365,8 +365,10 @@ enum page_memcg_data_flags {#endif /* CONFIG_MEMCG */ enum objext_flags { +/* slabobj_ext vector failed to allocate */ +OBJEXTS_ALLOC_FAIL = __FIRST_OBJEXT_FLAG,/* the next bit after the last actual flag */ - __NR_OBJEXTS_FLAGS = __FIRST_OBJEXT_FLAG, + __NR_OBJEXTS_FLAGS = (__FIRST_OBJEXT_FLAG <<1 ), };#define OBJEXTS_FLAGS_MASK (__NR_OBJEXTS_FLAGS - 1)mm/slab.h +0
-0
mm/slab.h static inline void mark_objexts_empty(struct slabobj_ext *obj_exts) · line 591
Quoted 6 lines
mm/slab.h +3
-0@@ -586,9 +586,34 @@ static inline void mark_objexts_empty(struct slabobj_ext *obj_exts) } } +static inline void mark_failed_objexts_alloc (struct slab *slab) +{ + slab->obj_exts =OBJEXTS_ALLOC_FAIL ;
Uh, does this mean slab->obj_exts is suddenly non-NULL? Is everything that accesses obj_exts expecting this?
-Kees
Quoted 80 lines
mm/slab.h +22
-0+} + +static inline void handle_failed_objexts_alloc (unsigned long obj_exts, +struct slabobj_ext *vec,unsigned int objects) +{ +/* +* If vector previously failed to allocate then we have live +* objects with no tag reference. Mark all references in this +* vector as empty to avoid warnings later on. +*/ +if (obj_exts &OBJEXTS_ALLOC_FAIL ) { +unsigned int i; + +for (i =0 ; i < objects; i++) +set_codetag_empty (&vec[i].ref); + } +} + +#else /* CONFIG_MEM_ALLOC_PROFILING_DEBUG */ static inline void mark_objexts_empty (struct slabobj_ext *obj_exts) {} +static inline void mark_failed_objexts_alloc (struct slab *slab) {} +static inline void handle_failed_objexts_alloc (unsigned long obj_exts, +struct slabobj_ext *vec,unsigned int objects) {}#endif /* CONFIG_MEM_ALLOC_PROFILING_DEBUG */ mm/slab_common.c +15
-7@@ -214,29 +214,37 @@ int alloc_slab_obj_exts(struct slab *slab, struct kmem_cache *s, gfp_t gfp, bool new_slab) {unsigned int objects =objs_per_slab (s, slab); -unsigned long obj_exts; -void *vec; +unsigned long new_exts; +unsigned long old_exts; +struct slabobj_ext *vec; gfp &= ~OBJCGS_CLEAR_MASK ;/* Prevent recursive extension vector allocation */ gfp |= __GFP_NO_OBJ_EXT; vec =kcalloc_node (objects,sizeof (struct slabobj_ext ), gfp,slab_nid (slab)); -if (!vec) +if (!vec) { +/* Mark vectors which failed to allocate */ +if (new_slab) +mark_failed_objexts_alloc (slab); +return -ENOMEM ; + } - obj_exts = (unsigned long )vec; + new_exts = (unsigned long )vec;#ifdef CONFIG_MEMCG - obj_exts |=MEMCG_DATA_OBJEXTS ; + new_exts |=MEMCG_DATA_OBJEXTS ;#endif + old_exts = slab->obj_exts; +handle_failed_objexts_alloc (old_exts, vec, objects);if (new_slab) {/* * If the slab is brand new and nobody can yet access its * obj_exts, no synchronization is required and obj_exts can * be simply assigned. */ - slab->obj_exts = obj_exts; - }else if (cmpxchg (&slab->obj_exts,0 , obj_exts)) { + slab->obj_exts = new_exts; + }else if (cmpxchg (&slab->obj_exts, old_exts, new_exts) != old_exts) {/* * If the slab is already in use, somebody can allocate and * assign slabobj_exts in parallel. In this case the existing Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Signature · 1 line
Kees Cook
Re: [PATCH v3 17/35] mm: enable page allocation tagging
In reply to Suren BaghdasaryanQuoted 24 lines · Suren Baghdasaryan
Redefine page allocators to record allocation tags upon their invocation. Instrument
post_alloc_hookandfree_pages_prepareto modify current allocation tag.Signed-off-by Suren Baghdasaryan <[email protected]>Co-developed-by Kent Overstreet <[email protected]>Signed-off-by Kent Overstreet <[email protected]>--- include/linux/alloc_tag.h | 10 +++ include/linux/gfp.h | 126 ++++++++++++++++++++++++-------------- include/linux/pagemap.h | 9 ++- mm/compaction.c | 7 ++- mm/filemap.c | 6 +- mm/mempolicy.c | 52 ++++++++-------- mm/page_alloc.c | 60 +++++++++--------- 7 files changed, 160 insertions(+), 110 deletions(-)include/linux/alloc_tag.h +0
-0@@ -130,4 +130,14 @@ static inline void alloc_tag_add(union codetag_ref *ref, struct alloc_tag *tag,#endif
include/linux/alloc_tag.h static inline void alloc_tag_add(union codetag_ref *ref, struct alloc_tag *tag, · line 141
Quoted 10 lines
include/linux/alloc_tag.h +9
-0+#define alloc_hooks (_do_alloc) \ +({ \ + typeof(_do_alloc) _res; \ + DEFINE_ALLOC_TAG(_alloc_tag, _old); \ + \ + _res = _do_alloc; \ + alloc_tag_restore(&_alloc_tag, _old); \ + _res; \ +})
I am delighted to see that __alloc_size survives this indirection. AFAICT, all the fortify goo continues to work with this in use.
-Kees
Quoted 675 lines
include/linux/alloc_tag.h +1
-0+#endif /* _LINUX_ALLOC_TAG_H */ include/linux/gfp.h +79
-47@@ -6,6 +6,8 @@#include <linux/mmzone.h> #include <linux/topology.h> +#include <linux/alloc_tag.h> +#include <linux/sched.h> struct vm_area_struct ;struct mempolicy ;@@ -175,42 +177,46 @@ static inline void arch_free_page(struct page *page, int order) { }static inline void arch_alloc_page (struct page *page,int order) { }#endif -struct page *__alloc_pages (gfp_t gfp,unsigned int order,int preferred_nid, +struct page *__alloc_pages_noprof (gfp_t gfp,unsigned int order,int preferred_nid,nodemask_t *nodemask); -struct folio *__folio_alloc (gfp_t gfp,unsigned int order,int preferred_nid, +#define __alloc_pages (...) alloc_hooks(__alloc_pages_noprof(__VA_ARGS__)) + +struct folio *__folio_alloc_noprof (gfp_t gfp,unsigned int order,int preferred_nid,nodemask_t *nodemask); +#define __folio_alloc (...) alloc_hooks(__folio_alloc_noprof(__VA_ARGS__)) -unsigned long __alloc_pages_bulk (gfp_t gfp,int preferred_nid, +unsigned long alloc_pages_bulk_noprof (gfp_t gfp,int preferred_nid,nodemask_t *nodemask,int nr_pages,struct list_head *page_list,struct page **page_array); +#define __alloc_pages_bulk (...) alloc_hooks(alloc_pages_bulk_noprof(__VA_ARGS__)) -unsigned long alloc_pages_bulk_array_mempolicy (gfp_t gfp, +unsigned long alloc_pages_bulk_array_mempolicy_noprof (gfp_t gfp,unsigned long nr_pages,struct page **page_array); +#define alloc_pages_bulk_array_mempolicy (...) \ + alloc_hooks(alloc_pages_bulk_array_mempolicy_noprof(__VA_ARGS__))/* Bulk allocate order-0 pages */ -static inline unsigned long -alloc_pages_bulk_list (gfp_t gfp,unsigned long nr_pages,struct list_head *list) -{ -return __alloc_pages_bulk (gfp,numa_mem_id (),NULL , nr_pages, list,NULL ); -} +#define alloc_pages_bulk_list (_gfp, _nr_pages, _list) \ + __alloc_pages_bulk(_gfp, numa_mem_id(), NULL, _nr_pages, _list, NULL) -static inline unsigned long -alloc_pages_bulk_array (gfp_t gfp,unsigned long nr_pages,struct page **page_array) -{ -return __alloc_pages_bulk (gfp,numa_mem_id (),NULL , nr_pages,NULL , page_array); -} +#define alloc_pages_bulk_array (_gfp, _nr_pages, _page_array) \ + __alloc_pages_bulk(_gfp, numa_mem_id(), NULL, _nr_pages, NULL, _page_array)static inline unsigned long -alloc_pages_bulk_array_node (gfp_t gfp,int nid,unsigned long nr_pages,struct page **page_array) +alloc_pages_bulk_array_node_noprof (gfp_t gfp,int nid,unsigned long nr_pages, +struct page **page_array) {if (nid ==NUMA_NO_NODE ) nid =numa_mem_id (); -return __alloc_pages_bulk (gfp, nid,NULL , nr_pages,NULL , page_array); +return alloc_pages_bulk_noprof (gfp, nid,NULL , nr_pages,NULL , page_array); } +#define alloc_pages_bulk_array_node (...) \ + alloc_hooks(alloc_pages_bulk_array_node_noprof(__VA_ARGS__)) +static inline void warn_if_node_offline (int this_node,gfp_t gfp_mask) {gfp_t warn_gfp = gfp_mask & (__GFP_THISNODE|__GFP_NOWARN);@@ -230,82 +236,104 @@ static inline void warn_if_node_offline(int this_node, gfp_t gfp_mask)* online. For more general interface, see alloc_pages_node(). */ static inline struct page * -__alloc_pages_node (int nid,gfp_t gfp_mask,unsigned int order) +__alloc_pages_node_noprof (int nid,gfp_t gfp_mask,unsigned int order) {VM_BUG_ON (nid <0 || nid >=MAX_NUMNODES );warn_if_node_offline (nid, gfp_mask); -return __alloc_pages (gfp_mask, order, nid,NULL ); +return __alloc_pages_noprof (gfp_mask, order, nid,NULL ); } +#define __alloc_pages_node (...) alloc_hooks(__alloc_pages_node_noprof(__VA_ARGS__)) +static inline -struct folio *__folio_alloc_node (gfp_t gfp,unsigned int order,int nid) +struct folio *__folio_alloc_node_noprof (gfp_t gfp,unsigned int order,int nid) {VM_BUG_ON (nid <0 || nid >=MAX_NUMNODES );warn_if_node_offline (nid, gfp); -return __folio_alloc (gfp, order, nid,NULL ); +return __folio_alloc_noprof (gfp, order, nid,NULL ); } +#define __folio_alloc_node (...) alloc_hooks(__folio_alloc_node_noprof(__VA_ARGS__)) +/* * Allocate pages, preferring the node given as nid. When nid == NUMA_NO_NODE, * prefer the current CPU's closest node. Otherwise node must be valid and * online. */ -static inline struct page *alloc_pages_node (int nid,gfp_t gfp_mask, -unsigned int order) +static inline struct page *alloc_pages_node_noprof (int nid,gfp_t gfp_mask, +unsigned int order) {if (nid ==NUMA_NO_NODE ) nid =numa_mem_id (); -return __alloc_pages_node (nid, gfp_mask, order); +return __alloc_pages_node_noprof (nid, gfp_mask, order); } +#define alloc_pages_node (...) alloc_hooks(alloc_pages_node_noprof(__VA_ARGS__)) +#ifdef CONFIG_NUMA -struct page *alloc_pages (gfp_t gfp,unsigned int order); -struct page *alloc_pages_mpol (gfp_t gfp,unsigned int order, +struct page *alloc_pages_noprof (gfp_t gfp,unsigned int order); +struct page *alloc_pages_mpol_noprof (gfp_t gfp,unsigned int order,struct mempolicy *mpol,pgoff_t ilx,int nid); -struct folio *folio_alloc (gfp_t gfp,unsigned int order); -struct folio *vma_alloc_folio (gfp_t gfp,int order,struct vm_area_struct *vma, +struct folio *folio_alloc_noprof (gfp_t gfp,unsigned int order); +struct folio *vma_alloc_folio_noprof (gfp_t gfp,int order,struct vm_area_struct *vma,unsigned long addr,bool hugepage);#else -static inline struct page *alloc_pages (gfp_t gfp_mask,unsigned int order) +static inline struct page *alloc_pages_noprof (gfp_t gfp_mask,unsigned int order) { -return alloc_pages_node (numa_node_id (), gfp_mask, order); +return alloc_pages_node_noprof (numa_node_id (), gfp_mask, order); } -static inline struct page *alloc_pages_mpol (gfp_t gfp,unsigned int order, +static inline struct page *alloc_pages_mpol_noprof (gfp_t gfp,unsigned int order,struct mempolicy *mpol,pgoff_t ilx,int nid) { -return alloc_pages (gfp, order); +return alloc_pages_noprof (gfp, order); } -static inline struct folio *folio_alloc (gfp_t gfp,unsigned int order) +static inline struct folio *folio_alloc_noprof (gfp_t gfp,unsigned int order) {return __folio_alloc_node (gfp, order,numa_node_id ()); } -#define vma_alloc_folio (gfp, order, vma, addr, hugepage) \ - folio_alloc(gfp, order) +#define vma_alloc_folio_noprof (gfp, order, vma, addr, hugepage) \ + folio_alloc_noprof(gfp, order)#endif + +#define alloc_pages (...) alloc_hooks(alloc_pages_noprof(__VA_ARGS__)) +#define alloc_pages_mpol (...) alloc_hooks(alloc_pages_mpol_noprof(__VA_ARGS__)) +#define folio_alloc (...) alloc_hooks(folio_alloc_noprof(__VA_ARGS__)) +#define vma_alloc_folio (...) alloc_hooks(vma_alloc_folio_noprof(__VA_ARGS__)) +#define alloc_page (gfp_mask) alloc_pages(gfp_mask, 0) -static inline struct page *alloc_page_vma (gfp_t gfp, + +static inline struct page *alloc_page_vma_noprof (gfp_t gfp,struct vm_area_struct *vma,unsigned long addr) { -struct folio *folio =vma_alloc_folio (gfp,0 , vma, addr, false); +struct folio *folio =vma_alloc_folio_noprof (gfp,0 , vma, addr, false);return &folio->page; } +#define alloc_page_vma (...) alloc_hooks(alloc_page_vma_noprof(__VA_ARGS__)) + +extern unsigned long get_free_pages_noprof (gfp_t gfp_mask,unsigned int order); +#define __get_free_pages (...) alloc_hooks(get_free_pages_noprof(__VA_ARGS__)) -extern unsigned long __get_free_pages (gfp_t gfp_mask,unsigned int order); -extern unsigned long get_zeroed_page (gfp_t gfp_mask); +extern unsigned long get_zeroed_page_noprof (gfp_t gfp_mask); +#define get_zeroed_page (...) alloc_hooks(get_zeroed_page_noprof(__VA_ARGS__)) + +void *alloc_pages_exact_noprof (size_t size,gfp_t gfp_mask)__alloc_size(1) ; +#define alloc_pages_exact (...) alloc_hooks(alloc_pages_exact_noprof(__VA_ARGS__)) -void *alloc_pages_exact (size_t size,gfp_t gfp_mask)__alloc_size(1) ;void free_pages_exact (void *virt,size_t size); -__meminit void *alloc_pages_exact_nid (int nid,size_t size,gfp_t gfp_mask)__alloc_size(2) ; -#define __get_free_page (gfp_mask) \ - __get_free_pages((gfp_mask), 0) +__meminit void *alloc_pages_exact_nid_noprof (int nid,size_t size,gfp_t gfp_mask)__alloc_size(2) ; +#define alloc_pages_exact_nid (...) \ + alloc_hooks(alloc_pages_exact_nid_noprof(__VA_ARGS__)) + +#define __get_free_page (gfp_mask) \ + __get_free_pages((gfp_mask), 0) -#define __get_dma_pages (gfp_mask, order) \ - __get_free_pages((gfp_mask) | GFP_DMA, (order)) +#define __get_dma_pages (gfp_mask, order) \ + __get_free_pages((gfp_mask) | GFP_DMA, (order))extern void __free_pages (struct page *page,unsigned int order);extern void free_pages (unsigned long addr,unsigned int order);@@ -357,10 +385,14 @@ extern gfp_t vma_thp_gfp_mask(struct vm_area_struct *vma);#ifdef CONFIG_CONTIG_ALLOC /* The below functions must be run on a range from a single zone. */ -extern int alloc_contig_range (unsigned long start,unsigned long end, +extern int alloc_contig_range_noprof (unsigned long start,unsigned long end,unsigned migratetype,gfp_t gfp_mask); -extern struct page *alloc_contig_pages (unsigned long nr_pages,gfp_t gfp_mask, -int nid,nodemask_t *nodemask); +#define alloc_contig_range (...) alloc_hooks(alloc_contig_range_noprof(__VA_ARGS__)) + +extern struct page *alloc_contig_pages_noprof (unsigned long nr_pages,gfp_t gfp_mask, +int nid,nodemask_t *nodemask); +#define alloc_contig_pages (...) alloc_hooks(alloc_contig_pages_noprof(__VA_ARGS__)) +#endif void free_contig_range (unsigned long pfn,unsigned long nr_pages);include/linux/pagemap.h +6
-3@@ -542,14 +542,17 @@ static inline void *detach_page_private(struct page *page)#endif #ifdef CONFIG_NUMA -struct folio *filemap_alloc_folio (gfp_t gfp,unsigned int order); +struct folio *filemap_alloc_folio_noprof (gfp_t gfp,unsigned int order);#else -static inline struct folio *filemap_alloc_folio (gfp_t gfp,unsigned int order) +static inline struct folio *filemap_alloc_folio_noprof (gfp_t gfp,unsigned int order) { -return folio_alloc (gfp, order); +return folio_alloc_noprof (gfp, order); }#endif +#define filemap_alloc_folio (...) \ + alloc_hooks(filemap_alloc_folio_noprof(__VA_ARGS__)) +static inline struct page *__page_cache_alloc (gfp_t gfp) {return &filemap_alloc_folio (gfp,0 )->page;mm/compaction.c +6
-1@@ -1781,7 +1781,7 @@ static void isolate_freepages(struct compact_control *cc)* This is a migrate-callback that "allocates" freepages by taking pages * from the isolated freelists in the block we are migrating to. */ -static struct folio *compaction_alloc (struct folio *src,unsigned long data) +static struct folio *compaction_alloc_noprof (struct folio *src,unsigned long data) {struct compact_control *cc = (struct compact_control *)data;struct folio *dst;@@ -1800,6 +1800,11 @@ static struct folio *compaction_alloc(struct folio *src, unsigned long data)return dst; } +static struct folio *compaction_alloc (struct folio *src,unsigned long data) +{ +return alloc_hooks (compaction_alloc_noprof (src, data)); +} +/* * This is a migrate-callback that "frees" freepages back to the isolated * freelist. All pages on the freelist are from the same zone, so there is no mm/filemap.c +3
-3@@ -957,7 +957,7 @@ int filemap_add_folio(struct address_space *mapping, struct folio *folio,EXPORT_SYMBOL_GPL (filemap_add_folio);#ifdef CONFIG_NUMA -struct folio *filemap_alloc_folio (gfp_t gfp,unsigned int order) +struct folio *filemap_alloc_folio_noprof (gfp_t gfp,unsigned int order) {int n;struct folio *folio;@@ -972,9 +972,9 @@ struct folio *filemap_alloc_folio(gfp_t gfp, unsigned int order) return folio; } -return folio_alloc (gfp, order); +return folio_alloc_noprof (gfp, order); } -EXPORT_SYMBOL (filemap_alloc_folio); +EXPORT_SYMBOL (filemap_alloc_folio_noprof);#endif /* mm/mempolicy.c +26
-26@@ -2070,15 +2070,15 @@ static struct page *alloc_pages_preferred_many(gfp_t gfp, unsigned int order,*/ preferred_gfp = gfp | __GFP_NOWARN; preferred_gfp &= ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL); - page =__alloc_pages (preferred_gfp, order, nid, nodemask); + page =__alloc_pages_noprof (preferred_gfp, order, nid, nodemask);if (!page) - page =__alloc_pages (gfp, order, nid,NULL ); + page =__alloc_pages_noprof (gfp, order, nid,NULL );return page; }/** -* alloc_pages_mpol - Allocate pages according to NUMA mempolicy. +* alloc_pages_mpol_noprof - Allocate pages according to NUMA mempolicy. * @gfp: GFP flags. * @order: Order of the page allocation. * @pol: Pointer to the NUMA mempolicy. @@ -2087,7 +2087,7 @@ static struct page *alloc_pages_preferred_many(gfp_t gfp, unsigned int order,* * Return: The page on success or NULL if allocation fails. */ -struct page *alloc_pages_mpol (gfp_t gfp,unsigned int order, +struct page *alloc_pages_mpol_noprof (gfp_t gfp,unsigned int order,struct mempolicy *pol,pgoff_t ilx,int nid) {nodemask_t *nodemask;@@ -2117,7 +2117,7 @@ struct page *alloc_pages_mpol(gfp_t gfp, unsigned int order,* First, try to allocate THP only on local node, but * don't reclaim unnecessarily, just compact. */ - page =__alloc_pages_node (nid, + page =__alloc_pages_node_noprof (nid, gfp | __GFP_THISNODE | __GFP_NORETRY, order);if (page || !(gfp & __GFP_DIRECT_RECLAIM))return page;@@ -2130,7 +2130,7 @@ struct page *alloc_pages_mpol(gfp_t gfp, unsigned int order, } } - page =__alloc_pages (gfp, order, nid, nodemask); + page =__alloc_pages_noprof (gfp, order, nid, nodemask);if (unlikely (pol->mode ==MPOL_INTERLEAVE ) && page) {/* skip NUMA_INTERLEAVE_HIT update if numa stats is disabled */ @@ -2146,7 +2146,7 @@ struct page *alloc_pages_mpol(gfp_t gfp, unsigned int order, }/** -* vma_alloc_folio - Allocate a folio for a VMA. +* vma_alloc_folio_noprof - Allocate a folio for a VMA. * @gfp: GFP flags. * @order: Order of the folio. * @vma: Pointer to VMA. @@ -2161,7 +2161,7 @@ struct page *alloc_pages_mpol(gfp_t gfp, unsigned int order,* * Return: The folio on success or NULL if allocation fails. */ -struct folio *vma_alloc_folio (gfp_t gfp,int order,struct vm_area_struct *vma, +struct folio *vma_alloc_folio_noprof (gfp_t gfp,int order,struct vm_area_struct *vma,unsigned long addr,bool hugepage) {struct mempolicy *pol;@@ -2169,15 +2169,15 @@ struct folio *vma_alloc_folio(gfp_t gfp, int order, struct vm_area_struct *vma,struct page *page; pol =get_vma_policy (vma, addr, order, &ilx); - page =alloc_pages_mpol (gfp | __GFP_COMP, order, - pol, ilx,numa_node_id ()); + page =alloc_pages_mpol_noprof (gfp | __GFP_COMP, order, + pol, ilx,numa_node_id ());mpol_cond_put (pol);return page_rmappable_folio (page); } -EXPORT_SYMBOL (vma_alloc_folio); +EXPORT_SYMBOL (vma_alloc_folio_noprof);/** -* alloc_pages - Allocate pages. +* alloc_pages_noprof - Allocate pages. * @gfp: GFP flags. * @order: Power of two of number of pages to allocate. * @@ -2190,7 +2190,7 @@ EXPORT_SYMBOL(vma_alloc_folio);* flags are used. * Return: The page on success or NULL if allocation fails. */ -struct page *alloc_pages (gfp_t gfp,unsigned int order) +struct page *alloc_pages_noprof (gfp_t gfp,unsigned int order) {struct mempolicy *pol = &default_policy;@@ -2201,16 +2201,16 @@ struct page *alloc_pages(gfp_t gfp, unsigned int order)if (!in_interrupt () && !(gfp & __GFP_THISNODE)) pol =get_task_policy (current); -return alloc_pages_mpol (gfp, order, - pol,NO_INTERLEAVE_INDEX ,numa_node_id ()); +return alloc_pages_mpol_noprof (gfp, order, pol,NO_INTERLEAVE_INDEX , +numa_node_id ()); } -EXPORT_SYMBOL (alloc_pages); +EXPORT_SYMBOL (alloc_pages_noprof); -struct folio *folio_alloc (gfp_t gfp,unsigned int order) +struct folio *folio_alloc_noprof (gfp_t gfp,unsigned int order) { -return page_rmappable_folio (alloc_pages (gfp | __GFP_COMP, order)); +return page_rmappable_folio (alloc_pages_noprof (gfp | __GFP_COMP, order)); } -EXPORT_SYMBOL (folio_alloc); +EXPORT_SYMBOL (folio_alloc_noprof); static unsigned long alloc_pages_bulk_array_interleave(gfp_t gfp, struct mempolicy *pol, unsigned long nr_pages,@@ -2229,13 +2229,13 @@ static unsigned long alloc_pages_bulk_array_interleave(gfp_t gfp,for (i =0 ; i < nodes; i++) {if (delta) { - nr_allocated =__alloc_pages_bulk (gfp, + nr_allocated =alloc_pages_bulk_noprof (gfp,interleave_nodes (pol),NULL , nr_pages_per_node +1 ,NULL , page_array); delta--; }else { - nr_allocated =__alloc_pages_bulk (gfp, + nr_allocated =alloc_pages_bulk_noprof (gfp,interleave_nodes (pol),NULL , nr_pages_per_node,NULL , page_array); }@@ -2257,11 +2257,11 @@ static unsigned long alloc_pages_bulk_array_preferred_many(gfp_t gfp, int nid, preferred_gfp = gfp | __GFP_NOWARN; preferred_gfp &= ~(__GFP_DIRECT_RECLAIM | __GFP_NOFAIL); - nr_allocated =__alloc_pages_bulk (preferred_gfp, nid, &pol->nodes, + nr_allocated =alloc_pages_bulk_noprof (preferred_gfp, nid, &pol->nodes, nr_pages,NULL , page_array);if (nr_allocated < nr_pages) - nr_allocated +=__alloc_pages_bulk (gfp,numa_node_id (),NULL , + nr_allocated +=alloc_pages_bulk_noprof (gfp,numa_node_id (),NULL , nr_pages - nr_allocated,NULL , page_array + nr_allocated);return nr_allocated;@@ -2273,7 +2273,7 @@ static unsigned long alloc_pages_bulk_array_preferred_many(gfp_t gfp, int nid,* It can accelerate memory allocation especially interleaving * allocate memory. */ -unsigned long alloc_pages_bulk_array_mempolicy (gfp_t gfp, +unsigned long alloc_pages_bulk_array_mempolicy_noprof (gfp_t gfp,unsigned long nr_pages,struct page **page_array) {struct mempolicy *pol = &default_policy;@@ -2293,8 +2293,8 @@ unsigned long alloc_pages_bulk_array_mempolicy(gfp_t gfp, nid =numa_node_id (); nodemask =policy_nodemask (gfp, pol,NO_INTERLEAVE_INDEX , &nid); -return __alloc_pages_bulk (gfp, nid, nodemask, - nr_pages,NULL , page_array); +return alloc_pages_bulk_noprof (gfp, nid, nodemask, + nr_pages,NULL , page_array); }int vma_dup_policy (struct vm_area_struct *src,struct vm_area_struct *dst)mm/page_alloc.c +30
-30@@ -4380,7 +4380,7 @@ static inline bool prepare_alloc_pages(gfp_t gfp_mask, unsigned int order,* * Returns the number of pages on the list or array. */ -unsigned long __alloc_pages_bulk (gfp_t gfp,int preferred_nid, +unsigned long alloc_pages_bulk_noprof (gfp_t gfp,int preferred_nid,nodemask_t *nodemask,int nr_pages,struct list_head *page_list,struct page **page_array)@@ -4516,7 +4516,7 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid,pcp_trylock_finish (UP_flags); failed: - page =__alloc_pages (gfp,0 , preferred_nid, nodemask); + page =__alloc_pages_noprof (gfp,0 , preferred_nid, nodemask);if (page) {if (page_list)list_add (&page->lru, page_list);@@ -4527,13 +4527,13 @@ unsigned long __alloc_pages_bulk(gfp_t gfp, int preferred_nid, goto out; } -EXPORT_SYMBOL_GPL (__alloc_pages_bulk); +EXPORT_SYMBOL_GPL (alloc_pages_bulk_noprof);/* * This is the 'heart' of the zoned buddy allocator. */ -struct page *__alloc_pages (gfp_t gfp,unsigned int order,int preferred_nid, -nodemask_t *nodemask) +struct page *__alloc_pages_noprof (gfp_t gfp,unsigned int order, +int preferred_nid,nodemask_t *nodemask) {struct page *page;unsigned int alloc_flags =ALLOC_WMARK_LOW ;@@ -4595,38 +4595,38 @@ struct page *__alloc_pages(gfp_t gfp, unsigned int order, int preferred_nid,return page; } -EXPORT_SYMBOL (__alloc_pages); +EXPORT_SYMBOL (__alloc_pages_noprof); -struct folio *__folio_alloc (gfp_t gfp,unsigned int order,int preferred_nid, +struct folio *__folio_alloc_noprof (gfp_t gfp,unsigned int order,int preferred_nid,nodemask_t *nodemask) { -struct page *page =__alloc_pages (gfp | __GFP_COMP, order, +struct page *page =__alloc_pages_noprof (gfp | __GFP_COMP, order, preferred_nid, nodemask);return page_rmappable_folio (page); } -EXPORT_SYMBOL (__folio_alloc); +EXPORT_SYMBOL (__folio_alloc_noprof);/* * Common helper functions. Never use with __GFP_HIGHMEM because the returned * address cannot represent highmem pages. Use alloc_pages and then kmap if * you need to access high mem. */ -unsigned long __get_free_pages (gfp_t gfp_mask,unsigned int order) +unsigned long get_free_pages_noprof (gfp_t gfp_mask,unsigned int order) {struct page *page; - page =alloc_pages (gfp_mask & ~__GFP_HIGHMEM, order); + page =alloc_pages_noprof (gfp_mask & ~__GFP_HIGHMEM, order);if (!page)return 0 ;return (unsigned long )page_address (page); } -EXPORT_SYMBOL (__get_free_pages); +EXPORT_SYMBOL (get_free_pages_noprof); -unsigned long get_zeroed_page (gfp_t gfp_mask) +unsigned long get_zeroed_page_noprof (gfp_t gfp_mask) { -return __get_free_page (gfp_mask | __GFP_ZERO); +return get_free_pages_noprof (gfp_mask | __GFP_ZERO,0 ); } -EXPORT_SYMBOL (get_zeroed_page); +EXPORT_SYMBOL (get_zeroed_page_noprof);/** * __free_pages - Free pages allocated with alloc_pages(). @@ -4818,7 +4818,7 @@ static void *make_alloc_exact(unsigned long addr, unsigned int order, }/** -* alloc_pages_exact - allocate an exact number physically-contiguous pages. +* alloc_pages_exact_noprof - allocate an exact number physically-contiguous pages. * @size: the number of bytes to allocate * @gfp_mask: GFP flags for the allocation, must not contain __GFP_COMP * @@ -4832,7 +4832,7 @@ static void *make_alloc_exact(unsigned long addr, unsigned int order,* * Return: pointer to the allocated area or %NULL in case of error. */ -void *alloc_pages_exact (size_t size,gfp_t gfp_mask) +void *alloc_pages_exact_noprof (size_t size,gfp_t gfp_mask) {unsigned int order =get_order (size);unsigned long addr;@@ -4840,13 +4840,13 @@ void *alloc_pages_exact(size_t size, gfp_t gfp_mask)if (WARN_ON_ONCE (gfp_mask & (__GFP_COMP | __GFP_HIGHMEM))) gfp_mask &= ~(__GFP_COMP | __GFP_HIGHMEM); - addr =__get_free_pages (gfp_mask, order); + addr =get_free_pages_noprof (gfp_mask, order);return make_alloc_exact (addr, order, size); } -EXPORT_SYMBOL (alloc_pages_exact); +EXPORT_SYMBOL (alloc_pages_exact_noprof);/** -* alloc_pages_exact_nid - allocate an exact number of physically-contiguous +* alloc_pages_exact_nid_noprof - allocate an exact number of physically-contiguous * pages on a node. * @nid: the preferred node ID where memory should be allocated * @size: the number of bytes to allocate @@ -4857,7 +4857,7 @@ EXPORT_SYMBOL(alloc_pages_exact);* * Return: pointer to the allocated area or %NULL in case of error. */ -void *__meminit alloc_pages_exact_nid (int nid,size_t size,gfp_t gfp_mask) +void *__meminit alloc_pages_exact_nid_noprof (int nid,size_t size,gfp_t gfp_mask) {unsigned int order =get_order (size);struct page *p;@@ -4865,7 +4865,7 @@ void * __meminit alloc_pages_exact_nid(int nid, size_t size, gfp_t gfp_mask)if (WARN_ON_ONCE (gfp_mask & (__GFP_COMP | __GFP_HIGHMEM))) gfp_mask &= ~(__GFP_COMP | __GFP_HIGHMEM); - p =alloc_pages_node (nid, gfp_mask, order); + p =alloc_pages_node_noprof (nid, gfp_mask, order);if (!p)return NULL ;return make_alloc_exact ((unsigned long )page_address (p), order, size);@@ -6283,7 +6283,7 @@ int __alloc_contig_migrate_range(struct compact_control *cc, }/** -* alloc_contig_range() -- tries to allocate given range of pages +* alloc_contig_range_noprof() -- tries to allocate given range of pages * @start: start PFN to allocate * @end: one-past-the-last PFN to allocate * @migratetype: migratetype of the underlying pageblocks (either @@ -6303,7 +6303,7 @@ int __alloc_contig_migrate_range(struct compact_control *cc,* pages which PFN is in [start, end) are allocated for the caller and * need to be freed with free_contig_range(). */ -int alloc_contig_range (unsigned long start,unsigned long end, +int alloc_contig_range_noprof (unsigned long start,unsigned long end,unsigned migratetype,gfp_t gfp_mask) {unsigned long outer_start, outer_end;@@ -6427,15 +6427,15 @@ int alloc_contig_range(unsigned long start, unsigned long end,undo_isolate_page_range (start, end, migratetype);return ret; } -EXPORT_SYMBOL (alloc_contig_range); +EXPORT_SYMBOL (alloc_contig_range_noprof);static int __alloc_contig_pages (unsigned long start_pfn,unsigned long nr_pages,gfp_t gfp_mask) {unsigned long end_pfn = start_pfn + nr_pages; -return alloc_contig_range (start_pfn, end_pfn,MIGRATE_MOVABLE , - gfp_mask); +return alloc_contig_range_noprof (start_pfn, end_pfn,MIGRATE_MOVABLE , + gfp_mask); } static bool pfn_range_valid_contig(struct zone *z, unsigned long start_pfn,@@ -6470,7 +6470,7 @@ static bool zone_spans_last_pfn(const struct zone *zone, }/** -* alloc_contig_pages() -- tries to find and allocate contiguous range of pages +* alloc_contig_pages_noprof() -- tries to find and allocate contiguous range of pages * @nr_pages: Number of contiguous pages to allocate * @gfp_mask: GFP mask to limit search and used during compaction * @nid: Target node @@ -6490,8 +6490,8 @@ static bool zone_spans_last_pfn(const struct zone *zone,* * Return: pointer to contiguous pages on success, or NULL if not successful. */ -struct page *alloc_contig_pages (unsigned long nr_pages,gfp_t gfp_mask, -int nid,nodemask_t *nodemask) +struct page *alloc_contig_pages_noprof (unsigned long nr_pages,gfp_t gfp_mask, +int nid,nodemask_t *nodemask) {unsigned long ret, pfn, flags;struct zonelist *zonelist;Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Signature · 1 line
Kees Cook
Re: [PATCH v3 21/35] mm/slab: add allocation accounting into slab allocation and free paths
In reply to Suren BaghdasaryanQuoted 3 lines · Suren Baghdasaryan
Account slab allocations using codetag reference embedded into
slabobj_ext.Signed-off-by Suren Baghdasaryan <[email protected]>
Signature · 1 line
Kees Cook
Re: [PATCH v3 22/35] mm/slab: enable slab allocation tagging for kmalloc and friends
In reply to Suren BaghdasaryanQuoted 6 lines · Suren Baghdasaryan
Redefine kmalloc, krealloc, kzalloc, kcalloc, etc. to record allocations and deallocations done by these functions.
Signed-off-by Suren Baghdasaryan <[email protected]>Co-developed-by Kent Overstreet <[email protected]>Signed-off-by Kent Overstreet <[email protected]>
I'm not a big fan of the _noprof suffix, but anything else I can think of isn't as descriptive, so:
Signature · 1 line
Kees Cook
Suren Baghdasaryan
maintainerRe: [PATCH v3 34/35] codetag: debug: introduce OBJEXTS_ALLOC_FAIL to mark failed slab_ext allocations
In reply to Kees CookQuoted 42 lines · Kees Cook
Quoted 40 lines · Suren Baghdasaryan
If
slabobj_extvector allocation for a slab object fails and later on it succeeds for another object in the same slab, theslabobj_extfor the original object will be NULL and will be flagged in case whenCONFIG_MEM_ALLOC_PROFILING_DEBUGis enabled.
Mark failedslabobj_extvector allocations using a newobjext_flagsflag stored in the lower bits ofslab->obj_exts. When new allocation succeeds it marks all tag references in the sameslabobj_extvector as empty to avoid warnings implemented byCONFIG_MEM_ALLOC_PROFILING_DEBUGchecks.Signed-off-by Suren Baghdasaryan <[email protected]>--- include/linux/memcontrol.h | 4 +++- mm/slab.h | 25 +++++++++++++++++++++++++ mm/slab_common.c | 22 +++++++++++++++------- 3 files changed, 43 insertions(+), 8 deletions(-)include/linux/memcontrol.h +3
-1@@ -365,8 +365,10 @@ enum page_memcg_data_flags {#endif /* CONFIG_MEMCG */ enum objext_flags { +/* slabobj_ext vector failed to allocate */ +OBJEXTS_ALLOC_FAIL = __FIRST_OBJEXT_FLAG,/* the next bit after the last actual flag */ - __NR_OBJEXTS_FLAGS = __FIRST_OBJEXT_FLAG, + __NR_OBJEXTS_FLAGS = (__FIRST_OBJEXT_FLAG <<1 ), };#define OBJEXTS_FLAGS_MASK (__NR_OBJEXTS_FLAGS - 1)mm/slab.h +0
-0@@ -586,9 +586,34 @@ static inline void mark_objexts_empty(struct slabobj_ext *obj_exts) } }
Quoted 6 lines
mm/slab.h static inline void mark_objexts_empty(struct slabobj_ext *obj_exts) · line 591
Quoted 3 lines
mm/slab.h +3
-0+static inline void mark_failed_objexts_alloc (struct slab *slab) +{ + slab->obj_exts =OBJEXTS_ALLOC_FAIL ;Uh, does this mean
slab->obj_extsis suddenly non-NULL? Is everything that accessesobj_extsexpecting this?
Hi Kees,
Thank you for the reviews!
Yes, I believe everything that accesses slab->obj_exts directly (currently alloc_slab_obj_exts() and free_slab_obj_exts()) handle this special non-NULL case. kfence_init_pool() initialized slab->obj_exts directly, but since it's setting it and not accessing it, it does not need to handle OBJEXTS_ALLOC_FAIL. All other slab->obj_exts users use slab_obj_exts() which applies OBJEXTS_FLAGS_MASK and masks out any special bits.
Thanks,
Suren.
Quoted 86 lines
-Kees
Quoted 78 lines
mm/slab.h +22
-0+} + +static inline void handle_failed_objexts_alloc (unsigned long obj_exts, +struct slabobj_ext *vec,unsigned int objects) +{ +/* +* If vector previously failed to allocate then we have live +* objects with no tag reference. Mark all references in this +* vector as empty to avoid warnings later on. +*/ +if (obj_exts &OBJEXTS_ALLOC_FAIL ) { +unsigned int i; + +for (i =0 ; i < objects; i++) +set_codetag_empty (&vec[i].ref); + } +} + +#else /* CONFIG_MEM_ALLOC_PROFILING_DEBUG */ static inline void mark_objexts_empty (struct slabobj_ext *obj_exts) {} +static inline void mark_failed_objexts_alloc (struct slab *slab) {} +static inline void handle_failed_objexts_alloc (unsigned long obj_exts, +struct slabobj_ext *vec,unsigned int objects) {}#endif /* CONFIG_MEM_ALLOC_PROFILING_DEBUG */ mm/slab_common.c +15
-8@@ -214,29 +214,37 @@ int alloc_slab_obj_exts(struct slab *slab, struct kmem_cache *s, gfp_t gfp, bool new_slab) {unsigned int objects =objs_per_slab (s, slab); -unsigned long obj_exts; -void *vec; +unsigned long new_exts; +unsigned long old_exts; +struct slabobj_ext *vec; gfp &= ~OBJCGS_CLEAR_MASK ;/* Prevent recursive extension vector allocation */ gfp |= __GFP_NO_OBJ_EXT; vec =kcalloc_node (objects,sizeof (struct slabobj_ext ), gfp,slab_nid (slab)); -if (!vec) +if (!vec) { +/* Mark vectors which failed to allocate */ +if (new_slab) +mark_failed_objexts_alloc (slab); +return -ENOMEM ; + } - obj_exts = (unsigned long )vec; + new_exts = (unsigned long )vec;#ifdef CONFIG_MEMCG - obj_exts |=MEMCG_DATA_OBJEXTS ; + new_exts |=MEMCG_DATA_OBJEXTS ;#endif + old_exts = slab->obj_exts; +handle_failed_objexts_alloc (old_exts, vec, objects);if (new_slab) {/* * If the slab is brand new and nobody can yet access its * obj_exts, no synchronization is required and obj_exts can * be simply assigned. */ - slab->obj_exts = obj_exts; - }else if (cmpxchg (&slab->obj_exts,0 , obj_exts)) { + slab->obj_exts = new_exts; + }else if (cmpxchg (&slab->obj_exts, old_exts, new_exts) != old_exts) {/* * If the slab is already in use, somebody can allocate and * assign slabobj_exts in parallel. In this case the existing -- Quoted 2 lines
2.43.0.687.g38aa6559b0-goog
--
Kees Cook
Re: [PATCH v3 31/35] lib: add memory allocations report in show_mem()
In reply to Suren BaghdasaryanQuoted 85 lines · Suren Baghdasaryan
Include allocations in
show_memreports.Signed-off-by Kent Overstreet <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>--- include/linux/alloc_tag.h | 2 ++ lib/alloc_tag.c | 38 ++++++++++++++++++++++++++++++++++++++ mm/show_mem.c | 15 +++++++++++++++ 3 files changed, 55 insertions(+)include/linux/alloc_tag.h +2
-0@@ -30,6 +30,8 @@ struct alloc_tag {#ifdef CONFIG_MEM_ALLOC_PROFILING +void alloc_tags_show_mem_report (struct seq_buf *s); +static inline struct alloc_tag *ct_to_alloc_tag (struct codetag *ct) { return container_of(ct, struct alloc_tag, ct);lib/alloc_tag.c +38
-0@@ -96,6 +96,44 @@ static const struct seq_operations allocinfo_seq_op = { .show = allocinfo_show, }; +void alloc_tags_show_mem_report (struct seq_buf *s) +{ +struct codetag_iterator iter; +struct codetag *ct; +struct { +struct codetag *tag; +size_t bytes; + } tags[10 ], n; +unsigned int i, nr =0 ; + +codetag_lock_module_list (alloc_tag_cttype, true); + iter =codetag_get_ct_iter (alloc_tag_cttype); +while ((ct =codetag_next_ct (&iter))) { +struct alloc_tag_counters counter =alloc_tag_read (ct_to_alloc_tag (ct)); + + n.tag = ct; + n.bytes = counter.bytes; + +for (i =0 ; i < nr; i++) +if (n.bytes > tags[i].bytes) +break ; + +if (i <ARRAY_SIZE (tags)) { + nr -= nr ==ARRAY_SIZE (tags); +memmove (&tags[i +1 ], + &tags[i], +sizeof (tags[0 ]) * (nr - i)); + nr++; + tags[i] = n; + } + } + +for (i =0 ; i < nr; i++) +alloc_tag_to_text (s, tags[i].tag); + +codetag_lock_module_list (alloc_tag_cttype, false); +} +static void __init procfs_init (void ) {proc_create_seq ("allocinfo" ,0444 ,NULL , &allocinfo_seq_op);mm/show_mem.c +1
-0@@ -12,6 +12,7 @@#include <linux/hugetlb.h> #include <linux/mm.h> #include <linux/mmzone.h> +#include <linux/seq_buf.h> #include <linux/swap.h> #include <linux/vmstat.h>
mm/show_mem.c void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) · line 430
Quoted 7 lines
mm/show_mem.c +4
-0@@ -423,4 +424,18 @@ void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)#ifdef CONFIG_MEMORY_FAILURE printk ("%lu pages hwpoisoned\n" ,atomic_long_read (&num_poisoned_pages));#endif +#ifdef CONFIG_MEM_ALLOC_PROFILING + { +struct seq_buf s; +char *buf =kmalloc (4096 ,GFP_ATOMIC );
Why 4096? Maybe use PAGE_SIZE instead?
mm/show_mem.c void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) · line 433
Quoted 3 lines
mm/show_mem.c +3
-0+ +if (buf) { +printk ("Memory allocations:\n" );
This needs a printk prefix, or better yet, just use pr_info() or similar.
mm/show_mem.c void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) · line 436
Quoted 3 lines
mm/show_mem.c +3
-0+seq_buf_init (&s, buf,4096 ); +alloc_tags_show_mem_report (&s); +printk ("%s" , buf);
Once a seq_buf "consumes" a char *, please don't use any directly any more. This should be:
pr_info("%s", seq_buf_str(s));Otherwise %NUL termination isn't certain. Very likely, given the use case here, but let's use good hygiene. :)
Quoted 8 lines
mm/show_mem.c +4
-0+ kfree(buf); + } + } +#endif }Signature · 1 line
2.43.0.687.g38aa6559b0-goog
Signature · 1 line
Kees Cook
Quoted 13 lines · Suren Baghdasaryan
Memory allocation, v3 and final:
Overview:
Low overhead [1] per-callsite memory allocation profiling. Not just for debug kernels, overhead low enough to be deployed in production.We're aiming to get this in the next merge window, for 6.9. The feedback we've gotten has been that even out of tree this patchset has already been useful, and there's a significant amount of other work gated on the code tagging functionality included in this patchset [2].
Example output:
Quoted 11 lines
root@moria-kvm:~# sort -h /proc/allocinfo|tail
3.11MiB 2850 fs/ext4/super.c:1408 module:ext4 func:ext4_alloc_inode
3.52MiB 225 kernel/fork.c:356 module:fork func:alloc_thread_stack_node
3.75MiB 960 mm/page_ext.c:270 module:page_ext func:alloc_page_ext
4.00MiB 2 mm/khugepaged.c:893 module:khugepaged func:hpage_collapse_alloc_folio
10.5MiB 168 block/blk-mq.c:3421 module:blk_mq func:blk_mq_alloc_rqs
14.0MiB 3594 include/linux/gfp.h:295 module:filemap func:folio_alloc_noprof
26.8MiB 6856 include/linux/gfp.h:295 module:memory func:folio_alloc_noprof
64.5MiB 98315 fs/xfs/xfs_rmap_item.c:147 module:xfs func:xfs_rui_init
98.7MiB 25264 include/linux/gfp.h:295 module:readahead func:folio_alloc_noprof
125MiB 7357 mm/slub.c:2201 module:slub func:alloc_slab_pageThis kind of memory profiling would be an incredible asset in cloud environments.
Over the past year, we've encountered several kernel memory overhead issues. Two particularly severe cases involved excessively large IOMMU page tables (20GB per machine) and IOVA magazines (up to 8GB). Considering thousands of machines were affected, the cumulative memory waste was huge.
While we eventually resolved these issues with custom kernel profiling hacks (some based on this series) and kdump analysis, comprehensive memory profiling would have significantly accelerated the diagnostic process, pinpointing the precise source of the allocations.
Suren Baghdasaryan
maintainerRe: [PATCH v3 33/35] codetag: debug: mark codetags for reserved pages as empty
In reply to Kees CookQuoted 7 lines · Kees Cook
Quoted 3 lines · Suren Baghdasaryan
To avoid debug warnings while freeing reserved pages which were not allocated with usual allocators, mark their codetags as empty before freeing.
How do these get their codetags to begin with?
The space for the codetag reference is inside the page_ext and that reference is set to NULL. So, unless we set the reference as empty (set it to CODETAG_EMPTY), the free routine will detect that we are freeing an allocation that has never been accounted for and will issue a warning. To prevent this warning we use this CODETAG_EMPTY to denote that this codetag reference is expected to be empty because it was not allocated in a usual way.
Quoted 6 lines
Regardless:
Reviewed-by Kees Cook <[email protected]>--
Kees Cook
Steven Rostedt
reviewerRe: [PATCH v3 31/35] lib: add memory allocations report in show_mem()
In reply to Kees CookQuoted 7 lines · On Mon, 12 Feb 2024 16:10:02 -0800 Kees Cook <[email protected]> wrote:
mm/show_mem.c void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) · line 430
Quoted 5 lines
#endif
mm/show_mem.c +4
-0+#ifdef CONFIG_MEM_ALLOC_PROFILING + { +struct seq_buf s; +char *buf =kmalloc (4096 ,GFP_ATOMIC );Why 4096? Maybe use
PAGE_SIZEinstead?
Will it make a difference for architectures that don't have 4096 PAGE_SIZE? Like PowerPC which has PAGE_SIZE of anywhere between 4K to 256K!
-- Steve
Quoted 2 lines · Suren Baghdasaryan
Low overhead [1] per-callsite memory allocation profiling. Not just for debug kernels, overhead low enough to be deployed in production.
What's the plan for things like devm_kmalloc() and similar relatively simple wrappers? I was thinking it would be possible to reimplement at least devm_kmalloc() with size and flags changing helper a while back:
https://lore.kernel.org/all/202309111428.6F36672F57@keescook/
I suspect it could be possible to adapt the alloc_hooks wrapper in this series similarly:
#define alloc_hooks_prep(_do_alloc, _do_prepare, _do_finish, \
ctx, size, flags) \
({ \
typeof(_do_alloc) _res; \
DEFINE_ALLOC_TAG(_alloc_tag, _old); \
ssize_t _size = (size); \
size_t _usable = _size; \
gfp_t _flags = (flags); \
\
_res = _do_prepare(ctx, &_size, &_flags); \
if (!IS_ERR_OR_NULL(_res) \
_res = _do_alloc(_size, _flags); \
if (!IS_ERR_OR_NULL(_res) \
_res = _do_finish(ctx, _usable, _size, _flags, _res); \
_res; \
})#define devm_kmalloc(dev, size, flags) \ alloc_hooks_prep(kmalloc, devm_alloc_prep, devm_alloc_finish, \ dev, size, flags)
And devm_alloc_prep() and devm_alloc_finish() adapted from the URL above.
And _do_finish instances could be marked with __realloc_size(2)
-Kees
Signature · 1 line
Kees Cook
Re: [PATCH v3 23/35] mm/slub: Mark slab_free_freelist_hook() __always_inline
In reply to Suren BaghdasaryanQuoted 3 lines · Suren Baghdasaryan
From: Kent Overstreet <[email protected]>
It seems we need to be more forceful with the compiler on this one.
Sure, but why?
Quoted 2 lines
Signed-off-by Kent Overstreet <[email protected]>
Signature · 1 line
Kees Cook
Suren Baghdasaryan
maintainerRe: [PATCH v3 35/35] MAINTAINERS: Add entries for code tagging and memory allocation profiling
In reply to Kees CookQuoted 34 lines · Kees Cook
Quoted 32 lines · Suren Baghdasaryan
From: Kent Overstreet <[email protected]>
The new code & libraries added are being maintained - mark them as such.
Signed-off-by Kent Overstreet <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>--- MAINTAINERS | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)MAINTAINERS +7
-0@@ -5210,6 +5210,13 @@ S: Supported F: Documentation/process/code-of-conduct-interpretation.rst F: Documentation/process/code-of-conduct.rst +CODE TAGGING +M: Suren Baghdasaryan <[email protected]> +M: Kent Overstreet <[email protected]> +S: Maintained +F: include/linux/codetag.h +F: lib/codetag.c + COMEDI DRIVERS M: Ian Abbott <[email protected]> M: H Hartley Sweeten <[email protected]>@@ -14056,6 +14063,15 @@ F: mm/memblock.c F: mm/mm_init.c F: tools/testing/memblock/
Quoted 10 lines
MAINTAINERS F: mm/memblock.c · line 14073
Quoted 8 lines
MAINTAINERS +8
-0+MEMORY ALLOCATION PROFILING +M: Suren Baghdasaryan <[email protected]> +M: Kent Overstreet <[email protected]> +S: Maintained +F: include/linux/alloc_tag.h +F: include/linux/codetag_ctx.h +F: lib/alloc_tag.c +F: lib/pgalloc_tag.cAny mailing list to aim at? linux-mm maybe?
Good point. Will add. Thanks!
Quoted 16 lines
Regardless:
Reviewed-by Kees Cook <[email protected]>Quoted 7 lines
MAINTAINERS +1
-1+ MEMORY CONTROLLER DRIVERS M: Krzysztof Kozlowski <[email protected]> L: [email protected] --2.43.0.687.g38aa6559b0-goog
--
Kees Cook
Suren Baghdasaryan
maintainerRe: [PATCH v3 23/35] mm/slub: Mark slab_free_freelist_hook() __always_inline
In reply to Kees CookQuoted 7 lines · Kees Cook
Quoted 3 lines · Suren Baghdasaryan
From: Kent Overstreet <[email protected]>
It seems we need to be more forceful with the compiler on this one.
Sure, but why?
IIRC Kent saw a case when it was not inlined for some reason... Kent, do you recall this?
Quoted 8 lines
Quoted 3 lines
Signed-off-by Kent Overstreet <[email protected]>Reviewed-by Kees Cook <[email protected]>--
Kees Cook
Suren Baghdasaryan
maintainerQuoted 31 lines · Kees Cook
Quoted 2 lines · Suren Baghdasaryan
Low overhead [1] per-callsite memory allocation profiling. Not just for debug kernels, overhead low enough to be deployed in production.
What's the plan for things like
devm_kmalloc()and similar relatively simple wrappers? I was thinking it would be possible to reimplement at leastdevm_kmalloc()with size and flags changing helper a while back:https://lore.kernel.org/all/202309111428.6F36672F57@keescook/
I suspect it could be possible to adapt the
alloc_hookswrapper in this series similarly:#define alloc_hooks_prep(_do_alloc, _do_prepare, _do_finish, \ ctx, size, flags) \ ({ \ typeof(_do_alloc) _res; \ DEFINE_ALLOC_TAG(_alloc_tag, _old); \ ssize_t _size = (size); \ size_t _usable = _size; \ gfp_t _flags = (flags); \ \ _res = _do_prepare(ctx, &_size, &_flags); \ if (!IS_ERR_OR_NULL(_res) \ _res = _do_alloc(_size, _flags); \ if (!IS_ERR_OR_NULL(_res) \ _res = _do_finish(ctx, _usable, _size, _flags, _res); \ _res; \ })
Quoted 8 lines
#define
devm_kmalloc(dev, size, flags)\
alloc_hooks_prep(kmalloc,devm_alloc_prep,devm_alloc_finish, \
dev, size, flags)And
devm_alloc_prep()anddevm_alloc_finish()adapted from the URL above.And
_do_finishinstances could be marked with__realloc_size(2)
devm_kmalloc() is definitely a great candidate to account separately. Looks like it's currently using alloc_dr()->kmalloc_node_track_caller(), so this series will account the internal kmalloc_node_track_caller() allocation. We can easily apply alloc_hook to devm_kmalloc() and friends and replace the kmalloc_node_track_caller() call inside alloc_dr() with kmalloc_node_track_caller_noprof(). That will move accounting directly to devm_kmalloc().
Quoted 5 lines
-Kees
--
Kees Cook
Suren Baghdasaryan
maintainerRe: [PATCH v3 13/35] lib: add allocation tagging support for memory allocation profiling
In reply to Kees CookQuoted 8 lines · Kees Cook
Quoted 3 lines · Suren Baghdasaryan
Introduce
CONFIG_MEM_ALLOC_PROFILINGwhich provides definitions to easily instrument memory allocators. It registers an "alloc_tags" codetag type with/proc/allocinfointerface to output allocation tag information whenPlease don't add anything new to the top-level /proc directory. This should likely live in /sys.
Ack. I'll find a more appropriate place for it then.
It just seemed like such generic information which would belong next to meminfo/zoneinfo and such...
Quoted 192 lines
Quoted 192 lines
the feature is enabled.
CONFIG_MEM_ALLOC_PROFILING_DEBUGis provided for debugging the memory allocation profiling instrumentation.
Memory allocation profiling can be enabled or disabled at runtime using/proc/sys/vm/mem_profilingsysctl whenCONFIG_MEM_ALLOC_PROFILING_DEBUG=n.CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULTenables memory allocation profiling by default.Signed-off-by Suren Baghdasaryan <[email protected]>Co-developed-by Kent Overstreet <[email protected]>Signed-off-by Kent Overstreet <[email protected]>--- Documentation/admin-guide/sysctl/vm.rst | 16 +++ Documentation/filesystems/proc.rst | 28 +++++ include/asm-generic/codetag.lds.h | 14 +++ include/asm-generic/vmlinux.lds.h | 3 + include/linux/alloc_tag.h | 133 ++++++++++++++++++++ include/linux/sched.h | 24 ++++ lib/Kconfig.debug | 25 ++++ lib/Makefile | 2 + lib/alloc_tag.c | 158 ++++++++++++++++++++++++ scripts/module.lds.S | 7 ++ 10 files changed, 410 insertions(+) create mode 100644 include/asm-generic/codetag.lds.h create mode 100644 include/linux/alloc_tag.h create mode 100644 lib/alloc_tag.cDocumentation/admin-guide/sysctl/vm.rst +16
-0@@ -43,6 +43,7 @@ Currently, these files are in /proc/sys/vm: - legacy_va_layout - lowmem_reserve_ratio - max_map_count +- mem_profiling (only if CONFIG_MEM_ALLOC_PROFILING=y) - memory_failure_early_kill - memory_failure_recovery - min_free_kbytes@@ -425,6 +426,21 @@ e.g., up to one or two maps per allocation. The default value is 65530. +mem_profiling +============== + +Enable memory profiling (when CONFIG_MEM_ALLOC_PROFILING=y) + +1: Enable memory profiling. + +0: Disabld memory profiling. + +Enabling memory profiling introduces a small performance overhead for all +memory allocations. + +The default value depends on CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT. + + memory_failure_early_kill: ==========================Documentation/filesystems/proc.rst +28
-0@@ -688,6 +688,7 @@ files are there, and which are missing. ============ =============================================================== File Content ============ =============================================================== + allocinfo Memory allocations profiling information apm Advanced power management info bootconfig Kernel command line obtained from boot config, and, if there were kernel parameters from the@@ -953,6 +954,33 @@ also be allocatable although a lot of filesystem metadata may have to be reclaimed to achieve this. +allocinfo +~~~~~~~ + +Provides information about memory allocations at all locations in the code +base. Each allocation in the code is identified by its source file, line +number, module and the function calling the allocation. The number of bytes +allocated at each location is reported. + +Example output. + +:: + + > cat /proc/allocinfo + + 153MiB mm/slub.c:1826 module:slub func:alloc_slab_page + 6.08MiB mm/slab_common.c:950 module:slab_common func:_kmalloc_order + 5.09MiB mm/memcontrol.c:2814 module:memcontrol func:alloc_slab_obj_exts + 4.54MiB mm/page_alloc.c:5777 module:page_alloc func:alloc_pages_exact + 1.32MiB include/asm-generic/pgalloc.h:63 module:pgtable func:__pte_alloc_one + 1.16MiB fs/xfs/xfs_log_priv.h:700 module:xfs func:xlog_kvmalloc + 1.00MiB mm/swap_cgroup.c:48 module:swap_cgroup func:swap_cgroup_prepare + 734KiB fs/xfs/kmem.c:20 module:xfs func:kmem_alloc + 640KiB kernel/rcu/tree.c:3184 module:tree func:fill_page_cache_func + 640KiB drivers/char/virtio_console.c:452 module:virtio_console func:alloc_buf + ... + + meminfo ~~~~~~~include/asm-generic/codetag.lds.h new file +14
-0@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __ASM_GENERIC_CODETAG_LDS_H +#define __ASM_GENERIC_CODETAG_LDS_H + +#define SECTION_WITH_BOUNDARIES (_name) \ + . = ALIGN(8); \ + __start_##_name = .; \ + KEEP(*(_name)) \ + __stop_##_name = .; + +#define CODETAG_SECTIONS () \ + SECTION_WITH_BOUNDARIES(alloc_tags) + +#endif /* __ASM_GENERIC_CODETAG_LDS_H */ include/asm-generic/vmlinux.lds.h +3
-0@@ -50,6 +50,8 @@* [__nosave_begin, __nosave_end] for the nosave data */ +#include <asm-generic/codetag.lds.h> +#ifndef LOAD_OFFSET #define LOAD_OFFSET 0#endif @@ -366,6 +368,7 @@ . = ALIGN(8); \ BOUNDED_SECTION_BY(__dyndbg_classes, ___dyndbg_classes) \ BOUNDED_SECTION_BY(__dyndbg, ___dyndbg) \ + CODETAG_SECTIONS() \ LIKELY_PROFILE() \ BRANCH_PROFILE() \TRACE_PRINTKS () \include/linux/alloc_tag.h new file +37
-0@@ -0,0 +1,133 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* allocation tagging +*/ +#ifndef _LINUX_ALLOC_TAG_H +#define _LINUX_ALLOC_TAG_H + +#include <linux/bug.h> +#include <linux/codetag.h> +#include <linux/container_of.h> +#include <linux/preempt.h> +#include <asm/percpu.h> +#include <linux/cpumask.h> +#include <linux/static_key.h> + +struct alloc_tag_counters { +u64 bytes; +u64 calls; +}; + +/* +* An instance of this structure is created in a special ELF section at every +* allocation callsite. At runtime, the special section is treated as +* an array of these. Embedded codetag utilizes codetag framework. +*/ +struct alloc_tag { +struct codetag ct; +struct alloc_tag_counters __percpu *counters; +}__aligned(8) ; + +#ifdef CONFIG_MEM_ALLOC_PROFILING + +static inline struct alloc_tag *ct_to_alloc_tag (struct codetag *ct) +{ + return container_of(ct, struct alloc_tag, ct); +} +
Quoted 8 lines
include/linux/alloc_tag.h line 43
Quoted 6 lines
include/linux/alloc_tag.h new file +6
-0+#ifdef ARCH_NEEDS_WEAK_PER_CPU +/* +* When percpu variables are required to be defined as weak, static percpu +* variables can't be used inside a function (see comments for DECLARE_PER_CPU_SECTION). +*/ +#error "Memory allocation profiling is incompatible with ARCH_NEEDS_WEAK_PER_CPU"Is this enforced via
Kconfigas well? (Looks like only alpha and s390?)
Unfortunately ARCH_NEEDS_WEAK_PER_CPU is not a Kconfig option but CONFIG_DEBUG_FORCE_WEAK_PER_CPU is, so that one is handled via Kconfig (see "depends on !DEBUG_FORCE_WEAK_PER_CPU" in this patch). We have to avoid both cases because of this:
https://elixir.bootlin.com/linux/latest/source/include/linux/percpu-defs.h#L75,
so I'm trying to provide an informative error here.
Quoted 99 lines
Quoted 99 lines
include/linux/alloc_tag.h new file +90
-0+#endif + +#define DEFINE_ALLOC_TAG (_alloc_tag, _old) \ + static DEFINE_PER_CPU(struct alloc_tag_counters, _alloc_tag_cntr); \ + static struct alloc_tag _alloc_tag __used __aligned(8) \ + __section("alloc_tags") = { \ + .ct = CODE_TAG_INIT, \ + .counters = &_alloc_tag_cntr }; \ + struct alloc_tag * __maybe_unused _old = alloc_tag_save(&_alloc_tag) + +DECLARE_STATIC_KEY_MAYBE (CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT , + mem_alloc_profiling_key); + +static inline bool mem_alloc_profiling_enabled (void ) +{ +return static_branch_maybe (CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT , + &mem_alloc_profiling_key); +} + +static inline struct alloc_tag_counters alloc_tag_read (struct alloc_tag *tag) +{ +struct alloc_tag_counters v = {0 ,0 }; +struct alloc_tag_counters *counter; +int cpu; + +for_each_possible_cpu (cpu) { + counter =per_cpu_ptr (tag->counters, cpu); + v.bytes += counter->bytes; + v.calls += counter->calls; + } + +return v; +} + +static inline void __alloc_tag_sub (union codetag_ref *ref,size_t bytes) +{ +struct alloc_tag *tag; + +#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG +WARN_ONCE (ref && !ref->ct,"alloc_tag was not set\n" ); +#endif +if (!ref || !ref->ct) +return ; + + tag =ct_to_alloc_tag (ref->ct); + +this_cpu_sub (tag->counters->bytes, bytes); +this_cpu_dec (tag->counters->calls); + + ref->ct =NULL ; +} + +static inline void alloc_tag_sub (union codetag_ref *ref,size_t bytes) +{ +__alloc_tag_sub (ref, bytes); +} + +static inline void alloc_tag_sub_noalloc (union codetag_ref *ref,size_t bytes) +{ +__alloc_tag_sub (ref, bytes); +} + +static inline void alloc_tag_add (union codetag_ref *ref,struct alloc_tag *tag,size_t bytes) +{ +#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG +WARN_ONCE (ref && ref->ct, +"alloc_tag was not cleared (got tag for %s:%u)\n" ,\ + ref->ct->filename, ref->ct->lineno); + +WARN_ONCE (!tag,"current->alloc_tag not set" ); +#endif +if (!ref || !tag) +return ; + + ref->ct = &tag->ct; +this_cpu_add (tag->counters->bytes, bytes); +this_cpu_inc (tag->counters->calls); +} + +#else + +#define DEFINE_ALLOC_TAG (_alloc_tag, _old) +static inline void alloc_tag_sub (union codetag_ref *ref,size_t bytes) {} +static inline void alloc_tag_sub_noalloc (union codetag_ref *ref,size_t bytes) {} +static inline void alloc_tag_add (union codetag_ref *ref,struct alloc_tag *tag, +size_t bytes) {} + +#endif + +#endif /* _LINUX_ALLOC_TAG_H */ include/linux/sched.h +0
-0@@ -770,6 +770,10 @@ struct task_struct {unsigned int flags;unsigned int ptrace;
Quoted 7 lines
include/linux/sched.h struct task_struct { · line 775
Quoted 3 lines
include/linux/sched.h +3
-0+#ifdef CONFIG_MEM_ALLOC_PROFILING +struct alloc_tag *alloc_tag; +#endif Normally scheduling is very sensitive to having anything early in
task_struct. I would suggest moving this theCONFIG_SCHED_COREifdef area.
Thanks for the warning! We will look into that.
Quoted 190 lines
Quoted 158 lines
include/linux/sched.h +21
-0+#ifdef CONFIG_SMP int on_cpu;struct __call_single_node wake_entry;@@ -810,6 +814,7 @@ struct task_struct {struct task_group *sched_task_group;#endif +#ifdef CONFIG_UCLAMP_TASK /* * Clamp values requested for a scheduling entity. @@ -2183,4 +2188,23 @@ static inline int sched_core_idle_cpu(int cpu) { return idle_cpu(cpu); }extern void sched_set_stop_task (int cpu,struct task_struct *stop); +#ifdef CONFIG_MEM_ALLOC_PROFILING +static inline struct alloc_tag *alloc_tag_save (struct alloc_tag *tag) +{ +swap (current->alloc_tag, tag); +return tag; +} + +static inline void alloc_tag_restore (struct alloc_tag *tag,struct alloc_tag *old) +{ +#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG +WARN (current->alloc_tag != tag,"current->alloc_tag was changed:\n" ); +#endif + current->alloc_tag = old; +} +#else +static inline struct alloc_tag *alloc_tag_save (struct alloc_tag *tag) {return NULL ; } +#define alloc_tag_restore (_tag, _old) +#endif +#endif lib/Kconfig.debug +25
-0@@ -972,6 +972,31 @@ config CODE_TAGGING bool select KALLSYMS +config MEM_ALLOC_PROFILING + bool "Enable memory allocation profiling" + default n + depends on PROC_FS + depends on !DEBUG_FORCE_WEAK_PER_CPU + select CODE_TAGGING + help + Track allocation source code and record total allocation size + initiated at that code location. The mechanism can be used to track + memory leaks with a low performance and memory impact. + +config MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT + bool "Enable memory allocation profiling by default" + default y + depends on MEM_ALLOC_PROFILING + +config MEM_ALLOC_PROFILING_DEBUG + bool "Memory allocation profiler debugging" + default n + depends on MEM_ALLOC_PROFILING + select MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT + help + Adds warnings with helpful error messages for memory allocation + profiling. + source "lib/Kconfig.kasan" source "lib/Kconfig.kfence" source "lib/Kconfig.kmsan"lib/Makefile +2
-0@@ -236,6 +236,8 @@ obj-$(CONFIG_OF_RECONFIG_NOTIFIER_ERROR_INJECT) += \ obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o obj-$(CONFIG_CODE_TAGGING) += codetag.o +obj-$(CONFIG_MEM_ALLOC_PROFILING) += alloc_tag.o + lib-$(CONFIG_GENERIC_BUG) += bug.o obj-$(CONFIG_HAVE_ARCH_TRACEHOOK) += syscall.olib/alloc_tag.c new file +66
-0@@ -0,0 +1,158 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <linux/alloc_tag.h> +#include <linux/fs.h> +#include <linux/gfp.h> +#include <linux/module.h> +#include <linux/proc_fs.h> +#include <linux/seq_buf.h> +#include <linux/seq_file.h> + +static struct codetag_type *alloc_tag_cttype; + +DEFINE_STATIC_KEY_MAYBE (CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT , + mem_alloc_profiling_key); + +static void *allocinfo_start (struct seq_file *m,loff_t *pos) +{ +struct codetag_iterator *iter; +struct codetag *ct; +loff_t node = *pos; + + iter =kzalloc (sizeof (*iter),GFP_KERNEL ); + m->private = iter; +if (!iter) +return NULL ; + +codetag_lock_module_list (alloc_tag_cttype, true); + *iter =codetag_get_ct_iter (alloc_tag_cttype); +while ((ct =codetag_next_ct (iter)) !=NULL && node) + node--; + +return ct ? iter :NULL ; +} + +static void *allocinfo_next (struct seq_file *m,void *arg,loff_t *pos) +{ +struct codetag_iterator *iter = (struct codetag_iterator *)arg; +struct codetag *ct =codetag_next_ct (iter); + + (*pos)++; +if (!ct) +return NULL ; + +return iter; +} + +static void allocinfo_stop (struct seq_file *m,void *arg) +{ +struct codetag_iterator *iter = (struct codetag_iterator *)m->private; + +if (iter) { +codetag_lock_module_list (alloc_tag_cttype, false); +kfree (iter); + } +} + +static void alloc_tag_to_text (struct seq_buf *out,struct codetag *ct) +{ +struct alloc_tag *tag =ct_to_alloc_tag (ct); +struct alloc_tag_counters counter =alloc_tag_read (tag); +s64 bytes = counter.bytes; +char val[10 ], *p = val; + +if (bytes <0 ) { + *p++ ='-' ; + bytes = -bytes; + }lib/alloc_tag.c line 76
Quoted 10 lines
lib/alloc_tag.c new file +10
-0+ +string_get_size (bytes,1 , +STRING_SIZE_BASE2 |STRING_SIZE_NOSPACE , + p, val +ARRAY_SIZE (val) - p); + +seq_buf_printf (out,"%8s %8llu " , val, counter.calls); +codetag_to_text (out, ct); +seq_buf_putc (out,' ' ); +seq_buf_putc (out,'\n' ); +}/me does happy
seq_bufdance!Quoted 19 lines
lib/alloc_tag.c new file +19
-0+ +static int allocinfo_show (struct seq_file *m,void *arg) +{ +struct codetag_iterator *iter = (struct codetag_iterator *)arg; +char *bufp; +size_t n =seq_get_buf (m, &bufp); +struct seq_buf buf; + +seq_buf_init (&buf, bufp, n); +alloc_tag_to_text (&buf, iter->ct); +seq_commit (m,seq_buf_used (&buf)); +return 0 ; +} + +static const struct seq_operations allocinfo_seq_op = { + .start = allocinfo_start, + .next = allocinfo_next, + .stop = allocinfo_stop, + .show = allocinfo_show,
Quoted 8 lines
lib/alloc_tag.c line 101
Quoted 6 lines
lib/alloc_tag.c new file +6
-0+}; + +static void __init procfs_init (void ) +{ +proc_create_seq ("allocinfo" ,0444 ,NULL , &allocinfo_seq_op); +}As mentioned, this really should be in /sys somewhere.
Ack.
Quoted 78 lines
Quoted 78 lines
lib/alloc_tag.c new file +57
-0+ +static bool alloc_tag_module_unload (struct codetag_type *cttype, +struct codetag_module *cmod) +{ +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; + +for (ct =codetag_next_ct (&iter); ct; ct =codetag_next_ct (&iter)) { +if (iter.cmod != cmod) +continue ; + + tag =ct_to_alloc_tag (ct); + counter =alloc_tag_read (tag); + +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; + } + +return module_unused; +} + +static struct ctl_table memory_allocation_profiling_sysctls[] = { + { + .procname ="mem_profiling" , + .data = &mem_alloc_profiling_key, +#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG + .mode =0444 , +#else + .mode =0644 , +#endif + .proc_handler = proc_do_static_key, + }, + { } +}; + +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, + }; + + alloc_tag_cttype =codetag_register_type (&desc); +if (IS_ERR_OR_NULL (alloc_tag_cttype)) +return PTR_ERR (alloc_tag_cttype); + +register_sysctl_init ("vm" , memory_allocation_profiling_sysctls); +procfs_init (); + +return 0 ; +} +module_init (alloc_tag_init);scripts/module.lds.S +3
-0@@ -9,6 +9,8 @@ #define DISCARD_EH_FRAME *(.eh_frame) #endif +#include <asm-generic/codetag.lds.h> + SECTIONS { /DISCARD/ : { *(.discard)@@ -47,12 +49,17 @@ SECTIONS { .data : { *(.data .data.[0-9a-zA-Z_]*) *(.data..L*) + CODETAG_SECTIONS() }
Quoted 12 lines
scripts/module.lds.S SECTIONS { · line 64
Quoted 10 lines
scripts/module.lds.S +4
-0.rodata : { *(.rodata .rodata.[0-9a-zA-Z_]*) *(.rodata..L*) } +#else + .data : { + CODETAG_SECTIONS() + } #endif }Otherwise, looks good.
Thanks!
Quoted 3 lines
--
Kees Cook
Suren Baghdasaryan
maintainerRe: [PATCH v3 10/35] lib: code tagging framework
In reply to Kees CookQuoted 233 lines · Kees Cook
Quoted 216 lines · Suren Baghdasaryan
Add basic infrastructure to support code tagging which stores tag common information consisting of the module name, function, file name and line number. Provide functions to register a new code tag type and navigate between code tags.
Co-developed-by Kent Overstreet <[email protected]>Signed-off-by Kent Overstreet <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>--- include/linux/codetag.h | 71 ++++++++++++++ lib/Kconfig.debug | 4 + lib/Makefile | 1 + lib/codetag.c | 199 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 275 insertions(+) create mode 100644 include/linux/codetag.h create mode 100644 lib/codetag.cinclude/linux/codetag.h new file +71
-0@@ -0,0 +1,71 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* code tagging framework +*/ +#ifndef _LINUX_CODETAG_H +#define _LINUX_CODETAG_H + +#include <linux/types.h> + +struct codetag_iterator ; +struct codetag_type ; +struct seq_buf ; +struct module ; + +/* +* 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 +* an array of these. +*/ +struct codetag { +unsigned int flags;/* used in later patches */ +unsigned int lineno; +const char *modname; +const char *function; +const char *filename; +}__aligned(8) ; + +union codetag_ref { +struct codetag *ct; +}; + +struct codetag_range { +struct codetag *start; +struct codetag *stop; +}; + +struct codetag_module { +struct module *mod; +struct codetag_range range; +}; + +struct codetag_type_desc { +const char *section; +size_t tag_size; +}; + +struct codetag_iterator { +struct codetag_type *cttype; +struct codetag_module *cmod; +unsigned long mod_id; +struct codetag *ct; +}; + +#define CODE_TAG_INIT { \ + .modname = KBUILD_MODNAME, \ + .function = __func__, \ + .filename = __FILE__, \ + .lineno = __LINE__, \ + .flags = 0, \ +} + +void codetag_lock_module_list (struct codetag_type *cttype,bool lock); +struct codetag_iterator codetag_get_ct_iter (struct codetag_type *cttype); +struct codetag *codetag_next_ct (struct codetag_iterator *iter); + +void codetag_to_text (struct seq_buf *out,struct codetag *ct); + +struct codetag_type * +codetag_register_type (const struct codetag_type_desc *desc); + +#endif /* _LINUX_CODETAG_H */ lib/Kconfig.debug +4
-0@@ -968,6 +968,10 @@ config DEBUG_STACKOVERFLOW If in doubt, say "N". +config CODE_TAGGING + bool + select KALLSYMS + source "lib/Kconfig.kasan" source "lib/Kconfig.kfence" source "lib/Kconfig.kmsan"lib/Makefile +1
-0@@ -235,6 +235,7 @@ obj-$(CONFIG_OF_RECONFIG_NOTIFIER_ERROR_INJECT) += \ of-reconfig-notifier-error-inject.o obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o +obj-$(CONFIG_CODE_TAGGING) += codetag.o lib-$(CONFIG_GENERIC_BUG) += bug.o obj-$(CONFIG_HAVE_ARCH_TRACEHOOK) += syscall.olib/codetag.c new file +89
-0@@ -0,0 +1,199 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <linux/codetag.h> +#include <linux/idr.h> +#include <linux/kallsyms.h> +#include <linux/module.h> +#include <linux/seq_buf.h> +#include <linux/slab.h> + +struct codetag_type { +struct list_head link; +unsigned int count; +struct idr mod_idr; +struct rw_semaphore mod_lock;/* protects mod_idr */ +struct codetag_type_desc desc; +}; + +static DEFINE_MUTEX (codetag_lock); +static LIST_HEAD (codetag_types); + +void codetag_lock_module_list (struct codetag_type *cttype,bool lock) +{ +if (lock) +down_read (&cttype->mod_lock); +else +up_read (&cttype->mod_lock); +} + +struct codetag_iterator codetag_get_ct_iter (struct codetag_type *cttype) +{ +struct codetag_iterator iter = { + .cttype = cttype, + .cmod =NULL , + .mod_id =0 , + .ct =NULL , + }; + +return iter; +} + +static inline struct codetag *get_first_module_ct (struct codetag_module *cmod) +{ +return cmod->range.start < cmod->range.stop ? cmod->range.start :NULL ; +} + +static inline +struct codetag *get_next_module_ct (struct codetag_iterator *iter) +{ +struct codetag *res = (struct codetag *) + ((char *)iter->ct + iter->cttype->desc.tag_size); + +return res < iter->cmod->range.stop ? res :NULL ; +} + +struct codetag *codetag_next_ct (struct codetag_iterator *iter) +{ +struct codetag_type *cttype = iter->cttype; +struct codetag_module *cmod; +struct codetag *ct; + +lockdep_assert_held (&cttype->mod_lock); + +if (unlikely (idr_is_empty (&cttype->mod_idr))) +return NULL ; + + ct =NULL ; +while (true) { + cmod =idr_find (&cttype->mod_idr, iter->mod_id); + +/* If module was removed move to the next one */ +if (!cmod) + cmod =idr_get_next_ul (&cttype->mod_idr, + &iter->mod_id); + +/* Exit if no more modules */ +if (!cmod) +break ; + +if (cmod != iter->cmod) { + iter->cmod = cmod; + ct =get_first_module_ct (cmod); + }else + ct =get_next_module_ct (iter); + +if (ct) +break ; + + iter->mod_id++; + } +lib/codetag.c line 99
Quoted 10 lines
lib/codetag.c new file +10
-0+ iter->ct = ct; +return ct; +} + +void codetag_to_text (struct seq_buf *out,struct codetag *ct) +{ +seq_buf_printf (out,"%s:%u module:%s func:%s" , + ct->filename, ct->lineno, + ct->modname, ct->function); +}Thank you for using
seq_bufhere!Also, will this need an
EXPORT_SYMBOL_GPL()?
Quoted 14 lines
lib/codetag.c line 110
Quoted 11 lines
lib/codetag.c new file +11
-0+ +static inline size_t range_size (const struct codetag_type *cttype, +const struct codetag_range *range) +{ +return ((char *)range->stop - (char *)range->start) / + cttype->desc.tag_size; +} + +static void *get_symbol (struct module *mod,const char *prefix,const char *name) +{ +char buf[64 ];Why is 64 enough? I was expecting
KSYM_NAME_LENhere, but perhaps this is specialized enough to section names that it will not be a problem?
This buffer is being used to hold the name of the section containing codetags appended with "__start_" or "__stop_" and the only current user is alloc_tag_init() which sets the section name to "alloc_tags". So, this buffer currently holds either "alloc_tags__start_" or "alloc_tags__stop_". When more codetag applications are added (like the ones we have shown in the original RFC [1]), there would be more section names. 64 was chosen as a big enough value to reasonably hold the section name with the suffix. But you are right, we should add a check for the section name size to ensure it always fits. Will add into my TODO list.
[1] https://lore.kernel.org/all/[email protected]/
Quoted 1 line
If so, please document it clearly with a comment.
Will do.
Quoted 10 lines
lib/codetag.c line 115
Quoted 6 lines
lib/codetag.c new file +5
-0+int res; + + res =snprintf (buf,sizeof (buf),"%s%s" , prefix, name); +if (WARN_ON (res <1 || res >sizeof (buf))) +return NULL ;Please use a
seq_bufhere instead of snprintf, which we're trying to get rid of.
Quoted 8 lines
DECLARE_SEQ_BUF(sb, KSYM_NAME_LEN);
char *buf;
seq_buf_printf(sb, "%s%s", prefix, name);
if (seq_buf_has_overflowed(sb))
return NULL;
buf = seq_buf_str(sb);Will do. Thanks!
Quoted 91 lines
Quoted 86 lines
lib/codetag.c new file +84
-1+ +return mod ? + (void *)find_kallsyms_symbol_value (mod, buf) : + (void *)kallsyms_lookup_name (buf); +} + +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), + }; +} + +static int codetag_module_init (struct codetag_type *cttype,struct module *mod) +{ +struct codetag_range range; +struct codetag_module *cmod; +int err; + + range =get_section_range (mod, cttype->desc.section); +if (!range.start || !range.stop) { +pr_warn ("Failed to load code tags of type %s from the module %s\n" , + cttype->desc.section, + mod ? mod->name :"(built-in)" ); +return -EINVAL ; + } + +/* Ignore empty ranges */ +if (range.start == range.stop) +return 0 ; + +BUG_ON (range.start > range.stop); + + cmod =kmalloc (sizeof (*cmod),GFP_KERNEL ); +if (unlikely (!cmod)) +return -ENOMEM ; + + cmod->mod = mod; + cmod->range = range; + +down_write (&cttype->mod_lock); + err =idr_alloc (&cttype->mod_idr, cmod,0 ,0 ,GFP_KERNEL ); +if (err >=0 ) + cttype->count +=range_size (cttype, &range); +up_write (&cttype->mod_lock); + +if (err <0 ) { +kfree (cmod); +return err; + } + +return 0 ; +} + +struct codetag_type * +codetag_register_type (const struct codetag_type_desc *desc) +{ +struct codetag_type *cttype; +int err; + +BUG_ON (desc->tag_size <=0 ); + + cttype =kzalloc (sizeof (*cttype),GFP_KERNEL ); +if (unlikely (!cttype)) +return ERR_PTR (-ENOMEM ); + + cttype->desc = *desc; +idr_init (&cttype->mod_idr); +init_rwsem (&cttype->mod_lock); + + err =codetag_module_init (cttype,NULL ); +if (unlikely (err)) { +kfree (cttype); +return ERR_PTR (err); + } + +mutex_lock (&codetag_lock); +list_add_tail (&cttype->link, &codetag_types); +mutex_unlock (&codetag_lock); + +return cttype; +} --Quoted 2 lines
2.43.0.687.g38aa6559b0-goog
--
Kees Cook
Kent Overstreet
maintainerRe: [PATCH v3 23/35] mm/slub: Mark slab_free_freelist_hook() __always_inline
In reply to Kees CookQuoted 6 lines · Kees Cook
Quoted 3 lines · Suren Baghdasaryan
From: Kent Overstreet <[email protected]>
It seems we need to be more forceful with the compiler on this one.
Sure, but why?
Wasn't getting inlined without it, and that's one we do want inlined - it's only called in one place.
Suren Baghdasaryan
maintainerRe: [PATCH v3 05/35] mm: introduce slabobj_ext to support slab object extensions
In reply to Kees CookQuoted 8 lines · Kees Cook
Quoted 6 lines · Suren Baghdasaryan
Currently slab pages can store only vectors of
obj_cgrouppointers inpage->memcg_data. Introduceslabobj_extstructure to allow more data to be stored for each slab object. Wrapobj_cgroupintoslabobj_extto support current functionality while allowing to extendslabobj_extin the future.
Quoted 6 lines
Quoted 1 line
Signed-off-by Suren Baghdasaryan <[email protected]>It looks like this doesn't change which buckets
GFP_KERNEL_ACCOUNTcomes out of, is that correct? I'd love it if we didn't have separate buckets soGFP_KERNELandGFP_KERNEL_ACCOUNTcame from the same pools (so that the randomized pools would coverGFP_KERNEL_ACCOUNT...)
This should not affect KMEM accounting in any way. We are simply changing the vector of obj_cgroup objects to hold complex objects which can contain more fields in addition to the original obj_cgroup (in our case it's the codetag reference).
Unless I misunderstood your question?
Quoted 7 lines
Regardless:
Reviewed-by Kees Cook <[email protected]>--
Kees Cook
Kent Overstreet
maintainerRe: [PATCH v3 31/35] lib: add memory allocations report in show_mem()
In reply to Steven RostedtQuoted 11 lines · Steven Rostedt
Quoted 7 lines · On Mon, 12 Feb 2024 16:10:02 -0800 Kees Cook <[email protected]> wrote:
mm/show_mem.c void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) · line 430
Quoted 5 lines
#endif
mm/show_mem.c +4
-0+#ifdef CONFIG_MEM_ALLOC_PROFILING + { +struct seq_buf s; +char *buf =kmalloc (4096 ,GFP_ATOMIC );Why 4096? Maybe use
PAGE_SIZEinstead?
Quoted 2 lines
Will it make a difference for architectures that don't have 4096
PAGE_SIZE? Like PowerPC which hasPAGE_SIZEof anywhere between 4K to 256K!
it's just a string buffer
Suren Baghdasaryan
maintainerRe: [PATCH v3 31/35] lib: add memory allocations report in show_mem()
Quoted 11 lines · Kent Overstreet
Quoted 9 lines · Steven Rostedt
Quoted 6 lines · On Mon, 12 Feb 2024 16:10:02 -0800 Kees Cook <[email protected]> wrote:
Quoted 5 lines
#endif
mm/show_mem.c +4
-0+#ifdef CONFIG_MEM_ALLOC_PROFILING + { +struct seq_buf s; +char *buf =kmalloc (4096 ,GFP_ATOMIC );
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Why 4096? Maybe use
PAGE_SIZEinstead?Will it make a difference for architectures that don't have 4096
PAGE_SIZE? Like PowerPC which hasPAGE_SIZEof anywhere between 4K to 256K!it's just a string buffer
We should document that __show_mem() prints only the top 10 largest allocations, therefore as long as this buffer is large enough to hold 10 records we should be good. Technically we could simply print one record at a time and then the buffer can be smaller.
Re: [PATCH v3 01/35] lib/string_helpers: Add flags param to string_get_size()
In reply to Suren BaghdasaryanQuoted 11 lines · Suren Baghdasaryan
From: Kent Overstreet <[email protected]>
The new flags parameter allows controlling
- Whether or not the units suffix is separated by a space, for compatibility with sort -h
- Whether or not to append a B suffix - we're not always printing bytes.Signed-off-by Kent Overstreet <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>
It seems most of my points from the previous review were refused...
...
You can move the below under --- cutter, so it won't pollute the git history.
Quoted 9 lines
Cc Andy Shevchenko <[email protected]>Cc Michael Ellerman <[email protected]>Cc Benjamin Herrenschmidt <[email protected]>Cc Paul Mackerras <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Cc Jason Wang <[email protected]>Cc "Noralf Trønnes" <[email protected]>Cc Jens Axboe <[email protected]>---
...
Quoted 3 lines
include/linux/string_helpers.h +0
-0@@ -17,14 +17,13 @@ static inline bool string_is_terminated(const char *s, int len)
...
include/linux/string_helpers.h static inline bool string_is_terminated(const char *s, int len) · line 24
Quoted 10 lines
include/linux/string_helpers.h +4
-5-/* Descriptions of the types of units to -* print in */ -enum string_size_units { -STRING_UNITS_10 ,/* use powers of 10^3 (standard SI) */ -STRING_UNITS_2 ,/* use binary powers of 2^10 */ +enum string_size_flags { +STRING_SIZE_BASE2 = (1 <<0 ), +STRING_SIZE_NOSPACE = (1 <<1 ), +STRING_SIZE_NOBYTES = (1 <<2 ), };
Do not kill documentation, I already said that. Or i.o.w. document this. Also the _SIZE is ambigous (if you don't want UNITS, use SIZE_FORMAT.
Also why did you kill BASE10 here? (see below as well)
...
lib/string_helpers.c line 25
Quoted 10 lines
lib/string_helpers.c +4
-0@@ -19,11 +19,17 @@#include <linux/string.h> #include <linux/string_helpers.h> +enum string_size_units { +STRING_UNITS_10 ,/* use powers of 10^3 (standard SI) */ +STRING_UNITS_2 ,/* use binary powers of 2^10 */ +};
Why do we need this duplication?
...
lib/string_helpers.c line 47
Quoted 2 lines
lib/string_helpers.c +2
-0+enum string_size_units units = flags & flags &STRING_SIZE_BASE2 + ?STRING_UNITS_2 :STRING_UNITS_10 ;
Double flags check is redundant.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v3 01/35] lib/string_helpers: Add flags param to string_get_size()
Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Suren Baghdasaryan
From: Kent Overstreet <[email protected]>
The new flags parameter allows controlling
- Whether or not the units suffix is separated by a space, for compatibility with sort -h
- Whether or not to append a B suffix - we're not always printing bytes.
And you effectively missed to _add_ the test cases for the modified code. Formal NAK for this, the rest is discussable, the absence of tests is not.
Quoted 64 lines
Quoted 2 lines
Signed-off-by Kent Overstreet <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>It seems most of my points from the previous review were refused...
...
You can move the below under --- cutter, so it won't pollute the git history.
Quoted 9 lines
Cc Andy Shevchenko <[email protected]>Cc Michael Ellerman <[email protected]>Cc Benjamin Herrenschmidt <[email protected]>Cc Paul Mackerras <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Cc Jason Wang <[email protected]>Cc "Noralf Trønnes" <[email protected]>Cc Jens Axboe <[email protected]>---
...
Quoted 3 lines
include/linux/string_helpers.h +0
-0@@ -17,14 +17,13 @@ static inline bool string_is_terminated(const char *s, int len)...
include/linux/string_helpers.h static inline bool string_is_terminated(const char *s, int len) · line 24
Quoted 10 lines
include/linux/string_helpers.h +4
-5-/* Descriptions of the types of units to -* print in */ -enum string_size_units { -STRING_UNITS_10 ,/* use powers of 10^3 (standard SI) */ -STRING_UNITS_2 ,/* use binary powers of 2^10 */ +enum string_size_flags { +STRING_SIZE_BASE2 = (1 <<0 ), +STRING_SIZE_NOSPACE = (1 <<1 ), +STRING_SIZE_NOBYTES = (1 <<2 ), };Do not kill documentation, I already said that. Or i.o.w. document this. Also the
_SIZEis ambigous (if you don't want UNITS, useSIZE_FORMAT.Also why did you kill BASE10 here? (see below as well)
...
lib/string_helpers.c line 25
Quoted 10 lines
lib/string_helpers.c +4
-0@@ -19,11 +19,17 @@#include <linux/string.h> #include <linux/string_helpers.h> +enum string_size_units { +STRING_UNITS_10 ,/* use powers of 10^3 (standard SI) */ +STRING_UNITS_2 ,/* use binary powers of 2^10 */ +};Why do we need this duplication?
...
lib/string_helpers.c line 47
Quoted 2 lines
lib/string_helpers.c +2
-0+enum string_size_units units = flags & flags &STRING_SIZE_BASE2 + ?STRING_UNITS_2 :STRING_UNITS_10 ;Double flags check is redundant.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Michal Hocko
reviewerOn Mon 12-02-24 13:38:46, Suren Baghdasaryan wrote:
[...]
Quoted 4 lines
We're aiming to get this in the next merge window, for 6.9. The feedback we've gotten has been that even out of tree this patchset has already been useful, and there's a significant amount of other work gated on the code tagging functionality included in this patchset [2].
I suspect it will not come as a surprise that I really dislike the implementation proposed here. I will not repeat my arguments, I have done so on several occasions already.
Anyway, I didn't go as far as to nak it even though I _strongly_ believe this debugging feature will add a maintenance overhead for a very long time. I can live with all the downsides of the proposed implementation _as long as_ there is a wider agreement from the MM community as this is where the maintenance cost will be payed. So far I have not seen (m)any acks by MM developers so aiming into the next merge window is more than little rushed.
Quoted 1 line
81 files changed, 2126 insertions(+), 695 deletions(-)
Signature · 2 lines
Michal Hocko SUSE Labs
kernel test robot
build botRe: [PATCH v3 20/35] lib: add codetag reference into slabobj_ext
In reply to Suren Baghdasaryankernel test robot noticed the following build warnings:
Hi Suren,
kernel test robot noticed the following build warnings:
[auto build test WARNING on akpm-mm/mm-nonmm-unstable]
[also build test WARNING on linus/master v6.8-rc4]
[cannot apply to akpm-mm/mm-everything vbabka-slab/for-next next-20240213] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Suren-Baghdasaryan/lib-string_helpers-Add-flags-param-to-string_get_size/20240213-054335 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-nonmm-unstable patch link: https://lore.kernel.org/r/20240212213922.783301-21-surenb%40google.com patch subject: [PATCH v3 20/35] lib: add codetag reference into slabobj_ext config: alpha-kismet-CONFIG_SLAB_OBJ_EXT-CONFIG_MEM_ALLOC_PROFILING-0-0 (https://download.01.org/0day-ci/archive/20240214/[email protected]/config) reproduce: (https://download.01.org/0day-ci/archive/20240214/[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]/
kismet warnings: (new ones prefixed by >>)
>> kismet: WARNING: unmet direct dependencies detected for SLAB_OBJ_EXT when selected by MEM_ALLOC_PROFILING
.config:5:warning: symbol value 'n' invalid for PANEL_LCD_PIN_E .config:352:warning: symbol value 'n' invalid for PSTORE_BLK_MAX_REASON .config:402:warning: symbol value 'n' invalid for AIC79XX_DEBUG_MASK .config:439:warning: symbol value 'n' invalid for KFENCE_SAMPLE_INTERVAL .config:629:warning: symbol value 'n' invalid for DRM_XE_JOB_TIMEOUT_MIN .config:653:warning: symbol value 'n' invalid for CRYPTO_DEV_QCE_SW_MAX_LEN .config:764:warning: symbol value 'n' invalid for PANEL_LCD_CHARSET .config:809:warning: symbol value 'n' invalid for SND_AC97_POWER_SAVE_DEFAULT .config:836:warning: symbol value 'n' invalid for AIC79XX_CMDS_PER_DEVICE .config:860:warning: symbol value 'n' invalid for MAGIC_SYSRQ_DEFAULT_ENABLE .config:875:warning: symbol value 'n' invalid for DRM_I915_MAX_REQUEST_BUSYWAIT .config:905:warning: symbol value 'n' invalid for PANEL_LCD_PIN_SDA .config:914:warning: symbol value 'n' invalid for SND_AT73C213_TARGET_BITRATE .config:918:warning: symbol value 'n' invalid for DRM_XE_PREEMPT_TIMEOUT_MIN .config:932:warning: symbol value 'n' invalid for VMCP_CMA_SIZE .config:1037:warning: symbol value 'n' invalid for AIC79XX_RESET_DELAY_MS .config:1195:warning: symbol value 'n' invalid for MTDRAM_ERASE_SIZE .config:1307:warning: symbol value 'n' invalid for SERIAL_UARTLITE_NR_UARTS .config:1486:warning: symbol value 'n' invalid for LEGACY_PTY_COUNT .config:1628:warning: symbol value 'n' invalid for MTD_REDBOOT_DIRECTORY_BLOCK .config:1634:warning: symbol value 'n' invalid for AIC7XXX_RESET_DELAY_MS .config:1638:warning: symbol value 'n' invalid for WATCHDOG_OPEN_TIMEOUT .config:1790:warning: symbol value 'n' invalid for IBM_EMAC_POLL_WEIGHT .config:1932:warning: symbol value 'n' invalid for DRM_I915_STOP_TIMEOUT .config:2209:warning: symbol value 'n' invalid for SERIAL_AR933X_NR_UARTS .config:2357:warning: symbol value 'n' invalid for KCOV_IRQ_AREA_SIZE .config:2369:warning: symbol value 'n' invalid for PANEL_LCD_BWIDTH .config:2382:warning: symbol value 'n' invalid for DRM_XE_TIMESLICE_MAX .config:2696:warning: symbol value 'n' invalid for NOUVEAU_DEBUG_DEFAULT .config:2888:warning: symbol value 'n' invalid for KCSAN_REPORT_ONCE_IN_MS .config:2890:warning: symbol value 'n' invalid for FTRACE_RECORD_RECURSION_SIZE .config:2960:warning: symbol value 'n' invalid for SND_SOC_SOF_DEBUG_IPC_FLOOD_TEST_NUM .config:2987:warning: symbol value 'n' invalid for KCSAN_UDELAY_INTERRUPT .config:3012:warning: symbol value 'n' invalid for PANEL_LCD_PIN_BL .config:3031:warning: symbol value 'n' invalid for DEBUG_OBJECTS_ENABLE_DEFAULT .config:3038:warning: symbol value 'n' invalid for INITRAMFS_ROOT_GID .config:3144:warning: symbol value 'n' invalid for ATM_FORE200E_TX_RETRY .config:3186:warning: symbol value 'n' invalid for FB_OMAP2_DSS_MIN_FCK_PER_PCK .config:3327:warning: symbol value 'n' invalid for PSTORE_BLK_CONSOLE_SIZE .config:3441:warning: symbol value 'n' invalid for BOOKE_WDT_DEFAULT_TIMEOUT .config:3501:warning: symbol value 'n' invalid for KCSAN_UDELAY_TASK .config:3530:warning: symbol value 'n' invalid for MMC_BLOCK_MINORS .config:3576:warning: symbol value 'n' invalid for SCSI_NCR53C8XX_SYNC .config:3647:warning: symbol value 'n' invalid for IP_VS_MH_TAB_INDEX .config:3699:warning: symbol value 'n' invalid for UCLAMP_BUCKETS_COUNT .config:3849:warning: symbol value 'n' invalid for SERIAL_MCF_BAUDRATE .config:3912:warning: symbol value 'n' invalid for DE2104X_DSL .config:3928:warning: symbol value 'n' invalid for BLK_DEV_RAM_COUNT .config:4043:warning: symbol value 'n' invalid for STACK_MAX_DEFAULT_SIZE_MB .config:4155:warning: symbol value 'n' invalid for PANEL_LCD_PIN_RW .config:4188:warning: symbol value 'n' invalid for IP_VS_SH_TAB_BITS .config:4242:warning: symbol value 'n' invalid for CMA_AREAS .config:4299:warning: symbol value 'n' invalid for INPUT_MOUSEDEV_SCREEN_X .config:4361:warning: symbol value 'n' invalid for USBIP_VHCI_HC_PORTS .config:4419:warning: symbol value 'n' invalid for RIONET_RX_SIZE .config:4686:warning: symbol value 'n' invalid for RADIO_TYPHOON_PORT .config:4733:warning: symbol value 'n' invalid for IBM_EMAC_TXB .config:4816:warning: symbol value 'n' invalid for SERIAL_TXX9_NR_UARTS .config:5003:warning: symbol value 'n' invalid for RCU_BOOST_DELAY .config:5126:warning: symbol value 'n' invalid for ARCH_MMAP_RND_BITS .config:5208:warning: symbol value 'n' invalid for DRM_I915_FENCE_TIMEOUT .config:5229:warning: symbol value 'n' invalid for TTY_PRINTK_LEVEL .config:5247:warning: symbol value 'n' invalid for XEN_MEMORY_HOTPLUG_LIMIT .config:5393:warning: symbol value 'n' invalid for MIPS_EJTAG_FDC_KGDB_CHAN .config:5489:warning: symbol value 'n' invalid for KDB_DEFAULT_ENABLE .config:5508:warning: symbol value 'n' invalid for SERIAL_ALTERA_UART_MAXPORTS .config:5548:warning: symbol value 'n' invalid for DRM_XE_PREEMPT_TIMEOUT_MAX .config:5702:warning: symbol value 'n' invalid for PPC_EARLY_DEBUG_EHV_BC_HANDLE .config:5772:warning: symbol value 'n' invalid for PANEL_LCD_HWIDTH .config:5806:warning: symbol value 'n' invalid for LOCKDEP_CHAINS_BITS .config:5901:warning: symbol value 'n' invalid for DRM_I915_HEARTBEAT_INTERVAL .config:5907:warning: symbol value 'n' invalid for KCSAN_SKIP_WATCH .config:5930:warning: symbol value 'n' invalid for PSTORE_BLK_KMSG_SIZE .config:6105:warning: symbol value 'n' invalid for VIDEO_VIVID_MAX_DEVS .config:6151:warning: symbol value 'n' invalid for SCSI_SYM53C8XX_MAX_TAGS .config:6166:warning: symbol value 'n' invalid for CRYPTO_DEV_FSL_CAAM_INTC_TIME_THLD .config:6232:warning: symbol value 'n' invalid for ARCH_MMAP_RND_COMPAT_BITS .config:6268:warning: symbol value 'n' invalid for MTD_UBI_WL_THRESHOLD .config:6301:warning: symbol value 'n' invalid for RIONET_TX_SIZE .config:6402:warning: symbol value 'n' invalid for RADIO_TRUST_PORT .config:6419:warning: symbol value 'n' invalid for SCSI_MESH_RESET_DELAY_MS .config:6548:warning: symbol value 'n' invalid for SERIAL_SH_SCI_NR_UARTS .config:6667:warning: symbol value 'n' invalid for OMAP2_DSS_MIN_FCK_PER_PCK .config:6779:warning: symbol value 'n' invalid for CMA_SIZE_PERCENTAGE .config:6924:warning: symbol value 'n' invalid for DRM_XE_TIMESLICE_MIN .config:7101:warning: symbol value 'n' invalid for SCSI_NCR53C8XX_MAX_TAGS .config:7104:warning: symbol value 'n' invalid for DVB_MAX_ADAPTERS .config:7118:warning: symbol value 'n' invalid for SCSI_SYM53C8XX_DMA_ADDRESSING_MODE .config:7302:warning: symbol value 'n' invalid for KDB_CONTINUE_CATASTROPHIC .config:7405:warning: symbol value 'n' invalid for SERIAL_ARC_NR_PORTS .config:7500:warning: symbol value 'n' invalid for IBM_EMAC_RXB .config:7579:warning: symbol value 'n' invalid for SCSI_MPT3SAS_MAX_SGE .config:7663:warning: symbol value 'n' invalid for ZSMALLOC_CHAIN_SIZE .config:7711:warning: symbol value 'n' invalid for PSTORE_DEFAULT_KMSG_BYTES .config:7716:warning: symbol value 'n' invalid for IIO_CONSUMERS_PER_TRIGGER .config:7903:warning: symbol value 'n' invalid for PANEL_LCD .config:7981:warning: symbol value 'n' invalid for LOCKDEP_BITS .config:8107:warning: symbol value 'n' invalid for CRYPTO_DEV_FSL_CAAM_RINGSIZE .config:8197:warning: symbol value 'n' invalid for MTDRAM_TOTAL_SIZE .config:8499:warning: symbol value 'n' invalid for LSM_MMAP_MIN_ADDR
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Suren Baghdasaryan
maintainerQuoted 12 lines · Michal Hocko
On Mon 12-02-24 13:38:46, Suren Baghdasaryan wrote:
[...]Quoted 4 lines
We're aiming to get this in the next merge window, for 6.9. The feedback we've gotten has been that even out of tree this patchset has already been useful, and there's a significant amount of other work gated on the code tagging functionality included in this patchset [2].
I suspect it will not come as a surprise that I really dislike the implementation proposed here. I will not repeat my arguments, I have done so on several occasions already.
Quoted 7 lines
Anyway, I didn't go as far as to nak it even though I _strongly_ believe this debugging feature will add a maintenance overhead for a very long time. I can live with all the downsides of the proposed implementation
_aslongas_there is a wider agreement from the MM community as this is where the maintenance cost will be payed. So far I have not seen (m)any acks by MM developers so aiming into the next merge window is more than little rushed.
We tried other previously proposed approaches and all have their downsides without making maintenance much easier. Your position is understandable and I think it's fair. Let's see if others see more benefit than cost here.
Thanks,
Suren.
Quoted 5 lines
Quoted 2 lines
81 files changed, 2126 insertions(+), 695 deletions(-)--
Michal Hocko
SUSE Labs
Quoted 21 lines · Suren Baghdasaryan
Quoted 12 lines · Michal Hocko
On Mon 12-02-24 13:38:46, Suren Baghdasaryan wrote:
[...]Quoted 4 lines
We're aiming to get this in the next merge window, for 6.9. The feedback we've gotten has been that even out of tree this patchset has already been useful, and there's a significant amount of other work gated on the code tagging functionality included in this patchset [2].
I suspect it will not come as a surprise that I really dislike the implementation proposed here. I will not repeat my arguments, I have done so on several occasions already.
Quoted 7 lines
Anyway, I didn't go as far as to nak it even though I _strongly_ believe this debugging feature will add a maintenance overhead for a very long time. I can live with all the downsides of the proposed implementation
_aslongas_there is a wider agreement from the MM community as this is where the maintenance cost will be payed. So far I have not seen (m)any acks by MM developers so aiming into the next merge window is more than little rushed.
Quoted 4 lines
We tried other previously proposed approaches and all have their downsides without making maintenance much easier. Your position is understandable and I think it's fair. Let's see if others see more benefit than cost here.
Would it make sense to discuss that at LSF/MM once again, especially covering why proposed alternatives did not work out? LSF/MM is not "too far" away (May).
I recall that the last LSF/MM session on this topic was a bit unfortunate (IMHO not as productive as it could have been). Maybe we can finally reach a consensus on this.
Signature · 3 lines
Cheers, David / dhildenb
Kent Overstreet
maintainerRe: [PATCH v3 01/35] lib/string_helpers: Add flags param to string_get_size()
In reply to Andy ShevchenkoQuoted 49 lines · Andy Shevchenko
Quoted 11 lines · Suren Baghdasaryan
From: Kent Overstreet <[email protected]>
The new flags parameter allows controlling
- Whether or not the units suffix is separated by a space, for compatibility with sort -h
- Whether or not to append a B suffix - we're not always printing bytes.Signed-off-by Kent Overstreet <[email protected]>Signed-off-by Suren Baghdasaryan <[email protected]>...
You can move the below under --- cutter, so it won't pollute the git history.
Quoted 9 lines
Cc Andy Shevchenko <[email protected]>Cc Michael Ellerman <[email protected]>Cc Benjamin Herrenschmidt <[email protected]>Cc Paul Mackerras <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Cc Jason Wang <[email protected]>Cc "Noralf Trønnes" <[email protected]>Cc Jens Axboe <[email protected]>---
...
Quoted 3 lines
include/linux/string_helpers.h +0
-0@@ -17,14 +17,13 @@ static inline bool string_is_terminated(const char *s, int len)...
include/linux/string_helpers.h static inline bool string_is_terminated(const char *s, int len) · line 24
Quoted 10 lines
include/linux/string_helpers.h +4
-5-/* Descriptions of the types of units to -* print in */ -enum string_size_units { -STRING_UNITS_10 ,/* use powers of 10^3 (standard SI) */ -STRING_UNITS_2 ,/* use binary powers of 2^10 */ +enum string_size_flags { +STRING_SIZE_BASE2 = (1 <<0 ), +STRING_SIZE_NOSPACE = (1 <<1 ), +STRING_SIZE_NOBYTES = (1 <<2 ), };Do not kill documentation, I already said that. Or i.o.w. document this. Also the
_SIZEis ambigous (if you don't want UNITS, useSIZE_FORMAT.
Quoted 1 line
Also why did you kill BASE10 here? (see below as well)
As you should be able to tell from the name, it's a set of flags.
Quoted 6 lines
Quoted 6 lines
lib/string_helpers.c +0
-0@@ -19,11 +19,17 @@#include <linux/string.h> #include <linux/string_helpers.h>
Quoted 6 lines
lib/string_helpers.c line 25
Quoted 4 lines
lib/string_helpers.c +4
-0+enum string_size_units { +STRING_UNITS_10 ,/* use powers of 10^3 (standard SI) */ +STRING_UNITS_2 ,/* use binary powers of 2^10 */ +};Why do we need this duplication?
Because otherwise a lot more code would have to change.
Quoted 2 lines
It seems most of my points from the previous review were refused...
Look, Andy, this is a pretty tiny part of the patchset, yet it's been eating up a pretty disproprortionate amount of time and your review feedback has been pretty unhelpful - asking for things to be broken up in ways that would not be bisectable, or (as here) re-asking the same things that I've already answered and that should've been obvious.
The code works. If you wish to complain about anything being broken, or if you can come up with anything more actionable than what you've got here, I will absolutely respond to that, but otherwise I'm just going to leave things where they sit.
Kent Overstreet
maintainerQuoted 27 lines · David Hildenbrand
Quoted 21 lines · Suren Baghdasaryan
Quoted 12 lines · Michal Hocko
On Mon 12-02-24 13:38:46, Suren Baghdasaryan wrote:
[...]Quoted 4 lines
We're aiming to get this in the next merge window, for 6.9. The feedback we've gotten has been that even out of tree this patchset has already been useful, and there's a significant amount of other work gated on the code tagging functionality included in this patchset [2].
I suspect it will not come as a surprise that I really dislike the implementation proposed here. I will not repeat my arguments, I have done so on several occasions already.
Quoted 7 lines
Anyway, I didn't go as far as to nak it even though I _strongly_ believe this debugging feature will add a maintenance overhead for a very long time. I can live with all the downsides of the proposed implementation
_aslongas_there is a wider agreement from the MM community as this is where the maintenance cost will be payed. So far I have not seen (m)any acks by MM developers so aiming into the next merge window is more than little rushed.Quoted 4 lines
We tried other previously proposed approaches and all have their downsides without making maintenance much easier. Your position is understandable and I think it's fair. Let's see if others see more benefit than cost here.
Quoted 7 lines
Would it make sense to discuss that at LSF/MM once again, especially covering why proposed alternatives did not work out? LSF/MM is not "too far" away (May).
I recall that the last LSF/MM session on this topic was a bit unfortunate (IMHO not as productive as it could have been). Maybe we can finally reach a consensus on this.
I'd rather not delay for more bikeshedding. Before agreeing to LSF I'd need to see a serious proposl - what we had at the last LSF was people jumping in with half baked alternative proposals that very much hadn't been thought through, and I see no need to repeat that.
Like I mentioned, there's other work gated on this patchset; if people want to hold this up for more discussion they better be putting forth something to discuss.
Quoted 36 lines · Kent Overstreet
Quoted 27 lines · David Hildenbrand
Quoted 21 lines · Suren Baghdasaryan
Quoted 12 lines · Michal Hocko
On Mon 12-02-24 13:38:46, Suren Baghdasaryan wrote:
[...]Quoted 4 lines
We're aiming to get this in the next merge window, for 6.9. The feedback we've gotten has been that even out of tree this patchset has already been useful, and there's a significant amount of other work gated on the code tagging functionality included in this patchset [2].
I suspect it will not come as a surprise that I really dislike the implementation proposed here. I will not repeat my arguments, I have done so on several occasions already.
Quoted 7 lines
Anyway, I didn't go as far as to nak it even though I _strongly_ believe this debugging feature will add a maintenance overhead for a very long time. I can live with all the downsides of the proposed implementation
_aslongas_there is a wider agreement from the MM community as this is where the maintenance cost will be payed. So far I have not seen (m)any acks by MM developers so aiming into the next merge window is more than little rushed.Quoted 4 lines
We tried other previously proposed approaches and all have their downsides without making maintenance much easier. Your position is understandable and I think it's fair. Let's see if others see more benefit than cost here.
Quoted 7 lines
Would it make sense to discuss that at LSF/MM once again, especially covering why proposed alternatives did not work out? LSF/MM is not "too far" away (May).
I recall that the last LSF/MM session on this topic was a bit unfortunate (IMHO not as productive as it could have been). Maybe we can finally reach a consensus on this.
Quoted 8 lines
I'd rather not delay for more bikeshedding. Before agreeing to LSF I'd need to see a serious proposl - what we had at the last LSF was people jumping in with half baked alternative proposals that very much hadn't been thought through, and I see no need to repeat that.
Like I mentioned, there's other work gated on this patchset; if people want to hold this up for more discussion they better be putting forth something to discuss.
I'm thinking of ways on how to achieve Michal's request: "as long as there is a wider agreement from the MM community". If we can achieve that without LSF, great! (a bi-weekly MM meeting might also be an option)
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v3 13/35] lib: add allocation tagging support for memory allocation profiling
In reply to Suren BaghdasaryanQuoted 7 lines · Suren Baghdasaryan
Quoted 6 lines · Kees Cook
Quoted 3 lines · Suren Baghdasaryan
Introduce
CONFIG_MEM_ALLOC_PROFILINGwhich provides definitions to easily instrument memory allocators. It registers an "alloc_tags" codetag type with/proc/allocinfointerface to output allocation tag information when
Quoted 6 lines
Quoted 2 lines
Please don't add anything new to the top-level /proc directory. This should likely live in /sys.
Ack. I'll find a more appropriate place for it then.
It just seemed like such generic information which would belong next to meminfo/zoneinfo and such...
Save yourself a cycle of "rework the whole fs interface only to have someone else tell you no" and put it in debugfs, not sysfs. Wrangling with debugfs is easier than all the macro-happy sysfs stuff; you don't have to integrate with the "device" model; and there is no 'one value per file' rule.
--D
Quoted 615 lines
Quoted 192 lines
Quoted 192 lines
the feature is enabled.
CONFIG_MEM_ALLOC_PROFILING_DEBUGis provided for debugging the memory allocation profiling instrumentation.
Memory allocation profiling can be enabled or disabled at runtime using/proc/sys/vm/mem_profilingsysctl whenCONFIG_MEM_ALLOC_PROFILING_DEBUG=n.CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULTenables memory allocation profiling by default.Signed-off-by Suren Baghdasaryan <[email protected]>Co-developed-by Kent Overstreet <[email protected]>Signed-off-by Kent Overstreet <[email protected]>--- Documentation/admin-guide/sysctl/vm.rst | 16 +++ Documentation/filesystems/proc.rst | 28 +++++ include/asm-generic/codetag.lds.h | 14 +++ include/asm-generic/vmlinux.lds.h | 3 + include/linux/alloc_tag.h | 133 ++++++++++++++++++++ include/linux/sched.h | 24 ++++ lib/Kconfig.debug | 25 ++++ lib/Makefile | 2 + lib/alloc_tag.c | 158 ++++++++++++++++++++++++ scripts/module.lds.S | 7 ++ 10 files changed, 410 insertions(+) create mode 100644 include/asm-generic/codetag.lds.h create mode 100644 include/linux/alloc_tag.h create mode 100644 lib/alloc_tag.cDocumentation/admin-guide/sysctl/vm.rst +16
-0@@ -43,6 +43,7 @@ Currently, these files are in /proc/sys/vm: - legacy_va_layout - lowmem_reserve_ratio - max_map_count +- mem_profiling (only if CONFIG_MEM_ALLOC_PROFILING=y) - memory_failure_early_kill - memory_failure_recovery - min_free_kbytes@@ -425,6 +426,21 @@ e.g., up to one or two maps per allocation. The default value is 65530. +mem_profiling +============== + +Enable memory profiling (when CONFIG_MEM_ALLOC_PROFILING=y) + +1: Enable memory profiling. + +0: Disabld memory profiling. + +Enabling memory profiling introduces a small performance overhead for all +memory allocations. + +The default value depends on CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT. + + memory_failure_early_kill: ==========================Documentation/filesystems/proc.rst +28
-0@@ -688,6 +688,7 @@ files are there, and which are missing. ============ =============================================================== File Content ============ =============================================================== + allocinfo Memory allocations profiling information apm Advanced power management info bootconfig Kernel command line obtained from boot config, and, if there were kernel parameters from the@@ -953,6 +954,33 @@ also be allocatable although a lot of filesystem metadata may have to be reclaimed to achieve this. +allocinfo +~~~~~~~ + +Provides information about memory allocations at all locations in the code +base. Each allocation in the code is identified by its source file, line +number, module and the function calling the allocation. The number of bytes +allocated at each location is reported. + +Example output. + +:: + + > cat /proc/allocinfo + + 153MiB mm/slub.c:1826 module:slub func:alloc_slab_page + 6.08MiB mm/slab_common.c:950 module:slab_common func:_kmalloc_order + 5.09MiB mm/memcontrol.c:2814 module:memcontrol func:alloc_slab_obj_exts + 4.54MiB mm/page_alloc.c:5777 module:page_alloc func:alloc_pages_exact + 1.32MiB include/asm-generic/pgalloc.h:63 module:pgtable func:__pte_alloc_one + 1.16MiB fs/xfs/xfs_log_priv.h:700 module:xfs func:xlog_kvmalloc + 1.00MiB mm/swap_cgroup.c:48 module:swap_cgroup func:swap_cgroup_prepare + 734KiB fs/xfs/kmem.c:20 module:xfs func:kmem_alloc + 640KiB kernel/rcu/tree.c:3184 module:tree func:fill_page_cache_func + 640KiB drivers/char/virtio_console.c:452 module:virtio_console func:alloc_buf + ... + + meminfo ~~~~~~~include/asm-generic/codetag.lds.h new file +14
-0@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __ASM_GENERIC_CODETAG_LDS_H +#define __ASM_GENERIC_CODETAG_LDS_H + +#define SECTION_WITH_BOUNDARIES (_name) \ + . = ALIGN(8); \ + __start_##_name = .; \ + KEEP(*(_name)) \ + __stop_##_name = .; + +#define CODETAG_SECTIONS () \ + SECTION_WITH_BOUNDARIES(alloc_tags) + +#endif /* __ASM_GENERIC_CODETAG_LDS_H */ include/asm-generic/vmlinux.lds.h +3
-0@@ -50,6 +50,8 @@* [__nosave_begin, __nosave_end] for the nosave data */ +#include <asm-generic/codetag.lds.h> +#ifndef LOAD_OFFSET #define LOAD_OFFSET 0#endif @@ -366,6 +368,7 @@ . = ALIGN(8); \ BOUNDED_SECTION_BY(__dyndbg_classes, ___dyndbg_classes) \ BOUNDED_SECTION_BY(__dyndbg, ___dyndbg) \ + CODETAG_SECTIONS() \ LIKELY_PROFILE() \ BRANCH_PROFILE() \TRACE_PRINTKS () \include/linux/alloc_tag.h new file +37
-0@@ -0,0 +1,133 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* allocation tagging +*/ +#ifndef _LINUX_ALLOC_TAG_H +#define _LINUX_ALLOC_TAG_H + +#include <linux/bug.h> +#include <linux/codetag.h> +#include <linux/container_of.h> +#include <linux/preempt.h> +#include <asm/percpu.h> +#include <linux/cpumask.h> +#include <linux/static_key.h> + +struct alloc_tag_counters { +u64 bytes; +u64 calls; +}; + +/* +* An instance of this structure is created in a special ELF section at every +* allocation callsite. At runtime, the special section is treated as +* an array of these. Embedded codetag utilizes codetag framework. +*/ +struct alloc_tag { +struct codetag ct; +struct alloc_tag_counters __percpu *counters; +}__aligned(8) ; + +#ifdef CONFIG_MEM_ALLOC_PROFILING + +static inline struct alloc_tag *ct_to_alloc_tag (struct codetag *ct) +{ + return container_of(ct, struct alloc_tag, ct); +} +Quoted 8 lines
include/linux/alloc_tag.h line 43
Quoted 6 lines
include/linux/alloc_tag.h new file +6
-0+#ifdef ARCH_NEEDS_WEAK_PER_CPU +/* +* When percpu variables are required to be defined as weak, static percpu +* variables can't be used inside a function (see comments for DECLARE_PER_CPU_SECTION). +*/ +#error "Memory allocation profiling is incompatible with ARCH_NEEDS_WEAK_PER_CPU"Is this enforced via
Kconfigas well? (Looks like only alpha and s390?)Unfortunately
ARCH_NEEDS_WEAK_PER_CPUis not aKconfigoption butCONFIG_DEBUG_FORCE_WEAK_PER_CPUis, so that one is handled viaKconfig(see "depends on !DEBUG_FORCE_WEAK_PER_CPU" in this patch). We have to avoid both cases because of this:
https://elixir.bootlin.com/linux/latest/source/include/linux/percpu-defs.h#L75,
so I'm trying to provide an informative error here.Quoted 99 lines
Quoted 99 lines
include/linux/alloc_tag.h new file +90
-0+#endif + +#define DEFINE_ALLOC_TAG (_alloc_tag, _old) \ + static DEFINE_PER_CPU(struct alloc_tag_counters, _alloc_tag_cntr); \ + static struct alloc_tag _alloc_tag __used __aligned(8) \ + __section("alloc_tags") = { \ + .ct = CODE_TAG_INIT, \ + .counters = &_alloc_tag_cntr }; \ + struct alloc_tag * __maybe_unused _old = alloc_tag_save(&_alloc_tag) + +DECLARE_STATIC_KEY_MAYBE (CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT , + mem_alloc_profiling_key); + +static inline bool mem_alloc_profiling_enabled (void ) +{ +return static_branch_maybe (CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT , + &mem_alloc_profiling_key); +} + +static inline struct alloc_tag_counters alloc_tag_read (struct alloc_tag *tag) +{ +struct alloc_tag_counters v = {0 ,0 }; +struct alloc_tag_counters *counter; +int cpu; + +for_each_possible_cpu (cpu) { + counter =per_cpu_ptr (tag->counters, cpu); + v.bytes += counter->bytes; + v.calls += counter->calls; + } + +return v; +} + +static inline void __alloc_tag_sub (union codetag_ref *ref,size_t bytes) +{ +struct alloc_tag *tag; + +#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG +WARN_ONCE (ref && !ref->ct,"alloc_tag was not set\n" ); +#endif +if (!ref || !ref->ct) +return ; + + tag =ct_to_alloc_tag (ref->ct); + +this_cpu_sub (tag->counters->bytes, bytes); +this_cpu_dec (tag->counters->calls); + + ref->ct =NULL ; +} + +static inline void alloc_tag_sub (union codetag_ref *ref,size_t bytes) +{ +__alloc_tag_sub (ref, bytes); +} + +static inline void alloc_tag_sub_noalloc (union codetag_ref *ref,size_t bytes) +{ +__alloc_tag_sub (ref, bytes); +} + +static inline void alloc_tag_add (union codetag_ref *ref,struct alloc_tag *tag,size_t bytes) +{ +#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG +WARN_ONCE (ref && ref->ct, +"alloc_tag was not cleared (got tag for %s:%u)\n" ,\ + ref->ct->filename, ref->ct->lineno); + +WARN_ONCE (!tag,"current->alloc_tag not set" ); +#endif +if (!ref || !tag) +return ; + + ref->ct = &tag->ct; +this_cpu_add (tag->counters->bytes, bytes); +this_cpu_inc (tag->counters->calls); +} + +#else + +#define DEFINE_ALLOC_TAG (_alloc_tag, _old) +static inline void alloc_tag_sub (union codetag_ref *ref,size_t bytes) {} +static inline void alloc_tag_sub_noalloc (union codetag_ref *ref,size_t bytes) {} +static inline void alloc_tag_add (union codetag_ref *ref,struct alloc_tag *tag, +size_t bytes) {} + +#endif + +#endif /* _LINUX_ALLOC_TAG_H */ include/linux/sched.h +0
-0@@ -770,6 +770,10 @@ struct task_struct {unsigned int flags;unsigned int ptrace;Quoted 7 lines
include/linux/sched.h struct task_struct { · line 775
Quoted 3 lines
include/linux/sched.h +3
-0+#ifdef CONFIG_MEM_ALLOC_PROFILING +struct alloc_tag *alloc_tag; +#endif Normally scheduling is very sensitive to having anything early in
task_struct. I would suggest moving this theCONFIG_SCHED_COREifdef area.Thanks for the warning! We will look into that.
Quoted 190 lines
Quoted 158 lines
include/linux/sched.h +21
-0+#ifdef CONFIG_SMP int on_cpu;struct __call_single_node wake_entry;@@ -810,6 +814,7 @@ struct task_struct {struct task_group *sched_task_group;#endif +#ifdef CONFIG_UCLAMP_TASK /* * Clamp values requested for a scheduling entity. @@ -2183,4 +2188,23 @@ static inline int sched_core_idle_cpu(int cpu) { return idle_cpu(cpu); }extern void sched_set_stop_task (int cpu,struct task_struct *stop); +#ifdef CONFIG_MEM_ALLOC_PROFILING +static inline struct alloc_tag *alloc_tag_save (struct alloc_tag *tag) +{ +swap (current->alloc_tag, tag); +return tag; +} + +static inline void alloc_tag_restore (struct alloc_tag *tag,struct alloc_tag *old) +{ +#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG +WARN (current->alloc_tag != tag,"current->alloc_tag was changed:\n" ); +#endif + current->alloc_tag = old; +} +#else +static inline struct alloc_tag *alloc_tag_save (struct alloc_tag *tag) {return NULL ; } +#define alloc_tag_restore (_tag, _old) +#endif +#endif lib/Kconfig.debug +25
-0@@ -972,6 +972,31 @@ config CODE_TAGGING bool select KALLSYMS +config MEM_ALLOC_PROFILING + bool "Enable memory allocation profiling" + default n + depends on PROC_FS + depends on !DEBUG_FORCE_WEAK_PER_CPU + select CODE_TAGGING + help + Track allocation source code and record total allocation size + initiated at that code location. The mechanism can be used to track + memory leaks with a low performance and memory impact. + +config MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT + bool "Enable memory allocation profiling by default" + default y + depends on MEM_ALLOC_PROFILING + +config MEM_ALLOC_PROFILING_DEBUG + bool "Memory allocation profiler debugging" + default n + depends on MEM_ALLOC_PROFILING + select MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT + help + Adds warnings with helpful error messages for memory allocation + profiling. + source "lib/Kconfig.kasan" source "lib/Kconfig.kfence" source "lib/Kconfig.kmsan"lib/Makefile +2
-0@@ -236,6 +236,8 @@ obj-$(CONFIG_OF_RECONFIG_NOTIFIER_ERROR_INJECT) += \ obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o obj-$(CONFIG_CODE_TAGGING) += codetag.o +obj-$(CONFIG_MEM_ALLOC_PROFILING) += alloc_tag.o + lib-$(CONFIG_GENERIC_BUG) += bug.o obj-$(CONFIG_HAVE_ARCH_TRACEHOOK) += syscall.olib/alloc_tag.c new file +66
-0@@ -0,0 +1,158 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <linux/alloc_tag.h> +#include <linux/fs.h> +#include <linux/gfp.h> +#include <linux/module.h> +#include <linux/proc_fs.h> +#include <linux/seq_buf.h> +#include <linux/seq_file.h> + +static struct codetag_type *alloc_tag_cttype; + +DEFINE_STATIC_KEY_MAYBE (CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT , + mem_alloc_profiling_key); + +static void *allocinfo_start (struct seq_file *m,loff_t *pos) +{ +struct codetag_iterator *iter; +struct codetag *ct; +loff_t node = *pos; + + iter =kzalloc (sizeof (*iter),GFP_KERNEL ); + m->private = iter; +if (!iter) +return NULL ; + +codetag_lock_module_list (alloc_tag_cttype, true); + *iter =codetag_get_ct_iter (alloc_tag_cttype); +while ((ct =codetag_next_ct (iter)) !=NULL && node) + node--; + +return ct ? iter :NULL ; +} + +static void *allocinfo_next (struct seq_file *m,void *arg,loff_t *pos) +{ +struct codetag_iterator *iter = (struct codetag_iterator *)arg; +struct codetag *ct =codetag_next_ct (iter); + + (*pos)++; +if (!ct) +return NULL ; + +return iter; +} + +static void allocinfo_stop (struct seq_file *m,void *arg) +{ +struct codetag_iterator *iter = (struct codetag_iterator *)m->private; + +if (iter) { +codetag_lock_module_list (alloc_tag_cttype, false); +kfree (iter); + } +} + +static void alloc_tag_to_text (struct seq_buf *out,struct codetag *ct) +{ +struct alloc_tag *tag =ct_to_alloc_tag (ct); +struct alloc_tag_counters counter =alloc_tag_read (tag); +s64 bytes = counter.bytes; +char val[10 ], *p = val; + +if (bytes <0 ) { + *p++ ='-' ; + bytes = -bytes; + }lib/alloc_tag.c line 76
Quoted 10 lines
lib/alloc_tag.c new file +10
-0+ +string_get_size (bytes,1 , +STRING_SIZE_BASE2 |STRING_SIZE_NOSPACE , + p, val +ARRAY_SIZE (val) - p); + +seq_buf_printf (out,"%8s %8llu " , val, counter.calls); +codetag_to_text (out, ct); +seq_buf_putc (out,' ' ); +seq_buf_putc (out,'\n' ); +}/me does happy
seq_bufdance!Quoted 19 lines
lib/alloc_tag.c new file +19
-0+ +static int allocinfo_show (struct seq_file *m,void *arg) +{ +struct codetag_iterator *iter = (struct codetag_iterator *)arg; +char *bufp; +size_t n =seq_get_buf (m, &bufp); +struct seq_buf buf; + +seq_buf_init (&buf, bufp, n); +alloc_tag_to_text (&buf, iter->ct); +seq_commit (m,seq_buf_used (&buf)); +return 0 ; +} + +static const struct seq_operations allocinfo_seq_op = { + .start = allocinfo_start, + .next = allocinfo_next, + .stop = allocinfo_stop, + .show = allocinfo_show,Quoted 8 lines
lib/alloc_tag.c line 101
Quoted 6 lines
lib/alloc_tag.c new file +6
-0+}; + +static void __init procfs_init (void ) +{ +proc_create_seq ("allocinfo" ,0444 ,NULL , &allocinfo_seq_op); +}As mentioned, this really should be in /sys somewhere.
Ack.
Quoted 78 lines
Quoted 78 lines
lib/alloc_tag.c new file +57
-0+ +static bool alloc_tag_module_unload (struct codetag_type *cttype, +struct codetag_module *cmod) +{ +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; + +for (ct =codetag_next_ct (&iter); ct; ct =codetag_next_ct (&iter)) { +if (iter.cmod != cmod) +continue ; + + tag =ct_to_alloc_tag (ct); + counter =alloc_tag_read (tag); + +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; + } + +return module_unused; +} + +static struct ctl_table memory_allocation_profiling_sysctls[] = { + { + .procname ="mem_profiling" , + .data = &mem_alloc_profiling_key, +#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG + .mode =0444 , +#else + .mode =0644 , +#endif + .proc_handler = proc_do_static_key, + }, + { } +}; + +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, + }; + + alloc_tag_cttype =codetag_register_type (&desc); +if (IS_ERR_OR_NULL (alloc_tag_cttype)) +return PTR_ERR (alloc_tag_cttype); + +register_sysctl_init ("vm" , memory_allocation_profiling_sysctls); +procfs_init (); + +return 0 ; +} +module_init (alloc_tag_init);scripts/module.lds.S +3
-0@@ -9,6 +9,8 @@ #define DISCARD_EH_FRAME *(.eh_frame) #endif +#include <asm-generic/codetag.lds.h> + SECTIONS { /DISCARD/ : { *(.discard)@@ -47,12 +49,17 @@ SECTIONS { .data : { *(.data .data.[0-9a-zA-Z_]*) *(.data..L*) + CODETAG_SECTIONS() }Quoted 12 lines
scripts/module.lds.S SECTIONS { · line 64
Quoted 10 lines
scripts/module.lds.S +4
-0.rodata : { *(.rodata .rodata.[0-9a-zA-Z_]*) *(.rodata..L*) } +#else + .data : { + CODETAG_SECTIONS() + } #endif }Otherwise, looks good.
Thanks!
Quoted 3 lines
--
Kees Cook
Kent Overstreet
maintainerQuoted 42 lines · David Hildenbrand
Quoted 41 lines · Kent Overstreet
Quoted 27 lines · David Hildenbrand
Quoted 21 lines · Suren Baghdasaryan
Quoted 12 lines · Michal Hocko
On Mon 12-02-24 13:38:46, Suren Baghdasaryan wrote:
[...]Quoted 4 lines
We're aiming to get this in the next merge window, for 6.9. The feedback we've gotten has been that even out of tree this patchset has already been useful, and there's a significant amount of other work gated on the code tagging functionality included in this patchset [2].
I suspect it will not come as a surprise that I really dislike the implementation proposed here. I will not repeat my arguments, I have done so on several occasions already.
Quoted 7 lines
Anyway, I didn't go as far as to nak it even though I _strongly_ believe this debugging feature will add a maintenance overhead for a very long time. I can live with all the downsides of the proposed implementation
_aslongas_there is a wider agreement from the MM community as this is where the maintenance cost will be payed. So far I have not seen (m)any acks by MM developers so aiming into the next merge window is more than little rushed.Quoted 4 lines
We tried other previously proposed approaches and all have their downsides without making maintenance much easier. Your position is understandable and I think it's fair. Let's see if others see more benefit than cost here.
Quoted 7 lines
Would it make sense to discuss that at LSF/MM once again, especially covering why proposed alternatives did not work out? LSF/MM is not "too far" away (May).
I recall that the last LSF/MM session on this topic was a bit unfortunate (IMHO not as productive as it could have been). Maybe we can finally reach a consensus on this.
I'd rather not delay for more bikeshedding. Before agreeing to LSF I'd need to see a serious proposl - what we had at the last LSF was people jumping in with half baked alternative proposals that very much hadn't been thought through, and I see no need to repeat that.
Quoted 7 lines
Quoted 3 lines
Like I mentioned, there's other work gated on this patchset; if people want to hold this up for more discussion they better be putting forth something to discuss.
I'm thinking of ways on how to achieve Michal's request: "as long as there is a wider agreement from the MM community". If we can achieve that without LSF, great! (a bi-weekly MM meeting might also be an option)
A meeting wouldn't be out of the question, _if_ there is an agenda, but:
What's that coffeee mug say? I just survived another meeting that could've been an email? What exactly is the outcome we're looking for?
Is there info that people are looking for? I think we summed things up pretty well in the cover letter; if there are specifics that people want to discuss, that's why we emailed the series out.
There's people in this thread who've used this patchset in production and diagnosed real issues (gigabytes of memory gone missing, I heard the other day); I'm personally looking for them to chime in on this thread (Johannes, Pasha).
If it's just grumbling about "maintenance overhead" we need to get past
- well, people are going to have to accept that we can't deliver features without writing code, and I'm confident that the hooking in particular is about as clean as it's going to get, _regardless_ of toolchain support; and moreover it addresses what's been historically a pretty gaping hole in our ability to profile and understand the code we write.
Suren Baghdasaryan
maintainerQuoted 43 lines · David Hildenbrand
Quoted 41 lines · Kent Overstreet
Quoted 27 lines · David Hildenbrand
Quoted 21 lines · Suren Baghdasaryan
Quoted 12 lines · Michal Hocko
On Mon 12-02-24 13:38:46, Suren Baghdasaryan wrote:
[...]Quoted 4 lines
We're aiming to get this in the next merge window, for 6.9. The feedback we've gotten has been that even out of tree this patchset has already been useful, and there's a significant amount of other work gated on the code tagging functionality included in this patchset [2].
I suspect it will not come as a surprise that I really dislike the implementation proposed here. I will not repeat my arguments, I have done so on several occasions already.
Quoted 7 lines
Anyway, I didn't go as far as to nak it even though I _strongly_ believe this debugging feature will add a maintenance overhead for a very long time. I can live with all the downsides of the proposed implementation
_aslongas_there is a wider agreement from the MM community as this is where the maintenance cost will be payed. So far I have not seen (m)any acks by MM developers so aiming into the next merge window is more than little rushed.Quoted 4 lines
We tried other previously proposed approaches and all have their downsides without making maintenance much easier. Your position is understandable and I think it's fair. Let's see if others see more benefit than cost here.
Quoted 7 lines
Would it make sense to discuss that at LSF/MM once again, especially covering why proposed alternatives did not work out? LSF/MM is not "too far" away (May).
I recall that the last LSF/MM session on this topic was a bit unfortunate (IMHO not as productive as it could have been). Maybe we can finally reach a consensus on this.
I'd rather not delay for more bikeshedding. Before agreeing to LSF I'd need to see a serious proposl - what we had at the last LSF was people jumping in with half baked alternative proposals that very much hadn't been thought through, and I see no need to repeat that.
Quoted 7 lines
Quoted 3 lines
Like I mentioned, there's other work gated on this patchset; if people want to hold this up for more discussion they better be putting forth something to discuss.
I'm thinking of ways on how to achieve Michal's request: "as long as there is a wider agreement from the MM community". If we can achieve that without LSF, great! (a bi-weekly MM meeting might also be an option)
There will be a maintenance burden even with the cleanest proposed approach. We worked hard to make the patchset as clean as possible and if benefits still don't outweigh the maintenance cost then we should probably stop trying. At LSF/MM I would rather discuss functonal issues/requirements/improvements than alternative approaches to instrument allocators.
I'm happy to arrange a separate meeting with MM folks if that would help to progress on the cost/benefit decision.
Quoted 9 lines
--
Cheers,David / dhildenb
--
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
Suren Baghdasaryan
maintainerRe: [PATCH v3 13/35] lib: add allocation tagging support for memory allocation profiling
In reply to Darrick J. WongQuoted 16 lines · Darrick J. Wong
Quoted 7 lines · Suren Baghdasaryan
Quoted 6 lines · Kees Cook
Quoted 3 lines · Suren Baghdasaryan
Introduce
CONFIG_MEM_ALLOC_PROFILINGwhich provides definitions to easily instrument memory allocators. It registers an "alloc_tags" codetag type with/proc/allocinfointerface to output allocation tag information whenQuoted 6 lines
Quoted 2 lines
Please don't add anything new to the top-level /proc directory. This should likely live in /sys.
Ack. I'll find a more appropriate place for it then.
It just seemed like such generic information which would belong next to meminfo/zoneinfo and such...
Quoted 5 lines
Save yourself a cycle of "rework the whole fs interface only to have someone else tell you no" and put it in debugfs, not sysfs. Wrangling with debugfs is easier than all the macro-happy sysfs stuff; you don't have to integrate with the "device" model; and there is no 'one value per file' rule.
Thanks for the input. This file used to be in debugfs but reviewers felt it belonged in /proc if it's to be used in production environments. Some distros (like Android) disable debugfs in production.
Quoted 618 lines
--D
Quoted 615 lines
Quoted 192 lines
Quoted 192 lines
the feature is enabled.
CONFIG_MEM_ALLOC_PROFILING_DEBUGis provided for debugging the memory allocation profiling instrumentation.
Memory allocation profiling can be enabled or disabled at runtime using/proc/sys/vm/mem_profilingsysctl whenCONFIG_MEM_ALLOC_PROFILING_DEBUG=n.CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULTenables memory allocation profiling by default.Signed-off-by Suren Baghdasaryan <[email protected]>Co-developed-by Kent Overstreet <[email protected]>Signed-off-by Kent Overstreet <[email protected]>--- Documentation/admin-guide/sysctl/vm.rst | 16 +++ Documentation/filesystems/proc.rst | 28 +++++ include/asm-generic/codetag.lds.h | 14 +++ include/asm-generic/vmlinux.lds.h | 3 + include/linux/alloc_tag.h | 133 ++++++++++++++++++++ include/linux/sched.h | 24 ++++ lib/Kconfig.debug | 25 ++++ lib/Makefile | 2 + lib/alloc_tag.c | 158 ++++++++++++++++++++++++ scripts/module.lds.S | 7 ++ 10 files changed, 410 insertions(+) create mode 100644 include/asm-generic/codetag.lds.h create mode 100644 include/linux/alloc_tag.h create mode 100644 lib/alloc_tag.cDocumentation/admin-guide/sysctl/vm.rst +16
-0@@ -43,6 +43,7 @@ Currently, these files are in /proc/sys/vm: - legacy_va_layout - lowmem_reserve_ratio - max_map_count +- mem_profiling (only if CONFIG_MEM_ALLOC_PROFILING=y) - memory_failure_early_kill - memory_failure_recovery - min_free_kbytes@@ -425,6 +426,21 @@ e.g., up to one or two maps per allocation. The default value is 65530. +mem_profiling +============== + +Enable memory profiling (when CONFIG_MEM_ALLOC_PROFILING=y) + +1: Enable memory profiling. + +0: Disabld memory profiling. + +Enabling memory profiling introduces a small performance overhead for all +memory allocations. + +The default value depends on CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT. + + memory_failure_early_kill: ==========================Documentation/filesystems/proc.rst +28
-0@@ -688,6 +688,7 @@ files are there, and which are missing. ============ =============================================================== File Content ============ =============================================================== + allocinfo Memory allocations profiling information apm Advanced power management info bootconfig Kernel command line obtained from boot config, and, if there were kernel parameters from the@@ -953,6 +954,33 @@ also be allocatable although a lot of filesystem metadata may have to be reclaimed to achieve this. +allocinfo +~~~~~~~ + +Provides information about memory allocations at all locations in the code +base. Each allocation in the code is identified by its source file, line +number, module and the function calling the allocation. The number of bytes +allocated at each location is reported. + +Example output. + +:: + + > cat /proc/allocinfo + + 153MiB mm/slub.c:1826 module:slub func:alloc_slab_page + 6.08MiB mm/slab_common.c:950 module:slab_common func:_kmalloc_order + 5.09MiB mm/memcontrol.c:2814 module:memcontrol func:alloc_slab_obj_exts + 4.54MiB mm/page_alloc.c:5777 module:page_alloc func:alloc_pages_exact + 1.32MiB include/asm-generic/pgalloc.h:63 module:pgtable func:__pte_alloc_one + 1.16MiB fs/xfs/xfs_log_priv.h:700 module:xfs func:xlog_kvmalloc + 1.00MiB mm/swap_cgroup.c:48 module:swap_cgroup func:swap_cgroup_prepare + 734KiB fs/xfs/kmem.c:20 module:xfs func:kmem_alloc + 640KiB kernel/rcu/tree.c:3184 module:tree func:fill_page_cache_func + 640KiB drivers/char/virtio_console.c:452 module:virtio_console func:alloc_buf + ... + + meminfo ~~~~~~~include/asm-generic/codetag.lds.h new file +14
-0@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef __ASM_GENERIC_CODETAG_LDS_H +#define __ASM_GENERIC_CODETAG_LDS_H + +#define SECTION_WITH_BOUNDARIES (_name) \ + . = ALIGN(8); \ + __start_##_name = .; \ + KEEP(*(_name)) \ + __stop_##_name = .; + +#define CODETAG_SECTIONS () \ + SECTION_WITH_BOUNDARIES(alloc_tags) + +#endif /* __ASM_GENERIC_CODETAG_LDS_H */ include/asm-generic/vmlinux.lds.h +3
-0@@ -50,6 +50,8 @@* [__nosave_begin, __nosave_end] for the nosave data */ +#include <asm-generic/codetag.lds.h> +#ifndef LOAD_OFFSET #define LOAD_OFFSET 0#endif @@ -366,6 +368,7 @@ . = ALIGN(8); \ BOUNDED_SECTION_BY(__dyndbg_classes, ___dyndbg_classes) \ BOUNDED_SECTION_BY(__dyndbg, ___dyndbg) \ + CODETAG_SECTIONS() \ LIKELY_PROFILE() \ BRANCH_PROFILE() \TRACE_PRINTKS () \include/linux/alloc_tag.h new file +37
-0@@ -0,0 +1,133 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* allocation tagging +*/ +#ifndef _LINUX_ALLOC_TAG_H +#define _LINUX_ALLOC_TAG_H + +#include <linux/bug.h> +#include <linux/codetag.h> +#include <linux/container_of.h> +#include <linux/preempt.h> +#include <asm/percpu.h> +#include <linux/cpumask.h> +#include <linux/static_key.h> + +struct alloc_tag_counters { +u64 bytes; +u64 calls; +}; + +/* +* An instance of this structure is created in a special ELF section at every +* allocation callsite. At runtime, the special section is treated as +* an array of these. Embedded codetag utilizes codetag framework. +*/ +struct alloc_tag { +struct codetag ct; +struct alloc_tag_counters __percpu *counters; +}__aligned(8) ; + +#ifdef CONFIG_MEM_ALLOC_PROFILING + +static inline struct alloc_tag *ct_to_alloc_tag (struct codetag *ct) +{ + return container_of(ct, struct alloc_tag, ct); +} +Quoted 8 lines
include/linux/alloc_tag.h line 43
Quoted 6 lines
include/linux/alloc_tag.h new file +6
-0+#ifdef ARCH_NEEDS_WEAK_PER_CPU +/* +* When percpu variables are required to be defined as weak, static percpu +* variables can't be used inside a function (see comments for DECLARE_PER_CPU_SECTION). +*/ +#error "Memory allocation profiling is incompatible with ARCH_NEEDS_WEAK_PER_CPU"Is this enforced via
Kconfigas well? (Looks like only alpha and s390?)Unfortunately
ARCH_NEEDS_WEAK_PER_CPUis not aKconfigoption butCONFIG_DEBUG_FORCE_WEAK_PER_CPUis, so that one is handled viaKconfig(see "depends on !DEBUG_FORCE_WEAK_PER_CPU" in this patch). We have to avoid both cases because of this:
https://elixir.bootlin.com/linux/latest/source/include/linux/percpu-defs.h#L75,
so I'm trying to provide an informative error here.Quoted 99 lines
Quoted 99 lines
include/linux/alloc_tag.h new file +90
-0+#endif + +#define DEFINE_ALLOC_TAG (_alloc_tag, _old) \ + static DEFINE_PER_CPU(struct alloc_tag_counters, _alloc_tag_cntr); \ + static struct alloc_tag _alloc_tag __used __aligned(8) \ + __section("alloc_tags") = { \ + .ct = CODE_TAG_INIT, \ + .counters = &_alloc_tag_cntr }; \ + struct alloc_tag * __maybe_unused _old = alloc_tag_save(&_alloc_tag) + +DECLARE_STATIC_KEY_MAYBE (CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT , + mem_alloc_profiling_key); + +static inline bool mem_alloc_profiling_enabled (void ) +{ +return static_branch_maybe (CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT , + &mem_alloc_profiling_key); +} + +static inline struct alloc_tag_counters alloc_tag_read (struct alloc_tag *tag) +{ +struct alloc_tag_counters v = {0 ,0 }; +struct alloc_tag_counters *counter; +int cpu; + +for_each_possible_cpu (cpu) { + counter =per_cpu_ptr (tag->counters, cpu); + v.bytes += counter->bytes; + v.calls += counter->calls; + } + +return v; +} + +static inline void __alloc_tag_sub (union codetag_ref *ref,size_t bytes) +{ +struct alloc_tag *tag; + +#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG +WARN_ONCE (ref && !ref->ct,"alloc_tag was not set\n" ); +#endif +if (!ref || !ref->ct) +return ; + + tag =ct_to_alloc_tag (ref->ct); + +this_cpu_sub (tag->counters->bytes, bytes); +this_cpu_dec (tag->counters->calls); + + ref->ct =NULL ; +} + +static inline void alloc_tag_sub (union codetag_ref *ref,size_t bytes) +{ +__alloc_tag_sub (ref, bytes); +} + +static inline void alloc_tag_sub_noalloc (union codetag_ref *ref,size_t bytes) +{ +__alloc_tag_sub (ref, bytes); +} + +static inline void alloc_tag_add (union codetag_ref *ref,struct alloc_tag *tag,size_t bytes) +{ +#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG +WARN_ONCE (ref && ref->ct, +"alloc_tag was not cleared (got tag for %s:%u)\n" ,\ + ref->ct->filename, ref->ct->lineno); + +WARN_ONCE (!tag,"current->alloc_tag not set" ); +#endif +if (!ref || !tag) +return ; + + ref->ct = &tag->ct; +this_cpu_add (tag->counters->bytes, bytes); +this_cpu_inc (tag->counters->calls); +} + +#else + +#define DEFINE_ALLOC_TAG (_alloc_tag, _old) +static inline void alloc_tag_sub (union codetag_ref *ref,size_t bytes) {} +static inline void alloc_tag_sub_noalloc (union codetag_ref *ref,size_t bytes) {} +static inline void alloc_tag_add (union codetag_ref *ref,struct alloc_tag *tag, +size_t bytes) {} + +#endif + +#endif /* _LINUX_ALLOC_TAG_H */ include/linux/sched.h +0
-0@@ -770,6 +770,10 @@ struct task_struct {unsigned int flags;unsigned int ptrace;Quoted 7 lines
include/linux/sched.h struct task_struct { · line 775
Quoted 3 lines
include/linux/sched.h +3
-0+#ifdef CONFIG_MEM_ALLOC_PROFILING +struct alloc_tag *alloc_tag; +#endif Normally scheduling is very sensitive to having anything early in
task_struct. I would suggest moving this theCONFIG_SCHED_COREifdef area.Thanks for the warning! We will look into that.
Quoted 190 lines
Quoted 158 lines
include/linux/sched.h +21
-0+#ifdef CONFIG_SMP int on_cpu;struct __call_single_node wake_entry;@@ -810,6 +814,7 @@ struct task_struct {struct task_group *sched_task_group;#endif +#ifdef CONFIG_UCLAMP_TASK /* * Clamp values requested for a scheduling entity. @@ -2183,4 +2188,23 @@ static inline int sched_core_idle_cpu(int cpu) { return idle_cpu(cpu); }extern void sched_set_stop_task (int cpu,struct task_struct *stop); +#ifdef CONFIG_MEM_ALLOC_PROFILING +static inline struct alloc_tag *alloc_tag_save (struct alloc_tag *tag) +{ +swap (current->alloc_tag, tag); +return tag; +} + +static inline void alloc_tag_restore (struct alloc_tag *tag,struct alloc_tag *old) +{ +#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG +WARN (current->alloc_tag != tag,"current->alloc_tag was changed:\n" ); +#endif + current->alloc_tag = old; +} +#else +static inline struct alloc_tag *alloc_tag_save (struct alloc_tag *tag) {return NULL ; } +#define alloc_tag_restore (_tag, _old) +#endif +#endif lib/Kconfig.debug +25
-0@@ -972,6 +972,31 @@ config CODE_TAGGING bool select KALLSYMS +config MEM_ALLOC_PROFILING + bool "Enable memory allocation profiling" + default n + depends on PROC_FS + depends on !DEBUG_FORCE_WEAK_PER_CPU + select CODE_TAGGING + help + Track allocation source code and record total allocation size + initiated at that code location. The mechanism can be used to track + memory leaks with a low performance and memory impact. + +config MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT + bool "Enable memory allocation profiling by default" + default y + depends on MEM_ALLOC_PROFILING + +config MEM_ALLOC_PROFILING_DEBUG + bool "Memory allocation profiler debugging" + default n + depends on MEM_ALLOC_PROFILING + select MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT + help + Adds warnings with helpful error messages for memory allocation + profiling. + source "lib/Kconfig.kasan" source "lib/Kconfig.kfence" source "lib/Kconfig.kmsan"lib/Makefile +2
-0@@ -236,6 +236,8 @@ obj-$(CONFIG_OF_RECONFIG_NOTIFIER_ERROR_INJECT) += \ obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o obj-$(CONFIG_CODE_TAGGING) += codetag.o +obj-$(CONFIG_MEM_ALLOC_PROFILING) += alloc_tag.o + lib-$(CONFIG_GENERIC_BUG) += bug.o obj-$(CONFIG_HAVE_ARCH_TRACEHOOK) += syscall.olib/alloc_tag.c new file +66
-0@@ -0,0 +1,158 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include <linux/alloc_tag.h> +#include <linux/fs.h> +#include <linux/gfp.h> +#include <linux/module.h> +#include <linux/proc_fs.h> +#include <linux/seq_buf.h> +#include <linux/seq_file.h> + +static struct codetag_type *alloc_tag_cttype; + +DEFINE_STATIC_KEY_MAYBE (CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT , + mem_alloc_profiling_key); + +static void *allocinfo_start (struct seq_file *m,loff_t *pos) +{ +struct codetag_iterator *iter; +struct codetag *ct; +loff_t node = *pos; + + iter =kzalloc (sizeof (*iter),GFP_KERNEL ); + m->private = iter; +if (!iter) +return NULL ; + +codetag_lock_module_list (alloc_tag_cttype, true); + *iter =codetag_get_ct_iter (alloc_tag_cttype); +while ((ct =codetag_next_ct (iter)) !=NULL && node) + node--; + +return ct ? iter :NULL ; +} + +static void *allocinfo_next (struct seq_file *m,void *arg,loff_t *pos) +{ +struct codetag_iterator *iter = (struct codetag_iterator *)arg; +struct codetag *ct =codetag_next_ct (iter); + + (*pos)++; +if (!ct) +return NULL ; + +return iter; +} + +static void allocinfo_stop (struct seq_file *m,void *arg) +{ +struct codetag_iterator *iter = (struct codetag_iterator *)m->private; + +if (iter) { +codetag_lock_module_list (alloc_tag_cttype, false); +kfree (iter); + } +} + +static void alloc_tag_to_text (struct seq_buf *out,struct codetag *ct) +{ +struct alloc_tag *tag =ct_to_alloc_tag (ct); +struct alloc_tag_counters counter =alloc_tag_read (tag); +s64 bytes = counter.bytes; +char val[10 ], *p = val; + +if (bytes <0 ) { + *p++ ='-' ; + bytes = -bytes; + }lib/alloc_tag.c line 76
Quoted 10 lines
lib/alloc_tag.c new file +10
-0+ +string_get_size (bytes,1 , +STRING_SIZE_BASE2 |STRING_SIZE_NOSPACE , + p, val +ARRAY_SIZE (val) - p); + +seq_buf_printf (out,"%8s %8llu " , val, counter.calls); +codetag_to_text (out, ct); +seq_buf_putc (out,' ' ); +seq_buf_putc (out,'\n' ); +}/me does happy
seq_bufdance!Quoted 19 lines
lib/alloc_tag.c new file +19
-0+ +static int allocinfo_show (struct seq_file *m,void *arg) +{ +struct codetag_iterator *iter = (struct codetag_iterator *)arg; +char *bufp; +size_t n =seq_get_buf (m, &bufp); +struct seq_buf buf; + +seq_buf_init (&buf, bufp, n); +alloc_tag_to_text (&buf, iter->ct); +seq_commit (m,seq_buf_used (&buf)); +return 0 ; +} + +static const struct seq_operations allocinfo_seq_op = { + .start = allocinfo_start, + .next = allocinfo_next, + .stop = allocinfo_stop, + .show = allocinfo_show,Quoted 8 lines
lib/alloc_tag.c line 101
Quoted 6 lines
lib/alloc_tag.c new file +6
-0+}; + +static void __init procfs_init (void ) +{ +proc_create_seq ("allocinfo" ,0444 ,NULL , &allocinfo_seq_op); +}As mentioned, this really should be in /sys somewhere.
Ack.
Quoted 78 lines
Quoted 78 lines
lib/alloc_tag.c new file +57
-0+ +static bool alloc_tag_module_unload (struct codetag_type *cttype, +struct codetag_module *cmod) +{ +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; + +for (ct =codetag_next_ct (&iter); ct; ct =codetag_next_ct (&iter)) { +if (iter.cmod != cmod) +continue ; + + tag =ct_to_alloc_tag (ct); + counter =alloc_tag_read (tag); + +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; + } + +return module_unused; +} + +static struct ctl_table memory_allocation_profiling_sysctls[] = { + { + .procname ="mem_profiling" , + .data = &mem_alloc_profiling_key, +#ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG + .mode =0444 , +#else + .mode =0644 , +#endif + .proc_handler = proc_do_static_key, + }, + { } +}; + +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, + }; + + alloc_tag_cttype =codetag_register_type (&desc); +if (IS_ERR_OR_NULL (alloc_tag_cttype)) +return PTR_ERR (alloc_tag_cttype); + +register_sysctl_init ("vm" , memory_allocation_profiling_sysctls); +procfs_init (); + +return 0 ; +} +module_init (alloc_tag_init);scripts/module.lds.S +3
-0@@ -9,6 +9,8 @@ #define DISCARD_EH_FRAME *(.eh_frame) #endif +#include <asm-generic/codetag.lds.h> + SECTIONS { /DISCARD/ : { *(.discard)@@ -47,12 +49,17 @@ SECTIONS { .data : { *(.data .data.[0-9a-zA-Z_]*) *(.data..L*) + CODETAG_SECTIONS() }Quoted 12 lines
scripts/module.lds.S SECTIONS { · line 64
Quoted 10 lines
scripts/module.lds.S +4
-0.rodata : { *(.rodata .rodata.[0-9a-zA-Z_]*) *(.rodata..L*) } +#else + .data : { + CODETAG_SECTIONS() + } #endif }Otherwise, looks good.
Thanks!
Quoted 3 lines
--
Kees Cook
Re: [PATCH v3 13/35] lib: add allocation tagging support for memory allocation profiling
Quoted 23 lines · Suren Baghdasaryan
Quoted 16 lines · Darrick J. Wong
Quoted 7 lines · Suren Baghdasaryan
Quoted 6 lines · Kees Cook
Quoted 3 lines · Suren Baghdasaryan
Introduce
CONFIG_MEM_ALLOC_PROFILINGwhich provides definitions to easily instrument memory allocators. It registers an "alloc_tags" codetag type with/proc/allocinfointerface to output allocation tag information whenQuoted 6 lines
Quoted 2 lines
Please don't add anything new to the top-level /proc directory. This should likely live in /sys.
Ack. I'll find a more appropriate place for it then.
It just seemed like such generic information which would belong next to meminfo/zoneinfo and such...Quoted 5 lines
Save yourself a cycle of "rework the whole fs interface only to have someone else tell you no" and put it in debugfs, not sysfs. Wrangling with debugfs is easier than all the macro-happy sysfs stuff; you don't have to integrate with the "device" model; and there is no 'one value per file' rule.
Quoted 4 lines
Thanks for the input. This file used to be in debugfs but reviewers felt it belonged in /proc if it's to be used in production environments. Some distros (like Android) disable debugfs in production.
FWIW, I agree debugfs is not right. If others feel it's right in /proc, I certainly won't NAK -- it's just been that we've traditionally been trying to avoid continuing to pollute the top-level /proc and instead associate new things with something in /sys.
Signature · 1 line
Kees Cook
Steven Rostedt
reviewerRe: [PATCH v3 13/35] lib: add allocation tagging support for memory allocation profiling
Quoted 11 lines · On Tue, 13 Feb 2024 14:38:16 -0800 Kees Cook <[email protected]> wrote:
Quoted 10 lines
Quoted 5 lines
Save yourself a cycle of "rework the whole fs interface only to have someone else tell you no" and put it in debugfs, not sysfs. Wrangling with debugfs is easier than all the macro-happy sysfs stuff; you don't have to integrate with the "device" model; and there is no 'one value per file' rule.
Thanks for the input. This file used to be in debugfs but reviewers felt it belonged in /proc if it's to be used in production environments. Some distros (like Android) disable debugfs in production.
Quoted 4 lines
FWIW, I agree debugfs is not right. If others feel it's right in /proc, I certainly won't NAK -- it's just been that we've traditionally been trying to avoid continuing to pollute the top-level /proc and instead associate new things with something in /sys.
You can create your own file system, but I would suggest using kernfs for it ;-)
If you look in /sys/kernel/ you'll see a bunch of kernel file systems already there:
~# mount |grep kernel
securityfs on /sys/kernel/security type securityfs (rw,nosuid,nodev,noexec,relatime) debugfs on /sys/kernel/debug type debugfs (rw,nosuid,nodev,noexec,relatime) tracefs on /sys/kernel/tracing type tracefs (rw,nosuid,nodev,noexec,relatime) configfs on /sys/kernel/config type configfs (rw,nosuid,nodev,noexec,relatime)
-- Steve
Quoted 48 lines · Suren Baghdasaryan
Quoted 47 lines · David Hildenbrand
Quoted 44 lines · Kent Overstreet
Quoted 27 lines · David Hildenbrand
Quoted 21 lines · Suren Baghdasaryan
Quoted 12 lines · Michal Hocko
On Mon 12-02-24 13:38:46, Suren Baghdasaryan wrote:
[...]Quoted 4 lines
We're aiming to get this in the next merge window, for 6.9. The feedback we've gotten has been that even out of tree this patchset has already been useful, and there's a significant amount of other work gated on the code tagging functionality included in this patchset [2].
I suspect it will not come as a surprise that I really dislike the implementation proposed here. I will not repeat my arguments, I have done so on several occasions already.
Quoted 7 lines
Anyway, I didn't go as far as to nak it even though I _strongly_ believe this debugging feature will add a maintenance overhead for a very long time. I can live with all the downsides of the proposed implementation
_aslongas_there is a wider agreement from the MM community as this is where the maintenance cost will be payed. So far I have not seen (m)any acks by MM developers so aiming into the next merge window is more than little rushed.Quoted 4 lines
We tried other previously proposed approaches and all have their downsides without making maintenance much easier. Your position is understandable and I think it's fair. Let's see if others see more benefit than cost here.
Quoted 7 lines
Would it make sense to discuss that at LSF/MM once again, especially covering why proposed alternatives did not work out? LSF/MM is not "too far" away (May).
I recall that the last LSF/MM session on this topic was a bit unfortunate (IMHO not as productive as it could have been). Maybe we can finally reach a consensus on this.
I'd rather not delay for more bikeshedding. Before agreeing to LSF I'd need to see a serious proposl - what we had at the last LSF was people jumping in with half baked alternative proposals that very much hadn't been thought through, and I see no need to repeat that.
Like I mentioned, there's other work gated on this patchset; if people want to hold this up for more discussion they better be putting forth something to discuss.
Quoted 6 lines
Quoted 3 lines
I'm thinking of ways on how to achieve Michal's request: "as long as there is a wider agreement from the MM community". If we can achieve that without LSF, great! (a bi-weekly MM meeting might also be an option)
There will be a maintenance burden even with the cleanest proposed approach.
Yes.
Quoted 3 lines
We worked hard to make the patchset as clean as possible and if benefits still don't outweigh the maintenance cost then we should probably stop trying.
Indeed.
Quoted 5 lines
At LSF/MM I would rather discuss functonal issues/requirements/improvements than alternative approaches to instrument allocators.
I'm happy to arrange a separate meeting with MM folks if that would help to progress on the cost/benefit decision.
Note that I am only proposing ways forward.
If you think you can easily achieve what Michal requested without all that, good.
My past experience was that LSF/MM / bi-weekly MM meetings were extremely helpful to reach consensus.
Signature · 3 lines
Cheers, David / dhildenb
Kent Overstreet
maintainerQuoted 69 lines · David Hildenbrand
Quoted 48 lines · Suren Baghdasaryan
Quoted 47 lines · David Hildenbrand
Quoted 44 lines · Kent Overstreet
Quoted 27 lines · David Hildenbrand
Quoted 21 lines · Suren Baghdasaryan
Quoted 12 lines · Michal Hocko
On Mon 12-02-24 13:38:46, Suren Baghdasaryan wrote:
[...]Quoted 4 lines
We're aiming to get this in the next merge window, for 6.9. The feedback we've gotten has been that even out of tree this patchset has already been useful, and there's a significant amount of other work gated on the code tagging functionality included in this patchset [2].
I suspect it will not come as a surprise that I really dislike the implementation proposed here. I will not repeat my arguments, I have done so on several occasions already.
Quoted 7 lines
Anyway, I didn't go as far as to nak it even though I _strongly_ believe this debugging feature will add a maintenance overhead for a very long time. I can live with all the downsides of the proposed implementation
_aslongas_there is a wider agreement from the MM community as this is where the maintenance cost will be payed. So far I have not seen (m)any acks by MM developers so aiming into the next merge window is more than little rushed.Quoted 4 lines
We tried other previously proposed approaches and all have their downsides without making maintenance much easier. Your position is understandable and I think it's fair. Let's see if others see more benefit than cost here.
Quoted 7 lines
Would it make sense to discuss that at LSF/MM once again, especially covering why proposed alternatives did not work out? LSF/MM is not "too far" away (May).
I recall that the last LSF/MM session on this topic was a bit unfortunate (IMHO not as productive as it could have been). Maybe we can finally reach a consensus on this.
I'd rather not delay for more bikeshedding. Before agreeing to LSF I'd need to see a serious proposl - what we had at the last LSF was people jumping in with half baked alternative proposals that very much hadn't been thought through, and I see no need to repeat that.
Like I mentioned, there's other work gated on this patchset; if people want to hold this up for more discussion they better be putting forth something to discuss.
Quoted 6 lines
Quoted 3 lines
I'm thinking of ways on how to achieve Michal's request: "as long as there is a wider agreement from the MM community". If we can achieve that without LSF, great! (a bi-weekly MM meeting might also be an option)
There will be a maintenance burden even with the cleanest proposed approach.
Yes.
Quoted 3 lines
We worked hard to make the patchset as clean as possible and if benefits still don't outweigh the maintenance cost then we should probably stop trying.
Indeed.
Quoted 5 lines
At LSF/MM I would rather discuss functonal issues/requirements/improvements than alternative approaches to instrument allocators.
I'm happy to arrange a separate meeting with MM folks if that would help to progress on the cost/benefit decision.
Quoted 4 lines
Note that I am only proposing ways forward.
If you think you can easily achieve what Michal requested without all that, good.
He requested something?
Quoted 70 lines · Kent Overstreet
Quoted 69 lines · David Hildenbrand
Quoted 48 lines · Suren Baghdasaryan
Quoted 47 lines · David Hildenbrand
Quoted 44 lines · Kent Overstreet
Quoted 27 lines · David Hildenbrand
Quoted 21 lines · Suren Baghdasaryan
Quoted 12 lines · Michal Hocko
On Mon 12-02-24 13:38:46, Suren Baghdasaryan wrote:
[...]Quoted 4 lines
We're aiming to get this in the next merge window, for 6.9. The feedback we've gotten has been that even out of tree this patchset has already been useful, and there's a significant amount of other work gated on the code tagging functionality included in this patchset [2].
I suspect it will not come as a surprise that I really dislike the implementation proposed here. I will not repeat my arguments, I have done so on several occasions already.
Quoted 7 lines
Anyway, I didn't go as far as to nak it even though I _strongly_ believe this debugging feature will add a maintenance overhead for a very long time. I can live with all the downsides of the proposed implementation
_aslongas_there is a wider agreement from the MM community as this is where the maintenance cost will be payed. So far I have not seen (m)any acks by MM developers so aiming into the next merge window is more than little rushed.Quoted 4 lines
We tried other previously proposed approaches and all have their downsides without making maintenance much easier. Your position is understandable and I think it's fair. Let's see if others see more benefit than cost here.
Quoted 7 lines
Would it make sense to discuss that at LSF/MM once again, especially covering why proposed alternatives did not work out? LSF/MM is not "too far" away (May).
I recall that the last LSF/MM session on this topic was a bit unfortunate (IMHO not as productive as it could have been). Maybe we can finally reach a consensus on this.
I'd rather not delay for more bikeshedding. Before agreeing to LSF I'd need to see a serious proposl - what we had at the last LSF was people jumping in with half baked alternative proposals that very much hadn't been thought through, and I see no need to repeat that.
Like I mentioned, there's other work gated on this patchset; if people want to hold this up for more discussion they better be putting forth something to discuss.
Quoted 6 lines
Quoted 3 lines
I'm thinking of ways on how to achieve Michal's request: "as long as there is a wider agreement from the MM community". If we can achieve that without LSF, great! (a bi-weekly MM meeting might also be an option)
There will be a maintenance burden even with the cleanest proposed approach.
Yes.
Quoted 3 lines
We worked hard to make the patchset as clean as possible and if benefits still don't outweigh the maintenance cost then we should probably stop trying.
Indeed.
Quoted 5 lines
At LSF/MM I would rather discuss functonal issues/requirements/improvements than alternative approaches to instrument allocators.
I'm happy to arrange a separate meeting with MM folks if that would help to progress on the cost/benefit decision.
Quoted 7 lines
Quoted 4 lines
Note that I am only proposing ways forward.
If you think you can easily achieve what Michal requested without all that, good.
He requested something?
This won't get merged without acks from MM people.
Signature · 3 lines
Cheers, David / dhildenb
Suren Baghdasaryan
maintainerQuoted 71 lines · Kent Overstreet
Quoted 69 lines · David Hildenbrand
Quoted 48 lines · Suren Baghdasaryan
Quoted 47 lines · David Hildenbrand
Quoted 44 lines · Kent Overstreet
Quoted 27 lines · David Hildenbrand
Quoted 21 lines · Suren Baghdasaryan
Quoted 12 lines · Michal Hocko
On Mon 12-02-24 13:38:46, Suren Baghdasaryan wrote:
[...]Quoted 4 lines
We're aiming to get this in the next merge window, for 6.9. The feedback we've gotten has been that even out of tree this patchset has already been useful, and there's a significant amount of other work gated on the code tagging functionality included in this patchset [2].
I suspect it will not come as a surprise that I really dislike the implementation proposed here. I will not repeat my arguments, I have done so on several occasions already.
Quoted 7 lines
Anyway, I didn't go as far as to nak it even though I _strongly_ believe this debugging feature will add a maintenance overhead for a very long time. I can live with all the downsides of the proposed implementation
_aslongas_there is a wider agreement from the MM community as this is where the maintenance cost will be payed. So far I have not seen (m)any acks by MM developers so aiming into the next merge window is more than little rushed.Quoted 4 lines
We tried other previously proposed approaches and all have their downsides without making maintenance much easier. Your position is understandable and I think it's fair. Let's see if others see more benefit than cost here.
Quoted 7 lines
Would it make sense to discuss that at LSF/MM once again, especially covering why proposed alternatives did not work out? LSF/MM is not "too far" away (May).
I recall that the last LSF/MM session on this topic was a bit unfortunate (IMHO not as productive as it could have been). Maybe we can finally reach a consensus on this.
I'd rather not delay for more bikeshedding. Before agreeing to LSF I'd need to see a serious proposl - what we had at the last LSF was people jumping in with half baked alternative proposals that very much hadn't been thought through, and I see no need to repeat that.
Like I mentioned, there's other work gated on this patchset; if people want to hold this up for more discussion they better be putting forth something to discuss.
Quoted 6 lines
Quoted 3 lines
I'm thinking of ways on how to achieve Michal's request: "as long as there is a wider agreement from the MM community". If we can achieve that without LSF, great! (a bi-weekly MM meeting might also be an option)
There will be a maintenance burden even with the cleanest proposed approach.
Yes.
Quoted 3 lines
We worked hard to make the patchset as clean as possible and if benefits still don't outweigh the maintenance cost then we should probably stop trying.
Indeed.
Quoted 5 lines
At LSF/MM I would rather discuss functonal issues/requirements/improvements than alternative approaches to instrument allocators.
I'm happy to arrange a separate meeting with MM folks if that would help to progress on the cost/benefit decision.
Quoted 6 lines
Quoted 4 lines
Note that I am only proposing ways forward.
If you think you can easily achieve what Michal requested without all that, good.
He requested something?
Yes, a cleaner instrumentation. Unfortunately the cleanest one is not possible until the compiler feature is developed and deployed. And it still would require changes to the headers, so don't think it's worth delaying the feature for years.
Quoted 78 lines · Suren Baghdasaryan
Quoted 76 lines · Kent Overstreet
Quoted 73 lines · David Hildenbrand
Quoted 48 lines · Suren Baghdasaryan
Quoted 47 lines · David Hildenbrand
Quoted 44 lines · Kent Overstreet
Quoted 27 lines · David Hildenbrand
Quoted 21 lines · Suren Baghdasaryan
Quoted 12 lines · Michal Hocko
On Mon 12-02-24 13:38:46, Suren Baghdasaryan wrote:
[...]Quoted 4 lines
We're aiming to get this in the next merge window, for 6.9. The feedback we've gotten has been that even out of tree this patchset has already been useful, and there's a significant amount of other work gated on the code tagging functionality included in this patchset [2].
I suspect it will not come as a surprise that I really dislike the implementation proposed here. I will not repeat my arguments, I have done so on several occasions already.
Quoted 7 lines
Anyway, I didn't go as far as to nak it even though I _strongly_ believe this debugging feature will add a maintenance overhead for a very long time. I can live with all the downsides of the proposed implementation
_aslongas_there is a wider agreement from the MM community as this is where the maintenance cost will be payed. So far I have not seen (m)any acks by MM developers so aiming into the next merge window is more than little rushed.Quoted 4 lines
We tried other previously proposed approaches and all have their downsides without making maintenance much easier. Your position is understandable and I think it's fair. Let's see if others see more benefit than cost here.
Quoted 7 lines
Would it make sense to discuss that at LSF/MM once again, especially covering why proposed alternatives did not work out? LSF/MM is not "too far" away (May).
I recall that the last LSF/MM session on this topic was a bit unfortunate (IMHO not as productive as it could have been). Maybe we can finally reach a consensus on this.
I'd rather not delay for more bikeshedding. Before agreeing to LSF I'd need to see a serious proposl - what we had at the last LSF was people jumping in with half baked alternative proposals that very much hadn't been thought through, and I see no need to repeat that.
Like I mentioned, there's other work gated on this patchset; if people want to hold this up for more discussion they better be putting forth something to discuss.
Quoted 6 lines
Quoted 3 lines
I'm thinking of ways on how to achieve Michal's request: "as long as there is a wider agreement from the MM community". If we can achieve that without LSF, great! (a bi-weekly MM meeting might also be an option)
There will be a maintenance burden even with the cleanest proposed approach.
Yes.
Quoted 3 lines
We worked hard to make the patchset as clean as possible and if benefits still don't outweigh the maintenance cost then we should probably stop trying.
Indeed.
Quoted 5 lines
At LSF/MM I would rather discuss functonal issues/requirements/improvements than alternative approaches to instrument allocators.
I'm happy to arrange a separate meeting with MM folks if that would help to progress on the cost/benefit decision.Note that I am only proposing ways forward.
If you think you can easily achieve what Michal requested without all that, good.
Quoted 7 lines
Quoted 1 line
He requested something?
Yes, a cleaner instrumentation. Unfortunately the cleanest one is not possible until the compiler feature is developed and deployed. And it still would require changes to the headers, so don't think it's worth delaying the feature for years.
I was talking about this: "I can live with all the downsides of the proposed implementationas long as there is a wider agreement from the MM community as this is where the maintenance cost will be payed. So far I have not seen (m)any acks by MM developers".
I certainly cannot be motivated at this point to review and ack this, unfortunately too much negative energy around here.
Signature · 3 lines
Cheers, David / dhildenb
Kent Overstreet
maintainerQuoted 9 lines · Suren Baghdasaryan
Quoted 7 lines · Kent Overstreet
Quoted 4 lines · David Hildenbrand
Quoted 1 line · Suren Baghdasaryan
On Tue, Feb 13, 2024 at 2:17 PM David Hildenbrand <[email protected]> wrote:
If you think you can easily achieve what Michal requested without all that, good.
Quoted 6 lines
Quoted 1 line
He requested something?
Yes, a cleaner instrumentation. Unfortunately the cleanest one is not possible until the compiler feature is developed and deployed. And it still would require changes to the headers, so don't think it's worth delaying the feature for years.
Hang on, let's look at the actual code.
This is what instrumenting an allocation function looks like:
#define krealloc_array(...) alloc_hooks(krealloc_array_noprof(__VA_ARGS__))
IOW, we have to:
- rename krealloc_array to krealloc_array_noprof
- replace krealloc_array with a one wrapper macro call
Is this really all we're getting worked up over?
The renaming we need regardless, because the thing that makes this approach efficient enough to run in production is that we account at _one_ point in the callstack, we don't save entire backtraces.
And thus we need to explicitly annotate which one that is; which means we need _noprof() versions of functions for when the accounting is done by an outer wraper (e.g. mempool).
And, as I keep saying: that alloc_hooks() macro will also get us _per callsite fault injection points_, and we really need that because - if you guys have been paying attention to other threads - whenever moving more stuff to PF_MEMALLOC_* flags comes up (including adding PF_MEMALLOC_NORECLAIM), the issue of small allocations not failing and not being testable keeps coming up.
kernel test robot
build botRe: [PATCH v3 29/35] mm: vmalloc: Enable memory allocation profiling
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 akpm-mm/mm-nonmm-unstable]
[also build test ERROR on linus/master v6.8-rc4]
[cannot apply to akpm-mm/mm-everything vbabka-slab/for-next next-20240213] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Suren-Baghdasaryan/lib-string_helpers-Add-flags-param-to-string_get_size/20240213-054335 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-nonmm-unstable patch link: https://lore.kernel.org/r/20240212213922.783301-30-surenb%40google.com patch subject: [PATCH v3 29/35] mm: vmalloc: Enable memory allocation profiling config: mips-bmips_stb_defconfig (https://download.01.org/0day-ci/archive/20240214/[email protected]/config) compiler: clang version 19.0.0git (https://github.com/llvm/llvm-project c08b90c50bcac9f3f563c79491c8dbcbe7c3b574) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240214/[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 >>):
In file included from arch/mips/kernel/asm-offsets.c:12:
In file included from include/linux/compat.h:15:
In file included from include/linux/socket.h:8:
In file included from include/linux/uio.h:10:
In file included from include/linux/mm_types.h:5:
In file included from include/linux/mm_types_task.h:13:
In file included from arch/mips/include/asm/page.h:200:
In file included from arch/mips/include/asm/io.h:563:
In file included from include/asm-generic/io.h:994:
In file included from include/linux/vmalloc.h:6:
>> include/linux/sched.h:1338:30: error: field has incomplete type 'struct tlbflush_unmap_batch'
1338 | struct tlbflush_unmap_batch tlb_ubc;
| ^
include/linux/sched.h:1338:9: note: forward declaration of 'struct tlbflush_unmap_batch'
1338 | struct tlbflush_unmap_batch tlb_ubc;
| ^
>> include/linux/sched.h:1343:20: error: field has incomplete type 'struct page_frag'
1343 | struct page_frag task_frag;
| ^
include/linux/sched.h:1343:9: note: forward declaration of 'struct page_frag'
1343 | struct page_frag task_frag;
| ^ 2 errors generated.
make[3]: *** [scripts/Makefile.build:116: arch/mips/kernel/asm-offsets.s] Error 1 make[3]: Target 'prepare' not remade because of errors.
make[2]: *** [Makefile:1191: prepare0] Error 2
make[2]: Target 'prepare' not remade because of errors.
make[1]: *** [Makefile:240: __sub-make] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:240: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.
vim +1338 include/linux/sched.h
af7f588d8f7355 Mathieu Desnoyers 2022-11-22 1337
72b252aed506b8 Mel Gorman 2015-09-04 @1338 struct tlbflush_unmap_batch tlb_ubc; 72b252aed506b8 Mel Gorman 2015-09-04 1339 5eca1c10cbaa9c Ingo Molnar 2017-02-06 1340 /* Cache last used pipe for splice(): */ b92ce55893745e Jens Axboe 2006-04-11 1341 struct pipe_inode_info *splice_pipe; 5640f7685831e0 Eric Dumazet 2012-09-23 1342 5640f7685831e0 Eric Dumazet 2012-09-23 @1343 struct page_frag task_frag; 5640f7685831e0 Eric Dumazet 2012-09-23 1344
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Quoted 25 lines · Kent Overstreet
Quoted 24 lines · David Hildenbrand
Quoted 23 lines · Kent Overstreet
Quoted 22 lines · David Hildenbrand
Quoted 21 lines · Suren Baghdasaryan
Quoted 19 lines · Michal Hocko
On Mon 12-02-24 13:38:46, Suren Baghdasaryan wrote:
[...]Quoted 4 lines
We're aiming to get this in the next merge window, for 6.9. The feedback we've gotten has been that even out of tree this patchset has already been useful, and there's a significant amount of other work gated on the code tagging functionality included in this patchset [2].
I suspect it will not come as a surprise that I really dislike the implementation proposed here. I will not repeat my arguments, I have done so on several occasions already.
Anyway, I didn't go as far as to nak it even though I _strongly_ believe this debugging feature will add a maintenance overhead for a very long time. I can live with all the downsides of the proposed implementation
_aslongas_there is a wider agreement from the MM community as this is where the maintenance cost will be payed. So far I have not seen (m)any acks by MM developers so aiming into the next merge window is more than little rushed.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
We tried other previously proposed approaches and all have their downsides without making maintenance much easier. Your position is understandable and I think it's fair. Let's see if others see more benefit than cost here.
Would it make sense to discuss that at LSF/MM once again, especially covering why proposed alternatives did not work out? LSF/MM is not "too far" away (May).
You want to stall this effort for *three months* to schedule a meeting?
I would love to have better profiling of memory allocations inside XFS so that we can answer questions like "What's going on with these allocation stalls?" or "What memory is getting used, and where?" more quickly than we can now.
Right now I get to stare at tracepoints and printk crap until my eyes bleed, and maybe dchinner comes to my rescue and figures out what's going on sooner than I do. More often we just never figure it out because only the customer can reproduce the problem, the reams of data produced by ftrace is unmanageable, and BPF isn't always available.
I'm not thrilled by the large increase in macro crap in the allocation paths, but I don't know of a better way to instrument things. Our attempts to use _RET_IP in XFS to instrument its code paths have never worked quite right w.r.t. inlined functions and whatnot.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
I recall that the last LSF/MM session on this topic was a bit unfortunate (IMHO not as productive as it could have been). Maybe we can finally reach a consensus on this.
From my outsider's perspective, nine months have gone by since the last LSF. Who has come up with a cleaner/better/faster way to do what Suren and Kent have done? Were those code changes integrated into this patchset? Or why not?
Most of what I saw in 2023 involved compiler changes (great; now I have to wait until RHEL 11/Debian 14 to use it) and/or still utilize fugly macros.
Recalling all the way back to suggestions made in 2022, who wrote the prototype for doing this via ftrace? Or BPF? How well did that go for counting allocation events and the like? I saw Tejun saying something about how they use BPF aggressively inside Meta, but that was about it.
Were any of those solutions significantly better than what's in front of us here?
I get it, a giant patch forcing everyone to know the difference between alloc_foo and alloc_foo_noperf offends my (yours?) stylistic sensibilities. On the other side, making analysis easier during customer escalations means we kernel people get data, answers, and solutions sooner instead of watching all our time get eaten up on L4 support and backporting hell.
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
I'd rather not delay for more bikeshedding. Before agreeing to LSF I'd need to see a serious proposl - what we had at the last LSF was people jumping in with half baked alternative proposals that very much hadn't been thought through, and I see no need to repeat that.
Like I mentioned, there's other work gated on this patchset; if people want to hold this up for more discussion they better be putting forth something to discuss.
Quoted 8 lines
Quoted 3 lines
I'm thinking of ways on how to achieve Michal's request: "as long as there is a wider agreement from the MM community". If we can achieve that without LSF, great! (a bi-weekly MM meeting might also be an option)
A meeting wouldn't be out of the question, _if_ there is an agenda, but:
What's that coffeee mug say? I just survived another meeting that could've been an email?
I congratulate you on your memory of my kitchen mug. Yes, that's what it says.
Quoted 11 lines
What exactly is the outcome we're looking for?
Is there info that people are looking for? I think we summed things up pretty well in the cover letter; if there are specifics that people want to discuss, that's why we emailed the series out.
There's people in this thread who've used this patchset in production and diagnosed real issues (gigabytes of memory gone missing, I heard the other day); I'm personally looking for them to chime in on this thread (Johannes, Pasha).
Quoted 7 lines
If it's just grumbling about "maintenance overhead" we need to get past
- well, people are going to have to accept that we can't deliver features without writing code, and I'm confident that the hooking in particular is about as clean as it's going to get, _regardless_ of toolchain support; and moreover it addresses what's been historically a pretty gaping hole in our ability to profile and understand the code we write.
Are you and Suren willing to pay whatever maintenance overhead there is?
--D
Kent Overstreet
maintainerQuoted 87 lines · David Hildenbrand
Quoted 78 lines · Suren Baghdasaryan
Quoted 76 lines · Kent Overstreet
Quoted 73 lines · David Hildenbrand
Quoted 48 lines · Suren Baghdasaryan
Quoted 47 lines · David Hildenbrand
Quoted 44 lines · Kent Overstreet
Quoted 27 lines · David Hildenbrand
Quoted 21 lines · Suren Baghdasaryan
Quoted 12 lines · Michal Hocko
On Mon 12-02-24 13:38:46, Suren Baghdasaryan wrote:
[...]Quoted 4 lines
We're aiming to get this in the next merge window, for 6.9. The feedback we've gotten has been that even out of tree this patchset has already been useful, and there's a significant amount of other work gated on the code tagging functionality included in this patchset [2].
I suspect it will not come as a surprise that I really dislike the implementation proposed here. I will not repeat my arguments, I have done so on several occasions already.
Quoted 7 lines
Anyway, I didn't go as far as to nak it even though I _strongly_ believe this debugging feature will add a maintenance overhead for a very long time. I can live with all the downsides of the proposed implementation
_aslongas_there is a wider agreement from the MM community as this is where the maintenance cost will be payed. So far I have not seen (m)any acks by MM developers so aiming into the next merge window is more than little rushed.Quoted 4 lines
We tried other previously proposed approaches and all have their downsides without making maintenance much easier. Your position is understandable and I think it's fair. Let's see if others see more benefit than cost here.
Quoted 7 lines
Would it make sense to discuss that at LSF/MM once again, especially covering why proposed alternatives did not work out? LSF/MM is not "too far" away (May).
I recall that the last LSF/MM session on this topic was a bit unfortunate (IMHO not as productive as it could have been). Maybe we can finally reach a consensus on this.
I'd rather not delay for more bikeshedding. Before agreeing to LSF I'd need to see a serious proposl - what we had at the last LSF was people jumping in with half baked alternative proposals that very much hadn't been thought through, and I see no need to repeat that.
Like I mentioned, there's other work gated on this patchset; if people want to hold this up for more discussion they better be putting forth something to discuss.
Quoted 6 lines
Quoted 3 lines
I'm thinking of ways on how to achieve Michal's request: "as long as there is a wider agreement from the MM community". If we can achieve that without LSF, great! (a bi-weekly MM meeting might also be an option)
There will be a maintenance burden even with the cleanest proposed approach.
Yes.
Quoted 3 lines
We worked hard to make the patchset as clean as possible and if benefits still don't outweigh the maintenance cost then we should probably stop trying.
Indeed.
Quoted 5 lines
At LSF/MM I would rather discuss functonal issues/requirements/improvements than alternative approaches to instrument allocators.
I'm happy to arrange a separate meeting with MM folks if that would help to progress on the cost/benefit decision.Note that I am only proposing ways forward.
If you think you can easily achieve what Michal requested without all that, good.
Quoted 7 lines
Quoted 1 line
He requested something?
Yes, a cleaner instrumentation. Unfortunately the cleanest one is not possible until the compiler feature is developed and deployed. And it still would require changes to the headers, so don't think it's worth delaying the feature for years.
Quoted 7 lines
I was talking about this: "I can live with all the downsides of the proposed implementationas long as there is a wider agreement from the MM community as this is where the maintenance cost will be payed. So far I have not seen (m)any acks by MM developers".
I certainly cannot be motivated at this point to review and ack this, unfortunately too much negative energy around here.
David, this kind of reaction is exactly why I was telling Andrew I was going to submit this as a direct pull request to Linus.
This is an important feature; if we can't stay focused ot the technical and get it done that's what I'll do.
kernel test robot
build botRe: [PATCH v3 29/35] mm: vmalloc: Enable memory allocation profiling
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 akpm-mm/mm-nonmm-unstable]
[also build test ERROR on linus/master v6.8-rc4]
[cannot apply to akpm-mm/mm-everything vbabka-slab/for-next next-20240213] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Suren-Baghdasaryan/lib-string_helpers-Add-flags-param-to-string_get_size/20240213-054335 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-nonmm-unstable patch link: https://lore.kernel.org/r/20240212213922.783301-30-surenb%40google.com patch subject: [PATCH v3 29/35] mm: vmalloc: Enable memory allocation profiling config: mips-allmodconfig (https://download.01.org/0day-ci/archive/20240214/[email protected]/config) compiler: mips-linux-gcc (GCC) 13.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240214/[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 >>):
In file included from include/linux/vmalloc.h:6,
from include/asm-generic/io.h:994,
from arch/mips/include/asm/io.h:563,
from arch/mips/include/asm/page.h:200,
from include/linux/mm_types_task.h:13,
from include/linux/mm_types.h:5,
from include/linux/uio.h:10,
from include/linux/socket.h:8,
from include/linux/compat.h:15,
from arch/mips/kernel/asm-offsets.c:12:
>> include/linux/sched.h:1338:41: error: field 'tlb_ubc' has incomplete type
1338 | struct tlbflush_unmap_batch tlb_ubc;
| ^~~~~~~
>> include/linux/sched.h:1343:41: error: field 'task_frag' has incomplete type
1343 | struct page_frag task_frag;
| ^~~~~~~~~ make[3]: *** [scripts/Makefile.build:116: arch/mips/kernel/asm-offsets.s] Error 1 make[3]: Target 'prepare' not remade because of errors.
make[2]: *** [Makefile:1191: prepare0] Error 2
make[2]: Target 'prepare' not remade because of errors.
make[1]: *** [Makefile:240: __sub-make] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:240: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.
vim +/tlb_ubc +1338 include/linux/sched.h
af7f588d8f7355 Mathieu Desnoyers 2022-11-22 1337
72b252aed506b8 Mel Gorman 2015-09-04 @1338 struct tlbflush_unmap_batch tlb_ubc; 72b252aed506b8 Mel Gorman 2015-09-04 1339 5eca1c10cbaa9c Ingo Molnar 2017-02-06 1340 /* Cache last used pipe for splice(): */ b92ce55893745e Jens Axboe 2006-04-11 1341 struct pipe_inode_info *splice_pipe; 5640f7685831e0 Eric Dumazet 2012-09-23 1342 5640f7685831e0 Eric Dumazet 2012-09-23 @1343 struct page_frag task_frag; 5640f7685831e0 Eric Dumazet 2012-09-23 1344
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Quoted 93 lines · Kent Overstreet
Quoted 92 lines · David Hildenbrand
Quoted 78 lines · Suren Baghdasaryan
Quoted 76 lines · Kent Overstreet
Quoted 73 lines · David Hildenbrand
Quoted 48 lines · Suren Baghdasaryan
Quoted 47 lines · David Hildenbrand
Quoted 44 lines · Kent Overstreet
Quoted 27 lines · David Hildenbrand
Quoted 21 lines · Suren Baghdasaryan
Quoted 12 lines · Michal Hocko
On Mon 12-02-24 13:38:46, Suren Baghdasaryan wrote:
[...]Quoted 4 lines
We're aiming to get this in the next merge window, for 6.9. The feedback we've gotten has been that even out of tree this patchset has already been useful, and there's a significant amount of other work gated on the code tagging functionality included in this patchset [2].
I suspect it will not come as a surprise that I really dislike the implementation proposed here. I will not repeat my arguments, I have done so on several occasions already.
Quoted 7 lines
Anyway, I didn't go as far as to nak it even though I _strongly_ believe this debugging feature will add a maintenance overhead for a very long time. I can live with all the downsides of the proposed implementation
_aslongas_there is a wider agreement from the MM community as this is where the maintenance cost will be payed. So far I have not seen (m)any acks by MM developers so aiming into the next merge window is more than little rushed.Quoted 4 lines
We tried other previously proposed approaches and all have their downsides without making maintenance much easier. Your position is understandable and I think it's fair. Let's see if others see more benefit than cost here.
Quoted 7 lines
Would it make sense to discuss that at LSF/MM once again, especially covering why proposed alternatives did not work out? LSF/MM is not "too far" away (May).
I recall that the last LSF/MM session on this topic was a bit unfortunate (IMHO not as productive as it could have been). Maybe we can finally reach a consensus on this.
I'd rather not delay for more bikeshedding. Before agreeing to LSF I'd need to see a serious proposl - what we had at the last LSF was people jumping in with half baked alternative proposals that very much hadn't been thought through, and I see no need to repeat that.
Like I mentioned, there's other work gated on this patchset; if people want to hold this up for more discussion they better be putting forth something to discuss.
Quoted 6 lines
Quoted 3 lines
I'm thinking of ways on how to achieve Michal's request: "as long as there is a wider agreement from the MM community". If we can achieve that without LSF, great! (a bi-weekly MM meeting might also be an option)
There will be a maintenance burden even with the cleanest proposed approach.
Yes.
Quoted 3 lines
We worked hard to make the patchset as clean as possible and if benefits still don't outweigh the maintenance cost then we should probably stop trying.
Indeed.
Quoted 5 lines
At LSF/MM I would rather discuss functonal issues/requirements/improvements than alternative approaches to instrument allocators.
I'm happy to arrange a separate meeting with MM folks if that would help to progress on the cost/benefit decision.Note that I am only proposing ways forward.
If you think you can easily achieve what Michal requested without all that, good.
Quoted 7 lines
Quoted 1 line
He requested something?
Yes, a cleaner instrumentation. Unfortunately the cleanest one is not possible until the compiler feature is developed and deployed. And it still would require changes to the headers, so don't think it's worth delaying the feature for years.
I was talking about this: "I can live with all the downsides of the proposed implementationas long as there is a wider agreement from the MM community as this is where the maintenance cost will be payed. So far I have not seen (m)any acks by MM developers".
Quoted 8 lines
Quoted 2 lines
I certainly cannot be motivated at this point to review and ack this, unfortunately too much negative energy around here.
David, this kind of reaction is exactly why I was telling Andrew I was going to submit this as a direct pull request to Linus.
This is an important feature; if we can't stay focused ot the technical and get it done that's what I'll do.
Kent, I started this with "Would it make sense" in an attempt to help Suren and you to finally make progress with this, one way or the other. I know that there were ways in the past to get the MM community to agree on such things.
I tried to be helpful, finding ways *not having to* bypass the MM community to get MM stuff merged.
The reply I got is mostly negative energy.
So you don't need my help here, understood.
But I will fight against any attempts to bypass the MM community.
Signature · 3 lines
Cheers, David / dhildenb
Kent Overstreet
maintainerQuoted 121 lines · Darrick J. Wong
Quoted 25 lines · Kent Overstreet
Quoted 24 lines · David Hildenbrand
Quoted 23 lines · Kent Overstreet
Quoted 22 lines · David Hildenbrand
Quoted 21 lines · Suren Baghdasaryan
Quoted 19 lines · Michal Hocko
On Mon 12-02-24 13:38:46, Suren Baghdasaryan wrote:
[...]Quoted 4 lines
We're aiming to get this in the next merge window, for 6.9. The feedback we've gotten has been that even out of tree this patchset has already been useful, and there's a significant amount of other work gated on the code tagging functionality included in this patchset [2].
I suspect it will not come as a surprise that I really dislike the implementation proposed here. I will not repeat my arguments, I have done so on several occasions already.
Anyway, I didn't go as far as to nak it even though I _strongly_ believe this debugging feature will add a maintenance overhead for a very long time. I can live with all the downsides of the proposed implementation
_aslongas_there is a wider agreement from the MM community as this is where the maintenance cost will be payed. So far I have not seen (m)any acks by MM developers so aiming into the next merge window is more than little rushed.Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
We tried other previously proposed approaches and all have their downsides without making maintenance much easier. Your position is understandable and I think it's fair. Let's see if others see more benefit than cost here.
Would it make sense to discuss that at LSF/MM once again, especially covering why proposed alternatives did not work out? LSF/MM is not "too far" away (May).
You want to stall this effort for *three months* to schedule a meeting?
I would love to have better profiling of memory allocations inside XFS so that we can answer questions like "What's going on with these allocation stalls?" or "What memory is getting used, and where?" more quickly than we can now.
Right now I get to stare at tracepoints and printk crap until my eyes bleed, and maybe dchinner comes to my rescue and figures out what's going on sooner than I do. More often we just never figure it out because only the customer can reproduce the problem, the reams of data produced by ftrace is unmanageable, and BPF isn't always available.
I'm not thrilled by the large increase in macro crap in the allocation paths, but I don't know of a better way to instrument things. Our attempts to use
_RET_IPin XFS to instrument its code paths have never worked quite right w.r.t. inlined functions and whatnot.Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
I recall that the last LSF/MM session on this topic was a bit unfortunate (IMHO not as productive as it could have been). Maybe we can finally reach a consensus on this.
From my outsider's perspective, nine months have gone by since the last LSF. Who has come up with a cleaner/better/faster way to do what Suren and Kent have done? Were those code changes integrated into this patchset? Or why not?
Most of what I saw in 2023 involved compiler changes (great; now I have to wait until RHEL 11/Debian 14 to use it) and/or still utilize fugly macros.
Recalling all the way back to suggestions made in 2022, who wrote the prototype for doing this via ftrace? Or BPF? How well did that go for counting allocation events and the like? I saw Tejun saying something about how they use BPF aggressively inside Meta, but that was about it.
Were any of those solutions significantly better than what's in front of us here?
I get it, a giant patch forcing everyone to know the difference between
alloc_fooandalloc_foo_noperfoffends my (yours?) stylistic sensibilities. On the other side, making analysis easier during customer escalations means we kernel people get data, answers, and solutions sooner instead of watching all our time get eaten up on L4 support and backporting hell.Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
I'd rather not delay for more bikeshedding. Before agreeing to LSF I'd need to see a serious proposl - what we had at the last LSF was people jumping in with half baked alternative proposals that very much hadn't been thought through, and I see no need to repeat that.
Like I mentioned, there's other work gated on this patchset; if people want to hold this up for more discussion they better be putting forth something to discuss.
Quoted 8 lines
Quoted 3 lines
I'm thinking of ways on how to achieve Michal's request: "as long as there is a wider agreement from the MM community". If we can achieve that without LSF, great! (a bi-weekly MM meeting might also be an option)
A meeting wouldn't be out of the question, _if_ there is an agenda, but:
What's that coffeee mug say? I just survived another meeting that could've been an email?
I congratulate you on your memory of my kitchen mug. Yes, that's what it says.
Quoted 11 lines
What exactly is the outcome we're looking for?
Is there info that people are looking for? I think we summed things up pretty well in the cover letter; if there are specifics that people want to discuss, that's why we emailed the series out.
There's people in this thread who've used this patchset in production and diagnosed real issues (gigabytes of memory gone missing, I heard the other day); I'm personally looking for them to chime in on this thread (Johannes, Pasha).
Quoted 7 lines
If it's just grumbling about "maintenance overhead" we need to get past
- well, people are going to have to accept that we can't deliver features without writing code, and I'm confident that the hooking in particular is about as clean as it's going to get, _regardless_ of toolchain support; and moreover it addresses what's been historically a pretty gaping hole in our ability to profile and understand the code we write.
Quoted 1 line
Are you and Suren willing to pay whatever maintenance overhead there is?
I'm still wondering what this supposed "maintenance overhead" is going to be...
As I use this patch series I occasionally notice places where a bunch of memory is being accounted to one line of code, and it would better be accounted to a caller - but then it's just a couple lines of code to fix that. You switch that callsite to the _noprof() version of whatever allocation it's doing, then add an alloc_hooks() wrapper at the place you do want it accounted.
That doesn't really feel like overhead to me, just the normal tweaking your tools to get the most out of them.
I will continue to do that for the code I'm looking at, yes.
If other people are doing that too, it'll be because they're also using memory allocation profiling and finding it valuable.
I did notice earlier that we're still lacking documentation in the Documentation/ directory; the workflow for "how you shift accounting to the right spot" is something that should go in there.
Suren Baghdasaryan
maintainerQuoted 109 lines · David Hildenbrand
Quoted 93 lines · Kent Overstreet
Quoted 92 lines · David Hildenbrand
Quoted 78 lines · Suren Baghdasaryan
Quoted 76 lines · Kent Overstreet
Quoted 73 lines · David Hildenbrand
Quoted 48 lines · Suren Baghdasaryan
Quoted 47 lines · David Hildenbrand
Quoted 44 lines · Kent Overstreet
Quoted 27 lines · David Hildenbrand
Quoted 21 lines · Suren Baghdasaryan
Quoted 12 lines · Michal Hocko
On Mon 12-02-24 13:38:46, Suren Baghdasaryan wrote:
[...]Quoted 4 lines
We're aiming to get this in the next merge window, for 6.9. The feedback
we've gotten has been that even out of tree this patchset has already
been useful, and there's a significant amount of other work gated on the
code tagging functionality included in this patchset [2].I suspect it will not come as a surprise that I really dislike the implementation proposed here. I will not repeat my arguments, I have done so on several occasions already.
Quoted 7 lines
Anyway, I didn't go as far as to nak it even though I _strongly_ believe this debugging feature will add a maintenance overhead for a very long time. I can live with all the downsides of the proposed implementation
_aslongas_there is a wider agreement from the MM community as this is where the maintenance cost will be payed. So far I have not seen (m)any acks by MM developers so aiming into the next merge window is more than little rushed.Quoted 4 lines
We tried other previously proposed approaches and all have their downsides without making maintenance much easier. Your position is understandable and I think it's fair. Let's see if others see more benefit than cost here.
Quoted 7 lines
Would it make sense to discuss that at LSF/MM once again, especially covering why proposed alternatives did not work out? LSF/MM is not "too far" away (May).
I recall that the last LSF/MM session on this topic was a bit unfortunate (IMHO not as productive as it could have been). Maybe we can finally reach a consensus on this.
I'd rather not delay for more bikeshedding. Before agreeing to LSF I'd need to see a serious proposl - what we had at the last LSF was people jumping in with half baked alternative proposals that very much hadn't been thought through, and I see no need to repeat that.
Like I mentioned, there's other work gated on this patchset; if people want to hold this up for more discussion they better be putting forth something to discuss.
Quoted 6 lines
Quoted 3 lines
I'm thinking of ways on how to achieve Michal's request: "as long as there is a wider agreement from the MM community". If we can achieve that without LSF, great! (a bi-weekly MM meeting might also be an option)
There will be a maintenance burden even with the cleanest proposed approach.
Yes.
Quoted 3 lines
We worked hard to make the patchset as clean as possible and if benefits still don't outweigh the maintenance cost then we should probably stop trying.
Indeed.
Quoted 5 lines
At LSF/MM I would rather discuss functonal issues/requirements/improvements than alternative approaches to instrument allocators.
I'm happy to arrange a separate meeting with MM folks if that would help to progress on the cost/benefit decision.Note that I am only proposing ways forward.
If you think you can easily achieve what Michal requested without all that, good.
Quoted 7 lines
Quoted 1 line
He requested something?
Yes, a cleaner instrumentation. Unfortunately the cleanest one is not possible until the compiler feature is developed and deployed. And it still would require changes to the headers, so don't think it's worth delaying the feature for years.
I was talking about this: "I can live with all the downsides of the proposed implementationas long as there is a wider agreement from the MM community as this is where the maintenance cost will be payed. So far I have not seen (m)any acks by MM developers".
Quoted 8 lines
Quoted 2 lines
I certainly cannot be motivated at this point to review and ack this, unfortunately too much negative energy around here.
David, this kind of reaction is exactly why I was telling Andrew I was going to submit this as a direct pull request to Linus.
This is an important feature; if we can't stay focused ot the technical and get it done that's what I'll do.
Kent, I started this with "Would it make sense" in an attempt to help Suren and you to finally make progress with this, one way or the other. I know that there were ways in the past to get the MM community to agree on such things.
Quoted 8 lines
I tried to be helpful, finding ways *not having to* bypass the MM community to get MM stuff merged.
The reply I got is mostly negative energy.
So you don't need my help here, understood.
But I will fight against any attempts to bypass the MM community.
Well, I'm definitely not trying to bypass the MM community, that's why this patchset is posted. Not sure why people can't voice their opinion on the benefit/cost balance of the patchset over the email... But if a meeting would be more productive I'm happy to set it up.
Quoted 6 lines
--
Cheers,David / dhildenb
Quoted 5 lines
Quoted 5 lines
I tried to be helpful, finding ways *not having to* bypass the MM community to get MM stuff merged.
The reply I got is mostly negative energy.
Quoted 8 lines
Quoted 3 lines
So you don't need my help here, understood.
But I will fight against any attempts to bypass the MM community.
Well, I'm definitely not trying to bypass the MM community, that's why this patchset is posted. Not sure why people can't voice their opinion on the benefit/cost balance of the patchset over the email... But if a meeting would be more productive I'm happy to set it up.
Discussing these concerns during the next available MM Alignment session makes sense. At a minimum, Suren and Kent can present their reasons for believing the current approach is superior to the previously proposed alternatives.
However, delaying the discussion and feature merge until after LSF/MM seems unnecessary. As I mentioned earlier in this thread, we've already leveraged the concepts within this feature to debug unexplained memory overhead, saving us many terabytes of memory. This was just the initial benefit; we haven't even explored its full potential to track every allocation path.
Pasha
Kent Overstreet
maintainerRe: [PATCH v3 01/35] lib/string_helpers: Add flags param to string_get_size()
In reply to Andy ShevchenkoQuoted 7 lines · Andy Shevchenko
Quoted 5 lines · Andy Shevchenko
Quoted 3 lines · Suren Baghdasaryan
From: Kent Overstreet <[email protected]>
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines
The new flags parameter allows controlling
- Whether or not the units suffix is separated by a space, for compatibility with sort -h
- Whether or not to append a B suffix - we're not always printing bytes.And you effectively missed to _add_ the test cases for the modified code. Formal NAK for this, the rest is discussable, the absence of tests is not.
Eh?
The core algorihtm for printing out a number in human readable units; that's definitely worth a test, and I assume there already is one - I didn't touch that.
But whether or not the units suffix has a space, or a B suffix? That's not going to break in subtle ways; that either works or it doesn't.
Kent Overstreet
maintainerQuoted 14 lines · Pasha Tatashin
Quoted 5 lines
Quoted 5 lines
I tried to be helpful, finding ways *not having to* bypass the MM community to get MM stuff merged.
The reply I got is mostly negative energy.
Quoted 8 lines
Quoted 3 lines
So you don't need my help here, understood.
But I will fight against any attempts to bypass the MM community.
Well, I'm definitely not trying to bypass the MM community, that's why this patchset is posted. Not sure why people can't voice their opinion on the benefit/cost balance of the patchset over the email... But if a meeting would be more productive I'm happy to set it up.
Quoted 4 lines
Discussing these concerns during the next available MM Alignment session makes sense. At a minimum, Suren and Kent can present their reasons for believing the current approach is superior to the previously proposed alternatives.
Hang on though: I believe we did so adequately within this thread. Both in the cover letter, and I further outlined exactly what the hooks need to do, and cited the exact code.
Nobody seems to be complaining about the specifics, so I'm not sure what would be on the agenda?
kernel test robot
build botRe: [PATCH v3 22/35] mm/slab: enable slab allocation tagging for kmalloc and friends
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 akpm-mm/mm-nonmm-unstable]
[also build test ERROR on linus/master v6.8-rc4]
[cannot apply to akpm-mm/mm-everything vbabka-slab/for-next next-20240213] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Suren-Baghdasaryan/lib-string_helpers-Add-flags-param-to-string_get_size/20240213-054335 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-nonmm-unstable patch link: https://lore.kernel.org/r/20240212213922.783301-23-surenb%40google.com patch subject: [PATCH v3 22/35] mm/slab: enable slab allocation tagging for kmalloc and friends config: x86_64-rhel-8.3-rust (https://download.01.org/0day-ci/archive/20240214/[email protected]/config) compiler: clang version 17.0.6 (https://github.com/llvm/llvm-project 6009708b4367171ccdbf4b5905cb6a803753fe18) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240214/[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 >>):
>> error[E0425]: cannot find function `krealloc` in crate `bindings`
--> rust/kernel/allocator.rs:39:24
|
39 | unsafe { bindings::krealloc(ptr as *const core::ffi::c_void, size, flags) as *mut u8 }
| ^^^^^^^^ not found in bindings
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Johannes Weiner
maintainerI'll do a more throrough code review, but before the discussion gets too sidetracked, I wanted to add my POV on the overall merit of the direction that is being proposed here.
I have backported and used this code for debugging production issues before. Logging into a random host with an unfamiliar workload and being able to get a reliable, comprehensive list of kernel memory consumers is one of the coolest things I have seen in a long time. This is a huge improvement to sysadmin quality of life.
It's also a huge improvement for MM developers. We're the first points of contact for memory regressions that can be caused by pretty much any driver or subsystem in the kernel.
I encourage anybody who is undecided on whether this is worth doing to build a kernel with these patches applied and run it on their own machine. I think you'll be surprised what you'll find - and how myopic and uninformative /proc/meminfo feels in comparison to this. Did you know there is a lot more to modern filesystems than the VFS objects we are currently tracking? :)
Then imagine what this looks like on a production host running a complex mix of filesystems, enterprise networking, bpf programs, gpus and accelerators etc.
Backporting the code to a slightly older production kernel wasn't too difficult. The instrumentation layering is explicit, clean, and fairly centralized, so resolving minor conflicts around the _noprof renames and the wrappers was pretty straight-forward.
When we talk about maintenance cost, a fair shake would be to weigh it against the cost and reliability of our current method: evaluating consumers in the kernel on a case-by-case basis and annotating the alloc/free sites by hand; then quibbling with the MM community about whether that consumer is indeed significant enough to warrant an entry in /proc/meminfo, and what the catchiest name for the stat would be.
I think we can agree that this is vastly less scalable and more burdensome than central annotations around a handful of mostly static allocator entry points. Especially considering the rate of change in the kernel as a whole, and that not everybody will think of the comprehensive MM picture when writing a random driver. And I think that's generous - we don't even have the network stack in meminfo.
So I think what we do now isn't working. In the Meta fleet, at any given time the p50 for unaccounted kernel memory is several gigabytes per host. The p99 is between 15% and 30% of total memory. That's a looot of opaque resource usage we have to accept on faith.
For hunting down regressions, all it takes is one untracked consumer in the kernel to really throw a wrench into things. It's difficult to find in the noise with tracing, and if it's not growing after an initial allocation spike, you're pretty much out of luck finding it at all. Raise your hand if you've written a drgn script to walk pfns and try to guess consumers from the state of struct page :)
I agree we should discuss how the annotations are implemented on a technical basis, but my take is that we need something like this.
In a codebase of our size, I don't think the allocator should be handing out memory without some basic implied tracking of where it's going. It's a liability for production environments, and it can hide bad memory management decisions in drivers and other subsystems for a very long time.
kernel test robot
build botRe: [PATCH v3 13/35] lib: add allocation tagging support for memory allocation profiling
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 akpm-mm/mm-nonmm-unstable]
[also build test ERROR on linus/master v6.8-rc4]
[cannot apply to akpm-mm/mm-everything vbabka-slab/for-next next-20240213] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Suren-Baghdasaryan/lib-string_helpers-Add-flags-param-to-string_get_size/20240213-054335 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-nonmm-unstable patch link: https://lore.kernel.org/r/20240212213922.783301-14-surenb%40google.com patch subject: [PATCH v3 13/35] lib: add allocation tagging support for memory allocation profiling config: arc-randconfig-r061-20240214 (https://download.01.org/0day-ci/archive/20240214/[email protected]/config) compiler: arc-elf-gcc (GCC) 13.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240214/[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 >>):
In file included from include/linux/kernel.h:31,
from include/linux/cpumask.h:10, from include/linux/smp.h:13, from include/linux/lockdep.h:14, from include/linux/radix-tree.h:14, from include/linux/idr.h:15, from lib/codetag.c:3:
lib/codetag.c: In function 'codetag_module_init':
>> lib/codetag.c:147:34: error: invalid use of undefined type 'struct module'
147 | mod ? mod->name : "(built-in)");
| ^~
include/linux/printk.h:427:33: note: in definition of macro 'printk_index_wrap'
427 | _p_func(_fmt, ##__VA_ARGS__); \
| ^~~~~~~~~~~
include/linux/printk.h:508:9: note: in expansion of macro 'printk'
508 | printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
| ^~~~~~
lib/codetag.c:145:17: note: in expansion of macro 'pr_warn'
145 | pr_warn("Failed to load code tags of type %s from the module %s\n",
| ^~~~~~~
--
In file included from ./arch/arc/include/generated/asm/percpu.h:1,
from include/linux/alloc_tag.h:12,
from lib/alloc_tag.c:2:
include/linux/alloc_tag.h: In function '__alloc_tag_sub':
>> include/asm-generic/percpu.h:155:9: error: implicit declaration of function 'raw_local_irq_save' [-Werror=implicit-function-declaration]
155 | raw_local_irq_save(__flags); \
| ^~~~~~~~~~~~~~~~~~
include/asm-generic/percpu.h:401:41: note: in expansion of macro 'this_cpu_generic_to_op'
401 | #define this_cpu_add_1(pcp, val) this_cpu_generic_to_op(pcp, val, +=)
| ^~~~~~~~~~~~~~~~~~~~~~
include/linux/percpu-defs.h:365:25: note: in expansion of macro 'this_cpu_add_1'
365 | case 1: stem##1(variable, __VA_ARGS__);break; \
| ^~~~
include/linux/percpu-defs.h:491:41: note: in expansion of macro '__pcpu_size_call'
491 | #define this_cpu_add(pcp, val) __pcpu_size_call(this_cpu_add_, pcp, val)
| ^~~~~~~~~~~~~~~~
include/linux/percpu-defs.h:500:41: note: in expansion of macro 'this_cpu_add'
500 | #define this_cpu_sub(pcp, val) this_cpu_add(pcp, -(typeof(pcp))(val))
| ^~~~~~~~~~~~
include/linux/alloc_tag.h:90:9: note: in expansion of macro 'this_cpu_sub'
90 | this_cpu_sub(tag->counters->bytes, bytes);
| ^~~~~~~~~~~~
>> include/asm-generic/percpu.h:157:9: error: implicit declaration of function 'raw_local_irq_restore' [-Werror=implicit-function-declaration]
157 | raw_local_irq_restore(__flags); \
| ^~~~~~~~~~~~~~~~~~~~~
include/asm-generic/percpu.h:401:41: note: in expansion of macro 'this_cpu_generic_to_op'
401 | #define this_cpu_add_1(pcp, val) this_cpu_generic_to_op(pcp, val, +=)
| ^~~~~~~~~~~~~~~~~~~~~~
include/linux/percpu-defs.h:365:25: note: in expansion of macro 'this_cpu_add_1'
365 | case 1: stem##1(variable, __VA_ARGS__);break; \
| ^~~~
include/linux/percpu-defs.h:491:41: note: in expansion of macro '__pcpu_size_call'
491 | #define this_cpu_add(pcp, val) __pcpu_size_call(this_cpu_add_, pcp, val)
| ^~~~~~~~~~~~~~~~
include/linux/percpu-defs.h:500:41: note: in expansion of macro 'this_cpu_add'
500 | #define this_cpu_sub(pcp, val) this_cpu_add(pcp, -(typeof(pcp))(val))
| ^~~~~~~~~~~~
include/linux/alloc_tag.h:90:9: note: in expansion of macro 'this_cpu_sub'
90 | this_cpu_sub(tag->counters->bytes, bytes);
| ^~~~~~~~~~~~cc1: some warnings being treated as errors
vim +/raw_local_irq_save +155 include/asm-generic/percpu.h
e88d62cd4b2f0b Mark Rutland 2017-09-26 151
eba117889ac444 Tejun Heo 2014-06-17 152 #define this_cpu_generic_to_op(pcp, val, op) \
9c28278a24c01c Tejun Heo 2014-06-17 153 do { \
eba117889ac444 Tejun Heo 2014-06-17 154 unsigned long __flags; \
eba117889ac444 Tejun Heo 2014-06-17 @155 raw_local_irq_save(__flags); \
1b5ca12127427c Nicholas Piggin 2016-09-22 156 raw_cpu_generic_to_op(pcp, val, op); \
eba117889ac444 Tejun Heo 2014-06-17 @157 raw_local_irq_restore(__flags); \
9c28278a24c01c Tejun Heo 2014-06-17 158 } while (0)9c28278a24c01c Tejun Heo 2014-06-17 1591b5ca12127427c Nicholas Piggin 2016-09-22 160
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Quoted 115 lines · Suren Baghdasaryan
Quoted 114 lines · David Hildenbrand
Quoted 93 lines · Kent Overstreet
Quoted 92 lines · David Hildenbrand
Quoted 78 lines · Suren Baghdasaryan
Quoted 76 lines · Kent Overstreet
Quoted 73 lines · David Hildenbrand
Quoted 48 lines · Suren Baghdasaryan
Quoted 47 lines · David Hildenbrand
Quoted 44 lines · Kent Overstreet
Quoted 27 lines · David Hildenbrand
Quoted 21 lines · Suren Baghdasaryan
Quoted 19 lines · Michal Hocko
On Mon 12-02-24 13:38:46, Suren Baghdasaryan wrote:
[...]
We're aiming to get this in the next merge window, for 6.9. The feedback
we've gotten has been that even out of tree this patchset has already
been useful, and there's a significant amount of other work gated on the
code tagging functionality included in this patchset [2].I suspect it will not come as a surprise that I really dislike the
implementation proposed here. I will not repeat my arguments, I have
done so on several occasions already.Anyway, I didn't go as far as to nak it even though I _strongly_ believe
this debugging feature will add a maintenance overhead for a very long
time. I can live with all the downsides of the proposed implementation
_as long as_ there is a wider agreement from the MM community as this is
where the maintenance cost will be payed. So far I have not seen (m)any
acks by MM developers so aiming into the next merge window is more than
little rushed.Quoted 4 lines
We tried other previously proposed approaches and all have their downsides without making maintenance much easier. Your position is understandable and I think it's fair. Let's see if others see more benefit than cost here.
Quoted 7 lines
Would it make sense to discuss that at LSF/MM once again, especially covering why proposed alternatives did not work out? LSF/MM is not "too far" away (May).
I recall that the last LSF/MM session on this topic was a bit unfortunate (IMHO not as productive as it could have been). Maybe we can finally reach a consensus on this.
I'd rather not delay for more bikeshedding. Before agreeing to LSF I'd need to see a serious proposl - what we had at the last LSF was people jumping in with half baked alternative proposals that very much hadn't been thought through, and I see no need to repeat that.
Like I mentioned, there's other work gated on this patchset; if people want to hold this up for more discussion they better be putting forth something to discuss.
Quoted 6 lines
Quoted 3 lines
I'm thinking of ways on how to achieve Michal's request: "as long as there is a wider agreement from the MM community". If we can achieve that without LSF, great! (a bi-weekly MM meeting might also be an option)
There will be a maintenance burden even with the cleanest proposed approach.
Yes.
Quoted 3 lines
We worked hard to make the patchset as clean as possible and if benefits still don't outweigh the maintenance cost then we should probably stop trying.
Indeed.
Quoted 5 lines
At LSF/MM I would rather discuss functonal issues/requirements/improvements than alternative approaches to instrument allocators.
I'm happy to arrange a separate meeting with MM folks if that would help to progress on the cost/benefit decision.Note that I am only proposing ways forward.
If you think you can easily achieve what Michal requested without all that, good.
Quoted 7 lines
Quoted 1 line
He requested something?
Yes, a cleaner instrumentation. Unfortunately the cleanest one is not possible until the compiler feature is developed and deployed. And it still would require changes to the headers, so don't think it's worth delaying the feature for years.
I was talking about this: "I can live with all the downsides of the proposed implementationas long as there is a wider agreement from the MM community as this is where the maintenance cost will be payed. So far I have not seen (m)any acks by MM developers".
Quoted 8 lines
Quoted 2 lines
I certainly cannot be motivated at this point to review and ack this, unfortunately too much negative energy around here.
David, this kind of reaction is exactly why I was telling Andrew I was going to submit this as a direct pull request to Linus.
This is an important feature; if we can't stay focused ot the technical and get it done that's what I'll do.
Kent, I started this with "Would it make sense" in an attempt to help Suren and you to finally make progress with this, one way or the other. I know that there were ways in the past to get the MM community to agree on such things.
I tried to be helpful, finding ways *not having to* bypass the MM community to get MM stuff merged.
The reply I got is mostly negative energy.
Quoted 8 lines
Quoted 3 lines
So you don't need my help here, understood.
But I will fight against any attempts to bypass the MM community.
Well, I'm definitely not trying to bypass the MM community, that's why this patchset is posted. Not sure why people can't voice their opinion on the benefit/cost balance of the patchset over the email... But if a meeting would be more productive I'm happy to set it up.
If you can get the acks without any additional meetings, great. The replies from Pasha and Johannes are encouraging, let's hope core memory-allocator people will voice their opinion here.
If you come to the conclusion that another meeting would help getting maintainers's attention and sorting out some of the remaining concerns, feel free to schedule a meeting with Dave R. I suspect only the slot next week is already taken. In the past, we also had "special" meetings just for things to make progress faster.
If you're looking for ideas on what the agenda of such a meeting could look like, I'll happily discuss that with you off-list.
v2 was more than 3 months ago. If it's really about minor details here, waiting another 3 months for LSF/MM is indeed not reasonable.
Myself, I'll be happy not having to sit through another LSF/MM session of that kind. The level of drama is exceptional and I'm hoping it won't be the new norm in the MM space.
Good luck!
Signature · 3 lines
Cheers, David / dhildenb
Quoted 37 lines · Kent Overstreet
Quoted 9 lines · Suren Baghdasaryan
Quoted 7 lines · Kent Overstreet
Quoted 4 lines · David Hildenbrand
Quoted 1 line · Suren Baghdasaryan
On Tue, Feb 13, 2024 at 2:17 PM David Hildenbrand <[email protected]> wrote:
If you think you can easily achieve what Michal requested without all that, good.
Quoted 6 lines
Quoted 1 line
He requested something?
Yes, a cleaner instrumentation. Unfortunately the cleanest one is not possible until the compiler feature is developed and deployed. And it still would require changes to the headers, so don't think it's worth delaying the feature for years.
Hang on, let's look at the actual code.
This is what instrumenting an allocation function looks like:
#define
krealloc_array(...)alloc_hooks(krealloc_array_noprof(__VA_ARGS__))IOW, we have to:
- renamekrealloc_arraytokrealloc_array_noprof
- replacekrealloc_arraywith a one wrapper macro callIs this really all we're getting worked up over?
The renaming we need regardless, because the thing that makes this approach efficient enough to run in production is that we account at _one_ point in the callstack, we don't save entire backtraces.
And thus we need to explicitly annotate which one that is; which means we need
_noprof()versions of functions for when the accounting is done by an outer wraper (e.g. mempool).
Quoted 6 lines
And, as I keep saying: that
alloc_hooks()macro will also get us_percallsite fault injectionpoints_, and we really need that because - if you guys have been paying attention to other threads - whenever moving more stuff to PF_MEMALLOC_* flags comes up (including addingPF_MEMALLOC_NORECLAIM), the issue of small allocations not failing and not being testable keeps coming up.
How exactly do you envision the fault injection to help here? The proposals are about scoping via a process flag, and the process may then call just about anything under that scope. So if our tool is per callsite fault injection points, how do we know which callsites to enable to focus the fault injection on the particular scope?
Michal Hocko
reviewerQuoted 4 lines · Suren Baghdasaryan
Quoted 2 lines · Kent Overstreet
On Tue, Feb 13, 2024 at 11:48:41PM +0100, David Hildenbrand wrote:
[...]
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
If you think you can easily achieve what Michal requested without all that, good.
He requested something?
Yes, a cleaner instrumentation.
Nope, not really. You have indicated you want to target this version for the _next_ merge window without any acks, really. If you want to go forward with this then you should gain a support from the MM community at least. Why? Because the whole macro layering is adding maintenance cost for MM people.
I have expressed why I absolutely hate the additional macro layer. We have been through similar layers of macros in other areas (not to mention page allocator interface itself) and it has _always_ turned out a bad idea long term. I do not see why this case should be any different.
The whole kernel is moving to a dynamic tracing realm and now we are going to build a statically macro based tracing infrastructure which will need tweaking anytime real memory consumers are one layer up the existing macro infrastructure (do not forget quite a lot of allocations are in library functions) and/or we need to modify the allocator API in some way. Call me unimpressed!
Now, I fully recognize that the solution doesn't really have to be perfect in order to be useful. Hence I never NAKed it even though I really _dislike_ the approach. I have expected you will grow the community support over time if this is indeed the only feasible approach but that is not reflected in the series posted here. If you find a support I will not stand in the way.
Quoted 4 lines
Unfortunately the cleanest one is not
possible until the compiler feature is developed and deployed. And it still would require changes to the headers, so don't think it's worth delaying the feature for years.
I am pretty sure you have invested a non-trivial time into evaluating other ways, yet your cover letter is rather modest about any details:
: - looked at alternate hooking methods.
: There were suggestions on alternate methods (compiler attribute, : trampolines), but they wouldn't have made the patchset any cleaner : (we still need to have different function versions for accounting vs. no : accounting to control at which point in a call chain the accounting : happens), and they would have added a dependency on toolchain : support.
First immediate question would be: What about page_owner? I do remember the runtime overhead being discussed but I do not really remember any actual numbers outside of artificial workloads. Has this been investigated? Is our stack unwinder the problem? Etc.
Also what are the biggest obstacles to efficiently track allocations via our tracing infrastructure? Has this been investigated? What were conclusions?
Signature · 2 lines
Michal Hocko SUSE Labs
Michal Hocko
reviewerOn Wed 14-02-24 01:20:20, Johannes Weiner wrote:
[...]
Quoted 2 lines
I agree we should discuss how the annotations are implemented on a technical basis, but my take is that we need something like this.
I do not think there is any disagreement on usefulness of a better memory allocation tracking. At least for me the primary problem is the implementation. At LFSMM last year we have heard that existing tracing infrastructure hasn't really been explored much. Cover letter doesn't really talk much about those alternatives so it is really hard to evaluate whether the proposed solution is indeed our best way to approach this.
Quoted 5 lines
In a codebase of our size, I don't think the allocator should be handing out memory without some basic implied tracking of where it's going. It's a liability for production environments, and it can hide bad memory management decisions in drivers and other subsystems for a very long time.
Fully agreed! It is quite common to see oom reports with a large portion of memory unaccounted and this really presents additional cost on the debugging side.
Signature · 2 lines
Michal Hocko SUSE Labs
Matthew Wilcox
maintainerQuoted 8 lines · Kent Overstreet
This is what instrumenting an allocation function looks like:
#define
krealloc_array(...)alloc_hooks(krealloc_array_noprof(__VA_ARGS__))IOW, we have to:
- renamekrealloc_arraytokrealloc_array_noprof
- replacekrealloc_arraywith a one wrapper macro call
Quoted 5 lines
Is this really all we're getting worked up over?
The renaming we need regardless, because the thing that makes this approach efficient enough to run in production is that we account at _one_ point in the callstack, we don't save entire backtraces.
I'm probably going to regret getting involved in this thread, but since Suren already decided to put me on the cc ...
There might be a way to do it without renaming. We have a bit of the linker script called SCHED_TEXT which lets us implement in_sched_functions(). ie we could have the equivalent of
include/linux/sched/debug.h:#define __sched __section(".sched.text")
perhaps #define __memalloc __section(".memalloc.text") which would do all the necessary magic to know where the backtrace should stop.
Kent Overstreet
maintainerQuoted 5 lines · Michal Hocko
On Wed 14-02-24 01:20:20, Johannes Weiner wrote:
[...]Quoted 2 lines
I agree we should discuss how the annotations are implemented on a technical basis, but my take is that we need something like this.
Quoted 7 lines
I do not think there is any disagreement on usefulness of a better memory allocation tracking. At least for me the primary problem is the implementation. At LFSMM last year we have heard that existing tracing infrastructure hasn't really been explored much. Cover letter doesn't really talk much about those alternatives so it is really hard to evaluate whether the proposed solution is indeed our best way to approach this.
Michal, we covered this before.
To do this with tracing you'd have to build up data structures separately, in userspace, that would mirror the allocator's data structures; you would have to track every single allocation so that you could match up the free event to the place it was allocated.
Even if it could be built, which I doubt, it'd be completely non viable because the performance would be terrible.
Like I said, we covered all this before; if you're going to spend so much time on these threads you really should be making a better attempt at keeping up with what's been talked about.
Kent Overstreet
maintainerQuoted 22 lines · Matthew Wilcox
Quoted 8 lines · Kent Overstreet
This is what instrumenting an allocation function looks like:
#define
krealloc_array(...)alloc_hooks(krealloc_array_noprof(__VA_ARGS__))IOW, we have to:
- renamekrealloc_arraytokrealloc_array_noprof
- replacekrealloc_arraywith a one wrapper macro callQuoted 5 lines
Is this really all we're getting worked up over?
The renaming we need regardless, because the thing that makes this approach efficient enough to run in production is that we account at _one_ point in the callstack, we don't save entire backtraces.
I'm probably going to regret getting involved in this thread, but since Suren already decided to put me on the cc ...
There might be a way to do it without renaming. We have a bit of the linker script called
SCHED_TEXTwhich lets us implementin_sched_functions(). ie we could have the equivalent of
Quoted 5 lines
include/linux/sched/debug.h:#define
__sched__section(".sched.text")perhaps #define
__memalloc__section(".memalloc.text")which would do all the necessary magic to know where the backtrace should stop.
Could we please try to get through the cover letter before proposing alternatives? I already explained there why we need the renaming.
In addition, you can't create the per-callsite codetag with linker magic; you nede the macro for that.
Instead of citing myself again, I'm just going to post what I was working on last night for the documentation directory:
.. SPDX-License-Identifier: GPL-2.0
===========================
MEMORY ALLOCATION PROFILING
===========================
Low overhead (suitable for production) accounting of all memory allocations, tracked by file and line number.
Usage:
kconfig options:
- CONFIG_MEM_ALLOC_PROFILING
- CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT
- CONFIG_MEM_ALLOC_PROFILING_DEBUG
adds warnings for allocations that weren't accounted because of a missing annotation
sysctl:
/proc/sys/vm/mem_profiling
Runtime info:
/proc/allocinfo
Example output:
root@moria-kvm:~# sort -h /proc/allocinfo|tail
3.11MiB 2850 fs/ext4/super.c:1408 module:ext4 func:ext4_alloc_inode
3.52MiB 225 kernel/fork.c:356 module:fork func:alloc_thread_stack_node
3.75MiB 960 mm/page_ext.c:270 module:page_ext func:alloc_page_ext
4.00MiB 2 mm/khugepaged.c:893 module:khugepaged func:hpage_collapse_alloc_folio
10.5MiB 168 block/blk-mq.c:3421 module:blk_mq func:blk_mq_alloc_rqs
14.0MiB 3594 include/linux/gfp.h:295 module:filemap func:folio_alloc_noprof
26.8MiB 6856 include/linux/gfp.h:295 module:memory func:folio_alloc_noprof
64.5MiB 98315 fs/xfs/xfs_rmap_item.c:147 module:xfs func:xfs_rui_init
98.7MiB 25264 include/linux/gfp.h:295 module:readahead func:folio_alloc_noprof
125MiB 7357 mm/slub.c:2201 module:slub func:alloc_slab_pageTheory of operation:
Memory allocation profiling builds off of code tagging, which is a library for declaring static structs (that typcially describe a file and line number in some way, hence code tagging) and then finding and operating on them at runtime
- i.e. iterating over them to print them in debugfs/procfs.
To add accounting for an allocation call, we replace it with a macro invocation, alloc_hooks(), that
- declares a code tag
- stashes a pointer to it in task_struct
- calls the real allocation function
- and finally, restores the task_struct alloc tag pointer to its previous value.
This allows for alloc_hooks() calls to be nested, with the most recent one taking effect. This is important for allocations internal to the mm/ code that do not properly belong to the outer allocation context and should be counted
separately: for example, slab object extension vectors, or when the slab allocates pages from the page allocator.
Thus, proper usage requires determining which function in an allocation call stack should be tagged. There are many helper functions that essentially wrap e.g. kmalloc() and do a little more work, then are called in multiple places; we'll generally want the accounting to happen in the callers of these helpers, not in the helpers themselves.
To fix up a given helper, for example foo(), do the following:
- switch its allocation call to the _noprof() version, e.g. kmalloc_noprof()
- rename it to foo_noprof()
- define a macro version of foo() like so:
#define foo(...) alloc_hooks(foo_noprof(__VA_ARGS__))
Re: [PATCH v3 23/35] mm/slub: Mark slab_free_freelist_hook() __always_inline
In reply to Kent OverstreetQuoted 10 lines · Kent Overstreet
Quoted 6 lines · Kees Cook
Quoted 3 lines · Suren Baghdasaryan
From: Kent Overstreet <[email protected]>
It seems we need to be more forceful with the compiler on this one.
Sure, but why?
Wasn't getting inlined without it, and that's one we do want inlined - it's only called in one place.
It would be better to mention this in the changelog so it's clear this is for performance and not e.g. needed for the code tagging to work as expected.
Michal Hocko
reviewerQuoted 14 lines · Kent Overstreet
Quoted 5 lines · Michal Hocko
On Wed 14-02-24 01:20:20, Johannes Weiner wrote:
[...]Quoted 2 lines
I agree we should discuss how the annotations are implemented on a technical basis, but my take is that we need something like this.
Quoted 7 lines
I do not think there is any disagreement on usefulness of a better memory allocation tracking. At least for me the primary problem is the implementation. At LFSMM last year we have heard that existing tracing infrastructure hasn't really been explored much. Cover letter doesn't really talk much about those alternatives so it is really hard to evaluate whether the proposed solution is indeed our best way to approach this.
Quoted 1 line
Michal, we covered this before.
It is a good practice to summarize previous discussions in the cover letter. Especially when there are different approaches discussed over a longer time period or when the topic is controversial.
I do not see anything like that here. Neither for the existing tracing infrastructure, page owner nor performance concerns discussed before etc. Look, I do not want to nit pick or insist on formalisms but having those data points layed out would make any further discussion much more smooth.
Signature · 2 lines
Michal Hocko SUSE Labs
Kent Overstreet
maintainerQuoted 21 lines · Michal Hocko
Quoted 14 lines · Kent Overstreet
Quoted 5 lines · Michal Hocko
On Wed 14-02-24 01:20:20, Johannes Weiner wrote:
[...]Quoted 2 lines
I agree we should discuss how the annotations are implemented on a technical basis, but my take is that we need something like this.
Quoted 7 lines
I do not think there is any disagreement on usefulness of a better memory allocation tracking. At least for me the primary problem is the implementation. At LFSMM last year we have heard that existing tracing infrastructure hasn't really been explored much. Cover letter doesn't really talk much about those alternatives so it is really hard to evaluate whether the proposed solution is indeed our best way to approach this.
Quoted 1 line
Michal, we covered this before.
It is a good practice to summarize previous discussions in the cover letter. Especially when there are different approaches discussed over a longer time period or when the topic is controversial.
Quoted 5 lines
I do not see anything like that here. Neither for the existing tracing infrastructure, page owner nor performance concerns discussed before etc. Look, I do not want to nit pick or insist on formalisms but having those data points layed out would make any further discussion much more smooth.
You don't want to nitpick???
Look, you've been consistently sidestepping the technical discussion; it seems all you want to talk about is process or "your nack".
If we're going to have a technical discussion, it's incumbent upon all of us to /keep the focus on the technical/; that is everyone's responsibility.
I'm not going to write a 20 page cover letter and recap every dead end that was proposed. That would be a lot of useless crap for eveyone to wade through. I'm going to summarize the important stuff, and keep the focus on what we're doing and documenting it. If you want to take part in a discussion, it's your responsibility to be reading with comprehension and finding useful things to say.
You gotta stop with this this derailing garbage.
Michal Hocko
reviewerOn Wed 14-02-24 11:17:20, Kent Overstreet wrote:
[...]
Quoted 1 line
You gotta stop with this this derailing garbage.
It is always pleasure talking to you Kent, but let me give you advice (free of charge of course). Let Suren talk, chances for civilized and productive discussion are much higher!
I do not have much more to add to the discussion. My point stays, find a support of the MM community if you want to proceed with this work.
Signature · 2 lines
Michal Hocko SUSE Labs
Kent Overstreet
maintainerQuoted 8 lines · Vlastimil Babka
Quoted 6 lines · Kent Overstreet
And, as I keep saying: that
alloc_hooks()macro will also get us_percallsite fault injectionpoints_, and we really need that because - if you guys have been paying attention to other threads - whenever moving more stuff to PF_MEMALLOC_* flags comes up (including addingPF_MEMALLOC_NORECLAIM), the issue of small allocations not failing and not being testable keeps coming up.
Quoted 5 lines
How exactly do you envision the fault injection to help here? The proposals are about scoping via a process flag, and the process may then call just about anything under that scope. So if our tool is per callsite fault injection points, how do we know which callsites to enable to focus the fault injection on the particular scope?
So the question with fault injection is - how do we integrate it into our existing tests?
We need fault injection that we can integrate into our existing tests because that's the only way to get the code coverage we need - writing new tests that cover all the error paths isn't going to happen, and wouldn't work as well anyways.
But the trouble with injecting memory allocation failures is that they'll result in errors bubbling up to userspace, and in unpredictable ways.
We _definitely_ cannot enable random memory allocation faults for the entire kernel at runttme - or rather we _could_, and that would actually be great to do as a side project; but that's not something we can do in our existing automated tests because the results will be completely unpredictable. If we did that the goal would be to just make sure the kernel doesn't explode - but what we actually want is for our automated pass/fail tests to still pass; we need to constrain what will fail.
So we need at a minumum to be able to only enable memory allocation failures for the code we're interested in testing (file/module) - enabling memory allocation failures in some other random subsystem we're not developing or looking at isn't what we want.
Beyond that, it's very much subsystem dependent. For bcachefs, my main strategy has been to flip on random (1%) memory allocation failures after the filesystem has mounted. During startup, we do a ton of allocations (I cover those with separate tests), but after startup we should be able to run normally in the precence of allocation failures without ever returning an error to userspace - so that's what I'm trying to test.
Andrew Morton
maintainerQuoted 9 lines · On Tue, 13 Feb 2024 14:59:11 -0800 Suren Baghdasaryan <[email protected]> wrote:
Quoted 4 lines
Quoted 2 lines
If you think you can easily achieve what Michal requested without all that, good.
He requested something?
Yes, a cleaner instrumentation. Unfortunately the cleanest one is not possible until the compiler feature is developed and deployed. And it still would require changes to the headers, so don't think it's worth delaying the feature for years.
Can we please be told much more about this compiler feature? Description of what it is, what it does, how it will affect this kernel feature, etc.
Who is developing it and when can we expect it to become available?
Will we be able to migrate to it without back-compatibility concerns? (I think "you need quite recent gcc for memory profiling" is reasonable).
Because: if the maintainability issues which Michel describes will be significantly addressed with the gcc support then we're kinda reviewing the wrong patchset. Yes, it may be a maintenance burden initially, but at some (yet to be revealed) time in the future, this will be addressed with the gcc support?
Suren Baghdasaryan
maintainerQuoted 8 lines · Michal Hocko
On Wed 14-02-24 11:17:20, Kent Overstreet wrote:
[...]Quoted 1 line
You gotta stop with this this derailing garbage.
It is always pleasure talking to you Kent, but let me give you advice (free of charge of course). Let Suren talk, chances for civilized and productive discussion are much higher!
Every time I wake up to a new drama... Sorry I won't follow up on this one not to feed the fire.
Quoted 6 lines
I do not have much more to add to the discussion. My point stays, find a support of the MM community if you want to proceed with this work. --
Michal Hocko
SUSE Labs
Suren Baghdasaryan
maintainerQuoted 8 lines · Andrew Morton
Quoted 5 lines · On Tue, 13 Feb 2024 14:59:11 -0800 Suren Baghdasaryan <[email protected]> wrote:
Quoted 4 lines
Quoted 2 lines
If you think you can easily achieve what Michal requested without all that, good.
He requested something?
Quoted 8 lines
Quoted 4 lines
Yes, a cleaner instrumentation. Unfortunately the cleanest one is not possible until the compiler feature is developed and deployed. And it still would require changes to the headers, so don't think it's worth delaying the feature for years.
Can we please be told much more about this compiler feature? Description of what it is, what it does, how it will affect this kernel feature, etc.
Sure. The compiler support will be in a form of a new __attribute__, simplified example:
// generate data for the wrapper
static void _alloc_tag()
{
static struct alloc_tag _alloc_tag __section ("alloc_tags")
= { .ct = CODE_TAG_INIT, .counter = 0 };
}static inline int
wrapper (const char *name, int x, int (*callee) (const char *, int),
struct alloc_tag *callsite_data)
{
callsite_data->counter++;
printf ("Call #%d from %s:%d (%s)\n", callsite_data->counter,
callsite_data->ct.filename, callsite_data->ct.lineno,
callsite_data->ct.function); int ret = callee (name, x);
printf ("Returned: %d\n", ret);
return ret;
}
__attribute__((annotate("callsite_wrapped_by", wrapper, _alloc_tag))) int foo(const char* name, int x);
int foo(const char* name, int x) {
printf ("Hello %s, %d!\n", name, x);
return x;
}
Which we will be able to attach to a function without changing its name and preserving the namespace (it applies only to functions with that name, not everything else).
Note that we will still need _noprof versions of the allocators.
Quoted 2 lines
Who is developing it and when can we expect it to become available?
Aleksei Vetrov (google) with the help of Nick Desaulniers (google). Both are CC'ed on this email.
After several iterations Aleksei has a POC which we are evaluating
(https://github.com/llvm/llvm-project/compare/main...noxwell:llvm-project:callsite-wrapper-tree-transform).
Once it's in good shape we are going to engage with CLANG and GCC community to get it upstreamed. When it will become available and when the distributions will pick it up is anybody's guess. Upstreaming is usually a lengthy process.
Quoted 4 lines
Will we be able to migrate to it without back-compatibility concerns? (I think "you need quite recent gcc for memory profiling" is reasonable).
The migration should be quite straight-forward, replacing the macros with functions with that attribute.
Quoted 7 lines
Because: if the maintainability issues which Michel describes will be significantly addressed with the gcc support then we're kinda reviewing the wrong patchset. Yes, it may be a maintenance burden initially, but at some (yet to be revealed) time in the future, this will be addressed with the gcc support?
That's what I'm aiming for. I just don't want this placed on hold until the compiler support is widely available, which might take years.
Quoted 1 line
Kent Overstreet
maintainerQuoted 24 lines · Andrew Morton
Quoted 9 lines · On Tue, 13 Feb 2024 14:59:11 -0800 Suren Baghdasaryan <[email protected]> wrote:
Quoted 4 lines
Quoted 2 lines
If you think you can easily achieve what Michal requested without all that, good.
He requested something?
Yes, a cleaner instrumentation. Unfortunately the cleanest one is not possible until the compiler feature is developed and deployed. And it still would require changes to the headers, so don't think it's worth delaying the feature for years.
Can we please be told much more about this compiler feature? Description of what it is, what it does, how it will affect this kernel feature, etc.
Who is developing it and when can we expect it to become available?
Will we be able to migrate to it without back-compatibility concerns? (I think "you need quite recent gcc for memory profiling" is reasonable).
Quoted 5 lines
Because: if the maintainability issues which Michel describes will be significantly addressed with the gcc support then we're kinda reviewing the wrong patchset. Yes, it may be a maintenance burden initially, but at some (yet to be revealed) time in the future, this will be addressed with the gcc support?
Even if we had compiler magic, after considering it more I don't think the patchset would be improved by it - I would still prefer to stick with the macro approach.
There's also a lot of unresolved questions about whether the compiler approach would even end being what we need; we need macro expansion to happen in the caller of the allocation function, and that's another level of hooking that I don't think the compiler people are even considering yet, since cpp runs before the main part of the compiler; if C macros worked and were implemented more like Rust macros I'm sure it could be done - in fact, I think this could all be done in Rust _without_ any new compiler support - but in C, this is a lot to ask.
Let's look at the instrumentation again. There's two steps:
- Renaming the original function to _noprof
- Adding a hooked version of the original function.
We need to do the renaming regardless of what approach we take in order to correctly handle allocations that happen inside the context of an existing alloc tag hook but should not be accounted to the outer context; we do that by selecting the alloc_foo() or alloc_foo_noprof() version as appropriate.
It's important to get this right; consider slab object extension vectors or the slab allocator allocating pages from the page allocator.
Second step, adding a hooked version of the original function. We do that with
#define alloc_foo(...) alloc_hooks(alloc_foo_noprof(__VA_ARGS__))
That's pretty clean, if you ask me. The only way to make it more succint be if it were possible for a C macro to define a new macro, then it could be just
alloc_fn(alloc_foo);
But honestly, the former is probably preferable anyways from a ctags/cscope POV.
Re: [PATCH v3 05/35] mm: introduce slabobj_ext to support slab object extensions
In reply to Suren BaghdasaryanQuoted 7 lines · Suren Baghdasaryan
Currently slab pages can store only vectors of
obj_cgrouppointers inpage->memcg_data. Introduceslabobj_extstructure to allow more data to be stored for each slab object. Wrapobj_cgroupintoslabobj_extto support current functionality while allowing to extendslabobj_extin the future.Signed-off-by Suren Baghdasaryan <[email protected]>
...
Quoted 18 lines
mm/slab.h +18
-0+static inline bool need_slab_obj_ext (void ) +{ +/* +* CONFIG_MEMCG_KMEM creates vector of obj_cgroup objects conditionally +* inside memcg_slab_post_alloc_hook. No other users for now. +*/ +return false; +} + +static inline struct slabobj_ext * +prepare_slab_obj_exts_hook (struct kmem_cache *s,gfp_t flags,void *p) +{ +struct slab *slab; + +if (!p) +return NULL ; + +if (!need_slab_obj_ext ())
mm/slab.h static inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t fla · line 599
Quoted 10 lines
mm/slab.h +10
-0+return NULL ; + + slab =virt_to_slab (p); +if (!slab_obj_exts (slab) && +WARN (alloc_slab_obj_exts (slab, s, flags, false), +"%s, %s: Failed to create slab extension vector!\n" , + __func__, s->name)) +return NULL ; + +return slab_obj_exts (slab) +obj_to_index (s, slab, p);
This is called in slab_post_alloc_hook() and the result stored to obj_exts but unused. Maybe introduce this only in a later patch where it becomes relevant?
Quoted 7 lines
mm/slab_common.c +1
-0@@ -201,6 +201,54 @@ struct kmem_cache *find_mergeable(unsigned int size, unsigned int align,return NULL ; } +#ifdef CONFIG_SLAB_OBJ_EXT
mm/slab_common.c struct kmem_cache *find_mergeable(unsigned int size, unsigned int align, · line 214
Quoted 10 lines
mm/slab_common.c +10
-0+/* +* The allocated objcg pointers array is not accounted directly. +* Moreover, it should not come from DMA buffer and is not readily +* reclaimable. So those GFP bits should be masked off. +*/ +#define OBJCGS_CLEAR_MASK (__GFP_DMA | __GFP_RECLAIMABLE | \ + __GFP_ACCOUNT | __GFP_NOFAIL) + +int alloc_slab_obj_exts (struct slab *slab,struct kmem_cache *s, +gfp_t gfp,bool new_slab)
Since you're moving this function between files anyway, could you please instead move it to mm/slub.c. I expect we'll eventually (maybe even soon) move the rest of performance sensitive kmemcg hooks there as well to make inlining possible.
Quoted 60 lines
mm/slab_common.c +37
-0+{ +unsigned int objects =objs_per_slab (s, slab); +unsigned long obj_exts; +void *vec; + + gfp &= ~OBJCGS_CLEAR_MASK ; + vec =kcalloc_node (objects,sizeof (struct slabobj_ext ), gfp, +slab_nid (slab)); +if (!vec) +return -ENOMEM ; + + obj_exts = (unsigned long )vec; +#ifdef CONFIG_MEMCG + obj_exts |=MEMCG_DATA_OBJEXTS ; +#endif +if (new_slab) { +/* +* If the slab is brand new and nobody can yet access its +* obj_exts, no synchronization is required and obj_exts can +* be simply assigned. +*/ + slab->obj_exts = obj_exts; + }else if (cmpxchg (&slab->obj_exts,0 , obj_exts)) { +/* +* If the slab is already in use, somebody can allocate and +* assign slabobj_exts in parallel. In this case the existing +* objcg vector should be reused. +*/ +kfree (vec); +return 0 ; + } + +kmemleak_not_leak (vec); +return 0 ; +} +#endif /* CONFIG_SLAB_OBJ_EXT */ + static struct kmem_cache *create_cache(const char *name, unsigned int object_size, unsigned int align, slab_flags_t flags, unsigned int useroffset,mm/slub.c +2
-2@@ -683,10 +683,10 @@ static inline bool __slab_update_freelist(struct kmem_cache *s, struct slab *slaif (s->flags & __CMPXCHG_DOUBLE) { ret =__update_freelist_fast (slab, freelist_old, counters_old, - freelist_new, counters_new); + freelist_new, counters_new); }else { ret =__update_freelist_slow (slab, freelist_old, counters_old, - freelist_new, counters_new); + freelist_new, counters_new); }if (likely (ret))return true;@@ -710,13 +710,13 @@ static inline bool slab_update_freelist(struct kmem_cache *s, struct slab *slab,if (s->flags & __CMPXCHG_DOUBLE) {
mm/slub.c static inline bool slab_update_freelist(struct kmem_cache *s, struct slab *slab, · line 719
Quoted 10 lines
mm/slub.c +2
-2ret =__update_freelist_fast (slab, freelist_old, counters_old, - freelist_new, counters_new); + freelist_new, counters_new); }else {unsigned long flags;local_irq_save (flags); ret =__update_freelist_slow (slab, freelist_old, counters_old, - freelist_new, counters_new); + freelist_new, counters_new);
I can see the mixing of tabs and spaces is wrong but perhaps not fix it as part of the series?
mm/slub.c static inline enum node_stat_item cache_vmstat_idx(struct kmem_cache *s) · line 1885
Quoted 11 lines
mm/slub.c +2
-2local_irq_restore (flags); }if (likely (ret))@@ -1881,13 +1881,25 @@ static inline enum node_stat_item cache_vmstat_idx(struct kmem_cache *s) NR_SLAB_RECLAIMABLE_B :NR_SLAB_UNRECLAIMABLE_B ; } -#ifdef CONFIG_MEMCG_KMEM -static inline void memcg_free_slab_cgroups (struct slab *slab) +#ifdef CONFIG_SLAB_OBJ_EXT +static inline void free_slab_obj_exts (struct slab *slab)
Right, freeing is already here, so makes sense put the allocation here as well.
mm/slub.c void slab_post_alloc_hook(struct kmem_cache *s, struct obj_cgroup *objcg, · line 3823
Quoted 5 lines
mm/slub.c +1
-0@@ -3817,6 +3820,7 @@ void slab_post_alloc_hook(struct kmem_cache *s, struct obj_cgroup *objcg,kmemleak_alloc_recursive (p[i], s->object_size,1 , s->flags, init_flags);kmsan_slab_alloc (s, p[i], init_flags); + obj_exts =prepare_slab_obj_exts_hook (s, flags, p[i]);
Yeah here's the hook used. Doesn't it generate a compiler warning? Maybe at least postpone the call until the result is further used.
Quoted 3 lines
mm/slub.c +0
-0}memcg_slab_post_alloc_hook (s, objcg, flags, size, p);
Quoted 1 line · Suren Baghdasaryan
Memory allocation, v3 and final:
Would be nice to have --base added to cover letter. The very first patch can't be applied on today's Linux Next.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Suren Baghdasaryan
maintainerQuoted 6 lines · Andy Shevchenko
Quoted 1 line · Suren Baghdasaryan
Memory allocation, v3 and final:
Would be nice to have --base added to cover letter. The very first patch can't be applied on today's Linux Next.
Sorry about that. It as based on Linus` ToT at the time of posting (7521f258ea30 Merge tag 'mm-hotfixes-stable-2024-02-10-11-16' ofgit://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm). I also applied it to mm-unstable with only one trivial merge conflict in one of the patches.
Quoted 7 lines
--
With Best Regards,
Andy Shevchenko
Quoted 63 lines · Suren Baghdasaryan
Memory allocation, v3 and final:
Overview:
Low overhead [1] per-callsite memory allocation profiling. Not just for debug kernels, overhead low enough to be deployed in production.We're aiming to get this in the next merge window, for 6.9. The feedback we've gotten has been that even out of tree this patchset has already been useful, and there's a significant amount of other work gated on the code tagging functionality included in this patchset [2].
Example output:
root@moria-kvm:~# sort -h /proc/allocinfo|tail 3.11MiB 2850 fs/ext4/super.c:1408 module:ext4 func:ext4_alloc_inode 3.52MiB 225 kernel/fork.c:356 module:fork func:alloc_thread_stack_node 3.75MiB 960 mm/page_ext.c:270 module:page_ext func:alloc_page_ext 4.00MiB 2 mm/khugepaged.c:893 module:khugepaged func:hpage_collapse_alloc_folio 10.5MiB 168 block/blk-mq.c:3421 module:blk_mq func:blk_mq_alloc_rqs 14.0MiB 3594 include/linux/gfp.h:295 module:filemap func:folio_alloc_noprof 26.8MiB 6856 include/linux/gfp.h:295 module:memory func:folio_alloc_noprof 64.5MiB 98315 fs/xfs/xfs_rmap_item.c:147 module:xfs func:xfs_rui_init 98.7MiB 25264 include/linux/gfp.h:295 module:readahead func:folio_alloc_noprof 125MiB 7357 mm/slub.c:2201 module:slub func:alloc_slab_pageSince v2:
- tglx noticed a circular header dependency betweensched.handpercpu.h; a bunch of header cleanups were merged into 6.8 to ameliorate this [3].- a number of improvements, moving
alloc_hooks()annotations to the correct place for better tracking (mempool), and bugfixes.- looked at alternate hooking methods.
There were suggestions on alternate methods (compiler attribute, trampolines), but they wouldn't have made the patchset any cleaner (we still need to have different function versions for accounting vs. no accounting to control at which point in a call chain the accounting happens), and they would have added a dependency on toolchain support.Usage:
kconfig options:
-CONFIG_MEM_ALLOC_PROFILING
-CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT
-CONFIG_MEM_ALLOC_PROFILING_DEBUG
adds warnings for allocations that weren't accounted because of a missing annotationsysctl:
/proc/sys/vm/mem_profilingRuntime info:
/proc/allocinfoNotes:
[1]: Overhead
To measure the overhead we are comparing the following configurations:
(1) Baseline withCONFIG_MEMCG_KMEM=n
(2) Disabled by default (CONFIG_MEM_ALLOC_PROFILING=y&&CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT=n)
(3) Enabled by default (CONFIG_MEM_ALLOC_PROFILING=y&&CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT=y)
(4) Enabled at runtime (CONFIG_MEM_ALLOC_PROFILING=y&&
Quoted 3 lines
CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT=n && /proc/sys/vm/mem_profiling=1)(5) Baseline with
CONFIG_MEMCG_KMEM=y&& allocating with__GFP_ACCOUNT
Thanks for the work on this patchset and it is quite useful. A clarification question on the data:
I assume Config (2), (3) and (4) has CONFIG_MEMCG_KMEM=n, right? If so do you have similar data for config (2), (3) and (4) but with CONFIG_MEMCG_KMEM=y for comparison with (5)?
Tim
Quoted 15 lines
Performance overhead:
To evaluate performance we implemented an in-kernel test executing multiple get_free_page/free_page and kmalloc/kfree calls with allocation sizes growing from 8 to 240 bytes with CPU frequency set to max and CPU affinity set to a specific CPU to minimize the noise. Below are results from running the test on Ubuntu 22.04.2 LTS with 6.8.0-rc1 kernel on 56 core Intel Xeon:kmalloc pgalloc (1 baseline) 6.764s 16.902s (2 default disabled) 6.793s (+0.43%) 17.007s (+0.62%) (3 default enabled) 7.197s (+6.40%) 23.666s (+40.02%) (4 runtime enabled) 7.405s (+9.48%) 23.901s (+41.41%) (5 memcg) 13.388s (+97.94%) 48.460s (+186.71%)
Suren Baghdasaryan
maintainerQuoted 69 lines · Tim Chen
Quoted 63 lines · Suren Baghdasaryan
Memory allocation, v3 and final:
Overview:
Low overhead [1] per-callsite memory allocation profiling. Not just for debug kernels, overhead low enough to be deployed in production.We're aiming to get this in the next merge window, for 6.9. The feedback we've gotten has been that even out of tree this patchset has already been useful, and there's a significant amount of other work gated on the code tagging functionality included in this patchset [2].
Example output:
root@moria-kvm:~# sort -h /proc/allocinfo|tail 3.11MiB 2850 fs/ext4/super.c:1408 module:ext4 func:ext4_alloc_inode 3.52MiB 225 kernel/fork.c:356 module:fork func:alloc_thread_stack_node 3.75MiB 960 mm/page_ext.c:270 module:page_ext func:alloc_page_ext 4.00MiB 2 mm/khugepaged.c:893 module:khugepaged func:hpage_collapse_alloc_folio 10.5MiB 168 block/blk-mq.c:3421 module:blk_mq func:blk_mq_alloc_rqs 14.0MiB 3594 include/linux/gfp.h:295 module:filemap func:folio_alloc_noprof 26.8MiB 6856 include/linux/gfp.h:295 module:memory func:folio_alloc_noprof 64.5MiB 98315 fs/xfs/xfs_rmap_item.c:147 module:xfs func:xfs_rui_init 98.7MiB 25264 include/linux/gfp.h:295 module:readahead func:folio_alloc_noprof 125MiB 7357 mm/slub.c:2201 module:slub func:alloc_slab_pageSince v2:
- tglx noticed a circular header dependency betweensched.handpercpu.h; a bunch of header cleanups were merged into 6.8 to ameliorate this [3].- a number of improvements, moving
alloc_hooks()annotations to the correct place for better tracking (mempool), and bugfixes.- looked at alternate hooking methods.
There were suggestions on alternate methods (compiler attribute, trampolines), but they wouldn't have made the patchset any cleaner (we still need to have different function versions for accounting vs. no accounting to control at which point in a call chain the accounting happens), and they would have added a dependency on toolchain support.Usage:
kconfig options:
-CONFIG_MEM_ALLOC_PROFILING
-CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT
-CONFIG_MEM_ALLOC_PROFILING_DEBUG
adds warnings for allocations that weren't accounted because of a missing annotationsysctl:
/proc/sys/vm/mem_profilingRuntime info:
/proc/allocinfoNotes:
[1]: Overhead
To measure the overhead we are comparing the following configurations:
(1) Baseline withCONFIG_MEMCG_KMEM=n
(2) Disabled by default (CONFIG_MEM_ALLOC_PROFILING=y&&CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT=n)
(3) Enabled by default (CONFIG_MEM_ALLOC_PROFILING=y&&CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT=y)
(4) Enabled at runtime (CONFIG_MEM_ALLOC_PROFILING=y&&Quoted 3 lines
CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT=n && /proc/sys/vm/mem_profiling=1)(5) Baseline with
CONFIG_MEMCG_KMEM=y&& allocating with__GFP_ACCOUNT
Quoted 4 lines
Thanks for the work on this patchset and it is quite useful. A clarification question on the data:
I assume Config (2), (3) and (4) has
CONFIG_MEMCG_KMEM=n, right?
Yes, correct.
Quoted 2 lines
If so do you have similar data for config (2), (3) and (4) but with
CONFIG_MEMCG_KMEM=yfor comparison with (5)?
I have data for these additional configs (didn't think there were that important):
(6) Disabled by default (CONFIG_MEM_ALLOC_PROFILING=y && CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT=n) && CONFIG_MEMCG_KMEM=y
(7) Enabled by default (CONFIG_MEM_ALLOC_PROFILING=y && CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT=y) && CONFIG_MEMCG_KMEM=y
Quoted 17 lines
Tim
Quoted 14 lines
Performance overhead:
To evaluate performance we implemented an in-kernel test executing multiple get_free_page/free_page and kmalloc/kfree calls with allocation sizes growing from 8 to 240 bytes with CPU frequency set to max and CPU affinity set to a specific CPU to minimize the noise. Below are results from running the test on Ubuntu 22.04.2 LTS with 6.8.0-rc1 kernel on 56 core Intel Xeon:kmalloc pgalloc (1 baseline) 6.764s 16.902s (2 default disabled) 6.793s (+0.43%) 17.007s (+0.62%) (3 default enabled) 7.197s (+6.40%) 23.666s (+40.02%) (4 runtime enabled) 7.405s (+9.48%) 23.901s (+41.41%) (5 memcg) 13.388s (+97.94%) 48.460s (+186.71%)
(6 default disabled+memcg) 13.332s (+97.10%) 48.105s (+184.61%)
(7 default enabled+memcg) 13.446s (+98.78%) 54.963s (+225.18%)
(6) shows a bit better performance than (5) but it's probably noise. I would expect them to be roughly the same. Hope this helps.
Quoted 3 lines
Quoted 1 line
Suren Baghdasaryan
maintainerRe: [PATCH v3 05/35] mm: introduce slabobj_ext to support slab object extensions
In reply to Vlastimil BabkaQuoted 36 lines · Vlastimil Babka
Quoted 7 lines · Suren Baghdasaryan
Currently slab pages can store only vectors of
obj_cgrouppointers inpage->memcg_data. Introduceslabobj_extstructure to allow more data to be stored for each slab object. Wrapobj_cgroupintoslabobj_extto support current functionality while allowing to extendslabobj_extin the future.Signed-off-by Suren Baghdasaryan <[email protected]>...
Quoted 24 lines
mm/slab.h +24
-0+static inline bool need_slab_obj_ext (void ) +{ +/* +* CONFIG_MEMCG_KMEM creates vector of obj_cgroup objects conditionally +* inside memcg_slab_post_alloc_hook. No other users for now. +*/ +return false; +} + +static inline struct slabobj_ext * +prepare_slab_obj_exts_hook(struct kmem_cache *s, gfp_t flags, void *p) +{ + struct slab *slab; + + if (!p) + return NULL; + + if (!need_slab_obj_ext()) + return NULL; + + slab = virt_to_slab(p); + if (!slab_obj_exts(slab) && + WARN(alloc_slab_obj_exts(slab, s, flags, false), + "%s, %s: Failed to create slab extension vector!\n",
Quoted 8 lines
mm/slab.h static inline bool kmem_cache_debug_flags(struct kmem_cache *s, slab_flags_t fla · line 599
Quoted 4 lines
mm/slab.h +4
-0+ __func__, s->name)) +return NULL ; + +return slab_obj_exts (slab) +obj_to_index (s, slab, p);This is called in
slab_post_alloc_hook()and the result stored toobj_extsbut unused. Maybe introduce this only in a later patch where it becomes relevant?
Ack. I'll move it into the patch where we start using obj_exts.
Quoted 7 lines
Quoted 7 lines
mm/slab_common.c +0
-0@@ -201,6 +201,54 @@ struct kmem_cache *find_mergeable(unsigned int size, unsigned int align,return NULL ; }
Quoted 16 lines
mm/slab_common.c struct kmem_cache *find_mergeable(unsigned int size, unsigned int align, · line 214
Quoted 11 lines
mm/slab_common.c +11
-0+#ifdef CONFIG_SLAB_OBJ_EXT +/* +* The allocated objcg pointers array is not accounted directly. +* Moreover, it should not come from DMA buffer and is not readily +* reclaimable. So those GFP bits should be masked off. +*/ +#define OBJCGS_CLEAR_MASK (__GFP_DMA | __GFP_RECLAIMABLE | \ + __GFP_ACCOUNT | __GFP_NOFAIL) + +int alloc_slab_obj_exts (struct slab *slab,struct kmem_cache *s, +gfp_t gfp,bool new_slab)Since you're moving this function between files anyway, could you please instead move it to
mm/slub.c. I expect we'll eventually (maybe even soon) move the rest of performance sensitive kmemcg hooks there as well to make inlining possible.
Will do.
Quoted 67 lines
Quoted 67 lines
mm/slab_common.c +37
-0+{ +unsigned int objects =objs_per_slab (s, slab); +unsigned long obj_exts; +void *vec; + + gfp &= ~OBJCGS_CLEAR_MASK ; + vec =kcalloc_node (objects,sizeof (struct slabobj_ext ), gfp, +slab_nid (slab)); +if (!vec) +return -ENOMEM ; + + obj_exts = (unsigned long )vec; +#ifdef CONFIG_MEMCG + obj_exts |=MEMCG_DATA_OBJEXTS ; +#endif +if (new_slab) { +/* +* If the slab is brand new and nobody can yet access its +* obj_exts, no synchronization is required and obj_exts can +* be simply assigned. +*/ + slab->obj_exts = obj_exts; + }else if (cmpxchg (&slab->obj_exts,0 , obj_exts)) { +/* +* If the slab is already in use, somebody can allocate and +* assign slabobj_exts in parallel. In this case the existing +* objcg vector should be reused. +*/ +kfree (vec); +return 0 ; + } + +kmemleak_not_leak (vec); +return 0 ; +} +#endif /* CONFIG_SLAB_OBJ_EXT */ + static struct kmem_cache *create_cache(const char *name, unsigned int object_size, unsigned int align, slab_flags_t flags, unsigned int useroffset,mm/slub.c +3
-3@@ -683,10 +683,10 @@ static inline bool __slab_update_freelist(struct kmem_cache *s, struct slab *slaif (s->flags & __CMPXCHG_DOUBLE) { ret =__update_freelist_fast (slab, freelist_old, counters_old, - freelist_new, counters_new); + freelist_new, counters_new); }else { ret =__update_freelist_slow (slab, freelist_old, counters_old, - freelist_new, counters_new); + freelist_new, counters_new); }if (likely (ret))return true;@@ -710,13 +710,13 @@ static inline bool slab_update_freelist(struct kmem_cache *s, struct slab *slab,if (s->flags & __CMPXCHG_DOUBLE) { ret =__update_freelist_fast (slab, freelist_old, counters_old, - freelist_new, counters_new); + freelist_new, counters_new); }else {unsigned long flags;
Quoted 7 lines
mm/slub.c static inline bool slab_update_freelist(struct kmem_cache *s, struct slab *slab, · line 719
Quoted 4 lines
mm/slub.c +1
-1local_irq_save (flags); ret =__update_freelist_slow (slab, freelist_old, counters_old, - freelist_new, counters_new); + freelist_new, counters_new);I can see the mixing of tabs and spaces is wrong but perhaps not fix it as part of the series?
I'll fix them in the next version.
Quoted 15 lines
mm/slub.c static inline enum node_stat_item cache_vmstat_idx(struct kmem_cache *s) · line 1885
Quoted 12 lines
mm/slub.c +2
-2local_irq_restore (flags); }if (likely (ret))@@ -1881,13 +1881,25 @@ static inline enum node_stat_item cache_vmstat_idx(struct kmem_cache *s) NR_SLAB_RECLAIMABLE_B :NR_SLAB_UNRECLAIMABLE_B ; } -#ifdef CONFIG_MEMCG_KMEM -static inline void memcg_free_slab_cgroups (struct slab *slab) +#ifdef CONFIG_SLAB_OBJ_EXT +static inline void free_slab_obj_exts (struct slab *slab)Right, freeing is already here, so makes sense put the allocation here as well.
Quoted 8 lines
mm/slub.c void slab_post_alloc_hook(struct kmem_cache *s, struct obj_cgroup *objcg, · line 3823
Quoted 5 lines
mm/slub.c +1
-0@@ -3817,6 +3820,7 @@ void slab_post_alloc_hook(struct kmem_cache *s, struct obj_cgroup *objcg,kmemleak_alloc_recursive (p[i], s->object_size,1 , s->flags, init_flags);kmsan_slab_alloc (s, p[i], init_flags); + obj_exts =prepare_slab_obj_exts_hook (s, flags, p[i]);Yeah here's the hook used. Doesn't it generate a compiler warning? Maybe at least postpone the call until the result is further used.
Yes, I'll move that into the patch where we start using it.
Thanks for the review, Vlastimil!
Quoted 8 lines
mm/slub.c void slab_post_alloc_hook(struct kmem_cache *s, struct obj_cgroup *objcg, · line 3826
Quoted 4 lines
mm/slub.c +0
-0}memcg_slab_post_alloc_hook (s, objcg, flags, size, p);--
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
Suren Baghdasaryan
maintainerQuoted 32 lines · Kent Overstreet
Quoted 24 lines · Andrew Morton
Quoted 9 lines · On Tue, 13 Feb 2024 14:59:11 -0800 Suren Baghdasaryan <[email protected]> wrote:
Quoted 4 lines
Quoted 2 lines
If you think you can easily achieve what Michal requested without all that, good.
He requested something?
Yes, a cleaner instrumentation. Unfortunately the cleanest one is not possible until the compiler feature is developed and deployed. And it still would require changes to the headers, so don't think it's worth delaying the feature for years.
Can we please be told much more about this compiler feature? Description of what it is, what it does, how it will affect this kernel feature, etc.
Who is developing it and when can we expect it to become available?
Will we be able to migrate to it without back-compatibility concerns? (I think "you need quite recent gcc for memory profiling" is reasonable).
Quoted 5 lines
Because: if the maintainability issues which Michel describes will be significantly addressed with the gcc support then we're kinda reviewing the wrong patchset. Yes, it may be a maintenance burden initially, but at some (yet to be revealed) time in the future, this will be addressed with the gcc support?
Quoted 7 lines
Even if we had compiler magic, after considering it more I don't think the patchset would be improved by it - I would still prefer to stick with the macro approach.
There's also a lot of unresolved questions about whether the compiler approach would even end being what we need; we need macro expansion to happen in the caller of the allocation function
For the record, that's what this attribute will be doing. So it should cover our usecase.
Quoted 33 lines
, and that's another
level of hooking that I don't think the compiler people are even considering yet, since cpp runs before the main part of the compiler; if C macros worked and were implemented more like Rust macros I'm sure it could be done - in fact, I think this could all be done in Rust _without_ any new compiler support - but in C, this is a lot to ask.Let's look at the instrumentation again. There's two steps:
- Renaming the original function to
_noprof
- Adding a hooked version of the original function.We need to do the renaming regardless of what approach we take in order to correctly handle allocations that happen inside the context of an existing alloc tag hook but should not be accounted to the outer context; we do that by selecting the
alloc_foo()oralloc_foo_noprof()version as appropriate.It's important to get this right; consider slab object extension vectors or the slab allocator allocating pages from the page allocator.
Second step, adding a hooked version of the original function. We do that with
#define
alloc_foo(...)alloc_hooks(alloc_foo_noprof(__VA_ARGS__))That's pretty clean, if you ask me. The only way to make it more succint be if it were possible for a C macro to define a new macro, then it could be just
alloc_fn(alloc_foo);But honestly, the former is probably preferable anyways from a ctags/cscope POV.