hc
2024-05-10 23fa18eaa71266feff7ba8d83022d9e1cc83c65a
kernel/mm/swap_slots.c
....@@ -33,6 +33,7 @@
3333 #include <linux/vmalloc.h>
3434 #include <linux/mutex.h>
3535 #include <linux/mm.h>
36
+#include <trace/hooks/mm.h>
3637
3738 static DEFINE_PER_CPU(struct swap_slots_cache, swp_slots);
3839 static bool swap_slot_cache_active;
....@@ -46,8 +47,7 @@
4647 static void deactivate_swap_slots_cache(void);
4748 static void reactivate_swap_slots_cache(void);
4849
49
-#define use_swap_slot_cache (swap_slot_cache_active && \
50
- swap_slot_cache_enabled && swap_slot_cache_initialized)
50
+#define use_swap_slot_cache (swap_slot_cache_active && swap_slot_cache_enabled)
5151 #define SLOTS_CACHE 0x1
5252 #define SLOTS_CACHE_RET 0x2
5353
....@@ -55,6 +55,7 @@
5555 {
5656 mutex_lock(&swap_slots_cache_mutex);
5757 swap_slot_cache_active = false;
58
+ trace_android_vh_swap_slot_cache_active(false);
5859 __drain_swap_slots_cache(SLOTS_CACHE|SLOTS_CACHE_RET);
5960 mutex_unlock(&swap_slots_cache_mutex);
6061 }
....@@ -63,6 +64,7 @@
6364 {
6465 mutex_lock(&swap_slots_cache_mutex);
6566 swap_slot_cache_active = true;
67
+ trace_android_vh_swap_slot_cache_active(true);
6668 mutex_unlock(&swap_slots_cache_mutex);
6769 }
6870
....@@ -90,11 +92,17 @@
9092 mutex_unlock(&swap_slots_cache_enable_mutex);
9193 }
9294
93
-static bool check_cache_active(void)
95
+bool is_swap_slot_cache_enabled(void)
96
+{
97
+ return swap_slot_cache_enabled;
98
+}
99
+EXPORT_SYMBOL_GPL(is_swap_slot_cache_enabled);
100
+
101
+bool check_cache_active(void)
94102 {
95103 long pages;
96104
97
- if (!swap_slot_cache_enabled || !swap_slot_cache_initialized)
105
+ if (!swap_slot_cache_enabled)
98106 return false;
99107
100108 pages = get_nr_swap_pages();
....@@ -111,17 +119,26 @@
111119 out:
112120 return swap_slot_cache_active;
113121 }
122
+EXPORT_SYMBOL_GPL(check_cache_active);
114123
115124 static int alloc_swap_slot_cache(unsigned int cpu)
116125 {
117126 struct swap_slots_cache *cache;
118127 swp_entry_t *slots, *slots_ret;
128
+ bool skip = false;
129
+ int ret = 0;
119130
120131 /*
121132 * Do allocation outside swap_slots_cache_mutex
122133 * as kvzalloc could trigger reclaim and get_swap_page,
123134 * which can lock swap_slots_cache_mutex.
124135 */
136
+ trace_android_rvh_alloc_swap_slot_cache(&per_cpu(swp_slots, cpu),
137
+ &ret, &skip);
138
+ trace_android_vh_alloc_swap_slot_cache(&per_cpu(swp_slots, cpu),
139
+ &ret, &skip);
140
+ if (skip)
141
+ return ret;
125142 slots = kvcalloc(SWAP_SLOTS_CACHE_SIZE, sizeof(swp_entry_t),
126143 GFP_KERNEL);
127144 if (!slots)
....@@ -136,9 +153,16 @@
136153
137154 mutex_lock(&swap_slots_cache_mutex);
138155 cache = &per_cpu(swp_slots, cpu);
139
- if (cache->slots || cache->slots_ret)
156
+ if (cache->slots || cache->slots_ret) {
140157 /* cache already allocated */
141
- goto out;
158
+ mutex_unlock(&swap_slots_cache_mutex);
159
+
160
+ kvfree(slots);
161
+ kvfree(slots_ret);
162
+
163
+ return 0;
164
+ }
165
+
142166 if (!cache->lock_initialized) {
143167 mutex_init(&cache->alloc_lock);
144168 spin_lock_init(&cache->free_lock);
....@@ -155,15 +179,8 @@
155179 */
156180 mb();
157181 cache->slots = slots;
158
- slots = NULL;
159182 cache->slots_ret = slots_ret;
160
- slots_ret = NULL;
161
-out:
162183 mutex_unlock(&swap_slots_cache_mutex);
163
- if (slots)
164
- kvfree(slots);
165
- if (slots_ret)
166
- kvfree(slots_ret);
167184 return 0;
168185 }
169186
....@@ -172,8 +189,15 @@
172189 {
173190 struct swap_slots_cache *cache;
174191 swp_entry_t *slots = NULL;
192
+ bool skip = false;
175193
176194 cache = &per_cpu(swp_slots, cpu);
195
+ trace_android_rvh_drain_slots_cache_cpu(cache, type,
196
+ free_slots, &skip);
197
+ trace_android_vh_drain_slots_cache_cpu(cache, type,
198
+ free_slots, &skip);
199
+ if (skip)
200
+ return;
177201 if ((type & SLOTS_CACHE) && cache->slots) {
178202 mutex_lock(&cache->alloc_lock);
179203 swapcache_free_entries(cache->slots + cache->cur, cache->nr);
....@@ -238,27 +262,24 @@
238262 return 0;
239263 }
240264
241
-int enable_swap_slots_cache(void)
265
+void enable_swap_slots_cache(void)
242266 {
243
- int ret = 0;
244
-
245267 mutex_lock(&swap_slots_cache_enable_mutex);
246
- if (swap_slot_cache_initialized) {
247
- __reenable_swap_slots_cache();
248
- goto out_unlock;
268
+ if (!swap_slot_cache_initialized) {
269
+ int ret;
270
+
271
+ ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "swap_slots_cache",
272
+ alloc_swap_slot_cache, free_slot_cache);
273
+ if (WARN_ONCE(ret < 0, "Cache allocation failed (%s), operating "
274
+ "without swap slots cache.\n", __func__))
275
+ goto out_unlock;
276
+
277
+ swap_slot_cache_initialized = true;
249278 }
250279
251
- ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "swap_slots_cache",
252
- alloc_swap_slot_cache, free_slot_cache);
253
- if (WARN_ONCE(ret < 0, "Cache allocation failed (%s), operating "
254
- "without swap slots cache.\n", __func__))
255
- goto out_unlock;
256
-
257
- swap_slot_cache_initialized = true;
258280 __reenable_swap_slots_cache();
259281 out_unlock:
260282 mutex_unlock(&swap_slots_cache_enable_mutex);
261
- return 0;
262283 }
263284
264285 /* called with swap slot cache's alloc lock held */
....@@ -278,8 +299,13 @@
278299 int free_swap_slot(swp_entry_t entry)
279300 {
280301 struct swap_slots_cache *cache;
302
+ bool skip = false;
281303
282304 cache = raw_cpu_ptr(&swp_slots);
305
+ trace_android_rvh_free_swap_slot(entry, cache, &skip);
306
+ trace_android_vh_free_swap_slot(entry, cache, &skip);
307
+ if (skip)
308
+ return 0;
283309 if (likely(use_swap_slot_cache && cache->slots_ret)) {
284310 spin_lock_irq(&cache->free_lock);
285311 /* Swap slots cache may be deactivated before acquiring lock */
....@@ -309,10 +335,15 @@
309335
310336 swp_entry_t get_swap_page(struct page *page)
311337 {
312
- swp_entry_t entry, *pentry;
338
+ swp_entry_t entry;
313339 struct swap_slots_cache *cache;
314
-
340
+ bool found = false;
315341 entry.val = 0;
342
+
343
+ trace_android_rvh_get_swap_page(page, &entry, raw_cpu_ptr(&swp_slots), &found);
344
+ trace_android_vh_get_swap_page(page, &entry, raw_cpu_ptr(&swp_slots), &found);
345
+ if (found)
346
+ goto out;
316347
317348 if (PageTransHuge(page)) {
318349 if (IS_ENABLED(CONFIG_THP_SWAP))
....@@ -336,13 +367,11 @@
336367 if (cache->slots) {
337368 repeat:
338369 if (cache->nr) {
339
- pentry = &cache->slots[cache->cur++];
340
- entry = *pentry;
341
- pentry->val = 0;
370
+ entry = cache->slots[cache->cur];
371
+ cache->slots[cache->cur++].val = 0;
342372 cache->nr--;
343
- } else {
344
- if (refill_swap_slots_cache(cache))
345
- goto repeat;
373
+ } else if (refill_swap_slots_cache(cache)) {
374
+ goto repeat;
346375 }
347376 }
348377 mutex_unlock(&cache->alloc_lock);