android_kernel_oneplus_msm8998/kernel/sched
Sultan Alsawaf 1b37d68f4c ANDROID: Fix massive cpufreq_times memory leaks
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>
2018-07-18 13:21:38 +00:00
..
auto_group.c sched/autogroup: Fix autogroup_move_group() to never skip sched_move_task() 2017-10-27 10:23:17 +02:00
auto_group.h sched, timer: Convert usages of ACCESS_ONCE() in the scheduler to READ_ONCE()/WRITE_ONCE() 2015-05-08 12:11:32 +02:00
clock.c treewide: Remove old email address 2015-11-23 09:44:58 +01:00
completion.c sched/completion: Serialize completion_done() with complete() 2015-02-18 14:27:40 +01:00
core.c ANDROID: Fix massive cpufreq_times memory leaks 2018-07-18 13:21:38 +00:00
cpuacct.c cgroup: rename cgroup_subsys->base_cftypes to ->legacy_cftypes 2014-07-15 11:05:09 -04:00
cpuacct.h sched/cpuacct: Initialize root cpuacct earlier 2013-04-10 13:54:20 +02:00
cpudeadline.c sched/deadline: Unify dl_time_before() usage 2015-09-23 09:51:25 +02:00
cpudeadline.h sched/deadline: Unify dl_time_before() usage 2015-09-23 09:51:25 +02:00
cpufreq.c sched: backport cpufreq hooks from 4.9-rc4 2017-06-02 08:01:50 -07:00
cpufreq_schedutil.c BACKPORT: schedutil: Reset cached freq if it is not in sync with next_freq 2017-12-12 16:45:55 +00:00
cpupri.c Merge commit '3cf2f34' into sched/core, to fix build error 2014-06-12 13:46:37 +02:00
cpupri.h sched/cpupri: Remove unnecessary definitions in cpupri.h 2014-11-16 10:58:59 +01:00
cputime.c ANDROID: Reduce use of #ifdef CONFIG_CPU_FREQ_TIMES 2018-07-18 13:21:13 +00:00
deadline.c This is the 4.4.114 stable release 2018-01-31 14:08:55 +01:00
debug.c sched/fair: Add eas (& cas) specific rq, sd and task stats 2017-06-02 08:01:54 -07:00
energy.c sched: Support for extracting EAS energy costs from DT 2016-05-10 16:49:53 +08:00
fair.c UPSTREAM: sched/fair: Consider RT/IRQ pressure in capacity_spare_wake 2018-05-29 09:01:57 -07:00
features.h sched: Add Kconfig option DEFAULT_USE_ENERGY_AWARE to set ENERGY_AWARE feature flag 2016-09-21 14:29:06 -07:00
idle.c vmstat: make vmstat_updater deferrable again and shut down on idle 2016-08-11 14:26:53 -07:00
idle_task.c FROMLIST: sched/fair: Use wake_q length as a hint for wake_wide 2017-10-27 18:50:59 +00:00
loadavg.c sched/loadavg: Avoid loadavg spikes caused by delayed NO_HZ accounting 2017-07-05 14:37:21 +02:00
Makefile BACKPORT: kernel: add kcov code coverage 2017-12-18 09:41:57 -08:00
rt.c This is the 4.4.134 stable release 2018-05-30 13:25:24 +02:00
sched.h This is the 4.4.116 stable release 2018-02-20 16:23:06 +01:00
stats.c schedstats/eas: guard properly to avoid breaking non-smp schedstats users 2017-06-03 15:03:03 +01:00
stats.h sched/stat: Simplify the sched_info accounting dependency 2015-07-04 10:04:30 +02:00
stop_task.c FROMLIST: sched/fair: Use wake_q length as a hint for wake_wide 2017-10-27 18:50:59 +00:00
tune.c Revert "ANDROID: sched/tune: Initialize raw_spin_lock in boosted_groups" 2017-09-23 01:25:03 +00:00
tune.h sched/tune: Introducing a new schedtune attribute prefer_idle 2016-08-11 14:26:49 -07:00
wait.c sched/wait: Fix the signal handling fix 2015-12-13 14:30:59 -08:00
walt.c sched: walt: Correct WALT window size initialization 2017-10-27 11:58:48 -07:00
walt.h sched: walt: Correct WALT window size initialization 2017-10-27 11:58:48 -07:00