public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* GFP_NOWAIT and GFP_NOMEMALLOC
@ 2009-09-29 11:25 Leonidas .
  2009-09-29 11:59 ` Arjan van de Ven
  2009-10-04 18:56 ` Pavel Machek
  0 siblings, 2 replies; 6+ messages in thread
From: Leonidas . @ 2009-09-29 11:25 UTC (permalink / raw)
  To: linux-kernel

Hi list,

I am new here, I have googled/binged enough before posting this message,
in case of redundancy please point me to appropriate links/resources etc.

I want to kmalloc memory while holding spinlocks in process context, here I
can't use GFP_KERNEL flag since it can sleep. Using GFP_ATOMIC guarantees
that allocation will succeed by allocating from emergency pools if needed.
But I dont think, I need to use emergency pool and I want to limit my memory
consumption to ZONE_NORMAL without sleeping, my module is ready to handle
the allocation failure if any.


Something like,

ptr =  kmalloc(..., GFP_KERNEL | GFP_DONT_SLEEP );

if( !ptr ) {

 /*GFP_KERNEL failed, Use GFP_ATOMIC pool*/
 ptr =  kmalloc(..., GFP_ATOMIC );

}
/*Go ahead and do stuff*/

Is there any flag which fetches memory from GFP_KERNEL pool with a
guarantee that
it will not sleep?

Does GFP_NOWAIT | GFP_KERNEL guarantee that?

Is ( GFP_NOWAIT | GFP_KERNEL ) == ( GFP_ATOMIC - dont access emergency pools)?

I am a bit confused about GFP_NOWAIT and GFP_NOMEMALLOC, any pointers will be
helpful.

-Leo.

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

* Re: GFP_NOWAIT and GFP_NOMEMALLOC
  2009-09-29 11:25 GFP_NOWAIT and GFP_NOMEMALLOC Leonidas .
@ 2009-09-29 11:59 ` Arjan van de Ven
  2009-09-29 12:16   ` Leonidas .
  2009-10-04 18:56 ` Pavel Machek
  1 sibling, 1 reply; 6+ messages in thread
From: Arjan van de Ven @ 2009-09-29 11:59 UTC (permalink / raw)
  To: Leonidas .; +Cc: linux-kernel

On Tue, 29 Sep 2009 16:55:24 +0530
"Leonidas ." <leonidas137@gmail.com> wrote:

> Hi list,
> 
> I am new here, I have googled/binged enough before posting this
> message, in case of redundancy please point me to appropriate
> links/resources etc.
> 
> I want to kmalloc memory while holding spinlocks in process context,
> here I can't use GFP_KERNEL flag since it can sleep. Using GFP_ATOMIC
> guarantees that allocation will succeed by allocating from emergency
> pools if needed. But I dont think, I need to use emergency pool and I
> want to limit my memory consumption to ZONE_NORMAL without sleeping,
> my module is ready to handle the allocation failure if any.

GFP_NOWAIT is what you want.

can you give us a pointer to what your module is about? Maybe there's
better solutions...


-- 
Arjan van de Ven 	Intel Open Source Technology Centre
For development, discussion and tips for power savings, 
visit http://www.lesswatts.org

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

* Re: GFP_NOWAIT and GFP_NOMEMALLOC
  2009-09-29 11:59 ` Arjan van de Ven
@ 2009-09-29 12:16   ` Leonidas .
  2009-09-29 12:37     ` Arjan van de Ven
  0 siblings, 1 reply; 6+ messages in thread
From: Leonidas . @ 2009-09-29 12:16 UTC (permalink / raw)
  To: Arjan van de Ven; +Cc: linux-kernel

On Tue, Sep 29, 2009 at 5:29 PM, Arjan van de Ven <arjan@infradead.org> wrote:
> On Tue, 29 Sep 2009 16:55:24 +0530
> "Leonidas ." <leonidas137@gmail.com> wrote:
>
>> Hi list,
>>
>> I am new here, I have googled/binged enough before posting this
>> message, in case of redundancy please point me to appropriate
>> links/resources etc.
>>
>> I want to kmalloc memory while holding spinlocks in process context,
>> here I can't use GFP_KERNEL flag since it can sleep. Using GFP_ATOMIC
>> guarantees that allocation will succeed by allocating from emergency
>> pools if needed. But I dont think, I need to use emergency pool and I
>> want to limit my memory consumption to ZONE_NORMAL without sleeping,
>> my module is ready to handle the allocation failure if any.
>
> GFP_NOWAIT is what you want.
>
> can you give us a pointer to what your module is about? Maybe there's
> better solutions...
>
>
> --
> Arjan van de Ven        Intel Open Source Technology Centre
> For development, discussion and tips for power savings,
> visit http://www.lesswatts.org
>


The module is going to be a profiling module and the user module which
is under profiling
will call the apis which I am exporting. My apis will collect certain
data from point
it is called and populate internal data strcuctures. My apis can be
called from process
context as well as interrupt context hence I am using spinlocks as
locking mechanism.
The amount of memory I am allocating each time my api is called around
100 bytes,
and in worst case I might end up allocating tens of thousands of
objects of almost same
size. I am aware that in such a scenario I should be using some kind
of memory pool
mechanism and preallocate my buffers to avoid frequent
allocations/deallocations.

But frankly, I am not very familiar with Linux kernel apis yet hence
have resorted to
using a tree data structure for the first cut where I allocate memory as needed
depending on the context I am executing in. To optimize a bit, I do
not free my memory
till some threshold is reached.

Some questions here:

1. GFP_NOWAIT as in kmalloc(size, GFP_KERNEL | GFPNOWAIT)?

or

kmalloc(size, GFP_NOWAIT), I know this one is a dumb one.

2. Any reference for the mempool kind of implementation mentioned above?

3. Any other pointers about design, references etc would be helpful.

-Leo.

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

* Re: GFP_NOWAIT and GFP_NOMEMALLOC
  2009-09-29 12:16   ` Leonidas .
@ 2009-09-29 12:37     ` Arjan van de Ven
  2009-09-29 12:43       ` Leonidas .
  0 siblings, 1 reply; 6+ messages in thread
From: Arjan van de Ven @ 2009-09-29 12:37 UTC (permalink / raw)
  To: Leonidas .; +Cc: linux-kernel

On Tue, 29 Sep 2009 17:46:35 +0530
"Leonidas ." <leonidas137@gmail.com> wrote:
> 
> The module is going to be a profiling module and the user module which
> is under profiling
> will call the apis which I am exporting. My apis will collect certain
> data from point

(I assume you are aware of 'perf' and such)


> But frankly, I am not very familiar with Linux kernel apis yet hence
> have resorted to
> using a tree data structure for the first cut where I allocate memory
> as needed depending on the context I am executing in. To optimize a
> bit, I do not free my memory
> till some threshold is reached.
> 
> Some questions here:
> 
> 1. GFP_NOWAIT as in kmalloc(size, GFP_KERNEL | GFPNOWAIT)?
> 
> or
> 
> kmalloc(size, GFP_NOWAIT), I know this one is a dumb one.

don't do GFP_KERNEL |

because that makes it basically equivalent to GFP_KERNEL, which is a
sleeping version.

> 
> 2. Any reference for the mempool kind of implementation mentioned
> above?

there's mm/mempool.c..

as a general suggestion I would say "look at perf, and see if you can
build on that, or borrow from it"...


-- 
Arjan van de Ven 	Intel Open Source Technology Centre
For development, discussion and tips for power savings, 
visit http://www.lesswatts.org

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

* Re: GFP_NOWAIT and GFP_NOMEMALLOC
  2009-09-29 12:37     ` Arjan van de Ven
@ 2009-09-29 12:43       ` Leonidas .
  0 siblings, 0 replies; 6+ messages in thread
From: Leonidas . @ 2009-09-29 12:43 UTC (permalink / raw)
  To: Arjan van de Ven; +Cc: linux-kernel

On Tue, Sep 29, 2009 at 6:07 PM, Arjan van de Ven <arjan@infradead.org> wrote:
> On Tue, 29 Sep 2009 17:46:35 +0530
> "Leonidas ." <leonidas137@gmail.com> wrote:
>>
>> The module is going to be a profiling module and the user module which
>> is under profiling
>> will call the apis which I am exporting. My apis will collect certain
>> data from point
>
> (I assume you are aware of 'perf' and such)
>
>
>> But frankly, I am not very familiar with Linux kernel apis yet hence
>> have resorted to
>> using a tree data structure for the first cut where I allocate memory
>> as needed depending on the context I am executing in. To optimize a
>> bit, I do not free my memory
>> till some threshold is reached.
>>
>> Some questions here:
>>
>> 1. GFP_NOWAIT as in kmalloc(size, GFP_KERNEL | GFPNOWAIT)?
>>
>> or
>>
>> kmalloc(size, GFP_NOWAIT), I know this one is a dumb one.
>
> don't do GFP_KERNEL |
>
> because that makes it basically equivalent to GFP_KERNEL, which is a
> sleeping version.
>
>>
>> 2. Any reference for the mempool kind of implementation mentioned
>> above?
>
> there's mm/mempool.c..
>
> as a general suggestion I would say "look at perf, and see if you can
> build on that, or borrow from it"...
>
>
> --
> Arjan van de Ven        Intel Open Source Technology Centre
> For development, discussion and tips for power savings,
> visit http://www.lesswatts.org
>

I will have a look at perf. Thanks for the inputs.

-Leo.

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

* Re: GFP_NOWAIT and GFP_NOMEMALLOC
  2009-09-29 11:25 GFP_NOWAIT and GFP_NOMEMALLOC Leonidas .
  2009-09-29 11:59 ` Arjan van de Ven
@ 2009-10-04 18:56 ` Pavel Machek
  1 sibling, 0 replies; 6+ messages in thread
From: Pavel Machek @ 2009-10-04 18:56 UTC (permalink / raw)
  To: Leonidas .; +Cc: linux-kernel

On Tue 2009-09-29 16:55:24, Leonidas . wrote:
> Hi list,
> 
> I am new here, I have googled/binged enough before posting this message,
> in case of redundancy please point me to appropriate links/resources etc.
> 
> I want to kmalloc memory while holding spinlocks in process context, here I
> can't use GFP_KERNEL flag since it can sleep. Using GFP_ATOMIC guarantees
> that allocation will succeed by allocating from emergency pools if needed.
> But I dont think, I need to use emergency pool and I want to limit my memory
> consumption to ZONE_NORMAL without sleeping, my module is ready to handle
> the allocation failure if any.

Umm. Either preallocate, or use gfp_atomic. It should be ok.

> Something like,
> 
> ptr =  kmalloc(..., GFP_KERNEL | GFP_DONT_SLEEP );
> 
> if( !ptr ) {
> 
>  /*GFP_KERNEL failed, Use GFP_ATOMIC pool*/
>  ptr =  kmalloc(..., GFP_ATOMIC );
> 
> }
> /*Go ahead and do stuff*/

That's pretty stupid, no? Just alloc GFP_ATOMIC directly... oh and
check return values...
-- 
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html

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

end of thread, other threads:[~2009-10-04 18:57 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-09-29 11:25 GFP_NOWAIT and GFP_NOMEMALLOC Leonidas .
2009-09-29 11:59 ` Arjan van de Ven
2009-09-29 12:16   ` Leonidas .
2009-09-29 12:37     ` Arjan van de Ven
2009-09-29 12:43       ` Leonidas .
2009-10-04 18:56 ` Pavel Machek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox