From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lgeamrelo07.lge.com (lgeamrelo07.lge.com [156.147.51.103]) (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 A50D62FBDF2 for ; Tue, 24 Feb 2026 07:48:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=156.147.51.103 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771919327; cv=none; b=KXW/xiqLVFeaYN+jZ8dvtTaSsBljSonC0whFJJMN5IMVTyGBTOR9e8VP9PM8nMGdk6hHxKr9+/4nk821OuZp8n5DmMgPgbKkqn4dj8trAo9eNPUF6azQBohBsoDaJEBPWOdUuMlZZsNSOw/6T7Q8ckLzzuBWzA8JeEKSZmOQqMA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771919327; c=relaxed/simple; bh=negKzoHZsxPU4o58MLlTjaixMIH5ZB/SrLcnXPhBDSI=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ljj5IMt9uFFWU8WhvDLP13c1vpMCJBHy0DMoCqe8KjfDbKssJ0wDrxCb1nPjg6uHRoc3rIBZbNbGJVpsjaH1cCCM2vHcX+3ElGGzOzB9Qfo14vgWHm/A9ay683enJAT1IzV/6Rwqw+Nmo54PVjmiowBn+RsxoMjaiiqQyyZ085c= 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.103 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) (10.177.112.156) by 156.147.51.103 with ESMTP; 24 Feb 2026 16:48:36 +0900 X-Original-SENDERIP: 10.177.112.156 X-Original-MAILFROM: youngjun.park@lge.com Date: Tue, 24 Feb 2026 16:48:36 +0900 From: YoungJun Park To: kasong@tencent.com Cc: linux-mm@kvack.org, Andrew Morton , Chris Li , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Carsten Grohmann , "Rafael J. Wysocki" , linux-kernel@vger.kernel.org, "open list:SUSPEND TO RAM" , taejoon.song@lge.com, "hyungjun.cho@lge.com Carsten Grohmann" , stable@vger.kernel.org Subject: Re: [PATCH v4 1/3] mm, swap: speed up hibernation allocation and writeout Message-ID: References: <20260216-hibernate-perf-v4-0-1ba9f0bf1ec9@tencent.com> <20260216-hibernate-perf-v4-1-1ba9f0bf1ec9@tencent.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20260216-hibernate-perf-v4-1-1ba9f0bf1ec9@tencent.com> On Mon, Feb 16, 2026 at 10:58:02PM +0800, Kairui Song via B4 Relay wrote: > From: Kairui Song <kasong@tencent.com> > > Since commit 0ff67f990bd4 ("mm, swap: remove swap slot cache"), > hibernation has been using the swap slot slow allocation path for > simplification, which turns out might cause regression for some > devices because the allocator now rotates clusters too often, leading to > slower allocation and more random distribution of data. ... > diff --git a/mm/swapfile.c b/mm/swapfile.c > index c6863ff7152c..32e0e7545ab8 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c > @@ -1926,8 +1926,9 @@ void swap_put_entries_direct(swp_entry_t entry, int nr) > /* Allocate a slot for hibernation */ > swp_entry_t swap_alloc_hibernation_slot(int type) > { > - struct swap_info_struct *si = swap_type_to_info(type); > - unsigned long offset; > + struct swap_info_struct *pcp_si, *si = swap_type_to_info(type); > + unsigned long pcp_offset, offset = SWAP_ENTRY_INVALID; > + struct swap_cluster_info *ci; > swp_entry_t entry = {0}; > > if (!si) > @@ -1937,11 +1938,21 @@ swp_entry_t swap_alloc_hibernation_slot(int type) > if (get_swap_device_info(si)) { Hi Kairui :) Reading through the patch, I have some thoughts and review comments regarding the hibernation slot allocation logic. I'd like to discuss potential improvements. (Somewhat long... lot of thoughts come up on my mind) First, regarding the race with swapoff and refcounting. The code identifies the swap type before allocation, so a swapoff could occur in between. It seems safer to acquire the reference when identifying the type (e.g., find_first_swap). Also, instead of repeating get/put for every slot (allocation and free), could we hold the reference once during the initial lookup and release it after the image load? This avoids overhead since swapoff is effectively blocked once hibernation slots are allocated. > if (si->flags & SWP_WRITEOK) { > /* > - * Grab the local lock to be compliant > - * with swap table allocation. > + * 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); Second, regarding local_lock: It seems mandatory now because distinguishing the lock context during swap table allocation is tricky (e.g., GFP_KERNEL allocation assumes a local locked context). Have you considered modifying the swap table allocation logic to handle this specifically? This might allow us to avoid holding the local_lock, especially if the device is not SWP_SOLIDSTATE. > - offset = cluster_alloc_swap_entry(si, NULL); > + 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); Third, regarding cluster allocation: 1. If hibernation targets a lower-priority device, the per-cpu cluster usage might cause priority inversion (though minimal). 2. Have you considered treating clusters as a global resource for this case? For instance, caching next_offset in si(using union on global_cluster or new field) or allowing the allocator to calculate the next value directly, rather than splitting clusters per CPU. Finally, regarding readahead and freeing: Hibernation slots might be read during cluster-based readahead. Can we avoid this (e.g., by checking for a NULL fake shadow entry or adding a specific check for hibernation slots)? If so, we could also avoid triggering try_to_reclaim when freeing these slots. Thanks for your work! Youngjun Park