From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A1C5CC021AA for ; Wed, 19 Feb 2025 02:12:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 09A346B009D; Tue, 18 Feb 2025 21:12:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0494A2801CA; Tue, 18 Feb 2025 21:12:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E7AB32801CC; Tue, 18 Feb 2025 21:12:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id C8DB92801CA for ; Tue, 18 Feb 2025 21:12:16 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 318C4140996 for ; Wed, 19 Feb 2025 02:12:16 +0000 (UTC) X-FDA: 83135069472.11.127D5A0 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf20.hostedemail.com (Postfix) with ESMTP id 507721C0005 for ; Wed, 19 Feb 2025 02:12:14 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="R/od2t+e"; spf=pass (imf20.hostedemail.com: domain of bhe@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=bhe@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1739931134; a=rsa-sha256; cv=none; b=5+lITnjf4m8HTl3D2R1M2aUdEBtjk3Uj/tQyRx/zhVKzeql9fWoofhQvOLkXhQAptlT8aO AW9/gx2/AODkFU18TDtatkBixuZoTaTEMDIkI77LSbT8gTWguCC8AGkwN/cTRsEKAHvhyf trvQF3vi3lJ3CQ4+S1OjD/BJT8S1910= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="R/od2t+e"; spf=pass (imf20.hostedemail.com: domain of bhe@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=bhe@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1739931134; h=from:from:sender: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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Y0P5Gj4WhipWloYam1SLmuS1RQMkBOTimmYouu8iJ04=; b=CYQGKOf5bE2wClUdKKTRLQdjVZ8x/RBm0++Htit4iu6CO9LgDyTVjeiwPl+gZRZIYHmn8R Bn90h0sg3DMoNEm3KthgjKHQB3zdXcpbq6Xc8dXIAO44O94XCJ3eansna3yL98sttV2cft RIfX9fk4XzWARP4i9gaZriMBGhyQkus= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1739931133; 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: in-reply-to:in-reply-to:references:references; bh=Y0P5Gj4WhipWloYam1SLmuS1RQMkBOTimmYouu8iJ04=; b=R/od2t+e0Isa0dArWO7VeKi3KFdFL+uL2ukZ/bv58pCtaR5EoN05Rdzgleazw0CVnLizRe SXWp4y/7egFLoJ/a6BlCmkWKNcRJq2boeNiSydC7lmaBfOfI6HzmSDGRXGQ4pAuJidquj8 sDfKs6Zg3rIn6StdzJGis4jvK6dyL60= Received: from mx-prod-mc-04.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-683-p777GerLNJSMbRYcfAJlvg-1; Tue, 18 Feb 2025 21:12:08 -0500 X-MC-Unique: p777GerLNJSMbRYcfAJlvg-1 X-Mimecast-MFC-AGG-ID: p777GerLNJSMbRYcfAJlvg_1739931127 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 00C02193578F; Wed, 19 Feb 2025 02:12:06 +0000 (UTC) Received: from localhost (unknown [10.72.112.127]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3A9271955BCB; Wed, 19 Feb 2025 02:12:03 +0000 (UTC) Date: Wed, 19 Feb 2025 10:11:59 +0800 From: Baoquan He To: Kairui Song Cc: linux-mm@kvack.org, Andrew Morton , Chris Li , Barry Song , Hugh Dickins , Yosry Ahmed , "Huang, Ying" , Nhat Pham , Johannes Weiner , Kalesh Singh , linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/7] mm, swap: avoid reclaiming irrelevant swap cache Message-ID: References: <20250214175709.76029-1-ryncsn@gmail.com> <20250214175709.76029-2-ryncsn@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250214175709.76029-2-ryncsn@gmail.com> X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Rspamd-Queue-Id: 507721C0005 X-Stat-Signature: wd46ybe1zzzqfuiwb9emrde4regktgx7 X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1739931134-670110 X-HE-Me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R4wgapCG7nI9W4Jo00K4rb85BiiYfGhv4/Uvmj5G7ebqy6+1s2np9CK8/URSzjrgp9W5lVfZc1wQ41mjlxuT0+RNDGuK1wW6LJ5Yy16tKU6LTvUA6US/SQ7xIICApXGQdPeJOktXU6c34/g7htvvtRRnJN+8iXvx0Q77ID1oL9MpQGPEi9TwHhtI8KN7hjSiKSqeON6PVMdbGxY5lqNL65LWjmaUWrQaWI7Oaj0Nmef4uIPFQ9FJsQEzg6MGIrPX7Ua5PCFMRcDuToNhvEGvbVcg8mfvk124Yw3FfS49m1YZukHHv1X54OWBiGO1hzYyINo6iw3LN/V8VUyWIjnnsjpDrMMkpjDsl4pyuJFXEWbunxnQ+qoa X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 02/15/25 at 01:57am, Kairui Song wrote: > From: Kairui Song > > Swap allocator will do swap cache reclaim to recycle HAS_CACHE slots for > allocation. It initiates the reclaim from the offset to be reclaimed and > looks up the corresponding folio. The lookup process is lockless, so it's > possible the folio will be removed from the swap cache and given > a different swap entry before the reclaim locks the folio. If > it happens, the reclaim will end up reclaiming an irrelevant folio, and > return wrong return value. > > This shouldn't cause any problem with correctness or stability, but > it is indeed confusing and unexpected, and will increase fragmentation, > decrease performance. > > Fix this by checking whether the folio is still pointing to the offset > the allocator want to reclaim before reclaiming it. > > Signed-off-by: Kairui Song > --- > mm/swapfile.c | 11 ++++++++++- > 1 file changed, 10 insertions(+), 1 deletion(-) > > diff --git a/mm/swapfile.c b/mm/swapfile.c > index 34baefb000b5..c77ffee4af86 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c > @@ -210,6 +210,7 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si, > int ret, nr_pages; > bool need_reclaim; > > +again: > folio = filemap_get_folio(address_space, swap_cache_index(entry)); > if (IS_ERR(folio)) > return 0; > @@ -227,8 +228,16 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si, > if (!folio_trylock(folio)) > goto out; > > - /* offset could point to the middle of a large folio */ > + /* > + * Offset could point to the middle of a large folio, or folio > + * may no longer point to the expected offset before it's locked. > + */ > entry = folio->swap; > + if (offset < swp_offset(entry) || offset >= swp_offset(entry) + nr_pages) { > + folio_unlock(folio); > + folio_put(folio); > + goto again; > + } > offset = swp_offset(entry); LGTM, Reviewed-by: Baoquan He While reading the code in __try_to_reclaim_swap(), I am always worried that entry indexed by offset could be accessed by other users so tht it doesn't only has cache, because we released the ci->lock and don't hold any lock during period. It could be me who think too much.