From patchwork Tue Oct 22 19:24:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kairui Song X-Patchwork-Id: 13846084 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 BE02ECDD0CB for ; Tue, 22 Oct 2024 19:30:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 321A76B009F; Tue, 22 Oct 2024 15:30:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 25DDF6B00A1; Tue, 22 Oct 2024 15:30:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0B1A06B00A2; Tue, 22 Oct 2024 15:30:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id DBAEE6B009F for ; Tue, 22 Oct 2024 15:30:24 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id AE012160426 for ; Tue, 22 Oct 2024 19:30:05 +0000 (UTC) X-FDA: 82702229400.29.A7BE50D Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) by imf03.hostedemail.com (Postfix) with ESMTP id 03B2320019 for ; Tue, 22 Oct 2024 19:30:14 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=BFDl+239; spf=pass (imf03.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=ryncsn@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=1729625346; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=6X+VEn+3kCPXJIqxEfgPvCWyrBXaWqi+AOuTj72rupI=; b=KEC0SKKtsjqE/CFxd82PDFkZaHtAIK/MrnrovTH3iM39/EScI+SGnGrps2a9PgGrWwS+iE Vu5ItwrVN5+65ttUUdOxYvxsccvZYj4DNf+Nza7LoDQRWMBkREM8ZKUkx1E27KdfUq7fd/ frkQNLiHIyPIc8EFI0fw2eGqwXeRFqE= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=BFDl+239; spf=pass (imf03.hostedemail.com: domain of ryncsn@gmail.com designates 209.85.214.173 as permitted sender) smtp.mailfrom=ryncsn@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729625346; a=rsa-sha256; cv=none; b=lJiH7WexhRiRzpTAbFKzh/ijpVLmK3neGfrBiXPSfPv/vPjfZlKmM+fP4hnVWq0/6UF3iG FEFbSI1IipmYX2A7dZUxntuI4Brouk7Hj0rhxgZS2OScjhjvsNGe6JvYj8jvgcIdEda1co tfWOdAdtMk/7FVF/r5NSPeEmjVERGvY= Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-20c714cd9c8so60047555ad.0 for ; Tue, 22 Oct 2024 12:30:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729625421; x=1730230221; darn=kvack.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=6X+VEn+3kCPXJIqxEfgPvCWyrBXaWqi+AOuTj72rupI=; b=BFDl+239OcD4qUouwrjjFrq+jy+TsJMtTWgH29DzeRgi8eIJ1yDDKCL1Ruq1qJPF7I kAIe3BnxSeCM35oH4D1JQAy/C1JUCivukoT+Upiv/v1D21GbmXSM0WSL2Q7P0zTvkqKd 0NOnny2WYc8vivs28GhwWKi8bbs1ovMhuvAO+cTTA99leUcTCdX/jMwKJl+NIcamlN7Z TrFPyKOxL3eje9KYQ+GySL0Qpu34g7ZHxofbvRYicvPO6xLj894wbhRcBwtk/SDG+ur7 01CE8pmzFUrKeWpMWtlU7Mb9WGVTO5FGMRTfypwtmpmhNcKLszSEZoy/R1TxNOWbLMMS uYuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729625421; x=1730230221; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=6X+VEn+3kCPXJIqxEfgPvCWyrBXaWqi+AOuTj72rupI=; b=mUUTzatTykwjdGrt+gO9w0TCfalifovVx5CpofBew6RukpqsEm7TkeH8PfioUNgh0C cJvhR9USHst6/JvlsPGEyROKAO76fLsKNiA6ZZcu8ELhydXVzt7UKvJ3xcdOaB1bX2RA wd5kWyss3MhMWw1wY6SmUnYkhUTXacugkSlg4LQy3TdRWR7+2hHjgNeb7RMoP6gt3HaV WhgZKuRwO+HXyge0QJxnRTSazfavjyd8IL1u9s3AhGAzKvsMZ4ZEEwUHjANbo6oMI/sI SeOEHVIt1qwNJUswXltM1OjWAzUZMn7omSHfIcsohRydTJ30NtfE6739GfyFjJvR5Ngj 1rKA== X-Gm-Message-State: AOJu0Yw4ydaz4NuUNKqU1j1uwZ9B8sNfwQDXweLxNkEU18nnkscJlZnp HpzlhIoWiXy3bWyyjotazYo9ZrVsuirbkxog33TdWxbbkU/5Tv6iY/gmvYLdPhw= X-Google-Smtp-Source: AGHT+IFwn5W3d+VVRyPS+Ks6t1Wg9f/VfrmjaEp3PoLGs96xVemWYbaZI0DuQ0MSgKZGu5ZlHrzEeA== X-Received: by 2002:a17:902:e74f:b0:20c:7898:a8f4 with SMTP id d9443c01a7336-20fab2d9835mr2557925ad.60.1729625420908; Tue, 22 Oct 2024 12:30:20 -0700 (PDT) Received: from KASONG-MC4.tencent.com ([106.37.123.36]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20e7f0d9f05sm45895305ad.186.2024.10.22.12.30.17 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 22 Oct 2024 12:30:20 -0700 (PDT) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , Chris Li , Barry Song , Ryan Roberts , Hugh Dickins , Yosry Ahmed , "Huang, Ying" , Tim Chen , Nhat Pham , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH 10/13] mm, swap: simplify percpu cluster updating Date: Wed, 23 Oct 2024 03:24:48 +0800 Message-ID: <20241022192451.38138-11-ryncsn@gmail.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241022192451.38138-1-ryncsn@gmail.com> References: <20241022192451.38138-1-ryncsn@gmail.com> Reply-To: Kairui Song MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 03B2320019 X-Stat-Signature: znr4xr8qqwe4r7ub4h6gwc6suxk1jahe X-HE-Tag: 1729625414-248419 X-HE-Meta: U2FsdGVkX1+xmBXIdngnPC7fW8NElfbc5xL63bKAVok/UMxAwoDkpT6RlqDSA1mbC2QNFkfUXP2UckidHxdlmusPgAuGjoH9LKT19Jw6hQtL/bntxTQdEKyZ2a336/F2Wvw4EXAlFFDEPWi8kjGd1v4D5YFPREJXgbiReyzh8bZa+lOLQVWdiEvLkNrAXE+JLOw2Zt2aaeAb0IqXCzZ00eLlh+iDe6Zeyak4O9W5wi8YDZqaXbRJ3Bqeio0bHjvIEfR+qm288KLWL0JfsP5tA9xydGq9wlMVfQ3MJApDO/Ci+vut9qrm7MPLg8GoyG//Ml8XKbVMOdpFBzxgpU2WAxlpA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tJnljQp5SVxeOQ3LspXbx5S9hV3LzTGC3tDQofZPGobSaRiEgKD0vFoSz87jG7JoPmG6d6cr64wUc5ZMJCWM3+ieXfiaIkvwLDCd9fVxHA0rBqfPg== 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: From: Kairui Song Instead of using a returning argument, we can simply store the next cluster offset to the fixed percpu location, which reduce the stack usage and simplify the function: Object size: ./scripts/bloat-o-meter mm/swapfile.o mm/swapfile.o.new add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-271 (-271) Function old new delta get_swap_pages 2847 2733 -114 alloc_swap_scan_cluster 894 737 -157 Total: Before=30833, After=30562, chg -0.88% Stack usage: Before: swapfile.c:1190:5:get_swap_pages 240 static After: swapfile.c:1185:5:get_swap_pages 216 static Signed-off-by: Kairui Song --- include/linux/swap.h | 4 ++-- mm/swapfile.c | 57 ++++++++++++++++++++------------------------ 2 files changed, 28 insertions(+), 33 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index a3b5d74b095a..0e6c6bb385f0 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -276,9 +276,9 @@ enum swap_cluster_flags { * The first page in the swap file is the swap header, which is always marked * bad to prevent it from being allocated as an entry. This also prevents the * cluster to which it belongs being marked free. Therefore 0 is safe to use as - * a sentinel to indicate next is not valid in percpu_cluster. + * a sentinel to indicate an entry is not valid. */ -#define SWAP_NEXT_INVALID 0 +#define SWAP_ENTRY_INVALID 0 #ifdef CONFIG_THP_SWAP #define SWAP_NR_ORDERS (PMD_ORDER + 1) diff --git a/mm/swapfile.c b/mm/swapfile.c index a19ee8d5ffd0..f529e2ce2019 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -743,11 +743,14 @@ static void cluster_alloc_range(struct swap_info_struct *si, struct swap_cluster ci->count += nr_pages; } -static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si, unsigned long offset, - unsigned int *foundp, unsigned int order, +/* Try use a new cluster for current CPU and allocate from it. */ +static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si, + unsigned long offset, + unsigned int order, unsigned char usage) { - unsigned long start = offset & ~(SWAPFILE_CLUSTER - 1); + unsigned int next = SWAP_ENTRY_INVALID, found = SWAP_ENTRY_INVALID; + unsigned long start = ALIGN_DOWN(offset, SWAPFILE_CLUSTER); unsigned long end = min(start + SWAPFILE_CLUSTER, si->max); unsigned int nr_pages = 1 << order; bool need_reclaim, ret; @@ -756,10 +759,8 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si, unsigne ci = &si->cluster_info[offset / SWAPFILE_CLUSTER]; lockdep_assert_held(&ci->lock); - if (end < nr_pages || ci->count + nr_pages > SWAPFILE_CLUSTER) { - offset = SWAP_NEXT_INVALID; + if (end < nr_pages || ci->count + nr_pages > SWAPFILE_CLUSTER) goto out; - } for (end -= nr_pages; offset <= end; offset += nr_pages) { need_reclaim = false; @@ -773,10 +774,8 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si, unsigne * cluster has no flag set, and change of list * won't cause fragmentation. */ - if (!cluster_is_usable(ci, order)) { - offset = SWAP_NEXT_INVALID; + if (!cluster_is_usable(ci, order)) goto out; - } if (cluster_is_free(ci)) offset = start; /* Reclaim failed but cluster is usable, try next */ @@ -784,20 +783,17 @@ static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si, unsigne continue; } cluster_alloc_range(si, ci, offset, usage, order); - *foundp = offset; - if (ci->count == SWAPFILE_CLUSTER) { - offset = SWAP_NEXT_INVALID; - goto out; - } + found = offset; offset += nr_pages; + if (ci->count < SWAPFILE_CLUSTER && offset <= end) + next = offset; break; } - if (offset > end) - offset = SWAP_NEXT_INVALID; out: relocate_cluster(si, ci); unlock_cluster(ci); - return offset; + __this_cpu_write(si->percpu_cluster->next[order], next); + return found; } /* Return true if reclaimed a whole cluster */ @@ -866,8 +862,8 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o if (cluster_is_usable(ci, order)) { if (cluster_is_free(ci)) offset = cluster_offset(si, ci); - offset = alloc_swap_scan_cluster(si, offset, &found, - order, usage); + found = alloc_swap_scan_cluster(si, offset, + order, usage); } else { unlock_cluster(ci); } @@ -878,8 +874,8 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o new_cluster: ci = cluster_isolate_lock(si, &si->free_clusters); if (ci) { - offset = alloc_swap_scan_cluster(si, cluster_offset(si, ci), - &found, order, usage); + found = alloc_swap_scan_cluster(si, cluster_offset(si, ci), + order, usage); /* * Allocation from free cluster must never fail and * cluster lock must remain untouched. @@ -896,8 +892,8 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o unsigned int frags = 0, frags_existing; while ((ci = cluster_isolate_lock(si, &si->nonfull_clusters[order]))) { - offset = alloc_swap_scan_cluster(si, cluster_offset(si, ci), - &found, order, usage); + found = alloc_swap_scan_cluster(si, cluster_offset(si, ci), + order, usage); /* * With `fragmenting` set to true, it will surely take * the cluster off nonfull list @@ -917,8 +913,8 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o * per-CPU usage, but either way they could contain * usable (eg. lazy-freed swap cache) slots. */ - offset = alloc_swap_scan_cluster(si, cluster_offset(si, ci), - &found, order, usage); + found = alloc_swap_scan_cluster(si, cluster_offset(si, ci), + order, usage); if (found) goto done; frags++; @@ -944,21 +940,20 @@ static unsigned long cluster_alloc_swap_entry(struct swap_info_struct *si, int o */ while ((ci = cluster_isolate_lock(si, &si->frag_clusters[o]))) { atomic_long_dec(&si->frag_cluster_nr[o]); - offset = alloc_swap_scan_cluster(si, cluster_offset(si, ci), - &found, order, usage); + found = alloc_swap_scan_cluster(si, cluster_offset(si, ci), + 0, usage); if (found) goto done; } while ((ci = cluster_isolate_lock(si, &si->nonfull_clusters[o]))) { - offset = alloc_swap_scan_cluster(si, cluster_offset(si, ci), - &found, order, usage); + found = alloc_swap_scan_cluster(si, cluster_offset(si, ci), + 0, usage); if (found) goto done; } } done: - __this_cpu_write(si->percpu_cluster->next[order], offset); local_unlock(&si->percpu_cluster->lock); return found; @@ -3150,7 +3145,7 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si, cluster = per_cpu_ptr(si->percpu_cluster, cpu); for (i = 0; i < SWAP_NR_ORDERS; i++) - cluster->next[i] = SWAP_NEXT_INVALID; + cluster->next[i] = SWAP_ENTRY_INVALID; local_lock_init(&cluster->lock); }