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 #else /* CONFIG_MEM_ALLOC_PROFILING */ 71 72 static inline void pgalloc_tag_add(struct page *page, struct task_struct *task, 73 unsigned int nr) {} 74 static inline void pgalloc_tag_sub(struct page *page, unsigned int nr) {} 75 76 #endif /* CONFIG_MEM_ALLOC_PROFILING */ 77 78 #endif /* _LINUX_PGALLOC_TAG_H */ 79