This source file includes following definitions.
- anon_map
- do_mmap
- get_unmapped_area
- avl_neighbours
- avl_rebalance
- avl_insert
- avl_insert_neighbours
- avl_remove
- printk_list
- printk_avl
- avl_checkheights
- avl_checkleft
- avl_checkright
- avl_checkorder
- avl_check
- unmap_fixup
- sys_munmap
- do_munmap
- build_mmap_avl
- exit_mmap
- insert_vm_struct
- remove_shared_vm_struct
- merge_segments
1
2
3
4
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
22
23
24 static inline int anon_map(struct inode *ino, struct file * file, struct vm_area_struct * vma)
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
33
34
35
36
37
38
39
40
41
42
43
44
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,
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
66 if (off + len < off)
67 return -EINVAL;
68
69
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
79
80
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
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
106
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
122
123
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
147
148
149
150
151
152
153
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);
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
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
198
199
200
201 unsigned long get_unmapped_area(unsigned long addr, unsigned long len)
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
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
223
224
225
226
227
228
229
230 #define vm_avl_key vm_end
231 #define vm_avl_key_t unsigned long
232
233
234
235
236
237
238
239
240
241
242
243
244 #define avl_maxheight 41
245 #define heightof(tree) ((tree) == avl_empty ? 0 : (tree)->vm_avl_height)
246
247
248
249
250
251
252
253
254
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)
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
297
298
299
300
301 static inline void avl_rebalance (struct vm_area_struct *** nodeplaces_ptr, int count)
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
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
322
323
324
325
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
333
334
335
336
337
338
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
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
378 static inline void avl_insert (struct vm_area_struct * new_node, struct vm_area_struct ** ptree)
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];
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
403
404
405 static inline void avl_insert_neighbours (struct vm_area_struct * new_node, struct vm_area_struct ** ptree,
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];
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
435 static inline void avl_remove (struct vm_area_struct * node_to_delete, struct vm_area_struct ** ptree)
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];
442 struct vm_area_struct ** nodeplace_to_delete;
443 for (;;) {
444 struct vm_area_struct * node = *nodeplace;
445 if (node == avl_empty) {
446
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
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
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;
480 *stack_ptr_to_delete = &node->vm_avl_left;
481 }
482 avl_rebalance(stack_ptr,stack_count);
483 }
484
485 #ifdef DEBUG_AVL
486
487
488 static void printk_list (struct vm_area_struct * vma)
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
502 static void printk_avl (struct vm_area_struct * tree)
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
522 static void avl_checkheights (struct vm_area_struct * tree)
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
541 static void avl_checkleft (struct vm_area_struct * tree, vm_avl_key_t key)
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
553 static void avl_checkright (struct vm_area_struct * tree, vm_avl_key_t key)
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
565 static void avl_checkorder (struct vm_area_struct * tree)
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
576 static void avl_check (struct task_struct * task, char *caller)
577 {
578 avl_check_point = caller;
579
580
581
582 avl_checkheights(task->mm->mmap_avl);
583 avl_checkorder(task->mm->mmap_avl);
584 }
585
586 #endif
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611 static void unmap_fixup(struct vm_area_struct *area,
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
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
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
648
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;
661 insert_vm_struct(current, mpnt);
662 }
663
664
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)
679 {
680 return do_munmap(addr, len);
681 }
682
683
684
685
686
687
688
689 int do_munmap(unsigned long addr, size_t len)
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
701
702
703
704
705 mpnt = find_vma(current, addr);
706 if (!mpnt)
707 return 0;
708 avl_neighbours(mpnt, current->mm->mmap_avl, &prev, &next);
709
710
711
712 npp = (prev ? &prev->vm_next : ¤t->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, ¤t->mm->mmap_avl);
719 }
720
721 if (free == NULL)
722 return 0;
723
724
725
726
727
728
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
754 void build_mmap_avl(struct mm_struct * mm)
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
764 void exit_mmap(struct mm_struct * mm)
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
793
794
795 void insert_vm_struct(struct task_struct *t, struct vm_area_struct *vmp)
796 {
797 struct vm_area_struct *share;
798 struct inode * inode;
799
800 #if 0
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
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
842
843 void remove_shared_vm_struct(struct vm_area_struct *mpnt)
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
866
867
868
869
870
871 void merge_segments (struct task_struct * task, unsigned long start_addr, unsigned long end_addr)
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
880
881 if (!prev) {
882 prev = mpnt;
883 mpnt = next;
884 }
885
886
887
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
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
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
919
920
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 }