Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3022317rwb; Mon, 15 Aug 2022 16:19:48 -0700 (PDT) X-Google-Smtp-Source: AA6agR79Cu/HXm0mR5m0uGwTfRenG77XmqBz3rIjZC9D1RDwPVoD4CdVpYaboIivHo+T2m6xeNwD X-Received: by 2002:a17:90a:66cf:b0:1f7:b8d2:c2d6 with SMTP id z15-20020a17090a66cf00b001f7b8d2c2d6mr21082573pjl.67.1660605587987; Mon, 15 Aug 2022 16:19:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660605587; cv=none; d=google.com; s=arc-20160816; b=f+P1kr6cy6nm80U1A3yPDvEDIA11JAVWLk/InQC1jAr4XfF2CLZV8lIuqvkpPLvFf3 jYueQL05m4wXy6ZsEMr5pEbx/EEnmIhP8NqZ92aqc2Vo0giba/Gxdef0wilB8COB/FOG 1tYIQcIKMorVvwFyWdsDE6Hlrgh2JBijpbrhnlrFSwwhu281o4XBJnc3nO3D7W4eoRyK N+YY7FmaF2RQmkVqrcGIm+Yx/wSZi7Ih6/aI+OaknaohK6azVCsLkuA+VCvGokmxhKZh +siD2DHnvVpejPnMiZOs6+HQey8zh9bbPjeHv3Es9IKnXfFRA36YZrXcfRrixjJO5ptO 9zVA== 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=CYjzBhKUFGbY2i6NOcKn0tSROntdPi6PmCyakWp5HntkONK/fEOmav678FUmKQTJvb o03lY8Ll9lyvKM/RQJKG8bq6fXBQ1LtZOY/YRpKOhvf5O2VvBnMavrFBOHDgL6QClPGu v6s07jMi4ytGCZ0GbPxG12ond9zgS8jfEtPjGwrxNNirvH7QGcAxWQG/sHVf/q6ySW04 bOaBRjkFadkmlt6z2zxYnCw0nuxrK1BLqIe/rDwNiO3dnF4QnNC7Vj2BDMS+/YkP0odi 1hoNf4YoA/YxRDJzDudAcNgtX0XgiyAA7YbWsUnM2hOViNtDzzq1rDITmRbOuLkBNIJ6 nTRg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=JArRvkp0; 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 bg8-20020a1709028e8800b00172748868d3si2632453plb.611.2022.08.15.16.19.37; Mon, 15 Aug 2022 16:19:47 -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=JArRvkp0; 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 S1349618AbiHOVsU (ORCPT + 99 others); Mon, 15 Aug 2022 17:48:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52774 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346706AbiHOVo1 (ORCPT ); Mon, 15 Aug 2022 17:44:27 -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 38A871022B1; Mon, 15 Aug 2022 12:29:54 -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 C693B60EF0; Mon, 15 Aug 2022 19:29:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B97A6C433D6; Mon, 15 Aug 2022 19:29:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660591793; bh=bZ788B6z466vmlS6LVITTCvYlqYe/U/IhX4N2/r73Is=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JArRvkp0Iarwi1tZkms2ephH3WB5Jef8NshxDiVpWhnoj+Nc3G/mUT3QNJECvpXbp LNJHeEHbyRhPThDrWrylc9gBPuwUn8T/sDbQ8wsYIlBYfBGGI4w0FPsWswS67u/kN5 9W3VIXGh7OflOr/duyCRAMSEjIrBI8vUTbS4iMHI= 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.18 0682/1095] lib/test_hmm: avoid accessing uninitialized pages Date: Mon, 15 Aug 2022 20:01:21 +0200 Message-Id: <20220815180457.586158042@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180429.240518113@linuxfoundation.org> References: <20220815180429.240518113@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