blob: ff9acf962da8c12f78ebee5d7a240e0716ad07bd
1 | /* |
2 | * linux/kernel/fork.c |
3 | * |
4 | * Copyright (C) 1991, 1992 Linus Torvalds |
5 | */ |
6 | |
7 | /* |
8 | * 'fork.c' contains the help-routines for the 'fork' system call |
9 | * (see also entry.S and others). |
10 | * Fork is rather simple, once you get the hang of it, but the memory |
11 | * management can be a bitch. See 'mm/memory.c': 'copy_page_range()' |
12 | */ |
13 | |
14 | #include <linux/slab.h> |
15 | #include <linux/init.h> |
16 | #include <linux/unistd.h> |
17 | #include <linux/module.h> |
18 | #include <linux/vmalloc.h> |
19 | #include <linux/completion.h> |
20 | #include <linux/personality.h> |
21 | #include <linux/mempolicy.h> |
22 | #include <linux/sem.h> |
23 | #include <linux/file.h> |
24 | #include <linux/fdtable.h> |
25 | #include <linux/iocontext.h> |
26 | #include <linux/key.h> |
27 | #include <linux/binfmts.h> |
28 | #include <linux/mman.h> |
29 | #include <linux/mmu_notifier.h> |
30 | #include <linux/fs.h> |
31 | #include <linux/mm.h> |
32 | #include <linux/vmacache.h> |
33 | #include <linux/nsproxy.h> |
34 | #include <linux/capability.h> |
35 | #include <linux/cpu.h> |
36 | #include <linux/cgroup.h> |
37 | #include <linux/security.h> |
38 | #include <linux/hugetlb.h> |
39 | #include <linux/seccomp.h> |
40 | #include <linux/swap.h> |
41 | #include <linux/syscalls.h> |
42 | #include <linux/jiffies.h> |
43 | #include <linux/futex.h> |
44 | #include <linux/compat.h> |
45 | #include <linux/kthread.h> |
46 | #include <linux/task_io_accounting_ops.h> |
47 | #include <linux/rcupdate.h> |
48 | #include <linux/ptrace.h> |
49 | #include <linux/mount.h> |
50 | #include <linux/audit.h> |
51 | #include <linux/memcontrol.h> |
52 | #include <linux/ftrace.h> |
53 | #include <linux/proc_fs.h> |
54 | #include <linux/profile.h> |
55 | #include <linux/rmap.h> |
56 | #include <linux/ksm.h> |
57 | #include <linux/acct.h> |
58 | #include <linux/tsacct_kern.h> |
59 | #include <linux/cn_proc.h> |
60 | #include <linux/freezer.h> |
61 | #include <linux/kaiser.h> |
62 | #include <linux/delayacct.h> |
63 | #include <linux/taskstats_kern.h> |
64 | #include <linux/random.h> |
65 | #include <linux/tty.h> |
66 | #include <linux/blkdev.h> |
67 | #include <linux/fs_struct.h> |
68 | #include <linux/magic.h> |
69 | #include <linux/perf_event.h> |
70 | #include <linux/posix-timers.h> |
71 | #include <linux/user-return-notifier.h> |
72 | #include <linux/oom.h> |
73 | #include <linux/khugepaged.h> |
74 | #include <linux/signalfd.h> |
75 | #include <linux/uprobes.h> |
76 | #include <linux/aio.h> |
77 | #include <linux/compiler.h> |
78 | #include <linux/sysctl.h> |
79 | #include <linux/kcov.h> |
80 | #include <linux/cpufreq_times.h> |
81 | |
82 | #include <asm/pgtable.h> |
83 | #include <asm/pgalloc.h> |
84 | #include <asm/uaccess.h> |
85 | #include <asm/mmu_context.h> |
86 | #include <asm/cacheflush.h> |
87 | #include <asm/tlbflush.h> |
88 | |
89 | #ifdef CONFIG_AMLOGIC_VMAP |
90 | #include <linux/amlogic/vmap_stack.h> |
91 | #endif |
92 | |
93 | #include <trace/events/sched.h> |
94 | |
95 | #define CREATE_TRACE_POINTS |
96 | #include <trace/events/task.h> |
97 | |
98 | /* |
99 | * Minimum number of threads to boot the kernel |
100 | */ |
101 | #define MIN_THREADS 20 |
102 | |
103 | /* |
104 | * Maximum number of threads |
105 | */ |
106 | #define MAX_THREADS FUTEX_TID_MASK |
107 | |
108 | /* |
109 | * Protected counters by write_lock_irq(&tasklist_lock) |
110 | */ |
111 | unsigned long total_forks; /* Handle normal Linux uptimes. */ |
112 | int nr_threads; /* The idle threads do not count.. */ |
113 | |
114 | int max_threads; /* tunable limit on nr_threads */ |
115 | |
116 | DEFINE_PER_CPU(unsigned long, process_counts) = 0; |
117 | |
118 | __cacheline_aligned DEFINE_RWLOCK(tasklist_lock); /* outer */ |
119 | |
120 | #ifdef CONFIG_PROVE_RCU |
121 | int lockdep_tasklist_lock_is_held(void) |
122 | { |
123 | return lockdep_is_held(&tasklist_lock); |
124 | } |
125 | EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held); |
126 | #endif /* #ifdef CONFIG_PROVE_RCU */ |
127 | |
128 | int nr_processes(void) |
129 | { |
130 | int cpu; |
131 | int total = 0; |
132 | |
133 | for_each_possible_cpu(cpu) |
134 | total += per_cpu(process_counts, cpu); |
135 | |
136 | return total; |
137 | } |
138 | |
139 | void __weak arch_release_task_struct(struct task_struct *tsk) |
140 | { |
141 | } |
142 | |
143 | #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR |
144 | static struct kmem_cache *task_struct_cachep; |
145 | |
146 | static inline struct task_struct *alloc_task_struct_node(int node) |
147 | { |
148 | return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node); |
149 | } |
150 | |
151 | static inline void free_task_struct(struct task_struct *tsk) |
152 | { |
153 | kmem_cache_free(task_struct_cachep, tsk); |
154 | } |
155 | #endif |
156 | |
157 | void __weak arch_release_thread_stack(unsigned long *stack) |
158 | { |
159 | } |
160 | |
161 | #ifndef CONFIG_ARCH_THREAD_STACK_ALLOCATOR |
162 | |
163 | /* |
164 | * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a |
165 | * kmemcache based allocator. |
166 | */ |
167 | # if THREAD_SIZE >= PAGE_SIZE || defined(CONFIG_VMAP_STACK) |
168 | |
169 | #ifdef CONFIG_VMAP_STACK |
170 | /* |
171 | * vmalloc() is a bit slow, and calling vfree() enough times will force a TLB |
172 | * flush. Try to minimize the number of calls by caching stacks. |
173 | */ |
174 | #define NR_CACHED_STACKS 2 |
175 | static DEFINE_PER_CPU(struct vm_struct *, cached_stacks[NR_CACHED_STACKS]); |
176 | #endif |
177 | |
178 | static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node) |
179 | { |
180 | #ifdef CONFIG_VMAP_STACK |
181 | void *stack; |
182 | int i; |
183 | |
184 | local_irq_disable(); |
185 | for (i = 0; i < NR_CACHED_STACKS; i++) { |
186 | struct vm_struct *s = this_cpu_read(cached_stacks[i]); |
187 | |
188 | if (!s) |
189 | continue; |
190 | this_cpu_write(cached_stacks[i], NULL); |
191 | |
192 | /* Clear stale pointers from reused stack. */ |
193 | memset(s->addr, 0, THREAD_SIZE); |
194 | |
195 | tsk->stack_vm_area = s; |
196 | local_irq_enable(); |
197 | return s->addr; |
198 | } |
199 | local_irq_enable(); |
200 | |
201 | stack = __vmalloc_node_range(THREAD_SIZE, THREAD_SIZE, |
202 | VMALLOC_START, VMALLOC_END, |
203 | THREADINFO_GFP | __GFP_HIGHMEM, |
204 | PAGE_KERNEL, |
205 | 0, node, __builtin_return_address(0)); |
206 | |
207 | /* |
208 | * We can't call find_vm_area() in interrupt context, and |
209 | * free_thread_stack() can be called in interrupt context, |
210 | * so cache the vm_struct. |
211 | */ |
212 | if (stack) |
213 | tsk->stack_vm_area = find_vm_area(stack); |
214 | return stack; |
215 | #else |
216 | #ifdef CONFIG_AMLOGIC_VMAP |
217 | return aml_stack_alloc(node, tsk); |
218 | #else /* CONFIG_AMLOGIC_VMAP */ |
219 | struct page *page = alloc_pages_node(node, THREADINFO_GFP, |
220 | THREAD_SIZE_ORDER); |
221 | |
222 | return page ? page_address(page) : NULL; |
223 | #endif /* CONFIG_AMLOGIC_VMAP */ |
224 | #endif |
225 | } |
226 | |
227 | static inline void free_thread_stack(struct task_struct *tsk) |
228 | { |
229 | #ifdef CONFIG_AMLOGIC_VMAP |
230 | aml_stack_free(tsk); |
231 | #else /* CONFIG_AMLOGIC_VMAP */ |
232 | kaiser_unmap_thread_stack(tsk->stack); |
233 | #ifdef CONFIG_VMAP_STACK |
234 | if (task_stack_vm_area(tsk)) { |
235 | unsigned long flags; |
236 | int i; |
237 | |
238 | local_irq_save(flags); |
239 | for (i = 0; i < NR_CACHED_STACKS; i++) { |
240 | if (this_cpu_read(cached_stacks[i])) |
241 | continue; |
242 | |
243 | this_cpu_write(cached_stacks[i], tsk->stack_vm_area); |
244 | local_irq_restore(flags); |
245 | return; |
246 | } |
247 | local_irq_restore(flags); |
248 | |
249 | vfree(tsk->stack); |
250 | return; |
251 | } |
252 | #endif |
253 | |
254 | __free_pages(virt_to_page(tsk->stack), THREAD_SIZE_ORDER); |
255 | #endif /* CONFIG_AMLOGIC_VMAP */ |
256 | } |
257 | # else |
258 | static struct kmem_cache *thread_stack_cache; |
259 | |
260 | static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, |
261 | int node) |
262 | { |
263 | return kmem_cache_alloc_node(thread_stack_cache, THREADINFO_GFP, node); |
264 | } |
265 | |
266 | static void free_thread_stack(struct task_struct *tsk) |
267 | { |
268 | kmem_cache_free(thread_stack_cache, tsk->stack); |
269 | } |
270 | |
271 | void thread_stack_cache_init(void) |
272 | { |
273 | thread_stack_cache = kmem_cache_create("thread_stack", THREAD_SIZE, |
274 | THREAD_SIZE, 0, NULL); |
275 | BUG_ON(thread_stack_cache == NULL); |
276 | } |
277 | # endif |
278 | #endif |
279 | |
280 | /* SLAB cache for signal_struct structures (tsk->signal) */ |
281 | static struct kmem_cache *signal_cachep; |
282 | |
283 | /* SLAB cache for sighand_struct structures (tsk->sighand) */ |
284 | struct kmem_cache *sighand_cachep; |
285 | |
286 | /* SLAB cache for files_struct structures (tsk->files) */ |
287 | struct kmem_cache *files_cachep; |
288 | |
289 | /* SLAB cache for fs_struct structures (tsk->fs) */ |
290 | struct kmem_cache *fs_cachep; |
291 | |
292 | /* SLAB cache for vm_area_struct structures */ |
293 | struct kmem_cache *vm_area_cachep; |
294 | |
295 | /* SLAB cache for mm_struct structures (tsk->mm) */ |
296 | static struct kmem_cache *mm_cachep; |
297 | |
298 | static void account_kernel_stack(struct task_struct *tsk, int account) |
299 | { |
300 | #ifdef CONFIG_AMLOGIC_VMAP |
301 | aml_account_task_stack(tsk, account); |
302 | #else |
303 | void *stack = task_stack_page(tsk); |
304 | struct vm_struct *vm = task_stack_vm_area(tsk); |
305 | |
306 | BUILD_BUG_ON(IS_ENABLED(CONFIG_VMAP_STACK) && PAGE_SIZE % 1024 != 0); |
307 | |
308 | if (vm) { |
309 | int i; |
310 | |
311 | BUG_ON(vm->nr_pages != THREAD_SIZE / PAGE_SIZE); |
312 | |
313 | for (i = 0; i < THREAD_SIZE / PAGE_SIZE; i++) { |
314 | mod_zone_page_state(page_zone(vm->pages[i]), |
315 | NR_KERNEL_STACK_KB, |
316 | PAGE_SIZE / 1024 * account); |
317 | } |
318 | |
319 | /* All stack pages belong to the same memcg. */ |
320 | memcg_kmem_update_page_stat(vm->pages[0], MEMCG_KERNEL_STACK_KB, |
321 | account * (THREAD_SIZE / 1024)); |
322 | } else { |
323 | /* |
324 | * All stack pages are in the same zone and belong to the |
325 | * same memcg. |
326 | */ |
327 | struct page *first_page = virt_to_page(stack); |
328 | |
329 | mod_zone_page_state(page_zone(first_page), NR_KERNEL_STACK_KB, |
330 | THREAD_SIZE / 1024 * account); |
331 | |
332 | memcg_kmem_update_page_stat(first_page, MEMCG_KERNEL_STACK_KB, |
333 | account * (THREAD_SIZE / 1024)); |
334 | } |
335 | #endif /* CONFIG_AMLOGIC_VMAP*/ |
336 | } |
337 | |
338 | static void release_task_stack(struct task_struct *tsk) |
339 | { |
340 | if (WARN_ON(tsk->state != TASK_DEAD)) |
341 | return; /* Better to leak the stack than to free prematurely */ |
342 | |
343 | account_kernel_stack(tsk, -1); |
344 | arch_release_thread_stack(tsk->stack); |
345 | free_thread_stack(tsk); |
346 | tsk->stack = NULL; |
347 | #ifdef CONFIG_VMAP_STACK |
348 | tsk->stack_vm_area = NULL; |
349 | #endif |
350 | } |
351 | |
352 | #ifdef CONFIG_THREAD_INFO_IN_TASK |
353 | void put_task_stack(struct task_struct *tsk) |
354 | { |
355 | if (atomic_dec_and_test(&tsk->stack_refcount)) |
356 | release_task_stack(tsk); |
357 | } |
358 | #endif |
359 | |
360 | void free_task(struct task_struct *tsk) |
361 | { |
362 | cpufreq_task_times_exit(tsk); |
363 | |
364 | #ifndef CONFIG_THREAD_INFO_IN_TASK |
365 | /* |
366 | * The task is finally done with both the stack and thread_info, |
367 | * so free both. |
368 | */ |
369 | release_task_stack(tsk); |
370 | #else |
371 | /* |
372 | * If the task had a separate stack allocation, it should be gone |
373 | * by now. |
374 | */ |
375 | WARN_ON_ONCE(atomic_read(&tsk->stack_refcount) != 0); |
376 | #endif |
377 | rt_mutex_debug_task_free(tsk); |
378 | ftrace_graph_exit_task(tsk); |
379 | put_seccomp_filter(tsk); |
380 | arch_release_task_struct(tsk); |
381 | free_task_struct(tsk); |
382 | } |
383 | EXPORT_SYMBOL(free_task); |
384 | |
385 | static inline void free_signal_struct(struct signal_struct *sig) |
386 | { |
387 | taskstats_tgid_free(sig); |
388 | sched_autogroup_exit(sig); |
389 | /* |
390 | * __mmdrop is not safe to call from softirq context on x86 due to |
391 | * pgd_dtor so postpone it to the async context |
392 | */ |
393 | if (sig->oom_mm) |
394 | mmdrop_async(sig->oom_mm); |
395 | kmem_cache_free(signal_cachep, sig); |
396 | } |
397 | |
398 | static inline void put_signal_struct(struct signal_struct *sig) |
399 | { |
400 | if (atomic_dec_and_test(&sig->sigcnt)) |
401 | free_signal_struct(sig); |
402 | } |
403 | |
404 | void __put_task_struct(struct task_struct *tsk) |
405 | { |
406 | WARN_ON(!tsk->exit_state); |
407 | WARN_ON(atomic_read(&tsk->usage)); |
408 | WARN_ON(tsk == current); |
409 | |
410 | cgroup_free(tsk); |
411 | task_numa_free(tsk); |
412 | security_task_free(tsk); |
413 | exit_creds(tsk); |
414 | delayacct_tsk_free(tsk); |
415 | put_signal_struct(tsk->signal); |
416 | |
417 | if (!profile_handoff_task(tsk)) |
418 | free_task(tsk); |
419 | } |
420 | EXPORT_SYMBOL_GPL(__put_task_struct); |
421 | |
422 | void __init __weak arch_task_cache_init(void) { } |
423 | |
424 | /* |
425 | * set_max_threads |
426 | */ |
427 | static void set_max_threads(unsigned int max_threads_suggested) |
428 | { |
429 | u64 threads; |
430 | |
431 | /* |
432 | * The number of threads shall be limited such that the thread |
433 | * structures may only consume a small part of the available memory. |
434 | */ |
435 | if (fls64(totalram_pages) + fls64(PAGE_SIZE) > 64) |
436 | threads = MAX_THREADS; |
437 | else |
438 | threads = div64_u64((u64) totalram_pages * (u64) PAGE_SIZE, |
439 | (u64) THREAD_SIZE * 8UL); |
440 | |
441 | if (threads > max_threads_suggested) |
442 | threads = max_threads_suggested; |
443 | |
444 | max_threads = clamp_t(u64, threads, MIN_THREADS, MAX_THREADS); |
445 | } |
446 | |
447 | #ifdef CONFIG_ARCH_WANTS_DYNAMIC_TASK_STRUCT |
448 | /* Initialized by the architecture: */ |
449 | int arch_task_struct_size __read_mostly; |
450 | #endif |
451 | |
452 | void __init fork_init(void) |
453 | { |
454 | int i; |
455 | #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR |
456 | #ifndef ARCH_MIN_TASKALIGN |
457 | #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES |
458 | #endif |
459 | /* create a slab on which task_structs can be allocated */ |
460 | task_struct_cachep = kmem_cache_create("task_struct", |
461 | arch_task_struct_size, ARCH_MIN_TASKALIGN, |
462 | SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, NULL); |
463 | #endif |
464 | |
465 | /* do the arch specific task caches init */ |
466 | arch_task_cache_init(); |
467 | |
468 | set_max_threads(MAX_THREADS); |
469 | |
470 | init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2; |
471 | init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2; |
472 | init_task.signal->rlim[RLIMIT_SIGPENDING] = |
473 | init_task.signal->rlim[RLIMIT_NPROC]; |
474 | |
475 | for (i = 0; i < UCOUNT_COUNTS; i++) { |
476 | init_user_ns.ucount_max[i] = max_threads/2; |
477 | } |
478 | } |
479 | |
480 | int __weak arch_dup_task_struct(struct task_struct *dst, |
481 | struct task_struct *src) |
482 | { |
483 | *dst = *src; |
484 | return 0; |
485 | } |
486 | |
487 | void set_task_stack_end_magic(struct task_struct *tsk) |
488 | { |
489 | unsigned long *stackend; |
490 | |
491 | stackend = end_of_stack(tsk); |
492 | #ifndef CONFIG_AMLOGIC_VMAP |
493 | *stackend = STACK_END_MAGIC; /* for overflow detection */ |
494 | #endif |
495 | } |
496 | |
497 | static struct task_struct *dup_task_struct(struct task_struct *orig, int node) |
498 | { |
499 | struct task_struct *tsk; |
500 | unsigned long *stack; |
501 | struct vm_struct *stack_vm_area; |
502 | int err; |
503 | |
504 | if (node == NUMA_NO_NODE) |
505 | node = tsk_fork_get_node(orig); |
506 | tsk = alloc_task_struct_node(node); |
507 | if (!tsk) |
508 | return NULL; |
509 | |
510 | stack = alloc_thread_stack_node(tsk, node); |
511 | if (!stack) |
512 | goto free_tsk; |
513 | |
514 | stack_vm_area = task_stack_vm_area(tsk); |
515 | |
516 | err = arch_dup_task_struct(tsk, orig); |
517 | |
518 | /* |
519 | * arch_dup_task_struct() clobbers the stack-related fields. Make |
520 | * sure they're properly initialized before using any stack-related |
521 | * functions again. |
522 | */ |
523 | tsk->stack = stack; |
524 | |
525 | err= kaiser_map_thread_stack(tsk->stack); |
526 | if (err) |
527 | goto free_stack; |
528 | #ifdef CONFIG_VMAP_STACK |
529 | tsk->stack_vm_area = stack_vm_area; |
530 | #endif |
531 | #ifdef CONFIG_THREAD_INFO_IN_TASK |
532 | atomic_set(&tsk->stack_refcount, 1); |
533 | #endif |
534 | |
535 | if (err) |
536 | goto free_stack; |
537 | |
538 | #ifdef CONFIG_SECCOMP |
539 | /* |
540 | * We must handle setting up seccomp filters once we're under |
541 | * the sighand lock in case orig has changed between now and |
542 | * then. Until then, filter must be NULL to avoid messing up |
543 | * the usage counts on the error path calling free_task. |
544 | */ |
545 | tsk->seccomp.filter = NULL; |
546 | #endif |
547 | |
548 | setup_thread_stack(tsk, orig); |
549 | clear_user_return_notifier(tsk); |
550 | clear_tsk_need_resched(tsk); |
551 | set_task_stack_end_magic(tsk); |
552 | |
553 | #ifdef CONFIG_CC_STACKPROTECTOR |
554 | tsk->stack_canary = get_random_long(); |
555 | #endif |
556 | |
557 | /* |
558 | * One for us, one for whoever does the "release_task()" (usually |
559 | * parent) |
560 | */ |
561 | atomic_set(&tsk->usage, 2); |
562 | #ifdef CONFIG_BLK_DEV_IO_TRACE |
563 | tsk->btrace_seq = 0; |
564 | #endif |
565 | tsk->splice_pipe = NULL; |
566 | tsk->task_frag.page = NULL; |
567 | tsk->wake_q.next = NULL; |
568 | |
569 | account_kernel_stack(tsk, 1); |
570 | |
571 | kcov_task_init(tsk); |
572 | |
573 | return tsk; |
574 | |
575 | free_stack: |
576 | free_thread_stack(tsk); |
577 | free_tsk: |
578 | free_task_struct(tsk); |
579 | return NULL; |
580 | } |
581 | |
582 | #ifdef CONFIG_MMU |
583 | static __latent_entropy int dup_mmap(struct mm_struct *mm, |
584 | struct mm_struct *oldmm) |
585 | { |
586 | struct vm_area_struct *mpnt, *tmp, *prev, **pprev; |
587 | struct rb_node **rb_link, *rb_parent; |
588 | int retval; |
589 | unsigned long charge; |
590 | |
591 | uprobe_start_dup_mmap(); |
592 | if (down_write_killable(&oldmm->mmap_sem)) { |
593 | retval = -EINTR; |
594 | goto fail_uprobe_end; |
595 | } |
596 | flush_cache_dup_mm(oldmm); |
597 | uprobe_dup_mmap(oldmm, mm); |
598 | /* |
599 | * Not linked in yet - no deadlock potential: |
600 | */ |
601 | down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING); |
602 | |
603 | /* No ordering required: file already has been exposed. */ |
604 | RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm)); |
605 | |
606 | mm->total_vm = oldmm->total_vm; |
607 | mm->data_vm = oldmm->data_vm; |
608 | mm->exec_vm = oldmm->exec_vm; |
609 | mm->stack_vm = oldmm->stack_vm; |
610 | |
611 | rb_link = &mm->mm_rb.rb_node; |
612 | rb_parent = NULL; |
613 | pprev = &mm->mmap; |
614 | retval = ksm_fork(mm, oldmm); |
615 | if (retval) |
616 | goto out; |
617 | retval = khugepaged_fork(mm, oldmm); |
618 | if (retval) |
619 | goto out; |
620 | |
621 | prev = NULL; |
622 | for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) { |
623 | struct file *file; |
624 | |
625 | if (mpnt->vm_flags & VM_DONTCOPY) { |
626 | vm_stat_account(mm, mpnt->vm_flags, -vma_pages(mpnt)); |
627 | continue; |
628 | } |
629 | charge = 0; |
630 | if (mpnt->vm_flags & VM_ACCOUNT) { |
631 | unsigned long len = vma_pages(mpnt); |
632 | |
633 | if (security_vm_enough_memory_mm(oldmm, len)) /* sic */ |
634 | goto fail_nomem; |
635 | charge = len; |
636 | } |
637 | tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL); |
638 | if (!tmp) |
639 | goto fail_nomem; |
640 | *tmp = *mpnt; |
641 | INIT_LIST_HEAD(&tmp->anon_vma_chain); |
642 | retval = vma_dup_policy(mpnt, tmp); |
643 | if (retval) |
644 | goto fail_nomem_policy; |
645 | tmp->vm_mm = mm; |
646 | if (anon_vma_fork(tmp, mpnt)) |
647 | goto fail_nomem_anon_vma_fork; |
648 | tmp->vm_flags &= |
649 | ~(VM_LOCKED|VM_LOCKONFAULT|VM_UFFD_MISSING|VM_UFFD_WP); |
650 | tmp->vm_next = tmp->vm_prev = NULL; |
651 | tmp->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX; |
652 | file = tmp->vm_file; |
653 | if (file) { |
654 | struct inode *inode = file_inode(file); |
655 | struct address_space *mapping = file->f_mapping; |
656 | |
657 | get_file(file); |
658 | if (tmp->vm_flags & VM_DENYWRITE) |
659 | atomic_dec(&inode->i_writecount); |
660 | i_mmap_lock_write(mapping); |
661 | if (tmp->vm_flags & VM_SHARED) |
662 | atomic_inc(&mapping->i_mmap_writable); |
663 | flush_dcache_mmap_lock(mapping); |
664 | /* insert tmp into the share list, just after mpnt */ |
665 | vma_interval_tree_insert_after(tmp, mpnt, |
666 | &mapping->i_mmap); |
667 | flush_dcache_mmap_unlock(mapping); |
668 | i_mmap_unlock_write(mapping); |
669 | } |
670 | |
671 | /* |
672 | * Clear hugetlb-related page reserves for children. This only |
673 | * affects MAP_PRIVATE mappings. Faults generated by the child |
674 | * are not guaranteed to succeed, even if read-only |
675 | */ |
676 | if (is_vm_hugetlb_page(tmp)) |
677 | reset_vma_resv_huge_pages(tmp); |
678 | |
679 | /* |
680 | * Link in the new vma and copy the page table entries. |
681 | */ |
682 | *pprev = tmp; |
683 | pprev = &tmp->vm_next; |
684 | tmp->vm_prev = prev; |
685 | prev = tmp; |
686 | |
687 | __vma_link_rb(mm, tmp, rb_link, rb_parent); |
688 | rb_link = &tmp->vm_rb.rb_right; |
689 | rb_parent = &tmp->vm_rb; |
690 | |
691 | mm->map_count++; |
692 | retval = copy_page_range(mm, oldmm, mpnt); |
693 | |
694 | if (tmp->vm_ops && tmp->vm_ops->open) |
695 | tmp->vm_ops->open(tmp); |
696 | |
697 | if (retval) |
698 | goto out; |
699 | } |
700 | /* a new mm has just been created */ |
701 | arch_dup_mmap(oldmm, mm); |
702 | retval = 0; |
703 | out: |
704 | up_write(&mm->mmap_sem); |
705 | flush_tlb_mm(oldmm); |
706 | up_write(&oldmm->mmap_sem); |
707 | fail_uprobe_end: |
708 | uprobe_end_dup_mmap(); |
709 | return retval; |
710 | fail_nomem_anon_vma_fork: |
711 | mpol_put(vma_policy(tmp)); |
712 | fail_nomem_policy: |
713 | kmem_cache_free(vm_area_cachep, tmp); |
714 | fail_nomem: |
715 | retval = -ENOMEM; |
716 | vm_unacct_memory(charge); |
717 | goto out; |
718 | } |
719 | |
720 | static inline int mm_alloc_pgd(struct mm_struct *mm) |
721 | { |
722 | mm->pgd = pgd_alloc(mm); |
723 | if (unlikely(!mm->pgd)) |
724 | return -ENOMEM; |
725 | return 0; |
726 | } |
727 | |
728 | static inline void mm_free_pgd(struct mm_struct *mm) |
729 | { |
730 | pgd_free(mm, mm->pgd); |
731 | } |
732 | #else |
733 | static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm) |
734 | { |
735 | down_write(&oldmm->mmap_sem); |
736 | RCU_INIT_POINTER(mm->exe_file, get_mm_exe_file(oldmm)); |
737 | up_write(&oldmm->mmap_sem); |
738 | return 0; |
739 | } |
740 | #define mm_alloc_pgd(mm) (0) |
741 | #define mm_free_pgd(mm) |
742 | #endif /* CONFIG_MMU */ |
743 | |
744 | __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock); |
745 | |
746 | #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL)) |
747 | #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm))) |
748 | |
749 | static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT; |
750 | |
751 | static int __init coredump_filter_setup(char *s) |
752 | { |
753 | default_dump_filter = |
754 | (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) & |
755 | MMF_DUMP_FILTER_MASK; |
756 | return 1; |
757 | } |
758 | |
759 | __setup("coredump_filter=", coredump_filter_setup); |
760 | |
761 | #include <linux/init_task.h> |
762 | |
763 | static void mm_init_aio(struct mm_struct *mm) |
764 | { |
765 | #ifdef CONFIG_AIO |
766 | spin_lock_init(&mm->ioctx_lock); |
767 | mm->ioctx_table = NULL; |
768 | #endif |
769 | } |
770 | |
771 | static void mm_init_owner(struct mm_struct *mm, struct task_struct *p) |
772 | { |
773 | #ifdef CONFIG_MEMCG |
774 | mm->owner = p; |
775 | #endif |
776 | } |
777 | |
778 | static void mm_init_uprobes_state(struct mm_struct *mm) |
779 | { |
780 | #ifdef CONFIG_UPROBES |
781 | mm->uprobes_state.xol_area = NULL; |
782 | #endif |
783 | } |
784 | |
785 | static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p, |
786 | struct user_namespace *user_ns) |
787 | { |
788 | mm->mmap = NULL; |
789 | mm->mm_rb = RB_ROOT; |
790 | mm->vmacache_seqnum = 0; |
791 | atomic_set(&mm->mm_users, 1); |
792 | atomic_set(&mm->mm_count, 1); |
793 | init_rwsem(&mm->mmap_sem); |
794 | INIT_LIST_HEAD(&mm->mmlist); |
795 | mm->core_state = NULL; |
796 | atomic_long_set(&mm->nr_ptes, 0); |
797 | mm_nr_pmds_init(mm); |
798 | mm->map_count = 0; |
799 | mm->locked_vm = 0; |
800 | mm->pinned_vm = 0; |
801 | memset(&mm->rss_stat, 0, sizeof(mm->rss_stat)); |
802 | spin_lock_init(&mm->page_table_lock); |
803 | mm_init_cpumask(mm); |
804 | mm_init_aio(mm); |
805 | mm_init_owner(mm, p); |
806 | RCU_INIT_POINTER(mm->exe_file, NULL); |
807 | mmu_notifier_mm_init(mm); |
808 | clear_tlb_flush_pending(mm); |
809 | #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS |
810 | mm->pmd_huge_pte = NULL; |
811 | #endif |
812 | mm_init_uprobes_state(mm); |
813 | |
814 | if (current->mm) { |
815 | mm->flags = current->mm->flags & MMF_INIT_MASK; |
816 | mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK; |
817 | } else { |
818 | mm->flags = default_dump_filter; |
819 | mm->def_flags = 0; |
820 | } |
821 | |
822 | if (mm_alloc_pgd(mm)) |
823 | goto fail_nopgd; |
824 | |
825 | if (init_new_context(p, mm)) |
826 | goto fail_nocontext; |
827 | |
828 | mm->user_ns = get_user_ns(user_ns); |
829 | return mm; |
830 | |
831 | fail_nocontext: |
832 | mm_free_pgd(mm); |
833 | fail_nopgd: |
834 | free_mm(mm); |
835 | return NULL; |
836 | } |
837 | |
838 | static void check_mm(struct mm_struct *mm) |
839 | { |
840 | int i; |
841 | |
842 | for (i = 0; i < NR_MM_COUNTERS; i++) { |
843 | long x = atomic_long_read(&mm->rss_stat.count[i]); |
844 | |
845 | if (unlikely(x)) |
846 | printk(KERN_ALERT "BUG: Bad rss-counter state " |
847 | "mm:%p idx:%d val:%ld\n", mm, i, x); |
848 | } |
849 | |
850 | if (atomic_long_read(&mm->nr_ptes)) |
851 | pr_alert("BUG: non-zero nr_ptes on freeing mm: %ld\n", |
852 | atomic_long_read(&mm->nr_ptes)); |
853 | if (mm_nr_pmds(mm)) |
854 | pr_alert("BUG: non-zero nr_pmds on freeing mm: %ld\n", |
855 | mm_nr_pmds(mm)); |
856 | |
857 | #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS |
858 | VM_BUG_ON_MM(mm->pmd_huge_pte, mm); |
859 | #endif |
860 | } |
861 | |
862 | /* |
863 | * Allocate and initialize an mm_struct. |
864 | */ |
865 | struct mm_struct *mm_alloc(void) |
866 | { |
867 | struct mm_struct *mm; |
868 | |
869 | mm = allocate_mm(); |
870 | if (!mm) |
871 | return NULL; |
872 | |
873 | memset(mm, 0, sizeof(*mm)); |
874 | return mm_init(mm, current, current_user_ns()); |
875 | } |
876 | |
877 | /* |
878 | * Called when the last reference to the mm |
879 | * is dropped: either by a lazy thread or by |
880 | * mmput. Free the page directory and the mm. |
881 | */ |
882 | void __mmdrop(struct mm_struct *mm) |
883 | { |
884 | BUG_ON(mm == &init_mm); |
885 | mm_free_pgd(mm); |
886 | destroy_context(mm); |
887 | mmu_notifier_mm_destroy(mm); |
888 | check_mm(mm); |
889 | put_user_ns(mm->user_ns); |
890 | free_mm(mm); |
891 | } |
892 | EXPORT_SYMBOL_GPL(__mmdrop); |
893 | |
894 | static inline void __mmput(struct mm_struct *mm) |
895 | { |
896 | VM_BUG_ON(atomic_read(&mm->mm_users)); |
897 | |
898 | uprobe_clear_state(mm); |
899 | exit_aio(mm); |
900 | ksm_exit(mm); |
901 | khugepaged_exit(mm); /* must run before exit_mmap */ |
902 | exit_mmap(mm); |
903 | mm_put_huge_zero_page(mm); |
904 | set_mm_exe_file(mm, NULL); |
905 | if (!list_empty(&mm->mmlist)) { |
906 | spin_lock(&mmlist_lock); |
907 | list_del(&mm->mmlist); |
908 | spin_unlock(&mmlist_lock); |
909 | } |
910 | if (mm->binfmt) |
911 | module_put(mm->binfmt->module); |
912 | set_bit(MMF_OOM_SKIP, &mm->flags); |
913 | mmdrop(mm); |
914 | } |
915 | |
916 | /* |
917 | * Decrement the use count and release all resources for an mm. |
918 | */ |
919 | void mmput(struct mm_struct *mm) |
920 | { |
921 | might_sleep(); |
922 | |
923 | if (atomic_dec_and_test(&mm->mm_users)) |
924 | __mmput(mm); |
925 | } |
926 | EXPORT_SYMBOL_GPL(mmput); |
927 | |
928 | #ifdef CONFIG_MMU |
929 | static void mmput_async_fn(struct work_struct *work) |
930 | { |
931 | struct mm_struct *mm = container_of(work, struct mm_struct, async_put_work); |
932 | __mmput(mm); |
933 | } |
934 | |
935 | void mmput_async(struct mm_struct *mm) |
936 | { |
937 | if (atomic_dec_and_test(&mm->mm_users)) { |
938 | INIT_WORK(&mm->async_put_work, mmput_async_fn); |
939 | schedule_work(&mm->async_put_work); |
940 | } |
941 | } |
942 | #endif |
943 | |
944 | /** |
945 | * set_mm_exe_file - change a reference to the mm's executable file |
946 | * |
947 | * This changes mm's executable file (shown as symlink /proc/[pid]/exe). |
948 | * |
949 | * Main users are mmput() and sys_execve(). Callers prevent concurrent |
950 | * invocations: in mmput() nobody alive left, in execve task is single |
951 | * threaded. sys_prctl(PR_SET_MM_MAP/EXE_FILE) also needs to set the |
952 | * mm->exe_file, but does so without using set_mm_exe_file() in order |
953 | * to do avoid the need for any locks. |
954 | */ |
955 | void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file) |
956 | { |
957 | struct file *old_exe_file; |
958 | |
959 | /* |
960 | * It is safe to dereference the exe_file without RCU as |
961 | * this function is only called if nobody else can access |
962 | * this mm -- see comment above for justification. |
963 | */ |
964 | old_exe_file = rcu_dereference_raw(mm->exe_file); |
965 | |
966 | if (new_exe_file) |
967 | get_file(new_exe_file); |
968 | rcu_assign_pointer(mm->exe_file, new_exe_file); |
969 | if (old_exe_file) |
970 | fput(old_exe_file); |
971 | } |
972 | |
973 | /** |
974 | * get_mm_exe_file - acquire a reference to the mm's executable file |
975 | * |
976 | * Returns %NULL if mm has no associated executable file. |
977 | * User must release file via fput(). |
978 | */ |
979 | struct file *get_mm_exe_file(struct mm_struct *mm) |
980 | { |
981 | struct file *exe_file; |
982 | |
983 | rcu_read_lock(); |
984 | exe_file = rcu_dereference(mm->exe_file); |
985 | if (exe_file && !get_file_rcu(exe_file)) |
986 | exe_file = NULL; |
987 | rcu_read_unlock(); |
988 | return exe_file; |
989 | } |
990 | EXPORT_SYMBOL(get_mm_exe_file); |
991 | |
992 | /** |
993 | * get_task_exe_file - acquire a reference to the task's executable file |
994 | * |
995 | * Returns %NULL if task's mm (if any) has no associated executable file or |
996 | * this is a kernel thread with borrowed mm (see the comment above get_task_mm). |
997 | * User must release file via fput(). |
998 | */ |
999 | struct file *get_task_exe_file(struct task_struct *task) |
1000 | { |
1001 | struct file *exe_file = NULL; |
1002 | struct mm_struct *mm; |
1003 | |
1004 | task_lock(task); |
1005 | mm = task->mm; |
1006 | if (mm) { |
1007 | if (!(task->flags & PF_KTHREAD)) |
1008 | exe_file = get_mm_exe_file(mm); |
1009 | } |
1010 | task_unlock(task); |
1011 | return exe_file; |
1012 | } |
1013 | EXPORT_SYMBOL(get_task_exe_file); |
1014 | |
1015 | /** |
1016 | * get_task_mm - acquire a reference to the task's mm |
1017 | * |
1018 | * Returns %NULL if the task has no mm. Checks PF_KTHREAD (meaning |
1019 | * this kernel workthread has transiently adopted a user mm with use_mm, |
1020 | * to do its AIO) is not set and if so returns a reference to it, after |
1021 | * bumping up the use count. User must release the mm via mmput() |
1022 | * after use. Typically used by /proc and ptrace. |
1023 | */ |
1024 | struct mm_struct *get_task_mm(struct task_struct *task) |
1025 | { |
1026 | struct mm_struct *mm; |
1027 | |
1028 | task_lock(task); |
1029 | mm = task->mm; |
1030 | if (mm) { |
1031 | if (task->flags & PF_KTHREAD) |
1032 | mm = NULL; |
1033 | else |
1034 | atomic_inc(&mm->mm_users); |
1035 | } |
1036 | task_unlock(task); |
1037 | return mm; |
1038 | } |
1039 | EXPORT_SYMBOL_GPL(get_task_mm); |
1040 | |
1041 | struct mm_struct *mm_access(struct task_struct *task, unsigned int mode) |
1042 | { |
1043 | struct mm_struct *mm; |
1044 | int err; |
1045 | |
1046 | err = mutex_lock_killable(&task->signal->cred_guard_mutex); |
1047 | if (err) |
1048 | return ERR_PTR(err); |
1049 | |
1050 | mm = get_task_mm(task); |
1051 | if (mm && mm != current->mm && |
1052 | !ptrace_may_access(task, mode)) { |
1053 | mmput(mm); |
1054 | mm = ERR_PTR(-EACCES); |
1055 | } |
1056 | mutex_unlock(&task->signal->cred_guard_mutex); |
1057 | |
1058 | return mm; |
1059 | } |
1060 | |
1061 | static void complete_vfork_done(struct task_struct *tsk) |
1062 | { |
1063 | struct completion *vfork; |
1064 | |
1065 | task_lock(tsk); |
1066 | vfork = tsk->vfork_done; |
1067 | if (likely(vfork)) { |
1068 | tsk->vfork_done = NULL; |
1069 | complete(vfork); |
1070 | } |
1071 | task_unlock(tsk); |
1072 | } |
1073 | |
1074 | static int wait_for_vfork_done(struct task_struct *child, |
1075 | struct completion *vfork) |
1076 | { |
1077 | int killed; |
1078 | |
1079 | freezer_do_not_count(); |
1080 | killed = wait_for_completion_killable(vfork); |
1081 | freezer_count(); |
1082 | |
1083 | if (killed) { |
1084 | task_lock(child); |
1085 | child->vfork_done = NULL; |
1086 | task_unlock(child); |
1087 | } |
1088 | |
1089 | put_task_struct(child); |
1090 | return killed; |
1091 | } |
1092 | |
1093 | /* Please note the differences between mmput and mm_release. |
1094 | * mmput is called whenever we stop holding onto a mm_struct, |
1095 | * error success whatever. |
1096 | * |
1097 | * mm_release is called after a mm_struct has been removed |
1098 | * from the current process. |
1099 | * |
1100 | * This difference is important for error handling, when we |
1101 | * only half set up a mm_struct for a new process and need to restore |
1102 | * the old one. Because we mmput the new mm_struct before |
1103 | * restoring the old one. . . |
1104 | * Eric Biederman 10 January 1998 |
1105 | */ |
1106 | void mm_release(struct task_struct *tsk, struct mm_struct *mm) |
1107 | { |
1108 | /* Get rid of any futexes when releasing the mm */ |
1109 | #ifdef CONFIG_FUTEX |
1110 | if (unlikely(tsk->robust_list)) { |
1111 | exit_robust_list(tsk); |
1112 | tsk->robust_list = NULL; |
1113 | } |
1114 | #ifdef CONFIG_COMPAT |
1115 | if (unlikely(tsk->compat_robust_list)) { |
1116 | compat_exit_robust_list(tsk); |
1117 | tsk->compat_robust_list = NULL; |
1118 | } |
1119 | #endif |
1120 | if (unlikely(!list_empty(&tsk->pi_state_list))) |
1121 | exit_pi_state_list(tsk); |
1122 | #endif |
1123 | |
1124 | uprobe_free_utask(tsk); |
1125 | |
1126 | /* Get rid of any cached register state */ |
1127 | deactivate_mm(tsk, mm); |
1128 | |
1129 | /* |
1130 | * Signal userspace if we're not exiting with a core dump |
1131 | * because we want to leave the value intact for debugging |
1132 | * purposes. |
1133 | */ |
1134 | if (tsk->clear_child_tid) { |
1135 | if (!(tsk->signal->flags & SIGNAL_GROUP_COREDUMP) && |
1136 | atomic_read(&mm->mm_users) > 1) { |
1137 | /* |
1138 | * We don't check the error code - if userspace has |
1139 | * not set up a proper pointer then tough luck. |
1140 | */ |
1141 | put_user(0, tsk->clear_child_tid); |
1142 | sys_futex(tsk->clear_child_tid, FUTEX_WAKE, |
1143 | 1, NULL, NULL, 0); |
1144 | } |
1145 | tsk->clear_child_tid = NULL; |
1146 | } |
1147 | |
1148 | /* |
1149 | * All done, finally we can wake up parent and return this mm to him. |
1150 | * Also kthread_stop() uses this completion for synchronization. |
1151 | */ |
1152 | if (tsk->vfork_done) |
1153 | complete_vfork_done(tsk); |
1154 | } |
1155 | |
1156 | /* |
1157 | * Allocate a new mm structure and copy contents from the |
1158 | * mm structure of the passed in task structure. |
1159 | */ |
1160 | static struct mm_struct *dup_mm(struct task_struct *tsk) |
1161 | { |
1162 | struct mm_struct *mm, *oldmm = current->mm; |
1163 | int err; |
1164 | |
1165 | mm = allocate_mm(); |
1166 | if (!mm) |
1167 | goto fail_nomem; |
1168 | |
1169 | memcpy(mm, oldmm, sizeof(*mm)); |
1170 | |
1171 | if (!mm_init(mm, tsk, mm->user_ns)) |
1172 | goto fail_nomem; |
1173 | |
1174 | err = dup_mmap(mm, oldmm); |
1175 | if (err) |
1176 | goto free_pt; |
1177 | |
1178 | mm->hiwater_rss = get_mm_rss(mm); |
1179 | mm->hiwater_vm = mm->total_vm; |
1180 | |
1181 | if (mm->binfmt && !try_module_get(mm->binfmt->module)) |
1182 | goto free_pt; |
1183 | |
1184 | return mm; |
1185 | |
1186 | free_pt: |
1187 | /* don't put binfmt in mmput, we haven't got module yet */ |
1188 | mm->binfmt = NULL; |
1189 | mmput(mm); |
1190 | |
1191 | fail_nomem: |
1192 | return NULL; |
1193 | } |
1194 | |
1195 | static int copy_mm(unsigned long clone_flags, struct task_struct *tsk) |
1196 | { |
1197 | struct mm_struct *mm, *oldmm; |
1198 | int retval; |
1199 | |
1200 | tsk->min_flt = tsk->maj_flt = 0; |
1201 | tsk->nvcsw = tsk->nivcsw = 0; |
1202 | #ifdef CONFIG_DETECT_HUNG_TASK |
1203 | tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw; |
1204 | #endif |
1205 | |
1206 | tsk->mm = NULL; |
1207 | tsk->active_mm = NULL; |
1208 | |
1209 | /* |
1210 | * Are we cloning a kernel thread? |
1211 | * |
1212 | * We need to steal a active VM for that.. |
1213 | */ |
1214 | oldmm = current->mm; |
1215 | if (!oldmm) |
1216 | return 0; |
1217 | |
1218 | /* initialize the new vmacache entries */ |
1219 | vmacache_flush(tsk); |
1220 | |
1221 | if (clone_flags & CLONE_VM) { |
1222 | atomic_inc(&oldmm->mm_users); |
1223 | mm = oldmm; |
1224 | goto good_mm; |
1225 | } |
1226 | |
1227 | retval = -ENOMEM; |
1228 | mm = dup_mm(tsk); |
1229 | if (!mm) |
1230 | goto fail_nomem; |
1231 | |
1232 | good_mm: |
1233 | tsk->mm = mm; |
1234 | tsk->active_mm = mm; |
1235 | return 0; |
1236 | |
1237 | fail_nomem: |
1238 | return retval; |
1239 | } |
1240 | |
1241 | static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) |
1242 | { |
1243 | struct fs_struct *fs = current->fs; |
1244 | if (clone_flags & CLONE_FS) { |
1245 | /* tsk->fs is already what we want */ |
1246 | spin_lock(&fs->lock); |
1247 | if (fs->in_exec) { |
1248 | spin_unlock(&fs->lock); |
1249 | return -EAGAIN; |
1250 | } |
1251 | fs->users++; |
1252 | spin_unlock(&fs->lock); |
1253 | return 0; |
1254 | } |
1255 | tsk->fs = copy_fs_struct(fs); |
1256 | if (!tsk->fs) |
1257 | return -ENOMEM; |
1258 | return 0; |
1259 | } |
1260 | |
1261 | static int copy_files(unsigned long clone_flags, struct task_struct *tsk) |
1262 | { |
1263 | struct files_struct *oldf, *newf; |
1264 | int error = 0; |
1265 | |
1266 | /* |
1267 | * A background process may not have any files ... |
1268 | */ |
1269 | oldf = current->files; |
1270 | if (!oldf) |
1271 | goto out; |
1272 | |
1273 | if (clone_flags & CLONE_FILES) { |
1274 | atomic_inc(&oldf->count); |
1275 | goto out; |
1276 | } |
1277 | |
1278 | newf = dup_fd(oldf, &error); |
1279 | if (!newf) |
1280 | goto out; |
1281 | |
1282 | tsk->files = newf; |
1283 | error = 0; |
1284 | out: |
1285 | return error; |
1286 | } |
1287 | |
1288 | static int copy_io(unsigned long clone_flags, struct task_struct *tsk) |
1289 | { |
1290 | #ifdef CONFIG_BLOCK |
1291 | struct io_context *ioc = current->io_context; |
1292 | struct io_context *new_ioc; |
1293 | |
1294 | if (!ioc) |
1295 | return 0; |
1296 | /* |
1297 | * Share io context with parent, if CLONE_IO is set |
1298 | */ |
1299 | if (clone_flags & CLONE_IO) { |
1300 | ioc_task_link(ioc); |
1301 | tsk->io_context = ioc; |
1302 | } else if (ioprio_valid(ioc->ioprio)) { |
1303 | new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE); |
1304 | if (unlikely(!new_ioc)) |
1305 | return -ENOMEM; |
1306 | |
1307 | new_ioc->ioprio = ioc->ioprio; |
1308 | put_io_context(new_ioc); |
1309 | } |
1310 | #endif |
1311 | return 0; |
1312 | } |
1313 | |
1314 | static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk) |
1315 | { |
1316 | struct sighand_struct *sig; |
1317 | |
1318 | if (clone_flags & CLONE_SIGHAND) { |
1319 | atomic_inc(¤t->sighand->count); |
1320 | return 0; |
1321 | } |
1322 | sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL); |
1323 | rcu_assign_pointer(tsk->sighand, sig); |
1324 | if (!sig) |
1325 | return -ENOMEM; |
1326 | |
1327 | atomic_set(&sig->count, 1); |
1328 | spin_lock_irq(¤t->sighand->siglock); |
1329 | memcpy(sig->action, current->sighand->action, sizeof(sig->action)); |
1330 | spin_unlock_irq(¤t->sighand->siglock); |
1331 | return 0; |
1332 | } |
1333 | |
1334 | void __cleanup_sighand(struct sighand_struct *sighand) |
1335 | { |
1336 | if (atomic_dec_and_test(&sighand->count)) { |
1337 | signalfd_cleanup(sighand); |
1338 | /* |
1339 | * sighand_cachep is SLAB_DESTROY_BY_RCU so we can free it |
1340 | * without an RCU grace period, see __lock_task_sighand(). |
1341 | */ |
1342 | kmem_cache_free(sighand_cachep, sighand); |
1343 | } |
1344 | } |
1345 | |
1346 | /* |
1347 | * Initialize POSIX timer handling for a thread group. |
1348 | */ |
1349 | static void posix_cpu_timers_init_group(struct signal_struct *sig) |
1350 | { |
1351 | unsigned long cpu_limit; |
1352 | |
1353 | cpu_limit = READ_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur); |
1354 | if (cpu_limit != RLIM_INFINITY) { |
1355 | sig->cputime_expires.prof_exp = secs_to_cputime(cpu_limit); |
1356 | sig->cputimer.running = true; |
1357 | } |
1358 | |
1359 | /* The timer lists. */ |
1360 | INIT_LIST_HEAD(&sig->cpu_timers[0]); |
1361 | INIT_LIST_HEAD(&sig->cpu_timers[1]); |
1362 | INIT_LIST_HEAD(&sig->cpu_timers[2]); |
1363 | } |
1364 | |
1365 | static int copy_signal(unsigned long clone_flags, struct task_struct *tsk) |
1366 | { |
1367 | struct signal_struct *sig; |
1368 | |
1369 | if (clone_flags & CLONE_THREAD) |
1370 | return 0; |
1371 | |
1372 | sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL); |
1373 | tsk->signal = sig; |
1374 | if (!sig) |
1375 | return -ENOMEM; |
1376 | |
1377 | sig->nr_threads = 1; |
1378 | atomic_set(&sig->live, 1); |
1379 | atomic_set(&sig->sigcnt, 1); |
1380 | |
1381 | /* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */ |
1382 | sig->thread_head = (struct list_head)LIST_HEAD_INIT(tsk->thread_node); |
1383 | tsk->thread_node = (struct list_head)LIST_HEAD_INIT(sig->thread_head); |
1384 | |
1385 | init_waitqueue_head(&sig->wait_chldexit); |
1386 | sig->curr_target = tsk; |
1387 | init_sigpending(&sig->shared_pending); |
1388 | INIT_LIST_HEAD(&sig->posix_timers); |
1389 | seqlock_init(&sig->stats_lock); |
1390 | prev_cputime_init(&sig->prev_cputime); |
1391 | |
1392 | hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
1393 | sig->real_timer.function = it_real_fn; |
1394 | |
1395 | task_lock(current->group_leader); |
1396 | memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim); |
1397 | task_unlock(current->group_leader); |
1398 | |
1399 | posix_cpu_timers_init_group(sig); |
1400 | |
1401 | tty_audit_fork(sig); |
1402 | sched_autogroup_fork(sig); |
1403 | |
1404 | sig->oom_score_adj = current->signal->oom_score_adj; |
1405 | sig->oom_score_adj_min = current->signal->oom_score_adj_min; |
1406 | |
1407 | sig->has_child_subreaper = current->signal->has_child_subreaper || |
1408 | current->signal->is_child_subreaper; |
1409 | |
1410 | mutex_init(&sig->cred_guard_mutex); |
1411 | |
1412 | return 0; |
1413 | } |
1414 | |
1415 | static void copy_seccomp(struct task_struct *p) |
1416 | { |
1417 | #ifdef CONFIG_SECCOMP |
1418 | /* |
1419 | * Must be called with sighand->lock held, which is common to |
1420 | * all threads in the group. Holding cred_guard_mutex is not |
1421 | * needed because this new task is not yet running and cannot |
1422 | * be racing exec. |
1423 | */ |
1424 | assert_spin_locked(¤t->sighand->siglock); |
1425 | |
1426 | /* Ref-count the new filter user, and assign it. */ |
1427 | get_seccomp_filter(current); |
1428 | p->seccomp = current->seccomp; |
1429 | |
1430 | /* |
1431 | * Explicitly enable no_new_privs here in case it got set |
1432 | * between the task_struct being duplicated and holding the |
1433 | * sighand lock. The seccomp state and nnp must be in sync. |
1434 | */ |
1435 | if (task_no_new_privs(current)) |
1436 | task_set_no_new_privs(p); |
1437 | |
1438 | /* |
1439 | * If the parent gained a seccomp mode after copying thread |
1440 | * flags and between before we held the sighand lock, we have |
1441 | * to manually enable the seccomp thread flag here. |
1442 | */ |
1443 | if (p->seccomp.mode != SECCOMP_MODE_DISABLED) |
1444 | set_tsk_thread_flag(p, TIF_SECCOMP); |
1445 | #endif |
1446 | } |
1447 | |
1448 | SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr) |
1449 | { |
1450 | current->clear_child_tid = tidptr; |
1451 | |
1452 | return task_pid_vnr(current); |
1453 | } |
1454 | |
1455 | static void rt_mutex_init_task(struct task_struct *p) |
1456 | { |
1457 | raw_spin_lock_init(&p->pi_lock); |
1458 | #ifdef CONFIG_RT_MUTEXES |
1459 | p->pi_waiters = RB_ROOT; |
1460 | p->pi_waiters_leftmost = NULL; |
1461 | p->pi_blocked_on = NULL; |
1462 | #endif |
1463 | } |
1464 | |
1465 | /* |
1466 | * Initialize POSIX timer handling for a single task. |
1467 | */ |
1468 | static void posix_cpu_timers_init(struct task_struct *tsk) |
1469 | { |
1470 | tsk->cputime_expires.prof_exp = 0; |
1471 | tsk->cputime_expires.virt_exp = 0; |
1472 | tsk->cputime_expires.sched_exp = 0; |
1473 | INIT_LIST_HEAD(&tsk->cpu_timers[0]); |
1474 | INIT_LIST_HEAD(&tsk->cpu_timers[1]); |
1475 | INIT_LIST_HEAD(&tsk->cpu_timers[2]); |
1476 | } |
1477 | |
1478 | static inline void |
1479 | init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid) |
1480 | { |
1481 | task->pids[type].pid = pid; |
1482 | } |
1483 | |
1484 | /* |
1485 | * This creates a new process as a copy of the old one, |
1486 | * but does not actually start it yet. |
1487 | * |
1488 | * It copies the registers, and all the appropriate |
1489 | * parts of the process environment (as per the clone |
1490 | * flags). The actual kick-off is left to the caller. |
1491 | */ |
1492 | static __latent_entropy struct task_struct *copy_process( |
1493 | unsigned long clone_flags, |
1494 | unsigned long stack_start, |
1495 | unsigned long stack_size, |
1496 | int __user *child_tidptr, |
1497 | struct pid *pid, |
1498 | int trace, |
1499 | unsigned long tls, |
1500 | int node) |
1501 | { |
1502 | int retval; |
1503 | struct task_struct *p; |
1504 | |
1505 | if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS)) |
1506 | return ERR_PTR(-EINVAL); |
1507 | |
1508 | if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS)) |
1509 | return ERR_PTR(-EINVAL); |
1510 | |
1511 | /* |
1512 | * Thread groups must share signals as well, and detached threads |
1513 | * can only be started up within the thread group. |
1514 | */ |
1515 | if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND)) |
1516 | return ERR_PTR(-EINVAL); |
1517 | |
1518 | /* |
1519 | * Shared signal handlers imply shared VM. By way of the above, |
1520 | * thread groups also imply shared VM. Blocking this case allows |
1521 | * for various simplifications in other code. |
1522 | */ |
1523 | if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM)) |
1524 | return ERR_PTR(-EINVAL); |
1525 | |
1526 | /* |
1527 | * Siblings of global init remain as zombies on exit since they are |
1528 | * not reaped by their parent (swapper). To solve this and to avoid |
1529 | * multi-rooted process trees, prevent global and container-inits |
1530 | * from creating siblings. |
1531 | */ |
1532 | if ((clone_flags & CLONE_PARENT) && |
1533 | current->signal->flags & SIGNAL_UNKILLABLE) |
1534 | return ERR_PTR(-EINVAL); |
1535 | |
1536 | /* |
1537 | * If the new process will be in a different pid or user namespace |
1538 | * do not allow it to share a thread group with the forking task. |
1539 | */ |
1540 | if (clone_flags & CLONE_THREAD) { |
1541 | if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) || |
1542 | (task_active_pid_ns(current) != |
1543 | current->nsproxy->pid_ns_for_children)) |
1544 | return ERR_PTR(-EINVAL); |
1545 | } |
1546 | |
1547 | retval = security_task_create(clone_flags); |
1548 | if (retval) |
1549 | goto fork_out; |
1550 | |
1551 | retval = -ENOMEM; |
1552 | p = dup_task_struct(current, node); |
1553 | if (!p) |
1554 | goto fork_out; |
1555 | |
1556 | cpufreq_task_times_init(p); |
1557 | |
1558 | /* |
1559 | * This _must_ happen before we call free_task(), i.e. before we jump |
1560 | * to any of the bad_fork_* labels. This is to avoid freeing |
1561 | * p->set_child_tid which is (ab)used as a kthread's data pointer for |
1562 | * kernel threads (PF_KTHREAD). |
1563 | */ |
1564 | p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL; |
1565 | /* |
1566 | * Clear TID on mm_release()? |
1567 | */ |
1568 | p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr : NULL; |
1569 | |
1570 | ftrace_graph_init_task(p); |
1571 | |
1572 | rt_mutex_init_task(p); |
1573 | |
1574 | #ifdef CONFIG_PROVE_LOCKING |
1575 | DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled); |
1576 | DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled); |
1577 | #endif |
1578 | retval = -EAGAIN; |
1579 | if (atomic_read(&p->real_cred->user->processes) >= |
1580 | task_rlimit(p, RLIMIT_NPROC)) { |
1581 | if (p->real_cred->user != INIT_USER && |
1582 | !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) |
1583 | goto bad_fork_free; |
1584 | } |
1585 | current->flags &= ~PF_NPROC_EXCEEDED; |
1586 | |
1587 | retval = copy_creds(p, clone_flags); |
1588 | if (retval < 0) |
1589 | goto bad_fork_free; |
1590 | |
1591 | /* |
1592 | * If multiple threads are within copy_process(), then this check |
1593 | * triggers too late. This doesn't hurt, the check is only there |
1594 | * to stop root fork bombs. |
1595 | */ |
1596 | retval = -EAGAIN; |
1597 | if (nr_threads >= max_threads) |
1598 | goto bad_fork_cleanup_count; |
1599 | |
1600 | delayacct_tsk_init(p); /* Must remain after dup_task_struct() */ |
1601 | p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER); |
1602 | p->flags |= PF_FORKNOEXEC; |
1603 | INIT_LIST_HEAD(&p->children); |
1604 | INIT_LIST_HEAD(&p->sibling); |
1605 | rcu_copy_process(p); |
1606 | p->vfork_done = NULL; |
1607 | spin_lock_init(&p->alloc_lock); |
1608 | |
1609 | init_sigpending(&p->pending); |
1610 | |
1611 | p->utime = p->stime = p->gtime = 0; |
1612 | p->utimescaled = p->stimescaled = 0; |
1613 | prev_cputime_init(&p->prev_cputime); |
1614 | |
1615 | #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN |
1616 | seqcount_init(&p->vtime_seqcount); |
1617 | p->vtime_snap = 0; |
1618 | p->vtime_snap_whence = VTIME_INACTIVE; |
1619 | #endif |
1620 | |
1621 | #if defined(SPLIT_RSS_COUNTING) |
1622 | memset(&p->rss_stat, 0, sizeof(p->rss_stat)); |
1623 | #endif |
1624 | |
1625 | p->default_timer_slack_ns = current->timer_slack_ns; |
1626 | |
1627 | #ifdef CONFIG_PSI |
1628 | p->psi_flags = 0; |
1629 | #endif |
1630 | |
1631 | task_io_accounting_init(&p->ioac); |
1632 | acct_clear_integrals(p); |
1633 | |
1634 | posix_cpu_timers_init(p); |
1635 | |
1636 | p->io_context = NULL; |
1637 | p->audit_context = NULL; |
1638 | cgroup_fork(p); |
1639 | #ifdef CONFIG_NUMA |
1640 | p->mempolicy = mpol_dup(p->mempolicy); |
1641 | if (IS_ERR(p->mempolicy)) { |
1642 | retval = PTR_ERR(p->mempolicy); |
1643 | p->mempolicy = NULL; |
1644 | goto bad_fork_cleanup_threadgroup_lock; |
1645 | } |
1646 | #endif |
1647 | #ifdef CONFIG_CPUSETS |
1648 | p->cpuset_mem_spread_rotor = NUMA_NO_NODE; |
1649 | p->cpuset_slab_spread_rotor = NUMA_NO_NODE; |
1650 | seqcount_init(&p->mems_allowed_seq); |
1651 | #endif |
1652 | #ifdef CONFIG_TRACE_IRQFLAGS |
1653 | p->irq_events = 0; |
1654 | p->hardirqs_enabled = 0; |
1655 | p->hardirq_enable_ip = 0; |
1656 | p->hardirq_enable_event = 0; |
1657 | p->hardirq_disable_ip = _THIS_IP_; |
1658 | p->hardirq_disable_event = 0; |
1659 | p->softirqs_enabled = 1; |
1660 | p->softirq_enable_ip = _THIS_IP_; |
1661 | p->softirq_enable_event = 0; |
1662 | p->softirq_disable_ip = 0; |
1663 | p->softirq_disable_event = 0; |
1664 | p->hardirq_context = 0; |
1665 | p->softirq_context = 0; |
1666 | #endif |
1667 | |
1668 | p->pagefault_disabled = 0; |
1669 | |
1670 | #ifdef CONFIG_LOCKDEP |
1671 | p->lockdep_depth = 0; /* no locks held yet */ |
1672 | p->curr_chain_key = 0; |
1673 | p->lockdep_recursion = 0; |
1674 | #endif |
1675 | |
1676 | #ifdef CONFIG_DEBUG_MUTEXES |
1677 | p->blocked_on = NULL; /* not blocked yet */ |
1678 | #endif |
1679 | #ifdef CONFIG_BCACHE |
1680 | p->sequential_io = 0; |
1681 | p->sequential_io_avg = 0; |
1682 | #endif |
1683 | |
1684 | /* Perform scheduler related setup. Assign this task to a CPU. */ |
1685 | retval = sched_fork(clone_flags, p); |
1686 | if (retval) |
1687 | goto bad_fork_cleanup_policy; |
1688 | |
1689 | retval = perf_event_init_task(p); |
1690 | if (retval) |
1691 | goto bad_fork_cleanup_policy; |
1692 | retval = audit_alloc(p); |
1693 | if (retval) |
1694 | goto bad_fork_cleanup_perf; |
1695 | /* copy all the process information */ |
1696 | shm_init_task(p); |
1697 | retval = copy_semundo(clone_flags, p); |
1698 | if (retval) |
1699 | goto bad_fork_cleanup_audit; |
1700 | retval = copy_files(clone_flags, p); |
1701 | if (retval) |
1702 | goto bad_fork_cleanup_semundo; |
1703 | retval = copy_fs(clone_flags, p); |
1704 | if (retval) |
1705 | goto bad_fork_cleanup_files; |
1706 | retval = copy_sighand(clone_flags, p); |
1707 | if (retval) |
1708 | goto bad_fork_cleanup_fs; |
1709 | retval = copy_signal(clone_flags, p); |
1710 | if (retval) |
1711 | goto bad_fork_cleanup_sighand; |
1712 | retval = copy_mm(clone_flags, p); |
1713 | if (retval) |
1714 | goto bad_fork_cleanup_signal; |
1715 | retval = copy_namespaces(clone_flags, p); |
1716 | if (retval) |
1717 | goto bad_fork_cleanup_mm; |
1718 | retval = copy_io(clone_flags, p); |
1719 | if (retval) |
1720 | goto bad_fork_cleanup_namespaces; |
1721 | retval = copy_thread_tls(clone_flags, stack_start, stack_size, p, tls); |
1722 | if (retval) |
1723 | goto bad_fork_cleanup_io; |
1724 | |
1725 | if (pid != &init_struct_pid) { |
1726 | pid = alloc_pid(p->nsproxy->pid_ns_for_children); |
1727 | if (IS_ERR(pid)) { |
1728 | retval = PTR_ERR(pid); |
1729 | goto bad_fork_cleanup_thread; |
1730 | } |
1731 | } |
1732 | |
1733 | #ifdef CONFIG_BLOCK |
1734 | p->plug = NULL; |
1735 | #endif |
1736 | #ifdef CONFIG_FUTEX |
1737 | p->robust_list = NULL; |
1738 | #ifdef CONFIG_COMPAT |
1739 | p->compat_robust_list = NULL; |
1740 | #endif |
1741 | INIT_LIST_HEAD(&p->pi_state_list); |
1742 | p->pi_state_cache = NULL; |
1743 | #endif |
1744 | /* |
1745 | * sigaltstack should be cleared when sharing the same VM |
1746 | */ |
1747 | if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM) |
1748 | sas_ss_reset(p); |
1749 | |
1750 | /* |
1751 | * Syscall tracing and stepping should be turned off in the |
1752 | * child regardless of CLONE_PTRACE. |
1753 | */ |
1754 | user_disable_single_step(p); |
1755 | clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE); |
1756 | #ifdef TIF_SYSCALL_EMU |
1757 | clear_tsk_thread_flag(p, TIF_SYSCALL_EMU); |
1758 | #endif |
1759 | clear_all_latency_tracing(p); |
1760 | |
1761 | /* ok, now we should be set up.. */ |
1762 | p->pid = pid_nr(pid); |
1763 | if (clone_flags & CLONE_THREAD) { |
1764 | p->exit_signal = -1; |
1765 | p->group_leader = current->group_leader; |
1766 | p->tgid = current->tgid; |
1767 | } else { |
1768 | if (clone_flags & CLONE_PARENT) |
1769 | p->exit_signal = current->group_leader->exit_signal; |
1770 | else |
1771 | p->exit_signal = (clone_flags & CSIGNAL); |
1772 | p->group_leader = p; |
1773 | p->tgid = p->pid; |
1774 | } |
1775 | |
1776 | p->nr_dirtied = 0; |
1777 | p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10); |
1778 | p->dirty_paused_when = 0; |
1779 | |
1780 | p->pdeath_signal = 0; |
1781 | INIT_LIST_HEAD(&p->thread_group); |
1782 | p->task_works = NULL; |
1783 | |
1784 | threadgroup_change_begin(current); |
1785 | /* |
1786 | * Ensure that the cgroup subsystem policies allow the new process to be |
1787 | * forked. It should be noted the the new process's css_set can be changed |
1788 | * between here and cgroup_post_fork() if an organisation operation is in |
1789 | * progress. |
1790 | */ |
1791 | retval = cgroup_can_fork(p); |
1792 | if (retval) |
1793 | goto bad_fork_free_pid; |
1794 | |
1795 | /* |
1796 | * From this point on we must avoid any synchronous user-space |
1797 | * communication until we take the tasklist-lock. In particular, we do |
1798 | * not want user-space to be able to predict the process start-time by |
1799 | * stalling fork(2) after we recorded the start_time but before it is |
1800 | * visible to the system. |
1801 | */ |
1802 | |
1803 | p->start_time = ktime_get_ns(); |
1804 | p->real_start_time = ktime_get_boot_ns(); |
1805 | |
1806 | /* |
1807 | * Make it visible to the rest of the system, but dont wake it up yet. |
1808 | * Need tasklist lock for parent etc handling! |
1809 | */ |
1810 | write_lock_irq(&tasklist_lock); |
1811 | |
1812 | /* CLONE_PARENT re-uses the old parent */ |
1813 | if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) { |
1814 | p->real_parent = current->real_parent; |
1815 | p->parent_exec_id = current->parent_exec_id; |
1816 | } else { |
1817 | p->real_parent = current; |
1818 | p->parent_exec_id = current->self_exec_id; |
1819 | } |
1820 | |
1821 | spin_lock(¤t->sighand->siglock); |
1822 | |
1823 | /* |
1824 | * Copy seccomp details explicitly here, in case they were changed |
1825 | * before holding sighand lock. |
1826 | */ |
1827 | copy_seccomp(p); |
1828 | |
1829 | /* |
1830 | * Process group and session signals need to be delivered to just the |
1831 | * parent before the fork or both the parent and the child after the |
1832 | * fork. Restart if a signal comes in before we add the new process to |
1833 | * it's process group. |
1834 | * A fatal signal pending means that current will exit, so the new |
1835 | * thread can't slip out of an OOM kill (or normal SIGKILL). |
1836 | */ |
1837 | recalc_sigpending(); |
1838 | if (signal_pending(current)) { |
1839 | retval = -ERESTARTNOINTR; |
1840 | goto bad_fork_cancel_cgroup; |
1841 | } |
1842 | if (unlikely(!(ns_of_pid(pid)->nr_hashed & PIDNS_HASH_ADDING))) { |
1843 | retval = -ENOMEM; |
1844 | goto bad_fork_cancel_cgroup; |
1845 | } |
1846 | |
1847 | if (likely(p->pid)) { |
1848 | ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace); |
1849 | |
1850 | init_task_pid(p, PIDTYPE_PID, pid); |
1851 | if (thread_group_leader(p)) { |
1852 | init_task_pid(p, PIDTYPE_PGID, task_pgrp(current)); |
1853 | init_task_pid(p, PIDTYPE_SID, task_session(current)); |
1854 | |
1855 | if (is_child_reaper(pid)) { |
1856 | ns_of_pid(pid)->child_reaper = p; |
1857 | p->signal->flags |= SIGNAL_UNKILLABLE; |
1858 | } |
1859 | |
1860 | p->signal->leader_pid = pid; |
1861 | p->signal->tty = tty_kref_get(current->signal->tty); |
1862 | list_add_tail(&p->sibling, &p->real_parent->children); |
1863 | list_add_tail_rcu(&p->tasks, &init_task.tasks); |
1864 | attach_pid(p, PIDTYPE_PGID); |
1865 | attach_pid(p, PIDTYPE_SID); |
1866 | __this_cpu_inc(process_counts); |
1867 | } else { |
1868 | current->signal->nr_threads++; |
1869 | atomic_inc(¤t->signal->live); |
1870 | atomic_inc(¤t->signal->sigcnt); |
1871 | list_add_tail_rcu(&p->thread_group, |
1872 | &p->group_leader->thread_group); |
1873 | list_add_tail_rcu(&p->thread_node, |
1874 | &p->signal->thread_head); |
1875 | } |
1876 | attach_pid(p, PIDTYPE_PID); |
1877 | nr_threads++; |
1878 | } |
1879 | |
1880 | total_forks++; |
1881 | spin_unlock(¤t->sighand->siglock); |
1882 | syscall_tracepoint_update(p); |
1883 | write_unlock_irq(&tasklist_lock); |
1884 | |
1885 | proc_fork_connector(p); |
1886 | cgroup_post_fork(p); |
1887 | threadgroup_change_end(current); |
1888 | perf_event_fork(p); |
1889 | |
1890 | trace_task_newtask(p, clone_flags); |
1891 | uprobe_copy_process(p, clone_flags); |
1892 | |
1893 | return p; |
1894 | |
1895 | bad_fork_cancel_cgroup: |
1896 | spin_unlock(¤t->sighand->siglock); |
1897 | write_unlock_irq(&tasklist_lock); |
1898 | cgroup_cancel_fork(p); |
1899 | bad_fork_free_pid: |
1900 | threadgroup_change_end(current); |
1901 | if (pid != &init_struct_pid) |
1902 | free_pid(pid); |
1903 | bad_fork_cleanup_thread: |
1904 | exit_thread(p); |
1905 | bad_fork_cleanup_io: |
1906 | if (p->io_context) |
1907 | exit_io_context(p); |
1908 | bad_fork_cleanup_namespaces: |
1909 | exit_task_namespaces(p); |
1910 | bad_fork_cleanup_mm: |
1911 | if (p->mm) |
1912 | mmput(p->mm); |
1913 | bad_fork_cleanup_signal: |
1914 | if (!(clone_flags & CLONE_THREAD)) |
1915 | free_signal_struct(p->signal); |
1916 | bad_fork_cleanup_sighand: |
1917 | __cleanup_sighand(p->sighand); |
1918 | bad_fork_cleanup_fs: |
1919 | exit_fs(p); /* blocking */ |
1920 | bad_fork_cleanup_files: |
1921 | exit_files(p); /* blocking */ |
1922 | bad_fork_cleanup_semundo: |
1923 | exit_sem(p); |
1924 | bad_fork_cleanup_audit: |
1925 | audit_free(p); |
1926 | bad_fork_cleanup_perf: |
1927 | perf_event_free_task(p); |
1928 | bad_fork_cleanup_policy: |
1929 | #ifdef CONFIG_NUMA |
1930 | mpol_put(p->mempolicy); |
1931 | bad_fork_cleanup_threadgroup_lock: |
1932 | #endif |
1933 | delayacct_tsk_free(p); |
1934 | bad_fork_cleanup_count: |
1935 | atomic_dec(&p->cred->user->processes); |
1936 | exit_creds(p); |
1937 | bad_fork_free: |
1938 | p->state = TASK_DEAD; |
1939 | put_task_stack(p); |
1940 | free_task(p); |
1941 | fork_out: |
1942 | return ERR_PTR(retval); |
1943 | } |
1944 | |
1945 | static inline void init_idle_pids(struct pid_link *links) |
1946 | { |
1947 | enum pid_type type; |
1948 | |
1949 | for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) { |
1950 | INIT_HLIST_NODE(&links[type].node); /* not really needed */ |
1951 | links[type].pid = &init_struct_pid; |
1952 | } |
1953 | } |
1954 | |
1955 | struct task_struct *fork_idle(int cpu) |
1956 | { |
1957 | struct task_struct *task; |
1958 | task = copy_process(CLONE_VM, 0, 0, NULL, &init_struct_pid, 0, 0, |
1959 | cpu_to_node(cpu)); |
1960 | if (!IS_ERR(task)) { |
1961 | init_idle_pids(task->pids); |
1962 | init_idle(task, cpu); |
1963 | } |
1964 | |
1965 | return task; |
1966 | } |
1967 | |
1968 | /* |
1969 | * Ok, this is the main fork-routine. |
1970 | * |
1971 | * It copies the process, and if successful kick-starts |
1972 | * it and waits for it to finish using the VM if required. |
1973 | */ |
1974 | long _do_fork(unsigned long clone_flags, |
1975 | unsigned long stack_start, |
1976 | unsigned long stack_size, |
1977 | int __user *parent_tidptr, |
1978 | int __user *child_tidptr, |
1979 | unsigned long tls) |
1980 | { |
1981 | struct task_struct *p; |
1982 | int trace = 0; |
1983 | long nr; |
1984 | |
1985 | /* |
1986 | * Determine whether and which event to report to ptracer. When |
1987 | * called from kernel_thread or CLONE_UNTRACED is explicitly |
1988 | * requested, no event is reported; otherwise, report if the event |
1989 | * for the type of forking is enabled. |
1990 | */ |
1991 | if (!(clone_flags & CLONE_UNTRACED)) { |
1992 | if (clone_flags & CLONE_VFORK) |
1993 | trace = PTRACE_EVENT_VFORK; |
1994 | else if ((clone_flags & CSIGNAL) != SIGCHLD) |
1995 | trace = PTRACE_EVENT_CLONE; |
1996 | else |
1997 | trace = PTRACE_EVENT_FORK; |
1998 | |
1999 | if (likely(!ptrace_event_enabled(current, trace))) |
2000 | trace = 0; |
2001 | } |
2002 | |
2003 | p = copy_process(clone_flags, stack_start, stack_size, |
2004 | child_tidptr, NULL, trace, tls, NUMA_NO_NODE); |
2005 | add_latent_entropy(); |
2006 | /* |
2007 | * Do this prior waking up the new thread - the thread pointer |
2008 | * might get invalid after that point, if the thread exits quickly. |
2009 | */ |
2010 | if (!IS_ERR(p)) { |
2011 | struct completion vfork; |
2012 | struct pid *pid; |
2013 | |
2014 | cpufreq_task_times_alloc(p); |
2015 | |
2016 | trace_sched_process_fork(current, p); |
2017 | |
2018 | pid = get_task_pid(p, PIDTYPE_PID); |
2019 | nr = pid_vnr(pid); |
2020 | |
2021 | if (clone_flags & CLONE_PARENT_SETTID) |
2022 | put_user(nr, parent_tidptr); |
2023 | |
2024 | if (clone_flags & CLONE_VFORK) { |
2025 | p->vfork_done = &vfork; |
2026 | init_completion(&vfork); |
2027 | get_task_struct(p); |
2028 | } |
2029 | |
2030 | wake_up_new_task(p); |
2031 | |
2032 | /* forking complete and child started to run, tell ptracer */ |
2033 | if (unlikely(trace)) |
2034 | ptrace_event_pid(trace, pid); |
2035 | |
2036 | if (clone_flags & CLONE_VFORK) { |
2037 | if (!wait_for_vfork_done(p, &vfork)) |
2038 | ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid); |
2039 | } |
2040 | |
2041 | put_pid(pid); |
2042 | } else { |
2043 | nr = PTR_ERR(p); |
2044 | } |
2045 | return nr; |
2046 | } |
2047 | |
2048 | #ifndef CONFIG_HAVE_COPY_THREAD_TLS |
2049 | /* For compatibility with architectures that call do_fork directly rather than |
2050 | * using the syscall entry points below. */ |
2051 | long do_fork(unsigned long clone_flags, |
2052 | unsigned long stack_start, |
2053 | unsigned long stack_size, |
2054 | int __user *parent_tidptr, |
2055 | int __user *child_tidptr) |
2056 | { |
2057 | return _do_fork(clone_flags, stack_start, stack_size, |
2058 | parent_tidptr, child_tidptr, 0); |
2059 | } |
2060 | #endif |
2061 | |
2062 | /* |
2063 | * Create a kernel thread. |
2064 | */ |
2065 | pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) |
2066 | { |
2067 | return _do_fork(flags|CLONE_VM|CLONE_UNTRACED, (unsigned long)fn, |
2068 | (unsigned long)arg, NULL, NULL, 0); |
2069 | } |
2070 | |
2071 | #ifdef __ARCH_WANT_SYS_FORK |
2072 | SYSCALL_DEFINE0(fork) |
2073 | { |
2074 | #ifdef CONFIG_MMU |
2075 | return _do_fork(SIGCHLD, 0, 0, NULL, NULL, 0); |
2076 | #else |
2077 | /* can not support in nommu mode */ |
2078 | return -EINVAL; |
2079 | #endif |
2080 | } |
2081 | #endif |
2082 | |
2083 | #ifdef __ARCH_WANT_SYS_VFORK |
2084 | SYSCALL_DEFINE0(vfork) |
2085 | { |
2086 | return _do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, 0, |
2087 | 0, NULL, NULL, 0); |
2088 | } |
2089 | #endif |
2090 | |
2091 | #ifdef __ARCH_WANT_SYS_CLONE |
2092 | #ifdef CONFIG_CLONE_BACKWARDS |
2093 | SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, |
2094 | int __user *, parent_tidptr, |
2095 | unsigned long, tls, |
2096 | int __user *, child_tidptr) |
2097 | #elif defined(CONFIG_CLONE_BACKWARDS2) |
2098 | SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags, |
2099 | int __user *, parent_tidptr, |
2100 | int __user *, child_tidptr, |
2101 | unsigned long, tls) |
2102 | #elif defined(CONFIG_CLONE_BACKWARDS3) |
2103 | SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp, |
2104 | int, stack_size, |
2105 | int __user *, parent_tidptr, |
2106 | int __user *, child_tidptr, |
2107 | unsigned long, tls) |
2108 | #else |
2109 | SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp, |
2110 | int __user *, parent_tidptr, |
2111 | int __user *, child_tidptr, |
2112 | unsigned long, tls) |
2113 | #endif |
2114 | { |
2115 | return _do_fork(clone_flags, newsp, 0, parent_tidptr, child_tidptr, tls); |
2116 | } |
2117 | #endif |
2118 | |
2119 | #ifndef ARCH_MIN_MMSTRUCT_ALIGN |
2120 | #define ARCH_MIN_MMSTRUCT_ALIGN 0 |
2121 | #endif |
2122 | |
2123 | static void sighand_ctor(void *data) |
2124 | { |
2125 | struct sighand_struct *sighand = data; |
2126 | |
2127 | spin_lock_init(&sighand->siglock); |
2128 | init_waitqueue_head(&sighand->signalfd_wqh); |
2129 | } |
2130 | |
2131 | void __init proc_caches_init(void) |
2132 | { |
2133 | sighand_cachep = kmem_cache_create("sighand_cache", |
2134 | sizeof(struct sighand_struct), 0, |
2135 | SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU| |
2136 | SLAB_NOTRACK|SLAB_ACCOUNT, sighand_ctor); |
2137 | signal_cachep = kmem_cache_create("signal_cache", |
2138 | sizeof(struct signal_struct), 0, |
2139 | SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, |
2140 | NULL); |
2141 | files_cachep = kmem_cache_create("files_cache", |
2142 | sizeof(struct files_struct), 0, |
2143 | SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, |
2144 | NULL); |
2145 | fs_cachep = kmem_cache_create("fs_cache", |
2146 | sizeof(struct fs_struct), 0, |
2147 | SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, |
2148 | NULL); |
2149 | /* |
2150 | * FIXME! The "sizeof(struct mm_struct)" currently includes the |
2151 | * whole struct cpumask for the OFFSTACK case. We could change |
2152 | * this to *only* allocate as much of it as required by the |
2153 | * maximum number of CPU's we can ever have. The cpumask_allocation |
2154 | * is at the end of the structure, exactly for that reason. |
2155 | */ |
2156 | mm_cachep = kmem_cache_create("mm_struct", |
2157 | sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN, |
2158 | SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK|SLAB_ACCOUNT, |
2159 | NULL); |
2160 | vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC|SLAB_ACCOUNT); |
2161 | mmap_init(); |
2162 | nsproxy_cache_init(); |
2163 | } |
2164 | |
2165 | /* |
2166 | * Check constraints on flags passed to the unshare system call. |
2167 | */ |
2168 | static int check_unshare_flags(unsigned long unshare_flags) |
2169 | { |
2170 | if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND| |
2171 | CLONE_VM|CLONE_FILES|CLONE_SYSVSEM| |
2172 | CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET| |
2173 | CLONE_NEWUSER|CLONE_NEWPID|CLONE_NEWCGROUP)) |
2174 | return -EINVAL; |
2175 | /* |
2176 | * Not implemented, but pretend it works if there is nothing |
2177 | * to unshare. Note that unsharing the address space or the |
2178 | * signal handlers also need to unshare the signal queues (aka |
2179 | * CLONE_THREAD). |
2180 | */ |
2181 | if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) { |
2182 | if (!thread_group_empty(current)) |
2183 | return -EINVAL; |
2184 | } |
2185 | if (unshare_flags & (CLONE_SIGHAND | CLONE_VM)) { |
2186 | if (atomic_read(¤t->sighand->count) > 1) |
2187 | return -EINVAL; |
2188 | } |
2189 | if (unshare_flags & CLONE_VM) { |
2190 | if (!current_is_single_threaded()) |
2191 | return -EINVAL; |
2192 | } |
2193 | |
2194 | return 0; |
2195 | } |
2196 | |
2197 | /* |
2198 | * Unshare the filesystem structure if it is being shared |
2199 | */ |
2200 | static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp) |
2201 | { |
2202 | struct fs_struct *fs = current->fs; |
2203 | |
2204 | if (!(unshare_flags & CLONE_FS) || !fs) |
2205 | return 0; |
2206 | |
2207 | /* don't need lock here; in the worst case we'll do useless copy */ |
2208 | if (fs->users == 1) |
2209 | return 0; |
2210 | |
2211 | *new_fsp = copy_fs_struct(fs); |
2212 | if (!*new_fsp) |
2213 | return -ENOMEM; |
2214 | |
2215 | return 0; |
2216 | } |
2217 | |
2218 | /* |
2219 | * Unshare file descriptor table if it is being shared |
2220 | */ |
2221 | static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp) |
2222 | { |
2223 | struct files_struct *fd = current->files; |
2224 | int error = 0; |
2225 | |
2226 | if ((unshare_flags & CLONE_FILES) && |
2227 | (fd && atomic_read(&fd->count) > 1)) { |
2228 | *new_fdp = dup_fd(fd, &error); |
2229 | if (!*new_fdp) |
2230 | return error; |
2231 | } |
2232 | |
2233 | return 0; |
2234 | } |
2235 | |
2236 | /* |
2237 | * unshare allows a process to 'unshare' part of the process |
2238 | * context which was originally shared using clone. copy_* |
2239 | * functions used by do_fork() cannot be used here directly |
2240 | * because they modify an inactive task_struct that is being |
2241 | * constructed. Here we are modifying the current, active, |
2242 | * task_struct. |
2243 | */ |
2244 | SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags) |
2245 | { |
2246 | struct fs_struct *fs, *new_fs = NULL; |
2247 | struct files_struct *fd, *new_fd = NULL; |
2248 | struct cred *new_cred = NULL; |
2249 | struct nsproxy *new_nsproxy = NULL; |
2250 | int do_sysvsem = 0; |
2251 | int err; |
2252 | |
2253 | /* |
2254 | * If unsharing a user namespace must also unshare the thread group |
2255 | * and unshare the filesystem root and working directories. |
2256 | */ |
2257 | if (unshare_flags & CLONE_NEWUSER) |
2258 | unshare_flags |= CLONE_THREAD | CLONE_FS; |
2259 | /* |
2260 | * If unsharing vm, must also unshare signal handlers. |
2261 | */ |
2262 | if (unshare_flags & CLONE_VM) |
2263 | unshare_flags |= CLONE_SIGHAND; |
2264 | /* |
2265 | * If unsharing a signal handlers, must also unshare the signal queues. |
2266 | */ |
2267 | if (unshare_flags & CLONE_SIGHAND) |
2268 | unshare_flags |= CLONE_THREAD; |
2269 | /* |
2270 | * If unsharing namespace, must also unshare filesystem information. |
2271 | */ |
2272 | if (unshare_flags & CLONE_NEWNS) |
2273 | unshare_flags |= CLONE_FS; |
2274 | |
2275 | err = check_unshare_flags(unshare_flags); |
2276 | if (err) |
2277 | goto bad_unshare_out; |
2278 | /* |
2279 | * CLONE_NEWIPC must also detach from the undolist: after switching |
2280 | * to a new ipc namespace, the semaphore arrays from the old |
2281 | * namespace are unreachable. |
2282 | */ |
2283 | if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM)) |
2284 | do_sysvsem = 1; |
2285 | err = unshare_fs(unshare_flags, &new_fs); |
2286 | if (err) |
2287 | goto bad_unshare_out; |
2288 | err = unshare_fd(unshare_flags, &new_fd); |
2289 | if (err) |
2290 | goto bad_unshare_cleanup_fs; |
2291 | err = unshare_userns(unshare_flags, &new_cred); |
2292 | if (err) |
2293 | goto bad_unshare_cleanup_fd; |
2294 | err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy, |
2295 | new_cred, new_fs); |
2296 | if (err) |
2297 | goto bad_unshare_cleanup_cred; |
2298 | |
2299 | if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) { |
2300 | if (do_sysvsem) { |
2301 | /* |
2302 | * CLONE_SYSVSEM is equivalent to sys_exit(). |
2303 | */ |
2304 | exit_sem(current); |
2305 | } |
2306 | if (unshare_flags & CLONE_NEWIPC) { |
2307 | /* Orphan segments in old ns (see sem above). */ |
2308 | exit_shm(current); |
2309 | shm_init_task(current); |
2310 | } |
2311 | |
2312 | if (new_nsproxy) |
2313 | switch_task_namespaces(current, new_nsproxy); |
2314 | |
2315 | task_lock(current); |
2316 | |
2317 | if (new_fs) { |
2318 | fs = current->fs; |
2319 | spin_lock(&fs->lock); |
2320 | current->fs = new_fs; |
2321 | if (--fs->users) |
2322 | new_fs = NULL; |
2323 | else |
2324 | new_fs = fs; |
2325 | spin_unlock(&fs->lock); |
2326 | } |
2327 | |
2328 | if (new_fd) { |
2329 | fd = current->files; |
2330 | current->files = new_fd; |
2331 | new_fd = fd; |
2332 | } |
2333 | |
2334 | task_unlock(current); |
2335 | |
2336 | if (new_cred) { |
2337 | /* Install the new user namespace */ |
2338 | commit_creds(new_cred); |
2339 | new_cred = NULL; |
2340 | } |
2341 | } |
2342 | |
2343 | bad_unshare_cleanup_cred: |
2344 | if (new_cred) |
2345 | put_cred(new_cred); |
2346 | bad_unshare_cleanup_fd: |
2347 | if (new_fd) |
2348 | put_files_struct(new_fd); |
2349 | |
2350 | bad_unshare_cleanup_fs: |
2351 | if (new_fs) |
2352 | free_fs_struct(new_fs); |
2353 | |
2354 | bad_unshare_out: |
2355 | return err; |
2356 | } |
2357 | |
2358 | /* |
2359 | * Helper to unshare the files of the current task. |
2360 | * We don't want to expose copy_files internals to |
2361 | * the exec layer of the kernel. |
2362 | */ |
2363 | |
2364 | int unshare_files(struct files_struct **displaced) |
2365 | { |
2366 | struct task_struct *task = current; |
2367 | struct files_struct *copy = NULL; |
2368 | int error; |
2369 | |
2370 | error = unshare_fd(CLONE_FILES, ©); |
2371 | if (error || !copy) { |
2372 | *displaced = NULL; |
2373 | return error; |
2374 | } |
2375 | *displaced = task->files; |
2376 | task_lock(task); |
2377 | task->files = copy; |
2378 | task_unlock(task); |
2379 | return 0; |
2380 | } |
2381 | |
2382 | int sysctl_max_threads(struct ctl_table *table, int write, |
2383 | void __user *buffer, size_t *lenp, loff_t *ppos) |
2384 | { |
2385 | struct ctl_table t; |
2386 | int ret; |
2387 | int threads = max_threads; |
2388 | int min = MIN_THREADS; |
2389 | int max = MAX_THREADS; |
2390 | |
2391 | t = *table; |
2392 | t.data = &threads; |
2393 | t.extra1 = &min; |
2394 | t.extra2 = &max; |
2395 | |
2396 | ret = proc_dointvec_minmax(&t, write, buffer, lenp, ppos); |
2397 | if (ret || !write) |
2398 | return ret; |
2399 | |
2400 | set_max_threads(threads); |
2401 | |
2402 | return 0; |
2403 | } |
2404 |