summaryrefslogtreecommitdiffstats
path: root/mm/swap_state.c
blob: 401c7a1fc31eb572118b98580944e62d20e719bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 *  linux/mm/swap_state.c
 *
 *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
 *  Swap reorganised 29.12.95, Stephen Tweedie
 *
 *  Rewritten to use page cache, (C) 1998 Stephen Tweedie
 */

#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/head.h>
#include <linux/kernel.h>
#include <linux/kernel_stat.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/stat.h>
#include <linux/swap.h>
#include <linux/fs.h>
#include <linux/swapctl.h>
#include <linux/init.h>
#include <linux/pagemap.h>

#include <asm/bitops.h>
#include <asm/pgtable.h>

#ifdef SWAP_CACHE_INFO
unsigned long swap_cache_add_total = 0;
unsigned long swap_cache_add_success = 0;
unsigned long swap_cache_del_total = 0;
unsigned long swap_cache_del_success = 0;
unsigned long swap_cache_find_total = 0;
unsigned long swap_cache_find_success = 0;

/* 
 * Keep a reserved false inode which we will use to mark pages in the
 * page cache are acting as swap cache instead of file cache. 
 *
 * We only need a unique pointer to satisfy the page cache, but we'll
 * reserve an entire zeroed inode structure for the purpose just to
 * ensure that any mistaken dereferences of this structure cause a
 * kernel oops.
 */
struct inode swapper_inode;


void show_swap_cache_info(void)
{
	printk("Swap cache: add %ld/%ld, delete %ld/%ld, find %ld/%ld\n",
		swap_cache_add_total, swap_cache_add_success, 
		swap_cache_del_total, swap_cache_del_success,
		swap_cache_find_total, swap_cache_find_success);
}
#endif

int add_to_swap_cache(struct page *page, unsigned long entry)
{
#ifdef SWAP_CACHE_INFO
	swap_cache_add_total++;
#endif
#ifdef DEBUG_SWAP
	printk("DebugVM: add_to_swap_cache(%08lx count %d, entry %08lx)\n",
	       page_address(page), atomic_read(&page->count), entry);
#endif
	if (PageTestandSetSwapCache(page)) {
		printk("swap_cache: replacing non-empty entry %08lx "
		       "on page %08lx\n",
		       page->offset, page_address(page));
		return 0;
	}
	if (page->inode) {
		printk("swap_cache: replacing page-cached entry "
		       "on page %08lx\n", page_address(page));
		return 0;
	}
	atomic_inc(&page->count);
	page->inode = &swapper_inode;
	page->offset = entry;
	add_page_to_hash_queue(page, &swapper_inode, entry);
	add_page_to_inode_queue(&swapper_inode, page);
#ifdef SWAP_CACHE_INFO
	swap_cache_add_success++;
#endif
	return 1;
}

/*
 * If swap_map[] reaches 127, the entries are treated as "permanent".
 */
void swap_duplicate(unsigned long entry)
{
	struct swap_info_struct * p;
	unsigned long offset, type;

	if (!entry)
		goto out;
	type = SWP_TYPE(entry);
	if (type & SHM_SWP_TYPE)
		goto out;
	if (type >= nr_swapfiles)
		goto bad_file;
	p = type + swap_info;
	offset = SWP_OFFSET(entry);
	if (offset >= p->max)
		goto bad_offset;
	if (!p->swap_map[offset])
		goto bad_unused;
	if (p->swap_map[offset] < 126)
		p->swap_map[offset]++;
	else {
		static int overflow = 0;
		if (overflow++ < 5)
			printk("swap_duplicate: entry %08lx map count=%d\n",
				entry, p->swap_map[offset]);
		p->swap_map[offset] = 127;
	}
#ifdef DEBUG_SWAP
	printk("DebugVM: swap_duplicate(entry %08lx, count now %d)\n",
	       entry, p->swap_map[offset]);
#endif
out:
	return;

bad_file:
	printk("swap_duplicate: Trying to duplicate nonexistent swap-page\n");
	goto out;
bad_offset:
	printk("swap_duplicate: offset exceeds max\n");
	goto out;
bad_unused:
	printk("swap_duplicate at %8p: unused page\n", 
	       __builtin_return_address(0));
	goto out;
}


void remove_from_swap_cache(struct page *page)
{
	if (!page->inode) {
		printk ("VM: Removing swap cache page with zero inode hash "
			"on page %08lx\n", page_address(page));
		return;
	}
	if (page->inode != &swapper_inode) {
		printk ("VM: Removing swap cache page with wrong inode hash "
			"on page %08lx\n", page_address(page));
	}
	/*
	 * This is a legal case, but warn about it.
	 */
	if (atomic_read(&page->count) == 1) {
		printk (KERN_WARNING 
			"VM: Removing page cache on unshared page %08lx\n", 
			page_address(page));
	}

#ifdef DEBUG_SWAP
	printk("DebugVM: remove_from_swap_cache(%08lx count %d)\n",
	       page_address(page), atomic_read(&page->count));
#endif
	PageClearSwapCache (page);
	remove_inode_page(page);
}


int delete_from_swap_cache(struct page *page)
{
#ifdef SWAP_CACHE_INFO
	swap_cache_del_total++;
#endif	
	if (PageSwapCache (page))  {
		long entry = page->offset;
#ifdef SWAP_CACHE_INFO
		swap_cache_del_success++;
#endif
#ifdef DEBUG_SWAP
		printk("DebugVM: delete_from_swap_cache(%08lx count %d, "
		       "entry %08lx)\n",
		       page_address(page), atomic_read(&page->count), entry);
#endif
		remove_from_swap_cache (page);
		swap_free (entry);
		return 1;
	}
	return 0;
}

/* 
 * Perform a free_page(), also freeing any swap cache associated with
 * this page if it is the last user of the page. 
 */

void free_page_and_swap_cache(unsigned long addr)
{
	struct page *page = mem_map + MAP_NR(addr);
	/* 
	 * If we are the only user, then free up the swap cache. 
	 */
	if (PageSwapCache(page) && !is_page_shared(page)) {
		delete_from_swap_cache(page);
	}
	
	free_user_page(page, addr);
}


/*
 * Lookup a swap entry in the swap cache.  We need to be careful about
 * locked pages.  A found page will be returned with its refcount
 * incremented.
 */

static struct page * lookup_swap_cache(unsigned long entry)
{
	struct page *found;
	
	while (1) {
		found = find_page(&swapper_inode, entry);
		if (!found)
			return 0;
		if (found->inode != &swapper_inode 
		    || !PageSwapCache(found)) {
			__free_page(found);
			printk ("VM: Found a non-swapper swap page!\n");
			return 0;
		}
		if (!PageLocked(found))
			return found;
		__free_page(found);
		__wait_on_page(found);
	}
}

/* 
 * Locate a page of swap in physical memory, reserving swap cache space
 * and reading the disk if it is not already cached.  If wait==0, we are
 * only doing readahead, so don't worry if the page is already locked.
 */

struct page * read_swap_cache_async(unsigned long entry, unsigned long addr, int wait)
{
	struct page *found_page, *new_page = 0;
	unsigned long new_page_addr = 0;
	
#ifdef DEBUG_SWAP
	printk("DebugVM: read_swap_cache_async entry %08lx%s\n",
	       entry, wait ? ", wait" : "");
#endif
repeat:
	found_page = lookup_swap_cache(entry);
	if (found_page) {
		if (new_page)
			__free_page(new_page);
		return found_page;
	}

	/* The entry is not present.  Lock down a new page, add it to
	 * the swap cache and read its contents. */
	if (!new_page) {
		new_page_addr = get_user_page(addr);
		if (!new_page_addr)
			return 0;	/* Out of memory */
		new_page = mem_map + MAP_NR(new_page_addr);
		goto repeat;		/* We might have stalled */
	}
	
	if (!add_to_swap_cache(new_page, entry)) {
		free_page(new_page_addr);
		return 0;
	}
	swap_duplicate(entry);		/* Account for the swap cache */
	set_bit(PG_locked, &new_page->flags);
	rw_swap_page(READ, entry, (char *) new_page_addr, wait);
#ifdef DEBUG_SWAP
	printk("DebugVM: read_swap_cache_async created "
	       "entry %08lx at %p\n",
	       entry, (char *) page_address(new_page));
#endif
	return new_page;
}