exit.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777
  1. /*
  2. * linux/kernel/exit.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/mm.h>
  7. #include <linux/slab.h>
  8. #include <linux/sched/autogroup.h>
  9. #include <linux/sched/mm.h>
  10. #include <linux/sched/stat.h>
  11. #include <linux/sched/task.h>
  12. #include <linux/sched/task_stack.h>
  13. #include <linux/sched/cputime.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/module.h>
  16. #include <linux/capability.h>
  17. #include <linux/completion.h>
  18. #include <linux/personality.h>
  19. #include <linux/tty.h>
  20. #include <linux/iocontext.h>
  21. #include <linux/key.h>
  22. #include <linux/cpu.h>
  23. #include <linux/acct.h>
  24. #include <linux/tsacct_kern.h>
  25. #include <linux/file.h>
  26. #include <linux/fdtable.h>
  27. #include <linux/freezer.h>
  28. #include <linux/binfmts.h>
  29. #include <linux/nsproxy.h>
  30. #include <linux/pid_namespace.h>
  31. #include <linux/ptrace.h>
  32. #include <linux/profile.h>
  33. #include <linux/mount.h>
  34. #include <linux/proc_fs.h>
  35. #include <linux/kthread.h>
  36. #include <linux/mempolicy.h>
  37. #include <linux/taskstats_kern.h>
  38. #include <linux/delayacct.h>
  39. #include <linux/cgroup.h>
  40. #include <linux/syscalls.h>
  41. #include <linux/signal.h>
  42. #include <linux/posix-timers.h>
  43. #include <linux/cn_proc.h>
  44. #include <linux/mutex.h>
  45. #include <linux/futex.h>
  46. #include <linux/pipe_fs_i.h>
  47. #include <linux/audit.h> /* for audit_free() */
  48. #include <linux/resource.h>
  49. #include <linux/blkdev.h>
  50. #include <linux/task_io_accounting_ops.h>
  51. #include <linux/tracehook.h>
  52. #include <linux/fs_struct.h>
  53. #include <linux/userfaultfd_k.h>
  54. #include <linux/init_task.h>
  55. #include <linux/perf_event.h>
  56. #include <trace/events/sched.h>
  57. #include <linux/hw_breakpoint.h>
  58. #include <linux/oom.h>
  59. #include <linux/writeback.h>
  60. #include <linux/shm.h>
  61. #include <linux/kcov.h>
  62. #include <linux/random.h>
  63. #include <linux/rcuwait.h>
  64. #include <linux/compat.h>
  65. #include <linux/uaccess.h>
  66. #include <asm/unistd.h>
  67. #include <asm/pgtable.h>
  68. #include <asm/mmu_context.h>
  69. static void __unhash_process(struct task_struct *p, bool group_dead)
  70. {
  71. nr_threads--;
  72. detach_pid(p, PIDTYPE_PID);
  73. if (group_dead) {
  74. detach_pid(p, PIDTYPE_PGID);
  75. detach_pid(p, PIDTYPE_SID);
  76. list_del_rcu(&p->tasks);
  77. list_del_init(&p->sibling);
  78. __this_cpu_dec(process_counts);
  79. }
  80. list_del_rcu(&p->thread_group);
  81. list_del_rcu(&p->thread_node);
  82. }
  83. /*
  84. * This function expects the tasklist_lock write-locked.
  85. */
  86. static void __exit_signal(struct task_struct *tsk)
  87. {
  88. struct signal_struct *sig = tsk->signal;
  89. bool group_dead = thread_group_leader(tsk);
  90. struct sighand_struct *sighand;
  91. struct tty_struct *uninitialized_var(tty);
  92. u64 utime, stime;
  93. sighand = rcu_dereference_check(tsk->sighand,
  94. lockdep_tasklist_lock_is_held());
  95. spin_lock(&sighand->siglock);
  96. #ifdef CONFIG_POSIX_TIMERS
  97. posix_cpu_timers_exit(tsk);
  98. if (group_dead) {
  99. posix_cpu_timers_exit_group(tsk);
  100. } else {
  101. /*
  102. * This can only happen if the caller is de_thread().
  103. * FIXME: this is the temporary hack, we should teach
  104. * posix-cpu-timers to handle this case correctly.
  105. */
  106. if (unlikely(has_group_leader_pid(tsk)))
  107. posix_cpu_timers_exit_group(tsk);
  108. }
  109. #endif
  110. if (group_dead) {
  111. tty = sig->tty;
  112. sig->tty = NULL;
  113. } else {
  114. /*
  115. * If there is any task waiting for the group exit
  116. * then notify it:
  117. */
  118. if (sig->notify_count > 0 && !--sig->notify_count)
  119. wake_up_process(sig->group_exit_task);
  120. if (tsk == sig->curr_target)
  121. sig->curr_target = next_thread(tsk);
  122. }
  123. add_device_randomness((const void*) &tsk->se.sum_exec_runtime,
  124. sizeof(unsigned long long));
  125. /*
  126. * Accumulate here the counters for all threads as they die. We could
  127. * skip the group leader because it is the last user of signal_struct,
  128. * but we want to avoid the race with thread_group_cputime() which can
  129. * see the empty ->thread_head list.
  130. */
  131. task_cputime(tsk, &utime, &stime);
  132. write_seqlock(&sig->stats_lock);
  133. sig->utime += utime;
  134. sig->stime += stime;
  135. sig->gtime += task_gtime(tsk);
  136. sig->min_flt += tsk->min_flt;
  137. sig->maj_flt += tsk->maj_flt;
  138. sig->nvcsw += tsk->nvcsw;
  139. sig->nivcsw += tsk->nivcsw;
  140. sig->inblock += task_io_get_inblock(tsk);
  141. sig->oublock += task_io_get_oublock(tsk);
  142. task_io_accounting_add(&sig->ioac, &tsk->ioac);
  143. sig->sum_sched_runtime += tsk->se.sum_exec_runtime;
  144. sig->nr_threads--;
  145. __unhash_process(tsk, group_dead);
  146. write_sequnlock(&sig->stats_lock);
  147. /*
  148. * Do this under ->siglock, we can race with another thread
  149. * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.
  150. */
  151. flush_sigqueue(&tsk->pending);
  152. tsk->sighand = NULL;
  153. spin_unlock(&sighand->siglock);
  154. __cleanup_sighand(sighand);
  155. clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
  156. if (group_dead) {
  157. flush_sigqueue(&sig->shared_pending);
  158. tty_kref_put(tty);
  159. }
  160. }
  161. static void delayed_put_task_struct(struct rcu_head *rhp)
  162. {
  163. struct task_struct *tsk = container_of(rhp, struct task_struct, rcu);
  164. perf_event_delayed_put(tsk);
  165. trace_sched_process_free(tsk);
  166. put_task_struct(tsk);
  167. }
  168. void release_task(struct task_struct *p)
  169. {
  170. struct task_struct *leader;
  171. int zap_leader;
  172. repeat:
  173. /* don't need to get the RCU readlock here - the process is dead and
  174. * can't be modifying its own credentials. But shut RCU-lockdep up */
  175. rcu_read_lock();
  176. atomic_dec(&__task_cred(p)->user->processes);
  177. rcu_read_unlock();
  178. proc_flush_task(p);
  179. write_lock_irq(&tasklist_lock);
  180. ptrace_release_task(p);
  181. __exit_signal(p);
  182. /*
  183. * If we are the last non-leader member of the thread
  184. * group, and the leader is zombie, then notify the
  185. * group leader's parent process. (if it wants notification.)
  186. */
  187. zap_leader = 0;
  188. leader = p->group_leader;
  189. if (leader != p && thread_group_empty(leader)
  190. && leader->exit_state == EXIT_ZOMBIE) {
  191. /*
  192. * If we were the last child thread and the leader has
  193. * exited already, and the leader's parent ignores SIGCHLD,
  194. * then we are the one who should release the leader.
  195. */
  196. zap_leader = do_notify_parent(leader, leader->exit_signal);
  197. if (zap_leader)
  198. leader->exit_state = EXIT_DEAD;
  199. }
  200. write_unlock_irq(&tasklist_lock);
  201. release_thread(p);
  202. call_rcu(&p->rcu, delayed_put_task_struct);
  203. p = leader;
  204. if (unlikely(zap_leader))
  205. goto repeat;
  206. }
  207. /*
  208. * Note that if this function returns a valid task_struct pointer (!NULL)
  209. * task->usage must remain >0 for the duration of the RCU critical section.
  210. */
  211. struct task_struct *task_rcu_dereference(struct task_struct **ptask)
  212. {
  213. struct sighand_struct *sighand;
  214. struct task_struct *task;
  215. /*
  216. * We need to verify that release_task() was not called and thus
  217. * delayed_put_task_struct() can't run and drop the last reference
  218. * before rcu_read_unlock(). We check task->sighand != NULL,
  219. * but we can read the already freed and reused memory.
  220. */
  221. retry:
  222. task = rcu_dereference(*ptask);
  223. if (!task)
  224. return NULL;
  225. probe_kernel_address(&task->sighand, sighand);
  226. /*
  227. * Pairs with atomic_dec_and_test() in put_task_struct(). If this task
  228. * was already freed we can not miss the preceding update of this
  229. * pointer.
  230. */
  231. smp_rmb();
  232. if (unlikely(task != READ_ONCE(*ptask)))
  233. goto retry;
  234. /*
  235. * We've re-checked that "task == *ptask", now we have two different
  236. * cases:
  237. *
  238. * 1. This is actually the same task/task_struct. In this case
  239. * sighand != NULL tells us it is still alive.
  240. *
  241. * 2. This is another task which got the same memory for task_struct.
  242. * We can't know this of course, and we can not trust
  243. * sighand != NULL.
  244. *
  245. * In this case we actually return a random value, but this is
  246. * correct.
  247. *
  248. * If we return NULL - we can pretend that we actually noticed that
  249. * *ptask was updated when the previous task has exited. Or pretend
  250. * that probe_slab_address(&sighand) reads NULL.
  251. *
  252. * If we return the new task (because sighand is not NULL for any
  253. * reason) - this is fine too. This (new) task can't go away before
  254. * another gp pass.
  255. *
  256. * And note: We could even eliminate the false positive if re-read
  257. * task->sighand once again to avoid the falsely NULL. But this case
  258. * is very unlikely so we don't care.
  259. */
  260. if (!sighand)
  261. return NULL;
  262. return task;
  263. }
  264. void rcuwait_wake_up(struct rcuwait *w)
  265. {
  266. struct task_struct *task;
  267. rcu_read_lock();
  268. /*
  269. * Order condition vs @task, such that everything prior to the load
  270. * of @task is visible. This is the condition as to why the user called
  271. * rcuwait_trywake() in the first place. Pairs with set_current_state()
  272. * barrier (A) in rcuwait_wait_event().
  273. *
  274. * WAIT WAKE
  275. * [S] tsk = current [S] cond = true
  276. * MB (A) MB (B)
  277. * [L] cond [L] tsk
  278. */
  279. smp_rmb(); /* (B) */
  280. /*
  281. * Avoid using task_rcu_dereference() magic as long as we are careful,
  282. * see comment in rcuwait_wait_event() regarding ->exit_state.
  283. */
  284. task = rcu_dereference(w->task);
  285. if (task)
  286. wake_up_process(task);
  287. rcu_read_unlock();
  288. }
  289. struct task_struct *try_get_task_struct(struct task_struct **ptask)
  290. {
  291. struct task_struct *task;
  292. rcu_read_lock();
  293. task = task_rcu_dereference(ptask);
  294. if (task)
  295. get_task_struct(task);
  296. rcu_read_unlock();
  297. return task;
  298. }
  299. /*
  300. * Determine if a process group is "orphaned", according to the POSIX
  301. * definition in 2.2.2.52. Orphaned process groups are not to be affected
  302. * by terminal-generated stop signals. Newly orphaned process groups are
  303. * to receive a SIGHUP and a SIGCONT.
  304. *
  305. * "I ask you, have you ever known what it is to be an orphan?"
  306. */
  307. static int will_become_orphaned_pgrp(struct pid *pgrp,
  308. struct task_struct *ignored_task)
  309. {
  310. struct task_struct *p;
  311. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  312. if ((p == ignored_task) ||
  313. (p->exit_state && thread_group_empty(p)) ||
  314. is_global_init(p->real_parent))
  315. continue;
  316. if (task_pgrp(p->real_parent) != pgrp &&
  317. task_session(p->real_parent) == task_session(p))
  318. return 0;
  319. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  320. return 1;
  321. }
  322. int is_current_pgrp_orphaned(void)
  323. {
  324. int retval;
  325. read_lock(&tasklist_lock);
  326. retval = will_become_orphaned_pgrp(task_pgrp(current), NULL);
  327. read_unlock(&tasklist_lock);
  328. return retval;
  329. }
  330. static bool has_stopped_jobs(struct pid *pgrp)
  331. {
  332. struct task_struct *p;
  333. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  334. if (p->signal->flags & SIGNAL_STOP_STOPPED)
  335. return true;
  336. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  337. return false;
  338. }
  339. /*
  340. * Check to see if any process groups have become orphaned as
  341. * a result of our exiting, and if they have any stopped jobs,
  342. * send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
  343. */
  344. static void
  345. kill_orphaned_pgrp(struct task_struct *tsk, struct task_struct *parent)
  346. {
  347. struct pid *pgrp = task_pgrp(tsk);
  348. struct task_struct *ignored_task = tsk;
  349. if (!parent)
  350. /* exit: our father is in a different pgrp than
  351. * we are and we were the only connection outside.
  352. */
  353. parent = tsk->real_parent;
  354. else
  355. /* reparent: our child is in a different pgrp than
  356. * we are, and it was the only connection outside.
  357. */
  358. ignored_task = NULL;
  359. if (task_pgrp(parent) != pgrp &&
  360. task_session(parent) == task_session(tsk) &&
  361. will_become_orphaned_pgrp(pgrp, ignored_task) &&
  362. has_stopped_jobs(pgrp)) {
  363. __kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp);
  364. __kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp);
  365. }
  366. }
  367. #ifdef CONFIG_MEMCG
  368. /*
  369. * A task is exiting. If it owned this mm, find a new owner for the mm.
  370. */
  371. void mm_update_next_owner(struct mm_struct *mm)
  372. {
  373. struct task_struct *c, *g, *p = current;
  374. retry:
  375. /*
  376. * If the exiting or execing task is not the owner, it's
  377. * someone else's problem.
  378. */
  379. if (mm->owner != p)
  380. return;
  381. /*
  382. * The current owner is exiting/execing and there are no other
  383. * candidates. Do not leave the mm pointing to a possibly
  384. * freed task structure.
  385. */
  386. if (atomic_read(&mm->mm_users) <= 1) {
  387. mm->owner = NULL;
  388. return;
  389. }
  390. read_lock(&tasklist_lock);
  391. /*
  392. * Search in the children
  393. */
  394. list_for_each_entry(c, &p->children, sibling) {
  395. if (c->mm == mm)
  396. goto assign_new_owner;
  397. }
  398. /*
  399. * Search in the siblings
  400. */
  401. list_for_each_entry(c, &p->real_parent->children, sibling) {
  402. if (c->mm == mm)
  403. goto assign_new_owner;
  404. }
  405. /*
  406. * Search through everything else, we should not get here often.
  407. */
  408. for_each_process(g) {
  409. if (g->flags & PF_KTHREAD)
  410. continue;
  411. for_each_thread(g, c) {
  412. if (c->mm == mm)
  413. goto assign_new_owner;
  414. if (c->mm)
  415. break;
  416. }
  417. }
  418. read_unlock(&tasklist_lock);
  419. /*
  420. * We found no owner yet mm_users > 1: this implies that we are
  421. * most likely racing with swapoff (try_to_unuse()) or /proc or
  422. * ptrace or page migration (get_task_mm()). Mark owner as NULL.
  423. */
  424. mm->owner = NULL;
  425. return;
  426. assign_new_owner:
  427. BUG_ON(c == p);
  428. get_task_struct(c);
  429. /*
  430. * The task_lock protects c->mm from changing.
  431. * We always want mm->owner->mm == mm
  432. */
  433. task_lock(c);
  434. /*
  435. * Delay read_unlock() till we have the task_lock()
  436. * to ensure that c does not slip away underneath us
  437. */
  438. read_unlock(&tasklist_lock);
  439. if (c->mm != mm) {
  440. task_unlock(c);
  441. put_task_struct(c);
  442. goto retry;
  443. }
  444. mm->owner = c;
  445. task_unlock(c);
  446. put_task_struct(c);
  447. }
  448. #endif /* CONFIG_MEMCG */
  449. /*
  450. * Turn us into a lazy TLB process if we
  451. * aren't already..
  452. */
  453. static void exit_mm(void)
  454. {
  455. struct mm_struct *mm = current->mm;
  456. struct core_state *core_state;
  457. mm_release(current, mm);
  458. if (!mm)
  459. return;
  460. sync_mm_rss(mm);
  461. /*
  462. * Serialize with any possible pending coredump.
  463. * We must hold mmap_sem around checking core_state
  464. * and clearing tsk->mm. The core-inducing thread
  465. * will increment ->nr_threads for each thread in the
  466. * group with ->mm != NULL.
  467. */
  468. down_read(&mm->mmap_sem);
  469. core_state = mm->core_state;
  470. if (core_state) {
  471. struct core_thread self;
  472. up_read(&mm->mmap_sem);
  473. self.task = current;
  474. self.next = xchg(&core_state->dumper.next, &self);
  475. /*
  476. * Implies mb(), the result of xchg() must be visible
  477. * to core_state->dumper.
  478. */
  479. if (atomic_dec_and_test(&core_state->nr_threads))
  480. complete(&core_state->startup);
  481. for (;;) {
  482. set_current_state(TASK_UNINTERRUPTIBLE);
  483. if (!self.task) /* see coredump_finish() */
  484. break;
  485. freezable_schedule();
  486. }
  487. __set_current_state(TASK_RUNNING);
  488. down_read(&mm->mmap_sem);
  489. }
  490. mmgrab(mm);
  491. BUG_ON(mm != current->active_mm);
  492. /* more a memory barrier than a real lock */
  493. task_lock(current);
  494. current->mm = NULL;
  495. up_read(&mm->mmap_sem);
  496. enter_lazy_tlb(mm, current);
  497. task_unlock(current);
  498. mm_update_next_owner(mm);
  499. mmput(mm);
  500. if (test_thread_flag(TIF_MEMDIE))
  501. exit_oom_victim();
  502. }
  503. static struct task_struct *find_alive_thread(struct task_struct *p)
  504. {
  505. struct task_struct *t;
  506. for_each_thread(p, t) {
  507. if (!(t->flags & PF_EXITING))
  508. return t;
  509. }
  510. return NULL;
  511. }
  512. static struct task_struct *find_child_reaper(struct task_struct *father)
  513. __releases(&tasklist_lock)
  514. __acquires(&tasklist_lock)
  515. {
  516. struct pid_namespace *pid_ns = task_active_pid_ns(father);
  517. struct task_struct *reaper = pid_ns->child_reaper;
  518. if (likely(reaper != father))
  519. return reaper;
  520. reaper = find_alive_thread(father);
  521. if (reaper) {
  522. pid_ns->child_reaper = reaper;
  523. return reaper;
  524. }
  525. write_unlock_irq(&tasklist_lock);
  526. if (unlikely(pid_ns == &init_pid_ns)) {
  527. panic("Attempted to kill init! exitcode=0x%08x\n",
  528. father->signal->group_exit_code ?: father->exit_code);
  529. }
  530. zap_pid_ns_processes(pid_ns);
  531. write_lock_irq(&tasklist_lock);
  532. return father;
  533. }
  534. /*
  535. * When we die, we re-parent all our children, and try to:
  536. * 1. give them to another thread in our thread group, if such a member exists
  537. * 2. give it to the first ancestor process which prctl'd itself as a
  538. * child_subreaper for its children (like a service manager)
  539. * 3. give it to the init process (PID 1) in our pid namespace
  540. */
  541. static struct task_struct *find_new_reaper(struct task_struct *father,
  542. struct task_struct *child_reaper)
  543. {
  544. struct task_struct *thread, *reaper;
  545. thread = find_alive_thread(father);
  546. if (thread)
  547. return thread;
  548. if (father->signal->has_child_subreaper) {
  549. unsigned int ns_level = task_pid(father)->level;
  550. /*
  551. * Find the first ->is_child_subreaper ancestor in our pid_ns.
  552. * We can't check reaper != child_reaper to ensure we do not
  553. * cross the namespaces, the exiting parent could be injected
  554. * by setns() + fork().
  555. * We check pid->level, this is slightly more efficient than
  556. * task_active_pid_ns(reaper) != task_active_pid_ns(father).
  557. */
  558. for (reaper = father->real_parent;
  559. task_pid(reaper)->level == ns_level;
  560. reaper = reaper->real_parent) {
  561. if (reaper == &init_task)
  562. break;
  563. if (!reaper->signal->is_child_subreaper)
  564. continue;
  565. thread = find_alive_thread(reaper);
  566. if (thread)
  567. return thread;
  568. }
  569. }
  570. return child_reaper;
  571. }
  572. /*
  573. * Any that need to be release_task'd are put on the @dead list.
  574. */
  575. static void reparent_leader(struct task_struct *father, struct task_struct *p,
  576. struct list_head *dead)
  577. {
  578. if (unlikely(p->exit_state == EXIT_DEAD))
  579. return;
  580. /* We don't want people slaying init. */
  581. p->exit_signal = SIGCHLD;
  582. /* If it has exited notify the new parent about this child's death. */
  583. if (!p->ptrace &&
  584. p->exit_state == EXIT_ZOMBIE && thread_group_empty(p)) {
  585. if (do_notify_parent(p, p->exit_signal)) {
  586. p->exit_state = EXIT_DEAD;
  587. list_add(&p->ptrace_entry, dead);
  588. }
  589. }
  590. kill_orphaned_pgrp(p, father);
  591. }
  592. /*
  593. * This does two things:
  594. *
  595. * A. Make init inherit all the child processes
  596. * B. Check to see if any process groups have become orphaned
  597. * as a result of our exiting, and if they have any stopped
  598. * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
  599. */
  600. static void forget_original_parent(struct task_struct *father,
  601. struct list_head *dead)
  602. {
  603. struct task_struct *p, *t, *reaper;
  604. if (unlikely(!list_empty(&father->ptraced)))
  605. exit_ptrace(father, dead);
  606. /* Can drop and reacquire tasklist_lock */
  607. reaper = find_child_reaper(father);
  608. if (list_empty(&father->children))
  609. return;
  610. reaper = find_new_reaper(father, reaper);
  611. list_for_each_entry(p, &father->children, sibling) {
  612. for_each_thread(p, t) {
  613. t->real_parent = reaper;
  614. BUG_ON((!t->ptrace) != (t->parent == father));
  615. if (likely(!t->ptrace))
  616. t->parent = t->real_parent;
  617. if (t->pdeath_signal)
  618. group_send_sig_info(t->pdeath_signal,
  619. SEND_SIG_NOINFO, t);
  620. }
  621. /*
  622. * If this is a threaded reparent there is no need to
  623. * notify anyone anything has happened.
  624. */
  625. if (!same_thread_group(reaper, father))
  626. reparent_leader(father, p, dead);
  627. }
  628. list_splice_tail_init(&father->children, &reaper->children);
  629. }
  630. /*
  631. * Send signals to all our closest relatives so that they know
  632. * to properly mourn us..
  633. */
  634. static void exit_notify(struct task_struct *tsk, int group_dead)
  635. {
  636. bool autoreap;
  637. struct task_struct *p, *n;
  638. LIST_HEAD(dead);
  639. write_lock_irq(&tasklist_lock);
  640. forget_original_parent(tsk, &dead);
  641. if (group_dead)
  642. kill_orphaned_pgrp(tsk->group_leader, NULL);
  643. if (unlikely(tsk->ptrace)) {
  644. int sig = thread_group_leader(tsk) &&
  645. thread_group_empty(tsk) &&
  646. !ptrace_reparented(tsk) ?
  647. tsk->exit_signal : SIGCHLD;
  648. autoreap = do_notify_parent(tsk, sig);
  649. } else if (thread_group_leader(tsk)) {
  650. autoreap = thread_group_empty(tsk) &&
  651. do_notify_parent(tsk, tsk->exit_signal);
  652. } else {
  653. autoreap = true;
  654. }
  655. tsk->exit_state = autoreap ? EXIT_DEAD : EXIT_ZOMBIE;
  656. if (tsk->exit_state == EXIT_DEAD)
  657. list_add(&tsk->ptrace_entry, &dead);
  658. /* mt-exec, de_thread() is waiting for group leader */
  659. if (unlikely(tsk->signal->notify_count < 0))
  660. wake_up_process(tsk->signal->group_exit_task);
  661. write_unlock_irq(&tasklist_lock);
  662. list_for_each_entry_safe(p, n, &dead, ptrace_entry) {
  663. list_del_init(&p->ptrace_entry);
  664. release_task(p);
  665. }
  666. }
  667. #ifdef CONFIG_DEBUG_STACK_USAGE
  668. static void check_stack_usage(void)
  669. {
  670. static DEFINE_SPINLOCK(low_water_lock);
  671. static int lowest_to_date = THREAD_SIZE;
  672. unsigned long free;
  673. free = stack_not_used(current);
  674. if (free >= lowest_to_date)
  675. return;
  676. spin_lock(&low_water_lock);
  677. if (free < lowest_to_date) {
  678. pr_info("%s (%d) used greatest stack depth: %lu bytes left\n",
  679. current->comm, task_pid_nr(current), free);
  680. lowest_to_date = free;
  681. }
  682. spin_unlock(&low_water_lock);
  683. }
  684. #else
  685. static inline void check_stack_usage(void) {}
  686. #endif
  687. void __noreturn do_exit(long code)
  688. {
  689. struct task_struct *tsk = current;
  690. int group_dead;
  691. TASKS_RCU(int tasks_rcu_i);
  692. profile_task_exit(tsk);
  693. kcov_task_exit(tsk);
  694. WARN_ON(blk_needs_flush_plug(tsk));
  695. if (unlikely(in_interrupt()))
  696. panic("Aiee, killing interrupt handler!");
  697. if (unlikely(!tsk->pid))
  698. panic("Attempted to kill the idle task!");
  699. /*
  700. * If do_exit is called because this processes oopsed, it's possible
  701. * that get_fs() was left as KERNEL_DS, so reset it to USER_DS before
  702. * continuing. Amongst other possible reasons, this is to prevent
  703. * mm_release()->clear_child_tid() from writing to a user-controlled
  704. * kernel address.
  705. */
  706. set_fs(USER_DS);
  707. ptrace_event(PTRACE_EVENT_EXIT, code);
  708. validate_creds_for_do_exit(tsk);
  709. /*
  710. * We're taking recursive faults here in do_exit. Safest is to just
  711. * leave this task alone and wait for reboot.
  712. */
  713. if (unlikely(tsk->flags & PF_EXITING)) {
  714. pr_alert("Fixing recursive fault but reboot is needed!\n");
  715. /*
  716. * We can do this unlocked here. The futex code uses
  717. * this flag just to verify whether the pi state
  718. * cleanup has been done or not. In the worst case it
  719. * loops once more. We pretend that the cleanup was
  720. * done as there is no way to return. Either the
  721. * OWNER_DIED bit is set by now or we push the blocked
  722. * task into the wait for ever nirwana as well.
  723. */
  724. tsk->flags |= PF_EXITPIDONE;
  725. set_current_state(TASK_UNINTERRUPTIBLE);
  726. schedule();
  727. }
  728. exit_signals(tsk); /* sets PF_EXITING */
  729. /*
  730. * Ensure that all new tsk->pi_lock acquisitions must observe
  731. * PF_EXITING. Serializes against futex.c:attach_to_pi_owner().
  732. */
  733. smp_mb();
  734. /*
  735. * Ensure that we must observe the pi_state in exit_mm() ->
  736. * mm_release() -> exit_pi_state_list().
  737. */
  738. raw_spin_unlock_wait(&tsk->pi_lock);
  739. if (unlikely(in_atomic())) {
  740. pr_info("note: %s[%d] exited with preempt_count %d\n",
  741. current->comm, task_pid_nr(current),
  742. preempt_count());
  743. preempt_count_set(PREEMPT_ENABLED);
  744. }
  745. /* sync mm's RSS info before statistics gathering */
  746. if (tsk->mm)
  747. sync_mm_rss(tsk->mm);
  748. acct_update_integrals(tsk);
  749. group_dead = atomic_dec_and_test(&tsk->signal->live);
  750. if (group_dead) {
  751. #ifdef CONFIG_POSIX_TIMERS
  752. hrtimer_cancel(&tsk->signal->real_timer);
  753. exit_itimers(tsk->signal);
  754. #endif
  755. if (tsk->mm)
  756. setmax_mm_hiwater_rss(&tsk->signal->maxrss, tsk->mm);
  757. }
  758. acct_collect(code, group_dead);
  759. if (group_dead)
  760. tty_audit_exit();
  761. audit_free(tsk);
  762. tsk->exit_code = code;
  763. taskstats_exit(tsk, group_dead);
  764. exit_mm();
  765. if (group_dead)
  766. acct_process();
  767. trace_sched_process_exit(tsk);
  768. exit_sem(tsk);
  769. exit_shm(tsk);
  770. exit_files(tsk);
  771. exit_fs(tsk);
  772. if (group_dead)
  773. disassociate_ctty(1);
  774. exit_task_namespaces(tsk);
  775. exit_task_work(tsk);
  776. exit_thread(tsk);
  777. /*
  778. * Flush inherited counters to the parent - before the parent
  779. * gets woken up by child-exit notifications.
  780. *
  781. * because of cgroup mode, must be called before cgroup_exit()
  782. */
  783. perf_event_exit_task(tsk);
  784. sched_autogroup_exit_task(tsk);
  785. cgroup_exit(tsk);
  786. /*
  787. * FIXME: do that only when needed, using sched_exit tracepoint
  788. */
  789. flush_ptrace_hw_breakpoint(tsk);
  790. TASKS_RCU(preempt_disable());
  791. TASKS_RCU(tasks_rcu_i = __srcu_read_lock(&tasks_rcu_exit_srcu));
  792. TASKS_RCU(preempt_enable());
  793. exit_notify(tsk, group_dead);
  794. proc_exit_connector(tsk);
  795. mpol_put_task_policy(tsk);
  796. #ifdef CONFIG_FUTEX
  797. if (unlikely(current->pi_state_cache))
  798. kfree(current->pi_state_cache);
  799. #endif
  800. /*
  801. * Make sure we are holding no locks:
  802. */
  803. debug_check_no_locks_held();
  804. /*
  805. * We can do this unlocked here. The futex code uses this flag
  806. * just to verify whether the pi state cleanup has been done
  807. * or not. In the worst case it loops once more.
  808. */
  809. tsk->flags |= PF_EXITPIDONE;
  810. if (tsk->io_context)
  811. exit_io_context(tsk);
  812. if (tsk->splice_pipe)
  813. free_pipe_info(tsk->splice_pipe);
  814. if (tsk->task_frag.page)
  815. put_page(tsk->task_frag.page);
  816. validate_creds_for_do_exit(tsk);
  817. check_stack_usage();
  818. preempt_disable();
  819. if (tsk->nr_dirtied)
  820. __this_cpu_add(dirty_throttle_leaks, tsk->nr_dirtied);
  821. exit_rcu();
  822. TASKS_RCU(__srcu_read_unlock(&tasks_rcu_exit_srcu, tasks_rcu_i));
  823. do_task_dead();
  824. }
  825. EXPORT_SYMBOL_GPL(do_exit);
  826. void complete_and_exit(struct completion *comp, long code)
  827. {
  828. if (comp)
  829. complete(comp);
  830. do_exit(code);
  831. }
  832. EXPORT_SYMBOL(complete_and_exit);
  833. SYSCALL_DEFINE1(exit, int, error_code)
  834. {
  835. do_exit((error_code&0xff)<<8);
  836. }
  837. /*
  838. * Take down every thread in the group. This is called by fatal signals
  839. * as well as by sys_exit_group (below).
  840. */
  841. void
  842. do_group_exit(int exit_code)
  843. {
  844. struct signal_struct *sig = current->signal;
  845. BUG_ON(exit_code & 0x80); /* core dumps don't get here */
  846. if (signal_group_exit(sig))
  847. exit_code = sig->group_exit_code;
  848. else if (!thread_group_empty(current)) {
  849. struct sighand_struct *const sighand = current->sighand;
  850. spin_lock_irq(&sighand->siglock);
  851. if (signal_group_exit(sig))
  852. /* Another thread got here before we took the lock. */
  853. exit_code = sig->group_exit_code;
  854. else {
  855. sig->group_exit_code = exit_code;
  856. sig->flags = SIGNAL_GROUP_EXIT;
  857. zap_other_threads(current);
  858. }
  859. spin_unlock_irq(&sighand->siglock);
  860. }
  861. do_exit(exit_code);
  862. /* NOTREACHED */
  863. }
  864. /*
  865. * this kills every thread in the thread group. Note that any externally
  866. * wait4()-ing process will get the correct exit code - even if this
  867. * thread is not the thread group leader.
  868. */
  869. SYSCALL_DEFINE1(exit_group, int, error_code)
  870. {
  871. do_group_exit((error_code & 0xff) << 8);
  872. /* NOTREACHED */
  873. return 0;
  874. }
  875. struct waitid_info {
  876. pid_t pid;
  877. uid_t uid;
  878. int status;
  879. int cause;
  880. };
  881. struct wait_opts {
  882. enum pid_type wo_type;
  883. int wo_flags;
  884. struct pid *wo_pid;
  885. struct waitid_info *wo_info;
  886. int wo_stat;
  887. struct rusage *wo_rusage;
  888. wait_queue_t child_wait;
  889. int notask_error;
  890. };
  891. static inline
  892. struct pid *task_pid_type(struct task_struct *task, enum pid_type type)
  893. {
  894. if (type != PIDTYPE_PID)
  895. task = task->group_leader;
  896. return task->pids[type].pid;
  897. }
  898. static int eligible_pid(struct wait_opts *wo, struct task_struct *p)
  899. {
  900. return wo->wo_type == PIDTYPE_MAX ||
  901. task_pid_type(p, wo->wo_type) == wo->wo_pid;
  902. }
  903. static int
  904. eligible_child(struct wait_opts *wo, bool ptrace, struct task_struct *p)
  905. {
  906. if (!eligible_pid(wo, p))
  907. return 0;
  908. /*
  909. * Wait for all children (clone and not) if __WALL is set or
  910. * if it is traced by us.
  911. */
  912. if (ptrace || (wo->wo_flags & __WALL))
  913. return 1;
  914. /*
  915. * Otherwise, wait for clone children *only* if __WCLONE is set;
  916. * otherwise, wait for non-clone children *only*.
  917. *
  918. * Note: a "clone" child here is one that reports to its parent
  919. * using a signal other than SIGCHLD, or a non-leader thread which
  920. * we can only see if it is traced by us.
  921. */
  922. if ((p->exit_signal != SIGCHLD) ^ !!(wo->wo_flags & __WCLONE))
  923. return 0;
  924. return 1;
  925. }
  926. static int wait_noreap_copyout(struct wait_opts *wo, struct task_struct *p,
  927. pid_t pid, uid_t uid, int why, int status)
  928. {
  929. struct waitid_info *infop;
  930. if (wo->wo_rusage)
  931. getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
  932. put_task_struct(p);
  933. infop = wo->wo_info;
  934. if (infop) {
  935. infop->cause = why;
  936. infop->pid = pid;
  937. infop->uid = uid;
  938. infop->status = status;
  939. }
  940. return pid;
  941. }
  942. /*
  943. * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
  944. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  945. * the lock and this task is uninteresting. If we return nonzero, we have
  946. * released the lock and the system call should return.
  947. */
  948. static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
  949. {
  950. int state, status;
  951. pid_t pid = task_pid_vnr(p);
  952. uid_t uid = from_kuid_munged(current_user_ns(), task_uid(p));
  953. struct waitid_info *infop;
  954. if (!likely(wo->wo_flags & WEXITED))
  955. return 0;
  956. if (unlikely(wo->wo_flags & WNOWAIT)) {
  957. int exit_code = p->exit_code;
  958. int why;
  959. get_task_struct(p);
  960. read_unlock(&tasklist_lock);
  961. sched_annotate_sleep();
  962. if ((exit_code & 0x7f) == 0) {
  963. why = CLD_EXITED;
  964. status = exit_code >> 8;
  965. } else {
  966. why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
  967. status = exit_code & 0x7f;
  968. }
  969. return wait_noreap_copyout(wo, p, pid, uid, why, status);
  970. }
  971. /*
  972. * Move the task's state to DEAD/TRACE, only one thread can do this.
  973. */
  974. state = (ptrace_reparented(p) && thread_group_leader(p)) ?
  975. EXIT_TRACE : EXIT_DEAD;
  976. if (cmpxchg(&p->exit_state, EXIT_ZOMBIE, state) != EXIT_ZOMBIE)
  977. return 0;
  978. /*
  979. * We own this thread, nobody else can reap it.
  980. */
  981. read_unlock(&tasklist_lock);
  982. sched_annotate_sleep();
  983. /*
  984. * Check thread_group_leader() to exclude the traced sub-threads.
  985. */
  986. if (state == EXIT_DEAD && thread_group_leader(p)) {
  987. struct signal_struct *sig = p->signal;
  988. struct signal_struct *psig = current->signal;
  989. unsigned long maxrss;
  990. u64 tgutime, tgstime;
  991. /*
  992. * The resource counters for the group leader are in its
  993. * own task_struct. Those for dead threads in the group
  994. * are in its signal_struct, as are those for the child
  995. * processes it has previously reaped. All these
  996. * accumulate in the parent's signal_struct c* fields.
  997. *
  998. * We don't bother to take a lock here to protect these
  999. * p->signal fields because the whole thread group is dead
  1000. * and nobody can change them.
  1001. *
  1002. * psig->stats_lock also protects us from our sub-theads
  1003. * which can reap other children at the same time. Until
  1004. * we change k_getrusage()-like users to rely on this lock
  1005. * we have to take ->siglock as well.
  1006. *
  1007. * We use thread_group_cputime_adjusted() to get times for
  1008. * the thread group, which consolidates times for all threads
  1009. * in the group including the group leader.
  1010. */
  1011. thread_group_cputime_adjusted(p, &tgutime, &tgstime);
  1012. spin_lock_irq(&current->sighand->siglock);
  1013. write_seqlock(&psig->stats_lock);
  1014. psig->cutime += tgutime + sig->cutime;
  1015. psig->cstime += tgstime + sig->cstime;
  1016. psig->cgtime += task_gtime(p) + sig->gtime + sig->cgtime;
  1017. psig->cmin_flt +=
  1018. p->min_flt + sig->min_flt + sig->cmin_flt;
  1019. psig->cmaj_flt +=
  1020. p->maj_flt + sig->maj_flt + sig->cmaj_flt;
  1021. psig->cnvcsw +=
  1022. p->nvcsw + sig->nvcsw + sig->cnvcsw;
  1023. psig->cnivcsw +=
  1024. p->nivcsw + sig->nivcsw + sig->cnivcsw;
  1025. psig->cinblock +=
  1026. task_io_get_inblock(p) +
  1027. sig->inblock + sig->cinblock;
  1028. psig->coublock +=
  1029. task_io_get_oublock(p) +
  1030. sig->oublock + sig->coublock;
  1031. maxrss = max(sig->maxrss, sig->cmaxrss);
  1032. if (psig->cmaxrss < maxrss)
  1033. psig->cmaxrss = maxrss;
  1034. task_io_accounting_add(&psig->ioac, &p->ioac);
  1035. task_io_accounting_add(&psig->ioac, &sig->ioac);
  1036. write_sequnlock(&psig->stats_lock);
  1037. spin_unlock_irq(&current->sighand->siglock);
  1038. }
  1039. if (wo->wo_rusage)
  1040. getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
  1041. status = (p->signal->flags & SIGNAL_GROUP_EXIT)
  1042. ? p->signal->group_exit_code : p->exit_code;
  1043. wo->wo_stat = status;
  1044. infop = wo->wo_info;
  1045. if (infop) {
  1046. if ((status & 0x7f) == 0) {
  1047. infop->cause = CLD_EXITED;
  1048. infop->status = status >> 8;
  1049. } else {
  1050. infop->cause = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
  1051. infop->status = status & 0x7f;
  1052. }
  1053. infop->pid = pid;
  1054. infop->uid = uid;
  1055. }
  1056. if (state == EXIT_TRACE) {
  1057. write_lock_irq(&tasklist_lock);
  1058. /* We dropped tasklist, ptracer could die and untrace */
  1059. ptrace_unlink(p);
  1060. /* If parent wants a zombie, don't release it now */
  1061. state = EXIT_ZOMBIE;
  1062. if (do_notify_parent(p, p->exit_signal))
  1063. state = EXIT_DEAD;
  1064. p->exit_state = state;
  1065. write_unlock_irq(&tasklist_lock);
  1066. }
  1067. if (state == EXIT_DEAD)
  1068. release_task(p);
  1069. return pid;
  1070. }
  1071. static int *task_stopped_code(struct task_struct *p, bool ptrace)
  1072. {
  1073. if (ptrace) {
  1074. if (task_is_traced(p) && !(p->jobctl & JOBCTL_LISTENING))
  1075. return &p->exit_code;
  1076. } else {
  1077. if (p->signal->flags & SIGNAL_STOP_STOPPED)
  1078. return &p->signal->group_exit_code;
  1079. }
  1080. return NULL;
  1081. }
  1082. /**
  1083. * wait_task_stopped - Wait for %TASK_STOPPED or %TASK_TRACED
  1084. * @wo: wait options
  1085. * @ptrace: is the wait for ptrace
  1086. * @p: task to wait for
  1087. *
  1088. * Handle sys_wait4() work for %p in state %TASK_STOPPED or %TASK_TRACED.
  1089. *
  1090. * CONTEXT:
  1091. * read_lock(&tasklist_lock), which is released if return value is
  1092. * non-zero. Also, grabs and releases @p->sighand->siglock.
  1093. *
  1094. * RETURNS:
  1095. * 0 if wait condition didn't exist and search for other wait conditions
  1096. * should continue. Non-zero return, -errno on failure and @p's pid on
  1097. * success, implies that tasklist_lock is released and wait condition
  1098. * search should terminate.
  1099. */
  1100. static int wait_task_stopped(struct wait_opts *wo,
  1101. int ptrace, struct task_struct *p)
  1102. {
  1103. struct waitid_info *infop;
  1104. int exit_code, *p_code, why;
  1105. uid_t uid = 0; /* unneeded, required by compiler */
  1106. pid_t pid;
  1107. /*
  1108. * Traditionally we see ptrace'd stopped tasks regardless of options.
  1109. */
  1110. if (!ptrace && !(wo->wo_flags & WUNTRACED))
  1111. return 0;
  1112. if (!task_stopped_code(p, ptrace))
  1113. return 0;
  1114. exit_code = 0;
  1115. spin_lock_irq(&p->sighand->siglock);
  1116. p_code = task_stopped_code(p, ptrace);
  1117. if (unlikely(!p_code))
  1118. goto unlock_sig;
  1119. exit_code = *p_code;
  1120. if (!exit_code)
  1121. goto unlock_sig;
  1122. if (!unlikely(wo->wo_flags & WNOWAIT))
  1123. *p_code = 0;
  1124. uid = from_kuid_munged(current_user_ns(), task_uid(p));
  1125. unlock_sig:
  1126. spin_unlock_irq(&p->sighand->siglock);
  1127. if (!exit_code)
  1128. return 0;
  1129. /*
  1130. * Now we are pretty sure this task is interesting.
  1131. * Make sure it doesn't get reaped out from under us while we
  1132. * give up the lock and then examine it below. We don't want to
  1133. * keep holding onto the tasklist_lock while we call getrusage and
  1134. * possibly take page faults for user memory.
  1135. */
  1136. get_task_struct(p);
  1137. pid = task_pid_vnr(p);
  1138. why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
  1139. read_unlock(&tasklist_lock);
  1140. sched_annotate_sleep();
  1141. if (unlikely(wo->wo_flags & WNOWAIT))
  1142. return wait_noreap_copyout(wo, p, pid, uid, why, exit_code);
  1143. if (wo->wo_rusage)
  1144. getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
  1145. wo->wo_stat = (exit_code << 8) | 0x7f;
  1146. infop = wo->wo_info;
  1147. if (infop) {
  1148. infop->cause = why;
  1149. infop->status = exit_code;
  1150. infop->pid = pid;
  1151. infop->uid = uid;
  1152. }
  1153. put_task_struct(p);
  1154. BUG_ON(!pid);
  1155. return pid;
  1156. }
  1157. /*
  1158. * Handle do_wait work for one task in a live, non-stopped state.
  1159. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  1160. * the lock and this task is uninteresting. If we return nonzero, we have
  1161. * released the lock and the system call should return.
  1162. */
  1163. static int wait_task_continued(struct wait_opts *wo, struct task_struct *p)
  1164. {
  1165. int retval;
  1166. pid_t pid;
  1167. uid_t uid;
  1168. if (!unlikely(wo->wo_flags & WCONTINUED))
  1169. return 0;
  1170. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
  1171. return 0;
  1172. spin_lock_irq(&p->sighand->siglock);
  1173. /* Re-check with the lock held. */
  1174. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
  1175. spin_unlock_irq(&p->sighand->siglock);
  1176. return 0;
  1177. }
  1178. if (!unlikely(wo->wo_flags & WNOWAIT))
  1179. p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
  1180. uid = from_kuid_munged(current_user_ns(), task_uid(p));
  1181. spin_unlock_irq(&p->sighand->siglock);
  1182. pid = task_pid_vnr(p);
  1183. get_task_struct(p);
  1184. read_unlock(&tasklist_lock);
  1185. sched_annotate_sleep();
  1186. if (!wo->wo_info) {
  1187. if (wo->wo_rusage)
  1188. getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
  1189. put_task_struct(p);
  1190. wo->wo_stat = 0xffff;
  1191. retval = pid;
  1192. } else {
  1193. retval = wait_noreap_copyout(wo, p, pid, uid,
  1194. CLD_CONTINUED, SIGCONT);
  1195. BUG_ON(retval == 0);
  1196. }
  1197. return retval;
  1198. }
  1199. /*
  1200. * Consider @p for a wait by @parent.
  1201. *
  1202. * -ECHILD should be in ->notask_error before the first call.
  1203. * Returns nonzero for a final return, when we have unlocked tasklist_lock.
  1204. * Returns zero if the search for a child should continue;
  1205. * then ->notask_error is 0 if @p is an eligible child,
  1206. * or still -ECHILD.
  1207. */
  1208. static int wait_consider_task(struct wait_opts *wo, int ptrace,
  1209. struct task_struct *p)
  1210. {
  1211. /*
  1212. * We can race with wait_task_zombie() from another thread.
  1213. * Ensure that EXIT_ZOMBIE -> EXIT_DEAD/EXIT_TRACE transition
  1214. * can't confuse the checks below.
  1215. */
  1216. int exit_state = ACCESS_ONCE(p->exit_state);
  1217. int ret;
  1218. if (unlikely(exit_state == EXIT_DEAD))
  1219. return 0;
  1220. ret = eligible_child(wo, ptrace, p);
  1221. if (!ret)
  1222. return ret;
  1223. if (unlikely(exit_state == EXIT_TRACE)) {
  1224. /*
  1225. * ptrace == 0 means we are the natural parent. In this case
  1226. * we should clear notask_error, debugger will notify us.
  1227. */
  1228. if (likely(!ptrace))
  1229. wo->notask_error = 0;
  1230. return 0;
  1231. }
  1232. if (likely(!ptrace) && unlikely(p->ptrace)) {
  1233. /*
  1234. * If it is traced by its real parent's group, just pretend
  1235. * the caller is ptrace_do_wait() and reap this child if it
  1236. * is zombie.
  1237. *
  1238. * This also hides group stop state from real parent; otherwise
  1239. * a single stop can be reported twice as group and ptrace stop.
  1240. * If a ptracer wants to distinguish these two events for its
  1241. * own children it should create a separate process which takes
  1242. * the role of real parent.
  1243. */
  1244. if (!ptrace_reparented(p))
  1245. ptrace = 1;
  1246. }
  1247. /* slay zombie? */
  1248. if (exit_state == EXIT_ZOMBIE) {
  1249. /* we don't reap group leaders with subthreads */
  1250. if (!delay_group_leader(p)) {
  1251. /*
  1252. * A zombie ptracee is only visible to its ptracer.
  1253. * Notification and reaping will be cascaded to the
  1254. * real parent when the ptracer detaches.
  1255. */
  1256. if (unlikely(ptrace) || likely(!p->ptrace))
  1257. return wait_task_zombie(wo, p);
  1258. }
  1259. /*
  1260. * Allow access to stopped/continued state via zombie by
  1261. * falling through. Clearing of notask_error is complex.
  1262. *
  1263. * When !@ptrace:
  1264. *
  1265. * If WEXITED is set, notask_error should naturally be
  1266. * cleared. If not, subset of WSTOPPED|WCONTINUED is set,
  1267. * so, if there are live subthreads, there are events to
  1268. * wait for. If all subthreads are dead, it's still safe
  1269. * to clear - this function will be called again in finite
  1270. * amount time once all the subthreads are released and
  1271. * will then return without clearing.
  1272. *
  1273. * When @ptrace:
  1274. *
  1275. * Stopped state is per-task and thus can't change once the
  1276. * target task dies. Only continued and exited can happen.
  1277. * Clear notask_error if WCONTINUED | WEXITED.
  1278. */
  1279. if (likely(!ptrace) || (wo->wo_flags & (WCONTINUED | WEXITED)))
  1280. wo->notask_error = 0;
  1281. } else {
  1282. /*
  1283. * @p is alive and it's gonna stop, continue or exit, so
  1284. * there always is something to wait for.
  1285. */
  1286. wo->notask_error = 0;
  1287. }
  1288. /*
  1289. * Wait for stopped. Depending on @ptrace, different stopped state
  1290. * is used and the two don't interact with each other.
  1291. */
  1292. ret = wait_task_stopped(wo, ptrace, p);
  1293. if (ret)
  1294. return ret;
  1295. /*
  1296. * Wait for continued. There's only one continued state and the
  1297. * ptracer can consume it which can confuse the real parent. Don't
  1298. * use WCONTINUED from ptracer. You don't need or want it.
  1299. */
  1300. return wait_task_continued(wo, p);
  1301. }
  1302. /*
  1303. * Do the work of do_wait() for one thread in the group, @tsk.
  1304. *
  1305. * -ECHILD should be in ->notask_error before the first call.
  1306. * Returns nonzero for a final return, when we have unlocked tasklist_lock.
  1307. * Returns zero if the search for a child should continue; then
  1308. * ->notask_error is 0 if there were any eligible children,
  1309. * or still -ECHILD.
  1310. */
  1311. static int do_wait_thread(struct wait_opts *wo, struct task_struct *tsk)
  1312. {
  1313. struct task_struct *p;
  1314. list_for_each_entry(p, &tsk->children, sibling) {
  1315. int ret = wait_consider_task(wo, 0, p);
  1316. if (ret)
  1317. return ret;
  1318. }
  1319. return 0;
  1320. }
  1321. static int ptrace_do_wait(struct wait_opts *wo, struct task_struct *tsk)
  1322. {
  1323. struct task_struct *p;
  1324. list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {
  1325. int ret = wait_consider_task(wo, 1, p);
  1326. if (ret)
  1327. return ret;
  1328. }
  1329. return 0;
  1330. }
  1331. static int child_wait_callback(wait_queue_t *wait, unsigned mode,
  1332. int sync, void *key)
  1333. {
  1334. struct wait_opts *wo = container_of(wait, struct wait_opts,
  1335. child_wait);
  1336. struct task_struct *p = key;
  1337. if (!eligible_pid(wo, p))
  1338. return 0;
  1339. if ((wo->wo_flags & __WNOTHREAD) && wait->private != p->parent)
  1340. return 0;
  1341. return default_wake_function(wait, mode, sync, key);
  1342. }
  1343. void __wake_up_parent(struct task_struct *p, struct task_struct *parent)
  1344. {
  1345. __wake_up_sync_key(&parent->signal->wait_chldexit,
  1346. TASK_INTERRUPTIBLE, 1, p);
  1347. }
  1348. static long do_wait(struct wait_opts *wo)
  1349. {
  1350. struct task_struct *tsk;
  1351. int retval;
  1352. trace_sched_process_wait(wo->wo_pid);
  1353. init_waitqueue_func_entry(&wo->child_wait, child_wait_callback);
  1354. wo->child_wait.private = current;
  1355. add_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
  1356. repeat:
  1357. /*
  1358. * If there is nothing that can match our criteria, just get out.
  1359. * We will clear ->notask_error to zero if we see any child that
  1360. * might later match our criteria, even if we are not able to reap
  1361. * it yet.
  1362. */
  1363. wo->notask_error = -ECHILD;
  1364. if ((wo->wo_type < PIDTYPE_MAX) &&
  1365. (!wo->wo_pid || hlist_empty(&wo->wo_pid->tasks[wo->wo_type])))
  1366. goto notask;
  1367. set_current_state(TASK_INTERRUPTIBLE);
  1368. read_lock(&tasklist_lock);
  1369. tsk = current;
  1370. do {
  1371. retval = do_wait_thread(wo, tsk);
  1372. if (retval)
  1373. goto end;
  1374. retval = ptrace_do_wait(wo, tsk);
  1375. if (retval)
  1376. goto end;
  1377. if (wo->wo_flags & __WNOTHREAD)
  1378. break;
  1379. } while_each_thread(current, tsk);
  1380. read_unlock(&tasklist_lock);
  1381. notask:
  1382. retval = wo->notask_error;
  1383. if (!retval && !(wo->wo_flags & WNOHANG)) {
  1384. retval = -ERESTARTSYS;
  1385. if (!signal_pending(current)) {
  1386. schedule();
  1387. goto repeat;
  1388. }
  1389. }
  1390. end:
  1391. __set_current_state(TASK_RUNNING);
  1392. remove_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
  1393. return retval;
  1394. }
  1395. static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop,
  1396. int options, struct rusage *ru)
  1397. {
  1398. struct wait_opts wo;
  1399. struct pid *pid = NULL;
  1400. enum pid_type type;
  1401. long ret;
  1402. if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED|
  1403. __WNOTHREAD|__WCLONE|__WALL))
  1404. return -EINVAL;
  1405. if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
  1406. return -EINVAL;
  1407. switch (which) {
  1408. case P_ALL:
  1409. type = PIDTYPE_MAX;
  1410. break;
  1411. case P_PID:
  1412. type = PIDTYPE_PID;
  1413. if (upid <= 0)
  1414. return -EINVAL;
  1415. break;
  1416. case P_PGID:
  1417. type = PIDTYPE_PGID;
  1418. if (upid <= 0)
  1419. return -EINVAL;
  1420. break;
  1421. default:
  1422. return -EINVAL;
  1423. }
  1424. if (type < PIDTYPE_MAX)
  1425. pid = find_get_pid(upid);
  1426. wo.wo_type = type;
  1427. wo.wo_pid = pid;
  1428. wo.wo_flags = options;
  1429. wo.wo_info = infop;
  1430. wo.wo_rusage = ru;
  1431. ret = do_wait(&wo);
  1432. if (ret > 0)
  1433. ret = 0;
  1434. put_pid(pid);
  1435. return ret;
  1436. }
  1437. SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
  1438. infop, int, options, struct rusage __user *, ru)
  1439. {
  1440. struct rusage r;
  1441. struct waitid_info info = {.status = 0};
  1442. long err = kernel_waitid(which, upid, &info, options, ru ? &r : NULL);
  1443. if (!err) {
  1444. if (ru && copy_to_user(ru, &r, sizeof(struct rusage)))
  1445. return -EFAULT;
  1446. }
  1447. if (!infop)
  1448. return err;
  1449. if (put_user(err ? 0 : SIGCHLD, &infop->si_signo) ||
  1450. put_user(0, &infop->si_errno) ||
  1451. put_user((short)info.cause, &infop->si_code) ||
  1452. put_user(info.pid, &infop->si_pid) ||
  1453. put_user(info.uid, &infop->si_uid) ||
  1454. put_user(info.status, &infop->si_status))
  1455. err = -EFAULT;
  1456. return err;
  1457. }
  1458. static long kernel_wait4(pid_t upid, int __user *stat_addr,
  1459. int options, struct rusage *ru)
  1460. {
  1461. struct wait_opts wo;
  1462. struct pid *pid = NULL;
  1463. enum pid_type type;
  1464. long ret;
  1465. if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
  1466. __WNOTHREAD|__WCLONE|__WALL))
  1467. return -EINVAL;
  1468. if (upid == -1)
  1469. type = PIDTYPE_MAX;
  1470. else if (upid < 0) {
  1471. type = PIDTYPE_PGID;
  1472. pid = find_get_pid(-upid);
  1473. } else if (upid == 0) {
  1474. type = PIDTYPE_PGID;
  1475. pid = get_task_pid(current, PIDTYPE_PGID);
  1476. } else /* upid > 0 */ {
  1477. type = PIDTYPE_PID;
  1478. pid = find_get_pid(upid);
  1479. }
  1480. wo.wo_type = type;
  1481. wo.wo_pid = pid;
  1482. wo.wo_flags = options | WEXITED;
  1483. wo.wo_info = NULL;
  1484. wo.wo_stat = 0;
  1485. wo.wo_rusage = ru;
  1486. ret = do_wait(&wo);
  1487. put_pid(pid);
  1488. if (ret > 0 && stat_addr && put_user(wo.wo_stat, stat_addr))
  1489. ret = -EFAULT;
  1490. return ret;
  1491. }
  1492. SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
  1493. int, options, struct rusage __user *, ru)
  1494. {
  1495. struct rusage r;
  1496. long err = kernel_wait4(upid, stat_addr, options, ru ? &r : NULL);
  1497. if (err > 0) {
  1498. if (ru && copy_to_user(ru, &r, sizeof(struct rusage)))
  1499. return -EFAULT;
  1500. }
  1501. return err;
  1502. }
  1503. #ifdef __ARCH_WANT_SYS_WAITPID
  1504. /*
  1505. * sys_waitpid() remains for compatibility. waitpid() should be
  1506. * implemented by calling sys_wait4() from libc.a.
  1507. */
  1508. SYSCALL_DEFINE3(waitpid, pid_t, pid, int __user *, stat_addr, int, options)
  1509. {
  1510. return sys_wait4(pid, stat_addr, options, NULL);
  1511. }
  1512. #endif
  1513. #ifdef CONFIG_COMPAT
  1514. COMPAT_SYSCALL_DEFINE4(wait4,
  1515. compat_pid_t, pid,
  1516. compat_uint_t __user *, stat_addr,
  1517. int, options,
  1518. struct compat_rusage __user *, ru)
  1519. {
  1520. struct rusage r;
  1521. long err = kernel_wait4(pid, stat_addr, options, ru ? &r : NULL);
  1522. if (err > 0) {
  1523. if (ru && put_compat_rusage(&r, ru))
  1524. return -EFAULT;
  1525. }
  1526. return err;
  1527. }
  1528. COMPAT_SYSCALL_DEFINE5(waitid,
  1529. int, which, compat_pid_t, pid,
  1530. struct compat_siginfo __user *, infop, int, options,
  1531. struct compat_rusage __user *, uru)
  1532. {
  1533. struct rusage ru;
  1534. struct waitid_info info = {.status = 0};
  1535. long err = kernel_waitid(which, pid, &info, options, uru ? &ru : NULL);
  1536. if (!err && uru) {
  1537. /* kernel_waitid() overwrites everything in ru */
  1538. if (COMPAT_USE_64BIT_TIME)
  1539. err = copy_to_user(uru, &ru, sizeof(ru));
  1540. else
  1541. err = put_compat_rusage(&ru, uru);
  1542. if (err)
  1543. return -EFAULT;
  1544. }
  1545. if (put_user(err ? 0 : SIGCHLD, &infop->si_signo) ||
  1546. put_user(0, &infop->si_errno) ||
  1547. put_user((short)info.cause, &infop->si_code) ||
  1548. put_user(info.pid, &infop->si_pid) ||
  1549. put_user(info.uid, &infop->si_uid) ||
  1550. put_user(info.status, &infop->si_status))
  1551. err = -EFAULT;
  1552. return err;
  1553. }
  1554. #endif