Received: by 2002:ab2:3350:0:b0:1f4:6588:b3a7 with SMTP id o16csp1546859lqe; Mon, 8 Apr 2024 12:04:35 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXl4RrlcGgmPb7aMibOtEYWcviZXntJVjrPFThmyEjLK6gfs2q3bPQ3SYy0+6a6dZDtatMSrOp1vrEUMt16Sn692AGR96KcK0fWwn1Qbg== X-Google-Smtp-Source: AGHT+IFR6lJeMc1GAyEoh/1Q1IumbOxrq7mToE7cBEdtVqQz5hQAhFBd4Xgb+Zp2Z/BnSy/t7FR/ X-Received: by 2002:a05:6a20:a11e:b0:1a7:822b:1969 with SMTP id q30-20020a056a20a11e00b001a7822b1969mr2981130pzk.45.1712603075253; Mon, 08 Apr 2024 12:04:35 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712603075; cv=pass; d=google.com; s=arc-20160816; b=huO+t4q0ciJPZpLkIF2O6Rg4zzwYt6fOs28aeEcLRqQVJwdgibA9RglbP7eGG+oLvi s29f1fNKYMT4iTn1QUgI/fqwuowD8k5h3GINqwEQNF3tloGQdgEL5tDHsmXO5rMKRr46 ilPrZZjFvKOQMwr76Lhnw3qRDkoqhE+qss2sa1OxNWdtRERz64MJxNqiTUtz+UJE/OYA dJ+TdOaYVm3W/cetyVTfuZiJ3N7yDN6CUmkoDzPN2Wcm8SDtboq/oPNLfHTyyjisN/ON 61oEVJVxMCgXho1QJksfEfIa0z4MxMgDNuqfbP6kfDrDAhrgxOJlrHJ7GrWKvREs9z53 0gtA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=CPvydlf1x8WC7f0KwckZbYpQubLZswQl7673V7csftE=; fh=MB8+QTNUEgTlBmcy60zUGiCFWfCMLwY1+agpXa5QLEY=; b=TkNdJanfoGAW/Jdl9QyJvgP6A/EkdnQNFWrSuij+tZn6Ql8e7bApbY9g/HygYoCIZf 90vZHnyYdZICzvp5vMoZjxJyWSDbdgJQtZdHx5AABRFoaVCSnG0KgCzBHky8nXW+4rE5 iGKW0ABEYD8hlVcBLuuwJ8b6T0Hzz/M/0VpDmsb25q7UzGvNN2LQshMFWK2x5q5fqz7E 83TtZ3bSLuilUOBPYM34Oks8KHj2Af0Ab5ZIe0NO9cLBCatYpX53/zxPLUD66C1YnaPa mN9Y7hwwssVS4FPGs7ZL7Uhm8BdW3oVIA0cQrQGV7Vb0uqmzBtx1fjmHU0j2w8TcN/l5 adhQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-135824-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-135824-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id e15-20020a63e00f000000b005f0bc71b125si6709747pgh.428.2024.04.08.12.04.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Apr 2024 12:04:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-135824-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-135824-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-135824-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id A835FB27A60 for ; Mon, 8 Apr 2024 18:40:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EAA47145B0B; Mon, 8 Apr 2024 18:40:10 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A24E9145320 for ; Mon, 8 Apr 2024 18:40:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712601610; cv=none; b=TFq8Y4h3amT39xP2gZsPRu7Xn+8agGZYD7VsjwBwsR/q9+ZURVC4Hurj4ZQzw7skjf+mh2Vpof1+D1FVUGji6mXEb8s/SNi6DA4NjquiA7tICJnA2jQTvCB2kchvJGirIxR778Nl8wh5gKIpJloAebzibMNqwKOG+k3UeutxTPM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712601610; c=relaxed/simple; bh=VOMWL6tnm1JJL+a6dWquCD5ePvOakBfnT6cOYxQeJ5M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AuVBnpKknwDMP4LoDVxPN0USBpdpWq1EFnOlAi20ZbHjAfY5002EZl8TmPkhC2Q8uDvPCTLiYFzeeTm1Q2FwZjl2GLuLhiNH38FzrxPU+e4tUEPG0hPQkoBYjzkTPTlalIluCVFKJZcFoe3UjOHYpyw++d7cWyXLwe6Yl/9hARk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com 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 86AF0139F; Mon, 8 Apr 2024 11:40:38 -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 36AA53F766; Mon, 8 Apr 2024 11:40:06 -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 v7 3/7] mm: swap: Simplify struct percpu_cluster Date: Mon, 8 Apr 2024 19:39:42 +0100 Message-Id: <20240408183946.2991168-4-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240408183946.2991168-1-ryan.roberts@arm.com> References: <20240408183946.2991168-1-ryan.roberts@arm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 20c45757f2b2..e3f855475278 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; } @@ -3163,8 +3162,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); -- 2.25.1