From patchwork Wed Aug 28 09:35:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kanchana P Sridhar X-Patchwork-Id: 13780970 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 104AFC5475C for ; Wed, 28 Aug 2024 09:35:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1B4B76B0096; Wed, 28 Aug 2024 05:35:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 117996B0098; Wed, 28 Aug 2024 05:35:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E5DD36B0099; Wed, 28 Aug 2024 05:35:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id B9ED46B0096 for ; Wed, 28 Aug 2024 05:35:24 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 6214E1C6CC7 for ; Wed, 28 Aug 2024 09:35:24 +0000 (UTC) X-FDA: 82501146168.30.8F91A1D Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) by imf06.hostedemail.com (Postfix) with ESMTP id 2BE2E180004 for ; Wed, 28 Aug 2024 09:35:21 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=fm7ONb5M; spf=pass (imf06.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.10 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=1724837653; 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=v3syzhnLtkmjwGHswwxuF3GJsIJt0yuSq8IHxuI0qUg=; b=t/DM03yFCxfMpr2PjfMoNxVPKkF6cdVVIGS3f6GB29RU44VL+dVPtjR2AxypO3MryMU0Qe IZfQc8CoehTIlrqvDQIpgtm8icdntVO2mU3ASadS1ty3VSNlOqJ8GRd9NGhcysp5T/vvne QIS9C5si7bDP0VFfV9UEphSA+Eq1X6A= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=fm7ONb5M; spf=pass (imf06.hostedemail.com: domain of kanchana.p.sridhar@intel.com designates 192.198.163.10 as permitted sender) smtp.mailfrom=kanchana.p.sridhar@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724837653; a=rsa-sha256; cv=none; b=re2hc1QNozv8r5kxR5nTuGlUykTadJIfrhbaFFXZD/uKyk5pcLR4/Vf02/ZCOcrbHIFxcR p8AxWpRZnxdaDmVDSDvbmK3xkRfrecqaLKfElLzC0lyADKjcdKsgOvTO/sGFWe+RzAsPXD d0khuokjKUO9Ga04KrR8lEJdepBOaTs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1724837722; x=1756373722; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=K3yroI8ejev2DdrGq2bi16BmrcYH0+k9ICg7i86HaWs=; b=fm7ONb5Ml56iq1nysix9O1w3YGuk6M4uZ9Z6BXtQODci7h5hxsLxkCRZ 6ps5E/Nih6tmYxMiVDwJ07Lm3gTWXiivIARuehnx1vprc1kGbit3Z9gAF 53WrJ1o7+ozbqBAxA9FBWPhlXUHnCcNUAor0cSEccXg+f9+cY705UapBY jzkbekmvc2Orh1TJvUH+nsBKRuvsSX5kx5HWTXMHg6w73U2ReKPncg+23 BDvQS+XRcFQgfjDgusizX2QaMMXyJMyq6dMynm37QXSnLcoLeRKHwnbAJ LXB3G6uRjK+/Y6fW6nqer8MIXZCrPmCPqXe+Nic8NwBzZbdls05l1vh/i Q==; X-CSE-ConnectionGUID: PeQYE9nHRoytbBT5NmbeKg== X-CSE-MsgGUID: t+1T0uEIQk+xY0myMDDERQ== X-IronPort-AV: E=McAfee;i="6700,10204,11177"; a="34763873" X-IronPort-AV: E=Sophos;i="6.10,182,1719903600"; d="scan'208";a="34763873" Received: from orviesa001.jf.intel.com ([10.64.159.141]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Aug 2024 02:35:17 -0700 X-CSE-ConnectionGUID: Bqk7Q+llS7+LZM2jckH8EQ== X-CSE-MsgGUID: eEaT/vxHTaq60gtZsRhdxg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.10,182,1719903600"; d="scan'208";a="100678975" Received: from jf5300-b11a338t.jf.intel.com ([10.242.51.6]) by orviesa001.jf.intel.com with ESMTP; 28 Aug 2024 02:35:16 -0700 From: Kanchana P Sridhar To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, hannes@cmpxchg.org, yosryahmed@google.com, nphamcs@gmail.com, ryan.roberts@arm.com, ying.huang@intel.com, 21cnbao@gmail.com, akpm@linux-foundation.org Cc: nanhai.zou@intel.com, wajdi.k.feghali@intel.com, vinodh.gopal@intel.com, kanchana.p.sridhar@intel.com Subject: [PATCH v5 2/3] mm: zswap: zswap_store() extended to handle mTHP folios. Date: Wed, 28 Aug 2024 02:35:15 -0700 Message-Id: <20240828093516.30228-3-kanchana.p.sridhar@intel.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20240828093516.30228-1-kanchana.p.sridhar@intel.com> References: <20240828093516.30228-1-kanchana.p.sridhar@intel.com> MIME-Version: 1.0 X-Stat-Signature: 9bwzajmse8taya5sa9bziw8t5rb6mdii X-Rspam-User: X-Rspamd-Queue-Id: 2BE2E180004 X-Rspamd-Server: rspam02 X-HE-Tag: 1724837721-150709 X-HE-Meta: U2FsdGVkX18+6ih8H1G2zukqRJ1Hhz127FVreXsPVvDDPBFcs8c4b0Ip/feSXP8PU8iVu7gTHEAwYOyHBT1Qknb6B6hjRKVs/2lvinHIy9WzkYM5lQkJ9gKZCe8ClgSFEBVqVryi8S+KjZfGeRNSNTkTIYJyRoeWoeKp9ELcgjbbMESPbuPDJkrjHW0N2cDRqDPXvcVIj6kNPByvV//546heDg8LjdrTGMpREhCD+2TCUBmwsIe4WvDBirhYGRFhIs+0D5zKTG1V1Nk00LWDOGxXqMMknl0x5tkgy22Iv+aSddFLql604KoZjcjMx014qx5fJeKL7STBHG5M8ytv2Ro/vjGHv9heJw7c0loQpNPW+NAlmCGZlRWr5SnwMnQPS3VIVTEJXWJy8fn/qnL72BeeSo9GxRYFbprV1phaIWer0BwqRoxatY/Fu4BWw1ABVwbaM/uK6nZhO6McYu3Y1bzoGjucIUcNT7I5v+3Ppstm0h+VzlBqRcluJ5W/W3FJSX4UUghgQBMZba2LR+99Kub91GuL5xhiHMH8ZtDqzk57Jbw1YW7TIXeWhsePthzXqdBiccFyQgadoIBse2hwlzgVzIeKF8kY9lz+khPraT8A4t9F+vVVeMmNiA8lC/0zIiLCPffkU+3NQgdK835e+jFaZQczdgGxyUXbOVoukEZnhYAo78giHRl81jZuiEwVl3Uh89zMMhBKZsjzSIryNqaeV1dPeWuYOcRKaASrudy8xUY38RR7YFi8yrtiA8Mz6GcGf4isLLns507LJZfHdT6/+LpYaYVbA2jZ/K5cfXZOs3zMB8TAte8h5/uB/8ZQs6mWxJgsH1FJQ3qaKXZLItV1PdXa4AGtdxiMkHD3PN+GcK+smjv6O+V+fqLDorFW/iODM5kN7CYzV3L/vB8575HCw1Ek0K2/abEp21QHUEJtBoQTQCqS+0jd2QdrXABs6QXdu/gmWlRF0Twc9Ux Cm4CD9Va tg+CjD/H+xyKRAdfw/5v27jS55ZB4lr7LYxVNUcTf947Ys2a3/vF8WiCvHQOzs8n5tz21F2k6KY5A/a1uYs+RL7h6kLmQ9PXYdrIZUQA2vMI6okFccIrDRNBUlTMzP+/qi9hR86JHokAqIjKA8iTJqETJHCb/DLjYfqcROoNrBvAGP1LivmKkPC0uGqWZ56YAaebF0hczvD80g0loQplc2o7zLJM7DrELrOJGWfaFqzR6CPzjJe5OMpcsJJ2pITEWh7QxXtv/bIRewCI9wSVJZRBqTbNahj7MQuNMOOcw+ZUWAUAnyJf92RnxE+p7fATlGBAoKCWQrvjnJZ5OzEaevWrSVvkA9+DHEPOj 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 now process and store mTHP and PMD-size THP folios. 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 This patch provides a sequential implementation of storing an mTHP in zswap_store() by iterating through each page in the folio to compress and store it in the zswap zpool. Towards this goal, zswap_compress() is modified to take a page instead of a folio as input. Each page's swap offset is stored as a separate zswap entry. If an error is encountered during the store of any page in the mTHP, all previous pages/entries stored will be invalidated. Thus, an mTHP is either entirely stored in ZSWAP, or entirely not stored in ZSWAP. This forms the basis for building batching of pages during zswap store of large folios, by compressing batches of up to say, 8 pages in an mTHP in parallel in hardware, with the Intel In-Memory Analytics Accelerator (Intel IAA). Also, addressed some of the RFC comments from the discussion in [1]. Made a minor edit in the comments for "struct zswap_entry" to delete the comments related to "value" since same-filled page handling has been removed from zswap. Co-developed-by: Ryan Roberts Signed-off-by: Signed-off-by: Kanchana P Sridhar --- mm/zswap.c | 231 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 170 insertions(+), 61 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index 449914ea9919..d6f012ca67d8 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -190,7 +190,6 @@ static struct shrinker *zswap_shrinker; * section for context. * pool - the zswap_pool the entry's data is in * handle - zpool allocation handle that stores the compressed page data - * value - value of the same-value filled pages which have same content * objcg - the obj_cgroup that the compressed memory is charged to * lru - handle to the pool's lru used to evict pages. */ @@ -876,7 +875,7 @@ static int zswap_cpu_comp_dead(unsigned int cpu, struct hlist_node *node) return 0; } -static bool zswap_compress(struct folio *folio, struct zswap_entry *entry) +static bool zswap_compress(struct page *page, struct zswap_entry *entry) { struct crypto_acomp_ctx *acomp_ctx; struct scatterlist input, output; @@ -894,7 +893,7 @@ static bool zswap_compress(struct folio *folio, struct zswap_entry *entry) dst = acomp_ctx->buffer; sg_init_table(&input, 1); - sg_set_folio(&input, folio, PAGE_SIZE, 0); + sg_set_page(&input, page, PAGE_SIZE, 0); /* * We need PAGE_SIZE * 2 here since there maybe over-compression case, @@ -1404,35 +1403,82 @@ static void shrink_worker(struct work_struct *w) /********************************* * main API **********************************/ -bool zswap_store(struct folio *folio) + +/* + * Returns true if the entry was successfully + * stored in the xarray, and false otherwise. + */ +static bool zswap_store_entry(struct xarray *tree, + struct zswap_entry *entry) { - 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; + struct zswap_entry *old; + pgoff_t offset = swp_offset(entry->swpentry); - VM_WARN_ON_ONCE(!folio_test_locked(folio)); - VM_WARN_ON_ONCE(!folio_test_swapcache(folio)); + old = xa_store(tree, offset, entry, GFP_KERNEL); - /* Large folios aren't supported */ - if (folio_test_large(folio)) + if (xa_is_err(old)) { + int err = xa_err(old); + + WARN_ONCE(err != -ENOMEM, "unexpected xarray error: %d\n", err); + zswap_reject_alloc_fail++; return false; + } - if (!zswap_enabled) - goto check_old; + /* + * We may have had an existing entry that became stale when + * the folio was redirtied and now the new version is being + * swapped out. Get rid of the old. + */ + if (old) + zswap_entry_free(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); + return true; +} + +/* + * 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. + * + * This is called after the store of the i-th offset in a large folio has + * failed. All zswap entries in the folio must be deleted. This helps make + * sure that a swapped-out mTHP is either entirely stored in zswap, or + * entirely not stored in zswap. + * + * This is also called if zswap_store() is invoked, but zswap is not enabled. + * All offsets for the folio are deleted from zswap in this case. + */ +static void zswap_delete_stored_offsets(struct xarray *tree, + pgoff_t offset, + long nr_pages) +{ + struct zswap_entry *entry; + long i; + + for (i = 0; i < nr_pages; ++i) { + entry = xa_erase(tree, offset + i); + if (entry) + zswap_entry_free(entry); } +} + +/* + * Stores the page at specified "index" in a folio. + */ +static bool zswap_store_page(struct folio *folio, long index, + struct obj_cgroup *objcg, + struct zswap_pool *pool) +{ + swp_entry_t swp = folio->swap; + int type = swp_type(swp); + pgoff_t offset = swp_offset(swp) + index; + struct page *page = folio_page(folio, index); + struct xarray *tree = swap_zswap_tree(swp); + struct zswap_entry *entry; + + if (objcg) + obj_cgroup_get(objcg); if (zswap_check_limits()) goto reject; @@ -1445,42 +1491,20 @@ bool zswap_store(struct folio *folio) } /* if entry is successfully added, it keeps the reference */ - entry->pool = zswap_pool_current_get(); - if (!entry->pool) + if (!zswap_pool_get(pool)) goto freepage; - 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); - } + entry->pool = pool; - if (!zswap_compress(folio, entry)) + if (!zswap_compress(page, entry)) goto put_pool; - entry->swpentry = swp; + entry->swpentry = swp_entry(type, offset); entry->objcg = objcg; entry->referenced = true; - old = xa_store(tree, offset, entry, GFP_KERNEL); - if (xa_is_err(old)) { - int err = xa_err(old); - - WARN_ONCE(err != -ENOMEM, "unexpected xarray error: %d\n", err); - zswap_reject_alloc_fail++; + if (!zswap_store_entry(tree, entry)) goto store_failed; - } - - /* - * We may have had an existing entry that became stale when - * the folio was redirtied and now the new version is being - * swapped out. Get rid of the old. - */ - if (old) - zswap_entry_free(old); if (objcg) { obj_cgroup_charge_zswap(objcg, entry->length); @@ -1511,23 +1535,108 @@ bool zswap_store(struct folio *folio) store_failed: zpool_free(entry->pool->zpool, entry->handle); put_pool: - zswap_pool_put(entry->pool); + zswap_pool_put(pool); freepage: zswap_entry_cache_free(entry); reject: obj_cgroup_put(objcg); if (zswap_pool_reached_full) queue_work(shrink_wq, &zswap_shrink_work); -check_old: + + return false; +} + +/* + * Modified to store mTHP folios. Each page in the mTHP will be compressed + * and stored sequentially. + */ +bool zswap_store(struct folio *folio) +{ + long nr_pages = folio_nr_pages(folio); + swp_entry_t swp = folio->swap; + pgoff_t offset = swp_offset(swp); + struct xarray *tree = swap_zswap_tree(swp); + struct obj_cgroup *objcg = NULL; + struct mem_cgroup *memcg = NULL; + struct zswap_pool *pool; + 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 reject; + /* - * 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. + * Check cgroup limits: + * + * The cgroup zswap limit check is done once at the beginning of an + * mTHP store, and not within zswap_store_page() for each page + * in the mTHP. We do however check the zswap pool limits at the + * start of zswap_store_page(). What this means is, the cgroup + * could go over the limits by at most (HPAGE_PMD_NR - 1) pages. + * However, the per-store-page zswap pool limits check should + * hopefully trigger the cgroup aware and zswap LRU aware global + * reclaim implemented in the shrinker. If this assumption holds, + * the cgroup exceeding the zswap limits could potentially be + * resolved before the next zswap_store, and if it is not, the next + * zswap_store would fail the cgroup zswap limit check at the start. */ - entry = xa_erase(tree, offset); - if (entry) - zswap_entry_free(entry); - return false; + 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); + } + + /* + * Store each page of the folio as a separate entry. If we fail to store + * a page, unwind by removing all the previous pages we stored. + */ + for (index = 0; index < nr_pages; ++index) { + if (!zswap_store_page(folio, index, objcg, pool)) + goto put_pool; + } + + ret = true; + +put_pool: + zswap_pool_put(pool); +put_objcg: + obj_cgroup_put(objcg); + if (zswap_pool_reached_full) + queue_work(shrink_wq, &zswap_shrink_work); +reject: + /* + * 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. + */ + if (!ret) + zswap_delete_stored_offsets(tree, offset, nr_pages); + + return ret; } bool zswap_load(struct folio *folio)