Received: by 2002:a05:6358:bb9e:b0:b9:5105:a5b4 with SMTP id df30csp2838907rwb; Mon, 5 Sep 2022 02:16:09 -0700 (PDT) X-Google-Smtp-Source: AA6agR5t8+mH1dd9J/jaiYqUETxfN/eWTa8hb7UHgEbghAK4ZFVpD/67C+Qhbm5+vaBhU89iqnxZ X-Received: by 2002:a17:902:eccf:b0:175:4410:b6db with SMTP id a15-20020a170902eccf00b001754410b6dbmr24468572plh.170.1662369369621; Mon, 05 Sep 2022 02:16:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662369369; cv=none; d=google.com; s=arc-20160816; b=OZrmVGHkxJ6SxoKQf5hL8Aeuz/kyEQ9VqALs6L+YUmmDXXr/J7aw6+awcIdp4Um3fj 3pGmFLUOrFxgQSXsdfmMLHAhYAjNlkbvSaJhR9mo7TSoxvijRxhLh9oZ2jlqtfR8wtoc ek5tqGmgBkovhC0JbqQ3NYer/1CQyxUkBErK2qYk3ZmMQVrjfVwSmEGIfQjNncbkCG/R U2UlhDq+5kFoYyg6YASrFPco9x+VQZ39o/tbosGpTJTaa05GlCGqzB4bYoile7rgCuQK +eV4/5G3N4Gc/kV786y9MowIzBu69AEND71oIzg1sM7kq8DVucSRYKe2OZkfBTP5p8aB CpAA== 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=aoFbemyDU+f+b3BvxYQMAAfqeFTJORyFpOVWGzC12nw=; b=Mao67VBPZvyo3jvyZw9Y84H82aVojVX1bkWP/TuUne4rbLa0uT2pONg4SkXiGS0+3R tU2tlQoHndlKN3yXrj1MLioRrPSiji4A3QPrDqNFNjdAqA5XAPIy8RXmqduS0M+katgJ LXg7GTh8JJY+Iqrk4MW1h7o6No+ypnmvn9NElOlu77cMVjz9p5qI98IKCvcCKUBfvfe9 HN6HKTXw8qkKtEjcTryLA4TH1/8ed8tCJ7O6iCv0AM//g83FH6Avu3iwKyiwIN2FUs6F is24jFsZA1A+pgktIfKPfjd+12W9h/WBJs92VyTN/TJfIzlJzE98ztGIQMbkicaMmpvB LXqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=YPnxvO1B; 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 1-20020a621401000000b00537e9dcebecsi10198892pfu.115.2022.09.05.02.15.58; Mon, 05 Sep 2022 02:16:09 -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=YPnxvO1B; 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 S237876AbiIEIiB (ORCPT + 99 others); Mon, 5 Sep 2022 04:38:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237001AbiIEIhq (ORCPT ); Mon, 5 Sep 2022 04:37:46 -0400 Received: from mail-m974.mail.163.com (mail-m974.mail.163.com [123.126.97.4]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 155EA19C20; Mon, 5 Sep 2022 01:36:17 -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=aoFbe myDU+f+b3BvxYQMAAfqeFTJORyFpOVWGzC12nw=; b=YPnxvO1Bh6Fm3CpwKWzZ1 1AeAXCmPKbRHp/cKDik/DNzoJWNaZm7xzf8Qz6udj0x8ETQlp/cs54lbxzEG0fd1 gelmaG8pykOzv8dMreaw4K07pPbAZ0MKWwsfbuqe8dBt5qVGmAidpa1AbzxR+ZM1 L8gI5qw5XVQYbMArxAYm7Y= Received: from localhost.localdomain (unknown [36.112.3.113]) by smtp4 (Coremail) with SMTP id HNxpCgDnKLLwtBVjHqmWaQ--.30535S4; Mon, 05 Sep 2022 16:36:08 +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: Mon, 5 Sep 2022 16:35:59 +0800 Message-Id: <20220905083559.49438-1-niejianglei2021@163.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: HNxpCgDnKLLwtBVjHqmWaQ--.30535S4 X-Coremail-Antispam: 1Uf129KBjvJXoW7XrW3Jw1rCr1kWrWkXF48Xrb_yoW8JF1xpr Z5KF4DGr1vywnrJ397tFn5Ca47ZwsYg345W39Ivw1kuF13WrnYva1rJrWjgr4xAry0g3W0 9FZ7Ww17XanFkw7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0ziqXdrUUUUU= X-Originating-IP: [36.112.3.113] X-CM-SenderInfo: xqlhyxxdqjzvrlsqjii6rwjhhfrp/1tbiFRhzjF5mMdNJBQAAsk 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..15345ad0b319 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