NOMMU: Optimise away the {dac_,}mmap_min_addr tests
Subsystems (8)
PATCH 1/6 NOMMU: Optimise away the {dac_,}mmap_min_addr tests
In NOMMU mode clamp dac_mmap_min_addr to zero to cause the tests on it to be skipped by the compiler. We do this as the minimum mmap address doesn't make any sense in NOMMU mode.
mmap_min_addr and round_hint_to_min() can be discarded entirely in NOMMU mode.
---
include/linux/security.h | 7 +++++++ kernel/sysctl.c | 2 ++ mm/Kconfig | 1 + security/Makefile | 3 ++- 4 files changed, 12 insertions(+), 1 deletions(-)
include/linux/security.h +7 -0
@@ -95,8 +95,13 @@ struct seq_file;extern int cap_netlink_send (struct sock *sk,struct sk_buff *skb);extern int cap_netlink_recv (struct sk_buff *skb,int cap); +#ifdef CONFIG_MMU extern unsigned long mmap_min_addr;extern unsigned long dac_mmap_min_addr; +#else +#define dac_mmap_min_addr 0UL +#endif +/* * Values used in the task_security_ops calls */
@@ -121,6 +126,7 @@ struct request_sock;#define LSM_UNSAFE_PTRACE 2#define LSM_UNSAFE_PTRACE_CAP 4 +#ifdef CONFIG_MMU /* * If a hint addr is less than mmap_min_addr change hint to be as * low as possible but still greater than mmap_min_addr
@@ -135,6 +141,7 @@ static inline unsigned long round_hint_to_min(unsigned long hint) }extern int mmap_min_addr_handler (struct ctl_table *table,int write,void __user *buffer,size_t *lenp,loff_t *ppos); +#endif #ifdef CONFIG_SECURITY
kernel/sysctl.c +2 -0
@@ -1214,6 +1214,7 @@ static struct ctl_table vm_table[] = { .proc_handler = proc_dointvec_jiffies, },#endif +#ifdef CONFIG_MMU { .procname = "mmap_min_addr", .data = &dac_mmap_min_addr,
@@ -1221,6 +1222,7 @@ static struct ctl_table vm_table[] = { .mode = 0644, .proc_handler = mmap_min_addr_handler, }, +#endif #ifdef CONFIG_NUMA { .procname = "numa_zonelist_order",
mm/Kconfig +1 -0
@@ -221,6 +221,7 @@ config KSM config DEFAULT_MMAP_MIN_ADDR int "Low address space to protect from user allocation" + depends on MMU default 4096 help This is the portion of low virtual memory which should be protected
security/Makefile +2 -1
@@ -8,7 +8,8 @@ subdir-$(CONFIG_SECURITY_SMACK) += smack subdir-$(CONFIG_SECURITY_TOMOYO) += tomoyo # always enable default capabilities -obj-y += commoncap.o min_addr.o +obj-y += commoncap.o +obj-$(CONFIG_MMU) += min_addr.o # Object file lists obj-$(CONFIG_SECURITY) += security.o capability.o
PATCH 2/6 NOMMU: Provide per-task stack usage through /proc for NOMMU
Make it possible to get the per-task stack usage through /proc on a NOMMU system. The MMU-mode routine can't be used because walk_page_range() doesn't work on NOMMU.
It can be tested to show the stack usages of non-kernel-thread processes:
# grep "Stack usage:" /proc/*/status | grep -v "0 kB" /proc/1/status:Stack usage: 2 kB /proc/57/status:Stack usage: 3 kB /proc/58/status:Stack usage: 1 kB /proc/59/status:Stack usage: 3 kB /proc/60/status:Stack usage: 5 kB /proc/self/status:Stack usage: 1 kB
I've only tested it with ELF-FDPIC, though it should work with FLAT too.
---
fs/proc/array.c | 20 +++++++++++++++++--- 1 files changed, 17 insertions(+), 3 deletions(-)
fs/proc/array.c +17 -3
@@ -387,8 +387,7 @@ static inline unsigned long get_stack_usage_in_bytes(struct vm_area_struct *vma,return ss.usage; } -static inline void task_show_stack_usage (struct seq_file *m, -struct task_struct *task) +static void task_show_stack_usage (struct seq_file *m,struct task_struct *task) {struct vm_area_struct *vma;struct mm_struct *mm =get_task_mm (task);
@@ -404,9 +403,24 @@ static inline void task_show_stack_usage(struct seq_file *m, mmput(mm); } } -#else +#else /* CONFIG_MMU */ +/* +* Calculate the size of a NOMMU process's stack +*/ static void task_show_stack_usage (struct seq_file *m,struct task_struct *task) { +unsigned long sp, base, usage; + + base = task->stack_start; + sp =KSTK_ESP (task); + +#ifdef CONFIG_STACK_GROWSUP + usage = sp - base; +#else + usage = base - sp; +#endif + +seq_printf (m,"Stack usage:\t%lu kB\n" , (usage +1023 ) >>10 ); }#endif /* CONFIG_MMU */
Re: [PATCH 2/6] NOMMU: Provide per-task stack usage through /proc for NOMMU
Am Mittwoch, den 16.12.2009, 23:56 +0000 schrieb David Howells:
Quoted 14 lines
Make it possible to get the per-task stack usage through /proc on a NOMMU system. The MMU-mode routine can't be used because
walk_page_range()doesn't work on NOMMU.It can be tested to show the stack usages of non-kernel-thread processes:
# grep "Stack usage:" /proc/*/status | grep -v "0 kB" /proc/1/status:Stack usage: 2 kB /proc/57/status:Stack usage: 3 kB /proc/58/status:Stack usage: 1 kB /proc/59/status:Stack usage: 3 kB /proc/60/status:Stack usage: 5 kB /proc/self/status:Stack usage: 1 kB
Quoted 7 lines
I've only tested it with ELF-FDPIC, though it should work with FLAT too.
Signed-off-by David Howells <[email protected]>Signed-off-by Mike Frysinger <[email protected]>Cc Stefani Seibold <[email protected]>Cc Paul Mundt <[email protected]>Cc Robin Getz <[email protected]>
Quoted 47 lines
---
fs/proc/array.c | 20 +++++++++++++++++--- 1 files changed, 17 insertions(+), 3 deletions(-)fs/proc/array.c +17
-3@@ -387,8 +387,7 @@ static inline unsigned long get_stack_usage_in_bytes(struct vm_area_struct *vma,return ss.usage; } -static inline void task_show_stack_usage (struct seq_file *m, -struct task_struct *task) +static void task_show_stack_usage (struct seq_file *m,struct task_struct *task) {struct vm_area_struct *vma;struct mm_struct *mm =get_task_mm (task);@@ -404,9 +403,24 @@ static inline void task_show_stack_usage(struct seq_file *m, mmput(mm); } } -#else +#else /* CONFIG_MMU */ +/* +* Calculate the size of a NOMMU process's stack +*/ static void task_show_stack_usage (struct seq_file *m,struct task_struct *task) { +unsigned long sp, base, usage; + + base = task->stack_start; + sp =KSTK_ESP (task); + +#ifdef CONFIG_STACK_GROWSUP + usage = sp - base; +#else + usage = base - sp; +#endif + +seq_printf (m,"Stack usage:\t%lu kB\n" , (usage +1023 ) >>10 ); }#endif /* CONFIG_MMU */
PATCH 3/6 FDPIC: Respect PT_GNU_STACK exec protection markings when creating NOMMU stack
In reply to David HowellsFrom: Mike Frysinger <[email protected]>
The current code will load the stack size and protection markings, but then only use the markings in the MMU code path. The NOMMU code path always passes PROT_EXEC to the mmap() call. While this doesn't matter to most people whilst the code is running, it will cause a pointless icache flush when starting every FDPIC application. Typically this icache flush will be of a region on the order of 128KB in size, or may be the entire icache, depending on the facilities available on the CPU.
In the case where the arch default behaviour seems to be desired (EXSTACK_DEFAULT), we probe VM_STACK_FLAGS for VM_EXEC to determine whether we should be setting PROT_EXEC or not.
For arches that support an MPU (Memory Protection Unit - an MMU without the virtual mapping capability), setting PROT_EXEC or not will make an important difference.
It should be noted that this change also affects the executability of the brk region, since ELF-FDPIC has that share with the stack. However, this is probably irrelevant as NOMMU programs aren't likely to use the brk region, preferring instead allocation via mmap().
---
arch/blackfin/include/asm/page.h | 5 +++++ arch/frv/include/asm/page.h | 2 -- fs/binfmt_elf_fdpic.c | 13 +++++++++++-- 3 files changed, 16 insertions(+), 4 deletions(-)
arch/blackfin/include/asm/page.h +5 -0
@@ -10,4 +10,9 @@#include <asm-generic/page.h> #define MAP_NR (addr) (((unsigned long)(addr)-PAGE_OFFSET) >> PAGE_SHIFT) +#define VM_DATA_DEFAULT_FLAGS \ + (VM_READ | VM_WRITE | \ + ((current->personality & READ_IMPLIES_EXEC) ? VM_EXEC : 0 ) | \ + VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC) +#endif
arch/frv/include/asm/page.h +0 -2
@@ -63,12 +63,10 @@ extern unsigned long max_pfn;#define virt_addr_valid (kaddr) pfn_valid(__pa(kaddr) >> PAGE_SHIFT) -#ifdef CONFIG_MMU #define VM_DATA_DEFAULT_FLAGS \ (VM_READ | VM_WRITE | \ ((current->personality & READ_IMPLIES_EXEC) ? VM_EXEC : 0 ) | \ VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC) -#endif #endif /* __ASSEMBLY__ */
fs/binfmt_elf_fdpic.c +11 -2
@@ -171,6 +171,9 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm,#ifdef ELF_FDPIC_PLAT_INIT unsigned long dynaddr;#endif +#ifndef CONFIG_MMU +unsigned long stack_prot; +#endif struct file *interpreter =NULL ;/* to shut gcc up */ char *interpreter_name =NULL ;int executable_stack;
@@ -316,6 +319,8 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm,* defunct, deceased, etc. after this point we have to exit via * error_kill */ set_personality (PER_LINUX_FDPIC ); +if (elf_read_implies_exec (&exec_params.hdr, executable_stack)) + current->personality |=READ_IMPLIES_EXEC ;set_binfmt (&elf_fdpic_format); current->mm->start_code =0 ;
@@ -377,9 +382,13 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm,if (stack_size <PAGE_SIZE *2 ) stack_size =PAGE_SIZE *2 ; + stack_prot =PROT_READ |PROT_WRITE ; +if (executable_stack ==EXSTACK_ENABLE_X || + (executable_stack ==EXSTACK_DEFAULT &&VM_STACK_FLAGS &VM_EXEC )) + stack_prot |=PROT_EXEC ; +down_write (¤t->mm->mmap_sem); - current->mm->start_brk =do_mmap (NULL ,0 , stack_size, -PROT_READ |PROT_WRITE |PROT_EXEC , + current->mm->start_brk =do_mmap (NULL ,0 , stack_size, stack_prot,MAP_PRIVATE |MAP_ANONYMOUS |MAP_UNINITIALIZED |MAP_GROWSDOWN ,0 );
PATCH 4/6 NOMMU: Avoiding duplicate icache flushes of shared maps
In reply to David HowellsFrom: Mike Frysinger <[email protected]>
When working with FDPIC, there are many shared mappings of read-only code regions between applications (the C library, applet packages like busybox, etc.), but the current do_mmap_pgoff() function will issue an icache flush whenever a VMA is added to an MM instead of only doing it when the map is initially created.
The flush can instead be done when a region is first mmapped PROT_EXEC. Note that we may not rely on the first mapping of a region being executable - it's possible for it to be PROT_READ only, so we have to remember whether we've flushed the region or not, and then flush the entire region when a bit of it is made executable.
However, this also affects the brk area. That will no longer be executable. We can mprotect() it to PROT_EXEC on MPU-mode kernels, but for NOMMU mode kernels, when it increases the brk allocation, making sys_brk() flush the extra from the icache should suffice. The brk area probably isn't used by NOMMU programs since the brk area can only use up the leavings from the stack allocation, where the stack allocation is larger than requested.
---
include/linux/mm_types.h | 2 ++ mm/nommu.c | 11 ++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-)
include/linux/mm_types.h +2 -0
@@ -123,6 +123,8 @@ struct vm_region {struct file *vm_file;/* the backing file or NULL */ atomic_t vm_usage;/* region usage count */ + bool vm_icache_flushed : 1; /* true if the icache has been flushed for +* this region */ };/*
mm/nommu.c +8 -3
@@ -432,6 +432,7 @@ SYSCALL_DEFINE1(brk, unsigned long, brk)/* * Ok, looks good - let it rip. */ +flush_icache_range (mm->brk, brk);return mm->brk = brk; }
@@ -1353,10 +1354,14 @@ unsigned long do_mmap_pgoff(struct file *file, share:add_vma_to_mm (current->mm, vma); -up_write (&nommu_region_sem); +/* we flush the region from the icache only when the first executable +* mapping of it is made */ +if (vma->vm_flags &VM_EXEC && !region->vm_icache_flushed) { +flush_icache_range (region->vm_start, region->vm_end); + region->vm_icache_flushed = true; + } -if (prot &PROT_EXEC ) -flush_icache_range (result, result + len); +up_write (&nommu_region_sem);kleave (" = %lx" , result);return result;
PATCH 5/6 NOMMU: Use copy_*_user_page() in access_process_vm()
In reply to David HowellsFrom: Jie Zhang <[email protected]>
The MMU code uses the copy_*_user_page() variants in access_process_vm() rather than copy_*_user() as the former includes an icache flush. This is important when doing things like setting software breakpoints with gdb. So switch the NOMMU code over to do the same.
This patch makes the reasonable assumption that copy_from_user_page() won't fail - which is probably fine, as we've checked the VMA from which we're copying is usable, and the copy is not allowed to cross VMAs. The one case where it might go wrong is if the VMA is a device rather than RAM, and that device returns an error which - in which case rubbish will be returned rather than EIO.
---
mm/nommu.c | 6 ++++-- 1 files changed, 4 insertions(+), 2 deletions(-)
mm/nommu.c +4 -2
@@ -1896,9 +1896,11 @@ int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, in/* only read or write mappings where it is permitted */ if (write && vma->vm_flags &VM_MAYWRITE ) - len -=copy_to_user ((void *) addr, buf, len); +copy_to_user_page (vma,NULL , addr, + (void *) addr, buf, len);else if (!write && vma->vm_flags &VM_MAYREAD ) - len -=copy_from_user (buf, (void *) addr, len); +copy_from_user_page (vma,NULL , addr, + buf, (void *) addr, len);else len =0 ; }else {
PATCH 6/6 NOMMU: ramfs: Drop unused local var
In reply to David HowellsFrom: Mike Frysinger <[email protected]>
---
fs/ramfs/file-nommu.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
fs/ramfs/file-nommu.c +1 -1
@@ -60,7 +60,7 @@ const struct inode_operations ramfs_file_inode_operations = {*/ int ramfs_nommu_expand_for_mapping (struct inode *inode,size_t newsize) { -unsigned long npages, xpages, loop, limit; +unsigned long npages, xpages, loop;struct page *pages;unsigned order;void *data;