Skip to content

Commit

Permalink
lib/test_hmm: avoid accessing uninitialized pages
Browse files Browse the repository at this point in the history
[ Upstream commit ed913b0 ]

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/20220609130835.35110-1-linmiaohe@huawei.com
Fixes: b659bae ("mm: selftests for exclusive device memory")
Signed-off-by: Miaohe Lin <linmiaohe@huawei.com>
Cc: Jerome Glisse <jglisse@redhat.com>
Cc: Alistair Popple <apopple@nvidia.com>
Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Ralph Campbell <rcampbell@nvidia.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
  • Loading branch information
MiaoheLin authored and gregkh committed Aug 17, 2022
1 parent 81b64a9 commit d1e0cee
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/test_hmm.c
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ static int dmirror_exclusive(struct dmirror *dmirror,

mmap_read_lock(mm);
for (addr = start; addr < end; addr = next) {
unsigned long mapped;
unsigned long mapped = 0;
int i;

if (end < addr + (ARRAY_SIZE(pages) << PAGE_SHIFT))
Expand All @@ -740,7 +740,13 @@ static int dmirror_exclusive(struct dmirror *dmirror,
next = addr + (ARRAY_SIZE(pages) << PAGE_SHIFT);

ret = make_device_exclusive_range(mm, addr, next, pages, NULL);
mapped = dmirror_atomic_map(addr, next, pages, dmirror);
/*
* Do dmirror_atomic_map() iff all pages are marked for
* exclusive access to avoid accessing uninitialized
* fields of pages.
*/
if (ret == (next - addr) >> PAGE_SHIFT)
mapped = dmirror_atomic_map(addr, next, pages, dmirror);
for (i = 0; i < ret; i++) {
if (pages[i]) {
unlock_page(pages[i]);
Expand Down

0 comments on commit d1e0cee

Please sign in to comment.