lkml.org 
[lkml]   [1997]   [Jun]   [30]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
Date
From
SubjectRe: Page cache patch - 2'nd version
> Now that things have settled down (actually I've been busy with the
> Ultra port these past few weeks, but I've been watching this thread
> carefully, don't worry)...

Aha ...

>
> Simply appling your patch should work. And IMHO you've found a
> solution. I've applied you patch over my patched kernel version
> ... after compiling and testing the system show's a real
> performance win on high _and_ low system load. You maybe found
> it's a (the?) real solution: Why shrink_mmap() should reference a
> page which is not used by shrink_mmap() it's self?
>
> Very nice, now time for me to scrutinize the total resulting changes.
> Can someone (Werner?) put together a complete patch for the buffer
> cache etc. performance fixes, cleanly against pre-2.0.31-2, so that I
> can have them for breakfast tomorrow? 8-)

Hmmm ... I've simply applied Krzysztof Strasburger's patch and run diff.
You will find the patch against pre-2.0.31-2 at the end of this mail.
And you will find both patches by viewing

http://www.linuxhq.com/patch/20-p0605.html

and

http://www.linuxhq.com/patch/20-p0595.html

... in the last URL I've given a full description of my changes I've
applied or done.



Werner

... Hoping the following works for everybody ...
------------------------------------------------------------------------
diff -urN -x *~1 linux-2.0.31-clean/fs/buffer.c linux/fs/buffer.c
--- linux-2.0.31-clean/fs/buffer.c Tue Jun 10 12:58:46 1997
+++ linux/fs/buffer.c Mon Jun 23 20:51:32 1997
@@ -10,11 +10,13 @@
* data, of course), but instead letting the caller do it.
*/

+/*
+ * NOTE! There is one discordant note here: checking floppies for
+ * disk change. This is where it fits best, I think, as it should
+ * invalidate changed floppy-disk-caches.
+ */
+
/* Some bdflush() changes for the dynamic ramdisk - Paul Gortmaker, 12/94 */
-/* Start bdflush() with kernel_thread not syscall - Paul Gortmaker, 12/95 */
-
-/* Removed a lot of unnecessary code and simplified things now that
- the buffer cache isn't our primary cache - Andrew Tridgell 12/96 */

#include <linux/sched.h>
#include <linux/kernel.h>
@@ -32,39 +34,43 @@
#include <asm/system.h>
#include <asm/segment.h>
#include <asm/io.h>
-#include <asm/bitops.h>

#define NR_SIZES 5
static char buffersize_index[17] =
{-1, 0, 1, -1, 2, -1, -1, -1, 3, -1, -1, -1, -1, -1, -1, -1, 4};
+static short int bufferindex_size[NR_SIZES] = {512, 1024, 2048, 4096, 8192};

#define BUFSIZE_INDEX(X) ((int) buffersize_index[(X)>>9])
#define MAX_BUF_PER_PAGE (PAGE_SIZE / 512)
-#define MAX_UNUSED_BUFFERS 30 /* don't ever have more than this number of
- unused buffer heads */
-#define HASH_PAGES 4 /* number of pages to use for the hash table */
-#define NR_HASH (HASH_PAGES*PAGE_SIZE/sizeof(struct buffer_head *))
-#define HASH_MASK (NR_HASH-1)

static int grow_buffers(int pri, int size);
+static int shrink_specific_buffers(unsigned int priority, int size);
+static int maybe_shrink_lav_buffers(int);

+static int nr_hash = 0; /* Size of hash table */
static struct buffer_head ** hash_table;
static struct buffer_head * lru_list[NR_LIST] = {NULL, };
+/* next_to_age is an array of pointers into the lru lists, used to
+ cycle through the buffers aging their contents when deciding which
+ buffers to discard when more memory is needed */
+static struct buffer_head * next_to_age[NR_LIST] = {NULL, };
static struct buffer_head * free_list[NR_SIZES] = {NULL, };

static struct buffer_head * unused_list = NULL;
-static struct buffer_head * reuse_list = NULL;
+struct buffer_head * reuse_list = NULL;
static struct wait_queue * buffer_wait = NULL;

-static int nr_buffers = 0;
-static int nr_buffers_type[NR_LIST] = {0,};
-static int nr_buffer_heads = 0;
-static int nr_unused_buffer_heads = 0;
-static int refilled = 0; /* Set NZ when a buffer freelist is refilled
- this is used by the loop device */
-
-/* this is used by some architectures to estimate available memory */
+int nr_buffers = 0;
+int nr_buffers_type[NR_LIST] = {0,};
+int nr_buffers_size[NR_SIZES] = {0,};
+int nr_buffers_st[NR_SIZES][NR_LIST] = {{0,},};
+int buffer_usage[NR_SIZES] = {0,}; /* Usage counts used to determine load average */
+int buffers_lav[NR_SIZES] = {0,}; /* Load average of buffer usage */
+int nr_free[NR_SIZES] = {0,};
int buffermem = 0;
+int nr_buffer_heads = 0;
+int refilled = 0; /* Set NZ when a buffer freelist is refilled */
+extern int *blksize_size[];

/* Here is the parameter block for the bdflush process. If you add or
* remove any of the parameters, make sure to update kernel/sysctl.c.
@@ -73,9 +79,8 @@
static void wakeup_bdflush(int);

#define N_PARAM 9
+#define LAV

-/* the dummy values in this structure are left in there for compatibility
- with old programs that play with the /proc entries */
union bdflush_param{
struct {
int nfract; /* Percentage of buffer cache dirty to
@@ -86,17 +91,26 @@
each time we call refill */
int nref_dirt; /* Dirty buffer threshold for activating bdflush
when trying to refill buffers. */
- int dummy1; /* unused */
+ int clu_nfract; /* Percentage of buffer cache to scan to
+ search for free clusters */
int age_buffer; /* Time for normal buffer to age before
we flush it */
int age_super; /* Time for superblock to age before we
flush it */
- int dummy2; /* unused */
- int dummy3; /* unused */
+ int lav_const; /* Constant used for load average (time
+ constant */
+ int lav_ratio; /* Used to determine how low a lav for a
+ particular size can go before we start to
+ trim back the buffers */
} b_un;
unsigned int data[N_PARAM];
} bdf_prm = {{60, 500, 64, 256, 15, 30*HZ, 5*HZ, 1884, 2}};

+/* The lav constant is set for 1 minute, as long as the update process runs
+ every 5 seconds. If you change the frequency of update, the time
+ constant will also change. */
+
+
/* These are the min and max parameter values that we will allow to be assigned */
int bdflush_min[N_PARAM] = { 0, 10, 5, 25, 0, 100, 100, 1, 1};
int bdflush_max[N_PARAM] = {100,5000, 2000, 2000,100, 60000, 60000, 2047, 5};
@@ -141,90 +155,77 @@
static int sync_buffers(kdev_t dev, int wait)
{
int i, retry, pass = 0, err = 0;
+ int nlist, ncount;
struct buffer_head * bh, *next;

/* One pass for no-wait, three for wait:
0) write out all dirty, unlocked buffers;
1) write out all dirty buffers, waiting if locked;
2) wait for completion by waiting for all buffers to unlock. */
- do {
- retry = 0;
-repeat:
+ repeat:
+ retry = 0;
+ repeat2:
+ ncount = 0;
/* We search all lists as a failsafe mechanism, not because we expect
there to be dirty buffers on any of the other lists. */
- bh = lru_list[BUF_DIRTY];
- if (!bh)
- goto repeat2;
- for (i = nr_buffers_type[BUF_DIRTY]*2 ; i-- > 0 ; bh = next) {
- if (bh->b_list != BUF_DIRTY)
- goto repeat;
- next = bh->b_next_free;
- if (!lru_list[BUF_DIRTY])
- break;
- if (dev && bh->b_dev != dev)
- continue;
- if (buffer_locked(bh)) {
- /* Buffer is locked; skip it unless wait is
- requested AND pass > 0. */
- if (!wait || !pass) {
- retry = 1;
- continue;
- }
- wait_on_buffer (bh);
- goto repeat;
- }
- /* If an unlocked buffer is not uptodate, there has
- been an IO error. Skip it. */
- if (wait && buffer_req(bh) && !buffer_locked(bh) &&
- !buffer_dirty(bh) && !buffer_uptodate(bh)) {
- err = 1;
- continue;
- }
- /* Don't write clean buffers. Don't write ANY buffers
- on the third pass. */
- if (!buffer_dirty(bh) || pass >= 2)
- continue;
- /* don't bother about locked buffers */
- if (buffer_locked(bh))
- continue;
- bh->b_count++;
- next->b_count++;
- bh->b_flushtime = 0;
- ll_rw_block(WRITE, 1, &bh);
- bh->b_count--;
- next->b_count--;
- retry = 1;
- }
-
- repeat2:
- bh = lru_list[BUF_LOCKED];
- if (!bh)
- break;
- for (i = nr_buffers_type[BUF_LOCKED]*2 ; i-- > 0 ; bh = next) {
- if (bh->b_list != BUF_LOCKED)
- goto repeat2;
- next = bh->b_next_free;
- if (!lru_list[BUF_LOCKED])
- break;
- if (dev && bh->b_dev != dev)
- continue;
- if (buffer_locked(bh)) {
- /* Buffer is locked; skip it unless wait is
- requested AND pass > 0. */
- if (!wait || !pass) {
- retry = 1;
- continue;
- }
- wait_on_buffer (bh);
- goto repeat2;
- }
- }
-
+ for(nlist = 0; nlist < NR_LIST; nlist++)
+ {
+ repeat1:
+ bh = lru_list[nlist];
+ if(!bh) continue;
+ for (i = nr_buffers_type[nlist]*2 ; i-- > 0 ; bh = next) {
+ if(bh->b_list != nlist) goto repeat1;
+ next = bh->b_next_free;
+ if(!lru_list[nlist]) break;
+ if (dev && bh->b_dev != dev)
+ continue;
+ if (buffer_locked(bh))
+ {
+ /* Buffer is locked; skip it unless wait is
+ requested AND pass > 0. */
+ if (!wait || !pass) {
+ retry = 1;
+ continue;
+ }
+ wait_on_buffer (bh);
+ goto repeat2;
+ }
+ /* If an unlocked buffer is not uptodate, there has
+ been an IO error. Skip it. */
+ if (wait && buffer_req(bh) && !buffer_locked(bh) &&
+ !buffer_dirty(bh) && !buffer_uptodate(bh)) {
+ err = 1;
+ continue;
+ }
+ /* Don't write clean buffers. Don't write ANY buffers
+ on the third pass. */
+ if (!buffer_dirty(bh) || pass>=2)
+ continue;
+ /* don't bother about locked buffers */
+ if (buffer_locked(bh))
+ continue;
+ bh->b_count++;
+ bh->b_flushtime = 0;
+ ll_rw_block(WRITE, 1, &bh);
+
+ if(nlist != BUF_DIRTY) {
+ printk("[%d %s %ld] ", nlist,
+ kdevname(bh->b_dev), bh->b_blocknr);
+ ncount++;
+ }
+ bh->b_count--;
+ retry = 1;
+ }
+ }
+ if (ncount)
+ printk("sys_sync: %d dirty buffers not on dirty list\n", ncount);
+
/* If we are waiting for the sync to succeed, and if any dirty
blocks were written, then repeat; on the second pass, only
wait for buffers being written (do not pass to write any
more buffers on the second pass). */
- } while (wait && retry && ++pass<=2);
+ if (wait && retry && ++pass<=2)
+ goto repeat;
return err;
}

@@ -311,7 +312,7 @@
}
}

-#define _hashfn(dev,block) (((unsigned)(HASHDEV(dev)^block))&HASH_MASK)
+#define _hashfn(dev,block) (((unsigned)(HASHDEV(dev)^block))%nr_hash)
#define hash(dev,block) hash_table[_hashfn(dev,block)]

static inline void remove_from_hash_queue(struct buffer_head * bh)
@@ -338,6 +339,11 @@
lru_list[bh->b_list] = bh->b_next_free;
if (lru_list[bh->b_list] == bh)
lru_list[bh->b_list] = NULL;
+ if (next_to_age[bh->b_list] == bh)
+ next_to_age[bh->b_list] = bh->b_next_free;
+ if (next_to_age[bh->b_list] == bh)
+ next_to_age[bh->b_list] = NULL;
+
bh->b_next_free = bh->b_prev_free = NULL;
}

@@ -350,6 +356,7 @@
panic("Free list corrupted");
if(!free_list[isize])
panic("Free list empty");
+ nr_free[isize]--;
if(bh->b_next_free == bh)
free_list[isize] = NULL;
else {
@@ -369,6 +376,7 @@
return;
}
nr_buffers_type[bh->b_list]--;
+ nr_buffers_st[BUFSIZE_INDEX(bh->b_size)][bh->b_list]--;
remove_from_hash_queue(bh);
remove_from_lru_list(bh);
}
@@ -379,6 +387,8 @@
return;
if (bh == lru_list[bh->b_list]) {
lru_list[bh->b_list] = bh->b_next_free;
+ if (next_to_age[bh->b_list] == bh)
+ next_to_age[bh->b_list] = bh->b_next_free;
return;
}
if(bh->b_dev == B_FREE)
@@ -390,6 +400,8 @@
lru_list[bh->b_list] = bh;
lru_list[bh->b_list]->b_prev_free = bh;
}
+ if (!next_to_age[bh->b_list])
+ next_to_age[bh->b_list] = bh;

bh->b_next_free = lru_list[bh->b_list];
bh->b_prev_free = lru_list[bh->b_list]->b_prev_free;
@@ -411,6 +423,7 @@
bh->b_prev_free = bh;
}

+ nr_free[isize]++;
bh->b_next_free = free_list[isize];
bh->b_prev_free = free_list[isize]->b_prev_free;
free_list[isize]->b_prev_free->b_next_free = bh;
@@ -428,13 +441,15 @@
lru_list[bh->b_list] = bh;
bh->b_prev_free = bh;
}
-
+ if (!next_to_age[bh->b_list])
+ next_to_age[bh->b_list] = bh;
if (bh->b_next_free) panic("VFS: buffer LRU pointers corrupted");
bh->b_next_free = lru_list[bh->b_list];
bh->b_prev_free = lru_list[bh->b_list]->b_prev_free;
lru_list[bh->b_list]->b_prev_free->b_next_free = bh;
lru_list[bh->b_list]->b_prev_free = bh;
nr_buffers_type[bh->b_list]++;
+ nr_buffers_st[BUFSIZE_INDEX(bh->b_size)][bh->b_list]++;
/* put the buffer in new hash-queue if it has a device */
bh->b_prev = NULL;
bh->b_next = NULL;
@@ -487,7 +502,6 @@

void set_blocksize(kdev_t dev, int size)
{
- extern int *blksize_size[];
int i, nlist;
struct buffer_head * bh, *bhnext;

@@ -523,7 +537,6 @@
continue;
if (bh->b_size == size)
continue;
-
bhnext->b_count++;
wait_on_buffer(bh);
bhnext->b_count--;
@@ -538,71 +551,26 @@
}
}

+#define BADNESS(bh) (buffer_dirty(bh) || buffer_locked(bh))

-/* check if a buffer is OK to be reclaimed */
-static inline int can_reclaim(struct buffer_head *bh, int size)
+void refill_freelist(int size)
{
- if (bh->b_count ||
- buffer_protected(bh) || buffer_locked(bh))
- return 0;
-
- if (mem_map[MAP_NR((unsigned long) bh->b_data)].count != 1 ||
- buffer_dirty(bh)) {
- refile_buffer(bh);
- return 0;
- }
-
- if (bh->b_size != size)
- return 0;
-
- return 1;
-}
-
-/* find a candidate buffer to be reclaimed */
-static struct buffer_head *find_candidate(struct buffer_head *list,int *list_len,int size)
-{
- struct buffer_head *bh;
-
- for (bh = list;
- bh && (*list_len) > 0;
- bh = bh->b_next_free, (*list_len)--) {
- if (size != bh->b_size) {
- /* this provides a mechanism for freeing blocks
- of other sizes, this is necessary now that we
- no longer have the lav code. */
- try_to_free_buffer(bh,&bh,1);
- if (!bh)
- break;
- continue;
- }
-
- if (buffer_locked(bh) &&
- (bh->b_list == BUF_LOCKED || bh->b_list == BUF_LOCKED1)) {
- /* Buffers are written in the order they are placed
- on the locked list. If we encounter a locked
- buffer here, this means that the rest of them
- are also locked */
- (*list_len) = 0;
- return NULL;
- }
-
- if (can_reclaim(bh,size))
- return bh;
- }
-
- return NULL;
-}
-
-static void refill_freelist(int size)
-{
- struct buffer_head * bh;
- struct buffer_head * candidate[BUF_DIRTY];
+ struct buffer_head * bh, * tmp;
+ struct buffer_head * candidate[NR_LIST];
unsigned int best_time, winner;
- int buffers[BUF_DIRTY];
+ int isize = BUFSIZE_INDEX(size);
+ int buffers[NR_LIST];
int i;
int needed;

- refilled = 1;
+ /* First see if we even need this. Sometimes it is advantageous
+ to request some blocks in a filesystem that we know that we will
+ be needing ahead of time. */
+
+ if (nr_free[isize] > 100)
+ return;
+
+ ++refilled;
/* If there are too many dirty buffers, we wake up the update process
now so as to ensure that there are still clean buffers available
for user processes to use (and dirty) */
@@ -615,9 +583,17 @@
needed -= PAGE_SIZE;
}

-repeat:
- if(needed <= 0)
- return;
+ if(needed <= 0) return;
+
+ /* See if there are too many buffers of a different size.
+ If so, victimize them */
+
+ while(maybe_shrink_lav_buffers(size))
+ {
+ if(!grow_buffers(GFP_BUFFER, size)) break;
+ needed -= PAGE_SIZE;
+ if(needed <= 0) return;
+ };

/* OK, we cannot grow the buffer cache, now try to get some
from the lru list */
@@ -625,15 +601,54 @@
/* First set the candidate pointers to usable buffers. This
should be quick nearly all of the time. */

- for(i=0; i<BUF_DIRTY; i++){
+repeat0:
+ for(i=0; i<NR_LIST; i++){
+ if(i == BUF_DIRTY || i == BUF_SHARED ||
+ nr_buffers_type[i] == 0) {
+ candidate[i] = NULL;
+ buffers[i] = 0;
+ continue;
+ }
buffers[i] = nr_buffers_type[i];
- candidate[i] = find_candidate(lru_list[i], &buffers[i], size);
+ for (bh = lru_list[i]; buffers[i] > 0; bh = tmp, buffers[i]--)
+ {
+ if(buffers[i] < 0) panic("Here is the problem");
+ tmp = bh->b_next_free;
+ if (!bh) break;
+
+ if (mem_map[MAP_NR((unsigned long) bh->b_data)].count != 1 ||
+ buffer_dirty(bh)) {
+ refile_buffer(bh);
+ continue;
+ }
+
+ if (bh->b_count || buffer_protected(bh) || bh->b_size != size)
+ continue;
+
+ /* Buffers are written in the order they are placed
+ on the locked list. If we encounter a locked
+ buffer here, this means that the rest of them
+ are also locked */
+ if (buffer_locked(bh) && (i == BUF_LOCKED || i == BUF_LOCKED1)) {
+ buffers[i] = 0;
+ break;
+ }
+
+ if (BADNESS(bh)) continue;
+ break;
+ };
+ if(!buffers[i]) candidate[i] = NULL; /* Nothing on this list */
+ else candidate[i] = bh;
+ if(candidate[i] && candidate[i]->b_count) panic("Here is the problem");
}

+ repeat:
+ if(needed <= 0) return;
+
/* Now see which candidate wins the election */

winner = best_time = UINT_MAX;
- for(i=0; i<BUF_DIRTY; i++){
+ for(i=0; i<NR_LIST; i++){
if(!candidate[i]) continue;
if(candidate[i]->b_lru_time < best_time){
best_time = candidate[i]->b_lru_time;
@@ -644,37 +659,79 @@
/* If we have a winner, use it, and then get a new candidate from that list */
if(winner != UINT_MAX) {
i = winner;
- while (needed>0 && (bh=candidate[i])) {
- candidate[i] = bh->b_next_free;
- if(candidate[i] == bh) candidate[i] = NULL; /* Got last one */
- remove_from_queues(bh);
- bh->b_dev = B_FREE;
- put_last_free(bh);
- needed -= bh->b_size;
- buffers[i]--;
- if(buffers[i] == 0) candidate[i] = NULL;
+ bh = candidate[i];
+ candidate[i] = bh->b_next_free;
+ if(candidate[i] == bh) candidate[i] = NULL; /* Got last one */
+ if (bh->b_count || bh->b_size != size)
+ panic("Busy buffer in candidate list\n");
+ if (mem_map[MAP_NR((unsigned long) bh->b_data)].count != 1)
+ panic("Shared buffer in candidate list\n");
+ if (buffer_protected(bh))
+ panic("Protected buffer in candidate list\n");
+ if (BADNESS(bh)) panic("Buffer in candidate list with BADNESS != 0\n");
+
+ if(bh->b_dev == B_FREE)
+ panic("Wrong list");
+ remove_from_queues(bh);
+ bh->b_dev = B_FREE;
+ put_last_free(bh);
+ needed -= bh->b_size;
+ buffers[i]--;
+ if(buffers[i] < 0) panic("Here is the problem");

- if (candidate[i] && !can_reclaim(candidate[i],size))
- candidate[i] = find_candidate(candidate[i],&buffers[i], size);
+ if(buffers[i] == 0) candidate[i] = NULL;
+
+ /* Now all we need to do is advance the candidate pointer
+ from the winner list to the next usable buffer */
+ if(candidate[i] && buffers[i] > 0){
+ if(buffers[i] <= 0) panic("Here is another problem");
+ for (bh = candidate[i]; buffers[i] > 0; bh = tmp, buffers[i]--) {
+ if(buffers[i] < 0) panic("Here is the problem");
+ tmp = bh->b_next_free;
+ if (!bh) break;
+
+ if (mem_map[MAP_NR((unsigned long) bh->b_data)].count != 1 ||
+ buffer_dirty(bh)) {
+ refile_buffer(bh);
+ continue;
+ };
+
+ if (bh->b_count || buffer_protected(bh) || bh->b_size != size)
+ continue;
+
+ /* Buffers are written in the order they are
+ placed on the locked list. If we encounter
+ a locked buffer here, this means that the
+ rest of them are also locked */
+ if (buffer_locked(bh) && (i == BUF_LOCKED || i == BUF_LOCKED1)) {
+ buffers[i] = 0;
+ break;
+ }
+
+ if (BADNESS(bh)) continue;
+ break;
+ };
+ if(!buffers[i]) candidate[i] = NULL; /* Nothing here */
+ else candidate[i] = bh;
+ if(candidate[i] && candidate[i]->b_count)
+ panic("Here is the problem");
}
+
goto repeat;
}

- /* Too bad, that was not enough. Try a little harder to grow some. */
+ if(needed <= 0) return;
+
+ /* Too bad, that was not enough. Try a little harder to grow some
+ * and repeat until we find something good.
+ */

- if (nr_free_pages > min_free_pages + 5) {
- if (grow_buffers(GFP_BUFFER, size)) {
- needed -= PAGE_SIZE;
- goto repeat;
- };
- }
-
/* and repeat until we find something good */
- if (grow_buffers(GFP_ATOMIC, size))
+ if (grow_buffers(GFP_BUFFER, size))
needed -= PAGE_SIZE;
else
wakeup_bdflush(1);
- goto repeat;
+ goto repeat0;
}

/*
@@ -692,6 +749,9 @@
struct buffer_head * bh;
int isize = BUFSIZE_INDEX(size);

+ /* Update this for the buffer size lav. */
+ buffer_usage[isize]++;
+
/* If there are too many dirty buffers, we wake up the update process
now so as to ensure that there are still clean buffers available
for user processes to use (and dirty) */
@@ -707,9 +767,7 @@
return bh;
}

- while(!free_list[isize]) {
- refill_freelist(size);
- }
+ while(!free_list[isize]) refill_freelist(size);

if (find_buffer(dev,block,size))
goto repeat;
@@ -751,6 +809,7 @@
void refile_buffer(struct buffer_head * buf)
{
int dispose;
+ int isize;

if(buf->b_dev == B_FREE) {
printk("Attempt to refile free buffer\n");
@@ -758,13 +817,17 @@
}
if (buffer_dirty(buf))
dispose = BUF_DIRTY;
+ else if ((mem_map[MAP_NR((unsigned long) buf->b_data)].count > 1) || buffer_protected(buf))
+ dispose = BUF_SHARED;
else if (buffer_locked(buf))
dispose = BUF_LOCKED;
+ else if (buf->b_list == BUF_SHARED)
+ dispose = BUF_UNSHARED;
else
dispose = BUF_CLEAN;
if(dispose == BUF_CLEAN) buf->b_lru_time = jiffies;
if(dispose != buf->b_list) {
- if(dispose == BUF_DIRTY)
+ if(dispose == BUF_DIRTY || dispose == BUF_UNSHARED)
buf->b_lru_time = jiffies;
if(dispose == BUF_LOCKED &&
(buf->b_flushtime - buf->b_lru_time) <= bdf_prm.b_un.age_super)
@@ -775,13 +838,16 @@
if (dispose == BUF_DIRTY) {
/* This buffer is dirty, maybe we need to start flushing. */
/* If too high a percentage of the buffers are dirty... */
- if (nr_buffers_type[BUF_DIRTY] > nr_buffers * bdf_prm.b_un.nfract/100)
+ if (nr_buffers_type[BUF_DIRTY] >
+ (nr_buffers - nr_buffers_type[BUF_SHARED]) *
+ bdf_prm.b_un.nfract/100)
wakeup_bdflush(0);
/* If this is a loop device, and
- * more than half of the buffers are dirty... */
+ * more than half of the buffers of this size are dirty... */
/* (Prevents no-free-buffers deadlock with loop device.) */
+ isize = BUFSIZE_INDEX(buf->b_size);
if (MAJOR(buf->b_dev) == LOOP_MAJOR &&
- nr_buffers_type[BUF_DIRTY]*2>nr_buffers)
+ nr_buffers_st[isize][BUF_DIRTY]*2>nr_buffers_size[isize])
wakeup_bdflush(1);
}
}
@@ -908,15 +974,11 @@
return NULL;
}

+/*
+ * See fs/inode.c for the weird use of volatile..
+ */
static void put_unused_buffer_head(struct buffer_head * bh)
{
- if (nr_unused_buffer_heads >= MAX_UNUSED_BUFFERS) {
- nr_buffer_heads--;
- kfree(bh);
- return;
- }
- memset(bh,0,sizeof(*bh));
- nr_unused_buffer_heads++;
bh->b_next_free = unused_list;
unused_list = bh;
wake_up(&buffer_wait);
@@ -924,23 +986,26 @@

static void get_more_buffer_heads(void)
{
+ int i;
struct buffer_head * bh;

- while (!unused_list) {
+ for (;;) {
+ if (unused_list)
+ return;
+
/*
* This is critical. We can't swap out pages to get
* more buffer heads, because the swap-out may need
* more buffer-heads itself. Thus GFP_ATOMIC.
+ *
+ * This is no longer true, it is GFP_BUFFER again, the
+ * swapping code now knows not to perform I/O when that
+ * GFP level is specified... -DaveM
*/
- /* we now use kmalloc() here instead of gfp as we want
- to be able to easily release buffer heads - they
- took up quite a bit of memory (tridge) */
- bh = (struct buffer_head *) kmalloc(sizeof(*bh),GFP_ATOMIC);
- if (bh) {
- put_unused_buffer_head(bh);
- nr_buffer_heads++;
- return;
- }
+
+ bh = (struct buffer_head *) get_free_page(GFP_BUFFER);
+ if (bh)
+ break;

/*
* Uhhuh. We're _really_ low on memory. Now we just
@@ -951,6 +1016,10 @@
sleep_on(&buffer_wait);
}

+ for (nr_buffer_heads+=i=PAGE_SIZE/sizeof*bh ; i>0; i--) {
+ bh->b_next_free = unused_list; /* only make link */
+ unused_list = bh++;
+ }
}

/*
@@ -969,15 +1038,17 @@
static inline void recover_reusable_buffer_heads(void)
{
if (reuse_list) {
- struct buffer_head *head;
-
- head = xchg(&reuse_list, NULL);
+ struct buffer_head *bh;
+ unsigned long flags;

+ save_flags(flags);
do {
- struct buffer_head *bh = head;
- head = head->b_next_free;
+ cli();
+ bh = reuse_list;
+ reuse_list = bh->b_next_free;
+ restore_flags(flags);
put_unused_buffer_head(bh);
- } while (head);
+ } while (reuse_list);
}
}

@@ -991,7 +1062,6 @@
return NULL;
bh = unused_list;
unused_list = bh->b_next_free;
- nr_unused_buffer_heads--;
return bh;
}

@@ -1146,7 +1216,7 @@
arr[nr++] = next;
} while (prev = next, (next = next->b_this_page) != NULL);
prev->b_this_page = bh;
-
+
if (nr) {
ll_rw_block(rw, nr, arr);
/* The rest of the work is done in mark_buffer_uptodate()
@@ -1271,7 +1341,7 @@
page->count++;
set_bit(PG_locked, &page->flags);
set_bit(PG_free_after, &page->flags);
-
+
i = PAGE_SIZE >> inode->i_sb->s_blocksize_bits;
block = page->offset >> inode->i_sb->s_blocksize_bits;
p = nr;
@@ -1317,6 +1387,7 @@

tmp = bh;
while (1) {
+ nr_free[isize]++;
if (insert_point) {
tmp->b_next_free = insert_point->b_next_free;
tmp->b_prev_free = insert_point;
@@ -1328,6 +1399,7 @@
}
insert_point = tmp;
++nr_buffers;
+ ++nr_buffers_size[isize];
if (tmp->b_this_page)
tmp = tmp->b_this_page;
else
@@ -1357,6 +1429,7 @@
{
unsigned long page;
struct buffer_head * tmp, * p;
+ int isize = BUFSIZE_INDEX(bh->b_size);

*bhp = bh;
page = (unsigned long) bh->b_data;
@@ -1378,6 +1451,7 @@
p = tmp;
tmp = tmp->b_this_page;
nr_buffers--;
+ nr_buffers_size[isize]--;
if (p == *bhp)
{
*bhp = p->b_prev_free;
@@ -1393,6 +1467,178 @@
return !mem_map[MAP_NR(page)].count;
}

+/* Age buffers on a given page, according to whether they have been
+ visited recently or not. */
+static inline void age_buffer(struct buffer_head *bh)
+{
+ struct buffer_head *tmp = bh;
+ int touched = 0;
+
+ /*
+ * When we age a page, we mark all other buffers in the page
+ * with the "has_aged" flag. Then, when these aliased buffers
+ * come up for aging, we skip them until next pass. This
+ * ensures that a page full of multiple buffers only gets aged
+ * once per pass through the lru lists.
+ */
+ if (clear_bit(BH_Has_aged, &bh->b_state))
+ return;
+
+ do {
+ touched |= clear_bit(BH_Touched, &tmp->b_state);
+ tmp = tmp->b_this_page;
+ set_bit(BH_Has_aged, &tmp->b_state);
+ } while (tmp != bh);
+ clear_bit(BH_Has_aged, &bh->b_state);
+
+ if (touched)
+ touch_page(mem_map + MAP_NR((unsigned long) bh->b_data));
+ else
+ age_page(mem_map + MAP_NR((unsigned long) bh->b_data));
+}
+
+/*
+ * Consult the load average for buffers and decide whether or not
+ * we should shrink the buffers of one size or not. If we decide yes,
+ * do it and return 1. Else return 0. Do not attempt to shrink size
+ * that is specified.
+ *
+ * I would prefer not to use a load average, but the way things are now it
+ * seems unavoidable. The way to get rid of it would be to force clustering
+ * universally, so that when we reclaim buffers we always reclaim an entire
+ * page. Doing this would mean that we all need to move towards QMAGIC.
+ */
+
+static int maybe_shrink_lav_buffers(int size)
+{
+ int nlist;
+ int isize;
+ int total_lav, total_n_buffers, n_sizes;
+
+ /* Do not consider the shared buffers since they would not tend
+ to have getblk called very often, and this would throw off
+ the lav. They are not easily reclaimable anyway (let the swapper
+ make the first move). */
+
+ total_lav = total_n_buffers = n_sizes = 0;
+ for(nlist = 0; nlist < NR_SIZES; nlist++)
+ {
+ total_lav += buffers_lav[nlist];
+ if(nr_buffers_size[nlist]) n_sizes++;
+ total_n_buffers += nr_buffers_size[nlist];
+ total_n_buffers -= nr_buffers_st[nlist][BUF_SHARED];
+ }
+
+ /* See if we have an excessive number of buffers of a particular
+ size - if so, victimize that bunch. */
+
+ isize = (size ? BUFSIZE_INDEX(size) : -1);
+
+ if (n_sizes > 1)
+ for(nlist = 0; nlist < NR_SIZES; nlist++)
+ {
+ if(nlist == isize) continue;
+ if(nr_buffers_size[nlist] &&
+ (bdf_prm.b_un.lav_const*buffers_lav[nlist] - total_lav) * total_n_buffers <
+ total_lav *
+ (nr_buffers_size[nlist] - nr_buffers_st[nlist][BUF_SHARED] - total_n_buffers))
+ if(shrink_specific_buffers(6, bufferindex_size[nlist]))
+ return 1;
+ }
+ return 0;
+}
+
+/*
+ * Try to free up some pages by shrinking the buffer-cache
+ *
+ * Priority tells the routine how hard to try to shrink the
+ * buffers: 6 means "don't bother too much", while a value
+ * of 0 means "we'd better get some free pages now".
+ *
+ * "limit" is meant to limit the shrink-action only to pages
+ * that are in the 0 - limit address range, for DMA re-allocations.
+ * We ignore that right now.
+ */
+
+static int shrink_specific_buffers(unsigned int priority, int size)
+{
+ struct buffer_head *bh;
+ int nlist;
+ int i, isize, isize1;
+
+#ifdef DEBUG
+ if(size) printk("Shrinking buffers of size %d\n", size);
+#endif
+ /* First try the free lists, and see if we can get a complete page
+ from here */
+ isize1 = (size ? BUFSIZE_INDEX(size) : -1);
+
+ for(isize = 0; isize<NR_SIZES; isize++){
+ if(isize1 != -1 && isize1 != isize) continue;
+ bh = free_list[isize];
+ if(!bh) continue;
+ for (i=0 ; !i || bh != free_list[isize]; bh = bh->b_next_free, i++) {
+ if (bh->b_count || buffer_protected(bh) ||
+ !bh->b_this_page)
+ continue;
+ if (!age_of((unsigned long) bh->b_data) &&
+ try_to_free_buffer(bh, &bh, 6))
+ return 1;
+ if(!bh) break;
+ /* Some interrupt must have used it after we
+ freed the page. No big deal - keep looking */
+ }
+ }
+
+ /* Not enough in the free lists, now try the lru list */
+
+ for(nlist = 0; nlist < NR_LIST; nlist++) {
+ repeat1:
+ if(priority > 2 && nlist == BUF_SHARED) continue;
+ i = nr_buffers_type[nlist];
+ i = ((BUFFEROUT_WEIGHT * i) >> 10) >> priority;
+ for ( ; i > 0; i-- ) {
+ bh = next_to_age[nlist];
+ if (!bh)
+ break;
+ next_to_age[nlist] = bh->b_next_free;
+
+ /* First, age the buffer. */
+ age_buffer(bh);
+ /* We may have stalled while waiting for I/O
+ to complete. */
+ if(bh->b_list != nlist) goto repeat1;
+ if (bh->b_count || buffer_protected(bh) ||
+ !bh->b_this_page)
+ continue;
+ if(size && bh->b_size != size) continue;
+ if (buffer_locked(bh))
+ if (priority)
+ continue;
+ else
+ wait_on_buffer(bh);
+ if (buffer_dirty(bh)) {
+ bh->b_count++;
+ bh->b_flushtime = 0;
+ ll_rw_block(WRITEA, 1, &bh);
+ bh->b_count--;
+ continue;
+ }
+ /* At priority 6, only consider really old
+ (age==0) buffers for reclaiming. At
+ priority 0, consider any buffers. */
+ if ((age_of((unsigned long) bh->b_data) >>
+ (6-priority)) > 0)
+ continue;
+ if (try_to_free_buffer(bh, &bh, 0))
+ return 1;
+ if(!bh) break;
+ }
+ }
+ return 0;
+}
+
+
/* ================== Debugging =================== */

void show_buffers(void)
@@ -1400,18 +1646,17 @@
struct buffer_head * bh;
int found = 0, locked = 0, dirty = 0, used = 0, lastused = 0;
int protected = 0;
- int nlist;
- static char *buf_types[NR_LIST] = {"CLEAN","LOCKED","LOCKED1","DIRTY"};
+ int shared;
+ int nlist, isize;

printk("Buffer memory: %6dkB\n",buffermem>>10);
printk("Buffer heads: %6d\n",nr_buffer_heads);
printk("Buffer blocks: %6d\n",nr_buffers);

for(nlist = 0; nlist < NR_LIST; nlist++) {
- found = locked = dirty = used = lastused = protected = 0;
+ shared = found = locked = dirty = used = lastused = protected = 0;
bh = lru_list[nlist];
if(!bh) continue;
-
do {
found++;
if (buffer_locked(bh))
@@ -1420,31 +1665,234 @@
protected++;
if (buffer_dirty(bh))
dirty++;
+ if (mem_map[MAP_NR(((unsigned long) bh->b_data))].count != 1)
+ shared++;
if (bh->b_count)
used++, lastused = found;
bh = bh->b_next_free;
} while (bh != lru_list[nlist]);
- printk("%8s: %d buffers, %d used (last=%d), "
- "%d locked, %d protected, %d dirty\n",
- buf_types[nlist], found, used, lastused,
- locked, protected, dirty);
+ printk("Buffer[%d] mem: %d buffers, %d used (last=%d), "
+ "%d locked, %d protected, %d dirty %d shrd\n",
+ nlist, found, used, lastused,
+ locked, protected, dirty, shared);
+ };
+ printk("Size [LAV] Free Clean Unshar Lck Lck1 Dirty Shared \n");
+ for(isize = 0; isize<NR_SIZES; isize++){
+ printk("%5d [%5d]: %7d ", bufferindex_size[isize],
+ buffers_lav[isize], nr_free[isize]);
+ for(nlist = 0; nlist < NR_LIST; nlist++)
+ printk("%7d ", nr_buffers_st[isize][nlist]);
+ printk("\n");
+ }
+}
+
+
+/* ====================== Cluster patches for ext2 ==================== */
+
+/*
+ * try_to_reassign() checks if all the buffers on this particular page
+ * are unused, and reassign to a new cluster them if this is true.
+ */
+static inline int try_to_reassign(struct buffer_head * bh, struct buffer_head ** bhp,
+ kdev_t dev, unsigned int starting_block)
+{
+ unsigned long page;
+ struct buffer_head * tmp, * p;
+
+ *bhp = bh;
+ page = (unsigned long) bh->b_data;
+ page &= PAGE_MASK;
+ if(mem_map[MAP_NR(page)].count != 1) return 0;
+ tmp = bh;
+ do {
+ if (!tmp)
+ return 0;
+
+ if (tmp->b_count || buffer_protected(tmp) ||
+ buffer_dirty(tmp) || buffer_locked(tmp))
+ return 0;
+ tmp = tmp->b_this_page;
+ } while (tmp != bh);
+ tmp = bh;
+
+ while((unsigned long) tmp->b_data & (PAGE_SIZE - 1))
+ tmp = tmp->b_this_page;
+
+ /* This is the buffer at the head of the page */
+ bh = tmp;
+ do {
+ p = tmp;
+ tmp = tmp->b_this_page;
+ remove_from_queues(p);
+ p->b_dev = dev;
+ mark_buffer_uptodate(p, 0);
+ clear_bit(BH_Req, &p->b_state);
+ p->b_blocknr = starting_block++;
+ insert_into_queues(p);
+ } while (tmp != bh);
+ return 1;
+}
+
+/*
+ * Try to find a free cluster by locating a page where
+ * all of the buffers are unused. We would like this function
+ * to be atomic, so we do not call anything that might cause
+ * the process to sleep. The priority is somewhat similar to
+ * the priority used in shrink_buffers.
+ *
+ * My thinking is that the kernel should end up using whole
+ * pages for the buffer cache as much of the time as possible.
+ * This way the other buffers on a particular page are likely
+ * to be very near each other on the free list, and we will not
+ * be expiring data prematurely. For now we only cannibalize buffers
+ * of the same size to keep the code simpler.
+ */
+static int reassign_cluster(kdev_t dev,
+ unsigned int starting_block, int size)
+{
+ struct buffer_head *bh;
+ int isize = BUFSIZE_INDEX(size);
+ int i;
+
+ /* We want to give ourselves a really good shot at generating
+ a cluster, and since we only take buffers from the free
+ list, we "overfill" it a little. */
+
+ while(nr_free[isize] < 32) refill_freelist(size);
+
+ bh = free_list[isize];
+ if(bh)
+ for (i=0 ; !i || bh != free_list[isize] ; bh = bh->b_next_free, i++) {
+ if (!bh->b_this_page) continue;
+ if (try_to_reassign(bh, &bh, dev, starting_block))
+ return 4;
+ }
+ return 0;
+}
+
+/* This function tries to generate a new cluster of buffers
+ * from a new page in memory. We should only do this if we have
+ * not expanded the buffer cache to the maximum size that we allow.
+ */
+static unsigned long try_to_generate_cluster(kdev_t dev, int block, int size)
+{
+ struct buffer_head * bh, * tmp, * arr[MAX_BUF_PER_PAGE];
+ int isize = BUFSIZE_INDEX(size);
+ unsigned long offset;
+ unsigned long page;
+ int nblock;
+
+ page = get_free_page(GFP_NOBUFFER);
+ if(!page) return 0;
+
+ bh = create_buffers(page, size);
+ if (!bh) {
+ free_page(page);
+ return 0;
};
+ nblock = block;
+ for (offset = 0 ; offset < PAGE_SIZE ; offset += size) {
+ if (find_buffer(dev, nblock++, size))
+ goto not_aligned;
+ }
+ tmp = bh;
+ nblock = 0;
+ while (1) {
+ arr[nblock++] = bh;
+ bh->b_count = 1;
+ bh->b_flushtime = 0;
+ bh->b_state = 0;
+ bh->b_dev = dev;
+ bh->b_list = BUF_CLEAN;
+ bh->b_blocknr = block++;
+ nr_buffers++;
+ nr_buffers_size[isize]++;
+ insert_into_queues(bh);
+ if (bh->b_this_page)
+ bh = bh->b_this_page;
+ else
+ break;
+ }
+ buffermem += PAGE_SIZE;
+ mem_map[MAP_NR(page)].buffers = bh;
+ bh->b_this_page = tmp;
+ while (nblock-- > 0)
+ brelse(arr[nblock]);
+ return 4; /* ?? */
+not_aligned:
+ while ((tmp = bh) != NULL) {
+ bh = bh->b_this_page;
+ put_unused_buffer_head(tmp);
+ }
+ free_page(page);
+ return 0;
+}
+
+unsigned long generate_cluster(kdev_t dev, int b[], int size)
+{
+ int i, offset;
+
+ for (i = 0, offset = 0 ; offset < PAGE_SIZE ; i++, offset += size) {
+ if(i && b[i]-1 != b[i-1]) return 0; /* No need to cluster */
+ if(find_buffer(dev, b[i], size)) return 0;
+ };
+
+ /* OK, we have a candidate for a new cluster */
+
+ /* See if one size of buffer is over-represented in the buffer cache,
+ if so reduce the numbers of buffers */
+ if(maybe_shrink_lav_buffers(size))
+ {
+ int retval;
+ retval = try_to_generate_cluster(dev, b[0], size);
+ if(retval) return retval;
+ };
+
+ if (nr_free_pages > min_free_pages*2)
+ return try_to_generate_cluster(dev, b[0], size);
+ else
+ return reassign_cluster(dev, b[0], size);
}

+
/* ===================== Init ======================= */

/*
- * allocate the hash table and init the free list
+ * This initializes the initial buffer free list. nr_buffers_type is set
+ * to one less the actual number of buffers, as a sop to backwards
+ * compatibility --- the old code did this (I think unintentionally,
+ * but I'm not sure), and programs in the ps package expect it.
+ * - TYT 8/30/92
*/
void buffer_init(void)
{
- hash_table = (struct buffer_head **)vmalloc(NR_HASH*sizeof(struct buffer_head *));
- if (!hash_table)
- panic("Failed to allocate buffer hash table\n");
- memset(hash_table,0,NR_HASH*sizeof(struct buffer_head *));
+ int i;
+ int isize = BUFSIZE_INDEX(BLOCK_SIZE);
+ long memsize = MAP_NR(high_memory) << PAGE_SHIFT;
+
+ if (memsize >= 64*1024*1024)
+ nr_hash = 65521;
+ else if (memsize >= 32*1024*1024)
+ nr_hash = 32749;
+ else if (memsize >= 16*1024*1024)
+ nr_hash = 16381;
+ else if (memsize >= 8*1024*1024)
+ nr_hash = 8191;
+ else if (memsize >= 4*1024*1024)
+ nr_hash = 4093;
+ else nr_hash = 997;
+
+ hash_table = (struct buffer_head **) vmalloc(nr_hash *
+ sizeof(struct buffer_head *));

+
+ for (i = 0 ; i < nr_hash ; i++)
+ hash_table[i] = NULL;
lru_list[BUF_CLEAN] = 0;
grow_buffers(GFP_KERNEL, BLOCK_SIZE);
+ if (!free_list[isize])
+ panic("VFS: Unable to initialize buffer free list!");
+ return;
}


@@ -1480,7 +1928,7 @@

asmlinkage int sync_old_buffers(void)
{
- int i;
+ int i, isize;
int ndirty, nwritten;
int nlist;
int ncount;
@@ -1499,7 +1947,6 @@
ndirty = 0;
nwritten = 0;
repeat:
-
bh = lru_list[nlist];
if(bh)
for (i = nr_buffers_type[nlist]; i-- > 0; bh = next) {
@@ -1539,6 +1986,13 @@
printk("Wrote %d/%d buffers\n", nwritten, ndirty);
#endif

+ /* We assume that we only come through here on a regular
+ schedule, like every 5 seconds. Now update load averages.
+ Shift usage counts to prevent overflow. */
+ for(isize = 0; isize<NR_SIZES; isize++){
+ CALC_LOAD(buffers_lav[isize], bdf_prm.b_un.lav_const, buffer_usage[isize]);
+ buffer_usage[isize] = 0;
+ }
return 0;
}

@@ -1550,6 +2004,8 @@

asmlinkage int sys_bdflush(int func, long data)
{
+ int i, error;
+
if (!suser())
return -EPERM;

@@ -1558,24 +2014,26 @@

/* Basically func 1 means read param 1, 2 means write param 1, etc */
if (func >= 2) {
- int i = (func-2) >> 1;
+ i = (func-2) >> 1;
if (i < 0 || i >= N_PARAM)
return -EINVAL;
if((func & 1) == 0) {
- int error = verify_area(VERIFY_WRITE, (int*)data, 4);
- if (!error)
- put_user(bdf_prm.data[i], (int*)data);
- return error;
- }
+ error = verify_area(VERIFY_WRITE, (void *) data, sizeof(int));
+ if (error)
+ return error;
+ put_user(bdf_prm.data[i], (int*)data);
+ return 0;
+ };
if (data < bdflush_min[i] || data > bdflush_max[i])
return -EINVAL;
bdf_prm.data[i] = data;
- }
+ return 0;
+ };

/* Having func 0 used to launch the actual bdflush and then never
- * return (unless explicitly killed). We return zero here to
- * remain semi-compatible with present update(8) programs.
- */
+ return (unless explicitly killed). We return zero here to
+ remain semi-compatible with present update(8) programs. */
+
return 0;
}

@@ -1617,12 +2075,12 @@
* and other internals and thus be subject to the SMP locking
* rules. (On a uniprocessor box this does nothing).
*/
-
-
+
#ifdef __SMP__
lock_kernel();
syscall_count++;
#endif
+
for (;;) {
#ifdef DEBUG
printk("bdflush() activated...");
@@ -1638,7 +2096,6 @@
ndirty = 0;
refilled = 0;
repeat:
-
bh = lru_list[nlist];
if(bh)
for (i = nr_buffers_type[nlist]; i-- > 0 && ndirty < bdf_prm.b_un.ndirty;
@@ -1701,7 +2158,9 @@

/* If there are still a lot of dirty buffers around, skip the sleep
and flush some more */
- if(nr_buffers_type[BUF_DIRTY] <= nr_buffers * bdf_prm.b_un.nfract/100) {
+
+ if(nr_buffers_type[BUF_DIRTY] <= (nr_buffers - nr_buffers_type[BUF_SHARED]) *
+ bdf_prm.b_un.nfract/100) {
current->signal = 0;
interruptible_sleep_on(&bdflush_wait);
}
diff -urN -x *~1 linux-2.0.31-clean/include/linux/fs.h linux/include/linux/fs.h
--- linux-2.0.31-clean/include/linux/fs.h Sat Mar 29 01:08:17 1997
+++ linux/include/linux/fs.h Mon Jun 30 15:24:00 1997
@@ -564,17 +564,20 @@

extern void refile_buffer(struct buffer_head * buf);
extern void set_writetime(struct buffer_head * buf, int flag);
+extern void refill_freelist(int size);
extern int try_to_free_buffer(struct buffer_head*, struct buffer_head**, int);

extern int nr_buffers;
extern int buffermem;
extern int nr_buffer_heads;

-#define BUF_CLEAN 0
-#define BUF_LOCKED 1 /* Buffers scheduled for write */
-#define BUF_LOCKED1 2 /* Supers, inodes */
-#define BUF_DIRTY 3 /* Dirty buffers, not yet scheduled for write */
-#define NR_LIST 4
+#define BUF_CLEAN 0
+#define BUF_UNSHARED 1 /* Buffers that were shared but are not any more */
+#define BUF_LOCKED 2 /* Buffers scheduled for write */
+#define BUF_LOCKED1 3 /* Supers, inodes */
+#define BUF_DIRTY 4 /* Dirty buffers, not yet scheduled for write */
+#define BUF_SHARED 5 /* Buffers shared */
+#define NR_LIST 6

void mark_buffer_uptodate(struct buffer_head * bh, int on);

diff -urN -x *~1 linux-2.0.31-clean/include/linux/mm.h linux/include/linux/mm.h
--- linux-2.0.31-clean/include/linux/mm.h Sat Mar 29 01:08:17 1997
+++ linux/include/linux/mm.h Mon Jun 30 15:24:00 1997
@@ -295,7 +295,7 @@

/* filemap.c */
extern unsigned long page_unuse(unsigned long);
-extern int shrink_mmap(int, int);
+extern int shrink_mmap(int, int, int);
extern void truncate_inode_pages(struct inode *, unsigned long);

#define GFP_BUFFER 0x00
diff -urN -x *~1 linux-2.0.31-clean/mm/filemap.c linux/mm/filemap.c
--- linux-2.0.31-clean/mm/filemap.c Tue Jun 10 12:58:48 1997
+++ linux/mm/filemap.c Mon Jun 30 15:22:44 1997
@@ -127,7 +127,7 @@
}
}

-int shrink_mmap(int priority, int dma)
+int shrink_mmap(int priority, int dma, int can_do_io)
{
static int clock = 0;
struct page * page;
@@ -183,13 +183,16 @@
}

/* is it a buffer cache page? */
- if (bh && try_to_free_buffer(bh, &bh, 6))
+ if (can_do_io && bh && try_to_free_buffer(bh, &bh, 6))
return 1;
break;

default:
/* more than one users: we can't throw it away */
- set_bit(PG_referenced, &page->flags);
+ /* Original: make the shared page inviolable. */
+ /* >set_bit(PG_referenced, &page->flags); */
+ /* Modified: make it good candidate for swapping out. */
+ page->age = 0;
/* fall through */
case 0:
/* nothing */
diff -urN -x *~1 linux-2.0.31-clean/mm/page_alloc.c linux/mm/page_alloc.c
--- linux-2.0.31-clean/mm/page_alloc.c Sat Aug 17 20:19:29 1996
+++ linux/mm/page_alloc.c Tue Jun 17 13:43:33 1997
@@ -202,9 +202,14 @@
priority = GFP_ATOMIC;
}
}
- reserved_pages = 5;
- if (priority != GFP_NFS)
- reserved_pages = min_free_pages;
+ switch (priority) {
+ case GFP_NFS:
+ case GFP_BUFFER:
+ reserved_pages = 5;
+ break;
+ default:
+ reserved_pages = min_free_pages;
+ }
save_flags(flags);
repeat:
cli();
@@ -214,7 +219,7 @@
return 0;
}
restore_flags(flags);
- if (priority != GFP_BUFFER && try_to_free_page(priority, dma, 1))
+ if (try_to_free_page(priority, dma, 1))
goto repeat;
return 0;
}
@@ -264,11 +269,11 @@

/*
* select nr of pages we try to keep free for important stuff
- * with a minimum of 16 pages. This is totally arbitrary
+ * with a minimum of 24 pages. This is totally arbitrary
*/
i = (end_mem - PAGE_OFFSET) >> (PAGE_SHIFT+7);
- if (i < 16)
- i = 16;
+ if (i < 24)
+ i = 24;
min_free_pages = i;
free_pages_low = i + (i>>1);
free_pages_high = i + i;
diff -urN -x *~1 linux-2.0.31-clean/mm/vmscan.c linux/mm/vmscan.c
--- linux-2.0.31-clean/mm/vmscan.c Sat Dec 14 13:24:31 1996
+++ linux/mm/vmscan.c Tue Jun 24 20:15:29 1997
@@ -68,7 +68,7 @@
* have died while we slept).
*/
static inline int try_to_swap_out(struct task_struct * tsk, struct vm_area_struct* vma,
- unsigned long address, pte_t * page_table, int dma, int wait)
+ unsigned long address, pte_t * page_table, int dma, int wait, int can_do_io)
{
pte_t pte;
unsigned long entry;
@@ -100,6 +100,8 @@
if (page_map->age)
return 0;
if (pte_dirty(pte)) {
+ if(!can_do_io)
+ return 0;
if (vma->vm_ops && vma->vm_ops->swapout) {
pid_t pid = tsk->pid;
vma->vm_mm->rss--;
@@ -157,7 +159,8 @@
*/

static inline int swap_out_pmd(struct task_struct * tsk, struct vm_area_struct * vma,
- pmd_t *dir, unsigned long address, unsigned long end, int dma, int wait)
+ pmd_t *dir, unsigned long address, unsigned long end, int dma, int wait,
+ int can_do_io)
{
pte_t * pte;
unsigned long pmd_end;
@@ -179,7 +182,7 @@
do {
int result;
tsk->swap_address = address + PAGE_SIZE;
- result = try_to_swap_out(tsk, vma, address, pte, dma, wait);
+ result = try_to_swap_out(tsk, vma, address, pte, dma, wait, can_do_io);
if (result)
return result;
address += PAGE_SIZE;
@@ -189,7 +192,8 @@
}

static inline int swap_out_pgd(struct task_struct * tsk, struct vm_area_struct * vma,
- pgd_t *dir, unsigned long address, unsigned long end, int dma, int wait)
+ pgd_t *dir, unsigned long address, unsigned long end, int dma, int wait,
+ int can_do_io)
{
pmd_t * pmd;
unsigned long pgd_end;
@@ -209,7 +213,7 @@
end = pgd_end;

do {
- int result = swap_out_pmd(tsk, vma, pmd, address, end, dma, wait);
+ int result = swap_out_pmd(tsk, vma, pmd, address, end, dma, wait, can_do_io);
if (result)
return result;
address = (address + PMD_SIZE) & PMD_MASK;
@@ -219,7 +223,7 @@
}

static int swap_out_vma(struct task_struct * tsk, struct vm_area_struct * vma,
- pgd_t *pgdir, unsigned long start, int dma, int wait)
+ pgd_t *pgdir, unsigned long start, int dma, int wait, int can_do_io)
{
unsigned long end;

@@ -230,7 +234,7 @@

end = vma->vm_end;
while (start < end) {
- int result = swap_out_pgd(tsk, vma, pgdir, start, end, dma, wait);
+ int result = swap_out_pgd(tsk, vma, pgdir, start, end, dma, wait, can_do_io);
if (result)
return result;
start = (start + PGDIR_SIZE) & PGDIR_MASK;
@@ -239,7 +243,7 @@
return 0;
}

-static int swap_out_process(struct task_struct * p, int dma, int wait)
+static int swap_out_process(struct task_struct * p, int dma, int wait, int can_do_io)
{
unsigned long address;
struct vm_area_struct* vma;
@@ -260,7 +264,7 @@
address = vma->vm_start;

for (;;) {
- int result = swap_out_vma(p, vma, pgd_offset(p->mm, address), address, dma, wait);
+ int result = swap_out_vma(p, vma, pgd_offset(p->mm, address), address, dma, wait, can_do_io);
if (result)
return result;
vma = vma->vm_next;
@@ -272,7 +276,7 @@
return 0;
}

-static int swap_out(unsigned int priority, int dma, int wait)
+static int swap_out(unsigned int priority, int dma, int wait, int can_do_io)
{
static int swap_task;
int loop, counter;
@@ -311,7 +315,7 @@
}
if (!--p->swap_cnt)
swap_task++;
- switch (swap_out_process(p, dma, wait)) {
+ switch (swap_out_process(p, dma, wait, can_do_io)) {
case 0:
if (p->swap_cnt)
swap_task++;
@@ -330,37 +334,65 @@
* to be. This works out OK, because we now do proper aging on page
* contents.
*/
+
int try_to_free_page(int priority, int dma, int wait)
{
- static int state = 0;
- int i=6;
- int stop;
-
- /* we don't try as hard if we're not waiting.. */
- stop = 3;
- if (wait)
+ static int common_state = 0;
+ static int single_state = 0;
+ int state;
+ int i=6, stop = 3, can_do_io = (priority != GFP_BUFFER);
+
+ /* We don't try as hard if we're not waiting.
+ * If we are called with wait, we really need a page ... therefore we switch
+ * to the next unused field. This in addition has a better chance to get a
+ * page as fast as possible. Werner <werner@suse.de>
+ */
+ state = common_state;
+ if (wait) {
+ state = single_state;
stop = 0;
+ }
+ single_state = common_state;
+
switch (state) {
do {
case 0:
- if (shrink_mmap(i, dma))
+ barrier();
+ /* Don't worry here for the GFP_BUFFER case, shrink_mmap never
+ * tries to write dirty things out...
+ */
+ if (shrink_mmap(i, dma, can_do_io)) {
+ single_state = 1;
return 1;
- state = 1;
+ }
+ common_state = 1;
+ barrier();
case 1:
- if (shm_swap(i, dma))
+ barrier();
+ /* shm_swap must always perform some I/O if it succeeds
+ * in finding things to free up, so don't waste any time
+ * if we are trying to get some buffer heads...
+ */
+ if (can_do_io && shm_swap(i, dma)) {
+ single_state = 2;
return 1;
- state = 2;
+ }
+ common_state = 2;
+ barrier();
default:
- if (swap_out(i, dma, wait))
+ barrier();
+ if (swap_out(i, dma, wait, can_do_io)) {
+ single_state = 0;
return 1;
- state = 0;
+ }
+ common_state = 0;
+ barrier();
i--;
} while ((i - stop) >= 0);
}
return 0;
}

-
/*
* The background pageout daemon.
* Started as a kernel thread from the init process.
@@ -410,7 +442,7 @@
swapstats.wakeups++;
/* Do the background pageout: */
for (i=0; i < kswapd_ctl.maxpages; i++)
- try_to_free_page(GFP_KERNEL, 0, 0);
+ try_to_free_page(GFP_KERNEL, 0, (nr_free_pages < min_free_pages));
}
}

@@ -440,7 +472,11 @@
wake_up(&kswapd_wait);
need_resched = 1;
}
- next_swap_jiffies = jiffies + swapout_interval;
+ /* low on memory, we need to start swapping soon */
+ if(last_wakeup_low)
+ next_swap_jiffies = jiffies;
+ else
+ next_swap_jiffies = jiffies + swapout_interval;
}
timer_active |= (1<<SWAP_TIMER);
}
\
 
 \ /
  Last update: 2005-03-22 13:39    [W:0.085 / U:1.948 seconds]
©2003-2018 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site