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/ExQaDnoTf4bDSeE2GELVREwFGvH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jiXIiJ/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;