xref: /linux-6.15/include/linux/pgalloc_tag.h (revision be25d1d4)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * page allocation tagging
4  */
5 #ifndef _LINUX_PGALLOC_TAG_H
6 #define _LINUX_PGALLOC_TAG_H
7 
8 #include <linux/alloc_tag.h>
9 
10 #ifdef CONFIG_MEM_ALLOC_PROFILING
11 
12 #include <linux/page_ext.h>
13 
14 extern struct page_ext_operations page_alloc_tagging_ops;
15 extern struct page_ext *page_ext_get(struct page *page);
16 extern void page_ext_put(struct page_ext *page_ext);
17 
18 static inline union codetag_ref *codetag_ref_from_page_ext(struct page_ext *page_ext)
19 {
20 	return (void *)page_ext + page_alloc_tagging_ops.offset;
21 }
22 
23 static inline struct page_ext *page_ext_from_codetag_ref(union codetag_ref *ref)
24 {
25 	return (void *)ref - page_alloc_tagging_ops.offset;
26 }
27 
28 /* Should be called only if mem_alloc_profiling_enabled() */
29 static inline union codetag_ref *get_page_tag_ref(struct page *page)
30 {
31 	if (page) {
32 		struct page_ext *page_ext = page_ext_get(page);
33 
34 		if (page_ext)
35 			return codetag_ref_from_page_ext(page_ext);
36 	}
37 	return NULL;
38 }
39 
40 static inline void put_page_tag_ref(union codetag_ref *ref)
41 {
42 	page_ext_put(page_ext_from_codetag_ref(ref));
43 }
44 
45 static inline void pgalloc_tag_add(struct page *page, struct task_struct *task,
46 				   unsigned int nr)
47 {
48 	if (mem_alloc_profiling_enabled()) {
49 		union codetag_ref *ref = get_page_tag_ref(page);
50 
51 		if (ref) {
52 			alloc_tag_add(ref, task->alloc_tag, PAGE_SIZE * nr);
53 			put_page_tag_ref(ref);
54 		}
55 	}
56 }
57 
58 static inline void pgalloc_tag_sub(struct page *page, unsigned int nr)
59 {
60 	if (mem_alloc_profiling_enabled()) {
61 		union codetag_ref *ref = get_page_tag_ref(page);
62 
63 		if (ref) {
64 			alloc_tag_sub(ref, PAGE_SIZE * nr);
65 			put_page_tag_ref(ref);
66 		}
67 	}
68 }
69 
70 static inline void pgalloc_tag_split(struct page *page, unsigned int nr)
71 {
72 	int i;
73 	struct page_ext *page_ext;
74 	union codetag_ref *ref;
75 	struct alloc_tag *tag;
76 
77 	if (!mem_alloc_profiling_enabled())
78 		return;
79 
80 	page_ext = page_ext_get(page);
81 	if (unlikely(!page_ext))
82 		return;
83 
84 	ref = codetag_ref_from_page_ext(page_ext);
85 	if (!ref->ct)
86 		goto out;
87 
88 	tag = ct_to_alloc_tag(ref->ct);
89 	page_ext = page_ext_next(page_ext);
90 	for (i = 1; i < nr; i++) {
91 		/* Set new reference to point to the original tag */
92 		alloc_tag_ref_set(codetag_ref_from_page_ext(page_ext), tag);
93 		page_ext = page_ext_next(page_ext);
94 	}
95 out:
96 	page_ext_put(page_ext);
97 }
98 
99 #else /* CONFIG_MEM_ALLOC_PROFILING */
100 
101 static inline void pgalloc_tag_add(struct page *page, struct task_struct *task,
102 				   unsigned int nr) {}
103 static inline void pgalloc_tag_sub(struct page *page, unsigned int nr) {}
104 static inline void pgalloc_tag_split(struct page *page, unsigned int nr) {}
105 
106 #endif /* CONFIG_MEM_ALLOC_PROFILING */
107 
108 #endif /* _LINUX_PGALLOC_TAG_H */
109