From nobody Sun Dec 14 06:15:48 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7A1E814A4ED for ; Thu, 11 Jul 2024 07:29:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720682961; cv=none; b=YCO63RaEoqJdSpvj9ronUXI9wAkAqYUbQ6sapa2uBLWPAFIeaoOe0OyXY8zAdbPM2pD0SLhQATbM8up7woEt7w6y2L8/fsD9Z3spgOAHQd6BYrJ2xDI1JLyR0I2fPFb5wizMW1Lmq8hMfT5wCr5FrZft64R4wwqTkvBJ6VvZHBc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720682961; c=relaxed/simple; bh=GQQhDBkbVm07F633nVzv9d4hoKaeH6pAoRTRgLjaQmQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=atpt6dXSVQ5WCSk1xqTayyJqXkP5i0e900N7jwPKJOSbCbrCJkox8cJjjOgFGYELArtn6hfunJq3Xk9cK6hIKWnZajQY3Pgh2OKv/PxxLdcWEhmykHp/1gSpg56PDFdm94l7gxC5qlrR+ipZQSsvIYdYJS4wzozEhQx7UExIxlA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=aeMOCOvu; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="aeMOCOvu" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 03A02C4AF0C; Thu, 11 Jul 2024 07:29:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1720682961; bh=GQQhDBkbVm07F633nVzv9d4hoKaeH6pAoRTRgLjaQmQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=aeMOCOvupqqC2Ej3StRoP1k7OPZxF8jo4beiiML+npBz7HZKH4qN/FxHXemgCWKl4 kP1rBI3p8z1t3UlHkyhh8gD1rACHQNRI19Z0KACtlrkrp7ShnMo/TtkNlUt9rZOtX1 EVkf9DPfQZIKVM6HBSxl+nULAKk1BtmI3EHx+a6kr56VVk6/wq5DMiFLlJTCIdXUfz XiFaRpxW/32iuxeNLx1GQQr0f/OwPBDnODLaCCGuUo0p8rFa0CcWVn3l9g+4V0f3tX QSWFyjLyxTlUZbH+EXqHPR2f7tFDCsrEkKB0mXK1S5jARo6rTfTHrT3GZPzpfY/gVv vecUSKI2Bfmtw== From: Chris Li Date: Thu, 11 Jul 2024 00:29:06 -0700 Subject: [PATCH v4 2/3] mm: swap: mTHP allocate swap entries from nonfull list Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20240711-swap-allocator-v4-2-0295a4d4c7aa@kernel.org> References: <20240711-swap-allocator-v4-0-0295a4d4c7aa@kernel.org> In-Reply-To: <20240711-swap-allocator-v4-0-0295a4d4c7aa@kernel.org> To: Andrew Morton Cc: Kairui Song , Hugh Dickins , Ryan Roberts , "Huang, Ying" , Kalesh Singh , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Chris Li , Barry Song X-Mailer: b4 0.13.0 Track the nonfull cluster as well as the empty cluster on lists. Each order has one nonfull cluster list. The cluster will remember which order it was used during new cluster allocation. When the cluster has free entry, add to the nonfull[order] list. =C2=A0When the free cluster list is empty, also allocate from the nonempty list of that order. This improves the mTHP swap allocation success rate. There are limitations if the distribution of numbers of different orders of mTHP changes a lot. e.g. there are a lot of nonfull cluster assign to order A while later time there are a lot of order B allocation while very little allocation in order A. Currently the cluster used by order A will not reused by order B unless the cluster is 100% empty. Signed-off-by: Chris Li --- include/linux/swap.h | 4 ++++ mm/swapfile.c | 34 +++++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index e9be95468fc7..db8d6000c116 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -254,9 +254,11 @@ struct swap_cluster_info { */ u16 count; u8 flags; + u8 order; struct list_head list; }; #define CLUSTER_FLAG_FREE 1 /* This cluster is free */ +#define CLUSTER_FLAG_NONFULL 2 /* This cluster is on nonfull list */ =20 =20 /* @@ -296,6 +298,8 @@ struct swap_info_struct { unsigned long *zeromap; /* vmalloc'ed bitmap to track zero pages */ struct swap_cluster_info *cluster_info; /* cluster info. Only for SSD */ struct list_head free_clusters; /* free clusters list */ + struct list_head nonfull_clusters[SWAP_NR_ORDERS]; + /* list of cluster that contains at least one free slot */ unsigned int lowest_bit; /* index of first free in swap_map */ unsigned int highest_bit; /* index of last free in swap_map */ unsigned int pages; /* total of usable pages of swap */ diff --git a/mm/swapfile.c b/mm/swapfile.c index f70d25005d2c..e13a33664cfa 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -361,14 +361,21 @@ static void swap_cluster_schedule_discard(struct swap= _info_struct *si, memset(si->swap_map + idx * SWAPFILE_CLUSTER, SWAP_MAP_BAD, SWAPFILE_CLUSTER); =20 - list_add_tail(&ci->list, &si->discard_clusters); + if (ci->flags) + list_move_tail(&ci->list, &si->discard_clusters); + else + list_add_tail(&ci->list, &si->discard_clusters); + ci->flags =3D 0; schedule_work(&si->discard_work); } =20 static void __free_cluster(struct swap_info_struct *si, struct swap_cluste= r_info *ci) { + if (ci->flags & CLUSTER_FLAG_NONFULL) + list_move_tail(&ci->list, &si->free_clusters); + else + list_add_tail(&ci->list, &si->free_clusters); ci->flags =3D CLUSTER_FLAG_FREE; - list_add_tail(&ci->list, &si->free_clusters); } =20 /* @@ -491,7 +498,12 @@ static void dec_cluster_info_page(struct swap_info_str= uct *p, struct swap_cluste ci->count--; =20 if (!ci->count) - free_cluster(p, ci); + return free_cluster(p, ci); + + if (!(ci->flags & CLUSTER_FLAG_NONFULL)) { + list_add_tail(&ci->list, &p->nonfull_clusters[ci->order]); + ci->flags |=3D CLUSTER_FLAG_NONFULL; + } } =20 /* @@ -550,6 +562,18 @@ static bool scan_swap_map_try_ssd_cluster(struct swap_= info_struct *si, if (tmp =3D=3D SWAP_NEXT_INVALID) { if (!list_empty(&si->free_clusters)) { ci =3D list_first_entry(&si->free_clusters, struct swap_cluster_info, l= ist); + list_del(&ci->list); + spin_lock(&ci->lock); + ci->order =3D order; + ci->flags =3D 0; + spin_unlock(&ci->lock); + tmp =3D cluster_index(si, ci) * SWAPFILE_CLUSTER; + } else if (!list_empty(&si->nonfull_clusters[order])) { + ci =3D list_first_entry(&si->nonfull_clusters[order], struct swap_clust= er_info, list); + list_del(&ci->list); + spin_lock(&ci->lock); + ci->flags =3D 0; + spin_unlock(&ci->lock); tmp =3D cluster_index(si, ci) * SWAPFILE_CLUSTER; } else if (!list_empty(&si->discard_clusters)) { /* @@ -964,6 +988,7 @@ static void swap_free_cluster(struct swap_info_struct *= si, unsigned long idx) ci =3D lock_cluster(si, offset); memset(si->swap_map + offset, 0, SWAPFILE_CLUSTER); ci->count =3D 0; + ci->order =3D 0; ci->flags =3D 0; free_cluster(si, ci); unlock_cluster(ci); @@ -2919,6 +2944,9 @@ static int setup_swap_map_and_extents(struct swap_inf= o_struct *p, INIT_LIST_HEAD(&p->free_clusters); INIT_LIST_HEAD(&p->discard_clusters); =20 + for (i =3D 0; i < SWAP_NR_ORDERS; i++) + INIT_LIST_HEAD(&p->nonfull_clusters[i]); + for (i =3D 0; i < swap_header->info.nr_badpages; i++) { unsigned int page_nr =3D swap_header->info.badpages[i]; if (page_nr =3D=3D 0 || page_nr > swap_header->info.last_page) --=20 2.45.2.803.g4e1b14247a-goog