Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Johannes Weiner | 5d1ea48 | 2014-12-10 15:44:55 -0800 | [diff] [blame] | 2 | #include <linux/swap_cgroup.h> |
Paul Mundt | 4c821042 | 2008-10-22 14:14:58 -0700 | [diff] [blame] | 3 | #include <linux/vmalloc.h> |
Johannes Weiner | 5d1ea48 | 2014-12-10 15:44:55 -0800 | [diff] [blame] | 4 | #include <linux/mm.h> |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 5 | |
Johannes Weiner | 5d1ea48 | 2014-12-10 15:44:55 -0800 | [diff] [blame] | 6 | #include <linux/swapops.h> /* depends on mm.h include */ |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 7 | |
| 8 | static DEFINE_MUTEX(swap_cgroup_mutex); |
| 9 | struct swap_cgroup_ctrl { |
| 10 | struct page **map; |
| 11 | unsigned long length; |
KAMEZAWA Hiroyuki | e9e58a4 | 2010-03-15 00:34:57 -0400 | [diff] [blame] | 12 | spinlock_t lock; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 13 | }; |
| 14 | |
H Hartley Sweeten | 61600f5 | 2011-11-02 13:38:36 -0700 | [diff] [blame] | 15 | static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES]; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 16 | |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 17 | struct swap_cgroup { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 18 | unsigned short id; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 19 | }; |
| 20 | #define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup)) |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 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 | */ |
| 39 | static int swap_cgroup_prepare(int type) |
| 40 | { |
| 41 | struct page *page; |
| 42 | struct swap_cgroup_ctrl *ctrl; |
| 43 | unsigned long idx, max; |
| 44 | |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 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; |
Yu Zhao | ef70762 | 2017-06-16 14:02:31 -0700 | [diff] [blame] | 52 | |
| 53 | if (!(idx % SWAP_CLUSTER_MAX)) |
| 54 | cond_resched(); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 55 | } |
| 56 | return 0; |
| 57 | not_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 Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 65 | static 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 Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 76 | static 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 Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 81 | |
| 82 | ctrl = &swap_cgroup_ctrl[swp_type(ent)]; |
| 83 | if (ctrlp) |
| 84 | *ctrlp = ctrl; |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 85 | return __lookup_swap_cgroup(ctrl, offset); |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 86 | } |
| 87 | |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 88 | /** |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 89 | * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry. |
Wanpeng Li | dad7557 | 2012-06-20 12:53:01 -0700 | [diff] [blame] | 90 | * @ent: swap entry to be cmpxchged |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 91 | * @old: old id |
| 92 | * @new: new id |
| 93 | * |
| 94 | * Returns old id at success, 0 at failure. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 95 | * (There is no mem_cgroup using 0 as its id) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 96 | */ |
| 97 | unsigned short swap_cgroup_cmpxchg(swp_entry_t ent, |
| 98 | unsigned short old, unsigned short new) |
| 99 | { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 100 | struct swap_cgroup_ctrl *ctrl; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 101 | struct swap_cgroup *sc; |
KAMEZAWA Hiroyuki | e9e58a4 | 2010-03-15 00:34:57 -0400 | [diff] [blame] | 102 | unsigned long flags; |
| 103 | unsigned short retval; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 104 | |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 105 | sc = lookup_swap_cgroup(ent, &ctrl); |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 106 | |
KAMEZAWA Hiroyuki | e9e58a4 | 2010-03-15 00:34:57 -0400 | [diff] [blame] | 107 | spin_lock_irqsave(&ctrl->lock, flags); |
| 108 | retval = sc->id; |
| 109 | if (retval == old) |
| 110 | sc->id = new; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 111 | else |
KAMEZAWA Hiroyuki | e9e58a4 | 2010-03-15 00:34:57 -0400 | [diff] [blame] | 112 | retval = 0; |
| 113 | spin_unlock_irqrestore(&ctrl->lock, flags); |
| 114 | return retval; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 118 | * swap_cgroup_record - record mem_cgroup for a set of swap entries |
| 119 | * @ent: the first swap entry to be recorded into |
Wanpeng Li | dad7557 | 2012-06-20 12:53:01 -0700 | [diff] [blame] | 120 | * @id: mem_cgroup to be recorded |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 121 | * @nr_ents: number of swap entries to be recorded |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 122 | * |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 123 | * Returns old value at success, 0 at failure. |
| 124 | * (Of course, old value can be 0.) |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 125 | */ |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 126 | unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id, |
| 127 | unsigned int nr_ents) |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 128 | { |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 129 | struct swap_cgroup_ctrl *ctrl; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 130 | struct swap_cgroup *sc; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 131 | unsigned short old; |
KAMEZAWA Hiroyuki | e9e58a4 | 2010-03-15 00:34:57 -0400 | [diff] [blame] | 132 | unsigned long flags; |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 133 | pgoff_t offset = swp_offset(ent); |
| 134 | pgoff_t end = offset + nr_ents; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 135 | |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 136 | sc = lookup_swap_cgroup(ent, &ctrl); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 137 | |
KAMEZAWA Hiroyuki | e9e58a4 | 2010-03-15 00:34:57 -0400 | [diff] [blame] | 138 | spin_lock_irqsave(&ctrl->lock, flags); |
| 139 | old = sc->id; |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 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 | } |
KAMEZAWA Hiroyuki | e9e58a4 | 2010-03-15 00:34:57 -0400 | [diff] [blame] | 151 | spin_unlock_irqrestore(&ctrl->lock, flags); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 152 | |
| 153 | return old; |
| 154 | } |
| 155 | |
| 156 | /** |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 157 | * lookup_swap_cgroup_id - lookup mem_cgroup id tied to swap entry |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 158 | * @ent: swap entry to be looked up. |
| 159 | * |
Hugh Dickins | b3ff8a2 | 2014-01-12 20:23:27 -0800 | [diff] [blame] | 160 | * Returns ID of mem_cgroup at success. 0 at failure. (0 is invalid ID) |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 161 | */ |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 162 | unsigned short lookup_swap_cgroup_id(swp_entry_t ent) |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 163 | { |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 164 | return lookup_swap_cgroup(ent, NULL)->id; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | int 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 | |
| 174 | if (!do_swap_account) |
| 175 | return 0; |
| 176 | |
Namhyung Kim | 33278f7 | 2011-05-26 16:25:30 -0700 | [diff] [blame] | 177 | length = DIV_ROUND_UP(max_pages, SC_PER_PAGE); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 178 | array_size = length * sizeof(void *); |
| 179 | |
Joe Perches | 8c1fec1 | 2011-05-28 10:36:34 -0700 | [diff] [blame] | 180 | array = vzalloc(array_size); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 181 | if (!array) |
| 182 | goto nomem; |
| 183 | |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 184 | ctrl = &swap_cgroup_ctrl[type]; |
| 185 | mutex_lock(&swap_cgroup_mutex); |
| 186 | ctrl->length = length; |
| 187 | ctrl->map = array; |
KAMEZAWA Hiroyuki | e9e58a4 | 2010-03-15 00:34:57 -0400 | [diff] [blame] | 188 | spin_lock_init(&ctrl->lock); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 189 | if (swap_cgroup_prepare(type)) { |
| 190 | /* memory shortage */ |
| 191 | ctrl->map = NULL; |
| 192 | ctrl->length = 0; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 193 | mutex_unlock(&swap_cgroup_mutex); |
Namhyung Kim | 6a5b18d | 2011-05-26 16:25:31 -0700 | [diff] [blame] | 194 | vfree(array); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 195 | goto nomem; |
| 196 | } |
| 197 | mutex_unlock(&swap_cgroup_mutex); |
| 198 | |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 199 | return 0; |
| 200 | nomem: |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 201 | pr_info("couldn't allocate enough memory for swap_cgroup\n"); |
| 202 | pr_info("swap_cgroup can be disabled by swapaccount=0 boot option\n"); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 203 | return -ENOMEM; |
| 204 | } |
| 205 | |
| 206 | void swap_cgroup_swapoff(int type) |
| 207 | { |
Namhyung Kim | 6a5b18d | 2011-05-26 16:25:31 -0700 | [diff] [blame] | 208 | struct page **map; |
| 209 | unsigned long i, length; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 210 | struct swap_cgroup_ctrl *ctrl; |
| 211 | |
| 212 | if (!do_swap_account) |
| 213 | return; |
| 214 | |
| 215 | mutex_lock(&swap_cgroup_mutex); |
| 216 | ctrl = &swap_cgroup_ctrl[type]; |
Namhyung Kim | 6a5b18d | 2011-05-26 16:25:31 -0700 | [diff] [blame] | 217 | map = ctrl->map; |
| 218 | length = ctrl->length; |
| 219 | ctrl->map = NULL; |
| 220 | ctrl->length = 0; |
| 221 | mutex_unlock(&swap_cgroup_mutex); |
| 222 | |
| 223 | if (map) { |
| 224 | for (i = 0; i < length; i++) { |
| 225 | struct page *page = map[i]; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 226 | if (page) |
| 227 | __free_page(page); |
David Rientjes | 460bcec | 2017-04-07 16:05:00 -0700 | [diff] [blame] | 228 | if (!(i % SWAP_CLUSTER_MAX)) |
| 229 | cond_resched(); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 230 | } |
Namhyung Kim | 6a5b18d | 2011-05-26 16:25:31 -0700 | [diff] [blame] | 231 | vfree(map); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 232 | } |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 233 | } |