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: Rafael Wysocki <rjw@rjwysocki.net>,
	Lists linaro-kernel <linaro-kernel@lists.linaro.org>,
	"linux-pm@vger.kernel.org" <linux-pm@vger.kernel.org>
Subject: Re: [PATCH 12/13] cpufreq: stats: Fix locking
Date: Wed, 24 Dec 2014 14:01:01 -0500	[thread overview]
Message-ID: <549B0D6D.8010705@redhat.com> (raw)
In-Reply-To: <CAKohpo=B99MdiJRvH021kOhU08V-W1g+a=FFpcC9tEHEDKjOvw@mail.gmail.com>



On 12/23/2014 05:55 AM, Viresh Kumar wrote:
> On 23 December 2014 at 16:17, Prarit Bhargava <prarit@redhat.com> wrote:
>>>       sysfs_remove_group(&policy->kobj, &stats_attr_group);
>>
>> ^^^ this line is not guaranteed to have removed the group files by the time it
> 
> Strange .. Under what conditions can the above statement be true?

Suppose the following happens:

1.  CPU A holds a file descriptor in userspace for one of the cpu stats files.

2.  CPU B is removing the group

3.  CPU B calls sysfs_remove_group, which does not remove the file because there
is a use count on it (because CPU A holds the file open).

4.  CPU B now brings up stats again ... and we hit the a collision in the sysfs
space.

FWIW: I've hit this scenario in all types of hotplug (memory, pci, and cpu).  I
understand why the sysfs_group and sysfs_file remove functions do NOT block on
use counts because it is entirely possible that userspace has done something
braindead which would then prevent the kernel from making progress, or in the
case of an error return (ex, -EBUSY) prevent hotplug from succeeding.  I dunno
if there is a way out of this :/

> 
>> returns.  That is important in this code IMO, as we are adding and removing the
>> stats entries rapidly.  You may hit the dreaded "sysfs file already exists"
>> WARNING if this is done too fast, or if sysfs is locked for some other reason
>> and delays the removal of the group.  In the worst case, it is possible you hit
>> a NULL pointer due to stale data access (because you kfree and set to a NULL
>> pointer below).
> 
> Hmm, I have seen such stuff yet for sure. But anyway, that would be out of scope
> of this patch. As w or w/o locking, it is broken :)

Yeah, I agree ... I'm just pointing it out as a problem here.  I'm simply trying
to make you aware of the conditions on which this locking may fail.

> 
>> I've never found a good method to block this sort of add/remove sysfs file race
>> from happening.  Perhaps someone else may have a suggestion.  In the past I've
>> thought about adding a usage count to the sysfs file handlers themselves but
>> that seems to lead to blocking on userspace access...
> 
> Some more details and we can drag Greg-kh into this, but lets be sure of what we
> are asking..
> 
>>>       kfree(stat->time_in_state);
>>>       kfree(stat);
>>>       policy->stats_data = NULL;
>>> +     spin_unlock(&cpufreq_stats_lock);
>>>  }
>>
>> There may not be an easy way around this ...
> 
> You were just completing the earlier horror story or is this a new comment which
> I couldn't understand ?

Sorry -- completing the earlier horror story :)

> 
> Which IRC do you hangout on? Just in case I need..

You can usually find me in freenode, #fedora-kernel.  I'm *really* bad about
marking myself away though ;) so don't be annoyed if I don't answer right away.

P.

> 
> --
> viresh
> 

  reply	other threads:[~2014-12-24 19:01 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-18 12:00 [PATCH 00/13] cpufreq: stats: cleanups Viresh Kumar
2014-12-18 12:00 ` [PATCH 01/13] cpufreq: stats: don't break strings into multiple lines Viresh Kumar
2014-12-18 12:00 ` [PATCH 02/13] cpufreq: stats: return -EEXIST when stats are already allocated Viresh Kumar
2014-12-18 12:00 ` [PATCH 03/13] cpufreq: stats: don't check for freq table while freeing stats Viresh Kumar
2014-12-18 12:00 ` [PATCH 04/13] cpufreq: stats: pass 'stat' to cpufreq_stats_update() Viresh Kumar
2014-12-18 12:00 ` [PATCH 05/13] cpufreq: stats: get rid of per-cpu cpufreq_stats_table Viresh Kumar
2014-12-18 12:00 ` [PATCH 06/13] cpufreq: Remove (now) unused 'last_cpu' from struct cpufreq_policy Viresh Kumar
2014-12-18 12:00 ` [PATCH 07/13] cpufreq: stats: remove cpufreq_stats_update_policy_cpu() Viresh Kumar
2014-12-18 12:00 ` [PATCH 08/13] cpufreq: remove CPUFREQ_UPDATE_POLICY_CPU notifications Viresh Kumar
2014-12-18 12:00 ` [PATCH 09/13] cpufreq: stats: create sysfs group once we are ready Viresh Kumar
2014-12-18 12:00 ` [PATCH 10/13] cpufreq: stats: don't update stats from show_trans_table() Viresh Kumar
2014-12-18 12:00 ` [PATCH 11/13] cpufreq: stats: don't update stats on false notifiers Viresh Kumar
2014-12-18 12:00 ` [PATCH 12/13] cpufreq: stats: Fix locking Viresh Kumar
2014-12-23  6:06   ` Viresh Kumar
2014-12-23 10:47     ` Prarit Bhargava
2014-12-23 10:55       ` Viresh Kumar
2014-12-24 19:01         ` Prarit Bhargava [this message]
2014-12-18 12:00 ` [PATCH 13/13] cpufreq: stats: call cpufreq_stats_update() with locks held Viresh Kumar
2014-12-19  2:15 ` [PATCH 00/13] cpufreq: stats: cleanups Rafael J. Wysocki
2014-12-29  4:41   ` Viresh Kumar
2014-12-30 14:56     ` Prarit Bhargava

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=549B0D6D.8010705@redhat.com \
    --to=prarit@redhat.com \
    --cc=linaro-kernel@lists.linaro.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=rjw@rjwysocki.net \
    --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).