From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lgeamrelo03.lge.com (lgeamrelo03.lge.com [156.147.51.102]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8C7093BE653 for ; Thu, 12 Mar 2026 11:25:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=156.147.51.102 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773314725; cv=none; b=j9Pa6nuSy0HRydDmNfEG2RjRmW/pu8VYgTeZc3fT/mcsig9pUqAWvs8elybeE1yqEGUeGZVZCPSiRyIVbmL68rNDNXV8YzL8cWQbgaoVJMjCus3VFWNM2ffzc+1Av1S9Z4tsF9RRygVtK0W84EkFA/8dc1uhekFOXB8aUT4T090= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773314725; c=relaxed/simple; bh=8sNTsTwdS2Ia7CLuaGAijuhstmMpeoWQ+Y10LgmmdoE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nGtbMLSWEw+1pJB87rIXPk3ed3pMI2DoRA5zTld6BlSD64gL097kcwpe8lORe8Klh1w1GrucmmkjCFiptl02idIEkoJo4UKX5HUZSe+UOEz3D4G9i144+ig26epbN02fyVYqtEmAs2catYQ+pH8JtG7NI+di9jzTGVU2iqP2xS4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=lge.com; spf=pass smtp.mailfrom=lge.com; arc=none smtp.client-ip=156.147.51.102 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=lge.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=lge.com Received: from unknown (HELO yjaykim-PowerEdge-T330.lge.net) (10.177.112.156) by 156.147.51.102 with ESMTP; 12 Mar 2026 20:25:14 +0900 X-Original-SENDERIP: 10.177.112.156 X-Original-MAILFROM: youngjun.park@lge.com From: Youngjun Park To: rafael@kernel.org, akpm@linux-foundation.org Cc: chrisl@kernel.org, kasong@tencent.com, pavel@kernel.org, shikemeng@huaweicloud.com, nphamcs@gmail.com, bhe@redhat.com, baohua@kernel.org, youngjun.park@lge.com, usama.arif@linux.dev, linux-pm@vger.kernel.org, linux-mm@kvack.org Subject: [RESEND RFC PATCH v3 2/2] mm/swap: remove redundant swap device reference in alloc/free Date: Thu, 12 Mar 2026 20:25:11 +0900 Message-Id: <20260312112511.3596781-3-youngjun.park@lge.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20260312112511.3596781-1-youngjun.park@lge.com> References: <20260312112511.3596781-1-youngjun.park@lge.com> Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit In the previous commit, uswsusp was modified to acquire the swap device reference at the time of determining the swap type. As a result, it is no longer necessary to repeatedly acquire and release the reference to protect against swapoff every time a swap slot is allocated. For hibernation via the sysfs interface, user-space processes are already frozen, making swapoff inherently impossible. Thus, acquiring and releasing the reference during allocation is unnecessary. Furthermore, even after returning from suspend, processes are not yet thawed when swap slots are freed, meaning reference management is not required at that stage either. Therefore, remove the redundant swap device reference acquire and release operations from the hibernation swap allocation and free functions. Additionally, remove the SWP_WRITEOK check before allocation. This check is redundant because the cluster allocation logic already handles it. Signed-off-by: Youngjun Park --- mm/swapfile.c | 50 ++++++++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 5a3d5c1e1f81..8467c3511870 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -2091,31 +2091,26 @@ swp_entry_t swap_alloc_hibernation_slot(int type) if (!si) goto fail; - /* This is called for allocating swap entry, not cache */ - if (get_swap_device_info(si)) { - if (si->flags & SWP_WRITEOK) { - /* - * Try the local cluster first if it matches the device. If - * not, try grab a new cluster and override local cluster. - */ - local_lock(&percpu_swap_cluster.lock); - pcp_si = this_cpu_read(percpu_swap_cluster.si[0]); - pcp_offset = this_cpu_read(percpu_swap_cluster.offset[0]); - if (pcp_si == si && pcp_offset) { - ci = swap_cluster_lock(si, pcp_offset); - if (cluster_is_usable(ci, 0)) - offset = alloc_swap_scan_cluster(si, ci, NULL, pcp_offset); - else - swap_cluster_unlock(ci); - } - if (!offset) - offset = cluster_alloc_swap_entry(si, NULL); - local_unlock(&percpu_swap_cluster.lock); - if (offset) - entry = swp_entry(si->type, offset); - } - put_swap_device(si); + /* + * Try the local cluster first if it matches the device. If + * not, try grab a new cluster and override local cluster. + */ + local_lock(&percpu_swap_cluster.lock); + pcp_si = this_cpu_read(percpu_swap_cluster.si[0]); + pcp_offset = this_cpu_read(percpu_swap_cluster.offset[0]); + if (pcp_si == si && pcp_offset) { + ci = swap_cluster_lock(si, pcp_offset); + if (cluster_is_usable(ci, 0)) + offset = alloc_swap_scan_cluster(si, ci, NULL, pcp_offset); + else + swap_cluster_unlock(ci); } + if (!offset) + offset = cluster_alloc_swap_entry(si, NULL); + local_unlock(&percpu_swap_cluster.lock); + if (offset) + entry = swp_entry(si->type, offset); + fail: return entry; } @@ -2123,14 +2118,10 @@ swp_entry_t swap_alloc_hibernation_slot(int type) /* Free a slot allocated by swap_alloc_hibernation_slot */ void swap_free_hibernation_slot(swp_entry_t entry) { - struct swap_info_struct *si; + struct swap_info_struct *si = __swap_entry_to_info(entry); struct swap_cluster_info *ci; pgoff_t offset = swp_offset(entry); - si = get_swap_device(entry); - if (WARN_ON(!si)) - return; - ci = swap_cluster_lock(si, offset); __swap_cluster_put_entry(ci, offset % SWAPFILE_CLUSTER); __swap_cluster_free_entries(si, ci, offset % SWAPFILE_CLUSTER, 1); @@ -2138,7 +2129,6 @@ void swap_free_hibernation_slot(swp_entry_t entry) /* In theory readahead might add it to the swap cache by accident */ __try_to_reclaim_swap(si, offset, TTRS_ANYWAY); - put_swap_device(si); } /* -- 2.34.1