Skip to content

Commit ed913b0

Browse files
MiaoheLinakpm00
authored andcommitted
lib/test_hmm: avoid accessing uninitialized pages
If make_device_exclusive_range() fails or returns pages marked for exclusive access less than required, remaining fields of pages will left uninitialized. So dmirror_atomic_map() will access those yet uninitialized fields of pages. To fix it, do dmirror_atomic_map() iff all pages are marked for exclusive access (we will break if mapped is less than required anyway) so we won't access those uninitialized fields of pages. Link: https://lkml.kernel.org/r/[email protected] Fixes: b659bae ("mm: selftests for exclusive device memory") Signed-off-by: Miaohe Lin <[email protected]> Cc: Jerome Glisse <[email protected]> Cc: Alistair Popple <[email protected]> Cc: Jason Gunthorpe <[email protected]> Cc: Ralph Campbell <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 2368903 commit ed913b0

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

lib/test_hmm.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ static int dmirror_exclusive(struct dmirror *dmirror,
732732

733733
mmap_read_lock(mm);
734734
for (addr = start; addr < end; addr = next) {
735-
unsigned long mapped;
735+
unsigned long mapped = 0;
736736
int i;
737737

738738
if (end < addr + (ARRAY_SIZE(pages) << PAGE_SHIFT))
@@ -741,7 +741,13 @@ static int dmirror_exclusive(struct dmirror *dmirror,
741741
next = addr + (ARRAY_SIZE(pages) << PAGE_SHIFT);
742742

743743
ret = make_device_exclusive_range(mm, addr, next, pages, NULL);
744-
mapped = dmirror_atomic_map(addr, next, pages, dmirror);
744+
/*
745+
* Do dmirror_atomic_map() iff all pages are marked for
746+
* exclusive access to avoid accessing uninitialized
747+
* fields of pages.
748+
*/
749+
if (ret == (next - addr) >> PAGE_SHIFT)
750+
mapped = dmirror_atomic_map(addr, next, pages, dmirror);
745751
for (i = 0; i < ret; i++) {
746752
if (pages[i]) {
747753
unlock_page(pages[i]);

0 commit comments

Comments
 (0)