blob: ac6318a064d35e6dcc5385d1dc8062ff6e46554c [file] [log] [blame]
Johannes Weiner5d1ea482014-12-10 15:44:55 -08001#include <linux/swap_cgroup.h>
Paul Mundt4c8210422008-10-22 14:14:58 -07002#include <linux/vmalloc.h>
Johannes Weiner5d1ea482014-12-10 15:44:55 -08003#include <linux/mm.h>
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -08004
Johannes Weiner5d1ea482014-12-10 15:44:55 -08005#include <linux/swapops.h> /* depends on mm.h include */
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -08006
7static DEFINE_MUTEX(swap_cgroup_mutex);
8struct swap_cgroup_ctrl {
9 struct page **map;
10 unsigned long length;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -040011 spinlock_t lock;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -080012};
13
H Hartley Sweeten61600f52011-11-02 13:38:36 -070014static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES];
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -080015
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -080016struct swap_cgroup {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -070017 unsigned short id;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -080018};
19#define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup))
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -080020
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
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -080044 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;
51 }
52 return 0;
53not_enough_page:
54 max = idx;
55 for (idx = 0; idx < max; idx++)
56 __free_page(ctrl->map[idx]);
57
58 return -ENOMEM;
59}
60
Bob Liu9fb4b7c2012-01-12 17:18:48 -080061static struct swap_cgroup *lookup_swap_cgroup(swp_entry_t ent,
62 struct swap_cgroup_ctrl **ctrlp)
63{
64 pgoff_t offset = swp_offset(ent);
65 struct swap_cgroup_ctrl *ctrl;
66 struct page *mappage;
Hugh Dickinsc09ff082012-03-05 20:52:55 -080067 struct swap_cgroup *sc;
Bob Liu9fb4b7c2012-01-12 17:18:48 -080068
69 ctrl = &swap_cgroup_ctrl[swp_type(ent)];
70 if (ctrlp)
71 *ctrlp = ctrl;
72
73 mappage = ctrl->map[offset / SC_PER_PAGE];
Hugh Dickinsc09ff082012-03-05 20:52:55 -080074 sc = page_address(mappage);
75 return sc + offset % SC_PER_PAGE;
Bob Liu9fb4b7c2012-01-12 17:18:48 -080076}
77
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -080078/**
Daisuke Nishimura02491442010-03-10 15:22:17 -080079 * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
Wanpeng Lidad75572012-06-20 12:53:01 -070080 * @ent: swap entry to be cmpxchged
Daisuke Nishimura02491442010-03-10 15:22:17 -080081 * @old: old id
82 * @new: new id
83 *
84 * Returns old id at success, 0 at failure.
Lucas De Marchi25985ed2011-03-30 22:57:33 -030085 * (There is no mem_cgroup using 0 as its id)
Daisuke Nishimura02491442010-03-10 15:22:17 -080086 */
87unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
88 unsigned short old, unsigned short new)
89{
Daisuke Nishimura02491442010-03-10 15:22:17 -080090 struct swap_cgroup_ctrl *ctrl;
Daisuke Nishimura02491442010-03-10 15:22:17 -080091 struct swap_cgroup *sc;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -040092 unsigned long flags;
93 unsigned short retval;
Daisuke Nishimura02491442010-03-10 15:22:17 -080094
Bob Liu9fb4b7c2012-01-12 17:18:48 -080095 sc = lookup_swap_cgroup(ent, &ctrl);
Daisuke Nishimura02491442010-03-10 15:22:17 -080096
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -040097 spin_lock_irqsave(&ctrl->lock, flags);
98 retval = sc->id;
99 if (retval == old)
100 sc->id = new;
Daisuke Nishimura02491442010-03-10 15:22:17 -0800101 else
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400102 retval = 0;
103 spin_unlock_irqrestore(&ctrl->lock, flags);
104 return retval;
Daisuke Nishimura02491442010-03-10 15:22:17 -0800105}
106
107/**
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800108 * swap_cgroup_record - record mem_cgroup for this swp_entry.
109 * @ent: swap entry to be recorded into
Wanpeng Lidad75572012-06-20 12:53:01 -0700110 * @id: mem_cgroup to be recorded
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800111 *
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700112 * Returns old value at success, 0 at failure.
113 * (Of course, old value can be 0.)
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800114 */
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700115unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800116{
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800117 struct swap_cgroup_ctrl *ctrl;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800118 struct swap_cgroup *sc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -0700119 unsigned short old;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400120 unsigned long flags;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800121
Bob Liu9fb4b7c2012-01-12 17:18:48 -0800122 sc = lookup_swap_cgroup(ent, &ctrl);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800123
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400124 spin_lock_irqsave(&ctrl->lock, flags);
125 old = sc->id;
126 sc->id = id;
127 spin_unlock_irqrestore(&ctrl->lock, flags);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800128
129 return old;
130}
131
132/**
Bob Liu9fb4b7c2012-01-12 17:18:48 -0800133 * lookup_swap_cgroup_id - lookup mem_cgroup id tied to swap entry
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800134 * @ent: swap entry to be looked up.
135 *
Hugh Dickinsb3ff8a22014-01-12 20:23:27 -0800136 * Returns ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800137 */
Bob Liu9fb4b7c2012-01-12 17:18:48 -0800138unsigned short lookup_swap_cgroup_id(swp_entry_t ent)
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800139{
Bob Liu9fb4b7c2012-01-12 17:18:48 -0800140 return lookup_swap_cgroup(ent, NULL)->id;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800141}
142
143int swap_cgroup_swapon(int type, unsigned long max_pages)
144{
145 void *array;
146 unsigned long array_size;
147 unsigned long length;
148 struct swap_cgroup_ctrl *ctrl;
149
150 if (!do_swap_account)
151 return 0;
152
Namhyung Kim33278f72011-05-26 16:25:30 -0700153 length = DIV_ROUND_UP(max_pages, SC_PER_PAGE);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800154 array_size = length * sizeof(void *);
155
Joe Perches8c1fec12011-05-28 10:36:34 -0700156 array = vzalloc(array_size);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800157 if (!array)
158 goto nomem;
159
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800160 ctrl = &swap_cgroup_ctrl[type];
161 mutex_lock(&swap_cgroup_mutex);
162 ctrl->length = length;
163 ctrl->map = array;
KAMEZAWA Hiroyukie9e58a42010-03-15 00:34:57 -0400164 spin_lock_init(&ctrl->lock);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800165 if (swap_cgroup_prepare(type)) {
166 /* memory shortage */
167 ctrl->map = NULL;
168 ctrl->length = 0;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800169 mutex_unlock(&swap_cgroup_mutex);
Namhyung Kim6a5b18d2011-05-26 16:25:31 -0700170 vfree(array);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800171 goto nomem;
172 }
173 mutex_unlock(&swap_cgroup_mutex);
174
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800175 return 0;
176nomem:
Joe Perches11705322016-03-17 14:19:50 -0700177 pr_info("couldn't allocate enough memory for swap_cgroup\n");
178 pr_info("swap_cgroup can be disabled by swapaccount=0 boot option\n");
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800179 return -ENOMEM;
180}
181
182void swap_cgroup_swapoff(int type)
183{
Namhyung Kim6a5b18d2011-05-26 16:25:31 -0700184 struct page **map;
185 unsigned long i, length;
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800186 struct swap_cgroup_ctrl *ctrl;
187
188 if (!do_swap_account)
189 return;
190
191 mutex_lock(&swap_cgroup_mutex);
192 ctrl = &swap_cgroup_ctrl[type];
Namhyung Kim6a5b18d2011-05-26 16:25:31 -0700193 map = ctrl->map;
194 length = ctrl->length;
195 ctrl->map = NULL;
196 ctrl->length = 0;
197 mutex_unlock(&swap_cgroup_mutex);
198
199 if (map) {
200 for (i = 0; i < length; i++) {
201 struct page *page = map[i];
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800202 if (page)
203 __free_page(page);
David Rientjes460bcec2017-04-07 16:05:00 -0700204 if (!(i % SWAP_CLUSTER_MAX))
205 cond_resched();
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800206 }
Namhyung Kim6a5b18d2011-05-26 16:25:31 -0700207 vfree(map);
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800208 }
KAMEZAWA Hiroyuki27a7faa2009-01-07 18:07:58 -0800209}