From patchwork Mon Mar 11 08:44:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Huang, Ying" X-Patchwork-Id: 13588365 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 35A3CC5475B for ; Mon, 11 Mar 2024 08:45:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B01E76B0074; Mon, 11 Mar 2024 04:45:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AB1E86B0075; Mon, 11 Mar 2024 04:45:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 953E96B0078; Mon, 11 Mar 2024 04:45:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 8553D6B0074 for ; Mon, 11 Mar 2024 04:45:49 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 54BE180481 for ; Mon, 11 Mar 2024 08:45:49 +0000 (UTC) X-FDA: 81884125218.11.45576EA Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.9]) by imf16.hostedemail.com (Postfix) with ESMTP id 9007A18000D for ; Mon, 11 Mar 2024 08:45:47 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ZEGxiQqq; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of ying.huang@intel.com designates 198.175.65.9 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=1710146747; 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:references:dkim-signature; bh=uTMdVIeUnxCpElyDcLdDQ83gMc7iiO+2m4d2wtk8zss=; b=kde2NgzPQ+8LK3kDn2nkl8OeVrMRBEQl7f69A8WeFDH2omnGl7jJsHO2ZmcDvsMu8lSBwU WrClK2ZQaw4JiZy7UsdY4UId1EzSA4cIylS64lfHwlahLguVc/MWdO+3n31RRo4ZP6l6Kk SBBq6vTaicG7KqfdefoIBJuvLn90s0c= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=ZEGxiQqq; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf16.hostedemail.com: domain of ying.huang@intel.com designates 198.175.65.9 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710146747; a=rsa-sha256; cv=none; b=FhetET0imutEpAvaRDm1piBKkOXkU8LYH7EB2vRp2lTs1sgLhe/XRo60YvcKFJkVdb0IoW 4+CCUwShAuusMJsSVGQ8nFo/bmeKHGmn/i+tz4VtnyqxIRgpDzUuuOl+3SzZVaf348tH7J hfswdi73lI+nvITQzyd8F1HSe2fOYiI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710146748; x=1741682748; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=5zZfkh7bTy10cbt+byFNi1KVFCC5exwzUgnJ75DgeOM=; b=ZEGxiQqq4pcItq6k/JIQjgFK4pnPuy25uNmINmRK+sFBzK4MTVr3G2d0 0PoFjPctjfnSP8E8/+guZ2mNuYr1NDdEKi7SO58EW+VuY+gfv0wLetLO3 UiGXtIOTw2iAxkzJGo1aY9HiaAVw1d0e9aXWsW7sQKGOVMCl0dFQdT5It 47c8rUPZS7EsHfHRJqx4IZTPTlh78uGQSW1OEF7jsIEQl0ZU1Nzh7gDze 4eMFn/66VB6pKrT82vxU0V+X1S3xR25w8KMGM/csB3z13WRD12dcVHy62 7x5+EE5ZEjee8r4VirudLtkNzf3QlQdrhipxpRGj7x6GhEW9x3hizHSgB A==; X-IronPort-AV: E=McAfee;i="6600,9927,11009"; a="27267163" X-IronPort-AV: E=Sophos;i="6.07,116,1708416000"; d="scan'208";a="27267163" Received: from orviesa007.jf.intel.com ([10.64.159.147]) by orvoesa101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Mar 2024 01:45:46 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,116,1708416000"; d="scan'208";a="11535184" Received: from yhuang6-mobl2.sh.intel.com ([10.238.6.133]) by orviesa007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Mar 2024 01:45:43 -0700 From: Huang Ying To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Ryan Roberts , "Huang, Ying" , David Hildenbrand , stable@vger.kernel.org Subject: [PATCH -v3] mm: swap: fix race between free_swap_and_cache() and swapoff() Date: Mon, 11 Mar 2024 16:44:26 +0800 Message-Id: <20240311084426.447164-1-ying.huang@intel.com> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-Rspamd-Queue-Id: 9007A18000D X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: nynfd93xc16cjsi5fdxfpmb34bqe5zew X-HE-Tag: 1710146747-310345 X-HE-Meta: U2FsdGVkX1/IvOnXnsXVF5VOnrOmOIisPALoamAreWPUHEQqVTa7+t+aORSJ+c5m3WgXeDbCBmSbC5VJkIDWQ/3wwcOSy0pb7rVp7zWJsEIsyG1+W+rRxzWbO0sbF3r6jKrrsdZVCkjnlYYTKvfYHvWFODVctTWFxDpEpZPKTAnbJf0UC844X4tmJdZpZFMu1MbeoyUnZXjLz1eM+ITlNUbIzet5+38+x7DF3T1+cUQw12K3y2T8IUAhjzvgNp774G6ELGPJjbrkf5+L6ND4C+jiRKAIoPkDKybRtiYU/sNqDxHDbQMFezCoDuut2/YxnT9qZm6lI9jSs6jeFTCfJ0YN0MYr7jMEK+sFzLL38LBLbb7NHXNEaj7HOg/itK9/ZMk1nJKdoGOca59xBk2TLu1m+8VOa3p08jzzTk3c5m6k89TG1oDJdV9Pg0aWmtYDzaCIW8evFQYo9i/IPybW1UORHlgZ6Sq2P3aS4ohCelC9znwili+finI8ekVOXHJy1XDYeoai+mFP1Nd3f6ci9hV7YSl+UlEpQMUTrHgMpvkZIEWfqTQnq5PGC4keTTd35tKSS1HnQjwNUYDtvcQFM4Ory/qGo7WB2z343wy+zCMYxi8TU+JbB2TaWtYEfuNyzSjd1QImyY0yQxwMJ0YTxn+j0V5hJbdg/ufHuEIU38w7zrLgJTkwgqtBJa2Do3GVh6eCwvsZr2MfxZ2KRJbOCZgkCQ36k3b+pCEfU4bv79MMkpSJ2DuH1OkSUObsJiNTvC300hJegqussNGvbhFEspUGF8UUO4HK2TjxheuPdC/dQHZdDQAmW1HEo5hMwOFFwaOQXe4DSXBS0zAHLPTi/xeEbkltIl+HffWCJjRQ3lddpqUrFi9XIKOL4R+NBmW0rDCV49xmHJzis2KgKPFMurJ2zMMQOOrSszv0Hwjiitw= 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: List-Subscribe: List-Unsubscribe: From: Ryan Roberts There was previously a theoretical window where swapoff() could run and teardown a swap_info_struct while a call to free_swap_and_cache() was running in another thread. This could cause, amongst other bad possibilities, swap_page_trans_huge_swapped() (called by free_swap_and_cache()) to access the freed memory for swap_map. This is a theoretical problem and I haven't been able to provoke it from a test case. But there has been agreement based on code review that this is possible (see link below). Fix it by using get_swap_device()/put_swap_device(), which will stall swapoff(). There was an extra check in _swap_info_get() to confirm that the swap entry was not free. This isn't present in get_swap_device() because it doesn't make sense in general due to the race between getting the reference and swapoff. So I've added an equivalent check directly in free_swap_and_cache(). Details of how to provoke one possible issue: --8<----- CPU0 CPU1 ---- ---- shmem_undo_range shmem_free_swap xa_cmpxchg_irq free_swap_and_cache __swap_entry_free /* swap_count() become 0 */ swapoff try_to_unuse shmem_unuse /* cannot find swap entry */ find_next_to_unuse filemap_get_folio folio_free_swap /* remove swap cache */ /* free si->swap_map[] */ swap_page_trans_huge_swapped <-- access freed si->swap_map !!! --8<----- Link: https://lkml.kernel.org/r/20240306140356.3974886-1-ryan.roberts@arm.com Closes: https://lore.kernel.org/linux-mm/8734t27awd.fsf@yhuang6-desk2.ccr.corp.intel.com/ Signed-off-by: Ryan Roberts Signed-off-by: "Huang, Ying" [patch description] Cc: David Hildenbrand Cc: Signed-off-by: Andrew Morton --- Hi, Andrew, If it's not too late. Please replace v2 of this patch in mm-stable with this version. Changes since v2: - Remove comments for get_swap_device() because it's not correct. - Revised patch description about the race condition description. Changes since v1: - Added comments for get_swap_device() as suggested by David - Moved check that swap entry is not free from get_swap_device() to free_swap_and_cache() since there are some paths that legitimately call with a free offset. Best Regards, Huang, Ying mm/swapfile.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mm/swapfile.c b/mm/swapfile.c index 2b3a2d85e350..9e0691276f5e 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1609,13 +1609,19 @@ int free_swap_and_cache(swp_entry_t entry) if (non_swap_entry(entry)) return 1; - p = _swap_info_get(entry); + p = get_swap_device(entry); if (p) { + if (WARN_ON(data_race(!p->swap_map[swp_offset(entry)]))) { + put_swap_device(p); + return 0; + } + count = __swap_entry_free(p, entry); if (count == SWAP_HAS_CACHE && !swap_page_trans_huge_swapped(p, entry)) __try_to_reclaim_swap(p, swp_offset(entry), TTRS_UNMAPPED | TTRS_FULL); + put_swap_device(p); } return p != NULL; }