aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/exit.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/exit.c')
-rw-r--r--kernel/exit.c258
1 files changed, 76 insertions, 182 deletions
diff --git a/kernel/exit.c b/kernel/exit.c
index f80dec3f1875..abf9cf3b95c6 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -46,6 +46,7 @@
46#include <linux/blkdev.h> 46#include <linux/blkdev.h>
47#include <linux/task_io_accounting_ops.h> 47#include <linux/task_io_accounting_ops.h>
48#include <linux/tracehook.h> 48#include <linux/tracehook.h>
49#include <linux/fs_struct.h>
49#include <linux/init_task.h> 50#include <linux/init_task.h>
50#include <trace/sched.h> 51#include <trace/sched.h>
51 52
@@ -61,11 +62,6 @@ DEFINE_TRACE(sched_process_wait);
61 62
62static void exit_mm(struct task_struct * tsk); 63static void exit_mm(struct task_struct * tsk);
63 64
64static inline int task_detached(struct task_struct *p)
65{
66 return p->exit_signal == -1;
67}
68
69static void __unhash_process(struct task_struct *p) 65static void __unhash_process(struct task_struct *p)
70{ 66{
71 nr_threads--; 67 nr_threads--;
@@ -118,6 +114,8 @@ static void __exit_signal(struct task_struct *tsk)
118 * We won't ever get here for the group leader, since it 114 * We won't ever get here for the group leader, since it
119 * will have been the last reference on the signal_struct. 115 * will have been the last reference on the signal_struct.
120 */ 116 */
117 sig->utime = cputime_add(sig->utime, task_utime(tsk));
118 sig->stime = cputime_add(sig->stime, task_stime(tsk));
121 sig->gtime = cputime_add(sig->gtime, task_gtime(tsk)); 119 sig->gtime = cputime_add(sig->gtime, task_gtime(tsk));
122 sig->min_flt += tsk->min_flt; 120 sig->min_flt += tsk->min_flt;
123 sig->maj_flt += tsk->maj_flt; 121 sig->maj_flt += tsk->maj_flt;
@@ -126,6 +124,7 @@ static void __exit_signal(struct task_struct *tsk)
126 sig->inblock += task_io_get_inblock(tsk); 124 sig->inblock += task_io_get_inblock(tsk);
127 sig->oublock += task_io_get_oublock(tsk); 125 sig->oublock += task_io_get_oublock(tsk);
128 task_io_accounting_add(&sig->ioac, &tsk->ioac); 126 task_io_accounting_add(&sig->ioac, &tsk->ioac);
127 sig->sum_sched_runtime += tsk->se.sum_exec_runtime;
129 sig = NULL; /* Marker for below. */ 128 sig = NULL; /* Marker for below. */
130 } 129 }
131 130
@@ -359,16 +358,12 @@ static void reparent_to_kthreadd(void)
359void __set_special_pids(struct pid *pid) 358void __set_special_pids(struct pid *pid)
360{ 359{
361 struct task_struct *curr = current->group_leader; 360 struct task_struct *curr = current->group_leader;
362 pid_t nr = pid_nr(pid);
363 361
364 if (task_session(curr) != pid) { 362 if (task_session(curr) != pid)
365 change_pid(curr, PIDTYPE_SID, pid); 363 change_pid(curr, PIDTYPE_SID, pid);
366 set_task_session(curr, nr); 364
367 } 365 if (task_pgrp(curr) != pid)
368 if (task_pgrp(curr) != pid) {
369 change_pid(curr, PIDTYPE_PGID, pid); 366 change_pid(curr, PIDTYPE_PGID, pid);
370 set_task_pgrp(curr, nr);
371 }
372} 367}
373 368
374static void set_special_pids(struct pid *pid) 369static void set_special_pids(struct pid *pid)
@@ -426,7 +421,6 @@ EXPORT_SYMBOL(disallow_signal);
426void daemonize(const char *name, ...) 421void daemonize(const char *name, ...)
427{ 422{
428 va_list args; 423 va_list args;
429 struct fs_struct *fs;
430 sigset_t blocked; 424 sigset_t blocked;
431 425
432 va_start(args, name); 426 va_start(args, name);
@@ -459,11 +453,7 @@ void daemonize(const char *name, ...)
459 453
460 /* Become as one with the init task */ 454 /* Become as one with the init task */
461 455
462 exit_fs(current); /* current->fs->count--; */ 456 daemonize_fs_struct();
463 fs = init_task.fs;
464 current->fs = fs;
465 atomic_inc(&fs->count);
466
467 exit_files(current); 457 exit_files(current);
468 current->files = init_task.files; 458 current->files = init_task.files;
469 atomic_inc(&current->files->count); 459 atomic_inc(&current->files->count);
@@ -562,30 +552,6 @@ void exit_files(struct task_struct *tsk)
562 } 552 }
563} 553}
564 554
565void put_fs_struct(struct fs_struct *fs)
566{
567 /* No need to hold fs->lock if we are killing it */
568 if (atomic_dec_and_test(&fs->count)) {
569 path_put(&fs->root);
570 path_put(&fs->pwd);
571 kmem_cache_free(fs_cachep, fs);
572 }
573}
574
575void exit_fs(struct task_struct *tsk)
576{
577 struct fs_struct * fs = tsk->fs;
578
579 if (fs) {
580 task_lock(tsk);
581 tsk->fs = NULL;
582 task_unlock(tsk);
583 put_fs_struct(fs);
584 }
585}
586
587EXPORT_SYMBOL_GPL(exit_fs);
588
589#ifdef CONFIG_MM_OWNER 555#ifdef CONFIG_MM_OWNER
590/* 556/*
591 * Task p is exiting and it owned mm, lets find a new owner for it 557 * Task p is exiting and it owned mm, lets find a new owner for it
@@ -729,119 +695,6 @@ static void exit_mm(struct task_struct * tsk)
729} 695}
730 696
731/* 697/*
732 * Return nonzero if @parent's children should reap themselves.
733 *
734 * Called with write_lock_irq(&tasklist_lock) held.
735 */
736static int ignoring_children(struct task_struct *parent)
737{
738 int ret;
739 struct sighand_struct *psig = parent->sighand;
740 unsigned long flags;
741 spin_lock_irqsave(&psig->siglock, flags);
742 ret = (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
743 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT));
744 spin_unlock_irqrestore(&psig->siglock, flags);
745 return ret;
746}
747
748/*
749 * Detach all tasks we were using ptrace on.
750 * Any that need to be release_task'd are put on the @dead list.
751 *
752 * Called with write_lock(&tasklist_lock) held.
753 */
754static void ptrace_exit(struct task_struct *parent, struct list_head *dead)
755{
756 struct task_struct *p, *n;
757 int ign = -1;
758
759 list_for_each_entry_safe(p, n, &parent->ptraced, ptrace_entry) {
760 __ptrace_unlink(p);
761
762 if (p->exit_state != EXIT_ZOMBIE)
763 continue;
764
765 /*
766 * If it's a zombie, our attachedness prevented normal
767 * parent notification or self-reaping. Do notification
768 * now if it would have happened earlier. If it should
769 * reap itself, add it to the @dead list. We can't call
770 * release_task() here because we already hold tasklist_lock.
771 *
772 * If it's our own child, there is no notification to do.
773 * But if our normal children self-reap, then this child
774 * was prevented by ptrace and we must reap it now.
775 */
776 if (!task_detached(p) && thread_group_empty(p)) {
777 if (!same_thread_group(p->real_parent, parent))
778 do_notify_parent(p, p->exit_signal);
779 else {
780 if (ign < 0)
781 ign = ignoring_children(parent);
782 if (ign)
783 p->exit_signal = -1;
784 }
785 }
786
787 if (task_detached(p)) {
788 /*
789 * Mark it as in the process of being reaped.
790 */
791 p->exit_state = EXIT_DEAD;
792 list_add(&p->ptrace_entry, dead);
793 }
794 }
795}
796
797/*
798 * Finish up exit-time ptrace cleanup.
799 *
800 * Called without locks.
801 */
802static void ptrace_exit_finish(struct task_struct *parent,
803 struct list_head *dead)
804{
805 struct task_struct *p, *n;
806
807 BUG_ON(!list_empty(&parent->ptraced));
808
809 list_for_each_entry_safe(p, n, dead, ptrace_entry) {
810 list_del_init(&p->ptrace_entry);
811 release_task(p);
812 }
813}
814
815static void reparent_thread(struct task_struct *p, struct task_struct *father)
816{
817 if (p->pdeath_signal)
818 /* We already hold the tasklist_lock here. */
819 group_send_sig_info(p->pdeath_signal, SEND_SIG_NOINFO, p);
820
821 list_move_tail(&p->sibling, &p->real_parent->children);
822
823 /* If this is a threaded reparent there is no need to
824 * notify anyone anything has happened.
825 */
826 if (same_thread_group(p->real_parent, father))
827 return;
828
829 /* We don't want people slaying init. */
830 if (!task_detached(p))
831 p->exit_signal = SIGCHLD;
832
833 /* If we'd notified the old parent about this child's death,
834 * also notify the new parent.
835 */
836 if (!ptrace_reparented(p) &&
837 p->exit_state == EXIT_ZOMBIE &&
838 !task_detached(p) && thread_group_empty(p))
839 do_notify_parent(p, p->exit_signal);
840
841 kill_orphaned_pgrp(p, father);
842}
843
844/*
845 * When we die, we re-parent all our children. 698 * When we die, we re-parent all our children.
846 * Try to give them to another thread in our thread 699 * Try to give them to another thread in our thread
847 * group, and if no such member exists, give it to 700 * group, and if no such member exists, give it to
@@ -880,17 +733,51 @@ static struct task_struct *find_new_reaper(struct task_struct *father)
880 return pid_ns->child_reaper; 733 return pid_ns->child_reaper;
881} 734}
882 735
736/*
737* Any that need to be release_task'd are put on the @dead list.
738 */
739static void reparent_thread(struct task_struct *father, struct task_struct *p,
740 struct list_head *dead)
741{
742 if (p->pdeath_signal)
743 group_send_sig_info(p->pdeath_signal, SEND_SIG_NOINFO, p);
744
745 list_move_tail(&p->sibling, &p->real_parent->children);
746
747 if (task_detached(p))
748 return;
749 /*
750 * If this is a threaded reparent there is no need to
751 * notify anyone anything has happened.
752 */
753 if (same_thread_group(p->real_parent, father))
754 return;
755
756 /* We don't want people slaying init. */
757 p->exit_signal = SIGCHLD;
758
759 /* If it has exited notify the new parent about this child's death. */
760 if (!p->ptrace &&
761 p->exit_state == EXIT_ZOMBIE && thread_group_empty(p)) {
762 do_notify_parent(p, p->exit_signal);
763 if (task_detached(p)) {
764 p->exit_state = EXIT_DEAD;
765 list_move_tail(&p->sibling, dead);
766 }
767 }
768
769 kill_orphaned_pgrp(p, father);
770}
771
883static void forget_original_parent(struct task_struct *father) 772static void forget_original_parent(struct task_struct *father)
884{ 773{
885 struct task_struct *p, *n, *reaper; 774 struct task_struct *p, *n, *reaper;
886 LIST_HEAD(ptrace_dead); 775 LIST_HEAD(dead_children);
776
777 exit_ptrace(father);
887 778
888 write_lock_irq(&tasklist_lock); 779 write_lock_irq(&tasklist_lock);
889 reaper = find_new_reaper(father); 780 reaper = find_new_reaper(father);
890 /*
891 * First clean up ptrace if we were using it.
892 */
893 ptrace_exit(father, &ptrace_dead);
894 781
895 list_for_each_entry_safe(p, n, &father->children, sibling) { 782 list_for_each_entry_safe(p, n, &father->children, sibling) {
896 p->real_parent = reaper; 783 p->real_parent = reaper;
@@ -898,13 +785,16 @@ static void forget_original_parent(struct task_struct *father)
898 BUG_ON(p->ptrace); 785 BUG_ON(p->ptrace);
899 p->parent = p->real_parent; 786 p->parent = p->real_parent;
900 } 787 }
901 reparent_thread(p, father); 788 reparent_thread(father, p, &dead_children);
902 } 789 }
903
904 write_unlock_irq(&tasklist_lock); 790 write_unlock_irq(&tasklist_lock);
791
905 BUG_ON(!list_empty(&father->children)); 792 BUG_ON(!list_empty(&father->children));
906 793
907 ptrace_exit_finish(father, &ptrace_dead); 794 list_for_each_entry_safe(p, n, &dead_children, sibling) {
795 list_del_init(&p->sibling);
796 release_task(p);
797 }
908} 798}
909 799
910/* 800/*
@@ -947,8 +837,7 @@ static void exit_notify(struct task_struct *tsk, int group_dead)
947 */ 837 */
948 if (tsk->exit_signal != SIGCHLD && !task_detached(tsk) && 838 if (tsk->exit_signal != SIGCHLD && !task_detached(tsk) &&
949 (tsk->parent_exec_id != tsk->real_parent->self_exec_id || 839 (tsk->parent_exec_id != tsk->real_parent->self_exec_id ||
950 tsk->self_exec_id != tsk->parent_exec_id) && 840 tsk->self_exec_id != tsk->parent_exec_id))
951 !capable(CAP_KILL))
952 tsk->exit_signal = SIGCHLD; 841 tsk->exit_signal = SIGCHLD;
953 842
954 signal = tracehook_notify_death(tsk, &cookie, group_dead); 843 signal = tracehook_notify_death(tsk, &cookie, group_dead);
@@ -977,12 +866,9 @@ static void check_stack_usage(void)
977{ 866{
978 static DEFINE_SPINLOCK(low_water_lock); 867 static DEFINE_SPINLOCK(low_water_lock);
979 static int lowest_to_date = THREAD_SIZE; 868 static int lowest_to_date = THREAD_SIZE;
980 unsigned long *n = end_of_stack(current);
981 unsigned long free; 869 unsigned long free;
982 870
983 while (*n == 0) 871 free = stack_not_used(current);
984 n++;
985 free = (unsigned long)n - (unsigned long)end_of_stack(current);
986 872
987 if (free >= lowest_to_date) 873 if (free >= lowest_to_date)
988 return; 874 return;
@@ -1037,6 +923,8 @@ NORET_TYPE void do_exit(long code)
1037 schedule(); 923 schedule();
1038 } 924 }
1039 925
926 exit_irq_thread();
927
1040 exit_signals(tsk); /* sets PF_EXITING */ 928 exit_signals(tsk); /* sets PF_EXITING */
1041 /* 929 /*
1042 * tsk->flags are checked in the futex code to protect against 930 * tsk->flags are checked in the futex code to protect against
@@ -1417,6 +1305,18 @@ static int wait_task_zombie(struct task_struct *p, int options,
1417 return retval; 1305 return retval;
1418} 1306}
1419 1307
1308static int *task_stopped_code(struct task_struct *p, bool ptrace)
1309{
1310 if (ptrace) {
1311 if (task_is_stopped_or_traced(p))
1312 return &p->exit_code;
1313 } else {
1314 if (p->signal->flags & SIGNAL_STOP_STOPPED)
1315 return &p->signal->group_exit_code;
1316 }
1317 return NULL;
1318}
1319
1420/* 1320/*
1421 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold 1321 * Handle sys_wait4 work for one task in state TASK_STOPPED. We hold
1422 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold 1322 * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
@@ -1427,7 +1327,7 @@ static int wait_task_stopped(int ptrace, struct task_struct *p,
1427 int options, struct siginfo __user *infop, 1327 int options, struct siginfo __user *infop,
1428 int __user *stat_addr, struct rusage __user *ru) 1328 int __user *stat_addr, struct rusage __user *ru)
1429{ 1329{
1430 int retval, exit_code, why; 1330 int retval, exit_code, *p_code, why;
1431 uid_t uid = 0; /* unneeded, required by compiler */ 1331 uid_t uid = 0; /* unneeded, required by compiler */
1432 pid_t pid; 1332 pid_t pid;
1433 1333
@@ -1437,22 +1337,16 @@ static int wait_task_stopped(int ptrace, struct task_struct *p,
1437 exit_code = 0; 1337 exit_code = 0;
1438 spin_lock_irq(&p->sighand->siglock); 1338 spin_lock_irq(&p->sighand->siglock);
1439 1339
1440 if (unlikely(!task_is_stopped_or_traced(p))) 1340 p_code = task_stopped_code(p, ptrace);
1441 goto unlock_sig; 1341 if (unlikely(!p_code))
1442
1443 if (!ptrace && p->signal->group_stop_count > 0)
1444 /*
1445 * A group stop is in progress and this is the group leader.
1446 * We won't report until all threads have stopped.
1447 */
1448 goto unlock_sig; 1342 goto unlock_sig;
1449 1343
1450 exit_code = p->exit_code; 1344 exit_code = *p_code;
1451 if (!exit_code) 1345 if (!exit_code)
1452 goto unlock_sig; 1346 goto unlock_sig;
1453 1347
1454 if (!unlikely(options & WNOWAIT)) 1348 if (!unlikely(options & WNOWAIT))
1455 p->exit_code = 0; 1349 *p_code = 0;
1456 1350
1457 /* don't need the RCU readlock here as we're holding a spinlock */ 1351 /* don't need the RCU readlock here as we're holding a spinlock */
1458 uid = __task_cred(p)->uid; 1352 uid = __task_cred(p)->uid;
@@ -1608,7 +1502,7 @@ static int wait_consider_task(struct task_struct *parent, int ptrace,
1608 */ 1502 */
1609 *notask_error = 0; 1503 *notask_error = 0;
1610 1504
1611 if (task_is_stopped_or_traced(p)) 1505 if (task_stopped_code(p, ptrace))
1612 return wait_task_stopped(ptrace, p, options, 1506 return wait_task_stopped(ptrace, p, options,
1613 infop, stat_addr, ru); 1507 infop, stat_addr, ru);
1614 1508
@@ -1812,7 +1706,7 @@ SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
1812 pid = find_get_pid(-upid); 1706 pid = find_get_pid(-upid);
1813 } else if (upid == 0) { 1707 } else if (upid == 0) {
1814 type = PIDTYPE_PGID; 1708 type = PIDTYPE_PGID;
1815 pid = get_pid(task_pgrp(current)); 1709 pid = get_task_pid(current, PIDTYPE_PGID);
1816 } else /* upid > 0 */ { 1710 } else /* upid > 0 */ {
1817 type = PIDTYPE_PID; 1711 type = PIDTYPE_PID;
1818 pid = find_get_pid(upid); 1712 pid = find_get_pid(upid);