Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | #ifndef _LINUX_PERCPU_COUNTER_H |
| 3 | #define _LINUX_PERCPU_COUNTER_H |
| 4 | /* |
| 5 | * A simple "approximate counter" for use in ext2 and ext3 superblocks. |
| 6 | * |
| 7 | * WARNING: these things are HUGE. 4 kbytes per counter on 32-way P4. |
| 8 | */ |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/spinlock.h> |
| 11 | #include <linux/smp.h> |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 12 | #include <linux/list.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/threads.h> |
| 14 | #include <linux/percpu.h> |
Mingming Cao | 0216bfc | 2006-06-23 02:05:41 -0700 | [diff] [blame] | 15 | #include <linux/types.h> |
Tejun Heo | 908c7f1 | 2014-09-08 09:51:29 +0900 | [diff] [blame] | 16 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | #ifdef CONFIG_SMP |
| 19 | |
| 20 | struct percpu_counter { |
Thomas Gleixner | f032a45 | 2009-07-25 16:21:48 +0200 | [diff] [blame] | 21 | raw_spinlock_t lock; |
Mingming Cao | 0216bfc | 2006-06-23 02:05:41 -0700 | [diff] [blame] | 22 | s64 count; |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 23 | #ifdef CONFIG_HOTPLUG_CPU |
| 24 | struct list_head list; /* All percpu_counters are on a list */ |
| 25 | #endif |
Tejun Heo | 43cf38e | 2010-02-02 14:38:57 +0900 | [diff] [blame] | 26 | s32 __percpu *counters; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | }; |
| 28 | |
Eric Dumazet | 179f7eb | 2009-01-06 14:41:04 -0800 | [diff] [blame] | 29 | extern int percpu_counter_batch; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Tejun Heo | 908c7f1 | 2014-09-08 09:51:29 +0900 | [diff] [blame] | 31 | int __percpu_counter_init(struct percpu_counter *fbc, s64 amount, gfp_t gfp, |
Peter Zijlstra | ea31951 | 2008-12-26 15:08:55 +0100 | [diff] [blame] | 32 | struct lock_class_key *key); |
| 33 | |
Tejun Heo | 908c7f1 | 2014-09-08 09:51:29 +0900 | [diff] [blame] | 34 | #define percpu_counter_init(fbc, value, gfp) \ |
Peter Zijlstra | ea31951 | 2008-12-26 15:08:55 +0100 | [diff] [blame] | 35 | ({ \ |
| 36 | static struct lock_class_key __key; \ |
| 37 | \ |
Tejun Heo | 908c7f1 | 2014-09-08 09:51:29 +0900 | [diff] [blame] | 38 | __percpu_counter_init(fbc, value, gfp, &__key); \ |
Peter Zijlstra | ea31951 | 2008-12-26 15:08:55 +0100 | [diff] [blame] | 39 | }) |
| 40 | |
Andrew Morton | c67ad91 | 2007-07-15 23:39:51 -0700 | [diff] [blame] | 41 | void percpu_counter_destroy(struct percpu_counter *fbc); |
Peter Zijlstra | 3a587f4 | 2007-10-16 23:25:44 -0700 | [diff] [blame] | 42 | void percpu_counter_set(struct percpu_counter *fbc, s64 amount); |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 43 | void percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount, |
| 44 | s32 batch); |
Andrew Morton | 02d211688 | 2008-12-09 13:14:14 -0800 | [diff] [blame] | 45 | s64 __percpu_counter_sum(struct percpu_counter *fbc); |
Dave Chinner | 80188b0 | 2015-05-29 07:39:34 +1000 | [diff] [blame] | 46 | int __percpu_counter_compare(struct percpu_counter *fbc, s64 rhs, s32 batch); |
| 47 | |
| 48 | static inline int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs) |
| 49 | { |
| 50 | return __percpu_counter_compare(fbc, rhs, percpu_counter_batch); |
| 51 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Peter Zijlstra | 20e8976 | 2007-10-16 23:25:43 -0700 | [diff] [blame] | 53 | static inline void percpu_counter_add(struct percpu_counter *fbc, s64 amount) |
Peter Zijlstra | 252e0ba | 2007-10-16 23:25:43 -0700 | [diff] [blame] | 54 | { |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 55 | percpu_counter_add_batch(fbc, amount, percpu_counter_batch); |
Peter Zijlstra | 252e0ba | 2007-10-16 23:25:43 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Peter Zijlstra | bf1d89c | 2007-10-16 23:25:45 -0700 | [diff] [blame] | 58 | static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc) |
| 59 | { |
Andrew Morton | 02d211688 | 2008-12-09 13:14:14 -0800 | [diff] [blame] | 60 | s64 ret = __percpu_counter_sum(fbc); |
Peter Zijlstra | bf1d89c | 2007-10-16 23:25:45 -0700 | [diff] [blame] | 61 | return ret < 0 ? 0 : ret; |
| 62 | } |
| 63 | |
| 64 | static inline s64 percpu_counter_sum(struct percpu_counter *fbc) |
| 65 | { |
Andrew Morton | 02d211688 | 2008-12-09 13:14:14 -0800 | [diff] [blame] | 66 | return __percpu_counter_sum(fbc); |
Peter Zijlstra | bf1d89c | 2007-10-16 23:25:45 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Mingming Cao | 0216bfc | 2006-06-23 02:05:41 -0700 | [diff] [blame] | 69 | static inline s64 percpu_counter_read(struct percpu_counter *fbc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
| 71 | return fbc->count; |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * It is possible for the percpu_counter_read() to return a small negative |
| 76 | * number for some counter which should never be negative. |
Mingming Cao | 0216bfc | 2006-06-23 02:05:41 -0700 | [diff] [blame] | 77 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | */ |
Mingming Cao | 0216bfc | 2006-06-23 02:05:41 -0700 | [diff] [blame] | 79 | static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { |
Mingming Cao | 0216bfc | 2006-06-23 02:05:41 -0700 | [diff] [blame] | 81 | s64 ret = fbc->count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | barrier(); /* Prevent reloads of fbc->count */ |
Mingming Cao | 0216bfc | 2006-06-23 02:05:41 -0700 | [diff] [blame] | 84 | if (ret >= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | return ret; |
Shaohua Li | c84598b | 2011-05-24 17:13:35 -0700 | [diff] [blame] | 86 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | |
guoyayun | 85dcbba | 2017-12-15 14:48:32 +0800 | [diff] [blame] | 89 | static inline bool percpu_counter_initialized(struct percpu_counter *fbc) |
Theodore Ts'o | 7f93cff | 2010-10-27 21:30:13 -0400 | [diff] [blame] | 90 | { |
| 91 | return (fbc->counters != NULL); |
| 92 | } |
| 93 | |
Jesper Dangaard Brouer | 7fa4cf9 | 2013-02-04 23:14:12 +0100 | [diff] [blame] | 94 | #else /* !CONFIG_SMP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | struct percpu_counter { |
Mingming Cao | 0216bfc | 2006-06-23 02:05:41 -0700 | [diff] [blame] | 97 | s64 count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
Tejun Heo | 908c7f1 | 2014-09-08 09:51:29 +0900 | [diff] [blame] | 100 | static inline int percpu_counter_init(struct percpu_counter *fbc, s64 amount, |
| 101 | gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | { |
Mingming Cao | 0216bfc | 2006-06-23 02:05:41 -0700 | [diff] [blame] | 103 | fbc->count = amount; |
Peter Zijlstra | 833f407 | 2007-10-16 23:25:45 -0700 | [diff] [blame] | 104 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static inline void percpu_counter_destroy(struct percpu_counter *fbc) |
| 108 | { |
| 109 | } |
| 110 | |
Peter Zijlstra | 3a587f4 | 2007-10-16 23:25:44 -0700 | [diff] [blame] | 111 | static inline void percpu_counter_set(struct percpu_counter *fbc, s64 amount) |
| 112 | { |
| 113 | fbc->count = amount; |
| 114 | } |
| 115 | |
Tim Chen | 27f5e0f | 2010-08-09 17:19:04 -0700 | [diff] [blame] | 116 | static inline int percpu_counter_compare(struct percpu_counter *fbc, s64 rhs) |
| 117 | { |
| 118 | if (fbc->count > rhs) |
| 119 | return 1; |
| 120 | else if (fbc->count < rhs) |
| 121 | return -1; |
| 122 | else |
| 123 | return 0; |
| 124 | } |
| 125 | |
Dave Chinner | 80188b0 | 2015-05-29 07:39:34 +1000 | [diff] [blame] | 126 | static inline int |
| 127 | __percpu_counter_compare(struct percpu_counter *fbc, s64 rhs, s32 batch) |
| 128 | { |
| 129 | return percpu_counter_compare(fbc, rhs); |
| 130 | } |
| 131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | static inline void |
Peter Zijlstra | 20e8976 | 2007-10-16 23:25:43 -0700 | [diff] [blame] | 133 | percpu_counter_add(struct percpu_counter *fbc, s64 amount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
| 135 | preempt_disable(); |
| 136 | fbc->count += amount; |
| 137 | preempt_enable(); |
| 138 | } |
| 139 | |
Anton Blanchard | 0c9cf2e | 2010-02-02 14:46:10 -0800 | [diff] [blame] | 140 | static inline void |
Nikolay Borisov | 104b4e5 | 2017-06-20 21:01:20 +0300 | [diff] [blame] | 141 | percpu_counter_add_batch(struct percpu_counter *fbc, s64 amount, s32 batch) |
Anton Blanchard | 0c9cf2e | 2010-02-02 14:46:10 -0800 | [diff] [blame] | 142 | { |
| 143 | percpu_counter_add(fbc, amount); |
| 144 | } |
| 145 | |
Mingming Cao | 0216bfc | 2006-06-23 02:05:41 -0700 | [diff] [blame] | 146 | static inline s64 percpu_counter_read(struct percpu_counter *fbc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
| 148 | return fbc->count; |
| 149 | } |
| 150 | |
Shaohua Li | c84598b | 2011-05-24 17:13:35 -0700 | [diff] [blame] | 151 | /* |
| 152 | * percpu_counter is intended to track positive numbers. In the UP case the |
| 153 | * number should never be negative. |
| 154 | */ |
Mingming Cao | 0216bfc | 2006-06-23 02:05:41 -0700 | [diff] [blame] | 155 | static inline s64 percpu_counter_read_positive(struct percpu_counter *fbc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
| 157 | return fbc->count; |
| 158 | } |
| 159 | |
Peter Zijlstra | 52d9f3b | 2007-10-16 23:25:44 -0700 | [diff] [blame] | 160 | static inline s64 percpu_counter_sum_positive(struct percpu_counter *fbc) |
Andrew Morton | e2bab3d | 2006-03-07 21:55:31 -0800 | [diff] [blame] | 161 | { |
| 162 | return percpu_counter_read_positive(fbc); |
| 163 | } |
| 164 | |
Peter Zijlstra | bf1d89c | 2007-10-16 23:25:45 -0700 | [diff] [blame] | 165 | static inline s64 percpu_counter_sum(struct percpu_counter *fbc) |
| 166 | { |
| 167 | return percpu_counter_read(fbc); |
| 168 | } |
| 169 | |
guoyayun | 85dcbba | 2017-12-15 14:48:32 +0800 | [diff] [blame] | 170 | static inline bool percpu_counter_initialized(struct percpu_counter *fbc) |
Theodore Ts'o | 7f93cff | 2010-10-27 21:30:13 -0400 | [diff] [blame] | 171 | { |
guoyayun | 85dcbba | 2017-12-15 14:48:32 +0800 | [diff] [blame] | 172 | return true; |
Theodore Ts'o | 7f93cff | 2010-10-27 21:30:13 -0400 | [diff] [blame] | 173 | } |
| 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | #endif /* CONFIG_SMP */ |
| 176 | |
| 177 | static inline void percpu_counter_inc(struct percpu_counter *fbc) |
| 178 | { |
Peter Zijlstra | aa0dff2 | 2007-10-16 23:25:42 -0700 | [diff] [blame] | 179 | percpu_counter_add(fbc, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static inline void percpu_counter_dec(struct percpu_counter *fbc) |
| 183 | { |
Peter Zijlstra | aa0dff2 | 2007-10-16 23:25:42 -0700 | [diff] [blame] | 184 | percpu_counter_add(fbc, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Peter Zijlstra | 3cb4f9f | 2007-10-16 23:25:42 -0700 | [diff] [blame] | 187 | static inline void percpu_counter_sub(struct percpu_counter *fbc, s64 amount) |
| 188 | { |
| 189 | percpu_counter_add(fbc, -amount); |
| 190 | } |
| 191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | #endif /* _LINUX_PERCPU_COUNTER_H */ |