acct.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/kernel/acct.c
  4. *
  5. * BSD Process Accounting for Linux
  6. *
  7. * Author: Marco van Wieringen <mvw@planets.elm.net>
  8. *
  9. * Some code based on ideas and code from:
  10. * Thomas K. Dyas <tdyas@eden.rutgers.edu>
  11. *
  12. * This file implements BSD-style process accounting. Whenever any
  13. * process exits, an accounting record of type "struct acct" is
  14. * written to the file specified with the acct() system call. It is
  15. * up to user-level programs to do useful things with the accounting
  16. * log. The kernel just provides the raw accounting information.
  17. *
  18. * (C) Copyright 1995 - 1997 Marco van Wieringen - ELM Consultancy B.V.
  19. *
  20. * Plugged two leaks. 1) It didn't return acct_file into the free_filps if
  21. * the file happened to be read-only. 2) If the accounting was suspended
  22. * due to the lack of space it happily allowed to reopen it and completely
  23. * lost the old acct_file. 3/10/98, Al Viro.
  24. *
  25. * Now we silently close acct_file on attempt to reopen. Cleaned sys_acct().
  26. * XTerms and EMACS are manifestations of pure evil. 21/10/98, AV.
  27. *
  28. * Fixed a nasty interaction with sys_umount(). If the accounting
  29. * was suspeneded we failed to stop it on umount(). Messy.
  30. * Another one: remount to readonly didn't stop accounting.
  31. * Question: what should we do if we have CAP_SYS_ADMIN but not
  32. * CAP_SYS_PACCT? Current code does the following: umount returns -EBUSY
  33. * unless we are messing with the root. In that case we are getting a
  34. * real mess with do_remount_sb(). 9/11/98, AV.
  35. *
  36. * Fixed a bunch of races (and pair of leaks). Probably not the best way,
  37. * but this one obviously doesn't introduce deadlocks. Later. BTW, found
  38. * one race (and leak) in BSD implementation.
  39. * OK, that's better. ANOTHER race and leak in BSD variant. There always
  40. * is one more bug... 10/11/98, AV.
  41. *
  42. * Oh, fsck... Oopsable SMP race in do_process_acct() - we must hold
  43. * ->mmap_lock to walk the vma list of current->mm. Nasty, since it leaks
  44. * a struct file opened for write. Fixed. 2/6/2000, AV.
  45. */
  46. #include <linux/mm.h>
  47. #include <linux/slab.h>
  48. #include <linux/acct.h>
  49. #include <linux/capability.h>
  50. #include <linux/file.h>
  51. #include <linux/tty.h>
  52. #include <linux/security.h>
  53. #include <linux/vfs.h>
  54. #include <linux/jiffies.h>
  55. #include <linux/times.h>
  56. #include <linux/syscalls.h>
  57. #include <linux/mount.h>
  58. #include <linux/uaccess.h>
  59. #include <linux/sched/cputime.h>
  60. #include <asm/div64.h>
  61. #include <linux/pid_namespace.h>
  62. #include <linux/fs_pin.h>
  63. /*
  64. * These constants control the amount of freespace that suspend and
  65. * resume the process accounting system, and the time delay between
  66. * each check.
  67. * Turned into sysctl-controllable parameters. AV, 12/11/98
  68. */
  69. static int acct_parm[3] = {4, 2, 30};
  70. #define RESUME (acct_parm[0]) /* >foo% free space - resume */
  71. #define SUSPEND (acct_parm[1]) /* <foo% free space - suspend */
  72. #define ACCT_TIMEOUT (acct_parm[2]) /* foo second timeout between checks */
  73. #ifdef CONFIG_SYSCTL
  74. static struct ctl_table kern_acct_table[] = {
  75. {
  76. .procname = "acct",
  77. .data = &acct_parm,
  78. .maxlen = 3*sizeof(int),
  79. .mode = 0644,
  80. .proc_handler = proc_dointvec,
  81. },
  82. };
  83. static __init int kernel_acct_sysctls_init(void)
  84. {
  85. register_sysctl_init("kernel", kern_acct_table);
  86. return 0;
  87. }
  88. late_initcall(kernel_acct_sysctls_init);
  89. #endif /* CONFIG_SYSCTL */
  90. /*
  91. * External references and all of the globals.
  92. */
  93. struct bsd_acct_struct {
  94. struct fs_pin pin;
  95. atomic_long_t count;
  96. struct rcu_head rcu;
  97. struct mutex lock;
  98. bool active;
  99. bool check_space;
  100. unsigned long needcheck;
  101. struct file *file;
  102. struct pid_namespace *ns;
  103. struct work_struct work;
  104. struct completion done;
  105. acct_t ac;
  106. };
  107. static void fill_ac(struct bsd_acct_struct *acct);
  108. static void acct_write_process(struct bsd_acct_struct *acct);
  109. /*
  110. * Check the amount of free space and suspend/resume accordingly.
  111. */
  112. static bool check_free_space(struct bsd_acct_struct *acct)
  113. {
  114. struct kstatfs sbuf;
  115. if (!acct->check_space)
  116. return acct->active;
  117. /* May block */
  118. if (vfs_statfs(&acct->file->f_path, &sbuf))
  119. return acct->active;
  120. if (acct->active) {
  121. u64 suspend = sbuf.f_blocks * SUSPEND;
  122. do_div(suspend, 100);
  123. if (sbuf.f_bavail <= suspend) {
  124. acct->active = false;
  125. pr_info("Process accounting paused\n");
  126. }
  127. } else {
  128. u64 resume = sbuf.f_blocks * RESUME;
  129. do_div(resume, 100);
  130. if (sbuf.f_bavail >= resume) {
  131. acct->active = true;
  132. pr_info("Process accounting resumed\n");
  133. }
  134. }
  135. acct->needcheck = jiffies + ACCT_TIMEOUT*HZ;
  136. return acct->active;
  137. }
  138. static void acct_put(struct bsd_acct_struct *p)
  139. {
  140. if (atomic_long_dec_and_test(&p->count))
  141. kfree_rcu(p, rcu);
  142. }
  143. static inline struct bsd_acct_struct *to_acct(struct fs_pin *p)
  144. {
  145. return p ? container_of(p, struct bsd_acct_struct, pin) : NULL;
  146. }
  147. static struct bsd_acct_struct *acct_get(struct pid_namespace *ns)
  148. {
  149. struct bsd_acct_struct *res;
  150. again:
  151. smp_rmb();
  152. rcu_read_lock();
  153. res = to_acct(READ_ONCE(ns->bacct));
  154. if (!res) {
  155. rcu_read_unlock();
  156. return NULL;
  157. }
  158. if (!atomic_long_inc_not_zero(&res->count)) {
  159. rcu_read_unlock();
  160. cpu_relax();
  161. goto again;
  162. }
  163. rcu_read_unlock();
  164. mutex_lock(&res->lock);
  165. if (res != to_acct(READ_ONCE(ns->bacct))) {
  166. mutex_unlock(&res->lock);
  167. acct_put(res);
  168. goto again;
  169. }
  170. return res;
  171. }
  172. static void acct_pin_kill(struct fs_pin *pin)
  173. {
  174. struct bsd_acct_struct *acct = to_acct(pin);
  175. mutex_lock(&acct->lock);
  176. /*
  177. * Fill the accounting struct with the exiting task's info
  178. * before punting to the workqueue.
  179. */
  180. fill_ac(acct);
  181. schedule_work(&acct->work);
  182. wait_for_completion(&acct->done);
  183. cmpxchg(&acct->ns->bacct, pin, NULL);
  184. mutex_unlock(&acct->lock);
  185. pin_remove(pin);
  186. acct_put(acct);
  187. }
  188. static void close_work(struct work_struct *work)
  189. {
  190. struct bsd_acct_struct *acct = container_of(work, struct bsd_acct_struct, work);
  191. struct file *file = acct->file;
  192. /* We were fired by acct_pin_kill() which holds acct->lock. */
  193. acct_write_process(acct);
  194. if (file->f_op->flush)
  195. file->f_op->flush(file, NULL);
  196. __fput_sync(file);
  197. complete(&acct->done);
  198. }
  199. static int acct_on(struct filename *pathname)
  200. {
  201. struct file *file;
  202. struct vfsmount *mnt, *internal;
  203. struct pid_namespace *ns = task_active_pid_ns(current);
  204. struct bsd_acct_struct *acct;
  205. struct fs_pin *old;
  206. int err;
  207. acct = kzalloc(sizeof(struct bsd_acct_struct), GFP_KERNEL);
  208. if (!acct)
  209. return -ENOMEM;
  210. /* Difference from BSD - they don't do O_APPEND */
  211. file = file_open_name(pathname, O_WRONLY|O_APPEND|O_LARGEFILE, 0);
  212. if (IS_ERR(file)) {
  213. kfree(acct);
  214. return PTR_ERR(file);
  215. }
  216. if (!S_ISREG(file_inode(file)->i_mode)) {
  217. kfree(acct);
  218. filp_close(file, NULL);
  219. return -EACCES;
  220. }
  221. /* Exclude kernel kernel internal filesystems. */
  222. if (file_inode(file)->i_sb->s_flags & (SB_NOUSER | SB_KERNMOUNT)) {
  223. kfree(acct);
  224. filp_close(file, NULL);
  225. return -EINVAL;
  226. }
  227. /* Exclude procfs and sysfs. */
  228. if (file_inode(file)->i_sb->s_iflags & SB_I_USERNS_VISIBLE) {
  229. kfree(acct);
  230. filp_close(file, NULL);
  231. return -EINVAL;
  232. }
  233. if (!(file->f_mode & FMODE_CAN_WRITE)) {
  234. kfree(acct);
  235. filp_close(file, NULL);
  236. return -EIO;
  237. }
  238. internal = mnt_clone_internal(&file->f_path);
  239. if (IS_ERR(internal)) {
  240. kfree(acct);
  241. filp_close(file, NULL);
  242. return PTR_ERR(internal);
  243. }
  244. err = mnt_get_write_access(internal);
  245. if (err) {
  246. mntput(internal);
  247. kfree(acct);
  248. filp_close(file, NULL);
  249. return err;
  250. }
  251. mnt = file->f_path.mnt;
  252. file->f_path.mnt = internal;
  253. atomic_long_set(&acct->count, 1);
  254. init_fs_pin(&acct->pin, acct_pin_kill);
  255. acct->file = file;
  256. acct->needcheck = jiffies;
  257. acct->ns = ns;
  258. mutex_init(&acct->lock);
  259. INIT_WORK(&acct->work, close_work);
  260. init_completion(&acct->done);
  261. mutex_lock_nested(&acct->lock, 1); /* nobody has seen it yet */
  262. pin_insert(&acct->pin, mnt);
  263. rcu_read_lock();
  264. old = xchg(&ns->bacct, &acct->pin);
  265. mutex_unlock(&acct->lock);
  266. pin_kill(old);
  267. mnt_put_write_access(mnt);
  268. mntput(mnt);
  269. return 0;
  270. }
  271. static DEFINE_MUTEX(acct_on_mutex);
  272. /**
  273. * sys_acct - enable/disable process accounting
  274. * @name: file name for accounting records or NULL to shutdown accounting
  275. *
  276. * sys_acct() is the only system call needed to implement process
  277. * accounting. It takes the name of the file where accounting records
  278. * should be written. If the filename is NULL, accounting will be
  279. * shutdown.
  280. *
  281. * Returns: 0 for success or negative errno values for failure.
  282. */
  283. SYSCALL_DEFINE1(acct, const char __user *, name)
  284. {
  285. int error = 0;
  286. if (!capable(CAP_SYS_PACCT))
  287. return -EPERM;
  288. if (name) {
  289. struct filename *tmp = getname(name);
  290. if (IS_ERR(tmp))
  291. return PTR_ERR(tmp);
  292. mutex_lock(&acct_on_mutex);
  293. error = acct_on(tmp);
  294. mutex_unlock(&acct_on_mutex);
  295. putname(tmp);
  296. } else {
  297. rcu_read_lock();
  298. pin_kill(task_active_pid_ns(current)->bacct);
  299. }
  300. return error;
  301. }
  302. void acct_exit_ns(struct pid_namespace *ns)
  303. {
  304. rcu_read_lock();
  305. pin_kill(ns->bacct);
  306. }
  307. /*
  308. * encode an u64 into a comp_t
  309. *
  310. * This routine has been adopted from the encode_comp_t() function in
  311. * the kern_acct.c file of the FreeBSD operating system. The encoding
  312. * is a 13-bit fraction with a 3-bit (base 8) exponent.
  313. */
  314. #define MANTSIZE 13 /* 13 bit mantissa. */
  315. #define EXPSIZE 3 /* Base 8 (3 bit) exponent. */
  316. #define MAXFRACT ((1 << MANTSIZE) - 1) /* Maximum fractional value. */
  317. static comp_t encode_comp_t(u64 value)
  318. {
  319. int exp, rnd;
  320. exp = rnd = 0;
  321. while (value > MAXFRACT) {
  322. rnd = value & (1 << (EXPSIZE - 1)); /* Round up? */
  323. value >>= EXPSIZE; /* Base 8 exponent == 3 bit shift. */
  324. exp++;
  325. }
  326. /*
  327. * If we need to round up, do it (and handle overflow correctly).
  328. */
  329. if (rnd && (++value > MAXFRACT)) {
  330. value >>= EXPSIZE;
  331. exp++;
  332. }
  333. if (exp > (((comp_t) ~0U) >> MANTSIZE))
  334. return (comp_t) ~0U;
  335. /*
  336. * Clean it up and polish it off.
  337. */
  338. exp <<= MANTSIZE; /* Shift the exponent into place */
  339. exp += value; /* and add on the mantissa. */
  340. return exp;
  341. }
  342. #if ACCT_VERSION == 1 || ACCT_VERSION == 2
  343. /*
  344. * encode an u64 into a comp2_t (24 bits)
  345. *
  346. * Format: 5 bit base 2 exponent, 20 bits mantissa.
  347. * The leading bit of the mantissa is not stored, but implied for
  348. * non-zero exponents.
  349. * Largest encodable value is 50 bits.
  350. */
  351. #define MANTSIZE2 20 /* 20 bit mantissa. */
  352. #define EXPSIZE2 5 /* 5 bit base 2 exponent. */
  353. #define MAXFRACT2 ((1ul << MANTSIZE2) - 1) /* Maximum fractional value. */
  354. #define MAXEXP2 ((1 << EXPSIZE2) - 1) /* Maximum exponent. */
  355. static comp2_t encode_comp2_t(u64 value)
  356. {
  357. int exp, rnd;
  358. exp = (value > (MAXFRACT2>>1));
  359. rnd = 0;
  360. while (value > MAXFRACT2) {
  361. rnd = value & 1;
  362. value >>= 1;
  363. exp++;
  364. }
  365. /*
  366. * If we need to round up, do it (and handle overflow correctly).
  367. */
  368. if (rnd && (++value > MAXFRACT2)) {
  369. value >>= 1;
  370. exp++;
  371. }
  372. if (exp > MAXEXP2) {
  373. /* Overflow. Return largest representable number instead. */
  374. return (1ul << (MANTSIZE2+EXPSIZE2-1)) - 1;
  375. } else {
  376. return (value & (MAXFRACT2>>1)) | (exp << (MANTSIZE2-1));
  377. }
  378. }
  379. #elif ACCT_VERSION == 3
  380. /*
  381. * encode an u64 into a 32 bit IEEE float
  382. */
  383. static u32 encode_float(u64 value)
  384. {
  385. unsigned exp = 190;
  386. unsigned u;
  387. if (value == 0)
  388. return 0;
  389. while ((s64)value > 0) {
  390. value <<= 1;
  391. exp--;
  392. }
  393. u = (u32)(value >> 40) & 0x7fffffu;
  394. return u | (exp << 23);
  395. }
  396. #endif
  397. /*
  398. * Write an accounting entry for an exiting process
  399. *
  400. * The acct_process() call is the workhorse of the process
  401. * accounting system. The struct acct is built here and then written
  402. * into the accounting file. This function should only be called from
  403. * do_exit() or when switching to a different output file.
  404. */
  405. static void fill_ac(struct bsd_acct_struct *acct)
  406. {
  407. struct pacct_struct *pacct = &current->signal->pacct;
  408. struct file *file = acct->file;
  409. acct_t *ac = &acct->ac;
  410. u64 elapsed, run_time;
  411. time64_t btime;
  412. struct tty_struct *tty;
  413. lockdep_assert_held(&acct->lock);
  414. if (time_is_after_jiffies(acct->needcheck)) {
  415. acct->check_space = false;
  416. /* Don't fill in @ac if nothing will be written. */
  417. if (!acct->active)
  418. return;
  419. } else {
  420. acct->check_space = true;
  421. }
  422. /*
  423. * Fill the accounting struct with the needed info as recorded
  424. * by the different kernel functions.
  425. */
  426. memset(ac, 0, sizeof(acct_t));
  427. ac->ac_version = ACCT_VERSION | ACCT_BYTEORDER;
  428. strscpy(ac->ac_comm, current->comm, sizeof(ac->ac_comm));
  429. /* calculate run_time in nsec*/
  430. run_time = ktime_get_ns();
  431. run_time -= current->group_leader->start_time;
  432. /* convert nsec -> AHZ */
  433. elapsed = nsec_to_AHZ(run_time);
  434. #if ACCT_VERSION == 3
  435. ac->ac_etime = encode_float(elapsed);
  436. #else
  437. ac->ac_etime = encode_comp_t(elapsed < (unsigned long) -1l ?
  438. (unsigned long) elapsed : (unsigned long) -1l);
  439. #endif
  440. #if ACCT_VERSION == 1 || ACCT_VERSION == 2
  441. {
  442. /* new enlarged etime field */
  443. comp2_t etime = encode_comp2_t(elapsed);
  444. ac->ac_etime_hi = etime >> 16;
  445. ac->ac_etime_lo = (u16) etime;
  446. }
  447. #endif
  448. do_div(elapsed, AHZ);
  449. btime = ktime_get_real_seconds() - elapsed;
  450. ac->ac_btime = clamp_t(time64_t, btime, 0, U32_MAX);
  451. #if ACCT_VERSION == 2
  452. ac->ac_ahz = AHZ;
  453. #endif
  454. spin_lock_irq(&current->sighand->siglock);
  455. tty = current->signal->tty; /* Safe as we hold the siglock */
  456. ac->ac_tty = tty ? old_encode_dev(tty_devnum(tty)) : 0;
  457. ac->ac_utime = encode_comp_t(nsec_to_AHZ(pacct->ac_utime));
  458. ac->ac_stime = encode_comp_t(nsec_to_AHZ(pacct->ac_stime));
  459. ac->ac_flag = pacct->ac_flag;
  460. ac->ac_mem = encode_comp_t(pacct->ac_mem);
  461. ac->ac_minflt = encode_comp_t(pacct->ac_minflt);
  462. ac->ac_majflt = encode_comp_t(pacct->ac_majflt);
  463. ac->ac_exitcode = pacct->ac_exitcode;
  464. spin_unlock_irq(&current->sighand->siglock);
  465. /* we really need to bite the bullet and change layout */
  466. ac->ac_uid = from_kuid_munged(file->f_cred->user_ns, current_uid());
  467. ac->ac_gid = from_kgid_munged(file->f_cred->user_ns, current_gid());
  468. #if ACCT_VERSION == 1 || ACCT_VERSION == 2
  469. /* backward-compatible 16 bit fields */
  470. ac->ac_uid16 = ac->ac_uid;
  471. ac->ac_gid16 = ac->ac_gid;
  472. #elif ACCT_VERSION == 3
  473. {
  474. struct pid_namespace *ns = acct->ns;
  475. ac->ac_pid = task_tgid_nr_ns(current, ns);
  476. rcu_read_lock();
  477. ac->ac_ppid = task_tgid_nr_ns(rcu_dereference(current->real_parent), ns);
  478. rcu_read_unlock();
  479. }
  480. #endif
  481. }
  482. static void acct_write_process(struct bsd_acct_struct *acct)
  483. {
  484. struct file *file = acct->file;
  485. const struct cred *cred;
  486. acct_t *ac = &acct->ac;
  487. /* Perform file operations on behalf of whoever enabled accounting */
  488. cred = override_creds(file->f_cred);
  489. /*
  490. * First check to see if there is enough free_space to continue
  491. * the process accounting system. Then get freeze protection. If
  492. * the fs is frozen, just skip the write as we could deadlock
  493. * the system otherwise.
  494. */
  495. if (check_free_space(acct) && file_start_write_trylock(file)) {
  496. /* it's been opened O_APPEND, so position is irrelevant */
  497. loff_t pos = 0;
  498. __kernel_write(file, ac, sizeof(acct_t), &pos);
  499. file_end_write(file);
  500. }
  501. revert_creds(cred);
  502. }
  503. static void do_acct_process(struct bsd_acct_struct *acct)
  504. {
  505. unsigned long flim;
  506. /* Accounting records are not subject to resource limits. */
  507. flim = rlimit(RLIMIT_FSIZE);
  508. current->signal->rlim[RLIMIT_FSIZE].rlim_cur = RLIM_INFINITY;
  509. fill_ac(acct);
  510. acct_write_process(acct);
  511. current->signal->rlim[RLIMIT_FSIZE].rlim_cur = flim;
  512. }
  513. /**
  514. * acct_collect - collect accounting information into pacct_struct
  515. * @exitcode: task exit code
  516. * @group_dead: not 0, if this thread is the last one in the process.
  517. */
  518. void acct_collect(long exitcode, int group_dead)
  519. {
  520. struct pacct_struct *pacct = &current->signal->pacct;
  521. u64 utime, stime;
  522. unsigned long vsize = 0;
  523. if (group_dead && current->mm) {
  524. struct mm_struct *mm = current->mm;
  525. VMA_ITERATOR(vmi, mm, 0);
  526. struct vm_area_struct *vma;
  527. mmap_read_lock(mm);
  528. for_each_vma(vmi, vma)
  529. vsize += vma->vm_end - vma->vm_start;
  530. mmap_read_unlock(mm);
  531. }
  532. spin_lock_irq(&current->sighand->siglock);
  533. if (group_dead)
  534. pacct->ac_mem = vsize / 1024;
  535. if (thread_group_leader(current)) {
  536. pacct->ac_exitcode = exitcode;
  537. if (current->flags & PF_FORKNOEXEC)
  538. pacct->ac_flag |= AFORK;
  539. }
  540. if (current->flags & PF_SUPERPRIV)
  541. pacct->ac_flag |= ASU;
  542. if (current->flags & PF_DUMPCORE)
  543. pacct->ac_flag |= ACORE;
  544. if (current->flags & PF_SIGNALED)
  545. pacct->ac_flag |= AXSIG;
  546. task_cputime(current, &utime, &stime);
  547. pacct->ac_utime += utime;
  548. pacct->ac_stime += stime;
  549. pacct->ac_minflt += current->min_flt;
  550. pacct->ac_majflt += current->maj_flt;
  551. spin_unlock_irq(&current->sighand->siglock);
  552. }
  553. static void slow_acct_process(struct pid_namespace *ns)
  554. {
  555. for ( ; ns; ns = ns->parent) {
  556. struct bsd_acct_struct *acct = acct_get(ns);
  557. if (acct) {
  558. do_acct_process(acct);
  559. mutex_unlock(&acct->lock);
  560. acct_put(acct);
  561. }
  562. }
  563. }
  564. /**
  565. * acct_process - handles process accounting for an exiting task
  566. */
  567. void acct_process(void)
  568. {
  569. struct pid_namespace *ns;
  570. /*
  571. * This loop is safe lockless, since current is still
  572. * alive and holds its namespace, which in turn holds
  573. * its parent.
  574. */
  575. for (ns = task_active_pid_ns(current); ns != NULL; ns = ns->parent) {
  576. if (ns->bacct)
  577. break;
  578. }
  579. if (unlikely(ns))
  580. slow_acct_process(ns);
  581. }