linux内存管理源码分析memory.c上篇(基于linux1.12.13)

/*
 *  linux/mm/memory.c
 *
 *  Copyright (C) 1991, 1992, 1993, 1994  Linus Torvalds
 */

/*
 * demand-loading started 01.12.91 - seems it is high on the list of
 * things wanted, and it should be easy to implement. - Linus
 */

/*
 * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
 * pages started 02.12.91, seems to work. - Linus.
 *
 * Tested sharing by executing about 30 /bin/sh: under the old kernel it
 * would have taken more than the 6M I have free, but it worked well as
 * far as I could see.
 *
 * Also corrected some "invalidate()"s - I wasn't doing enough of them.
 */

/*
 * Real VM (paging to/from disk) started 18.12.91. Much more work and
 * thought has to go into this. Oh, well..
 * 19.12.91  -  works, somewhat. Sometimes I get faults, don't know why.
 *    Found it. Everything seems to work now.
 * 20.12.91  -  Ok, making the swap-device changeable like the root.
 */

/*
 * 05.04.94  -  Multi-page memory management added for v1.1.
 *     Idea by Alex Bligh (alex@cconcepts.co.uk)
 */

#include <linux/config.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/head.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/types.h>
#include <linux/ptrace.h>
#include <linux/mman.h>
#include <linux/mm.h>

#include <asm/system.h>
#include <asm/segment.h>
#include <asm/pgtable.h>

unsigned long high_memory = 0;

/*
 * The free_area_list arrays point to the queue heads of the free areas
 * of different sizes
 */
int nr_swap_pages = 0;
int nr_free_pages = 0;
struct mem_list free_area_list[NR_MEM_LISTS];
unsigned char * free_area_map[NR_MEM_LISTS];

#define copy_page(from,to) memcpy((void *) to, (void *) from, PAGE_SIZE)
// 用户空间的最高级页目录项数,TASK_SIZE是用户空间的地址大小,PGDIR_SIZE是每一个最高级页目录项能管理的地址大小,这里是4M,算出共几个最高级目录项
#define USER_PTRS_PER_PGD (TASK_SIZE / PGDIR_SIZE)

mem_map_t * mem_map = NULL;

/*
 * oom() prints a message (so that the user knows why the process died),
 * and gives the process an untrappable SIGKILL.
 */
void oom(struct task_struct * task)
{
  printk("\nOut of memory for %s.\n", current->comm);
  task->sigaction[SIGKILL-1].sa_handler = NULL;
  task->blocked &= ~(1<<(SIGKILL-1));
  send_sig(SIGKILL,task,1);
}
// 释放一个页表项
static inline void free_one_pte(pte_t * page_table)
{
  pte_t page = *page_table;
  // 无效的页表项,直接返回
  if (pte_none(page))
    return;
  // 清空页表项内容,值为0
  pte_clear(page_table);
  // 如果页表项映射了物理地址,即present为1.
  if (!pte_present(page)) {
    // 
    swap_free(pte_val(page));
    return;
  }
  // 释放物理地址,pte_page得到页表项里记录的物理地址 
  free_page(pte_page(page));
  return;
}
// 释放二级页目录项和对应的页表、页表项
static inline void free_one_pmd(pmd_t * dir)
{
  int j;
  pte_t * pte;
  // 无效
  if (pmd_none(*dir))
    return;
  
  if (pmd_bad(*dir)) {
    printk("free_one_pmd: bad directory entry %08lx\n", pmd_val(*dir));
    pmd_clear(dir);
    return;
  }
  // 得到整个页表的首地址,也是第一个页表项的地址 
  pte = pte_offset(dir, 0);
  // 清除页目录项内容
  pmd_clear(dir);
  //  如果还有其他进程使用,这时候pte_free只会对pte对应的物理内存引用数减一,并且不能释放页表里的页表项
  if (pte_inuse(pte)) {
    pte_free(pte);
    return;
  }
  // 没有进程使用了,释放页表里每一个页表项
  for (j = 0; j < PTRS_PER_PTE ; j++)
    free_one_pte(pte+j);
  // 释放页表,这时候会回收物理地址,因为没人使用了
  pte_free(pte);
}
// 释放三级页目录项和相应的页表、页表项
static inline void free_one_pgd(pgd_t * dir)
{
  int j;
  pmd_t * pmd;

  if (pgd_none(*dir))
    return;
  if (pgd_bad(*dir)) {
    printk("free_one_pgd: bad directory entry %08lx\n", pgd_val(*dir));
    pgd_clear(dir);
    return;
  }
  // 取得pgd里保存的二级目录表首地址
  pmd = pmd_offset(dir, 0);
  // 清空pgd的内容
  pgd_clear(dir);
  // 如果pmd还有其他进程在使用,则pmd引用数减一即可
  if (pmd_inuse(pmd)) {
    pmd_free(pmd);
    return;
  }
  // 否则释放每一个二级目录表、目录表保存的页表、页表项
  for (j = 0; j < PTRS_PER_PMD ; j++)
    free_one_pmd(pmd+j);
  // 页目录表引用数减一
  pmd_free(pmd);
}
  

/*
 * This function clears all user-level page tables of a process - this
 * is needed by execve(), so that old pages aren't in the way. Note that
 * unlike 'free_page_tables()', this function still leaves a valid
 * page-table-tree in memory: it just removes the user pages. The two
 * functions are similar, but there is a fundamental difference.
 */
// 释放用户空间的页目录、页表
void clear_page_tables(struct task_struct * tsk)
{
  int i;
  pgd_t * page_dir;

  if (!tsk)
    return;
  // 不能释放进程0的页表
  if (tsk == task[0])
    panic("task[0] (swapper) doesn't support exec()\n");
  // 取得最高级页目录表的首地址
  page_dir = pgd_offset(tsk, 0);
  // 无效或者非法
  if (!page_dir || page_dir == swapper_pg_dir) {
    printk("%s trying to clear kernel page-directory: not good\n", tsk->comm);
    return;
  }
  // 还有进程在使用 
  if (pgd_inuse(page_dir)) {
    pgd_t * new_pg;
    // 申请一页
    if (!(new_pg = pgd_alloc())) {
      oom(tsk);
      return;
    }
    // 把用户空间的页表信息保存在新的页里
    for (i = USER_PTRS_PER_PGD ; i < PTRS_PER_PGD ; i++)
      new_pg[i] = page_dir[i];
    // 更新当前进程的最高级页目录表地址到cr3
    SET_PAGE_DIR(tsk, new_pg);
    // 最高级页目录表对应物理地址引用数减一
    pgd_free(page_dir);
    return;
  }
  // 释放每一个最高级的页目录项
  for (i = 0 ; i < USER_PTRS_PER_PGD ; i++)
    free_one_pgd(page_dir + i);
  // 刷新快表,使得对应的信息无效
  invalidate();
  return;
}

/*
 * This function frees up all page tables of a process when it exits.
 */
// 释放进程的所有页目录、页表
void free_page_tables(struct task_struct * tsk)
{
  int i;
  pgd_t * page_dir;

  if (!tsk)
    return;
  if (tsk == task[0]) {
    printk("task[0] (swapper) killed: unable to recover\n");
    panic("Trying to free up swapper memory space");
  }
  // 最高级页目录表首地址
  page_dir = pgd_offset(tsk, 0);
  if (!page_dir || page_dir == swapper_pg_dir) {
    printk("%s trying to free kernel page-directory: not good\n", tsk->comm);
    return;
  }
  // 更新进程的cr3字段
  SET_PAGE_DIR(tsk, swapper_pg_dir);
  // 对应的物理地址还有其他进程使用,则引用数减一
  if (pgd_inuse(page_dir)) {
    pgd_free(page_dir);
    return;
  }
  // 没有被使用了,释放全部页表信息
  for (i = 0 ; i < PTRS_PER_PGD ; i++)
    free_one_pgd(page_dir + i);
  // 释放pgd对应的物理内存
  pgd_free(page_dir);
  // 刷新快表
  invalidate();
}

/*
 * clone_page_tables() clones the page table for a process - both
 * processes will have the exact same pages in memory. There are
 * probably races in the memory management with cloning, but we'll
 * see..
 */
// 复制页表信息
int clone_page_tables(struct task_struct * tsk)
{
  pgd_t * pg_dir;
  // 取得当前进程最高级页目录表的地址
  pg_dir = pgd_offset(current, 0);
  // 引用数加一 
  pgd_reuse(pg_dir);
  // 设置进程的cr3位新的页目录表地址
  SET_PAGE_DIR(tsk, pg_dir);
  return 0;
}
// 复制一个页表项
static inline void copy_one_pte(pte_t * old_pte, pte_t * new_pte)
{
  pte_t pte = *old_pte;
  // 有效性判断
  if (pte_none(pte))
    return;
  // 页表项没有映射到物理页
  if (!pte_present(pte)) {
    swap_duplicate(pte_val(pte));
    *new_pte = pte;
    return;
  }
  if (pte_page(pte) > high_memory || (mem_map[MAP_NR(pte_page(pte))] & MAP_PAGE_RESERVED)) {
    *new_pte = pte;
    return;
  }
  // 
  if (pte_cow(pte))
    pte = pte_wrprotect(pte);
  if (delete_from_swap_cache(pte_page(pte)))
    pte = pte_mkdirty(pte);
  *new_pte = pte_mkold(pte);
  *old_pte = pte;
  mem_map[MAP_NR(pte_page(pte))]++;
}
// 复制一个二级页目录项
static inline int copy_one_pmd(pmd_t * old_pmd, pmd_t * new_pmd)
{
  int j;
  pte_t *old_pte, *new_pte;

  if (pmd_none(*old_pmd))
    return 0;
  if (pmd_bad(*old_pmd)) {
    printk("copy_one_pmd: bad page table: probable memory corruption\n");
    pmd_clear(old_pmd);
    return 0;
  }
  // 取得该页目录项对应的页表首地址
  old_pte = pte_offset(old_pmd, 0);
  // 页表有其他进程使用
  if (pte_inuse(old_pte)) {
    // 引用数加一
    pte_reuse(old_pte);
    // 复制页表首地址到页目录项里
    *new_pmd = *old_pmd;
    return 0;
  }
  // 在页目录项中取得页表首地址,然后返回第一个页表项地址,如果new_pmd是空,则分配新的一个,new_pmd指向新页的地址
  new_pte = pte_alloc(new_pmd, 0);
  if (!new_pte)
    return -ENOMEM;
  // 复制每一个页表项,即复制页表
  for (j = 0 ; j < PTRS_PER_PTE ; j++) {
    copy_one_pte(old_pte, new_pte);
    old_pte++;
    new_pte++;
  }
  return 0;
}
// 同上
static inline int copy_one_pgd(pgd_t * old_pgd, pgd_t * new_pgd)
{
  int j;
  pmd_t *old_pmd, *new_pmd;

  if (pgd_none(*old_pgd))
    return 0;
  if (pgd_bad(*old_pgd)) {
    printk("copy_one_pgd: bad page table (%p: %08lx): probable memory corruption\n", old_pgd, pgd_val(*old_pgd));
    pgd_clear(old_pgd);
    return 0;
  }
  old_pmd = pmd_offset(old_pgd, 0);
  if (pmd_inuse(old_pmd)) {
    pmd_reuse(old_pmd);
    *new_pgd = *old_pgd;
    return 0;
  }
  new_pmd = pmd_alloc(new_pgd, 0);
  if (!new_pmd)
    return -ENOMEM;
  for (j = 0 ; j < PTRS_PER_PMD ; j++) {
    int error = copy_one_pmd(old_pmd, new_pmd);
    if (error)
      return error;
    old_pmd++;
    new_pmd++;
  }
  return 0;
}

/*
 * copy_page_tables() just copies the whole process memory range:
 * note the special handling of RESERVED (ie kernel) pages, which
 * means that they are always shared by all processes.
 */
// 复制页表信息
int copy_page_tables(struct task_struct * tsk)
{
  int i;
  pgd_t *old_pgd;
  pgd_t *new_pgd;
  // 分配一页
  new_pgd = pgd_alloc();
  if (!new_pgd)
    return -ENOMEM;
  // 设置进程的cr3字段,即最高级页目录表首地址
  SET_PAGE_DIR(tsk, new_pgd);
  // 取得当前进程的最高级页目录表首地址
  old_pgd = pgd_offset(current, 0);
  // 复制每一项
  for (i = 0 ; i < PTRS_PER_PGD ; i++) {
    int errno = copy_one_pgd(old_pgd, new_pgd);
    if (errno) {
      free_page_tables(tsk);
      invalidate();
      return errno;
    }
    old_pgd++;
    new_pgd++;
  }
  invalidate();
  return 0;
}
// 释放页表项对应虚拟地址的物理页
static inline void forget_pte(pte_t page)
{
  if (pte_none(page))
    return;
  // 页表项映射了物理内存
  if (pte_present(page)) {
    // 物理页引用数减一
    free_page(pte_page(page));
    // 是保留页则直接返回
    if (mem_map[MAP_NR(pte_page(page))] & MAP_PAGE_RESERVED)
      return;
    if (current->mm->rss <= 0)
      return;
    // 进程驻留内存的页数减一
    current->mm->rss--;
    return;
  }
  // 释放交换区
  swap_free(pte_val(page));
}

static inline void unmap_pte_range(pmd_t * pmd, unsigned long address, unsigned long size)
{
  pte_t * pte;
  unsigned long end;

  if (pmd_none(*pmd))
    return;
  if (pmd_bad(*pmd)) {
    printk("unmap_pte_range: bad pmd (%08lx)\n", pmd_val(*pmd));
    pmd_clear(pmd);
    return;
  }
  // 取得address对应的页表项首地址,address是虚拟内存,里面保存了该页表项的偏移
  pte = pte_offset(pmd, address);
  // 取得物理页内偏移
  address &= ~PMD_MASK;
  // 物理地址的结束地址
  end = address + size;
  // 是否超出了该页目录项管理的内存大小
  if (end >= PMD_SIZE)
    end = PMD_SIZE;
  do {
    // 页表项内容
    pte_t page = *pte;
    // 清空
    pte_clear(pte);
    forget_pte(page);
    address += PAGE_SIZE;
    pte++;
  } while (address < end);
}

static inline void unmap_pmd_range(pgd_t * dir, unsigned long address, unsigned long size)
{
  pmd_t * pmd;
  unsigned long end;

  if (pgd_none(*dir))
    return;
  if (pgd_bad(*dir)) {
    printk("unmap_pmd_range: bad pgd (%08lx)\n", pgd_val(*dir));
    pgd_clear(dir);
    return;
  }
  pmd = pmd_offset(dir, address);
  address &= ~PGDIR_MASK;
  end = address + size;
  if (end > PGDIR_SIZE)
    end = PGDIR_SIZE;
  do {
    unmap_pte_range(pmd, address, end - address);
    address = (address + PMD_SIZE) & PMD_MASK; 
    pmd++;
  } while (address < end);
}

/*
 * a more complete version of free_page_tables which performs with page
 * granularity.
 */
int unmap_page_range(unsigned long address, unsigned long size)
{
  pgd_t * dir;
  unsigned long end = address + size;

  dir = pgd_offset(current, address);
  while (address < end) {
    unmap_pmd_range(dir, address, end - address);
    address = (address + PGDIR_SIZE) & PGDIR_MASK;
    dir++;
  }
  invalidate();
  return 0;
}
// 重新设置address到address+size地址范围内的页表项的内容,释放旧的物理地址
static inline void zeromap_pte_range(pte_t * pte, unsigned long address, unsigned long size, pte_t zero_pte)
{
  unsigned long end;
  // 屏蔽高位  
  address &= ~PMD_MASK;
  // 末地址
  end = address + size;
  // 末地址是否超过了该页目录项管理的地址范围
  if (end > PMD_SIZE)
    end = PMD_SIZE;
  do {
    pte_t oldpage = *pte;
    // 设置页表项的新内容
    *pte = zero_pte;
    // 释放旧的物理页
    forget_pte(oldpage);
    // 下一个页表项
    address += PAGE_SIZE;
    pte++;
  } while (address < end);
}
// 重新设置address到address+size地址范围内的页目录项内容,释放旧的物理页
static inline int zeromap_pmd_range(pmd_t * pmd, unsigned long address, unsigned long size, pte_t zero_pte)
{
  unsigned long end;

  address &= ~PGDIR_MASK;
  end = address + size;
  if (end > PGDIR_SIZE)
    end = PGDIR_SIZE;
  do {
    pte_t * pte = pte_alloc(pmd, address);
    if (!pte)
      return -ENOMEM;
    // 重新设置一个页目录项的内容和释放物理页
    zeromap_pte_range(pte, address, end - address, zero_pte);
    address = (address + PMD_SIZE) & PMD_MASK;
    pmd++;
  } while (address < end);
  return 0;
}
// 设置address到address+size地址范围内的页目录、页表内容 
int zeromap_page_range(unsigned long address, unsigned long size, pgprot_t prot)
{
  int error = 0;
  pgd_t * dir;
  unsigned long end = address + size;
  pte_t zero_pte;
  // 新的页表项,设置写保护,ZERO_PAGE is a global shared page that is always zero
  zero_pte = pte_wrprotect(mk_pte(ZERO_PAGE, prot));
  // 取得最高级页目录项的地址
  dir = pgd_offset(current, address);
  // 设置地址范围内的页目录、页表内容为zero_pte
  while (address < end) {
    pmd_t *pmd = pmd_alloc(dir, address);
    error = -ENOMEM;
    if (!pmd)
      break;
    error = zeromap_pmd_range(pmd, address, end - address, zero_pte);
    if (error)
      break;
    // 下一个最高级页目录项
    address = (address + PGDIR_SIZE) & PGDIR_MASK;
    dir++;
  }
  // 刷新快表
  invalidate();
  return error;
}

/*
 * maps a range of physical memory into the requested pages. the old
 * mappings are removed. any references to nonexistent pages results
 * in null mappings (currently treated as "copy-on-access")
 */
// 把物理地址offset保存到页表项中,prot为属性,size是保存连续的多页物理地址到多个页表项中
static inline void remap_pte_range(pte_t * pte, unsigned long address, unsigned long size,
  unsigned long offset, pgprot_t prot)
{
  unsigned long end;
  // 取得页目录项内的地址范围
  address &= ~PMD_MASK;
  // 末虚拟地址
  end = address + size;
  if (end > PMD_SIZE)
    end = PMD_SIZE;
  do {
    // 先保存旧的数据
    pte_t oldpage = *pte;
    // 清空页表项内容
    pte_clear(pte);
    // 把物理地址保存到页表项
    if (offset >= high_memory || (mem_map[MAP_NR(offset)] & MAP_PAGE_RESERVED))
      *pte = mk_pte(offset, prot);
    else if (mem_map[MAP_NR(offset)]) {
      mem_map[MAP_NR(offset)]++;
      *pte = mk_pte(offset, prot);
    }
    // 释放老数据
    forget_pte(oldpage);
    // 下一个虚拟地址
    address += PAGE_SIZE;
    // 下一个物理地址
    offset += PAGE_SIZE;
    pte++;
  } while (address < end);
}
// 保存多个连续的物理地址到多个页目录项的1024页表项中
static inline int remap_pmd_range(pmd_t * pmd, unsigned long address, unsigned long size,
  unsigned long offset, pgprot_t prot)
{
  unsigned long end;

  address &= ~PGDIR_MASK;
  end = address + size;
  if (end > PGDIR_SIZE)
    end = PGDIR_SIZE;
  offset -= address;
  do {
    // 根据虚拟地址算出页表项在页目录项的位置
    pte_t * pte = pte_alloc(pmd, address);
    if (!pte)
      return -ENOMEM;
    // 逐个页表项处理
    remap_pte_range(pte, address, end - address, address + offset, prot);
    address = (address + PMD_SIZE) & PMD_MASK;
    // 下一个页目录项
    pmd++;
  } while (address < end);
  return 0;
}

// 同上
int remap_page_range(unsigned long from, unsigned long offset, unsigned long size, pgprot_t prot)
{
  int error = 0;
  pgd_t * dir;
  unsigned long end = from + size;

  offset -= from;
  dir = pgd_offset(current, from);
  while (from < end) {
    pmd_t *pmd = pmd_alloc(dir, from);
    error = -ENOMEM;
    if (!pmd)
      break;
    error = remap_pmd_range(pmd, from, end - from, offset + from, prot);
    if (error)
      break;
    from = (from + PGDIR_SIZE) & PGDIR_MASK;
    dir++;
  }
  invalidate();
  return error;
}

/*
 * sanity-check function..
 */
// 复制页表项内容
static void put_page(pte_t * page_table, pte_t pte)
{  
  // 页表项已经保存了映射信息
  if (!pte_none(*page_table)) {
    printk("put_page: page already exists %08lx\n", pte_val(*page_table));
    free_page(pte_page(pte));
    return;
  }
/* no need for invalidate */
  // 复制
  *page_table = pte;
}

/*
 * This routine is used to map in a page into an address space: needed by
 * execve() for the initial stack and environment pages.
 */
// 把address对应的物理地址写入页表。address是虚拟地址,page是物理地址
unsigned long put_dirty_page(struct task_struct * tsk, unsigned long page, unsigned long address)
{
  pgd_t * pgd;
  pmd_t * pmd;
  pte_t * pte;

  if (page >= high_memory)
    printk("put_dirty_page: trying to put page %08lx at %08lx\n",page,address);
  // 该物理地址没有标记为使用,所以不能映射
  if (mem_map[MAP_NR(page)] != 1)
    printk("mem_map disagrees with %08lx at %08lx\n",page,address);
  // 取得address对应的最高级目录表中的一个项
  pgd = pgd_offset(tsk,address);
  // 取得二级目录表中的某个项,即页表首地址,pgd为空则分配新页,pgd指向新页
  pmd = pmd_alloc(pgd, address);
  if (!pmd) {
    free_page(page);
    oom(tsk);
    return 0;
  }
  // 同上,取得一个页表项首地址
  pte = pte_alloc(pmd, address);
  if (!pte) {
    free_page(page);
    oom(tsk);
    return 0;
  }
  // 页表项已经有信息了
  if (!pte_none(*pte)) {
    printk("put_dirty_page: page already exists\n");
    pte_clear(pte);
    invalidate();
  }
  // 填充页表项里的内容,包括dirty,可读写执行,PAGE_COPY看定义
  *pte = pte_mkwrite(pte_mkdirty(mk_pte(page, PAGE_COPY)));
/* no need for invalidate */
  return page;
}

/*
 * This routine handles present pages, when users try to write
 * to a shared page. It is done by copying the page to a new address
 * and decrementing the shared-page counter for the old page.
 *
 * Goto-purists beware: the only reason for goto's here is that it results
 * in better assembly code.. The "default" path will see no jumps at all.
 *
 * Note that this routine assumes that the protection checks have been
 * done by the caller (the low-level page fault routine in most cases).
 * Thus we can safely just mark it writable once we've done any necessary
 * COW.
 *
 * We also mark the page dirty at this point even though the page will
 * change only once the write actually happens. This avoids a few races,
 * and potentially makes it more efficient.
 */
// 用户写多个进程共用的页时,需要申请一个新页,然后把旧页的数据复制过来,修改页表项
void do_wp_page(struct vm_area_struct * vma, unsigned long address,
  int write_access)
{
  pgd_t *page_dir;
  pmd_t *page_middle;
  pte_t *page_table, pte;
  unsigned long old_page, new_page;
  // 获取一页物理页
  new_page = __get_free_page(GFP_KERNEL);
  // 获取进程最高级页目录项
  page_dir = pgd_offset(vma->vm_task,address);
  if (pgd_none(*page_dir))
    goto end_wp_page;
  if (pgd_bad(*page_dir))
    goto bad_wp_pagedir;
  // 获取对应的二级目录项
  page_middle = pmd_offset(page_dir, address);
  if (pmd_none(*page_middle))
    goto end_wp_page;
  if (pmd_bad(*page_middle))
    goto bad_wp_pagemiddle;
  // 获取页表项
  page_table = pte_offset(page_middle, address);
  pte = *page_table;
  // 没有映射物理内存
  if (!pte_present(pte))
    goto end_wp_page;
  // 可写,则不需要处理
  if (pte_write(pte))
    goto end_wp_page;
  // 获取对应的物理地址 
  old_page = pte_page(pte);
  if (old_page >= high_memory)
    goto bad_wp_page;
  vma->vm_task->mm->min_flt++;
  /*
   * Do we need to copy?
   */ 
  // 如果有多个进程在使用,则需要为执行写操作的进程,新增一个页表项,指向新页,如果只有一个进程在使用,则不需要
  if (mem_map[MAP_NR(old_page)] != 1) {
    if (new_page) {
      if (mem_map[MAP_NR(old_page)] & MAP_PAGE_RESERVED)
        ++vma->vm_task->mm->rss;
      // 把物理页里的内容复制到新页
      copy_page(old_page,new_page);
      // 标记新页可读写执行,dirty等
      *page_table = pte_mkwrite(pte_mkdirty(mk_pte(new_page, vma->vm_page_prot)));
      // 老页引用数减一
      free_page(old_page);
      // 刷新快表
      invalidate();
      return;
    }
    // 申请新页失败,标记页表项为失败
    *page_table = BAD_PAGE;
    // 老页引用数减一
    free_page(old_page);
    oom(vma->vm_task);
    invalidate();
    return;
  }
  // 打标记
  *page_table = pte_mkdirty(pte_mkwrite(pte));
  // 刷新快表
  invalidate();
  // 释放申请的新页,因为没有用到
  if (new_page)
    free_page(new_page);
  return;
bad_wp_page:
  printk("do_wp_page: bogus page at address %08lx (%08lx)\n",address,old_page);
  send_sig(SIGKILL, vma->vm_task, 1);
  goto end_wp_page;
bad_wp_pagemiddle:
  printk("do_wp_page: bogus page-middle at address %08lx (%08lx)\n", address, pmd_val(*page_middle));
  send_sig(SIGKILL, vma->vm_task, 1);
  goto end_wp_page;
bad_wp_pagedir:
  printk("do_wp_page: bogus page-dir entry at address %08lx (%08lx)\n", address, pgd_val(*page_dir));
  send_sig(SIGKILL, vma->vm_task, 1);
end_wp_page:
  if (new_page)
    free_page(new_page);
  return;
}

本文分享自微信公众号 - 编程杂技(theanarkh)

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2019-08-18

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券