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