From patchwork Wed Nov 13 05:24:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Sridhar, Kanchana P" X-Patchwork-Id: 13873164 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id EB564D41C02 for ; Wed, 13 Nov 2024 05:24:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3919E8D0005; Wed, 13 Nov 2024 00:24:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 33BEA8D0003; Wed, 13 Nov 2024 00:24:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1DD528D0005; Wed, 13 Nov 2024 00:24:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id E65FF8D0003 for ; Wed, 13 Nov 2024 00:24:19 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 57B02AC01C for ; Wed, 13 Nov 2024 05:24:19 +0000 (UTC) X-FDA: 82779930030.05.659F6F2 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.17]) by imf10.hostedemail.com (Postfix) with ESMTP id 69DF0C0008 for ; Wed, 13 Nov 2024 05:23:57 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=QZ+3CrCX; spf=pass (imf10.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 198.175.65.17 as permitted sender) smtp.mailfrom=kanchana.p.sridhar@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731475402; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=oP2XqoUm+so4bUKez9tax+BJmVjZpeYMkgN/6I8urHk=; b=oXaW6O7hObnGrYkd0/8LTm4o1ZNpCPGZRSnuk4YGFYnbsPqp0r2xtOtNmj9Rm/x58y2BEj 2XlHT0qophusVonujBDUl3MXuDhs6OpKGotwCW0Y1YEUPBrjIRIvrcO17HV8qZ6TtHCrs8 R+M0EC7LDtGkXPDgArYeXyonKoI86CQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731475402; a=rsa-sha256; cv=none; b=jXfAnQoEOXDuRjEcFfTuJAVrFbFJKBItRBPDpF1prmg7mNGc0kqih/66jwaqcy7opswB9m jDy87mtwh9c11ZcTHGDPdyy6xCUvQCzlHkrVwrb/uSHJcOAl3L+vnZDJ+KWD/tYEQHoMt4 PWK0gMu9xD0D9gHMXjPpIBOxKThgq1U= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=QZ+3CrCX; spf=pass (imf10.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 198.175.65.17 as permitted sender) smtp.mailfrom=kanchana.p.sridhar@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1731475456; x=1763011456; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=HmJm6AvXmIawUg1nmasfuVL0GJH2LxZ0AqcVw8E27Xk=; b=QZ+3CrCXgnuKzGe3zxm1FR79CTOxJl0nIBgahTBLHmazZ594I1JdRY58 fguxZE4KxvJYOl3PTL3yK6+FMcqbXApvBfy9NzcLcaM9HPdxFAG5D0xVA HTK/6ihLbQ7mWQCXKVkTGlKVbPjODYwpjBh9VeKY7DIIJpGUPhg2UqL04 qD3nlF9QBj6nN2IV2rMawdskWMr3HPlgbHx0L1VDoW86A/QhtZj/eB6l7 leFth8Jos40MkaYrABkihA8ENgSjSoVdF7cMIO4elxQCUCR2evO3Y1+2V /PmtP0MfjC/iyza2pkIGqZXO/X4ETXD7T4tuP6eAlkeh5zLNvMVxIpTBk Q==; X-CSE-ConnectionGUID: KrLn+MljSzWbeEwB3qcCNw== X-CSE-MsgGUID: xgJfx8nFTTG8EFS4dCivOQ== X-IronPort-AV: E=McAfee;i="6700,10204,11254"; a="31436615" X-IronPort-AV: E=Sophos;i="6.12,150,1728975600"; d="scan'208";a="31436615" Received: from orviesa007.jf.intel.com ([10.64.159.147]) by orvoesa109.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Nov 2024 21:24:13 -0800 X-CSE-ConnectionGUID: NJv4XNh/RC67hkv2ZqFUfQ== X-CSE-MsgGUID: Iwx2b4HYTRW0xNEwAz26Xw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.12,150,1728975600"; d="scan'208";a="88149831" Received: from unknown (HELO JF5300-B11A338T.jf.intel.com) ([10.242.51.115]) by orviesa007.jf.intel.com with ESMTP; 12 Nov 2024 21:24:14 -0800 From: Kanchana P Sridhar To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, hannes@cmpxchg.org, yosryahmed@google.com, nphamcs@gmail.com, chengming.zhou@linux.dev, usamaarif642@gmail.com, ryan.roberts@arm.com, ying.huang@intel.com, 21cnbao@gmail.com, akpm@linux-foundation.org Cc: wajdi.k.feghali@intel.com, vinodh.gopal@intel.com, kanchana.p.sridhar@intel.com Subject: [PATCH v1] mm: zswap: Fix a potential memory leak in zswap_decompress(). Date: Tue, 12 Nov 2024 21:24:13 -0800 Message-Id: <20241113052413.157039-1-kanchana.p.sridhar@intel.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Stat-Signature: emcn169hutdqgc6tc5fa1x8qkr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1xYi CaeAptBT ukC5mI0KJ3BVYaIY2rIqEXqCOPOV8cWnplQdbO5zqmyjvIvJeN7lahtVrp/00CgKPXeemYPgnOqXn/RNjC7T0lmdxLU9yNs5gpN+Xt8xMxodGQaEE5VGiKKtjeJEygyI1mmwpT+h8we2yT1wtvf6AoPCo5qSMbX00LYsoPFCXemfLVXujctQJvOcyRzfVdBFNAn+LJciWAU9ar2h3xzIlssK4AfGs7lbil1YQ 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: List-Subscribe: List-Unsubscribe: This is a hotfix for a potential zpool memory leak that could result in the existing zswap_decompress(): mutex_unlock(&acomp_ctx->mutex); if (src != acomp_ctx->buffer) zpool_unmap_handle(zpool, entry->handle); Releasing the lock before the conditional does not protect the integrity of "src", which is set earlier under the acomp_ctx mutex lock. This poses a risk for the conditional behaving as intended, and consequently not unmapping the zpool handle, which could cause a zswap zpool memory leak. This patch moves the mutex_unlock() to occur after the conditional and subsequent zpool_unmap_handle(). This ensures that the value of "src" obtained earlier, with the mutex locked, does not change. Even though an actual memory leak was not observed, this fix seems like a cleaner implementation. Signed-off-by: Kanchana P Sridhar Fixes: 9c500835f279 ("mm: zswap: fix kernel BUG in sg_init_one") --- mm/zswap.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) base-commit: 0e5bdedb39ded767bff4c6184225578595cee98c diff --git a/mm/zswap.c b/mm/zswap.c index f6316b66fb23..58810fa8ff23 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -986,10 +986,11 @@ static void zswap_decompress(struct zswap_entry *entry, struct folio *folio) acomp_request_set_params(acomp_ctx->req, &input, &output, entry->length, PAGE_SIZE); BUG_ON(crypto_wait_req(crypto_acomp_decompress(acomp_ctx->req), &acomp_ctx->wait)); BUG_ON(acomp_ctx->req->dlen != PAGE_SIZE); - mutex_unlock(&acomp_ctx->mutex); if (src != acomp_ctx->buffer) zpool_unmap_handle(zpool, entry->handle); + + mutex_unlock(&acomp_ctx->mutex); } /*********************************