blob: 7f34343c075a1cfbcad053aaa1d25bab7d548bf0 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Johannes Weiner5d1ea482014-12-10 15:44:55 -08002#include <linux/swap_cgroup.h>
Paul Mundt4c8210422008-10-22 14:14:58 -07003#include <linux/vmalloc.h>
Johannes Weiner5d1ea482014-12-10 15:44:55 -08004#include <linux/mm.h>
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -08005
Johannes Weiner5d1ea482014-12-10 15:44:55 -08006#include <linux/swapops.h> /* depends on mm.h include */
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -08007
8static DEFINE_MUTEX(swap_cgroup_mutex);
9struct swap_cgroup_ctrl {
10 struct page **map;
11 unsigned long length;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -040012 spinlock_t lock;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -080013};
14
H Hartley Sweeten61600f52011-11-02 13:38:36 -070015static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES];
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -080016
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -080017struct swap_cgroup {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -070018 unsigned short id;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -080019};
20#define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup))
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -080021
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
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -080045 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;
Yu Zhaoef707622017-06-16 14:02:31 -070052
53 if (!(idx % SWAP_CLUSTER_MAX))
54 cond_resched();
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -080055 }
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
Huang Ying38d8b4e2017-07-06 15:37:18 -070065static 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
Bob Liu9fb4b7c2012-01-12 17:18:48 -080076static 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;
Bob Liu9fb4b7c2012-01-12 17:18:48 -080081
82 ctrl = &swap_cgroup_ctrl[swp_type(ent)];
83 if (ctrlp)
84 *ctrlp = ctrl;
Huang Ying38d8b4e2017-07-06 15:37:18 -070085 return __lookup_swap_cgroup(ctrl, offset);
Bob Liu9fb4b7c2012-01-12 17:18:48 -080086}
87
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -080088/**
Daisuke Nishimura02491442010-03-10 15:22:17 -080089 * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
Wanpeng Lidad75572012-06-20 12:53:01 -070090 * @ent: swap entry to be cmpxchged
Daisuke Nishimura02491442010-03-10 15:22:17 -080091 * @old: old id
92 * @new: new id
93 *
94 * Returns old id at success, 0 at failure.
Lucas De Marchi25985ed2011-03-30 22:57:33 -030095 * (There is no mem_cgroup using 0 as its id)
Daisuke Nishimura02491442010-03-10 15:22:17 -080096 */
97unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
98 unsigned short old, unsigned short new)
99{
Daisuke Nishimura02491442010-03-10 15:22:17 -0800100 struct swap_cgroup_ctrl *ctrl;
Daisuke Nishimura02491442010-03-10 15:22:17 -0800101 struct swap_cgroup *sc;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400102 unsigned long flags;
103 unsigned short retval;
Daisuke Nishimura02491442010-03-10 15:22:17 -0800104
Bob Liu9fb4b7c2012-01-12 17:18:48 -0800105 sc = lookup_swap_cgroup(ent, &ctrl);
Daisuke Nishimura02491442010-03-10 15:22:17 -0800106
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400107 spin_lock_irqsave(&ctrl->lock, flags);
108 retval = sc->id;
109 if (retval == old)
110 sc->id = new;
Daisuke Nishimura02491442010-03-10 15:22:17 -0800111 else
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400112 retval = 0;
113 spin_unlock_irqrestore(&ctrl->lock, flags);
114 return retval;
Daisuke Nishimura02491442010-03-10 15:22:17 -0800115}
116
117/**
Huang Ying38d8b4e2017-07-06 15:37:18 -0700118 * swap_cgroup_record - record mem_cgroup for a set of swap entries
119 * @ent: the first swap entry to be recorded into
Wanpeng Lidad75572012-06-20 12:53:01 -0700120 * @id: mem_cgroup to be recorded
Huang Ying38d8b4e2017-07-06 15:37:18 -0700121 * @nr_ents: number of swap entries to be recorded
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800122 *
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700123 * Returns old value at success, 0 at failure.
124 * (Of course, old value can be 0.)
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800125 */
Huang Ying38d8b4e2017-07-06 15:37:18 -0700126unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id,
127 unsigned int nr_ents)
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800128{
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800129 struct swap_cgroup_ctrl *ctrl;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800130 struct swap_cgroup *sc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700131 unsigned short old;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400132 unsigned long flags;
Huang Ying38d8b4e2017-07-06 15:37:18 -0700133 pgoff_t offset = swp_offset(ent);
134 pgoff_t end = offset + nr_ents;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800135
Bob Liu9fb4b7c2012-01-12 17:18:48 -0800136 sc = lookup_swap_cgroup(ent, &ctrl);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800137
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400138 spin_lock_irqsave(&ctrl->lock, flags);
139 old = sc->id;
Huang Ying38d8b4e2017-07-06 15:37:18 -0700140 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 }
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400151 spin_unlock_irqrestore(&ctrl->lock, flags);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800152
153 return old;
154}
155
156/**
Bob Liu9fb4b7c2012-01-12 17:18:48 -0800157 * lookup_swap_cgroup_id - lookup mem_cgroup id tied to swap entry
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800158 * @ent: swap entry to be looked up.
159 *
Hugh Dickinsb3ff8a22014-01-12 20:23:27 -0800160 * Returns ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800161 */
Bob Liu9fb4b7c2012-01-12 17:18:48 -0800162unsigned short lookup_swap_cgroup_id(swp_entry_t ent)
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800163{
Bob Liu9fb4b7c2012-01-12 17:18:48 -0800164 return lookup_swap_cgroup(ent, NULL)->id;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800165}
166
167int swap_cgroup_swapon(int type, unsigned long max_pages)
168{
169 void *array;
170 unsigned long array_size;
171 unsigned long length;
172 struct swap_cgroup_ctrl *ctrl;
173
Namhyung Kim33278f72011-05-26 16:25:30 -0700174 length = DIV_ROUND_UP(max_pages, SC_PER_PAGE);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800175 array_size = length * sizeof(void *);
176
Joe Perches8c1fec12011-05-28 10:36:34 -0700177 array = vzalloc(array_size);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800178 if (!array)
179 goto nomem;
180
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800181 ctrl = &swap_cgroup_ctrl[type];
182 mutex_lock(&swap_cgroup_mutex);
183 ctrl->length = length;
184 ctrl->map = array;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400185 spin_lock_init(&ctrl->lock);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800186 if (swap_cgroup_prepare(type)) {
187 /* memory shortage */
188 ctrl->map = NULL;
189 ctrl->length = 0;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800190 mutex_unlock(&swap_cgroup_mutex);
Namhyung Kim6a5b18d2011-05-26 16:25:31 -0700191 vfree(array);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800192 goto nomem;
193 }
194 mutex_unlock(&swap_cgroup_mutex);
195
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800196 return 0;
197nomem:
Joe Perches11705322016-03-17 14:19:50 -0700198 pr_info("couldn't allocate enough memory for swap_cgroup\n");
199 pr_info("swap_cgroup can be disabled by swapaccount=0 boot option\n");
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800200 return -ENOMEM;
201}
202
203void swap_cgroup_swapoff(int type)
204{
Namhyung Kim6a5b18d2011-05-26 16:25:31 -0700205 struct page **map;
206 unsigned long i, length;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800207 struct swap_cgroup_ctrl *ctrl;
208
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800209 mutex_lock(&swap_cgroup_mutex);
210 ctrl = &swap_cgroup_ctrl[type];
Namhyung Kim6a5b18d2011-05-26 16:25:31 -0700211 map = ctrl->map;
212 length = ctrl->length;
213 ctrl->map = NULL;
214 ctrl->length = 0;
215 mutex_unlock(&swap_cgroup_mutex);
216
217 if (map) {
218 for (i = 0; i < length; i++) {
219 struct page *page = map[i];
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800220 if (page)
221 __free_page(page);
David Rientjes460bcec2017-04-07 16:05:00 -0700222 if (!(i % SWAP_CLUSTER_MAX))
223 cond_resched();
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800224 }
Namhyung Kim6a5b18d2011-05-26 16:25:31 -0700225 vfree(map);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800226 }
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800227}