Skip to content

Commit 35df17c

Browse files
Shailabh NagarLinus Torvalds
authored andcommitted
[PATCH] task delay accounting fixes
Cleanup allocation and freeing of tsk->delays used by delay accounting. This solves two problems reported for delay accounting: 1. oops in __delayacct_blkio_ticks http://www.uwsg.indiana.edu/hypermail/linux/kernel/0608.2/1844.html Currently tsk->delays is getting freed too early in task exit which can cause a NULL tsk->delays to get accessed via reading of /proc/<tgid>/stats. The patch fixes this problem by freeing tsk->delays closer to when task_struct itself is freed up. As a result, it also eliminates the use of tsk->delays_lock which was only being used (inadequately) to safeguard access to tsk->delays while a task was exiting. 2. Possible memory leak in kernel/delayacct.c http://www.uwsg.indiana.edu/hypermail/linux/kernel/0608.2/1389.html The patch cleans up tsk->delays allocations after a bad fork which was missing earlier. The patch has been tested to fix the problems listed above and stress tested with rapid calls to delay accounting's taskstats command interface (which is the other path that can access the same data, besides the /proc interface causing the oops above). Signed-off-by: Shailabh Nagar <nagar@watson.ibm.com> Cc: Balbir Singh <balbir@in.ibm.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 30f3174 commit 35df17c

5 files changed

Lines changed: 11 additions & 23 deletions

File tree

‎include/linux/delayacct.h‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,14 @@ static inline void delayacct_tsk_init(struct task_struct *tsk)
5959
__delayacct_tsk_init(tsk);
6060
}
6161

62-
static inline void delayacct_tsk_exit(struct task_struct *tsk)
62+
/* Free tsk->delays. Called from bad fork and __put_task_struct
63+
* where there's no risk of tsk->delays being accessed elsewhere
64+
*/
65+
static inline void delayacct_tsk_free(struct task_struct *tsk)
6366
{
6467
if (tsk->delays)
65-
__delayacct_tsk_exit(tsk);
68+
kmem_cache_free(delayacct_cache, tsk->delays);
69+
tsk->delays = NULL;
6670
}
6771

6872
static inline void delayacct_blkio_start(void)
@@ -101,7 +105,7 @@ static inline void delayacct_init(void)
101105
{}
102106
static inline void delayacct_tsk_init(struct task_struct *tsk)
103107
{}
104-
static inline void delayacct_tsk_exit(struct task_struct *tsk)
108+
static inline void delayacct_tsk_free(struct task_struct *tsk)
105109
{}
106110
static inline void delayacct_blkio_start(void)
107111
{}

‎include/linux/sched.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,6 @@ struct task_struct {
994994
*/
995995
struct pipe_inode_info *splice_pipe;
996996
#ifdef CONFIG_TASK_DELAY_ACCT
997-
spinlock_t delays_lock;
998997
struct task_delay_info *delays;
999998
#endif
1000999
};

‎kernel/delayacct.c‎

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,11 @@ void delayacct_init(void)
4141

4242
void __delayacct_tsk_init(struct task_struct *tsk)
4343
{
44-
spin_lock_init(&tsk->delays_lock);
45-
/* No need to acquire tsk->delays_lock for allocation here unless
46-
__delayacct_tsk_init called after tsk is attached to tasklist
47-
*/
4844
tsk->delays = kmem_cache_zalloc(delayacct_cache, SLAB_KERNEL);
4945
if (tsk->delays)
5046
spin_lock_init(&tsk->delays->lock);
5147
}
5248

53-
void __delayacct_tsk_exit(struct task_struct *tsk)
54-
{
55-
struct task_delay_info *delays = tsk->delays;
56-
spin_lock(&tsk->delays_lock);
57-
tsk->delays = NULL;
58-
spin_unlock(&tsk->delays_lock);
59-
kmem_cache_free(delayacct_cache, delays);
60-
}
61-
6249
/*
6350
* Start accounting for a delay statistic using
6451
* its starting timestamp (@start)
@@ -118,8 +105,6 @@ int __delayacct_add_tsk(struct taskstats *d, struct task_struct *tsk)
118105
struct timespec ts;
119106
unsigned long t1,t2,t3;
120107

121-
spin_lock(&tsk->delays_lock);
122-
123108
/* Though tsk->delays accessed later, early exit avoids
124109
* unnecessary returning of other data
125110
*/
@@ -161,7 +146,6 @@ int __delayacct_add_tsk(struct taskstats *d, struct task_struct *tsk)
161146
spin_unlock(&tsk->delays->lock);
162147

163148
done:
164-
spin_unlock(&tsk->delays_lock);
165149
return 0;
166150
}
167151

‎kernel/exit.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,6 @@ fastcall NORET_TYPE void do_exit(long code)
908908
audit_free(tsk);
909909
taskstats_exit_send(tsk, tidstats, group_dead, mycpu);
910910
taskstats_exit_free(tidstats);
911-
delayacct_tsk_exit(tsk);
912911

913912
exit_mm(tsk);
914913

‎kernel/fork.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ void __put_task_struct(struct task_struct *tsk)
117117
security_task_free(tsk);
118118
free_uid(tsk->user);
119119
put_group_info(tsk->group_info);
120+
delayacct_tsk_free(tsk);
120121

121122
if (!profile_handoff_task(tsk))
122123
free_task(tsk);
@@ -1011,7 +1012,7 @@ static struct task_struct *copy_process(unsigned long clone_flags,
10111012
retval = -EFAULT;
10121013
if (clone_flags & CLONE_PARENT_SETTID)
10131014
if (put_user(p->pid, parent_tidptr))
1014-
goto bad_fork_cleanup;
1015+
goto bad_fork_cleanup_delays_binfmt;
10151016

10161017
INIT_LIST_HEAD(&p->children);
10171018
INIT_LIST_HEAD(&p->sibling);
@@ -1277,7 +1278,8 @@ static struct task_struct *copy_process(unsigned long clone_flags,
12771278
bad_fork_cleanup_cpuset:
12781279
#endif
12791280
cpuset_exit(p);
1280-
bad_fork_cleanup:
1281+
bad_fork_cleanup_delays_binfmt:
1282+
delayacct_tsk_free(p);
12811283
if (p->binfmt)
12821284
module_put(p->binfmt->module);
12831285
bad_fork_cleanup_put_domain:

0 commit comments

Comments
 (0)