From patchwork Wed Jun 12 12:43:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usama Arif X-Patchwork-Id: 13694920 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 06CC7C27C7B for ; Wed, 12 Jun 2024 12:47:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8A3F76B0093; Wed, 12 Jun 2024 08:47:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7B7636B0095; Wed, 12 Jun 2024 08:47:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 657D96B0096; Wed, 12 Jun 2024 08:47:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 4568D6B0093 for ; Wed, 12 Jun 2024 08:47:57 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id E287814173C for ; Wed, 12 Jun 2024 12:47:56 +0000 (UTC) X-FDA: 82222213752.30.A0E2780 Received: from mail-qt1-f175.google.com (mail-qt1-f175.google.com [209.85.160.175]) by imf09.hostedemail.com (Postfix) with ESMTP id 14196140004 for ; Wed, 12 Jun 2024 12:47:54 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=kVAWELTo; spf=pass (imf09.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.160.175 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=1718196475; a=rsa-sha256; cv=none; b=vuY6atK2rPSreLb4GjV8ru5mSV14bHjB3MtY5oA1RWejKW3mOAIG+bib3QvkJ52dAPMFA1 qHuHSE2gQTqzhCAp0w5sxLE4BrkmtwEFP76QI+tRhX71uRqaZK8TgozpfPYnOnur/SJul1 lp1Tm7m7CAaq2ZWmXb+WbxssHuO6hDM= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=kVAWELTo; spf=pass (imf09.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.160.175 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=1718196475; 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=YA/i9kGTfJC6xvqtw7O5uORmvK7ZuPAipxeerdrc6dg=; b=pLZeVS9BfOZxNRNHU0pf+16MUuQ80DY1WYeSVDGNggPSNr+XKl8laWROlf53K/2YLDUCrE hsjtfBVtlQaajuXP8YHrFaUiHus1z9R8yaNgIMk00v4QPE6W1qD6RLAGmgXu2/NTfEnGF5 xknEUu44vM3pQ4tJdNo8iIS81xaST7U= Received: by mail-qt1-f175.google.com with SMTP id d75a77b69052e-44056295c8cso11670551cf.3 for ; Wed, 12 Jun 2024 05:47:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718196474; x=1718801274; 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=YA/i9kGTfJC6xvqtw7O5uORmvK7ZuPAipxeerdrc6dg=; b=kVAWELTo2X9e7pizEhodG+g8EGM4Wa2t02FGmlE3BJkHFFNqv8/9RsJpuw/cQsLVX8 hghd0zaBbymwGcTVJXZBPoOgubY/oT0S/jrKWQ8m1XE6O5uio3xbSZorogzC9rVTQXS3 TiU6PjKhXTbQGbq2idfcER/KQzOte/eQR/nELl2xM9b6BYXUYDllEWpzlQpppxwamkbH iakn7rYVdDncDqhscxA+vsdpXFFIrMEU+y9zgyscjsYpGW+HZwuNX+FoO3wDNVuwC4de oRM/nfU7wGbM85/d6N13eqb4+W36U70yZxRE9sbONMkEHf0PYSgyjocOhn7W1MIVW1op Zy0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718196474; x=1718801274; 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=YA/i9kGTfJC6xvqtw7O5uORmvK7ZuPAipxeerdrc6dg=; b=D3UMUIVODchkkqHnLQtpuByBnz2bSeYEuJHAdi1WMp6png2HTxpfqiBgLpmyquu6vu QxCKVH0u3qIUozc/giLyEJDDga/BOBU7kbg6FCQ3kr5psydShK5K7GRRDB0Y/VovdmgE opejKUvXTKx7BTSR5OZhkSHDeAOF8S5M4UVisUdRkHcNfsBNe29QWa0969HKvYJIt8VU va7vxzkajXDTTzdRoS8wx2Yzc+QCMF3AVJhmlqiE+d8PSvx6w59KqbXHvBEoC1eCp7xi m4qU53QZsOpUMn7XCgtJLCvZVrkXsaf3C7wDF3KagMTkn0PacCLTSTz9C1U0YFxilTFT WulQ== X-Forwarded-Encrypted: i=1; AJvYcCXiAS7jyhXqkfR4lkxXADPAUJqdxGXpSmsh5c1MvrmfUz4ryQndn7zF8BZ7CpTL6CLH/Ef1kKw/Ytb7ZUsREc63oK8= X-Gm-Message-State: AOJu0Yzq7QRZcqGGu5IhX5/2TdklFOViSmzf9WbTS7a8DaQ71SWX5G5N FFOsd1grWbRu6gX1ztydinvWy8zT/10gbr1rJgZqxYcymdTcJwAX X-Google-Smtp-Source: AGHT+IE1CPP/v/4tI/OK9eS0gZXEc9/e7Fh8lfMD02Ajhy/Np6G3I/hIvWhzZWbp9Vcv211/Yhk2Iw== X-Received: by 2002:ac8:5841:0:b0:440:5c2c:4cdb with SMTP id d75a77b69052e-4415abad660mr15732401cf.2.1718196474067; Wed, 12 Jun 2024 05:47:54 -0700 (PDT) Received: from localhost (fwdproxy-ash-001.fbsv.net. [2a03:2880:20ff:1::face:b00c]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-4406107ebf1sm33834781cf.95.2024.06.12.05.47.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 05:47:53 -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 Subject: [PATCH v4 1/2] mm: store zero pages to be swapped out in a bitmap Date: Wed, 12 Jun 2024 13:43:35 +0100 Message-ID: <20240612124750.2220726-2-usamaarif642@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240612124750.2220726-1-usamaarif642@gmail.com> References: <20240612124750.2220726-1-usamaarif642@gmail.com> MIME-Version: 1.0 X-Stat-Signature: y7u8a5f1ykwokcsyj5fzacrofm6jwcz3 X-Rspamd-Queue-Id: 14196140004 X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1718196474-67643 X-HE-Meta: U2FsdGVkX1/E7T939Sm814lNfma3/EucNq5yKRB67Ue7EnN+Y3+GaH6xYwLqddSRAk9ynNN6zSlhtPOIIHdWwMQ0y1RVlje7jqFkwLe5uwOKnQTcKiFo0QTLkn9P8nCoqoKYeXHR27hInlLS3p19RA/LYj7NvM91R+e2ApBlua+wi5pRcQbGYq73bVrmASQCd24EISgiaQaAO6AwC67hCPnfL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/1v+Ii7YT6esGm84Yt6lIk0kXrvv59ItuRiX0YmW3ZQshqikBn80Xgw7eZ6p9uHo5nz5Kghy8KS5KC47Vq2yRGa4JjkxRNW07gTbcWN5TshXZw== 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 Signed-off-by: Barry Song --- include/linux/swap.h | 1 + mm/page_io.c | 114 ++++++++++++++++++++++++++++++++++++++++++- mm/swapfile.c | 24 ++++++++- 3 files changed, 136 insertions(+), 3 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index a11c75e897ec..e88563978441 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 a360857cf75d..39fc3919ce15 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_start_writeback(folio); folio_unlock(folio); @@ -426,6 +515,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); @@ -515,8 +624,9 @@ void swap_read_folio(struct folio *folio, bool synchronous, psi_memstall_enter(&pflags); } delayacct_swapin_start(); - - if (zswap_load(folio)) { + if (swap_read_folio_zeromap(folio)) { + folio_unlock(folio); + } else if (zswap_load(folio)) { folio_mark_uptodate(folio); folio_unlock(folio); } else if (data_race(sis->flags & SWP_FS_OPS)) { diff --git a/mm/swapfile.c b/mm/swapfile.c index f1e559e216bd..48d8dca0b94b 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -453,6 +453,8 @@ static unsigned int cluster_list_del_first(struct swap_cluster_list *list, static void swap_cluster_schedule_discard(struct swap_info_struct *si, unsigned int idx) { + unsigned int i; + /* * If scan_swap_map_slots() can't find a free cluster, it will check * si->swap_map directly. To make sure the discarding cluster isn't @@ -461,6 +463,13 @@ static void swap_cluster_schedule_discard(struct swap_info_struct *si, */ memset(si->swap_map + idx * SWAPFILE_CLUSTER, SWAP_MAP_BAD, SWAPFILE_CLUSTER); + /* + * zeromap can see updates from concurrent swap_writepage() and swap_read_folio() + * call on other slots, hence use atomic clear_bit for zeromap instead of the + * non-atomic bitmap_clear. + */ + for (i = 0; i < SWAPFILE_CLUSTER; i++) + clear_bit(idx * SWAPFILE_CLUSTER + i, si->zeromap); cluster_list_add_tail(&si->discard_clusters, si->cluster_info, idx); @@ -482,7 +491,7 @@ static void __free_cluster(struct swap_info_struct *si, unsigned long idx) static void swap_do_scheduled_discard(struct swap_info_struct *si) { struct swap_cluster_info *info, *ci; - unsigned int idx; + unsigned int idx, i; info = si->cluster_info; @@ -498,6 +507,8 @@ static void swap_do_scheduled_discard(struct swap_info_struct *si) __free_cluster(si, idx); memset(si->swap_map + idx * SWAPFILE_CLUSTER, 0, SWAPFILE_CLUSTER); + for (i = 0; i < SWAPFILE_CLUSTER; i++) + clear_bit(idx * SWAPFILE_CLUSTER + i, si->zeromap); unlock_cluster(ci); } } @@ -1059,9 +1070,12 @@ static void swap_free_cluster(struct swap_info_struct *si, unsigned long idx) { unsigned long offset = idx * SWAPFILE_CLUSTER; struct swap_cluster_info *ci; + unsigned int i; ci = lock_cluster(si, offset); memset(si->swap_map + offset, 0, SWAPFILE_CLUSTER); + for (i = 0; i < SWAPFILE_CLUSTER; i++) + clear_bit(offset + i, si->zeromap); cluster_set_count_flag(ci, 0, 0); free_cluster(si, idx); unlock_cluster(ci); @@ -1336,6 +1350,7 @@ static void swap_entry_free(struct swap_info_struct *p, swp_entry_t entry) count = p->swap_map[offset]; VM_BUG_ON(count != SWAP_HAS_CACHE); p->swap_map[offset] = 0; + clear_bit(offset, p->zeromap); dec_cluster_info_page(p, p->cluster_info, offset); unlock_cluster(ci); @@ -2597,6 +2612,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) free_percpu(p->cluster_next_cpu); p->cluster_next_cpu = NULL; vfree(swap_map); + bitmap_free(p->zeromap); kvfree(cluster_info); /* Destroy swap account information */ swap_cgroup_swapoff(p->type); @@ -3123,6 +3139,12 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) goto bad_swap_unlock_inode; } + p->zeromap = bitmap_zalloc(maxpages, GFP_KERNEL); + 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 Wed Jun 12 12:43:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usama Arif X-Patchwork-Id: 13694921 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 BD57CC27C53 for ; Wed, 12 Jun 2024 12:47:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 17AB46B0096; Wed, 12 Jun 2024 08:47:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 100C56B0098; Wed, 12 Jun 2024 08:47:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E48F56B0099; Wed, 12 Jun 2024 08:47:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id B7C426B0096 for ; Wed, 12 Jun 2024 08:47:58 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 36A68120409 for ; Wed, 12 Jun 2024 12:47:58 +0000 (UTC) X-FDA: 82222213836.03.BB667F8 Received: from mail-yw1-f182.google.com (mail-yw1-f182.google.com [209.85.128.182]) by imf30.hostedemail.com (Postfix) with ESMTP id 6316D80008 for ; Wed, 12 Jun 2024 12:47:56 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=lGPUHCHg; spf=pass (imf30.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.128.182 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=1718196476; 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=U09nDqdyDjm09ov5OxqAlgrZTEnMx+ri6277Ej4J+SE=; b=xQSZp/zBcEAr4uIeP0944rW72kil4vCVLwCUzuhM6zzVDZRoejdJolRbHJqxVxUKp8x78h Lww50YmwjRnogPdcaTDHEcAFVu/4ki5rdkqGVSZh10UMlH+aV8NfG0bOCT9i0SaBuuc1xW MMQfKnVT9M+7NL6m14QndDx6pm1FH+o= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718196476; a=rsa-sha256; cv=none; b=Odq5xyZzwSbkfeHZbd2rU4EaR6xS12Rtmll00uWrAQQwZeo8bqiq8TdUrJWLQEJ2QFKI8y iPW97dbpeOeAtlFW8ogvdRjSL07NP7CSNNUMWMGM2GCSz3bQ3RPAgS+SVaLzrFS8Z8RsvC DOcdpaITO+o7WQK/AYvaTfTe1Scmd7Y= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=lGPUHCHg; spf=pass (imf30.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.128.182 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-yw1-f182.google.com with SMTP id 00721157ae682-627efad69b4so70938437b3.3 for ; Wed, 12 Jun 2024 05:47:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718196475; x=1718801275; 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=U09nDqdyDjm09ov5OxqAlgrZTEnMx+ri6277Ej4J+SE=; b=lGPUHCHgg8BV7QxDEtFkxazSVH9UdbtvUNFqGTzIpicdITiLu14MxdDpi9Np5rm+Zd 5TvSVFPguiCKFfgzQJDwLkm3W+Q3ro+P4zjTSCFsKQMeAT5sNbLxdYMEJJOIVJpqT+az fygbFMfpH47a3GjVZBYEFLO9+PGZWtIZVtdnTofywTFR+lUm9ovXiZI3leex8Jxs4Ihf 83tbUzm4JQKWl3V0BtuDNurpNLbFVlKvbs9lzxEs2WNNSCEd9KjFGsbaw+Zub5+jvaaf ACC2V5ISU8kuRguxq87+hB+7A39LK2Uzl4COFfVFvWhP9xrUp54SlcL7EBNNiLBK9o0M n0jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718196475; x=1718801275; 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=U09nDqdyDjm09ov5OxqAlgrZTEnMx+ri6277Ej4J+SE=; b=HmJAIV+utHtATqZ4QPcNfmcpKU5sPZj6WZsvdxdo1NVuzHRrKUzBy3cbQC2UdkCPzp UniUgfTVStkKQK/DXs+RaGe7THKpF0eDht6QK7JU7rBkSbj86AATK2cO4bLezHSCt8nG gNlOPD2jzbHJatlD5imAdD5F9FRzlDPfs2ae6fX2v1QdRkBwOULfzsWRvFn0zCp4VTpf k3ALaHJxNR3PYAYh9F4xff71+7xkaboXkAVeGOnVZ/hwxuo6jtI1eJBay+7GodHy/9kE J73HusPTmpyWiSQCl0XzYtRoTGvm7RnJRvOzJLbx3pbzTAbSfBfZFVJs9/bEhby8DT8Z NZDA== X-Forwarded-Encrypted: i=1; AJvYcCWXH+L6i8qX+VcF3QW7WPuvqWH8xRTPG2hT0Zt5WPYUgo22RQ+A0C1cV7W38qOJ7KIYuCVQ4g3Id04iJAw7lt92/Kk= X-Gm-Message-State: AOJu0YwegdBelRMscuKwKtzxoGyMITccSspp7t3u6tEFnQ1FrtszA80Z 3kG/oYPMntAoyyGF9ZDSd1WX5FGfflLlSKI+KxYMoLgAcjmmkzbY X-Google-Smtp-Source: AGHT+IGkQ17HzT2wzOFm9lZ1MUlnx3fVT0qSwDvdVstx+8uIKcOGNUUnfvL0eYxQuIWJ1sfFl/Gqdw== X-Received: by 2002:a81:8491:0:b0:630:3061:c22 with SMTP id 00721157ae682-63030610d9fmr7911287b3.20.1718196475331; Wed, 12 Jun 2024 05:47:55 -0700 (PDT) Received: from localhost (fwdproxy-ash-008.fbsv.net. [2a03:2880:20ff:8::face:b00c]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b07ed90264sm30056906d6.13.2024.06.12.05.47.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 05:47:54 -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 Subject: [PATCH v4 2/2] mm: remove code to handle same filled pages Date: Wed, 12 Jun 2024 13:43:36 +0100 Message-ID: <20240612124750.2220726-3-usamaarif642@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240612124750.2220726-1-usamaarif642@gmail.com> References: <20240612124750.2220726-1-usamaarif642@gmail.com> MIME-Version: 1.0 X-Stat-Signature: edu888n8r89iihy1ga9z6upcoa8zjjn8 X-Rspamd-Queue-Id: 6316D80008 X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1718196476-238256 X-HE-Meta: U2FsdGVkX1+uQCFvQ/BAbudWDU52zqNU7ajUufBkZCE9fBjW2b2XkIvT6WITYsvSY3ZEWvKf9eCJHvtAHhlHEEN+9oOibRnHu72cObQTF+oY1RL+rzPITFFX6ws0TO3WZQvn4oRER2CUGNFSo4oMlGoFrMrx+IaGRx6786JFoAwrBgSKKtnI3aXTfB/OtuX2F6C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a3d+I70h/y0kuchlRRvtjxESQFVlSngLGoPbJOBBQYm2kUkgEB55qIQQfJADXmFR3g== 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 --- mm/zswap.c | 86 +++++------------------------------------------------- 1 file changed, 8 insertions(+), 78 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index b9b35ef86d9b..ca8df9c99abf 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 @@ -182,11 +180,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. */ @@ -194,10 +190,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; }; @@ -814,13 +807,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); @@ -1262,11 +1251,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); } @@ -1370,42 +1354,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 **********************************/ @@ -1417,7 +1365,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)); @@ -1450,13 +1397,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) @@ -1474,7 +1414,6 @@ bool zswap_store(struct folio *folio) if (!zswap_compress(folio, entry)) goto put_pool; -store_entry: entry->swpentry = swp; entry->objcg = objcg; @@ -1522,13 +1461,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: @@ -1577,10 +1512,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) @@ -1682,8 +1614,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; }