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 0F7B9C48260 for ; Fri, 16 Feb 2024 18:01:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 825A88D0001; Fri, 16 Feb 2024 13:01:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7D5136B0088; Fri, 16 Feb 2024 13:01:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6023F8D0001; Fri, 16 Feb 2024 13:01:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 4ECA06B0085 for ; Fri, 16 Feb 2024 13:01:38 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id E9631140125 for ; Fri, 16 Feb 2024 18:01:37 +0000 (UTC) X-FDA: 81798434634.22.B27F123 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf25.hostedemail.com (Postfix) with ESMTP id 59738A001B for ; Fri, 16 Feb 2024 18:01:34 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=eVv+a0kC; spf=pass (imf25.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708106494; 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=aPeQsXRRlSRnTkrY5ryff46Xk3MXoXe6wCZy2RF2ng8=; b=ppfZ3YThB9TVTL5XEq9n0PpjaAFKlOWNoh9AfxUZ9RTSBBeqm8WzdJ4KzIPqR0GVo/i/dd ZFkCFmF8pPDOuniLArQNlKNSPsQjmeXIcRtOqnUnP7c9uMTpuxuiJeSJUHRPENyWT7Muwl zVVzwOySZDyLs49gf4b5nQsB6dA9te8= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=eVv+a0kC; spf=pass (imf25.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708106494; a=rsa-sha256; cv=none; b=aT8jo6lJbQ2qHWP09F6QMewZ1K/rE4Gce+mvzvnpEKYfbqMVBmcgoHZkgO/TmjS7fcy5bW 8ThUNcp1jDe5Lw+JKB007e04XvQoKnDzNg4sFl1y8uRg+T/H31hNfpygv9wUsDuIwCwRaG PEEwbeckNG4CWckGu1WO86oBjBj4Z+0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1708106493; h=from:from: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:autocrypt:autocrypt; bh=aPeQsXRRlSRnTkrY5ryff46Xk3MXoXe6wCZy2RF2ng8=; b=eVv+a0kCw4eWXx//uHyvngXnSrsTm3DZOtU8XJIIaZrYY8pZ268pNq1NxueidrvaFb8Jqw POGeh0Qc6alq2oJdn+wtO6swyQMorAy9ZLIJAc+wJPr+WAFMqBwH7RB+8hgvrPTJ4JP2tj q1PQ0zmw+liMYa7R7Dkz64A5a57XsFk= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-167-QQufhJlIPriLxS6ktMzQ9g-1; Fri, 16 Feb 2024 13:01:31 -0500 X-MC-Unique: QQufhJlIPriLxS6ktMzQ9g-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-40e40126031so12636715e9.0 for ; Fri, 16 Feb 2024 10:01:31 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708106491; x=1708711291; h=content-transfer-encoding:in-reply-to:organization:autocrypt :references:cc:to:from:content-language:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=aPeQsXRRlSRnTkrY5ryff46Xk3MXoXe6wCZy2RF2ng8=; b=gPVgI3T1nh3UU1/C7zTjXXb8E7cOqgM7wuWaYI5o73/8LOPtd5pCbv8f5sMKWTyo4g N+5bUFoKEX0CzEdFjhlvO69kpB50r52p+Kr5UfitXzIuk5aZYqf3qeD0EYLecUBMhAKq Xzvi6PaqfBKyq0F2YBB4p88BV2+TUvUpyU2wtyHe2NMo/Y38cT+BWUksG/MpdmpCgfz3 6tmsNCSViUr8EaDfueFwCJI6QMEzXiRpHgrdPZcZAbdBPw81dByacV18UbQ13ahcfkgp JNsXaLOL0dwvyOX3y1AMnuHO8wcugMpvMM5qolyKBwsrWE4LWJG++KDIB9WYJlHtwdEt Z6lQ== X-Forwarded-Encrypted: i=1; AJvYcCU3oTNnwFVjcyoj060YyPMX/cyCbWRnHpysUun2DDte2C9zGSw8lHzUkRWLLhAWYybsMjTTESsQXPMoPhg2RaoYAek= X-Gm-Message-State: AOJu0YxzUFbizCd4oGzf1KJTcrPOwBnmwhJL0LA2rezm4KWUT49X1x2X QP6Rxh9TtUZlncA34Km9uPcF1+lXF6uCcdM85g7UTmBWpYjqqOomrEInqRXeTHzOQJaPo79K0pE 94jCcH3gJAunlLqNKs73DeWcBJzo9RqOYDlWRP51MnlgOPhON X-Received: by 2002:a5d:6f0d:0:b0:33d:1d6f:9a30 with SMTP id ay13-20020a5d6f0d000000b0033d1d6f9a30mr2434275wrb.27.1708106490771; Fri, 16 Feb 2024 10:01:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IEJWWRPc5Uvwi3BEUnSY+TUGlvsL2HHWr3ZR/5laB1qhSPOmap5CfcOtgdKaLtQFmjieV3ZTA== X-Received: by 2002:a5d:6f0d:0:b0:33d:1d6f:9a30 with SMTP id ay13-20020a5d6f0d000000b0033d1d6f9a30mr2434245wrb.27.1708106490310; Fri, 16 Feb 2024 10:01:30 -0800 (PST) Received: from ?IPV6:2003:d8:2f3c:3f00:7177:eb0c:d3d2:4b0e? (p200300d82f3c3f007177eb0cd3d24b0e.dip0.t-ipconnect.de. [2003:d8:2f3c:3f00:7177:eb0c:d3d2:4b0e]) by smtp.gmail.com with ESMTPSA id u5-20020a056000038500b0033b66ce7ae9sm2873580wrf.84.2024.02.16.10.01.29 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 16 Feb 2024 10:01:29 -0800 (PST) Message-ID: Date: Fri, 16 Feb 2024 19:01:28 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3] mm/swap: fix race when skipping swapcache From: David Hildenbrand To: Kairui Song , linux-mm@kvack.org Cc: Andrew Morton , "Huang, Ying" , Chris Li , Minchan Kim , 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 References: <20240216095105.14502-1-ryncsn@gmail.com> Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYj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5deGuwxgb65pWIzufa N7eop7uh+6bezi+rugUI+w6DABEBAAHCwXwEGAEIACYCGwwWIQQb2cqtc1xMOkYN/MpN3hD3 AP+DWgUCXw7HsgUJEqkpoQAKCRBN3hD3AP+DWrrpD/4qS3dyVRxDcDHIlmguXjC1Q5tZTwNB boaBTPHSy/Nksu0eY7x6HfQJ3xajVH32Ms6t1trDQmPx2iP5+7iDsb7OKAb5eOS8h+BEBDeq 3ecsQDv0fFJOA9ag5O3LLNk+3x3q7e0uo06XMaY7UHS341ozXUUI7wC7iKfoUTv03iO9El5f XpNMx/YrIMduZ2+nd9Di7o5+KIwlb2mAB9sTNHdMrXesX8eBL6T9b+MZJk+mZuPxKNVfEQMQ a5SxUEADIPQTPNvBewdeI80yeOCrN+Zzwy/Mrx9EPeu59Y5vSJOx/z6OUImD/GhX7Xvkt3kq Er5KTrJz3++B6SH9pum9PuoE/k+nntJkNMmQpR4MCBaV/J9gIOPGodDKnjdng+mXliF3Ptu6 3oxc2RCyGzTlxyMwuc2U5Q7KtUNTdDe8T0uE+9b8BLMVQDDfJjqY0VVqSUwImzTDLX9S4g/8 kC4HRcclk8hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 59738A001B X-Rspam-User: X-Stat-Signature: cxbrgjx69euboda5go465ymjqf6pyi3c X-Rspamd-Server: rspam01 X-HE-Tag: 1708106493-537415 X-HE-Meta: U2FsdGVkX1/iWVpf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mq54WU4vwPveJUcUD2B+rW7t4WsdOJ6NiFQZ9YXU/qRsx4v3ODMLUl3u8smVhvXkakKu5jdeHSUkatBa+WcAgXNXCCWwkkZoH42Fqj17LBDzUWu3BzzQwGhohdB0hH01SKdVcyXuKyMiAKhtO064g/MVu/5ygSrAVIRRyLU23tuUCytVL6TaDo9NxqEkfuXA4fEvylbWFt/Vvpr6EQgZZnxmhQeGiROKTGEV3E9+0DBUwRic6pmj1UbD7iSVsPX0itsdHSUdAkaV7Q02V23blR+7WCTZSTWrp0yj2CcXKnmCy4ZMZVi41Q3SY8TnvbqiV5eZZN3URHhisLJqxlR8dBGDCg2lFX7RnnaM3UONBUak17+Kc8kDmgJjMkkschM2clguiZi23fm2ElRYpbKYfbHPtwVPBywF1I0ssGMQesGs/KCK4l7yqdCnbQlhBC2mU9xwDOmrdiqhfYylTKqpbhTQs382wdnll8Ndo+CW6C4z2uCH0I77553z1kevb517N1I 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 16.02.24 17:53, David Hildenbrand wrote: > On 16.02.24 10:51, Kairui Song wrote: >> From: Kairui Song >> >> 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 wait since it's a rare and very >> short event. A schedule() call is added to avoid wasting too much CPU >> or adding too much noise to perf statistics >> >> 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) >> >> Fixes: 0bcac06f27d7 ("mm, swap: skip swapcache for swapin of synchronous device") >> Link: https://github.com/ryncsn/emm-test-project/tree/master/swap-stress-race [1] >> Reported-by: "Huang, Ying" >> Closes: https://lore.kernel.org/lkml/87bk92gqpx.fsf_-_@yhuang6-desk2.ccr.corp.intel.com/ >> Signed-off-by: Kairui Song >> Cc: stable@vger.kernel.org >> >> --- >> Update from V2: >> - Add a schedule() if raced to prevent repeated page faults wasting CPU >> and add noise to perf statistics. >> - Use a bool to state the special case instead of reusing existing >> variables fixing error handling [Minchan Kim]. >> >> V2: https://lore.kernel.org/all/20240206182559.32264-1-ryncsn@gmail.com/ >> >> Update from V1: >> - Add some words on ZRAM case, it will discard swap content on swap_free so the race window is a bit different but cure is the same. [Barry Song] >> - Update comments make it cleaner [Huang, Ying] >> - Add a function place holder to fix CONFIG_SWAP=n built [SeongJae Park] >> - Update the commit message and summary, refer to SWP_SYNCHRONOUS_IO instead of "direct swapin path" [Yu Zhao] >> - Update commit message. >> - Collect Review and Acks. >> >> V1: https://lore.kernel.org/all/20240205110959.4021-1-ryncsn@gmail.com/ >> >> include/linux/swap.h | 5 +++++ >> mm/memory.c | 20 ++++++++++++++++++++ >> mm/swap.h | 5 +++++ >> mm/swapfile.c | 13 +++++++++++++ >> 4 files changed, 43 insertions(+) >> >> diff --git a/include/linux/swap.h b/include/linux/swap.h >> index 4db00ddad261..8d28f6091a32 100644 >> --- a/include/linux/swap.h >> +++ b/include/linux/swap.h >> @@ -549,6 +549,11 @@ static inline int swap_duplicate(swp_entry_t swp) >> return 0; >> } >> >> +static inline int swapcache_prepare(swp_entry_t swp) >> +{ >> + return 0; >> +} >> + >> static inline void swap_free(swp_entry_t swp) >> { >> } >> diff --git a/mm/memory.c b/mm/memory.c >> index 7e1f4849463a..7059230d0a54 100644 >> --- a/mm/memory.c >> +++ b/mm/memory.c >> @@ -3799,6 +3799,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) >> struct page *page; >> struct swap_info_struct *si = NULL; >> rmap_t rmap_flags = RMAP_NONE; >> + bool need_clear_cache = false; >> bool exclusive = false; >> swp_entry_t entry; >> pte_t pte; >> @@ -3867,6 +3868,20 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) >> 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)) { >> + /* Relax a bit to prevent rapid repeated page faults */ >> + schedule(); >> + goto out; >> + } >> + need_clear_cache = true; >> + > > I took a closer look at __read_swap_cache_async() and it essentially > does something similar. > > Instead of returning, it keeps retrying until it finds that > swapcache_prepare() fails for another reason than -EEXISTS (e.g., > freed concurrently) or it finds the entry in the swapcache. > > So if you would succeed here on a freed+reused swap entry, > __read_swap_cache_async() would simply retry. > > It spells that out: > > /* > * We might race against __delete_from_swap_cache(), and > * stumble across a swap_map entry whose SWAP_HAS_CACHE > * has not yet been cleared. Or race against another > * __read_swap_cache_async(), which has set SWAP_HAS_CACHE > * in swap_map, but not yet added its folio to swap cache. > */ > > Whereby we could not race against this code here as well where we > speculatively set SWAP_HAS_CACHE and might never add something to the swap > cache. > > > I'd probably avoid the wrong returns and do something even closer to > __read_swap_cache_async(). > > while (true) { > /* > * Fake that we are trying to insert a page into the swapcache, to > * serialize against concurrent threads wanting to do the same. > * [more from your description] > */ > ret = swapcache_prepare(entry); > if (likely(!ret) > /* > * Move forward with swapin, we'll recheck if the PTE hasn't > * changed later. > */ > break; > else if (ret != -EEXIST) > goto out; > > /* > * See __read_swap_cache_async(). We might either have raced against > * another thread, or the entry could have been freed and reused in the > * meantime. Make sure that the PTE did not change, to detect freeing. > */ > vmf->pte = pte_offset_map_lock(vma->vm_mm, vmf->pmd, > vmf->address, &vmf->ptl); > if (!vmf->pte || !pte_same(ptep_get(vmf->pte), vmf->orig_pte)) > goto unlock; > > > schedule(); > } > > > > I was skeptical about the schedule(), but __read_swap_cache_async() does it > already because there is no better way to wait for the event to happen. > > With something like above you would no longer depend on the speed of schedule() to > determine how often you would retry the fault, which would likely make sense. > > I do wonder about the schedule() vs. schedule_timeout_uninterruptible(), though. > No expert on that area, do you have any idea? > Forgot to add Acked-by: David Hildenbrand But I suspect we do not want to not rely on schedule() to actually sleep, and instead keep retrying until the other thread finished, similar to above. -- Cheers, David / dhildenb