exit.c 43 KB

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