From nobody Thu Dec 18 20:36:31 2025 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 681A51C4617 for ; Mon, 13 Jan 2025 18:00:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736791232; cv=none; b=seiSF0cxgrNbPSLanmmFrFa+b5ad3sh+yBxi+N5tE0U2b1DVVizLRsTWAQ7nWIsxXPw6hZw8ZNzA3Vi9GT0lD5GQ7X9vbvoXvuRzh7wDugWNwzJgtHCW7sCAX5wLIPFqFQbZgL64LfCSzQ4N2k6Jhpx1rOxUJnfMDBAFVPrFz58= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736791232; c=relaxed/simple; bh=3izO00mkjWU4GzS6p74TzLvk4ZT0MF/QoBFCEC3IZ1s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ES+i9BGExk2D3Qp91wLxF6QBABJ5B3kCUO+nVPcouD20jADJ15VjpFqqV850eva195Nce3gnV5eLLJNyyTvQbj5lRZkK0Phgep32T+gKhRYFkZhYeJhWVINV33a9OFWtjIJbj5cXXAB3juwAOpBtWgmehywnY92XJvfOqEErkMk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=LDlQJm0r; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="LDlQJm0r" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-21661be2c2dso78046645ad.1 for ; Mon, 13 Jan 2025 10:00:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1736791230; x=1737396030; darn=vger.kernel.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=w3/A4K7Bt53NzbRkeVq3PRJRV4Gi+Nq+TM9VfYB4Gnc=; b=LDlQJm0rkVquabjEEkSglCJtrVtcrHY61BStBQUt7Dh+6cs2Hoy08wfWaZzSPennSw XZ/As0D9ICu3UeccwemdxwDvBY/gqSGXIJvVAqp2SRqfSsiF8oQuE6oPaLA+ZvL3Q0+Y sa36zieuk6JvEbwzG7ntPUrL2ub9BwUgmdHhBqZqEDjBfPrqsce1tYdEIFCcD036jd2e INCzx7hH/rYPHIfGj/vF0jvLCRYB2wtC2MOWFtIv7Rz/edFCNZ5wLvjBePO/mudf8Q04 9w6km8MSoF5e+jT4qOuzx4+9QIlbYWR5S9ochQd8jFu6H8BU0xfFr4J0ohzfUDv275a1 nh/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736791230; x=1737396030; 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=w3/A4K7Bt53NzbRkeVq3PRJRV4Gi+Nq+TM9VfYB4Gnc=; b=XACTjWoBhkCTXqDEe5XELJFOG1O9C8YGhTJlq1E+cqBg+d9ZJAGPJsawaPf5Zz/RrM HWul65DdNFX321V3RA2bGcv2FaRsH3Gnc3K/k3dDDgHPMpSDtR9gakeM+38XHMrS3Esx 3zqEXaaTq8Yz0PTpt/AF0RPfwPZ1AI93RN34uLWNSo/Z0eSKs63kf05uhfaB6JfLn4Du et2vAgphMHSy8iRiAkVfMxzghTbhc7V+9EYAaPR6Zv6a0Jw4SqG9ctQAnwuKNxMt/QLd MjBJ7GFkO98zV0wPY6QUNOskr0hc1YO8/OkIlYQMBh/WmJlfw7hE2VEpAqU6VE90o+I2 +/WA== X-Forwarded-Encrypted: i=1; AJvYcCUFwQrKZaQFVncNiWTc5hAyKHSKmDoPipzvfPua4FhZgbDBxgHSsw1hHa5Epu19jl2giNwXardRlKgNQn0=@vger.kernel.org X-Gm-Message-State: AOJu0YyQF/Y0FxEFF0XGJfaKsyLTOouk4e8V1g3kiF5V6YiufP4wlPkJ 9NYU2uNAan9i0Fw6U6fdmFEnDD8z+nMkCyvyWiBLstIhm0AKhM4AJthiuxOROJI= X-Gm-Gg: ASbGnctMAsJSzarSY6470peRDBEjMZyinbNoLVfyJ9xLSRXQtIrSehZg6qUN3NSDpe1 Vi2Q1LFsUkbjQL8/rAU8B74siaXdlx6fuY0GA0/n+zADfOc/VvZ7oc3Tsf12vrSO+98KeJQibmN 4TTewH38PVos7yxcSmLnD/RLSLadG1NDaO9GXKM5ykk2BlHv2w3htidgffueNq89mgErssLSfxK Gn3JwcWDkKVZsVUfG5Xxn3ND9N8TYKV+UQGYoRVdlZ8h+EnXuyaO3+asnpsOBDwvBZ3PAYlGpDx 8g== X-Google-Smtp-Source: AGHT+IEb+lacgc6xktF1aaBt7hTsj1nPaNZqgDFnny/A4HmF9V2aouf4o4ZmssdJsfpBSua/NdU+2Q== X-Received: by 2002:a17:902:ea09:b0:216:2a36:5b2e with SMTP id d9443c01a7336-21a83f76879mr320846885ad.32.1736791229397; Mon, 13 Jan 2025 10:00:29 -0800 (PST) Received: from KASONG-MC4.tencent.com ([115.171.41.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21a9f21aba7sm57023635ad.113.2025.01.13.10.00.26 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 13 Jan 2025 10:00:28 -0800 (PST) From: Kairui Song To: linux-mm@kvack.org Cc: Andrew Morton , Chris Li , Barry Song , Ryan Roberts , Hugh Dickins , Yosry Ahmed , "Huang, Ying" , Baoquan He , Nhat Pham , Johannes Weiner , Kalesh Singh , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH v4 10/13] mm, swap: simplify percpu cluster updating Date: Tue, 14 Jan 2025 01:57:29 +0800 Message-ID: <20250113175732.48099-11-ryncsn@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250113175732.48099-1-ryncsn@gmail.com> References: <20250113175732.48099-1-ryncsn@gmail.com> Reply-To: Kairui Song Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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=3D30833, After=3D30562, 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 | 66 +++++++++++++++++++------------------------- 2 files changed, 31 insertions(+), 39 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index c4ff31cb6bde..4c1d2e69689f 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -275,9 +275,9 @@ enum swap_cluster_flags { * The first page in the swap file is the swap header, which is always mar= ked * 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 u= se 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 =20 #ifdef CONFIG_THP_SWAP #define SWAP_NR_ORDERS (PMD_ORDER + 1) diff --git a/mm/swapfile.c b/mm/swapfile.c index 489ac6997a0c..6da2f3aa55fb 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -765,23 +765,23 @@ static bool cluster_alloc_range(struct swap_info_stru= ct *si, struct swap_cluster return true; } =20 -static unsigned int alloc_swap_scan_cluster(struct swap_info_struct *si, u= nsigned 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, + struct swap_cluster_info *ci, + unsigned long offset, + unsigned int order, unsigned char usage) { - unsigned long start =3D offset & ~(SWAPFILE_CLUSTER - 1); + unsigned int next =3D SWAP_ENTRY_INVALID, found =3D SWAP_ENTRY_INVALID; + unsigned long start =3D ALIGN_DOWN(offset, SWAPFILE_CLUSTER); unsigned long end =3D min(start + SWAPFILE_CLUSTER, si->max); unsigned int nr_pages =3D 1 << order; bool need_reclaim, ret; - struct swap_cluster_info *ci; =20 - ci =3D &si->cluster_info[offset / SWAPFILE_CLUSTER]; lockdep_assert_held(&ci->lock); =20 - if (end < nr_pages || ci->count + nr_pages > SWAPFILE_CLUSTER) { - offset =3D SWAP_NEXT_INVALID; + if (end < nr_pages || ci->count + nr_pages > SWAPFILE_CLUSTER) goto out; - } =20 for (end -=3D nr_pages; offset <=3D end; offset +=3D nr_pages) { need_reclaim =3D false; @@ -795,34 +795,27 @@ static unsigned int alloc_swap_scan_cluster(struct sw= ap_info_struct *si, unsigne * cluster has no flag set, and change of list * won't cause fragmentation. */ - if (!cluster_is_usable(ci, order)) { - offset =3D SWAP_NEXT_INVALID; + if (!cluster_is_usable(ci, order)) goto out; - } if (cluster_is_empty(ci)) offset =3D start; /* Reclaim failed but cluster is usable, try next */ if (!ret) continue; } - if (!cluster_alloc_range(si, ci, offset, usage, order)) { - offset =3D SWAP_NEXT_INVALID; - goto out; - } - *foundp =3D offset; - if (ci->count =3D=3D SWAPFILE_CLUSTER) { - offset =3D SWAP_NEXT_INVALID; - goto out; - } + if (!cluster_alloc_range(si, ci, offset, usage, order)) + break; + found =3D offset; offset +=3D nr_pages; + if (ci->count < SWAPFILE_CLUSTER && offset <=3D end) + next =3D offset; break; } - if (offset > end) - offset =3D SWAP_NEXT_INVALID; out: relocate_cluster(si, ci); unlock_cluster(ci); - return offset; + __this_cpu_write(si->percpu_cluster->next[order], next); + return found; } =20 /* Return true if reclaimed a whole cluster */ @@ -891,8 +884,8 @@ static unsigned long cluster_alloc_swap_entry(struct sw= ap_info_struct *si, int o if (cluster_is_usable(ci, order)) { if (cluster_is_empty(ci)) offset =3D cluster_offset(si, ci); - offset =3D alloc_swap_scan_cluster(si, offset, &found, - order, usage); + found =3D alloc_swap_scan_cluster(si, ci, offset, + order, usage); } else { unlock_cluster(ci); } @@ -903,8 +896,8 @@ static unsigned long cluster_alloc_swap_entry(struct sw= ap_info_struct *si, int o new_cluster: ci =3D isolate_lock_cluster(si, &si->free_clusters); if (ci) { - offset =3D alloc_swap_scan_cluster(si, cluster_offset(si, ci), - &found, order, usage); + found =3D alloc_swap_scan_cluster(si, ci, cluster_offset(si, ci), + order, usage); if (found) goto done; } @@ -917,8 +910,8 @@ static unsigned long cluster_alloc_swap_entry(struct sw= ap_info_struct *si, int o unsigned int frags =3D 0, frags_existing; =20 while ((ci =3D isolate_lock_cluster(si, &si->nonfull_clusters[order]))) { - offset =3D alloc_swap_scan_cluster(si, cluster_offset(si, ci), - &found, order, usage); + found =3D alloc_swap_scan_cluster(si, ci, cluster_offset(si, ci), + order, usage); if (found) goto done; /* Clusters failed to allocate are moved to frag_clusters */ @@ -935,8 +928,8 @@ static unsigned long cluster_alloc_swap_entry(struct sw= ap_info_struct *si, int o * per-CPU usage, but they could contain newly released * reclaimable (eg. lazy-freed swap cache) slots. */ - offset =3D alloc_swap_scan_cluster(si, cluster_offset(si, ci), - &found, order, usage); + found =3D alloc_swap_scan_cluster(si, ci, cluster_offset(si, ci), + order, usage); if (found) goto done; frags++; @@ -962,21 +955,20 @@ static unsigned long cluster_alloc_swap_entry(struct = swap_info_struct *si, int o */ while ((ci =3D isolate_lock_cluster(si, &si->frag_clusters[o]))) { atomic_long_dec(&si->frag_cluster_nr[o]); - offset =3D alloc_swap_scan_cluster(si, cluster_offset(si, ci), - &found, order, usage); + found =3D alloc_swap_scan_cluster(si, ci, cluster_offset(si, ci), + 0, usage); if (found) goto done; } =20 while ((ci =3D isolate_lock_cluster(si, &si->nonfull_clusters[o]))) { - offset =3D alloc_swap_scan_cluster(si, cluster_offset(si, ci), - &found, order, usage); + found =3D alloc_swap_scan_cluster(si, ci, 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); =20 return found; @@ -3200,7 +3192,7 @@ static struct swap_cluster_info *setup_clusters(struc= t swap_info_struct *si, =20 cluster =3D per_cpu_ptr(si->percpu_cluster, cpu); for (i =3D 0; i < SWAP_NR_ORDERS; i++) - cluster->next[i] =3D SWAP_NEXT_INVALID; + cluster->next[i] =3D SWAP_ENTRY_INVALID; local_lock_init(&cluster->lock); } =20 --=20 2.47.1