linux-pm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Prarit Bhargava <prarit@redhat.com>
To: Viresh Kumar <viresh.kumar@linaro.org>
Cc: "Saravana Kannan" <skannan@codeaurora.org>,
	"Rafael J. Wysocki" <rjw@rjwysocki.net>,
	"linux-pm@vger.kernel.org" <linux-pm@vger.kernel.org>,
	"Linux Kernel" <linux-kernel@vger.kernel.org>,
	"Robert Schöne" <robert.schoene@tu-dresden.de>
Subject: Re: Locking issues with cpufreq and sysfs
Date: Fri, 17 Oct 2014 08:14:07 -0400	[thread overview]
Message-ID: <5441080F.1070900@redhat.com> (raw)
In-Reply-To: <CAKohponYvbpaphKCHoPH=6jDCpWop_BH_DKq6HwOx0SEe39sqw@mail.gmail.com>



On 10/16/2014 07:23 AM, Viresh Kumar wrote:
> On 14 October 2014 23:54, Prarit Bhargava <prarit@redhat.com> wrote:
>> Here's what I think we should do.  Taking a step back, the purpose of the
>> cpufreq sysfs files is to allow userspace to read current cpu frequency
>> settings, and to allow userspce to modify the governor and set the max & min
>> ranges for cpu frequencies.  This can be done per device or for all cpus
>> depending on the driver.
> 
> Okay.
> 
>> We have to guarantee that bothing reading and writing will always work and that
>> write operations will always be atomic relative to userspace.  The current
> 
> Ok.
> 
>> implementation of cpufreq does this through the following locks:
>>
>> cpufreq_driver_lock: protects the cpufreq_cpu_data array and cpufreq_driver->boost
>> cpufreq_governor_lock: protects the current governor
> 
> Its just for serialization..
> 
>> cpufreq_governor_mutex: protects the cpufreq_governor_list
>> cpufreq_rwsem: protects the driver from being unloaded
>> global_kobj_lock: protects the "cpufreq" kobject
>> each policy has a rwsem (policy->rwsem): protects the cpufreq_policy struct
>> each policy has a transition_lock (policy->transition): synchronizes
>>                                                         frequency transitions
>>
>> While examining this code I was wondering exactly why we allow multiple readers
>> and writers in cpufreq.  I could understand if we felt that this data was
>> critical; but it really isn't.  A short delay here isn't that big of a deal IMO
>> (if someone can produce a case where a delay would cause a serious problem I'd
>>  like to hear it).  I don't even think it is safe in most cases to allow readers
>> while cpufreq values are changing; if we're changing the governor userspace
>> cannot rely on the value of (for example) cpuinfo_max_freq.
> 
> I don't know how reader writer lock will fail and a normal lock will not.
> There is only benefit of rwlock, that readers can read things while
> there is nobody
> writing..
> 
>> So I'm proposing that we move to a single threaded read/write using, if
> 
> Okay, but how will that benefit us ?

It will greatly simplify the code.  The locking isn't working in this code at
all right now and is causing various reported panics ... you yourself are
pushing a lock patch that serializes operations -- which is causing other
problems during testing.

> 
>> possible, a single policy lock for now.  We might transition this back to a
>> rwsem later on, however, for the first attempt at cleaning this up I think we
>> should just stick with a simple lock.  In doing that, IMO we remove
>>
>> cpufreq_rwsem: protects the driver from being unloaded
>> cpufreq_governor_lock: protects the current governor
>> each policy has a rwsem (policy->rwsem): protects the cpufreq_policy struct
>>
>> and potentially
>>
>> cpufreq_driver_lock: protects the cpufreq_cpu_data array and cpufreq_driver->boost
> 
> Not really sure, but yeah we might be able to club few of them..
> 
>> After looking at the way the code would be structured, I'm wondering if
>>
>> cpufreq_governor_mutex: protects the cpufreq_governor_list
>>
>> is overkill.  The loading of a module should be atomic relative to the cpufreq
>> code, so this lock may not be required.  (Admittedly I haven't tested that...)
>>
>> That would leave:
>>
>> global_kobj_lock: protects the "cpufreq" kobject
>> each policy has a transition_lock (policy->transition): synchronizes
>>                                                         frequency transitions
>>
>> and a new lock, perhaps called policy->lock, to serialize all events.
>>
>> Pros: We clean all this up to a simpler single threaded model.  Bugs and races
>> here would be much easier to handle.  We're currently putting band-aid on
>> band-aids in this code ATM and it looks like we're seeing old races expanded
>> or new races exposed.
>>
>> Cons: We lose the ability to do simultaneous reads and writes ... although
>> I remain unconvinced that this would ever be safe to do.  ie) If I change the
>> governor while at the same time reading, for example, the current cpu
>> frequency I cannot rely on that value to be valid.
>>
>> After that we can add some reference counting to the sysfs file accesses
>> so that we can block after the sysfs removal when we change cpufreq
>> governors.  I think that would be trivial and that it would resolve any races
>> when adding and removing governor's sysfs files.
> 
> Not really sure, but if you solve few things with getting these bugs resolved
> then we might apply your patches without any issues.
> 

  reply	other threads:[~2014-10-17 12:14 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-13 13:11 Locking issues with cpufreq and sysfs Prarit Bhargava
2014-10-13 13:22 ` Prarit Bhargava
2014-10-13 15:09   ` Rafael J. Wysocki
2014-10-14  7:10 ` Viresh Kumar
2014-10-14 18:24   ` Prarit Bhargava
2014-10-14 19:18     ` Elliott, Robert (Server Storage)
2014-10-16 11:23     ` Viresh Kumar
2014-10-17 12:14       ` Prarit Bhargava [this message]
2014-10-17 11:38 ` Viresh Kumar
2014-10-17 12:15   ` Prarit Bhargava
2014-10-17 13:25     ` Viresh Kumar

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=5441080F.1070900@redhat.com \
    --to=prarit@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=rjw@rjwysocki.net \
    --cc=robert.schoene@tu-dresden.de \
    --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).