xref: /linux/lib/alloc_tag.c (revision 984921edea68bf24bcc87e1317bfc90451ff46c6)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/alloc_tag.h>
3 #include <linux/execmem.h>
4 #include <linux/fs.h>
5 #include <linux/gfp.h>
6 #include <linux/kallsyms.h>
7 #include <linux/module.h>
8 #include <linux/page_ext.h>
9 #include <linux/proc_fs.h>
10 #include <linux/seq_buf.h>
11 #include <linux/seq_file.h>
12 #include <linux/vmalloc.h>
13 #include <linux/kmemleak.h>
14 
15 #define ALLOCINFO_FILE_NAME		"allocinfo"
16 #define MODULE_ALLOC_TAG_VMAP_SIZE	(100000UL * sizeof(struct alloc_tag))
17 #define SECTION_START(NAME)		(CODETAG_SECTION_START_PREFIX NAME)
18 #define SECTION_STOP(NAME)		(CODETAG_SECTION_STOP_PREFIX NAME)
19 
20 #ifdef CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT
21 static bool mem_profiling_support = true;
22 #else
23 static bool mem_profiling_support;
24 #endif
25 
26 static struct codetag_type *alloc_tag_cttype;
27 
28 #ifdef CONFIG_ARCH_MODULE_NEEDS_WEAK_PER_CPU
29 DEFINE_PER_CPU(struct alloc_tag_counters, _shared_alloc_tag);
30 EXPORT_SYMBOL(_shared_alloc_tag);
31 #endif
32 
33 DEFINE_STATIC_KEY_MAYBE(CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT,
34 			mem_alloc_profiling_key);
35 EXPORT_SYMBOL(mem_alloc_profiling_key);
36 
37 DEFINE_STATIC_KEY_FALSE(mem_profiling_compressed);
38 
39 struct alloc_tag_kernel_section kernel_tags = { NULL, 0 };
40 unsigned long alloc_tag_ref_mask;
41 int alloc_tag_ref_offs;
42 
43 struct allocinfo_private {
44 	struct codetag_iterator iter;
45 	bool print_header;
46 };
47 
48 static void *allocinfo_start(struct seq_file *m, loff_t *pos)
49 {
50 	struct allocinfo_private *priv;
51 	loff_t node = *pos;
52 
53 	priv = (struct allocinfo_private *)m->private;
54 	codetag_lock_module_list(alloc_tag_cttype, true);
55 	if (node == 0) {
56 		priv->print_header = true;
57 		priv->iter = codetag_get_ct_iter(alloc_tag_cttype);
58 		codetag_next_ct(&priv->iter);
59 	}
60 	return priv->iter.ct ? priv : NULL;
61 }
62 
63 static void *allocinfo_next(struct seq_file *m, void *arg, loff_t *pos)
64 {
65 	struct allocinfo_private *priv = (struct allocinfo_private *)arg;
66 	struct codetag *ct = codetag_next_ct(&priv->iter);
67 
68 	(*pos)++;
69 	if (!ct)
70 		return NULL;
71 
72 	return priv;
73 }
74 
75 static void allocinfo_stop(struct seq_file *m, void *arg)
76 {
77 	codetag_lock_module_list(alloc_tag_cttype, false);
78 }
79 
80 static void print_allocinfo_header(struct seq_buf *buf)
81 {
82 	/* Output format version, so we can change it. */
83 	seq_buf_printf(buf, "allocinfo - version: 1.0\n");
84 	seq_buf_printf(buf, "#     <size>  <calls> <tag info>\n");
85 }
86 
87 static void alloc_tag_to_text(struct seq_buf *out, struct codetag *ct)
88 {
89 	struct alloc_tag *tag = ct_to_alloc_tag(ct);
90 	struct alloc_tag_counters counter = alloc_tag_read(tag);
91 	s64 bytes = counter.bytes;
92 
93 	seq_buf_printf(out, "%12lli %8llu ", bytes, counter.calls);
94 	codetag_to_text(out, ct);
95 	seq_buf_putc(out, ' ');
96 	seq_buf_putc(out, '\n');
97 }
98 
99 static int allocinfo_show(struct seq_file *m, void *arg)
100 {
101 	struct allocinfo_private *priv = (struct allocinfo_private *)arg;
102 	char *bufp;
103 	size_t n = seq_get_buf(m, &bufp);
104 	struct seq_buf buf;
105 
106 	seq_buf_init(&buf, bufp, n);
107 	if (priv->print_header) {
108 		print_allocinfo_header(&buf);
109 		priv->print_header = false;
110 	}
111 	alloc_tag_to_text(&buf, priv->iter.ct);
112 	seq_commit(m, seq_buf_used(&buf));
113 	return 0;
114 }
115 
116 static const struct seq_operations allocinfo_seq_op = {
117 	.start	= allocinfo_start,
118 	.next	= allocinfo_next,
119 	.stop	= allocinfo_stop,
120 	.show	= allocinfo_show,
121 };
122 
123 size_t alloc_tag_top_users(struct codetag_bytes *tags, size_t count, bool can_sleep)
124 {
125 	struct codetag_iterator iter;
126 	struct codetag *ct;
127 	struct codetag_bytes n;
128 	unsigned int i, nr = 0;
129 
130 	if (can_sleep)
131 		codetag_lock_module_list(alloc_tag_cttype, true);
132 	else if (!codetag_trylock_module_list(alloc_tag_cttype))
133 		return 0;
134 
135 	iter = codetag_get_ct_iter(alloc_tag_cttype);
136 	while ((ct = codetag_next_ct(&iter))) {
137 		struct alloc_tag_counters counter = alloc_tag_read(ct_to_alloc_tag(ct));
138 
139 		n.ct	= ct;
140 		n.bytes = counter.bytes;
141 
142 		for (i = 0; i < nr; i++)
143 			if (n.bytes > tags[i].bytes)
144 				break;
145 
146 		if (i < count) {
147 			nr -= nr == count;
148 			memmove(&tags[i + 1],
149 				&tags[i],
150 				sizeof(tags[0]) * (nr - i));
151 			nr++;
152 			tags[i] = n;
153 		}
154 	}
155 
156 	codetag_lock_module_list(alloc_tag_cttype, false);
157 
158 	return nr;
159 }
160 
161 void pgalloc_tag_split(struct folio *folio, int old_order, int new_order)
162 {
163 	int i;
164 	struct alloc_tag *tag;
165 	unsigned int nr_pages = 1 << new_order;
166 
167 	if (!mem_alloc_profiling_enabled())
168 		return;
169 
170 	tag = __pgalloc_tag_get(&folio->page);
171 	if (!tag)
172 		return;
173 
174 	for (i = nr_pages; i < (1 << old_order); i += nr_pages) {
175 		union pgtag_ref_handle handle;
176 		union codetag_ref ref;
177 
178 		if (get_page_tag_ref(folio_page(folio, i), &ref, &handle)) {
179 			/* Set new reference to point to the original tag */
180 			alloc_tag_ref_set(&ref, tag);
181 			update_page_tag_ref(handle, &ref);
182 			put_page_tag_ref(handle);
183 		}
184 	}
185 }
186 
187 void pgalloc_tag_swap(struct folio *new, struct folio *old)
188 {
189 	union pgtag_ref_handle handle_old, handle_new;
190 	union codetag_ref ref_old, ref_new;
191 	struct alloc_tag *tag_old, *tag_new;
192 
193 	if (!mem_alloc_profiling_enabled())
194 		return;
195 
196 	tag_old = __pgalloc_tag_get(&old->page);
197 	if (!tag_old)
198 		return;
199 	tag_new = __pgalloc_tag_get(&new->page);
200 	if (!tag_new)
201 		return;
202 
203 	if (!get_page_tag_ref(&old->page, &ref_old, &handle_old))
204 		return;
205 	if (!get_page_tag_ref(&new->page, &ref_new, &handle_new)) {
206 		put_page_tag_ref(handle_old);
207 		return;
208 	}
209 
210 	/*
211 	 * Clear tag references to avoid debug warning when using
212 	 * __alloc_tag_ref_set() with non-empty reference.
213 	 */
214 	set_codetag_empty(&ref_old);
215 	set_codetag_empty(&ref_new);
216 
217 	/* swap tags */
218 	__alloc_tag_ref_set(&ref_old, tag_new);
219 	update_page_tag_ref(handle_old, &ref_old);
220 	__alloc_tag_ref_set(&ref_new, tag_old);
221 	update_page_tag_ref(handle_new, &ref_new);
222 
223 	put_page_tag_ref(handle_old);
224 	put_page_tag_ref(handle_new);
225 }
226 
227 static void shutdown_mem_profiling(bool remove_file)
228 {
229 	if (mem_alloc_profiling_enabled())
230 		static_branch_disable(&mem_alloc_profiling_key);
231 
232 	if (!mem_profiling_support)
233 		return;
234 
235 	if (remove_file)
236 		remove_proc_entry(ALLOCINFO_FILE_NAME, NULL);
237 	mem_profiling_support = false;
238 }
239 
240 void __init alloc_tag_sec_init(void)
241 {
242 	struct alloc_tag *last_codetag;
243 
244 	if (!mem_profiling_support)
245 		return;
246 
247 	if (!static_key_enabled(&mem_profiling_compressed))
248 		return;
249 
250 	kernel_tags.first_tag = (struct alloc_tag *)kallsyms_lookup_name(
251 					SECTION_START(ALLOC_TAG_SECTION_NAME));
252 	last_codetag = (struct alloc_tag *)kallsyms_lookup_name(
253 					SECTION_STOP(ALLOC_TAG_SECTION_NAME));
254 	kernel_tags.count = last_codetag - kernel_tags.first_tag;
255 
256 	/* Check if kernel tags fit into page flags */
257 	if (kernel_tags.count > (1UL << NR_UNUSED_PAGEFLAG_BITS)) {
258 		shutdown_mem_profiling(false); /* allocinfo file does not exist yet */
259 		pr_err("%lu allocation tags cannot be references using %d available page flag bits. Memory allocation profiling is disabled!\n",
260 			kernel_tags.count, NR_UNUSED_PAGEFLAG_BITS);
261 		return;
262 	}
263 
264 	alloc_tag_ref_offs = (LRU_REFS_PGOFF - NR_UNUSED_PAGEFLAG_BITS);
265 	alloc_tag_ref_mask = ((1UL << NR_UNUSED_PAGEFLAG_BITS) - 1);
266 	pr_debug("Memory allocation profiling compression is using %d page flag bits!\n",
267 		 NR_UNUSED_PAGEFLAG_BITS);
268 }
269 
270 #ifdef CONFIG_MODULES
271 
272 static struct maple_tree mod_area_mt = MTREE_INIT(mod_area_mt, MT_FLAGS_ALLOC_RANGE);
273 static struct vm_struct *vm_module_tags;
274 /* A dummy object used to indicate an unloaded module */
275 static struct module unloaded_mod;
276 /* A dummy object used to indicate a module prepended area */
277 static struct module prepend_mod;
278 
279 struct alloc_tag_module_section module_tags;
280 
281 static inline unsigned long alloc_tag_align(unsigned long val)
282 {
283 	if (!static_key_enabled(&mem_profiling_compressed)) {
284 		/* No alignment requirements when we are not indexing the tags */
285 		return val;
286 	}
287 
288 	if (val % sizeof(struct alloc_tag) == 0)
289 		return val;
290 	return ((val / sizeof(struct alloc_tag)) + 1) * sizeof(struct alloc_tag);
291 }
292 
293 static bool ensure_alignment(unsigned long align, unsigned int *prepend)
294 {
295 	if (!static_key_enabled(&mem_profiling_compressed)) {
296 		/* No alignment requirements when we are not indexing the tags */
297 		return true;
298 	}
299 
300 	/*
301 	 * If alloc_tag size is not a multiple of required alignment, tag
302 	 * indexing does not work.
303 	 */
304 	if (!IS_ALIGNED(sizeof(struct alloc_tag), align))
305 		return false;
306 
307 	/* Ensure prepend consumes multiple of alloc_tag-sized blocks */
308 	if (*prepend)
309 		*prepend = alloc_tag_align(*prepend);
310 
311 	return true;
312 }
313 
314 static inline bool tags_addressable(void)
315 {
316 	unsigned long tag_idx_count;
317 
318 	if (!static_key_enabled(&mem_profiling_compressed))
319 		return true; /* with page_ext tags are always addressable */
320 
321 	tag_idx_count = CODETAG_ID_FIRST + kernel_tags.count +
322 			module_tags.size / sizeof(struct alloc_tag);
323 
324 	return tag_idx_count < (1UL << NR_UNUSED_PAGEFLAG_BITS);
325 }
326 
327 static bool needs_section_mem(struct module *mod, unsigned long size)
328 {
329 	if (!mem_profiling_support)
330 		return false;
331 
332 	return size >= sizeof(struct alloc_tag);
333 }
334 
335 static bool clean_unused_counters(struct alloc_tag *start_tag,
336 				  struct alloc_tag *end_tag)
337 {
338 	struct alloc_tag *tag;
339 	bool ret = true;
340 
341 	for (tag = start_tag; tag <= end_tag; tag++) {
342 		struct alloc_tag_counters counter;
343 
344 		if (!tag->counters)
345 			continue;
346 
347 		counter = alloc_tag_read(tag);
348 		if (!counter.bytes) {
349 			free_percpu(tag->counters);
350 			tag->counters = NULL;
351 		} else {
352 			ret = false;
353 		}
354 	}
355 
356 	return ret;
357 }
358 
359 /* Called with mod_area_mt locked */
360 static void clean_unused_module_areas_locked(void)
361 {
362 	MA_STATE(mas, &mod_area_mt, 0, module_tags.size);
363 	struct module *val;
364 
365 	mas_for_each(&mas, val, module_tags.size) {
366 		struct alloc_tag *start_tag;
367 		struct alloc_tag *end_tag;
368 
369 		if (val != &unloaded_mod)
370 			continue;
371 
372 		/* Release area if all tags are unused */
373 		start_tag = (struct alloc_tag *)(module_tags.start_addr + mas.index);
374 		end_tag = (struct alloc_tag *)(module_tags.start_addr + mas.last);
375 		if (clean_unused_counters(start_tag, end_tag))
376 			mas_erase(&mas);
377 	}
378 }
379 
380 /* Called with mod_area_mt locked */
381 static bool find_aligned_area(struct ma_state *mas, unsigned long section_size,
382 			      unsigned long size, unsigned int prepend, unsigned long align)
383 {
384 	bool cleanup_done = false;
385 
386 repeat:
387 	/* Try finding exact size and hope the start is aligned */
388 	if (!mas_empty_area(mas, 0, section_size - 1, prepend + size)) {
389 		if (IS_ALIGNED(mas->index + prepend, align))
390 			return true;
391 
392 		/* Try finding larger area to align later */
393 		mas_reset(mas);
394 		if (!mas_empty_area(mas, 0, section_size - 1,
395 				    size + prepend + align - 1))
396 			return true;
397 	}
398 
399 	/* No free area, try cleanup stale data and repeat the search once */
400 	if (!cleanup_done) {
401 		clean_unused_module_areas_locked();
402 		cleanup_done = true;
403 		mas_reset(mas);
404 		goto repeat;
405 	}
406 
407 	return false;
408 }
409 
410 static int vm_module_tags_populate(void)
411 {
412 	unsigned long phys_end = ALIGN_DOWN(module_tags.start_addr, PAGE_SIZE) +
413 				 (vm_module_tags->nr_pages << PAGE_SHIFT);
414 	unsigned long new_end = module_tags.start_addr + module_tags.size;
415 
416 	if (phys_end < new_end) {
417 		struct page **next_page = vm_module_tags->pages + vm_module_tags->nr_pages;
418 		unsigned long old_shadow_end = ALIGN(phys_end, MODULE_ALIGN);
419 		unsigned long new_shadow_end = ALIGN(new_end, MODULE_ALIGN);
420 		unsigned long more_pages;
421 		unsigned long nr = 0;
422 
423 		more_pages = ALIGN(new_end - phys_end, PAGE_SIZE) >> PAGE_SHIFT;
424 		while (nr < more_pages) {
425 			unsigned long allocated;
426 
427 			allocated = alloc_pages_bulk_node(GFP_KERNEL | __GFP_NOWARN,
428 				NUMA_NO_NODE, more_pages - nr, next_page + nr);
429 
430 			if (!allocated)
431 				break;
432 			nr += allocated;
433 		}
434 
435 		if (nr < more_pages ||
436 		    vmap_pages_range(phys_end, phys_end + (nr << PAGE_SHIFT), PAGE_KERNEL,
437 				     next_page, PAGE_SHIFT) < 0) {
438 			/* Clean up and error out */
439 			for (int i = 0; i < nr; i++)
440 				__free_page(next_page[i]);
441 			return -ENOMEM;
442 		}
443 
444 		vm_module_tags->nr_pages += nr;
445 
446 		/*
447 		 * Kasan allocates 1 byte of shadow for every 8 bytes of data.
448 		 * When kasan_alloc_module_shadow allocates shadow memory,
449 		 * its unit of allocation is a page.
450 		 * Therefore, here we need to align to MODULE_ALIGN.
451 		 */
452 		if (old_shadow_end < new_shadow_end)
453 			kasan_alloc_module_shadow((void *)old_shadow_end,
454 						  new_shadow_end - old_shadow_end,
455 						  GFP_KERNEL);
456 	}
457 
458 	/*
459 	 * Mark the pages as accessible, now that they are mapped.
460 	 * With hardware tag-based KASAN, marking is skipped for
461 	 * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc().
462 	 */
463 	kasan_unpoison_vmalloc((void *)module_tags.start_addr,
464 				new_end - module_tags.start_addr,
465 				KASAN_VMALLOC_PROT_NORMAL);
466 
467 	return 0;
468 }
469 
470 static void *reserve_module_tags(struct module *mod, unsigned long size,
471 				 unsigned int prepend, unsigned long align)
472 {
473 	unsigned long section_size = module_tags.end_addr - module_tags.start_addr;
474 	MA_STATE(mas, &mod_area_mt, 0, section_size - 1);
475 	unsigned long offset;
476 	void *ret = NULL;
477 
478 	/* If no tags return error */
479 	if (size < sizeof(struct alloc_tag))
480 		return ERR_PTR(-EINVAL);
481 
482 	/*
483 	 * align is always power of 2, so we can use IS_ALIGNED and ALIGN.
484 	 * align 0 or 1 means no alignment, to simplify set to 1.
485 	 */
486 	if (!align)
487 		align = 1;
488 
489 	if (!ensure_alignment(align, &prepend)) {
490 		shutdown_mem_profiling(true);
491 		pr_err("%s: alignment %lu is incompatible with allocation tag indexing. Memory allocation profiling is disabled!\n",
492 			mod->name, align);
493 		return ERR_PTR(-EINVAL);
494 	}
495 
496 	mas_lock(&mas);
497 	if (!find_aligned_area(&mas, section_size, size, prepend, align)) {
498 		ret = ERR_PTR(-ENOMEM);
499 		goto unlock;
500 	}
501 
502 	/* Mark found area as reserved */
503 	offset = mas.index;
504 	offset += prepend;
505 	offset = ALIGN(offset, align);
506 	if (offset != mas.index) {
507 		unsigned long pad_start = mas.index;
508 
509 		mas.last = offset - 1;
510 		mas_store(&mas, &prepend_mod);
511 		if (mas_is_err(&mas)) {
512 			ret = ERR_PTR(xa_err(mas.node));
513 			goto unlock;
514 		}
515 		mas.index = offset;
516 		mas.last = offset + size - 1;
517 		mas_store(&mas, mod);
518 		if (mas_is_err(&mas)) {
519 			mas.index = pad_start;
520 			mas_erase(&mas);
521 			ret = ERR_PTR(xa_err(mas.node));
522 		}
523 	} else {
524 		mas.last = offset + size - 1;
525 		mas_store(&mas, mod);
526 		if (mas_is_err(&mas))
527 			ret = ERR_PTR(xa_err(mas.node));
528 	}
529 unlock:
530 	mas_unlock(&mas);
531 
532 	if (IS_ERR(ret))
533 		return ret;
534 
535 	if (module_tags.size < offset + size) {
536 		int grow_res;
537 
538 		module_tags.size = offset + size;
539 		if (mem_alloc_profiling_enabled() && !tags_addressable()) {
540 			shutdown_mem_profiling(true);
541 			pr_warn("With module %s there are too many tags to fit in %d page flag bits. Memory allocation profiling is disabled!\n",
542 				mod->name, NR_UNUSED_PAGEFLAG_BITS);
543 		}
544 
545 		grow_res = vm_module_tags_populate();
546 		if (grow_res) {
547 			shutdown_mem_profiling(true);
548 			pr_err("Failed to allocate memory for allocation tags in the module %s. Memory allocation profiling is disabled!\n",
549 			       mod->name);
550 			return ERR_PTR(grow_res);
551 		}
552 	}
553 
554 	return (struct alloc_tag *)(module_tags.start_addr + offset);
555 }
556 
557 static void release_module_tags(struct module *mod, bool used)
558 {
559 	MA_STATE(mas, &mod_area_mt, module_tags.size, module_tags.size);
560 	struct alloc_tag *start_tag;
561 	struct alloc_tag *end_tag;
562 	struct module *val;
563 
564 	mas_lock(&mas);
565 	mas_for_each_rev(&mas, val, 0)
566 		if (val == mod)
567 			break;
568 
569 	if (!val) /* module not found */
570 		goto out;
571 
572 	if (!used)
573 		goto release_area;
574 
575 	start_tag = (struct alloc_tag *)(module_tags.start_addr + mas.index);
576 	end_tag = (struct alloc_tag *)(module_tags.start_addr + mas.last);
577 	if (!clean_unused_counters(start_tag, end_tag)) {
578 		struct alloc_tag *tag;
579 
580 		for (tag = start_tag; tag <= end_tag; tag++) {
581 			struct alloc_tag_counters counter;
582 
583 			if (!tag->counters)
584 				continue;
585 
586 			counter = alloc_tag_read(tag);
587 			pr_info("%s:%u module %s func:%s has %llu allocated at module unload\n",
588 				tag->ct.filename, tag->ct.lineno, tag->ct.modname,
589 				tag->ct.function, counter.bytes);
590 		}
591 	} else {
592 		used = false;
593 	}
594 release_area:
595 	mas_store(&mas, used ? &unloaded_mod : NULL);
596 	val = mas_prev_range(&mas, 0);
597 	if (val == &prepend_mod)
598 		mas_store(&mas, NULL);
599 out:
600 	mas_unlock(&mas);
601 }
602 
603 static int load_module(struct module *mod, struct codetag *start, struct codetag *stop)
604 {
605 	/* Allocate module alloc_tag percpu counters */
606 	struct alloc_tag *start_tag;
607 	struct alloc_tag *stop_tag;
608 	struct alloc_tag *tag;
609 
610 	/* percpu counters for core allocations are already statically allocated */
611 	if (!mod)
612 		return 0;
613 
614 	start_tag = ct_to_alloc_tag(start);
615 	stop_tag = ct_to_alloc_tag(stop);
616 	for (tag = start_tag; tag < stop_tag; tag++) {
617 		WARN_ON(tag->counters);
618 		tag->counters = alloc_percpu(struct alloc_tag_counters);
619 		if (!tag->counters) {
620 			while (--tag >= start_tag) {
621 				free_percpu(tag->counters);
622 				tag->counters = NULL;
623 			}
624 			pr_err("Failed to allocate memory for allocation tag percpu counters in the module %s\n",
625 			       mod->name);
626 			return -ENOMEM;
627 		}
628 
629 		/*
630 		 * Avoid a kmemleak false positive. The pointer to the counters is stored
631 		 * in the alloc_tag section of the module and cannot be directly accessed.
632 		 */
633 		kmemleak_ignore_percpu(tag->counters);
634 	}
635 	return 0;
636 }
637 
638 static void replace_module(struct module *mod, struct module *new_mod)
639 {
640 	MA_STATE(mas, &mod_area_mt, 0, module_tags.size);
641 	struct module *val;
642 
643 	mas_lock(&mas);
644 	mas_for_each(&mas, val, module_tags.size) {
645 		if (val != mod)
646 			continue;
647 
648 		mas_store_gfp(&mas, new_mod, GFP_KERNEL);
649 		break;
650 	}
651 	mas_unlock(&mas);
652 }
653 
654 static int __init alloc_mod_tags_mem(void)
655 {
656 	/* Map space to copy allocation tags */
657 	vm_module_tags = execmem_vmap(MODULE_ALLOC_TAG_VMAP_SIZE);
658 	if (!vm_module_tags) {
659 		pr_err("Failed to map %lu bytes for module allocation tags\n",
660 			MODULE_ALLOC_TAG_VMAP_SIZE);
661 		module_tags.start_addr = 0;
662 		return -ENOMEM;
663 	}
664 
665 	vm_module_tags->pages = kmalloc_array(get_vm_area_size(vm_module_tags) >> PAGE_SHIFT,
666 					sizeof(struct page *), GFP_KERNEL | __GFP_ZERO);
667 	if (!vm_module_tags->pages) {
668 		free_vm_area(vm_module_tags);
669 		return -ENOMEM;
670 	}
671 
672 	module_tags.start_addr = (unsigned long)vm_module_tags->addr;
673 	module_tags.end_addr = module_tags.start_addr + MODULE_ALLOC_TAG_VMAP_SIZE;
674 	/* Ensure the base is alloc_tag aligned when required for indexing */
675 	module_tags.start_addr = alloc_tag_align(module_tags.start_addr);
676 
677 	return 0;
678 }
679 
680 static void __init free_mod_tags_mem(void)
681 {
682 	int i;
683 
684 	module_tags.start_addr = 0;
685 	for (i = 0; i < vm_module_tags->nr_pages; i++)
686 		__free_page(vm_module_tags->pages[i]);
687 	kfree(vm_module_tags->pages);
688 	free_vm_area(vm_module_tags);
689 }
690 
691 #else /* CONFIG_MODULES */
692 
693 static inline int alloc_mod_tags_mem(void) { return 0; }
694 static inline void free_mod_tags_mem(void) {}
695 
696 #endif /* CONFIG_MODULES */
697 
698 /* See: Documentation/mm/allocation-profiling.rst */
699 static int __init setup_early_mem_profiling(char *str)
700 {
701 	bool compressed = false;
702 	bool enable;
703 
704 	if (!str || !str[0])
705 		return -EINVAL;
706 
707 	if (!strncmp(str, "never", 5)) {
708 		enable = false;
709 		mem_profiling_support = false;
710 		pr_info("Memory allocation profiling is disabled!\n");
711 	} else {
712 		char *token = strsep(&str, ",");
713 
714 		if (kstrtobool(token, &enable))
715 			return -EINVAL;
716 
717 		if (str) {
718 
719 			if (strcmp(str, "compressed"))
720 				return -EINVAL;
721 
722 			compressed = true;
723 		}
724 		mem_profiling_support = true;
725 		pr_info("Memory allocation profiling is enabled %s compression and is turned %s!\n",
726 			compressed ? "with" : "without", enable ? "on" : "off");
727 	}
728 
729 	if (enable != mem_alloc_profiling_enabled()) {
730 		if (enable)
731 			static_branch_enable(&mem_alloc_profiling_key);
732 		else
733 			static_branch_disable(&mem_alloc_profiling_key);
734 	}
735 	if (compressed != static_key_enabled(&mem_profiling_compressed)) {
736 		if (compressed)
737 			static_branch_enable(&mem_profiling_compressed);
738 		else
739 			static_branch_disable(&mem_profiling_compressed);
740 	}
741 
742 	return 0;
743 }
744 early_param("sysctl.vm.mem_profiling", setup_early_mem_profiling);
745 
746 static __init bool need_page_alloc_tagging(void)
747 {
748 	if (static_key_enabled(&mem_profiling_compressed))
749 		return false;
750 
751 	return mem_profiling_support;
752 }
753 
754 static __init void init_page_alloc_tagging(void)
755 {
756 }
757 
758 struct page_ext_operations page_alloc_tagging_ops = {
759 	.size = sizeof(union codetag_ref),
760 	.need = need_page_alloc_tagging,
761 	.init = init_page_alloc_tagging,
762 };
763 EXPORT_SYMBOL(page_alloc_tagging_ops);
764 
765 #ifdef CONFIG_SYSCTL
766 static struct ctl_table memory_allocation_profiling_sysctls[] = {
767 	{
768 		.procname	= "mem_profiling",
769 		.data		= &mem_alloc_profiling_key,
770 #ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG
771 		.mode		= 0444,
772 #else
773 		.mode		= 0644,
774 #endif
775 		.proc_handler	= proc_do_static_key,
776 	},
777 };
778 
779 static void __init sysctl_init(void)
780 {
781 	if (!mem_profiling_support)
782 		memory_allocation_profiling_sysctls[0].mode = 0444;
783 
784 	register_sysctl_init("vm", memory_allocation_profiling_sysctls);
785 }
786 #else /* CONFIG_SYSCTL */
787 static inline void sysctl_init(void) {}
788 #endif /* CONFIG_SYSCTL */
789 
790 static int __init alloc_tag_init(void)
791 {
792 	const struct codetag_type_desc desc = {
793 		.section		= ALLOC_TAG_SECTION_NAME,
794 		.tag_size		= sizeof(struct alloc_tag),
795 #ifdef CONFIG_MODULES
796 		.needs_section_mem	= needs_section_mem,
797 		.alloc_section_mem	= reserve_module_tags,
798 		.free_section_mem	= release_module_tags,
799 		.module_load		= load_module,
800 		.module_replaced	= replace_module,
801 #endif
802 	};
803 	int res;
804 
805 	sysctl_init();
806 
807 	if (!mem_profiling_support) {
808 		pr_info("Memory allocation profiling is not supported!\n");
809 		return 0;
810 	}
811 
812 	if (!proc_create_seq_private(ALLOCINFO_FILE_NAME, 0400, NULL, &allocinfo_seq_op,
813 				     sizeof(struct allocinfo_private), NULL)) {
814 		pr_err("Failed to create %s file\n", ALLOCINFO_FILE_NAME);
815 		shutdown_mem_profiling(false);
816 		return -ENOMEM;
817 	}
818 
819 	res = alloc_mod_tags_mem();
820 	if (res) {
821 		pr_err("Failed to reserve address space for module tags, errno = %d\n", res);
822 		shutdown_mem_profiling(true);
823 		return res;
824 	}
825 
826 	alloc_tag_cttype = codetag_register_type(&desc);
827 	if (IS_ERR(alloc_tag_cttype)) {
828 		pr_err("Allocation tags registration failed, errno = %ld\n", PTR_ERR(alloc_tag_cttype));
829 		free_mod_tags_mem();
830 		shutdown_mem_profiling(true);
831 		return PTR_ERR(alloc_tag_cttype);
832 	}
833 
834 	return 0;
835 }
836 module_init(alloc_tag_init);
837