exit.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901
  1. /*
  2. * linux/kernel/exit.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/mm.h>
  7. #include <linux/slab.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/module.h>
  10. #include <linux/capability.h>
  11. #include <linux/completion.h>
  12. #include <linux/personality.h>
  13. #include <linux/tty.h>
  14. #include <linux/iocontext.h>
  15. #include <linux/key.h>
  16. #include <linux/security.h>
  17. #include <linux/cpu.h>
  18. #include <linux/acct.h>
  19. #include <linux/tsacct_kern.h>
  20. #include <linux/file.h>
  21. #include <linux/fdtable.h>
  22. #include <linux/binfmts.h>
  23. #include <linux/nsproxy.h>
  24. #include <linux/pid_namespace.h>
  25. #include <linux/ptrace.h>
  26. #include <linux/profile.h>
  27. #include <linux/mount.h>
  28. #include <linux/proc_fs.h>
  29. #include <linux/kthread.h>
  30. #include <linux/mempolicy.h>
  31. #include <linux/taskstats_kern.h>
  32. #include <linux/delayacct.h>
  33. #include <linux/freezer.h>
  34. #include <linux/cgroup.h>
  35. #include <linux/syscalls.h>
  36. #include <linux/signal.h>
  37. #include <linux/posix-timers.h>
  38. #include <linux/cn_proc.h>
  39. #include <linux/mutex.h>
  40. #include <linux/futex.h>
  41. #include <linux/pipe_fs_i.h>
  42. #include <linux/audit.h> /* for audit_free() */
  43. #include <linux/resource.h>
  44. #include <linux/blkdev.h>
  45. #include <linux/task_io_accounting_ops.h>
  46. #include <linux/tracehook.h>
  47. #include <linux/fs_struct.h>
  48. #include <linux/init_task.h>
  49. #include <linux/perf_event.h>
  50. #include <trace/events/sched.h>
  51. #include <linux/hw_breakpoint.h>
  52. #include <linux/oom.h>
  53. #include <linux/writeback.h>
  54. #include <linux/shm.h>
  55. #include <asm/uaccess.h>
  56. #include <asm/unistd.h>
  57. #include <asm/pgtable.h>
  58. #include <asm/mmu_context.h>
  59. static void exit_mm(struct task_struct * tsk);
  60. static void __unhash_process(struct task_struct *p, bool group_dead)
  61. {
  62. nr_threads--;
  63. detach_pid(p, PIDTYPE_PID);
  64. if (group_dead) {
  65. detach_pid(p, PIDTYPE_PGID);
  66. detach_pid(p, PIDTYPE_SID);
  67. list_del_rcu(&p->tasks);
  68. list_del_init(&p->sibling);
  69. __this_cpu_dec(process_counts);
  70. }
  71. list_del_rcu(&p->thread_group);
  72. list_del_rcu(&p->thread_node);
  73. }
  74. /*
  75. * This function expects the tasklist_lock write-locked.
  76. */
  77. static void __exit_signal(struct task_struct *tsk)
  78. {
  79. struct signal_struct *sig = tsk->signal;
  80. bool group_dead = thread_group_leader(tsk);
  81. struct sighand_struct *sighand;
  82. struct tty_struct *uninitialized_var(tty);
  83. sighand = rcu_dereference_check(tsk->sighand,
  84. lockdep_tasklist_lock_is_held());
  85. spin_lock(&sighand->siglock);
  86. posix_cpu_timers_exit(tsk);
  87. if (group_dead) {
  88. posix_cpu_timers_exit_group(tsk);
  89. tty = sig->tty;
  90. sig->tty = NULL;
  91. } else {
  92. /*
  93. * This can only happen if the caller is de_thread().
  94. * FIXME: this is the temporary hack, we should teach
  95. * posix-cpu-timers to handle this case correctly.
  96. */
  97. if (unlikely(has_group_leader_pid(tsk)))
  98. posix_cpu_timers_exit_group(tsk);
  99. /*
  100. * If there is any task waiting for the group exit
  101. * then notify it:
  102. */
  103. if (sig->notify_count > 0 && !--sig->notify_count)
  104. wake_up_process(sig->group_exit_task);
  105. if (tsk == sig->curr_target)
  106. sig->curr_target = next_thread(tsk);
  107. /*
  108. * Accumulate here the counters for all threads but the
  109. * group leader as they die, so they can be added into
  110. * the process-wide totals when those are taken.
  111. * The group leader stays around as a zombie as long
  112. * as there are other threads. When it gets reaped,
  113. * the exit.c code will add its counts into these totals.
  114. * We won't ever get here for the group leader, since it
  115. * will have been the last reference on the signal_struct.
  116. */
  117. sig->utime += tsk->utime;
  118. sig->stime += tsk->stime;
  119. sig->gtime += tsk->gtime;
  120. sig->min_flt += tsk->min_flt;
  121. sig->maj_flt += tsk->maj_flt;
  122. sig->nvcsw += tsk->nvcsw;
  123. sig->nivcsw += tsk->nivcsw;
  124. sig->inblock += task_io_get_inblock(tsk);
  125. sig->oublock += task_io_get_oublock(tsk);
  126. task_io_accounting_add(&sig->ioac, &tsk->ioac);
  127. sig->sum_sched_runtime += tsk->se.sum_exec_runtime;
  128. }
  129. sig->nr_threads--;
  130. __unhash_process(tsk, group_dead);
  131. /*
  132. * Do this under ->siglock, we can race with another thread
  133. * doing sigqueue_free() if we have SIGQUEUE_PREALLOC signals.
  134. */
  135. flush_sigqueue(&tsk->pending);
  136. tsk->sighand = NULL;
  137. spin_unlock(&sighand->siglock);
  138. __cleanup_sighand(sighand);
  139. clear_tsk_thread_flag(tsk,TIF_SIGPENDING);
  140. if (group_dead) {
  141. flush_sigqueue(&sig->shared_pending);
  142. tty_kref_put(tty);
  143. }
  144. }
  145. static void delayed_put_task_struct(struct rcu_head *rhp)
  146. {
  147. struct task_struct *tsk = container_of(rhp, struct task_struct, rcu);
  148. perf_event_delayed_put(tsk);
  149. trace_sched_process_free(tsk);
  150. put_task_struct(tsk);
  151. }
  152. void release_task(struct task_struct * p)
  153. {
  154. struct task_struct *leader;
  155. int zap_leader;
  156. repeat:
  157. /* don't need to get the RCU readlock here - the process is dead and
  158. * can't be modifying its own credentials. But shut RCU-lockdep up */
  159. rcu_read_lock();
  160. atomic_dec(&__task_cred(p)->user->processes);
  161. rcu_read_unlock();
  162. proc_flush_task(p);
  163. write_lock_irq(&tasklist_lock);
  164. ptrace_release_task(p);
  165. __exit_signal(p);
  166. /*
  167. * If we are the last non-leader member of the thread
  168. * group, and the leader is zombie, then notify the
  169. * group leader's parent process. (if it wants notification.)
  170. */
  171. zap_leader = 0;
  172. leader = p->group_leader;
  173. if (leader != p && thread_group_empty(leader) && leader->exit_state == EXIT_ZOMBIE) {
  174. /*
  175. * If we were the last child thread and the leader has
  176. * exited already, and the leader's parent ignores SIGCHLD,
  177. * then we are the one who should release the leader.
  178. */
  179. zap_leader = do_notify_parent(leader, leader->exit_signal);
  180. if (zap_leader)
  181. leader->exit_state = EXIT_DEAD;
  182. }
  183. write_unlock_irq(&tasklist_lock);
  184. release_thread(p);
  185. call_rcu(&p->rcu, delayed_put_task_struct);
  186. p = leader;
  187. if (unlikely(zap_leader))
  188. goto repeat;
  189. }
  190. /*
  191. * This checks not only the pgrp, but falls back on the pid if no
  192. * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
  193. * without this...
  194. *
  195. * The caller must hold rcu lock or the tasklist lock.
  196. */
  197. struct pid *session_of_pgrp(struct pid *pgrp)
  198. {
  199. struct task_struct *p;
  200. struct pid *sid = NULL;
  201. p = pid_task(pgrp, PIDTYPE_PGID);
  202. if (p == NULL)
  203. p = pid_task(pgrp, PIDTYPE_PID);
  204. if (p != NULL)
  205. sid = task_session(p);
  206. return sid;
  207. }
  208. /*
  209. * Determine if a process group is "orphaned", according to the POSIX
  210. * definition in 2.2.2.52. Orphaned process groups are not to be affected
  211. * by terminal-generated stop signals. Newly orphaned process groups are
  212. * to receive a SIGHUP and a SIGCONT.
  213. *
  214. * "I ask you, have you ever known what it is to be an orphan?"
  215. */
  216. static int will_become_orphaned_pgrp(struct pid *pgrp, struct task_struct *ignored_task)
  217. {
  218. struct task_struct *p;
  219. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  220. if ((p == ignored_task) ||
  221. (p->exit_state && thread_group_empty(p)) ||
  222. is_global_init(p->real_parent))
  223. continue;
  224. if (task_pgrp(p->real_parent) != pgrp &&
  225. task_session(p->real_parent) == task_session(p))
  226. return 0;
  227. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  228. return 1;
  229. }
  230. int is_current_pgrp_orphaned(void)
  231. {
  232. int retval;
  233. read_lock(&tasklist_lock);
  234. retval = will_become_orphaned_pgrp(task_pgrp(current), NULL);
  235. read_unlock(&tasklist_lock);
  236. return retval;
  237. }
  238. static bool has_stopped_jobs(struct pid *pgrp)
  239. {
  240. struct task_struct *p;
  241. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  242. if (p->signal->flags & SIGNAL_STOP_STOPPED)
  243. return true;
  244. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  245. return false;
  246. }
  247. /*
  248. * Check to see if any process groups have become orphaned as
  249. * a result of our exiting, and if they have any stopped jobs,
  250. * send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
  251. */
  252. static void
  253. kill_orphaned_pgrp(struct task_struct *tsk, struct task_struct *parent)
  254. {
  255. struct pid *pgrp = task_pgrp(tsk);
  256. struct task_struct *ignored_task = tsk;
  257. if (!parent)
  258. /* exit: our father is in a different pgrp than
  259. * we are and we were the only connection outside.
  260. */
  261. parent = tsk->real_parent;
  262. else
  263. /* reparent: our child is in a different pgrp than
  264. * we are, and it was the only connection outside.
  265. */
  266. ignored_task = NULL;
  267. if (task_pgrp(parent) != pgrp &&
  268. task_session(parent) == task_session(tsk) &&
  269. will_become_orphaned_pgrp(pgrp, ignored_task) &&
  270. has_stopped_jobs(pgrp)) {
  271. __kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp);
  272. __kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp);
  273. }
  274. }
  275. /**
  276. * reparent_to_kthreadd - Reparent the calling kernel thread to kthreadd
  277. *
  278. * If a kernel thread is launched as a result of a system call, or if
  279. * it ever exits, it should generally reparent itself to kthreadd so it
  280. * isn't in the way of other processes and is correctly cleaned up on exit.
  281. *
  282. * The various task state such as scheduling policy and priority may have
  283. * been inherited from a user process, so we reset them to sane values here.
  284. *
  285. * NOTE that reparent_to_kthreadd() gives the caller full capabilities.
  286. */
  287. static void reparent_to_kthreadd(void)
  288. {
  289. write_lock_irq(&tasklist_lock);
  290. ptrace_unlink(current);
  291. /* Reparent to init */
  292. current->real_parent = current->parent = kthreadd_task;
  293. list_move_tail(&current->sibling, &current->real_parent->children);
  294. /* Set the exit signal to SIGCHLD so we signal init on exit */
  295. current->exit_signal = SIGCHLD;
  296. if (task_nice(current) < 0)
  297. set_user_nice(current, 0);
  298. /* cpus_allowed? */
  299. /* rt_priority? */
  300. /* signals? */
  301. memcpy(current->signal->rlim, init_task.signal->rlim,
  302. sizeof(current->signal->rlim));
  303. atomic_inc(&init_cred.usage);
  304. commit_creds(&init_cred);
  305. write_unlock_irq(&tasklist_lock);
  306. }
  307. void __set_special_pids(struct pid *pid)
  308. {
  309. struct task_struct *curr = current->group_leader;
  310. if (task_session(curr) != pid)
  311. change_pid(curr, PIDTYPE_SID, pid);
  312. if (task_pgrp(curr) != pid)
  313. change_pid(curr, PIDTYPE_PGID, pid);
  314. }
  315. static void set_special_pids(struct pid *pid)
  316. {
  317. write_lock_irq(&tasklist_lock);
  318. __set_special_pids(pid);
  319. write_unlock_irq(&tasklist_lock);
  320. }
  321. /*
  322. * Let kernel threads use this to say that they allow a certain signal.
  323. * Must not be used if kthread was cloned with CLONE_SIGHAND.
  324. */
  325. int allow_signal(int sig)
  326. {
  327. if (!valid_signal(sig) || sig < 1)
  328. return -EINVAL;
  329. spin_lock_irq(&current->sighand->siglock);
  330. /* This is only needed for daemonize()'ed kthreads */
  331. sigdelset(&current->blocked, sig);
  332. /*
  333. * Kernel threads handle their own signals. Let the signal code
  334. * know it'll be handled, so that they don't get converted to
  335. * SIGKILL or just silently dropped.
  336. */
  337. current->sighand->action[(sig)-1].sa.sa_handler = (void __user *)2;
  338. recalc_sigpending();
  339. spin_unlock_irq(&current->sighand->siglock);
  340. return 0;
  341. }
  342. EXPORT_SYMBOL(allow_signal);
  343. int disallow_signal(int sig)
  344. {
  345. if (!valid_signal(sig) || sig < 1)
  346. return -EINVAL;
  347. spin_lock_irq(&current->sighand->siglock);
  348. current->sighand->action[(sig)-1].sa.sa_handler = SIG_IGN;
  349. recalc_sigpending();
  350. spin_unlock_irq(&current->sighand->siglock);
  351. return 0;
  352. }
  353. EXPORT_SYMBOL(disallow_signal);
  354. /*
  355. * Put all the gunge required to become a kernel thread without
  356. * attached user resources in one place where it belongs.
  357. */
  358. void daemonize(const char *name, ...)
  359. {
  360. va_list args;
  361. sigset_t blocked;
  362. va_start(args, name);
  363. vsnprintf(current->comm, sizeof(current->comm), name, args);
  364. va_end(args);
  365. /*
  366. * If we were started as result of loading a module, close all of the
  367. * user space pages. We don't need them, and if we didn't close them
  368. * they would be locked into memory.
  369. */
  370. exit_mm(current);
  371. /*
  372. * We don't want to get frozen, in case system-wide hibernation
  373. * or suspend transition begins right now.
  374. */
  375. current->flags |= (PF_NOFREEZE | PF_KTHREAD);
  376. if (current->nsproxy != &init_nsproxy) {
  377. get_nsproxy(&init_nsproxy);
  378. switch_task_namespaces(current, &init_nsproxy);
  379. }
  380. set_special_pids(&init_struct_pid);
  381. proc_clear_tty(current);
  382. /* Block and flush all signals */
  383. sigfillset(&blocked);
  384. sigprocmask(SIG_BLOCK, &blocked, NULL);
  385. flush_signals(current);
  386. /* Become as one with the init task */
  387. daemonize_fs_struct();
  388. exit_files(current);
  389. current->files = init_task.files;
  390. atomic_inc(&current->files->count);
  391. reparent_to_kthreadd();
  392. }
  393. EXPORT_SYMBOL(daemonize);
  394. static void close_files(struct files_struct * files)
  395. {
  396. int i, j;
  397. struct fdtable *fdt;
  398. j = 0;
  399. /*
  400. * It is safe to dereference the fd table without RCU or
  401. * ->file_lock because this is the last reference to the
  402. * files structure. But use RCU to shut RCU-lockdep up.
  403. */
  404. rcu_read_lock();
  405. fdt = files_fdtable(files);
  406. rcu_read_unlock();
  407. for (;;) {
  408. unsigned long set;
  409. i = j * BITS_PER_LONG;
  410. if (i >= fdt->max_fds)
  411. break;
  412. set = fdt->open_fds[j++];
  413. while (set) {
  414. if (set & 1) {
  415. struct file * file = xchg(&fdt->fd[i], NULL);
  416. if (file) {
  417. filp_close(file, files);
  418. cond_resched();
  419. }
  420. }
  421. i++;
  422. set >>= 1;
  423. }
  424. }
  425. }
  426. struct files_struct *get_files_struct(struct task_struct *task)
  427. {
  428. struct files_struct *files;
  429. task_lock(task);
  430. files = task->files;
  431. if (files)
  432. atomic_inc(&files->count);
  433. task_unlock(task);
  434. return files;
  435. }
  436. void put_files_struct(struct files_struct *files)
  437. {
  438. struct fdtable *fdt;
  439. if (atomic_dec_and_test(&files->count)) {
  440. close_files(files);
  441. /*
  442. * Free the fd and fdset arrays if we expanded them.
  443. * If the fdtable was embedded, pass files for freeing
  444. * at the end of the RCU grace period. Otherwise,
  445. * you can free files immediately.
  446. */
  447. rcu_read_lock();
  448. fdt = files_fdtable(files);
  449. if (fdt != &files->fdtab)
  450. kmem_cache_free(files_cachep, files);
  451. free_fdtable(fdt);
  452. rcu_read_unlock();
  453. }
  454. }
  455. void reset_files_struct(struct files_struct *files)
  456. {
  457. struct task_struct *tsk = current;
  458. struct files_struct *old;
  459. old = tsk->files;
  460. task_lock(tsk);
  461. tsk->files = files;
  462. task_unlock(tsk);
  463. put_files_struct(old);
  464. }
  465. void exit_files(struct task_struct *tsk)
  466. {
  467. struct files_struct * files = tsk->files;
  468. if (files) {
  469. task_lock(tsk);
  470. tsk->files = NULL;
  471. task_unlock(tsk);
  472. put_files_struct(files);
  473. }
  474. }
  475. #ifdef CONFIG_MM_OWNER
  476. /*
  477. * A task is exiting. If it owned this mm, find a new owner for the mm.
  478. */
  479. void mm_update_next_owner(struct mm_struct *mm)
  480. {
  481. struct task_struct *c, *g, *p = current;
  482. retry:
  483. /*
  484. * If the exiting or execing task is not the owner, it's
  485. * someone else's problem.
  486. */
  487. if (mm->owner != p)
  488. return;
  489. /*
  490. * The current owner is exiting/execing and there are no other
  491. * candidates. Do not leave the mm pointing to a possibly
  492. * freed task structure.
  493. */
  494. if (atomic_read(&mm->mm_users) <= 1) {
  495. mm->owner = NULL;
  496. return;
  497. }
  498. read_lock(&tasklist_lock);
  499. /*
  500. * Search in the children
  501. */
  502. list_for_each_entry(c, &p->children, sibling) {
  503. if (c->mm == mm)
  504. goto assign_new_owner;
  505. }
  506. /*
  507. * Search in the siblings
  508. */
  509. list_for_each_entry(c, &p->real_parent->children, sibling) {
  510. if (c->mm == mm)
  511. goto assign_new_owner;
  512. }
  513. /*
  514. * Search through everything else. We should not get
  515. * here often
  516. */
  517. do_each_thread(g, c) {
  518. if (c->mm == mm)
  519. goto assign_new_owner;
  520. } while_each_thread(g, c);
  521. read_unlock(&tasklist_lock);
  522. /*
  523. * We found no owner yet mm_users > 1: this implies that we are
  524. * most likely racing with swapoff (try_to_unuse()) or /proc or
  525. * ptrace or page migration (get_task_mm()). Mark owner as NULL.
  526. */
  527. mm->owner = NULL;
  528. return;
  529. assign_new_owner:
  530. BUG_ON(c == p);
  531. get_task_struct(c);
  532. /*
  533. * The task_lock protects c->mm from changing.
  534. * We always want mm->owner->mm == mm
  535. */
  536. task_lock(c);
  537. /*
  538. * Delay read_unlock() till we have the task_lock()
  539. * to ensure that c does not slip away underneath us
  540. */
  541. read_unlock(&tasklist_lock);
  542. if (c->mm != mm) {
  543. task_unlock(c);
  544. put_task_struct(c);
  545. goto retry;
  546. }
  547. mm->owner = c;
  548. task_unlock(c);
  549. put_task_struct(c);
  550. }
  551. #endif /* CONFIG_MM_OWNER */
  552. /*
  553. * Turn us into a lazy TLB process if we
  554. * aren't already..
  555. */
  556. static void exit_mm(struct task_struct * tsk)
  557. {
  558. struct mm_struct *mm = tsk->mm;
  559. struct core_state *core_state;
  560. int mm_released;
  561. mm_release(tsk, mm);
  562. if (!mm)
  563. return;
  564. sync_mm_rss(mm);
  565. /*
  566. * Serialize with any possible pending coredump.
  567. * We must hold mmap_sem around checking core_state
  568. * and clearing tsk->mm. The core-inducing thread
  569. * will increment ->nr_threads for each thread in the
  570. * group with ->mm != NULL.
  571. */
  572. down_read(&mm->mmap_sem);
  573. core_state = mm->core_state;
  574. if (core_state) {
  575. struct core_thread self;
  576. up_read(&mm->mmap_sem);
  577. self.task = tsk;
  578. self.next = xchg(&core_state->dumper.next, &self);
  579. /*
  580. * Implies mb(), the result of xchg() must be visible
  581. * to core_state->dumper.
  582. */
  583. if (atomic_dec_and_test(&core_state->nr_threads))
  584. complete(&core_state->startup);
  585. for (;;) {
  586. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  587. if (!self.task) /* see coredump_finish() */
  588. break;
  589. schedule();
  590. }
  591. __set_task_state(tsk, TASK_RUNNING);
  592. down_read(&mm->mmap_sem);
  593. }
  594. atomic_inc(&mm->mm_count);
  595. BUG_ON(mm != tsk->active_mm);
  596. /* more a memory barrier than a real lock */
  597. task_lock(tsk);
  598. tsk->mm = NULL;
  599. up_read(&mm->mmap_sem);
  600. enter_lazy_tlb(mm, current);
  601. task_unlock(tsk);
  602. mm_update_next_owner(mm);
  603. mm_released = mmput(mm);
  604. if (mm_released)
  605. set_tsk_thread_flag(tsk, TIF_MM_RELEASED);
  606. clear_thread_flag(TIF_MEMDIE);
  607. }
  608. /*
  609. * When we die, we re-parent all our children, and try to:
  610. * 1. give them to another thread in our thread group, if such a member exists
  611. * 2. give it to the first ancestor process which prctl'd itself as a
  612. * child_subreaper for its children (like a service manager)
  613. * 3. give it to the init process (PID 1) in our pid namespace
  614. */
  615. static struct task_struct *find_new_reaper(struct task_struct *father)
  616. __releases(&tasklist_lock)
  617. __acquires(&tasklist_lock)
  618. {
  619. struct pid_namespace *pid_ns = task_active_pid_ns(father);
  620. struct task_struct *thread;
  621. thread = father;
  622. while_each_thread(father, thread) {
  623. if (thread->flags & PF_EXITING)
  624. continue;
  625. if (unlikely(pid_ns->child_reaper == father))
  626. pid_ns->child_reaper = thread;
  627. return thread;
  628. }
  629. if (unlikely(pid_ns->child_reaper == father)) {
  630. write_unlock_irq(&tasklist_lock);
  631. if (unlikely(pid_ns == &init_pid_ns)) {
  632. panic("Attempted to kill init! exitcode=0x%08x\n",
  633. father->signal->group_exit_code ?:
  634. father->exit_code);
  635. }
  636. zap_pid_ns_processes(pid_ns);
  637. write_lock_irq(&tasklist_lock);
  638. /*
  639. * We can not clear ->child_reaper or leave it alone.
  640. * There may by stealth EXIT_DEAD tasks on ->children,
  641. * forget_original_parent() must move them somewhere.
  642. */
  643. pid_ns->child_reaper = init_pid_ns.child_reaper;
  644. } else if (father->signal->has_child_subreaper) {
  645. struct task_struct *reaper;
  646. /*
  647. * Find the first ancestor marked as child_subreaper.
  648. * Note that the code below checks same_thread_group(reaper,
  649. * pid_ns->child_reaper). This is what we need to DTRT in a
  650. * PID namespace. However we still need the check above, see
  651. * http://marc.info/?l=linux-kernel&m=131385460420380
  652. */
  653. for (reaper = father->real_parent;
  654. reaper != &init_task;
  655. reaper = reaper->real_parent) {
  656. if (same_thread_group(reaper, pid_ns->child_reaper))
  657. break;
  658. if (!reaper->signal->is_child_subreaper)
  659. continue;
  660. thread = reaper;
  661. do {
  662. if (!(thread->flags & PF_EXITING))
  663. return reaper;
  664. } while_each_thread(reaper, thread);
  665. }
  666. }
  667. return pid_ns->child_reaper;
  668. }
  669. /*
  670. * Any that need to be release_task'd are put on the @dead list.
  671. */
  672. static void reparent_leader(struct task_struct *father, struct task_struct *p,
  673. struct list_head *dead)
  674. {
  675. list_move_tail(&p->sibling, &p->real_parent->children);
  676. /*
  677. * If this is a threaded reparent there is no need to
  678. * notify anyone anything has happened.
  679. */
  680. if (same_thread_group(p->real_parent, father))
  681. return;
  682. /*
  683. * We don't want people slaying init.
  684. *
  685. * Note: we do this even if it is EXIT_DEAD, wait_task_zombie()
  686. * can change ->exit_state to EXIT_ZOMBIE. If this is the final
  687. * state, do_notify_parent() was already called and ->exit_signal
  688. * doesn't matter.
  689. */
  690. p->exit_signal = SIGCHLD;
  691. if (p->exit_state == EXIT_DEAD)
  692. return;
  693. /* If it has exited notify the new parent about this child's death. */
  694. if (!p->ptrace &&
  695. p->exit_state == EXIT_ZOMBIE && thread_group_empty(p)) {
  696. if (do_notify_parent(p, p->exit_signal)) {
  697. p->exit_state = EXIT_DEAD;
  698. list_move_tail(&p->sibling, dead);
  699. }
  700. }
  701. kill_orphaned_pgrp(p, father);
  702. }
  703. static void forget_original_parent(struct task_struct *father)
  704. {
  705. struct task_struct *p, *n, *reaper;
  706. LIST_HEAD(dead_children);
  707. write_lock_irq(&tasklist_lock);
  708. /*
  709. * Note that exit_ptrace() and find_new_reaper() might
  710. * drop tasklist_lock and reacquire it.
  711. */
  712. exit_ptrace(father);
  713. reaper = find_new_reaper(father);
  714. list_for_each_entry_safe(p, n, &father->children, sibling) {
  715. struct task_struct *t = p;
  716. do {
  717. t->real_parent = reaper;
  718. if (t->parent == father) {
  719. BUG_ON(t->ptrace);
  720. t->parent = t->real_parent;
  721. }
  722. if (t->pdeath_signal)
  723. group_send_sig_info(t->pdeath_signal,
  724. SEND_SIG_NOINFO, t);
  725. } while_each_thread(p, t);
  726. reparent_leader(father, p, &dead_children);
  727. }
  728. write_unlock_irq(&tasklist_lock);
  729. BUG_ON(!list_empty(&father->children));
  730. list_for_each_entry_safe(p, n, &dead_children, sibling) {
  731. list_del_init(&p->sibling);
  732. release_task(p);
  733. }
  734. }
  735. /*
  736. * Send signals to all our closest relatives so that they know
  737. * to properly mourn us..
  738. */
  739. static void exit_notify(struct task_struct *tsk, int group_dead)
  740. {
  741. bool autoreap;
  742. /*
  743. * This does two things:
  744. *
  745. * A. Make init inherit all the child processes
  746. * B. Check to see if any process groups have become orphaned
  747. * as a result of our exiting, and if they have any stopped
  748. * jobs, send them a SIGHUP and then a SIGCONT. (POSIX 3.2.2.2)
  749. */
  750. forget_original_parent(tsk);
  751. exit_task_namespaces(tsk);
  752. write_lock_irq(&tasklist_lock);
  753. if (group_dead)
  754. kill_orphaned_pgrp(tsk->group_leader, NULL);
  755. if (unlikely(tsk->ptrace)) {
  756. int sig = thread_group_leader(tsk) &&
  757. thread_group_empty(tsk) &&
  758. !ptrace_reparented(tsk) ?
  759. tsk->exit_signal : SIGCHLD;
  760. autoreap = do_notify_parent(tsk, sig);
  761. } else if (thread_group_leader(tsk)) {
  762. autoreap = thread_group_empty(tsk) &&
  763. do_notify_parent(tsk, tsk->exit_signal);
  764. } else {
  765. autoreap = true;
  766. }
  767. tsk->exit_state = autoreap ? EXIT_DEAD : EXIT_ZOMBIE;
  768. /* mt-exec, de_thread() is waiting for group leader */
  769. if (unlikely(tsk->signal->notify_count < 0))
  770. wake_up_process(tsk->signal->group_exit_task);
  771. write_unlock_irq(&tasklist_lock);
  772. /* If the process is dead, release it - nobody will wait for it */
  773. if (autoreap)
  774. release_task(tsk);
  775. }
  776. #ifdef CONFIG_DEBUG_STACK_USAGE
  777. static void check_stack_usage(void)
  778. {
  779. static DEFINE_SPINLOCK(low_water_lock);
  780. static int lowest_to_date = THREAD_SIZE;
  781. unsigned long free;
  782. free = stack_not_used(current);
  783. if (free >= lowest_to_date)
  784. return;
  785. spin_lock(&low_water_lock);
  786. if (free < lowest_to_date) {
  787. printk(KERN_WARNING "%s (%d) used greatest stack depth: "
  788. "%lu bytes left\n",
  789. current->comm, task_pid_nr(current), free);
  790. lowest_to_date = free;
  791. }
  792. spin_unlock(&low_water_lock);
  793. }
  794. #else
  795. static inline void check_stack_usage(void) {}
  796. #endif
  797. void do_exit(long code)
  798. {
  799. struct task_struct *tsk = current;
  800. int group_dead;
  801. profile_task_exit(tsk);
  802. WARN_ON(blk_needs_flush_plug(tsk));
  803. if (unlikely(in_interrupt()))
  804. panic("Aiee, killing interrupt handler!");
  805. if (unlikely(!tsk->pid) || unlikely(tsk->pid==1))
  806. panic("Attempted to kill the idle task! or init task");
  807. /*
  808. * If do_exit is called because this processes oopsed, it's possible
  809. * that get_fs() was left as KERNEL_DS, so reset it to USER_DS before
  810. * continuing. Amongst other possible reasons, this is to prevent
  811. * mm_release()->clear_child_tid() from writing to a user-controlled
  812. * kernel address.
  813. */
  814. set_fs(USER_DS);
  815. ptrace_event(PTRACE_EVENT_EXIT, code);
  816. validate_creds_for_do_exit(tsk);
  817. /*
  818. * We're taking recursive faults here in do_exit. Safest is to just
  819. * leave this task alone and wait for reboot.
  820. */
  821. if (unlikely(tsk->flags & PF_EXITING)) {
  822. printk(KERN_ALERT
  823. "Fixing recursive fault but reboot is needed!\n");
  824. /*
  825. * We can do this unlocked here. The futex code uses
  826. * this flag just to verify whether the pi state
  827. * cleanup has been done or not. In the worst case it
  828. * loops once more. We pretend that the cleanup was
  829. * done as there is no way to return. Either the
  830. * OWNER_DIED bit is set by now or we push the blocked
  831. * task into the wait for ever nirwana as well.
  832. */
  833. tsk->flags |= PF_EXITPIDONE;
  834. set_current_state(TASK_UNINTERRUPTIBLE);
  835. schedule();
  836. }
  837. exit_signals(tsk); /* sets PF_EXITING */
  838. if (tsk->flags & PF_SU) {
  839. su_exit();
  840. }
  841. /*
  842. * tsk->flags are checked in the futex code to protect against
  843. * an exiting task cleaning up the robust pi futexes.
  844. */
  845. smp_mb();
  846. raw_spin_unlock_wait(&tsk->pi_lock);
  847. exit_irq_thread();
  848. if (unlikely(in_atomic()))
  849. printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
  850. current->comm, task_pid_nr(current),
  851. preempt_count());
  852. acct_update_integrals(tsk);
  853. /* sync mm's RSS info before statistics gathering */
  854. if (tsk->mm)
  855. sync_mm_rss(tsk->mm);
  856. group_dead = atomic_dec_and_test(&tsk->signal->live);
  857. if (group_dead) {
  858. hrtimer_cancel(&tsk->signal->real_timer);
  859. exit_itimers(tsk->signal);
  860. if (tsk->mm)
  861. setmax_mm_hiwater_rss(&tsk->signal->maxrss, tsk->mm);
  862. }
  863. acct_collect(code, group_dead);
  864. if (group_dead)
  865. tty_audit_exit();
  866. audit_free(tsk);
  867. tsk->exit_code = code;
  868. taskstats_exit(tsk, group_dead);
  869. exit_mm(tsk);
  870. if (group_dead)
  871. acct_process();
  872. trace_sched_process_exit(tsk);
  873. exit_sem(tsk);
  874. exit_shm(tsk);
  875. exit_files(tsk);
  876. exit_fs(tsk);
  877. check_stack_usage();
  878. exit_thread();
  879. /*
  880. * Flush inherited counters to the parent - before the parent
  881. * gets woken up by child-exit notifications.
  882. *
  883. * because of cgroup mode, must be called before cgroup_exit()
  884. */
  885. perf_event_exit_task(tsk);
  886. cgroup_exit(tsk, 1);
  887. if (group_dead)
  888. disassociate_ctty(1);
  889. module_put(task_thread_info(tsk)->exec_domain->module);
  890. proc_exit_connector(tsk);
  891. /*
  892. * FIXME: do that only when needed, using sched_exit tracepoint
  893. */
  894. ptrace_put_breakpoints(tsk);
  895. exit_notify(tsk, group_dead);
  896. #ifdef CONFIG_NUMA
  897. task_lock(tsk);
  898. mpol_put(tsk->mempolicy);
  899. tsk->mempolicy = NULL;
  900. task_unlock(tsk);
  901. #endif
  902. #ifdef CONFIG_FUTEX
  903. if (unlikely(current->pi_state_cache))
  904. kfree(current->pi_state_cache);
  905. #endif
  906. /*
  907. * Make sure we are holding no locks:
  908. */
  909. debug_check_no_locks_held();
  910. /*
  911. * We can do this unlocked here. The futex code uses this flag
  912. * just to verify whether the pi state cleanup has been done
  913. * or not. In the worst case it loops once more.
  914. */
  915. tsk->flags |= PF_EXITPIDONE;
  916. if (tsk->io_context)
  917. exit_io_context(tsk);
  918. if (tsk->splice_pipe)
  919. __free_pipe_info(tsk->splice_pipe);
  920. validate_creds_for_do_exit(tsk);
  921. preempt_disable();
  922. if (tsk->nr_dirtied)
  923. __this_cpu_add(dirty_throttle_leaks, tsk->nr_dirtied);
  924. exit_rcu();
  925. /*
  926. * The setting of TASK_RUNNING by try_to_wake_up() may be delayed
  927. * when the following two conditions become true.
  928. * - There is race condition of mmap_sem (It is acquired by
  929. * exit_mm()), and
  930. * - SMI occurs before setting TASK_RUNINNG.
  931. * (or hypervisor of virtual machine switches to other guest)
  932. * As a result, we may become TASK_RUNNING after becoming TASK_DEAD
  933. *
  934. * To avoid it, we have to wait for releasing tsk->pi_lock which
  935. * is held by try_to_wake_up()
  936. */
  937. smp_mb();
  938. raw_spin_unlock_wait(&tsk->pi_lock);
  939. /* causes final put_task_struct in finish_task_switch(). */
  940. tsk->state = TASK_DEAD;
  941. tsk->flags |= PF_NOFREEZE; /* tell freezer to ignore us */
  942. schedule();
  943. BUG();
  944. /* Avoid "noreturn function does return". */
  945. for (;;)
  946. cpu_relax(); /* For when BUG is null */
  947. }
  948. EXPORT_SYMBOL_GPL(do_exit);
  949. void complete_and_exit(struct completion *comp, long code)
  950. {
  951. if (comp)
  952. complete(comp);
  953. do_exit(code);
  954. }
  955. EXPORT_SYMBOL(complete_and_exit);
  956. SYSCALL_DEFINE1(exit, int, error_code)
  957. {
  958. do_exit((error_code&0xff)<<8);
  959. }
  960. /*
  961. * Take down every thread in the group. This is called by fatal signals
  962. * as well as by sys_exit_group (below).
  963. */
  964. void
  965. do_group_exit(int exit_code)
  966. {
  967. struct signal_struct *sig = current->signal;
  968. BUG_ON(exit_code & 0x80); /* core dumps don't get here */
  969. if (signal_group_exit(sig))
  970. exit_code = sig->group_exit_code;
  971. else if (!thread_group_empty(current)) {
  972. struct sighand_struct *const sighand = current->sighand;
  973. spin_lock_irq(&sighand->siglock);
  974. if (signal_group_exit(sig))
  975. /* Another thread got here before we took the lock. */
  976. exit_code = sig->group_exit_code;
  977. else {
  978. sig->group_exit_code = exit_code;
  979. sig->flags = SIGNAL_GROUP_EXIT;
  980. zap_other_threads(current);
  981. }
  982. spin_unlock_irq(&sighand->siglock);
  983. }
  984. do_exit(exit_code);
  985. /* NOTREACHED */
  986. }
  987. /*
  988. * this kills every thread in the thread group. Note that any externally
  989. * wait4()-ing process will get the correct exit code - even if this
  990. * thread is not the thread group leader.
  991. */
  992. SYSCALL_DEFINE1(exit_group, int, error_code)
  993. {
  994. do_group_exit((error_code & 0xff) << 8);
  995. /* NOTREACHED */
  996. return 0;
  997. }
  998. struct wait_opts {
  999. enum pid_type wo_type;
  1000. int wo_flags;
  1001. struct pid *wo_pid;
  1002. struct siginfo __user *wo_info;
  1003. int __user *wo_stat;
  1004. struct rusage __user *wo_rusage;
  1005. wait_queue_t child_wait;
  1006. int notask_error;
  1007. };
  1008. static inline
  1009. struct pid *task_pid_type(struct task_struct *task, enum pid_type type)
  1010. {
  1011. if (type != PIDTYPE_PID)
  1012. task = task->group_leader;
  1013. return task->pids[type].pid;
  1014. }
  1015. static int eligible_pid(struct wait_opts *wo, struct task_struct *p)
  1016. {
  1017. return wo->wo_type == PIDTYPE_MAX ||
  1018. task_pid_type(p, wo->wo_type) == wo->wo_pid;
  1019. }
  1020. static int eligible_child(struct wait_opts *wo, struct task_struct *p)
  1021. {
  1022. if (!eligible_pid(wo, p))
  1023. return 0;
  1024. /* Wait for all children (clone and not) if __WALL is set;
  1025. * otherwise, wait for clone children *only* if __WCLONE is
  1026. * set; otherwise, wait for non-clone children *only*. (Note:
  1027. * A "clone" child here is one that reports to its parent
  1028. * using a signal other than SIGCHLD.) */
  1029. if (((p->exit_signal != SIGCHLD) ^ !!(wo->wo_flags & __WCLONE))
  1030. && !(wo->wo_flags & __WALL))
  1031. return 0;
  1032. return 1;
  1033. }
  1034. static int wait_noreap_copyout(struct wait_opts *wo, struct task_struct *p,
  1035. pid_t pid, uid_t uid, int why, int status)
  1036. {
  1037. struct siginfo __user *infop;
  1038. int retval = wo->wo_rusage
  1039. ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
  1040. put_task_struct(p);
  1041. infop = wo->wo_info;
  1042. if (infop) {
  1043. if (!retval)
  1044. retval = put_user(SIGCHLD, &infop->si_signo);
  1045. if (!retval)
  1046. retval = put_user(0, &infop->si_errno);
  1047. if (!retval)
  1048. retval = put_user((short)why, &infop->si_code);
  1049. if (!retval)
  1050. retval = put_user(pid, &infop->si_pid);
  1051. if (!retval)
  1052. retval = put_user(uid, &infop->si_uid);
  1053. if (!retval)
  1054. retval = put_user(status, &infop->si_status);
  1055. }
  1056. if (!retval)
  1057. retval = pid;
  1058. return retval;
  1059. }
  1060. /*
  1061. * Handle sys_wait4 work for one task in state EXIT_ZOMBIE. We hold
  1062. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  1063. * the lock and this task is uninteresting. If we return nonzero, we have
  1064. * released the lock and the system call should return.
  1065. */
  1066. static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
  1067. {
  1068. unsigned long state;
  1069. int retval, status, traced;
  1070. pid_t pid = task_pid_vnr(p);
  1071. uid_t uid = from_kuid_munged(current_user_ns(), task_uid(p));
  1072. struct siginfo __user *infop;
  1073. if (!likely(wo->wo_flags & WEXITED))
  1074. return 0;
  1075. if (unlikely(wo->wo_flags & WNOWAIT)) {
  1076. int exit_code = p->exit_code;
  1077. int why;
  1078. get_task_struct(p);
  1079. read_unlock(&tasklist_lock);
  1080. if ((exit_code & 0x7f) == 0) {
  1081. why = CLD_EXITED;
  1082. status = exit_code >> 8;
  1083. } else {
  1084. why = (exit_code & 0x80) ? CLD_DUMPED : CLD_KILLED;
  1085. status = exit_code & 0x7f;
  1086. }
  1087. return wait_noreap_copyout(wo, p, pid, uid, why, status);
  1088. }
  1089. /*
  1090. * Try to move the task's state to DEAD
  1091. * only one thread is allowed to do this:
  1092. */
  1093. state = xchg(&p->exit_state, EXIT_DEAD);
  1094. if (state != EXIT_ZOMBIE) {
  1095. BUG_ON(state != EXIT_DEAD);
  1096. return 0;
  1097. }
  1098. traced = ptrace_reparented(p);
  1099. /*
  1100. * It can be ptraced but not reparented, check
  1101. * thread_group_leader() to filter out sub-threads.
  1102. */
  1103. if (likely(!traced) && thread_group_leader(p)) {
  1104. struct signal_struct *psig;
  1105. struct signal_struct *sig;
  1106. unsigned long maxrss;
  1107. cputime_t tgutime, tgstime;
  1108. /*
  1109. * The resource counters for the group leader are in its
  1110. * own task_struct. Those for dead threads in the group
  1111. * are in its signal_struct, as are those for the child
  1112. * processes it has previously reaped. All these
  1113. * accumulate in the parent's signal_struct c* fields.
  1114. *
  1115. * We don't bother to take a lock here to protect these
  1116. * p->signal fields, because they are only touched by
  1117. * __exit_signal, which runs with tasklist_lock
  1118. * write-locked anyway, and so is excluded here. We do
  1119. * need to protect the access to parent->signal fields,
  1120. * as other threads in the parent group can be right
  1121. * here reaping other children at the same time.
  1122. *
  1123. * We use thread_group_cputime_adjusted() to get times for the thread
  1124. * group, which consolidates times for all threads in the
  1125. * group including the group leader.
  1126. */
  1127. thread_group_cputime_adjusted(p, &tgutime, &tgstime);
  1128. spin_lock_irq(&p->real_parent->sighand->siglock);
  1129. psig = p->real_parent->signal;
  1130. sig = p->signal;
  1131. psig->cutime += tgutime + sig->cutime;
  1132. psig->cstime += tgstime + sig->cstime;
  1133. psig->cgtime += p->gtime + sig->gtime + sig->cgtime;
  1134. psig->cmin_flt +=
  1135. p->min_flt + sig->min_flt + sig->cmin_flt;
  1136. psig->cmaj_flt +=
  1137. p->maj_flt + sig->maj_flt + sig->cmaj_flt;
  1138. psig->cnvcsw +=
  1139. p->nvcsw + sig->nvcsw + sig->cnvcsw;
  1140. psig->cnivcsw +=
  1141. p->nivcsw + sig->nivcsw + sig->cnivcsw;
  1142. psig->cinblock +=
  1143. task_io_get_inblock(p) +
  1144. sig->inblock + sig->cinblock;
  1145. psig->coublock +=
  1146. task_io_get_oublock(p) +
  1147. sig->oublock + sig->coublock;
  1148. maxrss = max(sig->maxrss, sig->cmaxrss);
  1149. if (psig->cmaxrss < maxrss)
  1150. psig->cmaxrss = maxrss;
  1151. task_io_accounting_add(&psig->ioac, &p->ioac);
  1152. task_io_accounting_add(&psig->ioac, &sig->ioac);
  1153. spin_unlock_irq(&p->real_parent->sighand->siglock);
  1154. }
  1155. /*
  1156. * Now we are sure this task is interesting, and no other
  1157. * thread can reap it because we set its state to EXIT_DEAD.
  1158. */
  1159. read_unlock(&tasklist_lock);
  1160. retval = wo->wo_rusage
  1161. ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
  1162. status = (p->signal->flags & SIGNAL_GROUP_EXIT)
  1163. ? p->signal->group_exit_code : p->exit_code;
  1164. if (!retval && wo->wo_stat)
  1165. retval = put_user(status, wo->wo_stat);
  1166. infop = wo->wo_info;
  1167. if (!retval && infop)
  1168. retval = put_user(SIGCHLD, &infop->si_signo);
  1169. if (!retval && infop)
  1170. retval = put_user(0, &infop->si_errno);
  1171. if (!retval && infop) {
  1172. int why;
  1173. if ((status & 0x7f) == 0) {
  1174. why = CLD_EXITED;
  1175. status >>= 8;
  1176. } else {
  1177. why = (status & 0x80) ? CLD_DUMPED : CLD_KILLED;
  1178. status &= 0x7f;
  1179. }
  1180. retval = put_user((short)why, &infop->si_code);
  1181. if (!retval)
  1182. retval = put_user(status, &infop->si_status);
  1183. }
  1184. if (!retval && infop)
  1185. retval = put_user(pid, &infop->si_pid);
  1186. if (!retval && infop)
  1187. retval = put_user(uid, &infop->si_uid);
  1188. if (!retval)
  1189. retval = pid;
  1190. if (traced) {
  1191. write_lock_irq(&tasklist_lock);
  1192. /* We dropped tasklist, ptracer could die and untrace */
  1193. ptrace_unlink(p);
  1194. /*
  1195. * If this is not a sub-thread, notify the parent.
  1196. * If parent wants a zombie, don't release it now.
  1197. */
  1198. if (thread_group_leader(p) &&
  1199. !do_notify_parent(p, p->exit_signal)) {
  1200. p->exit_state = EXIT_ZOMBIE;
  1201. p = NULL;
  1202. }
  1203. write_unlock_irq(&tasklist_lock);
  1204. }
  1205. if (p != NULL)
  1206. release_task(p);
  1207. return retval;
  1208. }
  1209. static int *task_stopped_code(struct task_struct *p, bool ptrace)
  1210. {
  1211. if (ptrace) {
  1212. if (task_is_stopped_or_traced(p) &&
  1213. !(p->jobctl & JOBCTL_LISTENING))
  1214. return &p->exit_code;
  1215. } else {
  1216. if (p->signal->flags & SIGNAL_STOP_STOPPED)
  1217. return &p->signal->group_exit_code;
  1218. }
  1219. return NULL;
  1220. }
  1221. /**
  1222. * wait_task_stopped - Wait for %TASK_STOPPED or %TASK_TRACED
  1223. * @wo: wait options
  1224. * @ptrace: is the wait for ptrace
  1225. * @p: task to wait for
  1226. *
  1227. * Handle sys_wait4() work for %p in state %TASK_STOPPED or %TASK_TRACED.
  1228. *
  1229. * CONTEXT:
  1230. * read_lock(&tasklist_lock), which is released if return value is
  1231. * non-zero. Also, grabs and releases @p->sighand->siglock.
  1232. *
  1233. * RETURNS:
  1234. * 0 if wait condition didn't exist and search for other wait conditions
  1235. * should continue. Non-zero return, -errno on failure and @p's pid on
  1236. * success, implies that tasklist_lock is released and wait condition
  1237. * search should terminate.
  1238. */
  1239. static int wait_task_stopped(struct wait_opts *wo,
  1240. int ptrace, struct task_struct *p)
  1241. {
  1242. struct siginfo __user *infop;
  1243. int retval, exit_code, *p_code, why;
  1244. uid_t uid = 0; /* unneeded, required by compiler */
  1245. pid_t pid;
  1246. /*
  1247. * Traditionally we see ptrace'd stopped tasks regardless of options.
  1248. */
  1249. if (!ptrace && !(wo->wo_flags & WUNTRACED))
  1250. return 0;
  1251. if (!task_stopped_code(p, ptrace))
  1252. return 0;
  1253. exit_code = 0;
  1254. spin_lock_irq(&p->sighand->siglock);
  1255. p_code = task_stopped_code(p, ptrace);
  1256. if (unlikely(!p_code))
  1257. goto unlock_sig;
  1258. exit_code = *p_code;
  1259. if (!exit_code)
  1260. goto unlock_sig;
  1261. if (!unlikely(wo->wo_flags & WNOWAIT))
  1262. *p_code = 0;
  1263. uid = from_kuid_munged(current_user_ns(), __task_cred(p)->uid);
  1264. unlock_sig:
  1265. spin_unlock_irq(&p->sighand->siglock);
  1266. if (!exit_code)
  1267. return 0;
  1268. /*
  1269. * Now we are pretty sure this task is interesting.
  1270. * Make sure it doesn't get reaped out from under us while we
  1271. * give up the lock and then examine it below. We don't want to
  1272. * keep holding onto the tasklist_lock while we call getrusage and
  1273. * possibly take page faults for user memory.
  1274. */
  1275. get_task_struct(p);
  1276. pid = task_pid_vnr(p);
  1277. why = ptrace ? CLD_TRAPPED : CLD_STOPPED;
  1278. read_unlock(&tasklist_lock);
  1279. if (unlikely(wo->wo_flags & WNOWAIT))
  1280. return wait_noreap_copyout(wo, p, pid, uid, why, exit_code);
  1281. retval = wo->wo_rusage
  1282. ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
  1283. if (!retval && wo->wo_stat)
  1284. retval = put_user((exit_code << 8) | 0x7f, wo->wo_stat);
  1285. infop = wo->wo_info;
  1286. if (!retval && infop)
  1287. retval = put_user(SIGCHLD, &infop->si_signo);
  1288. if (!retval && infop)
  1289. retval = put_user(0, &infop->si_errno);
  1290. if (!retval && infop)
  1291. retval = put_user((short)why, &infop->si_code);
  1292. if (!retval && infop)
  1293. retval = put_user(exit_code, &infop->si_status);
  1294. if (!retval && infop)
  1295. retval = put_user(pid, &infop->si_pid);
  1296. if (!retval && infop)
  1297. retval = put_user(uid, &infop->si_uid);
  1298. if (!retval)
  1299. retval = pid;
  1300. put_task_struct(p);
  1301. BUG_ON(!retval);
  1302. return retval;
  1303. }
  1304. /*
  1305. * Handle do_wait work for one task in a live, non-stopped state.
  1306. * read_lock(&tasklist_lock) on entry. If we return zero, we still hold
  1307. * the lock and this task is uninteresting. If we return nonzero, we have
  1308. * released the lock and the system call should return.
  1309. */
  1310. static int wait_task_continued(struct wait_opts *wo, struct task_struct *p)
  1311. {
  1312. int retval;
  1313. pid_t pid;
  1314. uid_t uid;
  1315. if (!unlikely(wo->wo_flags & WCONTINUED))
  1316. return 0;
  1317. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
  1318. return 0;
  1319. spin_lock_irq(&p->sighand->siglock);
  1320. /* Re-check with the lock held. */
  1321. if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) {
  1322. spin_unlock_irq(&p->sighand->siglock);
  1323. return 0;
  1324. }
  1325. if (!unlikely(wo->wo_flags & WNOWAIT))
  1326. p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
  1327. uid = from_kuid_munged(current_user_ns(), __task_cred(p)->uid);
  1328. spin_unlock_irq(&p->sighand->siglock);
  1329. pid = task_pid_vnr(p);
  1330. get_task_struct(p);
  1331. read_unlock(&tasklist_lock);
  1332. if (!wo->wo_info) {
  1333. retval = wo->wo_rusage
  1334. ? getrusage(p, RUSAGE_BOTH, wo->wo_rusage) : 0;
  1335. put_task_struct(p);
  1336. if (!retval && wo->wo_stat)
  1337. retval = put_user(0xffff, wo->wo_stat);
  1338. if (!retval)
  1339. retval = pid;
  1340. } else {
  1341. retval = wait_noreap_copyout(wo, p, pid, uid,
  1342. CLD_CONTINUED, SIGCONT);
  1343. BUG_ON(retval == 0);
  1344. }
  1345. return retval;
  1346. }
  1347. /*
  1348. * Consider @p for a wait by @parent.
  1349. *
  1350. * -ECHILD should be in ->notask_error before the first call.
  1351. * Returns nonzero for a final return, when we have unlocked tasklist_lock.
  1352. * Returns zero if the search for a child should continue;
  1353. * then ->notask_error is 0 if @p is an eligible child,
  1354. * or another error from security_task_wait(), or still -ECHILD.
  1355. */
  1356. static int wait_consider_task(struct wait_opts *wo, int ptrace,
  1357. struct task_struct *p)
  1358. {
  1359. int ret = eligible_child(wo, p);
  1360. if (!ret)
  1361. return ret;
  1362. ret = security_task_wait(p);
  1363. if (unlikely(ret < 0)) {
  1364. /*
  1365. * If we have not yet seen any eligible child,
  1366. * then let this error code replace -ECHILD.
  1367. * A permission error will give the user a clue
  1368. * to look for security policy problems, rather
  1369. * than for mysterious wait bugs.
  1370. */
  1371. if (wo->notask_error)
  1372. wo->notask_error = ret;
  1373. return 0;
  1374. }
  1375. /* dead body doesn't have much to contribute */
  1376. if (unlikely(p->exit_state == EXIT_DEAD)) {
  1377. /*
  1378. * But do not ignore this task until the tracer does
  1379. * wait_task_zombie()->do_notify_parent().
  1380. */
  1381. if (likely(!ptrace) && unlikely(ptrace_reparented(p)))
  1382. wo->notask_error = 0;
  1383. return 0;
  1384. }
  1385. /* slay zombie? */
  1386. if (p->exit_state == EXIT_ZOMBIE) {
  1387. /*
  1388. * A zombie ptracee is only visible to its ptracer.
  1389. * Notification and reaping will be cascaded to the real
  1390. * parent when the ptracer detaches.
  1391. */
  1392. if (likely(!ptrace) && unlikely(p->ptrace)) {
  1393. /* it will become visible, clear notask_error */
  1394. wo->notask_error = 0;
  1395. return 0;
  1396. }
  1397. /* we don't reap group leaders with subthreads */
  1398. if (!delay_group_leader(p))
  1399. return wait_task_zombie(wo, p);
  1400. /*
  1401. * Allow access to stopped/continued state via zombie by
  1402. * falling through. Clearing of notask_error is complex.
  1403. *
  1404. * When !@ptrace:
  1405. *
  1406. * If WEXITED is set, notask_error should naturally be
  1407. * cleared. If not, subset of WSTOPPED|WCONTINUED is set,
  1408. * so, if there are live subthreads, there are events to
  1409. * wait for. If all subthreads are dead, it's still safe
  1410. * to clear - this function will be called again in finite
  1411. * amount time once all the subthreads are released and
  1412. * will then return without clearing.
  1413. *
  1414. * When @ptrace:
  1415. *
  1416. * Stopped state is per-task and thus can't change once the
  1417. * target task dies. Only continued and exited can happen.
  1418. * Clear notask_error if WCONTINUED | WEXITED.
  1419. */
  1420. if (likely(!ptrace) || (wo->wo_flags & (WCONTINUED | WEXITED)))
  1421. wo->notask_error = 0;
  1422. } else {
  1423. /*
  1424. * If @p is ptraced by a task in its real parent's group,
  1425. * hide group stop/continued state when looking at @p as
  1426. * the real parent; otherwise, a single stop can be
  1427. * reported twice as group and ptrace stops.
  1428. *
  1429. * If a ptracer wants to distinguish the two events for its
  1430. * own children, it should create a separate process which
  1431. * takes the role of real parent.
  1432. */
  1433. if (likely(!ptrace) && p->ptrace && !ptrace_reparented(p))
  1434. return 0;
  1435. /*
  1436. * @p is alive and it's gonna stop, continue or exit, so
  1437. * there always is something to wait for.
  1438. */
  1439. wo->notask_error = 0;
  1440. }
  1441. /*
  1442. * Wait for stopped. Depending on @ptrace, different stopped state
  1443. * is used and the two don't interact with each other.
  1444. */
  1445. ret = wait_task_stopped(wo, ptrace, p);
  1446. if (ret)
  1447. return ret;
  1448. /*
  1449. * Wait for continued. There's only one continued state and the
  1450. * ptracer can consume it which can confuse the real parent. Don't
  1451. * use WCONTINUED from ptracer. You don't need or want it.
  1452. */
  1453. return wait_task_continued(wo, p);
  1454. }
  1455. /*
  1456. * Do the work of do_wait() for one thread in the group, @tsk.
  1457. *
  1458. * -ECHILD should be in ->notask_error before the first call.
  1459. * Returns nonzero for a final return, when we have unlocked tasklist_lock.
  1460. * Returns zero if the search for a child should continue; then
  1461. * ->notask_error is 0 if there were any eligible children,
  1462. * or another error from security_task_wait(), or still -ECHILD.
  1463. */
  1464. static int do_wait_thread(struct wait_opts *wo, struct task_struct *tsk)
  1465. {
  1466. struct task_struct *p;
  1467. list_for_each_entry(p, &tsk->children, sibling) {
  1468. int ret = wait_consider_task(wo, 0, p);
  1469. if (ret)
  1470. return ret;
  1471. }
  1472. return 0;
  1473. }
  1474. static int ptrace_do_wait(struct wait_opts *wo, struct task_struct *tsk)
  1475. {
  1476. struct task_struct *p;
  1477. list_for_each_entry(p, &tsk->ptraced, ptrace_entry) {
  1478. int ret = wait_consider_task(wo, 1, p);
  1479. if (ret)
  1480. return ret;
  1481. }
  1482. return 0;
  1483. }
  1484. static int child_wait_callback(wait_queue_t *wait, unsigned mode,
  1485. int sync, void *key)
  1486. {
  1487. struct wait_opts *wo = container_of(wait, struct wait_opts,
  1488. child_wait);
  1489. struct task_struct *p = key;
  1490. if (!eligible_pid(wo, p))
  1491. return 0;
  1492. if ((wo->wo_flags & __WNOTHREAD) && wait->private != p->parent)
  1493. return 0;
  1494. return default_wake_function(wait, mode, sync, key);
  1495. }
  1496. void __wake_up_parent(struct task_struct *p, struct task_struct *parent)
  1497. {
  1498. __wake_up_sync_key(&parent->signal->wait_chldexit,
  1499. TASK_INTERRUPTIBLE, 1, p);
  1500. }
  1501. static long do_wait(struct wait_opts *wo)
  1502. {
  1503. struct task_struct *tsk;
  1504. int retval;
  1505. trace_sched_process_wait(wo->wo_pid);
  1506. init_waitqueue_func_entry(&wo->child_wait, child_wait_callback);
  1507. wo->child_wait.private = current;
  1508. add_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
  1509. repeat:
  1510. /*
  1511. * If there is nothing that can match our critiera just get out.
  1512. * We will clear ->notask_error to zero if we see any child that
  1513. * might later match our criteria, even if we are not able to reap
  1514. * it yet.
  1515. */
  1516. wo->notask_error = -ECHILD;
  1517. if ((wo->wo_type < PIDTYPE_MAX) &&
  1518. (!wo->wo_pid || hlist_empty(&wo->wo_pid->tasks[wo->wo_type])))
  1519. goto notask;
  1520. set_current_state(TASK_INTERRUPTIBLE);
  1521. read_lock(&tasklist_lock);
  1522. tsk = current;
  1523. do {
  1524. retval = do_wait_thread(wo, tsk);
  1525. if (retval)
  1526. goto end;
  1527. retval = ptrace_do_wait(wo, tsk);
  1528. if (retval)
  1529. goto end;
  1530. if (wo->wo_flags & __WNOTHREAD)
  1531. break;
  1532. } while_each_thread(current, tsk);
  1533. read_unlock(&tasklist_lock);
  1534. notask:
  1535. retval = wo->notask_error;
  1536. if (!retval && !(wo->wo_flags & WNOHANG)) {
  1537. retval = -ERESTARTSYS;
  1538. if (!signal_pending(current)) {
  1539. schedule();
  1540. goto repeat;
  1541. }
  1542. }
  1543. end:
  1544. __set_current_state(TASK_RUNNING);
  1545. remove_wait_queue(&current->signal->wait_chldexit, &wo->child_wait);
  1546. return retval;
  1547. }
  1548. SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
  1549. infop, int, options, struct rusage __user *, ru)
  1550. {
  1551. struct wait_opts wo;
  1552. struct pid *pid = NULL;
  1553. enum pid_type type;
  1554. long ret;
  1555. if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
  1556. return -EINVAL;
  1557. if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
  1558. return -EINVAL;
  1559. switch (which) {
  1560. case P_ALL:
  1561. type = PIDTYPE_MAX;
  1562. break;
  1563. case P_PID:
  1564. type = PIDTYPE_PID;
  1565. if (upid <= 0)
  1566. return -EINVAL;
  1567. break;
  1568. case P_PGID:
  1569. type = PIDTYPE_PGID;
  1570. if (upid <= 0)
  1571. return -EINVAL;
  1572. break;
  1573. default:
  1574. return -EINVAL;
  1575. }
  1576. if (type < PIDTYPE_MAX)
  1577. pid = find_get_pid(upid);
  1578. wo.wo_type = type;
  1579. wo.wo_pid = pid;
  1580. wo.wo_flags = options;
  1581. wo.wo_info = infop;
  1582. wo.wo_stat = NULL;
  1583. wo.wo_rusage = ru;
  1584. ret = do_wait(&wo);
  1585. if (ret > 0) {
  1586. ret = 0;
  1587. } else if (infop) {
  1588. /*
  1589. * For a WNOHANG return, clear out all the fields
  1590. * we would set so the user can easily tell the
  1591. * difference.
  1592. */
  1593. if (!ret)
  1594. ret = put_user(0, &infop->si_signo);
  1595. if (!ret)
  1596. ret = put_user(0, &infop->si_errno);
  1597. if (!ret)
  1598. ret = put_user(0, &infop->si_code);
  1599. if (!ret)
  1600. ret = put_user(0, &infop->si_pid);
  1601. if (!ret)
  1602. ret = put_user(0, &infop->si_uid);
  1603. if (!ret)
  1604. ret = put_user(0, &infop->si_status);
  1605. }
  1606. put_pid(pid);
  1607. /* avoid REGPARM breakage on x86: */
  1608. asmlinkage_protect(5, ret, which, upid, infop, options, ru);
  1609. return ret;
  1610. }
  1611. SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr,
  1612. int, options, struct rusage __user *, ru)
  1613. {
  1614. struct wait_opts wo;
  1615. struct pid *pid = NULL;
  1616. enum pid_type type;
  1617. long ret;
  1618. if (options & ~(WNOHANG|WUNTRACED|WCONTINUED|
  1619. __WNOTHREAD|__WCLONE|__WALL))
  1620. return -EINVAL;
  1621. /* -INT_MIN is not defined */
  1622. if (upid == INT_MIN)
  1623. return -ESRCH;
  1624. if (upid == -1)
  1625. type = PIDTYPE_MAX;
  1626. else if (upid < 0) {
  1627. type = PIDTYPE_PGID;
  1628. pid = find_get_pid(-upid);
  1629. } else if (upid == 0) {
  1630. type = PIDTYPE_PGID;
  1631. pid = get_task_pid(current, PIDTYPE_PGID);
  1632. } else /* upid > 0 */ {
  1633. type = PIDTYPE_PID;
  1634. pid = find_get_pid(upid);
  1635. }
  1636. wo.wo_type = type;
  1637. wo.wo_pid = pid;
  1638. wo.wo_flags = options | WEXITED;
  1639. wo.wo_info = NULL;
  1640. wo.wo_stat = stat_addr;
  1641. wo.wo_rusage = ru;
  1642. ret = do_wait(&wo);
  1643. put_pid(pid);
  1644. /* avoid REGPARM breakage on x86: */
  1645. asmlinkage_protect(4, ret, upid, stat_addr, options, ru);
  1646. return ret;
  1647. }
  1648. #ifdef __ARCH_WANT_SYS_WAITPID
  1649. /*
  1650. * sys_waitpid() remains for compatibility. waitpid() should be
  1651. * implemented by calling sys_wait4() from libc.a.
  1652. */
  1653. SYSCALL_DEFINE3(waitpid, pid_t, pid, int __user *, stat_addr, int, options)
  1654. {
  1655. return sys_wait4(pid, stat_addr, options, NULL);
  1656. }
  1657. #endif