swap_cgroup.c 4.7 KB
Newer Older
1
#include <linux/swap_cgroup.h>
2
#include <linux/vmalloc.h>
3
#include <linux/mm.h>
4

5
#include <linux/swapops.h> /* depends on mm.h include */
6 7 8 9 10

static DEFINE_MUTEX(swap_cgroup_mutex);
struct swap_cgroup_ctrl {
	struct page **map;
	unsigned long length;
11
	spinlock_t	lock;
12 13
};

14
static struct swap_cgroup_ctrl swap_cgroup_ctrl[MAX_SWAPFILES];
15 16

struct swap_cgroup {
17
	unsigned short		id;
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
};
#define SC_PER_PAGE	(PAGE_SIZE/sizeof(struct swap_cgroup))

/*
 * SwapCgroup implements "lookup" and "exchange" operations.
 * In typical usage, this swap_cgroup is accessed via memcg's charge/uncharge
 * against SwapCache. At swap_free(), this is accessed directly from swap.
 *
 * This means,
 *  - we have no race in "exchange" when we're accessed via SwapCache because
 *    SwapCache(and its swp_entry) is under lock.
 *  - When called via swap_free(), there is no user of this entry and no race.
 * Then, we don't need lock around "exchange".
 *
 * TODO: we can push these buffers out to HIGHMEM.
 */

/*
 * allocate buffer for swap_cgroup.
 */
static int swap_cgroup_prepare(int type)
{
	struct page *page;
	struct swap_cgroup_ctrl *ctrl;
	unsigned long idx, max;

	ctrl = &swap_cgroup_ctrl[type];

	for (idx = 0; idx < ctrl->length; idx++) {
		page = alloc_page(GFP_KERNEL | __GFP_ZERO);
		if (!page)
			goto not_enough_page;
		ctrl->map[idx] = page;
	}
	return 0;
not_enough_page:
	max = idx;
	for (idx = 0; idx < max; idx++)
		__free_page(ctrl->map[idx]);

	return -ENOMEM;
}

61 62 63 64 65 66
static struct swap_cgroup *lookup_swap_cgroup(swp_entry_t ent,
					struct swap_cgroup_ctrl **ctrlp)
{
	pgoff_t offset = swp_offset(ent);
	struct swap_cgroup_ctrl *ctrl;
	struct page *mappage;
67
	struct swap_cgroup *sc;
68 69 70 71 72 73

	ctrl = &swap_cgroup_ctrl[swp_type(ent)];
	if (ctrlp)
		*ctrlp = ctrl;

	mappage = ctrl->map[offset / SC_PER_PAGE];
74 75
	sc = page_address(mappage);
	return sc + offset % SC_PER_PAGE;
76 77
}

78 79
/**
 * swap_cgroup_cmpxchg - cmpxchg mem_cgroup's id for this swp_entry.
W
Wanpeng Li 已提交
80
 * @ent: swap entry to be cmpxchged
81 82 83 84
 * @old: old id
 * @new: new id
 *
 * Returns old id at success, 0 at failure.
L
Lucas De Marchi 已提交
85
 * (There is no mem_cgroup using 0 as its id)
86 87 88 89 90 91
 */
unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
					unsigned short old, unsigned short new)
{
	struct swap_cgroup_ctrl *ctrl;
	struct swap_cgroup *sc;
92 93
	unsigned long flags;
	unsigned short retval;
94

95
	sc = lookup_swap_cgroup(ent, &ctrl);
96

97 98 99 100
	spin_lock_irqsave(&ctrl->lock, flags);
	retval = sc->id;
	if (retval == old)
		sc->id = new;
101
	else
102 103 104
		retval = 0;
	spin_unlock_irqrestore(&ctrl->lock, flags);
	return retval;
105 106
}

107 108 109
/**
 * swap_cgroup_record - record mem_cgroup for this swp_entry.
 * @ent: swap entry to be recorded into
W
Wanpeng Li 已提交
110
 * @id: mem_cgroup to be recorded
111
 *
112 113
 * Returns old value at success, 0 at failure.
 * (Of course, old value can be 0.)
114
 */
115
unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
116 117 118
{
	struct swap_cgroup_ctrl *ctrl;
	struct swap_cgroup *sc;
119
	unsigned short old;
120
	unsigned long flags;
121

122
	sc = lookup_swap_cgroup(ent, &ctrl);
123

124 125 126 127
	spin_lock_irqsave(&ctrl->lock, flags);
	old = sc->id;
	sc->id = id;
	spin_unlock_irqrestore(&ctrl->lock, flags);
128 129 130 131 132

	return old;
}

/**
133
 * lookup_swap_cgroup_id - lookup mem_cgroup id tied to swap entry
134 135
 * @ent: swap entry to be looked up.
 *
136
 * Returns ID of mem_cgroup at success. 0 at failure. (0 is invalid ID)
137
 */
138
unsigned short lookup_swap_cgroup_id(swp_entry_t ent)
139
{
140
	return lookup_swap_cgroup(ent, NULL)->id;
141 142 143 144 145 146 147 148 149 150 151 152
}

int swap_cgroup_swapon(int type, unsigned long max_pages)
{
	void *array;
	unsigned long array_size;
	unsigned long length;
	struct swap_cgroup_ctrl *ctrl;

	if (!do_swap_account)
		return 0;

153
	length = DIV_ROUND_UP(max_pages, SC_PER_PAGE);
154 155
	array_size = length * sizeof(void *);

156
	array = vzalloc(array_size);
157 158 159 160 161 162 163
	if (!array)
		goto nomem;

	ctrl = &swap_cgroup_ctrl[type];
	mutex_lock(&swap_cgroup_mutex);
	ctrl->length = length;
	ctrl->map = array;
164
	spin_lock_init(&ctrl->lock);
165 166 167 168 169
	if (swap_cgroup_prepare(type)) {
		/* memory shortage */
		ctrl->map = NULL;
		ctrl->length = 0;
		mutex_unlock(&swap_cgroup_mutex);
170
		vfree(array);
171 172 173 174 175 176
		goto nomem;
	}
	mutex_unlock(&swap_cgroup_mutex);

	return 0;
nomem:
177 178
	pr_info("couldn't allocate enough memory for swap_cgroup\n");
	pr_info("swap_cgroup can be disabled by swapaccount=0 boot option\n");
179 180 181 182 183
	return -ENOMEM;
}

void swap_cgroup_swapoff(int type)
{
184 185
	struct page **map;
	unsigned long i, length;
186 187 188 189 190 191 192
	struct swap_cgroup_ctrl *ctrl;

	if (!do_swap_account)
		return;

	mutex_lock(&swap_cgroup_mutex);
	ctrl = &swap_cgroup_ctrl[type];
193 194 195 196 197 198 199 200 201
	map = ctrl->map;
	length = ctrl->length;
	ctrl->map = NULL;
	ctrl->length = 0;
	mutex_unlock(&swap_cgroup_mutex);

	if (map) {
		for (i = 0; i < length; i++) {
			struct page *page = map[i];
202 203
			if (page)
				__free_page(page);
204 205
			if (!(i % SWAP_CLUSTER_MAX))
				cond_resched();
206
		}
207
		vfree(map);
208 209
	}
}