Blame view

mm/frontswap.c 14.2 KB
7a338472f   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
29f233cff   Dan Magenheimer   mm: frontswap: co...
2
3
4
5
6
  /*
   * Frontswap frontend
   *
   * This code provides the generic "frontend" layer to call a matching
   * "backend" driver implementation of frontswap.  See
ad56b738c   Mike Rapoport   docs/vm: rename d...
7
   * Documentation/vm/frontswap.rst for more information.
29f233cff   Dan Magenheimer   mm: frontswap: co...
8
9
10
   *
   * Copyright (C) 2009-2012 Oracle Corp.  All rights reserved.
   * Author: Dan Magenheimer
29f233cff   Dan Magenheimer   mm: frontswap: co...
11
   */
29f233cff   Dan Magenheimer   mm: frontswap: co...
12
13
14
  #include <linux/mman.h>
  #include <linux/swap.h>
  #include <linux/swapops.h>
29f233cff   Dan Magenheimer   mm: frontswap: co...
15
  #include <linux/security.h>
29f233cff   Dan Magenheimer   mm: frontswap: co...
16
  #include <linux/module.h>
29f233cff   Dan Magenheimer   mm: frontswap: co...
17
18
19
  #include <linux/debugfs.h>
  #include <linux/frontswap.h>
  #include <linux/swapfile.h>
8ea1d2a19   Vlastimil Babka   mm, frontswap: co...
20
  DEFINE_STATIC_KEY_FALSE(frontswap_enabled_key);
29f233cff   Dan Magenheimer   mm: frontswap: co...
21
  /*
d1dc6f1bc   Dan Streetman   frontswap: allow ...
22
23
24
25
   * frontswap_ops are added by frontswap_register_ops, and provide the
   * frontswap "backend" implementation functions.  Multiple implementations
   * may be registered, but implementations can never deregister.  This
   * is a simple singly-linked list of all registered implementations.
29f233cff   Dan Magenheimer   mm: frontswap: co...
26
   */
1e01c968d   Konrad Rzeszutek Wilk   frontswap: make f...
27
  static struct frontswap_ops *frontswap_ops __read_mostly;
29f233cff   Dan Magenheimer   mm: frontswap: co...
28

d1dc6f1bc   Dan Streetman   frontswap: allow ...
29
30
  #define for_each_frontswap_ops(ops)		\
  	for ((ops) = frontswap_ops; (ops); (ops) = (ops)->next)
29f233cff   Dan Magenheimer   mm: frontswap: co...
31
  /*
165c8aed5   Konrad Rzeszutek Wilk   frontswap: s/put_...
32
   * If enabled, frontswap_store will return failure even on success.  As
29f233cff   Dan Magenheimer   mm: frontswap: co...
33
34
35
36
37
38
39
   * a result, the swap subsystem will always write the page to swap, in
   * effect converting frontswap into a writethrough cache.  In this mode,
   * there is no direct reduction in swap writes, but a frontswap backend
   * can unilaterally "reclaim" any pages in use with no data loss, thus
   * providing increases control over maximum memory usage due to frontswap.
   */
  static bool frontswap_writethrough_enabled __read_mostly;
e3483a5f3   Dan Magenheimer   frontswap: suppor...
40
41
42
43
44
45
  /*
   * If enabled, the underlying tmem implementation is capable of doing
   * exclusive gets, so frontswap_load, on a successful tmem_get must
   * mark the page as no longer in frontswap AND mark it dirty.
   */
  static bool frontswap_tmem_exclusive_gets_enabled __read_mostly;
29f233cff   Dan Magenheimer   mm: frontswap: co...
46
47
48
49
50
51
  #ifdef CONFIG_DEBUG_FS
  /*
   * Counters available via /sys/kernel/debug/frontswap (if debugfs is
   * properly configured).  These are for information only so are not protected
   * against increment races.
   */
165c8aed5   Konrad Rzeszutek Wilk   frontswap: s/put_...
52
53
54
  static u64 frontswap_loads;
  static u64 frontswap_succ_stores;
  static u64 frontswap_failed_stores;
29f233cff   Dan Magenheimer   mm: frontswap: co...
55
  static u64 frontswap_invalidates;
165c8aed5   Konrad Rzeszutek Wilk   frontswap: s/put_...
56
  static inline void inc_frontswap_loads(void) {
96bdd2bcc   Qian Cai   mm/frontswap: mar...
57
  	data_race(frontswap_loads++);
29f233cff   Dan Magenheimer   mm: frontswap: co...
58
  }
165c8aed5   Konrad Rzeszutek Wilk   frontswap: s/put_...
59
  static inline void inc_frontswap_succ_stores(void) {
96bdd2bcc   Qian Cai   mm/frontswap: mar...
60
  	data_race(frontswap_succ_stores++);
29f233cff   Dan Magenheimer   mm: frontswap: co...
61
  }
165c8aed5   Konrad Rzeszutek Wilk   frontswap: s/put_...
62
  static inline void inc_frontswap_failed_stores(void) {
96bdd2bcc   Qian Cai   mm/frontswap: mar...
63
  	data_race(frontswap_failed_stores++);
29f233cff   Dan Magenheimer   mm: frontswap: co...
64
65
  }
  static inline void inc_frontswap_invalidates(void) {
96bdd2bcc   Qian Cai   mm/frontswap: mar...
66
  	data_race(frontswap_invalidates++);
29f233cff   Dan Magenheimer   mm: frontswap: co...
67
68
  }
  #else
165c8aed5   Konrad Rzeszutek Wilk   frontswap: s/put_...
69
70
71
  static inline void inc_frontswap_loads(void) { }
  static inline void inc_frontswap_succ_stores(void) { }
  static inline void inc_frontswap_failed_stores(void) { }
29f233cff   Dan Magenheimer   mm: frontswap: co...
72
73
  static inline void inc_frontswap_invalidates(void) { }
  #endif
905cd0e1b   Dan Magenheimer   mm: frontswap: la...
74
75
76
77
78
79
80
  
  /*
   * Due to the asynchronous nature of the backends loading potentially
   * _after_ the swap system has been activated, we have chokepoints
   * on all frontswap functions to not call the backend until the backend
   * has registered.
   *
905cd0e1b   Dan Magenheimer   mm: frontswap: la...
81
82
   * This would not guards us against the user deciding to call swapoff right as
   * we are calling the backend to initialize (so swapon is in action).
404f3ecfd   Ethon Paul   mm/frontswap: fix...
83
   * Fortunately for us, the swapon_mutex has been taken by the callee so we are
905cd0e1b   Dan Magenheimer   mm: frontswap: la...
84
85
86
87
88
89
90
91
92
   * OK. The other scenario where calls to frontswap_store (called via
   * swap_writepage) is racing with frontswap_invalidate_area (called via
   * swapoff) is again guarded by the swap subsystem.
   *
   * While no backend is registered all calls to frontswap_[store|load|
   * invalidate_area|invalidate_page] are ignored or fail.
   *
   * The time between the backend being registered and the swap file system
   * calling the backend (via the frontswap_* functions) is indeterminate as
1e01c968d   Konrad Rzeszutek Wilk   frontswap: make f...
93
   * frontswap_ops is not atomic_t (or a value guarded by a spinlock).
905cd0e1b   Dan Magenheimer   mm: frontswap: la...
94
95
96
97
98
   * That is OK as we are comfortable missing some of these calls to the newly
   * registered backend.
   *
   * Obviously the opposite (unloading the backend) must be done after all
   * the frontswap_[store|load|invalidate_area|invalidate_page] start
d1dc6f1bc   Dan Streetman   frontswap: allow ...
99
100
   * ignoring or failing the requests.  However, there is currently no way
   * to unload a backend once it is registered.
905cd0e1b   Dan Magenheimer   mm: frontswap: la...
101
   */
905cd0e1b   Dan Magenheimer   mm: frontswap: la...
102

29f233cff   Dan Magenheimer   mm: frontswap: co...
103
  /*
d1dc6f1bc   Dan Streetman   frontswap: allow ...
104
   * Register operations for frontswap
29f233cff   Dan Magenheimer   mm: frontswap: co...
105
   */
d1dc6f1bc   Dan Streetman   frontswap: allow ...
106
  void frontswap_register_ops(struct frontswap_ops *ops)
29f233cff   Dan Magenheimer   mm: frontswap: co...
107
  {
d1dc6f1bc   Dan Streetman   frontswap: allow ...
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  	DECLARE_BITMAP(a, MAX_SWAPFILES);
  	DECLARE_BITMAP(b, MAX_SWAPFILES);
  	struct swap_info_struct *si;
  	unsigned int i;
  
  	bitmap_zero(a, MAX_SWAPFILES);
  	bitmap_zero(b, MAX_SWAPFILES);
  
  	spin_lock(&swap_lock);
  	plist_for_each_entry(si, &swap_active_head, list) {
  		if (!WARN_ON(!si->frontswap_map))
  			set_bit(si->type, a);
  	}
  	spin_unlock(&swap_lock);
  
  	/* the new ops needs to know the currently active swap devices */
  	for_each_set_bit(i, a, MAX_SWAPFILES)
  		ops->init(i);
  
  	/*
  	 * Setting frontswap_ops must happen after the ops->init() calls
  	 * above; cmpxchg implies smp_mb() which will ensure the init is
  	 * complete at this point.
  	 */
  	do {
  		ops->next = frontswap_ops;
  	} while (cmpxchg(&frontswap_ops, ops->next, ops) != ops->next);
8ea1d2a19   Vlastimil Babka   mm, frontswap: co...
135
  	static_branch_inc(&frontswap_enabled_key);
d1dc6f1bc   Dan Streetman   frontswap: allow ...
136
137
138
139
  	spin_lock(&swap_lock);
  	plist_for_each_entry(si, &swap_active_head, list) {
  		if (si->frontswap_map)
  			set_bit(si->type, b);
905cd0e1b   Dan Magenheimer   mm: frontswap: la...
140
  	}
d1dc6f1bc   Dan Streetman   frontswap: allow ...
141
  	spin_unlock(&swap_lock);
905cd0e1b   Dan Magenheimer   mm: frontswap: la...
142
  	/*
d1dc6f1bc   Dan Streetman   frontswap: allow ...
143
144
145
146
  	 * On the very unlikely chance that a swap device was added or
  	 * removed between setting the "a" list bits and the ops init
  	 * calls, we re-check and do init or invalidate for any changed
  	 * bits.
905cd0e1b   Dan Magenheimer   mm: frontswap: la...
147
  	 */
d1dc6f1bc   Dan Streetman   frontswap: allow ...
148
149
150
151
152
153
154
155
  	if (unlikely(!bitmap_equal(a, b, MAX_SWAPFILES))) {
  		for (i = 0; i < MAX_SWAPFILES; i++) {
  			if (!test_bit(i, a) && test_bit(i, b))
  				ops->init(i);
  			else if (test_bit(i, a) && !test_bit(i, b))
  				ops->invalidate_area(i);
  		}
  	}
29f233cff   Dan Magenheimer   mm: frontswap: co...
156
157
158
159
160
161
162
163
164
165
166
167
168
  }
  EXPORT_SYMBOL(frontswap_register_ops);
  
  /*
   * Enable/disable frontswap writethrough (see above).
   */
  void frontswap_writethrough(bool enable)
  {
  	frontswap_writethrough_enabled = enable;
  }
  EXPORT_SYMBOL(frontswap_writethrough);
  
  /*
e3483a5f3   Dan Magenheimer   frontswap: suppor...
169
170
171
172
173
174
175
176
177
   * Enable/disable frontswap exclusive gets (see above).
   */
  void frontswap_tmem_exclusive_gets(bool enable)
  {
  	frontswap_tmem_exclusive_gets_enabled = enable;
  }
  EXPORT_SYMBOL(frontswap_tmem_exclusive_gets);
  
  /*
29f233cff   Dan Magenheimer   mm: frontswap: co...
178
179
   * Called when a swap device is swapon'd.
   */
4f89849da   Minchan Kim   frontswap: get ri...
180
  void __frontswap_init(unsigned type, unsigned long *map)
29f233cff   Dan Magenheimer   mm: frontswap: co...
181
182
  {
  	struct swap_info_struct *sis = swap_info[type];
d1dc6f1bc   Dan Streetman   frontswap: allow ...
183
  	struct frontswap_ops *ops;
29f233cff   Dan Magenheimer   mm: frontswap: co...
184

8ea1d2a19   Vlastimil Babka   mm, frontswap: co...
185
  	VM_BUG_ON(sis == NULL);
4f89849da   Minchan Kim   frontswap: get ri...
186
187
188
189
190
191
192
193
194
195
196
197
198
  
  	/*
  	 * p->frontswap is a bitmap that we MUST have to figure out which page
  	 * has gone in frontswap. Without it there is no point of continuing.
  	 */
  	if (WARN_ON(!map))
  		return;
  	/*
  	 * Irregardless of whether the frontswap backend has been loaded
  	 * before this function or it will be later, we _MUST_ have the
  	 * p->frontswap set to something valid to work properly.
  	 */
  	frontswap_map_set(sis, map);
d1dc6f1bc   Dan Streetman   frontswap: allow ...
199
200
201
  
  	for_each_frontswap_ops(ops)
  		ops->init(type);
29f233cff   Dan Magenheimer   mm: frontswap: co...
202
203
  }
  EXPORT_SYMBOL(__frontswap_init);
f066ea230   Bob Liu   mm: frontswap: cl...
204
205
206
  bool __frontswap_test(struct swap_info_struct *sis,
  				pgoff_t offset)
  {
d1dc6f1bc   Dan Streetman   frontswap: allow ...
207
208
209
  	if (sis->frontswap_map)
  		return test_bit(offset, sis->frontswap_map);
  	return false;
f066ea230   Bob Liu   mm: frontswap: cl...
210
211
  }
  EXPORT_SYMBOL(__frontswap_test);
d1dc6f1bc   Dan Streetman   frontswap: allow ...
212
213
214
215
216
217
  static inline void __frontswap_set(struct swap_info_struct *sis,
  				   pgoff_t offset)
  {
  	set_bit(offset, sis->frontswap_map);
  	atomic_inc(&sis->frontswap_pages);
  }
f066ea230   Bob Liu   mm: frontswap: cl...
218
  static inline void __frontswap_clear(struct swap_info_struct *sis,
d1dc6f1bc   Dan Streetman   frontswap: allow ...
219
  				     pgoff_t offset)
611edfed2   Sasha Levin   mm: frontswap: sp...
220
  {
f066ea230   Bob Liu   mm: frontswap: cl...
221
  	clear_bit(offset, sis->frontswap_map);
611edfed2   Sasha Levin   mm: frontswap: sp...
222
223
  	atomic_dec(&sis->frontswap_pages);
  }
29f233cff   Dan Magenheimer   mm: frontswap: co...
224
  /*
165c8aed5   Konrad Rzeszutek Wilk   frontswap: s/put_...
225
   * "Store" data from a page to frontswap and associate it with the page's
29f233cff   Dan Magenheimer   mm: frontswap: co...
226
227
   * swaptype and offset.  Page must be locked and in the swap cache.
   * If frontswap already contains a page with matching swaptype and
1d00015e2   Wanpeng Li   mm/frontswap: cle...
228
   * offset, the frontswap implementation may either overwrite the data and
29f233cff   Dan Magenheimer   mm: frontswap: co...
229
230
   * return success or invalidate the page from frontswap and return failure.
   */
165c8aed5   Konrad Rzeszutek Wilk   frontswap: s/put_...
231
  int __frontswap_store(struct page *page)
29f233cff   Dan Magenheimer   mm: frontswap: co...
232
  {
d1dc6f1bc   Dan Streetman   frontswap: allow ...
233
  	int ret = -1;
29f233cff   Dan Magenheimer   mm: frontswap: co...
234
235
236
237
  	swp_entry_t entry = { .val = page_private(page), };
  	int type = swp_type(entry);
  	struct swap_info_struct *sis = swap_info[type];
  	pgoff_t offset = swp_offset(entry);
d1dc6f1bc   Dan Streetman   frontswap: allow ...
238
  	struct frontswap_ops *ops;
29f233cff   Dan Magenheimer   mm: frontswap: co...
239

8ea1d2a19   Vlastimil Babka   mm, frontswap: co...
240
241
242
  	VM_BUG_ON(!frontswap_ops);
  	VM_BUG_ON(!PageLocked(page));
  	VM_BUG_ON(sis == NULL);
d1dc6f1bc   Dan Streetman   frontswap: allow ...
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
  
  	/*
  	 * If a dup, we must remove the old page first; we can't leave the
  	 * old page no matter if the store of the new page succeeds or fails,
  	 * and we can't rely on the new page replacing the old page as we may
  	 * not store to the same implementation that contains the old page.
  	 */
  	if (__frontswap_test(sis, offset)) {
  		__frontswap_clear(sis, offset);
  		for_each_frontswap_ops(ops)
  			ops->invalidate_page(type, offset);
  	}
  
  	/* Try to store in each implementation, until one succeeds. */
  	for_each_frontswap_ops(ops) {
  		ret = ops->store(type, offset, page);
  		if (!ret) /* successful store */
  			break;
  	}
29f233cff   Dan Magenheimer   mm: frontswap: co...
262
  	if (ret == 0) {
d1dc6f1bc   Dan Streetman   frontswap: allow ...
263
  		__frontswap_set(sis, offset);
165c8aed5   Konrad Rzeszutek Wilk   frontswap: s/put_...
264
  		inc_frontswap_succ_stores();
d9674dda1   Sasha Levin   mm: frontswap: ma...
265
  	} else {
165c8aed5   Konrad Rzeszutek Wilk   frontswap: s/put_...
266
  		inc_frontswap_failed_stores();
4bb3e31ef   Sasha Levin   mm: frontswap: tr...
267
  	}
29f233cff   Dan Magenheimer   mm: frontswap: co...
268
269
270
271
272
  	if (frontswap_writethrough_enabled)
  		/* report failure so swap also writes to swap device */
  		ret = -1;
  	return ret;
  }
165c8aed5   Konrad Rzeszutek Wilk   frontswap: s/put_...
273
  EXPORT_SYMBOL(__frontswap_store);
29f233cff   Dan Magenheimer   mm: frontswap: co...
274
275
276
277
278
279
  
  /*
   * "Get" data from frontswap associated with swaptype and offset that were
   * specified when the data was put to frontswap and use it to fill the
   * specified page with data. Page must be locked and in the swap cache.
   */
165c8aed5   Konrad Rzeszutek Wilk   frontswap: s/put_...
280
  int __frontswap_load(struct page *page)
29f233cff   Dan Magenheimer   mm: frontswap: co...
281
282
283
284
285
286
  {
  	int ret = -1;
  	swp_entry_t entry = { .val = page_private(page), };
  	int type = swp_type(entry);
  	struct swap_info_struct *sis = swap_info[type];
  	pgoff_t offset = swp_offset(entry);
d1dc6f1bc   Dan Streetman   frontswap: allow ...
287
  	struct frontswap_ops *ops;
8ea1d2a19   Vlastimil Babka   mm, frontswap: co...
288
289
290
  	VM_BUG_ON(!frontswap_ops);
  	VM_BUG_ON(!PageLocked(page));
  	VM_BUG_ON(sis == NULL);
29f233cff   Dan Magenheimer   mm: frontswap: co...
291

d1dc6f1bc   Dan Streetman   frontswap: allow ...
292
293
294
295
296
297
298
299
300
  	if (!__frontswap_test(sis, offset))
  		return -1;
  
  	/* Try loading from each implementation, until one succeeds. */
  	for_each_frontswap_ops(ops) {
  		ret = ops->load(type, offset, page);
  		if (!ret) /* successful load */
  			break;
  	}
e3483a5f3   Dan Magenheimer   frontswap: suppor...
301
  	if (ret == 0) {
165c8aed5   Konrad Rzeszutek Wilk   frontswap: s/put_...
302
  		inc_frontswap_loads();
e3483a5f3   Dan Magenheimer   frontswap: suppor...
303
304
  		if (frontswap_tmem_exclusive_gets_enabled) {
  			SetPageDirty(page);
f066ea230   Bob Liu   mm: frontswap: cl...
305
  			__frontswap_clear(sis, offset);
e3483a5f3   Dan Magenheimer   frontswap: suppor...
306
307
  		}
  	}
29f233cff   Dan Magenheimer   mm: frontswap: co...
308
309
  	return ret;
  }
165c8aed5   Konrad Rzeszutek Wilk   frontswap: s/put_...
310
  EXPORT_SYMBOL(__frontswap_load);
29f233cff   Dan Magenheimer   mm: frontswap: co...
311
312
313
314
315
316
317
318
  
  /*
   * Invalidate any data from frontswap associated with the specified swaptype
   * and offset so that a subsequent "get" will fail.
   */
  void __frontswap_invalidate_page(unsigned type, pgoff_t offset)
  {
  	struct swap_info_struct *sis = swap_info[type];
d1dc6f1bc   Dan Streetman   frontswap: allow ...
319
  	struct frontswap_ops *ops;
8ea1d2a19   Vlastimil Babka   mm, frontswap: co...
320
321
  	VM_BUG_ON(!frontswap_ops);
  	VM_BUG_ON(sis == NULL);
29f233cff   Dan Magenheimer   mm: frontswap: co...
322

d1dc6f1bc   Dan Streetman   frontswap: allow ...
323
324
325
326
327
328
329
  	if (!__frontswap_test(sis, offset))
  		return;
  
  	for_each_frontswap_ops(ops)
  		ops->invalidate_page(type, offset);
  	__frontswap_clear(sis, offset);
  	inc_frontswap_invalidates();
29f233cff   Dan Magenheimer   mm: frontswap: co...
330
331
332
333
334
335
336
337
338
339
  }
  EXPORT_SYMBOL(__frontswap_invalidate_page);
  
  /*
   * Invalidate all data from frontswap associated with all offsets for the
   * specified swaptype.
   */
  void __frontswap_invalidate_area(unsigned type)
  {
  	struct swap_info_struct *sis = swap_info[type];
d1dc6f1bc   Dan Streetman   frontswap: allow ...
340
  	struct frontswap_ops *ops;
29f233cff   Dan Magenheimer   mm: frontswap: co...
341

8ea1d2a19   Vlastimil Babka   mm, frontswap: co...
342
343
  	VM_BUG_ON(!frontswap_ops);
  	VM_BUG_ON(sis == NULL);
d1dc6f1bc   Dan Streetman   frontswap: allow ...
344

d1dc6f1bc   Dan Streetman   frontswap: allow ...
345
346
347
348
349
350
351
  	if (sis->frontswap_map == NULL)
  		return;
  
  	for_each_frontswap_ops(ops)
  		ops->invalidate_area(type);
  	atomic_set(&sis->frontswap_pages, 0);
  	bitmap_zero(sis->frontswap_map, sis->max);
29f233cff   Dan Magenheimer   mm: frontswap: co...
352
353
  }
  EXPORT_SYMBOL(__frontswap_invalidate_area);
96253444d   Sasha Levin   mm: frontswap: sp...
354
355
  static unsigned long __frontswap_curr_pages(void)
  {
96253444d   Sasha Levin   mm: frontswap: sp...
356
357
358
359
  	unsigned long totalpages = 0;
  	struct swap_info_struct *si = NULL;
  
  	assert_spin_locked(&swap_lock);
18ab4d4ce   Dan Streetman   swap: change swap...
360
  	plist_for_each_entry(si, &swap_active_head, list)
96253444d   Sasha Levin   mm: frontswap: sp...
361
  		totalpages += atomic_read(&si->frontswap_pages);
96253444d   Sasha Levin   mm: frontswap: sp...
362
363
  	return totalpages;
  }
f116695a5   Sasha Levin   mm: frontswap: sp...
364
365
366
367
368
369
370
371
  static int __frontswap_unuse_pages(unsigned long total, unsigned long *unused,
  					int *swapid)
  {
  	int ret = -EINVAL;
  	struct swap_info_struct *si = NULL;
  	int si_frontswap_pages;
  	unsigned long total_pages_to_unuse = total;
  	unsigned long pages = 0, pages_to_unuse = 0;
f116695a5   Sasha Levin   mm: frontswap: sp...
372
373
  
  	assert_spin_locked(&swap_lock);
18ab4d4ce   Dan Streetman   swap: change swap...
374
  	plist_for_each_entry(si, &swap_active_head, list) {
f116695a5   Sasha Levin   mm: frontswap: sp...
375
376
377
378
379
380
381
382
383
384
385
386
387
388
  		si_frontswap_pages = atomic_read(&si->frontswap_pages);
  		if (total_pages_to_unuse < si_frontswap_pages) {
  			pages = pages_to_unuse = total_pages_to_unuse;
  		} else {
  			pages = si_frontswap_pages;
  			pages_to_unuse = 0; /* unuse all */
  		}
  		/* ensure there is enough RAM to fetch pages from frontswap */
  		if (security_vm_enough_memory_mm(current->mm, pages)) {
  			ret = -ENOMEM;
  			continue;
  		}
  		vm_unacct_memory(pages);
  		*unused = pages_to_unuse;
adfab836f   Dan Streetman   swap: change swap...
389
  		*swapid = si->type;
f116695a5   Sasha Levin   mm: frontswap: sp...
390
391
392
393
394
395
  		ret = 0;
  		break;
  	}
  
  	return ret;
  }
a00bb1e9f   Zhenzhong Duan   mm: frontswap: fi...
396
  /*
404f3ecfd   Ethon Paul   mm/frontswap: fix...
397
398
   * Used to check if it's necessary and feasible to unuse pages.
   * Return 1 when nothing to do, 0 when need to shrink pages,
a00bb1e9f   Zhenzhong Duan   mm: frontswap: fi...
399
400
   * error code when there is an error.
   */
69217b4cd   Sasha Levin   mm: frontswap: sp...
401
402
403
404
405
406
407
408
409
410
411
412
  static int __frontswap_shrink(unsigned long target_pages,
  				unsigned long *pages_to_unuse,
  				int *type)
  {
  	unsigned long total_pages = 0, total_pages_to_unuse;
  
  	assert_spin_locked(&swap_lock);
  
  	total_pages = __frontswap_curr_pages();
  	if (total_pages <= target_pages) {
  		/* Nothing to do */
  		*pages_to_unuse = 0;
a00bb1e9f   Zhenzhong Duan   mm: frontswap: fi...
413
  		return 1;
69217b4cd   Sasha Levin   mm: frontswap: sp...
414
415
416
417
  	}
  	total_pages_to_unuse = total_pages - target_pages;
  	return __frontswap_unuse_pages(total_pages_to_unuse, pages_to_unuse, type);
  }
29f233cff   Dan Magenheimer   mm: frontswap: co...
418
419
420
421
422
423
424
425
426
427
  /*
   * Frontswap, like a true swap device, may unnecessarily retain pages
   * under certain circumstances; "shrink" frontswap is essentially a
   * "partial swapoff" and works by calling try_to_unuse to attempt to
   * unuse enough frontswap pages to attempt to -- subject to memory
   * constraints -- reduce the number of pages in frontswap to the
   * number given in the parameter target_pages.
   */
  void frontswap_shrink(unsigned long target_pages)
  {
f116695a5   Sasha Levin   mm: frontswap: sp...
428
  	unsigned long pages_to_unuse = 0;
3f649ab72   Kees Cook   treewide: Remove ...
429
  	int type, ret;
29f233cff   Dan Magenheimer   mm: frontswap: co...
430
431
432
433
  
  	/*
  	 * we don't want to hold swap_lock while doing a very
  	 * lengthy try_to_unuse, but swap_list may change
18ab4d4ce   Dan Streetman   swap: change swap...
434
  	 * so restart scan from swap_active_head each time
29f233cff   Dan Magenheimer   mm: frontswap: co...
435
436
  	 */
  	spin_lock(&swap_lock);
69217b4cd   Sasha Levin   mm: frontswap: sp...
437
  	ret = __frontswap_shrink(target_pages, &pages_to_unuse, &type);
29f233cff   Dan Magenheimer   mm: frontswap: co...
438
  	spin_unlock(&swap_lock);
a00bb1e9f   Zhenzhong Duan   mm: frontswap: fi...
439
  	if (ret == 0)
69217b4cd   Sasha Levin   mm: frontswap: sp...
440
  		try_to_unuse(type, true, pages_to_unuse);
29f233cff   Dan Magenheimer   mm: frontswap: co...
441
442
443
444
445
446
447
448
449
450
451
  	return;
  }
  EXPORT_SYMBOL(frontswap_shrink);
  
  /*
   * Count and return the number of frontswap pages across all
   * swap devices.  This is exported so that backend drivers can
   * determine current usage without reading debugfs.
   */
  unsigned long frontswap_curr_pages(void)
  {
29f233cff   Dan Magenheimer   mm: frontswap: co...
452
  	unsigned long totalpages = 0;
29f233cff   Dan Magenheimer   mm: frontswap: co...
453
454
  
  	spin_lock(&swap_lock);
96253444d   Sasha Levin   mm: frontswap: sp...
455
  	totalpages = __frontswap_curr_pages();
29f233cff   Dan Magenheimer   mm: frontswap: co...
456
  	spin_unlock(&swap_lock);
96253444d   Sasha Levin   mm: frontswap: sp...
457

29f233cff   Dan Magenheimer   mm: frontswap: co...
458
459
460
461
462
463
464
465
466
467
  	return totalpages;
  }
  EXPORT_SYMBOL(frontswap_curr_pages);
  
  static int __init init_frontswap(void)
  {
  #ifdef CONFIG_DEBUG_FS
  	struct dentry *root = debugfs_create_dir("frontswap", NULL);
  	if (root == NULL)
  		return -ENXIO;
0825a6f98   Joe Perches   mm: use octal not...
468
469
470
471
472
  	debugfs_create_u64("loads", 0444, root, &frontswap_loads);
  	debugfs_create_u64("succ_stores", 0444, root, &frontswap_succ_stores);
  	debugfs_create_u64("failed_stores", 0444, root,
  			   &frontswap_failed_stores);
  	debugfs_create_u64("invalidates", 0444, root, &frontswap_invalidates);
29f233cff   Dan Magenheimer   mm: frontswap: co...
473
474
475
476
477
  #endif
  	return 0;
  }
  
  module_init(init_frontswap);