Skip to contentSkip to conversation

Add pacct_struct to fix some pacct bugs.

PATCH KaiGai Kohei 3 participants 10 messages Jun 20, 2006 – Jun 22, 2006

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

Hi, I noticed three problems in pacct facility.

1. Pacct facility has a possibility to write incorrect ac_flag in multi-threading cases.
2. There is a possibility to be waken up OOM Killer from pacct facility. It will cause system stall.
3. If several threads are killed at same time, There is a possibility not to pick up a part of those accountings.

The attached patch will resolve those matters.
Any comments please. Thanks,

[The details of those matters]

(1) about incorrect ac_flag
When pacct facility generate an 'ac_flag' field in accounting record, it refers a task_struct of the thread which died last in the process. But any other task_structs are ignored.

Therefore, pacct facility drops ASU flag even if root-privilege operations are used by any other threads except the last one. In addition, AFORK flag is always set when the thread of group-leader didn't die last, although this process has called execve() after fork().

---- in kernel/acct.c : do_acct_process() ----

        ac.ac_flag = 0;
        if (current->flags & PF_FORKNOEXEC)
                ac.ac_flag |= AFORK;
        if (current->flags & PF_SUPERPRIV)
                ac.ac_flag |= ASU;
        if (current->flags & PF_DUMPCORE)
                ac.ac_flag |= ACORE;
        if (current->flags & PF_SIGNALED)
                ac.ac_flag |= AXSIG;

----------------------------------------------
I think ASU, ACORE, AXSIG flag should be set if any task_structs satisfy the conditions. And, AFORK flag should not be checked for any threads except the group leader.

(2) about OOM killer
The pacct facility need an i/o operation when an accounting record is generated. There is a possibility to wake OOM killer up. If OOM killer is activated, it kills some processes to make them release process memory regions.
But acct_process() is called in the killed processes context before calling exit_mm(), so those processes cannot release own memory. In the results, any processes stop in this point and it finally cause a system stall.

---- in kernel/exit.c : do_exit() ------------

        group_dead = atomic_dec_and_test(&tsk->signal->live);
        if (group_dead) {
                hrtimer_cancel(&tsk->signal->real_timer);
                exit_itimers(tsk->signal);
                acct_process(code);
        }
           :
       - snip -
           :
        exit_mm(tsk);

----------------------------------------------
I think acct_process() should be called after exit_mm() to avoid this matter. As mm_struct is necessary to calculate ac_mem field, it should be kept before exit_mm().

(3) about race condition in pacct
In current 2.6.17 implementation, signal_struct refered from task_struct is used for per-process data structure. The pacct facility also uses it as a per-process data structure to save stime, utime, minflt, majflt. But those members are saved in __exit_signal(). It's too late.

For example, if some threads exits at same time, pacct facility has a possibility to drop accountings for a part of those threads. (see, the following 'The results of original 2.6.17 kernel') I think accounting information should be completely collected into the per-process data structure before writing out an accounting record.

[The solution for those matters]
a. pacct_struct is newly defined, and it's deployed into signal_struct
as a per-process accounting storage.
b. acct_collect() is newly added to collect any thread's accountings
before generating accounting record and calling exit_mm().
c. Calling acct_process() is moved after exit_mm().
d. do_acct_process() becomes to refer the per-process accounting
structure, not task_strcut of the last thread.

[The results of original 2.6.17 kernel]
* test for ac_flag

  # time -p ./bugacct
  real 10.11
  user 5.94
  sys 0.17
  # touch /tmp/acct-`uname -r`
  # accton /tmp/acct-`uname -r`
  # time -p ./bugacct
  real 10.04
  user 5.86
  sys 0.17
  -- The accounting results -----------------------------------------
  FLAG  BTIME          ETIME UTIME STIME    MEM MINFLT MAJFLT    COMM
  F---  06/20-14:31:47  1003   586    16 140992  32776      0 bugacct

(*) 'F' means it didn't execve() after fork().
'P' means it used root-privilege operations.

=> original 2.6.17 drops 'P' flag, and appends undesired 'F' flag.

* test for the race condition

  # time -p ./raceacct 4
  real 2.59
  user 7.56
  sys 0.00
  # time -p ./raceacct 4
  real 2.27
  user 6.62
  sys 0.00
  # time -p ./raceacct 4
  real 2.16
  user 6.16
  sys 0.00
  # time -p ./raceacct 4
  real 2.87
  user 8.36
  sys 0.00
  # time -p ./raceacct 4
  real 2.77
  user 8.11
  sys 0.00

-- The accounting results -----------------------------------------

  FLAG  BTIME          ETIME UTIME STIME    MEM MINFLT MAJFLT    COMM
  F---  06/20-11:18:57   259   596     0  28528     38      0 raceacct *
  ----  06/20-11:19:14   227   662     0  28528    171      0 raceacct
  F---  06/20-11:19:18   216   509     0  28528     38      0 raceacct *
  F---  06/20-11:19:22   287   651     0  28528     39      0 raceacct *
  ----  06/20-11:19:25   276   811     0  28528    170      0 raceacct

(*) = UTIME does not match with the result of 'time -p'

[The results of patched 2.6.17-kg kernel]
* test for ac_flag

  # time -p ./bugacct
  real 10.07
  user 5.88
  sys 0.17
  -- The accounting results -----------------------------------------
  FLAG  BTIME          ETIME UTIME STIME    MEM MINFLT MAJFLT    COMM
  -P--  06/20-14:08:06  1007   588    16 140992  32944      0 bugacct

* test for the race condition

  # time -p ./raceacct 4
  real 2.55
  user 7.48
  sys 0.00
  # time -p ./raceacct 4
  real 2.59
  user 7.58
  sys 0.00
  # time -p ./raceacct 4
  real 2.64
  user 7.75
  sys 0.00
  # time -p ./raceacct 4
  real 2.29
  user 6.71
  sys 0.00
  # time -p ./raceacct 4
  real 2.68
  user 7.83
  sys 0.00
  -- The accounting results -----------------------------------------
  FLAG  BTIME          ETIME UTIME STIME    MEM MINFLT MAJFLT     COMM
  ----  06/20-11:09:33   255   748     0  28528    171      0 raceacct
  ----  06/20-11:09:37   259   758     0  28528    171      0 raceacct
  ----  06/20-11:09:41   264   775     0  28528    170      0 raceacct
  ----  06/20-11:09:44   229   671     0  28528    170      0 raceacct
  ----  06/20-11:09:57   268   783     0  28528    170      0 raceacct

[The test program : raceacct.c]
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *worker(void *dummy) {
    int i, *flag = dummy;

    for (i=0; i < 100000000 && *flag; i++);
    *flag = 0;
    pthread_exit(NULL);
}
int main(int argc, char *argv[]) {
    pthread_t pthread;
    int i, num = 0, flag = 1;
  if (argc > 2) {
      fprintf(stderr, "argument too large\n");
      return 1;
  } else if (argc == 2) {
      num = atoi(argv[1]);
  }
  for (i=1; i < num; i++)
      pthread_create(&pthread, NULL, worker, (void *)&flag);

worker((void *)&flag);
}

[The test program : bugacct.c]
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/time.h>

#define BUFFER_SIZE (128 * 1024 * 1024) /* 128MB */

void *mychild(void *buffer) {
        struct timeval tv;
        u_int64_t t1, t2;

        gettimeofday(&tv, NULL);
        t1 = tv.tv_sec * 1000000 + tv.tv_usec;
        t2 = t1 + 6 * 1000000;
        /* heavy CPU/Mem job */
        srand(tv.tv_usec);
        while(t1 < t2) {
            memset(buffer, rand(), BUFFER_SIZE);
            gettimeofday(&tv, NULL);
            t1 = tv.tv_sec * 1000000 + tv.tv_usec;
        }
        return NULL;
}
int main(int argc, char *argv[]) {
        pthread_t pthread;
        void *buffer = NULL;

        buffer = malloc(BUFFER_SIZE);
        if (!buffer)
            return 1;

        sleep(4);
        pthread_create(&pthread, NULL, mychild, buffer);
        nice(-1);
        sleep(1);
        pthread_exit(0);
}

[The patch against to 2.6.17]

include/linux/acct.h +6 -2
@@ -123,13 +123,17 @@ struct vfsmount;
 struct super_block;
 extern void acct_auto_close_mnt(struct vfsmount *m);
 extern void acct_auto_close(struct super_block *sb);
-extern void acct_process(long exitcode);
+extern void acct_init_pacct(struct pacct_struct *pacct, struct task_struct *tsk);
+extern void acct_collect(long exitcode, int group_dead);
+extern void acct_process(void);
 extern void acct_update_integrals(struct task_struct *tsk);
 extern void acct_clear_integrals(struct task_struct *tsk);
 #else
 #define acct_auto_close_mnt(x)	do { } while (0)
 #define acct_auto_close(x)	do { } while (0)
-#define acct_process(x)		do { } while (0)
+#define acct_init_pacct(x,y)	do { } while (0)
+#define acct_collect(x,y)	do { } while (0)
+#define acct_process()		do { } while (0)
 #define acct_update_integrals(x)		do { } while (0)
 #define acct_clear_integrals(task)	do { } while (0)
 #endif
include/linux/sched.h +12 -0
@@ -358,6 +358,15 @@ struct sighand_struct {
 	spinlock_t		siglock;
 };
 
+struct pacct_struct {
+	int		ac_flag;
+	long		ac_exitcode;
+	cputime_t	ac_utime, ac_stime;
+	unsigned long	ac_mem;
+	unsigned long	ac_minflt, ac_majflt;
+	struct timespec	ac_start_time;
+};
+
 /*
  * NOTE! "signal_struct" does not have it's own
  * locking, because a shared signal_struct always
@@ -449,6 +458,9 @@ struct signal_struct {
 	struct key *session_keyring;	/* keyring inherited over fork */
 	struct key *process_keyring;	/* keyring private to this process */
 #endif
+#ifdef CONFIG_BSD_PROCESS_ACCT
+	struct pacct_struct pacct;	/* per-process accounting information */
+#endif
 };
 
 /* Context switch must be unlocked if interrupts are to be enabled */
kernel/acct.c +59 -44

170 lines not on this page · raw message

kernel/fork.c +1 -0
@@ -871,6 +871,7 @@ static inline int copy_signal(unsigned l
 		tsk->it_prof_expires =
 			secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
 	}
+	acct_init_pacct(&sig->pacct, tsk);
 
 	return 0;
 }
kernel/exit.c +3 -1
@@ -895,8 +895,8 @@ fastcall NORET_TYPE void do_exit(long co
 	if (group_dead) {
  		hrtimer_cancel(&tsk->signal->real_timer);
 		exit_itimers(tsk->signal);
-		acct_process(code);
 	}
+	acct_collect(code, group_dead);
 	if (unlikely(tsk->robust_list))
 		exit_robust_list(tsk);
 #ifdef CONFIG_COMPAT
@@ -907,6 +907,8 @@ fastcall NORET_TYPE void do_exit(long co
 		audit_free(tsk);
 	exit_mm(tsk);
 
+	if (group_dead)
+		acct_process();
 	exit_sem(tsk);
 	__exit_files(tsk);
 	__exit_fs(tsk);
#1 lore raw
Quoted 11 lines · On Tue, 20 Jun 2006 15:24:59 +0900 KaiGai Kohei <[email protected]> wrote:

Hi, I noticed three problems in pacct facility.

1. Pacct facility has a possibility to write incorrect ac_flag in multi-threading cases.
2. There is a possibility to be waken up OOM Killer from pacct facility. It will cause system stall.
3. If several threads are killed at same time, There is a possibility not to pick up a part of those accountings.

The attached patch will resolve those matters.
Any comments please. Thanks,

Thanks, but you have three quite distinct bugs here, and three quite distinct descriptions and, I think, three quite distinct fixes.

Would it be possible for you to prepare three patches?

#2 lore raw
Quoted 9 lines
Quoted 9 lines

Hi, I noticed three problems in pacct facility.

1. Pacct facility has a possibility to write incorrect ac_flag in multi-threading cases.
2. There is a possibility to be waken up OOM Killer from pacct facility. It will cause system stall.
3. If several threads are killed at same time, There is a possibility not to pick up a part of those accountings.

Quoted 7 lines
Quoted 2 lines

The attached patch will resolve those matters.
Any comments please. Thanks,

Thanks, but you have three quite distinct bugs here, and three quite distinct descriptions and, I think, three quite distinct fixes.

Would it be possible for you to prepare three patches?

It may be possible. Please wait for a while to separate it into three-part and to confirm its correct behavior.

Thanks,

Signature · 2 lines
Open Source Software Promotion Center, NEC
KaiGai Kohei <[email protected]>
#3 lore raw

The seriese of patches fixes some process accounting bugs.

[PATCH 1/3] two phase process accounting
[PATCH 2/3] avoidance to refer the last thread as a representation
of the process
[PATCH 3/3] none-delayed process accounting accumulation

* background of the patch.1

The pacct facility need an i/o operation when an accounting record is generated. There is a possibility to wake OOM killer up. If OOM killer is activated, it kills some processes to make them release process memory regions.
But acct_process() is called in the killed processes context before calling exit_mm(), so those processes cannot release own memory. In the results, any processes stop in this point and it finally cause a system stall.

---- in kernel/exit.c : do_exit() ------------
group_dead = atomic_dec_and_test(&tsk->signal->live); if (group_dead) {
hrtimer_cancel(&tsk->signal->real_timer); exit_itimers(tsk->signal); acct_process(code);
}
:
- snip -
:
exit_mm(tsk);
----------------------------------------------

This patch separates generating an accounting record facility into two-phase.
In the first one, acct_collect() calculate vitual memory size of the process and stores it into pacct_struct before exit_mm(). Then, acct_process() generates an accounting record and write it into medium.

* background of the patch.2

When pacct facility generate an 'ac_flag' field in accounting record, it refers a task_struct of the thread which died last in the process. But any other task_structs are ignored.

Therefore, pacct facility drops ASU flag even if root-privilege operations are used by any other threads except the last one. In addition, AFORK flag is always set when the thread of group-leader didn't die last, although this process has called execve() after fork().

We have a same matter in ac_exitcode. The recorded ac_exitcode is an exit code of the last thread in the process. There is a possibility this exitcode is not the group leader's one.

---- in kernel/acct.c : do_acct_process() ----
ac.ac_flag = 0;
if (current->flags & PF_FORKNOEXEC)
ac.ac_flag |= AFORK;
if (current->flags & PF_SUPERPRIV)
ac.ac_flag |= ASU;
if (current->flags & PF_DUMPCORE)
ac.ac_flag |= ACORE;
if (current->flags & PF_SIGNALED)
ac.ac_flag |= AXSIG;
:
- snip -
:
ac.ac_exitcode = exitcode;
----------------------------------------------

This patch fixes those matters.
- The exit code of group leader is recorded as ac_exitcode.
- ASU, ACORE, AXSIG flag are marked if any task_struct satisfy the conditions.
- AFORK flag is marked if only group leader thread satisfy the condition.

* background of the patch.3

In current 2.6.17 implementation, signal_struct refered from task_struct is used for per-process data structure. The pacct facility also uses it as a per-process data structure to store stime, utime, minflt, majflt. But those members are saved in __exit_signal(). It's too late.

For example, if some threads exits at same time, pacct facility has a possibility to drop accountings for a part of those threads. (see, the following 'The results of original 2.6.17 kernel') I think accounting information should be completely collected into the per-process data structure before writing out an accounting record.

This patch fixes this matter. Accumulation of stime, utime, minflt and majflt are done before generating accounting record.

* accounting results

[in original 2.6.17 cases]

# accton acct.log
# time -p ./bugacct
real 10.07
user 5.96
sys 0.10
# time -p ./raceacct 4
real 6.92
user 27.22
sys 0.00
# time -p ./raceacct 4
real 7.71
user 30.14
sys 0.00
# time -p ./raceacct 4
real 6.94
user 27.21
sys 0.00
# time -p ./raceacct 4
real 6.25
user 24.42
sys 0.00
# time -p ./raceacct 4
real 6.92
user 27.22
sys 0.00

-- accounting results --------
FLAG BTIME ETIME UTIME STIME MEM MINFLT MAJFLT COMM

-P-- 13:41:16      5       0     0    3072     110      0    accton
F--- 13:41:35   1006     596     9  143232    8200      0   bugacct *
F--- 13:41:53    692    2032     0   28528      38      0  raceacct *
---- 13:42:10    771    3014     0   28528     170      0  raceacct
F--- 13:42:19    694    2027     0   28528       8      0  raceacct *
F--- 13:42:26    625    1832     0   28528      40      0  raceacct *
---- 13:45:40    692    2722     0   28528     171      0  raceacct

'P' means this process used root privilege operations. 'F' means this process didn't execve() after fork().

=> bugacct used root privilege operation, but pacct facility droped it. => In raceacct, some threads exit on same time. pacct facility often drops
a part of utime, stime, minflt and majflt.
=> When group leader thread didn't die last in raceacct, incorrent flag 'F'
is set.

[in patched 2.6.17-kg cases]

# touch acct.log
# accton acct.log
# time -p ./bugacct
real 10.07
user 5.97
sys 0.09
# time -p ./raceacct 4
real 7.11
user 27.76
sys 0.00
# time -p ./raceacct 4
real 6.93
user 27.18
sys 0.00
# time -p ./raceacct 4
real 7.11
user 27.76
sys 0.00
# time -p ./raceacct 4
real 7.12
user 27.77
sys 0.00
# time -p ./raceacct 4
real 6.92
user 27.17
sys 0.00

-- accounting results --------
FLAG BTIME ETIME UTIME STIME MEM MINFLT MAJFLT COMM

-P-- 13:24:01      0      0      0    3072     111      0    accton
-P-- 13:24:05   1007    597      8  143232    8360      0   bugacct
---- 13:24:35    711   2776      0   28528     171      0  raceacct
---- 13:24:44    693   2718      0   28528     172      0  raceacct
---- 13:24:51    711   2776      0   28528     172      0  raceacct
---- 13:25:05    712   2777      0   28528     174      0  raceacct
---- 13:25:14    692   2717      0   28528     171      0  raceacct

I hope your any comments. Thanks,

Quoted 21 lines · KaiGai Kohei
Quoted 9 lines
Quoted 9 lines

Hi, I noticed three problems in pacct facility.

1. Pacct facility has a possibility to write incorrect ac_flag in multi-threading cases.
2. There is a possibility to be waken up OOM Killer from pacct facility. It will cause system stall.
3. If several threads are killed at same time, There is a possibility not to pick up a part of those accountings.

Quoted 7 lines
Quoted 2 lines

The attached patch will resolve those matters.
Any comments please. Thanks,

Thanks, but you have three quite distinct bugs here, and three quite distinct descriptions and, I think, three quite distinct fixes.

Would it be possible for you to prepare three patches?

It may be possible. Please wait for a while to separate it into three-part and to confirm its correct behavior.

Thanks,

Signature · 2 lines
Open Source Software Promotion Center, NEC
KaiGai Kohei <[email protected]>
#4 lore raw

[PACCT] two phase process accounting

The pacct facility need an i/o operation when an accounting record is generated. There is a possibility to wake OOM killer up. If OOM killer is activated, it kills some processes to make them release process memory regions.
But acct_process() is called in the killed processes context before calling exit_mm(), so those processes cannot release own memory. In the results, any processes stop in this point and it finally cause a system stall.

---- in kernel/exit.c : do_exit() ------------
group_dead = atomic_dec_and_test(&tsk->signal->live); if (group_dead) {
hrtimer_cancel(&tsk->signal->real_timer); exit_itimers(tsk->signal); acct_process(code);
}
:
- snip -
:
exit_mm(tsk);
----------------------------------------------

This patch separates generating an accounting record facility into two-phase.
In the first one, acct_collect() calculate vitual memory size of the process and stores it into pacct_struct before exit_mm(). Then, acct_process() generates an accounting record and write it into medium.

include/linux/acct.h +4 -0
index 9a66401..6753687 100644
@@ -121,16 +121,20 @@ struct acct_v3
 #ifdef CONFIG_BSD_PROCESS_ACCT
 struct vfsmount;
 struct super_block;
 extern void acct_auto_close_mnt(struct vfsmount *m);
 extern void acct_auto_close(struct super_block *sb);
+extern void acct_init_pacct(struct pacct_struct *pacct);
+extern void acct_collect();
 extern void acct_process(long exitcode);
 extern void acct_update_integrals(struct task_struct *tsk);
 extern void acct_clear_integrals(struct task_struct *tsk);
 #else
 #define acct_auto_close_mnt(x)	do { } while (0)
 #define acct_auto_close(x)	do { } while (0)
+#define acct_init_pacct(x)	do { } while (0)
+#define acct_collect()		do { } while (0)
 #define acct_process(x)		do { } while (0)
 #define acct_update_integrals(x)		do { } while (0)
 #define acct_clear_integrals(task)	do { } while (0)
 #endif
 
include/linux/sched.h +7 -0
index 29b7d4f..918fdda 100644
@@ -356,10 +356,14 @@ struct sighand_struct {
 	atomic_t		count;
 	struct k_sigaction	action[_NSIG];
 	spinlock_t		siglock;
 };
 
+struct pacct_struct {
+	unsigned long		ac_mem;
+};
+
 /*
  * NOTE! "signal_struct" does not have it's own
  * locking, because a shared signal_struct always
  * implies a shared sighand_struct, so locking
  * sighand_struct is always a proper superset of
@@ -447,10 +451,13 @@ struct signal_struct {
 	 * thing in threads created with CLONE_THREAD */
 #ifdef CONFIG_KEYS
 	struct key *session_keyring;	/* keyring inherited over fork */
 	struct key *process_keyring;	/* keyring private to this process */
 #endif
+#ifdef CONFIG_BSD_PROCESS_ACCT
+	struct pacct_struct pacct;	/* per-process accounting information */
+#endif
 };
 
 /* Context switch must be unlocked if interrupts are to be enabled */
 #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
 # define __ARCH_WANT_UNLOCKED_CTXSW
kernel/acct.c +36 -15

83 lines not on this page · raw message

kernel/exit.c +3 -1
index e06d0c1..54bdbd9 100644
@@ -893,11 +893,11 @@ fastcall NORET_TYPE void do_exit(long co
 	}
 	group_dead = atomic_dec_and_test(&tsk->signal->live);
 	if (group_dead) {
  		hrtimer_cancel(&tsk->signal->real_timer);
 		exit_itimers(tsk->signal);
-		acct_process(code);
+		acct_collect();
 	}
 	if (unlikely(tsk->robust_list))
 		exit_robust_list(tsk);
 #ifdef CONFIG_COMPAT
 	if (unlikely(tsk->compat_robust_list))
@@ -905,10 +905,12 @@ fastcall NORET_TYPE void do_exit(long co
 #endif
 	if (unlikely(tsk->audit_context))
 		audit_free(tsk);
 	exit_mm(tsk);
 
+	if (group_dead)
+		acct_process(code);
 	exit_sem(tsk);
 	__exit_files(tsk);
 	__exit_fs(tsk);
 	exit_namespace(tsk);
 	exit_thread();
kernel/fork.c +1 -0
index ac8100e..d6c812c 100644
@@ -869,10 +869,11 @@ static inline int copy_signal(unsigned l
 		 * of the whole CPU time limit.
 		 */
 		tsk->it_prof_expires =
 			secs_to_cputime(sig->rlim[RLIMIT_CPU].rlim_cur);
 	}
+	acct_init_pacct(&sig->pacct);
 
 	return 0;
 }
 
 void __cleanup_signal(struct signal_struct *sig)
Signature · 2 lines
Open Source Software Promotion Center, NEC
KaiGai Kohei <[email protected]>
#5 lore raw
In reply to KaiGai Kohei

[PACCT] avoidance to refer the last thread as a representation of the process

When pacct facility generate an 'ac_flag' field in accounting record, it refers a task_struct of the thread which died last in the process. But any other task_structs are ignored.

Therefore, pacct facility drops ASU flag even if root-privilege operations are used by any other threads except the last one. In addition, AFORK flag is always set when the thread of group-leader didn't die last, although this process has called execve() after fork().

We have a same matter in ac_exitcode. The recorded ac_exitcode is an exit code of the last thread in the process. There is a possibility this exitcode is not the group leader's one.

---- in kernel/acct.c : do_acct_process() ----
ac.ac_flag = 0;
if (current->flags & PF_FORKNOEXEC)
ac.ac_flag |= AFORK;
if (current->flags & PF_SUPERPRIV)
ac.ac_flag |= ASU;
if (current->flags & PF_DUMPCORE)
ac.ac_flag |= ACORE;
if (current->flags & PF_SIGNALED)
ac.ac_flag |= AXSIG;
:
- snip -
:
ac.ac_exitcode = exitcode;
----------------------------------------------

This patch fixes those matters.
- The exit code of group leader is recorded as ac_exitcode.
- ASU, ACORE, AXSIG flag are marked if any task_struct satisfy the conditions.
- AFORK flag is marked if only group leader thread satisfy the condition.

include/linux/acct.h +4 -4
index 6753687..879d441 100644
@@ -122,20 +122,20 @@ struct acct_v3
 struct vfsmount;
 struct super_block;
 extern void acct_auto_close_mnt(struct vfsmount *m);
 extern void acct_auto_close(struct super_block *sb);
 extern void acct_init_pacct(struct pacct_struct *pacct);
-extern void acct_collect();
-extern void acct_process(long exitcode);
+extern void acct_collect(long exitcode, int group_dead);
+extern void acct_process(void);
 extern void acct_update_integrals(struct task_struct *tsk);
 extern void acct_clear_integrals(struct task_struct *tsk);
 #else
 #define acct_auto_close_mnt(x)	do { } while (0)
 #define acct_auto_close(x)	do { } while (0)
 #define acct_init_pacct(x)	do { } while (0)
-#define acct_collect()		do { } while (0)
-#define acct_process(x)		do { } while (0)
+#define acct_collect(x,y)	do { } while (0)
+#define acct_process()		do { } while (0)
 #define acct_update_integrals(x)		do { } while (0)
 #define acct_clear_integrals(task)	do { } while (0)
 #endif
 
 /*
include/linux/sched.h +2 -0
index 918fdda..6905ac0 100644
@@ -357,10 +357,12 @@ struct sighand_struct {
 	struct k_sigaction	action[_NSIG];
 	spinlock_t		siglock;
 };
 
 struct pacct_struct {
+	int			ac_flag;
+	long			ac_exitcode;
 	unsigned long		ac_mem;
 };
 
 /*
  * NOTE! "signal_struct" does not have it's own
kernel/acct.c +24 -18

136 lines not on this page · raw message

kernel/exit.c +2 -2
index 54bdbd9..9d395c7 100644
@@ -893,12 +893,12 @@ fastcall NORET_TYPE void do_exit(long co
 	}
 	group_dead = atomic_dec_and_test(&tsk->signal->live);
 	if (group_dead) {
  		hrtimer_cancel(&tsk->signal->real_timer);
 		exit_itimers(tsk->signal);
-		acct_collect();
 	}
+	acct_collect(code, group_dead);
 	if (unlikely(tsk->robust_list))
 		exit_robust_list(tsk);
 #ifdef CONFIG_COMPAT
 	if (unlikely(tsk->compat_robust_list))
 		compat_exit_robust_list(tsk);
@@ -906,11 +906,11 @@ fastcall NORET_TYPE void do_exit(long co
 	if (unlikely(tsk->audit_context))
 		audit_free(tsk);
 	exit_mm(tsk);
 
 	if (group_dead)
-		acct_process(code);
+		acct_process();
 	exit_sem(tsk);
 	__exit_files(tsk);
 	__exit_fs(tsk);
 	exit_namespace(tsk);
 	exit_thread();
Signature · 2 lines
Open Source Software Promotion Center, NEC
KaiGai Kohei <[email protected]>
#6 lore raw
In reply to KaiGai Kohei

[PACCT] none-delayed process accounting accumulation

In current 2.6.17 implementation, signal_struct refered from task_struct is used for per-process data structure. The pacct facility also uses it as a per-process data structure to store stime, utime, minflt, majflt. But those members are saved in __exit_signal(). It's too late.

For example, if some threads exits at same time, pacct facility has a possibility to drop accountings for a part of those threads. (see, the following 'The results of original 2.6.17 kernel') I think accounting information should be completely collected into the per-process data structure before writing out an accounting record.

This patch fixes this matter. Accumulation of stime, utime, minflt and majflt are done before generating accounting record.

include/linux/sched.h +2 -0
index 6905ac0..06c72fb 100644
@@ -360,10 +360,12 @@ struct sighand_struct {
 
 struct pacct_struct {
 	int			ac_flag;
 	long			ac_exitcode;
 	unsigned long		ac_mem;
+	cputime_t		ac_utime, ac_stime;
+	unsigned long		ac_minflt, ac_majflt;
 };
 
 /*
  * NOTE! "signal_struct" does not have it's own
  * locking, because a shared signal_struct always
kernel/acct.c +9 -11
index 7111fe7..48c701c 100644
@@ -426,11 +426,10 @@ static void do_acct_process(struct file
 	mm_segment_t fs;
 	unsigned long flim;
 	u64 elapsed;
 	u64 run_time;
 	struct timespec uptime;
-	unsigned long jiffies;
 
 	/*
 	 * First check to see if there is enough free_space to continue
 	 * the process accounting system.
 	 */
@@ -467,16 +466,10 @@ static void do_acct_process(struct file
 		ac.ac_etime_lo = (u16) etime;
 	}
 #endif
 	do_div(elapsed, AHZ);
 	ac.ac_btime = xtime.tv_sec - elapsed;
-	jiffies = cputime_to_jiffies(cputime_add(current->utime,
-						 current->signal->utime));
-	ac.ac_utime = encode_comp_t(jiffies_to_AHZ(jiffies));
-	jiffies = cputime_to_jiffies(cputime_add(current->stime,
-						 current->signal->stime));
-	ac.ac_stime = encode_comp_t(jiffies_to_AHZ(jiffies));
 	/* we really need to bite the bullet and change layout */
 	ac.ac_uid = current->uid;
 	ac.ac_gid = current->gid;
 #if ACCT_VERSION==2
 	ac.ac_ahz = AHZ;
@@ -495,20 +488,20 @@ static void do_acct_process(struct file
 	ac.ac_tty = current->signal->tty ?
 		old_encode_dev(tty_devnum(current->signal->tty)) : 0;
 	read_unlock(&tasklist_lock);
 
 	spin_lock(&current->sighand->siglock);
+	ac.ac_utime = encode_comp_t(jiffies_to_AHZ(cputime_to_jiffies(pacct->ac_utime)));
+	ac.ac_stime = encode_comp_t(jiffies_to_AHZ(cputime_to_jiffies(pacct->ac_stime)));
 	ac.ac_flag = pacct->ac_flag;
 	ac.ac_mem = encode_comp_t(pacct->ac_mem);
+	ac.ac_minflt = encode_comp_t(pacct->ac_minflt);
+	ac.ac_majflt = encode_comp_t(pacct->ac_majflt);
 	ac.ac_exitcode = pacct->ac_exitcode;
 	spin_unlock(&current->sighand->siglock);
 	ac.ac_io = encode_comp_t(0 /* current->io_usage */);	/* %% */
 	ac.ac_rw = encode_comp_t(ac.ac_io / 1024);
-	ac.ac_minflt = encode_comp_t(current->signal->min_flt +
-				     current->min_flt);
-	ac.ac_majflt = encode_comp_t(current->signal->maj_flt +
-				     current->maj_flt);
 	ac.ac_swaps = encode_comp_t(0);
 
 	/*
          * Kernel segment override to datasegment and write it
          * to the accounting file.
@@ -530,10 +523,11 @@ static void do_acct_process(struct file
  * acct_init_pacct - initialize a new pacct_struct
  */
 void acct_init_pacct(struct pacct_struct *pacct)
 {
 	memset(pacct, 0, sizeof(struct pacct_struct));
+	pacct->ac_utime = pacct->ac_stime = cputime_zero;
 }
 
 /**
  * acct_collect - collect accounting information into pacct_struct
  * @exitcode: task exit code
@@ -567,10 +561,14 @@ void acct_collect(long exitcode, int gro
 		pacct->ac_flag |= ASU;
 	if (current->flags & PF_DUMPCORE)
 		pacct->ac_flag |= ACORE;
 	if (current->flags & PF_SIGNALED)
 		pacct->ac_flag |= AXSIG;
+	pacct->ac_utime = cputime_add(pacct->ac_utime, current->utime);
+	pacct->ac_stime = cputime_add(pacct->ac_stime, current->stime);
+	pacct->ac_minflt += current->min_flt;
+	pacct->ac_majflt += current->maj_flt;
 	spin_unlock(&current->sighand->siglock);
 }
 
 /**
  * acct_process - now just a wrapper around do_acct_process
Signature · 2 lines
Open Source Software Promotion Center, NEC
KaiGai Kohei <[email protected]>
#7 lore raw
In reply to KaiGai Kohei
Quoted 1 line · On Thu, 22 Jun 2006 12:07:19 +0900 KaiGai Kohei <[email protected]> wrote:

The seriese of patches fixes some process accounting bugs.

OK, thanks for splitting those up. A few patch-protocol things:

- Please make the email Subject: reflect the patch contents - all three patches here were called "Re: Add pacct_struct to fix some pacct bugs."

- Please don't indent the changlogs by five spaces. Start in column zero.

- Your email client performs space-stuffing, which corrupts the patches. I fixed them all by hand.

I don't know if it's possible to prevent thunderbird from doing this (my mozilla bugzilla record on this has to be three years old, and is still open). You might have to use text/plain attachments in the future.

#8 lore raw
Quoted 10 lines · Andrew Morton
Quoted 1 line · On Thu, 22 Jun 2006 12:07:19 +0900 KaiGai Kohei <[email protected]> wrote:

The seriese of patches fixes some process accounting bugs.

OK, thanks for splitting those up. A few patch-protocol things:

- Please make the email Subject: reflect the patch contents - all three patches here were called "Re: Add pacct_struct to fix some pacct bugs."

Quoted 8 lines

- Please don't indent the changlogs by five spaces. Start in column zero.

- Your email client performs space-stuffing, which corrupts the patches. I fixed them all by hand.

I don't know if it's possible to prevent thunderbird from doing this (my mozilla bugzilla record on this has to be three years old, and is still open). You might have to use text/plain attachments in the future.

Oops, I'm sorry for sending you a trouble.
I'll pay attention about those manners.

Thanks,

Signature · 2 lines
Open Source Software Promotion Center, NEC
KaiGai Kohei <[email protected]>
#9 lore raw
In reply to Andrew Morton
Quoted 10 lines · On Wed, 21 Jun 2006 20:35:50 -0700 Andrew Morton wrote:
Quoted 1 line · On Thu, 22 Jun 2006 12:07:19 +0900 KaiGai Kohei <[email protected]> wrote:

The seriese of patches fixes some process accounting bugs.

OK, thanks for splitting those up. A few patch-protocol things:

- Please make the email Subject: reflect the patch contents - all three patches here were called "Re: Add pacct_struct to fix some pacct bugs."

Quoted 8 lines

- Please don't indent the changlogs by five spaces. Start in column zero.

- Your email client performs space-stuffing, which corrupts the patches. I fixed them all by hand.

I don't know if it's possible to prevent thunderbird from doing this (my mozilla bugzilla record on this has to be three years old, and is still open). You might have to use text/plain attachments in the future.

tbird hints (works for me from oracle.com):

http://mbligh.org/linuxdocs/Email/Clients/Thunderbird
and
http://lkml.org/lkml/2005/12/27/191
and
http://lists.osdl.org/pipermail/kernel-janitors/2006-June/006478.html

However, I guess there could be other things between the sender and receiver that cause problems...

---
~Randy