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 (void *)page_ext + page_alloc_tagging_ops.offset;
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 pgalloc_tag_add(struct page *page, struct task_struct *task,
47 unsigned int nr)
48{
49 if (mem_alloc_profiling_enabled()) {
50 union codetag_ref *ref = get_page_tag_ref(page);
51
52 if (ref) {
53 alloc_tag_add(ref, task->alloc_tag, PAGE_SIZE * nr);
54 put_page_tag_ref(ref);
55 }
56 }
57}
58
59static inline void pgalloc_tag_sub(struct page *page, 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_sub(ref, PAGE_SIZE * nr);
66 put_page_tag_ref(ref);
67 }
68 }
69}
70
71static inline void pgalloc_tag_split(struct page *page, unsigned int nr)
72{
73 int i;
74 struct page_ext *page_ext;
75 union codetag_ref *ref;
76 struct alloc_tag *tag;
77
78 if (!mem_alloc_profiling_enabled())
79 return;
80
81 page_ext = page_ext_get(page);
82 if (unlikely(!page_ext))
83 return;
84
85 ref = codetag_ref_from_page_ext(page_ext);
86 if (!ref->ct)
87 goto out;
88
89 tag = ct_to_alloc_tag(ref->ct);
90 page_ext = page_ext_next(page_ext);
91 for (i = 1; i < nr; i++) {
92 /* Set new reference to point to the original tag */
93 alloc_tag_ref_set(codetag_ref_from_page_ext(page_ext), tag);
94 page_ext = page_ext_next(page_ext);
95 }
96out:
97 page_ext_put(page_ext);
98}
99
100static inline struct alloc_tag *pgalloc_tag_get(struct page *page)
101{
102 struct alloc_tag *tag = NULL;
103
104 if (mem_alloc_profiling_enabled()) {
105 union codetag_ref *ref = get_page_tag_ref(page);
106
107 alloc_tag_sub_check(ref);
108 if (ref) {
109 if (ref->ct)
110 tag = ct_to_alloc_tag(ref->ct);
111 put_page_tag_ref(ref);
112 }
113 }
114
115 return tag;
116}
117
118static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr)
119{
120 if (mem_alloc_profiling_enabled() && tag)
121 this_cpu_sub(tag->counters->bytes, PAGE_SIZE * nr);
122}
123
124#else /* CONFIG_MEM_ALLOC_PROFILING */
125
126static inline union codetag_ref *get_page_tag_ref(struct page *page) { return NULL; }
127static inline void put_page_tag_ref(union codetag_ref *ref) {}
128static inline void pgalloc_tag_add(struct page *page, struct task_struct *task,
129 unsigned int nr) {}
130static inline void pgalloc_tag_sub(struct page *page, unsigned int nr) {}
131static inline void pgalloc_tag_split(struct page *page, unsigned int nr) {}
132static inline struct alloc_tag *pgalloc_tag_get(struct page *page) { return NULL; }
133static inline void pgalloc_tag_sub_pages(struct alloc_tag *tag, unsigned int nr) {}
134
135#endif /* CONFIG_MEM_ALLOC_PROFILING */
136
137#endif /* _LINUX_PGALLOC_TAG_H */