exit.c 46 KB

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