From patchwork Mon Jun 22 10:52:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Song Bao Hua (Barry Song)" X-Patchwork-Id: 11617457 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B5DEE912 for ; Mon, 22 Jun 2020 10:54:15 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8CFCC20706 for ; Mon, 22 Jun 2020 10:54:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8CFCC20706 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=hisilicon.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 6EB4E6B0002; Mon, 22 Jun 2020 06:54:14 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 672826B0003; Mon, 22 Jun 2020 06:54:14 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5624B6B0005; Mon, 22 Jun 2020 06:54:14 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0152.hostedemail.com [216.40.44.152]) by kanga.kvack.org (Postfix) with ESMTP id 3E0446B0002 for ; Mon, 22 Jun 2020 06:54:14 -0400 (EDT) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 0C31B2DFD for ; Mon, 22 Jun 2020 10:54:14 +0000 (UTC) X-FDA: 76956538428.12.art02_06185cd26e32 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin12.hostedemail.com (Postfix) with ESMTP id D9E3F18001FA1 for ; Mon, 22 Jun 2020 10:54:13 +0000 (UTC) X-Spam-Summary: 2,0,0,dc635b57a967c778,d41d8cd98f00b204,song.bao.hua@hisilicon.com,,RULES_HIT:41:355:379:541:582:800:960:966:973:988:989:1152:1260:1261:1277:1311:1313:1314:1345:1431:1437:1515:1516:1518:1534:1542:1711:1714:1730:1747:1777:1792:2196:2199:2393:2559:2562:3138:3139:3140:3141:3142:3351:3865:3867:3868:4321:4385:4605:5007:6261:7903:10004:10400:11026:11473:11658:11914:12043:12296:12297:12438:12555:12895:12986:13894:14181:14394:14721:21080:21451:21627:21990:30054,0,RBL:45.249.212.190:@hisilicon.com:.lbl8.mailshell.net-64.100.201.201 62.2.2.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:23,LUA_SUMMARY:none X-HE-Tag: art02_06185cd26e32 X-Filterd-Recvd-Size: 3520 Received: from huawei.com (szxga04-in.huawei.com [45.249.212.190]) by imf26.hostedemail.com (Postfix) with ESMTP for ; Mon, 22 Jun 2020 10:54:13 +0000 (UTC) Received: from DGGEMS405-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id DD2B3CE1167AFCF01F41; Mon, 22 Jun 2020 18:54:09 +0800 (CST) Received: from SWX921481.china.huawei.com (10.126.203.232) by DGGEMS405-HUB.china.huawei.com (10.3.19.205) with Microsoft SMTP Server id 14.3.487.0; Mon, 22 Jun 2020 18:54:01 +0800 From: Barry Song To: CC: , , , Barry Song , Herbert Xu , Seth Jennings , "Dan Streetman" , Vitaly Wool Subject: [PATCH] mm/zswap: careful error path implementation in comp_prepare Date: Mon, 22 Jun 2020 22:52:28 +1200 Message-ID: <20200622105228.17720-1-song.bao.hua@hisilicon.com> X-Mailer: git-send-email 2.21.0.windows.1 MIME-Version: 1.0 X-Originating-IP: [10.126.203.232] X-CFilter-Loop: Reflected X-Rspamd-Queue-Id: D9E3F18001FA1 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam02 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Free the allocated memory and resource while an error occurs. Cc: Herbert Xu Cc: David S. Miller" Cc: Seth Jennings Cc: Dan Streetman Cc: Vitaly Wool Cc: Andrew Morton Signed-off-by: Barry Song --- -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)