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: 100082 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1307281vqr; Sun, 28 May 2023 23:15:41 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5RGmdEH5k3bNlDVCSE6FUGbV19AU9BrSzZUjnbMIL30uQUfwzqxbGVluHBpybGAajIsSt3 X-Received: by 2002:a05:6a00:23ca:b0:63d:6744:8cae with SMTP id g10-20020a056a0023ca00b0063d67448caemr7920225pfc.2.1685340940734; Sun, 28 May 2023 23:15:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685340940; cv=none; d=google.com; s=arc-20160816; b=rDmm9HJvzA3E+SC5QwVzgzYFfr861WQUPL5sAkbF7AWRwY1ee9+i4IM2ackBy+C2Px o4GPAbdl4biMDD3cLN9rk3vcVl9fmkF+yej43QJMuqO5eMpUqUaxKn4i2htZe0w69HX3 DMGp4Lj3wXBDmyHW4BmV1XQjy8ii1ntbePLv8+GY4gMYjisY008EJSYQ20CBiuoqWYgW 5VvLchnM8F9EB30aM44U1PYzOgi47ZhDGGGHimkfjelOqb9l6iuMLIlqWPMVqT/0DEjc wve47o4zcBF4gnKwCpV8t5tPX6mr/IdAYuPFzM+0+F/TgFD0Xaj6lUBYjaPtjSX8vMUO YLSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=gTjWga6OsszRk2MnRlZS+LYEg6py10ZYIDqkJr0/juk=; b=kTb0nmu/gMZPUSz/kGAxfzCOazQiNQuToJptQuTM5SFAm7ocLwFZ7Fwz3oF6WcW6ml bljrQ3ZMSV1+Ur9AZLCZGk4EbzDhYn5MNVaS548BDSnEqD2HjBHNg6RxWUSVcyGFqax7 52G1GXZnsHg+nGjvmxF3AlZGnIno58jmSEZlytvanxsSR7PcLDWtHOwXOQCyFIvR1+x9 du/UO6+BOurs9JgPJ9Qgqci7B0eRxcP35N2F60MDxp9ekYz30grbdzxQJeRWlAXQ+1vR ODIjCv7q1frUpEFUEVimPVNcuAuUbVbSDCVSbV0wgJda+Y518ifh1J+ADtO1oolY0wb7 KrGg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Gr+8bmWa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 191-20020a6307c8000000b005139d2b9306si8089807pgh.514.2023.05.28.23.15.26; Sun, 28 May 2023 23:15:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Gr+8bmWa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229682AbjE2GOX (ORCPT + 99 others); Mon, 29 May 2023 02:14:23 -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 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767208062162736333?= X-GMAIL-MSGID: =?utf-8?q?1767208062162736333?= 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, 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: 100090 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1309703vqr; Sun, 28 May 2023 23:22:54 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6PAQZ+mRW8dEwNAFTocQgYCtZqgeY7szpFgQ+xCjdD2x3YAnD3bpvahzVoABnsv6j/kL7W X-Received: by 2002:a17:90a:868a:b0:256:69e8:9298 with SMTP id p10-20020a17090a868a00b0025669e89298mr4130371pjn.17.1685341374487; Sun, 28 May 2023 23:22:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685341374; cv=none; d=google.com; s=arc-20160816; b=uxzNXCPVc6dikF2UcNkJjGExb0KljvgRB/y7Q4ziYSdjMLtwhufFJ8ny+0srxTCjsN B6pch4SyanAW/i2GN4HnR4AOy/eCH+M0UukQERBtwBtGkx7UiFE5nbI1A+bGc2th1JOb 7FYDflJrgaeKvylfeLL75dS+uqWMXkwujbjzWM+MnUWbjgykEGuEUSLTB9eIDPgdBvN9 /d8ONGnP2EdqrWsqcI/T6kZ+bB6MZgobYm7JQE4i1ofe/2dRCM/D/w6yqFACgfwSiA+L K6OnAGTQ8mGGzNldViWKYBccVWUkQX8xmOspZvHut6/6Fq2+RhhPyHYEn2zgddISdi/t /C7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=wOJYVTXnoEm5Z/BbFhrWdccE5L0r3SqMJHK9UvXxhZ8=; b=nd7n7NMyYXFVUo0DaI9+vKj9fDegEnmtE6XOVm26ONfT5rCAt788w0GkmHUHqdZo+9 zjyafpc3Y58G5AkfbJtGHwJVax4Kxdsn9scm8S3iEEaE6+UcQSQdBPrl+XI+Jp3Mp7o4 bcOChP35iF2fJhJNIws+3Kly/FJ0OPV2cskF2dVs6kJfZ2E+ruFiRHRUXbBBQfa0vsqo +6JxFQmIt3BJwdVPg4mjpT/QZEvG5KEXBETfQ3c8x61kbVnEjQS8ph5RyiNQu8XbnaPF tht4qn40i/0XAhD1qG0QihEf4Eae5ZZBblheZmnNGIGI0x5suQEtO3N3p6xtGCWq3SUT DRfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=DlehFadi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id em5-20020a17090b014500b002470f78a168si1425692pjb.104.2023.05.28.23.22.39; Sun, 28 May 2023 23:22:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=DlehFadi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230160AbjE2GOb (ORCPT + 99 others); Mon, 29 May 2023 02:14:31 -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 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767208517493294120?= X-GMAIL-MSGID: =?utf-8?q?1767208517493294120?= 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 Reviewed-by: Chris Li (Google) --- 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: 100087 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1308980vqr; Sun, 28 May 2023 23:20:44 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5Hcb/2G1ImtRQezSNqXqygbwr4fW2iyAheNSg4YE9OfoGm1ziPsfq3G4YBe1oReoUTJQhp X-Received: by 2002:a17:902:ea06:b0:1ad:7bc5:b9ea with SMTP id s6-20020a170902ea0600b001ad7bc5b9eamr12684448plg.60.1685341244503; Sun, 28 May 2023 23:20:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685341244; cv=none; d=google.com; s=arc-20160816; b=uPTebiEWoCj79owgU1oH/XgS5sguH/gqqJivNxFQUCH3j2D8ZO6ZQtONVpdpoa76xN J2EENyafUCUtb8Ks4c7S3MuCU/b47Mhlg0mbu9b+U6mf2cb+JIQxaKqH/zCYO8Roz6DO 9prJyaBT6jxGnGpw8dhxFQGcjYe5nODnXKYIwGH/TB6wwW4o6pCT7CYP9+nO2xrdq6rH t/EQb4NK2f/NZ5PgARJQBtAebJqH6t1iAgVlnFfHfEfB2NS2mMZhY9Wq/6LbriLo4rK9 /l/yHhDz8ckUVWZ/qaZIXaGLAXa/3t4qDWxkbdrgdSaIZkNFpZHRi8rED79Vom8DJjaM 36yg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=3xIj4lwktXLAySa7XqSBq9+rB1Zc0DD28GkVkkU93b0=; b=xC0XgW+BWAhsPPkXztw2MnFLLHblYBXR+XhqU+52h6s1NsFx1nQt+qWKQ8dlc7GyAQ 7lRn5Un5gK73Gh/EoRrBRyLM8jEx0MXvIgkl9Vx0LZkE8m2jlMRFqoJdOSR99bNv/ERo 2T8hqtF6u/SpVeUqFIbmOQaJxqoO6599uLmP008QE9rf0TtHO9lWmN3EMLNH87RBAmNv 4ms8b8vCBr7GhXX4r+OECL5vXFuF6FZ/gyQmmIMo+KLSRP9M07VDjm+g79qbtizDnnnp MFZOds4swRR+/snFCQSH8nwE1+dz7hpr8VHmt0bw7rTFfOPo7ZpVpk+Z4b+2AzNucVEP I1JA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZS60TbM6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x4-20020a170902a38400b001a804319df8si4939807pla.391.2023.05.28.23.20.32; Sun, 28 May 2023 23:20:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZS60TbM6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230158AbjE2GOe (ORCPT + 99 others); Mon, 29 May 2023 02:14:34 -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 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767208380902641233?= X-GMAIL-MSGID: =?utf-8?q?1767208380902641233?= __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 Reviewed-by: Chris Li (Google) --- 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: 100089 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1309562vqr; Sun, 28 May 2023 23:22:27 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4c69NEFTmwxFiFKY+jpDKjqbFTCZiXsqk79KHvS04QZcJHZi1O4UXdNvpqWqM6QTgiRfsR X-Received: by 2002:a17:90b:84:b0:24d:e296:659b with SMTP id bb4-20020a17090b008400b0024de296659bmr7272596pjb.22.1685341347257; Sun, 28 May 2023 23:22:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685341347; cv=none; d=google.com; s=arc-20160816; b=JlL5rbRzTKlirt/ldNyT2ZoooRpB+MxkqNmK9bsS/ZE1HXeYAxzkkcEe1PdNe6jT6n yyiLw0enRdzXpN8cUIy71xWFwbwmyq1Uq67EtikvC+GovemYkTLkjz8kP6/oCzMxL4e2 PW48TOlSFoGj2ECOFnU09TE6JWnvRewj5YzaIY4zcZ3upyjcKCZKs1RMTAj1gepyner9 Tt70IQZr2v2lBmdrDEX7ZhelB4B3HiSKXZspCMlx/X8+dl59i2VrEc6T49bbGuU3txzp a6ksr2ndtgr9daMHSoGokKE7bLUnhMrACdRi+o85mBOseoTWTD7b8PB+zhhoZ5o8z7in MIew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=OXQpXUyD3R4+lamxgQGM0SaoDtlSH6lRmsX6AawfPEk=; b=PQmIN089FCeSEY7btRzho0EvUQu/KELECNJClC1dJBlU/F9ge64169+/faU0/KuoFh 0o7JLv4lr0Xl0nZkEXhub7mk+U/Bqnu0NCG0WM0vjiOdqOAzOCsc238JVQRp0qRJEH9F TiufnGVUJR31W9Rze49WFCL6tiLCv0byvj7he3okDUQDAlu1CB5f01bkbuiSyGbpnW/Z +oMoZL6R+XHbIIMsS57alGs/WSVsAwNFCqR0uRKfStRdINJuk3lpsCPMigkRHY7KAdl5 DGJgSURIQUOF6/cV+7NYzLHO/l3NdqvqtYwA8yodJPoSsCiueCpsJNJ1ZpI0aI/XubkX X8HA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=cJxrFtlC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id gt14-20020a17090af2ce00b002502c10bdb4si2241501pjb.91.2023.05.28.23.22.13; Sun, 28 May 2023 23:22:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=cJxrFtlC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230427AbjE2GOl (ORCPT + 99 others); Mon, 29 May 2023 02:14:41 -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 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767208488422666285?= X-GMAIL-MSGID: =?utf-8?q?1767208488422666285?= __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 Reviewed-by: Chris Li (Google) --- 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: 100083 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1307740vqr; Sun, 28 May 2023 23:16:59 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5beVkOrgfCaDu8UO65Blfl4yauV0zKi6jiV22khJYbou84eB8FhpOooGkTSzrRVkKX6x+H X-Received: by 2002:a05:6a00:391c:b0:64f:52c9:ddd5 with SMTP id fh28-20020a056a00391c00b0064f52c9ddd5mr14372449pfb.34.1685341018866; Sun, 28 May 2023 23:16:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685341018; cv=none; d=google.com; s=arc-20160816; b=xGok+3Oz5t7M3fmgxKSCdni4GVmZnWLNnyV+jEsrgno7IKa78Ui428LqvtVw8ordEY XqBIqwGejHEo0UFAkbPBelZwfLa0+mozXSFyoHebp96dSgt1Zg4CnMqTjvkYfUB86/YD XXi+sgEp6RHEIRjnnLP5KVvdTdZ7XtT+IK9W3mvT/QrDcJOQO8FzfEa8LTfyYjoS09Wa rUi7mvMOrT0Q8Cm0AWeUNkNR1Ioev4acjvFWhHbv8y3c+SrsaaMuWsF1Qm8QOjRDlpuS merOq/RDpXlWy2ydSJDmsOQ/JfpAqjik52aaJfmZiNc4go4tNo37KFugiKAucPZ1Vhic hwzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=fpr/LXblsUT/QgnM7sVAAykJFiLzmJXXkITKLa/9iMc=; b=ImnmNWQIAxHxoRCegD3V+mrtj325Uuj5qLqiePgtLXiFIbQVyhCyodljz8926TAYJj GytZd5C7CjktuANms4y+fef51+1/fEI8FrHEOsQiE3pdcjSm7MgO0fZ1EEHidDBVH24A twYE7/nfvSPW+7uv1e0X6WHfqJsdm1CCw2xwpD3M0o4wwkVgTRE4GIIHHy9GQBs9T9+e FTTW9Lz0ttJkIo07C6cTwgIZi3oSyrUzULfXFc8Kd/exazLwfNaDFjy99LK+iaZZInNE VaaihnuJ50LZrLXDVT/Vw9vfRIOppDeUJRbo7KlatejlTrrAKd8LsAh3R6YO54mAnXfM RUJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=SSYZBD30; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 191-20020a6307c8000000b005139d2b9306si8089807pgh.514.2023.05.28.23.16.44; Sun, 28 May 2023 23:16:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=SSYZBD30; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230328AbjE2GOt (ORCPT + 99 others); Mon, 29 May 2023 02:14:49 -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 X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767208144457712067?= X-GMAIL-MSGID: =?utf-8?q?1767208144457712067?= 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, } /* + * 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()