From patchwork Thu May 30 10:19:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usama Arif X-Patchwork-Id: 13680131 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 05259C27C43 for ; Thu, 30 May 2024 10:21:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4D43F6B0095; Thu, 30 May 2024 06:21:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 45D396B0096; Thu, 30 May 2024 06:21:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3246A6B0098; Thu, 30 May 2024 06:21:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 1183D6B0095 for ; Thu, 30 May 2024 06:21:48 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id AC24C120ECE for ; Thu, 30 May 2024 10:21:47 +0000 (UTC) X-FDA: 82174671054.11.788D0C1 Received: from mail-yb1-f174.google.com (mail-yb1-f174.google.com [209.85.219.174]) by imf06.hostedemail.com (Postfix) with ESMTP id D81BF180014 for ; Thu, 30 May 2024 10:21:45 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=AO8N3dRX; spf=pass (imf06.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.219.174 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=1717064505; a=rsa-sha256; cv=none; b=uo1eBgcvxi4H1Gox/eVpEG3+vQInO0GvJXF4LfO8PWEj6OFUgquuD7cGvSX2XIbEGmTbKa JMKI25syZtLVJqm/Zn5lUXFjS+Wv9ZHAatqZ/c+oT81maYMY77O2bTshaE5smD0VSKbBff ICIdTb9r0KXAQHHp559TBWArFkyDhuA= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=AO8N3dRX; spf=pass (imf06.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.219.174 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=1717064505; 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=Uv2ZaVw6OgZTxb8YIYWTp0HwSOfDS6TRl5l+DF4nA8U=; b=BeK9u3cz60CWZS7meaEmg/n30n3W35U/wshhEDdRivckxy6bBOHazBQd5AMbl7bcN1+nue XFNCyYymbMKBzTGC9c0j7Pj/fQq9I9EfAcYLq0pWHfYXxgLlVthKS6b83R5Es//sIRllIt +PyIjiCp4CoorIAw0paggGTtYeuQbM0= Received: by mail-yb1-f174.google.com with SMTP id 3f1490d57ef6-dfa65af5367so283081276.1 for ; Thu, 30 May 2024 03:21:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717064505; x=1717669305; 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=Uv2ZaVw6OgZTxb8YIYWTp0HwSOfDS6TRl5l+DF4nA8U=; b=AO8N3dRX5D0j/YBn6Z+5MedL48niJOrNJjNv0X2RZ41bIJXFNjTF+WwqhwOTQuZKV0 ikYrOxVPPZEwUvbErUiDmZOvkgOlvB8LLHhwaDHOk4lhHFfQHci/gubSjIrtAQMQef6E 0dfdhfqANNfj7oh2SGYjaEAnkcHJi4DOMdtcUGw9ohoXTtsiViIGoQn6H97FieDWSX1p GiLYI6yuHzqf9QrRyyg2DZTWNOk5306dunLFZWLxsPnokmgrC1BkrOarUgdwmmIflmc/ 8buLmkrwPvx4xiwM/lojrrlF9hsXnNgKHs1DAd8waK8tiYsHgozO6BqxEe+pzSmxxGAy EvTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717064505; x=1717669305; 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=Uv2ZaVw6OgZTxb8YIYWTp0HwSOfDS6TRl5l+DF4nA8U=; b=OpK+/sPppbKN7DDHOTT60y51WJUnCRXLJB7rSy9ViigdwUX9fwP877s0OSzYmxQvIS IxhAzSbqOwPMrZ5dRzp6j8Sig9aix41q2kFWj+2cxvl+xEzPT9KxSlYsmwcG129Hz4Iq 97OgZJ/dt85CJLweMk8emF1Bd185wiOhJiYRt4CLkbpzRWBK7bs0yZNwSS2a7KyrVUSx jWvRaZZZbFQpRDgpAQeTBqYOJUqWqKyYRzCBzSXvV5UL+f1SDQmmtWs5xXsQKRmaaPSw MTiAlfJZCISmdk83+8epqU0GGpFu9bm6LsWnPes0EoogyDCgXeyH1728fZO1REFgSjUS hrwA== X-Forwarded-Encrypted: i=1; AJvYcCWHFdnGXJgobipdN/IvcerKGEJcdPmXPG5V4FzHXY7bjBJNgs8u2/7D1D+Al41RJgu9YjBWYAFUDf/mVn66PTkAxrg= X-Gm-Message-State: AOJu0YwU1Gh19JU7UeRhnkZNGP+S/idGCYdp0QCvpWagiXZZ568fhyN7 I3MHpMvhLa1c56E1RtkP2JndWkpQ0Hrq8/e3iEo5XePN00XsAgMZ5SByJllL X-Google-Smtp-Source: AGHT+IG2E/LBvRU71yDXoonabPkGi2UQCArX8XpUUuRFH+o0Gt0qc8K+S8vP/fgQu4t2rbZd7Wnm1g== X-Received: by 2002:a25:8389:0:b0:de5:5a6f:a52a with SMTP id 3f1490d57ef6-dfa5a5f219emr2032469276.26.1717064504809; Thu, 30 May 2024 03:21:44 -0700 (PDT) Received: from localhost (fwdproxy-nao-002.fbsv.net. [2a03:2880:23ff:2::face:b00c]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6ae24d5ea8dsm1524366d6.65.2024.05.30.03.21.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 May 2024 03:21:44 -0700 (PDT) From: Usama Arif To: akpm@linux-foundation.org Cc: hannes@cmpxchg.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 1/2] mm: store zero pages to be swapped out in a bitmap Date: Thu, 30 May 2024 11:19:07 +0100 Message-ID: <20240530102126.357438-2-usamaarif642@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240530102126.357438-1-usamaarif642@gmail.com> References: <20240530102126.357438-1-usamaarif642@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: D81BF180014 X-Rspam-User: X-Rspamd-Server: rspam12 X-Stat-Signature: pbqnaabzcpt4gz45mbpukzge6zaumr1z X-HE-Tag: 1717064505-804137 X-HE-Meta: U2FsdGVkX19ZWUQLBIP6m8JWmLaYujf0uirZm0yVe9TDg+DBb26eZP7AZE2MaMaNbxw3bXF+y1nGS1JWvA+caCyrvv/zn38oRWnbdu9SWG0RdoOVipPom4uzq9RVCPuglzfbWgfwAJJHrthJzIDRd/ia7bQJm9aRgeOR1fDTmgQ+BkILlf/JvBkCSm6IYRVBgNgyJhLo89MM18Kk9+w8Tz/JmhiJIzvpOw5Zsta9d5fXdYyZ+ytKNZimsr/rfaj+J99wa7uNH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v+Z3soqsHYLHxtKqgEpqIegqgQXdXb95SPklHmNzSHRmoB3awSgznhMWx+jCmZ5hy99dcKAVfnyG5zBume00aTIhmfUVay3BeRupCeuajFoZZI6dAjizvSmFWQlLvZ3JKYQP7DEaBo= 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 | 86 ++++++++++++++++++++++++++++++++++++++++++-- mm/swapfile.c | 10 ++++++ 3 files changed, 95 insertions(+), 2 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..ab043b4ad577 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -172,6 +172,77 @@ 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 *page; + unsigned int pos; + bool ret = false; + + page = kmap_local_folio(folio, i * PAGE_SIZE); + for (pos = 0; pos < PAGE_SIZE / sizeof(*page); pos++) { + if (page[pos] != 0) + goto out; + } + ret = true; +out: + kunmap_local(page); + 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_page_zero_fill(struct folio *folio, int i) +{ + unsigned long *page; + + page = kmap_local_folio(folio, i * PAGE_SIZE); + memset_l(page, 0, PAGE_SIZE / sizeof(unsigned long)); + kunmap_local(page); +} + +static void folio_zero_fill(struct folio *folio) +{ + unsigned int i; + + for (i = 0; i < folio_nr_pages(folio); i++) + folio_page_zero_fill(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)); + bitmap_set(sis->zeromap, swp_offset(entry), 1); + } +} + +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 +266,14 @@ 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; + } if (zswap_store(folio)) { folio_start_writeback(folio); folio_unlock(folio); @@ -515,8 +594,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..3f00a1cce5ba 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -461,6 +461,7 @@ static void swap_cluster_schedule_discard(struct swap_info_struct *si, */ memset(si->swap_map + idx * SWAPFILE_CLUSTER, SWAP_MAP_BAD, SWAPFILE_CLUSTER); + bitmap_clear(si->zeromap, idx * SWAPFILE_CLUSTER, SWAPFILE_CLUSTER); cluster_list_add_tail(&si->discard_clusters, si->cluster_info, idx); @@ -498,6 +499,7 @@ 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); + bitmap_clear(si->zeromap, idx * SWAPFILE_CLUSTER, SWAPFILE_CLUSTER); unlock_cluster(ci); } } @@ -1336,6 +1338,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; + bitmap_clear(p->zeromap, offset, 1); dec_cluster_info_page(p, p->cluster_info, offset); unlock_cluster(ci); @@ -2597,6 +2600,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 +3127,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;