From patchwork Mon May 29 06:13:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13258140 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 0693EC7EE23 for ; Mon, 29 May 2023 06:14:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9293B900004; Mon, 29 May 2023 02:14:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 88C53900002; Mon, 29 May 2023 02:14:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6DD89280002; Mon, 29 May 2023 02:14:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 59E73280001 for ; Mon, 29 May 2023 02:14:22 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 255DCC01F2 for ; Mon, 29 May 2023 06:14:22 +0000 (UTC) X-FDA: 80842277964.12.A214265 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by imf12.hostedemail.com (Postfix) with ESMTP id 1ECBC40005 for ; Mon, 29 May 2023 06:14:19 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=GQIwOG2F; spf=pass (imf12.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=ying.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685340860; a=rsa-sha256; cv=none; b=XNwS8FC2mtIga5Kla4/rYxnzK77FfuCLST/60JiDMsfauW9sQM6OihFHzSJE3NjbYIincs c7FwKDgdqUpJGnC2zuAJelzIdGnqcklFRXfUOjizb/7xSLUR9nfZU0LFjuUsMyyOe1EjPa tWfmD3Q1vn9vmxjjSfcMXM0GvIUq1SA= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=GQIwOG2F; spf=pass (imf12.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=ying.huang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1685340860; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=gTjWga6OsszRk2MnRlZS+LYEg6py10ZYIDqkJr0/juk=; b=1vU81vcAWgCdQchalydZ4AXraRqFchwSd4I/N1qgQY6I/NqsC3CixcX2bwfiQDRTaLOKku zzIZuBQ9j+tGFiKpHJE9q3nFfsjp1hhccM6QYo05yL+sJzFL4B37KPcgpF8lX8SJMF0p2u 2keqdiZ9bo2yQ554ciH1fO4ZAGQ7qag= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685340860; x=1716876860; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Kl6+gAKi9abH9eFCSqmd4OqB2g+JQSA7OXalXR3MGok=; b=GQIwOG2F7Tj5a/0df1l4BkiAKJB+Gi/J24ohc7wofsFTswcd5irnHLFq njt1sPkM9N8DIQ7PM1m28DYaN8vHWe2yYenXMYSvRG43/N0YqYTxW1mSF ZBf7R1JElSO60ltyAzqj/v9LI1XD2E8xkWZirkpl5TqNu4N/wzMjpp7yd dAHqpLV+7ovUR7GBwxBMNS752kBBhl9IEvlMTS5sFPW0u7m6rowpWQ5fI WdySvhLXuukbs/YEh/Ad6dcl1pEf9jBAZ0gNIJ3UBeXLTloOkz/wgqtMi mbihH4gH3kb33x0il6gKGNrGkmooI70mgFRsE7MRQKWEhNylzXDqWQ4FF Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="357881790" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="357881790" 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 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 1ECBC40005 X-Stat-Signature: e83tqkcnje7y3mgqsuuiu9x7581uceeq X-Rspam-User: X-HE-Tag: 1685340859-378228 X-HE-Meta: U2FsdGVkX1+U3TFa1KHMNvNXkqstdBgZIY19PD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l2VF7OnI0d3o1BPq8+lG8qHPiBT3vvCmUC0BKqh+sXYn+SDbqn1rfhu/p4pA/ByNladi80gp2pcgtNqbxHnYVWuEOVYmq3Y+dLMKyGI3twFi/k7Y034/OmeY/0oK0p9u9scbshLY8AFa4QlQ7Ry+Vcjq4geJsYRU9UuJ+rlXuPC29097bq9u1gceMvbV0dVL84ILfN68oR8j1gzquxxs86qmh1YPE4s+VI97aB69MWIGHxZ6jU26/387blWobimLA+Mz52pg5mbL2pwly/Bkd+toTKD4glQFOXIUCnwFRRwEZJDcGIZF+JqorIW0LJKZ0o4rF+gh05AtpsLiSaGxN8yelOV2NnqJsR2m/VSHeoevOfk6bFrVnagmiQkK2zZ4jZJEvNuTH6poJarN5No/Hv 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: 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 --- 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, int n) int __swap_count(swp_entry_t entry) { - struct swap_info_struct *si; + struct swap_info_struct *si = swp_swap_info(entry); pgoff_t offset = swp_offset(entry); - int count = 0; - si = get_swap_device(entry); - if (si) { - count = swap_count(si->swap_map[offset]); - put_swap_device(si); - } - return count; + return swap_count(si->swap_map[offset]); } /* From patchwork Mon May 29 06:13:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13258141 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 41E17C7EE29 for ; Mon, 29 May 2023 06:14:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C0F15280002; Mon, 29 May 2023 02:14:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B9861280001; Mon, 29 May 2023 02:14:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A3935280002; Mon, 29 May 2023 02:14:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 8D2F6280001 for ; Mon, 29 May 2023 02:14:25 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 5BE0D1401EA for ; Mon, 29 May 2023 06:14:25 +0000 (UTC) X-FDA: 80842278090.17.90D0086 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by imf14.hostedemail.com (Postfix) with ESMTP id 625DD100019 for ; Mon, 29 May 2023 06:14:23 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="mckJ/evu"; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf14.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1685340863; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=wOJYVTXnoEm5Z/BbFhrWdccE5L0r3SqMJHK9UvXxhZ8=; b=FDLkldpAJlK/OHsnlK5hwD4HLs6awb3geO8knYKIJwnjBDpk/VijKcdSJraJz6tMPjb0iM naLxT16Z/DkR+tme8t9nvipBHN/rBeCPW9OrqueoEzZak+cd7r0q0us8CeJAwO/FHcp9Gy 5hPAHASBPzOI5Qwn0b2DyEG2GjJx4Qw= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b="mckJ/evu"; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf14.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685340863; a=rsa-sha256; cv=none; b=yBINA/YQ23SUsbpMASfYK6zPkaBAwTMZBxan2eHkAS6pYo8Bh4rRUYqQXytD0WhlmyfMP2 FeVAs3G7k2BX/j0Qgvj5TCzwnmwSLY0RPw+3Fr0hS6smPrNAlm7hYqga5Wt6bb94e4a6Eu ZoA+g859LnkxrRJZ295ftUFNsGo40/E= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685340863; x=1716876863; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9Oxpi0PPziaQhMGSH3TrNy4IFICDc28ZfyvpBf2ejj4=; b=mckJ/evuzSuwRiaVf4daRgrIWcmVxP2HTb6o9FhdOEkJn30ynIYGrvlN XegjOH51xNhw8BcjkSojCIJQ36Y5+Mz9h3aMXZ3wTN4IadzwTBpvvhTZE C2A0TIxCdKykzck7zy3bw3lU/OAL0a9lHB63JvZduNGU8jOBtitwTu3OV OQlgsXu8fviMoSefWPaPUXIskb2OlWlMt7fHEiF+728C3kjH8VgzPdwHC Nm+FYcKnbcPPHcZCjHp9kFmRogTR6qhlqwGVBAXGqxtD3goe3xMzRBzpq hRjrLAAO/bkN5xRIsjQbBrSL4UndhOUhEzQIKGcDs+zI89hx5jg4JAPph A==; X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="357881822" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="357881822" 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 X-Rspamd-Queue-Id: 625DD100019 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 3qad6q6d38ojex6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QmgmN9Sm1HLYsotDlNDbkqq/hKdgI5pH44Q6bhuxqk9i31n+5Au+6p2BV9GZRNoHEd/RVL4WwhQkJs9hO865wHd1By+rF/4VHYPDLmSiDdY0df8zJ0VW8am2acbrEZ6ilShrNqrTA8N6KAWWgyczNitCR8KKW6XYYttY9I/X3lh/VXcQQEOL9rEXzSbvw9v31DEYd6MSN/vL1D5FIuO+4Eu34O9NuQgqnD7sMqQGnXEa/IOS+q0Guly803qtr1+zlbj0I8eiyCk+s8MmPUvbCT8fGyoJhhOU4PMzjglXVHJ5cUTNiRpZ8VZzM4UJdCvnyuCAVW/8iIijbRUoJa29DHy6BngZ3vSEb0tGmH/bfxanjYys1yGZDy5VeYE26uVLjZxzZLmIzabAfl/+EY2ZiH2/gXJt5+zMu895kxzLi0RW61IlGSF7KfcTlR187yOY1taZUQp/rPp48jOUHyppZ1/rHta4n 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: 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: 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 = NULL; *new_page_allocated = false; + si = get_swap_device(entry); + if (!si) + return NULL; for (;;) { int err; @@ -428,14 +432,12 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, * called after swap_cache_get_folio() failed, re-calling * that would confuse statistics. */ - si = get_swap_device(entry); - if (!si) - return NULL; folio = 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 = folio_file_page(folio, swp_offset(entry)); + goto got_page; + } /* * 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; /* * 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 = vma_alloc_folio(gfp_mask, 0, vma, addr, false); if (!folio) - return NULL; + goto fail_put_swap; /* * 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, folio_put(folio); if (err != -EEXIST) - return NULL; + goto fail_put_swap; /* * We might race against __delete_from_swap_cache(), and @@ -500,12 +502,17 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, /* Caller will initiate read into locked folio */ folio_add_lru(folio); *new_page_allocated = true; - return &folio->page; + page = &folio->page; +got_page: + put_swap_device(si); + return page; fail_unlock: put_swap_folio(folio, entry); folio_unlock(folio); folio_put(folio); +fail_put_swap: + put_swap_device(si); return NULL; } @@ -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, From patchwork Mon May 29 06:13:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13258142 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 EEA5CC7EE23 for ; Mon, 29 May 2023 06:14:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 901EC280003; Mon, 29 May 2023 02:14:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 88AF5280001; Mon, 29 May 2023 02:14:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 70505280003; Mon, 29 May 2023 02:14:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 58AC6280001 for ; Mon, 29 May 2023 02:14:28 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 2C6911A01DC for ; Mon, 29 May 2023 06:14:28 +0000 (UTC) X-FDA: 80842278216.29.0456912 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by imf14.hostedemail.com (Postfix) with ESMTP id F03A810000E for ; Mon, 29 May 2023 06:14:25 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=G9u8V89h; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf14.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1685340866; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3xIj4lwktXLAySa7XqSBq9+rB1Zc0DD28GkVkkU93b0=; b=7Bv9VeQawm9tJcHo3O/CMTkO+zk/5BwdAravcTXF+jjSkr8GRw/IZznVLsCX5PJUqLs2wW EAGaQm5+FzINk0FsSx80YwBv1NmdLluvyQ2/wgJVfZq4kP19O7Z/WcFlsu1GzoWTlZVZGW Er9qhyhoZevPF1D2em2e38PdIDt6wMM= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=G9u8V89h; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf14.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685340866; a=rsa-sha256; cv=none; b=1nTjU1niaLWU1dCvE5Si5zSjaYdQ8Afjw0yiHOt3XGVcamiMcTZa9q4RDxx9Ja60K6UbT8 PBQ8rc1fy/Axhjiqix5OSR7MtXXzzGFZxrK7Xp9ASFzSxIOx9zux3jMDlh1n3bStTeLgE8 Qlx65XIC2YtgAFCRQBlok/4dtgWw0xA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685340866; x=1716876866; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=tTttI7ks79EVJT0Lkkg7CgRwuqqX+cQLYrih/1r1FtU=; b=G9u8V89h7gMnJ7E0iHEEsg7GcBbp7vosAx2TvrJpo3wcvC/hdeQD3xpG 0cGI0545KoXp6XaBd7z1iHFAv3Jl5DF43zDfyzag+1URZ3XdzFSMjbknt 53X39dQYjoo11OjYleFeitis9txRax0uvJyUQ5o1HG3qI6YFBYGooXsBK RfrA0UAXdbgd4fZZYewWHBDqlnOTKtnuWSj4RojhLzrpzYMcWOPlQm/LO +yiAl21C5lAqUjlvPEZy0saXg8w83BzAd6nX5QdaFHr7XxTQWA2v8fTXr xya+guCeq9sBtEFbo30IqiJeVGqp3nhI6vUdQdM/M6i3HMRcOB2UGZzS/ Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="357881842" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="357881842" 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 X-Rspamd-Queue-Id: F03A810000E X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: ejb6ifdxkdk9j677whpkpemikuzed3uu X-HE-Tag: 1685340865-149910 X-HE-Meta: U2FsdGVkX1+cDJpYbZ/KFXv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lN1VtpZlpr2ElXIlMVbSyHz7Qrk5aNJHM34Lh1OBqRoX6Wxv1fi7eYIVtuBh/P7uL4FNUOUTdBk0C8/+byUhMisy2S4081lvShAaatSu7iSksmxb4Ea0g0jiCT9Jc8aUtqgx6P4TrIUF8UxcfJkpcUHgZL5YfwpND0WtSjCgwKhjjTx7XllWK6P6uYqwAtzA33vwWENwpjVaNRkmfWBFwwnKRwFI0Z4E9fNwUHZKW10m7QFz1RnMQIzukV2eHBKviq2oZwtNgfaaeDxXbq4PrH0J6kroMYN4bfxuVjVo/VEz8gNCqi8WGvgQcZ/E8Kx3WwKJCcaBEvAgafWjZYM5ZnMVfmFi3YSS5e5lOtxE3tGOVc/cemq//BglWl5QzLhD0uMoXP7h6da96Nt4tqG+rCx 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: __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: 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; } -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; /* 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 = swp_offset(entry); struct swap_cluster_info *ci; @@ -1455,24 +1455,6 @@ static int swap_swapcount(struct swap_info_struct *si, swp_entry_t entry) return count; } -/* - * 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 = 0; - struct swap_info_struct *si; - - si = get_swap_device(entry); - if (si) { - count = 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. From patchwork Mon May 29 06:13:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13258143 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 4A295C7EE23 for ; Mon, 29 May 2023 06:14:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D4798280004; Mon, 29 May 2023 02:14:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CD089280001; Mon, 29 May 2023 02:14:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B23E4280004; Mon, 29 May 2023 02:14:31 -0400 (EDT) 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 9BAC5280001 for ; Mon, 29 May 2023 02:14:31 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 6E38F1A01DC for ; Mon, 29 May 2023 06:14:31 +0000 (UTC) X-FDA: 80842278342.25.0791023 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by imf14.hostedemail.com (Postfix) with ESMTP id 69FF4100011 for ; Mon, 29 May 2023 06:14:29 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=cJxrFtlC; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf14.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1685340869; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=OXQpXUyD3R4+lamxgQGM0SaoDtlSH6lRmsX6AawfPEk=; b=X9fHl9/mLUafP0LUHO3BKt0RbEJPMfDNp2RikwooBjMmAZ0O/yT5zOLjpVzOwAolQTUT9G L4BXJVt8eaNhlTZED5mTDYe3NgyVYkWQcrM9PD6XhhEG8mqBq91Z+4Ii58H0/SLceyk6Uv k4w1IzoUeH2+T4/FKEmS/MqN6Em5M8A= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=cJxrFtlC; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf14.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685340869; a=rsa-sha256; cv=none; b=NfPwZ5j5VNYpINSFPQZfT9gPZQivEralJ8iO1U8VVs+PCHzWkjJBqxTd1gFxZuSli/ceMw wFQODh1Bmr9zj0a47pJmSfG+QQ0ee/MbingowfOeC1Mlc4WLEFyesgx2cfJVF6aPjzvoL0 apdYiSKaFYX1UTmndNiqcb6wCj2Y99s= 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="357881862" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="357881862" 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 X-Rspamd-Queue-Id: 69FF4100011 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: b458kkhjceimscpjz5oh8zou1g5en6nx X-HE-Tag: 1685340869-737623 X-HE-Meta: U2FsdGVkX19E/mxfg79st+yASorWuCoeFduDRSWeI7wnhKgufOUBHYhMmY6yf0kMAbrMRzcZ4Xa9tVhaCppHkdctUUmfP9zDA8g2n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ZEvFNivE6sHCE2MBnZrn87brqAeFIBtfbY6PmuyK0tIUgh4SUGAJs6nta3Kp2boW0sNCL3Cdj7es5QiOqrLIKSzo94EogkVlSORdOLoSSe1gQfHFFGWb2I5LfbPPaRJA2/h21vFZ643ayyzRFjTxD6tIdGOQQtc+jTlOSu7Dlu3GhqaFUOcKVOldL6BK507Vf5DeSRUVtFRlHYv+VT3Y28VSpmAi9EnncwoWuaTUFFuTQgjzKl8nsu2HYPsYzNsScl9lqKrajTMcFn4T/Yi6elawDx0J/lodP7tcOebiaVxd86iKYtTBpmSW1bNJHgBX2o40= 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: __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: 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, unsigned char usage) unsigned char has_cache; int err; - p = get_swap_device(entry); - if (!p) - return -EINVAL; + p = swp_swap_info(entry); offset = swp_offset(entry); ci = lock_cluster_or_swap_info(p, offset); @@ -3313,7 +3311,6 @@ static int __swap_duplicate(swp_entry_t entry, unsigned char usage) unlock_out: unlock_cluster_or_swap_info(p, ci); - put_swap_device(p); return err; } From patchwork Mon May 29 06:13:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13258144 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 DD5ECC7EE23 for ; Mon, 29 May 2023 06:14:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 75426280005; Mon, 29 May 2023 02:14:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6DD5B280001; Mon, 29 May 2023 02:14:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5303B280005; Mon, 29 May 2023 02:14:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 3C4BC280001 for ; Mon, 29 May 2023 02:14:35 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 1C37B1C6E56 for ; Mon, 29 May 2023 06:14:35 +0000 (UTC) X-FDA: 80842278510.28.C72C30E Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by imf14.hostedemail.com (Postfix) with ESMTP id E7DCD10000E for ; Mon, 29 May 2023 06:14:32 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=UWyzOOBS; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf14.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1685340873; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=fpr/LXblsUT/QgnM7sVAAykJFiLzmJXXkITKLa/9iMc=; b=vyqfDwPQezoxGPs6sSAqiNYrhELrWg1GFzgcoGJC94PuFvJrXk7EFnUY9o4Kk2SwMAD0bm weKdFGSu6kDlW0m+6E0e8KZZoBiWb2RSNUj2ilMCkEOQNpwJIOXRQv/nKJe0vK92o6kt1W AJpRdxU+Ktil6unWM4+egjpfu0foI8w= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=UWyzOOBS; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf14.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.65 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1685340873; a=rsa-sha256; cv=none; b=Y7pLRHU2H/OoktvWzgQqAo6yfr9MvnVa0xqIVq8FX6i6Z4orr5HCZ6ghkK4vZeayCUfaT1 wV/I2xZiFh96SO+OvEC8vgZvniAfT0JnkOu4YwjW1YpXcRbhOpm/SMiwbDar65qUqlVVpd 3Q7R+hlZkxwG9gi3sh+W3Vj8zxbavZg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685340873; x=1716876873; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=gqcq606hnAPL/bQemwK8mT4rmFd1An/GN7J3orZJsNQ=; b=UWyzOOBSEAHR8Hx3uDpC2RuAJazJWHKwmDu/u/NuBqEutOp3R6xWkTt5 HAOxrZGILlV6IlJNIjpUPfFzr4JbA9JLa5vWZggpG0O3OTVFncADWnIQl 5e4I577tuEXnToM33gPw0FBlvvQs97eNE9ZuAdYuXH1EhBzc7etagxTCe ohFiyI+CVx8aFb7xNKgGcpBQnRL+05e1JDQiIjYtUxjUqCi3Bg5jL/pse pFLfNxH4BrV5yvoV7Cdu9p99r/qhqCvVs72iK0z/bi6TrwuoltQHllR/6 KLRP2cGTo169MVZQC+FEgtsEMuoBYlrq2t8PUIvviujd4v8HnXubwbGwM g==; X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="357881887" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="357881887" 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 X-Rspamd-Queue-Id: E7DCD10000E X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: x51joftj87qfbbw9n4befenmh6k9cetu X-HE-Tag: 1685340872-354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NpQ6FBcEn1eddEjjpuNK5KeyC3OeLVZweBXd+EoA9wYjzsOXvyWM9hcbW8s5vJhh6vGpoAsm6z7HsucNCrkqKF8bm7TWO4aQfb8Il/nO4gGM1D/iEAmpg3jZ7vjr+7Uy/mW9akKv2eZz8D2MJ1tv8VvpBzdk1rG9F3eguIFXs/VxqQIUGG1XKm6Yv4SdYGV2Zc4pNPpgOyrVciFfyDHsUSfK+r8U1gjnOHl25r0SoPcyu98KPH85snPg4gTwnHFVELn1WqvV7AfkxxBHvGV9PqROvCV7a1e22IE/xhQlEAOMA11qdVnt2CYWDQ1PF4jNxR5GjOf63idOlvftZdx328ayECStcFEVMl7y49lPGhtkJpxyBtjxpb8qRi31svtQpZjJxdMRfdXmB08NpRcc/JFG8HLNa0MlRjiqV3XBrzH1LZ8wlGOzG1GkV7d7uIUgXHk= 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: 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 --- 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, } /* + * 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()