exit.c 43 KB

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