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