2 * linux/mm/swap_state.c
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 * Swap reorganised 29.12.95, Stephen Tweedie
7 * Rewritten to use page cache, (C) 1998 Stephen Tweedie
11 #include <linux/kernel_stat.h>
12 #include <linux/swap.h>
13 #include <linux/swapctl.h>
14 #include <linux/init.h>
15 #include <linux/pagemap.h>
17 #include <asm/pgtable.h>
20 * Keep a reserved false inode which we will use to mark pages in the
21 * page cache are acting as swap cache instead of file cache.
23 * We only need a unique pointer to satisfy the page cache, but we'll
24 * reserve an entire zeroed inode structure for the purpose just to
25 * ensure that any mistaken dereferences of this structure cause a
28 struct inode swapper_inode
;
30 #ifdef SWAP_CACHE_INFO
31 unsigned long swap_cache_add_total
= 0;
32 unsigned long swap_cache_del_total
= 0;
33 unsigned long swap_cache_find_total
= 0;
34 unsigned long swap_cache_find_success
= 0;
36 void show_swap_cache_info(void)
38 printk("Swap cache: add %ld, delete %ld, find %ld/%ld\n",
41 swap_cache_find_success
, swap_cache_find_total
);
45 int add_to_swap_cache(struct page
*page
, unsigned long entry
)
47 #ifdef SWAP_CACHE_INFO
48 swap_cache_add_total
++;
51 printk("DebugVM: add_to_swap_cache(%08lx count %d, entry %08lx)\n",
52 page_address(page
), atomic_read(&page
->count
), entry
);
54 if (PageTestandSetSwapCache(page
)) {
55 printk(KERN_ERR
"swap_cache: replacing non-empty entry %08lx "
57 page
->offset
, page_address(page
));
61 printk(KERN_ERR
"swap_cache: replacing page-cached entry "
62 "on page %08lx\n", page_address(page
));
65 atomic_inc(&page
->count
);
66 page
->inode
= &swapper_inode
;
68 add_page_to_hash_queue(page
, &swapper_inode
, entry
);
69 add_page_to_inode_queue(&swapper_inode
, page
);
74 * Verify that a swap entry is valid and increment its swap map count.
76 * Note: if swap_map[] reaches SWAP_MAP_MAX the entries are treated as
77 * "permanent", but will be reclaimed by the next swapoff.
79 int swap_duplicate(unsigned long entry
)
81 struct swap_info_struct
* p
;
82 unsigned long offset
, type
;
87 type
= SWP_TYPE(entry
);
88 if (type
& SHM_SWP_TYPE
)
90 if (type
>= nr_swapfiles
)
93 offset
= SWP_OFFSET(entry
);
96 if (!p
->swap_map
[offset
])
99 * Entry is valid, so increment the map count.
101 if (p
->swap_map
[offset
] < SWAP_MAP_MAX
)
102 p
->swap_map
[offset
]++;
104 static int overflow
= 0;
107 "swap_duplicate: entry %08lx map count=%d\n",
108 entry
, p
->swap_map
[offset
]);
109 p
->swap_map
[offset
] = SWAP_MAP_MAX
;
113 printk("DebugVM: swap_duplicate(entry %08lx, count now %d)\n",
114 entry
, p
->swap_map
[offset
]);
121 "swap_duplicate: entry %08lx, nonexistent swap file\n", entry
);
125 "swap_duplicate: entry %08lx, offset exceeds max\n", entry
);
129 "swap_duplicate at %8p: entry %08lx, unused page\n",
130 __builtin_return_address(0), entry
);
134 int swap_count(unsigned long entry
)
136 struct swap_info_struct
* p
;
137 unsigned long offset
, type
;
142 type
= SWP_TYPE(entry
);
143 if (type
& SHM_SWP_TYPE
)
145 if (type
>= nr_swapfiles
)
147 p
= type
+ swap_info
;
148 offset
= SWP_OFFSET(entry
);
149 if (offset
>= p
->max
)
151 if (!p
->swap_map
[offset
])
153 retval
= p
->swap_map
[offset
];
155 printk("DebugVM: swap_count(entry %08lx, count %d)\n",
162 printk(KERN_ERR
"swap_count: null entry!\n");
166 "swap_count: entry %08lx, nonexistent swap file!\n", entry
);
170 "swap_count: entry %08lx, offset exceeds max!\n", entry
);
174 "swap_count at %8p: entry %08lx, unused page!\n",
175 __builtin_return_address(0), entry
);
179 static inline void remove_from_swap_cache(struct page
*page
)
182 printk ("VM: Removing swap cache page with zero inode hash "
183 "on page %08lx\n", page_address(page
));
186 if (page
->inode
!= &swapper_inode
) {
187 printk ("VM: Removing swap cache page with wrong inode hash "
188 "on page %08lx\n", page_address(page
));
192 printk("DebugVM: remove_from_swap_cache(%08lx count %d)\n",
193 page_address(page
), atomic_read(&page
->count
));
195 PageClearSwapCache (page
);
196 remove_inode_page(page
);
201 * This must be called only on pages that have
202 * been verified to be in the swap cache.
204 void delete_from_swap_cache(struct page
*page
)
206 long entry
= page
->offset
;
208 #ifdef SWAP_CACHE_INFO
209 swap_cache_del_total
++;
212 printk("DebugVM: delete_from_swap_cache(%08lx count %d, "
214 page_address(page
), atomic_read(&page
->count
), entry
);
216 remove_from_swap_cache (page
);
221 * Perform a free_page(), also freeing any swap cache associated with
222 * this page if it is the last user of the page.
225 void free_page_and_swap_cache(unsigned long addr
)
227 struct page
*page
= mem_map
+ MAP_NR(addr
);
230 * If we are the only user, then free up the swap cache.
232 if (PageSwapCache(page
) && !is_page_shared(page
)) {
233 delete_from_swap_cache(page
);
241 * Lookup a swap entry in the swap cache. We need to be careful about
242 * locked pages. A found page will be returned with its refcount
246 struct page
* lookup_swap_cache(unsigned long entry
)
250 #ifdef SWAP_CACHE_INFO
251 swap_cache_find_total
++;
254 found
= find_page(&swapper_inode
, entry
);
257 if (found
->inode
!= &swapper_inode
|| !PageSwapCache(found
))
259 if (!PageLocked(found
)) {
260 #ifdef SWAP_CACHE_INFO
261 swap_cache_find_success
++;
266 __wait_on_page(found
);
270 printk (KERN_ERR
"VM: Found a non-swapper swap page!\n");
276 * Locate a page of swap in physical memory, reserving swap cache space
277 * and reading the disk if it is not already cached. If wait==0, we are
278 * only doing readahead, so don't worry if the page is already locked.
280 * A failure return means that either the page allocation failed or that
281 * the swap entry is no longer in use.
284 struct page
* read_swap_cache_async(unsigned long entry
, int wait
)
286 struct page
*found_page
= 0, *new_page
;
287 unsigned long new_page_addr
;
290 printk("DebugVM: read_swap_cache_async entry %08lx%s\n",
291 entry
, wait
? ", wait" : "");
294 * Make sure the swap entry is still in use.
296 if (!swap_duplicate(entry
)) /* Account for the swap cache */
299 * Look for the page in the swap cache.
301 found_page
= lookup_swap_cache(entry
);
305 new_page_addr
= __get_free_page(GFP_USER
);
307 goto out_free_swap
; /* Out of memory */
308 new_page
= mem_map
+ MAP_NR(new_page_addr
);
311 * Check the swap cache again, in case we stalled above.
313 found_page
= lookup_swap_cache(entry
);
317 * Add it to the swap cache and read its contents.
319 if (!add_to_swap_cache(new_page
, entry
))
322 set_bit(PG_locked
, &new_page
->flags
);
323 rw_swap_page(READ
, entry
, (char *) new_page_addr
, wait
);
325 printk("DebugVM: read_swap_cache_async created "
326 "entry %08lx at %p\n",
327 entry
, (char *) page_address(new_page
));
332 __free_page(new_page
);