exit.c 44 KB

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