OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rc203soc/] [sw/] [uClinux/] [mm/] [vmscan.c] - Blame information for rev 1777

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1634 jcastillo
/*
2
 *  linux/mm/vmscan.c
3
 *
4
 *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
5
 *
6
 *  Swap reorganised 29.12.95, Stephen Tweedie.
7
 *  kswapd added: 7.1.96  sct
8
 *  Version: $Id: vmscan.c,v 1.1 2005-12-20 11:47:02 jcastillo Exp $
9
 */
10
 
11
#include <linux/mm.h>
12
#include <linux/sched.h>
13
#include <linux/head.h>
14
#include <linux/kernel.h>
15
#include <linux/kernel_stat.h>
16
#include <linux/errno.h>
17
#include <linux/string.h>
18
#include <linux/stat.h>
19
#include <linux/swap.h>
20
#include <linux/fs.h>
21
#include <linux/swapctl.h>
22
#include <linux/pagemap.h>
23
#include <linux/smp_lock.h>
24
 
25
#include <asm/dma.h>
26
#include <asm/system.h> /* for cli()/sti() */
27
#include <asm/segment.h> /* for memcpy_to/fromfs */
28
#include <asm/bitops.h>
29
#include <asm/pgtable.h>
30
 
31
/*
32
 * To check memory consuming code elsewhere set this to 1
33
 */
34
/* #define MM_DEBUG */
35
 
36
/*
37
 * When are we next due for a page scan?
38
 */
39
static int next_swap_jiffies = 0;
40
 
41
/*
42
 * Was the last kswapd wakeup caused by
43
 *     nr_free_pages < free_pages_low
44
 */
45
static int last_wakeup_low = 0;
46
 
47
/*
48
 * How often do we do a pageout scan during normal conditions?
49
 * Default is four times a second.
50
 */
51
int swapout_interval = HZ / 4;
52
 
53
/*
54
 * The wait queue for waking up the pageout daemon:
55
 */
56
static struct wait_queue * kswapd_wait = NULL;
57
 
58
/*
59
 * We avoid doing a reschedule if the pageout daemon is already awake;
60
 */
61
static int kswapd_awake = 0;
62
 
63
/*
64
 * sysctl-modifiable parameters to control the aggressiveness of the
65
 * page-searching within the kswapd page recovery daemon.
66
 */
67
kswapd_control_t kswapd_ctl = {4, -1, -1, -1, -1};
68
 
69
static void init_swap_timer(void);
70
 
71
/*
72
 * The swap-out functions return 1 if they successfully
73
 * threw something out, and we got a free page. It returns
74
 * zero if it couldn't do anything, and any other value
75
 * indicates it decreased rss, but the page was shared.
76
 *
77
 * NOTE! If it sleeps, it *must* return 1 to make sure we
78
 * don't continue with the swap-out. Otherwise we may be
79
 * using a process that no longer actually exists (it might
80
 * have died while we slept).
81
 */
82
static inline int try_to_swap_out(struct task_struct * tsk, struct vm_area_struct* vma,
83
        unsigned long address, pte_t * page_table, int dma, int wait, int can_do_io)
84
{
85
        pte_t pte;
86
        unsigned long entry;
87
        unsigned long page;
88
        struct page * page_map;
89
 
90
        pte = *page_table;
91
        if (!pte_present(pte))
92
                return 0;
93
        page = pte_page(pte);
94
        if (MAP_NR(page) >= MAP_NR(high_memory))
95
                return 0;
96
 
97
        page_map = mem_map + MAP_NR(page);
98
        if (PageReserved(page_map)
99
            || PageLocked(page_map)
100
            || (dma && !PageDMA(page_map)))
101
                return 0;
102
        /* Deal with page aging.  Pages age from being unused; they
103
         * rejuvenate on being accessed.  Only swap old pages (age==0
104
         * is oldest). */
105
        if ((pte_dirty(pte) && delete_from_swap_cache(MAP_NR(page)))
106
            || pte_young(pte))  {
107
                set_pte(page_table, pte_mkold(pte));
108
                touch_page(page_map);
109
                return 0;
110
        }
111
        age_page(page_map);
112
        if (page_map->age)
113
                return 0;
114
        if (pte_dirty(pte)) {
115
                if(!can_do_io)
116
                        return 0;
117
                if (vma->vm_ops && vma->vm_ops->swapout) {
118
                        pid_t pid = tsk->pid;
119
                        vma->vm_mm->rss--;
120
                        if (vma->vm_ops->swapout(vma, address - vma->vm_start + vma->vm_offset, page_table))
121
                                kill_proc(pid, SIGBUS, 1);
122
                } else {
123
                        if (!(entry = get_swap_page()))
124
                                return 0;
125
                        vma->vm_mm->rss--;
126
                        flush_cache_page(vma, address);
127
                        set_pte(page_table, __pte(entry));
128
                        flush_tlb_page(vma, address);
129
                        tsk->nswap++;
130
                        rw_swap_page(WRITE, entry, (char *) page, wait);
131
                }
132
                free_page(page);
133
                return 1;       /* we slept: the process may not exist any more */
134
        }
135
        if ((entry = find_in_swap_cache(MAP_NR(page))))  {
136
                if (page_map->count != 1) {
137
                        set_pte(page_table, pte_mkdirty(pte));
138
                        printk("Aiee.. duplicated cached swap-cache entry\n");
139
                        return 0;
140
                }
141
                vma->vm_mm->rss--;
142
                flush_cache_page(vma, address);
143
                set_pte(page_table, __pte(entry));
144
                flush_tlb_page(vma, address);
145
                free_page(page);
146
                return 1;
147
        }
148
        vma->vm_mm->rss--;
149
        flush_cache_page(vma, address);
150
        pte_clear(page_table);
151
        flush_tlb_page(vma, address);
152
        entry = page_unuse(page);
153
        free_page(page);
154
        return entry;
155
}
156
 
157
/*
158
 * A new implementation of swap_out().  We do not swap complete processes,
159
 * but only a small number of blocks, before we continue with the next
160
 * process.  The number of blocks actually swapped is determined on the
161
 * number of page faults, that this process actually had in the last time,
162
 * so we won't swap heavily used processes all the time ...
163
 *
164
 * Note: the priority argument is a hint on much CPU to waste with the
165
 *       swap block search, not a hint, of how much blocks to swap with
166
 *       each process.
167
 *
168
 * (C) 1993 Kai Petzke, wpp@marie.physik.tu-berlin.de
169
 */
170
 
171
static inline int swap_out_pmd(struct task_struct * tsk, struct vm_area_struct * vma,
172
        pmd_t *dir, unsigned long address, unsigned long end, int dma, int wait,
173
        int can_do_io)
174
{
175
        pte_t * pte;
176
        unsigned long pmd_end;
177
 
178
        if (pmd_none(*dir))
179
                return 0;
180
        if (pmd_bad(*dir)) {
181
                printk("swap_out_pmd: bad pmd (%08lx)\n", pmd_val(*dir));
182
                pmd_clear(dir);
183
                return 0;
184
        }
185
 
186
        pte = pte_offset(dir, address);
187
 
188
        pmd_end = (address + PMD_SIZE) & PMD_MASK;
189
        if (end > pmd_end)
190
                end = pmd_end;
191
 
192
        do {
193
                int result;
194
                tsk->swap_address = address + PAGE_SIZE;
195
                result = try_to_swap_out(tsk, vma, address, pte, dma, wait,
196
                                         can_do_io);
197
                if (result)
198
                        return result;
199
                address += PAGE_SIZE;
200
                pte++;
201
        } while (address < end);
202
        return 0;
203
}
204
 
205
static inline int swap_out_pgd(struct task_struct * tsk, struct vm_area_struct * vma,
206
        pgd_t *dir, unsigned long address, unsigned long end, int dma, int wait,
207
        int can_do_io)
208
{
209
        pmd_t * pmd;
210
        unsigned long pgd_end;
211
 
212
        if (pgd_none(*dir))
213
                return 0;
214
        if (pgd_bad(*dir)) {
215
                printk("swap_out_pgd: bad pgd (%08lx)\n", pgd_val(*dir));
216
                pgd_clear(dir);
217
                return 0;
218
        }
219
 
220
        pmd = pmd_offset(dir, address);
221
 
222
        pgd_end = (address + PGDIR_SIZE) & PGDIR_MASK;
223
        if (end > pgd_end)
224
                end = pgd_end;
225
 
226
        do {
227
                int result = swap_out_pmd(tsk, vma, pmd, address, end, dma, wait,
228
                                          can_do_io);
229
                if (result)
230
                        return result;
231
                address = (address + PMD_SIZE) & PMD_MASK;
232
                pmd++;
233
        } while (address < end);
234
        return 0;
235
}
236
 
237
static int swap_out_vma(struct task_struct * tsk, struct vm_area_struct * vma,
238
        pgd_t *pgdir, unsigned long start, int dma, int wait, int can_do_io)
239
{
240
        unsigned long end;
241
 
242
        /* Don't swap out areas like shared memory which have their
243
            own separate swapping mechanism or areas which are locked down */
244
        if (vma->vm_flags & (VM_SHM | VM_LOCKED))
245
                return 0;
246
 
247
        end = vma->vm_end;
248
        while (start < end) {
249
                int result = swap_out_pgd(tsk, vma, pgdir, start, end, dma, wait,
250
                                          can_do_io);
251
                if (result)
252
                        return result;
253
                start = (start + PGDIR_SIZE) & PGDIR_MASK;
254
                pgdir++;
255
        }
256
        return 0;
257
}
258
 
259
static int swap_out_process(struct task_struct * p, int dma, int wait, int can_do_io)
260
{
261
        unsigned long address;
262
        struct vm_area_struct* vma;
263
 
264
        /*
265
         * Go through process' page directory.
266
         */
267
        address = p->swap_address;
268
 
269
        /*
270
         * Find the proper vm-area
271
         */
272
        vma = find_vma(p->mm, address);
273
        if (!vma) {
274
                p->swap_address = 0;
275
                return 0;
276
        }
277
        if (address < vma->vm_start)
278
                address = vma->vm_start;
279
 
280
        for (;;) {
281
                int result = swap_out_vma(p, vma, pgd_offset(p->mm, address), address, dma, wait,
282
                                          can_do_io);
283
                if (result)
284
                        return result;
285
                vma = vma->vm_next;
286
                if (!vma)
287
                        break;
288
                address = vma->vm_start;
289
        }
290
        p->swap_address = 0;
291
        return 0;
292
}
293
 
294
static int swap_out(unsigned int priority, int dma, int wait, int can_do_io)
295
{
296
        static int swap_task;
297
        int loop, counter, shfrv;
298
        struct task_struct *p;
299
 
300
#ifdef MM_DEBUG
301
        shfrv = 10;
302
#else
303
        /*
304
         * Trouble due ageing pages: In some situations it is possible that we cross only tasks
305
         * which are swapped out or which have only physical pages with age >= 3.
306
         * High values of swap_cnt for memory consuming tasks do aggravate such situations.
307
         *
308
         * If PAGEOUT_WEIGHT has a value of 8192 a right shift value of 10 leads to
309
         *     (8 * nr_tasks) >> priority
310
         * Together with a high number of tasks, say 100, we have counters (due priority)
311
         *     12(6) + 25(5) + 50(4) + 100(3) + 200(2) + 400(1) + 800(0)
312
         * and as total result 1587 scans of swap_out() to swap out a task page.
313
         *
314
         * Just assume 80 tasks are swapped out and the remaining tasks have a swap_cnt value >= 40
315
         * together with pages with age >= 3.  Then we need approx 20*40*2 = 1600 scans to get a
316
         * free page.
317
         * And now assume that the amount of cached pages, buffers, and ipc pages are really low.
318
         */
319
        switch (priority) {
320
                case 6: case 5: case 4:  /* be friendly */
321
                        shfrv = 10;
322
                        break;
323
                case 3: case 2: case 1:  /* more intensive */
324
                        shfrv =  9;
325
                        break;
326
                case 0: default:         /* sorry we need a page */
327
                        shfrv =  8;
328
                        break;
329
        }
330
        /*
331
         * kswapd should be more friendly to other processes.
332
         */
333
        if (kswapd_awake)
334
                shfrv = 10;
335
#endif
336
 
337
        counter = ((PAGEOUT_WEIGHT * nr_tasks) >> shfrv) >> priority;
338
        for(; counter >= 0; counter--) {
339
                /*
340
                 * Check that swap_task is suitable for swapping.  If not, look for
341
                 * the next suitable process.
342
                 */
343
                loop = 0;
344
                while(1) {
345
                        if (swap_task >= NR_TASKS) {
346
                                swap_task = 1;
347
                                if (loop)
348
                                        /* all processes are unswappable or already swapped out */
349
                                        return 0;
350
                                loop = 1;
351
                        }
352
 
353
                        p = task[swap_task];
354
                        if (p && p->swappable && p->mm->rss)
355
                                break;
356
 
357
                        swap_task++;
358
                }
359
 
360
                /*
361
                 * Determine the number of pages to swap from this process.
362
                 */
363
                if (!p->swap_cnt) {
364
                        /*
365
                         * Normalise the number of pages swapped by
366
                         * multiplying by (RSS / 1MB)
367
                         */
368
                        p->swap_cnt = AGE_CLUSTER_SIZE(p->mm->rss);
369
                }
370
                if (!--p->swap_cnt)
371
                        swap_task++;
372
                switch (swap_out_process(p, dma, wait, can_do_io)) {
373
                        case 0:
374
                                if (p->state == TASK_STOPPED)
375
                                        /* Stopped task occupy nonused ram */
376
                                        break;
377
                                if (p->swap_cnt)
378
                                        swap_task++;
379
                                break;
380
                        case 1:
381
                                return 1;
382
                        default:
383
                                break;
384
                }
385
        }
386
#ifdef MM_DEBUG
387
        if (!priority) {
388
                printk("swap_out: physical ram %6dkB, min pages   %6dkB\n",
389
                        (int)(high_memory>>10), min_free_pages<<(PAGE_SHIFT-10));
390
                printk("swap_out:   free pages %6dkB, async pages %6dkB\n",
391
                        nr_free_pages<<(PAGE_SHIFT-10), nr_async_pages<<(PAGE_SHIFT-10));
392
        }
393
#endif
394
        return 0;
395
}
396
 
397
/*
398
 * We are much more aggressive about trying to swap out than we used
399
 * to be.  This works out OK, because we now do proper aging on page
400
 * contents.
401
 */
402
int try_to_free_page(int priority, int dma, int wait)
403
{
404
        static int state = 0;
405
        int i=6;
406
        int stop, can_do_io;
407
 
408
        /* we don't try as hard if we're not waiting.. */
409
        stop = 3;
410
        can_do_io = 1;
411
        if (wait)
412
                stop = 0;
413
        if (priority == GFP_IO)
414
                can_do_io = 0;
415
        switch (state) {
416
                do {
417
                case 0:
418
                        if (shrink_mmap(i, dma, 1))
419
                                return 1;
420
                        state = 1;
421
                case 1:
422
                        if (can_do_io && shm_swap(i, dma))
423
                                return 1;
424
                        state = 2;
425
                default:
426
                        if (swap_out(i, dma, wait, can_do_io))
427
                                return 1;
428
                        state = 0;
429
                i--;
430
                } while ((i - stop) >= 0);
431
        }
432
        return 0;
433
}
434
 
435
/*
436
 * Before we start the kernel thread, print out the
437
 * kswapd initialization message (otherwise the init message
438
 * may be printed in the middle of another driver's init
439
 * message).  It looks very bad when that happens.
440
 */
441
void kswapd_setup(void)
442
{
443
       int i;
444
       char *revision="$Revision: 1.1 $", *s, *e;
445
 
446
       if ((s = strchr(revision, ':')) &&
447
           (e = strchr(s, '$')))
448
               s++, i = e - s;
449
       else
450
               s = revision, i = -1;
451
       printk ("Starting kswapd v%.*s\n", i, s);
452
}
453
 
454
/*
455
 * The background pageout daemon.
456
 * Started as a kernel thread from the init process.
457
 */
458
int kswapd(void *unused)
459
{
460
        int i, reserved_pages;
461
 
462
        current->session = 1;
463
        current->pgrp = 1;
464
        sprintf(current->comm, "kswapd");
465
        current->blocked = ~0UL;
466
 
467
        /*
468
         *      As a kernel thread we want to tamper with system buffers
469
         *      and other internals and thus be subject to the SMP locking
470
         *      rules. (On a uniprocessor box this does nothing).
471
         */
472
 
473
#ifdef __SMP__
474
        lock_kernel();
475
        syscall_count++;
476
#endif
477
 
478
        /* Give kswapd a realtime priority. */
479
        current->policy = SCHED_FIFO;
480
        current->priority = 32;  /* Fixme --- we need to standardise our
481
                                    namings for POSIX.4 realtime scheduling
482
                                    priorities.  */
483
 
484
        init_swap_timer();
485
 
486
        while (1) {
487
                /* low on memory, we need to start swapping soon */
488
                next_swap_jiffies = jiffies +
489
                        (last_wakeup_low ? swapout_interval >> 1 : swapout_interval);
490
                kswapd_awake = 0;
491
                current->signal = 0;
492
                run_task_queue(&tq_disk);
493
                interruptible_sleep_on(&kswapd_wait);
494
                kswapd_awake = 1;
495
                swapstats.wakeups++;
496
                /* Protect our reserved pages: */
497
                i = 0;
498
                reserved_pages = min_free_pages;
499
                if (min_free_pages >= 48)
500
                        reserved_pages -= (12 + (reserved_pages>>3));
501
                if (nr_free_pages <= reserved_pages)
502
                        i = (1+reserved_pages) - nr_free_pages;
503
                /* Do the background pageout: */
504
                for (i += kswapd_ctl.maxpages; i > 0; i--)
505
                        try_to_free_page(GFP_KERNEL, 0,
506
                                         (nr_free_pages <= min_free_pages));
507
        }
508
}
509
 
510
/*
511
 * The swap_tick function gets called on every clock tick.
512
 */
513
 
514
void swap_tick(void)
515
{
516
        int     want_wakeup = 0;
517
 
518
        if ((nr_free_pages + nr_async_pages) < free_pages_low) {
519
                if (last_wakeup_low)
520
                        want_wakeup = (jiffies >= next_swap_jiffies);
521
                else
522
                        last_wakeup_low = want_wakeup = 1;
523
        }
524
        else if (((nr_free_pages + nr_async_pages) < free_pages_high) &&
525
                 (jiffies >= next_swap_jiffies)) {
526
                last_wakeup_low = 0;
527
                want_wakeup = 1;
528
        }
529
 
530
        if (want_wakeup) {
531
                if (!kswapd_awake && kswapd_ctl.maxpages > 0) {
532
                        wake_up(&kswapd_wait);
533
                        need_resched = 1;
534
                }
535
        }
536
        timer_active |= (1<<SWAP_TIMER);
537
}
538
 
539
 
540
/*
541
 * Initialise the swap timer
542
 */
543
 
544
void init_swap_timer(void)
545
{
546
        timer_table[SWAP_TIMER].expires = 0;
547
        timer_table[SWAP_TIMER].fn = swap_tick;
548
        timer_active |= (1<<SWAP_TIMER);
549
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.