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 A8D89C2BD09 for ; Mon, 1 Jul 2024 17:15:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 05FE76B0088; Mon, 1 Jul 2024 13:15:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F2A876B008C; Mon, 1 Jul 2024 13:15:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D7CEB6B0092; Mon, 1 Jul 2024 13:15:55 -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 B04BA6B0088 for ; Mon, 1 Jul 2024 13:15:55 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 410C91614CA for ; Mon, 1 Jul 2024 17:15:55 +0000 (UTC) X-FDA: 82291836270.30.A6ADA20 Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) by imf20.hostedemail.com (Postfix) with ESMTP id 243DF1C0013 for ; Mon, 1 Jul 2024 17:15:51 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="fB0/6WKc"; spf=pass (imf20.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.221.44 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=1719854141; a=rsa-sha256; cv=none; b=ONDDWJzJj0doHht9fJ+p50BpWBDnCc2wqkv8K44keYSO9VNbYwudWNjQx+Urm4Ha3UqWMR xqQi844CyMlpZFJMIygWCiOwd3OGDksOZr652ApoFVqkag6Bc+kQjpr/6uOIENnxBWKGwr jsd43t0WKGS1t7B341gTwk8xjqg4qcA= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="fB0/6WKc"; spf=pass (imf20.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.221.44 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=1719854141; 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=udEDtypAj+ZPUGqPzgdGsSTfzcgkaIOxqbIXvndywcs=; b=KLTthHiL/2apN3bZFVG7dx65aKP5vivJnaf0MSuvPIgqwcLR72d+Fkf2hGoJZd82SA9CDh DXTolGvvVVLYzW8s0hvO6RgbMBTasJgbL8KeyEQ32p817C66hhtla6ny60C1VXOfRQCiYt QIorTOyJ/3b3Lc0xPuG1w7npgUwyY4s= Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-364ff42999eso1190817f8f.3 for ; Mon, 01 Jul 2024 10:15:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719854150; x=1720458950; 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=udEDtypAj+ZPUGqPzgdGsSTfzcgkaIOxqbIXvndywcs=; b=fB0/6WKcE8yQsWGBxGwLNRLUj46mMfxYya6oXTmX3Md+NxjI47FEzSOmk/SOBKFMR2 3O4IUHVDSCrFXQSI6THKivDghcoZEbUfKQcM1xSNvv9/yIP2xRCI5FDOy5g+LFvsYVOr zuuhytxOxS7cfDv+zrDhk+hwbM1hQOZkYgrNYAdCKKYnntN6bChgUM6jwKqRT8DudC0S T2a3YqfAAF2T5h6GsAQBTAJbRIEraTm29c9sJLmFDCSQ9rexZXX8y5+w5IKcmvmGnkyg oLB20Gh/KSEzCBG6qG5oS/axa6PXrh852+tynnNergi96nulRrRfuO3upfon2zY5BI75 TqpA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719854150; x=1720458950; 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=udEDtypAj+ZPUGqPzgdGsSTfzcgkaIOxqbIXvndywcs=; b=Hsm8EmxY2H8R1+QMXpjR6Zxr6ISXfWFF9cklRlm7pzhN4+apI8LCCAHtIDHJesVgIT 1e/vsmL/Npfnc68xrfsM/tIMKCULr3CHlFKtBwJ3ZlYoUUuUGNazjtyhfrMV5BmjrlC/ dnfI/PFOKI00cECIncliH7FDJRAuwOU64zWyFz8/g70BnTzTrYi3f683f0d9jorj4zv3 3XNGiw12bEgxFZzmrUwW5KiL4PWQqBqD0mEhN8XFiUOpNzEAYR8r/rzAPPKCd485Oo7s d3mwP7qEuDCkOVBZ9fWSoojI65zuiUf0SgX3aYEMBIWNFz6lod95UKbZ1Jj5gUqXN1q6 YazQ== X-Forwarded-Encrypted: i=1; AJvYcCXkF0uAYJGfTLB6k2HHFzZXnFKJWz4THSk6DKqU+DhwnXHMN4enrXTmye2V97nv+90bzMvus75RcE+j15lSD11pgTo= X-Gm-Message-State: AOJu0YyCR6i22dysOwI9TkG4EUVF4g1/CZ3bcmInEUs1o/tNERXijXXR aLMJjXUSgd0ZGkc2C9nXEjHbquxD/w7eS7fp2iw45FiG068JMI1f X-Google-Smtp-Source: AGHT+IEossGDSRxnaV4E5nry7TTgTxzSqxPf2Pj0/uqxzzhKIZYkn1SdzhEbKQsIzJ7BLT9+PIazlQ== X-Received: by 2002:a5d:47c6:0:b0:367:2d2f:e634 with SMTP id ffacd0b85a97d-367757249a4mr7559662f8f.55.1719854150041; Mon, 01 Jul 2024 10:15:50 -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 ffacd0b85a97d-3675a0e12fcsm10567482f8f.48.2024.07.01.10.15.47 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 01 Jul 2024 10:15:49 -0700 (PDT) Message-ID: <6f8b64f8-b7b0-42f4-a1d4-bf79624cff1e@gmail.com> Date: Mon, 1 Jul 2024 20:15:46 +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> <5743d4e4-3e34-4ac1-b4a9-0ddc4f0e624d@gmail.com> Content-Language: en-US In-Reply-To: <5743d4e4-3e34-4ac1-b4a9-0ddc4f0e624d@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Stat-Signature: t3b9r6mjsnq7ktyu7xyh3b5ah366bid6 X-Rspamd-Queue-Id: 243DF1C0013 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1719854151-55093 X-HE-Meta: U2FsdGVkX1/BW219dCrp28ej5YCNyUsNgdoJr9V7bouuH81i1udZp17ooChtRWP/hITFmNvLf+T7Klicd9lpS376+pvLQhJdpx8NKG0QNi0SJJZxvZ/mGu3YqSPzZCXmeDi8bq+XNYXJfATM291fqKMT6NxCstGkUHWEEGnA+Oajm9Us5ZElktYAtP21BFpitT/aq77Zrv5eP38+ZrIRoiwhce3W9UC388JADB/OPNcd5ojB1xeyjcrHHVqGRNZvlu86bcv45LqADUWKZiqQstvC+dwoJXI4jTOYAqC2isroln53ZoKXOCuhpGSr9nb6BsBtbfzGjm1O6PSOF+d6sBtLuspJ2ZUVPXnVKtpUmBSJhLz43GGQ2Q87lJdf2X7xKZSTVrctTPFo2tGE7xiKaB/OuppOz/92esZxf3+52Dum15EcaVeCZedXVt1ptBkAnJriuL1lcp7J5MVOBC2kgwjaLwYT5hQxWWJQKp3rimNHBv/vGfLug+OjmAMCsx/Pu7Iv3pS9ASsKvRPaDiHsI+zR2TX6d+U2RkR+avIjY1VvltHfjU8eY5zaNyg0Su4TWKkkHze0raOBMrGbzfuO8rBbbghjGTL6wBx8Tk24tVn76ECl/NAfdvgsadnpFMZHqLKBFpOiZ0vcVU9K09B40It+b3dmI3EXGfbc6z5KZ5IVHDAh5iQV40ZDXSlr+YmKknfKncyKAxMuE7zrbqlGrPjPdqVK9tMG2AiAcgBG2FwXLE2TNN+XFw6bi9rOJlln0Z1SPQlzSubl0xYJ1PyWQmz32y5joDVBj0tfI3StV/+8TLfrUwVLAn6mmKsQS3NXXRu6Ess9tnB9gmTMrO0yYs1O5aeuUeEGXm9Y6+emNx88feMokNGLYY123fKPPjX3Qep7HzAS+Lbd9zqJIYnD+rZeFFJHwL8Pdbphs4ZdMz79+12Zxp7scEgXu1Mvtzh1uHBCEMoAdblxkIi36YJ WvXcShha 1qN7OQ8uuaHME7Hm9L3qTSiLi8hLr2oWRe0064B2TzDDevqLNRsjm6Cz/wRHMO3hH1Y9trgOvsxb5XTrCR5le3TjIry0aJnOSqRvf13vTtd4PvY7FJwud8Bypg6vCj5HT7mzzM3QNDqYD815wVLVx9mptp5A4UHMwHy6KsZTt1czmuD4QSLf/0vGD3H/PlgmLlfr0RzN7rBU69ylgeImVR7WfVywuk6+nPHZ3mVnWmf9EDuIY2os/1dFlOi0nBZEbFGCeRekVSnfc4MpmclVQpKOpZEELHs8/ycRloCNN/6pxnE69wWB3lS4hpraFS2dOoHxtjUtoaHjQeefl72NtUX/8/fwkmexOJM6k0J1/ID2//e1axzzr5WkkVsy/ru9DGn/V7jO8poJwbjsREYyfmw6ZF/qRyIlekF4aK1rRtFrCoSIFqb/qIRzRbYHhwd6AsyNNEXA5RavpzGHqtFW1+vZOkaoRwvm0fcY9Uc0ENsAjUWBQOYl4l5hadYdk+Ew6aDn+LnvLJuWqOR7j628SlXRtsP3T8+JVealKFnZA7VKD48ddXg4/nSiFfS3BJ70gqYJp/vBGHu3h3D9iAbEDlFWme9qVGJbWZRNvqWkEZfvtTj3Cgyh6ICaWtj6kc4JtzUVc5k3vQlXPnGuo8lq51YIKtnPH7s1KQ0QieVOUKfqotLqudhSK/HnrGAgthtCdpDfII/NGp+VptiM= 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:37, Usama Arif wrote: > > 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 > Sorry for the repeated emails, this one should definitely work. Changes from v7 (Johannes): - Give overview and explain how locking works in zeromap in comments - Add comment for why last word is checked first when checking if folio is zero-filled - Merge is_folio_zero_filled and is_folio_page_zero_filled into 1 function. - Use folio_zero_range to fill a folio with zero at readtime. - Put swap_zeromap_folio_clear in an else branch (even if checkpatch gives warning) and add comment to make it explicitly clear that it needs to happen if folio is not zero filled. - add missing kvfree for zeromap incase swapon fails. >From 100983ea94cedf6a342352f91d60a83cf4fa27a5 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 | 21 ++++++++ 3 files changed, 138 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..111ca98a5944 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; @@ -3315,6 +3335,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) p->flags = 0; spin_unlock(&swap_lock); vfree(swap_map); + kvfree(p->zeromap); kvfree(cluster_info); if (inced_nr_rotate_swap) atomic_dec(&nr_rotate_swap); -- 2.43.0