Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 1 | #ifndef _LINUX_PAGE_REF_H |
| 2 | #define _LINUX_PAGE_REF_H |
| 3 | |
| 4 | #include <linux/atomic.h> |
| 5 | #include <linux/mm_types.h> |
| 6 | #include <linux/page-flags.h> |
Joonsoo Kim | 95813b8 | 2016-03-17 14:19:29 -0700 | [diff] [blame] | 7 | #include <linux/tracepoint-defs.h> |
| 8 | |
| 9 | extern struct tracepoint __tracepoint_page_ref_set; |
| 10 | extern struct tracepoint __tracepoint_page_ref_mod; |
| 11 | extern struct tracepoint __tracepoint_page_ref_mod_and_test; |
| 12 | extern struct tracepoint __tracepoint_page_ref_mod_and_return; |
| 13 | extern struct tracepoint __tracepoint_page_ref_mod_unless; |
| 14 | extern struct tracepoint __tracepoint_page_ref_freeze; |
| 15 | extern struct tracepoint __tracepoint_page_ref_unfreeze; |
| 16 | |
| 17 | #ifdef CONFIG_DEBUG_PAGE_REF |
| 18 | |
| 19 | /* |
| 20 | * Ideally we would want to use the trace_<tracepoint>_enabled() helper |
| 21 | * functions. But due to include header file issues, that is not |
| 22 | * feasible. Instead we have to open code the static key functions. |
| 23 | * |
| 24 | * See trace_##name##_enabled(void) in include/linux/tracepoint.h |
| 25 | */ |
| 26 | #define page_ref_tracepoint_active(t) static_key_false(&(t).key) |
| 27 | |
| 28 | extern void __page_ref_set(struct page *page, int v); |
| 29 | extern void __page_ref_mod(struct page *page, int v); |
| 30 | extern void __page_ref_mod_and_test(struct page *page, int v, int ret); |
| 31 | extern void __page_ref_mod_and_return(struct page *page, int v, int ret); |
| 32 | extern void __page_ref_mod_unless(struct page *page, int v, int u); |
| 33 | extern void __page_ref_freeze(struct page *page, int v, int ret); |
| 34 | extern void __page_ref_unfreeze(struct page *page, int v); |
| 35 | |
| 36 | #else |
| 37 | |
| 38 | #define page_ref_tracepoint_active(t) false |
| 39 | |
| 40 | static inline void __page_ref_set(struct page *page, int v) |
| 41 | { |
| 42 | } |
| 43 | static inline void __page_ref_mod(struct page *page, int v) |
| 44 | { |
| 45 | } |
| 46 | static inline void __page_ref_mod_and_test(struct page *page, int v, int ret) |
| 47 | { |
| 48 | } |
| 49 | static inline void __page_ref_mod_and_return(struct page *page, int v, int ret) |
| 50 | { |
| 51 | } |
| 52 | static inline void __page_ref_mod_unless(struct page *page, int v, int u) |
| 53 | { |
| 54 | } |
| 55 | static inline void __page_ref_freeze(struct page *page, int v, int ret) |
| 56 | { |
| 57 | } |
| 58 | static inline void __page_ref_unfreeze(struct page *page, int v) |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | #endif |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 63 | |
| 64 | static inline int page_ref_count(struct page *page) |
| 65 | { |
Joonsoo Kim | 0139aa7 | 2016-05-19 17:10:49 -0700 | [diff] [blame] | 66 | return atomic_read(&page->_refcount); |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static inline int page_count(struct page *page) |
| 70 | { |
Joonsoo Kim | 0139aa7 | 2016-05-19 17:10:49 -0700 | [diff] [blame] | 71 | return atomic_read(&compound_head(page)->_refcount); |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static inline void set_page_count(struct page *page, int v) |
| 75 | { |
Joonsoo Kim | 0139aa7 | 2016-05-19 17:10:49 -0700 | [diff] [blame] | 76 | atomic_set(&page->_refcount, v); |
Joonsoo Kim | 95813b8 | 2016-03-17 14:19:29 -0700 | [diff] [blame] | 77 | if (page_ref_tracepoint_active(__tracepoint_page_ref_set)) |
| 78 | __page_ref_set(page, v); |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Setup the page count before being freed into the page allocator for |
| 83 | * the first time (boot or memory hotplug) |
| 84 | */ |
| 85 | static inline void init_page_count(struct page *page) |
| 86 | { |
| 87 | set_page_count(page, 1); |
| 88 | } |
| 89 | |
| 90 | static inline void page_ref_add(struct page *page, int nr) |
| 91 | { |
Joonsoo Kim | 0139aa7 | 2016-05-19 17:10:49 -0700 | [diff] [blame] | 92 | atomic_add(nr, &page->_refcount); |
Joonsoo Kim | 95813b8 | 2016-03-17 14:19:29 -0700 | [diff] [blame] | 93 | if (page_ref_tracepoint_active(__tracepoint_page_ref_mod)) |
| 94 | __page_ref_mod(page, nr); |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static inline void page_ref_sub(struct page *page, int nr) |
| 98 | { |
Joonsoo Kim | 0139aa7 | 2016-05-19 17:10:49 -0700 | [diff] [blame] | 99 | atomic_sub(nr, &page->_refcount); |
Joonsoo Kim | 95813b8 | 2016-03-17 14:19:29 -0700 | [diff] [blame] | 100 | if (page_ref_tracepoint_active(__tracepoint_page_ref_mod)) |
| 101 | __page_ref_mod(page, -nr); |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static inline void page_ref_inc(struct page *page) |
| 105 | { |
Joonsoo Kim | 0139aa7 | 2016-05-19 17:10:49 -0700 | [diff] [blame] | 106 | atomic_inc(&page->_refcount); |
Joonsoo Kim | 95813b8 | 2016-03-17 14:19:29 -0700 | [diff] [blame] | 107 | if (page_ref_tracepoint_active(__tracepoint_page_ref_mod)) |
| 108 | __page_ref_mod(page, 1); |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | static inline void page_ref_dec(struct page *page) |
| 112 | { |
Joonsoo Kim | 0139aa7 | 2016-05-19 17:10:49 -0700 | [diff] [blame] | 113 | atomic_dec(&page->_refcount); |
Joonsoo Kim | 95813b8 | 2016-03-17 14:19:29 -0700 | [diff] [blame] | 114 | if (page_ref_tracepoint_active(__tracepoint_page_ref_mod)) |
| 115 | __page_ref_mod(page, -1); |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static inline int page_ref_sub_and_test(struct page *page, int nr) |
| 119 | { |
Joonsoo Kim | 0139aa7 | 2016-05-19 17:10:49 -0700 | [diff] [blame] | 120 | int ret = atomic_sub_and_test(nr, &page->_refcount); |
Joonsoo Kim | 95813b8 | 2016-03-17 14:19:29 -0700 | [diff] [blame] | 121 | |
| 122 | if (page_ref_tracepoint_active(__tracepoint_page_ref_mod_and_test)) |
| 123 | __page_ref_mod_and_test(page, -nr, ret); |
| 124 | return ret; |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static inline int page_ref_dec_and_test(struct page *page) |
| 128 | { |
Joonsoo Kim | 0139aa7 | 2016-05-19 17:10:49 -0700 | [diff] [blame] | 129 | int ret = atomic_dec_and_test(&page->_refcount); |
Joonsoo Kim | 95813b8 | 2016-03-17 14:19:29 -0700 | [diff] [blame] | 130 | |
| 131 | if (page_ref_tracepoint_active(__tracepoint_page_ref_mod_and_test)) |
| 132 | __page_ref_mod_and_test(page, -1, ret); |
| 133 | return ret; |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static inline int page_ref_dec_return(struct page *page) |
| 137 | { |
Joonsoo Kim | 0139aa7 | 2016-05-19 17:10:49 -0700 | [diff] [blame] | 138 | int ret = atomic_dec_return(&page->_refcount); |
Joonsoo Kim | 95813b8 | 2016-03-17 14:19:29 -0700 | [diff] [blame] | 139 | |
| 140 | if (page_ref_tracepoint_active(__tracepoint_page_ref_mod_and_return)) |
| 141 | __page_ref_mod_and_return(page, -1, ret); |
| 142 | return ret; |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static inline int page_ref_add_unless(struct page *page, int nr, int u) |
| 146 | { |
Joonsoo Kim | 0139aa7 | 2016-05-19 17:10:49 -0700 | [diff] [blame] | 147 | int ret = atomic_add_unless(&page->_refcount, nr, u); |
Joonsoo Kim | 95813b8 | 2016-03-17 14:19:29 -0700 | [diff] [blame] | 148 | |
| 149 | if (page_ref_tracepoint_active(__tracepoint_page_ref_mod_unless)) |
| 150 | __page_ref_mod_unless(page, nr, ret); |
| 151 | return ret; |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static inline int page_ref_freeze(struct page *page, int count) |
| 155 | { |
Joonsoo Kim | 0139aa7 | 2016-05-19 17:10:49 -0700 | [diff] [blame] | 156 | int ret = likely(atomic_cmpxchg(&page->_refcount, count, 0) == count); |
Joonsoo Kim | 95813b8 | 2016-03-17 14:19:29 -0700 | [diff] [blame] | 157 | |
| 158 | if (page_ref_tracepoint_active(__tracepoint_page_ref_freeze)) |
| 159 | __page_ref_freeze(page, count, ret); |
| 160 | return ret; |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static inline void page_ref_unfreeze(struct page *page, int count) |
| 164 | { |
| 165 | VM_BUG_ON_PAGE(page_count(page) != 0, page); |
| 166 | VM_BUG_ON(count == 0); |
| 167 | |
Joonsoo Kim | 0139aa7 | 2016-05-19 17:10:49 -0700 | [diff] [blame] | 168 | atomic_set(&page->_refcount, count); |
Joonsoo Kim | 95813b8 | 2016-03-17 14:19:29 -0700 | [diff] [blame] | 169 | if (page_ref_tracepoint_active(__tracepoint_page_ref_unfreeze)) |
| 170 | __page_ref_unfreeze(page, count); |
Joonsoo Kim | fe896d1 | 2016-03-17 14:19:26 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | #endif |