From: Javi Merino <javi.merino@arm.com>
To: Michele Di Giorgio <michele.digiorgio@arm.com>
Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org,
Zhang Rui <rui.zhang@intel.com>,
Eduardo Valentin <edubezval@gmail.com>,
Peter Feuerer <peter@piie.net>
Subject: Re: [PATCH] thermal: fix race condition when updating cooling device
Date: Mon, 6 Jun 2016 13:51:14 +0100 [thread overview]
Message-ID: <20160606125113.GC2897@e104805> (raw)
In-Reply-To: <1464877531-2161-1-git-send-email-michele.digiorgio@arm.com>
On Thu, Jun 02, 2016 at 03:25:31PM +0100, Michele Di Giorgio wrote:
> When multiple thermal zones are bound to the same cooling device, multiple
> kernel threads may want to update the cooling device state by calling
> thermal_cdev_update(). Having cdev not protected by a mutex can lead to a race
> condition. Consider the following situation with two kernel threads k1 and k2:
>
> Thread k1 Thread k2
> ||
> || call thermal_cdev_update()
> || ...
> || set_cur_state(cdev, target);
> call power_actor_set_power() ||
> ... ||
> instance->target = state; ||
> cdev->updated = false; ||
> || cdev->updated = true;
> || // completes execution
> call thermal_cdev_update() ||
> // cdev->updated == true ||
> return; ||
> \/
> time
>
> k2 has already looped through the thermal instances looking for the deepest
> cooling device state and is preempted right before setting cdev->updated to
> true. Now, k1 runs, modifies the thermal instance state and sets cdev->updated
> to false. Then, k1 is preempted and k2 continues the execution by setting
> cdev->updated to true, therefore preventing k1 from performing the update.
> Notice that this is not an issue if k2 looks at the instance->target modified by
> k1 "after" it is assigned by k1. In fact, in this case the update will happen
> anyway and k1 can safely return immediately from thermal_cdev_update().
>
> This may lead to a situation where a thermal governor never updates the cooling
> device. For example, this is the case for the step_wise governor: when calling
> the function thermal_zone_trip_update(), the governor may always get a new state
> equal to the old one (which, however, wasn't notified to the cooling device) and
> will therefore skip the update.
>
> CC: Zhang Rui <rui.zhang@intel.com>
> CC: Eduardo Valentin <edubezval@gmail.com>
> CC: Peter Feuerer <peter@piie.net>
> Reported-by: Toby Huang <toby.huang@arm.com>
> Signed-off-by: Michele Di Giorgio <michele.digiorgio@arm.com>
> ---
> Protecting only the assignment of cdev->updated with mutexes may look
> suspicious, but it is necessary to guarantee synchronization and avoiding the
> situation described in the commit message.
>
> There are other two possible solutions.
>
> Moving the cdev->lock mutex outside thermal_cdev_update() and protect both the
> assignment and the function. This would work, but will probably cause many
> issues when updating all the modules that use thermal_cdev_update().
>
> The other solution is to make cdev->updated an atomic_t, change the if
> condition to an atomic_cmpxchg and extend the critical section to include the
> call to cdev->ops->set_cur_state().
True. In any case, the mutex needs to cover set_cur_state() in
thermal_cdev_update(). This fixes the race condition, so I'm happy
with it.
Reviewed-by: Javi Merino <javi.merino@arm.com>
> drivers/thermal/fair_share.c | 2 ++
> drivers/thermal/gov_bang_bang.c | 2 ++
> drivers/thermal/power_allocator.c | 2 ++
> drivers/thermal/step_wise.c | 2 ++
> drivers/thermal/thermal_core.c | 10 +++++++---
> 5 files changed, 15 insertions(+), 3 deletions(-)
next prev parent reply other threads:[~2016-06-06 12:51 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-06-02 14:25 [PATCH] thermal: fix race condition when updating cooling device Michele Di Giorgio
2016-06-06 12:51 ` Javi Merino [this message]
2016-06-27 8:54 ` Michele DiGiorgio
2016-06-27 8:54 ` Michele DiGiorgio
-- strict thread matches above, loose matches on Subject: below --
2016-07-20 9:46 Michele Di Giorgio
2016-08-05 13:21 ` Michele DiGiorgio
2016-08-05 13:21 ` Michele DiGiorgio
2016-08-11 21:50 ` Peter Feuerer
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=20160606125113.GC2897@e104805 \
--to=javi.merino@arm.com \
--cc=edubezval@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pm@vger.kernel.org \
--cc=michele.digiorgio@arm.com \
--cc=peter@piie.net \
--cc=rui.zhang@intel.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.