1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/kernel/fork.c 4 * 5 * Copyright (C) 1991, 1992 Linus Torvalds 6 */ 7 8 /* 9 * 'fork.c' contains the help-routines for the 'fork' system call 10 * (see also entry.S and others). 11 * Fork is rather simple, once you get the hang of it, but the memory 12 * management can be a bitch. See 'mm/memory.c': 'copy_page_range()' 13 */ 14 15 #include <linux/anon_inodes.h> 16 #include <linux/slab.h> 17 #include <linux/sched/autogroup.h> 18 #include <linux/sched/mm.h> 19 #include <linux/sched/user.h> 20 #include <linux/sched/numa_balancing.h> 21 #include <linux/sched/stat.h> 22 #include <linux/sched/task.h> 23 #include <linux/sched/task_stack.h> 24 #include <linux/sched/cputime.h> 25 #include <linux/sched/ext.h> 26 #include <linux/sched/exec_state.h> 27 #include <linux/seq_file.h> 28 #include <linux/rtmutex.h> 29 #include <linux/init.h> 30 #include <linux/unistd.h> 31 #include <linux/module.h> 32 #include <linux/vmalloc.h> 33 #include <linux/completion.h> 34 #include <linux/personality.h> 35 #include <linux/mempolicy.h> 36 #include <linux/sem.h> 37 #include <linux/file.h> 38 #include <linux/fdtable.h> 39 #include <linux/iocontext.h> 40 #include <linux/key.h> 41 #include <linux/kmsan.h> 42 #include <linux/binfmts.h> 43 #include <linux/mman.h> 44 #include <linux/mmu_notifier.h> 45 #include <linux/fs.h> 46 #include <linux/mm.h> 47 #include <linux/mm_inline.h> 48 #include <linux/memblock.h> 49 #include <linux/nsproxy.h> 50 #include <linux/ns/ns_common_types.h> 51 #include <linux/capability.h> 52 #include <linux/cpu.h> 53 #include <linux/cgroup.h> 54 #include <linux/security.h> 55 #include <linux/hugetlb.h> 56 #include <linux/seccomp.h> 57 #include <linux/swap.h> 58 #include <linux/syscalls.h> 59 #include <linux/syscall_user_dispatch.h> 60 #include <linux/jiffies.h> 61 #include <linux/futex.h> 62 #include <linux/compat.h> 63 #include <linux/kthread.h> 64 #include <linux/task_io_accounting_ops.h> 65 #include <linux/rcupdate.h> 66 #include <linux/ptrace.h> 67 #include <linux/mount.h> 68 #include <linux/audit.h> 69 #include <linux/memcontrol.h> 70 #include <linux/ftrace.h> 71 #include <linux/proc_fs.h> 72 #include <linux/profile.h> 73 #include <linux/rmap.h> 74 #include <linux/ksm.h> 75 #include <linux/acct.h> 76 #include <linux/userfaultfd_k.h> 77 #include <linux/tsacct_kern.h> 78 #include <linux/cn_proc.h> 79 #include <linux/freezer.h> 80 #include <linux/delayacct.h> 81 #include <linux/taskstats_kern.h> 82 #include <linux/tty.h> 83 #include <linux/fs_struct.h> 84 #include <linux/magic.h> 85 #include <linux/perf_event.h> 86 #include <linux/posix-timers.h> 87 #include <linux/user-return-notifier.h> 88 #include <linux/oom.h> 89 #include <linux/khugepaged.h> 90 #include <linux/signalfd.h> 91 #include <linux/uprobes.h> 92 #include <linux/aio.h> 93 #include <linux/compiler.h> 94 #include <linux/sysctl.h> 95 #include <linux/kcov.h> 96 #include <linux/livepatch.h> 97 #include <linux/thread_info.h> 98 #include <linux/kstack_erase.h> 99 #include <linux/kasan.h> 100 #include <linux/randomize_kstack.h> 101 #include <linux/scs.h> 102 #include <linux/io_uring.h> 103 #include <linux/io_uring_types.h> 104 #include <linux/bpf.h> 105 #include <linux/stackprotector.h> 106 #include <linux/user_events.h> 107 #include <linux/iommu.h> 108 #include <linux/rseq.h> 109 #include <uapi/linux/pidfd.h> 110 #include <linux/pidfs.h> 111 #include <linux/tick.h> 112 #include <linux/unwind_deferred.h> 113 #include <linux/pgalloc.h> 114 #include <linux/uaccess.h> 115 116 #include <asm/mmu_context.h> 117 #include <asm/cacheflush.h> 118 #include <asm/tlbflush.h> 119 120 /* For dup_mmap(). */ 121 #include "../mm/internal.h" 122 123 #include <trace/events/sched.h> 124 125 #define CREATE_TRACE_POINTS 126 #include <trace/events/task.h> 127 128 #include <kunit/visibility.h> 129 130 /* 131 * Minimum number of threads to boot the kernel 132 */ 133 #define MIN_THREADS 20 134 135 /* 136 * Maximum number of threads 137 */ 138 #define MAX_THREADS FUTEX_TID_MASK 139 140 /* 141 * Protected counters by write_lock_irq(&tasklist_lock) 142 */ 143 unsigned long total_forks; /* Handle normal Linux uptimes. */ 144 int nr_threads; /* The idle threads do not count.. */ 145 146 static int max_threads; /* tunable limit on nr_threads */ 147 148 #define NAMED_ARRAY_INDEX(x) [x] = __stringify(x) 149 150 static const char * const resident_page_types[] = { 151 NAMED_ARRAY_INDEX(MM_FILEPAGES), 152 NAMED_ARRAY_INDEX(MM_ANONPAGES), 153 NAMED_ARRAY_INDEX(MM_SWAPENTS), 154 NAMED_ARRAY_INDEX(MM_SHMEMPAGES), 155 }; 156 157 DEFINE_PER_CPU(unsigned long, process_counts) = 0; 158 159 __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ 160 161 #ifdef CONFIG_PROVE_RCU 162 int lockdep_tasklist_lock_is_held(void) 163 { 164 return lockdep_is_held(&tasklist_lock); 165 } 166 EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held); 167 #endif /* #ifdef CONFIG_PROVE_RCU */ 168 169 int nr_processes(void) 170 { 171 int cpu; 172 int total = 0; 173 174 for_each_possible_cpu(cpu) 175 total += per_cpu(process_counts, cpu); 176 177 return total; 178 } 179 180 void __weak arch_release_task_struct(struct task_struct *tsk) 181 { 182 } 183 184 static struct kmem_cache *task_struct_cachep; 185 186 static inline struct task_struct *alloc_task_struct_node(int node) 187 { 188 return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node); 189 } 190 191 static inline void free_task_struct(struct task_struct *tsk) 192 { 193 kmem_cache_free(task_struct_cachep, tsk); 194 } 195 196 #ifdef CONFIG_VMAP_STACK 197 /* 198 * vmalloc() is a bit slow, and calling vfree() enough times will force a TLB 199 * flush. Try to minimize the number of calls by caching stacks. 200 */ 201 #define NR_CACHED_STACKS 2 202 static DEFINE_PER_CPU(struct vm_struct *, cached_stacks[NR_CACHED_STACKS]); 203 /* 204 * Allocated stacks are cached and later reused by new threads, so memcg 205 * accounting is performed by the code assigning/releasing stacks to tasks. 206 * We need a zeroed memory without __GFP_ACCOUNT. 207 */ 208 #define GFP_VMAP_STACK (GFP_KERNEL | __GFP_ZERO) 209 210 struct vm_stack { 211 struct rcu_head rcu; 212 struct vm_struct *stack_vm_area; 213 }; 214 215 static struct vm_struct *alloc_thread_stack_node_from_cache(struct task_struct *tsk, int node) 216 { 217 struct vm_struct *vm_area; 218 unsigned int i; 219 220 /* 221 * If the node has memory, we are guaranteed the stacks are backed by local pages. 222 * Otherwise the pages are arbitrary. 223 * 224 * Note that depending on cpuset it is possible we will get migrated to a different 225 * node immediately after allocating here, so this does *not* guarantee locality for 226 * arbitrary callers. 227 */ 228 scoped_guard(preempt) { 229 if (node != NUMA_NO_NODE && numa_node_id() != node) 230 return NULL; 231 232 for (i = 0; i < NR_CACHED_STACKS; i++) { 233 vm_area = this_cpu_xchg(cached_stacks[i], NULL); 234 if (vm_area) 235 return vm_area; 236 } 237 } 238 239 return NULL; 240 } 241 242 static bool try_release_thread_stack_to_cache(struct vm_struct *vm_area) 243 { 244 unsigned int i; 245 int nid; 246 247 /* 248 * Don't cache stacks if any of the pages don't match the local domain, unless 249 * there is no local memory to begin with. 250 * 251 * Note that lack of local memory does not automatically mean it makes no difference 252 * performance-wise which other domain backs the stack. In this case we are merely 253 * trying to avoid constantly going to vmalloc. 254 */ 255 scoped_guard(preempt) { 256 nid = numa_node_id(); 257 if (node_state(nid, N_MEMORY)) { 258 for (i = 0; i < vm_area->nr_pages; i++) { 259 struct page *page = vm_area->pages[i]; 260 if (page_to_nid(page) != nid) 261 return false; 262 } 263 } 264 265 for (i = 0; i < NR_CACHED_STACKS; i++) { 266 struct vm_struct *tmp = NULL; 267 268 if (this_cpu_try_cmpxchg(cached_stacks[i], &tmp, vm_area)) 269 return true; 270 } 271 } 272 return false; 273 } 274 275 static void thread_stack_free_rcu(struct rcu_head *rh) 276 { 277 struct vm_stack *vm_stack = container_of(rh, struct vm_stack, rcu); 278 struct vm_struct *vm_area = vm_stack->stack_vm_area; 279 280 if (try_release_thread_stack_to_cache(vm_stack->stack_vm_area)) 281 return; 282 283 vfree(vm_area->addr); 284 } 285 286 static void thread_stack_delayed_free(struct task_struct *tsk) 287 { 288 struct vm_stack *vm_stack = tsk->stack; 289 290 vm_stack->stack_vm_area = tsk->stack_vm_area; 291 call_rcu(&vm_stack->rcu, thread_stack_free_rcu); 292 } 293 294 static int free_vm_stack_cache(unsigned int cpu) 295 { 296 struct vm_struct **cached_vm_stack_areas = per_cpu_ptr(cached_stacks, cpu); 297 int i; 298 299 for (i = 0; i < NR_CACHED_STACKS; i++) { 300 struct vm_struct *vm_area = cached_vm_stack_areas[i]; 301 302 if (!vm_area) 303 continue; 304 305 vfree(vm_area->addr); 306 cached_vm_stack_areas[i] = NULL; 307 } 308 309 return 0; 310 } 311 312 static int memcg_charge_kernel_stack(struct vm_struct *vm_area) 313 { 314 int i; 315 int ret; 316 int nr_charged = 0; 317 318 BUG_ON(vm_area->nr_pages != THREAD_SIZE / PAGE_SIZE); 319 320 for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) { 321 ret = memcg_kmem_charge_page(vm_area->pages[i], GFP_KERNEL, 0); 322 if (ret) 323 goto err; 324 nr_charged++; 325 } 326 return 0; 327 err: 328 for (i = 0; i < nr_charged; i++) 329 memcg_kmem_uncharge_page(vm_area->pages[i], 0); 330 return ret; 331 } 332 333 static int alloc_thread_stack_node(struct task_struct *tsk, int node) 334 { 335 struct vm_struct *vm_area; 336 void *stack; 337 338 vm_area = alloc_thread_stack_node_from_cache(tsk, node); 339 if (vm_area) { 340 if (memcg_charge_kernel_stack(vm_area)) { 341 vfree(vm_area->addr); 342 return -ENOMEM; 343 } 344 345 /* Reset stack metadata. */ 346 kasan_unpoison_range(vm_area->addr, THREAD_SIZE); 347 348 stack = kasan_reset_tag(vm_area->addr); 349 350 /* Clear stale pointers from reused stack. */ 351 clear_pages(vm_area->addr, vm_area->nr_pages); 352 353 tsk->stack_vm_area = vm_area; 354 tsk->stack = stack; 355 return 0; 356 } 357 358 stack = __vmalloc_node(THREAD_SIZE, THREAD_ALIGN, 359 GFP_VMAP_STACK, 360 node, __builtin_return_address(0)); 361 if (!stack) 362 return -ENOMEM; 363 364 vm_area = find_vm_area(stack); 365 if (memcg_charge_kernel_stack(vm_area)) { 366 vfree(stack); 367 return -ENOMEM; 368 } 369 /* 370 * We can't call find_vm_area() in interrupt context, and 371 * free_thread_stack() can be called in interrupt context, 372 * so cache the vm_struct. 373 */ 374 tsk->stack_vm_area = vm_area; 375 stack = kasan_reset_tag(stack); 376 tsk->stack = stack; 377 return 0; 378 } 379 380 static void free_thread_stack(struct task_struct *tsk) 381 { 382 if (!try_release_thread_stack_to_cache(tsk->stack_vm_area)) 383 thread_stack_delayed_free(tsk); 384 385 tsk->stack = NULL; 386 tsk->stack_vm_area = NULL; 387 } 388 389 #else /* !CONFIG_VMAP_STACK */ 390 391 /* 392 * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a 393 * kmemcache based allocator. 394 */ 395 #if THREAD_SIZE >= PAGE_SIZE 396 397 static void thread_stack_free_rcu(struct rcu_head *rh) 398 { 399 __free_pages(virt_to_page(rh), THREAD_SIZE_ORDER); 400 } 401 402 static void thread_stack_delayed_free(struct task_struct *tsk) 403 { 404 struct rcu_head *rh = tsk->stack; 405 406 call_rcu(rh, thread_stack_free_rcu); 407 } 408 409 static int alloc_thread_stack_node(struct task_struct *tsk, int node) 410 { 411 struct page *page = alloc_pages_node(node, THREADINFO_GFP, 412 THREAD_SIZE_ORDER); 413 414 if (likely(page)) { 415 tsk->stack = kasan_reset_tag(page_address(page)); 416 return 0; 417 } 418 return -ENOMEM; 419 } 420 421 static void free_thread_stack(struct task_struct *tsk) 422 { 423 thread_stack_delayed_free(tsk); 424 tsk->stack = NULL; 425 } 426 427 #else /* !(THREAD_SIZE >= PAGE_SIZE) */ 428 429 static struct kmem_cache *thread_stack_cache; 430 431 static void thread_stack_free_rcu(struct rcu_head *rh) 432 { 433 kmem_cache_free(thread_stack_cache, rh); 434 } 435 436 static void thread_stack_delayed_free(struct task_struct *tsk) 437 { 438 struct rcu_head *rh = tsk->stack; 439 440 call_rcu(rh, thread_stack_free_rcu); 441 } 442 443 static int alloc_thread_stack_node(struct task_struct *tsk, int node) 444 { 445 unsigned long *stack; 446 stack = kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node); 447 stack = kasan_reset_tag(stack); 448 tsk->stack = stack; 449 return stack ? 0 : -ENOMEM; 450 } 451 452 static void free_thread_stack(struct task_struct *tsk) 453 { 454 thread_stack_delayed_free(tsk); 455 tsk->stack = NULL; 456 } 457 458 void thread_stack_cache_init(void) 459 { 460 thread_stack_cache = kmem_cache_create_usercopy("thread_stack", 461 THREAD_SIZE, THREAD_SIZE, 0, 0, 462 THREAD_SIZE, NULL); 463 BUG_ON(thread_stack_cache == NULL); 464 } 465 466 #endif /* THREAD_SIZE >= PAGE_SIZE */ 467 #endif /* CONFIG_VMAP_STACK */ 468 469 /* SLAB cache for signal_struct structures (tsk->signal) */ 470 static struct kmem_cache *signal_cachep; 471 472 /* SLAB cache for sighand_struct structures (tsk->sighand) */ 473 struct kmem_cache *sighand_cachep; 474 475 /* SLAB cache for files_struct structures (tsk->files) */ 476 struct kmem_cache *files_cachep; 477 478 /* SLAB cache for fs_struct structures (tsk->fs) */ 479 struct kmem_cache *fs_cachep; 480 481 /* SLAB cache for mm_struct structures (tsk->mm) */ 482 static struct kmem_cache *mm_cachep; 483 484 static void account_kernel_stack(struct task_struct *tsk, int account) 485 { 486 if (IS_ENABLED(CONFIG_VMAP_STACK)) { 487 struct vm_struct *vm_area = task_stack_vm_area(tsk); 488 int i; 489 490 for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) 491 mod_lruvec_page_state(vm_area->pages[i], NR_KERNEL_STACK_KB, 492 account * (PAGE_SIZE / 1024)); 493 } else { 494 void *stack = task_stack_page(tsk); 495 496 /* All stack pages are in the same node. */ 497 mod_lruvec_kmem_state(stack, NR_KERNEL_STACK_KB, 498 account * (THREAD_SIZE / 1024)); 499 } 500 } 501 502 void exit_task_stack_account(struct task_struct *tsk) 503 { 504 account_kernel_stack(tsk, -1); 505 506 if (IS_ENABLED(CONFIG_VMAP_STACK)) { 507 struct vm_struct *vm_area; 508 int i; 509 510 vm_area = task_stack_vm_area(tsk); 511 for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) 512 memcg_kmem_uncharge_page(vm_area->pages[i], 0); 513 } 514 } 515 516 static void release_task_stack(struct task_struct *tsk) 517 { 518 if (WARN_ON(READ_ONCE(tsk->__state) != TASK_DEAD)) 519 return; /* Better to leak the stack than to free prematurely */ 520 521 free_thread_stack(tsk); 522 } 523 524 #ifdef CONFIG_THREAD_INFO_IN_TASK 525 void put_task_stack(struct task_struct *tsk) 526 { 527 if (refcount_dec_and_test(&tsk->stack_refcount)) 528 release_task_stack(tsk); 529 } 530 #endif 531 532 void free_task(struct task_struct *tsk) 533 { 534 #ifdef CONFIG_SECCOMP 535 WARN_ON_ONCE(tsk->seccomp.filter); 536 #endif 537 release_user_cpus_ptr(tsk); 538 scs_release(tsk); 539 540 #ifndef CONFIG_THREAD_INFO_IN_TASK 541 /* 542 * The task is finally done with both the stack and thread_info, 543 * so free both. 544 */ 545 release_task_stack(tsk); 546 #else 547 /* 548 * If the task had a separate stack allocation, it should be gone 549 * by now. 550 */ 551 WARN_ON_ONCE(refcount_read(&tsk->stack_refcount) != 0); 552 #endif 553 rt_mutex_debug_task_free(tsk); 554 ftrace_graph_exit_task(tsk); 555 arch_release_task_struct(tsk); 556 if (tsk->flags & PF_KTHREAD) 557 free_kthread_struct(tsk); 558 bpf_task_storage_free(tsk); 559 put_task_exec_state(rcu_access_pointer(tsk->exec_state)); 560 free_task_struct(tsk); 561 } 562 EXPORT_SYMBOL(free_task); 563 564 void dup_mm_exe_file(struct mm_struct *mm, struct mm_struct *oldmm) 565 { 566 struct file *exe_file; 567 568 exe_file = get_mm_exe_file(oldmm); 569 RCU_INIT_POINTER(mm->exe_file, exe_file); 570 /* 571 * We depend on the oldmm having properly denied write access to the 572 * exe_file already. 573 */ 574 if (exe_file && exe_file_deny_write_access(exe_file)) 575 pr_warn_once("exe_file_deny_write_access() failed in %s\n", __func__); 576 } 577 578 #ifdef CONFIG_MMU 579 static inline int mm_alloc_pgd(struct mm_struct *mm) 580 { 581 mm->pgd = pgd_alloc(mm); 582 if (unlikely(!mm->pgd)) 583 return -ENOMEM; 584 return 0; 585 } 586 587 static inline void mm_free_pgd(struct mm_struct *mm) 588 { 589 pgd_free(mm, mm->pgd); 590 } 591 #else 592 #define mm_alloc_pgd(mm) (0) 593 #define mm_free_pgd(mm) 594 #endif /* CONFIG_MMU */ 595 596 #ifdef CONFIG_MM_ID 597 static DEFINE_IDA(mm_ida); 598 599 static inline int mm_alloc_id(struct mm_struct *mm) 600 { 601 int ret; 602 603 ret = ida_alloc_range(&mm_ida, MM_ID_MIN, MM_ID_MAX, GFP_KERNEL); 604 if (ret < 0) 605 return ret; 606 mm->mm_id = ret; 607 return 0; 608 } 609 610 static inline void mm_free_id(struct mm_struct *mm) 611 { 612 const mm_id_t id = mm->mm_id; 613 614 mm->mm_id = MM_ID_DUMMY; 615 if (id == MM_ID_DUMMY) 616 return; 617 if (WARN_ON_ONCE(id < MM_ID_MIN || id > MM_ID_MAX)) 618 return; 619 ida_free(&mm_ida, id); 620 } 621 #else /* !CONFIG_MM_ID */ 622 static inline int mm_alloc_id(struct mm_struct *mm) { return 0; } 623 static inline void mm_free_id(struct mm_struct *mm) {} 624 #endif /* CONFIG_MM_ID */ 625 626 static void check_mm(struct mm_struct *mm) 627 { 628 int i; 629 630 BUILD_BUG_ON_MSG(ARRAY_SIZE(resident_page_types) != NR_MM_COUNTERS, 631 "Please make sure 'struct resident_page_types[]' is updated as well"); 632 633 for (i = 0; i < NR_MM_COUNTERS; i++) { 634 long x = percpu_counter_sum(&mm->rss_stat[i]); 635 636 if (unlikely(x)) { 637 pr_alert("BUG: Bad rss-counter state mm:%p type:%s val:%ld Comm:%s Pid:%d\n", 638 mm, resident_page_types[i], x, 639 current->comm, 640 task_pid_nr(current)); 641 } 642 } 643 644 if (mm_pgtables_bytes(mm)) 645 pr_alert("BUG: non-zero pgtables_bytes on freeing mm: %ld\n", 646 mm_pgtables_bytes(mm)); 647 648 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !defined(CONFIG_SPLIT_PMD_PTLOCKS) 649 VM_BUG_ON_MM(mm->pmd_huge_pte, mm); 650 #endif 651 } 652 653 #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) 654 #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) 655 656 static void do_check_lazy_tlb(void *arg) 657 { 658 struct mm_struct *mm = arg; 659 660 WARN_ON_ONCE(current->active_mm == mm); 661 } 662 663 static void do_shoot_lazy_tlb(void *arg) 664 { 665 struct mm_struct *mm = arg; 666 667 if (current->active_mm == mm) { 668 WARN_ON_ONCE(current->mm); 669 current->active_mm = &init_mm; 670 switch_mm(mm, &init_mm, current); 671 } 672 } 673 674 static void cleanup_lazy_tlbs(struct mm_struct *mm) 675 { 676 if (!IS_ENABLED(CONFIG_MMU_LAZY_TLB_SHOOTDOWN)) { 677 /* 678 * In this case, lazy tlb mms are refounted and would not reach 679 * __mmdrop until all CPUs have switched away and mmdrop()ed. 680 */ 681 return; 682 } 683 684 /* 685 * Lazy mm shootdown does not refcount "lazy tlb mm" usage, rather it 686 * requires lazy mm users to switch to another mm when the refcount 687 * drops to zero, before the mm is freed. This requires IPIs here to 688 * switch kernel threads to init_mm. 689 * 690 * archs that use IPIs to flush TLBs can piggy-back that lazy tlb mm 691 * switch with the final userspace teardown TLB flush which leaves the 692 * mm lazy on this CPU but no others, reducing the need for additional 693 * IPIs here. There are cases where a final IPI is still required here, 694 * such as the final mmdrop being performed on a different CPU than the 695 * one exiting, or kernel threads using the mm when userspace exits. 696 * 697 * IPI overheads have not found to be expensive, but they could be 698 * reduced in a number of possible ways, for example (roughly 699 * increasing order of complexity): 700 * - The last lazy reference created by exit_mm() could instead switch 701 * to init_mm, however it's probable this will run on the same CPU 702 * immediately afterwards, so this may not reduce IPIs much. 703 * - A batch of mms requiring IPIs could be gathered and freed at once. 704 * - CPUs store active_mm where it can be remotely checked without a 705 * lock, to filter out false-positives in the cpumask. 706 * - After mm_users or mm_count reaches zero, switching away from the 707 * mm could clear mm_cpumask to reduce some IPIs, perhaps together 708 * with some batching or delaying of the final IPIs. 709 * - A delayed freeing and RCU-like quiescing sequence based on mm 710 * switching to avoid IPIs completely. 711 */ 712 on_each_cpu_mask(mm_cpumask(mm), do_shoot_lazy_tlb, (void *)mm, 1); 713 if (IS_ENABLED(CONFIG_DEBUG_VM_SHOOT_LAZIES)) 714 on_each_cpu(do_check_lazy_tlb, (void *)mm, 1); 715 } 716 717 /* 718 * Called when the last reference to the mm 719 * is dropped: either by a lazy thread or by 720 * mmput. Free the page directory and the mm. 721 */ 722 void __mmdrop(struct mm_struct *mm) 723 { 724 BUG_ON(mm == &init_mm); 725 WARN_ON_ONCE(mm == current->mm); 726 727 /* Ensure no CPUs are using this as their lazy tlb mm */ 728 cleanup_lazy_tlbs(mm); 729 730 WARN_ON_ONCE(mm == current->active_mm); 731 mm_destroy_sched(mm); 732 mm_free_pgd(mm); 733 mm_free_id(mm); 734 destroy_context(mm); 735 mmu_notifier_subscriptions_destroy(mm); 736 check_mm(mm); 737 mm_pasid_drop(mm); 738 mm_destroy_cid(mm); 739 percpu_counter_destroy_many(mm->rss_stat, NR_MM_COUNTERS); 740 741 free_mm(mm); 742 } 743 EXPORT_SYMBOL_GPL(__mmdrop); 744 745 static void mmdrop_async_fn(struct work_struct *work) 746 { 747 struct mm_struct *mm; 748 749 mm = container_of(work, struct mm_struct, async_put_work); 750 __mmdrop(mm); 751 } 752 753 static void mmdrop_async(struct mm_struct *mm) 754 { 755 if (unlikely(atomic_dec_and_test(&mm->mm_count))) { 756 INIT_WORK(&mm->async_put_work, mmdrop_async_fn); 757 schedule_work(&mm->async_put_work); 758 } 759 } 760 761 static inline void free_signal_struct(struct signal_struct *sig) 762 { 763 taskstats_tgid_free(sig); 764 sched_autogroup_exit(sig); 765 /* 766 * __mmdrop is not safe to call from softirq context on x86 due to 767 * pgd_dtor so postpone it to the async context 768 */ 769 if (sig->oom_mm) 770 mmdrop_async(sig->oom_mm); 771 kmem_cache_free(signal_cachep, sig); 772 } 773 774 static inline void put_signal_struct(struct signal_struct *sig) 775 { 776 if (refcount_dec_and_test(&sig->sigcnt)) 777 free_signal_struct(sig); 778 } 779 780 void __put_task_struct(struct task_struct *tsk) 781 { 782 WARN_ON(!tsk->exit_state); 783 WARN_ON(refcount_read(&tsk->usage)); 784 WARN_ON(tsk == current); 785 786 unwind_task_free(tsk); 787 io_uring_free(tsk); 788 cgroup_task_free(tsk); 789 task_numa_free(tsk, true); 790 security_task_free(tsk); 791 exit_creds(tsk); 792 delayacct_tsk_free(tsk); 793 put_signal_struct(tsk->signal); 794 sched_core_free(tsk); 795 free_task(tsk); 796 } 797 EXPORT_SYMBOL_GPL(__put_task_struct); 798 799 void __put_task_struct_rcu_cb(struct rcu_head *rhp) 800 { 801 struct task_struct *task = container_of(rhp, struct task_struct, rcu); 802 803 __put_task_struct(task); 804 } 805 EXPORT_SYMBOL_GPL(__put_task_struct_rcu_cb); 806 807 void __init __weak arch_task_cache_init(void) { } 808 809 /* 810 * set_max_threads 811 */ 812 static void __init set_max_threads(unsigned int max_threads_suggested) 813 { 814 u64 threads; 815 unsigned long nr_pages = memblock_estimated_nr_free_pages(); 816 817 /* 818 * The number of threads shall be limited such that the thread 819 * structures may only consume a small part of the available memory. 820 */ 821 if (fls64(nr_pages) + fls64(PAGE_SIZE) > 64) 822 threads = MAX_THREADS; 823 else 824 threads = div64_u64((u64) nr_pages * (u64) PAGE_SIZE, 825 (u64) THREAD_SIZE * 8UL); 826 827 if (threads > max_threads_suggested) 828 threads = max_threads_suggested; 829 830 max_threads = clamp_t(u64, threads, MIN_THREADS, MAX_THREADS); 831 } 832 833 #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT 834 /* Initialized by the architecture: */ 835 int arch_task_struct_size __read_mostly; 836 #endif 837 838 static void __init task_struct_whitelist(unsigned long *offset, unsigned long *size) 839 { 840 /* Fetch thread_struct whitelist for the architecture. */ 841 arch_thread_struct_whitelist(offset, size); 842 843 /* 844 * Handle zero-sized whitelist or empty thread_struct, otherwise 845 * adjust offset to position of thread_struct in task_struct. 846 */ 847 if (unlikely(*size == 0)) 848 *offset = 0; 849 else 850 *offset += offsetof(struct task_struct, thread); 851 } 852 853 void __init fork_init(void) 854 { 855 int i; 856 #ifndef ARCH_MIN_TASKALIGN 857 #define ARCH_MIN_TASKALIGN 0 858 #endif 859 int align = max_t(int, L1_CACHE_BYTES, ARCH_MIN_TASKALIGN); 860 unsigned long useroffset, usersize; 861 862 /* create a slab on which task_structs can be allocated */ 863 task_struct_whitelist(&useroffset, &usersize); 864 task_struct_cachep = kmem_cache_create_usercopy("task_struct", 865 arch_task_struct_size, align, 866 SLAB_PANIC|SLAB_ACCOUNT, 867 useroffset, usersize, NULL); 868 869 /* do the arch specific task caches init */ 870 arch_task_cache_init(); 871 872 set_max_threads(MAX_THREADS); 873 874 init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2; 875 init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2; 876 init_task.signal->rlim[RLIMIT_SIGPENDING] = 877 init_task.signal->rlim[RLIMIT_NPROC]; 878 879 for (i = 0; i < UCOUNT_COUNTS; i++) 880 init_user_ns.ucount_max[i] = max_threads/2; 881 882 set_userns_rlimit_max(&init_user_ns, UCOUNT_RLIMIT_NPROC, RLIM_INFINITY); 883 set_userns_rlimit_max(&init_user_ns, UCOUNT_RLIMIT_MSGQUEUE, RLIM_INFINITY); 884 set_userns_rlimit_max(&init_user_ns, UCOUNT_RLIMIT_SIGPENDING, RLIM_INFINITY); 885 set_userns_rlimit_max(&init_user_ns, UCOUNT_RLIMIT_MEMLOCK, RLIM_INFINITY); 886 887 #ifdef CONFIG_VMAP_STACK 888 cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "fork:vm_stack_cache", 889 NULL, free_vm_stack_cache); 890 #endif 891 892 scs_init(); 893 894 lockdep_init_task(&init_task); 895 uprobes_init(); 896 } 897 898 int __weak arch_dup_task_struct(struct task_struct *dst, 899 struct task_struct *src) 900 { 901 *dst = *src; 902 return 0; 903 } 904 905 void set_task_stack_end_magic(struct task_struct *tsk) 906 { 907 unsigned long *stackend; 908 909 stackend = end_of_stack(tsk); 910 *stackend = STACK_END_MAGIC; /* for overflow detection */ 911 } 912 913 static struct task_struct *dup_task_struct(struct task_struct *orig, int node) 914 { 915 struct task_struct *tsk; 916 int err; 917 918 if (node == NUMA_NO_NODE) 919 node = tsk_fork_get_node(orig); 920 tsk = alloc_task_struct_node(node); 921 if (!tsk) 922 return NULL; 923 924 err = arch_dup_task_struct(tsk, orig); 925 if (err) 926 goto free_tsk; 927 928 err = alloc_thread_stack_node(tsk, node); 929 if (err) 930 goto free_tsk; 931 932 #ifdef CONFIG_THREAD_INFO_IN_TASK 933 refcount_set(&tsk->stack_refcount, 1); 934 #endif 935 account_kernel_stack(tsk, 1); 936 937 err = scs_prepare(tsk, node); 938 if (err) 939 goto free_stack; 940 941 #ifdef CONFIG_SECCOMP 942 /* 943 * We must handle setting up seccomp filters once we're under 944 * the sighand lock in case orig has changed between now and 945 * then. Until then, filter must be NULL to avoid messing up 946 * the usage counts on the error path calling free_task. 947 */ 948 tsk->seccomp.filter = NULL; 949 #endif 950 951 RCU_INIT_POINTER(tsk->exec_state, NULL); 952 953 setup_thread_stack(tsk, orig); 954 clear_user_return_notifier(tsk); 955 clear_tsk_need_resched(tsk); 956 set_task_stack_end_magic(tsk); 957 clear_syscall_work_syscall_user_dispatch(tsk); 958 959 #ifdef CONFIG_STACKPROTECTOR 960 tsk->stack_canary = get_random_canary(); 961 #endif 962 if (orig->cpus_ptr == &orig->cpus_mask) 963 tsk->cpus_ptr = &tsk->cpus_mask; 964 dup_user_cpus_ptr(tsk, orig, node); 965 966 /* 967 * One for the user space visible state that goes away when reaped. 968 * One for the scheduler. 969 */ 970 refcount_set(&tsk->rcu_users, 2); 971 /* One for the rcu users */ 972 refcount_set(&tsk->usage, 1); 973 #ifdef CONFIG_BLK_DEV_IO_TRACE 974 tsk->btrace_seq = 0; 975 #endif 976 tsk->splice_pipe = NULL; 977 tsk->task_frag.page = NULL; 978 tsk->wake_q.next = NULL; 979 tsk->worker_private = NULL; 980 981 kcov_task_init(tsk); 982 kmsan_task_create(tsk); 983 kmap_local_fork(tsk); 984 985 #ifdef CONFIG_FAULT_INJECTION 986 tsk->fail_nth = 0; 987 #endif 988 989 #ifdef CONFIG_BLK_CGROUP 990 tsk->throttle_disk = NULL; 991 tsk->use_memdelay = 0; 992 #endif 993 994 #ifdef CONFIG_ARCH_HAS_CPU_PASID 995 tsk->pasid_activated = 0; 996 #endif 997 998 #ifdef CONFIG_MEMCG 999 tsk->active_memcg = NULL; 1000 #endif 1001 1002 #ifdef CONFIG_X86_BUS_LOCK_DETECT 1003 tsk->reported_split_lock = 0; 1004 #endif 1005 1006 #ifdef CONFIG_SCHED_MM_CID 1007 tsk->mm_cid.cid = MM_CID_UNSET; 1008 tsk->mm_cid.active = 0; 1009 INIT_HLIST_NODE(&tsk->mm_cid.node); 1010 #endif 1011 return tsk; 1012 1013 free_stack: 1014 exit_task_stack_account(tsk); 1015 free_thread_stack(tsk); 1016 free_tsk: 1017 free_task_struct(tsk); 1018 return NULL; 1019 } 1020 1021 __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock); 1022 1023 static unsigned long coredump_filter = MMF_DUMP_FILTER_DEFAULT; 1024 1025 static int __init coredump_filter_setup(char *s) 1026 { 1027 if (kstrtoul(s, 0, &coredump_filter)) 1028 return 0; 1029 coredump_filter <<= MMF_DUMP_FILTER_SHIFT; 1030 coredump_filter &= MMF_DUMP_FILTER_MASK; 1031 return 1; 1032 } 1033 1034 __setup("coredump_filter=", coredump_filter_setup); 1035 1036 #include <linux/init_task.h> 1037 1038 static void mm_init_aio(struct mm_struct *mm) 1039 { 1040 #ifdef CONFIG_AIO 1041 spin_lock_init(&mm->ioctx_lock); 1042 mm->ioctx_table = NULL; 1043 #endif 1044 } 1045 1046 static __always_inline void mm_clear_owner(struct mm_struct *mm, 1047 struct task_struct *p) 1048 { 1049 #ifdef CONFIG_MEMCG 1050 if (mm->owner == p) 1051 WRITE_ONCE(mm->owner, NULL); 1052 #endif 1053 } 1054 1055 static void mm_init_owner(struct mm_struct *mm, struct task_struct *p) 1056 { 1057 #ifdef CONFIG_MEMCG 1058 mm->owner = p; 1059 #endif 1060 } 1061 1062 static void mm_init_uprobes_state(struct mm_struct *mm) 1063 { 1064 #ifdef CONFIG_UPROBES 1065 mm->uprobes_state.xol_area = NULL; 1066 arch_uprobe_init_state(mm); 1067 #endif 1068 } 1069 1070 static void mmap_init_lock(struct mm_struct *mm) 1071 { 1072 init_rwsem(&mm->mmap_lock); 1073 mm_lock_seqcount_init(mm); 1074 #ifdef CONFIG_PER_VMA_LOCK 1075 rcuwait_init(&mm->vma_writer_wait); 1076 #endif 1077 } 1078 1079 static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p) 1080 { 1081 mt_init_flags(&mm->mm_mt, MM_MT_FLAGS); 1082 mt_set_external_lock(&mm->mm_mt, &mm->mmap_lock); 1083 atomic_set(&mm->mm_users, 1); 1084 atomic_set(&mm->mm_count, 1); 1085 seqcount_init(&mm->write_protect_seq); 1086 mmap_init_lock(mm); 1087 INIT_LIST_HEAD(&mm->mmlist); 1088 mm_pgtables_bytes_init(mm); 1089 mm->map_count = 0; 1090 mm->locked_vm = 0; 1091 atomic64_set(&mm->pinned_vm, 0); 1092 memset(&mm->rss_stat, 0, sizeof(mm->rss_stat)); 1093 spin_lock_init(&mm->page_table_lock); 1094 spin_lock_init(&mm->arg_lock); 1095 mm_init_cpumask(mm); 1096 mm_init_aio(mm); 1097 mm_init_owner(mm, p); 1098 mm_pasid_init(mm); 1099 RCU_INIT_POINTER(mm->exe_file, NULL); 1100 mmu_notifier_subscriptions_init(mm); 1101 init_tlb_flush_pending(mm); 1102 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !defined(CONFIG_SPLIT_PMD_PTLOCKS) 1103 mm->pmd_huge_pte = NULL; 1104 #endif 1105 mm_init_uprobes_state(mm); 1106 hugetlb_count_init(mm); 1107 futex_mm_init(mm); 1108 1109 mm_flags_clear_all(mm); 1110 if (current->mm) { 1111 unsigned long flags = __mm_flags_get_word(current->mm); 1112 1113 __mm_flags_overwrite_word(mm, mmf_init_legacy_flags(flags)); 1114 mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK; 1115 } else { 1116 __mm_flags_overwrite_word(mm, coredump_filter); 1117 mm->def_flags = 0; 1118 } 1119 1120 if (mm_alloc_pgd(mm)) 1121 goto fail_mm_init; 1122 1123 if (mm_alloc_id(mm)) 1124 goto fail_noid; 1125 1126 if (init_new_context(p, mm)) 1127 goto fail_nocontext; 1128 1129 if (mm_alloc_cid(mm, p)) 1130 goto fail_cid; 1131 1132 if (mm_alloc_sched(mm)) 1133 goto fail_sched; 1134 1135 if (percpu_counter_init_many(mm->rss_stat, 0, GFP_KERNEL_ACCOUNT, 1136 NR_MM_COUNTERS)) 1137 goto fail_pcpu; 1138 1139 lru_gen_init_mm(mm); 1140 return mm; 1141 1142 fail_pcpu: 1143 mm_destroy_sched(mm); 1144 fail_sched: 1145 mm_destroy_cid(mm); 1146 fail_cid: 1147 destroy_context(mm); 1148 fail_nocontext: 1149 mm_free_id(mm); 1150 fail_noid: 1151 mm_free_pgd(mm); 1152 fail_mm_init: 1153 free_mm(mm); 1154 return NULL; 1155 } 1156 1157 /* 1158 * Allocate and initialize an mm_struct. 1159 */ 1160 struct mm_struct *mm_alloc(void) 1161 { 1162 struct mm_struct *mm; 1163 1164 mm = allocate_mm(); 1165 if (!mm) 1166 return NULL; 1167 1168 memset(mm, 0, sizeof(*mm)); 1169 return mm_init(mm, current); 1170 } 1171 EXPORT_SYMBOL_IF_KUNIT(mm_alloc); 1172 1173 static inline void __mmput(struct mm_struct *mm) 1174 { 1175 VM_BUG_ON(atomic_read(&mm->mm_users)); 1176 1177 uprobe_clear_state(mm); 1178 exit_aio(mm); 1179 ksm_exit(mm); 1180 khugepaged_exit(mm); /* must run before exit_mmap */ 1181 exit_mmap(mm); 1182 mm_put_huge_zero_folio(mm); 1183 set_mm_exe_file(mm, NULL); 1184 if (!list_empty(&mm->mmlist)) { 1185 spin_lock(&mmlist_lock); 1186 list_del(&mm->mmlist); 1187 spin_unlock(&mmlist_lock); 1188 } 1189 if (mm->binfmt) 1190 module_put(mm->binfmt->module); 1191 lru_gen_del_mm(mm); 1192 futex_hash_free(mm); 1193 mmdrop(mm); 1194 } 1195 1196 /* 1197 * Decrement the use count and release all resources for an mm. 1198 */ 1199 void mmput(struct mm_struct *mm) 1200 { 1201 might_sleep(); 1202 1203 if (atomic_dec_and_test(&mm->mm_users)) 1204 __mmput(mm); 1205 } 1206 EXPORT_SYMBOL_GPL(mmput); 1207 1208 #if defined(CONFIG_MMU) || defined(CONFIG_FUTEX_PRIVATE_HASH) 1209 static void mmput_async_fn(struct work_struct *work) 1210 { 1211 struct mm_struct *mm = container_of(work, struct mm_struct, 1212 async_put_work); 1213 1214 __mmput(mm); 1215 } 1216 1217 void mmput_async(struct mm_struct *mm) 1218 { 1219 if (atomic_dec_and_test(&mm->mm_users)) { 1220 INIT_WORK(&mm->async_put_work, mmput_async_fn); 1221 schedule_work(&mm->async_put_work); 1222 } 1223 } 1224 EXPORT_SYMBOL_GPL(mmput_async); 1225 #endif 1226 1227 /** 1228 * set_mm_exe_file - change a reference to the mm's executable file 1229 * @mm: The mm to change. 1230 * @new_exe_file: The new file to use. 1231 * 1232 * This changes mm's executable file (shown as symlink /proc/[pid]/exe). 1233 * 1234 * Main users are mmput() and sys_execve(). Callers prevent concurrent 1235 * invocations: in mmput() nobody alive left, in execve it happens before 1236 * the new mm is made visible to anyone. 1237 * 1238 * Can only fail if new_exe_file != NULL. 1239 */ 1240 int set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 1241 { 1242 struct file *old_exe_file; 1243 1244 /* 1245 * It is safe to dereference the exe_file without RCU as 1246 * this function is only called if nobody else can access 1247 * this mm -- see comment above for justification. 1248 */ 1249 old_exe_file = rcu_dereference_raw(mm->exe_file); 1250 1251 if (new_exe_file) { 1252 /* 1253 * We expect the caller (i.e., sys_execve) to already denied 1254 * write access, so this is unlikely to fail. 1255 */ 1256 if (unlikely(exe_file_deny_write_access(new_exe_file))) 1257 return -EACCES; 1258 get_file(new_exe_file); 1259 } 1260 rcu_assign_pointer(mm->exe_file, new_exe_file); 1261 if (old_exe_file) { 1262 exe_file_allow_write_access(old_exe_file); 1263 fput(old_exe_file); 1264 } 1265 return 0; 1266 } 1267 1268 /** 1269 * replace_mm_exe_file - replace a reference to the mm's executable file 1270 * @mm: The mm to change. 1271 * @new_exe_file: The new file to use. 1272 * 1273 * This changes mm's executable file (shown as symlink /proc/[pid]/exe). 1274 * 1275 * Main user is sys_prctl(PR_SET_MM_MAP/EXE_FILE). 1276 */ 1277 int replace_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) 1278 { 1279 struct vm_area_struct *vma; 1280 struct file *old_exe_file; 1281 int ret = 0; 1282 1283 /* Forbid mm->exe_file change if old file still mapped. */ 1284 old_exe_file = get_mm_exe_file(mm); 1285 if (old_exe_file) { 1286 VMA_ITERATOR(vmi, mm, 0); 1287 mmap_read_lock(mm); 1288 for_each_vma(vmi, vma) { 1289 if (!vma->vm_file) 1290 continue; 1291 if (path_equal(&vma->vm_file->f_path, 1292 &old_exe_file->f_path)) { 1293 ret = -EBUSY; 1294 break; 1295 } 1296 } 1297 mmap_read_unlock(mm); 1298 fput(old_exe_file); 1299 if (ret) 1300 return ret; 1301 } 1302 1303 ret = exe_file_deny_write_access(new_exe_file); 1304 if (ret) 1305 return -EACCES; 1306 get_file(new_exe_file); 1307 1308 /* set the new file */ 1309 mmap_write_lock(mm); 1310 old_exe_file = rcu_dereference_raw(mm->exe_file); 1311 rcu_assign_pointer(mm->exe_file, new_exe_file); 1312 mmap_write_unlock(mm); 1313 1314 if (old_exe_file) { 1315 exe_file_allow_write_access(old_exe_file); 1316 fput(old_exe_file); 1317 } 1318 return 0; 1319 } 1320 1321 /** 1322 * get_mm_exe_file - acquire a reference to the mm's executable file 1323 * @mm: The mm of interest. 1324 * 1325 * Returns %NULL if mm has no associated executable file. 1326 * User must release file via fput(). 1327 */ 1328 struct file *get_mm_exe_file(struct mm_struct *mm) 1329 { 1330 struct file *exe_file; 1331 1332 rcu_read_lock(); 1333 exe_file = get_file_rcu(&mm->exe_file); 1334 rcu_read_unlock(); 1335 return exe_file; 1336 } 1337 1338 /** 1339 * get_task_exe_file - acquire a reference to the task's executable file 1340 * @task: The task. 1341 * 1342 * Returns %NULL if task's mm (if any) has no associated executable file or 1343 * this is a kernel thread with borrowed mm (see the comment above get_task_mm). 1344 * User must release file via fput(). 1345 */ 1346 struct file *get_task_exe_file(struct task_struct *task) 1347 { 1348 struct file *exe_file = NULL; 1349 struct mm_struct *mm; 1350 1351 if (task->flags & PF_KTHREAD) 1352 return NULL; 1353 1354 task_lock(task); 1355 mm = task->mm; 1356 if (mm) 1357 exe_file = get_mm_exe_file(mm); 1358 task_unlock(task); 1359 return exe_file; 1360 } 1361 1362 /** 1363 * get_task_mm - acquire a reference to the task's mm 1364 * @task: The task. 1365 * 1366 * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning 1367 * this kernel workthread has transiently adopted a user mm with kthread_use_mm, 1368 * to do its AIO) is not set and if so returns a reference to it, after 1369 * bumping up the use count. User must release the mm via mmput() 1370 * after use. Typically used by /proc and ptrace. 1371 */ 1372 struct mm_struct *get_task_mm(struct task_struct *task) 1373 { 1374 struct mm_struct *mm; 1375 1376 if (task->flags & PF_KTHREAD) 1377 return NULL; 1378 1379 task_lock(task); 1380 mm = task->mm; 1381 if (mm) 1382 mmget(mm); 1383 task_unlock(task); 1384 return mm; 1385 } 1386 EXPORT_SYMBOL_GPL(get_task_mm); 1387 1388 static bool may_access_mm(struct mm_struct *mm, struct task_struct *task, unsigned int mode) 1389 { 1390 if (mm == current->mm) 1391 return true; 1392 if (ptrace_may_access(task, mode)) 1393 return true; 1394 if ((mode & PTRACE_MODE_READ) && perfmon_capable()) 1395 return true; 1396 return false; 1397 } 1398 1399 struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) 1400 { 1401 struct mm_struct *mm; 1402 int err; 1403 1404 err = down_read_killable(&task->signal->exec_update_lock); 1405 if (err) 1406 return ERR_PTR(err); 1407 1408 mm = get_task_mm(task); 1409 if (!mm) { 1410 mm = ERR_PTR(-ESRCH); 1411 } else if (!may_access_mm(mm, task, mode)) { 1412 mmput(mm); 1413 mm = ERR_PTR(-EACCES); 1414 } 1415 up_read(&task->signal->exec_update_lock); 1416 1417 return mm; 1418 } 1419 1420 static void complete_vfork_done(struct task_struct *tsk) 1421 { 1422 struct completion *vfork; 1423 1424 task_lock(tsk); 1425 vfork = tsk->vfork_done; 1426 if (likely(vfork)) { 1427 tsk->vfork_done = NULL; 1428 complete(vfork); 1429 } 1430 task_unlock(tsk); 1431 } 1432 1433 static int wait_for_vfork_done(struct task_struct *child, 1434 struct completion *vfork) 1435 { 1436 unsigned int state = TASK_KILLABLE|TASK_FREEZABLE; 1437 int killed; 1438 1439 cgroup_enter_frozen(); 1440 killed = wait_for_completion_state(vfork, state); 1441 cgroup_leave_frozen(false); 1442 1443 if (killed) { 1444 task_lock(child); 1445 child->vfork_done = NULL; 1446 task_unlock(child); 1447 } 1448 1449 put_task_struct(child); 1450 return killed; 1451 } 1452 1453 /* Please note the differences between mmput and mm_release. 1454 * mmput is called whenever we stop holding onto a mm_struct, 1455 * error success whatever. 1456 * 1457 * mm_release is called after a mm_struct has been removed 1458 * from the current process. 1459 * 1460 * This difference is important for error handling, when we 1461 * only half set up a mm_struct for a new process and need to restore 1462 * the old one. Because we mmput the new mm_struct before 1463 * restoring the old one. . . 1464 * Eric Biederman 10 January 1998 1465 */ 1466 static void mm_release(struct task_struct *tsk, struct mm_struct *mm) 1467 { 1468 uprobe_free_utask(tsk); 1469 1470 /* Get rid of any cached register state */ 1471 deactivate_mm(tsk, mm); 1472 1473 /* 1474 * Signal userspace if we're not exiting with a core dump 1475 * because we want to leave the value intact for debugging 1476 * purposes. 1477 */ 1478 if (tsk->clear_child_tid) { 1479 if (atomic_read(&mm->mm_users) > 1) { 1480 /* 1481 * We don't check the error code - if userspace has 1482 * not set up a proper pointer then tough luck. 1483 */ 1484 put_user(0, tsk->clear_child_tid); 1485 do_futex(tsk->clear_child_tid, FUTEX_WAKE, 1486 1, NULL, NULL, 0, 0); 1487 } 1488 tsk->clear_child_tid = NULL; 1489 } 1490 1491 /* 1492 * All done, finally we can wake up parent and return this mm to him. 1493 * Also kthread_stop() uses this completion for synchronization. 1494 */ 1495 if (tsk->vfork_done) 1496 complete_vfork_done(tsk); 1497 } 1498 1499 void exit_mm_release(struct task_struct *tsk, struct mm_struct *mm) 1500 { 1501 futex_exit_release(tsk); 1502 mm_release(tsk, mm); 1503 } 1504 1505 void exec_mm_release(struct task_struct *tsk, struct mm_struct *mm) 1506 { 1507 futex_exec_release(tsk); 1508 mm_release(tsk, mm); 1509 } 1510 1511 /** 1512 * dup_mm() - duplicates an existing mm structure 1513 * @tsk: the task_struct with which the new mm will be associated. 1514 * @oldmm: the mm to duplicate. 1515 * 1516 * Allocates a new mm structure and duplicates the provided @oldmm structure 1517 * content into it. 1518 * 1519 * Return: the duplicated mm or NULL on failure. 1520 */ 1521 static struct mm_struct *dup_mm(struct task_struct *tsk, 1522 struct mm_struct *oldmm) 1523 { 1524 struct mm_struct *mm; 1525 int err; 1526 1527 mm = allocate_mm(); 1528 if (!mm) 1529 goto fail_nomem; 1530 1531 memcpy(mm, oldmm, sizeof(*mm)); 1532 1533 if (!mm_init(mm, tsk)) 1534 goto fail_nomem; 1535 1536 uprobe_start_dup_mmap(); 1537 err = dup_mmap(mm, oldmm); 1538 if (err) 1539 goto free_pt; 1540 uprobe_end_dup_mmap(); 1541 1542 mm->hiwater_rss = get_mm_rss(mm); 1543 mm->hiwater_vm = mm->total_vm; 1544 1545 if (mm->binfmt && !try_module_get(mm->binfmt->module)) 1546 goto free_pt; 1547 1548 return mm; 1549 1550 free_pt: 1551 /* don't put binfmt in mmput, we haven't got module yet */ 1552 mm->binfmt = NULL; 1553 mm_init_owner(mm, NULL); 1554 mmput(mm); 1555 if (err) 1556 uprobe_end_dup_mmap(); 1557 1558 fail_nomem: 1559 return NULL; 1560 } 1561 1562 static int copy_mm(u64 clone_flags, struct task_struct *tsk) 1563 { 1564 struct mm_struct *mm, *oldmm; 1565 1566 tsk->min_flt = tsk->maj_flt = 0; 1567 tsk->nvcsw = tsk->nivcsw = 0; 1568 #ifdef CONFIG_DETECT_HUNG_TASK 1569 tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw; 1570 tsk->last_switch_time = 0; 1571 #endif 1572 1573 tsk->mm = NULL; 1574 tsk->active_mm = NULL; 1575 1576 /* 1577 * Are we cloning a kernel thread? 1578 * 1579 * We need to steal a active VM for that.. 1580 */ 1581 oldmm = current->mm; 1582 if (!oldmm) 1583 return 0; 1584 1585 if (clone_flags & CLONE_VM) { 1586 mmget(oldmm); 1587 mm = oldmm; 1588 } else { 1589 mm = dup_mm(tsk, current->mm); 1590 if (!mm) 1591 return -ENOMEM; 1592 } 1593 1594 tsk->mm = mm; 1595 tsk->active_mm = mm; 1596 return 0; 1597 } 1598 1599 static int copy_exec_state(u64 clone_flags, struct task_struct *tsk) 1600 { 1601 struct task_exec_state *exec_state; 1602 1603 /* CLONE_VM siblings refcount-share the parent's exec_state. */ 1604 if (clone_flags & CLONE_VM) { 1605 exec_state = rcu_dereference_protected(current->exec_state, true); 1606 refcount_inc(&exec_state->count); 1607 rcu_assign_pointer(tsk->exec_state, exec_state); 1608 return 0; 1609 } 1610 1611 /* Everyone else inherits a fresh copy. */ 1612 return task_exec_state_copy(tsk); 1613 } 1614 1615 static int copy_fs(u64 clone_flags, struct task_struct *tsk) 1616 { 1617 struct fs_struct *fs = current->fs; 1618 if (clone_flags & CLONE_FS) { 1619 /* tsk->fs is already what we want */ 1620 read_seqlock_excl(&fs->seq); 1621 /* "users" and "in_exec" locked for check_unsafe_exec() */ 1622 if (fs->in_exec) { 1623 read_sequnlock_excl(&fs->seq); 1624 return -EAGAIN; 1625 } 1626 fs->users++; 1627 read_sequnlock_excl(&fs->seq); 1628 return 0; 1629 } 1630 tsk->fs = copy_fs_struct(fs); 1631 if (!tsk->fs) 1632 return -ENOMEM; 1633 return 0; 1634 } 1635 1636 static int copy_files(u64 clone_flags, struct task_struct *tsk, 1637 int no_files) 1638 { 1639 struct files_struct *oldf, *newf; 1640 1641 /* 1642 * A background process may not have any files ... 1643 */ 1644 oldf = current->files; 1645 if (!oldf) 1646 return 0; 1647 1648 if (no_files) { 1649 tsk->files = NULL; 1650 return 0; 1651 } 1652 1653 if (clone_flags & CLONE_FILES) { 1654 atomic_inc(&oldf->count); 1655 return 0; 1656 } 1657 1658 newf = dup_fd(oldf, NULL); 1659 if (IS_ERR(newf)) 1660 return PTR_ERR(newf); 1661 1662 tsk->files = newf; 1663 return 0; 1664 } 1665 1666 static int copy_sighand(u64 clone_flags, struct task_struct *tsk) 1667 { 1668 struct sighand_struct *sig; 1669 1670 if (clone_flags & CLONE_SIGHAND) { 1671 refcount_inc(¤t->sighand->count); 1672 return 0; 1673 } 1674 sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); 1675 RCU_INIT_POINTER(tsk->sighand, sig); 1676 if (!sig) 1677 return -ENOMEM; 1678 1679 refcount_set(&sig->count, 1); 1680 spin_lock_irq(¤t->sighand->siglock); 1681 memcpy(sig->action, current->sighand->action, sizeof(sig->action)); 1682 spin_unlock_irq(¤t->sighand->siglock); 1683 1684 /* Reset all signal handler not set to SIG_IGN to SIG_DFL. */ 1685 if (clone_flags & CLONE_CLEAR_SIGHAND) 1686 flush_signal_handlers(tsk, 0); 1687 1688 return 0; 1689 } 1690 1691 void __cleanup_sighand(struct sighand_struct *sighand) 1692 { 1693 if (refcount_dec_and_test(&sighand->count)) { 1694 signalfd_cleanup(sighand); 1695 /* 1696 * sighand_cachep is SLAB_TYPESAFE_BY_RCU so we can free it 1697 * without an RCU grace period, see __lock_task_sighand(). 1698 */ 1699 kmem_cache_free(sighand_cachep, sighand); 1700 } 1701 } 1702 1703 /* 1704 * Initialize POSIX timer handling for a thread group. 1705 */ 1706 static void posix_cpu_timers_init_group(struct signal_struct *sig) 1707 { 1708 struct posix_cputimers *pct = &sig->posix_cputimers; 1709 unsigned long cpu_limit; 1710 1711 cpu_limit = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur); 1712 posix_cputimers_group_init(pct, cpu_limit); 1713 } 1714 1715 static int copy_signal(u64 clone_flags, struct task_struct *tsk) 1716 { 1717 struct signal_struct *sig; 1718 1719 if (clone_flags & CLONE_THREAD) 1720 return 0; 1721 1722 sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL); 1723 tsk->signal = sig; 1724 if (!sig) 1725 return -ENOMEM; 1726 1727 sig->nr_threads = 1; 1728 sig->quick_threads = 1; 1729 atomic_set(&sig->live, 1); 1730 refcount_set(&sig->sigcnt, 1); 1731 1732 /* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */ 1733 sig->thread_head = (struct list_head)LIST_HEAD_INIT(tsk->thread_node); 1734 tsk->thread_node = (struct list_head)LIST_HEAD_INIT(sig->thread_head); 1735 1736 init_waitqueue_head(&sig->wait_chldexit); 1737 sig->curr_target = tsk; 1738 init_sigpending(&sig->shared_pending); 1739 INIT_HLIST_HEAD(&sig->multiprocess); 1740 seqlock_init(&sig->stats_lock); 1741 prev_cputime_init(&sig->prev_cputime); 1742 1743 #ifdef CONFIG_POSIX_TIMERS 1744 INIT_HLIST_HEAD(&sig->posix_timers); 1745 INIT_HLIST_HEAD(&sig->ignored_posix_timers); 1746 hrtimer_setup(&sig->real_timer, it_real_fn, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 1747 #endif 1748 1749 task_lock(current->group_leader); 1750 memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim); 1751 task_unlock(current->group_leader); 1752 1753 posix_cpu_timers_init_group(sig); 1754 1755 tty_audit_fork(sig); 1756 sched_autogroup_fork(sig); 1757 1758 #ifdef CONFIG_CGROUPS 1759 init_rwsem(&sig->cgroup_threadgroup_rwsem); 1760 #endif 1761 1762 sig->oom_score_adj = current->signal->oom_score_adj; 1763 sig->oom_score_adj_min = current->signal->oom_score_adj_min; 1764 1765 mutex_init(&sig->cred_guard_mutex); 1766 init_rwsem(&sig->exec_update_lock); 1767 1768 return 0; 1769 } 1770 1771 static void copy_seccomp(struct task_struct *p) 1772 { 1773 #ifdef CONFIG_SECCOMP 1774 /* 1775 * Must be called with sighand->lock held, which is common to 1776 * all threads in the group. Holding cred_guard_mutex is not 1777 * needed because this new task is not yet running and cannot 1778 * be racing exec. 1779 */ 1780 assert_spin_locked(¤t->sighand->siglock); 1781 1782 /* Ref-count the new filter user, and assign it. */ 1783 get_seccomp_filter(current); 1784 p->seccomp = current->seccomp; 1785 1786 /* 1787 * Explicitly enable no_new_privs here in case it got set 1788 * between the task_struct being duplicated and holding the 1789 * sighand lock. The seccomp state and nnp must be in sync. 1790 */ 1791 if (task_no_new_privs(current)) 1792 task_set_no_new_privs(p); 1793 1794 /* 1795 * If the parent gained a seccomp mode after copying thread 1796 * flags and between before we held the sighand lock, we have 1797 * to manually enable the seccomp thread flag here. 1798 */ 1799 if (p->seccomp.mode != SECCOMP_MODE_DISABLED) 1800 set_task_syscall_work(p, SECCOMP); 1801 #endif 1802 } 1803 1804 SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr) 1805 { 1806 current->clear_child_tid = tidptr; 1807 1808 return task_pid_vnr(current); 1809 } 1810 1811 static void rt_mutex_init_task(struct task_struct *p) 1812 { 1813 raw_spin_lock_init(&p->pi_lock); 1814 #ifdef CONFIG_RT_MUTEXES 1815 p->pi_waiters = RB_ROOT_CACHED; 1816 p->pi_top_task = NULL; 1817 p->pi_blocked_on = NULL; 1818 #endif 1819 } 1820 1821 static inline void init_task_pid_links(struct task_struct *task) 1822 { 1823 enum pid_type type; 1824 1825 for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) 1826 INIT_HLIST_NODE(&task->pid_links[type]); 1827 } 1828 1829 static inline void 1830 init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid) 1831 { 1832 if (type == PIDTYPE_PID) 1833 task->thread_pid = pid; 1834 else 1835 task->signal->pids[type] = pid; 1836 } 1837 1838 static inline void rcu_copy_process(struct task_struct *p) 1839 { 1840 #ifdef CONFIG_PREEMPT_RCU 1841 p->rcu_read_lock_nesting = 0; 1842 p->rcu_read_unlock_special.s = 0; 1843 p->rcu_blocked_node = NULL; 1844 INIT_LIST_HEAD(&p->rcu_node_entry); 1845 #endif /* #ifdef CONFIG_PREEMPT_RCU */ 1846 #ifdef CONFIG_TASKS_RCU 1847 p->rcu_tasks_holdout = false; 1848 INIT_LIST_HEAD(&p->rcu_tasks_holdout_list); 1849 p->rcu_tasks_idle_cpu = -1; 1850 INIT_LIST_HEAD(&p->rcu_tasks_exit_list); 1851 #endif /* #ifdef CONFIG_TASKS_RCU */ 1852 #ifdef CONFIG_TASKS_TRACE_RCU 1853 p->trc_reader_nesting = 0; 1854 #endif /* #ifdef CONFIG_TASKS_TRACE_RCU */ 1855 } 1856 1857 /** 1858 * pidfd_prepare - allocate a new pidfd_file and reserve a pidfd 1859 * @pid: the struct pid for which to create a pidfd 1860 * @flags: flags of the new @pidfd 1861 * @ret_file: return the new pidfs file 1862 * 1863 * Allocate a new file that stashes @pid and reserve a new pidfd number in the 1864 * caller's file descriptor table. The pidfd is reserved but not installed yet. 1865 * 1866 * The helper verifies that @pid is still in use, without PIDFD_THREAD the 1867 * task identified by @pid must be a thread-group leader. 1868 * 1869 * If this function returns successfully the caller is responsible to either 1870 * call fd_install() passing the returned pidfd and pidfd file as arguments in 1871 * order to install the pidfd into its file descriptor table or they must use 1872 * put_unused_fd() and fput() on the returned pidfd and pidfd file 1873 * respectively. 1874 * 1875 * This function is useful when a pidfd must already be reserved but there 1876 * might still be points of failure afterwards and the caller wants to ensure 1877 * that no pidfd is leaked into its file descriptor table. 1878 * 1879 * Return: On success, a reserved pidfd is returned from the function and a new 1880 * pidfd file is returned in the last argument to the function. On 1881 * error, a negative error code is returned from the function and the 1882 * last argument remains unchanged. 1883 */ 1884 int pidfd_prepare(struct pid *pid, unsigned int flags, struct file **ret_file) 1885 { 1886 struct file *pidfs_file; 1887 1888 /* 1889 * PIDFD_STALE is only allowed to be passed if the caller knows 1890 * that @pid is already registered in pidfs and thus 1891 * PIDFD_INFO_EXIT information is guaranteed to be available. 1892 */ 1893 if (!(flags & PIDFD_STALE)) { 1894 /* 1895 * While holding the pidfd waitqueue lock removing the 1896 * task linkage for the thread-group leader pid 1897 * (PIDTYPE_TGID) isn't possible. Thus, if there's still 1898 * task linkage for PIDTYPE_PID not having thread-group 1899 * leader linkage for the pid means it wasn't a 1900 * thread-group leader in the first place. 1901 */ 1902 guard(spinlock_irq)(&pid->wait_pidfd.lock); 1903 1904 /* Task has already been reaped. */ 1905 if (!pid_has_task(pid, PIDTYPE_PID)) 1906 return -ESRCH; 1907 /* 1908 * If this struct pid isn't used as a thread-group 1909 * leader but the caller requested to create a 1910 * thread-group leader pidfd then report ENOENT. 1911 */ 1912 if (!(flags & PIDFD_THREAD) && !pid_has_task(pid, PIDTYPE_TGID)) 1913 return -ENOENT; 1914 } 1915 1916 CLASS(get_unused_fd, pidfd)(O_CLOEXEC); 1917 if (pidfd < 0) 1918 return pidfd; 1919 1920 pidfs_file = pidfs_alloc_file(pid, flags | O_RDWR); 1921 if (IS_ERR(pidfs_file)) 1922 return PTR_ERR(pidfs_file); 1923 1924 *ret_file = pidfs_file; 1925 return take_fd(pidfd); 1926 } 1927 1928 static void __delayed_free_task(struct rcu_head *rhp) 1929 { 1930 struct task_struct *tsk = container_of(rhp, struct task_struct, rcu); 1931 1932 free_task(tsk); 1933 } 1934 1935 static __always_inline void delayed_free_task(struct task_struct *tsk) 1936 { 1937 if (IS_ENABLED(CONFIG_MEMCG)) 1938 call_rcu(&tsk->rcu, __delayed_free_task); 1939 else 1940 free_task(tsk); 1941 } 1942 1943 static void copy_oom_score_adj(u64 clone_flags, struct task_struct *tsk) 1944 { 1945 /* Skip if kernel thread */ 1946 if (!tsk->mm) 1947 return; 1948 1949 /* Skip if spawning a thread or using vfork */ 1950 if ((clone_flags & (CLONE_VM | CLONE_THREAD | CLONE_VFORK)) != CLONE_VM) 1951 return; 1952 1953 /* We need to synchronize with __set_oom_adj */ 1954 mutex_lock(&oom_adj_mutex); 1955 mm_flags_set(MMF_MULTIPROCESS, tsk->mm); 1956 /* Update the values in case they were changed after copy_signal */ 1957 tsk->signal->oom_score_adj = current->signal->oom_score_adj; 1958 tsk->signal->oom_score_adj_min = current->signal->oom_score_adj_min; 1959 mutex_unlock(&oom_adj_mutex); 1960 } 1961 1962 #ifdef CONFIG_RV 1963 static void rv_task_fork(struct task_struct *p) 1964 { 1965 memset(&p->rv, 0, sizeof(p->rv)); 1966 } 1967 #else 1968 #define rv_task_fork(p) do {} while (0) 1969 #endif 1970 1971 static bool need_futex_hash_allocate_default(u64 clone_flags) 1972 { 1973 /* 1974 * Allocate a default futex hash for any sibling that will 1975 * share the parent's mm, except vfork. 1976 */ 1977 return (clone_flags & (CLONE_VM | CLONE_VFORK)) == CLONE_VM; 1978 } 1979 1980 /* 1981 * This creates a new process as a copy of the old one, 1982 * but does not actually start it yet. 1983 * 1984 * It copies the registers, and all the appropriate 1985 * parts of the process environment (as per the clone 1986 * flags). The actual kick-off is left to the caller. 1987 */ 1988 __latent_entropy struct task_struct *copy_process( 1989 struct pid *pid, 1990 int trace, 1991 int node, 1992 struct kernel_clone_args *args) 1993 { 1994 int pidfd = -1, retval; 1995 struct task_struct *p; 1996 struct multiprocess_signals delayed; 1997 struct file *pidfile = NULL; 1998 const u64 clone_flags = args->flags; 1999 struct nsproxy *nsp = current->nsproxy; 2000 2001 /* 2002 * Don't allow sharing the root directory with processes in a different 2003 * namespace 2004 */ 2005 if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS)) 2006 return ERR_PTR(-EINVAL); 2007 2008 if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS)) 2009 return ERR_PTR(-EINVAL); 2010 2011 /* 2012 * Thread groups must share signals as well, and detached threads 2013 * can only be started up within the thread group. 2014 */ 2015 if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND)) 2016 return ERR_PTR(-EINVAL); 2017 2018 /* 2019 * Shared signal handlers imply shared VM. By way of the above, 2020 * thread groups also imply shared VM. Blocking this case allows 2021 * for various simplifications in other code. 2022 */ 2023 if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM)) 2024 return ERR_PTR(-EINVAL); 2025 2026 /* 2027 * Siblings of global init remain as zombies on exit since they are 2028 * not reaped by their parent (swapper). To solve this and to avoid 2029 * multi-rooted process trees, prevent global and container-inits 2030 * from creating siblings. 2031 */ 2032 if ((clone_flags & CLONE_PARENT) && 2033 current->signal->flags & SIGNAL_UNKILLABLE) 2034 return ERR_PTR(-EINVAL); 2035 2036 /* 2037 * If the new process will be in a different pid or user namespace 2038 * do not allow it to share a thread group with the forking task. 2039 */ 2040 if (clone_flags & CLONE_THREAD) { 2041 if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) || 2042 (task_active_pid_ns(current) != nsp->pid_ns_for_children)) 2043 return ERR_PTR(-EINVAL); 2044 } 2045 2046 if (clone_flags & CLONE_PIDFD) { 2047 /* 2048 * - CLONE_DETACHED is blocked so that we can potentially 2049 * reuse it later for CLONE_PIDFD. 2050 */ 2051 if (clone_flags & CLONE_DETACHED) 2052 return ERR_PTR(-EINVAL); 2053 } 2054 2055 if (clone_flags & CLONE_AUTOREAP) { 2056 if (clone_flags & CLONE_THREAD) 2057 return ERR_PTR(-EINVAL); 2058 if (clone_flags & CLONE_PARENT) 2059 return ERR_PTR(-EINVAL); 2060 if (args->exit_signal) 2061 return ERR_PTR(-EINVAL); 2062 } 2063 2064 if ((clone_flags & CLONE_PARENT) && current->signal->autoreap) 2065 return ERR_PTR(-EINVAL); 2066 2067 if (clone_flags & CLONE_NNP) { 2068 if (clone_flags & CLONE_THREAD) 2069 return ERR_PTR(-EINVAL); 2070 } 2071 2072 if (clone_flags & CLONE_PIDFD_AUTOKILL) { 2073 if (!(clone_flags & CLONE_PIDFD)) 2074 return ERR_PTR(-EINVAL); 2075 if (!(clone_flags & CLONE_AUTOREAP)) 2076 return ERR_PTR(-EINVAL); 2077 if (clone_flags & CLONE_THREAD) 2078 return ERR_PTR(-EINVAL); 2079 /* 2080 * Without CLONE_NNP the child could escalate privileges 2081 * after being spawned, so require CAP_SYS_ADMIN. 2082 * With CLONE_NNP the child can't gain new privileges, 2083 * so allow unprivileged usage. 2084 */ 2085 if (!(clone_flags & CLONE_NNP) && 2086 !ns_capable(current_user_ns(), CAP_SYS_ADMIN)) 2087 return ERR_PTR(-EPERM); 2088 } 2089 2090 /* 2091 * Force any signals received before this point to be delivered 2092 * before the fork happens. Collect up signals sent to multiple 2093 * processes that happen during the fork and delay them so that 2094 * they appear to happen after the fork. 2095 */ 2096 sigemptyset(&delayed.signal); 2097 INIT_HLIST_NODE(&delayed.node); 2098 2099 spin_lock_irq(¤t->sighand->siglock); 2100 if (!(clone_flags & CLONE_THREAD)) 2101 hlist_add_head(&delayed.node, ¤t->signal->multiprocess); 2102 recalc_sigpending(); 2103 spin_unlock_irq(¤t->sighand->siglock); 2104 retval = -ERESTARTNOINTR; 2105 if (task_sigpending(current)) 2106 goto fork_out; 2107 2108 retval = -ENOMEM; 2109 p = dup_task_struct(current, node); 2110 if (!p) 2111 goto fork_out; 2112 retval = copy_exec_state(clone_flags, p); 2113 if (retval) 2114 goto bad_fork_free; 2115 p->flags &= ~PF_KTHREAD; 2116 if (args->kthread) 2117 p->flags |= PF_KTHREAD; 2118 if (args->user_worker) { 2119 /* 2120 * Mark us a user worker, and block any signal that isn't 2121 * fatal or STOP 2122 */ 2123 p->flags |= PF_USER_WORKER; 2124 siginitsetinv(&p->blocked, sigmask(SIGKILL)|sigmask(SIGSTOP)); 2125 } 2126 if (args->io_thread) 2127 p->flags |= PF_IO_WORKER; 2128 2129 if (args->name) 2130 strscpy_pad(p->comm, args->name, sizeof(p->comm)); 2131 2132 p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? args->child_tid : NULL; 2133 /* 2134 * TID is cleared in mm_release() when the task exits 2135 */ 2136 p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? args->child_tid : NULL; 2137 2138 ftrace_graph_init_task(p); 2139 2140 rt_mutex_init_task(p); 2141 raw_spin_lock_init(&p->blocked_lock); 2142 2143 lockdep_assert_irqs_enabled(); 2144 #ifdef CONFIG_PROVE_LOCKING 2145 DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled); 2146 #endif 2147 retval = copy_creds(p, clone_flags); 2148 if (retval < 0) 2149 goto bad_fork_free; 2150 2151 retval = -EAGAIN; 2152 if (is_rlimit_overlimit(task_ucounts(p), UCOUNT_RLIMIT_NPROC, rlimit(RLIMIT_NPROC))) { 2153 if (p->real_cred->user != INIT_USER && 2154 !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) 2155 goto bad_fork_cleanup_count; 2156 } 2157 current->flags &= ~PF_NPROC_EXCEEDED; 2158 2159 /* 2160 * If multiple threads are within copy_process(), then this check 2161 * triggers too late. This doesn't hurt, the check is only there 2162 * to stop root fork bombs. 2163 */ 2164 retval = -EAGAIN; 2165 if (data_race(nr_threads >= max_threads)) 2166 goto bad_fork_cleanup_count; 2167 2168 delayacct_tsk_init(p); /* Must remain after dup_task_struct() */ 2169 p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER | PF_IDLE | PF_NO_SETAFFINITY); 2170 p->flags |= PF_FORKNOEXEC; 2171 INIT_LIST_HEAD(&p->children); 2172 INIT_LIST_HEAD(&p->sibling); 2173 rcu_copy_process(p); 2174 p->vfork_done = NULL; 2175 spin_lock_init(&p->alloc_lock); 2176 2177 init_sigpending(&p->pending); 2178 2179 p->utime = p->stime = p->gtime = 0; 2180 #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME 2181 p->utimescaled = p->stimescaled = 0; 2182 #endif 2183 prev_cputime_init(&p->prev_cputime); 2184 2185 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN 2186 seqcount_init(&p->vtime.seqcount); 2187 p->vtime.starttime = 0; 2188 p->vtime.state = VTIME_INACTIVE; 2189 #endif 2190 2191 #ifdef CONFIG_IO_URING 2192 p->io_uring = NULL; 2193 retval = io_uring_fork(p); 2194 if (unlikely(retval)) 2195 goto bad_fork_cleanup_delayacct; 2196 retval = -EAGAIN; 2197 #endif 2198 2199 p->default_timer_slack_ns = current->timer_slack_ns; 2200 2201 #ifdef CONFIG_PSI 2202 p->psi_flags = 0; 2203 #endif 2204 2205 task_io_accounting_init(&p->ioac); 2206 acct_clear_integrals(p); 2207 2208 posix_cputimers_init(&p->posix_cputimers); 2209 tick_dep_init_task(p); 2210 2211 p->io_context = NULL; 2212 audit_set_context(p, NULL); 2213 cgroup_fork(p); 2214 if (args->kthread) { 2215 if (!set_kthread_struct(p)) 2216 goto bad_fork_cleanup_delayacct; 2217 } 2218 #ifdef CONFIG_NUMA 2219 p->mempolicy = mpol_dup(p->mempolicy); 2220 if (IS_ERR(p->mempolicy)) { 2221 retval = PTR_ERR(p->mempolicy); 2222 p->mempolicy = NULL; 2223 goto bad_fork_cleanup_delayacct; 2224 } 2225 #endif 2226 #ifdef CONFIG_CPUSETS 2227 p->cpuset_mem_spread_rotor = NUMA_NO_NODE; 2228 seqcount_spinlock_init(&p->mems_allowed_seq, &p->alloc_lock); 2229 #endif 2230 #ifdef CONFIG_TRACE_IRQFLAGS 2231 memset(&p->irqtrace, 0, sizeof(p->irqtrace)); 2232 p->irqtrace.hardirq_disable_ip = _THIS_IP_; 2233 p->irqtrace.softirq_enable_ip = _THIS_IP_; 2234 p->softirqs_enabled = 1; 2235 p->softirq_context = 0; 2236 #endif 2237 2238 p->pagefault_disabled = 0; 2239 2240 lockdep_init_task(p); 2241 2242 p->blocked_on = NULL; /* not blocked yet */ 2243 p->blocked_donor = NULL; /* nobody is boosting p yet */ 2244 2245 #ifdef CONFIG_BCACHE 2246 p->sequential_io = 0; 2247 p->sequential_io_avg = 0; 2248 #endif 2249 #ifdef CONFIG_BPF_SYSCALL 2250 RCU_INIT_POINTER(p->bpf_storage, NULL); 2251 p->bpf_ctx = NULL; 2252 #endif 2253 2254 unwind_task_init(p); 2255 2256 /* Perform scheduler related setup. Assign this task to a CPU. */ 2257 retval = sched_fork(clone_flags, p); 2258 if (retval) 2259 goto bad_fork_cleanup_policy; 2260 2261 retval = perf_event_init_task(p, clone_flags); 2262 if (retval) 2263 goto bad_fork_sched_cancel_fork; 2264 retval = audit_alloc(p); 2265 if (retval) 2266 goto bad_fork_cleanup_perf; 2267 /* copy all the process information */ 2268 shm_init_task(p); 2269 retval = security_task_alloc(p, clone_flags); 2270 if (retval) 2271 goto bad_fork_cleanup_audit; 2272 retval = copy_semundo(clone_flags, p); 2273 if (retval) 2274 goto bad_fork_cleanup_security; 2275 retval = copy_files(clone_flags, p, args->no_files); 2276 if (retval) 2277 goto bad_fork_cleanup_semundo; 2278 retval = copy_fs(clone_flags, p); 2279 if (retval) 2280 goto bad_fork_cleanup_files; 2281 retval = copy_sighand(clone_flags, p); 2282 if (retval) 2283 goto bad_fork_cleanup_fs; 2284 retval = copy_signal(clone_flags, p); 2285 if (retval) 2286 goto bad_fork_cleanup_sighand; 2287 retval = copy_mm(clone_flags, p); 2288 if (retval) 2289 goto bad_fork_cleanup_signal; 2290 retval = copy_namespaces(clone_flags, p); 2291 if (retval) 2292 goto bad_fork_cleanup_mm; 2293 retval = copy_io(clone_flags, p); 2294 if (retval) 2295 goto bad_fork_cleanup_namespaces; 2296 retval = copy_thread(p, args); 2297 if (retval) 2298 goto bad_fork_cleanup_io; 2299 2300 stackleak_task_init(p); 2301 2302 if (pid != &init_struct_pid) { 2303 pid = alloc_pid(p->nsproxy->pid_ns_for_children, args->set_tid, 2304 args->set_tid_size); 2305 if (IS_ERR(pid)) { 2306 retval = PTR_ERR(pid); 2307 goto bad_fork_cleanup_thread; 2308 } 2309 } 2310 2311 /* 2312 * This has to happen after we've potentially unshared the file 2313 * descriptor table (so that the pidfd doesn't leak into the child 2314 * if the fd table isn't shared). 2315 */ 2316 if (clone_flags & CLONE_PIDFD) { 2317 unsigned flags = PIDFD_STALE; 2318 2319 if (clone_flags & CLONE_THREAD) 2320 flags |= PIDFD_THREAD; 2321 if (clone_flags & CLONE_PIDFD_AUTOKILL) 2322 flags |= PIDFD_AUTOKILL; 2323 2324 /* 2325 * Note that no task has been attached to @pid yet indicate 2326 * that via CLONE_PIDFD. 2327 */ 2328 retval = pidfd_prepare(pid, flags, &pidfile); 2329 if (retval < 0) 2330 goto bad_fork_free_pid; 2331 pidfd = retval; 2332 2333 retval = put_user(pidfd, args->pidfd); 2334 if (retval) 2335 goto bad_fork_put_pidfd; 2336 } 2337 2338 #ifdef CONFIG_BLOCK 2339 p->plug = NULL; 2340 #endif 2341 futex_init_task(p); 2342 2343 /* 2344 * sigaltstack should be cleared when sharing the same VM 2345 */ 2346 if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM) 2347 sas_ss_reset(p); 2348 2349 /* 2350 * Syscall tracing and stepping should be turned off in the 2351 * child regardless of CLONE_PTRACE. 2352 */ 2353 user_disable_single_step(p); 2354 clear_task_syscall_work(p, SYSCALL_TRACE); 2355 #if defined(CONFIG_GENERIC_ENTRY) || defined(TIF_SYSCALL_EMU) 2356 clear_task_syscall_work(p, SYSCALL_EMU); 2357 #endif 2358 clear_tsk_latency_tracing(p); 2359 2360 /* ok, now we should be set up.. */ 2361 p->pid = pid_nr(pid); 2362 if (clone_flags & CLONE_THREAD) { 2363 p->group_leader = current->group_leader; 2364 p->tgid = current->tgid; 2365 } else { 2366 p->group_leader = p; 2367 p->tgid = p->pid; 2368 } 2369 2370 p->nr_dirtied = 0; 2371 p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10); 2372 p->dirty_paused_when = 0; 2373 2374 p->pdeath_signal = 0; 2375 p->task_works = NULL; 2376 clear_posix_cputimers_work(p); 2377 2378 #ifdef CONFIG_KRETPROBES 2379 p->kretprobe_instances.first = NULL; 2380 #endif 2381 #ifdef CONFIG_RETHOOK 2382 p->rethooks.first = NULL; 2383 #endif 2384 2385 /* 2386 * Ensure that the cgroup subsystem policies allow the new process to be 2387 * forked. It should be noted that the new process's css_set can be changed 2388 * between here and cgroup_post_fork() if an organisation operation is in 2389 * progress. 2390 */ 2391 retval = cgroup_can_fork(p, args); 2392 if (retval) 2393 goto bad_fork_put_pidfd; 2394 2395 /* 2396 * Now that the cgroups are pinned, re-clone the parent cgroup and put 2397 * the new task on the correct runqueue. All this *before* the task 2398 * becomes visible. 2399 * 2400 * This isn't part of ->can_fork() because while the re-cloning is 2401 * cgroup specific, it unconditionally needs to place the task on a 2402 * runqueue. 2403 */ 2404 retval = sched_cgroup_fork(p, args); 2405 if (retval) 2406 goto bad_fork_cancel_cgroup; 2407 2408 if (need_futex_hash_allocate_default(clone_flags)) { 2409 retval = futex_hash_allocate_default(); 2410 if (retval) 2411 goto bad_fork_cancel_cgroup; 2412 /* 2413 * If we fail beyond this point we don't free the allocated 2414 * futex hash map. We assume that another thread will be created 2415 * and makes use of it. The hash map will be freed once the main 2416 * thread terminates. 2417 */ 2418 } 2419 /* 2420 * From this point on we must avoid any synchronous user-space 2421 * communication until we take the tasklist-lock. In particular, we do 2422 * not want user-space to be able to predict the process start-time by 2423 * stalling fork(2) after we recorded the start_time but before it is 2424 * visible to the system. 2425 */ 2426 2427 p->start_time = ktime_get_ns(); 2428 p->start_boottime = ktime_get_boottime_ns(); 2429 2430 /* 2431 * Make it visible to the rest of the system, but dont wake it up yet. 2432 * Need tasklist lock for parent etc handling! 2433 */ 2434 write_lock_irq(&tasklist_lock); 2435 2436 /* CLONE_PARENT re-uses the old parent */ 2437 if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) { 2438 p->real_parent = current->real_parent; 2439 p->parent_exec_id = current->parent_exec_id; 2440 if (clone_flags & CLONE_THREAD) 2441 p->exit_signal = -1; 2442 else 2443 p->exit_signal = current->group_leader->exit_signal; 2444 } else { 2445 p->real_parent = current; 2446 p->parent_exec_id = current->self_exec_id; 2447 p->exit_signal = args->exit_signal; 2448 } 2449 2450 klp_copy_process(p); 2451 2452 sched_core_fork(p); 2453 2454 spin_lock(¤t->sighand->siglock); 2455 2456 rv_task_fork(p); 2457 2458 rseq_fork(p, clone_flags); 2459 2460 /* 2461 * If zap_pid_ns_processes() was called after alloc_pid(), the new 2462 * child missed SIGKILL. If current is not in the same namespace, 2463 * we can't rely on fatal_signal_pending() below. 2464 */ 2465 if (unlikely(!(ns_of_pid(pid)->pid_allocated & PIDNS_ADDING))) { 2466 retval = -ENOMEM; 2467 goto bad_fork_core_free; 2468 } 2469 2470 /* Let kill terminate clone/fork in the middle */ 2471 if (fatal_signal_pending(current)) { 2472 retval = -EINTR; 2473 goto bad_fork_core_free; 2474 } 2475 2476 /* No more failure paths after this point. */ 2477 2478 /* 2479 * Copy seccomp details explicitly here, in case they were changed 2480 * before holding sighand lock. 2481 */ 2482 copy_seccomp(p); 2483 2484 if (clone_flags & CLONE_NNP) 2485 task_set_no_new_privs(p); 2486 2487 init_task_pid_links(p); 2488 if (likely(p->pid)) { 2489 ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace); 2490 2491 init_task_pid(p, PIDTYPE_PID, pid); 2492 if (thread_group_leader(p)) { 2493 init_task_pid(p, PIDTYPE_TGID, pid); 2494 init_task_pid(p, PIDTYPE_PGID, task_pgrp(current)); 2495 init_task_pid(p, PIDTYPE_SID, task_session(current)); 2496 2497 if (is_child_reaper(pid)) { 2498 struct pid_namespace *ns = ns_of_pid(pid); 2499 2500 ASSERT_EXCLUSIVE_WRITER(ns->child_reaper); 2501 WRITE_ONCE(ns->child_reaper, p); 2502 p->signal->flags |= SIGNAL_UNKILLABLE; 2503 } 2504 p->signal->shared_pending.signal = delayed.signal; 2505 p->signal->tty = tty_kref_get(current->signal->tty); 2506 /* 2507 * Inherit has_child_subreaper flag under the same 2508 * tasklist_lock with adding child to the process tree 2509 * for propagate_has_child_subreaper optimization. 2510 */ 2511 p->signal->has_child_subreaper = p->real_parent->signal->has_child_subreaper || 2512 p->real_parent->signal->is_child_subreaper; 2513 if (clone_flags & CLONE_AUTOREAP) 2514 p->signal->autoreap = 1; 2515 list_add_tail(&p->sibling, &p->real_parent->children); 2516 list_add_tail_rcu(&p->tasks, &init_task.tasks); 2517 attach_pid(p, PIDTYPE_TGID); 2518 attach_pid(p, PIDTYPE_PGID); 2519 attach_pid(p, PIDTYPE_SID); 2520 __this_cpu_inc(process_counts); 2521 } else { 2522 current->signal->nr_threads++; 2523 current->signal->quick_threads++; 2524 atomic_inc(¤t->signal->live); 2525 refcount_inc(¤t->signal->sigcnt); 2526 task_join_group_stop(p); 2527 list_add_tail_rcu(&p->thread_node, 2528 &p->signal->thread_head); 2529 } 2530 attach_pid(p, PIDTYPE_PID); 2531 nr_threads++; 2532 } 2533 total_forks++; 2534 hlist_del_init(&delayed.node); 2535 spin_unlock(¤t->sighand->siglock); 2536 syscall_tracepoint_update(p); 2537 write_unlock_irq(&tasklist_lock); 2538 2539 if (pidfile) 2540 fd_install(pidfd, pidfile); 2541 2542 proc_fork_connector(p); 2543 /* 2544 * sched_ext needs @p to be associated with its cgroup in its post_fork 2545 * hook. cgroup_post_fork() should come before sched_post_fork(). 2546 */ 2547 cgroup_post_fork(p, args); 2548 sched_post_fork(p); 2549 perf_event_fork(p); 2550 2551 trace_task_newtask(p, clone_flags); 2552 uprobe_copy_process(p, clone_flags); 2553 user_events_fork(p, clone_flags); 2554 2555 copy_oom_score_adj(clone_flags, p); 2556 2557 return p; 2558 2559 bad_fork_core_free: 2560 sched_core_free(p); 2561 spin_unlock(¤t->sighand->siglock); 2562 write_unlock_irq(&tasklist_lock); 2563 bad_fork_cancel_cgroup: 2564 cgroup_cancel_fork(p, args); 2565 bad_fork_put_pidfd: 2566 if (clone_flags & CLONE_PIDFD) { 2567 fput(pidfile); 2568 put_unused_fd(pidfd); 2569 } 2570 bad_fork_free_pid: 2571 if (pid != &init_struct_pid) 2572 free_pid(pid); 2573 bad_fork_cleanup_thread: 2574 exit_thread(p); 2575 bad_fork_cleanup_io: 2576 if (p->io_context) 2577 exit_io_context(p); 2578 bad_fork_cleanup_namespaces: 2579 exit_nsproxy_namespaces(p); 2580 bad_fork_cleanup_mm: 2581 if (p->mm) { 2582 mm_clear_owner(p->mm, p); 2583 mmput(p->mm); 2584 } 2585 bad_fork_cleanup_signal: 2586 if (!(clone_flags & CLONE_THREAD)) 2587 free_signal_struct(p->signal); 2588 bad_fork_cleanup_sighand: 2589 __cleanup_sighand(p->sighand); 2590 bad_fork_cleanup_fs: 2591 exit_fs(p); /* blocking */ 2592 bad_fork_cleanup_files: 2593 exit_files(p); /* blocking */ 2594 bad_fork_cleanup_semundo: 2595 exit_sem(p); 2596 bad_fork_cleanup_security: 2597 security_task_free(p); 2598 bad_fork_cleanup_audit: 2599 audit_free(p); 2600 bad_fork_cleanup_perf: 2601 perf_event_free_task(p); 2602 bad_fork_sched_cancel_fork: 2603 sched_cancel_fork(p); 2604 bad_fork_cleanup_policy: 2605 lockdep_free_task(p); 2606 #ifdef CONFIG_NUMA 2607 mpol_put(p->mempolicy); 2608 #endif 2609 bad_fork_cleanup_delayacct: 2610 io_uring_free(p); 2611 delayacct_tsk_free(p); 2612 bad_fork_cleanup_count: 2613 dec_rlimit_ucounts(task_ucounts(p), UCOUNT_RLIMIT_NPROC, 1); 2614 exit_cred_namespaces(p); 2615 exit_creds(p); 2616 bad_fork_free: 2617 WRITE_ONCE(p->__state, TASK_DEAD); 2618 exit_task_stack_account(p); 2619 put_task_stack(p); 2620 delayed_free_task(p); 2621 fork_out: 2622 spin_lock_irq(¤t->sighand->siglock); 2623 hlist_del_init(&delayed.node); 2624 spin_unlock_irq(¤t->sighand->siglock); 2625 return ERR_PTR(retval); 2626 } 2627 2628 static inline void init_idle_pids(struct task_struct *idle) 2629 { 2630 enum pid_type type; 2631 2632 for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { 2633 INIT_HLIST_NODE(&idle->pid_links[type]); /* not really needed */ 2634 init_task_pid(idle, type, &init_struct_pid); 2635 } 2636 } 2637 2638 static int idle_dummy(void *dummy) 2639 { 2640 /* This function is never called */ 2641 return 0; 2642 } 2643 2644 struct task_struct * __init fork_idle(int cpu) 2645 { 2646 struct task_struct *task; 2647 struct kernel_clone_args args = { 2648 .flags = CLONE_VM, 2649 .fn = &idle_dummy, 2650 .fn_arg = NULL, 2651 .kthread = 1, 2652 .idle = 1, 2653 }; 2654 2655 task = copy_process(&init_struct_pid, 0, cpu_to_node(cpu), &args); 2656 if (!IS_ERR(task)) { 2657 init_idle_pids(task); 2658 init_idle(task, cpu); 2659 } 2660 2661 return task; 2662 } 2663 2664 /* 2665 * This is like kernel_clone(), but shaved down and tailored to just 2666 * creating io_uring workers. It returns a created task, or an error pointer. 2667 * The returned task is inactive, and the caller must fire it up through 2668 * wake_up_new_task(p). All signals are blocked in the created task. 2669 */ 2670 struct task_struct *create_io_thread(int (*fn)(void *), void *arg, int node) 2671 { 2672 unsigned long flags = CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD| 2673 CLONE_IO|CLONE_VM|CLONE_UNTRACED; 2674 struct kernel_clone_args args = { 2675 .flags = flags, 2676 .fn = fn, 2677 .fn_arg = arg, 2678 .io_thread = 1, 2679 .user_worker = 1, 2680 }; 2681 2682 return copy_process(NULL, 0, node, &args); 2683 } 2684 2685 /* 2686 * Ok, this is the main fork-routine. 2687 * 2688 * It copies the process, and if successful kick-starts 2689 * it and waits for it to finish using the VM if required. 2690 */ 2691 pid_t kernel_clone(struct kernel_clone_args *args) 2692 { 2693 u64 clone_flags = args->flags; 2694 struct completion vfork; 2695 struct pid *pid; 2696 struct task_struct *p; 2697 int trace = 0; 2698 pid_t nr; 2699 2700 /* 2701 * Creating an empty mount namespace implies creating a new mount 2702 * namespace. Set this before copy_process() so that the 2703 * CLONE_NEWNS|CLONE_FS mutual exclusion check works correctly. 2704 */ 2705 if (clone_flags & CLONE_EMPTY_MNTNS) { 2706 clone_flags |= CLONE_NEWNS; 2707 args->flags = clone_flags; 2708 } 2709 2710 /* 2711 * For legacy clone() calls, CLONE_PIDFD uses the parent_tid argument 2712 * to return the pidfd. Hence, CLONE_PIDFD and CLONE_PARENT_SETTID are 2713 * mutually exclusive. With clone3() CLONE_PIDFD has grown a separate 2714 * field in struct clone_args and it still doesn't make sense to have 2715 * them both point at the same memory location. Performing this check 2716 * here has the advantage that we don't need to have a separate helper 2717 * to check for legacy clone(). 2718 */ 2719 if ((clone_flags & CLONE_PIDFD) && 2720 (clone_flags & CLONE_PARENT_SETTID) && 2721 (args->pidfd == args->parent_tid)) 2722 return -EINVAL; 2723 2724 if (!valid_signal(args->exit_signal)) 2725 return -EINVAL; 2726 2727 /* 2728 * Determine whether and which event to report to ptracer. When 2729 * called from kernel_thread or CLONE_UNTRACED is explicitly 2730 * requested, no event is reported; otherwise, report if the event 2731 * for the type of forking is enabled. 2732 */ 2733 if (!(clone_flags & CLONE_UNTRACED)) { 2734 if (clone_flags & CLONE_VFORK) 2735 trace = PTRACE_EVENT_VFORK; 2736 else if (args->exit_signal != SIGCHLD) 2737 trace = PTRACE_EVENT_CLONE; 2738 else 2739 trace = PTRACE_EVENT_FORK; 2740 2741 if (likely(!ptrace_event_enabled(current, trace))) 2742 trace = 0; 2743 } 2744 2745 p = copy_process(NULL, trace, NUMA_NO_NODE, args); 2746 add_latent_entropy(); 2747 2748 if (IS_ERR(p)) 2749 return PTR_ERR(p); 2750 2751 /* 2752 * Do this prior waking up the new thread - the thread pointer 2753 * might get invalid after that point, if the thread exits quickly. 2754 */ 2755 trace_sched_process_fork(current, p); 2756 2757 pid = get_task_pid(p, PIDTYPE_PID); 2758 nr = pid_vnr(pid); 2759 2760 if (clone_flags & CLONE_PARENT_SETTID) 2761 put_user(nr, args->parent_tid); 2762 2763 if (clone_flags & CLONE_VFORK) { 2764 p->vfork_done = &vfork; 2765 init_completion(&vfork); 2766 get_task_struct(p); 2767 } 2768 2769 if (IS_ENABLED(CONFIG_LRU_GEN_WALKS_MMU) && !(clone_flags & CLONE_VM)) { 2770 /* lock the task to synchronize with memcg migration */ 2771 task_lock(p); 2772 lru_gen_add_mm(p->mm); 2773 task_unlock(p); 2774 } 2775 2776 wake_up_new_task(p); 2777 2778 /* forking complete and child started to run, tell ptracer */ 2779 if (unlikely(trace)) 2780 ptrace_event_pid(trace, pid); 2781 2782 if (clone_flags & CLONE_VFORK) { 2783 if (!wait_for_vfork_done(p, &vfork)) 2784 ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid); 2785 } 2786 2787 put_pid(pid); 2788 return nr; 2789 } 2790 2791 /* 2792 * Create a kernel thread. 2793 */ 2794 pid_t kernel_thread(int (*fn)(void *), void *arg, const char *name, 2795 unsigned long flags) 2796 { 2797 struct kernel_clone_args args = { 2798 .flags = ((flags | CLONE_VM | CLONE_UNTRACED) & ~CSIGNAL), 2799 .exit_signal = (flags & CSIGNAL), 2800 .fn = fn, 2801 .fn_arg = arg, 2802 .name = name, 2803 .kthread = 1, 2804 }; 2805 2806 return kernel_clone(&args); 2807 } 2808 2809 /* 2810 * Create a user mode thread. 2811 */ 2812 pid_t user_mode_thread(int (*fn)(void *), void *arg, unsigned long flags) 2813 { 2814 struct kernel_clone_args args = { 2815 .flags = ((flags | CLONE_VM | CLONE_UNTRACED) & ~CSIGNAL), 2816 .exit_signal = (flags & CSIGNAL), 2817 .fn = fn, 2818 .fn_arg = arg, 2819 }; 2820 2821 return kernel_clone(&args); 2822 } 2823 2824 #ifdef __ARCH_WANT_SYS_FORK 2825 SYSCALL_DEFINE0(fork) 2826 { 2827 #ifdef CONFIG_MMU 2828 struct kernel_clone_args args = { 2829 .exit_signal = SIGCHLD, 2830 }; 2831 2832 return kernel_clone(&args); 2833 #else 2834 /* can not support in nommu mode */ 2835 return -EINVAL; 2836 #endif 2837 } 2838 #endif 2839 2840 #ifdef __ARCH_WANT_SYS_VFORK 2841 SYSCALL_DEFINE0(vfork) 2842 { 2843 struct kernel_clone_args args = { 2844 .flags = CLONE_VFORK | CLONE_VM, 2845 .exit_signal = SIGCHLD, 2846 }; 2847 2848 return kernel_clone(&args); 2849 } 2850 #endif 2851 2852 #ifdef __ARCH_WANT_SYS_CLONE 2853 #ifdef CONFIG_CLONE_BACKWARDS 2854 SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 2855 int __user *, parent_tidptr, 2856 unsigned long, tls, 2857 int __user *, child_tidptr) 2858 #elif defined(CONFIG_CLONE_BACKWARDS2) 2859 SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags, 2860 int __user *, parent_tidptr, 2861 int __user *, child_tidptr, 2862 unsigned long, tls) 2863 #elif defined(CONFIG_CLONE_BACKWARDS3) 2864 SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp, 2865 int, stack_size, 2866 int __user *, parent_tidptr, 2867 int __user *, child_tidptr, 2868 unsigned long, tls) 2869 #else 2870 SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, 2871 int __user *, parent_tidptr, 2872 int __user *, child_tidptr, 2873 unsigned long, tls) 2874 #endif 2875 { 2876 struct kernel_clone_args args = { 2877 .flags = (lower_32_bits(clone_flags) & ~CSIGNAL), 2878 .pidfd = parent_tidptr, 2879 .child_tid = child_tidptr, 2880 .parent_tid = parent_tidptr, 2881 .exit_signal = (lower_32_bits(clone_flags) & CSIGNAL), 2882 .stack = newsp, 2883 .tls = tls, 2884 }; 2885 2886 return kernel_clone(&args); 2887 } 2888 #endif 2889 2890 static noinline int copy_clone_args_from_user(struct kernel_clone_args *kargs, 2891 struct clone_args __user *uargs, 2892 size_t usize) 2893 { 2894 int err; 2895 struct clone_args args; 2896 pid_t *kset_tid = kargs->set_tid; 2897 2898 BUILD_BUG_ON(offsetofend(struct clone_args, tls) != 2899 CLONE_ARGS_SIZE_VER0); 2900 BUILD_BUG_ON(offsetofend(struct clone_args, set_tid_size) != 2901 CLONE_ARGS_SIZE_VER1); 2902 BUILD_BUG_ON(offsetofend(struct clone_args, cgroup) != 2903 CLONE_ARGS_SIZE_VER2); 2904 BUILD_BUG_ON(sizeof(struct clone_args) != CLONE_ARGS_SIZE_VER2); 2905 2906 if (unlikely(usize > PAGE_SIZE)) 2907 return -E2BIG; 2908 if (unlikely(usize < CLONE_ARGS_SIZE_VER0)) 2909 return -EINVAL; 2910 2911 err = copy_struct_from_user(&args, sizeof(args), uargs, usize); 2912 if (err) 2913 return err; 2914 2915 if (unlikely(args.set_tid_size > MAX_PID_NS_LEVEL)) 2916 return -EINVAL; 2917 2918 if (unlikely(!args.set_tid && args.set_tid_size > 0)) 2919 return -EINVAL; 2920 2921 if (unlikely(args.set_tid && args.set_tid_size == 0)) 2922 return -EINVAL; 2923 2924 /* 2925 * Verify that higher 32bits of exit_signal are unset 2926 */ 2927 if (unlikely(args.exit_signal & ~((u64)CSIGNAL))) 2928 return -EINVAL; 2929 2930 if ((args.flags & CLONE_INTO_CGROUP) && 2931 (args.cgroup > INT_MAX || usize < CLONE_ARGS_SIZE_VER2)) 2932 return -EINVAL; 2933 2934 *kargs = (struct kernel_clone_args){ 2935 .flags = args.flags, 2936 .pidfd = u64_to_user_ptr(args.pidfd), 2937 .child_tid = u64_to_user_ptr(args.child_tid), 2938 .parent_tid = u64_to_user_ptr(args.parent_tid), 2939 .exit_signal = args.exit_signal, 2940 .stack = args.stack, 2941 .stack_size = args.stack_size, 2942 .tls = args.tls, 2943 .set_tid_size = args.set_tid_size, 2944 .cgroup = args.cgroup, 2945 }; 2946 2947 if (args.set_tid && 2948 copy_from_user(kset_tid, u64_to_user_ptr(args.set_tid), 2949 (kargs->set_tid_size * sizeof(pid_t)))) 2950 return -EFAULT; 2951 2952 kargs->set_tid = kset_tid; 2953 2954 return 0; 2955 } 2956 2957 /** 2958 * clone3_stack_valid - check and prepare stack 2959 * @kargs: kernel clone args 2960 * 2961 * Verify that the stack arguments userspace gave us are sane. 2962 * In addition, set the stack direction for userspace since it's easy for us to 2963 * determine. 2964 */ 2965 static inline bool clone3_stack_valid(struct kernel_clone_args *kargs) 2966 { 2967 if (kargs->stack == 0) { 2968 if (kargs->stack_size > 0) 2969 return false; 2970 } else { 2971 if (kargs->stack_size == 0) 2972 return false; 2973 2974 if (!access_ok((void __user *)kargs->stack, kargs->stack_size)) 2975 return false; 2976 2977 #if !defined(CONFIG_STACK_GROWSUP) 2978 kargs->stack += kargs->stack_size; 2979 #endif 2980 } 2981 2982 return true; 2983 } 2984 2985 static bool clone3_args_valid(struct kernel_clone_args *kargs) 2986 { 2987 /* Verify that no unknown flags are passed along. */ 2988 if (kargs->flags & 2989 ~(CLONE_LEGACY_FLAGS | CLONE_CLEAR_SIGHAND | 2990 CLONE_INTO_CGROUP | CLONE_AUTOREAP | CLONE_NNP | 2991 CLONE_PIDFD_AUTOKILL | CLONE_EMPTY_MNTNS)) 2992 return false; 2993 2994 /* 2995 * - make the CLONE_DETACHED bit reusable for clone3 2996 * - make the CSIGNAL bits reusable for clone3 2997 */ 2998 if (kargs->flags & (CLONE_DETACHED | (CSIGNAL & (~CLONE_NEWTIME)))) 2999 return false; 3000 3001 if ((kargs->flags & (CLONE_SIGHAND | CLONE_CLEAR_SIGHAND)) == 3002 (CLONE_SIGHAND | CLONE_CLEAR_SIGHAND)) 3003 return false; 3004 3005 if ((kargs->flags & (CLONE_THREAD | CLONE_PARENT)) && 3006 kargs->exit_signal) 3007 return false; 3008 3009 if (!clone3_stack_valid(kargs)) 3010 return false; 3011 3012 return true; 3013 } 3014 3015 /** 3016 * sys_clone3 - create a new process with specific properties 3017 * @uargs: argument structure 3018 * @size: size of @uargs 3019 * 3020 * clone3() is the extensible successor to clone()/clone2(). 3021 * It takes a struct as argument that is versioned by its size. 3022 * 3023 * Return: On success, a positive PID for the child process. 3024 * On error, a negative errno number. 3025 */ 3026 SYSCALL_DEFINE2(clone3, struct clone_args __user *, uargs, size_t, size) 3027 { 3028 int err; 3029 3030 struct kernel_clone_args kargs; 3031 pid_t set_tid[MAX_PID_NS_LEVEL]; 3032 3033 #ifdef __ARCH_BROKEN_SYS_CLONE3 3034 #warning clone3() entry point is missing, please fix 3035 return -ENOSYS; 3036 #endif 3037 3038 kargs.set_tid = set_tid; 3039 3040 err = copy_clone_args_from_user(&kargs, uargs, size); 3041 if (err) 3042 return err; 3043 3044 if (!clone3_args_valid(&kargs)) 3045 return -EINVAL; 3046 3047 return kernel_clone(&kargs); 3048 } 3049 3050 void walk_process_tree(struct task_struct *top, proc_visitor visitor, void *data) 3051 { 3052 struct task_struct *leader, *parent, *child; 3053 int res; 3054 3055 read_lock(&tasklist_lock); 3056 leader = top = top->group_leader; 3057 down: 3058 for_each_thread(leader, parent) { 3059 list_for_each_entry(child, &parent->children, sibling) { 3060 res = visitor(child, data); 3061 if (res) { 3062 if (res < 0) 3063 goto out; 3064 leader = child; 3065 goto down; 3066 } 3067 up: 3068 ; 3069 } 3070 } 3071 3072 if (leader != top) { 3073 child = leader; 3074 parent = child->real_parent; 3075 leader = parent->group_leader; 3076 goto up; 3077 } 3078 out: 3079 read_unlock(&tasklist_lock); 3080 } 3081 3082 #ifndef ARCH_MIN_MMSTRUCT_ALIGN 3083 #define ARCH_MIN_MMSTRUCT_ALIGN 0 3084 #endif 3085 3086 static void sighand_ctor(void *data) 3087 { 3088 struct sighand_struct *sighand = data; 3089 3090 spin_lock_init(&sighand->siglock); 3091 init_waitqueue_head(&sighand->signalfd_wqh); 3092 } 3093 3094 void __init mm_cache_init(void) 3095 { 3096 unsigned int mm_size; 3097 3098 /* 3099 * The mm_cpumask is located at the end of mm_struct, and is 3100 * dynamically sized based on the maximum CPU number this system 3101 * can have, taking hotplug into account (nr_cpu_ids). 3102 */ 3103 mm_size = sizeof(struct mm_struct) + cpumask_size() + mm_cid_size(); 3104 3105 mm_cachep = kmem_cache_create_usercopy("mm_struct", 3106 mm_size, ARCH_MIN_MMSTRUCT_ALIGN, 3107 SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 3108 offsetof(struct mm_struct, saved_auxv), 3109 sizeof_field(struct mm_struct, saved_auxv), 3110 NULL); 3111 } 3112 3113 void __init proc_caches_init(void) 3114 { 3115 sighand_cachep = kmem_cache_create("sighand_cache", 3116 sizeof(struct sighand_struct), 0, 3117 SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_TYPESAFE_BY_RCU| 3118 SLAB_ACCOUNT, sighand_ctor); 3119 signal_cachep = kmem_cache_create("signal_cache", 3120 sizeof(struct signal_struct), 0, 3121 SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 3122 NULL); 3123 exec_state_init(); 3124 files_cachep = kmem_cache_create("files_cache", 3125 sizeof(struct files_struct), 0, 3126 SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 3127 NULL); 3128 fs_cachep = kmem_cache_create("fs_cache", 3129 sizeof(struct fs_struct), 0, 3130 SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, 3131 NULL); 3132 mmap_init(); 3133 nsproxy_cache_init(); 3134 } 3135 3136 /* 3137 * Check constraints on flags passed to the unshare system call. 3138 */ 3139 static int check_unshare_flags(unsigned long unshare_flags) 3140 { 3141 if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_SIGHAND| 3142 CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| 3143 CLONE_NS_ALL | UNSHARE_EMPTY_MNTNS)) 3144 return -EINVAL; 3145 /* 3146 * Not implemented, but pretend it works if there is nothing 3147 * to unshare. Note that unsharing the address space or the 3148 * signal handlers also need to unshare the signal queues (aka 3149 * CLONE_THREAD). 3150 */ 3151 if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) { 3152 if (!thread_group_empty(current)) 3153 return -EINVAL; 3154 } 3155 if (unshare_flags & (CLONE_SIGHAND | CLONE_VM)) { 3156 if (refcount_read(¤t->sighand->count) > 1) 3157 return -EINVAL; 3158 } 3159 if (unshare_flags & CLONE_VM) { 3160 if (!current_is_single_threaded()) 3161 return -EINVAL; 3162 } 3163 3164 return 0; 3165 } 3166 3167 /* 3168 * Unshare the filesystem structure if it is being shared 3169 */ 3170 static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) 3171 { 3172 struct fs_struct *fs = current->fs; 3173 3174 if (!(unshare_flags & CLONE_FS) || !fs) 3175 return 0; 3176 3177 /* don't need lock here; in the worst case we'll do useless copy */ 3178 if (!(unshare_flags & CLONE_NEWNS) && fs->users == 1) 3179 return 0; 3180 3181 *new_fsp = copy_fs_struct(fs); 3182 if (!*new_fsp) 3183 return -ENOMEM; 3184 3185 return 0; 3186 } 3187 3188 /* 3189 * Unshare file descriptor table if it is being shared 3190 */ 3191 static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp) 3192 { 3193 struct files_struct *fd = current->files; 3194 3195 if ((unshare_flags & CLONE_FILES) && 3196 (fd && atomic_read(&fd->count) > 1)) { 3197 fd = dup_fd(fd, NULL); 3198 if (IS_ERR(fd)) 3199 return PTR_ERR(fd); 3200 *new_fdp = fd; 3201 } 3202 3203 return 0; 3204 } 3205 3206 /* 3207 * unshare allows a process to 'unshare' part of the process 3208 * context which was originally shared using clone. copy_* 3209 * functions used by kernel_clone() cannot be used here directly 3210 * because they modify an inactive task_struct that is being 3211 * constructed. Here we are modifying the current, active, 3212 * task_struct. 3213 */ 3214 int ksys_unshare(unsigned long unshare_flags) 3215 { 3216 struct fs_struct *fs, *new_fs = NULL; 3217 struct files_struct *new_fd = NULL; 3218 struct cred *new_cred = NULL; 3219 struct nsproxy *new_nsproxy = NULL; 3220 int do_sysvsem = 0; 3221 int err; 3222 3223 /* 3224 * If unsharing a user namespace must also unshare the thread group 3225 * and unshare the filesystem root and working directories. 3226 */ 3227 if (unshare_flags & CLONE_NEWUSER) 3228 unshare_flags |= CLONE_THREAD | CLONE_FS; 3229 /* 3230 * If unsharing vm, must also unshare signal handlers. 3231 */ 3232 if (unshare_flags & CLONE_VM) 3233 unshare_flags |= CLONE_SIGHAND; 3234 /* 3235 * If unsharing a signal handlers, must also unshare the signal queues. 3236 */ 3237 if (unshare_flags & CLONE_SIGHAND) 3238 unshare_flags |= CLONE_THREAD; 3239 /* 3240 * If unsharing namespace, must also unshare filesystem information. 3241 */ 3242 if (unshare_flags & UNSHARE_EMPTY_MNTNS) 3243 unshare_flags |= CLONE_NEWNS; 3244 if (unshare_flags & CLONE_NEWNS) 3245 unshare_flags |= CLONE_FS; 3246 3247 err = check_unshare_flags(unshare_flags); 3248 if (err) 3249 goto bad_unshare_out; 3250 /* 3251 * CLONE_NEWIPC must also detach from the undolist: after switching 3252 * to a new ipc namespace, the semaphore arrays from the old 3253 * namespace are unreachable. 3254 */ 3255 if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM)) 3256 do_sysvsem = 1; 3257 err = unshare_fs(unshare_flags, &new_fs); 3258 if (err) 3259 goto bad_unshare_out; 3260 err = unshare_fd(unshare_flags, &new_fd); 3261 if (err) 3262 goto bad_unshare_cleanup_fs; 3263 err = unshare_userns(unshare_flags, &new_cred); 3264 if (err) 3265 goto bad_unshare_cleanup_fd; 3266 err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, 3267 new_cred, new_fs); 3268 if (err) 3269 goto bad_unshare_cleanup_cred; 3270 if (new_cred) { 3271 err = set_cred_ucounts(new_cred); 3272 if (err) 3273 goto bad_unshare_cleanup_nsproxy; 3274 } 3275 3276 if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) { 3277 if (do_sysvsem) { 3278 /* 3279 * CLONE_SYSVSEM is equivalent to sys_exit(). 3280 */ 3281 exit_sem(current); 3282 } 3283 if (unshare_flags & CLONE_NEWIPC) { 3284 /* Orphan segments in old ns (see sem above). */ 3285 exit_shm(current); 3286 shm_init_task(current); 3287 } 3288 3289 if (new_nsproxy) { 3290 switch_task_namespaces(current, new_nsproxy); 3291 new_nsproxy = NULL; 3292 } 3293 3294 task_lock(current); 3295 3296 if (new_fs) { 3297 fs = current->fs; 3298 read_seqlock_excl(&fs->seq); 3299 current->fs = new_fs; 3300 if (--fs->users) 3301 new_fs = NULL; 3302 else 3303 new_fs = fs; 3304 read_sequnlock_excl(&fs->seq); 3305 } 3306 3307 if (new_fd) 3308 swap(current->files, new_fd); 3309 3310 task_unlock(current); 3311 3312 if (new_cred) { 3313 /* Install the new user namespace */ 3314 commit_creds(new_cred); 3315 new_cred = NULL; 3316 } 3317 } 3318 3319 perf_event_namespaces(current); 3320 3321 bad_unshare_cleanup_nsproxy: 3322 if (new_nsproxy) 3323 put_nsproxy(new_nsproxy); 3324 bad_unshare_cleanup_cred: 3325 if (new_cred) 3326 put_cred(new_cred); 3327 bad_unshare_cleanup_fd: 3328 if (new_fd) 3329 put_files_struct(new_fd); 3330 bad_unshare_cleanup_fs: 3331 if (new_fs) 3332 free_fs_struct(new_fs); 3333 3334 bad_unshare_out: 3335 return err; 3336 } 3337 3338 SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) 3339 { 3340 return ksys_unshare(unshare_flags); 3341 } 3342 3343 /* 3344 * Helper to unshare the files of the current task. 3345 * We don't want to expose copy_files internals to 3346 * the exec layer of the kernel. 3347 */ 3348 3349 int unshare_files(void) 3350 { 3351 struct task_struct *task = current; 3352 struct files_struct *old, *copy = NULL; 3353 int error; 3354 3355 error = unshare_fd(CLONE_FILES, ©); 3356 if (error || !copy) 3357 return error; 3358 3359 old = task->files; 3360 task_lock(task); 3361 task->files = copy; 3362 task_unlock(task); 3363 put_files_struct(old); 3364 return 0; 3365 } 3366 3367 static int sysctl_max_threads(const struct ctl_table *table, int write, 3368 void *buffer, size_t *lenp, loff_t *ppos) 3369 { 3370 struct ctl_table t; 3371 int ret; 3372 int threads = max_threads; 3373 int min = 1; 3374 int max = MAX_THREADS; 3375 3376 t = *table; 3377 t.data = &threads; 3378 t.extra1 = &min; 3379 t.extra2 = &max; 3380 3381 ret = proc_dointvec_minmax(&t, write, buffer, lenp, ppos); 3382 if (ret || !write) 3383 return ret; 3384 3385 max_threads = threads; 3386 3387 return 0; 3388 } 3389 3390 static const struct ctl_table fork_sysctl_table[] = { 3391 { 3392 .procname = "threads-max", 3393 .data = NULL, 3394 .maxlen = sizeof(int), 3395 .mode = 0644, 3396 .proc_handler = sysctl_max_threads, 3397 }, 3398 }; 3399 3400 static int __init init_fork_sysctl(void) 3401 { 3402 register_sysctl_init("kernel", fork_sysctl_table); 3403 return 0; 3404 } 3405 3406 subsys_initcall(init_fork_sysctl); 3407