]> rtime.felk.cvut.cz Git - zynq/linux.git/blob - kernel/fork.c
Merge branch '4.0.8-rt6'
[zynq/linux.git] / kernel / fork.c
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/delayacct.h>
62 #include <linux/taskstats_kern.h>
63 #include <linux/random.h>
64 #include <linux/tty.h>
65 #include <linux/blkdev.h>
66 #include <linux/fs_struct.h>
67 #include <linux/magic.h>
68 #include <linux/perf_event.h>
69 #include <linux/posix-timers.h>
70 #include <linux/user-return-notifier.h>
71 #include <linux/oom.h>
72 #include <linux/khugepaged.h>
73 #include <linux/signalfd.h>
74 #include <linux/uprobes.h>
75 #include <linux/aio.h>
76 #include <linux/compiler.h>
77
78 #include <asm/pgtable.h>
79 #include <asm/pgalloc.h>
80 #include <asm/uaccess.h>
81 #include <asm/mmu_context.h>
82 #include <asm/cacheflush.h>
83 #include <asm/tlbflush.h>
84
85 #include <trace/events/sched.h>
86
87 #define CREATE_TRACE_POINTS
88 #include <trace/events/task.h>
89
90 /*
91  * Protected counters by write_lock_irq(&tasklist_lock)
92  */
93 unsigned long total_forks;      /* Handle normal Linux uptimes. */
94 int nr_threads;                 /* The idle threads do not count.. */
95
96 int max_threads;                /* tunable limit on nr_threads */
97
98 DEFINE_PER_CPU(unsigned long, process_counts) = 0;
99
100 DEFINE_RWLOCK(tasklist_lock);  /* outer */
101
102 #ifdef CONFIG_PROVE_RCU
103 int lockdep_tasklist_lock_is_held(void)
104 {
105         return lockdep_is_held(&tasklist_lock);
106 }
107 EXPORT_SYMBOL_GPL(lockdep_tasklist_lock_is_held);
108 #endif /* #ifdef CONFIG_PROVE_RCU */
109
110 int nr_processes(void)
111 {
112         int cpu;
113         int total = 0;
114
115         for_each_possible_cpu(cpu)
116                 total += per_cpu(process_counts, cpu);
117
118         return total;
119 }
120
121 void __weak arch_release_task_struct(struct task_struct *tsk)
122 {
123 }
124
125 #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
126 static struct kmem_cache *task_struct_cachep;
127
128 static inline struct task_struct *alloc_task_struct_node(int node)
129 {
130         return kmem_cache_alloc_node(task_struct_cachep, GFP_KERNEL, node);
131 }
132
133 static inline void free_task_struct(struct task_struct *tsk)
134 {
135         kmem_cache_free(task_struct_cachep, tsk);
136 }
137 #endif
138
139 void __weak arch_release_thread_info(struct thread_info *ti)
140 {
141 }
142
143 #ifndef CONFIG_ARCH_THREAD_INFO_ALLOCATOR
144
145 /*
146  * Allocate pages if THREAD_SIZE is >= PAGE_SIZE, otherwise use a
147  * kmemcache based allocator.
148  */
149 # if THREAD_SIZE >= PAGE_SIZE
150 static struct thread_info *alloc_thread_info_node(struct task_struct *tsk,
151                                                   int node)
152 {
153         struct page *page = alloc_kmem_pages_node(node, THREADINFO_GFP,
154                                                   THREAD_SIZE_ORDER);
155
156         return page ? page_address(page) : NULL;
157 }
158
159 static inline void free_thread_info(struct thread_info *ti)
160 {
161         free_kmem_pages((unsigned long)ti, THREAD_SIZE_ORDER);
162 }
163 # else
164 static struct kmem_cache *thread_info_cache;
165
166 static struct thread_info *alloc_thread_info_node(struct task_struct *tsk,
167                                                   int node)
168 {
169         return kmem_cache_alloc_node(thread_info_cache, THREADINFO_GFP, node);
170 }
171
172 static void free_thread_info(struct thread_info *ti)
173 {
174         kmem_cache_free(thread_info_cache, ti);
175 }
176
177 void thread_info_cache_init(void)
178 {
179         thread_info_cache = kmem_cache_create("thread_info", THREAD_SIZE,
180                                               THREAD_SIZE, 0, NULL);
181         BUG_ON(thread_info_cache == NULL);
182 }
183 # endif
184 #endif
185
186 /* SLAB cache for signal_struct structures (tsk->signal) */
187 static struct kmem_cache *signal_cachep;
188
189 /* SLAB cache for sighand_struct structures (tsk->sighand) */
190 struct kmem_cache *sighand_cachep;
191
192 /* SLAB cache for files_struct structures (tsk->files) */
193 struct kmem_cache *files_cachep;
194
195 /* SLAB cache for fs_struct structures (tsk->fs) */
196 struct kmem_cache *fs_cachep;
197
198 /* SLAB cache for vm_area_struct structures */
199 struct kmem_cache *vm_area_cachep;
200
201 /* SLAB cache for mm_struct structures (tsk->mm) */
202 static struct kmem_cache *mm_cachep;
203
204 static void account_kernel_stack(struct thread_info *ti, int account)
205 {
206         struct zone *zone = page_zone(virt_to_page(ti));
207
208         mod_zone_page_state(zone, NR_KERNEL_STACK, account);
209 }
210
211 void free_task(struct task_struct *tsk)
212 {
213         account_kernel_stack(tsk->stack, -1);
214         arch_release_thread_info(tsk->stack);
215         free_thread_info(tsk->stack);
216         rt_mutex_debug_task_free(tsk);
217         ftrace_graph_exit_task(tsk);
218         put_seccomp_filter(tsk);
219         arch_release_task_struct(tsk);
220         free_task_struct(tsk);
221 }
222 EXPORT_SYMBOL(free_task);
223
224 static inline void free_signal_struct(struct signal_struct *sig)
225 {
226         taskstats_tgid_free(sig);
227         sched_autogroup_exit(sig);
228         kmem_cache_free(signal_cachep, sig);
229 }
230
231 static inline void put_signal_struct(struct signal_struct *sig)
232 {
233         if (atomic_dec_and_test(&sig->sigcnt))
234                 free_signal_struct(sig);
235 }
236 #ifdef CONFIG_PREEMPT_RT_BASE
237 static
238 #endif
239 void __put_task_struct(struct task_struct *tsk)
240 {
241         WARN_ON(!tsk->exit_state);
242         WARN_ON(atomic_read(&tsk->usage));
243         WARN_ON(tsk == current);
244
245         task_numa_free(tsk);
246         security_task_free(tsk);
247         exit_creds(tsk);
248         delayacct_tsk_free(tsk);
249         put_signal_struct(tsk->signal);
250
251         if (!profile_handoff_task(tsk))
252                 free_task(tsk);
253 }
254 #ifndef CONFIG_PREEMPT_RT_BASE
255 EXPORT_SYMBOL_GPL(__put_task_struct);
256 #else
257 void __put_task_struct_cb(struct rcu_head *rhp)
258 {
259         struct task_struct *tsk = container_of(rhp, struct task_struct, put_rcu);
260
261         __put_task_struct(tsk);
262
263 }
264 EXPORT_SYMBOL_GPL(__put_task_struct_cb);
265 #endif
266
267 void __init __weak arch_task_cache_init(void) { }
268
269 void __init fork_init(unsigned long mempages)
270 {
271 #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
272 #ifndef ARCH_MIN_TASKALIGN
273 #define ARCH_MIN_TASKALIGN      L1_CACHE_BYTES
274 #endif
275         /* create a slab on which task_structs can be allocated */
276         task_struct_cachep =
277                 kmem_cache_create("task_struct", sizeof(struct task_struct),
278                         ARCH_MIN_TASKALIGN, SLAB_PANIC | SLAB_NOTRACK, NULL);
279 #endif
280
281         /* do the arch specific task caches init */
282         arch_task_cache_init();
283
284         /*
285          * The default maximum number of threads is set to a safe
286          * value: the thread structures can take up at most half
287          * of memory.
288          */
289         max_threads = mempages / (8 * THREAD_SIZE / PAGE_SIZE);
290
291         /*
292          * we need to allow at least 20 threads to boot a system
293          */
294         if (max_threads < 20)
295                 max_threads = 20;
296
297         init_task.signal->rlim[RLIMIT_NPROC].rlim_cur = max_threads/2;
298         init_task.signal->rlim[RLIMIT_NPROC].rlim_max = max_threads/2;
299         init_task.signal->rlim[RLIMIT_SIGPENDING] =
300                 init_task.signal->rlim[RLIMIT_NPROC];
301 }
302
303 int __weak arch_dup_task_struct(struct task_struct *dst,
304                                                struct task_struct *src)
305 {
306         *dst = *src;
307         return 0;
308 }
309
310 void set_task_stack_end_magic(struct task_struct *tsk)
311 {
312         unsigned long *stackend;
313
314         stackend = end_of_stack(tsk);
315         *stackend = STACK_END_MAGIC;    /* for overflow detection */
316 }
317
318 static struct task_struct *dup_task_struct(struct task_struct *orig)
319 {
320         struct task_struct *tsk;
321         struct thread_info *ti;
322         int node = tsk_fork_get_node(orig);
323         int err;
324
325         tsk = alloc_task_struct_node(node);
326         if (!tsk)
327                 return NULL;
328
329         ti = alloc_thread_info_node(tsk, node);
330         if (!ti)
331                 goto free_tsk;
332
333         err = arch_dup_task_struct(tsk, orig);
334         if (err)
335                 goto free_ti;
336
337         tsk->stack = ti;
338 #ifdef CONFIG_SECCOMP
339         /*
340          * We must handle setting up seccomp filters once we're under
341          * the sighand lock in case orig has changed between now and
342          * then. Until then, filter must be NULL to avoid messing up
343          * the usage counts on the error path calling free_task.
344          */
345         tsk->seccomp.filter = NULL;
346 #endif
347
348         setup_thread_stack(tsk, orig);
349         clear_user_return_notifier(tsk);
350         clear_tsk_need_resched(tsk);
351         set_task_stack_end_magic(tsk);
352
353 #ifdef CONFIG_CC_STACKPROTECTOR
354         tsk->stack_canary = get_random_int();
355 #endif
356
357         /*
358          * One for us, one for whoever does the "release_task()" (usually
359          * parent)
360          */
361         atomic_set(&tsk->usage, 2);
362 #ifdef CONFIG_BLK_DEV_IO_TRACE
363         tsk->btrace_seq = 0;
364 #endif
365         tsk->splice_pipe = NULL;
366         tsk->task_frag.page = NULL;
367
368         account_kernel_stack(ti, 1);
369
370         return tsk;
371
372 free_ti:
373         free_thread_info(ti);
374 free_tsk:
375         free_task_struct(tsk);
376         return NULL;
377 }
378
379 #ifdef CONFIG_MMU
380 static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
381 {
382         struct vm_area_struct *mpnt, *tmp, *prev, **pprev;
383         struct rb_node **rb_link, *rb_parent;
384         int retval;
385         unsigned long charge;
386
387         uprobe_start_dup_mmap();
388         down_write(&oldmm->mmap_sem);
389         flush_cache_dup_mm(oldmm);
390         uprobe_dup_mmap(oldmm, mm);
391         /*
392          * Not linked in yet - no deadlock potential:
393          */
394         down_write_nested(&mm->mmap_sem, SINGLE_DEPTH_NESTING);
395
396         mm->total_vm = oldmm->total_vm;
397         mm->shared_vm = oldmm->shared_vm;
398         mm->exec_vm = oldmm->exec_vm;
399         mm->stack_vm = oldmm->stack_vm;
400
401         rb_link = &mm->mm_rb.rb_node;
402         rb_parent = NULL;
403         pprev = &mm->mmap;
404         retval = ksm_fork(mm, oldmm);
405         if (retval)
406                 goto out;
407         retval = khugepaged_fork(mm, oldmm);
408         if (retval)
409                 goto out;
410
411         prev = NULL;
412         for (mpnt = oldmm->mmap; mpnt; mpnt = mpnt->vm_next) {
413                 struct file *file;
414
415                 if (mpnt->vm_flags & VM_DONTCOPY) {
416                         vm_stat_account(mm, mpnt->vm_flags, mpnt->vm_file,
417                                                         -vma_pages(mpnt));
418                         continue;
419                 }
420                 charge = 0;
421                 if (mpnt->vm_flags & VM_ACCOUNT) {
422                         unsigned long len = vma_pages(mpnt);
423
424                         if (security_vm_enough_memory_mm(oldmm, len)) /* sic */
425                                 goto fail_nomem;
426                         charge = len;
427                 }
428                 tmp = kmem_cache_alloc(vm_area_cachep, GFP_KERNEL);
429                 if (!tmp)
430                         goto fail_nomem;
431                 *tmp = *mpnt;
432                 INIT_LIST_HEAD(&tmp->anon_vma_chain);
433                 retval = vma_dup_policy(mpnt, tmp);
434                 if (retval)
435                         goto fail_nomem_policy;
436                 tmp->vm_mm = mm;
437                 if (anon_vma_fork(tmp, mpnt))
438                         goto fail_nomem_anon_vma_fork;
439                 tmp->vm_flags &= ~VM_LOCKED;
440                 tmp->vm_next = tmp->vm_prev = NULL;
441                 file = tmp->vm_file;
442                 if (file) {
443                         struct inode *inode = file_inode(file);
444                         struct address_space *mapping = file->f_mapping;
445
446                         get_file(file);
447                         if (tmp->vm_flags & VM_DENYWRITE)
448                                 atomic_dec(&inode->i_writecount);
449                         i_mmap_lock_write(mapping);
450                         if (tmp->vm_flags & VM_SHARED)
451                                 atomic_inc(&mapping->i_mmap_writable);
452                         flush_dcache_mmap_lock(mapping);
453                         /* insert tmp into the share list, just after mpnt */
454                         vma_interval_tree_insert_after(tmp, mpnt,
455                                         &mapping->i_mmap);
456                         flush_dcache_mmap_unlock(mapping);
457                         i_mmap_unlock_write(mapping);
458                 }
459
460                 /*
461                  * Clear hugetlb-related page reserves for children. This only
462                  * affects MAP_PRIVATE mappings. Faults generated by the child
463                  * are not guaranteed to succeed, even if read-only
464                  */
465                 if (is_vm_hugetlb_page(tmp))
466                         reset_vma_resv_huge_pages(tmp);
467
468                 /*
469                  * Link in the new vma and copy the page table entries.
470                  */
471                 *pprev = tmp;
472                 pprev = &tmp->vm_next;
473                 tmp->vm_prev = prev;
474                 prev = tmp;
475
476                 __vma_link_rb(mm, tmp, rb_link, rb_parent);
477                 rb_link = &tmp->vm_rb.rb_right;
478                 rb_parent = &tmp->vm_rb;
479
480                 mm->map_count++;
481                 retval = copy_page_range(mm, oldmm, mpnt);
482
483                 if (tmp->vm_ops && tmp->vm_ops->open)
484                         tmp->vm_ops->open(tmp);
485
486                 if (retval)
487                         goto out;
488         }
489         /* a new mm has just been created */
490         arch_dup_mmap(oldmm, mm);
491         retval = 0;
492 out:
493         up_write(&mm->mmap_sem);
494         flush_tlb_mm(oldmm);
495         up_write(&oldmm->mmap_sem);
496         uprobe_end_dup_mmap();
497         return retval;
498 fail_nomem_anon_vma_fork:
499         mpol_put(vma_policy(tmp));
500 fail_nomem_policy:
501         kmem_cache_free(vm_area_cachep, tmp);
502 fail_nomem:
503         retval = -ENOMEM;
504         vm_unacct_memory(charge);
505         goto out;
506 }
507
508 static inline int mm_alloc_pgd(struct mm_struct *mm)
509 {
510         mm->pgd = pgd_alloc(mm);
511         if (unlikely(!mm->pgd))
512                 return -ENOMEM;
513         return 0;
514 }
515
516 static inline void mm_free_pgd(struct mm_struct *mm)
517 {
518         pgd_free(mm, mm->pgd);
519 }
520 #else
521 #define dup_mmap(mm, oldmm)     (0)
522 #define mm_alloc_pgd(mm)        (0)
523 #define mm_free_pgd(mm)
524 #endif /* CONFIG_MMU */
525
526 __cacheline_aligned_in_smp DEFINE_SPINLOCK(mmlist_lock);
527
528 #define allocate_mm()   (kmem_cache_alloc(mm_cachep, GFP_KERNEL))
529 #define free_mm(mm)     (kmem_cache_free(mm_cachep, (mm)))
530
531 static unsigned long default_dump_filter = MMF_DUMP_FILTER_DEFAULT;
532
533 static int __init coredump_filter_setup(char *s)
534 {
535         default_dump_filter =
536                 (simple_strtoul(s, NULL, 0) << MMF_DUMP_FILTER_SHIFT) &
537                 MMF_DUMP_FILTER_MASK;
538         return 1;
539 }
540
541 __setup("coredump_filter=", coredump_filter_setup);
542
543 #include <linux/init_task.h>
544
545 static void mm_init_aio(struct mm_struct *mm)
546 {
547 #ifdef CONFIG_AIO
548         spin_lock_init(&mm->ioctx_lock);
549         mm->ioctx_table = NULL;
550 #endif
551 }
552
553 static void mm_init_owner(struct mm_struct *mm, struct task_struct *p)
554 {
555 #ifdef CONFIG_MEMCG
556         mm->owner = p;
557 #endif
558 }
559
560 static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p)
561 {
562         mm->mmap = NULL;
563         mm->mm_rb = RB_ROOT;
564         mm->vmacache_seqnum = 0;
565         atomic_set(&mm->mm_users, 1);
566         atomic_set(&mm->mm_count, 1);
567         init_rwsem(&mm->mmap_sem);
568         INIT_LIST_HEAD(&mm->mmlist);
569         mm->core_state = NULL;
570         atomic_long_set(&mm->nr_ptes, 0);
571         mm_nr_pmds_init(mm);
572         mm->map_count = 0;
573         mm->locked_vm = 0;
574         mm->pinned_vm = 0;
575         memset(&mm->rss_stat, 0, sizeof(mm->rss_stat));
576         spin_lock_init(&mm->page_table_lock);
577         mm_init_cpumask(mm);
578         mm_init_aio(mm);
579         mm_init_owner(mm, p);
580         mmu_notifier_mm_init(mm);
581         clear_tlb_flush_pending(mm);
582 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
583         mm->pmd_huge_pte = NULL;
584 #endif
585
586         if (current->mm) {
587                 mm->flags = current->mm->flags & MMF_INIT_MASK;
588                 mm->def_flags = current->mm->def_flags & VM_INIT_DEF_MASK;
589         } else {
590                 mm->flags = default_dump_filter;
591                 mm->def_flags = 0;
592         }
593
594         if (mm_alloc_pgd(mm))
595                 goto fail_nopgd;
596
597         if (init_new_context(p, mm))
598                 goto fail_nocontext;
599
600         return mm;
601
602 fail_nocontext:
603         mm_free_pgd(mm);
604 fail_nopgd:
605         free_mm(mm);
606         return NULL;
607 }
608
609 static void check_mm(struct mm_struct *mm)
610 {
611         int i;
612
613         for (i = 0; i < NR_MM_COUNTERS; i++) {
614                 long x = atomic_long_read(&mm->rss_stat.count[i]);
615
616                 if (unlikely(x))
617                         printk(KERN_ALERT "BUG: Bad rss-counter state "
618                                           "mm:%p idx:%d val:%ld\n", mm, i, x);
619         }
620
621         if (atomic_long_read(&mm->nr_ptes))
622                 pr_alert("BUG: non-zero nr_ptes on freeing mm: %ld\n",
623                                 atomic_long_read(&mm->nr_ptes));
624         if (mm_nr_pmds(mm))
625                 pr_alert("BUG: non-zero nr_pmds on freeing mm: %ld\n",
626                                 mm_nr_pmds(mm));
627
628 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
629         VM_BUG_ON_MM(mm->pmd_huge_pte, mm);
630 #endif
631 }
632
633 /*
634  * Allocate and initialize an mm_struct.
635  */
636 struct mm_struct *mm_alloc(void)
637 {
638         struct mm_struct *mm;
639
640         mm = allocate_mm();
641         if (!mm)
642                 return NULL;
643
644         memset(mm, 0, sizeof(*mm));
645         return mm_init(mm, current);
646 }
647
648 /*
649  * Called when the last reference to the mm
650  * is dropped: either by a lazy thread or by
651  * mmput. Free the page directory and the mm.
652  */
653 void __mmdrop(struct mm_struct *mm)
654 {
655         BUG_ON(mm == &init_mm);
656         mm_free_pgd(mm);
657         destroy_context(mm);
658         mmu_notifier_mm_destroy(mm);
659         check_mm(mm);
660         free_mm(mm);
661 }
662 EXPORT_SYMBOL_GPL(__mmdrop);
663
664 #ifdef CONFIG_PREEMPT_RT_BASE
665 /*
666  * RCU callback for delayed mm drop. Not strictly rcu, but we don't
667  * want another facility to make this work.
668  */
669 void __mmdrop_delayed(struct rcu_head *rhp)
670 {
671         struct mm_struct *mm = container_of(rhp, struct mm_struct, delayed_drop);
672
673         __mmdrop(mm);
674 }
675 #endif
676
677 /*
678  * Decrement the use count and release all resources for an mm.
679  */
680 void mmput(struct mm_struct *mm)
681 {
682         might_sleep();
683
684         if (atomic_dec_and_test(&mm->mm_users)) {
685                 uprobe_clear_state(mm);
686                 exit_aio(mm);
687                 ksm_exit(mm);
688                 khugepaged_exit(mm); /* must run before exit_mmap */
689                 exit_mmap(mm);
690                 set_mm_exe_file(mm, NULL);
691                 if (!list_empty(&mm->mmlist)) {
692                         spin_lock(&mmlist_lock);
693                         list_del(&mm->mmlist);
694                         spin_unlock(&mmlist_lock);
695                 }
696                 if (mm->binfmt)
697                         module_put(mm->binfmt->module);
698                 mmdrop(mm);
699         }
700 }
701 EXPORT_SYMBOL_GPL(mmput);
702
703 void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
704 {
705         if (new_exe_file)
706                 get_file(new_exe_file);
707         if (mm->exe_file)
708                 fput(mm->exe_file);
709         mm->exe_file = new_exe_file;
710 }
711
712 struct file *get_mm_exe_file(struct mm_struct *mm)
713 {
714         struct file *exe_file;
715
716         /* We need mmap_sem to protect against races with removal of exe_file */
717         down_read(&mm->mmap_sem);
718         exe_file = mm->exe_file;
719         if (exe_file)
720                 get_file(exe_file);
721         up_read(&mm->mmap_sem);
722         return exe_file;
723 }
724
725 static void dup_mm_exe_file(struct mm_struct *oldmm, struct mm_struct *newmm)
726 {
727         /* It's safe to write the exe_file pointer without exe_file_lock because
728          * this is called during fork when the task is not yet in /proc */
729         newmm->exe_file = get_mm_exe_file(oldmm);
730 }
731
732 /**
733  * get_task_mm - acquire a reference to the task's mm
734  *
735  * Returns %NULL if the task has no mm.  Checks PF_KTHREAD (meaning
736  * this kernel workthread has transiently adopted a user mm with use_mm,
737  * to do its AIO) is not set and if so returns a reference to it, after
738  * bumping up the use count.  User must release the mm via mmput()
739  * after use.  Typically used by /proc and ptrace.
740  */
741 struct mm_struct *get_task_mm(struct task_struct *task)
742 {
743         struct mm_struct *mm;
744
745         task_lock(task);
746         mm = task->mm;
747         if (mm) {
748                 if (task->flags & PF_KTHREAD)
749                         mm = NULL;
750                 else
751                         atomic_inc(&mm->mm_users);
752         }
753         task_unlock(task);
754         return mm;
755 }
756 EXPORT_SYMBOL_GPL(get_task_mm);
757
758 struct mm_struct *mm_access(struct task_struct *task, unsigned int mode)
759 {
760         struct mm_struct *mm;
761         int err;
762
763         err =  mutex_lock_killable(&task->signal->cred_guard_mutex);
764         if (err)
765                 return ERR_PTR(err);
766
767         mm = get_task_mm(task);
768         if (mm && mm != current->mm &&
769                         !ptrace_may_access(task, mode)) {
770                 mmput(mm);
771                 mm = ERR_PTR(-EACCES);
772         }
773         mutex_unlock(&task->signal->cred_guard_mutex);
774
775         return mm;
776 }
777
778 static void complete_vfork_done(struct task_struct *tsk)
779 {
780         struct completion *vfork;
781
782         task_lock(tsk);
783         vfork = tsk->vfork_done;
784         if (likely(vfork)) {
785                 tsk->vfork_done = NULL;
786                 complete(vfork);
787         }
788         task_unlock(tsk);
789 }
790
791 static int wait_for_vfork_done(struct task_struct *child,
792                                 struct completion *vfork)
793 {
794         int killed;
795
796         freezer_do_not_count();
797         killed = wait_for_completion_killable(vfork);
798         freezer_count();
799
800         if (killed) {
801                 task_lock(child);
802                 child->vfork_done = NULL;
803                 task_unlock(child);
804         }
805
806         put_task_struct(child);
807         return killed;
808 }
809
810 /* Please note the differences between mmput and mm_release.
811  * mmput is called whenever we stop holding onto a mm_struct,
812  * error success whatever.
813  *
814  * mm_release is called after a mm_struct has been removed
815  * from the current process.
816  *
817  * This difference is important for error handling, when we
818  * only half set up a mm_struct for a new process and need to restore
819  * the old one.  Because we mmput the new mm_struct before
820  * restoring the old one. . .
821  * Eric Biederman 10 January 1998
822  */
823 void mm_release(struct task_struct *tsk, struct mm_struct *mm)
824 {
825         /* Get rid of any futexes when releasing the mm */
826 #ifdef CONFIG_FUTEX
827         if (unlikely(tsk->robust_list)) {
828                 exit_robust_list(tsk);
829                 tsk->robust_list = NULL;
830         }
831 #ifdef CONFIG_COMPAT
832         if (unlikely(tsk->compat_robust_list)) {
833                 compat_exit_robust_list(tsk);
834                 tsk->compat_robust_list = NULL;
835         }
836 #endif
837         if (unlikely(!list_empty(&tsk->pi_state_list)))
838                 exit_pi_state_list(tsk);
839 #endif
840
841         uprobe_free_utask(tsk);
842
843         /* Get rid of any cached register state */
844         deactivate_mm(tsk, mm);
845
846         /*
847          * If we're exiting normally, clear a user-space tid field if
848          * requested.  We leave this alone when dying by signal, to leave
849          * the value intact in a core dump, and to save the unnecessary
850          * trouble, say, a killed vfork parent shouldn't touch this mm.
851          * Userland only wants this done for a sys_exit.
852          */
853         if (tsk->clear_child_tid) {
854                 if (!(tsk->flags & PF_SIGNALED) &&
855                     atomic_read(&mm->mm_users) > 1) {
856                         /*
857                          * We don't check the error code - if userspace has
858                          * not set up a proper pointer then tough luck.
859                          */
860                         put_user(0, tsk->clear_child_tid);
861                         sys_futex(tsk->clear_child_tid, FUTEX_WAKE,
862                                         1, NULL, NULL, 0);
863                 }
864                 tsk->clear_child_tid = NULL;
865         }
866
867         /*
868          * All done, finally we can wake up parent and return this mm to him.
869          * Also kthread_stop() uses this completion for synchronization.
870          */
871         if (tsk->vfork_done)
872                 complete_vfork_done(tsk);
873 }
874
875 /*
876  * Allocate a new mm structure and copy contents from the
877  * mm structure of the passed in task structure.
878  */
879 static struct mm_struct *dup_mm(struct task_struct *tsk)
880 {
881         struct mm_struct *mm, *oldmm = current->mm;
882         int err;
883
884         mm = allocate_mm();
885         if (!mm)
886                 goto fail_nomem;
887
888         memcpy(mm, oldmm, sizeof(*mm));
889
890         if (!mm_init(mm, tsk))
891                 goto fail_nomem;
892
893         dup_mm_exe_file(oldmm, mm);
894
895         err = dup_mmap(mm, oldmm);
896         if (err)
897                 goto free_pt;
898
899         mm->hiwater_rss = get_mm_rss(mm);
900         mm->hiwater_vm = mm->total_vm;
901
902         if (mm->binfmt && !try_module_get(mm->binfmt->module))
903                 goto free_pt;
904
905         return mm;
906
907 free_pt:
908         /* don't put binfmt in mmput, we haven't got module yet */
909         mm->binfmt = NULL;
910         mmput(mm);
911
912 fail_nomem:
913         return NULL;
914 }
915
916 static int copy_mm(unsigned long clone_flags, struct task_struct *tsk)
917 {
918         struct mm_struct *mm, *oldmm;
919         int retval;
920
921         tsk->min_flt = tsk->maj_flt = 0;
922         tsk->nvcsw = tsk->nivcsw = 0;
923 #ifdef CONFIG_DETECT_HUNG_TASK
924         tsk->last_switch_count = tsk->nvcsw + tsk->nivcsw;
925 #endif
926
927         tsk->mm = NULL;
928         tsk->active_mm = NULL;
929
930         /*
931          * Are we cloning a kernel thread?
932          *
933          * We need to steal a active VM for that..
934          */
935         oldmm = current->mm;
936         if (!oldmm)
937                 return 0;
938
939         /* initialize the new vmacache entries */
940         vmacache_flush(tsk);
941
942         if (clone_flags & CLONE_VM) {
943                 atomic_inc(&oldmm->mm_users);
944                 mm = oldmm;
945                 goto good_mm;
946         }
947
948         retval = -ENOMEM;
949         mm = dup_mm(tsk);
950         if (!mm)
951                 goto fail_nomem;
952
953 good_mm:
954         tsk->mm = mm;
955         tsk->active_mm = mm;
956         return 0;
957
958 fail_nomem:
959         return retval;
960 }
961
962 static int copy_fs(unsigned long clone_flags, struct task_struct *tsk)
963 {
964         struct fs_struct *fs = current->fs;
965         if (clone_flags & CLONE_FS) {
966                 /* tsk->fs is already what we want */
967                 spin_lock(&fs->lock);
968                 if (fs->in_exec) {
969                         spin_unlock(&fs->lock);
970                         return -EAGAIN;
971                 }
972                 fs->users++;
973                 spin_unlock(&fs->lock);
974                 return 0;
975         }
976         tsk->fs = copy_fs_struct(fs);
977         if (!tsk->fs)
978                 return -ENOMEM;
979         return 0;
980 }
981
982 static int copy_files(unsigned long clone_flags, struct task_struct *tsk)
983 {
984         struct files_struct *oldf, *newf;
985         int error = 0;
986
987         /*
988          * A background process may not have any files ...
989          */
990         oldf = current->files;
991         if (!oldf)
992                 goto out;
993
994         if (clone_flags & CLONE_FILES) {
995                 atomic_inc(&oldf->count);
996                 goto out;
997         }
998
999         newf = dup_fd(oldf, &error);
1000         if (!newf)
1001                 goto out;
1002
1003         tsk->files = newf;
1004         error = 0;
1005 out:
1006         return error;
1007 }
1008
1009 static int copy_io(unsigned long clone_flags, struct task_struct *tsk)
1010 {
1011 #ifdef CONFIG_BLOCK
1012         struct io_context *ioc = current->io_context;
1013         struct io_context *new_ioc;
1014
1015         if (!ioc)
1016                 return 0;
1017         /*
1018          * Share io context with parent, if CLONE_IO is set
1019          */
1020         if (clone_flags & CLONE_IO) {
1021                 ioc_task_link(ioc);
1022                 tsk->io_context = ioc;
1023         } else if (ioprio_valid(ioc->ioprio)) {
1024                 new_ioc = get_task_io_context(tsk, GFP_KERNEL, NUMA_NO_NODE);
1025                 if (unlikely(!new_ioc))
1026                         return -ENOMEM;
1027
1028                 new_ioc->ioprio = ioc->ioprio;
1029                 put_io_context(new_ioc);
1030         }
1031 #endif
1032         return 0;
1033 }
1034
1035 static int copy_sighand(unsigned long clone_flags, struct task_struct *tsk)
1036 {
1037         struct sighand_struct *sig;
1038
1039         if (clone_flags & CLONE_SIGHAND) {
1040                 atomic_inc(&current->sighand->count);
1041                 return 0;
1042         }
1043         sig = kmem_cache_alloc(sighand_cachep, GFP_KERNEL);
1044         rcu_assign_pointer(tsk->sighand, sig);
1045         if (!sig)
1046                 return -ENOMEM;
1047         atomic_set(&sig->count, 1);
1048         memcpy(sig->action, current->sighand->action, sizeof(sig->action));
1049         return 0;
1050 }
1051
1052 void __cleanup_sighand(struct sighand_struct *sighand)
1053 {
1054         if (atomic_dec_and_test(&sighand->count)) {
1055                 signalfd_cleanup(sighand);
1056                 /*
1057                  * sighand_cachep is SLAB_DESTROY_BY_RCU so we can free it
1058                  * without an RCU grace period, see __lock_task_sighand().
1059                  */
1060                 kmem_cache_free(sighand_cachep, sighand);
1061         }
1062 }
1063
1064 /*
1065  * Initialize POSIX timer handling for a thread group.
1066  */
1067 static void posix_cpu_timers_init_group(struct signal_struct *sig)
1068 {
1069         unsigned long cpu_limit;
1070
1071         /* Thread group counters. */
1072         thread_group_cputime_init(sig);
1073
1074         cpu_limit = ACCESS_ONCE(sig->rlim[RLIMIT_CPU].rlim_cur);
1075         if (cpu_limit != RLIM_INFINITY) {
1076                 sig->cputime_expires.prof_exp = secs_to_cputime(cpu_limit);
1077                 sig->cputimer.running = 1;
1078         }
1079
1080         /* The timer lists. */
1081         INIT_LIST_HEAD(&sig->cpu_timers[0]);
1082         INIT_LIST_HEAD(&sig->cpu_timers[1]);
1083         INIT_LIST_HEAD(&sig->cpu_timers[2]);
1084 }
1085
1086 static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
1087 {
1088         struct signal_struct *sig;
1089
1090         if (clone_flags & CLONE_THREAD)
1091                 return 0;
1092
1093         sig = kmem_cache_zalloc(signal_cachep, GFP_KERNEL);
1094         tsk->signal = sig;
1095         if (!sig)
1096                 return -ENOMEM;
1097
1098         sig->nr_threads = 1;
1099         atomic_set(&sig->live, 1);
1100         atomic_set(&sig->sigcnt, 1);
1101
1102         /* list_add(thread_node, thread_head) without INIT_LIST_HEAD() */
1103         sig->thread_head = (struct list_head)LIST_HEAD_INIT(tsk->thread_node);
1104         tsk->thread_node = (struct list_head)LIST_HEAD_INIT(sig->thread_head);
1105
1106         init_waitqueue_head(&sig->wait_chldexit);
1107         sig->curr_target = tsk;
1108         init_sigpending(&sig->shared_pending);
1109         INIT_LIST_HEAD(&sig->posix_timers);
1110         seqlock_init(&sig->stats_lock);
1111
1112         hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
1113         sig->real_timer.function = it_real_fn;
1114
1115         task_lock(current->group_leader);
1116         memcpy(sig->rlim, current->signal->rlim, sizeof sig->rlim);
1117         task_unlock(current->group_leader);
1118
1119         posix_cpu_timers_init_group(sig);
1120
1121         tty_audit_fork(sig);
1122         sched_autogroup_fork(sig);
1123
1124 #ifdef CONFIG_CGROUPS
1125         init_rwsem(&sig->group_rwsem);
1126 #endif
1127
1128         sig->oom_score_adj = current->signal->oom_score_adj;
1129         sig->oom_score_adj_min = current->signal->oom_score_adj_min;
1130
1131         sig->has_child_subreaper = current->signal->has_child_subreaper ||
1132                                    current->signal->is_child_subreaper;
1133
1134         mutex_init(&sig->cred_guard_mutex);
1135
1136         return 0;
1137 }
1138
1139 static void copy_seccomp(struct task_struct *p)
1140 {
1141 #ifdef CONFIG_SECCOMP
1142         /*
1143          * Must be called with sighand->lock held, which is common to
1144          * all threads in the group. Holding cred_guard_mutex is not
1145          * needed because this new task is not yet running and cannot
1146          * be racing exec.
1147          */
1148         assert_spin_locked(&current->sighand->siglock);
1149
1150         /* Ref-count the new filter user, and assign it. */
1151         get_seccomp_filter(current);
1152         p->seccomp = current->seccomp;
1153
1154         /*
1155          * Explicitly enable no_new_privs here in case it got set
1156          * between the task_struct being duplicated and holding the
1157          * sighand lock. The seccomp state and nnp must be in sync.
1158          */
1159         if (task_no_new_privs(current))
1160                 task_set_no_new_privs(p);
1161
1162         /*
1163          * If the parent gained a seccomp mode after copying thread
1164          * flags and between before we held the sighand lock, we have
1165          * to manually enable the seccomp thread flag here.
1166          */
1167         if (p->seccomp.mode != SECCOMP_MODE_DISABLED)
1168                 set_tsk_thread_flag(p, TIF_SECCOMP);
1169 #endif
1170 }
1171
1172 SYSCALL_DEFINE1(set_tid_address, int __user *, tidptr)
1173 {
1174         current->clear_child_tid = tidptr;
1175
1176         return task_pid_vnr(current);
1177 }
1178
1179 static void rt_mutex_init_task(struct task_struct *p)
1180 {
1181         raw_spin_lock_init(&p->pi_lock);
1182 #ifdef CONFIG_RT_MUTEXES
1183         p->pi_waiters = RB_ROOT;
1184         p->pi_waiters_leftmost = NULL;
1185         p->pi_blocked_on = NULL;
1186 #endif
1187 }
1188
1189 /*
1190  * Initialize POSIX timer handling for a single task.
1191  */
1192 static void posix_cpu_timers_init(struct task_struct *tsk)
1193 {
1194 #ifdef CONFIG_PREEMPT_RT_BASE
1195         tsk->posix_timer_list = NULL;
1196 #endif
1197         tsk->cputime_expires.prof_exp = 0;
1198         tsk->cputime_expires.virt_exp = 0;
1199         tsk->cputime_expires.sched_exp = 0;
1200         INIT_LIST_HEAD(&tsk->cpu_timers[0]);
1201         INIT_LIST_HEAD(&tsk->cpu_timers[1]);
1202         INIT_LIST_HEAD(&tsk->cpu_timers[2]);
1203 }
1204
1205 static inline void
1206 init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid)
1207 {
1208          task->pids[type].pid = pid;
1209 }
1210
1211 /*
1212  * This creates a new process as a copy of the old one,
1213  * but does not actually start it yet.
1214  *
1215  * It copies the registers, and all the appropriate
1216  * parts of the process environment (as per the clone
1217  * flags). The actual kick-off is left to the caller.
1218  */
1219 static struct task_struct *copy_process(unsigned long clone_flags,
1220                                         unsigned long stack_start,
1221                                         unsigned long stack_size,
1222                                         int __user *child_tidptr,
1223                                         struct pid *pid,
1224                                         int trace)
1225 {
1226         int retval;
1227         struct task_struct *p;
1228
1229         if ((clone_flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
1230                 return ERR_PTR(-EINVAL);
1231
1232         if ((clone_flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS))
1233                 return ERR_PTR(-EINVAL);
1234
1235         /*
1236          * Thread groups must share signals as well, and detached threads
1237          * can only be started up within the thread group.
1238          */
1239         if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
1240                 return ERR_PTR(-EINVAL);
1241
1242         /*
1243          * Shared signal handlers imply shared VM. By way of the above,
1244          * thread groups also imply shared VM. Blocking this case allows
1245          * for various simplifications in other code.
1246          */
1247         if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
1248                 return ERR_PTR(-EINVAL);
1249
1250         /*
1251          * Siblings of global init remain as zombies on exit since they are
1252          * not reaped by their parent (swapper). To solve this and to avoid
1253          * multi-rooted process trees, prevent global and container-inits
1254          * from creating siblings.
1255          */
1256         if ((clone_flags & CLONE_PARENT) &&
1257                                 current->signal->flags & SIGNAL_UNKILLABLE)
1258                 return ERR_PTR(-EINVAL);
1259
1260         /*
1261          * If the new process will be in a different pid or user namespace
1262          * do not allow it to share a thread group or signal handlers or
1263          * parent with the forking task.
1264          */
1265         if (clone_flags & CLONE_SIGHAND) {
1266                 if ((clone_flags & (CLONE_NEWUSER | CLONE_NEWPID)) ||
1267                     (task_active_pid_ns(current) !=
1268                                 current->nsproxy->pid_ns_for_children))
1269                         return ERR_PTR(-EINVAL);
1270         }
1271
1272         retval = security_task_create(clone_flags);
1273         if (retval)
1274                 goto fork_out;
1275
1276         retval = -ENOMEM;
1277         p = dup_task_struct(current);
1278         if (!p)
1279                 goto fork_out;
1280
1281         ftrace_graph_init_task(p);
1282
1283         rt_mutex_init_task(p);
1284
1285 #ifdef CONFIG_PROVE_LOCKING
1286         DEBUG_LOCKS_WARN_ON(!p->hardirqs_enabled);
1287         DEBUG_LOCKS_WARN_ON(!p->softirqs_enabled);
1288 #endif
1289         retval = -EAGAIN;
1290         if (atomic_read(&p->real_cred->user->processes) >=
1291                         task_rlimit(p, RLIMIT_NPROC)) {
1292                 if (p->real_cred->user != INIT_USER &&
1293                     !capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN))
1294                         goto bad_fork_free;
1295         }
1296         current->flags &= ~PF_NPROC_EXCEEDED;
1297
1298         retval = copy_creds(p, clone_flags);
1299         if (retval < 0)
1300                 goto bad_fork_free;
1301
1302         /*
1303          * If multiple threads are within copy_process(), then this check
1304          * triggers too late. This doesn't hurt, the check is only there
1305          * to stop root fork bombs.
1306          */
1307         retval = -EAGAIN;
1308         if (nr_threads >= max_threads)
1309                 goto bad_fork_cleanup_count;
1310
1311         if (!try_module_get(task_thread_info(p)->exec_domain->module))
1312                 goto bad_fork_cleanup_count;
1313
1314         delayacct_tsk_init(p);  /* Must remain after dup_task_struct() */
1315         p->flags &= ~(PF_SUPERPRIV | PF_WQ_WORKER);
1316         p->flags |= PF_FORKNOEXEC;
1317         INIT_LIST_HEAD(&p->children);
1318         INIT_LIST_HEAD(&p->sibling);
1319         rcu_copy_process(p);
1320         p->vfork_done = NULL;
1321         spin_lock_init(&p->alloc_lock);
1322
1323         init_sigpending(&p->pending);
1324         p->sigqueue_cache = NULL;
1325
1326         p->utime = p->stime = p->gtime = 0;
1327         p->utimescaled = p->stimescaled = 0;
1328 #ifndef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
1329         p->prev_cputime.utime = p->prev_cputime.stime = 0;
1330 #endif
1331 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
1332         raw_spin_lock_init(&p->vtime_lock);
1333         seqcount_init(&p->vtime_seq);
1334         p->vtime_snap = 0;
1335         p->vtime_snap_whence = VTIME_SLEEPING;
1336 #endif
1337
1338 #if defined(SPLIT_RSS_COUNTING)
1339         memset(&p->rss_stat, 0, sizeof(p->rss_stat));
1340 #endif
1341
1342         p->default_timer_slack_ns = current->timer_slack_ns;
1343
1344         task_io_accounting_init(&p->ioac);
1345         acct_clear_integrals(p);
1346
1347         posix_cpu_timers_init(p);
1348
1349         p->start_time = ktime_get_ns();
1350         p->real_start_time = ktime_get_boot_ns();
1351         p->io_context = NULL;
1352         p->audit_context = NULL;
1353         if (clone_flags & CLONE_THREAD)
1354                 threadgroup_change_begin(current);
1355         cgroup_fork(p);
1356 #ifdef CONFIG_NUMA
1357         p->mempolicy = mpol_dup(p->mempolicy);
1358         if (IS_ERR(p->mempolicy)) {
1359                 retval = PTR_ERR(p->mempolicy);
1360                 p->mempolicy = NULL;
1361                 goto bad_fork_cleanup_threadgroup_lock;
1362         }
1363 #endif
1364 #ifdef CONFIG_CPUSETS
1365         p->cpuset_mem_spread_rotor = NUMA_NO_NODE;
1366         p->cpuset_slab_spread_rotor = NUMA_NO_NODE;
1367         seqcount_init(&p->mems_allowed_seq);
1368 #endif
1369 #ifdef CONFIG_TRACE_IRQFLAGS
1370         p->irq_events = 0;
1371         p->hardirqs_enabled = 0;
1372         p->hardirq_enable_ip = 0;
1373         p->hardirq_enable_event = 0;
1374         p->hardirq_disable_ip = _THIS_IP_;
1375         p->hardirq_disable_event = 0;
1376         p->softirqs_enabled = 1;
1377         p->softirq_enable_ip = _THIS_IP_;
1378         p->softirq_enable_event = 0;
1379         p->softirq_disable_ip = 0;
1380         p->softirq_disable_event = 0;
1381         p->hardirq_context = 0;
1382         p->softirq_context = 0;
1383 #endif
1384
1385         p->pagefault_disabled = 0;
1386
1387 #ifdef CONFIG_LOCKDEP
1388         p->lockdep_depth = 0; /* no locks held yet */
1389         p->curr_chain_key = 0;
1390         p->lockdep_recursion = 0;
1391 #endif
1392
1393 #ifdef CONFIG_DEBUG_MUTEXES
1394         p->blocked_on = NULL; /* not blocked yet */
1395 #endif
1396 #ifdef CONFIG_BCACHE
1397         p->sequential_io        = 0;
1398         p->sequential_io_avg    = 0;
1399 #endif
1400
1401         /* Perform scheduler related setup. Assign this task to a CPU. */
1402         retval = sched_fork(clone_flags, p);
1403         if (retval)
1404                 goto bad_fork_cleanup_policy;
1405
1406         retval = perf_event_init_task(p);
1407         if (retval)
1408                 goto bad_fork_cleanup_policy;
1409         retval = audit_alloc(p);
1410         if (retval)
1411                 goto bad_fork_cleanup_perf;
1412         /* copy all the process information */
1413         shm_init_task(p);
1414         retval = copy_semundo(clone_flags, p);
1415         if (retval)
1416                 goto bad_fork_cleanup_audit;
1417         retval = copy_files(clone_flags, p);
1418         if (retval)
1419                 goto bad_fork_cleanup_semundo;
1420         retval = copy_fs(clone_flags, p);
1421         if (retval)
1422                 goto bad_fork_cleanup_files;
1423         retval = copy_sighand(clone_flags, p);
1424         if (retval)
1425                 goto bad_fork_cleanup_fs;
1426         retval = copy_signal(clone_flags, p);
1427         if (retval)
1428                 goto bad_fork_cleanup_sighand;
1429         retval = copy_mm(clone_flags, p);
1430         if (retval)
1431                 goto bad_fork_cleanup_signal;
1432         retval = copy_namespaces(clone_flags, p);
1433         if (retval)
1434                 goto bad_fork_cleanup_mm;
1435         retval = copy_io(clone_flags, p);
1436         if (retval)
1437                 goto bad_fork_cleanup_namespaces;
1438         retval = copy_thread(clone_flags, stack_start, stack_size, p);
1439         if (retval)
1440                 goto bad_fork_cleanup_io;
1441
1442         if (pid != &init_struct_pid) {
1443                 retval = -ENOMEM;
1444                 pid = alloc_pid(p->nsproxy->pid_ns_for_children);
1445                 if (!pid)
1446                         goto bad_fork_cleanup_io;
1447         }
1448
1449         p->set_child_tid = (clone_flags & CLONE_CHILD_SETTID) ? child_tidptr : NULL;
1450         /*
1451          * Clear TID on mm_release()?
1452          */
1453         p->clear_child_tid = (clone_flags & CLONE_CHILD_CLEARTID) ? child_tidptr : NULL;
1454 #ifdef CONFIG_BLOCK
1455         p->plug = NULL;
1456 #endif
1457 #ifdef CONFIG_FUTEX
1458         p->robust_list = NULL;
1459 #ifdef CONFIG_COMPAT
1460         p->compat_robust_list = NULL;
1461 #endif
1462         INIT_LIST_HEAD(&p->pi_state_list);
1463         p->pi_state_cache = NULL;
1464 #endif
1465         /*
1466          * sigaltstack should be cleared when sharing the same VM
1467          */
1468         if ((clone_flags & (CLONE_VM|CLONE_VFORK)) == CLONE_VM)
1469                 p->sas_ss_sp = p->sas_ss_size = 0;
1470
1471         /*
1472          * Syscall tracing and stepping should be turned off in the
1473          * child regardless of CLONE_PTRACE.
1474          */
1475         user_disable_single_step(p);
1476         clear_tsk_thread_flag(p, TIF_SYSCALL_TRACE);
1477 #ifdef TIF_SYSCALL_EMU
1478         clear_tsk_thread_flag(p, TIF_SYSCALL_EMU);
1479 #endif
1480         clear_all_latency_tracing(p);
1481
1482         /* ok, now we should be set up.. */
1483         p->pid = pid_nr(pid);
1484         if (clone_flags & CLONE_THREAD) {
1485                 p->exit_signal = -1;
1486                 p->group_leader = current->group_leader;
1487                 p->tgid = current->tgid;
1488         } else {
1489                 if (clone_flags & CLONE_PARENT)
1490                         p->exit_signal = current->group_leader->exit_signal;
1491                 else
1492                         p->exit_signal = (clone_flags & CSIGNAL);
1493                 p->group_leader = p;
1494                 p->tgid = p->pid;
1495         }
1496
1497         p->nr_dirtied = 0;
1498         p->nr_dirtied_pause = 128 >> (PAGE_SHIFT - 10);
1499         p->dirty_paused_when = 0;
1500
1501         p->pdeath_signal = 0;
1502         INIT_LIST_HEAD(&p->thread_group);
1503         p->task_works = NULL;
1504
1505         /*
1506          * Make it visible to the rest of the system, but dont wake it up yet.
1507          * Need tasklist lock for parent etc handling!
1508          */
1509         write_lock_irq(&tasklist_lock);
1510
1511         /* CLONE_PARENT re-uses the old parent */
1512         if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {
1513                 p->real_parent = current->real_parent;
1514                 p->parent_exec_id = current->parent_exec_id;
1515         } else {
1516                 p->real_parent = current;
1517                 p->parent_exec_id = current->self_exec_id;
1518         }
1519
1520         spin_lock(&current->sighand->siglock);
1521
1522         /*
1523          * Copy seccomp details explicitly here, in case they were changed
1524          * before holding sighand lock.
1525          */
1526         copy_seccomp(p);
1527
1528         /*
1529          * Process group and session signals need to be delivered to just the
1530          * parent before the fork or both the parent and the child after the
1531          * fork. Restart if a signal comes in before we add the new process to
1532          * it's process group.
1533          * A fatal signal pending means that current will exit, so the new
1534          * thread can't slip out of an OOM kill (or normal SIGKILL).
1535         */
1536         recalc_sigpending();
1537         if (signal_pending(current)) {
1538                 spin_unlock(&current->sighand->siglock);
1539                 write_unlock_irq(&tasklist_lock);
1540                 retval = -ERESTARTNOINTR;
1541                 goto bad_fork_free_pid;
1542         }
1543
1544         if (likely(p->pid)) {
1545                 ptrace_init_task(p, (clone_flags & CLONE_PTRACE) || trace);
1546
1547                 init_task_pid(p, PIDTYPE_PID, pid);
1548                 if (thread_group_leader(p)) {
1549                         init_task_pid(p, PIDTYPE_PGID, task_pgrp(current));
1550                         init_task_pid(p, PIDTYPE_SID, task_session(current));
1551
1552                         if (is_child_reaper(pid)) {
1553                                 ns_of_pid(pid)->child_reaper = p;
1554                                 p->signal->flags |= SIGNAL_UNKILLABLE;
1555                         }
1556
1557                         p->signal->leader_pid = pid;
1558                         p->signal->tty = tty_kref_get(current->signal->tty);
1559                         list_add_tail(&p->sibling, &p->real_parent->children);
1560                         list_add_tail_rcu(&p->tasks, &init_task.tasks);
1561                         attach_pid(p, PIDTYPE_PGID);
1562                         attach_pid(p, PIDTYPE_SID);
1563                         __this_cpu_inc(process_counts);
1564                 } else {
1565                         current->signal->nr_threads++;
1566                         atomic_inc(&current->signal->live);
1567                         atomic_inc(&current->signal->sigcnt);
1568                         list_add_tail_rcu(&p->thread_group,
1569                                           &p->group_leader->thread_group);
1570                         list_add_tail_rcu(&p->thread_node,
1571                                           &p->signal->thread_head);
1572                 }
1573                 attach_pid(p, PIDTYPE_PID);
1574                 nr_threads++;
1575         }
1576
1577         total_forks++;
1578         spin_unlock(&current->sighand->siglock);
1579         syscall_tracepoint_update(p);
1580         write_unlock_irq(&tasklist_lock);
1581
1582         proc_fork_connector(p);
1583         cgroup_post_fork(p);
1584         if (clone_flags & CLONE_THREAD)
1585                 threadgroup_change_end(current);
1586         perf_event_fork(p);
1587
1588         trace_task_newtask(p, clone_flags);
1589         uprobe_copy_process(p, clone_flags);
1590
1591         return p;
1592
1593 bad_fork_free_pid:
1594         if (pid != &init_struct_pid)
1595                 free_pid(pid);
1596 bad_fork_cleanup_io:
1597         if (p->io_context)
1598                 exit_io_context(p);
1599 bad_fork_cleanup_namespaces:
1600         exit_task_namespaces(p);
1601 bad_fork_cleanup_mm:
1602         if (p->mm)
1603                 mmput(p->mm);
1604 bad_fork_cleanup_signal:
1605         if (!(clone_flags & CLONE_THREAD))
1606                 free_signal_struct(p->signal);
1607 bad_fork_cleanup_sighand:
1608         __cleanup_sighand(p->sighand);
1609 bad_fork_cleanup_fs:
1610         exit_fs(p); /* blocking */
1611 bad_fork_cleanup_files:
1612         exit_files(p); /* blocking */
1613 bad_fork_cleanup_semundo:
1614         exit_sem(p);
1615 bad_fork_cleanup_audit:
1616         audit_free(p);
1617 bad_fork_cleanup_perf:
1618         perf_event_free_task(p);
1619 bad_fork_cleanup_policy:
1620 #ifdef CONFIG_NUMA
1621         mpol_put(p->mempolicy);
1622 bad_fork_cleanup_threadgroup_lock:
1623 #endif
1624         if (clone_flags & CLONE_THREAD)
1625                 threadgroup_change_end(current);
1626         delayacct_tsk_free(p);
1627         module_put(task_thread_info(p)->exec_domain->module);
1628 bad_fork_cleanup_count:
1629         atomic_dec(&p->cred->user->processes);
1630         exit_creds(p);
1631 bad_fork_free:
1632         free_task(p);
1633 fork_out:
1634         return ERR_PTR(retval);
1635 }
1636
1637 static inline void init_idle_pids(struct pid_link *links)
1638 {
1639         enum pid_type type;
1640
1641         for (type = PIDTYPE_PID; type < PIDTYPE_MAX; ++type) {
1642                 INIT_HLIST_NODE(&links[type].node); /* not really needed */
1643                 links[type].pid = &init_struct_pid;
1644         }
1645 }
1646
1647 struct task_struct *fork_idle(int cpu)
1648 {
1649         struct task_struct *task;
1650         task = copy_process(CLONE_VM, 0, 0, NULL, &init_struct_pid, 0);
1651         if (!IS_ERR(task)) {
1652                 init_idle_pids(task->pids);
1653                 init_idle(task, cpu);
1654         }
1655
1656         return task;
1657 }
1658
1659 /*
1660  *  Ok, this is the main fork-routine.
1661  *
1662  * It copies the process, and if successful kick-starts
1663  * it and waits for it to finish using the VM if required.
1664  */
1665 long do_fork(unsigned long clone_flags,
1666               unsigned long stack_start,
1667               unsigned long stack_size,
1668               int __user *parent_tidptr,
1669               int __user *child_tidptr)
1670 {
1671         struct task_struct *p;
1672         int trace = 0;
1673         long nr;
1674
1675         /*
1676          * Determine whether and which event to report to ptracer.  When
1677          * called from kernel_thread or CLONE_UNTRACED is explicitly
1678          * requested, no event is reported; otherwise, report if the event
1679          * for the type of forking is enabled.
1680          */
1681         if (!(clone_flags & CLONE_UNTRACED)) {
1682                 if (clone_flags & CLONE_VFORK)
1683                         trace = PTRACE_EVENT_VFORK;
1684                 else if ((clone_flags & CSIGNAL) != SIGCHLD)
1685                         trace = PTRACE_EVENT_CLONE;
1686                 else
1687                         trace = PTRACE_EVENT_FORK;
1688
1689                 if (likely(!ptrace_event_enabled(current, trace)))
1690                         trace = 0;
1691         }
1692
1693         p = copy_process(clone_flags, stack_start, stack_size,
1694                          child_tidptr, NULL, trace);
1695         /*
1696          * Do this prior waking up the new thread - the thread pointer
1697          * might get invalid after that point, if the thread exits quickly.
1698          */
1699         if (!IS_ERR(p)) {
1700                 struct completion vfork;
1701                 struct pid *pid;
1702
1703                 trace_sched_process_fork(current, p);
1704
1705                 pid = get_task_pid(p, PIDTYPE_PID);
1706                 nr = pid_vnr(pid);
1707
1708                 if (clone_flags & CLONE_PARENT_SETTID)
1709                         put_user(nr, parent_tidptr);
1710
1711                 if (clone_flags & CLONE_VFORK) {
1712                         p->vfork_done = &vfork;
1713                         init_completion(&vfork);
1714                         get_task_struct(p);
1715                 }
1716
1717                 wake_up_new_task(p);
1718
1719                 /* forking complete and child started to run, tell ptracer */
1720                 if (unlikely(trace))
1721                         ptrace_event_pid(trace, pid);
1722
1723                 if (clone_flags & CLONE_VFORK) {
1724                         if (!wait_for_vfork_done(p, &vfork))
1725                                 ptrace_event_pid(PTRACE_EVENT_VFORK_DONE, pid);
1726                 }
1727
1728                 put_pid(pid);
1729         } else {
1730                 nr = PTR_ERR(p);
1731         }
1732         return nr;
1733 }
1734
1735 /*
1736  * Create a kernel thread.
1737  */
1738 pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
1739 {
1740         return do_fork(flags|CLONE_VM|CLONE_UNTRACED, (unsigned long)fn,
1741                 (unsigned long)arg, NULL, NULL);
1742 }
1743
1744 #ifdef __ARCH_WANT_SYS_FORK
1745 SYSCALL_DEFINE0(fork)
1746 {
1747 #ifdef CONFIG_MMU
1748         return do_fork(SIGCHLD, 0, 0, NULL, NULL);
1749 #else
1750         /* can not support in nommu mode */
1751         return -EINVAL;
1752 #endif
1753 }
1754 #endif
1755
1756 #ifdef __ARCH_WANT_SYS_VFORK
1757 SYSCALL_DEFINE0(vfork)
1758 {
1759         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, 0,
1760                         0, NULL, NULL);
1761 }
1762 #endif
1763
1764 #ifdef __ARCH_WANT_SYS_CLONE
1765 #ifdef CONFIG_CLONE_BACKWARDS
1766 SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
1767                  int __user *, parent_tidptr,
1768                  int, tls_val,
1769                  int __user *, child_tidptr)
1770 #elif defined(CONFIG_CLONE_BACKWARDS2)
1771 SYSCALL_DEFINE5(clone, unsigned long, newsp, unsigned long, clone_flags,
1772                  int __user *, parent_tidptr,
1773                  int __user *, child_tidptr,
1774                  int, tls_val)
1775 #elif defined(CONFIG_CLONE_BACKWARDS3)
1776 SYSCALL_DEFINE6(clone, unsigned long, clone_flags, unsigned long, newsp,
1777                 int, stack_size,
1778                 int __user *, parent_tidptr,
1779                 int __user *, child_tidptr,
1780                 int, tls_val)
1781 #else
1782 SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
1783                  int __user *, parent_tidptr,
1784                  int __user *, child_tidptr,
1785                  int, tls_val)
1786 #endif
1787 {
1788         return do_fork(clone_flags, newsp, 0, parent_tidptr, child_tidptr);
1789 }
1790 #endif
1791
1792 #ifndef ARCH_MIN_MMSTRUCT_ALIGN
1793 #define ARCH_MIN_MMSTRUCT_ALIGN 0
1794 #endif
1795
1796 static void sighand_ctor(void *data)
1797 {
1798         struct sighand_struct *sighand = data;
1799
1800         spin_lock_init(&sighand->siglock);
1801         init_waitqueue_head(&sighand->signalfd_wqh);
1802 }
1803
1804 void __init proc_caches_init(void)
1805 {
1806         sighand_cachep = kmem_cache_create("sighand_cache",
1807                         sizeof(struct sighand_struct), 0,
1808                         SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_DESTROY_BY_RCU|
1809                         SLAB_NOTRACK, sighand_ctor);
1810         signal_cachep = kmem_cache_create("signal_cache",
1811                         sizeof(struct signal_struct), 0,
1812                         SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
1813         files_cachep = kmem_cache_create("files_cache",
1814                         sizeof(struct files_struct), 0,
1815                         SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
1816         fs_cachep = kmem_cache_create("fs_cache",
1817                         sizeof(struct fs_struct), 0,
1818                         SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
1819         /*
1820          * FIXME! The "sizeof(struct mm_struct)" currently includes the
1821          * whole struct cpumask for the OFFSTACK case. We could change
1822          * this to *only* allocate as much of it as required by the
1823          * maximum number of CPU's we can ever have.  The cpumask_allocation
1824          * is at the end of the structure, exactly for that reason.
1825          */
1826         mm_cachep = kmem_cache_create("mm_struct",
1827                         sizeof(struct mm_struct), ARCH_MIN_MMSTRUCT_ALIGN,
1828                         SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_NOTRACK, NULL);
1829         vm_area_cachep = KMEM_CACHE(vm_area_struct, SLAB_PANIC);
1830         mmap_init();
1831         nsproxy_cache_init();
1832 }
1833
1834 /*
1835  * Check constraints on flags passed to the unshare system call.
1836  */
1837 static int check_unshare_flags(unsigned long unshare_flags)
1838 {
1839         if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
1840                                 CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
1841                                 CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWNET|
1842                                 CLONE_NEWUSER|CLONE_NEWPID))
1843                 return -EINVAL;
1844         /*
1845          * Not implemented, but pretend it works if there is nothing to
1846          * unshare. Note that unsharing CLONE_THREAD or CLONE_SIGHAND
1847          * needs to unshare vm.
1848          */
1849         if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) {
1850                 /* FIXME: get_task_mm() increments ->mm_users */
1851                 if (atomic_read(&current->mm->mm_users) > 1)
1852                         return -EINVAL;
1853         }
1854
1855         return 0;
1856 }
1857
1858 /*
1859  * Unshare the filesystem structure if it is being shared
1860  */
1861 static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)
1862 {
1863         struct fs_struct *fs = current->fs;
1864
1865         if (!(unshare_flags & CLONE_FS) || !fs)
1866                 return 0;
1867
1868         /* don't need lock here; in the worst case we'll do useless copy */
1869         if (fs->users == 1)
1870                 return 0;
1871
1872         *new_fsp = copy_fs_struct(fs);
1873         if (!*new_fsp)
1874                 return -ENOMEM;
1875
1876         return 0;
1877 }
1878
1879 /*
1880  * Unshare file descriptor table if it is being shared
1881  */
1882 static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp)
1883 {
1884         struct files_struct *fd = current->files;
1885         int error = 0;
1886
1887         if ((unshare_flags & CLONE_FILES) &&
1888             (fd && atomic_read(&fd->count) > 1)) {
1889                 *new_fdp = dup_fd(fd, &error);
1890                 if (!*new_fdp)
1891                         return error;
1892         }
1893
1894         return 0;
1895 }
1896
1897 /*
1898  * unshare allows a process to 'unshare' part of the process
1899  * context which was originally shared using clone.  copy_*
1900  * functions used by do_fork() cannot be used here directly
1901  * because they modify an inactive task_struct that is being
1902  * constructed. Here we are modifying the current, active,
1903  * task_struct.
1904  */
1905 SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
1906 {
1907         struct fs_struct *fs, *new_fs = NULL;
1908         struct files_struct *fd, *new_fd = NULL;
1909         struct cred *new_cred = NULL;
1910         struct nsproxy *new_nsproxy = NULL;
1911         int do_sysvsem = 0;
1912         int err;
1913
1914         /*
1915          * If unsharing a user namespace must also unshare the thread.
1916          */
1917         if (unshare_flags & CLONE_NEWUSER)
1918                 unshare_flags |= CLONE_THREAD | CLONE_FS;
1919         /*
1920          * If unsharing a thread from a thread group, must also unshare vm.
1921          */
1922         if (unshare_flags & CLONE_THREAD)
1923                 unshare_flags |= CLONE_VM;
1924         /*
1925          * If unsharing vm, must also unshare signal handlers.
1926          */
1927         if (unshare_flags & CLONE_VM)
1928                 unshare_flags |= CLONE_SIGHAND;
1929         /*
1930          * If unsharing namespace, must also unshare filesystem information.
1931          */
1932         if (unshare_flags & CLONE_NEWNS)
1933                 unshare_flags |= CLONE_FS;
1934
1935         err = check_unshare_flags(unshare_flags);
1936         if (err)
1937                 goto bad_unshare_out;
1938         /*
1939          * CLONE_NEWIPC must also detach from the undolist: after switching
1940          * to a new ipc namespace, the semaphore arrays from the old
1941          * namespace are unreachable.
1942          */
1943         if (unshare_flags & (CLONE_NEWIPC|CLONE_SYSVSEM))
1944                 do_sysvsem = 1;
1945         err = unshare_fs(unshare_flags, &new_fs);
1946         if (err)
1947                 goto bad_unshare_out;
1948         err = unshare_fd(unshare_flags, &new_fd);
1949         if (err)
1950                 goto bad_unshare_cleanup_fs;
1951         err = unshare_userns(unshare_flags, &new_cred);
1952         if (err)
1953                 goto bad_unshare_cleanup_fd;
1954         err = unshare_nsproxy_namespaces(unshare_flags, &new_nsproxy,
1955                                          new_cred, new_fs);
1956         if (err)
1957                 goto bad_unshare_cleanup_cred;
1958
1959         if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) {
1960                 if (do_sysvsem) {
1961                         /*
1962                          * CLONE_SYSVSEM is equivalent to sys_exit().
1963                          */
1964                         exit_sem(current);
1965                 }
1966                 if (unshare_flags & CLONE_NEWIPC) {
1967                         /* Orphan segments in old ns (see sem above). */
1968                         exit_shm(current);
1969                         shm_init_task(current);
1970                 }
1971
1972                 if (new_nsproxy)
1973                         switch_task_namespaces(current, new_nsproxy);
1974
1975                 task_lock(current);
1976
1977                 if (new_fs) {
1978                         fs = current->fs;
1979                         spin_lock(&fs->lock);
1980                         current->fs = new_fs;
1981                         if (--fs->users)
1982                                 new_fs = NULL;
1983                         else
1984                                 new_fs = fs;
1985                         spin_unlock(&fs->lock);
1986                 }
1987
1988                 if (new_fd) {
1989                         fd = current->files;
1990                         current->files = new_fd;
1991                         new_fd = fd;
1992                 }
1993
1994                 task_unlock(current);
1995
1996                 if (new_cred) {
1997                         /* Install the new user namespace */
1998                         commit_creds(new_cred);
1999                         new_cred = NULL;
2000                 }
2001         }
2002
2003 bad_unshare_cleanup_cred:
2004         if (new_cred)
2005                 put_cred(new_cred);
2006 bad_unshare_cleanup_fd:
2007         if (new_fd)
2008                 put_files_struct(new_fd);
2009
2010 bad_unshare_cleanup_fs:
2011         if (new_fs)
2012                 free_fs_struct(new_fs);
2013
2014 bad_unshare_out:
2015         return err;
2016 }
2017
2018 /*
2019  *      Helper to unshare the files of the current task.
2020  *      We don't want to expose copy_files internals to
2021  *      the exec layer of the kernel.
2022  */
2023
2024 int unshare_files(struct files_struct **displaced)
2025 {
2026         struct task_struct *task = current;
2027         struct files_struct *copy = NULL;
2028         int error;
2029
2030         error = unshare_fd(CLONE_FILES, &copy);
2031         if (error || !copy) {
2032                 *displaced = NULL;
2033                 return error;
2034         }
2035         *displaced = task->files;
2036         task_lock(task);
2037         task->files = copy;
2038         task_unlock(task);
2039         return 0;
2040 }