aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/exit.c
diff options
context:
space:
mode:
authorJonathan Herman <hermanjl@cs.unc.edu>2013-01-17 16:15:55 -0500
committerJonathan Herman <hermanjl@cs.unc.edu>2013-01-17 16:15:55 -0500
commit8dea78da5cee153b8af9c07a2745f6c55057fe12 (patch)
treea8f4d49d63b1ecc92f2fddceba0655b2472c5bd9 /kernel/exit.c
parent406089d01562f1e2bf9f089fd7637009ebaad589 (diff)
Patched in Tegra support.
Diffstat (limited to 'kernel/exit.c')
-rw-r--r--kernel/exit.c329
1 files changed, 253 insertions, 76 deletions
diff --git a/kernel/exit.c b/kernel/exit.c
index b4df2193721..9e316ae4984 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -51,8 +51,6 @@
51#include <trace/events/sched.h> 51#include <trace/events/sched.h>
52#include <linux/hw_breakpoint.h> 52#include <linux/hw_breakpoint.h>
53#include <linux/oom.h> 53#include <linux/oom.h>
54#include <linux/writeback.h>
55#include <linux/shm.h>
56 54
57#include <asm/uaccess.h> 55#include <asm/uaccess.h>
58#include <asm/unistd.h> 56#include <asm/unistd.h>
@@ -123,9 +121,9 @@ static void __exit_signal(struct task_struct *tsk)
123 * We won't ever get here for the group leader, since it 121 * We won't ever get here for the group leader, since it
124 * will have been the last reference on the signal_struct. 122 * will have been the last reference on the signal_struct.
125 */ 123 */
126 sig->utime += tsk->utime; 124 sig->utime = cputime_add(sig->utime, tsk->utime);
127 sig->stime += tsk->stime; 125 sig->stime = cputime_add(sig->stime, tsk->stime);
128 sig->gtime += tsk->gtime; 126 sig->gtime = cputime_add(sig->gtime, tsk->gtime);
129 sig->min_flt += tsk->min_flt; 127 sig->min_flt += tsk->min_flt;
130 sig->maj_flt += tsk->maj_flt; 128 sig->maj_flt += tsk->maj_flt;
131 sig->nvcsw += tsk->nvcsw; 129 sig->nvcsw += tsk->nvcsw;
@@ -310,6 +308,43 @@ kill_orphaned_pgrp(struct task_struct *tsk, struct task_struct *parent)
310 } 308 }
311} 309}
312 310
311/**
312 * reparent_to_kthreadd - Reparent the calling kernel thread to kthreadd
313 *
314 * If a kernel thread is launched as a result of a system call, or if
315 * it ever exits, it should generally reparent itself to kthreadd so it
316 * isn't in the way of other processes and is correctly cleaned up on exit.
317 *
318 * The various task state such as scheduling policy and priority may have
319 * been inherited from a user process, so we reset them to sane values here.
320 *
321 * NOTE that reparent_to_kthreadd() gives the caller full capabilities.
322 */
323static void reparent_to_kthreadd(void)
324{
325 write_lock_irq(&tasklist_lock);
326
327 ptrace_unlink(current);
328 /* Reparent to init */
329 current->real_parent = current->parent = kthreadd_task;
330 list_move_tail(&current->sibling, &current->real_parent->children);
331
332 /* Set the exit signal to SIGCHLD so we signal init on exit */
333 current->exit_signal = SIGCHLD;
334
335 if (task_nice(current) < 0)
336 set_user_nice(current, 0);
337 /* cpus_allowed? */
338 /* rt_priority? */
339 /* signals? */
340 memcpy(current->signal->rlim, init_task.signal->rlim,
341 sizeof(current->signal->rlim));
342
343 atomic_inc(&init_cred.usage);
344 commit_creds(&init_cred);
345 write_unlock_irq(&tasklist_lock);
346}
347
313void __set_special_pids(struct pid *pid) 348void __set_special_pids(struct pid *pid)
314{ 349{
315 struct task_struct *curr = current->group_leader; 350 struct task_struct *curr = current->group_leader;
@@ -321,6 +356,13 @@ void __set_special_pids(struct pid *pid)
321 change_pid(curr, PIDTYPE_PGID, pid); 356 change_pid(curr, PIDTYPE_PGID, pid);
322} 357}
323 358
359static void set_special_pids(struct pid *pid)
360{
361 write_lock_irq(&tasklist_lock);
362 __set_special_pids(pid);
363 write_unlock_irq(&tasklist_lock);
364}
365
324/* 366/*
325 * Let kernel threads use this to say that they allow a certain signal. 367 * Let kernel threads use this to say that they allow a certain signal.
326 * Must not be used if kthread was cloned with CLONE_SIGHAND. 368 * Must not be used if kthread was cloned with CLONE_SIGHAND.
@@ -360,6 +402,149 @@ int disallow_signal(int sig)
360 402
361EXPORT_SYMBOL(disallow_signal); 403EXPORT_SYMBOL(disallow_signal);
362 404
405/*
406 * Put all the gunge required to become a kernel thread without
407 * attached user resources in one place where it belongs.
408 */
409
410void daemonize(const char *name, ...)
411{
412 va_list args;
413 sigset_t blocked;
414
415 va_start(args, name);
416 vsnprintf(current->comm, sizeof(current->comm), name, args);
417 va_end(args);
418
419 /*
420 * If we were started as result of loading a module, close all of the
421 * user space pages. We don't need them, and if we didn't close them
422 * they would be locked into memory.
423 */
424 exit_mm(current);
425 /*
426 * We don't want to have TIF_FREEZE set if the system-wide hibernation
427 * or suspend transition begins right now.
428 */
429 current->flags |= (PF_NOFREEZE | PF_KTHREAD);
430
431 if (current->nsproxy != &init_nsproxy) {
432 get_nsproxy(&init_nsproxy);
433 switch_task_namespaces(current, &init_nsproxy);
434 }
435 set_special_pids(&init_struct_pid);
436 proc_clear_tty(current);
437
438 /* Block and flush all signals */
439 sigfillset(&blocked);
440 sigprocmask(SIG_BLOCK, &blocked, NULL);
441 flush_signals(current);
442
443 /* Become as one with the init task */
444
445 daemonize_fs_struct();
446 exit_files(current);
447 current->files = init_task.files;
448 atomic_inc(&current->files->count);
449
450 reparent_to_kthreadd();
451}
452
453EXPORT_SYMBOL(daemonize);
454
455static void close_files(struct files_struct * files)
456{
457 int i, j;
458 struct fdtable *fdt;
459
460 j = 0;
461
462 /*
463 * It is safe to dereference the fd table without RCU or
464 * ->file_lock because this is the last reference to the
465 * files structure. But use RCU to shut RCU-lockdep up.
466 */
467 rcu_read_lock();
468 fdt = files_fdtable(files);
469 rcu_read_unlock();
470 for (;;) {
471 unsigned long set;
472 i = j * __NFDBITS;
473 if (i >= fdt->max_fds)
474 break;
475 set = fdt->open_fds->fds_bits[j++];
476 while (set) {
477 if (set & 1) {
478 struct file * file = xchg(&fdt->fd[i], NULL);
479 if (file) {
480 filp_close(file, files);
481 cond_resched();
482 }
483 }
484 i++;
485 set >>= 1;
486 }
487 }
488}
489
490struct files_struct *get_files_struct(struct task_struct *task)
491{
492 struct files_struct *files;
493
494 task_lock(task);
495 files = task->files;
496 if (files)
497 atomic_inc(&files->count);
498 task_unlock(task);
499
500 return files;
501}
502
503void put_files_struct(struct files_struct *files)
504{
505 struct fdtable *fdt;
506
507 if (atomic_dec_and_test(&files->count)) {
508 close_files(files);
509 /*
510 * Free the fd and fdset arrays if we expanded them.
511 * If the fdtable was embedded, pass files for freeing
512 * at the end of the RCU grace period. Otherwise,
513 * you can free files immediately.
514 */
515 rcu_read_lock();
516 fdt = files_fdtable(files);
517 if (fdt != &files->fdtab)
518 kmem_cache_free(files_cachep, files);
519 free_fdtable(fdt);
520 rcu_read_unlock();
521 }
522}
523
524void reset_files_struct(struct files_struct *files)
525{
526 struct task_struct *tsk = current;
527 struct files_struct *old;
528
529 old = tsk->files;
530 task_lock(tsk);
531 tsk->files = files;
532 task_unlock(tsk);
533 put_files_struct(old);
534}
535
536void exit_files(struct task_struct *tsk)
537{
538 struct files_struct * files = tsk->files;
539
540 if (files) {
541 task_lock(tsk);
542 tsk->files = NULL;
543 task_unlock(tsk);
544 put_files_struct(files);
545 }
546}
547
363#ifdef CONFIG_MM_OWNER 548#ifdef CONFIG_MM_OWNER
364/* 549/*
365 * A task is exiting. If it owned this mm, find a new owner for the mm. 550 * A task is exiting. If it owned this mm, find a new owner for the mm.
@@ -456,7 +641,6 @@ static void exit_mm(struct task_struct * tsk)
456 mm_release(tsk, mm); 641 mm_release(tsk, mm);
457 if (!mm) 642 if (!mm)
458 return; 643 return;
459 sync_mm_rss(mm);
460 /* 644 /*
461 * Serialize with any possible pending coredump. 645 * Serialize with any possible pending coredump.
462 * We must hold mmap_sem around checking core_state 646 * We must hold mmap_sem around checking core_state
@@ -495,17 +679,21 @@ static void exit_mm(struct task_struct * tsk)
495 tsk->mm = NULL; 679 tsk->mm = NULL;
496 up_read(&mm->mmap_sem); 680 up_read(&mm->mmap_sem);
497 enter_lazy_tlb(mm, current); 681 enter_lazy_tlb(mm, current);
682 /* We don't want this task to be frozen prematurely */
683 clear_freeze_flag(tsk);
684 if (tsk->signal->oom_score_adj == OOM_SCORE_ADJ_MIN)
685 atomic_dec(&mm->oom_disable_count);
498 task_unlock(tsk); 686 task_unlock(tsk);
499 mm_update_next_owner(mm); 687 mm_update_next_owner(mm);
500 mmput(mm); 688 mmput(mm);
501} 689}
502 690
503/* 691/*
504 * When we die, we re-parent all our children, and try to: 692 * When we die, we re-parent all our children.
505 * 1. give them to another thread in our thread group, if such a member exists 693 * Try to give them to another thread in our thread
506 * 2. give it to the first ancestor process which prctl'd itself as a 694 * group, and if no such member exists, give it to
507 * child_subreaper for its children (like a service manager) 695 * the child reaper process (ie "init") in our pid
508 * 3. give it to the init process (PID 1) in our pid namespace 696 * space.
509 */ 697 */
510static struct task_struct *find_new_reaper(struct task_struct *father) 698static struct task_struct *find_new_reaper(struct task_struct *father)
511 __releases(&tasklist_lock) 699 __releases(&tasklist_lock)
@@ -525,37 +713,17 @@ static struct task_struct *find_new_reaper(struct task_struct *father)
525 713
526 if (unlikely(pid_ns->child_reaper == father)) { 714 if (unlikely(pid_ns->child_reaper == father)) {
527 write_unlock_irq(&tasklist_lock); 715 write_unlock_irq(&tasklist_lock);
528 if (unlikely(pid_ns == &init_pid_ns)) { 716 if (unlikely(pid_ns == &init_pid_ns))
529 panic("Attempted to kill init! exitcode=0x%08x\n", 717 panic("Attempted to kill init!");
530 father->signal->group_exit_code ?:
531 father->exit_code);
532 }
533 718
534 zap_pid_ns_processes(pid_ns); 719 zap_pid_ns_processes(pid_ns);
535 write_lock_irq(&tasklist_lock); 720 write_lock_irq(&tasklist_lock);
536 } else if (father->signal->has_child_subreaper) {
537 struct task_struct *reaper;
538
539 /* 721 /*
540 * Find the first ancestor marked as child_subreaper. 722 * We can not clear ->child_reaper or leave it alone.
541 * Note that the code below checks same_thread_group(reaper, 723 * There may by stealth EXIT_DEAD tasks on ->children,
542 * pid_ns->child_reaper). This is what we need to DTRT in a 724 * forget_original_parent() must move them somewhere.
543 * PID namespace. However we still need the check above, see
544 * http://marc.info/?l=linux-kernel&m=131385460420380
545 */ 725 */
546 for (reaper = father->real_parent; 726 pid_ns->child_reaper = init_pid_ns.child_reaper;
547 reaper != &init_task;
548 reaper = reaper->real_parent) {
549 if (same_thread_group(reaper, pid_ns->child_reaper))
550 break;
551 if (!reaper->signal->is_child_subreaper)
552 continue;
553 thread = reaper;
554 do {
555 if (!(thread->flags & PF_EXITING))
556 return reaper;
557 } while_each_thread(reaper, thread);
558 }
559 } 727 }
560 728
561 return pid_ns->child_reaper; 729 return pid_ns->child_reaper;
@@ -653,6 +821,25 @@ static void exit_notify(struct task_struct *tsk, int group_dead)
653 if (group_dead) 821 if (group_dead)
654 kill_orphaned_pgrp(tsk->group_leader, NULL); 822 kill_orphaned_pgrp(tsk->group_leader, NULL);
655 823
824 /* Let father know we died
825 *
826 * Thread signals are configurable, but you aren't going to use
827 * that to send signals to arbitrary processes.
828 * That stops right now.
829 *
830 * If the parent exec id doesn't match the exec id we saved
831 * when we started then we know the parent has changed security
832 * domain.
833 *
834 * If our self_exec id doesn't match our parent_exec_id then
835 * we have changed execution domain as these two values started
836 * the same after a fork.
837 */
838 if (thread_group_leader(tsk) && tsk->exit_signal != SIGCHLD &&
839 (tsk->parent_exec_id != tsk->real_parent->self_exec_id ||
840 tsk->self_exec_id != tsk->parent_exec_id))
841 tsk->exit_signal = SIGCHLD;
842
656 if (unlikely(tsk->ptrace)) { 843 if (unlikely(tsk->ptrace)) {
657 int sig = thread_group_leader(tsk) && 844 int sig = thread_group_leader(tsk) &&
658 thread_group_empty(tsk) && 845 thread_group_empty(tsk) &&
@@ -692,9 +879,9 @@ static void check_stack_usage(void)
692 879
693 spin_lock(&low_water_lock); 880 spin_lock(&low_water_lock);
694 if (free < lowest_to_date) { 881 if (free < lowest_to_date) {
695 printk(KERN_WARNING "%s (%d) used greatest stack depth: " 882 printk(KERN_WARNING "%s used greatest stack depth: %lu bytes "
696 "%lu bytes left\n", 883 "left\n",
697 current->comm, task_pid_nr(current), free); 884 current->comm, free);
698 lowest_to_date = free; 885 lowest_to_date = free;
699 } 886 }
700 spin_unlock(&low_water_lock); 887 spin_unlock(&low_water_lock);
@@ -703,7 +890,7 @@ static void check_stack_usage(void)
703static inline void check_stack_usage(void) {} 890static inline void check_stack_usage(void) {}
704#endif 891#endif
705 892
706void do_exit(long code) 893NORET_TYPE void do_exit(long code)
707{ 894{
708 struct task_struct *tsk = current; 895 struct task_struct *tsk = current;
709 int group_dead; 896 int group_dead;
@@ -751,6 +938,8 @@ void do_exit(long code)
751 schedule(); 938 schedule();
752 } 939 }
753 940
941 exit_irq_thread();
942
754 exit_signals(tsk); /* sets PF_EXITING */ 943 exit_signals(tsk); /* sets PF_EXITING */
755 /* 944 /*
756 * tsk->flags are checked in the futex code to protect against 945 * tsk->flags are checked in the futex code to protect against
@@ -767,7 +956,7 @@ void do_exit(long code)
767 acct_update_integrals(tsk); 956 acct_update_integrals(tsk);
768 /* sync mm's RSS info before statistics gathering */ 957 /* sync mm's RSS info before statistics gathering */
769 if (tsk->mm) 958 if (tsk->mm)
770 sync_mm_rss(tsk->mm); 959 sync_mm_rss(tsk, tsk->mm);
771 group_dead = atomic_dec_and_test(&tsk->signal->live); 960 group_dead = atomic_dec_and_test(&tsk->signal->live);
772 if (group_dead) { 961 if (group_dead) {
773 hrtimer_cancel(&tsk->signal->real_timer); 962 hrtimer_cancel(&tsk->signal->real_timer);
@@ -778,7 +967,8 @@ void do_exit(long code)
778 acct_collect(code, group_dead); 967 acct_collect(code, group_dead);
779 if (group_dead) 968 if (group_dead)
780 tty_audit_exit(); 969 tty_audit_exit();
781 audit_free(tsk); 970 if (unlikely(tsk->audit_context))
971 audit_free(tsk);
782 972
783 tsk->exit_code = code; 973 tsk->exit_code = code;
784 taskstats_exit(tsk, group_dead); 974 taskstats_exit(tsk, group_dead);
@@ -793,7 +983,6 @@ void do_exit(long code)
793 exit_shm(tsk); 983 exit_shm(tsk);
794 exit_files(tsk); 984 exit_files(tsk);
795 exit_fs(tsk); 985 exit_fs(tsk);
796 exit_task_work(tsk);
797 check_stack_usage(); 986 check_stack_usage();
798 exit_thread(); 987 exit_thread();
799 988
@@ -847,34 +1036,12 @@ void do_exit(long code)
847 if (tsk->splice_pipe) 1036 if (tsk->splice_pipe)
848 __free_pipe_info(tsk->splice_pipe); 1037 __free_pipe_info(tsk->splice_pipe);
849 1038
850 if (tsk->task_frag.page)
851 put_page(tsk->task_frag.page);
852
853 validate_creds_for_do_exit(tsk); 1039 validate_creds_for_do_exit(tsk);
854 1040
855 preempt_disable(); 1041 preempt_disable();
856 if (tsk->nr_dirtied)
857 __this_cpu_add(dirty_throttle_leaks, tsk->nr_dirtied);
858 exit_rcu(); 1042 exit_rcu();
859
860 /*
861 * The setting of TASK_RUNNING by try_to_wake_up() may be delayed
862 * when the following two conditions become true.
863 * - There is race condition of mmap_sem (It is acquired by
864 * exit_mm()), and
865 * - SMI occurs before setting TASK_RUNINNG.
866 * (or hypervisor of virtual machine switches to other guest)
867 * As a result, we may become TASK_RUNNING after becoming TASK_DEAD
868 *
869 * To avoid it, we have to wait for releasing tsk->pi_lock which
870 * is held by try_to_wake_up()
871 */
872 smp_mb();
873 raw_spin_unlock_wait(&tsk->pi_lock);
874
875 /* causes final put_task_struct in finish_task_switch(). */ 1043 /* causes final put_task_struct in finish_task_switch(). */
876 tsk->state = TASK_DEAD; 1044 tsk->state = TASK_DEAD;
877 tsk->flags |= PF_NOFREEZE; /* tell freezer to ignore us */
878 schedule(); 1045 schedule();
879 BUG(); 1046 BUG();
880 /* Avoid "noreturn function does return". */ 1047 /* Avoid "noreturn function does return". */
@@ -884,7 +1051,7 @@ void do_exit(long code)
884 1051
885EXPORT_SYMBOL_GPL(do_exit); 1052EXPORT_SYMBOL_GPL(do_exit);
886 1053
887void complete_and_exit(struct completion *comp, long code) 1054NORET_TYPE void complete_and_exit(struct completion *comp, long code)
888{ 1055{
889 if (comp) 1056 if (comp)
890 complete(comp); 1057 complete(comp);
@@ -903,7 +1070,7 @@ SYSCALL_DEFINE1(exit, int, error_code)
903 * Take down every thread in the group. This is called by fatal signals 1070 * Take down every thread in the group. This is called by fatal signals
904 * as well as by sys_exit_group (below). 1071 * as well as by sys_exit_group (below).
905 */ 1072 */
906void 1073NORET_TYPE void
907do_group_exit(int exit_code) 1074do_group_exit(int exit_code)
908{ 1075{
909 struct signal_struct *sig = current->signal; 1076 struct signal_struct *sig = current->signal;
@@ -1024,7 +1191,7 @@ static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
1024 unsigned long state; 1191 unsigned long state;
1025 int retval, status, traced; 1192 int retval, status, traced;
1026 pid_t pid = task_pid_vnr(p); 1193 pid_t pid = task_pid_vnr(p);
1027 uid_t uid = from_kuid_munged(current_user_ns(), task_uid(p)); 1194 uid_t uid = __task_cred(p)->uid;
1028 struct siginfo __user *infop; 1195 struct siginfo __user *infop;
1029 1196
1030 if (!likely(wo->wo_flags & WEXITED)) 1197 if (!likely(wo->wo_flags & WEXITED))
@@ -1082,17 +1249,27 @@ static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
1082 * as other threads in the parent group can be right 1249 * as other threads in the parent group can be right
1083 * here reaping other children at the same time. 1250 * here reaping other children at the same time.
1084 * 1251 *
1085 * We use thread_group_cputime_adjusted() to get times for the thread 1252 * We use thread_group_times() to get times for the thread
1086 * group, which consolidates times for all threads in the 1253 * group, which consolidates times for all threads in the
1087 * group including the group leader. 1254 * group including the group leader.
1088 */ 1255 */
1089 thread_group_cputime_adjusted(p, &tgutime, &tgstime); 1256 thread_group_times(p, &tgutime, &tgstime);
1090 spin_lock_irq(&p->real_parent->sighand->siglock); 1257 spin_lock_irq(&p->real_parent->sighand->siglock);
1091 psig = p->real_parent->signal; 1258 psig = p->real_parent->signal;
1092 sig = p->signal; 1259 sig = p->signal;
1093 psig->cutime += tgutime + sig->cutime; 1260 psig->cutime =
1094 psig->cstime += tgstime + sig->cstime; 1261 cputime_add(psig->cutime,
1095 psig->cgtime += p->gtime + sig->gtime + sig->cgtime; 1262 cputime_add(tgutime,
1263 sig->cutime));
1264 psig->cstime =
1265 cputime_add(psig->cstime,
1266 cputime_add(tgstime,
1267 sig->cstime));
1268 psig->cgtime =
1269 cputime_add(psig->cgtime,
1270 cputime_add(p->gtime,
1271 cputime_add(sig->gtime,
1272 sig->cgtime)));
1096 psig->cmin_flt += 1273 psig->cmin_flt +=
1097 p->min_flt + sig->min_flt + sig->cmin_flt; 1274 p->min_flt + sig->min_flt + sig->cmin_flt;
1098 psig->cmaj_flt += 1275 psig->cmaj_flt +=
@@ -1237,7 +1414,7 @@ static int wait_task_stopped(struct wait_opts *wo,
1237 if (!unlikely(wo->wo_flags & WNOWAIT)) 1414 if (!unlikely(wo->wo_flags & WNOWAIT))
1238 *p_code = 0; 1415 *p_code = 0;
1239 1416
1240 uid = from_kuid_munged(current_user_ns(), task_uid(p)); 1417 uid = task_uid(p);
1241unlock_sig: 1418unlock_sig:
1242 spin_unlock_irq(&p->sighand->siglock); 1419 spin_unlock_irq(&p->sighand->siglock);
1243 if (!exit_code) 1420 if (!exit_code)
@@ -1310,7 +1487,7 @@ static int wait_task_continued(struct wait_opts *wo, struct task_struct *p)
1310 } 1487 }
1311 if (!unlikely(wo->wo_flags & WNOWAIT)) 1488 if (!unlikely(wo->wo_flags & WNOWAIT))
1312 p->signal->flags &= ~SIGNAL_STOP_CONTINUED; 1489 p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
1313 uid = from_kuid_munged(current_user_ns(), task_uid(p)); 1490 uid = task_uid(p);
1314 spin_unlock_irq(&p->sighand->siglock); 1491 spin_unlock_irq(&p->sighand->siglock);
1315 1492
1316 pid = task_pid_vnr(p); 1493 pid = task_pid_vnr(p);