From nobody Tue Dec 16 14:38:19 2025 Received: from mail-qt1-f177.google.com (mail-qt1-f177.google.com [209.85.160.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 54A4A8488 for ; Fri, 23 Aug 2024 19:05:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724439951; cv=none; b=Bp0am67KHA6tAr6mw98FLqG9TFxfvgWIjm1BgTp3fHHFyEpcP9QBVLTf8aGQIa6KnipZd2zVhj352KYhovbgUYxyW5ZQeEPy1+JlKDlIRuuWMejZKp9HEd/keWLWoUJie1mvhNeUzMXG2G1NABnkRfAmRm15A9iKbl9qbIS/nyM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724439951; c=relaxed/simple; bh=fVTGGiHrqbCOMH3q46MQofI2l1zDRxRqjSG+naZ+Feo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=heIV4v8N/V018yq5Wb30bYohsk/Jtw7nT+gO6zOYiYlFBlSCICxrYlTnU/nzeTLCTCYNSFqFslkV6blSvmCy0cItRI0A9sSdm1z0WvGqme2PMCeEJxBQngltR25agtBzFRTozzCVHxLaOcjWOP+j+0eid3wCckr6ixzhc8Qza5c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=h3n1oMwN; arc=none smtp.client-ip=209.85.160.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="h3n1oMwN" Received: by mail-qt1-f177.google.com with SMTP id d75a77b69052e-44fe106616eso12673431cf.1 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=1724439948; x=1725044748; darn=vger.kernel.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=h3n1oMwN7wPj32tfXI16l/0k3B3VUz57If5zLoZ0TbeS3i9MobJpIkkJYdWAAKvlAd VMISAkE2C8XzaSsetEG0ujlkbUxLyMJj4ekpV4COUr2gjG+NvHvKjHkdai04u9D1zHM5 ECyqwoSsEFw87mJRGfltipCea+Z43/IE98DCJrLZoQzMuxhwNJpCv7kUcMAwijusIK7p q+0xDX33DZ3EzJDmPYSdspK8BICtlIviotYUbV35bnaokCl4yg9HLRSHXVx3bxvahnjX bikcmflyy4sbhRrQW+59VOps9jjGn0HEiLpekwNhLakX+dRPDmLLhhc4bIH0YacbZdNO fCug== 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=gkppSBLCCjy/oqU6YK51sjdlEBFjxNxN/p3G/dggirdIu9unoO+8U5TnqSnwFcBBHR 6Go/6kQWke8BT8Wb3GsU5EElV0/22U4nlOHslC3Dged+76JgXsQ0sldZvD2tE9n+XeKq Yew6nGSAJ3oIwkV3pnBbFjvprvAnuYw3fkk173qtdYthMzP/HzGIXzyeHBOoR63c/xOa nzbL53qtImxsupVgce5bGZwMJ3pghSEhDkfhI2wmG7PFFMCSoI1Xd+ORcMdvuuQwQk3J 2CsLHOvefZnVF2m4fmcme0JhP4AUniefnfogkn8MxbSimbJ88RgiZbvCFXY11r/Yjf70 BJTw== X-Forwarded-Encrypted: i=1; AJvYcCXlX/+eTB+FdOA5MrHNvDDxYbT+XL9gc98+oahEckSzJ7Aj7vCO1R0Auo18g7+26vvcrw5MSrXNMpVuKSg=@vger.kernel.org X-Gm-Message-State: AOJu0YyCm75/+3itDIrM79XprYduHbIo5UHLI1Go7JMhw/LmsY2KxfgH ENW+/OZJNWsICRDHvjrjALKD494dwWsn9m6P4VlfsJo6mrK2S49n 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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/20171018104832epcms5p1b2232e2236258de3d03d1= 344dde9fce0@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; } =20 +static bool is_folio_zero_filled(struct folio *folio) +{ + unsigned int pos, last_pos; + unsigned long *data; + unsigned int i; + + last_pos =3D PAGE_SIZE / sizeof(*data) - 1; + for (i =3D 0; i < folio_nr_pages(folio); i++) { + data =3D 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 =3D 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 =3D swp_swap_info(folio->swap); + swp_entry_t entry; + unsigned int i; + + for (i =3D 0; i < folio_nr_pages(folio); i++) { + entry =3D 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 =3D swp_swap_info(folio->swap); + swp_entry_t entry; + unsigned int i; + + for (i =3D 0; i < folio_nr_pages(folio); i++) { + entry =3D 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 =3D swp_swap_info(folio->swap); + swp_entry_t entry; + unsigned int i; + + for (i =3D 0; i < folio_nr_pages(folio); i++) { + entry =3D 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); } =20 +static bool swap_read_folio_zeromap(struct folio *folio) +{ + unsigned int idx =3D swap_zeromap_folio_test(folio); + + if (idx =3D=3D 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 **plu= g) { struct swap_info_struct *sis =3D swp_swap_info(folio->swap); @@ -517,7 +630,10 @@ void swap_read_folio(struct folio *folio, struct swap_= iocb **plug) } delayacct_swapin_start(); =20 - 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 *s= i, unsigned long offset, unsigned long begin =3D offset; unsigned long end =3D 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 w= rites. + */ + for (i =3D 0; i < nr_entries; i++) + clear_bit(offset + i, si->zeromap); =20 if (offset < si->lowest_bit) si->lowest_bit =3D offset; @@ -2607,7 +2615,8 @@ static int swap_node(struct swap_info_struct *si) =20 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; =20 @@ -2632,6 +2641,7 @@ static void setup_swap_info(struct swap_info_struct *= si, int prio, } si->swap_map =3D swap_map; si->cluster_info =3D cluster_info; + si->zeromap =3D zeromap; } =20 static void _enable_swap_info(struct swap_info_struct *si) @@ -2660,11 +2670,12 @@ static void _enable_swap_info(struct swap_info_stru= ct *si) =20 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_struc= t *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 *, special= file) { struct swap_info_struct *p =3D 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 *, special= file) p->max =3D 0; swap_map =3D p->swap_map; p->swap_map =3D NULL; + zeromap =3D p->zeromap; + p->zeromap =3D NULL; cluster_info =3D p->cluster_info; p->cluster_info =3D NULL; spin_unlock(&p->lock); @@ -2841,6 +2855,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) free_percpu(p->cluster_next_cpu); p->cluster_next_cpu =3D 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 *, specialf= ile, int, swap_flags) sector_t span; unsigned long maxpages; unsigned char *swap_map =3D NULL; + unsigned long *zeromap =3D NULL; struct swap_cluster_info *cluster_info =3D NULL; struct page *page =3D NULL; struct inode *inode =3D NULL; @@ -3428,6 +3444,17 @@ SYSCALL_DEFINE2(swapon, const char __user *, special= file, int, swap_flags) goto bad_swap_unlock_inode; } =20 + /* + * Use kvmalloc_array instead of bitmap_zalloc as the allocation order mi= ght + * be above MAX_PAGE_ORDER incase of a large swap file. + */ + zeromap =3D kvmalloc_array(BITS_TO_LONGS(maxpages), sizeof(long), + GFP_KERNEL | __GFP_ZERO); + if (!zeromap) { + error =3D -ENOMEM; + goto bad_swap_unlock_inode; + } + if (si->bdev && bdev_stable_writes(si->bdev)) si->flags |=3D SWP_STABLE_WRITES; =20 @@ -3503,7 +3530,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialf= ile, int, swap_flags) if (swap_flags & SWAP_FLAG_PREFER) prio =3D (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); =20 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 *, specialf= ile, int, swap_flags) si->flags =3D 0; spin_unlock(&swap_lock); vfree(swap_map); + kvfree(zeromap); kvfree(cluster_info); if (inced_nr_rotate_swap) atomic_dec(&nr_rotate_swap); --=20 2.43.5 From nobody Tue Dec 16 14:38:19 2025 Received: from mail-qk1-f177.google.com (mail-qk1-f177.google.com [209.85.222.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 633231922F8 for ; Fri, 23 Aug 2024 19:05:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724439952; cv=none; b=ksRn4rTbAWCcVWxiFc4Z9XTWACQgrZw4sLHpd69pBkv5Pb+VPMCQFCQ4YjfJKPEqsu55mUF++8JDKQOW4hAz/VTxZ2j8/pkeGSffmocEMJM+e/hR5hhpIVa3P4nvHtWrosDOeaiX88Qo2+u4Ev5a5ClPO355zZOvXrtC94GXUts= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724439952; c=relaxed/simple; bh=4WcXynEz0+i6ixsIVhC2fUIHR6r8/g52R5NBK/cUdLo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=jHn53XIWaCJFpePvyBNzW68m/oh5Rqw4EOkTzdYh++QoENmjzzKVFhKAfCW6oDJUj9rgMahbCCivAVXYg74idsbweJdcav/SautCUy2sN+BRPy/0Ru2LQXsdbW4tYzx2gqZbvRcwJeWFV6oG63nTkSdhIsBZFQ0FOL0XFuPFJgw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=DqYdlb0t; arc=none smtp.client-ip=209.85.222.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DqYdlb0t" Received: by mail-qk1-f177.google.com with SMTP id af79cd13be357-7a1df0a9281so140094685a.1 for ; Fri, 23 Aug 2024 12:05:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724439949; x=1725044749; darn=vger.kernel.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=DqYdlb0tk6g9EnY+X/jLqN7X0m/telmcsBy30hJLBeeUdYmKqBPBP6SblBlrAD7Zh3 XVDRk76pft7YnbXcolVW0SAlV1XlTFxyuPlyVzaQqOIyZSXTTe+JhrL+/tVAwO0fJU4L dnNb97db7cUfaoBMrN/2YMAv03MEE+hQr0Dp/38gCFX7h7nhydMRqjY0D//k4Trraw9x Bq8eGMf9R6mlQHHZuKFq4bt+ggNXDhNZBcl5k1kVKQCpPVWB9pU+a1xt1lL+CJFNM9PW kTlIiKmjBw2KtQzKZPuycjZLaR3Zhl82DCZkX2m9XMt9DcEcNp3mjDdV/UgzCd+tyh6+ xMGw== 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=CVd1QMeQCSphRhAeewKiDiQbkMnHCUUbhB6JxJra1f71Sous3YUsy4QhodUdMalXwK CYEV0Wn3EX1fwdyFxih/HLr8CVCtEhtWkp8CuC+c5If58VfyxutlF3DyX2CwvLpMi6MC Wb1m9XyGFgUrCfoCmATeHRpZ2KWz9B0KC03wYY3gdkgiefqIzzCjQZTK9ZUd0+Y5hnq6 rEmJsj8bOVlS/QiRGHrRbN5uI+Gl0M5D3ZbhpBRFOEudqHoE+6w01Bvz4AImw/fIEzSk OwvvZ2RmbKoZgStPHA7RPnuM00qakR6p5QRg5K+uBJk6E4Sec3NtN5JB22Loa3mslqgc 1lqw== X-Forwarded-Encrypted: i=1; AJvYcCWsODNVlXWX5l6YhDM/hphsUBOusMxnn8+hMqdR/INBBhNlVv/t87IB3mtfYh62jxhe9byptiHvsHKuPaE=@vger.kernel.org X-Gm-Message-State: AOJu0Yx+h1RAfcP740ROI5gm4P0+cwERbsGUEU5CADtBUDaMtnr3nUXe LHqdY7NcxcO/J3R7+qD2LT19EtYB9U+LLO8tduAaTGGUFwyxK146 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 =3D ATOMIC_INIT(0); -/* The number of same-value filled pages currently stored in zswap */ -static atomic_t zswap_same_filled_pages =3D ATOMIC_INIT(0); =20 /* * 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 b= oth - * pool and lru are invalid and must be ignored. + * decompression. * referenced - true if the entry recently entered the zswap pool. Unset b= y the * writeback logic. The entry is only reclaimed by the writeb= ack * 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 shr= inker *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); } =20 -/********************************* -* 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 =3D PAGE_SIZE / sizeof(*data) - 1; - bool ret =3D false; - - data =3D kmap_local_folio(folio, 0); - val =3D data[0]; - - if (val !=3D data[last_pos]) - goto out; - - for (pos =3D 1; pos < last_pos; pos++) { - if (val !=3D data[pos]) - goto out; - } - - *value =3D val; - ret =3D true; -out: - kunmap_local(data); - return ret; -} - -static void zswap_fill_folio(struct folio *folio, unsigned long value) -{ - unsigned long *data =3D 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 =3D NULL; struct mem_cgroup *memcg =3D NULL; - unsigned long value; =20 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; } =20 - if (zswap_is_folio_same_filled(folio, &value)) { - entry->length =3D 0; - entry->value =3D value; - atomic_inc(&zswap_same_filled_pages); - goto store_entry; - } - /* if entry is successfully added, it keeps the reference */ entry->pool =3D 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; =20 -store_entry: entry->swpentry =3D swp; entry->objcg =3D objcg; entry->referenced =3D true; @@ -1569,13 +1509,9 @@ bool zswap_store(struct folio *folio) return true; =20 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; =20 - if (entry->length) - zswap_decompress(entry, folio); - else - zswap_fill_folio(folio, entry->value); + zswap_decompress(entry, folio); =20 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); =20 return 0; } --=20 2.43.5