Skip to contentSkip to conversation

ARM: Implement copy_to_user_page() for noMMU

PATCH New Catalin Marinas 3 participants 8 messages Mar 29, 2010 – Mar 30, 2010 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ARM: Implement copy_to_user_page() for noMMU

Commit 7959722 introduced calls to copy_(to|from)_user_page() from access_process_vm() in mm/nommu.c. The copy_to_user_page() was not implemented on noMMU ARM.

---
 arch/arm/mm/nommu.c |    9 +++++++++
 1 files changed, 9 insertions(+), 0 deletions(-)
arch/arm/mm/nommu.c +9 -0
index f8791ee..33b3273 100644
@@ -65,6 +65,15 @@ void flush_dcache_page(struct page *page)
 }
 EXPORT_SYMBOL(flush_dcache_page);
 
+void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
+		       unsigned long uaddr, void *dst, const void *src,
+		       unsigned long len)
+{
+	memcpy(dst, src, len);
+	if (vma->vm_flags & VM_EXEC)
+		__cpuc_coherent_user_range(uaddr, uaddr + len);
+}
+
 void __iomem *__arm_ioremap_pfn(unsigned long pfn, unsigned long offset,
 				size_t size, unsigned int mtype)
 {
#1 lore raw

PATCH 1/1 ARM: Implement copy_to_user_page() for noMMU

arch/arm/mm/nommu.c void flush_dcache_page(struct page *page) · line 75

Quoted 8 lines · Catalin Marinas
arch/arm/mm/nommu.c +8 -0
+void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
+		       unsigned long uaddr, void *dst, const void *src,
+		       unsigned long len)
+{
+	memcpy(dst, src, len);
+	if (vma->vm_flags & VM_EXEC)
+		__cpuc_coherent_user_range(uaddr, uaddr + len);
+}

Does that do the right thing with uaddr + len == 0?

Since this is debugging, it would be unfortunate if something was mapped !VM_EXEC but executed anyway (because it works, or because of a bug), and setting a breakpoint failed to be effective because of entries in the i-cache. It's forbidden semantically, and code which wrote _itself_ to code without flushing i-cache on nommu gets what it deserves. But it may occur that it's executing, even if just due to an application bug, and I'm thinking tracing under the debugger is one time it's good to be more reliable.

Other variations such as writing when a mapping is !VM_EXEC and later mapping or mprotecting the same shmem VM_EXEC, but it's even more forbidden semantically to write to a read-only mapping (and just as unchecked on nommu), and conversion of writable to VM_EXEC ought to flush i-cache at mprotect time.

-- Jamie

#2 lore raw

PATCH 1/1 ARM: Implement copy_to_user_page() for noMMU

Quoted 5 lines · Jamie Lokier

Other variations such as writing when a mapping is !VM_EXEC and later mapping or mprotecting the same shmem VM_EXEC, but it's even more forbidden semantically to write to a read-only mapping (and just as unchecked on nommu), and conversion of writable to VM_EXEC ought to flush i-cache at mprotect time.

If NX isn't implemented, then arm_elf_read_implies_exec() must return 1 for the CPU - so that any region that is marked readable will have VM_EXEC set.

#3 lore raw

PATCH 1/1 ARM: Implement copy_to_user_page() for noMMU

Quoted 10 lines · Russell King - ARM Linux
Quoted 5 lines · Jamie Lokier

Other variations such as writing when a mapping is !VM_EXEC and later mapping or mprotecting the same shmem VM_EXEC, but it's even more forbidden semantically to write to a read-only mapping (and just as unchecked on nommu), and conversion of writable to VM_EXEC ought to flush i-cache at mprotect time.

If NX isn't implemented, then arm_elf_read_implies_exec() must return 1 for the CPU - so that any region that is marked readable will have VM_EXEC set.

If we run uClinux on ARMv6 hardware, we get the above function returning
0. Should we make this function conditional on CONFIG_MMU?

Signature · 1 line
Catalin
#4 lore raw

PATCH 1/1 ARM: Implement copy_to_user_page() for noMMU

Quoted 8 lines · Catalin Marinas
Quoted 7 lines · Russell King - ARM Linux
Quoted 5 lines · Jamie Lokier

Other variations such as writing when a mapping is !VM_EXEC and later mapping or mprotecting the same shmem VM_EXEC, but it's even more forbidden semantically to write to a read-only mapping (and just as unchecked on nommu), and conversion of writable to VM_EXEC ought to flush i-cache at mprotect time.

Quoted 6 lines
Quoted 3 lines

If NX isn't implemented, then arm_elf_read_implies_exec() must return 1 for the CPU - so that any region that is marked readable will have VM_EXEC set.

If we run uClinux on ARMv6 hardware, we get the above function returning
0. Should we make this function conditional on CONFIG_MMU?

Good catch. I suspect no-MMU implies read-implies-exec, because it does... unless there is some exciting MPU setting against it.

If no-MMU implies read-implies-exec, there is no need to check VM_EXEC in this no-MMU code, is there? ;-)

Actually I'm inclined to unconditionally flush i-cache on ptrace write just because (a) ptrace write is not performance critical, and (b) invalid target program behaviour (forgetting to set PROT_EXEC) should not cause GDB setting a breakpoint to fail in such a subtle way.

-- Jamie

#5 lore raw

PATCH 1/1 ARM: Implement copy_to_user_page() for noMMU

In reply to Jamie Lokier
Quoted 11 lines · Jamie Lokier

arch/arm/mm/nommu.c void flush_dcache_page(struct page *page) · line 75

Quoted 8 lines · Catalin Marinas
arch/arm/mm/nommu.c +8 -0
+void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
+                    unsigned long uaddr, void *dst, const void *src,
+                    unsigned long len)
+{
+     memcpy(dst, src, len);
+     if (vma->vm_flags & VM_EXEC)
+             __cpuc_coherent_user_range(uaddr, uaddr + len);
+}

Does that do the right thing with uaddr + len == 0?

There is a corner case where uaddr + len == 0 and uaddr is at the top of the 4GB range. Are there such platforms? A workaround is to change the CPU-specific loop, probably to using BLT rather than BLO.

Quoted 5 lines

Other variations such as writing when a mapping is !VM_EXEC and later mapping or mprotecting the same shmem VM_EXEC, but it's even more forbidden semantically to write to a read-only mapping (and just as unchecked on nommu), and conversion of writable to VM_EXEC ought to flush i-cache at mprotect time.

ARM Linux doesn't do any cache maintenance for mprotect on VIPT or noMMU hardware (not sure about VIVT). We discussed this in the past and it wasn't clear whether it is required or not.

Signature · 1 line
Catalin
#6 lore raw

PATCH 1/1 ARM: Implement copy_to_user_page() for noMMU

Quoted 9 lines · Catalin Marinas
Quoted 5 lines

Other variations such as writing when a mapping is !VM_EXEC and later mapping or mprotecting the same shmem VM_EXEC, but it's even more forbidden semantically to write to a read-only mapping (and just as unchecked on nommu), and conversion of writable to VM_EXEC ought to flush i-cache at mprotect time.

ARM Linux doesn't do any cache maintenance for mprotect on VIPT or noMMU hardware (not sure about VIVT). We discussed this in the past and it wasn't clear whether it is required or not.

I still think it ought to happen on mprotect, but maybe that's a linux-arch discussion. I saw IRIX actually has two PROT_EXEC flavours for mprotect so you can choose.

Back to this, just to double check, what about mapping? E.g. where a shmem is mapped writable (but not executable), has executable code written to it by ptrace, and is later mapped in another process and executed. Will the act of making the second mapping flush i-cache for that range? (No-MMU doesn't need to flush caches on task switch)

-- Jamie

#7 lore raw

PATCH 1/1 ARM: Implement copy_to_user_page() for noMMU

Quoted 7 lines · Jamie Lokier
Quoted 6 lines · Catalin Marinas
Quoted 5 lines

Other variations such as writing when a mapping is !VM_EXEC and later mapping or mprotecting the same shmem VM_EXEC, but it's even more forbidden semantically to write to a read-only mapping (and just as unchecked on nommu), and conversion of writable to VM_EXEC ought to flush i-cache at mprotect time.

Quoted 7 lines
Quoted 3 lines

ARM Linux doesn't do any cache maintenance for mprotect on VIPT or noMMU hardware (not sure about VIVT). We discussed this in the past and it wasn't clear whether it is required or not.

I still think it ought to happen on mprotect, but maybe that's a linux-arch discussion. I saw IRIX actually has two PROT_EXEC flavours for mprotect so you can choose.

You can search some discussions a few months ago with COW pages and cache coherency. This is where mprotect was discussed as well. I think without additional kernel API, we just end up with too much flushing on VIPT hardware (by implementing flush_cache_page).

Quoted 5 lines

Back to this, just to double check, what about mapping? E.g. where a shmem is mapped writable (but not executable), has executable code written to it by ptrace, and is later mapped in another process and executed. Will the act of making the second mapping flush i-cache for that range? (No-MMU doesn't need to flush caches on task switch)

ptrace writes to user pages via copy_to_user_page() which does the flushing if it's a text page (VM_EXEC). In the case you describe above with shared mappings, you would not get any flushing. Is this a real situation?

Signature · 1 line
Catalin