exit.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715
  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. unsigned int ns_level = task_pid(father)->level;
  517. /*
  518. * Find the first ->is_child_subreaper ancestor in our pid_ns.
  519. * We can't check reaper != child_reaper to ensure we do not
  520. * cross the namespaces, the exiting parent could be injected
  521. * by setns() + fork().
  522. * We check pid->level, this is slightly more efficient than
  523. * task_active_pid_ns(reaper) != task_active_pid_ns(father).
  524. */
  525. for (reaper = father->real_parent;
  526. task_pid(reaper)->level == ns_level;
  527. reaper = reaper->real_parent) {
  528. if (reaper == &init_task)
  529. break;
  530. if (!reaper->signal->is_child_subreaper)
  531. continue;
  532. thread = find_alive_thread(reaper);
  533. if (thread)
  534. return thread;
  535. }
  536. }
  537. return child_reaper;
  538. }
  539. /*
  540. * Any that need to be release_task'd are put on the @dead list.
  541. */
  542. static void reparent_leader(struct task_struct *father, struct task_struct *p,
  543. struct list_head *dead)
  544. {
  545. if (unlikely(p->exit_state == EXIT_DEAD))
  546. return;
  547. /* We don't want people slaying init. */
  548. p->exit_signal = SIGCHLD;
  549. /* If it has exited notify the new parent about this child's death. */
  550. if (!p->ptrace &&
  551. p->exit_state == EXIT_ZOMBIE && thread_group_empty(p)) {
  552. if (do_notify_parent(p, p->exit_signal)) {
  553. p->exit_state = EXIT_DEAD;
  554. list_add(&p->ptrace_entry, dead);
  555. }
  556. }
  557. kill_orphaned_pgrp(p, father);
  558. }
  559. /*
  560. * This does two things:
  561. *
  562. * A. Make init inherit all the child processes
  563. * B. Check to see if any process groups have become orphaned
  564. * as a result of our exiting, and if they have any stopped
  565. * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
  566. */
  567. static void forget_original_parent(struct task_struct *father,
  568. struct list_head *dead)
  569. {
  570. struct task_struct *p, *t, *reaper;
  571. if (unlikely(!list_empty(&father->ptraced)))
  572. exit_ptrace(father, dead);
  573. /* Can drop and reacquire tasklist_lock */
  574. reaper = find_child_reaper(father);
  575. if (list_empty(&father->children))
  576. return;
  577. reaper = find_new_reaper(father, reaper);
  578. list_for_each_entry(p, &father->children, sibling) {
  579. for_each_thread(p, t) {
  580. t->real_parent = reaper;
  581. BUG_ON((!t->ptrace) != (t->parent == father));
  582. if (likely(!t->ptrace))
  583. t->parent = t->real_parent;
  584. if (t->pdeath_signal)
  585. group_send_sig_info(t->pdeath_signal,
  586. SEND_SIG_NOINFO, t);
  587. }
  588. /*
  589. * If this is a threaded reparent there is no need to
  590. * notify anyone anything has happened.
  591. */
  592. if (!same_thread_group(reaper, father))
  593. reparent_leader(father, p, dead);
  594. }
  595. list_splice_tail_init(&father->children, &reaper->children);
  596. }
  597. /*
  598. * Send signals to all our closest relatives so that they know
  599. * to properly mourn us..
  600. */
  601. static void exit_notify(struct task_struct *tsk, int group_dead)
  602. {
  603. bool autoreap;
  604. struct task_struct *p, *n;
  605. LIST_HEAD(dead);
  606. write_lock_irq(&tasklist_lock);
  607. forget_original_parent(tsk, &dead);
  608. if (group_dead)
  609. kill_orphaned_pgrp(tsk->group_leader, NULL);
  610. if (unlikely(tsk->ptrace)) {
  611. int sig = thread_group_leader(tsk) &&
  612. thread_group_empty(tsk) &&
  613. !ptrace_reparented(tsk) ?
  614. tsk->exit_signal : SIGCHLD;
  615. autoreap = do_notify_parent(tsk, sig);
  616. } else if (thread_group_leader(tsk)) {
  617. autoreap = thread_group_empty(tsk) &&
  618. do_notify_parent(tsk, tsk->exit_signal);
  619. } else {
  620. autoreap = true;
  621. }
  622. tsk->exit_state = autoreap ? EXIT_DEAD : EXIT_ZOMBIE;
  623. if (tsk->exit_state == EXIT_DEAD)
  624. list_add(&tsk->ptrace_entry, &dead);
  625. /* mt-exec, de_thread() is waiting for group leader */
  626. if (unlikely(tsk->signal->notify_count < 0))
  627. wake_up_process(tsk->signal->group_exit_task);
  628. write_unlock_irq(&tasklist_lock);
  629. list_for_each_entry_safe(p, n, &dead, ptrace_entry) {
  630. list_del_init(&p->ptrace_entry);
  631. release_task(p);
  632. }
  633. }
  634. #ifdef CONFIG_DEBUG_STACK_USAGE
  635. static void check_stack_usage(void)
  636. {
  637. static DEFINE_SPINLOCK(low_water_lock);
  638. static int lowest_to_date = THREAD_SIZE;
  639. unsigned long free;
  640. free = stack_not_used(current);
  641. if (free >= lowest_to_date)
  642. return;
  643. spin_lock(&low_water_lock);
  644. if (free < lowest_to_date) {
  645. pr_info("%s (%d) used greatest stack depth: %lu bytes left\n",
  646. current->comm, task_pid_nr(current), free);
  647. lowest_to_date = free;
  648. }
  649. spin_unlock(&low_water_lock);
  650. }
  651. #else
  652. static inline void check_stack_usage(void) {}
  653. #endif
  654. void __noreturn do_exit(long code)
  655. {
  656. struct task_struct *tsk = current;
  657. int group_dead;
  658. TASKS_RCU(int tasks_rcu_i);
  659. profile_task_exit(tsk);
  660. kcov_task_exit(tsk);
  661. WARN_ON(blk_needs_flush_plug(tsk));
  662. if (unlikely(in_interrupt()))
  663. panic("Aiee, killing interrupt handler!");
  664. if (unlikely(!tsk->pid))
  665. panic("Attempted to kill the idle task!");
  666. /*
  667. * If do_exit is called because this processes oopsed, it's possible
  668. * that get_fs() was left as KERNEL_DS, so reset it to USER_DS before
  669. * continuing. Amongst other possible reasons, this is to prevent
  670. * mm_release()->clear_child_tid() from writing to a user-controlled
  671. * kernel address.
  672. */
  673. set_fs(USER_DS);
  674. ptrace_event(PTRACE_EVENT_EXIT, code);
  675. validate_creds_for_do_exit(tsk);
  676. /*
  677. * We're taking recursive faults here in do_exit. Safest is to just
  678. * leave this task alone and wait for reboot.
  679. */
  680. if (unlikely(tsk->flags & PF_EXITING)) {
  681. pr_alert("Fixing recursive fault but reboot is needed!\n");
  682. /*
  683. * We can do this unlocked here. The futex code uses
  684. * this flag just to verify whether the pi state
  685. * cleanup has been done or not. In the worst case it
  686. * loops once more. We pretend that the cleanup was
  687. * done as there is no way to return. Either the
  688. * OWNER_DIED bit is set by now or we push the blocked
  689. * task into the wait for ever nirwana as well.
  690. */
  691. tsk->flags |= PF_EXITPIDONE;
  692. set_current_state(TASK_UNINTERRUPTIBLE);
  693. schedule();
  694. }
  695. exit_signals(tsk); /* sets PF_EXITING */
  696. /*
  697. * Ensure that all new tsk->pi_lock acquisitions must observe
  698. * PF_EXITING. Serializes against futex.c:attach_to_pi_owner().
  699. */
  700. smp_mb();
  701. /*
  702. * Ensure that we must observe the pi_state in exit_mm() ->
  703. * mm_release() -> exit_pi_state_list().
  704. */
  705. raw_spin_unlock_wait(&tsk->pi_lock);
  706. if (unlikely(in_atomic())) {
  707. pr_info("note: %s[%d] exited with preempt_count %d\n",
  708. current->comm, task_pid_nr(current),
  709. preempt_count());
  710. preempt_count_set(PREEMPT_ENABLED);
  711. }
  712. /* sync mm's RSS info before statistics gathering */
  713. if (tsk->mm)
  714. sync_mm_rss(tsk->mm);
  715. acct_update_integrals(tsk);
  716. group_dead = atomic_dec_and_test(&tsk->signal->live);
  717. if (group_dead) {
  718. #ifdef CONFIG_POSIX_TIMERS
  719. hrtimer_cancel(&tsk->signal->real_timer);
  720. exit_itimers(tsk->signal);
  721. #endif
  722. if (tsk->mm)
  723. setmax_mm_hiwater_rss(&tsk->signal->maxrss, tsk->mm);
  724. }
  725. acct_collect(code, group_dead);
  726. if (group_dead)
  727. tty_audit_exit();
  728. audit_free(tsk);
  729. tsk->exit_code = code;
  730. taskstats_exit(tsk, group_dead);
  731. exit_mm(tsk);
  732. if (group_dead)
  733. acct_process();
  734. trace_sched_process_exit(tsk);
  735. exit_sem(tsk);
  736. exit_shm(tsk);
  737. exit_files(tsk);
  738. exit_fs(tsk);
  739. if (group_dead)
  740. disassociate_ctty(1);
  741. exit_task_namespaces(tsk);
  742. exit_task_work(tsk);
  743. exit_thread(tsk);
  744. /*
  745. * Flush inherited counters to the parent - before the parent
  746. * gets woken up by child-exit notifications.
  747. *
  748. * because of cgroup mode, must be called before cgroup_exit()
  749. */
  750. perf_event_exit_task(tsk);
  751. sched_autogroup_exit_task(tsk);
  752. cgroup_exit(tsk);
  753. /*
  754. * FIXME: do that only when needed, using sched_exit tracepoint
  755. */
  756. flush_ptrace_hw_breakpoint(tsk);
  757. TASKS_RCU(preempt_disable());
  758. TASKS_RCU(tasks_rcu_i = __srcu_read_lock(&tasks_rcu_exit_srcu));
  759. TASKS_RCU(preempt_enable());
  760. exit_notify(tsk, group_dead);
  761. proc_exit_connector(tsk);
  762. mpol_put_task_policy(tsk);
  763. #ifdef CONFIG_FUTEX
  764. if (unlikely(current->pi_state_cache))
  765. kfree(current->pi_state_cache);
  766. #endif
  767. /*
  768. * Make sure we are holding no locks:
  769. */
  770. debug_check_no_locks_held();
  771. /*
  772. * We can do this unlocked here. The futex code uses this flag
  773. * just to verify whether the pi state cleanup has been done
  774. * or not. In the worst case it loops once more.
  775. */
  776. tsk->flags |= PF_EXITPIDONE;
  777. if (tsk->io_context)
  778. exit_io_context(tsk);
  779. if (tsk->splice_pipe)
  780. free_pipe_info(tsk->splice_pipe);
  781. if (tsk->task_frag.page)
  782. put_page(tsk->task_frag.page);
  783. validate_creds_for_do_exit(tsk);
  784. check_stack_usage();
  785. preempt_disable();
  786. if (tsk->nr_dirtied)
  787. __this_cpu_add(dirty_throttle_leaks, tsk->nr_dirtied);
  788. exit_rcu();
  789. TASKS_RCU(__srcu_read_unlock(&tasks_rcu_exit_srcu, tasks_rcu_i));
  790. do_task_dead();
  791. }
  792. EXPORT_SYMBOL_GPL(do_exit);
  793. void complete_and_exit(struct completion *comp, long code)
  794. {
  795. if (comp)
  796. complete(comp);
  797. do_exit(code);
  798. }
  799. EXPORT_SYMBOL(complete_and_exit);
  800. SYSCALL_DEFINE1(exit, int, error_code)
  801. {
  802. do_exit((error_code&0xff)<<8);
  803. }
  804. /*
  805. * Take down every thread in the group. This is called by fatal signals
  806. * as well as by sys_exit_group (below).
  807. */
  808. void
  809. do_group_exit(int exit_code)
  810. {
  811. struct signal_struct *sig = current->signal;
  812. BUG_ON(exit_code & 0x80); /* core dumps don't get here */
  813. if (signal_group_exit(sig))
  814. exit_code = sig->group_exit_code;
  815. else if (!thread_group_empty(current)) {
  816. struct sighand_struct *const sighand = current->sighand;
  817. spin_lock_irq(&sighand->siglock);
  818. if (signal_group_exit(sig))
  819. /* Another thread got here before we took the lock. */
  820. exit_code = sig->group_exit_code;
  821. else {
  822. sig->group_exit_code = exit_code;
  823. sig->flags = SIGNAL_GROUP_EXIT;
  824. zap_other_threads(current);
  825. }
  826. spin_unlock_irq(&sighand->siglock);
  827. }
  828. do_exit(exit_code);
  829. /* NOTREACHED */
  830. }
  831. /*
  832. * this kills every thread in the thread group. Note that any externally
  833. * wait4()-ing process will get the correct exit code - even if this
  834. * thread is not the thread group leader.
  835. */
  836. SYSCALL_DEFINE1(exit_group, int, error_code)
  837. {
  838. do_group_exit((error_code & 0xff) << 8);
  839. /* NOTREACHED */
  840. return 0;
  841. }
  842. struct wait_opts {
  843. enum pid_type wo_type;
  844. int wo_flags;
  845. struct pid *wo_pid;
  846. struct siginfo __user *wo_info;
  847. int __user *wo_stat;
  848. struct rusage __user *wo_rusage;
  849. wait_queue_t child_wait;
  850. int notask_error;
  851. };
  852. static inline
  853. struct pid *task_pid_type(struct task_struct *task, enum pid_type type)
  854. {
  855. if (type != PIDTYPE_PID)
  856. task = task->group_leader;
  857. return task->pids[type].pid;
  858. }
  859. static int eligible_pid(struct wait_opts *wo, struct task_struct *p)
  860. {
  861. return wo->wo_type == PIDTYPE_MAX ||
  862. task_pid_type(p, wo->wo_type) == wo->wo_pid;
  863. }
  864. static int
  865. eligible_child(struct wait_opts *wo, bool ptrace, struct task_struct *p)
  866. {
  867. if (!eligible_pid(wo, p))
  868. return 0;
  869. /*
  870. * Wait for all children (clone and not) if __WALL is set or
  871. * if it is traced by us.
  872. */
  873. if (ptrace || (wo->wo_flags & __WALL))
  874. return 1;
  875. /*
  876. * Otherwise, wait for clone children *only* if __WCLONE is set;
  877. * otherwise, wait for non-clone children *only*.
  878. *
  879. * Note: a "clone" child here is one that reports to its parent
  880. * using a signal other than SIGCHLD, or a non-leader thread which
  881. * we can only see if it is traced by us.
  882. */
  883. if ((p->exit_signal != SIGCHLD) ^ !!(wo->wo_flags & __WCLONE))
  884. return 0;
  885. return 1;
  886. }
  887. static int wait_noreap_copyout(struct wait_opts *wo, struct task_struct *p,
  888. pid_t pid, uid_t uid, int why, int status)
  889. {
  890. struct siginfo __user *infop;
  891. int retval = wo->wo_rusage
  892. ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
  893. put_task_struct(p);
  894. infop = wo->wo_info;
  895. if (infop) {
  896. if (!retval)
  897. retval = put_user(SIGCHLD, &infop->si_signo);
  898. if (!retval)
  899. retval = put_user(0, &infop->si_errno);
  900. if (!retval)
  901. retval = put_user((short)why, &infop->si_code);
  902. if (!retval)
  903. retval = put_user(pid, &infop->si_pid);
  904. if (!retval)
  905. retval = put_user(uid, &infop->si_uid);
  906. if (!retval)
  907. retval = put_user(status, &infop->si_status);
  908. }
  909. if (!retval)
  910. retval = pid;
  911. return retval;
  912. }
  913. /*
  914. * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
  915. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  916. * the lock and this task is uninteresting. If we return nonzero, we have
  917. * released the lock and the system call should return.
  918. */
  919. static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
  920. {
  921. int state, retval, status;
  922. pid_t pid = task_pid_vnr(p);
  923. uid_t uid = from_kuid_munged(current_user_ns(), task_uid(p));
  924. struct siginfo __user *infop;
  925. if (!likely(wo->wo_flags & WEXITED))
  926. return 0;
  927. if (unlikely(wo->wo_flags & WNOWAIT)) {
  928. int exit_code = p->exit_code;
  929. int why;
  930. get_task_struct(p);
  931. read_unlock(&tasklist_lock);
  932. sched_annotate_sleep();
  933. if ((exit_code & 0x7f) == 0) {
  934. why = CLD_EXITED;
  935. status = exit_code >> 8;
  936. } else {
  937. why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
  938. status = exit_code & 0x7f;
  939. }
  940. return wait_noreap_copyout(wo, p, pid, uid, why, status);
  941. }
  942. /*
  943. * Move the task's state to DEAD/TRACE, only one thread can do this.
  944. */
  945. state = (ptrace_reparented(p) && thread_group_leader(p)) ?
  946. EXIT_TRACE : EXIT_DEAD;
  947. if (cmpxchg(&p->exit_state, EXIT_ZOMBIE, state) != EXIT_ZOMBIE)
  948. return 0;
  949. /*
  950. * We own this thread, nobody else can reap it.
  951. */
  952. read_unlock(&tasklist_lock);
  953. sched_annotate_sleep();
  954. /*
  955. * Check thread_group_leader() to exclude the traced sub-threads.
  956. */
  957. if (state == EXIT_DEAD && thread_group_leader(p)) {
  958. struct signal_struct *sig = p->signal;
  959. struct signal_struct *psig = current->signal;
  960. unsigned long maxrss;
  961. cputime_t tgutime, tgstime;
  962. /*
  963. * The resource counters for the group leader are in its
  964. * own task_struct. Those for dead threads in the group
  965. * are in its signal_struct, as are those for the child
  966. * processes it has previously reaped. All these
  967. * accumulate in the parent's signal_struct c* fields.
  968. *
  969. * We don't bother to take a lock here to protect these
  970. * p->signal fields because the whole thread group is dead
  971. * and nobody can change them.
  972. *
  973. * psig->stats_lock also protects us from our sub-theads
  974. * which can reap other children at the same time. Until
  975. * we change k_getrusage()-like users to rely on this lock
  976. * we have to take ->siglock as well.
  977. *
  978. * We use thread_group_cputime_adjusted() to get times for
  979. * the thread group, which consolidates times for all threads
  980. * in the group including the group leader.
  981. */
  982. thread_group_cputime_adjusted(p, &tgutime, &tgstime);
  983. spin_lock_irq(&current->sighand->siglock);
  984. write_seqlock(&psig->stats_lock);
  985. psig->cutime += tgutime + sig->cutime;
  986. psig->cstime += tgstime + sig->cstime;
  987. psig->cgtime += task_gtime(p) + sig->gtime + sig->cgtime;
  988. psig->cmin_flt +=
  989. p->min_flt + sig->min_flt + sig->cmin_flt;
  990. psig->cmaj_flt +=
  991. p->maj_flt + sig->maj_flt + sig->cmaj_flt;
  992. psig->cnvcsw +=
  993. p->nvcsw + sig->nvcsw + sig->cnvcsw;
  994. psig->cnivcsw +=
  995. p->nivcsw + sig->nivcsw + sig->cnivcsw;
  996. psig->cinblock +=
  997. task_io_get_inblock(p) +
  998. sig->inblock + sig->cinblock;
  999. psig->coublock +=
  1000. task_io_get_oublock(p) +
  1001. sig->oublock + sig->coublock;
  1002. maxrss = max(sig->maxrss, sig->cmaxrss);
  1003. if (psig->cmaxrss < maxrss)
  1004. psig->cmaxrss = maxrss;
  1005. task_io_accounting_add(&psig->ioac, &p->ioac);
  1006. task_io_accounting_add(&psig->ioac, &sig->ioac);
  1007. write_sequnlock(&psig->stats_lock);
  1008. spin_unlock_irq(&current->sighand->siglock);
  1009. }
  1010. retval = wo->wo_rusage
  1011. ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
  1012. status = (p->signal->flags & SIGNAL_GROUP_EXIT)
  1013. ? p->signal->group_exit_code : p->exit_code;
  1014. if (!retval && wo->wo_stat)
  1015. retval = put_user(status, wo->wo_stat);
  1016. infop = wo->wo_info;
  1017. if (!retval && infop)
  1018. retval = put_user(SIGCHLD, &infop->si_signo);
  1019. if (!retval && infop)
  1020. retval = put_user(0, &infop->si_errno);
  1021. if (!retval && infop) {
  1022. int why;
  1023. if ((status & 0x7f) == 0) {
  1024. why = CLD_EXITED;
  1025. status >>= 8;
  1026. } else {
  1027. why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
  1028. status &= 0x7f;
  1029. }
  1030. retval = put_user((short)why, &infop->si_code);
  1031. if (!retval)
  1032. retval = put_user(status, &infop->si_status);
  1033. }
  1034. if (!retval && infop)
  1035. retval = put_user(pid, &infop->si_pid);
  1036. if (!retval && infop)
  1037. retval = put_user(uid, &infop->si_uid);
  1038. if (!retval)
  1039. retval = pid;
  1040. if (state == EXIT_TRACE) {
  1041. write_lock_irq(&tasklist_lock);
  1042. /* We dropped tasklist, ptracer could die and untrace */
  1043. ptrace_unlink(p);
  1044. /* If parent wants a zombie, don't release it now */
  1045. state = EXIT_ZOMBIE;
  1046. if (do_notify_parent(p, p->exit_signal))
  1047. state = EXIT_DEAD;
  1048. p->exit_state = state;
  1049. write_unlock_irq(&tasklist_lock);
  1050. }
  1051. if (state == EXIT_DEAD)
  1052. release_task(p);
  1053. return retval;
  1054. }
  1055. static int *task_stopped_code(struct task_struct *p, bool ptrace)
  1056. {
  1057. if (ptrace) {
  1058. if (task_is_traced(p) && !(p->jobctl & JOBCTL_LISTENING))
  1059. return &p->exit_code;
  1060. } else {
  1061. if (p->signal->flags & SIGNAL_STOP_STOPPED)
  1062. return &p->signal->group_exit_code;
  1063. }
  1064. return NULL;
  1065. }
  1066. /**
  1067. * wait_task_stopped - Wait for %TASK_STOPPED or %TASK_TRACED
  1068. * @wo: wait options
  1069. * @ptrace: is the wait for ptrace
  1070. * @p: task to wait for
  1071. *
  1072. * Handle sys_wait4() work for %p in state %TASK_STOPPED or %TASK_TRACED.
  1073. *
  1074. * CONTEXT:
  1075. * read_lock(&tasklist_lock), which is released if return value is
  1076. * non-zero. Also, grabs and releases @p->sighand->siglock.
  1077. *
  1078. * RETURNS:
  1079. * 0 if wait condition didn't exist and search for other wait conditions
  1080. * should continue. Non-zero return, -errno on failure and @p's pid on
  1081. * success, implies that tasklist_lock is released and wait condition
  1082. * search should terminate.
  1083. */
  1084. static int wait_task_stopped(struct wait_opts *wo,
  1085. int ptrace, struct task_struct *p)
  1086. {
  1087. struct siginfo __user *infop;
  1088. int retval, exit_code, *p_code, why;
  1089. uid_t uid = 0; /* unneeded, required by compiler */
  1090. pid_t pid;
  1091. /*
  1092. * Traditionally we see ptrace'd stopped tasks regardless of options.
  1093. */
  1094. if (!ptrace && !(wo->wo_flags & WUNTRACED))
  1095. return 0;
  1096. if (!task_stopped_code(p, ptrace))
  1097. return 0;
  1098. exit_code = 0;
  1099. spin_lock_irq(&p->sighand->siglock);
  1100. p_code = task_stopped_code(p, ptrace);
  1101. if (unlikely(!p_code))
  1102. goto unlock_sig;
  1103. exit_code = *p_code;
  1104. if (!exit_code)
  1105. goto unlock_sig;
  1106. if (!unlikely(wo->wo_flags & WNOWAIT))
  1107. *p_code = 0;
  1108. uid = from_kuid_munged(current_user_ns(), task_uid(p));
  1109. unlock_sig:
  1110. spin_unlock_irq(&p->sighand->siglock);
  1111. if (!exit_code)
  1112. return 0;
  1113. /*
  1114. * Now we are pretty sure this task is interesting.
  1115. * Make sure it doesn't get reaped out from under us while we
  1116. * give up the lock and then examine it below. We don't want to
  1117. * keep holding onto the tasklist_lock while we call getrusage and
  1118. * possibly take page faults for user memory.
  1119. */
  1120. get_task_struct(p);
  1121. pid = task_pid_vnr(p);
  1122. why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
  1123. read_unlock(&tasklist_lock);
  1124. sched_annotate_sleep();
  1125. if (unlikely(wo->wo_flags & WNOWAIT))
  1126. return wait_noreap_copyout(wo, p, pid, uid, why, exit_code);
  1127. retval = wo->wo_rusage
  1128. ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
  1129. if (!retval && wo->wo_stat)
  1130. retval = put_user((exit_code << 8) | 0x7f, wo->wo_stat);
  1131. infop = wo->wo_info;
  1132. if (!retval && infop)
  1133. retval = put_user(SIGCHLD, &infop->si_signo);
  1134. if (!retval && infop)
  1135. retval = put_user(0, &infop->si_errno);
  1136. if (!retval && infop)
  1137. retval = put_user((short)why, &infop->si_code);
  1138. if (!retval && infop)
  1139. retval = put_user(exit_code, &infop->si_status);
  1140. if (!retval && infop)
  1141. retval = put_user(pid, &infop->si_pid);
  1142. if (!retval && infop)
  1143. retval = put_user(uid, &infop->si_uid);
  1144. if (!retval)
  1145. retval = pid;
  1146. put_task_struct(p);
  1147. BUG_ON(!retval);
  1148. return retval;
  1149. }
  1150. /*
  1151. * Handle do_wait work for one task in a live, non-stopped state.
  1152. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  1153. * the lock and this task is uninteresting. If we return nonzero, we have
  1154. * released the lock and the system call should return.
  1155. */
  1156. static int wait_task_continued(struct wait_opts *wo, struct task_struct *p)
  1157. {
  1158. int retval;
  1159. pid_t pid;
  1160. uid_t uid;
  1161. if (!unlikely(wo->wo_flags & WCONTINUED))
  1162. return 0;
  1163. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
  1164. return 0;
  1165. spin_lock_irq(&p->sighand->siglock);
  1166. /* Re-check with the lock held. */
  1167. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
  1168. spin_unlock_irq(&p->sighand->siglock);
  1169. return 0;
  1170. }
  1171. if (!unlikely(wo->wo_flags & WNOWAIT))
  1172. p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
  1173. uid = from_kuid_munged(current_user_ns(), task_uid(p));
  1174. spin_unlock_irq(&p->sighand->siglock);
  1175. pid = task_pid_vnr(p);
  1176. get_task_struct(p);
  1177. read_unlock(&tasklist_lock);
  1178. sched_annotate_sleep();
  1179. if (!wo->wo_info) {
  1180. retval = wo->wo_rusage
  1181. ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
  1182. put_task_struct(p);
  1183. if (!retval && wo->wo_stat)
  1184. retval = put_user(0xffff, wo->wo_stat);
  1185. if (!retval)
  1186. retval = pid;
  1187. } else {
  1188. retval = wait_noreap_copyout(wo, p, pid, uid,
  1189. CLD_CONTINUED, SIGCONT);
  1190. BUG_ON(retval == 0);
  1191. }
  1192. return retval;
  1193. }
  1194. /*
  1195. * Consider @p for a wait by @parent.
  1196. *
  1197. * -ECHILD should be in ->notask_error before the first call.
  1198. * Returns nonzero for a final return, when we have unlocked tasklist_lock.
  1199. * Returns zero if the search for a child should continue;
  1200. * then ->notask_error is 0 if @p is an eligible child,
  1201. * or another error from security_task_wait(), or still -ECHILD.
  1202. */
  1203. static int wait_consider_task(struct wait_opts *wo, int ptrace,
  1204. struct task_struct *p)
  1205. {
  1206. /*
  1207. * We can race with wait_task_zombie() from another thread.
  1208. * Ensure that EXIT_ZOMBIE -> EXIT_DEAD/EXIT_TRACE transition
  1209. * can't confuse the checks below.
  1210. */
  1211. int exit_state = ACCESS_ONCE(p->exit_state);
  1212. int ret;
  1213. if (unlikely(exit_state == EXIT_DEAD))
  1214. return 0;
  1215. ret = eligible_child(wo, ptrace, p);
  1216. if (!ret)
  1217. return ret;
  1218. ret = security_task_wait(p);
  1219. if (unlikely(ret < 0)) {
  1220. /*
  1221. * If we have not yet seen any eligible child,
  1222. * then let this error code replace -ECHILD.
  1223. * A permission error will give the user a clue
  1224. * to look for security policy problems, rather
  1225. * than for mysterious wait bugs.
  1226. */
  1227. if (wo->notask_error)
  1228. wo->notask_error = ret;
  1229. return 0;
  1230. }
  1231. if (unlikely(exit_state == EXIT_TRACE)) {
  1232. /*
  1233. * ptrace == 0 means we are the natural parent. In this case
  1234. * we should clear notask_error, debugger will notify us.
  1235. */
  1236. if (likely(!ptrace))
  1237. wo->notask_error = 0;
  1238. return 0;
  1239. }
  1240. if (likely(!ptrace) && unlikely(p->ptrace)) {
  1241. /*
  1242. * If it is traced by its real parent's group, just pretend
  1243. * the caller is ptrace_do_wait() and reap this child if it
  1244. * is zombie.
  1245. *
  1246. * This also hides group stop state from real parent; otherwise
  1247. * a single stop can be reported twice as group and ptrace stop.
  1248. * If a ptracer wants to distinguish these two events for its
  1249. * own children it should create a separate process which takes
  1250. * the role of real parent.
  1251. */
  1252. if (!ptrace_reparented(p))
  1253. ptrace = 1;
  1254. }
  1255. /* slay zombie? */
  1256. if (exit_state == EXIT_ZOMBIE) {
  1257. /* we don't reap group leaders with subthreads */
  1258. if (!delay_group_leader(p)) {
  1259. /*
  1260. * A zombie ptracee is only visible to its ptracer.
  1261. * Notification and reaping will be cascaded to the
  1262. * real parent when the ptracer detaches.
  1263. */
  1264. if (unlikely(ptrace) || likely(!p->ptrace))
  1265. return wait_task_zombie(wo, p);
  1266. }
  1267. /*
  1268. * Allow access to stopped/continued state via zombie by
  1269. * falling through. Clearing of notask_error is complex.
  1270. *
  1271. * When !@ptrace:
  1272. *
  1273. * If WEXITED is set, notask_error should naturally be
  1274. * cleared. If not, subset of WSTOPPED|WCONTINUED is set,
  1275. * so, if there are live subthreads, there are events to
  1276. * wait for. If all subthreads are dead, it's still safe
  1277. * to clear - this function will be called again in finite
  1278. * amount time once all the subthreads are released and
  1279. * will then return without clearing.
  1280. *
  1281. * When @ptrace:
  1282. *
  1283. * Stopped state is per-task and thus can't change once the
  1284. * target task dies. Only continued and exited can happen.
  1285. * Clear notask_error if WCONTINUED | WEXITED.
  1286. */
  1287. if (likely(!ptrace) || (wo->wo_flags & (WCONTINUED | WEXITED)))
  1288. wo->notask_error = 0;
  1289. } else {
  1290. /*
  1291. * @p is alive and it's gonna stop, continue or exit, so
  1292. * there always is something to wait for.
  1293. */
  1294. wo->notask_error = 0;
  1295. }
  1296. /*
  1297. * Wait for stopped. Depending on @ptrace, different stopped state
  1298. * is used and the two don't interact with each other.
  1299. */
  1300. ret = wait_task_stopped(wo, ptrace, p);
  1301. if (ret)
  1302. return ret;
  1303. /*
  1304. * Wait for continued. There's only one continued state and the
  1305. * ptracer can consume it which can confuse the real parent. Don't
  1306. * use WCONTINUED from ptracer. You don't need or want it.
  1307. */
  1308. return wait_task_continued(wo, p);
  1309. }
  1310. /*
  1311. * Do the work of do_wait() for one thread in the group, @tsk.
  1312. *
  1313. * -ECHILD should be in ->notask_error before the first call.
  1314. * Returns nonzero for a final return, when we have unlocked tasklist_lock.
  1315. * Returns zero if the search for a child should continue; then
  1316. * ->notask_error is 0 if there were any eligible children,
  1317. * or another error from security_task_wait(), or still -ECHILD.
  1318. */
  1319. static int do_wait_thread(struct wait_opts *wo, struct task_struct *tsk)
  1320. {
  1321. struct task_struct *p;
  1322. list_for_each_entry(p, &tsk->children, sibling) {
  1323. int ret = wait_consider_task(wo, 0, p);
  1324. if (ret)
  1325. return ret;
  1326. }
  1327. return 0;
  1328. }
  1329. static int ptrace_do_wait(struct wait_opts *wo, struct task_struct *tsk)
  1330. {
  1331. struct task_struct *p;
  1332. list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {
  1333. int ret = wait_consider_task(wo, 1, p);
  1334. if (ret)
  1335. return ret;
  1336. }
  1337. return 0;
  1338. }
  1339. static int child_wait_callback(wait_queue_t *wait, unsigned mode,
  1340. int sync, void *key)
  1341. {
  1342. struct wait_opts *wo = container_of(wait, struct wait_opts,
  1343. child_wait);
  1344. struct task_struct *p = key;
  1345. if (!eligible_pid(wo, p))
  1346. return 0;
  1347. if ((wo->wo_flags & __WNOTHREAD) && wait->private != p->parent)
  1348. return 0;
  1349. return default_wake_function(wait, mode, sync, key);
  1350. }
  1351. void __wake_up_parent(struct task_struct *p, struct task_struct *parent)
  1352. {
  1353. __wake_up_sync_key(&parent->signal->wait_chldexit,
  1354. TASK_INTERRUPTIBLE, 1, p);
  1355. }
  1356. static long do_wait(struct wait_opts *wo)
  1357. {
  1358. struct task_struct *tsk;
  1359. int retval;
  1360. trace_sched_process_wait(wo->wo_pid);
  1361. init_waitqueue_func_entry(&wo->child_wait, child_wait_callback);
  1362. wo->child_wait.private = current;
  1363. add_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
  1364. repeat:
  1365. /*
  1366. * If there is nothing that can match our criteria, just get out.
  1367. * We will clear ->notask_error to zero if we see any child that
  1368. * might later match our criteria, even if we are not able to reap
  1369. * it yet.
  1370. */
  1371. wo->notask_error = -ECHILD;
  1372. if ((wo->wo_type < PIDTYPE_MAX) &&
  1373. (!wo->wo_pid || hlist_empty(&wo->wo_pid->tasks[wo->wo_type])))
  1374. goto notask;
  1375. set_current_state(TASK_INTERRUPTIBLE);
  1376. read_lock(&tasklist_lock);
  1377. tsk = current;
  1378. do {
  1379. retval = do_wait_thread(wo, tsk);
  1380. if (retval)
  1381. goto end;
  1382. retval = ptrace_do_wait(wo, tsk);
  1383. if (retval)
  1384. goto end;
  1385. if (wo->wo_flags & __WNOTHREAD)
  1386. break;
  1387. } while_each_thread(current, tsk);
  1388. read_unlock(&tasklist_lock);
  1389. notask:
  1390. retval = wo->notask_error;
  1391. if (!retval && !(wo->wo_flags & WNOHANG)) {
  1392. retval = -ERESTARTSYS;
  1393. if (!signal_pending(current)) {
  1394. schedule();
  1395. goto repeat;
  1396. }
  1397. }
  1398. end:
  1399. __set_current_state(TASK_RUNNING);
  1400. remove_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
  1401. return retval;
  1402. }
  1403. SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
  1404. infop, int, options, struct rusage __user *, ru)
  1405. {
  1406. struct wait_opts wo;
  1407. struct pid *pid = NULL;
  1408. enum pid_type type;
  1409. long ret;
  1410. if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED|
  1411. __WNOTHREAD|__WCLONE|__WALL))
  1412. return -EINVAL;
  1413. if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
  1414. return -EINVAL;
  1415. switch (which) {
  1416. case P_ALL:
  1417. type = PIDTYPE_MAX;
  1418. break;
  1419. case P_PID:
  1420. type = PIDTYPE_PID;
  1421. if (upid <= 0)
  1422. return -EINVAL;
  1423. break;
  1424. case P_PGID:
  1425. type = PIDTYPE_PGID;
  1426. if (upid <= 0)
  1427. return -EINVAL;
  1428. break;
  1429. default:
  1430. return -EINVAL;
  1431. }
  1432. if (type < PIDTYPE_MAX)
  1433. pid = find_get_pid(upid);
  1434. wo.wo_type = type;
  1435. wo.wo_pid = pid;
  1436. wo.wo_flags = options;
  1437. wo.wo_info = infop;
  1438. wo.wo_stat = NULL;
  1439. wo.wo_rusage = ru;
  1440. ret = do_wait(&wo);
  1441. if (ret > 0) {
  1442. ret = 0;
  1443. } else if (infop) {
  1444. /*
  1445. * For a WNOHANG return, clear out all the fields
  1446. * we would set so the user can easily tell the
  1447. * difference.
  1448. */
  1449. if (!ret)
  1450. ret = put_user(0, &infop->si_signo);
  1451. if (!ret)
  1452. ret = put_user(0, &infop->si_errno);
  1453. if (!ret)
  1454. ret = put_user(0, &infop->si_code);
  1455. if (!ret)
  1456. ret = put_user(0, &infop->si_pid);
  1457. if (!ret)
  1458. ret = put_user(0, &infop->si_uid);
  1459. if (!ret)
  1460. ret = put_user(0, &infop->si_status);
  1461. }
  1462. put_pid(pid);
  1463. return ret;
  1464. }
  1465. SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
  1466. int, options, struct rusage __user *, ru)
  1467. {
  1468. struct wait_opts wo;
  1469. struct pid *pid = NULL;
  1470. enum pid_type type;
  1471. long ret;
  1472. if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
  1473. __WNOTHREAD|__WCLONE|__WALL))
  1474. return -EINVAL;
  1475. if (upid == -1)
  1476. type = PIDTYPE_MAX;
  1477. else if (upid < 0) {
  1478. type = PIDTYPE_PGID;
  1479. pid = find_get_pid(-upid);
  1480. } else if (upid == 0) {
  1481. type = PIDTYPE_PGID;
  1482. pid = get_task_pid(current, PIDTYPE_PGID);
  1483. } else /* upid > 0 */ {
  1484. type = PIDTYPE_PID;
  1485. pid = find_get_pid(upid);
  1486. }
  1487. wo.wo_type = type;
  1488. wo.wo_pid = pid;
  1489. wo.wo_flags = options | WEXITED;
  1490. wo.wo_info = NULL;
  1491. wo.wo_stat = stat_addr;
  1492. wo.wo_rusage = ru;
  1493. ret = do_wait(&wo);
  1494. put_pid(pid);
  1495. return ret;
  1496. }
  1497. #ifdef __ARCH_WANT_SYS_WAITPID
  1498. /*
  1499. * sys_waitpid() remains for compatibility. waitpid() should be
  1500. * implemented by calling sys_wait4() from libc.a.
  1501. */
  1502. SYSCALL_DEFINE3(waitpid, pid_t, pid, int __user *, stat_addr, int, options)
  1503. {
  1504. return sys_wait4(pid, stat_addr, options, NULL);
  1505. }
  1506. #endif