From patchwork Mon Jun 24 14:01:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usama Arif X-Patchwork-Id: 13709548 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 B4770C2BD09 for ; Mon, 24 Jun 2024 14:04:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 103556B0191; Mon, 24 Jun 2024 10:04:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0650C6B0194; Mon, 24 Jun 2024 10:04:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D9A616B0191; Mon, 24 Jun 2024 10:04:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id BBF636B018F for ; Mon, 24 Jun 2024 10:04:46 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 788FD140F6E for ; Mon, 24 Jun 2024 14:04:46 +0000 (UTC) X-FDA: 82265952972.28.507F272 Received: from mail-qk1-f170.google.com (mail-qk1-f170.google.com [209.85.222.170]) by imf16.hostedemail.com (Postfix) with ESMTP id EFECB18001D for ; Mon, 24 Jun 2024 14:04:42 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=OY2kPn2N; spf=pass (imf16.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.222.170 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=1719237869; 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=An+vqzJxAs1Y7MV78NqDixz6Z4p5FgiXkGCQ6W1Ubis=; b=cyyiRoRZxIl5BAC0VjpsYcj+fO0wW9QJoTG0iql9STA9e4BqZD5VreZPFJ0F6PYft6xvVn YNI/Zp03wZ6xAIyqNyz3+0XPgfRl/0HoGaFG+rJKXbmoCrZK/XtYQMWlyo8Snd7gwFOagc xVe8XeBcvFSf8LXlfoKfLjdovU5NeHg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719237869; a=rsa-sha256; cv=none; b=QNLh0Pisb7/nFQmjh7/O2WgBefQnBuPklCXkPdEooSG8S8C/Jx8F8jGqRKacUnCUrUImdv viBvEqyijyQvSqv9iXlrFkzwOvcL7RH4zfn2/Wf8iKlmIYftvhhM7ShU/67sggNLeTxlwd CiZdHeN/6Plx9/KI+vnZ8h0RU5o8OAA= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=OY2kPn2N; spf=pass (imf16.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.222.170 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-qk1-f170.google.com with SMTP id af79cd13be357-797f2d8b408so292628685a.1 for ; Mon, 24 Jun 2024 07:04:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719237882; x=1719842682; 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=An+vqzJxAs1Y7MV78NqDixz6Z4p5FgiXkGCQ6W1Ubis=; b=OY2kPn2NripblytBqDJyGPMPYIsJJeeEVyv0oL99bUFYDTfV7ov99eP1Ozqdq0w2jE ZkWGpp14DWiSLyofcr5vDHghTZD1WsVxI8/PMBfET2CRQmSZHWOTEbqLtitg1nrP0nBB PK2sFUSaZbtqiWyJELe4uEVGFs7Me4+41uYhOzZOhu1ACEiso/CQLWEWvnFl87+59yeo drtrdAQfaty13hYa2laLJskKFsOwfSB7N4tBdeqt245hTajxkX4wmIHEDN/RjyplmtsO TA/iEB2AWTTt9q5tL43A0QuwYG2ukb++mF+kTFoeN7k4BGYwLbVUnDdtZtBTGZTxgjl1 0WDA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719237882; x=1719842682; 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=An+vqzJxAs1Y7MV78NqDixz6Z4p5FgiXkGCQ6W1Ubis=; b=VNpj972ETsYw6In5T1vXPSv0SKaTDwHVrUNWy2QA0g5zQ5dF3a01uPkN3fTrkniQMV B1w+KH8VGMfl3XOxWm9mM2/RXw6ad1DFJGM4sm6hLD6L5QKozGYQo7yUTQx37GNRftPU cS5ar8LFhrIdhSsUKQ8LnTqqljr8wA3Wwx9mUZUFY4ERtWAJ9nfAGIWVDI2ZKEwWZWl5 UpIhRtwX+5HswdPSDiFJLmIs/hcn1s0OSn4LURyFg8mAJNR4MUNh85V0zNpX6NauSs9/ j9rJNTNPpYQUoaEpoj6LKNtOu4aejae4RHqZ4YzsPPIvWvF3rvH+NL+ZQbsklejhADou PGjQ== X-Forwarded-Encrypted: i=1; AJvYcCXwhk/9hc/G1NNmJWgJo6jaejNwzvhpdfSY0BCTcbAXI3UhOO9Gutm6T5tQNGtrEcRsmAbOhpYICrLlMWVAynDp5VE= X-Gm-Message-State: AOJu0YxCC8TkEPe850469YjQhrxh0/IpYqhydH4oQiqktUW76JpC4Kbw JGbBjlauRf8EGH40TPZMrXk08vfuF4XQr8s9NuHJdTpbTcbe9IkE X-Google-Smtp-Source: AGHT+IH3aFASAHDw+XSRgjC/6aa1Ac8XWACcX5t5iWt10sqj4F6XeBrfB1g921q9rxXzYeZBImiZvw== X-Received: by 2002:a05:620a:394c:b0:79b:eb0f:7781 with SMTP id af79cd13be357-79beb0f7ec3mr389710885a.53.1719237882004; Mon, 24 Jun 2024 07:04:42 -0700 (PDT) Received: from localhost (fwdproxy-ash-008.fbsv.net. [2a03:2880:20ff:8::face:b00c]) by smtp.gmail.com with ESMTPSA id af79cd13be357-79bce8b0f3esm319945285a.29.2024.06.24.07.04.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 24 Jun 2024 07:04:41 -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: [PATCH v6 1/2] mm: store zero pages to be swapped out in a bitmap Date: Mon, 24 Jun 2024 15:01:28 +0100 Message-ID: <20240624140427.1334871-2-usamaarif642@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240624140427.1334871-1-usamaarif642@gmail.com> References: <20240624140427.1334871-1-usamaarif642@gmail.com> MIME-Version: 1.0 X-Stat-Signature: wmfh8rizoynfktc7hipw6dkbue17s6xz X-Rspamd-Queue-Id: EFECB18001D X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1719237882-298571 X-HE-Meta: U2FsdGVkX1+ihHXhcrthQqHkG0881Jon2GhB3akUWtSEOdg7+RwW+yHgjZKxyHGlQpAeod1iJ+bzKcHfa5Yxou24URE7gtyhNW6qZd6NJvNNBQ7G/trO8HrkZMqY33gSJU3mdxAl/jHTIenmD//lRb6w1QLYlZb/VdxaavGuinKVVeGhtEMMbMemwngTN8UmWF61SMielM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dcp8d/mK8b675weLRtZbPPgY5LXOzRnKJl8etQgOUE1IjWFKh0FKiOTDlcyRhMp0zDpsehhjjk/Gcz/cGQ4I7GgC0nUSVjJxR/Sd4mKiheIngx78jNnAjkKkBZ4DeuWsxqJKmC1jCQREILQVUm8P2xEZFTXW4efo6tcsCNWd6ZDG0ZdJudI7VStkBOGL+adS/tGUXLwgKlVcq5ENXnfQBUBE03Z/ME52QM79wh6gmN/tRsjI1hgMk6EliuDSs9lxYxLtklsePXnLv7Zdlog/Bx0i2OlRTptRR+LUMnt/oeryNm4KNMMVzg/9RlR5NkvKnWjM7h/5YqLuEqhDy13U9ODGwXgUH6cRg4= 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: "Huang, Ying" Cc: Hugh Dickins Cc: Johannes Weiner Cc: Matthew Wilcox (Oracle) Cc: Shakeel Butt Cc: Usama Arif Cc: Andi Kleen Signed-off-by: Andrew Morton --- 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..2263f71baa31 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); + kvfree(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 = kvzalloc(DIV_ROUND_UP(maxpages, 8), 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;