Skip to content

Commit 8334231

Browse files
Nick PigginLinus Torvalds
authored andcommitted
[PATCH] mm: introduce remap_vmalloc_range()
Add remap_vmalloc_range, vmalloc_user, and vmalloc_32_user so that drivers can have a nice interface for remapping vmalloc memory. Signed-off-by: Nick Piggin <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 762834e commit 8334231

File tree

2 files changed

+128
-2
lines changed

2 files changed

+128
-2
lines changed

include/linux/vmalloc.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
#include <linux/spinlock.h>
55
#include <asm/page.h> /* pgprot_t */
66

7+
struct vm_area_struct;
8+
79
/* bits in vm_struct->flags */
810
#define VM_IOREMAP 0x00000001 /* ioremap() and friends */
911
#define VM_ALLOC 0x00000002 /* vmalloc() */
1012
#define VM_MAP 0x00000004 /* vmap()ed pages */
13+
#define VM_USERMAP 0x00000008 /* suitable for remap_vmalloc_range */
1114
/* bits [20..32] reserved for arch specific ioremap internals */
1215

1316
/*
@@ -32,9 +35,11 @@ struct vm_struct {
3235
* Highlevel APIs for driver use
3336
*/
3437
extern void *vmalloc(unsigned long size);
38+
extern void *vmalloc_user(unsigned long size);
3539
extern void *vmalloc_node(unsigned long size, int node);
3640
extern void *vmalloc_exec(unsigned long size);
3741
extern void *vmalloc_32(unsigned long size);
42+
extern void *vmalloc_32_user(unsigned long size);
3843
extern void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot);
3944
extern void *__vmalloc_area(struct vm_struct *area, gfp_t gfp_mask,
4045
pgprot_t prot);
@@ -45,6 +50,9 @@ extern void vfree(void *addr);
4550
extern void *vmap(struct page **pages, unsigned int count,
4651
unsigned long flags, pgprot_t prot);
4752
extern void vunmap(void *addr);
53+
54+
extern int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
55+
unsigned long pgoff);
4856

4957
/*
5058
* Lowlevel-APIs (not for driver use!)

mm/vmalloc.c

Lines changed: 120 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,19 @@ struct vm_struct *get_vm_area_node(unsigned long size, unsigned long flags, int
256256
return __get_vm_area_node(size, flags, VMALLOC_START, VMALLOC_END, node);
257257
}
258258

259+
/* Caller must hold vmlist_lock */
260+
static struct vm_struct *__find_vm_area(void *addr)
261+
{
262+
struct vm_struct *tmp;
263+
264+
for (tmp = vmlist; tmp != NULL; tmp = tmp->next) {
265+
if (tmp->addr == addr)
266+
break;
267+
}
268+
269+
return tmp;
270+
}
271+
259272
/* Caller must hold vmlist_lock */
260273
struct vm_struct *__remove_vm_area(void *addr)
261274
{
@@ -498,10 +511,32 @@ EXPORT_SYMBOL(__vmalloc);
498511
*/
499512
void *vmalloc(unsigned long size)
500513
{
501-
return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
514+
return __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL);
502515
}
503516
EXPORT_SYMBOL(vmalloc);
504517

518+
/**
519+
* vmalloc_user - allocate virtually contiguous memory which has
520+
* been zeroed so it can be mapped to userspace without
521+
* leaking data.
522+
*
523+
* @size: allocation size
524+
*/
525+
void *vmalloc_user(unsigned long size)
526+
{
527+
struct vm_struct *area;
528+
void *ret;
529+
530+
ret = __vmalloc(size, GFP_KERNEL | __GFP_HIGHMEM | __GFP_ZERO, PAGE_KERNEL);
531+
write_lock(&vmlist_lock);
532+
area = __find_vm_area(ret);
533+
area->flags |= VM_USERMAP;
534+
write_unlock(&vmlist_lock);
535+
536+
return ret;
537+
}
538+
EXPORT_SYMBOL(vmalloc_user);
539+
505540
/**
506541
* vmalloc_node - allocate memory on a specific node
507542
*
@@ -516,7 +551,7 @@ EXPORT_SYMBOL(vmalloc);
516551
*/
517552
void *vmalloc_node(unsigned long size, int node)
518553
{
519-
return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL, node);
554+
return __vmalloc_node(size, GFP_KERNEL | __GFP_HIGHMEM, PAGE_KERNEL, node);
520555
}
521556
EXPORT_SYMBOL(vmalloc_node);
522557

@@ -556,6 +591,28 @@ void *vmalloc_32(unsigned long size)
556591
}
557592
EXPORT_SYMBOL(vmalloc_32);
558593

594+
/**
595+
* vmalloc_32_user - allocate virtually contiguous memory (32bit
596+
* addressable) which is zeroed so it can be
597+
* mapped to userspace without leaking data.
598+
*
599+
* @size: allocation size
600+
*/
601+
void *vmalloc_32_user(unsigned long size)
602+
{
603+
struct vm_struct *area;
604+
void *ret;
605+
606+
ret = __vmalloc(size, GFP_KERNEL | __GFP_ZERO, PAGE_KERNEL);
607+
write_lock(&vmlist_lock);
608+
area = __find_vm_area(ret);
609+
area->flags |= VM_USERMAP;
610+
write_unlock(&vmlist_lock);
611+
612+
return ret;
613+
}
614+
EXPORT_SYMBOL(vmalloc_32_user);
615+
559616
long vread(char *buf, char *addr, unsigned long count)
560617
{
561618
struct vm_struct *tmp;
@@ -630,3 +687,64 @@ long vwrite(char *buf, char *addr, unsigned long count)
630687
read_unlock(&vmlist_lock);
631688
return buf - buf_start;
632689
}
690+
691+
/**
692+
* remap_vmalloc_range - map vmalloc pages to userspace
693+
*
694+
* @vma: vma to cover (map full range of vma)
695+
* @addr: vmalloc memory
696+
* @pgoff: number of pages into addr before first page to map
697+
* @returns: 0 for success, -Exxx on failure
698+
*
699+
* This function checks that addr is a valid vmalloc'ed area, and
700+
* that it is big enough to cover the vma. Will return failure if
701+
* that criteria isn't met.
702+
*
703+
* Similar to remap_pfn_range (see mm/memory.c)
704+
*/
705+
int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
706+
unsigned long pgoff)
707+
{
708+
struct vm_struct *area;
709+
unsigned long uaddr = vma->vm_start;
710+
unsigned long usize = vma->vm_end - vma->vm_start;
711+
int ret;
712+
713+
if ((PAGE_SIZE-1) & (unsigned long)addr)
714+
return -EINVAL;
715+
716+
read_lock(&vmlist_lock);
717+
area = __find_vm_area(addr);
718+
if (!area)
719+
goto out_einval_locked;
720+
721+
if (!(area->flags & VM_USERMAP))
722+
goto out_einval_locked;
723+
724+
if (usize + (pgoff << PAGE_SHIFT) > area->size - PAGE_SIZE)
725+
goto out_einval_locked;
726+
read_unlock(&vmlist_lock);
727+
728+
addr += pgoff << PAGE_SHIFT;
729+
do {
730+
struct page *page = vmalloc_to_page(addr);
731+
ret = vm_insert_page(vma, uaddr, page);
732+
if (ret)
733+
return ret;
734+
735+
uaddr += PAGE_SIZE;
736+
addr += PAGE_SIZE;
737+
usize -= PAGE_SIZE;
738+
} while (usize > 0);
739+
740+
/* Prevent "things" like memory migration? VM_flags need a cleanup... */
741+
vma->vm_flags |= VM_RESERVED;
742+
743+
return ret;
744+
745+
out_einval_locked:
746+
read_unlock(&vmlist_lock);
747+
return -EINVAL;
748+
}
749+
EXPORT_SYMBOL(remap_vmalloc_range);
750+

0 commit comments

Comments
 (0)