linux/arch/mips/mm
Linus Torvalds 9471f1f2f5 Merge branch 'expand-stack'
This modifies our user mode stack expansion code to always take the
mmap_lock for writing before modifying the VM layout.

It's actually something we always technically should have done, but
because we didn't strictly need it, we were being lazy ("opportunistic"
sounds so much better, doesn't it?) about things, and had this hack in
place where we would extend the stack vma in-place without doing the
proper locking.

And it worked fine.  We just needed to change vm_start (or, in the case
of grow-up stacks, vm_end) and together with some special ad-hoc locking
using the anon_vma lock and the mm->page_table_lock, it all was fairly
straightforward.

That is, it was all fine until Ruihan Li pointed out that now that the
vma layout uses the maple tree code, we *really* don't just change
vm_start and vm_end any more, and the locking really is broken.  Oops.

It's not actually all _that_ horrible to fix this once and for all, and
do proper locking, but it's a bit painful.  We have basically three
different cases of stack expansion, and they all work just a bit
differently:

 - the common and obvious case is the page fault handling. It's actually
   fairly simple and straightforward, except for the fact that we have
   something like 24 different versions of it, and you end up in a maze
   of twisty little passages, all alike.

 - the simplest case is the execve() code that creates a new stack.
   There are no real locking concerns because it's all in a private new
   VM that hasn't been exposed to anybody, but lockdep still can end up
   unhappy if you get it wrong.

 - and finally, we have GUP and page pinning, which shouldn't really be
   expanding the stack in the first place, but in addition to execve()
   we also use it for ptrace(). And debuggers do want to possibly access
   memory under the stack pointer and thus need to be able to expand the
   stack as a special case.

None of these cases are exactly complicated, but the page fault case in
particular is just repeated slightly differently many many times.  And
ia64 in particular has a fairly complicated situation where you can have
both a regular grow-down stack _and_ a special grow-up stack for the
register backing store.

So to make this slightly more manageable, the bulk of this series is to
first create a helper function for the most common page fault case, and
convert all the straightforward architectures to it.

Thus the new 'lock_mm_and_find_vma()' helper function, which ends up
being used by x86, arm, powerpc, mips, riscv, alpha, arc, csky, hexagon,
loongarch, nios2, sh, sparc32, and xtensa.  So we not only convert more
than half the architectures, we now have more shared code and avoid some
of those twisty little passages.

And largely due to this common helper function, the full diffstat of
this series ends up deleting more lines than it adds.

That still leaves eight architectures (ia64, m68k, microblaze, openrisc,
parisc, s390, sparc64 and um) that end up doing 'expand_stack()'
manually because they are doing something slightly different from the
normal pattern.  Along with the couple of special cases in execve() and
GUP.

So there's a couple of patches that first create 'locked' helper
versions of the stack expansion functions, so that there's a obvious
path forward in the conversion.  The execve() case is then actually
pretty simple, and is a nice cleanup from our old "grow-up stackls are
special, because at execve time even they grow down".

The #ifdef CONFIG_STACK_GROWSUP in that code just goes away, because
it's just more straightforward to write out the stack expansion there
manually, instead od having get_user_pages_remote() do it for us in some
situations but not others and have to worry about locking rules for GUP.

And the final step is then to just convert the remaining odd cases to a
new world order where 'expand_stack()' is called with the mmap_lock held
for reading, but where it might drop it and upgrade it to a write, only
to return with it held for reading (in the success case) or with it
completely dropped (in the failure case).

In the process, we remove all the stack expansion from GUP (where
dropping the lock wouldn't be ok without special rules anyway), and add
it in manually to __access_remote_vm() for ptrace().

Thanks to Adrian Glaubitz and Frank Scheiner who tested the ia64 cases.
Everything else here felt pretty straightforward, but the ia64 rules for
stack expansion are really quite odd and very different from everything
else.  Also thanks to Vegard Nossum who caught me getting one of those
odd conditions entirely the wrong way around.

Anyway, I think I want to actually move all the stack expansion code to
a whole new file of its own, rather than have it split up between
mm/mmap.c and mm/memory.c, but since this will have to be backported to
the initial maple tree vma introduction anyway, I tried to keep the
patches _fairly_ minimal.

Also, while I don't think it's valid to expand the stack from GUP, the
final patch in here is a "warn if some crazy GUP user wants to try to
expand the stack" patch.  That one will be reverted before the final
release, but it's left to catch any odd cases during the merge window
and release candidates.

Reported-by: Ruihan Li <lrh2000@pku.edu.cn>

* branch 'expand-stack':
  gup: add warning if some caller would seem to want stack expansion
  mm: always expand the stack with the mmap write lock held
  execve: expand new process stack manually ahead of time
  mm: make find_extend_vma() fail if write lock not held
  powerpc/mm: convert coprocessor fault to lock_mm_and_find_vma()
  mm/fault: convert remaining simple cases to lock_mm_and_find_vma()
  arm/mm: Convert to using lock_mm_and_find_vma()
  riscv/mm: Convert to using lock_mm_and_find_vma()
  mips/mm: Convert to using lock_mm_and_find_vma()
  powerpc/mm: Convert to using lock_mm_and_find_vma()
  arm64/mm: Convert to using lock_mm_and_find_vma()
  mm: make the page fault mmap locking killable
  mm: introduce new 'lock_mm_and_find_vma()' page fault helper
2023-06-28 20:35:21 -07:00
..
c-octeon.c MIPS: c-octeon: Provide alternative SMP cache flush function 2023-04-05 09:45:09 +02:00
c-r3k.c MIPS: mm: Remove local_cache_flush_page 2023-04-05 10:08:53 +02:00
c-r4k.c MIPS: mm: Remove local_cache_flush_page 2023-04-05 10:08:53 +02:00
cache.c MIPS: mm: Remove local_cache_flush_page 2023-04-05 10:08:53 +02:00
cerr-sb1.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 156 2019-05-30 11:26:35 -07:00
cex-gen.S
cex-oct.S
cex-sb1.S treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 156 2019-05-30 11:26:35 -07:00
context.c MIPS: mm: Use the bitmap API to allocate bitmaps 2022-07-11 10:28:12 +02:00
dma-noncoherent.c MIPS: mm: XBurst CPU requires sync after DMA 2021-06-01 11:44:46 +02:00
extable.c Replace <asm/uaccess.h> with <linux/uaccess.h> globally 2016-12-24 11:46:01 -08:00
fault.c mips/mm: Convert to using lock_mm_and_find_vma() 2023-06-24 14:12:58 -07:00
highmem.c mips/mm/highmem: Switch to generic kmap atomic 2020-11-06 23:14:56 +01:00
hugetlbpage.c hugetlb: pass vma into huge_pte_alloc() and huge_pmd_share() 2021-05-05 11:27:20 -07:00
init.c mm: percpu: add generic pcpu_fc_alloc/free funciton 2022-01-20 08:52:52 +02:00
ioremap.c MIPS: use ioremap_page_range 2020-04-19 16:13:18 +02:00
ioremap64.c MIPS: move ioremap_prot und iounmap out of line 2020-04-19 16:12:31 +02:00
maccess.c MIPS: uaccess: Added __get/__put_kernel_nofault 2021-04-06 14:36:56 +02:00
Makefile MIPS: Remove TX39XX support 2022-03-01 10:07:22 +01:00
mmap.c mips: use generic mmap top-down layout and brk randomization 2019-09-24 15:54:12 -07:00
page-funcs.S MIPS: Export {copy, clear}_page functions alongside their definitions 2017-01-03 16:48:39 +01:00
page.c MIPS: remove asm/war.h 2022-02-22 09:35:49 +01:00
pgtable-32.c MIPS&LoongArch&NIOS2: adjust prototypes of p?d_init() 2022-12-11 18:12:11 -08:00
pgtable-64.c MIPS&LoongArch&NIOS2: adjust prototypes of p?d_init() 2022-12-11 18:12:11 -08:00
pgtable.c MIPS&LoongArch&NIOS2: adjust prototypes of p?d_init() 2022-12-11 18:12:11 -08:00
physaddr.c MIPS: Fixed __debug_virt_addr_valid() 2022-07-15 14:22:51 +02:00
sc-debugfs.c treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152 2019-05-30 11:26:32 -07:00
sc-ip22.c mm: don't include asm/pgtable.h if linux/mm.h is already included 2020-06-09 09:39:13 -07:00
sc-mips.c mips: fix Section mismatch in reference 2020-12-14 16:39:42 +01:00
sc-r5k.c mm: don't include asm/pgtable.h if linux/mm.h is already included 2020-06-09 09:39:13 -07:00
sc-rm7k.c License cleanup: add SPDX GPL-2.0 license identifier to files with no license 2017-11-02 11:10:55 +01:00
tlb-funcs.S MIPS: Consistently declare TLB functions 2018-08-10 17:27:53 -07:00
tlb-r3k.c MIPS: Remove TX39XX support 2022-03-01 10:07:22 +01:00
tlb-r4k.c mips: add pte_unmap() to balance pte_offset_map() 2023-06-19 16:19:07 -07:00
tlbex-fault.S MIPS: Add DWARF unwinding to assembly 2017-09-06 11:01:52 +02:00
tlbex.c added support for Netgear WNR3500L v2 2022-08-06 16:47:19 -07:00
uasm-micromips.c mips: bpf: implement jitting of BPF_ALU | BPF_ARSH | BPF_X 2018-12-07 13:30:48 -08:00
uasm-mips.c mips, uasm: Enable muhu opcode for MIPS R6 2021-10-06 12:28:02 -07:00
uasm.c mips, uasm: Enable muhu opcode for MIPS R6 2021-10-06 12:28:02 -07:00