xref: /linux-6.15/include/linux/pgalloc_tag.h (revision d224eb02)
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 static inline struct alloc_tag *pgalloc_tag_get(struct page *page)
100 {
101 	struct alloc_tag *tag = NULL;
102 
103 	if (mem_alloc_profiling_enabled()) {
104 		union codetag_ref *ref = get_page_tag_ref(page);
105 
106 		alloc_tag_sub_check(ref);
107 		if (ref && ref->ct)
108 			tag = ct_to_alloc_tag(ref->ct);
109 		put_page_tag_ref(ref);
110 	}
111 
112 	return tag;
113 }
114 
115 static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr)
116 {
117 	if (mem_alloc_profiling_enabled() && tag)
118 		this_cpu_sub(tag->counters->bytes, PAGE_SIZE * nr);
119 }
120 
121 #else /* CONFIG_MEM_ALLOC_PROFILING */
122 
123 static inline union codetag_ref *get_page_tag_ref(struct page *page) { return NULL; }
124 static inline void put_page_tag_ref(union codetag_ref *ref) {}
125 static inline void pgalloc_tag_add(struct page *page, struct task_struct *task,
126 				   unsigned int nr) {}
127 static inline void pgalloc_tag_sub(struct page *page, unsigned int nr) {}
128 static inline void pgalloc_tag_split(struct page *page, unsigned int nr) {}
129 static inline struct alloc_tag *pgalloc_tag_get(struct page *page) { return NULL; }
130 static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr) {}
131 
132 #endif /* CONFIG_MEM_ALLOC_PROFILING */
133 
134 #endif /* _LINUX_PGALLOC_TAG_H */
135