From mboxrd@z Thu Jan 1 00:00:00 1970 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 3C657C48BF6 for ; Thu, 7 Mar 2024 04:41:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BDD296B0100; Wed, 6 Mar 2024 23:41:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B8D056B0101; Wed, 6 Mar 2024 23:41:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A54B96B0102; Wed, 6 Mar 2024 23:41:15 -0500 (EST) 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 92B5F6B0100 for ; Wed, 6 Mar 2024 23:41:15 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 74744A0342 for ; Thu, 7 Mar 2024 04:41:15 +0000 (UTC) X-FDA: 81868993710.13.D5252F1 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) by imf07.hostedemail.com (Postfix) with ESMTP id B5FB94000B for ; Thu, 7 Mar 2024 04:41:13 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=QdwXMRqU; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf07.hostedemail.com: domain of ying.huang@intel.com designates 198.175.65.18 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=1709786473; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=17AjEfiQYZ7HSyhzAScyix3TEeKmT3YX586pxackRoY=; b=DJcJJkWBCHN2XqL120dBGV0vicBd2LAT3noL9F2jUjSppJcGNxUuQ9E781PeWRUYg4jSNt td6E7CBCt5IW30NfbCJVHIwoLX2SrFFtX2tLGYWxeHO6NJKp/EmxS+w+HC41gGmc0zRuTK Aok5pl94qi8trQVzQ8xYCExc5L4rfZQ= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=QdwXMRqU; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf07.hostedemail.com: domain of ying.huang@intel.com designates 198.175.65.18 as permitted sender) smtp.mailfrom=ying.huang@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709786473; a=rsa-sha256; cv=none; b=cfGZxKq1MJEsK2iFqitcAzDSCiuspPKGT8ARSSduuB6cFRtml7GifwI7q793TaKLPZn8rG MJZ6VByhwI5IZGNYuG6m1AHVQhtk7FKsJDPIzzUt/XIWK+IQZW01JpDV1dNFq18AJ5hdlu Q7EKyS0mafQ5BAVScHb4K+n/iuH5TFU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1709786474; x=1741322474; h=from:to:cc:subject:in-reply-to:references:date: message-id:mime-version; bh=XYbe7O1rnmoARPI/KbGn27Qyuj3Way4wqe09R1GltNI=; b=QdwXMRqUr+jdOhgJ5oukj5btxr1H3GBDmlJpXIFdXOAcQyCghmJCNtcM F8D1HjkoCJ8B1SYFK1VMGqJ6srN5YPF+yQWwU1pjAAVXE20P5y0hiGRi+ DA9i++apz+QhA6K2WzfTDjn6WOXDLcYf6qNBfmHD8gGnrdTXkmo5pwEnr /uoOXTgVTHUf3dDndePm6a8R5ACAtIDdYFpzZF25FeOQYP3POIIufmYaP EoSK/xG17K1cqx+IqvXIrTrTAT867rjqAaXWnmpdlW9rNolqg8ty1SQpP ZO4X0dfEu2i1xYVZPYkTYNEjtqAd7+MXTjnEZWdTwrcUvQcCwp7ssS62h w==; X-IronPort-AV: E=McAfee;i="6600,9927,11005"; a="4559105" X-IronPort-AV: E=Sophos;i="6.06,209,1705392000"; d="scan'208";a="4559105" Received: from orviesa010.jf.intel.com ([10.64.159.150]) by orvoesa110.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Mar 2024 20:41:13 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,209,1705392000"; d="scan'208";a="9965527" Received: from yhuang6-desk2.sh.intel.com (HELO yhuang6-desk2.ccr.corp.intel.com) ([10.238.208.55]) by orviesa010-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Mar 2024 20:41:11 -0800 From: "Huang, Ying" To: Miaohe Lin Cc: Ryan Roberts , Andrew Morton , David Hildenbrand , , , Subject: Re: [PATCH v1] mm: swap: Fix race between free_swap_and_cache() and swapoff() In-Reply-To: (Miaohe Lin's message of "Wed, 6 Mar 2024 16:51:22 +0800") References: <20240305151349.3781428-1-ryan.roberts@arm.com> <875xy0842q.fsf@yhuang6-desk2.ccr.corp.intel.com> Date: Thu, 07 Mar 2024 12:39:16 +0800 Message-ID: <87jzme7j0r.fsf@yhuang6-desk2.ccr.corp.intel.com> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain; charset=ascii X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: B5FB94000B X-Stat-Signature: kdhnqcce1iksjupz7s98z9tmod98k6yk X-Rspam-User: X-HE-Tag: 1709786473-490350 X-HE-Meta: U2FsdGVkX1/H3LKBGe5T734w/XHVCohPTRKiwAsE8S1+ISfPQwJPUjHX65z7Do/G2ReWaKARN4DaHNSANJxZXVs8r57fblBEw2bzGte9XrDxuQXAc9DRwwuMNveW1NBKR7T7qqrPJ2hNzfiMZfSdyyt09vbrL+jUkJHodeiiyfbk7Np8jlyRFbWlf41aVt7yLfAEGD9ZFoVgCA+SGvPuFI5uo+hRVczToC5cdd3M9zJiwj7fdtyWseNFZCW+Al63Qu+Q5j51InHtM0XUm+j2x82bFTptHm+Jajbq9D++Qh4xXDDEYSuk6Z6V6OkFg3zs6S3T4pEsnf5vvVRVjYG9bcAGIApTkzf/lLIfJDP92SBN5FkCuu0eJxF9cyDe3yAyh9ZJyXIH0v9wODFX4nKkQEr04tp3y8xqUNG3pyrB0yy+5MgBNM196OadF8vCOpePlmGz4hspDVXGxagNnRndrTZIcwbrPpgydRZaNE7DPpG1Z7mzTva23LIKBnWBqdbxgARjW1CqjU1pbwofpY5VH+w/wvDs/Xz21C9M29dzTmfvZ9lJ47Pf4hquWgpWc6BAq1dDrGnuO8lYYsSf/fHMdQ27DxsjrMZthP1JpxWUJJlGeFBZYzryLP68EVNOZH2GUt+mOsX1kxXkMKFm9GRLNYlvxRoEwLRlDNfcelw4HK3csQyJKm9BdFOcQi68ld2rt+Vb9kDRF1h8KOxV5YM8eJJbLtpyMiWSlRldFq7LrSIpnfPRn/2fEPLkFs0SSggDMFYpNV/QHYSYYXL6WjM26qp/+s5WwTAIRxCIkViH/33lnwkGodqJr02ZG1KbL18Erh8vvk+oInoVmrh8cu97MQsdeELbB8D3yU0X2YUnsZp/91LYFwlJir2GtQI8EHnzHzYET19HD5s= 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: Miaohe Lin writes: > On 2024/3/6 10:52, Huang, Ying wrote: >> Ryan Roberts writes: >> >>> 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 valid. This wasn't present in get_swap_device() so >>> I've added it. I couldn't find any existing get_swap_device() call sites >>> where this extra check would cause any false alarms. >>> >>> Details of how to provoke one possible issue (thanks to David Hilenbrand >>> for deriving this): >>> >>> --8<----- >>> >>> __swap_entry_free() might be the last user and result in >>> "count == SWAP_HAS_CACHE". >>> >>> swapoff->try_to_unuse() will stop as soon as soon as si->inuse_pages==0. >>> >>> So the question is: could someone reclaim the folio and turn >>> si->inuse_pages==0, before we completed swap_page_trans_huge_swapped(). >>> >>> Imagine the following: 2 MiB folio in the swapcache. Only 2 subpages are >>> still references by swap entries. >>> >>> Process 1 still references subpage 0 via swap entry. >>> Process 2 still references subpage 1 via swap entry. >>> >>> Process 1 quits. Calls free_swap_and_cache(). >>> -> count == SWAP_HAS_CACHE >>> [then, preempted in the hypervisor etc.] >>> >>> Process 2 quits. Calls free_swap_and_cache(). >>> -> count == SWAP_HAS_CACHE >>> >>> Process 2 goes ahead, passes swap_page_trans_huge_swapped(), and calls >>> __try_to_reclaim_swap(). >>> >>> __try_to_reclaim_swap()->folio_free_swap()->delete_from_swap_cache()-> >>> put_swap_folio()->free_swap_slot()->swapcache_free_entries()-> >>> swap_entry_free()->swap_range_free()-> >>> ... >>> WRITE_ONCE(si->inuse_pages, si->inuse_pages - nr_entries); >>> >>> What stops swapoff to succeed after process 2 reclaimed the swap cache >>> but before process1 finished its call to swap_page_trans_huge_swapped()? >>> >>> --8<----- >> >> I think that this can be simplified. Even for a 4K folio, this could >> happen. >> >> CPU0 CPU1 >> ---- ---- >> >> zap_pte_range >> free_swap_and_cache >> __swap_entry_free >> /* swap count become 0 */ >> swapoff >> try_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 !!! > > Sorry for jumping the discussion here. IMHO, free_swap_and_cache is called with pte lock held. > So synchronize_rcu (called by swapoff) will wait zap_pte_range to release the pte lock. So this > theoretical problem can't happen. Or am I miss something? > > CPU0 CPU1 > ---- ---- > > zap_pte_range > pte_offset_map_lock -- spin_lock is held. > free_swap_and_cache > __swap_entry_free > /* swap count become 0 */ > swapoff > try_to_unuse > filemap_get_folio > folio_free_swap > /* remove swap cache */ > percpu_ref_kill(&p->users); > swap_page_trans_huge_swapped > pte_unmap_unlock -- spin_lock is released. > synchronize_rcu(); --> Will wait pte_unmap_unlock to be called? > /* free si->swap_map[] */ I think that you are right. We are safe if PTL is held. Thanks a lot for pointing this out! -- Best Regards, Huang, Ying