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