exit.c 41 KB

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