Every time _cpu_up() is called for a CPU, idle_thread_get() is called which then re-initializes a CPU's idle thread that was already previously created and cached in a global variable in smpboot.c. idle_thread_get() calls init_idle() which then calls __sched_fork(). __sched_fork() is where cpufreq_task_times_init() is, and cpufreq_task_times_init() allocates memory for the task struct's time_in_state array. Since idle_thread_get() reuses a task struct instance that was already previously created, this means that every time it calls init_idle(), cpufreq_task_times_init() allocates this array again and overwrites the existing allocation that the idle thread already had. This causes memory to be leaked every time a CPU is onlined. In order to fix this, move allocation of time_in_state into _do_fork to avoid allocating it at all for idle threads. The cpufreq times interface is intended to be used for tracking userspace tasks, so we can safely remove it from the kernel's idle threads without killing any functionality. But that's not all! Task structs can be freed outside of release_task(), which creates another memory leak because a task struct can be freed without having its cpufreq times allocation freed. To fix this, free the cpufreq times allocation at the same time that task struct allocations are freed, in free_task(). Since free_task() can also be called in error paths of copy_process() after dup_task_struct(), set time_in_state to NULL immediately after calling dup_task_struct() to avoid possible double free. Bug description and fix adapted from patch submitted by Sultan Alsawaf <sultanxda@gmail.com> at https://android-review.googlesource.com/c/kernel/msm/+/700134 Bug: 110044919 Test: Hikey960 builds, boots & reports /proc/<pid>/time_in_state correctly Change-Id: I12fe7611fc88eb7f6c39f8f7629ad27b6ec4722c Signed-off-by: Connor O'Brien <connoro@google.com>
46 lines
1.9 KiB
C
46 lines
1.9 KiB
C
/* drivers/cpufreq/cpufreq_times.c
|
|
*
|
|
* Copyright (C) 2018 Google, Inc.
|
|
*
|
|
* This software is licensed under the terms of the GNU General Public
|
|
* License version 2, as published by the Free Software Foundation, and
|
|
* may be copied, distributed, and modified under those terms.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
*/
|
|
|
|
#ifndef _LINUX_CPUFREQ_TIMES_H
|
|
#define _LINUX_CPUFREQ_TIMES_H
|
|
|
|
#include <linux/cpufreq.h>
|
|
#include <linux/cputime.h>
|
|
#include <linux/pid.h>
|
|
|
|
#ifdef CONFIG_CPU_FREQ_TIMES
|
|
void cpufreq_task_times_init(struct task_struct *p);
|
|
void cpufreq_task_times_alloc(struct task_struct *p);
|
|
void cpufreq_task_times_exit(struct task_struct *p);
|
|
int proc_time_in_state_show(struct seq_file *m, struct pid_namespace *ns,
|
|
struct pid *pid, struct task_struct *p);
|
|
void cpufreq_acct_update_power(struct task_struct *p, cputime_t cputime);
|
|
void cpufreq_times_create_policy(struct cpufreq_policy *policy);
|
|
void cpufreq_times_record_transition(struct cpufreq_freqs *freq);
|
|
void cpufreq_task_times_remove_uids(uid_t uid_start, uid_t uid_end);
|
|
int single_uid_time_in_state_open(struct inode *inode, struct file *file);
|
|
#else
|
|
static inline void cpufreq_task_times_init(struct task_struct *p) {}
|
|
static inline void cpufreq_task_times_alloc(struct task_struct *p) {}
|
|
static inline void cpufreq_task_times_exit(struct task_struct *p) {}
|
|
static inline void cpufreq_acct_update_power(struct task_struct *p,
|
|
u64 cputime) {}
|
|
static inline void cpufreq_times_create_policy(struct cpufreq_policy *policy) {}
|
|
static inline void cpufreq_times_record_transition(
|
|
struct cpufreq_freqs *freq) {}
|
|
static inline void cpufreq_task_times_remove_uids(uid_t uid_start,
|
|
uid_t uid_end) {}
|
|
#endif /* CONFIG_CPU_FREQ_TIMES */
|
|
#endif /* _LINUX_CPUFREQ_TIMES_H */
|