From patchwork Fri Aug 23 19:04:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usama Arif X-Patchwork-Id: 13775726 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 0DB5CC5472C for ; Fri, 23 Aug 2024 19:05:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9572C6B027C; Fri, 23 Aug 2024 15:05:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 92B966B027E; Fri, 23 Aug 2024 15:05:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7A5666B027F; Fri, 23 Aug 2024 15:05:51 -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 5DB0E6B027C for ; Fri, 23 Aug 2024 15:05:51 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 196891A143D for ; Fri, 23 Aug 2024 19:05:51 +0000 (UTC) X-FDA: 82484439702.30.8C3140C Received: from mail-qt1-f178.google.com (mail-qt1-f178.google.com [209.85.160.178]) by imf10.hostedemail.com (Postfix) with ESMTP id 16D72C0018 for ; Fri, 23 Aug 2024 19:05:48 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="DxjCTo/q"; spf=pass (imf10.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.160.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=1724439839; 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=aiVexH2j1k9sFvPt9jGArMlV3SBSHlCMHUkgq1wlWgo=; b=h7MD46t0WCTUOI18hjlbS3AsskwBp7P/1NL21PwdT4fOAwccAgiMSuJ2OuC485nOlQd61Z ifmKtksYAIYJp+uvcNZscoH73R15Q/jWxIouDXvSlVibiPrMA3johv1PGhFgSD30r6sNHa gFQXkvrdxoMd2DeSkieOUmOeJM0Epls= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="DxjCTo/q"; spf=pass (imf10.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.160.178 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=1724439839; a=rsa-sha256; cv=none; b=vhYRQCpwhu3M/veufbRY2/erJqDHgKdq7tGQ2v+k7j9KXElrxt/IJJIpRgMV7bcyol/s4a 6ECDj/PZYOcUQi8CAgXguweyAY7wtrVKcS98TwbYKf00L7BM7lyMwlYRFiTokT1H4bApBr lb66D6PMGVVZhz01fKHTCZmRlsh8fJ4= Received: by mail-qt1-f178.google.com with SMTP id d75a77b69052e-44ff7cc5432so17479181cf.3 for ; Fri, 23 Aug 2024 12:05:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724439948; x=1725044748; 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=aiVexH2j1k9sFvPt9jGArMlV3SBSHlCMHUkgq1wlWgo=; b=DxjCTo/q6RqzyZoGNyoKFhIt0OPOCBT5IZ+JRJR+PUTV/Qs7/c+b6P/6Av3DMa3cgk HGAS/qMWFasqWLVeEI/g1vUqNBtRkBPRbWn9TdTNbFfKavgtx15rRjoA6Mo5MBRhSqhY HLdni6kAjz/wd89ikL2CCx94ei3vqYGeQJ5mbPe/u24YHVHvhJYoy5XTHvT2hSPo2yTr 4nZzhji7JFRogk6XSXzA8e47hZPUiggOBVmVJjYCdg1ZdNJMGl5DVrqdJ/Q/k5GnhLYO HmxPa8DkeTj1eDF3AV/E+Voohuo1BrRKhqTGD0RouAGfQsEOAH6ROr7W4F0jCto78U2z 7bxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724439948; x=1725044748; 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=aiVexH2j1k9sFvPt9jGArMlV3SBSHlCMHUkgq1wlWgo=; b=Gk1ApMSFJa2Gdc9c+wj0qfPcSfhnVShadXHrhXPHWAnXgbuDn1hlmwCKvkQmUyrY8L SaOTNM89p85ygQCsnf4oyHc87x5LBtswL4uR+C22PHZAL2ONz7spau3PLWMs5287yE9l VMr1ax3PX8ye247LBiwFHhf7izpfF8RaEgJlDtrL3244k7sR1xVN20f2mxoiOb0waurT SOKiTJGmB+ypUf6BFINchZl0QYs8OynS7gmNdiaoB+0v8Z01tbCq6ZQVueXb7siqg6qT EvAxKN/4CoDIZdt/EFe1Xq4XlU3eFD0wHyCx+OYa5LWw/VVvXMZMT9m6Spp1cRil4I4s 2j4A== X-Forwarded-Encrypted: i=1; AJvYcCWLHMtUTs1IcabbR57a9tA04jtZtW7eSSIKjH29wXtZnOpVCwN24lCuOWhmVDIELmmys2KREo2xSw==@kvack.org X-Gm-Message-State: AOJu0Yz1pG/E9xDPLhG8Wpd/vGBrhlXeoJ82osDveiYzXDL6Qg/FfLK+ M55kFvSeTxqyOhPlI2430ucEAHjXkiGlil1dWSjvczHgTEd4cjAW X-Google-Smtp-Source: AGHT+IFvZr3TGqZmBgSCALJLBRrd1whSJ7kwTO8AdQ4F1ejzeuSL8L8LZNOSIAtT/ZrnYyxamWOI5w== X-Received: by 2002:a05:622a:4805:b0:446:5368:cce4 with SMTP id d75a77b69052e-45509d37d36mr38680821cf.48.1724439947948; Fri, 23 Aug 2024 12:05:47 -0700 (PDT) Received: from localhost (fwdproxy-ash-013.fbsv.net. [2a03:2880:20ff:d::face:b00c]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-454fe1b7d2asm19823311cf.93.2024.08.23.12.05.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Aug 2024 12:05:47 -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 , Andi Kleen Subject: [RESEND v8 1/2] mm: store zero pages to be swapped out in a bitmap Date: Fri, 23 Aug 2024 20:04:39 +0100 Message-ID: <20240823190545.979059-2-usamaarif642@gmail.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240823190545.979059-1-usamaarif642@gmail.com> References: <20240823190545.979059-1-usamaarif642@gmail.com> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 16D72C0018 X-Stat-Signature: 98ojphjgqqtbosksfyzkcr6j4ca1cafi X-Rspam-User: X-HE-Tag: 1724439948-866778 X-HE-Meta: U2FsdGVkX1/4LTg3ZuN/gdgmfdy/fqGQdHYm5yEO+1h4wj91wpMRwXdpm4xj+vhl0EikfjnTk+F4/ojLOZNNzjCNiFpJUIon7/erCIjrb4FIw+c1LRqvD/hMZ5+EmAODo6zWm3pP9MA8ZbXioGuTcVbDuhPN+VdFncW2NNO6L28KR9AEXHPum4XX3zP/aaNnZbLD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JWQiF6GMqzthftVB7wt9RSc3Y3b7HjGyCNnV5sQP3sez36cPbDXnF9GgyYtRhAqM6eEAc2HjZXrWfMZs+dNYmjwdD+igJ01Q7YZygjdNLBxLITikmoFgZ4zpnc/v81V/ANnAdp9uXfQ+fj4dfMS9tam5CgtkEk5hEu6ShRq0dn1018jjir6gS/2aRh0V/HytBJuNzSRCNI4vJj8So0GpRHB3jiIPucDE3Uoum4TfBYWpg7RL2j39CEE0zOr7hku4Wf0EXQ1U613SE1ndR5R5yQi6n4PczSOMDZR/uuO8vekG4jYkojxp2lj1pPjNtyGzRzpymburE5LoJ5JU+ex5z68B7pAS06pgq+qXFa03e9/qL6jQL7jGyN66Uz9 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 Cc: David Hildenbrand Cc: Hugh Dickins Cc: Johannes Weiner Cc: Matthew Wilcox (Oracle) Cc: Shakeel Butt Cc: Usama Arif Cc: Andi Kleen Signed-off-by: Andrew Morton Acked-by: Johannes Weiner --- include/linux/swap.h | 1 + mm/page_io.c | 118 ++++++++++++++++++++++++++++++++++++++++++- mm/swapfile.c | 38 ++++++++++++-- 3 files changed, 151 insertions(+), 6 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 248db1dd7812..ca533b478c21 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -296,6 +296,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; /* kvmalloc'ed bitmap to track zero pages */ struct swap_cluster_info *cluster_info; /* cluster info. Only for SSD */ struct list_head free_clusters; /* free clusters list */ struct list_head full_clusters; /* full clusters list */ diff --git a/mm/page_io.c b/mm/page_io.c index a00e2f615118..b6f1519d63b0 100644 --- a/mm/page_io.c +++ b/mm/page_io.c @@ -172,6 +172,80 @@ int generic_swapfile_activate(struct swap_info_struct *sis, goto out; } +static bool is_folio_zero_filled(struct folio *folio) +{ + unsigned int pos, last_pos; + unsigned long *data; + unsigned int i; + + last_pos = PAGE_SIZE / sizeof(*data) - 1; + for (i = 0; i < folio_nr_pages(folio); i++) { + data = kmap_local_folio(folio, i * PAGE_SIZE); + /* + * Check last word first, incase the page is zero-filled at + * the start and has non-zero data at the end, which is common + * in real-world workloads. + */ + if (data[last_pos]) { + kunmap_local(data); + return false; + } + for (pos = 0; pos < last_pos; pos++) { + if (data[pos]) { + kunmap_local(data); + return false; + } + } + kunmap_local(data); + } + + return true; +} + +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 +269,25 @@ int swap_writepage(struct page *page, struct writeback_control *wbc) folio_unlock(folio); return ret; } + + /* + * Use a bitmap (zeromap) to avoid doing IO for zero-filled pages. + * The bits in zeromap are protected by the locked swapcache folio + * and atomic updates are used to protect against read-modify-write + * corruption due to other zero swap entries seeing concurrent updates. + */ + if (is_folio_zero_filled(folio)) { + swap_zeromap_folio_set(folio); + folio_unlock(folio); + return 0; + } else { + /* + * Clear bits this folio occupies in the zeromap to prevent + * zero data being read in from any previous zero writes that + * occupied the same swap entries. + */ + swap_zeromap_folio_clear(folio); + } if (zswap_store(folio)) { folio_unlock(folio); return 0; @@ -427,6 +520,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_range(folio, 0, folio_size(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); @@ -517,7 +630,10 @@ 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); + goto finish; + } else if (zswap_load(folio)) { folio_unlock(folio); goto finish; } diff --git a/mm/swapfile.c b/mm/swapfile.c index aff73a3d0ead..1cfb7c63cdcc 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -940,6 +940,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; @@ -2607,7 +2615,8 @@ static int swap_node(struct swap_info_struct *si) static void setup_swap_info(struct swap_info_struct *si, int prio, unsigned char *swap_map, - struct swap_cluster_info *cluster_info) + struct swap_cluster_info *cluster_info, + unsigned long *zeromap) { int i; @@ -2632,6 +2641,7 @@ static void setup_swap_info(struct swap_info_struct *si, int prio, } si->swap_map = swap_map; si->cluster_info = cluster_info; + si->zeromap = zeromap; } static void _enable_swap_info(struct swap_info_struct *si) @@ -2660,11 +2670,12 @@ static void _enable_swap_info(struct swap_info_struct *si) static void enable_swap_info(struct swap_info_struct *si, int prio, unsigned char *swap_map, - struct swap_cluster_info *cluster_info) + struct swap_cluster_info *cluster_info, + unsigned long *zeromap) { spin_lock(&swap_lock); spin_lock(&si->lock); - setup_swap_info(si, prio, swap_map, cluster_info); + setup_swap_info(si, prio, swap_map, cluster_info, zeromap); spin_unlock(&si->lock); spin_unlock(&swap_lock); /* @@ -2682,7 +2693,7 @@ static void reinsert_swap_info(struct swap_info_struct *si) { spin_lock(&swap_lock); spin_lock(&si->lock); - setup_swap_info(si, si->prio, si->swap_map, si->cluster_info); + setup_swap_info(si, si->prio, si->swap_map, si->cluster_info, si->zeromap); _enable_swap_info(si); spin_unlock(&si->lock); spin_unlock(&swap_lock); @@ -2707,6 +2718,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) { struct swap_info_struct *p = NULL; unsigned char *swap_map; + unsigned long *zeromap; struct swap_cluster_info *cluster_info; struct file *swap_file, *victim; struct address_space *mapping; @@ -2829,6 +2841,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) p->max = 0; swap_map = p->swap_map; p->swap_map = NULL; + zeromap = p->zeromap; + p->zeromap = NULL; cluster_info = p->cluster_info; p->cluster_info = NULL; spin_unlock(&p->lock); @@ -2841,6 +2855,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) free_percpu(p->cluster_next_cpu); p->cluster_next_cpu = NULL; vfree(swap_map); + kvfree(zeromap); kvfree(cluster_info); /* Destroy swap account information */ swap_cgroup_swapoff(p->type); @@ -3338,6 +3353,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) sector_t span; unsigned long maxpages; unsigned char *swap_map = NULL; + unsigned long *zeromap = NULL; struct swap_cluster_info *cluster_info = NULL; struct page *page = NULL; struct inode *inode = NULL; @@ -3428,6 +3444,17 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) goto bad_swap_unlock_inode; } + /* + * Use kvmalloc_array instead of bitmap_zalloc as the allocation order might + * be above MAX_PAGE_ORDER incase of a large swap file. + */ + zeromap = kvmalloc_array(BITS_TO_LONGS(maxpages), sizeof(long), + GFP_KERNEL | __GFP_ZERO); + if (!zeromap) { + error = -ENOMEM; + goto bad_swap_unlock_inode; + } + if (si->bdev && bdev_stable_writes(si->bdev)) si->flags |= SWP_STABLE_WRITES; @@ -3503,7 +3530,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) if (swap_flags & SWAP_FLAG_PREFER) prio = (swap_flags & SWAP_FLAG_PRIO_MASK) >> SWAP_FLAG_PRIO_SHIFT; - enable_swap_info(si, prio, swap_map, cluster_info); + enable_swap_info(si, prio, swap_map, cluster_info, zeromap); pr_info("Adding %uk swap on %s. Priority:%d extents:%d across:%lluk %s%s%s%s\n", K(si->pages), name->name, si->prio, nr_extents, @@ -3538,6 +3565,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) si->flags = 0; spin_unlock(&swap_lock); vfree(swap_map); + kvfree(zeromap); kvfree(cluster_info); if (inced_nr_rotate_swap) atomic_dec(&nr_rotate_swap); From patchwork Fri Aug 23 19:04:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usama Arif X-Patchwork-Id: 13775728 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 F3126C52D7C for ; Fri, 23 Aug 2024 19:05:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B51E36B027F; Fri, 23 Aug 2024 15:05:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ADC266B0280; Fri, 23 Aug 2024 15:05:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7CDC8800B4; Fri, 23 Aug 2024 15:05:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 60F566B027F for ; Fri, 23 Aug 2024 15:05:52 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 0B1B9121CF4 for ; Fri, 23 Aug 2024 19:05:52 +0000 (UTC) X-FDA: 82484439744.02.0940B07 Received: from mail-qv1-f46.google.com (mail-qv1-f46.google.com [209.85.219.46]) by imf16.hostedemail.com (Postfix) with ESMTP id 1C39E180009 for ; Fri, 23 Aug 2024 19:05:49 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=JX0vYuef; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf16.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.219.46 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724439889; a=rsa-sha256; cv=none; b=vy3jyTxXAnz1+EtCvczqL4mPnxNX73oxPgs/hTIWy284JXZ1HOKIhifAzB6aOM2ET32A02 UQo3cwO75RDnHEAJ3Ioy/Ykf7hZKmO1lsfVvfMa9Ex0s859Rs8u8JxZs7KpF0nxkGHCVSk XvL3bKuWHjvIdoe3by3hbl31bw42giM= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=JX0vYuef; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf16.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.219.46 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=1724439889; 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=KXz1WqDOSOf0lx3PDODMvA3lWYB2E2RmwkpOqwgzHOw=; b=sGus3kdkptKCEIE4OXVPytUMlxsmju4TdXrmH3boWEqL3hquuJS2QeKvWVL7TmpE0EXwWF OQv8fu+2yNbxImg5Q9x5ZwJZgK1vbCfh1tdTZTJ4wkQXw1qwGLCn4s5MlEMiQWFrSZiasK 69YV6XApfwvDCSQh/KMZ7pEsrEBkMdw= Received: by mail-qv1-f46.google.com with SMTP id 6a1803df08f44-6bf7f4a133aso11673786d6.2 for ; Fri, 23 Aug 2024 12:05:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724439949; x=1725044749; 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=KXz1WqDOSOf0lx3PDODMvA3lWYB2E2RmwkpOqwgzHOw=; b=JX0vYuefLCuJJHA9zuu/zd2jf+e1q6Ii1vbA63Of1UBCg3osINvQLYKslREbFBbRwW yAM7qOx1DhFhm2kTLXLlrrkehlNcp4FNUybnIfWW/4ieyu8i2KqSnQKA7eZV792NVaiR Ui8ELSg71GjuVzh8vLHFMEzGoqbkWqeAlyImE78xXhd8rino+UmGYU3SiBSRGbRMQgLs bBTE2KPRDYX+fap67vYinQEuoKy7dZL/3p7kY30IAnitUIJceWEq0tUuY3GfUnvqhudQ IlNb/crxrOq04qV3LyisKoZUkUa3J0zv3ZBZX/6xiTBucOKYmdJrcR7cbTQm5G5QCKwx 4bDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724439949; x=1725044749; 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=KXz1WqDOSOf0lx3PDODMvA3lWYB2E2RmwkpOqwgzHOw=; b=NxA+F97MJa3ZVVPIFHXziZ3ghFiA/y480owRU/s1YX1yMrmvPXJ7Igl4Xcndjq+ewd w2Ic8bpIfvVrCEtN060jxcwL7Sj8s5k/teMqmhMbpikBIQ5bBhLr2+O6A2cMVRcKG62b wigcC04L+c+mbcBWBZ3j5AUrqx46LxP1wLZZBfu4XQSQmZbu5EAUruE95L7D8RNPIWV4 lZEqYY0mJvp6fu8uU69m5RzFXzVv5LoY2IJtwuORqRfjP2rf4X0jGCWWWec2eJeEtiTW aoo2Rl7AFYQgCy0pye4qBeSohCcePbYpTMjHIh0mE8MC9gq/cLW3rpkL7vuOKDyv2DcE ASHw== X-Forwarded-Encrypted: i=1; AJvYcCUz4VT8dtHskIff608QuMgHeYseSC680SUBp3YHO/85CrMVFjjF7GMYy1lIJxhapwAlcmGevKsFzA==@kvack.org X-Gm-Message-State: AOJu0YzwDl2tjg6JGug1+4k1hEoX90cuQ2Yu4WcKtcQiPffdn229Uhco q9+fmvfZGa0eCcptodLCoOCCR36iHvlWT2zZHigZrrvjPFISapwm X-Google-Smtp-Source: AGHT+IE9Bd7WW/rU0Ll51OR3EAyq5JreQRHH8kzeghLjMl8faLxlPdzbZDil5qdNrBwCfKa0NMZlZA== X-Received: by 2002:a05:6214:3f83:b0:6c1:6b23:dc9 with SMTP id 6a1803df08f44-6c16dcbd32cmr35351236d6.37.1724439949011; Fri, 23 Aug 2024 12:05:49 -0700 (PDT) Received: from localhost (fwdproxy-ash-018.fbsv.net. [2a03:2880:20ff:12::face:b00c]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6c162d4c1c6sm21607346d6.46.2024.08.23.12.05.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Aug 2024 12:05:48 -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 , Andi Kleen Subject: [RESEND v8 2/2] mm: remove code to handle same filled pages Date: Fri, 23 Aug 2024 20:04:40 +0100 Message-ID: <20240823190545.979059-3-usamaarif642@gmail.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240823190545.979059-1-usamaarif642@gmail.com> References: <20240823190545.979059-1-usamaarif642@gmail.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 1C39E180009 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: ab9iy3g5exchixikwhrp35xy7eqs6t5y X-HE-Tag: 1724439949-135344 X-HE-Meta: U2FsdGVkX1/tFNV9zsjBrZnmLstkZ/UfHYLB06tI1/9LgP5d4ID8GJCW97wX3SxW1A7pvl5N07gWP9xKPX2gTvk4B7vBNg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2/w9BBfBO09iE2ILtmWS6Sl/ES6Ffepz4LK30UJ8jA/+pH57rDy/o1G7TbiMxire8lptAdu7+MupMr/bPAiNSFWmWQQPh7dkqXkqzCrJsnQm6hmYNE/9qOLA+Frk01TIf4Dze81UdKviygjy0kMwpQtQ3Z2bG4ZW6A1bbect8LhF4u44eCDpWHjc7w2LuLFP9ohTSsMOXkRK8es97zoYeqhcGSuG/nBZUSUgToETwhnKgDzkJBizrEoU30IsILCy2qhtGmXMjHWeojn9feHpgbBgeumVM5yvh4j7byuZze0TYljPmqJfqzjHhzUmCooMvu50MpqtMkqxRk7VhAdSF0EzQ1Qq9P4oQKClZoLa9wbmJR7OUFAqpVS7xjOvXXuQASZwKnbWq5L/JcwZsCfhO2uaT9r5L6DmtcqaHuJqg42D8flihNh1NqssTcSvtsX40= 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. Reviewed-by: Chengming Zhou Acked-by: Yosry Ahmed Reviewed-by: Nhat Pham Cc: Andi Kleen Cc: David Hildenbrand Cc: Hugh Dickins Cc: Johannes Weiner Cc: Matthew Wilcox (Oracle) Cc: Shakeel Butt Signed-off-by: Andrew Morton Acked-by: Johannes Weiner Signed-off-by: Usama Arif --- mm/zswap.c | 85 +++++------------------------------------------------- 1 file changed, 8 insertions(+), 77 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index df66ab102d27..449914ea9919 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 @@ -185,8 +183,7 @@ 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. * referenced - true if the entry recently entered the zswap pool. Unset by the * writeback logic. The entry is only reclaimed by the writeback * logic if referenced is unset. See comments in the shrinker @@ -202,10 +199,7 @@ struct zswap_entry { unsigned int length; bool referenced; struct zswap_pool *pool; - union { - unsigned long handle; - unsigned long value; - }; + unsigned long handle; struct obj_cgroup *objcg; struct list_head lru; }; @@ -801,13 +795,9 @@ static void zswap_entry_cache_free(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(entry->pool->zpool, entry->handle); - zswap_pool_put(entry->pool); - } + zswap_lru_del(&zswap_list_lru, entry); + zpool_free(entry->pool->zpool, entry->handle); + zswap_pool_put(entry->pool); if (entry->objcg) { obj_cgroup_uncharge_zswap(entry->objcg, entry->length); obj_cgroup_put(entry->objcg); @@ -1277,11 +1267,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); } @@ -1416,42 +1401,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 **********************************/ @@ -1463,7 +1412,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)); @@ -1496,13 +1444,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) @@ -1520,7 +1461,6 @@ bool zswap_store(struct folio *folio) if (!zswap_compress(folio, entry)) goto put_pool; -store_entry: entry->swpentry = swp; entry->objcg = objcg; entry->referenced = true; @@ -1569,13 +1509,9 @@ bool zswap_store(struct folio *folio) return true; store_failed: - if (!entry->length) - atomic_dec(&zswap_same_filled_pages); - else { - zpool_free(entry->pool->zpool, entry->handle); + zpool_free(entry->pool->zpool, entry->handle); put_pool: - zswap_pool_put(entry->pool); - } + zswap_pool_put(entry->pool); freepage: zswap_entry_cache_free(entry); reject: @@ -1638,10 +1574,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) @@ -1744,8 +1677,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; }