From nobody Sun Feb 8 20:52:15 2026 Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) (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 3E6AE1D935C for ; Tue, 24 Dec 2024 14:40:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735051214; cv=none; b=MEr+J9kCqUDgiP1pEYNg7BSvzpgDVVijC/50hMgB/q8Z/N36g2zYV4U4lghqdjei7SlgeVNddi8TN8Ft29kqdasGv4Uc3TmvBwTTs223hZQh0bFd7gXBGjbpYe4eFiaTmezmn+XxcL3BBVBFxk/z0tuYZ1lPWvpP7b7KiYwZYps= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735051214; c=relaxed/simple; bh=+pRwarSS3rJ9eQRZ3l11H4YiyDvqMpDS+WyC8vBC0lA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=vGPeGn9oMvzXxVe93A7JQ7GwTiN7GGDkeCMQ+S/MmyqBj9ZFSKFVREFcgke4gsB2hp1ZWtqy4a0sX9l3bt8uHZHGsRK7i0E8ZoqftnEYLgRlKxVQgAXoHwsBwwypExTqOQoqdmRC/171Yrd0WNTz4kksACpDGjJxLXxroQabq8g= 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=LcvIhDbK; arc=none smtp.client-ip=209.85.216.50 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="LcvIhDbK" Received: by mail-pj1-f50.google.com with SMTP id 98e67ed59e1d1-2f4409fc8fdso4499353a91.1 for ; Tue, 24 Dec 2024 06:40:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1735051211; x=1735656011; 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=OeMbPM60Q8v3Qfaz9R6/9c2/Ea7K6oyXleY7O4H/Tp8=; b=LcvIhDbKbPDzKykNlz+uaLoGgS8Tp29n/PPVtynTp+ml5PnDuuuPOqn8KEIckvJKk2 V7RQPpI34553A0flsJcHmKE2sEEzqYGAXnfHyJrAIQ+u/QSRtLESAmi1/1LQfrl1HdJB ArZs0RFx0G/4/b11TZEXmdrQWW1ijIvR+Z6kMfH+5SX8pot6EvGqNpx2uTciN1xWj0zo memQcRLEOnHZON/+6Odalpry0+XA9W0GLk3ZPAgDK9UcmRJuv9rFBlpG27uhMXIadzzo r6nS5+GLlTYk4l+hXUAf/mWtKn8sgfwLSp1N+nCMCR4RfBo/Wpq38B+KyAMAZxTtLr6x OsuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735051211; x=1735656011; 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=OeMbPM60Q8v3Qfaz9R6/9c2/Ea7K6oyXleY7O4H/Tp8=; b=bi+BGiRWQzmIK/tJuG6CFTRJdWIhOlg86yfS+gd5INIvIFCRCi5zZMKjNaRYW/P6Lu uAK/TQ8/XTI3tpzDmWz6WQWSJhEX75/ymQiKcnSUgZlfGcNipk+RsyYRX9zIB4/DKviw nCYe/fdYyJuuvqOuOILwqo6OH7G1ZGqrGy+yubZLDPoMW9hQ4DZafm+k8GV2en7r0lry S7p7mONl+vCNj2s2C2Rc8UihH/V2rnr+zpDKlHnqVwK2xq1+krFkoQ7qxg/UQWykRv68 g5BT1IwEDI35TP9JZUTi4fI67SHOtrljbiOp7jRy882tjpcd37BROUIMwG88SnXQzvqA 1GXg== X-Forwarded-Encrypted: i=1; AJvYcCVZHmfPaG59Zl0V/3txusHZz16o1Yqy4FUJOk8koBQ781/mszouBP7q6pKXwSV6ujOxLx0fzh8MeaD3P40=@vger.kernel.org X-Gm-Message-State: AOJu0YyetWT/m64lUmrHnLXavD2vm5pLqKTVCZp8dAfRmUaE+XQMyf5w nEa7nZ3uamZuK/dQk9yUU9y+kaHqe7M481/ygWrkTvwSLASeqNeE X-Gm-Gg: ASbGncsHRWQFYcy0A14Si6TKTRWh4Fte998xJaqgY8m+2rL8l+BJyGOjJdxNuISyRN8 cWX6SNYBIuvkLLEkEk7FFif4uKJkH8dii+lp3C0TdjdEecfPrF8sq9N4TZ3r6gSOGAnOP/wQadq ZcmHIi0xzrm76Jgk2jm+IuQR+IHXOWsCOv5pvovK+6tFHmp/QiBXmBLnP/aPTNRiNv5ihjNSJRd rhDpIXyvGxhDAHZeTKs4fQs8KQS1eaRaKWAIV1y3fH+V6zhDJvWqTFpO9WDwFNjOMDPk2Yid70/ pQ== X-Google-Smtp-Source: AGHT+IGbB/29Y+1ZgUGrQ+JERD995978jUTdlYdu6QWEKgLiCQEmWerJkgBsrHNrNTQtYolF6Xq0WQ== X-Received: by 2002:a17:90a:d64f:b0:2ee:9661:eafb with SMTP id 98e67ed59e1d1-2f4536d18a7mr24570769a91.12.1735051211431; Tue, 24 Dec 2024 06:40:11 -0800 (PST) Received: from KASONG-MC4.tencent.com ([115.171.41.189]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-219dc9cdec1sm90598735ad.136.2024.12.24.06.40.07 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 24 Dec 2024 06:40:11 -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" , Nhat Pham , Johannes Weiner , Kalesh Singh , linux-kernel@vger.kernel.org, Kairui Song Subject: [PATCH v2 10/13] mm, swap: simplify percpu cluster updating Date: Tue, 24 Dec 2024 22:38:08 +0800 Message-ID: <20241224143811.33462-11-ryncsn@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241224143811.33462-1-ryncsn@gmail.com> References: <20241224143811.33462-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 bb1ef9192d99..ac170acf55a7 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -759,23 +759,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; @@ -789,34 +789,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_free(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 */ @@ -885,8 +878,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_free(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); } @@ -897,8 +890,8 @@ static unsigned long cluster_alloc_swap_entry(struct sw= ap_info_struct *si, int o new_cluster: ci =3D cluster_isolate_lock(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; } @@ -911,8 +904,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 cluster_isolate_lock(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); /* * With `fragmenting` set to true, it will surely take * the cluster off nonfull list @@ -932,8 +925,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++; @@ -959,21 +952,20 @@ static unsigned long cluster_alloc_swap_entry(struct = swap_info_struct *si, int o */ while ((ci =3D cluster_isolate_lock(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 cluster_isolate_lock(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; @@ -3190,7 +3182,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