Should we acqire the memory in ISR? No I guess. - Linux

This is a discussion on Should we acqire the memory in ISR? No I guess. - Linux ; Greetings, Recently I came across a tech. article which describes some intricacies of memory. There was a guideline which read as, " Even though, you are sure that there is memory available, you should not make system call from an ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Should we acqire the memory in ISR? No I guess.

  1. Should we acqire the memory in ISR? No I guess.

    Greetings,

    Recently I came across a tech. article which describes some intricacies
    of memory. There was a guideline which read as,

    " Even though, you are sure that there is memory available, you should
    not make system call from an ISR to aquire the memory."

    The explaination read as,
    "...Because when the interrupt was occured and in the ISR you would not
    be sure enough that in which state (user mode or kernel mode) the
    kernel is while you are in ISR."

    Now my counter argument is, we know that if we are not sure whether
    there is available memory or not, we must not make a call to acquire
    the memory in ISR because it can block indefinately. But in
    hypothetical situation, if we are sure that there is memeory then we
    can make system call to get it. Now regarding the state of kernel, when
    we make system call, it has to go to kernel mode, so be it in user mode
    ,when ISR was run, or kernel mode we dont have to worry.

    Does it make sense. Moreover, I think that, the interrupt context is
    special context. so Does it has some roots to this constraint of not
    acquring the memory in ISR?


    Please reply. Thanks in anticipation.


    Mozis.


  2. Re: Should we acqire the memory in ISR? No I guess.

    Mozis wrote:
    > Greetings,
    >
    > Recently I came across a tech. article which describes some intricacies
    > of memory. There was a guideline which read as,
    >
    > " Even though, you are sure that there is memory available, you should
    > not make system call from an ISR to aquire the memory."
    >
    > The explaination read as,
    > "...Because when the interrupt was occured and in the ISR you would not
    > be sure enough that in which state (user mode or kernel mode) the
    > kernel is while you are in ISR."
    >
    > Now my counter argument is, we know that if we are not sure whether
    > there is available memory or not, we must not make a call to acquire
    > the memory in ISR because it can block indefinately. But in
    > hypothetical situation, if we are sure that there is memeory then we
    > can make system call to get it. Now regarding the state of kernel, when
    > we make system call, it has to go to kernel mode, so be it in user mode
    > ,when ISR was run, or kernel mode we dont have to worry.
    >
    > Does it make sense. Moreover, I think that, the interrupt context is
    > special context. so Does it has some roots to this constraint of not
    > acquring the memory in ISR?


    For one thing: in general, you have to take special precautions to make
    sure that an assumption you make in one part of the code ("there are 4GB
    of free memory") still holds in another part ("Can I have 2 bytes?"),
    even in the next line or in the next instruction. You usually do this be
    masking interrupts or protection by semaphores/spinlocks. So: ascerting
    that enough memory is available in one part of the code will not
    guarantee that you will be able to get any in another part.

    However, If you cannot set up a pool dedicated for the ISR, you can
    always _try_ to get memory in the ISR and fail gracefully if you can't.

    Of course, you can always relegate this stuff to a back handler, but
    that's not the subject.
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett


  3. Re: Should we acqire the memory in ISR? No I guess.

    Mozis wrote:

    > Now regarding the state of kernel, when
    > we make system call, it has to go to kernel mode, so be it in user mode
    > ,when ISR was run, or kernel mode we dont have to worry.
    >
    > Does it make sense. Moreover, I think that, the interrupt context is
    > special context. so Does it has some roots to this constraint of not
    > acquring the memory in ISR?
    >


    Hi Mozis,
    make sure you're familiar with GFP_KERNEL and GFP_ATOMIC
    There is a certain amount of memory reserved for ISRs etc.
    If you acquire it using GFP_ATOMIC, the kernel guarantees that it doesn't
    suspend your ISR.
    Acquiring memory with kmalloc(size,GFP_KERNEL), it would possibly suspend
    your ISR until enough memory is available (via page fault).
    Since your ISR probably blocks most of the other processes and even other
    ISRs, it could easily happen that your system locks up.

    Therefore it's a very good idea to never acquire memory via an ISR.
    There shouldn't be a need for it in a carefully designed driver.
    There are different approaches to overcome this issue:
    usually your driver is controlled by a user process (or the scheduler
    workqueue) and you can safely acquire memory.
    If not, you might want to install a scheduled process which pends on a flag,
    and you can delegate memory allocation to such a safe process, just by
    talking to it from the ISR and set/clear this flag.

    Bernhard


  4. Re: Should we acqire the memory in ISR? No I guess.

    In article <1159485934.436298.110280@m7g2000cwm.googlegroups.c om>,
    Mozis wrote:

    >The explaination read as,
    >"...Because when the interrupt was occured and in the ISR you would not
    >be sure enough that in which state (user mode or kernel mode) the
    >kernel is while you are in ISR."


    You can be sure it is in neither mode; you are in ISR context.

    >Does it make sense. Moreover, I think that, the interrupt
    >context is special context. so Does it has some roots to this
    >constraint of not acquring the memory in ISR?


    You *can* get memory in an ISR. In fact there are special flags
    on the kmalloc call for just. But don't ask for a lot and
    always be prepared for the call to fail.

    --
    http://www.spinics.net/lists/kernel/

+ Reply to Thread