exit.c 44 KB

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