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 EC8C2C677C4 for ; Wed, 11 Jun 2025 08:27:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8F3906B008A; Wed, 11 Jun 2025 04:27:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 87CFD6B008C; Wed, 11 Jun 2025 04:27:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 744B36B0092; Wed, 11 Jun 2025 04:27:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 562F26B008A for ; Wed, 11 Jun 2025 04:27:52 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id E67DA1D3767 for ; Wed, 11 Jun 2025 08:27:51 +0000 (UTC) X-FDA: 83542441542.10.299A85A Received: from dggsgout12.his.huawei.com (dggsgout12.his.huawei.com [45.249.212.56]) by imf16.hostedemail.com (Postfix) with ESMTP id ADBB718000A for ; Wed, 11 Jun 2025 08:27:48 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf16.hostedemail.com: domain of shikemeng@huaweicloud.com designates 45.249.212.56 as permitted sender) smtp.mailfrom=shikemeng@huaweicloud.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1749630469; 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; bh=NfmcLqPQysFcIQUdiJOEQ1gznGfoFn1DAD1p+tpXTcQ=; b=kdDjEzJr5BTWMl2iFFGfCDa8OEr1xKVVSipUk/rEu/Tw4PldkoiRtwwgtSx1W2nPIuMilN mBSacneUE7NoUQC02JP/GKSOpLpIXwAES4D99IMElkxVBYT0vAAfcE0PEy4uip+T6fx/YF 7HfwJCCOxuuZ47TTaMi3/InavOfbmEs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1749630469; a=rsa-sha256; cv=none; b=zoYUEnXOQG/dPM3uOjKfGY2gxrlPiD/hsJaQ+3ZD2innbZ18JHpbh9NmY9Ni07vDUIoSrV tSo36IHH6C2yItx7ZZavbdLtYfeWNrMQxWI5j3G0K3sOTXr9A7de6sb1hNNisU/vhffsva uBX07Gsa43P1B7ULoCFymvG8X6Wi9SU= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf16.hostedemail.com: domain of shikemeng@huaweicloud.com designates 45.249.212.56 as permitted sender) smtp.mailfrom=shikemeng@huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.163.216]) by dggsgout12.his.huawei.com (SkyGuard) with ESMTPS id 4bHJgj6HKFzKHMdh for ; Wed, 11 Jun 2025 16:27:45 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.252]) by mail.maildlp.com (Postfix) with ESMTP id 3D9971A01A1 for ; Wed, 11 Jun 2025 16:27:44 +0800 (CST) Received: from [10.174.99.169] (unknown [10.174.99.169]) by APP3 (Coremail) with SMTP id _Ch0CgC3VsH_PUloPN7COw--.27762S2; Wed, 11 Jun 2025 16:27:44 +0800 (CST) Subject: Re: [PATCH 2/4] mm: swap: correctly use maxpages in swapon syscall to avoid potensial deadloop To: Baoquan He Cc: akpm@linux-foundation.org, kasong@tencent.com, hannes@cmpxchg.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20250522122554.12209-1-shikemeng@huaweicloud.com> <20250522122554.12209-3-shikemeng@huaweicloud.com> From: Kemeng Shi Message-ID: Date: Wed, 11 Jun 2025 16:27:43 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.9.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=gbk Content-Transfer-Encoding: 7bit X-CM-TRANSID:_Ch0CgC3VsH_PUloPN7COw--.27762S2 X-Coremail-Antispam: 1UD129KBjvJXoWxWryrur4rtr13KrW3JFykGrg_yoWrKrW7pF W3XF1qkr4kJryIkw12va1DCFy3Gr1fKa17ta13Ja4fZ3WDZrySgry8trnYk3s2kr1kJFyv qrs7t347uF98ta7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUvjb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4 vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xIIjxv20xvEc7Cj xVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIE14v26rxl6s0DM28EF7xvwVC2z280aVCY1x 0267AKxVW0oVCq3wAS0I0E0xvYzxvE52x082IY62kv0487Mc02F40EFcxC0VAKzVAqx4xG 6I80ewAv7VC0I7IYx2IY67AKxVWUGVWUXwAv7VC2z280aVAFwI0_Jr0_Gr1lOx8S6xCaFV Cjc4AY6r1j6r4UM4x0Y48IcVAKI48JMxk0xIA0c2IEe2xFo4CEbIxvr21lc7CjxVAaw2AF wI0_JF0_Jw1l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4 xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r126r1D MIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I 0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVWU JVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUwz uWDUUUU X-CM-SenderInfo: 5vklyvpphqwq5kxd4v5lfo033gof0z/ X-Stat-Signature: pwrua9boja3769wq3xfmchq7otoenoue X-Rspamd-Queue-Id: ADBB718000A X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1749630468-368905 X-HE-Meta: U2FsdGVkX1+oT2B1Ox+9cDN0gA0GugjJlZp7vpEazCYAGKxxkfTIo0Ct592CFLWvLzI0PYqEXKzYq4ziSK76DtLcNM3OPsX7jvoKgW8OWW1Fhqhr2jDmhqDrWMumCCkLCupn4LgXaoQzBUhlRlRVKMdsFFN2JwTAaCEWorbkEBYgB7pXsTjBqtAAAlTLVG5NNtCXkGvPzMyoF76sLOWA2WaTSuPRB0Iqzao53OuzAPWECccu7kAN0R3//5pOa2VF12GI4MFibRZnsTxF2zSmjqx4m/0j5FzK7wBD80xKGEzx6Yd+gKU4JIf0jITaNA97qwc8tUgPrdBFEvYq4mAEODXsDzNrioSFCCIFclI7CxnXO5JAVchwkv7IYxKRVOyqNhmCmREurcMaYktxZAVooMHXO9vgmjKutZIqU77QXefjMbuAt6vtjaAGChXWrSELb5vbgZxZl315+zVhqq3Gq+kAZck1YNqlcoWwZnIVr8y0cnam+g0H6GQkTwIIEp5z3Mvj7quLTO8RLZsQAz+3vG++NxWebBoam1NfFHqpm5w1fbTwq53RbDhKpk5gkg15yDiREiD6NKryKC2oAgA+UGlq3yjCROpaZNGTj1QBoeyV/3zt5jH9ib3y4eU5xXJ62SWQFooelVVLeTA/OCoAgGgXXRjLX2OsJ8ZEyxjrrJmtoqRLqDh8BfSLwVSN2pf7QjSBLLEd9Z5VYv+CmOnwDCHfzkSJyw5TQ8ijMoCEUbQKL1Lx8smeG1fV1IExsohX6ninCthuOD7Yrvm0l4LGnhlKibS7OKZWkMukN8mgOoy6EM3bJiuqexc6yBuOGj2ZEAe5ul83HzLBtuhluHz2lK4+uVf1dhVSI/RicF3Rt/QGGfZOlaw6wHeTGcOYgMcPjpUeoFGGVDTiMg9xjA0247Aqelxg67BucHTHmIytSb7FpIpbmt4MuBnoj3411TTLYu6G79PcvIP6FeKPd2y S7xA1+tC cc4q6YXIORALfLUdUx2Jyvu7L0vnx9+G21n8zDTNUzWkeZm0oj5pYrYiqKPn2kS+1I0+gBgatUEpN2tGH5Bg9xnrVqVqYoNeKeqMGSARrL+q4NEJW0MDT8Y+0rqGpgxBViBKibY2aabQ7q58XXIRXqIbhjAvo/UKE/h15 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 5/30/2025 10:50 AM, Baoquan He wrote: > On 05/22/25 at 08:25pm, Kemeng Shi wrote: >> We use maxpages from read_swap_header() to initialize swap_info_struct, >> however the maxpages might be reduced in setup_swap_extents() and the >> si->max is assigned with the reduced maxpages from the >> setup_swap_extents(). >> Obviously, this could lead to memory waste as we allocated memory based on >> larger maxpages, besides, this could lead to a potensial deadloop as > ^ typo, potential Thanks, will fix this in next version. >> following: >> 1) When calling setup_clusters() with larger maxpages, unavailable pages >> within range [si->max, larger maxpages) are not accounted with >> inc_cluster_info_page(). As a result, these pages are assumed available >> but can not be allocated. The cluster contains these pages can be moved >> to frag_clusters list after it's all available pages were allocated. >> 2) When the cluster mentioned in 1) is the only cluster in frag_clusters >> list, cluster_alloc_swap_entry() assume order 0 allocation will never >> failed and will enter a deadloop by keep trying to allocate page from the >> only cluster in frag_clusters which contains no actually available page. >> >> Call setup_swap_extents() to get the final maxpages before swap_info_struct >> initialization to fix the issue. >> >> Fixes: 661383c6111a3 ("mm: swap: relaim the cached parts that got scanned") >> Signed-off-by: Kemeng Shi >> --- >> mm/swapfile.c | 47 ++++++++++++++++++++--------------------------- >> 1 file changed, 20 insertions(+), 27 deletions(-) > > Reviedwed-by: Baoquan He > >> >> diff --git a/mm/swapfile.c b/mm/swapfile.c >> index 75b69213c2e7..a82f4ebefca3 100644 >> --- a/mm/swapfile.c >> +++ b/mm/swapfile.c >> @@ -3141,43 +3141,30 @@ static unsigned long read_swap_header(struct swap_info_struct *si, >> return maxpages; >> } >> >> -static int setup_swap_map_and_extents(struct swap_info_struct *si, >> - union swap_header *swap_header, >> - unsigned char *swap_map, >> - unsigned long maxpages, >> - sector_t *span) >> +static int setup_swap_map(struct swap_info_struct *si, >> + union swap_header *swap_header, >> + unsigned char *swap_map, >> + unsigned long maxpages) >> { >> - unsigned int nr_good_pages; >> unsigned long i; >> - int nr_extents; >> - >> - nr_good_pages = maxpages - 1; /* omit header page */ >> >> + swap_map[0] = SWAP_MAP_BAD; /* omit header page */ >> for (i = 0; i < swap_header->info.nr_badpages; i++) { >> unsigned int page_nr = swap_header->info.badpages[i]; >> if (page_nr == 0 || page_nr > swap_header->info.last_page) >> return -EINVAL; >> if (page_nr < maxpages) { >> swap_map[page_nr] = SWAP_MAP_BAD; >> - nr_good_pages--; >> + si->pages--; >> } >> } >> >> - if (nr_good_pages) { >> - swap_map[0] = SWAP_MAP_BAD; >> - si->max = maxpages; >> - si->pages = nr_good_pages; >> - nr_extents = setup_swap_extents(si, span); >> - if (nr_extents < 0) >> - return nr_extents; >> - nr_good_pages = si->pages; >> - } >> - if (!nr_good_pages) { >> + if (!si->pages) { >> pr_warn("Empty swap-file\n"); >> return -EINVAL; >> } >> >> - return nr_extents; >> + return 0; >> } >> >> #define SWAP_CLUSTER_INFO_COLS \ >> @@ -3217,7 +3204,7 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si, >> * Mark unusable pages as unavailable. The clusters aren't >> * marked free yet, so no list operations are involved yet. >> * >> - * See setup_swap_map_and_extents(): header page, bad pages, >> + * See setup_swap_map(): header page, bad pages, >> * and the EOF part of the last cluster. >> */ >> inc_cluster_info_page(si, cluster_info, 0); >> @@ -3354,6 +3341,15 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) >> goto bad_swap_unlock_inode; >> } >> >> + si->max = maxpages; >> + si->pages = maxpages - 1; >> + nr_extents = setup_swap_extents(si, &span); >> + if (nr_extents < 0) { >> + error = nr_extents; >> + goto bad_swap_unlock_inode; >> + } >> + maxpages = si->max; >> + >> /* OK, set up the swap map and apply the bad block list */ >> swap_map = vzalloc(maxpages); >> if (!swap_map) { >> @@ -3365,12 +3361,9 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) >> if (error) >> goto bad_swap_unlock_inode; >> >> - nr_extents = setup_swap_map_and_extents(si, swap_header, swap_map, >> - maxpages, &span); >> - if (unlikely(nr_extents < 0)) { >> - error = nr_extents; >> + error = setup_swap_map(si, swap_header, swap_map, maxpages); >> + if (error) >> goto bad_swap_unlock_inode; >> - } >> >> /* >> * Use kvmalloc_array instead of bitmap_zalloc as the allocation order might >> -- >> 2.30.0 >> >