Skip to contentSkip to conversation

Memory allocation profiling

PATCH Superseded Suren Baghdasaryan 10 participants 64 messages Oct 24, 2023 – Oct 30, 2023

Subsystems (56)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Suren Baghdasaryan

author maintainer
#0 lore raw

v2 cover Memory allocation profiling

Updates since the last version [1]
- Simplified allocation tagging macros;
- Runtime enable/disable sysctl switch (/proc/sys/vm/mem_profiling) instead of kernel command-line option;
- CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT to select default enable state;
- Changed the user-facing API from debugfs to procfs (/proc/allocinfo);
- Removed context capture support to make patch incremental;
- Renamed uninstrumented allocation functions to use _noprof suffix;
- Added __GFP_LAST_BIT to make the code cleaner;
- Removed lazy per-cpu counters; it turned out the memory savings was minimal and not worth the performance impact;

Things we could not address:
- Alternative way of instrument allocation functions. We discussed an alternative way of instrumenting the allocators and Steven Rostedt wrote a proposal [2] to provide compiler support for Callsite Trampolines - a special attribution of functions to be instrumented. So far we spoke to representatives of GNU and CLANG communities, will be presenting a proposal at LPC 2023 and we are working on a proof of concept for CLANG (see [example 1]). While we will keep working with compiler community on adding this support, posting the latest version of the patchset as an immediately available solution until compiler support is implemented;
- Reclaim memory used by pageexts, slabexts, pcpuexts when profiling is disabled. The main obstacles of reclaiming the memory used for profiling:
1. References from already allocated objects still point to the allocation tags we are trying to reclaim; We would have to scan and kill all references before reclaiming;
2. pageext memory is allocated during early boot when fragmentation is

     low. Once reclaimed we might not be able to get it back;

3. pageext and slabext objects used for profiling can be interleaved with other data. Reformatting these vectors of objects at runtime is a complex and racy task.

Overview

Memory allocation profiling infrastructure provides a low overhead mechanism to make all kernel allocations in the system visible. It can be used to monitor memory usage, track memory hotspots, detect memory leaks, identify memory regressions.

To keep the overhead to the minimum, we record only allocation sizes for every allocation in the codebase. The data is exposed to the user space via /proc/allocinfo interface. Usage example:

$ sort -hr /proc/allocinfo | head
  153MiB     8599 mm/slub.c:1826 module:slub func:alloc_slab_page
 6.08MiB      49 mm/slab_common.c:950 module:slab_common func:_kmalloc_order
 5.09MiB     6335 mm/memcontrol.c:2814 module:memcontrol func:alloc_slab_obj_exts
 4.54MiB      78 mm/page_alloc.c:5777 module:page_alloc func:alloc_pages_exact
 1.32MiB      338 include/asm-generic/pgalloc.h:63 module:pgtable func:__pte_alloc_one
 1.16MiB      603 fs/xfs/xfs_log_priv.h:700 module:xfs func:xlog_kvmalloc
 1.00MiB      256 mm/swap_cgroup.c:48 module:swap_cgroup func:swap_cgroup_prepare
  734KiB     5380 fs/xfs/kmem.c:20 module:xfs func:kmem_alloc
  640KiB      160 kernel/rcu/tree.c:3184 module:tree func:fill_page_cache_func
  640KiB      160 drivers/char/virtio_console.c:452 module:virtio_console func:alloc_buf

Support for more detailed allocation context including pid, tgid, task name, allocation size, timestamp and call stack is not posted in this patchset to keep it small.

Implementation utilizes a more generic concept of code tagging, introduced as part of this patchset. Code tag is a structure identifying a specific location in the source code which is generated at compile time and can be embedded in an application-specific structure. A number of applications for code tagging have been presented in the original RFC [3]. Code tagging uses the old trick of "define a special elf section for objects of a given type so that we can iterate over them at runtime" and creates a proper library for it.

To profile memory allocations, we instrument page, slab and percpu allocators to record total memory allocated in the associated code tag at every allocation in the codebase. Every time an allocation is performed by an instrumented allocator, the code tag at that location increments its counter by allocation size. Every time the memory is freed the counter is decremented. To decrement the counter upon freeing, allocated object needs a reference to its code tag. Page allocators use page_ext to record this reference while slab allocators use memcg_data (renamed into more generic slabobj_ext) of the slab page.

Module allocations are accounted for the same way as other kernel allocations. Module loading and unloading is supported. If a module is unloaded while one or more of its allocations is still not freed (rather rare condition), its data section will be kept in memory to allow later code tag referencing when the allocation is freed later on.

As part of this series we introduce several kernel configs:
CONFIG_CODE_TAGGING - to enable code tagging framework. CONFIG_MEM_ALLOC_PROFILING - enables memory allocation profiling. CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT - enables memory allocation profiling by default.
CONFIG_MEM_ALLOC_PROFILING_DEBUG - enables memory allocation profiling validation.
Note: CONFIG_MEM_ALLOC_PROFILING enables CONFIG_PAGE_EXTENSION to store code tag reference in the page_ext object.

/proc/sys/vm/mem_profiling sysctl is provided to enable/disable the functionality and avoid the performance overhead.

Overhead
To measure the overhead we are comparing the following configurations:
(1) Baseline
(2) Disabled by default (CONFIG_MEM_ALLOC_PROFILING & !CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT)
(3) Enabled by default (CONFIG_MEM_ALLOC_PROFILING & CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT)
(4) Enabled at runtime (CONFIG_MEM_ALLOC_PROFILING & !CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT & /proc/sys/vm/mem_profiling=1)
(5) Memcg (CONFIG_MEMCG_KMEM)
(6) Enabled by default with memcg (CONFIG_MEM_ALLOC_PROFILING & CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT & CONFIG_MEMCG_KMEM)

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 is performance comparison between the baseline kernel, profiling when enabled, profiling when disabled and (for comparison purposes) baseline with CONFIG_MEMCG_KMEM enabled and allocations using __GFP_ACCOUNT:

                        kmalloc                 pgalloc
(1 baseline)            12.041s                 49.190s
(2 default disabled)    14.970s (+24.33%)       49.684s (+1.00%)
(3 default enabled)     16.859s (+40.01%)       56.287s (+14.43%)
(4 runtime enabled)     16.983s (+41.04%)       55.760s (+13.36%)
(5 memcg)               33.831s (+180.96%)      51.433s (+4.56%)
(6 enabled & memcg)     39.145s (+225.10%)      56.874s (+15.62%)

Memory overhead:
Kernel size:

   text           data            bss            dec            hex
(1) 32638461      18286426        18325508       69250395       420ad5b
(2) 32710110      18646586        18071556       69428252       423641c
(3) 32706918      18646586        18071556       69425060       42357a4
(4) 32709664      18646586        18071556       69427806       423625e
(5) 32715893      18345334        18239492       69300719       42171ef
(6) 32786068      18701958        17993732       69481758       424351e

Memory consumption on a 56 core Intel CPU with 125GB of memory running Fedora:

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.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://docs.google.com/presentation/d/1zQnuMbEfcq9lHUXgJRUZsd1McRAkr3Xq6Wk693YA0To/edit?usp=sharing
[3] https://lore.kernel.org/all/[email protected]/

[example 1]:
typedef struct codetag {
const char* file;
int line;
int counter;
} codetag;

void my_trampoline(func_ptr func, ...) {
static codetag callsite_data __section("alloc_tags") =

    { __callsite_FILE, __callsite_LINE, 0 };

callsite_data.counter++;
func(...);
}

__callsite_wrapper(my_trampoline)
__attribute__ ((always_inline))
static inline void foo1(void) {
printf("foo1 function\n");
}

__callsite_wrapper(my_trampoline)
__attribute__ ((always_inline))
static inline void foo2(void) {
printf("foo2 function\n");
}

void bar(void) {
foo1();
}

int main(int argc, char** argv) {
foo1();
foo2();
bar();
return 0;
}

Kent Overstreet (16):
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
nodemask: Split out include/linux/nodemask_types.h
prandom: Remove unused include
change alloc_pages name in ivpu_bo_ops to avoid conflicts mm/slub: Mark slab_free_freelist_hook() __always_inline
mempool: Hook up to memory allocation profiling
xfs: Memory allocation profiling fixups
timekeeping: Fix a circular include dependency
mm: percpu: Introduce pcpuobj_ext
mm: percpu: Add codetag reference into pcpuobj_ext
arm64: Fix circular header dependency
mm: vmalloc: Enable memory allocation profiling
rhashtable: Plumb through alloc tag
MAINTAINERS: Add entries for code tagging and memory allocation
profiling

Suren Baghdasaryan (23):
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
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/arm64/include/asm/spectre.h              |   4 +-
 arch/powerpc/mm/book3s64/radix_pgtable.c      |   2 +-
 arch/x86/kernel/amd_gart_64.c                 |   2 +-
 drivers/accel/ivpu/ivpu_gem.c                 |   8 +-
 drivers/accel/ivpu/ivpu_gem.h                 |   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/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                           | 111 +++++---
 include/linux/gfp_types.h                     | 101 +++++--
 include/linux/hrtimer.h                       |   2 +-
 include/linux/memcontrol.h                    |  56 +++-
 include/linux/mempool.h                       |  73 +++--
 include/linux/mm.h                            |   8 +
 include/linux/mm_types.h                      |   4 +-
 include/linux/nodemask.h                      |   2 +-
 include/linux/nodemask_types.h                |   9 +
 include/linux/page_ext.h                      |   1 -
 include/linux/pagemap.h                       |   9 +-
 include/linux/percpu.h                        |  23 +-
 include/linux/pgalloc_tag.h                   | 105 +++++++
 include/linux/prandom.h                       |   1 -
 include/linux/rhashtable-types.h              |  11 +-
 include/linux/sched.h                         |  26 +-
 include/linux/slab.h                          | 180 ++++++------
 include/linux/slab_def.h                      |   2 +-
 include/linux/slub_def.h                      |   4 +-
 include/linux/string.h                        |   4 +-
 include/linux/string_helpers.h                |  13 +-
 include/linux/time_namespace.h                |   2 +
 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                               | 212 ++++++++++++++
 lib/codetag.c                                 | 258 ++++++++++++++++++
 lib/rhashtable.c                              |  52 +++-
 lib/string_helpers.c                          |  24 +-
 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                                |  42 +--
 mm/mempool.c                                  |  34 +--
 mm/mm_init.c                                  |   1 +
 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.c                                     |  24 +-
 mm/slab.h                                     | 246 +++++++++++++----
 mm/slab_common.c                              |  96 +++++--
 mm/slub.c                                     |  26 +-
 mm/util.c                                     |  44 +--
 mm/vmalloc.c                                  |  88 +++---
 scripts/kallsyms.c                            |  13 +
 scripts/module.lds.S                          |   7 +
 85 files changed, 2117 insertions(+), 698 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/nodemask_types.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.42.0.758.gaed0368e0e-goog
author maintainer
#1 lore raw

PATCH v2 1/39 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.

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]>
---
 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                | 13 +++++-----
 lib/string_helpers.c                          | 24 +++++++++++++------
 lib/test-string_helpers.c                     |  4 ++--
 mm/hugetlb.c                                  |  8 +++----
 11 files changed, 44 insertions(+), 35 deletions(-)
arch/powerpc/mm/book3s64/radix_pgtable.c +1 -1
index c6a4ac766b2b..27aa5a083ff0 100644
@@ -260,7 +260,7 @@ print_mapping(unsigned long start, unsigned long end, unsigned long size, bool e
 	if (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
index 1fe011676d07..59140424d755 100644
@@ -986,9 +986,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
index 9d7bf8ee45f1..6b1748e1f666 100644
@@ -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
index 3a8f27c3e310..411dc8137f7c 100644
@@ -2511,7 +2511,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),
@@ -2707,7 +2707,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
index 6e163cb5b478..a1b61938fee2 100644
@@ -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", SPINOR_OP_CHIP_ERASE, buf);
 	}
 
drivers/net/ethernet/chelsio/cxgb4/cxgb4_debugfs.c +2 -2
index 14e0d989c3ba..7d5fbebd36fc 100644
@@ -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
index 83b6a3f3863b..c37593f76b65 100644
@@ -2689,10 +2689,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 +6 -7
index 9d1f5bb74dd5..a54467d891db 100644
@@ -17,15 +17,14 @@ 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),
 };
 
-void string_get_size(u64 size, u64 blk_size, enum string_size_units units,
-		     char *buf, int len);
+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 +17 -7
index 9982344cca34..b1496499b113 100644
@@ -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
  *
@@ -32,14 +38,16 @@
  * at least 9 bytes and will always be zero terminated.
  *
  */
-void string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
-		     char *buf, int len)
+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,
@@ -126,8 +134,10 @@ void string_get_size(u64 size, u64 blk_size, const enum string_size_units units,
 	else
 		unit = units_str[units][i];
 
-	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
index 9a68849a5d55..0b01ffca96fb 100644
@@ -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
index 52d26072dfda..37f2148d3b9c 100644
@@ -3228,7 +3228,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);
@@ -3290,7 +3290,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;
@@ -3336,7 +3336,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",
@@ -4227,7 +4227,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.42.0.758.gaed0368e0e-goog
maintainer
#2 lore raw

Re: [PATCH v2 01/39] lib/string_helpers: Add flags param to string_get_size()

(Minimized the list of people for my review / comments)

Quoted 7 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.

...

drivers/block/virtio_blk.c static void virtblk_update_capacity(struct virtio_blk *vblk, bool resize) · line 991

Quoted 3 lines
drivers/block/virtio_blk.c +1 -1
 	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));

This doesn't seem right (even if it works). We shouldn't rely on the implementation details.

...

drivers/scsi/sd.c sd_print_capacity(struct scsi_disk *sdkp, · line 2695

Quoted 5 lines
drivers/scsi/sd.c +2 -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, 0,
+			cap_str_10, sizeof(cap_str_10));

Neither this.

...

include/linux/string_helpers.h static inline bool string_is_terminated(const char *s, int len) · line 20

Quoted 6 lines
include/linux/string_helpers.h +1 -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 {

So, please add UNITS_10 as it is now. It will help if anybody in the future wants to add, e.g., 8-base numbers.

include/linux/string_helpers.h static inline bool string_is_terminated(const char *s, int len) · line 24

Quoted 4 lines
include/linux/string_helpers.h +3 -0
+	STRING_SIZE_BASE2	= (1 << 0),
+	STRING_SIZE_NOSPACE	= (1 << 1),
+	STRING_SIZE_NOBYTES	= (1 << 2),
 };

Please, add necessary comments.

...

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 */
+};

And what a point now in having these?

I assume you need to split this to a few patches:

1) rename parameter to be a flags without renaming the definitions (this will touch only string_helpers part);
2) do the end job by renaming it all over the drivers;
3) add the other flags one-by-one (each in a separate change);
4) use new flags where it's needed;

Also see below.

...

lib/string_helpers.c line 51

Quoted 8 lines
lib/string_helpers.c +2 -2
 	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"
 	};

Ouch, instead of leaving this and actually "cutting the letter" with NO* flags, you did something different.

...

Now the main part. Since in 50+% cases (I briefly estimated, it may be more) this is used in printf() why not introducing a new pointer extension for that?

Yes, it may be done separately, but it will look like a double effort to me. Instead it might give us a possibility to scale w/o touching users each time we want to do something and at the same time hide this complete API under printf() implementation.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#3 lore raw

Re: [PATCH v2 01/39] lib/string_helpers: Add flags param to string_get_size()

Quoted 2 lines · Andy Shevchenko

(Minimized the list of people for my review / comments)

Thanks! I'll let Kent address your feedback.

Quoted 96 lines · Suren Baghdasaryan
Quoted 7 lines

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.

...

drivers/block/virtio_blk.c static void virtblk_update_capacity(struct virtio_blk *vblk, bool resize) · line 991

Quoted 3 lines

string_get_size(nblocks, queue_logical_block_size(q),

drivers/block/virtio_blk.c +1 -1
-                     STRING_UNITS_10, cap_str_10, sizeof(cap_str_10));
+                     0, cap_str_10, sizeof(cap_str_10));

This doesn't seem right (even if it works). We shouldn't rely on the implementation details.

...

drivers/scsi/sd.c sd_print_capacity(struct scsi_disk *sdkp, · line 2695

Quoted 5 lines
drivers/scsi/sd.c +2 -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, 0,
+                     cap_str_10, sizeof(cap_str_10));

Neither this.

...

include/linux/string_helpers.h static inline bool string_is_terminated(const char *s, int len) · line 20

Quoted 6 lines
include/linux/string_helpers.h +1 -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 {

So, please add UNITS_10 as it is now. It will help if anybody in the future wants to add, e.g., 8-base numbers.

include/linux/string_helpers.h static inline bool string_is_terminated(const char *s, int len) · line 24

Quoted 4 lines
include/linux/string_helpers.h +3 -0
+     STRING_SIZE_BASE2       = (1 << 0),
+     STRING_SIZE_NOSPACE     = (1 << 1),
+     STRING_SIZE_NOBYTES     = (1 << 2),
 };

Please, add necessary comments.

...

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 */
+};

And what a point now in having these?

I assume you need to split this to a few patches:

1) rename parameter to be a flags without renaming the definitions (this will touch only string_helpers part);
2) do the end job by renaming it all over the drivers;
3) add the other flags one-by-one (each in a separate change);
4) use new flags where it's needed;

Also see below.

...

lib/string_helpers.c line 51

Quoted 8 lines
      static const char *const units_10[] = {
lib/string_helpers.c +2 -2
-             "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"
      };

Ouch, instead of leaving this and actually "cutting the letter" with NO* flags, you did something different.

...

Now the main part. Since in 50+% cases (I briefly estimated, it may be more) this is used in printf() why not introducing a new pointer extension for that?

Yes, it may be done separately, but it will look like a double effort to me. Instead it might give us a possibility to scale w/o touching users each time we want to do something and at the same time hide this complete API under printf() implementation.

--
With Best Regards,
Andy Shevchenko

maintainer
#4 lore raw

Re: [PATCH v2 01/39] lib/string_helpers: Add flags param to string_get_size()

In reply to Andy Shevchenko
Quoted 11 lines · Andy Shevchenko

(Minimized the list of people for my review / comments)

Quoted 7 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.

Quoted 8 lines

...

drivers/block/virtio_blk.c static void virtblk_update_capacity(struct virtio_blk *vblk, bool resize) · line 991

Quoted 3 lines
drivers/block/virtio_blk.c +1 -1
 	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));

This doesn't seem right (even if it works). We shouldn't rely on the implementation details.

It's now a flags parameter: passing an empty set of flags is not "relying on an implementation detail".

Quoted 9 lines

include/linux/string_helpers.h static inline bool string_is_terminated(const char *s, int len) · line 20

Quoted 6 lines
include/linux/string_helpers.h +1 -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 {

So, please add UNITS_10 as it is now. It will help if anybody in the future wants to add, e.g., 8-base numbers.

Octal human readable numbers? No, no one's wanted that so far and I very much doubt anyone will want that in the future.

Quoted 6 lines

include/linux/string_helpers.h static inline bool string_is_terminated(const char *s, int len) · line 24

Quoted 4 lines
include/linux/string_helpers.h +3 -0
+	STRING_SIZE_BASE2	= (1 << 0),
+	STRING_SIZE_NOSPACE	= (1 << 1),
+	STRING_SIZE_NOBYTES	= (1 << 2),
 };

Please, add necessary comments.

That I can do.

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 */
+};

And what a point now in having these?

Minimizing the size of the diff and making it more reviewable. It's fine as an internal implementation thing.

Quoted 8 lines

I assume you need to split this to a few patches:

1) rename parameter to be a flags without renaming the definitions (this will touch only string_helpers part);
2) do the end job by renaming it all over the drivers;
3) add the other flags one-by-one (each in a separate change);
4) use new flags where it's needed;

No, those would not be atomic changes. In particular changing the parameter to a flags without changing the callers - that's not how we do things.

We're currently working towards _better_ type safety for enums, fyi.

The new flags _could_ be a separate patch, but since it would be touching much the same code as the previous patch I don't see the point in splitting it.

Quoted 11 lines

lib/string_helpers.c line 51

Quoted 8 lines
lib/string_helpers.c +2 -2
 	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"
 	};

Ouch, instead of leaving this and actually "cutting the letter" with NO* flags, you did something different.

Not sure I understand your complaint? Were you attached to the redundant Bs?

Quoted 7 lines

Now the main part. Since in 50+% cases (I briefly estimated, it may be more) this is used in printf() why not introducing a new pointer extension for that?

Yes, it may be done separately, but it will look like a double effort to me. Instead it might give us a possibility to scale w/o touching users each time we want to do something and at the same time hide this complete API under printf() implementation.

No, I would not be in favor of another %p extension: in particular, since this takes integer inputs the lack of type safety for %p extensions coupled with C's very relaxed approach to integer type conversion would be a really nasty footgun.

maintainer
#5 lore raw

Re: [PATCH v2 01/39] lib/string_helpers: Add flags param to string_get_size()

Quoted 2 lines · Kent Overstreet
Quoted 1 line · Andy Shevchenko

On Tue, Oct 24, 2023 at 06:45:58AM -0700, Suren Baghdasaryan wrote:

...

Quoted 9 lines
Quoted 6 lines

drivers/block/virtio_blk.c static void virtblk_update_capacity(struct virtio_blk *vblk, bool resize) · line 991

Quoted 3 lines
drivers/block/virtio_blk.c +1 -1
 	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));

This doesn't seem right (even if it works). We shouldn't rely on the implementation details.

It's now a flags parameter: passing an empty set of flags is not "relying on an implementation detail".

0 is the "default" flag which is definitely relies on the "implementation detail". And I think that it's better that caller will explicitly tell what they want.

...

Quoted 10 lines
Quoted 9 lines

include/linux/string_helpers.h static inline bool string_is_terminated(const char *s, int len) · line 20

Quoted 6 lines
include/linux/string_helpers.h +1 -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 {

So, please add UNITS_10 as it is now. It will help if anybody in the future wants to add, e.g., 8-base numbers.

Quoted 2 lines

Octal human readable numbers? No, no one's wanted that so far and I very much doubt anyone will want that in the future.

I also in doubt, but still, the explicit is better than implicit in this case in my opinion.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
include/linux/string_helpers.h +3 -0
+	STRING_SIZE_BASE2	= (1 << 0),
+	STRING_SIZE_NOSPACE	= (1 << 1),
+	STRING_SIZE_NOBYTES	= (1 << 2),
 };

...

Quoted 9 lines
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 */
+};

And what a point now in having these?

Minimizing the size of the diff and making it more reviewable. It's fine as an internal implementation thing.

It's not an issue to rename these all over the places as you already did that for most of the users. And minimizing diff could be done better with --histogram algorithm or so. Otherwise it is not an objective here, right?

...

Quoted 12 lines
Quoted 7 lines

I assume you need to split this to a few patches:

1) rename parameter to be a flags without renaming the definitions (this will touch only string_helpers part);
2) do the end job by renaming it all over the drivers;
3) add the other flags one-by-one (each in a separate change);
4) use new flags where it's needed;

No, those would not be atomic changes. In particular changing the parameter to a flags without changing the callers - that's not how we do things.

Quoted 5 lines

We're currently working towards _better_ type safety for enums, fyi.

The new flags _could_ be a separate patch, but since it would be touching much the same code as the previous patch I don't see the point in splitting it.

Individual flags can be discussed, objected or approved and won't affect the rest of the changes. That's why I highly recommend to reconsider splitting of this change.

It would be possible to squash back if maintainer wants this, but from review perspective you are adding more burden to the reviewer's shoulders is not good.

...

Quoted 12 lines
Quoted 11 lines

lib/string_helpers.c line 51

Quoted 8 lines
lib/string_helpers.c +2 -2
 	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"
 	};

Ouch, instead of leaving this and actually "cutting the letter" with NO* flags, you did something different.

Quoted 2 lines

Not sure I understand your complaint? Were you attached to the redundant Bs?

Flag means "cutting" while in the code you "adding" (doing the opposite). Why not do exactly "cutting" without touching there. Or since you mentioned changes across the all callers, make them explicitly tell that they want Bytes suffix.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#6 lore raw

Re: [PATCH v2 01/39] lib/string_helpers: Add flags param to string_get_size()

Quoted 100 lines · Andy Shevchenko
Quoted 2 lines · Kent Overstreet
Quoted 1 line · Andy Shevchenko

On Tue, Oct 24, 2023 at 06:45:58AM -0700, Suren Baghdasaryan wrote:

...

Quoted 9 lines
Quoted 6 lines

drivers/block/virtio_blk.c static void virtblk_update_capacity(struct virtio_blk *vblk, bool resize) · line 991

Quoted 3 lines
drivers/block/virtio_blk.c +1 -1
 	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));

This doesn't seem right (even if it works). We shouldn't rely on the implementation details.

It's now a flags parameter: passing an empty set of flags is not "relying on an implementation detail".

0 is the "default" flag which is definitely relies on the "implementation detail". And I think that it's better that caller will explicitly tell what they want.

...

Quoted 10 lines
Quoted 9 lines

include/linux/string_helpers.h static inline bool string_is_terminated(const char *s, int len) · line 20

Quoted 6 lines
include/linux/string_helpers.h +1 -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 {

So, please add UNITS_10 as it is now. It will help if anybody in the future wants to add, e.g., 8-base numbers.

Quoted 2 lines

Octal human readable numbers? No, no one's wanted that so far and I very much doubt anyone will want that in the future.

I also in doubt, but still, the explicit is better than implicit in this case in my opinion.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
include/linux/string_helpers.h +3 -0
+	STRING_SIZE_BASE2	= (1 << 0),
+	STRING_SIZE_NOSPACE	= (1 << 1),
+	STRING_SIZE_NOBYTES	= (1 << 2),
 };

...

Quoted 9 lines
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 */
+};

And what a point now in having these?

Minimizing the size of the diff and making it more reviewable. It's fine as an internal implementation thing.

It's not an issue to rename these all over the places as you already did that for most of the users. And minimizing diff could be done better with --histogram algorithm or so. Otherwise it is not an objective here, right?

...

Quoted 12 lines
Quoted 7 lines

I assume you need to split this to a few patches:

1) rename parameter to be a flags without renaming the definitions (this will touch only string_helpers part);
2) do the end job by renaming it all over the drivers;
3) add the other flags one-by-one (each in a separate change);
4) use new flags where it's needed;

No, those would not be atomic changes. In particular changing the parameter to a flags without changing the callers - that's not how we do things.

Quoted 5 lines

We're currently working towards _better_ type safety for enums, fyi.

The new flags _could_ be a separate patch, but since it would be touching much the same code as the previous patch I don't see the point in splitting it.

Individual flags can be discussed, objected or approved and won't affect the rest of the changes. That's why I highly recommend to reconsider splitting of this change.

It would be possible to squash back if maintainer wants this, but from review perspective you are adding more burden to the reviewer's shoulders is not good.

...

Quoted 12 lines
Quoted 11 lines

lib/string_helpers.c line 51

Quoted 8 lines
lib/string_helpers.c +2 -2
 	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"
 	};

Ouch, instead of leaving this and actually "cutting the letter" with NO* flags, you did something different.

Quoted 6 lines
Quoted 2 lines

Not sure I understand your complaint? Were you attached to the redundant Bs?

Flag means "cutting" while in the code you "adding" (doing the opposite). Why not do exactly "cutting" without touching there. Or since you mentioned changes across the all callers, make them explicitly tell that they want Bytes suffix.

Andy: to be blunt, you've been pretty hostile and hysterical ("breaking the kernel!" over debug statements? really?) and the bikeshedding is getting to be too much - I'm just going to drop this patch from the series and we'll post process the output as needed.

#7 lore raw

Re: [PATCH v2 01/39] lib/string_helpers: Add flags param to string_get_size()

Quoted 4 lines · Kent Overstreet
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Kent Overstreet
Quoted 1 line · Andy Shevchenko

On Tue, Oct 24, 2023 at 06:45:58AM -0700, Suren Baghdasaryan wrote:

...

Quoted 2 lines

Andy: to be blunt, you've been pretty hostile and hysterical ("breaking the kernel!" over debug statements? really?)

Where did I say that?

And instead of judging me, look from the reviewer's and maintainer's point of view. The patch is unreviewable and has not good maintainability in this form as it does a few things at once.

Also the commit message is poorly written as it has no mention about use cases.

Quoted 3 lines

and the bikeshedding is
getting to be too much - I'm just going to drop this patch from the series and we'll post process the output as needed.

I don't think I bikeshed here too much. This is the regular asking the reviewers and/or maintainers usually do, so is it too hard to split?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#8 lore raw

Re: [PATCH v2 01/39] lib/string_helpers: Add flags param to string_get_size()

Quoted 2 lines · Andy Shevchenko

I don't think I bikeshed here too much. This is the regular asking the reviewers and/or maintainers usually do, so is it too hard to split?

Excuse me?

You were objecting to the new approach making it impossible to add _octal_ support, and your proposed patch split was completely broken.

Are you trying to be more reasonable now...?

author maintainer
#9 lore raw

PATCH v2 2/39 scripts/kallysms: Always include __start and __stop symbols

In reply to 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.

---
 scripts/kallsyms.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
scripts/kallsyms.c +13 -0
index 653b92f6d4c8..47978efe4797 100644
@@ -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.42.0.758.gaed0368e0e-goog
author maintainer
#10 lore raw

PATCH v2 3/39 fs: Convert alloc_inode_sb() to a macro

In reply to 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.

---
 include/linux/fs.h | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
include/linux/fs.h +1 -5
index 4a40823c3c67..c545b1839e96 100644
@@ -2862,11 +2862,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.42.0.758.gaed0368e0e-goog
author maintainer
#11 lore raw

PATCH v2 4/39 nodemask: Split out include/linux/nodemask_types.h

In reply to Suren Baghdasaryan

From: Kent Overstreet <[email protected]>

sched.h, which defines task_struct, needs nodemask_t - but sched.h is a frequently used header and ideally shouldn't be pulling in any more code that it needs to.

This splits out nodemask_types.h which has the definition sched.h needs, which will avoid a circular header dependency in the alloc tagging patch series, and as a bonus should speed up kernel build times.

---
 include/linux/nodemask.h       | 2 +-
 include/linux/nodemask_types.h | 9 +++++++++
 include/linux/sched.h          | 2 +-
 3 files changed, 11 insertions(+), 2 deletions(-)
 create mode 100644 include/linux/nodemask_types.h
include/linux/nodemask.h +1 -1
index 8d07116caaf1..b61438313a73 100644
@@ -93,10 +93,10 @@
 #include <linux/threads.h>
 #include <linux/bitmap.h>
 #include <linux/minmax.h>
+#include <linux/nodemask_types.h>
 #include <linux/numa.h>
 #include <linux/random.h>
 
-typedef struct { DECLARE_BITMAP(bits, MAX_NUMNODES); } nodemask_t;
 extern nodemask_t _unused_nodemask_arg_;
 
 /**
include/linux/nodemask_types.h new file +9 -0
new file mode 100644
index 000000000000..84c2f47c4237
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __LINUX_NODEMASK_TYPES_H
+#define __LINUX_NODEMASK_TYPES_H
+
+#include <linux/numa.h>
+
+typedef struct { DECLARE_BITMAP(bits, MAX_NUMNODES); } nodemask_t;
+
+#endif /* __LINUX_NODEMASK_TYPES_H */
include/linux/sched.h +1 -1
index 77f01ac385f7..12a2554a3164 100644
@@ -20,7 +20,7 @@
 #include <linux/hrtimer.h>
 #include <linux/irqflags.h>
 #include <linux/seccomp.h>
-#include <linux/nodemask.h>
+#include <linux/nodemask_types.h>
 #include <linux/rcupdate.h>
 #include <linux/refcount.h>
 #include <linux/resource.h>
Signature · 1 line
2.42.0.758.gaed0368e0e-goog
author maintainer
#12 lore raw

PATCH v2 5/39 prandom: Remove unused include

In reply to Suren Baghdasaryan

From: Kent Overstreet <[email protected]>

prandom.h doesn't use percpu.h - this fixes some circular header issues.

---
 include/linux/prandom.h | 1 -
 1 file changed, 1 deletion(-)
include/linux/prandom.h +0 -1
index f2ed5b72b3d6..f7f1e5251c67 100644
@@ -10,7 +10,6 @@
 
 #include <linux/types.h>
 #include <linux/once.h>
-#include <linux/percpu.h>
 #include <linux/random.h>
 
 struct rnd_state {
Signature · 1 line
2.42.0.758.gaed0368e0e-goog
author maintainer
#13 lore raw

PATCH v2 6/39 mm: enumerate all gfp flags

In reply to 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_SHIFT calculation.

---
 include/linux/gfp_types.h | 90 +++++++++++++++++++++++++++------------
 1 file changed, 62 insertions(+), 28 deletions(-)
include/linux/gfp_types.h +62 -28
index 6583a58670c5..3fbe624763d9 100644
@@ -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.42.0.758.gaed0368e0e-goog
#14 lore raw

Re: [PATCH v2 06/39] mm: enumerate all gfp flags

Quoted 62 lines · On Tue, 24 Oct 2023 06:46:03 -0700 Suren Baghdasaryan <[email protected]> wrote:

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_SHIFT calculation.

---
 include/linux/gfp_types.h | 90 +++++++++++++++++++++++++++------------
 1 file changed, 62 insertions(+), 28 deletions(-)
include/linux/gfp_types.h +35 -9
index 6583a58670c5..3fbe624763d9 100644
@@ -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

include/linux/gfp_types.h typedef unsigned int __bitwise gfp_t; · line 69

Quoted 10 lines
include/linux/gfp_types.h +9 -0
+#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 */

This comment can be also removed here, because it is already stated above with the definition of ___GFP_UNUSED_BIT.

Then again, I think that the GFP bits have never been compacted after Neil Brown removed __GFP_ATOMIC with commit 2973d8229b78 simply because that would mean changing definitions of all subsequent GFP flags. FWIW I am not aware of any code that would depend on the numeric value of ___GFP_* macros, so this patch seems like a good opportunity to change the numbering and get rid of this unused 0x200u altogether.

@Neil: I have added you to the conversation in case you want to correct my understanding of the unused bit.

Other than that LGTM.

Petr T

Quoted 56 lines
include/linux/gfp_types.h +18 -19
-#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))
 
 /**
author maintainer
#15 lore raw

Re: [PATCH v2 06/39] mm: enumerate all gfp flags

Quoted 71 lines · Petr Tesařík
Quoted 67 lines · On Tue, 24 Oct 2023 06:46:03 -0700 Suren Baghdasaryan <[email protected]> wrote:

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_SHIFT calculation.

---
 include/linux/gfp_types.h | 90 +++++++++++++++++++++++++++------------
 1 file changed, 62 insertions(+), 28 deletions(-)
include/linux/gfp_types.h +40 -9
index 6583a58670c5..3fbe624763d9 100644
@@ -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)
Quoted 8 lines

include/linux/gfp_types.h typedef unsigned int __bitwise gfp_t; · line 69

Quoted 5 lines
include/linux/gfp_types.h +4 -0
+#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 */

This comment can be also removed here, because it is already stated above with the definition of ___GFP_UNUSED_BIT.

Ack.

Quoted 10 lines

Then again, I think that the GFP bits have never been compacted after Neil Brown removed __GFP_ATOMIC with commit 2973d8229b78 simply because that would mean changing definitions of all subsequent GFP flags. FWIW I am not aware of any code that would depend on the numeric value of ___GFP_* macros, so this patch seems like a good opportunity to change the numbering and get rid of this unused 0x200u altogether.

@Neil: I have added you to the conversation in case you want to correct my understanding of the unused bit.

Hmm. I would prefer to do that in a separate patch even though it would be a one-line change. Seems safer to me in case something goes wrong and we have to bisect and revert it. If that sounds ok I'll post that in the next version.

Quoted 2 lines

Other than that LGTM.

Thanks for the review!
Suren.

Quoted 61 lines

Petr T

Quoted 56 lines
include/linux/gfp_types.h +18 -19
-#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))
 
 /**
#16 lore raw

Re: [PATCH v2 06/39] mm: enumerate all gfp flags

Quoted 91 lines · On Wed, 25 Oct 2023 08:28:32 -0700 Suren Baghdasaryan <[email protected]> wrote:
Quoted 71 lines · Petr Tesařík
Quoted 67 lines · On Tue, 24 Oct 2023 06:46:03 -0700 Suren Baghdasaryan <[email protected]> wrote:

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_SHIFT calculation.

---
 include/linux/gfp_types.h | 90 +++++++++++++++++++++++++++------------
 1 file changed, 62 insertions(+), 28 deletions(-)
include/linux/gfp_types.h +40 -9
index 6583a58670c5..3fbe624763d9 100644
@@ -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)
Quoted 8 lines

include/linux/gfp_types.h typedef unsigned int __bitwise gfp_t; · line 69

Quoted 5 lines
include/linux/gfp_types.h +4 -0
+#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 */  

This comment can be also removed here, because it is already stated above with the definition of ___GFP_UNUSED_BIT.

Ack.

Quoted 8 lines

Then again, I think that the GFP bits have never been compacted after Neil Brown removed __GFP_ATOMIC with commit 2973d8229b78 simply because that would mean changing definitions of all subsequent GFP flags. FWIW I am not aware of any code that would depend on the numeric value of ___GFP_* macros, so this patch seems like a good opportunity to change the numbering and get rid of this unused 0x200u altogether.

Quoted 7 lines
Quoted 2 lines

@Neil: I have added you to the conversation in case you want to correct my understanding of the unused bit.

Hmm. I would prefer to do that in a separate patch even though it would be a one-line change. Seems safer to me in case something goes wrong and we have to bisect and revert it. If that sounds ok I'll post that in the next version.

You're right. If something does go wrong, it will be easier to fix if the removal of the unused bit is in a commit of its own.

Petr T

author maintainer
#17 lore raw

PATCH v2 7/39 mm: introduce slabobj_ext to support slab object extensions

In reply to Suren Baghdasaryan

Currently 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                  | 148 +++++++++++++++++++++++++------------
 mm/slab_common.c           |  47 ++++++++++++
 9 files changed, 185 insertions(+), 114 deletions(-)
include/linux/memcontrol.h +14 -6
index e4e24da16d2c..4b17ebb7e723 100644
@@ -346,8 +346,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 */
@@ -385,7 +385,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);
@@ -406,7 +406,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);
@@ -503,7 +503,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) {
@@ -549,7 +549,7 @@ static inline struct mem_cgroup *get_mem_cgroup_from_objcg(struct obj_cgroup *ob
 static 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;
 }
 
@@ -1593,6 +1593,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
index 36c5b43999e6..5b55c4752c23 100644
@@ -180,7 +180,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
 
@@ -315,7 +315,7 @@ struct folio {
 			};
 			atomic_t _mapcount;
 			atomic_t _refcount;
-#ifdef CONFIG_MEMCG
+#ifdef CONFIG_SLAB_OBJ_EXT
 			unsigned long memcg_data;
 #endif
 	/* private: the union with struct page is transitional */
init/Kconfig +4 -0
index 6d35728b94b2..78a7abe36037 100644
@@ -937,10 +937,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
index 3872528d0963..02b744d2e07d 100644
@@ -599,9 +599,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
 	}
 
@@ -649,8 +649,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));
 	}
@@ -1143,8 +1143,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
index f46fbb03062b..084f5f36e8e7 100644
@@ -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
index 4e2723e1b300..de6ea5746acd 100644
@@ -372,7 +372,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 +101 -47

248 lines not on this page · raw message

mm/slab_common.c +47 -0

53 lines not on this page · raw message

Signature · 1 line
2.42.0.758.gaed0368e0e-goog
#18 lore raw

Re: [PATCH v2 07/39] mm: introduce slabobj_ext to support slab object extensions

kernel test robot noticed the following build warnings:

Hi Suren,

kernel test robot noticed the following build warnings:

[auto build test WARNING on vbabka-slab/for-next]
[also build test WARNING on kees/for-next/hardening linus/master v6.6-rc7] [cannot apply to akpm-mm/mm-everything next-20231024]
[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/20231024-215116
base:   git://git.kernel.org/pub/scm/linux/kernel/git/vbabka/slab.git for-next
patch link:    https://lore.kernel.org/r/20231024134637.3120277-8-surenb%40google.com
patch subject: [PATCH v2 07/39] mm: introduce slabobj_ext to support slab object extensions
config: m68k-allyesconfig (https://download.01.org/0day-ci/archive/20231025/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231025/[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 warnings (new ones prefixed by >>):

In file included from mm/mempool.c:21:
mm/slab.h: In function 'slab_post_alloc_hook':

>> mm/slab.h:778:29: warning: variable 'obj_exts' set but not used [-Wunused-but-set-variable]
     778 |         struct slabobj_ext *obj_exts;
         |                             ^~~~~~~~

vim +/obj_exts +778 mm/slab.h

771
772 static inline void slab_post_alloc_hook(struct kmem_cache *s,
773 struct obj_cgroup *objcg, gfp_t flags,
774 size_t size, void **p, bool init,
775 unsigned int orig_size)
776 {
777 unsigned int zero_size = s->object_size;

 > 778		struct slabobj_ext *obj_exts;

779 bool kasan_init = init;
780 size_t i;
781
782 flags &= gfp_allowed_mask;
783
784 /*
785 * For kmalloc object, the allocated memory size(object_size) is likely
786 * larger than the requested size(orig_size). If redzone check is
787 * enabled for the extra space, don't zero it, as it will be redzoned
788 * soon. The redzone operation for this extra space could be seen as a
789 * replacement of current poisoning under certain debug option, and
790 * won't break other sanity checks.
791 */
792 if (kmem_cache_debug_flags(s, SLAB_STORE_USER | SLAB_RED_ZONE) &&
793 (s->flags & SLAB_KMALLOC))
794 zero_size = orig_size;
795
796 /*
797 * When slub_debug is enabled, avoid memory initialization integrated
798 * into KASAN and instead zero out the memory via the memset below with
799 * the proper size. Otherwise, KASAN might overwrite SLUB redzones and
800 * cause false-positive reports. This does not lead to a performance
801 * penalty on production builds, as slub_debug is not intended to be
802 * enabled there.
803 */
804 if (__slub_debug_enabled())
805 kasan_init = false;
806
807 /*
808 * As memory initialization might be integrated into KASAN,
809 * kasan_slab_alloc and initialization memset must be
810 * kept together to avoid discrepancies in behavior.
811 *
812 * As p[i] might get tagged, memset and kmemleak hook come after KASAN.
813 */
814 for (i = 0; i < size; i++) {
815 p[i] = kasan_slab_alloc(s, p[i], flags, kasan_init);
816 if (p[i] && init && (!kasan_init || !kasan_has_integrated_init()))
817 memset(p[i], 0, zero_size);
818 kmemleak_alloc_recursive(p[i], s->object_size, 1,
819 s->flags, flags);
820 kmsan_slab_alloc(s, p[i], flags);
821 obj_exts = prepare_slab_obj_exts_hook(s, flags, p[i]);
822 }
823
824 memcg_slab_post_alloc_hook(s, objcg, flags, size, p);
825 }
826

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#19 lore raw

Re: [PATCH v2 07/39] mm: introduce slabobj_ext to support slab object extensions

In reply to Suren Baghdasaryan
kernel test robot noticed the following build warnings:

Hi Suren,

kernel test robot noticed the following build warnings:

[auto build test WARNING on vbabka-slab/for-next]
[also build test WARNING on kees/for-next/hardening linus/master v6.6-rc7] [cannot apply to akpm-mm/mm-everything next-20231024]
[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/20231024-215116
base:   git://git.kernel.org/pub/scm/linux/kernel/git/vbabka/slab.git for-next
patch link:    https://lore.kernel.org/r/20231024134637.3120277-8-surenb%40google.com
patch subject: [PATCH v2 07/39] mm: introduce slabobj_ext to support slab object extensions
config: loongarch-allyesconfig (https://download.01.org/0day-ci/archive/20231025/[email protected]/config)
compiler: loongarch64-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231025/[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 warnings (new ones prefixed by >>):

In file included from mm/kfence/kfence.h:17,

                    from mm/kfence/core.c:36:
   mm/kfence/../slab.h: In function 'slab_post_alloc_hook':
>> mm/kfence/../slab.h:778:29: warning: variable 'obj_exts' set but not used [-Wunused-but-set-variable]
     778 |         struct slabobj_ext *obj_exts;
         |                             ^~~~~~~~

vim +/obj_exts +778 mm/kfence/../slab.h

771
772 static inline void slab_post_alloc_hook(struct kmem_cache *s,
773 struct obj_cgroup *objcg, gfp_t flags,
774 size_t size, void **p, bool init,
775 unsigned int orig_size)
776 {
777 unsigned int zero_size = s->object_size;

 > 778		struct slabobj_ext *obj_exts;

779 bool kasan_init = init;
780 size_t i;
781
782 flags &= gfp_allowed_mask;
783
784 /*
785 * For kmalloc object, the allocated memory size(object_size) is likely
786 * larger than the requested size(orig_size). If redzone check is
787 * enabled for the extra space, don't zero it, as it will be redzoned
788 * soon. The redzone operation for this extra space could be seen as a
789 * replacement of current poisoning under certain debug option, and
790 * won't break other sanity checks.
791 */
792 if (kmem_cache_debug_flags(s, SLAB_STORE_USER | SLAB_RED_ZONE) &&
793 (s->flags & SLAB_KMALLOC))
794 zero_size = orig_size;
795
796 /*
797 * When slub_debug is enabled, avoid memory initialization integrated
798 * into KASAN and instead zero out the memory via the memset below with
799 * the proper size. Otherwise, KASAN might overwrite SLUB redzones and
800 * cause false-positive reports. This does not lead to a performance
801 * penalty on production builds, as slub_debug is not intended to be
802 * enabled there.
803 */
804 if (__slub_debug_enabled())
805 kasan_init = false;
806
807 /*
808 * As memory initialization might be integrated into KASAN,
809 * kasan_slab_alloc and initialization memset must be
810 * kept together to avoid discrepancies in behavior.
811 *
812 * As p[i] might get tagged, memset and kmemleak hook come after KASAN.
813 */
814 for (i = 0; i < size; i++) {
815 p[i] = kasan_slab_alloc(s, p[i], flags, kasan_init);
816 if (p[i] && init && (!kasan_init || !kasan_has_integrated_init()))
817 memset(p[i], 0, zero_size);
818 kmemleak_alloc_recursive(p[i], s->object_size, 1,
819 s->flags, flags);
820 kmsan_slab_alloc(s, p[i], flags);
821 obj_exts = prepare_slab_obj_exts_hook(s, flags, p[i]);
822 }
823
824 memcg_slab_post_alloc_hook(s, objcg, flags, size, p);
825 }
826

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author maintainer
#20 lore raw

PATCH v2 8/39 mm: introduce __GFP_NO_OBJ_EXT flag to selectively prevent slabobj_ext creation

In reply to Suren Baghdasaryan

Introduce __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
index 3fbe624763d9..1c6573d69347 100644
@@ -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.42.0.758.gaed0368e0e-goog
author maintainer
#21 lore raw

PATCH v2 9/39 mm/slab: introduce SLAB_NO_OBJ_EXT to avoid obj_ext creation

In reply to Suren Baghdasaryan

Slab 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/slab.c            | 2 +-
 mm/slub.c            | 5 +++--
 3 files changed, 11 insertions(+), 3 deletions(-)
include/linux/slab.h +7 -0
index 8228d1276a2f..11ef3d364b2b 100644
@@ -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/slab.c +1 -1
index 9ad3d0f2d1a5..cefcb7499b6c 100644
@@ -1232,7 +1232,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);
 	list_add(&kmem_cache->list, &slab_caches);
 	slab_state = PARTIAL;
 
mm/slub.c +3 -2
index f7940048138c..d16643492320 100644
@@ -5043,7 +5043,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);
 
@@ -5053,7 +5054,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.42.0.758.gaed0368e0e-goog
author maintainer
#22 lore raw

PATCH v2 10/39 mm: prevent slabobj_ext allocations for slabobj_ext and kmem_cache objects

In reply to Suren Baghdasaryan

Use __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
index 5a47125469f1..187acc593397 100644
@@ -489,6 +489,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
index 2b42a9d2c11c..446f406d2703 100644
@@ -222,6 +222,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.42.0.758.gaed0368e0e-goog
author maintainer
#23 lore raw

PATCH v2 11/39 slab: objext: introduce objext_flags as extension to page_memcg_data_flags

In reply to Suren Baghdasaryan

Introduce 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
index 4b17ebb7e723..f3ede28b6fa6 100644
@@ -354,7 +354,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);
 
@@ -388,7 +403,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);
 }
 
 /*
@@ -409,7 +424,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);
 }
 
 /*
@@ -466,11 +481,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);
 }
 
 /*
@@ -509,11 +524,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
index 187acc593397..60417fd262ea 100644
@@ -448,10 +448,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.42.0.758.gaed0368e0e-goog
author maintainer
#24 lore raw

PATCH v2 12/39 lib: code tagging framework

In reply to 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.

---
 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
new file mode 100644
index 000000000000..a9d7adecc2a5
@@ -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
index fa307f93fa2e..2acbef24e93e 100644
@@ -962,6 +962,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
index 740109b6e2c8..b50212b5b999 100644
@@ -233,6 +233,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.42.0.758.gaed0368e0e-goog
author maintainer
#25 lore raw

PATCH v2 13/39 lib: code tagging module support

In reply to Suren Baghdasaryan

Add 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
index a9d7adecc2a5..386733e89b31 100644
@@ -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
index 98fedfdb8db5..c0d3f562c7ab 100644
@@ -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);
 
 	/*
@@ -2975,6 +2977,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.42.0.758.gaed0368e0e-goog
author maintainer
#26 lore raw

PATCH v2 14/39 lib: prevent module unloading if memory is not freed

In reply to Suren Baghdasaryan

Skip 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
index 386733e89b31..d98e4c8e86f0 100644
@@ -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
index c0d3f562c7ab..079f40792ce8 100644
@@ -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)
@@ -2295,7 +2302,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;
 }
 
@@ -2425,7 +2432,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
index 4ea57fb37346..0ad4ea66c769 100644
@@ -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.42.0.758.gaed0368e0e-goog
author maintainer
#27 lore raw

PATCH v2 15/39 lib: add allocation tagging support for memory allocation profiling

In reply to Suren Baghdasaryan

Introduce 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
index 45ba1f4dc004..0a012ac13a38 100644
@@ -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
index 2b59cff8be17..41a7e6d95fe8 100644
@@ -688,6 +688,7 @@ files are there, and which are missing.
  ============ ===============================================================
  File         Content
  ============ ===============================================================
+ allocinfo    Memory allocations profiling information
  apm          Advanced power management info
  buddyinfo    Kernel memory allocator information (see text)	(2.5)
  bus          Directory containing bus specific information
@@ -947,6 +948,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
new file mode 100644
index 000000000000..64f536b80380
@@ -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
index 67d8dd2f1bde..f04661824939 100644
@@ -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
@@ -367,6 +369,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
index 12a2554a3164..d35901d8de06 100644
@@ -767,6 +767,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;
@@ -806,6 +810,7 @@ struct task_struct {
 	struct task_group		*sched_task_group;
 #endif
 
+
 #ifdef CONFIG_UCLAMP_TASK
 	/*
 	 * Clamp values requested for a scheduling entity.
@@ -2457,4 +2462,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
index 2acbef24e93e..475a14e70566 100644
@@ -966,6 +966,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
index b50212b5b999..4525469637fe 100644
@@ -234,6 +234,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
index bf5bcf2836d8..45c67a0994f3 100644
@@ -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.42.0.758.gaed0368e0e-goog
author maintainer
#28 lore raw

PATCH v2 16/39 lib: introduce support for page allocation tagging

In reply to Suren Baghdasaryan

Introduce helper functions to easily instrument page allocators by storing a pointer to the allocation tag associated with the code that allocated the page in a page_ext field.

---
 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
index be98564191e6..07e0656898f9 100644
@@ -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
index 475a14e70566..e1eda1450d68 100644
@@ -972,6 +972,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
index 4fc031f9cefd..2d5226d9262d 100644
@@ -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
index 50f2f34745af..8e72e431dc35 100644
@@ -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 "internal.h"
mm/page_alloc.c +4 -0
index 95546f376302..d490d0f73e72 100644
@@ -52,6 +52,7 @@
 #include <linux/psi.h>
 #include <linux/khugepaged.h>
 #include <linux/delayacct.h>
+#include <linux/pgalloc_tag.h>
 #include <asm/div64.h>
 #include "internal.h"
 #include "shuffle.h"
@@ -1093,6 +1094,7 @@ static __always_inline bool free_pages_prepare(struct page *page,
 			__memcg_kmem_uncharge_page(page, order);
 		reset_page_owner(page, order);
 		page_table_check_free(page, order);
+		pgalloc_tag_sub(page, order);
 		return false;
 	}
 
@@ -1135,6 +1137,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),
@@ -1535,6 +1538,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
index 4548fcc66d74..3c58fe8a24df 100644
@@ -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.42.0.758.gaed0368e0e-goog
author maintainer
#29 lore raw

PATCH v2 17/39 change alloc_pages name in dma_map_ops to avoid name conflicts

In reply to Suren Baghdasaryan

After 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/x86/kernel/amd_gart_64.c | 2 +-
 drivers/iommu/dma-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 ++--
 6 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/kernel/amd_gart_64.c +1 -1
index 56a917df410d..842a0ec5eaa9 100644
@@ -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
index 4b1a88f514c9..28b7b2d10655 100644
@@ -1603,7 +1603,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/xen/grant-dma-ops.c +1 -1
index 76f6f26265a3..29257d2639db 100644
@@ -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
index 946bd56f0ac5..4f1e3f1fc44e 100644
@@ -403,6 +403,6 @@ 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,
 };
include/linux/dma-map-ops.h +1 -1
index f2fc203fb8a1..3a8a015fdd2e 100644
@@ -28,7 +28,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
index e323ca48f7f2..58e490e2cfb4 100644
@@ -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.42.0.758.gaed0368e0e-goog
author maintainer
#30 lore raw

PATCH v2 18/39 change alloc_pages name in ivpu_bo_ops to avoid conflicts

In reply to Suren Baghdasaryan

From: Kent Overstreet <[email protected]>

After 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.

---
 drivers/accel/ivpu/ivpu_gem.c | 8 ++++----
 drivers/accel/ivpu/ivpu_gem.h | 2 +-
 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/accel/ivpu/ivpu_gem.c +4 -4
index d09f13b35902..d324eaf5bbe3 100644
@@ -61,7 +61,7 @@ static void prime_unmap_pages_locked(struct ivpu_bo *bo)
 static const struct ivpu_bo_ops prime_ops = {
 	.type = IVPU_BO_TYPE_PRIME,
 	.name = "prime",
-	.alloc_pages = prime_alloc_pages_locked,
+	.alloc_pages_op = prime_alloc_pages_locked,
 	.free_pages = prime_free_pages_locked,
 	.map_pages = prime_map_pages_locked,
 	.unmap_pages = prime_unmap_pages_locked,
@@ -134,7 +134,7 @@ static void ivpu_bo_unmap_pages_locked(struct ivpu_bo *bo)
 static const struct ivpu_bo_ops shmem_ops = {
 	.type = IVPU_BO_TYPE_SHMEM,
 	.name = "shmem",
-	.alloc_pages = shmem_alloc_pages_locked,
+	.alloc_pages_op = shmem_alloc_pages_locked,
 	.free_pages = shmem_free_pages_locked,
 	.map_pages = ivpu_bo_map_pages_locked,
 	.unmap_pages = ivpu_bo_unmap_pages_locked,
@@ -186,7 +186,7 @@ static void internal_free_pages_locked(struct ivpu_bo *bo)
 static const struct ivpu_bo_ops internal_ops = {
 	.type = IVPU_BO_TYPE_INTERNAL,
 	.name = "internal",
-	.alloc_pages = internal_alloc_pages_locked,
+	.alloc_pages_op = internal_alloc_pages_locked,
 	.free_pages = internal_free_pages_locked,
 	.map_pages = ivpu_bo_map_pages_locked,
 	.unmap_pages = ivpu_bo_unmap_pages_locked,
@@ -200,7 +200,7 @@ static int __must_check ivpu_bo_alloc_and_map_pages_locked(struct ivpu_bo *bo)
 	lockdep_assert_held(&bo->lock);
 	drm_WARN_ON(&vdev->drm, bo->sgt);
 
-	ret = bo->ops->alloc_pages(bo);
+	ret = bo->ops->alloc_pages_op(bo);
 	if (ret) {
 		ivpu_err(vdev, "Failed to allocate pages for BO: %d", ret);
 		return ret;
drivers/accel/ivpu/ivpu_gem.h +1 -1
index 6b0ceda5f253..b81cf2af0b2d 100644
@@ -42,7 +42,7 @@ enum ivpu_bo_type {
 struct ivpu_bo_ops {
 	enum ivpu_bo_type type;
 	const char *name;
-	int (*alloc_pages)(struct ivpu_bo *bo);
+	int (*alloc_pages_op)(struct ivpu_bo *bo);
 	void (*free_pages)(struct ivpu_bo *bo);
 	int (*map_pages)(struct ivpu_bo *bo);
 	void (*unmap_pages)(struct ivpu_bo *bo);
Signature · 1 line
2.42.0.758.gaed0368e0e-goog
author maintainer
#31 lore raw

PATCH v2 19/39 mm: enable page allocation tagging

In reply to Suren Baghdasaryan

Redefine 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       | 111 +++++++++++++++++++++++---------------
 include/linux/pagemap.h   |   9 ++--
 mm/compaction.c           |   7 ++-
 mm/filemap.c              |   6 +--
 mm/mempolicy.c            |  42 +++++++--------
 mm/page_alloc.c           |  60 ++++++++++-----------
 7 files changed, 144 insertions(+), 101 deletions(-)
include/linux/alloc_tag.h +10 -0
index cf55a149fa84..6fa8a94d8bc1 100644
@@ -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 +68 -43

200 lines not on this page · raw message

include/linux/pagemap.h +6 -3
index 351c3b7f93a1..cb387321f929 100644
@@ -508,14 +508,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
index 38c8d216c6a3..74a7ec71660d 100644
@@ -1760,7 +1760,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;
@@ -1779,6 +1779,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
index f0a15ce1bd1b..fc0e4b630b51 100644
@@ -958,7 +958,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;
@@ -973,9 +973,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 +21 -21

141 lines not on this page · raw message

mm/page_alloc.c +30 -30

183 lines not on this page · raw message

Signature · 1 line
2.42.0.758.gaed0368e0e-goog
author maintainer
#32 lore raw

PATCH v2 20/39 mm: create new codetag references during page splitting

In reply to Suren Baghdasaryan

When 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
index a060c26eb449..0174aff5e871 100644
@@ -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
index 064fbd90822b..392b6907d875 100644
@@ -37,6 +37,7 @@
 #include <linux/page_owner.h>
 #include <linux/sched/sysctl.h>
 #include <linux/memory-tiers.h>
+#include <linux/pgalloc_tag.h>
 
 #include <asm/tlb.h>
 #include <asm/pgalloc.h>
@@ -2545,6 +2546,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
index 63dc2f8c7901..c4f0cd127e14 100644
@@ -2540,6 +2540,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);
@@ -4669,6 +4670,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.42.0.758.gaed0368e0e-goog
author maintainer
#33 lore raw

PATCH v2 21/39 mm/page_ext: enable early_page_ext when CONFIG_MEM_ALLOC_PROFILING_DEBUG=y

In reply to Suren Baghdasaryan

For 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
index 3c58fe8a24df..e7d8f1a5589e 100644
@@ -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.42.0.758.gaed0368e0e-goog
author maintainer
#34 lore raw

PATCH v2 22/39 lib: add codetag reference into slabobj_ext

In reply to Suren Baghdasaryan

To 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
index f3ede28b6fa6..853a24b5f713 100644
@@ -1613,7 +1613,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
index e1eda1450d68..482a6aae7664 100644
@@ -973,6 +973,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
index 60417fd262ea..293210ed10a9 100644
@@ -457,6 +457,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.42.0.758.gaed0368e0e-goog
author maintainer
#35 lore raw

PATCH v2 23/39 mm/slab: add allocation accounting into slab allocation and free paths

In reply to Suren Baghdasaryan

Account slab allocations using codetag reference embedded into slabobj_ext.

---
 include/linux/slab_def.h |  2 +-
 include/linux/slub_def.h |  4 ++--
 mm/slab.c                |  4 +++-
 mm/slab.h                | 32 ++++++++++++++++++++++++++++++++
 4 files changed, 38 insertions(+), 4 deletions(-)
include/linux/slab_def.h +1 -1
index a61e7d55d0d3..23f14dcb8d5b 100644
@@ -107,7 +107,7 @@ static inline void *nearest_obj(struct kmem_cache *cache, const struct slab *sla
  *   reciprocal_divide(offset, cache->reciprocal_buffer_size)
  */
 static inline unsigned int obj_to_index(const struct kmem_cache *cache,
-					const struct slab *slab, void *obj)
+					const struct slab *slab, const void *obj)
 {
 	u32 offset = (obj - slab->s_mem);
 	return reciprocal_divide(offset, cache->reciprocal_buffer_size);
include/linux/slub_def.h +2 -2
index deb90cf4bffb..43fda4a5f23a 100644
@@ -182,14 +182,14 @@ static inline void *nearest_obj(struct kmem_cache *cache, const struct slab *sla
 
 /* Determine object index from a given position */
 static inline unsigned int __obj_to_index(const struct kmem_cache *cache,
-					  void *addr, void *obj)
+					  void *addr, const void *obj)
 {
 	return reciprocal_divide(kasan_reset_tag(obj) - addr,
 				 cache->reciprocal_size);
 }
 
 static inline unsigned int obj_to_index(const struct kmem_cache *cache,
-					const struct slab *slab, void *obj)
+					const struct slab *slab, const void *obj)
 {
 	if (is_kfence_address(obj))
 		return 0;
mm/slab.c +3 -1
index cefcb7499b6c..18923f5f05b5 100644
@@ -3348,9 +3348,11 @@ static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
 static __always_inline void __cache_free(struct kmem_cache *cachep, void *objp,
 					 unsigned long caller)
 {
+	struct slab *slab = virt_to_slab(objp);
 	bool init;
 
-	memcg_slab_free_hook(cachep, virt_to_slab(objp), &objp, 1);
+	memcg_slab_free_hook(cachep, slab, &objp, 1);
+	alloc_tagging_slab_free_hook(cachep, slab, &objp, 1);
 
 	if (is_kfence_address(objp)) {
 		kmemleak_free_recursive(objp, cachep->flags);
mm/slab.h +32 -0
index 293210ed10a9..4859ce1f8808 100644
@@ -533,6 +533,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);
@@ -827,6 +853,12 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s,
 					 s->flags, flags);
 		kmsan_slab_alloc(s, p[i], 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.42.0.758.gaed0368e0e-goog
author maintainer
#36 lore raw

PATCH v2 24/39 mm/slab: enable slab allocation tagging for kmalloc and friends

In reply to Suren Baghdasaryan

Redefine kmalloc, krealloc, kzalloc, kcalloc, etc. to record allocations and deallocations done by these functions.

---
 include/linux/fortify-string.h |   5 +-
 include/linux/slab.h           | 173 ++++++++++++++++-----------------
 include/linux/string.h         |   4 +-
 mm/slab.c                      |  18 ++--
 mm/slab_common.c               |  38 ++++----
 mm/slub.c                      |  19 ++--
 mm/util.c                      |  20 ++--
 7 files changed, 137 insertions(+), 140 deletions(-)
include/linux/fortify-string.h +3 -2
index da51a83b2829..11319e7634a4 100644
@@ -752,9 +752,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);
 
@@ -764,6 +764,7 @@ __FORTIFY_INLINE void *kmemdup(const void * const POS0 p, size_t size, gfp_t gfp
 		fortify_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 +83 -90

308 lines not on this page · raw message

include/linux/string.h +3 -1
index dbfc66400050..9516258d8117 100644
@@ -176,7 +176,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.c +9 -9
index 18923f5f05b5..f75519fa89b9 100644
@@ -3429,18 +3429,18 @@ void *__kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru,
 	return ret;
 }
 
-void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
+void *kmem_cache_alloc_noprof(struct kmem_cache *cachep, gfp_t flags)
 {
 	return __kmem_cache_alloc_lru(cachep, NULL, flags);
 }
-EXPORT_SYMBOL(kmem_cache_alloc);
+EXPORT_SYMBOL(kmem_cache_alloc_noprof);
 
-void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru,
+void *kmem_cache_alloc_lru_noprof(struct kmem_cache *cachep, struct list_lru *lru,
 			   gfp_t flags)
 {
 	return __kmem_cache_alloc_lru(cachep, lru, flags);
 }
-EXPORT_SYMBOL(kmem_cache_alloc_lru);
+EXPORT_SYMBOL(kmem_cache_alloc_lru_noprof);
 
 static __always_inline void
 cache_alloc_debugcheck_after_bulk(struct kmem_cache *s, gfp_t flags,
@@ -3452,8 +3452,8 @@ cache_alloc_debugcheck_after_bulk(struct kmem_cache *s, gfp_t flags,
 		p[i] = cache_alloc_debugcheck_after(s, flags, p[i], caller);
 }
 
-int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
-			  void **p)
+int kmem_cache_alloc_bulk_noprof(struct kmem_cache *s, gfp_t flags, size_t size,
+				 void **p)
 {
 	struct obj_cgroup *objcg = NULL;
 	unsigned long irqflags;
@@ -3491,7 +3491,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
 	kmem_cache_free_bulk(s, i, p);
 	return 0;
 }
-EXPORT_SYMBOL(kmem_cache_alloc_bulk);
+EXPORT_SYMBOL(kmem_cache_alloc_bulk_noprof);
 
 /**
  * kmem_cache_alloc_node - Allocate an object on the specified node
@@ -3506,7 +3506,7 @@ EXPORT_SYMBOL(kmem_cache_alloc_bulk);
  *
  * Return: pointer to the new object or %NULL in case of error
  */
-void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
+void *kmem_cache_alloc_node_noprof(struct kmem_cache *cachep, gfp_t flags, int nodeid)
 {
 	void *ret = slab_alloc_node(cachep, NULL, flags, nodeid, cachep->object_size, _RET_IP_);
 
@@ -3514,7 +3514,7 @@ void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
 
 	return ret;
 }
-EXPORT_SYMBOL(kmem_cache_alloc_node);
+EXPORT_SYMBOL(kmem_cache_alloc_node_noprof);
 
 void *__kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
 			     int nodeid, size_t orig_size,
mm/slab_common.c +19 -19
index 446f406d2703..8ef5e47ff6a7 100644
@@ -1077,24 +1077,24 @@ void *__do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller
 	return ret;
 }
 
-void *__kmalloc_node(size_t size, gfp_t flags, int node)
+void *__kmalloc_node_noprof(size_t size, gfp_t flags, int node)
 {
 	return __do_kmalloc_node(size, flags, node, _RET_IP_);
 }
-EXPORT_SYMBOL(__kmalloc_node);
+EXPORT_SYMBOL(__kmalloc_node_noprof);
 
-void *__kmalloc(size_t size, gfp_t flags)
+void *__kmalloc_noprof(size_t size, gfp_t flags)
 {
 	return __do_kmalloc_node(size, flags, NUMA_NO_NODE, _RET_IP_);
 }
-EXPORT_SYMBOL(__kmalloc);
+EXPORT_SYMBOL(__kmalloc_noprof);
 
-void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
-				  int node, unsigned long caller)
+void *kmalloc_node_track_caller_noprof(size_t size, gfp_t flags,
+				       int node, unsigned long caller)
 {
 	return __do_kmalloc_node(size, flags, node, caller);
 }
-EXPORT_SYMBOL(__kmalloc_node_track_caller);
+EXPORT_SYMBOL(kmalloc_node_track_caller_noprof);
 
 /**
  * kfree - free previously allocated memory
@@ -1161,7 +1161,7 @@ size_t __ksize(const void *object)
 	return slab_ksize(folio_slab(folio)->slab_cache);
 }
 
-void *kmalloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size)
+void *kmalloc_trace_noprof(struct kmem_cache *s, gfp_t gfpflags, size_t size)
 {
 	void *ret = __kmem_cache_alloc_node(s, gfpflags, NUMA_NO_NODE,
 					    size, _RET_IP_);
@@ -1171,9 +1171,9 @@ void *kmalloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size)
 	ret = kasan_kmalloc(s, ret, size, gfpflags);
 	return ret;
 }
-EXPORT_SYMBOL(kmalloc_trace);
+EXPORT_SYMBOL(kmalloc_trace_noprof);
 
-void *kmalloc_node_trace(struct kmem_cache *s, gfp_t gfpflags,
+void *kmalloc_node_trace_noprof(struct kmem_cache *s, gfp_t gfpflags,
 			 int node, size_t size)
 {
 	void *ret = __kmem_cache_alloc_node(s, gfpflags, node, size, _RET_IP_);
@@ -1183,7 +1183,7 @@ void *kmalloc_node_trace(struct kmem_cache *s, gfp_t gfpflags,
 	ret = kasan_kmalloc(s, ret, size, gfpflags);
 	return ret;
 }
-EXPORT_SYMBOL(kmalloc_node_trace);
+EXPORT_SYMBOL(kmalloc_node_trace_noprof);
 
 gfp_t kmalloc_fix_flags(gfp_t flags)
 {
@@ -1213,7 +1213,7 @@ static void *__kmalloc_large_node(size_t size, gfp_t flags, int node)
 		flags = kmalloc_fix_flags(flags);
 
 	flags |= __GFP_COMP;
-	page = alloc_pages_node(node, flags, order);
+	page = alloc_pages_node_noprof(node, flags, order);
 	if (page) {
 		ptr = page_address(page);
 		mod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B,
@@ -1228,7 +1228,7 @@ static void *__kmalloc_large_node(size_t size, gfp_t flags, int node)
 	return ptr;
 }
 
-void *kmalloc_large(size_t size, gfp_t flags)
+void *kmalloc_large_noprof(size_t size, gfp_t flags)
 {
 	void *ret = __kmalloc_large_node(size, flags, NUMA_NO_NODE);
 
@@ -1236,9 +1236,9 @@ void *kmalloc_large(size_t size, gfp_t flags)
 		      flags, NUMA_NO_NODE);
 	return ret;
 }
-EXPORT_SYMBOL(kmalloc_large);
+EXPORT_SYMBOL(kmalloc_large_noprof);
 
-void *kmalloc_large_node(size_t size, gfp_t flags, int node)
+void *kmalloc_large_node_noprof(size_t size, gfp_t flags, int node)
 {
 	void *ret = __kmalloc_large_node(size, flags, node);
 
@@ -1246,7 +1246,7 @@ void *kmalloc_large_node(size_t size, gfp_t flags, int node)
 		      flags, node);
 	return ret;
 }
-EXPORT_SYMBOL(kmalloc_large_node);
+EXPORT_SYMBOL(kmalloc_large_node_noprof);
 
 #ifdef CONFIG_SLAB_FREELIST_RANDOM
 /* Randomize a generic freelist */
@@ -1460,7 +1460,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();
@@ -1484,7 +1484,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;
 
@@ -1499,7 +1499,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 +10 -9
index d16643492320..f5e07d8802e2 100644
@@ -3497,18 +3497,18 @@ void *__kmem_cache_alloc_lru(struct kmem_cache *s, struct list_lru *lru,
 	return ret;
 }
 
-void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
+void *kmem_cache_alloc_noprof(struct kmem_cache *s, gfp_t gfpflags)
 {
 	return __kmem_cache_alloc_lru(s, NULL, gfpflags);
 }
-EXPORT_SYMBOL(kmem_cache_alloc);
+EXPORT_SYMBOL(kmem_cache_alloc_noprof);
 
-void *kmem_cache_alloc_lru(struct kmem_cache *s, struct list_lru *lru,
+void *kmem_cache_alloc_lru_noprof(struct kmem_cache *s, struct list_lru *lru,
 			   gfp_t gfpflags)
 {
 	return __kmem_cache_alloc_lru(s, lru, gfpflags);
 }
-EXPORT_SYMBOL(kmem_cache_alloc_lru);
+EXPORT_SYMBOL(kmem_cache_alloc_lru_noprof);
 
 void *__kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags,
 			      int node, size_t orig_size,
@@ -3518,7 +3518,7 @@ void *__kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags,
 			       caller, orig_size);
 }
 
-void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
+void *kmem_cache_alloc_node_noprof(struct kmem_cache *s, gfp_t gfpflags, int node)
 {
 	void *ret = slab_alloc_node(s, NULL, gfpflags, node, _RET_IP_, s->object_size);
 
@@ -3526,7 +3526,7 @@ void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
 
 	return ret;
 }
-EXPORT_SYMBOL(kmem_cache_alloc_node);
+EXPORT_SYMBOL(kmem_cache_alloc_node_noprof);
 
 static noinline void free_to_partial_list(
 	struct kmem_cache *s, struct slab *slab,
@@ -3802,6 +3802,7 @@ static __fastpath_inline void slab_free(struct kmem_cache *s, struct slab *slab,
 				      unsigned long addr)
 {
 	memcg_slab_free_hook(s, slab, p, cnt);
+	alloc_tagging_slab_free_hook(s, slab, p, cnt);
 	/*
 	 * With KASAN enabled slab_free_freelist_hook modifies the freelist
 	 * to remove objects, whose reuse must be delayed.
@@ -4032,8 +4033,8 @@ static int __kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags,
 #endif /* CONFIG_SLUB_TINY */
 
 /* Note that interrupts must be enabled when calling this function. */
-int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
-			  void **p)
+int kmem_cache_alloc_bulk_noprof(struct kmem_cache *s, gfp_t flags, size_t size,
+		                 void **p)
 {
 	int i;
 	struct obj_cgroup *objcg = NULL;
@@ -4057,7 +4058,7 @@ int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
 			slab_want_init_on_alloc(flags, s), s->object_size);
 	return i;
 }
-EXPORT_SYMBOL(kmem_cache_alloc_bulk);
+EXPORT_SYMBOL(kmem_cache_alloc_bulk_noprof);
 
 
 /*
mm/util.c +10 -10
index 8cbbfd3a3d59..27ed6a5ac31a 100644
@@ -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
@@ -567,7 +567,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.
@@ -582,7 +582,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;
@@ -604,7 +604,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
@@ -633,7 +633,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.
@@ -672,7 +672,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;
 
@@ -685,7 +685,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.42.0.758.gaed0368e0e-goog
#37 lore raw

Re: [PATCH v2 24/39] mm/slab: enable slab allocation tagging for kmalloc and friends

kernel test robot noticed the following build warnings:

Hi Suren,

kernel test robot noticed the following build warnings:

[auto build test WARNING on vbabka-slab/for-next]
[also build test WARNING on kees/for-next/hardening linus/master v6.6-rc7] [cannot apply to akpm-mm/mm-everything next-20231024]
[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/20231024-215116
base:   git://git.kernel.org/pub/scm/linux/kernel/git/vbabka/slab.git for-next
patch link:    https://lore.kernel.org/r/20231024134637.3120277-25-surenb%40google.com
patch subject: [PATCH v2 24/39] mm/slab: enable slab allocation tagging for kmalloc and friends
config: m68k-allyesconfig (https://download.01.org/0day-ci/archive/20231025/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231025/[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 warnings (new ones prefixed by >>):

>> mm/slab_common.c:1488: warning: expecting prototype for krealloc(). Prototype was for krealloc_noprof() instead

vim +1488 mm/slab_common.c

928cec9cd6db53 Andrey Ryabinin     2014-08-06  1473  
928cec9cd6db53 Andrey Ryabinin     2014-08-06  1474  /**
928cec9cd6db53 Andrey Ryabinin     2014-08-06  1475   * krealloc - reallocate memory. The contents will remain unchanged.
928cec9cd6db53 Andrey Ryabinin     2014-08-06  1476   * @p: object to reallocate memory for.
928cec9cd6db53 Andrey Ryabinin     2014-08-06  1477   * @new_size: how many bytes of memory are required.
928cec9cd6db53 Andrey Ryabinin     2014-08-06  1478   * @flags: the type of memory to allocate.
928cec9cd6db53 Andrey Ryabinin     2014-08-06  1479   *
928cec9cd6db53 Andrey Ryabinin     2014-08-06  1480   * The contents of the object pointed to are preserved up to the
15d5de496bebfd Bartosz Golaszewski 2020-12-14  1481   * lesser of the new and old sizes (__GFP_ZERO flag is effectively ignored).
15d5de496bebfd Bartosz Golaszewski 2020-12-14  1482   * If @p is %NULL, krealloc() behaves exactly like kmalloc().  If @new_size
15d5de496bebfd Bartosz Golaszewski 2020-12-14  1483   * is 0 and @p is not a %NULL pointer, the object pointed to is freed.
a862f68a8b3600 Mike Rapoport       2019-03-05  1484   *
a862f68a8b3600 Mike Rapoport       2019-03-05  1485   * Return: pointer to the allocated memory or %NULL in case of error
928cec9cd6db53 Andrey Ryabinin     2014-08-06  1486   */
8f61679532ae15 Suren Baghdasaryan  2023-10-24  1487  void *krealloc_noprof(const void *p, size_t new_size, gfp_t flags)
928cec9cd6db53 Andrey Ryabinin     2014-08-06 @1488  {
928cec9cd6db53 Andrey Ryabinin     2014-08-06  1489  	void *ret;
928cec9cd6db53 Andrey Ryabinin     2014-08-06  1490  
928cec9cd6db53 Andrey Ryabinin     2014-08-06  1491  	if (unlikely(!new_size)) {
928cec9cd6db53 Andrey Ryabinin     2014-08-06  1492  		kfree(p);
928cec9cd6db53 Andrey Ryabinin     2014-08-06  1493  		return ZERO_SIZE_PTR;
928cec9cd6db53 Andrey Ryabinin     2014-08-06  1494  	}
928cec9cd6db53 Andrey Ryabinin     2014-08-06  1495  
928cec9cd6db53 Andrey Ryabinin     2014-08-06  1496  	ret = __do_krealloc(p, new_size, flags);
772a2fa50ffb2f Andrey Konovalov    2018-12-28  1497  	if (ret && kasan_reset_tag(p) != kasan_reset_tag(ret))
928cec9cd6db53 Andrey Ryabinin     2014-08-06  1498  		kfree(p);
928cec9cd6db53 Andrey Ryabinin     2014-08-06  1499  
928cec9cd6db53 Andrey Ryabinin     2014-08-06  1500  	return ret;
928cec9cd6db53 Andrey Ryabinin     2014-08-06  1501  }
8f61679532ae15 Suren Baghdasaryan  2023-10-24  1502  EXPORT_SYMBOL(krealloc_noprof);
928cec9cd6db53 Andrey Ryabinin     2014-08-06  1503  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author maintainer
#38 lore raw

PATCH v2 25/39 mm/slub: Mark slab_free_freelist_hook() __always_inline

In reply to Suren Baghdasaryan

From: 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
index f5e07d8802e2..222c16cef729 100644
@@ -1800,7 +1800,7 @@ static __always_inline bool slab_free_hook(struct kmem_cache *s,
 	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.42.0.758.gaed0368e0e-goog
author maintainer
#39 lore raw

PATCH v2 26/39 mempool: Hook up to memory allocation profiling

In reply to Suren Baghdasaryan

From: 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            | 34 ++++++++-----------
 2 files changed, 48 insertions(+), 59 deletions(-)
include/linux/mempool.h +35 -38
index 4aae6c06c5f2..9fa126aa19b5 100644
@@ -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_free(void *element, mempool_t *pool);
 
 /*
@@ -61,19 +77,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
@@ -82,17 +89,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
@@ -101,16 +103,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 +13 -21
index 734bcf5afbb7..4fd949178449 100644
@@ -230,17 +230,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.
@@ -255,17 +255,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;
 
@@ -281,7 +273,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
@@ -377,7 +369,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;
@@ -444,7 +436,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_free - return an element to the pool.
@@ -515,7 +507,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);
 
@@ -533,7 +525,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);
 
@@ -550,7 +542,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.42.0.758.gaed0368e0e-goog
#40 lore raw

Re: [PATCH v2 26/39] mempool: Hook up to memory allocation profiling

kernel test robot noticed the following build warnings:

Hi Suren,

kernel test robot noticed the following build warnings:

[auto build test WARNING on vbabka-slab/for-next]
[also build test WARNING on kees/for-next/hardening linus/master v6.6-rc7] [cannot apply to akpm-mm/mm-everything next-20231024]
[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/20231024-215116
base:   git://git.kernel.org/pub/scm/linux/kernel/git/vbabka/slab.git for-next
patch link:    https://lore.kernel.org/r/20231024134637.3120277-27-surenb%40google.com
patch subject: [PATCH v2 26/39] mempool: Hook up to memory allocation profiling
config: m68k-allyesconfig (https://download.01.org/0day-ci/archive/20231025/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231025/[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 warnings (new ones prefixed by >>):

>> mm/mempool.c:235: warning: expecting prototype for mempool_init(). Prototype was for mempool_init_noprof() instead
>> mm/mempool.c:261: warning: Function parameter or member 'gfp_mask' not described in 'mempool_create_node_noprof'
>> mm/mempool.c:261: warning: Function parameter or member 'node_id' not described in 'mempool_create_node_noprof'
>> mm/mempool.c:261: warning: expecting prototype for mempool_create_node(). Prototype was for mempool_create_node_noprof() instead
>> mm/mempool.c:373: warning: expecting prototype for mempool_alloc(). Prototype was for mempool_alloc_noprof() instead

vim +235 mm/mempool.c

c1a67fefd0546a Kent Overstreet   2015-05-04  218  
c1a67fefd0546a Kent Overstreet   2015-05-04  219  /**
c1a67fefd0546a Kent Overstreet   2015-05-04  220   * mempool_init - initialize a memory pool
a3bf6ce3664960 Mike Rapoport     2018-08-21  221   * @pool:      pointer to the memory pool that should be initialized
c1a67fefd0546a Kent Overstreet   2015-05-04  222   * @min_nr:    the minimum number of elements guaranteed to be
c1a67fefd0546a Kent Overstreet   2015-05-04  223   *             allocated for this pool.
c1a67fefd0546a Kent Overstreet   2015-05-04  224   * @alloc_fn:  user-defined element-allocation function.
c1a67fefd0546a Kent Overstreet   2015-05-04  225   * @free_fn:   user-defined element-freeing function.
c1a67fefd0546a Kent Overstreet   2015-05-04  226   * @pool_data: optional private data available to the user-defined functions.
c1a67fefd0546a Kent Overstreet   2015-05-04  227   *
c1a67fefd0546a Kent Overstreet   2015-05-04  228   * Like mempool_create(), but initializes the pool in (i.e. embedded in another
c1a67fefd0546a Kent Overstreet   2015-05-04  229   * structure).
a862f68a8b3600 Mike Rapoport     2019-03-05  230   *
a862f68a8b3600 Mike Rapoport     2019-03-05  231   * Return: %0 on success, negative error code otherwise.
c1a67fefd0546a Kent Overstreet   2015-05-04  232   */
0bd30b9796272a Kent Overstreet   2023-10-24  233  int mempool_init_noprof(mempool_t *pool, int min_nr, mempool_alloc_t *alloc_fn,
c1a67fefd0546a Kent Overstreet   2015-05-04  234  			mempool_free_t *free_fn, void *pool_data)
c1a67fefd0546a Kent Overstreet   2015-05-04 @235  {
c1a67fefd0546a Kent Overstreet   2015-05-04  236  	return mempool_init_node(pool, min_nr, alloc_fn, free_fn,
c1a67fefd0546a Kent Overstreet   2015-05-04  237  				 pool_data, GFP_KERNEL, NUMA_NO_NODE);
c1a67fefd0546a Kent Overstreet   2015-05-04  238  
c1a67fefd0546a Kent Overstreet   2015-05-04  239  }
0bd30b9796272a Kent Overstreet   2023-10-24  240  EXPORT_SYMBOL(mempool_init_noprof);
c1a67fefd0546a Kent Overstreet   2015-05-04  241  
^1da177e4c3f41 Linus Torvalds    2005-04-16  242  /**
0bd30b9796272a Kent Overstreet   2023-10-24  243   * mempool_create_node - create a memory pool
^1da177e4c3f41 Linus Torvalds    2005-04-16  244   * @min_nr:    the minimum number of elements guaranteed to be
^1da177e4c3f41 Linus Torvalds    2005-04-16  245   *             allocated for this pool.
^1da177e4c3f41 Linus Torvalds    2005-04-16  246   * @alloc_fn:  user-defined element-allocation function.
^1da177e4c3f41 Linus Torvalds    2005-04-16  247   * @free_fn:   user-defined element-freeing function.
^1da177e4c3f41 Linus Torvalds    2005-04-16  248   * @pool_data: optional private data available to the user-defined functions.
^1da177e4c3f41 Linus Torvalds    2005-04-16  249   *
^1da177e4c3f41 Linus Torvalds    2005-04-16  250   * this function creates and allocates a guaranteed size, preallocated
72fd4a35a82433 Robert P. J. Day  2007-02-10  251   * memory pool. The pool can be used from the mempool_alloc() and mempool_free()
^1da177e4c3f41 Linus Torvalds    2005-04-16  252   * functions. This function might sleep. Both the alloc_fn() and the free_fn()
72fd4a35a82433 Robert P. J. Day  2007-02-10  253   * functions might sleep - as long as the mempool_alloc() function is not called
^1da177e4c3f41 Linus Torvalds    2005-04-16  254   * from IRQ contexts.
a862f68a8b3600 Mike Rapoport     2019-03-05  255   *
a862f68a8b3600 Mike Rapoport     2019-03-05  256   * Return: pointer to the created memory pool object or %NULL on error.
^1da177e4c3f41 Linus Torvalds    2005-04-16  257   */
0bd30b9796272a Kent Overstreet   2023-10-24  258  mempool_t *mempool_create_node_noprof(int min_nr, mempool_alloc_t *alloc_fn,
a91a5ac6858fbf Tejun Heo         2012-06-04  259  				      mempool_free_t *free_fn, void *pool_data,
a91a5ac6858fbf Tejun Heo         2012-06-04  260  				      gfp_t gfp_mask, int node_id)
1946089a109251 Christoph Lameter 2005-06-23 @261  {
1946089a109251 Christoph Lameter 2005-06-23  262  	mempool_t *pool;
c1a67fefd0546a Kent Overstreet   2015-05-04  263  
7b5219db00d0af Joe Perches       2013-09-11  264  	pool = kzalloc_node(sizeof(*pool), gfp_mask, node_id);
^1da177e4c3f41 Linus Torvalds    2005-04-16  265  	if (!pool)
^1da177e4c3f41 Linus Torvalds    2005-04-16  266  		return NULL;
c1a67fefd0546a Kent Overstreet   2015-05-04  267  
c1a67fefd0546a Kent Overstreet   2015-05-04  268  	if (mempool_init_node(pool, min_nr, alloc_fn, free_fn, pool_data,
c1a67fefd0546a Kent Overstreet   2015-05-04  269  			      gfp_mask, node_id)) {
^1da177e4c3f41 Linus Torvalds    2005-04-16  270  		kfree(pool);
^1da177e4c3f41 Linus Torvalds    2005-04-16  271  		return NULL;
^1da177e4c3f41 Linus Torvalds    2005-04-16  272  	}
^1da177e4c3f41 Linus Torvalds    2005-04-16  273  
^1da177e4c3f41 Linus Torvalds    2005-04-16  274  	return pool;
^1da177e4c3f41 Linus Torvalds    2005-04-16  275  }
0bd30b9796272a Kent Overstreet   2023-10-24  276  EXPORT_SYMBOL(mempool_create_node_noprof);
^1da177e4c3f41 Linus Torvalds    2005-04-16  277  
^1da177e4c3f41 Linus Torvalds    2005-04-16  278  /**
^1da177e4c3f41 Linus Torvalds    2005-04-16  279   * mempool_resize - resize an existing memory pool
^1da177e4c3f41 Linus Torvalds    2005-04-16  280   * @pool:       pointer to the memory pool which was allocated via
^1da177e4c3f41 Linus Torvalds    2005-04-16  281   *              mempool_create().
^1da177e4c3f41 Linus Torvalds    2005-04-16  282   * @new_min_nr: the new minimum number of elements guaranteed to be
^1da177e4c3f41 Linus Torvalds    2005-04-16  283   *              allocated for this pool.
^1da177e4c3f41 Linus Torvalds    2005-04-16  284   *
^1da177e4c3f41 Linus Torvalds    2005-04-16  285   * This function shrinks/grows the pool. In the case of growing,
^1da177e4c3f41 Linus Torvalds    2005-04-16  286   * it cannot be guaranteed that the pool will be grown to the new
^1da177e4c3f41 Linus Torvalds    2005-04-16  287   * size immediately, but new mempool_free() calls will refill it.
11d83360452ea2 David Rientjes    2015-04-14  288   * This function may sleep.
^1da177e4c3f41 Linus Torvalds    2005-04-16  289   *
^1da177e4c3f41 Linus Torvalds    2005-04-16  290   * Note, the caller must guarantee that no mempool_destroy is called
^1da177e4c3f41 Linus Torvalds    2005-04-16  291   * while this function is running. mempool_alloc() & mempool_free()
^1da177e4c3f41 Linus Torvalds    2005-04-16  292   * might be called (eg. from IRQ contexts) while this function executes.
a862f68a8b3600 Mike Rapoport     2019-03-05  293   *
a862f68a8b3600 Mike Rapoport     2019-03-05  294   * Return: %0 on success, negative error code otherwise.
^1da177e4c3f41 Linus Torvalds    2005-04-16  295   */
11d83360452ea2 David Rientjes    2015-04-14  296  int mempool_resize(mempool_t *pool, int new_min_nr)
^1da177e4c3f41 Linus Torvalds    2005-04-16  297  {
^1da177e4c3f41 Linus Torvalds    2005-04-16  298  	void *element;
^1da177e4c3f41 Linus Torvalds    2005-04-16  299  	void **new_elements;
^1da177e4c3f41 Linus Torvalds    2005-04-16  300  	unsigned long flags;
^1da177e4c3f41 Linus Torvalds    2005-04-16  301  
^1da177e4c3f41 Linus Torvalds    2005-04-16  302  	BUG_ON(new_min_nr <= 0);
11d83360452ea2 David Rientjes    2015-04-14  303  	might_sleep();
^1da177e4c3f41 Linus Torvalds    2005-04-16  304  
^1da177e4c3f41 Linus Torvalds    2005-04-16  305  	spin_lock_irqsave(&pool->lock, flags);
^1da177e4c3f41 Linus Torvalds    2005-04-16  306  	if (new_min_nr <= pool->min_nr) {
^1da177e4c3f41 Linus Torvalds    2005-04-16  307  		while (new_min_nr < pool->curr_nr) {
8cded8668e1f49 Jia-Ju Bai        2018-08-17  308  			element = remove_element(pool);
^1da177e4c3f41 Linus Torvalds    2005-04-16  309  			spin_unlock_irqrestore(&pool->lock, flags);
^1da177e4c3f41 Linus Torvalds    2005-04-16  310  			pool->free(element, pool->pool_data);
^1da177e4c3f41 Linus Torvalds    2005-04-16  311  			spin_lock_irqsave(&pool->lock, flags);
^1da177e4c3f41 Linus Torvalds    2005-04-16  312  		}
^1da177e4c3f41 Linus Torvalds    2005-04-16  313  		pool->min_nr = new_min_nr;
^1da177e4c3f41 Linus Torvalds    2005-04-16  314  		goto out_unlock;
^1da177e4c3f41 Linus Torvalds    2005-04-16  315  	}
^1da177e4c3f41 Linus Torvalds    2005-04-16  316  	spin_unlock_irqrestore(&pool->lock, flags);
^1da177e4c3f41 Linus Torvalds    2005-04-16  317  
^1da177e4c3f41 Linus Torvalds    2005-04-16  318  	/* Grow the pool */
11d83360452ea2 David Rientjes    2015-04-14  319  	new_elements = kmalloc_array(new_min_nr, sizeof(*new_elements),
11d83360452ea2 David Rientjes    2015-04-14  320  				     GFP_KERNEL);
^1da177e4c3f41 Linus Torvalds    2005-04-16  321  	if (!new_elements)
^1da177e4c3f41 Linus Torvalds    2005-04-16  322  		return -ENOMEM;
^1da177e4c3f41 Linus Torvalds    2005-04-16  323  
^1da177e4c3f41 Linus Torvalds    2005-04-16  324  	spin_lock_irqsave(&pool->lock, flags);
^1da177e4c3f41 Linus Torvalds    2005-04-16  325  	if (unlikely(new_min_nr <= pool->min_nr)) {
^1da177e4c3f41 Linus Torvalds    2005-04-16  326  		/* Raced, other resize will do our work */
^1da177e4c3f41 Linus Torvalds    2005-04-16  327  		spin_unlock_irqrestore(&pool->lock, flags);
^1da177e4c3f41 Linus Torvalds    2005-04-16  328  		kfree(new_elements);
^1da177e4c3f41 Linus Torvalds    2005-04-16  329  		goto out;
^1da177e4c3f41 Linus Torvalds    2005-04-16  330  	}
^1da177e4c3f41 Linus Torvalds    2005-04-16  331  	memcpy(new_elements, pool->elements,
^1da177e4c3f41 Linus Torvalds    2005-04-16  332  			pool->curr_nr * sizeof(*new_elements));
^1da177e4c3f41 Linus Torvalds    2005-04-16  333  	kfree(pool->elements);
^1da177e4c3f41 Linus Torvalds    2005-04-16  334  	pool->elements = new_elements;
^1da177e4c3f41 Linus Torvalds    2005-04-16  335  	pool->min_nr = new_min_nr;
^1da177e4c3f41 Linus Torvalds    2005-04-16  336  
^1da177e4c3f41 Linus Torvalds    2005-04-16  337  	while (pool->curr_nr < pool->min_nr) {
^1da177e4c3f41 Linus Torvalds    2005-04-16  338  		spin_unlock_irqrestore(&pool->lock, flags);
11d83360452ea2 David Rientjes    2015-04-14  339  		element = pool->alloc(GFP_KERNEL, pool->pool_data);
^1da177e4c3f41 Linus Torvalds    2005-04-16  340  		if (!element)
^1da177e4c3f41 Linus Torvalds    2005-04-16  341  			goto out;
^1da177e4c3f41 Linus Torvalds    2005-04-16  342  		spin_lock_irqsave(&pool->lock, flags);
^1da177e4c3f41 Linus Torvalds    2005-04-16  343  		if (pool->curr_nr < pool->min_nr) {
^1da177e4c3f41 Linus Torvalds    2005-04-16  344  			add_element(pool, element);
^1da177e4c3f41 Linus Torvalds    2005-04-16  345  		} else {
^1da177e4c3f41 Linus Torvalds    2005-04-16  346  			spin_unlock_irqrestore(&pool->lock, flags);
^1da177e4c3f41 Linus Torvalds    2005-04-16  347  			pool->free(element, pool->pool_data);	/* Raced */
^1da177e4c3f41 Linus Torvalds    2005-04-16  348  			goto out;
^1da177e4c3f41 Linus Torvalds    2005-04-16  349  		}
^1da177e4c3f41 Linus Torvalds    2005-04-16  350  	}
^1da177e4c3f41 Linus Torvalds    2005-04-16  351  out_unlock:
^1da177e4c3f41 Linus Torvalds    2005-04-16  352  	spin_unlock_irqrestore(&pool->lock, flags);
^1da177e4c3f41 Linus Torvalds    2005-04-16  353  out:
^1da177e4c3f41 Linus Torvalds    2005-04-16  354  	return 0;
^1da177e4c3f41 Linus Torvalds    2005-04-16  355  }
^1da177e4c3f41 Linus Torvalds    2005-04-16  356  EXPORT_SYMBOL(mempool_resize);
^1da177e4c3f41 Linus Torvalds    2005-04-16  357  
^1da177e4c3f41 Linus Torvalds    2005-04-16  358  /**
^1da177e4c3f41 Linus Torvalds    2005-04-16  359   * mempool_alloc - allocate an element from a specific memory pool
^1da177e4c3f41 Linus Torvalds    2005-04-16  360   * @pool:      pointer to the memory pool which was allocated via
^1da177e4c3f41 Linus Torvalds    2005-04-16  361   *             mempool_create().
^1da177e4c3f41 Linus Torvalds    2005-04-16  362   * @gfp_mask:  the usual allocation bitmask.
^1da177e4c3f41 Linus Torvalds    2005-04-16  363   *
72fd4a35a82433 Robert P. J. Day  2007-02-10  364   * this function only sleeps if the alloc_fn() function sleeps or
^1da177e4c3f41 Linus Torvalds    2005-04-16  365   * returns NULL. Note that due to preallocation, this function
^1da177e4c3f41 Linus Torvalds    2005-04-16  366   * *never* fails when called from process contexts. (it might
^1da177e4c3f41 Linus Torvalds    2005-04-16  367   * fail if called from an IRQ context.)
4e390b2b2f34b8 Michal Hocko      2016-07-28  368   * Note: using __GFP_ZERO is not supported.
a862f68a8b3600 Mike Rapoport     2019-03-05  369   *
a862f68a8b3600 Mike Rapoport     2019-03-05  370   * Return: pointer to the allocated element or %NULL on error.
^1da177e4c3f41 Linus Torvalds    2005-04-16  371   */
0bd30b9796272a Kent Overstreet   2023-10-24  372  void *mempool_alloc_noprof(mempool_t *pool, gfp_t gfp_mask)
^1da177e4c3f41 Linus Torvalds    2005-04-16 @373  {
^1da177e4c3f41 Linus Torvalds    2005-04-16  374  	void *element;
^1da177e4c3f41 Linus Torvalds    2005-04-16  375  	unsigned long flags;
ac6424b981bce1 Ingo Molnar       2017-06-20  376  	wait_queue_entry_t wait;
6daa0e28627abf Al Viro           2005-10-21  377  	gfp_t gfp_temp;
20a77776c24800 Nicholas Piggin   2005-05-01  378  
8bf8fcb07653fb Sebastian Ott     2014-06-04  379  	VM_WARN_ON_ONCE(gfp_mask & __GFP_ZERO);
21bfe8db0a4223 Daniel Vetter     2022-06-05  380  	might_alloc(gfp_mask);
b84a35be028522 Nicholas Piggin   2005-05-01  381  
4e390b2b2f34b8 Michal Hocko      2016-07-28  382  	gfp_mask |= __GFP_NOMEMALLOC;	/* don't allocate emergency reserves */
b84a35be028522 Nicholas Piggin   2005-05-01  383  	gfp_mask |= __GFP_NORETRY;	/* don't loop in __alloc_pages */
b84a35be028522 Nicholas Piggin   2005-05-01  384  	gfp_mask |= __GFP_NOWARN;	/* failures are OK */
^1da177e4c3f41 Linus Torvalds    2005-04-16  385  
d0164adc89f6bb Mel Gorman        2015-11-06  386  	gfp_temp = gfp_mask & ~(__GFP_DIRECT_RECLAIM|__GFP_IO);
20a77776c24800 Nicholas Piggin   2005-05-01  387  
^1da177e4c3f41 Linus Torvalds    2005-04-16  388  repeat_alloc:
^1da177e4c3f41 Linus Torvalds    2005-04-16  389  
20a77776c24800 Nicholas Piggin   2005-05-01  390  	element = pool->alloc(gfp_temp, pool->pool_data);
^1da177e4c3f41 Linus Torvalds    2005-04-16  391  	if (likely(element != NULL))
^1da177e4c3f41 Linus Torvalds    2005-04-16  392  		return element;
^1da177e4c3f41 Linus Torvalds    2005-04-16  393  
^1da177e4c3f41 Linus Torvalds    2005-04-16  394  	spin_lock_irqsave(&pool->lock, flags);
^1da177e4c3f41 Linus Torvalds    2005-04-16  395  	if (likely(pool->curr_nr)) {
8cded8668e1f49 Jia-Ju Bai        2018-08-17  396  		element = remove_element(pool);
^1da177e4c3f41 Linus Torvalds    2005-04-16  397  		spin_unlock_irqrestore(&pool->lock, flags);
5b990546e33477 Tejun Heo         2012-01-10  398  		/* paired with rmb in mempool_free(), read comment there */
5b990546e33477 Tejun Heo         2012-01-10  399  		smp_wmb();
174119628188b0 Catalin Marinas   2014-06-06  400  		/*
174119628188b0 Catalin Marinas   2014-06-06  401  		 * Update the allocation stack trace as this is more useful
174119628188b0 Catalin Marinas   2014-06-06  402  		 * for debugging.
174119628188b0 Catalin Marinas   2014-06-06  403  		 */
174119628188b0 Catalin Marinas   2014-06-06  404  		kmemleak_update_trace(element);
^1da177e4c3f41 Linus Torvalds    2005-04-16  405  		return element;
^1da177e4c3f41 Linus Torvalds    2005-04-16  406  	}
^1da177e4c3f41 Linus Torvalds    2005-04-16  407  
1ebb7044c9142c Tejun Heo         2012-01-10  408  	/*
d0164adc89f6bb Mel Gorman        2015-11-06  409  	 * We use gfp mask w/o direct reclaim or IO for the first round.  If
1ebb7044c9142c Tejun Heo         2012-01-10  410  	 * alloc failed with that and @pool was empty, retry immediately.
1ebb7044c9142c Tejun Heo         2012-01-10  411  	 */
4e390b2b2f34b8 Michal Hocko      2016-07-28  412  	if (gfp_temp != gfp_mask) {
1ebb7044c9142c Tejun Heo         2012-01-10  413  		spin_unlock_irqrestore(&pool->lock, flags);
1ebb7044c9142c Tejun Heo         2012-01-10  414  		gfp_temp = gfp_mask;
1ebb7044c9142c Tejun Heo         2012-01-10  415  		goto repeat_alloc;
1ebb7044c9142c Tejun Heo         2012-01-10  416  	}
1ebb7044c9142c Tejun Heo         2012-01-10  417  
d0164adc89f6bb Mel Gorman        2015-11-06  418  	/* We must not sleep if !__GFP_DIRECT_RECLAIM */
d0164adc89f6bb Mel Gorman        2015-11-06  419  	if (!(gfp_mask & __GFP_DIRECT_RECLAIM)) {
5b990546e33477 Tejun Heo         2012-01-10  420  		spin_unlock_irqrestore(&pool->lock, flags);
^1da177e4c3f41 Linus Torvalds    2005-04-16  421  		return NULL;
5b990546e33477 Tejun Heo         2012-01-10  422  	}
^1da177e4c3f41 Linus Torvalds    2005-04-16  423  
5b990546e33477 Tejun Heo         2012-01-10  424  	/* Let's wait for someone else to return an element to @pool */
01890a4c120f68 Benjamin LaHaise  2005-06-23  425  	init_wait(&wait);
^1da177e4c3f41 Linus Torvalds    2005-04-16  426  	prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
5b990546e33477 Tejun Heo         2012-01-10  427  
5b990546e33477 Tejun Heo         2012-01-10  428  	spin_unlock_irqrestore(&pool->lock, flags);
5b990546e33477 Tejun Heo         2012-01-10  429  
0b1d647a02c5a1 Pavel Mironchik   2006-08-31  430  	/*
5b990546e33477 Tejun Heo         2012-01-10  431  	 * FIXME: this should be io_schedule().  The timeout is there as a
5b990546e33477 Tejun Heo         2012-01-10  432  	 * workaround for some DM problems in 2.6.18.
0b1d647a02c5a1 Pavel Mironchik   2006-08-31  433  	 */
0b1d647a02c5a1 Pavel Mironchik   2006-08-31  434  	io_schedule_timeout(5*HZ);
^1da177e4c3f41 Linus Torvalds    2005-04-16  435  
5b990546e33477 Tejun Heo         2012-01-10  436  	finish_wait(&pool->wait, &wait);
^1da177e4c3f41 Linus Torvalds    2005-04-16  437  	goto repeat_alloc;
^1da177e4c3f41 Linus Torvalds    2005-04-16  438  }
0bd30b9796272a Kent Overstreet   2023-10-24  439  EXPORT_SYMBOL(mempool_alloc_noprof);
^1da177e4c3f41 Linus Torvalds    2005-04-16  440  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author maintainer
#41 lore raw

PATCH v2 27/39 xfs: Memory allocation profiling fixups

In reply to Suren Baghdasaryan

From: 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
index c557a030acfe..9aa57a4e2478 100644
@@ -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
index b987dc2c6851..c4cf1dc2a7af 100644
@@ -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.42.0.758.gaed0368e0e-goog
author maintainer
#42 lore raw

PATCH v2 28/39 timekeeping: Fix a circular include dependency

In reply to Suren Baghdasaryan

From: Kent Overstreet <[email protected]>

This avoids a circular header dependency in an upcoming patch by only making hrtimer.h depend on percpu-defs.h

---
 include/linux/hrtimer.h        | 2 +-
 include/linux/time_namespace.h | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
include/linux/hrtimer.h +1 -1
index 0ee140176f10..e67349e84364 100644
@@ -16,7 +16,7 @@
 #include <linux/rbtree.h>
 #include <linux/init.h>
 #include <linux/list.h>
-#include <linux/percpu.h>
+#include <linux/percpu-defs.h>
 #include <linux/seqlock.h>
 #include <linux/timer.h>
 #include <linux/timerqueue.h>
include/linux/time_namespace.h +2 -0
index 03d9c5ac01d1..a9e61120d4e3 100644
@@ -11,6 +11,8 @@
 struct user_namespace;
 extern struct user_namespace init_user_ns;
 
+struct vm_area_struct;
+
 struct timens_offsets {
 	struct timespec64 monotonic;
 	struct timespec64 boottime;
Signature · 1 line
2.42.0.758.gaed0368e0e-goog
#43 lore raw

Re: [PATCH v2 28/39] timekeeping: Fix a circular include dependency

Quoted 4 lines · Suren Baghdasaryan

From: Kent Overstreet <[email protected]>

This avoids a circular header dependency in an upcoming patch by only making hrtimer.h depend on percpu-defs.h

What's the actual dependency problem?

author maintainer
#44 lore raw

Re: [PATCH v2 28/39] timekeeping: Fix a circular include dependency

Quoted 8 lines · Thomas Gleixner
Quoted 4 lines · Suren Baghdasaryan

From: Kent Overstreet <[email protected]>

This avoids a circular header dependency in an upcoming patch by only making hrtimer.h depend on percpu-defs.h

What's the actual dependency problem?

Sorry for the delay.
When we instrument per-cpu allocations in [1] we need to include sched.h in percpu.h to be able to use alloc_tag_save(). sched.h includes hrtimer.h. So, without this change we end up with a circular
inclusion: percpu.h->sched.h->hrtimer.h->percpu.h

[1] https://lore.kernel.org/all/[email protected]/

Quoted 1 line
#45 lore raw

Re: [PATCH v2 28/39] timekeeping: Fix a circular include dependency

Quoted 9 lines · Suren Baghdasaryan
Quoted 4 lines · Thomas Gleixner
Quoted 2 lines

This avoids a circular header dependency in an upcoming patch by only making hrtimer.h depend on percpu-defs.h

What's the actual dependency problem?

Sorry for the delay.
When we instrument per-cpu allocations in [1] we need to include sched.h in percpu.h to be able to use alloc_tag_save(). sched.h

Including sched.h in percpu.h is fundamentally wrong as sched.h is the initial place of all header recursions.

There is a reason why a lot of funtionalitiy has been split out of sched.h into seperate headers over time in order to avoid that.

Thanks,

tglx

maintainer
#46 lore raw

Re: [PATCH v2 28/39] timekeeping: Fix a circular include dependency

Quoted 11 lines · Thomas Gleixner
Quoted 9 lines · Suren Baghdasaryan
Quoted 4 lines · Thomas Gleixner
Quoted 2 lines

This avoids a circular header dependency in an upcoming patch by only making hrtimer.h depend on percpu-defs.h

What's the actual dependency problem?

Sorry for the delay.
When we instrument per-cpu allocations in [1] we need to include sched.h in percpu.h to be able to use alloc_tag_save(). sched.h

Quoted 5 lines

Including sched.h in percpu.h is fundamentally wrong as sched.h is the initial place of all header recursions.

There is a reason why a lot of funtionalitiy has been split out of sched.h into seperate headers over time in order to avoid that.

Yeah, it's definitely unfortunate. The issue here is that alloc_tag_save() needs task_struct - we have to pull that in for alloc_tag_save() to be inline, which we really want.

What if we moved task_struct to its own dedicated header? That might be good to do anyways...

maintainer
#47 lore raw

Re: [PATCH v2 28/39] timekeeping: Fix a circular include dependency

Quoted 18 lines · Kent Overstreet
Quoted 11 lines · Thomas Gleixner
Quoted 9 lines · Suren Baghdasaryan
Quoted 4 lines · Thomas Gleixner
Quoted 2 lines

This avoids a circular header dependency in an upcoming patch by only making hrtimer.h depend on percpu-defs.h

What's the actual dependency problem?

Sorry for the delay.
When we instrument per-cpu allocations in [1] we need to include sched.h in percpu.h to be able to use alloc_tag_save(). sched.h

Quoted 5 lines

Including sched.h in percpu.h is fundamentally wrong as sched.h is the initial place of all header recursions.

There is a reason why a lot of funtionalitiy has been split out of sched.h into seperate headers over time in order to avoid that.

Quoted 6 lines

Yeah, it's definitely unfortunate. The issue here is that alloc_tag_save() needs task_struct - we have to pull that in for alloc_tag_save() to be inline, which we really want.

What if we moved task_struct to its own dedicated header? That might be good to do anyways...

Yes, I agree that is the best way to handle it. I've prototyped a more thorough header cleanup with good results (much improved build speed) in the past, and most of the work to get there is to seperate out structures like task_struct, mm_struct, net_device, etc into headers that only depend on the embedded structure definitions without needing all the inline functions associated with them.

Arnd

#48 lore raw

Re: [PATCH v2 28/39] timekeeping: Fix a circular include dependency

Quoted 27 lines · Arnd Bergmann
Quoted 18 lines · Kent Overstreet
Quoted 11 lines · Thomas Gleixner
Quoted 9 lines · Suren Baghdasaryan
Quoted 4 lines · Thomas Gleixner
Quoted 2 lines

This avoids a circular header dependency in an upcoming patch by only making hrtimer.h depend on percpu-defs.h

What's the actual dependency problem?

Sorry for the delay.
When we instrument per-cpu allocations in [1] we need to include sched.h in percpu.h to be able to use alloc_tag_save(). sched.h

Quoted 5 lines

Including sched.h in percpu.h is fundamentally wrong as sched.h is the initial place of all header recursions.

There is a reason why a lot of funtionalitiy has been split out of sched.h into seperate headers over time in order to avoid that.

Quoted 6 lines

Yeah, it's definitely unfortunate. The issue here is that alloc_tag_save() needs task_struct - we have to pull that in for alloc_tag_save() to be inline, which we really want.

What if we moved task_struct to its own dedicated header? That might be good to do anyways...

Quoted 7 lines

Yes, I agree that is the best way to handle it. I've prototyped a more thorough header cleanup with good results (much improved build speed) in the past, and most of the work to get there is to seperate out structures like task_struct, mm_struct, net_device, etc into headers that only depend on the embedded structure definitions without needing all the inline functions associated with them.

This is something I'll add to our automation todos which I plan to talk about at plumbers; I feel like it should be possible to write a script that given a header and identifier can split whatever declaration out into a new header, update the old header, then add the necessary includes for the newly created header to each dependent (optional).

Signature · 2 lines
Thanks,
~Nick Desaulniers
author maintainer
#49 lore raw

PATCH v2 29/39 mm: percpu: Introduce pcpuobj_ext

In reply to Suren Baghdasaryan

From: 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
index cdd0aa597a81..e62d582f4bf3 100644
@@ -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
index a7665de8485f..5a6202acffa3 100644
@@ -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);
@@ -1648,8 +1648,8 @@ static void pcpu_memcg_post_alloc_hook(struct obj_cgroup *objcg,
 	if (!objcg)
 		return;
 
-	if (likely(chunk && chunk->obj_cgroups)) {
-		chunk->obj_cgroups[off >> PCPU_MIN_ALLOC_SHIFT] = objcg;
+	if (likely(chunk && chunk->obj_exts)) {
+		chunk->obj_exts[off >> PCPU_MIN_ALLOC_SHIFT].cgroup = objcg;
 
 		rcu_read_lock();
 		mod_memcg_state(obj_cgroup_memcg(objcg), MEMCG_PERCPU_B,
@@ -1665,13 +1665,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.42.0.758.gaed0368e0e-goog
author maintainer
#50 lore raw

PATCH v2 30/39 mm: percpu: Add codetag reference into pcpuobj_ext

In reply to Suren Baghdasaryan

From: 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
index e62d582f4bf3..7e42f0ca3b7b 100644
@@ -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
index 5a6202acffa3..002ee5d38fd5 100644
@@ -1701,6 +1701,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.42.0.758.gaed0368e0e-goog
author maintainer
#51 lore raw

PATCH v2 31/39 mm: percpu: enable per-cpu allocation tagging

In reply to Suren Baghdasaryan

Redefine __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
index 6fa8a94d8bc1..3fe51e67e231 100644
@@ -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
index 68fac2e7cbe6..338c1ef9c93d 100644
@@ -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>
 
@@ -121,7 +123,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);
 
@@ -129,13 +130,15 @@ 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);
 
-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),	\
@@ -144,6 +147,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.42.0.758.gaed0368e0e-goog
author maintainer
#52 lore raw

PATCH v2 32/39 arm64: Fix circular header dependency

In reply to Suren Baghdasaryan

From: Kent Overstreet <[email protected]>

Replace linux/percpu.h include with asm/percpu.h to avoid circular dependency.

---
 arch/arm64/include/asm/spectre.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/arm64/include/asm/spectre.h +2 -2
index 9cc501450486..75e837753772 100644
@@ -13,8 +13,8 @@
 #define __BP_HARDEN_HYP_VECS_SZ	((BP_HARDEN_EL2_SLOTS - 1) * SZ_2K)
 
 #ifndef __ASSEMBLY__
-
-#include <linux/percpu.h>
+#include <linux/smp.h>
+#include <asm/percpu.h>
 
 #include <asm/cpufeature.h>
 #include <asm/virt.h>
Signature · 1 line
2.42.0.758.gaed0368e0e-goog
author maintainer
#53 lore raw

PATCH v2 33/39 mm: vmalloc: Enable memory allocation profiling

In reply to Suren Baghdasaryan

From: 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
index bb12644fd033..3e2899ad8517 100644
@@ -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
index b4cac76ea5e9..3ea9be364e32 100644
@@ -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
index 27ed6a5ac31a..7e6043ece040 100644
@@ -629,7 +629,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));
 }
@@ -688,12 +688,12 @@ void *kvrealloc_noprof(const void *p, size_t oldsize, size_t newsize, gfp_t flag
 EXPORT_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;
 
@@ -701,18 +701,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.
@@ -720,22 +720,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.42.0.758.gaed0368e0e-goog
#54 lore raw

Re: [PATCH v2 33/39] mm: vmalloc: Enable memory allocation profiling

kernel test robot noticed the following build warnings:

Hi Suren,

kernel test robot noticed the following build warnings:

[auto build test WARNING on vbabka-slab/for-next]
[also build test WARNING on kees/for-next/hardening linus/master v6.6-rc7] [cannot apply to akpm-mm/mm-everything next-20231024]
[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/20231024-215116
base:   git://git.kernel.org/pub/scm/linux/kernel/git/vbabka/slab.git for-next
patch link:    https://lore.kernel.org/r/20231024134637.3120277-34-surenb%40google.com
patch subject: [PATCH v2 33/39] mm: vmalloc: Enable memory allocation profiling
config: sparc-allyesconfig (https://download.01.org/0day-ci/archive/20231025/[email protected]/config)
compiler: sparc64-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231025/[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 warnings (new ones prefixed by >>):

In file included from include/linux/sched.h:12,

                    from include/linux/vmalloc.h:6,
                    from include/asm-generic/io.h:994,
                    from arch/sparc/include/asm/io.h:22,
                    from arch/sparc/vdso/vclock_gettime.c:18:
>> arch/sparc/include/asm/current.h:18:30: warning: call-clobbered register used for global register variable
      18 | register struct task_struct *current asm("g4");
         |                              ^~~~~~~
   In file included from include/linux/printk.h:10,
                    from include/linux/kernel.h:30,
                    from arch/sparc/vdso/vclock_gettime.c:15:
   include/linux/seqlock.h: In function '__seqprop_assert':
   arch/sparc/include/asm/percpu_64.h:19:25: error: '__local_per_cpu_offset' undeclared (first use in this function); did you mean 'per_cpu_offset'?
      19 | #define __my_cpu_offset __local_per_cpu_offset
         |                         ^~~~~~~~~~~~~~~~~~~~~~
   include/linux/once_lite.h:28:41: note: in definition of macro 'DO_ONCE_LITE_IF'
      28 |                 bool __ret_do_once = !!(condition);                     \
         |                                         ^~~~~~~~~
   include/linux/lockdep.h:644:9: note: in expansion of macro 'WARN_ON_ONCE'
     644 |         WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_COUNT)   &&              \
         |         ^~~~~~~~~~~~
   include/linux/compiler_types.h:413:9: note: in expansion of macro '__compiletime_assert'
     413 |         __compiletime_assert(condition, msg, prefix, suffix)
         |         ^~~~~~~~~~~~~~~~~~~~
   include/linux/compiler_types.h:425:9: note: in expansion of macro '_compiletime_assert'
     425 |         _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
         |         ^~~~~~~~~~~~~~~~~~~
   include/asm-generic/rwonce.h:36:9: note: in expansion of macro 'compiletime_assert'
      36 |         compiletime_assert(__native_word(t) || sizeof(t) == sizeof(long long),  \
         |         ^~~~~~~~~~~~~~~~~~
   include/asm-generic/rwonce.h:36:28: note: in expansion of macro '__native_word'
      36 |         compiletime_assert(__native_word(t) || sizeof(t) == sizeof(long long),  \
         |                            ^~~~~~~~~~~~~
   include/asm-generic/rwonce.h:49:9: note: in expansion of macro 'compiletime_assert_rwonce_type'
      49 |         compiletime_assert_rwonce_type(x);                              \
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/asm-generic/percpu.h:127:18: note: in expansion of macro 'READ_ONCE'
     127 |         ___ret = READ_ONCE(*raw_cpu_ptr(&(pcp)));                       \
         |                  ^~~~~~~~~
   include/linux/percpu-defs.h:231:9: note: in expansion of macro 'RELOC_HIDE'
     231 |         RELOC_HIDE((typeof(*(__p)) __kernel __force *)(__p), (__offset))
         |         ^~~~~~~~~~
   include/asm-generic/percpu.h:44:31: note: in expansion of macro 'SHIFT_PERCPU_PTR'
      44 | #define arch_raw_cpu_ptr(ptr) SHIFT_PERCPU_PTR(ptr, __my_cpu_offset)
         |                               ^~~~~~~~~~~~~~~~
   include/asm-generic/percpu.h:44:53: note: in expansion of macro '__my_cpu_offset'
      44 | #define arch_raw_cpu_ptr(ptr) SHIFT_PERCPU_PTR(ptr, __my_cpu_offset)
         |                                                     ^~~~~~~~~~~~~~~
   include/linux/percpu-defs.h:242:9: note: in expansion of macro 'arch_raw_cpu_ptr'
     242 |         arch_raw_cpu_ptr(ptr);                                          \
         |         ^~~~~~~~~~~~~~~~
   include/asm-generic/percpu.h:127:29: note: in expansion of macro 'raw_cpu_ptr'
     127 |         ___ret = READ_ONCE(*raw_cpu_ptr(&(pcp)));                       \
         |                             ^~~~~~~~~~~
   include/asm-generic/percpu.h:146:25: note: in expansion of macro '__this_cpu_generic_read_nopreempt'
     146 |                 __ret = __this_cpu_generic_read_nopreempt(pcp);         \
         |                         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/asm-generic/percpu.h:375:41: note: in expansion of macro 'this_cpu_generic_read'
     375 | #define this_cpu_read_1(pcp)            this_cpu_generic_read(pcp)
         |                                         ^~~~~~~~~~~~~~~~~~~~~
   include/linux/percpu-defs.h:321:30: note: in expansion of macro 'this_cpu_read_1'
     321 |         case 1: pscr_ret__ = stem##1(variable); break;                  \
         |                              ^~~~
   include/linux/percpu-defs.h:489:41: note: in expansion of macro '__pcpu_size_call_return'
     489 | #define this_cpu_read(pcp)              __pcpu_size_call_return(this_cpu_read_, pcp)
         |                                         ^~~~~~~~~~~~~~~~~~~~~~~
   include/linux/lockdep.h:611:50: note: in expansion of macro 'this_cpu_read'
     611 | #define __lockdep_enabled       (debug_locks && !this_cpu_read(lockdep_recursion))
         |                                                  ^~~~~~~~~~~~~
   include/linux/lockdep.h:645:22: note: in expansion of macro '__lockdep_enabled'
     645 |                      __lockdep_enabled                  &&              \
         |                      ^~~~~~~~~~~~~~~~~
   include/linux/seqlock.h:269:9: note: in expansion of macro 'lockdep_assert_preemption_disabled'
     269 |         lockdep_assert_preemption_disabled();
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   arch/sparc/include/asm/percpu_64.h:19:25: note: each undeclared identifier is reported only once for each function it appears in
      19 | #define __my_cpu_offset __local_per_cpu_offset
         |                         ^~~~~~~~~~~~~~~~~~~~~~
   include/linux/once_lite.h:28:41: note: in definition of macro 'DO_ONCE_LITE_IF'
      28 |                 bool __ret_do_once = !!(condition);                     \
         |                                         ^~~~~~~~~
   include/linux/lockdep.h:644:9: note: in expansion of macro 'WARN_ON_ONCE'
     644 |         WARN_ON_ONCE(IS_ENABLED(CONFIG_PREEMPT_COUNT)   &&              \
         |         ^~~~~~~~~~~~
   include/linux/compiler_types.h:413:9: note: in expansion of macro '__compiletime_assert'
     413 |         __compiletime_assert(condition, msg, prefix, suffix)
         |         ^~~~~~~~~~~~~~~~~~~~
   include/linux/compiler_types.h:425:9: note: in expansion of macro '_compiletime_assert'
     425 |         _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
         |         ^~~~~~~~~~~~~~~~~~~
   include/asm-generic/rwonce.h:36:9: note: in expansion of macro 'compiletime_assert'
      36 |         compiletime_assert(__native_word(t) || sizeof(t) == sizeof(long long),  \
         |         ^~~~~~~~~~~~~~~~~~
   include/asm-generic/rwonce.h:36:28: note: in expansion of macro '__native_word'
      36 |         compiletime_assert(__native_word(t) || sizeof(t) == sizeof(long long),  \
         |                            ^~~~~~~~~~~~~
   include/asm-generic/rwonce.h:49:9: note: in expansion of macro 'compiletime_assert_rwonce_type'
      49 |         compiletime_assert_rwonce_type(x);                              \
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   include/asm-generic/percpu.h:127:18: note: in expansion of macro 'READ_ONCE'
     127 |         ___ret = READ_ONCE(*raw_cpu_ptr(&(pcp)));                       \
         |                  ^~~~~~~~~
   include/linux/percpu-defs.h:231:9: note: in expansion of macro 'RELOC_HIDE'
     231 |         RELOC_HIDE((typeof(*(__p)) __kernel __force *)(__p), (__offset))
         |         ^~~~~~~~~~

include/asm-generic/percpu.h:44:31: note: in expansion of macro 'SHIFT_PERCPU_PTR'

vim +18 arch/sparc/include/asm/current.h

^1da177e4c3f41 include/asm-sparc/current.h Linus Torvalds 2005-04-16 16

ba89f59ab825d4 include/asm-sparc/current.h David S. Miller 2007-11-16  17  #ifdef CONFIG_SPARC64
ba89f59ab825d4 include/asm-sparc/current.h David S. Miller 2007-11-16 @18  register struct task_struct *current asm("g4");
ba89f59ab825d4 include/asm-sparc/current.h David S. Miller 2007-11-16  19  #endif

^1da177e4c3f41 include/asm-sparc/current.h Linus Torvalds 2005-04-16 20

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#55 lore raw

Re: [PATCH v2 33/39] mm: vmalloc: Enable memory allocation profiling

In reply to Suren Baghdasaryan
kernel test robot noticed the following build warnings:

Hi Suren,

kernel test robot noticed the following build warnings:

[auto build test WARNING on vbabka-slab/for-next]
[also build test WARNING on kees/for-next/hardening linus/master v6.6-rc7] [cannot apply to akpm-mm/mm-everything next-20231024]
[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/20231024-215116
base:   git://git.kernel.org/pub/scm/linux/kernel/git/vbabka/slab.git for-next
patch link:    https://lore.kernel.org/r/20231024134637.3120277-34-surenb%40google.com
patch subject: [PATCH v2 33/39] mm: vmalloc: Enable memory allocation profiling
config: m68k-allyesconfig (https://download.01.org/0day-ci/archive/20231025/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20231025/[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 warnings (new ones prefixed by >>):

>> mm/util.c:724: warning: expecting prototype for __vcalloc(). Prototype was for __vcalloc_noprof() instead
--
>> mm/vmalloc.c:3417: warning: expecting prototype for vmalloc(). Prototype was for vmalloc_noprof() instead
>> mm/vmalloc.c:3436: warning: expecting prototype for vmalloc_huge(). Prototype was for vmalloc_huge_noprof() instead
>> mm/vmalloc.c:3473: warning: expecting prototype for vmalloc_user(). Prototype was for vmalloc_user_noprof() instead

vim +724 mm/util.c

a8749a35c39903 Paolo Bonzini   2022-03-08  716  
a8749a35c39903 Paolo Bonzini   2022-03-08  717  /**
a8749a35c39903 Paolo Bonzini   2022-03-08  718   * __vcalloc - allocate and zero memory for a virtually contiguous array.
a8749a35c39903 Paolo Bonzini   2022-03-08  719   * @n: number of elements.
a8749a35c39903 Paolo Bonzini   2022-03-08  720   * @size: element size.
a8749a35c39903 Paolo Bonzini   2022-03-08  721   * @flags: the type of memory to allocate (see kmalloc).
a8749a35c39903 Paolo Bonzini   2022-03-08  722   */
d0e9eb4df906ad Kent Overstreet 2023-10-24  723  void *__vcalloc_noprof(size_t n, size_t size, gfp_t flags)
a8749a35c39903 Paolo Bonzini   2022-03-08 @724  {
a8749a35c39903 Paolo Bonzini   2022-03-08  725  	return __vmalloc_array(n, size, flags | __GFP_ZERO);
a8749a35c39903 Paolo Bonzini   2022-03-08  726  }
d0e9eb4df906ad Kent Overstreet 2023-10-24  727  EXPORT_SYMBOL(__vcalloc_noprof);

a8749a35c39903 Paolo Bonzini 2022-03-08 728

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author maintainer
#56 lore raw

PATCH v2 34/39 rhashtable: Plumb through alloc tag

In reply to Suren Baghdasaryan

From: 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
index 57467cbf4c5b..aac2984c2ef0 100644
@@ -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.42.0.758.gaed0368e0e-goog
author maintainer
#57 lore raw

PATCH v2 35/39 lib: add memory allocations report in show_mem()

In reply to Suren Baghdasaryan

Include allocations in show_mem reports.

---
 include/linux/alloc_tag.h |  2 ++
 lib/alloc_tag.c           | 37 +++++++++++++++++++++++++++++++++++++
 mm/show_mem.c             | 15 +++++++++++++++
 3 files changed, 54 insertions(+)
include/linux/alloc_tag.h +2 -0
index 3fe51e67e231..0a5973c4ad77 100644
@@ -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 +37 -0
index 2d5226d9262d..2f7a2e3ddf55 100644
@@ -96,6 +96,43 @@ 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
index 4b888b18bdde..660e9a78a34d 100644
@@ -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>
 
@@ -426,4 +427,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.42.0.758.gaed0368e0e-goog
author maintainer
#58 lore raw

PATCH v2 36/39 codetag: debug: skip objext checking when it's for objext itself

In reply to Suren Baghdasaryan

objext 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                 | 33 +++++++++++++++++++++++++++++++++
 mm/slab_common.c          |  1 +
 3 files changed, 60 insertions(+)
include/linux/alloc_tag.h +26 -0
index 0a5973c4ad77..1f3207097b03 100644
@@ -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 +33 -0
index 4859ce1f8808..45216bad34b8 100644
@@ -455,6 +455,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
@@ -476,6 +501,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;
 }
mm/slab_common.c +1 -0
index 8ef5e47ff6a7..db2cd7afc353 100644
@@ -246,6 +246,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;
 	}
Signature · 1 line
2.42.0.758.gaed0368e0e-goog
author maintainer
#59 lore raw

PATCH v2 37/39 codetag: debug: mark codetags for reserved pages as empty

In reply to Suren Baghdasaryan

To avoid debug warnings while freeing reserved pages which were not allocated with usual allocators, mark their codetags as empty before freeing.
Maybe we can annotate reserved pages correctly and avoid this?

---
 include/linux/alloc_tag.h   | 2 ++
 include/linux/mm.h          | 8 ++++++++
 include/linux/pgalloc_tag.h | 2 ++
 3 files changed, 12 insertions(+)
include/linux/alloc_tag.h +2 -0
index 1f3207097b03..102caf62c2a9 100644
@@ -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
index bf5d0b1b16f4..310129414833 100644
@@ -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>
@@ -3077,6 +3078,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
index 0174aff5e871..ae9b0f359264 100644
@@ -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) {}
Signature · 1 line
2.42.0.758.gaed0368e0e-goog
author maintainer
#60 lore raw

PATCH v2 38/39 codetag: debug: introduce OBJEXTS_ALLOC_FAIL to mark failed slab_ext allocations

In reply to Suren Baghdasaryan

If 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
index 853a24b5f713..6b680ca424e3 100644
@@ -363,8 +363,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
index 45216bad34b8..1736268892e6 100644
@@ -474,9 +474,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
index db2cd7afc353..cea73314f919 100644
@@ -218,29 +218,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.42.0.758.gaed0368e0e-goog
author maintainer
#61 lore raw

PATCH v2 39/39 MAINTAINERS: Add entries for code tagging and memory allocation profiling

In reply to Suren Baghdasaryan

From: 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
index 2894f0777537..22e51de42131 100644
@@ -5118,6 +5118,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]>
@@ -13708,6 +13715,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.42.0.758.gaed0368e0e-goog
maintainer
#62 lore raw
In reply to Suren Baghdasaryan
Quoted 11 lines · Suren Baghdasaryan

Updates since the last version [1]
- Simplified allocation tagging macros;
- Runtime enable/disable sysctl switch (/proc/sys/vm/mem_profiling) instead of kernel command-line option;
- CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT to select default enable state;
- Changed the user-facing API from debugfs to procfs (/proc/allocinfo);
- Removed context capture support to make patch incremental;
- Renamed uninstrumented allocation functions to use _noprof suffix;
- Added __GFP_LAST_BIT to make the code cleaner;
- Removed lazy per-cpu counters; it turned out the memory savings was minimal and not worth the performance impact;

Hello Suren,

Quoted 9 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 is performance comparison between the baseline kernel, profiling when enabled, profiling when disabled and (for comparison purposes) baseline with CONFIG_MEMCG_KMEM enabled and allocations using __GFP_ACCOUNT:

Quoted 6 lines
                        kmalloc                 pgalloc
(1 baseline)            12.041s                 49.190s
(2 default disabled)    14.970s (+24.33%)       49.684s (+1.00%)
(3 default enabled)     16.859s (+40.01%)       56.287s (+14.43%)
(4 runtime enabled)     16.983s (+41.04%)       55.760s (+13.36%)
(5 memcg)               33.831s (+180.96%)      51.433s (+4.56%)

some recent changes [1] to the kmem accounting should have made it quite a bit faster. Would be great if you can provide new numbers for the comparison. Maybe with the next revision?

And btw thank you (and Kent): your numbers inspired me to do this kmemcg performance work. I expect it still to be ~twice more expensive than your stuff because on the memcg side we handle separately charge and statistics, but hopefully the difference will be lower.

Thank you!

[1]:
patches from next tree, so no stable hashes:
mm: kmem: reimplement get_obj_cgroup_from_current()
percpu: scoped objcg protection
mm: kmem: scoped objcg protection
mm: kmem: make memcg keep a reference to the original objcg
mm: kmem: add direct objcg pointer to task_struct
mm: kmem: optimize get_obj_cgroup_from_current()

author maintainer
#63 lore raw
Quoted 32 lines · Roman Gushchin
Quoted 11 lines · Suren Baghdasaryan

Updates since the last version [1]
- Simplified allocation tagging macros;
- Runtime enable/disable sysctl switch (/proc/sys/vm/mem_profiling) instead of kernel command-line option;
- CONFIG_MEM_ALLOC_PROFILING_BY_DEFAULT to select default enable state;
- Changed the user-facing API from debugfs to procfs (/proc/allocinfo);
- Removed context capture support to make patch incremental;
- Renamed uninstrumented allocation functions to use _noprof suffix;
- Added __GFP_LAST_BIT to make the code cleaner;
- Removed lazy per-cpu counters; it turned out the memory savings was minimal and not worth the performance impact;

Hello Suren,

Quoted 9 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 is performance comparison between the baseline kernel, profiling when enabled, profiling when disabled and (for comparison purposes) baseline with CONFIG_MEMCG_KMEM enabled and allocations using __GFP_ACCOUNT:

Quoted 6 lines
                        kmalloc                 pgalloc
(1 baseline)            12.041s                 49.190s
(2 default disabled)    14.970s (+24.33%)       49.684s (+1.00%)
(3 default enabled)     16.859s (+40.01%)       56.287s (+14.43%)
(4 runtime enabled)     16.983s (+41.04%)       55.760s (+13.36%)
(5 memcg)               33.831s (+180.96%)      51.433s (+4.56%)
Quoted 8 lines

some recent changes [1] to the kmem accounting should have made it quite a bit faster. Would be great if you can provide new numbers for the comparison. Maybe with the next revision?

And btw thank you (and Kent): your numbers inspired me to do this kmemcg performance work. I expect it still to be ~twice more expensive than your stuff because on the memcg side we handle separately charge and statistics, but hopefully the difference will be lower.

Yes, I saw them! Well done! I'll definitely update my numbers once the patches land in their final form.

Quoted 2 lines

Thank you!

Thank you for the optimizations!

Quoted 9 lines

[1]:
patches from next tree, so no stable hashes:
mm: kmem: reimplement get_obj_cgroup_from_current()
percpu: scoped objcg protection
mm: kmem: scoped objcg protection
mm: kmem: make memcg keep a reference to the original objcg
mm: kmem: add direct objcg pointer to task_struct
mm: kmem: optimize get_obj_cgroup_from_current()