From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3F7E661FDF; Mon, 27 May 2024 19:37:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716838674; cv=none; b=fxIl355q0ixrBE15zG+az0NImRm+FJZiz2FiBbbFHurMtngmFidzvapqfFQdWqChaXfj7LcQbbrBuQtI1e12qap/MuiFhS6hDUXEkdlvhvzGmvAr3WazxsFQHbVhbuDtwg4SSTLPPhmsQlDGBavTle2zTpNTLoz/92qGOno//ZA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716838674; c=relaxed/simple; bh=hYk9Zwsc95JeuQQZJ8AFqTcEYolv4Zsb3Rn52WrGJ5M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=O12yLjxlmTfovJPx11lDC8Y/yHwsaaUc50rDtkybBpm7ZKEyLs/+cTBDQKf8I/cokJmTADVw+pU6CLYWDGr1wP8PHmDoyNQokiJNsuqvgCHMTRfaJriG2lA2tqaOSJoMmAgJMd9Fo6jE4nhwV50I1RM7SvMrTFwmGYuZdbiH2kk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=benRKdVC; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="benRKdVC" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C7D8DC32781; Mon, 27 May 2024 19:37:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1716838674; bh=hYk9Zwsc95JeuQQZJ8AFqTcEYolv4Zsb3Rn52WrGJ5M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=benRKdVCB/X18hlDHJO2Pf7BPW8pcWZ2bg3l3YE22+rYx8/Vdb8kq69lfPhjYioh9 ZfXAcyCoMUr4ekE9qLnblXRdID0p/dPVJfRL6XZlCkCqd2HWA8EZQLHJ9IgB/sinh+ 0YB/7I8913cLj4hDQpTfOQjkeIfMmlsDqRvs6c3U= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Duoming Zhou , =?UTF-8?q?J=C3=A9r=C3=B4me=20Glisse?= , Andrew Morton , Sasha Levin Subject: [PATCH 6.8 441/493] lib/test_hmm.c: handle src_pfns and dst_pfns allocation failure Date: Mon, 27 May 2024 20:57:23 +0200 Message-ID: <20240527185644.682141913@linuxfoundation.org> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240527185626.546110716@linuxfoundation.org> References: <20240527185626.546110716@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 6.8-stable review patch. If anyone has any objections, please let me know. ------------------ From: Duoming Zhou [ Upstream commit c2af060d1c18beaec56351cf9c9bcbbc5af341a3 ] The kcalloc() in dmirror_device_evict_chunk() will return null if the physical memory has run out. As a result, if src_pfns or dst_pfns is dereferenced, the null pointer dereference bug will happen. Moreover, the device is going away. If the kcalloc() fails, the pages mapping a chunk could not be evicted. So add a __GFP_NOFAIL flag in kcalloc(). Finally, as there is no need to have physically contiguous memory, Switch kcalloc() to kvcalloc() in order to avoid failing allocations. Link: https://lkml.kernel.org/r/20240312005905.9939-1-duoming@zju.edu.cn Fixes: b2ef9f5a5cb3 ("mm/hmm/test: add selftest driver for HMM") Signed-off-by: Duoming Zhou Cc: Jérôme Glisse Signed-off-by: Andrew Morton Signed-off-by: Sasha Levin --- lib/test_hmm.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/test_hmm.c b/lib/test_hmm.c index 717dcb8301273..b823ba7cb6a15 100644 --- a/lib/test_hmm.c +++ b/lib/test_hmm.c @@ -1226,8 +1226,8 @@ static void dmirror_device_evict_chunk(struct dmirror_chunk *chunk) unsigned long *src_pfns; unsigned long *dst_pfns; - src_pfns = kcalloc(npages, sizeof(*src_pfns), GFP_KERNEL); - dst_pfns = kcalloc(npages, sizeof(*dst_pfns), GFP_KERNEL); + src_pfns = kvcalloc(npages, sizeof(*src_pfns), GFP_KERNEL | __GFP_NOFAIL); + dst_pfns = kvcalloc(npages, sizeof(*dst_pfns), GFP_KERNEL | __GFP_NOFAIL); migrate_device_range(src_pfns, start_pfn, npages); for (i = 0; i < npages; i++) { @@ -1250,8 +1250,8 @@ static void dmirror_device_evict_chunk(struct dmirror_chunk *chunk) } migrate_device_pages(src_pfns, dst_pfns, npages); migrate_device_finalize(src_pfns, dst_pfns, npages); - kfree(src_pfns); - kfree(dst_pfns); + kvfree(src_pfns); + kvfree(dst_pfns); } /* Removes free pages from the free list so they can't be re-allocated */ -- 2.43.0