From patchwork Mon Mar 11 15:00:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13588930 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 B256FC54E60 for ; Mon, 11 Mar 2024 15:01:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7FBA96B00AB; Mon, 11 Mar 2024 11:01:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7AB296B00AC; Mon, 11 Mar 2024 11:01:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 673766B00AD; Mon, 11 Mar 2024 11:01:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 5485C6B00AB for ; Mon, 11 Mar 2024 11:01:24 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 06B62A0A3F for ; Mon, 11 Mar 2024 15:01:21 +0000 (UTC) X-FDA: 81885071562.25.CE9FE1E Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf24.hostedemail.com (Postfix) with ESMTP id 4AB86180044 for ; Mon, 11 Mar 2024 15:01:18 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf24.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1710169278; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JhGMTpj5yC0+MzmgutXJj1JLPcuamOP+qjERV2HcV3w=; b=ny1IgeWCcz0ZfzI/U8KjwNE6KW26DJX3zfi8VjBvzROgJS+iarQvTv4GuXGV3WLPUJIprl lWf1JS81K+Ij+oMgAz0PMH3UYAW5BmKkXVKRZGfgWfqB8eZbrHpHhbbj59vIbgIPxWEVia jnvg0Wb7NBzV77zUK2el/oYt0HA6eEc= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf24.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710169278; a=rsa-sha256; cv=none; b=tw41rUPAZ8Ta669WqAS/S4GTfjG2r9SujrtAsaUT1Q9mZv7wssX5O3gMrYjL+tUm0LJnJ8 nzmIm+ZJMihsuTxSS7MaEo0GtmH3gbweg95G/JXQ3qhv/lLHtnY9/lK9SuQ7qnTv0vv00G SfP4K1ibP04mYi8SYAbIfSrbP4Y7u78= Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 1953C1570; Mon, 11 Mar 2024 08:01:54 -0700 (PDT) Received: from e125769.cambridge.arm.com (e125769.cambridge.arm.com [10.1.196.27]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 3126C3F64C; Mon, 11 Mar 2024 08:01:15 -0700 (PDT) From: Ryan Roberts To: Andrew Morton , David Hildenbrand , Matthew Wilcox , Huang Ying , Gao Xiang , Yu Zhao , Yang Shi , Michal Hocko , Kefeng Wang , Barry Song <21cnbao@gmail.com>, Chris Li Cc: Ryan Roberts , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 3/6] mm: swap: Simplify struct percpu_cluster Date: Mon, 11 Mar 2024 15:00:55 +0000 Message-Id: <20240311150058.1122862-4-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240311150058.1122862-1-ryan.roberts@arm.com> References: <20240311150058.1122862-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 4AB86180044 X-Stat-Signature: r645mgbry4k35dn8cn3ai13ckher4whd X-HE-Tag: 1710169278-740556 X-HE-Meta: U2FsdGVkX19YXllZ9gLbLESeZWGCje6a0SUtJV3JUz2Ep1Y79oM7o3IeYENc7pvsOeu3nI+bCWcRYtFZ58/XPb9k9w6WBSJ+lhLeugLx3b53viiPfsTwu4q22RydH+9I5Ml4C0fe2OLp0NJzxuvn42SBeJaRMvlByPu+k/39HWVSY+bV4EINLK/JkKFjI6PzchtPvTlZ22+dGNWkwhdRGCtkMnuDAHZXp2goCT4D6b001oP0xMgxyMxoVqZQ8JY0pIdMB5xv7xrOdwz8U0c7p7S0VEQt8DC2YEd1a488ROVnduepNKfXRgO/x5nE/MClafg7aAhn/2gmcVS5cAdcVaBnKyS14/KLQl8EKQbhlsmawQjoDoSPtamDJTzXOL0rttMLB0sjRRTbPCXrKpvY4/zPD65ezs5vt4BJ5iQ+zPmi6Uy/G/aojRX0LGAABtMZyY383Zmx5yqq5r5wOAS4aTZVK/Md4/1dKwVhZsFy3fEGlSTZZ8TzpHWMxTmmf/iqmARf+t6caV0tTVyccfu+v1Ls9Q9dObP2O1HaWOYATtaVVz1oRMQQAAFza+BUYhc2LePmV3WSbK0jprXoN4msgUhHtdQHexQP4DhHx88B0uiKZxpVPaRIIJ3wm4R7f3c3Ik1BmnuldX2JH1azaYrcSK0Fx/4BXo/ZvkMrQYyr2h+gjCWbr0GOZbjTJi3jg+ui1dRz6YkIWqgA8SnjnbPnA6WndUow33LsXpspmGilLtf35W/XQ5fr7efpFILuEp+O1ejYcLtjKWfh9rLcDV3BCloTA3Q3pK0tcTJvSnKd4r0lWZa95Yq/ilHENV6TKYTPvPdjkJ1aFqz2G6UUWUqu6/gK7C0PA5tpi9QuB5Fzo8IKUf3SYP1jgwDEbC+ULiDtWyGfKlN2i09h1XBcDNuw49XduzoYSAjHQhDRm01deY4QO7BzKbjxb9B6xCBzyXC5cGjgXoDYDxkMY+i8Rmd iGmjcnCY 3JlDZHaDJPEFx5pyJGURLbFaYFVmJmFShxEFp2vJXy5itwuBIXeeTnrZtj7TbyNiOMaHflWzuNf/JsPc3s7/pnMWvtMBH8AsWPlk+I8mCZ+OCfHyw2PsirxDYDjRcAgcsH9ivm4UeYcTZ95N+BDMSqqIFmPQKOxIgaSexy6eWPyPBTU9KLvKlPmtbVEhjOS6SksR66Oq2JSY/lrY= 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: struct percpu_cluster stores the index of cpu's current cluster and the offset of the next entry that will be allocated for the cpu. These two pieces of information are redundant because the cluster index is just (offset / SWAPFILE_CLUSTER). The only reason for explicitly keeping the cluster index is because the structure used for it also has a flag to indicate "no cluster". However this data structure also contains a spin lock, which is never used in this context, as a side effect the code copies the spinlock_t structure, which is questionable coding practice in my view. So let's clean this up and store only the next offset, and use a sentinal value (SWAP_NEXT_INVALID) to indicate "no cluster". SWAP_NEXT_INVALID is chosen to be 0, because 0 will never be seen legitimately; 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, so it will never appear on the free list. This change saves 16 bytes per cpu. And given we are shortly going to extend this mechanism to be per-cpu-AND-per-order, we will end up saving 16 * 9 = 144 bytes per cpu, which adds up if you have 256 cpus in the system. Signed-off-by: Ryan Roberts Reviewed-by: "Huang, Ying" --- include/linux/swap.h | 9 ++++++++- mm/swapfile.c | 22 +++++++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index f2b7f204b968..0cb082bee717 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -260,13 +260,20 @@ struct swap_cluster_info { #define CLUSTER_FLAG_FREE 1 /* This cluster is free */ #define CLUSTER_FLAG_NEXT_NULL 2 /* This cluster has no next cluster */ +/* + * 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. + */ +#define SWAP_NEXT_INVALID 0 + /* * We assign a cluster to each CPU, so each CPU can allocate swap entry from * its own cluster and swapout sequentially. The purpose is to optimize swapout * throughput. */ struct percpu_cluster { - struct swap_cluster_info index; /* Current cluster index */ unsigned int next; /* Likely next allocation offset */ }; diff --git a/mm/swapfile.c b/mm/swapfile.c index ee7e44cb40c5..3828d81aa6b8 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -609,7 +609,7 @@ scan_swap_map_ssd_cluster_conflict(struct swap_info_struct *si, return false; percpu_cluster = this_cpu_ptr(si->percpu_cluster); - cluster_set_null(&percpu_cluster->index); + percpu_cluster->next = SWAP_NEXT_INVALID; return true; } @@ -622,14 +622,14 @@ static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si, { struct percpu_cluster *cluster; struct swap_cluster_info *ci; - unsigned long tmp, max; + unsigned int tmp, max; new_cluster: cluster = this_cpu_ptr(si->percpu_cluster); - if (cluster_is_null(&cluster->index)) { + tmp = cluster->next; + if (tmp == SWAP_NEXT_INVALID) { if (!cluster_list_empty(&si->free_clusters)) { - cluster->index = si->free_clusters.head; - cluster->next = cluster_next(&cluster->index) * + tmp = cluster_next(&si->free_clusters.head) * SWAPFILE_CLUSTER; } else if (!cluster_list_empty(&si->discard_clusters)) { /* @@ -649,9 +649,7 @@ static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si, * Other CPUs can use our cluster if they can't find a free cluster, * check if there is still free entry in the cluster */ - tmp = cluster->next; - max = min_t(unsigned long, si->max, - (cluster_next(&cluster->index) + 1) * SWAPFILE_CLUSTER); + max = min_t(unsigned long, si->max, ALIGN(tmp + 1, SWAPFILE_CLUSTER)); if (tmp < max) { ci = lock_cluster(si, tmp); while (tmp < max) { @@ -662,12 +660,13 @@ static bool scan_swap_map_try_ssd_cluster(struct swap_info_struct *si, unlock_cluster(ci); } if (tmp >= max) { - cluster_set_null(&cluster->index); + cluster->next = SWAP_NEXT_INVALID; goto new_cluster; } - cluster->next = tmp + 1; *offset = tmp; *scan_base = tmp; + tmp += 1; + cluster->next = tmp < max ? tmp : SWAP_NEXT_INVALID; return true; } @@ -3138,8 +3137,9 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) } for_each_possible_cpu(cpu) { struct percpu_cluster *cluster; + cluster = per_cpu_ptr(p->percpu_cluster, cpu); - cluster_set_null(&cluster->index); + cluster->next = SWAP_NEXT_INVALID; } } else { atomic_inc(&nr_rotate_swap);