b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 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/coredump.h> |
| 20 | #include <linux/sched/user.h> |
| 21 | #include <linux/sched/numa_balancing.h> |
| 22 | #include <linux/sched/stat.h> |
| 23 | #include <linux/sched/task.h> |
| 24 | #include <linux/sched/task_stack.h> |
| 25 | #include <linux/sched/cputime.h> |
| 26 | #include <linux/seq_file.h> |
| 27 | #include <linux/rtmutex.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/unistd.h> |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/vmalloc.h> |
| 32 | #include <linux/completion.h> |
| 33 | #include <linux/personality.h> |
| 34 | #include <linux/mempolicy.h> |
| 35 | #include <linux/sem.h> |
| 36 | #include <linux/file.h> |
| 37 | #include <linux/fdtable.h> |
| 38 | #include <linux/iocontext.h> |
| 39 | #include <linux/key.h> |
| 40 | #include <linux/binfmts.h> |
| 41 | #include <linux/mman.h> |
| 42 | #include <linux/mmu_notifier.h> |
| 43 | #include <linux/hmm.h> |
| 44 | #include <linux/fs.h> |
| 45 | #include <linux/mm.h> |
| 46 | #include <linux/vmacache.h> |
| 47 | #include <linux/nsproxy.h> |
| 48 | #include <linux/capability.h> |
| 49 | #include <linux/cpu.h> |
| 50 | #include <linux/cgroup.h> |
| 51 | #include <linux/security.h> |
| 52 | #include <linux/hugetlb.h> |
| 53 | #include <linux/seccomp.h> |
| 54 | #include <linux/swap.h> |
| 55 | #include <linux/syscalls.h> |
| 56 | #include <linux/jiffies.h> |
| 57 | #include <linux/futex.h> |
| 58 | #include <linux/compat.h> |
| 59 | #include <linux/kthread.h> |
| 60 | #include <linux/task_io_accounting_ops.h> |
| 61 | #include <linux/rcupdate.h> |
| 62 | #include <linux/ptrace.h> |
| 63 | #include <linux/mount.h> |
| 64 | #include <linux/audit.h> |
| 65 | #include <linux/memcontrol.h> |
| 66 | #include <linux/ftrace.h> |
| 67 | #include <linux/proc_fs.h> |
| 68 | #include <linux/profile.h> |
| 69 | #include <linux/rmap.h> |
| 70 | #include <linux/ksm.h> |
| 71 | #include <linux/acct.h> |
| 72 | #include <linux/userfaultfd_k.h> |
| 73 | #include <linux/tsacct_kern.h> |
| 74 | #include <linux/cn_proc.h> |
| 75 | #include <linux/freezer.h> |
| 76 | #include <linux/delayacct.h> |
| 77 | #include <linux/taskstats_kern.h> |
| 78 | #include <linux/random.h> |
| 79 | #include <linux/tty.h> |
| 80 | #include <linux/blkdev.h> |
| 81 | #include <linux/fs_struct.h> |
| 82 | #include <linux/magic.h> |
| 83 | #include <linux/perf_event.h> |
| 84 | #include <linux/posix-timers.h> |
| 85 | #include <linux/user-return-notifier.h> |
| 86 | #include <linux/oom.h> |
| 87 | #include <linux/khugepaged.h> |
| 88 | #include <linux/signalfd.h> |
| 89 | #include <linux/uprobes.h> |
| 90 | #include <linux/aio.h> |
| 91 | #include <linux/compiler.h> |
| 92 | #include <linux/sysctl.h> |
| 93 | #include <linux/kcov.h> |
| 94 | #include <linux/livepatch.h> |
| 95 | #include <linux/thread_info.h> |
| 96 | #include <linux/cpufreq_times.h> |
| 97 | #include <linux/stackleak.h> |
| 98 | #include <linux/scs.h> |
| 99 | |
| 100 | #include <asm/pgtable.h> |
| 101 | #include <asm/pgalloc.h> |
| 102 | #include <linux/uaccess.h> |
| 103 | #include <asm/mmu_context.h> |
| 104 | #include <asm/cacheflush.h> |
| 105 | #include <asm/tlbflush.h> |
| 106 | |
| 107 | #include <trace/events/sched.h> |
| 108 | |
| 109 | #define CREATE_TRACE_POINTS |
| 110 | #include <trace/events/task.h> |
| 111 | |
| 112 | /* |
| 113 | * Minimum number of threads to boot the kernel |
| 114 | */ |
| 115 | #define MIN_THREADS 20 |
| 116 | |
| 117 | /* |
| 118 | * Maximum number of threads |
| 119 | */ |
| 120 | #define MAX_THREADS FUTEX_TID_MASK |
| 121 | |
| 122 | /* |
| 123 | * Protected counters by write_lock_irq(&tasklist_lock) |
| 124 | */ |
| 125 | unsigned long total_forks; /* Handle normal Linux uptimes. */ |
| 126 | int nr_threads; /* The idle threads do not count.. */ |
| 127 | |
| 128 | static int max_threads; /* tunable limit on nr_threads */ |
| 129 | |
| 130 | #define NAMED_ARRAY_INDEX(x) [x] = __stringify(x) |
| 131 | |
| 132 | static const char * const resident_page_types[] = { |
| 133 | NAMED_ARRAY_INDEX(MM_FILEPAGES), |
| 134 | NAMED_ARRAY_INDEX(MM_ANONPAGES), |
| 135 | NAMED_ARRAY_INDEX(MM_SWAPENTS), |
| 136 | NAMED_ARRAY_INDEX(MM_SHMEMPAGES), |
| 137 | }; |
| 138 | |
| 139 | DEFINE_PER_CPU(unsigned long, process_counts) = 0; |
| 140 | |
| 141 | __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ |
| 142 | |
| 143 | #ifdef CONFIG_PROVE_RCU |
| 144 | int lockdep_tasklist_lock_is_held(void) |
| 145 | { |
| 146 | return lockdep_is_held(&tasklist_lock); |
| 147 | } |
| 148 | EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held); |
| 149 | #endif /* #ifdef CONFIG_PROVE_RCU */ |
| 150 | |
| 151 | int nr_processes(void) |
| 152 | { |
| 153 | int cpu; |
| 154 | int total = 0; |
| 155 | |
| 156 | for_each_possible_cpu(cpu) |
| 157 | total += per_cpu(process_counts, cpu); |
| 158 | |
| 159 | return total; |
| 160 | } |
| 161 | |
| 162 | void __weak arch_release_task_struct(struct task_struct *tsk) |
| 163 | { |
| 164 | } |
| 165 | |
| 166 | #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR |
| 167 | static struct kmem_cache *task_struct_cachep; |
| 168 | |
| 169 | static inline struct task_struct *alloc_task_struct_node(int node) |
| 170 | { |
| 171 | return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node); |
| 172 | } |
| 173 | |
| 174 | static inline void free_task_struct(struct task_struct *tsk) |
| 175 | { |
| 176 | kmem_cache_free(task_struct_cachep, tsk); |
| 177 | } |
| 178 | #endif |
| 179 | |
| 180 | #ifndef CONFIG_ARCH_THREAD_STACK_ALLOCATOR |
| 181 | |
| 182 | /* |
| 183 | * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a |
| 184 | * kmemcache based allocator. |
| 185 | */ |
| 186 | # if THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK) |
| 187 | |
| 188 | #ifdef CONFIG_VMAP_STACK |
| 189 | /* |
| 190 | * vmalloc() is a bit slow, and calling vfree() enough times will force a TLB |
| 191 | * flush. Try to minimize the number of calls by caching stacks. |
| 192 | */ |
| 193 | #define NR_CACHED_STACKS 2 |
| 194 | static DEFINE_PER_CPU(struct vm_struct *, cached_stacks[NR_CACHED_STACKS]); |
| 195 | |
| 196 | static int free_vm_stack_cache(unsigned int cpu) |
| 197 | { |
| 198 | struct vm_struct **cached_vm_stacks = per_cpu_ptr(cached_stacks, cpu); |
| 199 | int i; |
| 200 | |
| 201 | for (i = 0; i < NR_CACHED_STACKS; i++) { |
| 202 | struct vm_struct *vm_stack = cached_vm_stacks[i]; |
| 203 | |
| 204 | if (!vm_stack) |
| 205 | continue; |
| 206 | |
| 207 | vfree(vm_stack->addr); |
| 208 | cached_vm_stacks[i] = NULL; |
| 209 | } |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | #endif |
| 214 | |
| 215 | static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node) |
| 216 | { |
| 217 | #ifdef CONFIG_VMAP_STACK |
| 218 | void *stack; |
| 219 | int i; |
| 220 | |
| 221 | for (i = 0; i < NR_CACHED_STACKS; i++) { |
| 222 | struct vm_struct *s; |
| 223 | |
| 224 | s = this_cpu_xchg(cached_stacks[i], NULL); |
| 225 | |
| 226 | if (!s) |
| 227 | continue; |
| 228 | |
| 229 | /* Clear stale pointers from reused stack. */ |
| 230 | memset(s->addr, 0, THREAD_SIZE); |
| 231 | |
| 232 | tsk->stack_vm_area = s; |
| 233 | tsk->stack = s->addr; |
| 234 | return s->addr; |
| 235 | } |
| 236 | |
| 237 | /* |
| 238 | * Allocated stacks are cached and later reused by new threads, |
| 239 | * so memcg accounting is performed manually on assigning/releasing |
| 240 | * stacks to tasks. Drop __GFP_ACCOUNT. |
| 241 | */ |
| 242 | stack = __vmalloc_node_range(THREAD_SIZE, THREAD_ALIGN, |
| 243 | VMALLOC_START, VMALLOC_END, |
| 244 | THREADINFO_GFP & ~__GFP_ACCOUNT, |
| 245 | PAGE_KERNEL, |
| 246 | 0, node, __builtin_return_address(0)); |
| 247 | |
| 248 | /* |
| 249 | * We can't call find_vm_area() in interrupt context, and |
| 250 | * free_thread_stack() can be called in interrupt context, |
| 251 | * so cache the vm_struct. |
| 252 | */ |
| 253 | if (stack) { |
| 254 | tsk->stack_vm_area = find_vm_area(stack); |
| 255 | tsk->stack = stack; |
| 256 | } |
| 257 | return stack; |
| 258 | #else |
| 259 | struct page *page = alloc_pages_node(node, THREADINFO_GFP, |
| 260 | THREAD_SIZE_ORDER); |
| 261 | |
| 262 | if (likely(page)) { |
| 263 | tsk->stack = page_address(page); |
| 264 | return tsk->stack; |
| 265 | } |
| 266 | return NULL; |
| 267 | #endif |
| 268 | } |
| 269 | |
| 270 | static inline void free_thread_stack(struct task_struct *tsk) |
| 271 | { |
| 272 | #ifdef CONFIG_VMAP_STACK |
| 273 | struct vm_struct *vm = task_stack_vm_area(tsk); |
| 274 | |
| 275 | if (vm) { |
| 276 | int i; |
| 277 | |
| 278 | for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) { |
| 279 | mod_memcg_page_state(vm->pages[i], |
| 280 | MEMCG_KERNEL_STACK_KB, |
| 281 | -(int)(PAGE_SIZE / 1024)); |
| 282 | |
| 283 | memcg_kmem_uncharge(vm->pages[i], 0); |
| 284 | } |
| 285 | |
| 286 | for (i = 0; i < NR_CACHED_STACKS; i++) { |
| 287 | if (this_cpu_cmpxchg(cached_stacks[i], |
| 288 | NULL, tsk->stack_vm_area) != NULL) |
| 289 | continue; |
| 290 | |
| 291 | return; |
| 292 | } |
| 293 | |
| 294 | vfree_atomic(tsk->stack); |
| 295 | return; |
| 296 | } |
| 297 | #endif |
| 298 | |
| 299 | __free_pages(virt_to_page(tsk->stack), THREAD_SIZE_ORDER); |
| 300 | } |
| 301 | # else |
| 302 | static struct kmem_cache *thread_stack_cache; |
| 303 | |
| 304 | static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, |
| 305 | int node) |
| 306 | { |
| 307 | unsigned long *stack; |
| 308 | stack = kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node); |
| 309 | tsk->stack = stack; |
| 310 | return stack; |
| 311 | } |
| 312 | |
| 313 | static void free_thread_stack(struct task_struct *tsk) |
| 314 | { |
| 315 | kmem_cache_free(thread_stack_cache, tsk->stack); |
| 316 | } |
| 317 | |
| 318 | void thread_stack_cache_init(void) |
| 319 | { |
| 320 | thread_stack_cache = kmem_cache_create_usercopy("thread_stack", |
| 321 | THREAD_SIZE, THREAD_SIZE, 0, 0, |
| 322 | THREAD_SIZE, NULL); |
| 323 | BUG_ON(thread_stack_cache == NULL); |
| 324 | } |
| 325 | # endif |
| 326 | #endif |
| 327 | |
| 328 | /* SLAB cache for signal_struct structures (tsk->signal) */ |
| 329 | static struct kmem_cache *signal_cachep; |
| 330 | |
| 331 | /* SLAB cache for sighand_struct structures (tsk->sighand) */ |
| 332 | struct kmem_cache *sighand_cachep; |
| 333 | |
| 334 | /* SLAB cache for files_struct structures (tsk->files) */ |
| 335 | struct kmem_cache *files_cachep; |
| 336 | |
| 337 | /* SLAB cache for fs_struct structures (tsk->fs) */ |
| 338 | struct kmem_cache *fs_cachep; |
| 339 | |
| 340 | /* SLAB cache for vm_area_struct structures */ |
| 341 | static struct kmem_cache *vm_area_cachep; |
| 342 | |
| 343 | /* SLAB cache for mm_struct structures (tsk->mm) */ |
| 344 | static struct kmem_cache *mm_cachep; |
| 345 | |
| 346 | struct vm_area_struct *vm_area_alloc(struct mm_struct *mm) |
| 347 | { |
| 348 | struct vm_area_struct *vma; |
| 349 | |
| 350 | vma = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); |
| 351 | if (vma) |
| 352 | vma_init(vma, mm); |
| 353 | return vma; |
| 354 | } |
| 355 | |
| 356 | struct vm_area_struct *vm_area_dup(struct vm_area_struct *orig) |
| 357 | { |
| 358 | struct vm_area_struct *new = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); |
| 359 | |
| 360 | if (new) { |
| 361 | *new = *orig; |
| 362 | INIT_LIST_HEAD(&new->anon_vma_chain); |
| 363 | } |
| 364 | return new; |
| 365 | } |
| 366 | |
| 367 | void vm_area_free(struct vm_area_struct *vma) |
| 368 | { |
| 369 | kmem_cache_free(vm_area_cachep, vma); |
| 370 | } |
| 371 | |
| 372 | static void account_kernel_stack(struct task_struct *tsk, int account) |
| 373 | { |
| 374 | void *stack = task_stack_page(tsk); |
| 375 | struct vm_struct *vm = task_stack_vm_area(tsk); |
| 376 | |
| 377 | BUILD_BUG_ON(IS_ENABLED(CONFIG_VMAP_STACK) && PAGE_SIZE % 1024 != 0); |
| 378 | |
| 379 | if (vm) { |
| 380 | int i; |
| 381 | |
| 382 | BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE); |
| 383 | |
| 384 | for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) { |
| 385 | mod_zone_page_state(page_zone(vm->pages[i]), |
| 386 | NR_KERNEL_STACK_KB, |
| 387 | PAGE_SIZE / 1024 * account); |
| 388 | } |
| 389 | } else { |
| 390 | /* |
| 391 | * All stack pages are in the same zone and belong to the |
| 392 | * same memcg. |
| 393 | */ |
| 394 | struct page *first_page = virt_to_page(stack); |
| 395 | |
| 396 | mod_zone_page_state(page_zone(first_page), NR_KERNEL_STACK_KB, |
| 397 | THREAD_SIZE / 1024 * account); |
| 398 | |
| 399 | mod_memcg_obj_state(stack, MEMCG_KERNEL_STACK_KB, |
| 400 | account * (THREAD_SIZE / 1024)); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | static int memcg_charge_kernel_stack(struct task_struct *tsk) |
| 405 | { |
| 406 | #ifdef CONFIG_VMAP_STACK |
| 407 | struct vm_struct *vm = task_stack_vm_area(tsk); |
| 408 | int ret; |
| 409 | |
| 410 | if (vm) { |
| 411 | int i; |
| 412 | |
| 413 | for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) { |
| 414 | /* |
| 415 | * If memcg_kmem_charge() fails, page->mem_cgroup |
| 416 | * pointer is NULL, and both memcg_kmem_uncharge() |
| 417 | * and mod_memcg_page_state() in free_thread_stack() |
| 418 | * will ignore this page. So it's safe. |
| 419 | */ |
| 420 | ret = memcg_kmem_charge(vm->pages[i], GFP_KERNEL, 0); |
| 421 | if (ret) |
| 422 | return ret; |
| 423 | |
| 424 | mod_memcg_page_state(vm->pages[i], |
| 425 | MEMCG_KERNEL_STACK_KB, |
| 426 | PAGE_SIZE / 1024); |
| 427 | } |
| 428 | } |
| 429 | #endif |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | static void release_task_stack(struct task_struct *tsk) |
| 434 | { |
| 435 | if (WARN_ON(tsk->state != TASK_DEAD)) |
| 436 | return; /* Better to leak the stack than to free prematurely */ |
| 437 | |
| 438 | account_kernel_stack(tsk, -1); |
| 439 | free_thread_stack(tsk); |
| 440 | tsk->stack = NULL; |
| 441 | #ifdef CONFIG_VMAP_STACK |
| 442 | tsk->stack_vm_area = NULL; |
| 443 | #endif |
| 444 | } |
| 445 | |
| 446 | #ifdef CONFIG_THREAD_INFO_IN_TASK |
| 447 | void put_task_stack(struct task_struct *tsk) |
| 448 | { |
| 449 | if (refcount_dec_and_test(&tsk->stack_refcount)) |
| 450 | release_task_stack(tsk); |
| 451 | } |
| 452 | #endif |
| 453 | |
| 454 | void free_task(struct task_struct *tsk) |
| 455 | { |
| 456 | cpufreq_task_times_exit(tsk); |
| 457 | scs_release(tsk); |
| 458 | |
| 459 | #ifndef CONFIG_THREAD_INFO_IN_TASK |
| 460 | /* |
| 461 | * The task is finally done with both the stack and thread_info, |
| 462 | * so free both. |
| 463 | */ |
| 464 | release_task_stack(tsk); |
| 465 | #else |
| 466 | /* |
| 467 | * If the task had a separate stack allocation, it should be gone |
| 468 | * by now. |
| 469 | */ |
| 470 | WARN_ON_ONCE(refcount_read(&tsk->stack_refcount) != 0); |
| 471 | #endif |
| 472 | rt_mutex_debug_task_free(tsk); |
| 473 | ftrace_graph_exit_task(tsk); |
| 474 | arch_release_task_struct(tsk); |
| 475 | if (tsk->flags & PF_KTHREAD) |
| 476 | free_kthread_struct(tsk); |
| 477 | free_task_struct(tsk); |
| 478 | } |
| 479 | EXPORT_SYMBOL(free_task); |
| 480 | |
| 481 | #ifdef CONFIG_MMU |
| 482 | static __latent_entropy int dup_mmap(struct mm_struct *mm, |
| 483 | struct mm_struct *oldmm) |
| 484 | { |
| 485 | struct vm_area_struct *mpnt, *tmp, *prev, **pprev; |
| 486 | struct rb_node **rb_link, *rb_parent; |
| 487 | int retval; |
| 488 | unsigned long charge; |
| 489 | LIST_HEAD(uf); |
| 490 | |
| 491 | uprobe_start_dup_mmap(); |
| 492 | if (down_write_killable(&oldmm->mmap_sem)) { |
| 493 | retval = -EINTR; |
| 494 | goto fail_uprobe_end; |
| 495 | } |
| 496 | flush_cache_dup_mm(oldmm); |
| 497 | uprobe_dup_mmap(oldmm, mm); |
| 498 | /* |
| 499 | * Not linked in yet - no deadlock potential: |
| 500 | */ |
| 501 | down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING); |
| 502 | |
| 503 | /* No ordering required: file already has been exposed. */ |
| 504 | RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm)); |
| 505 | |
| 506 | mm->total_vm = oldmm->total_vm; |
| 507 | mm->data_vm = oldmm->data_vm; |
| 508 | mm->exec_vm = oldmm->exec_vm; |
| 509 | mm->stack_vm = oldmm->stack_vm; |
| 510 | |
| 511 | rb_link = &mm->mm_rb.rb_node; |
| 512 | rb_parent = NULL; |
| 513 | pprev = &mm->mmap; |
| 514 | retval = ksm_fork(mm, oldmm); |
| 515 | if (retval) |
| 516 | goto out; |
| 517 | retval = khugepaged_fork(mm, oldmm); |
| 518 | if (retval) |
| 519 | goto out; |
| 520 | |
| 521 | prev = NULL; |
| 522 | for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) { |
| 523 | struct file *file; |
| 524 | |
| 525 | if (mpnt->vm_flags & VM_DONTCOPY) { |
| 526 | vm_stat_account(mm, mpnt->vm_flags, -vma_pages(mpnt)); |
| 527 | continue; |
| 528 | } |
| 529 | charge = 0; |
| 530 | /* |
| 531 | * Don't duplicate many vmas if we've been oom-killed (for |
| 532 | * example) |
| 533 | */ |
| 534 | if (fatal_signal_pending(current)) { |
| 535 | retval = -EINTR; |
| 536 | goto out; |
| 537 | } |
| 538 | if (mpnt->vm_flags & VM_ACCOUNT) { |
| 539 | unsigned long len = vma_pages(mpnt); |
| 540 | |
| 541 | if (security_vm_enough_memory_mm(oldmm, len)) /* sic */ |
| 542 | goto fail_nomem; |
| 543 | charge = len; |
| 544 | } |
| 545 | tmp = vm_area_dup(mpnt); |
| 546 | if (!tmp) |
| 547 | goto fail_nomem; |
| 548 | retval = vma_dup_policy(mpnt, tmp); |
| 549 | if (retval) |
| 550 | goto fail_nomem_policy; |
| 551 | tmp->vm_mm = mm; |
| 552 | retval = dup_userfaultfd(tmp, &uf); |
| 553 | if (retval) |
| 554 | goto fail_nomem_anon_vma_fork; |
| 555 | if (tmp->vm_flags & VM_WIPEONFORK) { |
| 556 | /* VM_WIPEONFORK gets a clean slate in the child. */ |
| 557 | tmp->anon_vma = NULL; |
| 558 | if (anon_vma_prepare(tmp)) |
| 559 | goto fail_nomem_anon_vma_fork; |
| 560 | } else if (anon_vma_fork(tmp, mpnt)) |
| 561 | goto fail_nomem_anon_vma_fork; |
| 562 | tmp->vm_flags &= ~(VM_LOCKED | VM_LOCKONFAULT); |
| 563 | tmp->vm_next = tmp->vm_prev = NULL; |
| 564 | file = tmp->vm_file; |
| 565 | if (file) { |
| 566 | struct inode *inode = file_inode(file); |
| 567 | struct address_space *mapping = file->f_mapping; |
| 568 | |
| 569 | get_file(file); |
| 570 | if (tmp->vm_flags & VM_DENYWRITE) |
| 571 | atomic_dec(&inode->i_writecount); |
| 572 | i_mmap_lock_write(mapping); |
| 573 | if (tmp->vm_flags & VM_SHARED) |
| 574 | atomic_inc(&mapping->i_mmap_writable); |
| 575 | flush_dcache_mmap_lock(mapping); |
| 576 | /* insert tmp into the share list, just after mpnt */ |
| 577 | vma_interval_tree_insert_after(tmp, mpnt, |
| 578 | &mapping->i_mmap); |
| 579 | flush_dcache_mmap_unlock(mapping); |
| 580 | i_mmap_unlock_write(mapping); |
| 581 | } |
| 582 | |
| 583 | /* |
| 584 | * Clear hugetlb-related page reserves for children. This only |
| 585 | * affects MAP_PRIVATE mappings. Faults generated by the child |
| 586 | * are not guaranteed to succeed, even if read-only |
| 587 | */ |
| 588 | if (is_vm_hugetlb_page(tmp)) |
| 589 | reset_vma_resv_huge_pages(tmp); |
| 590 | |
| 591 | /* |
| 592 | * Link in the new vma and copy the page table entries. |
| 593 | */ |
| 594 | *pprev = tmp; |
| 595 | pprev = &tmp->vm_next; |
| 596 | tmp->vm_prev = prev; |
| 597 | prev = tmp; |
| 598 | |
| 599 | __vma_link_rb(mm, tmp, rb_link, rb_parent); |
| 600 | rb_link = &tmp->vm_rb.rb_right; |
| 601 | rb_parent = &tmp->vm_rb; |
| 602 | |
| 603 | mm->map_count++; |
| 604 | if (!(tmp->vm_flags & VM_WIPEONFORK)) |
| 605 | retval = copy_page_range(mm, oldmm, mpnt); |
| 606 | |
| 607 | if (tmp->vm_ops && tmp->vm_ops->open) |
| 608 | tmp->vm_ops->open(tmp); |
| 609 | |
| 610 | if (retval) |
| 611 | goto out; |
| 612 | } |
| 613 | /* a new mm has just been created */ |
| 614 | retval = arch_dup_mmap(oldmm, mm); |
| 615 | out: |
| 616 | up_write(&mm->mmap_sem); |
| 617 | flush_tlb_mm(oldmm); |
| 618 | up_write(&oldmm->mmap_sem); |
| 619 | dup_userfaultfd_complete(&uf); |
| 620 | fail_uprobe_end: |
| 621 | uprobe_end_dup_mmap(); |
| 622 | return retval; |
| 623 | fail_nomem_anon_vma_fork: |
| 624 | mpol_put(vma_policy(tmp)); |
| 625 | fail_nomem_policy: |
| 626 | vm_area_free(tmp); |
| 627 | fail_nomem: |
| 628 | retval = -ENOMEM; |
| 629 | vm_unacct_memory(charge); |
| 630 | goto out; |
| 631 | } |
| 632 | |
| 633 | static inline int mm_alloc_pgd(struct mm_struct *mm) |
| 634 | { |
| 635 | mm->pgd = pgd_alloc(mm); |
| 636 | if (unlikely(!mm->pgd)) |
| 637 | return -ENOMEM; |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | static inline void mm_free_pgd(struct mm_struct *mm) |
| 642 | { |
| 643 | pgd_free(mm, mm->pgd); |
| 644 | } |
| 645 | #else |
| 646 | static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) |
| 647 | { |
| 648 | down_write(&oldmm->mmap_sem); |
| 649 | RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm)); |
| 650 | up_write(&oldmm->mmap_sem); |
| 651 | return 0; |
| 652 | } |
| 653 | #define mm_alloc_pgd(mm) (0) |
| 654 | #define mm_free_pgd(mm) |
| 655 | #endif /* CONFIG_MMU */ |
| 656 | |
| 657 | static void check_mm(struct mm_struct *mm) |
| 658 | { |
| 659 | int i; |
| 660 | |
| 661 | BUILD_BUG_ON_MSG(ARRAY_SIZE(resident_page_types) != NR_MM_COUNTERS, |
| 662 | "Please make sure 'struct resident_page_types[]' is updated as well"); |
| 663 | |
| 664 | for (i = 0; i < NR_MM_COUNTERS; i++) { |
| 665 | long x = atomic_long_read(&mm->rss_stat.count[i]); |
| 666 | |
| 667 | if (unlikely(x)) |
| 668 | pr_alert("BUG: Bad rss-counter state mm:%p type:%s val:%ld\n", |
| 669 | mm, resident_page_types[i], x); |
| 670 | } |
| 671 | |
| 672 | if (mm_pgtables_bytes(mm)) |
| 673 | pr_alert("BUG: non-zero pgtables_bytes on freeing mm: %ld\n", |
| 674 | mm_pgtables_bytes(mm)); |
| 675 | |
| 676 | #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS |
| 677 | VM_BUG_ON_MM(mm->pmd_huge_pte, mm); |
| 678 | #endif |
| 679 | } |
| 680 | |
| 681 | #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) |
| 682 | #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) |
| 683 | |
| 684 | /* |
| 685 | * Called when the last reference to the mm |
| 686 | * is dropped: either by a lazy thread or by |
| 687 | * mmput. Free the page directory and the mm. |
| 688 | */ |
| 689 | void __mmdrop(struct mm_struct *mm) |
| 690 | { |
| 691 | BUG_ON(mm == &init_mm); |
| 692 | WARN_ON_ONCE(mm == current->mm); |
| 693 | WARN_ON_ONCE(mm == current->active_mm); |
| 694 | mm_free_pgd(mm); |
| 695 | destroy_context(mm); |
| 696 | mmu_notifier_mm_destroy(mm); |
| 697 | check_mm(mm); |
| 698 | put_user_ns(mm->user_ns); |
| 699 | free_mm(mm); |
| 700 | } |
| 701 | EXPORT_SYMBOL_GPL(__mmdrop); |
| 702 | |
| 703 | static void mmdrop_async_fn(struct work_struct *work) |
| 704 | { |
| 705 | struct mm_struct *mm; |
| 706 | |
| 707 | mm = container_of(work, struct mm_struct, async_put_work); |
| 708 | __mmdrop(mm); |
| 709 | } |
| 710 | |
| 711 | static void mmdrop_async(struct mm_struct *mm) |
| 712 | { |
| 713 | if (unlikely(atomic_dec_and_test(&mm->mm_count))) { |
| 714 | INIT_WORK(&mm->async_put_work, mmdrop_async_fn); |
| 715 | schedule_work(&mm->async_put_work); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | static inline void free_signal_struct(struct signal_struct *sig) |
| 720 | { |
| 721 | taskstats_tgid_free(sig); |
| 722 | sched_autogroup_exit(sig); |
| 723 | /* |
| 724 | * __mmdrop is not safe to call from softirq context on x86 due to |
| 725 | * pgd_dtor so postpone it to the async context |
| 726 | */ |
| 727 | if (sig->oom_mm) |
| 728 | mmdrop_async(sig->oom_mm); |
| 729 | kmem_cache_free(signal_cachep, sig); |
| 730 | } |
| 731 | |
| 732 | static inline void put_signal_struct(struct signal_struct *sig) |
| 733 | { |
| 734 | if (refcount_dec_and_test(&sig->sigcnt)) |
| 735 | free_signal_struct(sig); |
| 736 | } |
| 737 | |
| 738 | void __put_task_struct(struct task_struct *tsk) |
| 739 | { |
| 740 | WARN_ON(!tsk->exit_state); |
| 741 | WARN_ON(refcount_read(&tsk->usage)); |
| 742 | WARN_ON(tsk == current); |
| 743 | |
| 744 | cgroup_free(tsk); |
| 745 | task_numa_free(tsk, true); |
| 746 | security_task_free(tsk); |
| 747 | exit_creds(tsk); |
| 748 | delayacct_tsk_free(tsk); |
| 749 | put_signal_struct(tsk->signal); |
| 750 | |
| 751 | if (!profile_handoff_task(tsk)) |
| 752 | free_task(tsk); |
| 753 | } |
| 754 | EXPORT_SYMBOL_GPL(__put_task_struct); |
| 755 | |
| 756 | void __put_task_struct_rcu_cb(struct rcu_head *rhp) |
| 757 | { |
| 758 | struct task_struct *task = container_of(rhp, struct task_struct, rcu); |
| 759 | |
| 760 | __put_task_struct(task); |
| 761 | } |
| 762 | EXPORT_SYMBOL_GPL(__put_task_struct_rcu_cb); |
| 763 | |
| 764 | void __init __weak arch_task_cache_init(void) { } |
| 765 | |
| 766 | /* |
| 767 | * set_max_threads |
| 768 | */ |
| 769 | static void set_max_threads(unsigned int max_threads_suggested) |
| 770 | { |
| 771 | u64 threads; |
| 772 | unsigned long nr_pages = totalram_pages(); |
| 773 | |
| 774 | /* |
| 775 | * The number of threads shall be limited such that the thread |
| 776 | * structures may only consume a small part of the available memory. |
| 777 | */ |
| 778 | if (fls64(nr_pages) + fls64(PAGE_SIZE) > 64) |
| 779 | threads = MAX_THREADS; |
| 780 | else |
| 781 | threads = div64_u64((u64) nr_pages * (u64) PAGE_SIZE, |
| 782 | (u64) THREAD_SIZE * 8UL); |
| 783 | |
| 784 | if (threads > max_threads_suggested) |
| 785 | threads = max_threads_suggested; |
| 786 | |
| 787 | max_threads = clamp_t(u64, threads, MIN_THREADS, MAX_THREADS); |
| 788 | } |
| 789 | |
| 790 | #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT |
| 791 | /* Initialized by the architecture: */ |
| 792 | int arch_task_struct_size __read_mostly; |
| 793 | #endif |
| 794 | |
| 795 | #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR |
| 796 | static void task_struct_whitelist(unsigned long *offset, unsigned long *size) |
| 797 | { |
| 798 | /* Fetch thread_struct whitelist for the architecture. */ |
| 799 | arch_thread_struct_whitelist(offset, size); |
| 800 | |
| 801 | /* |
| 802 | * Handle zero-sized whitelist or empty thread_struct, otherwise |
| 803 | * adjust offset to position of thread_struct in task_struct. |
| 804 | */ |
| 805 | if (unlikely(*size == 0)) |
| 806 | *offset = 0; |
| 807 | else |
| 808 | *offset += offsetof(struct task_struct, thread); |
| 809 | } |
| 810 | #endif /* CONFIG_ARCH_TASK_STRUCT_ALLOCATOR */ |
| 811 | |
| 812 | void __init fork_init(void) |
| 813 | { |
| 814 | int i; |
| 815 | #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR |
| 816 | #ifndef ARCH_MIN_TASKALIGN |
| 817 | #define ARCH_MIN_TASKALIGN 0 |
| 818 | #endif |
| 819 | int align = max_t(int, L1_CACHE_BYTES, ARCH_MIN_TASKALIGN); |
| 820 | unsigned long useroffset, usersize; |
| 821 | |
| 822 | /* create a slab on which task_structs can be allocated */ |
| 823 | task_struct_whitelist(&useroffset, &usersize); |
| 824 | task_struct_cachep = kmem_cache_create_usercopy("task_struct", |
| 825 | arch_task_struct_size, align, |
| 826 | SLAB_PANIC|SLAB_ACCOUNT, |
| 827 | useroffset, usersize, NULL); |
| 828 | #endif |
| 829 | |
| 830 | /* do the arch specific task caches init */ |
| 831 | arch_task_cache_init(); |
| 832 | |
| 833 | set_max_threads(MAX_THREADS); |
| 834 | |
| 835 | init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2; |
| 836 | init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2; |
| 837 | init_task.signal->rlim[RLIMIT_SIGPENDING] = |
| 838 | init_task.signal->rlim[RLIMIT_NPROC]; |
| 839 | |
| 840 | for (i = 0; i < UCOUNT_COUNTS; i++) { |
| 841 | init_user_ns.ucount_max[i] = max_threads/2; |
| 842 | } |
| 843 | |
| 844 | #ifdef CONFIG_VMAP_STACK |
| 845 | cpuhp_setup_state(CPUHP_BP_PREPARE_DYN, "fork:vm_stack_cache", |
| 846 | NULL, free_vm_stack_cache); |
| 847 | #endif |
| 848 | |
| 849 | scs_init(); |
| 850 | |
| 851 | lockdep_init_task(&init_task); |
| 852 | uprobes_init(); |
| 853 | } |
| 854 | |
| 855 | int __weak arch_dup_task_struct(struct task_struct *dst, |
| 856 | struct task_struct *src) |
| 857 | { |
| 858 | *dst = *src; |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | void set_task_stack_end_magic(struct task_struct *tsk) |
| 863 | { |
| 864 | unsigned long *stackend; |
| 865 | |
| 866 | stackend = end_of_stack(tsk); |
| 867 | *stackend = STACK_END_MAGIC; /* for overflow detection */ |
| 868 | } |
| 869 | |
| 870 | static struct task_struct *dup_task_struct(struct task_struct *orig, int node) |
| 871 | { |
| 872 | struct task_struct *tsk; |
| 873 | unsigned long *stack; |
| 874 | struct vm_struct *stack_vm_area __maybe_unused; |
| 875 | int err; |
| 876 | |
| 877 | if (node == NUMA_NO_NODE) |
| 878 | node = tsk_fork_get_node(orig); |
| 879 | tsk = alloc_task_struct_node(node); |
| 880 | if (!tsk) |
| 881 | return NULL; |
| 882 | |
| 883 | stack = alloc_thread_stack_node(tsk, node); |
| 884 | if (!stack) |
| 885 | goto free_tsk; |
| 886 | |
| 887 | if (memcg_charge_kernel_stack(tsk)) |
| 888 | goto free_stack; |
| 889 | |
| 890 | stack_vm_area = task_stack_vm_area(tsk); |
| 891 | |
| 892 | err = arch_dup_task_struct(tsk, orig); |
| 893 | |
| 894 | /* |
| 895 | * arch_dup_task_struct() clobbers the stack-related fields. Make |
| 896 | * sure they're properly initialized before using any stack-related |
| 897 | * functions again. |
| 898 | */ |
| 899 | tsk->stack = stack; |
| 900 | #ifdef CONFIG_VMAP_STACK |
| 901 | tsk->stack_vm_area = stack_vm_area; |
| 902 | #endif |
| 903 | #ifdef CONFIG_THREAD_INFO_IN_TASK |
| 904 | refcount_set(&tsk->stack_refcount, 1); |
| 905 | #endif |
| 906 | |
| 907 | if (err) |
| 908 | goto free_stack; |
| 909 | |
| 910 | err = scs_prepare(tsk, node); |
| 911 | if (err) |
| 912 | goto free_stack; |
| 913 | |
| 914 | #ifdef CONFIG_SECCOMP |
| 915 | /* |
| 916 | * We must handle setting up seccomp filters once we're under |
| 917 | * the sighand lock in case orig has changed between now and |
| 918 | * then. Until then, filter must be NULL to avoid messing up |
| 919 | * the usage counts on the error path calling free_task. |
| 920 | */ |
| 921 | tsk->seccomp.filter = NULL; |
| 922 | #endif |
| 923 | |
| 924 | setup_thread_stack(tsk, orig); |
| 925 | clear_user_return_notifier(tsk); |
| 926 | clear_tsk_need_resched(tsk); |
| 927 | set_task_stack_end_magic(tsk); |
| 928 | |
| 929 | #ifdef CONFIG_STACKPROTECTOR |
| 930 | tsk->stack_canary = get_random_canary(); |
| 931 | #endif |
| 932 | if (orig->cpus_ptr == &orig->cpus_mask) |
| 933 | tsk->cpus_ptr = &tsk->cpus_mask; |
| 934 | |
| 935 | /* |
| 936 | * One for the user space visible state that goes away when reaped. |
| 937 | * One for the scheduler. |
| 938 | */ |
| 939 | refcount_set(&tsk->rcu_users, 2); |
| 940 | /* One for the rcu users */ |
| 941 | refcount_set(&tsk->usage, 1); |
| 942 | #ifdef CONFIG_BLK_DEV_IO_TRACE |
| 943 | tsk->btrace_seq = 0; |
| 944 | #endif |
| 945 | tsk->splice_pipe = NULL; |
| 946 | tsk->task_frag.page = NULL; |
| 947 | tsk->wake_q.next = NULL; |
| 948 | |
| 949 | account_kernel_stack(tsk, 1); |
| 950 | |
| 951 | kcov_task_init(tsk); |
| 952 | |
| 953 | #ifdef CONFIG_FAULT_INJECTION |
| 954 | tsk->fail_nth = 0; |
| 955 | #endif |
| 956 | |
| 957 | #ifdef CONFIG_BLK_CGROUP |
| 958 | tsk->throttle_queue = NULL; |
| 959 | tsk->use_memdelay = 0; |
| 960 | #endif |
| 961 | |
| 962 | #ifdef CONFIG_MEMCG |
| 963 | tsk->active_memcg = NULL; |
| 964 | #endif |
| 965 | return tsk; |
| 966 | |
| 967 | free_stack: |
| 968 | free_thread_stack(tsk); |
| 969 | free_tsk: |
| 970 | free_task_struct(tsk); |
| 971 | return NULL; |
| 972 | } |
| 973 | |
| 974 | __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock); |
| 975 | |
| 976 | static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT; |
| 977 | |
| 978 | static int __init coredump_filter_setup(char *s) |
| 979 | { |
| 980 | default_dump_filter = |
| 981 | (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) & |
| 982 | MMF_DUMP_FILTER_MASK; |
| 983 | return 1; |
| 984 | } |
| 985 | |
| 986 | __setup("coredump_filter=", coredump_filter_setup); |
| 987 | |
| 988 | #include <linux/init_task.h> |
| 989 | |
| 990 | static void mm_init_aio(struct mm_struct *mm) |
| 991 | { |
| 992 | #ifdef CONFIG_AIO |
| 993 | spin_lock_init(&mm->ioctx_lock); |
| 994 | mm->ioctx_table = NULL; |
| 995 | #endif |
| 996 | } |
| 997 | |
| 998 | static __always_inline void mm_clear_owner(struct mm_struct *mm, |
| 999 | struct task_struct *p) |
| 1000 | { |
| 1001 | #ifdef CONFIG_MEMCG |
| 1002 | if (mm->owner == p) |
| 1003 | WRITE_ONCE(mm->owner, NULL); |
| 1004 | #endif |
| 1005 | } |
| 1006 | |
| 1007 | static void mm_init_owner(struct mm_struct *mm, struct task_struct *p) |
| 1008 | { |
| 1009 | #ifdef CONFIG_MEMCG |
| 1010 | mm->owner = p; |
| 1011 | #endif |
| 1012 | } |
| 1013 | |
| 1014 | static void mm_init_uprobes_state(struct mm_struct *mm) |
| 1015 | { |
| 1016 | #ifdef CONFIG_UPROBES |
| 1017 | mm->uprobes_state.xol_area = NULL; |
| 1018 | #endif |
| 1019 | } |
| 1020 | |
| 1021 | static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, |
| 1022 | struct user_namespace *user_ns) |
| 1023 | { |
| 1024 | mm->mmap = NULL; |
| 1025 | mm->mm_rb = RB_ROOT; |
| 1026 | mm->vmacache_seqnum = 0; |
| 1027 | atomic_set(&mm->mm_users, 1); |
| 1028 | atomic_set(&mm->mm_count, 1); |
| 1029 | init_rwsem(&mm->mmap_sem); |
| 1030 | INIT_LIST_HEAD(&mm->mmlist); |
| 1031 | mm->core_state = NULL; |
| 1032 | mm_pgtables_bytes_init(mm); |
| 1033 | mm->map_count = 0; |
| 1034 | mm->locked_vm = 0; |
| 1035 | atomic64_set(&mm->pinned_vm, 0); |
| 1036 | memset(&mm->rss_stat, 0, sizeof(mm->rss_stat)); |
| 1037 | spin_lock_init(&mm->page_table_lock); |
| 1038 | spin_lock_init(&mm->arg_lock); |
| 1039 | mm_init_cpumask(mm); |
| 1040 | mm_init_aio(mm); |
| 1041 | mm_init_owner(mm, p); |
| 1042 | RCU_INIT_POINTER(mm->exe_file, NULL); |
| 1043 | mmu_notifier_mm_init(mm); |
| 1044 | init_tlb_flush_pending(mm); |
| 1045 | #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS |
| 1046 | mm->pmd_huge_pte = NULL; |
| 1047 | #endif |
| 1048 | mm_init_uprobes_state(mm); |
| 1049 | hugetlb_count_init(mm); |
| 1050 | |
| 1051 | if (current->mm) { |
| 1052 | mm->flags = current->mm->flags & MMF_INIT_MASK; |
| 1053 | mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK; |
| 1054 | } else { |
| 1055 | mm->flags = default_dump_filter; |
| 1056 | mm->def_flags = 0; |
| 1057 | } |
| 1058 | |
| 1059 | if (mm_alloc_pgd(mm)) |
| 1060 | goto fail_nopgd; |
| 1061 | |
| 1062 | if (init_new_context(p, mm)) |
| 1063 | goto fail_nocontext; |
| 1064 | |
| 1065 | mm->user_ns = get_user_ns(user_ns); |
| 1066 | return mm; |
| 1067 | |
| 1068 | fail_nocontext: |
| 1069 | mm_free_pgd(mm); |
| 1070 | fail_nopgd: |
| 1071 | free_mm(mm); |
| 1072 | return NULL; |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * Allocate and initialize an mm_struct. |
| 1077 | */ |
| 1078 | struct mm_struct *mm_alloc(void) |
| 1079 | { |
| 1080 | struct mm_struct *mm; |
| 1081 | |
| 1082 | mm = allocate_mm(); |
| 1083 | if (!mm) |
| 1084 | return NULL; |
| 1085 | |
| 1086 | memset(mm, 0, sizeof(*mm)); |
| 1087 | return mm_init(mm, current, current_user_ns()); |
| 1088 | } |
| 1089 | |
| 1090 | static inline void __mmput(struct mm_struct *mm) |
| 1091 | { |
| 1092 | VM_BUG_ON(atomic_read(&mm->mm_users)); |
| 1093 | |
| 1094 | uprobe_clear_state(mm); |
| 1095 | exit_aio(mm); |
| 1096 | ksm_exit(mm); |
| 1097 | khugepaged_exit(mm); /* must run before exit_mmap */ |
| 1098 | exit_mmap(mm); |
| 1099 | mm_put_huge_zero_page(mm); |
| 1100 | set_mm_exe_file(mm, NULL); |
| 1101 | if (!list_empty(&mm->mmlist)) { |
| 1102 | spin_lock(&mmlist_lock); |
| 1103 | list_del(&mm->mmlist); |
| 1104 | spin_unlock(&mmlist_lock); |
| 1105 | } |
| 1106 | if (mm->binfmt) |
| 1107 | module_put(mm->binfmt->module); |
| 1108 | mmdrop(mm); |
| 1109 | } |
| 1110 | |
| 1111 | /* |
| 1112 | * Decrement the use count and release all resources for an mm. |
| 1113 | */ |
| 1114 | void mmput(struct mm_struct *mm) |
| 1115 | { |
| 1116 | might_sleep(); |
| 1117 | |
| 1118 | if (atomic_dec_and_test(&mm->mm_users)) |
| 1119 | __mmput(mm); |
| 1120 | } |
| 1121 | EXPORT_SYMBOL_GPL(mmput); |
| 1122 | |
| 1123 | #ifdef CONFIG_MMU |
| 1124 | static void mmput_async_fn(struct work_struct *work) |
| 1125 | { |
| 1126 | struct mm_struct *mm = container_of(work, struct mm_struct, |
| 1127 | async_put_work); |
| 1128 | |
| 1129 | __mmput(mm); |
| 1130 | } |
| 1131 | |
| 1132 | void mmput_async(struct mm_struct *mm) |
| 1133 | { |
| 1134 | if (atomic_dec_and_test(&mm->mm_users)) { |
| 1135 | INIT_WORK(&mm->async_put_work, mmput_async_fn); |
| 1136 | schedule_work(&mm->async_put_work); |
| 1137 | } |
| 1138 | } |
| 1139 | #endif |
| 1140 | |
| 1141 | /** |
| 1142 | * set_mm_exe_file - change a reference to the mm's executable file |
| 1143 | * |
| 1144 | * This changes mm's executable file (shown as symlink /proc/[pid]/exe). |
| 1145 | * |
| 1146 | * Main users are mmput() and sys_execve(). Callers prevent concurrent |
| 1147 | * invocations: in mmput() nobody alive left, in execve task is single |
| 1148 | * threaded. sys_prctl(PR_SET_MM_MAP/EXE_FILE) also needs to set the |
| 1149 | * mm->exe_file, but does so without using set_mm_exe_file() in order |
| 1150 | * to do avoid the need for any locks. |
| 1151 | */ |
| 1152 | void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) |
| 1153 | { |
| 1154 | struct file *old_exe_file; |
| 1155 | |
| 1156 | /* |
| 1157 | * It is safe to dereference the exe_file without RCU as |
| 1158 | * this function is only called if nobody else can access |
| 1159 | * this mm -- see comment above for justification. |
| 1160 | */ |
| 1161 | old_exe_file = rcu_dereference_raw(mm->exe_file); |
| 1162 | |
| 1163 | if (new_exe_file) |
| 1164 | get_file(new_exe_file); |
| 1165 | rcu_assign_pointer(mm->exe_file, new_exe_file); |
| 1166 | if (old_exe_file) |
| 1167 | fput(old_exe_file); |
| 1168 | } |
| 1169 | |
| 1170 | /** |
| 1171 | * get_mm_exe_file - acquire a reference to the mm's executable file |
| 1172 | * |
| 1173 | * Returns %NULL if mm has no associated executable file. |
| 1174 | * User must release file via fput(). |
| 1175 | */ |
| 1176 | struct file *get_mm_exe_file(struct mm_struct *mm) |
| 1177 | { |
| 1178 | struct file *exe_file; |
| 1179 | |
| 1180 | rcu_read_lock(); |
| 1181 | exe_file = rcu_dereference(mm->exe_file); |
| 1182 | if (exe_file && !get_file_rcu(exe_file)) |
| 1183 | exe_file = NULL; |
| 1184 | rcu_read_unlock(); |
| 1185 | return exe_file; |
| 1186 | } |
| 1187 | EXPORT_SYMBOL(get_mm_exe_file); |
| 1188 | |
| 1189 | /** |
| 1190 | * get_task_exe_file - acquire a reference to the task's executable file |
| 1191 | * |
| 1192 | * Returns %NULL if task's mm (if any) has no associated executable file or |
| 1193 | * this is a kernel thread with borrowed mm (see the comment above get_task_mm). |
| 1194 | * User must release file via fput(). |
| 1195 | */ |
| 1196 | struct file *get_task_exe_file(struct task_struct *task) |
| 1197 | { |
| 1198 | struct file *exe_file = NULL; |
| 1199 | struct mm_struct *mm; |
| 1200 | |
| 1201 | task_lock(task); |
| 1202 | mm = task->mm; |
| 1203 | if (mm) { |
| 1204 | if (!(task->flags & PF_KTHREAD)) |
| 1205 | exe_file = get_mm_exe_file(mm); |
| 1206 | } |
| 1207 | task_unlock(task); |
| 1208 | return exe_file; |
| 1209 | } |
| 1210 | EXPORT_SYMBOL(get_task_exe_file); |
| 1211 | |
| 1212 | /** |
| 1213 | * get_task_mm - acquire a reference to the task's mm |
| 1214 | * |
| 1215 | * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning |
| 1216 | * this kernel workthread has transiently adopted a user mm with use_mm, |
| 1217 | * to do its AIO) is not set and if so returns a reference to it, after |
| 1218 | * bumping up the use count. User must release the mm via mmput() |
| 1219 | * after use. Typically used by /proc and ptrace. |
| 1220 | */ |
| 1221 | struct mm_struct *get_task_mm(struct task_struct *task) |
| 1222 | { |
| 1223 | struct mm_struct *mm; |
| 1224 | |
| 1225 | task_lock(task); |
| 1226 | mm = task->mm; |
| 1227 | if (mm) { |
| 1228 | if (task->flags & PF_KTHREAD) |
| 1229 | mm = NULL; |
| 1230 | else |
| 1231 | mmget(mm); |
| 1232 | } |
| 1233 | task_unlock(task); |
| 1234 | return mm; |
| 1235 | } |
| 1236 | EXPORT_SYMBOL_GPL(get_task_mm); |
| 1237 | |
| 1238 | struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) |
| 1239 | { |
| 1240 | struct mm_struct *mm; |
| 1241 | int err; |
| 1242 | |
| 1243 | err = down_read_killable(&task->signal->exec_update_lock); |
| 1244 | if (err) |
| 1245 | return ERR_PTR(err); |
| 1246 | |
| 1247 | mm = get_task_mm(task); |
| 1248 | if (mm && mm != current->mm && |
| 1249 | !ptrace_may_access(task, mode)) { |
| 1250 | mmput(mm); |
| 1251 | mm = ERR_PTR(-EACCES); |
| 1252 | } |
| 1253 | up_read(&task->signal->exec_update_lock); |
| 1254 | |
| 1255 | return mm; |
| 1256 | } |
| 1257 | |
| 1258 | static void complete_vfork_done(struct task_struct *tsk) |
| 1259 | { |
| 1260 | struct completion *vfork; |
| 1261 | |
| 1262 | task_lock(tsk); |
| 1263 | vfork = tsk->vfork_done; |
| 1264 | if (likely(vfork)) { |
| 1265 | tsk->vfork_done = NULL; |
| 1266 | complete(vfork); |
| 1267 | } |
| 1268 | task_unlock(tsk); |
| 1269 | } |
| 1270 | |
| 1271 | static int wait_for_vfork_done(struct task_struct *child, |
| 1272 | struct completion *vfork) |
| 1273 | { |
| 1274 | int killed; |
| 1275 | |
| 1276 | freezer_do_not_count(); |
| 1277 | cgroup_enter_frozen(); |
| 1278 | killed = wait_for_completion_killable(vfork); |
| 1279 | cgroup_leave_frozen(false); |
| 1280 | freezer_count(); |
| 1281 | |
| 1282 | if (killed) { |
| 1283 | task_lock(child); |
| 1284 | child->vfork_done = NULL; |
| 1285 | task_unlock(child); |
| 1286 | } |
| 1287 | |
| 1288 | put_task_struct(child); |
| 1289 | return killed; |
| 1290 | } |
| 1291 | |
| 1292 | /* Please note the differences between mmput and mm_release. |
| 1293 | * mmput is called whenever we stop holding onto a mm_struct, |
| 1294 | * error success whatever. |
| 1295 | * |
| 1296 | * mm_release is called after a mm_struct has been removed |
| 1297 | * from the current process. |
| 1298 | * |
| 1299 | * This difference is important for error handling, when we |
| 1300 | * only half set up a mm_struct for a new process and need to restore |
| 1301 | * the old one. Because we mmput the new mm_struct before |
| 1302 | * restoring the old one. . . |
| 1303 | * Eric Biederman 10 January 1998 |
| 1304 | */ |
| 1305 | static void mm_release(struct task_struct *tsk, struct mm_struct *mm) |
| 1306 | { |
| 1307 | uprobe_free_utask(tsk); |
| 1308 | |
| 1309 | /* Get rid of any cached register state */ |
| 1310 | deactivate_mm(tsk, mm); |
| 1311 | |
| 1312 | /* |
| 1313 | * Signal userspace if we're not exiting with a core dump |
| 1314 | * because we want to leave the value intact for debugging |
| 1315 | * purposes. |
| 1316 | */ |
| 1317 | if (tsk->clear_child_tid) { |
| 1318 | if (!(tsk->signal->flags & SIGNAL_GROUP_COREDUMP) && |
| 1319 | atomic_read(&mm->mm_users) > 1) { |
| 1320 | /* |
| 1321 | * We don't check the error code - if userspace has |
| 1322 | * not set up a proper pointer then tough luck. |
| 1323 | */ |
| 1324 | put_user(0, tsk->clear_child_tid); |
| 1325 | do_futex(tsk->clear_child_tid, FUTEX_WAKE, |
| 1326 | 1, NULL, NULL, 0, 0); |
| 1327 | } |
| 1328 | tsk->clear_child_tid = NULL; |
| 1329 | } |
| 1330 | |
| 1331 | /* |
| 1332 | * All done, finally we can wake up parent and return this mm to him. |
| 1333 | * Also kthread_stop() uses this completion for synchronization. |
| 1334 | */ |
| 1335 | if (tsk->vfork_done) |
| 1336 | complete_vfork_done(tsk); |
| 1337 | } |
| 1338 | |
| 1339 | void exit_mm_release(struct task_struct *tsk, struct mm_struct *mm) |
| 1340 | { |
| 1341 | futex_exit_release(tsk); |
| 1342 | mm_release(tsk, mm); |
| 1343 | } |
| 1344 | |
| 1345 | void exec_mm_release(struct task_struct *tsk, struct mm_struct *mm) |
| 1346 | { |
| 1347 | futex_exec_release(tsk); |
| 1348 | mm_release(tsk, mm); |
| 1349 | } |
| 1350 | |
| 1351 | /** |
| 1352 | * dup_mm() - duplicates an existing mm structure |
| 1353 | * @tsk: the task_struct with which the new mm will be associated. |
| 1354 | * @oldmm: the mm to duplicate. |
| 1355 | * |
| 1356 | * Allocates a new mm structure and duplicates the provided @oldmm structure |
| 1357 | * content into it. |
| 1358 | * |
| 1359 | * Return: the duplicated mm or NULL on failure. |
| 1360 | */ |
| 1361 | static struct mm_struct *dup_mm(struct task_struct *tsk, |
| 1362 | struct mm_struct *oldmm) |
| 1363 | { |
| 1364 | struct mm_struct *mm; |
| 1365 | int err; |
| 1366 | |
| 1367 | mm = allocate_mm(); |
| 1368 | if (!mm) |
| 1369 | goto fail_nomem; |
| 1370 | |
| 1371 | memcpy(mm, oldmm, sizeof(*mm)); |
| 1372 | |
| 1373 | if (!mm_init(mm, tsk, mm->user_ns)) |
| 1374 | goto fail_nomem; |
| 1375 | |
| 1376 | err = dup_mmap(mm, oldmm); |
| 1377 | if (err) |
| 1378 | goto free_pt; |
| 1379 | |
| 1380 | mm->hiwater_rss = get_mm_rss(mm); |
| 1381 | mm->hiwater_vm = mm->total_vm; |
| 1382 | |
| 1383 | if (mm->binfmt && !try_module_get(mm->binfmt->module)) |
| 1384 | goto free_pt; |
| 1385 | |
| 1386 | return mm; |
| 1387 | |
| 1388 | free_pt: |
| 1389 | /* don't put binfmt in mmput, we haven't got module yet */ |
| 1390 | mm->binfmt = NULL; |
| 1391 | mm_init_owner(mm, NULL); |
| 1392 | mmput(mm); |
| 1393 | |
| 1394 | fail_nomem: |
| 1395 | return NULL; |
| 1396 | } |
| 1397 | |
| 1398 | static int copy_mm(unsigned long clone_flags, struct task_struct *tsk) |
| 1399 | { |
| 1400 | struct mm_struct *mm, *oldmm; |
| 1401 | int retval; |
| 1402 | |
| 1403 | tsk->min_flt = tsk->maj_flt = 0; |
| 1404 | tsk->nvcsw = tsk->nivcsw = 0; |
| 1405 | #ifdef CONFIG_DETECT_HUNG_TASK |
| 1406 | tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw; |
| 1407 | tsk->last_switch_time = 0; |
| 1408 | #endif |
| 1409 | |
| 1410 | tsk->mm = NULL; |
| 1411 | tsk->active_mm = NULL; |
| 1412 | |
| 1413 | /* |
| 1414 | * Are we cloning a kernel thread? |
| 1415 | * |
| 1416 | * We need to steal a active VM for that.. |
| 1417 | */ |
| 1418 | oldmm = current->mm; |
| 1419 | if (!oldmm) |
| 1420 | return 0; |
| 1421 | |
| 1422 | /* initialize the new vmacache entries */ |
| 1423 | vmacache_flush(tsk); |
| 1424 | |
| 1425 | if (clone_flags & CLONE_VM) { |
| 1426 | mmget(oldmm); |
| 1427 | mm = oldmm; |
| 1428 | goto good_mm; |
| 1429 | } |
| 1430 | |
| 1431 | retval = -ENOMEM; |
| 1432 | mm = dup_mm(tsk, current->mm); |
| 1433 | if (!mm) |
| 1434 | goto fail_nomem; |
| 1435 | |
| 1436 | good_mm: |
| 1437 | tsk->mm = mm; |
| 1438 | tsk->active_mm = mm; |
| 1439 | return 0; |
| 1440 | |
| 1441 | fail_nomem: |
| 1442 | return retval; |
| 1443 | } |
| 1444 | |
| 1445 | static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) |
| 1446 | { |
| 1447 | struct fs_struct *fs = current->fs; |
| 1448 | if (clone_flags & CLONE_FS) { |
| 1449 | /* tsk->fs is already what we want */ |
| 1450 | spin_lock(&fs->lock); |
| 1451 | if (fs->in_exec) { |
| 1452 | spin_unlock(&fs->lock); |
| 1453 | return -EAGAIN; |
| 1454 | } |
| 1455 | fs->users++; |
| 1456 | spin_unlock(&fs->lock); |
| 1457 | return 0; |
| 1458 | } |
| 1459 | tsk->fs = copy_fs_struct(fs); |
| 1460 | if (!tsk->fs) |
| 1461 | return -ENOMEM; |
| 1462 | return 0; |
| 1463 | } |
| 1464 | |
| 1465 | static int copy_files(unsigned long clone_flags, struct task_struct *tsk) |
| 1466 | { |
| 1467 | struct files_struct *oldf, *newf; |
| 1468 | int error = 0; |
| 1469 | |
| 1470 | /* |
| 1471 | * A background process may not have any files ... |
| 1472 | */ |
| 1473 | oldf = current->files; |
| 1474 | if (!oldf) |
| 1475 | goto out; |
| 1476 | |
| 1477 | if (clone_flags & CLONE_FILES) { |
| 1478 | atomic_inc(&oldf->count); |
| 1479 | goto out; |
| 1480 | } |
| 1481 | |
| 1482 | newf = dup_fd(oldf, &error); |
| 1483 | if (!newf) |
| 1484 | goto out; |
| 1485 | |
| 1486 | tsk->files = newf; |
| 1487 | error = 0; |
| 1488 | out: |
| 1489 | return error; |
| 1490 | } |
| 1491 | |
| 1492 | static int copy_io(unsigned long clone_flags, struct task_struct *tsk) |
| 1493 | { |
| 1494 | #ifdef CONFIG_BLOCK |
| 1495 | struct io_context *ioc = current->io_context; |
| 1496 | struct io_context *new_ioc; |
| 1497 | |
| 1498 | if (!ioc) |
| 1499 | return 0; |
| 1500 | /* |
| 1501 | * Share io context with parent, if CLONE_IO is set |
| 1502 | */ |
| 1503 | if (clone_flags & CLONE_IO) { |
| 1504 | ioc_task_link(ioc); |
| 1505 | tsk->io_context = ioc; |
| 1506 | } else if (ioprio_valid(ioc->ioprio)) { |
| 1507 | new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE); |
| 1508 | if (unlikely(!new_ioc)) |
| 1509 | return -ENOMEM; |
| 1510 | |
| 1511 | new_ioc->ioprio = ioc->ioprio; |
| 1512 | put_io_context(new_ioc); |
| 1513 | } |
| 1514 | #endif |
| 1515 | return 0; |
| 1516 | } |
| 1517 | |
| 1518 | static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk) |
| 1519 | { |
| 1520 | struct sighand_struct *sig; |
| 1521 | |
| 1522 | if (clone_flags & CLONE_SIGHAND) { |
| 1523 | refcount_inc(¤t->sighand->count); |
| 1524 | return 0; |
| 1525 | } |
| 1526 | sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); |
| 1527 | rcu_assign_pointer(tsk->sighand, sig); |
| 1528 | if (!sig) |
| 1529 | return -ENOMEM; |
| 1530 | |
| 1531 | refcount_set(&sig->count, 1); |
| 1532 | spin_lock_irq(¤t->sighand->siglock); |
| 1533 | memcpy(sig->action, current->sighand->action, sizeof(sig->action)); |
| 1534 | spin_unlock_irq(¤t->sighand->siglock); |
| 1535 | return 0; |
| 1536 | } |
| 1537 | |
| 1538 | void __cleanup_sighand(struct sighand_struct *sighand) |
| 1539 | { |
| 1540 | if (refcount_dec_and_test(&sighand->count)) { |
| 1541 | signalfd_cleanup(sighand); |
| 1542 | /* |
| 1543 | * sighand_cachep is SLAB_TYPESAFE_BY_RCU so we can free it |
| 1544 | * without an RCU grace period, see __lock_task_sighand(). |
| 1545 | */ |
| 1546 | kmem_cache_free(sighand_cachep, sighand); |
| 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | /* |
| 1551 | * Initialize POSIX timer handling for a thread group. |
| 1552 | */ |
| 1553 | static void posix_cpu_timers_init_group(struct signal_struct *sig) |
| 1554 | { |
| 1555 | struct posix_cputimers *pct = &sig->posix_cputimers; |
| 1556 | unsigned long cpu_limit; |
| 1557 | |
| 1558 | cpu_limit = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur); |
| 1559 | posix_cputimers_group_init(pct, cpu_limit); |
| 1560 | } |
| 1561 | |
| 1562 | static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) |
| 1563 | { |
| 1564 | struct signal_struct *sig; |
| 1565 | |
| 1566 | if (clone_flags & CLONE_THREAD) |
| 1567 | return 0; |
| 1568 | |
| 1569 | sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL); |
| 1570 | tsk->signal = sig; |
| 1571 | if (!sig) |
| 1572 | return -ENOMEM; |
| 1573 | |
| 1574 | sig->nr_threads = 1; |
| 1575 | atomic_set(&sig->live, 1); |
| 1576 | refcount_set(&sig->sigcnt, 1); |
| 1577 | |
| 1578 | /* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */ |
| 1579 | sig->thread_head = (struct list_head)LIST_HEAD_INIT(tsk->thread_node); |
| 1580 | tsk->thread_node = (struct list_head)LIST_HEAD_INIT(sig->thread_head); |
| 1581 | |
| 1582 | init_waitqueue_head(&sig->wait_chldexit); |
| 1583 | sig->curr_target = tsk; |
| 1584 | init_sigpending(&sig->shared_pending); |
| 1585 | INIT_HLIST_HEAD(&sig->multiprocess); |
| 1586 | seqlock_init(&sig->stats_lock); |
| 1587 | prev_cputime_init(&sig->prev_cputime); |
| 1588 | |
| 1589 | #ifdef CONFIG_POSIX_TIMERS |
| 1590 | INIT_LIST_HEAD(&sig->posix_timers); |
| 1591 | hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 1592 | sig->real_timer.function = it_real_fn; |
| 1593 | #endif |
| 1594 | |
| 1595 | task_lock(current->group_leader); |
| 1596 | memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim); |
| 1597 | task_unlock(current->group_leader); |
| 1598 | |
| 1599 | posix_cpu_timers_init_group(sig); |
| 1600 | |
| 1601 | tty_audit_fork(sig); |
| 1602 | sched_autogroup_fork(sig); |
| 1603 | |
| 1604 | sig->oom_score_adj = current->signal->oom_score_adj; |
| 1605 | sig->oom_score_adj_min = current->signal->oom_score_adj_min; |
| 1606 | |
| 1607 | mutex_init(&sig->cred_guard_mutex); |
| 1608 | init_rwsem(&sig->exec_update_lock); |
| 1609 | |
| 1610 | return 0; |
| 1611 | } |
| 1612 | |
| 1613 | static void copy_seccomp(struct task_struct *p) |
| 1614 | { |
| 1615 | #ifdef CONFIG_SECCOMP |
| 1616 | /* |
| 1617 | * Must be called with sighand->lock held, which is common to |
| 1618 | * all threads in the group. Holding cred_guard_mutex is not |
| 1619 | * needed because this new task is not yet running and cannot |
| 1620 | * be racing exec. |
| 1621 | */ |
| 1622 | assert_spin_locked(¤t->sighand->siglock); |
| 1623 | |
| 1624 | /* Ref-count the new filter user, and assign it. */ |
| 1625 | get_seccomp_filter(current); |
| 1626 | p->seccomp = current->seccomp; |
| 1627 | |
| 1628 | /* |
| 1629 | * Explicitly enable no_new_privs here in case it got set |
| 1630 | * between the task_struct being duplicated and holding the |
| 1631 | * sighand lock. The seccomp state and nnp must be in sync. |
| 1632 | */ |
| 1633 | if (task_no_new_privs(current)) |
| 1634 | task_set_no_new_privs(p); |
| 1635 | |
| 1636 | /* |
| 1637 | * If the parent gained a seccomp mode after copying thread |
| 1638 | * flags and between before we held the sighand lock, we have |
| 1639 | * to manually enable the seccomp thread flag here. |
| 1640 | */ |
| 1641 | if (p->seccomp.mode != SECCOMP_MODE_DISABLED) |
| 1642 | set_tsk_thread_flag(p, TIF_SECCOMP); |
| 1643 | #endif |
| 1644 | } |
| 1645 | |
| 1646 | SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr) |
| 1647 | { |
| 1648 | current->clear_child_tid = tidptr; |
| 1649 | |
| 1650 | return task_pid_vnr(current); |
| 1651 | } |
| 1652 | |
| 1653 | static void rt_mutex_init_task(struct task_struct *p) |
| 1654 | { |
| 1655 | raw_spin_lock_init(&p->pi_lock); |
| 1656 | #ifdef CONFIG_RT_MUTEXES |
| 1657 | p->pi_waiters = RB_ROOT_CACHED; |
| 1658 | p->pi_top_task = NULL; |
| 1659 | p->pi_blocked_on = NULL; |
| 1660 | #endif |
| 1661 | } |
| 1662 | |
| 1663 | static inline void init_task_pid_links(struct task_struct *task) |
| 1664 | { |
| 1665 | enum pid_type type; |
| 1666 | |
| 1667 | for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { |
| 1668 | INIT_HLIST_NODE(&task->pid_links[type]); |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | static inline void |
| 1673 | init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid) |
| 1674 | { |
| 1675 | if (type == PIDTYPE_PID) |
| 1676 | task->thread_pid = pid; |
| 1677 | else |
| 1678 | task->signal->pids[type] = pid; |
| 1679 | } |
| 1680 | |
| 1681 | static inline void rcu_copy_process(struct task_struct *p) |
| 1682 | { |
| 1683 | #ifdef CONFIG_PREEMPT_RCU |
| 1684 | p->rcu_read_lock_nesting = 0; |
| 1685 | p->rcu_read_unlock_special.s = 0; |
| 1686 | p->rcu_blocked_node = NULL; |
| 1687 | INIT_LIST_HEAD(&p->rcu_node_entry); |
| 1688 | #endif /* #ifdef CONFIG_PREEMPT_RCU */ |
| 1689 | #ifdef CONFIG_TASKS_RCU |
| 1690 | p->rcu_tasks_holdout = false; |
| 1691 | INIT_LIST_HEAD(&p->rcu_tasks_holdout_list); |
| 1692 | p->rcu_tasks_idle_cpu = -1; |
| 1693 | #endif /* #ifdef CONFIG_TASKS_RCU */ |
| 1694 | } |
| 1695 | |
| 1696 | struct pid *pidfd_pid(const struct file *file) |
| 1697 | { |
| 1698 | if (file->f_op == &pidfd_fops) |
| 1699 | return file->private_data; |
| 1700 | |
| 1701 | return ERR_PTR(-EBADF); |
| 1702 | } |
| 1703 | |
| 1704 | static int pidfd_release(struct inode *inode, struct file *file) |
| 1705 | { |
| 1706 | struct pid *pid = file->private_data; |
| 1707 | |
| 1708 | file->private_data = NULL; |
| 1709 | put_pid(pid); |
| 1710 | return 0; |
| 1711 | } |
| 1712 | |
| 1713 | #ifdef CONFIG_PROC_FS |
| 1714 | static void pidfd_show_fdinfo(struct seq_file *m, struct file *f) |
| 1715 | { |
| 1716 | struct pid_namespace *ns = proc_pid_ns(file_inode(m->file)); |
| 1717 | struct pid *pid = f->private_data; |
| 1718 | |
| 1719 | seq_put_decimal_ull(m, "Pid:\t", pid_nr_ns(pid, ns)); |
| 1720 | seq_putc(m, '\n'); |
| 1721 | } |
| 1722 | #endif |
| 1723 | |
| 1724 | /* |
| 1725 | * Poll support for process exit notification. |
| 1726 | */ |
| 1727 | static __poll_t pidfd_poll(struct file *file, struct poll_table_struct *pts) |
| 1728 | { |
| 1729 | struct task_struct *task; |
| 1730 | struct pid *pid = file->private_data; |
| 1731 | __poll_t poll_flags = 0; |
| 1732 | |
| 1733 | poll_wait(file, &pid->wait_pidfd, pts); |
| 1734 | |
| 1735 | rcu_read_lock(); |
| 1736 | task = pid_task(pid, PIDTYPE_PID); |
| 1737 | /* |
| 1738 | * Inform pollers only when the whole thread group exits. |
| 1739 | * If the thread group leader exits before all other threads in the |
| 1740 | * group, then poll(2) should block, similar to the wait(2) family. |
| 1741 | */ |
| 1742 | if (!task || (task->exit_state && thread_group_empty(task))) |
| 1743 | poll_flags = EPOLLIN | EPOLLRDNORM; |
| 1744 | rcu_read_unlock(); |
| 1745 | |
| 1746 | return poll_flags; |
| 1747 | } |
| 1748 | |
| 1749 | const struct file_operations pidfd_fops = { |
| 1750 | .release = pidfd_release, |
| 1751 | .poll = pidfd_poll, |
| 1752 | #ifdef CONFIG_PROC_FS |
| 1753 | .show_fdinfo = pidfd_show_fdinfo, |
| 1754 | #endif |
| 1755 | }; |
| 1756 | |
| 1757 | static void __delayed_free_task(struct rcu_head *rhp) |
| 1758 | { |
| 1759 | struct task_struct *tsk = container_of(rhp, struct task_struct, rcu); |
| 1760 | |
| 1761 | free_task(tsk); |
| 1762 | } |
| 1763 | |
| 1764 | static __always_inline void delayed_free_task(struct task_struct *tsk) |
| 1765 | { |
| 1766 | if (IS_ENABLED(CONFIG_MEMCG)) |
| 1767 | call_rcu(&tsk->rcu, __delayed_free_task); |
| 1768 | else |
| 1769 | free_task(tsk); |
| 1770 | } |
| 1771 | |
| 1772 | static void copy_oom_score_adj(u64 clone_flags, struct task_struct *tsk) |
| 1773 | { |
| 1774 | /* Skip if kernel thread */ |
| 1775 | if (!tsk->mm) |
| 1776 | return; |
| 1777 | |
| 1778 | /* Skip if spawning a thread or using vfork */ |
| 1779 | if ((clone_flags & (CLONE_VM | CLONE_THREAD | CLONE_VFORK)) != CLONE_VM) |
| 1780 | return; |
| 1781 | |
| 1782 | /* We need to synchronize with __set_oom_adj */ |
| 1783 | mutex_lock(&oom_adj_mutex); |
| 1784 | set_bit(MMF_MULTIPROCESS, &tsk->mm->flags); |
| 1785 | /* Update the values in case they were changed after copy_signal */ |
| 1786 | tsk->signal->oom_score_adj = current->signal->oom_score_adj; |
| 1787 | tsk->signal->oom_score_adj_min = current->signal->oom_score_adj_min; |
| 1788 | mutex_unlock(&oom_adj_mutex); |
| 1789 | } |
| 1790 | |
| 1791 | /* |
| 1792 | * This creates a new process as a copy of the old one, |
| 1793 | * but does not actually start it yet. |
| 1794 | * |
| 1795 | * It copies the registers, and all the appropriate |
| 1796 | * parts of the process environment (as per the clone |
| 1797 | * flags). The actual kick-off is left to the caller. |
| 1798 | */ |
| 1799 | static __latent_entropy struct task_struct *copy_process( |
| 1800 | struct pid *pid, |
| 1801 | int trace, |
| 1802 | int node, |
| 1803 | struct kernel_clone_args *args) |
| 1804 | { |
| 1805 | int pidfd = -1, retval; |
| 1806 | struct task_struct *p; |
| 1807 | struct multiprocess_signals delayed; |
| 1808 | struct file *pidfile = NULL; |
| 1809 | u64 clone_flags = args->flags; |
| 1810 | |
| 1811 | /* |
| 1812 | * Don't allow sharing the root directory with processes in a different |
| 1813 | * namespace |
| 1814 | */ |
| 1815 | if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS)) |
| 1816 | return ERR_PTR(-EINVAL); |
| 1817 | |
| 1818 | if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS)) |
| 1819 | return ERR_PTR(-EINVAL); |
| 1820 | |
| 1821 | /* |
| 1822 | * Thread groups must share signals as well, and detached threads |
| 1823 | * can only be started up within the thread group. |
| 1824 | */ |
| 1825 | if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND)) |
| 1826 | return ERR_PTR(-EINVAL); |
| 1827 | |
| 1828 | /* |
| 1829 | * Shared signal handlers imply shared VM. By way of the above, |
| 1830 | * thread groups also imply shared VM. Blocking this case allows |
| 1831 | * for various simplifications in other code. |
| 1832 | */ |
| 1833 | if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM)) |
| 1834 | return ERR_PTR(-EINVAL); |
| 1835 | |
| 1836 | /* |
| 1837 | * Siblings of global init remain as zombies on exit since they are |
| 1838 | * not reaped by their parent (swapper). To solve this and to avoid |
| 1839 | * multi-rooted process trees, prevent global and container-inits |
| 1840 | * from creating siblings. |
| 1841 | */ |
| 1842 | if ((clone_flags & CLONE_PARENT) && |
| 1843 | current->signal->flags & SIGNAL_UNKILLABLE) |
| 1844 | return ERR_PTR(-EINVAL); |
| 1845 | |
| 1846 | /* |
| 1847 | * If the new process will be in a different pid or user namespace |
| 1848 | * do not allow it to share a thread group with the forking task. |
| 1849 | */ |
| 1850 | if (clone_flags & CLONE_THREAD) { |
| 1851 | if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) || |
| 1852 | (task_active_pid_ns(current) != |
| 1853 | current->nsproxy->pid_ns_for_children)) |
| 1854 | return ERR_PTR(-EINVAL); |
| 1855 | } |
| 1856 | |
| 1857 | if (clone_flags & CLONE_PIDFD) { |
| 1858 | /* |
| 1859 | * - CLONE_DETACHED is blocked so that we can potentially |
| 1860 | * reuse it later for CLONE_PIDFD. |
| 1861 | * - CLONE_THREAD is blocked until someone really needs it. |
| 1862 | */ |
| 1863 | if (clone_flags & (CLONE_DETACHED | CLONE_THREAD)) |
| 1864 | return ERR_PTR(-EINVAL); |
| 1865 | } |
| 1866 | |
| 1867 | /* |
| 1868 | * Force any signals received before this point to be delivered |
| 1869 | * before the fork happens. Collect up signals sent to multiple |
| 1870 | * processes that happen during the fork and delay them so that |
| 1871 | * they appear to happen after the fork. |
| 1872 | */ |
| 1873 | sigemptyset(&delayed.signal); |
| 1874 | INIT_HLIST_NODE(&delayed.node); |
| 1875 | |
| 1876 | spin_lock_irq(¤t->sighand->siglock); |
| 1877 | if (!(clone_flags & CLONE_THREAD)) |
| 1878 | hlist_add_head(&delayed.node, ¤t->signal->multiprocess); |
| 1879 | recalc_sigpending(); |
| 1880 | spin_unlock_irq(¤t->sighand->siglock); |
| 1881 | retval = -ERESTARTNOINTR; |
| 1882 | if (signal_pending(current)) |
| 1883 | goto fork_out; |
| 1884 | |
| 1885 | retval = -ENOMEM; |
| 1886 | p = dup_task_struct(current, node); |
| 1887 | if (!p) |
| 1888 | goto fork_out; |
| 1889 | |
| 1890 | cpufreq_task_times_init(p); |
| 1891 | |
| 1892 | /* |
| 1893 | * This _must_ happen before we call free_task(), i.e. before we jump |
| 1894 | * to any of the bad_fork_* labels. This is to avoid freeing |
| 1895 | * p->set_child_tid which is (ab)used as a kthread's data pointer for |
| 1896 | * kernel threads (PF_KTHREAD). |
| 1897 | */ |
| 1898 | p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? args->child_tid : NULL; |
| 1899 | /* |
| 1900 | * Clear TID on mm_release()? |
| 1901 | */ |
| 1902 | p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? args->child_tid : NULL; |
| 1903 | |
| 1904 | ftrace_graph_init_task(p); |
| 1905 | |
| 1906 | rt_mutex_init_task(p); |
| 1907 | |
| 1908 | #ifdef CONFIG_PROVE_LOCKING |
| 1909 | DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled); |
| 1910 | DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled); |
| 1911 | #endif |
| 1912 | retval = -EAGAIN; |
| 1913 | if (atomic_read(&p->real_cred->user->processes) >= |
| 1914 | task_rlimit(p, RLIMIT_NPROC)) { |
| 1915 | if (p->real_cred->user != INIT_USER && |
| 1916 | !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) |
| 1917 | goto bad_fork_free; |
| 1918 | } |
| 1919 | current->flags &= ~PF_NPROC_EXCEEDED; |
| 1920 | |
| 1921 | retval = copy_creds(p, clone_flags); |
| 1922 | if (retval < 0) |
| 1923 | goto bad_fork_free; |
| 1924 | |
| 1925 | /* |
| 1926 | * If multiple threads are within copy_process(), then this check |
| 1927 | * triggers too late. This doesn't hurt, the check is only there |
| 1928 | * to stop root fork bombs. |
| 1929 | */ |
| 1930 | retval = -EAGAIN; |
| 1931 | if (nr_threads >= max_threads) |
| 1932 | goto bad_fork_cleanup_count; |
| 1933 | |
| 1934 | delayacct_tsk_init(p); /* Must remain after dup_task_struct() */ |
| 1935 | p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER | PF_IDLE); |
| 1936 | p->flags |= PF_FORKNOEXEC; |
| 1937 | INIT_LIST_HEAD(&p->children); |
| 1938 | INIT_LIST_HEAD(&p->sibling); |
| 1939 | rcu_copy_process(p); |
| 1940 | p->vfork_done = NULL; |
| 1941 | spin_lock_init(&p->alloc_lock); |
| 1942 | |
| 1943 | init_sigpending(&p->pending); |
| 1944 | |
| 1945 | p->utime = p->stime = p->gtime = 0; |
| 1946 | #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME |
| 1947 | p->utimescaled = p->stimescaled = 0; |
| 1948 | #endif |
| 1949 | prev_cputime_init(&p->prev_cputime); |
| 1950 | |
| 1951 | #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN |
| 1952 | seqcount_init(&p->vtime.seqcount); |
| 1953 | p->vtime.starttime = 0; |
| 1954 | p->vtime.state = VTIME_INACTIVE; |
| 1955 | #endif |
| 1956 | |
| 1957 | #if defined(SPLIT_RSS_COUNTING) |
| 1958 | memset(&p->rss_stat, 0, sizeof(p->rss_stat)); |
| 1959 | #endif |
| 1960 | |
| 1961 | p->default_timer_slack_ns = current->timer_slack_ns; |
| 1962 | |
| 1963 | #ifdef CONFIG_PSI |
| 1964 | p->psi_flags = 0; |
| 1965 | #endif |
| 1966 | |
| 1967 | task_io_accounting_init(&p->ioac); |
| 1968 | acct_clear_integrals(p); |
| 1969 | |
| 1970 | posix_cputimers_init(&p->posix_cputimers); |
| 1971 | |
| 1972 | p->io_context = NULL; |
| 1973 | audit_set_context(p, NULL); |
| 1974 | cgroup_fork(p); |
| 1975 | #ifdef CONFIG_NUMA |
| 1976 | p->mempolicy = mpol_dup(p->mempolicy); |
| 1977 | if (IS_ERR(p->mempolicy)) { |
| 1978 | retval = PTR_ERR(p->mempolicy); |
| 1979 | p->mempolicy = NULL; |
| 1980 | goto bad_fork_cleanup_threadgroup_lock; |
| 1981 | } |
| 1982 | #endif |
| 1983 | #ifdef CONFIG_CPUSETS |
| 1984 | p->cpuset_mem_spread_rotor = NUMA_NO_NODE; |
| 1985 | p->cpuset_slab_spread_rotor = NUMA_NO_NODE; |
| 1986 | seqcount_init(&p->mems_allowed_seq); |
| 1987 | #endif |
| 1988 | #ifdef CONFIG_TRACE_IRQFLAGS |
| 1989 | p->irq_events = 0; |
| 1990 | p->hardirqs_enabled = 0; |
| 1991 | p->hardirq_enable_ip = 0; |
| 1992 | p->hardirq_enable_event = 0; |
| 1993 | p->hardirq_disable_ip = _THIS_IP_; |
| 1994 | p->hardirq_disable_event = 0; |
| 1995 | p->softirqs_enabled = 1; |
| 1996 | p->softirq_enable_ip = _THIS_IP_; |
| 1997 | p->softirq_enable_event = 0; |
| 1998 | p->softirq_disable_ip = 0; |
| 1999 | p->softirq_disable_event = 0; |
| 2000 | p->hardirq_context = 0; |
| 2001 | p->softirq_context = 0; |
| 2002 | #endif |
| 2003 | |
| 2004 | p->pagefault_disabled = 0; |
| 2005 | |
| 2006 | #ifdef CONFIG_LOCKDEP |
| 2007 | lockdep_init_task(p); |
| 2008 | #endif |
| 2009 | |
| 2010 | #ifdef CONFIG_DEBUG_MUTEXES |
| 2011 | p->blocked_on = NULL; /* not blocked yet */ |
| 2012 | #endif |
| 2013 | #ifdef CONFIG_BCACHE |
| 2014 | p->sequential_io = 0; |
| 2015 | p->sequential_io_avg = 0; |
| 2016 | #endif |
| 2017 | |
| 2018 | /* Perform scheduler related setup. Assign this task to a CPU. */ |
| 2019 | retval = sched_fork(clone_flags, p); |
| 2020 | if (retval) |
| 2021 | goto bad_fork_cleanup_policy; |
| 2022 | |
| 2023 | retval = perf_event_init_task(p); |
| 2024 | if (retval) |
| 2025 | goto bad_fork_cleanup_policy; |
| 2026 | retval = audit_alloc(p); |
| 2027 | if (retval) |
| 2028 | goto bad_fork_cleanup_perf; |
| 2029 | /* copy all the process information */ |
| 2030 | shm_init_task(p); |
| 2031 | retval = security_task_alloc(p, clone_flags); |
| 2032 | if (retval) |
| 2033 | goto bad_fork_cleanup_audit; |
| 2034 | retval = copy_semundo(clone_flags, p); |
| 2035 | if (retval) |
| 2036 | goto bad_fork_cleanup_security; |
| 2037 | retval = copy_files(clone_flags, p); |
| 2038 | if (retval) |
| 2039 | goto bad_fork_cleanup_semundo; |
| 2040 | retval = copy_fs(clone_flags, p); |
| 2041 | if (retval) |
| 2042 | goto bad_fork_cleanup_files; |
| 2043 | retval = copy_sighand(clone_flags, p); |
| 2044 | if (retval) |
| 2045 | goto bad_fork_cleanup_fs; |
| 2046 | retval = copy_signal(clone_flags, p); |
| 2047 | if (retval) |
| 2048 | goto bad_fork_cleanup_sighand; |
| 2049 | retval = copy_mm(clone_flags, p); |
| 2050 | if (retval) |
| 2051 | goto bad_fork_cleanup_signal; |
| 2052 | retval = copy_namespaces(clone_flags, p); |
| 2053 | if (retval) |
| 2054 | goto bad_fork_cleanup_mm; |
| 2055 | retval = copy_io(clone_flags, p); |
| 2056 | if (retval) |
| 2057 | goto bad_fork_cleanup_namespaces; |
| 2058 | retval = copy_thread_tls(clone_flags, args->stack, args->stack_size, p, |
| 2059 | args->tls); |
| 2060 | if (retval) |
| 2061 | goto bad_fork_cleanup_io; |
| 2062 | |
| 2063 | stackleak_task_init(p); |
| 2064 | |
| 2065 | if (pid != &init_struct_pid) { |
| 2066 | pid = alloc_pid(p->nsproxy->pid_ns_for_children); |
| 2067 | if (IS_ERR(pid)) { |
| 2068 | retval = PTR_ERR(pid); |
| 2069 | goto bad_fork_cleanup_thread; |
| 2070 | } |
| 2071 | } |
| 2072 | |
| 2073 | /* |
| 2074 | * This has to happen after we've potentially unshared the file |
| 2075 | * descriptor table (so that the pidfd doesn't leak into the child |
| 2076 | * if the fd table isn't shared). |
| 2077 | */ |
| 2078 | if (clone_flags & CLONE_PIDFD) { |
| 2079 | retval = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 2080 | if (retval < 0) |
| 2081 | goto bad_fork_free_pid; |
| 2082 | |
| 2083 | pidfd = retval; |
| 2084 | |
| 2085 | pidfile = anon_inode_getfile("[pidfd]", &pidfd_fops, pid, |
| 2086 | O_RDWR | O_CLOEXEC); |
| 2087 | if (IS_ERR(pidfile)) { |
| 2088 | put_unused_fd(pidfd); |
| 2089 | retval = PTR_ERR(pidfile); |
| 2090 | goto bad_fork_free_pid; |
| 2091 | } |
| 2092 | get_pid(pid); /* held by pidfile now */ |
| 2093 | |
| 2094 | retval = put_user(pidfd, args->pidfd); |
| 2095 | if (retval) |
| 2096 | goto bad_fork_put_pidfd; |
| 2097 | } |
| 2098 | |
| 2099 | #ifdef CONFIG_BLOCK |
| 2100 | p->plug = NULL; |
| 2101 | #endif |
| 2102 | futex_init_task(p); |
| 2103 | |
| 2104 | /* |
| 2105 | * sigaltstack should be cleared when sharing the same VM |
| 2106 | */ |
| 2107 | if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM) |
| 2108 | sas_ss_reset(p); |
| 2109 | |
| 2110 | /* |
| 2111 | * Syscall tracing and stepping should be turned off in the |
| 2112 | * child regardless of CLONE_PTRACE. |
| 2113 | */ |
| 2114 | user_disable_single_step(p); |
| 2115 | clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE); |
| 2116 | #ifdef TIF_SYSCALL_EMU |
| 2117 | clear_tsk_thread_flag(p, TIF_SYSCALL_EMU); |
| 2118 | #endif |
| 2119 | clear_tsk_latency_tracing(p); |
| 2120 | |
| 2121 | /* ok, now we should be set up.. */ |
| 2122 | p->pid = pid_nr(pid); |
| 2123 | if (clone_flags & CLONE_THREAD) { |
| 2124 | p->group_leader = current->group_leader; |
| 2125 | p->tgid = current->tgid; |
| 2126 | } else { |
| 2127 | p->group_leader = p; |
| 2128 | p->tgid = p->pid; |
| 2129 | } |
| 2130 | |
| 2131 | p->nr_dirtied = 0; |
| 2132 | p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10); |
| 2133 | p->dirty_paused_when = 0; |
| 2134 | |
| 2135 | p->pdeath_signal = 0; |
| 2136 | INIT_LIST_HEAD(&p->thread_group); |
| 2137 | p->task_works = NULL; |
| 2138 | |
| 2139 | cgroup_threadgroup_change_begin(current); |
| 2140 | /* |
| 2141 | * Ensure that the cgroup subsystem policies allow the new process to be |
| 2142 | * forked. It should be noted the the new process's css_set can be changed |
| 2143 | * between here and cgroup_post_fork() if an organisation operation is in |
| 2144 | * progress. |
| 2145 | */ |
| 2146 | retval = cgroup_can_fork(p); |
| 2147 | if (retval) |
| 2148 | goto bad_fork_cgroup_threadgroup_change_end; |
| 2149 | |
| 2150 | /* |
| 2151 | * From this point on we must avoid any synchronous user-space |
| 2152 | * communication until we take the tasklist-lock. In particular, we do |
| 2153 | * not want user-space to be able to predict the process start-time by |
| 2154 | * stalling fork(2) after we recorded the start_time but before it is |
| 2155 | * visible to the system. |
| 2156 | */ |
| 2157 | |
| 2158 | p->start_time = ktime_get_ns(); |
| 2159 | p->real_start_time = ktime_get_boottime_ns(); |
| 2160 | |
| 2161 | /* |
| 2162 | * Make it visible to the rest of the system, but dont wake it up yet. |
| 2163 | * Need tasklist lock for parent etc handling! |
| 2164 | */ |
| 2165 | write_lock_irq(&tasklist_lock); |
| 2166 | |
| 2167 | /* CLONE_PARENT re-uses the old parent */ |
| 2168 | if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) { |
| 2169 | p->real_parent = current->real_parent; |
| 2170 | p->parent_exec_id = current->parent_exec_id; |
| 2171 | if (clone_flags & CLONE_THREAD) |
| 2172 | p->exit_signal = -1; |
| 2173 | else |
| 2174 | p->exit_signal = current->group_leader->exit_signal; |
| 2175 | } else { |
| 2176 | p->real_parent = current; |
| 2177 | p->parent_exec_id = current->self_exec_id; |
| 2178 | p->exit_signal = args->exit_signal; |
| 2179 | } |
| 2180 | |
| 2181 | klp_copy_process(p); |
| 2182 | |
| 2183 | spin_lock(¤t->sighand->siglock); |
| 2184 | |
| 2185 | /* |
| 2186 | * Copy seccomp details explicitly here, in case they were changed |
| 2187 | * before holding sighand lock. |
| 2188 | */ |
| 2189 | copy_seccomp(p); |
| 2190 | |
| 2191 | rseq_fork(p, clone_flags); |
| 2192 | |
| 2193 | /* Don't start children in a dying pid namespace */ |
| 2194 | if (unlikely(!(ns_of_pid(pid)->pid_allocated & PIDNS_ADDING))) { |
| 2195 | retval = -ENOMEM; |
| 2196 | goto bad_fork_cancel_cgroup; |
| 2197 | } |
| 2198 | |
| 2199 | /* Let kill terminate clone/fork in the middle */ |
| 2200 | if (fatal_signal_pending(current)) { |
| 2201 | retval = -EINTR; |
| 2202 | goto bad_fork_cancel_cgroup; |
| 2203 | } |
| 2204 | |
| 2205 | init_task_pid_links(p); |
| 2206 | if (likely(p->pid)) { |
| 2207 | ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace); |
| 2208 | |
| 2209 | init_task_pid(p, PIDTYPE_PID, pid); |
| 2210 | if (thread_group_leader(p)) { |
| 2211 | init_task_pid(p, PIDTYPE_TGID, pid); |
| 2212 | init_task_pid(p, PIDTYPE_PGID, task_pgrp(current)); |
| 2213 | init_task_pid(p, PIDTYPE_SID, task_session(current)); |
| 2214 | |
| 2215 | if (is_child_reaper(pid)) { |
| 2216 | ns_of_pid(pid)->child_reaper = p; |
| 2217 | p->signal->flags |= SIGNAL_UNKILLABLE; |
| 2218 | } |
| 2219 | p->signal->shared_pending.signal = delayed.signal; |
| 2220 | p->signal->tty = tty_kref_get(current->signal->tty); |
| 2221 | /* |
| 2222 | * Inherit has_child_subreaper flag under the same |
| 2223 | * tasklist_lock with adding child to the process tree |
| 2224 | * for propagate_has_child_subreaper optimization. |
| 2225 | */ |
| 2226 | p->signal->has_child_subreaper = p->real_parent->signal->has_child_subreaper || |
| 2227 | p->real_parent->signal->is_child_subreaper; |
| 2228 | list_add_tail(&p->sibling, &p->real_parent->children); |
| 2229 | list_add_tail_rcu(&p->tasks, &init_task.tasks); |
| 2230 | attach_pid(p, PIDTYPE_TGID); |
| 2231 | attach_pid(p, PIDTYPE_PGID); |
| 2232 | attach_pid(p, PIDTYPE_SID); |
| 2233 | __this_cpu_inc(process_counts); |
| 2234 | } else { |
| 2235 | current->signal->nr_threads++; |
| 2236 | atomic_inc(¤t->signal->live); |
| 2237 | refcount_inc(¤t->signal->sigcnt); |
| 2238 | task_join_group_stop(p); |
| 2239 | list_add_tail_rcu(&p->thread_group, |
| 2240 | &p->group_leader->thread_group); |
| 2241 | list_add_tail_rcu(&p->thread_node, |
| 2242 | &p->signal->thread_head); |
| 2243 | } |
| 2244 | attach_pid(p, PIDTYPE_PID); |
| 2245 | nr_threads++; |
| 2246 | } |
| 2247 | total_forks++; |
| 2248 | hlist_del_init(&delayed.node); |
| 2249 | spin_unlock(¤t->sighand->siglock); |
| 2250 | syscall_tracepoint_update(p); |
| 2251 | write_unlock_irq(&tasklist_lock); |
| 2252 | |
| 2253 | if (pidfile) |
| 2254 | fd_install(pidfd, pidfile); |
| 2255 | |
| 2256 | proc_fork_connector(p); |
| 2257 | sched_post_fork(p); |
| 2258 | cgroup_post_fork(p); |
| 2259 | cgroup_threadgroup_change_end(current); |
| 2260 | perf_event_fork(p); |
| 2261 | |
| 2262 | trace_task_newtask(p, clone_flags); |
| 2263 | uprobe_copy_process(p, clone_flags); |
| 2264 | |
| 2265 | copy_oom_score_adj(clone_flags, p); |
| 2266 | |
| 2267 | return p; |
| 2268 | |
| 2269 | bad_fork_cancel_cgroup: |
| 2270 | spin_unlock(¤t->sighand->siglock); |
| 2271 | write_unlock_irq(&tasklist_lock); |
| 2272 | cgroup_cancel_fork(p); |
| 2273 | bad_fork_cgroup_threadgroup_change_end: |
| 2274 | cgroup_threadgroup_change_end(current); |
| 2275 | bad_fork_put_pidfd: |
| 2276 | if (clone_flags & CLONE_PIDFD) { |
| 2277 | fput(pidfile); |
| 2278 | put_unused_fd(pidfd); |
| 2279 | } |
| 2280 | bad_fork_free_pid: |
| 2281 | if (pid != &init_struct_pid) |
| 2282 | free_pid(pid); |
| 2283 | bad_fork_cleanup_thread: |
| 2284 | exit_thread(p); |
| 2285 | bad_fork_cleanup_io: |
| 2286 | if (p->io_context) |
| 2287 | exit_io_context(p); |
| 2288 | bad_fork_cleanup_namespaces: |
| 2289 | exit_task_namespaces(p); |
| 2290 | bad_fork_cleanup_mm: |
| 2291 | if (p->mm) { |
| 2292 | mm_clear_owner(p->mm, p); |
| 2293 | mmput(p->mm); |
| 2294 | } |
| 2295 | bad_fork_cleanup_signal: |
| 2296 | if (!(clone_flags & CLONE_THREAD)) |
| 2297 | free_signal_struct(p->signal); |
| 2298 | bad_fork_cleanup_sighand: |
| 2299 | __cleanup_sighand(p->sighand); |
| 2300 | bad_fork_cleanup_fs: |
| 2301 | exit_fs(p); /* blocking */ |
| 2302 | bad_fork_cleanup_files: |
| 2303 | exit_files(p); /* blocking */ |
| 2304 | bad_fork_cleanup_semundo: |
| 2305 | exit_sem(p); |
| 2306 | bad_fork_cleanup_security: |
| 2307 | security_task_free(p); |
| 2308 | bad_fork_cleanup_audit: |
| 2309 | audit_free(p); |
| 2310 | bad_fork_cleanup_perf: |
| 2311 | perf_event_free_task(p); |
| 2312 | bad_fork_cleanup_policy: |
| 2313 | lockdep_free_task(p); |
| 2314 | #ifdef CONFIG_NUMA |
| 2315 | mpol_put(p->mempolicy); |
| 2316 | bad_fork_cleanup_threadgroup_lock: |
| 2317 | #endif |
| 2318 | delayacct_tsk_free(p); |
| 2319 | bad_fork_cleanup_count: |
| 2320 | atomic_dec(&p->cred->user->processes); |
| 2321 | exit_creds(p); |
| 2322 | bad_fork_free: |
| 2323 | p->state = TASK_DEAD; |
| 2324 | put_task_stack(p); |
| 2325 | delayed_free_task(p); |
| 2326 | fork_out: |
| 2327 | spin_lock_irq(¤t->sighand->siglock); |
| 2328 | hlist_del_init(&delayed.node); |
| 2329 | spin_unlock_irq(¤t->sighand->siglock); |
| 2330 | return ERR_PTR(retval); |
| 2331 | } |
| 2332 | |
| 2333 | static inline void init_idle_pids(struct task_struct *idle) |
| 2334 | { |
| 2335 | enum pid_type type; |
| 2336 | |
| 2337 | for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { |
| 2338 | INIT_HLIST_NODE(&idle->pid_links[type]); /* not really needed */ |
| 2339 | init_task_pid(idle, type, &init_struct_pid); |
| 2340 | } |
| 2341 | } |
| 2342 | |
| 2343 | struct task_struct *fork_idle(int cpu) |
| 2344 | { |
| 2345 | struct task_struct *task; |
| 2346 | struct kernel_clone_args args = { |
| 2347 | .flags = CLONE_VM, |
| 2348 | }; |
| 2349 | |
| 2350 | task = copy_process(&init_struct_pid, 0, cpu_to_node(cpu), &args); |
| 2351 | if (!IS_ERR(task)) { |
| 2352 | init_idle_pids(task); |
| 2353 | init_idle(task, cpu); |
| 2354 | } |
| 2355 | |
| 2356 | return task; |
| 2357 | } |
| 2358 | |
| 2359 | /* |
| 2360 | * Ok, this is the main fork-routine. |
| 2361 | * |
| 2362 | * It copies the process, and if successful kick-starts |
| 2363 | * it and waits for it to finish using the VM if required. |
| 2364 | * |
| 2365 | * args->exit_signal is expected to be checked for sanity by the caller. |
| 2366 | */ |
| 2367 | long _do_fork(struct kernel_clone_args *args) |
| 2368 | { |
| 2369 | u64 clone_flags = args->flags; |
| 2370 | struct completion vfork; |
| 2371 | struct pid *pid; |
| 2372 | struct task_struct *p; |
| 2373 | int trace = 0; |
| 2374 | long nr; |
| 2375 | |
| 2376 | /* |
| 2377 | * Determine whether and which event to report to ptracer. When |
| 2378 | * called from kernel_thread or CLONE_UNTRACED is explicitly |
| 2379 | * requested, no event is reported; otherwise, report if the event |
| 2380 | * for the type of forking is enabled. |
| 2381 | */ |
| 2382 | if (!(clone_flags & CLONE_UNTRACED)) { |
| 2383 | if (clone_flags & CLONE_VFORK) |
| 2384 | trace = PTRACE_EVENT_VFORK; |
| 2385 | else if (args->exit_signal != SIGCHLD) |
| 2386 | trace = PTRACE_EVENT_CLONE; |
| 2387 | else |
| 2388 | trace = PTRACE_EVENT_FORK; |
| 2389 | |
| 2390 | if (likely(!ptrace_event_enabled(current, trace))) |
| 2391 | trace = 0; |
| 2392 | } |
| 2393 | |
| 2394 | p = copy_process(NULL, trace, NUMA_NO_NODE, args); |
| 2395 | add_latent_entropy(); |
| 2396 | |
| 2397 | if (IS_ERR(p)) |
| 2398 | return PTR_ERR(p); |
| 2399 | |
| 2400 | cpufreq_task_times_alloc(p); |
| 2401 | |
| 2402 | /* |
| 2403 | * Do this prior waking up the new thread - the thread pointer |
| 2404 | * might get invalid after that point, if the thread exits quickly. |
| 2405 | */ |
| 2406 | trace_sched_process_fork(current, p); |
| 2407 | |
| 2408 | pid = get_task_pid(p, PIDTYPE_PID); |
| 2409 | nr = pid_vnr(pid); |
| 2410 | |
| 2411 | if (clone_flags & CLONE_PARENT_SETTID) |
| 2412 | put_user(nr, args->parent_tid); |
| 2413 | |
| 2414 | if (clone_flags & CLONE_VFORK) { |
| 2415 | p->vfork_done = &vfork; |
| 2416 | init_completion(&vfork); |
| 2417 | get_task_struct(p); |
| 2418 | } |
| 2419 | |
| 2420 | wake_up_new_task(p); |
| 2421 | |
| 2422 | /* forking complete and child started to run, tell ptracer */ |
| 2423 | if (unlikely(trace)) |
| 2424 | ptrace_event_pid(trace, pid); |
| 2425 | |
| 2426 | if (clone_flags & CLONE_VFORK) { |
| 2427 | if (!wait_for_vfork_done(p, &vfork)) |
| 2428 | ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid); |
| 2429 | } |
| 2430 | |
| 2431 | put_pid(pid); |
| 2432 | return nr; |
| 2433 | } |
| 2434 | |
| 2435 | bool legacy_clone_args_valid(const struct kernel_clone_args *kargs) |
| 2436 | { |
| 2437 | /* clone(CLONE_PIDFD) uses parent_tidptr to return a pidfd */ |
| 2438 | if ((kargs->flags & CLONE_PIDFD) && |
| 2439 | (kargs->flags & CLONE_PARENT_SETTID)) |
| 2440 | return false; |
| 2441 | |
| 2442 | return true; |
| 2443 | } |
| 2444 | |
| 2445 | #ifndef CONFIG_HAVE_COPY_THREAD_TLS |
| 2446 | /* For compatibility with architectures that call do_fork directly rather than |
| 2447 | * using the syscall entry points below. */ |
| 2448 | long do_fork(unsigned long clone_flags, |
| 2449 | unsigned long stack_start, |
| 2450 | unsigned long stack_size, |
| 2451 | int __user *parent_tidptr, |
| 2452 | int __user *child_tidptr) |
| 2453 | { |
| 2454 | struct kernel_clone_args args = { |
| 2455 | .flags = (lower_32_bits(clone_flags) & ~CSIGNAL), |
| 2456 | .pidfd = parent_tidptr, |
| 2457 | .child_tid = child_tidptr, |
| 2458 | .parent_tid = parent_tidptr, |
| 2459 | .exit_signal = (lower_32_bits(clone_flags) & CSIGNAL), |
| 2460 | .stack = stack_start, |
| 2461 | .stack_size = stack_size, |
| 2462 | }; |
| 2463 | |
| 2464 | if (!legacy_clone_args_valid(&args)) |
| 2465 | return -EINVAL; |
| 2466 | |
| 2467 | return _do_fork(&args); |
| 2468 | } |
| 2469 | #endif |
| 2470 | |
| 2471 | /* |
| 2472 | * Create a kernel thread. |
| 2473 | */ |
| 2474 | pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) |
| 2475 | { |
| 2476 | struct kernel_clone_args args = { |
| 2477 | .flags = ((lower_32_bits(flags) | CLONE_VM | |
| 2478 | CLONE_UNTRACED) & ~CSIGNAL), |
| 2479 | .exit_signal = (lower_32_bits(flags) & CSIGNAL), |
| 2480 | .stack = (unsigned long)fn, |
| 2481 | .stack_size = (unsigned long)arg, |
| 2482 | }; |
| 2483 | |
| 2484 | return _do_fork(&args); |
| 2485 | } |
| 2486 | |
| 2487 | #ifdef __ARCH_WANT_SYS_FORK |
| 2488 | SYSCALL_DEFINE0(fork) |
| 2489 | { |
| 2490 | #ifdef CONFIG_MMU |
| 2491 | struct kernel_clone_args args = { |
| 2492 | .exit_signal = SIGCHLD, |
| 2493 | }; |
| 2494 | |
| 2495 | return _do_fork(&args); |
| 2496 | #else |
| 2497 | /* can not support in nommu mode */ |
| 2498 | return -EINVAL; |
| 2499 | #endif |
| 2500 | } |
| 2501 | #endif |
| 2502 | |
| 2503 | #ifdef __ARCH_WANT_SYS_VFORK |
| 2504 | SYSCALL_DEFINE0(vfork) |
| 2505 | { |
| 2506 | struct kernel_clone_args args = { |
| 2507 | .flags = CLONE_VFORK | CLONE_VM, |
| 2508 | .exit_signal = SIGCHLD, |
| 2509 | }; |
| 2510 | |
| 2511 | return _do_fork(&args); |
| 2512 | } |
| 2513 | #endif |
| 2514 | |
| 2515 | #ifdef __ARCH_WANT_SYS_CLONE |
| 2516 | #ifdef CONFIG_CLONE_BACKWARDS |
| 2517 | SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, |
| 2518 | int __user *, parent_tidptr, |
| 2519 | unsigned long, tls, |
| 2520 | int __user *, child_tidptr) |
| 2521 | #elif defined(CONFIG_CLONE_BACKWARDS2) |
| 2522 | SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags, |
| 2523 | int __user *, parent_tidptr, |
| 2524 | int __user *, child_tidptr, |
| 2525 | unsigned long, tls) |
| 2526 | #elif defined(CONFIG_CLONE_BACKWARDS3) |
| 2527 | SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp, |
| 2528 | int, stack_size, |
| 2529 | int __user *, parent_tidptr, |
| 2530 | int __user *, child_tidptr, |
| 2531 | unsigned long, tls) |
| 2532 | #else |
| 2533 | SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, |
| 2534 | int __user *, parent_tidptr, |
| 2535 | int __user *, child_tidptr, |
| 2536 | unsigned long, tls) |
| 2537 | #endif |
| 2538 | { |
| 2539 | struct kernel_clone_args args = { |
| 2540 | .flags = (lower_32_bits(clone_flags) & ~CSIGNAL), |
| 2541 | .pidfd = parent_tidptr, |
| 2542 | .child_tid = child_tidptr, |
| 2543 | .parent_tid = parent_tidptr, |
| 2544 | .exit_signal = (lower_32_bits(clone_flags) & CSIGNAL), |
| 2545 | .stack = newsp, |
| 2546 | .tls = tls, |
| 2547 | }; |
| 2548 | |
| 2549 | if (!legacy_clone_args_valid(&args)) |
| 2550 | return -EINVAL; |
| 2551 | |
| 2552 | return _do_fork(&args); |
| 2553 | } |
| 2554 | #endif |
| 2555 | |
| 2556 | #ifdef __ARCH_WANT_SYS_CLONE3 |
| 2557 | |
| 2558 | /* |
| 2559 | * copy_thread implementations handle CLONE_SETTLS by reading the TLS value from |
| 2560 | * the registers containing the syscall arguments for clone. This doesn't work |
| 2561 | * with clone3 since the TLS value is passed in clone_args instead. |
| 2562 | */ |
| 2563 | #ifndef CONFIG_HAVE_COPY_THREAD_TLS |
| 2564 | #error clone3 requires copy_thread_tls support in arch |
| 2565 | #endif |
| 2566 | |
| 2567 | noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs, |
| 2568 | struct clone_args __user *uargs, |
| 2569 | size_t usize) |
| 2570 | { |
| 2571 | int err; |
| 2572 | struct clone_args args; |
| 2573 | |
| 2574 | if (unlikely(usize > PAGE_SIZE)) |
| 2575 | return -E2BIG; |
| 2576 | if (unlikely(usize < CLONE_ARGS_SIZE_VER0)) |
| 2577 | return -EINVAL; |
| 2578 | |
| 2579 | err = copy_struct_from_user(&args, sizeof(args), uargs, usize); |
| 2580 | if (err) |
| 2581 | return err; |
| 2582 | |
| 2583 | /* |
| 2584 | * Verify that higher 32bits of exit_signal are unset and that |
| 2585 | * it is a valid signal |
| 2586 | */ |
| 2587 | if (unlikely((args.exit_signal & ~((u64)CSIGNAL)) || |
| 2588 | !valid_signal(args.exit_signal))) |
| 2589 | return -EINVAL; |
| 2590 | |
| 2591 | *kargs = (struct kernel_clone_args){ |
| 2592 | .flags = args.flags, |
| 2593 | .pidfd = u64_to_user_ptr(args.pidfd), |
| 2594 | .child_tid = u64_to_user_ptr(args.child_tid), |
| 2595 | .parent_tid = u64_to_user_ptr(args.parent_tid), |
| 2596 | .exit_signal = args.exit_signal, |
| 2597 | .stack = args.stack, |
| 2598 | .stack_size = args.stack_size, |
| 2599 | .tls = args.tls, |
| 2600 | }; |
| 2601 | |
| 2602 | return 0; |
| 2603 | } |
| 2604 | |
| 2605 | /** |
| 2606 | * clone3_stack_valid - check and prepare stack |
| 2607 | * @kargs: kernel clone args |
| 2608 | * |
| 2609 | * Verify that the stack arguments userspace gave us are sane. |
| 2610 | * In addition, set the stack direction for userspace since it's easy for us to |
| 2611 | * determine. |
| 2612 | */ |
| 2613 | static inline bool clone3_stack_valid(struct kernel_clone_args *kargs) |
| 2614 | { |
| 2615 | if (kargs->stack == 0) { |
| 2616 | if (kargs->stack_size > 0) |
| 2617 | return false; |
| 2618 | } else { |
| 2619 | if (kargs->stack_size == 0) |
| 2620 | return false; |
| 2621 | |
| 2622 | if (!access_ok((void __user *)kargs->stack, kargs->stack_size)) |
| 2623 | return false; |
| 2624 | |
| 2625 | #if !defined(CONFIG_STACK_GROWSUP) && !defined(CONFIG_IA64) |
| 2626 | kargs->stack += kargs->stack_size; |
| 2627 | #endif |
| 2628 | } |
| 2629 | |
| 2630 | return true; |
| 2631 | } |
| 2632 | |
| 2633 | static bool clone3_args_valid(struct kernel_clone_args *kargs) |
| 2634 | { |
| 2635 | /* |
| 2636 | * All lower bits of the flag word are taken. |
| 2637 | * Verify that no other unknown flags are passed along. |
| 2638 | */ |
| 2639 | if (kargs->flags & ~CLONE_LEGACY_FLAGS) |
| 2640 | return false; |
| 2641 | |
| 2642 | /* |
| 2643 | * - make the CLONE_DETACHED bit reuseable for clone3 |
| 2644 | * - make the CSIGNAL bits reuseable for clone3 |
| 2645 | */ |
| 2646 | if (kargs->flags & (CLONE_DETACHED | CSIGNAL)) |
| 2647 | return false; |
| 2648 | |
| 2649 | if ((kargs->flags & (CLONE_THREAD | CLONE_PARENT)) && |
| 2650 | kargs->exit_signal) |
| 2651 | return false; |
| 2652 | |
| 2653 | if (!clone3_stack_valid(kargs)) |
| 2654 | return false; |
| 2655 | |
| 2656 | return true; |
| 2657 | } |
| 2658 | |
| 2659 | /** |
| 2660 | * clone3 - create a new process with specific properties |
| 2661 | * @uargs: argument structure |
| 2662 | * @size: size of @uargs |
| 2663 | * |
| 2664 | * clone3() is the extensible successor to clone()/clone2(). |
| 2665 | * It takes a struct as argument that is versioned by its size. |
| 2666 | * |
| 2667 | * Return: On success, a positive PID for the child process. |
| 2668 | * On error, a negative errno number. |
| 2669 | */ |
| 2670 | SYSCALL_DEFINE2(clone3, struct clone_args __user *, uargs, size_t, size) |
| 2671 | { |
| 2672 | int err; |
| 2673 | |
| 2674 | struct kernel_clone_args kargs; |
| 2675 | |
| 2676 | err = copy_clone_args_from_user(&kargs, uargs, size); |
| 2677 | if (err) |
| 2678 | return err; |
| 2679 | |
| 2680 | if (!clone3_args_valid(&kargs)) |
| 2681 | return -EINVAL; |
| 2682 | |
| 2683 | return _do_fork(&kargs); |
| 2684 | } |
| 2685 | #endif |
| 2686 | |
| 2687 | void walk_process_tree(struct task_struct *top, proc_visitor visitor, void *data) |
| 2688 | { |
| 2689 | struct task_struct *leader, *parent, *child; |
| 2690 | int res; |
| 2691 | |
| 2692 | read_lock(&tasklist_lock); |
| 2693 | leader = top = top->group_leader; |
| 2694 | down: |
| 2695 | for_each_thread(leader, parent) { |
| 2696 | list_for_each_entry(child, &parent->children, sibling) { |
| 2697 | res = visitor(child, data); |
| 2698 | if (res) { |
| 2699 | if (res < 0) |
| 2700 | goto out; |
| 2701 | leader = child; |
| 2702 | goto down; |
| 2703 | } |
| 2704 | up: |
| 2705 | ; |
| 2706 | } |
| 2707 | } |
| 2708 | |
| 2709 | if (leader != top) { |
| 2710 | child = leader; |
| 2711 | parent = child->real_parent; |
| 2712 | leader = parent->group_leader; |
| 2713 | goto up; |
| 2714 | } |
| 2715 | out: |
| 2716 | read_unlock(&tasklist_lock); |
| 2717 | } |
| 2718 | |
| 2719 | #ifndef ARCH_MIN_MMSTRUCT_ALIGN |
| 2720 | #define ARCH_MIN_MMSTRUCT_ALIGN 0 |
| 2721 | #endif |
| 2722 | |
| 2723 | static void sighand_ctor(void *data) |
| 2724 | { |
| 2725 | struct sighand_struct *sighand = data; |
| 2726 | |
| 2727 | spin_lock_init(&sighand->siglock); |
| 2728 | init_waitqueue_head(&sighand->signalfd_wqh); |
| 2729 | } |
| 2730 | |
| 2731 | void __init mm_cache_init(void) |
| 2732 | { |
| 2733 | unsigned int mm_size; |
| 2734 | |
| 2735 | /* |
| 2736 | * The mm_cpumask is located at the end of mm_struct, and is |
| 2737 | * dynamically sized based on the maximum CPU number this system |
| 2738 | * can have, taking hotplug into account (nr_cpu_ids). |
| 2739 | */ |
| 2740 | mm_size = sizeof(struct mm_struct) + cpumask_size(); |
| 2741 | |
| 2742 | mm_cachep = kmem_cache_create_usercopy("mm_struct", |
| 2743 | mm_size, ARCH_MIN_MMSTRUCT_ALIGN, |
| 2744 | SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, |
| 2745 | offsetof(struct mm_struct, saved_auxv), |
| 2746 | sizeof_field(struct mm_struct, saved_auxv), |
| 2747 | NULL); |
| 2748 | } |
| 2749 | |
| 2750 | void __init proc_caches_init(void) |
| 2751 | { |
| 2752 | sighand_cachep = kmem_cache_create("sighand_cache", |
| 2753 | sizeof(struct sighand_struct), 0, |
| 2754 | SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_TYPESAFE_BY_RCU| |
| 2755 | SLAB_ACCOUNT, sighand_ctor); |
| 2756 | signal_cachep = kmem_cache_create("signal_cache", |
| 2757 | sizeof(struct signal_struct), 0, |
| 2758 | SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, |
| 2759 | NULL); |
| 2760 | files_cachep = kmem_cache_create("files_cache", |
| 2761 | sizeof(struct files_struct), 0, |
| 2762 | SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, |
| 2763 | NULL); |
| 2764 | fs_cachep = kmem_cache_create("fs_cache", |
| 2765 | sizeof(struct fs_struct), 0, |
| 2766 | SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, |
| 2767 | NULL); |
| 2768 | |
| 2769 | vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC|SLAB_ACCOUNT); |
| 2770 | mmap_init(); |
| 2771 | nsproxy_cache_init(); |
| 2772 | } |
| 2773 | |
| 2774 | /* |
| 2775 | * Check constraints on flags passed to the unshare system call. |
| 2776 | */ |
| 2777 | static int check_unshare_flags(unsigned long unshare_flags) |
| 2778 | { |
| 2779 | if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND| |
| 2780 | CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| |
| 2781 | CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET| |
| 2782 | CLONE_NEWUSER|CLONE_NEWPID|CLONE_NEWCGROUP)) |
| 2783 | return -EINVAL; |
| 2784 | /* |
| 2785 | * Not implemented, but pretend it works if there is nothing |
| 2786 | * to unshare. Note that unsharing the address space or the |
| 2787 | * signal handlers also need to unshare the signal queues (aka |
| 2788 | * CLONE_THREAD). |
| 2789 | */ |
| 2790 | if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) { |
| 2791 | if (!thread_group_empty(current)) |
| 2792 | return -EINVAL; |
| 2793 | } |
| 2794 | if (unshare_flags & (CLONE_SIGHAND | CLONE_VM)) { |
| 2795 | if (refcount_read(¤t->sighand->count) > 1) |
| 2796 | return -EINVAL; |
| 2797 | } |
| 2798 | if (unshare_flags & CLONE_VM) { |
| 2799 | if (!current_is_single_threaded()) |
| 2800 | return -EINVAL; |
| 2801 | } |
| 2802 | |
| 2803 | return 0; |
| 2804 | } |
| 2805 | |
| 2806 | /* |
| 2807 | * Unshare the filesystem structure if it is being shared |
| 2808 | */ |
| 2809 | static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) |
| 2810 | { |
| 2811 | struct fs_struct *fs = current->fs; |
| 2812 | |
| 2813 | if (!(unshare_flags & CLONE_FS) || !fs) |
| 2814 | return 0; |
| 2815 | |
| 2816 | /* don't need lock here; in the worst case we'll do useless copy */ |
| 2817 | if (fs->users == 1) |
| 2818 | return 0; |
| 2819 | |
| 2820 | *new_fsp = copy_fs_struct(fs); |
| 2821 | if (!*new_fsp) |
| 2822 | return -ENOMEM; |
| 2823 | |
| 2824 | return 0; |
| 2825 | } |
| 2826 | |
| 2827 | /* |
| 2828 | * Unshare file descriptor table if it is being shared |
| 2829 | */ |
| 2830 | static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp) |
| 2831 | { |
| 2832 | struct files_struct *fd = current->files; |
| 2833 | int error = 0; |
| 2834 | |
| 2835 | if ((unshare_flags & CLONE_FILES) && |
| 2836 | (fd && atomic_read(&fd->count) > 1)) { |
| 2837 | *new_fdp = dup_fd(fd, &error); |
| 2838 | if (!*new_fdp) |
| 2839 | return error; |
| 2840 | } |
| 2841 | |
| 2842 | return 0; |
| 2843 | } |
| 2844 | |
| 2845 | /* |
| 2846 | * unshare allows a process to 'unshare' part of the process |
| 2847 | * context which was originally shared using clone. copy_* |
| 2848 | * functions used by do_fork() cannot be used here directly |
| 2849 | * because they modify an inactive task_struct that is being |
| 2850 | * constructed. Here we are modifying the current, active, |
| 2851 | * task_struct. |
| 2852 | */ |
| 2853 | int ksys_unshare(unsigned long unshare_flags) |
| 2854 | { |
| 2855 | struct fs_struct *fs, *new_fs = NULL; |
| 2856 | struct files_struct *fd, *new_fd = NULL; |
| 2857 | struct cred *new_cred = NULL; |
| 2858 | struct nsproxy *new_nsproxy = NULL; |
| 2859 | int do_sysvsem = 0; |
| 2860 | int err; |
| 2861 | |
| 2862 | /* |
| 2863 | * If unsharing a user namespace must also unshare the thread group |
| 2864 | * and unshare the filesystem root and working directories. |
| 2865 | */ |
| 2866 | if (unshare_flags & CLONE_NEWUSER) |
| 2867 | unshare_flags |= CLONE_THREAD | CLONE_FS; |
| 2868 | /* |
| 2869 | * If unsharing vm, must also unshare signal handlers. |
| 2870 | */ |
| 2871 | if (unshare_flags & CLONE_VM) |
| 2872 | unshare_flags |= CLONE_SIGHAND; |
| 2873 | /* |
| 2874 | * If unsharing a signal handlers, must also unshare the signal queues. |
| 2875 | */ |
| 2876 | if (unshare_flags & CLONE_SIGHAND) |
| 2877 | unshare_flags |= CLONE_THREAD; |
| 2878 | /* |
| 2879 | * If unsharing namespace, must also unshare filesystem information. |
| 2880 | */ |
| 2881 | if (unshare_flags & CLONE_NEWNS) |
| 2882 | unshare_flags |= CLONE_FS; |
| 2883 | |
| 2884 | err = check_unshare_flags(unshare_flags); |
| 2885 | if (err) |
| 2886 | goto bad_unshare_out; |
| 2887 | /* |
| 2888 | * CLONE_NEWIPC must also detach from the undolist: after switching |
| 2889 | * to a new ipc namespace, the semaphore arrays from the old |
| 2890 | * namespace are unreachable. |
| 2891 | */ |
| 2892 | if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM)) |
| 2893 | do_sysvsem = 1; |
| 2894 | err = unshare_fs(unshare_flags, &new_fs); |
| 2895 | if (err) |
| 2896 | goto bad_unshare_out; |
| 2897 | err = unshare_fd(unshare_flags, &new_fd); |
| 2898 | if (err) |
| 2899 | goto bad_unshare_cleanup_fs; |
| 2900 | err = unshare_userns(unshare_flags, &new_cred); |
| 2901 | if (err) |
| 2902 | goto bad_unshare_cleanup_fd; |
| 2903 | err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, |
| 2904 | new_cred, new_fs); |
| 2905 | if (err) |
| 2906 | goto bad_unshare_cleanup_cred; |
| 2907 | |
| 2908 | if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) { |
| 2909 | if (do_sysvsem) { |
| 2910 | /* |
| 2911 | * CLONE_SYSVSEM is equivalent to sys_exit(). |
| 2912 | */ |
| 2913 | exit_sem(current); |
| 2914 | } |
| 2915 | if (unshare_flags & CLONE_NEWIPC) { |
| 2916 | /* Orphan segments in old ns (see sem above). */ |
| 2917 | exit_shm(current); |
| 2918 | shm_init_task(current); |
| 2919 | } |
| 2920 | |
| 2921 | if (new_nsproxy) |
| 2922 | switch_task_namespaces(current, new_nsproxy); |
| 2923 | |
| 2924 | task_lock(current); |
| 2925 | |
| 2926 | if (new_fs) { |
| 2927 | fs = current->fs; |
| 2928 | spin_lock(&fs->lock); |
| 2929 | current->fs = new_fs; |
| 2930 | if (--fs->users) |
| 2931 | new_fs = NULL; |
| 2932 | else |
| 2933 | new_fs = fs; |
| 2934 | spin_unlock(&fs->lock); |
| 2935 | } |
| 2936 | |
| 2937 | if (new_fd) { |
| 2938 | fd = current->files; |
| 2939 | current->files = new_fd; |
| 2940 | new_fd = fd; |
| 2941 | } |
| 2942 | |
| 2943 | task_unlock(current); |
| 2944 | |
| 2945 | if (new_cred) { |
| 2946 | /* Install the new user namespace */ |
| 2947 | commit_creds(new_cred); |
| 2948 | new_cred = NULL; |
| 2949 | } |
| 2950 | } |
| 2951 | |
| 2952 | perf_event_namespaces(current); |
| 2953 | |
| 2954 | bad_unshare_cleanup_cred: |
| 2955 | if (new_cred) |
| 2956 | put_cred(new_cred); |
| 2957 | bad_unshare_cleanup_fd: |
| 2958 | if (new_fd) |
| 2959 | put_files_struct(new_fd); |
| 2960 | |
| 2961 | bad_unshare_cleanup_fs: |
| 2962 | if (new_fs) |
| 2963 | free_fs_struct(new_fs); |
| 2964 | |
| 2965 | bad_unshare_out: |
| 2966 | return err; |
| 2967 | } |
| 2968 | |
| 2969 | SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) |
| 2970 | { |
| 2971 | return ksys_unshare(unshare_flags); |
| 2972 | } |
| 2973 | |
| 2974 | /* |
| 2975 | * Helper to unshare the files of the current task. |
| 2976 | * We don't want to expose copy_files internals to |
| 2977 | * the exec layer of the kernel. |
| 2978 | */ |
| 2979 | |
| 2980 | int unshare_files(struct files_struct **displaced) |
| 2981 | { |
| 2982 | struct task_struct *task = current; |
| 2983 | struct files_struct *copy = NULL; |
| 2984 | int error; |
| 2985 | |
| 2986 | error = unshare_fd(CLONE_FILES, ©); |
| 2987 | if (error || !copy) { |
| 2988 | *displaced = NULL; |
| 2989 | return error; |
| 2990 | } |
| 2991 | *displaced = task->files; |
| 2992 | task_lock(task); |
| 2993 | task->files = copy; |
| 2994 | task_unlock(task); |
| 2995 | return 0; |
| 2996 | } |
| 2997 | |
| 2998 | int sysctl_max_threads(struct ctl_table *table, int write, |
| 2999 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 3000 | { |
| 3001 | struct ctl_table t; |
| 3002 | int ret; |
| 3003 | int threads = max_threads; |
| 3004 | int min = 1; |
| 3005 | int max = MAX_THREADS; |
| 3006 | |
| 3007 | t = *table; |
| 3008 | t.data = &threads; |
| 3009 | t.extra1 = &min; |
| 3010 | t.extra2 = &max; |
| 3011 | |
| 3012 | ret = proc_dointvec_minmax(&t, write, buffer, lenp, ppos); |
| 3013 | if (ret || !write) |
| 3014 | return ret; |
| 3015 | |
| 3016 | max_threads = threads; |
| 3017 | |
| 3018 | return 0; |
| 3019 | } |