From nobody Sat Feb 7 17:42:14 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 93F4AC77B75 for ; Mon, 22 May 2023 07:11:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232356AbjEVHLH (ORCPT ); Mon, 22 May 2023 03:11:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232457AbjEVHJ7 (ORCPT ); Mon, 22 May 2023 03:09:59 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81675129 for ; Mon, 22 May 2023 00:09:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1684739375; x=1716275375; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=CAvLy4/ErwSMMyC1M2gR5vND9soNKvGVJOF+Oh8rPBI=; b=jQ7kzQqNSlMe8jzZScES1iN5M7sHDn2IentIcu0hBl/1rAre/gNE7EJz qyEN+e1EcjZ2s2CfIEFpUcpSw+hNHZ+g3IZEiYvtbgfypMHWI45qCcv3r X0oX5LXcc4ry3oo5KeQ2xoQAjkQAsISucPjYIJgVjOEwYNRnWfxJhlnm+ nitOjbh9tuk8fWrsxnN+2xHmHHE8Uwl2uE65TFkAVvTXhUMnqWoIsW4+R 9c9PCzAsD7iUsJwjDIYXre0tp4351VB4iffcMJqfpM5ZA4B3tG9luGEMA 0CKsxzS+Y/LWj5C+tjF2TneOlWlzCXEDAV7mXMLCnmmMiDq/YCMNMq4SP A==; X-IronPort-AV: E=McAfee;i="6600,9927,10717"; a="337436974" X-IronPort-AV: E=Sophos;i="6.00,183,1681196400"; d="scan'208";a="337436974" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 May 2023 00:09:25 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10717"; a="773212678" X-IronPort-AV: E=Sophos;i="6.00,183,1681196400"; d="scan'208";a="773212678" Received: from yhuang6-mobl2.sh.intel.com ([10.238.5.152]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 May 2023 00:09:22 -0700 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , David Hildenbrand , Hugh Dickins , Johannes Weiner , Matthew Wilcox , Michal Hocko , Minchan Kim , Tim Chen , Yang Shi , Yu Zhao Subject: [PATCH -V2 1/5] swap: Remove get/put_swap_device() in __swap_count() Date: Mon, 22 May 2023 15:09:01 +0800 Message-Id: <20230522070905.16773-2-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230522070905.16773-1-ying.huang@intel.com> References: <20230522070905.16773-1-ying.huang@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" __swap_count() is called in do_swap_page() only, which encloses the call site with get/put_swap_device() already. Signed-off-by: "Huang, Ying" Cc: David Hildenbrand Cc: Hugh Dickins Cc: Johannes Weiner Cc: Matthew Wilcox Cc: Michal Hocko Cc: Minchan Kim Cc: Tim Chen Cc: Yang Shi Cc: Yu Zhao Reviewed-by: David Hildenbrand Reviewed-by: Yosry Ahmed --- mm/swapfile.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 274bbf797480..8419cba9c192 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1432,16 +1432,10 @@ void swapcache_free_entries(swp_entry_t *entries, i= nt n) =20 int __swap_count(swp_entry_t entry) { - struct swap_info_struct *si; + struct swap_info_struct *si =3D swp_swap_info(entry); pgoff_t offset =3D swp_offset(entry); - int count =3D 0; =20 - si =3D get_swap_device(entry); - if (si) { - count =3D swap_count(si->swap_map[offset]); - put_swap_device(si); - } - return count; + return swap_count(si->swap_map[offset]); } =20 /* --=20 2.39.2 From nobody Sat Feb 7 17:42:14 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9F80FC77B75 for ; Mon, 22 May 2023 07:10:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232390AbjEVHKs (ORCPT ); Mon, 22 May 2023 03:10:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232461AbjEVHJ7 (ORCPT ); Mon, 22 May 2023 03:09:59 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8CAAB137 for ; Mon, 22 May 2023 00:09:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1684739376; x=1716275376; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=i1WT96efAZrjTV7W7kwHNRkRI/omQzn1KtV16Slt7b8=; b=Y1H0lgri+4aHfmT4yJe3hn3E1+4D02IKt9f+sS0xc0xDVusyfPNxjQs1 81IfaqhXQaIRVNuNhYqfIYSgtrat/Y57SN3ut+BO9dLwI0V6YNCiljikB p7bceeAbo26jBbK8r9WMk96dqdxqcYYRRubhdgspM9wNiBZ2tG2L659hY nvZLOwKGvhOIzcifTWccBMITw6AXOYx7+zYZjya2AI6CXwi9EqjCZFJEF uZblpGnvUKLYeZbVXWPMvXAuBJnXtKV7aMA+JFw907BKxLJMSS00Sc6R5 DxtjkqPsV3/uv0dwTtqTJVX/wNI6B6p2TvqrvxWXdiyuJqkI5W9JK1sA1 A==; X-IronPort-AV: E=McAfee;i="6600,9927,10717"; a="337436989" X-IronPort-AV: E=Sophos;i="6.00,183,1681196400"; d="scan'208";a="337436989" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 May 2023 00:09:28 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10717"; a="773212692" X-IronPort-AV: E=Sophos;i="6.00,183,1681196400"; d="scan'208";a="773212692" Received: from yhuang6-mobl2.sh.intel.com ([10.238.5.152]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 May 2023 00:09:25 -0700 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , David Hildenbrand , Hugh Dickins , Johannes Weiner , Matthew Wilcox , Michal Hocko , Minchan Kim , Tim Chen , Yang Shi , Yu Zhao Subject: [PATCH -V2 2/5] swap, __read_swap_cache_async(): enlarge get/put_swap_device protection range Date: Mon, 22 May 2023 15:09:02 +0800 Message-Id: <20230522070905.16773-3-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230522070905.16773-1-ying.huang@intel.com> References: <20230522070905.16773-1-ying.huang@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This makes the function a little easier to be understood because we don't need to consider swapoff. And this makes it possible to remove get/put_swap_device() calling in some functions called by __read_swap_cache_async(). Signed-off-by: "Huang, Ying" Cc: David Hildenbrand Cc: Hugh Dickins Cc: Johannes Weiner Cc: Matthew Wilcox Cc: Michal Hocko Cc: Minchan Kim Cc: Tim Chen Cc: Yang Shi Cc: Yu Zhao --- mm/swap_state.c | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/mm/swap_state.c b/mm/swap_state.c index b76a65ac28b3..a1028fe7214e 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -417,9 +417,13 @@ struct page *__read_swap_cache_async(swp_entry_t entry= , gfp_t gfp_mask, { struct swap_info_struct *si; struct folio *folio; + struct page *page; void *shadow =3D NULL; =20 *new_page_allocated =3D false; + si =3D get_swap_device(entry); + if (!si) + return NULL; =20 for (;;) { int err; @@ -428,14 +432,12 @@ struct page *__read_swap_cache_async(swp_entry_t entr= y, gfp_t gfp_mask, * called after swap_cache_get_folio() failed, re-calling * that would confuse statistics. */ - si =3D get_swap_device(entry); - if (!si) - return NULL; folio =3D filemap_get_folio(swap_address_space(entry), swp_offset(entry)); - put_swap_device(si); - if (!IS_ERR(folio)) - return folio_file_page(folio, swp_offset(entry)); + if (!IS_ERR(folio)) { + page =3D folio_file_page(folio, swp_offset(entry)); + goto got_page; + } =20 /* * Just skip read ahead for unused swap slot. @@ -445,8 +447,8 @@ struct page *__read_swap_cache_async(swp_entry_t entry,= gfp_t gfp_mask, * as SWAP_HAS_CACHE. That's done in later part of code or * else swap_off will be aborted if we return NULL. */ - if (!__swp_swapcount(entry) && swap_slot_cache_enabled) - return NULL; + if (!swap_swapcount(si, entry) && swap_slot_cache_enabled) + goto fail; =20 /* * Get a new page to read into from swap. Allocate it now, @@ -455,7 +457,7 @@ struct page *__read_swap_cache_async(swp_entry_t entry,= gfp_t gfp_mask, */ folio =3D vma_alloc_folio(gfp_mask, 0, vma, addr, false); if (!folio) - return NULL; + goto fail; =20 /* * Swap entry may have been freed since our caller observed it. @@ -466,7 +468,7 @@ struct page *__read_swap_cache_async(swp_entry_t entry,= gfp_t gfp_mask, =20 folio_put(folio); if (err !=3D -EEXIST) - return NULL; + goto fail; =20 /* * We might race against __delete_from_swap_cache(), and @@ -500,12 +502,17 @@ struct page *__read_swap_cache_async(swp_entry_t entr= y, gfp_t gfp_mask, /* Caller will initiate read into locked folio */ folio_add_lru(folio); *new_page_allocated =3D true; - return &folio->page; + page =3D &folio->page; +got_page: + put_swap_device(si); + return page; =20 fail_unlock: put_swap_folio(folio, entry); folio_unlock(folio); folio_put(folio); +fail: + put_swap_device(si); return NULL; } =20 @@ -514,6 +521,10 @@ struct page *__read_swap_cache_async(swp_entry_t entry= , gfp_t gfp_mask, * and reading the disk if it is not already cached. * A failure return means that either the page allocation failed or that * the swap entry is no longer in use. + * + * get/put_swap_device() aren't needed to call this function, because + * __read_swap_cache_async() call them and swap_readpage() holds the + * swap cache folio lock. */ struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, struct vm_area_struct *vma, --=20 2.39.2 From nobody Sat Feb 7 17:42:14 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 580A9C7EE23 for ; Mon, 22 May 2023 07:10:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229529AbjEVHK1 (ORCPT ); Mon, 22 May 2023 03:10:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39486 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232462AbjEVHJ7 (ORCPT ); Mon, 22 May 2023 03:09:59 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD844184 for ; Mon, 22 May 2023 00:09:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1684739376; x=1716275376; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Gm3LH1lmJVZ4Ay531OQCa0nV/AljYlwBGJj9BjRnG/w=; b=HwYCYQzTelpqkmc7BUCMu4WvJkRkoqcP1nwSIR+rgfVE4eDYtf03t8W6 NA2CkomX1YYMisL4Y2PQuN2gbXE/pyIjjUpEVtxLcPA9VQIM6oIi+v++X /pIrh5v3n86H2tlmnBrkMvLV1aYjV0EdCDVDzNWviqviAygd1wV2hhJmr VY3y7msFGgFBP+ni0cPklIDomEoJ9gIxcsqafieZ3x6RLiv90lT52UpIc IwsBR6h3GdIIJup6DRvbCZ/MXyXnAeebNB/tqC21V3vRqV55Nv9gGBo/n DzJNRoPDT4VieYsyBuFmNNiwkPsSyEosYR8n11BL8AuS1qPSAvro8iCJt A==; X-IronPort-AV: E=McAfee;i="6600,9927,10717"; a="337437017" X-IronPort-AV: E=Sophos;i="6.00,183,1681196400"; d="scan'208";a="337437017" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 May 2023 00:09:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10717"; a="773212698" X-IronPort-AV: E=Sophos;i="6.00,183,1681196400"; d="scan'208";a="773212698" Received: from yhuang6-mobl2.sh.intel.com ([10.238.5.152]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 May 2023 00:09:28 -0700 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , David Hildenbrand , Hugh Dickins , Johannes Weiner , Matthew Wilcox , Michal Hocko , Minchan Kim , Tim Chen , Yang Shi , Yu Zhao Subject: [PATCH -V2 3/5] swap: remove __swp_swapcount() Date: Mon, 22 May 2023 15:09:03 +0800 Message-Id: <20230522070905.16773-4-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230522070905.16773-1-ying.huang@intel.com> References: <20230522070905.16773-1-ying.huang@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" __swp_swapcount() just encloses the calling to swap_swapcount() with get/put_swap_device(). It is called in __read_swap_cache_async() only, which encloses the calling with get/put_swap_device() already. So, __read_swap_cache_async() can call swap_swapcount() directly. Signed-off-by: "Huang, Ying" Cc: David Hildenbrand Cc: Hugh Dickins Cc: Johannes Weiner Cc: Matthew Wilcox Cc: Michal Hocko Cc: Minchan Kim Cc: Tim Chen Cc: Yang Shi Cc: Yu Zhao --- include/linux/swap.h | 4 ++-- mm/swapfile.c | 20 +------------------- 2 files changed, 3 insertions(+), 21 deletions(-) diff --git a/include/linux/swap.h b/include/linux/swap.h index 3c69cb653cb9..f6bd51aa05ea 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -512,7 +512,7 @@ int find_first_swap(dev_t *device); extern unsigned int count_swap_pages(int, int); extern sector_t swapdev_block(int, pgoff_t); extern int __swap_count(swp_entry_t entry); -extern int __swp_swapcount(swp_entry_t entry); +extern int swap_swapcount(struct swap_info_struct *si, swp_entry_t entry); extern int swp_swapcount(swp_entry_t entry); extern struct swap_info_struct *page_swap_info(struct page *); extern struct swap_info_struct *swp_swap_info(swp_entry_t entry); @@ -590,7 +590,7 @@ static inline int __swap_count(swp_entry_t entry) return 0; } =20 -static inline int __swp_swapcount(swp_entry_t entry) +static inline int swap_swapcount(struct swap_info_struct *si, swp_entry_t = entry) { return 0; } diff --git a/mm/swapfile.c b/mm/swapfile.c index 8419cba9c192..e9cce775fb25 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1443,7 +1443,7 @@ int __swap_count(swp_entry_t entry) * This does not give an exact answer when swap count is continued, * but does include the high COUNT_CONTINUED flag to allow for that. */ -static int swap_swapcount(struct swap_info_struct *si, swp_entry_t entry) +int swap_swapcount(struct swap_info_struct *si, swp_entry_t entry) { pgoff_t offset =3D swp_offset(entry); struct swap_cluster_info *ci; @@ -1455,24 +1455,6 @@ static int swap_swapcount(struct swap_info_struct *s= i, swp_entry_t entry) return count; } =20 -/* - * How many references to @entry are currently swapped out? - * This does not give an exact answer when swap count is continued, - * but does include the high COUNT_CONTINUED flag to allow for that. - */ -int __swp_swapcount(swp_entry_t entry) -{ - int count =3D 0; - struct swap_info_struct *si; - - si =3D get_swap_device(entry); - if (si) { - count =3D swap_swapcount(si, entry); - put_swap_device(si); - } - return count; -} - /* * How many references to @entry are currently swapped out? * This considers COUNT_CONTINUED so it returns exact answer. --=20 2.39.2 From nobody Sat Feb 7 17:42:14 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 350F5C7EE23 for ; Mon, 22 May 2023 07:10:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232291AbjEVHKd (ORCPT ); Mon, 22 May 2023 03:10:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232476AbjEVHKB (ORCPT ); Mon, 22 May 2023 03:10:01 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1ECF4E52 for ; Mon, 22 May 2023 00:09:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1684739378; x=1716275378; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=YSumEPT2C3HRRYpGGamb3AFAC7MiJLIPce/70xDMyEY=; b=NkRZCdaazOPXQGcQnNsUdcHvnyC1e+/SbhGKcnai7yZTjUtGHvdVt0U+ KlnwZ9kixdS60Zb3iZIfYGb6UxOBudnbY5zIpfq6oFafe5ykcTnGtvMzk 6PlA2htXbJzaH6PGpYxShfiP10uYrpog0dKiM9Z1C+wLLURlc9FD4h9Mf AAXiIfvcQDrtxLB2vQHp4Y+rq1IJNWCh+FN4pMeyCApVDB69Ebj5+89Pd +msENVg5qHQb77JHd1pFW8tSHlO8MWyfqch0sx+oouIf7Agy7iA2mM5/2 xdaR8ZNLMMuxQ2VS7McORAUc3QmaHDdaEXIA4+Qqyj7d2lR9P2JoWtoCm g==; X-IronPort-AV: E=McAfee;i="6600,9927,10717"; a="337437037" X-IronPort-AV: E=Sophos;i="6.00,183,1681196400"; d="scan'208";a="337437037" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 May 2023 00:09:34 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10717"; a="773212723" X-IronPort-AV: E=Sophos;i="6.00,183,1681196400"; d="scan'208";a="773212723" Received: from yhuang6-mobl2.sh.intel.com ([10.238.5.152]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 May 2023 00:09:31 -0700 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , David Hildenbrand , Hugh Dickins , Johannes Weiner , Matthew Wilcox , Michal Hocko , Minchan Kim , Tim Chen , Yang Shi , Yu Zhao Subject: [PATCH -V2 4/5] swap: remove get/put_swap_device() in __swap_duplicate() Date: Mon, 22 May 2023 15:09:04 +0800 Message-Id: <20230522070905.16773-5-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230522070905.16773-1-ying.huang@intel.com> References: <20230522070905.16773-1-ying.huang@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" __swap_duplicate() is called by - swap_shmem_alloc(): the page lock of the swap cache is held. - copy_nonpresent_pte() -> swap_duplicate() and try_to_unmap_one() -> swap_duplicate(): the page table lock is held. - __read_swap_cache_async() -> swapcache_prepare(): enclosed with get/put_swap_device() in __read_swap_cache_async() already. So, it's safe to remove get/put_swap_device() in __swap_duplicate(). Signed-off-by: "Huang, Ying" Cc: David Hildenbrand Cc: Hugh Dickins Cc: Johannes Weiner Cc: Matthew Wilcox Cc: Michal Hocko Cc: Minchan Kim Cc: Tim Chen Cc: Yang Shi Cc: Yu Zhao Reviewed-by: Yosry Ahmed --- mm/swapfile.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index e9cce775fb25..4dbaea64635d 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -3264,9 +3264,7 @@ static int __swap_duplicate(swp_entry_t entry, unsign= ed char usage) unsigned char has_cache; int err; =20 - p =3D get_swap_device(entry); - if (!p) - return -EINVAL; + p =3D swp_swap_info(entry); =20 offset =3D swp_offset(entry); ci =3D lock_cluster_or_swap_info(p, offset); @@ -3313,7 +3311,6 @@ static int __swap_duplicate(swp_entry_t entry, unsign= ed char usage) =20 unlock_out: unlock_cluster_or_swap_info(p, ci); - put_swap_device(p); return err; } =20 --=20 2.39.2 From nobody Sat Feb 7 17:42:14 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 89EE4C77B73 for ; Mon, 22 May 2023 07:11:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232164AbjEVHLC (ORCPT ); Mon, 22 May 2023 03:11:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39174 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232486AbjEVHKD (ORCPT ); Mon, 22 May 2023 03:10:03 -0400 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E9F2E56 for ; Mon, 22 May 2023 00:09:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1684739378; x=1716275378; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pFsVEqpuPiRv19w/rqm+IY358s8W3zh5r+eFbJEC2g0=; b=JUARmWfctMfmKL6sWx4fMCOtvswpiF2eMo/ghaJS8hRaZ/lFgceibrOP Mzfnnxhcs7f/yUNPKSmlmf3EPvXaCw9+K+y9d2RvGMERZI/Z/it1LK8n2 aE8tAQGV5BRj9a3rwtcQNgNLbiS+4D/NADSLqH8IuM+CKPgVzKXX3hM0b TL9mLJpcOVexwjmWVbOR3MKmMxX+eiySzS3eZeZLqSwP+YdX5JeVkTmKf vdjmxeLqM8UO5n/k4iK05Gw8IBdiwSI015RastyRPXY1FU6Kjg/quZagl oTsRiIP/7loM5JkeMSqxoDx5W9LUJzCJzWcjAU0Bpf5RKOm7gAKpVvNjA g==; X-IronPort-AV: E=McAfee;i="6600,9927,10717"; a="337437062" X-IronPort-AV: E=Sophos;i="6.00,183,1681196400"; d="scan'208";a="337437062" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 May 2023 00:09:38 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10717"; a="773212746" X-IronPort-AV: E=Sophos;i="6.00,183,1681196400"; d="scan'208";a="773212746" Received: from yhuang6-mobl2.sh.intel.com ([10.238.5.152]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 May 2023 00:09:34 -0700 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , David Hildenbrand , Hugh Dickins , Johannes Weiner , Matthew Wilcox , Michal Hocko , Minchan Kim , Tim Chen , Yang Shi , Yu Zhao Subject: [PATCH -V2 5/5] swap: comments get_swap_device() with usage rule Date: Mon, 22 May 2023 15:09:05 +0800 Message-Id: <20230522070905.16773-6-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230522070905.16773-1-ying.huang@intel.com> References: <20230522070905.16773-1-ying.huang@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The general rule to use a swap entry is as follows. When we get a swap entry, if there isn't some other way to prevent swapoff, such as page lock for swap cache, page table lock, etc., the swap entry may become invalid because of swapoff. Then, we need to enclose all swap related functions with get_swap_device() and put_swap_device(), unless the swap functions call get/put_swap_device() by themselves. Add the rule as comments of get_swap_device(). Signed-off-by: "Huang, Ying" Cc: David Hildenbrand Cc: Hugh Dickins Cc: Johannes Weiner Cc: Matthew Wilcox Cc: Michal Hocko Cc: Minchan Kim Cc: Tim Chen Cc: Yang Shi Cc: Yu Zhao Reviewed-by: David Hildenbrand Reviewed-by: Yosry Ahmed --- mm/swapfile.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 4dbaea64635d..0c1cb935b2eb 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1219,6 +1219,13 @@ static unsigned char __swap_entry_free_locked(struct= swap_info_struct *p, } =20 /* + * When we get a swap entry, if there isn't some other way to prevent + * swapoff, such as page lock for swap cache, page table lock, etc., + * the swap entry may become invalid because of swapoff. Then, we + * need to enclose all swap related functions with get_swap_device() + * and put_swap_device(), unless the swap functions call + * get/put_swap_device() by themselves. + * * Check whether swap entry is valid in the swap device. If so, * return pointer to swap_info_struct, and keep the swap entry valid * via preventing the swap device from being swapoff, until @@ -1227,9 +1234,8 @@ static unsigned char __swap_entry_free_locked(struct = swap_info_struct *p, * Notice that swapoff or swapoff+swapon can still happen before the * percpu_ref_tryget_live() in get_swap_device() or after the * percpu_ref_put() in put_swap_device() if there isn't any other way - * to prevent swapoff, such as page lock, page table lock, etc. The - * caller must be prepared for that. For example, the following - * situation is possible. + * to prevent swapoff. The caller must be prepared for that. For + * example, the following situation is possible. * * CPU1 CPU2 * do_swap_page() --=20 2.39.2