Skip to content

Commit

Permalink
mm/vmalloc.c: add flags to mark vm_map_ram area
Browse files Browse the repository at this point in the history
Through vmalloc API, a virtual kernel area is reserved for physical
address mapping. And vmap_area is used to track them, while vm_struct
is allocated to associate with the vmap_area to store more information
and passed out.

However, area reserved via vm_map_ram() is an exception. It doesn't have
vm_struct to associate with vmap_area. And we can't recognize the
vmap_area with '->vm == NULL' as a vm_map_ram() area because the normal
freeing path will set va->vm = NULL before unmapping, please see
function remove_vm_area().

Meanwhile, there are two types of vm_map_ram area. One is the whole
vmap_area being reserved and mapped at one time; the other is the
whole vmap_area with VMAP_BLOCK_SIZE size being reserved, while mapped
into split regions with smaller size several times via vb_alloc().

To mark the area reserved through vm_map_ram(), add flags field into
struct vmap_area. Bit 0 indicates whether it's a vm_map_ram area,
while bit 1 indicates whether it's a vmap_block type of vm_map_ram
area.

This is a preparatoin for later use.

Signed-off-by: Baoquan He <bhe@redhat.com>
  • Loading branch information
Baoquan He authored and intel-lab-lkp committed Dec 4, 2022
1 parent 4926d89 commit 52b2d46
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/linux/vmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ struct vmap_area {
unsigned long subtree_max_size; /* in "free" tree */
struct vm_struct *vm; /* in "busy" tree */
};
unsigned long flags; /* mark type of vm_map_ram area */
};

/* archs that select HAVE_ARCH_HUGE_VMAP should override one or more of these */
Expand Down
18 changes: 17 additions & 1 deletion mm/vmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1827,6 +1827,7 @@ static void free_vmap_area_noflush(struct vmap_area *va)

spin_lock(&vmap_area_lock);
unlink_va(va, &vmap_area_root);
va->flags = 0;
spin_unlock(&vmap_area_lock);

nr_lazy = atomic_long_add_return((va->va_end - va->va_start) >>
Expand Down Expand Up @@ -1901,6 +1902,10 @@ struct vmap_area *find_vmap_area(unsigned long addr)

#define VMAP_BLOCK_SIZE (VMAP_BBMAP_BITS * PAGE_SIZE)

#define VMAP_RAM 0x1
#define VMAP_BLOCK 0x2
#define VMAP_FLAGS_MASK 0x3

struct vmap_block_queue {
spinlock_t lock;
struct list_head free;
Expand Down Expand Up @@ -1981,6 +1986,9 @@ static void *new_vmap_block(unsigned int order, gfp_t gfp_mask)
kfree(vb);
return ERR_CAST(va);
}
spin_lock(&vmap_area_lock);
va->flags = VMAP_RAM|VMAP_BLOCK;
spin_unlock(&vmap_area_lock);

vaddr = vmap_block_vaddr(va->va_start, 0);
spin_lock_init(&vb->lock);
Expand Down Expand Up @@ -2243,8 +2251,12 @@ void vm_unmap_ram(const void *mem, unsigned int count)
return;
}

va = find_vmap_area(addr);
spin_lock(&vmap_area_lock);
va = __find_vmap_area((unsigned long)addr, &vmap_area_root);
BUG_ON(!va);
if (va)
va->flags &= ~VMAP_RAM;
spin_unlock(&vmap_area_lock);
debug_check_no_locks_freed((void *)va->va_start,
(va->va_end - va->va_start));
free_unmap_vmap_area(va);
Expand Down Expand Up @@ -2283,6 +2295,10 @@ void *vm_map_ram(struct page **pages, unsigned int count, int node)
if (IS_ERR(va))
return NULL;

spin_lock(&vmap_area_lock);
va->flags = VMAP_RAM;
spin_unlock(&vmap_area_lock);

addr = va->va_start;
mem = (void *)addr;
}
Expand Down

0 comments on commit 52b2d46

Please sign in to comment.