Skip to content

Commit 616a99d

Browse files
committed
Merge tag 'for-linus-urgent' of git://git.kernel.org/pub/scm/virt/kvm/kvm
Pull kvm fixes from Paolo Bonzini: "A selftests fix for ARM, and the fix for page reference count underflow. This is a very small fix that was provided by Nick Piggin and tested by myself" * tag 'for-linus-urgent' of git://git.kernel.org/pub/scm/virt/kvm/kvm: KVM: do not allow mapping valid but non-reference-counted pages KVM: selftests: Fix mapping length truncation in m{,un}map()
2 parents 94ca94b + f8be156 commit 616a99d

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

tools/testing/selftests/kvm/set_memory_region_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ static void test_add_max_memory_regions(void)
376376
pr_info("Adding slots 0..%i, each memory region with %dK size\n",
377377
(max_mem_slots - 1), MEM_REGION_SIZE >> 10);
378378

379-
mem = mmap(NULL, MEM_REGION_SIZE * max_mem_slots + alignment,
379+
mem = mmap(NULL, (size_t)max_mem_slots * MEM_REGION_SIZE + alignment,
380380
PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
381381
TEST_ASSERT(mem != MAP_FAILED, "Failed to mmap() host");
382382
mem_aligned = (void *)(((size_t) mem + alignment - 1) & ~(alignment - 1));
@@ -401,7 +401,7 @@ static void test_add_max_memory_regions(void)
401401
TEST_ASSERT(ret == -1 && errno == EINVAL,
402402
"Adding one more memory slot should fail with EINVAL");
403403

404-
munmap(mem, MEM_REGION_SIZE * max_mem_slots + alignment);
404+
munmap(mem, (size_t)max_mem_slots * MEM_REGION_SIZE + alignment);
405405
munmap(mem_extra, MEM_REGION_SIZE);
406406
kvm_vm_free(vm);
407407
}

virt/kvm/kvm_main.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,6 +2055,13 @@ static bool vma_is_valid(struct vm_area_struct *vma, bool write_fault)
20552055
return true;
20562056
}
20572057

2058+
static int kvm_try_get_pfn(kvm_pfn_t pfn)
2059+
{
2060+
if (kvm_is_reserved_pfn(pfn))
2061+
return 1;
2062+
return get_page_unless_zero(pfn_to_page(pfn));
2063+
}
2064+
20582065
static int hva_to_pfn_remapped(struct vm_area_struct *vma,
20592066
unsigned long addr, bool *async,
20602067
bool write_fault, bool *writable,
@@ -2104,13 +2111,21 @@ static int hva_to_pfn_remapped(struct vm_area_struct *vma,
21042111
* Whoever called remap_pfn_range is also going to call e.g.
21052112
* unmap_mapping_range before the underlying pages are freed,
21062113
* causing a call to our MMU notifier.
2114+
*
2115+
* Certain IO or PFNMAP mappings can be backed with valid
2116+
* struct pages, but be allocated without refcounting e.g.,
2117+
* tail pages of non-compound higher order allocations, which
2118+
* would then underflow the refcount when the caller does the
2119+
* required put_page. Don't allow those pages here.
21072120
*/
2108-
kvm_get_pfn(pfn);
2121+
if (!kvm_try_get_pfn(pfn))
2122+
r = -EFAULT;
21092123

21102124
out:
21112125
pte_unmap_unlock(ptep, ptl);
21122126
*p_pfn = pfn;
2113-
return 0;
2127+
2128+
return r;
21142129
}
21152130

21162131
/*

0 commit comments

Comments
 (0)