exit.c 44 KB

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