From: Preeti U Murthy <preeti@linux.vnet.ibm.com>
To: Viresh Kumar <viresh.kumar@linaro.org>,
Rafael Wysocki <rjw@rjwysocki.net>
Cc: linaro-kernel@lists.linaro.org, linux-pm@vger.kernel.org,
ego@linux.vnet.ibm.com, paulus@samba.org,
shilpa.bhat@linux.vnet.ibm.com, prarit@redhat.com,
robert.schoene@tu-dresden.de, skannan@codeaurora.org
Subject: Re: [PATCH 3/3] cpufreq: governor: Serialize governor callbacks
Date: Thu, 04 Jun 2015 16:17:08 +0530 [thread overview]
Message-ID: <55702CAC.1060308@linux.vnet.ibm.com> (raw)
In-Reply-To: <ddd3ef47ef3ce88674139f9070650939477881ba.1433326032.git.viresh.kumar@linaro.org>
On 06/03/2015 03:57 PM, Viresh Kumar wrote:
> There are several races reported in cpufreq core around governors (only
> ondemand and conservative) by different people.
>
> There are at least two race scenarios present in governor code:
> (a) Concurrent access/updates of governor internal structures.
>
> It is possible that fields such as 'dbs_data->usage_count', etc. are
> accessed simultaneously for different policies using same governor
> structure (i.e. CPUFREQ_HAVE_GOVERNOR_PER_POLICY flag unset). And
> because of this we can dereference bad pointers.
>
> For example consider a system with two CPUs with separate 'struct
> cpufreq_policy' instances. CPU0 governor: ondemand and CPU1: powersave.
> CPU0 switching to powersave and CPU1 to ondemand:
> CPU0 CPU1
>
> store* store*
>
> cpufreq_governor_exit() cpufreq_governor_init()
> dbs_data = cdata->gdbs_data;
>
> if (!--dbs_data->usage_count)
> kfree(dbs_data);
>
> dbs_data->usage_count++;
> *Bad pointer dereference*
>
> There are other races possible between EXIT and START/STOP/LIMIT as
> well. Its really complicated.
>
> (b) Switching governor state in bad sequence:
>
> For example trying to switch a governor to START state, when the
> governor is in EXIT state. There are some checks present in
> __cpufreq_governor() but they aren't sufficient as they compare events
> against 'policy->governor_enabled', where as we need to take governor's
> state into account, which can be used by multiple policies.
>
> These two issues need to be solved separately and the responsibility
> should be properly divided between cpufreq and governor core.
>
> The first problem is more about the governor core, as it needs to
> protect its structures properly. And the second problem should be fixed
> in cpufreq core instead of governor, as its all about sequence of
> events.
>
> This patch is trying to solve only the first problem.
>
> There are two types of data we need to protect,
> - 'struct common_dbs_data': No matter what, there is going to be a
> single copy of this per governor.
> - 'struct dbs_data': With CPUFREQ_HAVE_GOVERNOR_PER_POLICY flag set, we
> will have per-policy copy of this data, otherwise a single copy.
>
> Because of such complexities, the mutex present in 'struct dbs_data' is
> insufficient to solve our problem. For example we need to protect
> fetching of 'dbs_data' from different structures at the beginning of
> cpufreq_governor_dbs(), to make sure it isn't currently being updated.
>
> This can be fixed if we can guarantee serialization of event parsing
> code for an individual governor. This is best solved with a mutex per
> governor, and the placeholder for that is 'struct common_dbs_data'.
>
> And so this patch moves the mutex from 'struct dbs_data' to 'struct
> common_dbs_data' and takes it at the beginning and drops it at the end
> of cpufreq_governor_dbs().
>
> Tested with and without following configuration options:
>
> CONFIG_LOCKDEP_SUPPORT=y
> CONFIG_DEBUG_RT_MUTEXES=y
> CONFIG_DEBUG_PI_LIST=y
> CONFIG_DEBUG_SPINLOCK=y
> CONFIG_DEBUG_MUTEXES=y
> CONFIG_DEBUG_LOCK_ALLOC=y
> CONFIG_PROVE_LOCKING=y
> CONFIG_LOCKDEP=y
> CONFIG_DEBUG_ATOMIC_SLEEP=y
>
> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Preeti U Murthy <preeti@linux.vnet.ibm.com>
next prev parent reply other threads:[~2015-06-04 10:47 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-06-03 10:27 [PATCH 0/3] cpufreq: governor: Fix potential races Viresh Kumar
2015-06-03 10:27 ` [PATCH 1/3] cpufreq: governor: register notifier from cs_init() Viresh Kumar
2015-06-04 5:38 ` Preeti U Murthy
2015-06-04 6:02 ` Viresh Kumar
2015-06-04 7:33 ` Preeti U Murthy
2015-06-03 10:27 ` [PATCH 2/3] cpufreq: governor: split cpufreq_governor_dbs() Viresh Kumar
2015-06-04 10:04 ` Preeti U Murthy
2015-06-04 10:17 ` Viresh Kumar
2015-06-04 11:13 ` [PATCH V2 " Viresh Kumar
2015-06-05 2:51 ` Preeti U Murthy
2015-06-03 10:27 ` [PATCH 3/3] cpufreq: governor: Serialize governor callbacks Viresh Kumar
2015-06-04 10:47 ` Preeti U Murthy [this message]
2015-06-04 5:14 ` [PATCH 0/3] cpufreq: governor: Fix potential races Preeti U Murthy
2015-06-04 6:08 ` Preeti U Murthy
2015-06-04 6:11 ` Viresh Kumar
2015-06-04 6:36 ` Preeti U Murthy
2015-06-04 6:42 ` Viresh Kumar
2015-06-04 7:04 ` Preeti U Murthy
2015-06-04 7:13 ` Viresh Kumar
2015-06-04 7:27 ` Preeti U Murthy
2015-06-05 3:00 ` Viresh Kumar
2015-06-05 3:04 ` Preeti U Murthy
2015-06-05 4:05 ` Preeti U Murthy
2015-06-15 23:48 ` Rafael J. Wysocki
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=55702CAC.1060308@linux.vnet.ibm.com \
--to=preeti@linux.vnet.ibm.com \
--cc=ego@linux.vnet.ibm.com \
--cc=linaro-kernel@lists.linaro.org \
--cc=linux-pm@vger.kernel.org \
--cc=paulus@samba.org \
--cc=prarit@redhat.com \
--cc=rjw@rjwysocki.net \
--cc=robert.schoene@tu-dresden.de \
--cc=shilpa.bhat@linux.vnet.ibm.com \
--cc=skannan@codeaurora.org \
--cc=viresh.kumar@linaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).