1 #include <linux/swap_cgroup.h>
2 #include <linux/vmalloc.h>
5 #include <linux/swapops.h> /* depends on mm.h include */
7 static DEFINE_MUTEX(swap_cgroup_mutex
);
8 struct swap_cgroup_ctrl
{
14 static struct swap_cgroup_ctrl swap_cgroup_ctrl
[MAX_SWAPFILES
];
19 #define SC_PER_PAGE (PAGE_SIZE/sizeof(struct swap_cgroup))
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.
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".
32 * TODO: we can push these buffers out to HIGHMEM.
36 * allocate buffer for swap_cgroup.
38 static int swap_cgroup_prepare(int type
)
41 struct swap_cgroup_ctrl
*ctrl
;
42 unsigned long idx
, max
;
44 ctrl
= &swap_cgroup_ctrl
[type
];
46 for (idx
= 0; idx
< ctrl
->length
; idx
++) {
47 page
= alloc_page(GFP_KERNEL
| __GFP_ZERO
);
50 ctrl
->map
[idx
] = page
;
52 if (!(idx
% SWAP_CLUSTER_MAX
))
58 for (idx
= 0; idx
< max
; idx
++)
59 __free_page(ctrl
->map
[idx
]);
64 static struct swap_cgroup
*__lookup_swap_cgroup(struct swap_cgroup_ctrl
*ctrl
,
68 struct swap_cgroup
*sc
;
70 mappage
= ctrl
->map
[offset
/ SC_PER_PAGE
];
71 sc
= page_address(mappage
);
72 return sc
+ offset
% SC_PER_PAGE
;
75 static struct swap_cgroup
*lookup_swap_cgroup(swp_entry_t ent
,
76 struct swap_cgroup_ctrl
**ctrlp
)
78 pgoff_t offset
= swp_offset(ent
);
79 struct swap_cgroup_ctrl
*ctrl
;
81 ctrl
= &swap_cgroup_ctrl
[swp_type(ent
)];
84 return __lookup_swap_cgroup(ctrl
, offset
);
88 * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
89 * @ent: swap entry to be cmpxchged
93 * Returns old id at success, 0 at failure.
94 * (There is no mem_cgroup using 0 as its id)
96 unsigned short swap_cgroup_cmpxchg(swp_entry_t ent
,
97 unsigned short old
, unsigned short new)
99 struct swap_cgroup_ctrl
*ctrl
;
100 struct swap_cgroup
*sc
;
102 unsigned short retval
;
104 sc
= lookup_swap_cgroup(ent
, &ctrl
);
106 spin_lock_irqsave(&ctrl
->lock
, flags
);
112 spin_unlock_irqrestore(&ctrl
->lock
, flags
);
117 * swap_cgroup_record - record mem_cgroup for a set of swap entries
118 * @ent: the first swap entry to be recorded into
119 * @id: mem_cgroup to be recorded
120 * @nr_ents: number of swap entries to be recorded
122 * Returns old value at success, 0 at failure.
123 * (Of course, old value can be 0.)
125 unsigned short swap_cgroup_record(swp_entry_t ent
, unsigned short id
,
126 unsigned int nr_ents
)
128 struct swap_cgroup_ctrl
*ctrl
;
129 struct swap_cgroup
*sc
;
132 pgoff_t offset
= swp_offset(ent
);
133 pgoff_t end
= offset
+ nr_ents
;
135 sc
= lookup_swap_cgroup(ent
, &ctrl
);
137 spin_lock_irqsave(&ctrl
->lock
, flags
);
140 VM_BUG_ON(sc
->id
!= old
);
145 if (offset
% SC_PER_PAGE
)
148 sc
= __lookup_swap_cgroup(ctrl
, offset
);
150 spin_unlock_irqrestore(&ctrl
->lock
, flags
);
156 * lookup_swap_cgroup_id - lookup mem_cgroup id tied to swap entry
157 * @ent: swap entry to be looked up.
159 * Returns ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
161 unsigned short lookup_swap_cgroup_id(swp_entry_t ent
)
163 return lookup_swap_cgroup(ent
, NULL
)->id
;
166 int swap_cgroup_swapon(int type
, unsigned long max_pages
)
169 unsigned long array_size
;
170 unsigned long length
;
171 struct swap_cgroup_ctrl
*ctrl
;
173 if (!do_swap_account
)
176 length
= DIV_ROUND_UP(max_pages
, SC_PER_PAGE
);
177 array_size
= length
* sizeof(void *);
179 array
= vzalloc(array_size
);
183 ctrl
= &swap_cgroup_ctrl
[type
];
184 mutex_lock(&swap_cgroup_mutex
);
185 ctrl
->length
= length
;
187 spin_lock_init(&ctrl
->lock
);
188 if (swap_cgroup_prepare(type
)) {
189 /* memory shortage */
192 mutex_unlock(&swap_cgroup_mutex
);
196 mutex_unlock(&swap_cgroup_mutex
);
200 pr_info("couldn't allocate enough memory for swap_cgroup\n");
201 pr_info("swap_cgroup can be disabled by swapaccount=0 boot option\n");
205 void swap_cgroup_swapoff(int type
)
208 unsigned long i
, length
;
209 struct swap_cgroup_ctrl
*ctrl
;
211 if (!do_swap_account
)
214 mutex_lock(&swap_cgroup_mutex
);
215 ctrl
= &swap_cgroup_ctrl
[type
];
217 length
= ctrl
->length
;
220 mutex_unlock(&swap_cgroup_mutex
);
223 for (i
= 0; i
< length
; i
++) {
224 struct page
*page
= map
[i
];
227 if (!(i
% SWAP_CLUSTER_MAX
))