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;