power: supply: Allow charger manager can be built as a module
[linux-2.6-microblaze.git] / fs / binfmt_elf.c
index e64bff4..f4713ea 100644 (file)
@@ -97,7 +97,7 @@ static struct linux_binfmt elf_format = {
        .min_coredump   = ELF_EXEC_PAGESIZE,
 };
 
-#define BAD_ADDR(x) ((unsigned long)(x) >= TASK_SIZE)
+#define BAD_ADDR(x) (unlikely((unsigned long)(x) >= TASK_SIZE))
 
 static int set_brk(unsigned long start, unsigned long end, int prot)
 {
@@ -165,6 +165,7 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
                unsigned long load_addr, unsigned long interp_load_addr,
                unsigned long e_entry)
 {
+       struct mm_struct *mm = current->mm;
        unsigned long p = bprm->p;
        int argc = bprm->argc;
        int envc = bprm->envc;
@@ -227,7 +228,7 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
                return -EFAULT;
 
        /* Create the ELF interpreter info */
-       elf_info = (elf_addr_t *)current->mm->saved_auxv;
+       elf_info = (elf_addr_t *)mm->saved_auxv;
        /* update AT_VECTOR_SIZE_BASE if the number of NEW_AUX_ENT() changes */
 #define NEW_AUX_ENT(id, val) \
        do { \
@@ -276,13 +277,13 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
        }
 #undef NEW_AUX_ENT
        /* AT_NULL is zero; clear the rest too */
-       memset(elf_info, 0, (char *)current->mm->saved_auxv +
-                       sizeof(current->mm->saved_auxv) - (char *)elf_info);
+       memset(elf_info, 0, (char *)mm->saved_auxv +
+                       sizeof(mm->saved_auxv) - (char *)elf_info);
 
        /* And advance past the AT_NULL entry.  */
        elf_info += 2;
 
-       ei_index = elf_info - (elf_addr_t *)current->mm->saved_auxv;
+       ei_index = elf_info - (elf_addr_t *)mm->saved_auxv;
        sp = STACK_ADD(p, ei_index);
 
        items = (argc + 1) + (envc + 1) + 1;
@@ -301,7 +302,7 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
         * Grow the stack manually; some architectures have a limit on how
         * far ahead a user-space access may be in order to grow the stack.
         */
-       vma = find_extend_vma(current->mm, bprm->p);
+       vma = find_extend_vma(mm, bprm->p);
        if (!vma)
                return -EFAULT;
 
@@ -310,7 +311,7 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
                return -EFAULT;
 
        /* Populate list of argv pointers back to argv strings. */
-       p = current->mm->arg_end = current->mm->arg_start;
+       p = mm->arg_end = mm->arg_start;
        while (argc-- > 0) {
                size_t len;
                if (__put_user((elf_addr_t)p, sp++))
@@ -322,10 +323,10 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
        }
        if (__put_user(0, sp++))
                return -EFAULT;
-       current->mm->arg_end = p;
+       mm->arg_end = p;
 
        /* Populate list of envp pointers back to envp strings. */
-       current->mm->env_end = current->mm->env_start = p;
+       mm->env_end = mm->env_start = p;
        while (envc-- > 0) {
                size_t len;
                if (__put_user((elf_addr_t)p, sp++))
@@ -337,10 +338,10 @@ create_elf_tables(struct linux_binprm *bprm, const struct elfhdr *exec,
        }
        if (__put_user(0, sp++))
                return -EFAULT;
-       current->mm->env_end = p;
+       mm->env_end = p;
 
        /* Put the elf_info on the stack in the right place.  */
-       if (copy_to_user(sp, current->mm->saved_auxv, ei_index * sizeof(elf_addr_t)))
+       if (copy_to_user(sp, mm->saved_auxv, ei_index * sizeof(elf_addr_t)))
                return -EFAULT;
        return 0;
 }
@@ -701,6 +702,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
                struct elfhdr interp_elf_ex;
        } *loc;
        struct arch_elf_state arch_state = INIT_ARCH_ELF_STATE;
+       struct mm_struct *mm;
        struct pt_regs *regs;
 
        loc = kmalloc(sizeof(*loc), GFP_KERNEL);
@@ -1096,11 +1098,13 @@ out_free_interp:
                          load_addr, interp_load_addr, e_entry);
        if (retval < 0)
                goto out;
-       current->mm->end_code = end_code;
-       current->mm->start_code = start_code;
-       current->mm->start_data = start_data;
-       current->mm->end_data = end_data;
-       current->mm->start_stack = bprm->p;
+
+       mm = current->mm;
+       mm->end_code = end_code;
+       mm->start_code = start_code;
+       mm->start_data = start_data;
+       mm->end_data = end_data;
+       mm->start_stack = bprm->p;
 
        if ((current->flags & PF_RANDOMIZE) && (randomize_va_space > 1)) {
                /*
@@ -1111,12 +1115,11 @@ out_free_interp:
                 * growing down), and into the unused ELF_ET_DYN_BASE region.
                 */
                if (IS_ENABLED(CONFIG_ARCH_HAS_ELF_RANDOMIZE) &&
-                   elf_ex->e_type == ET_DYN && !interpreter)
-                       current->mm->brk = current->mm->start_brk =
-                               ELF_ET_DYN_BASE;
+                   elf_ex->e_type == ET_DYN && !interpreter) {
+                       mm->brk = mm->start_brk = ELF_ET_DYN_BASE;
+               }
 
-               current->mm->brk = current->mm->start_brk =
-                       arch_randomize_brk(current->mm);
+               mm->brk = mm->start_brk = arch_randomize_brk(mm);
 #ifdef compat_brk_randomized
                current->brk_randomized = 1;
 #endif
@@ -1574,6 +1577,7 @@ static void fill_siginfo_note(struct memelfnote *note, user_siginfo_t *csigdata,
  */
 static int fill_files_note(struct memelfnote *note)
 {
+       struct mm_struct *mm = current->mm;
        struct vm_area_struct *vma;
        unsigned count, size, names_ofs, remaining, n;
        user_long_t *data;
@@ -1581,7 +1585,7 @@ static int fill_files_note(struct memelfnote *note)
        char *name_base, *name_curpos;
 
        /* *Estimated* file count and total data size needed */
-       count = current->mm->map_count;
+       count = mm->map_count;
        if (count > UINT_MAX / 64)
                return -EINVAL;
        size = count * 64;
@@ -1591,6 +1595,10 @@ static int fill_files_note(struct memelfnote *note)
        if (size >= MAX_FILE_NOTE_SIZE) /* paranoia check */
                return -EINVAL;
        size = round_up(size, PAGE_SIZE);
+       /*
+        * "size" can be 0 here legitimately.
+        * Let it ENOMEM and omit NT_FILE section which will be empty anyway.
+        */
        data = kvmalloc(size, GFP_KERNEL);
        if (ZERO_OR_NULL_PTR(data))
                return -ENOMEM;
@@ -1599,7 +1607,7 @@ static int fill_files_note(struct memelfnote *note)
        name_base = name_curpos = ((char *)data) + names_ofs;
        remaining = size - names_ofs;
        count = 0;
-       for (vma = current->mm->mmap; vma != NULL; vma = vma->vm_next) {
+       for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
                struct file *file;
                const char *filename;
 
@@ -1633,10 +1641,10 @@ static int fill_files_note(struct memelfnote *note)
        data[0] = count;
        data[1] = PAGE_SIZE;
        /*
-        * Count usually is less than current->mm->map_count,
+        * Count usually is less than mm->map_count,
         * we need to move filenames down.
         */
-       n = current->mm->map_count - count;
+       n = mm->map_count - count;
        if (n != 0) {
                unsigned shift_bytes = n * 3 * sizeof(data[0]);
                memmove(name_base - shift_bytes, name_base,
@@ -2182,7 +2190,7 @@ static int elf_core_dump(struct coredump_params *cprm)
        int segs, i;
        size_t vma_data_size = 0;
        struct vm_area_struct *vma, *gate_vma;
-       struct elfhdr *elf = NULL;
+       struct elfhdr elf;
        loff_t offset = 0, dataoff;
        struct elf_note_info info = { };
        struct elf_phdr *phdr4note = NULL;
@@ -2203,10 +2211,6 @@ static int elf_core_dump(struct coredump_params *cprm)
         * exists while dumping the mm->vm_next areas to the core file.
         */
   
-       /* alloc memory for large data structures: too large to be on stack */
-       elf = kmalloc(sizeof(*elf), GFP_KERNEL);
-       if (!elf)
-               goto out;
        /*
         * The number of segs are recored into ELF header as 16bit value.
         * Please check DEFAULT_MAX_MAP_COUNT definition when you modify here.
@@ -2230,7 +2234,7 @@ static int elf_core_dump(struct coredump_params *cprm)
         * Collect all the non-memory information about the process for the
         * notes.  This also sets up the file header.
         */
-       if (!fill_note_info(elf, e_phnum, &info, cprm->siginfo, cprm->regs))
+       if (!fill_note_info(&elf, e_phnum, &info, cprm->siginfo, cprm->regs))
                goto cleanup;
 
        has_dumped = 1;
@@ -2238,7 +2242,7 @@ static int elf_core_dump(struct coredump_params *cprm)
        fs = get_fs();
        set_fs(KERNEL_DS);
 
-       offset += sizeof(*elf);                         /* Elf header */
+       offset += sizeof(elf);                          /* Elf header */
        offset += segs * sizeof(struct elf_phdr);       /* Program headers */
 
        /* Write notes phdr entry */
@@ -2257,11 +2261,13 @@ static int elf_core_dump(struct coredump_params *cprm)
 
        dataoff = offset = roundup(offset, ELF_EXEC_PAGESIZE);
 
-       if (segs - 1 > ULONG_MAX / sizeof(*vma_filesz))
-               goto end_coredump;
+       /*
+        * Zero vma process will get ZERO_SIZE_PTR here.
+        * Let coredump continue for register state at least.
+        */
        vma_filesz = kvmalloc(array_size(sizeof(*vma_filesz), (segs - 1)),
                              GFP_KERNEL);
-       if (ZERO_OR_NULL_PTR(vma_filesz))
+       if (!vma_filesz)
                goto end_coredump;
 
        for (i = 0, vma = first_vma(current, gate_vma); vma != NULL;
@@ -2281,12 +2287,12 @@ static int elf_core_dump(struct coredump_params *cprm)
                shdr4extnum = kmalloc(sizeof(*shdr4extnum), GFP_KERNEL);
                if (!shdr4extnum)
                        goto end_coredump;
-               fill_extnum_info(elf, shdr4extnum, e_shoff, segs);
+               fill_extnum_info(&elf, shdr4extnum, e_shoff, segs);
        }
 
        offset = dataoff;
 
-       if (!dump_emit(cprm, elf, sizeof(*elf)))
+       if (!dump_emit(cprm, &elf, sizeof(elf)))
                goto end_coredump;
 
        if (!dump_emit(cprm, phdr4note, sizeof(*phdr4note)))
@@ -2370,8 +2376,6 @@ cleanup:
        kfree(shdr4extnum);
        kvfree(vma_filesz);
        kfree(phdr4note);
-       kfree(elf);
-out:
        return has_dumped;
 }