Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jlbec...
[safe/jmp/linux-2.6] / fs / proc / kcore.c
index 802de33..a44a789 100644 (file)
@@ -23,7 +23,6 @@
 #include <asm/io.h>
 #include <linux/list.h>
 #include <linux/ioport.h>
-#include <linux/mm.h>
 #include <linux/memory.h>
 #include <asm/sections.h>
 
@@ -103,23 +102,27 @@ static void free_kclist_ents(struct list_head *head)
        }
 }
 /*
- * Replace all KCORE_RAM information with passed list.
+ * Replace all KCORE_RAM/KCORE_VMEMMAP information with passed list.
  */
 static void __kcore_update_ram(struct list_head *list)
 {
+       int nphdr;
+       size_t size;
        struct kcore_list *tmp, *pos;
        LIST_HEAD(garbage);
 
        write_lock(&kclist_lock);
        if (kcore_need_update) {
                list_for_each_entry_safe(pos, tmp, &kclist_head, list) {
-                       if (pos->type == KCORE_RAM)
+                       if (pos->type == KCORE_RAM
+                               || pos->type == KCORE_VMEMMAP)
                                list_move(&pos->list, &garbage);
                }
                list_splice_tail(list, &kclist_head);
        } else
                list_splice(list, &garbage);
        kcore_need_update = 0;
+       proc_root_kcore->size = get_kcore_size(&nphdr, &size);
        write_unlock(&kclist_lock);
 
        free_kclist_ents(&garbage);
@@ -151,6 +154,47 @@ static int kcore_update_ram(void)
 
 #else /* !CONFIG_HIGHMEM */
 
+#ifdef CONFIG_SPARSEMEM_VMEMMAP
+/* calculate vmemmap's address from given system ram pfn and register it */
+int get_sparsemem_vmemmap_info(struct kcore_list *ent, struct list_head *head)
+{
+       unsigned long pfn = __pa(ent->addr) >> PAGE_SHIFT;
+       unsigned long nr_pages = ent->size >> PAGE_SHIFT;
+       unsigned long start, end;
+       struct kcore_list *vmm, *tmp;
+
+
+       start = ((unsigned long)pfn_to_page(pfn)) & PAGE_MASK;
+       end = ((unsigned long)pfn_to_page(pfn + nr_pages)) - 1;
+       end = ALIGN(end, PAGE_SIZE);
+       /* overlap check (because we have to align page */
+       list_for_each_entry(tmp, head, list) {
+               if (tmp->type != KCORE_VMEMMAP)
+                       continue;
+               if (start < tmp->addr + tmp->size)
+                       if (end > tmp->addr)
+                               end = tmp->addr;
+       }
+       if (start < end) {
+               vmm = kmalloc(sizeof(*vmm), GFP_KERNEL);
+               if (!vmm)
+                       return 0;
+               vmm->addr = start;
+               vmm->size = end - start;
+               vmm->type = KCORE_VMEMMAP;
+               list_add_tail(&vmm->list, head);
+       }
+       return 1;
+
+}
+#else
+int get_sparsemem_vmemmap_info(struct kcore_list *ent, struct list_head *head)
+{
+       return 1;
+}
+
+#endif
+
 static int
 kclist_add_private(unsigned long pfn, unsigned long nr_pages, void *arg)
 {
@@ -181,6 +225,12 @@ kclist_add_private(unsigned long pfn, unsigned long nr_pages, void *arg)
 
        ent->type = KCORE_RAM;
        list_add_tail(&ent->list, head);
+
+       if (!get_sparsemem_vmemmap_info(ent, head)) {
+               list_del(&ent->list);
+               goto free_out;
+       }
+
        return 0;
 free_out:
        kfree(ent);
@@ -381,7 +431,8 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
        unsigned long start;
 
        read_lock(&kclist_lock);
-       proc_root_kcore->size = size = get_kcore_size(&nphdr, &elf_buflen);
+       size = get_kcore_size(&nphdr, &elf_buflen);
+
        if (buflen == 0 || *fpos >= size) {
                read_unlock(&kclist_lock);
                return 0;
@@ -442,7 +493,7 @@ read_kcore(struct file *file, char __user *buffer, size_t buflen, loff_t *fpos)
                if (m == NULL) {
                        if (clear_user(buffer, tsz))
                                return -EFAULT;
-               } else if (is_vmalloc_addr((void *)start)) {
+               } else if (is_vmalloc_or_module_addr((void *)start)) {
                        char * elf_buf;
 
                        elf_buf = kzalloc(tsz, GFP_KERNEL);
@@ -494,6 +545,11 @@ static int open_kcore(struct inode *inode, struct file *filp)
                return -EPERM;
        if (kcore_need_update)
                kcore_update_ram();
+       if (i_size_read(inode) != proc_root_kcore->size) {
+               mutex_lock(&inode->i_mutex);
+               i_size_write(inode, proc_root_kcore->size);
+               mutex_unlock(&inode->i_mutex);
+       }
        return 0;
 }
 
@@ -538,19 +594,39 @@ static void __init proc_kcore_text_init(void)
 }
 #endif
 
+#if defined(CONFIG_MODULES) && defined(MODULES_VADDR)
+/*
+ * MODULES_VADDR has no intersection with VMALLOC_ADDR.
+ */
+struct kcore_list kcore_modules;
+static void __init add_modules_range(void)
+{
+       kclist_add(&kcore_modules, (void *)MODULES_VADDR,
+                       MODULES_END - MODULES_VADDR, KCORE_VMALLOC);
+}
+#else
+static void __init add_modules_range(void)
+{
+}
+#endif
+
 static int __init proc_kcore_init(void)
 {
        proc_root_kcore = proc_create("kcore", S_IRUSR, NULL,
                                      &proc_kcore_operations);
+       if (!proc_root_kcore) {
+               printk(KERN_ERR "couldn't create /proc/kcore\n");
+               return 0; /* Always returns 0. */
+       }
        /* Store text area if it's special */
        proc_kcore_text_init();
        /* Store vmalloc area */
        kclist_add(&kcore_vmalloc, (void *)VMALLOC_START,
                VMALLOC_END - VMALLOC_START, KCORE_VMALLOC);
+       add_modules_range();
        /* Store direct-map area from physical memory map */
        kcore_update_ram();
        hotplug_memory_notifier(kcore_callback, 0);
-       /* Other special area, area-for-module etc is arch specific. */
 
        return 0;
 }