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 D5767C4829E for ; Thu, 15 Feb 2024 22:58:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 209788D0010; Thu, 15 Feb 2024 17:58:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1B9B48D0001; Thu, 15 Feb 2024 17:58:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 05AA08D0010; Thu, 15 Feb 2024 17:58:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id E3D2A8D0001 for ; Thu, 15 Feb 2024 17:58:40 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 9D372A047C for ; Thu, 15 Feb 2024 22:58:40 +0000 (UTC) X-FDA: 81795554400.05.12DA69F Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf23.hostedemail.com (Postfix) with ESMTP id D325814001D for ; Thu, 15 Feb 2024 22:58:38 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=OwAgFTCP; spf=pass (imf23.hostedemail.com: domain of akpm@linux-foundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708037919; a=rsa-sha256; cv=none; b=yMXUGF3eYhAyfCOPnQt9/G+eFW2ejSRW0Kcn4SfuKK64mhx3By58uxdFflwtQ09LVrwISK gXKfkRE0kJVG6cBv4vTddVgWEXIKMKtX1jwC1eseVJR03rjVWRi/dSQWR6ui8CET8Hga6V znekvfec0mXPQCf2BZy3lSx5ZIMZRFE= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=linux-foundation.org header.s=korg header.b=OwAgFTCP; spf=pass (imf23.hostedemail.com: domain of akpm@linux-foundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=akpm@linux-foundation.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708037919; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=UP1uLsW+r2LMkYHArdCAkoAKQxdPMScL74860zBN0+o=; b=YbfcVa0eQqru7GBU0VNXi/u2azK1JPnWHeqHf8GnuS6Dsj/8PtE3E8MMMIJR1qwhVqQwnJ nCFBYCJ8ZfIBqi63fSBPCizDgKviZZSTISvtFfNuRDFlqP51ivHUC3SG3Ybsnf+UjjfGq9 KTn05xT1JPv5+Iv59d3waJCY780F3FI= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 2682161E56; Thu, 15 Feb 2024 22:58:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 400E9C433C7; Thu, 15 Feb 2024 22:58:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1708037917; bh=LKA/ClUmTfQI6DQT0IR/p+d0WUTkStXM03s4oe6WFx8=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=OwAgFTCPc6Uk64NzZ6GhG8EtJnwxDjnbWjnDV6G447crYs2YFfsyzmavZQXar+sOn CNknwea6X9YcvX/hNfVLJ//0OohAladfqIWc/EWQan6yTyeaNZNoWzLMtw6O/+L+Wm XL4YGf3f8J++vDDGzp0OFjeJcDMSHP4Ba4pG/KcI= Date: Thu, 15 Feb 2024 14:58:36 -0800 From: Andrew Morton To: Minchan Kim Cc: David Hildenbrand , Kairui Song , linux-mm@kvack.org, "Huang, Ying" , Chris Li , Yu Zhao , Barry Song , SeongJae Park , Hugh Dickins , Johannes Weiner , Matthew Wilcox , Michal Hocko , Yosry Ahmed , stable@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] mm/swap: fix race when skipping swapcache Message-Id: <20240215145836.6db8f62824e1547f0608591c@linux-foundation.org> In-Reply-To: References: <20240206182559.32264-1-ryncsn@gmail.com> <1d259a51-46e6-4d3b-9455-38dbcc17b168@redhat.com> <4c651673-132f-4cd8-997e-175f586fd2e6@redhat.com> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: D325814001D X-Stat-Signature: 57ycp31xabfprdgexkfemkheusdta6t6 X-Rspam-User: X-HE-Tag: 1708037918-285592 X-HE-Meta: U2FsdGVkX1+xYYJ37Y+3zw+ERJO56Pg7mlVA2O6uduh74dnkYkcSFjcUwRB8KxEvyz9+3sjrf5b0Xq6jnQyDMWRVIeVShmMmsEyM8aB8S3JDtGRYZ98xUwj1TG5AO8M1BbrCJY084IbXtjKGmYVPDCLzW8EkCtOymrj2HoyB6c3vvURQB9hM2E+oiPm/5c/PorTd19iD/cfWmf+FWfDawdahBZVMJnEWA8Xc6kAfoqD7RGHjS8OhzClzqOsKip0IqX0vLFkxTgc1B0O8bd6brnCDI35E1J1vuCvv4cIkzk28Qql619wPsA5i3JXdH+GmXXPEXVNZ5vnBdqmTvOVxvXW+go9I1i0V52tHEE2dHYIuWVhinN+lCkpjMAdJvuaodYWeVh/DIasi0UnPJ323+MAxVze9o6uLC5BcMU2q1cfzDComv6RkN6qg7Ft5o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ymu95+MU3SabhY4hdoXva7l5MzoZTZrnSIZT7SAb3xRpjfyh3XRFZzUQaI59HN4qkSKaTLedepKK/cSATwjw3aTeKjgRDzN6N6RQ6wLPlVhYcnKwhU697bQDeis0LLtHn+w82JvmnyA0EG31uUxKv9R/NfVVliTZ1Zcvz46VSXR1pYVHi/Xc0qJ7Pzqs= 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: On Thu, 15 Feb 2024 12:55:11 -0800 Minchan Kim wrote: > Only concern of the approach is that it would be harder to have the fix > in the stable tree. If there isn't strong objection, I prefer the > Kairui's orginal solution(with some tweak of scheduler if it's > necessary) first and then pursue your idea on latest tree. Do we agree that this fix is needed in -stable? For some reason I don't have a cc:stable in the changelog. I'd like to move this patch into mm-hotfixes-stable in order to get it upstream very soon. Are we all agreeable with that? I don't have an Acked-by: David? I have a note (which dates back to an earlier version) that Barry would be performing runtime testing. Has that occurred? Thanks. Here's what is presently in mm-hotfixes-unstable: From: Kairui Song Subject: mm/swap: fix race when skipping swapcache Date: Wed, 7 Feb 2024 02:25:59 +0800 When skipping swapcache for SWP_SYNCHRONOUS_IO, if two or more threads swapin the same entry at the same time, they get different pages (A, B). Before one thread (T0) finishes the swapin and installs page (A) to the PTE, another thread (T1) could finish swapin of page (B), swap_free the entry, then swap out the possibly modified page reusing the same entry. It breaks the pte_same check in (T0) because PTE value is unchanged, causing ABA problem. Thread (T0) will install a stalled page (A) into the PTE and cause data corruption. One possible callstack is like this: CPU0 CPU1 ---- ---- do_swap_page() do_swap_page() with same entry swap_read_folio() <- read to page A swap_read_folio() <- read to page B ... set_pte_at() swap_free() <- entry is free pte_same() <- Check pass, PTE seems unchanged, but page A is stalled! swap_free() <- page B content lost! set_pte_at() <- staled page A installed! And besides, for ZRAM, swap_free() allows the swap device to discard the entry content, so even if page (B) is not modified, if swap_read_folio() on CPU0 happens later than swap_free() on CPU1, it may also cause data loss. To fix this, reuse swapcache_prepare which will pin the swap entry using the cache flag, and allow only one thread to pin it. Release the pin after PT unlocked. Racers will simply busy wait since it's a rare and very short event. Other methods like increasing the swap count don't seem to be a good idea after some tests, that will cause racers to fall back to use the swap cache again. Parallel swapin using different methods leads to a much more complex scenario. Reproducer: This race issue can be triggered easily using a well constructed reproducer and patched brd (with a delay in read path) [1]: With latest 6.8 mainline, race caused data loss can be observed easily: $ gcc -g -lpthread test-thread-swap-race.c && ./a.out Polulating 32MB of memory region... Keep swapping out... Starting round 0... Spawning 65536 workers... 32746 workers spawned, wait for done... Round 0: Error on 0x5aa00, expected 32746, got 32743, 3 data loss! Round 0: Error on 0x395200, expected 32746, got 32743, 3 data loss! Round 0: Error on 0x3fd000, expected 32746, got 32737, 9 data loss! Round 0 Failed, 15 data loss! This reproducer spawns multiple threads sharing the same memory region using a small swap device. Every two threads updates mapped pages one by one in opposite direction trying to create a race, with one dedicated thread keep swapping out the data out using madvise. The reproducer created a reproduce rate of about once every 5 minutes, so the race should be totally possible in production. After this patch, I ran the reproducer for over a few hundred rounds and no data loss observed. Performance overhead is minimal, microbenchmark swapin 10G from 32G zram: Before: 10934698 us After: 11157121 us Non-direct: 13155355 us (Dropping SWP_SYNCHRONOUS_IO flag) Link: https://lkml.kernel.org/r/20240206182559.32264-1-ryncsn@gmail.com Fixes: 0bcac06f27d7 ("mm, swap: skip swapcache for swapin of synchronous device") Reported-by: "Huang, Ying" Closes: https://lore.kernel.org/lkml/87bk92gqpx.fsf_-_@yhuang6-desk2.ccr.corp.intel.com/ Link: https://github.com/ryncsn/emm-test-project/tree/master/swap-stress-race [1] Signed-off-by: Kairui Song Reviewed-by: "Huang, Ying" Acked-by: Yu Zhao Cc: Chris Li Cc: David Hildenbrand Cc: Hugh Dickins Cc: Johannes Weiner Cc: Matthew Wilcox (Oracle) Cc: Michal Hocko Cc: Minchan Kim Cc: Yosry Ahmed Cc: Yu Zhao Cc: Barry Song <21cnbao@gmail.com> Cc: SeongJae Park Signed-off-by: Andrew Morton --- include/linux/swap.h | 5 +++++ mm/memory.c | 15 +++++++++++++++ mm/swap.h | 5 +++++ mm/swapfile.c | 13 +++++++++++++ 4 files changed, 38 insertions(+) --- a/include/linux/swap.h~mm-swap-fix-race-when-skipping-swapcache +++ a/include/linux/swap.h @@ -549,6 +549,11 @@ static inline int swap_duplicate(swp_ent return 0; } +static inline int swapcache_prepare(swp_entry_t swp) +{ + return 0; +} + static inline void swap_free(swp_entry_t swp) { } --- a/mm/memory.c~mm-swap-fix-race-when-skipping-swapcache +++ a/mm/memory.c @@ -3867,6 +3867,16 @@ vm_fault_t do_swap_page(struct vm_fault if (!folio) { if (data_race(si->flags & SWP_SYNCHRONOUS_IO) && __swap_count(entry) == 1) { + /* + * Prevent parallel swapin from proceeding with + * the cache flag. Otherwise, another thread may + * finish swapin first, free the entry, and swapout + * reusing the same entry. It's undetectable as + * pte_same() returns true due to entry reuse. + */ + if (swapcache_prepare(entry)) + goto out; + /* skip swapcache */ folio = vma_alloc_folio(GFP_HIGHUSER_MOVABLE, 0, vma, vmf->address, false); @@ -4116,6 +4126,9 @@ vm_fault_t do_swap_page(struct vm_fault unlock: if (vmf->pte) pte_unmap_unlock(vmf->pte, vmf->ptl); + /* Clear the swap cache pin for direct swapin after PTL unlock */ + if (folio && !swapcache) + swapcache_clear(si, entry); out: if (si) put_swap_device(si); @@ -4124,6 +4137,8 @@ out_nomap: if (vmf->pte) pte_unmap_unlock(vmf->pte, vmf->ptl); out_page: + if (!swapcache) + swapcache_clear(si, entry); folio_unlock(folio); out_release: folio_put(folio); --- a/mm/swapfile.c~mm-swap-fix-race-when-skipping-swapcache +++ a/mm/swapfile.c @@ -3365,6 +3365,19 @@ int swapcache_prepare(swp_entry_t entry) return __swap_duplicate(entry, SWAP_HAS_CACHE); } +void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry) +{ + struct swap_cluster_info *ci; + unsigned long offset = swp_offset(entry); + unsigned char usage; + + ci = lock_cluster_or_swap_info(si, offset); + usage = __swap_entry_free_locked(si, offset, SWAP_HAS_CACHE); + unlock_cluster_or_swap_info(si, ci); + if (!usage) + free_swap_slot(entry); +} + struct swap_info_struct *swp_swap_info(swp_entry_t entry) { return swap_type_to_swap_info(swp_type(entry)); --- a/mm/swap.h~mm-swap-fix-race-when-skipping-swapcache +++ a/mm/swap.h @@ -41,6 +41,7 @@ void __delete_from_swap_cache(struct fol void delete_from_swap_cache(struct folio *folio); void clear_shadow_from_swap_cache(int type, unsigned long begin, unsigned long end); +void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry); struct folio *swap_cache_get_folio(swp_entry_t entry, struct vm_area_struct *vma, unsigned long addr); struct folio *filemap_get_incore_folio(struct address_space *mapping, @@ -97,6 +98,10 @@ static inline int swap_writepage(struct return 0; } +static inline void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry) +{ +} + static inline struct folio *swap_cache_get_folio(swp_entry_t entry, struct vm_area_struct *vma, unsigned long addr) { _