From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 6C07821CC59 for ; Thu, 18 Dec 2025 03:34:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766028852; cv=none; b=fo4bh8vhGG4kjTXkY0Sdnmb8ONGoJum5yxveWl1jv5kG+VK7TKiNcpLxQZU9EXEIDIbhylOnt/AwWBvqEg+DESan0/jUQ/LbwcjvFmcutCYMRCKQYCIaV819rhvaYL2G9Ps396+J44XnDQL3u1KYVIgaFvFTUenHxoNJ2RfipG4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766028852; c=relaxed/simple; bh=qAYf5X/MhMMpaCK2vG3YWoyFZ0otnR4x337+4bymKlU=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=rI9pYvvmOHlbqTWvp+y27urSkvjz2xpF50xZRa4A+InVGyzx7dOiG1dOney6zEUhg1PLC+HUBEn+qM2B/EV7wagOrVs2SSVfimuDvN8pT+mXaRsE1kbEGuf6rfkSuHUPjBRkv6TIGzttW0xqUddN9vdZtJ/SHq2CO+6e53EXL9Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=VAGweT4v; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="VAGweT4v" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1766028849; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=p7NCoY3gkUnMPv3EKCdtZ9hIEZ8MNGARhKh/iShdPtE=; b=VAGweT4vJe8zcBcxAQ/wL2Q+Q4D4obCQR6OCCIUzOMdbN+RbamTDxfLFmUNn90pS3vEaIl bil8iOomtVI23h3cL0HuU1VfX1mrAGlileO4r9VY/wVbtEvqEHT26us1Rojs9IjHi3dxc+ /pIsJCgpQ9VIoTZqTsICaTtLifPVwpQ= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-126-IJ3Mz50dO82-Jay4YSq0kA-1; Wed, 17 Dec 2025 22:34:04 -0500 X-MC-Unique: IJ3Mz50dO82-Jay4YSq0kA-1 X-Mimecast-MFC-AGG-ID: IJ3Mz50dO82-Jay4YSq0kA_1766028842 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id CFE981800669; Thu, 18 Dec 2025 03:34:01 +0000 (UTC) Received: from localhost (unknown [10.72.112.95]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E412E1956056; Thu, 18 Dec 2025 03:33:59 +0000 (UTC) Date: Thu, 18 Dec 2025 11:33:55 +0800 From: Baoquan He To: Kairui Song Cc: linux-mm@kvack.org, Andrew Morton , Barry Song , Chris Li , Nhat Pham , Yosry Ahmed , David Hildenbrand , Johannes Weiner , Youngjun Park , Hugh Dickins , Baolin Wang , Ying Huang , Kemeng Shi , Lorenzo Stoakes , "Matthew Wilcox (Oracle)" , linux-kernel@vger.kernel.org Subject: Re: [PATCH v4 10/19] mm, swap: consolidate cluster reclaim and usability check Message-ID: References: <20251205-swap-table-p2-v4-0-cb7e28a26a40@tencent.com> <20251205-swap-table-p2-v4-10-cb7e28a26a40@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=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 On 12/18/25 at 02:30am, Kairui Song wrote: > On Wed, Dec 17, 2025 at 7:16 PM Baoquan He wrote: > > > > On 12/15/25 at 12:38pm, Kairui Song wrote: > > > On Mon, Dec 15, 2025 at 12:13 PM Baoquan He wrote: > > > > > > > > On 12/05/25 at 03:29am, Kairui Song wrote: > > > > > From: Kairui Song > > > > > > > > > > Swap cluster cache reclaim requires releasing the lock, so the cluster > > > > > may become unusable after the reclaim. To prepare for checking swap > > > > > cache using the swap table directly, consolidate the swap cluster > > > > > reclaim and the check logic. > > > > > > > > > > We will want to avoid touching the cluster's data completely with the > > > > ~~~~~~~~ > > > > 'want to' means 'will'? > > > > > > Sorry about my english, I mean in the following commit, we need to > > > avoid accessing the cluster's table (ci->table) when the cluster is > > > empty, so the reclaim helper need to check cluster status before > > > accessing it. > > > > Got it, I could be wrong. Please ignore this nit pick unless any english > > native speaker raise concern on this. > > > > > > > > > > > > > > swap table, to avoid RCU overhead here. And by moving the cluster usable > > > > > check into the reclaim helper, it will also help avoid a redundant scan of > > > > > the slots if the cluster is no longer usable, and we will want to avoid > > > > ~~~~~~~~~~~~ > > > > this place too. > > > > > touching the cluster. > > > > > > > > > > Also, adjust it very slightly while at it: always scan the whole region > > > > > during reclaim, don't skip slots covered by a reclaimed folio. Because > > > > > the reclaim is lockless, it's possible that new cache lands at any time. > > > > > And for allocation, we want all caches to be reclaimed to avoid > > > > > fragmentation. Besides, if the scan offset is not aligned with the size > > > > > of the reclaimed folio, we might skip some existing cache and fail the > > > > > reclaim unexpectedly. > > > > > > > > > > There should be no observable behavior change. It might slightly improve > > > > > the fragmentation issue or performance. > > > > > > > > > > Signed-off-by: Kairui Song > > > > > --- > > > > > mm/swapfile.c | 45 +++++++++++++++++++++++++++++---------------- > > > > > 1 file changed, 29 insertions(+), 16 deletions(-) > > > > > > > > > > diff --git a/mm/swapfile.c b/mm/swapfile.c > > > > > index 5a766d4fcaa5..2703dfafc632 100644 > > > > > --- a/mm/swapfile.c > > > > > +++ b/mm/swapfile.c > > > > > @@ -777,33 +777,51 @@ static int swap_cluster_setup_bad_slot(struct swap_cluster_info *cluster_info, > > > > > return 0; > > > > > } > > > > > > > > > > +/* > > > > > + * Reclaim drops the ci lock, so the cluster may become unusable (freed or > > > > > + * stolen by a lower order). @usable will be set to false if that happens. > > > > > + */ > > > > > static bool cluster_reclaim_range(struct swap_info_struct *si, > > > > > struct swap_cluster_info *ci, > > > > > - unsigned long start, unsigned long end) > > > > > + unsigned long start, unsigned int order, > > > > > + bool *usable) > > > > > { > > > > > + unsigned int nr_pages = 1 << order; > > > > > + unsigned long offset = start, end = start + nr_pages; > > > > > unsigned char *map = si->swap_map; > > > > > - unsigned long offset = start; > > > > > int nr_reclaim; > > > > > > > > > > spin_unlock(&ci->lock); > > > > > do { > > > > > switch (READ_ONCE(map[offset])) { > > > > > case 0: > > > > > - offset++; > > > > > break; > > > > > case SWAP_HAS_CACHE: > > > > > nr_reclaim = __try_to_reclaim_swap(si, offset, TTRS_ANYWAY); > > > > > - if (nr_reclaim > 0) > > > > > - offset += nr_reclaim; > > > > > - else > > > > > + if (nr_reclaim < 0) > > > > > goto out; > > > > > break; > > > > > default: > > > > > goto out; > > > > > } > > > > > - } while (offset < end); > > > > > + } while (++offset < end); > > > > ~~~~~ '++offset' is conflicting with nr_reclaim > > > > returned from __try_to_reclaim_swap(). can you explain? > > > > > > What do you mean conflicting? If (nr_reclaim < 0), reclaim failed, > > > this loop ends. If (nr_reclaim == 0), the slot is likely concurrently > > > freed so the loop should just continue to iterate & reclaim to ensure > > > all slots are freed. If nr_reclaim > 0, the reclaim just freed a folio > > > of nr_reclaim pages. We can round up by nr_reclaim to skip the slots > > > that were occupied by the folio, but note here we are not locking the > > > ci so there could be new folios landing in that range. Just keep > > > iterating the reclaim seems still a good option and that makes the > > > code simpler, and in practice maybe faster as there are less branches > > > and calculations involved. > > > > I see now. The 'conflicting' may be not precise. I didn't understand > > this because __try_to_reclaim_swap() is called in several places, and > > all of them have the same situation about lock releasing and retaking > > on ci->lock around __try_to_reclaim_swap(). As you said, we may need > > refactor __try_to_reclaim_swap() and make change in all those places. > > It's a bit different, other callers of __try_to_reclaim_swap are just > best effort try to reclaim a slot's swap cache, because ultimately the > allocator will reclaim the slot if needed anyway. But here, it is the > allocator doing the reclaim, so we want precisely every slot to be > cleaned. OK, I see. Thanks for the explanation. While I think that's why we did the recheck in later for loop. The old way and your change may have the similar effect. > > Avoid the align /round_up also make the code a bit cleaner. >