binfmt: turn MAX_ARG_PAGES into a sysctl tunable
Subsystems (4)
RFC PATCH 1/1 binfmt: turn MAX_ARG_PAGES into a sysctl tunable
From: Peter Zijlstra <[email protected]>
Some folks find 128KB of env+arg space too little. Solaris provides them with 1MB. Manually changing MAX_ARG_PAGES worked for them so far, however they would like to run the supported vendor kernel.
In the interrest of not penalizing everybody with the overhead of just setting it larger, provide a sysctl to change it.
Compiles and boots on i386.
--- arch/ia64/ia32/binfmt_elf32.c | 4 +-- arch/mips/kernel/sysirix.c | 2 - arch/x86_64/ia32/ia32_binfmt.c | 4 +-- fs/binfmt_elf.c | 10 +++++---- fs/binfmt_elf_fdpic.c | 15 +++++++------ fs/binfmt_flat.c | 2 - fs/compat.c | 18 +++++++++++++--- fs/exec.c | 44 +++++++++++++++++++++++++++++------------ include/linux/binfmts.h | 14 ++++--------- include/linux/sysctl.h | 1 kernel/sysctl.c | 14 +++++++++++++ 11 files changed, 86 insertions(+), 42 deletions(-)
fs/binfmt_elf.c +6 -4
@@ -255,8 +255,9 @@ create_elf_tables(struct linux_binprm *bwhile (argc-- >0 ) {size_t len;__put_user ((elf_addr_t )p, argv++); - len =strnlen_user ((void __user *)p,PAGE_SIZE *MAX_ARG_PAGES ); -if (!len || len >PAGE_SIZE *MAX_ARG_PAGES ) + len =strnlen_user ((void __user *)p, +PAGE_SIZE *bprm->max_arg_pages); +if (!len || len >PAGE_SIZE *bprm->max_arg_pages)return 0 ; p += len; }
@@ -266,8 +267,9 @@ create_elf_tables(struct linux_binprm *bwhile (envc-- >0 ) {size_t len;__put_user ((elf_addr_t )p, envp++); - len =strnlen_user ((void __user *)p,PAGE_SIZE *MAX_ARG_PAGES ); -if (!len || len >PAGE_SIZE *MAX_ARG_PAGES ) + len =strnlen_user ((void __user *)p, +PAGE_SIZE *bprm->max_arg_pages); +if (!len || len >PAGE_SIZE *bprm->max_arg_pages)return 0 ; p += len; }
fs/binfmt_elf_fdpic.c +8 -7
@@ -578,14 +578,15 @@ static int create_elf_fdpic_tables(struc#ifdef CONFIG_MMU current->mm->arg_start = bprm->p;#else - current->mm->arg_start = current->mm->start_stack - (MAX_ARG_PAGES *PAGE_SIZE - bprm->p); + current->mm->arg_start = current->mm->start_stack - + (bprm->max_arg_pages *PAGE_SIZE - bprm->p);#endif p = (char *) current->mm->arg_start;for (loop = bprm->argc; loop >0 ; loop--) {__put_user ((elf_caddr_t ) p, argv++); - len =strnlen_user (p,PAGE_SIZE *MAX_ARG_PAGES ); -if (!len || len >PAGE_SIZE *MAX_ARG_PAGES ) + len =strnlen_user (p,PAGE_SIZE * bprm->max_arg_pages); +if (!len || len >PAGE_SIZE * bprm->max_arg_pages)return -EINVAL ; p += len; }
@@ -596,8 +597,8 @@ static int create_elf_fdpic_tables(struc current->mm->env_start = (unsigned long ) p;for (loop = bprm->envc; loop >0 ; loop--) {__put_user ((elf_caddr_t )(unsigned long ) p, envp++); - len =strnlen_user (p,PAGE_SIZE *MAX_ARG_PAGES ); -if (!len || len >PAGE_SIZE *MAX_ARG_PAGES ) + len =strnlen_user (p,PAGE_SIZE * bprm->max_arg_pages); +if (!len || len >PAGE_SIZE * bprm->max_arg_pages)return -EINVAL ; p += len; }
@@ -623,7 +624,7 @@ static int elf_fdpic_transfer_args_to_st stop = bprm->p >>PAGE_SHIFT ; sp = *_sp; -for (index =MAX_ARG_PAGES -1 ; index >= stop; index--) { +for (index = bprm->max_arg_pages -1 ; index >= stop; index--) { src =kmap (bprm->page[index]); sp -=PAGE_SIZE ;if (copy_to_user ((void *) sp, src,PAGE_SIZE ) !=0 )
@@ -633,7 +634,7 @@ static int elf_fdpic_transfer_args_to_st goto out; } - *_sp = (*_sp - (MAX_ARG_PAGES *PAGE_SIZE - bprm->p)) & ~15 ; + *_sp = (*_sp - (bprm->max_arg_pages *PAGE_SIZE - bprm->p)) & ~15 ; out:return ret;
fs/binfmt_flat.c +1 -1
@@ -840,7 +840,7 @@ static int load_flat_binary(struct linux* pedantic and include space for the argv/envp array as it may have * a lot of entries. */ -#define TOP_OF_ARGS (PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *)) +#define TOP_OF_ARGS (PAGE_SIZE * bprm->max_arg_pages - sizeof(void *)) stack_len =TOP_OF_ARGS - bprm->p;/* the strings */ stack_len += (bprm->argc +1 ) *sizeof (char *);/* the argv array */ stack_len += (bprm->envc +1 ) *sizeof (char *);/* the envp array */
fs/compat.c +14 -4
@@ -1476,7 +1476,7 @@ static inline void free_arg_pages(struct {int i; -for (i =0 ; i <MAX_ARG_PAGES ; i++) { +for (i =0 ; i < bprm->max_arg_pages; i++) {if (bprm->page[i])__free_page (bprm->page[i]); bprm->page[i] =NULL ;
@@ -1504,14 +1504,20 @@ int compat_do_execve(char * filename,if (!bprm) goto out_ret; + bprm->max_arg_pages = max_arg_pages; + bprm->page =kzalloc (bprm->max_arg_pages*sizeof (struct page *), +GFP_KERNEL ); +if (!bprm->page) + goto out_kfree; + file =open_exec (filename); retval =PTR_ERR (file);if (IS_ERR (file)) - goto out_kfree; + goto out_kfree_page;sched_exec (); - bprm->p =PAGE_SIZE *MAX_ARG_PAGES -sizeof (void *); + bprm->p =PAGE_SIZE *bprm->max_arg_pages-sizeof (void *); bprm->file = file; bprm->filename = filename; bprm->interp = filename;
@@ -1560,13 +1566,14 @@ int compat_do_execve(char * filename,/* execve success */ security_bprm_free (bprm);acct_update_integrals (current); +kfree (bprm->page);kfree (bprm);return retval; } out:/* Something went wrong, return the inode and free the argument pages*/ -for (i =0 ; i <MAX_ARG_PAGES ; i++) { +for (i =0 ; i < bprm->max_arg_pages ; i++) {struct page * page = bprm->page[i];if (page)__free_page (page);
@@ -1585,6 +1592,9 @@ out_file:fput (bprm->file); } +out_kfree_page: +kfree (bprm->page); + out_kfree:kfree (bprm);
fs/exec.c +32 -12
112 lines not on this page · raw message
include/linux/binfmts.h +5 -9
@@ -5,13 +5,6 @@struct pt_regs ; -/* -* MAX_ARG_PAGES defines the number of pages allocated for arguments -* and envelope for the new program. 32 should suffice, this gives -* a maximum env+arg of 128kB w/4KB pages! -*/ -#define MAX_ARG_PAGES 32 -/* sizeof(linux_binprm->buf) */ #define BINPRM_BUF_SIZE 128
@@ -20,9 +13,10 @@ struct pt_regs;/* * This structure is used to hold the arguments that are used when loading binaries. */ -struct linux_binprm { +struct linux_binprm {char buf[BINPRM_BUF_SIZE ]; -struct page *page[MAX_ARG_PAGES ]; +struct page **page; +int max_arg_pages;struct mm_struct *mm;unsigned long p;/* current top of mem */ int sh_bang;
@@ -40,6 +34,8 @@ struct linux_binprm{unsigned long loader, exec; }; +extern int max_arg_pages; +#define BINPRM_FLAGS_ENFORCE_NONDUMP_BIT 0#define BINPRM_FLAGS_ENFORCE_NONDUMP (1 << BINPRM_FLAGS_ENFORCE_NONDUMP_BIT)
include/linux/sysctl.h +1 -0
@@ -148,6 +148,7 @@ enumKERN_SPIN_RETRY =70 ,/* int: number of spinlock retries */ KERN_ACPI_VIDEO_FLAGS =71 ,/* int: flags for setting up video after ACPI sleep */ KERN_IA64_UNALIGNED =72 ,/* int: ia64 unaligned userland trap enable */ + KERN_MAX_ARG_PAGES=73, /* int: max env+arg in pages */ };
kernel/sysctl.c +14 -0
@@ -131,6 +131,9 @@ extern int acct_parm[];extern int no_unaligned_warning;#endif +extern int max_arg_pages; +extern int max_arg_pages_min, max_arg_pages_max; +static int parse_table (int __user *,int ,void __user *,size_t __user *,void __user *,size_t ,ctl_table *,void **); static int proc_doutsstring(ctl_table *table, int write, struct file *filp,
@@ -683,6 +686,17 @@ static ctl_table kern_table[] = { .proc_handler = &proc_dointvec, },#endif + { + .ctl_name = KERN_MAX_ARG_PAGES, + .procname = "max_arg_pages", + .data = &max_arg_pages, + .maxlen = sizeof (int), + .mode = 0644, + .proc_handler = &proc_dointvec_minmax, + .strategy = &sysctl_intvec, + .extra1 = &max_arg_pages_min, + .extra2 = &max_arg_pages_max, + }, { .ctl_name = 0 } };
arch/ia64/ia32/binfmt_elf32.c +2 -2
@@ -207,7 +207,7 @@ ia32_setup_arg_pages (struct linux_binprstruct mm_struct *mm = current->mm;int i, ret; - stack_base =IA32_STACK_TOP -MAX_ARG_PAGES *PAGE_SIZE ; + stack_base =IA32_STACK_TOP - bprm->max_arg_pages*PAGE_SIZE ; mm->arg_start = bprm->p + stack_base; bprm->p += stack_base;
@@ -242,7 +242,7 @@ ia32_setup_arg_pages (struct linux_binpr current->mm->stack_vm = current->mm->total_vm =vma_pages (mpnt); } -for (i =0 ; i <MAX_ARG_PAGES ; i++) { +for (i =0 ; i < bprm->max_arg_pages ; i++) {struct page *page = bprm->page[i];if (page) { bprm->page[i] =NULL ;
arch/mips/kernel/sysirix.c +1 -1
@@ -344,7 +344,7 @@ asmlinkage int irix_syssgi(struct pt_regcase SGI_SYSCONF : {switch (regs->regs[base +5 ]) {case 1 : - retval = (MAX_ARG_PAGES >>4 );/* XXX estimate... */ + retval = (max_arg_pages >>4 );/* XXX estimate... */ goto out;case 2 : retval = max_threads;
arch/x86_64/ia32/ia32_binfmt.c +2 -2
@@ -339,7 +339,7 @@ int ia32_setup_arg_pages(struct linux_bistruct mm_struct *mm = current->mm;int i, ret; - stack_base = stack_top -MAX_ARG_PAGES *PAGE_SIZE ; + stack_base = stack_top - bprm->max_arg_pages *PAGE_SIZE ; mm->arg_start = bprm->p + stack_base; bprm->p += stack_base;
@@ -374,7 +374,7 @@ int ia32_setup_arg_pages(struct linux_bi mm->stack_vm = mm->total_vm =vma_pages (mpnt); } -for (i =0 ; i <MAX_ARG_PAGES ; i++) { +for (i =0 ; i < bprm->max_arg_pages ; i++) {struct page *page = bprm->page[i];if (page) { bprm->page[i] =NULL ;
Quoted 5 lines · Peter Zijlstra
From: Peter Zijlstra <[email protected]>
Some folks find 128KB of env+arg space too little. Solaris provides them with 1MB. Manually changing
MAX_ARG_PAGESworked for them so far, however they would like to run the supported vendor kernel.
then this patch should go to the vendors maybe ;)
Quoted 4 lines
In the interrest of not penalizing everybody with the overhead of just setting it larger, provide a sysctl to change it.
why not go all the way and make it truely dynamic ?
Quoted 8 lines · On Wed, 14 Jun 2006 08:53:48 -0700 Arjan van de Ven wrote:
Quoted 5 lines · Peter Zijlstra
From: Peter Zijlstra <[email protected]>
Some folks find 128KB of env+arg space too little. Solaris provides them with 1MB. Manually changing
MAX_ARG_PAGESworked for them so far, however they would like to run the supported vendor kernel.then this patch should go to the vendors maybe ;)
Wouldn't a mainline merge get it to the vendors? or put another way, why would the distro vendors want it if it's not in mainline?
Quoted 6 lines
Quoted 3 lines
In the interrest of not penalizing everybody with the overhead of just setting it larger, provide a sysctl to change it.
why not go all the way and make it truely dynamic ?
---
~Randy
Quoted 8 lines · Arjan van de Ven
Quoted 5 lines · Peter Zijlstra
From: Peter Zijlstra <[email protected]>
Some folks find 128KB of env+arg space too little. Solaris provides them with 1MB. Manually changing
MAX_ARG_PAGESworked for them so far, however they would like to run the supported vendor kernel.then this patch should go to the vendors maybe ;)
You know it comes from one :-)
But as Randy said, there is interrest to not deviate too much and esp. wrt. interfaces.
Quoted 6 lines
Quoted 4 lines
In the interrest of not penalizing everybody with the overhead of just setting it larger, provide a sysctl to change it.
why not go all the way and make it truely dynamic ?
Looking at the code again even this change feels heavy, making it dynamic would only bloat it more.
If you really think we'll gain anything by having this dynamic please tell me and I'll make an honest effort, but for now I'd like to stick with this approach.
Peter
Hi!
Quoted 8 lines
From: Peter Zijlstra <[email protected]>
Some folks find 128KB of env+arg space too little. Solaris provides them with 1MB. Manually changing
MAX_ARG_PAGESworked for them so far, however they would like to run the supported vendor kernel.In the interrest of not penalizing everybody with the overhead of just setting it larger, provide a sysctl to change it.
I very muh like that idea.
Quoted 3 lines
Compiles and boots on i386.
Signed-off-by Peter Zijlstra <[email protected]>
...but does is also work if you keep changing that value in the tight loop while trying to work with the system?
Quoted 33 lines
Index:
linux-2.6/fs/binfmt_elf.c
===================================================================fs/binfmt_elf.c +6
-4@@ -255,8 +255,9 @@ create_elf_tables(struct linux_binprm *bwhile (argc-- >0 ) {size_t len;__put_user ((elf_addr_t )p, argv++); - len =strnlen_user ((void __user *)p,PAGE_SIZE *MAX_ARG_PAGES ); -if (!len || len >PAGE_SIZE *MAX_ARG_PAGES ) + len =strnlen_user ((void __user *)p, +PAGE_SIZE *bprm->max_arg_pages); +if (!len || len >PAGE_SIZE *bprm->max_arg_pages)return 0 ; p += len; }@@ -266,8 +267,9 @@ create_elf_tables(struct linux_binprm *bwhile (envc-- >0 ) {size_t len;__put_user ((elf_addr_t )p, envp++); - len =strnlen_user ((void __user *)p,PAGE_SIZE *MAX_ARG_PAGES ); -if (!len || len >PAGE_SIZE *MAX_ARG_PAGES ) + len =strnlen_user ((void __user *)p, +PAGE_SIZE *bprm->max_arg_pages); +if (!len || len >PAGE_SIZE *bprm->max_arg_pages)return 0 ; p += len; }Index:
linux-2.6/fs/binfmt_elf_fdpic.c
===================================================================
fs/binfmt_elf_fdpic.c static int create_elf_fdpic_tables(struc · line 586
Quoted 10 lines
fs/binfmt_elf_fdpic.c +2
-1@@ -578,14 +578,15 @@ static int create_elf_fdpic_tables(struc#ifdef CONFIG_MMU current->mm->arg_start = bprm->p;#else - current->mm->arg_start = current->mm->start_stack - (MAX_ARG_PAGES *PAGE_SIZE - bprm->p); + current->mm->arg_start = current->mm->start_stack - + (bprm->max_arg_pages *PAGE_SIZE - bprm->p);#endif p = (char *) current->mm->arg_start;for (loop = bprm->argc; loop >0 ; loop--) {
What happens if someone changes that sysctl from other cpu at this point?
Quoted 8 lines
fs/binfmt_elf_fdpic.c +2
-2__put_user ((elf_caddr_t ) p, argv++); - len =strnlen_user (p,PAGE_SIZE *MAX_ARG_PAGES ); -if (!len || len >PAGE_SIZE *MAX_ARG_PAGES ) + len =strnlen_user (p,PAGE_SIZE * bprm->max_arg_pages); +if (!len || len >PAGE_SIZE * bprm->max_arg_pages)return -EINVAL ; p += len; }
Signature · 1 line
Thanks for all the (sleeping) penguins.
Quoted 11 lines · Pavel Machek
Hi!
Quoted 8 lines
From: Peter Zijlstra <[email protected]>
Some folks find 128KB of env+arg space too little. Solaris provides them with 1MB. Manually changing
MAX_ARG_PAGESworked for them so far, however they would like to run the supported vendor kernel.In the interrest of not penalizing everybody with the overhead of just setting it larger, provide a sysctl to change it.
Quoted 9 lines
I very muh like that idea.
Quoted 3 lines
Compiles and boots on i386.
Signed-off-by Peter Zijlstra <[email protected]>....but does is also work if you keep changing that value in the tight loop while trying to work with the system?
Yes, I've added a bprm local copy of max_arg_pages:
Quoted 5 lines
(quoted diff) +0
-0@@ -20,9 +13,10 @@ struct pt_regs;Quoted 4 lines
/*
* This structure is used to hold the arguments that are used when loading binaries. */
-struct linux_binprm{
Quoted 8 lines
include/linux/binfmts.h struct pt_regs; · line 22
Quoted 8 lines
include/linux/binfmts.h +3
-1+struct linux_binprm {char buf[BINPRM_BUF_SIZE ]; -struct page *page[MAX_ARG_PAGES ]; +struct page **page; +int max_arg_pages;struct mm_struct *mm;unsigned long p;/* current top of mem */ int sh_bang;
That is set once from the sysctl variable:
Quoted 10 lines
fs/exec.c int do_execve(char * filename, · line 1172
Quoted 10 lines
fs/exec.c +6
-0@@ -1153,14 +1164,20 @@ int do_execve(char * filename,if (!bprm) goto out_ret; + bprm->max_arg_pages = max_arg_pages; + bprm->page =kzalloc (bprm->max_arg_pages*sizeof (struct page *), +GFP_KERNEL ); +if (!bprm->page) + goto out_kfree; +
And is thereafter used and never again changed.
This should be enough to guard against your scenario I recon.
Peter
Hi!
Quoted 11 lines
Quoted 11 lines
Quoted 6 lines
Some folks find 128KB of env+arg space too little. Solaris provides them with 1MB. Manually changing
MAX_ARG_PAGESworked for them so far, however they would like to run the supported vendor kernel.In the interrest of not penalizing everybody with the overhead of just setting it larger, provide a sysctl to change it.
I very muh like that idea.
Quoted 2 lines
Compiles and boots on i386.
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Signed-off-by Peter Zijlstra <[email protected]>....but does is also work if you keep changing that value in the tight loop while trying to work with the system?
Yes, I've added a bprm local copy of
max_arg_pages:
Thanks for explanation.
I've been upping that value 10 times on all my systems, so yes, I'd like to see that configurable.
You probably want to mail it to lkml, cc: akpm, and see what happens.
Pavel
Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek (cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html