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; From patchwork Thu May 30 10:19:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usama Arif X-Patchwork-Id: 13680132 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 C5AD0C25B74 for ; Thu, 30 May 2024 10:21:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 216C76B0096; Thu, 30 May 2024 06:21:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 177C16B0098; Thu, 30 May 2024 06:21:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E961F6B0099; Thu, 30 May 2024 06:21:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C1CC46B0096 for ; Thu, 30 May 2024 06:21:49 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 8A2BAA2658 for ; Thu, 30 May 2024 10:21:49 +0000 (UTC) X-FDA: 82174671138.05.04BEBA7 Received: from mail-qk1-f178.google.com (mail-qk1-f178.google.com [209.85.222.178]) by imf19.hostedemail.com (Postfix) with ESMTP id C34391A0015 for ; Thu, 30 May 2024 10:21:47 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Xnziqy8g; spf=pass (imf19.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.222.178 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=1717064507; 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=r8d6cDNDU6GShGq9eK0Fgxfj+4gyetgt78E216drysY=; b=c2gG+jZzpLjA6B7rmSYAd7TuUWYJImTltRIsD2YWO+qXeZ5YB2kAgmvR2WKJltX8piWagH mgu5OBMQb0ASVM1S1JmYVtoYTBUitrRaT6TPQibt5ntBkakhsv4XRk+W4p1T2vl7lZ50Qr GlAAnZuLciuJesj+0TRoW1mnWCA1NaQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1717064507; a=rsa-sha256; cv=none; b=dKJaBTqFjJ3PspXao+uxOd9YAElZ1YdH8itkkq5URmp3Hdj7XgFVgQ0oyJrXL+6ZJnztko VZF7D0peJah6Ou1AXdU6B5KsQ60xs3j4sAy6t/t4aYK+fcTTyfbITdjy4H0bDF5oRG5Fjz JXWul/J5ruCvgxwsLTu27VjRn7ZmXnM= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Xnziqy8g; spf=pass (imf19.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.222.178 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-qk1-f178.google.com with SMTP id af79cd13be357-794c3aed567so54252485a.2 for ; Thu, 30 May 2024 03:21:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717064507; x=1717669307; 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=r8d6cDNDU6GShGq9eK0Fgxfj+4gyetgt78E216drysY=; b=Xnziqy8gtxjmoov3HlcISedbd6UIrUXu5HfB6nrpxWYHQrDKZsBMJI9bakIrM1VRuz UgDStccNac88ZpmRv3eoe8KtiktpMvIIsdjYilBUHEkR+rDVITS/oRZqJ6FuLWwEHAVH Ts5ivOPsKhjVAB8WTgEZhpNA8+Dl4VaW8yAWwQ9s6jdXQFhbRu3ckLBBnZbbzwepxd4N AR303LySA2q4Tm6vl9SEhVLW93N7ZmcSt7WgDQgMYvQrAocTPCQ8LMXYf1fVfjpe/H1I 3CI1xx5oN9SLZNDiUKQPSzEh1jO1Tyukfn7RvZJDVEo3Ckcld+J6innSyEIKB+KbYEhW hDfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717064507; x=1717669307; 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=r8d6cDNDU6GShGq9eK0Fgxfj+4gyetgt78E216drysY=; b=hgfxl3Ybg4MvnL4GyrvPoMH2wxxpvIGJJZ4hmXtmdAoyLndBDnGXGa1YLaHt2xWtxi md2YSvzAna3Te4OEf53RNHiy7OVEg2VhZMLusulPbxpwRLug48q9A2x+NH+P/bStIJ1q VO4xhaYQsO08RR2XLXonSgaLH+huczmsCzjXSgnI7HWuYF6exXPjZzAmSVKAohQnNDKD YNrl549nv56v6suryXiwaSaoM1VfIueHxvOfPXXb5PYhiyiWz5qI4orKc4Ybn7OEewf2 XoIpujRr/QdBerVSBl2RPs1wjGfqXR1NoEc5+c5Wuc0IHgSsqMYREWyE8pLCA2efvhsN Kb0Q== X-Forwarded-Encrypted: i=1; AJvYcCUWE6SnPtJm+kQE6noiGa5Kp5skeyVesU0cT3EOP9yc/tBjVgegh5LIo2nT0+5QSls1+ZDmRZWVgp85OIvJngrTdzo= X-Gm-Message-State: AOJu0YwpFYYLqoq4SiWlC5d4P4Q3H44z7WvsZdQDAKRxCajqaUWdygTg k4DAWWyb98suGaiNFkwDydd0fV3eM5FvTGYbuF91qwnN6bIpHWWx X-Google-Smtp-Source: AGHT+IF6BgR0E1H6Yi7Z0olWyUfqvRPLWZXsZcDuztfZzFojo07FrRO9l8l3SwT6pG7Yaa1MM+8V+g== X-Received: by 2002:a05:6214:4189:b0:6ab:6ddc:4ba1 with SMTP id 6a1803df08f44-6ae0ccd3deamr16187306d6.60.1717064506651; Thu, 30 May 2024 03:21:46 -0700 (PDT) Received: from localhost (fwdproxy-nao-008.fbsv.net. [2a03:2880:23ff:8::face:b00c]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6ae12306f5bsm3351556d6.118.2024.05.30.03.21.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 May 2024 03:21:46 -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 2/2] mm: remove code to handle same filled pages Date: Thu, 30 May 2024 11:19:08 +0100 Message-ID: <20240530102126.357438-3-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-Server: rspam03 X-Rspamd-Queue-Id: C34391A0015 X-Rspam-User: X-Stat-Signature: 1t7dgt8pgbon89fos5z8ausmnaf9773e X-HE-Tag: 1717064507-556018 X-HE-Meta: U2FsdGVkX19u7Euc/thY1txVef3hY5jYBrGuq5RUghEG4XkzIArjVIK7kRnKGcmwTqhB9oahJUdTPjDWFkv2vtWObAtM044fGaYrpKF+93py6NpyVG7S86UGKwDI5qR4OT4uAGqoUgvYoZ+GGLmlsB7wG4Mmprn7vvrfGOgbffjTwPuBgklLNGWg/xkUvgBBQRP8RMsMHgxk2tfFh35vbexn33+xezlYPVPhu2XQEnimNJR2Dg2E+8NxkTucDia/mJYBTofxA+MzFjXZsSF98zgDbe9psi0flVxse30bRWyJ/sIi8hZ5vNBjQ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+2M02GDxne37o0uEqSwjaW8GE= 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 | 79 +++++------------------------------------------------- 1 file changed, 7 insertions(+), 72 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index b9b35ef86d9b..50c8d402516f 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 **********************************/ @@ -1450,13 +1398,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) @@ -1522,13 +1463,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: @@ -1682,8 +1619,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; }