Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3091561rwb; Mon, 15 Aug 2022 17:55:22 -0700 (PDT) X-Google-Smtp-Source: AA6agR7gXn6PiexeG7J6yKbN4gwcLw2/LAfLKpiNUTQNEMPAYNlJqVMZjITAG5Zyo6g951qWWrHy X-Received: by 2002:a05:6402:11cf:b0:43d:fc84:c51a with SMTP id j15-20020a05640211cf00b0043dfc84c51amr16130902edw.80.1660611196735; Mon, 15 Aug 2022 17:53:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660611196; cv=none; d=google.com; s=arc-20160816; b=TSEPlOuikAWH81FKZoxyaDN6C2A9MG3KI9PPCnjS4g8lc9DW2cH3WrUNgNMxydQeFQ rE4qtghiIizsixtKEi+QmznbCUcPhs2WXlCSPbXi2Nv08bQigmHQTUtxQu3e++TwAg5M PujDAKvDnWIb8PpYLn2kSbdoTVgZaomPOR07uHaqsckwDYq8t42NgJbRKL234t++zLq9 ipPKxp5Bgo499L6pFGVVt8Ftp5i3rZI5yTVDBxAieSrqnoEiOGsizvI1pKutlDT/ekM5 u9sJqYMHjV83D+5bXJTginhGcSBtL6oIUn4QIwlqb3odboTQnju5DE/QzKXtlxF00M8P g+FQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=msv0gv/6qhLgt0hjwDXdbbX7uljdqc7fY7+EOisOwQo=; b=pczmrCKvyB3G5bxm3M7kALiU3N1ykxSNJHke0UI2t5UqNJseVSJnFvmD/ue/vFdcX5 Exo+42egm3oZZZnWWXnhO03FIrXcKidC/81Yk98GWtlDk64bpqd0U/nicod7boWH1PVN bqGOY4T08Fq61RYoBwUN19ySVvEh032fyHmMguHOYEb3ZoLX9L0LiP3Nssx0ZmEHIs9l jYnWD01EDTkktuPTJEbxVnAl7DRaMCH1gVLMg/4foWhu07/vrJeEoGNZ84qOCT98AjN7 Ukd4X8Sbi8qazs7vaAH3hlZbzd1LqZ27tCrj5Iq+kAeXqHsFXwBRkpvOfPJDBX8IK+q9 pMsw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=EoroEjCm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h21-20020a170906111500b007317756bbffsi7778403eja.779.2022.08.15.17.52.51; Mon, 15 Aug 2022 17:53:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=EoroEjCm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353120AbiHPAUo (ORCPT + 99 others); Mon, 15 Aug 2022 20:20:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1357655AbiHPAN7 (ORCPT ); Mon, 15 Aug 2022 20:13:59 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A70D1782AE; Mon, 15 Aug 2022 13:30:25 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 6A6A7611B1; Mon, 15 Aug 2022 20:30:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4A126C433C1; Mon, 15 Aug 2022 20:30:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660595423; bh=bZ788B6z466vmlS6LVITTCvYlqYe/U/IhX4N2/r73Is=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EoroEjCmFOVGV5KfVxdfwd8dedhu8kGHYMjmIBcB3rNjCQDHO37n2Jdj6IRIvWkAM hbmfwogzabzDD1HBNa4J4DBdfsJi7+K92bZCrUNfVr6e105oYWbWcm/74S4V2JT8wQ Tv8jhaf22ppsSVJoXNCYukFvI7XqnB5Q2i5ukdM0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Miaohe Lin , Jerome Glisse , Alistair Popple , Jason Gunthorpe , Ralph Campbell , Andrew Morton , Sasha Levin Subject: [PATCH 5.19 0727/1157] lib/test_hmm: avoid accessing uninitialized pages Date: Mon, 15 Aug 2022 20:01:23 +0200 Message-Id: <20220815180508.560058174@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180439.416659447@linuxfoundation.org> References: <20220815180439.416659447@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Miaohe Lin [ Upstream commit ed913b055a74b723976f8e885a3395162a0371e6 ] 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: b659baea7546 ("mm: selftests for exclusive device memory") Signed-off-by: Miaohe Lin Cc: Jerome Glisse Cc: Alistair Popple Cc: Jason Gunthorpe Cc: Ralph Campbell Signed-off-by: Andrew Morton Signed-off-by: Sasha Levin --- 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 cfe632047839..f2c3015c5c82 100644 --- a/lib/test_hmm.c +++ b/lib/test_hmm.c @@ -732,7 +732,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)) @@ -741,7 +741,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]); -- 2.35.1