exit.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/kernel/exit.c
  4. *
  5. * Copyright (C) 1991, 1992 Linus Torvalds
  6. */
  7. #include <linux/mm.h>
  8. #include <linux/slab.h>
  9. #include <linux/sched/autogroup.h>
  10. #include <linux/sched/mm.h>
  11. #include <linux/sched/stat.h>
  12. #include <linux/sched/task.h>
  13. #include <linux/sched/task_stack.h>
  14. #include <linux/sched/cputime.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/module.h>
  17. #include <linux/capability.h>
  18. #include <linux/completion.h>
  19. #include <linux/personality.h>
  20. #include <linux/tty.h>
  21. #include <linux/iocontext.h>
  22. #include <linux/key.h>
  23. #include <linux/cpu.h>
  24. #include <linux/acct.h>
  25. #include <linux/tsacct_kern.h>
  26. #include <linux/file.h>
  27. #include <linux/fdtable.h>
  28. #include <linux/freezer.h>
  29. #include <linux/binfmts.h>
  30. #include <linux/nsproxy.h>
  31. #include <linux/pid_namespace.h>
  32. #include <linux/ptrace.h>
  33. #include <linux/profile.h>
  34. #include <linux/mount.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/kthread.h>
  37. #include <linux/mempolicy.h>
  38. #include <linux/taskstats_kern.h>
  39. #include <linux/delayacct.h>
  40. #include <linux/cgroup.h>
  41. #include <linux/syscalls.h>
  42. #include <linux/signal.h>
  43. #include <linux/posix-timers.h>
  44. #include <linux/cn_proc.h>
  45. #include <linux/mutex.h>
  46. #include <linux/futex.h>
  47. #include <linux/pipe_fs_i.h>
  48. #include <linux/audit.h> /* for audit_free() */
  49. #include <linux/resource.h>
  50. #include <linux/task_io_accounting_ops.h>
  51. #include <linux/blkdev.h>
  52. #include <linux/task_work.h>
  53. #include <linux/fs_struct.h>
  54. #include <linux/init_task.h>
  55. #include <linux/perf_event.h>
  56. #include <trace/events/sched.h>
  57. #include <linux/hw_breakpoint.h>
  58. #include <linux/oom.h>
  59. #include <linux/writeback.h>
  60. #include <linux/shm.h>
  61. #include <linux/kcov.h>
  62. #include <linux/kmsan.h>
  63. #include <linux/random.h>
  64. #include <linux/rcuwait.h>
  65. #include <linux/compat.h>
  66. #include <linux/io_uring.h>
  67. #include <linux/kprobes.h>
  68. #include <linux/rethook.h>
  69. #include <linux/sysfs.h>
  70. #include <linux/user_events.h>
  71. #include <linux/uaccess.h>
  72. #include <uapi/linux/wait.h>
  73. #include <asm/unistd.h>
  74. #include <asm/mmu_context.h>
  75. #include "exit.h"
  76. /*
  77. * The default value should be high enough to not crash a system that randomly
  78. * crashes its kernel from time to time, but low enough to at least not permit
  79. * overflowing 32-bit refcounts or the ldsem writer count.
  80. */
  81. static unsigned int oops_limit = 10000;
  82. #ifdef CONFIG_SYSCTL
  83. static struct ctl_table kern_exit_table[] = {
  84. {
  85. .procname = "oops_limit",
  86. .data = &oops_limit,
  87. .maxlen = sizeof(oops_limit),
  88. .mode = 0644,
  89. .proc_handler = proc_douintvec,
  90. },
  91. };
  92. static __init int kernel_exit_sysctls_init(void)
  93. {
  94. register_sysctl_init("kernel", kern_exit_table);
  95. return 0;
  96. }
  97. late_initcall(kernel_exit_sysctls_init);
  98. #endif
  99. static atomic_t oops_count = ATOMIC_INIT(0);
  100. #ifdef CONFIG_SYSFS
  101. static ssize_t oops_count_show(struct kobject *kobj, struct kobj_attribute *attr,
  102. char *page)
  103. {
  104. return sysfs_emit(page, "%d\n", atomic_read(&oops_count));
  105. }
  106. static struct kobj_attribute oops_count_attr = __ATTR_RO(oops_count);
  107. static __init int kernel_exit_sysfs_init(void)
  108. {
  109. sysfs_add_file_to_group(kernel_kobj, &oops_count_attr.attr, NULL);
  110. return 0;
  111. }
  112. late_initcall(kernel_exit_sysfs_init);
  113. #endif
  114. static void __unhash_process(struct task_struct *p, bool group_dead)
  115. {
  116. nr_threads--;
  117. detach_pid(p, PIDTYPE_PID);
  118. if (group_dead) {
  119. detach_pid(p, PIDTYPE_TGID);
  120. detach_pid(p, PIDTYPE_PGID);
  121. detach_pid(p, PIDTYPE_SID);
  122. list_del_rcu(&p->tasks);
  123. list_del_init(&p->sibling);
  124. __this_cpu_dec(process_counts);
  125. }
  126. list_del_rcu(&p->thread_node);
  127. }
  128. /*
  129. * This function expects the tasklist_lock write-locked.
  130. */
  131. static void __exit_signal(struct task_struct *tsk)
  132. {
  133. struct signal_struct *sig = tsk->signal;
  134. bool group_dead = thread_group_leader(tsk);
  135. struct sighand_struct *sighand;
  136. struct tty_struct *tty;
  137. u64 utime, stime;
  138. sighand = rcu_dereference_check(tsk->sighand,
  139. lockdep_tasklist_lock_is_held());
  140. spin_lock(&sighand->siglock);
  141. #ifdef CONFIG_POSIX_TIMERS
  142. posix_cpu_timers_exit(tsk);
  143. if (group_dead)
  144. posix_cpu_timers_exit_group(tsk);
  145. #endif
  146. if (group_dead) {
  147. tty = sig->tty;
  148. sig->tty = NULL;
  149. } else {
  150. /*
  151. * If there is any task waiting for the group exit
  152. * then notify it:
  153. */
  154. if (sig->notify_count > 0 && !--sig->notify_count)
  155. wake_up_process(sig->group_exec_task);
  156. if (tsk == sig->curr_target)
  157. sig->curr_target = next_thread(tsk);
  158. }
  159. add_device_randomness((const void*) &tsk->se.sum_exec_runtime,
  160. sizeof(unsigned long long));
  161. /*
  162. * Accumulate here the counters for all threads as they die. We could
  163. * skip the group leader because it is the last user of signal_struct,
  164. * but we want to avoid the race with thread_group_cputime() which can
  165. * see the empty ->thread_head list.
  166. */
  167. task_cputime(tsk, &utime, &stime);
  168. write_seqlock(&sig->stats_lock);
  169. sig->utime += utime;
  170. sig->stime += stime;
  171. sig->gtime += task_gtime(tsk);
  172. sig->min_flt += tsk->min_flt;
  173. sig->maj_flt += tsk->maj_flt;
  174. sig->nvcsw += tsk->nvcsw;
  175. sig->nivcsw += tsk->nivcsw;
  176. sig->inblock += task_io_get_inblock(tsk);
  177. sig->oublock += task_io_get_oublock(tsk);
  178. task_io_accounting_add(&sig->ioac, &tsk->ioac);
  179. sig->sum_sched_runtime += tsk->se.sum_exec_runtime;
  180. sig->nr_threads--;
  181. __unhash_process(tsk, group_dead);
  182. write_sequnlock(&sig->stats_lock);
  183. /*
  184. * Do this under ->siglock, we can race with another thread
  185. * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.
  186. */
  187. flush_sigqueue(&tsk->pending);
  188. tsk->sighand = NULL;
  189. spin_unlock(&sighand->siglock);
  190. __cleanup_sighand(sighand);
  191. clear_tsk_thread_flag(tsk, TIF_SIGPENDING);
  192. if (group_dead) {
  193. flush_sigqueue(&sig->shared_pending);
  194. tty_kref_put(tty);
  195. }
  196. }
  197. static void delayed_put_task_struct(struct rcu_head *rhp)
  198. {
  199. struct task_struct *tsk = container_of(rhp, struct task_struct, rcu);
  200. kprobe_flush_task(tsk);
  201. rethook_flush_task(tsk);
  202. perf_event_delayed_put(tsk);
  203. trace_sched_process_free(tsk);
  204. put_task_struct(tsk);
  205. }
  206. void put_task_struct_rcu_user(struct task_struct *task)
  207. {
  208. if (refcount_dec_and_test(&task->rcu_users))
  209. call_rcu(&task->rcu, delayed_put_task_struct);
  210. }
  211. void __weak release_thread(struct task_struct *dead_task)
  212. {
  213. }
  214. void release_task(struct task_struct *p)
  215. {
  216. struct task_struct *leader;
  217. struct pid *thread_pid;
  218. int zap_leader;
  219. repeat:
  220. /* don't need to get the RCU readlock here - the process is dead and
  221. * can't be modifying its own credentials. But shut RCU-lockdep up */
  222. rcu_read_lock();
  223. dec_rlimit_ucounts(task_ucounts(p), UCOUNT_RLIMIT_NPROC, 1);
  224. rcu_read_unlock();
  225. cgroup_release(p);
  226. write_lock_irq(&tasklist_lock);
  227. ptrace_release_task(p);
  228. thread_pid = get_pid(p->thread_pid);
  229. __exit_signal(p);
  230. /*
  231. * If we are the last non-leader member of the thread
  232. * group, and the leader is zombie, then notify the
  233. * group leader's parent process. (if it wants notification.)
  234. */
  235. zap_leader = 0;
  236. leader = p->group_leader;
  237. if (leader != p && thread_group_empty(leader)
  238. && leader->exit_state == EXIT_ZOMBIE) {
  239. /*
  240. * If we were the last child thread and the leader has
  241. * exited already, and the leader's parent ignores SIGCHLD,
  242. * then we are the one who should release the leader.
  243. */
  244. zap_leader = do_notify_parent(leader, leader->exit_signal);
  245. if (zap_leader)
  246. leader->exit_state = EXIT_DEAD;
  247. }
  248. write_unlock_irq(&tasklist_lock);
  249. proc_flush_pid(thread_pid);
  250. put_pid(thread_pid);
  251. release_thread(p);
  252. put_task_struct_rcu_user(p);
  253. p = leader;
  254. if (unlikely(zap_leader))
  255. goto repeat;
  256. }
  257. int rcuwait_wake_up(struct rcuwait *w)
  258. {
  259. int ret = 0;
  260. struct task_struct *task;
  261. rcu_read_lock();
  262. /*
  263. * Order condition vs @task, such that everything prior to the load
  264. * of @task is visible. This is the condition as to why the user called
  265. * rcuwait_wake() in the first place. Pairs with set_current_state()
  266. * barrier (A) in rcuwait_wait_event().
  267. *
  268. * WAIT WAKE
  269. * [S] tsk = current [S] cond = true
  270. * MB (A) MB (B)
  271. * [L] cond [L] tsk
  272. */
  273. smp_mb(); /* (B) */
  274. task = rcu_dereference(w->task);
  275. if (task)
  276. ret = wake_up_process(task);
  277. rcu_read_unlock();
  278. return ret;
  279. }
  280. EXPORT_SYMBOL_GPL(rcuwait_wake_up);
  281. /*
  282. * Determine if a process group is "orphaned", according to the POSIX
  283. * definition in 2.2.2.52. Orphaned process groups are not to be affected
  284. * by terminal-generated stop signals. Newly orphaned process groups are
  285. * to receive a SIGHUP and a SIGCONT.
  286. *
  287. * "I ask you, have you ever known what it is to be an orphan?"
  288. */
  289. static int will_become_orphaned_pgrp(struct pid *pgrp,
  290. struct task_struct *ignored_task)
  291. {
  292. struct task_struct *p;
  293. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  294. if ((p == ignored_task) ||
  295. (p->exit_state && thread_group_empty(p)) ||
  296. is_global_init(p->real_parent))
  297. continue;
  298. if (task_pgrp(p->real_parent) != pgrp &&
  299. task_session(p->real_parent) == task_session(p))
  300. return 0;
  301. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  302. return 1;
  303. }
  304. int is_current_pgrp_orphaned(void)
  305. {
  306. int retval;
  307. read_lock(&tasklist_lock);
  308. retval = will_become_orphaned_pgrp(task_pgrp(current), NULL);
  309. read_unlock(&tasklist_lock);
  310. return retval;
  311. }
  312. static bool has_stopped_jobs(struct pid *pgrp)
  313. {
  314. struct task_struct *p;
  315. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  316. if (p->signal->flags & SIGNAL_STOP_STOPPED)
  317. return true;
  318. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  319. return false;
  320. }
  321. /*
  322. * Check to see if any process groups have become orphaned as
  323. * a result of our exiting, and if they have any stopped jobs,
  324. * send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
  325. */
  326. static void
  327. kill_orphaned_pgrp(struct task_struct *tsk, struct task_struct *parent)
  328. {
  329. struct pid *pgrp = task_pgrp(tsk);
  330. struct task_struct *ignored_task = tsk;
  331. if (!parent)
  332. /* exit: our father is in a different pgrp than
  333. * we are and we were the only connection outside.
  334. */
  335. parent = tsk->real_parent;
  336. else
  337. /* reparent: our child is in a different pgrp than
  338. * we are, and it was the only connection outside.
  339. */
  340. ignored_task = NULL;
  341. if (task_pgrp(parent) != pgrp &&
  342. task_session(parent) == task_session(tsk) &&
  343. will_become_orphaned_pgrp(pgrp, ignored_task) &&
  344. has_stopped_jobs(pgrp)) {
  345. __kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp);
  346. __kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp);
  347. }
  348. }
  349. static void coredump_task_exit(struct task_struct *tsk)
  350. {
  351. struct core_state *core_state;
  352. /*
  353. * Serialize with any possible pending coredump.
  354. * We must hold siglock around checking core_state
  355. * and setting PF_POSTCOREDUMP. The core-inducing thread
  356. * will increment ->nr_threads for each thread in the
  357. * group without PF_POSTCOREDUMP set.
  358. */
  359. spin_lock_irq(&tsk->sighand->siglock);
  360. tsk->flags |= PF_POSTCOREDUMP;
  361. core_state = tsk->signal->core_state;
  362. spin_unlock_irq(&tsk->sighand->siglock);
  363. if (core_state) {
  364. struct core_thread self;
  365. self.task = current;
  366. if (self.task->flags & PF_SIGNALED)
  367. self.next = xchg(&core_state->dumper.next, &self);
  368. else
  369. self.task = NULL;
  370. /*
  371. * Implies mb(), the result of xchg() must be visible
  372. * to core_state->dumper.
  373. */
  374. if (atomic_dec_and_test(&core_state->nr_threads))
  375. complete(&core_state->startup);
  376. for (;;) {
  377. set_current_state(TASK_IDLE|TASK_FREEZABLE);
  378. if (!self.task) /* see coredump_finish() */
  379. break;
  380. schedule();
  381. }
  382. __set_current_state(TASK_RUNNING);
  383. }
  384. }
  385. #ifdef CONFIG_MEMCG
  386. /* drops tasklist_lock if succeeds */
  387. static bool __try_to_set_owner(struct task_struct *tsk, struct mm_struct *mm)
  388. {
  389. bool ret = false;
  390. task_lock(tsk);
  391. if (likely(tsk->mm == mm)) {
  392. /* tsk can't pass exit_mm/exec_mmap and exit */
  393. read_unlock(&tasklist_lock);
  394. WRITE_ONCE(mm->owner, tsk);
  395. lru_gen_migrate_mm(mm);
  396. ret = true;
  397. }
  398. task_unlock(tsk);
  399. return ret;
  400. }
  401. static bool try_to_set_owner(struct task_struct *g, struct mm_struct *mm)
  402. {
  403. struct task_struct *t;
  404. for_each_thread(g, t) {
  405. struct mm_struct *t_mm = READ_ONCE(t->mm);
  406. if (t_mm == mm) {
  407. if (__try_to_set_owner(t, mm))
  408. return true;
  409. } else if (t_mm)
  410. break;
  411. }
  412. return false;
  413. }
  414. /*
  415. * A task is exiting. If it owned this mm, find a new owner for the mm.
  416. */
  417. void mm_update_next_owner(struct mm_struct *mm)
  418. {
  419. struct task_struct *g, *p = current;
  420. /*
  421. * If the exiting or execing task is not the owner, it's
  422. * someone else's problem.
  423. */
  424. if (mm->owner != p)
  425. return;
  426. /*
  427. * The current owner is exiting/execing and there are no other
  428. * candidates. Do not leave the mm pointing to a possibly
  429. * freed task structure.
  430. */
  431. if (atomic_read(&mm->mm_users) <= 1) {
  432. WRITE_ONCE(mm->owner, NULL);
  433. return;
  434. }
  435. read_lock(&tasklist_lock);
  436. /*
  437. * Search in the children
  438. */
  439. list_for_each_entry(g, &p->children, sibling) {
  440. if (try_to_set_owner(g, mm))
  441. goto ret;
  442. }
  443. /*
  444. * Search in the siblings
  445. */
  446. list_for_each_entry(g, &p->real_parent->children, sibling) {
  447. if (try_to_set_owner(g, mm))
  448. goto ret;
  449. }
  450. /*
  451. * Search through everything else, we should not get here often.
  452. */
  453. for_each_process(g) {
  454. if (atomic_read(&mm->mm_users) <= 1)
  455. break;
  456. if (g->flags & PF_KTHREAD)
  457. continue;
  458. if (try_to_set_owner(g, mm))
  459. goto ret;
  460. }
  461. read_unlock(&tasklist_lock);
  462. /*
  463. * We found no owner yet mm_users > 1: this implies that we are
  464. * most likely racing with swapoff (try_to_unuse()) or /proc or
  465. * ptrace or page migration (get_task_mm()). Mark owner as NULL.
  466. */
  467. WRITE_ONCE(mm->owner, NULL);
  468. ret:
  469. return;
  470. }
  471. #endif /* CONFIG_MEMCG */
  472. /*
  473. * Turn us into a lazy TLB process if we
  474. * aren't already..
  475. */
  476. static void exit_mm(void)
  477. {
  478. struct mm_struct *mm = current->mm;
  479. exit_mm_release(current, mm);
  480. if (!mm)
  481. return;
  482. mmap_read_lock(mm);
  483. mmgrab_lazy_tlb(mm);
  484. BUG_ON(mm != current->active_mm);
  485. /* more a memory barrier than a real lock */
  486. task_lock(current);
  487. /*
  488. * When a thread stops operating on an address space, the loop
  489. * in membarrier_private_expedited() may not observe that
  490. * tsk->mm, and the loop in membarrier_global_expedited() may
  491. * not observe a MEMBARRIER_STATE_GLOBAL_EXPEDITED
  492. * rq->membarrier_state, so those would not issue an IPI.
  493. * Membarrier requires a memory barrier after accessing
  494. * user-space memory, before clearing tsk->mm or the
  495. * rq->membarrier_state.
  496. */
  497. smp_mb__after_spinlock();
  498. local_irq_disable();
  499. current->mm = NULL;
  500. membarrier_update_current_mm(NULL);
  501. enter_lazy_tlb(mm, current);
  502. local_irq_enable();
  503. task_unlock(current);
  504. mmap_read_unlock(mm);
  505. mm_update_next_owner(mm);
  506. mmput(mm);
  507. if (test_thread_flag(TIF_MEMDIE))
  508. exit_oom_victim();
  509. }
  510. static struct task_struct *find_alive_thread(struct task_struct *p)
  511. {
  512. struct task_struct *t;
  513. for_each_thread(p, t) {
  514. if (!(t->flags & PF_EXITING))
  515. return t;
  516. }
  517. return NULL;
  518. }
  519. static struct task_struct *find_child_reaper(struct task_struct *father,
  520. struct list_head *dead)
  521. __releases(&tasklist_lock)
  522. __acquires(&tasklist_lock)
  523. {
  524. struct pid_namespace *pid_ns = task_active_pid_ns(father);
  525. struct task_struct *reaper = pid_ns->child_reaper;
  526. struct task_struct *p, *n;
  527. if (likely(reaper != father))
  528. return reaper;
  529. reaper = find_alive_thread(father);
  530. if (reaper) {
  531. pid_ns->child_reaper = reaper;
  532. return reaper;
  533. }
  534. write_unlock_irq(&tasklist_lock);
  535. list_for_each_entry_safe(p, n, dead, ptrace_entry) {
  536. list_del_init(&p->ptrace_entry);
  537. release_task(p);
  538. }
  539. zap_pid_ns_processes(pid_ns);
  540. write_lock_irq(&tasklist_lock);
  541. return father;
  542. }
  543. /*
  544. * When we die, we re-parent all our children, and try to:
  545. * 1. give them to another thread in our thread group, if such a member exists
  546. * 2. give it to the first ancestor process which prctl'd itself as a
  547. * child_subreaper for its children (like a service manager)
  548. * 3. give it to the init process (PID 1) in our pid namespace
  549. */
  550. static struct task_struct *find_new_reaper(struct task_struct *father,
  551. struct task_struct *child_reaper)
  552. {
  553. struct task_struct *thread, *reaper;
  554. thread = find_alive_thread(father);
  555. if (thread)
  556. return thread;
  557. if (father->signal->has_child_subreaper) {
  558. unsigned int ns_level = task_pid(father)->level;
  559. /*
  560. * Find the first ->is_child_subreaper ancestor in our pid_ns.
  561. * We can't check reaper != child_reaper to ensure we do not
  562. * cross the namespaces, the exiting parent could be injected
  563. * by setns() + fork().
  564. * We check pid->level, this is slightly more efficient than
  565. * task_active_pid_ns(reaper) != task_active_pid_ns(father).
  566. */
  567. for (reaper = father->real_parent;
  568. task_pid(reaper)->level == ns_level;
  569. reaper = reaper->real_parent) {
  570. if (reaper == &init_task)
  571. break;
  572. if (!reaper->signal->is_child_subreaper)
  573. continue;
  574. thread = find_alive_thread(reaper);
  575. if (thread)
  576. return thread;
  577. }
  578. }
  579. return child_reaper;
  580. }
  581. /*
  582. * Any that need to be release_task'd are put on the @dead list.
  583. */
  584. static void reparent_leader(struct task_struct *father, struct task_struct *p,
  585. struct list_head *dead)
  586. {
  587. if (unlikely(p->exit_state == EXIT_DEAD))
  588. return;
  589. /* We don't want people slaying init. */
  590. p->exit_signal = SIGCHLD;
  591. /* If it has exited notify the new parent about this child's death. */
  592. if (!p->ptrace &&
  593. p->exit_state == EXIT_ZOMBIE && thread_group_empty(p)) {
  594. if (do_notify_parent(p, p->exit_signal)) {
  595. p->exit_state = EXIT_DEAD;
  596. list_add(&p->ptrace_entry, dead);
  597. }
  598. }
  599. kill_orphaned_pgrp(p, father);
  600. }
  601. /*
  602. * This does two things:
  603. *
  604. * A. Make init inherit all the child processes
  605. * B. Check to see if any process groups have become orphaned
  606. * as a result of our exiting, and if they have any stopped
  607. * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
  608. */
  609. static void forget_original_parent(struct task_struct *father,
  610. struct list_head *dead)
  611. {
  612. struct task_struct *p, *t, *reaper;
  613. if (unlikely(!list_empty(&father->ptraced)))
  614. exit_ptrace(father, dead);
  615. /* Can drop and reacquire tasklist_lock */
  616. reaper = find_child_reaper(father, dead);
  617. if (list_empty(&father->children))
  618. return;
  619. reaper = find_new_reaper(father, reaper);
  620. list_for_each_entry(p, &father->children, sibling) {
  621. for_each_thread(p, t) {
  622. RCU_INIT_POINTER(t->real_parent, reaper);
  623. BUG_ON((!t->ptrace) != (rcu_access_pointer(t->parent) == father));
  624. if (likely(!t->ptrace))
  625. t->parent = t->real_parent;
  626. if (t->pdeath_signal)
  627. group_send_sig_info(t->pdeath_signal,
  628. SEND_SIG_NOINFO, t,
  629. PIDTYPE_TGID);
  630. }
  631. /*
  632. * If this is a threaded reparent there is no need to
  633. * notify anyone anything has happened.
  634. */
  635. if (!same_thread_group(reaper, father))
  636. reparent_leader(father, p, dead);
  637. }
  638. list_splice_tail_init(&father->children, &reaper->children);
  639. }
  640. /*
  641. * Send signals to all our closest relatives so that they know
  642. * to properly mourn us..
  643. */
  644. static void exit_notify(struct task_struct *tsk, int group_dead)
  645. {
  646. bool autoreap;
  647. struct task_struct *p, *n;
  648. LIST_HEAD(dead);
  649. write_lock_irq(&tasklist_lock);
  650. forget_original_parent(tsk, &dead);
  651. if (group_dead)
  652. kill_orphaned_pgrp(tsk->group_leader, NULL);
  653. tsk->exit_state = EXIT_ZOMBIE;
  654. /*
  655. * sub-thread or delay_group_leader(), wake up the
  656. * PIDFD_THREAD waiters.
  657. */
  658. if (!thread_group_empty(tsk))
  659. do_notify_pidfd(tsk);
  660. if (unlikely(tsk->ptrace)) {
  661. int sig = thread_group_leader(tsk) &&
  662. thread_group_empty(tsk) &&
  663. !ptrace_reparented(tsk) ?
  664. tsk->exit_signal : SIGCHLD;
  665. autoreap = do_notify_parent(tsk, sig);
  666. } else if (thread_group_leader(tsk)) {
  667. autoreap = thread_group_empty(tsk) &&
  668. do_notify_parent(tsk, tsk->exit_signal);
  669. } else {
  670. autoreap = true;
  671. }
  672. if (autoreap) {
  673. tsk->exit_state = EXIT_DEAD;
  674. list_add(&tsk->ptrace_entry, &dead);
  675. }
  676. /* mt-exec, de_thread() is waiting for group leader */
  677. if (unlikely(tsk->signal->notify_count < 0))
  678. wake_up_process(tsk->signal->group_exec_task);
  679. write_unlock_irq(&tasklist_lock);
  680. list_for_each_entry_safe(p, n, &dead, ptrace_entry) {
  681. list_del_init(&p->ptrace_entry);
  682. release_task(p);
  683. }
  684. }
  685. #ifdef CONFIG_DEBUG_STACK_USAGE
  686. unsigned long stack_not_used(struct task_struct *p)
  687. {
  688. unsigned long *n = end_of_stack(p);
  689. do { /* Skip over canary */
  690. # ifdef CONFIG_STACK_GROWSUP
  691. n--;
  692. # else
  693. n++;
  694. # endif
  695. } while (!*n);
  696. # ifdef CONFIG_STACK_GROWSUP
  697. return (unsigned long)end_of_stack(p) - (unsigned long)n;
  698. # else
  699. return (unsigned long)n - (unsigned long)end_of_stack(p);
  700. # endif
  701. }
  702. /* Count the maximum pages reached in kernel stacks */
  703. static inline void kstack_histogram(unsigned long used_stack)
  704. {
  705. #ifdef CONFIG_VM_EVENT_COUNTERS
  706. if (used_stack <= 1024)
  707. count_vm_event(KSTACK_1K);
  708. #if THREAD_SIZE > 1024
  709. else if (used_stack <= 2048)
  710. count_vm_event(KSTACK_2K);
  711. #endif
  712. #if THREAD_SIZE > 2048
  713. else if (used_stack <= 4096)
  714. count_vm_event(KSTACK_4K);
  715. #endif
  716. #if THREAD_SIZE > 4096
  717. else if (used_stack <= 8192)
  718. count_vm_event(KSTACK_8K);
  719. #endif
  720. #if THREAD_SIZE > 8192
  721. else if (used_stack <= 16384)
  722. count_vm_event(KSTACK_16K);
  723. #endif
  724. #if THREAD_SIZE > 16384
  725. else if (used_stack <= 32768)
  726. count_vm_event(KSTACK_32K);
  727. #endif
  728. #if THREAD_SIZE > 32768
  729. else if (used_stack <= 65536)
  730. count_vm_event(KSTACK_64K);
  731. #endif
  732. #if THREAD_SIZE > 65536
  733. else
  734. count_vm_event(KSTACK_REST);
  735. #endif
  736. #endif /* CONFIG_VM_EVENT_COUNTERS */
  737. }
  738. static void check_stack_usage(void)
  739. {
  740. static DEFINE_SPINLOCK(low_water_lock);
  741. static int lowest_to_date = THREAD_SIZE;
  742. unsigned long free;
  743. free = stack_not_used(current);
  744. kstack_histogram(THREAD_SIZE - free);
  745. if (free >= lowest_to_date)
  746. return;
  747. spin_lock(&low_water_lock);
  748. if (free < lowest_to_date) {
  749. pr_info("%s (%d) used greatest stack depth: %lu bytes left\n",
  750. current->comm, task_pid_nr(current), free);
  751. lowest_to_date = free;
  752. }
  753. spin_unlock(&low_water_lock);
  754. }
  755. #else
  756. static inline void check_stack_usage(void) {}
  757. #endif
  758. static void synchronize_group_exit(struct task_struct *tsk, long code)
  759. {
  760. struct sighand_struct *sighand = tsk->sighand;
  761. struct signal_struct *signal = tsk->signal;
  762. spin_lock_irq(&sighand->siglock);
  763. signal->quick_threads--;
  764. if ((signal->quick_threads == 0) &&
  765. !(signal->flags & SIGNAL_GROUP_EXIT)) {
  766. signal->flags = SIGNAL_GROUP_EXIT;
  767. signal->group_exit_code = code;
  768. signal->group_stop_count = 0;
  769. }
  770. spin_unlock_irq(&sighand->siglock);
  771. }
  772. void __noreturn do_exit(long code)
  773. {
  774. struct task_struct *tsk = current;
  775. int group_dead;
  776. WARN_ON(irqs_disabled());
  777. synchronize_group_exit(tsk, code);
  778. WARN_ON(tsk->plug);
  779. kcov_task_exit(tsk);
  780. kmsan_task_exit(tsk);
  781. coredump_task_exit(tsk);
  782. ptrace_event(PTRACE_EVENT_EXIT, code);
  783. user_events_exit(tsk);
  784. io_uring_files_cancel();
  785. exit_signals(tsk); /* sets PF_EXITING */
  786. seccomp_filter_release(tsk);
  787. acct_update_integrals(tsk);
  788. group_dead = atomic_dec_and_test(&tsk->signal->live);
  789. if (group_dead) {
  790. /*
  791. * If the last thread of global init has exited, panic
  792. * immediately to get a useable coredump.
  793. */
  794. if (unlikely(is_global_init(tsk)))
  795. panic("Attempted to kill init! exitcode=0x%08x\n",
  796. tsk->signal->group_exit_code ?: (int)code);
  797. #ifdef CONFIG_POSIX_TIMERS
  798. hrtimer_cancel(&tsk->signal->real_timer);
  799. exit_itimers(tsk);
  800. #endif
  801. if (tsk->mm)
  802. setmax_mm_hiwater_rss(&tsk->signal->maxrss, tsk->mm);
  803. }
  804. acct_collect(code, group_dead);
  805. if (group_dead)
  806. tty_audit_exit();
  807. audit_free(tsk);
  808. tsk->exit_code = code;
  809. taskstats_exit(tsk, group_dead);
  810. exit_mm();
  811. if (group_dead)
  812. acct_process();
  813. trace_sched_process_exit(tsk);
  814. exit_sem(tsk);
  815. exit_shm(tsk);
  816. exit_files(tsk);
  817. exit_fs(tsk);
  818. if (group_dead)
  819. disassociate_ctty(1);
  820. exit_task_namespaces(tsk);
  821. exit_task_work(tsk);
  822. exit_thread(tsk);
  823. /*
  824. * Flush inherited counters to the parent - before the parent
  825. * gets woken up by child-exit notifications.
  826. *
  827. * because of cgroup mode, must be called before cgroup_exit()
  828. */
  829. perf_event_exit_task(tsk);
  830. sched_autogroup_exit_task(tsk);
  831. cgroup_exit(tsk);
  832. /*
  833. * FIXME: do that only when needed, using sched_exit tracepoint
  834. */
  835. flush_ptrace_hw_breakpoint(tsk);
  836. exit_tasks_rcu_start();
  837. exit_notify(tsk, group_dead);
  838. proc_exit_connector(tsk);
  839. mpol_put_task_policy(tsk);
  840. #ifdef CONFIG_FUTEX
  841. if (unlikely(current->pi_state_cache))
  842. kfree(current->pi_state_cache);
  843. #endif
  844. /*
  845. * Make sure we are holding no locks:
  846. */
  847. debug_check_no_locks_held();
  848. if (tsk->io_context)
  849. exit_io_context(tsk);
  850. if (tsk->splice_pipe)
  851. free_pipe_info(tsk->splice_pipe);
  852. if (tsk->task_frag.page)
  853. put_page(tsk->task_frag.page);
  854. exit_task_stack_account(tsk);
  855. check_stack_usage();
  856. preempt_disable();
  857. if (tsk->nr_dirtied)
  858. __this_cpu_add(dirty_throttle_leaks, tsk->nr_dirtied);
  859. exit_rcu();
  860. exit_tasks_rcu_finish();
  861. lockdep_free_task(tsk);
  862. do_task_dead();
  863. }
  864. void __noreturn make_task_dead(int signr)
  865. {
  866. /*
  867. * Take the task off the cpu after something catastrophic has
  868. * happened.
  869. *
  870. * We can get here from a kernel oops, sometimes with preemption off.
  871. * Start by checking for critical errors.
  872. * Then fix up important state like USER_DS and preemption.
  873. * Then do everything else.
  874. */
  875. struct task_struct *tsk = current;
  876. unsigned int limit;
  877. if (unlikely(in_interrupt()))
  878. panic("Aiee, killing interrupt handler!");
  879. if (unlikely(!tsk->pid))
  880. panic("Attempted to kill the idle task!");
  881. if (unlikely(irqs_disabled())) {
  882. pr_info("note: %s[%d] exited with irqs disabled\n",
  883. current->comm, task_pid_nr(current));
  884. local_irq_enable();
  885. }
  886. if (unlikely(in_atomic())) {
  887. pr_info("note: %s[%d] exited with preempt_count %d\n",
  888. current->comm, task_pid_nr(current),
  889. preempt_count());
  890. preempt_count_set(PREEMPT_ENABLED);
  891. }
  892. /*
  893. * Every time the system oopses, if the oops happens while a reference
  894. * to an object was held, the reference leaks.
  895. * If the oops doesn't also leak memory, repeated oopsing can cause
  896. * reference counters to wrap around (if they're not using refcount_t).
  897. * This means that repeated oopsing can make unexploitable-looking bugs
  898. * exploitable through repeated oopsing.
  899. * To make sure this can't happen, place an upper bound on how often the
  900. * kernel may oops without panic().
  901. */
  902. limit = READ_ONCE(oops_limit);
  903. if (atomic_inc_return(&oops_count) >= limit && limit)
  904. panic("Oopsed too often (kernel.oops_limit is %d)", limit);
  905. /*
  906. * We're taking recursive faults here in make_task_dead. Safest is to just
  907. * leave this task alone and wait for reboot.
  908. */
  909. if (unlikely(tsk->flags & PF_EXITING)) {
  910. pr_alert("Fixing recursive fault but reboot is needed!\n");
  911. futex_exit_recursive(tsk);
  912. tsk->exit_state = EXIT_DEAD;
  913. refcount_inc(&tsk->rcu_users);
  914. do_task_dead();
  915. }
  916. do_exit(signr);
  917. }
  918. SYSCALL_DEFINE1(exit, int, error_code)
  919. {
  920. do_exit((error_code&0xff)<<8);
  921. }
  922. /*
  923. * Take down every thread in the group. This is called by fatal signals
  924. * as well as by sys_exit_group (below).
  925. */
  926. void __noreturn
  927. do_group_exit(int exit_code)
  928. {
  929. struct signal_struct *sig = current->signal;
  930. if (sig->flags & SIGNAL_GROUP_EXIT)
  931. exit_code = sig->group_exit_code;
  932. else if (sig->group_exec_task)
  933. exit_code = 0;
  934. else {
  935. struct sighand_struct *const sighand = current->sighand;
  936. spin_lock_irq(&sighand->siglock);
  937. if (sig->flags & SIGNAL_GROUP_EXIT)
  938. /* Another thread got here before we took the lock. */
  939. exit_code = sig->group_exit_code;
  940. else if (sig->group_exec_task)
  941. exit_code = 0;
  942. else {
  943. sig->group_exit_code = exit_code;
  944. sig->flags = SIGNAL_GROUP_EXIT;
  945. zap_other_threads(current);
  946. }
  947. spin_unlock_irq(&sighand->siglock);
  948. }
  949. do_exit(exit_code);
  950. /* NOTREACHED */
  951. }
  952. /*
  953. * this kills every thread in the thread group. Note that any externally
  954. * wait4()-ing process will get the correct exit code - even if this
  955. * thread is not the thread group leader.
  956. */
  957. SYSCALL_DEFINE1(exit_group, int, error_code)
  958. {
  959. do_group_exit((error_code & 0xff) << 8);
  960. /* NOTREACHED */
  961. return 0;
  962. }
  963. static int eligible_pid(struct wait_opts *wo, struct task_struct *p)
  964. {
  965. return wo->wo_type == PIDTYPE_MAX ||
  966. task_pid_type(p, wo->wo_type) == wo->wo_pid;
  967. }
  968. static int
  969. eligible_child(struct wait_opts *wo, bool ptrace, struct task_struct *p)
  970. {
  971. if (!eligible_pid(wo, p))
  972. return 0;
  973. /*
  974. * Wait for all children (clone and not) if __WALL is set or
  975. * if it is traced by us.
  976. */
  977. if (ptrace || (wo->wo_flags & __WALL))
  978. return 1;
  979. /*
  980. * Otherwise, wait for clone children *only* if __WCLONE is set;
  981. * otherwise, wait for non-clone children *only*.
  982. *
  983. * Note: a "clone" child here is one that reports to its parent
  984. * using a signal other than SIGCHLD, or a non-leader thread which
  985. * we can only see if it is traced by us.
  986. */
  987. if ((p->exit_signal != SIGCHLD) ^ !!(wo->wo_flags & __WCLONE))
  988. return 0;
  989. return 1;
  990. }
  991. /*
  992. * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
  993. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  994. * the lock and this task is uninteresting. If we return nonzero, we have
  995. * released the lock and the system call should return.
  996. */
  997. static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
  998. {
  999. int state, status;
  1000. pid_t pid = task_pid_vnr(p);
  1001. uid_t uid = from_kuid_munged(current_user_ns(), task_uid(p));
  1002. struct waitid_info *infop;
  1003. if (!likely(wo->wo_flags & WEXITED))
  1004. return 0;
  1005. if (unlikely(wo->wo_flags & WNOWAIT)) {
  1006. status = (p->signal->flags & SIGNAL_GROUP_EXIT)
  1007. ? p->signal->group_exit_code : p->exit_code;
  1008. get_task_struct(p);
  1009. read_unlock(&tasklist_lock);
  1010. sched_annotate_sleep();
  1011. if (wo->wo_rusage)
  1012. getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
  1013. put_task_struct(p);
  1014. goto out_info;
  1015. }
  1016. /*
  1017. * Move the task's state to DEAD/TRACE, only one thread can do this.
  1018. */
  1019. state = (ptrace_reparented(p) && thread_group_leader(p)) ?
  1020. EXIT_TRACE : EXIT_DEAD;
  1021. if (cmpxchg(&p->exit_state, EXIT_ZOMBIE, state) != EXIT_ZOMBIE)
  1022. return 0;
  1023. /*
  1024. * We own this thread, nobody else can reap it.
  1025. */
  1026. read_unlock(&tasklist_lock);
  1027. sched_annotate_sleep();
  1028. /*
  1029. * Check thread_group_leader() to exclude the traced sub-threads.
  1030. */
  1031. if (state == EXIT_DEAD && thread_group_leader(p)) {
  1032. struct signal_struct *sig = p->signal;
  1033. struct signal_struct *psig = current->signal;
  1034. unsigned long maxrss;
  1035. u64 tgutime, tgstime;
  1036. /*
  1037. * The resource counters for the group leader are in its
  1038. * own task_struct. Those for dead threads in the group
  1039. * are in its signal_struct, as are those for the child
  1040. * processes it has previously reaped. All these
  1041. * accumulate in the parent's signal_struct c* fields.
  1042. *
  1043. * We don't bother to take a lock here to protect these
  1044. * p->signal fields because the whole thread group is dead
  1045. * and nobody can change them.
  1046. *
  1047. * psig->stats_lock also protects us from our sub-threads
  1048. * which can reap other children at the same time.
  1049. *
  1050. * We use thread_group_cputime_adjusted() to get times for
  1051. * the thread group, which consolidates times for all threads
  1052. * in the group including the group leader.
  1053. */
  1054. thread_group_cputime_adjusted(p, &tgutime, &tgstime);
  1055. write_seqlock_irq(&psig->stats_lock);
  1056. psig->cutime += tgutime + sig->cutime;
  1057. psig->cstime += tgstime + sig->cstime;
  1058. psig->cgtime += task_gtime(p) + sig->gtime + sig->cgtime;
  1059. psig->cmin_flt +=
  1060. p->min_flt + sig->min_flt + sig->cmin_flt;
  1061. psig->cmaj_flt +=
  1062. p->maj_flt + sig->maj_flt + sig->cmaj_flt;
  1063. psig->cnvcsw +=
  1064. p->nvcsw + sig->nvcsw + sig->cnvcsw;
  1065. psig->cnivcsw +=
  1066. p->nivcsw + sig->nivcsw + sig->cnivcsw;
  1067. psig->cinblock +=
  1068. task_io_get_inblock(p) +
  1069. sig->inblock + sig->cinblock;
  1070. psig->coublock +=
  1071. task_io_get_oublock(p) +
  1072. sig->oublock + sig->coublock;
  1073. maxrss = max(sig->maxrss, sig->cmaxrss);
  1074. if (psig->cmaxrss < maxrss)
  1075. psig->cmaxrss = maxrss;
  1076. task_io_accounting_add(&psig->ioac, &p->ioac);
  1077. task_io_accounting_add(&psig->ioac, &sig->ioac);
  1078. write_sequnlock_irq(&psig->stats_lock);
  1079. }
  1080. if (wo->wo_rusage)
  1081. getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
  1082. status = (p->signal->flags & SIGNAL_GROUP_EXIT)
  1083. ? p->signal->group_exit_code : p->exit_code;
  1084. wo->wo_stat = status;
  1085. if (state == EXIT_TRACE) {
  1086. write_lock_irq(&tasklist_lock);
  1087. /* We dropped tasklist, ptracer could die and untrace */
  1088. ptrace_unlink(p);
  1089. /* If parent wants a zombie, don't release it now */
  1090. state = EXIT_ZOMBIE;
  1091. if (do_notify_parent(p, p->exit_signal))
  1092. state = EXIT_DEAD;
  1093. p->exit_state = state;
  1094. write_unlock_irq(&tasklist_lock);
  1095. }
  1096. if (state == EXIT_DEAD)
  1097. release_task(p);
  1098. out_info:
  1099. infop = wo->wo_info;
  1100. if (infop) {
  1101. if ((status & 0x7f) == 0) {
  1102. infop->cause = CLD_EXITED;
  1103. infop->status = status >> 8;
  1104. } else {
  1105. infop->cause = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
  1106. infop->status = status & 0x7f;
  1107. }
  1108. infop->pid = pid;
  1109. infop->uid = uid;
  1110. }
  1111. return pid;
  1112. }
  1113. static int *task_stopped_code(struct task_struct *p, bool ptrace)
  1114. {
  1115. if (ptrace) {
  1116. if (task_is_traced(p) && !(p->jobctl & JOBCTL_LISTENING))
  1117. return &p->exit_code;
  1118. } else {
  1119. if (p->signal->flags & SIGNAL_STOP_STOPPED)
  1120. return &p->signal->group_exit_code;
  1121. }
  1122. return NULL;
  1123. }
  1124. /**
  1125. * wait_task_stopped - Wait for %TASK_STOPPED or %TASK_TRACED
  1126. * @wo: wait options
  1127. * @ptrace: is the wait for ptrace
  1128. * @p: task to wait for
  1129. *
  1130. * Handle sys_wait4() work for %p in state %TASK_STOPPED or %TASK_TRACED.
  1131. *
  1132. * CONTEXT:
  1133. * read_lock(&tasklist_lock), which is released if return value is
  1134. * non-zero. Also, grabs and releases @p->sighand->siglock.
  1135. *
  1136. * RETURNS:
  1137. * 0 if wait condition didn't exist and search for other wait conditions
  1138. * should continue. Non-zero return, -errno on failure and @p's pid on
  1139. * success, implies that tasklist_lock is released and wait condition
  1140. * search should terminate.
  1141. */
  1142. static int wait_task_stopped(struct wait_opts *wo,
  1143. int ptrace, struct task_struct *p)
  1144. {
  1145. struct waitid_info *infop;
  1146. int exit_code, *p_code, why;
  1147. uid_t uid = 0; /* unneeded, required by compiler */
  1148. pid_t pid;
  1149. /*
  1150. * Traditionally we see ptrace'd stopped tasks regardless of options.
  1151. */
  1152. if (!ptrace && !(wo->wo_flags & WUNTRACED))
  1153. return 0;
  1154. if (!task_stopped_code(p, ptrace))
  1155. return 0;
  1156. exit_code = 0;
  1157. spin_lock_irq(&p->sighand->siglock);
  1158. p_code = task_stopped_code(p, ptrace);
  1159. if (unlikely(!p_code))
  1160. goto unlock_sig;
  1161. exit_code = *p_code;
  1162. if (!exit_code)
  1163. goto unlock_sig;
  1164. if (!unlikely(wo->wo_flags & WNOWAIT))
  1165. *p_code = 0;
  1166. uid = from_kuid_munged(current_user_ns(), task_uid(p));
  1167. unlock_sig:
  1168. spin_unlock_irq(&p->sighand->siglock);
  1169. if (!exit_code)
  1170. return 0;
  1171. /*
  1172. * Now we are pretty sure this task is interesting.
  1173. * Make sure it doesn't get reaped out from under us while we
  1174. * give up the lock and then examine it below. We don't want to
  1175. * keep holding onto the tasklist_lock while we call getrusage and
  1176. * possibly take page faults for user memory.
  1177. */
  1178. get_task_struct(p);
  1179. pid = task_pid_vnr(p);
  1180. why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
  1181. read_unlock(&tasklist_lock);
  1182. sched_annotate_sleep();
  1183. if (wo->wo_rusage)
  1184. getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
  1185. put_task_struct(p);
  1186. if (likely(!(wo->wo_flags & WNOWAIT)))
  1187. wo->wo_stat = (exit_code << 8) | 0x7f;
  1188. infop = wo->wo_info;
  1189. if (infop) {
  1190. infop->cause = why;
  1191. infop->status = exit_code;
  1192. infop->pid = pid;
  1193. infop->uid = uid;
  1194. }
  1195. return pid;
  1196. }
  1197. /*
  1198. * Handle do_wait work for one task in a live, non-stopped state.
  1199. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  1200. * the lock and this task is uninteresting. If we return nonzero, we have
  1201. * released the lock and the system call should return.
  1202. */
  1203. static int wait_task_continued(struct wait_opts *wo, struct task_struct *p)
  1204. {
  1205. struct waitid_info *infop;
  1206. pid_t pid;
  1207. uid_t uid;
  1208. if (!unlikely(wo->wo_flags & WCONTINUED))
  1209. return 0;
  1210. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
  1211. return 0;
  1212. spin_lock_irq(&p->sighand->siglock);
  1213. /* Re-check with the lock held. */
  1214. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
  1215. spin_unlock_irq(&p->sighand->siglock);
  1216. return 0;
  1217. }
  1218. if (!unlikely(wo->wo_flags & WNOWAIT))
  1219. p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
  1220. uid = from_kuid_munged(current_user_ns(), task_uid(p));
  1221. spin_unlock_irq(&p->sighand->siglock);
  1222. pid = task_pid_vnr(p);
  1223. get_task_struct(p);
  1224. read_unlock(&tasklist_lock);
  1225. sched_annotate_sleep();
  1226. if (wo->wo_rusage)
  1227. getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
  1228. put_task_struct(p);
  1229. infop = wo->wo_info;
  1230. if (!infop) {
  1231. wo->wo_stat = 0xffff;
  1232. } else {
  1233. infop->cause = CLD_CONTINUED;
  1234. infop->pid = pid;
  1235. infop->uid = uid;
  1236. infop->status = SIGCONT;
  1237. }
  1238. return pid;
  1239. }
  1240. /*
  1241. * Consider @p for a wait by @parent.
  1242. *
  1243. * -ECHILD should be in ->notask_error before the first call.
  1244. * Returns nonzero for a final return, when we have unlocked tasklist_lock.
  1245. * Returns zero if the search for a child should continue;
  1246. * then ->notask_error is 0 if @p is an eligible child,
  1247. * or still -ECHILD.
  1248. */
  1249. static int wait_consider_task(struct wait_opts *wo, int ptrace,
  1250. struct task_struct *p)
  1251. {
  1252. /*
  1253. * We can race with wait_task_zombie() from another thread.
  1254. * Ensure that EXIT_ZOMBIE -> EXIT_DEAD/EXIT_TRACE transition
  1255. * can't confuse the checks below.
  1256. */
  1257. int exit_state = READ_ONCE(p->exit_state);
  1258. int ret;
  1259. if (unlikely(exit_state == EXIT_DEAD))
  1260. return 0;
  1261. ret = eligible_child(wo, ptrace, p);
  1262. if (!ret)
  1263. return ret;
  1264. if (unlikely(exit_state == EXIT_TRACE)) {
  1265. /*
  1266. * ptrace == 0 means we are the natural parent. In this case
  1267. * we should clear notask_error, debugger will notify us.
  1268. */
  1269. if (likely(!ptrace))
  1270. wo->notask_error = 0;
  1271. return 0;
  1272. }
  1273. if (likely(!ptrace) && unlikely(p->ptrace)) {
  1274. /*
  1275. * If it is traced by its real parent's group, just pretend
  1276. * the caller is ptrace_do_wait() and reap this child if it
  1277. * is zombie.
  1278. *
  1279. * This also hides group stop state from real parent; otherwise
  1280. * a single stop can be reported twice as group and ptrace stop.
  1281. * If a ptracer wants to distinguish these two events for its
  1282. * own children it should create a separate process which takes
  1283. * the role of real parent.
  1284. */
  1285. if (!ptrace_reparented(p))
  1286. ptrace = 1;
  1287. }
  1288. /* slay zombie? */
  1289. if (exit_state == EXIT_ZOMBIE) {
  1290. /* we don't reap group leaders with subthreads */
  1291. if (!delay_group_leader(p)) {
  1292. /*
  1293. * A zombie ptracee is only visible to its ptracer.
  1294. * Notification and reaping will be cascaded to the
  1295. * real parent when the ptracer detaches.
  1296. */
  1297. if (unlikely(ptrace) || likely(!p->ptrace))
  1298. return wait_task_zombie(wo, p);
  1299. }
  1300. /*
  1301. * Allow access to stopped/continued state via zombie by
  1302. * falling through. Clearing of notask_error is complex.
  1303. *
  1304. * When !@ptrace:
  1305. *
  1306. * If WEXITED is set, notask_error should naturally be
  1307. * cleared. If not, subset of WSTOPPED|WCONTINUED is set,
  1308. * so, if there are live subthreads, there are events to
  1309. * wait for. If all subthreads are dead, it's still safe
  1310. * to clear - this function will be called again in finite
  1311. * amount time once all the subthreads are released and
  1312. * will then return without clearing.
  1313. *
  1314. * When @ptrace:
  1315. *
  1316. * Stopped state is per-task and thus can't change once the
  1317. * target task dies. Only continued and exited can happen.
  1318. * Clear notask_error if WCONTINUED | WEXITED.
  1319. */
  1320. if (likely(!ptrace) || (wo->wo_flags & (WCONTINUED | WEXITED)))
  1321. wo->notask_error = 0;
  1322. } else {
  1323. /*
  1324. * @p is alive and it's gonna stop, continue or exit, so
  1325. * there always is something to wait for.
  1326. */
  1327. wo->notask_error = 0;
  1328. }
  1329. /*
  1330. * Wait for stopped. Depending on @ptrace, different stopped state
  1331. * is used and the two don't interact with each other.
  1332. */
  1333. ret = wait_task_stopped(wo, ptrace, p);
  1334. if (ret)
  1335. return ret;
  1336. /*
  1337. * Wait for continued. There's only one continued state and the
  1338. * ptracer can consume it which can confuse the real parent. Don't
  1339. * use WCONTINUED from ptracer. You don't need or want it.
  1340. */
  1341. return wait_task_continued(wo, p);
  1342. }
  1343. /*
  1344. * Do the work of do_wait() for one thread in the group, @tsk.
  1345. *
  1346. * -ECHILD should be in ->notask_error before the first call.
  1347. * Returns nonzero for a final return, when we have unlocked tasklist_lock.
  1348. * Returns zero if the search for a child should continue; then
  1349. * ->notask_error is 0 if there were any eligible children,
  1350. * or still -ECHILD.
  1351. */
  1352. static int do_wait_thread(struct wait_opts *wo, struct task_struct *tsk)
  1353. {
  1354. struct task_struct *p;
  1355. list_for_each_entry(p, &tsk->children, sibling) {
  1356. int ret = wait_consider_task(wo, 0, p);
  1357. if (ret)
  1358. return ret;
  1359. }
  1360. return 0;
  1361. }
  1362. static int ptrace_do_wait(struct wait_opts *wo, struct task_struct *tsk)
  1363. {
  1364. struct task_struct *p;
  1365. list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {
  1366. int ret = wait_consider_task(wo, 1, p);
  1367. if (ret)
  1368. return ret;
  1369. }
  1370. return 0;
  1371. }
  1372. bool pid_child_should_wake(struct wait_opts *wo, struct task_struct *p)
  1373. {
  1374. if (!eligible_pid(wo, p))
  1375. return false;
  1376. if ((wo->wo_flags & __WNOTHREAD) && wo->child_wait.private != p->parent)
  1377. return false;
  1378. return true;
  1379. }
  1380. static int child_wait_callback(wait_queue_entry_t *wait, unsigned mode,
  1381. int sync, void *key)
  1382. {
  1383. struct wait_opts *wo = container_of(wait, struct wait_opts,
  1384. child_wait);
  1385. struct task_struct *p = key;
  1386. if (pid_child_should_wake(wo, p))
  1387. return default_wake_function(wait, mode, sync, key);
  1388. return 0;
  1389. }
  1390. void __wake_up_parent(struct task_struct *p, struct task_struct *parent)
  1391. {
  1392. __wake_up_sync_key(&parent->signal->wait_chldexit,
  1393. TASK_INTERRUPTIBLE, p);
  1394. }
  1395. static bool is_effectively_child(struct wait_opts *wo, bool ptrace,
  1396. struct task_struct *target)
  1397. {
  1398. struct task_struct *parent =
  1399. !ptrace ? target->real_parent : target->parent;
  1400. return current == parent || (!(wo->wo_flags & __WNOTHREAD) &&
  1401. same_thread_group(current, parent));
  1402. }
  1403. /*
  1404. * Optimization for waiting on PIDTYPE_PID. No need to iterate through child
  1405. * and tracee lists to find the target task.
  1406. */
  1407. static int do_wait_pid(struct wait_opts *wo)
  1408. {
  1409. bool ptrace;
  1410. struct task_struct *target;
  1411. int retval;
  1412. ptrace = false;
  1413. target = pid_task(wo->wo_pid, PIDTYPE_TGID);
  1414. if (target && is_effectively_child(wo, ptrace, target)) {
  1415. retval = wait_consider_task(wo, ptrace, target);
  1416. if (retval)
  1417. return retval;
  1418. }
  1419. ptrace = true;
  1420. target = pid_task(wo->wo_pid, PIDTYPE_PID);
  1421. if (target && target->ptrace &&
  1422. is_effectively_child(wo, ptrace, target)) {
  1423. retval = wait_consider_task(wo, ptrace, target);
  1424. if (retval)
  1425. return retval;
  1426. }
  1427. return 0;
  1428. }
  1429. long __do_wait(struct wait_opts *wo)
  1430. {
  1431. long retval;
  1432. /*
  1433. * If there is nothing that can match our criteria, just get out.
  1434. * We will clear ->notask_error to zero if we see any child that
  1435. * might later match our criteria, even if we are not able to reap
  1436. * it yet.
  1437. */
  1438. wo->notask_error = -ECHILD;
  1439. if ((wo->wo_type < PIDTYPE_MAX) &&
  1440. (!wo->wo_pid || !pid_has_task(wo->wo_pid, wo->wo_type)))
  1441. goto notask;
  1442. read_lock(&tasklist_lock);
  1443. if (wo->wo_type == PIDTYPE_PID) {
  1444. retval = do_wait_pid(wo);
  1445. if (retval)
  1446. return retval;
  1447. } else {
  1448. struct task_struct *tsk = current;
  1449. do {
  1450. retval = do_wait_thread(wo, tsk);
  1451. if (retval)
  1452. return retval;
  1453. retval = ptrace_do_wait(wo, tsk);
  1454. if (retval)
  1455. return retval;
  1456. if (wo->wo_flags & __WNOTHREAD)
  1457. break;
  1458. } while_each_thread(current, tsk);
  1459. }
  1460. read_unlock(&tasklist_lock);
  1461. notask:
  1462. retval = wo->notask_error;
  1463. if (!retval && !(wo->wo_flags & WNOHANG))
  1464. return -ERESTARTSYS;
  1465. return retval;
  1466. }
  1467. static long do_wait(struct wait_opts *wo)
  1468. {
  1469. int retval;
  1470. trace_sched_process_wait(wo->wo_pid);
  1471. init_waitqueue_func_entry(&wo->child_wait, child_wait_callback);
  1472. wo->child_wait.private = current;
  1473. add_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
  1474. do {
  1475. set_current_state(TASK_INTERRUPTIBLE);
  1476. retval = __do_wait(wo);
  1477. if (retval != -ERESTARTSYS)
  1478. break;
  1479. if (signal_pending(current))
  1480. break;
  1481. schedule();
  1482. } while (1);
  1483. __set_current_state(TASK_RUNNING);
  1484. remove_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
  1485. return retval;
  1486. }
  1487. int kernel_waitid_prepare(struct wait_opts *wo, int which, pid_t upid,
  1488. struct waitid_info *infop, int options,
  1489. struct rusage *ru)
  1490. {
  1491. unsigned int f_flags = 0;
  1492. struct pid *pid = NULL;
  1493. enum pid_type type;
  1494. if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED|
  1495. __WNOTHREAD|__WCLONE|__WALL))
  1496. return -EINVAL;
  1497. if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
  1498. return -EINVAL;
  1499. switch (which) {
  1500. case P_ALL:
  1501. type = PIDTYPE_MAX;
  1502. break;
  1503. case P_PID:
  1504. type = PIDTYPE_PID;
  1505. if (upid <= 0)
  1506. return -EINVAL;
  1507. pid = find_get_pid(upid);
  1508. break;
  1509. case P_PGID:
  1510. type = PIDTYPE_PGID;
  1511. if (upid < 0)
  1512. return -EINVAL;
  1513. if (upid)
  1514. pid = find_get_pid(upid);
  1515. else
  1516. pid = get_task_pid(current, PIDTYPE_PGID);
  1517. break;
  1518. case P_PIDFD:
  1519. type = PIDTYPE_PID;
  1520. if (upid < 0)
  1521. return -EINVAL;
  1522. pid = pidfd_get_pid(upid, &f_flags);
  1523. if (IS_ERR(pid))
  1524. return PTR_ERR(pid);
  1525. break;
  1526. default:
  1527. return -EINVAL;
  1528. }
  1529. wo->wo_type = type;
  1530. wo->wo_pid = pid;
  1531. wo->wo_flags = options;
  1532. wo->wo_info = infop;
  1533. wo->wo_rusage = ru;
  1534. if (f_flags & O_NONBLOCK)
  1535. wo->wo_flags |= WNOHANG;
  1536. return 0;
  1537. }
  1538. static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop,
  1539. int options, struct rusage *ru)
  1540. {
  1541. struct wait_opts wo;
  1542. long ret;
  1543. ret = kernel_waitid_prepare(&wo, which, upid, infop, options, ru);
  1544. if (ret)
  1545. return ret;
  1546. ret = do_wait(&wo);
  1547. if (!ret && !(options & WNOHANG) && (wo.wo_flags & WNOHANG))
  1548. ret = -EAGAIN;
  1549. put_pid(wo.wo_pid);
  1550. return ret;
  1551. }
  1552. SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
  1553. infop, int, options, struct rusage __user *, ru)
  1554. {
  1555. struct rusage r;
  1556. struct waitid_info info = {.status = 0};
  1557. long err = kernel_waitid(which, upid, &info, options, ru ? &r : NULL);
  1558. int signo = 0;
  1559. if (err > 0) {
  1560. signo = SIGCHLD;
  1561. err = 0;
  1562. if (ru && copy_to_user(ru, &r, sizeof(struct rusage)))
  1563. return -EFAULT;
  1564. }
  1565. if (!infop)
  1566. return err;
  1567. if (!user_write_access_begin(infop, sizeof(*infop)))
  1568. return -EFAULT;
  1569. unsafe_put_user(signo, &infop->si_signo, Efault);
  1570. unsafe_put_user(0, &infop->si_errno, Efault);
  1571. unsafe_put_user(info.cause, &infop->si_code, Efault);
  1572. unsafe_put_user(info.pid, &infop->si_pid, Efault);
  1573. unsafe_put_user(info.uid, &infop->si_uid, Efault);
  1574. unsafe_put_user(info.status, &infop->si_status, Efault);
  1575. user_write_access_end();
  1576. return err;
  1577. Efault:
  1578. user_write_access_end();
  1579. return -EFAULT;
  1580. }
  1581. long kernel_wait4(pid_t upid, int __user *stat_addr, int options,
  1582. struct rusage *ru)
  1583. {
  1584. struct wait_opts wo;
  1585. struct pid *pid = NULL;
  1586. enum pid_type type;
  1587. long ret;
  1588. if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
  1589. __WNOTHREAD|__WCLONE|__WALL))
  1590. return -EINVAL;
  1591. /* -INT_MIN is not defined */
  1592. if (upid == INT_MIN)
  1593. return -ESRCH;
  1594. if (upid == -1)
  1595. type = PIDTYPE_MAX;
  1596. else if (upid < 0) {
  1597. type = PIDTYPE_PGID;
  1598. pid = find_get_pid(-upid);
  1599. } else if (upid == 0) {
  1600. type = PIDTYPE_PGID;
  1601. pid = get_task_pid(current, PIDTYPE_PGID);
  1602. } else /* upid > 0 */ {
  1603. type = PIDTYPE_PID;
  1604. pid = find_get_pid(upid);
  1605. }
  1606. wo.wo_type = type;
  1607. wo.wo_pid = pid;
  1608. wo.wo_flags = options | WEXITED;
  1609. wo.wo_info = NULL;
  1610. wo.wo_stat = 0;
  1611. wo.wo_rusage = ru;
  1612. ret = do_wait(&wo);
  1613. put_pid(pid);
  1614. if (ret > 0 && stat_addr && put_user(wo.wo_stat, stat_addr))
  1615. ret = -EFAULT;
  1616. return ret;
  1617. }
  1618. int kernel_wait(pid_t pid, int *stat)
  1619. {
  1620. struct wait_opts wo = {
  1621. .wo_type = PIDTYPE_PID,
  1622. .wo_pid = find_get_pid(pid),
  1623. .wo_flags = WEXITED,
  1624. };
  1625. int ret;
  1626. ret = do_wait(&wo);
  1627. if (ret > 0 && wo.wo_stat)
  1628. *stat = wo.wo_stat;
  1629. put_pid(wo.wo_pid);
  1630. return ret;
  1631. }
  1632. SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
  1633. int, options, struct rusage __user *, ru)
  1634. {
  1635. struct rusage r;
  1636. long err = kernel_wait4(upid, stat_addr, options, ru ? &r : NULL);
  1637. if (err > 0) {
  1638. if (ru && copy_to_user(ru, &r, sizeof(struct rusage)))
  1639. return -EFAULT;
  1640. }
  1641. return err;
  1642. }
  1643. #ifdef __ARCH_WANT_SYS_WAITPID
  1644. /*
  1645. * sys_waitpid() remains for compatibility. waitpid() should be
  1646. * implemented by calling sys_wait4() from libc.a.
  1647. */
  1648. SYSCALL_DEFINE3(waitpid, pid_t, pid, int __user *, stat_addr, int, options)
  1649. {
  1650. return kernel_wait4(pid, stat_addr, options, NULL);
  1651. }
  1652. #endif
  1653. #ifdef CONFIG_COMPAT
  1654. COMPAT_SYSCALL_DEFINE4(wait4,
  1655. compat_pid_t, pid,
  1656. compat_uint_t __user *, stat_addr,
  1657. int, options,
  1658. struct compat_rusage __user *, ru)
  1659. {
  1660. struct rusage r;
  1661. long err = kernel_wait4(pid, stat_addr, options, ru ? &r : NULL);
  1662. if (err > 0) {
  1663. if (ru && put_compat_rusage(&r, ru))
  1664. return -EFAULT;
  1665. }
  1666. return err;
  1667. }
  1668. COMPAT_SYSCALL_DEFINE5(waitid,
  1669. int, which, compat_pid_t, pid,
  1670. struct compat_siginfo __user *, infop, int, options,
  1671. struct compat_rusage __user *, uru)
  1672. {
  1673. struct rusage ru;
  1674. struct waitid_info info = {.status = 0};
  1675. long err = kernel_waitid(which, pid, &info, options, uru ? &ru : NULL);
  1676. int signo = 0;
  1677. if (err > 0) {
  1678. signo = SIGCHLD;
  1679. err = 0;
  1680. if (uru) {
  1681. /* kernel_waitid() overwrites everything in ru */
  1682. if (COMPAT_USE_64BIT_TIME)
  1683. err = copy_to_user(uru, &ru, sizeof(ru));
  1684. else
  1685. err = put_compat_rusage(&ru, uru);
  1686. if (err)
  1687. return -EFAULT;
  1688. }
  1689. }
  1690. if (!infop)
  1691. return err;
  1692. if (!user_write_access_begin(infop, sizeof(*infop)))
  1693. return -EFAULT;
  1694. unsafe_put_user(signo, &infop->si_signo, Efault);
  1695. unsafe_put_user(0, &infop->si_errno, Efault);
  1696. unsafe_put_user(info.cause, &infop->si_code, Efault);
  1697. unsafe_put_user(info.pid, &infop->si_pid, Efault);
  1698. unsafe_put_user(info.uid, &infop->si_uid, Efault);
  1699. unsafe_put_user(info.status, &infop->si_status, Efault);
  1700. user_write_access_end();
  1701. return err;
  1702. Efault:
  1703. user_write_access_end();
  1704. return -EFAULT;
  1705. }
  1706. #endif
  1707. /*
  1708. * This needs to be __function_aligned as GCC implicitly makes any
  1709. * implementation of abort() cold and drops alignment specified by
  1710. * -falign-functions=N.
  1711. *
  1712. * See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88345#c11
  1713. */
  1714. __weak __function_aligned void abort(void)
  1715. {
  1716. BUG();
  1717. /* if that doesn't kill us, halt */
  1718. panic("Oops failed to kill thread");
  1719. }
  1720. EXPORT_SYMBOL(abort);