From patchwork Mon Jun 10 12:15:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usama Arif X-Patchwork-Id: 13691954 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 598E3C27C65 for ; Mon, 10 Jun 2024 12:18:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D0F4B6B0092; Mon, 10 Jun 2024 08:18:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CC0126B00A4; Mon, 10 Jun 2024 08:18:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 98B336B00A5; Mon, 10 Jun 2024 08:18:28 -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 7511D6B0092 for ; Mon, 10 Jun 2024 08:18:28 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 26A80140D56 for ; Mon, 10 Jun 2024 12:18:28 +0000 (UTC) X-FDA: 82214881896.20.235C164 Received: from mail-qv1-f51.google.com (mail-qv1-f51.google.com [209.85.219.51]) by imf15.hostedemail.com (Postfix) with ESMTP id 4DECBA0004 for ; Mon, 10 Jun 2024 12:18:26 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=dkh8ksaV; spf=pass (imf15.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.219.51 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=1718021906; a=rsa-sha256; cv=none; b=tGZ5QlnQKu9HYVXE0m0hJWuNNnQs0PZ/pubZ5Z6/Q8jYiYKJg3IIx+xs8vjhTVc8d5r/o4 govv3f7DTXWkrGRIDcFLLx5G5qtJOIFqKepL6Pqz1cw9CIbGegjOReh8MffVUt+Bz4ZKUZ ReTUS/sH9BGy9ALlEA0thXyuAQ95vkY= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=dkh8ksaV; spf=pass (imf15.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.219.51 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=1718021906; 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=enp1TRQZwGsuV9i2AxeXdZVfnUfEdxfgu+pXvPIz5hY=; b=D38lOViUrOZ1IvBOK6N06VoUekipUPNTVgU/AVCxuKKBRQUCOqigxgN9M2JfqLN7J7aDIU vXcQjnidYSovV2UA8F7D8cWqhdPEcche/y3syrZ+M5fjZ7a77oDyvI0+2B8/MzGNFKy7EK qeHPXkQ09b6YAYhP8bInW1PTSPTECvI= Received: by mail-qv1-f51.google.com with SMTP id 6a1803df08f44-6b081c675e7so3168676d6.1 for ; Mon, 10 Jun 2024 05:18:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718021905; x=1718626705; 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=enp1TRQZwGsuV9i2AxeXdZVfnUfEdxfgu+pXvPIz5hY=; b=dkh8ksaV38KXb8dACKAW04aY9ZfmkHu+hikMlQGez9+vseJs/us9cV70VdFLbuuXsP dW4NomRERdnSb6AvOhkx0qKsqxWRKrhh162MtTtnD8ZRoFis7YCdGZ22TJKi3yOQgc2K 1cw45NeRd0P5gKzgu97Nz/+PFFbfJCaN8yg3C83gW33I6jvCZKfN9skHkBAvfb+W5nX/ XE4PSCFFues+Q56OWiKIGoS/J5ajqCPipUIk7aWT3e6fYIjuSpINKgiSRbzbxcStUd5P WbX8CfWaihOpzcwX4y1C9j+h0PxBFKVMEU5SpaiXiZzChtDe2gWGHyvfHVZovU4Yty+d 1rgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718021905; x=1718626705; 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=enp1TRQZwGsuV9i2AxeXdZVfnUfEdxfgu+pXvPIz5hY=; b=IREAm/6wK7vKgE7QTtrpfEwf5mtBZkeZm3by1PF5lqp/oD/8u4mD9XF9b2QPUyWZhW joVfAgQHCSzlvymPbjIm/q4dgFXSBgC3UGmLnj1Kve8lL54r2DTcX55SXI40sZ/yohy5 FwSdudHkJ9sn3qtl2AwTID02EHbgxoSvyOdTbXN06EVALGEhB4ttwcEGjVop64VbL5Lt WakiBkRCORmO7jfaeZYhR+EaoPXQu3uAHq5NNJK6KaQ+R+tcoQXMzb9FNLVyCaTzUBld 7O035/S93QQ9SKpzdWTrmJ19MUeIbrh38FN7qMWlawiQTL1HYP032L4CcydoGtb6uSbQ /c4Q== X-Forwarded-Encrypted: i=1; AJvYcCWDBFk/yeauzWExWw/Id7so6obpUc1c4hYAxGPQ/5e8k9BzcVZQHqCUqGQBWgxR4QsTYeVq0NtrDA+Qi62YhCFZfQg= X-Gm-Message-State: AOJu0YyKxMkCfCBWU1fELDePs8MpAXOKNMh0lggc2S/WivDCTcwtwOw5 Q8772/g+FRINNGKsuOvTgwVTIpwucrBzxDmfuyVImGGqhiD+29Tt X-Google-Smtp-Source: AGHT+IER/6/Ev/fin7kHekd/wvr1haZ/PJS9r0VJYzapmkP25Xc4EV0tD+B/2Rh4HpReLXXohs1EQQ== X-Received: by 2002:a05:6214:5990:b0:6b0:7206:e7a4 with SMTP id 6a1803df08f44-6b07206ebd7mr69801746d6.3.1718021905323; Mon, 10 Jun 2024 05:18:25 -0700 (PDT) Received: from localhost (fwdproxy-ash-000.fbsv.net. [2a03:2880:20ff::face:b00c]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b0683044dbsm27042936d6.75.2024.06.10.05.18.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 05:18:24 -0700 (PDT) From: Usama Arif To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, 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 v3 1/2] mm: store zero pages to be swapped out in a bitmap Date: Mon, 10 Jun 2024 13:15:59 +0100 Message-ID: <20240610121820.328876-2-usamaarif642@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240610121820.328876-1-usamaarif642@gmail.com> References: <20240610121820.328876-1-usamaarif642@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4DECBA0004 X-Rspam-User: X-Rspamd-Server: rspam12 X-Stat-Signature: at9e4ggxprhjecnwop177eiex1yb6umh X-HE-Tag: 1718021906-323770 X-HE-Meta: U2FsdGVkX1+Ik7L2K02xQ+bBT9O7TkLb37M0VQvdrW7pUMKpDue7V5NZ2cpAtlzCvzklo/nbaLShYiO6wp/p4a0nki32mhEn7QNkW3N0w9KBooYn/mHI17bOo4REvrqFzD2rvhc8dMaGgRXAvgMwB3QK62dfgL06B7AW3m1+HmDWzBxmPg8I/ExQaDnoTf4bDSeE2GELVREwFGvHLxRxZxQq1q27pvo07HpGseioVUWGP9LYRi1tQgb8y48UuDtxnKWfzSQSB64Ug9trEnHIQlBmBHPzq91B1ACFzGaHHVaeKgVDELTiVPnAQnz9g6/Bpn2BKfx9U8NFCTaA0RrWINr14mmu6B/2qFyyy0HY1cQBkdFDojQZnBTmuc2ApY/tbzHw8lpvVdYa0KS8u2jlAVo+L7GyFLqLKHPASnLa/GaL5R2aPwP8DvemgXgvF4lQGaOWCk26gLY1RKM7Cc53qgezCwM6O6R9G0piR+p4+5maMlCyaA4+En3tHAxBdM3QQbp8OyNPwukDnHULskcmYQYkTGjwV6BeisFoyrBtmyi8fi+UOTR2UGRU1AYYEJflNlTrcAprIqvXkX5+jaob0H5r6EV3T4ZA3UgxvgWQMRRYkQWExxQ18f40zuaLtGxH/XYYN7rqApRirGnYveFyKuGL0bhGBKpJ2HVVKuEXAnpATpLCZkHCY435/GpR9ePc4/Kbqkcth0eDBFXouqerQGqp7421tRH2ykdvf8PYmerXVrTY4Dy148OajCssOae33e3Ef+Z1t6f0NiQUrDyW1Uuo2Qj5chz8To70f52jM3JQACxtMhW/qAsVfBH026mVBl+n9OpZnDlGTG/63L6mgmlDLN/uHlAlv9c+3TjrnkB1skfTjn8dJmOd730xDO7uHmn1XSw0qdxIKHWX9X9jpe8i/uekeZHqYZ/r1V6Lbt/IDAWYu0qYgbIMkHu8ZCXfyDID8WOiH4Ah1cXmMak FzeLG1Wt i9iJp0S0wChv9XYxTiDVJ65JnSaWG/n2lL2adITPqWkoL1V5Nld3weqU2lpM2VBtq5lBQTtlVgT4t1/RLvpwvzpvDEnn7x0RbsllDx9lxt6tFjfhzj/zSwBU/ibrzGQbs3IwYIexj0CPRcrWXznhUvIBZAkV6xAomYs6v7NfJXxXev2vD9Pc0nswBaXzsN7dPyLG/zmQoLFXyf4DHFcGVIjTFRYYOZQrrODLmM7t7U0NAgW8oBFCxLcom7Ki2PIePcTGkJH4nWdxPtARSJPWRpVpVJ5Tym+KB3ON72y4jzbmPOjiXIiJ/4Q3BG2qpeGW9SEP0r5XxG2wnOSrNR6rcvHo4h66NMBZBRvi4NjTjEbO7oF6B8Oo34FqPZIMikeyt22IyRnF814Ux2mATe0Ll0worOi9aJ3Qc0kmAAZO8/RFzuxmxVFj5MQpV5VbMxQqx3PvfEnibTla5TVQlGErc9SEuMN2p7yG0qmFLzJUnjFwbJBY= 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 --- include/linux/swap.h | 1 + mm/page_io.c | 92 +++++++++++++++++++++++++++++++++++++++++++- mm/swapfile.c | 21 +++++++++- 3 files changed, 111 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..2cac1e11fb85 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -172,6 +172,82 @@ 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); + } +} + +static bool 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 false; + } + return true; +} + /* * We may have stale swap cache pages in memory: notice * them here and get rid of the unnecessary final write. @@ -195,6 +271,15 @@ 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_start_writeback(folio); + folio_unlock(folio); + folio_end_writeback(folio); + return 0; + } + swap_zeromap_folio_clear(folio); if (zswap_store(folio)) { folio_start_writeback(folio); folio_unlock(folio); @@ -515,8 +600,11 @@ void swap_read_folio(struct folio *folio, bool synchronous, psi_memstall_enter(&pflags); } delayacct_swapin_start(); - - if (zswap_load(folio)) { + if (swap_zeromap_folio_test(folio)) { + folio_zero_fill(folio); + folio_mark_uptodate(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..90451174fe34 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); } } @@ -1336,6 +1347,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 +2609,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 +3136,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 Mon Jun 10 12:16:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usama Arif X-Patchwork-Id: 13691955 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 164E3C27C55 for ; Mon, 10 Jun 2024 12:18:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 261906B00A7; Mon, 10 Jun 2024 08:18:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 211AA6B00A8; Mon, 10 Jun 2024 08:18:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F32A66B00A9; Mon, 10 Jun 2024 08:18:29 -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 C884C6B00A7 for ; Mon, 10 Jun 2024 08:18:29 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 6EF6E40F83 for ; Mon, 10 Jun 2024 12:18:29 +0000 (UTC) X-FDA: 82214881938.11.EC7F6BF Received: from mail-qt1-f175.google.com (mail-qt1-f175.google.com [209.85.160.175]) by imf27.hostedemail.com (Postfix) with ESMTP id A1E3D40019 for ; Mon, 10 Jun 2024 12:18:27 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=AVfz1+2K; spf=pass (imf27.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=1718021907; a=rsa-sha256; cv=none; b=4shn9akoY748FRddvx6/ByK9U6LZvsYZnvVj41m8wpxmrVgcjvDJUxySZ1/j5rSE35Wkf7 d4Gni8DXxNgTSluOWXnkGq12R0ZmUwaufTX0TVLJf5XD2CjDm8W70G1awH0/Pfs2KcUgy9 Cet5hs+cfmz8YFH7Ww8IlkD6OvE3dV8= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=AVfz1+2K; spf=pass (imf27.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=1718021907; 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=DXiBWXHL7CwKxeZukNI7HIW+CIUeGgh6jd5x/88roGDw5jXPmbQZPNxA+eIuS90eun25Ny SaJ2VWXOV02lR3W1A8oFbmRpRynCup5l+v115LrsMsOkuMkhG20zoHjwYMxh9gvM+LADAS A8sZ9GmZVfyGc01kvcRUvyctZbgGB9o= Received: by mail-qt1-f175.google.com with SMTP id d75a77b69052e-4405e343dd8so7936081cf.1 for ; Mon, 10 Jun 2024 05:18:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718021907; x=1718626707; 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=AVfz1+2K4k3i07ba0dHAYwpeOA0wj9gcaAcDkAG4IuwgFL/L/Cm56T2VZVSobpleYP 76oDaj7mD1LUBR3qehAUAm9KcD4BIUv6c0KE1dTzKXPrgR+tgHoeeZE2Z7GEr9B/3GnO kuVppqBxEQvPXbjbgW51EY31HCbh/JyDL7pxVmnMSW7vvUPOzZ5E48y5QIsNFtAQROMR b4uDvUvwR5o2rawKUlqx69b2oKAr1BXtB4aWPNNdj2s+X4csh+B/G/6AQhv0qyguLei3 jcXSaCl0KhcTZn9EQHtBPzwNcBp566WS+dcXjNEkL7UYSKYID9ccmQP/RdaXI5ImJP4t +YBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718021907; x=1718626707; 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=df/IMdkC5kyWeYDY9n5W4aNQvppLh07CJkvRsnGOdEbB/i26e4KLsRGxP7x6bAeObD vAGPIobASIXjhH7u8npbs8aFKP/0hFEyvnqt2sC6SgZCDOk7dLozSfGxC5XVyWAlmFYp of+EH/mIdrAO0zLFizVm54a4xTVkzdFzgibOvQ0f7v9GX8ktSsQ4G7G4dfSIvBJD+gIi 4PXQ+Cq/H63aUZ5Ip+uYoC0V4UX892CGRw8Su6CW4FmQzilBI/NSqJBgD6ULKUBxtsA9 pDf+xwlQWZLQ+eIXXlso1RHa9VEE9hR6jcVbeq06uPOd4XxvayoE1bcz7JEw+EcXU71C mCxA== X-Forwarded-Encrypted: i=1; AJvYcCVSZGhleUDuzypwo1zF94/JBV9n9hYLLGI6lcm3gFyvu+iIFTkKnHNeURRVt1Dab7LpEgG0OYG75oYdIIol+Eb+RkQ= X-Gm-Message-State: AOJu0Yx3MWHXH8lVWcd5rrJAc54fSyRLx1XuAw/M6VnhYqWu646Mrda8 PDGJhPIMhW2wnVToRDFDb9AEqWAZmg06tdK3VnoRA+8yomMVx8dl X-Google-Smtp-Source: AGHT+IHQjlqF6rwlkFyoaljIk9O9zBO+WkkXZoER6nk04i+5Kfvv4yHDqz5cR5JdPILe0xHkL/rhfQ== X-Received: by 2002:a05:622a:4c:b0:440:5fa0:5f2b with SMTP id d75a77b69052e-4405fa060c0mr59056201cf.44.1718021906662; Mon, 10 Jun 2024 05:18:26 -0700 (PDT) Received: from localhost (fwdproxy-ash-117.fbsv.net. [2a03:2880:20ff:75::face:b00c]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-440c9fed96asm9816111cf.17.2024.06.10.05.18.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jun 2024 05:18:26 -0700 (PDT) From: Usama Arif To: akpm@linux-foundation.org Cc: hannes@cmpxchg.org, 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 v3 2/2] mm: remove code to handle same filled pages Date: Mon, 10 Jun 2024 13:16:00 +0100 Message-ID: <20240610121820.328876-3-usamaarif642@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240610121820.328876-1-usamaarif642@gmail.com> References: <20240610121820.328876-1-usamaarif642@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: A1E3D40019 X-Stat-Signature: o9zhqa6co1odrho8w57fgpzzs145mb1j X-HE-Tag: 1718021907-485054 X-HE-Meta: U2FsdGVkX1//KWWlfny3eGMDsYE7F3oWrAY9w7wWw4Ti1l3FRlcNeHsX6PS2n56MXI0dtwYtGlnChOQVBYFuBC6OXsHlq29T8rk5c3EKTBKWWqFoxTek+pnlFEkzQuAYL/qJXMfANKNkCEuPgsb7BSBzLQnvopNeMjFwqrye2gxhdiUOlLl4ebLMx2opBxACiCeJmHvpv4aJgQqgU40L+K/h9zhA5q8Hi7HVmZSpwFslamqE9KcUVkYE7/vQ5WT5jLEoc97DAfinBas5dQ2SaE4f1zbR2zVFa3U/snLXnJkUmnk9UYl9PPlAJWucLREuj6dCfNwcFWWZ9uLvRd+aiDOiJnz1pyLJJ4ifBe3BYHLiaPtu+CJdnjQkT2ELvXsRvbvdWKZoWQ1Qs1QUh7ZRJgFojdPsQsFSFiZjURu75/H78qi36m6Iy1U/Qqc7yvZzyHtE2/flQReIEQXfNGiu6n1XIg9ae76++Wl3oka/Ai6J2fMOphwhxDUhN2qtK2Ui+EwlifITDNdrSnlfFGMo2NhJ91yJs04UB5iPIW7FGztfTNw7upkcR0JAe9sOVtxs9CUXy7ZsXDmQpJvzP+WPaZYuxpP8N5IEwvPHLP39e8RvSjqZFN9/3u2CKXoIIbemORvcwJkLMEqSvfsw+BRLYrKAbdnsCxI0pbLSWMojeFq0g4vKbNDOb1n/gzgclU9E7UjiNMlGWreNvIlXihWew8mCqrJ+a9I5bKWXw4XY5sDlW2pqFWtrzQaeA158W9zRaFxLIdqM9jQcY3jiRYliHn0JgYoK8qy3PgQ9o5G05GiO+nyQFTOV3iiDpWIFD5if1g4d5meUaRRsn/Bo/kNyg7qLKbq0JL+QX47eysH91tMcF0jynq/PEf72htkKc9+0MkWiq2gWiNCcvK/3Q4Wp4S1+COhB18EKXwOfSteQiWI2DnXjmItAejLPNo6er9ym3v0wSHch1+K4isUaZU3 yXBwfy5q gOFLT98cfVUmdcE8Pc5vkLsdKm1jifDYrgypHGqfx0J/Opm0h6Qa7fcGr0oKVguZMZ7Dlq/OeEektb9xeUoJQ43qD1SDGXXGHpUCCag2NMzt9T0qTXD4OoSlRqY6MIHJXPyX7Znm2M+V1TRvnRPVmx4N/1WXzQK8PJTOBAeJNuaho/UjbRx6f+u0QY809FKSstD7/J9ZEBCpk23i8eAPYqDBFmZDSa7G/yK5quJW4kDrsiq+C+O3obz8d4e9erfPO8A/TO7AG151Yh7tnAeOfVMynyvU6izvm6Zvej/eURXsK6f66sMz8Z1SqZrwIr+n1pOsAO4/MEJ2fUFN/jeEOLjS6TbVwyysX/BW/d8n7qYCMgTjywtc1mw5GYbR3+L+W7OygMk2aATXGRdeILHZJIrDrMeVDnHLF1fWypTO95iSGP83at/fdN4iCGw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000023, 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; }