From patchwork Mon May 22 07:09:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13249902 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 C89E0C77B73 for ; Mon, 22 May 2023 07:09:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 319FF900009; Mon, 22 May 2023 03:09:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2CC1C900002; Mon, 22 May 2023 03:09:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1924C900009; Mon, 22 May 2023 03:09:44 -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 075BF900002 for ; Mon, 22 May 2023 03:09:44 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C39E780C5D for ; Mon, 22 May 2023 07:09:43 +0000 (UTC) X-FDA: 80817015846.14.7A6DC69 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by imf19.hostedemail.com (Postfix) with ESMTP id A3AE41A0010 for ; Mon, 22 May 2023 07:09:41 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=DqxZboZR; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf19.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.126 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=1684739381; 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=783Bty5BOC1F0ZcikWUj9ZBiF3bJgnEki1bFOjoWQOc=; b=M4twr+s4O4HH1p8vVl8JefFjxsArT9y66jEhKAmb6LMR/1ODwdWJJOns5SzicY0g3mnO8O bUVxtXDbAututW2LxWjoKBXiCwWU8UW0drI87BRjEWIPutUpUWmO0S3dFOSAjcpwfssgxA tVCEF+WRgNUElgw6SoX90TS3zVRyXc8= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=DqxZboZR; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf19.hostedemail.com: domain of ying.huang@intel.com designates 134.134.136.126 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684739381; a=rsa-sha256; cv=none; b=h7SP+JikbgrPNBkVSXi18fxB9/8uzMLCa5eqk9CvxI3IhJFY7BbR+dbJd0/PGndLb53yIc abhf7CsjbFKx/Kd1lpVKkQUgdbMlIV6aEQwF8B66101pQgADUQ3KBLqIAmKxQULRMhqMbK YDOa4NpbETOqetUoUKSjKHkKnRGO3vY= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1684739381; x=1716275381; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pFsVEqpuPiRv19w/rqm+IY358s8W3zh5r+eFbJEC2g0=; b=DqxZboZRSjsMzFAjFX46t10GrQaUyFtDX8QN4xQb3/hAohwfsLHQ50pa +vG3VQHj0HR/mTwAZy7tUkITc8jgQdNbkKUuLQv9Nhz1m04anrZMrRzWV /xajBYXgRkLmxm+LoGwbIU7KsVPcsmcxv0WDNY7F9S4kylqa07byliTJ1 uMfh3QjDBR8buQ8l8rjO5mux67Q+Q2q96HINQKTE8Xuc85hYgknYDwtu2 F8qsNAXrtkwT3RWsdazV0jEfkU6Bcx7WcdWnxznha6XgyBLQ0PjAoaQwy visertsmO5/VoEgslmLQFK1ZgK/4Ye0gf7zEQjTK9iZvZJJ7DdXn7poWi Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10717"; a="337437064" X-IronPort-AV: E=Sophos;i="6.00,183,1681196400"; d="scan'208";a="337437064" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 May 2023 00:09:38 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10717"; a="773212746" X-IronPort-AV: E=Sophos;i="6.00,183,1681196400"; d="scan'208";a="773212746" Received: from yhuang6-mobl2.sh.intel.com ([10.238.5.152]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 22 May 2023 00:09:34 -0700 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Huang Ying , David Hildenbrand , Hugh Dickins , Johannes Weiner , Matthew Wilcox , Michal Hocko , Minchan Kim , Tim Chen , Yang Shi , Yu Zhao Subject: [PATCH -V2 5/5] swap: comments get_swap_device() with usage rule Date: Mon, 22 May 2023 15:09:05 +0800 Message-Id: <20230522070905.16773-6-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230522070905.16773-1-ying.huang@intel.com> References: <20230522070905.16773-1-ying.huang@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: A3AE41A0010 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: jripa6cx4tz7kpa4gowbubkzr5n7y7bp X-HE-Tag: 1684739381-311139 X-HE-Meta: U2FsdGVkX1+RnrfWAwdTcmVopNdZ3TYk13HTyhlbFqvnMvikfX/sl0c3cWXzDO94UZiaquGnlgpMLS4bVMRTzVDD+NTy6avOQe7i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tP4wCUFnKPN63GmRQKCS+UgoEWnD+K3QxYgqFbNTLVV+9HD8aq+idHyasOW5ZMfW+m2rUwkaKq1LDlDb7CrBfA+oPoI5JLhdeCexy6/lQLMrgYs+IK9UduvItmgYsUYbYYruhnEs3/c0E9eAb/dyVu73SNRmkGfxj7+uxo6H9otIU3yka3NUY6lZLuh9sJK5r3vXZPSBjWx1tWsKgTRcI7xrnwbRdCoEaXxn3cIkrzhIzXMuZkPEw8V7UML7r08Ru7YCW0fbJWzuo+Nrb2zE8Ld7ZPeW78GnPAz63/zHYdaI2PQ4c63uJxDXy3VZt7YA3+va3nmBupBI= 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 isn't some other way to prevent swapoff, such as page lock for swap cache, page table lock, etc., the swap entry may become invalid because of swapoff. Then, we need to enclose all swap related functions with get_swap_device() and put_swap_device(), unless the swap functions call get/put_swap_device() by themselves. Add the rule as comments of get_swap_device(). Signed-off-by: "Huang, Ying" Cc: David Hildenbrand Cc: Hugh Dickins Cc: Johannes Weiner Cc: Matthew Wilcox Cc: Michal Hocko Cc: Minchan Kim Cc: Tim Chen Cc: Yang Shi Cc: Yu Zhao Reviewed-by: David Hildenbrand Reviewed-by: Yosry Ahmed --- mm/swapfile.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 4dbaea64635d..0c1cb935b2eb 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1219,6 +1219,13 @@ static unsigned char __swap_entry_free_locked(struct swap_info_struct *p, } /* + * When we get a swap entry, if there isn't some other way to prevent + * swapoff, such as page lock for swap cache, page table lock, etc., + * the swap entry may become invalid because of swapoff. Then, we + * need to enclose all swap related functions with get_swap_device() + * and put_swap_device(), unless the swap functions call + * get/put_swap_device() by themselves. + * * Check whether swap entry is valid in the swap device. If so, * return pointer to swap_info_struct, and keep the swap entry valid * via preventing the swap device from being swapoff, until @@ -1227,9 +1234,8 @@ static unsigned char __swap_entry_free_locked(struct swap_info_struct *p, * Notice that swapoff or swapoff+swapon can still happen before the * percpu_ref_tryget_live() in get_swap_device() or after the * percpu_ref_put() in put_swap_device() if there isn't any other way - * to prevent swapoff, such as page lock, page table lock, etc. The - * caller must be prepared for that. For example, the following - * situation is possible. + * to prevent swapoff. The caller must be prepared for that. For + * example, the following situation is possible. * * CPU1 CPU2 * do_swap_page()