From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E0B23606A31C for ; Mon, 30 Nov 2026 15:09:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=MB5B/viH Received: by kanga.kvack.org (Postfix) id C144C6B05B7; Mon, 30 Nov 2026 10:09:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BEC526B05B8; Mon, 30 Nov 2026 10:09:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B29686B05B9; Mon, 30 Nov 2026 10:09:48 -0500 (EST) 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 84CC26B05B7 for ; Mon, 30 Nov 2026 10:09:48 -0500 (EST) Received: from smtpin02.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 0996C809A3 for ; Mon, 30 Nov 2026 15:09:48 +0000 (UTC) X-FDA: 85492120056.02.5C347B3 Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf09.hostedemail.com (Postfix) with ESMTP id 43193140019 for ; Mon, 30 Nov 2026 15:09:46 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b="MB5B/viH"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf09.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796051386; b=Ukn/sCzhS6SOhOW1hxbcNgfpiPpVz19+CSwqTAOkwlsiEoGn70Dnp3gPWK235Q1/02+yj0 g25UI0CinVAIdHGM1q4dYO8cQf2cVw02ftwMz69tSLged9Q3et2ENpmsB1sUBLbv6fzu7z vo0IM8mKJzFVPRN26tXLXUJt0sHhB0Q= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796051386; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=K6IEN8+BYEJFVS3q+jwOrq/t/C8jkpDAYydYZJdCao0=; b=ODqk97cZ4No1i+Qp27IIRLRM/g+YxGfz5ZbgBkVSUL6/DzW1LsjJ9KJfOdOwI8J6X2/Pwq hQ54Bx2ESW/IGzsVNBh57sDfsBkuKSvmR3TmibPpdqzkOXFOUB1gZGHHKW/wWXYPlryCfi 5kUry7LKQMrUXUqd3QP+nad9gE/o/Ec= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b="MB5B/viH"; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf09.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 86B6A42286; Mon, 30 Nov 2026 15:09:45 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 946961F00896; Mon, 30 Nov 2026 15:09:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796051385; bh=K6IEN8+BYEJFVS3q+jwOrq/t/C8jkpDAYydYZJdCao0=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=MB5B/viHlzC0iFA4Aa4GpgeTJaOg5+0gF88cDU+CXUBxO8nDphWLX+0tcbZsae7Bq O6WXfx7QmBnPhWICrcePttgdMHyQgO2AtG6JtP3iGOCem/P2l0wxsmeN5NMUqL+/L9 mIiEh5il/ZWGGWxTMsEZSVRzfR3FwrV4b6aqS73s4YDfN/6tRCGcTD2mLmMO9j/1W8 1rm08e/cSGFuDekZx7NpsNge9FA0bADqcq32SK4RxXgc4zQ+wwnUq8Xs/f5Qib7TW0 QilVAqAjqjlgu2vh/wClYeWIwcOGQT9e4LyX7fKMPwyG8MUfM8NQkbzABt/v7nNVtu OzOaBnNnzDvHA== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v7 11/14] mm, swap: add xswap grow trigger on cluster allocation Reply-To: sashiko-reviews@lists.linux.dev To: "Baoquan He" , linux-mm@kvack.org Cc: akpm@linux-foundation.org, david@kernel.org, chrisl@kernel.org, kasong@tencent.com, urezki@gmail.com, hannes@cmpxchg.org, corbet@lwn.net, nphamcs@gmail.com, baohua@kernel.org, youngjun.park@lge.com, shikemeng@huaweicloud.com, yosry@kernel.org, chengming.zhou@linux.dev, kunwu.chan@gmail.com, klarasmodin@gmail.com, "Baoquan He" In-Reply-To: <20261129150755.191662-12-baoquan.he@linux.dev> References: <20261129150755.191662-1-baoquan.he@linux.dev> <20261129150755.191662-12-baoquan.he@linux.dev> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Mon, 30 Nov 2026 15:09:44 +0000 X-Rspamd-Queue-Id: 43193140019 X-Stat-Signature: g7a5j44kpzbw4h4k1f6o7hqjw6sj9h1k X-Rspam-User: X-Spam-Status: No, score=-10.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Rspamd-Server: rspam07 X-HE-Tag: 1796051386-440650 X-HE-Meta: U2FsdGVkX1+p7W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Pzb1F56YVkvlqE3OfejtysFjMN5atSsiJ5uJ20NlKIjb1jCGoXWnA1TmAmgu6SbXYjAf4J2cqvVb1cpDRBVQfSQQ/kDqtGvnEUt+rrR99fmNiykSKwxuJ8mlAwD9FmybG6+Z9EFBjwFEMx7FszIbSzYMbHClTsOOlyTW5qQh3iP33gZETnso/pkvGY3fv4CMafeeCusu1x0BLdfFFQZP7Itg8TCmGLR0O4IeSeeyAx6Kz8KsINbSST+RSXcWoOn0Orvt3SbojwW1WH9kLY4qDUF+stGP6AdjOzkizr/zvErafv2zv4NTqrq1SE7FdC05uwwNqE6Wn1VpsyDU62WW8vg6dbfIrBjyFq/KHPNsX+d6/IhUwYnzt80s4OwTR0cqxqdADpo6sYhapjrhv3YtTbtYrhOXeBUSf21Qye+wAe3s4ogb+vPTZ6ohb5YSILc18UJxxKsYlvUzIdgjPednqWqqAkgR5HiuMd7ybCTzgV/FABDUNrLc71n6rKIi72UnURzPZR0TdFRl3e0us9VgGRdDsNmmzlncGr55Fb4xe8h4FJ8tcUTIxF+fWKRIwI6gXKYNwgn7C/uEc0s/bEGkjDYavpAr8rTus4a4+foYQ0ca5+1jXm2NHRVVg59lmSuoVPT2TnQUitTrOYD8NVnjn4ZxgknlWLNHiWMPoBQnMTf0DF7I2sUfyHtvtpO4ERhLcf9QDlvgRUeCpFaXSQW/jy2s90mVW6NvLNqOQNY/q9gOoyi6GMaROsVHCmT9iS6D4PHqjxXM= Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Archived-By: Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] mm: missing SWP_WRITEOK check in cluster_alloc_swap_entry() fallback grow - [Medium] mm: premature xswap allocation failure on concurrent xswap_grow() bursts - [Medium] mm: swap: xswap_grow() 4-chunk cap defeats XSWAP_GROW_UNTIL target -- commit d8e2d7959fb110ba7aca71775df5f4699fac8fc6 Author: Baoquan He mm, swap: add xswap grow trigger on cluster allocation This commit adds proactive and fallback grow triggers in cluster_alloc_swap_entry() to map additional xswap cluster_info pages and publish new clusters onto free_clusters as swap usage rises. > diff --git a/mm/swapfile.c b/mm/swapfile.c > index 074c5114d129a..9dc6bdc1a6dec 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c > @@ -68,6 +68,21 @@ > #define XSWAP_GROW_CLUSTERS \ > max_t(unsigned long, PAGE_SIZE / sizeof(struct swap_cluster_info), 16) > =20 > +/* > + * Grow thresholds, as a percentage of the mapped range in use. The tar= get > + * sits below the next grow trigger, so a grow that follows a rise in us= age > + * does not immediately trigger another one. > + */ > +#define XSWAP_GROW_UNTIL 65 > +#define XSWAP_GROW_WHEN 85 > +#define XSWAP_GROW_CHUNKS 4 [ ... ] > @@ -1325,6 +1340,13 @@ static unsigned long cluster_alloc_swap_entry(stru= ct swap_info_struct *si, > if (order && !(si->flags & SWP_BLKDEV)) > return 0; > =20 > +#ifdef CONFIG_XSWAP > + /* Grow before allocating: mapping pages can sleep. */ > + if ((si->flags & SWP_XSWAP) && (si->flags & SWP_WRITEOK) && > + xswap_should_grow(si)) > + xswap_grow(si, false); > +#endif [ ... ] > @@ -1397,6 +1419,20 @@ static unsigned long cluster_alloc_swap_entry(stru= ct swap_info_struct *si, > if (found) > goto done; > } > + > +#ifdef CONFIG_XSWAP > + /* > + * A burst of allocators can hold every mapped cluster while usage is > + * still below the growth threshold, so grow before retrying. > + */ > + if (!found && (si->flags & SWP_XSWAP)) { > + if (READ_ONCE(si->nr_clusters_mapped) < > + READ_ONCE(si->nr_clusters_max)) > + xswap_grow(si, true); > + found =3D alloc_swap_scan_list(si, &si->free_clusters, > + folio, false); > + } > +#endif [Severity: Medium] Should this fallback grow also check si->flags & SWP_WRITEOK, and should xswap_publish_clusters() recheck SWP_WRITEOK under si->lock? Once swap_info_remove() clears SWP_WRITEOK during swapoff, every list scan in cluster_alloc_swap_entry() fails because isolate_lock_cluster() bails out when SWP_WRITEOK is clear. An in-flight order-0 allocator reaching this fallback then mistakes !found for cluster exhaustion and calls xswap_grow(si, true), sleeping to allocate and map new cluster_vm pages, linking new clusters onto si->free_clusters, and advancing si->nr_clusters_mapped on a device being torn down. > done: > if (!(si->flags & SWP_SOLIDSTATE)) > spin_unlock(&si->global_cluster_lock); > @@ -3986,8 +4022,39 @@ static unsigned long read_swap_header(struct swap_= info_struct *si, > } > =20 > #ifdef CONFIG_XSWAP > +/* > + * Link the new clusters and publish the larger range in one si->lock > + * section: linking first makes a cluster visible to an allocator only > + * after it is on the free list. @link_free is false for the first > + * chunk, whose free list is not initialized yet. > + */ > +static void xswap_publish_clusters(struct swap_info_struct *si, > + unsigned long start_idx, unsigned long nr, > + bool link_free) > +{ > + unsigned long i; > + > + spin_lock(&si->lock); > + if (link_free) { > + for (i =3D start_idx; i < start_idx + nr; i++) { > + struct swap_cluster_info *ci =3D &si->cluster_info[i]; > + > + /* A concurrent grower cannot take these: xswap_lock. */ > + if (ci->flags !=3D CLUSTER_FLAG_NONE) > + continue; > + list_add_tail(&ci->list, &si->free_clusters); > + WRITE_ONCE(ci->flags, CLUSTER_FLAG_FREE); > + ci->order =3D 0; > + } > + } > + /* Pairs with the smp_load_acquire() in the readers of the range. */ > + smp_store_release(&si->nr_clusters_mapped, start_idx + nr); > + spin_unlock(&si->lock); > +} > + > static int xswap_map_clusters(struct swap_info_struct *si, > - unsigned long start_idx, unsigned long nr) > + unsigned long start_idx, unsigned long nr, > + bool link_free) > { > /* vm_area_grow_pages() takes an offset into the area, not an address. = */ > unsigned long vm_end =3D PAGE_ALIGN((size_t)(start_idx + nr) * [ ... ] > @@ -4023,14 +4090,65 @@ static int xswap_map_clusters(struct swap_info_st= ruct *si, > for (i =3D start_idx; i < start_idx + nr; i++) > spin_lock_init(&si->cluster_info[i].lock); > =20 > - /* > - * Publish the new mappings and cluster lock initialization before > - * the count; walkers without xswap_lock use smp_load_acquire(). > - */ > - smp_store_release(&si->nr_clusters_mapped, start_idx + nr); > + /* The mappings and the locks are in place; make them visible. */ > + xswap_publish_clusters(si, start_idx, nr, link_free); > mutex_unlock(&si->xswap_lock); > return 0; > } > +static bool xswap_should_grow(struct swap_info_struct *si) > +{ > + unsigned long mapped =3D READ_ONCE(si->nr_clusters_mapped); > + > + if (mapped >=3D READ_ONCE(si->nr_clusters_max)) > + return false; > + > + return swap_usage_in_pages(si) * 100 >=3D > + mapped * SWAPFILE_CLUSTER * XSWAP_GROW_WHEN; > +} > + > +/* > + * Map more of the cluster_info array, up to GROW_UNTIL in use. The cal= ler > + * holds percpu_swap_cluster.lock, which this drops while it sleeps. > + */ > +static unsigned long xswap_grow(struct swap_info_struct *si, bool force) > +{ > + unsigned long mapped =3D READ_ONCE(si->nr_clusters_mapped); > + unsigned long nr_new, want; > + int ret; > + > + lockdep_assert_held(&this_cpu_ptr(&percpu_swap_cluster)->lock); > + > + want =3D DIV_ROUND_UP(swap_usage_in_pages(si) * 100, > + XSWAP_GROW_UNTIL * SWAPFILE_CLUSTER); > + if (want <=3D mapped) { > + if (!force) > + return 0; > + nr_new =3D XSWAP_GROW_CLUSTERS; > + } else { > + nr_new =3D rounddown(want - mapped, XSWAP_GROW_CLUSTERS); > + } > + if (!nr_new) > + nr_new =3D XSWAP_GROW_CLUSTERS; > + if (nr_new > XSWAP_GROW_CHUNKS * XSWAP_GROW_CLUSTERS) > + nr_new =3D XSWAP_GROW_CHUNKS * XSWAP_GROW_CLUSTERS; [Severity: Medium] Does clamping nr_new to XSWAP_GROW_CHUNKS * XSWAP_GROW_CLUSTERS defeat the XSWAP_GROW_UNTIL (65%) hysteresis target once si->nr_clusters_mapped reaches 13 chunks? Bringing usage from 85% down to 65% requires growing by at least 4/13 of mapped. Once mapped exceeds 13 * XSWAP_GROW_CLUSTERS (~2.16 GiB with 48 byte swap_cluster_info), want - mapped is always larger than 4 chunks, so xswap_grow() maps at most 4 chunks per call, keeping usage near 85% and causing back-to-back synchronous xswap_grow() calls on subsequent cluster_alloc_swap_entry() invocations. > + nr_new =3D min(nr_new, READ_ONCE(si->nr_clusters_max) - mapped); > + if (!nr_new) > + return 0; > + > + /* > + * Mapping pages can sleep. The lock only guards the per-cpu > + * cluster cache, which this path does not touch. > + */ > + local_unlock(&percpu_swap_cluster.lock); > + ret =3D xswap_map_clusters(si, mapped, nr_new, true); > + local_lock(&percpu_swap_cluster.lock); > + if (ret) > + return 0; > + > + atomic_long_inc(&si->xswap_grows); > + return nr_new; > +} [Severity: Medium] Can a burst of concurrent allocators still fail in cluster_alloc_swap_entry() while unmapped clusters and free slots in si->frag_clusters[0] remain? CPU0 CPU1 cluster_alloc_swap_entry() xswap_grow(si, true) mapped =3D si->nr_clusters_mapped xswap_map_clusters(si, mapped, ...) mutex_lock(&si->xswap_lock) vm_area_grow_pages() cluster_alloc_swap_entry() xswap_grow(si, true) mapped =3D si->nr_clusters_mapped xswap_map_clusters(si, mapped, ..= .) mutex_lock(&si->xswap_lock) xswap_publish_clusters() mutex_unlock(&si->xswap_lock) start_idx !=3D nr_clusters_mapp= ed return -EAGAIN While CPU0 sleeps in vm_area_grow_pages(), any concurrent allocator that also enters xswap_grow() snapshots the same si->nr_clusters_mapped and blocks on si->xswap_lock, only to abort with -EAGAIN after CPU0 publishes one chunk. In the fallback path, each waking thread then scans only si->free_clusters once, even though any newly published cluster from which another CPU has allocated a single slot is immediately moved by relocate_cluster() to si->frag_clusters[0] with SWAPFILE_CLUSTER - 1 free slots remaining. If concurrent allocators drain si->free_clusters before a waiter reaches alloc_swap_scan_list(si, &si->free_clusters, ...), cluster_alloc_swap_entry() returns 0 despite free slots in si->frag_clusters[0] and unmapped clusters below si->nr_clusters_max. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261129150755.1916= 62-1-baoquan.he@linux.dev?part=3D11