exit.c 44 KB

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