exit.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  1. /*
  2. * linux/kernel/exit.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/mm.h>
  7. #include <linux/slab.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/module.h>
  10. #include <linux/capability.h>
  11. #include <linux/completion.h>
  12. #include <linux/personality.h>
  13. #include <linux/tty.h>
  14. #include <linux/iocontext.h>
  15. #include <linux/key.h>
  16. #include <linux/security.h>
  17. #include <linux/cpu.h>
  18. #include <linux/acct.h>
  19. #include <linux/tsacct_kern.h>
  20. #include <linux/file.h>
  21. #include <linux/fdtable.h>
  22. #include <linux/freezer.h>
  23. #include <linux/binfmts.h>
  24. #include <linux/nsproxy.h>
  25. #include <linux/pid_namespace.h>
  26. #include <linux/ptrace.h>
  27. #include <linux/profile.h>
  28. #include <linux/mount.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/kthread.h>
  31. #include <linux/mempolicy.h>
  32. #include <linux/taskstats_kern.h>
  33. #include <linux/delayacct.h>
  34. #include <linux/cgroup.h>
  35. #include <linux/syscalls.h>
  36. #include <linux/signal.h>
  37. #include <linux/posix-timers.h>
  38. #include <linux/cn_proc.h>
  39. #include <linux/mutex.h>
  40. #include <linux/futex.h>
  41. #include <linux/pipe_fs_i.h>
  42. #include <linux/audit.h> /* for audit_free() */
  43. #include <linux/resource.h>
  44. #include <linux/blkdev.h>
  45. #include <linux/task_io_accounting_ops.h>
  46. #include <linux/tracehook.h>
  47. #include <linux/fs_struct.h>
  48. #include <linux/init_task.h>
  49. #include <linux/perf_event.h>
  50. #include <trace/events/sched.h>
  51. #include <linux/hw_breakpoint.h>
  52. #include <linux/oom.h>
  53. #include <linux/writeback.h>
  54. #include <linux/shm.h>
  55. #ifdef CONFIG_MTPROF
  56. #include "mt_sched_mon.h"
  57. #include "mt_cputime.h"
  58. #endif
  59. #include <asm/uaccess.h>
  60. #include <asm/unistd.h>
  61. #include <asm/pgtable.h>
  62. #include <asm/mmu_context.h>
  63. static void exit_mm(struct task_struct *tsk);
  64. static void __unhash_process(struct task_struct *p, bool group_dead)
  65. {
  66. nr_threads--;
  67. detach_pid(p, PIDTYPE_PID);
  68. if (group_dead) {
  69. detach_pid(p, PIDTYPE_PGID);
  70. detach_pid(p, PIDTYPE_SID);
  71. list_del_rcu(&p->tasks);
  72. list_del_init(&p->sibling);
  73. __this_cpu_dec(process_counts);
  74. }
  75. list_del_rcu(&p->thread_group);
  76. list_del_rcu(&p->thread_node);
  77. }
  78. /*
  79. * This function expects the tasklist_lock write-locked.
  80. */
  81. static void __exit_signal(struct task_struct *tsk)
  82. {
  83. struct signal_struct *sig = tsk->signal;
  84. bool group_dead = thread_group_leader(tsk);
  85. struct sighand_struct *sighand;
  86. struct tty_struct *uninitialized_var(tty);
  87. cputime_t utime, stime;
  88. sighand = rcu_dereference_check(tsk->sighand,
  89. lockdep_tasklist_lock_is_held());
  90. spin_lock(&sighand->siglock);
  91. posix_cpu_timers_exit(tsk);
  92. if (group_dead) {
  93. posix_cpu_timers_exit_group(tsk);
  94. tty = sig->tty;
  95. sig->tty = NULL;
  96. } else {
  97. /*
  98. * This can only happen if the caller is de_thread().
  99. * FIXME: this is the temporary hack, we should teach
  100. * posix-cpu-timers to handle this case correctly.
  101. */
  102. if (unlikely(has_group_leader_pid(tsk)))
  103. posix_cpu_timers_exit_group(tsk);
  104. /*
  105. * If there is any task waiting for the group exit
  106. * then notify it:
  107. */
  108. if (sig->notify_count > 0 && !--sig->notify_count)
  109. wake_up_process(sig->group_exit_task);
  110. if (tsk == sig->curr_target)
  111. sig->curr_target = next_thread(tsk);
  112. }
  113. /*
  114. * Accumulate here the counters for all threads but the group leader
  115. * as they die, so they can be added into the process-wide totals
  116. * when those are taken. The group leader stays around as a zombie as
  117. * long as there are other threads. When it gets reaped, the exit.c
  118. * code will add its counts into these totals. We won't ever get here
  119. * for the group leader, since it will have been the last reference on
  120. * the signal_struct.
  121. */
  122. task_cputime(tsk, &utime, &stime);
  123. write_seqlock(&sig->stats_lock);
  124. sig->utime += utime;
  125. sig->stime += stime;
  126. sig->gtime += task_gtime(tsk);
  127. sig->min_flt += tsk->min_flt;
  128. sig->maj_flt += tsk->maj_flt;
  129. sig->nvcsw += tsk->nvcsw;
  130. sig->nivcsw += tsk->nivcsw;
  131. sig->inblock += task_io_get_inblock(tsk);
  132. sig->oublock += task_io_get_oublock(tsk);
  133. task_io_accounting_add(&sig->ioac, &tsk->ioac);
  134. sig->sum_sched_runtime += tsk->se.sum_exec_runtime;
  135. sig->nr_threads--;
  136. __unhash_process(tsk, group_dead);
  137. write_sequnlock(&sig->stats_lock);
  138. /*
  139. * Do this under ->siglock, we can race with another thread
  140. * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.
  141. */
  142. flush_sigqueue(&tsk->pending);
  143. tsk->sighand = NULL;
  144. spin_unlock(&sighand->siglock);
  145. __cleanup_sighand(sighand);
  146. clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
  147. if (group_dead) {
  148. flush_sigqueue(&sig->shared_pending);
  149. tty_kref_put(tty);
  150. }
  151. }
  152. static void delayed_put_task_struct(struct rcu_head *rhp)
  153. {
  154. struct task_struct *tsk = container_of(rhp, struct task_struct, rcu);
  155. perf_event_delayed_put(tsk);
  156. trace_sched_process_free(tsk);
  157. put_task_struct(tsk);
  158. }
  159. void release_task(struct task_struct *p)
  160. {
  161. struct task_struct *leader;
  162. int zap_leader;
  163. repeat:
  164. /* don't need to get the RCU readlock here - the process is dead and
  165. * can't be modifying its own credentials. But shut RCU-lockdep up */
  166. rcu_read_lock();
  167. atomic_dec(&__task_cred(p)->user->processes);
  168. rcu_read_unlock();
  169. proc_flush_task(p);
  170. write_lock_irq(&tasklist_lock);
  171. ptrace_release_task(p);
  172. __exit_signal(p);
  173. /*
  174. * If we are the last non-leader member of the thread
  175. * group, and the leader is zombie, then notify the
  176. * group leader's parent process. (if it wants notification.)
  177. */
  178. zap_leader = 0;
  179. leader = p->group_leader;
  180. if (leader != p && thread_group_empty(leader)
  181. && leader->exit_state == EXIT_ZOMBIE) {
  182. /*
  183. * If we were the last child thread and the leader has
  184. * exited already, and the leader's parent ignores SIGCHLD,
  185. * then we are the one who should release the leader.
  186. */
  187. zap_leader = do_notify_parent(leader, leader->exit_signal);
  188. if (zap_leader)
  189. leader->exit_state = EXIT_DEAD;
  190. }
  191. write_unlock_irq(&tasklist_lock);
  192. release_thread(p);
  193. call_rcu(&p->rcu, delayed_put_task_struct);
  194. p = leader;
  195. if (unlikely(zap_leader))
  196. goto repeat;
  197. }
  198. /*
  199. * This checks not only the pgrp, but falls back on the pid if no
  200. * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
  201. * without this...
  202. *
  203. * The caller must hold rcu lock or the tasklist lock.
  204. */
  205. struct pid *session_of_pgrp(struct pid *pgrp)
  206. {
  207. struct task_struct *p;
  208. struct pid *sid = NULL;
  209. p = pid_task(pgrp, PIDTYPE_PGID);
  210. if (p == NULL)
  211. p = pid_task(pgrp, PIDTYPE_PID);
  212. if (p != NULL)
  213. sid = task_session(p);
  214. return sid;
  215. }
  216. /*
  217. * Determine if a process group is "orphaned", according to the POSIX
  218. * definition in 2.2.2.52. Orphaned process groups are not to be affected
  219. * by terminal-generated stop signals. Newly orphaned process groups are
  220. * to receive a SIGHUP and a SIGCONT.
  221. *
  222. * "I ask you, have you ever known what it is to be an orphan?"
  223. */
  224. static int will_become_orphaned_pgrp(struct pid *pgrp,
  225. struct task_struct *ignored_task)
  226. {
  227. struct task_struct *p;
  228. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  229. if ((p == ignored_task) ||
  230. (p->exit_state && thread_group_empty(p)) ||
  231. is_global_init(p->real_parent))
  232. continue;
  233. if (task_pgrp(p->real_parent) != pgrp &&
  234. task_session(p->real_parent) == task_session(p))
  235. return 0;
  236. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  237. return 1;
  238. }
  239. int is_current_pgrp_orphaned(void)
  240. {
  241. int retval;
  242. read_lock(&tasklist_lock);
  243. retval = will_become_orphaned_pgrp(task_pgrp(current), NULL);
  244. read_unlock(&tasklist_lock);
  245. return retval;
  246. }
  247. static bool has_stopped_jobs(struct pid *pgrp)
  248. {
  249. struct task_struct *p;
  250. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  251. if (p->signal->flags & SIGNAL_STOP_STOPPED)
  252. return true;
  253. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  254. return false;
  255. }
  256. /*
  257. * Check to see if any process groups have become orphaned as
  258. * a result of our exiting, and if they have any stopped jobs,
  259. * send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
  260. */
  261. static void
  262. kill_orphaned_pgrp(struct task_struct *tsk, struct task_struct *parent)
  263. {
  264. struct pid *pgrp = task_pgrp(tsk);
  265. struct task_struct *ignored_task = tsk;
  266. if (!parent)
  267. /* exit: our father is in a different pgrp than
  268. * we are and we were the only connection outside.
  269. */
  270. parent = tsk->real_parent;
  271. else
  272. /* reparent: our child is in a different pgrp than
  273. * we are, and it was the only connection outside.
  274. */
  275. ignored_task = NULL;
  276. if (task_pgrp(parent) != pgrp &&
  277. task_session(parent) == task_session(tsk) &&
  278. will_become_orphaned_pgrp(pgrp, ignored_task) &&
  279. has_stopped_jobs(pgrp)) {
  280. __kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp);
  281. __kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp);
  282. }
  283. }
  284. #ifdef CONFIG_MEMCG
  285. /*
  286. * A task is exiting. If it owned this mm, find a new owner for the mm.
  287. */
  288. void mm_update_next_owner(struct mm_struct *mm)
  289. {
  290. struct task_struct *c, *g, *p = current;
  291. retry:
  292. /*
  293. * If the exiting or execing task is not the owner, it's
  294. * someone else's problem.
  295. */
  296. if (mm->owner != p)
  297. return;
  298. /*
  299. * The current owner is exiting/execing and there are no other
  300. * candidates. Do not leave the mm pointing to a possibly
  301. * freed task structure.
  302. */
  303. if (atomic_read(&mm->mm_users) <= 1) {
  304. mm->owner = NULL;
  305. return;
  306. }
  307. read_lock(&tasklist_lock);
  308. /*
  309. * Search in the children
  310. */
  311. list_for_each_entry(c, &p->children, sibling) {
  312. if (c->mm == mm)
  313. goto assign_new_owner;
  314. }
  315. /*
  316. * Search in the siblings
  317. */
  318. list_for_each_entry(c, &p->real_parent->children, sibling) {
  319. if (c->mm == mm)
  320. goto assign_new_owner;
  321. }
  322. /*
  323. * Search through everything else, we should not get here often.
  324. */
  325. for_each_process(g) {
  326. if (g->flags & PF_KTHREAD)
  327. continue;
  328. for_each_thread(g, c) {
  329. if (c->mm == mm)
  330. goto assign_new_owner;
  331. if (c->mm)
  332. break;
  333. }
  334. }
  335. read_unlock(&tasklist_lock);
  336. /*
  337. * We found no owner yet mm_users > 1: this implies that we are
  338. * most likely racing with swapoff (try_to_unuse()) or /proc or
  339. * ptrace or page migration (get_task_mm()). Mark owner as NULL.
  340. */
  341. mm->owner = NULL;
  342. return;
  343. assign_new_owner:
  344. BUG_ON(c == p);
  345. get_task_struct(c);
  346. /*
  347. * The task_lock protects c->mm from changing.
  348. * We always want mm->owner->mm == mm
  349. */
  350. task_lock(c);
  351. /*
  352. * Delay read_unlock() till we have the task_lock()
  353. * to ensure that c does not slip away underneath us
  354. */
  355. read_unlock(&tasklist_lock);
  356. if (c->mm != mm) {
  357. task_unlock(c);
  358. put_task_struct(c);
  359. goto retry;
  360. }
  361. mm->owner = c;
  362. task_unlock(c);
  363. put_task_struct(c);
  364. }
  365. #endif /* CONFIG_MEMCG */
  366. /*
  367. * Turn us into a lazy TLB process if we
  368. * aren't already..
  369. */
  370. static void exit_mm(struct task_struct *tsk)
  371. {
  372. struct mm_struct *mm = tsk->mm;
  373. struct core_state *core_state;
  374. mm_release(tsk, mm);
  375. if (!mm)
  376. return;
  377. sync_mm_rss(mm);
  378. /*
  379. * Serialize with any possible pending coredump.
  380. * We must hold mmap_sem around checking core_state
  381. * and clearing tsk->mm. The core-inducing thread
  382. * will increment ->nr_threads for each thread in the
  383. * group with ->mm != NULL.
  384. */
  385. down_read(&mm->mmap_sem);
  386. core_state = mm->core_state;
  387. if (core_state) {
  388. struct core_thread self;
  389. up_read(&mm->mmap_sem);
  390. self.task = tsk;
  391. self.next = xchg(&core_state->dumper.next, &self);
  392. /*
  393. * Implies mb(), the result of xchg() must be visible
  394. * to core_state->dumper.
  395. */
  396. if (atomic_dec_and_test(&core_state->nr_threads))
  397. complete(&core_state->startup);
  398. for (;;) {
  399. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  400. if (!self.task) /* see coredump_finish() */
  401. break;
  402. freezable_schedule();
  403. }
  404. __set_task_state(tsk, TASK_RUNNING);
  405. down_read(&mm->mmap_sem);
  406. }
  407. atomic_inc(&mm->mm_count);
  408. BUG_ON(mm != tsk->active_mm);
  409. /* more a memory barrier than a real lock */
  410. task_lock(tsk);
  411. tsk->mm = NULL;
  412. up_read(&mm->mmap_sem);
  413. enter_lazy_tlb(mm, current);
  414. task_unlock(tsk);
  415. mm_update_next_owner(mm);
  416. mmput(mm);
  417. clear_thread_flag(TIF_MEMDIE);
  418. }
  419. /*
  420. * When we die, we re-parent all our children, and try to:
  421. * 1. give them to another thread in our thread group, if such a member exists
  422. * 2. give it to the first ancestor process which prctl'd itself as a
  423. * child_subreaper for its children (like a service manager)
  424. * 3. give it to the init process (PID 1) in our pid namespace
  425. */
  426. static struct task_struct *find_new_reaper(struct task_struct *father)
  427. __releases(&tasklist_lock)
  428. __acquires(&tasklist_lock)
  429. {
  430. struct pid_namespace *pid_ns = task_active_pid_ns(father);
  431. struct task_struct *thread;
  432. thread = father;
  433. while_each_thread(father, thread) {
  434. if (thread->flags & PF_EXITING)
  435. continue;
  436. if (unlikely(pid_ns->child_reaper == father))
  437. pid_ns->child_reaper = thread;
  438. return thread;
  439. }
  440. if (unlikely(pid_ns->child_reaper == father)) {
  441. write_unlock_irq(&tasklist_lock);
  442. if (unlikely(pid_ns == &init_pid_ns)) {
  443. panic("Attempted to kill init! exitcode=0x%08x\n",
  444. father->signal->group_exit_code ?:
  445. father->exit_code);
  446. }
  447. zap_pid_ns_processes(pid_ns);
  448. write_lock_irq(&tasklist_lock);
  449. } else if (father->signal->has_child_subreaper) {
  450. struct task_struct *reaper;
  451. /*
  452. * Find the first ancestor marked as child_subreaper.
  453. * Note that the code below checks same_thread_group(reaper,
  454. * pid_ns->child_reaper). This is what we need to DTRT in a
  455. * PID namespace. However we still need the check above, see
  456. * http://marc.info/?l=linux-kernel&m=131385460420380
  457. */
  458. for (reaper = father->real_parent;
  459. reaper != &init_task;
  460. reaper = reaper->real_parent) {
  461. if (same_thread_group(reaper, pid_ns->child_reaper))
  462. break;
  463. if (!reaper->signal->is_child_subreaper)
  464. continue;
  465. thread = reaper;
  466. do {
  467. if (!(thread->flags & PF_EXITING))
  468. return reaper;
  469. } while_each_thread(reaper, thread);
  470. }
  471. }
  472. return pid_ns->child_reaper;
  473. }
  474. /*
  475. * Any that need to be release_task'd are put on the @dead list.
  476. */
  477. static void reparent_leader(struct task_struct *father, struct task_struct *p,
  478. struct list_head *dead)
  479. {
  480. list_move_tail(&p->sibling, &p->real_parent->children);
  481. if (p->exit_state == EXIT_DEAD)
  482. return;
  483. /*
  484. * If this is a threaded reparent there is no need to
  485. * notify anyone anything has happened.
  486. */
  487. if (same_thread_group(p->real_parent, father))
  488. return;
  489. /* We don't want people slaying init. */
  490. p->exit_signal = SIGCHLD;
  491. /* If it has exited notify the new parent about this child's death. */
  492. if (!p->ptrace &&
  493. p->exit_state == EXIT_ZOMBIE && thread_group_empty(p)) {
  494. if (do_notify_parent(p, p->exit_signal)) {
  495. p->exit_state = EXIT_DEAD;
  496. list_move_tail(&p->sibling, dead);
  497. }
  498. }
  499. kill_orphaned_pgrp(p, father);
  500. }
  501. static void forget_original_parent(struct task_struct *father)
  502. {
  503. struct task_struct *p, *n, *reaper;
  504. LIST_HEAD(dead_children);
  505. write_lock_irq(&tasklist_lock);
  506. /*
  507. * Note that exit_ptrace() and find_new_reaper() might
  508. * drop tasklist_lock and reacquire it.
  509. */
  510. exit_ptrace(father);
  511. reaper = find_new_reaper(father);
  512. list_for_each_entry_safe(p, n, &father->children, sibling) {
  513. struct task_struct *t = p;
  514. do {
  515. t->real_parent = reaper;
  516. if (t->parent == father) {
  517. BUG_ON(t->ptrace);
  518. t->parent = t->real_parent;
  519. }
  520. if (t->pdeath_signal)
  521. group_send_sig_info(t->pdeath_signal,
  522. SEND_SIG_NOINFO, t);
  523. } while_each_thread(p, t);
  524. reparent_leader(father, p, &dead_children);
  525. }
  526. write_unlock_irq(&tasklist_lock);
  527. BUG_ON(!list_empty(&father->children));
  528. list_for_each_entry_safe(p, n, &dead_children, sibling) {
  529. list_del_init(&p->sibling);
  530. release_task(p);
  531. }
  532. }
  533. /*
  534. * Send signals to all our closest relatives so that they know
  535. * to properly mourn us..
  536. */
  537. static void exit_notify(struct task_struct *tsk, int group_dead)
  538. {
  539. bool autoreap;
  540. /*
  541. * This does two things:
  542. *
  543. * A. Make init inherit all the child processes
  544. * B. Check to see if any process groups have become orphaned
  545. * as a result of our exiting, and if they have any stopped
  546. * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
  547. */
  548. forget_original_parent(tsk);
  549. write_lock_irq(&tasklist_lock);
  550. if (group_dead)
  551. kill_orphaned_pgrp(tsk->group_leader, NULL);
  552. if (unlikely(tsk->ptrace)) {
  553. int sig = thread_group_leader(tsk) &&
  554. thread_group_empty(tsk) &&
  555. !ptrace_reparented(tsk) ?
  556. tsk->exit_signal : SIGCHLD;
  557. autoreap = do_notify_parent(tsk, sig);
  558. } else if (thread_group_leader(tsk)) {
  559. autoreap = thread_group_empty(tsk) &&
  560. do_notify_parent(tsk, tsk->exit_signal);
  561. } else {
  562. autoreap = true;
  563. }
  564. tsk->exit_state = autoreap ? EXIT_DEAD : EXIT_ZOMBIE;
  565. /* mt-exec, de_thread() is waiting for group leader */
  566. if (unlikely(tsk->signal->notify_count < 0))
  567. wake_up_process(tsk->signal->group_exit_task);
  568. write_unlock_irq(&tasklist_lock);
  569. /* If the process is dead, release it - nobody will wait for it */
  570. if (autoreap)
  571. release_task(tsk);
  572. }
  573. #ifdef CONFIG_DEBUG_STACK_USAGE
  574. static void check_stack_usage(void)
  575. {
  576. static DEFINE_SPINLOCK(low_water_lock);
  577. static int lowest_to_date = THREAD_SIZE;
  578. unsigned long free;
  579. free = stack_not_used(current);
  580. if (free >= lowest_to_date)
  581. return;
  582. spin_lock(&low_water_lock);
  583. if (free < lowest_to_date) {
  584. pr_warn("%s (%d) used greatest stack depth: %lu bytes left\n",
  585. current->comm, task_pid_nr(current), free);
  586. lowest_to_date = free;
  587. }
  588. spin_unlock(&low_water_lock);
  589. }
  590. #else
  591. static inline void check_stack_usage(void) {}
  592. #endif
  593. void do_exit(long code)
  594. {
  595. struct task_struct *tsk = current;
  596. int group_dead;
  597. TASKS_RCU(int tasks_rcu_i);
  598. profile_task_exit(tsk);
  599. #ifdef CONFIG_MTPROF
  600. #ifdef CONFIG_MTPROF_CPUTIME
  601. /* mt shceduler profiling*/
  602. end_mtproc_info(tsk);
  603. #endif
  604. /* mt throttle monitor */
  605. end_mt_rt_mon_info(tsk);
  606. #endif
  607. WARN_ON(blk_needs_flush_plug(tsk));
  608. if (unlikely(in_interrupt()))
  609. panic("Aiee, killing interrupt handler!");
  610. if (unlikely(!tsk->pid))
  611. panic("Attempted to kill the idle task!");
  612. /*
  613. * If do_exit is called because this processes oopsed, it's possible
  614. * that get_fs() was left as KERNEL_DS, so reset it to USER_DS before
  615. * continuing. Amongst other possible reasons, this is to prevent
  616. * mm_release()->clear_child_tid() from writing to a user-controlled
  617. * kernel address.
  618. */
  619. set_fs(USER_DS);
  620. ptrace_event(PTRACE_EVENT_EXIT, code);
  621. validate_creds_for_do_exit(tsk);
  622. /*
  623. * We're taking recursive faults here in do_exit. Safest is to just
  624. * leave this task alone and wait for reboot.
  625. */
  626. if (unlikely(tsk->flags & PF_EXITING)) {
  627. pr_alert("Fixing recursive fault but reboot is needed!\n");
  628. /*
  629. * We can do this unlocked here. The futex code uses
  630. * this flag just to verify whether the pi state
  631. * cleanup has been done or not. In the worst case it
  632. * loops once more. We pretend that the cleanup was
  633. * done as there is no way to return. Either the
  634. * OWNER_DIED bit is set by now or we push the blocked
  635. * task into the wait for ever nirwana as well.
  636. */
  637. tsk->flags |= PF_EXITPIDONE;
  638. set_current_state(TASK_UNINTERRUPTIBLE);
  639. schedule();
  640. }
  641. exit_signals(tsk); /* sets PF_EXITING */
  642. /*
  643. * tsk->flags are checked in the futex code to protect against
  644. * an exiting task cleaning up the robust pi futexes.
  645. */
  646. smp_mb();
  647. raw_spin_unlock_wait(&tsk->pi_lock);
  648. if (unlikely(in_atomic()))
  649. pr_info("note: %s[%d] exited with preempt_count %d\n",
  650. current->comm, task_pid_nr(current),
  651. preempt_count());
  652. acct_update_integrals(tsk);
  653. /* sync mm's RSS info before statistics gathering */
  654. if (tsk->mm)
  655. sync_mm_rss(tsk->mm);
  656. group_dead = atomic_dec_and_test(&tsk->signal->live);
  657. if (group_dead) {
  658. hrtimer_cancel(&tsk->signal->real_timer);
  659. exit_itimers(tsk->signal);
  660. if (tsk->mm)
  661. setmax_mm_hiwater_rss(&tsk->signal->maxrss, tsk->mm);
  662. }
  663. acct_collect(code, group_dead);
  664. if (group_dead)
  665. tty_audit_exit();
  666. audit_free(tsk);
  667. tsk->exit_code = code;
  668. taskstats_exit(tsk, group_dead);
  669. exit_mm(tsk);
  670. if (group_dead)
  671. acct_process();
  672. trace_sched_process_exit(tsk);
  673. exit_sem(tsk);
  674. exit_shm(tsk);
  675. exit_files(tsk);
  676. exit_fs(tsk);
  677. if (group_dead)
  678. disassociate_ctty(1);
  679. exit_task_namespaces(tsk);
  680. exit_task_work(tsk);
  681. exit_thread();
  682. /*
  683. * Flush inherited counters to the parent - before the parent
  684. * gets woken up by child-exit notifications.
  685. *
  686. * because of cgroup mode, must be called before cgroup_exit()
  687. */
  688. perf_event_exit_task(tsk);
  689. cgroup_exit(tsk);
  690. module_put(task_thread_info(tsk)->exec_domain->module);
  691. /*
  692. * FIXME: do that only when needed, using sched_exit tracepoint
  693. */
  694. flush_ptrace_hw_breakpoint(tsk);
  695. TASKS_RCU(tasks_rcu_i = __srcu_read_lock(&tasks_rcu_exit_srcu));
  696. exit_notify(tsk, group_dead);
  697. proc_exit_connector(tsk);
  698. #ifdef CONFIG_NUMA
  699. task_lock(tsk);
  700. mpol_put(tsk->mempolicy);
  701. tsk->mempolicy = NULL;
  702. task_unlock(tsk);
  703. #endif
  704. #ifdef CONFIG_FUTEX
  705. if (unlikely(current->pi_state_cache))
  706. kfree(current->pi_state_cache);
  707. #endif
  708. /*
  709. * Make sure we are holding no locks:
  710. */
  711. debug_check_no_locks_held();
  712. /*
  713. * We can do this unlocked here. The futex code uses this flag
  714. * just to verify whether the pi state cleanup has been done
  715. * or not. In the worst case it loops once more.
  716. */
  717. tsk->flags |= PF_EXITPIDONE;
  718. if (tsk->io_context)
  719. exit_io_context(tsk);
  720. if (tsk->splice_pipe)
  721. free_pipe_info(tsk->splice_pipe);
  722. if (tsk->task_frag.page)
  723. put_page(tsk->task_frag.page);
  724. validate_creds_for_do_exit(tsk);
  725. check_stack_usage();
  726. preempt_disable();
  727. if (tsk->nr_dirtied)
  728. __this_cpu_add(dirty_throttle_leaks, tsk->nr_dirtied);
  729. exit_rcu();
  730. TASKS_RCU(__srcu_read_unlock(&tasks_rcu_exit_srcu, tasks_rcu_i));
  731. /*
  732. * The setting of TASK_RUNNING by try_to_wake_up() may be delayed
  733. * when the following two conditions become true.
  734. * - There is race condition of mmap_sem (It is acquired by
  735. * exit_mm()), and
  736. * - SMI occurs before setting TASK_RUNINNG.
  737. * (or hypervisor of virtual machine switches to other guest)
  738. * As a result, we may become TASK_RUNNING after becoming TASK_DEAD
  739. *
  740. * To avoid it, we have to wait for releasing tsk->pi_lock which
  741. * is held by try_to_wake_up()
  742. */
  743. smp_mb();
  744. raw_spin_unlock_wait(&tsk->pi_lock);
  745. /* causes final put_task_struct in finish_task_switch(). */
  746. tsk->state = TASK_DEAD;
  747. tsk->flags |= PF_NOFREEZE; /* tell freezer to ignore us */
  748. schedule();
  749. BUG();
  750. /* Avoid "noreturn function does return". */
  751. for (;;)
  752. cpu_relax(); /* For when BUG is null */
  753. }
  754. EXPORT_SYMBOL_GPL(do_exit);
  755. void complete_and_exit(struct completion *comp, long code)
  756. {
  757. if (comp)
  758. complete(comp);
  759. do_exit(code);
  760. }
  761. EXPORT_SYMBOL(complete_and_exit);
  762. SYSCALL_DEFINE1(exit, int, error_code)
  763. {
  764. do_exit((error_code&0xff)<<8);
  765. }
  766. /*
  767. * Take down every thread in the group. This is called by fatal signals
  768. * as well as by sys_exit_group (below).
  769. */
  770. void
  771. do_group_exit(int exit_code)
  772. {
  773. struct signal_struct *sig = current->signal;
  774. BUG_ON(exit_code & 0x80); /* core dumps don't get here */
  775. if (signal_group_exit(sig))
  776. exit_code = sig->group_exit_code;
  777. else if (!thread_group_empty(current)) {
  778. struct sighand_struct *const sighand = current->sighand;
  779. spin_lock_irq(&sighand->siglock);
  780. if (signal_group_exit(sig))
  781. /* Another thread got here before we took the lock. */
  782. exit_code = sig->group_exit_code;
  783. else {
  784. sig->group_exit_code = exit_code;
  785. sig->flags = SIGNAL_GROUP_EXIT;
  786. zap_other_threads(current);
  787. }
  788. spin_unlock_irq(&sighand->siglock);
  789. }
  790. do_exit(exit_code);
  791. /* NOTREACHED */
  792. }
  793. /*
  794. * this kills every thread in the thread group. Note that any externally
  795. * wait4()-ing process will get the correct exit code - even if this
  796. * thread is not the thread group leader.
  797. */
  798. SYSCALL_DEFINE1(exit_group, int, error_code)
  799. {
  800. do_group_exit((error_code & 0xff) << 8);
  801. /* NOTREACHED */
  802. return 0;
  803. }
  804. struct wait_opts {
  805. enum pid_type wo_type;
  806. int wo_flags;
  807. struct pid *wo_pid;
  808. struct siginfo __user *wo_info;
  809. int __user *wo_stat;
  810. struct rusage __user *wo_rusage;
  811. wait_queue_t child_wait;
  812. int notask_error;
  813. };
  814. static inline
  815. struct pid *task_pid_type(struct task_struct *task, enum pid_type type)
  816. {
  817. if (type != PIDTYPE_PID)
  818. task = task->group_leader;
  819. return task->pids[type].pid;
  820. }
  821. static int eligible_pid(struct wait_opts *wo, struct task_struct *p)
  822. {
  823. return wo->wo_type == PIDTYPE_MAX ||
  824. task_pid_type(p, wo->wo_type) == wo->wo_pid;
  825. }
  826. static int eligible_child(struct wait_opts *wo, struct task_struct *p)
  827. {
  828. if (!eligible_pid(wo, p))
  829. return 0;
  830. /* Wait for all children (clone and not) if __WALL is set;
  831. * otherwise, wait for clone children *only* if __WCLONE is
  832. * set; otherwise, wait for non-clone children *only*. (Note:
  833. * A "clone" child here is one that reports to its parent
  834. * using a signal other than SIGCHLD.) */
  835. if (((p->exit_signal != SIGCHLD) ^ !!(wo->wo_flags & __WCLONE))
  836. && !(wo->wo_flags & __WALL))
  837. return 0;
  838. return 1;
  839. }
  840. static int wait_noreap_copyout(struct wait_opts *wo, struct task_struct *p,
  841. pid_t pid, uid_t uid, int why, int status)
  842. {
  843. struct siginfo __user *infop;
  844. int retval = wo->wo_rusage
  845. ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
  846. put_task_struct(p);
  847. infop = wo->wo_info;
  848. if (infop) {
  849. if (!retval)
  850. retval = put_user(SIGCHLD, &infop->si_signo);
  851. if (!retval)
  852. retval = put_user(0, &infop->si_errno);
  853. if (!retval)
  854. retval = put_user((short)why, &infop->si_code);
  855. if (!retval)
  856. retval = put_user(pid, &infop->si_pid);
  857. if (!retval)
  858. retval = put_user(uid, &infop->si_uid);
  859. if (!retval)
  860. retval = put_user(status, &infop->si_status);
  861. }
  862. if (!retval)
  863. retval = pid;
  864. return retval;
  865. }
  866. /*
  867. * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
  868. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  869. * the lock and this task is uninteresting. If we return nonzero, we have
  870. * released the lock and the system call should return.
  871. */
  872. static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
  873. {
  874. unsigned long state;
  875. int retval, status, traced;
  876. pid_t pid = task_pid_vnr(p);
  877. uid_t uid = from_kuid_munged(current_user_ns(), task_uid(p));
  878. struct siginfo __user *infop;
  879. if (!likely(wo->wo_flags & WEXITED))
  880. return 0;
  881. if (unlikely(wo->wo_flags & WNOWAIT)) {
  882. int exit_code = p->exit_code;
  883. int why;
  884. get_task_struct(p);
  885. read_unlock(&tasklist_lock);
  886. if ((exit_code & 0x7f) == 0) {
  887. why = CLD_EXITED;
  888. status = exit_code >> 8;
  889. } else {
  890. why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
  891. status = exit_code & 0x7f;
  892. }
  893. return wait_noreap_copyout(wo, p, pid, uid, why, status);
  894. }
  895. traced = ptrace_reparented(p);
  896. /*
  897. * Move the task's state to DEAD/TRACE, only one thread can do this.
  898. */
  899. state = traced && thread_group_leader(p) ? EXIT_TRACE : EXIT_DEAD;
  900. if (cmpxchg(&p->exit_state, EXIT_ZOMBIE, state) != EXIT_ZOMBIE)
  901. return 0;
  902. /*
  903. * It can be ptraced but not reparented, check
  904. * thread_group_leader() to filter out sub-threads.
  905. */
  906. if (likely(!traced) && thread_group_leader(p)) {
  907. struct signal_struct *psig;
  908. struct signal_struct *sig;
  909. unsigned long maxrss;
  910. cputime_t tgutime, tgstime;
  911. /*
  912. * The resource counters for the group leader are in its
  913. * own task_struct. Those for dead threads in the group
  914. * are in its signal_struct, as are those for the child
  915. * processes it has previously reaped. All these
  916. * accumulate in the parent's signal_struct c* fields.
  917. *
  918. * We don't bother to take a lock here to protect these
  919. * p->signal fields, because they are only touched by
  920. * __exit_signal, which runs with tasklist_lock
  921. * write-locked anyway, and so is excluded here. We do
  922. * need to protect the access to parent->signal fields,
  923. * as other threads in the parent group can be right
  924. * here reaping other children at the same time.
  925. *
  926. * We use thread_group_cputime_adjusted() to get times for
  927. * the thread group, which consolidates times for all threads
  928. * in the group including the group leader.
  929. */
  930. thread_group_cputime_adjusted(p, &tgutime, &tgstime);
  931. spin_lock_irq(&p->real_parent->sighand->siglock);
  932. psig = p->real_parent->signal;
  933. sig = p->signal;
  934. write_seqlock(&psig->stats_lock);
  935. psig->cutime += tgutime + sig->cutime;
  936. psig->cstime += tgstime + sig->cstime;
  937. psig->cgtime += task_gtime(p) + sig->gtime + sig->cgtime;
  938. psig->cmin_flt +=
  939. p->min_flt + sig->min_flt + sig->cmin_flt;
  940. psig->cmaj_flt +=
  941. p->maj_flt + sig->maj_flt + sig->cmaj_flt;
  942. psig->cnvcsw +=
  943. p->nvcsw + sig->nvcsw + sig->cnvcsw;
  944. psig->cnivcsw +=
  945. p->nivcsw + sig->nivcsw + sig->cnivcsw;
  946. psig->cinblock +=
  947. task_io_get_inblock(p) +
  948. sig->inblock + sig->cinblock;
  949. psig->coublock +=
  950. task_io_get_oublock(p) +
  951. sig->oublock + sig->coublock;
  952. maxrss = max(sig->maxrss, sig->cmaxrss);
  953. if (psig->cmaxrss < maxrss)
  954. psig->cmaxrss = maxrss;
  955. task_io_accounting_add(&psig->ioac, &p->ioac);
  956. task_io_accounting_add(&psig->ioac, &sig->ioac);
  957. write_sequnlock(&psig->stats_lock);
  958. spin_unlock_irq(&p->real_parent->sighand->siglock);
  959. }
  960. /*
  961. * Now we are sure this task is interesting, and no other
  962. * thread can reap it because we its state == DEAD/TRACE.
  963. */
  964. read_unlock(&tasklist_lock);
  965. retval = wo->wo_rusage
  966. ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
  967. status = (p->signal->flags & SIGNAL_GROUP_EXIT)
  968. ? p->signal->group_exit_code : p->exit_code;
  969. if (!retval && wo->wo_stat)
  970. retval = put_user(status, wo->wo_stat);
  971. infop = wo->wo_info;
  972. if (!retval && infop)
  973. retval = put_user(SIGCHLD, &infop->si_signo);
  974. if (!retval && infop)
  975. retval = put_user(0, &infop->si_errno);
  976. if (!retval && infop) {
  977. int why;
  978. if ((status & 0x7f) == 0) {
  979. why = CLD_EXITED;
  980. status >>= 8;
  981. } else {
  982. why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
  983. status &= 0x7f;
  984. }
  985. retval = put_user((short)why, &infop->si_code);
  986. if (!retval)
  987. retval = put_user(status, &infop->si_status);
  988. }
  989. if (!retval && infop)
  990. retval = put_user(pid, &infop->si_pid);
  991. if (!retval && infop)
  992. retval = put_user(uid, &infop->si_uid);
  993. if (!retval)
  994. retval = pid;
  995. if (state == EXIT_TRACE) {
  996. write_lock_irq(&tasklist_lock);
  997. /* We dropped tasklist, ptracer could die and untrace */
  998. ptrace_unlink(p);
  999. /* If parent wants a zombie, don't release it now */
  1000. state = EXIT_ZOMBIE;
  1001. if (do_notify_parent(p, p->exit_signal))
  1002. state = EXIT_DEAD;
  1003. p->exit_state = state;
  1004. write_unlock_irq(&tasklist_lock);
  1005. }
  1006. if (state == EXIT_DEAD)
  1007. release_task(p);
  1008. return retval;
  1009. }
  1010. static int *task_stopped_code(struct task_struct *p, bool ptrace)
  1011. {
  1012. if (ptrace) {
  1013. if (task_is_stopped_or_traced(p) &&
  1014. !(p->jobctl & JOBCTL_LISTENING))
  1015. return &p->exit_code;
  1016. } else {
  1017. if (p->signal->flags & SIGNAL_STOP_STOPPED)
  1018. return &p->signal->group_exit_code;
  1019. }
  1020. return NULL;
  1021. }
  1022. /**
  1023. * wait_task_stopped - Wait for %TASK_STOPPED or %TASK_TRACED
  1024. * @wo: wait options
  1025. * @ptrace: is the wait for ptrace
  1026. * @p: task to wait for
  1027. *
  1028. * Handle sys_wait4() work for %p in state %TASK_STOPPED or %TASK_TRACED.
  1029. *
  1030. * CONTEXT:
  1031. * read_lock(&tasklist_lock), which is released if return value is
  1032. * non-zero. Also, grabs and releases @p->sighand->siglock.
  1033. *
  1034. * RETURNS:
  1035. * 0 if wait condition didn't exist and search for other wait conditions
  1036. * should continue. Non-zero return, -errno on failure and @p's pid on
  1037. * success, implies that tasklist_lock is released and wait condition
  1038. * search should terminate.
  1039. */
  1040. static int wait_task_stopped(struct wait_opts *wo,
  1041. int ptrace, struct task_struct *p)
  1042. {
  1043. struct siginfo __user *infop;
  1044. int retval, exit_code, *p_code, why;
  1045. uid_t uid = 0; /* unneeded, required by compiler */
  1046. pid_t pid;
  1047. /*
  1048. * Traditionally we see ptrace'd stopped tasks regardless of options.
  1049. */
  1050. if (!ptrace && !(wo->wo_flags & WUNTRACED))
  1051. return 0;
  1052. if (!task_stopped_code(p, ptrace))
  1053. return 0;
  1054. exit_code = 0;
  1055. spin_lock_irq(&p->sighand->siglock);
  1056. p_code = task_stopped_code(p, ptrace);
  1057. if (unlikely(!p_code))
  1058. goto unlock_sig;
  1059. exit_code = *p_code;
  1060. if (!exit_code)
  1061. goto unlock_sig;
  1062. if (!unlikely(wo->wo_flags & WNOWAIT))
  1063. *p_code = 0;
  1064. uid = from_kuid_munged(current_user_ns(), task_uid(p));
  1065. unlock_sig:
  1066. spin_unlock_irq(&p->sighand->siglock);
  1067. if (!exit_code)
  1068. return 0;
  1069. /*
  1070. * Now we are pretty sure this task is interesting.
  1071. * Make sure it doesn't get reaped out from under us while we
  1072. * give up the lock and then examine it below. We don't want to
  1073. * keep holding onto the tasklist_lock while we call getrusage and
  1074. * possibly take page faults for user memory.
  1075. */
  1076. get_task_struct(p);
  1077. pid = task_pid_vnr(p);
  1078. why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
  1079. read_unlock(&tasklist_lock);
  1080. if (unlikely(wo->wo_flags & WNOWAIT))
  1081. return wait_noreap_copyout(wo, p, pid, uid, why, exit_code);
  1082. retval = wo->wo_rusage
  1083. ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
  1084. if (!retval && wo->wo_stat)
  1085. retval = put_user((exit_code << 8) | 0x7f, wo->wo_stat);
  1086. infop = wo->wo_info;
  1087. if (!retval && infop)
  1088. retval = put_user(SIGCHLD, &infop->si_signo);
  1089. if (!retval && infop)
  1090. retval = put_user(0, &infop->si_errno);
  1091. if (!retval && infop)
  1092. retval = put_user((short)why, &infop->si_code);
  1093. if (!retval && infop)
  1094. retval = put_user(exit_code, &infop->si_status);
  1095. if (!retval && infop)
  1096. retval = put_user(pid, &infop->si_pid);
  1097. if (!retval && infop)
  1098. retval = put_user(uid, &infop->si_uid);
  1099. if (!retval)
  1100. retval = pid;
  1101. put_task_struct(p);
  1102. BUG_ON(!retval);
  1103. return retval;
  1104. }
  1105. /*
  1106. * Handle do_wait work for one task in a live, non-stopped state.
  1107. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  1108. * the lock and this task is uninteresting. If we return nonzero, we have
  1109. * released the lock and the system call should return.
  1110. */
  1111. static int wait_task_continued(struct wait_opts *wo, struct task_struct *p)
  1112. {
  1113. int retval;
  1114. pid_t pid;
  1115. uid_t uid;
  1116. if (!unlikely(wo->wo_flags & WCONTINUED))
  1117. return 0;
  1118. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
  1119. return 0;
  1120. spin_lock_irq(&p->sighand->siglock);
  1121. /* Re-check with the lock held. */
  1122. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
  1123. spin_unlock_irq(&p->sighand->siglock);
  1124. return 0;
  1125. }
  1126. if (!unlikely(wo->wo_flags & WNOWAIT))
  1127. p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
  1128. uid = from_kuid_munged(current_user_ns(), task_uid(p));
  1129. spin_unlock_irq(&p->sighand->siglock);
  1130. pid = task_pid_vnr(p);
  1131. get_task_struct(p);
  1132. read_unlock(&tasklist_lock);
  1133. if (!wo->wo_info) {
  1134. retval = wo->wo_rusage
  1135. ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
  1136. put_task_struct(p);
  1137. if (!retval && wo->wo_stat)
  1138. retval = put_user(0xffff, wo->wo_stat);
  1139. if (!retval)
  1140. retval = pid;
  1141. } else {
  1142. retval = wait_noreap_copyout(wo, p, pid, uid,
  1143. CLD_CONTINUED, SIGCONT);
  1144. BUG_ON(retval == 0);
  1145. }
  1146. return retval;
  1147. }
  1148. /*
  1149. * Consider @p for a wait by @parent.
  1150. *
  1151. * -ECHILD should be in ->notask_error before the first call.
  1152. * Returns nonzero for a final return, when we have unlocked tasklist_lock.
  1153. * Returns zero if the search for a child should continue;
  1154. * then ->notask_error is 0 if @p is an eligible child,
  1155. * or another error from security_task_wait(), or still -ECHILD.
  1156. */
  1157. static int wait_consider_task(struct wait_opts *wo, int ptrace,
  1158. struct task_struct *p)
  1159. {
  1160. /*
  1161. * We can race with wait_task_zombie() from another thread.
  1162. * Ensure that EXIT_ZOMBIE -> EXIT_DEAD/EXIT_TRACE transition
  1163. * can't confuse the checks below.
  1164. */
  1165. int exit_state = ACCESS_ONCE(p->exit_state);
  1166. int ret;
  1167. if (unlikely(exit_state == EXIT_DEAD))
  1168. return 0;
  1169. ret = eligible_child(wo, p);
  1170. if (!ret)
  1171. return ret;
  1172. ret = security_task_wait(p);
  1173. if (unlikely(ret < 0)) {
  1174. /*
  1175. * If we have not yet seen any eligible child,
  1176. * then let this error code replace -ECHILD.
  1177. * A permission error will give the user a clue
  1178. * to look for security policy problems, rather
  1179. * than for mysterious wait bugs.
  1180. */
  1181. if (wo->notask_error)
  1182. wo->notask_error = ret;
  1183. return 0;
  1184. }
  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 another error from security_task_wait(), 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_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 critiera 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. SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
  1358. infop, int, options, struct rusage __user *, 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. return -EINVAL;
  1366. if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
  1367. return -EINVAL;
  1368. switch (which) {
  1369. case P_ALL:
  1370. type = PIDTYPE_MAX;
  1371. break;
  1372. case P_PID:
  1373. type = PIDTYPE_PID;
  1374. if (upid <= 0)
  1375. return -EINVAL;
  1376. break;
  1377. case P_PGID:
  1378. type = PIDTYPE_PGID;
  1379. if (upid <= 0)
  1380. return -EINVAL;
  1381. break;
  1382. default:
  1383. return -EINVAL;
  1384. }
  1385. if (type < PIDTYPE_MAX)
  1386. pid = find_get_pid(upid);
  1387. wo.wo_type = type;
  1388. wo.wo_pid = pid;
  1389. wo.wo_flags = options;
  1390. wo.wo_info = infop;
  1391. wo.wo_stat = NULL;
  1392. wo.wo_rusage = ru;
  1393. ret = do_wait(&wo);
  1394. if (ret > 0) {
  1395. ret = 0;
  1396. } else if (infop) {
  1397. /*
  1398. * For a WNOHANG return, clear out all the fields
  1399. * we would set so the user can easily tell the
  1400. * difference.
  1401. */
  1402. if (!ret)
  1403. ret = put_user(0, &infop->si_signo);
  1404. if (!ret)
  1405. ret = put_user(0, &infop->si_errno);
  1406. if (!ret)
  1407. ret = put_user(0, &infop->si_code);
  1408. if (!ret)
  1409. ret = put_user(0, &infop->si_pid);
  1410. if (!ret)
  1411. ret = put_user(0, &infop->si_uid);
  1412. if (!ret)
  1413. ret = put_user(0, &infop->si_status);
  1414. }
  1415. put_pid(pid);
  1416. return ret;
  1417. }
  1418. SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
  1419. int, options, struct rusage __user *, ru)
  1420. {
  1421. struct wait_opts wo;
  1422. struct pid *pid = NULL;
  1423. enum pid_type type;
  1424. long ret;
  1425. if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
  1426. __WNOTHREAD|__WCLONE|__WALL))
  1427. return -EINVAL;
  1428. if (upid == -1)
  1429. type = PIDTYPE_MAX;
  1430. else if (upid < 0) {
  1431. type = PIDTYPE_PGID;
  1432. pid = find_get_pid(-upid);
  1433. } else if (upid == 0) {
  1434. type = PIDTYPE_PGID;
  1435. pid = get_task_pid(current, PIDTYPE_PGID);
  1436. } else /* upid > 0 */ {
  1437. type = PIDTYPE_PID;
  1438. pid = find_get_pid(upid);
  1439. }
  1440. wo.wo_type = type;
  1441. wo.wo_pid = pid;
  1442. wo.wo_flags = options | WEXITED;
  1443. wo.wo_info = NULL;
  1444. wo.wo_stat = stat_addr;
  1445. wo.wo_rusage = ru;
  1446. ret = do_wait(&wo);
  1447. put_pid(pid);
  1448. return ret;
  1449. }
  1450. #ifdef __ARCH_WANT_SYS_WAITPID
  1451. /*
  1452. * sys_waitpid() remains for compatibility. waitpid() should be
  1453. * implemented by calling sys_wait4() from libc.a.
  1454. */
  1455. SYSCALL_DEFINE3(waitpid, pid_t, pid, int __user *, stat_addr, int, options)
  1456. {
  1457. return sys_wait4(pid, stat_addr, options, NULL);
  1458. }
  1459. #endif