gnase
gnase

Reputation: 638

can allocation by kmalloc( ,GFP_KERNEL) be failed?

i am a newbie in Linux kernel field and i have these (silly) questions, please help me to understand this correctly

  1. As i read on the book Linux device driver, it said (with GFP_KERNEL) that

While the current process sleeps, the kernel takes proper action to locate some free memory, either by flushing buffers to disk or by swapping out memory from a user process.

So (maybe) in case kernel after doing all of above things but still not have enough memory to satisfy allocation, then the current process must still wait for it ? Is there any case that the current process must wait forever for enough memory for allocation (i mean with other devices also, not only in computer) ?

  1. As i read, GFP_ATOMIC is used "outside a process's context". So i guess the meaning of that group of words is if i have a normal process A running

    process A 
    {
    command 1; 
    command 2;
    .....;
    }
    

then i have an interrupt handler B using GFP_ATOMIC

    interrupt handler B
    {
    command 1;
    command 2;
    ......;
    kmalloc ( , GFP_ATOMIC);
    ......;
    }

so my current process A still runs without sleeping until it finishes and whether allocation in interrupt handler B successes or not, interrupt handler B does not affect my process A. Is that true ?

thank you very much for your help !

Upvotes: 1

Views: 2984

Answers (1)

Aymen Zayet
Aymen Zayet

Reputation: 86

kmalloc(, GFP_KERNEL) cannot wait forever unless you add the optional flag __GFP_NOFAIL. Note that this one is deprecated on the recent kernel.

GFP_ATOMIC is used only within an atomic context : which means the preemption is disabled, or interrupts are disabled or within an interrupt context (ISR). An easy rule to follow : as long as you are authorized to sleep, better to use GFP_KERNEL, otherwise, you have to use GFP_ATOMIC.

Hope that helps. Aymen.

Upvotes: 2

Related Questions