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-354523 X-HE-Meta: U2FsdGVkX1+7EaGrpw8YA2vg1Fn1FC8ys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OXvyWM9hcbW8s5vJhh6vGpoAsm6z7HsucNCrkqKF8bm7TWO4aQfb8Il/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()