From mboxrd@z Thu Jan 1 00:00:00 1970 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 9D9F0C2BD09 for ; Mon, 1 Jul 2024 15:37:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 19D246B0082; Mon, 1 Jul 2024 11:37:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 14DC86B008A; Mon, 1 Jul 2024 11:37:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F085F6B0092; Mon, 1 Jul 2024 11:37:26 -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 CD8476B0082 for ; Mon, 1 Jul 2024 11:37:26 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 55334A40DC for ; Mon, 1 Jul 2024 15:37:26 +0000 (UTC) X-FDA: 82291588092.10.F97E8A4 Received: from mail-lf1-f53.google.com (mail-lf1-f53.google.com [209.85.167.53]) by imf22.hostedemail.com (Postfix) with ESMTP id 435E9C0007 for ; Mon, 1 Jul 2024 15:37:24 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=f5mrMfJ3; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf22.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.167.53 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1719848226; a=rsa-sha256; cv=none; b=hennEfs7t3oSCKFQVMoKyEsNdlX1B8XCAjt125svx3k5w2DoL+LT7FvDye3cz6wCTdLUcu 5QAKZmoBqWEQ4UyTLO7SZhDMyX0xHnVpu9UYxEErkbpfinRonulgq4ZunugAq19xI13/W2 ccVjkGus71bS00lPTVNi4tag6HdfIPI= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=f5mrMfJ3; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf22.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.167.53 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=1719848226; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=sQfhSFiqrbWpIg3Ny2S4d0ryJUXXU8wg64ktG484uv4=; b=Ba1Sg3G5PlDucERj1wAU1a9uzEzJWJRmarEYEtmpzYD8K1KXQMfK10Vh9GfzvWQH6g2JX7 ueHKGEP0lJ5c6gOYtWae40bzPD/x1h3WcejXvD/WdAQ5RhHBqMbIaYAbrUzYUD0bDakzdJ MHAjswjf267Mm7nZ6zZ2SvYV2WP/Xvo= Received: by mail-lf1-f53.google.com with SMTP id 2adb3069b0e04-52cdea1387eso3305244e87.0 for ; Mon, 01 Jul 2024 08:37:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719848242; x=1720453042; darn=kvack.org; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id:from:to :cc:subject:date:message-id:reply-to; bh=sQfhSFiqrbWpIg3Ny2S4d0ryJUXXU8wg64ktG484uv4=; b=f5mrMfJ3WmCTeoKigaxYlO+pccxzAg3xQSgwbvcudCcl54ylQIUew3OhiOQt6W5BeN ND6D0BUPmtOslvQX9uvXqmjkSVPEYWYFdUINBhNDqPsF9sii0rH9abW+2Fw+ynPWVbZv EY2i9hwLm00S9t/L/SzKqB9ZpSDPxJYSv7frMqvk5T1xR02tfWFAoqbKKy4EBI6GDMd5 CbbEg7V9xfLvAyFFLzpYlCgCE6zD43JHlutgIoBtO7JyvRwdme4a/3PCV007WZ6znfQ3 UNvweeZauvhe92yCnB7c4CRLD1Dm036g6mNbKzrYAH2RiwUjGGDMyg92GdaaPcTj0Oh+ O2UQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719848242; x=1720453042; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sQfhSFiqrbWpIg3Ny2S4d0ryJUXXU8wg64ktG484uv4=; b=uE3cPi8sDDglHyKCfFJ5oXKw7HwrlaPyGP5x3xxFi3hMYO49Njt7geXGoQMSEopjH5 AbWQ2QM5u46IrjYJAQ+CmK8GyeStodYE5aX+CNGRwGJvlyH8hogXncawocT1R2XArXxn 3ES9S6jP8gWLIdJiapCXKAqVPvwkDBRDH9gpaKcEcNSqj4giFjyhCBGcH7TIWkG9fuhg U20M7ZJP09QwdZDMaw3kaFQXpubCiYy1uY9Aiyo9xLZx1DkRlm7YeYzRHQBaS/qc1PNq mb+0+ghUUcqyxBUuib6sI6ip3IpTzvah0qkKY9LNjtpXTkiRjH3YrVBtY7CIVLNeXyWV /0GQ== X-Forwarded-Encrypted: i=1; AJvYcCX8xov5n03vuLrkE+jrEiLpwBWUilGV/2Ftt9itqDH51Qh21GEdGu+23m87ppDFvPxXZXKOEpzn5KRqdBWh1WFo05w= X-Gm-Message-State: AOJu0YyX0/rg1U6i2agdixKtgF/mcDU17RF7zF4H8/ReIXlCmnIosxrd 66wE9LlbJSA6ScjsP1qSN6JiisYU6W1JkSomhvr9Oh9zu5nzEFOP X-Google-Smtp-Source: AGHT+IH2ZfOVOfrg3YdkSlcPjbBRbZXfC8cBkbtV8wd6XdRB9vtWxaji79ZUO+BDOo67IlKs8bmJEQ== X-Received: by 2002:ac2:4e0b:0:b0:52e:767a:ad9c with SMTP id 2adb3069b0e04-52e826fc0dcmr3719563e87.53.1719848242130; Mon, 01 Jul 2024 08:37:22 -0700 (PDT) Received: from ?IPV6:2001:16a2:dfed:e700:8f2:d143:dc90:1038? ([2001:16a2:dfed:e700:8f2:d143:dc90:1038]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4256af557fesm160797575e9.11.2024.07.01.08.37.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 01 Jul 2024 08:37:21 -0700 (PDT) Message-ID: <5743d4e4-3e34-4ac1-b4a9-0ddc4f0e624d@gmail.com> Date: Mon, 1 Jul 2024 18:37:19 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v7 1/2] mm: store zero pages to be swapped out in a bitmap From: Usama Arif To: Johannes Weiner , akpm@linux-foundation.org Cc: 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, Andi Kleen References: <20240627105730.3110705-1-usamaarif642@gmail.com> <20240627105730.3110705-2-usamaarif642@gmail.com> <20240627161852.GA469122@cmpxchg.org> <44a57df4-e54c-47ee-96b8-e2361c549239@gmail.com> Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 435E9C0007 X-Stat-Signature: emtdfmjg56y5ecm9swxy94pxoqgetogo X-Rspam-User: X-HE-Tag: 1719848244-484621 X-HE-Meta: U2FsdGVkX18fZOxxpCrzM6y6JPqghMKK6KUhTxh5nPRuTmVZTbsNKlhkn1eN3ZsC4dn0OsZs5Bk2zWmYEWw0nrDO2qTGsAFJW7ejovtpkQDLhFOOVLMrTbXS2NIVwweLpGDZKccyyj7dut4eAk4ihe57s/8Q7YxKv0PFvICLLaUhWUDBFhQkPHwgnY2GPOA+H/3GuH/1bvRGKYUkaGX+GhR3SjBsnsWgu6dzWwTqjGX2EmPKD31kZiF/yuyZ5Iwe2HTaga9K0/RGrUs1yEDkBm3SqNvosJQJaEyzaqHgZRVxsn+cC2RWJIeWINcQhO6QLgmhy9bZ3n1RM3Rnpc5EmKz1ywd3QE/cBXIIkQYfS754kxcAPgvsDJh5P+HhekgLUTLLr/rddN4+xdie+oAOIHA9FASUhzjR0KhLqKfDEyGuxalmAT2dYGdNoWk8GpE8lR69GBsuBPusxB+yZz6uZsjkJgR0VUBCyDU5KwX7vBARgGNy82f35QToaEFOF9SsafmpF4ZSXxwub1zWPU89Jez4s5Ups3p/j52uhqs+oKWM9ZI030WDIQeKAXYQ6KGgVcJPsITX/6SFGVe7ikXlJ8KJo4+HYy6NkyrJe1bo4sRsCwD+uwIBfpO1bmATnomMsiLjjUYILVpUqHimSFD150cvNgKz4LVruGix/ItVBBi6PW53eLD8dDJdAT3sPp6KQYyCNBeYE7fP3v566lctOnHplehxV5bVpjpWMqQ5X3DolqqSct5PHsAtTJOpTbnvOnAlBIapbyM2yzjNrIVEM0bLaVEd89j0ipwdw2RfKaIaqNW7/EFESS3723t+9Lq5E+7vZBzHt8qYsNqMaBunzTQoWIRTrOI6YSUVR+crGBSFySn7WkWA9WuFyo0yOyO0vYHpxYgZ+7yu26hDX1Gyy5E/aFyLoumZEQlVyGbVy9EhkpSKrEuNxVRFHC3DAxvzmcFNjIP0D3bLH2obIZt M/M+KBXL oqUHVngmdxILW8jRyH8h97VYVRWL5HyA2oyp4I/vwJoAiteXli9DPeMM7EDKtCeY5P9o+OyOSHwB1QgTJ3UjVbQRZq4tn8JuvY+ZiJT9hLQbqhZt7MgkZyrcA0nFjYW0V+ax7Atiokc0FSmaYP4ypBxrtcd+3xpY5WHIVJ2M9i3XhKCTs8MMtvCdvk4UaeerjiItiPAMbGc3XREPZe8kBTaOwe4GK4Bxq6B2jWYXUW8jnSgdBXRR38HSV8OvzvDBiJN05XhJguebJsYREO7aVUuCPF8bWH091OaxEi0JL7+HL99qeo4JW7OzK7HFfKbv9vPiUte8tSa5GbzydHFJrnMAVyMEhcTkAzMeDdkfXffDqn5LIoDWarvJi/1+Fo1D6VgHpv4nMVMjolZBQIsdxpeaSZYNrdd8h/9rYm1ACpJIcXwgyd9Q4azuzCWB9F6Gju/iyRHvP6IWmERAgF04yhVp7/FcmDwWXcBi3z4CXSlkIiIh4Ne6Li+nzjACxWZcanaqWn680TlyDB7Xy6Xl5P3+8CWfyU+5AbX/xDBMtM5e/XWh0UJa3g90lrxCeIFYS8kxDIXrd61SImjo+TRG8cxR4lkMkZ2lbyTLtEbSQ8vE1H09ZDz2cnQdXPwncCSfiU6o51fapBDaTWVBhqvJs42oBenF3zN9dw4Fmq2ihV9lVyq8tpud6hkvb0xVXbOA2c3FFeB0KVvjc9o0= 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: On 01/07/2024 18:01, Usama Arif wrote: > > On 28/06/2024 18:30, Usama Arif wrote: >> >> On 27/06/2024 19:18, Johannes Weiner wrote: >>> Hi Usama, >>> >>> On Thu, Jun 27, 2024 at 11:55:29AM +0100, Usama Arif wrote: >>>> 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 >>> This looks great to me, and the numbers speak for themselves. A few >>> minor comments below: >>> >>>> --- >>>>   include/linux/swap.h |   1 + >>>>   mm/page_io.c         | 113 >>>> ++++++++++++++++++++++++++++++++++++++++++- >>>>   mm/swapfile.c        |  20 ++++++++ >>>>   3 files changed, 133 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; >>>>   } >>> It might be good to have a short comment that gives 1) an overview, >>> that we're using a bitmap to avoid doing IO for zero-filled pages and >>> 2) the locking, that the bits are protected by the locked swapcache >>> folio and atomic updates are used to protect against RMW corruption >>> due to other zero swap entries seeing concurrent updates. >> >> Thanks! have addressed the comments and will include them in next >> revision. Just a couple of things. >> >> Will add the overview in swap_writepage when the check is made if the >> folio is zero filled and zeromap bits are set, instead of at this point. >> >>>> +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; >>>> +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)); >>>> +} >>> Should this be in highmem.h next to the other folio_zero_* functions? >> >> Thanks for pointing to highmem.h. It already has folio_zero_range, >> which should do the same thing, so I think I can just do >> folio_zero_range(folio, 0, folio_size(folio)) and this function >> shouldnt be needed. > > > How about this? Patch 2 to remove zswap code doesn't change. > > My mail client had messed up the spacing in my previous reply, so it might not apply cleanly. Hoping it comes out correct this time From b4aa285289b0d5c87b998cc48669cf3fc41fcbb6 Mon Sep 17 00:00:00 2001 From: Usama Arif Date: Fri, 24 May 2024 14:52:55 +0100 Subject: [PATCH 1/2] mm: store zero pages to be swapped out in a bitmap 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 Acked-by: Johannes Weiner ---  include/linux/swap.h |   1 +  mm/page_io.c         | 117 ++++++++++++++++++++++++++++++++++++++++++-  mm/swapfile.c        |  20 ++++++++  3 files changed, 137 insertions(+), 1 deletion(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 3df75d62a835..8c38a18320b8 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;        /* kvmalloc'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..6cbfb277d020 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; @@ -424,6 +517,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); @@ -514,7 +627,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..4f8e67d18cb5 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,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. +     */ +    p->zeromap = kvmalloc_array(BITS_TO_LONGS(maxpages), sizeof(long), +                    GFP_KERNEL | __GFP_ZERO); +    if (!p->zeromap) { +        error = -ENOMEM; +        goto bad_swap_unlock_inode; +    } +      if (p->bdev && bdev_stable_writes(p->bdev))          p->flags |= SWP_STABLE_WRITES; -- 2.43.0