exit.c 44 KB

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