From nobody Wed Feb 11 08:12:15 2026 Received: from mail-oo1-f44.google.com (mail-oo1-f44.google.com [209.85.161.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 36AB835EDC1 for ; Sun, 8 Feb 2026 21:58:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770587934; cv=none; b=FvCAbLOtJGTF2ztE/9G+kpeJdOk5tkbfhAjx/fXbuxhFJGwdfgrrjRYy3l6MAAHWWcMEacYKeIM5KYmreTZhkntyx3aSuBuYztezRCH1xznpSTZKVXXYoTjSIJeYE/LQWan+TybSwFTPhr5BhshvVdTFLNzF2yBlkUhia3RtMRs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770587934; c=relaxed/simple; bh=0Qlnn/0vz2JKpfqCrEGnXX7iDLpsICWEjt16tSDKWL0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mn3s3xZSRJTkYKh/n0k8MnVe859VR5JtFuSeVm2BIAKqVHBr5uZXLhTKw5O5HYqPWfE4jw8E23OHNG7We4zetlHVP+226i5xyNBgLYdmZGGGbsJkkRsf7aArY/Ra+ug/+bwu0iUR3UG6F36fEkgewSGzKldm8zzH8COAd0knfzo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=auk1PhKB; arc=none smtp.client-ip=209.85.161.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="auk1PhKB" Received: by mail-oo1-f44.google.com with SMTP id 006d021491bc7-6611c3b147eso1664680eaf.2 for ; Sun, 08 Feb 2026 13:58:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770587933; x=1771192733; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YCzAnUs0VtYcvzowlURB7PfVAQtyZ2LBB9m2XBAedLE=; b=auk1PhKBsxYRqxltOAaKXLjVDOEyDZzUzj+JtX9LUrCIFQF6bPF3dQtuyKXu9m81lc HU+U6FZm+FVJ7ReOUrZAre86MqCAZb/GWCrca75duv26SnzT71VUJnKRguKo1lW3OXQt qxO8f76iq2LmBt1xinTij06Pzn15DXRbB5RXImlM+jtUNmH7VfG62r2fwqeqCr2ez6Au UW99SgG2RwT3UbOka+bzbjE+lVI8DwwGce1GtXmergMXXhvOyG+bygae8VqilBkx+lTZ koeL0xYPYb5/+oH3vf1joCTAIPEM2KhGvxfcMDDlsxPNmZUlpsrS9IpF/nBNswO0DEWb eAuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770587933; x=1771192733; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=YCzAnUs0VtYcvzowlURB7PfVAQtyZ2LBB9m2XBAedLE=; b=KvXAGjSjA6vKVcauMEKziAIOi/5B/MTlm7VD6tOqVp9Nx87HZi8GYFrPmxME6yz/PL ezv8t5Hy1tZnp5B/Ng8zAGUFRkWa9zg5y4RINrpa6PvJEaFyEsWOjZj46UF2CBOOSAWL wN+IQsQyX2y3f5zpGyfX5XlM/qIjrdW5UF7vo+h7vy/zbq2LuNF9bWWwVdkcKElnXORu 8YA3etMIikXDo35A5ipfe0Rs4ge/885VJjNjGOORO4FEpnAgysmOhNGnPYF5iWtAXVjZ QLoXJ4AXtn8hUUXnlD7ahlDmzVSLRJz/mTlq/BiSP5ZhEQPdfG+jUBBKpGrz7bHOMu/k ULew== X-Forwarded-Encrypted: i=1; AJvYcCWqmtk3cZD46qMjJTQzF0LT7WP6NWApco3euNXidMHflkerjWJa6E7K/4XZmwxtPMqKuBfc2DQ6aelSeiY=@vger.kernel.org X-Gm-Message-State: AOJu0YzhlqY2kUfU08px+VycVoVJqAPHCA9F9Stvk1W+SRZ0B5ta6n5N egUgFEjpj2TN8oOsyzzQu0stp+RC2ZXbPoVS0WnAzMEwOpn32GNntvJY X-Gm-Gg: AZuq6aKVx90V9BzvMn7K5hHD9giAqa4+0/IKObje237JrlLLXjxjD5gH0d3KUAfUkS3 cynzZ91jkXp6jBTwCE+zp8TZskmkJgVGV5mWCbm4MiX7tbSfs41zbdp4FQSGMvoTnvZ2aOBlsia /TLaMsqYqYOq99f6FJkzGiWFjU4AlxwK8rViY3WWNg7ELm2aSqIHbE79TZ4XnfVCWaXbNogrUsq Wo9NPlbqUQGf8wuBMwxaAub4WR6cxrV1sc3heHvVaBfgXluV1F2GO++UwH6q2esGIri2AGegp1E QPXtqZk7LW9a2nRtcpcu1w4SZZtPulGJuDprMwQU/zf0OS39XmApiW0eNFXGex830VMeObyiKM8 cvGyPiSey6Whsg+VlefKu8ZppkS9KUD7YWcW/GKsuYEaof9oSfnOA29ixzlc/Jot02nLX2y2n4J wKK5pdFi9VE65xHPQLlv9vQgCnwSZ3GZeN5g== X-Received: by 2002:a05:6820:1514:b0:662:c369:8af1 with SMTP id 006d021491bc7-66d0d5e38b6mr4727396eaf.78.1770587933119; Sun, 08 Feb 2026 13:58:53 -0800 (PST) Received: from localhost ([2a03:2880:10ff:73::]) by smtp.gmail.com with ESMTPSA id 006d021491bc7-66d3adcae3csm5051168eaf.9.2026.02.08.13.58.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Feb 2026 13:58:52 -0800 (PST) From: Nhat Pham To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, hannes@cmpxchg.org, hughd@google.com, yosry.ahmed@linux.dev, mhocko@kernel.org, roman.gushchin@linux.dev, shakeel.butt@linux.dev, muchun.song@linux.dev, len.brown@intel.com, chengming.zhou@linux.dev, kasong@tencent.com, chrisl@kernel.org, huang.ying.caritas@gmail.com, ryan.roberts@arm.com, shikemeng@huaweicloud.com, viro@zeniv.linux.org.uk, baohua@kernel.org, bhe@redhat.com, osalvador@suse.de, lorenzo.stoakes@oracle.com, christophe.leroy@csgroup.eu, pavel@kernel.org, kernel-team@meta.com, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-pm@vger.kernel.org, peterx@redhat.com, riel@surriel.com, joshua.hahnjy@gmail.com, npache@redhat.com, gourry@gourry.net, axelrasmussen@google.com, yuanchu@google.com, weixugc@google.com, rafael@kernel.org, jannh@google.com, pfalcato@suse.de, zhengqi.arch@bytedance.com Subject: [PATCH v3 05/20] mm/swap: add a new function to check if a swap entry is in swap cached. Date: Sun, 8 Feb 2026 13:58:18 -0800 Message-ID: <20260208215839.87595-6-nphamcs@gmail.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260208215839.87595-1-nphamcs@gmail.com> References: <20260208215839.87595-1-nphamcs@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Userfaultfd checks whether a swap entry is in swapcache. This is currently done by directly looking at the swapfile's swap map - however, the swap cached state will soon be managed at the virtual swap layer. Abstract away this function. Signed-off-by: Nhat Pham --- include/linux/swap.h | 6 ++++++ mm/swapfile.c | 15 +++++++++++++++ mm/userfaultfd.c | 3 +-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 3da637b218baf..f91a442ac0e82 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -435,6 +435,7 @@ void free_swap_and_cache_nr(swp_entry_t entry, int nr); int __swap_count(swp_entry_t entry); bool swap_entry_swapped(struct swap_info_struct *si, swp_entry_t entry); int swp_swapcount(swp_entry_t entry); +bool is_swap_cached(swp_entry_t entry); =20 /* Swap cache API (mm/swap_state.c) */ static inline unsigned long total_swapcache_pages(void) @@ -554,6 +555,11 @@ static inline int swp_swapcount(swp_entry_t entry) return 0; } =20 +static inline bool is_swap_cached(swp_entry_t entry) +{ + return false; +} + static inline int folio_alloc_swap(struct folio *folio) { return -EINVAL; diff --git a/mm/swapfile.c b/mm/swapfile.c index cacfafa9a540d..3c89dedbd5718 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -194,6 +194,21 @@ static bool swap_only_has_cache(struct swap_info_struc= t *si, return true; } =20 +/** + * is_swap_cached - check if the swap entry is cached + * @entry: swap entry to check + * + * Check swap_map directly to minimize overhead, READ_ONCE is sufficient. + * + * Returns true if the swap entry is cached, false otherwise. + */ +bool is_swap_cached(swp_entry_t entry) +{ + struct swap_info_struct *si =3D __swap_entry_to_info(entry); + + return READ_ONCE(si->swap_map[swp_offset(entry)]) & SWAP_HAS_CACHE; +} + static bool swap_is_last_map(struct swap_info_struct *si, unsigned long offset, int nr_pages, bool *has_cache) { diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 25f89eba0438c..98be764fb3ecd 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -1190,7 +1190,6 @@ static int move_swap_pte(struct mm_struct *mm, struct= vm_area_struct *dst_vma, * Check if the swap entry is cached after acquiring the src_pte * lock. Otherwise, we might miss a newly loaded swap cache folio. * - * Check swap_map directly to minimize overhead, READ_ONCE is sufficient. * We are trying to catch newly added swap cache, the only possible case= is * when a folio is swapped in and out again staying in swap cache, using= the * same entry before the PTE check above. The PTL is acquired and releas= ed @@ -1200,7 +1199,7 @@ static int move_swap_pte(struct mm_struct *mm, struct= vm_area_struct *dst_vma, * cache, or during the tiny synchronization window between swap cache a= nd * swap_map, but it will be gone very quickly, worst result is retry jit= ters. */ - if (READ_ONCE(si->swap_map[swp_offset(entry)]) & SWAP_HAS_CACHE) { + if (is_swap_cached(entry)) { double_pt_unlock(dst_ptl, src_ptl); return -EAGAIN; } --=20 2.47.3