Johannes Weiner | 5d1ea48 | 2014-12-10 15:44:55 -0800 | [diff] [blame] | 1 | #include <linux/swap_cgroup.h> |
Paul Mundt | 4c821042 | 2008-10-22 14:14:58 -0700 | [diff] [blame] | 2 | #include <linux/vmalloc.h> |
Johannes Weiner | 5d1ea48 | 2014-12-10 15:44:55 -0800 | [diff] [blame] | 3 | #include <linux/mm.h> |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 4 | |
Johannes Weiner | 5d1ea48 | 2014-12-10 15:44:55 -0800 | [diff] [blame] | 5 | #include <linux/swapops.h> /* depends on mm.h include */ |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 6 | |
| 7 | static DEFINE_MUTEX(swap_cgroup_mutex); |
| 8 | struct swap_cgroup_ctrl { |
| 9 | struct page **map; |
| 10 | unsigned long length; |
KAMEZAWA Hiroyuki | e9e58a4 | 2010-03-15 00:34:57 -0400 | [diff] [blame] | 11 | spinlock_t lock; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 12 | }; |
| 13 | |
H Hartley Sweeten | 61600f5 | 2011-11-02 13:38:36 -0700 | [diff] [blame] | 14 | static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES]; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 15 | |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 16 | struct swap_cgroup { |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 17 | unsigned short id; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 18 | }; |
| 19 | #define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup)) |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 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 | */ |
| 38 | static int swap_cgroup_prepare(int type) |
| 39 | { |
| 40 | struct page *page; |
| 41 | struct swap_cgroup_ctrl *ctrl; |
| 42 | unsigned long idx, max; |
| 43 | |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 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; |
| 51 | } |
| 52 | return 0; |
| 53 | not_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 Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 61 | static 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 Dickins | c09ff08 | 2012-03-05 20:52:55 -0800 | [diff] [blame] | 67 | struct swap_cgroup *sc; |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 68 | |
| 69 | ctrl = &swap_cgroup_ctrl[swp_type(ent)]; |
| 70 | if (ctrlp) |
| 71 | *ctrlp = ctrl; |
| 72 | |
| 73 | mappage = ctrl->map[offset / SC_PER_PAGE]; |
Hugh Dickins | c09ff08 | 2012-03-05 20:52:55 -0800 | [diff] [blame] | 74 | sc = page_address(mappage); |
| 75 | return sc + offset % SC_PER_PAGE; |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 76 | } |
| 77 | |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 78 | /** |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 79 | * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry. |
Wanpeng Li | dad7557 | 2012-06-20 12:53:01 -0700 | [diff] [blame] | 80 | * @ent: swap entry to be cmpxchged |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 81 | * @old: old id |
| 82 | * @new: new id |
| 83 | * |
| 84 | * Returns old id at success, 0 at failure. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 85 | * (There is no mem_cgroup using 0 as its id) |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 86 | */ |
| 87 | unsigned short swap_cgroup_cmpxchg(swp_entry_t ent, |
| 88 | unsigned short old, unsigned short new) |
| 89 | { |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 90 | struct swap_cgroup_ctrl *ctrl; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 91 | struct swap_cgroup *sc; |
KAMEZAWA Hiroyuki | e9e58a4 | 2010-03-15 00:34:57 -0400 | [diff] [blame] | 92 | unsigned long flags; |
| 93 | unsigned short retval; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 94 | |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 95 | sc = lookup_swap_cgroup(ent, &ctrl); |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 96 | |
KAMEZAWA Hiroyuki | e9e58a4 | 2010-03-15 00:34:57 -0400 | [diff] [blame] | 97 | spin_lock_irqsave(&ctrl->lock, flags); |
| 98 | retval = sc->id; |
| 99 | if (retval == old) |
| 100 | sc->id = new; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 101 | else |
KAMEZAWA Hiroyuki | e9e58a4 | 2010-03-15 00:34:57 -0400 | [diff] [blame] | 102 | retval = 0; |
| 103 | spin_unlock_irqrestore(&ctrl->lock, flags); |
| 104 | return retval; |
Daisuke Nishimura | 0249144 | 2010-03-10 15:22:17 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /** |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 108 | * swap_cgroup_record - record mem_cgroup for this swp_entry. |
| 109 | * @ent: swap entry to be recorded into |
Wanpeng Li | dad7557 | 2012-06-20 12:53:01 -0700 | [diff] [blame] | 110 | * @id: mem_cgroup to be recorded |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 111 | * |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 112 | * Returns old value at success, 0 at failure. |
| 113 | * (Of course, old value can be 0.) |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 114 | */ |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 115 | unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id) |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 116 | { |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 117 | struct swap_cgroup_ctrl *ctrl; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 118 | struct swap_cgroup *sc; |
KAMEZAWA Hiroyuki | a3b2d69 | 2009-04-02 16:57:45 -0700 | [diff] [blame] | 119 | unsigned short old; |
KAMEZAWA Hiroyuki | e9e58a4 | 2010-03-15 00:34:57 -0400 | [diff] [blame] | 120 | unsigned long flags; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 121 | |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 122 | sc = lookup_swap_cgroup(ent, &ctrl); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 123 | |
KAMEZAWA Hiroyuki | e9e58a4 | 2010-03-15 00:34:57 -0400 | [diff] [blame] | 124 | spin_lock_irqsave(&ctrl->lock, flags); |
| 125 | old = sc->id; |
| 126 | sc->id = id; |
| 127 | spin_unlock_irqrestore(&ctrl->lock, flags); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 128 | |
| 129 | return old; |
| 130 | } |
| 131 | |
| 132 | /** |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 133 | * lookup_swap_cgroup_id - lookup mem_cgroup id tied to swap entry |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 134 | * @ent: swap entry to be looked up. |
| 135 | * |
Hugh Dickins | b3ff8a2 | 2014-01-12 20:23:27 -0800 | [diff] [blame] | 136 | * 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] | 137 | */ |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 138 | unsigned short lookup_swap_cgroup_id(swp_entry_t ent) |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 139 | { |
Bob Liu | 9fb4b7c | 2012-01-12 17:18:48 -0800 | [diff] [blame] | 140 | return lookup_swap_cgroup(ent, NULL)->id; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | int 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 Kim | 33278f7 | 2011-05-26 16:25:30 -0700 | [diff] [blame] | 153 | length = DIV_ROUND_UP(max_pages, SC_PER_PAGE); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 154 | array_size = length * sizeof(void *); |
| 155 | |
Joe Perches | 8c1fec1 | 2011-05-28 10:36:34 -0700 | [diff] [blame] | 156 | array = vzalloc(array_size); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 157 | if (!array) |
| 158 | goto nomem; |
| 159 | |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 160 | ctrl = &swap_cgroup_ctrl[type]; |
| 161 | mutex_lock(&swap_cgroup_mutex); |
| 162 | ctrl->length = length; |
| 163 | ctrl->map = array; |
KAMEZAWA Hiroyuki | e9e58a4 | 2010-03-15 00:34:57 -0400 | [diff] [blame] | 164 | spin_lock_init(&ctrl->lock); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 165 | if (swap_cgroup_prepare(type)) { |
| 166 | /* memory shortage */ |
| 167 | ctrl->map = NULL; |
| 168 | ctrl->length = 0; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 169 | mutex_unlock(&swap_cgroup_mutex); |
Namhyung Kim | 6a5b18d | 2011-05-26 16:25:31 -0700 | [diff] [blame] | 170 | vfree(array); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 171 | goto nomem; |
| 172 | } |
| 173 | mutex_unlock(&swap_cgroup_mutex); |
| 174 | |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 175 | return 0; |
| 176 | nomem: |
| 177 | printk(KERN_INFO "couldn't allocate enough memory for swap_cgroup.\n"); |
| 178 | printk(KERN_INFO |
WANG Cong | 00a66d2 | 2011-07-25 17:12:12 -0700 | [diff] [blame] | 179 | "swap_cgroup can be disabled by swapaccount=0 boot option\n"); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 180 | return -ENOMEM; |
| 181 | } |
| 182 | |
| 183 | void swap_cgroup_swapoff(int type) |
| 184 | { |
Namhyung Kim | 6a5b18d | 2011-05-26 16:25:31 -0700 | [diff] [blame] | 185 | struct page **map; |
| 186 | unsigned long i, length; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 187 | struct swap_cgroup_ctrl *ctrl; |
| 188 | |
| 189 | if (!do_swap_account) |
| 190 | return; |
| 191 | |
| 192 | mutex_lock(&swap_cgroup_mutex); |
| 193 | ctrl = &swap_cgroup_ctrl[type]; |
Namhyung Kim | 6a5b18d | 2011-05-26 16:25:31 -0700 | [diff] [blame] | 194 | map = ctrl->map; |
| 195 | length = ctrl->length; |
| 196 | ctrl->map = NULL; |
| 197 | ctrl->length = 0; |
| 198 | mutex_unlock(&swap_cgroup_mutex); |
| 199 | |
| 200 | if (map) { |
| 201 | for (i = 0; i < length; i++) { |
| 202 | struct page *page = map[i]; |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 203 | if (page) |
| 204 | __free_page(page); |
| 205 | } |
Namhyung Kim | 6a5b18d | 2011-05-26 16:25:31 -0700 | [diff] [blame] | 206 | vfree(map); |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 207 | } |
KAMEZAWA Hiroyuki | 27a7faa | 2009-01-07 18:07:58 -0800 | [diff] [blame] | 208 | } |