Fix ELF / FDPIC ELF core dumping, and use mmap_sem properly in there
Subsystems (5)
cover Fix ELF / FDPIC ELF core dumping, and use mmap_sem properly in there
At the moment, we have that rather ugly mmget_still_valid() helper to work around <https://crbug.com/project-zero/1790>: ELF core dumping doesn't take the mmap_sem while traversing the task's VMAs, and if anything (like userfaultfd) then remotely messes with the VMA tree, fireworks ensue. So at the moment we use mmget_still_valid() to bail out in any writers that might be operating on a remote mm's VMAs.
With this series, I'm trying to get rid of the need for that as cleanly as possible.
In particular, I want to avoid holding the mmap_sem across unbounded sleeps.
Patches 1, 2 and 3 are relatively unrelated cleanups in the core dumping code.
Patches 4 and 5 implement the main change: Instead of repeatedly accessing the VMA list with sleeps in between, we snapshot it at the start with proper locking, and then later we just use our copy of the VMA list. This ensures that the kernel won't crash, that VMA metadata in the coredump is consistent even in the presence of concurrent modifications, and that any virtual addresses that aren't being concurrently modified have their contents show up in the core dump properly.
The disadvantage of this approach is that we need a bit more memory during core dumping for storing metadata about all VMAs.
After this series has landed, we should be able to rip out mmget_still_valid().
Testing done so far:
- Creating a simple core dump on X86-64 still works.
- The created coredump on X86-64 opens in GDB, and both the stack and the exectutable look vaguely plausible.
- 32-bit ARM compiles with FDPIC support, both with MMU and !MMU config.
I'm CCing some folks from the architectures that use FDPIC in case anyone wants to give this a spin.
This series is based on
<https://lore.kernel.org/linux-fsdevel/[email protected]/>
(Christoph Hellwig's "remove set_fs calls from the coredump code v4").
Jann Horn (5):
binfmt_elf_fdpic: Stop using dump_emit() on user pointers on !MMU
coredump: Fix handling of partial writes in dump_emit()
coredump: Refactor page range dumping into common helper binfmt_elf, binfmt_elf_fdpic: Use a VMA list snapshot mm/gup: Take mmap_sem in get_dump_page()
fs/binfmt_elf.c | 170 ++++++++++++--------------------------- fs/binfmt_elf_fdpic.c | 106 +++++++++--------------- fs/coredump.c | 102 +++++++++++++++++++++++ include/linux/coredump.h | 12 +++ mm/gup.c | 69 +++++++++------- 5 files changed, 243 insertions(+), 216 deletions(-)
base-commit: 6a8b55ed4056ea5559ebe4f6a4b247f627870d4c
prerequisite-patch-id: c0a20b414eebc48fe0a8ca570b05de34c7980396
prerequisite-patch-id: 51973b8db0fa4b114e0c3fd8936b634d9d5061c5
prerequisite-patch-id: 0e1e8de282ca6d458dc6cbdc6b6ec5879edd8a05
prerequisite-patch-id: d5ee749c4d3a22ec80bd0dd88aadf89aeb569db8
prerequisite-patch-id: 46ce14e59e98e212a1eca0aef69c6dcdb62b8242
Signature · 1 line
2.26.2.303.gf8c07b1a785-goog
PATCH 1/5 binfmt_elf_fdpic: Stop using dump_emit() on user pointers on !MMU
dump_emit() is for kernel pointers, and VMAs describe userspace memory. Let's be tidy here and avoid accessing userspace pointers under KERNEL_DS, even if it probably doesn't matter much on !MMU systems - especially given that it looks like we can just use the same get_dump_page() as on MMU if we move it out of the CONFIG_MMU block.
--- fs/binfmt_elf_fdpic.c | 8 ------ mm/gup.c | 58 +++++++++++++++++++++---------------------- 2 files changed, 29 insertions(+), 37 deletions(-)
fs/binfmt_elf_fdpic.c +0 -8
@@ -1495,14 +1495,11 @@ static bool elf_fdpic_dump_segments(struct coredump_params *cprm)struct vm_area_struct *vma;for (vma = current->mm->mmap; vma; vma = vma->vm_next) { -#ifdef CONFIG_MMU unsigned long addr; -#endif if (!maydump (vma, cprm->mm_flags))continue ; -#ifdef CONFIG_MMU for (addr = vma->vm_start; addr < vma->vm_end; addr +=PAGE_SIZE ) {bool res;
@@ -1518,11 +1515,6 @@ static bool elf_fdpic_dump_segments(struct coredump_params *cprm)if (!res)return false; } -#else -if (!dump_emit (cprm, (void *) vma->vm_start, - vma->vm_end - vma->vm_start)) -return false; -#endif }return true; }
mm/gup.c +29 -29
70 lines not on this page · raw message
Signature · 1 line
2.26.2.303.gf8c07b1a785-goog
PATCH 2/5 coredump: Fix handling of partial writes in dump_emit()
In reply to Jann HornAfter a partial write, we have to update the input buffer pointer.
--- fs/coredump.c | 1 + 1 file changed, 1 insertion(+)
fs/coredump.c +1 -0
@@ -833,6 +833,7 @@ int dump_emit(struct coredump_params *cprm, const void *addr, int nr) cprm->written += n; cprm->pos += n; nr -= n; + addr += n; }return 1 ; }
Signature · 1 line
2.26.2.303.gf8c07b1a785-goog
Re: [PATCH 2/5] coredump: Fix handling of partial writes in dump_emit()
Quoted 2 lines · Jann Horn
After a partial write, we have to update the input buffer pointer.
Interesting. It seems this partial write case never triggers (except for actually killing the core-dump).
Or did you find a case where it actually matters?
Your fix is obviously correct, but it also makes me go "that function clearly never actually worked for partial writes, maybe we shouldn't even bother?"
Linus
Re: [PATCH 2/5] coredump: Fix handling of partial writes in dump_emit()
Quoted 4 lines · Linus Torvalds
Quoted 2 lines · Jann Horn
After a partial write, we have to update the input buffer pointer.
Quoted 8 lines
Interesting. It seems this partial write case never triggers (except for actually killing the core-dump).
Or did you find a case where it actually matters?
Your fix is obviously correct, but it also makes me go "that function clearly never actually worked for partial writes, maybe we shouldn't even bother?"
Hmm, yeah... I can't really think of cases where write handlers can spuriously return early without having a pending signal, and a second write is likely to succeed... I just know that there are some things that are notorious for returning short *reads* (e.g. pipes, sockets, /proc/$pid/maps).
Al's commit message refers to pipes specifically; but even at commit 2507a4fbd48a, I don't actually see where pipe_write() could return a short write without a page allocation failure or something like that.
So maybe you're right and we should just get rid of it...
Re: [PATCH 2/5] coredump: Fix handling of partial writes in dump_emit()
In reply to Linus TorvaldsQuoted 4 lines · Linus Torvalds
Quoted 2 lines · Jann Horn
After a partial write, we have to update the input buffer pointer.
Quoted 8 lines
Interesting. It seems this partial write case never triggers (except for actually killing the core-dump).
Or did you find a case where it actually matters?
Your fix is obviously correct, but it also makes me go "that function clearly never actually worked for partial writes, maybe we shouldn't even bother?"
Writes to a local filesystem should never be short unless disk full/error.
Once upon a time this was yet another thing that NFS could break that no other filesystem would break, but I dunno about now? (I think the page cache collates it and defers the flush until the error can't be reported back anyway?)
Rob
Re: [PATCH 2/5] coredump: Fix handling of partial writes in dump_emit()
Quoted 2 lines · Rob Landley
Writes to a local filesystem should never be short unless disk full/error.
Well, that code is definitely supposed to also write to pipes.
But it also has "was I interrupted" logic, which stops the core dump.
So short writes can very much happen, it's just that they also imply that the core dump should be aborted.
So the loop seems to be unnecessary. The situations where short writes can happen are all the same situations where we want to abort anyway, so the loop count should probably always be just one.
The same would go for any potential network filesystem with the traditional NFS intr-like behavior.
Linus
PATCH 3/5 coredump: Refactor page range dumping into common helper
In reply to Jann HornBoth fs/binfmt_elf.c and fs/binfmt_elf_fdpic.c need to dump ranges of pages into the coredump file. Extract that logic into a common helper.
Any other binfmt that actually wants to create coredumps will probably need the same function; so stop making get_dump_page() depend on CONFIG_ELF_CORE.
--- fs/binfmt_elf.c | 22 ++-------------------- fs/binfmt_elf_fdpic.c | 18 +++--------------- fs/coredump.c | 33 +++++++++++++++++++++++++++++++++ include/linux/coredump.h | 2 ++ mm/gup.c | 2 -- 5 files changed, 40 insertions(+), 37 deletions(-)
fs/binfmt_elf.c +2 -20
@@ -2323,26 +2323,8 @@ static int elf_core_dump(struct coredump_params *cprm)for (i =0 , vma =first_vma (current, gate_vma); vma !=NULL ; vma =next_vma (vma, gate_vma)) { -unsigned long addr; -unsigned long end; - - end = vma->vm_start + vma_filesz[i++]; - -for (addr = vma->vm_start; addr < end; addr +=PAGE_SIZE ) { -struct page *page; -int stop; - - page =get_dump_page (addr); -if (page) { -void *kaddr =kmap (page); - stop = !dump_emit (cprm, kaddr,PAGE_SIZE ); -kunmap (page); -put_page (page); - }else - stop = !dump_skip (cprm,PAGE_SIZE ); -if (stop) - goto cleanup; - } +if (!dump_user_range (cprm, vma->vm_start, vma_filesz[i++])) + goto cleanup; }dump_truncate (cprm);
fs/binfmt_elf_fdpic.c +3 -15
@@ -1500,21 +1500,9 @@ static bool elf_fdpic_dump_segments(struct coredump_params *cprm)if (!maydump (vma, cprm->mm_flags))continue ; -for (addr = vma->vm_start; addr < vma->vm_end; - addr +=PAGE_SIZE ) { -bool res; -struct page *page =get_dump_page (addr); -if (page) { -void *kaddr =kmap (page); - res =dump_emit (cprm, kaddr,PAGE_SIZE ); -kunmap (page); -put_page (page); - }else { - res =dump_skip (cprm,PAGE_SIZE ); - } -if (!res) -return false; - } +if (!dump_user_range (cprm, vma->vm_start, + vma->vma_end - vma->vm_start)) +return false; }return true; }
fs/coredump.c +33 -0
@@ -860,6 +860,39 @@ int dump_skip(struct coredump_params *cprm, size_t nr) }EXPORT_SYMBOL (dump_skip); +#ifdef CONFIG_ELF_CORE +int dump_user_range (struct coredump_params *cprm,unsigned long start, +unsigned long len) +{ +unsigned long addr; + +for (addr = start; addr < start + len; addr +=PAGE_SIZE ) { +struct page *page; +int stop; + +/* +* To avoid having to allocate page tables for virtual address +* ranges that have never been used yet, use a helper that +* returns NULL when encountering an empty page table entry that +* would otherwise have been filled with the zero page. +*/ + page =get_dump_page (addr); +if (page) { +void *kaddr =kmap (page); + + stop = !dump_emit (cprm, kaddr,PAGE_SIZE ); +kunmap (page); +put_page (page); + }else { + stop = !dump_skip (cprm,PAGE_SIZE ); + } +if (stop) +return 0 ; + } +return 1 ; +} +#endif +int dump_align (struct coredump_params *cprm,int align) {unsigned mod = cprm->pos & (align -1 );
include/linux/coredump.h +2 -0
@@ -16,6 +16,8 @@ extern int dump_skip(struct coredump_params *cprm, size_t nr);extern int dump_emit (struct coredump_params *cprm,const void *addr,int nr);extern int dump_align (struct coredump_params *cprm,int align);extern void dump_truncate (struct coredump_params *cprm); +int dump_user_range (struct coredump_params *cprm,unsigned long start, +unsigned long len);#ifdef CONFIG_COREDUMP extern void do_coredump (const kernel_siginfo_t *siginfo);#else
mm/gup.c +0 -2
@@ -1550,7 +1550,6 @@ static long __get_user_pages_locked(struct task_struct *tsk,* * Called without mmap_sem, but after all other threads have been killed. */ -#ifdef CONFIG_ELF_CORE struct page *get_dump_page (unsigned long addr) {struct vm_area_struct *vma;
@@ -1563,7 +1562,6 @@ struct page *get_dump_page(unsigned long addr)flush_cache_page (vma, addr,page_to_pfn (page));return page; } -#endif /* CONFIG_ELF_CORE */ #if defined(CONFIG_FS_DAX ) || defined (CONFIG_CMA )static bool check_dax_vmas (struct vm_area_struct **vmas,long nr_pages)
Signature · 1 line
2.26.2.303.gf8c07b1a785-goog
PATCH 4/5 binfmt_elf, binfmt_elf_fdpic: Use a VMA list snapshot
In reply to Jann HornIn both binfmt_elf and binfmt_elf_fdpic, use a new helper dump_vma_snapshot() to take a snapshot of the VMA list (including the gate VMA, if we have one) while protected by the mmap_sem, and then use that snapshot instead of walking the VMA list without locking.
An alternative approach would be to keep the mmap_sem held across the entire core dumping operation; however, keeping the mmap_sem locked while we may be blocked for an unbounded amount of time (e.g. because we're dumping to a FUSE filesystem or so) isn't really optimal; the mmap_sem blocks things like the ->release handler of userfaultfd, and we don't really want critical system daemons to grind to a halt just because someone "gifted" them SCM_RIGHTS to an eternally-locked userfaultfd, or something like that.
Since both the normal ELF code and the FDPIC ELF code need this functionality (and if any other binfmt wants to add coredump support in the future, they'd probably need it, too), implement this with a common helper in fs/coredump.c.
A downside of this approach is that we now need a bigger amount of kernel memory per userspace VMA in the normal ELF case, and that we need O(n) kernel memory in the FDPIC ELF case at all; but 40 bytes per VMA shouldn't be terribly bad.
--- fs/binfmt_elf.c | 152 +++++++++++++-------------------------- fs/binfmt_elf_fdpic.c | 86 ++++++++++------------ fs/coredump.c | 68 ++++++++++++++++++ include/linux/coredump.h | 10 +++ 4 files changed, 168 insertions(+), 148 deletions(-)
fs/binfmt_elf.c +50 -101
@@ -1292,8 +1292,12 @@ static bool always_dump_vma(struct vm_area_struct *vma)return false; } +#define DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER 1 +/* * Decide what to dump of a segment, part, all or none. +* The result must be fixed up via vma_dump_size_fixup() once we're in a context +* that's allowed to sleep arbitrarily long. */ static unsigned long vma_dump_size (struct vm_area_struct *vma,unsigned long mm_flags)
@@ -1348,30 +1352,15 @@ static unsigned long vma_dump_size(struct vm_area_struct *vma,/* * If this looks like the beginning of a DSO or executable mapping, -* check for an ELF header. If we find one, dump the first page to -* aid in determining what was mapped here. +* we'll check for an ELF header. If we find one, we'll dump the first +* page to aid in determining what was mapped here. +* However, we shouldn't sleep on userspace reads while holding the +* mmap_sem, so we just return a placeholder for now that will be fixed +* up later in vma_dump_size_fixup(). */ if (FILTER (ELF_HEADERS ) && - vma->vm_pgoff =0 && (vma->vm_flags &VM_READ )) { -u32 __user *header = (u32 __user *) vma->vm_start; -u32 word; -/* -* Doing it this way gets the constant folded by GCC. -*/ -union { -u32 cmp; -char elfmag[SELFMAG ]; - } magic; -BUILD_BUG_ON (SELFMAG !=sizeof word); - magic.elfmag[EI_MAG0 ] =ELFMAG0 ; - magic.elfmag[EI_MAG1 ] =ELFMAG1 ; - magic.elfmag[EI_MAG2 ] =ELFMAG2 ; - magic.elfmag[EI_MAG3 ] =ELFMAG3 ; -if (unlikely (get_user (word, header))) - word =0 ; -if (word = magic.cmp) -return PAGE_SIZE ; - } + vma->vm_pgoff =0 && (vma->vm_flags &VM_READ )) +return DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER ;#undef FILTER
@@ -1381,6 +1370,22 @@ static unsigned long vma_dump_size(struct vm_area_struct *vma,return vma->vm_end - vma->vm_start; } +/* Fix up the result from vma_dump_size(), now that we're allowed to sleep. */ +static void vma_dump_size_fixup (struct core_vma_metadata *meta) +{ +char elfmag[SELFMAG ]; + +if (meta->dump_size !=DUMP_SIZE_MAYBE_ELFHDR_PLACEHOLDER ) +return ; + +if (copy_from_user (elfmag, (void __user *)meta->start,SELFMAG )) { + meta->dump_size =0 ; +return ; + } + meta->dump_size + (memcmp (elfmag,ELFMAG ,SELFMAG ) =0 ) ?PAGE_SIZE :0 ; +} +/* An ELF note in memory */ struct memelfnote {
@@ -2124,32 +2129,6 @@ static void free_note_info(struct elf_note_info *info)#endif -static struct vm_area_struct *first_vma (struct task_struct *tsk, -struct vm_area_struct *gate_vma) -{ -struct vm_area_struct *ret = tsk->mm->mmap; - -if (ret) -return ret; -return gate_vma; -} -/* -* Helper function for iterating across a vma list. It ensures that the caller -* will visit `gate_vma' prior to terminating the search. -*/ -static struct vm_area_struct *next_vma (struct vm_area_struct *this_vma, -struct vm_area_struct *gate_vma) -{ -struct vm_area_struct *ret; - - ret = this_vma->vm_next; -if (ret) -return ret; -if (this_vma = gate_vma) -return NULL ; -return gate_vma; -} -static void fill_extnum_info (struct elfhdr *elf,struct elf_shdr *shdr4extnum,elf_addr_t e_shoff,int segs) {
@@ -2176,9 +2155,8 @@ static void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr4extnum,static int elf_core_dump (struct coredump_params *cprm) {int has_dumped =0 ; -int segs, i; +int vma_count, segs, i;size_t vma_data_size =0 ; -struct vm_area_struct *vma, *gate_vma;struct elfhdr elf;loff_t offset =0 , dataoff;struct elf_note_info info = { };
@@ -2186,30 +2164,21 @@ static int elf_core_dump(struct coredump_params *cprm)struct elf_shdr *shdr4extnum =NULL ;Elf_Half e_phnum;elf_addr_t e_shoff; -elf_addr_t *vma_filesz =NULL ; +struct core_vma_metadata *vma_meta; + +if (dump_vma_snapshot (cprm, &vma_count, &vma_meta, vma_dump_size)) +return 0 ; + +for (i =0 ; i < vma_count; i++) { +vma_dump_size_fixup (vma_meta + i); + vma_data_size += vma_meta[i].dump_size; + } -/* -* We no longer stop all VM operations. -* -* This is because those proceses that could possibly change map_count -* or the mmap / vma pages are now blocked in do_exit on current -* finishing this core dump. -* -* Only ptrace can touch these memory addresses, but it doesn't change -* the map_count or the pages allocated. So no possibility of crashing -* exists while dumping the mm->vm_next areas to the core file. -*/ -/* * The number of segs are recored into ELF header as 16bit value. * Please check DEFAULT_MAX_MAP_COUNT definition when you modify here. */ - segs = current->mm->map_count; - segs +=elf_core_extra_phdrs (); - - gate_vma =get_gate_vma (current->mm); -if (gate_vma !=NULL ) - segs++; + segs = vma_count +elf_core_extra_phdrs ();/* for notes section */ segs++;
@@ -2247,24 +2216,6 @@ static int elf_core_dump(struct coredump_params *cprm) dataoff = offset =roundup (offset,ELF_EXEC_PAGESIZE ); -/* -* 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 (!vma_filesz) - goto cleanup; - -for (i =0 , vma =first_vma (current, gate_vma); vma !=NULL ; - vma =next_vma (vma, gate_vma)) { -unsigned long dump_size; - - dump_size =vma_dump_size (vma, cprm->mm_flags); - vma_filesz[i++] = dump_size; - vma_data_size += dump_size; - } - offset += vma_data_size; offset +=elf_core_extra_data_size (); e_shoff = offset;
@@ -2285,22 +2236,20 @@ static int elf_core_dump(struct coredump_params *cprm) goto cleanup;/* Write program headers for segments dump */ -for (i =0 , vma =first_vma (current, gate_vma); vma !=NULL ; - vma =next_vma (vma, gate_vma)) { +for (i =0 ; i < vma_count; i++) { +struct core_vma_metadata *meta = vma_meta + i;struct elf_phdr phdr; phdr.p_type =PT_LOAD ; phdr.p_offset = offset; - phdr.p_vaddr = vma->vm_start; + phdr.p_vaddr = meta->start; phdr.p_paddr =0 ; - phdr.p_filesz = vma_filesz[i++]; - phdr.p_memsz = vma->vm_end - vma->vm_start; + phdr.p_filesz = meta->dump_size; + phdr.p_memsz = meta->end - meta->start; offset += phdr.p_filesz; - phdr.p_flags = vma->vm_flags &VM_READ ?PF_R :0 ; -if (vma->vm_flags &VM_WRITE ) - phdr.p_flags |=PF_W ; -if (vma->vm_flags &VM_EXEC ) - phdr.p_flags |=PF_X ; + phdr.p_flags = meta->flags &VM_READ ?PF_R :0 ; + phdr.p_flags |= meta->flags &VM_WRITE ?PF_W :0 ; + phdr.p_flags |= meta->flags &VM_EXEC ?PF_X :0 ; phdr.p_align =ELF_EXEC_PAGESIZE ;if (!dump_emit (cprm, &phdr,sizeof (phdr)))
@@ -2321,9 +2270,10 @@ static int elf_core_dump(struct coredump_params *cprm)if (!dump_skip (cprm, dataoff - cprm->pos)) goto cleanup; -for (i =0 , vma =first_vma (current, gate_vma); vma !=NULL ; - vma =next_vma (vma, gate_vma)) { -if (!dump_user_range (cprm, vma->vm_start, vma_filesz[i++])) +for (i =0 ; i < vma_count; i++) { +struct core_vma_metadata *meta = vma_meta + i; + +if (!dump_user_range (cprm, meta->start, meta->dump_size)) goto cleanup; }dump_truncate (cprm);
@@ -2339,7 +2289,7 @@ static int elf_core_dump(struct coredump_params *cprm) cleanup:free_note_info (&info);kfree (shdr4extnum); -kvfree (vma_filesz); +kvfree (vma_meta);kfree (phdr4note);return has_dumped; }
fs/binfmt_elf_fdpic.c +39 -47
196 lines not on this page · raw message
fs/coredump.c +68 -0
71 lines not on this page · raw message
include/linux/coredump.h +10 -0
@@ -7,6 +7,13 @@#include <linux/fs.h> #include <asm/siginfo.h> +struct core_vma_metadata { +unsigned long start, end; +unsigned long filesize; +unsigned long flags; +unsigned long dump_size; +}; +/* * These are the only things you should do on a core-file: use only these * functions to write out all the necessary info.
@@ -18,6 +25,9 @@ extern int dump_align(struct coredump_params *cprm, int align);extern void dump_truncate (struct coredump_params *cprm);int dump_user_range (struct coredump_params *cprm,unsigned long start,unsigned long len); +int dump_vma_snapshot (struct coredump_params *cprm,int *vma_count, +struct core_vma_metadata **vma_meta, +unsigned long (*dump_size_cb)(struct vm_area_struct *,unsigned long ));#ifdef CONFIG_COREDUMP extern void do_coredump (const kernel_siginfo_t *siginfo);#else
Signature · 1 line
2.26.2.303.gf8c07b1a785-goog
PATCH 5/5 mm/gup: Take mmap_sem in get_dump_page()
In reply to Jann HornProperly take the mmap_sem before calling into the GUP code from get_dump_page(); and play nice, allowing __get_user_pages_locked() to drop the mmap_sem if it has to sleep.
This requires adjusting the check in __get_user_pages_locked() to be slightly less strict: While vmas != NULL is normally incompatible with the lock-dropping retry logic, it's fine if we only want a single page, because then retries can only happen when we haven't grabbed any pages yet.
--- mm/gup.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-)
mm/gup.c +16 -5
@@ -1261,7 +1261,8 @@ static __always_inline long __get_user_pages_locked(struct task_struct *tsk,if (locked) {/* if VM_FAULT_RETRY can be returned, vmas become invalid */ -BUG_ON (vmas); +if (WARN_ON (vmas && nr_pages !=1 )) +return -EFAULT ;/* check caller initialized locked */ BUG_ON (*locked !=1 ); }
@@ -1548,18 +1549,28 @@ static long __get_user_pages_locked(struct task_struct *tsk,* NULL wherever the ZERO_PAGE, or an anonymous pte_none, has been found - * allowing a hole to be left in the corefile to save diskspace. * -* Called without mmap_sem, but after all other threads have been killed. +* Called without mmap_sem (takes and releases the mmap_sem by itself). */ struct page *get_dump_page (unsigned long addr) { +struct mm_struct *mm = current->mm;struct vm_area_struct *vma;struct page *page; +int locked =1 ; +int ret; -if (__get_user_pages (current, current->mm, addr,1 , -FOLL_FORCE |FOLL_DUMP |FOLL_GET , &page, &vma, -NULL ) <1 ) +if (down_read_killable (&mm->mmap_sem)) +return NULL ; + ret =__get_user_pages_locked (current, mm, addr,1 , &page, &vma, + &locked, +FOLL_FORCE |FOLL_DUMP |FOLL_GET ); +if (ret !=1 ) { +if (locked) +up_read (&mm->mmap_sem);return NULL ; + }flush_cache_page (vma, addr,page_to_pfn (page)); +up_read (&mm->mmap_sem);return page; }
Signature · 1 line
2.26.2.303.gf8c07b1a785-goog
Re: [PATCH 5/5] mm/gup: Take mmap_sem in get_dump_page()
Quoted 4 lines · Jann Horn
Properly take the
mmap_sembefore calling into the GUP code fromget_dump_page(); and play nice, allowing__get_user_pages_locked()to drop themmap_semif it has to sleep.
This makes my skin crawl.
The only reason for this all is that page cache flushing.
My gut feeling is that it should be done by get_user_pages() anyway, since all the other users presumably want it to be coherent in the cache.
And in fact, looking at __get_user_pages(), it already does that
if (pages) {
pages[i] = page;
flush_anon_page(vma, page, start);
flush_dcache_page(page);
ctx.page_mask = 0;
}and I think that the get_dump_page() logic is unnecessary to begin with.
Linus
Re: [PATCH 5/5] mm/gup: Take mmap_sem in get_dump_page()
Quoted 16 lines · Linus Torvalds
Quoted 4 lines · Jann Horn
Properly take the
mmap_sembefore calling into the GUP code fromget_dump_page(); and play nice, allowing__get_user_pages_locked()to drop themmap_semif it has to sleep.This makes my skin crawl.
The only reason for this all is that page cache flushing.
My gut feeling is that it should be done by
get_user_pages()anyway, since all the other users presumably want it to be coherent in the cache.And in fact, looking at
__get_user_pages(), it already does that
Quoted 8 lines
if (pages) { pages[i] = page; flush_anon_page(vma, page, start); flush_dcache_page(page); ctx.page_mask = 0; }and I think that the
get_dump_page()logic is unnecessary to begin with.
Ah! And even though flush_cache_page() is broader than flush_dcache_page(), that's actually unnecessary, right? Since the kernel only wants to read from the page, and therefore e.g. the icache is irrelevant?
Yay! :) I did think this was a bit gnarly, and it's nice to know that this can be simplified.
(And now I'm going to avert my eyes from the GUP code before I start thinking too hard about how much it sucks that FOLL_LONGTERM doesn't drop the mmap_sem across the access and how much I dislike the whole idea of FOLL_LONGTERM in general...)