From patchwork Thu Jun 9 13:08:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miaohe Lin X-Patchwork-Id: 12875560 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5B3FAC43334 for ; Thu, 9 Jun 2022 13:08:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BCFD28D001B; Thu, 9 Jun 2022 09:08:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B7FA38D0006; Thu, 9 Jun 2022 09:08:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A46F98D001B; Thu, 9 Jun 2022 09:08:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 972898D0006 for ; Thu, 9 Jun 2022 09:08:37 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 5CFE234A72 for ; Thu, 9 Jun 2022 13:08:37 +0000 (UTC) X-FDA: 79558726674.20.28C44CE Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf05.hostedemail.com (Postfix) with ESMTP id 1F3CF10004D for ; Thu, 9 Jun 2022 13:08:33 +0000 (UTC) Received: from canpemm500002.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4LJkrJ0Hq3zgY9W; Thu, 9 Jun 2022 21:06:40 +0800 (CST) Received: from huawei.com (10.175.124.27) by canpemm500002.china.huawei.com (7.192.104.244) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.24; Thu, 9 Jun 2022 21:08:30 +0800 From: Miaohe Lin To: CC: , , , , , , Subject: [PATCH] lib/test_hmm: avoid accessing uninitialized pages Date: Thu, 9 Jun 2022 21:08:35 +0800 Message-ID: <20220609130835.35110-1-linmiaohe@huawei.com> X-Mailer: git-send-email 2.23.0 MIME-Version: 1.0 X-Originating-IP: [10.175.124.27] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To canpemm500002.china.huawei.com (7.192.104.244) X-CFilter-Loop: Reflected ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1654780116; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references; bh=psEWkp3xG7ntMYguMS3iNXkWWGY9PL+5ppy81PcHpak=; b=m8ytoOnl/t1BK6TTZfuJB9ztqN8rvdYYYBE8fWF8GFLfpGCu/jjV8tLiNy7p37iG+Dpc4S mHgz17b0Hgfywl5lZIVqEX7CGlYydcHy67pdPQI0cEpq73mPdT3jVOxCaWvCowJ1xFdjLp 6yRYr3w06gcjKUldEHUABRPcI8eb0Z8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1654780116; a=rsa-sha256; cv=none; b=zSq7y8YjJRYcYdKPaeZ3HaFApNhzXZ6fF/F5heHYQWqIRxS5cvKlwvH9/b1+kIOAUvTbHi FKGKOWUvOTewpoHMnB0J+0IlOQNgvxKL582ik7TUiJhBa7N0wRbuc9gL58a1yLJGJhowPK NYCKA9W/eSEFNnz/aGCYMI1J5FW2b0k= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf05.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com Authentication-Results: imf05.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf05.hostedemail.com: domain of linmiaohe@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=linmiaohe@huawei.com X-Rspamd-Server: rspam08 X-Rspam-User: X-Stat-Signature: hr8wiu4z7u35umgpifrw3unmex7siw56 X-Rspamd-Queue-Id: 1F3CF10004D X-HE-Tag: 1654780113-590410 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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. Fixes: b659baea7546 ("mm: selftests for exclusive device memory") Signed-off-by: Miaohe Lin --- lib/test_hmm.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/test_hmm.c b/lib/test_hmm.c index 7930853e7fc5..e3965cafd27c 100644 --- a/lib/test_hmm.c +++ b/lib/test_hmm.c @@ -797,7 +797,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)) @@ -806,7 +806,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]);