afs: Provide a splice-read wrapper
[linux-block.git] / mm / swap_cgroup.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
5d1ea48b 2#include <linux/swap_cgroup.h>
4c821042 3#include <linux/vmalloc.h>
5d1ea48b 4#include <linux/mm.h>
27a7faa0 5
5d1ea48b 6#include <linux/swapops.h> /* depends on mm.h include */
27a7faa0
KH
7
8static DEFINE_MUTEX(swap_cgroup_mutex);
9struct swap_cgroup_ctrl {
10 struct page **map;
11 unsigned long length;
e9e58a4e 12 spinlock_t lock;
27a7faa0
KH
13};
14
61600f57 15static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES];
27a7faa0 16
27a7faa0 17struct swap_cgroup {
a3b2d692 18 unsigned short id;
27a7faa0
KH
19};
20#define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup))
27a7faa0
KH
21
22/*
23 * SwapCgroup implements "lookup" and "exchange" operations.
24 * In typical usage, this swap_cgroup is accessed via memcg's charge/uncharge
25 * against SwapCache. At swap_free(), this is accessed directly from swap.
26 *
27 * This means,
28 * - we have no race in "exchange" when we're accessed via SwapCache because
29 * SwapCache(and its swp_entry) is under lock.
30 * - When called via swap_free(), there is no user of this entry and no race.
31 * Then, we don't need lock around "exchange".
32 *
33 * TODO: we can push these buffers out to HIGHMEM.
34 */
35
36/*
37 * allocate buffer for swap_cgroup.
38 */
39static int swap_cgroup_prepare(int type)
40{
41 struct page *page;
42 struct swap_cgroup_ctrl *ctrl;
43 unsigned long idx, max;
44
27a7faa0
KH
45 ctrl = &swap_cgroup_ctrl[type];
46
47 for (idx = 0; idx < ctrl->length; idx++) {
48 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
49 if (!page)
50 goto not_enough_page;
51 ctrl->map[idx] = page;
ef707629
YZ
52
53 if (!(idx % SWAP_CLUSTER_MAX))
54 cond_resched();
27a7faa0
KH
55 }
56 return 0;
57not_enough_page:
58 max = idx;
59 for (idx = 0; idx < max; idx++)
60 __free_page(ctrl->map[idx]);
61
62 return -ENOMEM;
63}
64
38d8b4e6
HY
65static struct swap_cgroup *__lookup_swap_cgroup(struct swap_cgroup_ctrl *ctrl,
66 pgoff_t offset)
67{
68 struct page *mappage;
69 struct swap_cgroup *sc;
70
71 mappage = ctrl->map[offset / SC_PER_PAGE];
72 sc = page_address(mappage);
73 return sc + offset % SC_PER_PAGE;
74}
75
9fb4b7cc
BL
76static struct swap_cgroup *lookup_swap_cgroup(swp_entry_t ent,
77 struct swap_cgroup_ctrl **ctrlp)
78{
79 pgoff_t offset = swp_offset(ent);
80 struct swap_cgroup_ctrl *ctrl;
9fb4b7cc
BL
81
82 ctrl = &swap_cgroup_ctrl[swp_type(ent)];
83 if (ctrlp)
84 *ctrlp = ctrl;
38d8b4e6 85 return __lookup_swap_cgroup(ctrl, offset);
9fb4b7cc
BL
86}
87
02491447
DN
88/**
89 * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
dad7557e 90 * @ent: swap entry to be cmpxchged
02491447
DN
91 * @old: old id
92 * @new: new id
93 *
94 * Returns old id at success, 0 at failure.
25985edc 95 * (There is no mem_cgroup using 0 as its id)
02491447
DN
96 */
97unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
98 unsigned short old, unsigned short new)
99{
02491447 100 struct swap_cgroup_ctrl *ctrl;
02491447 101 struct swap_cgroup *sc;
e9e58a4e
KH
102 unsigned long flags;
103 unsigned short retval;
02491447 104
9fb4b7cc 105 sc = lookup_swap_cgroup(ent, &ctrl);
02491447 106
e9e58a4e
KH
107 spin_lock_irqsave(&ctrl->lock, flags);
108 retval = sc->id;
109 if (retval == old)
110 sc->id = new;
02491447 111 else
e9e58a4e
KH
112 retval = 0;
113 spin_unlock_irqrestore(&ctrl->lock, flags);
114 return retval;
02491447
DN
115}
116
27a7faa0 117/**
38d8b4e6
HY
118 * swap_cgroup_record - record mem_cgroup for a set of swap entries
119 * @ent: the first swap entry to be recorded into
dad7557e 120 * @id: mem_cgroup to be recorded
38d8b4e6 121 * @nr_ents: number of swap entries to be recorded
27a7faa0 122 *
a3b2d692
KH
123 * Returns old value at success, 0 at failure.
124 * (Of course, old value can be 0.)
27a7faa0 125 */
38d8b4e6
HY
126unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id,
127 unsigned int nr_ents)
27a7faa0 128{
27a7faa0 129 struct swap_cgroup_ctrl *ctrl;
27a7faa0 130 struct swap_cgroup *sc;
a3b2d692 131 unsigned short old;
e9e58a4e 132 unsigned long flags;
38d8b4e6
HY
133 pgoff_t offset = swp_offset(ent);
134 pgoff_t end = offset + nr_ents;
27a7faa0 135
9fb4b7cc 136 sc = lookup_swap_cgroup(ent, &ctrl);
27a7faa0 137
e9e58a4e
KH
138 spin_lock_irqsave(&ctrl->lock, flags);
139 old = sc->id;
38d8b4e6
HY
140 for (;;) {
141 VM_BUG_ON(sc->id != old);
142 sc->id = id;
143 offset++;
144 if (offset == end)
145 break;
146 if (offset % SC_PER_PAGE)
147 sc++;
148 else
149 sc = __lookup_swap_cgroup(ctrl, offset);
150 }
e9e58a4e 151 spin_unlock_irqrestore(&ctrl->lock, flags);
27a7faa0
KH
152
153 return old;
154}
155
156/**
9fb4b7cc 157 * lookup_swap_cgroup_id - lookup mem_cgroup id tied to swap entry
27a7faa0
KH
158 * @ent: swap entry to be looked up.
159 *
b3ff8a2f 160 * Returns ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
27a7faa0 161 */
9fb4b7cc 162unsigned short lookup_swap_cgroup_id(swp_entry_t ent)
27a7faa0 163{
9fb4b7cc 164 return lookup_swap_cgroup(ent, NULL)->id;
27a7faa0
KH
165}
166
167int swap_cgroup_swapon(int type, unsigned long max_pages)
168{
169 void *array;
27a7faa0
KH
170 unsigned long length;
171 struct swap_cgroup_ctrl *ctrl;
172
c91bdc93
JW
173 if (mem_cgroup_disabled())
174 return 0;
175
33278f7f 176 length = DIV_ROUND_UP(max_pages, SC_PER_PAGE);
27a7faa0 177
3000f2e2 178 array = vcalloc(length, sizeof(void *));
27a7faa0
KH
179 if (!array)
180 goto nomem;
181
27a7faa0
KH
182 ctrl = &swap_cgroup_ctrl[type];
183 mutex_lock(&swap_cgroup_mutex);
184 ctrl->length = length;
185 ctrl->map = array;
e9e58a4e 186 spin_lock_init(&ctrl->lock);
27a7faa0
KH
187 if (swap_cgroup_prepare(type)) {
188 /* memory shortage */
189 ctrl->map = NULL;
190 ctrl->length = 0;
27a7faa0 191 mutex_unlock(&swap_cgroup_mutex);
6a5b18d2 192 vfree(array);
27a7faa0
KH
193 goto nomem;
194 }
195 mutex_unlock(&swap_cgroup_mutex);
196
27a7faa0
KH
197 return 0;
198nomem:
1170532b
JP
199 pr_info("couldn't allocate enough memory for swap_cgroup\n");
200 pr_info("swap_cgroup can be disabled by swapaccount=0 boot option\n");
27a7faa0
KH
201 return -ENOMEM;
202}
203
204void swap_cgroup_swapoff(int type)
205{
6a5b18d2
NK
206 struct page **map;
207 unsigned long i, length;
27a7faa0
KH
208 struct swap_cgroup_ctrl *ctrl;
209
c91bdc93
JW
210 if (mem_cgroup_disabled())
211 return;
212
27a7faa0
KH
213 mutex_lock(&swap_cgroup_mutex);
214 ctrl = &swap_cgroup_ctrl[type];
6a5b18d2
NK
215 map = ctrl->map;
216 length = ctrl->length;
217 ctrl->map = NULL;
218 ctrl->length = 0;
219 mutex_unlock(&swap_cgroup_mutex);
220
221 if (map) {
222 for (i = 0; i < length; i++) {
223 struct page *page = map[i];
27a7faa0
KH
224 if (page)
225 __free_page(page);
460bcec8
DR
226 if (!(i % SWAP_CLUSTER_MAX))
227 cond_resched();
27a7faa0 228 }
6a5b18d2 229 vfree(map);
27a7faa0 230 }
27a7faa0 231}