exit.c 43 KB

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