Skip to contentSkip to conversation

net: don't use in_atomic() in gfp_any()

PATCH Applied [email protected] 3 participants 6 messages Feb 11, 2009 – Feb 13, 2009

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 net: don't use in_atomic() in gfp_any()

From: Andrew Morton <[email protected]>

The problem is that in_atomic() will return false inside spinlocks if CONFIG_PREEMPT=n. This will lead to deadlockable GFP_KERNEL allocations from spinlocked regions.

Secondly, if CONFIG_PREEMPT=y, this bug solves itself because networking will instead use GFP_ATOMIC from this callsite. Hence we won't get the might_sleep() debugging warnings which would have informed us of the buggy callsites.

Solve both these problems by switching to in_interrupt(). Now, if someone runs a gfp_any() allocation from inside spinlock we will get the warning if CONFIG_PREEMPT=y.

I reviewed all callsites and most of them were too complex for my little brain and none of them documented their interface requirements. I have no idea what this patch will do.

Cc Andi Kleen <[email protected]>
Cc "David S. Miller" <[email protected]>

---

 include/net/sock.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/net/sock.h renamed from include/net/sock.h~net-dont-use-in_atomic-in-gfp_any +1 -1
diff -puN include/net/sock.h~net-dont-use-in_atomic-in-gfp_any include/net/sock.h
@@ -1313,7 +1313,7 @@ static inline int sock_writeable(const s
 
 static inline gfp_t gfp_any(void)
 {
-	return in_atomic() ? GFP_ATOMIC : GFP_KERNEL;
+	return in_softirq() ? GFP_ATOMIC : GFP_KERNEL;
 }
 
 static inline long sock_rcvtimeo(const struct sock *sk, int noblock)

_

#1 lore raw

include/net/sock.h static inline int sock_writeable(const s · line 1317

Quoted 9 lines · Andrew Morton

Solve both these problems by switching to in_interrupt(). Now, if someone runs a gfp_any() allocation from inside spinlock we will get the warning if CONFIG_PREEMPT=y.

static inline gfp_t gfp_any(void)
{

include/net/sock.h renamed from include/net/sock.h~net-dont-use-in_atomic-in-gfp_any +1 -1
-	return in_atomic() ? GFP_ATOMIC : GFP_KERNEL;
+	return in_softirq() ? GFP_ATOMIC : GFP_KERNEL;
 }

sed -i -e 's/in_softirq/in_interrupt/' ?

#2 lore raw
Quoted 11 lines · On Thu, 12 Feb 2009 06:38:55 +0900 Tetsuo Handa…

include/net/sock.h static inline int sock_writeable(const s · line 1317

Quoted 9 lines · Andrew Morton

Solve both these problems by switching to in_interrupt(). Now, if someone runs a gfp_any() allocation from inside spinlock we will get the warning if CONFIG_PREEMPT=y.

static inline gfp_t gfp_any(void)
{

include/net/sock.h renamed from include/net/sock.h~net-dont-use-in_atomic-in-gfp_any +1 -1
-	return in_atomic() ? GFP_ATOMIC : GFP_KERNEL;
+	return in_softirq() ? GFP_ATOMIC : GFP_KERNEL;
 }

sed -i -e 's/in_softirq/in_interrupt/' ?

heh, good question.

I did all my testing with in_softirq() - the changelog didn't catch up.

Is gfp_any() supposed to be usable from hardirq context?

If so, we should use in_interrupt().

If not, we should use in_softirq(), and we'll then get might_sleep() warnings if anyone uses gfp_any() from hard irq context.

maintainer
#3 lore raw

From: Andrew Morton <[email protected]>
Date: Wed, 11 Feb 2009 13:48:42 -0800

Quoted 6 lines

Is gfp_any() supposed to be usable from hardirq context?

If so, we should use in_interrupt().

If not, we should use in_softirq(), and we'll then get might_sleep() warnings if anyone uses gfp_any() from hard irq context.

It never will be used in hardware IRQ context, at least in the networking.

And all uses in the tree are.. networking :-)

#4 lore raw
In reply to Tetsuo Handa

Resend with cc's restored. Please DO NOT edit the Cc: line :(

Quoted 11 lines · On Thu, 12 Feb 2009 06:38:55 +0900 Tetsuo Handa…

include/net/sock.h static inline int sock_writeable(const s · line 1317

Quoted 9 lines · Andrew Morton

Solve both these problems by switching to in_interrupt(). Now, if someone runs a gfp_any() allocation from inside spinlock we will get the warning if CONFIG_PREEMPT=y.

static inline gfp_t gfp_any(void)
{

include/net/sock.h renamed from include/net/sock.h~net-dont-use-in_atomic-in-gfp_any +1 -1
-	return in_atomic() ? GFP_ATOMIC : GFP_KERNEL;
+	return in_softirq() ? GFP_ATOMIC : GFP_KERNEL;
 }

sed -i -e 's/in_softirq/in_interrupt/' ?

heh, good question.

I did all my testing with in_softirq() - the changelog didn't catch up.

Is gfp_any() supposed to be usable from hardirq context?

If so, we should use in_interrupt().

If not, we should use in_softirq(), and we'll then get might_sleep() warnings if anyone uses gfp_any() from hard irq context.

maintainer Applied
#5 lore raw
In reply to [email protected]

From: [email protected]
Date: Wed, 11 Feb 2009 13:27:19 -0800

Quoted 13 lines

The problem is that in_atomic() will return false inside spinlocks if CONFIG_PREEMPT=n. This will lead to deadlockable GFP_KERNEL allocations from spinlocked regions.

Secondly, if CONFIG_PREEMPT=y, this bug solves itself because networking will instead use GFP_ATOMIC from this callsite. Hence we won't get the might_sleep() debugging warnings which would have informed us of the buggy callsites.

Solve both these problems by switching to in_interrupt(). Now, if someone runs a gfp_any() allocation from inside spinlock we will get the warning if CONFIG_PREEMPT=y.

Quoted 5 lines

I reviewed all callsites and most of them were too complex for my little brain and none of them documented their interface requirements. I have no idea what this patch will do.

Applied.