1 /* SPDX-License-Identifier: GPL-2.0 */
3 * page allocation tagging
5 #ifndef _LINUX_PGALLOC_TAG_H
6 #define _LINUX_PGALLOC_TAG_H
8 #include <linux/alloc_tag.h>
10 #ifdef CONFIG_MEM_ALLOC_PROFILING
12 #include <linux/page_ext.h>
14 extern struct page_ext_operations page_alloc_tagging_ops;
16 static inline union codetag_ref *codetag_ref_from_page_ext(struct page_ext *page_ext)
18 return (void *)page_ext + page_alloc_tagging_ops.offset;
21 static inline struct page_ext *page_ext_from_codetag_ref(union codetag_ref *ref)
23 return (void *)ref - page_alloc_tagging_ops.offset;
26 /* Should be called only if mem_alloc_profiling_enabled() */
27 static inline union codetag_ref *get_page_tag_ref(struct page *page)
30 struct page_ext *page_ext = page_ext_get(page);
33 return codetag_ref_from_page_ext(page_ext);
38 static inline void put_page_tag_ref(union codetag_ref *ref)
40 page_ext_put(page_ext_from_codetag_ref(ref));
43 static inline void pgalloc_tag_add(struct page *page, struct task_struct *task,
46 if (mem_alloc_profiling_enabled()) {
47 union codetag_ref *ref = get_page_tag_ref(page);
50 alloc_tag_add(ref, task->alloc_tag, PAGE_SIZE * nr);
51 put_page_tag_ref(ref);
56 static inline void pgalloc_tag_sub(struct page *page, unsigned int nr)
58 if (mem_alloc_profiling_enabled()) {
59 union codetag_ref *ref = get_page_tag_ref(page);
62 alloc_tag_sub(ref, PAGE_SIZE * nr);
63 put_page_tag_ref(ref);
68 static inline void pgalloc_tag_split(struct page *page, unsigned int nr)
71 struct page_ext *page_ext;
72 union codetag_ref *ref;
73 struct alloc_tag *tag;
75 if (!mem_alloc_profiling_enabled())
78 page_ext = page_ext_get(page);
79 if (unlikely(!page_ext))
82 ref = codetag_ref_from_page_ext(page_ext);
86 tag = ct_to_alloc_tag(ref->ct);
87 page_ext = page_ext_next(page_ext);
88 for (i = 1; i < nr; i++) {
89 /* Set new reference to point to the original tag */
90 alloc_tag_ref_set(codetag_ref_from_page_ext(page_ext), tag);
91 page_ext = page_ext_next(page_ext);
94 page_ext_put(page_ext);
97 static inline struct alloc_tag *pgalloc_tag_get(struct page *page)
99 struct alloc_tag *tag = NULL;
101 if (mem_alloc_profiling_enabled()) {
102 union codetag_ref *ref = get_page_tag_ref(page);
104 alloc_tag_sub_check(ref);
106 tag = ct_to_alloc_tag(ref->ct);
107 put_page_tag_ref(ref);
113 static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr)
115 if (mem_alloc_profiling_enabled() && tag)
116 this_cpu_sub(tag->counters->bytes, PAGE_SIZE * nr);
119 #else /* CONFIG_MEM_ALLOC_PROFILING */
121 static inline union codetag_ref *get_page_tag_ref(struct page *page) { return NULL; }
122 static inline void put_page_tag_ref(union codetag_ref *ref) {}
123 static inline void pgalloc_tag_add(struct page *page, struct task_struct *task,
125 static inline void pgalloc_tag_sub(struct page *page, unsigned int nr) {}
126 static inline void pgalloc_tag_split(struct page *page, unsigned int nr) {}
127 static inline struct alloc_tag *pgalloc_tag_get(struct page *page) { return NULL; }
128 static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr) {}
130 #endif /* CONFIG_MEM_ALLOC_PROFILING */
132 #endif /* _LINUX_PGALLOC_TAG_H */