This should hopefully fix the warnings reported.
[linux-2.6/linux-mips.git] / mm / memory.c
blobb4bf6ed369cb96c521dd35f2ab7f885c71c20918
1 /*
2 * linux/mm/memory.c
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 */
7 /*
8 * demand-loading started 01.12.91 - seems it is high on the list of
9 * things wanted, and it should be easy to implement. - Linus
13 * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
14 * pages started 02.12.91, seems to work. - Linus.
16 * Tested sharing by executing about 30 /bin/sh: under the old kernel it
17 * would have taken more than the 6M I have free, but it worked well as
18 * far as I could see.
20 * Also corrected some "invalidate()"s - I wasn't doing enough of them.
24 * Real VM (paging to/from disk) started 18.12.91. Much more work and
25 * thought has to go into this. Oh, well..
26 * 19.12.91 - works, somewhat. Sometimes I get faults, don't know why.
27 * Found it. Everything seems to work now.
28 * 20.12.91 - Ok, making the swap-device changeable like the root.
32 * 05.04.94 - Multi-page memory management added for v1.1.
33 * Idea by Alex Bligh (alex@cconcepts.co.uk)
35 * 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG
36 * (Gerhard.Wichert@pdb.siemens.de)
39 #include <linux/mm.h>
40 #include <linux/mman.h>
41 #include <linux/swap.h>
42 #include <linux/smp_lock.h>
43 #include <linux/swapctl.h>
44 #include <linux/iobuf.h>
45 #include <asm/uaccess.h>
46 #include <asm/pgalloc.h>
47 #include <linux/highmem.h>
48 #include <linux/pagemap.h>
51 unsigned long max_mapnr = 0;
52 unsigned long num_physpages = 0;
53 void * high_memory = NULL;
54 struct page *highmem_start_page;
57 * We special-case the C-O-W ZERO_PAGE, because it's such
58 * a common occurrence (no need to read the page to know
59 * that it's zero - better for the cache and memory subsystem).
61 static inline void copy_cow_page(struct page * from, struct page * to, unsigned long address)
63 if (from == ZERO_PAGE(address)) {
64 clear_highpage(to);
65 return;
67 copy_highpage(to, from);
70 mem_map_t * mem_map = NULL;
73 * oom() prints a message (so that the user knows why the process died),
74 * and gives the process an untrappable SIGKILL.
76 void oom(struct task_struct * task)
78 printk("\nOut of memory for %s.\n", task->comm);
79 force_sig(SIGKILL, task);
83 * Note: this doesn't free the actual pages themselves. That
84 * has been handled earlier when unmapping all the memory regions.
86 static inline void free_one_pmd(pmd_t * dir)
88 pte_t * pte;
90 if (pmd_none(*dir))
91 return;
92 if (pmd_bad(*dir)) {
93 pmd_ERROR(*dir);
94 pmd_clear(dir);
95 return;
97 pte = pte_offset(dir, 0);
98 pmd_clear(dir);
99 pte_free(pte);
102 static inline void free_one_pgd(pgd_t * dir)
104 int j;
105 pmd_t * pmd;
107 if (pgd_none(*dir))
108 return;
109 if (pgd_bad(*dir)) {
110 pgd_ERROR(*dir);
111 pgd_clear(dir);
112 return;
114 pmd = pmd_offset(dir, 0);
115 pgd_clear(dir);
116 for (j = 0; j < PTRS_PER_PMD ; j++)
117 free_one_pmd(pmd+j);
118 pmd_free(pmd);
121 /* Low and high watermarks for page table cache.
122 The system should try to have pgt_water[0] <= cache elements <= pgt_water[1]
124 int pgt_cache_water[2] = { 25, 50 };
126 /* Returns the number of pages freed */
127 int check_pgt_cache(void)
129 return do_check_pgt_cache(pgt_cache_water[0], pgt_cache_water[1]);
134 * This function clears all user-level page tables of a process - this
135 * is needed by execve(), so that old pages aren't in the way.
137 void clear_page_tables(struct mm_struct *mm, unsigned long first, int nr)
139 pgd_t * page_dir = mm->pgd;
141 page_dir += first;
142 do {
143 free_one_pgd(page_dir);
144 page_dir++;
145 } while (--nr);
147 /* keep the page table cache within bounds */
148 check_pgt_cache();
151 #define PTE_TABLE_MASK ((PTRS_PER_PTE-1) * sizeof(pte_t))
152 #define PMD_TABLE_MASK ((PTRS_PER_PMD-1) * sizeof(pmd_t))
155 * copy one vm_area from one task to the other. Assumes the page tables
156 * already present in the new task to be cleared in the whole range
157 * covered by this vma.
159 * 08Jan98 Merged into one routine from several inline routines to reduce
160 * variable count and make things faster. -jj
162 int copy_page_range(struct mm_struct *dst, struct mm_struct *src,
163 struct vm_area_struct *vma)
165 pgd_t * src_pgd, * dst_pgd;
166 unsigned long address = vma->vm_start;
167 unsigned long end = vma->vm_end;
168 unsigned long cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
170 src_pgd = pgd_offset(src, address)-1;
171 dst_pgd = pgd_offset(dst, address)-1;
173 for (;;) {
174 pmd_t * src_pmd, * dst_pmd;
176 src_pgd++; dst_pgd++;
178 /* copy_pmd_range */
180 if (pgd_none(*src_pgd))
181 goto skip_copy_pmd_range;
182 if (pgd_bad(*src_pgd)) {
183 pgd_ERROR(*src_pgd);
184 pgd_clear(src_pgd);
185 skip_copy_pmd_range: address = (address + PGDIR_SIZE) & PGDIR_MASK;
186 if (!address || (address >= end))
187 goto out;
188 continue;
190 if (pgd_none(*dst_pgd)) {
191 if (!pmd_alloc(dst_pgd, 0))
192 goto nomem;
195 src_pmd = pmd_offset(src_pgd, address);
196 dst_pmd = pmd_offset(dst_pgd, address);
198 do {
199 pte_t * src_pte, * dst_pte;
201 /* copy_pte_range */
203 if (pmd_none(*src_pmd))
204 goto skip_copy_pte_range;
205 if (pmd_bad(*src_pmd)) {
206 pmd_ERROR(*src_pmd);
207 pmd_clear(src_pmd);
208 skip_copy_pte_range: address = (address + PMD_SIZE) & PMD_MASK;
209 if (address >= end)
210 goto out;
211 goto cont_copy_pmd_range;
213 if (pmd_none(*dst_pmd)) {
214 if (!pte_alloc(dst_pmd, 0))
215 goto nomem;
218 src_pte = pte_offset(src_pmd, address);
219 dst_pte = pte_offset(dst_pmd, address);
221 do {
222 pte_t pte = *src_pte;
223 unsigned long page_nr;
225 /* copy_one_pte */
227 if (pte_none(pte))
228 goto cont_copy_pte_range;
229 if (!pte_present(pte)) {
230 swap_duplicate(pte_to_swp_entry(pte));
231 set_pte(dst_pte, pte);
232 goto cont_copy_pte_range;
234 page_nr = pte_pagenr(pte);
235 if (page_nr >= max_mapnr ||
236 PageReserved(mem_map+page_nr)) {
237 set_pte(dst_pte, pte);
238 goto cont_copy_pte_range;
240 /* If it's a COW mapping, write protect it both in the parent and the child */
241 if (cow) {
242 pte = pte_wrprotect(pte);
243 set_pte(src_pte, pte);
245 /* If it's a shared mapping, mark it clean in the child */
246 if (vma->vm_flags & VM_SHARED)
247 pte = pte_mkclean(pte);
248 set_pte(dst_pte, pte_mkold(pte));
249 get_page(mem_map + page_nr);
251 cont_copy_pte_range: address += PAGE_SIZE;
252 if (address >= end)
253 goto out;
254 src_pte++;
255 dst_pte++;
256 } while ((unsigned long)src_pte & PTE_TABLE_MASK);
258 cont_copy_pmd_range: src_pmd++;
259 dst_pmd++;
260 } while ((unsigned long)src_pmd & PMD_TABLE_MASK);
262 out:
263 return 0;
265 nomem:
266 return -ENOMEM;
270 * Return indicates whether a page was freed so caller can adjust rss
272 static inline int free_pte(pte_t page)
274 if (pte_present(page)) {
275 unsigned long nr = pte_pagenr(page);
276 if (nr >= max_mapnr || PageReserved(mem_map+nr))
277 return 0;
279 * free_page() used to be able to clear swap cache
280 * entries. We may now have to do it manually.
282 free_page_and_swap_cache(mem_map+nr);
283 return 1;
285 swap_free(pte_to_swp_entry(page));
286 return 0;
289 static inline void forget_pte(pte_t page)
291 if (!pte_none(page)) {
292 printk("forget_pte: old mapping existed!\n");
293 free_pte(page);
297 static inline int zap_pte_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address, unsigned long size)
299 pte_t * pte;
300 int freed;
302 if (pmd_none(*pmd))
303 return 0;
304 if (pmd_bad(*pmd)) {
305 pmd_ERROR(*pmd);
306 pmd_clear(pmd);
307 return 0;
309 pte = pte_offset(pmd, address);
310 address &= ~PMD_MASK;
311 if (address + size > PMD_SIZE)
312 size = PMD_SIZE - address;
313 size >>= PAGE_SHIFT;
314 freed = 0;
315 for (;;) {
316 pte_t page;
317 if (!size)
318 break;
319 page = *pte;
320 pte++;
321 size--;
322 pte_clear(pte-1);
323 if (pte_none(page))
324 continue;
325 freed += free_pte(page);
327 return freed;
330 static inline int zap_pmd_range(struct mm_struct *mm, pgd_t * dir, unsigned long address, unsigned long size)
332 pmd_t * pmd;
333 unsigned long end;
334 int freed;
336 if (pgd_none(*dir))
337 return 0;
338 if (pgd_bad(*dir)) {
339 pgd_ERROR(*dir);
340 pgd_clear(dir);
341 return 0;
343 pmd = pmd_offset(dir, address);
344 address &= ~PGDIR_MASK;
345 end = address + size;
346 if (end > PGDIR_SIZE)
347 end = PGDIR_SIZE;
348 freed = 0;
349 do {
350 freed += zap_pte_range(mm, pmd, address, end - address);
351 address = (address + PMD_SIZE) & PMD_MASK;
352 pmd++;
353 } while (address < end);
354 return freed;
358 * remove user pages in a given range.
360 void zap_page_range(struct mm_struct *mm, unsigned long address, unsigned long size)
362 pgd_t * dir;
363 unsigned long end = address + size;
364 int freed = 0;
366 dir = pgd_offset(mm, address);
369 * This is a long-lived spinlock. That's fine.
370 * There's no contention, because the page table
371 * lock only protects against kswapd anyway, and
372 * even if kswapd happened to be looking at this
373 * process we _want_ it to get stuck.
375 if (address >= end)
376 BUG();
377 spin_lock(&mm->page_table_lock);
378 do {
379 freed += zap_pmd_range(mm, dir, address, end - address);
380 address = (address + PGDIR_SIZE) & PGDIR_MASK;
381 dir++;
382 } while (address && (address < end));
383 spin_unlock(&mm->page_table_lock);
385 * Update rss for the mm_struct (not necessarily current->mm)
387 if (mm->rss > 0) {
388 mm->rss -= freed;
389 if (mm->rss < 0)
390 mm->rss = 0;
396 * Do a quick page-table lookup for a single page.
398 static struct page * follow_page(unsigned long address)
400 pgd_t *pgd;
401 pmd_t *pmd;
403 pgd = pgd_offset(current->mm, address);
404 pmd = pmd_offset(pgd, address);
405 if (pmd) {
406 pte_t * pte = pte_offset(pmd, address);
407 if (pte && pte_present(*pte))
408 return pte_page(*pte);
411 printk(KERN_ERR "Missing page in follow_page\n");
412 return NULL;
416 * Given a physical address, is there a useful struct page pointing to it?
419 struct page * get_page_map(struct page *page, unsigned long vaddr)
421 if (MAP_NR(page) >= max_mapnr)
422 return 0;
423 if (page == ZERO_PAGE(vaddr))
424 return 0;
425 if (PageReserved(page))
426 return 0;
427 return page;
431 * Force in an entire range of pages from the current process's user VA,
432 * and pin and lock the pages for IO.
435 #define dprintk(x...)
436 int map_user_kiobuf(int rw, struct kiobuf *iobuf, unsigned long va, size_t len)
438 unsigned long ptr, end;
439 int err;
440 struct mm_struct * mm;
441 struct vm_area_struct * vma = 0;
442 struct page * map;
443 int doublepage = 0;
444 int repeat = 0;
445 int i;
447 /* Make sure the iobuf is not already mapped somewhere. */
448 if (iobuf->nr_pages)
449 return -EINVAL;
451 mm = current->mm;
452 dprintk ("map_user_kiobuf: begin\n");
454 ptr = va & PAGE_MASK;
455 end = (va + len + PAGE_SIZE - 1) & PAGE_MASK;
456 err = expand_kiobuf(iobuf, (end - ptr) >> PAGE_SHIFT);
457 if (err)
458 return err;
460 repeat:
461 down(&mm->mmap_sem);
463 err = -EFAULT;
464 iobuf->locked = 1;
465 iobuf->offset = va & ~PAGE_MASK;
466 iobuf->length = len;
468 i = 0;
471 * First of all, try to fault in all of the necessary pages
473 while (ptr < end) {
474 if (!vma || ptr >= vma->vm_end) {
475 vma = find_vma(current->mm, ptr);
476 if (!vma)
477 goto out_unlock;
479 if (handle_mm_fault(current, vma, ptr, (rw==READ)) <= 0)
480 goto out_unlock;
481 spin_lock(&mm->page_table_lock);
482 map = follow_page(ptr);
483 if (!map) {
484 dprintk (KERN_ERR "Missing page in map_user_kiobuf\n");
485 goto retry;
487 map = get_page_map(map, ptr);
488 if (map) {
489 if (TryLockPage(map)) {
490 goto retry;
492 atomic_inc(&map->count);
494 spin_unlock(&mm->page_table_lock);
495 iobuf->maplist[i] = map;
496 iobuf->nr_pages = ++i;
498 ptr += PAGE_SIZE;
501 up(&mm->mmap_sem);
502 dprintk ("map_user_kiobuf: end OK\n");
503 return 0;
505 out_unlock:
506 up(&mm->mmap_sem);
507 unmap_kiobuf(iobuf);
508 dprintk ("map_user_kiobuf: end %d\n", err);
509 return err;
511 retry:
514 * Undo the locking so far, wait on the page we got to, and try again.
516 spin_unlock(&mm->page_table_lock);
517 unmap_kiobuf(iobuf);
518 up(&mm->mmap_sem);
521 * Did the release also unlock the page we got stuck on?
523 if (map) {
524 if (!PageLocked(map)) {
525 /* If so, we may well have the page mapped twice
526 * in the IO address range. Bad news. Of
527 * course, it _might_ * just be a coincidence,
528 * but if it happens more than * once, chances
529 * are we have a double-mapped page. */
530 if (++doublepage >= 3) {
531 return -EINVAL;
536 * Try again...
538 wait_on_page(map);
541 if (++repeat < 16) {
542 ptr = va & PAGE_MASK;
543 goto repeat;
545 return -EAGAIN;
550 * Unmap all of the pages referenced by a kiobuf. We release the pages,
551 * and unlock them if they were locked.
554 void unmap_kiobuf (struct kiobuf *iobuf)
556 int i;
557 struct page *map;
559 for (i = 0; i < iobuf->nr_pages; i++) {
560 map = iobuf->maplist[i];
562 if (map && iobuf->locked) {
563 UnlockPage(map);
564 __free_page(map);
568 iobuf->nr_pages = 0;
569 iobuf->locked = 0;
572 static inline void zeromap_pte_range(pte_t * pte, unsigned long address,
573 unsigned long size, pgprot_t prot)
575 unsigned long end;
577 address &= ~PMD_MASK;
578 end = address + size;
579 if (end > PMD_SIZE)
580 end = PMD_SIZE;
581 do {
582 pte_t zero_pte = pte_wrprotect(mk_pte(ZERO_PAGE(address), prot));
583 pte_t oldpage = *pte;
584 set_pte(pte, zero_pte);
585 forget_pte(oldpage);
586 address += PAGE_SIZE;
587 pte++;
588 } while (address && (address < end));
591 static inline int zeromap_pmd_range(pmd_t * pmd, unsigned long address,
592 unsigned long size, pgprot_t prot)
594 unsigned long end;
596 address &= ~PGDIR_MASK;
597 end = address + size;
598 if (end > PGDIR_SIZE)
599 end = PGDIR_SIZE;
600 do {
601 pte_t * pte = pte_alloc(pmd, address);
602 if (!pte)
603 return -ENOMEM;
604 zeromap_pte_range(pte, address, end - address, prot);
605 address = (address + PMD_SIZE) & PMD_MASK;
606 pmd++;
607 } while (address && (address < end));
608 return 0;
611 int zeromap_page_range(unsigned long address, unsigned long size, pgprot_t prot)
613 int error = 0;
614 pgd_t * dir;
615 unsigned long beg = address;
616 unsigned long end = address + size;
618 dir = pgd_offset(current->mm, address);
619 flush_cache_range(current->mm, beg, end);
620 if (address >= end)
621 BUG();
622 do {
623 pmd_t *pmd = pmd_alloc(dir, address);
624 error = -ENOMEM;
625 if (!pmd)
626 break;
627 error = zeromap_pmd_range(pmd, address, end - address, prot);
628 if (error)
629 break;
630 address = (address + PGDIR_SIZE) & PGDIR_MASK;
631 dir++;
632 } while (address && (address < end));
633 flush_tlb_range(current->mm, beg, end);
634 return error;
638 * maps a range of physical memory into the requested pages. the old
639 * mappings are removed. any references to nonexistent pages results
640 * in null mappings (currently treated as "copy-on-access")
642 static inline void remap_pte_range(pte_t * pte, unsigned long address, unsigned long size,
643 unsigned long phys_addr, pgprot_t prot)
645 unsigned long end;
647 address &= ~PMD_MASK;
648 end = address + size;
649 if (end > PMD_SIZE)
650 end = PMD_SIZE;
651 do {
652 unsigned long mapnr;
653 pte_t oldpage = *pte;
654 pte_clear(pte);
656 mapnr = MAP_NR(__va(phys_addr));
657 if (mapnr >= max_mapnr || PageReserved(mem_map+mapnr))
658 set_pte(pte, mk_pte_phys(phys_addr, prot));
659 forget_pte(oldpage);
660 address += PAGE_SIZE;
661 phys_addr += PAGE_SIZE;
662 pte++;
663 } while (address && (address < end));
666 static inline int remap_pmd_range(pmd_t * pmd, unsigned long address, unsigned long size,
667 unsigned long phys_addr, pgprot_t prot)
669 unsigned long end;
671 address &= ~PGDIR_MASK;
672 end = address + size;
673 if (end > PGDIR_SIZE)
674 end = PGDIR_SIZE;
675 phys_addr -= address;
676 do {
677 pte_t * pte = pte_alloc(pmd, address);
678 if (!pte)
679 return -ENOMEM;
680 remap_pte_range(pte, address, end - address, address + phys_addr, prot);
681 address = (address + PMD_SIZE) & PMD_MASK;
682 pmd++;
683 } while (address && (address < end));
684 return 0;
687 int remap_page_range(unsigned long from, unsigned long phys_addr, unsigned long size, pgprot_t prot)
689 int error = 0;
690 pgd_t * dir;
691 unsigned long beg = from;
692 unsigned long end = from + size;
694 phys_addr -= from;
695 dir = pgd_offset(current->mm, from);
696 flush_cache_range(current->mm, beg, end);
697 if (from >= end)
698 BUG();
699 do {
700 pmd_t *pmd = pmd_alloc(dir, from);
701 error = -ENOMEM;
702 if (!pmd)
703 break;
704 error = remap_pmd_range(pmd, from, end - from, phys_addr + from, prot);
705 if (error)
706 break;
707 from = (from + PGDIR_SIZE) & PGDIR_MASK;
708 dir++;
709 } while (from && (from < end));
710 flush_tlb_range(current->mm, beg, end);
711 return error;
715 * This routine handles present pages, when users try to write
716 * to a shared page. It is done by copying the page to a new address
717 * and decrementing the shared-page counter for the old page.
719 * Goto-purists beware: the only reason for goto's here is that it results
720 * in better assembly code.. The "default" path will see no jumps at all.
722 * Note that this routine assumes that the protection checks have been
723 * done by the caller (the low-level page fault routine in most cases).
724 * Thus we can safely just mark it writable once we've done any necessary
725 * COW.
727 * We also mark the page dirty at this point even though the page will
728 * change only once the write actually happens. This avoids a few races,
729 * and potentially makes it more efficient.
731 * We enter with the page table read-lock held, and need to exit without
732 * it.
734 static int do_wp_page(struct task_struct * tsk, struct vm_area_struct * vma,
735 unsigned long address, pte_t *page_table, pte_t pte)
737 unsigned long map_nr;
738 struct page *old_page, *new_page;
740 map_nr = pte_pagenr(pte);
741 if (map_nr >= max_mapnr)
742 goto bad_wp_page;
743 tsk->min_flt++;
744 old_page = mem_map + map_nr;
747 * We can avoid the copy if:
748 * - we're the only user (count == 1)
749 * - the only other user is the swap cache,
750 * and the only swap cache user is itself,
751 * in which case we can remove the page
752 * from the swap cache.
754 switch (page_count(old_page)) {
755 case 2:
757 * Lock the page so that no one can look it up from
758 * the swap cache, grab a reference and start using it.
759 * Can not do lock_page, holding page_table_lock.
761 if (!PageSwapCache(old_page) || TryLockPage(old_page))
762 break;
763 if (is_page_shared(old_page)) {
764 UnlockPage(old_page);
765 break;
767 delete_from_swap_cache_nolock(old_page);
768 UnlockPage(old_page);
769 /* FallThrough */
770 case 1:
771 flush_cache_page(vma, address);
772 set_pte(page_table, pte_mkyoung(pte_mkdirty(pte_mkwrite(pte))));
773 flush_tlb_page(vma, address);
774 spin_unlock(&tsk->mm->page_table_lock);
775 return 1;
779 * Ok, we need to copy. Oh, well..
781 spin_unlock(&tsk->mm->page_table_lock);
782 new_page = alloc_page(GFP_HIGHUSER);
783 if (!new_page)
784 return -1;
785 spin_lock(&tsk->mm->page_table_lock);
788 * Re-check the pte - we dropped the lock
790 if (pte_val(*page_table) == pte_val(pte)) {
791 if (PageReserved(old_page))
792 ++vma->vm_mm->rss;
793 copy_cow_page(old_page, new_page, address);
794 flush_page_to_ram(new_page);
795 flush_cache_page(vma, address);
796 set_pte(page_table, pte_mkwrite(pte_mkdirty(mk_pte(new_page, vma->vm_page_prot))));
797 flush_tlb_page(vma, address);
799 /* Free the old page.. */
800 new_page = old_page;
802 spin_unlock(&tsk->mm->page_table_lock);
803 __free_page(new_page);
804 return 1;
806 bad_wp_page:
807 spin_unlock(&tsk->mm->page_table_lock);
808 printk("do_wp_page: bogus page at address %08lx (nr %ld)\n",address,map_nr);
809 return -1;
813 * This function zeroes out partial mmap'ed pages at truncation time..
815 static void partial_clear(struct vm_area_struct *vma, unsigned long address)
817 unsigned int offset;
818 struct page *page;
819 pgd_t *page_dir;
820 pmd_t *page_middle;
821 pte_t *page_table, pte;
823 page_dir = pgd_offset(vma->vm_mm, address);
824 if (pgd_none(*page_dir))
825 return;
826 if (pgd_bad(*page_dir)) {
827 pgd_ERROR(*page_dir);
828 pgd_clear(page_dir);
829 return;
831 page_middle = pmd_offset(page_dir, address);
832 if (pmd_none(*page_middle))
833 return;
834 if (pmd_bad(*page_middle)) {
835 pmd_ERROR(*page_middle);
836 pmd_clear(page_middle);
837 return;
839 page_table = pte_offset(page_middle, address);
840 pte = *page_table;
841 if (!pte_present(pte))
842 return;
843 flush_cache_page(vma, address);
844 page = pte_page(pte);
845 if (page-mem_map >= max_mapnr)
846 return;
847 offset = address & ~PAGE_MASK;
848 memclear_highpage_flush(page, offset, PAGE_SIZE - offset);
852 * Handle all mappings that got truncated by a "truncate()"
853 * system call.
855 * NOTE! We have to be ready to update the memory sharing
856 * between the file and the memory map for a potential last
857 * incomplete page. Ugly, but necessary.
859 void vmtruncate(struct inode * inode, loff_t offset)
861 unsigned long partial, pgoff;
862 struct vm_area_struct * mpnt;
863 struct address_space *mapping = inode->i_mapping;
865 inode->i_size = offset;
866 truncate_inode_pages(mapping, offset);
867 spin_lock(&mapping->i_shared_lock);
868 if (!mapping->i_mmap)
869 goto out_unlock;
871 pgoff = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
872 partial = (unsigned long)offset & (PAGE_CACHE_SIZE - 1);
874 mpnt = mapping->i_mmap;
875 do {
876 struct mm_struct *mm = mpnt->vm_mm;
877 unsigned long start = mpnt->vm_start;
878 unsigned long end = mpnt->vm_end;
879 unsigned long len = end - start;
880 unsigned long diff;
882 /* mapping wholly truncated? */
883 if (mpnt->vm_pgoff >= pgoff) {
884 flush_cache_range(mm, start, end);
885 zap_page_range(mm, start, len);
886 flush_tlb_range(mm, start, end);
887 continue;
890 /* mapping wholly unaffected? */
891 len = len >> PAGE_SHIFT;
892 diff = pgoff - mpnt->vm_pgoff;
893 if (diff >= len)
894 continue;
896 /* Ok, partially affected.. */
897 start += diff << PAGE_SHIFT;
898 len = (len - diff) << PAGE_SHIFT;
899 if (start & ~PAGE_MASK) {
900 partial_clear(mpnt, start);
901 start = (start + ~PAGE_MASK) & PAGE_MASK;
903 flush_cache_range(mm, start, end);
904 zap_page_range(mm, start, len);
905 flush_tlb_range(mm, start, end);
906 } while ((mpnt = mpnt->vm_next_share) != NULL);
907 out_unlock:
908 spin_unlock(&mapping->i_shared_lock);
909 if (inode->i_op && inode->i_op->truncate)
910 inode->i_op->truncate(inode);
916 * Primitive swap readahead code. We simply read an aligned block of
917 * (1 << page_cluster) entries in the swap area. This method is chosen
918 * because it doesn't cost us any seek time. We also make sure to queue
919 * the 'original' request together with the readahead ones...
921 void swapin_readahead(swp_entry_t entry)
923 int i, num;
924 struct page *new_page;
925 unsigned long offset;
928 * Get the number of handles we should do readahead io to. Also,
929 * grab temporary references on them, releasing them as io completes.
931 num = valid_swaphandles(entry, &offset);
932 for (i = 0; i < num; offset++, i++) {
933 /* Don't block on I/O for read-ahead */
934 if (atomic_read(&nr_async_pages) >= pager_daemon.swap_cluster) {
935 while (i++ < num)
936 swap_free(SWP_ENTRY(SWP_TYPE(entry), offset++));
937 break;
939 /* Ok, do the async read-ahead now */
940 new_page = read_swap_cache_async(SWP_ENTRY(SWP_TYPE(entry), offset), 0);
941 if (new_page != NULL)
942 __free_page(new_page);
943 swap_free(SWP_ENTRY(SWP_TYPE(entry), offset));
945 return;
948 static int do_swap_page(struct task_struct * tsk,
949 struct vm_area_struct * vma, unsigned long address,
950 pte_t * page_table, swp_entry_t entry, int write_access)
952 struct page *page = lookup_swap_cache(entry);
953 pte_t pte;
955 if (!page) {
956 lock_kernel();
957 swapin_readahead(entry);
958 page = read_swap_cache(entry);
959 unlock_kernel();
960 if (!page)
961 return -1;
963 flush_page_to_ram(page);
964 flush_icache_page(vma, page);
967 vma->vm_mm->rss++;
968 tsk->min_flt++;
970 pte = mk_pte(page, vma->vm_page_prot);
972 set_bit(PG_swap_entry, &page->flags);
975 * Freeze the "shared"ness of the page, ie page_count + swap_count.
976 * Must lock page before transferring our swap count to already
977 * obtained page count.
979 lock_page(page);
980 swap_free(entry);
981 if (write_access && !is_page_shared(page)) {
982 delete_from_swap_cache_nolock(page);
983 UnlockPage(page);
984 page = replace_with_highmem(page);
985 pte = mk_pte(page, vma->vm_page_prot);
986 pte = pte_mkwrite(pte_mkdirty(pte));
987 } else
988 UnlockPage(page);
990 set_pte(page_table, pte);
991 /* No need to invalidate - it was non-present before */
992 update_mmu_cache(vma, address, pte);
993 return 1;
997 * This only needs the MM semaphore
999 static int do_anonymous_page(struct task_struct * tsk, struct vm_area_struct * vma, pte_t *page_table, int write_access, unsigned long addr)
1001 int high = 0;
1002 struct page *page = NULL;
1003 pte_t entry = pte_wrprotect(mk_pte(ZERO_PAGE(addr), vma->vm_page_prot));
1004 if (write_access) {
1005 page = alloc_page(GFP_HIGHUSER);
1006 if (!page)
1007 return -1;
1008 if (PageHighMem(page))
1009 high = 1;
1010 clear_highpage(page);
1011 entry = pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
1012 vma->vm_mm->rss++;
1013 tsk->min_flt++;
1014 flush_page_to_ram(page);
1016 set_pte(page_table, entry);
1017 /* No need to invalidate - it was non-present before */
1018 update_mmu_cache(vma, addr, entry);
1019 return 1;
1023 * do_no_page() tries to create a new page mapping. It aggressively
1024 * tries to share with existing pages, but makes a separate copy if
1025 * the "write_access" parameter is true in order to avoid the next
1026 * page fault.
1028 * As this is called only for pages that do not currently exist, we
1029 * do not need to flush old virtual caches or the TLB.
1031 * This is called with the MM semaphore held.
1033 static int do_no_page(struct task_struct * tsk, struct vm_area_struct * vma,
1034 unsigned long address, int write_access, pte_t *page_table)
1036 struct page * new_page;
1037 pte_t entry;
1039 if (!vma->vm_ops || !vma->vm_ops->nopage)
1040 return do_anonymous_page(tsk, vma, page_table, write_access, address);
1043 * The third argument is "no_share", which tells the low-level code
1044 * to copy, not share the page even if sharing is possible. It's
1045 * essentially an early COW detection.
1047 new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, (vma->vm_flags & VM_SHARED)?0:write_access);
1048 if (new_page == NULL) /* no page was available -- SIGBUS */
1049 return 0;
1050 if (new_page == NOPAGE_OOM)
1051 return -1;
1052 ++tsk->maj_flt;
1053 ++vma->vm_mm->rss;
1055 * This silly early PAGE_DIRTY setting removes a race
1056 * due to the bad i386 page protection. But it's valid
1057 * for other architectures too.
1059 * Note that if write_access is true, we either now have
1060 * an exclusive copy of the page, or this is a shared mapping,
1061 * so we can make it writable and dirty to avoid having to
1062 * handle that later.
1064 flush_page_to_ram(new_page);
1065 flush_icache_page(vma, new_page);
1066 entry = mk_pte(new_page, vma->vm_page_prot);
1067 if (write_access) {
1068 entry = pte_mkwrite(pte_mkdirty(entry));
1069 } else if (page_count(new_page) > 1 &&
1070 !(vma->vm_flags & VM_SHARED))
1071 entry = pte_wrprotect(entry);
1072 set_pte(page_table, entry);
1073 /* no need to invalidate: a not-present page shouldn't be cached */
1074 update_mmu_cache(vma, address, entry);
1075 return 1;
1079 * These routines also need to handle stuff like marking pages dirty
1080 * and/or accessed for architectures that don't do it in hardware (most
1081 * RISC architectures). The early dirtying is also good on the i386.
1083 * There is also a hook called "update_mmu_cache()" that architectures
1084 * with external mmu caches can use to update those (ie the Sparc or
1085 * PowerPC hashed page tables that act as extended TLBs).
1087 * Note the "page_table_lock". It is to protect against kswapd removing
1088 * pages from under us. Note that kswapd only ever _removes_ pages, never
1089 * adds them. As such, once we have noticed that the page is not present,
1090 * we can drop the lock early.
1092 * The adding of pages is protected by the MM semaphore (which we hold),
1093 * so we don't need to worry about a page being suddenly been added into
1094 * our VM.
1096 static inline int handle_pte_fault(struct task_struct *tsk,
1097 struct vm_area_struct * vma, unsigned long address,
1098 int write_access, pte_t * pte)
1100 pte_t entry;
1102 entry = *pte;
1103 if (!pte_present(entry)) {
1104 if (pte_none(entry))
1105 return do_no_page(tsk, vma, address, write_access, pte);
1106 return do_swap_page(tsk, vma, address, pte, pte_to_swp_entry(entry), write_access);
1110 * Ok, the entry was present, we need to get the page table
1111 * lock to synchronize with kswapd, and verify that the entry
1112 * didn't change from under us..
1114 spin_lock(&tsk->mm->page_table_lock);
1115 if (pte_val(entry) == pte_val(*pte)) {
1116 if (write_access) {
1117 if (!pte_write(entry))
1118 return do_wp_page(tsk, vma, address, pte, entry);
1120 entry = pte_mkdirty(entry);
1122 entry = pte_mkyoung(entry);
1123 set_pte(pte, entry);
1124 flush_tlb_page(vma, address);
1125 update_mmu_cache(vma, address, entry);
1127 spin_unlock(&tsk->mm->page_table_lock);
1128 return 1;
1132 * By the time we get here, we already hold the mm semaphore
1134 int handle_mm_fault(struct task_struct *tsk, struct vm_area_struct * vma,
1135 unsigned long address, int write_access)
1137 int ret = -1;
1138 pgd_t *pgd;
1139 pmd_t *pmd;
1141 pgd = pgd_offset(vma->vm_mm, address);
1142 pmd = pmd_alloc(pgd, address);
1144 if (pmd) {
1145 pte_t * pte = pte_alloc(pmd, address);
1146 if (pte)
1147 ret = handle_pte_fault(tsk, vma, address, write_access, pte);
1149 return ret;
1153 * Simplistic page force-in..
1155 int make_pages_present(unsigned long addr, unsigned long end)
1157 int write;
1158 struct task_struct *tsk = current;
1159 struct vm_area_struct * vma;
1161 vma = find_vma(tsk->mm, addr);
1162 write = (vma->vm_flags & VM_WRITE) != 0;
1163 if (addr >= end)
1164 BUG();
1165 do {
1166 if (handle_mm_fault(tsk, vma, addr, write) < 0)
1167 return -1;
1168 addr += PAGE_SIZE;
1169 } while (addr < end);
1170 return 0;