1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/swap_cgroup.h>
3 #include <linux/vmalloc.h>
6 #include <linux/swapops.h> /* depends on mm.h include */
8 static DEFINE_MUTEX(swap_cgroup_mutex);
9 struct swap_cgroup_ctrl {
15 static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES];
20 #define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup))
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.
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".
33 * TODO: we can push these buffers out to HIGHMEM.
37 * allocate buffer for swap_cgroup.
39 static int swap_cgroup_prepare(int type)
42 struct swap_cgroup_ctrl *ctrl;
43 unsigned long idx, max;
45 ctrl = &swap_cgroup_ctrl[type];
47 for (idx = 0; idx < ctrl->length; idx++) {
48 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
51 ctrl->map[idx] = page;
53 if (!(idx % SWAP_CLUSTER_MAX))
59 for (idx = 0; idx < max; idx++)
60 __free_page(ctrl->map[idx]);
65 static struct swap_cgroup *__lookup_swap_cgroup(struct swap_cgroup_ctrl *ctrl,
69 struct swap_cgroup *sc;
71 mappage = ctrl->map[offset / SC_PER_PAGE];
72 sc = page_address(mappage);
73 return sc + offset % SC_PER_PAGE;
76 static struct swap_cgroup *lookup_swap_cgroup(swp_entry_t ent,
77 struct swap_cgroup_ctrl **ctrlp)
79 pgoff_t offset = swp_offset(ent);
80 struct swap_cgroup_ctrl *ctrl;
82 ctrl = &swap_cgroup_ctrl[swp_type(ent)];
85 return __lookup_swap_cgroup(ctrl, offset);
89 * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
90 * @ent: swap entry to be cmpxchged
94 * Returns old id at success, 0 at failure.
95 * (There is no mem_cgroup using 0 as its id)
97 unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
98 unsigned short old, unsigned short new)
100 struct swap_cgroup_ctrl *ctrl;
101 struct swap_cgroup *sc;
103 unsigned short retval;
105 sc = lookup_swap_cgroup(ent, &ctrl);
107 spin_lock_irqsave(&ctrl->lock, flags);
113 spin_unlock_irqrestore(&ctrl->lock, flags);
118 * swap_cgroup_record - record mem_cgroup for a set of swap entries
119 * @ent: the first swap entry to be recorded into
120 * @id: mem_cgroup to be recorded
121 * @nr_ents: number of swap entries to be recorded
123 * Returns old value at success, 0 at failure.
124 * (Of course, old value can be 0.)
126 unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id,
127 unsigned int nr_ents)
129 struct swap_cgroup_ctrl *ctrl;
130 struct swap_cgroup *sc;
133 pgoff_t offset = swp_offset(ent);
134 pgoff_t end = offset + nr_ents;
136 sc = lookup_swap_cgroup(ent, &ctrl);
138 spin_lock_irqsave(&ctrl->lock, flags);
141 VM_BUG_ON(sc->id != old);
146 if (offset % SC_PER_PAGE)
149 sc = __lookup_swap_cgroup(ctrl, offset);
151 spin_unlock_irqrestore(&ctrl->lock, flags);
157 * lookup_swap_cgroup_id - lookup mem_cgroup id tied to swap entry
158 * @ent: swap entry to be looked up.
160 * Returns ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
162 unsigned short lookup_swap_cgroup_id(swp_entry_t ent)
164 return lookup_swap_cgroup(ent, NULL)->id;
167 int swap_cgroup_swapon(int type, unsigned long max_pages)
170 unsigned long array_size;
171 unsigned long length;
172 struct swap_cgroup_ctrl *ctrl;
174 if (!do_swap_account)
177 length = DIV_ROUND_UP(max_pages, SC_PER_PAGE);
178 array_size = length * sizeof(void *);
180 array = vzalloc(array_size);
184 ctrl = &swap_cgroup_ctrl[type];
185 mutex_lock(&swap_cgroup_mutex);
186 ctrl->length = length;
188 spin_lock_init(&ctrl->lock);
189 if (swap_cgroup_prepare(type)) {
190 /* memory shortage */
193 mutex_unlock(&swap_cgroup_mutex);
197 mutex_unlock(&swap_cgroup_mutex);
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");
206 void swap_cgroup_swapoff(int type)
209 unsigned long i, length;
210 struct swap_cgroup_ctrl *ctrl;
212 if (!do_swap_account)
215 mutex_lock(&swap_cgroup_mutex);
216 ctrl = &swap_cgroup_ctrl[type];
218 length = ctrl->length;
221 mutex_unlock(&swap_cgroup_mutex);
224 for (i = 0; i < length; i++) {
225 struct page *page = map[i];
228 if (!(i % SWAP_CLUSTER_MAX))