linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] drivers: base: power: Fix GFP_KERNEL in spinlock context
@ 2017-12-12 13:45 shrikant.maurya
  2017-12-12 14:58 ` Geert Uytterhoeven
  2017-12-12 16:55 ` Rafael J. Wysocki
  0 siblings, 2 replies; 7+ messages in thread
From: shrikant.maurya @ 2017-12-12 13:45 UTC (permalink / raw)
  To: rjw, pavel, len.brown, gregkh
  Cc: linux-pm, linux-kernel, sunil.m, karthik, raghu, Shrikant Maurya

From: Shrikant Maurya <shrikant.maurya@techveda.org>

As reported by Jia-Ju Bai (https://lkml.org/lkml/2017/12/11/872):
API's are using GFP_KERNEL to allocate memory which may sleep.

To ensure atomicity such allocations must be avoided in critical
sections under spinlock.
Fixed by replacing GFP_KERNEL to GFP_ATOMIC.

Reported-by: Jia-Ju Bai <baijiaju1990@gmail.com>
Signed-off-by: Shrikant Maurya <shrikant.maurya@techveda.org>
Signed-off-by: Suniel Mahesh <sunil.m@techveda.org>
Signed-off-by: Raghu Bharadwaj <raghu@techveda.org>
Signed-off-by: Karthik Tummala <karthik@techveda.org>
---
Note:
- Patch was compile tested and built(ARCH=arm) on linux-next
  (latest).
- No build issues reported.
---
 drivers/base/power/wakeup.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/base/power/wakeup.c b/drivers/base/power/wakeup.c
index 38559f0..de56952 100644
--- a/drivers/base/power/wakeup.c
+++ b/drivers/base/power/wakeup.c
@@ -92,11 +92,11 @@ struct wakeup_source *wakeup_source_create(const char *name)
 {
 	struct wakeup_source *ws;
 
-	ws = kmalloc(sizeof(*ws), GFP_KERNEL);
+	ws = kmalloc(sizeof(*ws), GFP_ATOMIC);
 	if (!ws)
 		return NULL;
 
-	wakeup_source_prepare(ws, name ? kstrdup_const(name, GFP_KERNEL) : NULL);
+	wakeup_source_prepare(ws, name ? kstrdup_const(name, GFP_ATOMIC) : NULL);
 	return ws;
 }
 EXPORT_SYMBOL_GPL(wakeup_source_create);
-- 
1.9.1

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH] drivers: base: power: Fix GFP_KERNEL in spinlock context
  2017-12-12 13:45 [PATCH] drivers: base: power: Fix GFP_KERNEL in spinlock context shrikant.maurya
@ 2017-12-12 14:58 ` Geert Uytterhoeven
  2017-12-15  8:45   ` Pavel Machek
  2017-12-16  8:14   ` shrikant
  2017-12-12 16:55 ` Rafael J. Wysocki
  1 sibling, 2 replies; 7+ messages in thread
From: Geert Uytterhoeven @ 2017-12-12 14:58 UTC (permalink / raw)
  To: shrikant.maurya
  Cc: Rafael J. Wysocki, Pavel Machek, Len Brown, Greg KH,
	Linux PM list, linux-kernel, Suniel Mahesh, Karthik Tummala,
	raghu

Hi Shrikant,

On Tue, Dec 12, 2017 at 2:45 PM,  <shrikant.maurya@techveda.org> wrote:
> From: Shrikant Maurya <shrikant.maurya@techveda.org>
>
> As reported by Jia-Ju Bai (https://lkml.org/lkml/2017/12/11/872):
> API's are using GFP_KERNEL to allocate memory which may sleep.
>
> To ensure atomicity such allocations must be avoided in critical
> sections under spinlock.
> Fixed by replacing GFP_KERNEL to GFP_ATOMIC.
>
> Reported-by: Jia-Ju Bai <baijiaju1990@gmail.com>
> Signed-off-by: Shrikant Maurya <shrikant.maurya@techveda.org>
> Signed-off-by: Suniel Mahesh <sunil.m@techveda.org>
> Signed-off-by: Raghu Bharadwaj <raghu@techveda.org>
> Signed-off-by: Karthik Tummala <karthik@techveda.org>

Can't the call to device_init_wakeup() in isp116x_start() just be moved
below the spinlock release?

> --- a/drivers/base/power/wakeup.c
> +++ b/drivers/base/power/wakeup.c
> @@ -92,11 +92,11 @@ struct wakeup_source *wakeup_source_create(const char *name)
>  {
>         struct wakeup_source *ws;
>
> -       ws = kmalloc(sizeof(*ws), GFP_KERNEL);
> +       ws = kmalloc(sizeof(*ws), GFP_ATOMIC);

With GFP_ATOMIC, allocation failure is much more likely to occur.
So IMHO it's better to fix the isp116x, than to impose this burden on
every user.

>         if (!ws)
>                 return NULL;
>
> -       wakeup_source_prepare(ws, name ? kstrdup_const(name, GFP_KERNEL) : NULL);
> +       wakeup_source_prepare(ws, name ? kstrdup_const(name, GFP_ATOMIC) : NULL);
>         return ws;
>  }
>  EXPORT_SYMBOL_GPL(wakeup_source_create);

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
                                -- Linus Torvalds

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] drivers: base: power: Fix GFP_KERNEL in spinlock context
  2017-12-12 13:45 [PATCH] drivers: base: power: Fix GFP_KERNEL in spinlock context shrikant.maurya
  2017-12-12 14:58 ` Geert Uytterhoeven
@ 2017-12-12 16:55 ` Rafael J. Wysocki
  2017-12-16  8:21   ` shrikant
  1 sibling, 1 reply; 7+ messages in thread
From: Rafael J. Wysocki @ 2017-12-12 16:55 UTC (permalink / raw)
  To: shrikant.maurya
  Cc: Rafael J. Wysocki, Pavel Machek, Len Brown, Greg Kroah-Hartman,
	Linux PM, Linux Kernel Mailing List, sunil.m, karthik, raghu

On Tue, Dec 12, 2017 at 2:45 PM,  <shrikant.maurya@techveda.org> wrote:
> From: Shrikant Maurya <shrikant.maurya@techveda.org>
>
> As reported by Jia-Ju Bai (https://lkml.org/lkml/2017/12/11/872):
> API's are using GFP_KERNEL to allocate memory which may sleep.
>
> To ensure atomicity such allocations must be avoided in critical
> sections under spinlock.

That's right.

Which is why wakeup_source_create() should never be called under a spinlock.

Are you aware of any place that happens in the mainline kernel?

Thanks,
Rafael

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] drivers: base: power: Fix GFP_KERNEL in spinlock context
  2017-12-12 14:58 ` Geert Uytterhoeven
@ 2017-12-15  8:45   ` Pavel Machek
  2017-12-16  8:15     ` shrikant
  2017-12-16  8:14   ` shrikant
  1 sibling, 1 reply; 7+ messages in thread
From: Pavel Machek @ 2017-12-15  8:45 UTC (permalink / raw)
  To: Geert Uytterhoeven
  Cc: shrikant.maurya, Rafael J. Wysocki, Len Brown, Greg KH,
	Linux PM list, linux-kernel, Suniel Mahesh, Karthik Tummala,
	raghu

[-- Attachment #1: Type: text/plain, Size: 1889 bytes --]

On Tue 2017-12-12 15:58:00, Geert Uytterhoeven wrote:
> Hi Shrikant,
> 
> On Tue, Dec 12, 2017 at 2:45 PM,  <shrikant.maurya@techveda.org> wrote:
> > From: Shrikant Maurya <shrikant.maurya@techveda.org>
> >
> > As reported by Jia-Ju Bai (https://lkml.org/lkml/2017/12/11/872):
> > API's are using GFP_KERNEL to allocate memory which may sleep.
> >
> > To ensure atomicity such allocations must be avoided in critical
> > sections under spinlock.
> > Fixed by replacing GFP_KERNEL to GFP_ATOMIC.
> >
> > Reported-by: Jia-Ju Bai <baijiaju1990@gmail.com>
> > Signed-off-by: Shrikant Maurya <shrikant.maurya@techveda.org>
> > Signed-off-by: Suniel Mahesh <sunil.m@techveda.org>
> > Signed-off-by: Raghu Bharadwaj <raghu@techveda.org>
> > Signed-off-by: Karthik Tummala <karthik@techveda.org>
> 
> Can't the call to device_init_wakeup() in isp116x_start() just be moved
> below the spinlock release?
> 
> > --- a/drivers/base/power/wakeup.c
> > +++ b/drivers/base/power/wakeup.c
> > @@ -92,11 +92,11 @@ struct wakeup_source *wakeup_source_create(const char *name)
> >  {
> >         struct wakeup_source *ws;
> >
> > -       ws = kmalloc(sizeof(*ws), GFP_KERNEL);
> > +       ws = kmalloc(sizeof(*ws), GFP_ATOMIC);
> 
> With GFP_ATOMIC, allocation failure is much more likely to occur.
> So IMHO it's better to fix the isp116x, than to impose this burden on
> every user.
> 
> >         if (!ws)
> >                 return NULL;
> >
> > -       wakeup_source_prepare(ws, name ? kstrdup_const(name, GFP_KERNEL) : NULL);
> > +       wakeup_source_prepare(ws, name ? kstrdup_const(name, GFP_ATOMIC) : NULL);
> >         return ws;

NAK. This will silently replace name with NULL if memory is low.

									Pavel
-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 181 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] drivers: base: power: Fix GFP_KERNEL in spinlock context
  2017-12-12 14:58 ` Geert Uytterhoeven
  2017-12-15  8:45   ` Pavel Machek
@ 2017-12-16  8:14   ` shrikant
  1 sibling, 0 replies; 7+ messages in thread
From: shrikant @ 2017-12-16  8:14 UTC (permalink / raw)
  To: Geert Uytterhoeven
  Cc: Rafael J. Wysocki, Pavel Machek, Len Brown, Greg KH,
	Linux PM list, linux-kernel, Suniel Mahesh, Karthik Tummala,
	raghu


On Tuesday 12 December 2017 08:28 PM, Geert Uytterhoeven wrote:
> Hi Shrikant,
> 
> On Tue, Dec 12, 2017 at 2:45 PM,  <shrikant.maurya@techveda.org> wrote:
>> From: Shrikant Maurya <shrikant.maurya@techveda.org>
>>
>> As reported by Jia-Ju Bai (https://lkml.org/lkml/2017/12/11/872):
>> API's are using GFP_KERNEL to allocate memory which may sleep.
>>
>> To ensure atomicity such allocations must be avoided in critical
>> sections under spinlock.
>> Fixed by replacing GFP_KERNEL to GFP_ATOMIC.
>>
>> Reported-by: Jia-Ju Bai <baijiaju1990@gmail.com>
>> Signed-off-by: Shrikant Maurya <shrikant.maurya@techveda.org>
>> Signed-off-by: Suniel Mahesh <sunil.m@techveda.org>
>> Signed-off-by: Raghu Bharadwaj <raghu@techveda.org>
>> Signed-off-by: Karthik Tummala <karthik@techveda.org>
> 
> Can't the call to device_init_wakeup() in isp116x_start() just be moved
> below the spinlock release?

Can't move it below the spinlock.
Value going to be written into HcRhStatus register depends on it:
isp116x_write_reg32(isp116x, HCRHSTATUS, val);

Instead we can move it before the spinlock.

> 
>> --- a/drivers/base/power/wakeup.c
>> +++ b/drivers/base/power/wakeup.c
>> @@ -92,11 +92,11 @@ struct wakeup_source *wakeup_source_create(const char *name)
>>  {
>>         struct wakeup_source *ws;
>>
>> -       ws = kmalloc(sizeof(*ws), GFP_KERNEL);
>> +       ws = kmalloc(sizeof(*ws), GFP_ATOMIC);
> 
> With GFP_ATOMIC, allocation failure is much more likely to occur.
> So IMHO it's better to fix the isp116x, than to impose this burden on
> every user.

Absolutely. Thanks for pointing it out, it's not the right solution.

> 
>>         if (!ws)
>>                 return NULL;
>>
>> -       wakeup_source_prepare(ws, name ? kstrdup_const(name, GFP_KERNEL) : NULL);
>> +       wakeup_source_prepare(ws, name ? kstrdup_const(name, GFP_ATOMIC) : NULL);
>>         return ws;
>>  }
>>  EXPORT_SYMBOL_GPL(wakeup_source_create);
> 
> Gr{oetje,eeting}s,
> 
>                         Geert
> 
> --
> Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org
> 
> In personal conversations with technical people, I call myself a hacker. But
> when I'm talking to journalists I just say "programmer" or something like that.
>                                 -- Linus Torvalds
> 

Thank you Geert.
-- 
Shrikant
techveda.org

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] drivers: base: power: Fix GFP_KERNEL in spinlock context
  2017-12-15  8:45   ` Pavel Machek
@ 2017-12-16  8:15     ` shrikant
  0 siblings, 0 replies; 7+ messages in thread
From: shrikant @ 2017-12-16  8:15 UTC (permalink / raw)
  To: Pavel Machek, Geert Uytterhoeven
  Cc: Rafael J. Wysocki, Len Brown, Greg KH, Linux PM list,
	linux-kernel, Suniel Mahesh, Karthik Tummala, raghu



On Friday 15 December 2017 02:15 PM, Pavel Machek wrote:
> On Tue 2017-12-12 15:58:00, Geert Uytterhoeven wrote:
>> Hi Shrikant,
>>
>> On Tue, Dec 12, 2017 at 2:45 PM,  <shrikant.maurya@techveda.org> wrote:
>>> From: Shrikant Maurya <shrikant.maurya@techveda.org>
>>>
>>> As reported by Jia-Ju Bai (https://lkml.org/lkml/2017/12/11/872):
>>> API's are using GFP_KERNEL to allocate memory which may sleep.
>>>
>>> To ensure atomicity such allocations must be avoided in critical
>>> sections under spinlock.
>>> Fixed by replacing GFP_KERNEL to GFP_ATOMIC.
>>>
>>> Reported-by: Jia-Ju Bai <baijiaju1990@gmail.com>
>>> Signed-off-by: Shrikant Maurya <shrikant.maurya@techveda.org>
>>> Signed-off-by: Suniel Mahesh <sunil.m@techveda.org>
>>> Signed-off-by: Raghu Bharadwaj <raghu@techveda.org>
>>> Signed-off-by: Karthik Tummala <karthik@techveda.org>
>>
>> Can't the call to device_init_wakeup() in isp116x_start() just be moved
>> below the spinlock release?
>>
>>> --- a/drivers/base/power/wakeup.c
>>> +++ b/drivers/base/power/wakeup.c
>>> @@ -92,11 +92,11 @@ struct wakeup_source *wakeup_source_create(const char *name)
>>>  {
>>>         struct wakeup_source *ws;
>>>
>>> -       ws = kmalloc(sizeof(*ws), GFP_KERNEL);
>>> +       ws = kmalloc(sizeof(*ws), GFP_ATOMIC);
>>
>> With GFP_ATOMIC, allocation failure is much more likely to occur.
>> So IMHO it's better to fix the isp116x, than to impose this burden on
>> every user.
>>
>>>         if (!ws)
>>>                 return NULL;
>>>
>>> -       wakeup_source_prepare(ws, name ? kstrdup_const(name, GFP_KERNEL) : NULL);
>>> +       wakeup_source_prepare(ws, name ? kstrdup_const(name, GFP_ATOMIC) : NULL);
>>>         return ws;
> 
> NAK. This will silently replace name with NULL if memory is low.

Yes, replacing GFP_KERNEL with GFP_ATOMIC in both places will cause more 
issues than it fixes.

> 
> 									Pavel
> 

Thank you Pavel.

-- 
Shrikant
techveda.org

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH] drivers: base: power: Fix GFP_KERNEL in spinlock context
  2017-12-12 16:55 ` Rafael J. Wysocki
@ 2017-12-16  8:21   ` shrikant
  0 siblings, 0 replies; 7+ messages in thread
From: shrikant @ 2017-12-16  8:21 UTC (permalink / raw)
  To: Rafael J. Wysocki
  Cc: Rafael J. Wysocki, Pavel Machek, Len Brown, Greg Kroah-Hartman,
	Linux PM, Linux Kernel Mailing List, sunil.m, karthik, raghu



On Tuesday 12 December 2017 10:25 PM, Rafael J. Wysocki wrote:
> On Tue, Dec 12, 2017 at 2:45 PM,  <shrikant.maurya@techveda.org> wrote:
>> From: Shrikant Maurya <shrikant.maurya@techveda.org>
>>
>> As reported by Jia-Ju Bai (https://lkml.org/lkml/2017/12/11/872):
>> API's are using GFP_KERNEL to allocate memory which may sleep.
>>
>> To ensure atomicity such allocations must be avoided in critical
>> sections under spinlock.
> 
> That's right.
> 
> Which is why wakeup_source_create() should never be called under a spinlock.

Yes.

Better approach is, to move the call to device_init_wakeup() before the spinlock.

> 
> Are you aware of any place that happens in the mainline kernel?

No.

> 
> Thanks,
> Rafael
> 

Thank you Rafeal.
-- 
Shrikant
techveda.org

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-12-16  8:21 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-12-12 13:45 [PATCH] drivers: base: power: Fix GFP_KERNEL in spinlock context shrikant.maurya
2017-12-12 14:58 ` Geert Uytterhoeven
2017-12-15  8:45   ` Pavel Machek
2017-12-16  8:15     ` shrikant
2017-12-16  8:14   ` shrikant
2017-12-12 16:55 ` Rafael J. Wysocki
2017-12-16  8:21   ` shrikant

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).