root/mm/mmap.c

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

DEFINITIONS

This source file includes following definitions.
  1. anon_map
  2. do_mmap
  3. get_unmapped_area
  4. avl_neighbours
  5. avl_rebalance
  6. avl_insert
  7. avl_insert_neighbours
  8. avl_remove
  9. printk_list
  10. printk_avl
  11. avl_checkheights
  12. avl_checkleft
  13. avl_checkright
  14. avl_checkorder
  15. avl_check
  16. unmap_fixup
  17. sys_munmap
  18. do_munmap
  19. build_mmap_avl
  20. exit_mmap
  21. insert_vm_struct
  22. remove_shared_vm_struct
  23. merge_segments

   1 /*
   2  *      linux/mm/mmap.c
   3  *
   4  * Written by obz.
   5  */
   6 #include <linux/stat.h>
   7 #include <linux/sched.h>
   8 #include <linux/kernel.h>
   9 #include <linux/mm.h>
  10 #include <linux/shm.h>
  11 #include <linux/errno.h>
  12 #include <linux/mman.h>
  13 #include <linux/string.h>
  14 #include <linux/malloc.h>
  15 
  16 #include <asm/segment.h>
  17 #include <asm/system.h>
  18 #include <asm/pgtable.h>
  19 
  20 /*
  21  * Map memory not associated with any file into a process
  22  * address space.  Adjacent memory is merged.
  23  */
  24 static inline int anon_map(struct inode *ino, struct file * file, struct vm_area_struct * vma)
     /* [previous][next][first][last][top][bottom][index][help] */
  25 {
  26         if (zeromap_page_range(vma->vm_start, vma->vm_end - vma->vm_start, vma->vm_page_prot))
  27                 return -ENOMEM;
  28         return 0;
  29 }
  30 
  31 /*
  32  * description of effects of mapping type and prot in current implementation.
  33  * this is due to the limited x86 page protection hardware.  The expected
  34  * behavior is in parens:
  35  *
  36  * map_type     prot
  37  *              PROT_NONE       PROT_READ       PROT_WRITE      PROT_EXEC
  38  * MAP_SHARED   r: (no) no      r: (yes) yes    r: (no) yes     r: (no) yes
  39  *              w: (no) no      w: (no) no      w: (yes) yes    w: (no) no
  40  *              x: (no) no      x: (no) yes     x: (no) yes     x: (yes) yes
  41  *              
  42  * MAP_PRIVATE  r: (no) no      r: (yes) yes    r: (no) yes     r: (no) yes
  43  *              w: (no) no      w: (no) no      w: (copy) copy  w: (no) no
  44  *              x: (no) no      x: (no) yes     x: (no) yes     x: (yes) yes
  45  *
  46  */
  47 
  48 pgprot_t protection_map[16] = {
  49         __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
  50         __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
  51 };
  52 
  53 unsigned long do_mmap(struct file * file, unsigned long addr, unsigned long len,
     /* [previous][next][first][last][top][bottom][index][help] */
  54         unsigned long prot, unsigned long flags, unsigned long off)
  55 {
  56         int error;
  57         struct vm_area_struct * vma;
  58 
  59         if ((len = PAGE_ALIGN(len)) == 0)
  60                 return addr;
  61 
  62         if (addr > TASK_SIZE || len > TASK_SIZE || addr > TASK_SIZE-len)
  63                 return -EINVAL;
  64 
  65         /* offset overflow? */
  66         if (off + len < off)
  67                 return -EINVAL;
  68 
  69         /* mlock MCL_FUTURE? */
  70         if (current->mm->def_flags & VM_LOCKED) {
  71                 unsigned long locked = current->mm->locked_vm << PAGE_SHIFT;
  72                 locked += len;
  73                 if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
  74                         return -EAGAIN;
  75         }
  76 
  77         /*
  78          * do simple checking here so the lower-level routines won't have
  79          * to. we assume access permissions have been handled by the open
  80          * of the memory object, so we don't do any here.
  81          */
  82 
  83         if (file != NULL) {
  84                 switch (flags & MAP_TYPE) {
  85                 case MAP_SHARED:
  86                         if ((prot & PROT_WRITE) && !(file->f_mode & 2))
  87                                 return -EACCES;
  88                         /* fall through */
  89                 case MAP_PRIVATE:
  90                         if (!(file->f_mode & 1))
  91                                 return -EACCES;
  92                         break;
  93 
  94                 default:
  95                         return -EINVAL;
  96                 }
  97                 if (flags & MAP_DENYWRITE) {
  98                         if (file->f_inode->i_writecount > 0)
  99                                 return -ETXTBSY;
 100                 }
 101         } else if ((flags & MAP_TYPE) != MAP_PRIVATE)
 102                 return -EINVAL;
 103 
 104         /*
 105          * obtain the address to map to. we verify (or select) it and ensure
 106          * that it represents a valid section of the address space.
 107          */
 108 
 109         if (flags & MAP_FIXED) {
 110                 if (addr & ~PAGE_MASK)
 111                         return -EINVAL;
 112                 if (len > TASK_SIZE || addr > TASK_SIZE - len)
 113                         return -EINVAL;
 114         } else {
 115                 addr = get_unmapped_area(addr, len);
 116                 if (!addr)
 117                         return -ENOMEM;
 118         }
 119 
 120         /*
 121          * determine the object being mapped and call the appropriate
 122          * specific mapper. the address has already been validated, but
 123          * not unmapped, but the maps are removed from the list.
 124          */
 125         if (file && (!file->f_op || !file->f_op->mmap))
 126                 return -ENODEV;
 127 
 128         vma = (struct vm_area_struct *)kmalloc(sizeof(struct vm_area_struct),
 129                 GFP_KERNEL);
 130         if (!vma)
 131                 return -ENOMEM;
 132 
 133         vma->vm_mm = current->mm;
 134         vma->vm_start = addr;
 135         vma->vm_end = addr + len;
 136         vma->vm_flags = prot & (VM_READ | VM_WRITE | VM_EXEC);
 137         vma->vm_flags |= flags & (VM_GROWSDOWN | VM_DENYWRITE | VM_EXECUTABLE);
 138         vma->vm_flags |= current->mm->def_flags;
 139 
 140         if (file) {
 141                 if (file->f_mode & 1)
 142                         vma->vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
 143                 if (flags & MAP_SHARED) {
 144                         vma->vm_flags |= VM_SHARED | VM_MAYSHARE;
 145                         /*
 146                          * This looks strange, but when we don't have the file open
 147                          * for writing, we can demote the shared mapping to a simpler
 148                          * private mapping. That also takes care of a security hole
 149                          * with ptrace() writing to a shared mapping without write
 150                          * permissions.
 151                          *
 152                          * We leave the VM_MAYSHARE bit on, just to get correct output
 153                          * from /proc/xxx/maps..
 154                          */
 155                         if (!(file->f_mode & 2))
 156                                 vma->vm_flags &= ~(VM_MAYWRITE | VM_SHARED);
 157                 }
 158         } else
 159                 vma->vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
 160         vma->vm_page_prot = protection_map[vma->vm_flags & 0x0f];
 161         vma->vm_ops = NULL;
 162         vma->vm_offset = off;
 163         vma->vm_inode = NULL;
 164         vma->vm_pte = 0;
 165 
 166         do_munmap(addr, len);   /* Clear old maps */
 167 
 168         if (file)
 169                 error = file->f_op->mmap(file->f_inode, file, vma);
 170         else
 171                 error = anon_map(NULL, NULL, vma);
 172         
 173         if (error) {
 174                 kfree(vma);
 175                 return error;
 176         }
 177         flags = vma->vm_flags;
 178         insert_vm_struct(current, vma);
 179         merge_segments(current, vma->vm_start, vma->vm_end);
 180 
 181         /* merge_segments might have merged our vma, so we can't use it any more */
 182         current->mm->total_vm += len >> PAGE_SHIFT;
 183         if (flags & VM_LOCKED) {
 184                 unsigned long start = addr;
 185                 current->mm->locked_vm += len >> PAGE_SHIFT;
 186                 do {
 187                         char c = get_user((char *) start);
 188                         len -= PAGE_SIZE;
 189                         start += PAGE_SIZE;
 190                         __asm__ __volatile__("": :"r" (c));
 191                 } while (len > 0);
 192         }
 193         return addr;
 194 }
 195 
 196 /*
 197  * Get an address range which is currently unmapped.
 198  * For mmap() without MAP_FIXED and shmat() with addr=0.
 199  * Return value 0 means ENOMEM.
 200  */
 201 unsigned long get_unmapped_area(unsigned long addr, unsigned long len)
     /* [previous][next][first][last][top][bottom][index][help] */
 202 {
 203         struct vm_area_struct * vmm;
 204 
 205         if (len > TASK_SIZE)
 206                 return 0;
 207         if (!addr)
 208                 addr = TASK_SIZE / 3;
 209         addr = PAGE_ALIGN(addr);
 210 
 211         for (vmm = find_vma(current, addr); ; vmm = vmm->vm_next) {
 212                 /* At this point:  (!vmm || addr < vmm->vm_end). */
 213                 if (TASK_SIZE - len < addr)
 214                         return 0;
 215                 if (!vmm || addr + len <= vmm->vm_start)
 216                         return addr;
 217                 addr = vmm->vm_end;
 218         }
 219 }
 220 
 221 /*
 222  * Searching a VMA in the linear list task->mm->mmap is horribly slow.
 223  * Use an AVL (Adelson-Velskii and Landis) tree to speed up this search
 224  * from O(n) to O(log n), where n is the number of VMAs of the task
 225  * (typically around 6, but may reach 3000 in some cases).
 226  * Written by Bruno Haible <haible@ma2s2.mathematik.uni-karlsruhe.de>.
 227  */
 228 
 229 /* We keep the list and tree sorted by address. */
 230 #define vm_avl_key      vm_end
 231 #define vm_avl_key_t    unsigned long   /* typeof(vma->avl_key) */
 232 
 233 /*
 234  * task->mm->mmap_avl is the AVL tree corresponding to task->mm->mmap
 235  * or, more exactly, its root.
 236  * A vm_area_struct has the following fields:
 237  *   vm_avl_left     left son of a tree node
 238  *   vm_avl_right    right son of a tree node
 239  *   vm_avl_height   1+max(heightof(left),heightof(right))
 240  * The empty tree is represented as NULL.
 241  */
 242 
 243 /* Since the trees are balanced, their height will never be large. */
 244 #define avl_maxheight   41      /* why this? a small exercise */
 245 #define heightof(tree)  ((tree) == avl_empty ? 0 : (tree)->vm_avl_height)
 246 /*
 247  * Consistency and balancing rules:
 248  * 1. tree->vm_avl_height == 1+max(heightof(tree->vm_avl_left),heightof(tree->vm_avl_right))
 249  * 2. abs( heightof(tree->vm_avl_left) - heightof(tree->vm_avl_right) ) <= 1
 250  * 3. foreach node in tree->vm_avl_left: node->vm_avl_key <= tree->vm_avl_key,
 251  *    foreach node in tree->vm_avl_right: node->vm_avl_key >= tree->vm_avl_key.
 252  */
 253 
 254 /* Look up the nodes at the left and at the right of a given node. */
 255 static inline void avl_neighbours (struct vm_area_struct * node, struct vm_area_struct * tree, struct vm_area_struct ** to_the_left, struct vm_area_struct ** to_the_right)
     /* [previous][next][first][last][top][bottom][index][help] */
 256 {
 257         vm_avl_key_t key = node->vm_avl_key;
 258 
 259         *to_the_left = *to_the_right = NULL;
 260         for (;;) {
 261                 if (tree == avl_empty) {
 262                         printk("avl_neighbours: node not found in the tree\n");
 263                         return;
 264                 }
 265                 if (key == tree->vm_avl_key)
 266                         break;
 267                 if (key < tree->vm_avl_key) {
 268                         *to_the_right = tree;
 269                         tree = tree->vm_avl_left;
 270                 } else {
 271                         *to_the_left = tree;
 272                         tree = tree->vm_avl_right;
 273                 }
 274         }
 275         if (tree != node) {
 276                 printk("avl_neighbours: node not exactly found in the tree\n");
 277                 return;
 278         }
 279         if (tree->vm_avl_left != avl_empty) {
 280                 struct vm_area_struct * node;
 281                 for (node = tree->vm_avl_left; node->vm_avl_right != avl_empty; node = node->vm_avl_right)
 282                         continue;
 283                 *to_the_left = node;
 284         }
 285         if (tree->vm_avl_right != avl_empty) {
 286                 struct vm_area_struct * node;
 287                 for (node = tree->vm_avl_right; node->vm_avl_left != avl_empty; node = node->vm_avl_left)
 288                         continue;
 289                 *to_the_right = node;
 290         }
 291         if ((*to_the_left && ((*to_the_left)->vm_next != node)) || (node->vm_next != *to_the_right))
 292                 printk("avl_neighbours: tree inconsistent with list\n");
 293 }
 294 
 295 /*
 296  * Rebalance a tree.
 297  * After inserting or deleting a node of a tree we have a sequence of subtrees
 298  * nodes[0]..nodes[k-1] such that
 299  * nodes[0] is the root and nodes[i+1] = nodes[i]->{vm_avl_left|vm_avl_right}.
 300  */
 301 static inline void avl_rebalance (struct vm_area_struct *** nodeplaces_ptr, int count)
     /* [previous][next][first][last][top][bottom][index][help] */
 302 {
 303         for ( ; count > 0 ; count--) {
 304                 struct vm_area_struct ** nodeplace = *--nodeplaces_ptr;
 305                 struct vm_area_struct * node = *nodeplace;
 306                 struct vm_area_struct * nodeleft = node->vm_avl_left;
 307                 struct vm_area_struct * noderight = node->vm_avl_right;
 308                 int heightleft = heightof(nodeleft);
 309                 int heightright = heightof(noderight);
 310                 if (heightright + 1 < heightleft) {
 311                         /*                                                      */
 312                         /*                            *                         */
 313                         /*                          /   \                       */
 314                         /*                       n+2      n                     */
 315                         /*                                                      */
 316                         struct vm_area_struct * nodeleftleft = nodeleft->vm_avl_left;
 317                         struct vm_area_struct * nodeleftright = nodeleft->vm_avl_right;
 318                         int heightleftright = heightof(nodeleftright);
 319                         if (heightof(nodeleftleft) >= heightleftright) {
 320                                 /*                                                        */
 321                                 /*                *                    n+2|n+3            */
 322                                 /*              /   \                  /    \             */
 323                                 /*           n+2      n      -->      /   n+1|n+2         */
 324                                 /*           / \                      |    /    \         */
 325                                 /*         n+1 n|n+1                 n+1  n|n+1  n        */
 326                                 /*                                                        */
 327                                 node->vm_avl_left = nodeleftright; nodeleft->vm_avl_right = node;
 328                                 nodeleft->vm_avl_height = 1 + (node->vm_avl_height = 1 + heightleftright);
 329                                 *nodeplace = nodeleft;
 330                         } else {
 331                                 /*                                                        */
 332                                 /*                *                     n+2               */
 333                                 /*              /   \                 /     \             */
 334                                 /*           n+2      n      -->    n+1     n+1           */
 335                                 /*           / \                    / \     / \           */
 336                                 /*          n  n+1                 n   L   R   n          */
 337                                 /*             / \                                        */
 338                                 /*            L   R                                       */
 339                                 /*                                                        */
 340                                 nodeleft->vm_avl_right = nodeleftright->vm_avl_left;
 341                                 node->vm_avl_left = nodeleftright->vm_avl_right;
 342                                 nodeleftright->vm_avl_left = nodeleft;
 343                                 nodeleftright->vm_avl_right = node;
 344                                 nodeleft->vm_avl_height = node->vm_avl_height = heightleftright;
 345                                 nodeleftright->vm_avl_height = heightleft;
 346                                 *nodeplace = nodeleftright;
 347                         }
 348                 }
 349                 else if (heightleft + 1 < heightright) {
 350                         /* similar to the above, just interchange 'left' <--> 'right' */
 351                         struct vm_area_struct * noderightright = noderight->vm_avl_right;
 352                         struct vm_area_struct * noderightleft = noderight->vm_avl_left;
 353                         int heightrightleft = heightof(noderightleft);
 354                         if (heightof(noderightright) >= heightrightleft) {
 355                                 node->vm_avl_right = noderightleft; noderight->vm_avl_left = node;
 356                                 noderight->vm_avl_height = 1 + (node->vm_avl_height = 1 + heightrightleft);
 357                                 *nodeplace = noderight;
 358                         } else {
 359                                 noderight->vm_avl_left = noderightleft->vm_avl_right;
 360                                 node->vm_avl_right = noderightleft->vm_avl_left;
 361                                 noderightleft->vm_avl_right = noderight;
 362                                 noderightleft->vm_avl_left = node;
 363                                 noderight->vm_avl_height = node->vm_avl_height = heightrightleft;
 364                                 noderightleft->vm_avl_height = heightright;
 365                                 *nodeplace = noderightleft;
 366                         }
 367                 }
 368                 else {
 369                         int height = (heightleft<heightright ? heightright : heightleft) + 1;
 370                         if (height == node->vm_avl_height)
 371                                 break;
 372                         node->vm_avl_height = height;
 373                 }
 374         }
 375 }
 376 
 377 /* Insert a node into a tree. */
 378 static inline void avl_insert (struct vm_area_struct * new_node, struct vm_area_struct ** ptree)
     /* [previous][next][first][last][top][bottom][index][help] */
 379 {
 380         vm_avl_key_t key = new_node->vm_avl_key;
 381         struct vm_area_struct ** nodeplace = ptree;
 382         struct vm_area_struct ** stack[avl_maxheight];
 383         int stack_count = 0;
 384         struct vm_area_struct *** stack_ptr = &stack[0]; /* = &stack[stackcount] */
 385         for (;;) {
 386                 struct vm_area_struct * node = *nodeplace;
 387                 if (node == avl_empty)
 388                         break;
 389                 *stack_ptr++ = nodeplace; stack_count++;
 390                 if (key < node->vm_avl_key)
 391                         nodeplace = &node->vm_avl_left;
 392                 else
 393                         nodeplace = &node->vm_avl_right;
 394         }
 395         new_node->vm_avl_left = avl_empty;
 396         new_node->vm_avl_right = avl_empty;
 397         new_node->vm_avl_height = 1;
 398         *nodeplace = new_node;
 399         avl_rebalance(stack_ptr,stack_count);
 400 }
 401 
 402 /* Insert a node into a tree, and
 403  * return the node to the left of it and the node to the right of it.
 404  */
 405 static inline void avl_insert_neighbours (struct vm_area_struct * new_node, struct vm_area_struct ** ptree,
     /* [previous][next][first][last][top][bottom][index][help] */
 406         struct vm_area_struct ** to_the_left, struct vm_area_struct ** to_the_right)
 407 {
 408         vm_avl_key_t key = new_node->vm_avl_key;
 409         struct vm_area_struct ** nodeplace = ptree;
 410         struct vm_area_struct ** stack[avl_maxheight];
 411         int stack_count = 0;
 412         struct vm_area_struct *** stack_ptr = &stack[0]; /* = &stack[stackcount] */
 413         *to_the_left = *to_the_right = NULL;
 414         for (;;) {
 415                 struct vm_area_struct * node = *nodeplace;
 416                 if (node == avl_empty)
 417                         break;
 418                 *stack_ptr++ = nodeplace; stack_count++;
 419                 if (key < node->vm_avl_key) {
 420                         *to_the_right = node;
 421                         nodeplace = &node->vm_avl_left;
 422                 } else {
 423                         *to_the_left = node;
 424                         nodeplace = &node->vm_avl_right;
 425                 }
 426         }
 427         new_node->vm_avl_left = avl_empty;
 428         new_node->vm_avl_right = avl_empty;
 429         new_node->vm_avl_height = 1;
 430         *nodeplace = new_node;
 431         avl_rebalance(stack_ptr,stack_count);
 432 }
 433 
 434 /* Removes a node out of a tree. */
 435 static inline void avl_remove (struct vm_area_struct * node_to_delete, struct vm_area_struct ** ptree)
     /* [previous][next][first][last][top][bottom][index][help] */
 436 {
 437         vm_avl_key_t key = node_to_delete->vm_avl_key;
 438         struct vm_area_struct ** nodeplace = ptree;
 439         struct vm_area_struct ** stack[avl_maxheight];
 440         int stack_count = 0;
 441         struct vm_area_struct *** stack_ptr = &stack[0]; /* = &stack[stackcount] */
 442         struct vm_area_struct ** nodeplace_to_delete;
 443         for (;;) {
 444                 struct vm_area_struct * node = *nodeplace;
 445                 if (node == avl_empty) {
 446                         /* what? node_to_delete not found in tree? */
 447                         printk("avl_remove: node to delete not found in tree\n");
 448                         return;
 449                 }
 450                 *stack_ptr++ = nodeplace; stack_count++;
 451                 if (key == node->vm_avl_key)
 452                         break;
 453                 if (key < node->vm_avl_key)
 454                         nodeplace = &node->vm_avl_left;
 455                 else
 456                         nodeplace = &node->vm_avl_right;
 457         }
 458         nodeplace_to_delete = nodeplace;
 459         /* Have to remove node_to_delete = *nodeplace_to_delete. */
 460         if (node_to_delete->vm_avl_left == avl_empty) {
 461                 *nodeplace_to_delete = node_to_delete->vm_avl_right;
 462                 stack_ptr--; stack_count--;
 463         } else {
 464                 struct vm_area_struct *** stack_ptr_to_delete = stack_ptr;
 465                 struct vm_area_struct ** nodeplace = &node_to_delete->vm_avl_left;
 466                 struct vm_area_struct * node;
 467                 for (;;) {
 468                         node = *nodeplace;
 469                         if (node->vm_avl_right == avl_empty)
 470                                 break;
 471                         *stack_ptr++ = nodeplace; stack_count++;
 472                         nodeplace = &node->vm_avl_right;
 473                 }
 474                 *nodeplace = node->vm_avl_left;
 475                 /* node replaces node_to_delete */
 476                 node->vm_avl_left = node_to_delete->vm_avl_left;
 477                 node->vm_avl_right = node_to_delete->vm_avl_right;
 478                 node->vm_avl_height = node_to_delete->vm_avl_height;
 479                 *nodeplace_to_delete = node; /* replace node_to_delete */
 480                 *stack_ptr_to_delete = &node->vm_avl_left; /* replace &node_to_delete->vm_avl_left */
 481         }
 482         avl_rebalance(stack_ptr,stack_count);
 483 }
 484 
 485 #ifdef DEBUG_AVL
 486 
 487 /* print a list */
 488 static void printk_list (struct vm_area_struct * vma)
     /* [previous][next][first][last][top][bottom][index][help] */
 489 {
 490         printk("[");
 491         while (vma) {
 492                 printk("%08lX-%08lX", vma->vm_start, vma->vm_end);
 493                 vma = vma->vm_next;
 494                 if (!vma)
 495                         break;
 496                 printk(" ");
 497         }
 498         printk("]");
 499 }
 500 
 501 /* print a tree */
 502 static void printk_avl (struct vm_area_struct * tree)
     /* [previous][next][first][last][top][bottom][index][help] */
 503 {
 504         if (tree != avl_empty) {
 505                 printk("(");
 506                 if (tree->vm_avl_left != avl_empty) {
 507                         printk_avl(tree->vm_avl_left);
 508                         printk("<");
 509                 }
 510                 printk("%08lX-%08lX", tree->vm_start, tree->vm_end);
 511                 if (tree->vm_avl_right != avl_empty) {
 512                         printk(">");
 513                         printk_avl(tree->vm_avl_right);
 514                 }
 515                 printk(")");
 516         }
 517 }
 518 
 519 static char *avl_check_point = "somewhere";
 520 
 521 /* check a tree's consistency and balancing */
 522 static void avl_checkheights (struct vm_area_struct * tree)
     /* [previous][next][first][last][top][bottom][index][help] */
 523 {
 524         int h, hl, hr;
 525 
 526         if (tree == avl_empty)
 527                 return;
 528         avl_checkheights(tree->vm_avl_left);
 529         avl_checkheights(tree->vm_avl_right);
 530         h = tree->vm_avl_height;
 531         hl = heightof(tree->vm_avl_left);
 532         hr = heightof(tree->vm_avl_right);
 533         if ((h == hl+1) && (hr <= hl) && (hl <= hr+1))
 534                 return;
 535         if ((h == hr+1) && (hl <= hr) && (hr <= hl+1))
 536                 return;
 537         printk("%s: avl_checkheights: heights inconsistent\n",avl_check_point);
 538 }
 539 
 540 /* check that all values stored in a tree are < key */
 541 static void avl_checkleft (struct vm_area_struct * tree, vm_avl_key_t key)
     /* [previous][next][first][last][top][bottom][index][help] */
 542 {
 543         if (tree == avl_empty)
 544                 return;
 545         avl_checkleft(tree->vm_avl_left,key);
 546         avl_checkleft(tree->vm_avl_right,key);
 547         if (tree->vm_avl_key < key)
 548                 return;
 549         printk("%s: avl_checkleft: left key %lu >= top key %lu\n",avl_check_point,tree->vm_avl_key,key);
 550 }
 551 
 552 /* check that all values stored in a tree are > key */
 553 static void avl_checkright (struct vm_area_struct * tree, vm_avl_key_t key)
     /* [previous][next][first][last][top][bottom][index][help] */
 554 {
 555         if (tree == avl_empty)
 556                 return;
 557         avl_checkright(tree->vm_avl_left,key);
 558         avl_checkright(tree->vm_avl_right,key);
 559         if (tree->vm_avl_key > key)
 560                 return;
 561         printk("%s: avl_checkright: right key %lu <= top key %lu\n",avl_check_point,tree->vm_avl_key,key);
 562 }
 563 
 564 /* check that all values are properly increasing */
 565 static void avl_checkorder (struct vm_area_struct * tree)
     /* [previous][next][first][last][top][bottom][index][help] */
 566 {
 567         if (tree == avl_empty)
 568                 return;
 569         avl_checkorder(tree->vm_avl_left);
 570         avl_checkorder(tree->vm_avl_right);
 571         avl_checkleft(tree->vm_avl_left,tree->vm_avl_key);
 572         avl_checkright(tree->vm_avl_right,tree->vm_avl_key);
 573 }
 574 
 575 /* all checks */
 576 static void avl_check (struct task_struct * task, char *caller)
     /* [previous][next][first][last][top][bottom][index][help] */
 577 {
 578         avl_check_point = caller;
 579 /*      printk("task \"%s\", %s\n",task->comm,caller); */
 580 /*      printk("task \"%s\" list: ",task->comm); printk_list(task->mm->mmap); printk("\n"); */
 581 /*      printk("task \"%s\" tree: ",task->comm); printk_avl(task->mm->mmap_avl); printk("\n"); */
 582         avl_checkheights(task->mm->mmap_avl);
 583         avl_checkorder(task->mm->mmap_avl);
 584 }
 585 
 586 #endif
 587 
 588 
 589 /*
 590  * Normal function to fix up a mapping
 591  * This function is the default for when an area has no specific
 592  * function.  This may be used as part of a more specific routine.
 593  * This function works out what part of an area is affected and
 594  * adjusts the mapping information.  Since the actual page
 595  * manipulation is done in do_mmap(), none need be done here,
 596  * though it would probably be more appropriate.
 597  *
 598  * By the time this function is called, the area struct has been
 599  * removed from the process mapping list, so it needs to be
 600  * reinserted if necessary.
 601  *
 602  * The 4 main cases are:
 603  *    Unmapping the whole area
 604  *    Unmapping from the start of the segment to a point in it
 605  *    Unmapping from an intermediate point to the end
 606  *    Unmapping between to intermediate points, making a hole.
 607  *
 608  * Case 4 involves the creation of 2 new areas, for each side of
 609  * the hole.
 610  */
 611 static void unmap_fixup(struct vm_area_struct *area,
     /* [previous][next][first][last][top][bottom][index][help] */
 612                  unsigned long addr, size_t len)
 613 {
 614         struct vm_area_struct *mpnt;
 615         unsigned long end = addr + len;
 616 
 617         if (addr < area->vm_start || addr >= area->vm_end ||
 618             end <= area->vm_start || end > area->vm_end ||
 619             end < addr)
 620         {
 621                 printk("unmap_fixup: area=%lx-%lx, unmap %lx-%lx!!\n",
 622                        area->vm_start, area->vm_end, addr, end);
 623                 return;
 624         }
 625         area->vm_mm->total_vm -= len >> PAGE_SHIFT;
 626         if (area->vm_flags & VM_LOCKED)
 627                 area->vm_mm->locked_vm -= len >> PAGE_SHIFT;
 628 
 629         /* Unmapping the whole area */
 630         if (addr == area->vm_start && end == area->vm_end) {
 631                 if (area->vm_ops && area->vm_ops->close)
 632                         area->vm_ops->close(area);
 633                 if (area->vm_inode)
 634                         iput(area->vm_inode);
 635                 return;
 636         }
 637 
 638         /* Work out to one of the ends */
 639         if (end == area->vm_end)
 640                 area->vm_end = addr;
 641         else
 642         if (addr == area->vm_start) {
 643                 area->vm_offset += (end - area->vm_start);
 644                 area->vm_start = end;
 645         }
 646         else {
 647         /* Unmapping a hole: area->vm_start < addr <= end < area->vm_end */
 648                 /* Add end mapping -- leave beginning for below */
 649                 mpnt = (struct vm_area_struct *)kmalloc(sizeof(*mpnt), GFP_KERNEL);
 650 
 651                 if (!mpnt)
 652                         return;
 653                 *mpnt = *area;
 654                 mpnt->vm_offset += (end - area->vm_start);
 655                 mpnt->vm_start = end;
 656                 if (mpnt->vm_inode)
 657                         mpnt->vm_inode->i_count++;
 658                 if (mpnt->vm_ops && mpnt->vm_ops->open)
 659                         mpnt->vm_ops->open(mpnt);
 660                 area->vm_end = addr;    /* Truncate area */
 661                 insert_vm_struct(current, mpnt);
 662         }
 663 
 664         /* construct whatever mapping is needed */
 665         mpnt = (struct vm_area_struct *)kmalloc(sizeof(*mpnt), GFP_KERNEL);
 666         if (!mpnt)
 667                 return;
 668         *mpnt = *area;
 669         if (mpnt->vm_ops && mpnt->vm_ops->open)
 670                 mpnt->vm_ops->open(mpnt);
 671         if (area->vm_ops && area->vm_ops->close) {
 672                 area->vm_end = area->vm_start;
 673                 area->vm_ops->close(area);
 674         }
 675         insert_vm_struct(current, mpnt);
 676 }
 677 
 678 asmlinkage int sys_munmap(unsigned long addr, size_t len)
     /* [previous][next][first][last][top][bottom][index][help] */
 679 {
 680         return do_munmap(addr, len);
 681 }
 682 
 683 /*
 684  * Munmap is split into 2 main parts -- this part which finds
 685  * what needs doing, and the areas themselves, which do the
 686  * work.  This now handles partial unmappings.
 687  * Jeremy Fitzhardine <jeremy@sw.oz.au>
 688  */
 689 int do_munmap(unsigned long addr, size_t len)
     /* [previous][next][first][last][top][bottom][index][help] */
 690 {
 691         struct vm_area_struct *mpnt, *prev, *next, **npp, *free;
 692 
 693         if ((addr & ~PAGE_MASK) || addr > TASK_SIZE || len > TASK_SIZE-addr)
 694                 return -EINVAL;
 695 
 696         if ((len = PAGE_ALIGN(len)) == 0)
 697                 return 0;
 698 
 699         /*
 700          * Check if this memory area is ok - put it on the temporary
 701          * list if so..  The checks here are pretty simple --
 702          * every area affected in some way (by any overlap) is put
 703          * on the list.  If nothing is put on, nothing is affected.
 704          */
 705         mpnt = find_vma(current, addr);
 706         if (!mpnt)
 707                 return 0;
 708         avl_neighbours(mpnt, current->mm->mmap_avl, &prev, &next);
 709         /* we have  prev->vm_next == mpnt && mpnt->vm_next = next */
 710         /* and  addr < mpnt->vm_end  */
 711 
 712         npp = (prev ? &prev->vm_next : &current->mm->mmap);
 713         free = NULL;
 714         for ( ; mpnt && mpnt->vm_start < addr+len; mpnt = *npp) {
 715                 *npp = mpnt->vm_next;
 716                 mpnt->vm_next = free;
 717                 free = mpnt;
 718                 avl_remove(mpnt, &current->mm->mmap_avl);
 719         }
 720 
 721         if (free == NULL)
 722                 return 0;
 723 
 724         /*
 725          * Ok - we have the memory areas we should free on the 'free' list,
 726          * so release them, and unmap the page range..
 727          * If the one of the segments is only being partially unmapped,
 728          * it will put new vm_area_struct(s) into the address space.
 729          */
 730         while (free) {
 731                 unsigned long st, end;
 732 
 733                 mpnt = free;
 734                 free = free->vm_next;
 735 
 736                 remove_shared_vm_struct(mpnt);
 737 
 738                 st = addr < mpnt->vm_start ? mpnt->vm_start : addr;
 739                 end = addr+len;
 740                 end = end > mpnt->vm_end ? mpnt->vm_end : end;
 741 
 742                 if (mpnt->vm_ops && mpnt->vm_ops->unmap)
 743                         mpnt->vm_ops->unmap(mpnt, st, end-st);
 744                 zap_page_range(current->mm, st, end-st);
 745                 unmap_fixup(mpnt, st, end-st);
 746                 kfree(mpnt);
 747         }
 748 
 749         zap_page_range(current->mm, addr, len);
 750         return 0;
 751 }
 752 
 753 /* Build the AVL tree corresponding to the VMA list. */
 754 void build_mmap_avl(struct mm_struct * mm)
     /* [previous][next][first][last][top][bottom][index][help] */
 755 {
 756         struct vm_area_struct * vma;
 757 
 758         mm->mmap_avl = NULL;
 759         for (vma = mm->mmap; vma; vma = vma->vm_next)
 760                 avl_insert(vma, &mm->mmap_avl);
 761 }
 762 
 763 /* Release all mmaps. */
 764 void exit_mmap(struct mm_struct * mm)
     /* [previous][next][first][last][top][bottom][index][help] */
 765 {
 766         struct vm_area_struct * mpnt;
 767 
 768         mpnt = mm->mmap;
 769         mm->mmap = NULL;
 770         mm->mmap_avl = NULL;
 771         mm->rss = 0;
 772         mm->total_vm = 0;
 773         mm->locked_vm = 0;
 774         while (mpnt) {
 775                 struct vm_area_struct * next = mpnt->vm_next;
 776                 if (mpnt->vm_ops) {
 777                         if (mpnt->vm_ops->unmap)
 778                                 mpnt->vm_ops->unmap(mpnt, mpnt->vm_start, mpnt->vm_end-mpnt->vm_start);
 779                         if (mpnt->vm_ops->close)
 780                                 mpnt->vm_ops->close(mpnt);
 781                 }
 782                 remove_shared_vm_struct(mpnt);
 783                 zap_page_range(mm, mpnt->vm_start, mpnt->vm_end-mpnt->vm_start);
 784                 if (mpnt->vm_inode)
 785                         iput(mpnt->vm_inode);
 786                 kfree(mpnt);
 787                 mpnt = next;
 788         }
 789 }
 790 
 791 /*
 792  * Insert vm structure into process list sorted by address
 793  * and into the inode's i_mmap ring.
 794  */
 795 void insert_vm_struct(struct task_struct *t, struct vm_area_struct *vmp)
     /* [previous][next][first][last][top][bottom][index][help] */
 796 {
 797         struct vm_area_struct *share;
 798         struct inode * inode;
 799 
 800 #if 0 /* equivalent, but slow */
 801         struct vm_area_struct **p, *mpnt;
 802 
 803         p = &t->mm->mmap;
 804         while ((mpnt = *p) != NULL) {
 805                 if (mpnt->vm_start > vmp->vm_start)
 806                         break;
 807                 if (mpnt->vm_end > vmp->vm_start)
 808                         printk("insert_vm_struct: overlapping memory areas\n");
 809                 p = &mpnt->vm_next;
 810         }
 811         vmp->vm_next = mpnt;
 812         *p = vmp;
 813 #else
 814         struct vm_area_struct * prev, * next;
 815 
 816         avl_insert_neighbours(vmp, &t->mm->mmap_avl, &prev, &next);
 817         if ((prev ? prev->vm_next : t->mm->mmap) != next)
 818                 printk("insert_vm_struct: tree inconsistent with list\n");
 819         if (prev)
 820                 prev->vm_next = vmp;
 821         else
 822                 t->mm->mmap = vmp;
 823         vmp->vm_next = next;
 824 #endif
 825 
 826         inode = vmp->vm_inode;
 827         if (!inode)
 828                 return;
 829 
 830         /* insert vmp into inode's circular share list */
 831         if ((share = inode->i_mmap)) {
 832                 vmp->vm_next_share = share->vm_next_share;
 833                 vmp->vm_next_share->vm_prev_share = vmp;
 834                 share->vm_next_share = vmp;
 835                 vmp->vm_prev_share = share;
 836         } else
 837                 inode->i_mmap = vmp->vm_next_share = vmp->vm_prev_share = vmp;
 838 }
 839 
 840 /*
 841  * Remove one vm structure from the inode's i_mmap ring.
 842  */
 843 void remove_shared_vm_struct(struct vm_area_struct *mpnt)
     /* [previous][next][first][last][top][bottom][index][help] */
 844 {
 845         struct inode * inode = mpnt->vm_inode;
 846 
 847         if (!inode)
 848                 return;
 849 
 850         if (mpnt->vm_next_share == mpnt) {
 851                 if (inode->i_mmap != mpnt)
 852                         printk("Inode i_mmap ring corrupted\n");
 853                 inode->i_mmap = NULL;
 854                 return;
 855         }
 856 
 857         if (inode->i_mmap == mpnt)
 858                 inode->i_mmap = mpnt->vm_next_share;
 859 
 860         mpnt->vm_prev_share->vm_next_share = mpnt->vm_next_share;
 861         mpnt->vm_next_share->vm_prev_share = mpnt->vm_prev_share;
 862 }
 863 
 864 /*
 865  * Merge the list of memory segments if possible.
 866  * Redundant vm_area_structs are freed.
 867  * This assumes that the list is ordered by address.
 868  * We don't need to traverse the entire list, only those segments
 869  * which intersect or are adjacent to a given interval.
 870  */
 871 void merge_segments (struct task_struct * task, unsigned long start_addr, unsigned long end_addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 872 {
 873         struct vm_area_struct *prev, *mpnt, *next;
 874 
 875         mpnt = find_vma(task, start_addr);
 876         if (!mpnt)
 877                 return;
 878         avl_neighbours(mpnt, task->mm->mmap_avl, &prev, &next);
 879         /* we have  prev->vm_next == mpnt && mpnt->vm_next = next */
 880 
 881         if (!prev) {
 882                 prev = mpnt;
 883                 mpnt = next;
 884         }
 885 
 886         /* prev and mpnt cycle through the list, as long as
 887          * start_addr < mpnt->vm_end && prev->vm_start < end_addr
 888          */
 889         for ( ; mpnt && prev->vm_start < end_addr ; prev = mpnt, mpnt = next) {
 890 #if 0
 891                 printk("looping in merge_segments, mpnt=0x%lX\n", (unsigned long) mpnt);
 892 #endif
 893 
 894                 next = mpnt->vm_next;
 895 
 896                 /*
 897                  * To share, we must have the same inode, operations.. 
 898                  */
 899                 if (mpnt->vm_inode != prev->vm_inode)
 900                         continue;
 901                 if (mpnt->vm_pte != prev->vm_pte)
 902                         continue;
 903                 if (mpnt->vm_ops != prev->vm_ops)
 904                         continue;
 905                 if (mpnt->vm_flags != prev->vm_flags)
 906                         continue;
 907                 if (prev->vm_end != mpnt->vm_start)
 908                         continue;
 909                 /*
 910                  * and if we have an inode, the offsets must be contiguous..
 911                  */
 912                 if ((mpnt->vm_inode != NULL) || (mpnt->vm_flags & VM_SHM)) {
 913                         if (prev->vm_offset + prev->vm_end - prev->vm_start != mpnt->vm_offset)
 914                                 continue;
 915                 }
 916 
 917                 /*
 918                  * merge prev with mpnt and set up pointers so the new
 919                  * big segment can possibly merge with the next one.
 920                  * The old unused mpnt is freed.
 921                  */
 922                 avl_remove(mpnt, &task->mm->mmap_avl);
 923                 prev->vm_end = mpnt->vm_end;
 924                 prev->vm_next = mpnt->vm_next;
 925                 if (mpnt->vm_ops && mpnt->vm_ops->close) {
 926                         mpnt->vm_offset += mpnt->vm_end - mpnt->vm_start;
 927                         mpnt->vm_start = mpnt->vm_end;
 928                         mpnt->vm_ops->close(mpnt);
 929                 }
 930                 remove_shared_vm_struct(mpnt);
 931                 if (mpnt->vm_inode)
 932                         mpnt->vm_inode->i_count--;
 933                 kfree_s(mpnt, sizeof(*mpnt));
 934                 mpnt = prev;
 935         }
 936 }

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