14 #include <linux/slab.h>
17 #include <linux/module.h>
20 #include <linux/personality.h>
21 #include <linux/mempolicy.h>
22 #include <linux/sem.h>
27 #include <linux/binfmts.h>
28 #include <linux/mman.h>
32 #include <linux/capability.h>
37 #include <linux/seccomp.h>
41 #include <linux/futex.h>
46 #include <linux/ptrace.h>
48 #include <linux/audit.h>
55 #include <linux/acct.h>
57 #include <linux/cn_proc.h>
61 #include <linux/random.h>
62 #include <linux/tty.h>
65 #include <linux/magic.h>
66 #include <linux/perf_event.h>
69 #include <linux/oom.h>
71 #include <linux/signalfd.h>
74 #include <asm/pgtable.h>
75 #include <asm/pgalloc.h>
76 #include <asm/uaccess.h>
77 #include <asm/mmu_context.h>
78 #include <asm/cacheflush.h>
79 #include <asm/tlbflush.h>
83 #define CREATE_TRACE_POINTS
98 #ifdef CONFIG_PROVE_RCU
99 int lockdep_tasklist_lock_is_held(
void)
101 return lockdep_is_held(&tasklist_lock);
112 total +=
per_cpu(process_counts, cpu);
121 #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
139 #ifndef CONFIG_ARCH_THREAD_INFO_ALLOCATOR
145 # if THREAD_SIZE >= PAGE_SIZE
149 struct page *
page = alloc_pages_node(node, THREADINFO_GFP,
200 static void account_kernel_stack(
struct thread_info *ti,
int account)
209 account_kernel_stack(tsk->
stack, -1);
213 ftrace_graph_exit_task(tsk);
214 put_seccomp_filter(tsk);
222 taskstats_tgid_free(sig);
223 sched_autogroup_exit(sig);
230 free_signal_struct(sig);
241 delayacct_tsk_free(tsk);
242 put_signal_struct(tsk->
signal);
253 #ifndef CONFIG_ARCH_TASK_STRUCT_ALLOCATOR
254 #ifndef ARCH_MIN_TASKALIGN
255 #define ARCH_MIN_TASKALIGN L1_CACHE_BYTES
296 unsigned long *stackend;
315 clear_user_return_notifier(tsk);
316 clear_tsk_need_resched(tsk);
320 #ifdef CONFIG_CC_STACKPROTECTOR
329 #ifdef CONFIG_BLK_DEV_IO_TRACE
335 account_kernel_stack(ti, 1);
352 unsigned long charge;
369 cpumask_clear(mm_cpumask(mm));
371 rb_link = &mm->
mm_rb.rb_node;
374 retval = ksm_fork(mm, oldmm);
377 retval = khugepaged_fork(mm, oldmm);
382 for (mpnt = oldmm->
mmap; mpnt; mpnt = mpnt->
vm_next) {
392 unsigned long len = vma_pages(mpnt);
404 retval = PTR_ERR(pol);
406 goto fail_nomem_policy;
410 goto fail_nomem_anon_vma_fork;
427 vma_nonlinear_insert(tmp,
430 vma_interval_tree_insert_after(tmp, mpnt,
441 if (is_vm_hugetlb_page(tmp))
453 rb_link = &tmp->
vm_rb.rb_right;
454 rb_parent = &tmp->
vm_rb;
466 arch_dup_mmap(oldmm, mm);
473 fail_nomem_anon_vma_fork:
479 vm_unacct_memory(charge);
496 #define dup_mmap(mm, oldmm) (0)
497 #define mm_alloc_pgd(mm) (0)
498 #define mm_free_pgd(mm)
503 #define allocate_mm() (kmem_cache_alloc(mm_cachep, GFP_KERNEL))
504 #define free_mm(mm) (kmem_cache_free(mm_cachep, (mm)))
508 static int __init coredump_filter_setup(
char *
s)
510 default_dump_filter =
516 __setup(
"coredump_filter=", coredump_filter_setup);
520 static void mm_init_aio(
struct mm_struct *mm)
533 INIT_LIST_HEAD(&mm->
mmlist);
543 mm_init_owner(mm, p);
547 mmu_notifier_mm_init(mm);
555 static void check_mm(
struct mm_struct *mm)
560 long x = atomic_long_read(&mm->
rss_stat.count[i]);
564 "mm:%p idx:%d val:%ld\n", mm, i, x);
567 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
583 memset(mm, 0,
sizeof(*mm));
598 mmu_notifier_mm_destroy(mm);
618 if (!list_empty(&mm->
mmlist)) {
624 module_put(mm->
binfmt->module);
633 get_file(new_exe_file);
641 struct file *exe_file;
705 static void complete_vfork_done(
struct task_struct *tsk)
723 freezer_do_not_count();
733 put_task_struct(child);
756 tsk->robust_list =
NULL;
759 if (
unlikely(tsk->compat_robust_list)) {
761 tsk->compat_robust_list =
NULL;
764 if (
unlikely(!list_empty(&tsk->pi_state_list)))
799 complete_vfork_done(tsk);
818 memcpy(mm, oldmm,
sizeof(*mm));
821 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
822 mm->pmd_huge_pte =
NULL;
824 if (!mm_init(mm, tsk))
830 dup_mm_exe_file(oldmm, mm);
862 static int copy_mm(
unsigned long clone_flags,
struct task_struct *tsk)
869 #ifdef CONFIG_DETECT_HUNG_TASK
905 static int copy_fs(
unsigned long clone_flags,
struct task_struct *tsk)
910 spin_lock(&fs->
lock);
912 spin_unlock(&fs->
lock);
916 spin_unlock(&fs->
lock);
925 static int copy_files(
unsigned long clone_flags,
struct task_struct *tsk)
942 newf =
dup_fd(oldf, &error);
952 static int copy_io(
unsigned long clone_flags,
struct task_struct *tsk)
978 static int copy_sighand(
unsigned long clone_flags,
struct task_struct *tsk)
1007 static void posix_cpu_timers_init_group(
struct signal_struct *sig)
1009 unsigned long cpu_limit;
1012 thread_group_cputime_init(sig);
1026 static int copy_signal(
unsigned long clone_flags,
struct task_struct *tsk)
1033 sig = kmem_cache_zalloc(signal_cachep,
GFP_KERNEL);
1051 task_lock(
current->group_leader);
1053 task_unlock(
current->group_leader);
1055 posix_cpu_timers_init_group(sig);
1058 sched_autogroup_fork(sig);
1060 #ifdef CONFIG_CGROUPS
1068 current->signal->is_child_subreaper;
1075 static void copy_flags(
unsigned long clone_flags,
struct task_struct *p)
1077 unsigned long new_flags = p->
flags;
1081 p->
flags = new_flags;
1086 current->clear_child_tid = tidptr;
1091 static void rt_mutex_init_task(
struct task_struct *p)
1094 #ifdef CONFIG_RT_MUTEXES
1095 plist_head_init(&p->pi_waiters);
1096 p->pi_blocked_on =
NULL;
1100 #ifdef CONFIG_MM_OWNER
1110 static void posix_cpu_timers_init(
struct task_struct *tsk)
1128 static struct task_struct *copy_process(
unsigned long clone_flags,
1131 unsigned long stack_size,
1132 int __user *child_tidptr,
1138 int cgroup_callbacks_done = 0;
1147 if ((clone_flags & CLONE_THREAD) && !(clone_flags & CLONE_SIGHAND))
1155 if ((clone_flags & CLONE_SIGHAND) && !(clone_flags & CLONE_VM))
1177 ftrace_graph_init_task(p);
1178 get_seccomp_filter(p);
1180 rt_mutex_init_task(p);
1182 #ifdef CONFIG_PROVE_LOCKING
1206 goto bad_fork_cleanup_count;
1209 goto bad_fork_cleanup_count;
1212 delayacct_tsk_init(p);
1213 copy_flags(clone_flags, p);
1216 rcu_copy_process(p);
1224 #ifndef CONFIG_VIRT_CPU_ACCOUNTING
1227 #if defined(SPLIT_RSS_COUNTING)
1228 memset(&p->rss_stat, 0,
sizeof(p->rss_stat));
1233 task_io_accounting_init(&p->
ioac);
1234 acct_clear_integrals(p);
1236 posix_cpu_timers_init(p);
1243 if (clone_flags & CLONE_THREAD)
1244 threadgroup_change_begin(
current);
1247 p->mempolicy = mpol_dup(p->mempolicy);
1248 if (IS_ERR(p->mempolicy)) {
1249 retval = PTR_ERR(p->mempolicy);
1250 p->mempolicy =
NULL;
1251 goto bad_fork_cleanup_cgroup;
1255 #ifdef CONFIG_CPUSETS
1260 #ifdef CONFIG_TRACE_IRQFLAGS
1262 p->hardirqs_enabled = 0;
1263 p->hardirq_enable_ip = 0;
1264 p->hardirq_enable_event = 0;
1266 p->hardirq_disable_event = 0;
1267 p->softirqs_enabled = 1;
1269 p->softirq_enable_event = 0;
1270 p->softirq_disable_ip = 0;
1271 p->softirq_disable_event = 0;
1272 p->hardirq_context = 0;
1273 p->softirq_context = 0;
1275 #ifdef CONFIG_LOCKDEP
1276 p->lockdep_depth = 0;
1277 p->curr_chain_key = 0;
1278 p->lockdep_recursion = 0;
1281 #ifdef CONFIG_DEBUG_MUTEXES
1282 p->blocked_on =
NULL;
1285 p->memcg_batch.do_batch = 0;
1286 p->memcg_batch.memcg =
NULL;
1294 goto bad_fork_cleanup_policy;
1297 goto bad_fork_cleanup_policy;
1301 goto bad_fork_cleanup_audit;
1302 retval = copy_files(clone_flags, p);
1304 goto bad_fork_cleanup_semundo;
1305 retval = copy_fs(clone_flags, p);
1307 goto bad_fork_cleanup_files;
1308 retval = copy_sighand(clone_flags, p);
1310 goto bad_fork_cleanup_fs;
1311 retval = copy_signal(clone_flags, p);
1313 goto bad_fork_cleanup_sighand;
1314 retval = copy_mm(clone_flags, p);
1316 goto bad_fork_cleanup_signal;
1319 goto bad_fork_cleanup_mm;
1320 retval = copy_io(clone_flags, p);
1322 goto bad_fork_cleanup_namespaces;
1323 retval =
copy_thread(clone_flags, stack_start, stack_size, p, regs);
1325 goto bad_fork_cleanup_io;
1331 goto bad_fork_cleanup_io;
1334 p->
pid = pid_nr(pid);
1336 if (clone_flags & CLONE_THREAD)
1348 p->robust_list =
NULL;
1349 #ifdef CONFIG_COMPAT
1350 p->compat_robust_list =
NULL;
1352 INIT_LIST_HEAD(&p->pi_state_list);
1353 p->pi_state_cache =
NULL;
1359 if ((clone_flags & (CLONE_VM|
CLONE_VFORK)) == CLONE_VM)
1368 #ifdef TIF_SYSCALL_EMU
1374 if (clone_flags & CLONE_THREAD)
1376 else if (clone_flags & CLONE_PARENT)
1400 cgroup_callbacks_done = 1;
1406 if (clone_flags & (CLONE_PARENT|CLONE_THREAD)) {
1414 spin_lock(&
current->sighand->siglock);
1425 if (signal_pending(
current)) {
1426 spin_unlock(&
current->sighand->siglock);
1429 goto bad_fork_free_pid;
1432 if (clone_flags & CLONE_THREAD) {
1433 current->signal->nr_threads++;
1441 ptrace_init_task(p, (clone_flags &
CLONE_PTRACE) || trace);
1443 if (thread_group_leader(p)) {
1444 if (is_child_reaper(pid))
1445 p->
nsproxy->pid_ns->child_reaper =
p;
1460 spin_unlock(&
current->sighand->siglock);
1464 if (clone_flags & CLONE_THREAD)
1465 threadgroup_change_end(
current);
1468 trace_task_newtask(p, clone_flags);
1475 bad_fork_cleanup_io:
1478 bad_fork_cleanup_namespaces:
1482 bad_fork_cleanup_mm:
1485 bad_fork_cleanup_signal:
1486 if (!(clone_flags & CLONE_THREAD))
1487 free_signal_struct(p->
signal);
1488 bad_fork_cleanup_sighand:
1490 bad_fork_cleanup_fs:
1492 bad_fork_cleanup_files:
1494 bad_fork_cleanup_semundo:
1496 bad_fork_cleanup_audit:
1498 bad_fork_cleanup_policy:
1501 mpol_put(p->mempolicy);
1502 bad_fork_cleanup_cgroup:
1504 if (clone_flags & CLONE_THREAD)
1505 threadgroup_change_end(
current);
1507 delayacct_tsk_free(p);
1509 bad_fork_cleanup_count:
1515 return ERR_PTR(retval);
1524 static inline void init_idle_pids(
struct pid_link *links)
1529 INIT_HLIST_NODE(&links[type].node);
1541 if (!IS_ERR(task)) {
1542 init_idle_pids(task->pids);
1556 unsigned long stack_start,
1558 unsigned long stack_size,
1559 int __user *parent_tidptr,
1560 int __user *child_tidptr)
1571 if (clone_flags & CLONE_THREAD)
1599 p = copy_process(clone_flags, stack_start, regs, stack_size,
1600 child_tidptr,
NULL, trace);
1608 trace_sched_process_fork(
current, p);
1610 nr = task_pid_vnr(p);
1617 init_completion(&vfork);
1625 ptrace_event(trace, nr);
1627 if (clone_flags & CLONE_VFORK) {
1628 if (!wait_for_vfork_done(p, &vfork))
1637 #ifdef CONFIG_GENERIC_KERNEL_THREAD
1648 #ifndef ARCH_MIN_MMSTRUCT_ALIGN
1649 #define ARCH_MIN_MMSTRUCT_ALIGN 0
1652 static void sighand_ctor(
void *
data)
1693 static int check_unshare_flags(
unsigned long unshare_flags)
1695 if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|
CLONE_NEWNS|CLONE_SIGHAND|
1704 if (unshare_flags & (CLONE_THREAD | CLONE_SIGHAND | CLONE_VM)) {
1716 static int unshare_fs(
unsigned long unshare_flags,
struct fs_struct **new_fsp)
1720 if (!(unshare_flags & CLONE_FS) || !fs)
1737 static int unshare_fd(
unsigned long unshare_flags,
struct files_struct **new_fdp)
1742 if ((unshare_flags & CLONE_FILES) &&
1744 *new_fdp =
dup_fd(fd, &error);
1768 err = check_unshare_flags(unshare_flags);
1770 goto bad_unshare_out;
1784 err = unshare_fs(unshare_flags, &new_fs);
1786 goto bad_unshare_out;
1787 err = unshare_fd(unshare_flags, &new_fd);
1789 goto bad_unshare_cleanup_fs;
1792 goto bad_unshare_cleanup_fd;
1794 if (new_fs || new_fd || do_sysvsem || new_nsproxy) {
1811 spin_lock(&fs->
lock);
1817 spin_unlock(&fs->
lock);
1830 put_nsproxy(new_nsproxy);
1832 bad_unshare_cleanup_fd:
1836 bad_unshare_cleanup_fs:
1856 error = unshare_fd(CLONE_FILES, ©);
1857 if (error || !copy) {
1861 *displaced = task->
files;