From patchwork Mon Jun 22 15:35:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colin King X-Patchwork-Id: 11618231 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 80D3D17C5 for ; Mon, 22 Jun 2020 15:35:58 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5655C20739 for ; Mon, 22 Jun 2020 15:35:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5655C20739 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=canonical.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 5BD3C6B0006; Mon, 22 Jun 2020 11:35:57 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 56C9D6B0007; Mon, 22 Jun 2020 11:35:57 -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 4AA4A6B0008; Mon, 22 Jun 2020 11:35:57 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0115.hostedemail.com [216.40.44.115]) by kanga.kvack.org (Postfix) with ESMTP id 32FEF6B0006 for ; Mon, 22 Jun 2020 11:35:57 -0400 (EDT) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id CB1DF1F0DA2 for ; Mon, 22 Jun 2020 15:35:56 +0000 (UTC) X-FDA: 76957248312.30.coil08_230b69d26e34 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin30.hostedemail.com (Postfix) with ESMTP id 8D735182E246C for ; Mon, 22 Jun 2020 15:35:56 +0000 (UTC) X-Spam-Summary: 2,0,0,c04be87adb9b9623,d41d8cd98f00b204,colin.king@canonical.com,,RULES_HIT:41:152:355:379:541:800:960:966:973:988:989:1260:1261:1277:1311:1313:1314:1345:1437:1515:1516:1518:1534:1541:1593:1594:1711:1730:1747:1777:1792:2196:2199:2393:2559:2562:3138:3139:3140:3141:3142:3352:3865:3866:3867:3868:4250:4385:4605:5007:6261:7576:7903:9707:10004:10400:11026:11473:11658:11914:12043:12048:12296:12297:12438:12517:12519:12555:12679:12895:13069:13255:13311:13357:13894:14181:14394:14721:21080:21451:21627:21990:30054:30070,0,RBL:91.189.89.112:@canonical.com:.lbl8.mailshell.net-64.201.201.201 62.8.15.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:25,LUA_SUMMARY:none X-HE-Tag: coil08_230b69d26e34 X-Filterd-Recvd-Size: 3078 Received: from youngberry.canonical.com (youngberry.canonical.com [91.189.89.112]) by imf33.hostedemail.com (Postfix) with ESMTP for ; Mon, 22 Jun 2020 15:35:55 +0000 (UTC) Received: from 1.general.cking.uk.vpn ([10.172.193.212] helo=localhost) by youngberry.canonical.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.86_2) (envelope-from ) id 1jnOU6-00034z-Ab; Mon, 22 Jun 2020 15:35:46 +0000 From: Colin King To: Seth Jennings , Dan Streetman , Vitaly Wool , Andrew Morton , Barry Song , Stephen Rothwell , linux-mm@kvack.org Cc: kernel-janitors@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH][next] mm/zswap: fix a couple of memory leaks and rework kzalloc failure check Date: Mon, 22 Jun 2020 16:35:46 +0100 Message-Id: <20200622153546.49880-1-colin.king@canonical.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Rspamd-Queue-Id: 8D735182E246C 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: From: Colin Ian King kzalloc failures return NULL on out of memory errors, so replace the IS_ERR_OR_NULL check with the usual null pointer check. Fix two memory leaks with on acomp and acomp_ctx by ensuring these objects are free'd on the error return path. Addresses-Coverity: ("Resource leak") Fixes: d4f86abd6e35 ("mm/zswap: move to use crypto_acomp API for hardware acceleration") Signed-off-by: Colin Ian King --- mm/zswap.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 0d914ba6b4a0..14839cbac7ff 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -433,23 +433,23 @@ static int zswap_cpu_comp_prepare(unsigned int cpu, struct hlist_node *node) return 0; acomp_ctx = kzalloc(sizeof(*acomp_ctx), GFP_KERNEL); - if (IS_ERR_OR_NULL(acomp_ctx)) { + if (!acomp_ctx) { pr_err("Could not initialize acomp_ctx\n"); return -ENOMEM; } acomp = crypto_alloc_acomp(pool->tfm_name, 0, 0); - if (IS_ERR_OR_NULL(acomp)) { + if (!acomp) { pr_err("could not alloc crypto acomp %s : %ld\n", pool->tfm_name, PTR_ERR(acomp)); - return -ENOMEM; + goto free_acomp_ctx; } acomp_ctx->acomp = acomp; req = acomp_request_alloc(acomp_ctx->acomp); - if (IS_ERR_OR_NULL(req)) { + if (!req) { pr_err("could not alloc crypto acomp %s : %ld\n", pool->tfm_name, PTR_ERR(acomp)); - return -ENOMEM; + goto free_acomp; } acomp_ctx->req = req; @@ -462,6 +462,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: + kfree(acomp); +free_acomp_ctx: + kfree(acomp_ctx); + return -ENOMEM; } static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node)