linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Barry Song <song.bao.hua@hisilicon.com>
To: <akpm@linux-foundation.org>
Cc: <linux-mm@kvack.org>, <linux-kernel@vger.kernel.org>,
	<linuxarm@huawei.com>, Barry Song <song.bao.hua@hisilicon.com>,
	Herbert Xu <herbert@gondor.apana.org.au>,
	Seth Jennings <sjenning@redhat.com>,
	"Dan Streetman" <ddstreet@ieee.org>,
	Vitaly Wool <vitaly.wool@konsulko.com>
Subject: [PATCH] mm/zswap: careful error path implementation in comp_prepare
Date: Mon, 22 Jun 2020 22:52:28 +1200	[thread overview]
Message-ID: <20200622105228.17720-1-song.bao.hua@hisilicon.com> (raw)

Free the allocated memory and resource while an error occurs.

Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: David S. Miller" <davem@davemloft.net>
Cc: Seth Jennings <sjenning@redhat.com>
Cc: Dan Streetman <ddstreet@ieee.org>
Cc: Vitaly Wool <vitaly.wool@konsulko.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Barry Song <song.bao.hua@hisilicon.com>
---
 -v1: an incremental patch againest linux-next to fix the issue pointed
      out by Vitaly

 mm/zswap.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/mm/zswap.c b/mm/zswap.c
index 0d914ba6b4a0..c0a85ef46610 100644
--- a/mm/zswap.c
+++ b/mm/zswap.c
@@ -428,28 +428,31 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
 	struct crypto_acomp *acomp;
 	struct acomp_req *req;
 	struct crypto_acomp_ctx *acomp_ctx;
+	int ret;
 
 	if (WARN_ON(*per_cpu_ptr(pool->acomp_ctx, cpu)))
 		return 0;
 
 	acomp_ctx = kzalloc(sizeof(*acomp_ctx), GFP_KERNEL);
-	if (IS_ERR_OR_NULL(acomp_ctx)) {
-		pr_err("Could not initialize acomp_ctx\n");
+	if (!acomp_ctx) {
+		pr_err("Could not allocate acomp_ctx\n");
 		return -ENOMEM;
 	}
 	acomp = crypto_alloc_acomp(pool->tfm_name, 0, 0);
-	if (IS_ERR_OR_NULL(acomp)) {
+	if (IS_ERR(acomp)) {
 		pr_err("could not alloc crypto acomp %s : %ld\n",
 				pool->tfm_name, PTR_ERR(acomp));
-		return -ENOMEM;
+		ret = PTR_ERR(acomp);
+		goto free_ctx;
 	}
 	acomp_ctx->acomp = acomp;
 
 	req = acomp_request_alloc(acomp_ctx->acomp);
-	if (IS_ERR_OR_NULL(req)) {
-		pr_err("could not alloc crypto acomp %s : %ld\n",
-		       pool->tfm_name, PTR_ERR(acomp));
-		return -ENOMEM;
+	if (!req) {
+		pr_err("could not alloc crypto acomp_request %s\n",
+		       pool->tfm_name);
+		ret = -ENOMEM;
+		goto free_acomp;
 	}
 	acomp_ctx->req = req;
 
@@ -462,6 +465,12 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node)
 	*per_cpu_ptr(pool->acomp_ctx, cpu) = acomp_ctx;
 
 	return 0;
+
+free_acomp:
+	crypto_free_acomp(acomp_ctx->acomp);
+free_ctx:
+	kfree(acomp_ctx);
+	return ret;
 }
 
 static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)
-- 
2.27.0




                 reply	other threads:[~2020-06-22 10:54 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200622105228.17720-1-song.bao.hua@hisilicon.com \
    --to=song.bao.hua@hisilicon.com \
    --cc=akpm@linux-foundation.org \
    --cc=ddstreet@ieee.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linuxarm@huawei.com \
    --cc=sjenning@redhat.com \
    --cc=vitaly.wool@konsulko.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox