public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* kmalloc without GFP_xxx?
@ 2005-06-29 11:02 Denis Vlasenko
  2005-06-29 11:15 ` Arjan van de Ven
  2005-06-29 11:15 ` Jens Axboe
  0 siblings, 2 replies; 27+ messages in thread
From: Denis Vlasenko @ 2005-06-29 11:02 UTC (permalink / raw)
  To: linux-kernel

Hi,

As anybody knows here, one needs to be careful with GFP_xxx
flags. I've no doubts it's important to get it right in fs
and elsewhere in critical places or else nasty things will happen.

However, for driver code it seems like questionaire
"do you remember which network callback is atomic?".

It struck me that kernel actually can figure out whether it's okay
to sleep or not by looking at combination of (flags & __GFP_WAIT)
and ((in_atomic() || irqs_disabled()) as it already does this for
might_sleep() barfing:

kmalloc => __cache_alloc =>

static inline void
cache_alloc_debugcheck_before(kmem_cache_t *cachep, unsigned int __nocast flags)
{
        might_sleep_if(flags & __GFP_WAIT);
#if DEBUG
        kmem_flagcheck(cachep, flags);
#endif
}

	and

void __might_sleep(char *file, int line)
{
#if defined(in_atomic)
        static unsigned long prev_jiffy;        /* ratelimiting */

        if ((in_atomic() || irqs_disabled()) &&
            system_state == SYSTEM_RUNNING && !oops_in_progress) {
                if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
                        return;
                prev_jiffy = jiffies;
                printk(KERN_ERR "Debug: sleeping function called from invalid"
                                " context at %s:%d\n", file, line);
                printk("in_atomic():%d, irqs_disabled():%d\n",
                        in_atomic(), irqs_disabled());
                dump_stack();
        }
#endif
}

So why can't we have kmalloc_auto(size) which does GFP_KERNEL alloc
if called from non-atomic context and GFP_ATOMIC one otherwise?
--
vda


^ permalink raw reply	[flat|nested] 27+ messages in thread
* Re: kmalloc without GFP_xxx?
@ 2005-06-29 20:28 Manfred Spraul
  0 siblings, 0 replies; 27+ messages in thread
From: Manfred Spraul @ 2005-06-29 20:28 UTC (permalink / raw)
  To: Denis Vlasenko; +Cc: linux-kernel

Denis wrote:

>It struck me that kernel actually can figure out whether it's okay
>to sleep or not by looking at combination of (flags & __GFP_WAIT)
>and ((in_atomic() || irqs_disabled()) as it already does this for
>might_sleep() barfing:
>
Wrong:
- the kernel cannot figure out if a thread owns a normal spinlock(): 
in_atomic detects spin_lock_bh(), irqs_disabled spin_lock_irq(). But 
spin_lock has no global state.
- dito for get_cpu()/put_cpu users.
- dito for rcu users, or anyone else that uses preempt_disable() for 
whatever purpose

--
    Manfred


^ permalink raw reply	[flat|nested] 27+ messages in thread
* Re: kmalloc without GFP_xxx?
@ 2005-06-29 20:44 Manfred Spraul
  2005-06-30  5:57 ` Steven Rostedt
  0 siblings, 1 reply; 27+ messages in thread
From: Manfred Spraul @ 2005-06-29 20:44 UTC (permalink / raw)
  To: Steven Rostedt; +Cc: Linux Kernel Mailing List

Hi,

One question from Linux-Tag was about the lack of documentation about/in 
the kernel. I try to maintain docbook entries when I modify code, even 
though I think it's mostly wasted time: Virtually noone reads it anyway, 
instead armchair logic on lkml.

Steven wrote:

>Here we see that task 2 can spin with interrupts off, while the first task
>is servicing an interrupt, and God forbid if the IRQ handler sends some
>kind of SMP signal to the CPU running task 2 since that would be a
>deadlock.  Granted, this is a hypothetical situation, but makes using
>spin_lock with interrupts enabled a little scary.
>  
>
Not, it's not even a hypothetical situation. It's an explicitely 
forbidden situation: SMP signals are sent with smp_call_function and the 
documentation to that function clearly says:
 *
 * You must not call this function with disabled interrupts or from a
 * hardware interrupt handler or from a bottom half handler.
 */

--
    Manfred

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

end of thread, other threads:[~2005-06-30  8:06 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-06-29 11:02 kmalloc without GFP_xxx? Denis Vlasenko
2005-06-29 11:15 ` Arjan van de Ven
2005-06-29 11:20   ` Denis Vlasenko
2005-06-29 11:37     ` Arjan van de Ven
2005-06-29 13:44       ` Steven Rostedt
2005-06-29 14:14         ` Denis Vlasenko
2005-06-29 14:23           ` Jörn Engel
2005-06-29 14:53             ` Steven Rostedt
2005-06-29 15:10               ` Jörn Engel
2005-06-29 15:48                 ` Steven Rostedt
2005-06-29 15:54                   ` Jörn Engel
2005-06-29 16:04                     ` Steven Rostedt
2005-06-29 15:12           ` Oliver Neukum
2005-06-29 16:48           ` Timur Tabi
2005-06-29 17:22             ` Steven Rostedt
2005-06-29 17:43               ` Richard B. Johnson
2005-06-29 18:07                 ` Steven Rostedt
2005-06-30  7:52             ` Denis Vlasenko
2005-06-30  8:05               ` Steven Rostedt
2005-06-30  1:02         ` Benjamin Herrenschmidt
2005-06-30  6:02           ` Steven Rostedt
2005-06-29 11:15 ` Jens Axboe
2005-06-29 11:18   ` Denis Vlasenko
2005-06-29 11:25     ` Jens Axboe
  -- strict thread matches above, loose matches on Subject: below --
2005-06-29 20:28 Manfred Spraul
2005-06-29 20:44 Manfred Spraul
2005-06-30  5:57 ` Steven Rostedt

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