diff -urN linux-rmap10-virgin/drivers/block/rd.c linux-ratmerge/drivers/block/rd.c --- linux-rmap10-virgin/drivers/block/rd.c Fri Dec 21 09:41:53 2001 +++ linux-ratmerge/drivers/block/rd.c Sat Jan 5 14:07:02 2002 @@ -243,7 +243,6 @@ do { int count; - struct page ** hash; struct page * page; char * src, * dst; int unlock = 0; @@ -253,8 +252,7 @@ count = size; size -= count; - hash = page_hash(mapping, index); - page = __find_get_page(mapping, index, hash); + page = find_get_page(mapping, index); if (!page) { page = grab_cache_page(mapping, index); err = -ENOMEM; diff -urN linux-rmap10-virgin/fs/inode.c linux-ratmerge/fs/inode.c --- linux-rmap10-virgin/fs/inode.c Fri Dec 21 09:41:55 2001 +++ linux-ratmerge/fs/inode.c Sat Jan 5 14:07:02 2002 @@ -109,6 +109,7 @@ INIT_LIST_HEAD(&inode->i_devices); sema_init(&inode->i_sem, 1); sema_init(&inode->i_zombie, 1); + INIT_RAT_ROOT(&inode->i_data.page_tree, GFP_ATOMIC); spin_lock_init(&inode->i_data.i_shared_lock); } } diff -urN linux-rmap10-virgin/include/linux/fs.h linux-ratmerge/include/linux/fs.h --- linux-rmap10-virgin/include/linux/fs.h Sun Jan 6 06:15:54 2002 +++ linux-ratmerge/include/linux/fs.h Sun Jan 6 04:21:33 2002 @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -394,6 +395,7 @@ }; struct address_space { + struct rat_root page_tree; /* radix tree of all pages */ struct list_head clean_pages; /* list of clean pages */ struct list_head dirty_pages; /* list of dirty pages */ struct list_head locked_pages; /* list of locked pages */ diff -urN linux-rmap10-virgin/include/linux/mm.h linux-ratmerge/include/linux/mm.h --- linux-rmap10-virgin/include/linux/mm.h Sun Jan 6 06:15:54 2002 +++ linux-ratmerge/include/linux/mm.h Sun Jan 6 04:21:53 2002 @@ -151,8 +151,6 @@ struct list_head list; /* ->mapping has some page lists. */ struct address_space *mapping; /* The inode (or ...) we belong to. */ unsigned long index; /* Our offset within mapping. */ - struct page *next_hash; /* Next page sharing our hash bucket in - the pagecache hash table. */ atomic_t count; /* Usage count, see below. */ unsigned long flags; /* atomic flags, some possibly updated asynchronously */ @@ -162,7 +160,6 @@ unsigned char zone; /* Memory zone the page belongs to. */ struct pte_chain * pte_chain; /* Reverse pte mapping pointer. */ wait_queue_head_t wait; /* Page locked? Stand in line... */ - struct page **pprev_hash; /* Complement to *next_hash. */ struct buffer_head * buffers; /* Buffer maps us to a disk block. */ /* @@ -244,9 +241,8 @@ * using the page->list list_head. These fields are also used for * freelist managemet (when page->count==0). * - * There is also a hash table mapping (mapping,index) to the page - * in memory if present. The lists for this hash table use the fields - * page->next_hash and page->pprev_hash. + * There is also a per-mapping radix tree mapping index to the page + * in memory if present. The tree is rooted at mapping->root. * * All process pages can do I/O: * - inode pages may need to be read from disk, @@ -578,6 +574,24 @@ return 1; else return 0; +} + +static inline void add_page_to_inode_queue(struct address_space *mapping, struct page * page) +{ + struct list_head *head = &mapping->clean_pages; + + mapping->nrpages++; + list_add(&page->list, head); + page->mapping = mapping; +} + +static inline void remove_page_from_inode_queue(struct page * page) +{ + struct address_space * mapping = page->mapping; + + mapping->nrpages--; + list_del(&page->list); + page->mapping = NULL; } struct zone_t; diff -urN linux-rmap10-virgin/include/linux/pagemap.h linux-ratmerge/include/linux/pagemap.h --- linux-rmap10-virgin/include/linux/pagemap.h Thu Nov 22 11:46:44 2001 +++ linux-ratmerge/include/linux/pagemap.h Sun Jan 6 04:27:58 2002 @@ -41,53 +41,22 @@ */ #define page_cache_entry(x) virt_to_page(x) -extern unsigned int page_hash_bits; -#define PAGE_HASH_BITS (page_hash_bits) -#define PAGE_HASH_SIZE (1 << PAGE_HASH_BITS) +extern atomic_t page_cache_size; /* # of pages currently in the page cache */ -extern atomic_t page_cache_size; /* # of pages currently in the hash table */ -extern struct page **page_hash_table; - -extern void page_cache_init(unsigned long); - -/* - * We use a power-of-two hash table to avoid a modulus, - * and get a reasonable hash by knowing roughly how the - * inode pointer and indexes are distributed (ie, we - * roughly know which bits are "significant") - * - * For the time being it will work for struct address_space too (most of - * them sitting inside the inodes). We might want to change it later. - */ -static inline unsigned long _page_hashfn(struct address_space * mapping, unsigned long index) -{ -#define i (((unsigned long) mapping)/(sizeof(struct inode) & ~ (sizeof(struct inode) - 1))) -#define s(x) ((x)+((x)>>PAGE_HASH_BITS)) - return s(i+index) & (PAGE_HASH_SIZE-1); -#undef i -#undef s -} - -#define page_hash(mapping,index) (page_hash_table+_page_hashfn(mapping,index)) - -extern struct page * __find_get_page(struct address_space *mapping, - unsigned long index, struct page **hash); -#define find_get_page(mapping, index) \ - __find_get_page(mapping, index, page_hash(mapping, index)) -extern struct page * __find_lock_page (struct address_space * mapping, - unsigned long index, struct page **hash); +extern struct page * find_get_page(struct address_space *mapping, + unsigned long index); +extern struct page * find_lock_page(struct address_space *mapping, + unsigned long index); extern struct page * find_or_create_page(struct address_space *mapping, unsigned long index, unsigned int gfp_mask); extern void FASTCALL(lock_page(struct page *page)); extern void FASTCALL(unlock_page(struct page *page)); -#define find_lock_page(mapping, index) \ - __find_lock_page(mapping, index, page_hash(mapping, index)) extern struct page *find_trylock_page(struct address_space *, unsigned long); -extern void add_to_page_cache(struct page * page, struct address_space *mapping, unsigned long index); -extern void add_to_page_cache_locked(struct page * page, struct address_space *mapping, unsigned long index); -extern int add_to_page_cache_unique(struct page * page, struct address_space *mapping, unsigned long index, struct page **hash); +extern int add_to_page_cache(struct page * page, struct address_space *mapping, unsigned long index); +extern int add_to_page_cache_locked(struct page * page, struct address_space *mapping, unsigned long index); +extern int add_to_page_cache_unique(struct page * page, struct address_space *mapping, unsigned long index); extern void ___wait_on_page(struct page *); diff -urN linux-rmap10-virgin/include/linux/rat.h linux-ratmerge/include/linux/rat.h --- linux-rmap10-virgin/include/linux/rat.h Wed Dec 31 16:00:00 1969 +++ linux-ratmerge/include/linux/rat.h Sat Jan 5 14:07:02 2002 @@ -0,0 +1,26 @@ +#ifndef _LINUX_RAT_H +#define _LINUX_RAT_H + +struct rat_node; + +#define RAT_SLOT_RESERVED ((void *)~0UL) + +struct rat_root { + unsigned int height; + int gfp_mask; + struct rat_node *rnode; +}; + +#define INIT_RAT_ROOT(root, mask) \ +do { \ + (root)->height = 0; \ + (root)->gfp_mask = (mask); \ + (root)->rnode = NULL; \ +} while (0) + +extern int rat_reserve(struct rat_root *, unsigned long, void ***); +extern int rat_insert(struct rat_root *, unsigned long, void *); +extern void *rat_lookup(struct rat_root *, unsigned long); +extern int rat_delete(struct rat_root *, unsigned long); + +#endif /* _LINUX_RAT_H */ diff -urN linux-rmap10-virgin/include/linux/swap.h linux-ratmerge/include/linux/swap.h --- linux-rmap10-virgin/include/linux/swap.h Sun Jan 6 06:15:54 2002 +++ linux-ratmerge/include/linux/swap.h Sun Jan 6 04:21:28 2002 @@ -97,7 +97,7 @@ struct task_struct; struct vm_area_struct; struct sysinfo; - +struct address_space; struct zone_t; /* linux/mm/rmap.c */ @@ -146,6 +146,9 @@ extern int add_to_swap(struct page *); extern void __delete_from_swap_cache(struct page *page); extern void delete_from_swap_cache(struct page *page); +extern int move_to_swap_cache(struct page *page, swp_entry_t entry); +extern int move_from_swap_cache(struct page *page, unsigned long index, + struct address_space *mapping); extern void free_page_and_swap_cache(struct page *page); extern struct page * lookup_swap_cache(swp_entry_t); extern struct page * read_swap_cache_async(swp_entry_t); diff -urN linux-rmap10-virgin/init/main.c linux-ratmerge/init/main.c --- linux-rmap10-virgin/init/main.c Fri Dec 21 09:42:04 2001 +++ linux-ratmerge/init/main.c Sat Jan 5 14:07:02 2002 @@ -94,6 +94,7 @@ extern void sysctl_init(void); extern void signals_init(void); extern int init_pcmcia_ds(void); +extern void ratcache_init(void) __init; extern void free_initmem(void); @@ -599,7 +600,7 @@ proc_caches_init(); vfs_caches_init(mempages); buffer_init(mempages); - page_cache_init(mempages); + ratcache_init(); #if defined(CONFIG_ARCH_S390) ccwcache_init(); #endif diff -urN linux-rmap10-virgin/kernel/ksyms.c linux-ratmerge/kernel/ksyms.c --- linux-rmap10-virgin/kernel/ksyms.c Fri Dec 21 09:42:04 2001 +++ linux-ratmerge/kernel/ksyms.c Sat Jan 5 14:07:02 2002 @@ -216,8 +216,6 @@ EXPORT_SYMBOL(generic_file_mmap); EXPORT_SYMBOL(generic_ro_fops); EXPORT_SYMBOL(generic_buffer_fdatasync); -EXPORT_SYMBOL(page_hash_bits); -EXPORT_SYMBOL(page_hash_table); EXPORT_SYMBOL(file_lock_list); EXPORT_SYMBOL(locks_init_lock); EXPORT_SYMBOL(locks_copy_lock); @@ -252,8 +250,8 @@ EXPORT_SYMBOL(__pollwait); EXPORT_SYMBOL(poll_freewait); EXPORT_SYMBOL(ROOT_DEV); -EXPORT_SYMBOL(__find_get_page); -EXPORT_SYMBOL(__find_lock_page); +EXPORT_SYMBOL(find_get_page); +EXPORT_SYMBOL(find_lock_page); EXPORT_SYMBOL(grab_cache_page); EXPORT_SYMBOL(grab_cache_page_nowait); EXPORT_SYMBOL(read_cache_page); diff -urN linux-rmap10-virgin/lib/Makefile linux-ratmerge/lib/Makefile --- linux-rmap10-virgin/lib/Makefile Mon Sep 17 15:31:15 2001 +++ linux-ratmerge/lib/Makefile Sat Jan 5 14:07:02 2002 @@ -8,9 +8,10 @@ L_TARGET := lib.a -export-objs := cmdline.o dec_and_lock.o rwsem-spinlock.o rwsem.o +export-objs := cmdline.o dec_and_lock.o rwsem-spinlock.o rwsem.o rat.o -obj-y := errno.o ctype.o string.o vsprintf.o brlock.o cmdline.o bust_spinlocks.o rbtree.o +obj-y := errno.o ctype.o string.o vsprintf.o brlock.o cmdline.o \ + bust_spinlocks.o rbtree.o rat.o obj-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o obj-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o diff -urN linux-rmap10-virgin/lib/rat.c linux-ratmerge/lib/rat.c --- linux-rmap10-virgin/lib/rat.c Wed Dec 31 16:00:00 1969 +++ linux-ratmerge/lib/rat.c Sat Jan 5 14:07:02 2002 @@ -0,0 +1,281 @@ +/* + * Copyright (C) 2001 Momchil Velikov + * Portions Copyright (C) 2001 Christoph Hellwig + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2, or (at + * your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include +#include +#include +#include +#include +#include + + +/* + * Radix tree node definition. + */ +#define RAT_MAP_SHIFT 7 +#define RAT_MAP_SIZE (1UL << RAT_MAP_SHIFT) +#define RAT_MAP_MASK (RAT_MAP_SIZE-1) + +struct rat_node { + unsigned int count; + void *slots[RAT_MAP_SIZE]; +}; + +struct rat_path { + struct rat_node *node, **slot; +}; + +#define RAT_INDEX_BITS (8/* CHAR_BIT */ * sizeof(unsigned long)) + +/* + * Radix tree node cache. + */ +static kmem_cache_t *ratnode_cachep; + +#define ratnode_alloc(root) \ + kmem_cache_alloc(ratnode_cachep, (root)->gfp_mask) +#define ratnode_free(node) \ + kmem_cache_free(ratnode_cachep, (node)) + + +/* + * Return the maximum key, which can be store into a radix tree + * with height HEIGHT. + */ +static inline unsigned long rat_maxindex(unsigned int height) +{ + unsigned int tmp = height * RAT_MAP_SHIFT; + unsigned long index = (~0UL >> (RAT_INDEX_BITS - tmp - 1)) >> 1; + + if (tmp >= RAT_INDEX_BITS) + index = ~0UL; + return index; +} + +/** + * rat_extend - extend radix tree + * @root: radix tree root + * @index: maximum key + * + * Extend a radix tree so it can store key @index. + */ +int rat_extend(struct rat_root *root, unsigned long index) +{ + struct rat_node *node; + unsigned int height; + + /* Figure out what the height should be. */ + height = root->height + 1; + while (index > rat_maxindex(height)) + height++; + + if (root->rnode) { + do { + if (!(node = ratnode_alloc(root))) + return -ENOMEM; + + /* Increase the height. */ + node->slots[0] = root->rnode; + if (root->rnode) + node->count = 1; + root->rnode = node; + root->height++; + } while (height > root->height); + } else + root->height = height; + + return 0; +} + +EXPORT_SYMBOL(rat_extend); + + +/** + * rat_reserve - reserve space in a radix tree + * @root: radix tree root + * @index: index key + * @pslot: pointer to reserved slot + * + * Reserve a slot in a radix tree for the key @index. + */ +int rat_reserve(struct rat_root *root, unsigned long index, void ***pslot) +{ + struct rat_node *node = NULL, *tmp, **slot; + unsigned int height, shift; + int error; + + /* Make sure the tree is high enough. */ + if (index > rat_maxindex(root->height)) { + error = rat_extend(root, index); + if (error) + return error; + } + + slot = &root->rnode; + height = root->height; + shift = (height-1) * RAT_MAP_SHIFT; + + while (height > 0) { + if (*slot == NULL) { + /* Have to add a child node. */ + if (!(tmp = ratnode_alloc(root))) + return -ENOMEM; + *slot = tmp; + if (node) + node->count++; + } + + /* Go a level down. */ + node = *slot; + slot = (struct rat_node **) + (node->slots + ((index >> shift) & RAT_MAP_MASK)); + shift -= RAT_MAP_SHIFT; + height--; + } + + if (*slot != NULL) + return -EEXIST; + if (node) + node->count++; + + *pslot = (void **)slot; + **pslot = RAT_SLOT_RESERVED; + return 0; +} + +EXPORT_SYMBOL(rat_reserve); + + +/** + * rat_insert - insert into a radix tree + * @root: radix tree root + * @index: index key + * @item: item to insert + * + * Insert an item into the radix tree at position @index. + */ +int rat_insert(struct rat_root *root, unsigned long index, void *item) +{ + void **slot; + int error; + + error = rat_reserve(root, index, &slot); + if (!error) + *slot = item; + return error; +} + +EXPORT_SYMBOL(rat_insert); + + +/** + * rat_lookup - perform lookup operation on a radix tree + * @root: radix tree root + * @index: index key + * + * Lookup them item at the position @index in the radix tree @root. + */ +void *rat_lookup(struct rat_root *root, unsigned long index) +{ + unsigned int height, shift; + struct rat_node **slot; + + height = root->height; + if (index > rat_maxindex(height)) + return NULL; + + shift = (height-1) * RAT_MAP_SHIFT; + slot = &root->rnode; + + while (height > 0) { + if (*slot == NULL) + return NULL; + + slot = (struct rat_node **) + ((*slot)->slots + ((index >> shift) & RAT_MAP_MASK)); + shift -= RAT_MAP_SHIFT; + height--; + } + + return (void *) *slot; +} + +EXPORT_SYMBOL(rat_lookup); + + +/** + * rat_delete - delete an item from a radix tree + * @root: radix tree root + * @index: index key + * + * Remove the item at @index from the radix tree rooted at @root. + */ +int rat_delete(struct rat_root *root, unsigned long index) +{ + struct rat_path path[RAT_INDEX_BITS/RAT_MAP_SHIFT + 2], *pathp = path; + unsigned int height, shift; + + height = root->height; + if (index > rat_maxindex(height)) + return -ENOENT; + + shift = (height-1) * RAT_MAP_SHIFT; + pathp->node = NULL; + pathp->slot = &root->rnode; + + while (height > 0) { + if (*pathp->slot == NULL) + return -ENOENT; + + pathp[1].node = *pathp[0].slot; + pathp[1].slot = (struct rat_node **) + (pathp[1].node->slots + ((index >> shift) & RAT_MAP_MASK)); + pathp++; + shift -= RAT_MAP_SHIFT; + height--; + } + + if (*pathp[0].slot == NULL) + return -ENOENT; + + *pathp[0].slot = NULL; + while (pathp[0].node && --pathp[0].node->count == 0) { + pathp--; + *pathp[0].slot = NULL; + ratnode_free(pathp[1].node); + } + + return 0; +} + +EXPORT_SYMBOL(rat_delete); + + +static void rat_node_ctor(void *node, kmem_cache_t *cachep, unsigned long flags) +{ + memset(node, 0, sizeof(struct rat_node)); +} + +void __init ratcache_init(void) +{ + ratnode_cachep = kmem_cache_create("ratnode", sizeof(struct rat_node), + 0, SLAB_HWCACHE_ALIGN, rat_node_ctor, NULL); + if (!ratnode_cachep) + panic ("Failed to create rat_node cache\n"); +} diff -urN linux-rmap10-virgin/mm/filemap.c linux-ratmerge/mm/filemap.c --- linux-rmap10-virgin/mm/filemap.c Sun Jan 6 06:15:54 2002 +++ linux-ratmerge/mm/filemap.c Sun Jan 6 05:27:50 2002 @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -45,75 +46,26 @@ */ atomic_t page_cache_size = ATOMIC_INIT(0); -unsigned int page_hash_bits; -struct page **page_hash_table; int vm_max_readahead = 31; int vm_min_readahead = 3; EXPORT_SYMBOL(vm_max_readahead); EXPORT_SYMBOL(vm_min_readahead); - -spinlock_t pagecache_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED; /* * NOTE: to avoid deadlocking you must never acquire the pagemap_lru_lock - * with the pagecache_lock held. + * with the mapping lock held. * * Ordering: * swap_lock -> * pagemap_lru_lock -> - * pagecache_lock + * mapping lock */ spinlock_t pagemap_lru_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED; #define CLUSTER_PAGES (1 << page_cluster) #define CLUSTER_OFFSET(x) (((x) >> page_cluster) << page_cluster) -static void FASTCALL(add_page_to_hash_queue(struct page * page, struct page **p)); -static void add_page_to_hash_queue(struct page * page, struct page **p) -{ - struct page *next = *p; - - *p = page; - page->next_hash = next; - page->pprev_hash = p; - if (next) - next->pprev_hash = &page->next_hash; - if (page->buffers) - PAGE_BUG(page); - atomic_inc(&page_cache_size); -} - -static inline void add_page_to_inode_queue(struct address_space *mapping, struct page * page) -{ - struct list_head *head = &mapping->clean_pages; - - mapping->nrpages++; - list_add(&page->list, head); - page->mapping = mapping; -} - -static inline void remove_page_from_inode_queue(struct page * page) -{ - struct address_space * mapping = page->mapping; - - mapping->nrpages--; - list_del(&page->list); - page->mapping = NULL; -} - -static inline void remove_page_from_hash_queue(struct page * page) -{ - struct page *next = page->next_hash; - struct page **pprev = page->pprev_hash; - - if (next) - next->pprev_hash = pprev; - *pprev = next; - page->pprev_hash = NULL; - atomic_dec(&page_cache_size); -} - /* * Remove a page from the page cache and free it. Caller has to make * sure the page is locked and that nobody else uses it - or that usage @@ -122,18 +74,20 @@ void __remove_inode_page(struct page *page) { if (PageDirty(page)) BUG(); + rat_delete(&page->mapping->page_tree, page->index); remove_page_from_inode_queue(page); - remove_page_from_hash_queue(page); + atomic_dec(&page_cache_size); } void remove_inode_page(struct page *page) { + struct address_space *mapping = page->mapping; if (!PageLocked(page)) PAGE_BUG(page); - spin_lock(&pagecache_lock); + spin_lock(&mapping->i_shared_lock); __remove_inode_page(page); - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); } static inline int sync_page(struct page *page) @@ -154,10 +108,10 @@ struct address_space *mapping = page->mapping; if (mapping) { - spin_lock(&pagecache_lock); + spin_lock(&mapping->i_shared_lock); list_del(&page->list); list_add(&page->list, &mapping->dirty_pages); - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); if (mapping->host) mark_inode_dirty_pages(mapping->host); @@ -177,11 +131,12 @@ { struct list_head *head, *curr; struct page * page; + struct address_space *mapping = inode->i_mapping; - head = &inode->i_mapping->clean_pages; + head = &mapping->clean_pages; spin_lock(&pagemap_lru_lock); - spin_lock(&pagecache_lock); + spin_lock(&mapping->i_shared_lock); curr = head->next; while (curr != head) { @@ -212,7 +167,7 @@ continue; } - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); spin_unlock(&pagemap_lru_lock); } @@ -251,8 +206,9 @@ page_cache_release(page); } -static int FASTCALL(truncate_list_pages(struct list_head *, unsigned long, unsigned *)); -static int truncate_list_pages(struct list_head *head, unsigned long start, unsigned *partial) +static int FASTCALL(truncate_list_pages(struct address_space *, struct list_head *, unsigned long, unsigned *)); +static int truncate_list_pages(struct address_space *mapping, + struct list_head *head, unsigned long start, unsigned *partial) { struct list_head *curr; struct page * page; @@ -281,7 +237,7 @@ /* Restart on this page */ list_add(head, curr); - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); unlocked = 1; if (!failed) { @@ -302,7 +258,7 @@ schedule(); } - spin_lock(&pagecache_lock); + spin_lock(&mapping->i_shared_lock); goto restart; } curr = curr->prev; @@ -326,24 +282,25 @@ unsigned partial = lstart & (PAGE_CACHE_SIZE - 1); int unlocked; - spin_lock(&pagecache_lock); + spin_lock(&mapping->i_shared_lock); do { - unlocked = truncate_list_pages(&mapping->clean_pages, start, &partial); - unlocked |= truncate_list_pages(&mapping->dirty_pages, start, &partial); - unlocked |= truncate_list_pages(&mapping->locked_pages, start, &partial); + unlocked = truncate_list_pages(mapping, &mapping->clean_pages, start, &partial); + unlocked |= truncate_list_pages(mapping, &mapping->dirty_pages, start, &partial); + unlocked |= truncate_list_pages(mapping, &mapping->locked_pages, start, &partial); } while (unlocked); /* Traversed all three lists without dropping the lock */ - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); } -static inline int invalidate_this_page2(struct page * page, +static inline int invalidate_this_page2(struct address_space *mapping, + struct page * page, struct list_head * curr, struct list_head * head) { int unlocked = 1; /* - * The page is locked and we hold the pagecache_lock as well + * The page is locked and we hold the mapping lock as well * so both page_count(page) and page->buffers stays constant here. */ if (page_count(page) == 1 + !!page->buffers) { @@ -352,7 +309,7 @@ list_add_tail(head, curr); page_cache_get(page); - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); truncate_complete_page(page); } else { if (page->buffers) { @@ -361,7 +318,7 @@ list_add_tail(head, curr); page_cache_get(page); - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); block_invalidate_page(page); } else unlocked = 0; @@ -373,8 +330,8 @@ return unlocked; } -static int FASTCALL(invalidate_list_pages2(struct list_head *)); -static int invalidate_list_pages2(struct list_head *head) +static int FASTCALL(invalidate_list_pages2(struct address_space *mapping, struct list_head *)); +static int invalidate_list_pages2(struct address_space *mapping, struct list_head *head) { struct list_head *curr; struct page * page; @@ -388,7 +345,7 @@ if (!TryLockPage(page)) { int __unlocked; - __unlocked = invalidate_this_page2(page, curr, head); + __unlocked = invalidate_this_page2(mapping, page, curr, head); UnlockPage(page); unlocked |= __unlocked; if (!__unlocked) { @@ -401,7 +358,7 @@ list_add(head, curr); page_cache_get(page); - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); unlocked = 1; wait_on_page(page); } @@ -412,7 +369,7 @@ schedule(); } - spin_lock(&pagecache_lock); + spin_lock(&mapping->i_shared_lock); goto restart; } return unlocked; @@ -427,37 +384,13 @@ { int unlocked; - spin_lock(&pagecache_lock); + spin_lock(&mapping->i_shared_lock); do { - unlocked = invalidate_list_pages2(&mapping->clean_pages); - unlocked |= invalidate_list_pages2(&mapping->dirty_pages); - unlocked |= invalidate_list_pages2(&mapping->locked_pages); + unlocked = invalidate_list_pages2(mapping, &mapping->clean_pages); + unlocked |= invalidate_list_pages2(mapping, &mapping->dirty_pages); + unlocked |= invalidate_list_pages2(mapping, &mapping->locked_pages); } while (unlocked); - spin_unlock(&pagecache_lock); -} - -static inline struct page * __find_page_nolock(struct address_space *mapping, unsigned long offset, struct page *page) -{ - goto inside; - - for (;;) { - page = page->next_hash; -inside: - if (!page) - goto not_found; - if (page->mapping != mapping) - continue; - if (page->index == offset) - break; - } - -not_found: - return page; -} - -static struct page * __find_page(struct address_space * mapping, unsigned long index) -{ - return __find_page_nolock(mapping, index, *page_hash(mapping,index)); + spin_unlock(&mapping->i_shared_lock); } /* @@ -495,13 +428,14 @@ return error; } -static int do_buffer_fdatasync(struct list_head *head, unsigned long start, unsigned long end, int (*fn)(struct page *)) +static int do_buffer_fdatasync(struct address_space *mapping, + struct list_head *head, unsigned long start, unsigned long end, int (*fn)(struct page *)) { struct list_head *curr; struct page *page; int retval = 0; - spin_lock(&pagecache_lock); + spin_lock(&mapping->i_shared_lock); curr = head->next; while (curr != head) { page = list_entry(curr, struct page, list); @@ -514,7 +448,7 @@ continue; page_cache_get(page); - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); lock_page(page); /* The buffers could have been free'd while we waited for the page lock */ @@ -522,11 +456,11 @@ retval |= fn(page); UnlockPage(page); - spin_lock(&pagecache_lock); + spin_lock(&mapping->i_shared_lock); curr = page->list.next; page_cache_release(page); } - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); return retval; } @@ -537,17 +471,18 @@ */ int generic_buffer_fdatasync(struct inode *inode, unsigned long start_idx, unsigned long end_idx) { + struct address_space *mapping = inode->i_mapping; int retval; /* writeout dirty buffers on pages from both clean and dirty lists */ - retval = do_buffer_fdatasync(&inode->i_mapping->dirty_pages, start_idx, end_idx, writeout_one_page); - retval |= do_buffer_fdatasync(&inode->i_mapping->clean_pages, start_idx, end_idx, writeout_one_page); - retval |= do_buffer_fdatasync(&inode->i_mapping->locked_pages, start_idx, end_idx, writeout_one_page); + retval = do_buffer_fdatasync(mapping, &mapping->dirty_pages, start_idx, end_idx, writeout_one_page); + retval |= do_buffer_fdatasync(mapping, &mapping->clean_pages, start_idx, end_idx, writeout_one_page); + retval |= do_buffer_fdatasync(mapping, &mapping->locked_pages, start_idx, end_idx, writeout_one_page); /* now wait for locked buffers on pages from both clean and dirty lists */ - retval |= do_buffer_fdatasync(&inode->i_mapping->dirty_pages, start_idx, end_idx, waitfor_one_page); - retval |= do_buffer_fdatasync(&inode->i_mapping->clean_pages, start_idx, end_idx, waitfor_one_page); - retval |= do_buffer_fdatasync(&inode->i_mapping->locked_pages, start_idx, end_idx, waitfor_one_page); + retval |= do_buffer_fdatasync(mapping, &mapping->dirty_pages, start_idx, end_idx, waitfor_one_page); + retval |= do_buffer_fdatasync(mapping, &mapping->clean_pages, start_idx, end_idx, waitfor_one_page); + retval |= do_buffer_fdatasync(mapping, &mapping->locked_pages, start_idx, end_idx, waitfor_one_page); return retval; } @@ -592,7 +527,7 @@ { int (*writepage)(struct page *) = mapping->a_ops->writepage; - spin_lock(&pagecache_lock); + spin_lock(&mapping->i_shared_lock); while (!list_empty(&mapping->dirty_pages)) { struct page *page = list_entry(mapping->dirty_pages.next, struct page, list); @@ -604,7 +539,7 @@ continue; page_cache_get(page); - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); lock_page(page); @@ -615,9 +550,9 @@ UnlockPage(page); page_cache_release(page); - spin_lock(&pagecache_lock); + spin_lock(&mapping->i_shared_lock); } - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); } /** @@ -629,7 +564,7 @@ */ void filemap_fdatawait(struct address_space * mapping) { - spin_lock(&pagecache_lock); + spin_lock(&mapping->i_shared_lock); while (!list_empty(&mapping->locked_pages)) { struct page *page = list_entry(mapping->locked_pages.next, struct page, list); @@ -641,14 +576,14 @@ continue; page_cache_get(page); - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); ___wait_on_page(page); page_cache_release(page); - spin_lock(&pagecache_lock); + spin_lock(&mapping->i_shared_lock); } - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); } /* @@ -657,64 +592,81 @@ * The caller must have locked the page and * set all the page flags correctly.. */ -void add_to_page_cache_locked(struct page * page, struct address_space *mapping, unsigned long index) +int add_to_page_cache_locked(struct page * page, struct address_space *mapping, unsigned long index) { if (!PageLocked(page)) BUG(); - page->index = index; page_cache_get(page); - spin_lock(&pagecache_lock); + spin_lock (&mapping->i_shared_lock); + if (rat_insert(&mapping->page_tree, index, page) < 0) + goto nomem; + page->index = index; add_page_to_inode_queue(mapping, page); - add_page_to_hash_queue(page, page_hash(mapping, index)); - spin_unlock(&pagecache_lock); + atomic_inc(&page_cache_size); + spin_unlock(&mapping->i_shared_lock); lru_cache_add(page); + return 0; + nomem: + spin_unlock (&mapping->i_shared_lock); + page_cache_release (page); + return -ENOMEM; } /* * This adds a page to the page cache, starting out as locked, * owned by us, but unreferenced, not uptodate and with no errors. */ -static inline void __add_to_page_cache(struct page * page, - struct address_space *mapping, unsigned long offset, - struct page **hash) +static inline int __add_to_page_cache(struct page * page, struct address_space *mapping, + unsigned long offset) { unsigned long flags; - flags = page->flags & ~(1 << PG_uptodate | 1 << PG_error | 1 << PG_dirty | 1 << PG_referenced | 1 << PG_arch_1 | 1 << PG_checked); - page->flags = flags | (1 << PG_locked); page_cache_get(page); + if (rat_insert(&mapping->page_tree, offset, page) < 0) + goto nomem; + flags = page->flags & ~(1 << PG_uptodate | 1 << PG_error | + 1 << PG_dirty | 1 << PG_referenced | + 1 << PG_arch_1 | 1 << PG_checked); + page->flags = flags | (1 << PG_locked); page->index = offset; add_page_to_inode_queue(mapping, page); - add_page_to_hash_queue(page, hash); + + atomic_inc(&page_cache_size); + return 0; + nomem: + page_cache_release (page); + return -ENOMEM; } -void add_to_page_cache(struct page * page, struct address_space * mapping, unsigned long offset) +int add_to_page_cache(struct page * page, struct address_space * mapping, unsigned long offset) { - spin_lock(&pagecache_lock); - __add_to_page_cache(page, mapping, offset, page_hash(mapping, offset)); - spin_unlock(&pagecache_lock); + spin_lock(&mapping->i_shared_lock); + if (__add_to_page_cache(page, mapping, offset) < 0) + goto nomem; + spin_unlock(&mapping->i_shared_lock); lru_cache_add(page); + return 0; + nomem: + spin_unlock (&mapping->i_shared_lock); + return -ENOMEM; } -int add_to_page_cache_unique(struct page * page, - struct address_space *mapping, unsigned long offset, - struct page **hash) +int add_to_page_cache_unique(struct page * page, struct address_space *mapping, + unsigned long offset) { int err; struct page *alias; - spin_lock(&pagecache_lock); - alias = __find_page_nolock(mapping, offset, *hash); + spin_lock(&mapping->i_shared_lock); + alias = rat_lookup(&mapping->page_tree, offset); - err = 1; - if (!alias) { - __add_to_page_cache(page,mapping,offset,hash); - err = 0; - } + err = -EEXIST; + if (!alias) + err = __add_to_page_cache(page,mapping,offset); - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); if (!err) lru_cache_add(page); return err; @@ -728,12 +680,12 @@ static int page_cache_read(struct file * file, unsigned long offset) { struct address_space *mapping = file->f_dentry->d_inode->i_mapping; - struct page **hash = page_hash(mapping, offset); struct page *page; + int error; - spin_lock(&pagecache_lock); - page = __find_page_nolock(mapping, offset, *hash); - spin_unlock(&pagecache_lock); + spin_lock(&mapping->i_shared_lock); + page = rat_lookup(&mapping->page_tree, offset); + spin_unlock(&mapping->i_shared_lock); if (page) return 0; @@ -741,17 +693,27 @@ if (!page) return -ENOMEM; - if (!add_to_page_cache_unique(page, mapping, offset, hash)) { - int error = mapping->a_ops->readpage(file, page); + error = add_to_page_cache_unique (page, mapping, offset); + while (error == -ENOMEM) { + /* Yield for kswapd, and try again */ + current->policy |= SCHED_YIELD; + __set_current_state(TASK_RUNNING); + schedule(); + error = add_to_page_cache_unique (page, mapping, offset); + } + + if (!error) { + error = mapping->a_ops->readpage(file, page); page_cache_release(page); return error; } /* * We arrive here in the unlikely event that someone - * raced with us and added our page to the cache first. + * raced with us and added our page to the cache first + * or we are out of memory. */ page_cache_release(page); - return 0; + return error == -ENOMEM ? -ENOMEM : 0; } /* @@ -849,8 +811,7 @@ * a rather lightweight function, finding and getting a reference to a * hashed page atomically. */ -struct page * __find_get_page(struct address_space *mapping, - unsigned long offset, struct page **hash) +struct page * find_get_page(struct address_space *mapping, unsigned long offset) { struct page *page; @@ -858,11 +819,11 @@ * We scan the hash list read-only. Addition to and removal from * the hash-list needs a held write-lock. */ - spin_lock(&pagecache_lock); - page = __find_page_nolock(mapping, offset, *hash); + spin_lock(&mapping->i_shared_lock); + page = rat_lookup(&mapping->page_tree, offset); if (page) page_cache_get(page); - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); return page; } @@ -872,15 +833,14 @@ struct page *find_trylock_page(struct address_space *mapping, unsigned long offset) { struct page *page; - struct page **hash = page_hash(mapping, offset); - spin_lock(&pagecache_lock); - page = __find_page_nolock(mapping, offset, *hash); + spin_lock(&mapping->i_shared_lock); + page = rat_lookup(&mapping->page_tree, offset); if (page) { if (TryLockPage(page)) page = NULL; } - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); return page; } @@ -889,9 +849,9 @@ * will return with it held (but it may be dropped * during blocking operations.. */ -static struct page * FASTCALL(__find_lock_page_helper(struct address_space *, unsigned long, struct page *)); -static struct page * __find_lock_page_helper(struct address_space *mapping, - unsigned long offset, struct page *hash) +static struct page * FASTCALL(find_lock_page_helper(struct address_space *, unsigned long)); +static struct page * find_lock_page_helper(struct address_space *mapping, + unsigned long offset) { struct page *page; @@ -900,13 +860,13 @@ * the hash-list needs a held write-lock. */ repeat: - page = __find_page_nolock(mapping, offset, hash); + page = rat_lookup(&mapping->page_tree, offset); if (page) { page_cache_get(page); if (TryLockPage(page)) { - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); lock_page(page); - spin_lock(&pagecache_lock); + spin_lock(&mapping->i_shared_lock); /* Has the page been re-allocated while we slept? */ if (page->mapping != mapping || page->index != offset) { @@ -923,14 +883,14 @@ * Same as the above, but lock the page too, verifying that * it's still valid once we own it. */ -struct page * __find_lock_page (struct address_space *mapping, - unsigned long offset, struct page **hash) +struct page * find_lock_page(struct address_space *mapping, unsigned long offset) { struct page *page; - spin_lock(&pagecache_lock); - page = __find_lock_page_helper(mapping, offset, *hash); - spin_unlock(&pagecache_lock); + spin_lock(&mapping->i_shared_lock); + page = find_lock_page_helper(mapping, offset); + spin_unlock(&mapping->i_shared_lock); + return page; } @@ -940,23 +900,22 @@ struct page * find_or_create_page(struct address_space *mapping, unsigned long index, unsigned int gfp_mask) { struct page *page; - struct page **hash = page_hash(mapping, index); - spin_lock(&pagecache_lock); - page = __find_lock_page_helper(mapping, index, *hash); - spin_unlock(&pagecache_lock); + spin_lock(&mapping->i_shared_lock); + page = find_lock_page_helper(mapping, index); + spin_unlock(&mapping->i_shared_lock); if (!page) { struct page *newpage = alloc_page(gfp_mask); - page = NULL; if (newpage) { - spin_lock(&pagecache_lock); - page = __find_lock_page_helper(mapping, index, *hash); + spin_lock(&mapping->i_shared_lock); + page = find_lock_page_helper(mapping, index); if (likely(!page)) { - page = newpage; - __add_to_page_cache(page, mapping, index, hash); - newpage = NULL; + if (__add_to_page_cache (newpage, mapping, index) == 0) { + page = newpage; + newpage = NULL; + } } - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); if (newpage == NULL) lru_cache_add(page); else @@ -1011,14 +970,14 @@ * stop when the page isn't there. */ spin_lock(&pagemap_lru_lock); - spin_lock(&pagecache_lock); + spin_lock(&mapping->i_shared_lock); while (--index >= start) { - page = __find_page(mapping, index); + page = rat_lookup(&mapping->page_tree, index); if (!page || !PageActive(page)) break; deactivate_page_nolock(page); } - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); spin_unlock(&pagemap_lru_lock); } @@ -1029,10 +988,9 @@ */ struct page *grab_cache_page_nowait(struct address_space *mapping, unsigned long index) { - struct page *page, **hash; + struct page *page; - hash = page_hash(mapping, index); - page = __find_get_page(mapping, index, hash); + page = find_get_page(mapping, index); if ( page ) { if ( !TryLockPage(page) ) { @@ -1057,7 +1015,7 @@ if ( unlikely(!page) ) return NULL; /* Failed to allocate a page */ - if ( unlikely(add_to_page_cache_unique(page, mapping, index, hash)) ) { + if ( unlikely(add_to_page_cache_unique(page, mapping, index)) ) { /* Someone else grabbed the page already. */ page_cache_release(page); return NULL; @@ -1369,7 +1327,7 @@ } for (;;) { - struct page *page, **hash; + struct page *page; unsigned long end_index, nr, ret; end_index = inode->i_size >> PAGE_CACHE_SHIFT; @@ -1388,15 +1346,14 @@ /* * Try to find the data in the page cache.. */ - hash = page_hash(mapping, index); - spin_lock(&pagecache_lock); - page = __find_page_nolock(mapping, index, *hash); + spin_lock(&mapping->i_shared_lock); + page = rat_lookup(&mapping->page_tree, index); if (!page) goto no_cached_page; found_page: page_cache_get(page); - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); if (!Page_Uptodate(page)) goto page_not_up_to_date; @@ -1490,7 +1447,7 @@ * We get here with the page cache lock held. */ if (!cached_page) { - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); cached_page = page_cache_alloc(mapping); if (!cached_page) { desc->error = -ENOMEM; @@ -1501,8 +1458,8 @@ * Somebody may have added the page while we * dropped the page cache lock. Check for that. */ - spin_lock(&pagecache_lock); - page = __find_page_nolock(mapping, index, *hash); + spin_lock(&mapping->i_shared_lock); + page = rat_lookup(&mapping->page_tree, index); if (page) goto found_page; } @@ -1510,9 +1467,13 @@ /* * Ok, add the new page to the hash-queues... */ + if (__add_to_page_cache (cached_page, mapping, index) < 0) { + spin_unlock (&mapping->i_shared_lock); + desc->error = -ENOMEM; + break; + } page = cached_page; - __add_to_page_cache(page, mapping, index, hash); - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); lru_cache_add(page); cached_page = NULL; @@ -1912,7 +1873,7 @@ struct file *file = area->vm_file; struct address_space *mapping = file->f_dentry->d_inode->i_mapping; struct inode *inode = mapping->host; - struct page *page, **hash; + struct page *page; unsigned long size, pgoff, endoff; pgoff = ((address - area->vm_start) >> PAGE_CACHE_SHIFT) + area->vm_pgoff; @@ -1934,9 +1895,8 @@ /* * Do we have something in the page cache already? */ - hash = page_hash(mapping, pgoff); retry_find: - page = __find_get_page(mapping, pgoff, hash); + page = find_get_page(mapping, pgoff); if (!page) goto no_cached_page; @@ -2621,13 +2581,13 @@ { unsigned char present = 0; struct address_space * as = vma->vm_file->f_dentry->d_inode->i_mapping; - struct page * page, ** hash = page_hash(as, pgoff); + struct page * page; - spin_lock(&pagecache_lock); - page = __find_page_nolock(as, pgoff, *hash); + spin_lock(&as->i_shared_lock); + page = rat_lookup(&as->page_tree, pgoff); if ((page) && (Page_Uptodate(page))) present = 1; - spin_unlock(&pagecache_lock); + spin_unlock(&as->i_shared_lock); return present; } @@ -2770,20 +2730,24 @@ int (*filler)(void *,struct page*), void *data) { - struct page **hash = page_hash(mapping, index); struct page *page, *cached_page = NULL; int err; repeat: - page = __find_get_page(mapping, index, hash); + page = find_get_page(mapping, index); if (!page) { if (!cached_page) { cached_page = page_cache_alloc(mapping); if (!cached_page) return ERR_PTR(-ENOMEM); } - page = cached_page; - if (add_to_page_cache_unique(page, mapping, index, hash)) + err = add_to_page_cache_unique (cached_page, mapping, index); + if (err == -EEXIST) goto repeat; + if (err < 0) { + page_cache_release(cached_page); + return ERR_PTR(err); + } + page = cached_page; cached_page = NULL; err = filler(data, page); if (err < 0) { @@ -2838,19 +2802,23 @@ static inline struct page * __grab_cache_page(struct address_space *mapping, unsigned long index, struct page **cached_page) { - struct page *page, **hash = page_hash(mapping, index); + int err; + struct page *page; repeat: - page = __find_lock_page(mapping, index, hash); + page = find_lock_page(mapping, index); if (!page) { if (!*cached_page) { *cached_page = page_cache_alloc(mapping); if (!*cached_page) return NULL; } - page = *cached_page; - if (add_to_page_cache_unique(page, mapping, index, hash)) + err = add_to_page_cache_unique (*cached_page, mapping, index); + if (err == -EEXIST) goto repeat; - *cached_page = NULL; + if (err == 0) { + page = *cached_page; + *cached_page = NULL; + } } return page; } @@ -3115,31 +3083,4 @@ if (written >= 0 && file->f_flags & O_SYNC) status = generic_osync_inode(inode, OSYNC_METADATA); goto out_status; -} - -void __init page_cache_init(unsigned long mempages) -{ - unsigned long htable_size, order; - - htable_size = mempages; - htable_size *= sizeof(struct page *); - for(order = 0; (PAGE_SIZE << order) < htable_size; order++) - ; - - do { - unsigned long tmp = (PAGE_SIZE << order) / sizeof(struct page *); - - page_hash_bits = 0; - while((tmp >>= 1UL) != 0UL) - page_hash_bits++; - - page_hash_table = (struct page **) - __get_free_pages(GFP_ATOMIC, order); - } while(page_hash_table == NULL && --order > 0); - - printk("Page-cache hash table entries: %d (order: %ld, %ld bytes)\n", - (1 << page_hash_bits), order, (PAGE_SIZE << order)); - if (!page_hash_table) - panic("Failed to allocate page hash table\n"); - memset((void *)page_hash_table, 0, PAGE_HASH_SIZE * sizeof(struct page *)); } diff -urN linux-rmap10-virgin/mm/shmem.c linux-ratmerge/mm/shmem.c --- linux-rmap10-virgin/mm/shmem.c Fri Dec 21 09:42:05 2001 +++ linux-ratmerge/mm/shmem.c Sat Jan 5 14:07:02 2002 @@ -27,6 +27,7 @@ #include #include #include +#include #include @@ -365,7 +366,7 @@ swp_entry_t *ptr; unsigned long idx; int offset; - + idx = 0; spin_lock (&info->lock); offset = shmem_clear_swp (entry, info->i_direct, SHMEM_NR_DIRECT); @@ -384,11 +385,8 @@ spin_unlock (&info->lock); return 0; found: - delete_from_swap_cache(page); - add_to_page_cache(page, info->inode->i_mapping, offset + idx); - SetPageDirty(page); - SetPageUptodate(page); - info->swapped--; + if (!move_from_swap_cache (page, offset + idx, info->inode->i_mapping)) + info->swapped--; spin_unlock(&info->lock); return 1; } @@ -420,6 +418,7 @@ */ static int shmem_writepage(struct page * page) { + int err; struct shmem_inode_info *info; swp_entry_t *entry, swap; struct address_space *mapping; @@ -437,7 +436,6 @@ info = SHMEM_I(inode); if (info->locked) return fail_writepage(page); -getswap: swap = get_swap_page(); if (!swap.val) return fail_writepage(page); @@ -450,29 +448,20 @@ if (entry->val) BUG(); - /* Remove it from the page cache */ - remove_inode_page(page); - page_cache_release(page); - - /* Add it to the swap cache */ - if (add_to_swap_cache(page, swap) != 0) { - /* - * Raced with "speculative" read_swap_cache_async. - * Add page back to page cache, unref swap, try again. - */ - add_to_page_cache_locked(page, mapping, index); + err = move_to_swap_cache(page, swap); + if (!err) { + *entry = swap; + info->swapped++; spin_unlock(&info->lock); - swap_free(swap); - goto getswap; + SetPageUptodate(page); + set_page_dirty(page); + UnlockPage(page); + return 0; } - *entry = swap; - info->swapped++; spin_unlock(&info->lock); - SetPageUptodate(page); - set_page_dirty(page); - UnlockPage(page); - return 0; + swap_free(swap); + return fail_writepage(page); } /* @@ -519,8 +508,6 @@ shmem_recalc_inode(inode); if (entry->val) { - unsigned long flags; - /* Look it up and read it in.. */ page = find_get_page(&swapper_space, entry->val); if (!page) { @@ -545,16 +532,15 @@ goto repeat; } - /* We have to this with page locked to prevent races */ + /* We have to do this with page locked to prevent races */ if (TryLockPage(page)) goto wait_retry; + if (move_from_swap_cache (page, idx, mapping)) + goto nomem_retry; + swap_free(*entry); *entry = (swp_entry_t) {0}; - delete_from_swap_cache(page); - flags = page->flags & ~((1 << PG_uptodate) | (1 << PG_error) | (1 << PG_referenced) | (1 << PG_arch_1)); - page->flags = flags | (1 << PG_dirty); - add_to_page_cache_locked(page, mapping, idx); info->swapped--; spin_unlock (&info->lock); } else { @@ -576,9 +562,14 @@ page = page_cache_alloc(mapping); if (!page) return ERR_PTR(-ENOMEM); + while (add_to_page_cache (page, mapping, idx) < 0) { + /* Yield for kswapd, and try again */ + current->policy |= SCHED_YIELD; + __set_current_state(TASK_RUNNING); + schedule(); + } clear_highpage(page); inode->i_blocks += BLOCKS_PER_PAGE; - add_to_page_cache (page, mapping, idx); } /* We have the page */ @@ -592,6 +583,17 @@ spin_unlock (&info->lock); wait_on_page(page); page_cache_release(page); + goto repeat; + +nomem_retry: + spin_unlock (&info->lock); + UnlockPage (page); + page_cache_release (page); + + /* Yield for kswapd, and try again */ + current->policy |= SCHED_YIELD; + __set_current_state(TASK_RUNNING); + schedule(); goto repeat; } diff -urN linux-rmap10-virgin/mm/swap_state.c linux-ratmerge/mm/swap_state.c --- linux-rmap10-virgin/mm/swap_state.c Sun Jan 6 06:15:55 2002 +++ linux-ratmerge/mm/swap_state.c Sat Jan 5 14:21:19 2002 @@ -14,6 +14,7 @@ #include #include #include +#include #include @@ -37,11 +38,12 @@ }; struct address_space swapper_space = { - LIST_HEAD_INIT(swapper_space.clean_pages), - LIST_HEAD_INIT(swapper_space.dirty_pages), - LIST_HEAD_INIT(swapper_space.locked_pages), - 0, /* nrpages */ - &swap_aops, + page_tree: {0, GFP_ATOMIC, NULL}, + clean_pages: LIST_HEAD_INIT(swapper_space.clean_pages), + dirty_pages: LIST_HEAD_INIT(swapper_space.dirty_pages), + locked_pages: LIST_HEAD_INIT(swapper_space.locked_pages), + a_ops: &swap_aops, + i_shared_lock: SPIN_LOCK_UNLOCKED, }; #ifdef SWAP_CACHE_INFO @@ -69,17 +71,20 @@ int add_to_swap_cache(struct page *page, swp_entry_t entry) { + int error; + if (page->mapping) BUG(); if (!swap_duplicate(entry)) { INC_CACHE_INFO(noent_race); return -ENOENT; } - if (add_to_page_cache_unique(page, &swapper_space, entry.val, - page_hash(&swapper_space, entry.val)) != 0) { + + error = add_to_page_cache_unique(page, &swapper_space, entry.val); + if (error != 0) { swap_free(entry); INC_CACHE_INFO(exist_race); - return -EEXIST; + return error; } if (!PageLocked(page)) BUG(); @@ -112,11 +117,17 @@ * (adding to the page cache will clear the dirty * and uptodate bits, so we need to do it again) */ - if (add_to_swap_cache(page, entry) == 0) { - SetPageUptodate(page); - set_page_dirty(page); - swap_free(entry); - return 1; + switch (add_to_swap_cache(page, entry)) { + case 0: + SetPageUptodate(page); + set_page_dirty(page); + swap_free(entry); + return 1; + + case -ENOMEM: + swap_free (entry); + default: + break; } /* Raced with "speculative" read_swap_cache_async */ swap_free(entry); @@ -155,12 +166,98 @@ entry.val = page->index; - spin_lock(&pagecache_lock); + spin_lock(&swapper_space.i_shared_lock); __delete_from_swap_cache(page); - spin_unlock(&pagecache_lock); + spin_unlock(&swapper_space.i_shared_lock); swap_free(entry); page_cache_release(page); +} + +int move_to_swap_cache(struct page *page, swp_entry_t entry) +{ + struct address_space *mapping = page->mapping; + void **pslot; + int err; + + if (!mapping) + BUG(); + + if (!swap_duplicate(entry)) { + INC_CACHE_INFO(noent_race); + return -ENOENT; + } + + spin_lock(&swapper_space.i_shared_lock); + spin_lock(&mapping->i_shared_lock); + + err = rat_reserve(&swapper_space.page_tree, entry.val, &pslot); + if (!err) { + /* Remove it from the page cache */ + __remove_inode_page (page); + + /* Add it to the swap cache */ + *pslot = page; + page->flags = ((page->flags & ~(1 << PG_uptodate | 1 << PG_error + | 1 << PG_dirty | 1 << PG_referenced + | 1 << PG_arch_1 | 1 << PG_checked)) + | (1 << PG_locked)); + page->index = entry.val; + add_page_to_inode_queue(&swapper_space, page); + atomic_inc(&page_cache_size); + } + + spin_unlock(&mapping->i_shared_lock); + spin_unlock(&swapper_space.i_shared_lock); + + if (!err) { + INC_CACHE_INFO(add_total); + return 0; + } + + swap_free(entry); + + if (err == -EEXIST) + INC_CACHE_INFO(exist_race); + + return err; +} + +int move_from_swap_cache(struct page *page, unsigned long index, + struct address_space *mapping) +{ + void **pslot; + int err; + + if (!PageLocked(page)) + BUG(); + + spin_lock(&swapper_space.i_shared_lock); + spin_lock(&mapping->i_shared_lock); + + err = rat_reserve(&mapping->page_tree, index, &pslot); + if (!err) { + swp_entry_t entry; + + block_flushpage(page, 0); + entry.val = page->index; + __delete_from_swap_cache(page); + swap_free(entry); + + *pslot = page; + page->flags = ((page->flags & ~(1 << PG_uptodate | 1 << PG_error + | 1 << PG_referenced | 1 << PG_arch_1 + | 1 << PG_checked)) + | (1 << PG_dirty)); + page->index = index; + add_page_to_inode_queue (mapping, page); + atomic_inc(&page_cache_size); + } + + spin_lock(&mapping->i_shared_lock); + spin_lock(&swapper_space.i_shared_lock); + + return err; } /* diff -urN linux-rmap10-virgin/mm/swapfile.c linux-ratmerge/mm/swapfile.c --- linux-rmap10-virgin/mm/swapfile.c Sun Jan 6 06:15:55 2002 +++ linux-ratmerge/mm/swapfile.c Sat Jan 5 14:07:02 2002 @@ -239,10 +239,10 @@ /* Is the only swap cache user the cache itself? */ if (p->swap_map[SWP_OFFSET(entry)] == 1) { /* Recheck the page count with the pagecache lock held.. */ - spin_lock(&pagecache_lock); + spin_lock(&swapper_space.i_shared_lock); if (page_count(page) - !!page->buffers == 2) retval = 1; - spin_unlock(&pagecache_lock); + spin_unlock(&swapper_space.i_shared_lock); } swap_info_put(p); } @@ -307,13 +307,13 @@ retval = 0; if (p->swap_map[SWP_OFFSET(entry)] == 1) { /* Recheck the page count with the pagecache lock held.. */ - spin_lock(&pagecache_lock); + spin_lock(&swapper_space.i_shared_lock); if (page_count(page) - !!page->buffers == 2) { __delete_from_swap_cache(page); SetPageDirty(page); retval = 1; } - spin_unlock(&pagecache_lock); + spin_unlock(&swapper_space.i_shared_lock); } swap_info_put(p); diff -urN linux-rmap10-virgin/mm/vmscan.c linux-ratmerge/mm/vmscan.c --- linux-rmap10-virgin/mm/vmscan.c Sun Jan 6 06:15:55 2002 +++ linux-ratmerge/mm/vmscan.c Sun Jan 6 05:28:53 2002 @@ -84,17 +84,24 @@ * reclaim_page() cannot race with find_get_page() and friends. */ spin_lock(&pagemap_lru_lock); - spin_lock(&pagecache_lock); maxscan = zone->inactive_clean_pages; while (maxscan-- && !list_empty(&zone->inactive_clean_list)) { + struct address_space *mapping; + page_lru = zone->inactive_clean_list.prev; - page = list_entry(page_lru, struct page, lru); + page = list_entry(page_lru, struct page, lru); + mapping = page->mapping; + + if(mapping) + spin_lock(&mapping->i_shared_lock); /* Wrong page on list?! (list corruption, should not happen) */ if (unlikely(!PageInactiveClean(page))) { printk("VM: reclaim_page, wrong page on list.\n"); list_del(page_lru); page_zone(page)->inactive_clean_pages--; + if(mapping) + spin_unlock(&mapping->i_shared_lock); continue; } @@ -102,6 +109,8 @@ if (unlikely(page_count(page)) == 0) { list_del(page_lru); list_add(page_lru, &zone->inactive_clean_list); + if(mapping) + spin_unlock(&mapping->i_shared_lock); continue; } @@ -111,6 +120,8 @@ page_count(page) > 1 || TryLockPage(page))) { del_page_from_inactive_clean_list(page); add_page_to_inactive_dirty_list(page); + if(mapping) + spin_unlock(&mapping->i_shared_lock); continue; } @@ -131,14 +142,15 @@ list_del(page_lru); zone->inactive_clean_pages--; UnlockPage(page); + if(mapping) + spin_unlock(&mapping->i_shared_lock); } - spin_unlock(&pagecache_lock); spin_unlock(&pagemap_lru_lock); return NULL; found_page: del_page_from_inactive_clean_list(page); - spin_unlock(&pagecache_lock); + spin_unlock(&mapping->i_shared_lock); spin_unlock(&pagemap_lru_lock); if (entry.val) swap_free(entry);