From patchwork Fri Jun 14 10:07:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usama Arif X-Patchwork-Id: 13698503 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 BDD6EC27C6E for ; Fri, 14 Jun 2024 10:15:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AB7EE6B00BC; Fri, 14 Jun 2024 06:09:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7CCFC6B00FD; Fri, 14 Jun 2024 06:09:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2C5016B014E; Fri, 14 Jun 2024 06:09:19 -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 BC3406B014F for ; Fri, 14 Jun 2024 06:09:12 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 406DDA0681 for ; Fri, 14 Jun 2024 10:09:12 +0000 (UTC) X-FDA: 82229071344.16.50F61DC Received: from mail-qv1-f43.google.com (mail-qv1-f43.google.com [209.85.219.43]) by imf13.hostedemail.com (Postfix) with ESMTP id 565DA2001A for ; Fri, 14 Jun 2024 10:09:10 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="i/D8BAzJ"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf13.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.219.43 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718359748; a=rsa-sha256; cv=none; b=jomDq0fOVZtdVk1N+aMIYtG7kn076zXjcZXRA6z5LgyVpMrc/1E087KMo3qWTmV4gVl4zk XeybjJBBh8OHCYCwhmMD9HuTvjWcCym3gGeiBhbD1Q1NtG1r65WFNyuHRGjKxV9g/DEnnD K+ChEOdlVO34frnV1oZPSbJMFT8n0K8= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="i/D8BAzJ"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf13.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.219.43 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718359748; 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=O6fv8HdnUEDMg3BMIhTRciMdCxiiErbM6c3rC1OGOWE=; b=YSWST1QziXfPByrenN0eNwzR9Jkpx5bu7iNk7DYyrQqsRFMC2Ed2nVR3t51PitvMpYz2Jq ErMPiwd9SOxwz0i9RsWovO79twfBjxK2Au8D6wpJEXnquo7pLY4wZLOhmhyRcrWTHj822g wmMYXcEYu/pJYoEGTSjZ0wUk1VhtpT0= Received: by mail-qv1-f43.google.com with SMTP id 6a1803df08f44-6b072522bd5so9742906d6.2 for ; Fri, 14 Jun 2024 03:09:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718359749; x=1718964549; 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=O6fv8HdnUEDMg3BMIhTRciMdCxiiErbM6c3rC1OGOWE=; b=i/D8BAzJETfEnP73JxZSpXBEmTkM3c0ou0qUwkTyXmZ3dXI54oUkOAAmr/dNLQ46vF wYB95MQYXTkkqODCqi8vhYJ/3yoSbhw5S3laJmpL2MF4DBir4FU+5/cnAycLSnI37kLO 1Cf/QDyYiK1SMtzW8ir4LHAA3n/rZkf3u9X9XUqAEbEdtNZlkVk20klkhlY5ii7KIvUW DK31EytGqmg1K7OJ29oEqk6nxOLHVrb7aZvxc+lbbE06fcgMLMttf+UOiuVtVPtzhDK2 E33J8+VicNGWfzZfNeb3LnkAhorUYjUUarn+lc3fFZq1P/rSpJkg4Jxvact31t1Qg+EZ Gb2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718359749; x=1718964549; 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=O6fv8HdnUEDMg3BMIhTRciMdCxiiErbM6c3rC1OGOWE=; b=gIlnIjeE3/JubzOe2SJlftXvxrJ1oENO6wl8UiqmQiBn9gwrtZfHGoWFdo3cO0jff9 N2ywf0lNQe/5rOCRfBO5n0fEgVbVzYw6Ihvd4hWuI8WZP7ZXZWiHkaa5FxDg7ZFB24YU U0h1VTDdwDirAMlCbv0Y+1eP4Yfj3oVTwZ5/HneE5lcC0RRtceVRQ3SHHpd/Po7gEiws 7SCppnA8EKIzn2d2WUnP6Cej3IDCFBROlt/HU2DzdD6SrvdXbEwIvKoljF7XzIlUiTaC xTc0odnI/x6dQdhAT61Y6yhPf9+qud6YRa7wEqK63PdYsAnJh58Ub2xU+30HQ0TDfkbL ip9Q== X-Forwarded-Encrypted: i=1; AJvYcCXWnfRiqyOQomjvJA+JZHfm6Yt18WhjJnFyS5nuCw0wL+XQPLZtUP5Ep4ntK9JCtkYzFi/FQ1ydOfkFZwCMJOmHmxo= X-Gm-Message-State: AOJu0YxaGooeuyI1f345p+WmK7UBHwjQ3nQXqsRA19LGhEaOs+tbo9sd 5y0YYuTlntjJclCJlOtgwEFRjzOXOuo8dx3796XOnwZP7mMvwU6j X-Google-Smtp-Source: AGHT+IGB51PlczTS+Q8lC/8MDqy7gI3RzP4surNROYc6BI+M5ZcIlbCQASDwUV5uflux1tzhLPLPaA== X-Received: by 2002:ad4:590c:0:b0:6b0:4542:e42e with SMTP id 6a1803df08f44-6b2afcc0ebfmr20182046d6.28.1718359748441; Fri, 14 Jun 2024 03:09:08 -0700 (PDT) Received: from localhost (fwdproxy-ash-116.fbsv.net. [2a03:2880:20ff:74::face:b00c]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b2a5c1ceadsm16455526d6.43.2024.06.14.03.09.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 03:09:07 -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 v5 1/2] mm: store zero pages to be swapped out in a bitmap Date: Fri, 14 Jun 2024 11:07:05 +0100 Message-ID: <20240614100902.3469724-2-usamaarif642@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240614100902.3469724-1-usamaarif642@gmail.com> References: <20240614100902.3469724-1-usamaarif642@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 565DA2001A X-Stat-Signature: xc79nnoeo37gnpuk5cix3w9by4s8uf88 X-Rspam-User: X-HE-Tag: 1718359750-623626 X-HE-Meta: U2FsdGVkX1+8T5SB3BEbPtH3dme63564xQO/MDaA4XdAmpRsOG/BrELkWyV2YqCiuL+W5Nz6thFcP2uLyotwJQNszwShdHTfZijeTS2aULL3+Oc+X/d/aYqWS0F1Dm1kTvY85A6IbyLOtSl24Ga2ulDL5cPILCNeVsiXqJD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DGKMbDD27Kj7M+xp0/nU02Za5hUOzl6X+YDtvlX/JHJ3d+GqYqWGIlz6Pssp/LSKy0mR1EONURQhhYlAbznYUYlp0RVPNI2RxcopnFUFNIwW/6X9zUWjpt3zlgbhJQ6ezD8i7LovI= 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 Reviewed-by: Chengming Zhou Reviewed-by: Yosry Ahmed Reviewed-by: Nhat Pham --- include/linux/swap.h | 1 + mm/page_io.c | 113 ++++++++++++++++++++++++++++++++++++++++++- mm/swapfile.c | 15 ++++++ 3 files changed, 128 insertions(+), 1 deletion(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 3df75d62a835..ed03d421febd 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 6c1c1828bb88..480b8f221d90 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_unlock(folio); return 0; @@ -424,6 +513,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); @@ -514,7 +623,9 @@ void swap_read_folio(struct folio *folio, struct swap_iocb **plug) } delayacct_swapin_start(); - if (zswap_load(folio)) { + if (swap_read_folio_zeromap(folio)) { + folio_unlock(folio); + } else if (zswap_load(folio)) { folio_unlock(folio); } else if (data_race(sis->flags & SWP_FS_OPS)) { swap_read_folio_fs(folio, plug); diff --git a/mm/swapfile.c b/mm/swapfile.c index 9c6d8e557c0f..0b8270359bcf 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -747,6 +747,14 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset, unsigned long begin = offset; unsigned long end = offset + nr_entries - 1; void (*swap_slot_free_notify)(struct block_device *, unsigned long); + unsigned int i; + + /* + * Use atomic clear_bit operations only on zeromap instead of non-atomic + * bitmap_clear to prevent adjacent bits corruption due to simultaneous writes. + */ + for (i = 0; i < nr_entries; i++) + clear_bit(offset + i, si->zeromap); if (offset < si->lowest_bit) si->lowest_bit = offset; @@ -2635,6 +2643,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); @@ -3161,6 +3170,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 Fri Jun 14 10:07:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usama Arif X-Patchwork-Id: 13698501 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 92647C27C75 for ; Fri, 14 Jun 2024 10:15:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 07D836B00B8; Fri, 14 Jun 2024 06:09:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D43966B00FA; Fri, 14 Jun 2024 06:09:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 778756B00BC; Fri, 14 Jun 2024 06:09:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 6828B6B0150 for ; Fri, 14 Jun 2024 06:09:14 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 0AD06A0673 for ; Fri, 14 Jun 2024 10:09:14 +0000 (UTC) X-FDA: 82229071428.21.A4812DD Received: from mail-qk1-f174.google.com (mail-qk1-f174.google.com [209.85.222.174]) by imf19.hostedemail.com (Postfix) with ESMTP id 3F01F1A0003 for ; Fri, 14 Jun 2024 10:09:12 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=LbNjL2Il; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf19.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.222.174 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1718359750; a=rsa-sha256; cv=none; b=tsBOzAhQxjI5gA9ZGPK+8MVpDLtzHamoF3y0ImpvwO/37sTB/4hu4obyB6VkxhPpQPpVG4 vNwcry6RKZzR8q1NgumGPD2w6hWZ+EIEZdvi0ePNN7MzTLOZdn8yh0Q7B7ZWvzBho5NpRx pXZQOKIOHGWn3n08h7ilgzILRWJz5nY= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=LbNjL2Il; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf19.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.222.174 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1718359750; 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=5EHezwSo6IEpdsNk9twdnn0QxPVkx1ShUAN5bWtyw3s=; b=PxOCbPfK5ex+m3uAhZCNjapQ3pNALkR/eDz4xGoF6RHT78iCaLembnQBgGqr7vPDoMLxpi F54PD7iMh2/59l6J3xiKwYUIajmhE9o1Uv+oL6NF3FNzZXgZyp42AB03mIoMQeFoIiE/kh o9vOhjckKotj5xKLc/nZmfIwSh/UWTw= Received: by mail-qk1-f174.google.com with SMTP id af79cd13be357-795569eedcaso110866085a.3 for ; Fri, 14 Jun 2024 03:09:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718359751; x=1718964551; 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=5EHezwSo6IEpdsNk9twdnn0QxPVkx1ShUAN5bWtyw3s=; b=LbNjL2Il+lEe2J5lCxV9oEr9wefO/B9rh0M73dJ4drd+hKlxbJfb2DCN0J03VZTtTD LEEYv80XE14zRfXQnfUPb8QFsltLl2S5sq1rO3cbeFl414o7wF+yVujQzL2kt71Qd/yy YeFqEP2+CZzA+MnyzaPMjVgi2SraFzMJe8cejwsGx2k6B3Of4QilVaBLwefgGKWkNMdL 9NsTnGkTs+gSY0hmpbazBHa/QgusKZBn9ZTjKRS000O+4l2/ArG4dsdgOG/gpXiBOv2U AlCYXjPOkuy0827l8War1hVLTABZKUrGhgxXcZ08YVEtjC5gtLdC5k6tmM+6uD225+5C qRrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718359751; x=1718964551; 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=5EHezwSo6IEpdsNk9twdnn0QxPVkx1ShUAN5bWtyw3s=; b=YGGoiyUw/9kEesouOclN6nzszwaDq+P95QTsikLyknyY8thtiJ8SSEqVhwy8h9T8OU 9w+oi67oQKItRGQS63lwxMmgMqfiCiRXTBsr5VIFYle2oFQZAVRoq1Fx2uiqa+kTbr8X KfjDfguNFKHAssb7toPa13jSJsX3FTfeK8/o8WGKKyG2iuXReJpr2vgY/Iwed9PE81fX mZq79jbm0PhWA6PcRTdOQ7dF+NSnagaHJZspIm1ljB1M1GDt9P/gRYTMpAGOynx+SJyu ApK64Z58PT9QqJR0rbABcq8Mn3DQhuD4J4Pxh2vU8YW8EYQIUf4OKxR7i/yWfxVYgnm/ HVEg== X-Forwarded-Encrypted: i=1; AJvYcCWXQdWyx53HLGUUu1sCglyvvmlQXRqkhjPfl5MGA5NmdGvnZF+y9dUkIX3eCOQSvHbNjFdpHi4TYPXIC2WaAeQ/S6k= X-Gm-Message-State: AOJu0YxAdJGaPNsFkvuCn6aoYlQX5x8STBphht3JKkIdjQKbhR8gjnCs 4IU8NKKPCIIq5Q7ye3PaboG/wP53vDg1gRsSDxQpADgNGiS3w03VER8h+ayIqyA= X-Google-Smtp-Source: AGHT+IFJEvCuMkaEjbOYT08iEZ5+NTEUu3Vx5zj5E25wX7sKr7Vg2rxYQdpasbTkSVE2OkRKgL3q4g== X-Received: by 2002:a05:620a:444d:b0:795:4df4:d4f6 with SMTP id af79cd13be357-798d243b29fmr254496485a.46.1718359751276; Fri, 14 Jun 2024 03:09:11 -0700 (PDT) Received: from localhost (fwdproxy-ash-002.fbsv.net. [2a03:2880:20ff:2::face:b00c]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-441f2fcc0b9sm14201161cf.65.2024.06.14.03.09.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Jun 2024 03:09:10 -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 v5 2/2] mm: remove code to handle same filled pages Date: Fri, 14 Jun 2024 11:07:06 +0100 Message-ID: <20240614100902.3469724-3-usamaarif642@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240614100902.3469724-1-usamaarif642@gmail.com> References: <20240614100902.3469724-1-usamaarif642@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 3F01F1A0003 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: ahffdbjrxst1dzsb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OzbstZV6ZE90lvLuzfLh4Uj/QgmeZ/Fln2b85lKKpOb9ROMOa7AHMRd3eDb+powFQaR91+TfcfRPGjBr/wlxSUiKe9EvWcA18MGDPbVbKKQDnG9Cxd7heDkZx1pqrswU+SlKtpv9Lxsxesu6/oCV4iZnRmtd9kjIkI6AyH3FMZovhn+Abqah1t/jhSdKX6gPE1opWMH0nFT3dBW+COxOMttl19kZe+ddsBuLpUxdoTg9LcO/JV58nCOTErT6IMYqNVL3vB3DITGrO2H4R5wbliGXMzZGA1kuqo96ekYLuXPO3av+xe2Wlvp1Aj8z54VbcntZ2BrU9C92FewoZA7qa77NQazh/sioF31d0UZyy 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 Reviewed-by: Chengming Zhou Acked-by: Yosry Ahmed Reviewed-by: Nhat Pham --- mm/zswap.c | 86 +++++------------------------------------------------- 1 file changed, 8 insertions(+), 78 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index a546c01602aa..e25a6808c2ed 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 @@ -188,11 +186,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. */ @@ -200,10 +196,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; }; @@ -820,13 +813,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); @@ -1268,11 +1257,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); } @@ -1376,42 +1360,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 **********************************/ @@ -1423,7 +1371,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)); @@ -1456,13 +1403,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) @@ -1480,7 +1420,6 @@ bool zswap_store(struct folio *folio) if (!zswap_compress(folio, entry)) goto put_pool; -store_entry: entry->swpentry = swp; entry->objcg = objcg; @@ -1528,13 +1467,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: @@ -1597,10 +1532,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) @@ -1703,8 +1635,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; }