exit.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975
  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. * Ignore thread-group leaders that exited before all
  656. * subthreads did.
  657. */
  658. if (!delay_group_leader(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. /*
  811. * Since sampling can touch ->mm, make sure to stop everything before we
  812. * tear it down.
  813. *
  814. * Also flushes inherited counters to the parent - before the parent
  815. * gets woken up by child-exit notifications.
  816. */
  817. perf_event_exit_task(tsk);
  818. exit_mm();
  819. if (group_dead)
  820. acct_process();
  821. trace_sched_process_exit(tsk);
  822. exit_sem(tsk);
  823. exit_shm(tsk);
  824. exit_files(tsk);
  825. exit_fs(tsk);
  826. if (group_dead)
  827. disassociate_ctty(1);
  828. exit_task_namespaces(tsk);
  829. exit_task_work(tsk);
  830. exit_thread(tsk);
  831. sched_autogroup_exit_task(tsk);
  832. cgroup_exit(tsk);
  833. /*
  834. * FIXME: do that only when needed, using sched_exit tracepoint
  835. */
  836. flush_ptrace_hw_breakpoint(tsk);
  837. exit_tasks_rcu_start();
  838. exit_notify(tsk, group_dead);
  839. proc_exit_connector(tsk);
  840. mpol_put_task_policy(tsk);
  841. #ifdef CONFIG_FUTEX
  842. if (unlikely(current->pi_state_cache))
  843. kfree(current->pi_state_cache);
  844. #endif
  845. /*
  846. * Make sure we are holding no locks:
  847. */
  848. debug_check_no_locks_held();
  849. if (tsk->io_context)
  850. exit_io_context(tsk);
  851. if (tsk->splice_pipe)
  852. free_pipe_info(tsk->splice_pipe);
  853. if (tsk->task_frag.page)
  854. put_page(tsk->task_frag.page);
  855. exit_task_stack_account(tsk);
  856. check_stack_usage();
  857. preempt_disable();
  858. if (tsk->nr_dirtied)
  859. __this_cpu_add(dirty_throttle_leaks, tsk->nr_dirtied);
  860. exit_rcu();
  861. exit_tasks_rcu_finish();
  862. lockdep_free_task(tsk);
  863. do_task_dead();
  864. }
  865. void __noreturn make_task_dead(int signr)
  866. {
  867. /*
  868. * Take the task off the cpu after something catastrophic has
  869. * happened.
  870. *
  871. * We can get here from a kernel oops, sometimes with preemption off.
  872. * Start by checking for critical errors.
  873. * Then fix up important state like USER_DS and preemption.
  874. * Then do everything else.
  875. */
  876. struct task_struct *tsk = current;
  877. unsigned int limit;
  878. if (unlikely(in_interrupt()))
  879. panic("Aiee, killing interrupt handler!");
  880. if (unlikely(!tsk->pid))
  881. panic("Attempted to kill the idle task!");
  882. if (unlikely(irqs_disabled())) {
  883. pr_info("note: %s[%d] exited with irqs disabled\n",
  884. current->comm, task_pid_nr(current));
  885. local_irq_enable();
  886. }
  887. if (unlikely(in_atomic())) {
  888. pr_info("note: %s[%d] exited with preempt_count %d\n",
  889. current->comm, task_pid_nr(current),
  890. preempt_count());
  891. preempt_count_set(PREEMPT_ENABLED);
  892. }
  893. /*
  894. * Every time the system oopses, if the oops happens while a reference
  895. * to an object was held, the reference leaks.
  896. * If the oops doesn't also leak memory, repeated oopsing can cause
  897. * reference counters to wrap around (if they're not using refcount_t).
  898. * This means that repeated oopsing can make unexploitable-looking bugs
  899. * exploitable through repeated oopsing.
  900. * To make sure this can't happen, place an upper bound on how often the
  901. * kernel may oops without panic().
  902. */
  903. limit = READ_ONCE(oops_limit);
  904. if (atomic_inc_return(&oops_count) >= limit && limit)
  905. panic("Oopsed too often (kernel.oops_limit is %d)", limit);
  906. /*
  907. * We're taking recursive faults here in make_task_dead. Safest is to just
  908. * leave this task alone and wait for reboot.
  909. */
  910. if (unlikely(tsk->flags & PF_EXITING)) {
  911. pr_alert("Fixing recursive fault but reboot is needed!\n");
  912. futex_exit_recursive(tsk);
  913. tsk->exit_state = EXIT_DEAD;
  914. refcount_inc(&tsk->rcu_users);
  915. do_task_dead();
  916. }
  917. do_exit(signr);
  918. }
  919. SYSCALL_DEFINE1(exit, int, error_code)
  920. {
  921. do_exit((error_code&0xff)<<8);
  922. }
  923. /*
  924. * Take down every thread in the group. This is called by fatal signals
  925. * as well as by sys_exit_group (below).
  926. */
  927. void __noreturn
  928. do_group_exit(int exit_code)
  929. {
  930. struct signal_struct *sig = current->signal;
  931. if (sig->flags & SIGNAL_GROUP_EXIT)
  932. exit_code = sig->group_exit_code;
  933. else if (sig->group_exec_task)
  934. exit_code = 0;
  935. else {
  936. struct sighand_struct *const sighand = current->sighand;
  937. spin_lock_irq(&sighand->siglock);
  938. if (sig->flags & SIGNAL_GROUP_EXIT)
  939. /* Another thread got here before we took the lock. */
  940. exit_code = sig->group_exit_code;
  941. else if (sig->group_exec_task)
  942. exit_code = 0;
  943. else {
  944. sig->group_exit_code = exit_code;
  945. sig->flags = SIGNAL_GROUP_EXIT;
  946. zap_other_threads(current);
  947. }
  948. spin_unlock_irq(&sighand->siglock);
  949. }
  950. do_exit(exit_code);
  951. /* NOTREACHED */
  952. }
  953. /*
  954. * this kills every thread in the thread group. Note that any externally
  955. * wait4()-ing process will get the correct exit code - even if this
  956. * thread is not the thread group leader.
  957. */
  958. SYSCALL_DEFINE1(exit_group, int, error_code)
  959. {
  960. do_group_exit((error_code & 0xff) << 8);
  961. /* NOTREACHED */
  962. return 0;
  963. }
  964. static int eligible_pid(struct wait_opts *wo, struct task_struct *p)
  965. {
  966. return wo->wo_type == PIDTYPE_MAX ||
  967. task_pid_type(p, wo->wo_type) == wo->wo_pid;
  968. }
  969. static int
  970. eligible_child(struct wait_opts *wo, bool ptrace, struct task_struct *p)
  971. {
  972. if (!eligible_pid(wo, p))
  973. return 0;
  974. /*
  975. * Wait for all children (clone and not) if __WALL is set or
  976. * if it is traced by us.
  977. */
  978. if (ptrace || (wo->wo_flags & __WALL))
  979. return 1;
  980. /*
  981. * Otherwise, wait for clone children *only* if __WCLONE is set;
  982. * otherwise, wait for non-clone children *only*.
  983. *
  984. * Note: a "clone" child here is one that reports to its parent
  985. * using a signal other than SIGCHLD, or a non-leader thread which
  986. * we can only see if it is traced by us.
  987. */
  988. if ((p->exit_signal != SIGCHLD) ^ !!(wo->wo_flags & __WCLONE))
  989. return 0;
  990. return 1;
  991. }
  992. /*
  993. * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
  994. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  995. * the lock and this task is uninteresting. If we return nonzero, we have
  996. * released the lock and the system call should return.
  997. */
  998. static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
  999. {
  1000. int state, status;
  1001. pid_t pid = task_pid_vnr(p);
  1002. uid_t uid = from_kuid_munged(current_user_ns(), task_uid(p));
  1003. struct waitid_info *infop;
  1004. if (!likely(wo->wo_flags & WEXITED))
  1005. return 0;
  1006. if (unlikely(wo->wo_flags & WNOWAIT)) {
  1007. status = (p->signal->flags & SIGNAL_GROUP_EXIT)
  1008. ? p->signal->group_exit_code : p->exit_code;
  1009. get_task_struct(p);
  1010. read_unlock(&tasklist_lock);
  1011. sched_annotate_sleep();
  1012. if (wo->wo_rusage)
  1013. getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
  1014. put_task_struct(p);
  1015. goto out_info;
  1016. }
  1017. /*
  1018. * Move the task's state to DEAD/TRACE, only one thread can do this.
  1019. */
  1020. state = (ptrace_reparented(p) && thread_group_leader(p)) ?
  1021. EXIT_TRACE : EXIT_DEAD;
  1022. if (cmpxchg(&p->exit_state, EXIT_ZOMBIE, state) != EXIT_ZOMBIE)
  1023. return 0;
  1024. /*
  1025. * We own this thread, nobody else can reap it.
  1026. */
  1027. read_unlock(&tasklist_lock);
  1028. sched_annotate_sleep();
  1029. /*
  1030. * Check thread_group_leader() to exclude the traced sub-threads.
  1031. */
  1032. if (state == EXIT_DEAD && thread_group_leader(p)) {
  1033. struct signal_struct *sig = p->signal;
  1034. struct signal_struct *psig = current->signal;
  1035. unsigned long maxrss;
  1036. u64 tgutime, tgstime;
  1037. /*
  1038. * The resource counters for the group leader are in its
  1039. * own task_struct. Those for dead threads in the group
  1040. * are in its signal_struct, as are those for the child
  1041. * processes it has previously reaped. All these
  1042. * accumulate in the parent's signal_struct c* fields.
  1043. *
  1044. * We don't bother to take a lock here to protect these
  1045. * p->signal fields because the whole thread group is dead
  1046. * and nobody can change them.
  1047. *
  1048. * psig->stats_lock also protects us from our sub-threads
  1049. * which can reap other children at the same time.
  1050. *
  1051. * We use thread_group_cputime_adjusted() to get times for
  1052. * the thread group, which consolidates times for all threads
  1053. * in the group including the group leader.
  1054. */
  1055. thread_group_cputime_adjusted(p, &tgutime, &tgstime);
  1056. write_seqlock_irq(&psig->stats_lock);
  1057. psig->cutime += tgutime + sig->cutime;
  1058. psig->cstime += tgstime + sig->cstime;
  1059. psig->cgtime += task_gtime(p) + sig->gtime + sig->cgtime;
  1060. psig->cmin_flt +=
  1061. p->min_flt + sig->min_flt + sig->cmin_flt;
  1062. psig->cmaj_flt +=
  1063. p->maj_flt + sig->maj_flt + sig->cmaj_flt;
  1064. psig->cnvcsw +=
  1065. p->nvcsw + sig->nvcsw + sig->cnvcsw;
  1066. psig->cnivcsw +=
  1067. p->nivcsw + sig->nivcsw + sig->cnivcsw;
  1068. psig->cinblock +=
  1069. task_io_get_inblock(p) +
  1070. sig->inblock + sig->cinblock;
  1071. psig->coublock +=
  1072. task_io_get_oublock(p) +
  1073. sig->oublock + sig->coublock;
  1074. maxrss = max(sig->maxrss, sig->cmaxrss);
  1075. if (psig->cmaxrss < maxrss)
  1076. psig->cmaxrss = maxrss;
  1077. task_io_accounting_add(&psig->ioac, &p->ioac);
  1078. task_io_accounting_add(&psig->ioac, &sig->ioac);
  1079. write_sequnlock_irq(&psig->stats_lock);
  1080. }
  1081. if (wo->wo_rusage)
  1082. getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
  1083. status = (p->signal->flags & SIGNAL_GROUP_EXIT)
  1084. ? p->signal->group_exit_code : p->exit_code;
  1085. wo->wo_stat = status;
  1086. if (state == EXIT_TRACE) {
  1087. write_lock_irq(&tasklist_lock);
  1088. /* We dropped tasklist, ptracer could die and untrace */
  1089. ptrace_unlink(p);
  1090. /* If parent wants a zombie, don't release it now */
  1091. state = EXIT_ZOMBIE;
  1092. if (do_notify_parent(p, p->exit_signal))
  1093. state = EXIT_DEAD;
  1094. p->exit_state = state;
  1095. write_unlock_irq(&tasklist_lock);
  1096. }
  1097. if (state == EXIT_DEAD)
  1098. release_task(p);
  1099. out_info:
  1100. infop = wo->wo_info;
  1101. if (infop) {
  1102. if ((status & 0x7f) == 0) {
  1103. infop->cause = CLD_EXITED;
  1104. infop->status = status >> 8;
  1105. } else {
  1106. infop->cause = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
  1107. infop->status = status & 0x7f;
  1108. }
  1109. infop->pid = pid;
  1110. infop->uid = uid;
  1111. }
  1112. return pid;
  1113. }
  1114. static int *task_stopped_code(struct task_struct *p, bool ptrace)
  1115. {
  1116. if (ptrace) {
  1117. if (task_is_traced(p) && !(p->jobctl & JOBCTL_LISTENING))
  1118. return &p->exit_code;
  1119. } else {
  1120. if (p->signal->flags & SIGNAL_STOP_STOPPED)
  1121. return &p->signal->group_exit_code;
  1122. }
  1123. return NULL;
  1124. }
  1125. /**
  1126. * wait_task_stopped - Wait for %TASK_STOPPED or %TASK_TRACED
  1127. * @wo: wait options
  1128. * @ptrace: is the wait for ptrace
  1129. * @p: task to wait for
  1130. *
  1131. * Handle sys_wait4() work for %p in state %TASK_STOPPED or %TASK_TRACED.
  1132. *
  1133. * CONTEXT:
  1134. * read_lock(&tasklist_lock), which is released if return value is
  1135. * non-zero. Also, grabs and releases @p->sighand->siglock.
  1136. *
  1137. * RETURNS:
  1138. * 0 if wait condition didn't exist and search for other wait conditions
  1139. * should continue. Non-zero return, -errno on failure and @p's pid on
  1140. * success, implies that tasklist_lock is released and wait condition
  1141. * search should terminate.
  1142. */
  1143. static int wait_task_stopped(struct wait_opts *wo,
  1144. int ptrace, struct task_struct *p)
  1145. {
  1146. struct waitid_info *infop;
  1147. int exit_code, *p_code, why;
  1148. uid_t uid = 0; /* unneeded, required by compiler */
  1149. pid_t pid;
  1150. /*
  1151. * Traditionally we see ptrace'd stopped tasks regardless of options.
  1152. */
  1153. if (!ptrace && !(wo->wo_flags & WUNTRACED))
  1154. return 0;
  1155. if (!task_stopped_code(p, ptrace))
  1156. return 0;
  1157. exit_code = 0;
  1158. spin_lock_irq(&p->sighand->siglock);
  1159. p_code = task_stopped_code(p, ptrace);
  1160. if (unlikely(!p_code))
  1161. goto unlock_sig;
  1162. exit_code = *p_code;
  1163. if (!exit_code)
  1164. goto unlock_sig;
  1165. if (!unlikely(wo->wo_flags & WNOWAIT))
  1166. *p_code = 0;
  1167. uid = from_kuid_munged(current_user_ns(), task_uid(p));
  1168. unlock_sig:
  1169. spin_unlock_irq(&p->sighand->siglock);
  1170. if (!exit_code)
  1171. return 0;
  1172. /*
  1173. * Now we are pretty sure this task is interesting.
  1174. * Make sure it doesn't get reaped out from under us while we
  1175. * give up the lock and then examine it below. We don't want to
  1176. * keep holding onto the tasklist_lock while we call getrusage and
  1177. * possibly take page faults for user memory.
  1178. */
  1179. get_task_struct(p);
  1180. pid = task_pid_vnr(p);
  1181. why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
  1182. read_unlock(&tasklist_lock);
  1183. sched_annotate_sleep();
  1184. if (wo->wo_rusage)
  1185. getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
  1186. put_task_struct(p);
  1187. if (likely(!(wo->wo_flags & WNOWAIT)))
  1188. wo->wo_stat = (exit_code << 8) | 0x7f;
  1189. infop = wo->wo_info;
  1190. if (infop) {
  1191. infop->cause = why;
  1192. infop->status = exit_code;
  1193. infop->pid = pid;
  1194. infop->uid = uid;
  1195. }
  1196. return pid;
  1197. }
  1198. /*
  1199. * Handle do_wait work for one task in a live, non-stopped state.
  1200. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  1201. * the lock and this task is uninteresting. If we return nonzero, we have
  1202. * released the lock and the system call should return.
  1203. */
  1204. static int wait_task_continued(struct wait_opts *wo, struct task_struct *p)
  1205. {
  1206. struct waitid_info *infop;
  1207. pid_t pid;
  1208. uid_t uid;
  1209. if (!unlikely(wo->wo_flags & WCONTINUED))
  1210. return 0;
  1211. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
  1212. return 0;
  1213. spin_lock_irq(&p->sighand->siglock);
  1214. /* Re-check with the lock held. */
  1215. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
  1216. spin_unlock_irq(&p->sighand->siglock);
  1217. return 0;
  1218. }
  1219. if (!unlikely(wo->wo_flags & WNOWAIT))
  1220. p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
  1221. uid = from_kuid_munged(current_user_ns(), task_uid(p));
  1222. spin_unlock_irq(&p->sighand->siglock);
  1223. pid = task_pid_vnr(p);
  1224. get_task_struct(p);
  1225. read_unlock(&tasklist_lock);
  1226. sched_annotate_sleep();
  1227. if (wo->wo_rusage)
  1228. getrusage(p, RUSAGE_BOTH, wo->wo_rusage);
  1229. put_task_struct(p);
  1230. infop = wo->wo_info;
  1231. if (!infop) {
  1232. wo->wo_stat = 0xffff;
  1233. } else {
  1234. infop->cause = CLD_CONTINUED;
  1235. infop->pid = pid;
  1236. infop->uid = uid;
  1237. infop->status = SIGCONT;
  1238. }
  1239. return pid;
  1240. }
  1241. /*
  1242. * Consider @p for a wait by @parent.
  1243. *
  1244. * -ECHILD should be in ->notask_error before the first call.
  1245. * Returns nonzero for a final return, when we have unlocked tasklist_lock.
  1246. * Returns zero if the search for a child should continue;
  1247. * then ->notask_error is 0 if @p is an eligible child,
  1248. * or still -ECHILD.
  1249. */
  1250. static int wait_consider_task(struct wait_opts *wo, int ptrace,
  1251. struct task_struct *p)
  1252. {
  1253. /*
  1254. * We can race with wait_task_zombie() from another thread.
  1255. * Ensure that EXIT_ZOMBIE -> EXIT_DEAD/EXIT_TRACE transition
  1256. * can't confuse the checks below.
  1257. */
  1258. int exit_state = READ_ONCE(p->exit_state);
  1259. int ret;
  1260. if (unlikely(exit_state == EXIT_DEAD))
  1261. return 0;
  1262. ret = eligible_child(wo, ptrace, p);
  1263. if (!ret)
  1264. return ret;
  1265. if (unlikely(exit_state == EXIT_TRACE)) {
  1266. /*
  1267. * ptrace == 0 means we are the natural parent. In this case
  1268. * we should clear notask_error, debugger will notify us.
  1269. */
  1270. if (likely(!ptrace))
  1271. wo->notask_error = 0;
  1272. return 0;
  1273. }
  1274. if (likely(!ptrace) && unlikely(p->ptrace)) {
  1275. /*
  1276. * If it is traced by its real parent's group, just pretend
  1277. * the caller is ptrace_do_wait() and reap this child if it
  1278. * is zombie.
  1279. *
  1280. * This also hides group stop state from real parent; otherwise
  1281. * a single stop can be reported twice as group and ptrace stop.
  1282. * If a ptracer wants to distinguish these two events for its
  1283. * own children it should create a separate process which takes
  1284. * the role of real parent.
  1285. */
  1286. if (!ptrace_reparented(p))
  1287. ptrace = 1;
  1288. }
  1289. /* slay zombie? */
  1290. if (exit_state == EXIT_ZOMBIE) {
  1291. /* we don't reap group leaders with subthreads */
  1292. if (!delay_group_leader(p)) {
  1293. /*
  1294. * A zombie ptracee is only visible to its ptracer.
  1295. * Notification and reaping will be cascaded to the
  1296. * real parent when the ptracer detaches.
  1297. */
  1298. if (unlikely(ptrace) || likely(!p->ptrace))
  1299. return wait_task_zombie(wo, p);
  1300. }
  1301. /*
  1302. * Allow access to stopped/continued state via zombie by
  1303. * falling through. Clearing of notask_error is complex.
  1304. *
  1305. * When !@ptrace:
  1306. *
  1307. * If WEXITED is set, notask_error should naturally be
  1308. * cleared. If not, subset of WSTOPPED|WCONTINUED is set,
  1309. * so, if there are live subthreads, there are events to
  1310. * wait for. If all subthreads are dead, it's still safe
  1311. * to clear - this function will be called again in finite
  1312. * amount time once all the subthreads are released and
  1313. * will then return without clearing.
  1314. *
  1315. * When @ptrace:
  1316. *
  1317. * Stopped state is per-task and thus can't change once the
  1318. * target task dies. Only continued and exited can happen.
  1319. * Clear notask_error if WCONTINUED | WEXITED.
  1320. */
  1321. if (likely(!ptrace) || (wo->wo_flags & (WCONTINUED | WEXITED)))
  1322. wo->notask_error = 0;
  1323. } else {
  1324. /*
  1325. * @p is alive and it's gonna stop, continue or exit, so
  1326. * there always is something to wait for.
  1327. */
  1328. wo->notask_error = 0;
  1329. }
  1330. /*
  1331. * Wait for stopped. Depending on @ptrace, different stopped state
  1332. * is used and the two don't interact with each other.
  1333. */
  1334. ret = wait_task_stopped(wo, ptrace, p);
  1335. if (ret)
  1336. return ret;
  1337. /*
  1338. * Wait for continued. There's only one continued state and the
  1339. * ptracer can consume it which can confuse the real parent. Don't
  1340. * use WCONTINUED from ptracer. You don't need or want it.
  1341. */
  1342. return wait_task_continued(wo, p);
  1343. }
  1344. /*
  1345. * Do the work of do_wait() for one thread in the group, @tsk.
  1346. *
  1347. * -ECHILD should be in ->notask_error before the first call.
  1348. * Returns nonzero for a final return, when we have unlocked tasklist_lock.
  1349. * Returns zero if the search for a child should continue; then
  1350. * ->notask_error is 0 if there were any eligible children,
  1351. * or still -ECHILD.
  1352. */
  1353. static int do_wait_thread(struct wait_opts *wo, struct task_struct *tsk)
  1354. {
  1355. struct task_struct *p;
  1356. list_for_each_entry(p, &tsk->children, sibling) {
  1357. int ret = wait_consider_task(wo, 0, p);
  1358. if (ret)
  1359. return ret;
  1360. }
  1361. return 0;
  1362. }
  1363. static int ptrace_do_wait(struct wait_opts *wo, struct task_struct *tsk)
  1364. {
  1365. struct task_struct *p;
  1366. list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {
  1367. int ret = wait_consider_task(wo, 1, p);
  1368. if (ret)
  1369. return ret;
  1370. }
  1371. return 0;
  1372. }
  1373. bool pid_child_should_wake(struct wait_opts *wo, struct task_struct *p)
  1374. {
  1375. if (!eligible_pid(wo, p))
  1376. return false;
  1377. if ((wo->wo_flags & __WNOTHREAD) && wo->child_wait.private != p->parent)
  1378. return false;
  1379. return true;
  1380. }
  1381. static int child_wait_callback(wait_queue_entry_t *wait, unsigned mode,
  1382. int sync, void *key)
  1383. {
  1384. struct wait_opts *wo = container_of(wait, struct wait_opts,
  1385. child_wait);
  1386. struct task_struct *p = key;
  1387. if (pid_child_should_wake(wo, p))
  1388. return default_wake_function(wait, mode, sync, key);
  1389. return 0;
  1390. }
  1391. void __wake_up_parent(struct task_struct *p, struct task_struct *parent)
  1392. {
  1393. __wake_up_sync_key(&parent->signal->wait_chldexit,
  1394. TASK_INTERRUPTIBLE, p);
  1395. }
  1396. static bool is_effectively_child(struct wait_opts *wo, bool ptrace,
  1397. struct task_struct *target)
  1398. {
  1399. struct task_struct *parent =
  1400. !ptrace ? target->real_parent : target->parent;
  1401. return current == parent || (!(wo->wo_flags & __WNOTHREAD) &&
  1402. same_thread_group(current, parent));
  1403. }
  1404. /*
  1405. * Optimization for waiting on PIDTYPE_PID. No need to iterate through child
  1406. * and tracee lists to find the target task.
  1407. */
  1408. static int do_wait_pid(struct wait_opts *wo)
  1409. {
  1410. bool ptrace;
  1411. struct task_struct *target;
  1412. int retval;
  1413. ptrace = false;
  1414. target = pid_task(wo->wo_pid, PIDTYPE_TGID);
  1415. if (target && is_effectively_child(wo, ptrace, target)) {
  1416. retval = wait_consider_task(wo, ptrace, target);
  1417. if (retval)
  1418. return retval;
  1419. }
  1420. ptrace = true;
  1421. target = pid_task(wo->wo_pid, PIDTYPE_PID);
  1422. if (target && target->ptrace &&
  1423. is_effectively_child(wo, ptrace, target)) {
  1424. retval = wait_consider_task(wo, ptrace, target);
  1425. if (retval)
  1426. return retval;
  1427. }
  1428. return 0;
  1429. }
  1430. long __do_wait(struct wait_opts *wo)
  1431. {
  1432. long retval;
  1433. /*
  1434. * If there is nothing that can match our criteria, just get out.
  1435. * We will clear ->notask_error to zero if we see any child that
  1436. * might later match our criteria, even if we are not able to reap
  1437. * it yet.
  1438. */
  1439. wo->notask_error = -ECHILD;
  1440. if ((wo->wo_type < PIDTYPE_MAX) &&
  1441. (!wo->wo_pid || !pid_has_task(wo->wo_pid, wo->wo_type)))
  1442. goto notask;
  1443. read_lock(&tasklist_lock);
  1444. if (wo->wo_type == PIDTYPE_PID) {
  1445. retval = do_wait_pid(wo);
  1446. if (retval)
  1447. return retval;
  1448. } else {
  1449. struct task_struct *tsk = current;
  1450. do {
  1451. retval = do_wait_thread(wo, tsk);
  1452. if (retval)
  1453. return retval;
  1454. retval = ptrace_do_wait(wo, tsk);
  1455. if (retval)
  1456. return retval;
  1457. if (wo->wo_flags & __WNOTHREAD)
  1458. break;
  1459. } while_each_thread(current, tsk);
  1460. }
  1461. read_unlock(&tasklist_lock);
  1462. notask:
  1463. retval = wo->notask_error;
  1464. if (!retval && !(wo->wo_flags & WNOHANG))
  1465. return -ERESTARTSYS;
  1466. return retval;
  1467. }
  1468. static long do_wait(struct wait_opts *wo)
  1469. {
  1470. int retval;
  1471. trace_sched_process_wait(wo->wo_pid);
  1472. init_waitqueue_func_entry(&wo->child_wait, child_wait_callback);
  1473. wo->child_wait.private = current;
  1474. add_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
  1475. do {
  1476. set_current_state(TASK_INTERRUPTIBLE);
  1477. retval = __do_wait(wo);
  1478. if (retval != -ERESTARTSYS)
  1479. break;
  1480. if (signal_pending(current))
  1481. break;
  1482. schedule();
  1483. } while (1);
  1484. __set_current_state(TASK_RUNNING);
  1485. remove_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
  1486. return retval;
  1487. }
  1488. int kernel_waitid_prepare(struct wait_opts *wo, int which, pid_t upid,
  1489. struct waitid_info *infop, int options,
  1490. struct rusage *ru)
  1491. {
  1492. unsigned int f_flags = 0;
  1493. struct pid *pid = NULL;
  1494. enum pid_type type;
  1495. if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED|
  1496. __WNOTHREAD|__WCLONE|__WALL))
  1497. return -EINVAL;
  1498. if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
  1499. return -EINVAL;
  1500. switch (which) {
  1501. case P_ALL:
  1502. type = PIDTYPE_MAX;
  1503. break;
  1504. case P_PID:
  1505. type = PIDTYPE_PID;
  1506. if (upid <= 0)
  1507. return -EINVAL;
  1508. pid = find_get_pid(upid);
  1509. break;
  1510. case P_PGID:
  1511. type = PIDTYPE_PGID;
  1512. if (upid < 0)
  1513. return -EINVAL;
  1514. if (upid)
  1515. pid = find_get_pid(upid);
  1516. else
  1517. pid = get_task_pid(current, PIDTYPE_PGID);
  1518. break;
  1519. case P_PIDFD:
  1520. type = PIDTYPE_PID;
  1521. if (upid < 0)
  1522. return -EINVAL;
  1523. pid = pidfd_get_pid(upid, &f_flags);
  1524. if (IS_ERR(pid))
  1525. return PTR_ERR(pid);
  1526. break;
  1527. default:
  1528. return -EINVAL;
  1529. }
  1530. wo->wo_type = type;
  1531. wo->wo_pid = pid;
  1532. wo->wo_flags = options;
  1533. wo->wo_info = infop;
  1534. wo->wo_rusage = ru;
  1535. if (f_flags & O_NONBLOCK)
  1536. wo->wo_flags |= WNOHANG;
  1537. return 0;
  1538. }
  1539. static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop,
  1540. int options, struct rusage *ru)
  1541. {
  1542. struct wait_opts wo;
  1543. long ret;
  1544. ret = kernel_waitid_prepare(&wo, which, upid, infop, options, ru);
  1545. if (ret)
  1546. return ret;
  1547. ret = do_wait(&wo);
  1548. if (!ret && !(options & WNOHANG) && (wo.wo_flags & WNOHANG))
  1549. ret = -EAGAIN;
  1550. put_pid(wo.wo_pid);
  1551. return ret;
  1552. }
  1553. SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
  1554. infop, int, options, struct rusage __user *, ru)
  1555. {
  1556. struct rusage r;
  1557. struct waitid_info info = {.status = 0};
  1558. long err = kernel_waitid(which, upid, &info, options, ru ? &r : NULL);
  1559. int signo = 0;
  1560. if (err > 0) {
  1561. signo = SIGCHLD;
  1562. err = 0;
  1563. if (ru && copy_to_user(ru, &r, sizeof(struct rusage)))
  1564. return -EFAULT;
  1565. }
  1566. if (!infop)
  1567. return err;
  1568. if (!user_write_access_begin(infop, sizeof(*infop)))
  1569. return -EFAULT;
  1570. unsafe_put_user(signo, &infop->si_signo, Efault);
  1571. unsafe_put_user(0, &infop->si_errno, Efault);
  1572. unsafe_put_user(info.cause, &infop->si_code, Efault);
  1573. unsafe_put_user(info.pid, &infop->si_pid, Efault);
  1574. unsafe_put_user(info.uid, &infop->si_uid, Efault);
  1575. unsafe_put_user(info.status, &infop->si_status, Efault);
  1576. user_write_access_end();
  1577. return err;
  1578. Efault:
  1579. user_write_access_end();
  1580. return -EFAULT;
  1581. }
  1582. long kernel_wait4(pid_t upid, int __user *stat_addr, int options,
  1583. struct rusage *ru)
  1584. {
  1585. struct wait_opts wo;
  1586. struct pid *pid = NULL;
  1587. enum pid_type type;
  1588. long ret;
  1589. if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
  1590. __WNOTHREAD|__WCLONE|__WALL))
  1591. return -EINVAL;
  1592. /* -INT_MIN is not defined */
  1593. if (upid == INT_MIN)
  1594. return -ESRCH;
  1595. if (upid == -1)
  1596. type = PIDTYPE_MAX;
  1597. else if (upid < 0) {
  1598. type = PIDTYPE_PGID;
  1599. pid = find_get_pid(-upid);
  1600. } else if (upid == 0) {
  1601. type = PIDTYPE_PGID;
  1602. pid = get_task_pid(current, PIDTYPE_PGID);
  1603. } else /* upid > 0 */ {
  1604. type = PIDTYPE_PID;
  1605. pid = find_get_pid(upid);
  1606. }
  1607. wo.wo_type = type;
  1608. wo.wo_pid = pid;
  1609. wo.wo_flags = options | WEXITED;
  1610. wo.wo_info = NULL;
  1611. wo.wo_stat = 0;
  1612. wo.wo_rusage = ru;
  1613. ret = do_wait(&wo);
  1614. put_pid(pid);
  1615. if (ret > 0 && stat_addr && put_user(wo.wo_stat, stat_addr))
  1616. ret = -EFAULT;
  1617. return ret;
  1618. }
  1619. int kernel_wait(pid_t pid, int *stat)
  1620. {
  1621. struct wait_opts wo = {
  1622. .wo_type = PIDTYPE_PID,
  1623. .wo_pid = find_get_pid(pid),
  1624. .wo_flags = WEXITED,
  1625. };
  1626. int ret;
  1627. ret = do_wait(&wo);
  1628. if (ret > 0 && wo.wo_stat)
  1629. *stat = wo.wo_stat;
  1630. put_pid(wo.wo_pid);
  1631. return ret;
  1632. }
  1633. SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
  1634. int, options, struct rusage __user *, ru)
  1635. {
  1636. struct rusage r;
  1637. long err = kernel_wait4(upid, stat_addr, options, ru ? &r : NULL);
  1638. if (err > 0) {
  1639. if (ru && copy_to_user(ru, &r, sizeof(struct rusage)))
  1640. return -EFAULT;
  1641. }
  1642. return err;
  1643. }
  1644. #ifdef __ARCH_WANT_SYS_WAITPID
  1645. /*
  1646. * sys_waitpid() remains for compatibility. waitpid() should be
  1647. * implemented by calling sys_wait4() from libc.a.
  1648. */
  1649. SYSCALL_DEFINE3(waitpid, pid_t, pid, int __user *, stat_addr, int, options)
  1650. {
  1651. return kernel_wait4(pid, stat_addr, options, NULL);
  1652. }
  1653. #endif
  1654. #ifdef CONFIG_COMPAT
  1655. COMPAT_SYSCALL_DEFINE4(wait4,
  1656. compat_pid_t, pid,
  1657. compat_uint_t __user *, stat_addr,
  1658. int, options,
  1659. struct compat_rusage __user *, ru)
  1660. {
  1661. struct rusage r;
  1662. long err = kernel_wait4(pid, stat_addr, options, ru ? &r : NULL);
  1663. if (err > 0) {
  1664. if (ru && put_compat_rusage(&r, ru))
  1665. return -EFAULT;
  1666. }
  1667. return err;
  1668. }
  1669. COMPAT_SYSCALL_DEFINE5(waitid,
  1670. int, which, compat_pid_t, pid,
  1671. struct compat_siginfo __user *, infop, int, options,
  1672. struct compat_rusage __user *, uru)
  1673. {
  1674. struct rusage ru;
  1675. struct waitid_info info = {.status = 0};
  1676. long err = kernel_waitid(which, pid, &info, options, uru ? &ru : NULL);
  1677. int signo = 0;
  1678. if (err > 0) {
  1679. signo = SIGCHLD;
  1680. err = 0;
  1681. if (uru) {
  1682. /* kernel_waitid() overwrites everything in ru */
  1683. if (COMPAT_USE_64BIT_TIME)
  1684. err = copy_to_user(uru, &ru, sizeof(ru));
  1685. else
  1686. err = put_compat_rusage(&ru, uru);
  1687. if (err)
  1688. return -EFAULT;
  1689. }
  1690. }
  1691. if (!infop)
  1692. return err;
  1693. if (!user_write_access_begin(infop, sizeof(*infop)))
  1694. return -EFAULT;
  1695. unsafe_put_user(signo, &infop->si_signo, Efault);
  1696. unsafe_put_user(0, &infop->si_errno, Efault);
  1697. unsafe_put_user(info.cause, &infop->si_code, Efault);
  1698. unsafe_put_user(info.pid, &infop->si_pid, Efault);
  1699. unsafe_put_user(info.uid, &infop->si_uid, Efault);
  1700. unsafe_put_user(info.status, &infop->si_status, Efault);
  1701. user_write_access_end();
  1702. return err;
  1703. Efault:
  1704. user_write_access_end();
  1705. return -EFAULT;
  1706. }
  1707. #endif
  1708. /*
  1709. * This needs to be __function_aligned as GCC implicitly makes any
  1710. * implementation of abort() cold and drops alignment specified by
  1711. * -falign-functions=N.
  1712. *
  1713. * See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88345#c11
  1714. */
  1715. __weak __function_aligned void abort(void)
  1716. {
  1717. BUG();
  1718. /* if that doesn't kill us, halt */
  1719. panic("Oops failed to kill thread");
  1720. }
  1721. EXPORT_SYMBOL(abort);