root/mm/memory.c

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

DEFINITIONS

This source file includes following definitions.
  1. oom
  2. free_one_pte
  3. free_one_pmd
  4. free_one_pgd
  5. clear_page_tables
  6. free_page_tables
  7. clone_page_tables
  8. copy_one_pte
  9. copy_one_pmd
  10. copy_one_pgd
  11. copy_page_tables
  12. forget_pte
  13. unmap_pte_range
  14. unmap_pmd_range
  15. unmap_page_range
  16. zeromap_pte_range
  17. zeromap_pmd_range
  18. zeromap_page_range
  19. remap_pte_range
  20. remap_pmd_range
  21. remap_page_range
  22. put_page
  23. put_dirty_page
  24. do_wp_page
  25. verify_area
  26. get_empty_page
  27. try_to_share
  28. share_page
  29. get_empty_pgtable
  30. do_swap_page
  31. do_no_page

   1 #define THREE_LEVEL
   2 /*
   3  *  linux/mm/memory.c
   4  *
   5  *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
   6  */
   7 
   8 /*
   9  * demand-loading started 01.12.91 - seems it is high on the list of
  10  * things wanted, and it should be easy to implement. - Linus
  11  */
  12 
  13 /*
  14  * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
  15  * pages started 02.12.91, seems to work. - Linus.
  16  *
  17  * Tested sharing by executing about 30 /bin/sh: under the old kernel it
  18  * would have taken more than the 6M I have free, but it worked well as
  19  * far as I could see.
  20  *
  21  * Also corrected some "invalidate()"s - I wasn't doing enough of them.
  22  */
  23 
  24 /*
  25  * Real VM (paging to/from disk) started 18.12.91. Much more work and
  26  * thought has to go into this. Oh, well..
  27  * 19.12.91  -  works, somewhat. Sometimes I get faults, don't know why.
  28  *              Found it. Everything seems to work now.
  29  * 20.12.91  -  Ok, making the swap-device changeable like the root.
  30  */
  31 
  32 /*
  33  * 05.04.94  -  Multi-page memory management added for v1.1.
  34  *              Idea by Alex Bligh (alex@cconcepts.co.uk)
  35  */
  36 
  37 #include <linux/config.h>
  38 #include <linux/signal.h>
  39 #include <linux/sched.h>
  40 #include <linux/head.h>
  41 #include <linux/kernel.h>
  42 #include <linux/errno.h>
  43 #include <linux/string.h>
  44 #include <linux/types.h>
  45 #include <linux/ptrace.h>
  46 #include <linux/mman.h>
  47 #include <linux/mm.h>
  48 
  49 #include <asm/system.h>
  50 #include <asm/segment.h>
  51 #include <asm/pgtable.h>
  52 
  53 unsigned long high_memory = 0;
  54 
  55 /*
  56  * The free_area_list arrays point to the queue heads of the free areas
  57  * of different sizes
  58  */
  59 int nr_swap_pages = 0;
  60 int nr_free_pages = 0;
  61 struct mem_list free_area_list[NR_MEM_LISTS];
  62 unsigned char * free_area_map[NR_MEM_LISTS];
  63 
  64 #define copy_page(from,to) memcpy((void *) to, (void *) from, PAGE_SIZE)
  65 
  66 #define USER_PTRS_PER_PGD (TASK_SIZE / PGDIR_SIZE)
  67 
  68 mem_map_t * mem_map = NULL;
  69 
  70 /*
  71  * oom() prints a message (so that the user knows why the process died),
  72  * and gives the process an untrappable SIGKILL.
  73  */
  74 void oom(struct task_struct * task)
     /* [previous][next][first][last][top][bottom][index][help] */
  75 {
  76         printk("\nOut of memory for %s.\n", current->comm);
  77         task->sigaction[SIGKILL-1].sa_handler = NULL;
  78         task->blocked &= ~(1<<(SIGKILL-1));
  79         send_sig(SIGKILL,task,1);
  80 }
  81 
  82 static inline void free_one_pte(pte_t * page_table)
     /* [previous][next][first][last][top][bottom][index][help] */
  83 {
  84         pte_t page = *page_table;
  85 
  86         if (pte_none(page))
  87                 return;
  88         pte_clear(page_table);
  89         if (!pte_present(page)) {
  90                 swap_free(pte_val(page));
  91                 return;
  92         }
  93         free_page(pte_page(page));
  94         return;
  95 }
  96 
  97 static inline void free_one_pmd(pmd_t * dir)
     /* [previous][next][first][last][top][bottom][index][help] */
  98 {
  99         int j;
 100         pte_t * pte;
 101 
 102         if (pmd_none(*dir))
 103                 return;
 104         if (pmd_bad(*dir)) {
 105                 printk("free_one_pmd: bad directory entry %08lx\n", pmd_val(*dir));
 106                 pmd_clear(dir);
 107                 return;
 108         }
 109         pte = pte_offset(dir, 0);
 110         pmd_clear(dir);
 111         if (pte_inuse(pte)) {
 112                 pte_free(pte);
 113                 return;
 114         }
 115         for (j = 0; j < PTRS_PER_PTE ; j++)
 116                 free_one_pte(pte+j);
 117         pte_free(pte);
 118 }
 119 
 120 static inline void free_one_pgd(pgd_t * dir)
     /* [previous][next][first][last][top][bottom][index][help] */
 121 {
 122         int j;
 123         pmd_t * pmd;
 124 
 125         if (pgd_none(*dir))
 126                 return;
 127         if (pgd_bad(*dir)) {
 128                 printk("free_one_pgd: bad directory entry %08lx\n", pgd_val(*dir));
 129                 pgd_clear(dir);
 130                 return;
 131         }
 132         pmd = pmd_offset(dir, 0);
 133         pgd_clear(dir);
 134         if (pmd_inuse(pmd)) {
 135                 pmd_free(pmd);
 136                 return;
 137         }
 138         for (j = 0; j < PTRS_PER_PMD ; j++)
 139                 free_one_pmd(pmd+j);
 140         pmd_free(pmd);
 141 }
 142         
 143 
 144 /*
 145  * This function clears all user-level page tables of a process - this
 146  * is needed by execve(), so that old pages aren't in the way. Note that
 147  * unlike 'free_page_tables()', this function still leaves a valid
 148  * page-table-tree in memory: it just removes the user pages. The two
 149  * functions are similar, but there is a fundamental difference.
 150  */
 151 void clear_page_tables(struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
 152 {
 153         int i;
 154         pgd_t * page_dir;
 155 
 156         if (!tsk)
 157                 return;
 158         if (tsk == task[0])
 159                 panic("task[0] (swapper) doesn't support exec()\n");
 160         page_dir = pgd_offset(tsk, 0);
 161         if (!page_dir || page_dir == swapper_pg_dir) {
 162                 printk("Trying to clear kernel page-directory: not good\n");
 163                 return;
 164         }
 165         if (pgd_inuse(page_dir)) {
 166                 pgd_t * new_pg;
 167 
 168                 if (!(new_pg = pgd_alloc())) {
 169                         oom(tsk);
 170                         return;
 171                 }
 172                 for (i = USER_PTRS_PER_PGD ; i < PTRS_PER_PGD ; i++)
 173                         new_pg[i] = page_dir[i];
 174                 SET_PAGE_DIR(tsk, new_pg);
 175                 pgd_free(page_dir);
 176                 return;
 177         }
 178         for (i = 0 ; i < USER_PTRS_PER_PGD ; i++)
 179                 free_one_pgd(page_dir + i);
 180         invalidate();
 181         return;
 182 }
 183 
 184 /*
 185  * This function frees up all page tables of a process when it exits.
 186  */
 187 void free_page_tables(struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
 188 {
 189         int i;
 190         pgd_t * page_dir;
 191 
 192         if (!tsk)
 193                 return;
 194         if (tsk == task[0]) {
 195                 printk("task[0] (swapper) killed: unable to recover\n");
 196                 panic("Trying to free up swapper memory space");
 197         }
 198         page_dir = pgd_offset(tsk, 0);
 199         if (!page_dir || page_dir == swapper_pg_dir) {
 200                 printk("Trying to free kernel page-directory: not good\n");
 201                 return;
 202         }
 203         SET_PAGE_DIR(tsk, swapper_pg_dir);
 204         if (pgd_inuse(page_dir)) {
 205                 pgd_free(page_dir);
 206                 return;
 207         }
 208         for (i = 0 ; i < PTRS_PER_PGD ; i++)
 209                 free_one_pgd(page_dir + i);
 210         pgd_free(page_dir);
 211         invalidate();
 212 }
 213 
 214 /*
 215  * clone_page_tables() clones the page table for a process - both
 216  * processes will have the exact same pages in memory. There are
 217  * probably races in the memory management with cloning, but we'll
 218  * see..
 219  */
 220 int clone_page_tables(struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
 221 {
 222         pgd_t * pg_dir;
 223 
 224         pg_dir = pgd_offset(current, 0);
 225         pgd_reuse(pg_dir);
 226         SET_PAGE_DIR(tsk, pg_dir);
 227         return 0;
 228 }
 229 
 230 static inline void copy_one_pte(pte_t * old_pte, pte_t * new_pte)
     /* [previous][next][first][last][top][bottom][index][help] */
 231 {
 232         pte_t pte = *old_pte;
 233 
 234         if (pte_none(pte))
 235                 return;
 236         if (!pte_present(pte)) {
 237                 swap_duplicate(pte_val(pte));
 238                 *new_pte = pte;
 239                 return;
 240         }
 241         if (pte_page(pte) > high_memory || (mem_map[MAP_NR(pte_page(pte))] & MAP_PAGE_RESERVED)) {
 242                 *new_pte = pte;
 243                 return;
 244         }
 245         if (pte_cow(pte))
 246                 pte = pte_wrprotect(pte);
 247         if (delete_from_swap_cache(pte_page(pte)))
 248                 pte = pte_mkdirty(pte);
 249         *new_pte = pte;
 250         *old_pte = pte;
 251         mem_map[MAP_NR(pte_page(pte))]++;
 252 }
 253 
 254 static inline int copy_one_pmd(pmd_t * old_pmd, pmd_t * new_pmd)
     /* [previous][next][first][last][top][bottom][index][help] */
 255 {
 256         int j;
 257         pte_t *old_pte, *new_pte;
 258 
 259         if (pmd_none(*old_pmd))
 260                 return 0;
 261         if (pmd_bad(*old_pmd)) {
 262                 printk("copy_one_pmd: bad page table: probable memory corruption\n");
 263                 pmd_clear(old_pmd);
 264                 return 0;
 265         }
 266         old_pte = pte_offset(old_pmd, 0);
 267         if (pte_inuse(old_pte)) {
 268                 pte_reuse(old_pte);
 269                 *new_pmd = *old_pmd;
 270                 return 0;
 271         }
 272         new_pte = pte_alloc(new_pmd, 0);
 273         if (!new_pte)
 274                 return -ENOMEM;
 275         for (j = 0 ; j < PTRS_PER_PTE ; j++) {
 276                 copy_one_pte(old_pte, new_pte);
 277                 old_pte++;
 278                 new_pte++;
 279         }
 280         return 0;
 281 }
 282 
 283 static inline int copy_one_pgd(pgd_t * old_pgd, pgd_t * new_pgd)
     /* [previous][next][first][last][top][bottom][index][help] */
 284 {
 285         int j;
 286         pmd_t *old_pmd, *new_pmd;
 287 
 288         if (pgd_none(*old_pgd))
 289                 return 0;
 290         if (pgd_bad(*old_pgd)) {
 291                 printk("copy_one_pgd: bad page table: probable memory corruption\n");
 292                 pgd_clear(old_pgd);
 293                 return 0;
 294         }
 295         old_pmd = pmd_offset(old_pgd, 0);
 296         if (pmd_inuse(old_pmd)) {
 297                 pmd_reuse(old_pmd);
 298                 *new_pgd = *old_pgd;
 299                 return 0;
 300         }
 301         new_pmd = pmd_alloc(new_pgd, 0);
 302         if (!new_pmd)
 303                 return -ENOMEM;
 304         for (j = 0 ; j < PTRS_PER_PMD ; j++) {
 305                 int error = copy_one_pmd(old_pmd, new_pmd);
 306                 if (error)
 307                         return error;
 308                 old_pmd++;
 309                 new_pmd++;
 310         }
 311         return 0;
 312 }
 313 
 314 /*
 315  * copy_page_tables() just copies the whole process memory range:
 316  * note the special handling of RESERVED (ie kernel) pages, which
 317  * means that they are always shared by all processes.
 318  */
 319 int copy_page_tables(struct task_struct * tsk)
     /* [previous][next][first][last][top][bottom][index][help] */
 320 {
 321         int i;
 322         pgd_t *old_pgd;
 323         pgd_t *new_pgd;
 324 
 325         new_pgd = pgd_alloc();
 326         if (!new_pgd)
 327                 return -ENOMEM;
 328         old_pgd = pgd_offset(current, 0);
 329         SET_PAGE_DIR(tsk, new_pgd);
 330         for (i = 0 ; i < PTRS_PER_PGD ; i++) {
 331                 int errno = copy_one_pgd(old_pgd, new_pgd);
 332                 if (errno) {
 333                         free_page_tables(tsk);
 334                         invalidate();
 335                         return errno;
 336                 }
 337                 old_pgd++;
 338                 new_pgd++;
 339         }
 340         invalidate();
 341         return 0;
 342 }
 343 
 344 static inline void forget_pte(pte_t page)
     /* [previous][next][first][last][top][bottom][index][help] */
 345 {
 346         if (pte_none(page))
 347                 return;
 348         if (pte_present(page)) {
 349                 free_page(pte_page(page));
 350                 if (mem_map[MAP_NR(pte_page(page))] & MAP_PAGE_RESERVED)
 351                         return;
 352                 if (current->mm->rss <= 0)
 353                         return;
 354                 current->mm->rss--;
 355                 return;
 356         }
 357         swap_free(pte_val(page));
 358 }
 359 
 360 static inline void unmap_pte_range(pmd_t * pmd, unsigned long address, unsigned long size)
     /* [previous][next][first][last][top][bottom][index][help] */
 361 {
 362         pte_t * pte;
 363         unsigned long end;
 364 
 365         if (pmd_none(*pmd))
 366                 return;
 367         if (pmd_bad(*pmd)) {
 368                 printk("unmap_pte_range: bad pmd (%08lx)\n", pmd_val(*pmd));
 369                 pmd_clear(pmd);
 370                 return;
 371         }
 372         pte = pte_offset(pmd, address);
 373         address &= ~PMD_MASK;
 374         end = address + size;
 375         if (end >= PMD_SIZE)
 376                 end = PMD_SIZE;
 377         do {
 378                 pte_t page = *pte;
 379                 pte_clear(pte);
 380                 forget_pte(page);
 381                 address += PAGE_SIZE;
 382                 pte++;
 383         } while (address < end);
 384 }
 385 
 386 static inline void unmap_pmd_range(pgd_t * dir, unsigned long address, unsigned long size)
     /* [previous][next][first][last][top][bottom][index][help] */
 387 {
 388         pmd_t * pmd;
 389         unsigned long end;
 390 
 391         if (pgd_none(*dir))
 392                 return;
 393         if (pgd_bad(*dir)) {
 394                 printk("unmap_pmd_range: bad pgd (%08lx)\n", pgd_val(*dir));
 395                 pgd_clear(dir);
 396                 return;
 397         }
 398         pmd = pmd_offset(dir, address);
 399         address &= ~PGDIR_MASK;
 400         end = address + size;
 401         if (end > PGDIR_SIZE)
 402                 end = PGDIR_SIZE;
 403         do {
 404                 unmap_pte_range(pmd, address, end - address);
 405                 address = (address + PMD_SIZE) & PMD_MASK; 
 406                 pmd++;
 407         } while (address < end);
 408 }
 409 
 410 /*
 411  * a more complete version of free_page_tables which performs with page
 412  * granularity.
 413  */
 414 int unmap_page_range(unsigned long address, unsigned long size)
     /* [previous][next][first][last][top][bottom][index][help] */
 415 {
 416         pgd_t * dir;
 417         unsigned long end = address + size;
 418 
 419         dir = pgd_offset(current, address);
 420         while (address < end) {
 421                 unmap_pmd_range(dir, address, end - address);
 422                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
 423                 dir++;
 424         }
 425         invalidate();
 426         return 0;
 427 }
 428 
 429 static inline void zeromap_pte_range(pte_t * pte, unsigned long address, unsigned long size, pte_t zero_pte)
     /* [previous][next][first][last][top][bottom][index][help] */
 430 {
 431         unsigned long end;
 432 
 433         address &= ~PMD_MASK;
 434         end = address + size;
 435         if (end > PMD_SIZE)
 436                 end = PMD_SIZE;
 437         do {
 438                 pte_t oldpage = *pte;
 439                 *pte = zero_pte;
 440                 forget_pte(oldpage);
 441                 address += PAGE_SIZE;
 442                 pte++;
 443         } while (address < end);
 444 }
 445 
 446 static inline int zeromap_pmd_range(pmd_t * pmd, unsigned long address, unsigned long size, pte_t zero_pte)
     /* [previous][next][first][last][top][bottom][index][help] */
 447 {
 448         unsigned long end;
 449 
 450         address &= ~PGDIR_MASK;
 451         end = address + size;
 452         if (end > PGDIR_SIZE)
 453                 end = PGDIR_SIZE;
 454         do {
 455                 pte_t * pte = pte_alloc(pmd, address);
 456                 if (!pte)
 457                         return -ENOMEM;
 458                 zeromap_pte_range(pte, address, end - address, zero_pte);
 459                 address = (address + PMD_SIZE) & PMD_MASK;
 460                 pmd++;
 461         } while (address < end);
 462         return 0;
 463 }
 464 
 465 int zeromap_page_range(unsigned long address, unsigned long size, pgprot_t prot)
     /* [previous][next][first][last][top][bottom][index][help] */
 466 {
 467         int error = 0;
 468         pgd_t * dir;
 469         unsigned long end = address + size;
 470         pte_t zero_pte;
 471 
 472         zero_pte = pte_wrprotect(mk_pte(ZERO_PAGE, prot));
 473         dir = pgd_offset(current, address);
 474         while (address < end) {
 475                 pmd_t *pmd = pmd_alloc(dir, address);
 476                 error = -ENOMEM;
 477                 if (!pmd)
 478                         break;
 479                 error = zeromap_pmd_range(pmd, address, end - address, zero_pte);
 480                 if (error)
 481                         break;
 482                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
 483                 dir++;
 484         }
 485         invalidate();
 486         return error;
 487 }
 488 
 489 /*
 490  * maps a range of physical memory into the requested pages. the old
 491  * mappings are removed. any references to nonexistent pages results
 492  * in null mappings (currently treated as "copy-on-access")
 493  */
 494 static inline void remap_pte_range(pte_t * pte, unsigned long address, unsigned long size,
     /* [previous][next][first][last][top][bottom][index][help] */
 495         unsigned long offset, pgprot_t prot)
 496 {
 497         unsigned long end;
 498 
 499         address &= ~PMD_MASK;
 500         end = address + size;
 501         if (end > PMD_SIZE)
 502                 end = PMD_SIZE;
 503         do {
 504                 pte_t oldpage = *pte;
 505                 pte_clear(pte);
 506                 if (offset >= high_memory || (mem_map[MAP_NR(offset)] & MAP_PAGE_RESERVED))
 507                         *pte = mk_pte(offset, prot);
 508                 else if (mem_map[MAP_NR(offset)]) {
 509                         mem_map[MAP_NR(offset)]++;
 510                         *pte = mk_pte(offset, prot);
 511                 }
 512                 forget_pte(oldpage);
 513                 address += PAGE_SIZE;
 514                 offset += PAGE_SIZE;
 515                 pte++;
 516         } while (address < end);
 517 }
 518 
 519 static inline int remap_pmd_range(pmd_t * pmd, unsigned long address, unsigned long size,
     /* [previous][next][first][last][top][bottom][index][help] */
 520         unsigned long offset, pgprot_t prot)
 521 {
 522         unsigned long end;
 523 
 524         address &= ~PGDIR_MASK;
 525         end = address + size;
 526         if (end > PGDIR_SIZE)
 527                 end = PGDIR_SIZE;
 528         offset -= address;
 529         do {
 530                 pte_t * pte = pte_alloc(pmd, address);
 531                 if (!pte)
 532                         return -ENOMEM;
 533                 remap_pte_range(pte, address, end - address, address + offset, prot);
 534                 address = (address + PMD_SIZE) & PMD_MASK;
 535                 pmd++;
 536         } while (address < end);
 537         return 0;
 538 }
 539 
 540 int remap_page_range(unsigned long from, unsigned long offset, unsigned long size, pgprot_t prot)
     /* [previous][next][first][last][top][bottom][index][help] */
 541 {
 542         int error = 0;
 543         pgd_t * dir;
 544         unsigned long end = from + size;
 545 
 546         offset -= from;
 547         dir = pgd_offset(current, from);
 548         while (from < end) {
 549                 pmd_t *pmd = pmd_alloc(dir, from);
 550                 error = -ENOMEM;
 551                 if (!pmd)
 552                         break;
 553                 error = remap_pmd_range(pmd, from, end - from, offset + from, prot);
 554                 if (error)
 555                         break;
 556                 from = (from + PGDIR_SIZE) & PGDIR_MASK;
 557                 dir++;
 558         }
 559         invalidate();
 560         return error;
 561 }
 562 
 563 /*
 564  * sanity-check function..
 565  */
 566 static void put_page(pte_t * page_table, pte_t pte)
     /* [previous][next][first][last][top][bottom][index][help] */
 567 {
 568         if (!pte_none(*page_table)) {
 569                 printk("put_page: page already exists\n");
 570                 free_page(pte_page(pte));
 571                 return;
 572         }
 573 /* no need for invalidate */
 574         *page_table = pte;
 575 }
 576 
 577 /*
 578  * This routine is used to map in a page into an address space: needed by
 579  * execve() for the initial stack and environment pages.
 580  */
 581 unsigned long put_dirty_page(struct task_struct * tsk, unsigned long page, unsigned long address)
     /* [previous][next][first][last][top][bottom][index][help] */
 582 {
 583         pgd_t * pgd;
 584         pmd_t * pmd;
 585         pte_t * pte;
 586 
 587         if (page >= high_memory)
 588                 printk("put_dirty_page: trying to put page %08lx at %08lx\n",page,address);
 589         if (mem_map[MAP_NR(page)] != 1)
 590                 printk("mem_map disagrees with %08lx at %08lx\n",page,address);
 591         pgd = pgd_offset(tsk,address);
 592         pmd = pmd_alloc(pgd, address);
 593         if (!pmd)
 594                 return 0;
 595         pte = pte_alloc(pmd, address);
 596         if (!pte)
 597                 return 0;
 598         if (!pte_none(*pte)) {
 599                 printk("put_dirty_page: page already exists\n");
 600                 pte_clear(pte);
 601                 invalidate();
 602         }
 603         *pte = pte_mkwrite(pte_mkdirty(mk_pte(page, PAGE_COPY)));
 604 /* no need for invalidate */
 605         return page;
 606 }
 607 
 608 /*
 609  * This routine handles present pages, when users try to write
 610  * to a shared page. It is done by copying the page to a new address
 611  * and decrementing the shared-page counter for the old page.
 612  *
 613  * Goto-purists beware: the only reason for goto's here is that it results
 614  * in better assembly code.. The "default" path will see no jumps at all.
 615  *
 616  * Note that this routine assumes that the protection checks have been
 617  * done by the caller (the low-level page fault routine in most cases).
 618  * Thus we can safely just mark it writable once we've done any necessary
 619  * COW.
 620  *
 621  * We also mark the page dirty at this point even though the page will
 622  * change only once the write actually happens. This avoids a few races,
 623  * and potentially makes it more efficient.
 624  */
 625 void do_wp_page(struct vm_area_struct * vma, unsigned long address,
     /* [previous][next][first][last][top][bottom][index][help] */
 626         int write_access)
 627 {
 628         pgd_t *page_dir;
 629         pmd_t *page_middle;
 630         pte_t *page_table, pte;
 631         unsigned long old_page, new_page;
 632 
 633         new_page = __get_free_page(GFP_KERNEL);
 634         page_dir = pgd_offset(vma->vm_task,address);
 635         if (pgd_none(*page_dir))
 636                 goto end_wp_page;
 637         if (pgd_bad(*page_dir))
 638                 goto bad_wp_pagedir;
 639         page_middle = pmd_offset(page_dir, address);
 640         if (pmd_none(*page_middle))
 641                 goto end_wp_page;
 642         if (pmd_bad(*page_middle))
 643                 goto bad_wp_pagemiddle;
 644         page_table = pte_offset(page_middle, address);
 645         pte = *page_table;
 646         if (!pte_present(pte))
 647                 goto end_wp_page;
 648         if (pte_write(pte))
 649                 goto end_wp_page;
 650         old_page = pte_page(pte);
 651         if (old_page >= high_memory)
 652                 goto bad_wp_page;
 653         vma->vm_task->mm->min_flt++;
 654         /*
 655          * Do we need to copy?
 656          */
 657         if (mem_map[MAP_NR(old_page)] != 1) {
 658                 if (new_page) {
 659                         if (mem_map[MAP_NR(old_page)] & MAP_PAGE_RESERVED)
 660                                 ++vma->vm_task->mm->rss;
 661                         copy_page(old_page,new_page);
 662                         *page_table = pte_mkwrite(pte_mkdirty(mk_pte(new_page, vma->vm_page_prot)));
 663                         free_page(old_page);
 664                         invalidate();
 665                         return;
 666                 }
 667                 free_page(old_page);
 668                 oom(vma->vm_task);
 669                 *page_table = BAD_PAGE;
 670                 invalidate();
 671                 return;
 672         }
 673         *page_table = pte_mkdirty(pte_mkwrite(pte));
 674         invalidate();
 675         if (new_page)
 676                 free_page(new_page);
 677         return;
 678 bad_wp_page:
 679         printk("do_wp_page: bogus page at address %08lx (%08lx)\n",address,old_page);
 680         send_sig(SIGKILL, vma->vm_task, 1);
 681         goto end_wp_page;
 682 bad_wp_pagemiddle:
 683         printk("do_wp_page: bogus page-middle at address %08lx (%08lx)\n", address, pmd_val(*page_middle));
 684         send_sig(SIGKILL, vma->vm_task, 1);
 685         goto end_wp_page;
 686 bad_wp_pagedir:
 687         printk("do_wp_page: bogus page-dir entry at address %08lx (%08lx)\n", address, pgd_val(*page_dir));
 688         send_sig(SIGKILL, vma->vm_task, 1);
 689 end_wp_page:
 690         if (new_page)
 691                 free_page(new_page);
 692         return;
 693 }
 694 
 695 /*
 696  * Ugly, ugly, but the goto's result in better assembly..
 697  */
 698 int verify_area(int type, const void * addr, unsigned long size)
     /* [previous][next][first][last][top][bottom][index][help] */
 699 {
 700         struct vm_area_struct * vma;
 701         unsigned long start = (unsigned long) addr;
 702 
 703         /* If the current user space is mapped to kernel space (for the
 704          * case where we use a fake user buffer with get_fs/set_fs()) we
 705          * don't expect to find the address in the user vm map.
 706          */
 707         if (get_fs() == get_ds())
 708                 return 0;
 709 
 710         vma = find_vma(current, start);
 711         if (!vma)
 712                 goto bad_area;
 713         if (vma->vm_start <= start)
 714                 goto good_area;
 715         if (!(vma->vm_flags & VM_GROWSDOWN))
 716                 goto bad_area;
 717         if (vma->vm_end - start > current->rlim[RLIMIT_STACK].rlim_cur)
 718                 goto bad_area;
 719 
 720 good_area:
 721         if (type == VERIFY_WRITE)
 722                 goto check_write;
 723         for (;;) {
 724                 struct vm_area_struct * next;
 725                 if (!(vma->vm_flags & VM_READ))
 726                         goto bad_area;
 727                 if (vma->vm_end - start >= size)
 728                         return 0;
 729                 next = vma->vm_next;
 730                 if (!next || vma->vm_end != next->vm_start)
 731                         goto bad_area;
 732                 vma = next;
 733         }
 734 
 735 check_write:
 736         if (!(vma->vm_flags & VM_WRITE))
 737                 goto bad_area;
 738         if (!wp_works_ok)
 739                 goto check_wp_fault_by_hand;
 740         for (;;) {
 741                 if (vma->vm_end - start >= size)
 742                         break;
 743                 if (!vma->vm_next || vma->vm_end != vma->vm_next->vm_start)
 744                         goto bad_area;
 745                 vma = vma->vm_next;
 746                 if (!(vma->vm_flags & VM_WRITE))
 747                         goto bad_area;
 748         }
 749         return 0;
 750 
 751 check_wp_fault_by_hand:
 752         size--;
 753         size += start & ~PAGE_MASK;
 754         size >>= PAGE_SHIFT;
 755         start &= PAGE_MASK;
 756 
 757         for (;;) {
 758                 do_wp_page(vma, start, 1);
 759                 if (!size)
 760                         break;
 761                 size--;
 762                 start += PAGE_SIZE;
 763                 if (start < vma->vm_end)
 764                         continue;
 765                 vma = vma->vm_next;
 766                 if (!vma || vma->vm_start != start)
 767                         goto bad_area;
 768                 if (!(vma->vm_flags & VM_WRITE))
 769                         goto bad_area;;
 770         }
 771         return 0;
 772 
 773 bad_area:
 774         return -EFAULT;
 775 }
 776 
 777 static inline void get_empty_page(struct vm_area_struct * vma, pte_t * page_table)
     /* [previous][next][first][last][top][bottom][index][help] */
 778 {
 779         unsigned long tmp;
 780 
 781         if (!(tmp = get_free_page(GFP_KERNEL))) {
 782                 oom(vma->vm_task);
 783                 put_page(page_table, BAD_PAGE);
 784                 return;
 785         }
 786         put_page(page_table, pte_mkwrite(mk_pte(tmp, vma->vm_page_prot)));
 787 }
 788 
 789 /*
 790  * try_to_share() checks the page at address "address" in the task "p",
 791  * to see if it exists, and if it is clean. If so, share it with the current
 792  * task.
 793  *
 794  * NOTE! This assumes we have checked that p != current, and that they
 795  * share the same inode and can generally otherwise be shared.
 796  */
 797 static int try_to_share(unsigned long to_address, struct vm_area_struct * to_area,
     /* [previous][next][first][last][top][bottom][index][help] */
 798         unsigned long from_address, struct vm_area_struct * from_area,
 799         unsigned long newpage)
 800 {
 801         pgd_t * from_dir, * to_dir;
 802         pmd_t * from_middle, * to_middle;
 803         pte_t * from_table, * to_table;
 804         pte_t from, to;
 805 
 806         from_dir = pgd_offset(from_area->vm_task,from_address);
 807 /* is there a page-directory at from? */
 808         if (pgd_none(*from_dir))
 809                 return 0;
 810         if (pgd_bad(*from_dir)) {
 811                 printk("try_to_share: bad page directory %08lx\n", pgd_val(*from_dir));
 812                 return 0;
 813         }
 814         from_middle = pmd_offset(from_dir, from_address);
 815 /* is there a mid-directory at from? */
 816         if (pmd_none(*from_middle))
 817                 return 0;
 818         if (pmd_bad(*from_middle)) {
 819                 printk("try_to_share: bad mid directory %08lx\n", pmd_val(*from_middle));
 820                 return 0;
 821         }
 822         from_table = pte_offset(from_middle, from_address);
 823         from = *from_table;
 824 /* is the page present? */
 825         if (!pte_present(from))
 826                 return 0;
 827 /* if it is dirty it must be from a shared mapping to be shared */
 828         if (pte_dirty(from)) {
 829                 if (!(from_area->vm_flags & VM_SHARED))
 830                         return 0;
 831                 if (pte_write(from)) {
 832                         printk("nonwritable, but dirty, shared page\n");
 833                         return 0;
 834                 }
 835         }
 836 /* is the page reasonable at all? */
 837         if (pte_page(from) >= high_memory)
 838                 return 0;
 839         if (mem_map[MAP_NR(pte_page(from))] & MAP_PAGE_RESERVED)
 840                 return 0;
 841 /* is the destination ok? */
 842         to_dir = pgd_offset(to_area->vm_task,to_address);
 843 /* is there a page-directory at to? */
 844         if (pgd_none(*to_dir))
 845                 return 0;
 846         if (pgd_bad(*to_dir)) {
 847                 printk("try_to_share: bad page directory %08lx\n", pgd_val(*to_dir));
 848                 return 0;
 849         }
 850         to_middle = pmd_offset(to_dir, to_address);
 851 /* is there a mid-directory at to? */
 852         if (pmd_none(*to_middle))
 853                 return 0;
 854         if (pmd_bad(*to_middle)) {
 855                 printk("try_to_share: bad mid directory %08lx\n", pmd_val(*to_middle));
 856                 return 0;
 857         }
 858         to_table = pte_offset(to_middle, to_address);
 859         to = *to_table;
 860         if (!pte_none(to))
 861                 return 0;
 862 /* do we copy? */
 863         if (newpage) {
 864                 /* if it's in the swap cache, it's dirty by implication */
 865                 /* so we can't use it if it's not from a shared mapping */
 866                 if (in_swap_cache(pte_page(from))) {
 867                         if (!(from_area->vm_flags & VM_SHARED))
 868                                 return 0;
 869                         if (!pte_write(from)) {
 870                                 printk("nonwritable, but dirty, shared page\n");
 871                                 return 0;
 872                         }
 873                 }
 874                 copy_page(pte_page(from), newpage);
 875                 *to_table = mk_pte(newpage, to_area->vm_page_prot);
 876                 return 1;
 877         }
 878 /*
 879  * do a final swap-cache test before sharing them: if it's in the swap
 880  * cache, we have to remove it now, as we get two pointers to the same
 881  * physical page and the cache can't handle it. Mark the original dirty.
 882  *
 883  * NOTE! Even if "from" is dirty, "to" will be clean: if we get here
 884  * with a dirty "from", the from-mapping is a shared map, so we can trust
 885  * the page contents to be up-to-date
 886  */
 887         if (in_swap_cache(pte_page(from))) {
 888                 if (!(from_area->vm_flags & VM_SHARED))
 889                         return 0;
 890                 *from_table = pte_mkdirty(from);
 891                 delete_from_swap_cache(pte_page(from));
 892         }
 893         mem_map[MAP_NR(pte_page(from))]++;
 894         *to_table = mk_pte(pte_page(from), to_area->vm_page_prot);
 895 /* Check if we need to do anything at all to the 'from' field */
 896         if (!pte_write(from))
 897                 return 1;
 898         if (from_area->vm_flags & VM_SHARED)
 899                 return 1;
 900 /* ok, need to mark it read-only, so invalidate any possible old TB entry */
 901         *from_table = pte_wrprotect(from);
 902         invalidate();
 903         return 1;
 904 }
 905 
 906 /*
 907  * share_page() tries to find a process that could share a page with
 908  * the current one.
 909  *
 910  * We first check if it is at all feasible by checking inode->i_count.
 911  * It should be >1 if there are other tasks sharing this inode.
 912  */
 913 static int share_page(struct vm_area_struct * area, unsigned long address,
     /* [previous][next][first][last][top][bottom][index][help] */
 914         int write_access, unsigned long newpage)
 915 {
 916         struct inode * inode;
 917         unsigned long offset;
 918         unsigned long from_address;
 919         unsigned long give_page;
 920         struct vm_area_struct * mpnt;
 921 
 922         if (!area || !(inode = area->vm_inode) || inode->i_count < 2)
 923                 return 0;
 924         /* do we need to copy or can we just share? */
 925         give_page = 0;
 926         if (write_access && !(area->vm_flags & VM_SHARED)) {
 927                 if (!newpage)
 928                         return 0;
 929                 give_page = newpage;
 930         }
 931         offset = address - area->vm_start + area->vm_offset;
 932         /* See if there is something in the VM we can share pages with. */
 933         /* Traverse the entire circular i_mmap list, except `area' itself. */
 934         for (mpnt = area->vm_next_share; mpnt != area; mpnt = mpnt->vm_next_share) {
 935                 /* must be same inode */
 936                 if (mpnt->vm_inode != inode) {
 937                         printk("Aiee! Corrupt vm_area_struct i_mmap ring\n");
 938                         break;  
 939                 }
 940                 /* offsets must be mutually page-aligned */
 941                 if ((mpnt->vm_offset ^ area->vm_offset) & ~PAGE_MASK)
 942                         continue;
 943                 /* the other area must actually cover the wanted page.. */
 944                 from_address = offset + mpnt->vm_start - mpnt->vm_offset;
 945                 if (from_address < mpnt->vm_start || from_address >= mpnt->vm_end)
 946                         continue;
 947                 /* .. NOW we can actually try to use the same physical page */
 948                 if (!try_to_share(address, area, from_address, mpnt, give_page))
 949                         continue;
 950                 /* free newpage if we never used it.. */
 951                 if (give_page || !newpage)
 952                         return 1;
 953                 free_page(newpage);
 954                 return 1;
 955         }
 956         return 0;
 957 }
 958 
 959 /*
 960  * fill in an empty page-table if none exists.
 961  */
 962 static inline pte_t * get_empty_pgtable(struct task_struct * tsk,unsigned long address)
     /* [previous][next][first][last][top][bottom][index][help] */
 963 {
 964         pgd_t *pgd;
 965         pmd_t *pmd;
 966         pte_t *pte;
 967 
 968         pgd = pgd_offset(tsk, address);
 969         pmd = pmd_alloc(pgd, address);
 970         if (!pmd)
 971                 return NULL;
 972         pte = pte_alloc(pmd, address);
 973         return pte;
 974 }
 975 
 976 static inline void do_swap_page(struct vm_area_struct * vma, unsigned long address,
     /* [previous][next][first][last][top][bottom][index][help] */
 977         pte_t * page_table, pte_t entry, int write_access)
 978 {
 979         pte_t page;
 980 
 981         if (!vma->vm_ops || !vma->vm_ops->swapin) {
 982                 swap_in(vma, page_table, pte_val(entry), write_access);
 983                 return;
 984         }
 985         page = vma->vm_ops->swapin(vma, address - vma->vm_start + vma->vm_offset, pte_val(entry));
 986         if (pte_val(*page_table) != pte_val(entry)) {
 987                 free_page(pte_page(page));
 988                 return;
 989         }
 990         if (mem_map[MAP_NR(pte_page(page))] > 1 && !(vma->vm_flags & VM_SHARED))
 991                 page = pte_wrprotect(page);
 992         ++vma->vm_task->mm->rss;
 993         ++vma->vm_task->mm->maj_flt;
 994         *page_table = page;
 995         return;
 996 }
 997 
 998 /*
 999  * do_no_page() tries to create a new page mapping. It aggressively
1000  * tries to share with existing pages, but makes a separate copy if
1001  * the "write_access" parameter is true in order to avoid the next
1002  * page fault.
1003  */
1004 void do_no_page(struct vm_area_struct * vma, unsigned long address,
     /* [previous][next][first][last][top][bottom][index][help] */
1005         int write_access)
1006 {
1007         pte_t * page_table;
1008         pte_t entry;
1009         unsigned long page;
1010 
1011         page_table = get_empty_pgtable(vma->vm_task,address);
1012         if (!page_table)
1013                 return;
1014         entry = *page_table;
1015         if (pte_present(entry))
1016                 return;
1017         if (!pte_none(entry)) {
1018                 do_swap_page(vma, address, page_table, entry, write_access);
1019                 return;
1020         }
1021         address &= PAGE_MASK;
1022         if (!vma->vm_ops || !vma->vm_ops->nopage) {
1023                 ++vma->vm_task->mm->rss;
1024                 ++vma->vm_task->mm->min_flt;
1025                 get_empty_page(vma, page_table);
1026                 return;
1027         }
1028         page = get_free_page(GFP_KERNEL);
1029         if (share_page(vma, address, write_access, page)) {
1030                 ++vma->vm_task->mm->min_flt;
1031                 ++vma->vm_task->mm->rss;
1032                 return;
1033         }
1034         if (!page) {
1035                 oom(current);
1036                 put_page(page_table, BAD_PAGE);
1037                 return;
1038         }
1039         ++vma->vm_task->mm->maj_flt;
1040         ++vma->vm_task->mm->rss;
1041         /*
1042          * The fourth argument is "no_share", which tells the low-level code
1043          * to copy, not share the page even if sharing is possible.  It's
1044          * essentially an early COW detection 
1045          */
1046         page = vma->vm_ops->nopage(vma, address, page,
1047                 write_access && !(vma->vm_flags & VM_SHARED));
1048         if (share_page(vma, address, write_access, 0)) {
1049                 free_page(page);
1050                 return;
1051         }
1052         /*
1053          * This silly early PAGE_DIRTY setting removes a race
1054          * due to the bad i386 page protection. But it's valid
1055          * for other architectures too.
1056          *
1057          * Note that if write_access is true, we either now have
1058          * a exclusive copy of the page, or this is a shared mapping,
1059          * so we can make it writable and dirty to avoid having to
1060          * handle that later.
1061          */
1062         entry = mk_pte(page, vma->vm_page_prot);
1063         if (write_access) {
1064                 entry = pte_mkwrite(pte_mkdirty(entry));
1065         } else if (mem_map[MAP_NR(page)] > 1 && !(vma->vm_flags & VM_SHARED))
1066                 entry = pte_wrprotect(entry);
1067         put_page(page_table, entry);
1068 }

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