Skip to content

Commit dfc428b

Browse files
oleg-nesterovtorvalds
authored andcommitted
taskstats: add_del_listener() shouldn't use the wrong node
1. Commit 26c4cae "don't allow duplicate entries in listener mode" changed add_del_listener(REGISTER) so that "next_cpu:" can reuse the listener allocated for the previous cpu, this doesn't look exactly right even if minor. Change the code to kfree() in the already-registered case, this case is unlikely anyway so the extra kmalloc_node() shouldn't hurt but looke more correct and clean. 2. use the plain list_for_each_entry() instead of _safe() to scan listeners->list. 3. Remove the unneeded INIT_LIST_HEAD(&s->list), we are going to list_add(&s->list). Signed-off-by: Oleg Nesterov <oleg@redhat.com> Reviewed-by: Vasiliy Kulikov <segoon@openwall.com> Cc: Balbir Singh <bsingharora@gmail.com> Reviewed-by: Jerome Marchand <jmarchan@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 12b3e03 commit dfc428b

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

‎kernel/taskstats.c‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -291,30 +291,28 @@ static int add_del_listener(pid_t pid, const struct cpumask *mask, int isadd)
291291
if (!cpumask_subset(mask, cpu_possible_mask))
292292
return -EINVAL;
293293

294-
s = NULL;
295294
if (isadd == REGISTER) {
296295
for_each_cpu(cpu, mask) {
297-
if (!s)
298-
s = kmalloc_node(sizeof(struct listener),
299-
GFP_KERNEL, cpu_to_node(cpu));
296+
s = kmalloc_node(sizeof(struct listener),
297+
GFP_KERNEL, cpu_to_node(cpu));
300298
if (!s)
301299
goto cleanup;
300+
302301
s->pid = pid;
303-
INIT_LIST_HEAD(&s->list);
304302
s->valid = 1;
305303

306304
listeners = &per_cpu(listener_array, cpu);
307305
down_write(&listeners->sem);
308-
list_for_each_entry_safe(s2, tmp, &listeners->list, list) {
306+
list_for_each_entry(s2, &listeners->list, list) {
309307
if (s2->pid == pid)
310-
goto next_cpu;
308+
goto exists;
311309
}
312310
list_add(&s->list, &listeners->list);
313311
s = NULL;
314-
next_cpu:
312+
exists:
315313
up_write(&listeners->sem);
314+
kfree(s); /* nop if NULL */
316315
}
317-
kfree(s);
318316
return 0;
319317
}
320318

0 commit comments

Comments
 (0)