From patchwork Wed Nov 6 19:21:00 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: 13865323 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 10686D59F6C for ; Wed, 6 Nov 2024 19:21:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A179C6B0099; Wed, 6 Nov 2024 14:21:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9BD356B009A; Wed, 6 Nov 2024 14:21:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 810526B009B; Wed, 6 Nov 2024 14:21:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 5EE636B0099 for ; Wed, 6 Nov 2024 14:21:18 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 24AEC1A06CF for ; Wed, 6 Nov 2024 19:21:18 +0000 (UTC) X-FDA: 82756637292.25.B561C6F Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.11]) by imf23.hostedemail.com (Postfix) with ESMTP id 5A11C14001D for ; Wed, 6 Nov 2024 19:20:53 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=UiPFPSU8; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf23.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 198.175.65.11 as permitted sender) smtp.mailfrom=kanchana.p.sridhar@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730920816; a=rsa-sha256; cv=none; b=UqTivA0NFvyMW6tVSMQLiXyKbmX/oNC3N+dXHS+ymEupzPWN+z5OgQBnl/T+uK00eUJmd0 bfSiJL4hUMiwkIEuK69GTpmxCT+iy6NrQD4ZrOukdNM5SzWuhNCxhGopz7Zn6aJ8WeHlfX /1WRz2bidZghFclJ37I+sCwTN7Efq6k= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=UiPFPSU8; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf23.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 198.175.65.11 as permitted sender) smtp.mailfrom=kanchana.p.sridhar@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730920816; 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:in-reply-to:references:references:dkim-signature; bh=Qxz8u9Shn/SiPqBxAwwlyMbQX8CsM2ppl302cd3vziA=; b=Fjmnjp/pODocZihziSdRXM6WnrwYTTJhda012ITsGc3dax+jOfowqcpjY1GSuN1Z+SXmND aVNf5OHRqw4/bvG0ykBL44vu7ORh6zjfnp0FnTBcd6KdTGc+ai8mblDKQ12C6YrCvqeBBR HfMyx+ERE+3/MvGDYuGCZUIru3f/qmY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1730920876; x=1762456876; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=e3w3jsrwSRn0skEK+1OKRkuNEeuFjmM/VVAG1YViexY=; b=UiPFPSU8Ov7UOiS7tp2v9uaefhC5/EiITiGp9//V/RUpgtVv3Ec3d4Ai hURV99CwD/6cLIFQ/lLiThGLkaJ0yTa5lFGKfZO3HNruJDFNdePC1aRmh rzwYzetNzEy2QDF/R+3iWu3ewoE5UcZEcpMcussE8VAtMwVO+WJcnZYBb T0w6PesVK+txE8bTmpSK/zhlDO3+9LqeIouxqnuMHSpJG1QfxgGqlPXMt KHpiCn/DAtTrYp38pSW+BwQlKyGfuxz8AVh7js8XWjzYEocHHIYrMBGlv TXBj9X31XwrVmE58g8jfQQPVw7h5K9rlV9QIQANg2z/Qah0dAtbRzwnMs w==; X-CSE-ConnectionGUID: W3CMmoTWRgyA9rHSDa2tVw== X-CSE-MsgGUID: g6YagjqxQ1anNsUVUgMtnA== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="41292024" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="41292024" Received: from fmviesa003.fm.intel.com ([10.60.135.143]) by orvoesa103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Nov 2024 11:21:12 -0800 X-CSE-ConnectionGUID: JuZp7wgeTZupSYi8bw+nGA== X-CSE-MsgGUID: /kRNAQktRwSLiBLyCdU2qg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,263,1725346800"; d="scan'208";a="88695402" Received: from unknown (HELO JF5300-B11A338T.jf.intel.com) ([10.242.51.115]) by fmviesa003.fm.intel.com with ESMTP; 06 Nov 2024 11:21:11 -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, linux-crypto@vger.kernel.org, herbert@gondor.apana.org.au, davem@davemloft.net, clabbe@baylibre.com, ardb@kernel.org, ebiggers@google.com, surenb@google.com, kristen.c.accardi@intel.com, zanussi@kernel.org Cc: wajdi.k.feghali@intel.com, vinodh.gopal@intel.com, kanchana.p.sridhar@intel.com Subject: [PATCH v3 08/13] mm: zswap: acomp_ctx mutex lock/unlock optimizations. Date: Wed, 6 Nov 2024 11:21:00 -0800 Message-Id: <20241106192105.6731-9-kanchana.p.sridhar@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20241106192105.6731-1-kanchana.p.sridhar@intel.com> References: <20241106192105.6731-1-kanchana.p.sridhar@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 5A11C14001D X-Rspamd-Server: rspam11 X-Stat-Signature: kzwhnmpr6bm5zmst9zxkxkedgj33g8gu X-HE-Tag: 1730920853-584332 X-HE-Meta: U2FsdGVkX1/nKCcqmgIO7eDDPWZ/rPdVeOLalcVN04i06hJobhpgkVb92mZHnmUxvHfl5kPU7yF18OqjKQCgx2HI7xhh2JJeU+xhczmcvNGWpIybYpYkoX73b6tPwHKGse1kwjIhg7Y6+0YdOTUFh36GjVGsuVZUOJHqvmNkzMTrNpWFxRna6Ux4meKB1avmMY7ZA+HdigWK+NmidPgdCOUsCNP4kZA//VSWOtJdDvxPY6CIOYvcnDXEV//51RXhHZ5mXp6NqcxatGB90OCulkoCjEnkXl4DupJp0qlenLEi2/kCbPLXApFSjt3aVwEhjJ2ZsxxxNDfmbgonDZwqUdylgMnCqVZ1D8uPWxQHBiatB40ihQDyiq/mY0MmcfO19AtgVCzNifclEy5Qzmo5D+S/oUJyKJt+9KbKpa2wD3FqXfQ8L0/Yh3CAQWF4yb+uWTIlLH7B34IBGw10oH5hVhwLoUAqFdY7P3bcDNLvVsgnC2k0rNHt7GjF0yMS5Vnk7ki7/vVBfhYHexgayoceug8IXSB9XiLuGxrDCD6Wv/g+I9YbtvKwHoOQhPZ/SjsQ4b8ycMacbZcXKwYXPrSblLj6yXfOvynvvisNk9546UjRsp1JzRKS1/bv+mDmL+rTt0Qd0mv41Z38cViYw96xm0wCgYMzMggwt7VndWmterhHJkqkfRVU4XX/rSk2tr9URkmGpI5hoNEnWrSI56XxALXw3m+EPnhXsZO6m2N0zPFNBFJnC3h7952UGT4LV22sood+DNqsXk5tdE1sAlR2ibYaOtX3oCcqlFVhttSwkSX3gS7auLzCpI49CP+vgbgXDMXzeNMupHZCWJGjpDFa7QSbUvitOtfZZwCTw0alEvPN7XEvPDwFmu/8nZA53uFfdNwi3Y9rNzAwPJ2R7Fviv9otIwpZ1hbXJe1eh34rKYN3Yw96vq541xawcyqMk0vZ/I9Mj88fWT96owgUxfx 0MrI+NmI dmcOvG+YmI8UjDusCsuEW6BUaKNNJ3AN9yrQuQhZnr/h7g8xUFf5IhlBT1XjDXYLjC8ek+PP9s1rR8vOhkmZZoSw2fmA7X5Zu7oFBlNiRhVRkAirdFBsf0pqp86TXoDnASEzi6VWDYlCw3JotZLZ9fcyVmGHYid5RYKFx09axd4mhIbTVGT0jd2ap+6DhQlL1Oba55u7e+Re4OR8vihFu+ifOSZGeGjCe92Z7L+zhKFAPR64Sxz5CYTWc25w1BCV1BxwrHohgNIwA8+E= 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 patch implements two changes with respect to the acomp_ctx mutex lock: 1) The mutex lock is not acquired/released in zswap_compress(). Instead, zswap_store() acquires the mutex lock once before compressing each page in a large folio, and releases the lock once all pages in the folio have been compressed. This should reduce some compute cycles in case of large folio stores. 2) In zswap_decompress(), the mutex lock is released after the conditional zpool_unmap_handle() based on "src != acomp_ctx->buffer" rather than before. This ensures that the value of "src" obtained earlier does not change. If the mutex lock is released before the comparison of "src" it is possible that another call to reclaim by the same process could obtain the mutex lock and over-write the value of "src". Signed-off-by: Kanchana P Sridhar --- mm/zswap.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index f6316b66fb23..3e899fa61445 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -880,6 +880,9 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) return 0; } +/* + * The acomp_ctx->mutex must be locked/unlocked in the calling procedure. + */ static bool zswap_compress(struct page *page, struct zswap_entry *entry, struct zswap_pool *pool) { @@ -895,8 +898,6 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, acomp_ctx = raw_cpu_ptr(pool->acomp_ctx); - mutex_lock(&acomp_ctx->mutex); - dst = acomp_ctx->buffer; sg_init_table(&input, 1); sg_set_page(&input, page, PAGE_SIZE, 0); @@ -949,7 +950,6 @@ static bool zswap_compress(struct page *page, struct zswap_entry *entry, else if (alloc_ret) zswap_reject_alloc_fail++; - mutex_unlock(&acomp_ctx->mutex); return comp_ret == 0 && alloc_ret == 0; } @@ -986,10 +986,16 @@ 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); + + /* + * It is safer to unlock the mutex after the check for + * "src != acomp_ctx->buffer" so that the value of "src" + * does not change. + */ + mutex_unlock(&acomp_ctx->mutex); } /********************************* @@ -1487,6 +1493,7 @@ bool zswap_store(struct folio *folio) { long nr_pages = folio_nr_pages(folio); swp_entry_t swp = folio->swap; + struct crypto_acomp_ctx *acomp_ctx; struct obj_cgroup *objcg = NULL; struct mem_cgroup *memcg = NULL; struct zswap_pool *pool; @@ -1526,6 +1533,9 @@ bool zswap_store(struct folio *folio) mem_cgroup_put(memcg); } + acomp_ctx = raw_cpu_ptr(pool->acomp_ctx); + mutex_lock(&acomp_ctx->mutex); + for (index = 0; index < nr_pages; ++index) { struct page *page = folio_page(folio, index); ssize_t bytes; @@ -1547,6 +1557,7 @@ bool zswap_store(struct folio *folio) ret = true; put_pool: + mutex_unlock(&acomp_ctx->mutex); zswap_pool_put(pool); put_objcg: obj_cgroup_put(objcg);