Skip to content

Commit 9a9d1d3

Browse files
committed
Merge branch 'mm-enforce-ioremap-address-space-and-introduce-sparse-vm_area'
Alexei Starovoitov says: ==================== mm: Enforce ioremap address space and introduce sparse vm_area From: Alexei Starovoitov <ast@kernel.org> v3 -> v4 - dropped VM_XEN patch for now. It will be in the follow up. - fixed constant as pointed out by Mike v2 -> v3 - added Christoph's reviewed-by to patch 1 - cap commit log lines to 75 chars - factored out common checks in patch 3 into helper - made vm_area_unmap_pages() return void There are various users of kernel virtual address space: vmalloc, vmap, ioremap, xen. - vmalloc use case dominates the usage. Such vm areas have VM_ALLOC flag and these areas are treated differently by KASAN. - the areas created by vmap() function should be tagged with VM_MAP (as majority of the users do). - ioremap areas are tagged with VM_IOREMAP and vm area start is aligned to size of the area unlike vmalloc/vmap. - there is also xen usage that is marked as VM_IOREMAP, but it doesn't call ioremap_page_range() unlike all other VM_IOREMAP users. To clean this up a bit, enforce that ioremap_page_range() checks the range and VM_IOREMAP flag. In addition BPF would like to reserve regions of kernel virtual address space and populate it lazily, similar to xen use cases. For that reason, introduce VM_SPARSE flag and vm_area_[un]map_pages() helpers to populate this sparse area. In the end the /proc/vmallocinfo will show "vmalloc" "vmap" "ioremap" "sparse" categories for different kinds of address regions. ioremap, sparse will return zero when dumped through /proc/kcore ==================== Link: https://lore.kernel.org/r/20240305030516.41519-1-alexei.starovoitov@gmail.com Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
2 parents 8f50d5c + e6f7982 commit 9a9d1d3

2 files changed

Lines changed: 75 additions & 2 deletions

File tree

include/linux/vmalloc.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct iov_iter; /* in uio.h */
3535
#else
3636
#define VM_DEFER_KMEMLEAK 0
3737
#endif
38+
#define VM_SPARSE 0x00001000 /* sparse vm_area. not all pages are present. */
3839

3940
/* bits [20..32] reserved for arch specific ioremap internals */
4041

@@ -232,6 +233,10 @@ static inline bool is_vm_area_hugepages(const void *addr)
232233
}
233234

234235
#ifdef CONFIG_MMU
236+
int vm_area_map_pages(struct vm_struct *area, unsigned long start,
237+
unsigned long end, struct page **pages);
238+
void vm_area_unmap_pages(struct vm_struct *area, unsigned long start,
239+
unsigned long end);
235240
void vunmap_range(unsigned long addr, unsigned long end);
236241
static inline void set_vm_flush_reset_perms(void *addr)
237242
{

mm/vmalloc.c

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,21 @@ static int vmap_range_noflush(unsigned long addr, unsigned long end,
307307
int ioremap_page_range(unsigned long addr, unsigned long end,
308308
phys_addr_t phys_addr, pgprot_t prot)
309309
{
310+
struct vm_struct *area;
310311
int err;
311312

313+
area = find_vm_area((void *)addr);
314+
if (!area || !(area->flags & VM_IOREMAP)) {
315+
WARN_ONCE(1, "vm_area at addr %lx is not marked as VM_IOREMAP\n", addr);
316+
return -EINVAL;
317+
}
318+
if (addr != (unsigned long)area->addr ||
319+
(void *)end != area->addr + get_vm_area_size(area)) {
320+
WARN_ONCE(1, "ioremap request [%lx,%lx) doesn't match vm_area [%lx, %lx)\n",
321+
addr, end, (long)area->addr,
322+
(long)area->addr + get_vm_area_size(area));
323+
return -ERANGE;
324+
}
312325
err = vmap_range_noflush(addr, end, phys_addr, pgprot_nx(prot),
313326
ioremap_max_page_shift);
314327
flush_cache_vmap(addr, end);
@@ -635,6 +648,58 @@ static int vmap_pages_range(unsigned long addr, unsigned long end,
635648
return err;
636649
}
637650

651+
static int check_sparse_vm_area(struct vm_struct *area, unsigned long start,
652+
unsigned long end)
653+
{
654+
might_sleep();
655+
if (WARN_ON_ONCE(area->flags & VM_FLUSH_RESET_PERMS))
656+
return -EINVAL;
657+
if (WARN_ON_ONCE(area->flags & VM_NO_GUARD))
658+
return -EINVAL;
659+
if (WARN_ON_ONCE(!(area->flags & VM_SPARSE)))
660+
return -EINVAL;
661+
if ((end - start) >> PAGE_SHIFT > totalram_pages())
662+
return -E2BIG;
663+
if (start < (unsigned long)area->addr ||
664+
(void *)end > area->addr + get_vm_area_size(area))
665+
return -ERANGE;
666+
return 0;
667+
}
668+
669+
/**
670+
* vm_area_map_pages - map pages inside given sparse vm_area
671+
* @area: vm_area
672+
* @start: start address inside vm_area
673+
* @end: end address inside vm_area
674+
* @pages: pages to map (always PAGE_SIZE pages)
675+
*/
676+
int vm_area_map_pages(struct vm_struct *area, unsigned long start,
677+
unsigned long end, struct page **pages)
678+
{
679+
int err;
680+
681+
err = check_sparse_vm_area(area, start, end);
682+
if (err)
683+
return err;
684+
685+
return vmap_pages_range(start, end, PAGE_KERNEL, pages, PAGE_SHIFT);
686+
}
687+
688+
/**
689+
* vm_area_unmap_pages - unmap pages inside given sparse vm_area
690+
* @area: vm_area
691+
* @start: start address inside vm_area
692+
* @end: end address inside vm_area
693+
*/
694+
void vm_area_unmap_pages(struct vm_struct *area, unsigned long start,
695+
unsigned long end)
696+
{
697+
if (check_sparse_vm_area(area, start, end))
698+
return;
699+
700+
vunmap_range(start, end);
701+
}
702+
638703
int is_vmalloc_or_module_addr(const void *x)
639704
{
640705
/*
@@ -3809,9 +3874,9 @@ long vread_iter(struct iov_iter *iter, const char *addr, size_t count)
38093874

38103875
if (flags & VMAP_RAM)
38113876
copied = vmap_ram_vread_iter(iter, addr, n, flags);
3812-
else if (!(vm && (vm->flags & VM_IOREMAP)))
3877+
else if (!(vm && (vm->flags & (VM_IOREMAP | VM_SPARSE))))
38133878
copied = aligned_vread_iter(iter, addr, n);
3814-
else /* IOREMAP area is treated as memory hole */
3879+
else /* IOREMAP | SPARSE area is treated as memory hole */
38153880
copied = zero_iter(iter, n);
38163881

38173882
addr += copied;
@@ -4402,6 +4467,9 @@ static int s_show(struct seq_file *m, void *p)
44024467
if (v->flags & VM_IOREMAP)
44034468
seq_puts(m, " ioremap");
44044469

4470+
if (v->flags & VM_SPARSE)
4471+
seq_puts(m, " sparse");
4472+
44054473
if (v->flags & VM_ALLOC)
44064474
seq_puts(m, " vmalloc");
44074475

0 commit comments

Comments
 (0)