Commit 6fc96ef2 authored by David Howells's avatar David Howells Committed by Linus Torvalds

[PATCH] Fix nommu MAP_SHARED handling

The attached patch does the following things:

 (1) It uniquifies permitted overlapping VMAs (eg: MAP_SHARED on chardevs) in
     nommu_vma_tree. Identical entries break the assumptions on which rbtrees
     work. Since we don't need to share VMAs in this case, we uniquify such
     VMAs by using the pointer to the VMA. They're only kept in the tree for
     /proc/maps visibility.

 (2) Extracts VMA unlinking into its own function so that the source is
     adjacent to the VMA linking function.

 (3) No longer releases memory belonging to a shared chardev or file (the
     underlying driver is expected to provide mappable memory).

 (4) Frees the file attached to a VMA whether or not that VMA is shared or is
     a memory-mapped I/O mapping.
Signed-Off-By: default avatarDavid Howells <dhowells@redhat.com>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent 9a9474d9
...@@ -315,25 +315,51 @@ static inline struct vm_area_struct *find_nommu_vma(unsigned long start) ...@@ -315,25 +315,51 @@ static inline struct vm_area_struct *find_nommu_vma(unsigned long start)
static void add_nommu_vma(struct vm_area_struct *vma) static void add_nommu_vma(struct vm_area_struct *vma)
{ {
struct vm_area_struct *pvma; struct vm_area_struct *pvma;
struct address_space *mapping;
struct rb_node **p = &nommu_vma_tree.rb_node; struct rb_node **p = &nommu_vma_tree.rb_node;
struct rb_node *parent = NULL; struct rb_node *parent = NULL;
/* add the VMA to the master list */
while (*p) { while (*p) {
parent = *p; parent = *p;
pvma = rb_entry(parent, struct vm_area_struct, vm_rb); pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
if (vma->vm_start < pvma->vm_start) if (vma->vm_start < pvma->vm_start) {
p = &(*p)->rb_left; p = &(*p)->rb_left;
else if (vma->vm_start > pvma->vm_start) }
else if (vma->vm_start > pvma->vm_start) {
p = &(*p)->rb_right; p = &(*p)->rb_right;
else }
BUG(); /* shouldn't happen by this point */ else {
/* mappings are at the same address - this can only
* happen for shared-mem chardevs and shared file
* mappings backed by ramfs/tmpfs */
BUG_ON(!(pvma->vm_flags & VM_SHARED));
if (vma < pvma)
p = &(*p)->rb_left;
else if (vma > pvma)
p = &(*p)->rb_right;
else
BUG();
}
} }
rb_link_node(&vma->vm_rb, parent, p); rb_link_node(&vma->vm_rb, parent, p);
rb_insert_color(&vma->vm_rb, &nommu_vma_tree); rb_insert_color(&vma->vm_rb, &nommu_vma_tree);
} }
static void delete_nommu_vma(struct vm_area_struct *vma)
{
struct address_space *mapping;
/* remove from the master list */
rb_erase(&vma->vm_rb, &nommu_vma_tree);
}
/*
* handle mapping creation for uClinux
*/
unsigned long do_mmap_pgoff(struct file *file, unsigned long do_mmap_pgoff(struct file *file,
unsigned long addr, unsigned long addr,
unsigned long len, unsigned long len,
...@@ -633,27 +659,33 @@ unsigned long do_mmap_pgoff(struct file *file, ...@@ -633,27 +659,33 @@ unsigned long do_mmap_pgoff(struct file *file,
return -ENOMEM; return -ENOMEM;
} }
/*
* handle mapping disposal for uClinux
*/
static void put_vma(struct vm_area_struct *vma) static void put_vma(struct vm_area_struct *vma)
{ {
if (vma) { if (vma) {
down_write(&nommu_vma_sem); down_write(&nommu_vma_sem);
if (atomic_dec_and_test(&vma->vm_usage)) { if (atomic_dec_and_test(&vma->vm_usage)) {
rb_erase(&vma->vm_rb, &nommu_vma_tree); delete_nommu_vma(vma);
if (vma->vm_ops && vma->vm_ops->close) if (vma->vm_ops && vma->vm_ops->close)
vma->vm_ops->close(vma); vma->vm_ops->close(vma);
if (!(vma->vm_flags & VM_IO) && vma->vm_start) { /* IO memory and memory shared directly out of the pagecache from
* ramfs/tmpfs mustn't be released here */
if (!(vma->vm_flags & (VM_IO | VM_SHARED)) && vma->vm_start) {
realalloc -= kobjsize((void *) vma->vm_start); realalloc -= kobjsize((void *) vma->vm_start);
askedalloc -= vma->vm_end - vma->vm_start; askedalloc -= vma->vm_end - vma->vm_start;
if (vma->vm_file)
fput(vma->vm_file);
kfree((void *) vma->vm_start); kfree((void *) vma->vm_start);
} }
realalloc -= kobjsize(vma); realalloc -= kobjsize(vma);
askedalloc -= sizeof(*vma); askedalloc -= sizeof(*vma);
if (vma->vm_file)
fput(vma->vm_file);
kfree(vma); kfree(vma);
} }
...@@ -664,6 +696,7 @@ static void put_vma(struct vm_area_struct *vma) ...@@ -664,6 +696,7 @@ static void put_vma(struct vm_area_struct *vma)
int do_munmap(struct mm_struct *mm, unsigned long addr, size_t len) int do_munmap(struct mm_struct *mm, unsigned long addr, size_t len)
{ {
struct vm_list_struct *vml, **parent; struct vm_list_struct *vml, **parent;
unsigned long end = addr + len;
#ifdef MAGIC_ROM_PTR #ifdef MAGIC_ROM_PTR
/* For efficiency's sake, if the pointer is obviously in ROM, /* For efficiency's sake, if the pointer is obviously in ROM,
...@@ -677,15 +710,16 @@ int do_munmap(struct mm_struct *mm, unsigned long addr, size_t len) ...@@ -677,15 +710,16 @@ int do_munmap(struct mm_struct *mm, unsigned long addr, size_t len)
#endif #endif
for (parent = &mm->context.vmlist; *parent; parent = &(*parent)->next) for (parent = &mm->context.vmlist; *parent; parent = &(*parent)->next)
if ((*parent)->vma->vm_start == addr) if ((*parent)->vma->vm_start == addr &&
break; (*parent)->vma->vm_end == end)
vml = *parent; goto found;
if (!vml) { printk("munmap of non-mmaped memory by process %d (%s): %p\n",
printk("munmap of non-mmaped memory by process %d (%s): %p\n", current->pid, current->comm, (void *) addr);
current->pid, current->comm, (void *) addr); return -EINVAL;
return -EINVAL;
} found:
vml = *parent;
put_vma(vml->vma); put_vma(vml->vma);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment