mm: kvmalloc: Bail if called with soft or hard IRQs disabled

The function kvmalloc may call vmalloc under the hood even when
used with GFP_ATOMIC.  However, vmalloc uses naked spin locks so
it must not be called if the caller already holds spin locks that
can be used in hard or soft IRQ context.

Use the heuristic irqs_disabled() || in_softirq() to detect such
callers and prevent them from using vmalloc.

Reported-by: Liam R. Howlett (Google) <liam@infradead.org>
Fixes: c6307674ed82 ("mm: kvmalloc: add non-blocking support for vmalloc")
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
diff --git a/mm/slub.c b/mm/slub.c
index 5c33d12..f6720b9 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -7146,6 +7146,14 @@ void *__kvmalloc_node_noprof(DECL_KMALLOC_PARAMS(size, b, token), unsigned long
 	if (ret || size <= PAGE_SIZE)
 		return ret;
 
+	/*
+	 * vmalloc uses naked spin locks, so it must not be used if
+	 * the caller already holds a spin lock that can be used in
+	 * a soft or hard IRQ.  Use heuristics to detect this.
+	 */
+	if (irqs_disabled() || in_interrupt())
+		return NULL;
+
 	/* Don't even allow crazy sizes */
 	if (unlikely(size > INT_MAX)) {
 		WARN_ON_ONCE(!(flags & __GFP_NOWARN));