From nobody Sun Feb 8 04:30:33 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 1B687C77B7A for ; Mon, 29 May 2023 06:14:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230117AbjE2GOY (ORCPT ); Mon, 29 May 2023 02:14:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229624AbjE2GOT (ORCPT ); Mon, 29 May 2023 02:14:19 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AEA7FAF for ; Sun, 28 May 2023 23:14:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685340858; x=1716876858; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Kl6+gAKi9abH9eFCSqmd4OqB2g+JQSA7OXalXR3MGok=; b=Gr+8bmWaXfU1i2tRBYM3nL3PQXbZgt/268Xjsm3ZBxKQWYMWzVcN4SEH GTXk2TJds4Gwexzcvj+SHCAYk+1anls4nGNRc3CQs4g+4nvdq4e0GT7ow sEPM1GgBQf6lCgXyomfpD1ez3ZBzPHz1dcyfpluD8iqwnlTu+wQlFl+iv uNppnq6zP5xoovxHB+RjR397xsmApZwGhzO1VABvAG7Z1rEj4zV20TLef zu6e5SVZ0AHwR/NBQYD8q1zPc8DMeIyIERDueZynyDNM020OktvQDmmln Fcpxa9178gNssT2pO+A+/s2xOOS43VzsI5N8IZ5PowrraH6txJw++bLL3 w==; X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="357881783" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="357881783" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 23:14:18 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="1036079982" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="1036079982" Received: from azhao3-mobl1.ccr.corp.intel.com (HELO yhuang6-mobl2.ccr.corp.intel.com) ([10.255.28.126]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 23:14:15 -0700 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Yosry Ahmed , David Hildenbrand , Hugh Dickins , Johannes Weiner , Matthew Wilcox , Michal Hocko , Minchan Kim , Tim Chen , Yang Shi , Yu Zhao , Chris Li Subject: [PATCH -V3 1/5] swap: Remove get/put_swap_device() in __swap_count() Date: Mon, 29 May 2023 14:13:51 +0800 Message-Id: <20230529061355.125791-2-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230529061355.125791-1-ying.huang@intel.com> References: <20230529061355.125791-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" get/put_swap_device() are added to __swap_count() in commit eb085574a752 ("mm, swap: fix race between swapoff and some swap operations"). Later, in commit 2799e77529c2 ("swap: fix do_swap_page() race with swapoff"), get/put_swap_device() are added to do_swap_page(). And they enclose the only call site of __swap_count(). So, it's safe to remove get/put_swap_device() in __swap_count() now. Signed-off-by: "Huang, Ying" Reviewed-by: Yosry Ahmed Reviewed-by: 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 Cc: Chris Li Reviewed-By: Chris Li (Google) --- 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 Sun Feb 8 04:30:33 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 8B45AC7EE29 for ; Mon, 29 May 2023 06:14:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230153AbjE2GOc (ORCPT ); Mon, 29 May 2023 02:14:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43006 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229958AbjE2GOX (ORCPT ); Mon, 29 May 2023 02:14:23 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4AF1DB1 for ; Sun, 28 May 2023 23:14:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685340862; x=1716876862; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9Oxpi0PPziaQhMGSH3TrNy4IFICDc28ZfyvpBf2ejj4=; b=DlehFadix+4U+iWpL/fGaz7SLQ8p/smv5qoK/XfFKa6WPKzUKo4ZYRlF go98QSjc5Gt5s7PmqGZpaLv2+EiSntjVbIXIlrB7UYchNbq2tgZetQ2VJ St6zHzrnU8JyBItisGmohcSwjDj2Oi89j2iRzdaPN2gE0ItATH3LYP9p+ s5ul0U7OVGd9ffArcwvwCyI8hzl8xE72sqvXYsf+NCTFLBxXXmVkzj9+J IokpS/30mPMXuSUS1tghh/MUuXRT/Nritu1yiZvdSUlbYh4B/6ZI0SHgp KHJF9UlDihAEIBLX98vENdrLrix9KR7G9yqHHiJ7YPMKH+3IwLMbEQqsL A==; X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="357881814" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="357881814" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 23:14:21 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="1036079991" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="1036079991" Received: from azhao3-mobl1.ccr.corp.intel.com (HELO yhuang6-mobl2.ccr.corp.intel.com) ([10.255.28.126]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 23:14:18 -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 , Chris Li , Yosry Ahmed Subject: [PATCH -V3 2/5] swap, __read_swap_cache_async(): enlarge get/put_swap_device protection range Date: Mon, 29 May 2023 14:13:52 +0800 Message-Id: <20230529061355.125791-3-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230529061355.125791-1-ying.huang@intel.com> References: <20230529061355.125791-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 Cc: Chris Li Cc: Yosry Ahmed Reviewed-by: Chris Li (Google) Reviewed-by: David Hildenbrand --- mm/swap_state.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/mm/swap_state.c b/mm/swap_state.c index b76a65ac28b3..a8450b4a110c 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. @@ -446,7 +448,7 @@ struct page *__read_swap_cache_async(swp_entry_t entry,= gfp_t gfp_mask, * else swap_off will be aborted if we return NULL. */ if (!__swp_swapcount(entry) && swap_slot_cache_enabled) - return NULL; + goto fail_put_swap; =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_put_swap; =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_put_swap; =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: + 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 Sun Feb 8 04:30:33 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 4A269C7EE29 for ; Mon, 29 May 2023 06:14:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231138AbjE2GOg (ORCPT ); Mon, 29 May 2023 02:14:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43052 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229624AbjE2GO1 (ORCPT ); Mon, 29 May 2023 02:14:27 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF0B3BB for ; Sun, 28 May 2023 23:14:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685340865; x=1716876865; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=tTttI7ks79EVJT0Lkkg7CgRwuqqX+cQLYrih/1r1FtU=; b=ZS60TbM6qYbN/Q3CVL42COpkGiFTtmto2DACxT0SSW21D9JEoDmHh7Cx ZHNrykD1KPtmB0FZc+9nL+MNg3sr9yZ3ZCxkFizkchdiDHy6wAg568PPg XdwLE+2T3omXzYVzcqxW3BWshiZe4UK+gVgGmqGt/iAyiJYf6BLlNOUiM IudLGi0St3ihKDDHY2Qmrumqs3J+AQT3tYjF3uFCdDy/hXYXvHoHBWylP qh5kQGpLN7oEMgdY6c+beM648+nu+q5ZgfwYH0V5chQtSr0Qgk4HYEQYb Pz12B7hf4p20YEDh+YPy0bgQsTSDsPEAqMUvCequ6LAOSsA+m8Q5rlCKx Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="357881834" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="357881834" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 23:14:25 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="1036079999" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="1036079999" Received: from azhao3-mobl1.ccr.corp.intel.com (HELO yhuang6-mobl2.ccr.corp.intel.com) ([10.255.28.126]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 23:14: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 , Chris Li , Yosry Ahmed Subject: [PATCH -V3 3/5] swap: remove __swp_swapcount() Date: Mon, 29 May 2023 14:13:53 +0800 Message-Id: <20230529061355.125791-4-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230529061355.125791-1-ying.huang@intel.com> References: <20230529061355.125791-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 Cc: Chris Li Cc: Yosry Ahmed Reviewed-by: Chris Li (Google) Reviewed-by: David Hildenbrand --- include/linux/swap.h | 4 ++-- mm/swap_state.c | 2 +- mm/swapfile.c | 20 +------------------- 3 files changed, 4 insertions(+), 22 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/swap_state.c b/mm/swap_state.c index a8450b4a110c..ef32353c18a6 100644 --- a/mm/swap_state.c +++ b/mm/swap_state.c @@ -447,7 +447,7 @@ 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) + if (!swap_swapcount(si, entry) && swap_slot_cache_enabled) goto fail_put_swap; =20 /* 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 Sun Feb 8 04:30:33 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 03FA6C7EE23 for ; Mon, 29 May 2023 06:14:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230521AbjE2GOm (ORCPT ); Mon, 29 May 2023 02:14:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43158 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230342AbjE2GOd (ORCPT ); Mon, 29 May 2023 02:14:33 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 70F8EF1 for ; Sun, 28 May 2023 23:14:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685340869; x=1716876869; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=BU9rFfysv4elNLJ2GHzqi2liUatC3EzDYJWAUuFdw+0=; b=cJxrFtlCwIuI2Qjv4t0svwIYrYf8UN4laXVC3qIsq+xZpAtNS+1bPG4V w2QJ0pcsaEAY83MpX20jynYcuQx85NV01amjXU4pw5N6Wce5yK3+ICZco 41D0VgIKfZ9U9tBkRoE7eoSFx7zxvflNl4lFRqZMY45WpwvyXiKECzKYT elWTHgSozyruFTewpFqcuPxnm6cR/8d9Em19AQ7PGiPVR9H9wt/F3wPRA 2g10+RcObIGDHtdXWDAVaetUpmjRsxIdChk2PtJOWHZ0/DvM+vgpYAjU6 bVaEpse+rgKHOv+FqDZRH/c4SEyvpOKupgPB0uKd3jBU92DGtOy9Zzq3I A==; X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="357881854" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="357881854" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 23:14:28 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="1036080010" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="1036080010" Received: from azhao3-mobl1.ccr.corp.intel.com (HELO yhuang6-mobl2.ccr.corp.intel.com) ([10.255.28.126]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 23:14:25 -0700 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , Yosry Ahmed , David Hildenbrand , Hugh Dickins , Johannes Weiner , Matthew Wilcox , Michal Hocko , Minchan Kim , Tim Chen , Yang Shi , Yu Zhao , Chris Li Subject: [PATCH -V3 4/5] swap: remove get/put_swap_device() in __swap_duplicate() Date: Mon, 29 May 2023 14:13:54 +0800 Message-Id: <20230529061355.125791-5-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230529061355.125791-1-ying.huang@intel.com> References: <20230529061355.125791-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 folio in swap cache is locked. - 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" Reviewed-by: Yosry Ahmed 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 Cc: Chris Li Reviewed-by: Chris Li (Google) Reviewed-by: David Hildenbrand --- 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 Sun Feb 8 04:30:33 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 61688C77B7A for ; Mon, 29 May 2023 06:14:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231434AbjE2GOv (ORCPT ); Mon, 29 May 2023 02:14:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231428AbjE2GOk (ORCPT ); Mon, 29 May 2023 02:14:40 -0400 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8435D8 for ; Sun, 28 May 2023 23:14:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685340872; x=1716876872; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=gqcq606hnAPL/bQemwK8mT4rmFd1An/GN7J3orZJsNQ=; b=SSYZBD301JuYZBoBT7PD8AXXBoFzSO4yCeBuFXJdalYNwpHI8vbfrPgq 7Ojw1/FHjZ+1gCzdD4TEc5ri+ul3ewyumOgpcMLepDI5OJSUpJKK1rwiL UIAzJnVIbsBW5YVZXHfSxvqrcYple6lvkmaBoze5nwoHu/6am0CmRbWRy jD0F06lFIx6fZmBgyAq9NX5SxAEusvXQOjWzJdfEJTtE2dQ9VPgcY4f4B EiMbSLer25BIcWAtrD+QZr/7bjBGWH3AVR6yqxLWqyj8mXMrPBZ3aLUm+ aw051r7ZzpD8TPUBIYQo7EYnKVYKenAUSCbuztyX40qRLhWQ/0HTkBlCh g==; X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="357881879" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="357881879" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 23:14:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="1036080021" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="1036080021" Received: from azhao3-mobl1.ccr.corp.intel.com (HELO yhuang6-mobl2.ccr.corp.intel.com) ([10.255.28.126]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 23:14:29 -0700 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , David Hildenbrand , Yosry Ahmed , Hugh Dickins , Johannes Weiner , Matthew Wilcox , Michal Hocko , Minchan Kim , Tim Chen , Yang Shi , Yu Zhao , Chris Li Subject: [PATCH -V3 5/5] swap: comments get_swap_device() with usage rule Date: Mon, 29 May 2023 14:13:55 +0800 Message-Id: <20230529061355.125791-6-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230529061355.125791-1-ying.huang@intel.com> References: <20230529061355.125791-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 aren't some other ways to prevent swapoff, such as the folio in swap cache is locked, page table lock is held, 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" Reviewed-by: David Hildenbrand Reviewed-by: Yosry Ahmed Cc: Hugh Dickins Cc: Johannes Weiner Cc: Matthew Wilcox Cc: Michal Hocko Cc: Minchan Kim Cc: Tim Chen Cc: Yang Shi Cc: Yu Zhao Cc: Chris Li Reviewed-by: Chris Li (Google) --- mm/swapfile.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 4dbaea64635d..3d0e932497f0 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 aren't some other ways to + * prevent swapoff, such as the folio in swap cache is locked, page + * table lock is held, 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