Received: by 2002:a05:6358:bb9e:b0:b9:5105:a5b4 with SMTP id df30csp5295638rwb; Tue, 6 Sep 2022 23:57:07 -0700 (PDT) X-Google-Smtp-Source: AA6agR7pT05otVdINWTkOReBv4ayooCsVquqivb/dBY0WjnixzhX+52PvLwNwFtDASoyidJPrjpk X-Received: by 2002:a17:907:7faa:b0:741:7674:5ea1 with SMTP id qk42-20020a1709077faa00b0074176745ea1mr1398782ejc.27.1662533827176; Tue, 06 Sep 2022 23:57:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662533827; cv=none; d=google.com; s=arc-20160816; b=KusbuHOf53H57hT1mnL6WvvB48Sx1DoZlLv8Mhju+HIDxI434z2IpThhmRrmzEX0Cf xLDcKoaSUu+lhxNzXXzrGDwwZnpwqnj1v1YbQVCb6c6MGx0nbvbIMTm2P03JsPn876O6 /2ZE4w80fflJ4pjEltVgL+Prt4O55/2dvdHZyETbjlS7SwmB013iRN+0D2ZqdFhk1/hb VuTH48Cre2LgvWnKK+XOq/qByU6u1Ki3/9cFVA/rCEeXq851ZpTA0p36Rvh4DT47K4XD 0QSz9lIEWA2wVB7MgZ1WFMCeRsdzYhPYPfkBehRC4eKia9KCPH2T/Wc3b92zm/jERTEw MHwA== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=2rIc6tezrARE1+CFWv+5cFs1Lg6X5Lme/PX2cYrHUA8=; b=je5eO72JpqhdMGozZAiUmed19py0ho7p43OaIip+ra6knir8HqgAGy4WZvAJbNJOZ8 871Kt6Cw8B9DkHY72XSuRaoOlBHJP2wG61FenWweDpjKeh+pwLwm7z8sJfKhi6j2nuAd UOaYt0ESN76uCdMLT9C6GZK9r36s94p79RM8V+tpMstq+RnLLDuhZ6DHvx1T9yqDFAxf qbH8xVSCSfeWjc/Z0i3MlMvnfYlqq60E+tsFZJrP1jQi3zx44UToSDfd2Kj8XI00tQ6Y QJRUK4hrGQCtUqpPLfEurfa4FRRllIiPWR1wqacnjA3UmizpHZAhebXPhh3sDLItObes 90tQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=ALygE55e; 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=163.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o8-20020a50c908000000b0043d37773dbasi3728151edh.90.2022.09.06.23.56.41; Tue, 06 Sep 2022 23:57:07 -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=@163.com header.s=s110527 header.b=ALygE55e; 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=163.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229768AbiIGGnr (ORCPT + 99 others); Wed, 7 Sep 2022 02:43:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229902AbiIGGng (ORCPT ); Wed, 7 Sep 2022 02:43:36 -0400 Received: from mail-m971.mail.163.com (mail-m971.mail.163.com [123.126.97.1]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1BC7114D22; Tue, 6 Sep 2022 23:43:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=2rIc6 tezrARE1+CFWv+5cFs1Lg6X5Lme/PX2cYrHUA8=; b=ALygE55e6myvo4zphyjfp Os1x6wwOfIX4pmddSjq3sI9ZzuxQG1OQP9BRCBHQGIZpPFZ++2zrwobeaR+R7DVO RrEsS5b/okcz3maBG/i4MdlEoNZUcAgGhEgTvkABcQQ//KuQydoTvV2OTPSD7MYr niLSiwFRM4AMLEB04IftDY= Received: from localhost.localdomain (unknown [36.112.3.164]) by smtp1 (Coremail) with SMTP id GdxpCgA3UqOEPRhjZ0q_ag--.42658S4; Wed, 07 Sep 2022 14:43:23 +0800 (CST) From: Jianglei Nie To: rafael@kernel.org Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Jianglei Nie Subject: [PATCH] PM: hibernate: Fix potential memory leak in hibernate_preallocate_memory() and prepare_highmem_image() Date: Wed, 7 Sep 2022 14:43:11 +0800 Message-Id: <20220907064311.54475-1-niejianglei2021@163.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: GdxpCgA3UqOEPRhjZ0q_ag--.42658S4 X-Coremail-Antispam: 1Uf129KBjvJXoW7XrW3Jw1rCr1kWrWkXF48Xrb_yoW8JF13pr Z5KF4DGr1vywnrJ397tFn5Ca47ZwsYg345W39Ivwn5uF13WrnYva1rJrWjgr4Iyry0g3Wj 9FZ7Ww1UXanrKw7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zieWldUUUUU= X-Originating-IP: [36.112.3.164] X-CM-SenderInfo: xqlhyxxdqjzvrlsqjii6rwjhhfrp/1tbiFQt1jF5mMevBQgAAsG X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,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 hibernate_preallocate_memory() and prepare_highmem_image() allocates memory chunk with memory_bm_create(). When the function gets some error after memory_bm_create(), relavent memory should be released with memory_bm_free(). Fix it by calling memory_bm_free() at the right time. Signed-off-by: Jianglei Nie --- kernel/power/snapshot.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/kernel/power/snapshot.c b/kernel/power/snapshot.c index 2a406753af90..e5ec204ebe22 100644 --- a/kernel/power/snapshot.c +++ b/kernel/power/snapshot.c @@ -1752,6 +1752,7 @@ int hibernate_preallocate_memory(void) error = memory_bm_create(©_bm, GFP_IMAGE, PG_ANY); if (error) { + memory_bm_free(&orig_bm, PG_UNSAFE_CLEAR); pr_err("Cannot allocate copy bitmap\n"); goto err_out; } @@ -2335,8 +2336,10 @@ static int prepare_highmem_image(struct memory_bitmap *bm, if (memory_bm_create(bm, GFP_ATOMIC, PG_SAFE)) return -ENOMEM; - if (get_highmem_buffer(PG_SAFE)) + if (get_highmem_buffer(PG_SAFE)) { + memory_bm_free(bm, PG_UNSAFE_CLEAR); return -ENOMEM; + } to_alloc = count_free_highmem_pages(); if (to_alloc > *nr_highmem_p) -- 2.25.1