From patchwork Thu Jun 27 10:55:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usama Arif X-Patchwork-Id: 13714184 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 D2255C30653 for ; Thu, 27 Jun 2024 10:57:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 58D7A6B00A4; Thu, 27 Jun 2024 06:57:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5130C6B00A3; Thu, 27 Jun 2024 06:57:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 366276B00A6; Thu, 27 Jun 2024 06:57:38 -0400 (EDT) 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 112CE6B00A3 for ; Thu, 27 Jun 2024 06:57:38 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B09C9C111B for ; Thu, 27 Jun 2024 10:57:37 +0000 (UTC) X-FDA: 82276367754.24.01AB2C1 Received: from mail-qt1-f170.google.com (mail-qt1-f170.google.com [209.85.160.170]) by imf04.hostedemail.com (Postfix) with ESMTP id D919740009 for ; Thu, 27 Jun 2024 10:57:34 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=eaDk2xbO; spf=pass (imf04.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.160.170 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719485838; 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=TkzTW/11bPuGoCkFDaajiq8WxKpIkXClRk/0ynrGzvk=; b=RoQk7giUMm12A/kIMFQ1KN3fr3k+vp0Jsu3O+2jyCS5jXQ2qGQscnZn5zDNVIsifZtC0V6 SNTR0uz6lVBlZiuZOh9nQ1ONxk9wAqgov+FkhEpICSYguoHvOIT/f4nQpQmP/COKWxfI/X faAgdgjQhcT0jqAAVTu6uSzAS7jzkIE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719485838; a=rsa-sha256; cv=none; b=SiK5PuBeH4QZosL1yP1Bu+vUhzlOVhvV80EX//ozZG0Ibh/fD0QEtZtsKWCOPZz10f9iUq xdBR4+jqFf89NYJwEGofyyv1VPYidH08B7UZrQLG99hi2gCzpIclIMRoiVdlbOGLRTs3cq 8yse+0kWRkjfl1/xxzXFnnkFgITd3fQ= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=eaDk2xbO; spf=pass (imf04.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.160.170 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-qt1-f170.google.com with SMTP id d75a77b69052e-44054a2c153so34753371cf.2 for ; Thu, 27 Jun 2024 03:57:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719485854; x=1720090654; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TkzTW/11bPuGoCkFDaajiq8WxKpIkXClRk/0ynrGzvk=; b=eaDk2xbOZvcwcD4uDJ1tuDqSxTxQGPMVcs2nhPD6+S65mrgmRItATF7na7fLhbPICz I6p4O+5ymupJK8LmjuIQyrXFrp60Ny6ZMu8bFLFURjtPfD416Y2HYm807rrUqIQAeC1B 2w2fddXyQRSx/e+QWvKH6i/E2zIaD81mphrWxMlPwz63eUf2Ch6YcM5c/s0y6yxnzeik 7sHfRpvYrvovcN69PcOWF3AgU7uAzJH5fzYok0/QBonTaU+vMzRsW2P+RaxMIe/w1cQD yapRiWoDx7EzrK3Z4i4wMBppRMv1xX1sFdkTudehx3mucnvzYbAMBT++SBR28q4cph9r jllQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719485854; x=1720090654; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TkzTW/11bPuGoCkFDaajiq8WxKpIkXClRk/0ynrGzvk=; b=bhlgvJ0bDtRokAOTxR16ddMu0HslKqimyGWF05bqfko027Rqz/wJ3GssJWWilyhwDi h8jfwSkpci16AmPXK0XxFUd1WG2dYlwntyvQmPzdBnFO9URH47Kg7N2yIm06244z8O8f GYdbdExK/TESzzADQTSAu5SSFQJPxiwuHjBlE0FUpXfIx8ML1u3SkoGR7NSNIqVkeuQO apaDNdwG+z3z8/LPyw/RDO0eIdvlNcU2jIG+nK+6x6zLT3YMfz+rhv1y+rINwZNq6ywR 9N130pUi13gmw1ZuUGhiXrJ1ua+jdnZghngSo5/3A7O8DI15C+tvd/Xve2Ilx7UF0ayk o3Ww== X-Forwarded-Encrypted: i=1; AJvYcCWsjtgNobTNOmP/MLdwWV1dg7xwzqHmmEd2i+AxMyiT88UJeXJpjd34IEgne4j70CHTeC75VQAg7IXqwMloedpqGgI= X-Gm-Message-State: AOJu0Yxum3dvL1ypAc+OV3bQQ/I84znl65//EPfr4gHBImILIX/RSdMj a7XOcAH4i2WGcdfBc+B9I2F0zi16TN0/QM1UfP6fHJSuK+ku3Vbo X-Google-Smtp-Source: AGHT+IG1BjfEOxsFo5R+ptwaWh5pstieIxvFw0KT+PkhQXtF7aARv9WfbDIto8OWrrisoVsji9V5dA== X-Received: by 2002:a05:622a:15d3:b0:445:5ae:ba84 with SMTP id d75a77b69052e-44505aec008mr64953341cf.15.1719485853832; Thu, 27 Jun 2024 03:57:33 -0700 (PDT) Received: from localhost (fwdproxy-ash-004.fbsv.net. [2a03:2880:20ff:4::face:b00c]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-44641eb4699sm4796381cf.38.2024.06.27.03.57.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jun 2024 03:57:33 -0700 (PDT) From: Usama Arif To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, shakeel.butt@linux.dev, david@redhat.com, ying.huang@intel.com, hughd@google.com, willy@infradead.org, yosryahmed@google.com, nphamcs@gmail.com, chengming.zhou@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com, Usama Arif , Andi Kleen Subject: [PATCH v7 1/2] mm: store zero pages to be swapped out in a bitmap Date: Thu, 27 Jun 2024 11:55:29 +0100 Message-ID: <20240627105730.3110705-2-usamaarif642@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240627105730.3110705-1-usamaarif642@gmail.com> References: <20240627105730.3110705-1-usamaarif642@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: D919740009 X-Stat-Signature: 3ybtz5oqgireiaxe4t3by7bn4umo3ene X-HE-Tag: 1719485854-3112 X-HE-Meta: U2FsdGVkX18xa3pfCt3KSNvI1dOVWLUDjeNw4vdPKpwnHbE2CcSkO9fSYaT90LlZYmaXHjqXXkba2MJbnYh6VJTnFQaLF9z+cpCU2VJmfROUxRCrjdDq4tSJ7ATCV7N1eelaMtE2tW6BWDQmT3TzCxdIvn36P0dPyUhBhq9vxGzZczv0SsLKFBDUFDq8XH9b7MQISDabDyDo0n8fc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KfWy0s4B+F9ncjeblixXQQDrQCagCuH/1Ad7BQJ0wE0cSEcpGSSAbFnxpRiraw0ddi/62YvlXEMePRK6HkiqJaO01gMKq55qSoFUEMe/9ICJKsAvrxhVs3S0wXf7qObZnSBnCKLnAFcEo34PBX5qPJwJ0z9tHHmYLchwfsUQb0KioKbVD7AW8jxSnbjk6G9LCyoIt/Ll1W4BA/tJY395Tt7IVmD1/swDJCcZ0d3qIaTE9sBzpSUJXitOZv27Ihnx8wQqo9+tPiNiglyYImDOif+stB2t9UVM4EX62xMyoAJBvmxydAMsniAdNKcF70rmdPQZ9Gih4AgrbvXI+yvO8cL3qPN7UwvOcSyIyfzx1A8TZu1SL7U= 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: Approximately 10-20% of pages to be swapped out are zero pages [1]. Rather than reading/writing these pages to flash resulting in increased I/O and flash wear, a bitmap can be used to mark these pages as zero at write time, and the pages can be filled at read time if the bit corresponding to the page is set. With this patch, NVMe writes in Meta server fleet decreased by almost 10% with conventional swap setup (zswap disabled). [1] https://lore.kernel.org/all/20171018104832epcms5p1b2232e2236258de3d03d1344dde9fce0@epcms5p1/ Signed-off-by: Usama Arif Reviewed-by: Chengming Zhou Reviewed-by: Yosry Ahmed Reviewed-by: Nhat Pham Cc: David Hildenbrand Cc: "Huang, Ying" Cc: Hugh Dickins Cc: Johannes Weiner Cc: Matthew Wilcox (Oracle) Cc: Shakeel Butt Cc: Usama Arif Cc: Andi Kleen Signed-off-by: Andrew Morton Acked-by: Johannes Weiner Signed-off-by: Usama Arif Reviewed-by: Chengming Zhou Reviewed-by: Yosry Ahmed Reviewed-by: Nhat Pham Signed-off-by: Andrew Morton Acked-by: Johannes Weiner Signed-off-by: Usama Arif Reviewed-by: Chengming Zhou Reviewed-by: Yosry Ahmed Reviewed-by: Nhat Pham Signed-off-by: Andrew Morton Acked-by: Johannes Weiner Signed-off-by: Usama Arif Reviewed-by: Chengming Zhou Reviewed-by: Yosry Ahmed Reviewed-by: Nhat Pham Signed-off-by: Andrew Morton Acked-by: Johannes Weiner --- include/linux/swap.h | 1 + mm/page_io.c | 113 ++++++++++++++++++++++++++++++++++++++++++- mm/swapfile.c | 20 ++++++++ 3 files changed, 133 insertions(+), 1 deletion(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 3df75d62a835..ed03d421febd 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -299,6 +299,7 @@ struct swap_info_struct { signed char type; /* strange name for an index */ unsigned int max; /* extent of the swap_map */ unsigned char *swap_map; /* vmalloc'ed array of usage counts */ + unsigned long *zeromap; /* vmalloc'ed bitmap to track zero pages */ struct swap_cluster_info *cluster_info; /* cluster info. Only for SSD */ struct swap_cluster_list free_clusters; /* free clusters list */ unsigned int lowest_bit; /* index of first free in swap_map */ diff --git a/mm/page_io.c b/mm/page_io.c index 6c1c1828bb88..480b8f221d90 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -172,6 +172,88 @@ int generic_swapfile_activate(struct swap_info_struct *sis, goto out; } +static bool is_folio_page_zero_filled(struct folio *folio, int i) +{ + unsigned long *data; + unsigned int pos, last_pos = PAGE_SIZE / sizeof(*data) - 1; + bool ret = false; + + data = kmap_local_folio(folio, i * PAGE_SIZE); + if (data[last_pos]) + goto out; + for (pos = 0; pos < PAGE_SIZE / sizeof(*data); pos++) { + if (data[pos]) + goto out; + } + ret = true; +out: + kunmap_local(data); + return ret; +} + +static bool is_folio_zero_filled(struct folio *folio) +{ + unsigned int i; + + for (i = 0; i < folio_nr_pages(folio); i++) { + if (!is_folio_page_zero_filled(folio, i)) + return false; + } + return true; +} + +static void folio_zero_fill(struct folio *folio) +{ + unsigned int i; + + for (i = 0; i < folio_nr_pages(folio); i++) + clear_highpage(folio_page(folio, i)); +} + +static void swap_zeromap_folio_set(struct folio *folio) +{ + struct swap_info_struct *sis = swp_swap_info(folio->swap); + swp_entry_t entry; + unsigned int i; + + for (i = 0; i < folio_nr_pages(folio); i++) { + entry = page_swap_entry(folio_page(folio, i)); + set_bit(swp_offset(entry), sis->zeromap); + } +} + +static void swap_zeromap_folio_clear(struct folio *folio) +{ + struct swap_info_struct *sis = swp_swap_info(folio->swap); + swp_entry_t entry; + unsigned int i; + + for (i = 0; i < folio_nr_pages(folio); i++) { + entry = page_swap_entry(folio_page(folio, i)); + clear_bit(swp_offset(entry), sis->zeromap); + } +} + +/* + * Return the index of the first subpage which is not zero-filled + * according to swap_info_struct->zeromap. + * If all pages are zero-filled according to zeromap, it will return + * folio_nr_pages(folio). + */ +static unsigned int swap_zeromap_folio_test(struct folio *folio) +{ + struct swap_info_struct *sis = swp_swap_info(folio->swap); + swp_entry_t entry; + unsigned int i; + + for (i = 0; i < folio_nr_pages(folio); i++) { + entry = page_swap_entry(folio_page(folio, i)); + if (!test_bit(swp_offset(entry), sis->zeromap)) + return i; + } + return i; +} + /* * We may have stale swap cache pages in memory: notice * them here and get rid of the unnecessary final write. @@ -195,6 +277,13 @@ int swap_writepage(struct page *page, struct writeback_control *wbc) folio_unlock(folio); return ret; } + + if (is_folio_zero_filled(folio)) { + swap_zeromap_folio_set(folio); + folio_unlock(folio); + return 0; + } + swap_zeromap_folio_clear(folio); if (zswap_store(folio)) { folio_unlock(folio); return 0; @@ -424,6 +513,26 @@ static void sio_read_complete(struct kiocb *iocb, long ret) mempool_free(sio, sio_pool); } +static bool swap_read_folio_zeromap(struct folio *folio) +{ + unsigned int idx = swap_zeromap_folio_test(folio); + + if (idx == 0) + return false; + + /* + * Swapping in a large folio that is partially in the zeromap is not + * currently handled. Return true without marking the folio uptodate so + * that an IO error is emitted (e.g. do_swap_page() will sigbus). + */ + if (WARN_ON_ONCE(idx < folio_nr_pages(folio))) + return true; + + folio_zero_fill(folio); + folio_mark_uptodate(folio); + return true; +} + static void swap_read_folio_fs(struct folio *folio, struct swap_iocb **plug) { struct swap_info_struct *sis = swp_swap_info(folio->swap); @@ -514,7 +623,9 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug) } delayacct_swapin_start(); - if (zswap_load(folio)) { + if (swap_read_folio_zeromap(folio)) { + folio_unlock(folio); + } else if (zswap_load(folio)) { folio_unlock(folio); } else if (data_race(sis->flags & SWP_FS_OPS)) { swap_read_folio_fs(folio, plug); diff --git a/mm/swapfile.c b/mm/swapfile.c index 9c6d8e557c0f..028d33f3a76d 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -747,6 +747,14 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset, unsigned long begin = offset; unsigned long end = offset + nr_entries - 1; void (*swap_slot_free_notify)(struct block_device *, unsigned long); + unsigned int i; + + /* + * Use atomic clear_bit operations only on zeromap instead of non-atomic + * bitmap_clear to prevent adjacent bits corruption due to simultaneous writes. + */ + for (i = 0; i < nr_entries; i++) + clear_bit(offset + i, si->zeromap); if (offset < si->lowest_bit) si->lowest_bit = offset; @@ -2635,6 +2643,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) free_percpu(p->cluster_next_cpu); p->cluster_next_cpu = NULL; vfree(swap_map); + kvfree(p->zeromap); kvfree(cluster_info); /* Destroy swap account information */ swap_cgroup_swapoff(p->type); @@ -3161,6 +3170,17 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) goto bad_swap_unlock_inode; } + /* + * Use kvmalloc_array instead of bitmap_zalloc as the allocation order might + * be above MAX_PAGE_ORDER incase of a large swap file. + */ + p->zeromap = kvmalloc_array(BITS_TO_LONGS(maxpages), sizeof(unsigned long), + GFP_KERNEL | __GFP_ZERO); + if (!p->zeromap) { + error = -ENOMEM; + goto bad_swap_unlock_inode; + } + if (p->bdev && bdev_stable_writes(p->bdev)) p->flags |= SWP_STABLE_WRITES; From patchwork Thu Jun 27 10:55:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usama Arif X-Patchwork-Id: 13714185 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 92678C2BD09 for ; Thu, 27 Jun 2024 10:57:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8E78A6B00A3; Thu, 27 Jun 2024 06:57:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 846246B00A7; Thu, 27 Jun 2024 06:57:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 49DF46B00A5; Thu, 27 Jun 2024 06:57:38 -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 260B16B00A4 for ; Thu, 27 Jun 2024 06:57:38 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id CBA8FA1C72 for ; Thu, 27 Jun 2024 10:57:37 +0000 (UTC) X-FDA: 82276367754.08.7F0FC1B Received: from mail-qk1-f171.google.com (mail-qk1-f171.google.com [209.85.222.171]) by imf26.hostedemail.com (Postfix) with ESMTP id EC60A14000C for ; Thu, 27 Jun 2024 10:57:35 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="DLwDk/lz"; spf=pass (imf26.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.222.171 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1719485843; 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=/jytiD3F6dVDoVcn3tgOCkYr8qgKU/Kw/DG4oKzaeY4=; b=lfFZJYOxWuiIuzQN9e/mwJ0rgyXGUFJQ2vzSZgAvZGHxinsM8X0krKxFTAl65hoYHuvbcg uwqPxOMC+yu44qmVHHu7vVkxjhO43jPjWGixy+eXL+su4W/FvwP+WJOzZb66qEwnc5VtRV MzcTynbcw++D0/dbXD7umtd/HsdMQ9M= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="DLwDk/lz"; spf=pass (imf26.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.222.171 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719485843; a=rsa-sha256; cv=none; b=X7y5vrFlwanoJuQoLwOCaJzv4JuBY185dWhpRyVaEY0R/Sxx5endn2Kt1xxU+8Whnd3CtX ZQVL7HwTR8LSCB9Fbnb8CBhRtJWkazr4a3GkiThdvEKU5m7dvk3+rt2gDSckvAcGZyisPq bLvSiRqEvMpHNWajevo+oZfAAe+jfdM= Received: by mail-qk1-f171.google.com with SMTP id af79cd13be357-79c054b65d4so156858085a.1 for ; Thu, 27 Jun 2024 03:57:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719485855; x=1720090655; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/jytiD3F6dVDoVcn3tgOCkYr8qgKU/Kw/DG4oKzaeY4=; b=DLwDk/lzDDnXJR8oV/ggTGcDFHrAeRpNbfbuh4jOZWFWIZkn3svg8nmbrvSO+ESfie Su6oH6J2O9eqpsv8sAV0lHc88OlRwpL9HuaDcThEM+IIMfMieJ7SKjyt7/4lpyDOrxa5 8vPrC9AEbM6hT0AyNIt9tKKqT5JNoIw1++DSSNDMJGF27rqZiuN5gsGCMYbkBulVVTzL 10JcNWs5LvaLJ+jIWSCavaQrzjENf6hFbEohKSg6TTTgHgCR4Z545QCU3nBigkgo/0BX yEYmyrVQBS6XlwDifvzy/sCRPJJ61GfA7JqWALCdHZ/RaxJn5bTe5zIHAHfCk8T3ukRM ZAPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719485855; x=1720090655; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/jytiD3F6dVDoVcn3tgOCkYr8qgKU/Kw/DG4oKzaeY4=; b=ilI4L26NIg5j93Cnao1v4o3ilLKmvHDc8Eun4GhaDb/xGNkwWkB95f7zDSVrbaaK8r nZKHEWA0vijsc1twrR2JOsQ6n6P6ED25RfqYFI2pMQDk4beLzItuNWls+ly6ZEfylKLk 7FWjgglYDEw93wh57ElSKcPC/e4OnVjkQ3RSRwol4KlS0Xgh9sx/C7TrA3gKM8ugnA5I lpb+Wz/wZSKIuTnJrREbrcTPebkk0awpbyGvXJmowrd1vmQRegAwpzx5/ESX4iLNTUaX /TT9v5S6UrIhUsgrHw8RC+dHCfB5kcVJWGvFuqm5SREKFUEpgUw3ljLLHYC2PR/Xm7MJ vKaw== X-Forwarded-Encrypted: i=1; AJvYcCXRsooCerlNkbzHZSokIgwd6HOFfKFKauHlT2M9CGLyMgMS/SxNiLDG2zw2YTK1/HI+FShy+jrpvx2lIzo57Dr1zEc= X-Gm-Message-State: AOJu0YyRgwhTMwrbLJLpLYqUIUGPjh6NL6EuWz3iN6N7kLXWq+O+TsL0 plzaU3c/BJyb8PAYW6/YKUYJ+lms5JWEMv7idFNIypNFNljrmyMi X-Google-Smtp-Source: AGHT+IFRSA2E/3cS18UczoHmhGiJ1yqas1kMncm42FdVDN8CvhCvuezJ9y6/uHNF847ZRU8aorvsYg== X-Received: by 2002:a05:620a:404d:b0:794:b302:96b5 with SMTP id af79cd13be357-79be0c62f1bmr1704032485a.36.1719485854953; Thu, 27 Jun 2024 03:57:34 -0700 (PDT) Received: from localhost (fwdproxy-ash-012.fbsv.net. [2a03:2880:20ff:c::face:b00c]) by smtp.gmail.com with ESMTPSA id af79cd13be357-79d5c8ba5b1sm43902985a.108.2024.06.27.03.57.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Jun 2024 03:57:34 -0700 (PDT) From: Usama Arif To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, shakeel.butt@linux.dev, david@redhat.com, ying.huang@intel.com, hughd@google.com, willy@infradead.org, yosryahmed@google.com, nphamcs@gmail.com, chengming.zhou@linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@meta.com, Usama Arif , Andi Kleen Subject: [PATCH v7 2/2] mm: remove code to handle same filled pages Date: Thu, 27 Jun 2024 11:55:30 +0100 Message-ID: <20240627105730.3110705-3-usamaarif642@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240627105730.3110705-1-usamaarif642@gmail.com> References: <20240627105730.3110705-1-usamaarif642@gmail.com> MIME-Version: 1.0 X-Stat-Signature: gcakqsd6g64ebhkyiif6jco3dnfj9pq3 X-Rspam-User: X-Rspamd-Queue-Id: EC60A14000C X-Rspamd-Server: rspam02 X-HE-Tag: 1719485855-611698 X-HE-Meta: U2FsdGVkX19q2NAgBo1oklYkA9Zs1ozWvl8BBLuY+lRBNx0YvDrcFsw2t8wDJA8sKHZmjNK0gVEqwNCrj4SVXH7YlD3qJjh7aawBcupP0Hgt+CWOAoZ/aToUjdxzujA1SCEtP19zlKgDPre8NNHVj96fAdkRL8a5cmYjuBUzWbPnjK+0gkSez0xYsdChhAXphubJNkeLGlku5sEHiMm4xgyL44A1/nvuL/fudokgzA9buH1wadY5Oi+r7/biXURhw4+MDbpw7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iAcGuOMNNq9aRw2+uvUFQt/Hj9rHnzrZPfcj6jelwaH8FVq6AA6Va5aTwucscw5gym1Y8e7NhjyOddmP88oolShU2tGV13yEyxNIBJnduVROQiZ9NuubfVsF7aKqgHMKJ5WsTCyGtUo94WoI+SCU3LEUR33OKxNIZjkOd5IGRaPk6Lk/I5XdRckBzrkc26kAjfmkzHMqn5MfcA9guTuDrnL+nWH9xqCR7Qf8= 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: With an earlier commit to handle zero-filled pages in swap directly, and with only 1% of the same-filled pages being non-zero, zswap no longer needs to handle same-filled pages and can just work on compressed pages. Signed-off-by: Usama Arif Reviewed-by: Chengming Zhou Acked-by: Yosry Ahmed Reviewed-by: Nhat Pham Cc: Andi Kleen Cc: David Hildenbrand Cc: "Huang, Ying" Cc: Hugh Dickins Cc: Johannes Weiner Cc: Matthew Wilcox (Oracle) Cc: Shakeel Butt Signed-off-by: Andrew Morton Acked-by: Johannes Weiner --- mm/zswap.c | 86 +++++------------------------------------------------- 1 file changed, 8 insertions(+), 78 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index a546c01602aa..e25a6808c2ed 100644 --- a/mm/zswap.c +++ b/mm/zswap.c @@ -44,8 +44,6 @@ **********************************/ /* The number of compressed pages currently stored in zswap */ atomic_t zswap_stored_pages = ATOMIC_INIT(0); -/* The number of same-value filled pages currently stored in zswap */ -static atomic_t zswap_same_filled_pages = ATOMIC_INIT(0); /* * The statistics below are not protected from concurrent access for @@ -188,11 +186,9 @@ static struct shrinker *zswap_shrinker; * * swpentry - associated swap entry, the offset indexes into the red-black tree * length - the length in bytes of the compressed page data. Needed during - * decompression. For a same value filled page length is 0, and both - * pool and lru are invalid and must be ignored. + * decompression. * 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. */ @@ -200,10 +196,7 @@ struct zswap_entry { swp_entry_t swpentry; unsigned int length; struct zswap_pool *pool; - union { - unsigned long handle; - unsigned long value; - }; + unsigned long handle; struct obj_cgroup *objcg; struct list_head lru; }; @@ -820,13 +813,9 @@ static struct zpool *zswap_find_zpool(struct zswap_entry *entry) */ static void zswap_entry_free(struct zswap_entry *entry) { - if (!entry->length) - atomic_dec(&zswap_same_filled_pages); - else { - zswap_lru_del(&zswap_list_lru, entry); - zpool_free(zswap_find_zpool(entry), entry->handle); - zswap_pool_put(entry->pool); - } + zswap_lru_del(&zswap_list_lru, entry); + zpool_free(zswap_find_zpool(entry), entry->handle); + zswap_pool_put(entry->pool); if (entry->objcg) { obj_cgroup_uncharge_zswap(entry->objcg, entry->length); obj_cgroup_put(entry->objcg); @@ -1268,11 +1257,6 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker, * This ensures that the better zswap compresses memory, the fewer * pages we will evict to swap (as it will otherwise incur IO for * relatively small memory saving). - * - * The memory saving factor calculated here takes same-filled pages into - * account, but those are not freeable since they almost occupy no - * space. Hence, we may scale nr_freeable down a little bit more than we - * should if we have a lot of same-filled pages. */ return mult_frac(nr_freeable, nr_backing, nr_stored); } @@ -1376,42 +1360,6 @@ static void shrink_worker(struct work_struct *w) } while (zswap_total_pages() > thr); } -/********************************* -* same-filled functions -**********************************/ -static bool zswap_is_folio_same_filled(struct folio *folio, unsigned long *value) -{ - unsigned long *data; - unsigned long val; - unsigned int pos, last_pos = PAGE_SIZE / sizeof(*data) - 1; - bool ret = false; - - data = kmap_local_folio(folio, 0); - val = data[0]; - - if (val != data[last_pos]) - goto out; - - for (pos = 1; pos < last_pos; pos++) { - if (val != data[pos]) - goto out; - } - - *value = val; - ret = true; -out: - kunmap_local(data); - return ret; -} - -static void zswap_fill_folio(struct folio *folio, unsigned long value) -{ - unsigned long *data = kmap_local_folio(folio, 0); - - memset_l(data, value, PAGE_SIZE / sizeof(unsigned long)); - kunmap_local(data); -} - /********************************* * main API **********************************/ @@ -1423,7 +1371,6 @@ bool zswap_store(struct folio *folio) struct zswap_entry *entry, *old; struct obj_cgroup *objcg = NULL; struct mem_cgroup *memcg = NULL; - unsigned long value; VM_WARN_ON_ONCE(!folio_test_locked(folio)); VM_WARN_ON_ONCE(!folio_test_swapcache(folio)); @@ -1456,13 +1403,6 @@ bool zswap_store(struct folio *folio) goto reject; } - if (zswap_is_folio_same_filled(folio, &value)) { - entry->length = 0; - entry->value = value; - atomic_inc(&zswap_same_filled_pages); - goto store_entry; - } - /* if entry is successfully added, it keeps the reference */ entry->pool = zswap_pool_current_get(); if (!entry->pool) @@ -1480,7 +1420,6 @@ bool zswap_store(struct folio *folio) if (!zswap_compress(folio, entry)) goto put_pool; -store_entry: entry->swpentry = swp; entry->objcg = objcg; @@ -1528,13 +1467,9 @@ bool zswap_store(struct folio *folio) return true; store_failed: - if (!entry->length) - atomic_dec(&zswap_same_filled_pages); - else { - zpool_free(zswap_find_zpool(entry), entry->handle); + zpool_free(zswap_find_zpool(entry), entry->handle); put_pool: - zswap_pool_put(entry->pool); - } + zswap_pool_put(entry->pool); freepage: zswap_entry_cache_free(entry); reject: @@ -1597,10 +1532,7 @@ bool zswap_load(struct folio *folio) if (!entry) return false; - if (entry->length) - zswap_decompress(entry, folio); - else - zswap_fill_folio(folio, entry->value); + zswap_decompress(entry, folio); count_vm_event(ZSWPIN); if (entry->objcg) @@ -1703,8 +1635,6 @@ static int zswap_debugfs_init(void) zswap_debugfs_root, NULL, &total_size_fops); debugfs_create_atomic_t("stored_pages", 0444, zswap_debugfs_root, &zswap_stored_pages); - debugfs_create_atomic_t("same_filled_pages", 0444, - zswap_debugfs_root, &zswap_same_filled_pages); return 0; }