Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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
14extern struct page_ext_operations page_alloc_tagging_ops;
15
16static inline union codetag_ref *codetag_ref_from_page_ext(struct page_ext *page_ext)
17{
18 return (union codetag_ref *)page_ext_data(page_ext, &page_alloc_tagging_ops);
19}
20
21static inline struct page_ext *page_ext_from_codetag_ref(union codetag_ref *ref)
22{
23 return (void *)ref - page_alloc_tagging_ops.offset;
24}
25
26/* Should be called only if mem_alloc_profiling_enabled() */
27static inline union codetag_ref *get_page_tag_ref(struct page *page)
28{
29 if (page) {
30 struct page_ext *page_ext = page_ext_get(page);
31
32 if (page_ext)
33 return codetag_ref_from_page_ext(page_ext);
34 }
35 return NULL;
36}
37
38static inline void put_page_tag_ref(union codetag_ref *ref)
39{
40 if (WARN_ON(!ref))
41 return;
42
43 page_ext_put(page_ext_from_codetag_ref(ref));
44}
45
46static inline void clear_page_tag_ref(struct page *page)
47{
48 if (mem_alloc_profiling_enabled()) {
49 union codetag_ref *ref = get_page_tag_ref(page);
50
51 if (ref) {
52 set_codetag_empty(ref);
53 put_page_tag_ref(ref);
54 }
55 }
56}
57
58static inline void pgalloc_tag_add(struct page *page, struct task_struct *task,
59 unsigned int nr)
60{
61 if (mem_alloc_profiling_enabled()) {
62 union codetag_ref *ref = get_page_tag_ref(page);
63
64 if (ref) {
65 alloc_tag_add(ref, task->alloc_tag, PAGE_SIZE * nr);
66 put_page_tag_ref(ref);
67 }
68 }
69}
70
71static inline void pgalloc_tag_sub(struct page *page, unsigned int nr)
72{
73 if (mem_alloc_profiling_enabled()) {
74 union codetag_ref *ref = get_page_tag_ref(page);
75
76 if (ref) {
77 alloc_tag_sub(ref, PAGE_SIZE * nr);
78 put_page_tag_ref(ref);
79 }
80 }
81}
82
83static inline void pgalloc_tag_split(struct page *page, unsigned int nr)
84{
85 int i;
86 struct page_ext *first_page_ext;
87 struct page_ext *page_ext;
88 union codetag_ref *ref;
89 struct alloc_tag *tag;
90
91 if (!mem_alloc_profiling_enabled())
92 return;
93
94 first_page_ext = page_ext = page_ext_get(page);
95 if (unlikely(!page_ext))
96 return;
97
98 ref = codetag_ref_from_page_ext(page_ext);
99 if (!ref->ct)
100 goto out;
101
102 tag = ct_to_alloc_tag(ref->ct);
103 page_ext = page_ext_next(page_ext);
104 for (i = 1; i < nr; i++) {
105 /* Set new reference to point to the original tag */
106 alloc_tag_ref_set(codetag_ref_from_page_ext(page_ext), tag);
107 page_ext = page_ext_next(page_ext);
108 }
109out:
110 page_ext_put(first_page_ext);
111}
112
113static inline struct alloc_tag *pgalloc_tag_get(struct page *page)
114{
115 struct alloc_tag *tag = NULL;
116
117 if (mem_alloc_profiling_enabled()) {
118 union codetag_ref *ref = get_page_tag_ref(page);
119
120 alloc_tag_sub_check(ref);
121 if (ref) {
122 if (ref->ct)
123 tag = ct_to_alloc_tag(ref->ct);
124 put_page_tag_ref(ref);
125 }
126 }
127
128 return tag;
129}
130
131static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr)
132{
133 if (mem_alloc_profiling_enabled() && tag)
134 this_cpu_sub(tag->counters->bytes, PAGE_SIZE * nr);
135}
136
137#else /* CONFIG_MEM_ALLOC_PROFILING */
138
139static inline union codetag_ref *get_page_tag_ref(struct page *page) { return NULL; }
140static inline void put_page_tag_ref(union codetag_ref *ref) {}
141static inline void clear_page_tag_ref(struct page *page) {}
142static inline void pgalloc_tag_add(struct page *page, struct task_struct *task,
143 unsigned int nr) {}
144static inline void pgalloc_tag_sub(struct page *page, unsigned int nr) {}
145static inline void pgalloc_tag_split(struct page *page, unsigned int nr) {}
146static inline struct alloc_tag *pgalloc_tag_get(struct page *page) { return NULL; }
147static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr) {}
148
149#endif /* CONFIG_MEM_ALLOC_PROFILING */
150
151#endif /* _LINUX_PGALLOC_TAG_H */