On Wed, Mar 6, 2024 at 5:13 PM Alexei Starovoitov <alexei.starovoitov@xxxxxxxxx> wrote: > > On Wed, Mar 6, 2024 at 1:46 PM Pasha Tatashin <pasha.tatashin@xxxxxxxxxx> wrote: > > > > > > This interface and in general VM_SPARSE would be useful for > > > > dynamically grown kernel stacks [1]. However, the might_sleep() here > > > > would be a problem. We would need to be able to handle > > > > vm_area_map_pages() from interrupt disabled context therefore no > > > > sleeping. The caller would need to guarantee that the page tables are > > > > pre-allocated before the mapping. > > > > > > Sounds like we'd need to differentiate two kinds of sparse regions. > > > One that is really sparse where page tables are not populated (bpf use case) > > > and another where only the pte level might be empty. > > > Only the latter one will be usable for such auto-grow stacks. > > > > > > Months back I played with this idea: > > > https://git.kernel.org/pub/scm/linux/kernel/git/ast/bpf.git/commit/?&id=ce63949a879f2f26c1c1834303e6dfbfb79d1fbd > > > that > > > "Make vmap_pages_range() allocate page tables down to the last (PTE) level." > > > Essentially pass NULL instead of 'pages' into vmap_pages_range() > > > and it will populate all levels except the last. > > > > Yes, this is what is needed, however, it can be a little simpler with > > kernel stacks: > > given that the first page in the vm_area is mapped when stack is first > > allocated, and that the VA range is aligned to 16K, we actually are > > guaranteed to have all page table levels down to pte pre-allocated > > during that initial mapping. Therefore, we do not need to worry about > > allocating them later during PFs. > > Ahh. Found: > stack = __vmalloc_node_range(THREAD_SIZE, THREAD_ALIGN, ... > > > > Then the page fault handler can service a fault in auto-growing stack > > > area if it has a page stashed in some per-cpu free list. > > > I suspect this is something you might need for > > > "16k stack that is populated on fault", > > > plus a free list of 3 pages per-cpu, > > > and set_pte_at() in pf handler. > > > > Yes, what you described is exactly what I am working on: using 3-pages > > per-cpu to handle kstack page faults. The only thing that is missing > > is that I would like to have the ability to call a non-sleeping > > version of vm_area_map_pages(). > > vm_area_map_pages() cannot be non-sleepable, since the [start, end) > range will dictate whether mid level allocs and locks are needed. > > Instead in alloc_thread_stack_node() you'd need a flavor > of get_vm_area() that can align the range to THREAD_ALIGN. > Then immediately call _sleepable_ vm_area_map_pages() to populate > the first page and later set_pte_at() the other pages on demand > from the fault handler. We still need to get to PTE level to use set_pte_at(). So, either store it in task_struct for faster PF handling, or add another non-sleeping vmap function that will do something like this: vm_area_set_page_at(addr, page) { pgd = pgd_offset_k(addr) p4d = vunmap_p4d_range(pgd, addr) pud = pud_offset(p4d, addr) pmd = pmd_offset(pud, addr) pte = pte_offset_kernel(pmd, addr) set_pte_at(init_mm, addr, pte, mk_pte(page...)); } Pasha