* [2/4,v5] memstick: Prevent memstick host from getting runtime suspended during card detection
@ 2018-10-24 8:49 Kai-Heng Feng
0 siblings, 0 replies; 7+ messages in thread
From: Kai-Heng Feng @ 2018-10-24 8:49 UTC (permalink / raw)
To: arnd, gregkh; +Cc: ulf.hansson, stern, linux-usb, linux-kernel, Kai-Heng Feng
We can use MEMSTICK_POWER_{ON,OFF} along with pm_runtime_{get,put}
helpers to let memstick host support runtime pm.
There's a small window between memstick_detect_change() and its queued
work, memstick_check(). In this window the rpm count may go down to zero
before the memstick host powers on, so the host can be inadvertently
suspended.
Increment rpm count before calling memstick_check(), and decrement rpm
count afterward, as now we are sure the memstick host should be
suspended or not.
Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
---
drivers/memstick/core/memstick.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/drivers/memstick/core/memstick.c b/drivers/memstick/core/memstick.c
index 76382c858c35..5f16a8826401 100644
--- a/drivers/memstick/core/memstick.c
+++ b/drivers/memstick/core/memstick.c
@@ -18,6 +18,7 @@
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/module.h>
+#include <linux/pm_runtime.h>
#define DRIVER_NAME "memstick"
@@ -209,6 +210,7 @@ static int memstick_dummy_check(struct memstick_dev *card)
*/
void memstick_detect_change(struct memstick_host *host)
{
+ pm_runtime_get_noresume(host->dev.parent);
queue_work(workqueue, &host->media_checker);
}
EXPORT_SYMBOL(memstick_detect_change);
@@ -479,6 +481,8 @@ static void memstick_check(struct work_struct *work)
host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
mutex_unlock(&host->lock);
+
+ pm_runtime_put(host->dev.parent);
dev_dbg(&host->dev, "memstick_check finished\n");
}
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [2/4,v5] memstick: Prevent memstick host from getting runtime suspended during card detection
@ 2018-10-29 12:25 Ulf Hansson
0 siblings, 0 replies; 7+ messages in thread
From: Ulf Hansson @ 2018-10-29 12:25 UTC (permalink / raw)
To: Kai-Heng Feng
Cc: Arnd Bergmann, Greg Kroah-Hartman, Alan Stern, Linux USB List,
Linux Kernel Mailing List
On 24 October 2018 at 10:49, Kai-Heng Feng <kai.heng.feng@canonical.com> wrote:
> We can use MEMSTICK_POWER_{ON,OFF} along with pm_runtime_{get,put}
> helpers to let memstick host support runtime pm.
>
> There's a small window between memstick_detect_change() and its queued
> work, memstick_check(). In this window the rpm count may go down to zero
> before the memstick host powers on, so the host can be inadvertently
> suspended.
>
> Increment rpm count before calling memstick_check(), and decrement rpm
> count afterward, as now we are sure the memstick host should be
> suspended or not.
>
> Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
> ---
> drivers/memstick/core/memstick.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/drivers/memstick/core/memstick.c b/drivers/memstick/core/memstick.c
> index 76382c858c35..5f16a8826401 100644
> --- a/drivers/memstick/core/memstick.c
> +++ b/drivers/memstick/core/memstick.c
> @@ -18,6 +18,7 @@
> #include <linux/delay.h>
> #include <linux/slab.h>
> #include <linux/module.h>
> +#include <linux/pm_runtime.h>
>
> #define DRIVER_NAME "memstick"
>
> @@ -209,6 +210,7 @@ static int memstick_dummy_check(struct memstick_dev *card)
> */
> void memstick_detect_change(struct memstick_host *host)
> {
> + pm_runtime_get_noresume(host->dev.parent);
> queue_work(workqueue, &host->media_checker);
> }
> EXPORT_SYMBOL(memstick_detect_change);
> @@ -479,6 +481,8 @@ static void memstick_check(struct work_struct *work)
> host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
>
> mutex_unlock(&host->lock);
> +
> + pm_runtime_put(host->dev.parent);
> dev_dbg(&host->dev, "memstick_check finished\n");
> }
>
I am not sure this works, sorry.
More precisely, I don't think there is a guarantee that the calls to
pm_runtime_get|put*() becomes properly balanced. In principle
memstick_detect_change() could be called, without actually causing a
new work to be scheduled if there is already such a work in the queue
(depends on the workqueue configuration). Isn't it so?
Kind regards
Uffe
^ permalink raw reply [flat|nested] 7+ messages in thread
* [2/4,v5] memstick: Prevent memstick host from getting runtime suspended during card detection
@ 2018-10-29 16:31 Kai-Heng Feng
0 siblings, 0 replies; 7+ messages in thread
From: Kai-Heng Feng @ 2018-10-29 16:31 UTC (permalink / raw)
To: Ulf Hansson
Cc: Arnd Bergmann, Greg Kroah-Hartman, Alan Stern, Linux USB List,
Linux Kernel Mailing List
> On Oct 29, 2018, at 20:25, Ulf Hansson <ulf.hansson@linaro.org> wrote:
>
> On 24 October 2018 at 10:49, Kai-Heng Feng <kai.heng.feng@canonical.com> wrote:
>> We can use MEMSTICK_POWER_{ON,OFF} along with pm_runtime_{get,put}
>> helpers to let memstick host support runtime pm.
>>
>> There's a small window between memstick_detect_change() and its queued
>> work, memstick_check(). In this window the rpm count may go down to zero
>> before the memstick host powers on, so the host can be inadvertently
>> suspended.
>>
>> Increment rpm count before calling memstick_check(), and decrement rpm
>> count afterward, as now we are sure the memstick host should be
>> suspended or not.
>>
>> Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
>> ---
>> drivers/memstick/core/memstick.c | 4 ++++
>> 1 file changed, 4 insertions(+)
>>
>> diff --git a/drivers/memstick/core/memstick.c b/drivers/memstick/core/memstick.c
>> index 76382c858c35..5f16a8826401 100644
>> --- a/drivers/memstick/core/memstick.c
>> +++ b/drivers/memstick/core/memstick.c
>> @@ -18,6 +18,7 @@
>> #include <linux/delay.h>
>> #include <linux/slab.h>
>> #include <linux/module.h>
>> +#include <linux/pm_runtime.h>
>>
>> #define DRIVER_NAME "memstick"
>>
>> @@ -209,6 +210,7 @@ static int memstick_dummy_check(struct memstick_dev *card)
>> */
>> void memstick_detect_change(struct memstick_host *host)
>> {
>> + pm_runtime_get_noresume(host->dev.parent);
>> queue_work(workqueue, &host->media_checker);
>> }
>> EXPORT_SYMBOL(memstick_detect_change);
>> @@ -479,6 +481,8 @@ static void memstick_check(struct work_struct *work)
>> host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
>>
>> mutex_unlock(&host->lock);
>> +
>> + pm_runtime_put(host->dev.parent);
>> dev_dbg(&host->dev, "memstick_check finished\n");
>> }
>>
>
> I am not sure this works, sorry.
>
> More precisely, I don't think there is a guarantee that the calls to
> pm_runtime_get|put*() becomes properly balanced. In principle
> memstick_detect_change() could be called, without actually causing a
> new work to be scheduled if there is already such a work in the queue
> (depends on the workqueue configuration). Isn't it so?
You are right.
We can use test_and_set_bit() or alike to properly balance pm_runtime
helpers, but the most straightforward solution in my mind is to merge
memstick_detect_change() and memstick_check() as one function.
memstick_detect_change() it’s the only user of memstick_check() anyway.
Or is there a better way in your mind?
Kai-Heng
>
> Kind regards
> Uffe
^ permalink raw reply [flat|nested] 7+ messages in thread
* [2/4,v5] memstick: Prevent memstick host from getting runtime suspended during card detection
@ 2018-10-30 13:03 Ulf Hansson
0 siblings, 0 replies; 7+ messages in thread
From: Ulf Hansson @ 2018-10-30 13:03 UTC (permalink / raw)
To: Kai Heng Feng
Cc: Arnd Bergmann, Greg Kroah-Hartman, Alan Stern, Linux USB List,
Linux Kernel Mailing List
On 29 October 2018 at 17:31, Kai Heng Feng <kai.heng.feng@canonical.com> wrote:
>
>
>> On Oct 29, 2018, at 20:25, Ulf Hansson <ulf.hansson@linaro.org> wrote:
>>
>> On 24 October 2018 at 10:49, Kai-Heng Feng <kai.heng.feng@canonical.com> wrote:
>>> We can use MEMSTICK_POWER_{ON,OFF} along with pm_runtime_{get,put}
>>> helpers to let memstick host support runtime pm.
>>>
>>> There's a small window between memstick_detect_change() and its queued
>>> work, memstick_check(). In this window the rpm count may go down to zero
>>> before the memstick host powers on, so the host can be inadvertently
>>> suspended.
>>>
>>> Increment rpm count before calling memstick_check(), and decrement rpm
>>> count afterward, as now we are sure the memstick host should be
>>> suspended or not.
>>>
>>> Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
>>> ---
>>> drivers/memstick/core/memstick.c | 4 ++++
>>> 1 file changed, 4 insertions(+)
>>>
>>> diff --git a/drivers/memstick/core/memstick.c b/drivers/memstick/core/memstick.c
>>> index 76382c858c35..5f16a8826401 100644
>>> --- a/drivers/memstick/core/memstick.c
>>> +++ b/drivers/memstick/core/memstick.c
>>> @@ -18,6 +18,7 @@
>>> #include <linux/delay.h>
>>> #include <linux/slab.h>
>>> #include <linux/module.h>
>>> +#include <linux/pm_runtime.h>
>>>
>>> #define DRIVER_NAME "memstick"
>>>
>>> @@ -209,6 +210,7 @@ static int memstick_dummy_check(struct memstick_dev *card)
>>> */
>>> void memstick_detect_change(struct memstick_host *host)
>>> {
>>> + pm_runtime_get_noresume(host->dev.parent);
>>> queue_work(workqueue, &host->media_checker);
>>> }
>>> EXPORT_SYMBOL(memstick_detect_change);
>>> @@ -479,6 +481,8 @@ static void memstick_check(struct work_struct *work)
>>> host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
>>>
>>> mutex_unlock(&host->lock);
>>> +
>>> + pm_runtime_put(host->dev.parent);
>>> dev_dbg(&host->dev, "memstick_check finished\n");
>>> }
>>>
>>
>> I am not sure this works, sorry.
>>
>> More precisely, I don't think there is a guarantee that the calls to
>> pm_runtime_get|put*() becomes properly balanced. In principle
>> memstick_detect_change() could be called, without actually causing a
>> new work to be scheduled if there is already such a work in the queue
>> (depends on the workqueue configuration). Isn't it so?
>
> You are right.
>
> We can use test_and_set_bit() or alike to properly balance pm_runtime
> helpers, but the most straightforward solution in my mind is to merge
> memstick_detect_change() and memstick_check() as one function.
>
> memstick_detect_change() it’s the only user of memstick_check() anyway.
I suspect memstick_detect_change() is supposed to be called by host
drivers, when they receive some kind of notification due to a card
being inserted or removed. I guess that happen (at least
hypothetically) also from atomic (IRQ) context.
As memstick_check() is doing hole bunch of operations, I am not sure
bypassing the work-queue is a good idea, if that is what you are
proposing.
>
> Or is there a better way in your mind?
I don't know.
Well, I am not sure I understand why you need to call
pm_runtime_get_noresume() from memstick_detect_change() in the first
place. Could you explain that in more detail?
Kind regards
Uffe
^ permalink raw reply [flat|nested] 7+ messages in thread
* [2/4,v5] memstick: Prevent memstick host from getting runtime suspended during card detection
@ 2018-10-30 15:23 Kai-Heng Feng
0 siblings, 0 replies; 7+ messages in thread
From: Kai-Heng Feng @ 2018-10-30 15:23 UTC (permalink / raw)
To: Ulf Hansson
Cc: Arnd Bergmann, Greg Kroah-Hartman, Alan Stern, Linux USB List,
Linux Kernel Mailing List
> On Oct 30, 2018, at 21:03, Ulf Hansson <ulf.hansson@linaro.org> wrote:
>
> On 29 October 2018 at 17:31, Kai Heng Feng <kai.heng.feng@canonical.com> wrote:
>>
>>
>>> On Oct 29, 2018, at 20:25, Ulf Hansson <ulf.hansson@linaro.org> wrote:
>>>
>>> On 24 October 2018 at 10:49, Kai-Heng Feng <kai.heng.feng@canonical.com> wrote:
>>>> We can use MEMSTICK_POWER_{ON,OFF} along with pm_runtime_{get,put}
>>>> helpers to let memstick host support runtime pm.
>>>>
>>>> There's a small window between memstick_detect_change() and its queued
>>>> work, memstick_check(). In this window the rpm count may go down to zero
>>>> before the memstick host powers on, so the host can be inadvertently
>>>> suspended.
>>>>
>>>> Increment rpm count before calling memstick_check(), and decrement rpm
>>>> count afterward, as now we are sure the memstick host should be
>>>> suspended or not.
>>>>
>>>> Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
>>>> ---
>>>> drivers/memstick/core/memstick.c | 4 ++++
>>>> 1 file changed, 4 insertions(+)
>>>>
>>>> diff --git a/drivers/memstick/core/memstick.c b/drivers/memstick/core/memstick.c
>>>> index 76382c858c35..5f16a8826401 100644
>>>> --- a/drivers/memstick/core/memstick.c
>>>> +++ b/drivers/memstick/core/memstick.c
>>>> @@ -18,6 +18,7 @@
>>>> #include <linux/delay.h>
>>>> #include <linux/slab.h>
>>>> #include <linux/module.h>
>>>> +#include <linux/pm_runtime.h>
>>>>
>>>> #define DRIVER_NAME "memstick"
>>>>
>>>> @@ -209,6 +210,7 @@ static int memstick_dummy_check(struct memstick_dev *card)
>>>> */
>>>> void memstick_detect_change(struct memstick_host *host)
>>>> {
>>>> + pm_runtime_get_noresume(host->dev.parent);
>>>> queue_work(workqueue, &host->media_checker);
>>>> }
>>>> EXPORT_SYMBOL(memstick_detect_change);
>>>> @@ -479,6 +481,8 @@ static void memstick_check(struct work_struct *work)
>>>> host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
>>>>
>>>> mutex_unlock(&host->lock);
>>>> +
>>>> + pm_runtime_put(host->dev.parent);
>>>> dev_dbg(&host->dev, "memstick_check finished\n");
>>>> }
>>>>
>>>
>>> I am not sure this works, sorry.
>>>
>>> More precisely, I don't think there is a guarantee that the calls to
>>> pm_runtime_get|put*() becomes properly balanced. In principle
>>> memstick_detect_change() could be called, without actually causing a
>>> new work to be scheduled if there is already such a work in the queue
>>> (depends on the workqueue configuration). Isn't it so?
>>
>> You are right.
>>
>> We can use test_and_set_bit() or alike to properly balance pm_runtime
>> helpers, but the most straightforward solution in my mind is to merge
>> memstick_detect_change() and memstick_check() as one function.
>>
>> memstick_detect_change() it’s the only user of memstick_check() anyway.
>
> I suspect memstick_detect_change() is supposed to be called by host
> drivers, when they receive some kind of notification due to a card
> being inserted or removed. I guess that happen (at least
> hypothetically) also from atomic (IRQ) context.
>
> As memstick_check() is doing hole bunch of operations, I am not sure
> bypassing the work-queue is a good idea, if that is what you are
> proposing.
Okay, it’s better to keep it that way.
>
>>
>> Or is there a better way in your mind?
>
> I don't know.
>
> Well, I am not sure I understand why you need to call
> pm_runtime_get_noresume() from memstick_detect_change() in the first
> place. Could you explain that in more detail?
I guess it didn’t explain it well enough in the log, let me add some detail:
There's a small window between memstick_detect_change() and its queued
work, memstick_check(). In this window the rpm count may go down to zero
before the memstick host powers on, where I use
pm_runtime_get_noresume() to increment the rpm count.
memstick_check() uses some functions in rtsx_usb_ms that have
pm_runtime_put*() so the rpm count may go down to zero, before the
memstick host powers on.
Kai-Heng
>
> Kind regards
> Uffe
^ permalink raw reply [flat|nested] 7+ messages in thread
* [2/4,v5] memstick: Prevent memstick host from getting runtime suspended during card detection
@ 2018-10-30 16:04 Ulf Hansson
0 siblings, 0 replies; 7+ messages in thread
From: Ulf Hansson @ 2018-10-30 16:04 UTC (permalink / raw)
To: Kai Heng Feng
Cc: Arnd Bergmann, Greg Kroah-Hartman, Alan Stern, Linux USB List,
Linux Kernel Mailing List
On 30 October 2018 at 16:23, Kai Heng Feng <kai.heng.feng@canonical.com> wrote:
>
>
>> On Oct 30, 2018, at 21:03, Ulf Hansson <ulf.hansson@linaro.org> wrote:
>>
>> On 29 October 2018 at 17:31, Kai Heng Feng <kai.heng.feng@canonical.com> wrote:
>>>
>>>
>>>> On Oct 29, 2018, at 20:25, Ulf Hansson <ulf.hansson@linaro.org> wrote:
>>>>
>>>> On 24 October 2018 at 10:49, Kai-Heng Feng <kai.heng.feng@canonical.com> wrote:
>>>>> We can use MEMSTICK_POWER_{ON,OFF} along with pm_runtime_{get,put}
>>>>> helpers to let memstick host support runtime pm.
>>>>>
>>>>> There's a small window between memstick_detect_change() and its queued
>>>>> work, memstick_check(). In this window the rpm count may go down to zero
>>>>> before the memstick host powers on, so the host can be inadvertently
>>>>> suspended.
>>>>>
>>>>> Increment rpm count before calling memstick_check(), and decrement rpm
>>>>> count afterward, as now we are sure the memstick host should be
>>>>> suspended or not.
>>>>>
>>>>> Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
>>>>> ---
>>>>> drivers/memstick/core/memstick.c | 4 ++++
>>>>> 1 file changed, 4 insertions(+)
>>>>>
>>>>> diff --git a/drivers/memstick/core/memstick.c b/drivers/memstick/core/memstick.c
>>>>> index 76382c858c35..5f16a8826401 100644
>>>>> --- a/drivers/memstick/core/memstick.c
>>>>> +++ b/drivers/memstick/core/memstick.c
>>>>> @@ -18,6 +18,7 @@
>>>>> #include <linux/delay.h>
>>>>> #include <linux/slab.h>
>>>>> #include <linux/module.h>
>>>>> +#include <linux/pm_runtime.h>
>>>>>
>>>>> #define DRIVER_NAME "memstick"
>>>>>
>>>>> @@ -209,6 +210,7 @@ static int memstick_dummy_check(struct memstick_dev *card)
>>>>> */
>>>>> void memstick_detect_change(struct memstick_host *host)
>>>>> {
>>>>> + pm_runtime_get_noresume(host->dev.parent);
>>>>> queue_work(workqueue, &host->media_checker);
>>>>> }
>>>>> EXPORT_SYMBOL(memstick_detect_change);
>>>>> @@ -479,6 +481,8 @@ static void memstick_check(struct work_struct *work)
>>>>> host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
>>>>>
>>>>> mutex_unlock(&host->lock);
>>>>> +
>>>>> + pm_runtime_put(host->dev.parent);
>>>>> dev_dbg(&host->dev, "memstick_check finished\n");
>>>>> }
>>>>>
>>>>
>>>> I am not sure this works, sorry.
>>>>
>>>> More precisely, I don't think there is a guarantee that the calls to
>>>> pm_runtime_get|put*() becomes properly balanced. In principle
>>>> memstick_detect_change() could be called, without actually causing a
>>>> new work to be scheduled if there is already such a work in the queue
>>>> (depends on the workqueue configuration). Isn't it so?
>>>
>>> You are right.
>>>
>>> We can use test_and_set_bit() or alike to properly balance pm_runtime
>>> helpers, but the most straightforward solution in my mind is to merge
>>> memstick_detect_change() and memstick_check() as one function.
>>>
>>> memstick_detect_change() it’s the only user of memstick_check() anyway.
>>
>> I suspect memstick_detect_change() is supposed to be called by host
>> drivers, when they receive some kind of notification due to a card
>> being inserted or removed. I guess that happen (at least
>> hypothetically) also from atomic (IRQ) context.
>>
>> As memstick_check() is doing hole bunch of operations, I am not sure
>> bypassing the work-queue is a good idea, if that is what you are
>> proposing.
>
> Okay, it’s better to keep it that way.
>
>>
>>>
>>> Or is there a better way in your mind?
>>
>> I don't know.
>>
>> Well, I am not sure I understand why you need to call
>> pm_runtime_get_noresume() from memstick_detect_change() in the first
>> place. Could you explain that in more detail?
>
> I guess it didn’t explain it well enough in the log, let me add some detail:
> There's a small window between memstick_detect_change() and its queued
> work, memstick_check(). In this window the rpm count may go down to zero
> before the memstick host powers on, where I use
> pm_runtime_get_noresume() to increment the rpm count.
>
> memstick_check() uses some functions in rtsx_usb_ms that have
> pm_runtime_put*() so the rpm count may go down to zero, before the
> memstick host powers on.
So then, why doesn't memstick_check() early on calls
pm_runtime_get_sync() and when it has finished with probing for a
card, balance that with a call pm_runtime_put()?
Kind regards
Uffe
^ permalink raw reply [flat|nested] 7+ messages in thread
* [2/4,v5] memstick: Prevent memstick host from getting runtime suspended during card detection
@ 2018-10-31 6:33 Kai-Heng Feng
0 siblings, 0 replies; 7+ messages in thread
From: Kai-Heng Feng @ 2018-10-31 6:33 UTC (permalink / raw)
To: Ulf Hansson
Cc: Arnd Bergmann, Greg Kroah-Hartman, Alan Stern, Linux USB List,
Linux Kernel Mailing List
> On Oct 31, 2018, at 12:04 AM, Ulf Hansson <ulf.hansson@linaro.org> wrote:
>
> On 30 October 2018 at 16:23, Kai Heng Feng <kai.heng.feng@canonical.com> wrote:
>>
>>
>>> On Oct 30, 2018, at 21:03, Ulf Hansson <ulf.hansson@linaro.org> wrote:
>>>
>>> On 29 October 2018 at 17:31, Kai Heng Feng <kai.heng.feng@canonical.com> wrote:
>>>>
>>>>
>>>>> On Oct 29, 2018, at 20:25, Ulf Hansson <ulf.hansson@linaro.org> wrote:
>>>>>
>>>>> On 24 October 2018 at 10:49, Kai-Heng Feng <kai.heng.feng@canonical.com> wrote:
>>>>>> We can use MEMSTICK_POWER_{ON,OFF} along with pm_runtime_{get,put}
>>>>>> helpers to let memstick host support runtime pm.
>>>>>>
>>>>>> There's a small window between memstick_detect_change() and its queued
>>>>>> work, memstick_check(). In this window the rpm count may go down to zero
>>>>>> before the memstick host powers on, so the host can be inadvertently
>>>>>> suspended.
>>>>>>
>>>>>> Increment rpm count before calling memstick_check(), and decrement rpm
>>>>>> count afterward, as now we are sure the memstick host should be
>>>>>> suspended or not.
>>>>>>
>>>>>> Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
>>>>>> ---
>>>>>> drivers/memstick/core/memstick.c | 4 ++++
>>>>>> 1 file changed, 4 insertions(+)
>>>>>>
>>>>>> diff --git a/drivers/memstick/core/memstick.c b/drivers/memstick/core/memstick.c
>>>>>> index 76382c858c35..5f16a8826401 100644
>>>>>> --- a/drivers/memstick/core/memstick.c
>>>>>> +++ b/drivers/memstick/core/memstick.c
>>>>>> @@ -18,6 +18,7 @@
>>>>>> #include <linux/delay.h>
>>>>>> #include <linux/slab.h>
>>>>>> #include <linux/module.h>
>>>>>> +#include <linux/pm_runtime.h>
>>>>>>
>>>>>> #define DRIVER_NAME "memstick"
>>>>>>
>>>>>> @@ -209,6 +210,7 @@ static int memstick_dummy_check(struct memstick_dev *card)
>>>>>> */
>>>>>> void memstick_detect_change(struct memstick_host *host)
>>>>>> {
>>>>>> + pm_runtime_get_noresume(host->dev.parent);
>>>>>> queue_work(workqueue, &host->media_checker);
>>>>>> }
>>>>>> EXPORT_SYMBOL(memstick_detect_change);
>>>>>> @@ -479,6 +481,8 @@ static void memstick_check(struct work_struct *work)
>>>>>> host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
>>>>>>
>>>>>> mutex_unlock(&host->lock);
>>>>>> +
>>>>>> + pm_runtime_put(host->dev.parent);
>>>>>> dev_dbg(&host->dev, "memstick_check finished\n");
>>>>>> }
>>>>>>
>>>>>
>>>>> I am not sure this works, sorry.
>>>>>
>>>>> More precisely, I don't think there is a guarantee that the calls to
>>>>> pm_runtime_get|put*() becomes properly balanced. In principle
>>>>> memstick_detect_change() could be called, without actually causing a
>>>>> new work to be scheduled if there is already such a work in the queue
>>>>> (depends on the workqueue configuration). Isn't it so?
>>>>
>>>> You are right.
>>>>
>>>> We can use test_and_set_bit() or alike to properly balance pm_runtime
>>>> helpers, but the most straightforward solution in my mind is to merge
>>>> memstick_detect_change() and memstick_check() as one function.
>>>>
>>>> memstick_detect_change() it’s the only user of memstick_check() anyway.
>>>
>>> I suspect memstick_detect_change() is supposed to be called by host
>>> drivers, when they receive some kind of notification due to a card
>>> being inserted or removed. I guess that happen (at least
>>> hypothetically) also from atomic (IRQ) context.
>>>
>>> As memstick_check() is doing hole bunch of operations, I am not sure
>>> bypassing the work-queue is a good idea, if that is what you are
>>> proposing.
>>
>> Okay, it’s better to keep it that way.
>>
>>>
>>>>
>>>> Or is there a better way in your mind?
>>>
>>> I don't know.
>>>
>>> Well, I am not sure I understand why you need to call
>>> pm_runtime_get_noresume() from memstick_detect_change() in the first
>>> place. Could you explain that in more detail?
>>
>> I guess it didn’t explain it well enough in the log, let me add some detail:
>> There's a small window between memstick_detect_change() and its queued
>> work, memstick_check(). In this window the rpm count may go down to zero
>> before the memstick host powers on, where I use
>> pm_runtime_get_noresume() to increment the rpm count.
>>
>> memstick_check() uses some functions in rtsx_usb_ms that have
>> pm_runtime_put*() so the rpm count may go down to zero, before the
>> memstick host powers on.
>
> So then, why doesn't memstick_check() early on calls
> pm_runtime_get_sync() and when it has finished with probing for a
> card, balance that with a call pm_runtime_put()?
This will do, not sure what I was thinking. Thanks for pointing out.
Kai-Heng
>
> Kind regards
> Uffe
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2018-10-31 6:33 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-10-29 12:25 [2/4,v5] memstick: Prevent memstick host from getting runtime suspended during card detection Ulf Hansson
-- strict thread matches above, loose matches on Subject: below --
2018-10-31 6:33 Kai-Heng Feng
2018-10-30 16:04 Ulf Hansson
2018-10-30 15:23 Kai-Heng Feng
2018-10-30 13:03 Ulf Hansson
2018-10-29 16:31 Kai-Heng Feng
2018-10-24 8:49 Kai-Heng Feng
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).