What's the reason for BUG_ON in __get_vm_area_node?

524 Views Asked by At

Why is this assertion in __get_vm_area_node?

static struct vm_struct *__get_vm_area_node(...)
{
    // ...
    BUG_ON(in_interrupt())
    // ...
} 
1

There are 1 best solutions below

0
Marco Bonelli On

I think the commit that introduced that line explains it pretty well:

If __vmalloc is called to allocate memory with GFP_ATOMIC in atomic context, the chain of calls results in __get_vm_area_node allocating memory for vm_struct with GFP_KERNEL, causing the 'sleeping from invalid context' warning. This patch fixes it by passing the gfp flags along so __get_vm_area_node allocates memory for vm_struct with the same flags.

@@ -160,13 +160,15 @@ int map_vm_area(struct vm_struct *area, pgprot_t prot, struct page ***pages)
    return err;
 }
 
-struct vm_struct *__get_vm_area_node(unsigned long size, unsigned long flags,
-               unsigned long start, unsigned long end, int node)
+static struct vm_struct *__get_vm_area_node(unsigned long size, unsigned long flags,
+                       unsigned long start, unsigned long end,
+                       int node, gfp_t gfp_mask)
 {
    struct vm_struct **p, *tmp, *area;
    unsigned long align = 1;
    unsigned long addr;
 
+   BUG_ON(in_interrupt());
    if (flags & VM_IOREMAP) {
        int bit = fls(size);

In short, the following code in that function could sleep, and sleeping is not allowed in interrupt context. The BUG_ON is added to prevent this kind of problem.