From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ot1-f44.google.com (mail-ot1-f44.google.com [209.85.210.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 800B748AE39 for ; Tue, 5 May 2026 15:39:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777995551; cv=none; b=W3eHA3EtobLZIEaKXzIp/XZmg25AjTKIT0zXizI2qwLztbriEuj/OXu5iuDlXnGlcK1rotqoGjWBI+v0+OoCWktSXXf56/c4UjXi/MwtYPzPX4JFdTVmsBDrpNSPnS9ZjQrN1YaIxiicV4B0WdQHom0W8YFK5G+ShTZsfe/ZDpw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777995551; c=relaxed/simple; bh=X9dE5SnxTHQWLGLGF1Tm4Hd+Xo2utpkChYtUmYAYABQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rzSznQh3scBNeEVJ3x5c//dBWnF4ls2i/TwOQiRIyZuYuSD2zF1XgrHgIcayLhInfQkS21LeW9B/UCJ29fbwSv/dWGZTAsS2SAOyXKYqTs8Oz6BkL3D5EOnhbN9j2rP9tEr0XFYD/jhlM09djRMWqDZJjfiwKR6pfs+Y2bU31wM= 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=IYEashty; arc=none smtp.client-ip=209.85.210.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="IYEashty" Received: by mail-ot1-f44.google.com with SMTP id 46e09a7af769-7de7dc85b74so4875969a34.2 for ; Tue, 05 May 2026 08:39:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1777995548; x=1778600348; 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=uq6RStVYG/AZxKiwJHi2taHWJECHTirH9n/mFE+40q0=; b=IYEashtyxtd23fIvcGom3d/XGBqE+vfKKGeTvI07iIv1ui7fclp5VRCvpvq1z22fwJ 6AnuDzEYy2K0umZd0jeojqKmGxtpXMtOj4I1oTStSoNm1uaJZqk4f4ZoEQPMjsizEHVm o8Wf7IZ6zxmQHzQvPS98IDjgD78kV09976nnKbBHDfECpA3gBb3AMxP+Dd+mNlkymQx5 b7v7CdJYY5gE/T9sxVB33Nw5AQ5zgs2PP/VO6eu4S7Gr5OouRaKxn9oIbytD3Zunw1yS aPkA9AtDt4oYDTfV4PhCl56C4WK8/lYgYSTBtIwv7poZqGyNIk2AeILlk4pRIIEY2Yqx oP7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777995548; x=1778600348; 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=uq6RStVYG/AZxKiwJHi2taHWJECHTirH9n/mFE+40q0=; b=k0cce+VU8engTsZOq6dvDDhdvqeeYuzV1G6KkW72gUiW3+8zwtH4TwF2R3o5+WM5Zi Cke5GpU5oS5bTOFO3tGEqcObazQJa2e8G8uVmvptaAtWckq8O3OJU0HDrC3zJeTDS8B6 PzakIimz4kyavdOUSorm9Jx6Qxv0mDAocIG1W501eymLfk2Z2bHrTU+LdoYsr4EkKrwi D6IkK8OLYXTzM4v+MtaEWU1N6DfRI/RAczPWmE5dWMTR1BlocNOVuKQt4X0r7qEQ3Rsb gkElmJ4pL4YMkKtMdDVTZp7X+xj+WqnujpUbrBJ4sROHTV+JOuXlqb35BWxphRkk0sd3 uWNA== X-Forwarded-Encrypted: i=1; AFNElJ9a14eix8sL0uJoTKxk3WDWdo2um9Q3oz/kSYenwUGXwJQjbSxmgOg4l2OLnEkr6cXBGHjOeS0NCUZOsoI=@vger.kernel.org X-Gm-Message-State: AOJu0Yy7Rq+scvFOIl7jafgtYs8PsddaaynaF4Usfp/yr+4JK4bzna+o Lej8FSGrcfaGO8SbGIYDBkXjw3LDrj+w38pbD7vAmS+wZPllzFZ6eW6R X-Gm-Gg: AeBDieuSvFXOPnHlxfDap859WAqETh61PVN/Fo5fU87q+JgqBABhIbBqCK6CbBCrWMl hu9PmgozvlmsMUYBZhRSiqqX0Idy6YYkfkUbUtLIrMEoDgM9dBznnu9W3QmDIa8GdEK/Kd8pv0U Z3uV2qWkkD32JNc9CxL2DNfWUwznigO9DG/HPOjs/EzOure1/di66ooXro8SLZJJNnBM0E4tOX4 7DuW0N8x+ZwEAk2khpN1T0HNg5eGwjfxYyis80FnzwtGWXX24uf15206rF7+QPcJS9e1Gl9N101 1Iju2JeuqLupBEPfB4SEWz2GJDXNaUJu6+4tPC3uPRsUCnYpX3ECQVr72f3txskDUq56Hpg4grr jQrORlF1nwoTKdGr87KbsTVRYNT2tIDcbdGa0631/In+PjlNprj4vQOSM018lf5QX7+MHc6BMre PvNhmkrwICvs+NzGjT4203ZNu2W20+wep/y3LxdbzaJZoCX8r9OKFgiZZc X-Received: by 2002:a05:6830:2690:b0:7d9:b338:a695 with SMTP id 46e09a7af769-7dee11f9cacmr8510809a34.5.1777995548342; Tue, 05 May 2026 08:39:08 -0700 (PDT) Received: from localhost ([2a03:2880:10ff:45::]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-7deca7f46e7sm10930497a34.8.2026.05.05.08.39.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 May 2026 08:39:07 -0700 (PDT) From: Nhat Pham To: kasong@tencent.com Cc: Liam.Howlett@oracle.com, akpm@linux-foundation.org, apopple@nvidia.com, axelrasmussen@google.com, baohua@kernel.org, baolin.wang@linux.alibaba.com, bhe@redhat.com, byungchul@sk.com, cgroups@vger.kernel.org, chengming.zhou@linux.dev, chrisl@kernel.org, corbet@lwn.net, david@kernel.org, dev.jain@arm.com, gourry@gourry.net, hannes@cmpxchg.org, hughd@google.com, jannh@google.com, joshua.hahnjy@gmail.com, lance.yang@linux.dev, lenb@kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-pm@vger.kernel.org, lorenzo.stoakes@oracle.com, matthew.brost@intel.com, mhocko@suse.com, muchun.song@linux.dev, npache@redhat.com, nphamcs@gmail.com, pavel@kernel.org, peterx@redhat.com, peterz@infradead.org, pfalcato@suse.de, rafael@kernel.org, rakie.kim@sk.com, roman.gushchin@linux.dev, rppt@kernel.org, ryan.roberts@arm.com, shakeel.butt@linux.dev, shikemeng@huaweicloud.com, surenb@google.com, tglx@kernel.org, vbabka@suse.cz, weixugc@google.com, ying.huang@linux.alibaba.com, yosry.ahmed@linux.dev, yuanchu@google.com, zhengqi.arch@bytedance.com, ziy@nvidia.com, kernel-team@meta.com, riel@surriel.com, haowenchao22@gmail.com Subject: [PATCH v6 05/22] mm/swap: add a new function to check if a swap entry is in swap cached. Date: Tue, 5 May 2026 08:38:34 -0700 Message-ID: <20260505153854.1612033-6-nphamcs@gmail.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260505153854.1612033-1-nphamcs@gmail.com> References: <20260505153854.1612033-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: 8bit 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 3da637b218ba..f91a442ac0e8 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); /* 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; } +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 46da28c533bb..0471a965f222 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -194,6 +194,21 @@ static bool swap_only_has_cache(struct swap_info_struct *si, return true; } +/** + * 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 = __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 25f89eba0438..98be764fb3ec 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 released @@ -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 and * swap_map, but it will be gone very quickly, worst result is retry jitters. */ - 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; } -- 2.52.0