exit.c 44 KB

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