From patchwork Wed Mar 27 14:45:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13606751 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 37509CD11DD for ; Wed, 27 Mar 2024 14:46:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ADD506B00A0; Wed, 27 Mar 2024 10:45:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A65DE6B00A2; Wed, 27 Mar 2024 10:45:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8E28C6B00A3; Wed, 27 Mar 2024 10:45:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 5AF036B00A0 for ; Wed, 27 Mar 2024 10:45:57 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 27C0E16031F for ; Wed, 27 Mar 2024 14:45:57 +0000 (UTC) X-FDA: 81943093554.21.61609E2 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf07.hostedemail.com (Postfix) with ESMTP id 7CD434001A for ; Wed, 27 Mar 2024 14:45:55 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf07.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=1711550755; 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=jeW/8Xg51YBdRBeCo1i7YjaFCNT3MfYkaoHFC94xolc=; b=zJMfQwTgoFhNVRFUr93+WGe/05dP5afkCkU28ly1ZICK64hjkcNGTNJ5/2fMmwr0+HSA1H 2/aJzDzoKf2BvWVnbwDVW3TIZF5Uq6R6ibZHl8kiP3bUlAU5y4hSF84dSan44LkdbYjSf7 iYTK6RzMGCtFbAFsiumX0il4eRvHm/M= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf07.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=1711550755; a=rsa-sha256; cv=none; b=FTxV+hl7TGf6vF7w3uVLUZDTz4VHfumz/40N6bmH6PPQBXynxxqHA5i3zvOT6yPN49+jH2 52tA0rk6ADLziz4UnI2r/y95n2RTznNoaqvCsFHfBs4ZglnplKtKXTXw14TeYroPHjxbz1 6xmYneTbvTfG1FbE5Be3+SoSMK6voEw= 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 C84B62F4; Wed, 27 Mar 2024 07:46:28 -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 CDFE73F64C; Wed, 27 Mar 2024 07:45:52 -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 , Lance Yang Cc: Ryan Roberts , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v5 3/6] mm: swap: Simplify struct percpu_cluster Date: Wed, 27 Mar 2024 14:45:34 +0000 Message-Id: <20240327144537.4165578-4-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240327144537.4165578-1-ryan.roberts@arm.com> References: <20240327144537.4165578-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: mdpnqqj7g1zi5ej9k88btw4r7omcdkeb X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 7CD434001A X-HE-Tag: 1711550755-672751 X-HE-Meta: U2FsdGVkX18GS87Xum5Ja4zs/2hnGg6A8YkRNRkhO3fjspZNtwJ1z2kIIPsjzhizhpF7sEPoy0/feAEpuvNbv2o9Q3hht80Ak/fSExiY14PZg5XXX7pw8H1fCgjIwsycGiPANwxMY+xq4fQgx//IYuaHGOqT9C44i0HblAGCnEgfYZYvCGEjeEX50YA9FAeV2UkUBmyAE+aIFgvmLXFdtmaS7h7HvbZzWbHs6gbIDCtqaZJni0DOeMxGinirBI8Kd8CP0VaShL2azgYyWydqqi2WAZhA4HNW7jUOU0ogxz2LDhSJF3LW5E0BFNBPb5KGU3oTquqV7Nl6wYCC0fTqWIa/MSHwBKZUsl6O5Cn9FJjtb2ZE8C42bViHC2FNOTZVCff+SaB3lJgZnWDHXAzLnhidP1j92KuZMWAevFfg8QludemOaem3doUaLFHqCKP5esn6M3EqzI1ORtMw5QT3eZpb8cZSkv4bz0eABPjl/o7KaUWCUv36FGFrh5c1EjgycRxZ+hHhEfGU7niS+TXZ23idkS4khYVns8qM2dVDEfp91ntfKOnvfs3g17boZdN64TgDAJEoG8/AI44OBbIVj6RCUu7o0IjzuLuL3yeouhiwwQiGKqYRenJ+fLc5RtuXkZK8tRWpVI0ufmWPt9hIRPiTlu0IbOjRZGa4hAAiTMqJ9KL1KNxoNUR7YaAUctm1Ar+jXdwIWPkF7jZz1c4qusixbHm6PZZny4KYLHgL84LPvztLXqQBKYI/Ng03+4YoDn+MlI6brOxljZi0DbUlX9SaG9SYEjBc/dwn7JYT76Dg4Ur/Swau3bhMhVWVZlj9Tog/EHtmx+NY6qixNeRC2DL6F4ofN7mzDC1b2j5V5O649QrfHQamY4QsZSv5ySvT9Np5L9FIQEC26IFlmJsq57CN7IcRD0wDXa3cMnXAr+tubPbYzgBNh4mrsgDmHW2N1uFOvojrJ/r224IKHC8 w+CQSxIM 9J9LCs1msLxABGfmXxTCp/6Zp3LURxt/2b6ey96yXM42qDfFNw8YmuP0QOqFavybplwH9bo+dj3eAAkVp6CXwzbrIPBQiutOldrh2hc7ykOkOMmhIPoSzshucaLSKBVyTAqskMfsC6Y0sOitF+OwN7ywUQeCoSvM/4gyaxWFOEMBUGh5tSXVRF/bj7Y0M2hFgUPLHByTw4QmuSqiv06FvXwtdeZuYKjzwQKAc 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. Reviewed-by: "Huang, Ying" Signed-off-by: Ryan Roberts --- 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 5737236dc3ce..5e1e4f5bf0cb 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 cedfc82d37e5..1393966b77af 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);