~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/exit.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /kernel/exit.c (Version linux-6.12-rc7) and /kernel/exit.c (Version linux-6.1.116)


  1 // SPDX-License-Identifier: GPL-2.0-only            1 // SPDX-License-Identifier: GPL-2.0-only
  2 /*                                                  2 /*
  3  *  linux/kernel/exit.c                             3  *  linux/kernel/exit.c
  4  *                                                  4  *
  5  *  Copyright (C) 1991, 1992  Linus Torvalds        5  *  Copyright (C) 1991, 1992  Linus Torvalds
  6  */                                                 6  */
  7                                                     7 
  8 #include <linux/mm.h>                               8 #include <linux/mm.h>
  9 #include <linux/slab.h>                             9 #include <linux/slab.h>
 10 #include <linux/sched/autogroup.h>                 10 #include <linux/sched/autogroup.h>
 11 #include <linux/sched/mm.h>                        11 #include <linux/sched/mm.h>
 12 #include <linux/sched/stat.h>                      12 #include <linux/sched/stat.h>
 13 #include <linux/sched/task.h>                      13 #include <linux/sched/task.h>
 14 #include <linux/sched/task_stack.h>                14 #include <linux/sched/task_stack.h>
 15 #include <linux/sched/cputime.h>                   15 #include <linux/sched/cputime.h>
 16 #include <linux/interrupt.h>                       16 #include <linux/interrupt.h>
 17 #include <linux/module.h>                          17 #include <linux/module.h>
 18 #include <linux/capability.h>                      18 #include <linux/capability.h>
 19 #include <linux/completion.h>                      19 #include <linux/completion.h>
 20 #include <linux/personality.h>                     20 #include <linux/personality.h>
 21 #include <linux/tty.h>                             21 #include <linux/tty.h>
 22 #include <linux/iocontext.h>                       22 #include <linux/iocontext.h>
 23 #include <linux/key.h>                             23 #include <linux/key.h>
 24 #include <linux/cpu.h>                             24 #include <linux/cpu.h>
 25 #include <linux/acct.h>                            25 #include <linux/acct.h>
 26 #include <linux/tsacct_kern.h>                     26 #include <linux/tsacct_kern.h>
 27 #include <linux/file.h>                            27 #include <linux/file.h>
 28 #include <linux/fdtable.h>                         28 #include <linux/fdtable.h>
 29 #include <linux/freezer.h>                         29 #include <linux/freezer.h>
 30 #include <linux/binfmts.h>                         30 #include <linux/binfmts.h>
 31 #include <linux/nsproxy.h>                         31 #include <linux/nsproxy.h>
 32 #include <linux/pid_namespace.h>                   32 #include <linux/pid_namespace.h>
 33 #include <linux/ptrace.h>                          33 #include <linux/ptrace.h>
 34 #include <linux/profile.h>                         34 #include <linux/profile.h>
 35 #include <linux/mount.h>                           35 #include <linux/mount.h>
 36 #include <linux/proc_fs.h>                         36 #include <linux/proc_fs.h>
 37 #include <linux/kthread.h>                         37 #include <linux/kthread.h>
 38 #include <linux/mempolicy.h>                       38 #include <linux/mempolicy.h>
 39 #include <linux/taskstats_kern.h>                  39 #include <linux/taskstats_kern.h>
 40 #include <linux/delayacct.h>                       40 #include <linux/delayacct.h>
 41 #include <linux/cgroup.h>                          41 #include <linux/cgroup.h>
 42 #include <linux/syscalls.h>                        42 #include <linux/syscalls.h>
 43 #include <linux/signal.h>                          43 #include <linux/signal.h>
 44 #include <linux/posix-timers.h>                    44 #include <linux/posix-timers.h>
 45 #include <linux/cn_proc.h>                         45 #include <linux/cn_proc.h>
 46 #include <linux/mutex.h>                           46 #include <linux/mutex.h>
 47 #include <linux/futex.h>                           47 #include <linux/futex.h>
 48 #include <linux/pipe_fs_i.h>                       48 #include <linux/pipe_fs_i.h>
 49 #include <linux/audit.h> /* for audit_free() *     49 #include <linux/audit.h> /* for audit_free() */
 50 #include <linux/resource.h>                        50 #include <linux/resource.h>
 51 #include <linux/task_io_accounting_ops.h>          51 #include <linux/task_io_accounting_ops.h>
 52 #include <linux/blkdev.h>                          52 #include <linux/blkdev.h>
 53 #include <linux/task_work.h>                       53 #include <linux/task_work.h>
 54 #include <linux/fs_struct.h>                       54 #include <linux/fs_struct.h>
 55 #include <linux/init_task.h>                       55 #include <linux/init_task.h>
 56 #include <linux/perf_event.h>                      56 #include <linux/perf_event.h>
 57 #include <trace/events/sched.h>                    57 #include <trace/events/sched.h>
 58 #include <linux/hw_breakpoint.h>                   58 #include <linux/hw_breakpoint.h>
 59 #include <linux/oom.h>                             59 #include <linux/oom.h>
 60 #include <linux/writeback.h>                       60 #include <linux/writeback.h>
 61 #include <linux/shm.h>                             61 #include <linux/shm.h>
 62 #include <linux/kcov.h>                            62 #include <linux/kcov.h>
 63 #include <linux/kmsan.h>                           63 #include <linux/kmsan.h>
 64 #include <linux/random.h>                          64 #include <linux/random.h>
 65 #include <linux/rcuwait.h>                         65 #include <linux/rcuwait.h>
 66 #include <linux/compat.h>                          66 #include <linux/compat.h>
 67 #include <linux/io_uring.h>                        67 #include <linux/io_uring.h>
 68 #include <linux/kprobes.h>                         68 #include <linux/kprobes.h>
 69 #include <linux/rethook.h>                         69 #include <linux/rethook.h>
 70 #include <linux/sysfs.h>                           70 #include <linux/sysfs.h>
 71 #include <linux/user_events.h>                 << 
 72 #include <linux/uaccess.h>                     << 
 73                                                << 
 74 #include <uapi/linux/wait.h>                   << 
 75                                                    71 
                                                   >>  72 #include <linux/uaccess.h>
 76 #include <asm/unistd.h>                            73 #include <asm/unistd.h>
 77 #include <asm/mmu_context.h>                       74 #include <asm/mmu_context.h>
 78                                                    75 
 79 #include "exit.h"                              << 
 80                                                << 
 81 /*                                                 76 /*
 82  * The default value should be high enough to      77  * The default value should be high enough to not crash a system that randomly
 83  * crashes its kernel from time to time, but l     78  * crashes its kernel from time to time, but low enough to at least not permit
 84  * overflowing 32-bit refcounts or the ldsem w     79  * overflowing 32-bit refcounts or the ldsem writer count.
 85  */                                                80  */
 86 static unsigned int oops_limit = 10000;            81 static unsigned int oops_limit = 10000;
 87                                                    82 
 88 #ifdef CONFIG_SYSCTL                               83 #ifdef CONFIG_SYSCTL
 89 static struct ctl_table kern_exit_table[] = {      84 static struct ctl_table kern_exit_table[] = {
 90         {                                          85         {
 91                 .procname       = "oops_limit"     86                 .procname       = "oops_limit",
 92                 .data           = &oops_limit,     87                 .data           = &oops_limit,
 93                 .maxlen         = sizeof(oops_     88                 .maxlen         = sizeof(oops_limit),
 94                 .mode           = 0644,            89                 .mode           = 0644,
 95                 .proc_handler   = proc_douintv     90                 .proc_handler   = proc_douintvec,
 96         },                                         91         },
                                                   >>  92         { }
 97 };                                                 93 };
 98                                                    94 
 99 static __init int kernel_exit_sysctls_init(voi     95 static __init int kernel_exit_sysctls_init(void)
100 {                                                  96 {
101         register_sysctl_init("kernel", kern_ex     97         register_sysctl_init("kernel", kern_exit_table);
102         return 0;                                  98         return 0;
103 }                                                  99 }
104 late_initcall(kernel_exit_sysctls_init);          100 late_initcall(kernel_exit_sysctls_init);
105 #endif                                            101 #endif
106                                                   102 
107 static atomic_t oops_count = ATOMIC_INIT(0);      103 static atomic_t oops_count = ATOMIC_INIT(0);
108                                                   104 
109 #ifdef CONFIG_SYSFS                               105 #ifdef CONFIG_SYSFS
110 static ssize_t oops_count_show(struct kobject     106 static ssize_t oops_count_show(struct kobject *kobj, struct kobj_attribute *attr,
111                                char *page)        107                                char *page)
112 {                                                 108 {
113         return sysfs_emit(page, "%d\n", atomic    109         return sysfs_emit(page, "%d\n", atomic_read(&oops_count));
114 }                                                 110 }
115                                                   111 
116 static struct kobj_attribute oops_count_attr =    112 static struct kobj_attribute oops_count_attr = __ATTR_RO(oops_count);
117                                                   113 
118 static __init int kernel_exit_sysfs_init(void)    114 static __init int kernel_exit_sysfs_init(void)
119 {                                                 115 {
120         sysfs_add_file_to_group(kernel_kobj, &    116         sysfs_add_file_to_group(kernel_kobj, &oops_count_attr.attr, NULL);
121         return 0;                                 117         return 0;
122 }                                                 118 }
123 late_initcall(kernel_exit_sysfs_init);            119 late_initcall(kernel_exit_sysfs_init);
124 #endif                                            120 #endif
125                                                   121 
126 static void __unhash_process(struct task_struc    122 static void __unhash_process(struct task_struct *p, bool group_dead)
127 {                                                 123 {
128         nr_threads--;                             124         nr_threads--;
129         detach_pid(p, PIDTYPE_PID);               125         detach_pid(p, PIDTYPE_PID);
130         if (group_dead) {                         126         if (group_dead) {
131                 detach_pid(p, PIDTYPE_TGID);      127                 detach_pid(p, PIDTYPE_TGID);
132                 detach_pid(p, PIDTYPE_PGID);      128                 detach_pid(p, PIDTYPE_PGID);
133                 detach_pid(p, PIDTYPE_SID);       129                 detach_pid(p, PIDTYPE_SID);
134                                                   130 
135                 list_del_rcu(&p->tasks);          131                 list_del_rcu(&p->tasks);
136                 list_del_init(&p->sibling);       132                 list_del_init(&p->sibling);
137                 __this_cpu_dec(process_counts)    133                 __this_cpu_dec(process_counts);
138         }                                         134         }
                                                   >> 135         list_del_rcu(&p->thread_group);
139         list_del_rcu(&p->thread_node);            136         list_del_rcu(&p->thread_node);
140 }                                                 137 }
141                                                   138 
142 /*                                                139 /*
143  * This function expects the tasklist_lock wri    140  * This function expects the tasklist_lock write-locked.
144  */                                               141  */
145 static void __exit_signal(struct task_struct *    142 static void __exit_signal(struct task_struct *tsk)
146 {                                                 143 {
147         struct signal_struct *sig = tsk->signa    144         struct signal_struct *sig = tsk->signal;
148         bool group_dead = thread_group_leader(    145         bool group_dead = thread_group_leader(tsk);
149         struct sighand_struct *sighand;           146         struct sighand_struct *sighand;
150         struct tty_struct *tty;                   147         struct tty_struct *tty;
151         u64 utime, stime;                         148         u64 utime, stime;
152                                                   149 
153         sighand = rcu_dereference_check(tsk->s    150         sighand = rcu_dereference_check(tsk->sighand,
154                                         lockde    151                                         lockdep_tasklist_lock_is_held());
155         spin_lock(&sighand->siglock);             152         spin_lock(&sighand->siglock);
156                                                   153 
157 #ifdef CONFIG_POSIX_TIMERS                        154 #ifdef CONFIG_POSIX_TIMERS
158         posix_cpu_timers_exit(tsk);               155         posix_cpu_timers_exit(tsk);
159         if (group_dead)                           156         if (group_dead)
160                 posix_cpu_timers_exit_group(ts    157                 posix_cpu_timers_exit_group(tsk);
161 #endif                                            158 #endif
162                                                   159 
163         if (group_dead) {                         160         if (group_dead) {
164                 tty = sig->tty;                   161                 tty = sig->tty;
165                 sig->tty = NULL;                  162                 sig->tty = NULL;
166         } else {                                  163         } else {
167                 /*                                164                 /*
168                  * If there is any task waitin    165                  * If there is any task waiting for the group exit
169                  * then notify it:                166                  * then notify it:
170                  */                               167                  */
171                 if (sig->notify_count > 0 && !    168                 if (sig->notify_count > 0 && !--sig->notify_count)
172                         wake_up_process(sig->g    169                         wake_up_process(sig->group_exec_task);
173                                                   170 
174                 if (tsk == sig->curr_target)      171                 if (tsk == sig->curr_target)
175                         sig->curr_target = nex    172                         sig->curr_target = next_thread(tsk);
176         }                                         173         }
177                                                   174 
178         add_device_randomness((const void*) &t    175         add_device_randomness((const void*) &tsk->se.sum_exec_runtime,
179                               sizeof(unsigned     176                               sizeof(unsigned long long));
180                                                   177 
181         /*                                        178         /*
182          * Accumulate here the counters for al    179          * Accumulate here the counters for all threads as they die. We could
183          * skip the group leader because it is    180          * skip the group leader because it is the last user of signal_struct,
184          * but we want to avoid the race with     181          * but we want to avoid the race with thread_group_cputime() which can
185          * see the empty ->thread_head list.      182          * see the empty ->thread_head list.
186          */                                       183          */
187         task_cputime(tsk, &utime, &stime);        184         task_cputime(tsk, &utime, &stime);
188         write_seqlock(&sig->stats_lock);          185         write_seqlock(&sig->stats_lock);
189         sig->utime += utime;                      186         sig->utime += utime;
190         sig->stime += stime;                      187         sig->stime += stime;
191         sig->gtime += task_gtime(tsk);            188         sig->gtime += task_gtime(tsk);
192         sig->min_flt += tsk->min_flt;             189         sig->min_flt += tsk->min_flt;
193         sig->maj_flt += tsk->maj_flt;             190         sig->maj_flt += tsk->maj_flt;
194         sig->nvcsw += tsk->nvcsw;                 191         sig->nvcsw += tsk->nvcsw;
195         sig->nivcsw += tsk->nivcsw;               192         sig->nivcsw += tsk->nivcsw;
196         sig->inblock += task_io_get_inblock(ts    193         sig->inblock += task_io_get_inblock(tsk);
197         sig->oublock += task_io_get_oublock(ts    194         sig->oublock += task_io_get_oublock(tsk);
198         task_io_accounting_add(&sig->ioac, &ts    195         task_io_accounting_add(&sig->ioac, &tsk->ioac);
199         sig->sum_sched_runtime += tsk->se.sum_    196         sig->sum_sched_runtime += tsk->se.sum_exec_runtime;
200         sig->nr_threads--;                        197         sig->nr_threads--;
201         __unhash_process(tsk, group_dead);        198         __unhash_process(tsk, group_dead);
202         write_sequnlock(&sig->stats_lock);        199         write_sequnlock(&sig->stats_lock);
203                                                   200 
204         /*                                        201         /*
205          * Do this under ->siglock, we can rac    202          * Do this under ->siglock, we can race with another thread
206          * doing sigqueue_free() if we have SI    203          * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.
207          */                                       204          */
208         flush_sigqueue(&tsk->pending);            205         flush_sigqueue(&tsk->pending);
209         tsk->sighand = NULL;                      206         tsk->sighand = NULL;
210         spin_unlock(&sighand->siglock);           207         spin_unlock(&sighand->siglock);
211                                                   208 
212         __cleanup_sighand(sighand);               209         __cleanup_sighand(sighand);
213         clear_tsk_thread_flag(tsk, TIF_SIGPEND    210         clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
214         if (group_dead) {                         211         if (group_dead) {
215                 flush_sigqueue(&sig->shared_pe    212                 flush_sigqueue(&sig->shared_pending);
216                 tty_kref_put(tty);                213                 tty_kref_put(tty);
217         }                                         214         }
218 }                                                 215 }
219                                                   216 
220 static void delayed_put_task_struct(struct rcu    217 static void delayed_put_task_struct(struct rcu_head *rhp)
221 {                                                 218 {
222         struct task_struct *tsk = container_of    219         struct task_struct *tsk = container_of(rhp, struct task_struct, rcu);
223                                                   220 
224         kprobe_flush_task(tsk);                   221         kprobe_flush_task(tsk);
225         rethook_flush_task(tsk);                  222         rethook_flush_task(tsk);
226         perf_event_delayed_put(tsk);              223         perf_event_delayed_put(tsk);
227         trace_sched_process_free(tsk);            224         trace_sched_process_free(tsk);
228         put_task_struct(tsk);                     225         put_task_struct(tsk);
229 }                                                 226 }
230                                                   227 
231 void put_task_struct_rcu_user(struct task_stru    228 void put_task_struct_rcu_user(struct task_struct *task)
232 {                                                 229 {
233         if (refcount_dec_and_test(&task->rcu_u    230         if (refcount_dec_and_test(&task->rcu_users))
234                 call_rcu(&task->rcu, delayed_p    231                 call_rcu(&task->rcu, delayed_put_task_struct);
235 }                                                 232 }
236                                                   233 
237 void __weak release_thread(struct task_struct     234 void __weak release_thread(struct task_struct *dead_task)
238 {                                                 235 {
239 }                                                 236 }
240                                                   237 
241 void release_task(struct task_struct *p)          238 void release_task(struct task_struct *p)
242 {                                                 239 {
243         struct task_struct *leader;               240         struct task_struct *leader;
244         struct pid *thread_pid;                   241         struct pid *thread_pid;
245         int zap_leader;                           242         int zap_leader;
246 repeat:                                           243 repeat:
247         /* don't need to get the RCU readlock     244         /* don't need to get the RCU readlock here - the process is dead and
248          * can't be modifying its own credenti    245          * can't be modifying its own credentials. But shut RCU-lockdep up */
249         rcu_read_lock();                          246         rcu_read_lock();
250         dec_rlimit_ucounts(task_ucounts(p), UC    247         dec_rlimit_ucounts(task_ucounts(p), UCOUNT_RLIMIT_NPROC, 1);
251         rcu_read_unlock();                        248         rcu_read_unlock();
252                                                   249 
253         cgroup_release(p);                        250         cgroup_release(p);
254                                                   251 
255         write_lock_irq(&tasklist_lock);           252         write_lock_irq(&tasklist_lock);
256         ptrace_release_task(p);                   253         ptrace_release_task(p);
257         thread_pid = get_pid(p->thread_pid);      254         thread_pid = get_pid(p->thread_pid);
258         __exit_signal(p);                         255         __exit_signal(p);
259                                                   256 
260         /*                                        257         /*
261          * If we are the last non-leader membe    258          * If we are the last non-leader member of the thread
262          * group, and the leader is zombie, th    259          * group, and the leader is zombie, then notify the
263          * group leader's parent process. (if     260          * group leader's parent process. (if it wants notification.)
264          */                                       261          */
265         zap_leader = 0;                           262         zap_leader = 0;
266         leader = p->group_leader;                 263         leader = p->group_leader;
267         if (leader != p && thread_group_empty(    264         if (leader != p && thread_group_empty(leader)
268                         && leader->exit_state     265                         && leader->exit_state == EXIT_ZOMBIE) {
269                 /*                                266                 /*
270                  * If we were the last child t    267                  * If we were the last child thread and the leader has
271                  * exited already, and the lea    268                  * exited already, and the leader's parent ignores SIGCHLD,
272                  * then we are the one who sho    269                  * then we are the one who should release the leader.
273                  */                               270                  */
274                 zap_leader = do_notify_parent(    271                 zap_leader = do_notify_parent(leader, leader->exit_signal);
275                 if (zap_leader)                   272                 if (zap_leader)
276                         leader->exit_state = E    273                         leader->exit_state = EXIT_DEAD;
277         }                                         274         }
278                                                   275 
279         write_unlock_irq(&tasklist_lock);         276         write_unlock_irq(&tasklist_lock);
                                                   >> 277         seccomp_filter_release(p);
280         proc_flush_pid(thread_pid);               278         proc_flush_pid(thread_pid);
281         put_pid(thread_pid);                      279         put_pid(thread_pid);
282         release_thread(p);                        280         release_thread(p);
283         put_task_struct_rcu_user(p);              281         put_task_struct_rcu_user(p);
284                                                   282 
285         p = leader;                               283         p = leader;
286         if (unlikely(zap_leader))                 284         if (unlikely(zap_leader))
287                 goto repeat;                      285                 goto repeat;
288 }                                                 286 }
289                                                   287 
290 int rcuwait_wake_up(struct rcuwait *w)            288 int rcuwait_wake_up(struct rcuwait *w)
291 {                                                 289 {
292         int ret = 0;                              290         int ret = 0;
293         struct task_struct *task;                 291         struct task_struct *task;
294                                                   292 
295         rcu_read_lock();                          293         rcu_read_lock();
296                                                   294 
297         /*                                        295         /*
298          * Order condition vs @task, such that    296          * Order condition vs @task, such that everything prior to the load
299          * of @task is visible. This is the co    297          * of @task is visible. This is the condition as to why the user called
300          * rcuwait_wake() in the first place.     298          * rcuwait_wake() in the first place. Pairs with set_current_state()
301          * barrier (A) in rcuwait_wait_event()    299          * barrier (A) in rcuwait_wait_event().
302          *                                        300          *
303          *    WAIT                WAKE            301          *    WAIT                WAKE
304          *    [S] tsk = current   [S] cond = t    302          *    [S] tsk = current   [S] cond = true
305          *        MB (A)              MB (B)      303          *        MB (A)              MB (B)
306          *    [L] cond            [L] tsk         304          *    [L] cond            [L] tsk
307          */                                       305          */
308         smp_mb(); /* (B) */                       306         smp_mb(); /* (B) */
309                                                   307 
310         task = rcu_dereference(w->task);          308         task = rcu_dereference(w->task);
311         if (task)                                 309         if (task)
312                 ret = wake_up_process(task);      310                 ret = wake_up_process(task);
313         rcu_read_unlock();                        311         rcu_read_unlock();
314                                                   312 
315         return ret;                               313         return ret;
316 }                                                 314 }
317 EXPORT_SYMBOL_GPL(rcuwait_wake_up);               315 EXPORT_SYMBOL_GPL(rcuwait_wake_up);
318                                                   316 
319 /*                                                317 /*
320  * Determine if a process group is "orphaned",    318  * Determine if a process group is "orphaned", according to the POSIX
321  * definition in 2.2.2.52.  Orphaned process g    319  * definition in 2.2.2.52.  Orphaned process groups are not to be affected
322  * by terminal-generated stop signals.  Newly     320  * by terminal-generated stop signals.  Newly orphaned process groups are
323  * to receive a SIGHUP and a SIGCONT.             321  * to receive a SIGHUP and a SIGCONT.
324  *                                                322  *
325  * "I ask you, have you ever known what it is     323  * "I ask you, have you ever known what it is to be an orphan?"
326  */                                               324  */
327 static int will_become_orphaned_pgrp(struct pi    325 static int will_become_orphaned_pgrp(struct pid *pgrp,
328                                         struct    326                                         struct task_struct *ignored_task)
329 {                                                 327 {
330         struct task_struct *p;                    328         struct task_struct *p;
331                                                   329 
332         do_each_pid_task(pgrp, PIDTYPE_PGID, p    330         do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
333                 if ((p == ignored_task) ||        331                 if ((p == ignored_task) ||
334                     (p->exit_state && thread_g    332                     (p->exit_state && thread_group_empty(p)) ||
335                     is_global_init(p->real_par    333                     is_global_init(p->real_parent))
336                         continue;                 334                         continue;
337                                                   335 
338                 if (task_pgrp(p->real_parent)     336                 if (task_pgrp(p->real_parent) != pgrp &&
339                     task_session(p->real_paren    337                     task_session(p->real_parent) == task_session(p))
340                         return 0;                 338                         return 0;
341         } while_each_pid_task(pgrp, PIDTYPE_PG    339         } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
342                                                   340 
343         return 1;                                 341         return 1;
344 }                                                 342 }
345                                                   343 
346 int is_current_pgrp_orphaned(void)                344 int is_current_pgrp_orphaned(void)
347 {                                                 345 {
348         int retval;                               346         int retval;
349                                                   347 
350         read_lock(&tasklist_lock);                348         read_lock(&tasklist_lock);
351         retval = will_become_orphaned_pgrp(tas    349         retval = will_become_orphaned_pgrp(task_pgrp(current), NULL);
352         read_unlock(&tasklist_lock);              350         read_unlock(&tasklist_lock);
353                                                   351 
354         return retval;                            352         return retval;
355 }                                                 353 }
356                                                   354 
357 static bool has_stopped_jobs(struct pid *pgrp)    355 static bool has_stopped_jobs(struct pid *pgrp)
358 {                                                 356 {
359         struct task_struct *p;                    357         struct task_struct *p;
360                                                   358 
361         do_each_pid_task(pgrp, PIDTYPE_PGID, p    359         do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
362                 if (p->signal->flags & SIGNAL_    360                 if (p->signal->flags & SIGNAL_STOP_STOPPED)
363                         return true;              361                         return true;
364         } while_each_pid_task(pgrp, PIDTYPE_PG    362         } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
365                                                   363 
366         return false;                             364         return false;
367 }                                                 365 }
368                                                   366 
369 /*                                                367 /*
370  * Check to see if any process groups have bec    368  * Check to see if any process groups have become orphaned as
371  * a result of our exiting, and if they have a    369  * a result of our exiting, and if they have any stopped jobs,
372  * send them a SIGHUP and then a SIGCONT. (POS    370  * send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
373  */                                               371  */
374 static void                                       372 static void
375 kill_orphaned_pgrp(struct task_struct *tsk, st    373 kill_orphaned_pgrp(struct task_struct *tsk, struct task_struct *parent)
376 {                                                 374 {
377         struct pid *pgrp = task_pgrp(tsk);        375         struct pid *pgrp = task_pgrp(tsk);
378         struct task_struct *ignored_task = tsk    376         struct task_struct *ignored_task = tsk;
379                                                   377 
380         if (!parent)                              378         if (!parent)
381                 /* exit: our father is in a di    379                 /* exit: our father is in a different pgrp than
382                  * we are and we were the only    380                  * we are and we were the only connection outside.
383                  */                               381                  */
384                 parent = tsk->real_parent;        382                 parent = tsk->real_parent;
385         else                                      383         else
386                 /* reparent: our child is in a    384                 /* reparent: our child is in a different pgrp than
387                  * we are, and it was the only    385                  * we are, and it was the only connection outside.
388                  */                               386                  */
389                 ignored_task = NULL;              387                 ignored_task = NULL;
390                                                   388 
391         if (task_pgrp(parent) != pgrp &&          389         if (task_pgrp(parent) != pgrp &&
392             task_session(parent) == task_sessi    390             task_session(parent) == task_session(tsk) &&
393             will_become_orphaned_pgrp(pgrp, ig    391             will_become_orphaned_pgrp(pgrp, ignored_task) &&
394             has_stopped_jobs(pgrp)) {             392             has_stopped_jobs(pgrp)) {
395                 __kill_pgrp_info(SIGHUP, SEND_    393                 __kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp);
396                 __kill_pgrp_info(SIGCONT, SEND    394                 __kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp);
397         }                                         395         }
398 }                                                 396 }
399                                                   397 
400 static void coredump_task_exit(struct task_str    398 static void coredump_task_exit(struct task_struct *tsk)
401 {                                                 399 {
402         struct core_state *core_state;            400         struct core_state *core_state;
403                                                   401 
404         /*                                        402         /*
405          * Serialize with any possible pending    403          * Serialize with any possible pending coredump.
406          * We must hold siglock around checkin    404          * We must hold siglock around checking core_state
407          * and setting PF_POSTCOREDUMP.  The c    405          * and setting PF_POSTCOREDUMP.  The core-inducing thread
408          * will increment ->nr_threads for eac    406          * will increment ->nr_threads for each thread in the
409          * group without PF_POSTCOREDUMP set.     407          * group without PF_POSTCOREDUMP set.
410          */                                       408          */
411         spin_lock_irq(&tsk->sighand->siglock);    409         spin_lock_irq(&tsk->sighand->siglock);
412         tsk->flags |= PF_POSTCOREDUMP;            410         tsk->flags |= PF_POSTCOREDUMP;
413         core_state = tsk->signal->core_state;     411         core_state = tsk->signal->core_state;
414         spin_unlock_irq(&tsk->sighand->siglock    412         spin_unlock_irq(&tsk->sighand->siglock);
415         if (core_state) {                         413         if (core_state) {
416                 struct core_thread self;          414                 struct core_thread self;
417                                                   415 
418                 self.task = current;              416                 self.task = current;
419                 if (self.task->flags & PF_SIGN    417                 if (self.task->flags & PF_SIGNALED)
420                         self.next = xchg(&core    418                         self.next = xchg(&core_state->dumper.next, &self);
421                 else                              419                 else
422                         self.task = NULL;         420                         self.task = NULL;
423                 /*                                421                 /*
424                  * Implies mb(), the result of    422                  * Implies mb(), the result of xchg() must be visible
425                  * to core_state->dumper.         423                  * to core_state->dumper.
426                  */                               424                  */
427                 if (atomic_dec_and_test(&core_    425                 if (atomic_dec_and_test(&core_state->nr_threads))
428                         complete(&core_state->    426                         complete(&core_state->startup);
429                                                   427 
430                 for (;;) {                        428                 for (;;) {
431                         set_current_state(TASK !! 429                         set_current_state(TASK_UNINTERRUPTIBLE|TASK_FREEZABLE);
432                         if (!self.task) /* see    430                         if (!self.task) /* see coredump_finish() */
433                                 break;            431                                 break;
434                         schedule();               432                         schedule();
435                 }                                 433                 }
436                 __set_current_state(TASK_RUNNI    434                 __set_current_state(TASK_RUNNING);
437         }                                         435         }
438 }                                                 436 }
439                                                   437 
440 #ifdef CONFIG_MEMCG                               438 #ifdef CONFIG_MEMCG
441 /* drops tasklist_lock if succeeds */          << 
442 static bool __try_to_set_owner(struct task_str << 
443 {                                              << 
444         bool ret = false;                      << 
445                                                << 
446         task_lock(tsk);                        << 
447         if (likely(tsk->mm == mm)) {           << 
448                 /* tsk can't pass exit_mm/exec << 
449                 read_unlock(&tasklist_lock);   << 
450                 WRITE_ONCE(mm->owner, tsk);    << 
451                 lru_gen_migrate_mm(mm);        << 
452                 ret = true;                    << 
453         }                                      << 
454         task_unlock(tsk);                      << 
455         return ret;                            << 
456 }                                              << 
457                                                << 
458 static bool try_to_set_owner(struct task_struc << 
459 {                                              << 
460         struct task_struct *t;                 << 
461                                                << 
462         for_each_thread(g, t) {                << 
463                 struct mm_struct *t_mm = READ_ << 
464                 if (t_mm == mm) {              << 
465                         if (__try_to_set_owner << 
466                                 return true;   << 
467                 } else if (t_mm)               << 
468                         break;                 << 
469         }                                      << 
470                                                << 
471         return false;                          << 
472 }                                              << 
473                                                << 
474 /*                                                439 /*
475  * A task is exiting.   If it owned this mm, f    440  * A task is exiting.   If it owned this mm, find a new owner for the mm.
476  */                                               441  */
477 void mm_update_next_owner(struct mm_struct *mm    442 void mm_update_next_owner(struct mm_struct *mm)
478 {                                                 443 {
479         struct task_struct *g, *p = current;   !! 444         struct task_struct *c, *g, *p = current;
480                                                   445 
                                                   >> 446 retry:
481         /*                                        447         /*
482          * If the exiting or execing task is n    448          * If the exiting or execing task is not the owner, it's
483          * someone else's problem.                449          * someone else's problem.
484          */                                       450          */
485         if (mm->owner != p)                       451         if (mm->owner != p)
486                 return;                           452                 return;
487         /*                                        453         /*
488          * The current owner is exiting/execin    454          * The current owner is exiting/execing and there are no other
489          * candidates.  Do not leave the mm po    455          * candidates.  Do not leave the mm pointing to a possibly
490          * freed task structure.                  456          * freed task structure.
491          */                                       457          */
492         if (atomic_read(&mm->mm_users) <= 1) {    458         if (atomic_read(&mm->mm_users) <= 1) {
493                 WRITE_ONCE(mm->owner, NULL);      459                 WRITE_ONCE(mm->owner, NULL);
494                 return;                           460                 return;
495         }                                         461         }
496                                                   462 
497         read_lock(&tasklist_lock);                463         read_lock(&tasklist_lock);
498         /*                                        464         /*
499          * Search in the children                 465          * Search in the children
500          */                                       466          */
501         list_for_each_entry(g, &p->children, s !! 467         list_for_each_entry(c, &p->children, sibling) {
502                 if (try_to_set_owner(g, mm))   !! 468                 if (c->mm == mm)
503                         goto ret;              !! 469                         goto assign_new_owner;
504         }                                         470         }
                                                   >> 471 
505         /*                                        472         /*
506          * Search in the siblings                 473          * Search in the siblings
507          */                                       474          */
508         list_for_each_entry(g, &p->real_parent !! 475         list_for_each_entry(c, &p->real_parent->children, sibling) {
509                 if (try_to_set_owner(g, mm))   !! 476                 if (c->mm == mm)
510                         goto ret;              !! 477                         goto assign_new_owner;
511         }                                         478         }
                                                   >> 479 
512         /*                                        480         /*
513          * Search through everything else, we     481          * Search through everything else, we should not get here often.
514          */                                       482          */
515         for_each_process(g) {                     483         for_each_process(g) {
516                 if (atomic_read(&mm->mm_users)    484                 if (atomic_read(&mm->mm_users) <= 1)
517                         break;                    485                         break;
518                 if (g->flags & PF_KTHREAD)        486                 if (g->flags & PF_KTHREAD)
519                         continue;                 487                         continue;
520                 if (try_to_set_owner(g, mm))   !! 488                 for_each_thread(g, c) {
521                         goto ret;              !! 489                         if (c->mm == mm)
                                                   >> 490                                 goto assign_new_owner;
                                                   >> 491                         if (c->mm)
                                                   >> 492                                 break;
                                                   >> 493                 }
522         }                                         494         }
523         read_unlock(&tasklist_lock);              495         read_unlock(&tasklist_lock);
524         /*                                        496         /*
525          * We found no owner yet mm_users > 1:    497          * We found no owner yet mm_users > 1: this implies that we are
526          * most likely racing with swapoff (tr    498          * most likely racing with swapoff (try_to_unuse()) or /proc or
527          * ptrace or page migration (get_task_    499          * ptrace or page migration (get_task_mm()).  Mark owner as NULL.
528          */                                       500          */
529         WRITE_ONCE(mm->owner, NULL);              501         WRITE_ONCE(mm->owner, NULL);
530  ret:                                          << 
531         return;                                   502         return;
532                                                   503 
                                                   >> 504 assign_new_owner:
                                                   >> 505         BUG_ON(c == p);
                                                   >> 506         get_task_struct(c);
                                                   >> 507         /*
                                                   >> 508          * The task_lock protects c->mm from changing.
                                                   >> 509          * We always want mm->owner->mm == mm
                                                   >> 510          */
                                                   >> 511         task_lock(c);
                                                   >> 512         /*
                                                   >> 513          * Delay read_unlock() till we have the task_lock()
                                                   >> 514          * to ensure that c does not slip away underneath us
                                                   >> 515          */
                                                   >> 516         read_unlock(&tasklist_lock);
                                                   >> 517         if (c->mm != mm) {
                                                   >> 518                 task_unlock(c);
                                                   >> 519                 put_task_struct(c);
                                                   >> 520                 goto retry;
                                                   >> 521         }
                                                   >> 522         WRITE_ONCE(mm->owner, c);
                                                   >> 523         lru_gen_migrate_mm(mm);
                                                   >> 524         task_unlock(c);
                                                   >> 525         put_task_struct(c);
533 }                                                 526 }
534 #endif /* CONFIG_MEMCG */                         527 #endif /* CONFIG_MEMCG */
535                                                   528 
536 /*                                                529 /*
537  * Turn us into a lazy TLB process if we          530  * Turn us into a lazy TLB process if we
538  * aren't already..                               531  * aren't already..
539  */                                               532  */
540 static void exit_mm(void)                         533 static void exit_mm(void)
541 {                                                 534 {
542         struct mm_struct *mm = current->mm;       535         struct mm_struct *mm = current->mm;
543                                                   536 
544         exit_mm_release(current, mm);             537         exit_mm_release(current, mm);
545         if (!mm)                                  538         if (!mm)
546                 return;                           539                 return;
                                                   >> 540         sync_mm_rss(mm);
547         mmap_read_lock(mm);                       541         mmap_read_lock(mm);
548         mmgrab_lazy_tlb(mm);                   !! 542         mmgrab(mm);
549         BUG_ON(mm != current->active_mm);         543         BUG_ON(mm != current->active_mm);
550         /* more a memory barrier than a real l    544         /* more a memory barrier than a real lock */
551         task_lock(current);                       545         task_lock(current);
552         /*                                        546         /*
553          * When a thread stops operating on an    547          * When a thread stops operating on an address space, the loop
554          * in membarrier_private_expedited() m    548          * in membarrier_private_expedited() may not observe that
555          * tsk->mm, and the loop in membarrier    549          * tsk->mm, and the loop in membarrier_global_expedited() may
556          * not observe a MEMBARRIER_STATE_GLOB    550          * not observe a MEMBARRIER_STATE_GLOBAL_EXPEDITED
557          * rq->membarrier_state, so those woul    551          * rq->membarrier_state, so those would not issue an IPI.
558          * Membarrier requires a memory barrie    552          * Membarrier requires a memory barrier after accessing
559          * user-space memory, before clearing     553          * user-space memory, before clearing tsk->mm or the
560          * rq->membarrier_state.                  554          * rq->membarrier_state.
561          */                                       555          */
562         smp_mb__after_spinlock();                 556         smp_mb__after_spinlock();
563         local_irq_disable();                      557         local_irq_disable();
564         current->mm = NULL;                       558         current->mm = NULL;
565         membarrier_update_current_mm(NULL);       559         membarrier_update_current_mm(NULL);
566         enter_lazy_tlb(mm, current);              560         enter_lazy_tlb(mm, current);
567         local_irq_enable();                       561         local_irq_enable();
568         task_unlock(current);                     562         task_unlock(current);
569         mmap_read_unlock(mm);                     563         mmap_read_unlock(mm);
570         mm_update_next_owner(mm);                 564         mm_update_next_owner(mm);
571         mmput(mm);                                565         mmput(mm);
572         if (test_thread_flag(TIF_MEMDIE))         566         if (test_thread_flag(TIF_MEMDIE))
573                 exit_oom_victim();                567                 exit_oom_victim();
574 }                                                 568 }
575                                                   569 
576 static struct task_struct *find_alive_thread(s    570 static struct task_struct *find_alive_thread(struct task_struct *p)
577 {                                                 571 {
578         struct task_struct *t;                    572         struct task_struct *t;
579                                                   573 
580         for_each_thread(p, t) {                   574         for_each_thread(p, t) {
581                 if (!(t->flags & PF_EXITING))     575                 if (!(t->flags & PF_EXITING))
582                         return t;                 576                         return t;
583         }                                         577         }
584         return NULL;                              578         return NULL;
585 }                                                 579 }
586                                                   580 
587 static struct task_struct *find_child_reaper(s    581 static struct task_struct *find_child_reaper(struct task_struct *father,
588                                                   582                                                 struct list_head *dead)
589         __releases(&tasklist_lock)                583         __releases(&tasklist_lock)
590         __acquires(&tasklist_lock)                584         __acquires(&tasklist_lock)
591 {                                                 585 {
592         struct pid_namespace *pid_ns = task_ac    586         struct pid_namespace *pid_ns = task_active_pid_ns(father);
593         struct task_struct *reaper = pid_ns->c    587         struct task_struct *reaper = pid_ns->child_reaper;
594         struct task_struct *p, *n;                588         struct task_struct *p, *n;
595                                                   589 
596         if (likely(reaper != father))             590         if (likely(reaper != father))
597                 return reaper;                    591                 return reaper;
598                                                   592 
599         reaper = find_alive_thread(father);       593         reaper = find_alive_thread(father);
600         if (reaper) {                             594         if (reaper) {
601                 pid_ns->child_reaper = reaper;    595                 pid_ns->child_reaper = reaper;
602                 return reaper;                    596                 return reaper;
603         }                                         597         }
604                                                   598 
605         write_unlock_irq(&tasklist_lock);         599         write_unlock_irq(&tasklist_lock);
606                                                   600 
607         list_for_each_entry_safe(p, n, dead, p    601         list_for_each_entry_safe(p, n, dead, ptrace_entry) {
608                 list_del_init(&p->ptrace_entry    602                 list_del_init(&p->ptrace_entry);
609                 release_task(p);                  603                 release_task(p);
610         }                                         604         }
611                                                   605 
612         zap_pid_ns_processes(pid_ns);             606         zap_pid_ns_processes(pid_ns);
613         write_lock_irq(&tasklist_lock);           607         write_lock_irq(&tasklist_lock);
614                                                   608 
615         return father;                            609         return father;
616 }                                                 610 }
617                                                   611 
618 /*                                                612 /*
619  * When we die, we re-parent all our children,    613  * When we die, we re-parent all our children, and try to:
620  * 1. give them to another thread in our threa    614  * 1. give them to another thread in our thread group, if such a member exists
621  * 2. give it to the first ancestor process wh    615  * 2. give it to the first ancestor process which prctl'd itself as a
622  *    child_subreaper for its children (like a    616  *    child_subreaper for its children (like a service manager)
623  * 3. give it to the init process (PID 1) in o    617  * 3. give it to the init process (PID 1) in our pid namespace
624  */                                               618  */
625 static struct task_struct *find_new_reaper(str    619 static struct task_struct *find_new_reaper(struct task_struct *father,
626                                            str    620                                            struct task_struct *child_reaper)
627 {                                                 621 {
628         struct task_struct *thread, *reaper;      622         struct task_struct *thread, *reaper;
629                                                   623 
630         thread = find_alive_thread(father);       624         thread = find_alive_thread(father);
631         if (thread)                               625         if (thread)
632                 return thread;                    626                 return thread;
633                                                   627 
634         if (father->signal->has_child_subreape    628         if (father->signal->has_child_subreaper) {
635                 unsigned int ns_level = task_p    629                 unsigned int ns_level = task_pid(father)->level;
636                 /*                                630                 /*
637                  * Find the first ->is_child_s    631                  * Find the first ->is_child_subreaper ancestor in our pid_ns.
638                  * We can't check reaper != ch    632                  * We can't check reaper != child_reaper to ensure we do not
639                  * cross the namespaces, the e    633                  * cross the namespaces, the exiting parent could be injected
640                  * by setns() + fork().           634                  * by setns() + fork().
641                  * We check pid->level, this i    635                  * We check pid->level, this is slightly more efficient than
642                  * task_active_pid_ns(reaper)     636                  * task_active_pid_ns(reaper) != task_active_pid_ns(father).
643                  */                               637                  */
644                 for (reaper = father->real_par    638                 for (reaper = father->real_parent;
645                      task_pid(reaper)->level =    639                      task_pid(reaper)->level == ns_level;
646                      reaper = reaper->real_par    640                      reaper = reaper->real_parent) {
647                         if (reaper == &init_ta    641                         if (reaper == &init_task)
648                                 break;            642                                 break;
649                         if (!reaper->signal->i    643                         if (!reaper->signal->is_child_subreaper)
650                                 continue;         644                                 continue;
651                         thread = find_alive_th    645                         thread = find_alive_thread(reaper);
652                         if (thread)               646                         if (thread)
653                                 return thread;    647                                 return thread;
654                 }                                 648                 }
655         }                                         649         }
656                                                   650 
657         return child_reaper;                      651         return child_reaper;
658 }                                                 652 }
659                                                   653 
660 /*                                                654 /*
661 * Any that need to be release_task'd are put o    655 * Any that need to be release_task'd are put on the @dead list.
662  */                                               656  */
663 static void reparent_leader(struct task_struct    657 static void reparent_leader(struct task_struct *father, struct task_struct *p,
664                                 struct list_he    658                                 struct list_head *dead)
665 {                                                 659 {
666         if (unlikely(p->exit_state == EXIT_DEA    660         if (unlikely(p->exit_state == EXIT_DEAD))
667                 return;                           661                 return;
668                                                   662 
669         /* We don't want people slaying init.     663         /* We don't want people slaying init. */
670         p->exit_signal = SIGCHLD;                 664         p->exit_signal = SIGCHLD;
671                                                   665 
672         /* If it has exited notify the new par    666         /* If it has exited notify the new parent about this child's death. */
673         if (!p->ptrace &&                         667         if (!p->ptrace &&
674             p->exit_state == EXIT_ZOMBIE && th    668             p->exit_state == EXIT_ZOMBIE && thread_group_empty(p)) {
675                 if (do_notify_parent(p, p->exi    669                 if (do_notify_parent(p, p->exit_signal)) {
676                         p->exit_state = EXIT_D    670                         p->exit_state = EXIT_DEAD;
677                         list_add(&p->ptrace_en    671                         list_add(&p->ptrace_entry, dead);
678                 }                                 672                 }
679         }                                         673         }
680                                                   674 
681         kill_orphaned_pgrp(p, father);            675         kill_orphaned_pgrp(p, father);
682 }                                                 676 }
683                                                   677 
684 /*                                                678 /*
685  * This does two things:                          679  * This does two things:
686  *                                                680  *
687  * A.  Make init inherit all the child process    681  * A.  Make init inherit all the child processes
688  * B.  Check to see if any process groups have    682  * B.  Check to see if any process groups have become orphaned
689  *      as a result of our exiting, and if the    683  *      as a result of our exiting, and if they have any stopped
690  *      jobs, send them a SIGHUP and then a SI    684  *      jobs, send them a SIGHUP and then a SIGCONT.  (POSIX 3.2.2.2)
691  */                                               685  */
692 static void forget_original_parent(struct task    686 static void forget_original_parent(struct task_struct *father,
693                                         struct    687                                         struct list_head *dead)
694 {                                                 688 {
695         struct task_struct *p, *t, *reaper;       689         struct task_struct *p, *t, *reaper;
696                                                   690 
697         if (unlikely(!list_empty(&father->ptra    691         if (unlikely(!list_empty(&father->ptraced)))
698                 exit_ptrace(father, dead);        692                 exit_ptrace(father, dead);
699                                                   693 
700         /* Can drop and reacquire tasklist_loc    694         /* Can drop and reacquire tasklist_lock */
701         reaper = find_child_reaper(father, dea    695         reaper = find_child_reaper(father, dead);
702         if (list_empty(&father->children))        696         if (list_empty(&father->children))
703                 return;                           697                 return;
704                                                   698 
705         reaper = find_new_reaper(father, reape    699         reaper = find_new_reaper(father, reaper);
706         list_for_each_entry(p, &father->childr    700         list_for_each_entry(p, &father->children, sibling) {
707                 for_each_thread(p, t) {           701                 for_each_thread(p, t) {
708                         RCU_INIT_POINTER(t->re    702                         RCU_INIT_POINTER(t->real_parent, reaper);
709                         BUG_ON((!t->ptrace) !=    703                         BUG_ON((!t->ptrace) != (rcu_access_pointer(t->parent) == father));
710                         if (likely(!t->ptrace)    704                         if (likely(!t->ptrace))
711                                 t->parent = t-    705                                 t->parent = t->real_parent;
712                         if (t->pdeath_signal)     706                         if (t->pdeath_signal)
713                                 group_send_sig    707                                 group_send_sig_info(t->pdeath_signal,
714                                                   708                                                     SEND_SIG_NOINFO, t,
715                                                   709                                                     PIDTYPE_TGID);
716                 }                                 710                 }
717                 /*                                711                 /*
718                  * If this is a threaded repar    712                  * If this is a threaded reparent there is no need to
719                  * notify anyone anything has     713                  * notify anyone anything has happened.
720                  */                               714                  */
721                 if (!same_thread_group(reaper,    715                 if (!same_thread_group(reaper, father))
722                         reparent_leader(father    716                         reparent_leader(father, p, dead);
723         }                                         717         }
724         list_splice_tail_init(&father->childre    718         list_splice_tail_init(&father->children, &reaper->children);
725 }                                                 719 }
726                                                   720 
727 /*                                                721 /*
728  * Send signals to all our closest relatives s    722  * Send signals to all our closest relatives so that they know
729  * to properly mourn us..                         723  * to properly mourn us..
730  */                                               724  */
731 static void exit_notify(struct task_struct *ts    725 static void exit_notify(struct task_struct *tsk, int group_dead)
732 {                                                 726 {
733         bool autoreap;                            727         bool autoreap;
734         struct task_struct *p, *n;                728         struct task_struct *p, *n;
735         LIST_HEAD(dead);                          729         LIST_HEAD(dead);
736                                                   730 
737         write_lock_irq(&tasklist_lock);           731         write_lock_irq(&tasklist_lock);
738         forget_original_parent(tsk, &dead);       732         forget_original_parent(tsk, &dead);
739                                                   733 
740         if (group_dead)                           734         if (group_dead)
741                 kill_orphaned_pgrp(tsk->group_    735                 kill_orphaned_pgrp(tsk->group_leader, NULL);
742                                                   736 
743         tsk->exit_state = EXIT_ZOMBIE;            737         tsk->exit_state = EXIT_ZOMBIE;
744         /*                                     << 
745          * sub-thread or delay_group_leader(), << 
746          * PIDFD_THREAD waiters.               << 
747          */                                    << 
748         if (!thread_group_empty(tsk))          << 
749                 do_notify_pidfd(tsk);          << 
750                                                << 
751         if (unlikely(tsk->ptrace)) {              738         if (unlikely(tsk->ptrace)) {
752                 int sig = thread_group_leader(    739                 int sig = thread_group_leader(tsk) &&
753                                 thread_group_e    740                                 thread_group_empty(tsk) &&
754                                 !ptrace_repare    741                                 !ptrace_reparented(tsk) ?
755                         tsk->exit_signal : SIG    742                         tsk->exit_signal : SIGCHLD;
756                 autoreap = do_notify_parent(ts    743                 autoreap = do_notify_parent(tsk, sig);
757         } else if (thread_group_leader(tsk)) {    744         } else if (thread_group_leader(tsk)) {
758                 autoreap = thread_group_empty(    745                 autoreap = thread_group_empty(tsk) &&
759                         do_notify_parent(tsk,     746                         do_notify_parent(tsk, tsk->exit_signal);
760         } else {                                  747         } else {
761                 autoreap = true;                  748                 autoreap = true;
762         }                                         749         }
763                                                   750 
764         if (autoreap) {                           751         if (autoreap) {
765                 tsk->exit_state = EXIT_DEAD;      752                 tsk->exit_state = EXIT_DEAD;
766                 list_add(&tsk->ptrace_entry, &    753                 list_add(&tsk->ptrace_entry, &dead);
767         }                                         754         }
768                                                   755 
769         /* mt-exec, de_thread() is waiting for    756         /* mt-exec, de_thread() is waiting for group leader */
770         if (unlikely(tsk->signal->notify_count    757         if (unlikely(tsk->signal->notify_count < 0))
771                 wake_up_process(tsk->signal->g    758                 wake_up_process(tsk->signal->group_exec_task);
772         write_unlock_irq(&tasklist_lock);         759         write_unlock_irq(&tasklist_lock);
773                                                   760 
774         list_for_each_entry_safe(p, n, &dead,     761         list_for_each_entry_safe(p, n, &dead, ptrace_entry) {
775                 list_del_init(&p->ptrace_entry    762                 list_del_init(&p->ptrace_entry);
776                 release_task(p);                  763                 release_task(p);
777         }                                         764         }
778 }                                                 765 }
779                                                   766 
780 #ifdef CONFIG_DEBUG_STACK_USAGE                   767 #ifdef CONFIG_DEBUG_STACK_USAGE
781 unsigned long stack_not_used(struct task_struc << 
782 {                                              << 
783         unsigned long *n = end_of_stack(p);    << 
784                                                << 
785         do {    /* Skip over canary */         << 
786 # ifdef CONFIG_STACK_GROWSUP                   << 
787                 n--;                           << 
788 # else                                         << 
789                 n++;                           << 
790 # endif                                        << 
791         } while (!*n);                         << 
792                                                << 
793 # ifdef CONFIG_STACK_GROWSUP                   << 
794         return (unsigned long)end_of_stack(p)  << 
795 # else                                         << 
796         return (unsigned long)n - (unsigned lo << 
797 # endif                                        << 
798 }                                              << 
799                                                << 
800 /* Count the maximum pages reached in kernel s << 
801 static inline void kstack_histogram(unsigned l << 
802 {                                              << 
803 #ifdef CONFIG_VM_EVENT_COUNTERS                << 
804         if (used_stack <= 1024)                << 
805                 count_vm_event(KSTACK_1K);     << 
806 #if THREAD_SIZE > 1024                         << 
807         else if (used_stack <= 2048)           << 
808                 count_vm_event(KSTACK_2K);     << 
809 #endif                                         << 
810 #if THREAD_SIZE > 2048                         << 
811         else if (used_stack <= 4096)           << 
812                 count_vm_event(KSTACK_4K);     << 
813 #endif                                         << 
814 #if THREAD_SIZE > 4096                         << 
815         else if (used_stack <= 8192)           << 
816                 count_vm_event(KSTACK_8K);     << 
817 #endif                                         << 
818 #if THREAD_SIZE > 8192                         << 
819         else if (used_stack <= 16384)          << 
820                 count_vm_event(KSTACK_16K);    << 
821 #endif                                         << 
822 #if THREAD_SIZE > 16384                        << 
823         else if (used_stack <= 32768)          << 
824                 count_vm_event(KSTACK_32K);    << 
825 #endif                                         << 
826 #if THREAD_SIZE > 32768                        << 
827         else if (used_stack <= 65536)          << 
828                 count_vm_event(KSTACK_64K);    << 
829 #endif                                         << 
830 #if THREAD_SIZE > 65536                        << 
831         else                                   << 
832                 count_vm_event(KSTACK_REST);   << 
833 #endif                                         << 
834 #endif /* CONFIG_VM_EVENT_COUNTERS */          << 
835 }                                              << 
836                                                << 
837 static void check_stack_usage(void)               768 static void check_stack_usage(void)
838 {                                                 769 {
839         static DEFINE_SPINLOCK(low_water_lock)    770         static DEFINE_SPINLOCK(low_water_lock);
840         static int lowest_to_date = THREAD_SIZ    771         static int lowest_to_date = THREAD_SIZE;
841         unsigned long free;                       772         unsigned long free;
842                                                   773 
843         free = stack_not_used(current);           774         free = stack_not_used(current);
844         kstack_histogram(THREAD_SIZE - free);  << 
845                                                   775 
846         if (free >= lowest_to_date)               776         if (free >= lowest_to_date)
847                 return;                           777                 return;
848                                                   778 
849         spin_lock(&low_water_lock);               779         spin_lock(&low_water_lock);
850         if (free < lowest_to_date) {              780         if (free < lowest_to_date) {
851                 pr_info("%s (%d) used greatest    781                 pr_info("%s (%d) used greatest stack depth: %lu bytes left\n",
852                         current->comm, task_pi    782                         current->comm, task_pid_nr(current), free);
853                 lowest_to_date = free;            783                 lowest_to_date = free;
854         }                                         784         }
855         spin_unlock(&low_water_lock);             785         spin_unlock(&low_water_lock);
856 }                                                 786 }
857 #else                                             787 #else
858 static inline void check_stack_usage(void) {}     788 static inline void check_stack_usage(void) {}
859 #endif                                            789 #endif
860                                                   790 
861 static void synchronize_group_exit(struct task    791 static void synchronize_group_exit(struct task_struct *tsk, long code)
862 {                                                 792 {
863         struct sighand_struct *sighand = tsk->    793         struct sighand_struct *sighand = tsk->sighand;
864         struct signal_struct *signal = tsk->si    794         struct signal_struct *signal = tsk->signal;
865                                                   795 
866         spin_lock_irq(&sighand->siglock);         796         spin_lock_irq(&sighand->siglock);
867         signal->quick_threads--;                  797         signal->quick_threads--;
868         if ((signal->quick_threads == 0) &&       798         if ((signal->quick_threads == 0) &&
869             !(signal->flags & SIGNAL_GROUP_EXI    799             !(signal->flags & SIGNAL_GROUP_EXIT)) {
870                 signal->flags = SIGNAL_GROUP_E    800                 signal->flags = SIGNAL_GROUP_EXIT;
871                 signal->group_exit_code = code    801                 signal->group_exit_code = code;
872                 signal->group_stop_count = 0;     802                 signal->group_stop_count = 0;
873         }                                         803         }
874         spin_unlock_irq(&sighand->siglock);       804         spin_unlock_irq(&sighand->siglock);
875 }                                                 805 }
876                                                   806 
877 void __noreturn do_exit(long code)                807 void __noreturn do_exit(long code)
878 {                                                 808 {
879         struct task_struct *tsk = current;        809         struct task_struct *tsk = current;
880         int group_dead;                           810         int group_dead;
881                                                   811 
882         WARN_ON(irqs_disabled());                 812         WARN_ON(irqs_disabled());
883                                                   813 
884         synchronize_group_exit(tsk, code);        814         synchronize_group_exit(tsk, code);
885                                                   815 
886         WARN_ON(tsk->plug);                       816         WARN_ON(tsk->plug);
887                                                   817 
888         kcov_task_exit(tsk);                      818         kcov_task_exit(tsk);
889         kmsan_task_exit(tsk);                     819         kmsan_task_exit(tsk);
890                                                   820 
891         coredump_task_exit(tsk);                  821         coredump_task_exit(tsk);
892         ptrace_event(PTRACE_EVENT_EXIT, code);    822         ptrace_event(PTRACE_EVENT_EXIT, code);
893         user_events_exit(tsk);                 !! 823 
                                                   >> 824         validate_creds_for_do_exit(tsk);
894                                                   825 
895         io_uring_files_cancel();                  826         io_uring_files_cancel();
896         exit_signals(tsk);  /* sets PF_EXITING    827         exit_signals(tsk);  /* sets PF_EXITING */
897                                                   828 
898         seccomp_filter_release(tsk);           !! 829         /* sync mm's RSS info before statistics gathering */
899                                                !! 830         if (tsk->mm)
                                                   >> 831                 sync_mm_rss(tsk->mm);
900         acct_update_integrals(tsk);               832         acct_update_integrals(tsk);
901         group_dead = atomic_dec_and_test(&tsk-    833         group_dead = atomic_dec_and_test(&tsk->signal->live);
902         if (group_dead) {                         834         if (group_dead) {
903                 /*                                835                 /*
904                  * If the last thread of globa    836                  * If the last thread of global init has exited, panic
905                  * immediately to get a useabl    837                  * immediately to get a useable coredump.
906                  */                               838                  */
907                 if (unlikely(is_global_init(ts    839                 if (unlikely(is_global_init(tsk)))
908                         panic("Attempted to ki    840                         panic("Attempted to kill init! exitcode=0x%08x\n",
909                                 tsk->signal->g    841                                 tsk->signal->group_exit_code ?: (int)code);
910                                                   842 
911 #ifdef CONFIG_POSIX_TIMERS                        843 #ifdef CONFIG_POSIX_TIMERS
912                 hrtimer_cancel(&tsk->signal->r    844                 hrtimer_cancel(&tsk->signal->real_timer);
913                 exit_itimers(tsk);                845                 exit_itimers(tsk);
914 #endif                                            846 #endif
915                 if (tsk->mm)                      847                 if (tsk->mm)
916                         setmax_mm_hiwater_rss(    848                         setmax_mm_hiwater_rss(&tsk->signal->maxrss, tsk->mm);
917         }                                         849         }
918         acct_collect(code, group_dead);           850         acct_collect(code, group_dead);
919         if (group_dead)                           851         if (group_dead)
920                 tty_audit_exit();                 852                 tty_audit_exit();
921         audit_free(tsk);                          853         audit_free(tsk);
922                                                   854 
923         tsk->exit_code = code;                    855         tsk->exit_code = code;
924         taskstats_exit(tsk, group_dead);          856         taskstats_exit(tsk, group_dead);
925                                                   857 
926         exit_mm();                                858         exit_mm();
927                                                   859 
928         if (group_dead)                           860         if (group_dead)
929                 acct_process();                   861                 acct_process();
930         trace_sched_process_exit(tsk);            862         trace_sched_process_exit(tsk);
931                                                   863 
932         exit_sem(tsk);                            864         exit_sem(tsk);
933         exit_shm(tsk);                            865         exit_shm(tsk);
934         exit_files(tsk);                          866         exit_files(tsk);
935         exit_fs(tsk);                             867         exit_fs(tsk);
936         if (group_dead)                           868         if (group_dead)
937                 disassociate_ctty(1);             869                 disassociate_ctty(1);
938         exit_task_namespaces(tsk);                870         exit_task_namespaces(tsk);
939         exit_task_work(tsk);                      871         exit_task_work(tsk);
940         exit_thread(tsk);                         872         exit_thread(tsk);
941                                                   873 
942         /*                                        874         /*
943          * Flush inherited counters to the par    875          * Flush inherited counters to the parent - before the parent
944          * gets woken up by child-exit notific    876          * gets woken up by child-exit notifications.
945          *                                        877          *
946          * because of cgroup mode, must be cal    878          * because of cgroup mode, must be called before cgroup_exit()
947          */                                       879          */
948         perf_event_exit_task(tsk);                880         perf_event_exit_task(tsk);
949                                                   881 
950         sched_autogroup_exit_task(tsk);           882         sched_autogroup_exit_task(tsk);
951         cgroup_exit(tsk);                         883         cgroup_exit(tsk);
952                                                   884 
953         /*                                        885         /*
954          * FIXME: do that only when needed, us    886          * FIXME: do that only when needed, using sched_exit tracepoint
955          */                                       887          */
956         flush_ptrace_hw_breakpoint(tsk);          888         flush_ptrace_hw_breakpoint(tsk);
957                                                   889 
958         exit_tasks_rcu_start();                   890         exit_tasks_rcu_start();
959         exit_notify(tsk, group_dead);             891         exit_notify(tsk, group_dead);
960         proc_exit_connector(tsk);                 892         proc_exit_connector(tsk);
961         mpol_put_task_policy(tsk);                893         mpol_put_task_policy(tsk);
962 #ifdef CONFIG_FUTEX                               894 #ifdef CONFIG_FUTEX
963         if (unlikely(current->pi_state_cache))    895         if (unlikely(current->pi_state_cache))
964                 kfree(current->pi_state_cache)    896                 kfree(current->pi_state_cache);
965 #endif                                            897 #endif
966         /*                                        898         /*
967          * Make sure we are holding no locks:     899          * Make sure we are holding no locks:
968          */                                       900          */
969         debug_check_no_locks_held();              901         debug_check_no_locks_held();
970                                                   902 
971         if (tsk->io_context)                      903         if (tsk->io_context)
972                 exit_io_context(tsk);             904                 exit_io_context(tsk);
973                                                   905 
974         if (tsk->splice_pipe)                     906         if (tsk->splice_pipe)
975                 free_pipe_info(tsk->splice_pip    907                 free_pipe_info(tsk->splice_pipe);
976                                                   908 
977         if (tsk->task_frag.page)                  909         if (tsk->task_frag.page)
978                 put_page(tsk->task_frag.page);    910                 put_page(tsk->task_frag.page);
979                                                   911 
                                                   >> 912         validate_creds_for_do_exit(tsk);
980         exit_task_stack_account(tsk);             913         exit_task_stack_account(tsk);
981                                                   914 
982         check_stack_usage();                      915         check_stack_usage();
983         preempt_disable();                        916         preempt_disable();
984         if (tsk->nr_dirtied)                      917         if (tsk->nr_dirtied)
985                 __this_cpu_add(dirty_throttle_    918                 __this_cpu_add(dirty_throttle_leaks, tsk->nr_dirtied);
986         exit_rcu();                               919         exit_rcu();
987         exit_tasks_rcu_finish();                  920         exit_tasks_rcu_finish();
988                                                   921 
989         lockdep_free_task(tsk);                   922         lockdep_free_task(tsk);
990         do_task_dead();                           923         do_task_dead();
991 }                                                 924 }
992                                                   925 
993 void __noreturn make_task_dead(int signr)         926 void __noreturn make_task_dead(int signr)
994 {                                                 927 {
995         /*                                        928         /*
996          * Take the task off the cpu after som    929          * Take the task off the cpu after something catastrophic has
997          * happened.                              930          * happened.
998          *                                        931          *
999          * We can get here from a kernel oops,    932          * We can get here from a kernel oops, sometimes with preemption off.
1000          * Start by checking for critical err    933          * Start by checking for critical errors.
1001          * Then fix up important state like U    934          * Then fix up important state like USER_DS and preemption.
1002          * Then do everything else.              935          * Then do everything else.
1003          */                                      936          */
1004         struct task_struct *tsk = current;       937         struct task_struct *tsk = current;
1005         unsigned int limit;                      938         unsigned int limit;
1006                                                  939 
1007         if (unlikely(in_interrupt()))            940         if (unlikely(in_interrupt()))
1008                 panic("Aiee, killing interrup    941                 panic("Aiee, killing interrupt handler!");
1009         if (unlikely(!tsk->pid))                 942         if (unlikely(!tsk->pid))
1010                 panic("Attempted to kill the     943                 panic("Attempted to kill the idle task!");
1011                                                  944 
1012         if (unlikely(irqs_disabled())) {         945         if (unlikely(irqs_disabled())) {
1013                 pr_info("note: %s[%d] exited     946                 pr_info("note: %s[%d] exited with irqs disabled\n",
1014                         current->comm, task_p    947                         current->comm, task_pid_nr(current));
1015                 local_irq_enable();              948                 local_irq_enable();
1016         }                                        949         }
1017         if (unlikely(in_atomic())) {             950         if (unlikely(in_atomic())) {
1018                 pr_info("note: %s[%d] exited     951                 pr_info("note: %s[%d] exited with preempt_count %d\n",
1019                         current->comm, task_p    952                         current->comm, task_pid_nr(current),
1020                         preempt_count());        953                         preempt_count());
1021                 preempt_count_set(PREEMPT_ENA    954                 preempt_count_set(PREEMPT_ENABLED);
1022         }                                        955         }
1023                                                  956 
1024         /*                                       957         /*
1025          * Every time the system oopses, if t    958          * Every time the system oopses, if the oops happens while a reference
1026          * to an object was held, the referen    959          * to an object was held, the reference leaks.
1027          * If the oops doesn't also leak memo    960          * If the oops doesn't also leak memory, repeated oopsing can cause
1028          * reference counters to wrap around     961          * reference counters to wrap around (if they're not using refcount_t).
1029          * This means that repeated oopsing c    962          * This means that repeated oopsing can make unexploitable-looking bugs
1030          * exploitable through repeated oopsi    963          * exploitable through repeated oopsing.
1031          * To make sure this can't happen, pl    964          * To make sure this can't happen, place an upper bound on how often the
1032          * kernel may oops without panic().      965          * kernel may oops without panic().
1033          */                                      966          */
1034         limit = READ_ONCE(oops_limit);           967         limit = READ_ONCE(oops_limit);
1035         if (atomic_inc_return(&oops_count) >=    968         if (atomic_inc_return(&oops_count) >= limit && limit)
1036                 panic("Oopsed too often (kern    969                 panic("Oopsed too often (kernel.oops_limit is %d)", limit);
1037                                                  970 
1038         /*                                       971         /*
1039          * We're taking recursive faults here    972          * We're taking recursive faults here in make_task_dead. Safest is to just
1040          * leave this task alone and wait for    973          * leave this task alone and wait for reboot.
1041          */                                      974          */
1042         if (unlikely(tsk->flags & PF_EXITING)    975         if (unlikely(tsk->flags & PF_EXITING)) {
1043                 pr_alert("Fixing recursive fa    976                 pr_alert("Fixing recursive fault but reboot is needed!\n");
1044                 futex_exit_recursive(tsk);       977                 futex_exit_recursive(tsk);
1045                 tsk->exit_state = EXIT_DEAD;     978                 tsk->exit_state = EXIT_DEAD;
1046                 refcount_inc(&tsk->rcu_users)    979                 refcount_inc(&tsk->rcu_users);
1047                 do_task_dead();                  980                 do_task_dead();
1048         }                                        981         }
1049                                                  982 
1050         do_exit(signr);                          983         do_exit(signr);
1051 }                                                984 }
1052                                                  985 
1053 SYSCALL_DEFINE1(exit, int, error_code)           986 SYSCALL_DEFINE1(exit, int, error_code)
1054 {                                                987 {
1055         do_exit((error_code&0xff)<<8);           988         do_exit((error_code&0xff)<<8);
1056 }                                                989 }
1057                                                  990 
1058 /*                                               991 /*
1059  * Take down every thread in the group.  This    992  * Take down every thread in the group.  This is called by fatal signals
1060  * as well as by sys_exit_group (below).         993  * as well as by sys_exit_group (below).
1061  */                                              994  */
1062 void __noreturn                                  995 void __noreturn
1063 do_group_exit(int exit_code)                     996 do_group_exit(int exit_code)
1064 {                                                997 {
1065         struct signal_struct *sig = current->    998         struct signal_struct *sig = current->signal;
1066                                                  999 
1067         if (sig->flags & SIGNAL_GROUP_EXIT)      1000         if (sig->flags & SIGNAL_GROUP_EXIT)
1068                 exit_code = sig->group_exit_c    1001                 exit_code = sig->group_exit_code;
1069         else if (sig->group_exec_task)           1002         else if (sig->group_exec_task)
1070                 exit_code = 0;                   1003                 exit_code = 0;
1071         else {                                   1004         else {
1072                 struct sighand_struct *const     1005                 struct sighand_struct *const sighand = current->sighand;
1073                                                  1006 
1074                 spin_lock_irq(&sighand->siglo    1007                 spin_lock_irq(&sighand->siglock);
1075                 if (sig->flags & SIGNAL_GROUP    1008                 if (sig->flags & SIGNAL_GROUP_EXIT)
1076                         /* Another thread got    1009                         /* Another thread got here before we took the lock.  */
1077                         exit_code = sig->grou    1010                         exit_code = sig->group_exit_code;
1078                 else if (sig->group_exec_task    1011                 else if (sig->group_exec_task)
1079                         exit_code = 0;           1012                         exit_code = 0;
1080                 else {                           1013                 else {
1081                         sig->group_exit_code     1014                         sig->group_exit_code = exit_code;
1082                         sig->flags = SIGNAL_G    1015                         sig->flags = SIGNAL_GROUP_EXIT;
1083                         zap_other_threads(cur    1016                         zap_other_threads(current);
1084                 }                                1017                 }
1085                 spin_unlock_irq(&sighand->sig    1018                 spin_unlock_irq(&sighand->siglock);
1086         }                                        1019         }
1087                                                  1020 
1088         do_exit(exit_code);                      1021         do_exit(exit_code);
1089         /* NOTREACHED */                         1022         /* NOTREACHED */
1090 }                                                1023 }
1091                                                  1024 
1092 /*                                               1025 /*
1093  * this kills every thread in the thread grou    1026  * this kills every thread in the thread group. Note that any externally
1094  * wait4()-ing process will get the correct e    1027  * wait4()-ing process will get the correct exit code - even if this
1095  * thread is not the thread group leader.        1028  * thread is not the thread group leader.
1096  */                                              1029  */
1097 SYSCALL_DEFINE1(exit_group, int, error_code)     1030 SYSCALL_DEFINE1(exit_group, int, error_code)
1098 {                                                1031 {
1099         do_group_exit((error_code & 0xff) <<     1032         do_group_exit((error_code & 0xff) << 8);
1100         /* NOTREACHED */                         1033         /* NOTREACHED */
1101         return 0;                                1034         return 0;
1102 }                                                1035 }
1103                                                  1036 
                                                   >> 1037 struct waitid_info {
                                                   >> 1038         pid_t pid;
                                                   >> 1039         uid_t uid;
                                                   >> 1040         int status;
                                                   >> 1041         int cause;
                                                   >> 1042 };
                                                   >> 1043 
                                                   >> 1044 struct wait_opts {
                                                   >> 1045         enum pid_type           wo_type;
                                                   >> 1046         int                     wo_flags;
                                                   >> 1047         struct pid              *wo_pid;
                                                   >> 1048 
                                                   >> 1049         struct waitid_info      *wo_info;
                                                   >> 1050         int                     wo_stat;
                                                   >> 1051         struct rusage           *wo_rusage;
                                                   >> 1052 
                                                   >> 1053         wait_queue_entry_t              child_wait;
                                                   >> 1054         int                     notask_error;
                                                   >> 1055 };
                                                   >> 1056 
1104 static int eligible_pid(struct wait_opts *wo,    1057 static int eligible_pid(struct wait_opts *wo, struct task_struct *p)
1105 {                                                1058 {
1106         return  wo->wo_type == PIDTYPE_MAX ||    1059         return  wo->wo_type == PIDTYPE_MAX ||
1107                 task_pid_type(p, wo->wo_type)    1060                 task_pid_type(p, wo->wo_type) == wo->wo_pid;
1108 }                                                1061 }
1109                                                  1062 
1110 static int                                       1063 static int
1111 eligible_child(struct wait_opts *wo, bool ptr    1064 eligible_child(struct wait_opts *wo, bool ptrace, struct task_struct *p)
1112 {                                                1065 {
1113         if (!eligible_pid(wo, p))                1066         if (!eligible_pid(wo, p))
1114                 return 0;                        1067                 return 0;
1115                                                  1068 
1116         /*                                       1069         /*
1117          * Wait for all children (clone and n    1070          * Wait for all children (clone and not) if __WALL is set or
1118          * if it is traced by us.                1071          * if it is traced by us.
1119          */                                      1072          */
1120         if (ptrace || (wo->wo_flags & __WALL)    1073         if (ptrace || (wo->wo_flags & __WALL))
1121                 return 1;                        1074                 return 1;
1122                                                  1075 
1123         /*                                       1076         /*
1124          * Otherwise, wait for clone children    1077          * Otherwise, wait for clone children *only* if __WCLONE is set;
1125          * otherwise, wait for non-clone chil    1078          * otherwise, wait for non-clone children *only*.
1126          *                                       1079          *
1127          * Note: a "clone" child here is one     1080          * Note: a "clone" child here is one that reports to its parent
1128          * using a signal other than SIGCHLD,    1081          * using a signal other than SIGCHLD, or a non-leader thread which
1129          * we can only see if it is traced by    1082          * we can only see if it is traced by us.
1130          */                                      1083          */
1131         if ((p->exit_signal != SIGCHLD) ^ !!(    1084         if ((p->exit_signal != SIGCHLD) ^ !!(wo->wo_flags & __WCLONE))
1132                 return 0;                        1085                 return 0;
1133                                                  1086 
1134         return 1;                                1087         return 1;
1135 }                                                1088 }
1136                                                  1089 
1137 /*                                               1090 /*
1138  * Handle sys_wait4 work for one task in stat    1091  * Handle sys_wait4 work for one task in state EXIT_ZOMBIE.  We hold
1139  * read_lock(&tasklist_lock) on entry.  If we    1092  * read_lock(&tasklist_lock) on entry.  If we return zero, we still hold
1140  * the lock and this task is uninteresting.      1093  * the lock and this task is uninteresting.  If we return nonzero, we have
1141  * released the lock and the system call shou    1094  * released the lock and the system call should return.
1142  */                                              1095  */
1143 static int wait_task_zombie(struct wait_opts     1096 static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
1144 {                                                1097 {
1145         int state, status;                       1098         int state, status;
1146         pid_t pid = task_pid_vnr(p);             1099         pid_t pid = task_pid_vnr(p);
1147         uid_t uid = from_kuid_munged(current_    1100         uid_t uid = from_kuid_munged(current_user_ns(), task_uid(p));
1148         struct waitid_info *infop;               1101         struct waitid_info *infop;
1149                                                  1102 
1150         if (!likely(wo->wo_flags & WEXITED))     1103         if (!likely(wo->wo_flags & WEXITED))
1151                 return 0;                        1104                 return 0;
1152                                                  1105 
1153         if (unlikely(wo->wo_flags & WNOWAIT))    1106         if (unlikely(wo->wo_flags & WNOWAIT)) {
1154                 status = (p->signal->flags &     1107                 status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1155                         ? p->signal->group_ex    1108                         ? p->signal->group_exit_code : p->exit_code;
1156                 get_task_struct(p);              1109                 get_task_struct(p);
1157                 read_unlock(&tasklist_lock);     1110                 read_unlock(&tasklist_lock);
1158                 sched_annotate_sleep();          1111                 sched_annotate_sleep();
1159                 if (wo->wo_rusage)               1112                 if (wo->wo_rusage)
1160                         getrusage(p, RUSAGE_B    1113                         getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
1161                 put_task_struct(p);              1114                 put_task_struct(p);
1162                 goto out_info;                   1115                 goto out_info;
1163         }                                        1116         }
1164         /*                                       1117         /*
1165          * Move the task's state to DEAD/TRAC    1118          * Move the task's state to DEAD/TRACE, only one thread can do this.
1166          */                                      1119          */
1167         state = (ptrace_reparented(p) && thre    1120         state = (ptrace_reparented(p) && thread_group_leader(p)) ?
1168                 EXIT_TRACE : EXIT_DEAD;          1121                 EXIT_TRACE : EXIT_DEAD;
1169         if (cmpxchg(&p->exit_state, EXIT_ZOMB    1122         if (cmpxchg(&p->exit_state, EXIT_ZOMBIE, state) != EXIT_ZOMBIE)
1170                 return 0;                        1123                 return 0;
1171         /*                                       1124         /*
1172          * We own this thread, nobody else ca    1125          * We own this thread, nobody else can reap it.
1173          */                                      1126          */
1174         read_unlock(&tasklist_lock);             1127         read_unlock(&tasklist_lock);
1175         sched_annotate_sleep();                  1128         sched_annotate_sleep();
1176                                                  1129 
1177         /*                                       1130         /*
1178          * Check thread_group_leader() to exc    1131          * Check thread_group_leader() to exclude the traced sub-threads.
1179          */                                      1132          */
1180         if (state == EXIT_DEAD && thread_grou    1133         if (state == EXIT_DEAD && thread_group_leader(p)) {
1181                 struct signal_struct *sig = p    1134                 struct signal_struct *sig = p->signal;
1182                 struct signal_struct *psig =     1135                 struct signal_struct *psig = current->signal;
1183                 unsigned long maxrss;            1136                 unsigned long maxrss;
1184                 u64 tgutime, tgstime;            1137                 u64 tgutime, tgstime;
1185                                                  1138 
1186                 /*                               1139                 /*
1187                  * The resource counters for     1140                  * The resource counters for the group leader are in its
1188                  * own task_struct.  Those fo    1141                  * own task_struct.  Those for dead threads in the group
1189                  * are in its signal_struct,     1142                  * are in its signal_struct, as are those for the child
1190                  * processes it has previousl    1143                  * processes it has previously reaped.  All these
1191                  * accumulate in the parent's    1144                  * accumulate in the parent's signal_struct c* fields.
1192                  *                               1145                  *
1193                  * We don't bother to take a     1146                  * We don't bother to take a lock here to protect these
1194                  * p->signal fields because t    1147                  * p->signal fields because the whole thread group is dead
1195                  * and nobody can change them    1148                  * and nobody can change them.
1196                  *                               1149                  *
1197                  * psig->stats_lock also prot    1150                  * psig->stats_lock also protects us from our sub-threads
1198                  * which can reap other child !! 1151                  * which can reap other children at the same time. Until
                                                   >> 1152                  * we change k_getrusage()-like users to rely on this lock
                                                   >> 1153                  * we have to take ->siglock as well.
1199                  *                               1154                  *
1200                  * We use thread_group_cputim    1155                  * We use thread_group_cputime_adjusted() to get times for
1201                  * the thread group, which co    1156                  * the thread group, which consolidates times for all threads
1202                  * in the group including the    1157                  * in the group including the group leader.
1203                  */                              1158                  */
1204                 thread_group_cputime_adjusted    1159                 thread_group_cputime_adjusted(p, &tgutime, &tgstime);
1205                 write_seqlock_irq(&psig->stat !! 1160                 spin_lock_irq(&current->sighand->siglock);
                                                   >> 1161                 write_seqlock(&psig->stats_lock);
1206                 psig->cutime += tgutime + sig    1162                 psig->cutime += tgutime + sig->cutime;
1207                 psig->cstime += tgstime + sig    1163                 psig->cstime += tgstime + sig->cstime;
1208                 psig->cgtime += task_gtime(p)    1164                 psig->cgtime += task_gtime(p) + sig->gtime + sig->cgtime;
1209                 psig->cmin_flt +=                1165                 psig->cmin_flt +=
1210                         p->min_flt + sig->min    1166                         p->min_flt + sig->min_flt + sig->cmin_flt;
1211                 psig->cmaj_flt +=                1167                 psig->cmaj_flt +=
1212                         p->maj_flt + sig->maj    1168                         p->maj_flt + sig->maj_flt + sig->cmaj_flt;
1213                 psig->cnvcsw +=                  1169                 psig->cnvcsw +=
1214                         p->nvcsw + sig->nvcsw    1170                         p->nvcsw + sig->nvcsw + sig->cnvcsw;
1215                 psig->cnivcsw +=                 1171                 psig->cnivcsw +=
1216                         p->nivcsw + sig->nivc    1172                         p->nivcsw + sig->nivcsw + sig->cnivcsw;
1217                 psig->cinblock +=                1173                 psig->cinblock +=
1218                         task_io_get_inblock(p    1174                         task_io_get_inblock(p) +
1219                         sig->inblock + sig->c    1175                         sig->inblock + sig->cinblock;
1220                 psig->coublock +=                1176                 psig->coublock +=
1221                         task_io_get_oublock(p    1177                         task_io_get_oublock(p) +
1222                         sig->oublock + sig->c    1178                         sig->oublock + sig->coublock;
1223                 maxrss = max(sig->maxrss, sig    1179                 maxrss = max(sig->maxrss, sig->cmaxrss);
1224                 if (psig->cmaxrss < maxrss)      1180                 if (psig->cmaxrss < maxrss)
1225                         psig->cmaxrss = maxrs    1181                         psig->cmaxrss = maxrss;
1226                 task_io_accounting_add(&psig-    1182                 task_io_accounting_add(&psig->ioac, &p->ioac);
1227                 task_io_accounting_add(&psig-    1183                 task_io_accounting_add(&psig->ioac, &sig->ioac);
1228                 write_sequnlock_irq(&psig->st !! 1184                 write_sequnlock(&psig->stats_lock);
                                                   >> 1185                 spin_unlock_irq(&current->sighand->siglock);
1229         }                                        1186         }
1230                                                  1187 
1231         if (wo->wo_rusage)                       1188         if (wo->wo_rusage)
1232                 getrusage(p, RUSAGE_BOTH, wo-    1189                 getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
1233         status = (p->signal->flags & SIGNAL_G    1190         status = (p->signal->flags & SIGNAL_GROUP_EXIT)
1234                 ? p->signal->group_exit_code     1191                 ? p->signal->group_exit_code : p->exit_code;
1235         wo->wo_stat = status;                    1192         wo->wo_stat = status;
1236                                                  1193 
1237         if (state == EXIT_TRACE) {               1194         if (state == EXIT_TRACE) {
1238                 write_lock_irq(&tasklist_lock    1195                 write_lock_irq(&tasklist_lock);
1239                 /* We dropped tasklist, ptrac    1196                 /* We dropped tasklist, ptracer could die and untrace */
1240                 ptrace_unlink(p);                1197                 ptrace_unlink(p);
1241                                                  1198 
1242                 /* If parent wants a zombie,     1199                 /* If parent wants a zombie, don't release it now */
1243                 state = EXIT_ZOMBIE;             1200                 state = EXIT_ZOMBIE;
1244                 if (do_notify_parent(p, p->ex    1201                 if (do_notify_parent(p, p->exit_signal))
1245                         state = EXIT_DEAD;       1202                         state = EXIT_DEAD;
1246                 p->exit_state = state;           1203                 p->exit_state = state;
1247                 write_unlock_irq(&tasklist_lo    1204                 write_unlock_irq(&tasklist_lock);
1248         }                                        1205         }
1249         if (state == EXIT_DEAD)                  1206         if (state == EXIT_DEAD)
1250                 release_task(p);                 1207                 release_task(p);
1251                                                  1208 
1252 out_info:                                        1209 out_info:
1253         infop = wo->wo_info;                     1210         infop = wo->wo_info;
1254         if (infop) {                             1211         if (infop) {
1255                 if ((status & 0x7f) == 0) {      1212                 if ((status & 0x7f) == 0) {
1256                         infop->cause = CLD_EX    1213                         infop->cause = CLD_EXITED;
1257                         infop->status = statu    1214                         infop->status = status >> 8;
1258                 } else {                         1215                 } else {
1259                         infop->cause = (statu    1216                         infop->cause = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
1260                         infop->status = statu    1217                         infop->status = status & 0x7f;
1261                 }                                1218                 }
1262                 infop->pid = pid;                1219                 infop->pid = pid;
1263                 infop->uid = uid;                1220                 infop->uid = uid;
1264         }                                        1221         }
1265                                                  1222 
1266         return pid;                              1223         return pid;
1267 }                                                1224 }
1268                                                  1225 
1269 static int *task_stopped_code(struct task_str    1226 static int *task_stopped_code(struct task_struct *p, bool ptrace)
1270 {                                                1227 {
1271         if (ptrace) {                            1228         if (ptrace) {
1272                 if (task_is_traced(p) && !(p-    1229                 if (task_is_traced(p) && !(p->jobctl & JOBCTL_LISTENING))
1273                         return &p->exit_code;    1230                         return &p->exit_code;
1274         } else {                                 1231         } else {
1275                 if (p->signal->flags & SIGNAL    1232                 if (p->signal->flags & SIGNAL_STOP_STOPPED)
1276                         return &p->signal->gr    1233                         return &p->signal->group_exit_code;
1277         }                                        1234         }
1278         return NULL;                             1235         return NULL;
1279 }                                                1236 }
1280                                                  1237 
1281 /**                                              1238 /**
1282  * wait_task_stopped - Wait for %TASK_STOPPED    1239  * wait_task_stopped - Wait for %TASK_STOPPED or %TASK_TRACED
1283  * @wo: wait options                             1240  * @wo: wait options
1284  * @ptrace: is the wait for ptrace               1241  * @ptrace: is the wait for ptrace
1285  * @p: task to wait for                          1242  * @p: task to wait for
1286  *                                               1243  *
1287  * Handle sys_wait4() work for %p in state %T    1244  * Handle sys_wait4() work for %p in state %TASK_STOPPED or %TASK_TRACED.
1288  *                                               1245  *
1289  * CONTEXT:                                      1246  * CONTEXT:
1290  * read_lock(&tasklist_lock), which is releas    1247  * read_lock(&tasklist_lock), which is released if return value is
1291  * non-zero.  Also, grabs and releases @p->si    1248  * non-zero.  Also, grabs and releases @p->sighand->siglock.
1292  *                                               1249  *
1293  * RETURNS:                                      1250  * RETURNS:
1294  * 0 if wait condition didn't exist and searc    1251  * 0 if wait condition didn't exist and search for other wait conditions
1295  * should continue.  Non-zero return, -errno     1252  * should continue.  Non-zero return, -errno on failure and @p's pid on
1296  * success, implies that tasklist_lock is rel    1253  * success, implies that tasklist_lock is released and wait condition
1297  * search should terminate.                      1254  * search should terminate.
1298  */                                              1255  */
1299 static int wait_task_stopped(struct wait_opts    1256 static int wait_task_stopped(struct wait_opts *wo,
1300                                 int ptrace, s    1257                                 int ptrace, struct task_struct *p)
1301 {                                                1258 {
1302         struct waitid_info *infop;               1259         struct waitid_info *infop;
1303         int exit_code, *p_code, why;             1260         int exit_code, *p_code, why;
1304         uid_t uid = 0; /* unneeded, required     1261         uid_t uid = 0; /* unneeded, required by compiler */
1305         pid_t pid;                               1262         pid_t pid;
1306                                                  1263 
1307         /*                                       1264         /*
1308          * Traditionally we see ptrace'd stop    1265          * Traditionally we see ptrace'd stopped tasks regardless of options.
1309          */                                      1266          */
1310         if (!ptrace && !(wo->wo_flags & WUNTR    1267         if (!ptrace && !(wo->wo_flags & WUNTRACED))
1311                 return 0;                        1268                 return 0;
1312                                                  1269 
1313         if (!task_stopped_code(p, ptrace))       1270         if (!task_stopped_code(p, ptrace))
1314                 return 0;                        1271                 return 0;
1315                                                  1272 
1316         exit_code = 0;                           1273         exit_code = 0;
1317         spin_lock_irq(&p->sighand->siglock);     1274         spin_lock_irq(&p->sighand->siglock);
1318                                                  1275 
1319         p_code = task_stopped_code(p, ptrace)    1276         p_code = task_stopped_code(p, ptrace);
1320         if (unlikely(!p_code))                   1277         if (unlikely(!p_code))
1321                 goto unlock_sig;                 1278                 goto unlock_sig;
1322                                                  1279 
1323         exit_code = *p_code;                     1280         exit_code = *p_code;
1324         if (!exit_code)                          1281         if (!exit_code)
1325                 goto unlock_sig;                 1282                 goto unlock_sig;
1326                                                  1283 
1327         if (!unlikely(wo->wo_flags & WNOWAIT)    1284         if (!unlikely(wo->wo_flags & WNOWAIT))
1328                 *p_code = 0;                     1285                 *p_code = 0;
1329                                                  1286 
1330         uid = from_kuid_munged(current_user_n    1287         uid = from_kuid_munged(current_user_ns(), task_uid(p));
1331 unlock_sig:                                      1288 unlock_sig:
1332         spin_unlock_irq(&p->sighand->siglock)    1289         spin_unlock_irq(&p->sighand->siglock);
1333         if (!exit_code)                          1290         if (!exit_code)
1334                 return 0;                        1291                 return 0;
1335                                                  1292 
1336         /*                                       1293         /*
1337          * Now we are pretty sure this task i    1294          * Now we are pretty sure this task is interesting.
1338          * Make sure it doesn't get reaped ou    1295          * Make sure it doesn't get reaped out from under us while we
1339          * give up the lock and then examine     1296          * give up the lock and then examine it below.  We don't want to
1340          * keep holding onto the tasklist_loc    1297          * keep holding onto the tasklist_lock while we call getrusage and
1341          * possibly take page faults for user    1298          * possibly take page faults for user memory.
1342          */                                      1299          */
1343         get_task_struct(p);                      1300         get_task_struct(p);
1344         pid = task_pid_vnr(p);                   1301         pid = task_pid_vnr(p);
1345         why = ptrace ? CLD_TRAPPED : CLD_STOP    1302         why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
1346         read_unlock(&tasklist_lock);             1303         read_unlock(&tasklist_lock);
1347         sched_annotate_sleep();                  1304         sched_annotate_sleep();
1348         if (wo->wo_rusage)                       1305         if (wo->wo_rusage)
1349                 getrusage(p, RUSAGE_BOTH, wo-    1306                 getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
1350         put_task_struct(p);                      1307         put_task_struct(p);
1351                                                  1308 
1352         if (likely(!(wo->wo_flags & WNOWAIT))    1309         if (likely(!(wo->wo_flags & WNOWAIT)))
1353                 wo->wo_stat = (exit_code << 8    1310                 wo->wo_stat = (exit_code << 8) | 0x7f;
1354                                                  1311 
1355         infop = wo->wo_info;                     1312         infop = wo->wo_info;
1356         if (infop) {                             1313         if (infop) {
1357                 infop->cause = why;              1314                 infop->cause = why;
1358                 infop->status = exit_code;       1315                 infop->status = exit_code;
1359                 infop->pid = pid;                1316                 infop->pid = pid;
1360                 infop->uid = uid;                1317                 infop->uid = uid;
1361         }                                        1318         }
1362         return pid;                              1319         return pid;
1363 }                                                1320 }
1364                                                  1321 
1365 /*                                               1322 /*
1366  * Handle do_wait work for one task in a live    1323  * Handle do_wait work for one task in a live, non-stopped state.
1367  * read_lock(&tasklist_lock) on entry.  If we    1324  * read_lock(&tasklist_lock) on entry.  If we return zero, we still hold
1368  * the lock and this task is uninteresting.      1325  * the lock and this task is uninteresting.  If we return nonzero, we have
1369  * released the lock and the system call shou    1326  * released the lock and the system call should return.
1370  */                                              1327  */
1371 static int wait_task_continued(struct wait_op    1328 static int wait_task_continued(struct wait_opts *wo, struct task_struct *p)
1372 {                                                1329 {
1373         struct waitid_info *infop;               1330         struct waitid_info *infop;
1374         pid_t pid;                               1331         pid_t pid;
1375         uid_t uid;                               1332         uid_t uid;
1376                                                  1333 
1377         if (!unlikely(wo->wo_flags & WCONTINU    1334         if (!unlikely(wo->wo_flags & WCONTINUED))
1378                 return 0;                        1335                 return 0;
1379                                                  1336 
1380         if (!(p->signal->flags & SIGNAL_STOP_    1337         if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
1381                 return 0;                        1338                 return 0;
1382                                                  1339 
1383         spin_lock_irq(&p->sighand->siglock);     1340         spin_lock_irq(&p->sighand->siglock);
1384         /* Re-check with the lock held.  */      1341         /* Re-check with the lock held.  */
1385         if (!(p->signal->flags & SIGNAL_STOP_    1342         if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
1386                 spin_unlock_irq(&p->sighand->    1343                 spin_unlock_irq(&p->sighand->siglock);
1387                 return 0;                        1344                 return 0;
1388         }                                        1345         }
1389         if (!unlikely(wo->wo_flags & WNOWAIT)    1346         if (!unlikely(wo->wo_flags & WNOWAIT))
1390                 p->signal->flags &= ~SIGNAL_S    1347                 p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
1391         uid = from_kuid_munged(current_user_n    1348         uid = from_kuid_munged(current_user_ns(), task_uid(p));
1392         spin_unlock_irq(&p->sighand->siglock)    1349         spin_unlock_irq(&p->sighand->siglock);
1393                                                  1350 
1394         pid = task_pid_vnr(p);                   1351         pid = task_pid_vnr(p);
1395         get_task_struct(p);                      1352         get_task_struct(p);
1396         read_unlock(&tasklist_lock);             1353         read_unlock(&tasklist_lock);
1397         sched_annotate_sleep();                  1354         sched_annotate_sleep();
1398         if (wo->wo_rusage)                       1355         if (wo->wo_rusage)
1399                 getrusage(p, RUSAGE_BOTH, wo-    1356                 getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
1400         put_task_struct(p);                      1357         put_task_struct(p);
1401                                                  1358 
1402         infop = wo->wo_info;                     1359         infop = wo->wo_info;
1403         if (!infop) {                            1360         if (!infop) {
1404                 wo->wo_stat = 0xffff;            1361                 wo->wo_stat = 0xffff;
1405         } else {                                 1362         } else {
1406                 infop->cause = CLD_CONTINUED;    1363                 infop->cause = CLD_CONTINUED;
1407                 infop->pid = pid;                1364                 infop->pid = pid;
1408                 infop->uid = uid;                1365                 infop->uid = uid;
1409                 infop->status = SIGCONT;         1366                 infop->status = SIGCONT;
1410         }                                        1367         }
1411         return pid;                              1368         return pid;
1412 }                                                1369 }
1413                                                  1370 
1414 /*                                               1371 /*
1415  * Consider @p for a wait by @parent.            1372  * Consider @p for a wait by @parent.
1416  *                                               1373  *
1417  * -ECHILD should be in ->notask_error before    1374  * -ECHILD should be in ->notask_error before the first call.
1418  * Returns nonzero for a final return, when w    1375  * Returns nonzero for a final return, when we have unlocked tasklist_lock.
1419  * Returns zero if the search for a child sho    1376  * Returns zero if the search for a child should continue;
1420  * then ->notask_error is 0 if @p is an eligi    1377  * then ->notask_error is 0 if @p is an eligible child,
1421  * or still -ECHILD.                             1378  * or still -ECHILD.
1422  */                                              1379  */
1423 static int wait_consider_task(struct wait_opt    1380 static int wait_consider_task(struct wait_opts *wo, int ptrace,
1424                                 struct task_s    1381                                 struct task_struct *p)
1425 {                                                1382 {
1426         /*                                       1383         /*
1427          * We can race with wait_task_zombie(    1384          * We can race with wait_task_zombie() from another thread.
1428          * Ensure that EXIT_ZOMBIE -> EXIT_DE    1385          * Ensure that EXIT_ZOMBIE -> EXIT_DEAD/EXIT_TRACE transition
1429          * can't confuse the checks below.       1386          * can't confuse the checks below.
1430          */                                      1387          */
1431         int exit_state = READ_ONCE(p->exit_st    1388         int exit_state = READ_ONCE(p->exit_state);
1432         int ret;                                 1389         int ret;
1433                                                  1390 
1434         if (unlikely(exit_state == EXIT_DEAD)    1391         if (unlikely(exit_state == EXIT_DEAD))
1435                 return 0;                        1392                 return 0;
1436                                                  1393 
1437         ret = eligible_child(wo, ptrace, p);     1394         ret = eligible_child(wo, ptrace, p);
1438         if (!ret)                                1395         if (!ret)
1439                 return ret;                      1396                 return ret;
1440                                                  1397 
1441         if (unlikely(exit_state == EXIT_TRACE    1398         if (unlikely(exit_state == EXIT_TRACE)) {
1442                 /*                               1399                 /*
1443                  * ptrace == 0 means we are t    1400                  * ptrace == 0 means we are the natural parent. In this case
1444                  * we should clear notask_err    1401                  * we should clear notask_error, debugger will notify us.
1445                  */                              1402                  */
1446                 if (likely(!ptrace))             1403                 if (likely(!ptrace))
1447                         wo->notask_error = 0;    1404                         wo->notask_error = 0;
1448                 return 0;                        1405                 return 0;
1449         }                                        1406         }
1450                                                  1407 
1451         if (likely(!ptrace) && unlikely(p->pt    1408         if (likely(!ptrace) && unlikely(p->ptrace)) {
1452                 /*                               1409                 /*
1453                  * If it is traced by its rea    1410                  * If it is traced by its real parent's group, just pretend
1454                  * the caller is ptrace_do_wa    1411                  * the caller is ptrace_do_wait() and reap this child if it
1455                  * is zombie.                    1412                  * is zombie.
1456                  *                               1413                  *
1457                  * This also hides group stop    1414                  * This also hides group stop state from real parent; otherwise
1458                  * a single stop can be repor    1415                  * a single stop can be reported twice as group and ptrace stop.
1459                  * If a ptracer wants to dist    1416                  * If a ptracer wants to distinguish these two events for its
1460                  * own children it should cre    1417                  * own children it should create a separate process which takes
1461                  * the role of real parent.      1418                  * the role of real parent.
1462                  */                              1419                  */
1463                 if (!ptrace_reparented(p))       1420                 if (!ptrace_reparented(p))
1464                         ptrace = 1;              1421                         ptrace = 1;
1465         }                                        1422         }
1466                                                  1423 
1467         /* slay zombie? */                       1424         /* slay zombie? */
1468         if (exit_state == EXIT_ZOMBIE) {         1425         if (exit_state == EXIT_ZOMBIE) {
1469                 /* we don't reap group leader    1426                 /* we don't reap group leaders with subthreads */
1470                 if (!delay_group_leader(p)) {    1427                 if (!delay_group_leader(p)) {
1471                         /*                       1428                         /*
1472                          * A zombie ptracee i    1429                          * A zombie ptracee is only visible to its ptracer.
1473                          * Notification and r    1430                          * Notification and reaping will be cascaded to the
1474                          * real parent when t    1431                          * real parent when the ptracer detaches.
1475                          */                      1432                          */
1476                         if (unlikely(ptrace)     1433                         if (unlikely(ptrace) || likely(!p->ptrace))
1477                                 return wait_t    1434                                 return wait_task_zombie(wo, p);
1478                 }                                1435                 }
1479                                                  1436 
1480                 /*                               1437                 /*
1481                  * Allow access to stopped/co    1438                  * Allow access to stopped/continued state via zombie by
1482                  * falling through.  Clearing    1439                  * falling through.  Clearing of notask_error is complex.
1483                  *                               1440                  *
1484                  * When !@ptrace:                1441                  * When !@ptrace:
1485                  *                               1442                  *
1486                  * If WEXITED is set, notask_    1443                  * If WEXITED is set, notask_error should naturally be
1487                  * cleared.  If not, subset o    1444                  * cleared.  If not, subset of WSTOPPED|WCONTINUED is set,
1488                  * so, if there are live subt    1445                  * so, if there are live subthreads, there are events to
1489                  * wait for.  If all subthrea    1446                  * wait for.  If all subthreads are dead, it's still safe
1490                  * to clear - this function w    1447                  * to clear - this function will be called again in finite
1491                  * amount time once all the s    1448                  * amount time once all the subthreads are released and
1492                  * will then return without c    1449                  * will then return without clearing.
1493                  *                               1450                  *
1494                  * When @ptrace:                 1451                  * When @ptrace:
1495                  *                               1452                  *
1496                  * Stopped state is per-task     1453                  * Stopped state is per-task and thus can't change once the
1497                  * target task dies.  Only co    1454                  * target task dies.  Only continued and exited can happen.
1498                  * Clear notask_error if WCON    1455                  * Clear notask_error if WCONTINUED | WEXITED.
1499                  */                              1456                  */
1500                 if (likely(!ptrace) || (wo->w    1457                 if (likely(!ptrace) || (wo->wo_flags & (WCONTINUED | WEXITED)))
1501                         wo->notask_error = 0;    1458                         wo->notask_error = 0;
1502         } else {                                 1459         } else {
1503                 /*                               1460                 /*
1504                  * @p is alive and it's gonna    1461                  * @p is alive and it's gonna stop, continue or exit, so
1505                  * there always is something     1462                  * there always is something to wait for.
1506                  */                              1463                  */
1507                 wo->notask_error = 0;            1464                 wo->notask_error = 0;
1508         }                                        1465         }
1509                                                  1466 
1510         /*                                       1467         /*
1511          * Wait for stopped.  Depending on @p    1468          * Wait for stopped.  Depending on @ptrace, different stopped state
1512          * is used and the two don't interact    1469          * is used and the two don't interact with each other.
1513          */                                      1470          */
1514         ret = wait_task_stopped(wo, ptrace, p    1471         ret = wait_task_stopped(wo, ptrace, p);
1515         if (ret)                                 1472         if (ret)
1516                 return ret;                      1473                 return ret;
1517                                                  1474 
1518         /*                                       1475         /*
1519          * Wait for continued.  There's only     1476          * Wait for continued.  There's only one continued state and the
1520          * ptracer can consume it which can c    1477          * ptracer can consume it which can confuse the real parent.  Don't
1521          * use WCONTINUED from ptracer.  You     1478          * use WCONTINUED from ptracer.  You don't need or want it.
1522          */                                      1479          */
1523         return wait_task_continued(wo, p);       1480         return wait_task_continued(wo, p);
1524 }                                                1481 }
1525                                                  1482 
1526 /*                                               1483 /*
1527  * Do the work of do_wait() for one thread in    1484  * Do the work of do_wait() for one thread in the group, @tsk.
1528  *                                               1485  *
1529  * -ECHILD should be in ->notask_error before    1486  * -ECHILD should be in ->notask_error before the first call.
1530  * Returns nonzero for a final return, when w    1487  * Returns nonzero for a final return, when we have unlocked tasklist_lock.
1531  * Returns zero if the search for a child sho    1488  * Returns zero if the search for a child should continue; then
1532  * ->notask_error is 0 if there were any elig    1489  * ->notask_error is 0 if there were any eligible children,
1533  * or still -ECHILD.                             1490  * or still -ECHILD.
1534  */                                              1491  */
1535 static int do_wait_thread(struct wait_opts *w    1492 static int do_wait_thread(struct wait_opts *wo, struct task_struct *tsk)
1536 {                                                1493 {
1537         struct task_struct *p;                   1494         struct task_struct *p;
1538                                                  1495 
1539         list_for_each_entry(p, &tsk->children    1496         list_for_each_entry(p, &tsk->children, sibling) {
1540                 int ret = wait_consider_task(    1497                 int ret = wait_consider_task(wo, 0, p);
1541                                                  1498 
1542                 if (ret)                         1499                 if (ret)
1543                         return ret;              1500                         return ret;
1544         }                                        1501         }
1545                                                  1502 
1546         return 0;                                1503         return 0;
1547 }                                                1504 }
1548                                                  1505 
1549 static int ptrace_do_wait(struct wait_opts *w    1506 static int ptrace_do_wait(struct wait_opts *wo, struct task_struct *tsk)
1550 {                                                1507 {
1551         struct task_struct *p;                   1508         struct task_struct *p;
1552                                                  1509 
1553         list_for_each_entry(p, &tsk->ptraced,    1510         list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {
1554                 int ret = wait_consider_task(    1511                 int ret = wait_consider_task(wo, 1, p);
1555                                                  1512 
1556                 if (ret)                         1513                 if (ret)
1557                         return ret;              1514                         return ret;
1558         }                                        1515         }
1559                                                  1516 
1560         return 0;                                1517         return 0;
1561 }                                                1518 }
1562                                                  1519 
1563 bool pid_child_should_wake(struct wait_opts * << 
1564 {                                             << 
1565         if (!eligible_pid(wo, p))             << 
1566                 return false;                 << 
1567                                               << 
1568         if ((wo->wo_flags & __WNOTHREAD) && w << 
1569                 return false;                 << 
1570                                               << 
1571         return true;                          << 
1572 }                                             << 
1573                                               << 
1574 static int child_wait_callback(wait_queue_ent    1520 static int child_wait_callback(wait_queue_entry_t *wait, unsigned mode,
1575                                 int sync, voi    1521                                 int sync, void *key)
1576 {                                                1522 {
1577         struct wait_opts *wo = container_of(w    1523         struct wait_opts *wo = container_of(wait, struct wait_opts,
1578                                                  1524                                                 child_wait);
1579         struct task_struct *p = key;             1525         struct task_struct *p = key;
1580                                                  1526 
1581         if (pid_child_should_wake(wo, p))     !! 1527         if (!eligible_pid(wo, p))
1582                 return default_wake_function( !! 1528                 return 0;
                                                   >> 1529 
                                                   >> 1530         if ((wo->wo_flags & __WNOTHREAD) && wait->private != p->parent)
                                                   >> 1531                 return 0;
1583                                                  1532 
1584         return 0;                             !! 1533         return default_wake_function(wait, mode, sync, key);
1585 }                                                1534 }
1586                                                  1535 
1587 void __wake_up_parent(struct task_struct *p,     1536 void __wake_up_parent(struct task_struct *p, struct task_struct *parent)
1588 {                                                1537 {
1589         __wake_up_sync_key(&parent->signal->w    1538         __wake_up_sync_key(&parent->signal->wait_chldexit,
1590                            TASK_INTERRUPTIBLE    1539                            TASK_INTERRUPTIBLE, p);
1591 }                                                1540 }
1592                                                  1541 
1593 static bool is_effectively_child(struct wait_    1542 static bool is_effectively_child(struct wait_opts *wo, bool ptrace,
1594                                  struct task_    1543                                  struct task_struct *target)
1595 {                                                1544 {
1596         struct task_struct *parent =             1545         struct task_struct *parent =
1597                 !ptrace ? target->real_parent    1546                 !ptrace ? target->real_parent : target->parent;
1598                                                  1547 
1599         return current == parent || (!(wo->wo    1548         return current == parent || (!(wo->wo_flags & __WNOTHREAD) &&
1600                                      same_thr    1549                                      same_thread_group(current, parent));
1601 }                                                1550 }
1602                                                  1551 
1603 /*                                               1552 /*
1604  * Optimization for waiting on PIDTYPE_PID. N    1553  * Optimization for waiting on PIDTYPE_PID. No need to iterate through child
1605  * and tracee lists to find the target task.     1554  * and tracee lists to find the target task.
1606  */                                              1555  */
1607 static int do_wait_pid(struct wait_opts *wo)     1556 static int do_wait_pid(struct wait_opts *wo)
1608 {                                                1557 {
1609         bool ptrace;                             1558         bool ptrace;
1610         struct task_struct *target;              1559         struct task_struct *target;
1611         int retval;                              1560         int retval;
1612                                                  1561 
1613         ptrace = false;                          1562         ptrace = false;
1614         target = pid_task(wo->wo_pid, PIDTYPE    1563         target = pid_task(wo->wo_pid, PIDTYPE_TGID);
1615         if (target && is_effectively_child(wo    1564         if (target && is_effectively_child(wo, ptrace, target)) {
1616                 retval = wait_consider_task(w    1565                 retval = wait_consider_task(wo, ptrace, target);
1617                 if (retval)                      1566                 if (retval)
1618                         return retval;           1567                         return retval;
1619         }                                        1568         }
1620                                                  1569 
1621         ptrace = true;                           1570         ptrace = true;
1622         target = pid_task(wo->wo_pid, PIDTYPE    1571         target = pid_task(wo->wo_pid, PIDTYPE_PID);
1623         if (target && target->ptrace &&          1572         if (target && target->ptrace &&
1624             is_effectively_child(wo, ptrace,     1573             is_effectively_child(wo, ptrace, target)) {
1625                 retval = wait_consider_task(w    1574                 retval = wait_consider_task(wo, ptrace, target);
1626                 if (retval)                      1575                 if (retval)
1627                         return retval;           1576                         return retval;
1628         }                                        1577         }
1629                                                  1578 
1630         return 0;                                1579         return 0;
1631 }                                                1580 }
1632                                                  1581 
1633 long __do_wait(struct wait_opts *wo)          !! 1582 static long do_wait(struct wait_opts *wo)
1634 {                                                1583 {
1635         long retval;                          !! 1584         int retval;
                                                   >> 1585 
                                                   >> 1586         trace_sched_process_wait(wo->wo_pid);
1636                                                  1587 
                                                   >> 1588         init_waitqueue_func_entry(&wo->child_wait, child_wait_callback);
                                                   >> 1589         wo->child_wait.private = current;
                                                   >> 1590         add_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
                                                   >> 1591 repeat:
1637         /*                                       1592         /*
1638          * If there is nothing that can match    1593          * If there is nothing that can match our criteria, just get out.
1639          * We will clear ->notask_error to ze    1594          * We will clear ->notask_error to zero if we see any child that
1640          * might later match our criteria, ev    1595          * might later match our criteria, even if we are not able to reap
1641          * it yet.                               1596          * it yet.
1642          */                                      1597          */
1643         wo->notask_error = -ECHILD;              1598         wo->notask_error = -ECHILD;
1644         if ((wo->wo_type < PIDTYPE_MAX) &&       1599         if ((wo->wo_type < PIDTYPE_MAX) &&
1645            (!wo->wo_pid || !pid_has_task(wo->    1600            (!wo->wo_pid || !pid_has_task(wo->wo_pid, wo->wo_type)))
1646                 goto notask;                     1601                 goto notask;
1647                                                  1602 
                                                   >> 1603         set_current_state(TASK_INTERRUPTIBLE);
1648         read_lock(&tasklist_lock);               1604         read_lock(&tasklist_lock);
1649                                                  1605 
1650         if (wo->wo_type == PIDTYPE_PID) {        1606         if (wo->wo_type == PIDTYPE_PID) {
1651                 retval = do_wait_pid(wo);        1607                 retval = do_wait_pid(wo);
1652                 if (retval)                      1608                 if (retval)
1653                         return retval;        !! 1609                         goto end;
1654         } else {                                 1610         } else {
1655                 struct task_struct *tsk = cur    1611                 struct task_struct *tsk = current;
1656                                                  1612 
1657                 do {                             1613                 do {
1658                         retval = do_wait_thre    1614                         retval = do_wait_thread(wo, tsk);
1659                         if (retval)              1615                         if (retval)
1660                                 return retval !! 1616                                 goto end;
1661                                                  1617 
1662                         retval = ptrace_do_wa    1618                         retval = ptrace_do_wait(wo, tsk);
1663                         if (retval)              1619                         if (retval)
1664                                 return retval !! 1620                                 goto end;
1665                                                  1621 
1666                         if (wo->wo_flags & __    1622                         if (wo->wo_flags & __WNOTHREAD)
1667                                 break;           1623                                 break;
1668                 } while_each_thread(current,     1624                 } while_each_thread(current, tsk);
1669         }                                        1625         }
1670         read_unlock(&tasklist_lock);             1626         read_unlock(&tasklist_lock);
1671                                                  1627 
1672 notask:                                          1628 notask:
1673         retval = wo->notask_error;               1629         retval = wo->notask_error;
1674         if (!retval && !(wo->wo_flags & WNOHA !! 1630         if (!retval && !(wo->wo_flags & WNOHANG)) {
1675                 return -ERESTARTSYS;          !! 1631                 retval = -ERESTARTSYS;
1676                                               !! 1632                 if (!signal_pending(current)) {
1677         return retval;                        !! 1633                         schedule();
1678 }                                             !! 1634                         goto repeat;
1679                                               !! 1635                 }
1680 static long do_wait(struct wait_opts *wo)     !! 1636         }
1681 {                                             !! 1637 end:
1682         int retval;                           << 
1683                                               << 
1684         trace_sched_process_wait(wo->wo_pid); << 
1685                                               << 
1686         init_waitqueue_func_entry(&wo->child_ << 
1687         wo->child_wait.private = current;     << 
1688         add_wait_queue(&current->signal->wait << 
1689                                               << 
1690         do {                                  << 
1691                 set_current_state(TASK_INTERR << 
1692                 retval = __do_wait(wo);       << 
1693                 if (retval != -ERESTARTSYS)   << 
1694                         break;                << 
1695                 if (signal_pending(current))  << 
1696                         break;                << 
1697                 schedule();                   << 
1698         } while (1);                          << 
1699                                               << 
1700         __set_current_state(TASK_RUNNING);       1638         __set_current_state(TASK_RUNNING);
1701         remove_wait_queue(&current->signal->w    1639         remove_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
1702         return retval;                           1640         return retval;
1703 }                                                1641 }
1704                                                  1642 
1705 int kernel_waitid_prepare(struct wait_opts *w !! 1643 static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop,
1706                           struct waitid_info  !! 1644                           int options, struct rusage *ru)
1707                           struct rusage *ru)  << 
1708 {                                                1645 {
1709         unsigned int f_flags = 0;             !! 1646         struct wait_opts wo;
1710         struct pid *pid = NULL;                  1647         struct pid *pid = NULL;
1711         enum pid_type type;                      1648         enum pid_type type;
                                                   >> 1649         long ret;
                                                   >> 1650         unsigned int f_flags = 0;
1712                                                  1651 
1713         if (options & ~(WNOHANG|WNOWAIT|WEXIT    1652         if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED|
1714                         __WNOTHREAD|__WCLONE|    1653                         __WNOTHREAD|__WCLONE|__WALL))
1715                 return -EINVAL;                  1654                 return -EINVAL;
1716         if (!(options & (WEXITED|WSTOPPED|WCO    1655         if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
1717                 return -EINVAL;                  1656                 return -EINVAL;
1718                                                  1657 
1719         switch (which) {                         1658         switch (which) {
1720         case P_ALL:                              1659         case P_ALL:
1721                 type = PIDTYPE_MAX;              1660                 type = PIDTYPE_MAX;
1722                 break;                           1661                 break;
1723         case P_PID:                              1662         case P_PID:
1724                 type = PIDTYPE_PID;              1663                 type = PIDTYPE_PID;
1725                 if (upid <= 0)                   1664                 if (upid <= 0)
1726                         return -EINVAL;          1665                         return -EINVAL;
1727                                                  1666 
1728                 pid = find_get_pid(upid);        1667                 pid = find_get_pid(upid);
1729                 break;                           1668                 break;
1730         case P_PGID:                             1669         case P_PGID:
1731                 type = PIDTYPE_PGID;             1670                 type = PIDTYPE_PGID;
1732                 if (upid < 0)                    1671                 if (upid < 0)
1733                         return -EINVAL;          1672                         return -EINVAL;
1734                                                  1673 
1735                 if (upid)                        1674                 if (upid)
1736                         pid = find_get_pid(up    1675                         pid = find_get_pid(upid);
1737                 else                             1676                 else
1738                         pid = get_task_pid(cu    1677                         pid = get_task_pid(current, PIDTYPE_PGID);
1739                 break;                           1678                 break;
1740         case P_PIDFD:                            1679         case P_PIDFD:
1741                 type = PIDTYPE_PID;              1680                 type = PIDTYPE_PID;
1742                 if (upid < 0)                    1681                 if (upid < 0)
1743                         return -EINVAL;          1682                         return -EINVAL;
1744                                                  1683 
1745                 pid = pidfd_get_pid(upid, &f_    1684                 pid = pidfd_get_pid(upid, &f_flags);
1746                 if (IS_ERR(pid))                 1685                 if (IS_ERR(pid))
1747                         return PTR_ERR(pid);     1686                         return PTR_ERR(pid);
1748                                                  1687 
1749                 break;                           1688                 break;
1750         default:                                 1689         default:
1751                 return -EINVAL;                  1690                 return -EINVAL;
1752         }                                        1691         }
1753                                                  1692 
1754         wo->wo_type     = type;               !! 1693         wo.wo_type      = type;
1755         wo->wo_pid      = pid;                !! 1694         wo.wo_pid       = pid;
1756         wo->wo_flags    = options;            !! 1695         wo.wo_flags     = options;
1757         wo->wo_info     = infop;              !! 1696         wo.wo_info      = infop;
1758         wo->wo_rusage   = ru;                 !! 1697         wo.wo_rusage    = ru;
1759         if (f_flags & O_NONBLOCK)                1698         if (f_flags & O_NONBLOCK)
1760                 wo->wo_flags |= WNOHANG;      !! 1699                 wo.wo_flags |= WNOHANG;
1761                                               << 
1762         return 0;                             << 
1763 }                                             << 
1764                                               << 
1765 static long kernel_waitid(int which, pid_t up << 
1766                           int options, struct << 
1767 {                                             << 
1768         struct wait_opts wo;                  << 
1769         long ret;                             << 
1770                                               << 
1771         ret = kernel_waitid_prepare(&wo, whic << 
1772         if (ret)                              << 
1773                 return ret;                   << 
1774                                                  1700 
1775         ret = do_wait(&wo);                      1701         ret = do_wait(&wo);
1776         if (!ret && !(options & WNOHANG) && ( !! 1702         if (!ret && !(options & WNOHANG) && (f_flags & O_NONBLOCK))
1777                 ret = -EAGAIN;                   1703                 ret = -EAGAIN;
1778                                                  1704 
1779         put_pid(wo.wo_pid);                   !! 1705         put_pid(pid);
1780         return ret;                              1706         return ret;
1781 }                                                1707 }
1782                                                  1708 
1783 SYSCALL_DEFINE5(waitid, int, which, pid_t, up    1709 SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
1784                 infop, int, options, struct r    1710                 infop, int, options, struct rusage __user *, ru)
1785 {                                                1711 {
1786         struct rusage r;                         1712         struct rusage r;
1787         struct waitid_info info = {.status =     1713         struct waitid_info info = {.status = 0};
1788         long err = kernel_waitid(which, upid,    1714         long err = kernel_waitid(which, upid, &info, options, ru ? &r : NULL);
1789         int signo = 0;                           1715         int signo = 0;
1790                                                  1716 
1791         if (err > 0) {                           1717         if (err > 0) {
1792                 signo = SIGCHLD;                 1718                 signo = SIGCHLD;
1793                 err = 0;                         1719                 err = 0;
1794                 if (ru && copy_to_user(ru, &r    1720                 if (ru && copy_to_user(ru, &r, sizeof(struct rusage)))
1795                         return -EFAULT;          1721                         return -EFAULT;
1796         }                                        1722         }
1797         if (!infop)                              1723         if (!infop)
1798                 return err;                      1724                 return err;
1799                                                  1725 
1800         if (!user_write_access_begin(infop, s    1726         if (!user_write_access_begin(infop, sizeof(*infop)))
1801                 return -EFAULT;                  1727                 return -EFAULT;
1802                                                  1728 
1803         unsafe_put_user(signo, &infop->si_sig    1729         unsafe_put_user(signo, &infop->si_signo, Efault);
1804         unsafe_put_user(0, &infop->si_errno,     1730         unsafe_put_user(0, &infop->si_errno, Efault);
1805         unsafe_put_user(info.cause, &infop->s    1731         unsafe_put_user(info.cause, &infop->si_code, Efault);
1806         unsafe_put_user(info.pid, &infop->si_    1732         unsafe_put_user(info.pid, &infop->si_pid, Efault);
1807         unsafe_put_user(info.uid, &infop->si_    1733         unsafe_put_user(info.uid, &infop->si_uid, Efault);
1808         unsafe_put_user(info.status, &infop->    1734         unsafe_put_user(info.status, &infop->si_status, Efault);
1809         user_write_access_end();                 1735         user_write_access_end();
1810         return err;                              1736         return err;
1811 Efault:                                          1737 Efault:
1812         user_write_access_end();                 1738         user_write_access_end();
1813         return -EFAULT;                          1739         return -EFAULT;
1814 }                                                1740 }
1815                                                  1741 
1816 long kernel_wait4(pid_t upid, int __user *sta    1742 long kernel_wait4(pid_t upid, int __user *stat_addr, int options,
1817                   struct rusage *ru)             1743                   struct rusage *ru)
1818 {                                                1744 {
1819         struct wait_opts wo;                     1745         struct wait_opts wo;
1820         struct pid *pid = NULL;                  1746         struct pid *pid = NULL;
1821         enum pid_type type;                      1747         enum pid_type type;
1822         long ret;                                1748         long ret;
1823                                                  1749 
1824         if (options & ~(WNOHANG|WUNTRACED|WCO    1750         if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
1825                         __WNOTHREAD|__WCLONE|    1751                         __WNOTHREAD|__WCLONE|__WALL))
1826                 return -EINVAL;                  1752                 return -EINVAL;
1827                                                  1753 
1828         /* -INT_MIN is not defined */            1754         /* -INT_MIN is not defined */
1829         if (upid == INT_MIN)                     1755         if (upid == INT_MIN)
1830                 return -ESRCH;                   1756                 return -ESRCH;
1831                                                  1757 
1832         if (upid == -1)                          1758         if (upid == -1)
1833                 type = PIDTYPE_MAX;              1759                 type = PIDTYPE_MAX;
1834         else if (upid < 0) {                     1760         else if (upid < 0) {
1835                 type = PIDTYPE_PGID;             1761                 type = PIDTYPE_PGID;
1836                 pid = find_get_pid(-upid);       1762                 pid = find_get_pid(-upid);
1837         } else if (upid == 0) {                  1763         } else if (upid == 0) {
1838                 type = PIDTYPE_PGID;             1764                 type = PIDTYPE_PGID;
1839                 pid = get_task_pid(current, P    1765                 pid = get_task_pid(current, PIDTYPE_PGID);
1840         } else /* upid > 0 */ {                  1766         } else /* upid > 0 */ {
1841                 type = PIDTYPE_PID;              1767                 type = PIDTYPE_PID;
1842                 pid = find_get_pid(upid);        1768                 pid = find_get_pid(upid);
1843         }                                        1769         }
1844                                                  1770 
1845         wo.wo_type      = type;                  1771         wo.wo_type      = type;
1846         wo.wo_pid       = pid;                   1772         wo.wo_pid       = pid;
1847         wo.wo_flags     = options | WEXITED;     1773         wo.wo_flags     = options | WEXITED;
1848         wo.wo_info      = NULL;                  1774         wo.wo_info      = NULL;
1849         wo.wo_stat      = 0;                     1775         wo.wo_stat      = 0;
1850         wo.wo_rusage    = ru;                    1776         wo.wo_rusage    = ru;
1851         ret = do_wait(&wo);                      1777         ret = do_wait(&wo);
1852         put_pid(pid);                            1778         put_pid(pid);
1853         if (ret > 0 && stat_addr && put_user(    1779         if (ret > 0 && stat_addr && put_user(wo.wo_stat, stat_addr))
1854                 ret = -EFAULT;                   1780                 ret = -EFAULT;
1855                                                  1781 
1856         return ret;                              1782         return ret;
1857 }                                                1783 }
1858                                                  1784 
1859 int kernel_wait(pid_t pid, int *stat)            1785 int kernel_wait(pid_t pid, int *stat)
1860 {                                                1786 {
1861         struct wait_opts wo = {                  1787         struct wait_opts wo = {
1862                 .wo_type        = PIDTYPE_PID    1788                 .wo_type        = PIDTYPE_PID,
1863                 .wo_pid         = find_get_pi    1789                 .wo_pid         = find_get_pid(pid),
1864                 .wo_flags       = WEXITED,       1790                 .wo_flags       = WEXITED,
1865         };                                       1791         };
1866         int ret;                                 1792         int ret;
1867                                                  1793 
1868         ret = do_wait(&wo);                      1794         ret = do_wait(&wo);
1869         if (ret > 0 && wo.wo_stat)               1795         if (ret > 0 && wo.wo_stat)
1870                 *stat = wo.wo_stat;              1796                 *stat = wo.wo_stat;
1871         put_pid(wo.wo_pid);                      1797         put_pid(wo.wo_pid);
1872         return ret;                              1798         return ret;
1873 }                                                1799 }
1874                                                  1800 
1875 SYSCALL_DEFINE4(wait4, pid_t, upid, int __use    1801 SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
1876                 int, options, struct rusage _    1802                 int, options, struct rusage __user *, ru)
1877 {                                                1803 {
1878         struct rusage r;                         1804         struct rusage r;
1879         long err = kernel_wait4(upid, stat_ad    1805         long err = kernel_wait4(upid, stat_addr, options, ru ? &r : NULL);
1880                                                  1806 
1881         if (err > 0) {                           1807         if (err > 0) {
1882                 if (ru && copy_to_user(ru, &r    1808                 if (ru && copy_to_user(ru, &r, sizeof(struct rusage)))
1883                         return -EFAULT;          1809                         return -EFAULT;
1884         }                                        1810         }
1885         return err;                              1811         return err;
1886 }                                                1812 }
1887                                                  1813 
1888 #ifdef __ARCH_WANT_SYS_WAITPID                   1814 #ifdef __ARCH_WANT_SYS_WAITPID
1889                                                  1815 
1890 /*                                               1816 /*
1891  * sys_waitpid() remains for compatibility. w    1817  * sys_waitpid() remains for compatibility. waitpid() should be
1892  * implemented by calling sys_wait4() from li    1818  * implemented by calling sys_wait4() from libc.a.
1893  */                                              1819  */
1894 SYSCALL_DEFINE3(waitpid, pid_t, pid, int __us    1820 SYSCALL_DEFINE3(waitpid, pid_t, pid, int __user *, stat_addr, int, options)
1895 {                                                1821 {
1896         return kernel_wait4(pid, stat_addr, o    1822         return kernel_wait4(pid, stat_addr, options, NULL);
1897 }                                                1823 }
1898                                                  1824 
1899 #endif                                           1825 #endif
1900                                                  1826 
1901 #ifdef CONFIG_COMPAT                             1827 #ifdef CONFIG_COMPAT
1902 COMPAT_SYSCALL_DEFINE4(wait4,                    1828 COMPAT_SYSCALL_DEFINE4(wait4,
1903         compat_pid_t, pid,                       1829         compat_pid_t, pid,
1904         compat_uint_t __user *, stat_addr,       1830         compat_uint_t __user *, stat_addr,
1905         int, options,                            1831         int, options,
1906         struct compat_rusage __user *, ru)       1832         struct compat_rusage __user *, ru)
1907 {                                                1833 {
1908         struct rusage r;                         1834         struct rusage r;
1909         long err = kernel_wait4(pid, stat_add    1835         long err = kernel_wait4(pid, stat_addr, options, ru ? &r : NULL);
1910         if (err > 0) {                           1836         if (err > 0) {
1911                 if (ru && put_compat_rusage(&    1837                 if (ru && put_compat_rusage(&r, ru))
1912                         return -EFAULT;          1838                         return -EFAULT;
1913         }                                        1839         }
1914         return err;                              1840         return err;
1915 }                                                1841 }
1916                                                  1842 
1917 COMPAT_SYSCALL_DEFINE5(waitid,                   1843 COMPAT_SYSCALL_DEFINE5(waitid,
1918                 int, which, compat_pid_t, pid    1844                 int, which, compat_pid_t, pid,
1919                 struct compat_siginfo __user     1845                 struct compat_siginfo __user *, infop, int, options,
1920                 struct compat_rusage __user *    1846                 struct compat_rusage __user *, uru)
1921 {                                                1847 {
1922         struct rusage ru;                        1848         struct rusage ru;
1923         struct waitid_info info = {.status =     1849         struct waitid_info info = {.status = 0};
1924         long err = kernel_waitid(which, pid,     1850         long err = kernel_waitid(which, pid, &info, options, uru ? &ru : NULL);
1925         int signo = 0;                           1851         int signo = 0;
1926         if (err > 0) {                           1852         if (err > 0) {
1927                 signo = SIGCHLD;                 1853                 signo = SIGCHLD;
1928                 err = 0;                         1854                 err = 0;
1929                 if (uru) {                       1855                 if (uru) {
1930                         /* kernel_waitid() ov    1856                         /* kernel_waitid() overwrites everything in ru */
1931                         if (COMPAT_USE_64BIT_    1857                         if (COMPAT_USE_64BIT_TIME)
1932                                 err = copy_to    1858                                 err = copy_to_user(uru, &ru, sizeof(ru));
1933                         else                     1859                         else
1934                                 err = put_com    1860                                 err = put_compat_rusage(&ru, uru);
1935                         if (err)                 1861                         if (err)
1936                                 return -EFAUL    1862                                 return -EFAULT;
1937                 }                                1863                 }
1938         }                                        1864         }
1939                                                  1865 
1940         if (!infop)                              1866         if (!infop)
1941                 return err;                      1867                 return err;
1942                                                  1868 
1943         if (!user_write_access_begin(infop, s    1869         if (!user_write_access_begin(infop, sizeof(*infop)))
1944                 return -EFAULT;                  1870                 return -EFAULT;
1945                                                  1871 
1946         unsafe_put_user(signo, &infop->si_sig    1872         unsafe_put_user(signo, &infop->si_signo, Efault);
1947         unsafe_put_user(0, &infop->si_errno,     1873         unsafe_put_user(0, &infop->si_errno, Efault);
1948         unsafe_put_user(info.cause, &infop->s    1874         unsafe_put_user(info.cause, &infop->si_code, Efault);
1949         unsafe_put_user(info.pid, &infop->si_    1875         unsafe_put_user(info.pid, &infop->si_pid, Efault);
1950         unsafe_put_user(info.uid, &infop->si_    1876         unsafe_put_user(info.uid, &infop->si_uid, Efault);
1951         unsafe_put_user(info.status, &infop->    1877         unsafe_put_user(info.status, &infop->si_status, Efault);
1952         user_write_access_end();                 1878         user_write_access_end();
1953         return err;                              1879         return err;
1954 Efault:                                          1880 Efault:
1955         user_write_access_end();                 1881         user_write_access_end();
1956         return -EFAULT;                          1882         return -EFAULT;
1957 }                                                1883 }
1958 #endif                                           1884 #endif
1959                                                  1885 
1960 /*                                            !! 1886 /**
1961  * This needs to be __function_aligned as GCC !! 1887  * thread_group_exited - check that a thread group has exited
1962  * implementation of abort() cold and drops a !! 1888  * @pid: tgid of thread group to be checked.
1963  * -falign-functions=N.                       << 
1964  *                                               1889  *
1965  * See https://gcc.gnu.org/bugzilla/show_bug. !! 1890  * Test if the thread group represented by tgid has exited (all
                                                   >> 1891  * threads are zombies, dead or completely gone).
                                                   >> 1892  *
                                                   >> 1893  * Return: true if the thread group has exited. false otherwise.
1966  */                                              1894  */
1967 __weak __function_aligned void abort(void)    !! 1895 bool thread_group_exited(struct pid *pid)
                                                   >> 1896 {
                                                   >> 1897         struct task_struct *task;
                                                   >> 1898         bool exited;
                                                   >> 1899 
                                                   >> 1900         rcu_read_lock();
                                                   >> 1901         task = pid_task(pid, PIDTYPE_PID);
                                                   >> 1902         exited = !task ||
                                                   >> 1903                 (READ_ONCE(task->exit_state) && thread_group_empty(task));
                                                   >> 1904         rcu_read_unlock();
                                                   >> 1905 
                                                   >> 1906         return exited;
                                                   >> 1907 }
                                                   >> 1908 EXPORT_SYMBOL(thread_group_exited);
                                                   >> 1909 
                                                   >> 1910 __weak void abort(void)
1968 {                                                1911 {
1969         BUG();                                   1912         BUG();
1970                                                  1913 
1971         /* if that doesn't kill us, halt */      1914         /* if that doesn't kill us, halt */
1972         panic("Oops failed to kill thread");     1915         panic("Oops failed to kill thread");
1973 }                                                1916 }
1974 EXPORT_SYMBOL(abort);                            1917 EXPORT_SYMBOL(abort);
1975                                                  1918 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php