From patchwork Tue Oct 1 05:32:21 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: 13817486 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 0533CCEB2FD for ; Tue, 1 Oct 2024 05:37:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 155AE280050; Tue, 1 Oct 2024 01:37:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F34BD6B01FC; Tue, 1 Oct 2024 01:37:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CBE9C6B01AB; Tue, 1 Oct 2024 01:37:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id A9F856B0117 for ; Tue, 1 Oct 2024 01:37:16 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 6529E1607A0 for ; Tue, 1 Oct 2024 05:37:16 +0000 (UTC) X-FDA: 82623925272.01.C9E52B0 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) by imf20.hostedemail.com (Postfix) with ESMTP id 46CC51C0008 for ; Tue, 1 Oct 2024 05:37:14 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=lrEDS9CS; spf=pass (imf20.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.11 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=1727760907; 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=8zMVJkZztE4XRAtOofTluJRZbGUYTdnwE6zdaFu+s54=; b=ZP/U6XrLG5pBnhfCMu6nu53e/Yqhtr3H9GcasEBrlG0DPY6H6QNlXiAGXAMAmjVZR7bq6J BilE+EGU9IvEFMgmMnHQHTp7G5dqwLnQrCfezVlBScNmFLclr5CUCu5dpPKpMPFNL5BpdT rvTdFfQkoB3R+E4DBDWMig13stnJCzg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1727760907; a=rsa-sha256; cv=none; b=i56uo53LoKIIIKHXeVmxvXZaz6sOYkkYHloTvsSWcS8DOZXPtk8cOubSOgPK0nLdj2G714 nOtTMWVREJe3i2vaQAqXIUCO3AFgQAbWQIMdOEwU8rCENQ7bPkUaxtm9PdPyX/Qa1b1F1i KeBWw+c14OhaV0k79OZMbADuAQIifek= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=lrEDS9CS; spf=pass (imf20.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.11 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=1727761034; x=1759297034; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ZqA5lLeXDY0x6sdJphTljhIJY7azBNvVtOD2DnEXQcU=; b=lrEDS9CSOLxMjvD/+R+Nc2PhlUWE7qGxvYYVD1N9GfzMDlTnMlGkx1DI +inlceYXkGbXyxFLaIxjXsgbRWqkMTBHOmZ0ObAp4o8hQ8r4lFzN1Ac/A +IJ19GFDoC+hDtUcBr8NOqT080vOoyrm10xg36jXd6VAoc1GyU1/lgA77 locj4Sor86YPkr2JT3PAYef6iO80sRCqgw5sdkztS28+OpuZeFVgA4Nq/ MjOsr9m9O1X6TVhY+c+mZ04eGN9cxnfwL/xWark6ZXlTLoGI+vVjb6UTW 7m1o9ko1miCqNSuvhcC89R008ljegaZoie21JVggYBP5B+QM5fnG0qF6i g==; X-CSE-ConnectionGUID: 3NtlLURYQZW+rS2q/ZoIJg== X-CSE-MsgGUID: vRJfCd3FTuKxoe22fkR5ig== X-IronPort-AV: E=McAfee;i="6700,10204,11211"; a="37465130" X-IronPort-AV: E=Sophos;i="6.11,167,1725346800"; d="scan'208";a="37465130" Received: from fmviesa007.fm.intel.com ([10.60.135.147]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Sep 2024 22:32:27 -0700 X-CSE-ConnectionGUID: fGhzmQz2RFu8X+pw/8tNSA== X-CSE-MsgGUID: 7dEULFhAQP6ZmQjTLj2l8A== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,167,1725346800"; d="scan'208";a="73205815" Received: from jf5300-b11a338t.jf.intel.com ([10.242.51.6]) by fmviesa007.fm.intel.com with ESMTP; 30 Sep 2024 22:32:26 -0700 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, shakeel.butt@linux.dev, ryan.roberts@arm.com, ying.huang@intel.com, 21cnbao@gmail.com, akpm@linux-foundation.org, willy@infradead.org Cc: nanhai.zou@intel.com, wajdi.k.feghali@intel.com, vinodh.gopal@intel.com, kanchana.p.sridhar@intel.com Subject: [PATCH v10 6/7] mm: zswap: Support large folios in zswap_store(). Date: Mon, 30 Sep 2024 22:32:21 -0700 Message-Id: <20241001053222.6944-7-kanchana.p.sridhar@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20241001053222.6944-1-kanchana.p.sridhar@intel.com> References: <20241001053222.6944-1-kanchana.p.sridhar@intel.com> MIME-Version: 1.0 X-Stat-Signature: hcyahjca15hata4rderj9np1u9efbgph X-Rspamd-Queue-Id: 46CC51C0008 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1727761034-548274 X-HE-Meta: U2FsdGVkX1/R54yqq7Ap0xqgcyfO4flDDbInRnqdh6e8CyGj8QutN99cqxPyodIF4VopSPx3dD0q8/Sre7gZZkjYCr7T9SStx3EHGwkGnLSABbc44ZLKLQ2u7Y9KSCosxF6zd+9ZqJeehCSOxWL+IXS1VvMjntgSXNHRodlHsF4/AYypwEJVnokgI8dHsKgf4NtD5SXuNjki6nDAAZOhU0bQIkrmlSYkgfXZmGp8djXmYaUXBKr43OwaEIrGWvlcL5LFVgTsTdIiNxT4YJaE/oBi78ZIKnXi2HZHZ9JdM7HIhQ+ZVax8+g2r33g6b4jpi3phOwc+i3ws+J0oK74MObR6oJ1AHa/aP6mNctvl/G+suChYg2tDjhQbZUZXS0xeS6X8YLYzbS+X9EL9enDsGcsCrW/ENIuhm/SVCHXeWkxYuZQRvgzjp8MWbi2NI1Ddo9OheOQaDNrWSmfun3PK+AXBMKEgH2EwCxbE3KeXjLQQEPLyDLNucgeIMlf6Lv5uX8V6f8XD4wFDdt+i5hT0foijeH0g/KrN1db165qur/EXzOOvALAb0Bf3NtFBis9M8oK35rp2cj8817fO0LD8hgOzu+G5+QysZ3X+DpOPsCthV0i8NUQCZvt0Da2glVDEuZU+oeU3Zg0VaU5Tp7UWJKfFHefqnmavWqmQDmT4xr1tqIBapIVLJKBadP7Xnsq9rRUd2pGqOZxxidjB4xcFarVrJ7zqsMIQTd7HFFET4eQOXqLV47OzK0fo3PgdQpChBSM1fTSE6f+okz/smQzA2PLIYUm04q3DbBBuQ58IBe9y0mjuTJHBAWZmChuvm0u8fBnOJosF6Ub5og9vbVr3nsIQBF0/RBSEe2RguTTUqNG0YcCG6Y4bMB0UnyIKrSen7iagaB7i5bZh1lfTDq0kR3ITzXJsyumQBoZrZP7dKLnXe3Blh4Do53PjfnpdsKtxX48RqyYGROyl6IMvoG6 HKTXkomE UywJbg0u7LoXlS0PMV0bJPUtsLGDEbwv4dIg7+ZOJsk1mM+xf2uIWkGhVPHIXVrhEhP16cIK+np+W97/sCgbqRyx4bS/FPK7cSi3nVfZN/1EQU2Y7z7Z1CNkL/XYUw29fri1JkZ9jq/d0OWneP2eVk4YAR4t3xgcasovD0myU4ByN/tyVbm+iLW74abfPWiFpxwu6NRq9H+y2bkG/9GQ3uvDjyw6OBui8TA3nzvuFOzGCAvw6/b7Sami5eu7VvPx/ebnlEV9lutMPRbn23NiD8dEvrgl0ALQThGOjAjCIFwdFkaFIchZCehKjYQ== 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: zswap_store() will store large folios by compressing them page by page. This patch provides a sequential implementation of storing a large folio in zswap_store() by iterating through each page in the folio to compress and store it in the zswap zpool. zswap_store() calls the newly added zswap_store_page() function for each page in the folio. zswap_store_page() handles compressing and storing each page. We check the global and per-cgroup limits once at the beginning of zswap_store(), and only check that the limit is not reached yet. This is racy and inaccurate, but it should be sufficient for now. We also obtain initial references to the relevant objcg and pool to guarantee that subsequent references can be acquired by zswap_store_page(). A new function zswap_pool_get() is added to facilitate this. If these one-time checks pass, we compress the pages of the folio, while maintaining a running count of compressed bytes for all the folio's pages. If all pages are successfully compressed and stored, we do the cgroup zswap charging with the total compressed bytes, and batch update the zswap_stored_pages atomic/zswpout event stats with folio_nr_pages() once, before returning from zswap_store(). If an error is encountered during the store of any page in the folio, all pages in that folio currently stored in zswap will be invalidated. Thus, a folio is either entirely stored in zswap, or entirely not stored in zswap. The most important value provided by this patch is it enables swapping out large folios to zswap without splitting them. Furthermore, it batches some operations while doing so (cgroup charging, stats updates). This patch also forms the basis for building compress batching of pages in a large folio in zswap_store() by compressing up to say, 8 pages of the folio in parallel in hardware using the Intel In-Memory Analytics Accelerator (Intel IAA). This change reuses and adapts the functionality in Ryan Roberts' RFC patch [1]: "[RFC,v1] mm: zswap: Store large folios without splitting" [1] https://lore.kernel.org/linux-mm/20231019110543.3284654-1-ryan.roberts@arm.com/T/#u Co-developed-by: Ryan Roberts Signed-off-by: Signed-off-by: Kanchana P Sridhar Acked-by: Johannes Weiner Reviewed-by: Nhat Pham Acked-by: Yosry Ahmed --- mm/zswap.c | 189 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 121 insertions(+), 68 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 2b8da50f6322..09aaf70f95c6 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -411,6 +411,12 @@ static int __must_check zswap_pool_tryget(struct zswap_pool *pool) return percpu_ref_tryget(&pool->ref); } +/* The caller must already have a reference. */ +static void zswap_pool_get(struct zswap_pool *pool) +{ + percpu_ref_get(&pool->ref); +} + static void zswap_pool_put(struct zswap_pool *pool) { percpu_ref_put(&pool->ref); @@ -1402,68 +1408,38 @@ static void shrink_worker(struct work_struct *w) /********************************* * main API **********************************/ -bool zswap_store(struct folio *folio) + +static ssize_t zswap_store_page(struct page *page, + struct obj_cgroup *objcg, + struct zswap_pool *pool) { - swp_entry_t swp = folio->swap; - pgoff_t offset = swp_offset(swp); - struct xarray *tree = swap_zswap_tree(swp); struct zswap_entry *entry, *old; - struct obj_cgroup *objcg = NULL; - struct mem_cgroup *memcg = NULL; - - VM_WARN_ON_ONCE(!folio_test_locked(folio)); - VM_WARN_ON_ONCE(!folio_test_swapcache(folio)); - - /* Large folios aren't supported */ - if (folio_test_large(folio)) - return false; - - if (!zswap_enabled) - goto check_old; - - /* Check cgroup limits */ - objcg = get_obj_cgroup_from_folio(folio); - if (objcg && !obj_cgroup_may_zswap(objcg)) { - memcg = get_mem_cgroup_from_objcg(objcg); - if (shrink_memcg(memcg)) { - mem_cgroup_put(memcg); - goto reject; - } - mem_cgroup_put(memcg); - } - - if (zswap_check_limits()) - goto reject; /* allocate entry */ - entry = zswap_entry_cache_alloc(GFP_KERNEL, folio_nid(folio)); + entry = zswap_entry_cache_alloc(GFP_KERNEL, page_to_nid(page)); if (!entry) { zswap_reject_kmemcache_fail++; goto reject; } - /* if entry is successfully added, it keeps the reference */ - entry->pool = zswap_pool_current_get(); - if (!entry->pool) - goto freepage; + /* zswap_store() already holds a ref on 'objcg' and 'pool' */ + if (objcg) + obj_cgroup_get(objcg); + zswap_pool_get(pool); - if (objcg) { - memcg = get_mem_cgroup_from_objcg(objcg); - if (memcg_list_lru_alloc(memcg, &zswap_list_lru, GFP_KERNEL)) { - mem_cgroup_put(memcg); - goto put_pool; - } - mem_cgroup_put(memcg); - } + /* if entry is successfully added, it keeps the reference */ + entry->pool = pool; - if (!zswap_compress(&folio->page, entry)) - goto put_pool; + if (!zswap_compress(page, entry)) + goto put_pool_objcg; - entry->swpentry = swp; + entry->swpentry = page_swap_entry(page); entry->objcg = objcg; entry->referenced = true; - old = xa_store(tree, offset, entry, GFP_KERNEL); + old = xa_store(swap_zswap_tree(entry->swpentry), + swp_offset(entry->swpentry), + entry, GFP_KERNEL); if (xa_is_err(old)) { int err = xa_err(old); @@ -1480,11 +1456,6 @@ bool zswap_store(struct folio *folio) if (old) zswap_entry_free(old); - if (objcg) { - obj_cgroup_charge_zswap(objcg, entry->length); - count_objcg_events(objcg, ZSWPOUT, 1); - } - /* * We finish initializing the entry while it's already in xarray. * This is safe because: @@ -1500,32 +1471,114 @@ bool zswap_store(struct folio *folio) zswap_lru_add(&zswap_list_lru, entry); } - /* update stats */ - atomic_long_inc(&zswap_stored_pages); - count_vm_event(ZSWPOUT); - - return true; + /* + * We shouldn't have any possibility of failure after the entry is + * added in the xarray. The pool/objcg refs obtained here will only + * be dropped if/when zswap_entry_free() gets called. + */ + return entry->length; store_failed: zpool_free(entry->pool->zpool, entry->handle); -put_pool: - zswap_pool_put(entry->pool); -freepage: +put_pool_objcg: + zswap_pool_put(pool); + obj_cgroup_put(objcg); zswap_entry_cache_free(entry); reject: + return -EINVAL; +} + +bool zswap_store(struct folio *folio) +{ + long nr_pages = folio_nr_pages(folio); + swp_entry_t swp = folio->swap; + struct obj_cgroup *objcg = NULL; + struct mem_cgroup *memcg = NULL; + struct zswap_pool *pool; + size_t compressed_bytes = 0; + bool ret = false; + long index; + + VM_WARN_ON_ONCE(!folio_test_locked(folio)); + VM_WARN_ON_ONCE(!folio_test_swapcache(folio)); + + if (!zswap_enabled) + goto check_old; + + objcg = get_obj_cgroup_from_folio(folio); + if (objcg && !obj_cgroup_may_zswap(objcg)) { + memcg = get_mem_cgroup_from_objcg(objcg); + if (shrink_memcg(memcg)) { + mem_cgroup_put(memcg); + goto put_objcg; + } + mem_cgroup_put(memcg); + } + + if (zswap_check_limits()) + goto put_objcg; + + pool = zswap_pool_current_get(); + if (!pool) + goto put_objcg; + + if (objcg) { + memcg = get_mem_cgroup_from_objcg(objcg); + if (memcg_list_lru_alloc(memcg, &zswap_list_lru, GFP_KERNEL)) { + mem_cgroup_put(memcg); + goto put_pool; + } + mem_cgroup_put(memcg); + } + + for (index = 0; index < nr_pages; ++index) { + struct page *page = folio_page(folio, index); + ssize_t bytes; + + bytes = zswap_store_page(page, objcg, pool); + if (bytes < 0) + goto put_pool; + compressed_bytes += bytes; + } + + if (objcg) { + obj_cgroup_charge_zswap(objcg, compressed_bytes); + count_objcg_events(objcg, ZSWPOUT, nr_pages); + } + + atomic_long_add(nr_pages, &zswap_stored_pages); + count_vm_events(ZSWPOUT, nr_pages); + + ret = true; + +put_pool: + zswap_pool_put(pool); +put_objcg: obj_cgroup_put(objcg); - if (zswap_pool_reached_full) + if (!ret && zswap_pool_reached_full) queue_work(shrink_wq, &zswap_shrink_work); check_old: /* - * If the zswap store fails or zswap is disabled, we must invalidate the - * possibly stale entry which was previously stored at this offset. - * Otherwise, writeback could overwrite the new data in the swapfile. + * If the zswap store fails or zswap is disabled, we must invalidate + * the possibly stale entries which were previously stored at the + * offsets corresponding to each page of the folio. Otherwise, + * writeback could overwrite the new data in the swapfile. */ - entry = xa_erase(tree, offset); - if (entry) - zswap_entry_free(entry); - return false; + if (!ret) { + unsigned type = swp_type(swp); + pgoff_t offset = swp_offset(swp); + struct zswap_entry *entry; + struct xarray *tree; + + for (index = 0; index < nr_pages; ++index) { + tree = swap_zswap_tree(swp_entry(type, offset + index)); + entry = xa_erase(tree, offset + index); + if (entry) + zswap_entry_free(entry); + } + } + + return ret; } bool zswap_load(struct folio *folio)