root/mm/vmscan.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. try_to_swap_out
  2. swap_out_pmd
  3. swap_out_pgd
  4. swap_out_vma
  5. swap_out_process
  6. swap_out
  7. try_to_free_page
  8. kswapd
  9. swap_tick
  10. init_swap_timer

   1 /*
   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.4.2.2 1996/01/20 18:22:47 linux 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/smp_lock.h>
  23 
  24 #include <asm/dma.h>
  25 #include <asm/system.h> /* for cli()/sti() */
  26 #include <asm/segment.h> /* for memcpy_to/fromfs */
  27 #include <asm/bitops.h>
  28 #include <asm/pgtable.h>
  29 
  30 /* 
  31  * When are we next due for a page scan? 
  32  */
  33 static int next_swap_jiffies = 0;
  34 
  35 /* 
  36  * How often do we do a pageout scan during normal conditions?
  37  * Default is four times a second.
  38  */
  39 int swapout_interval = HZ / 4;
  40 
  41 /* 
  42  * The wait queue for waking up the pageout daemon:
  43  */
  44 static struct wait_queue * kswapd_wait = NULL;
  45 
  46 /* 
  47  * We avoid doing a reschedule if the pageout daemon is already awake;
  48  */
  49 static int kswapd_awake = 0;
  50 
  51 /*
  52  * sysctl-modifiable parameters to control the aggressiveness of the
  53  * page-searching within the kswapd page recovery daemon.
  54  */
  55 kswapd_control_t kswapd_ctl = {4, -1, -1, -1, -1};
  56 
  57 static void init_swap_timer(void);
  58 
  59 /*
  60  * The swap-out functions return 1 if they successfully
  61  * threw something out, and we got a free page. It returns
  62  * zero if it couldn't do anything, and any other value
  63  * indicates it decreased rss, but the page was shared.
  64  *
  65  * NOTE! If it sleeps, it *must* return 1 to make sure we
  66  * don't continue with the swap-out. Otherwise we may be
  67  * using a process that no longer actually exists (it might
  68  * have died while we slept).
  69  */
  70 static inline int try_to_swap_out(struct task_struct * tsk, struct vm_area_struct* vma,
     /* [previous][next][first][last][top][bottom][index][help] */
  71         unsigned long address, pte_t * page_table, unsigned long limit, int wait)
  72 {
  73         pte_t pte;
  74         unsigned long entry;
  75         unsigned long page;
  76         struct page * page_map;
  77 
  78         pte = *page_table;
  79         if (!pte_present(pte))
  80                 return 0;
  81         page = pte_page(pte);
  82         if (MAP_NR(page) >= MAP_NR(high_memory))
  83                 return 0;
  84         if (page >= limit)
  85                 return 0;
  86 
  87         page_map = mem_map + MAP_NR(page);
  88         if (page_map->reserved || page_map->locked)
  89                 return 0;
  90         /* Deal with page aging.  Pages age from being unused; they
  91          * rejuvinate on being accessed.  Only swap old pages (age==0
  92          * is oldest). */
  93         if ((pte_dirty(pte) && delete_from_swap_cache(page)) 
  94             || pte_young(pte))  {
  95                 set_pte(page_table, pte_mkold(pte));
  96                 touch_page(page_map);
  97                 return 0;
  98         }
  99         age_page(page_map);
 100         if (page_map->age)
 101                 return 0;
 102         if (pte_dirty(pte)) {
 103                 if (vma->vm_ops && vma->vm_ops->swapout) {
 104                         pid_t pid = tsk->pid;
 105                         vma->vm_mm->rss--;
 106                         if (vma->vm_ops->swapout(vma, address - vma->vm_start + vma->vm_offset, page_table))
 107                                 kill_proc(pid, SIGBUS, 1);
 108                 } else {
 109                         if (page_map->count != 1)
 110                                 return 0;
 111                         if (!(entry = get_swap_page()))
 112                                 return 0;
 113                         vma->vm_mm->rss--;
 114                         set_pte(page_table, __pte(entry));
 115                         invalidate_page(vma, address);
 116                         tsk->nswap++;
 117                         rw_swap_page(WRITE, entry, (char *) page, wait);
 118                 }
 119                 free_page(page);
 120                 return 1;       /* we slept: the process may not exist any more */
 121         }
 122         if ((entry = find_in_swap_cache(page)))  {
 123                 if (page_map->count != 1) {
 124                         set_pte(page_table, pte_mkdirty(pte));
 125                         printk("Aiee.. duplicated cached swap-cache entry\n");
 126                         return 0;
 127                 }
 128                 vma->vm_mm->rss--;
 129                 set_pte(page_table, __pte(entry));
 130                 invalidate_page(vma, address);
 131                 free_page(page);
 132                 return 1;
 133         } 
 134         vma->vm_mm->rss--;
 135         pte_clear(page_table);
 136         invalidate_page(vma, address);
 137         entry = page_unuse(page);
 138         free_page(page);
 139         return entry;
 140 }
 141 
 142 /*
 143  * A new implementation of swap_out().  We do not swap complete processes,
 144  * but only a small number of blocks, before we continue with the next
 145  * process.  The number of blocks actually swapped is determined on the
 146  * number of page faults, that this process actually had in the last time,
 147  * so we won't swap heavily used processes all the time ...
 148  *
 149  * Note: the priority argument is a hint on much CPU to waste with the
 150  *       swap block search, not a hint, of how much blocks to swap with
 151  *       each process.
 152  *
 153  * (C) 1993 Kai Petzke, wpp@marie.physik.tu-berlin.de
 154  */
 155 
 156 static inline int swap_out_pmd(struct task_struct * tsk, struct vm_area_struct * vma,
     /* [previous][next][first][last][top][bottom][index][help] */
 157         pmd_t *dir, unsigned long address, unsigned long end, unsigned long limit, int wait)
 158 {
 159         pte_t * pte;
 160         unsigned long pmd_end;
 161 
 162         if (pmd_none(*dir))
 163                 return 0;
 164         if (pmd_bad(*dir)) {
 165                 printk("swap_out_pmd: bad pmd (%08lx)\n", pmd_val(*dir));
 166                 pmd_clear(dir);
 167                 return 0;
 168         }
 169         
 170         pte = pte_offset(dir, address);
 171         
 172         pmd_end = (address + PMD_SIZE) & PMD_MASK;
 173         if (end > pmd_end)
 174                 end = pmd_end;
 175 
 176         do {
 177                 int result;
 178                 tsk->swap_address = address + PAGE_SIZE;
 179                 result = try_to_swap_out(tsk, vma, address, pte, limit, wait);
 180                 if (result)
 181                         return result;
 182                 address += PAGE_SIZE;
 183                 pte++;
 184         } while (address < end);
 185         return 0;
 186 }
 187 
 188 static inline int swap_out_pgd(struct task_struct * tsk, struct vm_area_struct * vma,
     /* [previous][next][first][last][top][bottom][index][help] */
 189         pgd_t *dir, unsigned long address, unsigned long end, unsigned long limit, int wait)
 190 {
 191         pmd_t * pmd;
 192         unsigned long pgd_end;
 193 
 194         if (pgd_none(*dir))
 195                 return 0;
 196         if (pgd_bad(*dir)) {
 197                 printk("swap_out_pgd: bad pgd (%08lx)\n", pgd_val(*dir));
 198                 pgd_clear(dir);
 199                 return 0;
 200         }
 201 
 202         pmd = pmd_offset(dir, address);
 203 
 204         pgd_end = (address + PGDIR_SIZE) & PGDIR_MASK;  
 205         if (end > pgd_end)
 206                 end = pgd_end;
 207         
 208         do {
 209                 int result = swap_out_pmd(tsk, vma, pmd, address, end, limit, wait);
 210                 if (result)
 211                         return result;
 212                 address = (address + PMD_SIZE) & PMD_MASK;
 213                 pmd++;
 214         } while (address < end);
 215         return 0;
 216 }
 217 
 218 static int swap_out_vma(struct task_struct * tsk, struct vm_area_struct * vma,
     /* [previous][next][first][last][top][bottom][index][help] */
 219         pgd_t *pgdir, unsigned long start, unsigned long limit, int wait)
 220 {
 221         unsigned long end;
 222 
 223         /* Don't swap out areas like shared memory which have their
 224             own separate swapping mechanism or areas which are locked down */
 225         if (vma->vm_flags & (VM_SHM | VM_LOCKED))
 226                 return 0;
 227 
 228         end = vma->vm_end;
 229         while (start < end) {
 230                 int result = swap_out_pgd(tsk, vma, pgdir, start, end, limit, wait);
 231                 if (result)
 232                         return result;
 233                 start = (start + PGDIR_SIZE) & PGDIR_MASK;
 234                 pgdir++;
 235         }
 236         return 0;
 237 }
 238 
 239 static int swap_out_process(struct task_struct * p, unsigned long limit, int wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 240 {
 241         unsigned long address;
 242         struct vm_area_struct* vma;
 243 
 244         /*
 245          * Go through process' page directory.
 246          */
 247         address = p->swap_address;
 248         p->swap_address = 0;
 249 
 250         /*
 251          * Find the proper vm-area
 252          */
 253         vma = find_vma(p, address);
 254         if (!vma)
 255                 return 0;
 256         if (address < vma->vm_start)
 257                 address = vma->vm_start;
 258 
 259         for (;;) {
 260                 int result = swap_out_vma(p, vma, pgd_offset(p->mm, address), address, limit, wait);
 261                 if (result)
 262                         return result;
 263                 vma = vma->vm_next;
 264                 if (!vma)
 265                         break;
 266                 address = vma->vm_start;
 267         }
 268         p->swap_address = 0;
 269         return 0;
 270 }
 271 
 272 static int swap_out(unsigned int priority, unsigned long limit, int wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 273 {
 274         static int swap_task;
 275         int loop, counter;
 276         struct task_struct *p;
 277 
 278         counter = ((PAGEOUT_WEIGHT * nr_tasks) >> 10) >> priority;
 279         for(; counter >= 0; counter--) {
 280                 /*
 281                  * Check that swap_task is suitable for swapping.  If not, look for
 282                  * the next suitable process.
 283                  */
 284                 loop = 0;
 285                 while(1) {
 286                         if (swap_task >= NR_TASKS) {
 287                                 swap_task = 1;
 288                                 if (loop)
 289                                         /* all processes are unswappable or already swapped out */
 290                                         return 0;
 291                                 loop = 1;
 292                         }
 293 
 294                         p = task[swap_task];
 295                         if (p && p->swappable && p->mm->rss)
 296                                 break;
 297 
 298                         swap_task++;
 299                 }
 300 
 301                 /*
 302                  * Determine the number of pages to swap from this process.
 303                  */
 304                 if (!p->swap_cnt) {
 305                         /* Normalise the number of pages swapped by
 306                            multiplying by (RSS / 1MB) */
 307                         p->swap_cnt = AGE_CLUSTER_SIZE(p->mm->rss);
 308                 }
 309                 if (!--p->swap_cnt)
 310                         swap_task++;
 311                 switch (swap_out_process(p, limit, wait)) {
 312                         case 0:
 313                                 if (p->swap_cnt)
 314                                         swap_task++;
 315                                 break;
 316                         case 1:
 317                                 return 1;
 318                         default:
 319                                 break;
 320                 }
 321         }
 322         return 0;
 323 }
 324 
 325 /*
 326  * We are much more aggressive about trying to swap out than we used
 327  * to be.  This works out OK, because we now do proper aging on page
 328  * contents. 
 329  */
 330 int try_to_free_page(int priority, unsigned long limit, int wait)
     /* [previous][next][first][last][top][bottom][index][help] */
 331 {
 332         static int state = 0;
 333         int i=6;
 334 
 335         switch (state) {
 336                 do {
 337                 case 0:
 338                         if (shrink_mmap(i, limit))
 339                                 return 1;
 340                         state = 1;
 341                 case 1:
 342                         if (shm_swap(i, limit))
 343                                 return 1;
 344                         state = 2;
 345                 default:
 346                         if (swap_out(i, limit, wait))
 347                                 return 1;
 348                         state = 0;
 349                 } while (i--);
 350         }
 351         return 0;
 352 }
 353 
 354 
 355 /*
 356  * The background pageout daemon.
 357  * Started as a kernel thread from the init process.
 358  */
 359 int kswapd(void *unused)
     /* [previous][next][first][last][top][bottom][index][help] */
 360 {
 361         int i;
 362         char *revision="$Revision: 1.4.2.2 $", *s, *e;
 363         
 364         current->session = 1;
 365         current->pgrp = 1;
 366         sprintf(current->comm, "kswapd");
 367         current->blocked = ~0UL;
 368         
 369         /*
 370          *      As a kernel thread we want to tamper with system buffers
 371          *      and other internals and thus be subject to the SMP locking
 372          *      rules. (On a uniprocessor box this does nothing).
 373          */
 374          
 375 #ifdef __SMP__
 376         lock_kernel();
 377         syscall_count++;
 378 #endif
 379 
 380         /* Give kswapd a realtime priority. */
 381         current->policy = SCHED_FIFO;
 382         current->priority = 32;  /* Fixme --- we need to standardise our
 383                                     namings for POSIX.4 realtime scheduling
 384                                     priorities.  */
 385 
 386         init_swap_timer();
 387         
 388         if ((s = strchr(revision, ':')) &&
 389             (e = strchr(s, '$')))
 390                 s++, i = e - s;
 391         else
 392                 s = revision, i = -1;
 393         printk ("Started kswapd v%.*s\n", i, s);
 394 
 395         while (1) {
 396                 kswapd_awake = 0;
 397                 current->signal = 0;
 398                 interruptible_sleep_on(&kswapd_wait);
 399                 kswapd_awake = 1;
 400                 swapstats.wakeups++;
 401                 /* Do the background pageout: */
 402                 for (i=0; i < kswapd_ctl.maxpages; i++)
 403                         try_to_free_page(GFP_KERNEL, ~0UL, 0);
 404         }
 405 }
 406 
 407 /* 
 408  * The swap_tick function gets called on every clock tick.
 409  */
 410 
 411 void swap_tick(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 412 {
 413         if ((nr_free_pages + nr_async_pages) < free_pages_low ||
 414             ((nr_free_pages + nr_async_pages) < free_pages_high && 
 415              jiffies >= next_swap_jiffies)) {
 416                 if (!kswapd_awake && kswapd_ctl.maxpages > 0) {
 417                         wake_up(&kswapd_wait);
 418                         need_resched = 1;
 419                         kswapd_awake = 1;
 420                 }
 421                 next_swap_jiffies = jiffies + swapout_interval;
 422         }
 423         timer_active |= (1<<SWAP_TIMER);
 424 }
 425 
 426 
 427 /* 
 428  * Initialise the swap timer
 429  */
 430 
 431 void init_swap_timer(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 432 {
 433         timer_table[SWAP_TIMER].expires = 0;
 434         timer_table[SWAP_TIMER].fn = swap_tick;
 435         timer_active |= (1<<SWAP_TIMER);
 436 }

/* [previous][next][first][last][top][bottom][index][help] */