2004-11-21 12:23:22

by Peter T. Breuer

[permalink] [raw]
Subject: can kfree sleep?


Just a question: can kfree sleep?

I believe so, but slab.c does not enlighten me immediately:

void
kfree (const void *objp)
{
kmem_cache_t *c;
unsigned long flags;

if (!objp)
return;
local_irq_save (flags);
c = GET_PAGE_CACHE (virt_to_page (objp));
__cache_free (c, (void *) objp);
local_irq_restore (flags);
}

static inline void __cache_free (kmem_cache_t *cachep, void* objp)
{
struct array_cache *ac = ac_data(cachep);

check_irq_off();
objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));

if (likely(ac->avail < ac->limit)) {
STATS_INC_FREEHIT(cachep);
ac_entry(ac)[ac->avail++] = objp;
return;
} else {
STATS_INC_FREEMISS(cachep);
cache_flusharray(cachep, ac);
ac_entry(ac)[ac->avail++] = objp;
}
}

...


Peter


2004-11-21 18:56:55

by Manfred Spraul

[permalink] [raw]
Subject: Re: can kfree sleep?

Hi Peter,

>Just a question: can kfree sleep?
>
>
>
>
No, it never sleeps. It's safe to call kfree from arbitrary context. The
only exception is the NMI oopser and similar arch code.

>I believe so, but slab.c does not enlighten me immediately:
>
>
Yes, the kfree code is quite long - it must check if freeing one object
created a freeable page and return it to the page allocator. Together
with lots of caching and debug checks.

--
Manfred

2004-11-21 20:52:22

by ptb

[permalink] [raw]
Subject: Re: can kfree sleep?

In article <[email protected]> you wrote:
> Hi Peter,

Hi!

> >Just a question: can kfree sleep?
> >
> No, it never sleeps. It's safe to call kfree from arbitrary context. The
> only exception is the NMI oopser and similar arch code.

OK - thanks. I'll take that as read and eliminate kfree from the list
of sleep "seeds" in my code analyser.

> >I believe so, but slab.c does not enlighten me immediately:
> >
> Yes, the kfree code is quite long - it must check if freeing one object
> created a freeable page and return it to the page allocator. Together
> with lots of caching and debug checks.

May I ask where your knowledge of this comes from? (So I can duplicate
the learning process)!

Peter

2004-11-21 21:10:58

by Andrew Morton

[permalink] [raw]
Subject: Re: can kfree sleep?

"Peter T. Breuer" <[email protected]> wrote:
>
> Just a question: can kfree sleep?

Nope. All memory freeing codepaths are atomic and may be called from any
context except NMI handlers.

2004-11-21 21:17:25

by Christoph Hellwig

[permalink] [raw]
Subject: Re: can kfree sleep?

On Sun, Nov 21, 2004 at 01:10:38PM -0800, Andrew Morton wrote:
> Nope. All memory freeing codepaths are atomic and may be called from any
> context except NMI handlers.

Not true for vfree()

2004-11-21 22:31:30

by ptb

[permalink] [raw]
Subject: Re: can kfree sleep?

In article <[email protected]> you wrote:
> On Sun, Nov 21, 2004 at 01:10:38PM -0800, Andrew Morton wrote:
> > Nope. All memory freeing codepaths are atomic and may be called from any
> > context except NMI handlers.
>
> Not true for vfree()

My interest at the moment is in what can sleep and what cannot sleep.
Are you saying that vfree can sleep or that vfree cannot be called from
at least one other context in addition to the NMI handler context (from
which it cannot be called ...)?

:)

Peter

2004-11-22 05:41:20

by Christoph Hellwig

[permalink] [raw]
Subject: Re: can kfree sleep?

On Sun, Nov 21, 2004 at 11:30:38PM +0100, Peter T. Breuer wrote:
> In article <[email protected]> you wrote:
> > On Sun, Nov 21, 2004 at 01:10:38PM -0800, Andrew Morton wrote:
> > > Nope. All memory freeing codepaths are atomic and may be called from any
> > > context except NMI handlers.
> >
> > Not true for vfree()
>
> My interest at the moment is in what can sleep and what cannot sleep.
> Are you saying that vfree can sleep or that vfree cannot be called from
> at least one other context in addition to the NMI handler context (from
> which it cannot be called ...)?

vfree can't sleep, but it can't be called from every context.

2004-11-22 08:18:58

by Arjan van de Ven

[permalink] [raw]
Subject: Re: can kfree sleep?


> > Not true for vfree()
>
> My interest at the moment is in what can sleep and what cannot sleep.
> Are you saying that vfree can sleep or that vfree cannot be called from
> at least one other context in addition to the NMI handler context (from
> which it cannot be called ...)?


vfree() is not allowed to be called from irq context, and since it can do cross cpu IPI's, you have to generally be careful with it.