exit.c 44 KB

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