public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Kairui Song <ryncsn@gmail.com>
To: linux-mm@kvack.org
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Chris Li <chrisl@kernel.org>, Barry Song <v-songbaohua@oppo.com>,
	Ryan Roberts <ryan.roberts@arm.com>,
	Hugh Dickins <hughd@google.com>,
	Yosry Ahmed <yosryahmed@google.com>,
	"Huang, Ying" <ying.huang@linux.alibaba.com>,
	Baoquan He <bhe@redhat.com>, Nhat Pham <nphamcs@gmail.com>,
	Johannes Weiner <hannes@cmpxchg.org>,
	Kalesh Singh <kaleshsingh@google.com>,
	linux-kernel@vger.kernel.org, Kairui Song <kasong@tencent.com>
Subject: [PATCH v4 11/13] mm, swap: introduce a helper for retrieving cluster from offset
Date: Tue, 14 Jan 2025 01:57:30 +0800	[thread overview]
Message-ID: <20250113175732.48099-12-ryncsn@gmail.com> (raw)
In-Reply-To: <20250113175732.48099-1-ryncsn@gmail.com>

From: Kairui Song <kasong@tencent.com>

It's a common operation to retrieve the cluster info from offset,
introduce a helper for this.

Suggested-by: Chris Li <chrisl@kernel.org>
Signed-off-by: Kairui Song <kasong@tencent.com>
---
 mm/swapfile.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/mm/swapfile.c b/mm/swapfile.c
index 6da2f3aa55fb..37d540fa0310 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -424,6 +424,12 @@ static inline unsigned int cluster_index(struct swap_info_struct *si,
 	return ci - si->cluster_info;
 }
 
+static inline struct swap_cluster_info *offset_to_cluster(struct swap_info_struct *si,
+							  unsigned long offset)
+{
+	return &si->cluster_info[offset / SWAPFILE_CLUSTER];
+}
+
 static inline unsigned int cluster_offset(struct swap_info_struct *si,
 					  struct swap_cluster_info *ci)
 {
@@ -435,7 +441,7 @@ static inline struct swap_cluster_info *lock_cluster(struct swap_info_struct *si
 {
 	struct swap_cluster_info *ci;
 
-	ci = &si->cluster_info[offset / SWAPFILE_CLUSTER];
+	ci = offset_to_cluster(si, offset);
 	spin_lock(&ci->lock);
 
 	return ci;
@@ -1480,10 +1486,10 @@ static void swap_entry_range_free(struct swap_info_struct *si, swp_entry_t entry
 	unsigned char *map_end = map + nr_pages;
 	struct swap_cluster_info *ci;
 
-	/* It should never free entries across different clusters */
-	VM_BUG_ON((offset / SWAPFILE_CLUSTER) != ((offset + nr_pages - 1) / SWAPFILE_CLUSTER));
-
 	ci = lock_cluster(si, offset);
+
+	/* It should never free entries across different clusters */
+	VM_BUG_ON(ci != offset_to_cluster(si, offset + nr_pages - 1));
 	VM_BUG_ON(cluster_is_empty(ci));
 	VM_BUG_ON(ci->count < nr_pages);
 
-- 
2.47.1


  parent reply	other threads:[~2025-01-13 18:00 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-13 17:57 [PATCH v4 00/13] mm, swap: rework of swap allocator locks Kairui Song
2025-01-13 17:57 ` [PATCH v4 01/13] mm, swap: minor clean up for swap entry allocation Kairui Song
2025-01-13 17:57 ` [PATCH v4 02/13] mm, swap: fold swap_info_get_cont in the only caller Kairui Song
2025-01-13 17:57 ` [PATCH v4 03/13] mm, swap: remove old allocation path for HDD Kairui Song
2025-01-13 17:57 ` [PATCH v4 04/13] mm, swap: use cluster lock " Kairui Song
2025-01-13 17:57 ` [PATCH v4 05/13] mm, swap: clean up device availability check Kairui Song
2025-01-13 17:57 ` [PATCH v4 06/13] mm, swap: clean up plist removal and adding Kairui Song
2025-01-13 17:57 ` [PATCH v4 07/13] mm, swap: hold a reference during scan and cleanup flag usage Kairui Song
2025-01-13 17:57 ` [PATCH v4 08/13] mm, swap: use an enum to define all cluster flags and wrap flags changes Kairui Song
2025-01-13 17:57 ` [PATCH v4 09/13] mm, swap: reduce contention on device lock Kairui Song
2025-01-13 17:57 ` [PATCH v4 10/13] mm, swap: simplify percpu cluster updating Kairui Song
2025-01-13 17:57 ` Kairui Song [this message]
2025-01-13 17:57 ` [PATCH v4 12/13] mm, swap: use a global swap cluster for non-rotation devices Kairui Song
2025-01-15 10:51   ` Kairui Song
2025-01-13 17:57 ` [PATCH v4 13/13] mm, swap_slots: remove slot cache for freeing path Kairui Song

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250113175732.48099-12-ryncsn@gmail.com \
    --to=ryncsn@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=bhe@redhat.com \
    --cc=chrisl@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=hughd@google.com \
    --cc=kaleshsingh@google.com \
    --cc=kasong@tencent.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=nphamcs@gmail.com \
    --cc=ryan.roberts@arm.com \
    --cc=v-songbaohua@oppo.com \
    --cc=ying.huang@linux.alibaba.com \
    --cc=yosryahmed@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox