From nobody Thu Feb 12 12:34:56 2026 Received: from mail-qt1-f173.google.com (mail-qt1-f173.google.com [209.85.160.173]) (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 61AE3176256 for ; Wed, 12 Jun 2024 12:47:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718196477; cv=none; b=L39d6LWhOfT3d/mpcjM8X/pnSsKAqslx/EFS3D+ncdb3vAgPMUu9eoWHZ/JAu4w9p5Q8GsyDrRYHA4T/qrv9QlWsMS+zeL8z/8mn3NpWizMJNhkOJCHJjFcFU6Q/rPYjgb5Lqp6swKQ3L7qYS03DmpTj5fUCvlLBsmSzhFTBDLI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718196477; c=relaxed/simple; bh=gitYfoxKBKX3SHFNxHblEiolXRlKgPqtoJcmsOdNG9c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=n5erE8cOMVEGav1x7JemOsHRfTZJgzdF8I9C1oMuUlXY9Hk8YHoFJUrz3cLHomkWRaNzWECid1J+LmljRMXNfUpf0Dv40KU0SIWs9oZUPkMog19DKB//vaxgimYYQD2sNE2ONuZj8SADh9o3ruYHy4zl5PMYUBw2Of/YmL++hUM= 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=kO2ywnDv; arc=none smtp.client-ip=209.85.160.173 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="kO2ywnDv" Received: by mail-qt1-f173.google.com with SMTP id d75a77b69052e-440530cadc7so12661881cf.0 for ; Wed, 12 Jun 2024 05:47:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718196474; x=1718801274; 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=YA/i9kGTfJC6xvqtw7O5uORmvK7ZuPAipxeerdrc6dg=; b=kO2ywnDv18tSNOdq3xBQBIbd2X+8PMSj1/+oQapThorRTDyup+Zq7TcNJXKcwXQacJ y1iS6G2fhAgGnlVoEiA5HkHphp8ayu9wqgQwDyt6syLwUuZeUDWDcanyqqWHOcY8UCVH 55dVjJ/aT9AqGN2fWVhVV4F9nxE4niR5ALC/xZBjTXDnkHpB+2zJi2vXyv1wv+/AUguR 5AbXhK55woz2zMuNCh8gSmOh4RQdEKsOvrl+5G0IfV5R3SHszhPNR/WUG0KSAG7SanRe jMJaYROJgXcG5Qh0c1zN5ON+zPH6AG2xejSOR8pkp/BWnow5Qnz5BE+iLcpoozcxhgiF NUQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718196474; x=1718801274; 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=YA/i9kGTfJC6xvqtw7O5uORmvK7ZuPAipxeerdrc6dg=; b=JNGI6zTe1mvrd7b3UFhur1EwVaQZEXJNC9qksFi/J+uJTAnL/SkMub1y+nm5R+aCfG WqhsDqne2B6qdmuT2x1xK7Kx4xK/ksgccGzB6o2hmcfXCS5msc2d8x+5hYxVaaO9bvAj lt4z36cWZ7vae/jAwr+/D/h/pQJo1Lsylf0KuyboirlZS+GFg/yqBuRHZcM/c6e9xAdF oLa62fa3fej0xqK2JpILKS+C8j+0sItDD2kIj4pLWXQ0gP/f17DHLEBhdMskEslpAZhB rVfrcQ6VBLlJn7UQ7a614oI2QBl3sjX/SEsTGmc2+CuFfXSsX9lJYzte7oCppN0p0+KI KXUw== X-Forwarded-Encrypted: i=1; AJvYcCUO9bxWYTxR3PnkQNTT1Nviyfp7vh7OarlCssM++PtwOO9txzJr9x85yo8GyaHrjOZHj4HcZ6W2E1Cu3LULbOa6OvRsQGLv5O2y1/Wj X-Gm-Message-State: AOJu0YzWGLWiu1k+Xgej/nZdVamUUDlxojGIXpoFp0hTBfqRdHtaohOW OJD2eFO59U2OsIJ4+D9C0b8VrrGuGQ4A4qQ28jwrSOfHbMHrJYrt X-Google-Smtp-Source: AGHT+IE1CPP/v/4tI/OK9eS0gZXEc9/e7Fh8lfMD02Ajhy/Np6G3I/hIvWhzZWbp9Vcv211/Yhk2Iw== X-Received: by 2002:ac8:5841:0:b0:440:5c2c:4cdb with SMTP id d75a77b69052e-4415abad660mr15732401cf.2.1718196474067; Wed, 12 Jun 2024 05:47:54 -0700 (PDT) Received: from localhost (fwdproxy-ash-001.fbsv.net. [2a03:2880:20ff:1::face:b00c]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-4406107ebf1sm33834781cf.95.2024.06.12.05.47.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 05:47:53 -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 v4 1/2] mm: store zero pages to be swapped out in a bitmap Date: Wed, 12 Jun 2024 13:43:35 +0100 Message-ID: <20240612124750.2220726-2-usamaarif642@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240612124750.2220726-1-usamaarif642@gmail.com> References: <20240612124750.2220726-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 --- include/linux/swap.h | 1 + mm/page_io.c | 114 ++++++++++++++++++++++++++++++++++++++++++- mm/swapfile.c | 24 ++++++++- 3 files changed, 136 insertions(+), 3 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..39fc3919ce15 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; } =20 +static bool is_folio_page_zero_filled(struct folio *folio, int i) +{ + unsigned long *data; + unsigned int pos, last_pos =3D PAGE_SIZE / sizeof(*data) - 1; + bool ret =3D false; + + data =3D kmap_local_folio(folio, i * PAGE_SIZE); + if (data[last_pos]) + goto out; + for (pos =3D 0; pos < PAGE_SIZE / sizeof(*data); pos++) { + if (data[pos]) + goto out; + } + ret =3D true; +out: + kunmap_local(data); + return ret; +} + +static bool is_folio_zero_filled(struct folio *folio) +{ + unsigned int i; + + for (i =3D 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 =3D 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 =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 +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_start_writeback(folio); folio_unlock(folio); @@ -426,6 +515,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_fill(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); @@ -515,8 +624,9 @@ void swap_read_folio(struct folio *folio, bool synchron= ous, psi_memstall_enter(&pflags); } delayacct_swapin_start(); - - if (zswap_load(folio)) { + if (swap_read_folio_zeromap(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..48d8dca0b94b 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -453,6 +453,8 @@ static unsigned int cluster_list_del_first(struct swap_= cluster_list *list, static void swap_cluster_schedule_discard(struct swap_info_struct *si, unsigned int idx) { + unsigned int i; + /* * If scan_swap_map_slots() can't find a free cluster, it will check * si->swap_map directly. To make sure the discarding cluster isn't @@ -461,6 +463,13 @@ static void swap_cluster_schedule_discard(struct swap_= info_struct *si, */ memset(si->swap_map + idx * SWAPFILE_CLUSTER, SWAP_MAP_BAD, SWAPFILE_CLUSTER); + /* + * zeromap can see updates from concurrent swap_writepage() and swap_read= _folio() + * call on other slots, hence use atomic clear_bit for zeromap instead of= the + * non-atomic bitmap_clear. + */ + for (i =3D 0; i < SWAPFILE_CLUSTER; i++) + clear_bit(idx * SWAPFILE_CLUSTER + i, si->zeromap); =20 cluster_list_add_tail(&si->discard_clusters, si->cluster_info, idx); =20 @@ -482,7 +491,7 @@ static void __free_cluster(struct swap_info_struct *si,= unsigned long idx) static void swap_do_scheduled_discard(struct swap_info_struct *si) { struct swap_cluster_info *info, *ci; - unsigned int idx; + unsigned int idx, i; =20 info =3D si->cluster_info; =20 @@ -498,6 +507,8 @@ 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); + for (i =3D 0; i < SWAPFILE_CLUSTER; i++) + clear_bit(idx * SWAPFILE_CLUSTER + i, si->zeromap); unlock_cluster(ci); } } @@ -1059,9 +1070,12 @@ static void swap_free_cluster(struct swap_info_struc= t *si, unsigned long idx) { unsigned long offset =3D idx * SWAPFILE_CLUSTER; struct swap_cluster_info *ci; + unsigned int i; =20 ci =3D lock_cluster(si, offset); memset(si->swap_map + offset, 0, SWAPFILE_CLUSTER); + for (i =3D 0; i < SWAPFILE_CLUSTER; i++) + clear_bit(offset + i, si->zeromap); cluster_set_count_flag(ci, 0, 0); free_cluster(si, idx); unlock_cluster(ci); @@ -1336,6 +1350,7 @@ static void swap_entry_free(struct swap_info_struct *= p, swp_entry_t entry) count =3D p->swap_map[offset]; VM_BUG_ON(count !=3D SWAP_HAS_CACHE); p->swap_map[offset] =3D 0; + clear_bit(offset, p->zeromap); dec_cluster_info_page(p, p->cluster_info, offset); unlock_cluster(ci); =20 @@ -2597,6 +2612,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, special= file) free_percpu(p->cluster_next_cpu); p->cluster_next_cpu =3D NULL; vfree(swap_map); + bitmap_free(p->zeromap); kvfree(cluster_info); /* Destroy swap account information */ swap_cgroup_swapoff(p->type); @@ -3123,6 +3139,12 @@ SYSCALL_DEFINE2(swapon, const char __user *, special= file, int, swap_flags) goto bad_swap_unlock_inode; } =20 + p->zeromap =3D bitmap_zalloc(maxpages, GFP_KERNEL); + if (!p->zeromap) { + error =3D -ENOMEM; + goto bad_swap_unlock_inode; + } + if (p->bdev && bdev_stable_writes(p->bdev)) p->flags |=3D SWP_STABLE_WRITES; =20 --=20 2.43.0 From nobody Thu Feb 12 12:34:56 2026 Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.180]) (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 69922176244 for ; Wed, 12 Jun 2024 12:47:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718196478; cv=none; b=OF9HvH1Mxvs5lVSBuk94RWmOwL11zyEnKtyUIgEksM0UZ7TYTmNvUXfpcJ8Od7N01sE5oPlr2/YIps1HnOEzJuADF7GucFG2CS7SlRJSYS+yZyvQXOg7rd+0Ih7q8AmtUOadjjoePG8ROBq72AqPn6gJmAropjItnfES18zLPNM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718196478; c=relaxed/simple; bh=HefF2syNjxvt876eLES/u8vyCE/hTUTrXSNifQKmNQg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oi4ZC9vftxIcvJQap814IpEfF3NEKdoQrbV8dcYHWiq/0jmZdv31KBXXLwOKdmpLqkkmKB0wv34XBnHA/cVJbqmTjV2lDP4q19hWLTc2gEUa5zoJBwX25uxTZkSQs3RaVCOQ04vWWFWJFtkAHSTDtH37+Okz9jiB5VMdJ0iLm/Y= 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=nejc/pxA; arc=none smtp.client-ip=209.85.128.180 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="nejc/pxA" Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-627e3368394so75241897b3.2 for ; Wed, 12 Jun 2024 05:47:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718196475; x=1718801275; 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=U09nDqdyDjm09ov5OxqAlgrZTEnMx+ri6277Ej4J+SE=; b=nejc/pxAwXHvYEOjiFQeH3aTHsitaRNvp97HwVMc06DtBrYMURQUgWMIh4myLitL49 kjYkzlrywBnbYwwgQDgGp1tU2QzOVaZAOmZmMK2BpZ8lcDg2dKOFmhKFH4WFAc4hKXSN 7rIobZGfxHZE+gcYQhCasfdsO36Wp2BYC/OQJRToApdX12L4LyOyatQle1Cw/hFDPVpX njCKJiYDaFuLLrpiqiSQXYV80io6r+aupfSiV3/fTE0SeZU3ibAUYnaiW6w2RsIDVmXQ tuK/ua/Ol9sydCwNYP/23pulN1A8hu2t8xWsxPw9JYOexYJsDmGv1mERFYudwPiJYBb4 QeKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718196475; x=1718801275; 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=U09nDqdyDjm09ov5OxqAlgrZTEnMx+ri6277Ej4J+SE=; b=qxnjK1e3ceX3HxO+GLgOdisl+ZUocvSvvjLLW6aXzSgn3M/LZ5zRsdr0lt1348vQJ0 h1T7nGKh451LsA5qHLYSbmUWMgQalUI/hNALSTotttQEuqgHmZZn019lrtc0okcsei2r 1QTIeIc6FPJqPnU5YlpciorPvn1VVA4Yyt/qYnapQ/95vjj3ZHNa3B2+OKNu+rFQOZXA oI1KWa0IQy6kkgUGNqPWtpEV9eUgxm3+SiEFjNbA3P5wuagmfqFpzbwIprKhmmvZc8yX WW0xNy8JQMQEx/TiHZTOjjOGQCOq3Whse4jxi5X5822kFNtZ5//Tdy70gQ8g4SogpJ8w j82w== X-Forwarded-Encrypted: i=1; AJvYcCWDGwD3zS+fSQ3/WwpQX4IgWzRZDpHvCljHlZ4Fdb4YFPy+SZqjzdcOURgAwEuXLy+Nw9Q0DQXzrQO6s6MlqW1ZO6tR0se9qP6IWlAU X-Gm-Message-State: AOJu0YxLh/bI2HbPC4Splxv0ZdaGVgtZUJpo3+Uv3SBpN/TGrQfc3s4w c7ZL7NAeRxRKvF5MtarlEeBggChXbMg3YFES7Sm5+xUSRu9E0Jf6 X-Google-Smtp-Source: AGHT+IGkQ17HzT2wzOFm9lZ1MUlnx3fVT0qSwDvdVstx+8uIKcOGNUUnfvL0eYxQuIWJ1sfFl/Gqdw== X-Received: by 2002:a81:8491:0:b0:630:3061:c22 with SMTP id 00721157ae682-63030610d9fmr7911287b3.20.1718196475331; Wed, 12 Jun 2024 05:47:55 -0700 (PDT) Received: from localhost (fwdproxy-ash-008.fbsv.net. [2a03:2880:20ff:8::face:b00c]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6b07ed90264sm30056906d6.13.2024.06.12.05.47.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 05:47:54 -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 v4 2/2] mm: remove code to handle same filled pages Date: Wed, 12 Jun 2024 13:43:36 +0100 Message-ID: <20240612124750.2220726-3-usamaarif642@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240612124750.2220726-1-usamaarif642@gmail.com> References: <20240612124750.2220726-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. Signed-off-by: Usama Arif --- mm/zswap.c | 86 +++++------------------------------------------------- 1 file changed, 8 insertions(+), 78 deletions(-) diff --git a/mm/zswap.c b/mm/zswap.c index b9b35ef86d9b..ca8df9c99abf 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 @@ -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 b= oth - * 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_ent= ry *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 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); } @@ -1370,42 +1354,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 **********************************/ @@ -1417,7 +1365,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)); @@ -1450,13 +1397,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) @@ -1474,7 +1414,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; =20 @@ -1522,13 +1461,9 @@ bool zswap_store(struct folio *folio) return true; =20 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: @@ -1577,10 +1512,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) @@ -1682,8 +1614,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.0